Skip to content

Instantly share code, notes, and snippets.

@digizeph
Created May 9, 2023 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digizeph/8dcae023eb6ce253ba18c744fbdd2836 to your computer and use it in GitHub Desktop.
Save digizeph/8dcae023eb6ce253ba18c744fbdd2836 to your computer and use it in GitHub Desktop.
Parse a RIB dump file into prefix-to-AS mapping HashMap
fn pfx2as(url: &str) -> Result<HashMap<IpNet, HashSet<u32>>> {
let mut data: HashMap<IpNet, HashSet<u32>> = HashMap::new();
for elem in bgpkit_parser::BgpkitParser::new(url)? {
let prefix = elem.prefix.prefix;
let origins = elem
.origin_asns
.unwrap()
.into_iter()
.map(|v| v.asn)
.collect::<Vec<u32>>();
data.entry(prefix)
.or_insert_with(|| HashSet::new())
.extend(origins);
}
Ok(data)
}
@digizeph
Copy link
Author

digizeph commented May 9, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment