PCAP_SETNONBLOCK(3PCAP)
β¨ PCAP_SETNONBLOCK β¨
okay bestie so this is a page from the 3PCAP manual and it's literally about flipping your capture handle's non-blocking switch on and off π
like a light switch but for packets, iykyk
πΈ NAME πΈ
pcap_setnonblock, pcap_getnonblock β set or get the state of non-blocking mode on a capture device π
π¦ SYNOPSIS π¦
#include <pcap/pcap.h>
char errbuf[PCAP_ERRBUF_SIZE];
int pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf);
int pcap_getnonblock(pcap_t *p, char *errbuf);
π DESCRIPTION π
so pcap_setnonblock() is the main character here π β it puts a capture handle into "non-blocking" mode, or takes it out of "non-blocking" mode, and honestly? it's all decided by whether the nonblock argument is non-zero or zero. non-zero = slay, we're non-blocking now. zero = okay we're back to blocking. periodt.
one thing though, bestie β it has no effect on "savefiles" π«Ά like, none. savefiles are not part of this conversation.
errbuf is a buffer large enough to hold at least PCAP_ERRBUF_SIZE chars. that's the whole vibe check on errbuf, ngl.
π what non-blocking mode actually means π
in "non-blocking" mode, if you try to read from the capture descriptor with pcap_dispatch(3PCAP) and pcap_next_ex(3PCAP) and there's no packets currently available to be read, it will return 0 immediately rather than blocking and waiting around for packets to arrive. it's giving no patience energy β¨ and honestly? iconic.
but like β the warnings, because there are warnings π«Ά:
pcap_loop(3PCAP)will loop forever, consuming CPU time when no packets are currently available. it just sits there. spinning. hot mess behavior fr fr π so you should usepcap_dispatch()instead.pcap_next(3PCAP)will returnNULLif there are no packets currently available to read. and that? that is indistinguishable from an error π so please, for the love of everything, usepcap_next_ex()instead.
bestie. read those two again.pcap_loop()= CPU go brrr for nothing, andpcap_next()= can't tell the difference between "no packets" and "something went wrong." use the_exversions, please, i'm begging π
π§ when you're non-blocking and when you're not π§
when a capture handle is first activated with pcap_activate(3PCAP) or opened with pcap_open_live(3PCAP), it is not in "non-blocking mode". nope! default is blocking, bestie. you need to actually call pcap_setnonblock() in order to put it into "non-blocking" mode. it doesn't justβ¦ happen. you have to want it π
π₯ RETURN VALUE π₯
okay so here are the numbers, keep them in your notes app π
pcap_setnonblock():
- returns
0on success β - returns
PCAP_ERROR_NOT_ACTIVATEDif it's called on a capture handle that has been created but not activated π« - returns
PCAP_ERRORfor other errors π
pcap_getnonblock():
- returns the current "non-blocking" state of the capture descriptor on success β
- it always returns
0on "savefiles" (yes, always β savefiles stay unbothered) π«Ά - returns
PCAP_ERROR_NOT_ACTIVATEDif it's called on a capture handle that has been created but not activated π« - returns
PCAP_ERRORfor other errors π
and if PCAP_ERROR gets returned, errbuf is filled in with an appropriate error message. so like, check errbuf. it's there for a reason, babe π
fr fr: a handle that's created but not activated is a whole separate situation from a general error β that's why PCAP_ERROR_NOT_ACTIVATED exists as its own thing. don't lump them together π
π SEE ALSO π
pcap(3PCAP)pcap_next_ex(3PCAP)pcap_geterr(3PCAP)
that's the whole page, bestie β you ate that π
with love from the 3PCAP manual fam, dated 30 November 2023 π mwah π
π¦ where this page comes from
- alpine 3.22: libpcap-doc 1.10.7-r0 Β· BSD-3-Clause
- alpine 3.23: libpcap-doc 1.10.7-r0 Β· BSD-3-Clause
- alpine 3.24: libpcap-doc 1.10.7-r0 Β· BSD-3-Clause
- debian trixie: libpcap0.8-dev 1.10.5-2
- fedora 43: libpcap-devel 14:1.10.5-3.fc43 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- fedora 43: libpcap-devel 14:1.10.5-3.fc43 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- fedora 43: libpcap-devel 14:1.10.7-1.fc43 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- fedora 43: libpcap-devel 14:1.10.7-1.fc43 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- fedora 44: libpcap-devel 14:1.10.6-2.fc44 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- fedora 44: libpcap-devel 14:1.10.6-2.fc44 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- fedora 44: libpcap-devel 14:1.10.7-1.fc44 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- fedora 44: libpcap-devel 14:1.10.7-1.fc44 Β· ISC AND BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC
- nixos 26.05: libpcap 1.10.7
- opensuse-leap 16.0: libpcap-devel 1.10.5-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libpcap-devel 1.10.5-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libpcap-devel 1.10.5-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libpcap-devel 1.10.5-160000.5.1 Β· BSD-3-Clause
- ubuntu questing: libpcap0.8-dev 1.10.5-2ubuntu2
- ubuntu resolute: libpcap0.8-dev 1.10.6-1ubuntu1 Β· BSD-3-clause-var3
- ubuntu resolute: libpcap0.8-dev 1.10.6-1ubuntu1.1 Β· BSD-3-clause-var3
- ubuntu stonking: libpcap0.8-dev 1.10.7-1ubuntu1 Β· BSD-3-clause-var3
- Copyright (c) 1994, 1996, 1997 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms