5 releases (1 stable)

1.0.0 Dec 31, 2021
0.3.0 Sep 2, 2017
0.2.2 Aug 18, 2017
0.2.1 Aug 2, 2017
0.2.0 Aug 1, 2017

#1320 in Network programming

Download history 14/week @ 2024-07-22 119/week @ 2024-07-29 31/week @ 2024-08-05 23/week @ 2024-08-12 30/week @ 2024-08-19 37/week @ 2024-08-26 34/week @ 2024-09-02 57/week @ 2024-09-09 68/week @ 2024-09-16 67/week @ 2024-09-23 110/week @ 2024-09-30 56/week @ 2024-10-07 44/week @ 2024-10-14 23/week @ 2024-10-21 13/week @ 2024-10-28 18/week @ 2024-11-04

100 downloads per month
Used in 3 crates

MIT/Apache

36KB
707 lines

RPCAP

An all-Rust library for reading and writing PCAP files.

Full API Documentation

use std::fs::File;
use std::io::{BufReader, BufWriter};
use rpcap::read::PcapReader;
use rpcap::write::{PcapWriter, WriteOptions};

// read a PCAP file
let infile = File::open("example.pcap").unwrap();
let reader = BufReader::new(infile);
let (file_opts, mut pcapr) = PcapReader::new(reader).unwrap();
println!("type of captured packets: {}", file_opts.linktype);
println!("maximum packet size: {}", file_opts.snaplen);

// create a new PCAP file
let outfile = File::create("copy.pcap").unwrap();
let writer = BufWriter::new(outfile);
let mut pcapw = PcapWriter::new(writer, file_opts).unwrap();

// copy all packets from example.pcap to copy.pcap
while let Some(packet) = pcapr.next().unwrap() {
    println!("packet at {:?} with size {} (cropped from {})",
        packet.time, packet.data.len(), packet.orig_len);
    pcapw.write(&packet).unwrap();
}

Please note that there is no support for the newer pcapng file format. If you need that, you might want to have a look at the libpcap-wrapper for rust. The same applies if you need the advanced filtering options it has out of the box. To disect the packets from the pcap file, you could use the pnet library. In the time between me writing and publishing this library, it looks like the pcap-file and pcap-rs libraries have popped up, which seem to be doing a very similar thing as this library.

Options

By default, timestamps are returned as std::time::SystemTime. With the optiona time feature you can opt to get values as time::Timespec type from the time crate (version 1.0) instead:

[dependencies]
rpcap = { version = "1.0.0", features = ["time"] }

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Upgrading from Version 0.3

PcapWriter::append now requires an argument of type Read + Write + Seek. This allows the library to ensure that the output format matches of written packages matches the format specified in the file header. If this does not work for your use case, you can use the new PcapWriter::append_unchecked function. Be sure to only use it with files created with this library on the same platform, or else the files might get corrupted.

PcapReader::new now returns a tuple of information about the file header and a faux-iterator. If you do not need the first item in the tuple, you can do something like let (_, reader) = PcapReader::new().

WriteOptions is now called FileOptions, and has two additional fields. As a result, all possible variants of PCAP (v1) files can be created now. Due to the previous changes, it is now much less likely you'd have to manually create instances of this struct. If you do and are upgrading from an old release, set high_res_timestamps to true and non_native_byte_order to false to get the old behavior back.

Dependencies

~2.5MB
~49K SLoC