ipnet-trie

Crates.ioipnet-trie
lib.rsipnet-trie
version0.2.0
sourcesrc
created_at2023-08-01 02:03:15.011675
updated_at2024-09-12 02:38:25.10951
descriptionIPv4 and IPv6 network fast lookup trie.
homepagehttps://github.com/bgpkit/ipnet-trie
repositoryhttps://github.com/bgpkit/ipnet-trie
max_upload_size
id931411
size115,853
Mingwei Zhang (digizeph)

documentation

https://docs.rs/ipnet-trie

README

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. 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.

Features

  • Fast prefix lookup for both IPv4 and IPv6 networks
  • Efficient storage of IP prefixes and associated data
  • Support for exact match and longest prefix match operations
  • Ability to iterate over all stored prefixes
  • Export and import functionality (with the export feature flag)
  • Diff operation to compare two tries

Feature flags

  • export: Enable export of the trie to bytes or a writer, and import from bytes or a reader.

Usage

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")));

Insertion and Retrieval

// 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);

Iteration

// Iterate over all networks
for (network, value) in table.iter() {
    // ...
}

IP Count

// Get the total number of unique IPv4 and IPv6 addresses in the trie
let (ipv4_count, ipv6_count) = table.ip_count();

Diff Operation

// Compare two tries
let (added, removed) = trie1.diff(&trie2);

Export and Import (with 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)?;
Commit count: 71

cargo fmt