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

Unified Diff: net/base/net_util.cc

Issue 1177933002: Resolve RFC 6761 localhost names to loopback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: style tweaks, simplification Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: net/base/net_util.cc
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index 028b56b74a8bb526b4e486bb1c328efda71005d4..dc03a91817efff39282f517310074706a182eaf6 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -45,6 +45,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/sys_byteorder.h"
#include "base/values.h"
+#include "net/base/address_list.h"
#include "net/base/dns_util.h"
#include "net/base/ip_address_number.h"
#include "net/base/net_module.h"
@@ -146,6 +147,23 @@ static const int kAllowedFtpPorts[] = {
22, // ssh
};
+bool IsLocalHostname(const std::string& host) {
+ std::string lowercased_host = base::StringToLowerASCII(host);
Ryan Sleevi 2015/06/12 01:00:04 Suggestion: To reduce comparisons for the forms, a
estark 2015/06/12 05:59:46 Done.
+ return (lowercased_host == "localhost" ||
+ lowercased_host == "localhost.localdomain" ||
+ lowercased_host == "localhost." ||
+ lowercased_host == "localhost.localdomain." ||
+ IsLocalhostTLD(lowercased_host));
+}
+
+bool IsLocal6Hostname(const std::string& host) {
+ std::string lowercased_host = base::StringToLowerASCII(host);
+ return (lowercased_host == "localhost6" ||
+ lowercased_host == "localhost6.localdomain6" ||
+ lowercased_host == "localhost6." ||
+ lowercased_host == "localhost6.localdomain6.");
+}
+
} // namespace
static base::LazyInstance<std::multiset<int> >::Leaky
@@ -730,10 +748,35 @@ int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
return base::NetToHost16(*port_field);
}
+bool ResolveLocalHostname(const std::string& host,
+ uint16_t port,
+ AddressList* address_list) {
+ const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1};
+ const unsigned char kLocalhostIPv6[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Ryan Sleevi 2015/06/12 01:00:04 nit: Can make these static (so that they're in the
estark 2015/06/12 05:59:46 Done.
+
+ address_list->clear();
+
+ bool isLocal6 = IsLocal6Hostname(host);
Ryan Sleevi 2015/06/12 01:00:04 naming: is_local6
estark 2015/06/12 05:59:46 Done.
+ if (!isLocal6 && !IsLocalHostname(host))
+ return false;
+
+ address_list->push_back(
+ IPEndPoint(IPAddressNumber(kLocalhostIPv6,
+ kLocalhostIPv6 + arraysize(kLocalhostIPv6)),
+ port));
+ if (!isLocal6) {
+ address_list->push_back(
+ IPEndPoint(IPAddressNumber(kLocalhostIPv4,
+ kLocalhostIPv4 + arraysize(kLocalhostIPv4)),
+ port));
+ }
+
+ return true;
+}
+
bool IsLocalhost(const std::string& host) {
- if (host == "localhost" || host == "localhost.localdomain" ||
- host == "localhost6" || host == "localhost6.localdomain6" ||
- IsLocalhostTLD(host))
+ if (IsLocalHostname(host) || IsLocal6Hostname(host))
return true;
IPAddressNumber ip_number;

Powered by Google App Engine
This is Rietveld 408576698