/* ... */ /* this file is part of netraf project */ /* Copyright (c) 2005 M.S. */ /* little example of how to obtain device's MAC address. This currently compile only on linux. In BSD systems instead of is something like but doesn't work for me now... */ #include #include #include #include #include #include #include int main(int argc, char *argv[]){ int s; struct ifreq interface; unsigned char mac[6]; if(argc != 2){ printf("Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) exit(EXIT_FAILURE); strcpy(interface.ifr_name, argv[1]); if(ioctl(s, SIOCGIFHWADDR, &interface) == -1){ perror("ioctl(SIOCGIFHWADDR)"); exit(EXIT_FAILURE); } memcpy(mac, interface.ifr_hwaddr.sa_data, 6); printf("%s HWaddr %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", argv[1], mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); exit(EXIT_SUCCESS); }