【Linux网络编程】原始套接字实例:MAC 头部报文分析
生活随笔
收集整理的這篇文章主要介紹了
【Linux网络编程】原始套接字实例:MAC 头部报文分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過《Linux網絡編程——原始套接字編程》得知,我們可以通過原始套接字以及?recvfrom( ) 可以獲取鏈路層的數據包,那我們接收的鏈路層數據包到底長什么樣的呢?
MAC 頭部(有線局域網)
注意:CRC、PAD 在組包時可以忽略
鏈路層數據包的其中一種情況:
unsigned char msg[1024] = {//--------------組MAC--------14------0xb8, 0x88, 0xe3, 0xe1, 0x10, 0xe6, // dst_mac: b8:88:e3:e1:10:e60xc8, 0x9c, 0xdc, 0xb7, 0x0f, 0x19, // src_mac: c8:9c:dc:b7:0f:190x08, 0x00, // 類型:0x0800 IP協議// …… ……// …… …… };
接收的鏈路層數據包,并對其進行簡單分析:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ether.h>int main(int argc,char *argv[]) {int i = 0;unsigned char buf[1024] = "";int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));while(1){unsigned char src_mac[18] = "";unsigned char dst_mac[18] = "";//獲取鏈路層的數據幀recvfrom(sock_raw_fd, buf, sizeof(buf),0,NULL,NULL);//從buf里提取目的mac、源macsprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);//判斷是否為IP數據包if(buf[12]==0x08 && buf[13]==0x00){ printf("______________IP數據報_______________\n");printf("MAC:%s >> %s\n",src_mac,dst_mac);}//判斷是否為ARP數據包else if(buf[12]==0x08 && buf[13]==0x06){printf("______________ARP數據報_______________\n");printf("MAC:%s >> %s\n",src_mac,dst_mac);}//判斷是否為RARP數據包else if(buf[12]==0x80 && buf[13]==0x35){printf("______________RARP數據報_______________\n");printf("MAC:%s>>%s\n",src_mac,dst_mac);}}return 0; }
記得以管理者權限運行程序:
每個報文頭部都有一個相應的結構體,通過這些結構體對報文進行相應的組包或拆包會方便很多。
ubuntu 12.04 中描述網絡協議結構的文件如下:
以太網頭部(所需要頭文件:#include <net/ethernet.h>):
上面的例子,改為用結構體實現,如下:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ether.h> #include <net/ethernet.h> // 以太網頭部 頭文件 #include <netinet/ip.h> // ip頭部 頭文件 // #include <net/if_arp.h> // arp頭部 頭文件int main(int argc,char *argv[]) {int i = 0;unsigned char buf[1024] = "";int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));while(1){unsigned char src_mac[18] = "";unsigned char dst_mac[18] = "";//獲取鏈路層的數據幀recvfrom(sock_raw_fd, buf, sizeof(buf),0,NULL,NULL);//從數據中提取mac首部信息(14個字節)struct ether_header *ethdr = NULL;ethdr = (struct ether_header *)buf;//從buf里提取目的mac、源macsprintf(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]);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]);//判斷是否為IP數據包if( 0x0800 == ntohs(ethdr->ether_type) ){ printf("______________IP數據報_______________\n");printf("MAC:%s >> %s\n",src_mac,dst_mac);}//0x0806為ARP數據包, 0x8035為RARP數據包else if( 0x0806 == ntohs(ethdr->ether_type) || 0x8035 == ntohs(ethdr->ether_type) ){printf("______________ARP數據報_______________\n");printf("MAC:%s >> %s\n",src_mac,dst_mac);}}return 0; }
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的【Linux网络编程】原始套接字实例:MAC 头部报文分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux网络编程】原始套接字编程
- 下一篇: 【Linux网络编程】原始套接字实例:M