【问题标题】:get packet number in libpcap callback在 libpcap 回调中获取数据包号
【发布时间】:2021-02-26 21:19:04
【问题描述】:

我正在使用 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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2015-10-23
    • 1970-01-01
    • 2011-02-05
    相关资源
    最近更新 更多