feat: add pcap

This commit is contained in:
2020-11-07 11:21:53 +08:00
parent 82bb330b3c
commit 73eddfbd5b
19 changed files with 2160 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
use pcap::*;
fn main() {
{
// open capture from default device
let device = Device::lookup().unwrap();
println!("Using device {}", device.name);
// Setup Capture
let mut cap = Capture::from_device(device)
.unwrap()
.immediate_mode(true)
.open()
.unwrap();
// open savefile using the capture
let mut savefile = cap.savefile("test.pcap").unwrap();
// get a packet from the interface
let p = cap.next().unwrap();
// print the packet out
println!("packet received on network: {:?}", p);
// write the packet to the savefile
savefile.write(&p);
}
// open a new capture from the test.pcap file we wrote to above
let mut cap = Capture::from_file("test.pcap").unwrap();
// get a packet
let p = cap.next().unwrap();
// print that packet out -- it should be the same as the one we printed above
println!("packet obtained from file: {:?}", p);
}