File: savemultiplefiles.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 (31 lines) | stat: -rw-r--r-- 809 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
use pcap::Capture;

fn main() {
    // get the default Device
    let device = pcap::Device::lookup().unwrap().unwrap();

    // Setup Capture
    let mut cap = pcap::Capture::from_device(device)
        .unwrap()
        .immediate_mode(true)
        .open()
        .unwrap();

    // remember linktype to create PCAP files later
    let linktype = cap.get_datalink();

    // For example purposes we will only save 5 files...
    for counter in 0..5 {
        let mut save_file = Capture::dead(linktype)
            .unwrap()
            .savefile(format!("dump_{}.pcap", counter))
            .unwrap();

        // ...30 packets each
        for _ in 0..30 {
            let packet = cap.next_packet().unwrap();
            save_file.write(&packet);
        }
        save_file.flush().unwrap();
    }
}