use std::net::IpAddr;
mod ipv4_global {
use std::net::Ipv4Addr;
#[must_use]
#[inline]
pub(crate) const fn is_reserved(a: Ipv4Addr) -> bool {
a.octets()[0] & 240 == 240 && !a.is_broadcast()
}
#[must_use]
#[inline]
pub(crate) const fn is_benchmarking(a: Ipv4Addr) -> bool {
a.octets()[0] == 198 && (a.octets()[1] & 0xfe) == 18
}
#[must_use]
#[inline]
pub(crate) const fn is_shared(a: Ipv4Addr) -> bool {
a.octets()[0] == 100 && (a.octets()[1] & 0b1100_0000 == 0b0100_0000)
}
#[must_use]
#[inline]
pub(crate) const fn is_private(a: Ipv4Addr) -> bool {
match a.octets() {
[10, ..] => true,
[172, b, ..] if b >= 16 && b <= 31 => true,
[192, 168, ..] => true,
_ => false,
}
}
#[must_use]
#[inline]
pub(crate) const fn is_global(a: Ipv4Addr) -> bool {
!(a.octets()[0] == 0 || is_private(a)
|| is_shared(a)
|| a.is_loopback()
|| a.is_link_local()
||(a.octets()[0] == 192 && a.octets()[1] == 0 && a.octets()[2] == 0)
|| a.is_documentation()
|| is_benchmarking(a)
|| is_reserved(a)
|| a.is_broadcast())
}
}
mod ipv6_global {
use std::net::Ipv6Addr;
#[must_use]
#[inline]
pub(crate) const fn is_unicast_link_local(a: Ipv6Addr) -> bool {
(a.segments()[0] & 0xffc0) == 0xfe80
}
#[must_use]
#[inline]
pub(crate) const fn is_unique_local(a: Ipv6Addr) -> bool {
(a.segments()[0] & 0xfe00) == 0xfc00
}
#[must_use]
#[inline]
pub(crate) const fn is_documentation(a: Ipv6Addr) -> bool {
(a.segments()[0] == 0x2001) && (a.segments()[1] == 0xdb8)
}
#[must_use]
#[inline]
pub(crate) const fn is_global(a: Ipv6Addr) -> bool {
!(a.is_unspecified()
|| a.is_loopback()
|| matches!(a.segments(), [0, 0, 0, 0, 0, 0xffff, _, _])
|| matches!(a.segments(), [0x64, 0xff9b, 1, _, _, _, _, _])
|| matches!(a.segments(), [0x100, 0, 0, 0, _, _, _, _])
|| (matches!(a.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200)
&& !(
u128::from_be_bytes(a.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001
|| u128::from_be_bytes(a.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002
|| matches!(a.segments(), [0x2001, 3, _, _, _, _, _, _])
|| matches!(a.segments(), [0x2001, 4, 0x112, _, _, _, _, _])
|| matches!(a.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x2F)
))
|| is_documentation(a)
|| is_unique_local(a)
|| is_unicast_link_local(a))
}
}
pub(crate) fn is_global_ip(ip: &IpAddr) -> bool {
match ip {
IpAddr::V4(ipv4) => ipv4_global::is_global(*ipv4),
IpAddr::V6(ipv6) => ipv6_global::is_global(*ipv6),
}
}
#[allow(dead_code)]
pub(crate) fn is_private_ip(ip: &IpAddr) -> bool {
match ip {
IpAddr::V4(ipv4) => ipv4_global::is_private(*ipv4),
IpAddr::V6(ipv6) => ipv6_global::is_unique_local(*ipv6),
}
}