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
Conversation
jD91mZM2
commented
Jun 19, 2018
•
edited
edited
- Replace update with get_ref/get_mut
- Rename ipv4 add_default_ipv4_route to set since it can only have one gateway
- Add get_default_ipv4_route and get_default_ipv6_routes
- Add remove_default_ipv4_route and clear_default_ipv6_routes
The |
I think |
There is--if you just give away a |
What exactly do you mean by internal invariants? |
Not every route is legal. For example, no multicast addresses should be added to the route table. This isn't checked currently, but should be, and it can't be checked with |
Aaaaaah okay, that explains it. I'll implement your add_route idea |
The |
There wasn't much to do in |
src/iface/ethernet.rs
Outdated
f(&mut self.inner.ip_addrs); | ||
InterfaceInner::check_ip_addrs(&self.inner.ip_addrs) | ||
pub fn set_ip_addrs<S>(&mut self, ips: S) | ||
where S: Into<ManagedSlice<'c, IpCidr>> |
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?
This reverts commit 3d9b73b.
@@ -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 comment
The 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 comment
The 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 comment
The 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)
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
} | ||
/// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
returns the route it deleted, if any
/// Delete a route. | ||
/// | ||
/// On success, returns the value it deleted, if any. | ||
pub fn del_route(&mut self, cidr: &IpCidr) -> Option<Route> { |
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.
This function should be called delete_route
(or remove_route
; not del_route
).
/// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
The naming of this function is inconsistent with set_*_default_route
. Either this should be called set_route
, or those functions should be called add_*_default_route
. They fundamentally do the same thing.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
IPv6 gateways
#[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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
IPv6 gateways
|
||
/// Clear default ipv6 gateways. | ||
/// | ||
/// On success, returns the cleared routes, if any. |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This function should use self.remove_route
.
Since you previously said you don't think this is needed, I'll just close this because I'm lazy 😛. Better now than later when I put in more time into something that isn't even desired. |