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

bin+lib bgpkit-commons

A library for common BGP-related data and functions

11 unstable releases (5 breaking)

0.6.0 Jun 26, 2024
0.5.2 Mar 21, 2024
0.5.0 Jan 30, 2024
0.4.3 Nov 29, 2023
0.1.0 Sep 3, 2023

#332 in Network programming

Download history 39/week @ 2024-03-27 41/week @ 2024-04-03 5/week @ 2024-04-24 3/week @ 2024-05-01 2/week @ 2024-05-29 5/week @ 2024-06-05 4/week @ 2024-06-12 2/week @ 2024-06-19 162/week @ 2024-06-26 4/week @ 2024-07-03

169 downloads per month
Used in bgpkit-broker

MIT license

74KB
1.5K SLoC

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 sources

Data structure

use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct AsName {
    pub asn: u32,
    pub name: String,
    pub country: String,
    pub population: Option<AsnPopulationData>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct As2orgInfo {
    pub name: String,
    pub country: String,
    pub org_id: String,
    pub org_name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AsnPopulationData {
    pub user_count: i64,
    pub percent_country: f64,
    pub percent_global: f64,
    pub sample_count: i64,
}

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

Bogon utilities

We provide a utility to check if an IP prefix or an ASN is a bogon.

Data sources

IANA special registries:

Usage Examples

use bgpkit_commons::bogons::Bogons;

let bogons = Bogons::new().unwrap();
assert!(bogons.matches_str("10.0.0.0/9"));
assert!(bogons.matches_str("112"));
assert!(bogons.is_bogon_prefix(&"2001::/24".parse().unwrap()));
assert!(bogons.is_bogon_asn(65535));

AS-level relationship

bgpkit-commons provides access to AS-level relationship data generated by BGPKIT.

Data sources

Data Structure

pub enum AsRelationship {
    ProviderCustomer,
    CustomerProvider,
    PeerPeer,
}

pub struct As2relBgpkitData {
    pub rel: AsRelationship,
    pub peers_count: u32,
    pub max_peer_count: u32,
}

Usage Examples

let bgpkit = bgpkit_commons::as2rel::As2relBgpkit::new().unwrap();
let (v4_data, v6_data) = bgpkit.lookup_pair(400644, 54825);
assert!(v4_data.is_none());
assert!(v6_data.is_some());
assert_eq!(v6_data.unwrap().rel, bgpkit_commons::as2rel::AsRelationship::CustomerProvider);

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–24MB
~382K SLoC