6 releases (3 breaking)

new 0.5.0 Nov 5, 2024
0.4.1 Aug 8, 2024
0.3.1 Oct 10, 2024
0.3.0 Jan 31, 2024
0.2.0 Oct 9, 2023

#2074 in Network programming

Download history 15/week @ 2024-07-30 259/week @ 2024-08-06 12/week @ 2024-08-13 3/week @ 2024-08-20 21/week @ 2024-09-10 14/week @ 2024-09-17 27/week @ 2024-09-24 12/week @ 2024-10-01 102/week @ 2024-10-08 8/week @ 2024-10-15

123 downloads per month
Used in 4 crates

Apache-2.0

15KB
233 lines

PCAP and PCAPNG helper library

Helper Reading pcap and pcap files for testing only See parse for a simple code to parse BMP packets from a pcapng file.

Run example

use std::{collections::HashMap, fs::File};

use bytes::BytesMut;
use netgauze_bmp_pkt::codec::BmpCodec;
use netgauze_pcap_reader::{PcapIter, TransportProtocol};
use pcap_parser::PcapNGReader;
use tokio_util::codec::Decoder;

fn main() {
    let mut path = env!("CARGO_MANIFEST_DIR").to_owned();
    path.push_str("/data/bmp.pcapng");
    let file = File::open(path).unwrap();
    let reader = PcapNGReader::new(165536, file).unwrap();
    let reader = Box::new(reader);
    let iter = PcapIter::new(reader);
    let mut peers = HashMap::new();
    for (src_ip, src_port, dst_ip, dst_port, protocol, value) in iter {
        if protocol != TransportProtocol::TCP {
            continue;
        }
        let key = (src_ip, src_port, dst_ip, dst_port);
        let (codec, buf) = peers
            .entry(key)
            .or_insert((BmpCodec::default(), BytesMut::new()));
        buf.extend_from_slice(value.as_slice());
        match codec.decode(buf) {
            Ok(Some(msg)) => println!("{}", serde_json::to_string(&msg).unwrap()),
            Ok(None) => {}
            Err(err) => println!("Error parsing BMP Message: {:?}", err),
        }
    }
}

Dependencies

~1.5MB
~24K SLoC