pcap_loop中的回调方法
callback method in pcap_loop
我正试图将Linux中libpcab库中的pcap_loop函数与以下原型一起使用:
int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
pcap_pkthdr是一个函数指针:
typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *, const u_char *);
在我的程序中,我在类SniffEthernet中定义了以下方法:
void SniffEthernet::got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
现在调用pcap_loop如下
pcap_loop(handle, num_packets, this->got_packet, NULL);
给我以下编译时错误:
SniffEthernet.cc:139:58: error: cannot convert ‘VENTOS::SniffEthernet::got_packet’ from type ‘void (VENTOS::SniffEthernet::)(u_char*, const pcap_pkthdr*, const u_char*) {aka void (VENTOS::SniffEthernet::)(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’ to type ‘pcap_handler {aka void (*)(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’
我在这里做错了什么?
编辑:我在这里找到了一个类似的帖子。
回调函数不能是成员函数(方法)。不要忘记,成员函数总是有隐藏的参数this
。
回调函数必须是命名空间级别的函数或类的静态成员。
如果您想让对象可用于CB函数,您可以使用user
成员(pcap_loop()
的最后一个参数,回调函数的第一个成员)和适当的类型强制转换来传递任意数据,在您的情况下,该数据应该是用于捕获的对象。
下面的代码是不完整和未经测试,但可能会给您一个想法。
class SniffEther {
private:
pcap_t *cap_handler;
char errbuf[PCAP_ERRBUF_SIZE];
/* capture-related data members (properties) */
public:
static friend void pkt_callback(u_char *user, const pcap_pkthdr *hdr, const u_char *bytes){
SniffEther *sniffer=reinterpret_cast<SniffEther *>(user);
/*
Process header and bytes.
You can call things like sniffer->somemethod(), and also
access sniffer->someproperty.
*/
}
// constructor
SniffEther(const char *if_name){
cap_handler=pcap_create(if_name, errbuf);
if(!cap_handler)
throw runtime_error(errbuf);
/* Set the many pcap_options (see pcap(3)). */
if(pcap_activate(cap_handler)!=0){
string error(pcap_geterr(cap_handler));
pcap_close(cap_handler);
throw runtime_error(error);
}
}
~SniffEther(){
if(cap_handler)
pcap_close(cap_handler);
}
void capture_loop(int pkt_count=-1){
if(
pcap_loop(
cap_handler, pkt_count, pkt_callback,
reinterpret_cast<u_char *>(this)
)==-1
)
throw runtime_error(pcap_geterr(cap_handler));
}
};
相关文章:
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- C++ - 如何在不静态的情况下将回调绑定到类方法?
- 为什么可以在没有实例变量的情况下访问静态回调方法中的静态成员变量?
- 回调方法显示错误,类型为"void(*)"的值不能用于初始化类型的实体
- 全局挂钩 DLL 仅在 C# 主窗口处于活动状态/前台时调用 C# 回调方法
- 如何从回调方法访问自定义类成员
- 异步I/O回调方法并发
- 苹果运行循环的回调方法
- C++如何通过回调方法访问成员变量
- VS2013 wndProg 回调方法
- 回调方法无法访问成员变量或实例
- 定义glDebugMessageCallbackARB回调方法
- pcap_loop中的回调方法
- ROS:subscribe函数无法识别我的回调方法
- 在构造函数中绑定到类中的回调方法
- GLUT回调方法问题
- DirectShow从内存中读取一个回调方法
- c++装饰器模式,带有模板的静态多态性和注册回调方法
- 如何为IAsyncOperation指定一个回调方法
- 在c代码中注册一个c++回调方法