summaryrefslogtreecommitdiffstats
path: root/src/macaddr.c
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>2014-08-21 14:14:38 +0200
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2014-08-21 14:19:37 +0200
commitefe0100189a85fabddeb1e0c61dc845d983b7893 (patch)
tree64f976b1138a74ce4988c2ffbc3890e921b980e1 /src/macaddr.c
parentf196a02a65a71dc8687d571c94af75047684348f (diff)
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.
Diffstat (limited to 'src/macaddr.c')
-rw-r--r--src/macaddr.c41
1 files changed, 41 insertions, 0 deletions
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 <sys/socket.h>
+#include <sys/types.h>
+#include <ifaddrs.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+
+
+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 <sys/ioctl.h>
#include <net/if.h>
#include <netinet/ip.h>
@@ -50,3 +90,4 @@ int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name)
return 0;
}
+#endif