参考http://blog.csdn.net/koudaidai/article/details/7673082
首先看下最常见的包的层次:frame层是不是实际传输的数据,是tcpdump添加的一层。eth层如果显示linuxcooked的话是没有指定网卡抓包的原因需要tcpdump -i 指定网卡。
可能会有其他协议层,自行添加即可。例如下图2
1、几个结构体
//pacp文件头结构体 struct pcap_file_header { bpf_u_int32 magic; /* 0xa1b2c3d4 */ u_short version_major; /* magjor Version 2 */ u_short version_minor; /* magjor Version 4 */ bpf_int32 thiszone; /* gmt to local correction */ bpf_u_int32 sigfigs; /* accuracy of timestamps */ bpf_u_int32 snaplen; /* max length saved portion of each pkt */ bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */ }; //时间戳 struct time_val { int tv_sec; /* seconds 含义同 time_t 对象的值 */ int tv_usec; /* and microseconds */ }; //pcap数据包头结构体 struct pcap_pkthdr { struct time_val ts; /* time stamp */ bpf_u_int32 caplen; /* length of portion present */ bpf_u_int32 len; /* length this packet (off wire) */ }; //数据帧头 typedef struct FramHeader_t { //Pcap捕获的数据帧头 u_int8 DstMAC[6]; //目的MAC地址 u_int8 SrcMAC[6]; //源MAC地址 u_short FrameType; //帧类型 } FramHeader_t; //IP数据报头 typedef struct IPHeader_t { //IP数据报头 u_int8 Ver_HLen; //版本+报头长度 u_int8 TOS; //服务类型 u_int16 TotalLen; //总长度 u_int16 ID; //标识 u_int16 Flag_Segment; //标志+片偏移 u_int8 TTL; //生存周期 u_int8 Protocol; //协议类型 u_int16 Checksum; //头部校验和 u_int32 SrcIP; //源IP地址 u_int32 DstIP; //目的IP地址 } IPHeader_t; //TCP数据报头 typedef struct TCPHeader_t { //TCP数据报头 u_int16 SrcPort;//源端口 u_int16 DstPort;//目的端口 u_int32 SeqNO;//序号 u_int32 AckNO; //确认号 u_int8 HeaderLen; //数据报头的长度(4 bit) + 保留(4 bit) u_int8 Flags; //标识TCP不同的控制消息 u_int16 Window; //窗口大小 u_int16 Checksum; //校验和 u_int16 UrgentPointer; //紧急指针 }TCPHeader_t; //UDP数据 typedef struct UDPHeader_s { u_int16_t SrcPort; // 源端口号16bit u_int16_t DstPort; // 目的端口号16bit u_int16_t len; // 数据包长度16bit u_int16_t checkSum; // 校验和16bit }UDPHeader_t;
2、代码只是解析源ip 包大小以及时间输出到文件中,其他字段自行添加。简单的将就是read协议长度数据到对应协议结构体中输出即可。可以认为都是小端序的。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<netinet/in.h> #include<time.h> #include <unistd.h> #include <sys/stat.h> #define BUFSIZE 10240 #define STRSIZE 1024 typedef int32_t bpf_int32; typedef u_int32_t bpf_u_int32; typedef u_int16_t u_short; typedef u_int32_t u_int32; typedef u_int16_t u_int16; typedef u_int8_t u_int8; //pacp文件头结构体 struct pcap_file_header { bpf_u_int32 magic; /* 0xa1b2c3d4 */ u_short version_major; /* magjor Version 2 */ u_short version_minor; /* magjor Version 4 */ bpf_int32 thiszone; /* gmt to local correction */ bpf_u_int32 si