Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: net/base/net_util.cc

Issue 22538003: Add IP address handling to net::IsHostnameNonUnique (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adding logging statements to debug linux asan failure Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <map> 9 #include <map>
10 10
(...skipping 1373 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 } 1384 }
1385 1385
1386 std::string GetHostAndOptionalPort(const GURL& url) { 1386 std::string GetHostAndOptionalPort(const GURL& url) {
1387 // For IPv6 literals, GURL::host() already includes the brackets 1387 // For IPv6 literals, GURL::host() already includes the brackets
1388 // so it is safe to just append a colon. 1388 // so it is safe to just append a colon.
1389 if (url.has_port()) 1389 if (url.has_port())
1390 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); 1390 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str());
1391 return url.host(); 1391 return url.host();
1392 } 1392 }
1393 1393
1394 // static
1395 bool IsHostnameNonUnique(const std::string& hostname) { 1394 bool IsHostnameNonUnique(const std::string& hostname) {
1396 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address. 1395 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
1397 const std::string host_or_ip = hostname.find(':') != std::string::npos ? 1396 const std::string host_or_ip = hostname.find(':') != std::string::npos ?
1398 "[" + hostname + "]" : hostname; 1397 "[" + hostname + "]" : hostname;
1399 url_canon::CanonHostInfo host_info; 1398 url_canon::CanonHostInfo host_info;
1400 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info); 1399 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info);
1401 1400
1402 // If canonicalization fails, then the input is truly malformed. However, 1401 // If canonicalization fails, then the input is truly malformed. However,
1403 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique. 1402 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique.
1404 if (canonical_name.empty()) 1403 if (canonical_name.empty())
1405 return false; 1404 return false;
1406 1405
1407 // If |hostname| is an IP address, presume it's unique. 1406 // If |hostname| is an IP address, check to see if it's in an IANA-reserved
1408 // TODO(rsleevi): In the future, this should also reject IP addresses in 1407 // range.
1409 // IANA-reserved ranges. 1408 if (host_info.IsIPAddress()) {
1410 if (host_info.IsIPAddress()) 1409 switch (host_info.family) {
1411 return false; 1410 case url_canon::CanonHostInfo::IPV4:
1411 return IsIPAddressReserved(hostname, true);
1412 case url_canon::CanonHostInfo::IPV6:
1413 return IsIPAddressReserved(hostname, false);
1414 case url_canon::CanonHostInfo::NEUTRAL:
1415 case url_canon::CanonHostInfo::BROKEN:
1416 return false;
1417 }
1418 }
1412 1419
1413 // Check for a registry controlled portion of |hostname|, ignoring private 1420 // Check for a registry controlled portion of |hostname|, ignoring private
1414 // registries, as they already chain to ICANN-administered registries, 1421 // registries, as they already chain to ICANN-administered registries,
1415 // and explicitly ignoring unknown registries. 1422 // and explicitly ignoring unknown registries.
1416 // 1423 //
1417 // Note: This means that as new gTLDs are introduced on the Internet, they 1424 // Note: This means that as new gTLDs are introduced on the Internet, they
1418 // will be treated as non-unique until the registry controlled domain list 1425 // will be treated as non-unique until the registry controlled domain list
1419 // is updated. However, because gTLDs are expected to provide significant 1426 // is updated. However, because gTLDs are expected to provide significant
1420 // advance notice to deprecate older versions of this code, this an 1427 // advance notice to deprecate older versions of this code, this an
1421 // acceptable tradeoff. 1428 // acceptable tradeoff.
1422 return 0 == registry_controlled_domains::GetRegistryLength( 1429 return 0 == registry_controlled_domains::GetRegistryLength(
1423 canonical_name, 1430 canonical_name,
1424 registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, 1431 registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
1425 registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); 1432 registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
1426 } 1433 }
1427 1434
1435 bool IsIPAddressReserved(const std::string& hostname, bool ipv4) {
1436 LOG(INFO) << "IsIPAddressReserved " << hostname;
1437 IPAddressNumber host_addr;
1438 if (!ParseIPLiteralToNumber(hostname, &host_addr)) return false;
Ryan Sleevi 2013/08/07 19:04:44 nit: linebreak for the if.
felt 2013/08/08 19:35:33 Done.
1439
1440 // We shouldn't compare IPv4 to IPv6, so we keep separate arrays.
1441 static const char* const reserved_ipv4[] = {
1442 "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8",
1443 "169.254.0.0/16", "172.16.0.0/12", "192.0.2.0/24", "192.88.99.0/24",
1444 "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24",
1445 "224.0.0.0/8", "225.0.0.0/8", "226.0.0.0/8", "227.0.0.0/8", "228.0.0.0/8",
1446 "229.0.0.0/8", "230.0.0.0/8", "231.0.0.0/8", "232.0.0.0/8", "233.0.0.0/8",
1447 "234.0.0.0/8", "235.0.0.0/8", "236.0.0.0/8", "237.0.0.0/8", "238.0.0.0/8",
1448 "239.0.0.0/8", "240.0.0.0/8", "241.0.0.0/8", "242.0.0.0/8", "243.0.0.0/8",
1449 "244.0.0.0/8", "245.0.0.0/8", "246.0.0.0/8", "247.0.0.0/8", "248.0.0.0/8",
1450 "249.0.0.0/8", "250.0.0.0/8", "251.0.0.0/8", "252.0.0.0/8", "253.0.0.0/8",
1451 "254.0.0.0/8", "255.0.0.0/8"
1452 };
Ryan Sleevi 2013/08/07 19:04:44 List the sources for these. eg: http://www.iana.o
felt 2013/08/08 19:35:33 Done.
1453 static const char* const reserved_ipv6[] = {
1454 "0000::/8", "::1/128", "0100::/8", "0200::/7", "0400::/6", "0800::/5",
1455 "1000::/4", "4000::/3", "6000::/3", "8000::/3", "a000::/3", "c000::/3",
1456 "e000::/4", "f000::/5", "f800::/6", "FC00::/7", "fe00::/9", "fe80::/10",
1457 "fec0::/10"
Ryan Sleevi 2013/08/07 19:04:44 List the sources for this data: http://www.iana.o
felt 2013/08/08 19:35:33 Done
1458 };
Ryan Sleevi 2013/08/07 19:04:44 Rather than storing these as strings (and re-parsi
felt 2013/08/08 19:35:33 Done.
1459
1460 size_t array_size =
1461 ipv4 ? arraysize(reserved_ipv4) : arraysize(reserved_ipv6);
1462 for (size_t i = 0; i < array_size; i++) {
1463 IPAddressNumber reserved_address;
1464 size_t prefix_length;
1465 DCHECK(ParseCIDRBlock(ipv4 ? reserved_ipv4[i] : reserved_ipv6[i],
1466 &reserved_address,
1467 &prefix_length));
1468 LOG(INFO) << ipv4;
1469 LOG(INFO) << i;
1470 if (IPNumberMatchesPrefix(host_addr, reserved_address, prefix_length))
1471 return true;
1472 }
1473 return false;
1474 }
1475
1428 // Extracts the address and port portions of a sockaddr. 1476 // Extracts the address and port portions of a sockaddr.
1429 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr, 1477 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
1430 socklen_t sock_addr_len, 1478 socklen_t sock_addr_len,
1431 const uint8** address, 1479 const uint8** address,
1432 size_t* address_len, 1480 size_t* address_len,
1433 uint16* port) { 1481 uint16* port) {
1434 if (sock_addr->sa_family == AF_INET) { 1482 if (sock_addr->sa_family == AF_INET) {
1435 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in))) 1483 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in)))
1436 return false; 1484 return false;
1437 const struct sockaddr_in* addr = 1485 const struct sockaddr_in* addr =
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
2081 2129
2082 NetworkInterface::NetworkInterface(const std::string& name, 2130 NetworkInterface::NetworkInterface(const std::string& name,
2083 const IPAddressNumber& address) 2131 const IPAddressNumber& address)
2084 : name(name), address(address) { 2132 : name(name), address(address) {
2085 } 2133 }
2086 2134
2087 NetworkInterface::~NetworkInterface() { 2135 NetworkInterface::~NetworkInterface() {
2088 } 2136 }
2089 2137
2090 } // namespace net 2138 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698