-
Notifications
You must be signed in to change notification settings - Fork 144
/
savefile.rs
39 lines (30 loc) · 1.09 KB
/
savefile.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use pcap::*;
fn main() {
{
// open capture from default device
let device = Device::lookup()
.expect("device lookup failed")
.expect("no device available");
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_packet().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_packet().unwrap();
// print that packet out -- it should be the same as the one we printed above
println!("packet obtained from file: {:?}", p);
}