1 // Tests for this module
2 #[cfg(all(test, not(target_os = "emscripten")))]
5 use crate::cmp::Ordering;
6 use crate::fmt::{self, Write as FmtWrite};
8 use crate::io::Write as IoWrite;
9 use crate::mem::transmute;
10 use crate::sys::net::netc as c;
11 use crate::sys_common::{AsInner, FromInner, IntoInner};
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.
18 /// The size of an `IpAddr` instance may vary depending on the target operating
24 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
26 /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
27 /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
29 /// assert_eq!("127.0.0.1".parse(), Ok(localhost_v4));
30 /// assert_eq!("::1".parse(), Ok(localhost_v6));
32 /// assert_eq!(localhost_v4.is_ipv6(), false);
33 /// assert_eq!(localhost_v4.is_ipv4(), true);
35 #[stable(feature = "ip_addr", since = "1.7.0")]
36 #[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
39 #[stable(feature = "ip_addr", since = "1.7.0")]
40 V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
42 #[stable(feature = "ip_addr", since = "1.7.0")]
43 V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
48 /// IPv4 addresses are defined as 32-bit integers in [IETF RFC 791].
49 /// They are usually represented as four octets.
51 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
53 /// The size of an `Ipv4Addr` struct may vary depending on the target operating
56 /// [IETF RFC 791]: https://tools.ietf.org/html/rfc791
58 /// # Textual representation
60 /// `Ipv4Addr` provides a [`FromStr`] implementation. The four octets are in decimal
61 /// notation, divided by `.` (this is called "dot-decimal notation").
62 /// Notably, octal numbers (which are indicated with a leading `0`) and hexadecimal numbers (which
63 /// are indicated with a leading `0x`) are not allowed per [IETF RFC 6943].
65 /// [IETF RFC 6943]: https://tools.ietf.org/html/rfc6943#section-3.1.1
66 /// [`FromStr`]: crate::str::FromStr
71 /// use std::net::Ipv4Addr;
73 /// let localhost = Ipv4Addr::new(127, 0, 0, 1);
74 /// assert_eq!("127.0.0.1".parse(), Ok(localhost));
75 /// assert_eq!(localhost.is_loopback(), true);
76 /// assert!("012.004.002.000".parse::<Ipv4Addr>().is_err()); // all octets are in octal
77 /// assert!("0000000.0.0.0".parse::<Ipv4Addr>().is_err()); // first octet is a zero in octal
78 /// assert!("0xcb.0x0.0x71.0x00".parse::<Ipv4Addr>().is_err()); // all octets are in hex
81 #[stable(feature = "rust1", since = "1.0.0")]
88 /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291].
89 /// They are usually represented as eight 16-bit segments.
91 /// The size of an `Ipv6Addr` struct may vary depending on the target operating
94 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
96 /// # Embedding IPv4 Addresses
98 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
100 /// To assist in the transition from IPv4 to IPv6 two types of IPv6 addresses that embed an IPv4 address were defined:
101 /// IPv4-compatible and IPv4-mapped addresses. Of these IPv4-compatible addresses have been officially deprecated.
103 /// Both types of addresses are not assigned any special meaning by this implementation,
104 /// other than what the relevant standards prescribe. This means that an address like `::ffff:127.0.0.1`,
105 /// while representing an IPv4 loopback address, is not itself an IPv6 loopback address; only `::1` is.
106 /// To handle these so called "IPv4-in-IPv6" addresses, they have to first be converted to their canonical IPv4 address.
108 /// ### IPv4-Compatible IPv6 Addresses
110 /// IPv4-compatible IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.1], and have been officially deprecated.
111 /// The RFC describes the format of an "IPv4-Compatible IPv6 address" as follows:
114 /// | 80 bits | 16 | 32 bits |
115 /// +--------------------------------------+--------------------------+
116 /// |0000..............................0000|0000| IPv4 address |
117 /// +--------------------------------------+----+---------------------+
119 /// So `::a.b.c.d` would be an IPv4-compatible IPv6 address representing the IPv4 address `a.b.c.d`.
121 /// To convert from an IPv4 address to an IPv4-compatible IPv6 address, use [`Ipv4Addr::to_ipv6_compatible`].
122 /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-compatible IPv6 address to the canonical IPv4 address.
124 /// [IETF RFC 4291 Section 2.5.5.1]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.1
126 /// ### IPv4-Mapped IPv6 Addresses
128 /// IPv4-mapped IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.2].
129 /// The RFC describes the format of an "IPv4-Mapped IPv6 address" as follows:
132 /// | 80 bits | 16 | 32 bits |
133 /// +--------------------------------------+--------------------------+
134 /// |0000..............................0000|FFFF| IPv4 address |
135 /// +--------------------------------------+----+---------------------+
137 /// So `::ffff:a.b.c.d` would be an IPv4-mapped IPv6 address representing the IPv4 address `a.b.c.d`.
139 /// To convert from an IPv4 address to an IPv4-mapped IPv6 address, use [`Ipv4Addr::to_ipv6_mapped`].
140 /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-mapped IPv6 address to the canonical IPv4 address.
141 /// Note that this will also convert the IPv6 loopback address `::1` to `0.0.0.1`. Use
142 /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this.
144 /// [IETF RFC 4291 Section 2.5.5.2]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2
146 /// # Textual representation
148 /// `Ipv6Addr` provides a [`FromStr`] implementation. There are many ways to represent
149 /// an IPv6 address in text, but in general, each segments is written in hexadecimal
150 /// notation, and segments are separated by `:`. For more information, see
153 /// [`FromStr`]: crate::str::FromStr
154 /// [IETF RFC 5952]: https://tools.ietf.org/html/rfc5952
159 /// use std::net::Ipv6Addr;
161 /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
162 /// assert_eq!("::1".parse(), Ok(localhost));
163 /// assert_eq!(localhost.is_loopback(), true);
166 #[stable(feature = "rust1", since = "1.0.0")]
167 pub struct Ipv6Addr {
171 /// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2].
173 /// # Stability Guarantees
175 /// Not all possible values for a multicast scope have been assigned.
176 /// Future RFCs may introduce new scopes, which will be added as variants to this enum;
177 /// because of this the enum is marked as `#[non_exhaustive]`.
183 /// use std::net::Ipv6Addr;
184 /// use std::net::Ipv6MulticastScope::*;
186 /// // An IPv6 multicast address with global scope (`ff0e::`).
187 /// let address = Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0);
189 /// // Will print "Global scope".
190 /// match address.multicast_scope() {
191 /// Some(InterfaceLocal) => println!("Interface-Local scope"),
192 /// Some(LinkLocal) => println!("Link-Local scope"),
193 /// Some(RealmLocal) => println!("Realm-Local scope"),
194 /// Some(AdminLocal) => println!("Admin-Local scope"),
195 /// Some(SiteLocal) => println!("Site-Local scope"),
196 /// Some(OrganizationLocal) => println!("Organization-Local scope"),
197 /// Some(Global) => println!("Global scope"),
198 /// Some(_) => println!("Unknown scope"),
199 /// None => println!("Not a multicast address!")
204 /// [IPv6 multicast address]: Ipv6Addr
205 /// [IETF RFC 7346 section 2]: https://tools.ietf.org/html/rfc7346#section-2
206 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
207 #[unstable(feature = "ip", issue = "27709")]
209 pub enum Ipv6MulticastScope {
210 /// Interface-Local scope.
212 /// Link-Local scope.
214 /// Realm-Local scope.
216 /// Admin-Local scope.
218 /// Site-Local scope.
220 /// Organization-Local scope.
227 /// Returns [`true`] for the special 'unspecified' address.
229 /// See the documentation for [`Ipv4Addr::is_unspecified()`] and
230 /// [`Ipv6Addr::is_unspecified()`] for more details.
235 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
237 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
238 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
240 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
241 #[stable(feature = "ip_shared", since = "1.12.0")]
244 pub const fn is_unspecified(&self) -> bool {
246 IpAddr::V4(ip) => ip.is_unspecified(),
247 IpAddr::V6(ip) => ip.is_unspecified(),
251 /// Returns [`true`] if this is a loopback address.
253 /// See the documentation for [`Ipv4Addr::is_loopback()`] and
254 /// [`Ipv6Addr::is_loopback()`] for more details.
259 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
261 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
262 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
264 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
265 #[stable(feature = "ip_shared", since = "1.12.0")]
268 pub const fn is_loopback(&self) -> bool {
270 IpAddr::V4(ip) => ip.is_loopback(),
271 IpAddr::V6(ip) => ip.is_loopback(),
275 /// Returns [`true`] if the address appears to be globally routable.
277 /// See the documentation for [`Ipv4Addr::is_global()`] and
278 /// [`Ipv6Addr::is_global()`] for more details.
285 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
287 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
288 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true);
290 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
291 #[unstable(feature = "ip", issue = "27709")]
294 pub const fn is_global(&self) -> bool {
296 IpAddr::V4(ip) => ip.is_global(),
297 IpAddr::V6(ip) => ip.is_global(),
301 /// Returns [`true`] if this is a multicast address.
303 /// See the documentation for [`Ipv4Addr::is_multicast()`] and
304 /// [`Ipv6Addr::is_multicast()`] for more details.
309 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
311 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
312 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
314 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
315 #[stable(feature = "ip_shared", since = "1.12.0")]
318 pub const fn is_multicast(&self) -> bool {
320 IpAddr::V4(ip) => ip.is_multicast(),
321 IpAddr::V6(ip) => ip.is_multicast(),
325 /// Returns [`true`] if this address is in a range designated for documentation.
327 /// See the documentation for [`Ipv4Addr::is_documentation()`] and
328 /// [`Ipv6Addr::is_documentation()`] for more details.
335 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
337 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
339 /// IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_documentation(),
343 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
344 #[unstable(feature = "ip", issue = "27709")]
347 pub const fn is_documentation(&self) -> bool {
349 IpAddr::V4(ip) => ip.is_documentation(),
350 IpAddr::V6(ip) => ip.is_documentation(),
354 /// Returns [`true`] if this address is in a range designated for benchmarking.
356 /// See the documentation for [`Ipv4Addr::is_benchmarking()`] and
357 /// [`Ipv6Addr::is_benchmarking()`] for more details.
364 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
366 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255)).is_benchmarking(), true);
367 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0)).is_benchmarking(), true);
369 #[unstable(feature = "ip", issue = "27709")]
372 pub const fn is_benchmarking(&self) -> bool {
374 IpAddr::V4(ip) => ip.is_benchmarking(),
375 IpAddr::V6(ip) => ip.is_benchmarking(),
379 /// Returns [`true`] if this address is an [`IPv4` address], and [`false`]
382 /// [`IPv4` address]: IpAddr::V4
387 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
389 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
390 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false);
392 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
393 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
396 pub const fn is_ipv4(&self) -> bool {
397 matches!(self, IpAddr::V4(_))
400 /// Returns [`true`] if this address is an [`IPv6` address], and [`false`]
403 /// [`IPv6` address]: IpAddr::V6
408 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
410 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
411 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true);
413 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
414 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
417 pub const fn is_ipv6(&self) -> bool {
418 matches!(self, IpAddr::V6(_))
421 /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped IPv6 addresses, otherwise it
422 /// return `self` as-is.
428 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
430 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
431 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
432 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
435 #[must_use = "this returns the result of the operation, \
436 without modifying the original"]
437 #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
438 #[unstable(feature = "ip", issue = "27709")]
439 pub const fn to_canonical(&self) -> IpAddr {
441 &v4 @ IpAddr::V4(_) => v4,
442 IpAddr::V6(v6) => v6.to_canonical(),
448 /// Creates a new IPv4 address from four eight-bit octets.
450 /// The result will represent the IP address `a`.`b`.`c`.`d`.
455 /// use std::net::Ipv4Addr;
457 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
459 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
460 #[stable(feature = "rust1", since = "1.0.0")]
463 pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
464 // `s_addr` is stored as BE on all machine and the array is in BE order.
465 // So the native endian conversion method is used so that it's never swapped.
466 Ipv4Addr { inner: c::in_addr { s_addr: u32::from_ne_bytes([a, b, c, d]) } }
469 /// An IPv4 address with the address pointing to localhost: `127.0.0.1`
474 /// use std::net::Ipv4Addr;
476 /// let addr = Ipv4Addr::LOCALHOST;
477 /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
479 #[stable(feature = "ip_constructors", since = "1.30.0")]
480 pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
482 /// An IPv4 address representing an unspecified address: `0.0.0.0`
484 /// This corresponds to the constant `INADDR_ANY` in other languages.
489 /// use std::net::Ipv4Addr;
491 /// let addr = Ipv4Addr::UNSPECIFIED;
492 /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
494 #[doc(alias = "INADDR_ANY")]
495 #[stable(feature = "ip_constructors", since = "1.30.0")]
496 pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
498 /// An IPv4 address representing the broadcast address: `255.255.255.255`
503 /// use std::net::Ipv4Addr;
505 /// let addr = Ipv4Addr::BROADCAST;
506 /// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
508 #[stable(feature = "ip_constructors", since = "1.30.0")]
509 pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
511 /// Returns the four eight-bit integers that make up this address.
516 /// use std::net::Ipv4Addr;
518 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
519 /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
521 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
522 #[stable(feature = "rust1", since = "1.0.0")]
525 pub const fn octets(&self) -> [u8; 4] {
526 // This returns the order we want because s_addr is stored in big-endian.
527 self.inner.s_addr.to_ne_bytes()
530 /// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
532 /// This property is defined in _UNIX Network Programming, Second Edition_,
533 /// W. Richard Stevens, p. 891; see also [ip7].
535 /// [ip7]: https://man7.org/linux/man-pages/man7/ip.7.html
540 /// use std::net::Ipv4Addr;
542 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
543 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
545 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
546 #[stable(feature = "ip_shared", since = "1.12.0")]
549 pub const fn is_unspecified(&self) -> bool {
550 self.inner.s_addr == 0
553 /// Returns [`true`] if this is a loopback address (`127.0.0.0/8`).
555 /// This property is defined by [IETF RFC 1122].
557 /// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122
562 /// use std::net::Ipv4Addr;
564 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
565 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
567 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
568 #[stable(since = "1.7.0", feature = "ip_17")]
571 pub const fn is_loopback(&self) -> bool {
572 self.octets()[0] == 127
575 /// Returns [`true`] if this is a private address.
577 /// The private address ranges are defined in [IETF RFC 1918] and include:
580 /// - `172.16.0.0/12`
581 /// - `192.168.0.0/16`
583 /// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918
588 /// use std::net::Ipv4Addr;
590 /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
591 /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
592 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
593 /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
594 /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
595 /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
596 /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
598 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
599 #[stable(since = "1.7.0", feature = "ip_17")]
602 pub const fn is_private(&self) -> bool {
603 match self.octets() {
605 [172, b, ..] if b >= 16 && b <= 31 => true,
606 [192, 168, ..] => true,
611 /// Returns [`true`] if the address is link-local (`169.254.0.0/16`).
613 /// This property is defined by [IETF RFC 3927].
615 /// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927
620 /// use std::net::Ipv4Addr;
622 /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
623 /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
624 /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
626 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
627 #[stable(since = "1.7.0", feature = "ip_17")]
630 pub const fn is_link_local(&self) -> bool {
631 matches!(self.octets(), [169, 254, ..])
634 /// Returns [`true`] if the address appears to be globally routable.
635 /// See [iana-ipv4-special-registry][ipv4-sr].
637 /// The following return [`false`]:
639 /// - private addresses (see [`Ipv4Addr::is_private()`])
640 /// - the loopback address (see [`Ipv4Addr::is_loopback()`])
641 /// - the link-local address (see [`Ipv4Addr::is_link_local()`])
642 /// - the broadcast address (see [`Ipv4Addr::is_broadcast()`])
643 /// - addresses used for documentation (see [`Ipv4Addr::is_documentation()`])
644 /// - the unspecified address (see [`Ipv4Addr::is_unspecified()`]), and the whole
645 /// `0.0.0.0/8` block
646 /// - addresses reserved for future protocols, except
647 /// `192.0.0.9/32` and `192.0.0.10/32` which are globally routable
648 /// - addresses reserved for future use (see [`Ipv4Addr::is_reserved()`]
649 /// - addresses reserved for networking devices benchmarking (see
650 /// [`Ipv4Addr::is_benchmarking()`])
652 /// [ipv4-sr]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
659 /// use std::net::Ipv4Addr;
661 /// // private addresses are not global
662 /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
663 /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
664 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
666 /// // the 0.0.0.0/8 block is not global
667 /// assert_eq!(Ipv4Addr::new(0, 1, 2, 3).is_global(), false);
668 /// // in particular, the unspecified address is not global
669 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
671 /// // the loopback address is not global
672 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_global(), false);
674 /// // link local addresses are not global
675 /// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false);
677 /// // the broadcast address is not global
678 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_global(), false);
680 /// // the address space designated for documentation is not global
681 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false);
682 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false);
683 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false);
685 /// // shared addresses are not global
686 /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false);
688 /// // addresses reserved for protocol assignment are not global
689 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_global(), false);
690 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_global(), false);
692 /// // addresses reserved for future use are not global
693 /// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false);
695 /// // addresses reserved for network devices benchmarking are not global
696 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false);
698 /// // All the other addresses are global
699 /// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true);
700 /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
702 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
703 #[unstable(feature = "ip", issue = "27709")]
706 pub const fn is_global(&self) -> bool {
707 // check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
708 // globally routable addresses in the 192.0.0.0/24 range.
709 if u32::from_be_bytes(self.octets()) == 0xc0000009
710 || u32::from_be_bytes(self.octets()) == 0xc000000a
715 && !self.is_loopback()
716 && !self.is_link_local()
717 && !self.is_broadcast()
718 && !self.is_documentation()
720 // addresses reserved for future protocols (`192.0.0.0/24`)
721 && !(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0)
722 && !self.is_reserved()
723 && !self.is_benchmarking()
724 // Make sure the address is not in 0.0.0.0/8
725 && self.octets()[0] != 0
728 /// Returns [`true`] if this address is part of the Shared Address Space defined in
729 /// [IETF RFC 6598] (`100.64.0.0/10`).
731 /// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598
737 /// use std::net::Ipv4Addr;
739 /// assert_eq!(Ipv4Addr::new(100, 64, 0, 0).is_shared(), true);
740 /// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
741 /// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
743 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
744 #[unstable(feature = "ip", issue = "27709")]
747 pub const fn is_shared(&self) -> bool {
748 self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
751 /// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for
752 /// network devices benchmarking. This range is defined in [IETF RFC 2544] as `192.18.0.0`
753 /// through `198.19.255.255` but [errata 423] corrects it to `198.18.0.0/15`.
755 /// [IETF RFC 2544]: https://tools.ietf.org/html/rfc2544
756 /// [errata 423]: https://www.rfc-editor.org/errata/eid423
762 /// use std::net::Ipv4Addr;
764 /// assert_eq!(Ipv4Addr::new(198, 17, 255, 255).is_benchmarking(), false);
765 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_benchmarking(), true);
766 /// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true);
767 /// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false);
769 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
770 #[unstable(feature = "ip", issue = "27709")]
773 pub const fn is_benchmarking(&self) -> bool {
774 self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
777 /// Returns [`true`] if this address is reserved by IANA for future use. [IETF RFC 1112]
778 /// defines the block of reserved addresses as `240.0.0.0/4`. This range normally includes the
779 /// broadcast address `255.255.255.255`, but this implementation explicitly excludes it, since
780 /// it is obviously not reserved for future use.
782 /// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112
786 /// As IANA assigns new addresses, this method will be
787 /// updated. This may result in non-reserved addresses being
788 /// treated as reserved in code that relies on an outdated version
795 /// use std::net::Ipv4Addr;
797 /// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true);
798 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true);
800 /// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false);
801 /// // The broadcast address is not considered as reserved for future use by this implementation
802 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
804 #[rustc_const_unstable(feature = "const_ipv4", issue = "76205")]
805 #[unstable(feature = "ip", issue = "27709")]
808 pub const fn is_reserved(&self) -> bool {
809 self.octets()[0] & 240 == 240 && !self.is_broadcast()
812 /// Returns [`true`] if this is a multicast address (`224.0.0.0/4`).
814 /// Multicast addresses have a most significant octet between `224` and `239`,
815 /// and is defined by [IETF RFC 5771].
817 /// [IETF RFC 5771]: https://tools.ietf.org/html/rfc5771
822 /// use std::net::Ipv4Addr;
824 /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
825 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
826 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
828 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
829 #[stable(since = "1.7.0", feature = "ip_17")]
832 pub const fn is_multicast(&self) -> bool {
833 self.octets()[0] >= 224 && self.octets()[0] <= 239
836 /// Returns [`true`] if this is a broadcast address (`255.255.255.255`).
838 /// A broadcast address has all octets set to `255` as defined in [IETF RFC 919].
840 /// [IETF RFC 919]: https://tools.ietf.org/html/rfc919
845 /// use std::net::Ipv4Addr;
847 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
848 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
850 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
851 #[stable(since = "1.7.0", feature = "ip_17")]
854 pub const fn is_broadcast(&self) -> bool {
855 u32::from_be_bytes(self.octets()) == u32::from_be_bytes(Self::BROADCAST.octets())
858 /// Returns [`true`] if this address is in a range designated for documentation.
860 /// This is defined in [IETF RFC 5737]:
862 /// - `192.0.2.0/24` (TEST-NET-1)
863 /// - `198.51.100.0/24` (TEST-NET-2)
864 /// - `203.0.113.0/24` (TEST-NET-3)
866 /// [IETF RFC 5737]: https://tools.ietf.org/html/rfc5737
871 /// use std::net::Ipv4Addr;
873 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
874 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
875 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
876 /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
878 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
879 #[stable(since = "1.7.0", feature = "ip_17")]
882 pub const fn is_documentation(&self) -> bool {
883 matches!(self.octets(), [192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _])
886 /// Converts this address to an [IPv4-compatible] [`IPv6` address].
888 /// `a.b.c.d` becomes `::a.b.c.d`
890 /// Note that IPv4-compatible addresses have been officially deprecated.
891 /// If you don't explicitly need an IPv4-compatible address for legacy reasons, consider using `to_ipv6_mapped` instead.
893 /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
894 /// [`IPv6` address]: Ipv6Addr
899 /// use std::net::{Ipv4Addr, Ipv6Addr};
902 /// Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
903 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x2ff)
906 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
907 #[stable(feature = "rust1", since = "1.0.0")]
908 #[must_use = "this returns the result of the operation, \
909 without modifying the original"]
911 pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
912 let [a, b, c, d] = self.octets();
914 inner: c::in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] },
918 /// Converts this address to an [IPv4-mapped] [`IPv6` address].
920 /// `a.b.c.d` becomes `::ffff:a.b.c.d`
922 /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
923 /// [`IPv6` address]: Ipv6Addr
928 /// use std::net::{Ipv4Addr, Ipv6Addr};
930 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
931 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff));
933 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
934 #[stable(feature = "rust1", since = "1.0.0")]
935 #[must_use = "this returns the result of the operation, \
936 without modifying the original"]
938 pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
939 let [a, b, c, d] = self.octets();
941 inner: c::in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d] },
946 #[stable(feature = "ip_addr", since = "1.7.0")]
947 impl fmt::Display for IpAddr {
948 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
950 IpAddr::V4(ip) => ip.fmt(fmt),
951 IpAddr::V6(ip) => ip.fmt(fmt),
956 #[stable(feature = "ip_addr", since = "1.7.0")]
957 impl fmt::Debug for IpAddr {
958 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
959 fmt::Display::fmt(self, fmt)
963 #[stable(feature = "ip_from_ip", since = "1.16.0")]
964 impl From<Ipv4Addr> for IpAddr {
965 /// Copies this address to a new `IpAddr::V4`.
970 /// use std::net::{IpAddr, Ipv4Addr};
972 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
975 /// IpAddr::V4(addr),
976 /// IpAddr::from(addr)
980 fn from(ipv4: Ipv4Addr) -> IpAddr {
985 #[stable(feature = "ip_from_ip", since = "1.16.0")]
986 impl From<Ipv6Addr> for IpAddr {
987 /// Copies this address to a new `IpAddr::V6`.
992 /// use std::net::{IpAddr, Ipv6Addr};
994 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
997 /// IpAddr::V6(addr),
998 /// IpAddr::from(addr)
1002 fn from(ipv6: Ipv6Addr) -> IpAddr {
1007 #[stable(feature = "rust1", since = "1.0.0")]
1008 impl fmt::Display for Ipv4Addr {
1009 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1010 let octets = self.octets();
1011 // Fast Path: if there's no alignment stuff, write directly to the buffer
1012 if fmt.precision().is_none() && fmt.width().is_none() {
1013 write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
1015 const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address
1016 let mut buf = [0u8; IPV4_BUF_LEN];
1017 let mut buf_slice = &mut buf[..];
1019 // Note: The call to write should never fail, hence the unwrap
1020 write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
1021 let len = IPV4_BUF_LEN - buf_slice.len();
1023 // This unsafe is OK because we know what is being written to the buffer
1024 let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
1030 #[stable(feature = "rust1", since = "1.0.0")]
1031 impl fmt::Debug for Ipv4Addr {
1032 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1033 fmt::Display::fmt(self, fmt)
1037 #[stable(feature = "rust1", since = "1.0.0")]
1038 impl Clone for Ipv4Addr {
1040 fn clone(&self) -> Ipv4Addr {
1045 #[stable(feature = "rust1", since = "1.0.0")]
1046 impl PartialEq for Ipv4Addr {
1048 fn eq(&self, other: &Ipv4Addr) -> bool {
1049 self.inner.s_addr == other.inner.s_addr
1053 #[stable(feature = "ip_cmp", since = "1.16.0")]
1054 impl PartialEq<Ipv4Addr> for IpAddr {
1056 fn eq(&self, other: &Ipv4Addr) -> bool {
1058 IpAddr::V4(v4) => v4 == other,
1059 IpAddr::V6(_) => false,
1064 #[stable(feature = "ip_cmp", since = "1.16.0")]
1065 impl PartialEq<IpAddr> for Ipv4Addr {
1067 fn eq(&self, other: &IpAddr) -> bool {
1069 IpAddr::V4(v4) => self == v4,
1070 IpAddr::V6(_) => false,
1075 #[stable(feature = "rust1", since = "1.0.0")]
1076 impl Eq for Ipv4Addr {}
1078 #[stable(feature = "rust1", since = "1.0.0")]
1079 impl hash::Hash for Ipv4Addr {
1081 fn hash<H: hash::Hasher>(&self, s: &mut H) {
1083 // * hash in big endian order
1084 // * in netbsd, `in_addr` has `repr(packed)`, we need to
1085 // copy `s_addr` to avoid unsafe borrowing
1086 { self.inner.s_addr }.hash(s)
1090 #[stable(feature = "rust1", since = "1.0.0")]
1091 impl PartialOrd for Ipv4Addr {
1093 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1094 Some(self.cmp(other))
1098 #[stable(feature = "ip_cmp", since = "1.16.0")]
1099 impl PartialOrd<Ipv4Addr> for IpAddr {
1101 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1103 IpAddr::V4(v4) => v4.partial_cmp(other),
1104 IpAddr::V6(_) => Some(Ordering::Greater),
1109 #[stable(feature = "ip_cmp", since = "1.16.0")]
1110 impl PartialOrd<IpAddr> for Ipv4Addr {
1112 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1114 IpAddr::V4(v4) => self.partial_cmp(v4),
1115 IpAddr::V6(_) => Some(Ordering::Less),
1120 #[stable(feature = "rust1", since = "1.0.0")]
1121 impl Ord for Ipv4Addr {
1123 fn cmp(&self, other: &Ipv4Addr) -> Ordering {
1124 // Compare as native endian
1125 u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
1129 impl IntoInner<c::in_addr> for Ipv4Addr {
1131 fn into_inner(self) -> c::in_addr {
1136 #[stable(feature = "ip_u32", since = "1.1.0")]
1137 impl From<Ipv4Addr> for u32 {
1138 /// Converts an `Ipv4Addr` into a host byte order `u32`.
1143 /// use std::net::Ipv4Addr;
1145 /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78);
1146 /// assert_eq!(0x12345678, u32::from(addr));
1149 fn from(ip: Ipv4Addr) -> u32 {
1150 let ip = ip.octets();
1151 u32::from_be_bytes(ip)
1155 #[stable(feature = "ip_u32", since = "1.1.0")]
1156 impl From<u32> for Ipv4Addr {
1157 /// Converts a host byte order `u32` into an `Ipv4Addr`.
1162 /// use std::net::Ipv4Addr;
1164 /// let addr = Ipv4Addr::from(0x12345678);
1165 /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr);
1168 fn from(ip: u32) -> Ipv4Addr {
1169 Ipv4Addr::from(ip.to_be_bytes())
1173 #[stable(feature = "from_slice_v4", since = "1.9.0")]
1174 impl From<[u8; 4]> for Ipv4Addr {
1175 /// Creates an `Ipv4Addr` from a four element byte array.
1180 /// use std::net::Ipv4Addr;
1182 /// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
1183 /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
1186 fn from(octets: [u8; 4]) -> Ipv4Addr {
1187 Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
1191 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1192 impl From<[u8; 4]> for IpAddr {
1193 /// Creates an `IpAddr::V4` from a four element byte array.
1198 /// use std::net::{IpAddr, Ipv4Addr};
1200 /// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]);
1201 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr);
1204 fn from(octets: [u8; 4]) -> IpAddr {
1205 IpAddr::V4(Ipv4Addr::from(octets))
1210 /// Creates a new IPv6 address from eight 16-bit segments.
1212 /// The result will represent the IP address `a:b:c:d:e:f:g:h`.
1217 /// use std::net::Ipv6Addr;
1219 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
1221 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
1222 #[stable(feature = "rust1", since = "1.0.0")]
1225 pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
1237 inner: c::in6_addr {
1238 // All elements in `addr16` are big endian.
1239 // SAFETY: `[u16; 8]` is always safe to transmute to `[u8; 16]`.
1240 // rustc_allow_const_fn_unstable: the transmute could be written as stable const
1241 // code, but that leads to worse code generation (#75085)
1242 s6_addr: unsafe { transmute::<_, [u8; 16]>(addr16) },
1247 /// An IPv6 address representing localhost: `::1`.
1252 /// use std::net::Ipv6Addr;
1254 /// let addr = Ipv6Addr::LOCALHOST;
1255 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1257 #[stable(feature = "ip_constructors", since = "1.30.0")]
1258 pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
1260 /// An IPv6 address representing the unspecified address: `::`
1265 /// use std::net::Ipv6Addr;
1267 /// let addr = Ipv6Addr::UNSPECIFIED;
1268 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1270 #[stable(feature = "ip_constructors", since = "1.30.0")]
1271 pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
1273 /// Returns the eight 16-bit segments that make up this address.
1278 /// use std::net::Ipv6Addr;
1280 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
1281 /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
1283 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1284 #[stable(feature = "rust1", since = "1.0.0")]
1287 pub const fn segments(&self) -> [u16; 8] {
1288 // All elements in `s6_addr` must be big endian.
1289 // SAFETY: `[u8; 16]` is always safe to transmute to `[u16; 8]`.
1290 // rustc_allow_const_fn_unstable: the transmute could be written as stable const code, but
1291 // that leads to worse code generation (#75085)
1292 let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(self.inner.s6_addr) };
1293 // We want native endian u16
1306 /// Returns [`true`] for the special 'unspecified' address (`::`).
1308 /// This property is defined in [IETF RFC 4291].
1310 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1315 /// use std::net::Ipv6Addr;
1317 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
1318 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
1320 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1321 #[stable(since = "1.7.0", feature = "ip_17")]
1324 pub const fn is_unspecified(&self) -> bool {
1325 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
1328 /// Returns [`true`] if this is the [loopback address] (`::1`),
1329 /// as defined in [IETF RFC 4291 section 2.5.3].
1331 /// Contrary to IPv4, in IPv6 there is only one loopback address.
1333 /// [loopback address]: Ipv6Addr::LOCALHOST
1334 /// [IETF RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
1339 /// use std::net::Ipv6Addr;
1341 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
1342 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
1344 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1345 #[stable(since = "1.7.0", feature = "ip_17")]
1348 pub const fn is_loopback(&self) -> bool {
1349 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
1352 /// Returns [`true`] if the address appears to be globally routable.
1354 /// The following return [`false`]:
1356 /// - the loopback address
1357 /// - link-local and unique local unicast addresses
1358 /// - interface-, link-, realm-, admin- and site-local multicast addresses
1365 /// use std::net::Ipv6Addr;
1367 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
1368 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
1369 /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
1371 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1372 #[unstable(feature = "ip", issue = "27709")]
1375 pub const fn is_global(&self) -> bool {
1376 match self.multicast_scope() {
1377 Some(Ipv6MulticastScope::Global) => true,
1378 None => self.is_unicast_global(),
1383 /// Returns [`true`] if this is a unique local address (`fc00::/7`).
1385 /// This property is defined in [IETF RFC 4193].
1387 /// [IETF RFC 4193]: https://tools.ietf.org/html/rfc4193
1394 /// use std::net::Ipv6Addr;
1396 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
1397 /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
1399 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1400 #[unstable(feature = "ip", issue = "27709")]
1403 pub const fn is_unique_local(&self) -> bool {
1404 (self.segments()[0] & 0xfe00) == 0xfc00
1407 /// Returns [`true`] if this is a unicast address, as defined by [IETF RFC 4291].
1408 /// Any address that is not a [multicast address] (`ff00::/8`) is unicast.
1410 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1411 /// [multicast address]: Ipv6Addr::is_multicast
1418 /// use std::net::Ipv6Addr;
1420 /// // The unspecified and loopback addresses are unicast.
1421 /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast(), true);
1422 /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast(), true);
1424 /// // Any address that is not a multicast address (`ff00::/8`) is unicast.
1425 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast(), true);
1426 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_unicast(), false);
1428 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1429 #[unstable(feature = "ip", issue = "27709")]
1432 pub const fn is_unicast(&self) -> bool {
1433 !self.is_multicast()
1436 /// Returns `true` if the address is a unicast address with link-local scope,
1437 /// as defined in [RFC 4291].
1439 /// A unicast address has link-local scope if it has the prefix `fe80::/10`, as per [RFC 4291 section 2.4].
1440 /// Note that this encompasses more addresses than those defined in [RFC 4291 section 2.5.6],
1441 /// which describes "Link-Local IPv6 Unicast Addresses" as having the following stricter format:
1444 /// | 10 bits | 54 bits | 64 bits |
1445 /// +----------+-------------------------+----------------------------+
1446 /// |1111111010| 0 | interface ID |
1447 /// +----------+-------------------------+----------------------------+
1449 /// So while currently the only addresses with link-local scope an application will encounter are all in `fe80::/64`,
1450 /// this might change in the future with the publication of new standards. More addresses in `fe80::/10` could be allocated,
1451 /// and those addresses will have link-local scope.
1453 /// 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",
1454 /// this does not mean that the loopback address actually has link-local scope and this method will return `false` on it.
1456 /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
1457 /// [RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
1458 /// [RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
1459 /// [RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6
1460 /// [loopback address]: Ipv6Addr::LOCALHOST
1467 /// use std::net::Ipv6Addr;
1469 /// // The loopback address (`::1`) does not actually have link-local scope.
1470 /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast_link_local(), false);
1472 /// // Only addresses in `fe80::/10` have link-local scope.
1473 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), false);
1474 /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
1476 /// // Addresses outside the stricter `fe80::/64` also have link-local scope.
1477 /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0).is_unicast_link_local(), true);
1478 /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
1480 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1481 #[unstable(feature = "ip", issue = "27709")]
1484 pub const fn is_unicast_link_local(&self) -> bool {
1485 (self.segments()[0] & 0xffc0) == 0xfe80
1488 /// Returns [`true`] if this is an address reserved for documentation
1489 /// (`2001:db8::/32`).
1491 /// This property is defined in [IETF RFC 3849].
1493 /// [IETF RFC 3849]: https://tools.ietf.org/html/rfc3849
1500 /// use std::net::Ipv6Addr;
1502 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
1503 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
1505 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1506 #[unstable(feature = "ip", issue = "27709")]
1509 pub const fn is_documentation(&self) -> bool {
1510 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
1513 /// Returns [`true`] if this is an address reserved for benchmarking (`2001:2::/48`).
1515 /// This property is defined in [IETF RFC 5180], where it is mistakenly specified as covering the range `2001:0200::/48`.
1516 /// This is corrected in [IETF RFC Errata 1752] to `2001:0002::/48`.
1518 /// [IETF RFC 5180]: https://tools.ietf.org/html/rfc5180
1519 /// [IETF RFC Errata 1752]: https://www.rfc-editor.org/errata_search.php?eid=1752
1524 /// use std::net::Ipv6Addr;
1526 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc613, 0x0).is_benchmarking(), false);
1527 /// assert_eq!(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0).is_benchmarking(), true);
1529 #[unstable(feature = "ip", issue = "27709")]
1532 pub const fn is_benchmarking(&self) -> bool {
1533 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) && (self.segments()[2] == 0)
1536 /// Returns [`true`] if the address is a globally routable unicast address.
1538 /// The following return false:
1540 /// - the loopback address
1541 /// - the link-local addresses
1542 /// - unique local addresses
1543 /// - the unspecified address
1544 /// - the address range reserved for documentation
1546 /// This method returns [`true`] for site-local addresses as per [RFC 4291 section 2.5.7]
1549 /// The special behavior of [the site-local unicast] prefix defined in [RFC3513] must no longer
1550 /// be supported in new implementations (i.e., new implementations must treat this prefix as
1551 /// Global Unicast).
1554 /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
1561 /// use std::net::Ipv6Addr;
1563 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1564 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
1566 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1567 #[unstable(feature = "ip", issue = "27709")]
1570 pub const fn is_unicast_global(&self) -> bool {
1572 && !self.is_loopback()
1573 && !self.is_unicast_link_local()
1574 && !self.is_unique_local()
1575 && !self.is_unspecified()
1576 && !self.is_documentation()
1579 /// Returns the address's multicast scope if the address is multicast.
1586 /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
1589 /// Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
1590 /// Some(Ipv6MulticastScope::Global)
1592 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
1594 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1595 #[unstable(feature = "ip", issue = "27709")]
1598 pub const fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
1599 if self.is_multicast() {
1600 match self.segments()[0] & 0x000f {
1601 1 => Some(Ipv6MulticastScope::InterfaceLocal),
1602 2 => Some(Ipv6MulticastScope::LinkLocal),
1603 3 => Some(Ipv6MulticastScope::RealmLocal),
1604 4 => Some(Ipv6MulticastScope::AdminLocal),
1605 5 => Some(Ipv6MulticastScope::SiteLocal),
1606 8 => Some(Ipv6MulticastScope::OrganizationLocal),
1607 14 => Some(Ipv6MulticastScope::Global),
1615 /// Returns [`true`] if this is a multicast address (`ff00::/8`).
1617 /// This property is defined by [IETF RFC 4291].
1619 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1624 /// use std::net::Ipv6Addr;
1626 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
1627 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
1629 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1630 #[stable(since = "1.7.0", feature = "ip_17")]
1633 pub const fn is_multicast(&self) -> bool {
1634 (self.segments()[0] & 0xff00) == 0xff00
1637 /// Converts this address to an [`IPv4` address] if it's an [IPv4-mapped] address,
1638 /// as defined in [IETF RFC 4291 section 2.5.5.2], otherwise returns [`None`].
1640 /// `::ffff:a.b.c.d` becomes `a.b.c.d`.
1641 /// All addresses *not* starting with `::ffff` will return `None`.
1643 /// [`IPv4` address]: Ipv4Addr
1644 /// [IPv4-mapped]: Ipv6Addr
1645 /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
1650 /// use std::net::{Ipv4Addr, Ipv6Addr};
1652 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4_mapped(), None);
1653 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4_mapped(),
1654 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
1655 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
1657 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1658 #[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
1659 #[must_use = "this returns the result of the operation, \
1660 without modifying the original"]
1662 pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
1663 match self.octets() {
1664 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
1665 Some(Ipv4Addr::new(a, b, c, d))
1671 /// Converts this address to an [`IPv4` address] if it is either
1672 /// an [IPv4-compatible] address as defined in [IETF RFC 4291 section 2.5.5.1],
1673 /// or an [IPv4-mapped] address as defined in [IETF RFC 4291 section 2.5.5.2],
1674 /// otherwise returns [`None`].
1676 /// Note that this will return an [`IPv4` address] for the IPv6 loopback address `::1`. Use
1677 /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this.
1679 /// `::a.b.c.d` and `::ffff:a.b.c.d` become `a.b.c.d`. `::1` becomes `0.0.0.1`.
1680 /// All addresses *not* starting with either all zeroes or `::ffff` will return `None`.
1682 /// [`IPv4` address]: Ipv4Addr
1683 /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
1684 /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
1685 /// [IETF RFC 4291 section 2.5.5.1]: https://tools.ietf.org/html/rfc4291#section-2.5.5.1
1686 /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
1691 /// use std::net::{Ipv4Addr, Ipv6Addr};
1693 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
1694 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
1695 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
1696 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
1697 /// Some(Ipv4Addr::new(0, 0, 0, 1)));
1699 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1700 #[stable(feature = "rust1", since = "1.0.0")]
1701 #[must_use = "this returns the result of the operation, \
1702 without modifying the original"]
1704 pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
1705 if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
1706 let [a, b] = ab.to_be_bytes();
1707 let [c, d] = cd.to_be_bytes();
1708 Some(Ipv4Addr::new(a, b, c, d))
1714 /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped addresses, otherwise it
1715 /// returns self wrapped in an `IpAddr::V6`.
1721 /// use std::net::Ipv6Addr;
1723 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
1724 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
1726 #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1727 #[unstable(feature = "ip", issue = "27709")]
1728 #[must_use = "this returns the result of the operation, \
1729 without modifying the original"]
1731 pub const fn to_canonical(&self) -> IpAddr {
1732 if let Some(mapped) = self.to_ipv4_mapped() {
1733 return IpAddr::V4(mapped);
1738 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
1741 /// use std::net::Ipv6Addr;
1743 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
1744 /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1746 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
1747 #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
1750 pub const fn octets(&self) -> [u8; 16] {
1755 /// Write an Ipv6Addr, conforming to the canonical style described by
1756 /// [RFC 5952](https://tools.ietf.org/html/rfc5952).
1757 #[stable(feature = "rust1", since = "1.0.0")]
1758 impl fmt::Display for Ipv6Addr {
1759 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1760 // If there are no alignment requirements, write out the IP address to
1761 // f. Otherwise, write it to a local buffer, then use f.pad.
1762 if f.precision().is_none() && f.width().is_none() {
1763 let segments = self.segments();
1765 // Special case for :: and ::1; otherwise they get written with the
1767 if self.is_unspecified() {
1769 } else if self.is_loopback() {
1771 } else if let Some(ipv4) = self.to_ipv4() {
1773 // IPv4 Compatible address
1774 0 => write!(f, "::{}", ipv4),
1775 // IPv4 Mapped address
1776 0xffff => write!(f, "::ffff:{}", ipv4),
1777 _ => unreachable!(),
1780 #[derive(Copy, Clone, Default)]
1786 // Find the inner 0 span
1788 let mut longest = Span::default();
1789 let mut current = Span::default();
1791 for (i, &segment) in segments.iter().enumerate() {
1793 if current.len == 0 {
1799 if current.len > longest.len {
1803 current = Span::default();
1810 /// Write a colon-separated part of the address
1812 fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result {
1813 if let Some((first, tail)) = chunk.split_first() {
1814 write!(f, "{:x}", first)?;
1815 for segment in tail {
1817 write!(f, "{:x}", segment)?;
1824 fmt_subslice(f, &segments[..zeroes.start])?;
1826 fmt_subslice(f, &segments[zeroes.start + zeroes.len..])
1828 fmt_subslice(f, &segments)
1832 // Slow path: write the address to a local buffer, then use f.pad.
1833 // Defined recursively by using the fast path to write to the
1836 // This is the largest possible size of an IPv6 address
1837 const IPV6_BUF_LEN: usize = (4 * 8) + 7;
1838 let mut buf = [0u8; IPV6_BUF_LEN];
1839 let mut buf_slice = &mut buf[..];
1841 // Note: This call to write should never fail, so unwrap is okay.
1842 write!(buf_slice, "{}", self).unwrap();
1843 let len = IPV6_BUF_LEN - buf_slice.len();
1845 // This is safe because we know exactly what can be in this buffer
1846 let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
1852 #[stable(feature = "rust1", since = "1.0.0")]
1853 impl fmt::Debug for Ipv6Addr {
1854 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1855 fmt::Display::fmt(self, fmt)
1859 #[stable(feature = "rust1", since = "1.0.0")]
1860 impl Clone for Ipv6Addr {
1862 fn clone(&self) -> Ipv6Addr {
1867 #[stable(feature = "rust1", since = "1.0.0")]
1868 impl PartialEq for Ipv6Addr {
1870 fn eq(&self, other: &Ipv6Addr) -> bool {
1871 self.inner.s6_addr == other.inner.s6_addr
1875 #[stable(feature = "ip_cmp", since = "1.16.0")]
1876 impl PartialEq<IpAddr> for Ipv6Addr {
1878 fn eq(&self, other: &IpAddr) -> bool {
1880 IpAddr::V4(_) => false,
1881 IpAddr::V6(v6) => self == v6,
1886 #[stable(feature = "ip_cmp", since = "1.16.0")]
1887 impl PartialEq<Ipv6Addr> for IpAddr {
1889 fn eq(&self, other: &Ipv6Addr) -> bool {
1891 IpAddr::V4(_) => false,
1892 IpAddr::V6(v6) => v6 == other,
1897 #[stable(feature = "rust1", since = "1.0.0")]
1898 impl Eq for Ipv6Addr {}
1900 #[stable(feature = "rust1", since = "1.0.0")]
1901 impl hash::Hash for Ipv6Addr {
1903 fn hash<H: hash::Hasher>(&self, s: &mut H) {
1904 self.inner.s6_addr.hash(s)
1908 #[stable(feature = "rust1", since = "1.0.0")]
1909 impl PartialOrd for Ipv6Addr {
1911 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1912 Some(self.cmp(other))
1916 #[stable(feature = "ip_cmp", since = "1.16.0")]
1917 impl PartialOrd<Ipv6Addr> for IpAddr {
1919 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1921 IpAddr::V4(_) => Some(Ordering::Less),
1922 IpAddr::V6(v6) => v6.partial_cmp(other),
1927 #[stable(feature = "ip_cmp", since = "1.16.0")]
1928 impl PartialOrd<IpAddr> for Ipv6Addr {
1930 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1932 IpAddr::V4(_) => Some(Ordering::Greater),
1933 IpAddr::V6(v6) => self.partial_cmp(v6),
1938 #[stable(feature = "rust1", since = "1.0.0")]
1939 impl Ord for Ipv6Addr {
1941 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1942 self.segments().cmp(&other.segments())
1946 impl AsInner<c::in6_addr> for Ipv6Addr {
1948 fn as_inner(&self) -> &c::in6_addr {
1952 impl FromInner<c::in6_addr> for Ipv6Addr {
1954 fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1955 Ipv6Addr { inner: addr }
1959 #[stable(feature = "i128", since = "1.26.0")]
1960 impl From<Ipv6Addr> for u128 {
1961 /// Convert an `Ipv6Addr` into a host byte order `u128`.
1966 /// use std::net::Ipv6Addr;
1968 /// let addr = Ipv6Addr::new(
1969 /// 0x1020, 0x3040, 0x5060, 0x7080,
1970 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1972 /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1975 fn from(ip: Ipv6Addr) -> u128 {
1976 let ip = ip.octets();
1977 u128::from_be_bytes(ip)
1980 #[stable(feature = "i128", since = "1.26.0")]
1981 impl From<u128> for Ipv6Addr {
1982 /// Convert a host byte order `u128` into an `Ipv6Addr`.
1987 /// use std::net::Ipv6Addr;
1989 /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1992 /// 0x1020, 0x3040, 0x5060, 0x7080,
1993 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1998 fn from(ip: u128) -> Ipv6Addr {
1999 Ipv6Addr::from(ip.to_be_bytes())
2003 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
2004 impl From<[u8; 16]> for Ipv6Addr {
2005 /// Creates an `Ipv6Addr` from a sixteen element byte array.
2010 /// use std::net::Ipv6Addr;
2012 /// let addr = Ipv6Addr::from([
2013 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
2014 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
2027 fn from(octets: [u8; 16]) -> Ipv6Addr {
2028 let inner = c::in6_addr { s6_addr: octets };
2029 Ipv6Addr::from_inner(inner)
2033 #[stable(feature = "ipv6_from_segments", since = "1.16.0")]
2034 impl From<[u16; 8]> for Ipv6Addr {
2035 /// Creates an `Ipv6Addr` from an eight element 16-bit array.
2040 /// use std::net::Ipv6Addr;
2042 /// let addr = Ipv6Addr::from([
2043 /// 525u16, 524u16, 523u16, 522u16,
2044 /// 521u16, 520u16, 519u16, 518u16,
2057 fn from(segments: [u16; 8]) -> Ipv6Addr {
2058 let [a, b, c, d, e, f, g, h] = segments;
2059 Ipv6Addr::new(a, b, c, d, e, f, g, h)
2063 #[stable(feature = "ip_from_slice", since = "1.17.0")]
2064 impl From<[u8; 16]> for IpAddr {
2065 /// Creates an `IpAddr::V6` from a sixteen element byte array.
2070 /// use std::net::{IpAddr, Ipv6Addr};
2072 /// let addr = IpAddr::from([
2073 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
2074 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
2077 /// IpAddr::V6(Ipv6Addr::new(
2087 fn from(octets: [u8; 16]) -> IpAddr {
2088 IpAddr::V6(Ipv6Addr::from(octets))
2092 #[stable(feature = "ip_from_slice", since = "1.17.0")]
2093 impl From<[u16; 8]> for IpAddr {
2094 /// Creates an `IpAddr::V6` from an eight element 16-bit array.
2099 /// use std::net::{IpAddr, Ipv6Addr};
2101 /// let addr = IpAddr::from([
2102 /// 525u16, 524u16, 523u16, 522u16,
2103 /// 521u16, 520u16, 519u16, 518u16,
2106 /// IpAddr::V6(Ipv6Addr::new(
2116 fn from(segments: [u16; 8]) -> IpAddr {
2117 IpAddr::V6(Ipv6Addr::from(segments))