diff options
Diffstat (limited to 'src/route.rs')
-rw-r--r-- | src/route.rs | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/src/route.rs b/src/route.rs index b3e7cc9..0e5cd5d 100644 --- a/src/route.rs +++ b/src/route.rs @@ -2,10 +2,99 @@ use crate::error::{Error, Result}; use std::net::{Ipv4Addr, Ipv6Addr}; +use futures::future; use futures_util::TryStreamExt; -use netlink_packet_route::rtnl::RT_SCOPE_LINK; +use netlink_packet_route::{RouteMessage, RT_SCOPE_LINK}; +use rtnetlink::IpVersion; use tokio::runtime::Runtime; +async fn do_flush4(link: String) -> Result<()> { + let (conn, handle, _) = rtnetlink::new_connection()?; + tokio::spawn(conn); + + let link = handle + .link() + .get() + .match_name(link.clone()) + .execute() + .try_next() + .await? + .ok_or(Error::LinkNotFound(link))?; + + let id = link.header.index; + + let routes: Vec<RouteMessage> = handle + .route() + .get(IpVersion::V4) + .execute() + .try_filter(|route| { + future::ready(if let Some(ifi) = route.output_interface() { + ifi == id + } else { + false + }) + }) + .try_collect() + .await?; + + for route in routes { + handle.route().del(route).execute().await?; + } + + Ok(()) +} + +pub fn flush4(link: String) -> Result<()> { + Runtime::new()?.block_on(do_flush4(link)) +} + +async fn do_flush6(link: String) -> Result<()> { + let (conn, handle, _) = rtnetlink::new_connection()?; + tokio::spawn(conn); + + let link = handle + .link() + .get() + .match_name(link.clone()) + .execute() + .try_next() + .await? + .ok_or(Error::LinkNotFound(link))?; + + let id = link.header.index; + + let routes: Vec<RouteMessage> = handle + .route() + .get(IpVersion::V6) + .execute() + .try_filter(|route| { + future::ready(if let Some(ifi) = route.output_interface() { + ifi == id + } else { + false + }) + }) + .try_collect() + .await?; + + for route in routes { + handle.route().del(route).execute().await?; + } + + Ok(()) +} + +pub fn flush6(link: String) -> Result<()> { + Runtime::new()?.block_on(do_flush6(link)) +} + +pub fn flush(link: String) -> Result<()> { + flush4(link.clone())?; + flush6(link)?; + + Ok(()) +} + async fn do_add4(dst: Ipv4Addr, prefix_len: u8, rtr: Option<Ipv4Addr>, link: String) -> Result<()> { let (conn, handle, _) = rtnetlink::new_connection()?; tokio::spawn(conn); |