PCAP_COMPILE(3PCAP)

πŸ“¦
version 1.10.7 Β· 4 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, debian trixie, fedora 43, fedora 44, nixos 26.05, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

okay bestie, welcome to the PCAP_COMPILE(3PCAP) page β€” we’re talking about pcap_compile, and it’s giving β€œtiny function, huge filter-compiling main character energy.” πŸ’…βœ¨

πŸŽ€ NAME πŸŽ€

pcap_compile β€” compile a filter expression 🫢

✨ SYNOPSIS ✨

#include <pcap/pcap.h>

int pcap_compile(pcap_t *p, struct bpf_program *fp,
    const char *str, int optimize, bpf_u_int32 netmask);

πŸ’– DESCRIPTION πŸ’–

So pcap_compile() is literally used to compile the string str into a filter program. For the syntax of that string, peek at pcap-filter(7) β€” that’s where the filter-language lore lives. πŸ¦‹

🌸 the cast of parameters 🌸

  • pcap_t *p β€” your pcap handle bestie; it gets passed along so error stuff can use it later.
  • struct bpf_program *fp β€” a pointer to a bpf_program struct, and pcap_compile() fills it in. Not me being obsessed with a struct getting filled in, but here we are. πŸ’€
  • const char *str β€” the filter expression string that gets compiled into a filter program.
  • int optimize β€” controls whether optimization on the resulting code is performed. Yes, it’s a glow-up switch for the generated code. ✨
  • bpf_u_int32 netmask β€” specifies the IPv4 netmask of the network on which packets are being captured. It is used only when checking for IPv4 broadcast addresses in the filter program. Lowkey specific, highkey important. πŸ‘‘

If the netmask of the network on which packets are being captured isn’t known to the program, or if packets are being captured on the Linux "any" pseudo-interface that can capture on more than one network, a value of PCAP_NETMASK_UNKNOWN can be supplied. In that case, tests for IPv4 broadcast addresses will fail to compile, but all other tests in the filter program will be OK. So like… those broadcast tests are not slaying, but the rest of the filter is fine. 🫠

NOTE, bestie: in libpcap 1.8.0 and later, pcap_compile() can be used in multiple threads within a single process. However, in earlier versions of libpcap, it is not safe to use pcap_compile() in multiple threads in a single process without some form of mutual exclusion allowing only one thread to call it at any given time. That’s not a vibe check you want to fail β€” protect your threads. 😭πŸ”₯

🌈 RETURN VALUE 🌈

pcap_compile() returns 0 on success and PCAP_ERROR on failure. If PCAP_ERROR is returned, pcap_geterr(3PCAP) or pcap_perror(3PCAP) may be called with p as an argument to fetch or display the error text. So when it goes wrong, you’re not left guessing in the group chat β€” there’s a way to get the tea. πŸ“

πŸ¦‹ BACKWARD COMPATIBILITY πŸ¦‹

The PCAP_NETMASK_UNKNOWN constant became available in libpcap release 1.1.0. Tiny historical footnote, but we keep the receipts. πŸŽ€

πŸ‘‘ SEE ALSO πŸ‘‘

  • pcap(3PCAP)
  • pcap_setfilter(3PCAP)
  • pcap_freecode(3PCAP)

mwah πŸ’‹ β€” that’s the PCAP_COMPILE(3PCAP) page, dated 22 August 2018. Go compile your filters, bestie, and remember: the vibe is optimized, but the facts are not. 🌟

πŸ“¦ where this page comes from