2012-02-25 21 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