Skip to content

Commit

Permalink
Move the RouteAddRequest method into a separate builder
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
little-dude committed Apr 24, 2024
1 parent 628a5cb commit 9049f59
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 358 deletions.
13 changes: 5 additions & 8 deletions examples/add_route.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: MIT

use std::env;
use std::{env, net::Ipv4Addr};

use ipnetwork::Ipv4Network;
use rtnetlink::{new_connection, Error, Handle};
use rtnetlink::{new_connection, Error, Handle, RouteMessageBuilder};

const TEST_TABLE_ID: u32 = 299;

Expand Down Expand Up @@ -40,15 +40,12 @@ async fn add_route(
gateway: &Ipv4Network,
handle: Handle,
) -> Result<(), Error> {
let route = handle.route();
route
.add()
.v4()
let route = RouteMessageBuilder::<Ipv4Addr>::new()
.destination_prefix(dest.ip(), dest.prefix())
.gateway(gateway.ip())
.table_id(TEST_TABLE_ID)
.execute()
.await?;
.build();
handle.route().add(route).execute().await?;
Ok(())
}

Expand Down
11 changes: 4 additions & 7 deletions examples/add_route_pref_src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use futures::TryStreamExt;
use std::{env, net::Ipv4Addr};

use ipnetwork::Ipv4Network;
use rtnetlink::{new_connection, Error, Handle};
use rtnetlink::{new_connection, Error, Handle, RouteMessageBuilder};

#[tokio::main]
async fn main() -> Result<(), ()> {
Expand Down Expand Up @@ -53,15 +53,12 @@ async fn add_route(
.header
.index;

let route = handle.route();
route
.add()
.v4()
let route = RouteMessageBuilder::<Ipv4Addr>::new()
.destination_prefix(dest.ip(), dest.prefix())
.output_interface(iface_idx)
.pref_source(source)
.execute()
.await?;
.build();
handle.route().add(route).execute().await?;
Ok(())
}

Expand Down
11 changes: 9 additions & 2 deletions examples/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use futures::stream::StreamExt;
use netlink_sys::{AsyncSocket, SocketAddr};
use rtnetlink::{
constants::{RTMGRP_IPV4_ROUTE, RTMGRP_IPV6_ROUTE},
constants::{
RTMGRP_IPV4_IFADDR, RTMGRP_IPV4_ROUTE, RTMGRP_IPV6_IFADDR,
RTMGRP_IPV6_ROUTE, RTMGRP_LINK,
},
new_connection,
};

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

// These flags specify what kinds of broadcast messages we want to listen
// for.
let mgroup_flags = RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE;
let mgroup_flags = RTMGRP_LINK
| RTMGRP_IPV4_IFADDR
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV6_IFADDR
| RTMGRP_IPV6_ROUTE;

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

0 comments on commit 9049f59

Please sign in to comment.