0

In winpcap C++ environmet.

pcap_loop(adhandle, 0, packet_handler, NULL);

packet_handler is a callback function when it receives a packet.

Now my problem is

If I wrote a code like this

while(true)
 {
     pcap_loop(adhandle,0, packet_handler,NULL);
}

Could it happen that the pcap_loop function will be executed twice or more before the resend function being callbacked?

Or the loop will be blocked until the callback function finished to continue?

kururu
  • 69
  • 1
  • 10
  • As for your problem, my understanding is that `pcap_loop` will not return until it's done. So then no, the loop will stop at the `pcap_loop` call, and not continue until `pcap_loop` returns, which means there will only be one `pcap_loop` active at a time. – Some programmer dude Jul 18 '14 at 07:05
  • Sorry,I mistakenly add parentheses to it, it is just a callback function as I said. – kururu Jul 18 '14 at 07:05
  • Because the callback function is just a pointer to a function just like other parameter? – kururu Jul 18 '14 at 07:08
  • It's easy to test though: Add some output before and after the `pcap_loop` call (remember to flush the output to make sure it's written immediately). – Some programmer dude Jul 18 '14 at 07:11

1 Answers1

0

The callback is called synchronously - pcap_loop won't process the next packet until the callback is completed.

Furthermore, pcap_loop (with cnt = 0) itself just runs in its own infinite loop, until you call pcap_breakloop somewhere. It therefore makes little sense to enclose the call to pcap_loop within your own while (true) loop.

The corollary of that is that pcap_loop itself is a blocking synchronous function. Therefore in a Windows UI environment then unless your pcap_loop is contained within its own thread you should consider just calling pcap_next (or pcap_next_ex) within the main application event loop, so as not to block the UI thread.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Thank you.And if `pcap_loop` is an infinite loop, can I use two `pcap_loop` function in one thread? – kururu Jul 18 '14 at 07:18
  • @kururu that wouldn't make sense - they can't both run simultaneously. Wondering why you'd want to... – Alnitak Jul 18 '14 at 07:21
  • I want to transfer data between net cards,like a router software.And each net card will have its own pcat_t handle.May be it is not the best way to do it. – kururu Jul 18 '14 at 07:25
  • @kururu assuming that you're using `libpcap` to obtain raw ethernet frames, ideally you'd use one thread per card. I presume you're going to use some non-pcap-based method to write the frames out on the other card? – Alnitak Jul 18 '14 at 07:28
  • That would be too difficult for me.I just use the `pcap_sendpacket` function to send raw packet to the ethernet.It is just a course design not for actually use , may be I can just use `pcap_next_ex` instead because there won't be much traffic so the efficiency is not really a problem. – kururu Jul 18 '14 at 07:35
  • @kururu ah yes, I forgot about `pcap_sendpacket`. Note that `pcap_next_ex` is also a blocking function, although I believe it does support a timeout. "Simultaneous" reading of two separate interfaces from a single thread would require either a very short time out or non-blocking pcap reads. – Alnitak Jul 18 '14 at 07:44
  • Non-blocking pcap reads are done by calling `pcap_setnonblock()`, with a *nonblock* argument of 1, on the `pcap_t`. However, if you're going to put the `pcap_t` in non-blocking mode, you will probably need to have your code block *somewhere* if you don't want it consuming CPU 100% of the time, so you will need to use `pcap_getevent()` to get a `HANDLE` on which you could do `WaitForSingleObject()`, `WaitForMultipleObjects()`, `MsgWaitForMultipleObjects()`, etc.. –  Jul 18 '14 at 17:49
  • Note that if the wait function you call indicates that you can read from the `HANDLE`, and therefore that you can get packets from the `pcap_t` without blocking, you can call `pcap_dispatch()`, which will loop through the packets that are immediately available, but will not block in that loop if you have set non-blocking mode. –  Jul 18 '14 at 17:56