nex_core/
ip.rs

1//! IP address utilities.
2
3use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
4
5/// Returns [`true`] if the address appears to be globally routable.
6pub fn is_global_ip(ip_addr: &IpAddr) -> bool {
7    match ip_addr {
8        IpAddr::V4(ip) => is_global_ipv4(ip),
9        IpAddr::V6(ip) => is_global_ipv6(ip),
10    }
11}
12
13/// Returns [`true`] if the address appears to be globally reachable
14/// as specified by the [IANA IPv4 Special-Purpose Address Registry].
15pub fn is_global_ipv4(ipv4_addr: &Ipv4Addr) -> bool {
16    !(ipv4_addr.octets()[0] == 0 // "This network"
17        || ipv4_addr.is_private()
18        || is_shared_ipv4(ipv4_addr)
19        || ipv4_addr.is_loopback()
20        || ipv4_addr.is_link_local()
21        // addresses reserved for future protocols (`192.0.0.0/24`)
22        // .9 and .10 are documented as globally reachable so they're excluded
23        || (
24            ipv4_addr.octets()[0] == 192 && ipv4_addr.octets()[1] == 0 && ipv4_addr.octets()[2] == 0
25            && ipv4_addr.octets()[3] != 9 && ipv4_addr.octets()[3] != 10
26        )
27        || ipv4_addr.is_documentation()
28        || is_benchmarking_ipv4(ipv4_addr)
29        || is_reserved_ipv4(ipv4_addr)
30        || ipv4_addr.is_broadcast())
31}
32
33/// Returns [`true`] if the address appears to be globally reachable
34/// as specified by the [IANA IPv6 Special-Purpose Address Registry].
35pub fn is_global_ipv6(ipv6_addr: &Ipv6Addr) -> bool {
36    !(ipv6_addr.is_unspecified()
37        || ipv6_addr.is_loopback()
38        // IPv4-mapped Address (`::ffff:0:0/96`)
39        || matches!(ipv6_addr.segments(), [0, 0, 0, 0, 0, 0xffff, _, _])
40        // IPv4-IPv6 Translat. (`64:ff9b:1::/48`)
41        || matches!(ipv6_addr.segments(), [0x64, 0xff9b, 1, _, _, _, _, _])
42        // Discard-Only Address Block (`100::/64`)
43        || matches!(ipv6_addr.segments(), [0x100, 0, 0, 0, _, _, _, _])
44        // IETF Protocol Assignments (`2001::/23`)
45        || (matches!(ipv6_addr.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200)
46            && !(
47                // Port Control Protocol Anycast (`2001:1::1`)
48                u128::from_be_bytes(ipv6_addr.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001
49                // Traversal Using Relays around NAT Anycast (`2001:1::2`)
50                || u128::from_be_bytes(ipv6_addr.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002
51                // AMT (`2001:3::/32`)
52                || matches!(ipv6_addr.segments(), [0x2001, 3, _, _, _, _, _, _])
53                // AS112-v6 (`2001:4:112::/48`)
54                || matches!(ipv6_addr.segments(), [0x2001, 4, 0x112, _, _, _, _, _])
55                // ORCHIDv2 (`2001:20::/28`)
56                // Drone Remote ID Protocol Entity Tags (DETs) Prefix (`2001:30::/28`)`
57                || matches!(ipv6_addr.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x3F)
58            ))
59        // 6to4 (`2002::/16`) - it's not explicitly documented as globally reachable,
60        // IANA says N/A.
61        || matches!(ipv6_addr.segments(), [0x2002, _, _, _, _, _, _, _])
62        || is_documentation_ipv6(ipv6_addr)
63        || ipv6_addr.is_unique_local()
64        || ipv6_addr.is_unicast_link_local())
65}
66
67/// Returns [`true`] if this address is part of the Shared Address Space defined in
68/// [IETF RFC 6598] (`100.64.0.0/10`).
69///
70/// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598
71fn is_shared_ipv4(ipv4_addr: &Ipv4Addr) -> bool {
72    ipv4_addr.octets()[0] == 100 && (ipv4_addr.octets()[1] & 0b1100_0000 == 0b0100_0000)
73}
74
75/// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for
76/// network devices benchmarking.
77fn is_benchmarking_ipv4(ipv4_addr: &Ipv4Addr) -> bool {
78    ipv4_addr.octets()[0] == 198 && (ipv4_addr.octets()[1] & 0xfe) == 18
79}
80
81/// Returns [`true`] if this address is reserved by IANA for future use.
82fn is_reserved_ipv4(ipv4_addr: &Ipv4Addr) -> bool {
83    ipv4_addr.octets()[0] & 240 == 240 && !ipv4_addr.is_broadcast()
84}
85
86/// Returns [`true`] if this is an address reserved for documentation
87/// (`2001:db8::/32` and `3fff::/20`).
88fn is_documentation_ipv6(ipv6_addr: &Ipv6Addr) -> bool {
89    matches!(
90        ipv6_addr.segments(),
91        [0x2001, 0xdb8, ..] | [0x3fff, 0..=0x0fff, ..]
92    )
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
99
100    #[test]
101    fn test_is_global_ipv4() {
102        let global = Ipv4Addr::new(1, 1, 1, 1); // Cloudflare
103        let private = Ipv4Addr::new(192, 168, 1, 1);
104        let loopback = Ipv4Addr::new(127, 0, 0, 1);
105        let shared = Ipv4Addr::new(100, 64, 0, 1); // RFC6598
106        let doc = Ipv4Addr::new(192, 0, 2, 1); // Documentation
107
108        assert!(is_global_ipv4(&global));
109        assert!(!is_global_ipv4(&private));
110        assert!(!is_global_ipv4(&loopback));
111        assert!(!is_global_ipv4(&shared));
112        assert!(!is_global_ipv4(&doc));
113    }
114
115    #[test]
116    fn test_is_global_ipv6() {
117        let global = Ipv6Addr::new(0x2606, 0x4700, 0, 0, 0, 0, 0, 0x1111); // Cloudflare
118        let loopback = Ipv6Addr::LOCALHOST;
119        let unspecified = Ipv6Addr::UNSPECIFIED;
120        let unique_local = Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 1);
121        let doc = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1); // Documentation
122
123        assert!(is_global_ipv6(&global));
124        assert!(!is_global_ipv6(&loopback));
125        assert!(!is_global_ipv6(&unspecified));
126        assert!(!is_global_ipv6(&unique_local));
127        assert!(!is_global_ipv6(&doc));
128    }
129
130    #[test]
131    fn test_is_global_ip() {
132        let ip_v4 = IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1));
133        let ip_v6 = IpAddr::V6(Ipv6Addr::new(0x2606, 0x4700, 0, 0, 0, 0, 0, 0x1111)); // Cloudflare
134        let ip_private = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
135        let ip_ula = IpAddr::V6(Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 1));
136
137        assert!(is_global_ip(&ip_v4));
138        assert!(is_global_ip(&ip_v6));
139        assert!(!is_global_ip(&ip_private));
140        assert!(!is_global_ip(&ip_ula));
141    }
142}