[编程实例]linux 设置网卡为混杂模式
来源:互联网 发布:最短路径问题算法 编辑:程序博客网 时间:2024/06/05 03:17
- #include <stdio.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <string.h>
- #include <linux/in.h>
- #include <linux/if_ether.h>
- #include <unistd.h>
- #include <net/if.h>
- int main(int argc, char **argv) {
- int sock, n;
- struct ifreq ethreq;
- if ( (sock=socket(PF_PACKET, SOCK_RAW,
- htons(ETH_P_ALL)))<0) {
- perror("socket");
- exit(1);
- }
-
- strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ);
- if (ioctl(sock,SIOCGIFFLAGS,ðreq)==-1) {
- perror("ioctl");
- close(sock);
- exit(1);
- }
- ethreq.ifr_flags|=IFF_PROMISC;
- if (ioctl(sock,SIOCSIFFLAGS,ðreq)==-1) {
- perror("ioctl");
- close(sock);
- exit(1);
- }
- printf("Success to set eth0 to promiscuos mode.../n");
- return 0;
- }