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