Chromium Code Reviews| Index: net/base/net_util.cc |
| diff --git a/net/base/net_util.cc b/net/base/net_util.cc |
| index 958e3c3bca82049671f39fda3574b7eca3871664..c892ac23418bc20e8a7a75aaa621dc8603cf7815 100644 |
| --- a/net/base/net_util.cc |
| +++ b/net/base/net_util.cc |
| @@ -1391,7 +1391,6 @@ std::string GetHostAndOptionalPort(const GURL& url) { |
| return url.host(); |
| } |
| -// static |
| bool IsHostnameNonUnique(const std::string& hostname) { |
| // CanonicalizeHost requires surrounding brackets to parse an IPv6 address. |
| const std::string host_or_ip = hostname.find(':') != std::string::npos ? |
| @@ -1404,11 +1403,24 @@ bool IsHostnameNonUnique(const std::string& hostname) { |
| if (canonical_name.empty()) |
| return false; |
| - // If |hostname| is an IP address, presume it's unique. |
| - // TODO(rsleevi): In the future, this should also reject IP addresses in |
| - // IANA-reserved ranges. |
| - if (host_info.IsIPAddress()) |
| - return false; |
| + // If |hostname| is an IP address, check to see if it's in an IANA-reserved |
| + // range. |
| + if (host_info.IsIPAddress()) { |
| + IPAddressNumber host_addr; |
| + if (!ParseIPLiteralToNumber(hostname.substr(host_info.out_host.begin, |
| + host_info.out_host.len), |
| + &host_addr)) { |
| + return false; |
| + } |
| + switch (host_info.family) { |
| + case url_canon::CanonHostInfo::IPV4: |
| + case url_canon::CanonHostInfo::IPV6: |
| + return IsIPAddressReserved(host_addr); |
| + case url_canon::CanonHostInfo::NEUTRAL: |
| + case url_canon::CanonHostInfo::BROKEN: |
| + return false; |
| + } |
| + } |
| // Check for a registry controlled portion of |hostname|, ignoring private |
| // registries, as they already chain to ICANN-administered registries, |
| @@ -1425,6 +1437,75 @@ bool IsHostnameNonUnique(const std::string& hostname) { |
| registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| } |
| +template<typename Arr, typename Length> |
|
Ryan Sleevi
2013/08/09 22:50:12
You can treat length as a size_t and let integer p
felt
2013/08/10 03:04:33
Done.
|
| +bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, |
| + Arr ip_prefix, |
|
Ryan Sleevi
2013/08/09 22:50:12
This doesn't match your definition in the header (
felt
2013/08/10 03:04:33
Done.
|
| + Length prefix_length_in_bits) { |
| + // Compare all the bytes that fall entirely within the prefix. |
| + int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; |
| + for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { |
| + if (ip_number[i] != ip_prefix[i]) |
| + return false; |
| + } |
| + |
| + // In case the prefix was not a multiple of 8, there will be 1 byte |
| + // which is only partially masked. |
| + int remaining_bits = prefix_length_in_bits % 8; |
| + if (remaining_bits != 0) { |
| + unsigned char mask = 0xFF << (8 - remaining_bits); |
| + int i = num_entire_bytes_in_prefix; |
| + if ((ip_number[i] & mask) != (ip_prefix[i] & mask)) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// We shouldn't compare IPv4 to IPv6 (they have different range reservations), |
| +// so we keep separate arrays. Within each array, adjacent reserved ranges are |
| +// consolidated when possible. |
| +// Sources for info: |
| +// www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml |
| +// www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml |
| +// They're formatted here with the prefix as the last element. For example: |
| +// 10.0.0.0/8 becomes 10,0,0,0,8 and fec0::/10 becomes 0xfe,0xc0,0,0,0...,10. |
| +bool IsIPAddressReserved(const IPAddressNumber& host_addr) { |
| + const unsigned char reserved_ipv4[][5] = { |
|
Ryan Sleevi
2013/08/09 22:50:12
nit: static const unsigned char kReservedIPv4
felt
2013/08/10 03:04:33
Done.
|
| + { 0,0,0,0,8 }, { 10,0,0,0,8 }, { 100,64,0,0,10 }, { 127,0,0,0,8 }, |
| + { 169,254,0,0,16 }, { 172,16,0,0,12 }, { 192,0,2,0,24 }, { 192,88,99,0,24 }, |
| + { 192,168,0,0,16 }, { 198,18,0,0,15 }, { 198,51,100,0,24 }, |
| + { 203,0,113,0,24 }, { 224,0,0,0,3 } |
| + }; |
| + const unsigned char reserved_ipv6[][17] = { |
|
Ryan Sleevi
2013/08/09 22:50:12
nit: static const unsigned char kReservedIPv6
felt
2013/08/10 03:04:33
Done.
|
| + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8 }, |
| + { 0x40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 }, |
| + { 0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 }, |
| + { 0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3 }, |
| + { 0xe0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 }, |
| + { 0xf0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5 }, |
| + { 0xf8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6 }, |
| + { 0xfc,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7 }, |
| + { 0xfe,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9 }, |
| + { 0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 }, |
| + { 0xfe,0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 }, |
| + }; |
| + bool ipv4 = (host_addr.size() == kIPv4AddressSize); |
| + size_t arr_size = (ipv4) ? |
| + arraysize(reserved_ipv4) : arraysize(reserved_ipv6); |
| + bool match = false; |
| + for (size_t i = 0; i < arr_size; ++i) { |
| + if (ipv4) { |
| + match = IPNumberPrefixCheck( |
| + host_addr, reserved_ipv4[i], reserved_ipv4[i][4]); |
| + } else { |
| + match = IPNumberPrefixCheck( |
| + host_addr, reserved_ipv6[i], reserved_ipv6[i][16]); |
| + } |
| + if (match) |
| + return true; |
| + } |
|
Ryan Sleevi
2013/08/09 22:50:12
FWIW, you could rewrite this as
size_t array_size
Ryan Sleevi
2013/08/09 22:51:31
and I made a typo :)
"const char* const* array" -
felt
2013/08/10 03:04:33
I didn't use this exactly, but something like it.
|
| + return false; |
| +} |
| + |
| // Extracts the address and port portions of a sockaddr. |
| bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr, |
| socklen_t sock_addr_len, |
| @@ -1996,25 +2077,7 @@ bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number, |
| 96 + prefix_length_in_bits); |
| } |
| - // Otherwise we are comparing two IPv4 addresses, or two IPv6 addresses. |
| - // Compare all the bytes that fall entirely within the prefix. |
| - int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; |
| - for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { |
| - if (ip_number[i] != ip_prefix[i]) |
| - return false; |
| - } |
| - |
| - // In case the prefix was not a multiple of 8, there will be 1 byte |
| - // which is only partially masked. |
| - int remaining_bits = prefix_length_in_bits % 8; |
| - if (remaining_bits != 0) { |
| - unsigned char mask = 0xFF << (8 - remaining_bits); |
| - int i = num_entire_bytes_in_prefix; |
| - if ((ip_number[i] & mask) != (ip_prefix[i] & mask)) |
| - return false; |
| - } |
| - |
| - return true; |
| + return IPNumberPrefixCheck(ip_number, ip_prefix, prefix_length_in_bits); |
|
Ryan Sleevi
2013/08/09 22:50:12
Just pass &ip_prefix[0] here - char* goodness, wit
felt
2013/08/10 03:04:33
Ahhhh.
|
| } |
| const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, |