3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // Copyright (C) 2006 Daniel Hokka Zakrisson
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; version 2 of the License.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "attribute-util.h"
26 #include <lib/vserver.h>
32 #define ENSC_WRAPPERS_PREFIX "nattribute: "
33 #define ENSC_WRAPPERS_VSERVER 1
34 #define ENSC_WRAPPERS_UNISTD 1
37 #define CMD_HELP 0x1000
38 #define CMD_VERSION 0x1001
39 #define CMD_NID 0x2000
40 #define CMD_SET 0x2001
41 #define CMD_CAP 0x2002
42 #define CMD_FLAG 0x2003
43 #define CMD_SECURE 0x2004
44 #define CMD_GET 0x2005
46 int wrapper_exit_code = 1;
50 { "help", no_argument, 0, CMD_HELP },
51 { "version", no_argument, 0, CMD_VERSION },
52 { "nid", required_argument, 0, CMD_NID },
53 { "set", no_argument, 0, CMD_SET },
54 { "ncap", required_argument, 0, CMD_CAP },
55 { "flag", required_argument, 0, CMD_FLAG },
56 { "secure", no_argument, 0, CMD_SECURE },
57 { "get", no_argument, 0, CMD_GET },
64 struct vc_net_flags flags;
65 struct vc_net_caps caps;
69 showHelp(int fd, char const *cmd, int res)
71 WRITE_MSG(fd, "Usage:\n ");
74 " {--set|--get} [--nid <nid>] [--ncap [~!]<ncap>] [--flag [~!]<flag>] [--secure] --\n"
75 " [<program> <args>*]\n"
77 " --ncap <cap> ... network capability to be added\n"
78 " --flag <flag> ... network flag to be added\n"
80 "Please report bugs to " PACKAGE_BUGREPORT "\n");
89 "nattribute " VERSION " -- sets/gets attributes of network contexts\n"
90 "This program is part of " PACKAGE_STRING "\n\n"
91 "Copyright (C) 2004 Enrico Scholz\n"
92 "Copyright (C) 2006 Daniel Hokka Zakrisson\n"
93 VERSION_COPYRIGHT_DISCLAIMER);
98 parseFlags(char const *str, struct vc_net_flags *flags)
100 struct vc_err_listparser err;
103 rc = vc_list2nflag(str, 0, &err, flags);
106 WRITE_MSG(2, "Unknown flag '");
107 Vwrite(2, err.ptr, err.len);
109 exit(wrapper_exit_code);
114 parseNCaps(char const *str, struct vc_net_caps *caps)
116 struct vc_err_listparser err;
119 rc = vc_list2ncap(str,0, &err, caps);
122 WRITE_MSG(2, "Unknown ncap '");
123 Vwrite(2, err.ptr, err.len);
125 exit(wrapper_exit_code);
130 parseSecure(struct vc_net_flags * flags,
131 struct vc_net_caps * caps)
133 // TODO: generalize this
134 caps->ncaps = VC_NXC_RAW_ICMP;
135 caps->cmask = VC_NXC_RAW_ICMP;
137 flags->flagword = VC_NXF_HIDE_NETIF;
138 flags->mask = VC_NXF_HIDE_NETIF;
142 printAttrs(struct Arguments *args)
144 struct vc_net_flags flags;
145 struct vc_net_caps caps;
147 Evc_get_nflags(args->nid, &flags);
148 Evc_get_ncaps(args->nid, &caps);
150 print_bitfield(1, ncap, "ncapabilities", &caps.ncaps);
151 print_bitfield(1, nflag, "nflags", &flags.flagword);
156 int main(int argc, char *argv[])
158 struct Arguments args = {
161 .flags = { .flagword = 0, .mask = 0 },
162 .caps = { .ncaps = 0, .cmask = 0 },
166 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
170 case CMD_HELP : showHelp(1, argv[0], 0);
171 case CMD_VERSION : showVersion();
172 case CMD_SET : args.mode = CMD_SET; break;
173 case CMD_GET : args.mode = CMD_GET; break;
174 case CMD_NID : args.nid = Evc_nidopt2nid(optarg,true); break;
175 case CMD_FLAG : parseFlags(optarg, &args.flags); break;
176 case CMD_CAP : parseNCaps(optarg, &args.caps); break;
177 case CMD_SECURE : parseSecure(&args.flags, &args.caps); break;
179 WRITE_MSG(2, "Try '");
180 WRITE_STR(2, argv[0]);
181 WRITE_MSG(2, " --help' for more information.\n");
187 if (args.nid==VC_NOCTX) args.nid = Evc_get_task_nid(0);
189 if (args.mode == CMD_SET) {
190 if (args.caps.cmask &&
191 vc_set_ncaps(args.nid, &args.caps)==-1)
192 perror(ENSC_WRAPPERS_PREFIX "vc_set_ncaps()");
193 else if (args.flags.mask &&
194 vc_set_nflags(args.nid, &args.flags)==-1)
195 perror(ENSC_WRAPPERS_PREFIX "vc_set_nflags()");
196 else if (optind<argc)
197 EexecvpD(argv[optind], argv+optind);
201 else if (args.mode == CMD_GET) {
204 EexecvpD(argv[optind], argv+optind);