简体   繁体   English

在 libpcap 回调中获取数据包号

[英]get packet number in libpcap callback

I'm using libpcap to process the WS output.我正在使用 libpcap 来处理 WS output。

My question is: can I have access in the packet number in the pcap_loop callback?我的问题是:我可以访问 pcap_loop 回调中的数据包号吗? Or I will have to use a static variable?或者我将不得不使用 static 变量?

EDIT:编辑:

As requested:按照要求:

long Foo:Main()
{
    handle = pcap_open_dead( DLT_EN10MB, MAX_PACKET_SIZE );
    if( !handle )
    {
    }
    dumper = pcap_dump_open( handle, fileOut.ToString() );
    if( !dumper )
    {
    }
    handle = pcap_open_offline( fileNameStr.ToString(), errbuf );
    if( !handle )
    {
    }
    if( pcap_compile( handle, &fp, FltString.ToString(), 0, net ) == PCAP_ERROR )
    {
    }
    // Set filter for JREAP only
    if( pcap_setfilter( handle, &fp ) == PCAP_ERROR )
    {
    }
    unchar *uncharThis = reinterpret_cast<unchar*>( this );
    // The pcap_loop is implemented like:
    // for( int i = 0; i < num_of_packets; i++ )
    //     ProcessPackets();
    // where i is the current packet number to process
    int ret_val = pcap_loop( handle, 0, ProcessPackets, uncharThis );
    if( ret_val == PCAP_ERROR )
    {
    }
}

bool Foo::ProcessPackets(unchar *userData, const struct pcap_pkthdr *pkthdr, const unchar *packet)
{
    // This function will be called for every packet in the pcap file
    // that satisfy the filter condition.
    // Inside this function do I have access to the packet number.
    // Do I have an access to the variable `i` from the comment above
    // Or I will have to introduce a static variable here?
}

libpcap does not keep track of the ordinal numbers of packets, so you'll have to maintain a packet count in your code. libpcap 不跟踪数据包的序数,因此您必须在代码中维护数据包计数。

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

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