This commit was generated by cvs2svn to compensate for changes in r120,
[util-vserver.git] / src / listdevip.c
1 // $Id: listdevip.c,v 1.1 2003/09/29 22:01:57 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on listdevip.cc by Jacques Gelinas
5 //  
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)
9 // any later version.
10 //  
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.
15 //  
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.
19
20 /*
21         Print the list of all network (IP) devices. Print the IP
22         in fact, including all aliases.
23 */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <netinet/in.h>
31 #include <net/if.h>
32
33
34 static int ifconfig_ioctl(
35         int fd,
36         const char *ifname,
37         int cmd,
38         struct ifreq *ifr)
39 {
40         strcpy(ifr->ifr_name, ifname);
41         return ioctl(fd, cmd,ifr);
42 }
43
44
45 static int devlist_read2_2()
46 {
47         int ret = -1;
48         int skfd = socket (AF_INET,SOCK_DGRAM,0);
49         if (skfd < 0) {
50                 perror ("socket");
51         }else{
52                 struct ifconf ifc;
53                 int numreqs = 30;
54                 ifc.ifc_buf = NULL;
55                 ret = 0;
56                 while (1) {
57                         ifc.ifc_len = sizeof(struct ifreq) * numreqs;
58                         ifc.ifc_buf = (char*)realloc(ifc.ifc_buf, ifc.ifc_len);
59
60                         if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
61                                 perror("SIOCGIFCONF");
62                                 ret = -1;
63                                 break;
64                         }
65                         if (ifc.ifc_len == (int)sizeof(struct ifreq) * numreqs) {
66                                 /* assume it overflowed and try again */
67                                 numreqs += 10;
68                                 continue;
69                         }
70                         break;
71                 }
72                 if (ret == 0){
73                         struct ifreq *ifr = ifc.ifc_req;
74                         int             n;
75                         for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
76                                 struct sockaddr_in *sin = (struct sockaddr_in*)&ifr->ifr_addr;
77                                 unsigned long addr = ntohl(sin->sin_addr.s_addr);
78                                 unsigned long mask = 0xffffff00;
79                                 struct ifreq ifmask;
80                                 if (ifconfig_ioctl(skfd,ifr->ifr_name,SIOCGIFNETMASK, &ifmask) >= 0){
81                                         struct sockaddr_in *sin = (struct sockaddr_in*)&ifmask.ifr_addr;
82                                         mask = ntohl(sin->sin_addr.s_addr);
83                                 }
84
85                                 printf ("%lu.%lu.%lu.%lu/%lu.%lu.%lu.%lu\n"
86                                         ,(addr>>24)&0xff
87                                         ,(addr>>16)&0xff
88                                         ,(addr>>8)&0xff
89                                         ,addr&0xff
90                                         ,(mask>>24)&0xff
91                                         ,(mask>>16)&0xff
92                                         ,(mask>>8)&0xff
93                                         ,mask&0xff);
94                                 ifr++;
95                         }
96                 }
97                 free(ifc.ifc_buf);
98         }
99         return ret;
100 }
101
102 int main (int argc, char *argv[])
103 {
104         devlist_read2_2();
105         return 0;
106 }
107