点滴

C++博客 首页 新随笔 联系 聚合 管理
  7 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks

 

  1 /*
  2  * Create by chage
  3  * Email : chagenh@163.com
  4  * Date  : 2009-11-24
  5  * 转载请注明出处
  6   */

  7
  8 #include  < iostream >
  9 #include  < string >
 10 #include  < errno.h >   
 11 #include  < unistd.h >
 12 #include  < sys / socket.h >
 13 #include  < sys / types.h >   
 14
 15 #include  < netinet / in .h >
 16 #include  < netinet / if_ether.h >
 17 #include  < netinet / ether.h >
 18 #include  < net / ethernet.h >
 19 #include  < netinet / ip.h >
 20 #include  < netinet / udp.h >
 21 #include  < netinet / tcp.h >
 22 #include  < net / if .h >
 23 #include  < sys / ioctl.h >
 24 #include  < stdlib.h >
 25 #include  < signal.h >
 26 #include  < unistd.h >
 27
 28 using   namespace  std;
 29
 30 const   int  MAXBUFLEN  =   4096 ;
 31 const   int  ETH_LEN  =   14 ;
 32 int  sock;
 33
 34 struct  ifreq ethreq_old;
 35
 36 static   void  sig_alrm( int  signo)
 37 {
 38  printf( " test " );
 39  alarm( 1 );
 40 }

 41
 42 static   void  sig_leave( int  signo)
 43 {
 44  ioctl(sock, SIOCGIFFLAGS,  & ethreq_old);
 45  close(sock);
 46  printf( " Bye.\n " );
 47  exit( 0 );
 48 }

 49
 50 static   void  socket_init( char   * device)
 51 {
 52   struct  ifreq ethreq;
 53  
 54   if  (signal(SIGALRM, sig_alrm)  ==  SIG_ERR)
 55   {
 56   exit( 1 );
 57  }

 58
 59   if  (signal(SIGINT, sig_leave)  ==  SIG_ERR)
 60   {
 61   exit( 1 );
 62  }

 63
 64   if  (signal(SIGTERM, sig_leave)  ==  SIG_ERR)
 65   {
 66   exit( 1 );
 67  }

 68
 69   if  ( (sock  =  socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))  ==   - 1  )
 70   {
 71   perror( " socket fail.\n " );
 72   exit( 1 );
 73  }

 74
 75
 76  strncpy(ethreq.ifr_name, device, IFNAMSIZ);
 77
 78   if  ( ioctl(sock, SIOCGIFFLAGS,  & ethreq)  ==   - 1  )
 79   {
 80   perror( " ioctl fail.\n " );
 81   close(sock);
 82   exit( 1 );
 83  }

 84  
 85  ethreq_old  =  ethreq;
 86  ethreq.ifr_flags  |=  IFF_PROMISC;
 87  
 88   if  ( ioctl(sock, SIOCSIFFLAGS,  & ethreq)  ==   - 1  )
 89   {
 90   perror( " ioctl fail.\n " );
 91   close(sock);
 92   exit( 1 );
 93  }

 94  alarm( 1 );
 95
 96 }

 97
 98 int  main( int  argc,  char   ** argv)
 99 {
100  
101   int  n;
102   char  buffer[MAXBUFLEN];
103  unsigned  char   * iphead;
104  unsigned  char   * ethhead;
105
106  socket_init( " eth0 " );
107
108   while  ( 1 )
109   {
110   n  =  recvfrom(sock, buffer,  2048 0 , NULL, NULL);
111
112    if  (n  <   42 )
113    {
114    perror( " recvfrom(): " );
115    printf( " Incomplete packet (errno is %d)\n " , errno);
116    close(sock);
117    exit( 0 );
118   }

119   printf( " n = %d\n " , n);
120   
121   
122   ethhead  =  (unsigned  char * )buffer;
123   printf( " ethnet addr is %02x:%02x:%02x:%02x:%02x:%02x ==> %02x:%02x:%02x:%02x:%02x:%02x\n " ,
124      ethhead[ 0 ], ethhead[ 1 ], ethhead[ 2 ], ethhead[ 3 ], ethhead[ 4 ], ethhead[ 5 ],
125      ethhead[ 6 ], ethhead[ 7 ], ethhead[ 8 ], ethhead[ 9 ], ethhead[ 10 ], ethhead[ 11 ]);
126      
127   iphead  =  (unsigned  char * )(buffer  +  ETH_LEN);  /*  Skip Ethernet header  */
128   
129    if  ( * iphead == 0x45 )
130    {
131    printf( " ip addr is %d.%d.%d.%d:%d ==> %d.%d.%d.%d:%d\n "
132     iphead[ 12 ],iphead[ 13 ], iphead[ 14 ],iphead[ 15 ], (iphead[ 20 ] << 8 ) + iphead[ 21 ],
133     iphead[ 16 ],iphead[ 17 ], iphead[ 18 ],iphead[ 19 ], (iphead[ 22 ] << 8 ) + iphead[ 23 ]);
134
135   }

136  }
// while
137
138  sig_leave( 0 );
139 }

140
141
posted on 2009-11-24 15:17 chage 阅读(689) 评论(0)  编辑 收藏 引用 所属分类: network

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理