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"
122 if (maskstr != NULL && maskstr[0] != '\0'){
123 printf ("NETMASK=%s\n",maskstr);
124 solved.mask = ip_cnv (maskstr);
125 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFNETMASK, &ifr) >= 0){
126 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
127 unsigned long addr = ntohl(sin->sin_addr.s_addr);
128 printf ("NETMASK=%lu.%lu.%lu.%lu\n"
136 if (bcaststr != NULL && bcaststr[0] != '\0'){
137 printf ("BCAST=%s\n",bcaststr);
138 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFBRDADDR, &ifr) >= 0){
139 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
140 unsigned long addr = ntohl(sin->sin_addr.s_addr);
141 printf ("BCAST=%lu.%lu.%lu.%lu\n"
148 // Can't get it from the kernel, compute it from the IP
150 unsigned long addr = (solved.addr & solved.mask)
152 printf ("BCAST=%lu.%lu.%lu.%lu\n"
165 int main (int argc, char *argv[])
171 const char *addrstr = argc >= 3 ? argv[2] : NULL;
172 const char *maskstr = argc >= 4 ? argv[3] : NULL;
173 const char *bcaststr = argc >= 5 ? argv[4] : NULL;
174 ret = ifconfig_print (argv[1],addrstr,maskstr,bcaststr);