Pregunta

I searched that in the forum and I tried several solutions that I found here, but none worked. My problem is, I need to pass a private member of a class as a pointer to a function argument, so I tried put this method as static but it didn't work, follows code.

  class monitor {
   public:
    monitor(device *dev);
    ...
    void exec();
    void end();
    virtual ~monitor();

   private:
    static pcap_handler process_packet(u_char *arg, const struct pcap_pkthdr* pkthdr,
                          const u_char * packet);
    static void thread_func(monitor *mon);
      device *dev;
        };

and the how the method is used:

void monitor::exec() {
  std::thread t (monitor::thread_func, this);
}

void monitor::thread_func(monitor *mon) {
  pcap_loop(mon->dev->get_descr(), -1, monitor::process_packet, reinterpret_cast<u_char*>(mon)); // error in this line
}

pcap_handler monitor::process_packet(u_char *arg, const struct pcap_pkthdr* pkthdr,
                             const u_char * packet) {
  monitor *mon = reinterpret_cast<monitor*>(arg);

  ...
}

The errors was:

error: invalid conversion from ‘void (* (*)(u_char*, const pcap_pkthdr*, const u_char*))(u_char*, const pcap_pkthdr*, const u_char*) {aka void (* (*)(unsigned char*, const pcap_pkthdr*, const unsigned char*))(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’ to ‘pcap_handler {aka void (*)(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’ [-fpermissive]

error: initializing argument 3 of ‘int pcap_loop(pcap_t*, int, pcap_handler, u_char*)’ [-fpermissive]

follows the code of the prototypes of the libpcap functions

typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);

int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user);

I tried several solutions explained in other post as:

pthread in a class

Private member function that takes a pointer to a private member in the same class

C++: Calling member function via pointer

Function pointer to class member function problems

But none of these posts solved my problem, so, has someone some tip?

Thanks...

¿Fue útil?

Solución

process_packet is a function returning a pcap_handler. It is not a pcap_handler.

To make it a pcap_handler, change its line to:

static void process_packet(u_char *arg, const struct pcap_pkthdr* pkthdr,
                      const u_char * packet);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top