netgauze-pcap-reader

Crates.ionetgauze-pcap-reader
lib.rsnetgauze-pcap-reader
version0.5.0
sourcesrc
created_at2023-10-09 17:28:23.26665
updated_at2024-11-05 16:50:13.898529
descriptionSimple PCAP reader
homepagehttps://github.com/NetGauze/NetGauze
repositoryhttps://github.com/NetGauze/NetGauze
max_upload_size
id998289
size30,608
Ahmed Elhassany (ahassany)

documentation

README

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),
        }
    }
}
Commit count: 552

cargo fmt