#include
#include
#include
#include
#include
#include
int get_ethx_mac(char *psz_mac, int size, char *psz_ethn, int *ptr_active)
{
//函数输入参数合法性校验
if ((NULL == psz_mac) || size < 18 || NULL == psz_ethn
|| NULL == ptr_active
|| strlen(psz_ethn) >= sizeof(struct ifreq))
{
return -1;
}
int i_fd;
struct ifreq st_ifreq;
unsigned char* hwaddr;
int i;
*ptr_active = 0;
//创建套接字i_fd
if((i_fd = socket(AF_INET,SOCK_STREAM,0)) <= 0)
{
return -2;
}
memset(&st_ifreq, 0x0, sizeof(struct ifreq));
strcpy(st_ifreq.ifr_name,psz_ethn);
if(ioctl(i_fd,SIOCGIFFLAGS,&st_ifreq) < 0)
{
close(i_fd);
return -4;
}
if((st_ifreq.ifr_flags & IFF_UP) && (st_ifreq.ifr_flags & IFF_RUNNING))
{
*ptr_active = 1;
}
if(ioctl(i_fd,SIOCGIFHWADDR,&st_ifreq) < 0)
{
close(i_fd);
return -3;
}
hwaddr = (unsigned char*)st_ifreq.ifr_hwaddr.sa_data;
sprintf(psz_mac,"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
hwaddr[0],hwaddr[1],hwaddr[2],
hwaddr[3],hwaddr[4],hwaddr[5]);
close(i_fd);
return 0;
}
阅读(725) | 评论(0) | 转发(0) |