aboutsummaryrefslogtreecommitdiff
path: root/src/addr.rs
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-11-14 17:35:54 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-11-14 17:35:54 +0100
commit1c2a4a8ace82f3ed0696e112b0434301538b3500 (patch)
treecfa2eb80f7f1cbcbbc67ba6b2f7bfbb568b5cdba /src/addr.rs
parente1d63ff533bd1b56ac5394635c96aa354308d859 (diff)
initial split
Diffstat (limited to 'src/addr.rs')
-rw-r--r--src/addr.rs189
1 files changed, 189 insertions, 0 deletions
diff --git a/src/addr.rs b/src/addr.rs
new file mode 100644
index 0000000..5c3ba34
--- /dev/null
+++ b/src/addr.rs
@@ -0,0 +1,189 @@
+use crate::{Error, Result};
+
+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<()> {
+ 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 addrs: Vec<AddressMessage> = handle
+ .address()
+ .get()
+ .set_link_index_filter(id)
+ .execute()
+ .try_collect()
+ .await?;
+
+ for addr in addrs {
+ handle.address().del(addr).execute().await?;
+ }
+
+ Ok(())
+}
+
+// pub fn flush(link: String) -> Result<()> {
+// Runtime::new()?.block_on(do_flush(link))
+// }
+
+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 addrs: Vec<AddressMessage> = handle
+ .address()
+ .get()
+ .set_link_index_filter(id)
+ .execute()
+ .try_filter(|addr| future::ready(addr.header.family == AF_INET as u8))
+ .try_collect()
+ .await?;
+
+ for addr in addrs {
+ handle.address().del(addr).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 addrs: Vec<AddressMessage> = handle
+ .address()
+ .get()
+ .set_link_index_filter(id)
+ .execute()
+ .try_filter(|addr| future::ready(addr.header.family == AF_INET6 as u8))
+ .try_collect()
+ .await?;
+
+ for addr in addrs {
+ handle.address().del(addr).execute().await?;
+ }
+
+ Ok(())
+}
+
+// pub fn flush6(link: String) -> Result<()> {
+// Runtime::new()?.block_on(do_flush6(link))
+// }
+
+async fn do_flush6_global() -> Result<()> {
+ let (conn, handle, _) = rtnetlink::new_connection()?;
+ tokio::spawn(conn);
+
+ let addrs: Vec<AddressMessage> = handle
+ .address()
+ .get()
+ .execute()
+ .try_filter(|addr| {
+ future::ready(
+ addr.header.family == AF_INET6 as u8 && addr.header.scope == RT_SCOPE_UNIVERSE,
+ )
+ })
+ .try_collect()
+ .await?;
+
+ for addr in addrs {
+ handle.address().del(addr).execute().await?;
+ }
+
+ 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<()> {
+ 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;
+
+ handle.address().add(id, addr, prefix_len).execute().await?;
+
+ 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<()> {
+ 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 mut req = handle.address().add(id, addr, prefix_len);
+ req.message_mut().header.scope = RT_SCOPE_LINK;
+
+ req.execute().await?;
+
+ 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))
+// }