1 // $Id: ifspec.c 965 2004-02-20 16:59:40Z ensc $
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
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <netinet/in.h>
43 fprintf (stderr,"ifspec version %s\n",VERSION);
45 ,"ifspec network-device [ ipaddr netmask broadcast ]\n"
46 "prints device specification in a shell usable way\n");
50 static int ifconfig_ioctl(
56 strcpy(ifr->ifr_name, ifname);
57 return ioctl(fd, cmd,ifr);
60 static unsigned long ip_cnv (const char *str)
62 const char *start_str = str;
67 memset (tb,-1,sizeof(tb));
68 while (*str != '\0' && no < 4){
73 while (isdigit(*str)) str++;
84 ret = (tb[0] << 24) | (tb[1]<<16) | (tb[2] << 8) | tb[3];
85 if (no != 4 || *str != '\0'){
86 fprintf (stderr,"Invalid IP number or netmask: %s\n",start_str);
94 Fetch the IP number of an interface from the kernel.
95 Assume the device is already available in the kernel
96 Return -1 if any error.
102 const char *bcaststr)
105 int skfd = socket(AF_INET, SOCK_DGRAM, 0);
112 if (addrstr != NULL && addrstr[0] != '\0'){
113 printf ("ADDR=%s\n",addrstr);
114 solved.addr = ip_cnv (addrstr);
115 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFADDR, &ifr) >= 0){
116 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
117 unsigned long addr = ntohl(sin->sin_addr.s_addr);
118 printf ("ADDR=%lu.%lu.%lu.%lu\n"
127 perror("ifspec: ioctl(SIOCGIFADDR)");
130 if (maskstr != NULL && maskstr[0] != '\0'){
131 printf ("NETMASK=%s\n",maskstr);
132 solved.mask = ip_cnv (maskstr);
133 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFNETMASK, &ifr) >= 0){
134 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
135 unsigned long addr = ntohl(sin->sin_addr.s_addr);
136 printf ("NETMASK=%lu.%lu.%lu.%lu\n"
145 perror("ifspec: ioctl(SIOCGIFNETMASK)");
148 if (bcaststr != NULL && bcaststr[0] != '\0'){
149 printf ("BCAST=%s\n",bcaststr);
150 }else if (ifconfig_ioctl(skfd,ifname,SIOCGIFBRDADDR, &ifr) >= 0){
151 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr.ifr_addr;
152 unsigned long addr = ntohl(sin->sin_addr.s_addr);
153 printf ("BCAST=%lu.%lu.%lu.%lu\n"
159 }else if (solved.addr!=0 && solved.mask!=0) {
160 // Can't get it from the kernel, compute it from the IP
162 unsigned long addr = (solved.addr & solved.mask)
164 printf ("BCAST=%lu.%lu.%lu.%lu\n"
177 int main (int argc, char *argv[])
183 const char *addrstr = argc >= 3 ? argv[2] : NULL;
184 const char *maskstr = argc >= 4 ? argv[3] : NULL;
185 const char *bcaststr = argc >= 5 ? argv[4] : NULL;
186 ret = ifconfig_print (argv[1],addrstr,maskstr,bcaststr);