@@ -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,14 @@ impl RouteMessageBuilder<Ipv4Addr> {
168
168
. push ( RouteAttribute :: Gateway ( RouteAddress :: Inet ( addr) ) ) ;
169
169
self
170
170
}
171
+
172
+ /// Sets the IPv6 gateway (via) address.
173
+ pub fn via ( mut self , addr : Ipv6Addr ) -> Self {
174
+ self . message
175
+ . attributes
176
+ . push ( RouteAttribute :: Via ( RouteVia :: Inet6 ( addr) ) ) ;
177
+ self
178
+ }
171
179
}
172
180
173
181
impl Default for RouteMessageBuilder < Ipv4Addr > {
@@ -360,25 +368,15 @@ impl RouteMessageBuilder<IpAddr> {
360
368
mut self ,
361
369
addr : IpAddr ,
362
370
) -> 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
- } ;
371
+ use AddressFamily :: * ;
372
+ let attr = match ( self . message . header . address_family , addr) {
373
+ ( Inet , addr @ IpAddr :: V4 ( _) ) | ( Inet6 , addr @ IpAddr :: V6 ( _) ) => {
374
+ RouteAttribute :: Gateway ( addr. into ( ) )
369
375
}
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 ( ) ) ) ;
376
+ ( Inet , IpAddr :: V6 ( v6) ) => RouteAttribute :: Via ( RouteVia :: Inet6 ( v6) ) ,
377
+ ( af, _) => return Err ( InvalidRouteMessage :: AddressFamily ( af) ) ,
378
+ } ;
379
+ self . message . attributes . push ( attr) ;
382
380
Ok ( self )
383
381
}
384
382
0 commit comments