gpt4 book ai didi

c++ - libpcap c/c++ 回调函数

转载 作者:行者123 更新时间:2023-11-30 05:04:59 30 4
gpt4 key购买 nike

<分区>

我写学习申请,我没有 c/c++ 经验。我在调用回调函数时遇到问题,当我尝试调用回调函数时出现错误

非静态成员函数的无效使用 if(pcap_loop(handle, 1, got_packet, NULL)<0){

这是我的回调函数:

void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
static int count = 1;
fprintf(stdout,"%d, ",count);
if(count == 4)
fprintf(stdout,"Come on baby sayyy you love me!!! ");
if(count == 7)
fprintf(stdout,"Tiiimmmeesss!! ");
fflush(stdout);
count++;
}

这就是我调用这个函数的方式

    int CapThread::capture()
{

char *dev, errbuf[PCAP_ERRBUF_SIZE];

dev = pcap_lookupdev(errbuf);

QString filter_expression = "udp and ip dst host "+ui->ip_addr->text();
QByteArray byte_array =filter_expression.toUtf8();

const char *filter_exp = byte_array.data();
struct bpf_program fp;
bpf_u_int32 mask;
bpf_u_int32 net; /* The IP of our sniffing device */
struct pcap_pkthdr header;
struct ether_header *eptr;
struct udphdr *udp_header;
const u_char *packet;
struct ip *ip;
struct rtp_header *rtp_header;


u_char *ptr;

if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
return(1);
}

pcap_t *handle;
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}

if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(3);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filtecho deb http://repository.spotify.com stable non-free | sudo tee /etc/apt/sources.list.d/spotify.lister %s: %s\n", filter_exp, pcap_geterr(handle));
return(4);
}
// packet = pcap_next(handle, &header);
// pcap_loop(handle,5,got_packet,NULL);
if(pcap_loop(handle, 6, got_packet, NULL)<0){
qDebug()<<"got packet";
}

pcap_close(handle);
return 0;
}

这是我的头文件的样子:

    #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

实际上我没有想法,有人可以帮助我吗?

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com