Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / net_util.cc
blobd4b525b35d45d57b87d1654219c0148b422c7b6b
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
3 // found in the LICENSE file.
5 #include "net/base/net_util.h"
7 #include <errno.h>
9 #include <algorithm>
10 #include <limits>
11 #include <string>
13 #include "build/build_config.h"
15 #if defined(OS_WIN)
16 #include <windows.h>
17 #include <iphlpapi.h>
18 #include <winsock2.h>
19 #include <ws2bth.h>
20 #pragma comment(lib, "iphlpapi.lib")
21 #elif defined(OS_POSIX)
22 #include <fcntl.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #include <unistd.h>
26 #if !defined(OS_NACL)
27 #include <net/if.h>
28 #if !defined(OS_ANDROID)
29 #include <ifaddrs.h>
30 #endif // !defined(OS_NACL)
31 #endif // !defined(OS_ANDROID)
32 #endif // defined(OS_POSIX)
34 #include "base/basictypes.h"
35 #include "base/json/string_escape.h"
36 #include "base/logging.h"
37 #include "base/strings/string_piece.h"
38 #include "base/strings/string_split.h"
39 #include "base/strings/string_util.h"
40 #include "base/strings/stringprintf.h"
41 #include "base/strings/utf_string_conversions.h"
42 #include "base/sys_byteorder.h"
43 #include "base/values.h"
44 #include "net/base/address_list.h"
45 #include "net/base/dns_util.h"
46 #include "net/base/ip_address_number.h"
47 #include "net/base/net_module.h"
48 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
49 #include "net/grit/net_resources.h"
50 #include "net/http/http_content_disposition.h"
51 #include "url/gurl.h"
52 #include "url/third_party/mozilla/url_parse.h"
53 #include "url/url_canon.h"
54 #include "url/url_canon_ip.h"
56 #if defined(OS_ANDROID)
57 #include "net/android/network_library.h"
58 #endif
59 #if defined(OS_WIN)
60 #include "net/base/winsock_init.h"
61 #endif
63 namespace net {
65 namespace {
67 std::string NormalizeHostname(const std::string& host) {
68 std::string result = base::StringToLowerASCII(host);
69 if (!result.empty() && *result.rbegin() == '.')
70 result.resize(result.size() - 1);
71 return result;
74 bool IsNormalizedLocalhostTLD(const std::string& host) {
75 return base::EndsWith(host, ".localhost", base::CompareCase::SENSITIVE);
78 // |host| should be normalized.
79 bool IsLocalHostname(const std::string& host) {
80 return host == "localhost" || host == "localhost.localdomain" ||
81 IsNormalizedLocalhostTLD(host);
84 // |host| should be normalized.
85 bool IsLocal6Hostname(const std::string& host) {
86 return host == "localhost6" || host == "localhost6.localdomain6";
89 } // namespace
91 std::string CanonicalizeHost(const std::string& host,
92 url::CanonHostInfo* host_info) {
93 // Try to canonicalize the host.
94 const url::Component raw_host_component(0, static_cast<int>(host.length()));
95 std::string canon_host;
96 url::StdStringCanonOutput canon_host_output(&canon_host);
97 url::CanonicalizeHostVerbose(host.c_str(), raw_host_component,
98 &canon_host_output, host_info);
100 if (host_info->out_host.is_nonempty() &&
101 host_info->family != url::CanonHostInfo::BROKEN) {
102 // Success! Assert that there's no extra garbage.
103 canon_host_output.Complete();
104 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length()));
105 } else {
106 // Empty host, or canonicalization failed. We'll return empty.
107 canon_host.clear();
110 return canon_host;
113 std::string GetDirectoryListingHeader(const base::string16& title) {
114 static const base::StringPiece header(
115 NetModule::GetResource(IDR_DIR_HEADER_HTML));
116 // This can be null in unit tests.
117 DLOG_IF(WARNING, header.empty()) <<
118 "Missing resource: directory listing header";
120 std::string result;
121 if (!header.empty())
122 result.assign(header.data(), header.size());
124 result.append("<script>start(");
125 base::EscapeJSONString(title, true, &result);
126 result.append(");</script>\n");
128 return result;
131 inline bool IsHostCharAlphanumeric(char c) {
132 // We can just check lowercase because uppercase characters have already been
133 // normalized.
134 return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'));
137 bool IsCanonicalizedHostCompliant(const std::string& host) {
138 if (host.empty())
139 return false;
141 bool in_component = false;
142 bool most_recent_component_started_alphanumeric = false;
144 for (std::string::const_iterator i(host.begin()); i != host.end(); ++i) {
145 const char c = *i;
146 if (!in_component) {
147 most_recent_component_started_alphanumeric = IsHostCharAlphanumeric(c);
148 if (!most_recent_component_started_alphanumeric && (c != '-') &&
149 (c != '_')) {
150 return false;
152 in_component = true;
153 } else if (c == '.') {
154 in_component = false;
155 } else if (!IsHostCharAlphanumeric(c) && (c != '-') && (c != '_')) {
156 return false;
160 return most_recent_component_started_alphanumeric;
163 base::string16 StripWWW(const base::string16& text) {
164 const base::string16 www(base::ASCIIToUTF16("www."));
165 return base::StartsWith(text, www, base::CompareCase::SENSITIVE)
166 ? text.substr(www.length()) : text;
169 base::string16 StripWWWFromHost(const GURL& url) {
170 DCHECK(url.is_valid());
171 return StripWWW(base::ASCIIToUTF16(url.host()));
174 int SetNonBlocking(int fd) {
175 #if defined(OS_WIN)
176 unsigned long no_block = 1;
177 return ioctlsocket(fd, FIONBIO, &no_block);
178 #elif defined(OS_POSIX)
179 int flags = fcntl(fd, F_GETFL, 0);
180 if (-1 == flags)
181 return flags;
182 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
183 #endif
186 bool ParseHostAndPort(std::string::const_iterator host_and_port_begin,
187 std::string::const_iterator host_and_port_end,
188 std::string* host,
189 int* port) {
190 if (host_and_port_begin >= host_and_port_end)
191 return false;
193 // When using url, we use char*.
194 const char* auth_begin = &(*host_and_port_begin);
195 int auth_len = host_and_port_end - host_and_port_begin;
197 url::Component auth_component(0, auth_len);
198 url::Component username_component;
199 url::Component password_component;
200 url::Component hostname_component;
201 url::Component port_component;
203 url::ParseAuthority(auth_begin, auth_component, &username_component,
204 &password_component, &hostname_component, &port_component);
206 // There shouldn't be a username/password.
207 if (username_component.is_valid() || password_component.is_valid())
208 return false;
210 if (!hostname_component.is_nonempty())
211 return false; // Failed parsing.
213 int parsed_port_number = -1;
214 if (port_component.is_nonempty()) {
215 parsed_port_number = url::ParsePort(auth_begin, port_component);
217 // If parsing failed, port_number will be either PORT_INVALID or
218 // PORT_UNSPECIFIED, both of which are negative.
219 if (parsed_port_number < 0)
220 return false; // Failed parsing the port number.
223 if (port_component.len == 0)
224 return false; // Reject inputs like "foo:"
226 unsigned char tmp_ipv6_addr[16];
228 // If the hostname starts with a bracket, it is either an IPv6 literal or
229 // invalid. If it is an IPv6 literal then strip the brackets.
230 if (hostname_component.len > 0 &&
231 auth_begin[hostname_component.begin] == '[') {
232 if (auth_begin[hostname_component.end() - 1] == ']' &&
233 url::IPv6AddressToNumber(
234 auth_begin, hostname_component, tmp_ipv6_addr)) {
235 // Strip the brackets.
236 hostname_component.begin++;
237 hostname_component.len -= 2;
238 } else {
239 return false;
243 // Pass results back to caller.
244 host->assign(auth_begin + hostname_component.begin, hostname_component.len);
245 *port = parsed_port_number;
247 return true; // Success.
250 bool ParseHostAndPort(const std::string& host_and_port,
251 std::string* host,
252 int* port) {
253 return ParseHostAndPort(
254 host_and_port.begin(), host_and_port.end(), host, port);
257 std::string GetHostAndPort(const GURL& url) {
258 // For IPv6 literals, GURL::host() already includes the brackets so it is
259 // safe to just append a colon.
260 return base::StringPrintf("%s:%d", url.host().c_str(),
261 url.EffectiveIntPort());
264 std::string GetHostAndOptionalPort(const GURL& url) {
265 // For IPv6 literals, GURL::host() already includes the brackets
266 // so it is safe to just append a colon.
267 if (url.has_port())
268 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str());
269 return url.host();
272 bool IsHostnameNonUnique(const std::string& hostname) {
273 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
274 const std::string host_or_ip = hostname.find(':') != std::string::npos ?
275 "[" + hostname + "]" : hostname;
276 url::CanonHostInfo host_info;
277 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info);
279 // If canonicalization fails, then the input is truly malformed. However,
280 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique.
281 if (canonical_name.empty())
282 return false;
284 // If |hostname| is an IP address, check to see if it's in an IANA-reserved
285 // range.
286 if (host_info.IsIPAddress()) {
287 IPAddressNumber host_addr;
288 if (!ParseIPLiteralToNumber(hostname.substr(host_info.out_host.begin,
289 host_info.out_host.len),
290 &host_addr)) {
291 return false;
293 switch (host_info.family) {
294 case url::CanonHostInfo::IPV4:
295 case url::CanonHostInfo::IPV6:
296 return IsIPAddressReserved(host_addr);
297 case url::CanonHostInfo::NEUTRAL:
298 case url::CanonHostInfo::BROKEN:
299 return false;
303 // Check for a registry controlled portion of |hostname|, ignoring private
304 // registries, as they already chain to ICANN-administered registries,
305 // and explicitly ignoring unknown registries.
307 // Note: This means that as new gTLDs are introduced on the Internet, they
308 // will be treated as non-unique until the registry controlled domain list
309 // is updated. However, because gTLDs are expected to provide significant
310 // advance notice to deprecate older versions of this code, this an
311 // acceptable tradeoff.
312 return 0 == registry_controlled_domains::GetRegistryLength(
313 canonical_name,
314 registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
315 registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
318 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other)
319 : addr_len(other.addr_len),
320 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {
321 memcpy(addr, other.addr, addr_len);
324 void SockaddrStorage::operator=(const SockaddrStorage& other) {
325 addr_len = other.addr_len;
326 // addr is already set to &this->addr_storage by default ctor.
327 memcpy(addr, other.addr, addr_len);
330 // Extracts the address and port portions of a sockaddr.
331 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
332 socklen_t sock_addr_len,
333 const uint8_t** address,
334 size_t* address_len,
335 uint16_t* port) {
336 if (sock_addr->sa_family == AF_INET) {
337 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in)))
338 return false;
339 const struct sockaddr_in* addr =
340 reinterpret_cast<const struct sockaddr_in*>(sock_addr);
341 *address = reinterpret_cast<const uint8_t*>(&addr->sin_addr);
342 *address_len = kIPv4AddressSize;
343 if (port)
344 *port = base::NetToHost16(addr->sin_port);
345 return true;
348 if (sock_addr->sa_family == AF_INET6) {
349 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6)))
350 return false;
351 const struct sockaddr_in6* addr =
352 reinterpret_cast<const struct sockaddr_in6*>(sock_addr);
353 *address = reinterpret_cast<const uint8_t*>(&addr->sin6_addr);
354 *address_len = kIPv6AddressSize;
355 if (port)
356 *port = base::NetToHost16(addr->sin6_port);
357 return true;
360 #if defined(OS_WIN)
361 if (sock_addr->sa_family == AF_BTH) {
362 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
363 return false;
364 const SOCKADDR_BTH* addr =
365 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
366 *address = reinterpret_cast<const uint8_t*>(&addr->btAddr);
367 *address_len = kBluetoothAddressSize;
368 if (port)
369 *port = static_cast<uint16_t>(addr->port);
370 return true;
372 #endif
374 return false; // Unrecognized |sa_family|.
377 std::string NetAddressToString(const struct sockaddr* sa,
378 socklen_t sock_addr_len) {
379 const uint8_t* address;
380 size_t address_len;
381 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address,
382 &address_len, NULL)) {
383 NOTREACHED();
384 return std::string();
386 return IPAddressToString(address, address_len);
389 std::string NetAddressToStringWithPort(const struct sockaddr* sa,
390 socklen_t sock_addr_len) {
391 const uint8_t* address;
392 size_t address_len;
393 uint16_t port;
394 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address,
395 &address_len, &port)) {
396 NOTREACHED();
397 return std::string();
399 return IPAddressToStringWithPort(address, address_len, port);
402 std::string GetHostName() {
403 #if defined(OS_NACL)
404 NOTIMPLEMENTED();
405 return std::string();
406 #else // defined(OS_NACL)
407 #if defined(OS_WIN)
408 EnsureWinsockInit();
409 #endif
411 // Host names are limited to 255 bytes.
412 char buffer[256];
413 int result = gethostname(buffer, sizeof(buffer));
414 if (result != 0) {
415 DVLOG(1) << "gethostname() failed with " << result;
416 buffer[0] = '\0';
418 return std::string(buffer);
419 #endif // !defined(OS_NACL)
422 void GetIdentityFromURL(const GURL& url,
423 base::string16* username,
424 base::string16* password) {
425 UnescapeRule::Type flags =
426 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS;
427 *username = UnescapeAndDecodeUTF8URLComponent(url.username(), flags);
428 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags);
431 std::string GetHostOrSpecFromURL(const GURL& url) {
432 return url.has_host() ? TrimEndingDot(url.host()) : url.spec();
435 bool CanStripTrailingSlash(const GURL& url) {
436 // Omit the path only for standard, non-file URLs with nothing but "/" after
437 // the hostname.
438 return url.IsStandard() && !url.SchemeIsFile() &&
439 !url.SchemeIsFileSystem() && !url.has_query() && !url.has_ref()
440 && url.path() == "/";
443 GURL SimplifyUrlForRequest(const GURL& url) {
444 DCHECK(url.is_valid());
445 GURL::Replacements replacements;
446 replacements.ClearUsername();
447 replacements.ClearPassword();
448 replacements.ClearRef();
449 return url.ReplaceComponents(replacements);
452 bool HaveOnlyLoopbackAddresses() {
453 #if defined(OS_ANDROID)
454 return android::HaveOnlyLoopbackAddresses();
455 #elif defined(OS_NACL)
456 NOTIMPLEMENTED();
457 return false;
458 #elif defined(OS_POSIX)
459 struct ifaddrs* interface_addr = NULL;
460 int rv = getifaddrs(&interface_addr);
461 if (rv != 0) {
462 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
463 return false;
466 bool result = true;
467 for (struct ifaddrs* interface = interface_addr;
468 interface != NULL;
469 interface = interface->ifa_next) {
470 if (!(IFF_UP & interface->ifa_flags))
471 continue;
472 if (IFF_LOOPBACK & interface->ifa_flags)
473 continue;
474 const struct sockaddr* addr = interface->ifa_addr;
475 if (!addr)
476 continue;
477 if (addr->sa_family == AF_INET6) {
478 // Safe cast since this is AF_INET6.
479 const struct sockaddr_in6* addr_in6 =
480 reinterpret_cast<const struct sockaddr_in6*>(addr);
481 const struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
482 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr))
483 continue;
485 if (addr->sa_family != AF_INET6 && addr->sa_family != AF_INET)
486 continue;
488 result = false;
489 break;
491 freeifaddrs(interface_addr);
492 return result;
493 #elif defined(OS_WIN)
494 // TODO(wtc): implement with the GetAdaptersAddresses function.
495 NOTIMPLEMENTED();
496 return false;
497 #else
498 NOTIMPLEMENTED();
499 return false;
500 #endif // defined(various platforms)
503 AddressFamily GetAddressFamily(const IPAddressNumber& address) {
504 switch (address.size()) {
505 case kIPv4AddressSize:
506 return ADDRESS_FAMILY_IPV4;
507 case kIPv6AddressSize:
508 return ADDRESS_FAMILY_IPV6;
509 default:
510 return ADDRESS_FAMILY_UNSPECIFIED;
514 int ConvertAddressFamily(AddressFamily address_family) {
515 switch (address_family) {
516 case ADDRESS_FAMILY_UNSPECIFIED:
517 return AF_UNSPEC;
518 case ADDRESS_FAMILY_IPV4:
519 return AF_INET;
520 case ADDRESS_FAMILY_IPV6:
521 return AF_INET6;
523 NOTREACHED();
524 return AF_UNSPEC;
527 const uint16_t* GetPortFieldFromSockaddr(const struct sockaddr* address,
528 socklen_t address_len) {
529 if (address->sa_family == AF_INET) {
530 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len));
531 const struct sockaddr_in* sockaddr =
532 reinterpret_cast<const struct sockaddr_in*>(address);
533 return &sockaddr->sin_port;
534 } else if (address->sa_family == AF_INET6) {
535 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len));
536 const struct sockaddr_in6* sockaddr =
537 reinterpret_cast<const struct sockaddr_in6*>(address);
538 return &sockaddr->sin6_port;
539 } else {
540 NOTREACHED();
541 return NULL;
545 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
546 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len);
547 if (!port_field)
548 return -1;
549 return base::NetToHost16(*port_field);
552 bool ResolveLocalHostname(const std::string& host,
553 uint16_t port,
554 AddressList* address_list) {
555 static const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1};
556 static const unsigned char kLocalhostIPv6[] = {
557 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
559 std::string normalized_host = NormalizeHostname(host);
561 address_list->clear();
563 bool is_local6 = IsLocal6Hostname(normalized_host);
564 if (!is_local6 && !IsLocalHostname(normalized_host))
565 return false;
567 address_list->push_back(
568 IPEndPoint(IPAddressNumber(kLocalhostIPv6,
569 kLocalhostIPv6 + arraysize(kLocalhostIPv6)),
570 port));
571 if (!is_local6) {
572 address_list->push_back(
573 IPEndPoint(IPAddressNumber(kLocalhostIPv4,
574 kLocalhostIPv4 + arraysize(kLocalhostIPv4)),
575 port));
578 return true;
581 bool IsLocalhost(const std::string& host) {
582 std::string normalized_host = NormalizeHostname(host);
583 if (IsLocalHostname(normalized_host) || IsLocal6Hostname(normalized_host))
584 return true;
586 IPAddressNumber ip_number;
587 if (ParseIPLiteralToNumber(host, &ip_number)) {
588 size_t size = ip_number.size();
589 switch (size) {
590 case kIPv4AddressSize: {
591 IPAddressNumber localhost_prefix;
592 localhost_prefix.push_back(127);
593 for (int i = 0; i < 3; ++i) {
594 localhost_prefix.push_back(0);
596 return IPNumberMatchesPrefix(ip_number, localhost_prefix, 8);
599 case kIPv6AddressSize: {
600 struct in6_addr sin6_addr;
601 memcpy(&sin6_addr, &ip_number[0], kIPv6AddressSize);
602 return !!IN6_IS_ADDR_LOOPBACK(&sin6_addr);
605 default:
606 NOTREACHED();
610 return false;
613 bool IsLocalhostTLD(const std::string& host) {
614 return IsNormalizedLocalhostTLD(NormalizeHostname(host));
617 bool HasGoogleHost(const GURL& url) {
618 static const char* kGoogleHostSuffixes[] = {
619 ".google.com",
620 ".youtube.com",
621 ".gmail.com",
622 ".doubleclick.net",
623 ".gstatic.com",
624 ".googlevideo.com",
625 ".googleusercontent.com",
626 ".googlesyndication.com",
627 ".google-analytics.com",
628 ".googleadservices.com",
629 ".googleapis.com",
630 ".ytimg.com",
632 const std::string& host = url.host();
633 for (const char* suffix : kGoogleHostSuffixes) {
634 if (base::EndsWith(host, suffix, base::CompareCase::INSENSITIVE_ASCII))
635 return true;
637 return false;
640 } // namespace net