pcap文件解析(二)--初识IP包在上一篇我们简单认识pcap文件,现在我们来看看IP包的大致结构。IP包在开始之前
pcap文件解析(二)--初识IP包
在上一篇我们简单认识pcap文件,现在我们来看看IP包的大致结构。
IP包在开始之前给大家推荐一个非常好用的工具RFCView,通过这个工具我们只需要输入RFC(Request For Comments,基本的因特网通讯协定都有在RFC文件内详细说明)号码就能查看各种RFC文档了。在RFC791中详细定义了IP包的数据结构,这里做大致介绍: 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Version:版本号 4位IHL:包头长度,IHL并不直接表示包头所占用的长度,而是表示包头占用了多少个32位 4位Type of Service: 服务类型,为在数据传输中选择具体网络类型的摘要描述。 8位
Total Length: 总长度 16位Identification: 用于组装数据包的标识符 16位
Flags:控制表示 3位
Bit 0: reserved, must be zero
Bit 1: (DF) 0 = May Fragment, 1 = Don't Fragment.
Bit 2: (MF) 0 = Last Fragment, 1 = More Fragments.
Fragment Offset:数据偏移 13位
Time to Live:生命周期 8位
Protocol:协议类型,具体定义见:RFC790文档 8位
Header Checksum:头校验码 16位
Source Address:源地址 32位
Destination Address:目的地址 32位
IP数据在PCAP文件中的位置pcap头数据包头目的MAC、源MAC、TYPE(14字节)IP数据……数据包头目的MAC、源MAC、TYPE(14字节)IP数据……
根据上表……在每个IP数据包中,IP数据的起始位置=数据包起始位置+包头长度+14。解析IP数据下面是一个重pacp文件中导出SCTP协议数据的程序。
pcap_header.h
#include"pcap_adapter.h"int main(){ if( OpenPcapFile( "ZSRNC3_IUPS_inerface3.pcap")) { printf( "打开pcap文件失败"); return 0; } __pcap_header header; GetPcapHeader( &header);int iNo = 1;MoveFirst();FILE* pwFile = fopen( "export-file.pcap", "wb");fwrite((void*)&header, sizeof( __pcap_header), 1, pwFile);while( !IeEof()){__pk_header data;__ip_header ipData;Byte* pBuffer; GetPacketAndMoveNext( &data, &pBuffer); GetIpData( &ipData, pBuffer); // SCTP == 132 if( ipData.byteProtocol == 132) { fwrite( (void*)(&data), sizeof(struct __pkthdr), 1, pwFile); fwrite( (void*)pBuffer, data.iLength, 1, pwFile); } free( pBuffer);} fclose( pwFile);printf( "Export over");return 1;}