The mrt-rs
crate provides functionality to parse an MRT-formatted streams.
Examples
Reading a MRT file containing BPG messages
use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use mrt_rs::MRTReader;
use mrt_rs::MRTRecord;
use mrt_rs::BGP4MP;
use libflate::gzip::Decoder;
fn main() {
// Open an MRT-formatted file.
let file = File::open("res/updates.20190101.0000.gz").unwrap();
// Decode the GZIP stream
let mut decoder = Decoder::new(file).unwrap();
// Read the entire contents of the File in a buffer.
let mut buffer: Vec<u8> = vec![];
decoder.read_to_end(&mut buffer).unwrap();
let length = buffer.len() as u64;
// Create a new MRTReader with a Cursor such that we can keep track of the position.
let mut reader = MRTReader {
stream: Cursor::new(buffer),
};
// Keep reading entries till the end of the file has been reached.
while reader.stream.position() < length {
let result = reader.read();
match &result.unwrap() {
MRTRecord::BGP4MP(x) => match x {
BGP4MP::MESSAGE(y) => println!("{:?}", y),
BGP4MP::MESSAGE_AS4(y) => println!("{:?}", y),
_ => continue,
},
_ => continue,
}
}
}