3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on chbind.cc by Jacques Gelinas
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; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
41 #define ENSC_WRAPPERS_IO 1
42 #define ENSC_WRAPPERS_UNISTD 1
45 #define CMD_HELP 0x1000
46 #define CMD_VERSION 0x1001
48 #define CMD_SILENT 0x2000
50 #define CMD_BCAST 0x2002
52 int wrapper_exit_code = 255;
55 static struct option const
57 { "help", no_argument, 0, CMD_HELP },
58 { "version", no_argument, 0, CMD_VERSION },
59 { "silent", no_argument, 0, CMD_SILENT },
60 { "ip", required_argument, 0, CMD_IP },
61 { "bcast", required_argument, 0, CMD_BCAST },
66 showHelp(int fd, char const *cmd, int res)
68 WRITE_MSG(fd, "Usage:\n ");
71 " [--silent] [--ip <ip_num>[/<mask>]] [--bcast <broadcast>] [--] <commands> <args>*\n\n"
72 "Please report bugs to " PACKAGE_BUGREPORT "\n");
81 "chbind " VERSION " -- bind to an ip and execute a program\n"
82 "This program is part of " PACKAGE_STRING "\n\n"
83 "Copyright (C) 2003,2004 Enrico Scholz\n"
84 VERSION_COPYRIGHT_DISCLAIMER);
89 Check if a network device exist in /proc/net/dev.
90 This is used because ifconfig_ioctl triggers modprobe if requesting
91 information about non existant devices.
93 Return != 0 if the device exist.
96 existsDevice(char const *dev_raw)
99 char dev[strlen(dev_raw)+2];
101 strcpy(dev, dev_raw);
107 int fd=open("/proc/net/dev", O_RDONLY);
109 if (fd==-1) return false;
110 too_small = EreadAll(fd, buf, buf_size);
118 pos = strstr(buf, dev);
119 return (pos && (pos==buf || pos[-1]==' ' || pos[-1]=='\n'));
123 static int ifconfig_ioctl(
129 strcpy(ifr->ifr_name, ifname);
130 return ioctl(fd, cmd,ifr);
134 Fetch the IP number of an interface from the kernel.
135 Assume the device is already available in the kernel
136 Return -1 if any error.
138 int ifconfig_getaddr (
145 if (existsDevice(ifname)){
146 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
151 if (ifconfig_ioctl(skfd,ifname,SIOCGIFADDR, &ifr) >= 0){
152 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
153 *addr = sin->sin_addr.s_addr;
156 if (ifconfig_ioctl(skfd,ifname,SIOCGIFNETMASK, &ifr) >= 0){
157 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
158 *mask = sin->sin_addr.s_addr;
161 if (ifconfig_ioctl(skfd,ifname,SIOCGIFBRDADDR, &ifr) >= 0){
162 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
163 *bcast = sin->sin_addr.s_addr;
173 readIP(char const *str, struct vc_ip_mask_pair *ip, uint32_t *bcast)
175 if (ifconfig_getaddr(str, &ip->ip, &ip->mask, bcast)==-1) {
177 char tmpopt[strlen(str)+1];
181 pt = strchr(tmpopt,'/');
184 ip->mask = ntohl(0xffffff00);
188 // Ok, we have a network size, not a netmask
189 if (strchr(pt,'.')==0) {
192 for (ip->mask = 0; sz>0; --sz) {
194 ip->mask |= 0x80000000;
196 ip->mask = ntohl(ip->mask);
199 struct hostent *h = gethostbyname (pt);
201 WRITE_MSG(2, "Invalid netmask '");
204 exit(wrapper_exit_code);
207 memcpy (&ip->mask, h->h_addr, sizeof(ip->mask));
211 h = gethostbyname (tmpopt);
213 WRITE_MSG(2, "Invalid IP number or host name '");
214 WRITE_STR(2, tmpopt);
216 exit(wrapper_exit_code);
219 memcpy (&ip->ip, h->h_addr,sizeof(ip->ip));
224 readBcast(char const *str, uint32_t *bcast)
227 if (ifconfig_getaddr(str, &tmp, &tmp, bcast)==-1){
228 struct hostent *h = gethostbyname (str);
230 WRITE_MSG(2, "Invalid broadcast number '");
231 WRITE_STR(2, optarg);
233 exit(wrapper_exit_code);
235 memcpy (bcast, h->h_addr,sizeof(*bcast));
239 int main (int argc, char *argv[])
241 bool is_silent = false;
242 struct vc_ip_mask_pair ips[16];
244 uint32_t bcast = 0xffffffff;
247 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
251 case CMD_HELP : showHelp(1, argv[0], 0);
252 case CMD_VERSION : showVersion();
253 case CMD_SILENT : is_silent = true; break;
254 case CMD_BCAST : readBcast(optarg, &bcast); break;
257 WRITE_MSG(2, "Too many IP numbers, max 16\n");
258 exit(wrapper_exit_code);
260 readIP(optarg, ips+nbaddrs, &bcast);
264 WRITE_MSG(2, "Try '");
265 WRITE_STR(2, argv[0]);
266 WRITE_MSG(2, " --help\" for more information.\n");
267 exit(wrapper_exit_code);
273 WRITE_MSG(2, "No command given; try '--help' for more information\n");
274 exit(wrapper_exit_code);
278 if (vc_set_ipv4root(bcast,nbaddrs,ips)!=0) {
279 perror("vc_set_ipv4root()");
280 exit(wrapper_exit_code);
286 WRITE_MSG(1, "ipv4root is now");
287 for (i=0; i<nbaddrs; ++i) {
289 WRITE_STR(1, inet_ntoa(*reinterpret_cast(struct in_addr *)(&ips[i].ip)));
294 Eexecvp (argv[optind],argv+optind);
298 #ifdef ENSC_TESTSUITE
303 struct vc_ip_mask_pair ip;
308 readIP("1.2.3.4", &ip, &bcast);
309 assert(ip.ip==ntohl(0x01020304) && ip.mask==ntohl(0xffffff00) && bcast==0);
311 readIP("1.2.3.4/8", &ip, &bcast);
312 assert(ip.ip==ntohl(0x01020304) && ip.mask==ntohl(0xff000000) && bcast==0);
314 readIP("1.2.3.4/255.255.0.0", &ip, &bcast);
315 assert(ip.ip==ntohl(0x01020304) && ip.mask==ntohl(0xffff0000) && bcast==0);
317 readIP("localhost", &ip, &bcast);
318 assert(ip.ip==ntohl(0x7f000001) && ip.mask==ntohl(0xffffff00) && bcast==0);
320 if (ifconfig_getaddr("lo", &tmp, &tmp, &tmp)!=-1) {
321 readIP("lo", &ip, &bcast);
322 assert(ip.ip==ntohl(0x7f000001) && ip.mask==ntohl(0xff000000) && bcast==ntohl(0x7fffffff));