3 reason = "extra functionality has not been \
4 scrutinized to the level that it should \
9 use crate::cmp::Ordering;
10 use crate::fmt::{self, Write as FmtWrite};
12 use crate::io::Write as IoWrite;
13 use crate::sys::net::netc as c;
14 use crate::sys_common::{AsInner, FromInner};
16 /// An IP address, either IPv4 or IPv6.
18 /// This enum can contain either an [`Ipv4Addr`] or an [`Ipv6Addr`], see their
19 /// respective documentation for more details.
21 /// The size of an `IpAddr` instance may vary depending on the target operating
24 /// [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html
25 /// [`Ipv6Addr`]: ../../std/net/struct.Ipv6Addr.html
30 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
32 /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
33 /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
35 /// assert_eq!("127.0.0.1".parse(), Ok(localhost_v4));
36 /// assert_eq!("::1".parse(), Ok(localhost_v6));
38 /// assert_eq!(localhost_v4.is_ipv6(), false);
39 /// assert_eq!(localhost_v4.is_ipv4(), true);
41 #[stable(feature = "ip_addr", since = "1.7.0")]
42 #[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
45 #[stable(feature = "ip_addr", since = "1.7.0")]
46 V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
48 #[stable(feature = "ip_addr", since = "1.7.0")]
49 V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
54 /// IPv4 addresses are defined as 32-bit integers in [IETF RFC 791].
55 /// They are usually represented as four octets.
57 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
59 /// The size of an `Ipv4Addr` struct may vary depending on the target operating
62 /// [IETF RFC 791]: https://tools.ietf.org/html/rfc791
63 /// [`IpAddr`]: ../../std/net/enum.IpAddr.html
65 /// # Textual representation
67 /// `Ipv4Addr` provides a [`FromStr`] implementation. The four octets are in decimal
68 /// notation, divided by `.` (this is called "dot-decimal notation").
70 /// [`FromStr`]: ../../std/str/trait.FromStr.html
75 /// use std::net::Ipv4Addr;
77 /// let localhost = Ipv4Addr::new(127, 0, 0, 1);
78 /// assert_eq!("127.0.0.1".parse(), Ok(localhost));
79 /// assert_eq!(localhost.is_loopback(), true);
82 #[stable(feature = "rust1", since = "1.0.0")]
89 /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291].
90 /// They are usually represented as eight 16-bit segments.
92 /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
94 /// The size of an `Ipv6Addr` struct may vary depending on the target operating
97 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
98 /// [`IpAddr`]: ../../std/net/enum.IpAddr.html
100 /// # Textual representation
102 /// `Ipv6Addr` provides a [`FromStr`] implementation. There are many ways to represent
103 /// an IPv6 address in text, but in general, each segments is written in hexadecimal
104 /// notation, and segments are separated by `:`. For more information, see
107 /// [`FromStr`]: ../../std/str/trait.FromStr.html
108 /// [IETF RFC 5952]: https://tools.ietf.org/html/rfc5952
113 /// use std::net::Ipv6Addr;
115 /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
116 /// assert_eq!("::1".parse(), Ok(localhost));
117 /// assert_eq!(localhost.is_loopback(), true);
120 #[stable(feature = "rust1", since = "1.0.0")]
121 pub struct Ipv6Addr {
125 #[allow(missing_docs)]
126 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
127 pub enum Ipv6MulticastScope {
138 /// Returns [`true`] for the special 'unspecified' address.
140 /// See the documentation for [`Ipv4Addr::is_unspecified`][IPv4] and
141 /// [`Ipv6Addr::is_unspecified`][IPv6] for more details.
143 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
144 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
145 /// [`true`]: ../../std/primitive.bool.html
150 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
152 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
153 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
155 #[stable(feature = "ip_shared", since = "1.12.0")]
156 pub fn is_unspecified(&self) -> bool {
158 IpAddr::V4(ip) => ip.is_unspecified(),
159 IpAddr::V6(ip) => ip.is_unspecified(),
163 /// Returns [`true`] if this is a loopback address.
165 /// See the documentation for [`Ipv4Addr::is_loopback`][IPv4] and
166 /// [`Ipv6Addr::is_loopback`][IPv6] for more details.
168 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
169 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
170 /// [`true`]: ../../std/primitive.bool.html
175 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
177 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
178 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
180 #[stable(feature = "ip_shared", since = "1.12.0")]
181 pub fn is_loopback(&self) -> bool {
183 IpAddr::V4(ip) => ip.is_loopback(),
184 IpAddr::V6(ip) => ip.is_loopback(),
188 /// Returns [`true`] if the address appears to be globally routable.
190 /// See the documentation for [`Ipv4Addr::is_global`][IPv4] and
191 /// [`Ipv6Addr::is_global`][IPv6] for more details.
193 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
194 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
195 /// [`true`]: ../../std/primitive.bool.html
202 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
204 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
205 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true);
207 pub fn is_global(&self) -> bool {
209 IpAddr::V4(ip) => ip.is_global(),
210 IpAddr::V6(ip) => ip.is_global(),
214 /// Returns [`true`] if this is a multicast address.
216 /// See the documentation for [`Ipv4Addr::is_multicast`][IPv4] and
217 /// [`Ipv6Addr::is_multicast`][IPv6] for more details.
219 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
220 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
221 /// [`true`]: ../../std/primitive.bool.html
226 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
228 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
229 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
231 #[stable(feature = "ip_shared", since = "1.12.0")]
232 pub fn is_multicast(&self) -> bool {
234 IpAddr::V4(ip) => ip.is_multicast(),
235 IpAddr::V6(ip) => ip.is_multicast(),
239 /// Returns [`true`] if this address is in a range designated for documentation.
241 /// See the documentation for [`Ipv4Addr::is_documentation`][IPv4] and
242 /// [`Ipv6Addr::is_documentation`][IPv6] for more details.
244 /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
245 /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
246 /// [`true`]: ../../std/primitive.bool.html
253 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
255 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
257 /// IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_documentation(),
261 pub fn is_documentation(&self) -> bool {
263 IpAddr::V4(ip) => ip.is_documentation(),
264 IpAddr::V6(ip) => ip.is_documentation(),
268 /// Returns [`true`] if this address is an [IPv4 address], and [`false`] otherwise.
270 /// [`true`]: ../../std/primitive.bool.html
271 /// [`false`]: ../../std/primitive.bool.html
272 /// [IPv4 address]: #variant.V4
277 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
279 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
280 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false);
282 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
283 pub fn is_ipv4(&self) -> bool {
284 matches!(self, IpAddr::V4(_))
287 /// Returns [`true`] if this address is an [IPv6 address], and [`false`] otherwise.
289 /// [`true`]: ../../std/primitive.bool.html
290 /// [`false`]: ../../std/primitive.bool.html
291 /// [IPv6 address]: #variant.V6
296 /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
298 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
299 /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true);
301 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
302 pub fn is_ipv6(&self) -> bool {
303 matches!(self, IpAddr::V6(_))
308 /// Creates a new IPv4 address from four eight-bit octets.
310 /// The result will represent the IP address `a`.`b`.`c`.`d`.
315 /// use std::net::Ipv4Addr;
317 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
319 #[stable(feature = "rust1", since = "1.0.0")]
320 #[rustc_const_stable(feature = "const_ipv4", since = "1.32.0")]
321 pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
322 // FIXME: should just be u32::from_be_bytes([a, b, c, d]),
323 // once that method is no longer rustc_const_unstable
327 ((a as u32) << 24) | ((b as u32) << 16) | ((c as u32) << 8) | (d as u32),
333 /// An IPv4 address with the address pointing to localhost: 127.0.0.1.
338 /// use std::net::Ipv4Addr;
340 /// let addr = Ipv4Addr::LOCALHOST;
341 /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
343 #[stable(feature = "ip_constructors", since = "1.30.0")]
344 pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
346 /// An IPv4 address representing an unspecified address: 0.0.0.0
351 /// use std::net::Ipv4Addr;
353 /// let addr = Ipv4Addr::UNSPECIFIED;
354 /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
356 #[stable(feature = "ip_constructors", since = "1.30.0")]
357 pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
359 /// An IPv4 address representing the broadcast address: 255.255.255.255
364 /// use std::net::Ipv4Addr;
366 /// let addr = Ipv4Addr::BROADCAST;
367 /// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
369 #[stable(feature = "ip_constructors", since = "1.30.0")]
370 pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
372 /// Returns the four eight-bit integers that make up this address.
377 /// use std::net::Ipv4Addr;
379 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
380 /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
382 #[stable(feature = "rust1", since = "1.0.0")]
383 pub fn octets(&self) -> [u8; 4] {
384 // This returns the order we want because s_addr is stored in big-endian.
385 self.inner.s_addr.to_ne_bytes()
388 /// Returns [`true`] for the special 'unspecified' address (0.0.0.0).
390 /// This property is defined in _UNIX Network Programming, Second Edition_,
391 /// W. Richard Stevens, p. 891; see also [ip7].
393 /// [ip7]: http://man7.org/linux/man-pages/man7/ip.7.html
394 /// [`true`]: ../../std/primitive.bool.html
399 /// use std::net::Ipv4Addr;
401 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
402 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
404 #[stable(feature = "ip_shared", since = "1.12.0")]
405 #[rustc_const_stable(feature = "const_ipv4", since = "1.32.0")]
406 pub const fn is_unspecified(&self) -> bool {
407 self.inner.s_addr == 0
410 /// Returns [`true`] if this is a loopback address (127.0.0.0/8).
412 /// This property is defined by [IETF RFC 1122].
414 /// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122
415 /// [`true`]: ../../std/primitive.bool.html
420 /// use std::net::Ipv4Addr;
422 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
423 /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
425 #[stable(since = "1.7.0", feature = "ip_17")]
426 pub fn is_loopback(&self) -> bool {
427 self.octets()[0] == 127
430 /// Returns [`true`] if this is a private address.
432 /// The private address ranges are defined in [IETF RFC 1918] and include:
438 /// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918
439 /// [`true`]: ../../std/primitive.bool.html
444 /// use std::net::Ipv4Addr;
446 /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
447 /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
448 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
449 /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
450 /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
451 /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
452 /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
454 #[stable(since = "1.7.0", feature = "ip_17")]
455 pub fn is_private(&self) -> bool {
456 match self.octets() {
458 [172, b, ..] if b >= 16 && b <= 31 => true,
459 [192, 168, ..] => true,
464 /// Returns [`true`] if the address is link-local (169.254.0.0/16).
466 /// This property is defined by [IETF RFC 3927].
468 /// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927
469 /// [`true`]: ../../std/primitive.bool.html
474 /// use std::net::Ipv4Addr;
476 /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
477 /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
478 /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
480 #[stable(since = "1.7.0", feature = "ip_17")]
481 pub fn is_link_local(&self) -> bool {
482 match self.octets() {
483 [169, 254, ..] => true,
488 /// Returns [`true`] if the address appears to be globally routable.
489 /// See [iana-ipv4-special-registry][ipv4-sr].
491 /// The following return false:
493 /// - private addresses (see [`is_private()`](#method.is_private))
494 /// - the loopback address (see [`is_loopback()`](#method.is_loopback))
495 /// - the link-local address (see [`is_link_local()`](#method.is_link_local))
496 /// - the broadcast address (see [`is_broadcast()`](#method.is_broadcast))
497 /// - addresses used for documentation (see [`is_documentation()`](#method.is_documentation))
498 /// - the unspecified address (see [`is_unspecified()`](#method.is_unspecified)), and the whole
500 /// - addresses reserved for future protocols (see
501 /// [`is_ietf_protocol_assignment()`](#method.is_ietf_protocol_assignment), except
502 /// `192.0.0.9/32` and `192.0.0.10/32` which are globally routable
503 /// - addresses reserved for future use (see [`is_reserved()`](#method.is_reserved)
504 /// - addresses reserved for networking devices benchmarking (see
505 /// [`is_benchmarking`](#method.is_benchmarking))
507 /// [ipv4-sr]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
508 /// [`true`]: ../../std/primitive.bool.html
515 /// use std::net::Ipv4Addr;
517 /// // private addresses are not global
518 /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
519 /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
520 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
522 /// // the 0.0.0.0/8 block is not global
523 /// assert_eq!(Ipv4Addr::new(0, 1, 2, 3).is_global(), false);
524 /// // in particular, the unspecified address is not global
525 /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
527 /// // the loopback address is not global
528 /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_global(), false);
530 /// // link local addresses are not global
531 /// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false);
533 /// // the broadcast address is not global
534 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_global(), false);
536 /// // the address space designated for documentation is not global
537 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false);
538 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false);
539 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false);
541 /// // shared addresses are not global
542 /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false);
544 /// // addresses reserved for protocol assignment are not global
545 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_global(), false);
546 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_global(), false);
548 /// // addresses reserved for future use are not global
549 /// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false);
551 /// // addresses reserved for network devices benchmarking are not global
552 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false);
554 /// // All the other addresses are global
555 /// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true);
556 /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
558 pub fn is_global(&self) -> bool {
559 // check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
560 // globally routable addresses in the 192.0.0.0/24 range.
561 if u32::from(*self) == 0xc0000009 || u32::from(*self) == 0xc000000a {
565 && !self.is_loopback()
566 && !self.is_link_local()
567 && !self.is_broadcast()
568 && !self.is_documentation()
570 && !self.is_ietf_protocol_assignment()
571 && !self.is_reserved()
572 && !self.is_benchmarking()
573 // Make sure the address is not in 0.0.0.0/8
574 && self.octets()[0] != 0
577 /// Returns [`true`] if this address is part of the Shared Address Space defined in
578 /// [IETF RFC 6598] (`100.64.0.0/10`).
580 /// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598
581 /// [`true`]: ../../std/primitive.bool.html
587 /// use std::net::Ipv4Addr;
589 /// assert_eq!(Ipv4Addr::new(100, 64, 0, 0).is_shared(), true);
590 /// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
591 /// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
593 pub fn is_shared(&self) -> bool {
594 self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
597 /// Returns [`true`] if this address is part of `192.0.0.0/24`, which is reserved to
598 /// IANA for IETF protocol assignments, as documented in [IETF RFC 6890].
600 /// Note that parts of this block are in use:
602 /// - `192.0.0.8/32` is the "IPv4 dummy address" (see [IETF RFC 7600])
603 /// - `192.0.0.9/32` is the "Port Control Protocol Anycast" (see [IETF RFC 7723])
604 /// - `192.0.0.10/32` is used for NAT traversal (see [IETF RFC 8155])
606 /// [IETF RFC 6890]: https://tools.ietf.org/html/rfc6890
607 /// [IETF RFC 7600]: https://tools.ietf.org/html/rfc7600
608 /// [IETF RFC 7723]: https://tools.ietf.org/html/rfc7723
609 /// [IETF RFC 8155]: https://tools.ietf.org/html/rfc8155
610 /// [`true`]: ../../std/primitive.bool.html
616 /// use std::net::Ipv4Addr;
618 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_ietf_protocol_assignment(), true);
619 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 8).is_ietf_protocol_assignment(), true);
620 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 9).is_ietf_protocol_assignment(), true);
621 /// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_ietf_protocol_assignment(), true);
622 /// assert_eq!(Ipv4Addr::new(192, 0, 1, 0).is_ietf_protocol_assignment(), false);
623 /// assert_eq!(Ipv4Addr::new(191, 255, 255, 255).is_ietf_protocol_assignment(), false);
625 pub fn is_ietf_protocol_assignment(&self) -> bool {
626 self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0
629 /// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for
630 /// network devices benchmarking. This range is defined in [IETF RFC 2544] as `192.18.0.0`
631 /// through `198.19.255.255` but [errata 423] corrects it to `198.18.0.0/15`.
633 /// [IETF RFC 2544]: https://tools.ietf.org/html/rfc2544
634 /// [errata 423]: https://www.rfc-editor.org/errata/eid423
635 /// [`true`]: ../../std/primitive.bool.html
641 /// use std::net::Ipv4Addr;
643 /// assert_eq!(Ipv4Addr::new(198, 17, 255, 255).is_benchmarking(), false);
644 /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_benchmarking(), true);
645 /// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true);
646 /// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false);
648 pub fn is_benchmarking(&self) -> bool {
649 self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
652 /// Returns [`true`] if this address is reserved by IANA for future use. [IETF RFC 1112]
653 /// defines the block of reserved addresses as `240.0.0.0/4`. This range normally includes the
654 /// broadcast address `255.255.255.255`, but this implementation explicitly excludes it, since
655 /// it is obviously not reserved for future use.
657 /// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112
658 /// [`true`]: ../../std/primitive.bool.html
662 /// As IANA assigns new addresses, this method will be
663 /// updated. This may result in non-reserved addresses being
664 /// treated as reserved in code that relies on an outdated version
671 /// use std::net::Ipv4Addr;
673 /// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true);
674 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true);
676 /// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false);
677 /// // The broadcast address is not considered as reserved for future use by this implementation
678 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
680 pub fn is_reserved(&self) -> bool {
681 self.octets()[0] & 240 == 240 && !self.is_broadcast()
684 /// Returns [`true`] if this is a multicast address (224.0.0.0/4).
686 /// Multicast addresses have a most significant octet between 224 and 239,
687 /// and is defined by [IETF RFC 5771].
689 /// [IETF RFC 5771]: https://tools.ietf.org/html/rfc5771
690 /// [`true`]: ../../std/primitive.bool.html
695 /// use std::net::Ipv4Addr;
697 /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
698 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
699 /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
701 #[stable(since = "1.7.0", feature = "ip_17")]
702 pub fn is_multicast(&self) -> bool {
703 self.octets()[0] >= 224 && self.octets()[0] <= 239
706 /// Returns [`true`] if this is a broadcast address (255.255.255.255).
708 /// A broadcast address has all octets set to 255 as defined in [IETF RFC 919].
710 /// [IETF RFC 919]: https://tools.ietf.org/html/rfc919
711 /// [`true`]: ../../std/primitive.bool.html
716 /// use std::net::Ipv4Addr;
718 /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
719 /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
721 #[stable(since = "1.7.0", feature = "ip_17")]
722 pub fn is_broadcast(&self) -> bool {
723 self == &Self::BROADCAST
726 /// Returns [`true`] if this address is in a range designated for documentation.
728 /// This is defined in [IETF RFC 5737]:
730 /// - 192.0.2.0/24 (TEST-NET-1)
731 /// - 198.51.100.0/24 (TEST-NET-2)
732 /// - 203.0.113.0/24 (TEST-NET-3)
734 /// [IETF RFC 5737]: https://tools.ietf.org/html/rfc5737
735 /// [`true`]: ../../std/primitive.bool.html
740 /// use std::net::Ipv4Addr;
742 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
743 /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
744 /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
745 /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
747 #[stable(since = "1.7.0", feature = "ip_17")]
748 pub fn is_documentation(&self) -> bool {
749 match self.octets() {
750 [192, 0, 2, _] => true,
751 [198, 51, 100, _] => true,
752 [203, 0, 113, _] => true,
757 /// Converts this address to an IPv4-compatible [IPv6 address].
759 /// a.b.c.d becomes ::a.b.c.d
761 /// [IPv6 address]: ../../std/net/struct.Ipv6Addr.html
766 /// use std::net::{Ipv4Addr, Ipv6Addr};
769 /// Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
770 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 767)
773 #[stable(feature = "rust1", since = "1.0.0")]
774 pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
775 let octets = self.octets();
777 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, octets[0], octets[1], octets[2], octets[3],
781 /// Converts this address to an IPv4-mapped [IPv6 address].
783 /// a.b.c.d becomes ::ffff:a.b.c.d
785 /// [IPv6 address]: ../../std/net/struct.Ipv6Addr.html
790 /// use std::net::{Ipv4Addr, Ipv6Addr};
792 /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
793 /// Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 49152, 767));
795 #[stable(feature = "rust1", since = "1.0.0")]
796 pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
797 let octets = self.octets();
799 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, octets[0], octets[1], octets[2], octets[3],
804 #[stable(feature = "ip_addr", since = "1.7.0")]
805 impl fmt::Display for IpAddr {
806 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
808 IpAddr::V4(ip) => ip.fmt(fmt),
809 IpAddr::V6(ip) => ip.fmt(fmt),
814 #[stable(feature = "ip_addr", since = "1.7.0")]
815 impl fmt::Debug for IpAddr {
816 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
817 fmt::Display::fmt(self, fmt)
821 #[stable(feature = "ip_from_ip", since = "1.16.0")]
822 impl From<Ipv4Addr> for IpAddr {
823 /// Copies this address to a new `IpAddr::V4`.
828 /// use std::net::{IpAddr, Ipv4Addr};
830 /// let addr = Ipv4Addr::new(127, 0, 0, 1);
833 /// IpAddr::V4(addr),
834 /// IpAddr::from(addr)
837 fn from(ipv4: Ipv4Addr) -> IpAddr {
842 #[stable(feature = "ip_from_ip", since = "1.16.0")]
843 impl From<Ipv6Addr> for IpAddr {
844 /// Copies this address to a new `IpAddr::V6`.
849 /// use std::net::{IpAddr, Ipv6Addr};
851 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
854 /// IpAddr::V6(addr),
855 /// IpAddr::from(addr)
858 fn from(ipv6: Ipv6Addr) -> IpAddr {
863 #[stable(feature = "rust1", since = "1.0.0")]
864 impl fmt::Display for Ipv4Addr {
865 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
866 let octets = self.octets();
867 // Fast Path: if there's no alignment stuff, write directly to the buffer
868 if fmt.precision().is_none() && fmt.width().is_none() {
869 write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
871 const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address
872 let mut buf = [0u8; IPV4_BUF_LEN];
873 let mut buf_slice = &mut buf[..];
875 // Note: The call to write should never fail, hence the unwrap
876 write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
877 let len = IPV4_BUF_LEN - buf_slice.len();
879 // This unsafe is OK because we know what is being written to the buffer
880 let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
886 #[stable(feature = "rust1", since = "1.0.0")]
887 impl fmt::Debug for Ipv4Addr {
888 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
889 fmt::Display::fmt(self, fmt)
893 #[stable(feature = "rust1", since = "1.0.0")]
894 impl Clone for Ipv4Addr {
895 fn clone(&self) -> Ipv4Addr {
900 #[stable(feature = "rust1", since = "1.0.0")]
901 impl PartialEq for Ipv4Addr {
902 fn eq(&self, other: &Ipv4Addr) -> bool {
903 self.inner.s_addr == other.inner.s_addr
907 #[stable(feature = "ip_cmp", since = "1.16.0")]
908 impl PartialEq<Ipv4Addr> for IpAddr {
909 fn eq(&self, other: &Ipv4Addr) -> bool {
911 IpAddr::V4(v4) => v4 == other,
912 IpAddr::V6(_) => false,
917 #[stable(feature = "ip_cmp", since = "1.16.0")]
918 impl PartialEq<IpAddr> for Ipv4Addr {
919 fn eq(&self, other: &IpAddr) -> bool {
921 IpAddr::V4(v4) => self == v4,
922 IpAddr::V6(_) => false,
927 #[stable(feature = "rust1", since = "1.0.0")]
928 impl Eq for Ipv4Addr {}
930 #[stable(feature = "rust1", since = "1.0.0")]
931 impl hash::Hash for Ipv4Addr {
932 fn hash<H: hash::Hasher>(&self, s: &mut H) {
933 // `inner` is #[repr(packed)], so we need to copy `s_addr`.
934 { self.inner.s_addr }.hash(s)
938 #[stable(feature = "rust1", since = "1.0.0")]
939 impl PartialOrd for Ipv4Addr {
940 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
941 Some(self.cmp(other))
945 #[stable(feature = "ip_cmp", since = "1.16.0")]
946 impl PartialOrd<Ipv4Addr> for IpAddr {
947 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
949 IpAddr::V4(v4) => v4.partial_cmp(other),
950 IpAddr::V6(_) => Some(Ordering::Greater),
955 #[stable(feature = "ip_cmp", since = "1.16.0")]
956 impl PartialOrd<IpAddr> for Ipv4Addr {
957 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
959 IpAddr::V4(v4) => self.partial_cmp(v4),
960 IpAddr::V6(_) => Some(Ordering::Less),
965 #[stable(feature = "rust1", since = "1.0.0")]
966 impl Ord for Ipv4Addr {
967 fn cmp(&self, other: &Ipv4Addr) -> Ordering {
968 u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
972 impl AsInner<c::in_addr> for Ipv4Addr {
973 fn as_inner(&self) -> &c::in_addr {
977 impl FromInner<c::in_addr> for Ipv4Addr {
978 fn from_inner(addr: c::in_addr) -> Ipv4Addr {
979 Ipv4Addr { inner: addr }
983 #[stable(feature = "ip_u32", since = "1.1.0")]
984 impl From<Ipv4Addr> for u32 {
985 /// Converts an `Ipv4Addr` into a host byte order `u32`.
990 /// use std::net::Ipv4Addr;
992 /// let addr = Ipv4Addr::new(13, 12, 11, 10);
993 /// assert_eq!(0x0d0c0b0au32, u32::from(addr));
995 fn from(ip: Ipv4Addr) -> u32 {
996 let ip = ip.octets();
997 u32::from_be_bytes(ip)
1001 #[stable(feature = "ip_u32", since = "1.1.0")]
1002 impl From<u32> for Ipv4Addr {
1003 /// Converts a host byte order `u32` into an `Ipv4Addr`.
1008 /// use std::net::Ipv4Addr;
1010 /// let addr = Ipv4Addr::from(0x0d0c0b0au32);
1011 /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
1013 fn from(ip: u32) -> Ipv4Addr {
1014 Ipv4Addr::from(ip.to_be_bytes())
1018 #[stable(feature = "from_slice_v4", since = "1.9.0")]
1019 impl From<[u8; 4]> for Ipv4Addr {
1020 /// Creates an `Ipv4Addr` from a four element byte array.
1025 /// use std::net::Ipv4Addr;
1027 /// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
1028 /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
1030 fn from(octets: [u8; 4]) -> Ipv4Addr {
1031 Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
1035 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1036 impl From<[u8; 4]> for IpAddr {
1037 /// Creates an `IpAddr::V4` from a four element byte array.
1042 /// use std::net::{IpAddr, Ipv4Addr};
1044 /// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]);
1045 /// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr);
1047 fn from(octets: [u8; 4]) -> IpAddr {
1048 IpAddr::V4(Ipv4Addr::from(octets))
1053 /// Creates a new IPv6 address from eight 16-bit segments.
1055 /// The result will represent the IP address `a:b:c:d:e:f:g:h`.
1060 /// use std::net::Ipv6Addr;
1062 /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
1064 #[stable(feature = "rust1", since = "1.0.0")]
1065 #[rustc_const_stable(feature = "const_ipv6", since = "1.32.0")]
1066 pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
1068 inner: c::in6_addr {
1091 /// An IPv6 address representing localhost: `::1`.
1096 /// use std::net::Ipv6Addr;
1098 /// let addr = Ipv6Addr::LOCALHOST;
1099 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
1101 #[stable(feature = "ip_constructors", since = "1.30.0")]
1102 pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
1104 /// An IPv6 address representing the unspecified address: `::`
1109 /// use std::net::Ipv6Addr;
1111 /// let addr = Ipv6Addr::UNSPECIFIED;
1112 /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
1114 #[stable(feature = "ip_constructors", since = "1.30.0")]
1115 pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
1117 /// Returns the eight 16-bit segments that make up this address.
1122 /// use std::net::Ipv6Addr;
1124 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
1125 /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
1127 #[stable(feature = "rust1", since = "1.0.0")]
1128 pub fn segments(&self) -> [u16; 8] {
1129 let arr = &self.inner.s6_addr;
1131 u16::from_be_bytes([arr[0], arr[1]]),
1132 u16::from_be_bytes([arr[2], arr[3]]),
1133 u16::from_be_bytes([arr[4], arr[5]]),
1134 u16::from_be_bytes([arr[6], arr[7]]),
1135 u16::from_be_bytes([arr[8], arr[9]]),
1136 u16::from_be_bytes([arr[10], arr[11]]),
1137 u16::from_be_bytes([arr[12], arr[13]]),
1138 u16::from_be_bytes([arr[14], arr[15]]),
1142 /// Returns [`true`] for the special 'unspecified' address (::).
1144 /// This property is defined in [IETF RFC 4291].
1146 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1147 /// [`true`]: ../../std/primitive.bool.html
1152 /// use std::net::Ipv6Addr;
1154 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
1155 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
1157 #[stable(since = "1.7.0", feature = "ip_17")]
1158 pub fn is_unspecified(&self) -> bool {
1159 self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
1162 /// Returns [`true`] if this is a loopback address (::1).
1164 /// This property is defined in [IETF RFC 4291].
1166 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1167 /// [`true`]: ../../std/primitive.bool.html
1172 /// use std::net::Ipv6Addr;
1174 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
1175 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
1177 #[stable(since = "1.7.0", feature = "ip_17")]
1178 pub fn is_loopback(&self) -> bool {
1179 self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
1182 /// Returns [`true`] if the address appears to be globally routable.
1184 /// The following return [`false`]:
1186 /// - the loopback address
1187 /// - link-local and unique local unicast addresses
1188 /// - interface-, link-, realm-, admin- and site-local multicast addresses
1190 /// [`true`]: ../../std/primitive.bool.html
1191 /// [`false`]: ../../std/primitive.bool.html
1198 /// use std::net::Ipv6Addr;
1200 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
1201 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
1202 /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
1204 pub fn is_global(&self) -> bool {
1205 match self.multicast_scope() {
1206 Some(Ipv6MulticastScope::Global) => true,
1207 None => self.is_unicast_global(),
1212 /// Returns [`true`] if this is a unique local address (`fc00::/7`).
1214 /// This property is defined in [IETF RFC 4193].
1216 /// [IETF RFC 4193]: https://tools.ietf.org/html/rfc4193
1217 /// [`true`]: ../../std/primitive.bool.html
1224 /// use std::net::Ipv6Addr;
1226 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
1227 /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
1229 pub fn is_unique_local(&self) -> bool {
1230 (self.segments()[0] & 0xfe00) == 0xfc00
1233 /// Returns [`true`] if the address is a unicast link-local address (`fe80::/64`).
1235 /// A common mis-conception is to think that "unicast link-local addresses start with
1236 /// `fe80::`", but the [IETF RFC 4291] actually defines a stricter format for these addresses:
1240 /// | bits | 54 bits | 64 bits |
1241 /// +----------+-------------------------+----------------------------+
1242 /// |1111111010| 0 | interface ID |
1243 /// +----------+-------------------------+----------------------------+
1246 /// This method validates the format defined in the RFC and won't recognize the following
1247 /// addresses such as `fe80:0:0:1::` or `fe81::` as unicast link-local addresses for example.
1248 /// If you need a less strict validation use [`is_unicast_link_local()`] instead.
1255 /// use std::net::Ipv6Addr;
1257 /// let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0);
1258 /// assert!(ip.is_unicast_link_local_strict());
1260 /// let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0xffff, 0xffff, 0xffff, 0xffff);
1261 /// assert!(ip.is_unicast_link_local_strict());
1263 /// let ip = Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0);
1264 /// assert!(!ip.is_unicast_link_local_strict());
1265 /// assert!(ip.is_unicast_link_local());
1267 /// let ip = Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0);
1268 /// assert!(!ip.is_unicast_link_local_strict());
1269 /// assert!(ip.is_unicast_link_local());
1274 /// - [IETF RFC 4291 section 2.5.6]
1275 /// - [RFC 4291 errata 4406]
1276 /// - [`is_unicast_link_local()`]
1278 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1279 /// [IETF RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6
1280 /// [`true`]: ../../std/primitive.bool.html
1281 /// [RFC 4291 errata 4406]: https://www.rfc-editor.org/errata/eid4406
1282 /// [`is_unicast_link_local()`]: ../../std/net/struct.Ipv6Addr.html#method.is_unicast_link_local
1284 pub fn is_unicast_link_local_strict(&self) -> bool {
1285 (self.segments()[0] & 0xffff) == 0xfe80
1286 && (self.segments()[1] & 0xffff) == 0
1287 && (self.segments()[2] & 0xffff) == 0
1288 && (self.segments()[3] & 0xffff) == 0
1291 /// Returns [`true`] if the address is a unicast link-local address (`fe80::/10`).
1293 /// This method returns [`true`] for addresses in the range reserved by [RFC 4291 section 2.4],
1294 /// i.e. addresses with the following format:
1298 /// | bits | 54 bits | 64 bits |
1299 /// +----------+-------------------------+----------------------------+
1300 /// |1111111010| arbitratry value | interface ID |
1301 /// +----------+-------------------------+----------------------------+
1304 /// As a result, this method consider addresses such as `fe80:0:0:1::` or `fe81::` to be
1305 /// unicast link-local addresses, whereas [`is_unicast_link_local_strict()`] does not. If you
1306 /// need a strict validation fully compliant with the RFC, use
1307 /// [`is_unicast_link_local_strict()`].
1314 /// use std::net::Ipv6Addr;
1316 /// let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0);
1317 /// assert!(ip.is_unicast_link_local());
1319 /// let ip = Ipv6Addr::new(0xfe80, 0, 0, 0, 0xffff, 0xffff, 0xffff, 0xffff);
1320 /// assert!(ip.is_unicast_link_local());
1322 /// let ip = Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0);
1323 /// assert!(ip.is_unicast_link_local());
1324 /// assert!(!ip.is_unicast_link_local_strict());
1326 /// let ip = Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0);
1327 /// assert!(ip.is_unicast_link_local());
1328 /// assert!(!ip.is_unicast_link_local_strict());
1333 /// - [IETF RFC 4291 section 2.4]
1334 /// - [RFC 4291 errata 4406]
1336 /// [IETF RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
1337 /// [`true`]: ../../std/primitive.bool.html
1338 /// [RFC 4291 errata 4406]: https://www.rfc-editor.org/errata/eid4406
1339 /// [`is_unicast_link_local_strict()`]: ../../std/net/struct.Ipv6Addr.html#method.is_unicast_link_local_strict
1341 pub fn is_unicast_link_local(&self) -> bool {
1342 (self.segments()[0] & 0xffc0) == 0xfe80
1345 /// Returns [`true`] if this is a deprecated unicast site-local address (fec0::/10). The
1346 /// unicast site-local address format is defined in [RFC 4291 section 2.5.7] as:
1350 /// | bits | 54 bits | 64 bits |
1351 /// +----------+-------------------------+----------------------------+
1352 /// |1111111011| subnet ID | interface ID |
1353 /// +----------+-------------------------+----------------------------+
1356 /// [`true`]: ../../std/primitive.bool.html
1357 /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
1364 /// use std::net::Ipv6Addr;
1367 /// Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_site_local(),
1370 /// assert_eq!(Ipv6Addr::new(0xfec2, 0, 0, 0, 0, 0, 0, 0).is_unicast_site_local(), true);
1375 /// As per [RFC 3879], the whole `FEC0::/10` prefix is
1376 /// deprecated. New software must not support site-local
1379 /// [RFC 3879]: https://tools.ietf.org/html/rfc3879
1380 pub fn is_unicast_site_local(&self) -> bool {
1381 (self.segments()[0] & 0xffc0) == 0xfec0
1384 /// Returns [`true`] if this is an address reserved for documentation
1385 /// (2001:db8::/32).
1387 /// This property is defined in [IETF RFC 3849].
1389 /// [IETF RFC 3849]: https://tools.ietf.org/html/rfc3849
1390 /// [`true`]: ../../std/primitive.bool.html
1397 /// use std::net::Ipv6Addr;
1399 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
1400 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
1402 pub fn is_documentation(&self) -> bool {
1403 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
1406 /// Returns [`true`] if the address is a globally routable unicast address.
1408 /// The following return false:
1410 /// - the loopback address
1411 /// - the link-local addresses
1412 /// - unique local addresses
1413 /// - the unspecified address
1414 /// - the address range reserved for documentation
1416 /// This method returns [`true`] for site-local addresses as per [RFC 4291 section 2.5.7]
1419 /// The special behavior of [the site-local unicast] prefix defined in [RFC3513] must no longer
1420 /// be supported in new implementations (i.e., new implementations must treat this prefix as
1421 /// Global Unicast).
1424 /// [`true`]: ../../std/primitive.bool.html
1425 /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
1432 /// use std::net::Ipv6Addr;
1434 /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
1435 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
1437 pub fn is_unicast_global(&self) -> bool {
1438 !self.is_multicast()
1439 && !self.is_loopback()
1440 && !self.is_unicast_link_local()
1441 && !self.is_unique_local()
1442 && !self.is_unspecified()
1443 && !self.is_documentation()
1446 /// Returns the address's multicast scope if the address is multicast.
1453 /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
1456 /// Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
1457 /// Some(Ipv6MulticastScope::Global)
1459 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
1461 pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
1462 if self.is_multicast() {
1463 match self.segments()[0] & 0x000f {
1464 1 => Some(Ipv6MulticastScope::InterfaceLocal),
1465 2 => Some(Ipv6MulticastScope::LinkLocal),
1466 3 => Some(Ipv6MulticastScope::RealmLocal),
1467 4 => Some(Ipv6MulticastScope::AdminLocal),
1468 5 => Some(Ipv6MulticastScope::SiteLocal),
1469 8 => Some(Ipv6MulticastScope::OrganizationLocal),
1470 14 => Some(Ipv6MulticastScope::Global),
1478 /// Returns [`true`] if this is a multicast address (ff00::/8).
1480 /// This property is defined by [IETF RFC 4291].
1482 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1483 /// [`true`]: ../../std/primitive.bool.html
1488 /// use std::net::Ipv6Addr;
1490 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
1491 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
1493 #[stable(since = "1.7.0", feature = "ip_17")]
1494 pub fn is_multicast(&self) -> bool {
1495 (self.segments()[0] & 0xff00) == 0xff00
1498 /// Converts this address to an [IPv4 address]. Returns [`None`] if this address is
1499 /// neither IPv4-compatible or IPv4-mapped.
1501 /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
1503 /// [IPv4 address]: ../../std/net/struct.Ipv4Addr.html
1504 /// [`None`]: ../../std/option/enum.Option.html#variant.None
1509 /// use std::net::{Ipv4Addr, Ipv6Addr};
1511 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
1512 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
1513 /// Some(Ipv4Addr::new(192, 10, 2, 255)));
1514 /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
1515 /// Some(Ipv4Addr::new(0, 0, 0, 1)));
1517 #[stable(feature = "rust1", since = "1.0.0")]
1518 pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
1519 match self.segments() {
1520 [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
1521 Some(Ipv4Addr::new((g >> 8) as u8, g as u8, (h >> 8) as u8, h as u8))
1527 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
1530 /// use std::net::Ipv6Addr;
1532 /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
1533 /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1535 #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
1536 #[rustc_const_stable(feature = "const_ipv6", since = "1.32.0")]
1537 pub const fn octets(&self) -> [u8; 16] {
1542 /// Write an Ipv6Addr, conforming to the canonical style described by
1543 /// [RFC 5952](https://tools.ietf.org/html/rfc5952).
1544 #[stable(feature = "rust1", since = "1.0.0")]
1545 impl fmt::Display for Ipv6Addr {
1546 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1547 // If there are no alignment requirements, write out the IP address to
1548 // f. Otherwise, write it to a local buffer, then use f.pad.
1549 if f.precision().is_none() && f.width().is_none() {
1550 let segments = self.segments();
1552 // Special case for :: and ::1; otherwise they get written with the
1554 if self.is_unspecified() {
1556 } else if self.is_loopback() {
1558 } else if let Some(ipv4) = self.to_ipv4() {
1560 // IPv4 Compatible address
1561 0 => write!(f, "::{}", ipv4),
1562 // IPv4 Mapped address
1563 0xffff => write!(f, "::ffff:{}", ipv4),
1564 _ => unreachable!(),
1567 #[derive(Copy, Clone, Default)]
1573 // Find the inner 0 span
1575 let mut longest = Span::default();
1576 let mut current = Span::default();
1578 for (i, &segment) in segments.iter().enumerate() {
1580 if current.len == 0 {
1586 if current.len > longest.len {
1590 current = Span::default();
1597 /// Write a colon-separated part of the address
1599 fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result {
1600 if let Some(first) = chunk.first() {
1601 fmt::LowerHex::fmt(first, f)?;
1602 for segment in &chunk[1..] {
1604 fmt::LowerHex::fmt(segment, f)?;
1611 fmt_subslice(f, &segments[..zeroes.start])?;
1613 fmt_subslice(f, &segments[zeroes.start + zeroes.len..])
1615 fmt_subslice(f, &segments)
1619 // Slow path: write the address to a local buffer, the use f.pad.
1620 // Defined recursively by using the fast path to write to the
1623 // This is the largest possible size of an IPv6 address
1624 const IPV6_BUF_LEN: usize = (4 * 8) + 7;
1625 let mut buf = [0u8; IPV6_BUF_LEN];
1626 let mut buf_slice = &mut buf[..];
1628 // Note: This call to write should never fail, so unwrap is okay.
1629 write!(buf_slice, "{}", self).unwrap();
1630 let len = IPV6_BUF_LEN - buf_slice.len();
1632 // This is safe because we know exactly what can be in this buffer
1633 let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
1639 #[stable(feature = "rust1", since = "1.0.0")]
1640 impl fmt::Debug for Ipv6Addr {
1641 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1642 fmt::Display::fmt(self, fmt)
1646 #[stable(feature = "rust1", since = "1.0.0")]
1647 impl Clone for Ipv6Addr {
1648 fn clone(&self) -> Ipv6Addr {
1653 #[stable(feature = "rust1", since = "1.0.0")]
1654 impl PartialEq for Ipv6Addr {
1655 fn eq(&self, other: &Ipv6Addr) -> bool {
1656 self.inner.s6_addr == other.inner.s6_addr
1660 #[stable(feature = "ip_cmp", since = "1.16.0")]
1661 impl PartialEq<IpAddr> for Ipv6Addr {
1662 fn eq(&self, other: &IpAddr) -> bool {
1664 IpAddr::V4(_) => false,
1665 IpAddr::V6(v6) => self == v6,
1670 #[stable(feature = "ip_cmp", since = "1.16.0")]
1671 impl PartialEq<Ipv6Addr> for IpAddr {
1672 fn eq(&self, other: &Ipv6Addr) -> bool {
1674 IpAddr::V4(_) => false,
1675 IpAddr::V6(v6) => v6 == other,
1680 #[stable(feature = "rust1", since = "1.0.0")]
1681 impl Eq for Ipv6Addr {}
1683 #[stable(feature = "rust1", since = "1.0.0")]
1684 impl hash::Hash for Ipv6Addr {
1685 fn hash<H: hash::Hasher>(&self, s: &mut H) {
1686 self.inner.s6_addr.hash(s)
1690 #[stable(feature = "rust1", since = "1.0.0")]
1691 impl PartialOrd for Ipv6Addr {
1692 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1693 Some(self.cmp(other))
1697 #[stable(feature = "ip_cmp", since = "1.16.0")]
1698 impl PartialOrd<Ipv6Addr> for IpAddr {
1699 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
1701 IpAddr::V4(_) => Some(Ordering::Less),
1702 IpAddr::V6(v6) => v6.partial_cmp(other),
1707 #[stable(feature = "ip_cmp", since = "1.16.0")]
1708 impl PartialOrd<IpAddr> for Ipv6Addr {
1709 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1711 IpAddr::V4(_) => Some(Ordering::Greater),
1712 IpAddr::V6(v6) => self.partial_cmp(v6),
1717 #[stable(feature = "rust1", since = "1.0.0")]
1718 impl Ord for Ipv6Addr {
1719 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
1720 self.segments().cmp(&other.segments())
1724 impl AsInner<c::in6_addr> for Ipv6Addr {
1725 fn as_inner(&self) -> &c::in6_addr {
1729 impl FromInner<c::in6_addr> for Ipv6Addr {
1730 fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
1731 Ipv6Addr { inner: addr }
1735 #[stable(feature = "i128", since = "1.26.0")]
1736 impl From<Ipv6Addr> for u128 {
1737 /// Convert an `Ipv6Addr` into a host byte order `u128`.
1742 /// use std::net::Ipv6Addr;
1744 /// let addr = Ipv6Addr::new(
1745 /// 0x1020, 0x3040, 0x5060, 0x7080,
1746 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1748 /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
1750 fn from(ip: Ipv6Addr) -> u128 {
1751 let ip = ip.octets();
1752 u128::from_be_bytes(ip)
1755 #[stable(feature = "i128", since = "1.26.0")]
1756 impl From<u128> for Ipv6Addr {
1757 /// Convert a host byte order `u128` into an `Ipv6Addr`.
1762 /// use std::net::Ipv6Addr;
1764 /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
1767 /// 0x1020, 0x3040, 0x5060, 0x7080,
1768 /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
1772 fn from(ip: u128) -> Ipv6Addr {
1773 Ipv6Addr::from(ip.to_be_bytes())
1777 #[stable(feature = "ipv6_from_octets", since = "1.9.0")]
1778 impl From<[u8; 16]> for Ipv6Addr {
1779 /// Creates an `Ipv6Addr` from a sixteen element byte array.
1784 /// use std::net::Ipv6Addr;
1786 /// let addr = Ipv6Addr::from([
1787 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
1788 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
1800 fn from(octets: [u8; 16]) -> Ipv6Addr {
1801 let inner = c::in6_addr { s6_addr: octets };
1802 Ipv6Addr::from_inner(inner)
1806 #[stable(feature = "ipv6_from_segments", since = "1.16.0")]
1807 impl From<[u16; 8]> for Ipv6Addr {
1808 /// Creates an `Ipv6Addr` from an eight element 16-bit array.
1813 /// use std::net::Ipv6Addr;
1815 /// let addr = Ipv6Addr::from([
1816 /// 525u16, 524u16, 523u16, 522u16,
1817 /// 521u16, 520u16, 519u16, 518u16,
1829 fn from(segments: [u16; 8]) -> Ipv6Addr {
1830 let [a, b, c, d, e, f, g, h] = segments;
1831 Ipv6Addr::new(a, b, c, d, e, f, g, h)
1835 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1836 impl From<[u8; 16]> for IpAddr {
1837 /// Creates an `IpAddr::V6` from a sixteen element byte array.
1842 /// use std::net::{IpAddr, Ipv6Addr};
1844 /// let addr = IpAddr::from([
1845 /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
1846 /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
1849 /// IpAddr::V6(Ipv6Addr::new(
1858 fn from(octets: [u8; 16]) -> IpAddr {
1859 IpAddr::V6(Ipv6Addr::from(octets))
1863 #[stable(feature = "ip_from_slice", since = "1.17.0")]
1864 impl From<[u16; 8]> for IpAddr {
1865 /// Creates an `IpAddr::V6` from an eight element 16-bit array.
1870 /// use std::net::{IpAddr, Ipv6Addr};
1872 /// let addr = IpAddr::from([
1873 /// 525u16, 524u16, 523u16, 522u16,
1874 /// 521u16, 520u16, 519u16, 518u16,
1877 /// IpAddr::V6(Ipv6Addr::new(
1886 fn from(segments: [u16; 8]) -> IpAddr {
1887 IpAddr::V6(Ipv6Addr::from(segments))
1891 // Tests for this module
1892 #[cfg(all(test, not(target_os = "emscripten")))]
1894 use crate::net::test::{sa4, sa6, tsa};
1896 use crate::str::FromStr;
1899 fn test_from_str_ipv4() {
1900 assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
1901 assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
1902 assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
1905 let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
1906 assert_eq!(None, none);
1908 let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
1909 assert_eq!(None, none);
1911 let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok();
1912 assert_eq!(None, none);
1913 // no number between dots
1914 let none: Option<Ipv4Addr> = "255.0..1".parse().ok();
1915 assert_eq!(None, none);
1919 fn test_from_str_ipv6() {
1920 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
1921 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
1923 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
1924 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
1927 Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
1928 "2a02:6b8::11:11".parse()
1932 let none: Option<Ipv6Addr> = "::00000".parse().ok();
1933 assert_eq!(None, none);
1935 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
1936 assert_eq!(None, none);
1938 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
1939 assert_eq!(None, none);
1941 let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
1942 assert_eq!(None, none);
1943 // two double colons
1944 let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok();
1945 assert_eq!(None, none);
1946 // `::` indicating zero groups of zeros
1947 let none: Option<Ipv6Addr> = "1:2:3:4::5:6:7:8".parse().ok();
1948 assert_eq!(None, none);
1952 fn test_from_str_ipv4_in_ipv6() {
1953 assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)), "::192.0.2.33".parse());
1955 Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)),
1956 "::FFFF:192.0.2.33".parse()
1959 Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
1960 "64:ff9b::192.0.2.33".parse()
1963 Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
1964 "2001:db8:122:c000:2:2100:192.0.2.33".parse()
1968 let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
1969 assert_eq!(None, none);
1970 // not enough groups
1971 let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok();
1972 assert_eq!(None, none);
1974 let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
1975 assert_eq!(None, none);
1979 fn test_from_str_socket_addr() {
1980 assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)), "77.88.21.11:80".parse());
1982 Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)),
1983 "77.88.21.11:80".parse()
1986 Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
1987 "[2a02:6b8:0:1::1]:53".parse()
1990 Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53, 0, 0)),
1991 "[2a02:6b8:0:1::1]:53".parse()
1994 Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
1995 "[::127.0.0.1]:22".parse()
1998 Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22, 0, 0)),
1999 "[::127.0.0.1]:22".parse()
2003 let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
2004 assert_eq!(None, none);
2006 let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
2007 assert_eq!(None, none);
2008 // wrong brackets around v4
2009 let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
2010 assert_eq!(None, none);
2011 // port out of range
2012 let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
2013 assert_eq!(None, none);
2017 fn ipv4_addr_to_string() {
2019 assert_eq!(Ipv4Addr::new(1, 1, 1, 1).to_string(), "1.1.1.1");
2021 assert_eq!(Ipv4Addr::new(127, 127, 127, 127).to_string(), "127.127.127.127");
2024 assert_eq!(&format!("{:16}", Ipv4Addr::new(1, 1, 1, 1)), "1.1.1.1 ");
2025 assert_eq!(&format!("{:>16}", Ipv4Addr::new(1, 1, 1, 1)), " 1.1.1.1");
2029 fn ipv6_addr_to_string() {
2030 // ipv4-mapped address
2031 let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
2032 assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
2034 // ipv4-compatible address
2035 let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
2036 assert_eq!(a1.to_string(), "::192.0.2.128");
2038 // v6 address with no zero segments
2039 assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(), "8:9:a:b:c:d:e:f");
2041 // longest possible IPv6 length
2043 Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888)
2045 "1111:2222:3333:4444:5555:6666:7777:8888"
2049 &format!("{:20}", Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8)),
2053 &format!("{:>20}", Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8)),
2057 // reduce a single run of zeros
2060 Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string()
2063 // don't reduce just a single zero segment
2064 assert_eq!("1:2:3:4:5:6:0:8", Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
2067 assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
2070 assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
2073 assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
2075 // two runs of zeros, second one is longer
2076 assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
2078 // two runs of zeros, equal length
2079 assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
2085 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
2086 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped()
2089 Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
2090 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible()
2097 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
2098 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
2101 Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
2102 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
2104 assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(), None);
2108 fn ip_properties() {
2111 IpAddr::from_str($s).unwrap()
2115 macro_rules! check {
2120 ($s:expr, $mask:expr) => {{
2121 let unspec: u8 = 1 << 0;
2122 let loopback: u8 = 1 << 1;
2123 let global: u8 = 1 << 2;
2124 let multicast: u8 = 1 << 3;
2125 let doc: u8 = 1 << 4;
2127 if ($mask & unspec) == unspec {
2128 assert!(ip!($s).is_unspecified());
2130 assert!(!ip!($s).is_unspecified());
2133 if ($mask & loopback) == loopback {
2134 assert!(ip!($s).is_loopback());
2136 assert!(!ip!($s).is_loopback());
2139 if ($mask & global) == global {
2140 assert!(ip!($s).is_global());
2142 assert!(!ip!($s).is_global());
2145 if ($mask & multicast) == multicast {
2146 assert!(ip!($s).is_multicast());
2148 assert!(!ip!($s).is_multicast());
2151 if ($mask & doc) == doc {
2152 assert!(ip!($s).is_documentation());
2154 assert!(!ip!($s).is_documentation());
2159 let unspec: u8 = 1 << 0;
2160 let loopback: u8 = 1 << 1;
2161 let global: u8 = 1 << 2;
2162 let multicast: u8 = 1 << 3;
2163 let doc: u8 = 1 << 4;
2165 check!("0.0.0.0", unspec);
2169 check!("127.1.2.3", loopback);
2170 check!("172.31.254.253");
2171 check!("169.254.253.242");
2172 check!("192.0.2.183", doc);
2173 check!("192.1.2.183", global);
2174 check!("192.168.254.253");
2175 check!("198.51.100.0", doc);
2176 check!("203.0.113.0", doc);
2177 check!("203.2.113.0", global);
2178 check!("224.0.0.0", global | multicast);
2179 check!("239.255.255.255", global | multicast);
2180 check!("255.255.255.255");
2181 // make sure benchmarking addresses are not global
2182 check!("198.18.0.0");
2183 check!("198.18.54.2");
2184 check!("198.19.255.255");
2185 // make sure addresses reserved for protocol assignment are not global
2186 check!("192.0.0.0");
2187 check!("192.0.0.255");
2188 check!("192.0.0.100");
2189 // make sure reserved addresses are not global
2190 check!("240.0.0.0");
2191 check!("251.54.1.76");
2192 check!("254.255.255.255");
2193 // make sure shared addresses are not global
2194 check!("100.64.0.0");
2195 check!("100.127.255.255");
2196 check!("100.100.100.0");
2198 check!("::", unspec);
2199 check!("::1", loopback);
2200 check!("::0.0.0.2", global);
2201 check!("1::", global);
2203 check!("fdff:ffff::");
2204 check!("fe80:ffff::");
2205 check!("febf:ffff::");
2206 check!("fec0::", global);
2207 check!("ff01::", multicast);
2208 check!("ff02::", multicast);
2209 check!("ff03::", multicast);
2210 check!("ff04::", multicast);
2211 check!("ff05::", multicast);
2212 check!("ff08::", multicast);
2213 check!("ff0e::", global | multicast);
2214 check!("2001:db8:85a3::8a2e:370:7334", doc);
2215 check!("102:304:506:708:90a:b0c:d0e:f10", global);
2219 fn ipv4_properties() {
2222 Ipv4Addr::from_str($s).unwrap()
2226 macro_rules! check {
2231 ($s:expr, $mask:expr) => {{
2232 let unspec: u16 = 1 << 0;
2233 let loopback: u16 = 1 << 1;
2234 let private: u16 = 1 << 2;
2235 let link_local: u16 = 1 << 3;
2236 let global: u16 = 1 << 4;
2237 let multicast: u16 = 1 << 5;
2238 let broadcast: u16 = 1 << 6;
2239 let documentation: u16 = 1 << 7;
2240 let benchmarking: u16 = 1 << 8;
2241 let ietf_protocol_assignment: u16 = 1 << 9;
2242 let reserved: u16 = 1 << 10;
2243 let shared: u16 = 1 << 11;
2245 if ($mask & unspec) == unspec {
2246 assert!(ip!($s).is_unspecified());
2248 assert!(!ip!($s).is_unspecified());
2251 if ($mask & loopback) == loopback {
2252 assert!(ip!($s).is_loopback());
2254 assert!(!ip!($s).is_loopback());
2257 if ($mask & private) == private {
2258 assert!(ip!($s).is_private());
2260 assert!(!ip!($s).is_private());
2263 if ($mask & link_local) == link_local {
2264 assert!(ip!($s).is_link_local());
2266 assert!(!ip!($s).is_link_local());
2269 if ($mask & global) == global {
2270 assert!(ip!($s).is_global());
2272 assert!(!ip!($s).is_global());
2275 if ($mask & multicast) == multicast {
2276 assert!(ip!($s).is_multicast());
2278 assert!(!ip!($s).is_multicast());
2281 if ($mask & broadcast) == broadcast {
2282 assert!(ip!($s).is_broadcast());
2284 assert!(!ip!($s).is_broadcast());
2287 if ($mask & documentation) == documentation {
2288 assert!(ip!($s).is_documentation());
2290 assert!(!ip!($s).is_documentation());
2293 if ($mask & benchmarking) == benchmarking {
2294 assert!(ip!($s).is_benchmarking());
2296 assert!(!ip!($s).is_benchmarking());
2299 if ($mask & ietf_protocol_assignment) == ietf_protocol_assignment {
2300 assert!(ip!($s).is_ietf_protocol_assignment());
2302 assert!(!ip!($s).is_ietf_protocol_assignment());
2305 if ($mask & reserved) == reserved {
2306 assert!(ip!($s).is_reserved());
2308 assert!(!ip!($s).is_reserved());
2311 if ($mask & shared) == shared {
2312 assert!(ip!($s).is_shared());
2314 assert!(!ip!($s).is_shared());
2319 let unspec: u16 = 1 << 0;
2320 let loopback: u16 = 1 << 1;
2321 let private: u16 = 1 << 2;
2322 let link_local: u16 = 1 << 3;
2323 let global: u16 = 1 << 4;
2324 let multicast: u16 = 1 << 5;
2325 let broadcast: u16 = 1 << 6;
2326 let documentation: u16 = 1 << 7;
2327 let benchmarking: u16 = 1 << 8;
2328 let ietf_protocol_assignment: u16 = 1 << 9;
2329 let reserved: u16 = 1 << 10;
2330 let shared: u16 = 1 << 11;
2332 check!("0.0.0.0", unspec);
2335 check!("10.9.8.7", private);
2336 check!("127.1.2.3", loopback);
2337 check!("172.31.254.253", private);
2338 check!("169.254.253.242", link_local);
2339 check!("192.0.2.183", documentation);
2340 check!("192.1.2.183", global);
2341 check!("192.168.254.253", private);
2342 check!("198.51.100.0", documentation);
2343 check!("203.0.113.0", documentation);
2344 check!("203.2.113.0", global);
2345 check!("224.0.0.0", global | multicast);
2346 check!("239.255.255.255", global | multicast);
2347 check!("255.255.255.255", broadcast);
2348 check!("198.18.0.0", benchmarking);
2349 check!("198.18.54.2", benchmarking);
2350 check!("198.19.255.255", benchmarking);
2351 check!("192.0.0.0", ietf_protocol_assignment);
2352 check!("192.0.0.255", ietf_protocol_assignment);
2353 check!("192.0.0.100", ietf_protocol_assignment);
2354 check!("240.0.0.0", reserved);
2355 check!("251.54.1.76", reserved);
2356 check!("254.255.255.255", reserved);
2357 check!("100.64.0.0", shared);
2358 check!("100.127.255.255", shared);
2359 check!("100.100.100.0", shared);
2363 fn ipv6_properties() {
2366 Ipv6Addr::from_str($s).unwrap()
2370 macro_rules! check {
2371 ($s:expr, &[$($octet:expr),*], $mask:expr) => {
2372 assert_eq!($s, ip!($s).to_string());
2373 let octets = &[$($octet),*];
2374 assert_eq!(&ip!($s).octets(), octets);
2375 assert_eq!(Ipv6Addr::from(*octets), ip!($s));
2377 let unspecified: u16 = 1 << 0;
2378 let loopback: u16 = 1 << 1;
2379 let unique_local: u16 = 1 << 2;
2380 let global: u16 = 1 << 3;
2381 let unicast_link_local: u16 = 1 << 4;
2382 let unicast_link_local_strict: u16 = 1 << 5;
2383 let unicast_site_local: u16 = 1 << 6;
2384 let unicast_global: u16 = 1 << 7;
2385 let documentation: u16 = 1 << 8;
2386 let multicast_interface_local: u16 = 1 << 9;
2387 let multicast_link_local: u16 = 1 << 10;
2388 let multicast_realm_local: u16 = 1 << 11;
2389 let multicast_admin_local: u16 = 1 << 12;
2390 let multicast_site_local: u16 = 1 << 13;
2391 let multicast_organization_local: u16 = 1 << 14;
2392 let multicast_global: u16 = 1 << 15;
2393 let multicast: u16 = multicast_interface_local
2394 | multicast_admin_local
2396 | multicast_link_local
2397 | multicast_realm_local
2398 | multicast_site_local
2399 | multicast_organization_local;
2401 if ($mask & unspecified) == unspecified {
2402 assert!(ip!($s).is_unspecified());
2404 assert!(!ip!($s).is_unspecified());
2406 if ($mask & loopback) == loopback {
2407 assert!(ip!($s).is_loopback());
2409 assert!(!ip!($s).is_loopback());
2411 if ($mask & unique_local) == unique_local {
2412 assert!(ip!($s).is_unique_local());
2414 assert!(!ip!($s).is_unique_local());
2416 if ($mask & global) == global {
2417 assert!(ip!($s).is_global());
2419 assert!(!ip!($s).is_global());
2421 if ($mask & unicast_link_local) == unicast_link_local {
2422 assert!(ip!($s).is_unicast_link_local());
2424 assert!(!ip!($s).is_unicast_link_local());
2426 if ($mask & unicast_link_local_strict) == unicast_link_local_strict {
2427 assert!(ip!($s).is_unicast_link_local_strict());
2429 assert!(!ip!($s).is_unicast_link_local_strict());
2431 if ($mask & unicast_site_local) == unicast_site_local {
2432 assert!(ip!($s).is_unicast_site_local());
2434 assert!(!ip!($s).is_unicast_site_local());
2436 if ($mask & unicast_global) == unicast_global {
2437 assert!(ip!($s).is_unicast_global());
2439 assert!(!ip!($s).is_unicast_global());
2441 if ($mask & documentation) == documentation {
2442 assert!(ip!($s).is_documentation());
2444 assert!(!ip!($s).is_documentation());
2446 if ($mask & multicast) != 0 {
2447 assert!(ip!($s).multicast_scope().is_some());
2448 assert!(ip!($s).is_multicast());
2450 assert!(ip!($s).multicast_scope().is_none());
2451 assert!(!ip!($s).is_multicast());
2453 if ($mask & multicast_interface_local) == multicast_interface_local {
2454 assert_eq!(ip!($s).multicast_scope().unwrap(),
2455 Ipv6MulticastScope::InterfaceLocal);
2457 if ($mask & multicast_link_local) == multicast_link_local {
2458 assert_eq!(ip!($s).multicast_scope().unwrap(),
2459 Ipv6MulticastScope::LinkLocal);
2461 if ($mask & multicast_realm_local) == multicast_realm_local {
2462 assert_eq!(ip!($s).multicast_scope().unwrap(),
2463 Ipv6MulticastScope::RealmLocal);
2465 if ($mask & multicast_admin_local) == multicast_admin_local {
2466 assert_eq!(ip!($s).multicast_scope().unwrap(),
2467 Ipv6MulticastScope::AdminLocal);
2469 if ($mask & multicast_site_local) == multicast_site_local {
2470 assert_eq!(ip!($s).multicast_scope().unwrap(),
2471 Ipv6MulticastScope::SiteLocal);
2473 if ($mask & multicast_organization_local) == multicast_organization_local {
2474 assert_eq!(ip!($s).multicast_scope().unwrap(),
2475 Ipv6MulticastScope::OrganizationLocal);
2477 if ($mask & multicast_global) == multicast_global {
2478 assert_eq!(ip!($s).multicast_scope().unwrap(),
2479 Ipv6MulticastScope::Global);
2484 let unspecified: u16 = 1 << 0;
2485 let loopback: u16 = 1 << 1;
2486 let unique_local: u16 = 1 << 2;
2487 let global: u16 = 1 << 3;
2488 let unicast_link_local: u16 = 1 << 4;
2489 let unicast_link_local_strict: u16 = 1 << 5;
2490 let unicast_site_local: u16 = 1 << 6;
2491 let unicast_global: u16 = 1 << 7;
2492 let documentation: u16 = 1 << 8;
2493 let multicast_interface_local: u16 = 1 << 9;
2494 let multicast_link_local: u16 = 1 << 10;
2495 let multicast_realm_local: u16 = 1 << 11;
2496 let multicast_admin_local: u16 = 1 << 12;
2497 let multicast_site_local: u16 = 1 << 13;
2498 let multicast_organization_local: u16 = 1 << 14;
2499 let multicast_global: u16 = 1 << 15;
2501 check!("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unspecified);
2503 check!("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], loopback);
2507 &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
2508 global | unicast_global
2511 check!("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);
2513 check!("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unique_local);
2517 &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2523 &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2529 &[0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2530 unicast_link_local | unicast_link_local_strict
2535 &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2541 &[0xfe, 0xbf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2546 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
2548 0xfe, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2555 "fe80::ffff:ffff:ffff:ffff",
2557 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2560 unicast_link_local | unicast_link_local_strict
2565 &[0xfe, 0x80, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
2571 &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2572 unicast_site_local | unicast_global | global
2577 &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2578 multicast_interface_local
2583 &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2584 multicast_link_local
2589 &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2590 multicast_realm_local
2595 &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2596 multicast_admin_local
2601 &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2602 multicast_site_local
2607 &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2608 multicast_organization_local
2613 &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2614 multicast_global | global
2618 "2001:db8:85a3::8a2e:370:7334",
2619 &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
2624 "102:304:506:708:90a:b0c:d0e:f10",
2625 &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
2626 global | unicast_global
2631 fn to_socket_addr_socketaddr() {
2632 let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345);
2633 assert_eq!(Ok(vec![a]), tsa(a));
2637 fn test_ipv4_to_int() {
2638 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
2639 assert_eq!(u32::from(a), 0x11223344);
2643 fn test_int_to_ipv4() {
2644 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
2645 assert_eq!(Ipv4Addr::from(0x11223344), a);
2649 fn test_ipv6_to_int() {
2650 let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
2651 assert_eq!(u128::from(a), 0x112233445566778899aabbccddeeff11u128);
2655 fn test_int_to_ipv6() {
2656 let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
2657 assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
2661 fn ipv4_from_constructors() {
2662 assert_eq!(Ipv4Addr::LOCALHOST, Ipv4Addr::new(127, 0, 0, 1));
2663 assert!(Ipv4Addr::LOCALHOST.is_loopback());
2664 assert_eq!(Ipv4Addr::UNSPECIFIED, Ipv4Addr::new(0, 0, 0, 0));
2665 assert!(Ipv4Addr::UNSPECIFIED.is_unspecified());
2666 assert_eq!(Ipv4Addr::BROADCAST, Ipv4Addr::new(255, 255, 255, 255));
2667 assert!(Ipv4Addr::BROADCAST.is_broadcast());
2671 fn ipv6_from_contructors() {
2672 assert_eq!(Ipv6Addr::LOCALHOST, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
2673 assert!(Ipv6Addr::LOCALHOST.is_loopback());
2674 assert_eq!(Ipv6Addr::UNSPECIFIED, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
2675 assert!(Ipv6Addr::UNSPECIFIED.is_unspecified());
2679 fn ipv4_from_octets() {
2680 assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
2684 fn ipv6_from_segments() {
2686 Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
2687 let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff);
2688 assert_eq!(new, from_u16s);
2692 fn ipv6_from_octets() {
2694 Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
2695 let from_u8s = Ipv6Addr::from([
2696 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
2699 assert_eq!(from_u16s, from_u8s);
2704 let v41 = Ipv4Addr::new(100, 64, 3, 3);
2705 let v42 = Ipv4Addr::new(192, 0, 2, 2);
2706 let v61 = "2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap();
2707 let v62 = "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap();
2711 assert_eq!(v41, IpAddr::V4(v41));
2712 assert_eq!(v61, IpAddr::V6(v61));
2713 assert!(v41 != IpAddr::V4(v42));
2714 assert!(v61 != IpAddr::V6(v62));
2716 assert!(v41 < IpAddr::V4(v42));
2717 assert!(v61 < IpAddr::V6(v62));
2718 assert!(IpAddr::V4(v41) < v42);
2719 assert!(IpAddr::V6(v61) < v62);
2721 assert!(v41 < IpAddr::V6(v61));
2722 assert!(IpAddr::V4(v41) < v61);
2727 let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
2728 assert!(ip.is_ipv4());
2729 assert!(!ip.is_ipv6());
2734 let ip = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
2735 assert!(!ip.is_ipv4());
2736 assert!(ip.is_ipv6());