2013-03-15 17 views
1

我使用的libpcap函數pcap_next()來捕獲來自其他主機 一些TCP包我檢查捕獲的數據包 的字節並注意 數據包的以太網和IP報頭是扭曲的,在很多0的亂七八糟,但TCP頭很好爲什麼分組的以太網和IP報頭,其通過libpcap的功能捕獲,被扭曲

這是什麼原因?

代碼:

pcap_t* create_pcap_handler() 
{ 
    pcap_t *handle;     /* Session handle */ 
    char *dev;      /* The device to sniff on */ 
    char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */ 
    struct bpf_program fp;   /* The compiled filter */ 
    char filter_exp[] = "port 32000"; /* The filter expression */ 
    bpf_u_int32 mask;    /* Our netmask */ 
    bpf_u_int32 net;    /* Our IP subnet*/ 

    /* Define the device */ 
    dev = pcap_lookupdev(errbuf); 
    if (dev == NULL) { 
      fprintf(stderr, "Couldn't find default device: %s\n", errbuf); 
      exit(2); 
    } 
    /* Find the properties for the device */ 
    if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) { 
      fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf); 
      net = 0; 
      mask = 0; 
    } 

    struct in_addr tmp; 
    tmp.s_addr=net; 
    char IPdotdec[20]; 
    inet_ntop(AF_INET, (void *)&tmp, IPdotdec, 16); 
    printf("net is %s\n", IPdotdec); 
    tmp.s_addr=mask; 
    inet_ntop(AF_INET, (void *)&tmp, IPdotdec, 16); 
    printf("mask is %s\n", IPdotdec); 
    printf("dev is %s\n",dev); 

    handle = pcap_open_live(dev, BUFSIZ, 0, 0, errbuf); 
    if (handle == NULL) { 
      fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf); 
      exit(2) ; 
    } 
    /* Compile and apply the filter */ 
    if (pcap_compile(handle, &fp, filter_exp, 0, mask) == -1) { 
      fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle)); 
      exit(2); 
    } 
    if (pcap_setfilter(handle, &fp) == -1) { 
      fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle)); 
      exit(2); 
    } 

    return handle; 
} 

和主要功能

int main() 
{ 
    pcap_t * pcap_handler=create_pcap_handler(); 
    struct pcap_pkthdr pcap_header;  /* The header that pcap gives us */ 
    const u_char *pcap_packet;   /* The actual packet */ 
    pcap_packet = pcap_next(pcap_handler, &pcap_header); 
    if(pcap_packet !=NULL) 
      printf("capture one packet with length of %d\n", pcap_header.len); 
    pcap_close(pcap_handler); 


    return 0; 
} 
+0

這是用於傳入還是傳出的數據包?對於傳出數據包,這些圖層的捕獲可能是錯誤的,因爲有些數據可能會在捕獲後由網卡填充。 – wldsvc 2013-03-15 16:24:06

+0

傳入數據包,我也用tcpdump捕獲了tcp數據包,那些數據包都很好 – user1944267 2013-03-15 16:25:27

+0

你能提供樣例代碼嗎? – wldsvc 2013-03-15 16:39:46

回答

1
pcap_packet = pcap_next(pcap_handler, &pcap_header); 
if(pcap_packet !=NULL) 
     printf("capture one packet with length of %d\n", pcap_header.len); 
pcap_close(pcap_handler); 
parse_pkt(pcap_packet,pcap_header.len); 

這是行不通的。

當您關閉pcap_handler,也不能保證通過調用與pcap_handler回到pcap_next()pcap_next_ex()任何指針將繼續有效。

嘗試

pcap_packet = pcap_next(pcap_handler, &pcap_header); 
if(pcap_packet !=NULL) 
     printf("capture one packet with length of %d\n", pcap_header.len); 
parse_pkt(pcap_packet,pcap_header.len); 
pcap_close(pcap_handler); 

代替。

+0

我根據你的建議修改了代碼,現在IP頭能正常工作,但是TCP seq_ack字段錯誤!!!!! TCP seq字段是可以的,出什麼問題了? – user1944267 2013-03-15 19:14:04

+0

對於TCP校驗和應該有效的傳入數據包,請自己執行tcp校驗和,看看它是否相加。如果你有一個很好的校驗和的奇怪的TCP seq ack,那麼你對這個領域的解釋是不正確的。此外,使用pcap_loop或pcap_dispatch比pcap_next更好。 – wldsvc 2013-03-15 23:21:00

+0

TCP seq_ack字段在什麼意義上是「錯誤的」?你解析它的代碼是什麼? – 2013-03-16 00:27:18

相關問題