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
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use get_if_addrs::{self, IfAddr};
use priv_prelude::*;

/// Some helpful additional methods for `IpvAddr`.
pub trait IpAddrExt {
    /// Check whether an IP address is global.
    fn is_global(&self) -> bool;

    /// Check whether an IP address belongs to a private subnet.
    fn is_private(&self) -> bool;

    /// If the IP address is an unspecified address (eg. `0.0.0.0`), then it is expanded into a
    /// vector with a seperate IP address for each network interface.
    fn expand_local_unspecified(&self) -> io::Result<Vec<IpAddr>>;

    /// If this is the unspecified address then map it to the localhost address.
    fn unspecified_to_localhost(&self) -> IpAddr;
}

impl IpAddrExt for IpAddr {
    fn is_global(&self) -> bool {
        match *self {
            IpAddr::V4(ref ip) => Ipv4AddrExt::is_global(ip),
            IpAddr::V6(ref ip) => Ipv6AddrExt::is_global(ip),
        }
    }

    fn is_private(&self) -> bool {
        match *self {
            //IpAddr::V4(ref ip) => Ipv4AddrExt::is_private(ip),
            IpAddr::V4(ref ip) => ip.is_private(),
            IpAddr::V6(_) => false,
        }
    }

    fn expand_local_unspecified(&self) -> io::Result<Vec<IpAddr>> {
        let ret = match *self {
            IpAddr::V4(v4_addr) => v4_addr
                .expand_local_unspecified()?
                .into_iter()
                .map(IpAddr::V4)
                .collect(),
            IpAddr::V6(v6_addr) => v6_addr
                .expand_local_unspecified()?
                .into_iter()
                .map(IpAddr::V6)
                .collect(),
        };
        Ok(ret)
    }

    fn unspecified_to_localhost(&self) -> IpAddr {
        match *self {
            IpAddr::V4(ref ip) => IpAddr::V4(ip.unspecified_to_localhost()),
            IpAddr::V6(ref ip) => IpAddr::V6(ip.unspecified_to_localhost()),
        }
    }
}

/// Some helpful additional methods for `Ipv4Addr`.
pub trait Ipv4AddrExt {
    /// Check whether an IP address is global.
    fn is_global(&self) -> bool;

    /// If the IP address is the unspecified address `0.0.0.0`, then it is expanded into a vector
    /// with a seperate IP address for each network interface.
    fn expand_local_unspecified(&self) -> io::Result<Vec<Ipv4Addr>>;

    /// Convert the unspecified address 0.0.0.0 to 127.0.0.1
    fn unspecified_to_localhost(&self) -> Ipv4Addr;
}

/// Some helpful additional methods for `Ipv6Addr`.
pub trait Ipv6AddrExt {
    /// Check whether an IP address is global.
    fn is_global(&self) -> bool;

    /// If the IP address is the unspecified address `::`, then it is expanded into a vector with a
    /// seperate IP address for each network interface.
    fn expand_local_unspecified(&self) -> io::Result<Vec<Ipv6Addr>>;

    /// Convert the unspecified address :: to ::1
    fn unspecified_to_localhost(&self) -> Ipv6Addr;
}

impl Ipv4AddrExt for Ipv4Addr {
    fn is_global(&self) -> bool {
        !self.is_private()
            && !self.is_loopback()
            && !self.is_link_local()
            && !self.is_broadcast()
            && !self.is_documentation()
            && !self.is_unspecified()
    }

    fn expand_local_unspecified(&self) -> io::Result<Vec<Ipv4Addr>> {
        if !self.is_unspecified() {
            return Ok(vec![*self]);
        }

        let mut ret = Vec::new();
        let ifs = get_if_addrs::get_if_addrs()?;
        for interface in ifs {
            if let IfAddr::V4(v4_addr) = interface.addr {
                ret.push(v4_addr.ip);
            }
        }
        Ok(ret)
    }

    fn unspecified_to_localhost(&self) -> Ipv4Addr {
        if self.is_unspecified() {
            ipv4!("127.0.0.1")
        } else {
            *self
        }
    }
}

impl Ipv6AddrExt for Ipv6Addr {
    fn is_global(&self) -> bool {
        // TODO: this is very incomplete
        !self.is_loopback() && !self.is_unspecified()
    }

    fn expand_local_unspecified(&self) -> io::Result<Vec<Ipv6Addr>> {
        if !self.is_unspecified() {
            return Ok(vec![*self]);
        }

        let mut ret = Vec::new();
        let ifs = get_if_addrs::get_if_addrs()?;
        for interface in ifs {
            if let IfAddr::V6(v6_addr) = interface.addr {
                ret.push(v6_addr.ip);
            }
        }
        Ok(ret)
    }

    fn unspecified_to_localhost(&self) -> Ipv6Addr {
        if self.is_unspecified() {
            ipv6!("::1")
        } else {
            *self
        }
    }
}