diff options
Diffstat (limited to 'src/macaddr.c')
-rw-r--r-- | src/macaddr.c | 26 |
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; +} |