2012-02-25 100 views
0

我写网络分析仪和我需要过滤保存在文件包,我已经写了一些代码来过滤HTTP数据包,但我不知道,如果它工作,因为它应该,因为当我使用我在PCAP转储代码的结果是5包,但在过滤器Wireshark的书面HTTP给了我2个包,如果我用:过滤包

tcpdump port http -r trace-1.pcap 

它给了我11个包。

好吧,3个不同的结果,这有点混乱。

过滤器和在我的代码数据包处理:

... 
if (pcap_compile(handle, &fcode, "tcp port 80", 1, netmask) < 0) 
... 
while ((packet = pcap_next(handle,&header))) { 
    u_char *pkt_ptr = (u_char *)packet; 

    //parse the first (ethernet) header, grabbing the type field 
    int ether_type = ((int)(pkt_ptr[12]) << 8) | (int)pkt_ptr[13]; 
    int ether_offset = 0; 

    if (ether_type == ETHER_TYPE_IP) // ethernet II 
     ether_offset = 14; 
    else if (ether_type == ETHER_TYPE_8021Q) // 802 
     ether_offset = 18; 
    else 
     fprintf(stderr, "Unknown ethernet type, %04X, skipping...\n", ether_type); 

    //parse the IP header 
    pkt_ptr += ether_offset; //skip past the Ethernet II header 
    struct ip_header *ip_hdr = (struct ip_header *)pkt_ptr; 
    int packet_length = ntohs(ip_hdr->tlen); 

    printf("\n%d - packet length: %d, and the capture lenght: %d\n", cnt++,packet_length, header.caplen); 

} 

我的问题是,为什么有过滤HTTP时,3种不同的结果呢?和/或如果我过滤错了,那么我该怎么做对,也有一种方法来过滤http(或ssh,ftp,telnet ...)数据包使用别的不同端口号?

谢谢

+0

你比较各色nce或结果?或者你可以附加pcap文件链接,否则很难说明原因。 – 2012-02-26 10:32:49

回答

0

所以我想通了。它花了一点搜索和理解,但我做到了。

Wireshark的过滤器设置为已经在TCP端口80设置,并且还设置为PSH,ACK标志的HTTP过滤器数据包。意识到这一点后,导致相同数量的数据包的tcpdump命令参数很容易写入。

所以现在Wireshark的和tcpdump的给出了相同的结果

那我的代码?还有我想,其实我曾在我的问题上的错误,过滤器

if (pcap_compile(handle, &fcode, "tcp port 80", 1, netmask) < 0) 

确实给了11个包(src和目标端口设置为80,无论TCP标志是什么)

我们筛选所需数据包是一个很好的理解过滤器语法 或设置为仅过滤端口80(21,22,...),然后在回调函数中或在while循环获取tcp头并从那里获得标志和使用掩码的问题看它是否是正确的数据包(PSH,ACK,SYN ...)标志数量例如here