Expand description
A high-performance, memory-efficient Rust crate for mapping IP addresses to Autonomous System (AS) information with sub-microsecond lookups.
The core of the crate is the IpAsnMap
, a read-optimized data structure
constructed using a flexible Builder
. The builder can ingest data from
files, network streams, or in-memory buffers, and can operate in either a
strict mode that errors on malformed lines or a resilient mode that skips
them. Lookups are performed in sub-microsecond time, returning a lightweight
view of the ASN details. The crate provides detailed Error
and Warning
types for robust error handling.
All public data structures are marked as #[non_exhaustive]
to allow for
future additions without breaking backward compatibility. This means you must
use the ..
syntax in match patterns on enums and struct instantiations.
§Quick Start
use ip2asn::{Builder, IpAsnMap};
use ip_network::IpNetwork;
use std::net::IpAddr;
fn main() -> Result<(), ip2asn::Error> {
// A small, in-memory TSV data source for the example.
let data = "31.13.64.0\t31.13.127.255\t32934\tUS\tFACEBOOK-AS";
// Build the map from a source that implements `io::Read`.
let map = Builder::new()
.with_source(data.as_bytes())?
.build()?;
// Perform a lookup.
let ip: IpAddr = "31.13.100.100".parse().unwrap();
if let Some(info) = map.lookup(ip) {
assert_eq!(info.network, "31.13.64.0/18".parse::<IpNetwork>().unwrap());
assert_eq!(info.asn, 32934);
assert_eq!(info.country_code, "US");
assert_eq!(info.organization, "FACEBOOK-AS");
println!(
"{} -> AS{} {} ({}) in {}",
ip, info.asn, info.organization, info.country_code, info.network
);
}
Ok(())
}
Modules§
- parser
- Line-by-line parsing logic for IP-to-ASN data.
Contains the logic for parsing a single line of the
iptoasn.com
TSV data. - range
- IP range to CIDR conversion logic.
- types
- Core data structures for ASN records.
This module contains the primary data structures for the
ip2asn
crate, with a focus on memory efficiency and query performance.
Structs§
- AsnInfo
- An owned struct containing ASN information for an IP address.
- AsnInfo
View - A lightweight, read-only view into the ASN information for an IP address.
This struct is returned by the
lookup
method. - Builder
- A builder for configuring and loading an
IpAsnMap
. - IpAsn
Map - A read-optimized, in-memory map for IP address to ASN lookups.
Construction is handled by the
Builder
.
Enums§
- Error
- The primary error type for the crate.
- Parse
Error Kind - The specific kind of error that occurred during line parsing.
- Warning
- A non-fatal warning for a skipped line during parsing.