How to use pcap_breakloop?

柔情痞子 提交于 2019-12-01 05:54:56

问题


I have a pcap_loop function in another function, that captures packets until the user stops it, i.e.

void functionA()
{
   signal(SIGINT, terminate_process); 
   pcap_loop(handle, -1, callback, NULL);
   ...

}

void terminate_process(int signum)
{
   pcap_breakloop(handle);
   pcap_close(handle);
} 

Is it possible to set a duration for when packets would be captured? Something like:

if (time(NULL) - start_time > 100)
   pcap_breakloop(handle);

But I don't know where to put this, because so far all the examples I've seen used pcap_breakloop in a signal handler, which requires user intervention. How will the time condition be checked while pcap_loop is running?

Thank you.

Regards, Rayne


回答1:


You can use alarm to generate a signal after a given number of seconds:

void functionA()
{
    signal(SIGALRM, terminate_process); 
    alarm(100);
}


来源:https://stackoverflow.com/questions/2377309/how-to-use-pcap-breakloop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!