Re: [tcpdump-workers] Problems with libpcap and C++
Ury Segal wrote: 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*)' Therefore, you cannot pass a non-static member function to libpcap. You can make dumper() a static member. Yes. If I make dumper() a static member of class X, then the compiler accepts it. It works even if I use it whithin a concrete instance of X. I mean the following two ways are accepted: X x; pcap_loop(..., x.dumper, ...); pcap_loop(..., X::dumper, ...); Thanks. __david - This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.
Re: [tcpdump-workers] Problems with libpcap and C++
On Wed, Jun 14, 2006 at 04:48:02PM +0200, David Rosal wrote: > 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); > }; To pass a C++ member function as a callback it must be static, or you must have mechanisms to allow you to pass an instantiated object as well. -m -- Mike Kershaw/Dragorn <[EMAIL PROTECTED]> GPG Fingerprint: 3546 89DF 3C9D ED80 3381 A661 D7B2 8822 738B BDB1 "Yes, yes, LORD OF HUMANS! I will rule you ALL with an iron fist! YOU! OBEY THE FIST!" -- Invader Zim pgpRhTYI6Gmep.pgp Description: PGP signature
Re: [tcpdump-workers] Problems with libpcap and C++
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 <[EMAIL PROTECTED]> 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.
Re: [tcpdump-workers] Problems with libpcap and C++
David Rosal wrote: 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): ... Should I avoid C++ and use C instead (don't say that please...) Should you avoid C++ completely? No. Should you avoid writing the callback routine passed to pcap_loop() in C++? No, but you should avoid, as far as I know, making it a class method, because it's not going to be passed a "self" pointer. I'd suggest writing your own capture loop and using pcap_next_ex() instead, if you want to have the packet processing done by a class member. - This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.