aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-11-14 18:48:08 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-11-14 18:48:08 +0100
commit64702d5bb846660a3df6556192828daee286cb1b (patch)
tree733111a04916874698d51fcfbf6d2b7e4a7f6a9f
parent50ed7ad4abf7b58e33cfd0743b41c916d2e0a1dd (diff)
add blocking api behind crate feature
-rw-r--r--Cargo.toml3
-rw-r--r--src/blocking.rs55
-rw-r--r--src/lib.rs3
3 files changed, 61 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index cb23c79..eedf5f4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,3 +12,6 @@ netlink-packet-route = "^0.17"
rtnetlink = { version = "0.13.1" }
thiserror = "1.0"
tokio = { version = "1.0", features = ["time"] }
+
+[features]
+blocking = ["tokio/rt-multi-thread"]
diff --git a/src/blocking.rs b/src/blocking.rs
new file mode 100644
index 0000000..3a54ac4
--- /dev/null
+++ b/src/blocking.rs
@@ -0,0 +1,55 @@
+pub use crate::tunnel;
+
+macro_rules! blockify {
+ ($blk:ident, $r:path) => {
+ pub fn $blk() -> crate::Result<()> {
+ tokio::runtime::Runtime::new()?.block_on($r())
+ }
+ };
+ ($blk:ident, $r:path, $($v:tt: $t:ty),*) => {
+ pub fn $blk($($v: $t),*) -> crate::Result<()> {
+ tokio::runtime::Runtime::new()?.block_on($r($($v),*))
+ }
+ };
+ ($blk:ident -> $ret:ty, $r:path, $($v:tt: $t:ty),*) => {
+ pub fn $blk($($v: $t),*) -> crate::Result<$ret> {
+ tokio::runtime::Runtime::new()?.block_on($r($($v),*))
+ }
+ };
+}
+
+pub mod addr {
+ use crate::addr;
+
+ use std::net::IpAddr;
+
+ blockify!(flush, addr::flush, link: String);
+ blockify!(flush4, addr::flush4, link: String);
+ blockify!(flush6, addr::flush6, link: String);
+ blockify!(flush6_global, addr::flush6_global);
+ blockify!(add, addr::add, link: String, addr: IpAddr, prefix_len: u8);
+ blockify!(add_link_local, addr::add_link_local, link: String, addr: IpAddr, prefix_len: u8);
+}
+
+pub mod link {
+ use crate::link::{self, LinkState};
+
+ blockify!(set, link::set, link: String, state: LinkState);
+ blockify!(is_up -> bool, link::is_up, link: String);
+ blockify!(set_mtu, link::set_mtu, link: String, mtu: u32);
+ blockify!(add_vlan, link::add_vlan, link: String, parent: String, vlan_id: u16);
+ blockify!(wait_up, link::wait_up, link: String);
+ blockify!(exists -> bool, link::exists, link: String);
+ blockify!(wait_exists, link::wait_exists, link: String);
+}
+
+pub mod route {
+ use crate::route;
+
+ use std::net::{Ipv4Addr, Ipv6Addr};
+
+ blockify!(flush4, route::flush4, link: String);
+ blockify!(flush6, route::flush6, link: String);
+ blockify!(add4, route::add4, dst: Ipv4Addr, prefix_len: u8, rtr: Option<Ipv4Addr>, link: String);
+ blockify!(add6, route::add6, dst: Ipv6Addr, prefix_len: u8, rtr: Option<Ipv6Addr>, link: String);
+}
diff --git a/src/lib.rs b/src/lib.rs
index 4f06d33..ea23a46 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,3 +5,6 @@ pub mod addr;
pub mod link;
pub mod route;
pub mod tunnel;
+
+#[cfg(feature = "blocking")]
+pub mod blocking;