This commit was manufactured by cvs2svn to create branch
[util-vserver.git] / src / listdevip.c
1 // $Id: listdevip.c,v 1.3 2004/02/20 16:59:40 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 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27 #include "compat.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <netinet/in.h>
36 #include <net/if.h>
37
38
39 static int ifconfig_ioctl(
40         int fd,
41         const char *ifname,
42         int cmd,
43         struct ifreq *ifr)
44 {
45         strcpy(ifr->ifr_name, ifname);
46         return ioctl(fd, cmd,ifr);
47 }
48
49
50 static int devlist_read2_2()
51 {
52         int ret = -1;
53         int skfd = socket (AF_INET,SOCK_DGRAM,0);
54         if (skfd < 0) {
55                 perror ("listdevip: socket()");
56         }else{
57                 struct ifconf ifc;
58                 int numreqs = 30;
59                 ifc.ifc_buf = NULL;
60                 ret = 0;
61                 while (1) {
62                         ifc.ifc_len = sizeof(struct ifreq) * numreqs;
63                         ifc.ifc_buf = (char*)realloc(ifc.ifc_buf, ifc.ifc_len);
64
65                         if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
66                                 perror("listdevip: SIOCGIFCONF");
67                                 ret = -1;
68                                 break;
69                         }
70                         if (ifc.ifc_len == (int)sizeof(struct ifreq) * numreqs) {
71                                 /* assume it overflowed and try again */
72                                 numreqs += 10;
73                                 continue;
74                         }
75                         break;
76                 }
77                 if (ret == 0){
78                         struct ifreq *ifr = ifc.ifc_req;
79                         int             n;
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;
84                                 struct ifreq ifmask;
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);
88                                 }
89
90                                 printf ("%lu.%lu.%lu.%lu/%lu.%lu.%lu.%lu\n"
91                                         ,(addr>>24)&0xff
92                                         ,(addr>>16)&0xff
93                                         ,(addr>>8)&0xff
94                                         ,addr&0xff
95                                         ,(mask>>24)&0xff
96                                         ,(mask>>16)&0xff
97                                         ,(mask>>8)&0xff
98                                         ,mask&0xff);
99                                 ifr++;
100                         }
101                 }
102                 free(ifc.ifc_buf);
103         }
104         return ret;
105 }
106
107 int main (int UNUSED argc, char UNUSED *argv[])
108 {
109         devlist_read2_2();
110         return 0;
111 }
112