bgp-rs 0.6.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 BPG4MP messages

use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use std::io::BufReader;
use libflate::gzip::Decoder;
use bgp_rs::{Identifier, PathAttribute};
use mrt_rs::Record;
use mrt_rs::bgp4mp::BGP4MP;

// Download an update message.
let file = File::open("res/mrt/updates.20190101.0000.gz").unwrap();

// Decode the GZIP stream.
let mut decoder = Decoder::new(BufReader::new(file)).unwrap();

// Keep reading MRT (Header, Record) tuples till the end of the file has been reached.
while let Ok(Some((_, record))) = mrt_rs::read(&mut decoder) {

    // Extract BGP4MP::MESSAGE_AS4 entries.
    if let Record::BGP4MP(BGP4MP::MESSAGE_AS4(x)) = record {

        // Read each BGP (Header, Message)
        let cursor = Cursor::new(x.message);
        let mut reader = bgp_rs::Reader::new(cursor);
        let (_, message) = reader.read().unwrap();

        // If this is an UPDATE message that contains announcements, extract its origin.
        if let bgp_rs::Message::Update(x) = message {
            if x.is_announcement() {
                if let PathAttribute::AS_PATH(path) = x.get(Identifier::AS_PATH).unwrap()
                {
                    // Test the path.origin() method.
                    let origin = path.origin();

                    // Do other stuff ...
                }
            }
        }
    }
}

Reading a MRT file containing TABLE_DUMP_V2 messages

use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use std::io::BufReader;
use libflate::gzip::Decoder;
use bgp_rs::{Identifier, PathAttribute, Capabilities};
use mrt_rs::records::tabledump::TABLE_DUMP_V2;
use mrt_rs::Record;
use mrt_rs::bgp4mp::BGP4MP;

// Download an update message.
let file = File::open("res/mrt/bview.20100101.0759.gz").unwrap();

// Decode the GZIP stream.
let mut decoder = Decoder::new(BufReader::new(file)).unwrap();

// Keep reading MRT (Header, Record) tuples till the end of the file has been reached.
while let Ok(Some((_, record))) = mrt_rs::read(&mut decoder) {

    // Extract TABLE_DUMP_V2::RIB_IPV4_UNICAST entries.
    if let Record::TABLE_DUMP_V2(TABLE_DUMP_V2::RIB_IPV4_UNICAST(x)) = record {

        // Loop over each route for this particular prefix.
        for mut entry in x.entries {
            let length = entry.attributes.len() as u64;
            let mut cursor = Cursor::new(entry.attributes);

            // Parse each PathAttribute in each route.
            while cursor.position() < length {
                PathAttribute::parse(&mut cursor, &Default::default()).unwrap();
            }
        }
    }
}