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.
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
35 fprintf (stderr,"chbind version %s\n",VERSION);
36 fprintf (stderr,"chbind [ --silent ] [ --ip ip_num[/mask] ] [ --bcast broadcast ] command argument\n");
41 Check if a network device exist in /proc/net/dev.
42 This is used because ifconfig_ioctl triggers modprobe if requesting
43 information about non existant devices.
45 Return != 0 if the device exist.
47 static int chbind_devexist (const char *dev)
50 FILE *fin = fopen ("/proc/net/dev","r");
52 int len = strlen(dev);
54 fgets(buf,sizeof(buf)-1,fin); // Skip one line
55 while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
56 const char *pt = strstr(buf,dev);
57 if (pt != NULL && pt[len] == ':'){
68 static int ifconfig_ioctl(
74 strcpy(ifr->ifr_name, ifname);
75 return ioctl(fd, cmd,ifr);
79 Fetch the IP number of an interface from the kernel.
80 Assume the device is already available in the kernel
81 Return -1 if any error.
83 int ifconfig_getaddr (
90 if (chbind_devexist(ifname)){
91 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
96 if (ifconfig_ioctl(skfd,ifname,SIOCGIFADDR, &ifr) >= 0){
97 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
98 *addr = sin->sin_addr.s_addr;
101 if (ifconfig_ioctl(skfd,ifname,SIOCGIFNETMASK, &ifr) >= 0){
102 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
103 *mask = sin->sin_addr.s_addr;
106 if (ifconfig_ioctl(skfd,ifname,SIOCGIFBRDADDR, &ifr) >= 0){
107 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
108 *bcast = sin->sin_addr.s_addr;
120 int main (int argc, char *argv[])
125 unsigned long addrs[16],masks[16];
127 unsigned long bcast = 0xffffffff;
128 for (i=1; i<argc; i++){
129 const char *arg = argv[i];
130 const char *opt = argv[i+1];
131 if (strcmp(arg,"--ip")==0){
132 unsigned long addr,mask;
134 fprintf (stderr,"Too many IP numbers, max 16, ignored\n");
136 }else if (ifconfig_getaddr(opt,&addr,&mask,&bcast)==-1){
137 unsigned long mask = 0x00ffffff;
138 const char *pt = strchr(opt,'/');
139 char tmpopt[strlen(opt)+1];
144 tmpopt[pt-opt] = '\0';
147 if (strchr(pt,'.')==NULL){
148 // Ok, we have a network size, not a netmask
152 for (i=0; i<size; i++){
158 struct hostent *h = gethostbyname (pt);
160 memcpy (&mask,h->h_addr,sizeof(mask));
162 fprintf (stderr,"Invalid netmask: %s\n",pt);
169 h = gethostbyname (opt);
171 fprintf (stderr,"Invalid IP number or host name: %s\n",opt);
174 memcpy (&addr,h->h_addr,sizeof(addr));
175 masks[nbaddrs] = mask;
176 addrs[nbaddrs++] = addr;
179 masks[nbaddrs] = mask;
180 addrs[nbaddrs++] = addr;
183 }else if (strcmp(arg,"--bcast")==0){
185 if (ifconfig_getaddr(opt,&tmp,&tmp,&bcast)==-1){
186 struct hostent *h = gethostbyname (opt);
188 fprintf (stderr,"Invalid broadcast number: %s\n",opt);
191 memcpy (&bcast,h->h_addr,sizeof(bcast));
195 }else if (strcmp(arg,"--silent")==0){
203 }else if (argv[i][0] == '-'){
206 if (call_set_ipv4root(addrs,nbaddrs,bcast,masks)==0){
209 printf ("ipv4root is now");
210 for (i=0; i<nbaddrs; i++){
211 unsigned long hostaddr = ntohl(addrs[i]);
212 printf (" %ld.%ld.%ld.%ld"
220 execvp (argv[i],argv+i);
221 fprintf (stderr,"Can't exec %s (%s)\n",argv[i],strerror(errno));
223 fprintf (stderr,"Can't set the ipv4 root (%s)\n",strerror(errno));