Skip to content

Commit

Permalink
Clean up routes module
Browse files Browse the repository at this point in the history
  • Loading branch information
jD91mZM2 committed Jun 19, 2018
1 parent d23aee4 commit a739df8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn main() {
let default_v6_gw = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100);
let mut routes_storage = [None; 2];
let mut routes = Routes::new(&mut routes_storage[..]);
routes.add_default_ipv4_route(default_v4_gw).unwrap();
routes.set_default_ipv4_route(default_v4_gw).unwrap();
routes.add_default_ipv6_route(default_v6_gw).unwrap();
let mut iface = EthernetInterfaceBuilder::new(device)
.ethernet_addr(ethernet_addr)
Expand Down
55 changes: 44 additions & 11 deletions src/iface/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ impl<'a> Routes<'a> {
Routes { storage }
}

/// Update the routes of this node.
pub fn update<F: FnOnce(&mut ManagedMap<'a, IpCidr, Route>)>(&mut self, f: F) {
f(&mut self.storage);
/// Return a reference to the inner map
pub fn get_ref(&mut self) -> &ManagedMap<'a, IpCidr, Route> {
&self.storage
}
/// Return a mutable reference to the inner map
pub fn get_mut(&mut self) -> &mut ManagedMap<'a, IpCidr, Route> {
&mut self.storage
}

/// Add a default ipv4 gateway (ie. "ip route add 0.0.0.0/0 via `gateway`").
/// Set the default ipv4 gateway (ie. "ip route add 0.0.0.0/0 via `gateway`").
///
/// On success, returns the previous default route, if any.
#[cfg(feature = "proto-ipv4")]
pub fn add_default_ipv4_route(&mut self, gateway: Ipv4Address) -> Result<Option<Route>> {
pub fn set_default_ipv4_route(&mut self, gateway: Ipv4Address) -> Result<Option<Route>> {
let cidr = IpCidr::new(IpAddress::v4(0, 0, 0, 0), 0);
let route = Route::new_ipv4_gateway(gateway);
match self.storage.insert(cidr, route) {
Expand All @@ -92,6 +96,22 @@ impl<'a> Routes<'a> {
}
}

/// Get the current default ipv4 gateway, if any
#[cfg(feature = "proto-ipv4")]
pub fn get_default_ipv4_route(&mut self) -> Option<&Route> {
let cidr = IpCidr::new(IpAddress::v4(0, 0, 0, 0), 0);
self.storage.get(&cidr)
}

/// Remove default ipv4 gateway.
///
/// On success, returns the previous default route, if any
#[cfg(feature = "proto-ipv4")]
pub fn remove_default_ipv4_route(&mut self) -> Option<Route> {
let cidr = IpCidr::new(IpAddress::v4(0, 0, 0, 0), 0);
self.storage.remove(&cidr)
}

/// Add a default ipv6 gateway (ie. "ip -6 route add ::/0 via `gateway`").
///
/// On success, returns the previous default route, if any.
Expand All @@ -105,6 +125,23 @@ impl<'a> Routes<'a> {
}
}

/// Get the current default ipv6 gateways, if any
#[cfg(feature = "proto-ipv6")]
pub fn get_default_ipv6_routes(&mut self) -> impl Iterator<Item = &Route> {
let cidr = IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 0), 0);
// TODO: Support multiple default ipv6 gateways
self.storage.get(&cidr).into_iter()
}

/// Clear default ipv6 gateways.
///
/// On success, returns the previous default route, if any
#[cfg(feature = "proto-ipv6")]
pub fn clear_default_ipv6_routes(&mut self) -> Option<Route> {
let cidr = IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 0), 0);
self.storage.remove(&cidr)
}

pub(crate) fn lookup(&self, addr: &IpAddress, timestamp: Instant) ->
Option<IpAddress> {
assert!(addr.is_unicast());
Expand Down Expand Up @@ -195,9 +232,7 @@ mod test {
via_router: ADDR_1A.into(),
preferred_until: None, expires_at: None,
};
routes.update(|storage| {
storage.insert(cidr_1().into(), route).unwrap();
});
routes.get_mut().insert(cidr_1().into(), route).unwrap();

assert_eq!(routes.lookup(&ADDR_1A.into(), Instant::from_millis(0)), Some(ADDR_1A.into()));
assert_eq!(routes.lookup(&ADDR_1B.into(), Instant::from_millis(0)), Some(ADDR_1A.into()));
Expand All @@ -210,9 +245,7 @@ mod test {
preferred_until: Some(Instant::from_millis(10)),
expires_at: Some(Instant::from_millis(10)),
};
routes.update(|storage| {
storage.insert(cidr_2().into(), route2).unwrap();
});
routes.get_mut().insert(cidr_2().into(), route2).unwrap();

assert_eq!(routes.lookup(&ADDR_1A.into(), Instant::from_millis(0)), Some(ADDR_1A.into()));
assert_eq!(routes.lookup(&ADDR_1B.into(), Instant::from_millis(0)), Some(ADDR_1A.into()));
Expand Down

0 comments on commit a739df8

Please sign in to comment.