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