Safe and mostly sane Rust bindings for libpcap and some other network related functionalities.
Libpacp bindings are split in two different crates:
luomu-libpcap-sysfor unsafe Rust bindings generated directly fromlibpcap.luomu-libpcapfor safe and sane libpcap interface.
luomu-libpcap crate is split into two parts itself:
functionsmodule contains safe wrappers and sane return values for libpcap functions.- the root of the project contains
Pcapstruct et al. for more Rusty API to interact with libpcap.
use luomu_libpcap::{Pcap, Result};
fn main() -> Result<()> {
let pcap = Pcap::builder("en0")?
.set_promiscuous(true)?
.set_immediate(true)?
.set_snaplen(65535)?
.set_buffer_size(512 * 1024)?
.activate()?;
pcap.set_filter("udp")?;
for packet in pcap.capture() {
let mut hex = String::new();
for i in 0..packet.len() {
if i % 4 == 0 {
hex.push(' ');
}
if i % 32 == 0 {
hex.push('\n');
}
hex.push_str(&format!("{:02x}", packet[i]));
}
println!("{}", hex);
}
Ok(())
}Other crates, these do not require or use libpcap:
luomu-tpacketv3contains Rust bindings for capturing network traffic on Linux withAF_PACKETsockets and tpcacket_v3luomu-getifaddrsprovides Rust bindings forgetiffaddrs()to get network interface addresses and statistics withluomu-commoncontains utilities for working with IP Addresses, MAC addresses and such.
You probably want to use the Pcap struct and other things from root of this
crate.
See LICENSE. MIT license.