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