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