]> git.lizzy.rs Git - rust.git/blob - src/libstd/net/ip.rs
2a8bd0c88beb6f535d2a53d81b839d3eb9cab951
[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 #[stable(feature = "ip_addr", since = "1.7.0")]
26 #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
27 pub enum IpAddr {
28     /// Representation of an IPv4 address.
29     #[stable(feature = "ip_addr", since = "1.7.0")]
30     V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
31     /// Representation of an IPv6 address.
32     #[stable(feature = "ip_addr", since = "1.7.0")]
33     V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
34 }
35
36 /// Representation of an IPv4 address.
37 #[derive(Copy)]
38 #[stable(feature = "rust1", since = "1.0.0")]
39 pub struct Ipv4Addr {
40     inner: c::in_addr,
41 }
42
43 /// Representation of an IPv6 address.
44 #[derive(Copy)]
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub struct Ipv6Addr {
47     inner: c::in6_addr,
48 }
49
50 #[allow(missing_docs)]
51 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
52 pub enum Ipv6MulticastScope {
53     InterfaceLocal,
54     LinkLocal,
55     RealmLocal,
56     AdminLocal,
57     SiteLocal,
58     OrganizationLocal,
59     Global
60 }
61
62 impl IpAddr {
63     /// Returns true for the special 'unspecified' address ([IPv4], [IPv6]).
64     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
65     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
66     #[unstable(feature="ip", issue="27709",
67                reason="recently added and depends on unstable Ipv4Addr.is_unspecified()")]
68     pub fn is_unspecified(&self) -> bool {
69         match *self {
70             IpAddr::V4(ref a) => a.is_unspecified(),
71             IpAddr::V6(ref a) => a.is_unspecified(),
72         }
73     }
74
75     /// Returns true if this is a loopback address ([IPv4], [IPv6]).
76     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
77     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
78     #[unstable(feature="ip", reason="recently added", issue="27709")]
79     pub fn is_loopback(&self) -> bool {
80         match *self {
81             IpAddr::V4(ref a) => a.is_loopback(),
82             IpAddr::V6(ref a) => a.is_loopback(),
83         }
84     }
85
86     /// Returns true if the address appears to be globally routable ([IPv4], [IPv6]).
87     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
88     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
89     #[unstable(feature="ip", issue="27709",
90                reason="recently added and depends on unstable Ip{v4,v6}Addr.is_global()")]
91     pub fn is_global(&self) -> bool {
92         match *self {
93             IpAddr::V4(ref a) => a.is_global(),
94             IpAddr::V6(ref a) => a.is_global(),
95         }
96     }
97
98     /// Returns true if this is a multicast address ([IPv4], [IPv6]).
99     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
100     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
101     #[unstable(feature="ip", reason="recently added", issue="27709")]
102     pub fn is_multicast(&self) -> bool {
103         match *self {
104             IpAddr::V4(ref a) => a.is_multicast(),
105             IpAddr::V6(ref a) => a.is_multicast(),
106         }
107     }
108
109     /// Returns true if this address is in a range designated for documentation ([IPv4], [IPv6]).
110     /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
111     /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
112     #[unstable(feature="ip", issue="27709",
113                reason="recently added and depends on unstable Ipv6Addr.is_documentation()")]
114     pub fn is_documentation(&self) -> bool {
115         match *self {
116             IpAddr::V4(ref a) => a.is_documentation(),
117             IpAddr::V6(ref a) => a.is_documentation(),
118         }
119     }
120 }
121
122 impl Ipv4Addr {
123     /// Creates a new IPv4 address from four eight-bit octets.
124     ///
125     /// The result will represent the IP address `a`.`b`.`c`.`d`.
126     #[stable(feature = "rust1", since = "1.0.0")]
127     pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
128         Ipv4Addr {
129             inner: c::in_addr {
130                 s_addr: hton(((a as u32) << 24) |
131                              ((b as u32) << 16) |
132                              ((c as u32) <<  8) |
133                               (d as u32)),
134             }
135         }
136     }
137
138     /// Returns the four eight-bit integers that make up this address.
139     #[stable(feature = "rust1", since = "1.0.0")]
140     pub fn octets(&self) -> [u8; 4] {
141         let bits = ntoh(self.inner.s_addr);
142         [(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
143     }
144
145     /// Returns true for the special 'unspecified' address (0.0.0.0).
146     ///
147     /// This property is defined in _UNIX Network Programming, Second Edition_,
148     /// W. Richard Stevens, p. 891; see also [ip7]
149     /// [ip7][http://man7.org/linux/man-pages/man7/ip.7.html]
150     pub fn is_unspecified(&self) -> bool {
151         self.inner.s_addr == 0
152     }
153
154     /// Returns true if this is a loopback address (127.0.0.0/8).
155     ///
156     /// This property is defined by [RFC 1122].
157     /// [RFC 1122]: https://tools.ietf.org/html/rfc1122
158     #[stable(since = "1.7.0", feature = "ip_17")]
159     pub fn is_loopback(&self) -> bool {
160         self.octets()[0] == 127
161     }
162
163     /// Returns true if this is a private address.
164     ///
165     /// The private address ranges are defined in [RFC 1918] and include:
166     /// [RFC 1918]: https://tools.ietf.org/html/rfc1918
167     ///
168     ///  - 10.0.0.0/8
169     ///  - 172.16.0.0/12
170     ///  - 192.168.0.0/16
171     #[stable(since = "1.7.0", feature = "ip_17")]
172     pub fn is_private(&self) -> bool {
173         match (self.octets()[0], self.octets()[1]) {
174             (10, _) => true,
175             (172, b) if b >= 16 && b <= 31 => true,
176             (192, 168) => true,
177             _ => false
178         }
179     }
180
181     /// Returns true if the address is link-local (169.254.0.0/16).
182     ///
183     /// This property is defined by [RFC 3927].
184     /// [RFC 3927]: https://tools.ietf.org/html/rfc3927
185     #[stable(since = "1.7.0", feature = "ip_17")]
186     pub fn is_link_local(&self) -> bool {
187         self.octets()[0] == 169 && self.octets()[1] == 254
188     }
189
190     /// Returns true if the address appears to be globally routable.
191     /// See [iana-ipv4-special-registry][ipv4-sr].
192     /// [ipv4-sr]: http://goo.gl/RaZ7lg
193     ///
194     /// The following return false:
195     ///
196     /// - private address (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16)
197     /// - the loopback address (127.0.0.0/8)
198     /// - the link-local address (169.254.0.0/16)
199     /// - the broadcast address (255.255.255.255/32)
200     /// - test addresses used for documentation (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24)
201     /// - the unspecified address (0.0.0.0)
202     pub fn is_global(&self) -> bool {
203         !self.is_private() && !self.is_loopback() && !self.is_link_local() &&
204         !self.is_broadcast() && !self.is_documentation() && !self.is_unspecified()
205     }
206
207     /// Returns true if this is a multicast address (224.0.0.0/4).
208     ///
209     /// Multicast addresses have a most significant octet between 224 and 239,
210     /// and is defined by [RFC 5771].
211     /// [RFC 5771]: https://tools.ietf.org/html/rfc5771
212     #[stable(since = "1.7.0", feature = "ip_17")]
213     pub fn is_multicast(&self) -> bool {
214         self.octets()[0] >= 224 && self.octets()[0] <= 239
215     }
216
217     /// Returns true if this is a broadcast address (255.255.255.255).
218     ///
219     /// A broadcast address has all octets set to 255 as defined in [RFC 919].
220     /// [RFC 919]: https://tools.ietf.org/html/rfc919
221     #[stable(since = "1.7.0", feature = "ip_17")]
222     pub fn is_broadcast(&self) -> bool {
223         self.octets()[0] == 255 && self.octets()[1] == 255 &&
224         self.octets()[2] == 255 && self.octets()[3] == 255
225     }
226
227     /// Returns true if this address is in a range designated for documentation.
228     ///
229     /// This is defined in [RFC 5737]:
230     /// [RFC 5737]: https://tools.ietf.org/html/rfc5737
231     ///
232     /// - 192.0.2.0/24 (TEST-NET-1)
233     /// - 198.51.100.0/24 (TEST-NET-2)
234     /// - 203.0.113.0/24 (TEST-NET-3)
235     #[stable(since = "1.7.0", feature = "ip_17")]
236     pub fn is_documentation(&self) -> bool {
237         match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) {
238             (192, 0, 2, _) => true,
239             (198, 51, 100, _) => true,
240             (203, 0, 113, _) => true,
241             _ => false
242         }
243     }
244
245     /// Converts this address to an IPv4-compatible IPv6 address.
246     ///
247     /// a.b.c.d becomes ::a.b.c.d
248     #[stable(feature = "rust1", since = "1.0.0")]
249     pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
250         Ipv6Addr::new(0, 0, 0, 0, 0, 0,
251                       ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
252                       ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
253     }
254
255     /// Converts this address to an IPv4-mapped IPv6 address.
256     ///
257     /// a.b.c.d becomes ::ffff:a.b.c.d
258     #[stable(feature = "rust1", since = "1.0.0")]
259     pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
260         Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff,
261                       ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
262                       ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
263     }
264 }
265
266 #[stable(feature = "rust1", since = "1.0.0")]
267 #[allow(deprecated)]
268 impl fmt::Display for IpAddr {
269     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
270         match *self {
271             IpAddr::V4(ref a) => a.fmt(fmt),
272             IpAddr::V6(ref a) => a.fmt(fmt),
273         }
274     }
275 }
276
277 #[stable(feature = "rust1", since = "1.0.0")]
278 impl fmt::Display for Ipv4Addr {
279     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
280         let octets = self.octets();
281         write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
282     }
283 }
284
285 #[stable(feature = "rust1", since = "1.0.0")]
286 impl fmt::Debug for Ipv4Addr {
287     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
288         fmt::Display::fmt(self, fmt)
289     }
290 }
291
292 #[stable(feature = "rust1", since = "1.0.0")]
293 impl Clone for Ipv4Addr {
294     fn clone(&self) -> Ipv4Addr { *self }
295 }
296
297 #[stable(feature = "rust1", since = "1.0.0")]
298 impl PartialEq for Ipv4Addr {
299     fn eq(&self, other: &Ipv4Addr) -> bool {
300         self.inner.s_addr == other.inner.s_addr
301     }
302 }
303
304 #[stable(feature = "rust1", since = "1.0.0")]
305 impl Eq for Ipv4Addr {}
306
307 #[stable(feature = "rust1", since = "1.0.0")]
308 impl hash::Hash for Ipv4Addr {
309     fn hash<H: hash::Hasher>(&self, s: &mut H) {
310         self.inner.s_addr.hash(s)
311     }
312 }
313
314 #[stable(feature = "rust1", since = "1.0.0")]
315 impl PartialOrd for Ipv4Addr {
316     fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
317         Some(self.cmp(other))
318     }
319 }
320
321 #[stable(feature = "rust1", since = "1.0.0")]
322 impl Ord for Ipv4Addr {
323     fn cmp(&self, other: &Ipv4Addr) -> Ordering {
324         ntoh(self.inner.s_addr).cmp(&ntoh(other.inner.s_addr))
325     }
326 }
327
328 impl AsInner<c::in_addr> for Ipv4Addr {
329     fn as_inner(&self) -> &c::in_addr { &self.inner }
330 }
331 impl FromInner<c::in_addr> for Ipv4Addr {
332     fn from_inner(addr: c::in_addr) -> Ipv4Addr {
333         Ipv4Addr { inner: addr }
334     }
335 }
336
337 #[stable(feature = "ip_u32", since = "1.1.0")]
338 impl From<Ipv4Addr> for u32 {
339     fn from(ip: Ipv4Addr) -> u32 {
340         let ip = ip.octets();
341         ((ip[0] as u32) << 24) + ((ip[1] as u32) << 16) + ((ip[2] as u32) << 8) + (ip[3] as u32)
342     }
343 }
344
345 #[stable(feature = "ip_u32", since = "1.1.0")]
346 impl From<u32> for Ipv4Addr {
347     fn from(ip: u32) -> Ipv4Addr {
348         Ipv4Addr::new((ip >> 24) as u8, (ip >> 16) as u8, (ip >> 8) as u8, ip as u8)
349     }
350 }
351
352 #[stable(feature = "from_slice_v4", since = "1.9.0")]
353 impl From<[u8; 4]> for Ipv4Addr {
354     fn from(octets: [u8; 4]) -> Ipv4Addr {
355         Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
356     }
357 }
358
359 impl Ipv6Addr {
360     /// Creates a new IPv6 address from eight 16-bit segments.
361     ///
362     /// The result will represent the IP address a:b:c:d:e:f:g:h.
363     #[stable(feature = "rust1", since = "1.0.0")]
364     pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
365                h: u16) -> Ipv6Addr {
366         let mut addr: c::in6_addr = unsafe { mem::zeroed() };
367         addr.s6_addr = [(a >> 8) as u8, a as u8,
368                         (b >> 8) as u8, b as u8,
369                         (c >> 8) as u8, c as u8,
370                         (d >> 8) as u8, d as u8,
371                         (e >> 8) as u8, e as u8,
372                         (f >> 8) as u8, f as u8,
373                         (g >> 8) as u8, g as u8,
374                         (h >> 8) as u8, h as u8];
375         Ipv6Addr { inner: addr }
376     }
377
378     /// Returns the eight 16-bit segments that make up this address.
379     #[stable(feature = "rust1", since = "1.0.0")]
380     pub fn segments(&self) -> [u16; 8] {
381         let arr = &self.inner.s6_addr;
382         [
383             (arr[0] as u16) << 8 | (arr[1] as u16),
384             (arr[2] as u16) << 8 | (arr[3] as u16),
385             (arr[4] as u16) << 8 | (arr[5] as u16),
386             (arr[6] as u16) << 8 | (arr[7] as u16),
387             (arr[8] as u16) << 8 | (arr[9] as u16),
388             (arr[10] as u16) << 8 | (arr[11] as u16),
389             (arr[12] as u16) << 8 | (arr[13] as u16),
390             (arr[14] as u16) << 8 | (arr[15] as u16),
391         ]
392     }
393
394     /// Returns true for the special 'unspecified' address (::).
395     ///
396     /// This property is defined in [RFC 4291].
397     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
398     #[stable(since = "1.7.0", feature = "ip_17")]
399     pub fn is_unspecified(&self) -> bool {
400         self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
401     }
402
403     /// Returns true if this is a loopback address (::1).
404     ///
405     /// This property is defined in [RFC 4291].
406     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
407     #[stable(since = "1.7.0", feature = "ip_17")]
408     pub fn is_loopback(&self) -> bool {
409         self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
410     }
411
412     /// Returns true if the address appears to be globally routable.
413     ///
414     /// The following return false:
415     ///
416     /// - the loopback address
417     /// - link-local, site-local, and unique local unicast addresses
418     /// - interface-, link-, realm-, admin- and site-local multicast addresses
419     pub fn is_global(&self) -> bool {
420         match self.multicast_scope() {
421             Some(Ipv6MulticastScope::Global) => true,
422             None => self.is_unicast_global(),
423             _ => false
424         }
425     }
426
427     /// Returns true if this is a unique local address (fc00::/7).
428     ///
429     /// This property is defined in [RFC 4193].
430     /// [RFC 4193]: https://tools.ietf.org/html/rfc4193
431     pub fn is_unique_local(&self) -> bool {
432         (self.segments()[0] & 0xfe00) == 0xfc00
433     }
434
435     /// Returns true if the address is unicast and link-local (fe80::/10).
436     ///
437     /// This property is defined in [RFC 4291].
438     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
439     pub fn is_unicast_link_local(&self) -> bool {
440         (self.segments()[0] & 0xffc0) == 0xfe80
441     }
442
443     /// Returns true if this is a deprecated unicast site-local address
444     /// (fec0::/10).
445     pub fn is_unicast_site_local(&self) -> bool {
446         (self.segments()[0] & 0xffc0) == 0xfec0
447     }
448
449     /// Returns true if this is an address reserved for documentation
450     /// (2001:db8::/32).
451     ///
452     /// This property is defined in [RFC 3849].
453     /// [RFC 3849]: https://tools.ietf.org/html/rfc3849
454     pub fn is_documentation(&self) -> bool {
455         (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
456     }
457
458     /// Returns true if the address is a globally routable unicast address.
459     ///
460     /// The following return false:
461     ///
462     /// - the loopback address
463     /// - the link-local addresses
464     /// - the (deprecated) site-local addresses
465     /// - unique local addresses
466     /// - the unspecified address
467     /// - the address range reserved for documentation
468     pub fn is_unicast_global(&self) -> bool {
469         !self.is_multicast()
470             && !self.is_loopback() && !self.is_unicast_link_local()
471             && !self.is_unicast_site_local() && !self.is_unique_local()
472             && !self.is_unspecified() && !self.is_documentation()
473     }
474
475     /// Returns the address's multicast scope if the address is multicast.
476     pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
477         if self.is_multicast() {
478             match self.segments()[0] & 0x000f {
479                 1 => Some(Ipv6MulticastScope::InterfaceLocal),
480                 2 => Some(Ipv6MulticastScope::LinkLocal),
481                 3 => Some(Ipv6MulticastScope::RealmLocal),
482                 4 => Some(Ipv6MulticastScope::AdminLocal),
483                 5 => Some(Ipv6MulticastScope::SiteLocal),
484                 8 => Some(Ipv6MulticastScope::OrganizationLocal),
485                 14 => Some(Ipv6MulticastScope::Global),
486                 _ => None
487             }
488         } else {
489             None
490         }
491     }
492
493     /// Returns true if this is a multicast address (ff00::/8).
494     ///
495     /// This property is defined by [RFC 4291].
496     /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
497     #[stable(since = "1.7.0", feature = "ip_17")]
498     pub fn is_multicast(&self) -> bool {
499         (self.segments()[0] & 0xff00) == 0xff00
500     }
501
502     /// Converts this address to an IPv4 address. Returns None if this address is
503     /// neither IPv4-compatible or IPv4-mapped.
504     ///
505     /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
506     #[stable(feature = "rust1", since = "1.0.0")]
507     pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
508         match self.segments() {
509             [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
510                 Some(Ipv4Addr::new((g >> 8) as u8, g as u8,
511                                    (h >> 8) as u8, h as u8))
512             },
513             _ => None
514         }
515     }
516
517     /// Returns the sixteen eight-bit integers the IPv6 address consists of.
518     #[unstable(feature = "ipv6_to_octets", reason = "needs some testing",
519                issue = "32313")]
520     pub fn octets(&self) -> [u8; 16] {
521         self.inner.s6_addr
522     }
523 }
524
525 #[stable(feature = "rust1", since = "1.0.0")]
526 impl fmt::Display for Ipv6Addr {
527     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
528         match self.segments() {
529             // We need special cases for :: and ::1, otherwise they're formatted
530             // as ::0.0.0.[01]
531             [0, 0, 0, 0, 0, 0, 0, 0] => write!(fmt, "::"),
532             [0, 0, 0, 0, 0, 0, 0, 1] => write!(fmt, "::1"),
533             // Ipv4 Compatible address
534             [0, 0, 0, 0, 0, 0, g, h] => {
535                 write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
536                        (h >> 8) as u8, h as u8)
537             }
538             // Ipv4-Mapped address
539             [0, 0, 0, 0, 0, 0xffff, g, h] => {
540                 write!(fmt, "::ffff:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
541                        (h >> 8) as u8, h as u8)
542             },
543             _ => {
544                 fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) {
545                     let mut longest_span_len = 0;
546                     let mut longest_span_at = 0;
547                     let mut cur_span_len = 0;
548                     let mut cur_span_at = 0;
549
550                     for i in 0..8 {
551                         if segments[i] == 0 {
552                             if cur_span_len == 0 {
553                                 cur_span_at = i;
554                             }
555
556                             cur_span_len += 1;
557
558                             if cur_span_len > longest_span_len {
559                                 longest_span_len = cur_span_len;
560                                 longest_span_at = cur_span_at;
561                             }
562                         } else {
563                             cur_span_len = 0;
564                             cur_span_at = 0;
565                         }
566                     }
567
568                     (longest_span_at, longest_span_len)
569                 }
570
571                 let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
572
573                 if zeros_len > 1 {
574                     fn fmt_subslice(segments: &[u16], fmt: &mut fmt::Formatter) -> fmt::Result {
575                         if !segments.is_empty() {
576                             write!(fmt, "{:x}", segments[0])?;
577                             for &seg in &segments[1..] {
578                                 write!(fmt, ":{:x}", seg)?;
579                             }
580                         }
581                         Ok(())
582                     }
583
584                     fmt_subslice(&self.segments()[..zeros_at], fmt)?;
585                     fmt.write_str("::")?;
586                     fmt_subslice(&self.segments()[zeros_at + zeros_len..], fmt)
587                 } else {
588                     let &[a, b, c, d, e, f, g, h] = &self.segments();
589                     write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
590                            a, b, c, d, e, f, g, h)
591                 }
592             }
593         }
594     }
595 }
596
597 #[stable(feature = "rust1", since = "1.0.0")]
598 impl fmt::Debug for Ipv6Addr {
599     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
600         fmt::Display::fmt(self, fmt)
601     }
602 }
603
604 #[stable(feature = "rust1", since = "1.0.0")]
605 impl Clone for Ipv6Addr {
606     fn clone(&self) -> Ipv6Addr { *self }
607 }
608
609 #[stable(feature = "rust1", since = "1.0.0")]
610 impl PartialEq for Ipv6Addr {
611     fn eq(&self, other: &Ipv6Addr) -> bool {
612         self.inner.s6_addr == other.inner.s6_addr
613     }
614 }
615
616 #[stable(feature = "rust1", since = "1.0.0")]
617 impl Eq for Ipv6Addr {}
618
619 #[stable(feature = "rust1", since = "1.0.0")]
620 impl hash::Hash for Ipv6Addr {
621     fn hash<H: hash::Hasher>(&self, s: &mut H) {
622         self.inner.s6_addr.hash(s)
623     }
624 }
625
626 #[stable(feature = "rust1", since = "1.0.0")]
627 impl PartialOrd for Ipv6Addr {
628     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
629         Some(self.cmp(other))
630     }
631 }
632
633 #[stable(feature = "rust1", since = "1.0.0")]
634 impl Ord for Ipv6Addr {
635     fn cmp(&self, other: &Ipv6Addr) -> Ordering {
636         self.segments().cmp(&other.segments())
637     }
638 }
639
640 impl AsInner<c::in6_addr> for Ipv6Addr {
641     fn as_inner(&self) -> &c::in6_addr { &self.inner }
642 }
643 impl FromInner<c::in6_addr> for Ipv6Addr {
644     fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
645         Ipv6Addr { inner: addr }
646     }
647 }
648
649 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
650 impl From<[u8; 16]> for Ipv6Addr {
651     fn from(octets: [u8; 16]) -> Ipv6Addr {
652         let mut inner: c::in6_addr = unsafe { mem::zeroed() };
653         inner.s6_addr = octets;
654         Ipv6Addr::from_inner(inner)
655     }
656 }
657
658 // Tests for this module
659 #[cfg(test)]
660 mod tests {
661     use prelude::v1::*;
662     use net::*;
663     use net::Ipv6MulticastScope::*;
664     use net::test::{tsa, sa6, sa4};
665
666     #[test]
667     fn test_from_str_ipv4() {
668         assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
669         assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
670         assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
671
672         // out of range
673         let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
674         assert_eq!(None, none);
675         // too short
676         let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
677         assert_eq!(None, none);
678         // too long
679         let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok();
680         assert_eq!(None, none);
681         // no number between dots
682         let none: Option<Ipv4Addr> = "255.0..1".parse().ok();
683         assert_eq!(None, none);
684     }
685
686     #[test]
687     fn test_from_str_ipv6() {
688         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
689         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
690
691         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
692         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
693
694         assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
695                 "2a02:6b8::11:11".parse());
696
697         // too long group
698         let none: Option<Ipv6Addr> = "::00000".parse().ok();
699         assert_eq!(None, none);
700         // too short
701         let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
702         assert_eq!(None, none);
703         // too long
704         let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
705         assert_eq!(None, none);
706         // triple colon
707         let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
708         assert_eq!(None, none);
709         // two double colons
710         let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok();
711         assert_eq!(None, none);
712     }
713
714     #[test]
715     fn test_from_str_ipv4_in_ipv6() {
716         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)),
717                 "::192.0.2.33".parse());
718         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)),
719                 "::FFFF:192.0.2.33".parse());
720         assert_eq!(Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
721                 "64:ff9b::192.0.2.33".parse());
722         assert_eq!(Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
723                 "2001:db8:122:c000:2:2100:192.0.2.33".parse());
724
725         // colon after v4
726         let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
727         assert_eq!(None, none);
728         // not enough groups
729         let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok();
730         assert_eq!(None, none);
731         // too many groups
732         let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
733         assert_eq!(None, none);
734     }
735
736     #[test]
737     fn test_from_str_socket_addr() {
738         assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)),
739                    "77.88.21.11:80".parse());
740         assert_eq!(Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)),
741                    "77.88.21.11:80".parse());
742         assert_eq!(Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
743                    "[2a02:6b8:0:1::1]:53".parse());
744         assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1,
745                                                       0, 0, 0, 1), 53, 0, 0)),
746                    "[2a02:6b8:0:1::1]:53".parse());
747         assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
748                    "[::127.0.0.1]:22".parse());
749         assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0,
750                                                       0x7F00, 1), 22, 0, 0)),
751                    "[::127.0.0.1]:22".parse());
752
753         // without port
754         let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
755         assert_eq!(None, none);
756         // without port
757         let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
758         assert_eq!(None, none);
759         // wrong brackets around v4
760         let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
761         assert_eq!(None, none);
762         // port out of range
763         let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
764         assert_eq!(None, none);
765     }
766
767     #[test]
768     fn ipv6_addr_to_string() {
769         // ipv4-mapped address
770         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
771         assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
772
773         // ipv4-compatible address
774         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
775         assert_eq!(a1.to_string(), "::192.0.2.128");
776
777         // v6 address with no zero segments
778         assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
779                    "8:9:a:b:c:d:e:f");
780
781         // reduce a single run of zeros
782         assert_eq!("ae::ffff:102:304",
783                    Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string());
784
785         // don't reduce just a single zero segment
786         assert_eq!("1:2:3:4:5:6:0:8",
787                    Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
788
789         // 'any' address
790         assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
791
792         // loopback address
793         assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
794
795         // ends in zeros
796         assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
797
798         // two runs of zeros, second one is longer
799         assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
800
801         // two runs of zeros, equal length
802         assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
803     }
804
805     #[test]
806     fn ipv4_to_ipv6() {
807         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
808                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped());
809         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
810                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible());
811     }
812
813     #[test]
814     fn ipv6_to_ipv4() {
815         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
816                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
817         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
818                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
819         assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
820                    None);
821     }
822
823     #[test]
824     fn ip_properties() {
825         fn check4(octets: &[u8; 4], unspec: bool, loopback: bool,
826                   global: bool, multicast: bool, documentation: bool) {
827             let ip = IpAddr::V4(Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]));
828             assert_eq!(ip.is_unspecified(), unspec);
829             assert_eq!(ip.is_loopback(), loopback);
830             assert_eq!(ip.is_global(), global);
831             assert_eq!(ip.is_multicast(), multicast);
832             assert_eq!(ip.is_documentation(), documentation);
833         }
834
835         fn check6(str_addr: &str, unspec: bool, loopback: bool,
836                   global: bool, u_doc: bool, mcast: bool) {
837             let ip = IpAddr::V6(str_addr.parse().unwrap());
838             assert_eq!(ip.is_unspecified(), unspec);
839             assert_eq!(ip.is_loopback(), loopback);
840             assert_eq!(ip.is_global(), global);
841             assert_eq!(ip.is_documentation(), u_doc);
842             assert_eq!(ip.is_multicast(), mcast);
843         }
844
845         //     address                unspec loopbk global multicast doc
846         check4(&[0, 0, 0, 0],         true,  false, false,  false,   false);
847         check4(&[0, 0, 0, 1],         false, false, true,   false,   false);
848         check4(&[0, 1, 0, 0],         false, false, true,   false,   false);
849         check4(&[10, 9, 8, 7],        false, false, false,  false,   false);
850         check4(&[127, 1, 2, 3],       false, true,  false,  false,   false);
851         check4(&[172, 31, 254, 253],  false, false, false,  false,   false);
852         check4(&[169, 254, 253, 242], false, false, false,  false,   false);
853         check4(&[192, 0, 2, 183],     false, false, false,  false,   true);
854         check4(&[192, 1, 2, 183],     false, false, true,   false,   false);
855         check4(&[192, 168, 254, 253], false, false, false,  false,   false);
856         check4(&[198, 51, 100, 0],    false, false, false,  false,   true);
857         check4(&[203, 0, 113, 0],     false, false, false,  false,   true);
858         check4(&[203, 2, 113, 0],     false, false, true,   false,   false);
859         check4(&[224, 0, 0, 0],       false, false, true,   true,    false);
860         check4(&[239, 255, 255, 255], false, false, true,   true,    false);
861         check4(&[255, 255, 255, 255], false, false, false,  false,   false);
862
863         //     address                            unspec loopbk global doc    mcast
864         check6("::",                              true,  false, false, false, false);
865         check6("::1",                             false, true,  false, false, false);
866         check6("::0.0.0.2",                       false, false, true,  false, false);
867         check6("1::",                             false, false, true,  false, false);
868         check6("fc00::",                          false, false, false, false, false);
869         check6("fdff:ffff::",                     false, false, false, false, false);
870         check6("fe80:ffff::",                     false, false, false, false, false);
871         check6("febf:ffff::",                     false, false, false, false, false);
872         check6("fec0::",                          false, false, false, false, false);
873         check6("ff01::",                          false, false, false, false, true);
874         check6("ff02::",                          false, false, false, false, true);
875         check6("ff03::",                          false, false, false, false, true);
876         check6("ff04::",                          false, false, false, false, true);
877         check6("ff05::",                          false, false, false, false, true);
878         check6("ff08::",                          false, false, false, false, true);
879         check6("ff0e::",                          false, false, true,  false, true);
880         check6("2001:db8:85a3::8a2e:370:7334",    false, false, false, true,  false);
881         check6("102:304:506:708:90a:b0c:d0e:f10", false, false, true,  false, false);
882     }
883
884     #[test]
885     fn ipv4_properties() {
886         fn check(octets: &[u8; 4], unspec: bool, loopback: bool,
887                  private: bool, link_local: bool, global: bool,
888                  multicast: bool, broadcast: bool, documentation: bool) {
889             let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
890             assert_eq!(octets, &ip.octets());
891
892             assert_eq!(ip.is_unspecified(), unspec);
893             assert_eq!(ip.is_loopback(), loopback);
894             assert_eq!(ip.is_private(), private);
895             assert_eq!(ip.is_link_local(), link_local);
896             assert_eq!(ip.is_global(), global);
897             assert_eq!(ip.is_multicast(), multicast);
898             assert_eq!(ip.is_broadcast(), broadcast);
899             assert_eq!(ip.is_documentation(), documentation);
900         }
901
902         //    address                unspec loopbk privt  linloc global multicast brdcast doc
903         check(&[0, 0, 0, 0],         true,  false, false, false, false,  false,    false,  false);
904         check(&[0, 0, 0, 1],         false, false, false, false, true,   false,    false,  false);
905         check(&[0, 1, 0, 0],         false, false, false, false, true,   false,    false,  false);
906         check(&[10, 9, 8, 7],        false, false, true,  false, false,  false,    false,  false);
907         check(&[127, 1, 2, 3],       false, true,  false, false, false,  false,    false,  false);
908         check(&[172, 31, 254, 253],  false, false, true,  false, false,  false,    false,  false);
909         check(&[169, 254, 253, 242], false, false, false, true,  false,  false,    false,  false);
910         check(&[192, 0, 2, 183],     false, false, false, false, false,  false,    false,  true);
911         check(&[192, 1, 2, 183],     false, false, false, false, true,   false,    false,  false);
912         check(&[192, 168, 254, 253], false, false, true,  false, false,  false,    false,  false);
913         check(&[198, 51, 100, 0],    false, false, false, false, false,  false,    false,  true);
914         check(&[203, 0, 113, 0],     false, false, false, false, false,  false,    false,  true);
915         check(&[203, 2, 113, 0],     false, false, false, false, true,   false,    false,  false);
916         check(&[224, 0, 0, 0],       false, false, false, false, true,   true,     false,  false);
917         check(&[239, 255, 255, 255], false, false, false, false, true,   true,     false,  false);
918         check(&[255, 255, 255, 255], false, false, false, false, false,  false,    true,   false);
919     }
920
921     #[test]
922     fn ipv6_properties() {
923         fn check(str_addr: &str, octets: &[u8; 16], unspec: bool, loopback: bool,
924                  unique_local: bool, global: bool,
925                  u_link_local: bool, u_site_local: bool, u_global: bool, u_doc: bool,
926                  m_scope: Option<Ipv6MulticastScope>) {
927             let ip: Ipv6Addr = str_addr.parse().unwrap();
928             assert_eq!(str_addr, ip.to_string());
929             assert_eq!(&ip.octets(), octets);
930             assert_eq!(Ipv6Addr::from(*octets), ip);
931
932             assert_eq!(ip.is_unspecified(), unspec);
933             assert_eq!(ip.is_loopback(), loopback);
934             assert_eq!(ip.is_unique_local(), unique_local);
935             assert_eq!(ip.is_global(), global);
936             assert_eq!(ip.is_unicast_link_local(), u_link_local);
937             assert_eq!(ip.is_unicast_site_local(), u_site_local);
938             assert_eq!(ip.is_unicast_global(), u_global);
939             assert_eq!(ip.is_documentation(), u_doc);
940             assert_eq!(ip.multicast_scope(), m_scope);
941             assert_eq!(ip.is_multicast(), m_scope.is_some());
942         }
943
944         //    unspec loopbk uniqlo global unill  unisl  uniglo doc    mscope
945         check("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
946               true,  false, false, false, false, false, false, false, None);
947         check("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
948               false, true,  false, false, false, false, false, false, None);
949         check("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
950               false, false, false, true,  false, false, true,  false, None);
951         check("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
952               false, false, false, true,  false, false, true,  false, None);
953         check("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
954               false, false, true,  false, false, false, false, false, None);
955         check("fdff:ffff::", &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
956               false, false, true,  false, false, false, false, false, None);
957         check("fe80:ffff::", &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
958               false, false, false, false, true,  false, false, false, None);
959         check("febf:ffff::", &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
960               false, false, false, false, true,  false, false, false, None);
961         check("fec0::", &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
962               false, false, false, false, false, true,  false, false, None);
963         check("ff01::", &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
964               false, false, false, false, false, false, false, false, Some(InterfaceLocal));
965         check("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
966               false, false, false, false, false, false, false, false, Some(LinkLocal));
967         check("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
968               false, false, false, false, false, false, false, false, Some(RealmLocal));
969         check("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
970               false, false, false, false, false, false, false, false, Some(AdminLocal));
971         check("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
972               false, false, false, false, false, false, false, false, Some(SiteLocal));
973         check("ff08::", &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
974               false, false, false, false, false, false, false, false, Some(OrganizationLocal));
975         check("ff0e::", &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
976               false, false, false, true,  false, false, false, false, Some(Global));
977         check("2001:db8:85a3::8a2e:370:7334",
978               &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
979               false, false, false, false, false, false, false, true, None);
980         check("102:304:506:708:90a:b0c:d0e:f10",
981               &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
982               false, false, false, true,  false, false, true,  false, None);
983     }
984
985     #[test]
986     fn to_socket_addr_socketaddr() {
987         let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345);
988         assert_eq!(Ok(vec![a]), tsa(a));
989     }
990
991     #[test]
992     fn test_ipv4_to_int() {
993         let a = Ipv4Addr::new(127, 0, 0, 1);
994         assert_eq!(u32::from(a), 2130706433);
995     }
996
997     #[test]
998     fn test_int_to_ipv4() {
999         let a = Ipv4Addr::new(127, 0, 0, 1);
1000         assert_eq!(Ipv4Addr::from(2130706433), a);
1001     }
1002
1003     #[test]
1004     fn ipv4_from_u32_slice() {
1005         assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
1006     }
1007
1008     #[test]
1009     fn ord() {
1010         assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
1011         assert!("2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap() <
1012                 "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap());
1013     }
1014 }