]> gitweb.fperrin.net Git - iftop.git/blobdiff - addrs_ioctl.c
Import iftop-1.0pre4
[iftop.git] / addrs_ioctl.c
index c68c50fafdbb50493ef3eaada5cd51a09eca060d..870c83b2b7e3006252398844a023f1d8f9f3c7cd 100644 (file)
@@ -19,7 +19,7 @@
 #include <netinet/in.h>
 
 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
-      || ( defined __GNUC__ && ! defined __linux__ )
+      || ( defined __GLIBC__ && ! defined __linux__ )
 #include <sys/param.h>
 #include <sys/sysctl.h>
 #include <net/if_dl.h>
@@ -81,7 +81,7 @@ get_addrs_ioctl(char *interface, char if_hw_addr[], struct in_addr *if_ip_addr,
   }
 #else
 #if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
-      || ( defined __GNUC__ && ! defined __linux__ )
+      || ( defined __GLIBC__ && ! defined __linux__ )
   {
     int sysctlparam[6] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
     size_t needed = 0;
@@ -90,28 +90,23 @@ get_addrs_ioctl(char *interface, char if_hw_addr[], struct in_addr *if_ip_addr,
     sysctlparam[5] = if_nametoindex(interface);
     if (sysctlparam[5] == 0) {
       fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
-      goto ENDHWADDR;
     }
-    if (sysctl(sysctlparam, 6, NULL, &needed, NULL, 0) < 0) {
+    else if (sysctl(sysctlparam, 6, NULL, &needed, NULL, 0) < 0) {
       fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
-      goto ENDHWADDR;
     }
-    if ((buf = malloc(needed)) == NULL) {
+    else if ((buf = malloc(needed)) == NULL) {
       fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
-      goto ENDHWADDR;
     }
-    if (sysctl(sysctlparam, 6, buf, &needed, NULL, 0) < 0) {
+    else if (sysctl(sysctlparam, 6, buf, &needed, NULL, 0) < 0) {
       fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
       free(buf);
-      goto ENDHWADDR;
     }
-    msghdr = (struct if_msghdr *) buf;
-    memcpy(if_hw_addr, LLADDR((struct sockaddr_dl *)(buf + sizeof(struct if_msghdr) - sizeof(struct if_data) + sizeof(struct if_data))), 6);
-    free(buf);
-    got_hw_addr = 1;
-
-  ENDHWADDR:
-    1; /* compiler whines if there is a label at the end of a block...*/
+    else {
+      msghdr = (struct if_msghdr *) buf;
+      memcpy(if_hw_addr, LLADDR((struct sockaddr_dl *)(buf + sizeof(struct if_msghdr) - sizeof(struct if_data) + sizeof(struct if_data))), 6);
+      free(buf);
+      got_hw_addr = 1;
+    }
   }
 #else
   fprintf(stderr, "Cannot obtain hardware address on this platform\n");
@@ -132,6 +127,9 @@ get_addrs_ioctl(char *interface, char if_hw_addr[], struct in_addr *if_ip_addr,
         if (strcmp(ifa->ifa_name, interface))
            continue; /* Not our interface. */
 
+        if (ifa->ifa_addr == NULL)
+           continue; /* Skip NULL interface address. */
+
         if ( (ifa->ifa_addr->sa_family != AF_INET)
               && (ifa->ifa_addr->sa_family != AF_INET6) )
            continue; /* AF_PACKET is beyond our scope. */