Chinaunix首页 | 论坛 | 博客
  • 博客访问: 705839
  • 博文数量: 90
  • 博客积分: 3225
  • 博客等级: 少校
  • 技术积分: 1200
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-20 11:04
个人简介

菩提本无树,明镜变非台 本来无一物,何处惹尘埃

文章分类

全部博文(90)

文章存档

2015年(1)

2014年(12)

2013年(15)

2012年(31)

2011年(8)

2010年(23)

分类: LINUX

2012-04-13 11:12:58

该代码实现接收所有以太网上的数据报。
  1.     #include <stdio.h>
  2.     #include <string.h>
  3.     #include <sys/types.h>
  4.     #include <sys/socket.h>
  5.     #include <netpacket/packet.h>
  6.     #include <sys/ioctl.h>
  7.     #include <net/if.h>
  8.     #define LEN 2048
  9.     int main(int argc, char **argv)
  10.     {
  11.         int sockfd;
  12.         int len;
  13.         int optval;
  14.         int read_size;
  15.         short type;
  16.         char *p;
  17.         char buf[LEN];
  18.         struct ifreq ifr;
  19.         struct sockaddr_ll sll;
  20.         struct packet_mreq mr;
  21.         
  22.         memset(buf, 0, LEN);
  23.         memset(&sll, 0, sizeof(sll));
  24.         memset(&ifr, 0, sizeof(ifr));
  25.         memset(&mr, 0, sizeof(mr));
  26.         sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  27.         if( sockfd < 0 )
  28.         {
  29.             perror("Create socket failed\n");
  30.             return -1;
  31.         }
  32.         
  33.         strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name)-1);
  34.         if( ioctl(sockfd, SIOCGIFINDEX, &ifr) < 0 )
  35.         {
  36.             perror("Ioctl SIOCGIFINDEX failed\n");
  37.             close(sockfd);
  38.             return -1;
  39.         }
  40.         sll.sll_family = AF_PACKET;
  41.         sll.sll_ifindex = ifr.ifr_ifindex;
  42.         sll.sll_protocol = htons(ETH_P_ALL);
  43.         if( (ifr.ifr_flags|IFF_UP|IFF_BROADCAST|IFF_RUNNING) != ifr.ifr_flags){
  44.             ifr.ifr_flags |= IFF_UP|IFF_BROADCAST|IFF_RUNNING;
  45.             if( ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
  46.             {
  47.                 perror("Ioctl SIOCSIFFLAGS failed\n");
  48.                 close(sockfd);
  49.                 return -1;
  50.             }
  51.         }
  52.         
  53.         if( bind(sockfd, (struct sockaddr *)&sll, sizeof(sll)) < 0)
  54.         {
  55.             perror("Bind ETH_P_ALL failed\n");
  56.             close(sockfd);
  57.             return -1;
  58.         }
  59.         if( ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0 )
  60.         {
  61.             perror("Ioctl SIOCGIFHWADDR failed\n");
  62.             close(sockfd);
  63.             return -1;
  64.         }
  65.         if( ifr.ifr_hwaddr.sa_family == 1 )
  66.         {
  67.             printf("ARP linktype is set to Ethernet\n");
  68.         }
  69.         else
  70.         {
  71.             printf("Unsupported hardware link type %4d\n",ifr.ifr_hwaddr.sa_family);
  72.             close(sockfd);
  73.             return -1;
  74.         }
  75.         
  76.         mr.mr_ifindex = sll.sll_ifindex;
  77.         mr.mr_type = PACKET_MR_PROMISC;
  78.         if( setsockopt(sockfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0){
  79.             perror("setsockopt failed\n");
  80.             close(sockfd);
  81.             return -1;
  82.         }
  83.         len = 1;
  84.         read_size = LEN;
  85.         p = buf;
  86.         len = read(sockfd, p, read_size);
  87.         if( len < 0 )
  88.         {
  89.             perror("Read failed\n");
  90.         }
  91.         close(sockfd);
  92.         return 0;
  93.     }

阅读(1459) | 评论(0) | 转发(0) |
0

上一篇:延迟操作

下一篇:定时器(timer)

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