#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>
}
#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;
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");
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. */