bgp-rs 0.3.0

A library for parsing Border Gateway Protocol (BGP) formatted streams.
Documentation

The bgp-rs crate provides functionality to parse BGP-formatted streams.

Examples

Reading a MRT file containing BPG messages

use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use std::io::BufReader;
use mrt_rs::Record;
use mrt_rs::bgp4mp::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 using BufReader for better performance.
    let mut decoder = Decoder::new(BufReader::new(file)).unwrap();

    // Create a new MRTReader with a Cursor such that we can keep track of the position.
    let mut reader = mrt_rs::Reader { stream: decoder };

    // Keep reading MRT (Header, Record) tuples till the end of the file has been reached.
    while let Ok(Some((_, record))) = reader.read() {
        match record {
           Record::BGP4MP(x) => match x {
               BGP4MP::MESSAGE(x) => {
                   let cursor = Cursor::new(x.message);
                   let mut reader = bgp_rs::Reader { stream: cursor };
                   reader.read().unwrap();
               }
               BGP4MP::MESSAGE_AS4(x) => {
                   let cursor = Cursor::new(x.message);
                   let mut reader = bgp_rs::Reader { stream: cursor };

                   // Read BGP (Header, Message) tuples.
                   match reader.read() {
                       Err(x) => println!("Error: {}", x),
                       Ok((_, message)) => println!("{:?}", message),
                   }
               }

               _ => continue,
           },
           _ => continue,
       }
    }
}