#bgp #bgpkit #data-structures #function #collector #collection #country

bin+lib bgpkit-commons

A library for common BGP-related data and functions

10 releases (4 breaking)

0.5.2 Mar 21, 2024
0.5.1 Mar 21, 2024
0.5.0 Jan 30, 2024
0.4.3 Nov 29, 2023
0.1.0 Sep 3, 2023

#352 in Network programming

Download history 24/week @ 2023-12-18 7/week @ 2024-01-29 10/week @ 2024-02-19 21/week @ 2024-02-26 8/week @ 2024-03-11 252/week @ 2024-03-18 205/week @ 2024-04-01

465 downloads per month

MIT license

48KB
799 lines

BGPKIT Commons

This readme is generated from the library's doc comments using cargo-readme. Please refer to the Rust docs website for the full documentation

Crates.io Docs.rs License Discord

Overview

BGPKIT-Commons is a library for common BGP-related data and functions.

Categories

MRT collectors

This crate provides three functions to retrieve the full list of MRT collectors from RouteViews and RIPE RIS:

  • get_routeviews_collectors()
  • get_riperis_collectors()
  • get_all_collectors()

Data structure

The collectors are abstract to the following struct:

use chrono::NaiveDateTime;
use bgpkit_commons::collectors::MrtCollectorProject;
 /// MRT collector meta information
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MrtCollector {
    /// name of the collector
    pub name: String,
    /// collector project
    pub project: MrtCollectorProject,
    /// MRT data files root URL
    pub data_url: String,
    /// collector activation timestamp
    pub activated_on: NaiveDateTime,
    /// collector deactivation timestamp (None for active collectors)
    pub deactivated_on: Option<NaiveDateTime>,
    /// country where the collect runs in
    pub country: String,
}

where MrtCollectorProject is defined as:

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum MrtCollectorProject {
    RouteViews,
    RipeRis,
}

Usage example

See the following example for usage:

use bgpkit_commons::collectors::get_routeviews_collectors;

println!("get route views collectors");
let mut routeviews_collectors = get_routeviews_collectors().unwrap();
routeviews_collectors.sort();
let earliest = routeviews_collectors.first().unwrap();
let latest = routeviews_collectors.last().unwrap();
println!("\t total of {} collectors", routeviews_collectors.len());
println!(
    "\t earliest collector: {} (activated on {})",
    earliest.name, earliest.activated_on
);
println!(
    "\t latest collector: {} (activated on {})",
    latest.name, latest.activated_on
);

AS name and country

asnames is a module for Autonomous System (AS) names and country lookup

Data source:

Data structure

#[derive(Debug, Clone)]
pub struct AsName {
    pub asn: u32,
    pub name: String,
    pub country: String,
}

Usage example

use std::collections::HashMap;
use bgpkit_commons::asnames::{AsName, get_asnames};

let asnames: HashMap<u32, AsName> = get_asnames().unwrap();
assert_eq!(asnames.get(&3333).unwrap().name, "RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC)");
assert_eq!(asnames.get(&400644).unwrap().name, "BGPKIT-LLC");
assert_eq!(asnames.get(&400644).unwrap().country, "US");

Countries detailed information

Data Structure

pub struct Country {
    /// 2-letter country code
    pub code: String,
    /// 3-letter country code
    pub code3: String,
    /// Country name
    pub name: String,
    /// Capital city
    pub capital: String,
    /// Continent
    pub continent: String,
    /// Country's top-level domain
    pub ltd: Option<String>,
    /// Neighboring countries in 2-letter country code
    pub neighbors: Vec<String>,
}

Usage Examples

use bgpkit_commons::countries::Countries;

let countries = Countries::new().unwrap();
assert_eq!(
    countries.lookup_by_code("US").unwrap().name,
    "United States"
);
assert_eq!(countries.lookup_by_name("united states").len(), 2);
assert_eq!(countries.lookup_by_name("united kingdom").len(), 1);

RPKI utilities

Data sources

Usage Examples

Check current RPKI validation using Cloudflare RPKI portal
use std::str::FromStr;
use ipnet::IpNet;
use bgpkit_commons::rpki::{RpkiTrie, RpkiValidation};

let trie = RpkiTrie::from_cloudflare().unwrap();
let prefix = IpNet::from_str("1.1.1.0/24").unwrap();
assert_eq!(trie.validate(&prefix, 13335), RpkiValidation::Valid);
Check RPKI validation for a given date
use std::str::FromStr;
use chrono::NaiveDate;
use ipnet::IpNet;
use bgpkit_commons::rpki::{RpkiTrie, RpkiValidation};

let rpki = RpkiTrie::from_ripe_historical(NaiveDate::from_ymd_opt(2023, 1, 1).unwrap()).unwrap();
// let rpki = RpkiTrie::from_rpkiviews_historical(NaiveDate::from_ymd_opt(2023, 1, 1).unwrap()).unwrap();
let prefix = IpNet::from_str("1.1.1.0/24").unwrap();
assert_eq!(rpki.validate(&prefix, 13335), RpkiValidation::Valid);

Feature Flags

  • rustls: use rustls instead of native-tls for the underlying HTTPS requests

Commandline tool

This crate also provides a commandline tool bgpkit-commons for easy access to the data and utilities.

Installation

On macOS:

brew install bgpkit/tap/bgpkit-commons

On other platforms:

cargo binstall bgpkit-commons

Export all data to JSON

bgpkit-commons export --help
Export data to local files

 Usage: bgpkit-commons export [OPTIONS]

 Options:
   -o, --output-dir <OUTPUT_DIR>  output directory [default: .]
   -h, --help                     Print help
   -V, --version                  Print version

Built with ❤️ by BGPKIT Team

https://bgpkit.com/favicon.ico

License

MIT

Dependencies

~7–25MB
~390K SLoC