1 // $Id: listdevip.c,v 1.3 2004/02/20 16:59:40 ensc Exp $
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on listdevip.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 Print the list of all network (IP) devices. Print the IP
22 in fact, including all aliases.
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <netinet/in.h>
39 static int ifconfig_ioctl(
45 strcpy(ifr->ifr_name, ifname);
46 return ioctl(fd, cmd,ifr);
50 static int devlist_read2_2()
53 int skfd = socket (AF_INET,SOCK_DGRAM,0);
55 perror ("listdevip: socket()");
62 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
63 ifc.ifc_buf = (char*)realloc(ifc.ifc_buf, ifc.ifc_len);
65 if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
66 perror("listdevip: SIOCGIFCONF");
70 if (ifc.ifc_len == (int)sizeof(struct ifreq) * numreqs) {
71 /* assume it overflowed and try again */
78 struct ifreq *ifr = ifc.ifc_req;
80 for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
81 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr->ifr_addr;
82 unsigned long addr = ntohl(sin->sin_addr.s_addr);
83 unsigned long mask = 0xffffff00;
85 if (ifconfig_ioctl(skfd,ifr->ifr_name,SIOCGIFNETMASK, &ifmask) >= 0){
86 struct sockaddr_in *sin = (struct sockaddr_in*)&ifmask.ifr_addr;
87 mask = ntohl(sin->sin_addr.s_addr);
90 printf ("%lu.%lu.%lu.%lu/%lu.%lu.%lu.%lu\n"
107 int main (int UNUSED argc, char UNUSED *argv[])