* Author: S. Piccardi Sep. 2006
*
*****************************************************************************/
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdlib.h> /* C standard library */
-#include <stdio.h> /* I/O standard library */
-#include <unistd.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/ioctl.h>
-#include <net/if.h>
-#include <arpa/inet.h>
+#include <sys/types.h> /* primitive system data types */
+#include <sys/stat.h> /* file characteristics constants and functions */
+#include <stdlib.h> /* C standard library */
+#include <stdio.h> /* standard I/O library */
+#include <unistd.h> /* unix standard library */
+#include <sys/socket.h> /* socket constants, types and functions */
+#include <arpa/inet.h> /* IP addresses conversion utilities */
+#include <sys/ioctl.h> /* ioctl syscall and constants */
+#include <net/if.h> /* network interfaces constants and types */
/*
* Program iflist
printf("Wrong number of arguments %d\n", argc - optind);
usage();
}
- iflist.ifc_len = sizeof(buffer);
- iflist.ifc_buf = buffer;
-
+ /* create a socket for the operation */
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket creation error");
return 1;
}
+ /* init values for the ifcon structure and do SIOCGIFCONF */
+ iflist.ifc_len = sizeof(buffer);
+ iflist.ifc_buf = buffer;
ret = ioctl(sock, SIOCGIFCONF, &iflist);
if (ret < 0) {
perror("ioctl failed");
return 1;
}
+ /* check that we have all data */
if (iflist.ifc_len == sizeof(buffer)) {
printf("Probable overflow, too many interfaces, cannot read\n");
return 1;
num = iflist.ifc_len/sizeof(struct ifreq);
printf("Found %i interfaces \n", num);
}
+ /* loop on interface to write data */
for (i=0; i < num; i++) {
address = (struct sockaddr_in *) &iflist.ifc_req[i].ifr_addr;
printf("Interface %s, address %s\n", iflist.ifc_req[i].ifr_name,