Skip to content

Commit

Permalink
Merge pull request #13 from bgpkit/feature/add-org-info
Browse files Browse the repository at this point in the history
Add as2org info to asnames
  • Loading branch information
digizeph authored Jun 24, 2024
2 parents f2e5b92 + 584305a commit 4e6a6d1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,26 @@ println!(
`asnames` is a module for Autonomous System (AS) names and country lookup

Data source:
- <https://ftp.ripe.net/ripe/asnames/asn.txt>
- RIPE NCC asnames: <https://ftp.ripe.net/ripe/asnames/asn.txt>
- CAIDA as-to-organization mapping: <https://www.caida.org/catalog/datasets/as-organizations/>

#### 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<As2orgInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct As2orgInfo {
pub name: String,
pub country: String,
pub org_id: String,
pub org_name: String,
}
```

Expand Down
4 changes: 4 additions & 0 deletions examples/asnames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use std::collections::HashMap;

fn main() {
let asnames: HashMap<u32, AsName> = 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)"
Expand Down
32 changes: 30 additions & 2 deletions src/asnames/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
//!
//! # Data source
//!
//! - <https://ftp.ripe.net/ripe/asnames/asn.txt>
//! - RIPE NCC asnames: <https://ftp.ripe.net/ripe/asnames/asn.txt>
//! - CAIDA as-to-organization mapping: <https://www.caida.org/catalog/datasets/as-organizations/>
//!
//! # 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<As2orgInfo>,
//! }
//! #[derive(Debug, Clone, Serialize, Deserialize)]
//! pub struct As2orgInfo {
//! pub name: String,
//! pub country: String,
//! pub org_id: String,
//! pub org_name: String,
//! }
//! ```
//!
Expand All @@ -36,12 +46,22 @@ pub struct AsName {
pub asn: u32,
pub name: String,
pub country: String,
pub as2org: Option<As2orgInfo>,
}

#[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<HashMap<u32, AsName>> {
let text = oneio::read_to_string(DATA_URL)?;
let as2org = as2org_rs::As2org::new(None)?;
let asnames = text
.lines()
.filter_map(|line| {
Expand All @@ -53,10 +73,18 @@ pub fn get_asnames() -> Result<HashMap<u32, AsName>> {
Some((name, country)) => (name, country),
None => return None,
};
let asn = asn_str.parse::<u32>().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::<u32>().unwrap(),
asn,
name: name_str.to_string(),
country: country_str.to_string(),
as2org,
})
})
.collect::<Vec<AsName>>();
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,26 @@
//! `asnames` is a module for Autonomous System (AS) names and country lookup
//!
//! Data source:
//! - <https://ftp.ripe.net/ripe/asnames/asn.txt>
//! - RIPE NCC asnames: <https://ftp.ripe.net/ripe/asnames/asn.txt>
//! - CAIDA as-to-organization mapping: <https://www.caida.org/catalog/datasets/as-organizations/>
//!
//! ### 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<As2orgInfo>,
//! }
//! #[derive(Debug, Clone, Serialize, Deserialize)]
//! pub struct As2orgInfo {
//! pub name: String,
//! pub country: String,
//! pub org_id: String,
//! pub org_name: String,
//! }
//! ```
//!
Expand Down

0 comments on commit 4e6a6d1

Please sign in to comment.