Expand description
asn-db is a Rust library that can load and index ASN database (ip2asn-v4.tsv file) from IPtoASN website.
Once loaded it can be used to lookup an IP address for matching ASN record that contains:
- network base IP address and mask (e.g. ipnet::Ipv4Net value like
1.1.1.0/24), - assigned AS number (e.g.
13335), - owner country code (e.g.
US), - owner information (e.g.
CLOUDFLARENET - Cloudflare, Inc.).
§Example
Load database from ip2asn-v4.tsv file and lookup 1.1.1.1 IP address.
use asn_db::Db;
use std::fs::File;
use std::io::BufReader;
let db = Db::form_tsv(BufReader::new(File::open("ip2asn-v4.tsv").unwrap())).unwrap();
let record = db.lookup("1.1.1.1".parse().unwrap()).unwrap();
println!("{:#?}", record);
println!("{:#?}", record.network());This prints:
Record {
ip: 16843008,
prefix_len: 24,
as_number: 13335,
country: "US",
owner: "CLOUDFLARENET - Cloudflare, Inc."
}
1.1.1.0/24§Usage
Use Db::from_tsv(input) to load database from ip2asn-v4.tsv data.
You can then use db.store(output) to store the binary encoded data index for fast loading with Db::load(input).
Use db.lookup(ip) to lookup for matching record by an IP address.
Structs§
- Db
- ASN record database that is optimized for lookup by an IP address.
- Ipv4
Addr - An IPv4 address.
- Ipv4Net
- An IPv4 network address.
- Record
- Autonomous System number record.
Enums§
Functions§
- read_
asn_ tsv - Reads ASN database TSV file (
ip2asn-v4.tsvformat) provided by IPtoASN as iterator ofRecords.