C++ libpcap c/c++;回调函数
我写学习申请书,我没有c/c++方面的经验。我在调用回调函数时遇到问题,当我尝试调用回调函数时,我出现了一个错误 非静态成员函数的使用无效 if(pcap_循环(句柄,1,got_数据包,NULL)ip_addr->text(); QByteArray字节数组=过滤器表达式.toUtf8(); const char*filter_exp=byte_array.data(); 结构bpf_程序fp; bpf_____int32掩模; bpf____int32 net;/*我们的嗅探设备的IP*/ 结构pcap_pkthdr头; 结构乙醚_头*eptr; 结构udphdr*udp_头; const u_char*数据包; 结构ip*ip; 结构rtp_头*rtp_头; u_char*ptr; if(pcap_lookupnet(dev、&net、&mask、errbuf)=-1){ fprintf(stderr,“无法获取设备%s的网络掩码:%s\n”,dev,errbuf); 净=0; 掩码=0; 申报表(1); } pcap_t*手柄; handle=pcap\u open\u live(dev,BUFSIZ,11000,errbuf); if(handle==NULL){ fprintf(stderr,“无法打开设备%s:%s\n”,dev,errbuf); 返回(2); } if(pcap_compile(handle,&fp,filter_exp,0,net)=-1){ fprintf(stderr,“无法分析筛选器%s:%s\n”,筛选器\u exp,pcap\u geterr(句柄)); 返回(3); } if(pcap_setfilter(handle,&fp)=-1){ fprintf(stderr,“无法安装filtecho deb”http://repository.spotify.com 稳定的非自由| sudo tee/etc/apt/sources.list.d/spotify.lister%s:%s\n“,过滤器exp,pcap_geterr(句柄)); 返回(4); } //packet=pcap_next(句柄和报头); //pcap_循环(句柄,5,got_数据包,NULL);C++ libpcap c/c++;回调函数,c++,libpcap,C++,Libpcap,我写学习申请书,我没有c/c++方面的经验。我在调用回调函数时遇到问题,当我尝试调用回调函数时,我出现了一个错误 非静态成员函数的使用无效 if(pcap_循环(句柄,1,got_数据包,NULL)ip_addr->text(); QByteArray字节数组=过滤器表达式.toUtf8(); const char*filter_exp=byte_array.data(); 结构bpf_程序fp; bpf_____int32掩模; bpf____int32 net;/*我们的嗅探设备的IP*/
如果(pcap_loop(handle,6,got_packet,NULL)您遇到的问题是因为您试图在没有实例的情况下调用成员函数。由于您的回调不使用任何类成员属性,您可以将其声明为
static
,这意味着您可以在没有类实例的情况下调用它
在标题上:
static void got_packet(u_char *args, const pcap_pkthdr *header, const u_char *packet);
关于实施:
static void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
...
}
您不能将非
静态
类成员函数用作普通函数指针。在标头和实现上将回调声明为静态
,这样它就可以工作了。是的,就是这样。问题解决了
#ifndef CAPTHREAD_H
#define CAPTHREAD_H
#include <pcap.h>
#include <netinet/ether.h> //plik nagłówkowy umożliwiający przekonwertowanie danych z nagłówka na kod ASCII
#include <netinet/ip.h> //plik zawierający struktury nagłówka IP i umożliwiający przekonwertowanie danych z nagłówka na kod ASCII
#include <net/ethernet.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <QDebug>
#include <iostream>
#include <stdio.h>
#include "ui_mainwindow.h"
class CapThread
{
public:
CapThread();
CapThread(Ui::MainWindow *ui);
~CapThread();
void got_packet(u_char *args, const pcap_pkthdr *header, const u_char *packet);
int capture();
private:
struct rtp;
Ui::MainWindow *ui;
char *dev, errbuf;
pcap_t *handle;
char filter_exp[];
struct bpf_program fp;
struct ether_header *eptr;
bpf_u_int32 mask;
bpf_u_int32 net;
};
#endif // CAPTHREAD_H
static void got_packet(u_char *args, const pcap_pkthdr *header, const u_char *packet);
static void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
...
}