From efe0100189a85fabddeb1e0c61dc845d983b7893 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Thu, 21 Aug 2014 14:14:38 +0200 Subject: macaddr: Add some code for FreeBSD (it should work on the others too) There doesn't seem to be a way to share this code with Linux as it doesn't have the sockaddr_dl concept inside the getifaddrs. I manually verified this on a FreeBSD10 box and hex decoding gave me the correct mac address and rc was 0. --- src/macaddr.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/macaddr.c') diff --git a/src/macaddr.c b/src/macaddr.c index 392ee13f..f6b1ebb3 100644 --- a/src/macaddr.c +++ b/src/macaddr.c @@ -25,6 +25,46 @@ int osmo_macaddr_parse(uint8_t *out, const char *in) return 0; } +#if defined(__FreeBSD__) +#include +#include +#include +#include +#include + + +int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name) +{ + int rc = -1; + struct ifaddrs *ifa, *ifaddr; + + if (getifaddrs(&ifaddr) != 0) + return -1; + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + struct sockaddr_dl *sdl; + + sdl = (struct sockaddr_dl *) ifa->ifa_addr; + if (!sdl) + continue; + if (sdl->sdl_family != AF_LINK) + continue; + if (sdl->sdl_type != IFT_ETHER) + continue; + if (strcmp(ifa->ifa_name, dev_name) != 0) + continue; + + memcpy(mac_out, LLADDR(sdl), 6); + rc = 0; + break; + } + + freeifaddrs(ifaddr); + return 0; +} + +#else + #include #include #include @@ -50,3 +90,4 @@ int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name) return 0; } +#endif -- cgit v1.2.3