net::IPAddress currently doesn't support the ability to determine the reserved IP range to which a reserved address belongs. This functionality is used in a handful of places in the codebase (e.g., net::IsLocalhost(), net::HostResolverImpl::IsGloballyReachable(), LocalNetworkRequestsPageLoadMetricsObserver::OnCommit()), resulting in code duplication. Changes to the list of reserved IP address prefixes by IANA would require changes to some of these checks.
The information required to determine which reserved IP range an address falls into is present in tables within IPAddress::IsReservedIPv4() and IPAddress::IsReservedIPv6(), but is not accessible outside the class or elsewhere in net/. As a result, determining whether two IP addresses belong to the same reserved IP range (as in LocalNetworkRequestsPageLoadMetricsObserver::OnCommit()) requires duplication of the information.
I propose two new methods be added to net::IPAddress to provide the ability to retrieve the reserved IP prefix for a reserved IP address:
bool IPAddress::IsInReservedRangeIPv4(uint8_t* prefix, size_t* prefix_length)
bool IPAddress::IsInReservedRangeIPv6(uint8_t* prefix, size_t* prefix_length)
The methods should return true iff the IPAddress is of the correct type (IPv4 or IPv6) and belongs to a reserved range (same behavior as IsReservedIPv4 and IsReservedIPv6), and return through output parameters |prefix| and |prefix_length| the reserved IP prefix as an array of uint8_t and corresponding prefix length in bits.
For example, for the address 10.5.0.1, IsInReservedRangeIPv4 should return true and assign prefix=[10,0,0,0] and prefix_length=8.
Comment 1 by uthakore@chromium.org
, Jul 6 2017