歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Unix知識 >> 關於Unix

獲取網卡硬件地址問題


int main()
{
int fd;
struct ifreq ifr;
struct sockaddr_in *sin;
char waddr[14];

fd = socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
if (fd < 0)
{
printf("socket is error ,fd:%d\n",fd); //recevie all ip protocal
return -1;
}

memset(&ifr,0,sizeof(struct ifreq));
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name,"eth0");

if(ioctl(fd,SIOCGIFADDR,&ifr)!=0) //get wan ip address
{
printf("ioctl is error \n");
return -1;
}
sin = (struct sockaddr_in *) &ifr.ifr_addr;
printf("%s (our ip) = %s\n", ifr.ifr_name, inet_ntoa(sin->sin_addr));

ioctl(fd, SIOCGIFINDEX, &ifr); //get wan index
printf("adapter index %d\n", ifr.ifr_ifindex);

if(ioctl(fd, SIOCGIFHWADDR, &ifr)!=0) //get hardware address
{
printf("get hardware is faile\n");
return -1;
}
memcpy(waddr, ifr.ifr_hwaddr.sa_data, 6);
printf("adapter hardware address %s:len:%d\n",ifr.ifr_hwaddr.sa_data,strlen(waddr));
return 0;
}
這個程序我在打印waddr這個值的時候為什麼是空了,長度也是0,難道沒有取道網卡的地址?前面的都能正常顯示
Copyright © Linux教程網 All Rights Reserved