Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ readme = "README.md"
keywords = ["ip", "networking", "trie", "cidr"]
categories = ["data-structures", "network-programming"]

[dependencies]
bytecheck = { version = "~0.6.8", default-features = false, optional = true }
rkyv = { version = "0.7.42", features = ["validation", "strict"], optional = true }

[dev-dependencies]
rand = "0.7.2"

Expand Down
138 changes: 125 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "rkyv")]
extern crate rkyv;


#[cfg(feature = "alloc")]
use core as std;
use alloc::vec::Vec;

#[cfg(feature = "alloc")]
use core as std;
use std::marker::PhantomData;

mod tree_bitmap;
Expand All @@ -35,14 +41,46 @@ use address::Address;
pub use address::addr::*;

/// A fast, compressed IP lookup table.
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[cfg_attr(feature = "bytecheck", archive_attr(derive(rkyv::CheckBytes)))]
#[derive(Debug)]
pub struct IpLookupTable<A, T> {
inner: TreeBitmap<T>,
_addrtype: PhantomData<A>,
}

impl<A: Address + Ord, T: Clone + Copy + Default + Ord> PartialEq for IpLookupTable<A, T> {
fn eq(&self, other: &Self) -> bool {
let mut self_entries: Vec<(A, u32, &T)> = self.iter().collect();
let mut other_entries: Vec<(A, u32, &T)> = other.iter().collect();

if self_entries.len() != other_entries.len() {
return false;
}

self_entries.sort_by(|a, b| {
a.0.cmp(&b.0)
.then_with(|| a.1.cmp(&b.1))
.then_with(|| a.2.cmp(&b.2))
});

other_entries.sort_by(|a, b| {
a.0.cmp(&b.0)
.then_with(|| a.1.cmp(&b.1))
.then_with(|| a.2.cmp(&b.2))
});

self_entries.iter().eq(other_entries.iter())
}
}

