File: savefile.rs

package info (click to toggle)
rust-pcap 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 584 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,119 bytes parent folder | download
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);
}