[编程实例]linux 设置网卡为混杂模式

来源:互联网 发布:最短路径问题算法 编辑:程序博客网 时间:2024/06/05 03:17
 
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <sys/ioctl.h>
  4. #include <stdlib.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h> 
  7. #include <string.h>
  8. #include <linux/in.h>
  9. #include <linux/if_ether.h>
  10. #include <unistd.h>
  11. #include <net/if.h>
  12. int main(int argc, char **argv) {
  13.   int sock, n;
  14.   struct ifreq ethreq;
  15.   if ( (sock=socket(PF_PACKET, SOCK_RAW, 
  16.                     htons(ETH_P_ALL)))<0) {
  17.     perror("socket");
  18.     exit(1);
  19.   }
  20.  /* Set the network card in promiscuos mode */
  21.   strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ);
  22.   if (ioctl(sock,SIOCGIFFLAGS,&ethreq)==-1) {
  23.     perror("ioctl");
  24.     close(sock);
  25.     exit(1);
  26.   }
  27.   ethreq.ifr_flags|=IFF_PROMISC;
  28.   if (ioctl(sock,SIOCSIFFLAGS,&ethreq)==-1) {
  29.     perror("ioctl");
  30.     close(sock);
  31.     exit(1);
  32.   }
  33.   printf("Success to set eth0 to promiscuos mode.../n");
  34.   return 0;
  35. }
原创粉丝点击