tcpdump mailing list archives

Re: Problems with libpcap and C++


From: Ury Segal <ury_segal () yahoo com>
Date: Wed, 14 Jun 2006 09:54:02 -0700 (PDT)

The buttom of the problem is this:

You excpect libpcap to call X::dumper in
the context of an instance of class X.
(The "real" first parameter of "X::dumper"
is a variable named "this" of the type "X*".)

But the libpcap API is not defining a 

`void (X::)(u_char*,  const pcap_pkthdr*, const
u_char*)'

Just a

void (*)(u_char*, const pcap_pkthdr*, const
u_char*)

Which is what the compiler is telling you.


Therefore, you cannot pass a non-static member
function to libpcap. You can make dumper() a 
static member.


--- David Rosal <david.rosal () upf edu> wrote:

Hello.

I'm writing a packet sniffer in C++ using
libpcap-0.9.4.

I've tried to use a class function member as a
callback for 
pcap_loop(), but the compiler complains that
arguments don't 
match. The code is something like this (I have
simplified it):

8<-----------------------------

class X
{
      public:
      // This is the callback, defined below
      void dumper(u_char *u, const struct
pcap_pkthdr *h,
              const u_char *p);
};


void X::dumper(u_char *u, const struct
pcap_pkthdr *h, const
      u_char *p)
{
      // stuff...
      pcap_dump(blah, blah, blah);
}


int main()
{
      X x;
      
      // Open pcap, etc...
      
      pcap_loop(p, -1, x.dumper, 0);
}

----------------------------->8

And g++ says this:

In function `int main()':
sniffer.cc:645: error: argument of type `void
(X::)(u_char*, 
const pcap_pkthdr*, const u_char*)' does not
match `void 
(*)(u_char*, const pcap_pkthdr*, const
u_char*)'

This is not a warning but an error, so I cannot
compile the program.

Is there any trick to fix this? Am I missing
something?

Should I avoid C++ and use C instead (don't say
that please...)


Thanks.


__david
-
This is the tcpdump-workers list.
Visit https://lists.sandelman.ca/ to
unsubscribe.



--ury
-
This is the tcpdump-workers list.
Visit https://lists.sandelman.ca/ to unsubscribe.


Current thread: