diff --git a/Cargo.toml b/Cargo.toml index 3b73def..163dfbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ path = "src/bin/main.rs" required-features = ["cli"] [dependencies] +as2org-rs = "0.1.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1" oneio = { version = "0.16.5", default-features = false, features = ["lib-core"] } diff --git a/README.md b/README.md index fc734d1..10a64f5 100644 --- a/README.md +++ b/README.md @@ -81,16 +81,26 @@ println!( `asnames` is a module for Autonomous System (AS) names and country lookup Data source: -- +- RIPE NCC asnames: +- CAIDA as-to-organization mapping: #### Data structure ```rust +use serde::{Deserialize, Serialize}; #[derive(Debug, Clone)] pub struct AsName { pub asn: u32, pub name: String, pub country: String, + pub as2org: Option, +} +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct As2orgInfo { + pub name: String, + pub country: String, + pub org_id: String, + pub org_name: String, } ``` diff --git a/examples/asnames.rs b/examples/asnames.rs index c097dad..c117042 100644 --- a/examples/asnames.rs +++ b/examples/asnames.rs @@ -3,6 +3,10 @@ use std::collections::HashMap; fn main() { let asnames: HashMap = get_asnames().unwrap(); + println!( + "{}", + serde_json::to_string_pretty(asnames.get(&400644).unwrap()).unwrap() + ); assert_eq!( asnames.get(&3333).unwrap().name, "RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC)" diff --git a/src/asnames/mod.rs b/src/asnames/mod.rs index ea26131..dbf6c9c 100644 --- a/src/asnames/mod.rs +++ b/src/asnames/mod.rs @@ -2,16 +2,26 @@ //! //! # Data source //! -//! - +//! - RIPE NCC asnames: +//! - CAIDA as-to-organization mapping: //! //! # Data structure //! //! ```rust +//! use serde::{Deserialize, Serialize}; //! #[derive(Debug, Clone)] //! pub struct AsName { //! pub asn: u32, //! pub name: String, //! pub country: String, +//! pub as2org: Option, +//! } +//! #[derive(Debug, Clone, Serialize, Deserialize)] +//! pub struct As2orgInfo { +//! pub name: String, +//! pub country: String, +//! pub org_id: String, +//! pub org_name: String, //! } //! ``` //! @@ -36,12 +46,22 @@ pub struct AsName { pub asn: u32, pub name: String, pub country: String, + pub as2org: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct As2orgInfo { + pub name: String, + pub country: String, + pub org_id: String, + pub org_name: String, } const DATA_URL: &str = "https://ftp.ripe.net/ripe/asnames/asn.txt"; pub fn get_asnames() -> Result> { let text = oneio::read_to_string(DATA_URL)?; + let as2org = as2org_rs::As2org::new(None)?; let asnames = text .lines() .filter_map(|line| { @@ -53,10 +73,18 @@ pub fn get_asnames() -> Result> { Some((name, country)) => (name, country), None => return None, }; + let asn = asn_str.parse::().unwrap(); + let as2org = as2org.get_as_info(asn).map(|info| As2orgInfo { + name: info.name.clone(), + country: info.country_code.clone(), + org_id: info.org_id.clone(), + org_name: info.org_name.clone(), + }); Some(AsName { - asn: asn_str.parse::().unwrap(), + asn, name: name_str.to_string(), country: country_str.to_string(), + as2org, }) }) .collect::>(); diff --git a/src/lib.rs b/src/lib.rs index f87d126..723ee98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,16 +72,26 @@ //! `asnames` is a module for Autonomous System (AS) names and country lookup //! //! Data source: -//! - +//! - RIPE NCC asnames: +//! - CAIDA as-to-organization mapping: //! //! ### Data structure //! //! ```rust +//! use serde::{Deserialize, Serialize}; //! #[derive(Debug, Clone)] //! pub struct AsName { //! pub asn: u32, //! pub name: String, //! pub country: String, +//! pub as2org: Option, +//! } +//! #[derive(Debug, Clone, Serialize, Deserialize)] +//! pub struct As2orgInfo { +//! pub name: String, +//! pub country: String, +//! pub org_id: String, +//! pub org_name: String, //! } //! ``` //!