Chinaunix首页 | 论坛 | 博客
  • 博客访问: 589044
  • 博文数量: 92
  • 博客积分: 5026
  • 博客等级: 大校
  • 技术积分: 1321
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-28 11:04
文章分类

全部博文(92)

文章存档

2011年(9)

2010年(17)

2009年(12)

2008年(54)

我的朋友

分类: LINUX

2008-07-25 16:01:36

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
阅读(1490) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~