1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::net::{IpAddr, SocketAddr};

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum SocketAddrSpace {
    Unspecified,
    Global,
}

impl SocketAddrSpace {
    pub fn new(allow_private_addr: bool) -> Self {
        if allow_private_addr {
            SocketAddrSpace::Unspecified
        } else {
            SocketAddrSpace::Global
        }
    }

    /// Returns true if the IP address is valid.
    pub fn check(&self, addr: &SocketAddr) -> bool {
        if self == &SocketAddrSpace::Unspecified {
            return true;
        }
        // TODO: remove these once IpAddr::is_global is stable.
        match addr.ip() {
            IpAddr::V4(addr) => {
                // TODO: Consider excluding:
                //    addr.is_loopback() || addr.is_link_local()
                // || addr.is_broadcast() || addr.is_documentation()
                // || addr.is_unspecified()
                !addr.is_private()
            }
            IpAddr::V6(_) => {
                // TODO: Consider excluding:
                // addr.is_loopback() || addr.is_unspecified(),
                true
            }
        }
    }
}