mirror of https://gerrit.osmocom.org/libosmocore
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:
parent
40d56f96b9
commit
fe3e42bdcb
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue