Coder Social home page Coder Social logo

zettabgp's Introduction

zettabgp - BGP&BMP Rust library

This is a BGP and BMP protocols driver library for Rust.

BGP - Border Gateway Protocol version 4. BMP - BGP Monitoring Protocol version 3.

Supported BGP message types

  • Open
  • Notification
  • Keepalive
  • Update

Supported BMP message types

  • Initiation
  • Termination
  • PeerUpNotification
  • RouteMonitoring

Supported address families NLRI (network layer reachability information)

  • ipv4 unicast
  • ipv4 labeled-unicast
  • ipv4 multicast
  • ipv4 mvpn
  • ipv4 mdt
  • vpnv4 unicast
  • vpnv4 multicast
  • ipv6 unicast
  • ipv6 labeled-unicast
  • ipv6 multicast
  • ipv6 mdt
  • vpnv6 unicast
  • vpnv6 multicast
  • vpls
  • evpn
  • flowspec ipv4
  • flowspec ipv6

Supported path attributes

  • MED
  • Origin
  • Local preference
  • AS path
  • Communities
  • Extended communities
  • Aggregator AS
  • Atomic aggregate
  • Cluster list
  • Originator ID
  • Attribute set
  • Connector
  • some PMSI tunnels

Usage

Library allow you to parse protocol messages (as binary buffers) into Rust data structures to frther processing. Or generate valid protocol messages from Rust data structure. So it can be use in any environment (synrchronous or asynchronous) to make a BGP RR, monitoring system or BGP analytics.

use zettabgp::prelude::*;
use std::io::{Read,Write};
let mut socket = match std::net::TcpStream::connect("127.0.0.1:179") {
  Ok(sck) => sck,
  Err(e) => {eprintln!("Unable to connect to BGP neighbor: {}",e);return;}
};
let params=BgpSessionParams::new(64512,180,BgpTransportMode::IPv4,std::net::Ipv4Addr::new(1,1,1,1),vec![BgpCapability::SafiIPv4u].into_iter().collect());
let mut buf = [0 as u8; 32768];
let mut open_my = params.open_message();
let open_sz = open_my.encode_to(&params, &mut buf[19..]).unwrap();
let tosend = params.prepare_message_buf(&mut buf, BgpMessageType::Open, open_sz).unwrap();
socket.write_all(&buf[0..tosend]).unwrap();//send my open message
socket.read_exact(&mut buf[0..19]).unwrap();//read response message head
let messagehead=params.decode_message_head(&buf).unwrap();//decode message head
if messagehead.0 == BgpMessageType::Open {
   socket.read_exact(&mut buf[0..messagehead.1]).unwrap();//read message body
   let mut bom = BgpOpenMessage::new();
   bom.decode_from(&params, &buf[0..messagehead.1]).unwrap();//decode received message body
   eprintln!("BGP Open message received: {:?}", bom);
}

Crates.io

https://crates.io/crates/zettabgp

Documentation

https://docs.rs/zettabgp

License

MIT OR Apache-2.0

zettabgp's People

Contributors

eugskim avatar tuetuopay avatar wladwm avatar yu-re-ka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

zettabgp's Issues

panic in BmpMessage::decode_from

thread 'main' panicked at 'range end index 3 out of range for slice of length 1', /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/afi/ipv6.rs:158:40
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::slice::index::slice_end_index_len_fail_rt
   3: core::slice::index::slice_end_index_len_fail
   4: <core::ops::range::Range<usize> as core::slice::index::SliceIndex<[T]>>::index
             at /build/rustc-1.65.0-src/library/core/src/slice/index.rs:316:13
   5: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /build/rustc-1.65.0-src/library/core/src/slice/index.rs:18:9
   6: zettabgp::afi::ipv6::BgpAddrV6::from_bits
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/afi/ipv6.rs:158:40
   7: <zettabgp::afi::ipv6::BgpAddrV6 as zettabgp::afi::BgpItem<zettabgp::afi::ipv6::BgpAddrV6>>::extract_bits_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/afi/ipv6.rs:198:9
   8: zettabgp::afi::decode_bgpitem_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/afi/mod.rs:198:13
   9: zettabgp::afi::decode_bgpitems_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/afi/mod.rs:205:20
  10: zettabgp::afi::BgpAddrs::decode_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/afi/mod.rs:933:37
  11: zettabgp::message::attributes::multiproto::BgpMPUpdates::decode_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/message/attributes/multiproto.rs:143:18
  12: zettabgp::message::attributes::BgpAttrItem::decode_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/message/attributes/mod.rs:115:45
  13: <zettabgp::message::update::BgpUpdateMessage as zettabgp::BgpMessage>::decode_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/message/update/mod.rs:162:29
  14: zettabgp::bmp::msgrmon::BmpMessageRouteMonitoring::decode_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/bmp/msgrmon.rs:41:9
  15: zettabgp::bmp::BmpMessage::decode_from
             at /home/yuka/.cargo/registry/src/github.com-1ecc6299db9ec823/zettabgp-0.3.0/src/bmp/mod.rs:75:17
  16: fernglas::bmp_collector::run::{{closure}}::{{closure}}
             at ./src/bmp_collector.rs:61:33

The input:

00000000  03 00 00 00 8d 00 00 80  00 00 00 00 00 00 00 00  |................|
00000010  2a 0e b9 40 00 00 00 02  00 00 00 00 00 00 00 20  |*..@........... |
00000020  00 03 2e 0b 2d 8b 8a 14  63 bf ee a1 00 00 2a ff  |....-...c.....*.|
00000030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff 00  |................|
00000040  5d 02 00 00 00 46 40 01  01 00 40 02 0a 02 02 00  |]....F@...@.....|
00000050  00 fd e9 00 00 fd e9 40  05 04 00 00 00 64 90 0e  |[email protected]..|
00000060  00 2a 00 02 01 10 2a 0e  b9 40 00 00 00 02 00 00  |.*....*..@......|
00000070  00 00 00 00 00 12 00 00  00 00 07 80 2a 0e b9 40  |............*..@|
00000080  00 00 00 00 00 00 00 00  00 00 13 35              |...........5|
0000008c

proposed patch:

diff --git a/src/afi/ipv6.rs b/src/afi/ipv6.rs
index e7fec7c..374e5c6 100644
--- a/src/afi/ipv6.rs
+++ b/src/afi/ipv6.rs
@@ -138,7 +138,8 @@ impl BgpAddrV6 {
         self.addr.octets()[0] == 255
     }
     pub fn from_bits(bits: u8, buf: &[u8]) -> Result<(BgpAddrV6, usize), BgpError> {
-        if bits > 128 {
+        let bytes = ((bits + 7) / 8) as usize;
+        if bits > 128 || buf.len() < bytes {
             return Err(BgpError::from_string(format!(
                 "Invalid FEC length: {:?}",
                 bits
@@ -154,7 +155,6 @@ impl BgpAddrV6 {
                 0,
             ));
         }
-        let bytes = ((bits + 7) / 8) as usize;
         bf[0..bytes].clone_from_slice(&buf[0..bytes]);
         Ok((
             BgpAddrV6 {

Feature request: use log crate

Currently there are many println or eprintln statements in the code, which can be confusing to users of an application using the zettabgp library.
It would be wonderful if these could be switched to log::info! and log::warn! statements, which can be filtered by the application.
If you like this idea, I could prepare a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.