]> git.lizzy.rs Git - rust.git/blob - src/libstd/net/ip.rs
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[rust.git] / src / libstd / net / ip.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![unstable(feature = "ip", reason = "extra functionality has not been \
12                                       scrutinized to the level that it should \
13                                       be stable",
14             issue = "27709")]
15
16 use cmp::Ordering;
17 use fmt;
18 use hash;
19 use mem;
20 use net::{hton, ntoh};
21 use sys::net::netc as c;
22 use sys_common::{AsInner, FromInner};
23
24 /// An IP address, either an IPv4 or IPv6 address.
25 ///
26 /// # Examples
27 ///
28 /// Constructing an IPv4 address:
29 ///
30 /// ```
31 /// use std::net::{IpAddr, Ipv4Addr};
32 ///
33 /// IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
34 /// ```
35 ///
36 /// Constructing an IPv6 address:
37 ///
38 /// ```
39 /// use std::net::{IpAddr, Ipv6Addr};
40 ///
41 /// IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
42 /// ```
43 #[stable(feature = "ip_addr", since = "1.7.0")]
44 #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
45 pub enum IpAddr {
46     /// Representation of an IPv4 address.
47     #[stable(feature = "ip_addr", since = "1.7.0")]
48     V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
49     /// Representation of an IPv6 address.
50     #[stable(feature = "ip_addr", since = "1.7.0")]
51     V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
52 }
53
54 /// Representation of an IPv4 address.
55 #[derive(Copy)]
56 #[stable(feature = "rust1", since = "1.0.0")]
57 pub struct Ipv4Addr {
58     inner: c::in_addr,
59 }
60
61 /// Representation of an IPv6 address.
62 #[derive(Copy)]
63 #[stable(feature = "rust1", since = "1.0.0")]
64 pub struct Ipv6Addr {
65     inner: c::in6_addr,
66 }
67
68 #[allow(missing_docs)]
69 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
70 pub enum Ipv6MulticastScope {
71     InterfaceLocal,
72     LinkLocal,
73     RealmLocal,
74     AdminLocal,
75     SiteLocal,
76     OrganizationLocal,
77     Global
78 }
79
80 impl IpAddr {
81     /// Returns true for the special 'unspecified' address ([IPv4], [IPv6]).
82     ///
83     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
84     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
85     ///
86     /// # Examples
87     ///
88     /// ```
89     /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
90     ///
91     /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
92     /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
93     /// ```
94     #[stable(feature = "ip_shared", since = "1.12.0")]
95     pub fn is_unspecified(&self) -> bool {
96         match *self {
97             IpAddr::V4(ref a) => a.is_unspecified(),
98             IpAddr::V6(ref a) => a.is_unspecified(),
99         }
100     }
101
102     /// Returns true if this is a loopback address ([IPv4], [IPv6]).
103     ///
104     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
105     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
106     ///
107     /// # Examples
108     ///
109     /// ```
110     /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
111     ///
112     /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
113     /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
114     /// ```
115     #[stable(feature = "ip_shared", since = "1.12.0")]
116     pub fn is_loopback(&self) -> bool {
117         match *self {
118             IpAddr::V4(ref a) => a.is_loopback(),
119             IpAddr::V6(ref a) => a.is_loopback(),
120         }
121     }
122
123     /// Returns true if the address appears to be globally routable ([IPv4], [IPv6]).
124     ///
125     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
126     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
127     ///
128     /// # Examples
129     ///
130     /// ```
131     /// #![feature(ip)]
132     ///
133     /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
134     ///
135     /// fn main() {
136     ///     assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
137     ///     assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(),
138     ///                true);
139     /// }
140     /// ```
141     pub fn is_global(&self) -> bool {
142         match *self {
143             IpAddr::V4(ref a) => a.is_global(),
144             IpAddr::V6(ref a) => a.is_global(),
145         }
146     }
147
148     /// Returns true if this is a multicast address ([IPv4], [IPv6]).
149     ///
150     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
151     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
152     ///
153     /// # Examples
154     ///
155     /// ```
156     /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
157     ///
158     /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
159     /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
160     /// ```
161     #[stable(feature = "ip_shared", since = "1.12.0")]
162     pub fn is_multicast(&self) -> bool {
163         match *self {
164             IpAddr::V4(ref a) => a.is_multicast(),
165             IpAddr::V6(ref a) => a.is_multicast(),
166         }
167     }
168
169     /// Returns true if this address is in a range designated for documentation ([IPv4], [IPv6]).
170     ///
171     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
172     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
173     ///
174     /// # Examples
175     ///
176     /// ```
177     /// #![feature(ip)]
178     ///
179     /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
180     ///
181     /// fn main() {
182     ///     assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
183     ///     assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0))
184     ///                       .is_documentation(), true);
185     /// }
186     /// ```
187     pub fn is_documentation(&self) -> bool {
188         match *self {
189             IpAddr::V4(ref a) => a.is_documentation(),
190             IpAddr::V6(ref a) => a.is_documentation(),
191         }
192     }
193
194     /// Returns true if this address is a valid IPv4 address, false if it's a valid IPv6 address.
195     ///
196     /// # Examples
197     ///
198     /// ```
199     /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
200     ///
201     /// fn main() {
202     ///     assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
203     ///     assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(),
204     ///                false);
205     /// }
206     /// ```
207     #[stable(feature = "ipaddr_checker", since = "1.16.0")]
208     pub fn is_ipv4(&self) -> bool {
209         match *self {
210             IpAddr::V4(_) => true,
211             IpAddr::V6(_) => false,
212         }
213     }
214
215     /// Returns true if this address is a valid IPv6 address, false if it's a valid IPv4 address.
216     ///
217     /// # Examples
218     ///
219     /// ```
220     /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
221     ///
222     /// fn main() {
223     ///     assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
224     ///     assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(),
225     ///                true);
226     /// }
227     /// ```
228     #[stable(feature = "ipaddr_checker", since = "1.16.0")]
229     pub fn is_ipv6(&self) -> bool {
230         match *self {
231             IpAddr::V4(_) => false,
232             IpAddr::V6(_) => true,
233         }
234     }
235 }
236
237 impl Ipv4Addr {
238     /// Creates a new IPv4 address from four eight-bit octets.
239     ///
240     /// The result will represent the IP address `a`.`b`.`c`.`d`.
241     ///
242     /// # Examples
243     ///
244     /// ```
245     /// use std::net::Ipv4Addr;
246     ///
247     /// let addr = Ipv4Addr::new(127, 0, 0, 1);
248     /// ```
249     #[stable(feature = "rust1", since = "1.0.0")]
250     pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
251         Ipv4Addr {
252             inner: c::in_addr {
253                 s_addr: hton(((a as u32) << 24) |
254                              ((b as u32) << 16) |
255                              ((c as u32) <<  8) |
256                               (d as u32)),
257             }
258         }
259     }
260
261     /// Returns the four eight-bit integers that make up this address.
262     ///
263     /// # Examples
264     ///
265     /// ```
266     /// use std::net::Ipv4Addr;
267     ///
268     /// let addr = Ipv4Addr::new(127, 0, 0, 1);
269     /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
270     /// ```
271     #[stable(feature = "rust1", since = "1.0.0")]
272     pub fn octets(&self) -> [u8; 4] {
273         let bits = ntoh(self.inner.s_addr);
274         [(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
275     }
276
277     /// Returns true for the special 'unspecified' address (0.0.0.0).
278     ///
279     /// This property is defined in _UNIX Network Programming, Second Edition_,
280     /// W. Richard Stevens, p. 891; see also [ip7].
281     ///
282     /// [ip7]: http://man7.org/linux/man-pages/man7/ip.7.html
283     ///
284     /// # Examples
285     ///
286     /// ```
287     /// use std::net::Ipv4Addr;
288     ///
289     /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
290     /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
291     /// ```
292     #[stable(feature = "ip_shared", since = "1.12.0")]
293     pub fn is_unspecified(&self) -> bool {
294         self.inner.s_addr == 0
295     }
296
297     /// Returns true if this is a loopback address (127.0.0.0/8).
298     ///
299     /// This property is defined by [RFC 1122].
300     ///
301     /// [RFC 1122]: https://tools.ietf.org/html/rfc1122
302     ///
303     /// # Examples
304     ///
305     /// ```
306     /// use std::net::Ipv4Addr;
307     ///
308     /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
309     /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
310     /// ```
311     #[stable(since = "1.7.0", feature = "ip_17")]
312     pub fn is_loopback(&self) -> bool {
313         self.octets()[0] == 127
314     }
315
316     /// Returns true if this is a private address.
317     ///
318     /// The private address ranges are defined in [RFC 1918] and include:
319     ///
320     ///  - 10.0.0.0/8
321     ///  - 172.16.0.0/12
322     ///  - 192.168.0.0/16
323     ///
324     /// [RFC 1918]: https://tools.ietf.org/html/rfc1918
325     ///
326     /// # Examples
327     ///
328     /// ```
329     /// use std::net::Ipv4Addr;
330     ///
331     /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
332     /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
333     /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
334     /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
335     /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
336     /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
337     /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
338     /// ```
339     #[stable(since = "1.7.0", feature = "ip_17")]
340     pub fn is_private(&self) -> bool {
341         match (self.octets()[0], self.octets()[1]) {
342             (10, _) => true,
343             (172, b) if b >= 16 && b <= 31 => true,
344             (192, 168) => true,
345             _ => false
346         }
347     }
348
349     /// Returns true if the address is link-local (169.254.0.0/16).
350     ///
351     /// This property is defined by [RFC 3927].
352     ///
353     /// [RFC 3927]: https://tools.ietf.org/html/rfc3927
354     ///
355     /// # Examples
356     ///
357     /// ```
358     /// use std::net::Ipv4Addr;
359     ///
360     /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
361     /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
362     /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
363     /// ```
364     #[stable(since = "1.7.0", feature = "ip_17")]
365     pub fn is_link_local(&self) -> bool {
366         self.octets()[0] == 169 && self.octets()[1] == 254
367     }
368
369     /// Returns true if the address appears to be globally routable.
370     /// See [iana-ipv4-special-registry][ipv4-sr].
371     ///
372     /// The following return false:
373     ///
374     /// - private address (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16)
375     /// - the loopback address (127.0.0.0/8)
376     /// - the link-local address (169.254.0.0/16)
377     /// - the broadcast address (255.255.255.255/32)
378     /// - test addresses used for documentation (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24)
379     /// - the unspecified address (0.0.0.0)
380     ///
381     /// [ipv4-sr]: http://goo.gl/RaZ7lg
382     ///
383     /// # Examples
384     ///
385     /// ```
386     /// #![feature(ip)]
387     ///
388     /// use std::net::Ipv4Addr;
389     ///
390     /// fn main() {
391     ///     assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
392     ///     assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
393     ///     assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
394     ///     assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
395     ///     assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
396     /// }
397     /// ```
398     pub fn is_global(&self) -> bool {
399         !self.is_private() && !self.is_loopback() && !self.is_link_local() &&
400         !self.is_broadcast() && !self.is_documentation() && !self.is_unspecified()
401     }
402
403     /// Returns true if this is a multicast address (224.0.0.0/4).
404     ///
405     /// Multicast addresses have a most significant octet between 224 and 239,
406     /// and is defined by [RFC 5771].
407     ///
408     /// [RFC 5771]: https://tools.ietf.org/html/rfc5771
409     ///
410     /// # Examples
411     ///
412     /// ```
413     /// use std::net::Ipv4Addr;
414     ///
415     /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
416     /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
417     /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
418     /// ```
419     #[stable(since = "1.7.0", feature = "ip_17")]
420     pub fn is_multicast(&self) -> bool {
421         self.octets()[0] >= 224 && self.octets()[0] <= 239
422     }
423
424     /// Returns true if this is a broadcast address (255.255.255.255).
425     ///
426     /// A broadcast address has all octets set to 255 as defined in [RFC 919].
427     ///
428     /// [RFC 919]: https://tools.ietf.org/html/rfc919
429     ///
430     /// # Examples
431     ///
432     /// ```
433     /// use std::net::Ipv4Addr;
434     ///
435     /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
436     /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
437     /// ```
438     #[stable(since = "1.7.0", feature = "ip_17")]
439     pub fn is_broadcast(&self) -> bool {
440         self.octets()[0] == 255 && self.octets()[1] == 255 &&
441         self.octets()[2] == 255 && self.octets()[3] == 255
442     }
443
444     /// Returns true if this address is in a range designated for documentation.
445     ///
446     /// This is defined in [RFC 5737]:
447     ///
448     /// - 192.0.2.0/24 (TEST-NET-1)
449     /// - 198.51.100.0/24 (TEST-NET-2)
450     /// - 203.0.113.0/24 (TEST-NET-3)
451     ///
452     /// [RFC 5737]: https://tools.ietf.org/html/rfc5737
453     ///
454     /// # Examples
455     ///
456     /// ```
457     /// use std::net::Ipv4Addr;
458     ///
459     /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
460     /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
461     /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
462     /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
463     /// ```
464     #[stable(since = "1.7.0", feature = "ip_17")]
465     pub fn is_documentation(&self) -> bool {
466         match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) {
467             (192, 0, 2, _) => true,
468             (198, 51, 100, _) => true,
469             (203, 0, 113, _) => true,
470             _ => false
471         }
472     }
473
474     /// Converts this address to an IPv4-compatible IPv6 address.
475     ///
476     /// a.b.c.d becomes ::a.b.c.d
477     ///
478     /// # Examples
479     ///
480     /// ```
481     /// use std::net::{Ipv4Addr, Ipv6Addr};
482     ///
483     /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
484     ///            Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 767));
485     /// ```
486     #[stable(feature = "rust1", since = "1.0.0")]
487     pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
488         Ipv6Addr::new(0, 0, 0, 0, 0, 0,
489                       ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
490                       ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
491     }
492
493     /// Converts this address to an IPv4-mapped IPv6 address.
494     ///
495     /// a.b.c.d becomes ::ffff:a.b.c.d
496     ///
497     /// # Examples
498     ///
499     /// ```
500     /// use std::net::{Ipv4Addr, Ipv6Addr};
501     ///
502     /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
503     ///            Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 49152, 767));
504     /// ```
505     #[stable(feature = "rust1", since = "1.0.0")]
506     pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
507         Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff,
508                       ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
509                       ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
510     }
511 }
512
513 #[stable(feature = "ip_addr", since = "1.7.0")]
514 impl fmt::Display for IpAddr {
515     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
516         match *self {
517             IpAddr::V4(ref a) => a.fmt(fmt),
518             IpAddr::V6(ref a) => a.fmt(fmt),
519         }
520     }
521 }
522
523 #[stable(feature = "ip_from_ip", since = "1.16.0")]
524 impl From<Ipv4Addr> for IpAddr {
525     fn from(ipv4: Ipv4Addr) -> IpAddr {
526         IpAddr::V4(ipv4)
527     }
528 }
529
530 #[stable(feature = "ip_from_ip", since = "1.16.0")]
531 impl From<Ipv6Addr> for IpAddr {
532     fn from(ipv6: Ipv6Addr) -> IpAddr {
533         IpAddr::V6(ipv6)
534     }
535 }
536
537 #[stable(feature = "rust1", since = "1.0.0")]
538 impl fmt::Display for Ipv4Addr {
539     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
540         let octets = self.octets();
541         write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
542     }
543 }
544
545 #[stable(feature = "rust1", since = "1.0.0")]
546 impl fmt::Debug for Ipv4Addr {
547     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
548         fmt::Display::fmt(self, fmt)
549     }
550 }
551
552 #[stable(feature = "rust1", since = "1.0.0")]
553 impl Clone for Ipv4Addr {
554     fn clone(&self) -> Ipv4Addr { *self }
555 }
556
557 #[stable(feature = "rust1", since = "1.0.0")]
558 impl PartialEq for Ipv4Addr {
559     fn eq(&self, other: &Ipv4Addr) -> bool {
560         self.inner.s_addr == other.inner.s_addr
561     }
562 }
563
564 #[stable(feature = "ip_cmp", since = "1.15.0")]
565 impl PartialEq<Ipv4Addr> for IpAddr {
566     fn eq(&self, other: &Ipv4Addr) -> bool {
567         match *self {
568             IpAddr::V4(ref v4) => v4 == other,
569             IpAddr::V6(_) => false,
570         }
571     }
572 }
573
574 #[stable(feature = "ip_cmp", since = "1.15.0")]
575 impl PartialEq<IpAddr> for Ipv4Addr {
576     fn eq(&self, other: &IpAddr) -> bool {
577         match *other {
578             IpAddr::V4(ref v4) => self == v4,
579             IpAddr::V6(_) => false,
580         }
581     }
582 }
583
584 #[stable(feature = "rust1", since = "1.0.0")]
585 impl Eq for Ipv4Addr {}
586
587 #[stable(feature = "rust1", since = "1.0.0")]
588 impl hash::Hash for Ipv4Addr {
589     fn hash<H: hash::Hasher>(&self, s: &mut H) {
590         self.inner.s_addr.hash(s)
591     }
592 }
593
594 #[stable(feature = "rust1", since = "1.0.0")]
595 impl PartialOrd for Ipv4Addr {
596     fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
597         Some(self.cmp(other))
598     }
599 }
600
601 #[stable(feature = "ip_cmp", since = "1.15.0")]
602 impl PartialOrd<Ipv4Addr> for IpAddr {
603     fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
604         match *self {
605             IpAddr::V4(ref v4) => v4.partial_cmp(other),
606             IpAddr::V6(_) => Some(Ordering::Greater),
607         }
608     }
609 }
610
611 #[stable(feature = "ip_cmp", since = "1.15.0")]
612 impl PartialOrd<IpAddr> for Ipv4Addr {
613     fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
614         match *other {
615             IpAddr::V4(ref v4) => self.partial_cmp(v4),
616             IpAddr::V6(_) => Some(Ordering::Less),
617         }
618     }
619 }
620
621 #[stable(feature = "rust1", since = "1.0.0")]
622 impl Ord for Ipv4Addr {
623     fn cmp(&self, other: &Ipv4Addr) -> Ordering {
624         ntoh(self.inner.s_addr).cmp(&ntoh(other.inner.s_addr))
625     }
626 }
627
628 impl AsInner<c::in_addr> for Ipv4Addr {
629     fn as_inner(&self) -> &c::in_addr { &self.inner }
630 }
631 impl FromInner<c::in_addr> for Ipv4Addr {
632     fn from_inner(addr: c::in_addr) -> Ipv4Addr {
633         Ipv4Addr { inner: addr }
634     }
635 }
636
637 #[stable(feature = "ip_u32", since = "1.1.0")]
638 impl From<Ipv4Addr> for u32 {
639     fn from(ip: Ipv4Addr) -> u32 {
640         let ip = ip.octets();
641         ((ip[0] as u32) << 24) + ((ip[1] as u32) << 16) + ((ip[2] as u32) << 8) + (ip[3] as u32)
642     }
643 }
644
645 #[stable(feature = "ip_u32", since = "1.1.0")]
646 impl From<u32> for Ipv4Addr {
647     fn from(ip: u32) -> Ipv4Addr {
648         Ipv4Addr::new((ip >> 24) as u8, (ip >> 16) as u8, (ip >> 8) as u8, ip as u8)
649     }
650 }
651
652 #[stable(feature = "from_slice_v4", since = "1.9.0")]
653 impl From<[u8; 4]> for Ipv4Addr {
654     fn from(octets: [u8; 4]) -> Ipv4Addr {
655         Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
656     }
657 }
658
659 #[stable(feature = "ip_from_slice", since = "1.17.0")]
660 impl From<[u8; 4]> for IpAddr {
661     fn from(octets: [u8; 4]) -> IpAddr {
662         IpAddr::V4(Ipv4Addr::from(octets))
663     }
664 }
665
666 impl Ipv6Addr {
667     /// Creates a new IPv6 address from eight 16-bit segments.
668     ///
669     /// The result will represent the IP address a:b:c:d:e:f:g:h.
670     ///
671     /// # Examples
672     ///
673     /// ```
674     /// use std::net::Ipv6Addr;
675     ///
676     /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
677     /// ```
678     #[stable(feature = "rust1", since = "1.0.0")]
679     pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
680                h: u16) -> Ipv6Addr {
681         let mut addr: c::in6_addr = unsafe { mem::zeroed() };
682         addr.s6_addr = [(a >> 8) as u8, a as u8,
683                         (b >> 8) as u8, b as u8,
684                         (c >> 8) as u8, c as u8,
685                         (d >> 8) as u8, d as u8,
686                         (e >> 8) as u8, e as u8,
687                         (f >> 8) as u8, f as u8,
688                         (g >> 8) as u8, g as u8,
689                         (h >> 8) as u8, h as u8];
690         Ipv6Addr { inner: addr }
691     }
692
693     /// Returns the eight 16-bit segments that make up this address.
694     ///
695     /// # Examples
696     ///
697     /// ```
698     /// use std::net::Ipv6Addr;
699     ///
700     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
701     ///            [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
702     /// ```
703     #[stable(feature = "rust1", since = "1.0.0")]
704     pub fn segments(&self) -> [u16; 8] {
705         let arr = &self.inner.s6_addr;
706         [
707             (arr[0] as u16) << 8 | (arr[1] as u16),
708             (arr[2] as u16) << 8 | (arr[3] as u16),
709             (arr[4] as u16) << 8 | (arr[5] as u16),
710             (arr[6] as u16) << 8 | (arr[7] as u16),
711             (arr[8] as u16) << 8 | (arr[9] as u16),
712             (arr[10] as u16) << 8 | (arr[11] as u16),
713             (arr[12] as u16) << 8 | (arr[13] as u16),
714             (arr[14] as u16) << 8 | (arr[15] as u16),
715         ]
716     }
717
718     /// Returns true for the special 'unspecified' address (::).
719     ///
720     /// This property is defined in [RFC 4291].
721     ///
722     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
723     ///
724     /// # Examples
725     ///
726     /// ```
727     /// use std::net::Ipv6Addr;
728     ///
729     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
730     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
731     /// ```
732     #[stable(since = "1.7.0", feature = "ip_17")]
733     pub fn is_unspecified(&self) -> bool {
734         self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
735     }
736
737     /// Returns true if this is a loopback address (::1).
738     ///
739     /// This property is defined in [RFC 4291].
740     ///
741     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
742     ///
743     /// # Examples
744     ///
745     /// ```
746     /// use std::net::Ipv6Addr;
747     ///
748     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
749     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
750     /// ```
751     #[stable(since = "1.7.0", feature = "ip_17")]
752     pub fn is_loopback(&self) -> bool {
753         self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
754     }
755
756     /// Returns true if the address appears to be globally routable.
757     ///
758     /// The following return false:
759     ///
760     /// - the loopback address
761     /// - link-local, site-local, and unique local unicast addresses
762     /// - interface-, link-, realm-, admin- and site-local multicast addresses
763     ///
764     /// # Examples
765     ///
766     /// ```
767     /// #![feature(ip)]
768     ///
769     /// use std::net::Ipv6Addr;
770     ///
771     /// fn main() {
772     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
773     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
774     ///     assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
775     /// }
776     /// ```
777     pub fn is_global(&self) -> bool {
778         match self.multicast_scope() {
779             Some(Ipv6MulticastScope::Global) => true,
780             None => self.is_unicast_global(),
781             _ => false
782         }
783     }
784
785     /// Returns true if this is a unique local address (fc00::/7).
786     ///
787     /// This property is defined in [RFC 4193].
788     ///
789     /// [RFC 4193]: https://tools.ietf.org/html/rfc4193
790     ///
791     /// # Examples
792     ///
793     /// ```
794     /// #![feature(ip)]
795     ///
796     /// use std::net::Ipv6Addr;
797     ///
798     /// fn main() {
799     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(),
800     ///                false);
801     ///     assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
802     /// }
803     /// ```
804     pub fn is_unique_local(&self) -> bool {
805         (self.segments()[0] & 0xfe00) == 0xfc00
806     }
807
808     /// Returns true if the address is unicast and link-local (fe80::/10).
809     ///
810     /// This property is defined in [RFC 4291].
811     ///
812     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
813     ///
814     /// # Examples
815     ///
816     /// ```
817     /// #![feature(ip)]
818     ///
819     /// use std::net::Ipv6Addr;
820     ///
821     /// fn main() {
822     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_link_local(),
823     ///                false);
824     ///     assert_eq!(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
825     /// }
826     /// ```
827     pub fn is_unicast_link_local(&self) -> bool {
828         (self.segments()[0] & 0xffc0) == 0xfe80
829     }
830
831     /// Returns true if this is a deprecated unicast site-local address
832     /// (fec0::/10).
833     ///
834     /// # Examples
835     ///
836     /// ```
837     /// #![feature(ip)]
838     ///
839     /// use std::net::Ipv6Addr;
840     ///
841     /// fn main() {
842     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
843     ///                false);
844     ///     assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
845     /// }
846     /// ```
847     pub fn is_unicast_site_local(&self) -> bool {
848         (self.segments()[0] & 0xffc0) == 0xfec0
849     }
850
851     /// Returns true if this is an address reserved for documentation
852     /// (2001:db8::/32).
853     ///
854     /// This property is defined in [RFC 3849].
855     ///
856     /// [RFC 3849]: https://tools.ietf.org/html/rfc3849
857     ///
858     /// # Examples
859     ///
860     /// ```
861     /// #![feature(ip)]
862     ///
863     /// use std::net::Ipv6Addr;
864     ///
865     /// fn main() {
866     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(),
867     ///                false);
868     ///     assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
869     /// }
870     /// ```
871     pub fn is_documentation(&self) -> bool {
872         (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
873     }
874
875     /// Returns true if the address is a globally routable unicast address.
876     ///
877     /// The following return false:
878     ///
879     /// - the loopback address
880     /// - the link-local addresses
881     /// - the (deprecated) site-local addresses
882     /// - unique local addresses
883     /// - the unspecified address
884     /// - the address range reserved for documentation
885     ///
886     /// # Examples
887     ///
888     /// ```
889     /// #![feature(ip)]
890     ///
891     /// use std::net::Ipv6Addr;
892     ///
893     /// fn main() {
894     ///     assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
895     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(),
896     ///                true);
897     /// }
898     /// ```
899     pub fn is_unicast_global(&self) -> bool {
900         !self.is_multicast()
901             && !self.is_loopback() && !self.is_unicast_link_local()
902             && !self.is_unicast_site_local() && !self.is_unique_local()
903             && !self.is_unspecified() && !self.is_documentation()
904     }
905
906     /// Returns the address's multicast scope if the address is multicast.
907     ///
908     /// # Examples
909     ///
910     /// ```
911     /// #![feature(ip)]
912     ///
913     /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
914     ///
915     /// fn main() {
916     ///     assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
917     ///                              Some(Ipv6MulticastScope::Global));
918     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
919     /// }
920     /// ```
921     pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
922         if self.is_multicast() {
923             match self.segments()[0] & 0x000f {
924                 1 => Some(Ipv6MulticastScope::InterfaceLocal),
925                 2 => Some(Ipv6MulticastScope::LinkLocal),
926                 3 => Some(Ipv6MulticastScope::RealmLocal),
927                 4 => Some(Ipv6MulticastScope::AdminLocal),
928                 5 => Some(Ipv6MulticastScope::SiteLocal),
929                 8 => Some(Ipv6MulticastScope::OrganizationLocal),
930                 14 => Some(Ipv6MulticastScope::Global),
931                 _ => None
932             }
933         } else {
934             None
935         }
936     }
937
938     /// Returns true if this is a multicast address (ff00::/8).
939     ///
940     /// This property is defined by [RFC 4291].
941     ///
942     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
943     /// # Examples
944     ///
945     /// ```
946     /// use std::net::Ipv6Addr;
947     ///
948     /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
949     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
950     /// ```
951     #[stable(since = "1.7.0", feature = "ip_17")]
952     pub fn is_multicast(&self) -> bool {
953         (self.segments()[0] & 0xff00) == 0xff00
954     }
955
956     /// Converts this address to an IPv4 address. Returns None if this address is
957     /// neither IPv4-compatible or IPv4-mapped.
958     ///
959     /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
960     ///
961     /// ```
962     /// use std::net::{Ipv4Addr, Ipv6Addr};
963     ///
964     /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
965     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
966     ///            Some(Ipv4Addr::new(192, 10, 2, 255)));
967     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
968     ///            Some(Ipv4Addr::new(0, 0, 0, 1)));
969     /// ```
970     #[stable(feature = "rust1", since = "1.0.0")]
971     pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
972         match self.segments() {
973             [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
974                 Some(Ipv4Addr::new((g >> 8) as u8, g as u8,
975                                    (h >> 8) as u8, h as u8))
976             },
977             _ => None
978         }
979     }
980
981     /// Returns the sixteen eight-bit integers the IPv6 address consists of.
982     ///
983     /// ```
984     /// use std::net::Ipv6Addr;
985     ///
986     /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
987     ///            [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
988     /// ```
989     #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
990     pub fn octets(&self) -> [u8; 16] {
991         self.inner.s6_addr
992     }
993 }
994
995 #[stable(feature = "rust1", since = "1.0.0")]
996 impl fmt::Display for Ipv6Addr {
997     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
998         match self.segments() {
999             // We need special cases for :: and ::1, otherwise they're formatted
1000             // as ::0.0.0.[01]
1001             [0, 0, 0, 0, 0, 0, 0, 0] => write!(fmt, "::"),
1002             [0, 0, 0, 0, 0, 0, 0, 1] => write!(fmt, "::1"),
1003             // Ipv4 Compatible address
1004             [0, 0, 0, 0, 0, 0, g, h] => {
1005                 write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
1006                        (h >> 8) as u8, h as u8)
1007             }
1008             // Ipv4-Mapped address
1009             [0, 0, 0, 0, 0, 0xffff, g, h] => {
1010                 write!(fmt, "::ffff:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
1011                        (h >> 8) as u8, h as u8)
1012             },
1013             _ => {
1014                 fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) {
1015                     let mut longest_span_len = 0;
1016                     let mut longest_span_at = 0;
1017                     let mut cur_span_len = 0;
1018                     let mut cur_span_at = 0;
1019
1020                     for i in 0..8 {
1021                         if segments[i] == 0 {
1022                             if cur_span_len == 0 {
1023                                 cur_span_at = i;
1024                             }
1025
1026                             cur_span_len += 1;
1027
1028                             if cur_span_len > longest_span_len {
1029                                 longest_span_len = cur_span_len;
1030                                 longest_span_at = cur_span_at;
1031                             }
1032                         } else {
1033                             cur_span_len = 0;
1034                             cur_span_at = 0;
1035                         }
1036                     }
1037
1038                     (longest_span_at, longest_span_len)
1039                 }
1040
1041                 let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
1042
1043                 if zeros_len > 1 {
1044                     fn fmt_subslice(segments: &[u16], fmt: &mut fmt::Formatter) -> fmt::Result {
1045                         if !segments.is_empty() {
1046                             write!(fmt, "{:x}", segments[0])?;
1047                             for &seg in &segments[1..] {
1048                                 write!(fmt, ":{:x}", seg)?;
1049                             }
1050                         }
1051                         Ok(())
1052                     }
1053
1054                     fmt_subslice(&self.segments()[..zeros_at], fmt)?;
1055                     fmt.write_str("::")?;
1056                     fmt_subslice(&self.segments()[zeros_at + zeros_len..], fmt)
1057                 } else {
1058                     let &[a, b, c, d, e, f, g, h] = &self.segments();
1059                     write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
1060                            a, b, c, d, e, f, g, h)
1061                 }
1062             }
1063         }
1064     }
1065 }
1066
1067 #[stable(feature = "rust1", since = "1.0.0")]
1068 impl fmt::Debug for Ipv6Addr {
1069     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1070         fmt::Display::fmt(self, fmt)
1071     }
1072 }
1073
1074 #[stable(feature = "rust1", since = "1.0.0")]
1075 impl Clone for Ipv6Addr {
1076     fn clone(&self) -> Ipv6Addr { *self }
1077 }
1078
1079 #[stable(feature = "rust1", since = "1.0.0")]
1080 impl PartialEq for Ipv6Addr {
1081     fn eq(&self, other: &Ipv6Addr) -> bool {
1082         self.inner.s6_addr == other.inner.s6_addr
1083     }
1084 }
1085
1086 #[stable(feature = "ip_cmp", since = "1.15.0")]
1087 impl PartialEq<IpAddr> for Ipv6Addr {
1088     fn eq(&self, other: &IpAddr) -> bool {
1089         match *other {
1090             IpAddr::V4(_) => false,
1091             IpAddr::V6(ref v6) => self == v6,
1092         }
1093     }
1094 }
1095
1096 #[stable(feature = "ip_cmp", since = "1.15.0")]
1097 impl PartialEq<Ipv6Addr> for IpAddr {
1098     fn eq(&self, other: &Ipv6Addr) -> bool {
1099         match *self {
1100             IpAddr::V4(_) => false,
1101             IpAddr::V6(ref v6) => v6 == other,
1102         }
1103     }
1104 }
1105
1106 #[stable(feature = "rust1", since = "1.0.0")]
1107 impl Eq for Ipv6Addr {}
1108
1109 #[stable(feature = "rust1", since = "1.0.0")]
1110 impl hash::Hash for Ipv6Addr {
1111     fn hash<H: hash::Hasher>(&self, s: &mut H) {
1112         self.inner.s6_addr.hash(s)
1113     }
1114 }
1115
1116 #[stable(feature = "rust1", since = "1.0.0")]
1117 impl PartialOrd for Ipv6Addr {
1118     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1119         Some(self.cmp(other))
1120     }
1121 }
1122
1123 #[stable(feature = "ip_cmp", since = "1.15.0")]
1124 impl PartialOrd<Ipv6Addr> for IpAddr {
1125     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1126         match *self {
1127             IpAddr::V4(_) => Some(Ordering::Less),
1128             IpAddr::V6(ref v6) => v6.partial_cmp(other),
1129         }
1130     }
1131 }
1132
1133 #[stable(feature = "ip_cmp", since = "1.15.0")]
1134 impl PartialOrd<IpAddr> for Ipv6Addr {
1135     fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1136         match *other {
1137             IpAddr::V4(_) => Some(Ordering::Greater),
1138             IpAddr::V6(ref v6) => self.partial_cmp(v6),
1139         }
1140     }
1141 }
1142
1143 #[stable(feature = "rust1", since = "1.0.0")]
1144 impl Ord for Ipv6Addr {
1145     fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1146         self.segments().cmp(&other.segments())
1147     }
1148 }
1149
1150 impl AsInner<c::in6_addr> for Ipv6Addr {
1151     fn as_inner(&self) -> &c::in6_addr { &self.inner }
1152 }
1153 impl FromInner<c::in6_addr> for Ipv6Addr {
1154     fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1155         Ipv6Addr { inner: addr }
1156     }
1157 }
1158
1159 #[unstable(feature = "i128", issue = "35118")]
1160 impl From<Ipv6Addr> for u128 {
1161     fn from(ip: Ipv6Addr) -> u128 {
1162         let ip = ip.segments();
1163         ((ip[0] as u128) << 112) + ((ip[1] as u128) << 96) + ((ip[2] as u128) << 80) +
1164             ((ip[3] as u128) << 64) + ((ip[4] as u128) << 48) + ((ip[5] as u128) << 32) +
1165             ((ip[6] as u128) << 16) + (ip[7] as u128)
1166     }
1167 }
1168 #[unstable(feature = "i128", issue = "35118")]
1169 impl From<u128> for Ipv6Addr {
1170     fn from(ip: u128) -> Ipv6Addr {
1171         Ipv6Addr::new(
1172             (ip >> 112) as u16, (ip >> 96) as u16, (ip >> 80) as u16,
1173             (ip >> 64) as u16, (ip >> 48) as u16, (ip >> 32) as u16,
1174             (ip >> 16) as u16, ip as u16,
1175         )
1176     }
1177 }
1178
1179 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
1180 impl From<[u8; 16]> for Ipv6Addr {
1181     fn from(octets: [u8; 16]) -> Ipv6Addr {
1182         let mut inner: c::in6_addr = unsafe { mem::zeroed() };
1183         inner.s6_addr = octets;
1184         Ipv6Addr::from_inner(inner)
1185     }
1186 }
1187
1188 #[stable(feature = "ipv6_from_segments", since = "1.15.0")]
1189 impl From<[u16; 8]> for Ipv6Addr {
1190     fn from(segments: [u16; 8]) -> Ipv6Addr {
1191         let [a, b, c, d, e, f, g, h] = segments;
1192         Ipv6Addr::new(a, b, c, d, e, f, g, h)
1193     }
1194 }
1195
1196
1197 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1198 impl From<[u8; 16]> for IpAddr {
1199     fn from(octets: [u8; 16]) -> IpAddr {
1200         IpAddr::V6(Ipv6Addr::from(octets))
1201     }
1202 }
1203
1204 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1205 impl From<[u16; 8]> for IpAddr {
1206     fn from(segments: [u16; 8]) -> IpAddr {
1207         IpAddr::V6(Ipv6Addr::from(segments))
1208     }
1209 }
1210
1211 // Tests for this module
1212 #[cfg(all(test, not(target_os = "emscripten")))]
1213 mod tests {
1214     use net::*;
1215     use net::Ipv6MulticastScope::*;
1216     use net::test::{tsa, sa6, sa4};
1217
1218     #[test]
1219     fn test_from_str_ipv4() {
1220         assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
1221         assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
1222         assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
1223
1224         // out of range
1225         let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
1226         assert_eq!(None, none);
1227         // too short
1228         let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
1229         assert_eq!(None, none);
1230         // too long
1231         let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok();
1232         assert_eq!(None, none);
1233         // no number between dots
1234         let none: Option<Ipv4Addr> = "255.0..1".parse().ok();
1235         assert_eq!(None, none);
1236     }
1237
1238     #[test]
1239     fn test_from_str_ipv6() {
1240         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
1241         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
1242
1243         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
1244         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
1245
1246         assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
1247                 "2a02:6b8::11:11".parse());
1248
1249         // too long group
1250         let none: Option<Ipv6Addr> = "::00000".parse().ok();
1251         assert_eq!(None, none);
1252         // too short
1253         let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
1254         assert_eq!(None, none);
1255         // too long
1256         let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
1257         assert_eq!(None, none);
1258         // triple colon
1259         let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
1260         assert_eq!(None, none);
1261         // two double colons
1262         let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok();
1263         assert_eq!(None, none);
1264     }
1265
1266     #[test]
1267     fn test_from_str_ipv4_in_ipv6() {
1268         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)),
1269                 "::192.0.2.33".parse());
1270         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)),
1271                 "::FFFF:192.0.2.33".parse());
1272         assert_eq!(Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
1273                 "64:ff9b::192.0.2.33".parse());
1274         assert_eq!(Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
1275                 "2001:db8:122:c000:2:2100:192.0.2.33".parse());
1276
1277         // colon after v4
1278         let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
1279         assert_eq!(None, none);
1280         // not enough groups
1281         let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok();
1282         assert_eq!(None, none);
1283         // too many groups
1284         let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
1285         assert_eq!(None, none);
1286     }
1287
1288     #[test]
1289     fn test_from_str_socket_addr() {
1290         assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)),
1291                    "77.88.21.11:80".parse());
1292         assert_eq!(Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)),
1293                    "77.88.21.11:80".parse());
1294         assert_eq!(Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
1295                    "[2a02:6b8:0:1::1]:53".parse());
1296         assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1,
1297                                                       0, 0, 0, 1), 53, 0, 0)),
1298                    "[2a02:6b8:0:1::1]:53".parse());
1299         assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
1300                    "[::127.0.0.1]:22".parse());
1301         assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0,
1302                                                       0x7F00, 1), 22, 0, 0)),
1303                    "[::127.0.0.1]:22".parse());
1304
1305         // without port
1306         let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
1307         assert_eq!(None, none);
1308         // without port
1309         let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
1310         assert_eq!(None, none);
1311         // wrong brackets around v4
1312         let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
1313         assert_eq!(None, none);
1314         // port out of range
1315         let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
1316         assert_eq!(None, none);
1317     }
1318
1319     #[test]
1320     fn ipv6_addr_to_string() {
1321         // ipv4-mapped address
1322         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
1323         assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
1324
1325         // ipv4-compatible address
1326         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
1327         assert_eq!(a1.to_string(), "::192.0.2.128");
1328
1329         // v6 address with no zero segments
1330         assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
1331                    "8:9:a:b:c:d:e:f");
1332
1333         // reduce a single run of zeros
1334         assert_eq!("ae::ffff:102:304",
1335                    Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string());
1336
1337         // don't reduce just a single zero segment
1338         assert_eq!("1:2:3:4:5:6:0:8",
1339                    Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
1340
1341         // 'any' address
1342         assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
1343
1344         // loopback address
1345         assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
1346
1347         // ends in zeros
1348         assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
1349
1350         // two runs of zeros, second one is longer
1351         assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
1352
1353         // two runs of zeros, equal length
1354         assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
1355     }
1356
1357     #[test]
1358     fn ipv4_to_ipv6() {
1359         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
1360                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped());
1361         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
1362                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible());
1363     }
1364
1365     #[test]
1366     fn ipv6_to_ipv4() {
1367         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
1368                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1369         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1370                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1371         assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1372                    None);
1373     }
1374
1375     #[test]
1376     fn ip_properties() {
1377         fn check4(octets: &[u8; 4], unspec: bool, loopback: bool,
1378                   global: bool, multicast: bool, documentation: bool) {
1379             let ip = IpAddr::V4(Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]));
1380             assert_eq!(ip.is_unspecified(), unspec);
1381             assert_eq!(ip.is_loopback(), loopback);
1382             assert_eq!(ip.is_global(), global);
1383             assert_eq!(ip.is_multicast(), multicast);
1384             assert_eq!(ip.is_documentation(), documentation);
1385         }
1386
1387         fn check6(str_addr: &str, unspec: bool, loopback: bool,
1388                   global: bool, u_doc: bool, mcast: bool) {
1389             let ip = IpAddr::V6(str_addr.parse().unwrap());
1390             assert_eq!(ip.is_unspecified(), unspec);
1391             assert_eq!(ip.is_loopback(), loopback);
1392             assert_eq!(ip.is_global(), global);
1393             assert_eq!(ip.is_documentation(), u_doc);
1394             assert_eq!(ip.is_multicast(), mcast);
1395         }
1396
1397         //     address                unspec loopbk global multicast doc
1398         check4(&[0, 0, 0, 0],         true,  false, false,  false,   false);
1399         check4(&[0, 0, 0, 1],         false, false, true,   false,   false);
1400         check4(&[0, 1, 0, 0],         false, false, true,   false,   false);
1401         check4(&[10, 9, 8, 7],        false, false, false,  false,   false);
1402         check4(&[127, 1, 2, 3],       false, true,  false,  false,   false);
1403         check4(&[172, 31, 254, 253],  false, false, false,  false,   false);
1404         check4(&[169, 254, 253, 242], false, false, false,  false,   false);
1405         check4(&[192, 0, 2, 183],     false, false, false,  false,   true);
1406         check4(&[192, 1, 2, 183],     false, false, true,   false,   false);
1407         check4(&[192, 168, 254, 253], false, false, false,  false,   false);
1408         check4(&[198, 51, 100, 0],    false, false, false,  false,   true);
1409         check4(&[203, 0, 113, 0],     false, false, false,  false,   true);
1410         check4(&[203, 2, 113, 0],     false, false, true,   false,   false);
1411         check4(&[224, 0, 0, 0],       false, false, true,   true,    false);
1412         check4(&[239, 255, 255, 255], false, false, true,   true,    false);
1413         check4(&[255, 255, 255, 255], false, false, false,  false,   false);
1414
1415         //     address                            unspec loopbk global doc    mcast
1416         check6("::",                              true,  false, false, false, false);
1417         check6("::1",                             false, true,  false, false, false);
1418         check6("::0.0.0.2",                       false, false, true,  false, false);
1419         check6("1::",                             false, false, true,  false, false);
1420         check6("fc00::",                          false, false, false, false, false);
1421         check6("fdff:ffff::",                     false, false, false, false, false);
1422         check6("fe80:ffff::",                     false, false, false, false, false);
1423         check6("febf:ffff::",                     false, false, false, false, false);
1424         check6("fec0::",                          false, false, false, false, false);
1425         check6("ff01::",                          false, false, false, false, true);
1426         check6("ff02::",                          false, false, false, false, true);
1427         check6("ff03::",                          false, false, false, false, true);
1428         check6("ff04::",                          false, false, false, false, true);
1429         check6("ff05::",                          false, false, false, false, true);
1430         check6("ff08::",                          false, false, false, false, true);
1431         check6("ff0e::",                          false, false, true,  false, true);
1432         check6("2001:db8:85a3::8a2e:370:7334",    false, false, false, true,  false);
1433         check6("102:304:506:708:90a:b0c:d0e:f10", false, false, true,  false, false);
1434     }
1435
1436     #[test]
1437     fn ipv4_properties() {
1438         fn check(octets: &[u8; 4], unspec: bool, loopback: bool,
1439                  private: bool, link_local: bool, global: bool,
1440                  multicast: bool, broadcast: bool, documentation: bool) {
1441             let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
1442             assert_eq!(octets, &ip.octets());
1443
1444             assert_eq!(ip.is_unspecified(), unspec);
1445             assert_eq!(ip.is_loopback(), loopback);
1446             assert_eq!(ip.is_private(), private);
1447             assert_eq!(ip.is_link_local(), link_local);
1448             assert_eq!(ip.is_global(), global);
1449             assert_eq!(ip.is_multicast(), multicast);
1450             assert_eq!(ip.is_broadcast(), broadcast);
1451             assert_eq!(ip.is_documentation(), documentation);
1452         }
1453
1454         //    address                unspec loopbk privt  linloc global multicast brdcast doc
1455         check(&[0, 0, 0, 0],         true,  false, false, false, false,  false,    false,  false);
1456         check(&[0, 0, 0, 1],         false, false, false, false, true,   false,    false,  false);
1457         check(&[0, 1, 0, 0],         false, false, false, false, true,   false,    false,  false);
1458         check(&[10, 9, 8, 7],        false, false, true,  false, false,  false,    false,  false);
1459         check(&[127, 1, 2, 3],       false, true,  false, false, false,  false,    false,  false);
1460         check(&[172, 31, 254, 253],  false, false, true,  false, false,  false,    false,  false);
1461         check(&[169, 254, 253, 242], false, false, false, true,  false,  false,    false,  false);
1462         check(&[192, 0, 2, 183],     false, false, false, false, false,  false,    false,  true);
1463         check(&[192, 1, 2, 183],     false, false, false, false, true,   false,    false,  false);
1464         check(&[192, 168, 254, 253], false, false, true,  false, false,  false,    false,  false);
1465         check(&[198, 51, 100, 0],    false, false, false, false, false,  false,    false,  true);
1466         check(&[203, 0, 113, 0],     false, false, false, false, false,  false,    false,  true);
1467         check(&[203, 2, 113, 0],     false, false, false, false, true,   false,    false,  false);
1468         check(&[224, 0, 0, 0],       false, false, false, false, true,   true,     false,  false);
1469         check(&[239, 255, 255, 255], false, false, false, false, true,   true,     false,  false);
1470         check(&[255, 255, 255, 255], false, false, false, false, false,  false,    true,   false);
1471     }
1472
1473     #[test]
1474     fn ipv6_properties() {
1475         fn check(str_addr: &str, octets: &[u8; 16], unspec: bool, loopback: bool,
1476                  unique_local: bool, global: bool,
1477                  u_link_local: bool, u_site_local: bool, u_global: bool, u_doc: bool,
1478                  m_scope: Option<Ipv6MulticastScope>) {
1479             let ip: Ipv6Addr = str_addr.parse().unwrap();
1480             assert_eq!(str_addr, ip.to_string());
1481             assert_eq!(&ip.octets(), octets);
1482             assert_eq!(Ipv6Addr::from(*octets), ip);
1483
1484             assert_eq!(ip.is_unspecified(), unspec);
1485             assert_eq!(ip.is_loopback(), loopback);
1486             assert_eq!(ip.is_unique_local(), unique_local);
1487             assert_eq!(ip.is_global(), global);
1488             assert_eq!(ip.is_unicast_link_local(), u_link_local);
1489             assert_eq!(ip.is_unicast_site_local(), u_site_local);
1490             assert_eq!(ip.is_unicast_global(), u_global);
1491             assert_eq!(ip.is_documentation(), u_doc);
1492             assert_eq!(ip.multicast_scope(), m_scope);
1493             assert_eq!(ip.is_multicast(), m_scope.is_some());
1494         }
1495
1496         //    unspec loopbk uniqlo global unill  unisl  uniglo doc    mscope
1497         check("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1498               true,  false, false, false, false, false, false, false, None);
1499         check("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
1500               false, true,  false, false, false, false, false, false, None);
1501         check("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
1502               false, false, false, true,  false, false, true,  false, None);
1503         check("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1504               false, false, false, true,  false, false, true,  false, None);
1505         check("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1506               false, false, true,  false, false, false, false, false, None);
1507         check("fdff:ffff::", &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1508               false, false, true,  false, false, false, false, false, None);
1509         check("fe80:ffff::", &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1510               false, false, false, false, true,  false, false, false, None);
1511         check("febf:ffff::", &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1512               false, false, false, false, true,  false, false, false, None);
1513         check("fec0::", &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1514               false, false, false, false, false, true,  false, false, None);
1515         check("ff01::", &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1516               false, false, false, false, false, false, false, false, Some(InterfaceLocal));
1517         check("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1518               false, false, false, false, false, false, false, false, Some(LinkLocal));
1519         check("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1520               false, false, false, false, false, false, false, false, Some(RealmLocal));
1521         check("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1522               false, false, false, false, false, false, false, false, Some(AdminLocal));
1523         check("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1524               false, false, false, false, false, false, false, false, Some(SiteLocal));
1525         check("ff08::", &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1526               false, false, false, false, false, false, false, false, Some(OrganizationLocal));
1527         check("ff0e::", &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1528               false, false, false, true,  false, false, false, false, Some(Global));
1529         check("2001:db8:85a3::8a2e:370:7334",
1530               &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
1531               false, false, false, false, false, false, false, true, None);
1532         check("102:304:506:708:90a:b0c:d0e:f10",
1533               &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1534               false, false, false, true,  false, false, true,  false, None);
1535     }
1536
1537     #[test]
1538     fn to_socket_addr_socketaddr() {
1539         let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345);
1540         assert_eq!(Ok(vec![a]), tsa(a));
1541     }
1542
1543     #[test]
1544     fn test_ipv4_to_int() {
1545         let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
1546         assert_eq!(u32::from(a), 0x11223344);
1547     }
1548
1549     #[test]
1550     fn test_int_to_ipv4() {
1551         let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
1552         assert_eq!(Ipv4Addr::from(0x11223344), a);
1553     }
1554
1555     #[test]
1556     fn test_ipv6_to_int() {
1557         let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
1558         assert_eq!(u128::from(a), 0x112233445566778899aabbccddeeff11u128);
1559     }
1560
1561     #[test]
1562     fn test_int_to_ipv6() {
1563         let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
1564         assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
1565     }
1566
1567     #[test]
1568     fn ipv4_from_octets() {
1569         assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
1570     }
1571
1572     #[test]
1573     fn ipv6_from_segments() {
1574         let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1575                                         0x8899, 0xaabb, 0xccdd, 0xeeff]);
1576         let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677,
1577                                 0x8899, 0xaabb, 0xccdd, 0xeeff);
1578         assert_eq!(new, from_u16s);
1579     }
1580
1581     #[test]
1582     fn ipv6_from_octets() {
1583         let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1584                                         0x8899, 0xaabb, 0xccdd, 0xeeff]);
1585         let from_u8s = Ipv6Addr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
1586                                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
1587         assert_eq!(from_u16s, from_u8s);
1588     }
1589
1590     #[test]
1591     fn cmp() {
1592         let v41 = Ipv4Addr::new(100, 64, 3, 3);
1593         let v42 = Ipv4Addr::new(192, 0, 2, 2);
1594         let v61 = "2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap();
1595         let v62 = "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap();
1596         assert!(v41 < v42);
1597         assert!(v61 < v62);
1598
1599         assert_eq!(v41, IpAddr::V4(v41));
1600         assert_eq!(v61, IpAddr::V6(v61));
1601         assert!(v41 != IpAddr::V4(v42));
1602         assert!(v61 != IpAddr::V6(v62));
1603
1604         assert!(v41 < IpAddr::V4(v42));
1605         assert!(v61 < IpAddr::V6(v62));
1606         assert!(IpAddr::V4(v41) < v42);
1607         assert!(IpAddr::V6(v61) < v62);
1608
1609         assert!(v41 < IpAddr::V6(v61));
1610         assert!(IpAddr::V4(v41) < v61);
1611     }
1612
1613     #[test]
1614     fn is_v4() {
1615         let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
1616         assert!(ip.is_ipv4());
1617         assert!(!ip.is_ipv6());
1618     }
1619
1620     #[test]
1621     fn is_v6() {
1622         let ip = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
1623         assert!(!ip.is_ipv4());
1624         assert!(ip.is_ipv6());
1625     }
1626 }