Chinaunix首页 | 论坛 | 博客
  • 博客访问: 647090
  • 博文数量: 156
  • 博客积分: 4833
  • 博客等级: 上校
  • 技术积分: 1554
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-21 19:36
文章分类

全部博文(156)

文章存档

2016年(2)

2013年(1)

2012年(13)

2011年(30)

2010年(46)

2009年(29)

2008年(23)

2007年(12)

分类: LINUX

2009-11-19 16:34:51


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include /* the L2 protocols */

#define BUFFER_MAX 2048





int   vlan_bind(int   fd)   
{   
    struct   sockaddr_ll sll;     
    struct   ifreq   ifstruct;
    struct   ifreq   ifreq;     
    
    memset(  &sll,   0,   sizeof(sll)   );  
    sll.sll_family =   PF_PACKET;  
    sll.sll_protocol =   htons(ETH_P_ALL); 
    
    strcpy(ifstruct.ifr_name,   "eth0.1");  
    ioctl(fd,   SIOCGIFINDEX,   &ifstruct);  
    sll.sll_ifindex   =   ifstruct.ifr_ifindex;
   
   
    ioctl (fd, SIOCGIFHWADDR, &ifstruct); 
    memcpy (sll.sll_addr, ifstruct.ifr_ifru.ifru_hwaddr.sa_data, sizeof(sll.sll_addr));
   
   
    if( bind(fd,  (struct  sockaddr *)&sll, sizeof(sll)) ==  -1  )  
    {  
         printf(  "bind:   ERROR\n"   );  
       return   -1;  
    }  
 
#if 0 
    ioctl (fd, SIOCGIFFLAGS, &ifreq);   
    ifreq.ifr_flags |= (IFF_UP | IFF_BROADCAST | IFF_MULTICAST);
    ifreq.ifr_flags &= ~IFF_ALLMULTI;
    ioctl (fd, SIOCSIFFLAGS, &ifreq);
#endif

    return   0;  


int main(int argc, char *argv[])
{
      
    int sock, n_read, proto;      
    char buffer[BUFFER_MAX];
    char  *ethhead, *iphead, *tcphead, *udphead, *icmphead, *p;
                
   
    if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
    {
        fprintf(stdout, "create socket error\n");
        return -1;
    }
   
    vlan_bind(sock);
  

      
  while(1)
  {
       n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
        /*
        14   6(dest)+6(source)+2(type or length)
        +
        20   ip header
        +
        8   icmp,tcp or udp header
        = 42
        */
        if(n_read < 42)
        {
           fprintf(stdout, "Incomplete header, packet corrupt\n");
           continue;
        }
              
        ethhead = buffer;
        p = ethhead;
        int n = 0XFF;
        printf("MAC: %.2X:%02X:%02X:%02X:%02X:%02X==>"
               "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
        p[6]&n, p[7]&n, p[8]&n, p[9]&n, p[10]&n, p[11]&n,
        p[0]&n, p[1]&n, p[2]&n,p[3]&n, p[4]&n, p[5]&n);

       iphead = ethhead + 14; 
       p = iphead + 12;
       
       printf("IP: %d.%d.%d.%d => %d.%d.%d.%d\n",
       p[0]&0XFF, p[1]&0XFF, p[2]&0XFF, p[3]&0XFF,
       p[4]&0XFF, p[5]&0XFF, p[6]&0XFF, p[7]&0XFF);
       proto = (iphead + 9)[0];
       p = iphead + 20;
       printf("Protocol: ");
       switch(proto)
         {
           case IPPROTO_ICMP: printf("ICMP\n");break;
           case IPPROTO_IGMP: printf("IGMP\n");break;
           case IPPROTO_IPIP: printf("IPIP\n");break;
           case IPPROTO_TCP :
           case IPPROTO_UDP :
           printf("%s,", proto == IPPROTO_TCP ? "TCP": "UDP");
           printf("source port: %u,",(p[0]<<8)&0XFF00 |  p[1]&0XFF);
           printf("dest port: %u\n", (p[2]<<8)&0XFF00 | p[3]&0XFF);
                break;
           case IPPROTO_RAW : printf("RAW\n");break;
           default:printf("Unkown, please query in include/linux/in.h\n");
        }
   }
}

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

上一篇:add protocol zz+modify

下一篇:MTD init

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