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])
660 /// Creates a new IPv6 address from eight 16-bit segments.
662 /// The result will represent the IP address a:b:c:d:e:f:g:h.
667 /// use std::net::Ipv6Addr;
669 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
671 #[stable(feature = "rust1", since = "1.0.0")]
672 pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
673 h: u16) -> Ipv6Addr {
674 let mut addr: c::in6_addr = unsafe { mem::zeroed() };
675 addr.s6_addr = [(a >> 8) as u8, a as u8,
676 (b >> 8) as u8, b as u8,
677 (c >> 8) as u8, c as u8,
678 (d >> 8) as u8, d as u8,
679 (e >> 8) as u8, e as u8,
680 (f >> 8) as u8, f as u8,
681 (g >> 8) as u8, g as u8,
682 (h >> 8) as u8, h as u8];
683 Ipv6Addr { inner: addr }
686 /// Returns the eight 16-bit segments that make up this address.
691 /// use std::net::Ipv6Addr;
693 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
694 /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
696 #[stable(feature = "rust1", since = "1.0.0")]
697 pub fn segments(&self) -> [u16; 8] {
698 let arr = &self.inner.s6_addr;
700 (arr[0] as u16) << 8 | (arr[1] as u16),
701 (arr[2] as u16) << 8 | (arr[3] as u16),
702 (arr[4] as u16) << 8 | (arr[5] as u16),
703 (arr[6] as u16) << 8 | (arr[7] as u16),
704 (arr[8] as u16) << 8 | (arr[9] as u16),
705 (arr[10] as u16) << 8 | (arr[11] as u16),
706 (arr[12] as u16) << 8 | (arr[13] as u16),
707 (arr[14] as u16) << 8 | (arr[15] as u16),
711 /// Returns true for the special 'unspecified' address (::).
713 /// This property is defined in [RFC 4291].
715 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
720 /// use std::net::Ipv6Addr;
722 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
723 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
725 #[stable(since = "1.7.0", feature = "ip_17")]
726 pub fn is_unspecified(&self) -> bool {
727 self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
730 /// Returns true if this is a loopback address (::1).
732 /// This property is defined in [RFC 4291].
734 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
739 /// use std::net::Ipv6Addr;
741 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
742 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
744 #[stable(since = "1.7.0", feature = "ip_17")]
745 pub fn is_loopback(&self) -> bool {
746 self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
749 /// Returns true if the address appears to be globally routable.
751 /// The following return false:
753 /// - the loopback address
754 /// - link-local, site-local, and unique local unicast addresses
755 /// - interface-, link-, realm-, admin- and site-local multicast addresses
762 /// use std::net::Ipv6Addr;
765 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
766 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
767 /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
770 pub fn is_global(&self) -> bool {
771 match self.multicast_scope() {
772 Some(Ipv6MulticastScope::Global) => true,
773 None => self.is_unicast_global(),
778 /// Returns true if this is a unique local address (fc00::/7).
780 /// This property is defined in [RFC 4193].
782 /// [RFC 4193]: https://tools.ietf.org/html/rfc4193
789 /// use std::net::Ipv6Addr;
792 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(),
794 /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
797 pub fn is_unique_local(&self) -> bool {
798 (self.segments()[0] & 0xfe00) == 0xfc00
801 /// Returns true if the address is unicast and link-local (fe80::/10).
803 /// This property is defined in [RFC 4291].
805 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
812 /// use std::net::Ipv6Addr;
815 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_link_local(),
817 /// assert_eq!(Ipv6Addr::new(0xfe8a, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
820 pub fn is_unicast_link_local(&self) -> bool {
821 (self.segments()[0] & 0xffc0) == 0xfe80
824 /// Returns true if this is a deprecated unicast site-local address
832 /// use std::net::Ipv6Addr;
835 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
837 /// assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
840 pub fn is_unicast_site_local(&self) -> bool {
841 (self.segments()[0] & 0xffc0) == 0xfec0
844 /// Returns true if this is an address reserved for documentation
847 /// This property is defined in [RFC 3849].
849 /// [RFC 3849]: https://tools.ietf.org/html/rfc3849
856 /// use std::net::Ipv6Addr;
859 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(),
861 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
864 pub fn is_documentation(&self) -> bool {
865 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
868 /// Returns true if the address is a globally routable unicast address.
870 /// The following return false:
872 /// - the loopback address
873 /// - the link-local addresses
874 /// - the (deprecated) site-local addresses
875 /// - unique local addresses
876 /// - the unspecified address
877 /// - the address range reserved for documentation
884 /// use std::net::Ipv6Addr;
887 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
888 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(),
892 pub fn is_unicast_global(&self) -> bool {
894 && !self.is_loopback() && !self.is_unicast_link_local()
895 && !self.is_unicast_site_local() && !self.is_unique_local()
896 && !self.is_unspecified() && !self.is_documentation()
899 /// Returns the address's multicast scope if the address is multicast.
906 /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
909 /// assert_eq!(Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
910 /// Some(Ipv6MulticastScope::Global));
911 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
914 pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
915 if self.is_multicast() {
916 match self.segments()[0] & 0x000f {
917 1 => Some(Ipv6MulticastScope::InterfaceLocal),
918 2 => Some(Ipv6MulticastScope::LinkLocal),
919 3 => Some(Ipv6MulticastScope::RealmLocal),
920 4 => Some(Ipv6MulticastScope::AdminLocal),
921 5 => Some(Ipv6MulticastScope::SiteLocal),
922 8 => Some(Ipv6MulticastScope::OrganizationLocal),
923 14 => Some(Ipv6MulticastScope::Global),
931 /// Returns true if this is a multicast address (ff00::/8).
933 /// This property is defined by [RFC 4291].
935 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
939 /// use std::net::Ipv6Addr;
941 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
942 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
944 #[stable(since = "1.7.0", feature = "ip_17")]
945 pub fn is_multicast(&self) -> bool {
946 (self.segments()[0] & 0xff00) == 0xff00
949 /// Converts this address to an IPv4 address. Returns None if this address is
950 /// neither IPv4-compatible or IPv4-mapped.
952 /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
955 /// use std::net::{Ipv4Addr, Ipv6Addr};
957 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
958 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
959 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
960 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
961 /// Some(Ipv4Addr::new(0, 0, 0, 1)));
963 #[stable(feature = "rust1", since = "1.0.0")]
964 pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
965 match self.segments() {
966 [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
967 Some(Ipv4Addr::new((g >> 8) as u8, g as u8,
968 (h >> 8) as u8, h as u8))
974 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
977 /// use std::net::Ipv6Addr;
979 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
980 /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
982 #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
983 pub fn octets(&self) -> [u8; 16] {
988 #[stable(feature = "rust1", since = "1.0.0")]
989 impl fmt::Display for Ipv6Addr {
990 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
991 match self.segments() {
992 // We need special cases for :: and ::1, otherwise they're formatted
994 [0, 0, 0, 0, 0, 0, 0, 0] => write!(fmt, "::"),
995 [0, 0, 0, 0, 0, 0, 0, 1] => write!(fmt, "::1"),
996 // Ipv4 Compatible address
997 [0, 0, 0, 0, 0, 0, g, h] => {
998 write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
999 (h >> 8) as u8, h as u8)
1001 // Ipv4-Mapped address
1002 [0, 0, 0, 0, 0, 0xffff, g, h] => {
1003 write!(fmt, "::ffff:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
1004 (h >> 8) as u8, h as u8)
1007 fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) {
1008 let mut longest_span_len = 0;
1009 let mut longest_span_at = 0;
1010 let mut cur_span_len = 0;
1011 let mut cur_span_at = 0;
1014 if segments[i] == 0 {
1015 if cur_span_len == 0 {
1021 if cur_span_len > longest_span_len {
1022 longest_span_len = cur_span_len;
1023 longest_span_at = cur_span_at;
1031 (longest_span_at, longest_span_len)
1034 let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
1037 fn fmt_subslice(segments: &[u16], fmt: &mut fmt::Formatter) -> fmt::Result {
1038 if !segments.is_empty() {
1039 write!(fmt, "{:x}", segments[0])?;
1040 for &seg in &segments[1..] {
1041 write!(fmt, ":{:x}", seg)?;
1047 fmt_subslice(&self.segments()[..zeros_at], fmt)?;
1048 fmt.write_str("::")?;
1049 fmt_subslice(&self.segments()[zeros_at + zeros_len..], fmt)
1051 let &[a, b, c, d, e, f, g, h] = &self.segments();
1052 write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
1053 a, b, c, d, e, f, g, h)
1060 #[stable(feature = "rust1", since = "1.0.0")]
1061 impl fmt::Debug for Ipv6Addr {
1062 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1063 fmt::Display::fmt(self, fmt)
1067 #[stable(feature = "rust1", since = "1.0.0")]
1068 impl Clone for Ipv6Addr {
1069 fn clone(&self) -> Ipv6Addr { *self }
1072 #[stable(feature = "rust1", since = "1.0.0")]
1073 impl PartialEq for Ipv6Addr {
1074 fn eq(&self, other: &Ipv6Addr) -> bool {
1075 self.inner.s6_addr == other.inner.s6_addr
1079 #[stable(feature = "ip_cmp", since = "1.15.0")]
1080 impl PartialEq<IpAddr> for Ipv6Addr {
1081 fn eq(&self, other: &IpAddr) -> bool {
1083 IpAddr::V4(_) => false,
1084 IpAddr::V6(ref v6) => self == v6,
1089 #[stable(feature = "ip_cmp", since = "1.15.0")]
1090 impl PartialEq<Ipv6Addr> for IpAddr {
1091 fn eq(&self, other: &Ipv6Addr) -> bool {
1093 IpAddr::V4(_) => false,
1094 IpAddr::V6(ref v6) => v6 == other,
1099 #[stable(feature = "rust1", since = "1.0.0")]
1100 impl Eq for Ipv6Addr {}
1102 #[stable(feature = "rust1", since = "1.0.0")]
1103 impl hash::Hash for Ipv6Addr {
1104 fn hash<H: hash::Hasher>(&self, s: &mut H) {
1105 self.inner.s6_addr.hash(s)
1109 #[stable(feature = "rust1", since = "1.0.0")]
1110 impl PartialOrd for Ipv6Addr {
1111 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1112 Some(self.cmp(other))
1116 #[stable(feature = "ip_cmp", since = "1.15.0")]
1117 impl PartialOrd<Ipv6Addr> for IpAddr {
1118 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1120 IpAddr::V4(_) => Some(Ordering::Less),
1121 IpAddr::V6(ref v6) => v6.partial_cmp(other),
1126 #[stable(feature = "ip_cmp", since = "1.15.0")]
1127 impl PartialOrd<IpAddr> for Ipv6Addr {
1128 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1130 IpAddr::V4(_) => Some(Ordering::Greater),
1131 IpAddr::V6(ref v6) => self.partial_cmp(v6),
1136 #[stable(feature = "rust1", since = "1.0.0")]
1137 impl Ord for Ipv6Addr {
1138 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1139 self.segments().cmp(&other.segments())
1143 impl AsInner<c::in6_addr> for Ipv6Addr {
1144 fn as_inner(&self) -> &c::in6_addr { &self.inner }
1146 impl FromInner<c::in6_addr> for Ipv6Addr {
1147 fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1148 Ipv6Addr { inner: addr }
1152 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
1153 impl From<[u8; 16]> for Ipv6Addr {
1154 fn from(octets: [u8; 16]) -> Ipv6Addr {
1155 let mut inner: c::in6_addr = unsafe { mem::zeroed() };
1156 inner.s6_addr = octets;
1157 Ipv6Addr::from_inner(inner)
1161 #[stable(feature = "ipv6_from_segments", since = "1.15.0")]
1162 impl From<[u16; 8]> for Ipv6Addr {
1163 fn from(segments: [u16; 8]) -> Ipv6Addr {
1164 let [a, b, c, d, e, f, g, h] = segments;
1165 Ipv6Addr::new(a, b, c, d, e, f, g, h)
1169 // Tests for this module
1170 #[cfg(all(test, not(target_os = "emscripten")))]
1173 use net::Ipv6MulticastScope::*;
1174 use net::test::{tsa, sa6, sa4};
1177 fn test_from_str_ipv4() {
1178 assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
1179 assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
1180 assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
1183 let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
1184 assert_eq!(None, none);
1186 let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
1187 assert_eq!(None, none);
1189 let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok();
1190 assert_eq!(None, none);
1191 // no number between dots
1192 let none: Option<Ipv4Addr> = "255.0..1".parse().ok();
1193 assert_eq!(None, none);
1197 fn test_from_str_ipv6() {
1198 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
1199 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
1201 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
1202 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
1204 assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
1205 "2a02:6b8::11:11".parse());
1208 let none: Option<Ipv6Addr> = "::00000".parse().ok();
1209 assert_eq!(None, none);
1211 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
1212 assert_eq!(None, none);
1214 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
1215 assert_eq!(None, none);
1217 let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
1218 assert_eq!(None, none);
1219 // two double colons
1220 let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok();
1221 assert_eq!(None, none);
1225 fn test_from_str_ipv4_in_ipv6() {
1226 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)),
1227 "::192.0.2.33".parse());
1228 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)),
1229 "::FFFF:192.0.2.33".parse());
1230 assert_eq!(Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
1231 "64:ff9b::192.0.2.33".parse());
1232 assert_eq!(Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
1233 "2001:db8:122:c000:2:2100:192.0.2.33".parse());
1236 let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
1237 assert_eq!(None, none);
1238 // not enough groups
1239 let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok();
1240 assert_eq!(None, none);
1242 let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
1243 assert_eq!(None, none);
1247 fn test_from_str_socket_addr() {
1248 assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)),
1249 "77.88.21.11:80".parse());
1250 assert_eq!(Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)),
1251 "77.88.21.11:80".parse());
1252 assert_eq!(Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
1253 "[2a02:6b8:0:1::1]:53".parse());
1254 assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1,
1255 0, 0, 0, 1), 53, 0, 0)),
1256 "[2a02:6b8:0:1::1]:53".parse());
1257 assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
1258 "[::127.0.0.1]:22".parse());
1259 assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0,
1260 0x7F00, 1), 22, 0, 0)),
1261 "[::127.0.0.1]:22".parse());
1264 let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
1265 assert_eq!(None, none);
1267 let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
1268 assert_eq!(None, none);
1269 // wrong brackets around v4
1270 let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
1271 assert_eq!(None, none);
1272 // port out of range
1273 let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
1274 assert_eq!(None, none);
1278 fn ipv6_addr_to_string() {
1279 // ipv4-mapped address
1280 let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
1281 assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
1283 // ipv4-compatible address
1284 let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
1285 assert_eq!(a1.to_string(), "::192.0.2.128");
1287 // v6 address with no zero segments
1288 assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
1291 // reduce a single run of zeros
1292 assert_eq!("ae::ffff:102:304",
1293 Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string());
1295 // don't reduce just a single zero segment
1296 assert_eq!("1:2:3:4:5:6:0:8",
1297 Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
1300 assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
1303 assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
1306 assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
1308 // two runs of zeros, second one is longer
1309 assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
1311 // two runs of zeros, equal length
1312 assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
1317 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
1318 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped());
1319 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
1320 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible());
1325 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
1326 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1327 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1328 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
1329 assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
1334 fn ip_properties() {
1335 fn check4(octets: &[u8; 4], unspec: bool, loopback: bool,
1336 global: bool, multicast: bool, documentation: bool) {
1337 let ip = IpAddr::V4(Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]));
1338 assert_eq!(ip.is_unspecified(), unspec);
1339 assert_eq!(ip.is_loopback(), loopback);
1340 assert_eq!(ip.is_global(), global);
1341 assert_eq!(ip.is_multicast(), multicast);
1342 assert_eq!(ip.is_documentation(), documentation);
1345 fn check6(str_addr: &str, unspec: bool, loopback: bool,
1346 global: bool, u_doc: bool, mcast: bool) {
1347 let ip = IpAddr::V6(str_addr.parse().unwrap());
1348 assert_eq!(ip.is_unspecified(), unspec);
1349 assert_eq!(ip.is_loopback(), loopback);
1350 assert_eq!(ip.is_global(), global);
1351 assert_eq!(ip.is_documentation(), u_doc);
1352 assert_eq!(ip.is_multicast(), mcast);
1355 // address unspec loopbk global multicast doc
1356 check4(&[0, 0, 0, 0], true, false, false, false, false);
1357 check4(&[0, 0, 0, 1], false, false, true, false, false);
1358 check4(&[0, 1, 0, 0], false, false, true, false, false);
1359 check4(&[10, 9, 8, 7], false, false, false, false, false);
1360 check4(&[127, 1, 2, 3], false, true, false, false, false);
1361 check4(&[172, 31, 254, 253], false, false, false, false, false);
1362 check4(&[169, 254, 253, 242], false, false, false, false, false);
1363 check4(&[192, 0, 2, 183], false, false, false, false, true);
1364 check4(&[192, 1, 2, 183], false, false, true, false, false);
1365 check4(&[192, 168, 254, 253], false, false, false, false, false);
1366 check4(&[198, 51, 100, 0], false, false, false, false, true);
1367 check4(&[203, 0, 113, 0], false, false, false, false, true);
1368 check4(&[203, 2, 113, 0], false, false, true, false, false);
1369 check4(&[224, 0, 0, 0], false, false, true, true, false);
1370 check4(&[239, 255, 255, 255], false, false, true, true, false);
1371 check4(&[255, 255, 255, 255], false, false, false, false, false);
1373 // address unspec loopbk global doc mcast
1374 check6("::", true, false, false, false, false);
1375 check6("::1", false, true, false, false, false);
1376 check6("::0.0.0.2", false, false, true, false, false);
1377 check6("1::", false, false, true, false, false);
1378 check6("fc00::", false, false, false, false, false);
1379 check6("fdff:ffff::", false, false, false, false, false);
1380 check6("fe80:ffff::", false, false, false, false, false);
1381 check6("febf:ffff::", false, false, false, false, false);
1382 check6("fec0::", false, false, false, false, false);
1383 check6("ff01::", false, false, false, false, true);
1384 check6("ff02::", false, false, false, false, true);
1385 check6("ff03::", false, false, false, false, true);
1386 check6("ff04::", false, false, false, false, true);
1387 check6("ff05::", false, false, false, false, true);
1388 check6("ff08::", false, false, false, false, true);
1389 check6("ff0e::", false, false, true, false, true);
1390 check6("2001:db8:85a3::8a2e:370:7334", false, false, false, true, false);
1391 check6("102:304:506:708:90a:b0c:d0e:f10", false, false, true, false, false);
1395 fn ipv4_properties() {
1396 fn check(octets: &[u8; 4], unspec: bool, loopback: bool,
1397 private: bool, link_local: bool, global: bool,
1398 multicast: bool, broadcast: bool, documentation: bool) {
1399 let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
1400 assert_eq!(octets, &ip.octets());
1402 assert_eq!(ip.is_unspecified(), unspec);
1403 assert_eq!(ip.is_loopback(), loopback);
1404 assert_eq!(ip.is_private(), private);
1405 assert_eq!(ip.is_link_local(), link_local);
1406 assert_eq!(ip.is_global(), global);
1407 assert_eq!(ip.is_multicast(), multicast);
1408 assert_eq!(ip.is_broadcast(), broadcast);
1409 assert_eq!(ip.is_documentation(), documentation);
1412 // address unspec loopbk privt linloc global multicast brdcast doc
1413 check(&[0, 0, 0, 0], true, false, false, false, false, false, false, false);
1414 check(&[0, 0, 0, 1], false, false, false, false, true, false, false, false);
1415 check(&[0, 1, 0, 0], false, false, false, false, true, false, false, false);
1416 check(&[10, 9, 8, 7], false, false, true, false, false, false, false, false);
1417 check(&[127, 1, 2, 3], false, true, false, false, false, false, false, false);
1418 check(&[172, 31, 254, 253], false, false, true, false, false, false, false, false);
1419 check(&[169, 254, 253, 242], false, false, false, true, false, false, false, false);
1420 check(&[192, 0, 2, 183], false, false, false, false, false, false, false, true);
1421 check(&[192, 1, 2, 183], false, false, false, false, true, false, false, false);
1422 check(&[192, 168, 254, 253], false, false, true, false, false, false, false, false);
1423 check(&[198, 51, 100, 0], false, false, false, false, false, false, false, true);
1424 check(&[203, 0, 113, 0], false, false, false, false, false, false, false, true);
1425 check(&[203, 2, 113, 0], false, false, false, false, true, false, false, false);
1426 check(&[224, 0, 0, 0], false, false, false, false, true, true, false, false);
1427 check(&[239, 255, 255, 255], false, false, false, false, true, true, false, false);
1428 check(&[255, 255, 255, 255], false, false, false, false, false, false, true, false);
1432 fn ipv6_properties() {
1433 fn check(str_addr: &str, octets: &[u8; 16], unspec: bool, loopback: bool,
1434 unique_local: bool, global: bool,
1435 u_link_local: bool, u_site_local: bool, u_global: bool, u_doc: bool,
1436 m_scope: Option<Ipv6MulticastScope>) {
1437 let ip: Ipv6Addr = str_addr.parse().unwrap();
1438 assert_eq!(str_addr, ip.to_string());
1439 assert_eq!(&ip.octets(), octets);
1440 assert_eq!(Ipv6Addr::from(*octets), ip);
1442 assert_eq!(ip.is_unspecified(), unspec);
1443 assert_eq!(ip.is_loopback(), loopback);
1444 assert_eq!(ip.is_unique_local(), unique_local);
1445 assert_eq!(ip.is_global(), global);
1446 assert_eq!(ip.is_unicast_link_local(), u_link_local);
1447 assert_eq!(ip.is_unicast_site_local(), u_site_local);
1448 assert_eq!(ip.is_unicast_global(), u_global);
1449 assert_eq!(ip.is_documentation(), u_doc);
1450 assert_eq!(ip.multicast_scope(), m_scope);
1451 assert_eq!(ip.is_multicast(), m_scope.is_some());
1454 // unspec loopbk uniqlo global unill unisl uniglo doc mscope
1455 check("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1456 true, false, false, false, false, false, false, false, None);
1457 check("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
1458 false, true, false, false, false, false, false, false, None);
1459 check("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
1460 false, false, false, true, false, false, true, false, None);
1461 check("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1462 false, false, false, true, false, false, true, false, None);
1463 check("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1464 false, false, true, false, false, false, false, false, None);
1465 check("fdff:ffff::", &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1466 false, false, true, false, false, false, false, false, None);
1467 check("fe80:ffff::", &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1468 false, false, false, false, true, false, false, false, None);
1469 check("febf:ffff::", &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1470 false, false, false, false, true, false, false, false, None);
1471 check("fec0::", &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1472 false, false, false, false, false, true, false, false, None);
1473 check("ff01::", &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1474 false, false, false, false, false, false, false, false, Some(InterfaceLocal));
1475 check("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1476 false, false, false, false, false, false, false, false, Some(LinkLocal));
1477 check("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1478 false, false, false, false, false, false, false, false, Some(RealmLocal));
1479 check("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1480 false, false, false, false, false, false, false, false, Some(AdminLocal));
1481 check("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1482 false, false, false, false, false, false, false, false, Some(SiteLocal));
1483 check("ff08::", &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1484 false, false, false, false, false, false, false, false, Some(OrganizationLocal));
1485 check("ff0e::", &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
1486 false, false, false, true, false, false, false, false, Some(Global));
1487 check("2001:db8:85a3::8a2e:370:7334",
1488 &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
1489 false, false, false, false, false, false, false, true, None);
1490 check("102:304:506:708:90a:b0c:d0e:f10",
1491 &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1492 false, false, false, true, false, false, true, false, None);
1496 fn to_socket_addr_socketaddr() {
1497 let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345);
1498 assert_eq!(Ok(vec![a]), tsa(a));
1502 fn test_ipv4_to_int() {
1503 let a = Ipv4Addr::new(127, 0, 0, 1);
1504 assert_eq!(u32::from(a), 2130706433);
1508 fn test_int_to_ipv4() {
1509 let a = Ipv4Addr::new(127, 0, 0, 1);
1510 assert_eq!(Ipv4Addr::from(2130706433), a);
1514 fn ipv4_from_octets() {
1515 assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
1519 fn ipv6_from_segments() {
1520 let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1521 0x8899, 0xaabb, 0xccdd, 0xeeff]);
1522 let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677,
1523 0x8899, 0xaabb, 0xccdd, 0xeeff);
1524 assert_eq!(new, from_u16s);
1528 fn ipv6_from_octets() {
1529 let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
1530 0x8899, 0xaabb, 0xccdd, 0xeeff]);
1531 let from_u8s = Ipv6Addr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
1532 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
1533 assert_eq!(from_u16s, from_u8s);
1538 let v41 = Ipv4Addr::new(100, 64, 3, 3);
1539 let v42 = Ipv4Addr::new(192, 0, 2, 2);
1540 let v61 = "2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap();
1541 let v62 = "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap();
1545 assert_eq!(v41, IpAddr::V4(v41));
1546 assert_eq!(v61, IpAddr::V6(v61));
1547 assert!(v41 != IpAddr::V4(v42));
1548 assert!(v61 != IpAddr::V6(v62));
1550 assert!(v41 < IpAddr::V4(v42));
1551 assert!(v61 < IpAddr::V6(v62));
1552 assert!(IpAddr::V4(v41) < v42);
1553 assert!(IpAddr::V6(v61) < v62);
1555 assert!(v41 < IpAddr::V6(v61));
1556 assert!(IpAddr::V4(v41) < v61);
1561 let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
1562 assert!(ip.is_ipv4());
1563 assert!(!ip.is_ipv6());
1568 let ip = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
1569 assert!(!ip.is_ipv4());
1570 assert!(ip.is_ipv6());