Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
02d815f
std::net: site-local ipv6 prefixes are global
little-dude Nov 19, 2018
5aea184
std::net: improve Ipv6Addr::is_unicast_site_local() doc
little-dude Nov 19, 2018
1f0aa40
std::net: add Ipv6Addr::is_unicast_link_local_strict()
little-dude Nov 19, 2018
aea687c
std::net: fix doc markdown in Ipv6Addr::is_unique_local()
little-dude Nov 19, 2018
8f67997
std::net: add Ipv4Addr::is_reserved()
little-dude Nov 19, 2018
de3cf0d
std::net: add Ipv4Addr::is_benchmarking()
little-dude Nov 19, 2018
f87b967
std::net: add Ipv4Addr::is_ietf_protocol_assignment()
little-dude Nov 19, 2018
67291cc
std::net: add Ipv4Addr::is_shared()
little-dude Nov 19, 2018
9f6a747
std::net: fix Ipv4Addr::is_global()
little-dude Nov 19, 2018
8106320
std::net: fix documentation markdown
little-dude Nov 20, 2018
c34bcc6
std::net: use macros to test ip properties
little-dude Nov 20, 2018
c302d2c
std::net: fix Ipv4addr::is_global() tests
little-dude Apr 20, 2019
99d9bb6
std::net: fix tests for site-local ipv6 addresses
little-dude Apr 21, 2019
40d0127
std::net: tests for Ipv6addr::is_unicast_link_local{_strict}()
little-dude Apr 22, 2019
9dcfd9f
std::net: tests for Ipv4addr::is_benchmarking()
little-dude Apr 22, 2019
a2bead8
std::net: tests for Ipv4addr::is_ietf_protocol_assignment()
little-dude Apr 22, 2019
6662777
std::net: tests for Ipv4addr::is_reserved()
little-dude Apr 22, 2019
634dcd0
std::net: add warning in Ipv6Addr::is_unicast_site_local() doc
little-dude Apr 23, 2019
fe718ef
std::net: add warning in Ipv4addr::is_reserved() documentation
little-dude Apr 23, 2019
cddb838
std::net: tests for Ipv4addr::is_shared()
little-dude Apr 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
std::net: fix Ipv4Addr::is_global()
As per @therealbstern's comment[0]:

The implementation of Ipv4::is_global is not complete, according to the
IANA IPv4 Special-Purpose Address Registry.

        - It compares the address to 0.0.0.0, but anything in 0.0.0.0/8
          should not be considered global.
                - 0/8 is not global and is currently forbidden because
                  some systems used to treat it as the local network.
                - The implementation of Ipv4::is_unspecified is correct.
                  0.0.0.0 is the unspecified address.
        - It does not examine 100.64.0.0/10, which is "Shared Address
          Space" and not global.
        - Ditto 192.0.0.0/24 (IETF Protocol Assignments), except for
          192.0.0.9/32 and 192.0.0.10/32, which are carved out as
          globally reachable.
        - 198.18.0.0/15 is for "Benchmarking" and should not be globally
          reachable.
        - 240.0.0.0/4 is reserved and not currently reachable
  • Loading branch information
little-dude committed Apr 22, 2019
commit 9f6a747b32d55d0219a909eb29c136efbb98c473
72 changes: 64 additions & 8 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,19 @@ impl Ipv4Addr {
///
/// The following return false:
///
/// - private address (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16)
/// - the loopback address (127.0.0.0/8)
/// - the link-local address (169.254.0.0/16)
/// - the broadcast address (255.255.255.255/32)
/// - test addresses used for documentation (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24)
/// - the unspecified address (0.0.0.0)
/// - private addresses (see [`is_private()`](#method.is_private))
/// - the loopback address (see [`is_loopback()`](#method.is_loopback))
/// - the link-local address (see [`is_link_local()`](#method.is_link_local))
/// - the broadcast address (see [`is_broadcast()`](#method.is_broadcast))
/// - addresses used for documentation (see [`is_documentation()`](#method.is_documentation))
/// - the unspecified address (see [`is_unspecified()`](#method.is_unspecified)), and the whole
/// 0.0.0.0/8 block
/// - addresses reserved for future protocols (see
/// [`is_ietf_protocol_assignment()`](#method.is_ietf_protocol_assignment), except
/// `192.0.0.9/32` and `192.0.0.10/32` which are globally routable
/// - addresses reserved for future use (see [`is_reserved()`](#method.is_reserved())
/// - addresses reserved for networking devices benchmarking (see
/// [`is_benchmarking`](#method.is_benchmarking))
///
/// [ipv4-sr]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
/// [`true`]: ../../std/primitive.bool.html
Expand All @@ -520,16 +527,65 @@ impl Ipv4Addr {
/// use std::net::Ipv4Addr;
///
/// fn main() {
/// // private addresses are not global
/// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
/// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
///
/// // the 0.0.0.0/8 block is not global
/// assert_eq!(Ipv4Addr::new(0, 1, 2, 3).is_global(), false);
/// // in particular, the unspecified address is not global
/// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
///
/// // the loopback address is not global
/// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_global(), false);
///
/// // link local addresses are not global
/// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false);
///
/// // the broadcast address is not global
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_global(), false);
///
/// // the broadcast address is not global
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false);
/// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false);
/// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false);
///
/// // shared addresses are not global
/// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false);
///
/// // addresses reserved for protocol assignment are not global
/// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_global(), false);
/// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_global(), false);
///
/// // addresses reserved for future use are not global
/// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false);
///
/// // addresses reserved for network devices benchmarking are not global
/// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false);
///
/// // All the other addresses are global
/// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true);
/// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
/// }
/// ```
pub fn is_global(&self) -> bool {
!self.is_private() && !self.is_loopback() && !self.is_link_local() &&
!self.is_broadcast() && !self.is_documentation() && !self.is_unspecified()
// check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
// globally routable addresses in the 192.0.0.0/24 range.
if u32::from(*self) == 0xc0000009 || u32::from(*self) == 0xc000000a {
return true;
}
!self.is_private()
&& !self.is_loopback()
&& !self.is_link_local()
&& !self.is_broadcast()
&& !self.is_documentation()
&& !self.is_shared()
&& !self.is_ietf_protocol_assignment()
&& !self.is_reserved()
&& !self.is_benchmarking()
// Make sure the address is not in 0.0.0.0/8
&& self.octets()[0] != 0
}

/// Returns [`true`] if this address is part of the Shared Address Space defined in
Expand Down