#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int get_dev_mac(char *dev, unsigned char mac[6])
{
	int i;
	int status;
	int sock;
	struct ifreq ifr;

	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1) {
		perror("socket() ERROR");
		return -1;
	}

	strcpy(ifr.ifr_name, dev);

	status = ioctl(sock, SIOCGIFHWADDR, (char *)&ifr);
	if (status == -1) {
		perror("ioctl(SIOCGIFHWADDR) ERROR");
		return -1;
	}

	memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
	close(sock);
	return 0;
}

void show_mac(unsigned char mac[6])
{
	int i;
	for (i = 0; i < 6; i++) {
		if (i > 0)
			printf(":");
		printf("%02X", mac[i]);
	}
	printf("\n");
}

int main(void)
{
	unsigned char eth0_mac[6];
	unsigned char eth1_mac[6];

	get_dev_mac("eth0", eth0_mac);
	get_dev_mac("eth1", eth1_mac);

	show_mac(eth0_mac);
	show_mac(eth1_mac);

	return 0;
}