用C实现截获网络数据包(二)

2014-11-24 02:31:59 · 作者: · 浏览: 7
9. perror( "Could not retrive flags for the interface ");
50. exit(0);
51. } 
52. printf( "The interface is ::: %s\n ", interface);  
53. perror( "Retrieved flags from interface successfully ");
54. ifr.ifr_flags |= IFF_PROMISC;  
55. if (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1 ) {  
/*Could not set the flags on the interface */  
56. perror( "Could not set the PROMISC flag: ");
57. exit(0);    
58. }
59. printf( "Setting interface ::: %s ::: to promisc ", interface);
60. return(0);
61. }


  首先 struct ifreq ifr; 定一了一个ifrreg的结构ifr,接下来 strncpy(ifr.ifr_name, interface,strnlen(interface)+1);,就是把我们网络设备的名字填充到ifr结构中,在这里 #define INTERFACE "eth0 " ,让我们再往下看,ioctl(sock, SIOCGIFFLAGS, &ifr),SIOCGIFFLAGS请求表示需要获取接口标志,现在到了第54行,在我们成功的获取接口标志后把他设置成混杂模式,ifr.ifr_flags |= IFF_PROMISC;ioctl (sock, SIOCSIFFLAGS, &ifr)。OK,现在我们所说的第一步已经完成--------把网卡置于混杂模式。