libpcap c / c ++回调函数

时间:2018-02-04 16:06:43

标签: c++ libpcap

我写了研究申请,我对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

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

1 个答案:

答案 0 :(得分:1)

您遇到的问题是因为您试图在没有实例的情况下调用成员函数。由于您的回调不使用任何类成员属性,您可以将其声明为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)
    {
       ...
    }