1. in userspace use ioctl
/*************some error in code***********************/
static int __init getmac_init()
{
int sock_id;
struct ifreq ifr;
char *macstr = "";
unsigned char *mac = "";
memset(&ifr,0,sizeof(struct ifreq));
sock_id = socket(PF_INET, SOCK_STREAM, 0);
if(sock_id == -1){
return -1;
}
sprintf(ifr.ifr_name,"eth0");
if(ioctl(sock_id, SIOCGIFHWADDR, &ifr) == 0){
memcpy(mac,ifr.ifr_hwaddr.sa_data,6);
sprintf(macstr,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
}
printk("the mac address is:/n %s /n",macstr);
sock_release(sock_id);
return 0;
}
/************************************/
2.in kernel space use struct net_device
/****************/
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
#define DEV_GET_BY_NAME(name) dev_get_by_name(name);
#else
#define DEV_GET_BY_NAME(name) dev_get_by_name(&init_net, name);
#endif
struct net_device *dev = NULL;
unsigned char *name = "eth0"
dev = DEV_GET_BY_NAME(name);
pirntk("HW address %02X:%02X:%02X:%02X:%02X:%02X/n",
dev->dev_addr[0],dev->dev_addr[1],dev->dev_addr[2],
dev->dev_addr[3],dev->dev_addr[4],dev->dev_addr[5])
/***************/