#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>

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

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

	strcpy(mac_data.ifr_name, dev);
	status = ioctl(sock, SIOCGIFHWADDR, (char *)&mac_data);
	if (status == -1) {
		perror("ioctl(SIOCGIFHWADDR) ERROR");
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < 6; i++)
		mac[i] = (unsigned char)
		    ((mac_data.ifr_hwaddr.sa_data[i]) & 0x00FF);

	return mac;
}

void show_mac(unsigned char *mac)
{
	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];

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

	show_mac(eth0_mac);
	show_mac(eth1_mac);

	return 0;
}