]> git.lizzy.rs Git - rust.git/blob - src/libstd/net/ip.rs
Unignore u128 test for stage 0,1
[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 impl Ipv6Addr {
660     /// Creates a new IPv6 address from eight 16-bit segments.
661     ///
662     /// The result will represent the IP address a:b:c:d:e:f:g:h.
663     ///
664     /// # Examples
665     ///
666     /// ```
667     /// use std::net::Ipv6Addr;
668     ///
669     /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
670     /// ```
671     #[stable(feature = "rust1", since = "1.0.0")]
672     pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
673                h: u16) -> Ipv6Addr {
674         let mut addr: c::in6_addr = unsafe { mem::zeroed() };
675         addr.s6_addr = [(a >> 8) as u8, a as u8,
676                         (b >> 8) as u8, b as u8,
677                         (c >> 8) as u8, c as u8,
678                         (d >> 8) as u8, d as u8,
679                         (e >> 8) as u8, e as u8,
680                         (f >> 8) as u8, f as u8,
681                         (g >> 8) as u8, g as u8,
682                         (h >> 8) as u8, h as u8];
683         Ipv6Addr { inner: addr }
684     }
685
686     /// Returns the eight 16-bit segments that make up this address.
687     ///
688     /// # Examples
689     ///
690     /// ```
691     /// use std::net::Ipv6Addr;
692     ///
693     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
694     ///            [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
695     /// ```
696     #[stable(feature = "rust1", since = "1.0.0")]
697     pub fn segments(&self) -> [u16; 8] {
698         let arr = &self.inner.s6_addr;
699         [
700             (arr[0] as u16) << 8 | (arr[1] as u16),
701             (arr[2] as u16) << 8 | (arr[3] as u16),
702             (arr[4] as u16) << 8 | (arr[5] as u16),
703             (arr[6] as u16) << 8 | (arr[7] as u16),
704             (arr[8] as u16) << 8 | (arr[9] as u16),
705             (arr[10] as u16) << 8 | (arr[11] as u16),
706             (arr[12] as u16) << 8 | (arr[13] as u16),
707             (arr[14] as u16) << 8 | (arr[15] as u16),
708         ]
709     }
710
711     /// Returns true for the special 'unspecified' address (::).
712     ///
713     /// This property is defined in [RFC 4291].
714     ///
715     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
716     ///
717     /// # Examples
718     ///
719     /// ```
720     /// use std::net::Ipv6Addr;
721     ///
722     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
723     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
724     /// ```
725     #[stable(since = "1.7.0", feature = "ip_17")]
726     pub fn is_unspecified(&self) -> bool {
727         self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
728     }
729
730     /// Returns true if this is a loopback address (::1).
731     ///
732     /// This property is defined in [RFC 4291].
733     ///
734     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
735     ///
736     /// # Examples
737     ///
738     /// ```
739     /// use std::net::Ipv6Addr;
740     ///
741     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
742     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
743     /// ```
744     #[stable(since = "1.7.0", feature = "ip_17")]
745     pub fn is_loopback(&self) -> bool {
746         self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
747     }
748
749     /// Returns true if the address appears to be globally routable.
750     ///
751     /// The following return false:
752     ///
753     /// - the loopback address
754     /// - link-local, site-local, and unique local unicast addresses
755     /// - interface-, link-, realm-, admin- and site-local multicast addresses
756     ///
757     /// # Examples
758     ///
759     /// ```
760     /// #![feature(ip)]
761     ///
762     /// use std::net::Ipv6Addr;
763     ///
764     /// fn main() {
765     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
766     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
767     ///     assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
768     /// }
769     /// ```
770     pub fn is_global(&self) -> bool {
771         match self.multicast_scope() {
772             Some(Ipv6MulticastScope::Global) => true,
773             None => self.is_unicast_global(),
774             _ => false
775         }
776     }
777
778     /// Returns true if this is a unique local address (fc00::/7).
779     ///
780     /// This property is defined in [RFC 4193].
781     ///
782     /// [RFC 4193]: https://tools.ietf.org/html/rfc4193
783     ///
784     /// # Examples
785     ///
786     /// ```
787     /// #![feature(ip)]
788     ///
789     /// use std::net::Ipv6Addr;
790     ///
791     /// fn main() {
792     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(),
793     ///                false);
794     ///     assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
795     /// }
796     /// ```
797     pub fn is_unique_local(&self) -> bool {
798         (self.segments()[0] & 0xfe00) == 0xfc00
799     }
800
801     /// Returns true if the address is unicast and link-local (fe80::/10).
802     ///
803     /// This property is defined in [RFC 4291].
804     ///
805     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
806     ///
807     /// # Examples
808     ///
809     /// ```
810     /// #![feature(ip)]
811     ///
812     /// use std::net::Ipv6Addr;
813     ///
814     /// fn main() {
815     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_link_local(),
816     ///                false);
817     ///     assert_eq!(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
818     /// }
819     /// ```
820     pub fn is_unicast_link_local(&self) -> bool {
821         (self.segments()[0] & 0xffc0) == 0xfe80
822     }
823
824     /// Returns true if this is a deprecated unicast site-local address
825     /// (fec0::/10).
826     ///
827     /// # Examples
828     ///
829     /// ```
830     /// #![feature(ip)]
831     ///
832     /// use std::net::Ipv6Addr;
833     ///
834     /// fn main() {
835     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
836     ///                false);
837     ///     assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
838     /// }
839     /// ```
840     pub fn is_unicast_site_local(&self) -> bool {
841         (self.segments()[0] & 0xffc0) == 0xfec0
842     }
843
844     /// Returns true if this is an address reserved for documentation
845     /// (2001:db8::/32).
846     ///
847     /// This property is defined in [RFC 3849].
848     ///
849     /// [RFC 3849]: https://tools.ietf.org/html/rfc3849
850     ///
851     /// # Examples
852     ///
853     /// ```
854     /// #![feature(ip)]
855     ///
856     /// use std::net::Ipv6Addr;
857     ///
858     /// fn main() {
859     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(),
860     ///                false);
861     ///     assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
862     /// }
863     /// ```
864     pub fn is_documentation(&self) -> bool {
865         (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
866     }
867
868     /// Returns true if the address is a globally routable unicast address.
869     ///
870     /// The following return false:
871     ///
872     /// - the loopback address
873     /// - the link-local addresses
874     /// - the (deprecated) site-local addresses
875     /// - unique local addresses
876     /// - the unspecified address
877     /// - the address range reserved for documentation
878     ///
879     /// # Examples
880     ///
881     /// ```
882     /// #![feature(ip)]
883     ///
884     /// use std::net::Ipv6Addr;
885     ///
886     /// fn main() {
887     ///     assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
888     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(),
889     ///                true);
890     /// }
891     /// ```
892     pub fn is_unicast_global(&self) -> bool {
893         !self.is_multicast()
894             && !self.is_loopback() && !self.is_unicast_link_local()
895             && !self.is_unicast_site_local() && !self.is_unique_local()
896             && !self.is_unspecified() && !self.is_documentation()
897     }
898
899     /// Returns the address's multicast scope if the address is multicast.
900     ///
901     /// # Examples
902     ///
903     /// ```
904     /// #![feature(ip)]
905     ///
906     /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
907     ///
908     /// fn main() {
909     ///     assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
910     ///                              Some(Ipv6MulticastScope::Global));
911     ///     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
912     /// }
913     /// ```
914     pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
915         if self.is_multicast() {
916             match self.segments()[0] & 0x000f {
917                 1 => Some(Ipv6MulticastScope::InterfaceLocal),
918                 2 => Some(Ipv6MulticastScope::LinkLocal),
919                 3 => Some(Ipv6MulticastScope::RealmLocal),
920                 4 => Some(Ipv6MulticastScope::AdminLocal),
921                 5 => Some(Ipv6MulticastScope::SiteLocal),
922                 8 => Some(Ipv6MulticastScope::OrganizationLocal),
923                 14 => Some(Ipv6MulticastScope::Global),
924                 _ => None
925             }
926         } else {
927             None
928         }
929     }
930
931     /// Returns true if this is a multicast address (ff00::/8).
932     ///
933     /// This property is defined by [RFC 4291].
934     ///
935     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
936     /// # Examples
937     ///
938     /// ```
939     /// use std::net::Ipv6Addr;
940     ///
941     /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
942     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
943     /// ```
944     #[stable(since = "1.7.0", feature = "ip_17")]
945     pub fn is_multicast(&self) -> bool {
946         (self.segments()[0] & 0xff00) == 0xff00
947     }
948
949     /// Converts this address to an IPv4 address. Returns None if this address is
950     /// neither IPv4-compatible or IPv4-mapped.
951     ///
952     /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
953     ///
954     /// ```
955     /// use std::net::{Ipv4Addr, Ipv6Addr};
956     ///
957     /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
958     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
959     ///            Some(Ipv4Addr::new(192, 10, 2, 255)));
960     /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
961     ///            Some(Ipv4Addr::new(0, 0, 0, 1)));
962     /// ```
963     #[stable(feature = "rust1", since = "1.0.0")]
964     pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
965         match self.segments() {
966             [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
967                 Some(Ipv4Addr::new((g >> 8) as u8, g as u8,
968                                    (h >> 8) as u8, h as u8))
969             },
970             _ => None
971         }
972     }
973
974     /// Returns the sixteen eight-bit integers the IPv6 address consists of.
975     ///
976     /// ```
977     /// use std::net::Ipv6Addr;
978     ///
979     /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
980     ///            [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
981     /// ```
982     #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
983     pub fn octets(&self) -> [u8; 16] {
984         self.inner.s6_addr
985     }
986 }
987
988 #[stable(feature = "rust1", since = "1.0.0")]
989 impl fmt::Display for Ipv6Addr {
990     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
991         match self.segments() {
992             // We need special cases for :: and ::1, otherwise they're formatted
993             // as ::0.0.0.[01]
994             [0, 0, 0, 0, 0, 0, 0, 0] => write!(fmt, "::"),
995             [0, 0, 0, 0, 0, 0, 0, 1] => write!(fmt, "::1"),
996             // Ipv4 Compatible address
997             [0, 0, 0, 0, 0, 0, g, h] => {
998                 write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
999                        (h >> 8) as u8, h as u8)
1000             }
1001             // Ipv4-Mapped address
1002             [0, 0, 0, 0, 0, 0xffff, g, h] => {
1003                 write!(fmt, "::ffff:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
1004                        (h >> 8) as u8, h as u8)
1005             },
1006             _ => {
1007                 fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) {
1008                     let mut longest_span_len = 0;
1009                     let mut longest_span_at = 0;
1010                     let mut cur_span_len = 0;
1011                     let mut cur_span_at = 0;
1012
1013                     for i in 0..8 {
1014                         if segments[i] == 0 {
1015                             if cur_span_len == 0 {
1016                                 cur_span_at = i;
1017                             }
1018
1019                             cur_span_len += 1;
1020
1021                             if cur_span_len > longest_span_len {
1022                                 longest_span_len = cur_span_len;
1023                                 longest_span_at = cur_span_at;
1024                             }
1025                         } else {
1026                             cur_span_len = 0;
1027                             cur_span_at = 0;
1028                         }
1029                     }
1030
1031                     (longest_span_at, longest_span_len)
1032                 }
1033
1034                 let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
1035
1036                 if zeros_len > 1 {
1037                     fn fmt_subslice(segments: &[u16], fmt: &mut fmt::Formatter) -> fmt::Result {
1038                         if !segments.is_empty() {
1039                             write!(fmt, "{:x}", segments[0])?;
1040                             for &seg in &segments[1..] {
1041                                 write!(fmt, ":{:x}", seg)?;
1042                             }
1043                         }
1044                         Ok(())
1045                     }
1046
1047                     fmt_subslice(&self.segments()[..zeros_at], fmt)?;
1048                     fmt.write_str("::")?;
1049                     fmt_subslice(&self.segments()[zeros_at + zeros_len..], fmt)
1050                 } else {
1051                     let &[a, b, c, d, e, f, g, h] = &self.segments();
1052                     write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
1053                            a, b, c, d, e, f, g, h)
1054                 }
1055             }
1056         }
1057     }
1058 }
1059
1060 #[stable(feature = "rust1", since = "1.0.0")]
1061 impl fmt::Debug for Ipv6Addr {
1062     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1063         fmt::Display::fmt(self, fmt)
1064     }
1065 }
1066
1067 #[stable(feature = "rust1", since = "1.0.0")]
1068 impl Clone for Ipv6Addr {
1069     fn clone(&self) -> Ipv6Addr { *self }
1070 }
1071
1072 #[stable(feature = "rust1", since = "1.0.0")]
1073 impl PartialEq for Ipv6Addr {
1074     fn eq(&self, other: &Ipv6Addr) -> bool {
1075         self.inner.s6_addr == other.inner.s6_addr
1076     }
1077 }
1078
1079 #[stable(feature = "ip_cmp", since = "1.15.0")]
1080 impl PartialEq<IpAddr> for Ipv6Addr {
1081     fn eq(&self, other: &IpAddr) -> bool {
1082         match *other {
1083             IpAddr::V4(_) => false,
1084             IpAddr::V6(ref v6) => self == v6,
1085         }
1086     }
1087 }
1088
1089 #[stable(feature = "ip_cmp", since = "1.15.0")]
1090 impl PartialEq<Ipv6Addr> for IpAddr {
1091     fn eq(&self, other: &Ipv6Addr) -> bool {
1092         match *self {
1093             IpAddr::V4(_) => false,
1094             IpAddr::V6(ref v6) => v6 == other,
1095         }
1096     }
1097 }
1098
1099 #[stable(feature = "rust1", since = "1.0.0")]
1100 impl Eq for Ipv6Addr {}
1101
1102 #[stable(feature = "rust1", since = "1.0.0")]
1103 impl hash::Hash for Ipv6Addr {
1104     fn hash<H: hash::Hasher>(&self, s: &mut H) {
1105         self.inner.s6_addr.hash(s)
1106     }
1107 }
1108
1109 #[stable(feature = "rust1", since = "1.0.0")]
1110 impl PartialOrd for Ipv6Addr {
1111     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1112         Some(self.cmp(other))
1113     }
1114 }
1115
1116 #[stable(feature = "ip_cmp", since = "1.15.0")]
1117 impl PartialOrd<Ipv6Addr> for IpAddr {
1118     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1119         match *self {
1120             IpAddr::V4(_) => Some(Ordering::Less),
1121             IpAddr::V6(ref v6) => v6.partial_cmp(other),
1122         }
1123     }
1124 }
1125
1126 #[stable(feature = "ip_cmp", since = "1.15.0")]
1127 impl PartialOrd<IpAddr> for Ipv6Addr {
1128     fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1129         match *other {
1130             IpAddr::V4(_) => Some(Ordering::Greater),
1131             IpAddr::V6(ref v6) => self.partial_cmp(v6),
1132         }
1133     }
1134 }
1135
1136 #[stable(feature = "rust1", since = "1.0.0")]
1137 impl Ord for Ipv6Addr {
1138     fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1139         self.segments().cmp(&other.segments())
1140     }
1141 }
1142
1143 impl AsInner<c::in6_addr> for Ipv6Addr {
1144     fn as_inner(&self) -> &c::in6_addr { &self.inner }
1145 }
1146 impl FromInner<c::in6_addr> for Ipv6Addr {
1147     fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1148         Ipv6Addr { inner: addr }
1149     }
1150 }
1151
1152 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
1153 impl From<[u8; 16]> for Ipv6Addr {
1154     fn from(octets: [u8; 16]) -> Ipv6Addr {
1155         let mut inner: c::in6_addr = unsafe { mem::zeroed() };
1156         inner.s6_addr = octets;
1157         Ipv6Addr::from_inner(inner)
1158     }
1159 }
1160
1161 #[stable(feature = "ipv6_from_segments", since = "1.15.0")]
1162 impl From<[u16; 8]> for Ipv6Addr {
1163     fn from(segments: [u16; 8]) -> Ipv6Addr {
1164         let [a, b, c, d, e, f, g, h] = segments;
1165         Ipv6Addr::new(a, b, c, d, e, f, g, h)
1166     }
1167 }
1168
1169 // Tests for this module
1170 #[cfg(all(test, not(target_os = "emscripten")))]
1171 mod tests {
1172     use net::*;
1173     use net::Ipv6MulticastScope::*;
1174     use net::test::{tsa, sa6, sa4};
1175
1176     #[test]
1177     fn test_from_str_ipv4() {
1178         assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
1179         assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
1180         assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
1181
1182         // out of range
1183         let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
1184         assert_eq!(None, none);
1185         // too short
1186         let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
1187         assert_eq!(None, none);
1188         // too long
1189         let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok();
1190         assert_eq!(None, none);
1191         // no number between dots
1192         let none: Option<Ipv4Addr> = "255.0..1".parse().ok();
1193         assert_eq!(None, none);
1194     }
1195
1196     #[test]
1197     fn test_from_str_ipv6() {
1198         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
1199         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
1200
1201         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
1202         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
1203
1204         assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
1205                 "2a02:6b8::11:11".parse());
1206
1207         // too long group
1208         let none: Option<Ipv6Addr> = "::00000".parse().ok();
1209         assert_eq!(None, none);
1210         // too short
1211         let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
1212         assert_eq!(None, none);
1213         // too long
1214         let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
1215         assert_eq!(None, none);
1216         // triple colon
1217         let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
1218         assert_eq!(None, none);
1219         // two double colons
1220         let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok();
1221         assert_eq!(None, none);
1222     }
1223
1224     #[test]
1225     fn test_from_str_ipv4_in_ipv6() {
1226         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)),
1227                 "::192.0.2.33".parse());
1228         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)),
1229                 "::FFFF:192.0.2.33".parse());
1230         assert_eq!(Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
1231                 "64:ff9b::192.0.2.33".parse());
1232         assert_eq!(Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
1233                 "2001:db8:122:c000:2:2100:192.0.2.33".parse());
1234
1235         // colon after v4
1236         let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
1237         assert_eq!(None, none);
1238         // not enough groups
1239         let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok();
1240         assert_eq!(None, none);
1241         // too many groups
1242         let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
1243         assert_eq!(None, none);
1244     }
1245
1246     #[test]
1247     fn test_from_str_socket_addr() {
1248         assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)),
1249                    "77.88.21.11:80".parse());
1250         assert_eq!(Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)),
1251                    "77.88.21.11:80".parse());
1252         assert_eq!(Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
1253                    "[2a02:6b8:0:1::1]:53".parse());
1254         assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1,
1255                                                       0, 0, 0, 1), 53, 0, 0)),
1256                    "[2a02:6b8:0:1::1]:53".parse());
1257         assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
1258                    "[::127.0.0.1]:22".parse());
1259         assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0,
1260                                                       0x7F00, 1), 22, 0, 0)),
1261                    "[::127.0.0.1]:22".parse());
1262
1263         // without port
1264         let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
1265         assert_eq!(None, none);
1266         // without port
1267         let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
1268         assert_eq!(None, none);
1269         // wrong brackets around v4
1270         let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
1271         assert_eq!(None, none);
1272         // port out of range
1273         let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
1274         assert_eq!(None, none);
1275     }
1276
1277     #[test]
1278     fn ipv6_addr_to_string() {
1279         // ipv4-mapped address
1280         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
1281         assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
1282
1283         // ipv4-compatible address
1284         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
1285         assert_eq!(a1.to_string(), "::192.0.2.128");
1286
1287         // v6 address with no zero segments
1288         assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
1289                    "8:9:a:b:c:d:e:f");
1290
1291         // reduce a single run of zeros
1292         assert_eq!("ae::ffff:102:304",
1293                    Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string());
1294
1295         // don't reduce just a single zero segment
1296         assert_eq!("1:2:3:4:5:6:0:8",
1297                    Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
1298
1299         // 'any' address
1300         assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
1301
1302         // loopback address
1303         assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
1304
1305         // ends in zeros
1306         assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
1307
1308         // two runs of zeros, second one is longer
1309         assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
1310
1311         // two runs of zeros, equal length
1312         assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
1313     }
1314
1315     #[test]
1316     fn ipv4_to_ipv6() {
1317         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
1318                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped());
1319         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
1320                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible());
1321     }
1322
1323     #[test]
1324     fn ipv6_to_ipv4() {
1325         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
1326                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1327         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1328                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1329         assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1330                    None);
1331     }
1332
1333     #[test]
1334     fn ip_properties() {
1335         fn check4(octets: &[u8; 4], unspec: bool, loopback: bool,
1336                   global: bool, multicast: bool, documentation: bool) {
1337             let ip = IpAddr::V4(Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]));
1338             assert_eq!(ip.is_unspecified(), unspec);
1339             assert_eq!(ip.is_loopback(), loopback);
1340             assert_eq!(ip.is_global(), global);
1341             assert_eq!(ip.is_multicast(), multicast);
1342             assert_eq!(ip.is_documentation(), documentation);
1343         }
1344
1345         fn check6(str_addr: &str, unspec: bool, loopback: bool,
1346                   global: bool, u_doc: bool, mcast: bool) {
1347             let ip = IpAddr::V6(str_addr.parse().unwrap());
1348             assert_eq!(ip.is_unspecified(), unspec);
1349             assert_eq!(ip.is_loopback(), loopback);
1350             assert_eq!(ip.is_global(), global);
1351             assert_eq!(ip.is_documentation(), u_doc);
1352             assert_eq!(ip.is_multicast(), mcast);
1353         }
1354
1355         //     address                unspec loopbk global multicast doc
1356         check4(&[0, 0, 0, 0],         true,  false, false,  false,   false);
1357         check4(&[0, 0, 0, 1],         false, false, true,   false,   false);
1358         check4(&[0, 1, 0, 0],         false, false, true,   false,   false);
1359         check4(&[10, 9, 8, 7],        false, false, false,  false,   false);
1360         check4(&[127, 1, 2, 3],       false, true,  false,  false,   false);
1361         check4(&[172, 31, 254, 253],  false, false, false,  false,   false);
1362         check4(&[169, 254, 253, 242], false, false, false,  false,   false);
1363         check4(&[192, 0, 2, 183],     false, false, false,  false,   true);
1364         check4(&[192, 1, 2, 183],     false, false, true,   false,   false);
1365         check4(&[192, 168, 254, 253], false, false, false,  false,   false);
1366         check4(&[198, 51, 100, 0],    false, false, false,  false,   true);
1367         check4(&[203, 0, 113, 0],     false, false, false,  false,   true);
1368         check4(&[203, 2, 113, 0],     false, false, true,   false,   false);
1369         check4(&[224, 0, 0, 0],       false, false, true,   true,    false);
1370         check4(&[239, 255, 255, 255], false, false, true,   true,    false);
1371         check4(&[255, 255, 255, 255], false, false, false,  false,   false);
1372
1373         //     address                            unspec loopbk global doc    mcast
1374         check6("::",                              true,  false, false, false, false);
1375         check6("::1",                             false, true,  false, false, false);
1376         check6("::0.0.0.2",                       false, false, true,  false, false);
1377         check6("1::",                             false, false, true,  false, false);
1378         check6("fc00::",                          false, false, false, false, false);
1379         check6("fdff:ffff::",                     false, false, false, false, false);
1380         check6("fe80:ffff::",                     false, false, false, false, false);
1381         check6("febf:ffff::",                     false, false, false, false, false);
1382         check6("fec0::",                          false, false, false, false, false);
1383         check6("ff01::",                          false, false, false, false, true);
1384         check6("ff02::",                          false, false, false, false, true);
1385         check6("ff03::",                          false, false, false, false, true);
1386         check6("ff04::",                          false, false, false, false, true);
1387         check6("ff05::",                          false, false, false, false, true);
1388         check6("ff08::",                          false, false, false, false, true);
1389         check6("ff0e::",                          false, false, true,  false, true);
1390         check6("2001:db8:85a3::8a2e:370:7334",    false, false, false, true,  false);
1391         check6("102:304:506:708:90a:b0c:d0e:f10", false, false, true,  false, false);
1392     }
1393
1394     #[test]
1395     fn ipv4_properties() {
1396         fn check(octets: &[u8; 4], unspec: bool, loopback: bool,
1397                  private: bool, link_local: bool, global: bool,
1398                  multicast: bool, broadcast: bool, documentation: bool) {
1399             let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
1400             assert_eq!(octets, &ip.octets());
1401
1402             assert_eq!(ip.is_unspecified(), unspec);
1403             assert_eq!(ip.is_loopback(), loopback);
1404             assert_eq!(ip.is_private(), private);
1405             assert_eq!(ip.is_link_local(), link_local);
1406             assert_eq!(ip.is_global(), global);
1407             assert_eq!(ip.is_multicast(), multicast);
1408             assert_eq!(ip.is_broadcast(), broadcast);
1409             assert_eq!(ip.is_documentation(), documentation);
1410         }
1411
1412         //    address                unspec loopbk privt  linloc global multicast brdcast doc
1413         check(&[0, 0, 0, 0],         true,  false, false, false, false,  false,    false,  false);
1414         check(&[0, 0, 0, 1],         false, false, false, false, true,   false,    false,  false);
1415         check(&[0, 1, 0, 0],         false, false, false, false, true,   false,    false,  false);
1416         check(&[10, 9, 8, 7],        false, false, true,  false, false,  false,    false,  false);
1417         check(&[127, 1, 2, 3],       false, true,  false, false, false,  false,    false,  false);
1418         check(&[172, 31, 254, 253],  false, false, true,  false, false,  false,    false,  false);
1419         check(&[169, 254, 253, 242], false, false, false, true,  false,  false,    false,  false);
1420         check(&[192, 0, 2, 183],     false, false, false, false, false,  false,    false,  true);
1421         check(&[192, 1, 2, 183],     false, false, false, false, true,   false,    false,  false);
1422         check(&[192, 168, 254, 253], false, false, true,  false, false,  false,    false,  false);
1423         check(&[198, 51, 100, 0],    false, false, false, false, false,  false,    false,  true);
1424         check(&[203, 0, 113, 0],     false, false, false, false, false,  false,    false,  true);
1425         check(&[203, 2, 113, 0],     false, false, false, false, true,   false,    false,  false);
1426         check(&[224, 0, 0, 0],       false, false, false, false, true,   true,     false,  false);
1427         check(&[239, 255, 255, 255], false, false, false, false, true,   true,     false,  false);
1428         check(&[255, 255, 255, 255], false, false, false, false, false,  false,    true,   false);
1429     }
1430
1431     #[test]
1432     fn ipv6_properties() {
1433         fn check(str_addr: &str, octets: &[u8; 16], unspec: bool, loopback: bool,
1434                  unique_local: bool, global: bool,
1435                  u_link_local: bool, u_site_local: bool, u_global: bool, u_doc: bool,
1436                  m_scope: Option<Ipv6MulticastScope>) {
1437             let ip: Ipv6Addr = str_addr.parse().unwrap();
1438             assert_eq!(str_addr, ip.to_string());
1439             assert_eq!(&ip.octets(), octets);
1440             assert_eq!(Ipv6Addr::from(*octets), ip);
1441
1442             assert_eq!(ip.is_unspecified(), unspec);
1443             assert_eq!(ip.is_loopback(), loopback);
1444             assert_eq!(ip.is_unique_local(), unique_local);
1445             assert_eq!(ip.is_global(), global);
1446             assert_eq!(ip.is_unicast_link_local(), u_link_local);
1447             assert_eq!(ip.is_unicast_site_local(), u_site_local);
1448             assert_eq!(ip.is_unicast_global(), u_global);
1449             assert_eq!(ip.is_documentation(), u_doc);
1450             assert_eq!(ip.multicast_scope(), m_scope);
1451             assert_eq!(ip.is_multicast(), m_scope.is_some());
1452         }
1453
1454         //    unspec loopbk uniqlo global unill  unisl  uniglo doc    mscope
1455         check("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1456               true,  false, false, false, false, false, false, false, None);
1457         check("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
1458               false, true,  false, false, false, false, false, false, None);
1459         check("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
1460               false, false, false, true,  false, false, true,  false, None);
1461         check("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1462               false, false, false, true,  false, false, true,  false, None);
1463         check("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1464               false, false, true,  false, false, false, false, false, None);
1465         check("fdff:ffff::", &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1466               false, false, true,  false, false, false, false, false, None);
1467         check("fe80:ffff::", &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1468               false, false, false, false, true,  false, false, false, None);
1469         check("febf:ffff::", &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1470               false, false, false, false, true,  false, false, false, None);
1471         check("fec0::", &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1472               false, false, false, false, false, true,  false, false, None);
1473         check("ff01::", &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1474               false, false, false, false, false, false, false, false, Some(InterfaceLocal));
1475         check("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1476               false, false, false, false, false, false, false, false, Some(LinkLocal));
1477         check("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1478               false, false, false, false, false, false, false, false, Some(RealmLocal));
1479         check("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1480               false, false, false, false, false, false, false, false, Some(AdminLocal));
1481         check("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1482               false, false, false, false, false, false, false, false, Some(SiteLocal));
1483         check("ff08::", &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1484               false, false, false, false, false, false, false, false, Some(OrganizationLocal));
1485         check("ff0e::", &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1486               false, false, false, true,  false, false, false, false, Some(Global));
1487         check("2001:db8:85a3::8a2e:370:7334",
1488               &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
1489               false, false, false, false, false, false, false, true, None);
1490         check("102:304:506:708:90a:b0c:d0e:f10",
1491               &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1492               false, false, false, true,  false, false, true,  false, None);
1493     }
1494
1495     #[test]
1496     fn to_socket_addr_socketaddr() {
1497         let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345);
1498         assert_eq!(Ok(vec![a]), tsa(a));
1499     }
1500
1501     #[test]
1502     fn test_ipv4_to_int() {
1503         let a = Ipv4Addr::new(127, 0, 0, 1);
1504         assert_eq!(u32::from(a), 2130706433);
1505     }
1506
1507     #[test]
1508     fn test_int_to_ipv4() {
1509         let a = Ipv4Addr::new(127, 0, 0, 1);
1510         assert_eq!(Ipv4Addr::from(2130706433), a);
1511     }
1512
1513     #[test]
1514     fn ipv4_from_octets() {
1515         assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
1516     }
1517
1518     #[test]
1519     fn ipv6_from_segments() {
1520         let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1521                                         0x8899, 0xaabb, 0xccdd, 0xeeff]);
1522         let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677,
1523                                 0x8899, 0xaabb, 0xccdd, 0xeeff);
1524         assert_eq!(new, from_u16s);
1525     }
1526
1527     #[test]
1528     fn ipv6_from_octets() {
1529         let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1530                                         0x8899, 0xaabb, 0xccdd, 0xeeff]);
1531         let from_u8s = Ipv6Addr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
1532                                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
1533         assert_eq!(from_u16s, from_u8s);
1534     }
1535
1536     #[test]
1537     fn cmp() {
1538         let v41 = Ipv4Addr::new(100, 64, 3, 3);
1539         let v42 = Ipv4Addr::new(192, 0, 2, 2);
1540         let v61 = "2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap();
1541         let v62 = "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap();
1542         assert!(v41 < v42);
1543         assert!(v61 < v62);
1544
1545         assert_eq!(v41, IpAddr::V4(v41));
1546         assert_eq!(v61, IpAddr::V6(v61));
1547         assert!(v41 != IpAddr::V4(v42));
1548         assert!(v61 != IpAddr::V6(v62));
1549
1550         assert!(v41 < IpAddr::V4(v42));
1551         assert!(v61 < IpAddr::V6(v62));
1552         assert!(IpAddr::V4(v41) < v42);
1553         assert!(IpAddr::V6(v61) < v62);
1554
1555         assert!(v41 < IpAddr::V6(v61));
1556         assert!(IpAddr::V4(v41) < v61);
1557     }
1558
1559     #[test]
1560     fn is_v4() {
1561         let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
1562         assert!(ip.is_ipv4());
1563         assert!(!ip.is_ipv6());
1564     }
1565
1566     #[test]
1567     fn is_v6() {
1568         let ip = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
1569         assert!(!ip.is_ipv4());
1570         assert!(ip.is_ipv6());
1571     }
1572 }