aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-05-07 12:21:42 +0200
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-05-07 12:21:42 +0200
commit3be501c827bce8300e44a12656a2e8c605cc4ad0 (patch)
tree0c0259116dc5a07afa08d5d9bd27cdf4f663828f
parentdd053bc60bba68c8754905c3968d16fb9ba98514 (diff)
add helper function to add a link-local address
-rw-r--r--src/addr.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/addr.rs b/src/addr.rs
index 1cd0301..36ddea1 100644
--- a/src/addr.rs
+++ b/src/addr.rs
@@ -4,6 +4,7 @@ use std::net::IpAddr;
use futures_util::TryStreamExt;
use netlink_packet_route::rtnl::AddressMessage;
+use netlink_packet_route::RT_SCOPE_LINK;
use tokio::runtime::Runtime;
async fn do_flush(link: String) -> Result<()> {
@@ -63,3 +64,30 @@ async fn do_add(link: String, addr: IpAddr, prefix_len: u8) -> Result<()> {
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))
+}