Linux网络编程——原始套接字实例:MAC 头部报文分析

来源:互联网 发布:淘宝运营书籍推荐 编辑:程序博客网 时间:2024/05/22 08:22

一、前导

通过《Linux网络编程——原始套接字编程》得知,我们可以通过原始套接字以及 recvfrom( ) 可以获取链路层的数据包,那我们接收的链路层数据包到底长什么样的呢


二、链路层封包格式


三、MAC 头部(有线局域网)


注意:CRC、PAD 在组包时可以忽略


四、链路层数据包的其中一种情况:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. unsigned char msg[1024] = {  
  2.     //--------------组MAC--------14------  
  3.     0xb80x880xe30xe10x100xe6// dst_mac: b8:88:e3:e1:10:e6  
  4.     0xc80x9c, 0xdc, 0xb70x0f, 0x19// src_mac: c8:9c:dc:b7:0f:19  
  5.     0x080x00,                         // 类型:0x0800 IP协议  
  6.     // …… ……  
  7.     // …… ……  
  8. };  


五、接收的链路层数据包,并对其进行简单分析:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <sys/socket.h>  
  5. #include <netinet/in.h>  
  6. #include <arpa/inet.h>  
  7. #include <netinet/ether.h>  
  8.   
  9. int main(int argc,charchar *argv[])  
  10. {  
  11.     int i = 0;  
  12.     unsigned char buf[1024] = "";  
  13.     int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));  
  14.     while(1)  
  15.     {  
  16.         unsigned char src_mac[18] = "";  
  17.         unsigned char dst_mac[18] = "";  
  18.         //获取链路层的数据帧  
  19.         recvfrom(sock_raw_fd, buf, sizeof(buf),0,NULL,NULL);  
  20.         //从buf里提取目的mac、源mac  
  21.         sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);  
  22.         sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);  
  23.         //判断是否为IP数据包  
  24.         if(buf[12]==0x08 && buf[13]==0x00)  
  25.         {     
  26.             printf("______________IP数据报_______________\n");  
  27.             printf("MAC:%s >> %s\n",src_mac,dst_mac);  
  28.         }//判断是否为ARP数据包  
  29.         else if(buf[12]==0x08 && buf[13]==0x06)  
  30.         {  
  31.             printf("______________ARP数据报_______________\n");  
  32.             printf("MAC:%s >> %s\n",src_mac,dst_mac);  
  33.         }//判断是否为RARP数据包  
  34.         else if(buf[12]==0x80 && buf[13]==0x35)  
  35.         {  
  36.             printf("______________RARP数据报_______________\n");  
  37.             printf("MAC:%s>>%s\n",src_mac,dst_mac);  
  38.         }  
  39.     }  
  40.     return 0;  
  41. }  


记得以管理者权限运行程序:



每个报文头部都有一个相应的结构体,通过这些结构体对报文进行相应的组包或拆包会方便很多。


ubuntu 12.04 中描述网络协议结构的文件如下:




六、以太网头部(所需要头文件:#include <net/ethernet.h>):



上面的例子,改为用结构体实现,如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <sys/socket.h>  
  5. #include <netinet/in.h>  
  6. #include <arpa/inet.h>  
  7. #include <netinet/ether.h>  
  8. #include <net/ethernet.h>         // 以太网头部 头文件      
  9. #include <netinet/ip.h>           // ip头部 头文件  
  10. // #include <net/if_arp.h>            // arp头部 头文件  
  11.   
  12. int main(int argc,charchar *argv[])  
  13. {  
  14.     int i = 0;  
  15.     unsigned char buf[1024] = "";  
  16.     int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));  
  17.     while(1)  
  18.     {  
  19.         unsigned char src_mac[18] = "";  
  20.         unsigned char dst_mac[18] = "";  
  21.         //获取链路层的数据帧  
  22.         recvfrom(sock_raw_fd, buf, sizeof(buf),0,NULL,NULL);  
  23.           
  24.         //从数据中提取mac首部信息(14个字节)  
  25.         struct ether_header *ethdr = NULL;  
  26.         ethdr = (struct ether_header *)buf;  
  27.           
  28.         //从buf里提取目的mac、源mac  
  29.         sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x", ethdr->ether_dhost[0], ethdr->ether_dhost[1],ethdr->ether_dhost[2],ethdr->ether_dhost[3],ethdr->ether_dhost[4],ethdr->ether_dhost[5]);  
  30.         sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x", ethdr->ether_shost[0], ethdr->ether_shost[1],ethdr->ether_shost[2],ethdr->ether_shost[3],ethdr->ether_shost[4],ethdr->ether_shost[5]);  
  31.           
  32.         //判断是否为IP数据包  
  33.         if0x0800 == ntohs(ethdr->ether_type) )  
  34.         {     
  35.             printf("______________IP数据报_______________\n");  
  36.             printf("MAC:%s >> %s\n",src_mac,dst_mac);  
  37.               
  38.         }//0x0806为ARP数据包, 0x8035为RARP数据包  
  39.         else if0x0806 == ntohs(ethdr->ether_type) || 0x8035 == ntohs(ethdr->ether_type) )  
  40.         {  
  41.             printf("______________ARP数据报_______________\n");  
  42.             printf("MAC:%s >> %s\n",src_mac,dst_mac);  
  43.         }  
  44.           
  45.     }  
  46.     return 0;  
  47. }  
0 0
原创粉丝点击