首页 > 解决方案 > 在 libpcap 回调中获取数据包号

问题描述

我正在使用 libpcap 来处理 WS 输出。

我的问题是:我可以访问 pcap_loop 回调中的数据包号吗?或者我将不得不使用静态变量?

编辑:

按照要求:

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?
}

标签: c++libpcap

解决方案


libpcap 不跟踪数据包的序数,因此您必须在代码中维护数据包计数。


推荐阅读