#include <net/if.h>
#include <net/if_arp.h>
static int get_device_mac(u8 *mac, const char *dev_name)
{
int ret = 0, i = 0;
int fd = -1;
struct ifreq ifr;
if (NULL == mac || NULL == dev_name) {
return -1;
}
/* if dev name is too long, just report error. */
if (strlen(dev_name) > IFNAMSIZ) {
return -2;
}
strcpy(ifr.ifr_name, dev_name);
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return -3;
}
if (ioctl(fd, SIOCGIFHWADDR, &ifr, sizeof(ifr)) < 0)
{
ret = -4;
goto end;
}
memcpy(mac, (u8 *)&(ifr.ifr_hwaddr.sa_data[i]), ETH_ALEN);
end:
close(fd);
return ret;
}