c++ - 在 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?
}
解决方案
libpcap 不跟踪数据包的序数,因此您必须在代码中维护数据包计数。
推荐阅读
- c# - Web.Config 中的 Oracle.ManagedDataAccess TNS_ADMIN
- cloud-foundry - 如何在 pcf 命令中从特定应用程序获取路由?
- qt - 在 QML 中创建带有背景和垂直环绕的字幕
- spring-boot - 从命令行参数覆盖 Spring 中的应用程序属性值
- javascript - 在 Google 地图信息窗口中将 href 显示为图像
- android - 在 API 级别 26 上集成 Google Maps API v1
- android - 使用 Rxjava2 交错项目
- prolog - 使用 prolog 编码给定的输入
- sql - 案例陈述选择
- android - MutableLivedata 观察者触发