aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-11-14 17:58:56 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-11-14 17:58:56 +0100
commitb4ff762972ec928fdbcad10a51a1a0b6a7c0fd3f (patch)
tree2056173ea8d3ead1450dd8982bde8e9fea1d34ba
parent1c2a4a8ace82f3ed0696e112b0434301538b3500 (diff)
expose async api
-rw-r--r--Cargo.toml2
-rw-r--r--src/addr.rs37
-rw-r--r--src/link.rs110
-rw-r--r--src/route.rs42
4 files changed, 45 insertions, 146 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 359ead8..aa3d321 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,4 +13,4 @@ netlink-packet-route = "^0.17"
rtnetlink = { version = "0.13.1" }
serde_json = "1.0"
thiserror = "1.0"
-tokio = "1.0"
+tokio = { version = "1.0", features = ["time"] }
diff --git a/src/addr.rs b/src/addr.rs
index 5c3ba34..ed68fcc 100644
--- a/src/addr.rs
+++ b/src/addr.rs
@@ -4,9 +4,8 @@ use std::net::IpAddr;
use futures::{future, TryStreamExt};
use netlink_packet_route::{AddressMessage, AF_INET, AF_INET6, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE};
-use tokio::runtime::Runtime;
-async fn do_flush(link: String) -> Result<()> {
+pub async fn flush(link: String) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -36,11 +35,7 @@ async fn do_flush(link: String) -> Result<()> {
Ok(())
}
-// pub fn flush(link: String) -> Result<()> {
-// Runtime::new()?.block_on(do_flush(link))
-// }
-
-async fn do_flush4(link: String) -> Result<()> {
+pub async fn flush4(link: String) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -71,11 +66,7 @@ async fn do_flush4(link: String) -> Result<()> {
Ok(())
}
-// pub fn flush4(link: String) -> Result<()> {
-// Runtime::new()?.block_on(do_flush4(link))
-// }
-
-async fn do_flush6(link: String) -> Result<()> {
+pub async fn flush6(link: String) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -106,11 +97,7 @@ async fn do_flush6(link: String) -> Result<()> {
Ok(())
}
-// pub fn flush6(link: String) -> Result<()> {
-// Runtime::new()?.block_on(do_flush6(link))
-// }
-
-async fn do_flush6_global() -> Result<()> {
+pub async fn flush6_global() -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -133,11 +120,7 @@ async fn do_flush6_global() -> Result<()> {
Ok(())
}
-// pub fn flush6_global() -> Result<()> {
-// Runtime::new()?.block_on(do_flush6_global())
-// }
-
-async fn do_add(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
+pub async fn add(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -157,11 +140,7 @@ async fn do_add(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
Ok(())
}
-// pub fn add(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
-// Runtime::new()?.block_on(do_add(link, addr, prefix_len))
-// }
-
-async fn do_add_link_local(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
+pub async fn add_link_local(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -183,7 +162,3 @@ async fn do_add_link_local(link: String, addr: IpAddr, prefix_len: u8) -> Result
Ok(())
}
-
-// pub fn add_link_local(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
-// Runtime::new()?.block_on(do_add_link_local(link, addr, prefix_len))
-// }
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))
-// }
diff --git a/src/route.rs b/src/route.rs
index b180e96..cd5e032 100644
--- a/src/route.rs
+++ b/src/route.rs
@@ -5,9 +5,8 @@ use std::net::{Ipv4Addr, Ipv6Addr};
use futures::{future, TryStreamExt};
use netlink_packet_route::{RouteMessage, RT_SCOPE_LINK};
use rtnetlink::IpVersion;
-use tokio::runtime::Runtime;
-async fn do_flush4(link: String) -> Result<()> {
+pub async fn flush4(link: String) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -43,11 +42,7 @@ async fn do_flush4(link: String) -> Result<()> {
Ok(())
}
-// pub fn flush4(link: String) -> Result<()> {
-// Runtime::new()?.block_on(do_flush4(link))
-// }
-
-async fn do_flush6(link: String) -> Result<()> {
+pub async fn flush6(link: String) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -83,18 +78,12 @@ async fn do_flush6(link: String) -> Result<()> {
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<()> {
+pub async fn add4(
+ dst: Ipv4Addr,
+ prefix_len: u8,
+ rtr: Option<Ipv4Addr>,
+ link: String,
+) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -126,11 +115,12 @@ async fn do_add4(dst: Ipv4Addr, prefix_len: u8, rtr: Option<Ipv4Addr>, link: Str
Ok(())
}
-// pub fn add4(dst: Ipv4Addr, prefix_len: u8, rtr: Option<Ipv4Addr>, link: String) -> Result<()> {
-// Runtime::new()?.block_on(do_add4(dst, prefix_len, rtr, link))
-// }
-
-async fn do_add6(dst: Ipv6Addr, prefix_len: u8, rtr: Option<Ipv6Addr>, link: String) -> Result<()> {
+pub async fn add6(
+ dst: Ipv6Addr,
+ prefix_len: u8,
+ rtr: Option<Ipv6Addr>,
+ link: String,
+) -> Result<()> {
let (conn, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(conn);
@@ -161,7 +151,3 @@ async fn do_add6(dst: Ipv6Addr, prefix_len: u8, rtr: Option<Ipv6Addr>, link: Str
add.execute().await?;
Ok(())
}
-
-// pub fn add6(dst: Ipv6Addr, prefix_len: u8, rtr: Option<Ipv6Addr>, link: String) -> Result<()> {
-// Runtime::new()?.block_on(do_add6(dst, prefix_len, rtr, link))
-// }