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,难道没有取道网卡的地址?前面的都能正常显示
linuxguy 于 2007-06-16 00:44:33发表:
:time:1