#pcap #read-write #pcap-parser #pcapng #read #write #parse

pcap-file-tokio

A crate to parse, read and write Pcap and PcapNg asynchronously with Tokio

1 unstable release

0.1.0 Jun 16, 2023

#2447 in Parser implementations

Download history 1767/week @ 2024-01-06 1188/week @ 2024-01-13 1571/week @ 2024-01-20 1609/week @ 2024-01-27 1474/week @ 2024-02-03 1369/week @ 2024-02-10 1032/week @ 2024-02-17 1686/week @ 2024-02-24 1861/week @ 2024-03-02 1546/week @ 2024-03-09 1824/week @ 2024-03-16 1672/week @ 2024-03-23 1692/week @ 2024-03-30 1697/week @ 2024-04-06 2736/week @ 2024-04-13 2275/week @ 2024-04-20

8,501 downloads per month

MIT license

130KB
2K SLoC

pcap-file-tokio

Fork of the awesome pcap-file crate, modified to support tokio.

Provides parsers, readers and writers for Pcap and PcapNg files.

For Pcap files see the pcap module.

For PcapNg files see the pcapng module.

Crates.io rustdoc Crates.io

Documentation

https://docs.rs/pcap-file-tokio

Installation

This crate is on crates.io. Add it to your Cargo.toml:

[dependencies]
pcap-file-tokio = "2.0.0-rc1"

Examples

PcapReader

use tokio::fs::File;
use pcap_file_tokio::pcap::PcapReader;

#[tokio::main]
async fn main() {
    let file_in = File::open("test.pcap").await.expect("Error opening file");
    let mut pcap_reader = PcapReader::new(file_in).await.unwrap();

    // Read test.pcap
    while let Some(pkt) = pcap_reader.next_packet().await {
        //Check if there is no error
        let pkt = pkt.unwrap();

        //Do something
    }
}

PcapNgReader

use tokio::fs::File;
use pcap_file_tokio::pcapng::PcapNgReader;

#[tokio::main]
async fn main() {
    let file_in = File::open("test.pcapng").await.expect("Error opening file");
    let mut pcapng_reader = PcapNgReader::new(file_in).await.unwrap();

    // Read test.pcapng
    while let Some(block) = pcapng_reader.next_block().await {
        // Check if there is no error
        let block = block.unwrap();

        //  Do something
    }
}

Fuzzing

Currently there are 4 crude harnesses to check that the parser won't panic in any situation. To start fuzzing you must install cargo-fuzz with the command:

$ cargo install cargo-fuzz

And then, in the root of the repository, you can run the harnesses as:

$ cargo fuzz run pcap_reader
$ cargo fuzz run pcap_ng_reader
$ cargo fuzz run pcap_parser
$ cargo fuzz run pcap_ng_parser

Keep in mind that libfuzzer by default uses only one core, so you can either run all the harnesses in different terminals, or you can pass the -jobs and -workers attributes. More info can be found in its documentation here. To get better crash reports add to you rust flags: -Zsanitizer=address. E.g.

RUSTFLAGS="-Zsanitizer=address" cargo fuzz run pcap_reader

License

Licensed under MIT.

Disclaimer

To test the library I used the excellent PcapNg testing suite provided by hadrielk.

Dependencies

~2.8–4.5MB
~74K SLoC