Module org.jnetpcap
Package org.jnetpcap

Class BpFilter

java.lang.Object
org.jnetpcap.BpFilter
All Implemented Interfaces:
AutoCloseable

public final class BpFilter extends Object implements AutoCloseable
Berkeley Packet Filter (BPF) program implementation for packet filtering. This class encapsulates a compiled BPF program that can be applied to network packets to determine if they match specific criteria.

Native Structure

Each filter instruction is represented as a 64-bit value that maps to the following C structure from pcap/bpf.h:

 struct bpf_insn {
     u_short     code;     // Operation code
     u_char      jt;       // Jump if true
     u_char      jf;       // Jump if false
     bpf_u_int32 k;       // Generic field
 };
 

Usage Example


 // Create and compile a filter for TCP packets on port 80
 try (BpFilter filter = new BpFilter("tcp port 80")) {
     Pcap pcap = ...;
     pcap.compile(filter, true, 0);
     pcap.setFilter(filter);
 }
 

Memory Management

The class implements AutoCloseable to ensure proper deallocation of native resources. The filter must be explicitly closed when no longer needed to prevent memory leaks.
Author:
Sly Technologies, repos@slytechs.com
See Also: