简体   繁体   English

在C ++中读取pcap文件

[英]reading a pcap file in c++

I want to read a pcap file and get the header information of the packets of a flow... 我想读取一个pcap文件并获取流的数据包的标头信息...

for this I found a c++ program from http://www.cnblogs.com/xiangshancuizhu/archive/2012/10/14/2723654.html which read the pcap file and have a header pointer , this pointer 为此,我从http://www.cnblogs.com/xiangshancuizhu/archive/2012/10/14/2723654.html找到了一个c ++程序,该程序读取pcap文件并具有标头指针,该指针

class just point to length of header, but I want to access the other features of header 类只是指向标题的长度,但是我想访问标题的其他功能

such as syn,fin,ack,seq no,....here is my code, how can I do this? 例如syn,fin,ack,seq no .....这是我的代码,我该怎么做?

 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);

and header is a structure like: 标头是一个像这样的结构:

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) */
};

and I want this structure have more members, what should I do? 我希望这个结构有更多的成员,我该怎么办? thanks a lot. 非常感谢。

You can't add more fields to pcap_pkthdr - libpcap won't do anything with those fields. 您无法将更多字段添加到pcap_pkthdr-libpcap不会对这些字段执行任何操作。 Packet data fields, such as the link-layer, IP, and TCP headers, are NOT features of that header. 分组数据字段(例如链路层,IP和TCP头) 不是该头的功能。

The packet data is pointed to by the data variable. 包数据由data变量指向。 That's where the link-layer header is, where the IP header is in IP packets, where the TCP header is in TCP packets, and so on. 那就是链接层头,IP头位于IP数据包中,TCP头位于TCP数据包中的地方,等等。 See, for example, Tim Carstens' tutorial on how to use libpcap . 例如,请参见Tim Carstens的有关如何使用libpcap的教程

You might want to look at building your program using libcrafter , which is "a high level library for C++ designed to make easier the creation and decoding of network packets". 您可能想看看使用libcrafter构建程序,它是“ C ++的高级库,旨在简化网络数据包的创建和解码”。 You may not be interested in the "crafting" part, but would be interested in the "decoding" part. 您可能对“制作”部分不感兴趣,但对“解码”部分感兴趣。

If you want to read a pcap file (without using any external lib to read pcap file) you need to parse that file according to the format which is something like this 如果您想读取一个pcap文件(不使用任何外部库来读取pcap文件),则需要根据如下格式解析该文件:

GLOBAL_HEADER | GLOBAL_HEADER | PACKET_HEADER |PACKET_DATA | PACKET_HEADER | PACKET_DATA | PACKET_HEADER |PACKET_DATA|.... PACKET_HEADER | PACKET_DATA | ....

Read global header and after that, you parse data in a loop till file ends. 读取全局标头,然后在循环中解析数据,直到文件结束。 You must know the proper format and your structures must be correct 您必须知道正确的格式,并且结构必须正确

for this, you can follow this link https://www.tcpdump.org/pcap.html **UDP Header is not mentioned in the link 为此,您可以点击以下链接https://www.tcpdump.org/pcap.html **链接中未提及UDP标头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM