Aggiornamenti + alcune sockopt di IP
[gapil.git] / listati / iflist.c
1 int i, num, ret, sock;
2 struct ifconf iflist;
3 char buffer[4096];
4 struct sockaddr_in * address;
5 ...
6 /* create a socket for the operation */
7 sock = socket(PF_INET, SOCK_STREAM, 0);
8 if (sock < 0) {
9     perror("Socket creation error");
10     return 1;
11 }
12 /* init values for the ifcon structure and do SIOCGIFCONF */
13 iflist.ifc_len = sizeof(buffer);
14 iflist.ifc_buf = buffer;
15 ret = ioctl(sock, SIOCGIFCONF, &iflist);
16 if (ret < 0) {
17     perror("ioctl failed");
18     return 1;
19 }
20 /* check that we have all data */
21 if (iflist.ifc_len == sizeof(buffer)) {
22     printf("Probable overflow, too many interfaces, cannot read\n");
23     return 1;
24 } else {
25     num = iflist.ifc_len/sizeof(struct ifreq);
26     printf("Found %i interfaces \n", num);
27 }
28 /* loop on interface to write data */
29 for (i=0; i < num; i++) {
30     address = (struct sockaddr_in *) &iflist.ifc_req[i].ifr_addr;
31     printf("Interface %s, address %s\n", iflist.ifc_req[i].ifr_name, 
32            inet_ntoa(address->sin_addr));
33 }
34 return 0;