-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is pcap_next ignoring timeout? #572
Comments
If this is a modern Linux, then the usual cause of this difference between old and new libpcap is that new libpcap uses TPACKET_V3 if it is available. This results in less overhead work but involves deeper buffers. A side effect of this is that the timeout may mean the amount of time after the first packet, not before. Please see "packet buffer timeout" in this man page for explanation. If you need to receive packets without the delay, pcap_set_immediate_mode() is the function, you can see how it works in the tcpdump source code. Let us know if this resolves your issue. |
I ran into the same problem. Replacing |
It's the
To quote the pcap(3) man page:
There has never been a guarantee that |
Hi, i ran into the same problem. Replacing
Hi, i got the same problem and also tried your combination, but that didn't solve it. May you or someone else post the code how you create that pointer? Maybe some of my overloads are not correct. Cheers Manu |
If you set immediate mode, packets are delivered, well, immediately - no timeout, no delay, nothing. This also means that you be more likely to drop packets. |
As I noted, there is no documented guarantee that the timer will go off if no packets have arrived, so not a bug. |
@guyharris So what's the use of |
@beinhaerter i got that timeout problem solved now with using other functions than open_live. In my case it worked with pcap_create, pcap_set_snaplen, pcap_set_promisc, pcap_set_timeout, pcap_set_immediate_mode, pcap_setnonblocking and pcap_activate. @guyharris i'm not your opinion. If open_live provides a timeoutfunction, then it should work. If it leads into killing the whole program, because there is no return, then it is a bug. |
They are respected. They just don't do what you are expecting them to do. You're expecting the timeout to occur whether or not there are packets available. That's not how the timeout in Linux TPACKET_V3 works. The main purpose of timeouts in packet capture mechanisms is to allow the capture mechanism to buffer up multiple packets, and deliver multiple packets in a single wakeup, rather than one wakeup per packet, reducing the number of wakeups (which aren't free), without causing indefinitely-long waits for a packet to be delivered. If packets are delivered only when the packet buffer is full, then, if the buffer is large enough to hold more than one packet, the amount of time between the arrival of a packet and its delivery to the capturing program is unbounded above - the first packet arrives, but it doesn't completely fill the buffer, and it won't be delivered until the buffer is full, so it won't be delivered until another packet arrives, and there's no guarantee, in all cases, that this will happen within any given period of time. So a timeout was added - if the buffer isn't full, it'll be delivered when the timeout expires. Whether the buffer is delivered if it's empty is not guaranteed. Some packet capture mechanisms, such as the BPF packet capture mechanism used in *BSD, macOS, AIX, and Solaris 11, and the mechanisms in the WinPcap and Npcap drivers on Windows, will deliver an empty buffer. Other packet capture mechanisms, such as (as I remember from when I last checked) DLPI+bufmod mechanism in Solaris 10 and earlier, the non-memory-mapped mechanism in Linux (which was originally the only mechanism in Linux), and the TPACKET_V3 capture mechanism in Linux, don't deliver empty buffers. So, given that libpcap runs atop many packet capture mechanisms, it makes no guarantee that the timeout is a timeout that puts an upper bound on how long
Why is your program running into trouble if no packet arrives? Presumably that's because it's doing something other than processing captured packets, and needs to do that every so often. If so, that's no different from, for example, a program that's reading a stream of data from a TCP socket and also doing something other than processing that data; those programs would typically either use a mechanism such as
It can be used in a safe manner by assuming that |
In non-blocking mode,
It does work, as per the libpcap man page I quoted above. It just doesn't work the way you expect it to work; see the "NOTE" section in the quoted man page.
It's a bug in the program, because it's assuming behavior not guaranteed by the library. |
@guyharris Your latest comment contradict with the man pages,
This means that
Could you clarify the discrepancy here? |
I should clarify what "will not work" means in the To quote the
If you use It will also return NULL if an error occurred. And there's no way to determine which was the case. I might be tempted simply to say " (And for |
Done in 7b8c17a. |
For posterity, there is now a FAQ entry about this. |
pcap_t *p = pcap_open_live("eth0", 65535, 1, 500. errbuf);
u_char *buf = pcap_next(p, pkt_hdr);
in libpcap 1.7.4, if after 500 ms no packet is captured it's return NULL, in libpcap 1.8.1 pcap_next still waiting next packet.
The text was updated successfully, but these errors were encountered: