0

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...

Community
  • 1
  • 1
Alex
  • 3,301
  • 4
  • 29
  • 43
  • What are you returning from `process_packet`? – David G Jan 28 '14 at 00:49
  • What is in the way of you posting a http://sscce.org ? – Yakk - Adam Nevraumont Jan 28 '14 at 00:50
  • In fact I am returning nothing, I put only return. – Alex Jan 28 '14 at 00:55
  • Yakk, unfortunately if I put shorter than that, I can not explain the problem. – Alex Jan 28 '14 at 00:58
  • 1
    @Alex no, it is easy to isolate. Start with your code. Eliminate a line. Error remain? Keep the line eliminated. Repeat until each line eliminated either cause a different error, or prevents the error. Now look at the remaining lines: can you simplify them? Try simplifying a line. Does the error remain? If so, keep it simpler. Repeat until each line is simpler. There are entire FUNCTIONS above that have nothing to do with your error, and you needlessly included them. – Yakk - Adam Nevraumont Jan 28 '14 at 02:41
  • @Yakk, I tried that, I eliminated the line and the error kept, in fact the answer given solved my problem. Thanks. – Alex Jan 28 '14 at 14:44
  • If you tried eliminating lines that did not matter, you should POST a compiling (except for the error) example the MINIMAL case that generates your error. Here is a non-minimal complete example: http://ideone.com/RPisYh - note the error is the same, and the changes are minimal. You should have started with that, then started eliminating code that did not matter and you got the same error. Spewing a pile of code to stack overflow and saying "help" is both impolite, and means you are less likely to get help, *and* the process of http://sscce.org can help you solve the problem yourself. – Yakk - Adam Nevraumont Jan 28 '14 at 14:48
  • sorry, I wrote wrong, in fact the error disappeared. I knew the problem was in this line, and so the solution provided solved – Alex Jan 28 '14 at 15:31

1 Answers1

1

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);
David G
  • 94,763
  • 41
  • 167
  • 253
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524