2 releases
0.1.1 | Jul 2, 2024 |
---|---|
0.1.0 | Mar 5, 2024 |
#493 in Database interfaces
38KB
667 lines
Read location databases in libloc format.
It features constant-time lookups of IPv6/IPv4 addresses to ASN and country data ("GeoIP"). The database also contains names of AS and countries.
The main struct of this crate is Locations
. First, you'll need a
database. A recent version of the official IPFire database can be obtained from
https://location.ipfire.org/databases/1/location.db.xz, xz-compressed. Then
you can start from Locations::open
and use Locations::lookup
to look up
information on a particular IP address.
A short overview about the internals of the database format can be found at
https://www.ipfire.org/blog/libloc-or-what-is-working-inside-it. A Kaitai
struct implementation ipfire_libloc_db_v1.ksy
can be found in this
repository.
Examples
use libloc::Locations;
let locations = Locations::open("example-location.db")?;
let network: libloc::Network = locations.lookup("2a07:1c44:5800::1".parse().unwrap()).unwrap();
assert_eq!(network.country_code(), "DE");
assert_eq!(network.asn(), 204867);
assert_eq!(network.is_anonymous_proxy(), false);
assert_eq!(network.addrs().to_string(), "2a07:1c44:5800::/40");
let country: libloc::Country = locations.country("DE").unwrap();
assert_eq!(country.continent_code(), "EU");
assert_eq!(country.name(), "Germany");
let as_: libloc::As = locations.as_(204867).unwrap();
assert_eq!(as_.name(), "Lightning Wire Labs GmbH");
# Ok::<(), libloc::OpenError>(())
Panics
Any function from this library might panic if the database is corrupt.
Benches
This library was written for fun. It's still a lot faster than the original libloc, even though it wasn't optimized for speed.
Running benches/locations.rs
running 4 tests
test lookup ... bench: 87 ns/iter (+/- 15)
test lookup_v4 ... bench: 61 ns/iter (+/- 5)
test lookup_v6 ... bench: 396 ns/iter (+/- 22)
test open ... bench: 64,012 ns/iter (+/- 4,592)
test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured
Running benches/native.rs
running 2 tests
test lookup_v6 ... bench: 1,019 ns/iter (+/- 128)
test open ... bench: 234,420 ns/iter (+/- 19,638)
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
It's looking up IPv6 addresses roughly 2.5x as fast, it has special handling for IPv4 addresses which makes it >10x as fast.
Dependencies
~1–1.6MB
~22K SLoC