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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Provide operations over IPv6 addresses.
use std::cmp::Ordering;
use std::fmt;
use std::io::IpAddr as StdIpAddr;
use std::ops::*;
use std::simd::u64x2;
use std::str::FromStr;
use super::IpAddrVersion::{self, Ipv6};

pub const MAX_PREFIXLEN: uint = 128;

#[derive(Copy, Clone, Show, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct IpAddr(pub u16, pub u16, pub u16, pub u16, pub u16, pub u16, pub u16, pub u16);

impl IpAddr {
    /// Get the corresponding IP address version.
    pub fn version(&self) -> IpAddrVersion {
        Ipv6
    }

    /// The total number of bits in the address representation for this version: `32` for IPv4, `128` for IPv6.
    ///
    /// The prefix defines the number of leading bits in an address that are compared to determine whether or not an address is part of a network.
    pub fn max_prefixlen(&self) -> uint {
        MAX_PREFIXLEN
    }

    /// Create an IP mask with the specified prefixlen.
    ///
    /// The provided prefixlen must be in the prefixlen-range (`0` <= `n` <= `128`).
    pub fn with_prefixlen(n: uint) -> IpAddr {
        assert!(n <= MAX_PREFIXLEN);
        let mid = MAX_PREFIXLEN / 2;
        if n >= mid {
            let n = n - mid;

            let hi = ::std::u64::MAX;
            let lo = (1 << n) - 1;
            let lo = lo << (mid - n);

            IpAddr::from_simd(u64x2(hi, lo))
        } else {
            let hi = (1 << n) - 1;
            let hi = hi << (MAX_PREFIXLEN - n);

            IpAddr::from_simd(u64x2(hi, 0))
        }
    }

    /// The binary representation of this address - a bytes vector of the appropriate length (most significant octet first).
    /// This is 4 bytes for IPv4 and 16 bytes for IPv6.
    pub fn packed(&self) -> [u8; 16] {
        let &IpAddr(a, b, c, d, e, f, g, h) = self;
        [
            ((a >> 8) & 0xff) as u8,
            (a & 0xff) as u8,
            ((b >> 8) & 0xff) as u8,
            (b & 0xff) as u8,
            ((c >> 8) & 0xff) as u8,
            (c & 0xff) as u8,
            ((d >> 8) & 0xff) as u8,
            (d & 0xff) as u8,
            ((e >> 8) & 0xff) as u8,
            (e & 0xff) as u8,
            ((f >> 8) & 0xff) as u8,
            (f & 0xff) as u8,
            ((g >> 8) & 0xff) as u8,
            (g & 0xff) as u8,
            ((h >> 8) & 0xff) as u8,
            (h & 0xff) as u8,
        ]
    }

    /// Create an `IpAddr` instance from a 128-bits integer.
    ///
    /// As Rust doesn't support u128-bits integer natively, this
    /// method take an array of two u64-bits integers
    pub fn from_u128(n: [u64; 2]) -> IpAddr {
        let a = (n[0] >> 48) & 0xffff;
        let b = (n[0] >> 32) & 0xffff;
        let c = (n[0] >> 16) & 0xffff;
        let d = n[0] & 0xffff;
        let e = (n[1] >> 48) & 0xffff;
        let f = (n[1] >> 32) & 0xffff;
        let g = (n[1] >> 16) & 0xffff;
        let h = n[1] & 0xffff;

        IpAddr(a as u16, b as u16, c as u16, d as u16,
                e as u16, f as u16, g as u16, h as u16)
    }

    /// Convert an `IpAddr` instance to a 128-bits integer.
    ///
    /// As Rust doesn't support u128-bits integer natively, this
    /// method return an array of two u64-bits integers
    pub fn to_u128(&self) -> [u64; 2] {
        let &IpAddr(a, b, c, d, e, f, g, h) = self;
        let (a, b, c, d) = (a as u64, b as u64, c as u64, d as u64);
        let (e, f, g, h) = (e as u64, f as u64, g as u64, h as u64);
        [(a << 48) | (b << 32) | (c << 16) | d, (e << 48) | (f << 32) | (g << 16) | h]
    }

    /// Create an `IpAddr` instance from a 128-bits SIMD integer.
    pub fn from_simd(n: u64x2) -> IpAddr {
        let u64x2(a, b) = n;
        IpAddr::from_u128([a, b])
    }

    /// Convert an `IpAddr` instance to a 128-bits SIMD integer.
    pub fn to_simd(&self) -> u64x2 {
        let [a, b] = self.to_u128();
        u64x2(a, b)
    }
}

impl Add<u64> for IpAddr {
    type Output = Self;

    fn add(self, rhs: u64) -> IpAddr {
        let u64x2(mut hi, mut lo) = self.to_simd();

        lo += rhs;
        if lo < rhs {
            hi += 1;
        }

        IpAddr::from_simd(u64x2(hi, lo))
    }
}

