1 // Tests for this module
2 #[cfg(all(test, not(target_os = "emscripten")))]
5 use crate::cmp::Ordering;
6 use crate::fmt::{self, Write};
7 use crate::mem::transmute;
8 use crate::sys::net::netc as c;
9 use crate::sys_common::{FromInner, IntoInner};
11 use super::display_buffer::DisplayBuffer;
13 /// An IP address, either IPv4 or IPv6.
15 /// This enum can contain either an [`Ipv4Addr`] or an [`Ipv6Addr`], see their
16 /// respective documentation for more details.
21 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
23 /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
24 /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
26 /// assert_eq!("127.0.0.1".parse(), Ok(localhost_v4));
27 /// assert_eq!("::1".parse(), Ok(localhost_v6));
29 /// assert_eq!(localhost_v4.is_ipv6(), false);
30 /// assert_eq!(localhost_v4.is_ipv4(), true);
32 #[stable(feature = "ip_addr", since = "1.7.0")]
33 #[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
36 #[stable(feature = "ip_addr", since = "1.7.0")]
37 V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
39 #[stable(feature = "ip_addr", since = "1.7.0")]
40 V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
45 /// IPv4 addresses are defined as 32-bit integers in [IETF RFC 791].
46 /// They are usually represented as four octets.
48 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
50 /// [IETF RFC 791]: https://tools.ietf.org/html/rfc791
52 /// # Textual representation
54 /// `Ipv4Addr` provides a [`FromStr`] implementation. The four octets are in decimal
55 /// notation, divided by `.` (this is called "dot-decimal notation").
56 /// Notably, octal numbers (which are indicated with a leading `0`) and hexadecimal numbers (which
57 /// are indicated with a leading `0x`) are not allowed per [IETF RFC 6943].
59 /// [IETF RFC 6943]: https://tools.ietf.org/html/rfc6943#section-3.1.1
60 /// [`FromStr`]: crate::str::FromStr
65 /// use std::net::Ipv4Addr;
67 /// let localhost = Ipv4Addr::new(127, 0, 0, 1);
68 /// assert_eq!("127.0.0.1".parse(), Ok(localhost));
69 /// assert_eq!(localhost.is_loopback(), true);
70 /// assert!("012.004.002.000".parse::<Ipv4Addr>().is_err()); // all octets are in octal
71 /// assert!("0000000.0.0.0".parse::<Ipv4Addr>().is_err()); // first octet is a zero in octal
72 /// assert!("0xcb.0x0.0x71.0x00".parse::<Ipv4Addr>().is_err()); // all octets are in hex
74 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
75 #[stable(feature = "rust1", since = "1.0.0")]
82 /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291].
83 /// They are usually represented as eight 16-bit segments.
85 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
87 /// # Embedding IPv4 Addresses
89 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
91 /// To assist in the transition from IPv4 to IPv6 two types of IPv6 addresses that embed an IPv4 address were defined:
92 /// IPv4-compatible and IPv4-mapped addresses. Of these IPv4-compatible addresses have been officially deprecated.
94 /// Both types of addresses are not assigned any special meaning by this implementation,
95 /// other than what the relevant standards prescribe. This means that an address like `::ffff:127.0.0.1`,
96 /// while representing an IPv4 loopback address, is not itself an IPv6 loopback address; only `::1` is.
97 /// To handle these so called "IPv4-in-IPv6" addresses, they have to first be converted to their canonical IPv4 address.
99 /// ### IPv4-Compatible IPv6 Addresses
101 /// IPv4-compatible IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.1], and have been officially deprecated.
102 /// The RFC describes the format of an "IPv4-Compatible IPv6 address" as follows:
105 /// | 80 bits | 16 | 32 bits |
106 /// +--------------------------------------+--------------------------+
107 /// |0000..............................0000|0000| IPv4 address |
108 /// +--------------------------------------+----+---------------------+
110 /// So `::a.b.c.d` would be an IPv4-compatible IPv6 address representing the IPv4 address `a.b.c.d`.
112 /// To convert from an IPv4 address to an IPv4-compatible IPv6 address, use [`Ipv4Addr::to_ipv6_compatible`].
113 /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-compatible IPv6 address to the canonical IPv4 address.
115 /// [IETF RFC 4291 Section 2.5.5.1]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.1
117 /// ### IPv4-Mapped IPv6 Addresses
119 /// IPv4-mapped IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.2].
120 /// The RFC describes the format of an "IPv4-Mapped IPv6 address" as follows:
123 /// | 80 bits | 16 | 32 bits |
124 /// +--------------------------------------+--------------------------+
125 /// |0000..............................0000|FFFF| IPv4 address |
126 /// +--------------------------------------+----+---------------------+
128 /// So `::ffff:a.b.c.d` would be an IPv4-mapped IPv6 address representing the IPv4 address `a.b.c.d`.
130 /// To convert from an IPv4 address to an IPv4-mapped IPv6 address, use [`Ipv4Addr::to_ipv6_mapped`].
131 /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-mapped IPv6 address to the canonical IPv4 address.
132 /// Note that this will also convert the IPv6 loopback address `::1` to `0.0.0.1`. Use
133 /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this.
135 /// [IETF RFC 4291 Section 2.5.5.2]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2
137 /// # Textual representation
139 /// `Ipv6Addr` provides a [`FromStr`] implementation. There are many ways to represent
140 /// an IPv6 address in text, but in general, each segments is written in hexadecimal
141 /// notation, and segments are separated by `:`. For more information, see
144 /// [`FromStr`]: crate::str::FromStr
145 /// [IETF RFC 5952]: https://tools.ietf.org/html/rfc5952
150 /// use std::net::Ipv6Addr;
152 /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
153 /// assert_eq!("::1".parse(), Ok(localhost));
154 /// assert_eq!(localhost.is_loopback(), true);
156 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
157 #[stable(feature = "rust1", since = "1.0.0")]
158 pub struct Ipv6Addr {
162 /// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2].
164 /// # Stability Guarantees
166 /// Not all possible values for a multicast scope have been assigned.
167 /// Future RFCs may introduce new scopes, which will be added as variants to this enum;
168 /// because of this the enum is marked as `#[non_exhaustive]`.
174 /// use std::net::Ipv6Addr;
175 /// use std::net::Ipv6MulticastScope::*;
177 /// // An IPv6 multicast address with global scope (`ff0e::`).
178 /// let address = Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0);
180 /// // Will print "Global scope".
181 /// match address.multicast_scope() {
182 /// Some(InterfaceLocal) => println!("Interface-Local scope"),
183 /// Some(LinkLocal) => println!("Link-Local scope"),
184 /// Some(RealmLocal) => println!("Realm-Local scope"),
185 /// Some(AdminLocal) => println!("Admin-Local scope"),
186 /// Some(SiteLocal) => println!("Site-Local scope"),
187 /// Some(OrganizationLocal) => println!("Organization-Local scope"),
188 /// Some(Global) => println!("Global scope"),
189 /// Some(_) => println!("Unknown scope"),
190 /// None => println!("Not a multicast address!")
195 /// [IPv6 multicast address]: Ipv6Addr
196 /// [IETF RFC 7346 section 2]: https://tools.ietf.org/html/rfc7346#section-2
197 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
198 #[unstable(feature = "ip", issue = "27709")]
200 pub enum Ipv6MulticastScope {
201 /// Interface-Local scope.
203 /// Link-Local scope.
205 /// Realm-Local scope.
207 /// Admin-Local scope.
209 /// Site-Local scope.
211 /// Organization-Local scope.
218 /// Returns [`true`] for the special 'unspecified' address.
220 /// See the documentation for [`Ipv4Addr::is_unspecified()`] and
221 /// [`Ipv6Addr::is_unspecified()`] for more details.
226 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
228 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
229 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
231 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
232 #[stable(feature = "ip_shared", since = "1.12.0")]
235 pub const fn is_unspecified(&self) -> bool {
237 IpAddr::V4(ip) => ip.is_unspecified(),
238 IpAddr::V6(ip) => ip.is_unspecified(),
242 /// Returns [`true`] if this is a loopback address.
244 /// See the documentation for [`Ipv4Addr::is_loopback()`] and
245 /// [`Ipv6Addr::is_loopback()`] for more details.
250 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
252 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
253 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
255 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
256 #[stable(feature = "ip_shared", since = "1.12.0")]
259 pub const fn is_loopback(&self) -> bool {
261 IpAddr::V4(ip) => ip.is_loopback(),
262 IpAddr::V6(ip) => ip.is_loopback(),
266 /// Returns [`true`] if the address appears to be globally routable.
268 /// See the documentation for [`Ipv4Addr::is_global()`] and
269 /// [`Ipv6Addr::is_global()`] for more details.
276 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
278 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
279 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true);
281 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
282 #[unstable(feature = "ip", issue = "27709")]
285 pub const fn is_global(&self) -> bool {
287 IpAddr::V4(ip) => ip.is_global(),
288 IpAddr::V6(ip) => ip.is_global(),
292 /// Returns [`true`] if this is a multicast address.
294 /// See the documentation for [`Ipv4Addr::is_multicast()`] and
295 /// [`Ipv6Addr::is_multicast()`] for more details.
300 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
302 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
303 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
305 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
306 #[stable(feature = "ip_shared", since = "1.12.0")]
309 pub const fn is_multicast(&self) -> bool {
311 IpAddr::V4(ip) => ip.is_multicast(),
312 IpAddr::V6(ip) => ip.is_multicast(),
316 /// Returns [`true`] if this address is in a range designated for documentation.
318 /// See the documentation for [`Ipv4Addr::is_documentation()`] and
319 /// [`Ipv6Addr::is_documentation()`] for more details.
326 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
328 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
330 /// IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_documentation(),
334 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
335 #[unstable(feature = "ip", issue = "27709")]
338 pub const fn is_documentation(&self) -> bool {
340 IpAddr::V4(ip) => ip.is_documentation(),
341 IpAddr::V6(ip) => ip.is_documentation(),
345 /// Returns [`true`] if this address is in a range designated for benchmarking.
347 /// See the documentation for [`Ipv4Addr::is_benchmarking()`] and
348 /// [`Ipv6Addr::is_benchmarking()`] for more details.
355 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
357 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255)).is_benchmarking(), true);
358 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0)).is_benchmarking(), true);
360 #[unstable(feature = "ip", issue = "27709")]
363 pub const fn is_benchmarking(&self) -> bool {
365 IpAddr::V4(ip) => ip.is_benchmarking(),
366 IpAddr::V6(ip) => ip.is_benchmarking(),
370 /// Returns [`true`] if this address is an [`IPv4` address], and [`false`]
373 /// [`IPv4` address]: IpAddr::V4
378 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
380 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
381 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false);
383 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
384 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
387 pub const fn is_ipv4(&self) -> bool {
388 matches!(self, IpAddr::V4(_))
391 /// Returns [`true`] if this address is an [`IPv6` address], and [`false`]
394 /// [`IPv6` address]: IpAddr::V6
399 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
401 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
402 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true);
404 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
405 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
408 pub const fn is_ipv6(&self) -> bool {
409 matches!(self, IpAddr::V6(_))
412 /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped IPv6 addresses, otherwise it
413 /// return `self` as-is.
419 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
421 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
422 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
423 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
426 #[must_use = "this returns the result of the operation, \
427 without modifying the original"]
428 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
429 #[unstable(feature = "ip", issue = "27709")]
430 pub const fn to_canonical(&self) -> IpAddr {
432 &v4 @ IpAddr::V4(_) => v4,
433 IpAddr::V6(v6) => v6.to_canonical(),
439 /// Creates a new IPv4 address from four eight-bit octets.
441 /// The result will represent the IP address `a`.`b`.`c`.`d`.
446 /// use std::net::Ipv4Addr;
448 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
450 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
451 #[stable(feature = "rust1", since = "1.0.0")]
454 pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
455 Ipv4Addr { octets: [a, b, c, d] }
458 /// An IPv4 address with the address pointing to localhost: `127.0.0.1`
463 /// use std::net::Ipv4Addr;
465 /// let addr = Ipv4Addr::LOCALHOST;
466 /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
468 #[stable(feature = "ip_constructors", since = "1.30.0")]
469 pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
471 /// An IPv4 address representing an unspecified address: `0.0.0.0`
473 /// This corresponds to the constant `INADDR_ANY` in other languages.
478 /// use std::net::Ipv4Addr;
480 /// let addr = Ipv4Addr::UNSPECIFIED;
481 /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
483 #[doc(alias = "INADDR_ANY")]
484 #[stable(feature = "ip_constructors", since = "1.30.0")]
485 pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
487 /// An IPv4 address representing the broadcast address: `255.255.255.255`
492 /// use std::net::Ipv4Addr;
494 /// let addr = Ipv4Addr::BROADCAST;
495 /// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
497 #[stable(feature = "ip_constructors", since = "1.30.0")]
498 pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
500 /// Returns the four eight-bit integers that make up this address.
505 /// use std::net::Ipv4Addr;
507 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
508 /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
510 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
511 #[stable(feature = "rust1", since = "1.0.0")]
514 pub const fn octets(&self) -> [u8; 4] {
518 /// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
520 /// This property is defined in _UNIX Network Programming, Second Edition_,
521 /// W. Richard Stevens, p. 891; see also [ip7].
523 /// [ip7]: https://man7.org/linux/man-pages/man7/ip.7.html
528 /// use std::net::Ipv4Addr;
530 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
531 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
533 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
534 #[stable(feature = "ip_shared", since = "1.12.0")]
537 pub const fn is_unspecified(&self) -> bool {
538 u32::from_be_bytes(self.octets) == 0
541 /// Returns [`true`] if this is a loopback address (`127.0.0.0/8`).
543 /// This property is defined by [IETF RFC 1122].
545 /// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122
550 /// use std::net::Ipv4Addr;
552 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
553 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
555 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
556 #[stable(since = "1.7.0", feature = "ip_17")]
559 pub const fn is_loopback(&self) -> bool {
560 self.octets()[0] == 127
563 /// Returns [`true`] if this is a private address.
565 /// The private address ranges are defined in [IETF RFC 1918] and include:
568 /// - `172.16.0.0/12`
569 /// - `192.168.0.0/16`
571 /// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918
576 /// use std::net::Ipv4Addr;
578 /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
579 /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
580 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
581 /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
582 /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
583 /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
584 /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
586 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
587 #[stable(since = "1.7.0", feature = "ip_17")]
590 pub const fn is_private(&self) -> bool {
591 match self.octets() {
593 [172, b, ..] if b >= 16 && b <= 31 => true,
594 [192, 168, ..] => true,
599 /// Returns [`true`] if the address is link-local (`169.254.0.0/16`).
601 /// This property is defined by [IETF RFC 3927].
603 /// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927
608 /// use std::net::Ipv4Addr;
610 /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
611 /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
612 /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
614 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
615 #[stable(since = "1.7.0", feature = "ip_17")]
618 pub const fn is_link_local(&self) -> bool {
619 matches!(self.octets(), [169, 254, ..])
622 /// Returns [`true`] if the address appears to be globally reachable
623 /// as specified by the [IANA IPv4 Special-Purpose Address Registry].
624 /// Whether or not an address is practically reachable will depend on your network configuration.
626 /// Most IPv4 addresses are globally reachable;
627 /// unless they are specifically defined as *not* globally reachable.
629 /// Non-exhaustive list of notable addresses that are not globally reachable:
631 /// - The [unspecified address] ([`is_unspecified`](Ipv4Addr::is_unspecified))
632 /// - Addresses reserved for private use ([`is_private`](Ipv4Addr::is_private))
633 /// - Addresses in the shared address space ([`is_shared`](Ipv4Addr::is_shared))
634 /// - Loopback addresses ([`is_loopback`](Ipv4Addr::is_loopback))
635 /// - Link-local addresses ([`is_link_local`](Ipv4Addr::is_link_local))
636 /// - Addresses reserved for documentation ([`is_documentation`](Ipv4Addr::is_documentation))
637 /// - Addresses reserved for benchmarking ([`is_benchmarking`](Ipv4Addr::is_benchmarking))
638 /// - Reserved addresses ([`is_reserved`](Ipv4Addr::is_reserved))
639 /// - The [broadcast address] ([`is_broadcast`](Ipv4Addr::is_broadcast))
641 /// For the complete overview of which addresses are globally reachable, see the table at the [IANA IPv4 Special-Purpose Address Registry].
643 /// [IANA IPv4 Special-Purpose Address Registry]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
644 /// [unspecified address]: Ipv4Addr::UNSPECIFIED
645 /// [broadcast address]: Ipv4Addr::BROADCAST
653 /// use std::net::Ipv4Addr;
655 /// // Most IPv4 addresses are globally reachable:
656 /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
658 /// // However some addresses have been assigned a special meaning
659 /// // that makes them not globally reachable. Some examples are:
661 /// // The unspecified address (`0.0.0.0`)
662 /// assert_eq!(Ipv4Addr::UNSPECIFIED.is_global(), false);
664 /// // Addresses reserved for private use (`10.0.0.0/8`, `172.16.0.0/12`, 192.168.0.0/16)
665 /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
666 /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
667 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
669 /// // Addresses in the shared address space (`100.64.0.0/10`)
670 /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false);
672 /// // The loopback addresses (`127.0.0.0/8`)
673 /// assert_eq!(Ipv4Addr::LOCALHOST.is_global(), false);
675 /// // Link-local addresses (`169.254.0.0/16`)
676 /// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false);
678 /// // Addresses reserved for documentation (`192.0.2.0/24`, `198.51.100.0/24`, `203.0.113.0/24`)
679 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false);
680 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false);
681 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false);
683 /// // Addresses reserved for benchmarking (`198.18.0.0/15`)
684 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false);
686 /// // Reserved addresses (`240.0.0.0/4`)
687 /// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false);
689 /// // The broadcast address (`255.255.255.255`)
690 /// assert_eq!(Ipv4Addr::BROADCAST.is_global(), false);
692 /// // For a complete overview see the IANA IPv4 Special-Purpose Address Registry.
694 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
695 #[unstable(feature = "ip", issue = "27709")]
698 pub const fn is_global(&self) -> bool {
699 !(self.octets()[0] == 0 // "This network"
702 || self.is_loopback()
703 || self.is_link_local()
704 // addresses reserved for future protocols (`192.0.0.0/24`)
705 ||(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0)
706 || self.is_documentation()
707 || self.is_benchmarking()
708 || self.is_reserved()
709 || self.is_broadcast())
712 /// Returns [`true`] if this address is part of the Shared Address Space defined in
713 /// [IETF RFC 6598] (`100.64.0.0/10`).
715 /// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598
721 /// use std::net::Ipv4Addr;
723 /// assert_eq!(Ipv4Addr::new(100, 64, 0, 0).is_shared(), true);
724 /// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
725 /// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
727 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
728 #[unstable(feature = "ip", issue = "27709")]
731 pub const fn is_shared(&self) -> bool {
732 self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
735 /// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for
736 /// network devices benchmarking. This range is defined in [IETF RFC 2544] as `192.18.0.0`
737 /// through `198.19.255.255` but [errata 423] corrects it to `198.18.0.0/15`.
739 /// [IETF RFC 2544]: https://tools.ietf.org/html/rfc2544
740 /// [errata 423]: https://www.rfc-editor.org/errata/eid423
746 /// use std::net::Ipv4Addr;
748 /// assert_eq!(Ipv4Addr::new(198, 17, 255, 255).is_benchmarking(), false);
749 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_benchmarking(), true);
750 /// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true);
751 /// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false);
753 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
754 #[unstable(feature = "ip", issue = "27709")]
757 pub const fn is_benchmarking(&self) -> bool {
758 self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
761 /// Returns [`true`] if this address is reserved by IANA for future use. [IETF RFC 1112]
762 /// defines the block of reserved addresses as `240.0.0.0/4`. This range normally includes the
763 /// broadcast address `255.255.255.255`, but this implementation explicitly excludes it, since
764 /// it is obviously not reserved for future use.
766 /// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112
770 /// As IANA assigns new addresses, this method will be
771 /// updated. This may result in non-reserved addresses being
772 /// treated as reserved in code that relies on an outdated version
779 /// use std::net::Ipv4Addr;
781 /// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true);
782 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true);
784 /// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false);
785 /// // The broadcast address is not considered as reserved for future use by this implementation
786 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
788 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
789 #[unstable(feature = "ip", issue = "27709")]
792 pub const fn is_reserved(&self) -> bool {
793 self.octets()[0] & 240 == 240 && !self.is_broadcast()
796 /// Returns [`true`] if this is a multicast address (`224.0.0.0/4`).
798 /// Multicast addresses have a most significant octet between `224` and `239`,
799 /// and is defined by [IETF RFC 5771].
801 /// [IETF RFC 5771]: https://tools.ietf.org/html/rfc5771
806 /// use std::net::Ipv4Addr;
808 /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
809 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
810 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
812 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
813 #[stable(since = "1.7.0", feature = "ip_17")]
816 pub const fn is_multicast(&self) -> bool {
817 self.octets()[0] >= 224 && self.octets()[0] <= 239
820 /// Returns [`true`] if this is a broadcast address (`255.255.255.255`).
822 /// A broadcast address has all octets set to `255` as defined in [IETF RFC 919].
824 /// [IETF RFC 919]: https://tools.ietf.org/html/rfc919
829 /// use std::net::Ipv4Addr;
831 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
832 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
834 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
835 #[stable(since = "1.7.0", feature = "ip_17")]
838 pub const fn is_broadcast(&self) -> bool {
839 u32::from_be_bytes(self.octets()) == u32::from_be_bytes(Self::BROADCAST.octets())
842 /// Returns [`true`] if this address is in a range designated for documentation.
844 /// This is defined in [IETF RFC 5737]:
846 /// - `192.0.2.0/24` (TEST-NET-1)
847 /// - `198.51.100.0/24` (TEST-NET-2)
848 /// - `203.0.113.0/24` (TEST-NET-3)
850 /// [IETF RFC 5737]: https://tools.ietf.org/html/rfc5737
855 /// use std::net::Ipv4Addr;
857 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
858 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
859 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
860 /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
862 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
863 #[stable(since = "1.7.0", feature = "ip_17")]
866 pub const fn is_documentation(&self) -> bool {
867 matches!(self.octets(), [192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _])
870 /// Converts this address to an [IPv4-compatible] [`IPv6` address].
872 /// `a.b.c.d` becomes `::a.b.c.d`
874 /// Note that IPv4-compatible addresses have been officially deprecated.
875 /// If you don't explicitly need an IPv4-compatible address for legacy reasons, consider using `to_ipv6_mapped` instead.
877 /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
878 /// [`IPv6` address]: Ipv6Addr
883 /// use std::net::{Ipv4Addr, Ipv6Addr};
886 /// Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
887 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x2ff)
890 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
891 #[stable(feature = "rust1", since = "1.0.0")]
892 #[must_use = "this returns the result of the operation, \
893 without modifying the original"]
895 pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
896 let [a, b, c, d] = self.octets();
897 Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] }
900 /// Converts this address to an [IPv4-mapped] [`IPv6` address].
902 /// `a.b.c.d` becomes `::ffff:a.b.c.d`
904 /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
905 /// [`IPv6` address]: Ipv6Addr
910 /// use std::net::{Ipv4Addr, Ipv6Addr};
912 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
913 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff));
915 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
916 #[stable(feature = "rust1", since = "1.0.0")]
917 #[must_use = "this returns the result of the operation, \
918 without modifying the original"]
920 pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
921 let [a, b, c, d] = self.octets();
922 Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d] }
926 #[stable(feature = "ip_addr", since = "1.7.0")]
927 impl fmt::Display for IpAddr {
928 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
930 IpAddr::V4(ip) => ip.fmt(fmt),
931 IpAddr::V6(ip) => ip.fmt(fmt),
936 #[stable(feature = "ip_addr", since = "1.7.0")]
937 impl fmt::Debug for IpAddr {
938 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
939 fmt::Display::fmt(self, fmt)
943 #[stable(feature = "ip_from_ip", since = "1.16.0")]
944 impl From<Ipv4Addr> for IpAddr {
945 /// Copies this address to a new `IpAddr::V4`.
950 /// use std::net::{IpAddr, Ipv4Addr};
952 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
955 /// IpAddr::V4(addr),
956 /// IpAddr::from(addr)
960 fn from(ipv4: Ipv4Addr) -> IpAddr {
965 #[stable(feature = "ip_from_ip", since = "1.16.0")]
966 impl From<Ipv6Addr> for IpAddr {
967 /// Copies this address to a new `IpAddr::V6`.
972 /// use std::net::{IpAddr, Ipv6Addr};
974 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
977 /// IpAddr::V6(addr),
978 /// IpAddr::from(addr)
982 fn from(ipv6: Ipv6Addr) -> IpAddr {
987 #[stable(feature = "rust1", since = "1.0.0")]
988 impl fmt::Display for Ipv4Addr {
989 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
990 let octets = self.octets();
992 // If there are no alignment requirements, write the IP address directly to `f`.
993 // Otherwise, write it to a local buffer and then use `f.pad`.
994 if fmt.precision().is_none() && fmt.width().is_none() {
995 write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
997 const LONGEST_IPV4_ADDR: &str = "255.255.255.255";
999 let mut buf = DisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new();
1000 // Buffer is long enough for the longest possible IPv4 address, so this should never fail.
1001 write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
1003 fmt.pad(buf.as_str())
1008 #[stable(feature = "rust1", since = "1.0.0")]
1009 impl fmt::Debug for Ipv4Addr {
1010 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1011 fmt::Display::fmt(self, fmt)
1015 #[stable(feature = "ip_cmp", since = "1.16.0")]
1016 impl PartialEq<Ipv4Addr> for IpAddr {
1018 fn eq(&self, other: &Ipv4Addr) -> bool {
1020 IpAddr::V4(v4) => v4 == other,
1021 IpAddr::V6(_) => false,
1026 #[stable(feature = "ip_cmp", since = "1.16.0")]
1027 impl PartialEq<IpAddr> for Ipv4Addr {
1029 fn eq(&self, other: &IpAddr) -> bool {
1031 IpAddr::V4(v4) => self == v4,
1032 IpAddr::V6(_) => false,
1037 #[stable(feature = "rust1", since = "1.0.0")]
1038 impl PartialOrd for Ipv4Addr {
1040 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1041 Some(self.cmp(other))
1045 #[stable(feature = "ip_cmp", since = "1.16.0")]
1046 impl PartialOrd<Ipv4Addr> for IpAddr {
1048 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1050 IpAddr::V4(v4) => v4.partial_cmp(other),
1051 IpAddr::V6(_) => Some(Ordering::Greater),
1056 #[stable(feature = "ip_cmp", since = "1.16.0")]
1057 impl PartialOrd<IpAddr> for Ipv4Addr {
1059 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1061 IpAddr::V4(v4) => self.partial_cmp(v4),
1062 IpAddr::V6(_) => Some(Ordering::Less),
1067 #[stable(feature = "rust1", since = "1.0.0")]
1068 impl Ord for Ipv4Addr {
1070 fn cmp(&self, other: &Ipv4Addr) -> Ordering {
1071 self.octets.cmp(&other.octets)
1075 impl IntoInner<c::in_addr> for Ipv4Addr {
1077 fn into_inner(self) -> c::in_addr {
1078 // `s_addr` is stored as BE on all machines and the array is in BE order.
1079 // So the native endian conversion method is used so that it's never swapped.
1080 c::in_addr { s_addr: u32::from_ne_bytes(self.octets) }
1083 impl FromInner<c::in_addr> for Ipv4Addr {
1084 fn from_inner(addr: c::in_addr) -> Ipv4Addr {
1085 Ipv4Addr { octets: addr.s_addr.to_ne_bytes() }
1089 #[stable(feature = "ip_u32", since = "1.1.0")]
1090 impl From<Ipv4Addr> for u32 {
1091 /// Converts an `Ipv4Addr` into a host byte order `u32`.
1096 /// use std::net::Ipv4Addr;
1098 /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
1099 /// assert_eq!(0x12345678, u32::from(addr));
1102 fn from(ip: Ipv4Addr) -> u32 {
1103 u32::from_be_bytes(ip.octets)
1107 #[stable(feature = "ip_u32", since = "1.1.0")]
1108 impl From<u32> for Ipv4Addr {
1109 /// Converts a host byte order `u32` into an `Ipv4Addr`.
1114 /// use std::net::Ipv4Addr;
1116 /// let addr = Ipv4Addr::from(0x12345678);
1117 /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr);
1120 fn from(ip: u32) -> Ipv4Addr {
1121 Ipv4Addr { octets: ip.to_be_bytes() }
1125 #[stable(feature = "from_slice_v4", since = "1.9.0")]
1126 impl From<[u8; 4]> for Ipv4Addr {
1127 /// Creates an `Ipv4Addr` from a four element byte array.
1132 /// use std::net::Ipv4Addr;
1134 /// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
1135 /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
1138 fn from(octets: [u8; 4]) -> Ipv4Addr {
1143 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1144 impl From<[u8; 4]> for IpAddr {
1145 /// Creates an `IpAddr::V4` from a four element byte array.
1150 /// use std::net::{IpAddr, Ipv4Addr};
1152 /// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]);
1153 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr);
1156 fn from(octets: [u8; 4]) -> IpAddr {
1157 IpAddr::V4(Ipv4Addr::from(octets))
1162 /// Creates a new IPv6 address from eight 16-bit segments.
1164 /// The result will represent the IP address `a:b:c:d:e:f:g:h`.
1169 /// use std::net::Ipv6Addr;
1171 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
1173 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
1174 #[stable(feature = "rust1", since = "1.0.0")]
1177 pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
1189 // All elements in `addr16` are big endian.
1190 // SAFETY: `[u16; 8]` is always safe to transmute to `[u8; 16]`.
1191 octets: unsafe { transmute::<_, [u8; 16]>(addr16) },
1195 /// An IPv6 address representing localhost: `::1`.
1200 /// use std::net::Ipv6Addr;
1202 /// let addr = Ipv6Addr::LOCALHOST;
1203 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1205 #[stable(feature = "ip_constructors", since = "1.30.0")]
1206 pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
1208 /// An IPv6 address representing the unspecified address: `::`
1213 /// use std::net::Ipv6Addr;
1215 /// let addr = Ipv6Addr::UNSPECIFIED;
1216 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1218 #[stable(feature = "ip_constructors", since = "1.30.0")]
1219 pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
1221 /// Returns the eight 16-bit segments that make up this address.
1226 /// use std::net::Ipv6Addr;
1228 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
1229 /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
1231 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1232 #[stable(feature = "rust1", since = "1.0.0")]
1235 pub const fn segments(&self) -> [u16; 8] {
1236 // All elements in `self.octets` must be big endian.
1237 // SAFETY: `[u8; 16]` is always safe to transmute to `[u16; 8]`.
1238 let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(self.octets) };
1239 // We want native endian u16
1252 /// Returns [`true`] for the special 'unspecified' address (`::`).
1254 /// This property is defined in [IETF RFC 4291].
1256 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1261 /// use std::net::Ipv6Addr;
1263 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
1264 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
1266 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1267 #[stable(since = "1.7.0", feature = "ip_17")]
1270 pub const fn is_unspecified(&self) -> bool {
1271 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
1274 /// Returns [`true`] if this is the [loopback address] (`::1`),
1275 /// as defined in [IETF RFC 4291 section 2.5.3].
1277 /// Contrary to IPv4, in IPv6 there is only one loopback address.
1279 /// [loopback address]: Ipv6Addr::LOCALHOST
1280 /// [IETF RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
1285 /// use std::net::Ipv6Addr;
1287 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
1288 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
1290 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1291 #[stable(since = "1.7.0", feature = "ip_17")]
1294 pub const fn is_loopback(&self) -> bool {
1295 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
1298 /// Returns [`true`] if the address appears to be globally reachable
1299 /// as specified by the [IANA IPv6 Special-Purpose Address Registry].
1300 /// Whether or not an address is practically reachable will depend on your network configuration.
1302 /// Most IPv6 addresses are globally reachable;
1303 /// unless they are specifically defined as *not* globally reachable.
1305 /// Non-exhaustive list of notable addresses that are not globally reachable:
1306 /// - The [unspecified address] ([`is_unspecified`](Ipv6Addr::is_unspecified))
1307 /// - The [loopback address] ([`is_loopback`](Ipv6Addr::is_loopback))
1308 /// - IPv4-mapped addresses
1309 /// - Addresses reserved for benchmarking
1310 /// - Addresses reserved for documentation ([`is_documentation`](Ipv6Addr::is_documentation))
1311 /// - Unique local addresses ([`is_unique_local`](Ipv6Addr::is_unique_local))
1312 /// - Unicast addresses with link-local scope ([`is_unicast_link_local`](Ipv6Addr::is_unicast_link_local))
1314 /// For the complete overview of which addresses are globally reachable, see the table at the [IANA IPv6 Special-Purpose Address Registry].
1316 /// Note that an address having global scope is not the same as being globally reachable,
1317 /// and there is no direct relation between the two concepts: There exist addresses with global scope
1318 /// that are not globally reachable (for example unique local addresses),
1319 /// and addresses that are globally reachable without having global scope
1320 /// (multicast addresses with non-global scope).
1322 /// [IANA IPv6 Special-Purpose Address Registry]: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
1323 /// [unspecified address]: Ipv6Addr::UNSPECIFIED
1324 /// [loopback address]: Ipv6Addr::LOCALHOST
1331 /// use std::net::Ipv6Addr;
1333 /// // Most IPv6 addresses are globally reachable:
1334 /// assert_eq!(Ipv6Addr::new(0x26, 0, 0x1c9, 0, 0, 0xafc8, 0x10, 0x1).is_global(), true);
1336 /// // However some addresses have been assigned a special meaning
1337 /// // that makes them not globally reachable. Some examples are:
1339 /// // The unspecified address (`::`)
1340 /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_global(), false);
1342 /// // The loopback address (`::1`)
1343 /// assert_eq!(Ipv6Addr::LOCALHOST.is_global(), false);
1345 /// // IPv4-mapped addresses (`::ffff:0:0/96`)
1346 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), false);
1348 /// // Addresses reserved for benchmarking (`2001:2::/48`)
1349 /// assert_eq!(Ipv6Addr::new(0x2001, 2, 0, 0, 0, 0, 0, 1,).is_global(), false);
1351 /// // Addresses reserved for documentation (`2001:db8::/32`)
1352 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).is_global(), false);
1354 /// // Unique local addresses (`fc00::/7`)
1355 /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 1).is_global(), false);
1357 /// // Unicast addresses with link-local scope (`fe80::/10`)
1358 /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 1).is_global(), false);
1360 /// // For a complete overview see the IANA IPv6 Special-Purpose Address Registry.
1362 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1363 #[unstable(feature = "ip", issue = "27709")]
1366 pub const fn is_global(&self) -> bool {
1367 !(self.is_unspecified()
1368 || self.is_loopback()
1369 // IPv4-mapped Address (`::ffff:0:0/96`)
1370 || matches!(self.segments(), [0, 0, 0, 0, 0, 0xffff, _, _])
1371 // IPv4-IPv6 Translat. (`64:ff9b:1::/48`)
1372 || matches!(self.segments(), [0x64, 0xff9b, 1, _, _, _, _, _])
1373 // Discard-Only Address Block (`100::/64`)
1374 || matches!(self.segments(), [0x100, 0, 0, 0, _, _, _, _])
1375 // IETF Protocol Assignments (`2001::/23`)
1376 || (matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200)
1378 // Port Control Protocol Anycast (`2001:1::1`)
1379 u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001
1380 // Traversal Using Relays around NAT Anycast (`2001:1::2`)
1381 || u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002
1382 // AMT (`2001:3::/32`)
1383 || matches!(self.segments(), [0x2001, 3, _, _, _, _, _, _])
1384 // AS112-v6 (`2001:4:112::/48`)
1385 || matches!(self.segments(), [0x2001, 4, 0x112, _, _, _, _, _])
1386 // ORCHIDv2 (`2001:20::/28`)
1387 || matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x2F)
1389 || self.is_documentation()
1390 || self.is_unique_local()
1391 || self.is_unicast_link_local())
1394 /// Returns [`true`] if this is a unique local address (`fc00::/7`).
1396 /// This property is defined in [IETF RFC 4193].
1398 /// [IETF RFC 4193]: https://tools.ietf.org/html/rfc4193
1405 /// use std::net::Ipv6Addr;
1407 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
1408 /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
1410 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1411 #[unstable(feature = "ip", issue = "27709")]
1414 pub const fn is_unique_local(&self) -> bool {
1415 (self.segments()[0] & 0xfe00) == 0xfc00
1418 /// Returns [`true`] if this is a unicast address, as defined by [IETF RFC 4291].
1419 /// Any address that is not a [multicast address] (`ff00::/8`) is unicast.
1421 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1422 /// [multicast address]: Ipv6Addr::is_multicast
1429 /// use std::net::Ipv6Addr;
1431 /// // The unspecified and loopback addresses are unicast.
1432 /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast(), true);
1433 /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast(), true);
1435 /// // Any address that is not a multicast address (`ff00::/8`) is unicast.
1436 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast(), true);
1437 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_unicast(), false);
1439 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1440 #[unstable(feature = "ip", issue = "27709")]
1443 pub const fn is_unicast(&self) -> bool {
1444 !self.is_multicast()
1447 /// Returns `true` if the address is a unicast address with link-local scope,
1448 /// as defined in [RFC 4291].
1450 /// A unicast address has link-local scope if it has the prefix `fe80::/10`, as per [RFC 4291 section 2.4].
1451 /// Note that this encompasses more addresses than those defined in [RFC 4291 section 2.5.6],
1452 /// which describes "Link-Local IPv6 Unicast Addresses" as having the following stricter format:
1455 /// | 10 bits | 54 bits | 64 bits |
1456 /// +----------+-------------------------+----------------------------+
1457 /// |1111111010| 0 | interface ID |
1458 /// +----------+-------------------------+----------------------------+
1460 /// So while currently the only addresses with link-local scope an application will encounter are all in `fe80::/64`,
1461 /// this might change in the future with the publication of new standards. More addresses in `fe80::/10` could be allocated,
1462 /// and those addresses will have link-local scope.
1464 /// Also note that while [RFC 4291 section 2.5.3] mentions about the [loopback address] (`::1`) that "it is treated as having Link-Local scope",
1465 /// this does not mean that the loopback address actually has link-local scope and this method will return `false` on it.
1467 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
1468 /// [RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
1469 /// [RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
1470 /// [RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6
1471 /// [loopback address]: Ipv6Addr::LOCALHOST
1478 /// use std::net::Ipv6Addr;
1480 /// // The loopback address (`::1`) does not actually have link-local scope.
1481 /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast_link_local(), false);
1483 /// // Only addresses in `fe80::/10` have link-local scope.
1484 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), false);
1485 /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
1487 /// // Addresses outside the stricter `fe80::/64` also have link-local scope.
1488 /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0).is_unicast_link_local(), true);
1489 /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
1491 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1492 #[unstable(feature = "ip", issue = "27709")]
1495 pub const fn is_unicast_link_local(&self) -> bool {
1496 (self.segments()[0] & 0xffc0) == 0xfe80
1499 /// Returns [`true`] if this is an address reserved for documentation
1500 /// (`2001:db8::/32`).
1502 /// This property is defined in [IETF RFC 3849].
1504 /// [IETF RFC 3849]: https://tools.ietf.org/html/rfc3849
1511 /// use std::net::Ipv6Addr;
1513 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
1514 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
1516 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1517 #[unstable(feature = "ip", issue = "27709")]
1520 pub const fn is_documentation(&self) -> bool {
1521 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
1524 /// Returns [`true`] if this is an address reserved for benchmarking (`2001:2::/48`).
1526 /// This property is defined in [IETF RFC 5180], where it is mistakenly specified as covering the range `2001:0200::/48`.
1527 /// This is corrected in [IETF RFC Errata 1752] to `2001:0002::/48`.
1529 /// [IETF RFC 5180]: https://tools.ietf.org/html/rfc5180
1530 /// [IETF RFC Errata 1752]: https://www.rfc-editor.org/errata_search.php?eid=1752
1535 /// use std::net::Ipv6Addr;
1537 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc613, 0x0).is_benchmarking(), false);
1538 /// assert_eq!(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0).is_benchmarking(), true);
1540 #[unstable(feature = "ip", issue = "27709")]
1543 pub const fn is_benchmarking(&self) -> bool {
1544 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) && (self.segments()[2] == 0)
1547 /// Returns [`true`] if the address is a globally routable unicast address.
1549 /// The following return false:
1551 /// - the loopback address
1552 /// - the link-local addresses
1553 /// - unique local addresses
1554 /// - the unspecified address
1555 /// - the address range reserved for documentation
1557 /// This method returns [`true`] for site-local addresses as per [RFC 4291 section 2.5.7]
1560 /// The special behavior of [the site-local unicast] prefix defined in [RFC3513] must no longer
1561 /// be supported in new implementations (i.e., new implementations must treat this prefix as
1562 /// Global Unicast).
1565 /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
1572 /// use std::net::Ipv6Addr;
1574 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1575 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
1577 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1578 #[unstable(feature = "ip", issue = "27709")]
1581 pub const fn is_unicast_global(&self) -> bool {
1583 && !self.is_loopback()
1584 && !self.is_unicast_link_local()
1585 && !self.is_unique_local()
1586 && !self.is_unspecified()
1587 && !self.is_documentation()
1588 && !self.is_benchmarking()
1591 /// Returns the address's multicast scope if the address is multicast.
1598 /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
1601 /// Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
1602 /// Some(Ipv6MulticastScope::Global)
1604 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
1606 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1607 #[unstable(feature = "ip", issue = "27709")]
1610 pub const fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
1611 if self.is_multicast() {
1612 match self.segments()[0] & 0x000f {
1613 1 => Some(Ipv6MulticastScope::InterfaceLocal),
1614 2 => Some(Ipv6MulticastScope::LinkLocal),
1615 3 => Some(Ipv6MulticastScope::RealmLocal),
1616 4 => Some(Ipv6MulticastScope::AdminLocal),
1617 5 => Some(Ipv6MulticastScope::SiteLocal),
1618 8 => Some(Ipv6MulticastScope::OrganizationLocal),
1619 14 => Some(Ipv6MulticastScope::Global),
1627 /// Returns [`true`] if this is a multicast address (`ff00::/8`).
1629 /// This property is defined by [IETF RFC 4291].
1631 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1636 /// use std::net::Ipv6Addr;
1638 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
1639 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
1641 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1642 #[stable(since = "1.7.0", feature = "ip_17")]
1645 pub const fn is_multicast(&self) -> bool {
1646 (self.segments()[0] & 0xff00) == 0xff00
1649 /// Converts this address to an [`IPv4` address] if it's an [IPv4-mapped] address,
1650 /// as defined in [IETF RFC 4291 section 2.5.5.2], otherwise returns [`None`].
1652 /// `::ffff:a.b.c.d` becomes `a.b.c.d`.
1653 /// All addresses *not* starting with `::ffff` will return `None`.
1655 /// [`IPv4` address]: Ipv4Addr
1656 /// [IPv4-mapped]: Ipv6Addr
1657 /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
1662 /// use std::net::{Ipv4Addr, Ipv6Addr};
1664 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4_mapped(), None);
1665 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4_mapped(),
1666 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
1667 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
1669 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1670 #[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
1671 #[must_use = "this returns the result of the operation, \
1672 without modifying the original"]
1674 pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
1675 match self.octets() {
1676 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
1677 Some(Ipv4Addr::new(a, b, c, d))
1683 /// Converts this address to an [`IPv4` address] if it is either
1684 /// an [IPv4-compatible] address as defined in [IETF RFC 4291 section 2.5.5.1],
1685 /// or an [IPv4-mapped] address as defined in [IETF RFC 4291 section 2.5.5.2],
1686 /// otherwise returns [`None`].
1688 /// Note that this will return an [`IPv4` address] for the IPv6 loopback address `::1`. Use
1689 /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this.
1691 /// `::a.b.c.d` and `::ffff:a.b.c.d` become `a.b.c.d`. `::1` becomes `0.0.0.1`.
1692 /// All addresses *not* starting with either all zeroes or `::ffff` will return `None`.
1694 /// [`IPv4` address]: Ipv4Addr
1695 /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
1696 /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
1697 /// [IETF RFC 4291 section 2.5.5.1]: https://tools.ietf.org/html/rfc4291#section-2.5.5.1
1698 /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
1703 /// use std::net::{Ipv4Addr, Ipv6Addr};
1705 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
1706 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
1707 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
1708 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
1709 /// Some(Ipv4Addr::new(0, 0, 0, 1)));
1711 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1712 #[stable(feature = "rust1", since = "1.0.0")]
1713 #[must_use = "this returns the result of the operation, \
1714 without modifying the original"]
1716 pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
1717 if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
1718 let [a, b] = ab.to_be_bytes();
1719 let [c, d] = cd.to_be_bytes();
1720 Some(Ipv4Addr::new(a, b, c, d))
1726 /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped addresses, otherwise it
1727 /// returns self wrapped in an `IpAddr::V6`.
1733 /// use std::net::Ipv6Addr;
1735 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
1736 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
1738 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1739 #[unstable(feature = "ip", issue = "27709")]
1740 #[must_use = "this returns the result of the operation, \
1741 without modifying the original"]
1743 pub const fn to_canonical(&self) -> IpAddr {
1744 if let Some(mapped) = self.to_ipv4_mapped() {
1745 return IpAddr::V4(mapped);
1750 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
1753 /// use std::net::Ipv6Addr;
1755 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
1756 /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1758 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
1759 #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
1762 pub const fn octets(&self) -> [u8; 16] {
1767 /// Write an Ipv6Addr, conforming to the canonical style described by
1768 /// [RFC 5952](https://tools.ietf.org/html/rfc5952).
1769 #[stable(feature = "rust1", since = "1.0.0")]
1770 impl fmt::Display for Ipv6Addr {
1771 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1772 // If there are no alignment requirements, write the IP address directly to `f`.
1773 // Otherwise, write it to a local buffer and then use `f.pad`.
1774 if f.precision().is_none() && f.width().is_none() {
1775 let segments = self.segments();
1777 // Special case for :: and ::1; otherwise they get written with the
1779 if self.is_unspecified() {
1781 } else if self.is_loopback() {
1783 } else if let Some(ipv4) = self.to_ipv4() {
1785 // IPv4 Compatible address
1786 0 => write!(f, "::{}", ipv4),
1787 // IPv4 Mapped address
1788 0xffff => write!(f, "::ffff:{}", ipv4),
1789 _ => unreachable!(),
1792 #[derive(Copy, Clone, Default)]
1798 // Find the inner 0 span
1800 let mut longest = Span::default();
1801 let mut current = Span::default();
1803 for (i, &segment) in segments.iter().enumerate() {
1805 if current.len == 0 {
1811 if current.len > longest.len {
1815 current = Span::default();
1822 /// Write a colon-separated part of the address
1824 fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result {
1825 if let Some((first, tail)) = chunk.split_first() {
1826 write!(f, "{:x}", first)?;
1827 for segment in tail {
1829 write!(f, "{:x}", segment)?;
1836 fmt_subslice(f, &segments[..zeroes.start])?;
1838 fmt_subslice(f, &segments[zeroes.start + zeroes.len..])
1840 fmt_subslice(f, &segments)
1844 const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";
1846 let mut buf = DisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new();
1847 // Buffer is long enough for the longest possible IPv6 address, so this should never fail.
1848 write!(buf, "{}", self).unwrap();
1855 #[stable(feature = "rust1", since = "1.0.0")]
1856 impl fmt::Debug for Ipv6Addr {
1857 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1858 fmt::Display::fmt(self, fmt)
1862 #[stable(feature = "ip_cmp", since = "1.16.0")]
1863 impl PartialEq<IpAddr> for Ipv6Addr {
1865 fn eq(&self, other: &IpAddr) -> bool {
1867 IpAddr::V4(_) => false,
1868 IpAddr::V6(v6) => self == v6,
1873 #[stable(feature = "ip_cmp", since = "1.16.0")]
1874 impl PartialEq<Ipv6Addr> for IpAddr {
1876 fn eq(&self, other: &Ipv6Addr) -> bool {
1878 IpAddr::V4(_) => false,
1879 IpAddr::V6(v6) => v6 == other,
1884 #[stable(feature = "rust1", since = "1.0.0")]
1885 impl PartialOrd for Ipv6Addr {
1887 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1888 Some(self.cmp(other))
1892 #[stable(feature = "ip_cmp", since = "1.16.0")]
1893 impl PartialOrd<Ipv6Addr> for IpAddr {
1895 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1897 IpAddr::V4(_) => Some(Ordering::Less),
1898 IpAddr::V6(v6) => v6.partial_cmp(other),
1903 #[stable(feature = "ip_cmp", since = "1.16.0")]
1904 impl PartialOrd<IpAddr> for Ipv6Addr {
1906 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1908 IpAddr::V4(_) => Some(Ordering::Greater),
1909 IpAddr::V6(v6) => self.partial_cmp(v6),
1914 #[stable(feature = "rust1", since = "1.0.0")]
1915 impl Ord for Ipv6Addr {
1917 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1918 self.segments().cmp(&other.segments())
1922 impl IntoInner<c::in6_addr> for Ipv6Addr {
1923 fn into_inner(self) -> c::in6_addr {
1924 c::in6_addr { s6_addr: self.octets }
1927 impl FromInner<c::in6_addr> for Ipv6Addr {
1929 fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1930 Ipv6Addr { octets: addr.s6_addr }
1934 #[stable(feature = "i128", since = "1.26.0")]
1935 impl From<Ipv6Addr> for u128 {
1936 /// Convert an `Ipv6Addr` into a host byte order `u128`.
1941 /// use std::net::Ipv6Addr;
1943 /// let addr = Ipv6Addr::new(
1944 /// 0x1020, 0x3040, 0x5060, 0x7080,
1945 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1947 /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1950 fn from(ip: Ipv6Addr) -> u128 {
1951 u128::from_be_bytes(ip.octets)
1954 #[stable(feature = "i128", since = "1.26.0")]
1955 impl From<u128> for Ipv6Addr {
1956 /// Convert a host byte order `u128` into an `Ipv6Addr`.
1961 /// use std::net::Ipv6Addr;
1963 /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1966 /// 0x1020, 0x3040, 0x5060, 0x7080,
1967 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1972 fn from(ip: u128) -> Ipv6Addr {
1973 Ipv6Addr::from(ip.to_be_bytes())
1977 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
1978 impl From<[u8; 16]> for Ipv6Addr {
1979 /// Creates an `Ipv6Addr` from a sixteen element byte array.
1984 /// use std::net::Ipv6Addr;
1986 /// let addr = Ipv6Addr::from([
1987 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
1988 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
2001 fn from(octets: [u8; 16]) -> Ipv6Addr {
2006 #[stable(feature = "ipv6_from_segments", since = "1.16.0")]
2007 impl From<[u16; 8]> for Ipv6Addr {
2008 /// Creates an `Ipv6Addr` from an eight element 16-bit array.
2013 /// use std::net::Ipv6Addr;
2015 /// let addr = Ipv6Addr::from([
2016 /// 525u16, 524u16, 523u16, 522u16,
2017 /// 521u16, 520u16, 519u16, 518u16,
2030 fn from(segments: [u16; 8]) -> Ipv6Addr {
2031 let [a, b, c, d, e, f, g, h] = segments;
2032 Ipv6Addr::new(a, b, c, d, e, f, g, h)
2036 #[stable(feature = "ip_from_slice", since = "1.17.0")]
2037 impl From<[u8; 16]> for IpAddr {
2038 /// Creates an `IpAddr::V6` from a sixteen element byte array.
2043 /// use std::net::{IpAddr, Ipv6Addr};
2045 /// let addr = IpAddr::from([
2046 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
2047 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
2050 /// IpAddr::V6(Ipv6Addr::new(
2060 fn from(octets: [u8; 16]) -> IpAddr {
2061 IpAddr::V6(Ipv6Addr::from(octets))
2065 #[stable(feature = "ip_from_slice", since = "1.17.0")]
2066 impl From<[u16; 8]> for IpAddr {
2067 /// Creates an `IpAddr::V6` from an eight element 16-bit array.
2072 /// use std::net::{IpAddr, Ipv6Addr};
2074 /// let addr = IpAddr::from([
2075 /// 525u16, 524u16, 523u16, 522u16,
2076 /// 521u16, 520u16, 519u16, 518u16,
2079 /// IpAddr::V6(Ipv6Addr::new(
2089 fn from(segments: [u16; 8]) -> IpAddr {
2090 IpAddr::V6(Ipv6Addr::from(segments))