Skip to content

Commit ef77d05

Browse files
committed
Move the RouteAddRequest method into a separate builder
For both the `RouteAddRequest` and `RouteDelRequest`, we need to build a route message. However only the former provides helper methods to do so. Instead of duplicating code, create a separate builder, and use it for both request types.
1 parent 9d913ea commit ef77d05

File tree

7 files changed

+396
-358
lines changed

7 files changed

+396
-358
lines changed

examples/add_route.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-License-Identifier: MIT
22

3-
use std::env;
3+
use std::{env, net::Ipv4Addr};
44

55
use ipnetwork::Ipv4Network;
6-
use rtnetlink::{new_connection, Error, Handle};
6+
use rtnetlink::{new_connection, Error, Handle, RouteMessageBuilder};
77

88
const TEST_TABLE_ID: u32 = 299;
99

@@ -40,15 +40,12 @@ async fn add_route(
4040
gateway: &Ipv4Network,
4141
handle: Handle,
4242
) -> Result<(), Error> {
43-
let route = handle.route();
44-
route
45-
.add()
46-
.v4()
43+
let route = RouteMessageBuilder::<Ipv4Addr>::new()
4744
.destination_prefix(dest.ip(), dest.prefix())
4845
.gateway(gateway.ip())
4946
.table_id(TEST_TABLE_ID)
50-
.execute()
51-
.await?;
47+
.build();
48+
handle.route().add(route).execute().await?;
5249
Ok(())
5350
}
5451

examples/add_route_pref_src.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use futures::TryStreamExt;
44
use std::{env, net::Ipv4Addr};
55

66
use ipnetwork::Ipv4Network;
7-
use rtnetlink::{new_connection, Error, Handle};
7+
use rtnetlink::{new_connection, Error, Handle, RouteMessageBuilder};
88

99
#[tokio::main]
1010
async fn main() -> Result<(), ()> {
@@ -53,15 +53,12 @@ async fn add_route(
5353
.header
5454
.index;
5555

56-
let route = handle.route();
57-
route
58-
.add()
59-
.v4()
56+
let route = RouteMessageBuilder::<Ipv4Addr>::new()
6057
.destination_prefix(dest.ip(), dest.prefix())
6158
.output_interface(iface_idx)
6259
.pref_source(source)
63-
.execute()
64-
.await?;
60+
.build();
61+
handle.route().add(route).execute().await?;
6562
Ok(())
6663
}
6764

examples/listen.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
use futures::stream::StreamExt;
77
use netlink_sys::{AsyncSocket, SocketAddr};
88
use rtnetlink::{
9-
constants::{RTMGRP_IPV4_ROUTE, RTMGRP_IPV6_ROUTE},
9+
constants::{
10+
RTMGRP_IPV4_IFADDR, RTMGRP_IPV4_ROUTE, RTMGRP_IPV6_IFADDR,
11+
RTMGRP_IPV6_ROUTE, RTMGRP_LINK,
12+
},
1013
new_connection,
1114
};
1215

@@ -18,7 +21,11 @@ async fn main() -> Result<(), String> {
1821

1922
// These flags specify what kinds of broadcast messages we want to listen
2023
// for.
21-
let mgroup_flags = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
24+
let mgroup_flags = RTMGRP_LINK
25+
| RTMGRP_IPV4_IFADDR
26+
| RTMGRP_IPV4_ROUTE
27+
| RTMGRP_IPV6_IFADDR
28+
| RTMGRP_IPV6_ROUTE;
2229

2330
// A netlink socket address is created with said flags.
2431
let addr = SocketAddr::new(0, mgroup_flags);

0 commit comments

Comments
 (0)