PCAP_SETNONBLOCK(3)
๐ pcap_setnonblock / pcap_getnonblock ๐
okay bestie, this is the PCAP_SETNONBLOCK(3) page from the Library Functions Manual, and itโs literally all about flipping non-blocking mode on and off for a capture device. no cap, itโs giving very niche but very useful. ๐ฆ
โจ 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 ๐
pcap_setnonblock() puts a capture handle into โnon-blockingโ mode, or takes it out of โnon-blockingโ mode, depending on whether the nonblock argument is non-zero or zero. It has no effect on โsavefilesโ. ๐ซถ
errbuf is a buffer large enough to hold at least PCAP_ERRBUF_SIZE chars. yes, bestie, make sure itโs big enough โ weโre not trying to be messy. ๐
In โnon-blockingโ mode, an attempt to read from the capture descriptor with pcap_dispatch(3) and pcap_next_ex(3) will, if no packets are currently available to be read, return 0 immediately rather than blocking waiting for packets to arrive. โจ
heads up:pcap_loop(3)will loop forever, consuming CPU time when no packets are currently available;pcap_dispatch()should be used instead. not it spinning like a fidget spinner ๐ญ
pcap_next(3) will return NULL if there are no packets currently available to read; this is indistinguishable from an error, so pcap_next_ex() should be used instead. fr fr, donโt get caught out by that one. ๐
When first activated with pcap_activate(3) or opened with pcap_open_live(3), a capture handle is not in โnon-blocking modeโ; a call to pcap_setnonblock() is required in order to put it into โnon-blockingโ mode. so yeah, it starts off blocking โ you gotta do the little mode switch yourself. ๐ฆ
๐ RETURN VALUE ๐
pcap_setnonblock() returns 0 on success, PCAP_ERROR_NOT_ACTIVATED if called on a capture handle that has been created but not activated, and PCAP_ERROR for other errors. ๐
pcap_getnonblock() returns the current โnon-blockingโ state of the capture descriptor on success; it always returns 0 on โsavefilesโ. It returns PCAP_ERROR_NOT_ACTIVATED if called on a capture handle that has been created but not activated, and PCAP_ERROR for other errors. ๐
If PCAP_ERROR is returned, errbuf is filled in with an appropriate error message. so check the errbuf, bestie, itโs literally there to help. ๐ซถ
๐ฅ SEE ALSO ๐ฅ
xoxo, your fav manpage โ from the Library Functions Manual, dated 30 November 2023 ๐๐โจ
๐ฆ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- 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