linux下设置网卡为 混杂模式 的程序!
#include "sys/types.h" #include "netinet/in.h" #include "arpa/inet.h" #include "errno.h" #include "netdb.h" #include "net/if.h" #include "sys/ioctl.h"
extern int errno;
int open_fd() { int f, s; struct ifreq ifr; if ((f = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL))) < 0)return -1;
// if ((f = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)return -1;
strcpy(ifr.ifr_name, "eth0"); if ((s = ioctl(f, SIOCGIFFLAGS, &ifr)) < 0) { close(f); return(-1); } ifr.ifr_flags |= IFF_PROMISC; if ((s = ioctl(f, SIOCSIFFLAGS, &ifr)) < 0) { return(-1); } printf("Setting interface ::: %s ::: to promisc\n\n", ifr.ifr_name); return(f); } int main() { open_fd(); }
|
sock_fd = socket( PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
sock_fd = socket( AF_INET, SOCK_PACKET, htons(ETH_P_ALL)); //已经过时
表示接收所有类型的数据帧 ip arp rarp
阅读(1501) | 评论(0) | 转发(0) |