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> /* 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 */
39 * Use ioctl to get a list of network interfaces and address
42 /* Help printing routine */
45 int main(int argc, char *argv[])
48 * Variables definition
50 int i, num, ret, sock;
53 struct sockaddr_in * address;
55 * Input section: decode command line parameters
58 opterr = 0; /* don't want writing to stderr */
59 while ( (i = getopt(argc, argv, "h")) != -1) {
64 case 'h': /* help option */
65 printf("Wrong -h option use\n");
69 case '?': /* unrecognized options */
70 printf("Unrecognized options -%c\n",optopt);
72 default: /* should not reached */
76 /* ***********************************************************
78 * Options processing completed
82 * ***********************************************************/
83 if ((argc - optind) != 0) {
84 printf("Wrong number of arguments %d\n", argc - optind);
87 /* create a socket for the operation */
88 sock = socket(PF_INET, SOCK_STREAM, 0);
90 perror("Socket creation error");
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);
98 perror("ioctl failed");
101 /* check that we have all data */
102 if (iflist.ifc_len == sizeof(buffer)) {
103 printf("Probable overflow, too many interfaces, cannot read\n");
106 num = iflist.ifc_len/sizeof(struct ifreq);
107 printf("Found %i interfaces \n", num);
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));
120 * routine to print usage info and exit
123 printf("Program iflist: list active network interface with address \n");
125 printf(" iflist [-h] \n");
126 printf(" -h print this help\n");