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