Skip to content

Commit 8f67997

Browse files
committed
std::net: add Ipv4Addr::is_reserved()
1 parent aea687c commit 8f67997

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libstd/net/ip.rs

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

535+
/// Returns [`true`] if this address is reserved by IANA for future use. [IETF RFC 1112]
536+
/// defines the block of reserved addresses as `240.0.0.0/4`. This range normally includes the
537+
/// broadcast address `255.255.255.255`, but this implementation explicitely excludes it, since
538+
/// it is obviously not reserved for future use.
539+
///
540+
/// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112
541+
/// [`true`]: ../../std/primitive.bool.html
542+
///
543+
/// # Examples
544+
///
545+
/// ```
546+
/// #![feature(ip)]
547+
/// use std::net::Ipv4Addr;
548+
///
549+
/// fn main() {
550+
/// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true);
551+
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true);
552+
///
553+
/// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false);
554+
/// // The broadcast address is not considered as reserved for future use by this
555+
/// // implementation
556+
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
557+
/// }
558+
/// ```
559+
pub fn is_reserved(&self) -> bool {
560+
self.octets()[0] & 240 == 240 && !self.is_broadcast()
561+
}
562+
535563
/// Returns [`true`] if this is a multicast address (224.0.0.0/4).
536564
///
537565
/// Multicast addresses have a most significant octet between 224 and 239,

0 commit comments

Comments
 (0)