Patch to get hardware address on FreeBSD and OpenBSD from Nicolas Bernard
<n.bernard@worldonline.fr> Here is a patch which uses sysctl to get the hardware address. I tried it on FreeBSD 5.3 and OpenBSD 3.5. Note that I don't know how this code work on an non-ethernet interface (tried on some pseudo-interface and get an all-0 address, but...). I left the code in the get_addrs_ioctl fct despite the fact it uses sysctl because it was simpler. Oh yes, I used gotos, I found them more readable in this case than deeply imbricated ifs.
This commit is contained in:
@@ -18,6 +18,12 @@
|
|||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
#if defined __FreeBSD__ || defined __OpenBSD__
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <net/if_dl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "iftop.h"
|
#include "iftop.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -64,8 +70,42 @@ get_addrs_ioctl(char *interface, char if_hw_addr[], struct in_addr *if_ip_addr)
|
|||||||
memcpy(if_hw_addr, ifr.ifr_hwaddr.sa_data, 6);
|
memcpy(if_hw_addr, ifr.ifr_hwaddr.sa_data, 6);
|
||||||
got_hw_addr = 1;
|
got_hw_addr = 1;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
#if defined __FreeBSD__ || defined __OpenBSD__
|
||||||
|
{
|
||||||
|
int sysctlparam[6] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
|
||||||
|
int needed = 0;
|
||||||
|
char *buf = NULL;
|
||||||
|
struct if_msghdr *msghdr = NULL;
|
||||||
|
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) {
|
||||||
|
fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
|
||||||
|
goto ENDHWADDR;
|
||||||
|
}
|
||||||
|
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) {
|
||||||
|
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
|
#else
|
||||||
fprintf(stderr, "Cannot obtain hardware address on this platform\n");
|
fprintf(stderr, "Cannot obtain hardware address on this platform\n");
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Get the IP address of the interface */
|
/* Get the IP address of the interface */
|
||||||
|
|||||||
Reference in New Issue
Block a user