-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up routes module slightly #244
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,16 +74,36 @@ 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); | ||
/// Add a new route. | ||
/// | ||
/// On success, return the previous route, if any. | ||
pub fn add_route(&mut self, cidr: IpCidr, route: Route) -> Result<Option<Route>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming of this function is inconsistent with |
||
// TODO: Validate | ||
match self.storage.insert(cidr, route) { | ||
Ok(route) => Ok(route), | ||
Err((_cidr, _route)) => Err(Error::Exhausted) | ||
} | ||
} | ||
/// Returns a route registered on cidr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This docstring isn't informative at all. What does "registered on cidr" mean? It's not clear from the documentation whether this function would only return a route that is an exact match or any route matching the argument. Also, "CIDR" should be capitalized, it's an acronym. Also, there's a missing newline. |
||
pub fn get_route(&mut self, cidr: &IpCidr) -> Option<&Route> { | ||
self.storage.get(cidr) | ||
} | ||
/// Returns a mutable reference to the route registered by cidr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
pub fn get_route_mut(&mut self, cidr: &IpCidr) -> Option<&mut Route> { | ||
self.storage.get_mut(cidr) | ||
} | ||
/// Delete a route. | ||
/// | ||
/// On success, returns the value it deleted, if any. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
pub fn del_route(&mut self, cidr: &IpCidr) -> Option<Route> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should be called |
||
self.storage.remove(&cidr) | ||
} | ||
|
||
/// 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`"). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/// | ||
/// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should use |
||
let route = Route::new_ipv4_gateway(gateway); | ||
match self.storage.insert(cidr, route) { | ||
|
@@ -92,6 +112,22 @@ impl<'a> Routes<'a> { | |
} | ||
} | ||
|
||
/// Get the current default ipv4 gateway, if any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#[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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should use |
||
self.storage.get(&cidr) | ||
} | ||
|
||
/// Remove default ipv4 gateway. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/// | ||
/// Returns the removed route, if any. | ||
#[cfg(feature = "proto-ipv4")] | ||
pub fn remove_default_ipv4_route(&mut self) -> Option<Route> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once again, inconsistent naming with |
||
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. | ||
|
@@ -105,6 +141,23 @@ impl<'a> Routes<'a> { | |
} | ||
} | ||
|
||
/// Get the current default ipv6 gateways, if any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#[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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a TODO if you already return an iterator? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I return an iterator over one thing, but I heard that in the future you might support multiple |
||
self.storage.get(&cidr).into_iter() | ||
} | ||
|
||
/// Clear default ipv6 gateways. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/// | ||
/// On success, returns the cleared routes, if any. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function returns exactly one route. |
||
#[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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should use |
||
} | ||
|
||
pub(crate) fn lookup(&self, addr: &IpAddress, timestamp: Instant) -> | ||
Option<IpAddress> { | ||
assert!(addr.is_unicast()); | ||
|
@@ -195,9 +248,7 @@ mod test { | |
via_router: ADDR_1A.into(), | ||
preferred_until: None, expires_at: None, | ||
}; | ||
routes.update(|storage| { | ||
storage.insert(cidr_1().into(), route).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what does this achieve? One line less in route setup code? I'm not convinced it's worth adding so many functions in the public API (and you don't even validate input in some of them). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, imagine you want to fetch the default gateway with the current code: let mut gateway = None;
routes.update(|storage| {
let cidr = IpCidr::new(IpAddress::v4(0, 0, 0, 0), 0);
gateway = Some(storage.get(&cidr).clone());
});
let gateway = gateway.unwrap(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the redox netstack code after this PR. (Your existing code doesn't validate input either btw) |
||
}); | ||
routes.add_route(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())); | ||
|
@@ -210,9 +261,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.add_route(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())); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of this method is to be able to update IP addresses without assigning a different ManagedSlice. It's for memory-constrained devices without an allocator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I make a way to set an ip based on index? Or revert the change to that file?