6 releases (3 breaking)

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

#1970 in Network programming

Download history 16/week @ 2024-08-12 3/week @ 2024-08-19 33/week @ 2024-09-16 28/week @ 2024-09-23 13/week @ 2024-09-30 97/week @ 2024-10-07 13/week @ 2024-10-14 125/week @ 2024-11-04 10/week @ 2024-11-11 14/week @ 2024-11-18 8/week @ 2024-11-25

157 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