diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2022-10-23 19:03:50 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2022-10-23 19:03:50 +0200 |
commit | 8bdd05973df9baa05abfc13dfe935b1ce3e8915f (patch) | |
tree | 970c0daaf19b14e756ba8973afe3d203521b23df | |
parent | f5268691c33bbc8ac332495ecf98a1e1259f179e (diff) |
add ipv6 link-local support
-rw-r--r-- | src/lib.rs | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -11,6 +11,7 @@ use socket2::{Domain, Socket, Type}; pub enum Error { IoError(std::io::Error), WrongIpVer(String, IpAddr), + NoLinkLocal(Ipv6Addr), NoUla(Ipv6Addr), NoGua(Ipv6Addr), } @@ -26,6 +27,9 @@ impl fmt::Display for Error { Self::WrongIpVer(want, got) => { write!(fmt, "wrong ip version: expected {}, got {}", want, got) } + Self::NoLinkLocal(ip) => { + write!(fmt, "ipv6 address {} is not a link-local address", ip) + } Self::NoUla(ip) => write!(fmt, "ipv6 address {} is not a ula", ip), Self::NoGua(ip) => write!(fmt, "ipv6 address {} is not a gua", ip), } @@ -57,6 +61,18 @@ fn get_ipv6(interface: &str, network: &str) -> Result<Ipv6Addr> { } } +/// Get the (preferred outgoing) IPv6 link-local address +/// of the given interface. +pub fn get_ipv6_unicast_link_local(interface: &str) -> Result<Ipv6Addr> { + let ipv6 = get_ipv6(interface, "fe80::")?; + + if ipv6.is_unicast_link_local() { + Ok(ipv6) + } else { + Err(Error::NoLinkLocal(ipv6)) + } +} + /// Get the preferred outgoing IPv6 ULA of the given interface. pub fn get_ipv6_unique_local(interface: &str) -> Result<Ipv6Addr> { let ipv6 = get_ipv6(interface, "fc00::")?; |