diff options
Diffstat (limited to 'src/link.rs')
-rw-r--r-- | src/link.rs | 110 |
1 files changed, 24 insertions, 86 deletions
diff --git a/src/link.rs b/src/link.rs index d936dd9..20c319e 100644 --- a/src/link.rs +++ b/src/link.rs @@ -1,21 +1,19 @@ use crate::{Error, Result}; -use std::num::NonZeroI32; -use std::thread; use std::time::Duration; +use tokio::time::sleep; + use futures::TryStreamExt; use netlink_packet_route::rtnl::IFF_UP; -use rtnetlink::Error::NetlinkError; -use tokio::runtime::Runtime; -#[derive(Clone, Copy, Debug)] -enum State { +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +pub enum LinkState { Up, Down, } -async fn set(link: String, state: State) -> Result<()> { +pub async fn set(link: String, state: LinkState) -> Result<()> { let (conn, handle, _) = rtnetlink::new_connection()?; tokio::spawn(conn); @@ -31,8 +29,8 @@ async fn set(link: String, state: State) -> Result<()> { let id = link.header.index; match state { - State::Up => handle.link().set(id).up(), - State::Down => handle.link().set(id).down(), + LinkState::Up => handle.link().set(id).up(), + LinkState::Down => handle.link().set(id).down(), } .execute() .await?; @@ -40,15 +38,7 @@ async fn set(link: String, state: State) -> Result<()> { Ok(()) } -// pub fn up(link: String) -> Result<()> { -// Runtime::new()?.block_on(set(link, State::Up)) -// } -// -// pub fn down(link: String) -> Result<()> { -// Runtime::new()?.block_on(set(link, State::Down)) -// } - -async fn do_is_up(link: String) -> Result<bool> { +pub async fn is_up(link: String) -> Result<bool> { let (conn, handle, _) = rtnetlink::new_connection()?; tokio::spawn(conn); @@ -65,11 +55,7 @@ async fn do_is_up(link: String) -> Result<bool> { Ok(is_up) } -// pub fn is_up(link: String) -> Result<bool> { -// Runtime::new()?.block_on(do_is_up(link)) -// } - -async fn do_set_mtu(link: String, mtu: u32) -> Result<()> { +pub async fn set_mtu(link: String, mtu: u32) -> Result<()> { let (conn, handle, _) = rtnetlink::new_connection()?; tokio::spawn(conn); @@ -88,11 +74,7 @@ async fn do_set_mtu(link: String, mtu: u32) -> Result<()> { Ok(()) } -// pub fn set_mtu(link: String, mtu: u32) -> Result<()> { -// Runtime::new()?.block_on(do_set_mtu(link, mtu)) -// } - -async fn do_add_vlan(link: String, parent: String, vlan_id: u16) -> Result<()> { +pub async fn add_vlan(link: String, parent: String, vlan_id: u16) -> Result<()> { let (conn, handle, _) = rtnetlink::new_connection()?; tokio::spawn(conn); @@ -117,35 +99,15 @@ async fn do_add_vlan(link: String, parent: String, vlan_id: u16) -> Result<()> { Ok(()) } -// pub fn add_vlan(link: String, parent: String, vlan_id: u16) -> Result<()> { -// Runtime::new()?.block_on(do_add_vlan(link, parent, vlan_id)) -// } - -// pub fn wait_up(link: String) -> Result<()> { -// while !match is_up(link.clone()) { -// Ok(v) => v, -// Err(e) => { -// if let Error::LinkNotFound(_) = e { -// false -// } else if let Error::RtNetlink(NetlinkError(ref msg)) = e { -// // Error -19 is "No such device". -// if msg.code == NonZeroI32::new(-19) { -// false -// } else { -// return Err(e); -// } -// } else { -// return Err(e); -// } -// } -// } { -// thread::sleep(Duration::from_secs(1)); -// } -// -// Ok(()) -// } - -async fn do_exists(link: String) -> Result<bool> { +pub async fn wait_up(link: String) -> Result<()> { + while !exists(link.clone()).await? || !is_up(link.clone()).await? { + sleep(Duration::from_millis(200)).await; + } + + Ok(()) +} + +pub async fn exists(link: String) -> Result<bool> { let (conn, handle, _) = rtnetlink::new_connection()?; tokio::spawn(conn); @@ -161,34 +123,10 @@ async fn do_exists(link: String) -> Result<bool> { Ok(exists) } -// pub fn exists(link: String) -> Result<bool> { -// Runtime::new()?.block_on(do_exists(link)) -// } -// -// pub fn wait_exists(link: String) -> Result<()> { -// while !exists(link.clone())? { -// thread::sleep(Duration::from_secs(1)); -// } -// -// Ok(()) -// } - -async fn do_index(link: String) -> Result<u32> { - 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))?; +pub async fn wait_exists(link: String) -> Result<()> { + while !exists(link.clone()).await? { + sleep(Duration::from_millis(200)).await; + } - Ok(link.header.index) + Ok(()) } - -// pub fn index(link: String) -> Result<u32> { -// Runtime::new()?.block_on(do_index(link)) -// } |