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.
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.
11 #![unstable(feature = "ip", reason = "extra functionality has not been \
12 scrutinized to the level that it should \
20 use net::{hton, ntoh};
21 use sys::net::netc as c;
22 use sys_common::{AsInner, FromInner};
24 /// An IP address, either an IPv4 or IPv6 address.
28 /// Constructing an IPv4 address:
31 /// use std::net::{IpAddr, Ipv4Addr};
33 /// IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
36 /// Constructing an IPv6 address:
39 /// use std::net::{IpAddr, Ipv6Addr};
41 /// IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
43 #[stable(feature = "ip_addr", since = "1.7.0")]
44 #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
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),
54 /// Representation of an IPv4 address.
56 #[stable(feature = "rust1", since = "1.0.0")]
61 /// Representation of an IPv6 address.
63 #[stable(feature = "rust1", since = "1.0.0")]
68 #[allow(missing_docs)]
69 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
70 pub enum Ipv6MulticastScope {
81 /// Returns true for the special 'unspecified' address ([IPv4], [IPv6]).
83 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
84 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
89 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
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);
94 #[stable(feature = "ip_shared", since = "1.12.0")]
95 pub fn is_unspecified(&self) -> bool {
97 IpAddr::V4(ref a) => a.is_unspecified(),
98 IpAddr::V6(ref a) => a.is_unspecified(),
102 /// Returns true if this is a loopback address ([IPv4], [IPv6]).
104 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
105 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
110 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
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);
115 #[stable(feature = "ip_shared", since = "1.12.0")]
116 pub fn is_loopback(&self) -> bool {
118 IpAddr::V4(ref a) => a.is_loopback(),
119 IpAddr::V6(ref a) => a.is_loopback(),
123 /// Returns true if the address appears to be globally routable ([IPv4], [IPv6]).
125 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
126 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
133 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
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(),
141 pub fn is_global(&self) -> bool {
143 IpAddr::V4(ref a) => a.is_global(),
144 IpAddr::V6(ref a) => a.is_global(),
148 /// Returns true if this is a multicast address ([IPv4], [IPv6]).
150 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
151 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
156 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
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);
161 #[stable(feature = "ip_shared", since = "1.12.0")]
162 pub fn is_multicast(&self) -> bool {
164 IpAddr::V4(ref a) => a.is_multicast(),
165 IpAddr::V6(ref a) => a.is_multicast(),
169 /// Returns true if this address is in a range designated for documentation ([IPv4], [IPv6]).
171 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
172 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
179 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
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);
187 pub fn is_documentation(&self) -> bool {
189 IpAddr::V4(ref a) => a.is_documentation(),
190 IpAddr::V6(ref a) => a.is_documentation(),
194 /// Returns true if this address is a valid IPv4 address, false if it's a valid IPv6 address.
199 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
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(),
207 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
208 pub fn is_ipv4(&self) -> bool {
210 IpAddr::V4(_) => true,
211 IpAddr::V6(_) => false,
215 /// Returns true if this address is a valid IPv6 address, false if it's a valid IPv4 address.
220 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
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(),
228 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
229 pub fn is_ipv6(&self) -> bool {
231 IpAddr::V4(_) => false,
232 IpAddr::V6(_) => true,
238 /// Creates a new IPv4 address from four eight-bit octets.
240 /// The result will represent the IP address `a`.`b`.`c`.`d`.
245 /// use std::net::Ipv4Addr;
247 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
249 #[stable(feature = "rust1", since = "1.0.0")]
250 pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
253 s_addr: hton(((a as u32) << 24) |
261 /// Returns the four eight-bit integers that make up this address.
266 /// use std::net::Ipv4Addr;
268 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
269 /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
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]
277 /// Returns true for the special 'unspecified' address (0.0.0.0).
279 /// This property is defined in _UNIX Network Programming, Second Edition_,
280 /// W. Richard Stevens, p. 891; see also [ip7].
282 /// [ip7]: http://man7.org/linux/man-pages/man7/ip.7.html
287 /// use std::net::Ipv4Addr;
289 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
290 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
292 #[stable(feature = "ip_shared", since = "1.12.0")]
293 pub fn is_unspecified(&self) -> bool {
294 self.inner.s_addr == 0
297 /// Returns true if this is a loopback address (127.0.0.0/8).
299 /// This property is defined by [RFC 1122].
301 /// [RFC 1122]: https://tools.ietf.org/html/rfc1122
306 /// use std::net::Ipv4Addr;
308 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
309 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
311 #[stable(since = "1.7.0", feature = "ip_17")]
312 pub fn is_loopback(&self) -> bool {
313 self.octets()[0] == 127
316 /// Returns true if this is a private address.
318 /// The private address ranges are defined in [RFC 1918] and include:
324 /// [RFC 1918]: https://tools.ietf.org/html/rfc1918
329 /// use std::net::Ipv4Addr;
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);
339 #[stable(since = "1.7.0", feature = "ip_17")]
340 pub fn is_private(&self) -> bool {
341 match (self.octets()[0], self.octets()[1]) {
343 (172, b) if b >= 16 && b <= 31 => true,
349 /// Returns true if the address is link-local (169.254.0.0/16).
351 /// This property is defined by [RFC 3927].
353 /// [RFC 3927]: https://tools.ietf.org/html/rfc3927
358 /// use std::net::Ipv4Addr;
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);
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
369 /// Returns true if the address appears to be globally routable.
370 /// See [iana-ipv4-special-registry][ipv4-sr].
372 /// The following return false:
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)
381 /// [ipv4-sr]: http://goo.gl/RaZ7lg
388 /// use std::net::Ipv4Addr;
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);
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()
403 /// Returns true if this is a multicast address (224.0.0.0/4).
405 /// Multicast addresses have a most significant octet between 224 and 239,
406 /// and is defined by [RFC 5771].
408 /// [RFC 5771]: https://tools.ietf.org/html/rfc5771
413 /// use std::net::Ipv4Addr;
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);
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
424 /// Returns true if this is a broadcast address (255.255.255.255).
426 /// A broadcast address has all octets set to 255 as defined in [RFC 919].
428 /// [RFC 919]: https://tools.ietf.org/html/rfc919
433 /// use std::net::Ipv4Addr;
435 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
436 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
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
444 /// Returns true if this address is in a range designated for documentation.
446 /// This is defined in [RFC 5737]:
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)
452 /// [RFC 5737]: https://tools.ietf.org/html/rfc5737
457 /// use std::net::Ipv4Addr;
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);
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,
474 /// Converts this address to an IPv4-compatible IPv6 address.
476 /// a.b.c.d becomes ::a.b.c.d
481 /// use std::net::{Ipv4Addr, Ipv6Addr};
483 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
484 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 767));
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)
493 /// Converts this address to an IPv4-mapped IPv6 address.
495 /// a.b.c.d becomes ::ffff:a.b.c.d
500 /// use std::net::{Ipv4Addr, Ipv6Addr};
502 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
503 /// Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 49152, 767));
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)
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 {
517 IpAddr::V4(ref a) => a.fmt(fmt),
518 IpAddr::V6(ref a) => a.fmt(fmt),
523 #[stable(feature = "ip_from_ip", since = "1.16.0")]
524 impl From<Ipv4Addr> for IpAddr {
525 fn from(ipv4: Ipv4Addr) -> IpAddr {
530 #[stable(feature = "ip_from_ip", since = "1.16.0")]
531 impl From<Ipv6Addr> for IpAddr {
532 fn from(ipv6: Ipv6Addr) -> IpAddr {
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])
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)
552 #[stable(feature = "rust1", since = "1.0.0")]
553 impl Clone for Ipv4Addr {
554 fn clone(&self) -> Ipv4Addr { *self }
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
564 #[stable(feature = "ip_cmp", since = "1.15.0")]
565 impl PartialEq<Ipv4Addr> for IpAddr {
566 fn eq(&self, other: &Ipv4Addr) -> bool {
568 IpAddr::V4(ref v4) => v4 == other,
569 IpAddr::V6(_) => false,
574 #[stable(feature = "ip_cmp", since = "1.15.0")]
575 impl PartialEq<IpAddr> for Ipv4Addr {
576 fn eq(&self, other: &IpAddr) -> bool {
578 IpAddr::V4(ref v4) => self == v4,
579 IpAddr::V6(_) => false,
584 #[stable(feature = "rust1", since = "1.0.0")]
585 impl Eq for Ipv4Addr {}
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)
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))
601 #[stable(feature = "ip_cmp", since = "1.15.0")]
602 impl PartialOrd<Ipv4Addr> for IpAddr {
603 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
605 IpAddr::V4(ref v4) => v4.partial_cmp(other),
606 IpAddr::V6(_) => Some(Ordering::Greater),
611 #[stable(feature = "ip_cmp", since = "1.15.0")]
612 impl PartialOrd<IpAddr> for Ipv4Addr {
613 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
615 IpAddr::V4(ref v4) => self.partial_cmp(v4),
616 IpAddr::V6(_) => Some(Ordering::Less),
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))
628 impl AsInner<c::in_addr> for Ipv4Addr {
629 fn as_inner(&self) -> &c::in_addr { &self.inner }
631 impl FromInner<c::in_addr> for Ipv4Addr {
632 fn from_inner(addr: c::in_addr) -> Ipv4Addr {
633 Ipv4Addr { inner: addr }
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)
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)
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])
659 #[stable(feature = "ip_from_slice", since = "1.17.0")]
660 impl From<[u8; 4]> for IpAddr {
661 fn from(octets: [u8; 4]) -> IpAddr {
662 IpAddr::V4(Ipv4Addr::from(octets))
667 /// Creates a new IPv6 address from eight 16-bit segments.
669 /// The result will represent the IP address a:b:c:d:e:f:g:h.
674 /// use std::net::Ipv6Addr;
676 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
678 #[stable(feature = "rust1", since = "1.0.0")]
679 pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
680 h: u16) -> Ipv6Addr {
681 let mut addr: c::in6_addr = unsafe { mem::zeroed() };
682 addr.s6_addr = [(a >> 8) as u8, a as u8,
683 (b >> 8) as u8, b as u8,
684 (c >> 8) as u8, c as u8,
685 (d >> 8) as u8, d as u8,
686 (e >> 8) as u8, e as u8,
687 (f >> 8) as u8, f as u8,
688 (g >> 8) as u8, g as u8,
689 (h >> 8) as u8, h as u8];
690 Ipv6Addr { inner: addr }
693 /// Returns the eight 16-bit segments that make up this address.
698 /// use std::net::Ipv6Addr;
700 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
701 /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
703 #[stable(feature = "rust1", since = "1.0.0")]
704 pub fn segments(&self) -> [u16; 8] {
705 let arr = &self.inner.s6_addr;
707 (arr[0] as u16) << 8 | (arr[1] as u16),
708 (arr[2] as u16) << 8 | (arr[3] as u16),
709 (arr[4] as u16) << 8 | (arr[5] as u16),
710 (arr[6] as u16) << 8 | (arr[7] as u16),
711 (arr[8] as u16) << 8 | (arr[9] as u16),
712 (arr[10] as u16) << 8 | (arr[11] as u16),
713 (arr[12] as u16) << 8 | (arr[13] as u16),
714 (arr[14] as u16) << 8 | (arr[15] as u16),
718 /// Returns true for the special 'unspecified' address (::).
720 /// This property is defined in [RFC 4291].
722 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
727 /// use std::net::Ipv6Addr;
729 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
730 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
732 #[stable(since = "1.7.0", feature = "ip_17")]
733 pub fn is_unspecified(&self) -> bool {
734 self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
737 /// Returns true if this is a loopback address (::1).
739 /// This property is defined in [RFC 4291].
741 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
746 /// use std::net::Ipv6Addr;
748 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
749 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
751 #[stable(since = "1.7.0", feature = "ip_17")]
752 pub fn is_loopback(&self) -> bool {
753 self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
756 /// Returns true if the address appears to be globally routable.
758 /// The following return false:
760 /// - the loopback address
761 /// - link-local, site-local, and unique local unicast addresses
762 /// - interface-, link-, realm-, admin- and site-local multicast addresses
769 /// use std::net::Ipv6Addr;
772 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
773 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
774 /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
777 pub fn is_global(&self) -> bool {
778 match self.multicast_scope() {
779 Some(Ipv6MulticastScope::Global) => true,
780 None => self.is_unicast_global(),
785 /// Returns true if this is a unique local address (fc00::/7).
787 /// This property is defined in [RFC 4193].
789 /// [RFC 4193]: https://tools.ietf.org/html/rfc4193
796 /// use std::net::Ipv6Addr;
799 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(),
801 /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
804 pub fn is_unique_local(&self) -> bool {
805 (self.segments()[0] & 0xfe00) == 0xfc00
808 /// Returns true if the address is unicast and link-local (fe80::/10).
810 /// This property is defined in [RFC 4291].
812 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
819 /// use std::net::Ipv6Addr;
822 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_link_local(),
824 /// assert_eq!(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
827 pub fn is_unicast_link_local(&self) -> bool {
828 (self.segments()[0] & 0xffc0) == 0xfe80
831 /// Returns true if this is a deprecated unicast site-local address
839 /// use std::net::Ipv6Addr;
842 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
844 /// assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
847 pub fn is_unicast_site_local(&self) -> bool {
848 (self.segments()[0] & 0xffc0) == 0xfec0
851 /// Returns true if this is an address reserved for documentation
854 /// This property is defined in [RFC 3849].
856 /// [RFC 3849]: https://tools.ietf.org/html/rfc3849
863 /// use std::net::Ipv6Addr;
866 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(),
868 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
871 pub fn is_documentation(&self) -> bool {
872 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
875 /// Returns true if the address is a globally routable unicast address.
877 /// The following return false:
879 /// - the loopback address
880 /// - the link-local addresses
881 /// - the (deprecated) site-local addresses
882 /// - unique local addresses
883 /// - the unspecified address
884 /// - the address range reserved for documentation
891 /// use std::net::Ipv6Addr;
894 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
895 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(),
899 pub fn is_unicast_global(&self) -> bool {
901 && !self.is_loopback() && !self.is_unicast_link_local()
902 && !self.is_unicast_site_local() && !self.is_unique_local()
903 && !self.is_unspecified() && !self.is_documentation()
906 /// Returns the address's multicast scope if the address is multicast.
913 /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
916 /// assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
917 /// Some(Ipv6MulticastScope::Global));
918 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
921 pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
922 if self.is_multicast() {
923 match self.segments()[0] & 0x000f {
924 1 => Some(Ipv6MulticastScope::InterfaceLocal),
925 2 => Some(Ipv6MulticastScope::LinkLocal),
926 3 => Some(Ipv6MulticastScope::RealmLocal),
927 4 => Some(Ipv6MulticastScope::AdminLocal),
928 5 => Some(Ipv6MulticastScope::SiteLocal),
929 8 => Some(Ipv6MulticastScope::OrganizationLocal),
930 14 => Some(Ipv6MulticastScope::Global),
938 /// Returns true if this is a multicast address (ff00::/8).
940 /// This property is defined by [RFC 4291].
942 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
946 /// use std::net::Ipv6Addr;
948 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
949 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
951 #[stable(since = "1.7.0", feature = "ip_17")]
952 pub fn is_multicast(&self) -> bool {
953 (self.segments()[0] & 0xff00) == 0xff00
956 /// Converts this address to an IPv4 address. Returns None if this address is
957 /// neither IPv4-compatible or IPv4-mapped.
959 /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
962 /// use std::net::{Ipv4Addr, Ipv6Addr};
964 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
965 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
966 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
967 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
968 /// Some(Ipv4Addr::new(0, 0, 0, 1)));
970 #[stable(feature = "rust1", since = "1.0.0")]
971 pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
972 match self.segments() {
973 [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
974 Some(Ipv4Addr::new((g >> 8) as u8, g as u8,
975 (h >> 8) as u8, h as u8))
981 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
984 /// use std::net::Ipv6Addr;
986 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
987 /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
989 #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
990 pub fn octets(&self) -> [u8; 16] {
995 #[stable(feature = "rust1", since = "1.0.0")]
996 impl fmt::Display for Ipv6Addr {
997 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
998 match self.segments() {
999 // We need special cases for :: and ::1, otherwise they're formatted
1001 [0, 0, 0, 0, 0, 0, 0, 0] => write!(fmt, "::"),
1002 [0, 0, 0, 0, 0, 0, 0, 1] => write!(fmt, "::1"),
1003 // Ipv4 Compatible address
1004 [0, 0, 0, 0, 0, 0, g, h] => {
1005 write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
1006 (h >> 8) as u8, h as u8)
1008 // Ipv4-Mapped address
1009 [0, 0, 0, 0, 0, 0xffff, g, h] => {
1010 write!(fmt, "::ffff:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
1011 (h >> 8) as u8, h as u8)
1014 fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) {
1015 let mut longest_span_len = 0;
1016 let mut longest_span_at = 0;
1017 let mut cur_span_len = 0;
1018 let mut cur_span_at = 0;
1021 if segments[i] == 0 {
1022 if cur_span_len == 0 {
1028 if cur_span_len > longest_span_len {
1029 longest_span_len = cur_span_len;
1030 longest_span_at = cur_span_at;
1038 (longest_span_at, longest_span_len)
1041 let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
1044 fn fmt_subslice(segments: &[u16], fmt: &mut fmt::Formatter) -> fmt::Result {
1045 if !segments.is_empty() {
1046 write!(fmt, "{:x}", segments[0])?;
1047 for &seg in &segments[1..] {
1048 write!(fmt, ":{:x}", seg)?;
1054 fmt_subslice(&self.segments()[..zeros_at], fmt)?;
1055 fmt.write_str("::")?;
1056 fmt_subslice(&self.segments()[zeros_at + zeros_len..], fmt)
1058 let &[a, b, c, d, e, f, g, h] = &self.segments();
1059 write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
1060 a, b, c, d, e, f, g, h)
1067 #[stable(feature = "rust1", since = "1.0.0")]
1068 impl fmt::Debug for Ipv6Addr {
1069 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1070 fmt::Display::fmt(self, fmt)
1074 #[stable(feature = "rust1", since = "1.0.0")]
1075 impl Clone for Ipv6Addr {
1076 fn clone(&self) -> Ipv6Addr { *self }
1079 #[stable(feature = "rust1", since = "1.0.0")]
1080 impl PartialEq for Ipv6Addr {
1081 fn eq(&self, other: &Ipv6Addr) -> bool {
1082 self.inner.s6_addr == other.inner.s6_addr
1086 #[stable(feature = "ip_cmp", since = "1.15.0")]
1087 impl PartialEq<IpAddr> for Ipv6Addr {
1088 fn eq(&self, other: &IpAddr) -> bool {
1090 IpAddr::V4(_) => false,
1091 IpAddr::V6(ref v6) => self == v6,
1096 #[stable(feature = "ip_cmp", since = "1.15.0")]
1097 impl PartialEq<Ipv6Addr> for IpAddr {
1098 fn eq(&self, other: &Ipv6Addr) -> bool {
1100 IpAddr::V4(_) => false,
1101 IpAddr::V6(ref v6) => v6 == other,
1106 #[stable(feature = "rust1", since = "1.0.0")]
1107 impl Eq for Ipv6Addr {}
1109 #[stable(feature = "rust1", since = "1.0.0")]
1110 impl hash::Hash for Ipv6Addr {
1111 fn hash<H: hash::Hasher>(&self, s: &mut H) {
1112 self.inner.s6_addr.hash(s)
1116 #[stable(feature = "rust1", since = "1.0.0")]
1117 impl PartialOrd for Ipv6Addr {
1118 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1119 Some(self.cmp(other))
1123 #[stable(feature = "ip_cmp", since = "1.15.0")]
1124 impl PartialOrd<Ipv6Addr> for IpAddr {
1125 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1127 IpAddr::V4(_) => Some(Ordering::Less),
1128 IpAddr::V6(ref v6) => v6.partial_cmp(other),
1133 #[stable(feature = "ip_cmp", since = "1.15.0")]
1134 impl PartialOrd<IpAddr> for Ipv6Addr {
1135 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1137 IpAddr::V4(_) => Some(Ordering::Greater),
1138 IpAddr::V6(ref v6) => self.partial_cmp(v6),
1143 #[stable(feature = "rust1", since = "1.0.0")]
1144 impl Ord for Ipv6Addr {
1145 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1146 self.segments().cmp(&other.segments())
1150 impl AsInner<c::in6_addr> for Ipv6Addr {
1151 fn as_inner(&self) -> &c::in6_addr { &self.inner }
1153 impl FromInner<c::in6_addr> for Ipv6Addr {
1154 fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1155 Ipv6Addr { inner: addr }
1159 #[unstable(feature = "i128", issue = "35118")]
1160 impl From<Ipv6Addr> for u128 {
1161 fn from(ip: Ipv6Addr) -> u128 {
1162 let ip = ip.segments();
1163 ((ip[0] as u128) << 112) + ((ip[1] as u128) << 96) + ((ip[2] as u128) << 80) +
1164 ((ip[3] as u128) << 64) + ((ip[4] as u128) << 48) + ((ip[5] as u128) << 32) +
1165 ((ip[6] as u128) << 16) + (ip[7] as u128)
1168 #[unstable(feature = "i128", issue = "35118")]
1169 impl From<u128> for Ipv6Addr {
1170 fn from(ip: u128) -> Ipv6Addr {
1172 (ip >> 112) as u16, (ip >> 96) as u16, (ip >> 80) as u16,
1173 (ip >> 64) as u16, (ip >> 48) as u16, (ip >> 32) as u16,
1174 (ip >> 16) as u16, ip as u16,
1179 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
1180 impl From<[u8; 16]> for Ipv6Addr {
1181 fn from(octets: [u8; 16]) -> Ipv6Addr {
1182 let mut inner: c::in6_addr = unsafe { mem::zeroed() };
1183 inner.s6_addr = octets;
1184 Ipv6Addr::from_inner(inner)
1188 #[stable(feature = "ipv6_from_segments", since = "1.15.0")]
1189 impl From<[u16; 8]> for Ipv6Addr {
1190 fn from(segments: [u16; 8]) -> Ipv6Addr {
1191 let [a, b, c, d, e, f, g, h] = segments;
1192 Ipv6Addr::new(a, b, c, d, e, f, g, h)
1197 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1198 impl From<[u8; 16]> for IpAddr {
1199 fn from(octets: [u8; 16]) -> IpAddr {
1200 IpAddr::V6(Ipv6Addr::from(octets))
1204 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1205 impl From<[u16; 8]> for IpAddr {
1206 fn from(segments: [u16; 8]) -> IpAddr {
1207 IpAddr::V6(Ipv6Addr::from(segments))
1211 // Tests for this module
1212 #[cfg(all(test, not(target_os = "emscripten")))]
1215 use net::Ipv6MulticastScope::*;
1216 use net::test::{tsa, sa6, sa4};
1219 fn test_from_str_ipv4() {
1220 assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
1221 assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
1222 assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
1225 let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
1226 assert_eq!(None, none);
1228 let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
1229 assert_eq!(None, none);
1231 let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok();
1232 assert_eq!(None, none);
1233 // no number between dots
1234 let none: Option<Ipv4Addr> = "255.0..1".parse().ok();
1235 assert_eq!(None, none);
1239 fn test_from_str_ipv6() {
1240 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
1241 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
1243 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
1244 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
1246 assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
1247 "2a02:6b8::11:11".parse());
1250 let none: Option<Ipv6Addr> = "::00000".parse().ok();
1251 assert_eq!(None, none);
1253 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
1254 assert_eq!(None, none);
1256 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
1257 assert_eq!(None, none);
1259 let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
1260 assert_eq!(None, none);
1261 // two double colons
1262 let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok();
1263 assert_eq!(None, none);
1267 fn test_from_str_ipv4_in_ipv6() {
1268 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)),
1269 "::192.0.2.33".parse());
1270 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)),
1271 "::FFFF:192.0.2.33".parse());
1272 assert_eq!(Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
1273 "64:ff9b::192.0.2.33".parse());
1274 assert_eq!(Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
1275 "2001:db8:122:c000:2:2100:192.0.2.33".parse());
1278 let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
1279 assert_eq!(None, none);
1280 // not enough groups
1281 let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok();
1282 assert_eq!(None, none);
1284 let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
1285 assert_eq!(None, none);
1289 fn test_from_str_socket_addr() {
1290 assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)),
1291 "77.88.21.11:80".parse());
1292 assert_eq!(Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)),
1293 "77.88.21.11:80".parse());
1294 assert_eq!(Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
1295 "[2a02:6b8:0:1::1]:53".parse());
1296 assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1,
1297 0, 0, 0, 1), 53, 0, 0)),
1298 "[2a02:6b8:0:1::1]:53".parse());
1299 assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
1300 "[::127.0.0.1]:22".parse());
1301 assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0,
1302 0x7F00, 1), 22, 0, 0)),
1303 "[::127.0.0.1]:22".parse());
1306 let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
1307 assert_eq!(None, none);
1309 let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
1310 assert_eq!(None, none);
1311 // wrong brackets around v4
1312 let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
1313 assert_eq!(None, none);
1314 // port out of range
1315 let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
1316 assert_eq!(None, none);
1320 fn ipv6_addr_to_string() {
1321 // ipv4-mapped address
1322 let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
1323 assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
1325 // ipv4-compatible address
1326 let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
1327 assert_eq!(a1.to_string(), "::192.0.2.128");
1329 // v6 address with no zero segments
1330 assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
1333 // reduce a single run of zeros
1334 assert_eq!("ae::ffff:102:304",
1335 Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string());
1337 // don't reduce just a single zero segment
1338 assert_eq!("1:2:3:4:5:6:0:8",
1339 Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
1342 assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
1345 assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
1348 assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
1350 // two runs of zeros, second one is longer
1351 assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
1353 // two runs of zeros, equal length
1354 assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
1359 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
1360 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped());
1361 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
1362 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible());
1367 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
1368 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1369 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1370 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1371 assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1376 fn ip_properties() {
1377 fn check4(octets: &[u8; 4], unspec: bool, loopback: bool,
1378 global: bool, multicast: bool, documentation: bool) {
1379 let ip = IpAddr::V4(Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]));
1380 assert_eq!(ip.is_unspecified(), unspec);
1381 assert_eq!(ip.is_loopback(), loopback);
1382 assert_eq!(ip.is_global(), global);
1383 assert_eq!(ip.is_multicast(), multicast);
1384 assert_eq!(ip.is_documentation(), documentation);
1387 fn check6(str_addr: &str, unspec: bool, loopback: bool,
1388 global: bool, u_doc: bool, mcast: bool) {
1389 let ip = IpAddr::V6(str_addr.parse().unwrap());
1390 assert_eq!(ip.is_unspecified(), unspec);
1391 assert_eq!(ip.is_loopback(), loopback);
1392 assert_eq!(ip.is_global(), global);
1393 assert_eq!(ip.is_documentation(), u_doc);
1394 assert_eq!(ip.is_multicast(), mcast);
1397 // address unspec loopbk global multicast doc
1398 check4(&[0, 0, 0, 0], true, false, false, false, false);
1399 check4(&[0, 0, 0, 1], false, false, true, false, false);
1400 check4(&[0, 1, 0, 0], false, false, true, false, false);
1401 check4(&[10, 9, 8, 7], false, false, false, false, false);
1402 check4(&[127, 1, 2, 3], false, true, false, false, false);
1403 check4(&[172, 31, 254, 253], false, false, false, false, false);
1404 check4(&[169, 254, 253, 242], false, false, false, false, false);
1405 check4(&[192, 0, 2, 183], false, false, false, false, true);
1406 check4(&[192, 1, 2, 183], false, false, true, false, false);
1407 check4(&[192, 168, 254, 253], false, false, false, false, false);
1408 check4(&[198, 51, 100, 0], false, false, false, false, true);
1409 check4(&[203, 0, 113, 0], false, false, false, false, true);
1410 check4(&[203, 2, 113, 0], false, false, true, false, false);
1411 check4(&[224, 0, 0, 0], false, false, true, true, false);
1412 check4(&[239, 255, 255, 255], false, false, true, true, false);
1413 check4(&[255, 255, 255, 255], false, false, false, false, false);
1415 // address unspec loopbk global doc mcast
1416 check6("::", true, false, false, false, false);
1417 check6("::1", false, true, false, false, false);
1418 check6("::0.0.0.2", false, false, true, false, false);
1419 check6("1::", false, false, true, false, false);
1420 check6("fc00::", false, false, false, false, false);
1421 check6("fdff:ffff::", false, false, false, false, false);
1422 check6("fe80:ffff::", false, false, false, false, false);
1423 check6("febf:ffff::", false, false, false, false, false);
1424 check6("fec0::", false, false, false, false, false);
1425 check6("ff01::", false, false, false, false, true);
1426 check6("ff02::", false, false, false, false, true);
1427 check6("ff03::", false, false, false, false, true);
1428 check6("ff04::", false, false, false, false, true);
1429 check6("ff05::", false, false, false, false, true);
1430 check6("ff08::", false, false, false, false, true);
1431 check6("ff0e::", false, false, true, false, true);
1432 check6("2001:db8:85a3::8a2e:370:7334", false, false, false, true, false);
1433 check6("102:304:506:708:90a:b0c:d0e:f10", false, false, true, false, false);
1437 fn ipv4_properties() {
1438 fn check(octets: &[u8; 4], unspec: bool, loopback: bool,
1439 private: bool, link_local: bool, global: bool,
1440 multicast: bool, broadcast: bool, documentation: bool) {
1441 let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
1442 assert_eq!(octets, &ip.octets());
1444 assert_eq!(ip.is_unspecified(), unspec);
1445 assert_eq!(ip.is_loopback(), loopback);
1446 assert_eq!(ip.is_private(), private);
1447 assert_eq!(ip.is_link_local(), link_local);
1448 assert_eq!(ip.is_global(), global);
1449 assert_eq!(ip.is_multicast(), multicast);
1450 assert_eq!(ip.is_broadcast(), broadcast);
1451 assert_eq!(ip.is_documentation(), documentation);
1454 // address unspec loopbk privt linloc global multicast brdcast doc
1455 check(&[0, 0, 0, 0], true, false, false, false, false, false, false, false);
1456 check(&[0, 0, 0, 1], false, false, false, false, true, false, false, false);
1457 check(&[0, 1, 0, 0], false, false, false, false, true, false, false, false);
1458 check(&[10, 9, 8, 7], false, false, true, false, false, false, false, false);
1459 check(&[127, 1, 2, 3], false, true, false, false, false, false, false, false);
1460 check(&[172, 31, 254, 253], false, false, true, false, false, false, false, false);
1461 check(&[169, 254, 253, 242], false, false, false, true, false, false, false, false);
1462 check(&[192, 0, 2, 183], false, false, false, false, false, false, false, true);
1463 check(&[192, 1, 2, 183], false, false, false, false, true, false, false, false);
1464 check(&[192, 168, 254, 253], false, false, true, false, false, false, false, false);
1465 check(&[198, 51, 100, 0], false, false, false, false, false, false, false, true);
1466 check(&[203, 0, 113, 0], false, false, false, false, false, false, false, true);
1467 check(&[203, 2, 113, 0], false, false, false, false, true, false, false, false);
1468 check(&[224, 0, 0, 0], false, false, false, false, true, true, false, false);
1469 check(&[239, 255, 255, 255], false, false, false, false, true, true, false, false);
1470 check(&[255, 255, 255, 255], false, false, false, false, false, false, true, false);
1474 fn ipv6_properties() {
1475 fn check(str_addr: &str, octets: &[u8; 16], unspec: bool, loopback: bool,
1476 unique_local: bool, global: bool,
1477 u_link_local: bool, u_site_local: bool, u_global: bool, u_doc: bool,
1478 m_scope: Option<Ipv6MulticastScope>) {
1479 let ip: Ipv6Addr = str_addr.parse().unwrap();
1480 assert_eq!(str_addr, ip.to_string());
1481 assert_eq!(&ip.octets(), octets);
1482 assert_eq!(Ipv6Addr::from(*octets), ip);
1484 assert_eq!(ip.is_unspecified(), unspec);
1485 assert_eq!(ip.is_loopback(), loopback);
1486 assert_eq!(ip.is_unique_local(), unique_local);
1487 assert_eq!(ip.is_global(), global);
1488 assert_eq!(ip.is_unicast_link_local(), u_link_local);
1489 assert_eq!(ip.is_unicast_site_local(), u_site_local);
1490 assert_eq!(ip.is_unicast_global(), u_global);
1491 assert_eq!(ip.is_documentation(), u_doc);
1492 assert_eq!(ip.multicast_scope(), m_scope);
1493 assert_eq!(ip.is_multicast(), m_scope.is_some());
1496 // unspec loopbk uniqlo global unill unisl uniglo doc mscope
1497 check("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1498 true, false, false, false, false, false, false, false, None);
1499 check("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
1500 false, true, false, false, false, false, false, false, None);
1501 check("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
1502 false, false, false, true, false, false, true, false, None);
1503 check("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1504 false, false, false, true, false, false, true, false, None);
1505 check("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1506 false, false, true, false, false, false, false, false, None);
1507 check("fdff:ffff::", &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1508 false, false, true, false, false, false, false, false, None);
1509 check("fe80:ffff::", &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1510 false, false, false, false, true, false, false, false, None);
1511 check("febf:ffff::", &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1512 false, false, false, false, true, false, false, false, None);
1513 check("fec0::", &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1514 false, false, false, false, false, true, false, false, None);
1515 check("ff01::", &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1516 false, false, false, false, false, false, false, false, Some(InterfaceLocal));
1517 check("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1518 false, false, false, false, false, false, false, false, Some(LinkLocal));
1519 check("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1520 false, false, false, false, false, false, false, false, Some(RealmLocal));
1521 check("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1522 false, false, false, false, false, false, false, false, Some(AdminLocal));
1523 check("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1524 false, false, false, false, false, false, false, false, Some(SiteLocal));
1525 check("ff08::", &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1526 false, false, false, false, false, false, false, false, Some(OrganizationLocal));
1527 check("ff0e::", &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1528 false, false, false, true, false, false, false, false, Some(Global));
1529 check("2001:db8:85a3::8a2e:370:7334",
1530 &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
1531 false, false, false, false, false, false, false, true, None);
1532 check("102:304:506:708:90a:b0c:d0e:f10",
1533 &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1534 false, false, false, true, false, false, true, false, None);
1538 fn to_socket_addr_socketaddr() {
1539 let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345);
1540 assert_eq!(Ok(vec![a]), tsa(a));
1544 fn test_ipv4_to_int() {
1545 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
1546 assert_eq!(u32::from(a), 0x11223344);
1550 fn test_int_to_ipv4() {
1551 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
1552 assert_eq!(Ipv4Addr::from(0x11223344), a);
1556 fn test_ipv6_to_int() {
1557 let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
1558 assert_eq!(u128::from(a), 0x112233445566778899aabbccddeeff11u128);
1562 fn test_int_to_ipv6() {
1563 let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
1564 assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
1568 fn ipv4_from_octets() {
1569 assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
1573 fn ipv6_from_segments() {
1574 let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1575 0x8899, 0xaabb, 0xccdd, 0xeeff]);
1576 let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677,
1577 0x8899, 0xaabb, 0xccdd, 0xeeff);
1578 assert_eq!(new, from_u16s);
1582 fn ipv6_from_octets() {
1583 let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1584 0x8899, 0xaabb, 0xccdd, 0xeeff]);
1585 let from_u8s = Ipv6Addr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
1586 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
1587 assert_eq!(from_u16s, from_u8s);
1592 let v41 = Ipv4Addr::new(100, 64, 3, 3);
1593 let v42 = Ipv4Addr::new(192, 0, 2, 2);
1594 let v61 = "2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap();
1595 let v62 = "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap();
1599 assert_eq!(v41, IpAddr::V4(v41));
1600 assert_eq!(v61, IpAddr::V6(v61));
1601 assert!(v41 != IpAddr::V4(v42));
1602 assert!(v61 != IpAddr::V6(v62));
1604 assert!(v41 < IpAddr::V4(v42));
1605 assert!(v61 < IpAddr::V6(v62));
1606 assert!(IpAddr::V4(v41) < v42);
1607 assert!(IpAddr::V6(v61) < v62);
1609 assert!(v41 < IpAddr::V6(v61));
1610 assert!(IpAddr::V4(v41) < v61);
1615 let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
1616 assert!(ip.is_ipv4());
1617 assert!(!ip.is_ipv6());
1622 let ip = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
1623 assert!(!ip.is_ipv4());
1624 assert!(ip.is_ipv6());