summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2014-08-18 19:19:45 +0200
committerHarald Welte <laforge@gnumonks.org>2014-08-18 19:19:45 +0200
commitfe3e42bdcbaa3bcb336227b8d2a7aad73c636047 (patch)
tree2e4f608e819d15cb694eed78be4be4e3b89093ae /src
parent40d56f96b907f8afb717a5d33fd72bee0f6ad1fb (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/macaddr.c26
1 files changed, 26 insertions, 0 deletions
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 <sys/ioctl.h>
+#include <net/if.h>
+#include <netinet/ip.h>
+
+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;
+}