2014-01-15 144 views
2

一個PCAP文件,我想讀一個PCAP文件,並得到一個流的數據包的報頭信息...閱讀C++

爲了這個,我發現從http://www.cnblogs.com/xiangshancuizhu/archive/2012/10/14/2723654.html一個C++程序,讀取PCAP文件,並有頭指針,該指針

類只是指向頭的長度,但我想訪問頭的其他功能

如SYN,FIN,ACK,序列編號,......這是我的代碼,我該怎麼做?

char errbuff[PCAP_ERRBUF_SIZE]; 

/* 
* Step 4 - Open the file and store result in pointer to pcap_t 
*/ 

// Use pcap_open_offline 
// http://www.winpcap.org/docs/docs_41b5/html/group__wpcapfunc.html#g91078168a13de8848df2b7b83d1f5b69 
pcap_t * pcap = pcap_open_offline(file.c_str(), errbuff); 

/* 
* Step 5 - Create a header and a data object 
*/ 

// Create a header object: 
// http://www.winpcap.org/docs/docs_40_2/html/structpcap__pkthdr.html 
struct pcap_pkthdr *header; 

// Create a character array using a u_char 
// u_char is defined here: 
// C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\WinSock2.h 
// typedef unsigned char u_char; 
const u_char *data; 

/* 
* Step 6 - Loop through packets and print them to screen 
*/ 
u_int packetCount = 0; 
while (int returnValue = pcap_next_ex(pcap, &header, &data) >= 0) 
{ 
    // Print using printf. See printf reference: 
    // http://www.cplusplus.com/reference/clibrary/cstdio/printf/ 

    // Show the packet number 
    printf("Packet # %i\n", ++packetCount); 

    // Show the size in bytes of the packet 
    printf("Packet size: %d bytes\n", header->len); 

和頭是一個結構,如:

struct pcap_pkthdr { 
    struct timeval ts; /* time stamp */ 
    bpf_u_int32 caplen; /* length of portion present */ 
    bpf_u_int32 len; /* length this packet (off wire) */ 
}; 

,我想這種結構有更多的成員,我該怎麼辦? 非常感謝。

回答

3

您不能將更多字段添加到pcap_pkthdr - libpcap不會對這些字段執行任何操作。數據包數據字段(如鏈路層,IP和TCP標頭)是NOT該標頭的功能。

分組數據由data變量指向。 這就是,其中鏈路層頭是,IP頭在IP數據包中,TCP頭在TCP數據包中,等等。例如參見Tim Carstens' tutorial on how to use libpcap

您可能想看看使用libcrafter構建程序,它是「C++的高級庫,旨在簡化網絡數據包的創建和解碼」。您可能對「製作」部分不感興趣,但會對「解碼」部分感興趣。

+0

非常感謝你的回答,我讀了這個庫,正如我所說的我想訪問標誌位,正如我在這個庫函數中看到的那樣,它可以得到我的標誌,輸出如下所示: 我使用該標誌,並找到tcp數據包中的SIN或FIN? – user3116869