16 releases

Uses old Rust 2015

0.6.7 Jan 17, 2022
0.6.6 Nov 7, 2021
0.6.4 May 18, 2021
0.6.3 May 13, 2020
0.4.1 Nov 2, 2017

#586 in Network programming

Download history 18499/week @ 2024-08-08 20744/week @ 2024-08-15 20640/week @ 2024-08-22 18963/week @ 2024-08-29 18789/week @ 2024-09-05 16957/week @ 2024-09-12 19057/week @ 2024-09-19 19860/week @ 2024-09-26 20375/week @ 2024-10-03 18790/week @ 2024-10-10 21670/week @ 2024-10-17 20644/week @ 2024-10-24 19571/week @ 2024-10-31 20198/week @ 2024-11-07 24125/week @ 2024-11-14 21724/week @ 2024-11-21

89,270 downloads per month
Used in 23 crates (11 directly)

MIT license

62KB
1.5K SLoC

iprange-rs

Crates Version CI

iprange-rs is a Rust library for managing IP ranges.

It provides fast adding and removing operations.

It also provides merge, intersect and exclude methods that enable you to manipulate it like a set.

Of course, you can test whether an IP address is in an IpRange.

See the documentation for details.

Example

extern crate iprange;
extern crate ipnet;

use std::net::Ipv4Addr;
use iprange::IpRange;
use ipnet::Ipv4Net;

fn main() {
    let ip_range: IpRange<Ipv4Net> = ["10.0.0.0/8", "172.16.0.0/16", "192.168.1.0/24"]
        .iter()
        .map(|s| s.parse().unwrap())
        .collect();

    assert!(ip_range.contains(&"172.16.32.1".parse::<Ipv4Addr>().unwrap()));
    assert!(ip_range.contains(&"192.168.1.1".parse::<Ipv4Addr>().unwrap()));
}

Serde support

Serde support is optional and disabled by default. To enable, use the feature serde.

[dependencies]
iprange = { version = "0.6", features = ["serde"] }

Benchmark

iprange-rs stores the IP networks in a radix trie. This allows us to store and lookup IP information quickly.

There is no Rust alternative to this crate, so I decide to compare it to those written in Go.

On my computer, here is the benchmark result for Go implementations:

BenchmarkIPv4Contains-8                   500000              2545 ns/op
BenchmarkIPv4Contains_Radix-8             200000              6960 ns/op
BenchmarkIPv4Contains_NRadix-8           1000000              1828 ns/op
BenchmarkIPv6Contains-8                   300000              3989 ns/op
BenchmarkIPv6Contains_Radix-8             200000              6818 ns/op
BenchmarkIPv6Contains_NRadix-8            500000              3039 ns/op

And below are the results of the equivalent Rust program using iprange-rs:

test test_ipv4_against_go             ... bench:         751 ns/iter (+/- 5)
test test_ipv6_against_go             ... bench:       2,500 ns/iter (+/- 20)

We can see the Rust one using iprange-rs is 2.4x faster than even the fastest Go implementation when dealing with IPv4 and is 1.2x faster with IPv6.

License

iprange-rs is licensed under the MIT license.

Dependencies

~110–295KB