@@ -8,7 +8,7 @@ use std::{
8
8
use netlink_packet_route:: {
9
9
route:: {
10
10
RouteAddress , RouteAttribute , RouteFlags , RouteHeader , RouteMessage ,
11
- RouteProtocol , RouteScope , RouteType ,
11
+ RouteProtocol , RouteScope , RouteType , RouteVia ,
12
12
} ,
13
13
AddressFamily ,
14
14
} ;
@@ -168,6 +168,13 @@ impl RouteMessageBuilder<Ipv4Addr> {
168
168
. push ( RouteAttribute :: Gateway ( RouteAddress :: Inet ( addr) ) ) ;
169
169
self
170
170
}
171
+
172
+ pub fn gateway6 ( mut self , addr : Ipv6Addr ) -> Self {
173
+ self . message
174
+ . attributes
175
+ . push ( RouteAttribute :: Via ( RouteVia :: Inet6 ( addr) ) ) ;
176
+ self
177
+ }
171
178
}
172
179
173
180
impl Default for RouteMessageBuilder < Ipv4Addr > {
@@ -360,25 +367,15 @@ impl RouteMessageBuilder<IpAddr> {
360
367
mut self ,
361
368
addr : IpAddr ,
362
369
) -> Result < Self , InvalidRouteMessage > {
363
- self . set_address_family_from_ip_addr ( addr) ;
364
- match self . message . header . address_family {
365
- AddressFamily :: Inet => {
366
- if addr. is_ipv6 ( ) {
367
- return Err ( InvalidRouteMessage :: Gateway ( addr) ) ;
368
- } ;
370
+ use AddressFamily :: * ;
371
+ let attr = match ( self . message . header . address_family , addr) {
372
+ ( Inet , addr @ IpAddr :: V4 ( _) ) | ( Inet6 , addr @ IpAddr :: V6 ( _) ) => {
373
+ RouteAttribute :: Gateway ( addr. into ( ) )
369
374
}
370
- AddressFamily :: Inet6 => {
371
- if addr. is_ipv4 ( ) {
372
- return Err ( InvalidRouteMessage :: Gateway ( addr) ) ;
373
- } ;
374
- }
375
- af => {
376
- return Err ( InvalidRouteMessage :: AddressFamily ( af) ) ;
377
- }
378
- }
379
- self . message
380
- . attributes
381
- . push ( RouteAttribute :: Gateway ( addr. into ( ) ) ) ;
375
+ ( Inet , IpAddr :: V6 ( v6) ) => RouteAttribute :: Via ( RouteVia :: Inet6 ( v6) ) ,
376
+ ( af, _) => return Err ( InvalidRouteMessage :: AddressFamily ( af) ) ,
377
+ } ;
378
+ self . message . attributes . push ( attr) ;
382
379
Ok ( self )
383
380
}
384
381
0 commit comments