Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / iflist.c
1 /* iflist.c
2  * 
3  * Copyright (C) 2006 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * File ifist.c: to get a list of network interface and their addresses
22  *
23  * Author: S. Piccardi Sep. 2006
24  *
25  *****************************************************************************/
26 #include <sys/types.h>   /* primitive system data types */
27 #include <sys/stat.h>    /* file characteristics constants and functions */
28 #include <stdlib.h>      /* C standard library */
29 #include <stdio.h>       /* standard I/O library */
30 #include <unistd.h>      /* unix standard library */
31 #include <sys/socket.h>  /* socket constants, types and functions */
32 #include <arpa/inet.h>   /* IP addresses conversion utilities */
33 #include <sys/ioctl.h>   /* ioctl syscall and constants */
34 #include <net/if.h>      /* network interfaces constants and types */
35
36 /*
37  * Program iflist
38  *
39  * Use ioctl to get a list of network interfaces and address
40  */
41
42 /* Help printing routine */
43 void usage(void);
44
45 int main(int argc, char *argv[]) 
46 {
47 /* 
48  * Variables definition
49  */
50     int i, num, ret, sock;
51     struct ifconf iflist;
52     char buffer[4096];
53     struct sockaddr_in * address;
54     /*
55      * Input section: decode command line parameters 
56      * Use getopt function
57      */
58     opterr = 0;  /* don't want writing to stderr */
59     while ( (i = getopt(argc, argv, "h")) != -1) {
60         switch (i) {
61         /* 
62          * Handling options 
63          */ 
64         case 'h':                                            /* help option */
65             printf("Wrong -h option use\n");
66             usage();
67             return -1;
68             break;
69         case '?':                                    /* unrecognized options */
70             printf("Unrecognized options -%c\n",optopt);
71             usage();
72         default:                                       /* should not reached */
73             usage();
74         }
75     }
76     /* ***********************************************************
77      * 
78      *           Options processing completed
79      *
80      *                Main code beginning
81      * 
82      * ***********************************************************/
83     if ((argc - optind) != 0) {
84         printf("Wrong number of arguments %d\n", argc - optind);
85         usage();
86     }
87     /* create a socket for the operation */
88     sock = socket(PF_INET, SOCK_STREAM, 0);
89     if (sock < 0) {
90         perror("Socket creation error");
91         return 1;
92     }
93     /* init values for the ifcon structure and do SIOCGIFCONF */
94     iflist.ifc_len = sizeof(buffer);
95     iflist.ifc_buf = buffer;
96     ret = ioctl(sock, SIOCGIFCONF, &iflist);
97     if (ret < 0) {
98         perror("ioctl failed");
99         return 1;
100     }
101     /* check that we have all data */
102     if (iflist.ifc_len == sizeof(buffer)) {
103         printf("Probable overflow, too many interfaces, cannot read\n");
104         return 1;
105     } else {
106         num = iflist.ifc_len/sizeof(struct ifreq);
107         printf("Found %i interfaces \n", num);
108     }
109     /* loop on interface to write data */
110     for (i=0; i < num; i++) {
111         address = (struct sockaddr_in *) &iflist.ifc_req[i].ifr_addr;
112         printf("Interface %s, address %s\n", iflist.ifc_req[i].ifr_name, 
113                inet_ntoa(address->sin_addr));
114     }
115
116     return 0;
117 }
118
119 /*
120  * routine to print usage info and exit
121  */
122 void usage(void) {
123     printf("Program iflist: list active network interface with address \n");
124     printf("Usage:\n");
125     printf("  iflist [-h] \n");
126     printf("  -h   print this help\n");
127     exit(1);
128 }