【发布时间】: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?
}
【问题讨论】:
-
请edit您的问题包含您拥有的完整源代码作为minimal reproducible example,其他人可以编译和测试。请参阅:Why is “Is it possible to…” a poorly worded question?(不要只是将您的问题改为“如何”)。请显示您尝试过的尝试以及您从尝试中得到的问题/错误消息。
-
@Progman,我编辑了问题,是否更清楚?