Skip to main content

RoutingTable

Trait RoutingTable 

Source
pub trait RoutingTable {
    type Value: 'static;

    // Required methods
    fn contains(&self, ip: IpAddr) -> bool;
    fn insert(&mut self, prefix: IpNet, val: Self::Value) -> Option<Self::Value>;
    fn remove(&mut self, prefix: IpNet) -> Option<Self::Value>;
    fn clear(&mut self);
    fn lookup(&self, ip: IpAddr) -> Option<&Self::Value>;
    fn lookup_all(&self, ip: IpAddr) -> LookupIter<'_, Self::Value> ;
    fn lookup_prefix_exact(&self, prefix: IpNet) -> Option<&Self::Value>;
    fn lookup_prefix(&self, prefix: IpNet) -> Option<&Self::Value>;
    fn lookup_prefix_lpm(&self, prefix: IpNet) -> Option<(IpNet, &Self::Value)>;
    fn size(&self) -> usize;
}
Expand description

Abstracts routing table operations.

Required Associated Types§

Source

type Value: 'static

The value stored in each route.

Required Methods§

Source

fn contains(&self, ip: IpAddr) -> bool

Report whether ip is covered by a route in the table.

§Examples
let mut table = Table::default();
let pfx = "1.2.3.0/24".parse().unwrap();

table.insert(pfx, 12);
assert!(table.contains("1.2.3.4".parse().unwrap()));
assert!(!table.contains("1.2.4.4".parse().unwrap()));
Source

fn insert(&mut self, prefix: IpNet, val: Self::Value) -> Option<Self::Value>

Insert a route into the table at prefix.

§Examples
let mut table = Table::default();
let pfx = "0.0.0.0/0".parse().unwrap();

assert_eq!(None, table.insert(pfx, 12));
// Table has the value now
assert_eq!(Some(&12), table.lookup_prefix_exact(pfx));
// Repeated insert returns the removed value
assert_eq!(Some(12), table.insert(pfx, 13));
Source

fn remove(&mut self, prefix: IpNet) -> Option<Self::Value>

Remove the route from the table with the given prefix.

§Examples
let mut table = Table::default();
let pfx = "0.0.0.0/0".parse().unwrap();

// remove without a route returns `None`
assert_eq!(None, table.remove(pfx));

// insert then remove returns the value that was inserted
assert_eq!(None, table.insert(pfx, 12));
assert_eq!(Some(12), table.remove(pfx));
Source

fn clear(&mut self)

Wipe the whole table.

Source

fn lookup(&self, ip: IpAddr) -> Option<&Self::Value>

Lookup the route that most closely covers ip.

§Examples
let mut table = Table::default();

table.insert("1.2.0.0/16".parse().unwrap(), "value");
assert_eq!(Some(&"value"), table.lookup("1.2.3.4".parse().unwrap()));
Source

fn lookup_all(&self, ip: IpAddr) -> LookupIter<'_, Self::Value>

Lookup all matches that cover ip.

The iterator yields prefixes in reverse length order (most specific to least).

Source

fn lookup_prefix_exact(&self, prefix: IpNet) -> Option<&Self::Value>

Lookup a route that exactly matches prefix (supernets do not match).

§Examples
let mut table = Table::default();
let pfx = "1.2.0.0/16".parse().unwrap();

table.insert(pfx, "route");

// Exact prefix matches
assert_eq!(Some(&"route"), table.lookup_prefix_exact(pfx));
// Subnet does not
assert_eq!(
    None,
    table.lookup_prefix_exact("1.2.3.0/24".parse().unwrap())
);
Source

fn lookup_prefix(&self, prefix: IpNet) -> Option<&Self::Value>

Lookup the route that most closely covers prefix.

This represents a slight optimization over lookup_prefix_lpm if the matched prefix isn’t needed.

§Examples
let mut table = Table::default();
let pfx = "1.2.0.0/16".parse().unwrap();

table.insert(pfx, 1234);

// subnet lookup gets only the value in the table
assert_eq!(
    Some(&1234),
    table.lookup_prefix("1.2.3.4/30".parse().unwrap())
);
Source

fn lookup_prefix_lpm(&self, prefix: IpNet) -> Option<(IpNet, &Self::Value)>

Lookup the route that most closely covers prefix, and return that matching prefix.

§Examples
let mut table = Table::default();
let pfx = "1.2.0.0/16".parse().unwrap();

table.insert(pfx, true);

// subnet lookup returns the matching prefix and route value in the table
assert_eq!(
    Some((pfx, &true)),
    table.lookup_prefix_lpm("1.2.12.143/32".parse().unwrap())
);
Source

fn size(&self) -> usize

Report the number of routes stored in the table.

§Examples
let mut table = Table::default();
assert_eq!(0, table.size());

table.insert("0.0.0.0/0".parse().unwrap(), true);
assert_eq!(1, table.size());

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> RoutingTable for &mut T
where T: RoutingTable,

Source§

type Value = <T as RoutingTable>::Value

Source§

fn insert(&mut self, prefix: IpNet, val: Self::Value) -> Option<Self::Value>

Source§

fn remove(&mut self, prefix: IpNet) -> Option<Self::Value>

Source§

fn clear(&mut self)

Source§

fn contains(&self, ip: IpAddr) -> bool

Source§

fn lookup(&self, ip: IpAddr) -> Option<&Self::Value>

Source§

fn lookup_all(&self, ip: IpAddr) -> LookupIter<'_, Self::Value>

Source§

fn lookup_prefix_exact(&self, prefix: IpNet) -> Option<&Self::Value>

Source§

fn lookup_prefix(&self, prefix: IpNet) -> Option<&Self::Value>

Source§

fn lookup_prefix_lpm(&self, prefix: IpNet) -> Option<(IpNet, &Self::Value)>

Source§

fn size(&self) -> usize

Implementors§

Source§

impl<Node> RoutingTable for SimpleTable<Node>
where Node: StrideOps,

Source§

type Value = <Node as StrideBase>::T

Source§

impl<Node> RoutingTable for SplitStackTable<Node>
where Node: StrideOps,

Source§

type Value = <Node as StrideBase>::T