| 1 | # Rust MaxMind DB Reader |
| 2 | |
| 3 | [](https://crates.io/crates/maxminddb) [](http://docs.rs/maxminddb) |
| 4 | |
| 5 | This library reads the MaxMind DB format, including the GeoIP2 and GeoLite2 |
| 6 | databases. |
| 7 | |
| 8 | ## Building |
| 9 | |
| 10 | To build everything: |
| 11 | |
| 12 | ``` |
| 13 | cargo build |
| 14 | ``` |
| 15 | |
| 16 | ## Testing |
| 17 | |
| 18 | This crate manages its test data within a git submodule. |
| 19 | To run the tests, you will first need to run the following command. |
| 20 | |
| 21 | ```bash |
| 22 | git submodule update --init |
| 23 | ``` |
| 24 | |
| 25 | ## Usage |
| 26 | |
| 27 | Add this to your `Cargo.toml`: |
| 28 | |
| 29 | ```toml |
| 30 | [dependencies] |
| 31 | maxminddb = "0.30" |
| 32 | ``` |
| 33 | |
| 34 | Enable optional features as needed: |
| 35 | |
| 36 | ```toml |
| 37 | [dependencies] |
| 38 | maxminddb = { version = "0.30", features = ["mmap"] } |
| 39 | ``` |
| 40 | |
| 41 | ## Example |
| 42 | |
| 43 | ```rust |
| 44 | use maxminddb::{geoip2, path, Reader}; |
| 45 | use std::net::IpAddr; |
| 46 | |
| 47 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 48 | let reader = Reader::open_readfile("/path/to/GeoLite2-City.mmdb")?; |
| 49 | |
| 50 | let ip: IpAddr = "89.160.20.128".parse()?; |
| 51 | let result = reader.lookup(ip)?; |
| 52 | println!("Network: {}", result.network()?); |
| 53 | |
| 54 | if let Some(city) = result.decode::<geoip2::City>()? { |
| 55 | println!("Country: {}", city.country.iso_code.unwrap_or("N/A")); |
| 56 | println!("City: {}", city.city.names.english.unwrap_or("N/A")); |
| 57 | } |
| 58 | |
| 59 | let iso_code: Option<&str> = result.decode_path(&path!["country", "iso_code"])?; |
| 60 | println!("Country code via decode_path: {}", iso_code.unwrap_or("N/A")); |
| 61 | |
| 62 | Ok(()) |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | `lookup()` returns a lightweight `LookupResult` handle. You can: |
| 67 | |
| 68 | - Check whether a record exists with `has_data()` |
| 69 | - Read the matched network with `network()` |
| 70 | - Decode the full record with `decode()` |
| 71 | - Decode one field with `decode_path()` |
| 72 | - Reuse `offset()` as a cache key when many IPs share the same record |
| 73 | |
| 74 | ## Iterating networks |
| 75 | |
| 76 | Use `within()` to iterate over the networks contained in a CIDR range, or |
| 77 | `networks()` to iterate over the whole database. The example below uses the |
| 78 | [`ipnetwork`](https://crates.io/crates/ipnetwork) crate, which is not |
| 79 | re-exported by `maxminddb`; add it to your own `Cargo.toml` to run this code. |
| 80 | |
| 81 | ```rust |
| 82 | use ipnetwork::IpNetwork; |
| 83 | use maxminddb::{Reader, WithinOptions}; |
| 84 | |
| 85 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 86 | let reader = Reader::open_readfile("/path/to/GeoLite2-City.mmdb")?; |
| 87 | let cidr: IpNetwork = "89.160.20.0/24".parse()?; |
| 88 | let opts = WithinOptions::default().skip_empty_values(); |
| 89 | |
| 90 | for result in reader.within(cidr, opts)? { |
| 91 | let lookup = result?; |
| 92 | println!("{}", lookup.network()?); |
| 93 | } |
| 94 | |
| 95 | Ok(()) |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | See the [examples](examples/) directory for runnable programs, including: |
| 100 | |
| 101 | - `cargo run --example lookup -- <database.mmdb> <ip>` |
| 102 | - `cargo run --example within -- <database.mmdb> <cidr>` |
| 103 | |
| 104 | ## Features |
| 105 | |
| 106 | Optional features: |
| 107 | |
| 108 | - **`mmap`**: Memory-mapped file access for long-running applications |
| 109 | - **`simdutf8`**: SIMD-accelerated UTF-8 validation |
| 110 | - **`unsafe-str-decode`**: Skip UTF-8 validation when deserializing trusted |
| 111 | database strings into Rust `str` or `String` values. Cross-runtime format |
| 112 | adapters should prefer `deserialize_any_with_raw_strings()` and validate |
| 113 | while constructing the target runtime's string type. |
| 114 | |
| 115 | Enable in `Cargo.toml`: |
| 116 | |
| 117 | ```toml |
| 118 | [dependencies] |
| 119 | maxminddb = { version = "0.30", features = ["mmap"] } |
| 120 | ``` |
| 121 | |
| 122 | Note: `simdutf8` and `unsafe-str-decode` are mutually exclusive. |
| 123 | |
| 124 | ## Documentation |
| 125 | |
| 126 | [API documentation on docs.rs](https://docs.rs/maxminddb) |
| 127 | |
| 128 | ## Benchmarks |
| 129 | |
| 130 | The project includes benchmarks using [Criterion.rs](https://github.com/bheisler/criterion.rs). |
| 131 | |
| 132 | First you need to have a working copy of the GeoIP City database. |
| 133 | You can fetch it from [here](https://dev.maxmind.com/geoip/geoip2/geolite2/). |
| 134 | |
| 135 | Place it in the root folder as `GeoIP2-City.mmdb`. |
| 136 | |
| 137 | Once this is done, run |
| 138 | |
| 139 | ``` |
| 140 | cargo bench |
| 141 | ``` |
| 142 | |
| 143 | Two focused benchmarks are especially useful while iterating on changes: |
| 144 | |
| 145 | ```bash |
| 146 | cargo bench --bench lookup |
| 147 | cargo bench --bench serde_usage |
| 148 | ``` |
| 149 | |
| 150 | If [gnuplot](http://www.gnuplot.info/) is installed, Criterion.rs can generate |
| 151 | an HTML report displaying the results of the benchmark under |
| 152 | `target/criterion/report/index.html`. |
| 153 | |
| 154 | ## Contributing |
| 155 | |
| 156 | Contributions welcome! Please fork the repository and open a pull request |
| 157 | with your changes. |
| 158 | |
| 159 | ## License |
| 160 | |
| 161 | This is free software, licensed under the ISC license. |