From fe3e42bdcbaa3bcb336227b8d2a7aad73c636047 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 18 Aug 2014 19:19:45 +0200 Subject: Add osmo_get_macaddr() function to obtain etherent mac address This used to be private in osmo-bts/common/abis.c, where it really didn't belong. --- include/osmocom/core/macaddr.h | 1 + src/macaddr.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/osmocom/core/macaddr.h b/include/osmocom/core/macaddr.h index 517977bb..3a4e67ff 100644 --- a/include/osmocom/core/macaddr.h +++ b/include/osmocom/core/macaddr.h @@ -2,5 +2,6 @@ #define _OSMO_MACADDR_H int osmo_macaddr_parse(uint8_t *out, const char *in); +int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name); #endif diff --git a/src/macaddr.c b/src/macaddr.c index 1181dfe5..8b78cb6f 100644 --- a/src/macaddr.c +++ b/src/macaddr.c @@ -23,3 +23,29 @@ int osmo_macaddr_parse(uint8_t *out, const char *in) return 0; } + +#include +#include +#include + +int osmo_get_macaddr(uint8_t *mac_out, const char *dev_name) +{ + int fd, rc; + struct ifreq ifr; + + fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + if (fd < 0) + return fd; + + memset(&ifr, 0, sizeof(ifr)); + memcpy(&ifr.ifr_name, dev_name, sizeof(ifr.ifr_name)); + rc = ioctl(fd, SIOCGIFHWADDR, &ifr); + close(fd); + + if (rc < 0) + return rc; + + memcpy(mac_out, ifr.ifr_hwaddr.sa_data, 6); + + return 0; +} -- cgit v1.2.3