Router

Struct Router 

Source
pub struct Router {
    pub if_index: u32,
    pub neighbors: HashMap<Ipv4Addr, Neighbor>,
    pub routes: PrefixMap<Ipv4Net, Ipv4Route>,
}
Expand description

Manages a cached view of the system’s IPv4 routing and neighbor tables.

It holds the routing table in a prefix trie for efficient longest-prefix-match lookups and the neighbor (ARP) table in a hash map.

Fields§

§if_index: u32

The network interface index this router is bound to.

§neighbors: HashMap<Ipv4Addr, Neighbor>

A cache of neighbor (ARP) entries, mapping IP addresses to Neighbor structs.

§routes: PrefixMap<Ipv4Net, Ipv4Route>

A prefix trie (PrefixMap) for efficient longest-prefix-match lookups on routes.

Implementations§

Source§

impl Router

Source

pub fn new(if_index: u32) -> Self

Creates a new Router for a specific network interface.

§Arguments
  • if_index - The index of the network interface to manage routes for.
Source

pub fn route(&mut self, dest_ip: &Ipv4Addr) -> Option<NextHop>

Finds the next-hop information for a given destination IPv4 address.

§How it works

It first performs a longest-prefix-match (LPM) lookup in the cached routes table. If a route is found, it determines the next-hop IP (either the gateway or the destination itself). It then looks up the MAC address for that next-hop IP in the neighbors cache. If no route is found via LPM, it attempts a direct lookup in the neighbor cache for the destination IP. Returns a NextHop struct containing the next-hop IP and MAC address if successful.

Source

pub fn refresh(&mut self) -> Result<(), Error>

Refreshes the router’s cached tables from the kernel.

§How it works

It calls get_ipv4_routes and get_neighbors from the netlink module to fetch the latest data from the kernel for the router’s interface. It then rebuilds the internal routes prefix map and neighbors hash map with the new data.

Trait Implementations§

Source§

impl Debug for Router

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl Send for Router

§

impl Sync for Router

§

impl Unpin for Router

§

impl UnwindSafe for Router

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.