Rust MaxMind DB Reader
This library reads the MaxMind DB format, including the GeoIP2 and GeoLite2 databases.
Building
To build everything:
cargo build
Testing
This crate manages its test data within a git submodule. To run the tests, you will first need to run the following command.
Usage
Add this to your Cargo.toml:
[]
= "0.28"
Enable optional features as needed:
[]
= { = "0.28", = ["mmap"] }
Example
use ;
use IpAddr;
lookup() returns a lightweight LookupResult handle. You can:
- Check whether a record exists with
has_data() - Read the matched network with
network() - Decode the full record with
decode() - Decode one field with
decode_path() - Reuse
offset()as a cache key when many IPs share the same record
Iterating networks
Use within() to iterate over the networks contained in a CIDR range, or
networks() to iterate over the whole database. The example below uses the
ipnetwork crate, which is not
re-exported by maxminddb; add it to your own Cargo.toml to run this code.
use IpNetwork;
use ;
See the examples directory for runnable programs, including:
cargo run --example lookup -- <database.mmdb> <ip>cargo run --example within -- <database.mmdb> <cidr>
Features
Optional features:
mmap: Memory-mapped file access for long-running applicationssimdutf8: SIMD-accelerated UTF-8 validationunsafe-str-decode: Skip UTF-8 validation (requires trusted data)
Enable in Cargo.toml:
[]
= { = "0.28", = ["mmap"] }
Note: simdutf8 and unsafe-str-decode are mutually exclusive.
Documentation
Benchmarks
The project includes benchmarks using Criterion.rs.
First you need to have a working copy of the GeoIP City database. You can fetch it from here.
Place it in the root folder as GeoIP2-City.mmdb.
Once this is done, run
cargo bench
Two focused benchmarks are especially useful while iterating on changes:
If gnuplot is installed, Criterion.rs can generate
an HTML report displaying the results of the benchmark under
target/criterion/report/index.html.
Contributing
Contributions welcome! Please fork the repository and open a pull request with your changes.
License
This is free software, licensed under the ISC license.