1

I write application for studies, i am not experienced with c/c++. I have problem with call callback function, when I try call callback function i have an error

invalid use of non-static member function if(pcap_loop(handle, 1, got_packet, NULL)<0){

Here is my callback function:

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++;
    }

this is how I call this function

    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;
}

this is how my header file look like:

    #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

actually i am out of ideas, can someone help me?

dave
  • 11
  • 1
  • 1
    You cannot use non `static` class member functions as plain function pointers. –  Feb 04 '18 at 16:08
  • 1
    Declare your callback as `static` on the header and on the implementation and it will work. – Nogoseke Feb 04 '18 at 16:11

1 Answers1

1

The problem you are having is because you are trying to call a member function without an instance. Since your callback does not use any class member attributes, you could declare it as static, this means you can call it without an instance of the class.

On the header:

static void got_packet(u_char *args, const pcap_pkthdr *header, const u_char *packet);

On the implementation:

static void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
    {
       ...
    }
Nogoseke
  • 969
  • 1
  • 12
  • 24