Skip to content

Commit f59979f

Browse files
Merge branch 'rust-netlink:main' into ipvlan_and_ipvtap
2 parents 3f3859b + a4d2611 commit f59979f

File tree

9 files changed

+428
-214
lines changed

9 files changed

+428
-214
lines changed

examples/add_neighbour.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
use futures::stream::TryStreamExt;
44
use rtnetlink::{new_connection, Error, Handle};
5-
use std::{env, net::IpAddr};
5+
use std::{convert::TryFrom, env, net::IpAddr};
66

77
#[tokio::main]
88
async fn main() -> Result<(), ()> {
99
let args: Vec<String> = env::args().collect();
10-
if args.len() != 3 {
10+
if args.len() != 4 {
1111
usage();
1212
return Ok(());
1313
}
@@ -18,10 +18,27 @@ async fn main() -> Result<(), ()> {
1818
std::process::exit(1);
1919
});
2020

21+
let link_local_parts: Vec<u8> = args[3]
22+
.split(':')
23+
.map(|b| {
24+
u8::from_str_radix(b, 16).unwrap_or_else(|e| {
25+
eprintln!("invalid part of mac {}: {}", b, e);
26+
std::process::exit(1);
27+
})
28+
})
29+
.collect();
30+
31+
let link_local_address = <[u8; 6]>::try_from(link_local_parts).unwrap_or_else(|_| {
32+
eprintln!("invalid mac address, please give it in the format of 56:78:90:ab:cd:ef");
33+
std::process::exit(1);
34+
});
35+
2136
let (connection, handle, _) = new_connection().unwrap();
2237
tokio::spawn(connection);
2338

24-
if let Err(e) = add_neighbour(link_name, ip, handle.clone()).await {
39+
if let Err(e) =
40+
add_neighbour(link_name, ip, link_local_address, handle.clone()).await
41+
{
2542
eprintln!("{e}");
2643
}
2744
Ok(())
@@ -30,6 +47,7 @@ async fn main() -> Result<(), ()> {
3047
async fn add_neighbour(
3148
link_name: &str,
3249
ip: IpAddr,
50+
link_local_address: [u8; 6],
3351
handle: Handle,
3452
) -> Result<(), Error> {
3553
let mut links = handle
@@ -41,6 +59,7 @@ async fn add_neighbour(
4159
handle
4260
.neighbours()
4361
.add(link.header.index, ip)
62+
.link_local_address(&link_local_address)
4463
.execute()
4564
.await?;
4665
println!("Done");
@@ -52,7 +71,7 @@ async fn add_neighbour(
5271
fn usage() {
5372
eprintln!(
5473
"usage:
55-
cargo run --example add_neighbour -- <link_name> <ip_address>
74+
cargo run --example add_neighbour -- <link_name> <ip_address> <mac>
5675
5776
Note that you need to run this program as root. Instead of running cargo as root,
5877
build the example normally:
@@ -61,6 +80,7 @@ build the example normally:
6180
6281
Then find the binary in the target directory:
6382
64-
cd ../target/debug/example ; sudo ./add_neighbour <link_name> <ip_address>"
83+
cd ../target/debug/example ;
84+
sudo ./add_neighbour <link_name> <ip_address> <mac>"
6585
);
6686
}

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);

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
66
#![allow(clippy::module_inception)]
77

8+
pub use netlink_packet_core as packet_core;
9+
pub use netlink_packet_route as packet_route;
10+
pub use netlink_packet_utils as packet_utils;
11+
pub use netlink_proto as proto;
12+
pub use netlink_sys as sys;
13+
814
mod handle;
915
pub use crate::handle::*;
1016

0 commit comments

Comments
 (0)