Skip to content

Commit e1784a3

Browse files
hack3riccathay4t
authored andcommitted
route: allow IPv6 gateway in IPv4 route
Linux supports it by using RTA_VIA instead of RTA_GATEWAY. Since it essentially acts as gateway, we extend the current API to allow it.
1 parent 7fcba62 commit e1784a3

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/route/builder.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ impl RouteMessageBuilder<Ipv4Addr> {
211211
.push(RouteAttribute::Gateway(RouteAddress::Inet(addr)));
212212
self
213213
}
214+
215+
/// Sets the IPv6 gateway (via) address.
216+
pub fn via(mut self, addr: Ipv6Addr) -> Self {
217+
self.message
218+
.attributes
219+
.push(RouteAttribute::Via(RouteVia::Inet6(addr)));
220+
self
221+
}
214222
}
215223

216224
impl Default for RouteMessageBuilder<Ipv4Addr> {
@@ -403,25 +411,17 @@ impl RouteMessageBuilder<IpAddr> {
403411
mut self,
404412
addr: IpAddr,
405413
) -> Result<Self, InvalidRouteMessage> {
406-
self.set_address_family_from_ip_addr(addr);
407-
match self.message.header.address_family {
408-
AddressFamily::Inet => {
409-
if addr.is_ipv6() {
410-
return Err(InvalidRouteMessage::Gateway(addr));
411-
};
412-
}
413-
AddressFamily::Inet6 => {
414-
if addr.is_ipv4() {
415-
return Err(InvalidRouteMessage::Gateway(addr));
416-
};
414+
let attr = match (self.message.header.address_family, addr) {
415+
(AddressFamily::Inet, addr @ IpAddr::V4(_))
416+
| (AddressFamily::Inet6, addr @ IpAddr::V6(_)) => {
417+
RouteAttribute::Gateway(addr.into())
417418
}
418-
af => {
419-
return Err(InvalidRouteMessage::AddressFamily(af));
419+
(AddressFamily::Inet, IpAddr::V6(v6)) => {
420+
RouteAttribute::Via(RouteVia::Inet6(v6))
420421
}
421-
}
422-
self.message
423-
.attributes
424-
.push(RouteAttribute::Gateway(addr.into()));
422+
(af, _) => return Err(InvalidRouteMessage::AddressFamily(af)),
423+
};
424+
self.message.attributes.push(attr);
425425
Ok(self)
426426
}
427427

0 commit comments

Comments
 (0)