Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems interrupting capture with libpcap

Hi I'm doing a sniffer with c++ and libpcap and I would want to stop the capture when I press ctrl+c, this is my code:

void Capture::terminate_process(int s){
  pcap_breakloop(descr);
  pcap_close(descr);
}

void Capture::capturar(){
  signal(SIGINT, terminate_process);
  pcap_loop (descr, -1, mycallback, NULL);
}

At the .h I have declared:

 pcap_t *descr;

I've seen similar solutions for my problem like this: How to use pcap_breakloop? But I can't compile, I get this error:

capture.cpp: 138:35: error: argument of type 'void (Capture ::) (int)' does not match '{aka __sighandler_t void (*) (int)}'

like image 423
user1027524 Avatar asked Aug 26 '26 13:08

user1027524


1 Answers

signal requires a function pointer, you are using a member function pointer. Just declare Capture::terminate_process(int) as static:

class Capture {
public: 
    /* ... */
    static void Capture::terminate_process(int s);
    /* ... */
};

void Capture::terminate_process(int s){
  pcap_breakloop(descr);
  pcap_close(descr);
}
/* ... */
signal(SIGINT, &Capture::terminate_process); 

You will have to make some changes to your code so that you don't depend on instance variables though.

like image 138
mfontanini Avatar answered Aug 29 '26 04:08

mfontanini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!