[email protected] | a273088 | 2012-01-21 00:56:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 5 | #include "net/dns/host_resolver_impl.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 6 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 7 | #if defined(OS_WIN) |
| 8 | #include <Winsock2.h> |
| 9 | #elif defined(OS_POSIX) |
| 10 | #include <netdb.h> |
| 11 | #endif |
| 12 | |
tfarina | da9e136 | 2017-03-14 16:49:06 | [diff] [blame] | 13 | #if defined(OS_POSIX) |
| 14 | #include <netinet/in.h> |
| 15 | #if !defined(OS_NACL) |
| 16 | #include <net/if.h> |
| 17 | #if !defined(OS_ANDROID) |
| 18 | #include <ifaddrs.h> |
| 19 | #endif // !defined(OS_ANDROID) |
| 20 | #endif // !defined(OS_NACL) |
| 21 | #endif // defined(OS_POSIX) |
| 22 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 23 | #include <cmath> |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 24 | #include <memory> |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 25 | #include <utility> |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 26 | #include <vector> |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 27 | |
[email protected] | 33152acc | 2011-10-20 23:37:12 | [diff] [blame] | 28 | #include "base/bind.h" |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 29 | #include "base/bind_helpers.h" |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 30 | #include "base/callback.h" |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 31 | #include "base/callback_helpers.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 32 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 33 | #include "base/debug/debugger.h" |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 34 | #include "base/debug/leak_annotations.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 35 | #include "base/debug/stack_trace.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 36 | #include "base/macros.h" |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 37 | #include "base/memory/ptr_util.h" |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 38 | #include "base/metrics/field_trial.h" |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 39 | #include "base/metrics/histogram_macros.h" |
| 40 | #include "base/metrics/sparse_histogram.h" |
eroman | e186724 | 2015-04-23 23:07:44 | [diff] [blame] | 41 | #include "base/profiler/scoped_tracker.h" |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 42 | #include "base/single_thread_task_runner.h" |
[email protected] | be528af | 2013-06-11 07:39:48 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 44 | #include "base/strings/utf_string_conversions.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 45 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 46 | #include "base/threading/worker_pool.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 47 | #include "base/time/time.h" |
ssid | 6d6b4010 | 2016-04-05 18:59:56 | [diff] [blame] | 48 | #include "base/trace_event/trace_event.h" |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 49 | #include "base/values.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 50 | #include "net/base/address_family.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 51 | #include "net/base/address_list.h" |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 52 | #include "net/base/host_port_pair.h" |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 53 | #include "net/base/ip_address.h" |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 54 | #include "net/base/ip_endpoint.h" |
[email protected] | 2bb0444 | 2010-08-18 18:01:15 | [diff] [blame] | 55 | #include "net/base/net_errors.h" |
xunjieli | 0b7f5b6 | 2016-12-06 20:43:48 | [diff] [blame] | 56 | #include "net/base/trace_constants.h" |
tfarina | 9ed7f8c5 | 2016-02-19 17:50:18 | [diff] [blame] | 57 | #include "net/base/url_util.h" |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 58 | #include "net/dns/address_sorter.h" |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 59 | #include "net/dns/dns_client.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 60 | #include "net/dns/dns_config_service.h" |
| 61 | #include "net/dns/dns_protocol.h" |
tfarina | 47598f04 | 2015-10-07 23:08:35 | [diff] [blame] | 62 | #include "net/dns/dns_reloader.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 63 | #include "net/dns/dns_response.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 64 | #include "net/dns/dns_transaction.h" |
tfarina | 77021d6 | 2015-10-11 20:19:03 | [diff] [blame] | 65 | #include "net/dns/dns_util.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 66 | #include "net/dns/host_resolver_proc.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 67 | #include "net/log/net_log.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 68 | #include "net/log/net_log_capture_mode.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 69 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 70 | #include "net/log/net_log_parameters_callback.h" |
| 71 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 72 | #include "net/log/net_log_source_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 73 | #include "net/log/net_log_with_source.h" |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 74 | #include "net/socket/client_socket_factory.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 75 | #include "net/socket/datagram_client_socket.h" |
pauljensen | 370f1c7 | 2015-02-17 16:59:14 | [diff] [blame] | 76 | #include "url/url_canon_ip.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 77 | |
| 78 | #if defined(OS_WIN) |
| 79 | #include "net/base/winsock_init.h" |
| 80 | #endif |
| 81 | |
tfarina | da9e136 | 2017-03-14 16:49:06 | [diff] [blame] | 82 | #if defined(OS_ANDROID) |
| 83 | #include "net/android/network_library.h" |
| 84 | #endif |
| 85 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 86 | namespace net { |
| 87 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 88 | namespace { |
| 89 | |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 90 | // Default delay between calls to the system resolver for the same hostname. |
| 91 | // (Can be overridden by field trial.) |
| 92 | const int64_t kDnsDefaultUnresponsiveDelayMs = 6000; |
| 93 | |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 94 | // Limit the size of hostnames that will be resolved to combat issues in |
| 95 | // some platform's resolvers. |
| 96 | const size_t kMaxHostLength = 4096; |
| 97 | |
[email protected] | a273088 | 2012-01-21 00:56:27 | [diff] [blame] | 98 | // Default TTL for successful resolutions with ProcTask. |
| 99 | const unsigned kCacheEntryTTLSeconds = 60; |
| 100 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 101 | // Default TTL for unsuccessful resolutions with ProcTask. |
| 102 | const unsigned kNegativeCacheEntryTTLSeconds = 0; |
| 103 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 104 | // Minimum TTL for successful resolutions with DnsTask. |
| 105 | const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds; |
| 106 | |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 107 | // Time between IPv6 probes, i.e. for how long results of each IPv6 probe are |
| 108 | // cached. |
| 109 | const int kIPv6ProbePeriodMs = 1000; |
| 110 | |
| 111 | // Google DNS address used for IPv6 probes. |
| 112 | const uint8_t kIPv6ProbeAddress[] = |
| 113 | { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, |
| 114 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 }; |
| 115 | |
[email protected] | 24f4bab | 2010-10-15 01:27:11 | [diff] [blame] | 116 | // We use a separate histogram name for each platform to facilitate the |
| 117 | // display of error codes by their symbolic name (since each platform has |
| 118 | // different mappings). |
| 119 | const char kOSErrorsForGetAddrinfoHistogramName[] = |
| 120 | #if defined(OS_WIN) |
| 121 | "Net.OSErrorsForGetAddrinfo_Win"; |
| 122 | #elif defined(OS_MACOSX) |
| 123 | "Net.OSErrorsForGetAddrinfo_Mac"; |
| 124 | #elif defined(OS_LINUX) |
| 125 | "Net.OSErrorsForGetAddrinfo_Linux"; |
| 126 | #else |
| 127 | "Net.OSErrorsForGetAddrinfo"; |
| 128 | #endif |
| 129 | |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 130 | // Gets a list of the likely error codes that getaddrinfo() can return |
| 131 | // (non-exhaustive). These are the error codes that we will track via |
| 132 | // a histogram. |
| 133 | std::vector<int> GetAllGetAddrinfoOSErrors() { |
| 134 | int os_errors[] = { |
| 135 | #if defined(OS_POSIX) |
[email protected] | 23f77116 | 2011-06-02 18:37:51 | [diff] [blame] | 136 | #if !defined(OS_FREEBSD) |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 137 | #if !defined(OS_ANDROID) |
[email protected] | c48aef9 | 2011-11-22 23:41:45 | [diff] [blame] | 138 | // EAI_ADDRFAMILY has been declared obsolete in Android's and |
| 139 | // FreeBSD's netdb.h. |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 140 | EAI_ADDRFAMILY, |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 141 | #endif |
[email protected] | c48aef9 | 2011-11-22 23:41:45 | [diff] [blame] | 142 | // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h. |
[email protected] | 23f77116 | 2011-06-02 18:37:51 | [diff] [blame] | 143 | EAI_NODATA, |
| 144 | #endif |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 145 | EAI_AGAIN, |
| 146 | EAI_BADFLAGS, |
| 147 | EAI_FAIL, |
| 148 | EAI_FAMILY, |
| 149 | EAI_MEMORY, |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 150 | EAI_NONAME, |
| 151 | EAI_SERVICE, |
| 152 | EAI_SOCKTYPE, |
| 153 | EAI_SYSTEM, |
| 154 | #elif defined(OS_WIN) |
| 155 | // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx |
| 156 | WSA_NOT_ENOUGH_MEMORY, |
| 157 | WSAEAFNOSUPPORT, |
| 158 | WSAEINVAL, |
| 159 | WSAESOCKTNOSUPPORT, |
| 160 | WSAHOST_NOT_FOUND, |
| 161 | WSANO_DATA, |
| 162 | WSANO_RECOVERY, |
| 163 | WSANOTINITIALISED, |
| 164 | WSATRY_AGAIN, |
| 165 | WSATYPE_NOT_FOUND, |
| 166 | // The following are not in doc, but might be to appearing in results :-(. |
| 167 | WSA_INVALID_HANDLE, |
| 168 | #endif |
| 169 | }; |
| 170 | |
| 171 | // Ensure all errors are positive, as histogram only tracks positive values. |
| 172 | for (size_t i = 0; i < arraysize(os_errors); ++i) { |
| 173 | os_errors[i] = std::abs(os_errors[i]); |
| 174 | } |
| 175 | |
| 176 | return base::CustomHistogram::ArrayToCustomRanges(os_errors, |
| 177 | arraysize(os_errors)); |
| 178 | } |
| 179 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 180 | enum DnsResolveStatus { |
| 181 | RESOLVE_STATUS_DNS_SUCCESS = 0, |
| 182 | RESOLVE_STATUS_PROC_SUCCESS, |
| 183 | RESOLVE_STATUS_FAIL, |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 184 | RESOLVE_STATUS_SUSPECT_NETBIOS, |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 185 | RESOLVE_STATUS_MAX |
| 186 | }; |
| 187 | |
eroman | 91dd360 | 2015-03-26 03:46:33 | [diff] [blame] | 188 | // ICANN uses this localhost address to indicate a name collision. |
| 189 | // |
| 190 | // The policy in Chromium is to fail host resolving if it resolves to |
| 191 | // this special address. |
| 192 | // |
| 193 | // Not however that IP literals are exempt from this policy, so it is still |
| 194 | // possible to navigate to http://127.0.53.53/ directly. |
| 195 | // |
| 196 | // For more details: https://www.icann.org/news/announcement-2-2014-08-01-en |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 197 | const uint8_t kIcanNameCollisionIp[] = {127, 0, 53, 53}; |
eroman | 91dd360 | 2015-03-26 03:46:33 | [diff] [blame] | 198 | |
eroman | 1efc237c | 2016-12-14 00:00:45 | [diff] [blame] | 199 | bool ContainsIcannNameCollisionIp(const AddressList& addr_list) { |
| 200 | for (const auto& endpoint : addr_list) { |
| 201 | const IPAddress& addr = endpoint.address(); |
| 202 | if (addr.IsIPv4() && IPAddressStartsWith(addr, kIcanNameCollisionIp)) { |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 209 | void UmaAsyncDnsResolveStatus(DnsResolveStatus result) { |
| 210 | UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus", |
| 211 | result, |
| 212 | RESOLVE_STATUS_MAX); |
| 213 | } |
| 214 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 215 | bool ResemblesNetBIOSName(const std::string& hostname) { |
| 216 | return (hostname.size() < 16) && (hostname.find('.') == std::string::npos); |
| 217 | } |
| 218 | |
| 219 | // True if |hostname| ends with either ".local" or ".local.". |
| 220 | bool ResemblesMulticastDNSName(const std::string& hostname) { |
| 221 | DCHECK(!hostname.empty()); |
| 222 | const char kSuffix[] = ".local."; |
| 223 | const size_t kSuffixLen = sizeof(kSuffix) - 1; |
| 224 | const size_t kSuffixLenTrimmed = kSuffixLen - 1; |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 225 | if (hostname.back() == '.') { |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 226 | return hostname.size() > kSuffixLen && |
| 227 | !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix); |
| 228 | } |
| 229 | return hostname.size() > kSuffixLenTrimmed && |
| 230 | !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed, |
| 231 | kSuffix, kSuffixLenTrimmed); |
| 232 | } |
| 233 | |
[email protected] | 34e6136 | 2013-07-24 20:41:56 | [diff] [blame] | 234 | // Attempts to connect a UDP socket to |dest|:53. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 235 | bool IsGloballyReachable(const IPAddress& dest, |
| 236 | const NetLogWithSource& net_log) { |
eroman | 0eb394e | 2015-04-30 23:25:46 | [diff] [blame] | 237 | // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed. |
| 238 | tracked_objects::ScopedTracker tracking_profile_1( |
eroman | 2734a9d | 2015-05-05 19:24:38 | [diff] [blame] | 239 | FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 IsGloballyReachable")); |
eroman | 0eb394e | 2015-04-30 23:25:46 | [diff] [blame] | 240 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 241 | std::unique_ptr<DatagramClientSocket> socket( |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 242 | ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket( |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 243 | DatagramSocket::DEFAULT_BIND, RandIntCallback(), net_log.net_log(), |
[email protected] | 2b74a2f | 2013-07-23 19:37:38 | [diff] [blame] | 244 | net_log.source())); |
[email protected] | 34e6136 | 2013-07-24 20:41:56 | [diff] [blame] | 245 | int rv = socket->Connect(IPEndPoint(dest, 53)); |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 246 | if (rv != OK) |
| 247 | return false; |
| 248 | IPEndPoint endpoint; |
| 249 | rv = socket->GetLocalAddress(&endpoint); |
| 250 | if (rv != OK) |
| 251 | return false; |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 252 | DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily()); |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 253 | const IPAddress& address = endpoint.address(); |
| 254 | |
| 255 | bool is_link_local = |
| 256 | (address.bytes()[0] == 0xFE) && ((address.bytes()[1] & 0xC0) == 0x80); |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 257 | if (is_link_local) |
| 258 | return false; |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 259 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 260 | const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0}; |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 261 | if (IPAddressStartsWith(address, kTeredoPrefix)) |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 262 | return false; |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 263 | |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 264 | return true; |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 267 | // Provide a common macro to simplify code and readability. We must use a |
| 268 | // macro as the underlying HISTOGRAM macro creates static variables. |
| 269 | #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ |
| 270 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100) |
| 271 | |
| 272 | // A macro to simplify code and readability. |
| 273 | #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \ |
| 274 | do { \ |
| 275 | switch (priority) { \ |
| 276 | case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \ |
| 277 | case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \ |
| 278 | case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \ |
| 279 | case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \ |
| 280 | case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \ |
mmenke | 2f23b7029 | 2016-10-26 23:26:42 | [diff] [blame] | 281 | case THROTTLED: DNS_HISTOGRAM(basename "_THROTTLED", time); break; \ |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 282 | } \ |
| 283 | DNS_HISTOGRAM(basename, time); \ |
| 284 | } while (0) |
| 285 | |
| 286 | // Record time from Request creation until a valid DNS response. |
| 287 | void RecordTotalTime(bool had_dns_config, |
| 288 | bool speculative, |
| 289 | base::TimeDelta duration) { |
| 290 | if (had_dns_config) { |
| 291 | if (speculative) { |
| 292 | DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration); |
| 293 | } else { |
| 294 | DNS_HISTOGRAM("AsyncDNS.TotalTime", duration); |
| 295 | } |
| 296 | } else { |
| 297 | if (speculative) { |
| 298 | DNS_HISTOGRAM("DNS.TotalTime_speculative", duration); |
| 299 | } else { |
| 300 | DNS_HISTOGRAM("DNS.TotalTime", duration); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 305 | void RecordTTL(base::TimeDelta ttl) { |
| 306 | UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl, |
| 307 | base::TimeDelta::FromSeconds(1), |
| 308 | base::TimeDelta::FromDays(1), 100); |
| 309 | } |
| 310 | |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 311 | bool ConfigureAsyncDnsNoFallbackFieldTrial() { |
| 312 | const bool kDefault = false; |
| 313 | |
| 314 | // Configure the AsyncDns field trial as follows: |
| 315 | // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true, |
| 316 | // groups AsyncDnsA and AsyncDnsB: return false, |
| 317 | // groups SystemDnsA and SystemDnsB: return false, |
| 318 | // otherwise (trial absent): return default. |
| 319 | std::string group_name = base::FieldTrialList::FindFullName("AsyncDns"); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 320 | if (!group_name.empty()) { |
| 321 | return base::StartsWith(group_name, "AsyncDnsNoFallback", |
| 322 | base::CompareCase::INSENSITIVE_ASCII); |
| 323 | } |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 324 | return kDefault; |
| 325 | } |
| 326 | |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 327 | //----------------------------------------------------------------------------- |
| 328 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 329 | AddressList EnsurePortOnAddressList(const AddressList& list, uint16_t port) { |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 330 | if (list.empty() || list.front().port() == port) |
| 331 | return list; |
| 332 | return AddressList::CopyWithPort(list, port); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 335 | // Returns true if |addresses| contains only IPv4 loopback addresses. |
| 336 | bool IsAllIPv4Loopback(const AddressList& addresses) { |
| 337 | for (unsigned i = 0; i < addresses.size(); ++i) { |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 338 | const IPAddress& address = addresses[i].address(); |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 339 | switch (addresses[i].GetFamily()) { |
| 340 | case ADDRESS_FAMILY_IPV4: |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 341 | if (address.bytes()[0] != 127) |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 342 | return false; |
| 343 | break; |
| 344 | case ADDRESS_FAMILY_IPV6: |
| 345 | return false; |
| 346 | default: |
| 347 | NOTREACHED(); |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
tfarina | da9e136 | 2017-03-14 16:49:06 | [diff] [blame] | 354 | // Returns true if it can determine that only loopback addresses are configured. |
| 355 | // i.e. if only 127.0.0.1 and ::1 are routable. |
| 356 | // Also returns false if it cannot determine this. |
| 357 | bool HaveOnlyLoopbackAddresses() { |
| 358 | #if defined(OS_ANDROID) |
| 359 | return android::HaveOnlyLoopbackAddresses(); |
| 360 | #elif defined(OS_NACL) |
| 361 | NOTIMPLEMENTED(); |
| 362 | return false; |
| 363 | #elif defined(OS_POSIX) |
| 364 | struct ifaddrs* interface_addr = NULL; |
| 365 | int rv = getifaddrs(&interface_addr); |
| 366 | if (rv != 0) { |
| 367 | DVLOG(1) << "getifaddrs() failed with errno = " << errno; |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | bool result = true; |
| 372 | for (struct ifaddrs* interface = interface_addr; |
| 373 | interface != NULL; |
| 374 | interface = interface->ifa_next) { |
| 375 | if (!(IFF_UP & interface->ifa_flags)) |
| 376 | continue; |
| 377 | if (IFF_LOOPBACK & interface->ifa_flags) |
| 378 | continue; |
| 379 | const struct sockaddr* addr = interface->ifa_addr; |
| 380 | if (!addr) |
| 381 | continue; |
| 382 | if (addr->sa_family == AF_INET6) { |
| 383 | // Safe cast since this is AF_INET6. |
| 384 | const struct sockaddr_in6* addr_in6 = |
| 385 | reinterpret_cast<const struct sockaddr_in6*>(addr); |
| 386 | const struct in6_addr* sin6_addr = &addr_in6->sin6_addr; |
| 387 | if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr)) |
| 388 | continue; |
| 389 | } |
| 390 | if (addr->sa_family != AF_INET6 && addr->sa_family != AF_INET) |
| 391 | continue; |
| 392 | |
| 393 | result = false; |
| 394 | break; |
| 395 | } |
| 396 | freeifaddrs(interface_addr); |
| 397 | return result; |
| 398 | #elif defined(OS_WIN) |
| 399 | // TODO(wtc): implement with the GetAdaptersAddresses function. |
| 400 | NOTIMPLEMENTED(); |
| 401 | return false; |
| 402 | #else |
| 403 | NOTIMPLEMENTED(); |
| 404 | return false; |
| 405 | #endif // defined(various platforms) |
| 406 | } |
| 407 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 408 | // Creates NetLog parameters when the resolve failed. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 409 | std::unique_ptr<base::Value> NetLogProcTaskFailedCallback( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 410 | uint32_t attempt_number, |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 411 | int net_error, |
| 412 | int os_error, |
| 413 | NetLogCaptureMode /* capture_mode */) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 414 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 415 | if (attempt_number) |
| 416 | dict->SetInteger("attempt_number", attempt_number); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 417 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 418 | dict->SetInteger("net_error", net_error); |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 419 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 420 | if (os_error) { |
| 421 | dict->SetInteger("os_error", os_error); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 422 | #if defined(OS_POSIX) |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 423 | dict->SetString("os_error_string", gai_strerror(os_error)); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 424 | #elif defined(OS_WIN) |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 425 | // Map the error code to a human-readable string. |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 426 | LPWSTR error_string = nullptr; |
Peter Kasting | be940e9 | 2014-11-20 23:14:08 | [diff] [blame] | 427 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 428 | 0, // Use the internal message table. |
| 429 | os_error, |
| 430 | 0, // Use default language. |
| 431 | (LPWSTR)&error_string, |
| 432 | 0, // Buffer size. |
| 433 | 0); // Arguments (unused). |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 434 | dict->SetString("os_error_string", base::WideToUTF8(error_string)); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 435 | LocalFree(error_string); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 436 | #endif |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 437 | } |
| 438 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 439 | return std::move(dict); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 440 | } |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 441 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 442 | // Creates NetLog parameters when the DnsTask failed. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 443 | std::unique_ptr<base::Value> NetLogDnsTaskFailedCallback( |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 444 | int net_error, |
| 445 | int dns_error, |
| 446 | NetLogCaptureMode /* capture_mode */) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 447 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 448 | dict->SetInteger("net_error", net_error); |
| 449 | if (dns_error) |
| 450 | dict->SetInteger("dns_error", dns_error); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 451 | return std::move(dict); |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 452 | } |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 453 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 454 | // Creates NetLog parameters containing the information in a RequestInfo object, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 455 | // along with the associated NetLogSource. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 456 | std::unique_ptr<base::Value> NetLogRequestInfoCallback( |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 457 | const HostResolver::RequestInfo* info, |
| 458 | NetLogCaptureMode /* capture_mode */) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 459 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 460 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 461 | dict->SetString("host", info->host_port_pair().ToString()); |
| 462 | dict->SetInteger("address_family", |
| 463 | static_cast<int>(info->address_family())); |
| 464 | dict->SetBoolean("allow_cached_response", info->allow_cached_response()); |
| 465 | dict->SetBoolean("is_speculative", info->is_speculative()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 466 | return std::move(dict); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 467 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 468 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 469 | // Creates NetLog parameters for the creation of a HostResolverImpl::Job. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 470 | std::unique_ptr<base::Value> NetLogJobCreationCallback( |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 471 | const NetLogSource& source, |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 472 | const std::string* host, |
| 473 | NetLogCaptureMode /* capture_mode */) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 474 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 475 | source.AddToEventParameters(dict.get()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 476 | dict->SetString("host", *host); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 477 | return std::move(dict); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 478 | } |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 479 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 480 | // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 481 | std::unique_ptr<base::Value> NetLogJobAttachCallback( |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 482 | const NetLogSource& source, |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 483 | RequestPriority priority, |
| 484 | NetLogCaptureMode /* capture_mode */) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 485 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 486 | source.AddToEventParameters(dict.get()); |
[email protected] | 3b04d1f2 | 2013-10-16 00:23:56 | [diff] [blame] | 487 | dict->SetString("priority", RequestPriorityToString(priority)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 488 | return std::move(dict); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 489 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 490 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 491 | // Creates NetLog parameters for the DNS_CONFIG_CHANGED event. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 492 | std::unique_ptr<base::Value> NetLogDnsConfigCallback( |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 493 | const DnsConfig* config, |
| 494 | NetLogCaptureMode /* capture_mode */) { |
tfh | ef3618f | 2016-01-11 23:07:08 | [diff] [blame] | 495 | return config->ToValue(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 496 | } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 497 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 498 | std::unique_ptr<base::Value> NetLogIPv6AvailableCallback( |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 499 | bool ipv6_available, |
| 500 | bool cached, |
| 501 | NetLogCaptureMode /* capture_mode */) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 502 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 503 | dict->SetBoolean("ipv6_available", ipv6_available); |
| 504 | dict->SetBoolean("cached", cached); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 505 | return std::move(dict); |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 506 | } |
| 507 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 508 | // The logging routines are defined here because some requests are resolved |
| 509 | // without a Request object. |
| 510 | |
| 511 | // Logs when a request has just been started. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 512 | void LogStartRequest(const NetLogWithSource& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 513 | const HostResolver::RequestInfo& info) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 514 | source_net_log.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST, |
| 515 | base::Bind(&NetLogRequestInfoCallback, &info)); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // Logs when a request has just completed (before its callback is run). |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 519 | void LogFinishRequest(const NetLogWithSource& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 520 | const HostResolver::RequestInfo& info, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 521 | int net_error) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 522 | source_net_log.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 523 | NetLogEventType::HOST_RESOLVER_IMPL_REQUEST, net_error); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Logs when a request has been cancelled. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 527 | void LogCancelRequest(const NetLogWithSource& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 528 | const HostResolverImpl::RequestInfo& info) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 529 | source_net_log.AddEvent(NetLogEventType::CANCELLED); |
| 530 | source_net_log.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 531 | } |
| 532 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 533 | //----------------------------------------------------------------------------- |
| 534 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 535 | // Keeps track of the highest priority. |
| 536 | class PriorityTracker { |
| 537 | public: |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 538 | explicit PriorityTracker(RequestPriority initial_priority) |
| 539 | : highest_priority_(initial_priority), total_count_(0) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 540 | memset(counts_, 0, sizeof(counts_)); |
| 541 | } |
| 542 | |
| 543 | RequestPriority highest_priority() const { |
| 544 | return highest_priority_; |
| 545 | } |
| 546 | |
| 547 | size_t total_count() const { |
| 548 | return total_count_; |
| 549 | } |
| 550 | |
| 551 | void Add(RequestPriority req_priority) { |
| 552 | ++total_count_; |
| 553 | ++counts_[req_priority]; |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 554 | if (highest_priority_ < req_priority) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 555 | highest_priority_ = req_priority; |
| 556 | } |
| 557 | |
| 558 | void Remove(RequestPriority req_priority) { |
| 559 | DCHECK_GT(total_count_, 0u); |
| 560 | DCHECK_GT(counts_[req_priority], 0u); |
| 561 | --total_count_; |
| 562 | --counts_[req_priority]; |
| 563 | size_t i; |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 564 | for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i) { |
| 565 | } |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 566 | highest_priority_ = static_cast<RequestPriority>(i); |
| 567 | |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 568 | // In absence of requests, default to MINIMUM_PRIORITY. |
| 569 | if (total_count_ == 0) |
| 570 | DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | private: |
| 574 | RequestPriority highest_priority_; |
| 575 | size_t total_count_; |
| 576 | size_t counts_[NUM_PRIORITIES]; |
| 577 | }; |
| 578 | |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 579 | void MakeNotStale(HostCache::EntryStaleness* stale_info) { |
| 580 | if (!stale_info) |
| 581 | return; |
| 582 | stale_info->expired_by = base::TimeDelta::FromSeconds(-1); |
| 583 | stale_info->network_changes = 0; |
| 584 | stale_info->stale_hits = 0; |
| 585 | } |
| 586 | |
juliatuttle | d7fa585 | 2016-07-29 13:16:57 | [diff] [blame] | 587 | // Persist data every five minutes (potentially, cache and learned RTT). |
| 588 | const int64_t kPersistDelaySec = 300; |
| 589 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 590 | } // namespace |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 591 | |
| 592 | //----------------------------------------------------------------------------- |
| 593 | |
tfarina | 9ed7f8c5 | 2016-02-19 17:50:18 | [diff] [blame] | 594 | bool ResolveLocalHostname(base::StringPiece host, |
| 595 | uint16_t port, |
| 596 | AddressList* address_list) { |
tfarina | 9ed7f8c5 | 2016-02-19 17:50:18 | [diff] [blame] | 597 | address_list->clear(); |
| 598 | |
| 599 | bool is_local6; |
| 600 | if (!IsLocalHostname(host, &is_local6)) |
| 601 | return false; |
| 602 | |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 603 | address_list->push_back(IPEndPoint(IPAddress::IPv6Localhost(), port)); |
tfarina | 9ed7f8c5 | 2016-02-19 17:50:18 | [diff] [blame] | 604 | if (!is_local6) { |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 605 | address_list->push_back(IPEndPoint(IPAddress::IPv4Localhost(), port)); |
tfarina | 9ed7f8c5 | 2016-02-19 17:50:18 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | return true; |
| 609 | } |
| 610 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 611 | const unsigned HostResolverImpl::kMaximumDnsFailures = 16; |
| 612 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 613 | // Holds the data for a request that could not be completed synchronously. |
| 614 | // It is owned by a Job. Canceled Requests are only marked as canceled rather |
| 615 | // than removed from the Job's |requests_| list. |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 616 | class HostResolverImpl::RequestImpl : public HostResolver::Request { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 617 | public: |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 618 | RequestImpl(const NetLogWithSource& source_net_log, |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 619 | const RequestInfo& info, |
| 620 | RequestPriority priority, |
| 621 | const CompletionCallback& callback, |
| 622 | AddressList* addresses, |
| 623 | Job* job) |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 624 | : source_net_log_(source_net_log), |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 625 | info_(info), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 626 | priority_(priority), |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 627 | job_(job), |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 628 | callback_(callback), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 629 | addresses_(addresses), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 630 | request_time_(base::TimeTicks::Now()) {} |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 631 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 632 | ~RequestImpl() override; |
| 633 | |
| 634 | void ChangeRequestPriority(RequestPriority priority) override; |
| 635 | |
| 636 | void OnJobCancelled(Job* job) { |
| 637 | DCHECK_EQ(job_, job); |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 638 | job_ = nullptr; |
| 639 | addresses_ = nullptr; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 640 | callback_.Reset(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 641 | } |
| 642 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 643 | // Prepare final AddressList and call completion callback. |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 644 | void OnJobCompleted(Job* job, int error, const AddressList& addr_list) { |
| 645 | DCHECK_EQ(job_, job); |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 646 | if (error == OK) |
| 647 | *addresses_ = EnsurePortOnAddressList(addr_list, info_.port()); |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 648 | job_ = nullptr; |
| 649 | addresses_ = nullptr; |
| 650 | base::ResetAndReturn(&callback_).Run(error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 651 | } |
| 652 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 653 | Job* job() const { |
| 654 | return job_; |
| 655 | } |
| 656 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 657 | // NetLog for the source, passed in HostResolver::Resolve. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 658 | const NetLogWithSource& source_net_log() { return source_net_log_; } |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 659 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 660 | const RequestInfo& info() const { |
| 661 | return info_; |
| 662 | } |
| 663 | |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 664 | RequestPriority priority() const { return priority_; } |
juliatuttle | c53b19a7 | 2016-05-05 13:51:31 | [diff] [blame] | 665 | void set_priority(RequestPriority priority) { priority_ = priority; } |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 666 | |
| 667 | base::TimeTicks request_time() const { return request_time_; } |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 668 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 669 | private: |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 670 | const NetLogWithSource source_net_log_; |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 671 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 672 | // The request info that started the request. |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 673 | const RequestInfo info_; |
| 674 | |
juliatuttle | c53b19a7 | 2016-05-05 13:51:31 | [diff] [blame] | 675 | RequestPriority priority_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 676 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 677 | // The resolve job that this request is dependent on. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 678 | Job* job_; |
| 679 | |
| 680 | // The user's callback to invoke when the request completes. |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 681 | CompletionCallback callback_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 682 | |
| 683 | // The address list to save result into. |
| 684 | AddressList* addresses_; |
| 685 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 686 | const base::TimeTicks request_time_; |
| 687 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 688 | DISALLOW_COPY_AND_ASSIGN(RequestImpl); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 689 | }; |
| 690 | |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 691 | //------------------------------------------------------------------------------ |
| 692 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 693 | // Calls HostResolverProc using a worker task runner. Performs retries if |
| 694 | // necessary. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 695 | // |
| 696 | // Whenever we try to resolve the host, we post a delayed task to check if host |
| 697 | // resolution (OnLookupComplete) is completed or not. If the original attempt |
| 698 | // hasn't completed, then we start another attempt for host resolution. We take |
| 699 | // the results from the first attempt that finishes and ignore the results from |
| 700 | // all other attempts. |
| 701 | // |
| 702 | // TODO(szym): Move to separate source file for testing and mocking. |
| 703 | // |
| 704 | class HostResolverImpl::ProcTask |
| 705 | : public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 706 | public: |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 707 | typedef base::Callback<void(int net_error, |
| 708 | const AddressList& addr_list)> Callback; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 709 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 710 | ProcTask(const Key& key, |
| 711 | const ProcTaskParams& params, |
| 712 | const Callback& callback, |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 713 | scoped_refptr<base::TaskRunner> worker_task_runner, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 714 | const NetLogWithSource& job_net_log) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 715 | : key_(key), |
| 716 | params_(params), |
| 717 | callback_(callback), |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 718 | worker_task_runner_(std::move(worker_task_runner)), |
| 719 | network_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 720 | attempt_number_(0), |
| 721 | completed_attempt_number_(0), |
| 722 | completed_attempt_error_(ERR_UNEXPECTED), |
| 723 | had_non_speculative_request_(false), |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 724 | net_log_(job_net_log) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 725 | if (!params_.resolver_proc.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 726 | params_.resolver_proc = HostResolverProc::GetDefault(); |
| 727 | // If default is unset, use the system proc. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 728 | if (!params_.resolver_proc.get()) |
[email protected] | 1ee9afa1 | 2013-04-16 14:18:06 | [diff] [blame] | 729 | params_.resolver_proc = new SystemHostResolverProc(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 730 | } |
| 731 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 732 | void Start() { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 733 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 734 | net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 735 | StartLookupAttempt(); |
| 736 | } |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 737 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 738 | // Cancels this ProcTask. It will be orphaned. Any outstanding resolve |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 739 | // attempts running on worker thread will continue running. Only once all the |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 740 | // attempts complete will the final reference to this ProcTask be released. |
| 741 | void Cancel() { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 742 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 743 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 744 | if (was_canceled() || was_completed()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 745 | return; |
| 746 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 747 | callback_.Reset(); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 748 | net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | void set_had_non_speculative_request() { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 752 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 753 | had_non_speculative_request_ = true; |
| 754 | } |
| 755 | |
| 756 | bool was_canceled() const { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 757 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 758 | return callback_.is_null(); |
| 759 | } |
| 760 | |
| 761 | bool was_completed() const { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 762 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 763 | return completed_attempt_number_ > 0; |
| 764 | } |
| 765 | |
| 766 | private: |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 767 | friend class base::RefCountedThreadSafe<ProcTask>; |
| 768 | ~ProcTask() {} |
| 769 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 770 | void StartLookupAttempt() { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 771 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 772 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 773 | ++attempt_number_; |
| 774 | // Dispatch the lookup attempt to a worker thread. |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 775 | if (!worker_task_runner_->PostTask( |
| 776 | FROM_HERE, base::Bind(&ProcTask::DoLookup, this, start_time, |
| 777 | attempt_number_))) { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 778 | NOTREACHED(); |
| 779 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 780 | // Since this method may have been called from Resolve(), can't just call |
| 781 | // OnLookupComplete(). Instead, must wait until Resolve() has returned |
| 782 | // (IO_PENDING). |
| 783 | network_task_runner_->PostTask( |
| 784 | FROM_HERE, |
| 785 | base::Bind(&ProcTask::OnLookupComplete, this, AddressList(), |
| 786 | start_time, attempt_number_, ERR_UNEXPECTED, 0)); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 787 | return; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 788 | } |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 789 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 790 | net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_ATTEMPT_STARTED, |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 791 | NetLog::IntCallback("attempt_number", attempt_number_)); |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 792 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 793 | // If the results aren't received within a given time, RetryIfNotComplete |
| 794 | // will start a new attempt if none of the outstanding attempts have |
| 795 | // completed yet. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 796 | if (attempt_number_ <= params_.max_retry_attempts) { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 797 | network_task_runner_->PostDelayedTask( |
| 798 | FROM_HERE, base::Bind(&ProcTask::RetryIfNotComplete, this), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 799 | params_.unresponsive_delay); |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 800 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 801 | } |
| 802 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 803 | // WARNING: In production, this code runs on a worker pool. The shutdown code |
| 804 | // cannot wait for it to finish, so this code must be very careful about using |
| 805 | // other objects (like MessageLoops, Singletons, etc). During shutdown these |
| 806 | // objects may no longer exist. Multiple DoLookups() could be running in |
| 807 | // parallel, so any state inside of |this| must not mutate . |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 808 | void DoLookup(const base::TimeTicks& start_time, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 809 | const uint32_t attempt_number) { |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 810 | AddressList results; |
| 811 | int os_error = 0; |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 812 | // Running on a worker task runner. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 813 | int error = params_.resolver_proc->Resolve(key_.hostname, |
| 814 | key_.address_family, |
| 815 | key_.host_resolver_flags, |
| 816 | &results, |
| 817 | &os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 818 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 819 | network_task_runner_->PostTask( |
| 820 | FROM_HERE, base::Bind(&ProcTask::OnLookupComplete, this, results, |
| 821 | start_time, attempt_number, error, os_error)); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 822 | } |
| 823 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 824 | // Makes next attempt if DoLookup() has not finished. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 825 | void RetryIfNotComplete() { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 826 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 827 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 828 | if (was_completed() || was_canceled()) |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 829 | return; |
| 830 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 831 | params_.unresponsive_delay *= params_.retry_factor; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 832 | StartLookupAttempt(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 833 | } |
| 834 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 835 | // Callback for when DoLookup() completes (runs on task runner thread). |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 836 | void OnLookupComplete(const AddressList& results, |
| 837 | const base::TimeTicks& start_time, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 838 | const uint32_t attempt_number, |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 839 | int error, |
| 840 | const int os_error) { |
xunjieli | 0b7f5b6 | 2016-12-06 20:43:48 | [diff] [blame] | 841 | TRACE_EVENT0(kNetTracingCategory, "ProcTask::OnLookupComplete"); |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 842 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 49b70b22 | 2013-05-07 21:24:23 | [diff] [blame] | 843 | // If results are empty, we should return an error. |
| 844 | bool empty_list_on_ok = (error == OK && results.empty()); |
| 845 | UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok); |
| 846 | if (empty_list_on_ok) |
| 847 | error = ERR_NAME_NOT_RESOLVED; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 848 | |
| 849 | bool was_retry_attempt = attempt_number > 1; |
| 850 | |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 851 | // Ideally the following code would be part of host_resolver_proc.cc, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 852 | // however it isn't safe to call NetworkChangeNotifier from worker threads. |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 853 | // So do it here on the IO thread instead. |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 854 | if (error != OK && NetworkChangeNotifier::IsOffline()) |
| 855 | error = ERR_INTERNET_DISCONNECTED; |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 856 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 857 | // If this is the first attempt that is finishing later, then record data |
| 858 | // for the first attempt. Won't contaminate with retry attempt's data. |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 859 | if (!was_retry_attempt) |
| 860 | RecordPerformanceHistograms(start_time, error, os_error); |
| 861 | |
| 862 | RecordAttemptHistograms(start_time, attempt_number, error, os_error); |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 863 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 864 | if (was_canceled()) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 865 | return; |
| 866 | |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 867 | NetLogParametersCallback net_log_callback; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 868 | if (error != OK) { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 869 | net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, |
| 870 | attempt_number, |
| 871 | error, |
| 872 | os_error); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 873 | } else { |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 874 | net_log_callback = NetLog::IntCallback("attempt_number", attempt_number); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 875 | } |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 876 | net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_ATTEMPT_FINISHED, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 877 | net_log_callback); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 878 | |
| 879 | if (was_completed()) |
| 880 | return; |
| 881 | |
| 882 | // Copy the results from the first worker thread that resolves the host. |
| 883 | results_ = results; |
| 884 | completed_attempt_number_ = attempt_number; |
| 885 | completed_attempt_error_ = error; |
| 886 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 887 | if (was_retry_attempt) { |
| 888 | // If retry attempt finishes before 1st attempt, then get stats on how |
| 889 | // much time is saved by having spawned an extra attempt. |
| 890 | retry_attempt_finished_time_ = base::TimeTicks::Now(); |
| 891 | } |
| 892 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 893 | if (error != OK) { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 894 | net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, |
| 895 | 0, error, os_error); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 896 | } else { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 897 | net_log_callback = results_.CreateNetLogCallback(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 898 | } |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 899 | net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 900 | net_log_callback); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 901 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 902 | callback_.Run(error, results_); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 903 | } |
| 904 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 905 | void RecordPerformanceHistograms(const base::TimeTicks& start_time, |
| 906 | const int error, |
| 907 | const int os_error) const { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 908 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
asvitkine | c0fb802 | 2014-08-26 04:39:35 | [diff] [blame] | 909 | enum Category { // Used in UMA_HISTOGRAM_ENUMERATION. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 910 | RESOLVE_SUCCESS, |
| 911 | RESOLVE_FAIL, |
| 912 | RESOLVE_SPECULATIVE_SUCCESS, |
| 913 | RESOLVE_SPECULATIVE_FAIL, |
| 914 | RESOLVE_MAX, // Bounding value. |
| 915 | }; |
| 916 | int category = RESOLVE_MAX; // Illegal value for later DCHECK only. |
| 917 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 918 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
| 919 | if (error == OK) { |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 920 | if (had_non_speculative_request_) { |
| 921 | category = RESOLVE_SUCCESS; |
| 922 | DNS_HISTOGRAM("DNS.ResolveSuccess", duration); |
| 923 | } else { |
| 924 | category = RESOLVE_SPECULATIVE_SUCCESS; |
| 925 | DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration); |
| 926 | } |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 927 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 928 | // Log DNS lookups based on |address_family|. This will help us determine |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 929 | // if IPv4 or IPv4/6 lookups are faster or slower. |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 930 | switch (key_.address_family) { |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 931 | case ADDRESS_FAMILY_IPV4: |
| 932 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration); |
| 933 | break; |
| 934 | case ADDRESS_FAMILY_IPV6: |
| 935 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration); |
| 936 | break; |
| 937 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 938 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration); |
| 939 | break; |
| 940 | } |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 941 | } else { |
| 942 | if (had_non_speculative_request_) { |
| 943 | category = RESOLVE_FAIL; |
| 944 | DNS_HISTOGRAM("DNS.ResolveFail", duration); |
| 945 | } else { |
| 946 | category = RESOLVE_SPECULATIVE_FAIL; |
| 947 | DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration); |
| 948 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 949 | // Log DNS lookups based on |address_family|. This will help us determine |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 950 | // if IPv4 or IPv4/6 lookups are faster or slower. |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 951 | switch (key_.address_family) { |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 952 | case ADDRESS_FAMILY_IPV4: |
| 953 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration); |
| 954 | break; |
| 955 | case ADDRESS_FAMILY_IPV6: |
| 956 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration); |
| 957 | break; |
| 958 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 959 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration); |
| 960 | break; |
| 961 | } |
[email protected] | c833e32 | 2010-10-16 23:51:36 | [diff] [blame] | 962 | UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName, |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 963 | std::abs(os_error), |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 964 | GetAllGetAddrinfoOSErrors()); |
| 965 | } |
[email protected] | 051b6ab | 2010-10-18 16:50:46 | [diff] [blame] | 966 | DCHECK_LT(category, static_cast<int>(RESOLVE_MAX)); // Be sure it was set. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 967 | |
| 968 | UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category, RESOLVE_MAX); |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 969 | } |
| 970 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 971 | void RecordAttemptHistograms(const base::TimeTicks& start_time, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 972 | const uint32_t attempt_number, |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 973 | const int error, |
| 974 | const int os_error) const { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 975 | DCHECK(network_task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 976 | bool first_attempt_to_complete = |
| 977 | completed_attempt_number_ == attempt_number; |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 978 | bool is_first_attempt = (attempt_number == 1); |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 979 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 980 | if (first_attempt_to_complete) { |
| 981 | // If this was first attempt to complete, then record the resolution |
| 982 | // status of the attempt. |
| 983 | if (completed_attempt_error_ == OK) { |
| 984 | UMA_HISTOGRAM_ENUMERATION( |
| 985 | "DNS.AttemptFirstSuccess", attempt_number, 100); |
| 986 | } else { |
| 987 | UMA_HISTOGRAM_ENUMERATION( |
| 988 | "DNS.AttemptFirstFailure", attempt_number, 100); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | if (error == OK) |
| 993 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100); |
| 994 | else |
| 995 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100); |
| 996 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 997 | // If first attempt didn't finish before retry attempt, then calculate stats |
| 998 | // on how much time is saved by having spawned an extra attempt. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 999 | if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) { |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 1000 | DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry", |
| 1001 | base::TimeTicks::Now() - retry_attempt_finished_time_); |
| 1002 | } |
| 1003 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1004 | if (was_canceled() || !first_attempt_to_complete) { |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 1005 | // Count those attempts which completed after the job was already canceled |
| 1006 | // OR after the job was already completed by an earlier attempt (so in |
| 1007 | // effect). |
| 1008 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100); |
| 1009 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1010 | // Record if job is canceled. |
| 1011 | if (was_canceled()) |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 1012 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100); |
| 1013 | } |
| 1014 | |
| 1015 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
| 1016 | if (error == OK) |
| 1017 | DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration); |
| 1018 | else |
| 1019 | DNS_HISTOGRAM("DNS.AttemptFailDuration", duration); |
| 1020 | } |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 1021 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 1022 | // Set on the task runner thread, read on the worker thread. |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1023 | Key key_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1024 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1025 | // Holds an owning reference to the HostResolverProc that we are going to use. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1026 | // This may not be the current resolver procedure by the time we call |
| 1027 | // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning |
| 1028 | // reference ensures that it remains valid until we are done. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1029 | ProcTaskParams params_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1030 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1031 | // The listener to the results of this ProcTask. |
| 1032 | Callback callback_; |
| 1033 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1034 | // Task runner for the call to the HostResolverProc. |
| 1035 | scoped_refptr<base::TaskRunner> worker_task_runner_; |
| 1036 | |
| 1037 | // Used to post events onto the network thread. |
| 1038 | scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 1039 | |
| 1040 | // Keeps track of the number of attempts we have made so far to resolve the |
| 1041 | // host. Whenever we start an attempt to resolve the host, we increase this |
| 1042 | // number. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 1043 | uint32_t attempt_number_; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 1044 | |
| 1045 | // The index of the attempt which finished first (or 0 if the job is still in |
| 1046 | // progress). |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 1047 | uint32_t completed_attempt_number_; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 1048 | |
| 1049 | // The result (a net error code) from the first attempt to complete. |
| 1050 | int completed_attempt_error_; |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 1051 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 1052 | // The time when retry attempt was finished. |
| 1053 | base::TimeTicks retry_attempt_finished_time_; |
| 1054 | |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 1055 | // True if a non-speculative request was ever attached to this job |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1056 | // (regardless of whether or not it was later canceled. |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 1057 | // This boolean is used for histogramming the duration of jobs used to |
| 1058 | // service non-speculative requests. |
| 1059 | bool had_non_speculative_request_; |
| 1060 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1061 | AddressList results_; |
| 1062 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1063 | NetLogWithSource net_log_; |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1064 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1065 | DISALLOW_COPY_AND_ASSIGN(ProcTask); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1066 | }; |
| 1067 | |
| 1068 | //----------------------------------------------------------------------------- |
| 1069 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1070 | // Wraps a call to HaveOnlyLoopbackAddresses to be executed on a |
| 1071 | // |worker_task_runner|, as it takes 40-100ms and should not block |
| 1072 | // initialization. |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1073 | class HostResolverImpl::LoopbackProbeJob { |
| 1074 | public: |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1075 | LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver, |
| 1076 | base::TaskRunner* worker_task_runner) |
| 1077 | : resolver_(resolver), result_(false) { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1078 | DCHECK(resolver.get()); |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1079 | |
| 1080 | // |worker_task_runner| may posts tasks to the WorkerPool, so need this to |
| 1081 | // avoid reporting worker pool leaks in tests. The WorkerPool doesn't have a |
| 1082 | // flushing API, so can't do anything about them, other than using another |
| 1083 | // task runner. |
| 1084 | // http://crbug.com/248513 |
| 1085 | ANNOTATE_SCOPED_MEMORY_LEAK; |
| 1086 | |
| 1087 | worker_task_runner->PostTaskAndReply( |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1088 | FROM_HERE, |
| 1089 | base::Bind(&LoopbackProbeJob::DoProbe, base::Unretained(this)), |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1090 | base::Bind(&LoopbackProbeJob::OnProbeComplete, base::Owned(this))); |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | virtual ~LoopbackProbeJob() {} |
| 1094 | |
| 1095 | private: |
| 1096 | // Runs on worker thread. |
| 1097 | void DoProbe() { |
| 1098 | result_ = HaveOnlyLoopbackAddresses(); |
| 1099 | } |
| 1100 | |
| 1101 | void OnProbeComplete() { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1102 | if (!resolver_.get()) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1103 | return; |
| 1104 | resolver_->SetHaveOnlyLoopbackAddresses(result_); |
| 1105 | } |
| 1106 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 1107 | // Used/set only on task runner thread. |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1108 | base::WeakPtr<HostResolverImpl> resolver_; |
| 1109 | |
| 1110 | bool result_; |
| 1111 | |
| 1112 | DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob); |
| 1113 | }; |
| 1114 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1115 | //----------------------------------------------------------------------------- |
| 1116 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1117 | // Resolves the hostname using DnsTransaction. |
| 1118 | // TODO(szym): This could be moved to separate source file as well. |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1119 | class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1120 | public: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1121 | class Delegate { |
| 1122 | public: |
| 1123 | virtual void OnDnsTaskComplete(base::TimeTicks start_time, |
| 1124 | int net_error, |
| 1125 | const AddressList& addr_list, |
| 1126 | base::TimeDelta ttl) = 0; |
| 1127 | |
| 1128 | // Called when the first of two jobs succeeds. If the first completed |
| 1129 | // transaction fails, this is not called. Also not called when the DnsTask |
| 1130 | // only needs to run one transaction. |
| 1131 | virtual void OnFirstDnsTransactionComplete() = 0; |
| 1132 | |
| 1133 | protected: |
| 1134 | Delegate() {} |
| 1135 | virtual ~Delegate() {} |
| 1136 | }; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1137 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1138 | DnsTask(DnsClient* client, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1139 | const Key& key, |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1140 | Delegate* delegate, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1141 | const NetLogWithSource& job_net_log) |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1142 | : client_(client), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1143 | key_(key), |
| 1144 | delegate_(delegate), |
| 1145 | net_log_(job_net_log), |
| 1146 | num_completed_transactions_(0), |
| 1147 | task_start_time_(base::TimeTicks::Now()) { |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1148 | DCHECK(client); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1149 | DCHECK(delegate_); |
[email protected] | 1affed6 | 2013-08-21 03:24:50 | [diff] [blame] | 1150 | } |
| 1151 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1152 | bool needs_two_transactions() const { |
| 1153 | return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED; |
| 1154 | } |
| 1155 | |
| 1156 | bool needs_another_transaction() const { |
| 1157 | return needs_two_transactions() && !transaction_aaaa_; |
| 1158 | } |
| 1159 | |
| 1160 | void StartFirstTransaction() { |
| 1161 | DCHECK_EQ(0u, num_completed_transactions_); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1162 | net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1163 | if (key_.address_family == ADDRESS_FAMILY_IPV6) { |
| 1164 | StartAAAA(); |
| 1165 | } else { |
| 1166 | StartA(); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | void StartSecondTransaction() { |
| 1171 | DCHECK(needs_two_transactions()); |
| 1172 | StartAAAA(); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | private: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1176 | void StartA() { |
| 1177 | DCHECK(!transaction_a_); |
| 1178 | DCHECK_NE(ADDRESS_FAMILY_IPV6, key_.address_family); |
| 1179 | transaction_a_ = CreateTransaction(ADDRESS_FAMILY_IPV4); |
| 1180 | transaction_a_->Start(); |
| 1181 | } |
| 1182 | |
| 1183 | void StartAAAA() { |
| 1184 | DCHECK(!transaction_aaaa_); |
| 1185 | DCHECK_NE(ADDRESS_FAMILY_IPV4, key_.address_family); |
| 1186 | transaction_aaaa_ = CreateTransaction(ADDRESS_FAMILY_IPV6); |
| 1187 | transaction_aaaa_->Start(); |
| 1188 | } |
| 1189 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 1190 | std::unique_ptr<DnsTransaction> CreateTransaction(AddressFamily family) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1191 | DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family); |
| 1192 | return client_->GetTransactionFactory()->CreateTransaction( |
| 1193 | key_.hostname, |
| 1194 | family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA : |
| 1195 | dns_protocol::kTypeA, |
| 1196 | base::Bind(&DnsTask::OnTransactionComplete, base::Unretained(this), |
| 1197 | base::TimeTicks::Now()), |
| 1198 | net_log_); |
| 1199 | } |
| 1200 | |
| 1201 | void OnTransactionComplete(const base::TimeTicks& start_time, |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1202 | DnsTransaction* transaction, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1203 | int net_error, |
| 1204 | const DnsResponse* response) { |
[email protected] | add7653 | 2012-03-30 14:47:47 | [diff] [blame] | 1205 | DCHECK(transaction); |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1206 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1207 | if (net_error != OK) { |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1208 | DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1209 | OnFailure(net_error, DnsResponse::DNS_PARSE_OK); |
| 1210 | return; |
[email protected] | 6c41190 | 2012-08-14 22:36:36 | [diff] [blame] | 1211 | } |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1212 | |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1213 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration); |
| 1214 | switch (transaction->GetType()) { |
| 1215 | case dns_protocol::kTypeA: |
| 1216 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration); |
| 1217 | break; |
| 1218 | case dns_protocol::kTypeAAAA: |
| 1219 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration); |
| 1220 | break; |
| 1221 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1222 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1223 | AddressList addr_list; |
| 1224 | base::TimeDelta ttl; |
| 1225 | DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl); |
| 1226 | UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList", |
| 1227 | result, |
| 1228 | DnsResponse::DNS_PARSE_RESULT_MAX); |
| 1229 | if (result != DnsResponse::DNS_PARSE_OK) { |
| 1230 | // Fail even if the other query succeeds. |
| 1231 | OnFailure(ERR_DNS_MALFORMED_RESPONSE, result); |
| 1232 | return; |
| 1233 | } |
| 1234 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1235 | ++num_completed_transactions_; |
| 1236 | if (num_completed_transactions_ == 1) { |
| 1237 | ttl_ = ttl; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1238 | } else { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1239 | ttl_ = std::min(ttl_, ttl); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1240 | } |
| 1241 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1242 | if (transaction->GetType() == dns_protocol::kTypeA) { |
| 1243 | DCHECK_EQ(transaction_a_.get(), transaction); |
| 1244 | // Place IPv4 addresses after IPv6. |
| 1245 | addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end()); |
| 1246 | } else { |
| 1247 | DCHECK_EQ(transaction_aaaa_.get(), transaction); |
| 1248 | // Place IPv6 addresses before IPv4. |
| 1249 | addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end()); |
| 1250 | } |
| 1251 | |
| 1252 | if (needs_two_transactions() && num_completed_transactions_ == 1) { |
| 1253 | // No need to repeat the suffix search. |
| 1254 | key_.hostname = transaction->GetHostname(); |
| 1255 | delegate_->OnFirstDnsTransactionComplete(); |
| 1256 | return; |
| 1257 | } |
| 1258 | |
| 1259 | if (addr_list_.empty()) { |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1260 | // TODO(szym): Don't fallback to ProcTask in this case. |
| 1261 | OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1262 | return; |
| 1263 | } |
| 1264 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1265 | // If there are multiple addresses, and at least one is IPv6, need to sort |
| 1266 | // them. Note that IPv6 addresses are always put before IPv4 ones, so it's |
| 1267 | // sufficient to just check the family of the first address. |
| 1268 | if (addr_list_.size() > 1 && |
| 1269 | addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) { |
| 1270 | // Sort addresses if needed. Sort could complete synchronously. |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1271 | client_->GetAddressSorter()->Sort( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1272 | addr_list_, |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 1273 | base::Bind(&DnsTask::OnSortComplete, |
| 1274 | AsWeakPtr(), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1275 | base::TimeTicks::Now())); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1276 | } else { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1277 | OnSuccess(addr_list_); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | void OnSortComplete(base::TimeTicks start_time, |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1282 | bool success, |
| 1283 | const AddressList& addr_list) { |
| 1284 | if (!success) { |
| 1285 | DNS_HISTOGRAM("AsyncDNS.SortFailure", |
| 1286 | base::TimeTicks::Now() - start_time); |
| 1287 | OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK); |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | DNS_HISTOGRAM("AsyncDNS.SortSuccess", |
| 1292 | base::TimeTicks::Now() - start_time); |
| 1293 | |
| 1294 | // AddressSorter prunes unusable destinations. |
| 1295 | if (addr_list.empty()) { |
| 1296 | LOG(WARNING) << "Address list empty after RFC3484 sort"; |
| 1297 | OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK); |
| 1298 | return; |
| 1299 | } |
| 1300 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1301 | OnSuccess(addr_list); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | void OnFailure(int net_error, DnsResponse::Result result) { |
| 1305 | DCHECK_NE(OK, net_error); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1306 | net_log_.EndEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1307 | NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1308 | base::Bind(&NetLogDnsTaskFailedCallback, net_error, result)); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1309 | delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(), |
| 1310 | base::TimeDelta()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1311 | } |
| 1312 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1313 | void OnSuccess(const AddressList& addr_list) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1314 | net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK, |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1315 | addr_list.CreateNetLogCallback()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1316 | delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | DnsClient* client_; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1320 | Key key_; |
| 1321 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1322 | // The listener to the results of this DnsTask. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1323 | Delegate* delegate_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1324 | const NetLogWithSource net_log_; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1325 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 1326 | std::unique_ptr<DnsTransaction> transaction_a_; |
| 1327 | std::unique_ptr<DnsTransaction> transaction_aaaa_; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1328 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1329 | unsigned num_completed_transactions_; |
| 1330 | |
| 1331 | // These are updated as each transaction completes. |
| 1332 | base::TimeDelta ttl_; |
| 1333 | // IPv6 addresses must appear first in the list. |
| 1334 | AddressList addr_list_; |
| 1335 | |
| 1336 | base::TimeTicks task_start_time_; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1337 | |
| 1338 | DISALLOW_COPY_AND_ASSIGN(DnsTask); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1339 | }; |
| 1340 | |
| 1341 | //----------------------------------------------------------------------------- |
| 1342 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1343 | // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1344 | class HostResolverImpl::Job : public PrioritizedDispatcher::Job, |
| 1345 | public HostResolverImpl::DnsTask::Delegate { |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1346 | public: |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1347 | // Creates new job for |key| where |request_net_log| is bound to the |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1348 | // request that spawned it. |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1349 | Job(const base::WeakPtr<HostResolverImpl>& resolver, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1350 | const Key& key, |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 1351 | RequestPriority priority, |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1352 | scoped_refptr<base::TaskRunner> worker_task_runner, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1353 | const NetLogWithSource& source_net_log) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1354 | : resolver_(resolver), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1355 | key_(key), |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 1356 | priority_tracker_(priority), |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1357 | worker_task_runner_(std::move(worker_task_runner)), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1358 | had_non_speculative_request_(false), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1359 | had_dns_config_(false), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1360 | num_occupied_job_slots_(0), |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1361 | dns_task_error_(OK), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1362 | creation_time_(base::TimeTicks::Now()), |
| 1363 | priority_change_time_(creation_time_), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1364 | net_log_( |
| 1365 | NetLogWithSource::Make(source_net_log.net_log(), |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1366 | NetLogSourceType::HOST_RESOLVER_IMPL_JOB)) { |
| 1367 | source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_CREATE_JOB); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1368 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1369 | net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB, |
| 1370 | base::Bind(&NetLogJobCreationCallback, |
| 1371 | source_net_log.source(), &key_.hostname)); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1372 | } |
| 1373 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1374 | ~Job() override { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1375 | if (is_running()) { |
| 1376 | // |resolver_| was destroyed with this Job still in flight. |
| 1377 | // Clean-up, record in the log, but don't run any callbacks. |
| 1378 | if (is_proc_running()) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1379 | proc_task_->Cancel(); |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 1380 | proc_task_ = nullptr; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1381 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1382 | // Clean up now for nice NetLog. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1383 | KillDnsTask(); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1384 | net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1385 | ERR_ABORTED); |
| 1386 | } else if (is_queued()) { |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 1387 | // |resolver_| was destroyed without running this Job. |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1388 | // TODO(szym): is there any benefit in having this distinction? |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1389 | net_log_.AddEvent(NetLogEventType::CANCELLED); |
| 1390 | net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1391 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1392 | // else CompleteRequests logged EndEvent. |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1393 | if (!requests_.empty()) { |
| 1394 | // Log any remaining Requests as cancelled. |
| 1395 | for (RequestImpl* req : requests_) { |
| 1396 | DCHECK_EQ(this, req->job()); |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1397 | LogCancelRequest(req->source_net_log(), req->info()); |
maksim.sisov | 320d596 | 2016-08-15 18:02:51 | [diff] [blame] | 1398 | req->OnJobCancelled(this); |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1399 | } |
| 1400 | requests_.clear(); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1401 | } |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1402 | } |
| 1403 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1404 | // Add this job to the dispatcher. If "at_head" is true, adds at the front |
| 1405 | // of the queue. |
| 1406 | void Schedule(bool at_head) { |
| 1407 | DCHECK(!is_queued()); |
| 1408 | PrioritizedDispatcher::Handle handle; |
| 1409 | if (!at_head) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1410 | handle = resolver_->dispatcher_->Add(this, priority()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1411 | } else { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1412 | handle = resolver_->dispatcher_->AddAtHead(this, priority()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1413 | } |
| 1414 | // The dispatcher could have started |this| in the above call to Add, which |
| 1415 | // could have called Schedule again. In that case |handle| will be null, |
| 1416 | // but |handle_| may have been set by the other nested call to Schedule. |
| 1417 | if (!handle.is_null()) { |
| 1418 | DCHECK(handle_.is_null()); |
| 1419 | handle_ = handle; |
| 1420 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1421 | } |
| 1422 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1423 | void AddRequest(RequestImpl* request) { |
| 1424 | DCHECK_EQ(key_.hostname, request->info().hostname()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1425 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1426 | priority_tracker_.Add(request->priority()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1427 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1428 | request->source_net_log().AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1429 | NetLogEventType::HOST_RESOLVER_IMPL_JOB_ATTACH, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1430 | net_log_.source().ToEventParametersCallback()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1431 | |
| 1432 | net_log_.AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1433 | NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH, |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1434 | base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(), |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1435 | priority())); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1436 | |
| 1437 | // TODO(szym): Check if this is still needed. |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1438 | if (!request->info().is_speculative()) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1439 | had_non_speculative_request_ = true; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1440 | if (proc_task_.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1441 | proc_task_->set_had_non_speculative_request(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1442 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1443 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1444 | requests_.push_back(request); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1445 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1446 | UpdatePriority(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1447 | } |
| 1448 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1449 | void ChangeRequestPriority(RequestImpl* req, RequestPriority priority) { |
juliatuttle | c53b19a7 | 2016-05-05 13:51:31 | [diff] [blame] | 1450 | DCHECK_EQ(key_.hostname, req->info().hostname()); |
juliatuttle | c53b19a7 | 2016-05-05 13:51:31 | [diff] [blame] | 1451 | |
| 1452 | priority_tracker_.Remove(req->priority()); |
| 1453 | req->set_priority(priority); |
| 1454 | priority_tracker_.Add(req->priority()); |
| 1455 | UpdatePriority(); |
| 1456 | } |
| 1457 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1458 | // Detach cancelled request. If it was the last active Request, also finishes |
| 1459 | // this Job. |
| 1460 | void CancelRequest(RequestImpl* request) { |
| 1461 | DCHECK_EQ(key_.hostname, request->info().hostname()); |
| 1462 | DCHECK(!requests_.empty()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1463 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1464 | LogCancelRequest(request->source_net_log(), request->info()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1465 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1466 | priority_tracker_.Remove(request->priority()); |
| 1467 | net_log_.AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1468 | NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH, |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1469 | base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(), |
| 1470 | priority())); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1471 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1472 | if (num_active_requests() > 0) { |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1473 | UpdatePriority(); |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1474 | RemoveRequest(request); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1475 | } else { |
| 1476 | // If we were called from a Request's callback within CompleteRequests, |
| 1477 | // that Request could not have been cancelled, so num_active_requests() |
| 1478 | // could not be 0. Therefore, we are not in CompleteRequests(). |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1479 | CompleteRequestsWithError(OK /* cancelled */); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1480 | } |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1481 | } |
| 1482 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1483 | void RemoveRequest(RequestImpl* request) { |
| 1484 | auto it = std::find(requests_.begin(), requests_.end(), request); |
| 1485 | DCHECK(it != requests_.end()); |
| 1486 | requests_.erase(it); |
| 1487 | } |
| 1488 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1489 | // Called from AbortAllInProgressJobs. Completes all requests and destroys |
| 1490 | // the job. This currently assumes the abort is due to a network change. |
olli.raula | a21e9eb7 | 2015-12-17 08:18:11 | [diff] [blame] | 1491 | // TODO This should not delete |this|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1492 | void Abort() { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1493 | DCHECK(is_running()); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1494 | CompleteRequestsWithError(ERR_NETWORK_CHANGED); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1495 | } |
| 1496 | |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1497 | // If DnsTask present, abort it and fall back to ProcTask. |
| 1498 | void AbortDnsTask() { |
| 1499 | if (dns_task_) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1500 | KillDnsTask(); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1501 | dns_task_error_ = OK; |
| 1502 | StartProcTask(); |
| 1503 | } |
| 1504 | } |
| 1505 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1506 | // Called by HostResolverImpl when this job is evicted due to queue overflow. |
| 1507 | // Completes all requests and destroys the job. |
| 1508 | void OnEvicted() { |
| 1509 | DCHECK(!is_running()); |
| 1510 | DCHECK(is_queued()); |
| 1511 | handle_.Reset(); |
| 1512 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1513 | net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_EVICTED); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1514 | |
| 1515 | // This signals to CompleteRequests that this job never ran. |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1516 | CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1517 | } |
| 1518 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1519 | // Attempts to serve the job from HOSTS. Returns true if succeeded and |
| 1520 | // this Job was destroyed. |
| 1521 | bool ServeFromHosts() { |
| 1522 | DCHECK_GT(num_active_requests(), 0u); |
| 1523 | AddressList addr_list; |
| 1524 | if (resolver_->ServeFromHosts(key(), |
[email protected] | 3cb676a1 | 2012-06-30 15:46:03 | [diff] [blame] | 1525 | requests_.front()->info(), |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1526 | &addr_list)) { |
| 1527 | // This will destroy the Job. |
eroman | 0cd87b6 | 2016-12-14 19:13:45 | [diff] [blame] | 1528 | CompleteRequests(MakeCacheEntry(OK, addr_list), base::TimeDelta()); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1529 | return true; |
| 1530 | } |
| 1531 | return false; |
| 1532 | } |
| 1533 | |
tanay.c | 6ddd5f6f | 2015-08-25 06:34:57 | [diff] [blame] | 1534 | const Key& key() const { return key_; } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1535 | |
| 1536 | bool is_queued() const { |
| 1537 | return !handle_.is_null(); |
| 1538 | } |
| 1539 | |
| 1540 | bool is_running() const { |
| 1541 | return is_dns_running() || is_proc_running(); |
| 1542 | } |
| 1543 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1544 | private: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1545 | void KillDnsTask() { |
| 1546 | if (dns_task_) { |
| 1547 | ReduceToOneJobSlot(); |
| 1548 | dns_task_.reset(); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | // Reduce the number of job slots occupied and queued in the dispatcher |
| 1553 | // to one. If the second Job slot is queued in the dispatcher, cancels the |
| 1554 | // queued job. Otherwise, the second Job has been started by the |
| 1555 | // PrioritizedDispatcher, so signals it is complete. |
| 1556 | void ReduceToOneJobSlot() { |
| 1557 | DCHECK_GE(num_occupied_job_slots_, 1u); |
| 1558 | if (is_queued()) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1559 | resolver_->dispatcher_->Cancel(handle_); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1560 | handle_.Reset(); |
| 1561 | } else if (num_occupied_job_slots_ > 1) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1562 | resolver_->dispatcher_->OnJobFinished(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1563 | --num_occupied_job_slots_; |
| 1564 | } |
| 1565 | DCHECK_EQ(1u, num_occupied_job_slots_); |
| 1566 | } |
| 1567 | |
eroman | 0cd87b6 | 2016-12-14 19:13:45 | [diff] [blame] | 1568 | // MakeCacheEntry() and MakeCacheEntryWithTTL() are helpers to build a |
| 1569 | // HostCache::Entry(). The address list is omited from the cache entry |
| 1570 | // for errors. |
| 1571 | HostCache::Entry MakeCacheEntry(int net_error, |
| 1572 | const AddressList& addr_list) const { |
| 1573 | return HostCache::Entry( |
| 1574 | net_error, |
| 1575 | net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList()); |
| 1576 | } |
| 1577 | |
| 1578 | HostCache::Entry MakeCacheEntryWithTTL(int net_error, |
| 1579 | const AddressList& addr_list, |
| 1580 | base::TimeDelta ttl) const { |
| 1581 | return HostCache::Entry( |
| 1582 | net_error, |
| 1583 | net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(), |
| 1584 | ttl); |
| 1585 | } |
| 1586 | |
juliatuttle | c53b19a7 | 2016-05-05 13:51:31 | [diff] [blame] | 1587 | AddressList MakeAddressListForRequest(const AddressList& list) const { |
| 1588 | if (requests_.empty()) |
| 1589 | return list; |
| 1590 | return AddressList::CopyWithPort(list, requests_.front()->info().port()); |
| 1591 | } |
| 1592 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1593 | void UpdatePriority() { |
| 1594 | if (is_queued()) { |
| 1595 | if (priority() != static_cast<RequestPriority>(handle_.priority())) |
| 1596 | priority_change_time_ = base::TimeTicks::Now(); |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1597 | handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority()); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1601 | // PriorityDispatch::Job: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1602 | void Start() override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1603 | DCHECK_LE(num_occupied_job_slots_, 1u); |
| 1604 | |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1605 | handle_.Reset(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1606 | ++num_occupied_job_slots_; |
| 1607 | |
| 1608 | if (num_occupied_job_slots_ == 2) { |
| 1609 | StartSecondDnsTransaction(); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | DCHECK(!is_running()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1614 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1615 | net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_STARTED); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1616 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1617 | had_dns_config_ = resolver_->HaveDnsConfig(); |
| 1618 | |
| 1619 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1620 | base::TimeDelta queue_time = now - creation_time_; |
| 1621 | base::TimeDelta queue_time_after_change = now - priority_change_time_; |
| 1622 | |
| 1623 | if (had_dns_config_) { |
| 1624 | DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(), |
| 1625 | queue_time); |
| 1626 | DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(), |
| 1627 | queue_time_after_change); |
| 1628 | } else { |
| 1629 | DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time); |
| 1630 | DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(), |
| 1631 | queue_time_after_change); |
| 1632 | } |
| 1633 | |
[email protected] | 443714fad | 2013-09-19 04:52:01 | [diff] [blame] | 1634 | bool system_only = |
| 1635 | (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0; |
| 1636 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1637 | // Caution: Job::Start must not complete synchronously. |
[email protected] | 443714fad | 2013-09-19 04:52:01 | [diff] [blame] | 1638 | if (!system_only && had_dns_config_ && |
| 1639 | !ResemblesMulticastDNSName(key_.hostname)) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1640 | StartDnsTask(); |
| 1641 | } else { |
| 1642 | StartProcTask(); |
| 1643 | } |
| 1644 | } |
| 1645 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1646 | // TODO(szym): Since DnsTransaction does not consume threads, we can increase |
| 1647 | // the limits on |dispatcher_|. But in order to keep the number of WorkerPool |
| 1648 | // threads low, we will need to use an "inner" PrioritizedDispatcher with |
| 1649 | // tighter limits. |
| 1650 | void StartProcTask() { |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1651 | DCHECK(!is_dns_running()); |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1652 | proc_task_ = |
| 1653 | new ProcTask(key_, resolver_->proc_params_, |
| 1654 | base::Bind(&Job::OnProcTaskComplete, |
| 1655 | base::Unretained(this), base::TimeTicks::Now()), |
| 1656 | worker_task_runner_, net_log_); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1657 | |
| 1658 | if (had_non_speculative_request_) |
| 1659 | proc_task_->set_had_non_speculative_request(); |
| 1660 | // Start() could be called from within Resolve(), hence it must NOT directly |
| 1661 | // call OnProcTaskComplete, for example, on synchronous failure. |
| 1662 | proc_task_->Start(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1663 | } |
| 1664 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1665 | // Called by ProcTask when it completes. |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1666 | void OnProcTaskComplete(base::TimeTicks start_time, |
| 1667 | int net_error, |
| 1668 | const AddressList& addr_list) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1669 | DCHECK(is_proc_running()); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1670 | |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1671 | if (!resolver_->resolved_known_ipv6_hostname_ && |
| 1672 | net_error == OK && |
| 1673 | key_.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 1674 | if (key_.hostname == "www.google.com") { |
| 1675 | resolver_->resolved_known_ipv6_hostname_ = true; |
| 1676 | bool got_ipv6_address = false; |
| 1677 | for (size_t i = 0; i < addr_list.size(); ++i) { |
[email protected] | 5134c22a | 2013-08-06 18:09:02 | [diff] [blame] | 1678 | if (addr_list[i].GetFamily() == ADDRESS_FAMILY_IPV6) { |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1679 | got_ipv6_address = true; |
[email protected] | 5134c22a | 2013-08-06 18:09:02 | [diff] [blame] | 1680 | break; |
| 1681 | } |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1682 | } |
| 1683 | UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address); |
| 1684 | } |
| 1685 | } |
| 1686 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1687 | if (dns_task_error_ != OK) { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1688 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1689 | if (net_error == OK) { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1690 | DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration); |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1691 | if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) && |
| 1692 | ResemblesNetBIOSName(key_.hostname)) { |
| 1693 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS); |
| 1694 | } else { |
| 1695 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS); |
| 1696 | } |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 1697 | UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.ResolveError", |
| 1698 | std::abs(dns_task_error_)); |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 1699 | resolver_->OnDnsTaskResolve(dns_task_error_); |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1700 | } else { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1701 | DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration); |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1702 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL); |
| 1703 | } |
| 1704 | } |
| 1705 | |
eroman | 1efc237c | 2016-12-14 00:00:45 | [diff] [blame] | 1706 | if (ContainsIcannNameCollisionIp(addr_list)) |
| 1707 | net_error = ERR_ICANN_NAME_COLLISION; |
| 1708 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1709 | base::TimeDelta ttl = |
| 1710 | base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1711 | if (net_error == OK) |
| 1712 | ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1713 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1714 | // Don't store the |ttl| in cache since it's not obtained from the server. |
eroman | 0cd87b6 | 2016-12-14 19:13:45 | [diff] [blame] | 1715 | CompleteRequests(MakeCacheEntry(net_error, addr_list), ttl); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | void StartDnsTask() { |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1719 | DCHECK(resolver_->HaveDnsConfig()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1720 | dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this, |
| 1721 | net_log_)); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1722 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1723 | dns_task_->StartFirstTransaction(); |
| 1724 | // Schedule a second transaction, if needed. |
| 1725 | if (dns_task_->needs_two_transactions()) |
| 1726 | Schedule(true); |
| 1727 | } |
| 1728 | |
| 1729 | void StartSecondDnsTransaction() { |
| 1730 | DCHECK(dns_task_->needs_two_transactions()); |
| 1731 | dns_task_->StartSecondTransaction(); |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be |
| 1735 | // deleted before this callback. In this case dns_task is deleted as well, |
| 1736 | // so we use it as indicator whether Job is still valid. |
| 1737 | void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task, |
| 1738 | base::TimeDelta duration, |
| 1739 | int net_error) { |
| 1740 | DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration); |
| 1741 | |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 1742 | if (!dns_task) |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1743 | return; |
| 1744 | |
| 1745 | dns_task_error_ = net_error; |
| 1746 | |
| 1747 | // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so. |
| 1748 | // http://crbug.com/117655 |
| 1749 | |
| 1750 | // TODO(szym): Some net errors indicate lack of connectivity. Starting |
| 1751 | // ProcTask in that case is a waste of time. |
| 1752 | if (resolver_->fallback_to_proctask_) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1753 | KillDnsTask(); |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1754 | StartProcTask(); |
| 1755 | } else { |
| 1756 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL); |
| 1757 | CompleteRequestsWithError(net_error); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1761 | |
| 1762 | // HostResolverImpl::DnsTask::Delegate implementation: |
| 1763 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1764 | void OnDnsTaskComplete(base::TimeTicks start_time, |
| 1765 | int net_error, |
| 1766 | const AddressList& addr_list, |
| 1767 | base::TimeDelta ttl) override { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1768 | DCHECK(is_dns_running()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1769 | |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1770 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1771 | if (net_error != OK) { |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1772 | OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1773 | return; |
| 1774 | } |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1775 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration); |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1776 | // Log DNS lookups based on |address_family|. |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 1777 | switch (key_.address_family) { |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1778 | case ADDRESS_FAMILY_IPV4: |
| 1779 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration); |
| 1780 | break; |
| 1781 | case ADDRESS_FAMILY_IPV6: |
| 1782 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration); |
| 1783 | break; |
| 1784 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 1785 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration); |
| 1786 | break; |
| 1787 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1788 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1789 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS); |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1790 | RecordTTL(ttl); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1791 | |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 1792 | resolver_->OnDnsTaskResolve(OK); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1793 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1794 | base::TimeDelta bounded_ttl = |
| 1795 | std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds)); |
| 1796 | |
eroman | 0cd87b6 | 2016-12-14 19:13:45 | [diff] [blame] | 1797 | if (ContainsIcannNameCollisionIp(addr_list)) { |
| 1798 | CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION); |
| 1799 | } else { |
| 1800 | CompleteRequests(MakeCacheEntryWithTTL(net_error, addr_list, ttl), |
| 1801 | bounded_ttl); |
| 1802 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1803 | } |
| 1804 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1805 | void OnFirstDnsTransactionComplete() override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1806 | DCHECK(dns_task_->needs_two_transactions()); |
| 1807 | DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued()); |
| 1808 | // No longer need to occupy two dispatcher slots. |
| 1809 | ReduceToOneJobSlot(); |
| 1810 | |
| 1811 | // We already have a job slot at the dispatcher, so if the second |
| 1812 | // transaction hasn't started, reuse it now instead of waiting in the queue |
| 1813 | // for the second slot. |
| 1814 | if (dns_task_->needs_another_transaction()) |
| 1815 | dns_task_->StartSecondTransaction(); |
| 1816 | } |
| 1817 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1818 | // Performs Job's last rites. Completes all Requests. Deletes this. |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1819 | void CompleteRequests(const HostCache::Entry& entry, |
| 1820 | base::TimeDelta ttl) { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1821 | CHECK(resolver_.get()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1822 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1823 | // This job must be removed from resolver's |jobs_| now to make room for a |
| 1824 | // new job with the same key in case one of the OnComplete callbacks decides |
| 1825 | // to spawn one. Consequently, the job deletes itself when CompleteRequests |
| 1826 | // is done. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 1827 | std::unique_ptr<Job> self_deleter(this); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1828 | |
| 1829 | resolver_->RemoveJob(this); |
| 1830 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1831 | if (is_running()) { |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1832 | if (is_proc_running()) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1833 | DCHECK(!is_queued()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1834 | proc_task_->Cancel(); |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 1835 | proc_task_ = nullptr; |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1836 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1837 | KillDnsTask(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1838 | |
| 1839 | // Signal dispatcher that a slot has opened. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1840 | resolver_->dispatcher_->OnJobFinished(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1841 | } else if (is_queued()) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1842 | resolver_->dispatcher_->Cancel(handle_); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1843 | handle_.Reset(); |
| 1844 | } |
| 1845 | |
| 1846 | if (num_active_requests() == 0) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1847 | net_log_.AddEvent(NetLogEventType::CANCELLED); |
| 1848 | net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB, |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1849 | OK); |
| 1850 | return; |
| 1851 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1852 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1853 | net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB, |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 1854 | entry.error()); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1855 | |
juliatuttle | d7fa585 | 2016-07-29 13:16:57 | [diff] [blame] | 1856 | resolver_->SchedulePersist(); |
| 1857 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1858 | DCHECK(!requests_.empty()); |
| 1859 | |
eroman | 1efc237c | 2016-12-14 00:00:45 | [diff] [blame] | 1860 | if (entry.error() == OK || entry.error() == ERR_ICANN_NAME_COLLISION) { |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1861 | // Record this histogram here, when we know the system has a valid DNS |
| 1862 | // configuration. |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 1863 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig", |
| 1864 | resolver_->received_dns_config_); |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1865 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1866 | |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 1867 | bool did_complete = (entry.error() != ERR_NETWORK_CHANGED) && |
| 1868 | (entry.error() != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 1869 | if (did_complete) { |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1870 | resolver_->CacheResult(key_, entry, ttl); |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 1871 | // Erase any previous cache hit callbacks, since a new DNS request went |
| 1872 | // out since they were set. |
| 1873 | resolver_->cache_hit_callbacks_.erase(key_); |
| 1874 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1875 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1876 | // Complete all of the requests that were attached to the job and |
| 1877 | // detach them. |
| 1878 | while (!requests_.empty()) { |
| 1879 | RequestImpl* req = requests_.front(); |
| 1880 | requests_.pop_front(); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1881 | DCHECK_EQ(this, req->job()); |
| 1882 | // Update the net log and notify registered observers. |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 1883 | LogFinishRequest(req->source_net_log(), req->info(), entry.error()); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1884 | if (did_complete) { |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 1885 | resolver_->MaybeAddCacheHitCallback(key_, req->info()); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1886 | // Record effective total time from creation to completion. |
| 1887 | RecordTotalTime(had_dns_config_, req->info().is_speculative(), |
| 1888 | base::TimeTicks::Now() - req->request_time()); |
| 1889 | } |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1890 | req->OnJobCompleted(this, entry.error(), entry.addresses()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1891 | |
| 1892 | // Check if the resolver was destroyed as a result of running the |
| 1893 | // callback. If it was, we could continue, but we choose to bail. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1894 | if (!resolver_.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1895 | return; |
| 1896 | } |
| 1897 | } |
| 1898 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1899 | // Convenience wrapper for CompleteRequests in case of failure. |
| 1900 | void CompleteRequestsWithError(int net_error) { |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1901 | CompleteRequests(HostCache::Entry(net_error, AddressList()), |
| 1902 | base::TimeDelta()); |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1903 | } |
| 1904 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1905 | RequestPriority priority() const { |
| 1906 | return priority_tracker_.highest_priority(); |
| 1907 | } |
| 1908 | |
| 1909 | // Number of non-canceled requests in |requests_|. |
| 1910 | size_t num_active_requests() const { |
| 1911 | return priority_tracker_.total_count(); |
| 1912 | } |
| 1913 | |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 1914 | bool is_dns_running() const { return !!dns_task_; } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1915 | |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 1916 | bool is_proc_running() const { return !!proc_task_; } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1917 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1918 | base::WeakPtr<HostResolverImpl> resolver_; |
| 1919 | |
| 1920 | Key key_; |
| 1921 | |
| 1922 | // Tracks the highest priority across |requests_|. |
| 1923 | PriorityTracker priority_tracker_; |
| 1924 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1925 | // Task runner where the HostResolverProc is invoked. |
| 1926 | scoped_refptr<base::TaskRunner> worker_task_runner_; |
| 1927 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1928 | bool had_non_speculative_request_; |
| 1929 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1930 | // Distinguishes measurements taken while DnsClient was fully configured. |
| 1931 | bool had_dns_config_; |
| 1932 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1933 | // Number of slots occupied by this Job in resolver's PrioritizedDispatcher. |
| 1934 | unsigned num_occupied_job_slots_; |
| 1935 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1936 | // Result of DnsTask. |
| 1937 | int dns_task_error_; |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1938 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1939 | const base::TimeTicks creation_time_; |
| 1940 | base::TimeTicks priority_change_time_; |
| 1941 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1942 | NetLogWithSource net_log_; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1943 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1944 | // Resolves the host using a HostResolverProc. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1945 | scoped_refptr<ProcTask> proc_task_; |
| 1946 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1947 | // Resolves the host using a DnsTransaction. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 1948 | std::unique_ptr<DnsTask> dns_task_; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1949 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1950 | // All Requests waiting for the result of this Job. Some can be canceled. |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 1951 | std::deque<RequestImpl*> requests_; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1952 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1953 | // A handle used in |HostResolverImpl::dispatcher_|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1954 | PrioritizedDispatcher::Handle handle_; |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1955 | }; |
| 1956 | |
| 1957 | //----------------------------------------------------------------------------- |
| 1958 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1959 | HostResolverImpl::ProcTaskParams::ProcTaskParams( |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1960 | HostResolverProc* resolver_proc, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1961 | size_t max_retry_attempts) |
| 1962 | : resolver_proc(resolver_proc), |
| 1963 | max_retry_attempts(max_retry_attempts), |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 1964 | unresponsive_delay( |
| 1965 | base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs)), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1966 | retry_factor(2) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1967 | // Maximum of 4 retry attempts for host resolution. |
| 1968 | static const size_t kDefaultMaxRetryAttempts = 4u; |
| 1969 | if (max_retry_attempts == HostResolver::kDefaultRetryAttempts) |
| 1970 | max_retry_attempts = kDefaultMaxRetryAttempts; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1971 | } |
| 1972 | |
vmpstr | acd23b7 | 2016-02-26 21:08:55 | [diff] [blame] | 1973 | HostResolverImpl::ProcTaskParams::ProcTaskParams(const ProcTaskParams& other) = |
| 1974 | default; |
| 1975 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1976 | HostResolverImpl::ProcTaskParams::~ProcTaskParams() {} |
| 1977 | |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1978 | HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log) |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 1979 | : HostResolverImpl( |
| 1980 | options, |
| 1981 | net_log, |
| 1982 | base::WorkerPool::GetTaskRunner(true /* task_is_slow */)) {} |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1983 | |
| 1984 | HostResolverImpl::~HostResolverImpl() { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1985 | // Prevent the dispatcher from starting new jobs. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1986 | dispatcher_->SetLimitsToZero(); |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 1987 | // It's now safe for Jobs to call KillDnsTask on destruction, because |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1988 | // OnJobComplete will not start any new jobs. |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 1989 | jobs_.clear(); |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1990 | |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 1991 | NetworkChangeNotifier::RemoveIPAddressObserver(this); |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 1992 | NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 1993 | NetworkChangeNotifier::RemoveDNSObserver(this); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1994 | } |
| 1995 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1996 | void HostResolverImpl::SetMaxQueuedJobs(size_t value) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1997 | DCHECK_EQ(0u, dispatcher_->num_queued_jobs()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1998 | DCHECK_GT(value, 0u); |
| 1999 | max_queued_jobs_ = value; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2000 | } |
| 2001 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2002 | int HostResolverImpl::Resolve(const RequestInfo& info, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 2003 | RequestPriority priority, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2004 | AddressList* addresses, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 2005 | const CompletionCallback& callback, |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 2006 | std::unique_ptr<Request>* out_req, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2007 | const NetLogWithSource& source_net_log) { |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2008 | DCHECK(addresses); |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 2009 | DCHECK(CalledOnValidThread()); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 2010 | DCHECK_EQ(false, callback.is_null()); |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 2011 | DCHECK(out_req); |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 2012 | |
[email protected] | e806cd7 | 2013-05-17 02:08:43 | [diff] [blame] | 2013 | // Check that the caller supplied a valid hostname to resolve. |
mmenke | d6e9161aff | 2016-09-09 21:55:44 | [diff] [blame] | 2014 | if (!IsValidDNSDomain(info.hostname())) |
[email protected] | e806cd7 | 2013-05-17 02:08:43 | [diff] [blame] | 2015 | return ERR_NAME_NOT_RESOLVED; |
| 2016 | |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2017 | LogStartRequest(source_net_log, info); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2018 | |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2019 | IPAddress ip_address; |
| 2020 | IPAddress* ip_address_ptr = nullptr; |
| 2021 | if (ip_address.AssignFromIPLiteral(info.hostname())) |
| 2022 | ip_address_ptr = &ip_address; |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2023 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 2024 | // Build a key that identifies the request in the cache and in the |
| 2025 | // outstanding jobs map. |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2026 | Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 2027 | |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2028 | int rv = ResolveHelper(key, info, ip_address_ptr, addresses, false, nullptr, |
| 2029 | source_net_log); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2030 | if (rv != ERR_DNS_CACHE_MISS) { |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2031 | MaybeAddCacheHitCallback(key, info); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2032 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 2033 | RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2034 | return rv; |
[email protected] | 3836871 | 2011-03-02 08:09:40 | [diff] [blame] | 2035 | } |
| 2036 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2037 | // Next we need to attach our request to a "job". This job is responsible for |
| 2038 | // calling "getaddrinfo(hostname)" on a worker thread. |
| 2039 | |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2040 | auto jobit = jobs_.find(key); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2041 | Job* job; |
| 2042 | if (jobit == jobs_.end()) { |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 2043 | job = new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, |
| 2044 | worker_task_runner_, source_net_log); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2045 | job->Schedule(false); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2046 | |
| 2047 | // Check for queue overflow. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2048 | if (dispatcher_->num_queued_jobs() > max_queued_jobs_) { |
| 2049 | Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2050 | DCHECK(evicted); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2051 | evicted->OnEvicted(); // Deletes |evicted|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2052 | if (evicted == job) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2053 | rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2054 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2055 | return rv; |
| 2056 | } |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2057 | } |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2058 | jobs_[key] = base::WrapUnique(job); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2059 | } else { |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2060 | job = jobit->second.get(); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | // Can't complete synchronously. Create and attach request. |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2064 | auto req = base::MakeUnique<RequestImpl>(source_net_log, info, priority, |
| 2065 | callback, addresses, job); |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 2066 | job->AddRequest(req.get()); |
| 2067 | *out_req = std::move(req); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2068 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2069 | // Completion happens during Job::CompleteRequests(). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2070 | return ERR_IO_PENDING; |
| 2071 | } |
| 2072 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 2073 | HostResolverImpl::HostResolverImpl( |
| 2074 | const Options& options, |
| 2075 | NetLog* net_log, |
| 2076 | scoped_refptr<base::TaskRunner> worker_task_runner) |
| 2077 | : max_queued_jobs_(0), |
| 2078 | proc_params_(NULL, options.max_retry_attempts), |
| 2079 | net_log_(net_log), |
| 2080 | received_dns_config_(false), |
| 2081 | num_dns_failures_(0), |
mgersh | 3eba057d | 2017-02-28 18:50:56 | [diff] [blame] | 2082 | default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 2083 | use_local_ipv6_(false), |
| 2084 | last_ipv6_probe_result_(true), |
| 2085 | resolved_known_ipv6_hostname_(false), |
| 2086 | additional_resolver_flags_(0), |
| 2087 | fallback_to_proctask_(true), |
| 2088 | worker_task_runner_(std::move(worker_task_runner)), |
juliatuttle | d7fa585 | 2016-07-29 13:16:57 | [diff] [blame] | 2089 | persist_initialized_(false), |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 2090 | weak_ptr_factory_(this), |
| 2091 | probe_weak_ptr_factory_(this) { |
| 2092 | if (options.enable_caching) |
| 2093 | cache_ = HostCache::CreateDefaultCache(); |
| 2094 | |
| 2095 | PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits(); |
| 2096 | dispatcher_.reset(new PrioritizedDispatcher(job_limits)); |
| 2097 | max_queued_jobs_ = job_limits.total_jobs * 100u; |
| 2098 | |
| 2099 | DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES)); |
| 2100 | |
| 2101 | #if defined(OS_WIN) |
| 2102 | EnsureWinsockInit(); |
| 2103 | #endif |
| 2104 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 2105 | RunLoopbackProbeJob(); |
| 2106 | #endif |
| 2107 | NetworkChangeNotifier::AddIPAddressObserver(this); |
| 2108 | NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 2109 | NetworkChangeNotifier::AddDNSObserver(this); |
| 2110 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \ |
| 2111 | !defined(OS_ANDROID) |
| 2112 | EnsureDnsReloaderInit(); |
| 2113 | #endif |
| 2114 | |
| 2115 | OnConnectionTypeChanged(NetworkChangeNotifier::GetConnectionType()); |
| 2116 | |
| 2117 | { |
| 2118 | DnsConfig dns_config; |
| 2119 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2120 | received_dns_config_ = dns_config.IsValid(); |
| 2121 | // Conservatively assume local IPv6 is needed when DnsConfig is not valid. |
| 2122 | use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6; |
| 2123 | } |
| 2124 | |
| 2125 | fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial(); |
| 2126 | } |
| 2127 | |
| 2128 | void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) { |
| 2129 | if (result) { |
| 2130 | additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 2131 | } else { |
| 2132 | additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; |
| 2133 | } |
| 2134 | } |
| 2135 | |
[email protected] | 287d7c2 | 2011-11-15 17:34:25 | [diff] [blame] | 2136 | int HostResolverImpl::ResolveHelper(const Key& key, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2137 | const RequestInfo& info, |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2138 | const IPAddress* ip_address, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2139 | AddressList* addresses, |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2140 | bool allow_stale, |
| 2141 | HostCache::EntryStaleness* stale_info, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2142 | const NetLogWithSource& source_net_log) { |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2143 | DCHECK(allow_stale == !!stale_info); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2144 | // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
| 2145 | // On Windows it gives the default interface's address, whereas on Linux it |
| 2146 | // gives an error. We will make it fail on all platforms for consistency. |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2147 | if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) { |
| 2148 | MakeNotStale(stale_info); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2149 | return ERR_NAME_NOT_RESOLVED; |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2150 | } |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2151 | |
| 2152 | int net_error = ERR_UNEXPECTED; |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2153 | if (ResolveAsIP(key, info, ip_address, &net_error, addresses)) { |
| 2154 | MakeNotStale(stale_info); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2155 | return net_error; |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2156 | } |
| 2157 | if (ServeFromCache(key, info, &net_error, addresses, allow_stale, |
| 2158 | stale_info)) { |
mgersh | 33d23c0 | 2017-03-29 20:24:25 | [diff] [blame^] | 2159 | source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_CACHE_HIT, |
| 2160 | addresses->CreateNetLogCallback()); |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2161 | // |ServeFromCache()| will set |*stale_info| as needed. |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2162 | RunCacheHitCallbacks(key, info); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2163 | return net_error; |
| 2164 | } |
| 2165 | // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 2166 | // http://crbug.com/117655 |
| 2167 | if (ServeFromHosts(key, info, addresses)) { |
mgersh | 33d23c0 | 2017-03-29 20:24:25 | [diff] [blame^] | 2168 | source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_HOSTS_HIT, |
| 2169 | addresses->CreateNetLogCallback()); |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2170 | MakeNotStale(stale_info); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2171 | return OK; |
| 2172 | } |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 2173 | |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2174 | if (ServeLocalhost(key, info, addresses)) { |
| 2175 | MakeNotStale(stale_info); |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 2176 | return OK; |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2177 | } |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 2178 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2179 | return ERR_DNS_CACHE_MISS; |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | int HostResolverImpl::ResolveFromCache(const RequestInfo& info, |
| 2183 | AddressList* addresses, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2184 | const NetLogWithSource& source_net_log) { |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2185 | DCHECK(CalledOnValidThread()); |
| 2186 | DCHECK(addresses); |
| 2187 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2188 | // Update the net log and notify registered observers. |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2189 | LogStartRequest(source_net_log, info); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2190 | |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2191 | IPAddress ip_address; |
| 2192 | IPAddress* ip_address_ptr = nullptr; |
| 2193 | if (ip_address.AssignFromIPLiteral(info.hostname())) |
| 2194 | ip_address_ptr = &ip_address; |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2195 | |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2196 | Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2197 | |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2198 | int rv = ResolveHelper(key, info, ip_address_ptr, addresses, false, nullptr, |
| 2199 | source_net_log); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2200 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2201 | return rv; |
| 2202 | } |
| 2203 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2204 | void HostResolverImpl::SetDnsClientEnabled(bool enabled) { |
| 2205 | DCHECK(CalledOnValidThread()); |
| 2206 | #if defined(ENABLE_BUILT_IN_DNS) |
| 2207 | if (enabled && !dns_client_) { |
| 2208 | SetDnsClient(DnsClient::CreateClient(net_log_)); |
| 2209 | } else if (!enabled && dns_client_) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 2210 | SetDnsClient(std::unique_ptr<DnsClient>()); |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2211 | } |
| 2212 | #endif |
| 2213 | } |
| 2214 | |
[email protected] | 489d1a8 | 2011-10-12 03:09:11 | [diff] [blame] | 2215 | HostCache* HostResolverImpl::GetHostCache() { |
| 2216 | return cache_.get(); |
| 2217 | } |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2218 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 2219 | std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const { |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 2220 | // Check if async DNS is disabled. |
| 2221 | if (!dns_client_.get()) |
tfh | ef3618f | 2016-01-11 23:07:08 | [diff] [blame] | 2222 | return nullptr; |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 2223 | |
| 2224 | // Check if async DNS is enabled, but we currently have no configuration |
| 2225 | // for it. |
| 2226 | const DnsConfig* dns_config = dns_client_->GetConfig(); |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 2227 | if (!dns_config) |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 2228 | return base::MakeUnique<base::DictionaryValue>(); |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 2229 | |
| 2230 | return dns_config->ToValue(); |
| 2231 | } |
| 2232 | |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2233 | int HostResolverImpl::ResolveStaleFromCache( |
| 2234 | const RequestInfo& info, |
| 2235 | AddressList* addresses, |
| 2236 | HostCache::EntryStaleness* stale_info, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2237 | const NetLogWithSource& source_net_log) { |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2238 | DCHECK(CalledOnValidThread()); |
| 2239 | DCHECK(addresses); |
| 2240 | DCHECK(stale_info); |
| 2241 | |
| 2242 | // Update the net log and notify registered observers. |
| 2243 | LogStartRequest(source_net_log, info); |
| 2244 | |
| 2245 | IPAddress ip_address; |
| 2246 | IPAddress* ip_address_ptr = nullptr; |
| 2247 | if (ip_address.AssignFromIPLiteral(info.hostname())) |
| 2248 | ip_address_ptr = &ip_address; |
| 2249 | |
| 2250 | Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); |
| 2251 | |
| 2252 | int rv = ResolveHelper(key, info, ip_address_ptr, addresses, true, stale_info, |
| 2253 | source_net_log); |
| 2254 | LogFinishRequest(source_net_log, info, rv); |
| 2255 | return rv; |
| 2256 | } |
| 2257 | |
mgersh | 3eba057d | 2017-02-28 18:50:56 | [diff] [blame] | 2258 | void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { |
| 2259 | DCHECK(CalledOnValidThread()); |
| 2260 | default_address_family_ = address_family; |
| 2261 | } |
| 2262 | |
| 2263 | AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { |
| 2264 | return default_address_family_; |
| 2265 | } |
| 2266 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2267 | bool HostResolverImpl::ResolveAsIP(const Key& key, |
| 2268 | const RequestInfo& info, |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2269 | const IPAddress* ip_address, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2270 | int* net_error, |
| 2271 | AddressList* addresses) { |
| 2272 | DCHECK(addresses); |
| 2273 | DCHECK(net_error); |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2274 | if (ip_address == nullptr) |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2275 | return false; |
| 2276 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2277 | *net_error = OK; |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2278 | AddressFamily family = GetAddressFamily(*ip_address); |
mgersh | 3eba057d | 2017-02-28 18:50:56 | [diff] [blame] | 2279 | if (family == ADDRESS_FAMILY_IPV6 && |
| 2280 | default_address_family_ == ADDRESS_FAMILY_IPV4) { |
| 2281 | // Don't return IPv6 addresses if default address family is set to IPv4. |
| 2282 | *net_error = ERR_NAME_NOT_RESOLVED; |
| 2283 | } |
cbentzel | 1906f87 | 2015-06-05 16:25:25 | [diff] [blame] | 2284 | if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED && |
| 2285 | key.address_family != family) { |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 2286 | // Don't return IPv6 addresses for IPv4 queries, and vice versa. |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2287 | *net_error = ERR_NAME_NOT_RESOLVED; |
| 2288 | } else { |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2289 | *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2290 | if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 2291 | addresses->SetDefaultCanonicalName(); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2292 | } |
| 2293 | return true; |
| 2294 | } |
| 2295 | |
| 2296 | bool HostResolverImpl::ServeFromCache(const Key& key, |
| 2297 | const RequestInfo& info, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2298 | int* net_error, |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2299 | AddressList* addresses, |
| 2300 | bool allow_stale, |
| 2301 | HostCache::EntryStaleness* stale_info) { |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2302 | DCHECK(addresses); |
| 2303 | DCHECK(net_error); |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2304 | DCHECK(allow_stale == !!stale_info); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2305 | if (!info.allow_cached_response() || !cache_.get()) |
| 2306 | return false; |
| 2307 | |
juliatuttle | 9fb7aeb | 2016-06-06 20:16:33 | [diff] [blame] | 2308 | const HostCache::Entry* cache_entry; |
| 2309 | if (allow_stale) |
| 2310 | cache_entry = cache_->LookupStale(key, base::TimeTicks::Now(), stale_info); |
| 2311 | else |
| 2312 | cache_entry = cache_->Lookup(key, base::TimeTicks::Now()); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2313 | if (!cache_entry) |
| 2314 | return false; |
| 2315 | |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 2316 | *net_error = cache_entry->error(); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2317 | if (*net_error == OK) { |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2318 | if (cache_entry->has_ttl()) |
juliatuttle | 317860e | 2016-05-12 14:47:22 | [diff] [blame] | 2319 | RecordTTL(cache_entry->ttl()); |
| 2320 | *addresses = EnsurePortOnAddressList(cache_entry->addresses(), info.port()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2321 | } |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2322 | return true; |
| 2323 | } |
| 2324 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2325 | bool HostResolverImpl::ServeFromHosts(const Key& key, |
| 2326 | const RequestInfo& info, |
| 2327 | AddressList* addresses) { |
| 2328 | DCHECK(addresses); |
| 2329 | if (!HaveDnsConfig()) |
| 2330 | return false; |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2331 | addresses->clear(); |
| 2332 | |
[email protected] | cb50762 | 2012-03-23 16:17:06 | [diff] [blame] | 2333 | // HOSTS lookups are case-insensitive. |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 2334 | std::string hostname = base::ToLowerASCII(key.hostname); |
[email protected] | cb50762 | 2012-03-23 16:17:06 | [diff] [blame] | 2335 | |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2336 | const DnsHosts& hosts = dns_client_->GetConfig()->hosts; |
| 2337 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2338 | // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations |
| 2339 | // (glibc and c-ares) return the first matching line. We have more |
| 2340 | // flexibility, but lose implicit ordering. |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2341 | // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if |
| 2342 | // necessary. |
| 2343 | if (key.address_family == ADDRESS_FAMILY_IPV6 || |
| 2344 | key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2345 | DnsHosts::const_iterator it = hosts.find( |
| 2346 | DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6)); |
| 2347 | if (it != hosts.end()) |
| 2348 | addresses->push_back(IPEndPoint(it->second, info.port())); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2349 | } |
| 2350 | |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2351 | if (key.address_family == ADDRESS_FAMILY_IPV4 || |
| 2352 | key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2353 | DnsHosts::const_iterator it = hosts.find( |
| 2354 | DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4)); |
| 2355 | if (it != hosts.end()) |
| 2356 | addresses->push_back(IPEndPoint(it->second, info.port())); |
| 2357 | } |
| 2358 | |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 2359 | // If got only loopback addresses and the family was restricted, resolve |
| 2360 | // again, without restrictions. See SystemHostResolverCall for rationale. |
| 2361 | if ((key.host_resolver_flags & |
| 2362 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) && |
| 2363 | IsAllIPv4Loopback(*addresses)) { |
| 2364 | Key new_key(key); |
| 2365 | new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED; |
| 2366 | new_key.host_resolver_flags &= |
| 2367 | ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2368 | return ServeFromHosts(new_key, info, addresses); |
| 2369 | } |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2370 | return !addresses->empty(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2371 | } |
| 2372 | |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 2373 | bool HostResolverImpl::ServeLocalhost(const Key& key, |
| 2374 | const RequestInfo& info, |
| 2375 | AddressList* addresses) { |
| 2376 | AddressList resolved_addresses; |
| 2377 | if (!ResolveLocalHostname(key.hostname, info.port(), &resolved_addresses)) |
| 2378 | return false; |
| 2379 | |
| 2380 | addresses->clear(); |
| 2381 | |
| 2382 | for (const auto& address : resolved_addresses) { |
| 2383 | // Include the address if: |
| 2384 | // - caller didn't specify an address family, or |
| 2385 | // - caller specifically asked for the address family of this address, or |
| 2386 | // - this is an IPv6 address and caller specifically asked for IPv4 due |
| 2387 | // to lack of detected IPv6 support. (See SystemHostResolverCall for |
| 2388 | // rationale). |
| 2389 | if (key.address_family == ADDRESS_FAMILY_UNSPECIFIED || |
| 2390 | key.address_family == address.GetFamily() || |
| 2391 | (address.GetFamily() == ADDRESS_FAMILY_IPV6 && |
| 2392 | key.address_family == ADDRESS_FAMILY_IPV4 && |
| 2393 | (key.host_resolver_flags & |
| 2394 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6))) { |
| 2395 | addresses->push_back(address); |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | return true; |
| 2400 | } |
| 2401 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2402 | void HostResolverImpl::CacheResult(const Key& key, |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2403 | const HostCache::Entry& entry, |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2404 | base::TimeDelta ttl) { |
| 2405 | if (cache_.get()) |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2406 | cache_->Set(key, entry, base::TimeTicks::Now(), ttl); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2407 | } |
| 2408 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2409 | void HostResolverImpl::RemoveJob(Job* job) { |
| 2410 | DCHECK(job); |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2411 | auto it = jobs_.find(job->key()); |
| 2412 | if (it != jobs_.end() && it->second.get() == job) { |
| 2413 | it->second.release(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2414 | jobs_.erase(it); |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2415 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2416 | } |
| 2417 | |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2418 | HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2419 | const RequestInfo& info, |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2420 | const IPAddress* ip_address, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2421 | const NetLogWithSource& net_log) { |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 2422 | HostResolverFlags effective_flags = |
| 2423 | info.host_resolver_flags() | additional_resolver_flags_; |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2424 | AddressFamily effective_address_family = info.address_family(); |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 2425 | |
mgersh | 3eba057d | 2017-02-28 18:50:56 | [diff] [blame] | 2426 | if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) |
| 2427 | effective_address_family = default_address_family_; |
| 2428 | |
| 2429 | if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED && |
| 2430 | // When resolving IPv4 literals, there's no need to probe for IPv6. |
| 2431 | // When resolving IPv6 literals, there's no benefit to artificially |
| 2432 | // limiting our resolution based on a probe. Prior logic ensures |
| 2433 | // that this query is UNSPECIFIED (see effective_address_family |
| 2434 | // check above) so the code requesting the resolution should be amenable |
| 2435 | // to receiving a IPv6 resolution. |
| 2436 | !use_local_ipv6_ && ip_address == nullptr && !IsIPv6Reachable(net_log)) { |
| 2437 | effective_address_family = ADDRESS_FAMILY_IPV4; |
| 2438 | effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 2439 | } |
| 2440 | |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 2441 | return Key(info.hostname(), effective_address_family, effective_flags); |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2442 | } |
| 2443 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2444 | bool HostResolverImpl::IsIPv6Reachable(const NetLogWithSource& net_log) { |
eroman | 1a17ef3 | 2016-12-14 01:06:09 | [diff] [blame] | 2445 | // Cache the result for kIPv6ProbePeriodMs (measured from after |
| 2446 | // IsGloballyReachable() completes). |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2447 | bool cached = true; |
eroman | 1a17ef3 | 2016-12-14 01:06:09 | [diff] [blame] | 2448 | if ((base::TimeTicks::Now() - last_ipv6_probe_time_).InMilliseconds() > |
| 2449 | kIPv6ProbePeriodMs) { |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 2450 | last_ipv6_probe_result_ = |
| 2451 | IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log); |
eroman | 1a17ef3 | 2016-12-14 01:06:09 | [diff] [blame] | 2452 | last_ipv6_probe_time_ = base::TimeTicks::Now(); |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2453 | cached = false; |
| 2454 | } |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2455 | net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK, |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2456 | base::Bind(&NetLogIPv6AvailableCallback, |
| 2457 | last_ipv6_probe_result_, cached)); |
| 2458 | return last_ipv6_probe_result_; |
| 2459 | } |
| 2460 | |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 2461 | void HostResolverImpl::RunLoopbackProbeJob() { |
| 2462 | new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr(), |
| 2463 | worker_task_runner_.get()); |
| 2464 | } |
| 2465 | |
[email protected] | 35ddc28 | 2010-09-21 23:42:06 | [diff] [blame] | 2466 | void HostResolverImpl::AbortAllInProgressJobs() { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2467 | // In Abort, a Request callback could spawn new Jobs with matching keys, so |
| 2468 | // first collect and remove all running jobs from |jobs_|. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 2469 | std::vector<std::unique_ptr<Job>> jobs_to_abort; |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2470 | for (auto it = jobs_.begin(); it != jobs_.end();) { |
| 2471 | Job* job = it->second.get(); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2472 | if (job->is_running()) { |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2473 | jobs_to_abort.push_back(std::move(it->second)); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2474 | jobs_.erase(it++); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2475 | } else { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2476 | DCHECK(job->is_queued()); |
| 2477 | ++it; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2478 | } |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2479 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2480 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2481 | // Pause the dispatcher so it won't start any new dispatcher jobs while |
| 2482 | // aborting the old ones. This is needed so that it won't start the second |
| 2483 | // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became |
| 2484 | // invalid. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2485 | PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits(); |
| 2486 | dispatcher_->SetLimits( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2487 | PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 2488 | |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2489 | // Life check to bail once |this| is deleted. |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 2490 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2491 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2492 | // Then Abort them. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 2493 | for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) { |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2494 | jobs_to_abort[i]->Abort(); |
olli.raula | a21e9eb7 | 2015-12-17 08:18:11 | [diff] [blame] | 2495 | ignore_result(jobs_to_abort[i].release()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2496 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2497 | |
| 2498 | if (self) |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2499 | dispatcher_->SetLimits(limits); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | void HostResolverImpl::AbortDnsTasks() { |
| 2503 | // Pause the dispatcher so it won't start any new dispatcher jobs while |
| 2504 | // aborting the old ones. This is needed so that it won't start the second |
| 2505 | // DnsTransaction for a job if the DnsConfig just changed. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2506 | PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits(); |
| 2507 | dispatcher_->SetLimits( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2508 | PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); |
| 2509 | |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2510 | for (auto it = jobs_.begin(); it != jobs_.end(); ++it) |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2511 | it->second->AbortDnsTask(); |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2512 | dispatcher_->SetLimits(limits); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2513 | } |
| 2514 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2515 | void HostResolverImpl::TryServingAllJobsFromHosts() { |
| 2516 | if (!HaveDnsConfig()) |
| 2517 | return; |
| 2518 | |
| 2519 | // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 2520 | // http://crbug.com/117655 |
| 2521 | |
| 2522 | // Life check to bail once |this| is deleted. |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 2523 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2524 | |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 2525 | for (auto it = jobs_.begin(); self.get() && it != jobs_.end();) { |
| 2526 | Job* job = it->second.get(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2527 | ++it; |
| 2528 | // This could remove |job| from |jobs_|, but iterator will remain valid. |
| 2529 | job->ServeFromHosts(); |
| 2530 | } |
| 2531 | } |
| 2532 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2533 | void HostResolverImpl::OnIPAddressChanged() { |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 2534 | resolved_known_ipv6_hostname_ = false; |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2535 | last_ipv6_probe_time_ = base::TimeTicks(); |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 2536 | // Abandon all ProbeJobs. |
| 2537 | probe_weak_ptr_factory_.InvalidateWeakPtrs(); |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2538 | if (cache_.get()) { |
mgersh | 5bb3b9d | 2017-02-13 21:27:10 | [diff] [blame] | 2539 | cache_->OnNetworkChange(); |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2540 | cache_hit_callbacks_.clear(); |
| 2541 | } |
[email protected] | 7c466e9 | 2013-07-20 01:44:48 | [diff] [blame] | 2542 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
mmenke | 91c1716 | 2016-06-02 16:03:23 | [diff] [blame] | 2543 | RunLoopbackProbeJob(); |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2544 | #endif |
| 2545 | AbortAllInProgressJobs(); |
| 2546 | // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2547 | } |
| 2548 | |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 2549 | void HostResolverImpl::OnConnectionTypeChanged( |
| 2550 | NetworkChangeNotifier::ConnectionType type) { |
| 2551 | proc_params_.unresponsive_delay = |
| 2552 | GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( |
| 2553 | "DnsUnresponsiveDelayMsByConnectionType", |
| 2554 | base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs), |
| 2555 | type); |
| 2556 | } |
| 2557 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2558 | void HostResolverImpl::OnInitialDNSConfigRead() { |
| 2559 | UpdateDNSConfig(false); |
| 2560 | } |
| 2561 | |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 2562 | void HostResolverImpl::OnDNSChanged() { |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2563 | UpdateDNSConfig(true); |
| 2564 | } |
| 2565 | |
| 2566 | void HostResolverImpl::UpdateDNSConfig(bool config_changed) { |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 2567 | DnsConfig dns_config; |
| 2568 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 2569 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 2570 | if (net_log_) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2571 | net_log_->AddGlobalEntry(NetLogEventType::DNS_CONFIG_CHANGED, |
| 2572 | base::Bind(&NetLogDnsConfigCallback, &dns_config)); |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 2573 | } |
| 2574 | |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2575 | // TODO(szym): Remove once http://crbug.com/137914 is resolved. |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 2576 | received_dns_config_ = dns_config.IsValid(); |
[email protected] | c9fa8f31 | 2013-09-17 12:24:52 | [diff] [blame] | 2577 | // Conservatively assume local IPv6 is needed when DnsConfig is not valid. |
| 2578 | use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2579 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2580 | num_dns_failures_ = 0; |
| 2581 | |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2582 | // We want a new DnsSession in place, before we Abort running Jobs, so that |
| 2583 | // the newly started jobs use the new config. |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2584 | if (dns_client_.get()) { |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 2585 | dns_client_->SetConfig(dns_config); |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2586 | if (dns_client_->GetConfig()) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2587 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2588 | // If we just switched DnsClients, restart jobs using new resolver. |
| 2589 | // TODO(pauljensen): Is this necessary? |
| 2590 | config_changed = true; |
| 2591 | } |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2592 | } |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2593 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2594 | if (config_changed) { |
| 2595 | // If the DNS server has changed, existing cached info could be wrong so we |
mgersh | 92b1af1 | 2017-03-29 15:49:23 | [diff] [blame] | 2596 | // have to expire our internal cache :( Note that OS level DNS caches, such |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2597 | // as NSCD's cache should be dropped automatically by the OS when |
| 2598 | // resolv.conf changes so we don't need to do anything to clear that cache. |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2599 | if (cache_.get()) { |
mgersh | 92b1af1 | 2017-03-29 15:49:23 | [diff] [blame] | 2600 | cache_->OnNetworkChange(); |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2601 | cache_hit_callbacks_.clear(); |
| 2602 | } |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2603 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2604 | // Life check to bail once |this| is deleted. |
| 2605 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2606 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2607 | // Existing jobs will have been sent to the original server so they need to |
| 2608 | // be aborted. |
| 2609 | AbortAllInProgressJobs(); |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2610 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2611 | // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2612 | if (self.get()) |
| 2613 | TryServingAllJobsFromHosts(); |
| 2614 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | bool HostResolverImpl::HaveDnsConfig() const { |
[email protected] | 32b1dbcf | 2013-01-26 03:48:25 | [diff] [blame] | 2618 | // Use DnsClient only if it's fully configured and there is no override by |
| 2619 | // ScopedDefaultHostResolverProc. |
| 2620 | // The alternative is to use NetworkChangeNotifier to override DnsConfig, |
| 2621 | // but that would introduce construction order requirements for NCN and SDHRP. |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 2622 | return dns_client_ && dns_client_->GetConfig() && |
| 2623 | (proc_params_.resolver_proc || !HostResolverProc::GetDefault()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2624 | } |
| 2625 | |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 2626 | void HostResolverImpl::OnDnsTaskResolve(int net_error) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2627 | DCHECK(dns_client_); |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 2628 | if (net_error == OK) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2629 | num_dns_failures_ = 0; |
| 2630 | return; |
| 2631 | } |
| 2632 | ++num_dns_failures_; |
| 2633 | if (num_dns_failures_ < kMaximumDnsFailures) |
| 2634 | return; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2635 | |
| 2636 | // Disable DnsClient until the next DNS change. Must be done before aborting |
| 2637 | // DnsTasks, since doing so may start new jobs. |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2638 | dns_client_->SetConfig(DnsConfig()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2639 | |
| 2640 | // Switch jobs with active DnsTasks over to using ProcTasks. |
| 2641 | AbortDnsTasks(); |
| 2642 | |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2643 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false); |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 2644 | UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.DnsClientDisabledReason", |
| 2645 | std::abs(net_error)); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2646 | } |
| 2647 | |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2648 | void HostResolverImpl::OnCacheEntryEvicted(const HostCache::Key& key, |
| 2649 | const HostCache::Entry& entry) { |
| 2650 | cache_hit_callbacks_.erase(key); |
| 2651 | } |
| 2652 | |
| 2653 | void HostResolverImpl::MaybeAddCacheHitCallback(const HostCache::Key& key, |
| 2654 | const RequestInfo& info) { |
| 2655 | const RequestInfo::CacheHitCallback& callback = info.cache_hit_callback(); |
| 2656 | if (callback.is_null()) |
| 2657 | return; |
| 2658 | cache_hit_callbacks_[key].push_back(callback); |
| 2659 | } |
| 2660 | |
| 2661 | void HostResolverImpl::RunCacheHitCallbacks(const HostCache::Key& key, |
| 2662 | const RequestInfo& info) { |
| 2663 | auto it = cache_hit_callbacks_.find(key); |
| 2664 | if (it == cache_hit_callbacks_.end()) |
| 2665 | return; |
| 2666 | for (auto& callback : it->second) |
| 2667 | callback.Run(info); |
| 2668 | } |
| 2669 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 2670 | void HostResolverImpl::SetDnsClient(std::unique_ptr<DnsClient> dns_client) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2671 | // DnsClient and config must be updated before aborting DnsTasks, since doing |
| 2672 | // so may start new jobs. |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2673 | dns_client_ = std::move(dns_client); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2674 | if (dns_client_ && !dns_client_->GetConfig() && |
| 2675 | num_dns_failures_ < kMaximumDnsFailures) { |
| 2676 | DnsConfig dns_config; |
| 2677 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2678 | dns_client_->SetConfig(dns_config); |
| 2679 | num_dns_failures_ = 0; |
| 2680 | if (dns_client_->GetConfig()) |
| 2681 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2682 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2683 | |
| 2684 | AbortDnsTasks(); |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2685 | } |
| 2686 | |
juliatuttle | d7fa585 | 2016-07-29 13:16:57 | [diff] [blame] | 2687 | void HostResolverImpl::InitializePersistence( |
| 2688 | const PersistCallback& persist_callback, |
| 2689 | std::unique_ptr<const base::Value> old_data) { |
| 2690 | DCHECK(!persist_initialized_); |
| 2691 | persist_callback_ = persist_callback; |
| 2692 | persist_initialized_ = true; |
| 2693 | if (old_data) |
| 2694 | ApplyPersistentData(std::move(old_data)); |
| 2695 | } |
| 2696 | |
juliatuttle | 7e8bf1c | 2016-08-02 19:09:09 | [diff] [blame] | 2697 | void HostResolverImpl::ApplyPersistentData( |
| 2698 | std::unique_ptr<const base::Value> data) {} |
| 2699 | |
| 2700 | std::unique_ptr<const base::Value> HostResolverImpl::GetPersistentData() { |
| 2701 | return std::unique_ptr<const base::Value>(); |
| 2702 | } |
| 2703 | |
juliatuttle | d7fa585 | 2016-07-29 13:16:57 | [diff] [blame] | 2704 | void HostResolverImpl::SchedulePersist() { |
| 2705 | if (!persist_initialized_ || persist_timer_.IsRunning()) |
| 2706 | return; |
| 2707 | persist_timer_.Start( |
| 2708 | FROM_HERE, base::TimeDelta::FromSeconds(kPersistDelaySec), |
| 2709 | base::Bind(&HostResolverImpl::DoPersist, weak_ptr_factory_.GetWeakPtr())); |
| 2710 | } |
| 2711 | |
| 2712 | void HostResolverImpl::DoPersist() { |
| 2713 | DCHECK(persist_initialized_); |
| 2714 | persist_callback_.Run(GetPersistentData()); |
| 2715 | } |
| 2716 | |
maksim.sisov | 31452af | 2016-07-27 06:38:10 | [diff] [blame] | 2717 | HostResolverImpl::RequestImpl::~RequestImpl() { |
| 2718 | if (job_) |
| 2719 | job_->CancelRequest(this); |
| 2720 | } |
| 2721 | |
| 2722 | void HostResolverImpl::RequestImpl::ChangeRequestPriority( |
| 2723 | RequestPriority priority) { |
| 2724 | job_->ChangeRequestPriority(this, priority); |
| 2725 | } |
| 2726 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2727 | } // namespace net |