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