use nlink::netlink::{Connection, Route};
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> nlink::Result<()> {
let conn = Connection::<Route>::new()?;
let mut count = 0usize;
let mut stream = conn.stream_links().await?;
while let Some(link) = stream.next().await {
let link = link?;
count += 1;
if count <= 5 {
println!(
"link {:>3}: {:<16} mtu={}",
link.ifindex(),
link.name_or("?"),
link.mtu().map(|m| m.to_string()).unwrap_or_else(|| "-".into()),
);
}
}
println!("link stream: {count} total\n");
count = 0;
let mut stream = conn.stream_routes().await?;
while let Some(route) = stream.next().await {
let route = route?;
count += 1;
if count <= 5 {
println!(
"route: dst={:?} oif={:?} table={}",
route.destination(),
route.oif(),
route.table_id(),
);
}
}
println!("route stream: {count} total");
println!("\nMemory note: peak buffer use is one socket batch \
(~8KiB by default), not the full dump. For BGP-scale \
route tables (1M+ routes) this is the difference \
between a working tool and an OOM kill.");
Ok(())
}