天天看點

Linux網絡程式設計——原始套接字執行個體:MAC 頭部封包分析

通過《Linux網絡程式設計——原始套接字程式設計》得知,我們可以通過原始套接字以及 recvfrom( ) 可以擷取鍊路層的資料包,那我們接收的鍊路層資料包到底長什麼樣的呢?

鍊路層封包格式

Linux網絡程式設計——原始套接字執行個體:MAC 頭部封包分析

MAC 頭部(有線區域網路)

Linux網絡程式設計——原始套接字執行個體:MAC 頭部封包分析

注意:CRC、PAD 在組包時可以忽略

鍊路層資料包的其中一種情況:

1 unsigned char msg[1024] = {
2     //--------------組MAC--------14------
3     0xb8, 0x88, 0xe3, 0xe1, 0x10, 0xe6, // dst_mac: b8:88:e3:e1:10:e6
4     0xc8, 0x9c, 0xdc, 0xb7, 0x0f, 0x19, // src_mac: c8:9c:dc:b7:0f:19
5     0x08, 0x00,                         // 類型:0x0800 IP協定
6     // …… ……
7     // …… ……
8 };      

接收的鍊路層資料包,并對其進行簡單分析:

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,char *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資料報_______________
");
27             printf("MAC:%s >> %s
",src_mac,dst_mac);
28         }//判斷是否為ARP資料包
29         else if(buf[12]==0x08 && buf[13]==0x06)
30         {
31             printf("______________ARP資料報_______________
");
32             printf("MAC:%s >> %s
",src_mac,dst_mac);
33         }//判斷是否為RARP資料包
34         else if(buf[12]==0x80 && buf[13]==0x35)
35         {
36             printf("______________RARP資料報_______________
");
37             printf("MAC:%s>>%s
",src_mac,dst_mac);
38         }
39     }
40     return 0;
41 }      

記得以管理者權限運作程式:

Linux網絡程式設計——原始套接字執行個體:MAC 頭部封包分析

每個封包頭部都有一個相應的結構體,通過這些結構體對封包進行相應的組包或拆包會友善很多。

ubuntu 12.04 中描述網絡協定結構的檔案如下:

Linux網絡程式設計——原始套接字執行個體:MAC 頭部封包分析
Linux網絡程式設計——原始套接字執行個體:MAC 頭部封包分析

以太網頭部(所需要頭檔案:#include <net/ethernet.h>):

Linux網絡程式設計——原始套接字執行個體:MAC 頭部封包分析

上面的例子,改為用結構體實作,如下:

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,char *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         if( 0x0800 == ntohs(ethdr->ether_type) )
34         {    
35             printf("______________IP資料報_______________
");
36             printf("MAC:%s >> %s
",src_mac,dst_mac);
37             
38         }//0x0806為ARP資料包, 0x8035為RARP資料包
39         else if( 0x0806 == ntohs(ethdr->ether_type) || 0x8035 == ntohs(ethdr->ether_type) )
40         {
41             printf("______________ARP資料報_______________
");
42             printf("MAC:%s >> %s
",src_mac,dst_mac);
43         }
44         
45     }
46     return 0;
47 }      

轉自:http://blog.csdn.net/tennysonsky/article/details/44751997