-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdump_links_async.rs
44 lines (37 loc) · 1.36 KB
/
dump_links_async.rs
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
// SPDX-License-Identifier: MIT
use futures::StreamExt;
use netlink_packet_core::{
NetlinkHeader, NetlinkMessage, NLM_F_DUMP, NLM_F_REQUEST,
};
use netlink_packet_route::{link::LinkMessage, RouteNetlinkMessage};
use netlink_proto::{
new_connection,
sys::{protocols::NETLINK_ROUTE, SocketAddr},
};
#[tokio::main]
async fn main() -> Result<(), String> {
// Create the netlink socket. Here, we won't use the channel that
// receives unsolicited messages.
let (conn, handle, _) = new_connection(NETLINK_ROUTE).map_err(|e| {
format!("Failed to create a new netlink connection: {}", e)
})?;
// Spawn the `Connection` so that it starts polling the netlink
// socket in the background.
async_std::task::spawn(conn);
// Create the netlink message that requests the links to be dumped
let mut nl_hdr = NetlinkHeader::default();
nl_hdr.flags = NLM_F_DUMP | NLM_F_REQUEST;
let request = NetlinkMessage::new(
nl_hdr,
RouteNetlinkMessage::GetLink(LinkMessage::default()).into(),
);
// Send the request
let mut response = handle
.request(request, SocketAddr::new(0, 0))
.map_err(|e| format!("Failed to send request: {}", e))?;
// Print all the messages received in response
while let Some(packet) = response.next().await {
println!("<<< {:?}", packet);
}
Ok(())
}