@@ -211,6 +211,14 @@ impl RouteMessageBuilder<Ipv4Addr> {
211
211
. push ( RouteAttribute :: Gateway ( RouteAddress :: Inet ( addr) ) ) ;
212
212
self
213
213
}
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
+ }
214
222
}
215
223
216
224
impl Default for RouteMessageBuilder < Ipv4Addr > {
@@ -403,25 +411,15 @@ impl RouteMessageBuilder<IpAddr> {
403
411
mut self ,
404
412
addr : IpAddr ,
405
413
) -> 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
- } ;
417
- }
418
- af => {
419
- return Err ( InvalidRouteMessage :: AddressFamily ( af) ) ;
414
+ use AddressFamily :: * ;
415
+ let attr = match ( self . message . header . address_family , addr) {
416
+ ( Inet , addr @ IpAddr :: V4 ( _) ) | ( Inet6 , addr @ IpAddr :: V6 ( _) ) => {
417
+ RouteAttribute :: Gateway ( addr. into ( ) )
420
418
}
421
- }
422
- self . message
423
- . attributes
424
- . push ( RouteAttribute :: Gateway ( addr . into ( ) ) ) ;
419
+ ( Inet , IpAddr :: V6 ( v6 ) ) => RouteAttribute :: Via ( RouteVia :: Inet6 ( v6 ) ) ,
420
+ ( af , _ ) => return Err ( InvalidRouteMessage :: AddressFamily ( af ) ) ,
421
+ } ;
422
+ self . message . attributes . push ( attr ) ;
425
423
Ok ( self )
426
424
}
427
425
0 commit comments