Skip to content

Commit f87b967

Browse files
committed
std::net: add Ipv4Addr::is_ietf_protocol_assignment()
1 parent de3cf0d commit f87b967

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libstd/net/ip.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,40 @@ impl Ipv4Addr {
532532
!self.is_broadcast() && !self.is_documentation() && !self.is_unspecified()
533533
}
534534

535+
/// Returns [`true`] if this address is part of `192.0.0.0/24`, which is reserved to
536+
/// IANA for IETF protocol assignments, as documented in [IETF RFC 6890].
537+
///
538+
/// Note that parts of this block are in use:
539+
///
540+
/// - `192.0.0.8/32` is the "IPv4 dummy address" (see [IETF RFC 7600])
541+
/// - `192.0.0.9/32` is the "Port Control Protocol Anycast" (see [IETF RFC 7723])
542+
/// - `192.0.0.10/32` is used for NAT traversal (see [IETF RFC 8155])
543+
///
544+
/// [IETF RFC 6890]: https://tools.ietf.org/html/rfc6890
545+
/// [IETF RFC 7600]: https://tools.ietf.org/html/rfc7600
546+
/// [IETF RFC 7723]: https://tools.ietf.org/html/rfc7723
547+
/// [IETF RFC 8155]: https://tools.ietf.org/html/rfc8155
548+
/// [`true`]: ../../std/primitive.bool.html
549+
///
550+
/// # Examples
551+
///
552+
/// ```
553+
/// #![feature(ip)]
554+
/// use std::net::Ipv4Addr;
555+
///
556+
/// fn main() {
557+
/// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_ietf_protocol_assignment(), true);
558+
/// assert_eq!(Ipv4Addr::new(192, 0, 0, 8).is_ietf_protocol_assignment(), true);
559+
/// assert_eq!(Ipv4Addr::new(192, 0, 0, 9).is_ietf_protocol_assignment(), true);
560+
/// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_ietf_protocol_assignment(), true);
561+
/// assert_eq!(Ipv4Addr::new(192, 0, 1, 0).is_ietf_protocol_assignment(), false);
562+
/// assert_eq!(Ipv4Addr::new(191, 255, 255, 255).is_ietf_protocol_assignment(), false);
563+
/// }
564+
/// ```
565+
pub fn is_ietf_protocol_assignment(&self) -> bool {
566+
self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0
567+
}
568+
535569
/// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for
536570
/// network devices benchmarking. This range is defined in [IETF RFC 2544] as `192.18.0.0`
537571
/// through `198.19.255.255` but [errata 423] corrects it to `198.18.0.0/15`.

0 commit comments

Comments
 (0)