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: u32The 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
impl Router
Sourcepub fn new(if_index: u32) -> Self
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.
Sourcepub fn route(&mut self, dest_ip: &Ipv4Addr) -> Option<NextHop>
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.
Sourcepub fn refresh(&mut self) -> Result<(), Error>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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