@@ -8,7 +8,7 @@ use std::{
8
8
use netlink_packet_route:: {
9
9
route:: {
10
10
RouteAddress , RouteAttribute , RouteHeader , RouteMessage , RouteProtocol ,
11
- RouteScope , RouteType ,
11
+ RouteScope , RouteType , RouteVia ,
12
12
} ,
13
13
AddressFamily ,
14
14
} ;
@@ -153,10 +153,12 @@ impl RouteMessageBuilder<Ipv4Addr> {
153
153
}
154
154
155
155
/// Sets the gateway (via) address.
156
- pub fn gateway ( mut self , addr : Ipv4Addr ) -> Self {
157
- self . message
158
- . attributes
159
- . push ( RouteAttribute :: Gateway ( RouteAddress :: Inet ( addr) ) ) ;
156
+ pub fn gateway ( mut self , addr : IpAddr ) -> Self {
157
+ let attr = match addr {
158
+ IpAddr :: V4 ( v4) => RouteAttribute :: Gateway ( RouteAddress :: Inet ( v4) ) ,
159
+ IpAddr :: V6 ( v6) => RouteAttribute :: Via ( RouteVia :: Inet6 ( v6) ) ,
160
+ } ;
161
+ self . message . attributes . push ( attr) ;
160
162
self
161
163
}
162
164
}
@@ -351,25 +353,15 @@ impl RouteMessageBuilder<IpAddr> {
351
353
mut self ,
352
354
addr : IpAddr ,
353
355
) -> Result < Self , InvalidRouteMessage > {
354
- self . set_address_family_from_ip_addr ( addr) ;
355
- match self . message . header . address_family {
356
- AddressFamily :: Inet => {
357
- if addr. is_ipv6 ( ) {
358
- return Err ( InvalidRouteMessage :: Gateway ( addr) ) ;
359
- } ;
356
+ use AddressFamily :: * ;
357
+ let attr = match ( self . message . header . address_family , addr) {
358
+ ( Inet , addr @ IpAddr :: V4 ( _) ) | ( Inet6 , addr @ IpAddr :: V6 ( _) ) => {
359
+ RouteAttribute :: Gateway ( addr. into ( ) )
360
360
}
361
- AddressFamily :: Inet6 => {
362
- if addr. is_ipv4 ( ) {
363
- return Err ( InvalidRouteMessage :: Gateway ( addr) ) ;
364
- } ;
365
- }
366
- af => {
367
- return Err ( InvalidRouteMessage :: AddressFamily ( af) ) ;
368
- }
369
- }
370
- self . message
371
- . attributes
372
- . push ( RouteAttribute :: Gateway ( addr. into ( ) ) ) ;
361
+ ( Inet , IpAddr :: V6 ( v6) ) => RouteAttribute :: Via ( RouteVia :: Inet6 ( v6) ) ,
362
+ ( af, _) => return Err ( InvalidRouteMessage :: AddressFamily ( af) ) ,
363
+ } ;
364
+ self . message . attributes . push ( attr) ;
373
365
Ok ( self )
374
366
}
375
367
0 commit comments