Crates.io | netgauze-pcap-reader |
lib.rs | netgauze-pcap-reader |
version | 0.5.0 |
source | src |
created_at | 2023-10-09 17:28:23.26665 |
updated_at | 2024-11-05 16:50:13.898529 |
description | Simple PCAP reader |
homepage | https://github.com/NetGauze/NetGauze |
repository | https://github.com/NetGauze/NetGauze |
max_upload_size | |
id | 998289 |
size | 30,608 |
Helper Reading pcap and pcap files for testing only See parse for a simple code to parse BMP packets from a pcapng file.
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),
}
}
}