aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-03-19 19:31:37 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-03-19 19:31:37 +0100
commit0e342ddf332bfc871c72ea315ebf45bc8ad8d177 (patch)
treeae1f6233709f99642b0b275fa732469a04f5e757 /src
parent00d06a7035f81670ac0bdf71ff803bee5dd8668d (diff)
add is_up api
Diffstat (limited to 'src')
-rw-r--r--src/link.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/link.rs b/src/link.rs
index 8810a35..b14d7fd 100644
--- a/src/link.rs
+++ b/src/link.rs
@@ -1,6 +1,7 @@
use crate::error::{Error, Result};
use futures_util::TryStreamExt;
+use netlink_packet_route::rtnl::IFF_UP;
use tokio::runtime::Runtime;
#[derive(Clone, Copy, Debug)]
@@ -41,3 +42,24 @@ pub fn up(link: String) -> Result<()> {
pub fn down(link: String) -> Result<()> {
Runtime::new()?.block_on(set(link, State::Down))
}
+
+async fn do_is_up(link: String) -> Result<bool> {
+ 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 is_up = link.header.flags & IFF_UP == IFF_UP;
+ Ok(is_up)
+}
+
+pub fn is_up(link: String) -> Result<bool> {
+ Runtime::new()?.block_on(do_is_up(link))
+}