impl<A, T> IpLookupTable<A, T>
where
A: Address,
T: Clone + Copy + Default,
{
/// Initialize an empty lookup table with no preallocation.
pub fn new() -> Self {
Expand Down Expand Up @@ -85,7 +123,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -106,7 +144,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -132,7 +170,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -154,7 +192,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -180,7 +218,7 @@ where
/// # Example
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand Down Expand Up @@ -212,7 +250,7 @@ where
/// # Example
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand Down Expand Up @@ -244,7 +282,7 @@ where
/// # Example
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand Down Expand Up @@ -282,7 +320,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -308,7 +346,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let x: Ipv6Addr = "2001:db8:100::".parse().unwrap();
Expand Down Expand Up @@ -339,15 +377,17 @@ where
impl<A, T> Default for IpLookupTable<A, T>
where
A: Address,
T: Clone + Copy + Default,
{
fn default() -> Self {
Self::new()
}
}

impl<'a, A, T: 'a> Iterator for Iter<'a, A, T>
impl<'a, A, T> Iterator for Iter<'a, A, T>
where
A: Address,
T: 'a + Clone + Copy + Default,
{
type Item = (A, u32, &'a T);

Expand All @@ -361,9 +401,10 @@ where
}
}

impl<'a, A, T: 'a> Iterator for IterMut<'a, A, T>
impl<'a, A, T> Iterator for IterMut<'a, A, T>
where
A: Address,
T: 'a + Clone + Copy + Default,
{
type Item = (A, u32, &'a mut T);

Expand All @@ -377,9 +418,10 @@ where
}
}

impl<'a, A, T: 'a> Iterator for IntoIter<A, T>
impl<'a, A, T> Iterator for IntoIter<A, T>
where
A: Address,
T: 'a + Clone + Copy + Default,
{
type Item = (A, u32, T);

Expand All @@ -396,6 +438,7 @@ where
impl<A, T> IntoIterator for IpLookupTable<A, T>
where
A: Address,
T: Clone + Copy + Default,
{
type Item = (A, u32, T);
type IntoIter = IntoIter<A, T>;
Expand Down Expand Up @@ -431,3 +474,72 @@ pub struct IntoIter<A, T> {
inner: tree_bitmap::IntoIter<T>,
_addrtype: PhantomData<A>,
}


#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_partial_eq() {
let mut tbl1 = IpLookupTable::<core::net::Ipv4Addr, i32>::new();
tbl1.insert(core::net::Ipv4Addr::new(10, 0, 0, 1), 17, 1);
tbl1.insert(core::net::Ipv4Addr::new(172, 16, 0, 1), 17, 2);
tbl1.insert(core::net::Ipv4Addr::new(192, 168, 1, 1), 17, 3);
tbl1.insert(core::net::Ipv4Addr::new(8, 8, 8, 8), 17, 4);

// insertion order shouldn't affect equality
let mut tbl2 = IpLookupTable::<core::net::Ipv4Addr, i32>::new();
tbl2.insert(core::net::Ipv4Addr::new(172, 16, 0, 1), 17, 2);
tbl2.insert(core::net::Ipv4Addr::new(10, 0, 0, 1), 17, 1);
tbl2.insert(core::net::Ipv4Addr::new(8, 8, 8, 8), 17, 4);
tbl2.insert(core::net::Ipv4Addr::new(192, 168, 1, 1), 17, 3);
assert_eq!(tbl1, tbl2);

// mismatching data
let mut tbl3 = IpLookupTable::<core::net::Ipv4Addr, i32>::new();
tbl3.insert(core::net::Ipv4Addr::new(10, 0, 0, 1), 17, 100);
tbl3.insert(core::net::Ipv4Addr::new(172, 16, 0, 1), 17, 2);
tbl3.insert(core::net::Ipv4Addr::new(192, 168, 1, 1), 17, 3);
tbl3.insert(core::net::Ipv4Addr::new(8, 8, 8, 8), 17, 4);
assert_ne!(tbl1, tbl3);

// IP missing
let mut tbl4 = IpLookupTable::<core::net::Ipv4Addr, i32>::new();
tbl4.insert(core::net::Ipv4Addr::new(10, 0, 0, 1), 17, 1);
tbl4.insert(core::net::Ipv4Addr::new(172, 16, 0, 1), 17, 2);
tbl4.insert(core::net::Ipv4Addr::new(192, 168, 1, 1), 17, 3);
assert_ne!(tbl1, tbl4);

// Extra IP
let mut tbl5 = IpLookupTable::<core::net::Ipv4Addr, i32>::new();
tbl5.insert(core::net::Ipv4Addr::new(10, 0, 0, 1), 17, 1);
tbl5.insert(core::net::Ipv4Addr::new(172, 16, 0, 1), 17, 2);
tbl5.insert(core::net::Ipv4Addr::new(192, 168, 1, 1), 17, 3);
tbl5.insert(core::net::Ipv4Addr::new(8, 8, 8, 8), 17, 4);
tbl5.insert(core::net::Ipv4Addr::new(1, 1, 1, 1), 17, 4);
assert_ne!(tbl1, tbl5);


// IPV6
let mut tbl6 = IpLookupTable::<core::net::Ipv6Addr, i32>::new();
tbl6.insert(core::net::Ipv6Addr::new(0x2001, 0x0db8, 0x85a3, 0, 0, 0x8a2e, 0x0370, 0x7334), 128, 1);
tbl6.insert(core::net::Ipv6Addr::new(0x2607, 0xf8b0, 0x400d, 0x0, 0x0, 0x0, 0x0, 0x200e), 128, 2);
tbl6.insert(core::net::Ipv6Addr::new(0x2a00, 0x1450, 0x4001, 0x80b, 0x0, 0x0, 0x0, 0x2003), 128, 3);
tbl6.insert(core::net::Ipv6Addr::new(0x2404, 0x6800, 0x4003, 0x802, 0x0, 0x0, 0x0, 0x200e), 128, 4);

let mut tbl7 = IpLookupTable::<core::net::Ipv6Addr, i32>::new();
tbl7.insert(core::net::Ipv6Addr::new(0x2607, 0xf8b0, 0x400d, 0x0, 0x0, 0x0, 0x0, 0x200e), 128, 2);
tbl7.insert(core::net::Ipv6Addr::new(0x2001, 0x0db8, 0x85a3, 0, 0, 0x8a2e, 0x0370, 0x7334), 128, 1);
tbl7.insert(core::net::Ipv6Addr::new(0x2404, 0x6800, 0x4003, 0x802, 0x0, 0x0, 0x0, 0x200e), 128, 4);
tbl7.insert(core::net::Ipv6Addr::new(0x2a00, 0x1450, 0x4001, 0x80b, 0x0, 0x0, 0x0, 0x2003), 128, 3);
assert_eq!(tbl6, tbl7);

let mut tbl8 = IpLookupTable::<core::net::Ipv6Addr, i32>::new();
tbl8.insert(core::net::Ipv6Addr::new(0x2001, 0x0db8, 0x85a3, 0, 0, 0x8a2e, 0x0370, 0x7334), 128, 1);
tbl8.insert(core::net::Ipv6Addr::new(0x2607, 0xf8b0, 0x400d, 0x0, 0x0, 0x0, 0x0, 0x200e), 128, 200);
tbl8.insert(core::net::Ipv6Addr::new(0x2a00, 0x1450, 0x4001, 0x80b, 0x0, 0x0, 0x0, 0x2003), 128, 3);
tbl8.insert(core::net::Ipv6Addr::new(0x2404, 0x6800, 0x4003, 0x802, 0x0, 0x0, 0x0, 0x200e), 128, 4);
assert_ne!(tbl6, tbl8);

}
}
Loading