1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
// SPDX-License-Identifier: MIT
#![cfg(feature = "tokio_socket")]
use futures::TryStreamExt;
use std::{env, net::Ipv4Addr};
use ipnetwork::Ipv4Network;
use rtnetlink::{new_connection, Error, Handle};
#[tokio::main]
async fn main() -> Result<(), ()> {
let args: Vec<String> = env::args().collect();
if args.len() != 4 {
usage();
return Ok(());
}
let dest: Ipv4Network = args[1].parse().unwrap_or_else(|_| {
eprintln!("invalid destination");
std::process::exit(1);
});
let iface: String = args[2].parse().unwrap_or_else(|_| {
eprintln!("invalid interface");
std::process::exit(1);
});
let source: Ipv4Addr = args[3].parse().unwrap_or_else(|_| {
eprintln!("invalid source");
std::process::exit(1);
});
let (connection, handle, _) = new_connection().unwrap();
tokio::spawn(connection);
if let Err(e) = add_route(&dest, iface, source, handle.clone()).await {
eprintln!("{e}");
}
Ok(())
}
async fn add_route(
dest: &Ipv4Network,
iface: String,
source: Ipv4Addr,
handle: Handle,
) -> Result<(), Error> {
let iface_idx = handle
.link()
.get()
.match_name(iface)
.execute()
.try_next()
.await?
.unwrap()
.header
.index;
let route = handle.route();
route
.add()
.v4()
.destination_prefix(dest.ip(), dest.prefix())
.output_interface(iface_idx)
.pref_source(source)
.execute()
.await?;
Ok(())
}
fn usage() {
eprintln!(
"usage:
cargo run --example add_route_pref_src -- <destination>/<prefix_length> <interface> <source>
Note that you need to run this program as root. Instead of running cargo as root,
build the example normally:
cd rtnetlink ; cargo build --example add_route_pref_src
Then find the binary in the target directory:
cd ../target/debug/example ; sudo ./add_route_pref_src <destination>/<prefix_length> <interface> <source>"
);
}
|