[][src]Crate bgp_rs

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();
            }
        }
    }
}

Re-exports

pub use crate::open::*;
pub use crate::notification::*;
pub use crate::update::*;

Modules

notification

Contains the NOTIFICATION Message implementation

open

Contains the OPEN Message implementation The open mod provides structs and implementation for OPEN messages

update

Contains the UPDATE Message implementation

Structs

Header

Represents the BGP header accompanying every BGP message.

Reader

The BGPReader can read BGP messages from a BGP-formatted stream.

RouteRefresh

Represents a BGP Route Refresh message.

Enums

AFI

Represents an Address Family Identifier. Currently only IPv4 and IPv6 are supported. Currently only IPv4, IPv6, and L2VPN are supported.

Message

Represents a single BGP message.

SAFI

Represents an Subsequent Address Family Identifier. Currently only Unicast and Multicast are supported.

Traits

CapabilitiesRef

An abstract way of getting a reference to a Capabilities struct. This is used in Reader to allow use of either an owned Capabilites or a reference to one.