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.
This commit is contained in:
Harald Welte 2014-08-18 19:19:45 +02:00
parent 40d56f96b9
commit fe3e42bdcb
2 changed files with 27 additions and 0 deletions

View File

@ -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

View File

@ -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;
}