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