分类:
2010-11-16 22:24:17
/* function to find the local ethernet MAC address */
int GetLocalMacAddr(const char *ifname, unsigned char *mac)
{
struct ifreq ifr;
int fd;
int rv;// return value - error value from df or ioctl call
/* determine the local MAC address */
strcpy(ifr.ifr_name, ifname);
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0)
{
rv = fd;
}
else
{
rv = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (rv >= 0) /* worked okay */
{
memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
}
}
return rv;
}