Crates.io | ipnet-trie |
lib.rs | ipnet-trie |
version | 0.2.0 |
source | src |
created_at | 2023-08-01 02:03:15.011675 |
updated_at | 2024-09-12 02:38:25.10951 |
description | IPv4 and IPv6 network fast lookup trie. |
homepage | https://github.com/bgpkit/ipnet-trie |
repository | https://github.com/bgpkit/ipnet-trie |
max_upload_size | |
id | 931411 |
size | 115,853 |
IPv4 and IPv6 network fast lookup prefix trie.
This crate provides storage and retrieval of IPv4 and IPv6 network prefixes. It uses the ipnet
crate as the IP network data structure and prefix-trie
as the backend, offering fast lookup times and a small memory footprint.
export
feature flag)export
: Enable export of the trie to bytes or a writer, and import from bytes or a reader.Add this to your Cargo.toml
:
[dependencies]
ipnet = "2"
ipnet-trie = "0.2"
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")));
// Insert a network
table.insert(network, value);
// Exact match
let value = table.exact_match(network);
// Longest prefix match
let (matched_network, value) = table.longest_match(&ip_address);
// Iterate over all networks
for (network, value) in table.iter() {
// ...
}
// Get the total number of unique IPv4 and IPv6 addresses in the trie
let (ipv4_count, ipv6_count) = table.ip_count();
// Compare two tries
let (added, removed) = trie1.diff(&trie2);
export
feature)// Export to bytes
let bytes = table.export_to_bytes();
// Import from bytes
table.import_from_bytes(&bytes);
// Export to writer
table.export_to_writer(&mut writer)?;
// Import from reader
table.import_from_reader(&mut reader)?;