3 * Copyright (C) 2006 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
21 * File ifist.c: to get a list of network interface and their addresses
23 * Author: S. Piccardi Sep. 2006
25 *****************************************************************************/
26 #include <sys/types.h>
28 #include <stdlib.h> /* C standard library */
29 #include <stdio.h> /* I/O standard library */
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <sys/ioctl.h>
35 #include <arpa/inet.h>
40 * Use ioctl to get a list of network interfaces and address
43 /* Help printing routine */
46 int main(int argc, char *argv[])
49 * Variables definition
51 int i, num, ret, sock;
54 struct sockaddr_in * address;
56 * Input section: decode command line parameters
59 opterr = 0; /* don't want writing to stderr */
60 while ( (i = getopt(argc, argv, "h")) != -1) {
65 case 'h': /* help option */
66 printf("Wrong -h option use\n");
70 case '?': /* unrecognized options */
71 printf("Unrecognized options -%c\n",optopt);
73 default: /* should not reached */
77 /* ***********************************************************
79 * Options processing completed
83 * ***********************************************************/
84 if ((argc - optind) != 0) {
85 printf("Wrong number of arguments %d\n", argc - optind);
88 /* create a socket for the operation */
89 sock = socket(PF_INET, SOCK_STREAM, 0);
91 perror("Socket creation error");
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);
99 perror("ioctl failed");
102 /* check that we have all data */
103 if (iflist.ifc_len == sizeof(buffer)) {
104 printf("Probable overflow, too many interfaces, cannot read\n");
107 num = iflist.ifc_len/sizeof(struct ifreq);
108 printf("Found %i interfaces \n", num);
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));
121 * routine to print usage info and exit
124 printf("Program iflist: list active network interface with address \n");
126 printf(" iflist [-h] \n");
127 printf(" -h print this help\n");