feat: add pcap
This commit is contained in:
37
__network/pcap/examples/savefile.rs
Normal file
37
__network/pcap/examples/savefile.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user