ip2asn
A high-performance, memory-efficient Rust crate for mapping IP addresses to Autonomous System (AS) information with sub-microsecond lookups.
Features
- High Performance: Sub-microsecond longest-prefix match lookups using a
PATRICIA trie (
ip_network_table
). - Memory Efficient: Uses string interning and optimized data structures to minimize memory footprint.
- Ergonomic API: A simple, chainable Builder pattern for easy configuration and map creation.
- Flexible Data Sources: Load data from any
std::io::Read
source (files, in-memory buffers, etc.). - Gzip Support: Transparently decompresses
.gz
data sources out of the box. - Remote Fetching: An optional
fetch
feature allows building the map directly from a URL. - Async-Friendly Lookups: An owned
AsnInfo
struct is available vialookup_owned()
for safe, lifetime-free use in async or threaded contexts. - Serde Support: An optional
serde
feature allowsAsnInfo
to be serialized and deserialized. - Robust Error Handling: Supports a
strict
mode to fail on any parsing error and a flexibleon_warning
callback for custom logging. - Runtime Agnostic: A fully synchronous core library that works with any
async runtime (
tokio
,smol
, etc.) or in non-async applications.
Quick Start
Add ip2asn
to your Cargo.toml
:
[]
= "0.1.1"
Then, use the Builder
to load your data and perform lookups.
use ;
use IpAddr;
Creating an Empty Map
For applications that start with an empty map and load data later, you can use
IpAsnMap::new()
or Default::default()
.
use IpAsnMap;
let empty_map = new;
assert!;
Async-Friendly Lookups
The standard lookup
method returns a view with a lifetime tied to the map.
For async code or situations where you need to store the result, use
lookup_owned
.
use ; // Note: AsnInfo is the owned struct
use IpAddr;
#
Fetching from a URL
With the fetch
feature enabled, you can build the map directly from a remote
data source.
[]
= { = "0.1.1", = ["fetch"] }
use ;
use IpAddr;
Serialization with Serde
Enable the serde
feature to serialize and deserialize the AsnInfo
struct.
[]
= { = "0.1.1", = ["serde"] }
= "1.0"
#
#
#
#
Error Handling
By default, the builder skips lines that cannot be parsed. You can enable
strict
mode to fail fast or use the on_warning
callback for custom logging.
use ;
use ;
ip2asn-cli
This workspace also includes ip2asn-cli
, a command-line tool that uses the ip2asn
library to perform lookups from your terminal.
Installation
You can install the CLI directly from this repository using cargo
:
Usage
The primary command is lookup
, which takes one or more IP addresses as arguments. If no IPs are provided, it reads them from stdin
.
# Look up a single IP
| | | |
# Look up multiple IPs and get JSON output
}}
}}
# Read from stdin
|
| | | |
Automatic Dataset Updates
On the first run, and subsequently whenever the cached data is more than 24 hours old, ip2asn-cli
will automatically check for and download the latest IP-to-ASN dataset from iptoasn.com.
- Data is cached at:
~/.cache/ip2asn/data.tsv.gz
- Progress messages are printed to
stderr
, so they won't interfere withstdout
parsing. - You can force a download at any time by running
ip2asn-cli update
.
Configuration
You can configure the tool's behavior by creating a file at ~/.config/ip2asn/config.toml
.
config.toml
# Set to false to disable the automatic 24-hour update check.
# The `update` subcommand will still work.
# Defaults to false.
= true
License
This project is licensed under the MIT License.