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_PREFIX "chbind: "
42 #define ENSC_WRAPPERS_IO 1
43 #define ENSC_WRAPPERS_UNISTD 1
46 #define CMD_HELP 0x1000
47 #define CMD_VERSION 0x1001
49 #define CMD_SILENT 0x2000
51 #define CMD_BCAST 0x2002
53 int wrapper_exit_code = 255;
56 static struct option const
58 { "help", no_argument, 0, CMD_HELP },
59 { "version", no_argument, 0, CMD_VERSION },
60 { "silent", no_argument, 0, CMD_SILENT },
61 { "ip", required_argument, 0, CMD_IP },
62 { "bcast", required_argument, 0, CMD_BCAST },
67 showHelp(int fd, char const *cmd, int res)
69 WRITE_MSG(fd, "Usage:\n ");
72 " [--silent] [--ip <ip_num>[/<mask>]] [--bcast <broadcast>] [--] <commands> <args>*\n\n"
73 "Please report bugs to " PACKAGE_BUGREPORT "\n");
82 "chbind " VERSION " -- bind to an ip and execute a program\n"
83 "This program is part of " PACKAGE_STRING "\n\n"
84 "Copyright (C) 2003,2004 Enrico Scholz\n"
85 VERSION_COPYRIGHT_DISCLAIMER);
90 Check if a network device exist in /proc/net/dev.
91 This is used because ifconfig_ioctl triggers modprobe if requesting
92 information about non existant devices.
94 Return != 0 if the device exist.
97 existsDevice(char const *dev_raw)
100 char dev[strlen(dev_raw)+2];
102 strcpy(dev, dev_raw);
108 int fd=open("/proc/net/dev", O_RDONLY);
110 if (fd==-1) return false;
111 too_small = EreadAll(fd, buf, buf_size);
119 pos = strstr(buf, dev);
120 return (pos && (pos==buf || pos[-1]==' ' || pos[-1]=='\n'));
124 static int ifconfig_ioctl(
130 strcpy(ifr->ifr_name, ifname);
131 return ioctl(fd, cmd,ifr);
135 Fetch the IP number of an interface from the kernel.
136 Assume the device is already available in the kernel
137 Return -1 if any error.
139 int ifconfig_getaddr (
146 if (existsDevice(ifname)){
147 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
152 if (ifconfig_ioctl(skfd,ifname,SIOCGIFADDR, &ifr) >= 0){
153 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
154 *addr = sin->sin_addr.s_addr;
157 if (ifconfig_ioctl(skfd,ifname,SIOCGIFNETMASK, &ifr) >= 0){
158 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
159 *mask = sin->sin_addr.s_addr;
162 if (ifconfig_ioctl(skfd,ifname,SIOCGIFBRDADDR, &ifr) >= 0){
163 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
164 *bcast = sin->sin_addr.s_addr;
174 readIP(char const *str, struct vc_ip_mask_pair *ip, uint32_t *bcast)
176 if (ifconfig_getaddr(str, &ip->ip, &ip->mask, bcast)==-1) {
178 char tmpopt[strlen(str)+1];
182 pt = strchr(tmpopt,'/');
185 ip->mask = ntohl(0xffffff00);
189 // Ok, we have a network size, not a netmask
190 if (strchr(pt,'.')==0) {
193 for (ip->mask = 0; sz>0; --sz) {
195 ip->mask |= 0x80000000;
197 ip->mask = ntohl(ip->mask);
200 struct hostent *h = gethostbyname (pt);
202 WRITE_MSG(2, "Invalid netmask '");
205 exit(wrapper_exit_code);
208 memcpy (&ip->mask, h->h_addr, sizeof(ip->mask));
212 h = gethostbyname (tmpopt);
214 WRITE_MSG(2, "Invalid IP number or host name '");
215 WRITE_STR(2, tmpopt);
217 exit(wrapper_exit_code);
220 memcpy (&ip->ip, h->h_addr,sizeof(ip->ip));
225 readBcast(char const *str, uint32_t *bcast)
228 if (ifconfig_getaddr(str, &tmp, &tmp, bcast)==-1){
229 struct hostent *h = gethostbyname (str);
231 WRITE_MSG(2, "Invalid broadcast number '");
232 WRITE_STR(2, optarg);
234 exit(wrapper_exit_code);
236 memcpy (bcast, h->h_addr,sizeof(*bcast));
240 int main (int argc, char *argv[])
242 bool is_silent = false;
243 struct vc_ip_mask_pair ips[16];
245 uint32_t bcast = 0xffffffff;
248 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
252 case CMD_HELP : showHelp(1, argv[0], 0);
253 case CMD_VERSION : showVersion();
254 case CMD_SILENT : is_silent = true; break;
255 case CMD_BCAST : readBcast(optarg, &bcast); break;
258 WRITE_MSG(2, "Too many IP numbers, max 16\n");
259 exit(wrapper_exit_code);
261 readIP(optarg, ips+nbaddrs, &bcast);
265 WRITE_MSG(2, "Try '");
266 WRITE_STR(2, argv[0]);
267 WRITE_MSG(2, " --help\" for more information.\n");
268 exit(wrapper_exit_code);
274 WRITE_MSG(2, "No command given; try '--help' for more information\n");
275 exit(wrapper_exit_code);
279 if (vc_set_ipv4root(bcast,nbaddrs,ips)!=0) {
280 perror("chbind: vc_set_ipv4root()");
281 exit(wrapper_exit_code);
287 WRITE_MSG(1, "ipv4root is now");
288 for (i=0; i<nbaddrs; ++i) {
290 WRITE_STR(1, inet_ntoa(*reinterpret_cast(struct in_addr *)(&ips[i].ip)));
295 Eexecvp (argv[optind],argv+optind);
299 #ifdef ENSC_TESTSUITE
304 struct vc_ip_mask_pair ip;
309 readIP("1.2.3.4", &ip, &bcast);
310 assert(ip.ip==ntohl(0x01020304) && ip.mask==ntohl(0xffffff00) && bcast==0);
312 readIP("1.2.3.4/8", &ip, &bcast);
313 assert(ip.ip==ntohl(0x01020304) && ip.mask==ntohl(0xff000000) && bcast==0);
315 readIP("1.2.3.4/255.255.0.0", &ip, &bcast);
316 assert(ip.ip==ntohl(0x01020304) && ip.mask==ntohl(0xffff0000) && bcast==0);
318 readIP("localhost", &ip, &bcast);
319 assert(ip.ip==ntohl(0x7f000001) && ip.mask==ntohl(0xffffff00) && bcast==0);
322 if (ifconfig_getaddr("lo", &tmp, &tmp, &tmp)!=-1) {
323 readIP("lo", &ip, &bcast);
324 assert(ip.ip==ntohl(0x7f000001) && ip.mask==ntohl(0xff000000) && bcast==ntohl(0x7fffffff));