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.
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <netinet/in.h>
39 fprintf (stderr,"chbind version %s\n",VERSION);
40 fprintf (stderr,"chbind [ --silent ] [ --ip ip_num[/mask] ] [ --bcast broadcast ] command argument\n");
45 Check if a network device exist in /proc/net/dev.
46 This is used because ifconfig_ioctl triggers modprobe if requesting
47 information about non existant devices.
49 Return != 0 if the device exist.
51 static int chbind_devexist (const char *dev)
54 FILE *fin = fopen ("/proc/net/dev","r");
56 int len = strlen(dev);
58 fgets(buf,sizeof(buf)-1,fin); // Skip one line
59 while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
60 const char *pt = strstr(buf,dev);
61 if (pt != NULL && pt[len] == ':'){
72 static int ifconfig_ioctl(
78 strcpy(ifr->ifr_name, ifname);
79 return ioctl(fd, cmd,ifr);
83 Fetch the IP number of an interface from the kernel.
84 Assume the device is already available in the kernel
85 Return -1 if any error.
87 int ifconfig_getaddr (
94 if (chbind_devexist(ifname)){
95 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
100 if (ifconfig_ioctl(skfd,ifname,SIOCGIFADDR, &ifr) >= 0){
101 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
102 *addr = sin->sin_addr.s_addr;
105 if (ifconfig_ioctl(skfd,ifname,SIOCGIFNETMASK, &ifr) >= 0){
106 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
107 *mask = sin->sin_addr.s_addr;
110 if (ifconfig_ioctl(skfd,ifname,SIOCGIFBRDADDR, &ifr) >= 0){
111 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
112 *bcast = sin->sin_addr.s_addr;
124 int main (int argc, char *argv[])
129 unsigned long addrs[16],masks[16];
131 unsigned long bcast = 0xffffffff;
132 for (i=1; i<argc; i++){
133 const char *arg = argv[i];
134 const char *opt = argv[i+1];
135 if (strcmp(arg,"--ip")==0){
136 unsigned long addr,mask;
138 fprintf (stderr,"Too many IP numbers, max 16, ignored\n");
140 }else if (ifconfig_getaddr(opt,&addr,&mask,&bcast)==-1){
141 unsigned long mask = 0x00ffffff;
142 const char *pt = strchr(opt,'/');
143 char tmpopt[strlen(opt)+1];
148 tmpopt[pt-opt] = '\0';
151 if (strchr(pt,'.')==NULL){
152 // Ok, we have a network size, not a netmask
156 for (i=0; i<size; i++){
162 struct hostent *h = gethostbyname (pt);
164 memcpy (&mask,h->h_addr,sizeof(mask));
166 fprintf (stderr,"Invalid netmask: %s\n",pt);
173 h = gethostbyname (opt);
175 fprintf (stderr,"Invalid IP number or host name: %s\n",opt);
178 memcpy (&addr,h->h_addr,sizeof(addr));
179 masks[nbaddrs] = mask;
180 addrs[nbaddrs++] = addr;
183 masks[nbaddrs] = mask;
184 addrs[nbaddrs++] = addr;
187 }else if (strcmp(arg,"--bcast")==0){
189 if (ifconfig_getaddr(opt,&tmp,&tmp,&bcast)==-1){
190 struct hostent *h = gethostbyname (opt);
192 fprintf (stderr,"Invalid broadcast number: %s\n",opt);
195 memcpy (&bcast,h->h_addr,sizeof(bcast));
199 }else if (strcmp(arg,"--silent")==0){
207 }else if (argv[i][0] == '-'){
210 if (call_set_ipv4root(addrs,nbaddrs,bcast,masks)==0){
213 printf ("ipv4root is now");
214 for (i=0; i<nbaddrs; i++){
215 unsigned long hostaddr = ntohl(addrs[i]);
216 printf (" %ld.%ld.%ld.%ld"
224 execvp (argv[i],argv+i);
225 fprintf (stderr,"Can't exec %s (%s)\n",argv[i],strerror(errno));
227 fprintf (stderr,"Can't set the ipv4 root (%s)\n",strerror(errno));