PCAP_SETNONBLOCK(3)

๐Ÿ“ฆ version 15.1, in freebsd 14.5, freebsd 15.1

๐Ÿ’… 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