3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on ifspec.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.
21 Prints the specs of a network device in shell like form
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <netinet/in.h>
39 fprintf (stderr,"ifspec version %s\n",VERSION);
41 ,"ifspec network-device [ ipaddr netmask broadcast ]\n"
42 "prints device specification in a shell usable way\n");
46 static int ifconfig_ioctl(
52 strcpy(ifr->ifr_name, ifname);
53 return ioctl(fd, cmd,ifr);
56 static unsigned long ip_cnv (const char *str)
58 const char *start_str = str;
63 memset (tb,-1,sizeof(tb));
64 while (*str != '\0' && no < 4){
69 while (isdigit(*str)) str++;
80 ret = (tb[0] << 24) | (tb[1]<<16) | (tb[2] << 8) | tb[3];
81 if (no != 4 || *str != '\0'){
82 fprintf (stderr,"Invalid IP number or netmask: %s\n",start_str);
90 Fetch the IP number of an interface from the kernel.
91 Assume the device is already available in the kernel
92 Return -1 if any error.
101 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
108 if (addrstr != NULL && addrstr[0] != '\0'){
109 printf ("ADDR=%s\n",addrstr);
110 solved.addr = ip_cnv (addrstr);
111 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFADDR, &ifr) >= 0){
112 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
113 unsigned long addr = ntohl(sin->sin_addr.s_addr);
114 printf ("ADDR=%lu.%lu.%lu.%lu\n"
123 perror("ioctl(SIOCGIFADDR)");
126 if (maskstr != NULL && maskstr[0] != '\0'){
127 printf ("NETMASK=%s\n",maskstr);
128 solved.mask = ip_cnv (maskstr);
129 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFNETMASK, &ifr) >= 0){
130 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
131 unsigned long addr = ntohl(sin->sin_addr.s_addr);
132 printf ("NETMASK=%lu.%lu.%lu.%lu\n"
141 perror("ioctl(SIOCGIFNETMASK)");
144 if (bcaststr != NULL && bcaststr[0] != '\0'){
145 printf ("BCAST=%s\n",bcaststr);
146 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFBRDADDR, &ifr) >= 0){
147 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
148 unsigned long addr = ntohl(sin->sin_addr.s_addr);
149 printf ("BCAST=%lu.%lu.%lu.%lu\n"
155 }else if (solved.addr!=0 && solved.mask!=0) {
156 // Can't get it from the kernel, compute it from the IP
158 unsigned long addr = (solved.addr & solved.mask)
160 printf ("BCAST=%lu.%lu.%lu.%lu\n"
173 int main (int argc, char *argv[])
179 const char *addrstr = argc >= 3 ? argv[2] : NULL;
180 const char *maskstr = argc >= 4 ? argv[3] : NULL;
181 const char *bcaststr = argc >= 5 ? argv[4] : NULL;
182 ret = ifconfig_print (argv[1],addrstr,maskstr,bcaststr);