java.lang.Object
org.jnetpcap.BpFilter
- All Implemented Interfaces:
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:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Deallocates the native BPF program and associated resources.static void
Deallocates a native BPF program.int
length()
Returns the number of instructions in this BPF program.toString()
Returns the original filter expression string used to create this filter.
-
Method Details
-
freeCode
Deallocates a native BPF program. This is a convenience method equivalent to callingclose()
on the filter.- Parameters:
bpFilter
- The filter to deallocate- Throws:
IllegalStateException
- if the filter has already been closed- Since:
- libpcap 0.6
- See Also:
-
close
Deallocates the native BPF program and associated resources. After calling this method, the filter can no longer be used.- Specified by:
close
in interfaceAutoCloseable
- Throws:
IllegalStateException
- if the filter has already been closed- See Also:
-
length
public int length()Returns the number of instructions in this BPF program. Each instruction is a 64-bit value containing operation code and parameters.- Returns:
- The number of BPF instructions in this filter
-
toString
-