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 <lib/vserver.h>
31 #define ENSC_WRAPPERS_PREFIX "nattribute: "
32 #define ENSC_WRAPPERS_VSERVER 1
33 #define ENSC_WRAPPERS_UNISTD 1
36 #define CMD_HELP 0x1000
37 #define CMD_VERSION 0x1001
38 #define CMD_NID 0x2000
39 #define CMD_SET 0x2001
40 #define CMD_CAP 0x2002
41 #define CMD_FLAG 0x2003
42 #define CMD_SECURE 0x2004
44 int wrapper_exit_code = 1;
48 { "help", no_argument, 0, CMD_HELP },
49 { "version", no_argument, 0, CMD_VERSION },
50 { "nid", required_argument, 0, CMD_NID },
51 { "set", no_argument, 0, CMD_SET },
52 { "ncap", required_argument, 0, CMD_CAP },
53 { "flag", required_argument, 0, CMD_FLAG },
54 { "secure", no_argument, 0, CMD_SECURE },
60 struct vc_net_flags flags;
61 struct vc_net_caps caps;
65 showHelp(int fd, char const *cmd, int res)
67 WRITE_MSG(fd, "Usage:\n ");
70 " --set [--nid <nid>] [--ncap [~!]<ncap>] [--flag [~!]<flag>] [--secure] -- [<program> <args>*]\n"
72 " --ncap <cap> ... network capability to be added\n"
73 " --flag <flag> ... network flag to be added\n"
75 "Please report bugs to " PACKAGE_BUGREPORT "\n");
84 "nattribute " VERSION " -- sets attributes of network contexts\n"
85 "This program is part of " PACKAGE_STRING "\n\n"
86 "Copyright (C) 2004 Enrico Scholz\n"
87 "Copyright (C) 2006 Daniel Hokka Zakrisson\n"
88 VERSION_COPYRIGHT_DISCLAIMER);
93 parseFlags(char const *str, struct vc_net_flags *flags)
95 struct vc_err_listparser err;
98 rc = vc_list2nflag(str, 0, &err, flags);
101 WRITE_MSG(2, "Unknown flag '");
102 Vwrite(2, err.ptr, err.len);
104 exit(wrapper_exit_code);
109 parseNCaps(char const *str, struct vc_net_caps *caps)
111 struct vc_err_listparser err;
114 rc = vc_list2ncap(str,0, &err, caps);
117 WRITE_MSG(2, "Unknown ncap '");
118 Vwrite(2, err.ptr, err.len);
120 exit(wrapper_exit_code);
125 parseSecure(struct vc_net_flags * flags,
126 struct vc_net_caps * caps)
128 // TODO: generalize this
129 caps->ncaps = VC_NXC_RAW_ICMP;
130 caps->cmask = VC_NXC_RAW_ICMP;
132 flags->flagword = VC_NXF_HIDE_NETIF;
133 flags->mask = VC_NXF_HIDE_NETIF;
136 int main(int argc, char *argv[])
138 struct Arguments args = {
140 .flags = { .flagword = 0, .mask = 0 },
141 .caps = { .ncaps = 0, .cmask = 0 },
145 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
149 case CMD_HELP : showHelp(1, argv[0], 0);
150 case CMD_VERSION : showVersion();
151 case CMD_SET : break; // default op currently
152 case CMD_NID : args.nid = Evc_nidopt2nid(optarg,true); break;
153 case CMD_FLAG : parseFlags(optarg, &args.flags); break;
154 case CMD_CAP : parseNCaps(optarg, &args.caps); break;
155 case CMD_SECURE : parseSecure(&args.flags, &args.caps); break;
157 WRITE_MSG(2, "Try '");
158 WRITE_STR(2, argv[0]);
159 WRITE_MSG(2, " --help' for more information.\n");
165 if (args.nid==VC_NOCTX) args.nid = Evc_get_task_nid(0);
167 if (args.caps.cmask &&
168 vc_set_ncaps(args.nid, &args.caps)==-1)
169 perror(ENSC_WRAPPERS_PREFIX "vc_set_ncaps()");
170 else if (args.flags.mask &&
171 vc_set_nflags(args.nid, &args.flags)==-1)
172 perror(ENSC_WRAPPERS_PREFIX "vc_set_nflags()");
173 else if (optind<argc)
174 EexecvpD(argv[optind], argv+optind);