這幾天完成一個對比以太網幀的程序(c語言),老師給了以太網幀頭部和IP報文頭部的結構體,跟實際抓取到的數據包的格式是相同的。
以太網幀頭部的數據結構:
typedef struct { unsigned char dest_mac[6]; unsigned char src_mac[6]; unsigned short eth_type; } ethernet_header;
eth_type字段用來指明上層協議類型,兩字節。eth_type字段常見值及對應協議
0x0800 網際協議(IP)
0x0806 地址解析協議(ARP)
0x8035 反向地址解析協議
更多可見:http://blog.sina.com.cn/s/blog_a206e924010111tm.html
IP報文格式頭部的數據結構:
typedef struct { unsigned char header_len:4; unsigned char version:4; unsigned char tos:8; unsigned short total_len; unsigned short ident; unsigned short flags; unsigned char ttl:8; unsigned char proto:8; unsigned short checksum; unsigned char src_ip[4]; unsigned char dest_ip[4]; } ip_header;
IP報文的格式如圖:
IP報文中有的字段只占了4位,結構體中的成員采用了位域定義的形式。
struct name{
type name:n;
};
成員變量name占用空間為n位,n必須為正整數,其值必須小於type類型占用的位數,如果type是int,那么n必須是1~31之間的整數。對於位域類型的成員,在賦值時如果實際值超過n位能表達的范圍,超出部分將會被截掉。
結構體中 首部長度字段 在 版本字段的前面,跟圖片中的相反,這兩個字段占一個字節,版本字段是低四位,在內存中存儲的位置是 首部長度|版本 ,因此結構體中將首部長度放在第一個。
proto表示傳輸層的協議,常見的對應描述:
1 Internet控制消息(ICMP)
2 Internet組管理(IGMP)
6 傳輸控制(TCP)
17 用戶數據報文(UDP)
更多見:http://blog.csdn.net/jiary5201314/article/details/41213561
IP報文中的報頭長度字段表示的是頭部占32比特的數字,即IP報文頭部的長度為 header_len * 4 字節。