impl Sub<u64> for IpAddr {
    type Output = Self;

    fn sub(self, rhs: u64) -> IpAddr {
        let u64x2(mut hi, mut lo) = self.to_simd();

        lo -= rhs;
        if lo > rhs {
            hi -= 1;
        }

        IpAddr::from_simd(u64x2(hi, lo))
    }
}

impl BitXor<IpAddr> for IpAddr {
    type Output = Self;

    /// > Use SIMD to do operations on 128-bits integer.
    fn bitxor(self, rhs: IpAddr) -> IpAddr {
        IpAddr::from_simd(self.to_simd() ^ rhs.to_simd())
    }
}

impl BitOr<IpAddr> for IpAddr {
    type Output = Self;

    /// > Use SIMD to do operations on 128-bits integer.
    fn bitor(self, rhs: IpAddr) -> IpAddr {
        IpAddr::from_simd(self.to_simd() | rhs.to_simd())
    }
}

impl BitAnd<IpAddr> for IpAddr {
    type Output = Self;

    /// > Use SIMD to do operations on 128-bits integer.
    fn bitand(self, rhs: IpAddr) -> IpAddr {
        IpAddr::from_simd(self.to_simd() & rhs.to_simd())
    }
}

impl Not for IpAddr {
    type Output = Self;

    fn not(self) -> IpAddr {
        let u64x2(a, b) = self.to_simd();
        IpAddr::from_simd(u64x2(!a, !b))
    }
}

impl PartialOrd for IpAddr {
    fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
        self.to_u128().partial_cmp(&other.to_u128())
    }
}

impl Ord for IpAddr {
    fn cmp(&self, other: &IpAddr) -> Ordering {
        self.to_u128().cmp(&other.to_u128())
    }
}

///! Convert operations from/to Rust's standard library IP addresses.
impl IpAddr {
    /// Create an `ipv6::IpAddr` instance from a Rust's standard library `IpAddr` instance.
    pub fn from_std(ip: StdIpAddr) -> Option<IpAddr> {
        match ip {
            StdIpAddr::Ipv6Addr(a, b, c, d, e, f, g, h) =>
                Some(IpAddr(a, b, c, d, e, f, g, h)),
            _ =>
                None,
        }
    }

    /// Create a Rust's standard library `IpAddr` instance from an `ipv6::IpAddr` instance.
    pub fn to_std(&self) -> StdIpAddr {
        let &IpAddr(a, b, c, d, e, f, g, h) = self;
        StdIpAddr::Ipv6Addr(a, b, c, d, e, f, g, h)
    }

    /// Convert an `ipv6::IpAddr` instance into a Rust's standard library `IpAddr` instance.
    pub fn into_std(self) -> StdIpAddr {
        self.to_std()
    }
}

impl fmt::String for IpAddr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.to_std().fmt(f)
    }
}

impl FromStr for IpAddr {
    fn from_str(s: &str) -> Option<IpAddr> {
        let ip: Option<::std::io::IpAddr> = FromStr::from_str(s);
        ip.and_then(|ip| IpAddr::from_std(ip))
    }
}

#[cfg(test)]
mod test {
    use super::IpAddr;

    #[test]
    fn test_num() {
        let a = IpAddr(0, 0, 0, 0, 0, 0, 0, 1);
        let b = IpAddr(0, 0, 0, 0, 0, 0, 0, 2);
        let c = IpAddr(0, 0, 0, 0, 0xffff, 0xffff, 0xffff, 0xffff);
        let d = IpAddr(0, 0, 0, 1, 0, 0, 0, 0);

        assert_eq!(a + 1, b);
        assert_eq!(c + 1, d);

        assert_eq!(b - 1, a);
        assert_eq!(d - 1, c);

        assert!(a < b);
    }

    #[test]
    fn test_bitops() {
        let ip = IpAddr(12, 15, 18, 22, 32, 33, 34, 35);
        let mask = IpAddr(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0);

        assert_eq!(ip | mask, IpAddr(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 33, 34, 35));
        assert_eq!(ip & mask, IpAddr(12, 15, 18, 22, 32, 0, 0, 0));
        assert_eq!(!mask, IpAddr(0, 0, 0, 0, 0, 0xffff, 0xffff, 0xffff));
    }

    #[test]
    fn test_prefixlen() {
        assert_eq!(IpAddr::with_prefixlen(32), IpAddr(0xffff, 0xffff, 0, 0, 0, 0, 0, 0));
        assert_eq!(IpAddr::with_prefixlen(64), IpAddr(0xffff, 0xffff, 0xffff, 0xffff, 0, 0, 0, 0));
        assert_eq!(IpAddr::with_prefixlen(96), IpAddr(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0, 0));
    }
}