Libpcap读取文件问题解决

来源:互联网 发布:lake软件 编辑:程序博客网 时间:2024/05/29 14:29

问题描述

    在使用Libpcap库读取PCAP文件(使用pcap_open_offline函数打开PCAP文件)时,在pcap包头(structpcap_pkthdr)中出现len与caplen不相等的情况,并且最大的caplen为112字节。但是,使用Wireshark打开该PCAP文件是正常的,即len与caplen是一样的。

问题解决

    使用UltraEdit打开PCAP文件,对照PCAP文件格式发现在PCAP文件头中SnapLen为0x60(96字节)。PCAP文件头中SnapLen为4个字节的整数,从第17个字节开始,其值为6000 00 00,把SnapLen修改为FF FF 00 00后问题得到解决。
    原因:使用pcap_open_offline函数打开PCAP文件时,在读取报文时会根据PCAP文件头部的SnapLen对报文进行截断。如果SnapLen为96,那么caplen最大值为112字节,大于112字节的报文数据会被丢弃,SnapLen于CapLen两者之间差16个字节。
    PCAP文件头格式如下:
struct pcap_file_header {
       bpf_u_int32 magic;
       u_short version_major;
       u_short version_minor;
       bpf_int32 thiszone;    
       bpf_u_int32 sigfigs;   
       bpf_u_int32 snaplen;   
       bpf_u_int32 linktype;  
};
    把SnapLen修改为0x50 = 80,最大的CapLen为96字节,两者之差为16个字节。
    当SnapLen为0x60=96时,最大的CapLen为112字节,两者之差也为16个字节。
    这16个字节为PCAP报文头部(pcap_pkthdr)的字节数,如下:
structpcap_pkthdr
{
structtimeval         ts;
      DWORD              caplen;
      DWORD              len;
}
 
struct timeval
{
     DWORD       GMTtime;
     DWORD       microTime
}
原创粉丝点击