PCAP_COMPILE(3PCAP)
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 abpf_programstruct, andpcap_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 libpcap1.8.0and later,pcap_compile()can be used in multiple threads within a single process. However, in earlier versions of libpcap, it is not safe to usepcap_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
- 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