savefile/savefile.rs
1extern crate pcap;
2
3use pcap::*;
4
5fn main() {
6 {
7 // open capture from default device
8 let mut cap = Capture::from_device(Device::lookup().unwrap()).unwrap().open().unwrap();
9
10 // open savefile using the capture
11 let mut savefile = cap.savefile("test.pcap").unwrap();
12
13 // get a packet from the interface
14 let p = cap.next().unwrap();
15
16 // print the packet out
17 println!("packet received on network: {:?}", p);
18
19 // write the packet to the savefile
20 savefile.write(&p);
21 }
22
23 // open a new capture from the test.pcap file we wrote to above
24 let mut cap = Capture::from_file("test.pcap").unwrap();
25
26 // get a packet
27 let p = cap.next().unwrap();
28
29 // print that packet out -- it should be the same as the one we printed above
30 println!("packet obtained from file: {:?}", p);
31}