Created
June 1, 2014 13:10
-
-
Save ajithbh/7505602f57fc9cd9f45b to your computer and use it in GitHub Desktop.
Get MAC Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int get_mac_addr(char *mac_addr) | |
{ | |
int sockfd; | |
struct ifreq ifr; | |
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) >= 0) { | |
strncpy(ifr.ifr_name, "eth0", IFNAMESIZE); | |
ifr.ifr_addr.sa_family = AF_INET; | |
if (ioctl(sockfd, SIOGIFHWADDR, (char*) &ifr) == 0) { | |
sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X", | |
(unsigned char) ifr.ifr_ifru.ifru_hwaddr.sa_data[0], (unsigned char) ifr.ifr_ifru.ifru_hwaddr.sa_data[1], | |
(unsigned char) ifr.ifr_ifru.ifru_hwaddr.sa_data[2], (unsigned char) ifr.ifr_ifru.ifru_hwaddr.sa_data[3], | |
(unsigned char) ifr.ifr_ifru.ifru_hwaddr.sa_data[4], (unsigned char) ifr.ifr_ifru.ifru_hwaddr.sa_data[5]); | |
return 0; | |
} | |
} | |
/* error */ | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment