Struct ipnet_trie::IpnetTrie
source · pub struct IpnetTrie<T> { /* private fields */ }Expand description
Table holding IPv4 and IPv6 network prefixes with value.
Implementations§
source§impl<T> IpnetTrie<T>
impl<T> IpnetTrie<T>
sourcepub fn with_capacity(ipv4_size: usize, ipv6_size: usize) -> Self
pub fn with_capacity(ipv4_size: usize, ipv6_size: usize) -> Self
Constructs a new, empty IpNetworkTable<T> with the specified capacity.
sourcepub fn len(&self) -> (usize, usize)
pub fn len(&self) -> (usize, usize)
Returns the number of elements in the table. First value is number of IPv4 networks and second is number of IPv6 networks.
sourcepub fn ip_count(&self) -> (u32, u128)
pub fn ip_count(&self) -> (u32, u128)
Count the number of unique IPv4 and IPv6 addresses in the trie.
use std::str::FromStr;
use ipnet::{Ipv4Net, Ipv6Net};
use ipnet_trie::IpnetTrie;
let mut table = IpnetTrie::new();
table.insert(Ipv4Net::from_str("192.0.2.129/25").unwrap(), 1);
table.insert(Ipv4Net::from_str("192.0.2.0/24").unwrap(), 1);
table.insert(Ipv4Net::from_str("192.0.2.0/24").unwrap(), 1);
table.insert(Ipv4Net::from_str("192.0.2.0/24").unwrap(), 1);
assert_eq!(table.ip_count(), (256, 0));
table.insert(Ipv4Net::from_str("198.51.100.0/25").unwrap(), 1);
table.insert(Ipv4Net::from_str("198.51.100.64/26").unwrap(), 1);
assert_eq!(table.ip_count(), (384, 0));
table.insert(Ipv4Net::from_str("198.51.100.65/26").unwrap(), 1);
assert_eq!(table.ip_count(), (384, 0));
table.insert(Ipv6Net::from_str("2001:DB80::/48").unwrap(), 1);
assert_eq!(table.ip_count(), (384, 2_u128.pow(80)));
table.insert(Ipv6Net::from_str("2001:DB80::/49").unwrap(), 1);
assert_eq!(table.ip_count(), (384, 2_u128.pow(80)));sourcepub fn insert<N: Into<IpNet>>(&mut self, network: N, data: T) -> Option<T>
pub fn insert<N: Into<IpNet>>(&mut self, network: N, data: T) -> Option<T>
Insert a value for the IpNetwork. If prefix existed previously, the old value is returned.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::Ipv6Net;
use std::net::Ipv6Addr;
let mut table = IpnetTrie::new();
let network = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap();
assert_eq!(table.insert(network, "foo"), None);
// Insert duplicate
assert_eq!(table.insert(network, "bar"), Some("foo"));
// Value is replaced
assert_eq!(table.insert(network, "null"), Some("bar"));sourcepub fn remove<N: Into<IpNet>>(&mut self, network: N) -> Option<T>
pub fn remove<N: Into<IpNet>>(&mut self, network: N) -> Option<T>
Remove a IpNetwork from table. If prefix existed, the value is returned.
Examples
use ipnet_trie::IpnetTrie;
use std::net::Ipv6Addr;
use ipnet::Ipv6Net;
let mut table = IpnetTrie::new();
let network = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap();
assert_eq!(table.insert(network, "foo"), None);
// Remove network from table
assert_eq!(table.remove(network), Some("foo"));
// Network is removed
assert_eq!(table.exact_match(network), None);sourcepub fn exact_match<N: Into<IpNet>>(&self, network: N) -> Option<&T>
pub fn exact_match<N: Into<IpNet>>(&self, network: N) -> Option<&T>
Get pointer to value from table based on exact network match.
If network is not in table, None is returned.
Examples
use ipnet_trie::IpnetTrie;
use std::net::Ipv6Addr;
use ipnet::Ipv6Net;
let mut table = IpnetTrie::new();
let network_a = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap();
let network_b = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 128).unwrap();
assert_eq!(table.insert(network_a, "foo"), None);
// Get value for network from table
assert_eq!(table.exact_match(network_a), Some(&"foo"));
// Network B doesnt exists in table
assert_eq!(table.exact_match(network_b), None);sourcepub fn exact_match_mut<N: Into<IpNet>>(&mut self, network: N) -> Option<&mut T>
pub fn exact_match_mut<N: Into<IpNet>>(&mut self, network: N) -> Option<&mut T>
Get mutable pointer to value from table based on exact network match.
If network is not in table, None is returned.
Examples
use ipnet_trie::IpnetTrie;
use std::net::Ipv6Addr;
use ipnet::Ipv6Net;
let mut table = IpnetTrie::new();
let network_a = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap();
let network_b = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 128).unwrap();
assert_eq!(table.insert(network_a, "foo"), None);
// Get value for network from table
assert_eq!(table.exact_match_mut(network_a), Some(&mut "foo"));
// Network B doesnt exists in table
assert_eq!(table.exact_match(network_b), None);sourcepub fn longest_match<I: Into<IpAddr>>(&self, ip: I) -> Option<(IpNet, &T)>
pub fn longest_match<I: Into<IpAddr>>(&self, ip: I) -> Option<(IpNet, &T)>
Find most specific IP network in table that contains given IP address. If no network in table contains
given IP address, None is returned.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::{IpNet, Ipv6Net};
use std::net::{IpAddr, Ipv6Addr};
let mut table = IpnetTrie::new();
let network = IpNet::new(IpAddr::V6(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")));sourcepub fn longest_match_mut<I: Into<IpAddr>>(
&mut self,
ip: I
) -> Option<(IpNet, &mut T)>
pub fn longest_match_mut<I: Into<IpAddr>>( &mut self, ip: I ) -> Option<(IpNet, &mut T)>
Find most specific IP network in table that contains given IP address. If no network in table contains
given IP address, None is returned.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::{IpNet, Ipv6Net};
use std::net::{IpAddr, Ipv6Addr};
let mut table = IpnetTrie::new();
let network = IpNet::new(IpAddr::V6(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_mut(ip_address), Some((network, &mut "foo")));sourcepub fn longest_match_ipv4(&self, ip: Ipv4Addr) -> Option<(Ipv4Net, &T)>
pub fn longest_match_ipv4(&self, ip: Ipv4Addr) -> Option<(Ipv4Net, &T)>
Specific version of longest_match for IPv4 address.
sourcepub fn longest_match_ipv6(&self, ip: Ipv6Addr) -> Option<(Ipv6Net, &T)>
pub fn longest_match_ipv6(&self, ip: Ipv6Addr) -> Option<(Ipv6Net, &T)>
Specific version of longest_match for IPv6 address.
sourcepub fn matches<I: Into<IpAddr>>(
&self,
ip: I
) -> Box<dyn Iterator<Item = (IpNet, &T)> + '_>
pub fn matches<I: Into<IpAddr>>( &self, ip: I ) -> Box<dyn Iterator<Item = (IpNet, &T)> + '_>
Find all IP networks in table that contains given IP address.
Returns iterator of IpNetwork and reference to value.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::{IpNet, Ipv6Net};
use std::net::{IpAddr, Ipv6Addr};
let mut table = IpnetTrie::new();
let network = IpNet::new(IpAddr::V6(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.matches(ip_address).count(), 1);sourcepub fn matches_ipv4(&self, ip: Ipv4Addr) -> impl Iterator<Item = (Ipv4Net, &T)>
pub fn matches_ipv4(&self, ip: Ipv4Addr) -> impl Iterator<Item = (Ipv4Net, &T)>
Specific version of matches for IPv4 address.
sourcepub fn matches_ipv6(&self, ip: Ipv6Addr) -> impl Iterator<Item = (Ipv6Net, &T)>
pub fn matches_ipv6(&self, ip: Ipv6Addr) -> impl Iterator<Item = (Ipv6Net, &T)>
Specific version of matches for IPv6 address.
sourcepub fn matches_mut<I: Into<IpAddr>>(
&mut self,
ip: I
) -> Box<dyn Iterator<Item = (IpNet, &mut T)> + '_>
pub fn matches_mut<I: Into<IpAddr>>( &mut self, ip: I ) -> Box<dyn Iterator<Item = (IpNet, &mut T)> + '_>
Find all IP networks in table that contains given IP address.
Returns iterator of IpNetwork and mutable reference to value.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::{IpNet, Ipv6Net};
use std::net::{IpAddr, Ipv6Addr};
let mut table = IpnetTrie::new();
let network = IpNet::new(IpAddr::V6(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.matches_mut(ip_address).count(), 1);sourcepub fn matches_mut_ipv4(
&mut self,
ip: Ipv4Addr
) -> impl Iterator<Item = (Ipv4Net, &mut T)>
pub fn matches_mut_ipv4( &mut self, ip: Ipv4Addr ) -> impl Iterator<Item = (Ipv4Net, &mut T)>
Specific version of matches_mut for IPv4 address.
sourcepub fn matches_mut_ipv6(
&mut self,
ip: Ipv6Addr
) -> impl Iterator<Item = (Ipv6Net, &mut T)>
pub fn matches_mut_ipv6( &mut self, ip: Ipv6Addr ) -> impl Iterator<Item = (Ipv6Net, &mut T)>
Specific version of matches_mut for IPv6 address.
sourcepub fn iter(&self) -> impl Iterator<Item = (IpNet, &T)>
pub fn iter(&self) -> impl Iterator<Item = (IpNet, &T)>
Iterator for all networks in table, first are iterated IPv4 and then IPv6 networks. Order is not guaranteed.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
let mut table: IpnetTrie<&str> = IpnetTrie::new();
let network_a = Ipv4Net::new(Ipv4Addr::new(192, 168, 0, 0), 24).unwrap();
assert_eq!(table.insert(network_a, "foo"), None);
let network_b = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap();
assert_eq!(table.insert(network_b, "foo"), None);
let mut iterator = table.iter();
assert_eq!(iterator.next(), Some((IpNet::V4(network_a), &"foo")));
assert_eq!(iterator.next(), Some((IpNet::V6(network_b), &"foo")));
assert_eq!(iterator.next(), None);sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (IpNet, &mut T)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (IpNet, &mut T)>
Mutable iterator for all networks in table, first are iterated IPv4 and then IPv6 networks. Order is not guaranteed.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
let mut table: IpnetTrie<&str> = IpnetTrie::new();
let network_a = Ipv4Net::new(Ipv4Addr::new(192, 168, 0, 0), 24).unwrap();
assert_eq!(table.insert(network_a, "foo"), None);
let network_b = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap();
assert_eq!(table.insert(network_b, "foo"), None);
let mut iterator = table.iter_mut();
for (network, value) in iterator {
*value = "bar";
}
assert_eq!(table.exact_match(network_a), Some(&"bar"));
assert_eq!(table.exact_match(network_b), Some(&"bar"));sourcepub fn iter_ipv4(&self) -> impl Iterator<Item = (Ipv4Net, &T)>
pub fn iter_ipv4(&self) -> impl Iterator<Item = (Ipv4Net, &T)>
Iterator for all IPv4 networks in table. Order is not guaranteed.
sourcepub fn iter_ipv6(&self) -> impl Iterator<Item = (Ipv6Net, &T)>
pub fn iter_ipv6(&self) -> impl Iterator<Item = (Ipv6Net, &T)>
Iterator for all IPv6 networks in table. Order is not guaranteed.
sourcepub fn retain<F>(&mut self, f: F)where
F: FnMut(IpNet, &mut T) -> bool,
pub fn retain<F>(&mut self, f: F)where F: FnMut(IpNet, &mut T) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v) such that f(ip_network, &mut v) returns false.
Examples
use ipnet_trie::IpnetTrie;
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
let mut table: IpnetTrie<&str> = IpnetTrie::new();
let network_a = Ipv4Net::new(Ipv4Addr::new(192, 168, 0, 0), 24).unwrap();
assert_eq!(table.insert(network_a, "foo"), None);
let network_b = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap();
assert_eq!(table.insert(network_b, "foo"), None);
// Keep just IPv4 networks
table.retain(|network, _| network.network().is_ipv4());
assert_eq!(table.exact_match(network_a), Some(&"foo"));
assert_eq!(table.exact_match(network_b), None);