4 releases (2 breaking)

0.2.0-beta.1 Feb 21, 2024
0.1.0 Jan 16, 2024
0.0.2 Aug 1, 2023
0.0.1 Aug 1, 2023

#1352 in Network programming

40 downloads per month
Used in bgpkit-commons

MIT license

47KB
648 lines

ipnet-trie

IPv4 and IPv6 network fast lookup prefix trie.

Rust Documentation Crates.io License

Description

This crate provides storage and retrieval of IPv4 and IPv6 network prefixes.

Currently, it uses ipnet crate as IP network data structure and prefix-trie as backend, that provides fast lookup times, and a small memory footprint.

Feature flags

  • export: enable export trie into bytes or to a writer and import from bytes or from a reader.

Usage

Add this to your Cargo.toml:

[dependencies]
ipnet = "2.8"
ipnet-trie = "0.1"

and then you can use it like this:

use std::net::{IpAddr, Ipv6Addr};
use ipnet::{IpNet, Ipv6Net};
use ipnet_trie::IpnetTrie;

let mut table = IpnetTrie::new();
let network = IpNet::from(Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap());
let ip_address = Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0x1);

assert_eq!(table.insert(network, "foo"), None);
// Get value for network from table
assert_eq!(table.longest_match(ip_address), Some((network, &"foo")));

Dependencies

~355–490KB