Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5699954
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2008-08-19 11:53:43

网卡混杂模式的设置可以通过下面的命令来进行设置:
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:1e:4f:e7:81:16  
          inet addr:10.0.16.111  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::21e:4fff:fee7:8116/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1678 errors:0 dropped:0 overruns:0 frame:0
          TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:164865 (161.0 KB)  TX bytes:8362 (8.1 KB)
          Base address:0xecc0 Memory:fe9e0000-fea00000

root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0 promisc
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:1e:4f:e7:81:16  
          inet addr:10.0.16.111  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::21e:4fff:fee7:8116/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:1751 errors:0 dropped:0 overruns:0 frame:0
          TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:169849 (165.8 KB)  TX bytes:8362 (8.1 KB)
          Base address:0xecc0 Memory:fe9e0000-fea00000

root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0 -promisc
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:1e:4f:e7:81:16  
          inet addr:10.0.16.111  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::21e:4fff:fee7:8116/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1815 errors:0 dropped:0 overruns:0 frame:0
          TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:176114 (171.9 KB)  TX bytes:8458 (8.2 KB)
          Base address:0xecc0 Memory:fe9e0000-fea00000

程序可以通过IOCTL来实现:
int Set_Promisc(char *interface, int sock ) {
       struct ifreq ifr;
       strncpy(ifr.ifr_name, interface,strnlen(interface)+1);
       if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {
             /*Could not retrieve flags for the interface*/
               perror("Could not retrive flags for the interface");
               exit(0);
       }
       printf("The interface is ::: %s\n", interface);
       perror("Retrieved flags from interface successfully");

     /*now that the flags have been retrieved*/
     /* set the flags to PROMISC */
       ifr.ifr_flags |= IFF_PROMISC;
       if (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1 ) {
             /*Could not set the flags on the interface */
               perror("Could not set the PROMISC flag:");
               exit(0);
       }
       printf("Setting interface ::: %s ::: to promisc", interface);

       return(0);
 }

分析ifconfig promisc的实现:

调用 ioctl(skfd, SIOCSIFFLAGS, &ifr)
net/core/dev.c     net/core/dev_mcast.c
int dev_ioctl(unsigned int cmd, void __user *arg)
int dev_change_flags(struct net_device *dev, unsigned flags)
void dev_set_promiscuity(struct net_device *dev, int inc)
void dev_mc_upload(struct net_device *dev)
dev->set_multicast_list(dev)
调驱动的 void smc_set_multicast_list(struct net_device *dev)并操作硬件。

参考:
http://www.cnitblog.com/darkstax/articles/18219.html

阅读(2047) | 评论(0) | 转发(1) |
0

上一篇:共享内存---shmget shmat shmdt

下一篇:lock指令

给主人留下些什么吧!~~