aboutsummaryrefslogtreecommitdiffstats
path: root/gethwaddr.c
blob: ceac2cdfbf0d5f31957f26896e340abfecea1005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>    
#include <sys/socket.h>
#include <net/if.h>
#include <string.h>

int main(int argc, char *argv[]) {
	int s;
	struct ifreq ifr;

	s = socket(PF_INET, SOCK_DGRAM, 0);
	memset(&ifr, 0x00, sizeof(ifr));
	strcpy(ifr.ifr_name, "eth0");
	ioctl(s, SIOCGIFHWADDR, &ifr);
	close(s);

	unsigned char mac_address[6];
	sprintf(mac_address, "%.2X%.2X%.2X%.2X%.2X%.2X",
			(unsigned char) ifr.ifr_hwaddr.sa_data[0],
			(unsigned char) ifr.ifr_hwaddr.sa_data[1],
			(unsigned char) ifr.ifr_hwaddr.sa_data[2],
			(unsigned char) ifr.ifr_hwaddr.sa_data[3],
			(unsigned char) ifr.ifr_hwaddr.sa_data[4],
			(unsigned char) ifr.ifr_hwaddr.sa_data[5]);
	printf("%s", mac_address);

}