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