| Crates.io | bpf |
| lib.rs | bpf |
| version | 0.1.4 |
| created_at | 2017-01-24 17:00:47.347102+00 |
| updated_at | 2025-05-14 18:23:19.937752+00 |
| description | Attach BPF filters |
| homepage | https://github.com/jedisct1/rust-bpf |
| repository | https://github.com/jedisct1/rust-bpf |
| max_upload_size | |
| id | 8207 |
| size | 23,056 |
A Rust library for attaching Berkeley Packet Filter (BPF) programs to sockets on Linux systems.
Add this to your Cargo.toml:
[dependencies]
bpf = "0.1"
use bpf::{bpfprog, BpfFilterAttachable};
use std::net::UdpSocket;
fn main() -> std::io::Result<()> {
let socket = UdpSocket::bind("127.0.0.1:0")?;
// Create a BPF program that only accepts UDP packets on port 53 (DNS)
// Each tuple is (code, jt, jf, k) for a BPF instruction
let filter = bpfprog!(4,
0x28 0 0 0x0000000c, // (000) ldh [12]
0x15 0 2 0x00000800, // (001) jeq #0x800 jt 2 jf 4
0x30 0 0 0x00000017, // (002) ldb [23]
0x15 0 1 0x00000011, // (003) jeq #0x11 jt 4 jf 5
0x06 0 0 0x00000001 // (004) ret #1
);
// Attach filter to socket
socket.attach_filter(filter)?;
// Lock the filter if needed
socket.lock_filter()?;
// Use the socket...
// Later, detach the filter if needed
socket.detach_filter()?;
Ok(())
}
ISC License
Contributions are welcome! Please feel free to submit a Pull Request.