Modifiche di ieri sera.
[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>
27 #include <sys/stat.h>
28 #include <stdlib.h>        /* C standard library */
29 #include <stdio.h>         /* I/O standard library */
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <sys/ioctl.h>
34 #include <net/if.h>
35 #include <arpa/inet.h>
36
37 /*
38  * Program iflist
39  *
40  * Use ioctl to get a list of network interfaces and address
41  */
42
43 /* Help printing routine */
44 void usage(void);
45
46 int main(int argc, char *argv[]) 
47 {
48 /* 
49  * Variables definition
50  */
51     int i, num, ret, sock;
52     struct ifconf iflist;
53     char buffer[4096];
54     struct sockaddr_in * address;
55     /*
56      * Input section: decode command line parameters 
57      * Use getopt function
58      */
59     opterr = 0;  /* don't want writing to stderr */
60     while ( (i = getopt(argc, argv, "h")) != -1) {
61         switch (i) {
62         /* 
63          * Handling options 
64          */ 
65         case 'h':                                            /* help option */
66             printf("Wrong -h option use\n");
67             usage();
68             return -1;
69             break;
70         case '?':                                    /* unrecognized options */
71             printf("Unrecognized options -%c\n",optopt);
72             usage();
73         default:                                       /* should not reached */
74             usage();
75         }
76     }
77     /* ***********************************************************
78      * 
79      *           Options processing completed
80      *
81      *                Main code beginning
82      * 
83      * ***********************************************************/
84     if ((argc - optind) != 0) {
85         printf("Wrong number of arguments %d\n", argc - optind);
86         usage();
87     }
88     /* create a socket for the operation */
89     sock = socket(PF_INET, SOCK_STREAM, 0);
90     if (sock < 0) {
91         perror("Socket creation error");
92         return 1;
93     }
94     /* init values for the ifcon structure and do SIOCGIFCONF */
95     iflist.ifc_len = sizeof(buffer);
96     iflist.ifc_buf = buffer;
97     ret = ioctl(sock, SIOCGIFCONF, &iflist);
98     if (ret < 0) {
99         perror("ioctl failed");
100         return 1;
101     }
102     /* check that we have all data */
103     if (iflist.ifc_len == sizeof(buffer)) {
104         printf("Probable overflow, too many interfaces, cannot read\n");
105         return 1;
106     } else {
107         num = iflist.ifc_len/sizeof(struct ifreq);
108         printf("Found %i interfaces \n", num);
109     }
110     /* loop on interface to write data */
111     for (i=0; i < num; i++) {
112         address = (struct sockaddr_in *) &iflist.ifc_req[i].ifr_addr;
113         printf("Interface %s, address %s\n", iflist.ifc_req[i].ifr_name, 
114                inet_ntoa(address->sin_addr));
115     }
116
117     return 0;
118 }
119
120 /*
121  * routine to print usage info and exit
122  */
123 void usage(void) {
124     printf("Program iflist: list active network interface with address \n");
125     printf("Usage:\n");
126     printf("  iflist [-h] \n");
127     printf("  -h   print this help\n");
128     exit(1);
129 }