blob: 9134bbdee36acd5da5f4d9e4eda7d395362234cd [file] [log] [blame]
[email protected]a2730882012-01-21 00:56:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b59ff372009-07-15 22:04:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f2cb3cf2013-03-21 01:40:535#include "net/dns/host_resolver_impl.h"
[email protected]b59ff372009-07-15 22:04:326
[email protected]21526002010-05-16 19:42:467#if defined(OS_WIN)
8#include <Winsock2.h>
Fabrice de Gans-Riberi7de47372018-05-08 20:23:479#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]21526002010-05-16 19:42:4610#include <netdb.h>
tfarinada9e1362017-03-14 16:49:0611#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-Riberi7de47372018-05-08 20:23:4718#endif // defined(OS_WIN)
tfarinada9e1362017-03-14 16:49:0619
[email protected]68ad3ee2010-01-30 03:45:3920#include <cmath>
thestiga74ad2b2016-07-11 20:52:3621#include <memory>
[email protected]0f292de02012-02-01 22:28:2022#include <utility>
[email protected]21526002010-05-16 19:42:4623#include <vector>
[email protected]68ad3ee2010-01-30 03:45:3924
[email protected]33152acc2011-10-20 23:37:1225#include "base/bind.h"
[email protected]aa22b242011-11-16 18:58:2926#include "base/bind_helpers.h"
[email protected]0f292de02012-02-01 22:28:2027#include "base/callback.h"
maksim.sisov31452af2016-07-27 06:38:1028#include "base/callback_helpers.h"
[email protected]b59ff372009-07-15 22:04:3229#include "base/compiler_specific.h"
David Benjamina5677192018-04-23 23:25:4230#include "base/containers/linked_list.h"
[email protected]58580352010-10-26 04:07:5031#include "base/debug/debugger.h"
32#include "base/debug/stack_trace.h"
Avi Drissman13fc8932015-12-20 04:40:4633#include "base/macros.h"
mmenke91c17162016-06-02 16:03:2334#include "base/memory/ptr_util.h"
[email protected]1e9bbd22010-10-15 16:42:4535#include "base/metrics/field_trial.h"
Maks Orlovich5393c8682018-02-26 16:17:5036#include "base/metrics/field_trial_params.h"
Ilya Sherman0eb39802017-12-08 20:58:1837#include "base/metrics/histogram_functions.h"
davidbenad6fc442015-05-18 20:59:1338#include "base/metrics/histogram_macros.h"
anujk.sharma7c907f02015-05-29 02:55:4439#include "base/single_thread_task_runner.h"
[email protected]be528af2013-06-11 07:39:4840#include "base/strings/string_util.h"
[email protected]750b2f3c2013-06-07 18:41:0541#include "base/strings/utf_string_conversions.h"
Francois Doraya2d01ba2017-09-25 19:17:4042#include "base/task_scheduler/post_task.h"
43#include "base/threading/scoped_blocking_call.h"
gabf767595f2016-05-11 18:50:3544#include "base/threading/thread_task_runner_handle.h"
[email protected]66e96c42013-06-28 15:20:3145#include "base/time/time.h"
ssid6d6b40102016-04-05 18:59:5646#include "base/trace_event/trace_event.h"
[email protected]21526002010-05-16 19:42:4647#include "base/values.h"
Fabrice de Gans-Riberi7de47372018-05-08 20:23:4748#include "build/build_config.h"
[email protected]b3601bc22012-02-21 21:23:2049#include "net/base/address_family.h"
[email protected]b59ff372009-07-15 22:04:3250#include "net/base/address_list.h"
[email protected]ee094b82010-08-24 15:55:5151#include "net/base/host_port_pair.h"
martijna23c8962016-03-04 18:18:5152#include "net/base/ip_address.h"
[email protected]1c7cf3f82014-08-07 21:33:4853#include "net/base/ip_endpoint.h"
[email protected]2bb04442010-08-18 18:01:1554#include "net/base/net_errors.h"
xunjieli0b7f5b62016-12-06 20:43:4855#include "net/base/trace_constants.h"
tfarina9ed7f8c52016-02-19 17:50:1856#include "net/base/url_util.h"
[email protected]0adcb2b2012-08-15 21:30:4657#include "net/dns/address_sorter.h"
[email protected]78eac2a2012-03-14 19:09:2758#include "net/dns/dns_client.h"
[email protected]b3601bc22012-02-21 21:23:2059#include "net/dns/dns_config_service.h"
60#include "net/dns/dns_protocol.h"
tfarina47598f042015-10-07 23:08:3561#include "net/dns/dns_reloader.h"
[email protected]b3601bc22012-02-21 21:23:2062#include "net/dns/dns_response.h"
[email protected]b3601bc22012-02-21 21:23:2063#include "net/dns/dns_transaction.h"
tfarina77021d62015-10-11 20:19:0364#include "net/dns/dns_util.h"
[email protected]f2cb3cf2013-03-21 01:40:5365#include "net/dns/host_resolver_proc.h"
eroman87c53d62015-04-02 06:51:0766#include "net/log/net_log.h"
mikecironef22f9812016-10-04 03:40:1967#include "net/log/net_log_capture_mode.h"
mikecirone8b85c432016-09-08 19:11:0068#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1969#include "net/log/net_log_parameters_callback.h"
70#include "net/log/net_log_source.h"
mikecirone8b85c432016-09-08 19:11:0071#include "net/log/net_log_source_type.h"
mikecironef22f9812016-10-04 03:40:1972#include "net/log/net_log_with_source.h"
[email protected]9db6f702013-04-10 18:10:5173#include "net/socket/client_socket_factory.h"
tfarina5dd13c22016-11-16 12:08:2674#include "net/socket/datagram_client_socket.h"
pauljensen370f1c72015-02-17 16:59:1475#include "url/url_canon_ip.h"
[email protected]b59ff372009-07-15 22:04:3276
77#if defined(OS_WIN)
78#include "net/base/winsock_init.h"
79#endif
80
tfarinada9e1362017-03-14 16:49:0681#if defined(OS_ANDROID)
82#include "net/android/network_library.h"
83#endif
84
[email protected]b59ff372009-07-15 22:04:3285namespace net {
86
[email protected]e95d3aca2010-01-11 22:47:4387namespace {
88
ttuttlecf1158bf2016-03-18 16:37:4489// Default delay between calls to the system resolver for the same hostname.
90// (Can be overridden by field trial.)
91const int64_t kDnsDefaultUnresponsiveDelayMs = 6000;
92
[email protected]6e78dfb2011-07-28 21:34:4793// Limit the size of hostnames that will be resolved to combat issues in
94// some platform's resolvers.
95const size_t kMaxHostLength = 4096;
96
[email protected]a2730882012-01-21 00:56:2797// Default TTL for successful resolutions with ProcTask.
98const unsigned kCacheEntryTTLSeconds = 60;
99
[email protected]b3601bc22012-02-21 21:23:20100// Default TTL for unsuccessful resolutions with ProcTask.
101const unsigned kNegativeCacheEntryTTLSeconds = 0;
102
[email protected]895123222012-10-25 15:21:17103// Minimum TTL for successful resolutions with DnsTask.
104const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
105
sergeyub8cdc212015-05-14 18:50:37106// Time between IPv6 probes, i.e. for how long results of each IPv6 probe are
107// cached.
108const int kIPv6ProbePeriodMs = 1000;
109
110// Google DNS address used for IPv6 probes.
111const uint8_t kIPv6ProbeAddress[] =
112 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
114
[email protected]24f4bab2010-10-15 01:27:11115// We use a separate histogram name for each platform to facilitate the
116// display of error codes by their symbolic name (since each platform has
117// different mappings).
118const char kOSErrorsForGetAddrinfoHistogramName[] =
119#if defined(OS_WIN)
120 "Net.OSErrorsForGetAddrinfo_Win";
121#elif defined(OS_MACOSX)
122 "Net.OSErrorsForGetAddrinfo_Mac";
123#elif defined(OS_LINUX)
124 "Net.OSErrorsForGetAddrinfo_Linux";
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47125#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]24f4bab2010-10-15 01:27:11126 "Net.OSErrorsForGetAddrinfo";
127#endif
128
[email protected]c89b2442011-05-26 14:28:27129// Gets a list of the likely error codes that getaddrinfo() can return
130// (non-exhaustive). These are the error codes that we will track via
131// a histogram.
132std::vector<int> GetAllGetAddrinfoOSErrors() {
133 int os_errors[] = {
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47134#if defined(OS_WIN)
135 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
136 WSA_NOT_ENOUGH_MEMORY,
137 WSAEAFNOSUPPORT,
138 WSAEINVAL,
139 WSAESOCKTNOSUPPORT,
140 WSAHOST_NOT_FOUND,
141 WSANO_DATA,
142 WSANO_RECOVERY,
143 WSANOTINITIALISED,
144 WSATRY_AGAIN,
145 WSATYPE_NOT_FOUND,
146 // The following are not in doc, but might be to appearing in results :-(.
147 WSA_INVALID_HANDLE,
148#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]23f771162011-06-02 18:37:51149#if !defined(OS_FREEBSD)
[email protected]39588992011-07-11 19:54:37150#if !defined(OS_ANDROID)
[email protected]c48aef92011-11-22 23:41:45151 // EAI_ADDRFAMILY has been declared obsolete in Android's and
152 // FreeBSD's netdb.h.
[email protected]c89b2442011-05-26 14:28:27153 EAI_ADDRFAMILY,
[email protected]39588992011-07-11 19:54:37154#endif
[email protected]c48aef92011-11-22 23:41:45155 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
[email protected]23f771162011-06-02 18:37:51156 EAI_NODATA,
157#endif
[email protected]c89b2442011-05-26 14:28:27158 EAI_AGAIN,
159 EAI_BADFLAGS,
160 EAI_FAIL,
161 EAI_FAMILY,
162 EAI_MEMORY,
[email protected]c89b2442011-05-26 14:28:27163 EAI_NONAME,
164 EAI_SERVICE,
165 EAI_SOCKTYPE,
166 EAI_SYSTEM,
[email protected]c89b2442011-05-26 14:28:27167#endif
168 };
169
170 // Ensure all errors are positive, as histogram only tracks positive values.
171 for (size_t i = 0; i < arraysize(os_errors); ++i) {
172 os_errors[i] = std::abs(os_errors[i]);
173 }
174
Ryan Sleevi9c79c3542018-05-12 01:38:09175 return base::CustomHistogram::ArrayToCustomEnumRanges(os_errors);
[email protected]c89b2442011-05-26 14:28:27176}
177
[email protected]1def74c2012-03-22 20:07:00178enum DnsResolveStatus {
179 RESOLVE_STATUS_DNS_SUCCESS = 0,
180 RESOLVE_STATUS_PROC_SUCCESS,
181 RESOLVE_STATUS_FAIL,
[email protected]1d932852012-06-19 19:40:33182 RESOLVE_STATUS_SUSPECT_NETBIOS,
[email protected]1def74c2012-03-22 20:07:00183 RESOLVE_STATUS_MAX
184};
185
eroman91dd3602015-03-26 03:46:33186// ICANN uses this localhost address to indicate a name collision.
187//
188// The policy in Chromium is to fail host resolving if it resolves to
189// this special address.
190//
191// Not however that IP literals are exempt from this policy, so it is still
192// possible to navigate to http://127.0.53.53/ directly.
193//
194// For more details: https://www.icann.org/news/announcement-2-2014-08-01-en
martijna23c8962016-03-04 18:18:51195const uint8_t kIcanNameCollisionIp[] = {127, 0, 53, 53};
eroman91dd3602015-03-26 03:46:33196
eroman1efc237c2016-12-14 00:00:45197bool ContainsIcannNameCollisionIp(const AddressList& addr_list) {
198 for (const auto& endpoint : addr_list) {
199 const IPAddress& addr = endpoint.address();
200 if (addr.IsIPv4() && IPAddressStartsWith(addr, kIcanNameCollisionIp)) {
201 return true;
202 }
203 }
204 return false;
205}
206
[email protected]1def74c2012-03-22 20:07:00207void UmaAsyncDnsResolveStatus(DnsResolveStatus result) {
208 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
209 result,
210 RESOLVE_STATUS_MAX);
211}
212
[email protected]1d932852012-06-19 19:40:33213bool ResemblesNetBIOSName(const std::string& hostname) {
214 return (hostname.size() < 16) && (hostname.find('.') == std::string::npos);
215}
216
217// True if |hostname| ends with either ".local" or ".local.".
218bool ResemblesMulticastDNSName(const std::string& hostname) {
219 DCHECK(!hostname.empty());
220 const char kSuffix[] = ".local.";
221 const size_t kSuffixLen = sizeof(kSuffix) - 1;
222 const size_t kSuffixLenTrimmed = kSuffixLen - 1;
thestiga74ad2b2016-07-11 20:52:36223 if (hostname.back() == '.') {
[email protected]1d932852012-06-19 19:40:33224 return hostname.size() > kSuffixLen &&
225 !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix);
226 }
227 return hostname.size() > kSuffixLenTrimmed &&
228 !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed,
229 kSuffix, kSuffixLenTrimmed);
230}
231
[email protected]51b9a6b2012-06-25 21:50:29232// A macro to simplify code and readability.
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21233#define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
234 do { \
235 switch (priority) { \
236 case HIGHEST: \
Miriam Gershenson90d05e02017-09-28 19:29:28237 UMA_HISTOGRAM_LONG_TIMES_100(basename ".HIGHEST", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21238 break; \
239 case MEDIUM: \
Miriam Gershenson90d05e02017-09-28 19:29:28240 UMA_HISTOGRAM_LONG_TIMES_100(basename ".MEDIUM", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21241 break; \
242 case LOW: \
Miriam Gershenson90d05e02017-09-28 19:29:28243 UMA_HISTOGRAM_LONG_TIMES_100(basename ".LOW", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21244 break; \
245 case LOWEST: \
Miriam Gershenson90d05e02017-09-28 19:29:28246 UMA_HISTOGRAM_LONG_TIMES_100(basename ".LOWEST", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21247 break; \
248 case IDLE: \
Miriam Gershenson90d05e02017-09-28 19:29:28249 UMA_HISTOGRAM_LONG_TIMES_100(basename ".IDLE", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21250 break; \
251 case THROTTLED: \
Miriam Gershenson90d05e02017-09-28 19:29:28252 UMA_HISTOGRAM_LONG_TIMES_100(basename ".THROTTLED", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21253 break; \
254 } \
255 UMA_HISTOGRAM_LONG_TIMES_100(basename, time); \
[email protected]51b9a6b2012-06-25 21:50:29256 } while (0)
257
258// Record time from Request creation until a valid DNS response.
Miriam Gershenson90d05e02017-09-28 19:29:28259void RecordTotalTime(bool speculative,
260 bool from_cache,
[email protected]51b9a6b2012-06-25 21:50:29261 base::TimeDelta duration) {
Miriam Gershenson7ba03e12018-02-07 22:48:24262 if (!speculative) {
Miriam Gershenson90d05e02017-09-28 19:29:28263 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.TotalTime", duration);
Miriam Gershenson90d05e02017-09-28 19:29:28264
Miriam Gershenson7ba03e12018-02-07 22:48:24265 if (!from_cache)
Miriam Gershenson90d05e02017-09-28 19:29:28266 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.TotalTimeNotCached", duration);
[email protected]51b9a6b2012-06-25 21:50:29267 }
268}
269
[email protected]1339a2a22012-10-17 08:39:43270void RecordTTL(base::TimeDelta ttl) {
271 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl,
272 base::TimeDelta::FromSeconds(1),
273 base::TimeDelta::FromDays(1), 100);
274}
275
[email protected]16c2bd72013-06-28 01:19:22276bool ConfigureAsyncDnsNoFallbackFieldTrial() {
277 const bool kDefault = false;
278
279 // Configure the AsyncDns field trial as follows:
280 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
281 // groups AsyncDnsA and AsyncDnsB: return false,
282 // groups SystemDnsA and SystemDnsB: return false,
283 // otherwise (trial absent): return default.
284 std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
brettw3a2c6902015-07-06 19:43:29285 if (!group_name.empty()) {
286 return base::StartsWith(group_name, "AsyncDnsNoFallback",
287 base::CompareCase::INSENSITIVE_ASCII);
288 }
[email protected]16c2bd72013-06-28 01:19:22289 return kDefault;
290}
291
Maks Orlovich5393c8682018-02-26 16:17:50292const base::FeatureParam<base::TaskPriority>::Option prio_modes[] = {
293 {base::TaskPriority::USER_VISIBLE, "default"},
294 {base::TaskPriority::USER_BLOCKING, "user_blocking"}};
295const base::Feature kSystemResolverPriorityExperiment = {
296 "SystemResolverPriorityExperiment", base::FEATURE_DISABLED_BY_DEFAULT};
297const base::FeatureParam<base::TaskPriority> priority_mode{
298 &kSystemResolverPriorityExperiment, "mode",
299 base::TaskPriority::USER_VISIBLE, &prio_modes};
300
[email protected]d7b9a2b2012-05-31 22:31:19301//-----------------------------------------------------------------------------
302
Avi Drissman13fc8932015-12-20 04:40:46303AddressList EnsurePortOnAddressList(const AddressList& list, uint16_t port) {
[email protected]895123222012-10-25 15:21:17304 if (list.empty() || list.front().port() == port)
305 return list;
306 return AddressList::CopyWithPort(list, port);
[email protected]7054e78f2012-05-07 21:44:56307}
308
[email protected]ec666ab22013-04-17 20:05:59309// Returns true if |addresses| contains only IPv4 loopback addresses.
310bool IsAllIPv4Loopback(const AddressList& addresses) {
311 for (unsigned i = 0; i < addresses.size(); ++i) {
martijna23c8962016-03-04 18:18:51312 const IPAddress& address = addresses[i].address();
[email protected]ec666ab22013-04-17 20:05:59313 switch (addresses[i].GetFamily()) {
314 case ADDRESS_FAMILY_IPV4:
martijna23c8962016-03-04 18:18:51315 if (address.bytes()[0] != 127)
[email protected]ec666ab22013-04-17 20:05:59316 return false;
317 break;
318 case ADDRESS_FAMILY_IPV6:
319 return false;
320 default:
321 NOTREACHED();
322 return false;
323 }
324 }
325 return true;
326}
327
tfarinada9e1362017-03-14 16:49:06328// Returns true if it can determine that only loopback addresses are configured.
329// i.e. if only 127.0.0.1 and ::1 are routable.
330// Also returns false if it cannot determine this.
331bool HaveOnlyLoopbackAddresses() {
Francois Doraya2d01ba2017-09-25 19:17:40332 base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::WILL_BLOCK);
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47333#if defined(OS_WIN)
334 // TODO(wtc): implement with the GetAdaptersAddresses function.
335 NOTIMPLEMENTED();
336 return false;
337#elif defined(OS_ANDROID)
tfarinada9e1362017-03-14 16:49:06338 return android::HaveOnlyLoopbackAddresses();
339#elif defined(OS_NACL)
340 NOTIMPLEMENTED();
341 return false;
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47342#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
tfarinada9e1362017-03-14 16:49:06343 struct ifaddrs* interface_addr = NULL;
344 int rv = getifaddrs(&interface_addr);
345 if (rv != 0) {
346 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
347 return false;
348 }
349
350 bool result = true;
351 for (struct ifaddrs* interface = interface_addr;
352 interface != NULL;
353 interface = interface->ifa_next) {
354 if (!(IFF_UP & interface->ifa_flags))
355 continue;
356 if (IFF_LOOPBACK & interface->ifa_flags)
357 continue;
358 const struct sockaddr* addr = interface->ifa_addr;
359 if (!addr)
360 continue;
361 if (addr->sa_family == AF_INET6) {
362 // Safe cast since this is AF_INET6.
363 const struct sockaddr_in6* addr_in6 =
364 reinterpret_cast<const struct sockaddr_in6*>(addr);
365 const struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
366 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr))
367 continue;
368 }
369 if (addr->sa_family != AF_INET6 && addr->sa_family != AF_INET)
370 continue;
371
372 result = false;
373 break;
374 }
375 freeifaddrs(interface_addr);
376 return result;
tfarinada9e1362017-03-14 16:49:06377#endif // defined(various platforms)
378}
379
[email protected]cd565142012-06-12 16:21:45380// Creates NetLog parameters when the resolve failed.
danakj22f90e72016-04-16 01:55:40381std::unique_ptr<base::Value> NetLogProcTaskFailedCallback(
Avi Drissman13fc8932015-12-20 04:40:46382 uint32_t attempt_number,
eroman001c3742015-04-23 03:11:17383 int net_error,
384 int os_error,
385 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40386 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]cd565142012-06-12 16:21:45387 if (attempt_number)
388 dict->SetInteger("attempt_number", attempt_number);
[email protected]21526002010-05-16 19:42:46389
[email protected]cd565142012-06-12 16:21:45390 dict->SetInteger("net_error", net_error);
[email protected]13024882011-05-18 23:19:16391
[email protected]cd565142012-06-12 16:21:45392 if (os_error) {
393 dict->SetInteger("os_error", os_error);
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47394#if defined(OS_WIN)
[email protected]cd565142012-06-12 16:21:45395 // Map the error code to a human-readable string.
wezb9820d42016-06-22 23:41:04396 LPWSTR error_string = nullptr;
Peter Kastingbe940e92014-11-20 23:14:08397 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
398 0, // Use the internal message table.
399 os_error,
400 0, // Use default language.
401 (LPWSTR)&error_string,
402 0, // Buffer size.
403 0); // Arguments (unused).
[email protected]ad65a3e2013-12-25 18:18:01404 dict->SetString("os_error_string", base::WideToUTF8(error_string));
[email protected]cd565142012-06-12 16:21:45405 LocalFree(error_string);
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47406#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
407 dict->SetString("os_error_string", gai_strerror(os_error));
[email protected]21526002010-05-16 19:42:46408#endif
[email protected]21526002010-05-16 19:42:46409 }
410
dchengc7eeda422015-12-26 03:56:48411 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45412}
[email protected]a9813302012-04-28 09:29:28413
[email protected]cd565142012-06-12 16:21:45414// Creates NetLog parameters when the DnsTask failed.
danakj22f90e72016-04-16 01:55:40415std::unique_ptr<base::Value> NetLogDnsTaskFailedCallback(
estade5e5529d2015-05-21 20:59:11416 int net_error,
417 int dns_error,
418 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40419 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]cd565142012-06-12 16:21:45420 dict->SetInteger("net_error", net_error);
421 if (dns_error)
422 dict->SetInteger("dns_error", dns_error);
dchengc7eeda422015-12-26 03:56:48423 return std::move(dict);
thestiga74ad2b2016-07-11 20:52:36424}
[email protected]ee094b82010-08-24 15:55:51425
[email protected]cd565142012-06-12 16:21:45426// Creates NetLog parameters containing the information in a RequestInfo object,
mikecironef22f9812016-10-04 03:40:19427// along with the associated NetLogSource.
danakj22f90e72016-04-16 01:55:40428std::unique_ptr<base::Value> NetLogRequestInfoCallback(
estade5e5529d2015-05-21 20:59:11429 const HostResolver::RequestInfo* info,
430 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40431 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b3601bc22012-02-21 21:23:20432
[email protected]cd565142012-06-12 16:21:45433 dict->SetString("host", info->host_port_pair().ToString());
434 dict->SetInteger("address_family",
435 static_cast<int>(info->address_family()));
436 dict->SetBoolean("allow_cached_response", info->allow_cached_response());
437 dict->SetBoolean("is_speculative", info->is_speculative());
dchengc7eeda422015-12-26 03:56:48438 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45439}
[email protected]b3601bc22012-02-21 21:23:20440
[email protected]cd565142012-06-12 16:21:45441// Creates NetLog parameters for the creation of a HostResolverImpl::Job.
danakj22f90e72016-04-16 01:55:40442std::unique_ptr<base::Value> NetLogJobCreationCallback(
mikecironef22f9812016-10-04 03:40:19443 const NetLogSource& source,
estade5e5529d2015-05-21 20:59:11444 const std::string* host,
445 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40446 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
estade5e5529d2015-05-21 20:59:11447 source.AddToEventParameters(dict.get());
[email protected]cd565142012-06-12 16:21:45448 dict->SetString("host", *host);
dchengc7eeda422015-12-26 03:56:48449 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45450}
[email protected]a9813302012-04-28 09:29:28451
[email protected]cd565142012-06-12 16:21:45452// Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
danakj22f90e72016-04-16 01:55:40453std::unique_ptr<base::Value> NetLogJobAttachCallback(
mikecironef22f9812016-10-04 03:40:19454 const NetLogSource& source,
estade5e5529d2015-05-21 20:59:11455 RequestPriority priority,
456 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40457 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
estade5e5529d2015-05-21 20:59:11458 source.AddToEventParameters(dict.get());
[email protected]3b04d1f22013-10-16 00:23:56459 dict->SetString("priority", RequestPriorityToString(priority));
dchengc7eeda422015-12-26 03:56:48460 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45461}
[email protected]b3601bc22012-02-21 21:23:20462
[email protected]cd565142012-06-12 16:21:45463// Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
danakj22f90e72016-04-16 01:55:40464std::unique_ptr<base::Value> NetLogDnsConfigCallback(
estade5e5529d2015-05-21 20:59:11465 const DnsConfig* config,
466 NetLogCaptureMode /* capture_mode */) {
tfhef3618f2016-01-11 23:07:08467 return config->ToValue();
[email protected]cd565142012-06-12 16:21:45468}
[email protected]b4481b222012-03-16 17:13:11469
danakj22f90e72016-04-16 01:55:40470std::unique_ptr<base::Value> NetLogIPv6AvailableCallback(
estade5e5529d2015-05-21 20:59:11471 bool ipv6_available,
472 bool cached,
473 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40474 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
sergeyub8cdc212015-05-14 18:50:37475 dict->SetBoolean("ipv6_available", ipv6_available);
476 dict->SetBoolean("cached", cached);
dchengc7eeda422015-12-26 03:56:48477 return std::move(dict);
sergeyub8cdc212015-05-14 18:50:37478}
479
[email protected]0f292de02012-02-01 22:28:20480// The logging routines are defined here because some requests are resolved
481// without a Request object.
482
483// Logs when a request has just been started.
tfarina428341112016-09-22 13:38:20484void LogStartRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20485 const HostResolver::RequestInfo& info) {
mikecirone8b85c432016-09-08 19:11:00486 source_net_log.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST,
487 base::Bind(&NetLogRequestInfoCallback, &info));
[email protected]0f292de02012-02-01 22:28:20488}
489
490// Logs when a request has just completed (before its callback is run).
tfarina428341112016-09-22 13:38:20491void LogFinishRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20492 const HostResolver::RequestInfo& info,
[email protected]b3601bc22012-02-21 21:23:20493 int net_error) {
xunjieli26f90452014-11-10 16:23:02494 source_net_log.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00495 NetLogEventType::HOST_RESOLVER_IMPL_REQUEST, net_error);
[email protected]0f292de02012-02-01 22:28:20496}
497
498// Logs when a request has been cancelled.
tfarina428341112016-09-22 13:38:20499void LogCancelRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20500 const HostResolverImpl::RequestInfo& info) {
mikecirone8b85c432016-09-08 19:11:00501 source_net_log.AddEvent(NetLogEventType::CANCELLED);
502 source_net_log.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST);
[email protected]0f292de02012-02-01 22:28:20503}
504
[email protected]b59ff372009-07-15 22:04:32505//-----------------------------------------------------------------------------
506
[email protected]0f292de02012-02-01 22:28:20507// Keeps track of the highest priority.
508class PriorityTracker {
509 public:
[email protected]8c98d002012-07-18 19:02:27510 explicit PriorityTracker(RequestPriority initial_priority)
511 : highest_priority_(initial_priority), total_count_(0) {
[email protected]0f292de02012-02-01 22:28:20512 memset(counts_, 0, sizeof(counts_));
513 }
514
515 RequestPriority highest_priority() const {
516 return highest_priority_;
517 }
518
519 size_t total_count() const {
520 return total_count_;
521 }
522
523 void Add(RequestPriority req_priority) {
524 ++total_count_;
525 ++counts_[req_priority];
[email protected]31ae7ab2012-04-24 21:09:05526 if (highest_priority_ < req_priority)
[email protected]0f292de02012-02-01 22:28:20527 highest_priority_ = req_priority;
528 }
529
530 void Remove(RequestPriority req_priority) {
531 DCHECK_GT(total_count_, 0u);
532 DCHECK_GT(counts_[req_priority], 0u);
533 --total_count_;
534 --counts_[req_priority];
535 size_t i;
thestiga74ad2b2016-07-11 20:52:36536 for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i) {
537 }
[email protected]0f292de02012-02-01 22:28:20538 highest_priority_ = static_cast<RequestPriority>(i);
539
[email protected]31ae7ab2012-04-24 21:09:05540 // In absence of requests, default to MINIMUM_PRIORITY.
541 if (total_count_ == 0)
542 DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
[email protected]0f292de02012-02-01 22:28:20543 }
544
545 private:
546 RequestPriority highest_priority_;
547 size_t total_count_;
548 size_t counts_[NUM_PRIORITIES];
549};
550
juliatuttle9fb7aeb2016-06-06 20:16:33551void MakeNotStale(HostCache::EntryStaleness* stale_info) {
552 if (!stale_info)
553 return;
554 stale_info->expired_by = base::TimeDelta::FromSeconds(-1);
555 stale_info->network_changes = 0;
556 stale_info->stale_hits = 0;
557}
558
[email protected]c54a8912012-10-22 22:09:43559} // namespace
[email protected]0f292de02012-02-01 22:28:20560
561//-----------------------------------------------------------------------------
562
tfarina9ed7f8c52016-02-19 17:50:18563bool ResolveLocalHostname(base::StringPiece host,
564 uint16_t port,
565 AddressList* address_list) {
tfarina9ed7f8c52016-02-19 17:50:18566 address_list->clear();
567
568 bool is_local6;
569 if (!IsLocalHostname(host, &is_local6))
570 return false;
571
martijna23c8962016-03-04 18:18:51572 address_list->push_back(IPEndPoint(IPAddress::IPv6Localhost(), port));
tfarina9ed7f8c52016-02-19 17:50:18573 if (!is_local6) {
martijna23c8962016-03-04 18:18:51574 address_list->push_back(IPEndPoint(IPAddress::IPv4Localhost(), port));
tfarina9ed7f8c52016-02-19 17:50:18575 }
576
577 return true;
578}
579
[email protected]daae1322013-09-05 18:26:50580const unsigned HostResolverImpl::kMaximumDnsFailures = 16;
581
Eric Roman91aab1e62018-04-24 20:31:32582// Holds the callback and request parameters for an outstanding request.
583//
584// The RequestImpl is owned by the end user of host resolution. Deletion prior
585// to the request having completed means the request was cancelled by the
586// caller.
587//
588// Both the RequestImpl and its associated Job hold non-owning pointers to each
589// other. Care must be taken to clear the corresponding pointer when
590// cancellation is initiated by the Job (OnJobCancelled) vs by the end user
591// (~RequestImpl).
David Benjamina5677192018-04-23 23:25:42592class HostResolverImpl::RequestImpl
593 : public HostResolver::Request,
594 public base::LinkNode<HostResolverImpl::RequestImpl> {
[email protected]b59ff372009-07-15 22:04:32595 public:
tfarina428341112016-09-22 13:38:20596 RequestImpl(const NetLogWithSource& source_net_log,
maksim.sisov31452af2016-07-27 06:38:10597 const RequestInfo& info,
598 RequestPriority priority,
599 const CompletionCallback& callback,
600 AddressList* addresses,
601 Job* job)
[email protected]ee094b82010-08-24 15:55:51602 : source_net_log_(source_net_log),
[email protected]54e13772009-08-14 03:01:09603 info_(info),
[email protected]5109c1952013-08-20 18:44:10604 priority_(priority),
maksim.sisov31452af2016-07-27 06:38:10605 job_(job),
[email protected]54e13772009-08-14 03:01:09606 callback_(callback),
[email protected]51b9a6b2012-06-25 21:50:29607 addresses_(addresses),
[email protected]5109c1952013-08-20 18:44:10608 request_time_(base::TimeTicks::Now()) {}
[email protected]b59ff372009-07-15 22:04:32609
maksim.sisov31452af2016-07-27 06:38:10610 ~RequestImpl() override;
611
612 void ChangeRequestPriority(RequestPriority priority) override;
613
614 void OnJobCancelled(Job* job) {
615 DCHECK_EQ(job_, job);
wezb9820d42016-06-22 23:41:04616 job_ = nullptr;
617 addresses_ = nullptr;
[email protected]aa22b242011-11-16 18:58:29618 callback_.Reset();
[email protected]b59ff372009-07-15 22:04:32619 }
620
[email protected]0f292de02012-02-01 22:28:20621 // Prepare final AddressList and call completion callback.
maksim.sisov31452af2016-07-27 06:38:10622 void OnJobCompleted(Job* job, int error, const AddressList& addr_list) {
623 DCHECK_EQ(job_, job);
[email protected]895123222012-10-25 15:21:17624 if (error == OK)
625 *addresses_ = EnsurePortOnAddressList(addr_list, info_.port());
maksim.sisov31452af2016-07-27 06:38:10626 job_ = nullptr;
627 addresses_ = nullptr;
628 base::ResetAndReturn(&callback_).Run(error);
[email protected]b59ff372009-07-15 22:04:32629 }
630
[email protected]b59ff372009-07-15 22:04:32631 Job* job() const {
632 return job_;
633 }
634
[email protected]0f292de02012-02-01 22:28:20635 // NetLog for the source, passed in HostResolver::Resolve.
tfarina428341112016-09-22 13:38:20636 const NetLogWithSource& source_net_log() { return source_net_log_; }
[email protected]ee094b82010-08-24 15:55:51637
[email protected]b59ff372009-07-15 22:04:32638 const RequestInfo& info() const {
639 return info_;
640 }
641
[email protected]5109c1952013-08-20 18:44:10642 RequestPriority priority() const { return priority_; }
juliatuttlec53b19a72016-05-05 13:51:31643 void set_priority(RequestPriority priority) { priority_ = priority; }
[email protected]5109c1952013-08-20 18:44:10644
645 base::TimeTicks request_time() const { return request_time_; }
[email protected]51b9a6b2012-06-25 21:50:29646
[email protected]b59ff372009-07-15 22:04:32647 private:
tfarina428341112016-09-22 13:38:20648 const NetLogWithSource source_net_log_;
[email protected]54e13772009-08-14 03:01:09649
[email protected]b59ff372009-07-15 22:04:32650 // The request info that started the request.
[email protected]5109c1952013-08-20 18:44:10651 const RequestInfo info_;
652
juliatuttlec53b19a72016-05-05 13:51:31653 RequestPriority priority_;
[email protected]b59ff372009-07-15 22:04:32654
[email protected]0f292de02012-02-01 22:28:20655 // The resolve job that this request is dependent on.
[email protected]b59ff372009-07-15 22:04:32656 Job* job_;
657
658 // The user's callback to invoke when the request completes.
[email protected]aa22b242011-11-16 18:58:29659 CompletionCallback callback_;
[email protected]b59ff372009-07-15 22:04:32660
661 // The address list to save result into.
662 AddressList* addresses_;
663
[email protected]51b9a6b2012-06-25 21:50:29664 const base::TimeTicks request_time_;
665
maksim.sisov31452af2016-07-27 06:38:10666 DISALLOW_COPY_AND_ASSIGN(RequestImpl);
[email protected]b59ff372009-07-15 22:04:32667};
668
[email protected]1e9bbd22010-10-15 16:42:45669//------------------------------------------------------------------------------
670
Francois Doraya2d01ba2017-09-25 19:17:40671// Calls HostResolverProc in TaskScheduler. Performs retries if necessary.
[email protected]0f292de02012-02-01 22:28:20672//
Miriam Gershenson02592182018-03-22 17:42:37673// In non-test code, the HostResolverProc is always SystemHostResolverProc,
674// which calls a platform API that implements host resolution.
675//
[email protected]0f292de02012-02-01 22:28:20676// Whenever we try to resolve the host, we post a delayed task to check if host
677// resolution (OnLookupComplete) is completed or not. If the original attempt
678// hasn't completed, then we start another attempt for host resolution. We take
679// the results from the first attempt that finishes and ignore the results from
680// all other attempts.
681//
682// TODO(szym): Move to separate source file for testing and mocking.
683//
684class HostResolverImpl::ProcTask
685 : public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> {
[email protected]b59ff372009-07-15 22:04:32686 public:
[email protected]b3601bc22012-02-21 21:23:20687 typedef base::Callback<void(int net_error,
688 const AddressList& addr_list)> Callback;
[email protected]b59ff372009-07-15 22:04:32689
[email protected]0f292de02012-02-01 22:28:20690 ProcTask(const Key& key,
691 const ProcTaskParams& params,
692 const Callback& callback,
Miriam Gershensone42adb22017-10-16 16:19:38693 scoped_refptr<base::TaskRunner> proc_task_runner,
tfarina428341112016-09-22 13:38:20694 const NetLogWithSource& job_net_log)
[email protected]0f292de02012-02-01 22:28:20695 : key_(key),
696 params_(params),
697 callback_(callback),
mmenke91c17162016-06-02 16:03:23698 network_task_runner_(base::ThreadTaskRunnerHandle::Get()),
Miriam Gershensone42adb22017-10-16 16:19:38699 proc_task_runner_(std::move(proc_task_runner)),
[email protected]0f292de02012-02-01 22:28:20700 attempt_number_(0),
701 completed_attempt_number_(0),
702 completed_attempt_error_(ERR_UNEXPECTED),
[email protected]b3601bc22012-02-21 21:23:20703 net_log_(job_net_log) {
[email protected]90499482013-06-01 00:39:50704 if (!params_.resolver_proc.get())
[email protected]0f292de02012-02-01 22:28:20705 params_.resolver_proc = HostResolverProc::GetDefault();
706 // If default is unset, use the system proc.
[email protected]90499482013-06-01 00:39:50707 if (!params_.resolver_proc.get())
[email protected]1ee9afa12013-04-16 14:18:06708 params_.resolver_proc = new SystemHostResolverProc();
[email protected]b59ff372009-07-15 22:04:32709 }
710
[email protected]b59ff372009-07-15 22:04:32711 void Start() {
mmenke91c17162016-06-02 16:03:23712 DCHECK(network_task_runner_->BelongsToCurrentThread());
mikecirone8b85c432016-09-08 19:11:00713 net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK);
[email protected]189163e2011-05-11 01:48:54714 StartLookupAttempt();
715 }
[email protected]252b699b2010-02-05 21:38:06716
[email protected]0f292de02012-02-01 22:28:20717 // Cancels this ProcTask. It will be orphaned. Any outstanding resolve
mmenke91c17162016-06-02 16:03:23718 // attempts running on worker thread will continue running. Only once all the
[email protected]0f292de02012-02-01 22:28:20719 // attempts complete will the final reference to this ProcTask be released.
720 void Cancel() {
mmenke91c17162016-06-02 16:03:23721 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0f292de02012-02-01 22:28:20722
[email protected]0adcb2b2012-08-15 21:30:46723 if (was_canceled() || was_completed())
[email protected]0f292de02012-02-01 22:28:20724 return;
725
[email protected]0f292de02012-02-01 22:28:20726 callback_.Reset();
mikecirone8b85c432016-09-08 19:11:00727 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK);
[email protected]0f292de02012-02-01 22:28:20728 }
729
[email protected]0f292de02012-02-01 22:28:20730 bool was_canceled() const {
mmenke91c17162016-06-02 16:03:23731 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0f292de02012-02-01 22:28:20732 return callback_.is_null();
733 }
734
735 bool was_completed() const {
mmenke91c17162016-06-02 16:03:23736 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0f292de02012-02-01 22:28:20737 return completed_attempt_number_ > 0;
738 }
739
740 private:
[email protected]a9813302012-04-28 09:29:28741 friend class base::RefCountedThreadSafe<ProcTask>;
Chris Watkins68b15032017-12-01 03:07:13742 ~ProcTask() = default;
[email protected]a9813302012-04-28 09:29:28743
[email protected]189163e2011-05-11 01:48:54744 void StartLookupAttempt() {
mmenke91c17162016-06-02 16:03:23745 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]189163e2011-05-11 01:48:54746 base::TimeTicks start_time = base::TimeTicks::Now();
747 ++attempt_number_;
748 // Dispatch the lookup attempt to a worker thread.
Miriam Gershensone42adb22017-10-16 16:19:38749 proc_task_runner_->PostTask(
Francois Doraya2d01ba2017-09-25 19:17:40750 FROM_HERE,
Francois Doraya2d01ba2017-09-25 19:17:40751 base::Bind(&ProcTask::DoLookup, this, start_time, attempt_number_));
[email protected]13024882011-05-18 23:19:16752
mikecirone8b85c432016-09-08 19:11:00753 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_ATTEMPT_STARTED,
tfarina5e24b242015-10-27 13:11:28754 NetLog::IntCallback("attempt_number", attempt_number_));
[email protected]13024882011-05-18 23:19:16755
mmenke91c17162016-06-02 16:03:23756 // If the results aren't received within a given time, RetryIfNotComplete
757 // will start a new attempt if none of the outstanding attempts have
758 // completed yet.
[email protected]0f292de02012-02-01 22:28:20759 if (attempt_number_ <= params_.max_retry_attempts) {
mmenke91c17162016-06-02 16:03:23760 network_task_runner_->PostDelayedTask(
761 FROM_HERE, base::Bind(&ProcTask::RetryIfNotComplete, this),
[email protected]7e560102012-03-08 20:58:42762 params_.unresponsive_delay);
[email protected]06ef6d92011-05-19 04:24:58763 }
[email protected]b59ff372009-07-15 22:04:32764 }
765
Francois Doraya2d01ba2017-09-25 19:17:40766 // WARNING: This code runs in TaskScheduler with CONTINUE_ON_SHUTDOWN. The
767 // shutdown code cannot wait for it to finish, so this code must be very
768 // careful about using other objects (like MessageLoops, Singletons, etc).
769 // During shutdown these objects may no longer exist. Multiple DoLookups()
770 // could be running in parallel, so any state inside of |this| must not
771 // mutate.
[email protected]189163e2011-05-11 01:48:54772 void DoLookup(const base::TimeTicks& start_time,
Avi Drissman13fc8932015-12-20 04:40:46773 const uint32_t attempt_number) {
[email protected]189163e2011-05-11 01:48:54774 AddressList results;
775 int os_error = 0;
[email protected]0f292de02012-02-01 22:28:20776 int error = params_.resolver_proc->Resolve(key_.hostname,
777 key_.address_family,
778 key_.host_resolver_flags,
779 &results,
780 &os_error);
[email protected]b59ff372009-07-15 22:04:32781
mmenke91c17162016-06-02 16:03:23782 network_task_runner_->PostTask(
783 FROM_HERE, base::Bind(&ProcTask::OnLookupComplete, this, results,
784 start_time, attempt_number, error, os_error));
[email protected]189163e2011-05-11 01:48:54785 }
786
mmenke91c17162016-06-02 16:03:23787 // Makes next attempt if DoLookup() has not finished.
[email protected]0f292de02012-02-01 22:28:20788 void RetryIfNotComplete() {
mmenke91c17162016-06-02 16:03:23789 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]189163e2011-05-11 01:48:54790
[email protected]0f292de02012-02-01 22:28:20791 if (was_completed() || was_canceled())
[email protected]189163e2011-05-11 01:48:54792 return;
793
[email protected]0f292de02012-02-01 22:28:20794 params_.unresponsive_delay *= params_.retry_factor;
[email protected]189163e2011-05-11 01:48:54795 StartLookupAttempt();
[email protected]b59ff372009-07-15 22:04:32796 }
797
anujk.sharma7c907f02015-05-29 02:55:44798 // Callback for when DoLookup() completes (runs on task runner thread).
[email protected]189163e2011-05-11 01:48:54799 void OnLookupComplete(const AddressList& results,
800 const base::TimeTicks& start_time,
Avi Drissman13fc8932015-12-20 04:40:46801 const uint32_t attempt_number,
[email protected]189163e2011-05-11 01:48:54802 int error,
803 const int os_error) {
xunjieli0b7f5b62016-12-06 20:43:48804 TRACE_EVENT0(kNetTracingCategory, "ProcTask::OnLookupComplete");
mmenke91c17162016-06-02 16:03:23805 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]49b70b222013-05-07 21:24:23806 // If results are empty, we should return an error.
807 bool empty_list_on_ok = (error == OK && results.empty());
[email protected]49b70b222013-05-07 21:24:23808 if (empty_list_on_ok)
809 error = ERR_NAME_NOT_RESOLVED;
[email protected]189163e2011-05-11 01:48:54810
811 bool was_retry_attempt = attempt_number > 1;
812
[email protected]2d3b7762010-10-09 00:35:47813 // Ideally the following code would be part of host_resolver_proc.cc,
[email protected]b3601bc22012-02-21 21:23:20814 // however it isn't safe to call NetworkChangeNotifier from worker threads.
mmenke91c17162016-06-02 16:03:23815 // So do it here on the IO thread instead.
[email protected]189163e2011-05-11 01:48:54816 if (error != OK && NetworkChangeNotifier::IsOffline())
817 error = ERR_INTERNET_DISCONNECTED;
[email protected]2d3b7762010-10-09 00:35:47818
[email protected]189163e2011-05-11 01:48:54819 RecordAttemptHistograms(start_time, attempt_number, error, os_error);
[email protected]f2d8c4212010-02-02 00:56:35820
[email protected]0f292de02012-02-01 22:28:20821 if (was_canceled())
[email protected]b59ff372009-07-15 22:04:32822 return;
823
mikecironef22f9812016-10-04 03:40:19824 NetLogParametersCallback net_log_callback;
[email protected]0f292de02012-02-01 22:28:20825 if (error != OK) {
[email protected]cd565142012-06-12 16:21:45826 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
827 attempt_number,
828 error,
829 os_error);
[email protected]0f292de02012-02-01 22:28:20830 } else {
tfarina5e24b242015-10-27 13:11:28831 net_log_callback = NetLog::IntCallback("attempt_number", attempt_number);
[email protected]0f292de02012-02-01 22:28:20832 }
mikecirone8b85c432016-09-08 19:11:00833 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_ATTEMPT_FINISHED,
[email protected]cd565142012-06-12 16:21:45834 net_log_callback);
[email protected]0f292de02012-02-01 22:28:20835
836 if (was_completed())
837 return;
838
Miriam Gershenson61604662017-09-28 23:51:11839 RecordTaskHistograms(start_time, error, os_error);
840
[email protected]0f292de02012-02-01 22:28:20841 // Copy the results from the first worker thread that resolves the host.
842 results_ = results;
843 completed_attempt_number_ = attempt_number;
844 completed_attempt_error_ = error;
845
[email protected]e87b8b512011-06-14 22:12:52846 if (was_retry_attempt) {
847 // If retry attempt finishes before 1st attempt, then get stats on how
848 // much time is saved by having spawned an extra attempt.
849 retry_attempt_finished_time_ = base::TimeTicks::Now();
850 }
851
[email protected]189163e2011-05-11 01:48:54852 if (error != OK) {
[email protected]cd565142012-06-12 16:21:45853 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
854 0, error, os_error);
[email protected]ee094b82010-08-24 15:55:51855 } else {
[email protected]cd565142012-06-12 16:21:45856 net_log_callback = results_.CreateNetLogCallback();
[email protected]ee094b82010-08-24 15:55:51857 }
mikecirone8b85c432016-09-08 19:11:00858 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK,
[email protected]cd565142012-06-12 16:21:45859 net_log_callback);
[email protected]ee094b82010-08-24 15:55:51860
[email protected]b3601bc22012-02-21 21:23:20861 callback_.Run(error, results_);
[email protected]b59ff372009-07-15 22:04:32862 }
863
Miriam Gershenson61604662017-09-28 23:51:11864 void RecordTaskHistograms(const base::TimeTicks& start_time,
865 const int error,
866 const int os_error) const {
mmenke91c17162016-06-02 16:03:23867 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]189163e2011-05-11 01:48:54868 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
Miriam Gershenson61604662017-09-28 23:51:11869 if (error == OK)
870 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ProcTask.SuccessTime", duration);
871 else
872 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ProcTask.FailureTime", duration);
[email protected]7e96d792011-06-10 17:08:23873
Miriam Gershenson61604662017-09-28 23:51:11874 UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName,
875 std::abs(os_error),
876 GetAllGetAddrinfoOSErrors());
[email protected]1e9bbd22010-10-15 16:42:45877 }
878
[email protected]189163e2011-05-11 01:48:54879 void RecordAttemptHistograms(const base::TimeTicks& start_time,
Avi Drissman13fc8932015-12-20 04:40:46880 const uint32_t attempt_number,
[email protected]189163e2011-05-11 01:48:54881 const int error,
882 const int os_error) const {
mmenke91c17162016-06-02 16:03:23883 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]189163e2011-05-11 01:48:54884 bool first_attempt_to_complete =
885 completed_attempt_number_ == attempt_number;
[email protected]e87b8b512011-06-14 22:12:52886 bool is_first_attempt = (attempt_number == 1);
[email protected]1e9bbd22010-10-15 16:42:45887
[email protected]189163e2011-05-11 01:48:54888 if (first_attempt_to_complete) {
889 // If this was first attempt to complete, then record the resolution
890 // status of the attempt.
891 if (completed_attempt_error_ == OK) {
892 UMA_HISTOGRAM_ENUMERATION(
893 "DNS.AttemptFirstSuccess", attempt_number, 100);
894 } else {
895 UMA_HISTOGRAM_ENUMERATION(
896 "DNS.AttemptFirstFailure", attempt_number, 100);
897 }
898 }
899
900 if (error == OK)
901 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100);
902 else
903 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100);
904
[email protected]e87b8b512011-06-14 22:12:52905 // If first attempt didn't finish before retry attempt, then calculate stats
906 // on how much time is saved by having spawned an extra attempt.
[email protected]0f292de02012-02-01 22:28:20907 if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21908 UMA_HISTOGRAM_LONG_TIMES_100(
909 "DNS.AttemptTimeSavedByRetry",
910 base::TimeTicks::Now() - retry_attempt_finished_time_);
[email protected]e87b8b512011-06-14 22:12:52911 }
912
[email protected]0f292de02012-02-01 22:28:20913 if (was_canceled() || !first_attempt_to_complete) {
[email protected]189163e2011-05-11 01:48:54914 // Count those attempts which completed after the job was already canceled
915 // OR after the job was already completed by an earlier attempt (so in
916 // effect).
917 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100);
918
[email protected]0f292de02012-02-01 22:28:20919 // Record if job is canceled.
920 if (was_canceled())
[email protected]189163e2011-05-11 01:48:54921 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100);
922 }
923
924 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
925 if (error == OK)
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21926 UMA_HISTOGRAM_LONG_TIMES_100("DNS.AttemptSuccessDuration", duration);
[email protected]189163e2011-05-11 01:48:54927 else
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21928 UMA_HISTOGRAM_LONG_TIMES_100("DNS.AttemptFailDuration", duration);
[email protected]189163e2011-05-11 01:48:54929 }
[email protected]1e9bbd22010-10-15 16:42:45930
anujk.sharma7c907f02015-05-29 02:55:44931 // Set on the task runner thread, read on the worker thread.
[email protected]123ab1e32009-10-21 19:12:57932 Key key_;
[email protected]b59ff372009-07-15 22:04:32933
[email protected]0f292de02012-02-01 22:28:20934 // Holds an owning reference to the HostResolverProc that we are going to use.
[email protected]b59ff372009-07-15 22:04:32935 // This may not be the current resolver procedure by the time we call
936 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
937 // reference ensures that it remains valid until we are done.
[email protected]0f292de02012-02-01 22:28:20938 ProcTaskParams params_;
[email protected]b59ff372009-07-15 22:04:32939
[email protected]0f292de02012-02-01 22:28:20940 // The listener to the results of this ProcTask.
941 Callback callback_;
942
mmenke91c17162016-06-02 16:03:23943 // Used to post events onto the network thread.
944 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
Miriam Gershensone42adb22017-10-16 16:19:38945 // Used to post blocking HostResolverProc tasks.
946 scoped_refptr<base::TaskRunner> proc_task_runner_;
[email protected]189163e2011-05-11 01:48:54947
948 // Keeps track of the number of attempts we have made so far to resolve the
949 // host. Whenever we start an attempt to resolve the host, we increase this
950 // number.
Avi Drissman13fc8932015-12-20 04:40:46951 uint32_t attempt_number_;
[email protected]189163e2011-05-11 01:48:54952
953 // The index of the attempt which finished first (or 0 if the job is still in
954 // progress).
Avi Drissman13fc8932015-12-20 04:40:46955 uint32_t completed_attempt_number_;
[email protected]189163e2011-05-11 01:48:54956
957 // The result (a net error code) from the first attempt to complete.
958 int completed_attempt_error_;
[email protected]252b699b2010-02-05 21:38:06959
[email protected]e87b8b512011-06-14 22:12:52960 // The time when retry attempt was finished.
961 base::TimeTicks retry_attempt_finished_time_;
962
[email protected]b59ff372009-07-15 22:04:32963 AddressList results_;
964
tfarina428341112016-09-22 13:38:20965 NetLogWithSource net_log_;
[email protected]ee094b82010-08-24 15:55:51966
[email protected]0f292de02012-02-01 22:28:20967 DISALLOW_COPY_AND_ASSIGN(ProcTask);
[email protected]b59ff372009-07-15 22:04:32968};
969
970//-----------------------------------------------------------------------------
971
Miriam Gershenson02592182018-03-22 17:42:37972// Resolves the hostname using DnsTransaction, which is a full implementation of
973// a DNS stub resolver. One DnsTransaction is created for each resolution
974// needed, which for AF_UNSPEC resolutions includes both A and AAAA. The
975// transactions are scheduled separately and started separately.
976//
[email protected]b3601bc22012-02-21 21:23:20977// TODO(szym): This could be moved to separate source file as well.
[email protected]0adcb2b2012-08-15 21:30:46978class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> {
[email protected]b3601bc22012-02-21 21:23:20979 public:
[email protected]daae1322013-09-05 18:26:50980 class Delegate {
981 public:
982 virtual void OnDnsTaskComplete(base::TimeTicks start_time,
983 int net_error,
984 const AddressList& addr_list,
985 base::TimeDelta ttl) = 0;
986
987 // Called when the first of two jobs succeeds. If the first completed
988 // transaction fails, this is not called. Also not called when the DnsTask
989 // only needs to run one transaction.
990 virtual void OnFirstDnsTransactionComplete() = 0;
991
Brad Lassey786929ad2018-02-21 20:54:27992 virtual URLRequestContext* url_request_context() = 0;
993 virtual RequestPriority priority() const = 0;
994
[email protected]daae1322013-09-05 18:26:50995 protected:
Chris Watkins68b15032017-12-01 03:07:13996 Delegate() = default;
997 virtual ~Delegate() = default;
[email protected]daae1322013-09-05 18:26:50998 };
[email protected]b3601bc22012-02-21 21:23:20999
[email protected]0adcb2b2012-08-15 21:30:461000 DnsTask(DnsClient* client,
[email protected]b3601bc22012-02-21 21:23:201001 const Key& key,
[email protected]daae1322013-09-05 18:26:501002 Delegate* delegate,
tfarina428341112016-09-22 13:38:201003 const NetLogWithSource& job_net_log)
[email protected]0adcb2b2012-08-15 21:30:461004 : client_(client),
[email protected]daae1322013-09-05 18:26:501005 key_(key),
1006 delegate_(delegate),
1007 net_log_(job_net_log),
1008 num_completed_transactions_(0),
1009 task_start_time_(base::TimeTicks::Now()) {
[email protected]0adcb2b2012-08-15 21:30:461010 DCHECK(client);
[email protected]daae1322013-09-05 18:26:501011 DCHECK(delegate_);
[email protected]1affed62013-08-21 03:24:501012 }
1013
[email protected]daae1322013-09-05 18:26:501014 bool needs_two_transactions() const {
1015 return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED;
1016 }
1017
1018 bool needs_another_transaction() const {
1019 return needs_two_transactions() && !transaction_aaaa_;
1020 }
1021
1022 void StartFirstTransaction() {
1023 DCHECK_EQ(0u, num_completed_transactions_);
mikecirone8b85c432016-09-08 19:11:001024 net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK);
[email protected]daae1322013-09-05 18:26:501025 if (key_.address_family == ADDRESS_FAMILY_IPV6) {
1026 StartAAAA();
1027 } else {
1028 StartA();
1029 }
1030 }
1031
1032 void StartSecondTransaction() {
1033 DCHECK(needs_two_transactions());
1034 StartAAAA();
[email protected]70c04ab2013-08-22 16:05:121035 }
1036
1037 private:
[email protected]daae1322013-09-05 18:26:501038 void StartA() {
1039 DCHECK(!transaction_a_);
1040 DCHECK_NE(ADDRESS_FAMILY_IPV6, key_.address_family);
1041 transaction_a_ = CreateTransaction(ADDRESS_FAMILY_IPV4);
1042 transaction_a_->Start();
1043 }
1044
1045 void StartAAAA() {
1046 DCHECK(!transaction_aaaa_);
1047 DCHECK_NE(ADDRESS_FAMILY_IPV4, key_.address_family);
1048 transaction_aaaa_ = CreateTransaction(ADDRESS_FAMILY_IPV6);
1049 transaction_aaaa_->Start();
1050 }
1051
danakj22f90e72016-04-16 01:55:401052 std::unique_ptr<DnsTransaction> CreateTransaction(AddressFamily family) {
[email protected]daae1322013-09-05 18:26:501053 DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family);
Brad Lassey786929ad2018-02-21 20:54:271054 std::unique_ptr<DnsTransaction> trans =
1055 client_->GetTransactionFactory()->CreateTransaction(
1056 key_.hostname,
1057 family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA
1058 : dns_protocol::kTypeA,
Brad Lasseydba0a84b2018-02-23 22:18:531059 base::BindOnce(&DnsTask::OnTransactionComplete,
1060 base::Unretained(this), base::TimeTicks::Now()),
Brad Lassey786929ad2018-02-21 20:54:271061 net_log_);
1062 trans->SetRequestContext(delegate_->url_request_context());
1063 trans->SetRequestPriority(delegate_->priority());
1064 return trans;
[email protected]daae1322013-09-05 18:26:501065 }
1066
1067 void OnTransactionComplete(const base::TimeTicks& start_time,
[email protected]1def74c2012-03-22 20:07:001068 DnsTransaction* transaction,
[email protected]b3601bc22012-02-21 21:23:201069 int net_error,
1070 const DnsResponse* response) {
[email protected]add76532012-03-30 14:47:471071 DCHECK(transaction);
[email protected]02cd6982013-01-10 20:12:511072 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
Miriam Gershenson66024d72017-12-05 04:30:321073 if (net_error != OK) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211074 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionFailure", duration);
[email protected]0adcb2b2012-08-15 21:30:461075 OnFailure(net_error, DnsResponse::DNS_PARSE_OK);
1076 return;
[email protected]6c411902012-08-14 22:36:361077 }
[email protected]0adcb2b2012-08-15 21:30:461078
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211079 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionSuccess", duration);
[email protected]02cd6982013-01-10 20:12:511080 switch (transaction->GetType()) {
1081 case dns_protocol::kTypeA:
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211082 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionSuccess_A", duration);
[email protected]02cd6982013-01-10 20:12:511083 break;
1084 case dns_protocol::kTypeAAAA:
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211085 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionSuccess_AAAA",
1086 duration);
[email protected]02cd6982013-01-10 20:12:511087 break;
1088 }
[email protected]daae1322013-09-05 18:26:501089
[email protected]0adcb2b2012-08-15 21:30:461090 AddressList addr_list;
1091 base::TimeDelta ttl;
1092 DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl);
1093 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
1094 result,
1095 DnsResponse::DNS_PARSE_RESULT_MAX);
1096 if (result != DnsResponse::DNS_PARSE_OK) {
1097 // Fail even if the other query succeeds.
1098 OnFailure(ERR_DNS_MALFORMED_RESPONSE, result);
1099 return;
1100 }
1101
[email protected]daae1322013-09-05 18:26:501102 ++num_completed_transactions_;
1103 if (num_completed_transactions_ == 1) {
1104 ttl_ = ttl;
[email protected]0adcb2b2012-08-15 21:30:461105 } else {
[email protected]daae1322013-09-05 18:26:501106 ttl_ = std::min(ttl_, ttl);
[email protected]0adcb2b2012-08-15 21:30:461107 }
1108
[email protected]daae1322013-09-05 18:26:501109 if (transaction->GetType() == dns_protocol::kTypeA) {
1110 DCHECK_EQ(transaction_a_.get(), transaction);
1111 // Place IPv4 addresses after IPv6.
1112 addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end());
1113 } else {
1114 DCHECK_EQ(transaction_aaaa_.get(), transaction);
1115 // Place IPv6 addresses before IPv4.
1116 addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end());
1117 }
1118
1119 if (needs_two_transactions() && num_completed_transactions_ == 1) {
1120 // No need to repeat the suffix search.
1121 key_.hostname = transaction->GetHostname();
1122 delegate_->OnFirstDnsTransactionComplete();
1123 return;
1124 }
1125
1126 if (addr_list_.empty()) {
[email protected]70c04ab2013-08-22 16:05:121127 // TODO(szym): Don't fallback to ProcTask in this case.
1128 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
[email protected]0adcb2b2012-08-15 21:30:461129 return;
1130 }
1131
[email protected]daae1322013-09-05 18:26:501132 // If there are multiple addresses, and at least one is IPv6, need to sort
1133 // them. Note that IPv6 addresses are always put before IPv4 ones, so it's
1134 // sufficient to just check the family of the first address.
1135 if (addr_list_.size() > 1 &&
1136 addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) {
1137 // Sort addresses if needed. Sort could complete synchronously.
[email protected]0adcb2b2012-08-15 21:30:461138 client_->GetAddressSorter()->Sort(
[email protected]daae1322013-09-05 18:26:501139 addr_list_,
[email protected]4589a3a2012-09-20 20:57:071140 base::Bind(&DnsTask::OnSortComplete,
1141 AsWeakPtr(),
[email protected]daae1322013-09-05 18:26:501142 base::TimeTicks::Now()));
[email protected]0adcb2b2012-08-15 21:30:461143 } else {
[email protected]daae1322013-09-05 18:26:501144 OnSuccess(addr_list_);
[email protected]0adcb2b2012-08-15 21:30:461145 }
1146 }
1147
1148 void OnSortComplete(base::TimeTicks start_time,
[email protected]0adcb2b2012-08-15 21:30:461149 bool success,
1150 const AddressList& addr_list) {
1151 if (!success) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211152 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.SortFailure",
1153 base::TimeTicks::Now() - start_time);
[email protected]0adcb2b2012-08-15 21:30:461154 OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK);
1155 return;
1156 }
1157
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211158 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.SortSuccess",
1159 base::TimeTicks::Now() - start_time);
[email protected]0adcb2b2012-08-15 21:30:461160
1161 // AddressSorter prunes unusable destinations.
1162 if (addr_list.empty()) {
1163 LOG(WARNING) << "Address list empty after RFC3484 sort";
1164 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
1165 return;
1166 }
1167
[email protected]daae1322013-09-05 18:26:501168 OnSuccess(addr_list);
[email protected]0adcb2b2012-08-15 21:30:461169 }
1170
1171 void OnFailure(int net_error, DnsResponse::Result result) {
1172 DCHECK_NE(OK, net_error);
[email protected]cd565142012-06-12 16:21:451173 net_log_.EndEvent(
mikecirone8b85c432016-09-08 19:11:001174 NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK,
[email protected]cd565142012-06-12 16:21:451175 base::Bind(&NetLogDnsTaskFailedCallback, net_error, result));
Miriam Gershenson66024d72017-12-05 04:30:321176 delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(),
1177 base::TimeDelta());
[email protected]b3601bc22012-02-21 21:23:201178 }
1179
[email protected]daae1322013-09-05 18:26:501180 void OnSuccess(const AddressList& addr_list) {
mikecirone8b85c432016-09-08 19:11:001181 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK,
[email protected]0adcb2b2012-08-15 21:30:461182 addr_list.CreateNetLogCallback());
[email protected]daae1322013-09-05 18:26:501183 delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_);
[email protected]0adcb2b2012-08-15 21:30:461184 }
1185
1186 DnsClient* client_;
[email protected]daae1322013-09-05 18:26:501187 Key key_;
1188
[email protected]b3601bc22012-02-21 21:23:201189 // The listener to the results of this DnsTask.
[email protected]daae1322013-09-05 18:26:501190 Delegate* delegate_;
tfarina428341112016-09-22 13:38:201191 const NetLogWithSource net_log_;
[email protected]b3601bc22012-02-21 21:23:201192
danakj22f90e72016-04-16 01:55:401193 std::unique_ptr<DnsTransaction> transaction_a_;
1194 std::unique_ptr<DnsTransaction> transaction_aaaa_;
[email protected]0adcb2b2012-08-15 21:30:461195
[email protected]daae1322013-09-05 18:26:501196 unsigned num_completed_transactions_;
1197
1198 // These are updated as each transaction completes.
1199 base::TimeDelta ttl_;
1200 // IPv6 addresses must appear first in the list.
1201 AddressList addr_list_;
1202
1203 base::TimeTicks task_start_time_;
[email protected]0adcb2b2012-08-15 21:30:461204
1205 DISALLOW_COPY_AND_ASSIGN(DnsTask);
[email protected]b3601bc22012-02-21 21:23:201206};
1207
1208//-----------------------------------------------------------------------------
1209
[email protected]0f292de02012-02-01 22:28:201210// Aggregates all Requests for the same Key. Dispatched via PriorityDispatch.
[email protected]daae1322013-09-05 18:26:501211class HostResolverImpl::Job : public PrioritizedDispatcher::Job,
1212 public HostResolverImpl::DnsTask::Delegate {
[email protected]68ad3ee2010-01-30 03:45:391213 public:
[email protected]0f292de02012-02-01 22:28:201214 // Creates new job for |key| where |request_net_log| is bound to the
[email protected]16ee26d2012-03-08 03:34:351215 // request that spawned it.
[email protected]12faa4c2012-11-06 04:44:181216 Job(const base::WeakPtr<HostResolverImpl>& resolver,
[email protected]0f292de02012-02-01 22:28:201217 const Key& key,
[email protected]8c98d002012-07-18 19:02:271218 RequestPriority priority,
Miriam Gershensone42adb22017-10-16 16:19:381219 scoped_refptr<base::TaskRunner> proc_task_runner,
tfarina428341112016-09-22 13:38:201220 const NetLogWithSource& source_net_log)
[email protected]12faa4c2012-11-06 04:44:181221 : resolver_(resolver),
[email protected]0f292de02012-02-01 22:28:201222 key_(key),
[email protected]8c98d002012-07-18 19:02:271223 priority_tracker_(priority),
Miriam Gershensone42adb22017-10-16 16:19:381224 proc_task_runner_(std::move(proc_task_runner)),
[email protected]0f292de02012-02-01 22:28:201225 had_non_speculative_request_(false),
[email protected]51b9a6b2012-06-25 21:50:291226 had_dns_config_(false),
[email protected]daae1322013-09-05 18:26:501227 num_occupied_job_slots_(0),
[email protected]1d932852012-06-19 19:40:331228 dns_task_error_(OK),
[email protected]51b9a6b2012-06-25 21:50:291229 creation_time_(base::TimeTicks::Now()),
1230 priority_change_time_(creation_time_),
tfarina428341112016-09-22 13:38:201231 net_log_(
1232 NetLogWithSource::Make(source_net_log.net_log(),
mikecirone8b85c432016-09-08 19:11:001233 NetLogSourceType::HOST_RESOLVER_IMPL_JOB)) {
1234 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_CREATE_JOB);
[email protected]0f292de02012-02-01 22:28:201235
mikecirone8b85c432016-09-08 19:11:001236 net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
1237 base::Bind(&NetLogJobCreationCallback,
1238 source_net_log.source(), &key_.hostname));
[email protected]68ad3ee2010-01-30 03:45:391239 }
1240
dchengb03027d2014-10-21 12:00:201241 ~Job() override {
[email protected]b3601bc22012-02-21 21:23:201242 if (is_running()) {
1243 // |resolver_| was destroyed with this Job still in flight.
1244 // Clean-up, record in the log, but don't run any callbacks.
1245 if (is_proc_running()) {
[email protected]0f292de02012-02-01 22:28:201246 proc_task_->Cancel();
wezb9820d42016-06-22 23:41:041247 proc_task_ = nullptr;
[email protected]0f292de02012-02-01 22:28:201248 }
[email protected]16ee26d2012-03-08 03:34:351249 // Clean up now for nice NetLog.
[email protected]daae1322013-09-05 18:26:501250 KillDnsTask();
mikecirone8b85c432016-09-08 19:11:001251 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
[email protected]b3601bc22012-02-21 21:23:201252 ERR_ABORTED);
1253 } else if (is_queued()) {
[email protected]57a48d32012-03-03 00:04:551254 // |resolver_| was destroyed without running this Job.
[email protected]16ee26d2012-03-08 03:34:351255 // TODO(szym): is there any benefit in having this distinction?
mikecirone8b85c432016-09-08 19:11:001256 net_log_.AddEvent(NetLogEventType::CANCELLED);
1257 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB);
[email protected]68ad3ee2010-01-30 03:45:391258 }
[email protected]b3601bc22012-02-21 21:23:201259 // else CompleteRequests logged EndEvent.
David Benjamina5677192018-04-23 23:25:421260 while (!requests_.empty()) {
maksim.sisov31452af2016-07-27 06:38:101261 // Log any remaining Requests as cancelled.
David Benjamina5677192018-04-23 23:25:421262 RequestImpl* req = requests_.head()->value();
1263 req->RemoveFromList();
1264 DCHECK_EQ(this, req->job());
1265 LogCancelRequest(req->source_net_log(), req->info());
1266 req->OnJobCancelled(this);
[email protected]b3601bc22012-02-21 21:23:201267 }
[email protected]68ad3ee2010-01-30 03:45:391268 }
1269
[email protected]daae1322013-09-05 18:26:501270 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1271 // of the queue.
1272 void Schedule(bool at_head) {
1273 DCHECK(!is_queued());
1274 PrioritizedDispatcher::Handle handle;
1275 if (!at_head) {
[email protected]106ccd2c2014-06-17 09:21:001276 handle = resolver_->dispatcher_->Add(this, priority());
[email protected]daae1322013-09-05 18:26:501277 } else {
[email protected]106ccd2c2014-06-17 09:21:001278 handle = resolver_->dispatcher_->AddAtHead(this, priority());
[email protected]daae1322013-09-05 18:26:501279 }
1280 // The dispatcher could have started |this| in the above call to Add, which
1281 // could have called Schedule again. In that case |handle| will be null,
1282 // but |handle_| may have been set by the other nested call to Schedule.
1283 if (!handle.is_null()) {
1284 DCHECK(handle_.is_null());
1285 handle_ = handle;
1286 }
[email protected]16ee26d2012-03-08 03:34:351287 }
1288
maksim.sisov31452af2016-07-27 06:38:101289 void AddRequest(RequestImpl* request) {
1290 DCHECK_EQ(key_.hostname, request->info().hostname());
[email protected]0f292de02012-02-01 22:28:201291
maksim.sisov31452af2016-07-27 06:38:101292 priority_tracker_.Add(request->priority());
[email protected]0f292de02012-02-01 22:28:201293
maksim.sisov31452af2016-07-27 06:38:101294 request->source_net_log().AddEvent(
mikecirone8b85c432016-09-08 19:11:001295 NetLogEventType::HOST_RESOLVER_IMPL_JOB_ATTACH,
[email protected]cd565142012-06-12 16:21:451296 net_log_.source().ToEventParametersCallback());
[email protected]0f292de02012-02-01 22:28:201297
1298 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001299 NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH,
maksim.sisov31452af2016-07-27 06:38:101300 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(),
[email protected]cd565142012-06-12 16:21:451301 priority()));
[email protected]0f292de02012-02-01 22:28:201302
Miriam Gershenson61604662017-09-28 23:51:111303 if (!request->info().is_speculative())
[email protected]0f292de02012-02-01 22:28:201304 had_non_speculative_request_ = true;
[email protected]b3601bc22012-02-21 21:23:201305
David Benjamina5677192018-04-23 23:25:421306 requests_.Append(request);
[email protected]b3601bc22012-02-21 21:23:201307
[email protected]51b9a6b2012-06-25 21:50:291308 UpdatePriority();
[email protected]68ad3ee2010-01-30 03:45:391309 }
1310
maksim.sisov31452af2016-07-27 06:38:101311 void ChangeRequestPriority(RequestImpl* req, RequestPriority priority) {
juliatuttlec53b19a72016-05-05 13:51:311312 DCHECK_EQ(key_.hostname, req->info().hostname());
juliatuttlec53b19a72016-05-05 13:51:311313
1314 priority_tracker_.Remove(req->priority());
1315 req->set_priority(priority);
1316 priority_tracker_.Add(req->priority());
1317 UpdatePriority();
1318 }
1319
maksim.sisov31452af2016-07-27 06:38:101320 // Detach cancelled request. If it was the last active Request, also finishes
1321 // this Job.
1322 void CancelRequest(RequestImpl* request) {
1323 DCHECK_EQ(key_.hostname, request->info().hostname());
1324 DCHECK(!requests_.empty());
[email protected]16ee26d2012-03-08 03:34:351325
maksim.sisov31452af2016-07-27 06:38:101326 LogCancelRequest(request->source_net_log(), request->info());
[email protected]16ee26d2012-03-08 03:34:351327
maksim.sisov31452af2016-07-27 06:38:101328 priority_tracker_.Remove(request->priority());
1329 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001330 NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH,
maksim.sisov31452af2016-07-27 06:38:101331 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(),
1332 priority()));
[email protected]b3601bc22012-02-21 21:23:201333
[email protected]16ee26d2012-03-08 03:34:351334 if (num_active_requests() > 0) {
[email protected]51b9a6b2012-06-25 21:50:291335 UpdatePriority();
David Benjamina5677192018-04-23 23:25:421336 request->RemoveFromList();
[email protected]16ee26d2012-03-08 03:34:351337 } else {
1338 // If we were called from a Request's callback within CompleteRequests,
1339 // that Request could not have been cancelled, so num_active_requests()
1340 // could not be 0. Therefore, we are not in CompleteRequests().
[email protected]1339a2a22012-10-17 08:39:431341 CompleteRequestsWithError(OK /* cancelled */);
[email protected]b3601bc22012-02-21 21:23:201342 }
[email protected]68ad3ee2010-01-30 03:45:391343 }
1344
[email protected]7af985a2012-12-14 22:40:421345 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1346 // the job. This currently assumes the abort is due to a network change.
olli.raulaa21e9eb72015-12-17 08:18:111347 // TODO This should not delete |this|.
[email protected]0f292de02012-02-01 22:28:201348 void Abort() {
[email protected]0f292de02012-02-01 22:28:201349 DCHECK(is_running());
[email protected]7af985a2012-12-14 22:40:421350 CompleteRequestsWithError(ERR_NETWORK_CHANGED);
[email protected]b3601bc22012-02-21 21:23:201351 }
1352
[email protected]f0f602bd2012-11-15 18:01:021353 // If DnsTask present, abort it and fall back to ProcTask.
1354 void AbortDnsTask() {
1355 if (dns_task_) {
[email protected]daae1322013-09-05 18:26:501356 KillDnsTask();
[email protected]f0f602bd2012-11-15 18:01:021357 dns_task_error_ = OK;
1358 StartProcTask();
1359 }
1360 }
1361
[email protected]16ee26d2012-03-08 03:34:351362 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1363 // Completes all requests and destroys the job.
1364 void OnEvicted() {
1365 DCHECK(!is_running());
1366 DCHECK(is_queued());
1367 handle_.Reset();
1368
mikecirone8b85c432016-09-08 19:11:001369 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_EVICTED);
[email protected]16ee26d2012-03-08 03:34:351370
1371 // This signals to CompleteRequests that this job never ran.
[email protected]1339a2a22012-10-17 08:39:431372 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
[email protected]16ee26d2012-03-08 03:34:351373 }
1374
[email protected]78eac2a2012-03-14 19:09:271375 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1376 // this Job was destroyed.
1377 bool ServeFromHosts() {
1378 DCHECK_GT(num_active_requests(), 0u);
1379 AddressList addr_list;
David Benjamina5677192018-04-23 23:25:421380 if (resolver_->ServeFromHosts(key(), requests_.head()->value()->info(),
[email protected]78eac2a2012-03-14 19:09:271381 &addr_list)) {
1382 // This will destroy the Job.
Rob Percival94f21ad2017-11-14 10:20:241383 CompleteRequests(
1384 MakeCacheEntry(OK, addr_list, HostCache::Entry::SOURCE_HOSTS),
1385 base::TimeDelta());
[email protected]78eac2a2012-03-14 19:09:271386 return true;
1387 }
1388 return false;
1389 }
1390
tanay.c6ddd5f6f2015-08-25 06:34:571391 const Key& key() const { return key_; }
[email protected]b4481b222012-03-16 17:13:111392
1393 bool is_queued() const {
1394 return !handle_.is_null();
1395 }
1396
1397 bool is_running() const {
1398 return is_dns_running() || is_proc_running();
1399 }
1400
[email protected]16ee26d2012-03-08 03:34:351401 private:
[email protected]daae1322013-09-05 18:26:501402 void KillDnsTask() {
1403 if (dns_task_) {
1404 ReduceToOneJobSlot();
1405 dns_task_.reset();
1406 }
1407 }
1408
1409 // Reduce the number of job slots occupied and queued in the dispatcher
1410 // to one. If the second Job slot is queued in the dispatcher, cancels the
1411 // queued job. Otherwise, the second Job has been started by the
1412 // PrioritizedDispatcher, so signals it is complete.
1413 void ReduceToOneJobSlot() {
1414 DCHECK_GE(num_occupied_job_slots_, 1u);
1415 if (is_queued()) {
[email protected]106ccd2c2014-06-17 09:21:001416 resolver_->dispatcher_->Cancel(handle_);
[email protected]daae1322013-09-05 18:26:501417 handle_.Reset();
1418 } else if (num_occupied_job_slots_ > 1) {
[email protected]106ccd2c2014-06-17 09:21:001419 resolver_->dispatcher_->OnJobFinished();
[email protected]daae1322013-09-05 18:26:501420 --num_occupied_job_slots_;
1421 }
1422 DCHECK_EQ(1u, num_occupied_job_slots_);
1423 }
1424
eroman0cd87b62016-12-14 19:13:451425 // MakeCacheEntry() and MakeCacheEntryWithTTL() are helpers to build a
1426 // HostCache::Entry(). The address list is omited from the cache entry
1427 // for errors.
1428 HostCache::Entry MakeCacheEntry(int net_error,
Rob Percival94f21ad2017-11-14 10:20:241429 const AddressList& addr_list,
1430 HostCache::Entry::Source source) const {
eroman0cd87b62016-12-14 19:13:451431 return HostCache::Entry(
1432 net_error,
Rob Percival94f21ad2017-11-14 10:20:241433 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(),
1434 source);
eroman0cd87b62016-12-14 19:13:451435 }
1436
1437 HostCache::Entry MakeCacheEntryWithTTL(int net_error,
1438 const AddressList& addr_list,
Rob Percival94f21ad2017-11-14 10:20:241439 HostCache::Entry::Source source,
eroman0cd87b62016-12-14 19:13:451440 base::TimeDelta ttl) const {
1441 return HostCache::Entry(
1442 net_error,
1443 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(),
Rob Percival94f21ad2017-11-14 10:20:241444 source, ttl);
eroman0cd87b62016-12-14 19:13:451445 }
1446
juliatuttlec53b19a72016-05-05 13:51:311447 AddressList MakeAddressListForRequest(const AddressList& list) const {
1448 if (requests_.empty())
1449 return list;
David Benjamina5677192018-04-23 23:25:421450 return AddressList::CopyWithPort(list,
1451 requests_.head()->value()->info().port());
juliatuttlec53b19a72016-05-05 13:51:311452 }
1453
[email protected]51b9a6b2012-06-25 21:50:291454 void UpdatePriority() {
1455 if (is_queued()) {
1456 if (priority() != static_cast<RequestPriority>(handle_.priority()))
1457 priority_change_time_ = base::TimeTicks::Now();
[email protected]106ccd2c2014-06-17 09:21:001458 handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
[email protected]51b9a6b2012-06-25 21:50:291459 }
1460 }
1461
[email protected]16ee26d2012-03-08 03:34:351462 // PriorityDispatch::Job:
dchengb03027d2014-10-21 12:00:201463 void Start() override {
[email protected]daae1322013-09-05 18:26:501464 DCHECK_LE(num_occupied_job_slots_, 1u);
1465
[email protected]70c04ab2013-08-22 16:05:121466 handle_.Reset();
[email protected]daae1322013-09-05 18:26:501467 ++num_occupied_job_slots_;
1468
1469 if (num_occupied_job_slots_ == 2) {
1470 StartSecondDnsTransaction();
1471 return;
1472 }
1473
1474 DCHECK(!is_running());
[email protected]0f292de02012-02-01 22:28:201475
mikecirone8b85c432016-09-08 19:11:001476 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_STARTED);
[email protected]0f292de02012-02-01 22:28:201477
[email protected]51b9a6b2012-06-25 21:50:291478 had_dns_config_ = resolver_->HaveDnsConfig();
1479
Miriam Gershenson61604662017-09-28 23:51:111480 start_time_ = base::TimeTicks::Now();
1481 base::TimeDelta queue_time = start_time_ - creation_time_;
1482 base::TimeDelta queue_time_after_change =
1483 start_time_ - priority_change_time_;
[email protected]51b9a6b2012-06-25 21:50:291484
Miriam Gershenson90d05e02017-09-28 19:29:281485 DNS_HISTOGRAM_BY_PRIORITY("Net.DNS.JobQueueTime", priority(), queue_time);
1486 DNS_HISTOGRAM_BY_PRIORITY("Net.DNS.JobQueueTimeAfterChange", priority(),
1487 queue_time_after_change);
[email protected]51b9a6b2012-06-25 21:50:291488
[email protected]443714fad2013-09-19 04:52:011489 bool system_only =
1490 (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0;
1491
[email protected]1d932852012-06-19 19:40:331492 // Caution: Job::Start must not complete synchronously.
[email protected]443714fad2013-09-19 04:52:011493 if (!system_only && had_dns_config_ &&
1494 !ResemblesMulticastDNSName(key_.hostname)) {
[email protected]b3601bc22012-02-21 21:23:201495 StartDnsTask();
1496 } else {
1497 StartProcTask();
1498 }
1499 }
1500
[email protected]b3601bc22012-02-21 21:23:201501 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
Francois Doraya2d01ba2017-09-25 19:17:401502 // the limits on |dispatcher_|. But in order to keep the number of
1503 // TaskScheduler threads low, we will need to use an "inner"
1504 // PrioritizedDispatcher with tighter limits.
[email protected]b3601bc22012-02-21 21:23:201505 void StartProcTask() {
[email protected]16ee26d2012-03-08 03:34:351506 DCHECK(!is_dns_running());
mmenke91c17162016-06-02 16:03:231507 proc_task_ =
1508 new ProcTask(key_, resolver_->proc_params_,
1509 base::Bind(&Job::OnProcTaskComplete,
1510 base::Unretained(this), base::TimeTicks::Now()),
Miriam Gershensone42adb22017-10-16 16:19:381511 proc_task_runner_, net_log_);
[email protected]0f292de02012-02-01 22:28:201512
[email protected]0f292de02012-02-01 22:28:201513 // Start() could be called from within Resolve(), hence it must NOT directly
1514 // call OnProcTaskComplete, for example, on synchronous failure.
1515 proc_task_->Start();
[email protected]68ad3ee2010-01-30 03:45:391516 }
1517
[email protected]0f292de02012-02-01 22:28:201518 // Called by ProcTask when it completes.
[email protected]e3bd4822012-10-23 18:01:371519 void OnProcTaskComplete(base::TimeTicks start_time,
1520 int net_error,
1521 const AddressList& addr_list) {
[email protected]b3601bc22012-02-21 21:23:201522 DCHECK(is_proc_running());
[email protected]68ad3ee2010-01-30 03:45:391523
[email protected]1d932852012-06-19 19:40:331524 if (dns_task_error_ != OK) {
[email protected]e3bd4822012-10-23 18:01:371525 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
[email protected]1def74c2012-03-22 20:07:001526 if (net_error == OK) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211527 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.FallbackSuccess", duration);
[email protected]1d932852012-06-19 19:40:331528 if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) &&
1529 ResemblesNetBIOSName(key_.hostname)) {
1530 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS);
1531 } else {
1532 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS);
1533 }
Ilya Sherman0eb39802017-12-08 20:58:181534 base::UmaHistogramSparse("Net.DNS.DnsTask.Errors",
1535 std::abs(dns_task_error_));
[email protected]1ffdda82012-12-12 23:04:221536 resolver_->OnDnsTaskResolve(dns_task_error_);
[email protected]1def74c2012-03-22 20:07:001537 } else {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211538 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.FallbackFail", duration);
[email protected]1def74c2012-03-22 20:07:001539 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
1540 }
1541 }
1542
eroman1efc237c2016-12-14 00:00:451543 if (ContainsIcannNameCollisionIp(addr_list))
1544 net_error = ERR_ICANN_NAME_COLLISION;
1545
[email protected]1339a2a22012-10-17 08:39:431546 base::TimeDelta ttl =
1547 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds);
[email protected]b3601bc22012-02-21 21:23:201548 if (net_error == OK)
1549 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds);
[email protected]68ad3ee2010-01-30 03:45:391550
Rob Percival94f21ad2017-11-14 10:20:241551 // Source unknown because the system resolver could have gotten it from a
1552 // hosts file, its own cache, a DNS lookup or somewhere else.
[email protected]895123222012-10-25 15:21:171553 // Don't store the |ttl| in cache since it's not obtained from the server.
Rob Percival94f21ad2017-11-14 10:20:241554 CompleteRequests(
1555 MakeCacheEntry(net_error, addr_list, HostCache::Entry::SOURCE_UNKNOWN),
1556 ttl);
[email protected]b3601bc22012-02-21 21:23:201557 }
1558
1559 void StartDnsTask() {
[email protected]78eac2a2012-03-14 19:09:271560 DCHECK(resolver_->HaveDnsConfig());
[email protected]daae1322013-09-05 18:26:501561 dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this,
1562 net_log_));
[email protected]b3601bc22012-02-21 21:23:201563
[email protected]daae1322013-09-05 18:26:501564 dns_task_->StartFirstTransaction();
1565 // Schedule a second transaction, if needed.
1566 if (dns_task_->needs_two_transactions())
1567 Schedule(true);
1568 }
1569
1570 void StartSecondDnsTransaction() {
1571 DCHECK(dns_task_->needs_two_transactions());
1572 dns_task_->StartSecondTransaction();
[email protected]16c2bd72013-06-28 01:19:221573 }
1574
1575 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
1576 // deleted before this callback. In this case dns_task is deleted as well,
1577 // so we use it as indicator whether Job is still valid.
1578 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
1579 base::TimeDelta duration,
1580 int net_error) {
Miriam Gershenson61604662017-09-28 23:51:111581 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.FailureTime", duration);
[email protected]16c2bd72013-06-28 01:19:221582
wezb9820d42016-06-22 23:41:041583 if (!dns_task)
[email protected]16c2bd72013-06-28 01:19:221584 return;
1585
Miriam Gershenson19faef812018-02-07 23:56:441586 if (duration < base::TimeDelta::FromMilliseconds(10)) {
1587 base::UmaHistogramSparse("Net.DNS.DnsTask.ErrorBeforeFallback.Fast",
1588 std::abs(net_error));
1589 } else {
1590 base::UmaHistogramSparse("Net.DNS.DnsTask.ErrorBeforeFallback.Slow",
1591 std::abs(net_error));
1592 }
[email protected]16c2bd72013-06-28 01:19:221593 dns_task_error_ = net_error;
1594
1595 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so.
1596 // http://crbug.com/117655
1597
1598 // TODO(szym): Some net errors indicate lack of connectivity. Starting
1599 // ProcTask in that case is a waste of time.
1600 if (resolver_->fallback_to_proctask_) {
[email protected]daae1322013-09-05 18:26:501601 KillDnsTask();
[email protected]16c2bd72013-06-28 01:19:221602 StartProcTask();
1603 } else {
1604 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
Miriam Gershenson66024d72017-12-05 04:30:321605 CompleteRequestsWithError(net_error);
[email protected]b3601bc22012-02-21 21:23:201606 }
1607 }
1608
[email protected]daae1322013-09-05 18:26:501609
1610 // HostResolverImpl::DnsTask::Delegate implementation:
1611
dchengb03027d2014-10-21 12:00:201612 void OnDnsTaskComplete(base::TimeTicks start_time,
1613 int net_error,
1614 const AddressList& addr_list,
1615 base::TimeDelta ttl) override {
[email protected]b3601bc22012-02-21 21:23:201616 DCHECK(is_dns_running());
[email protected]b3601bc22012-02-21 21:23:201617
[email protected]e3bd4822012-10-23 18:01:371618 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
[email protected]b3601bc22012-02-21 21:23:201619 if (net_error != OK) {
[email protected]16c2bd72013-06-28 01:19:221620 OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error);
[email protected]b3601bc22012-02-21 21:23:201621 return;
1622 }
Miriam Gershenson61604662017-09-28 23:51:111623
1624 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.SuccessTime", duration);
[email protected]b3601bc22012-02-21 21:23:201625
[email protected]1def74c2012-03-22 20:07:001626 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS);
[email protected]1339a2a22012-10-17 08:39:431627 RecordTTL(ttl);
[email protected]0adcb2b2012-08-15 21:30:461628
[email protected]1ffdda82012-12-12 23:04:221629 resolver_->OnDnsTaskResolve(OK);
[email protected]f0f602bd2012-11-15 18:01:021630
[email protected]895123222012-10-25 15:21:171631 base::TimeDelta bounded_ttl =
1632 std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds));
1633
eroman0cd87b62016-12-14 19:13:451634 if (ContainsIcannNameCollisionIp(addr_list)) {
1635 CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION);
1636 } else {
Rob Percival94f21ad2017-11-14 10:20:241637 CompleteRequests(MakeCacheEntryWithTTL(net_error, addr_list,
1638 HostCache::Entry::SOURCE_DNS, ttl),
eroman0cd87b62016-12-14 19:13:451639 bounded_ttl);
1640 }
[email protected]b3601bc22012-02-21 21:23:201641 }
1642
dchengb03027d2014-10-21 12:00:201643 void OnFirstDnsTransactionComplete() override {
[email protected]daae1322013-09-05 18:26:501644 DCHECK(dns_task_->needs_two_transactions());
1645 DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued());
1646 // No longer need to occupy two dispatcher slots.
1647 ReduceToOneJobSlot();
1648
1649 // We already have a job slot at the dispatcher, so if the second
1650 // transaction hasn't started, reuse it now instead of waiting in the queue
1651 // for the second slot.
1652 if (dns_task_->needs_another_transaction())
1653 dns_task_->StartSecondTransaction();
1654 }
1655
Brad Lassey786929ad2018-02-21 20:54:271656 URLRequestContext* url_request_context() override {
1657 return resolver_->url_request_context_;
1658 }
1659
Miriam Gershenson61604662017-09-28 23:51:111660 void RecordJobHistograms(int error) {
Miriam Gershensonc282ab42017-11-20 23:19:581661 // Used in UMA_HISTOGRAM_ENUMERATION. Do not renumber entries or reuse
1662 // deprecated values.
1663 enum Category {
1664 RESOLVE_SUCCESS = 0,
1665 RESOLVE_FAIL = 1,
1666 RESOLVE_SPECULATIVE_SUCCESS = 2,
1667 RESOLVE_SPECULATIVE_FAIL = 3,
1668 RESOLVE_ABORT = 4,
1669 RESOLVE_SPECULATIVE_ABORT = 5,
Miriam Gershenson61604662017-09-28 23:51:111670 RESOLVE_MAX, // Bounding value.
1671 };
1672 Category category = RESOLVE_MAX; // Illegal value for later DCHECK only.
1673
1674 base::TimeDelta duration = base::TimeTicks::Now() - start_time_;
1675 if (error == OK) {
1676 if (had_non_speculative_request_) {
1677 category = RESOLVE_SUCCESS;
1678 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime", duration);
1679 switch (key_.address_family) {
1680 case ADDRESS_FAMILY_IPV4:
1681 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.IPV4",
1682 duration);
1683 break;
1684 case ADDRESS_FAMILY_IPV6:
1685 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.IPV6",
1686 duration);
1687 break;
1688 case ADDRESS_FAMILY_UNSPECIFIED:
1689 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.UNSPEC",
1690 duration);
1691 break;
1692 }
1693 } else {
1694 category = RESOLVE_SPECULATIVE_SUCCESS;
Miriam Gershenson61604662017-09-28 23:51:111695 }
Miriam Gershensonc282ab42017-11-20 23:19:581696 } else if (error == ERR_NETWORK_CHANGED ||
1697 error == ERR_HOST_RESOLVER_QUEUE_TOO_LARGE) {
1698 category = had_non_speculative_request_ ? RESOLVE_ABORT
1699 : RESOLVE_SPECULATIVE_ABORT;
Miriam Gershenson61604662017-09-28 23:51:111700 } else {
1701 if (had_non_speculative_request_) {
1702 category = RESOLVE_FAIL;
1703 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime", duration);
1704 switch (key_.address_family) {
1705 case ADDRESS_FAMILY_IPV4:
Miriam Gershensonc282ab42017-11-20 23:19:581706 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.IPV4",
Miriam Gershenson61604662017-09-28 23:51:111707 duration);
1708 break;
1709 case ADDRESS_FAMILY_IPV6:
Miriam Gershensonc282ab42017-11-20 23:19:581710 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.IPV6",
Miriam Gershenson61604662017-09-28 23:51:111711 duration);
1712 break;
1713 case ADDRESS_FAMILY_UNSPECIFIED:
Miriam Gershensonc282ab42017-11-20 23:19:581714 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.UNSPEC",
Miriam Gershenson61604662017-09-28 23:51:111715 duration);
1716 break;
1717 }
1718 } else {
1719 category = RESOLVE_SPECULATIVE_FAIL;
Miriam Gershenson61604662017-09-28 23:51:111720 }
1721 }
1722 DCHECK_LT(static_cast<int>(category),
1723 static_cast<int>(RESOLVE_MAX)); // Be sure it was set.
1724 UMA_HISTOGRAM_ENUMERATION("Net.DNS.ResolveCategory", category, RESOLVE_MAX);
Miriam Gershensonaaf139582017-11-27 16:06:061725
1726 if (category == RESOLVE_FAIL || category == RESOLVE_ABORT) {
1727 if (duration < base::TimeDelta::FromMilliseconds(10))
Ilya Sherman0eb39802017-12-08 20:58:181728 base::UmaHistogramSparse("Net.DNS.ResolveError.Fast", std::abs(error));
Miriam Gershensonaaf139582017-11-27 16:06:061729 else
Ilya Sherman0eb39802017-12-08 20:58:181730 base::UmaHistogramSparse("Net.DNS.ResolveError.Slow", std::abs(error));
Miriam Gershensonaaf139582017-11-27 16:06:061731 }
Miriam Gershenson61604662017-09-28 23:51:111732 }
1733
[email protected]16ee26d2012-03-08 03:34:351734 // Performs Job's last rites. Completes all Requests. Deletes this.
[email protected]895123222012-10-25 15:21:171735 void CompleteRequests(const HostCache::Entry& entry,
1736 base::TimeDelta ttl) {
[email protected]11fbca0b2013-06-02 23:37:211737 CHECK(resolver_.get());
[email protected]b3601bc22012-02-21 21:23:201738
[email protected]16ee26d2012-03-08 03:34:351739 // This job must be removed from resolver's |jobs_| now to make room for a
1740 // new job with the same key in case one of the OnComplete callbacks decides
1741 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1742 // is done.
danakj22f90e72016-04-16 01:55:401743 std::unique_ptr<Job> self_deleter(this);
[email protected]16ee26d2012-03-08 03:34:351744
1745 resolver_->RemoveJob(this);
1746
[email protected]16ee26d2012-03-08 03:34:351747 if (is_running()) {
[email protected]16ee26d2012-03-08 03:34:351748 if (is_proc_running()) {
[email protected]daae1322013-09-05 18:26:501749 DCHECK(!is_queued());
[email protected]16ee26d2012-03-08 03:34:351750 proc_task_->Cancel();
wezb9820d42016-06-22 23:41:041751 proc_task_ = nullptr;
[email protected]16ee26d2012-03-08 03:34:351752 }
[email protected]daae1322013-09-05 18:26:501753 KillDnsTask();
[email protected]16ee26d2012-03-08 03:34:351754
1755 // Signal dispatcher that a slot has opened.
[email protected]106ccd2c2014-06-17 09:21:001756 resolver_->dispatcher_->OnJobFinished();
[email protected]16ee26d2012-03-08 03:34:351757 } else if (is_queued()) {
[email protected]106ccd2c2014-06-17 09:21:001758 resolver_->dispatcher_->Cancel(handle_);
[email protected]16ee26d2012-03-08 03:34:351759 handle_.Reset();
1760 }
1761
1762 if (num_active_requests() == 0) {
mikecirone8b85c432016-09-08 19:11:001763 net_log_.AddEvent(NetLogEventType::CANCELLED);
1764 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
[email protected]16ee26d2012-03-08 03:34:351765 OK);
1766 return;
1767 }
[email protected]b3601bc22012-02-21 21:23:201768
mikecirone8b85c432016-09-08 19:11:001769 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
juliatuttle317860e2016-05-12 14:47:221770 entry.error());
[email protected]68ad3ee2010-01-30 03:45:391771
[email protected]78eac2a2012-03-14 19:09:271772 DCHECK(!requests_.empty());
1773
eroman1efc237c2016-12-14 00:00:451774 if (entry.error() == OK || entry.error() == ERR_ICANN_NAME_COLLISION) {
[email protected]d7b9a2b2012-05-31 22:31:191775 // Record this histogram here, when we know the system has a valid DNS
1776 // configuration.
[email protected]539df6c2012-06-19 21:21:291777 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1778 resolver_->received_dns_config_);
[email protected]d7b9a2b2012-05-31 22:31:191779 }
[email protected]16ee26d2012-03-08 03:34:351780
juliatuttle317860e2016-05-12 14:47:221781 bool did_complete = (entry.error() != ERR_NETWORK_CHANGED) &&
1782 (entry.error() != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
Miriam Gershensonc282ab42017-11-20 23:19:581783 if (did_complete)
[email protected]1339a2a22012-10-17 08:39:431784 resolver_->CacheResult(key_, entry, ttl);
Miriam Gershensonc282ab42017-11-20 23:19:581785
1786 RecordJobHistograms(entry.error());
[email protected]16ee26d2012-03-08 03:34:351787
maksim.sisov31452af2016-07-27 06:38:101788 // Complete all of the requests that were attached to the job and
1789 // detach them.
1790 while (!requests_.empty()) {
David Benjamina5677192018-04-23 23:25:421791 RequestImpl* req = requests_.head()->value();
1792 req->RemoveFromList();
[email protected]0f292de02012-02-01 22:28:201793 DCHECK_EQ(this, req->job());
1794 // Update the net log and notify registered observers.
juliatuttle317860e2016-05-12 14:47:221795 LogFinishRequest(req->source_net_log(), req->info(), entry.error());
[email protected]51b9a6b2012-06-25 21:50:291796 if (did_complete) {
1797 // Record effective total time from creation to completion.
Miriam Gershenson90d05e02017-09-28 19:29:281798 RecordTotalTime(req->info().is_speculative(), false,
[email protected]51b9a6b2012-06-25 21:50:291799 base::TimeTicks::Now() - req->request_time());
1800 }
maksim.sisov31452af2016-07-27 06:38:101801 req->OnJobCompleted(this, entry.error(), entry.addresses());
[email protected]0f292de02012-02-01 22:28:201802
1803 // Check if the resolver was destroyed as a result of running the
1804 // callback. If it was, we could continue, but we choose to bail.
[email protected]11fbca0b2013-06-02 23:37:211805 if (!resolver_.get())
[email protected]0f292de02012-02-01 22:28:201806 return;
1807 }
1808 }
1809
[email protected]1339a2a22012-10-17 08:39:431810 // Convenience wrapper for CompleteRequests in case of failure.
1811 void CompleteRequestsWithError(int net_error) {
Rob Percival94f21ad2017-11-14 10:20:241812 CompleteRequests(HostCache::Entry(net_error, AddressList(),
1813 HostCache::Entry::SOURCE_UNKNOWN),
[email protected]895123222012-10-25 15:21:171814 base::TimeDelta());
[email protected]1339a2a22012-10-17 08:39:431815 }
1816
Brad Lassey786929ad2018-02-21 20:54:271817 RequestPriority priority() const override {
[email protected]b4481b222012-03-16 17:13:111818 return priority_tracker_.highest_priority();
1819 }
1820
1821 // Number of non-canceled requests in |requests_|.
1822 size_t num_active_requests() const {
1823 return priority_tracker_.total_count();
1824 }
1825
wezb9820d42016-06-22 23:41:041826 bool is_dns_running() const { return !!dns_task_; }
[email protected]b4481b222012-03-16 17:13:111827
wezb9820d42016-06-22 23:41:041828 bool is_proc_running() const { return !!proc_task_; }
[email protected]b4481b222012-03-16 17:13:111829
[email protected]0f292de02012-02-01 22:28:201830 base::WeakPtr<HostResolverImpl> resolver_;
1831
1832 Key key_;
1833
1834 // Tracks the highest priority across |requests_|.
1835 PriorityTracker priority_tracker_;
1836
Miriam Gershensone42adb22017-10-16 16:19:381837 // Task runner used for HostResolverProc.
1838 scoped_refptr<base::TaskRunner> proc_task_runner_;
1839
[email protected]0f292de02012-02-01 22:28:201840 bool had_non_speculative_request_;
1841
[email protected]51b9a6b2012-06-25 21:50:291842 // Distinguishes measurements taken while DnsClient was fully configured.
1843 bool had_dns_config_;
1844
[email protected]daae1322013-09-05 18:26:501845 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1846 unsigned num_occupied_job_slots_;
1847
[email protected]1d932852012-06-19 19:40:331848 // Result of DnsTask.
1849 int dns_task_error_;
[email protected]1def74c2012-03-22 20:07:001850
[email protected]51b9a6b2012-06-25 21:50:291851 const base::TimeTicks creation_time_;
1852 base::TimeTicks priority_change_time_;
Miriam Gershenson61604662017-09-28 23:51:111853 base::TimeTicks start_time_;
[email protected]51b9a6b2012-06-25 21:50:291854
tfarina428341112016-09-22 13:38:201855 NetLogWithSource net_log_;
[email protected]0f292de02012-02-01 22:28:201856
[email protected]b3601bc22012-02-21 21:23:201857 // Resolves the host using a HostResolverProc.
[email protected]0f292de02012-02-01 22:28:201858 scoped_refptr<ProcTask> proc_task_;
1859
[email protected]b3601bc22012-02-21 21:23:201860 // Resolves the host using a DnsTransaction.
danakj22f90e72016-04-16 01:55:401861 std::unique_ptr<DnsTask> dns_task_;
[email protected]b3601bc22012-02-21 21:23:201862
[email protected]0f292de02012-02-01 22:28:201863 // All Requests waiting for the result of this Job. Some can be canceled.
David Benjamina5677192018-04-23 23:25:421864 base::LinkedList<RequestImpl> requests_;
[email protected]0f292de02012-02-01 22:28:201865
[email protected]16ee26d2012-03-08 03:34:351866 // A handle used in |HostResolverImpl::dispatcher_|.
[email protected]0f292de02012-02-01 22:28:201867 PrioritizedDispatcher::Handle handle_;
[email protected]68ad3ee2010-01-30 03:45:391868};
1869
1870//-----------------------------------------------------------------------------
1871
[email protected]0f292de02012-02-01 22:28:201872HostResolverImpl::ProcTaskParams::ProcTaskParams(
[email protected]e95d3aca2010-01-11 22:47:431873 HostResolverProc* resolver_proc,
[email protected]0f292de02012-02-01 22:28:201874 size_t max_retry_attempts)
1875 : resolver_proc(resolver_proc),
1876 max_retry_attempts(max_retry_attempts),
ttuttlecf1158bf2016-03-18 16:37:441877 unresponsive_delay(
1878 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs)),
[email protected]0f292de02012-02-01 22:28:201879 retry_factor(2) {
[email protected]106ccd2c2014-06-17 09:21:001880 // Maximum of 4 retry attempts for host resolution.
1881 static const size_t kDefaultMaxRetryAttempts = 4u;
1882 if (max_retry_attempts == HostResolver::kDefaultRetryAttempts)
1883 max_retry_attempts = kDefaultMaxRetryAttempts;
[email protected]0f292de02012-02-01 22:28:201884}
1885
vmpstracd23b72016-02-26 21:08:551886HostResolverImpl::ProcTaskParams::ProcTaskParams(const ProcTaskParams& other) =
1887 default;
1888
Chris Watkins68b15032017-12-01 03:07:131889HostResolverImpl::ProcTaskParams::~ProcTaskParams() = default;
[email protected]0f292de02012-02-01 22:28:201890
[email protected]b59ff372009-07-15 22:04:321891HostResolverImpl::~HostResolverImpl() {
gab47aa7da2017-06-02 16:09:431892 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]daae1322013-09-05 18:26:501893 // Prevent the dispatcher from starting new jobs.
[email protected]106ccd2c2014-06-17 09:21:001894 dispatcher_->SetLimitsToZero();
avi9e72eb482016-12-09 21:10:541895 // It's now safe for Jobs to call KillDnsTask on destruction, because
[email protected]daae1322013-09-05 18:26:501896 // OnJobComplete will not start any new jobs.
avi9e72eb482016-12-09 21:10:541897 jobs_.clear();
[email protected]e95d3aca2010-01-11 22:47:431898
[email protected]232a5812011-03-04 22:42:081899 NetworkChangeNotifier::RemoveIPAddressObserver(this);
ttuttlecf1158bf2016-03-18 16:37:441900 NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
[email protected]bb0e34542012-08-31 19:52:401901 NetworkChangeNotifier::RemoveDNSObserver(this);
[email protected]b59ff372009-07-15 22:04:321902}
1903
[email protected]0f292de02012-02-01 22:28:201904void HostResolverImpl::SetMaxQueuedJobs(size_t value) {
[email protected]106ccd2c2014-06-17 09:21:001905 DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
[email protected]0f292de02012-02-01 22:28:201906 DCHECK_GT(value, 0u);
1907 max_queued_jobs_ = value;
[email protected]be1a48b2011-01-20 00:12:131908}
1909
[email protected]684970b2009-08-14 04:54:461910int HostResolverImpl::Resolve(const RequestInfo& info,
[email protected]5109c1952013-08-20 18:44:101911 RequestPriority priority,
[email protected]b59ff372009-07-15 22:04:321912 AddressList* addresses,
[email protected]aa22b242011-11-16 18:58:291913 const CompletionCallback& callback,
maksim.sisov31452af2016-07-27 06:38:101914 std::unique_ptr<Request>* out_req,
tfarina428341112016-09-22 13:38:201915 const NetLogWithSource& source_net_log) {
[email protected]95a214c2011-08-04 21:50:401916 DCHECK(addresses);
gab47aa7da2017-06-02 16:09:431917 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]aa22b242011-11-16 18:58:291918 DCHECK_EQ(false, callback.is_null());
maksim.sisov31452af2016-07-27 06:38:101919 DCHECK(out_req);
[email protected]1ac6af92010-06-03 21:00:141920
Chris Palmer9cbf4212017-07-19 01:54:551921 LogStartRequest(source_net_log, info);
pauljensen19eb73422015-04-15 23:20:061922
Matt Menke23b4bbf82017-07-20 19:11:291923 Key key;
1924 int rv = ResolveHelper(info, false, nullptr, source_net_log, addresses, &key);
[email protected]95a214c2011-08-04 21:50:401925 if (rv != ERR_DNS_CACHE_MISS) {
xunjieli26f90452014-11-10 16:23:021926 LogFinishRequest(source_net_log, info, rv);
Miriam Gershenson90d05e02017-09-28 19:29:281927 RecordTotalTime(info.is_speculative(), true, base::TimeDelta());
[email protected]95a214c2011-08-04 21:50:401928 return rv;
[email protected]38368712011-03-02 08:09:401929 }
1930
[email protected]0f292de02012-02-01 22:28:201931 // Next we need to attach our request to a "job". This job is responsible for
1932 // calling "getaddrinfo(hostname)" on a worker thread.
1933
avi9e72eb482016-12-09 21:10:541934 auto jobit = jobs_.find(key);
[email protected]0f292de02012-02-01 22:28:201935 Job* job;
1936 if (jobit == jobs_.end()) {
Miriam Gershensone42adb22017-10-16 16:19:381937 job = new Job(weak_ptr_factory_.GetWeakPtr(), key, priority,
1938 proc_task_runner_, source_net_log);
[email protected]daae1322013-09-05 18:26:501939 job->Schedule(false);
[email protected]0f292de02012-02-01 22:28:201940
1941 // Check for queue overflow.
[email protected]106ccd2c2014-06-17 09:21:001942 if (dispatcher_->num_queued_jobs() > max_queued_jobs_) {
1943 Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest());
[email protected]0f292de02012-02-01 22:28:201944 DCHECK(evicted);
[email protected]16ee26d2012-03-08 03:34:351945 evicted->OnEvicted(); // Deletes |evicted|.
[email protected]0f292de02012-02-01 22:28:201946 if (evicted == job) {
[email protected]0f292de02012-02-01 22:28:201947 rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE;
xunjieli26f90452014-11-10 16:23:021948 LogFinishRequest(source_net_log, info, rv);
[email protected]0f292de02012-02-01 22:28:201949 return rv;
1950 }
[email protected]0f292de02012-02-01 22:28:201951 }
avi9e72eb482016-12-09 21:10:541952 jobs_[key] = base::WrapUnique(job);
[email protected]0f292de02012-02-01 22:28:201953 } else {
avi9e72eb482016-12-09 21:10:541954 job = jobit->second.get();
[email protected]0f292de02012-02-01 22:28:201955 }
1956
1957 // Can't complete synchronously. Create and attach request.
Jeremy Roman0579ed62017-08-29 15:56:191958 auto req = std::make_unique<RequestImpl>(source_net_log, info, priority,
avi9e72eb482016-12-09 21:10:541959 callback, addresses, job);
maksim.sisov31452af2016-07-27 06:38:101960 job->AddRequest(req.get());
1961 *out_req = std::move(req);
[email protected]b59ff372009-07-15 22:04:321962
[email protected]0f292de02012-02-01 22:28:201963 // Completion happens during Job::CompleteRequests().
[email protected]b59ff372009-07-15 22:04:321964 return ERR_IO_PENDING;
1965}
1966
Francois Doraya2d01ba2017-09-25 19:17:401967HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log)
mmenke91c17162016-06-02 16:03:231968 : max_queued_jobs_(0),
1969 proc_params_(NULL, options.max_retry_attempts),
1970 net_log_(net_log),
1971 received_dns_config_(false),
1972 num_dns_failures_(0),
mgershaf9a9232017-04-13 20:19:031973 assume_ipv6_failure_on_wifi_(false),
mmenke91c17162016-06-02 16:03:231974 use_local_ipv6_(false),
1975 last_ipv6_probe_result_(true),
mmenke91c17162016-06-02 16:03:231976 additional_resolver_flags_(0),
1977 fallback_to_proctask_(true),
mmenke91c17162016-06-02 16:03:231978 weak_ptr_factory_(this),
1979 probe_weak_ptr_factory_(this) {
1980 if (options.enable_caching)
1981 cache_ = HostCache::CreateDefaultCache();
1982
1983 PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits();
1984 dispatcher_.reset(new PrioritizedDispatcher(job_limits));
1985 max_queued_jobs_ = job_limits.total_jobs * 100u;
1986
1987 DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
1988
Miriam Gershensone42adb22017-10-16 16:19:381989 proc_task_runner_ = base::CreateTaskRunnerWithTraits(
Maks Orlovich5393c8682018-02-26 16:17:501990 {base::MayBlock(), priority_mode.Get(),
1991 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
Miriam Gershensone42adb22017-10-16 16:19:381992
mmenke91c17162016-06-02 16:03:231993#if defined(OS_WIN)
1994 EnsureWinsockInit();
1995#endif
Fabrice de Gans-Riberi7de47372018-05-08 20:23:471996#if (defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)) || \
1997 defined(OS_FUCHSIA)
mmenke91c17162016-06-02 16:03:231998 RunLoopbackProbeJob();
1999#endif
2000 NetworkChangeNotifier::AddIPAddressObserver(this);
2001 NetworkChangeNotifier::AddConnectionTypeObserver(this);
2002 NetworkChangeNotifier::AddDNSObserver(this);
Fabrice de Gans-Riberi7de47372018-05-08 20:23:472003// TODO(crbug/836416): Remove OS_FUCHSIA here.
mmenke91c17162016-06-02 16:03:232004#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
Sergey Ulanov49085572017-07-10 23:25:462005 !defined(OS_ANDROID) && !defined(OS_FUCHSIA)
mmenke91c17162016-06-02 16:03:232006 EnsureDnsReloaderInit();
2007#endif
2008
2009 OnConnectionTypeChanged(NetworkChangeNotifier::GetConnectionType());
2010
2011 {
2012 DnsConfig dns_config;
2013 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2014 received_dns_config_ = dns_config.IsValid();
2015 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2016 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
2017 }
2018
2019 fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
2020}
2021
2022void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) {
2023 if (result) {
2024 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
2025 } else {
2026 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
2027 }
2028}
2029
Miriam Gershensone42adb22017-10-16 16:19:382030void HostResolverImpl::SetTaskRunnerForTesting(
2031 scoped_refptr<base::TaskRunner> task_runner) {
2032 proc_task_runner_ = std::move(task_runner);
2033}
2034
Matt Menke23b4bbf82017-07-20 19:11:292035int HostResolverImpl::ResolveHelper(const RequestInfo& info,
juliatuttle9fb7aeb2016-06-06 20:16:332036 bool allow_stale,
2037 HostCache::EntryStaleness* stale_info,
Matt Menke23b4bbf82017-07-20 19:11:292038 const NetLogWithSource& source_net_log,
2039 AddressList* addresses,
2040 Key* key) {
2041 IPAddress ip_address;
2042 IPAddress* ip_address_ptr = nullptr;
2043 if (ip_address.AssignFromIPLiteral(info.hostname())) {
2044 ip_address_ptr = &ip_address;
2045 } else {
2046 // Check that the caller supplied a valid hostname to resolve.
2047 if (!IsValidDNSDomain(info.hostname()))
2048 return ERR_NAME_NOT_RESOLVED;
2049 }
2050
2051 // Build a key that identifies the request in the cache and in the
2052 // outstanding jobs map.
2053 *key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log);
2054
juliatuttle9fb7aeb2016-06-06 20:16:332055 DCHECK(allow_stale == !!stale_info);
[email protected]95a214c2011-08-04 21:50:402056 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
2057 // On Windows it gives the default interface's address, whereas on Linux it
2058 // gives an error. We will make it fail on all platforms for consistency.
juliatuttle9fb7aeb2016-06-06 20:16:332059 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) {
2060 MakeNotStale(stale_info);
[email protected]95a214c2011-08-04 21:50:402061 return ERR_NAME_NOT_RESOLVED;
juliatuttle9fb7aeb2016-06-06 20:16:332062 }
[email protected]95a214c2011-08-04 21:50:402063
2064 int net_error = ERR_UNEXPECTED;
Matt Menke23b4bbf82017-07-20 19:11:292065 if (ResolveAsIP(*key, info, ip_address_ptr, &net_error, addresses)) {
juliatuttle9fb7aeb2016-06-06 20:16:332066 MakeNotStale(stale_info);
[email protected]95a214c2011-08-04 21:50:402067 return net_error;
juliatuttle9fb7aeb2016-06-06 20:16:332068 }
Mike Westda1c690e2017-08-12 05:57:162069
2070 // Special-case localhost names, as per the recommendations in
2071 // https://tools.ietf.org/html/draft-west-let-localhost-be-localhost.
2072 if (ServeLocalhost(*key, info, addresses)) {
2073 MakeNotStale(stale_info);
2074 return OK;
2075 }
2076
Matt Menke23b4bbf82017-07-20 19:11:292077 if (ServeFromCache(*key, info, &net_error, addresses, allow_stale,
juliatuttle9fb7aeb2016-06-06 20:16:332078 stale_info)) {
mgersh33d23c02017-03-29 20:24:252079 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_CACHE_HIT,
2080 addresses->CreateNetLogCallback());
juliatuttle9fb7aeb2016-06-06 20:16:332081 // |ServeFromCache()| will set |*stale_info| as needed.
[email protected]78eac2a2012-03-14 19:09:272082 return net_error;
2083 }
Mike Westda1c690e2017-08-12 05:57:162084
[email protected]78eac2a2012-03-14 19:09:272085 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2086 // http://crbug.com/117655
Matt Menke23b4bbf82017-07-20 19:11:292087 if (ServeFromHosts(*key, info, addresses)) {
mgersh33d23c02017-03-29 20:24:252088 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_HOSTS_HIT,
2089 addresses->CreateNetLogCallback());
juliatuttle9fb7aeb2016-06-06 20:16:332090 MakeNotStale(stale_info);
[email protected]78eac2a2012-03-14 19:09:272091 return OK;
2092 }
estarkd1bc206e2015-06-20 00:44:392093
[email protected]78eac2a2012-03-14 19:09:272094 return ERR_DNS_CACHE_MISS;
[email protected]95a214c2011-08-04 21:50:402095}
2096
2097int HostResolverImpl::ResolveFromCache(const RequestInfo& info,
2098 AddressList* addresses,
tfarina428341112016-09-22 13:38:202099 const NetLogWithSource& source_net_log) {
gab47aa7da2017-06-02 16:09:432100 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]95a214c2011-08-04 21:50:402101 DCHECK(addresses);
2102
[email protected]95a214c2011-08-04 21:50:402103 // Update the net log and notify registered observers.
xunjieli26f90452014-11-10 16:23:022104 LogStartRequest(source_net_log, info);
[email protected]95a214c2011-08-04 21:50:402105
Matt Menke23b4bbf82017-07-20 19:11:292106 Key key;
2107 int rv = ResolveHelper(info, false, nullptr, source_net_log, addresses, &key);
[email protected]95a214c2011-08-04 21:50:402108
xunjieli26f90452014-11-10 16:23:022109 LogFinishRequest(source_net_log, info, rv);
[email protected]95a214c2011-08-04 21:50:402110 return rv;
2111}
2112
[email protected]a8883e452012-11-17 05:58:062113void HostResolverImpl::SetDnsClientEnabled(bool enabled) {
gab47aa7da2017-06-02 16:09:432114 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]a8883e452012-11-17 05:58:062115#if defined(ENABLE_BUILT_IN_DNS)
2116 if (enabled && !dns_client_) {
2117 SetDnsClient(DnsClient::CreateClient(net_log_));
2118 } else if (!enabled && dns_client_) {
danakj22f90e72016-04-16 01:55:402119 SetDnsClient(std::unique_ptr<DnsClient>());
[email protected]a8883e452012-11-17 05:58:062120 }
2121#endif
2122}
2123
[email protected]489d1a82011-10-12 03:09:112124HostCache* HostResolverImpl::GetHostCache() {
2125 return cache_.get();
2126}
[email protected]95a214c2011-08-04 21:50:402127
Rob Percivalbc658a22017-12-13 08:24:422128bool HostResolverImpl::HasCached(base::StringPiece hostname,
2129 HostCache::Entry::Source* source_out,
2130 HostCache::EntryStaleness* stale_out) const {
2131 if (!cache_)
2132 return false;
2133
2134 return cache_->HasEntry(hostname, source_out, stale_out);
2135}
2136
danakj22f90e72016-04-16 01:55:402137std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const {
[email protected]17e92032012-03-29 00:56:242138 // Check if async DNS is disabled.
2139 if (!dns_client_.get())
tfhef3618f2016-01-11 23:07:082140 return nullptr;
[email protected]17e92032012-03-29 00:56:242141
2142 // Check if async DNS is enabled, but we currently have no configuration
2143 // for it.
2144 const DnsConfig* dns_config = dns_client_->GetConfig();
wezb9820d42016-06-22 23:41:042145 if (!dns_config)
Jeremy Roman0579ed62017-08-29 15:56:192146 return std::make_unique<base::DictionaryValue>();
[email protected]17e92032012-03-29 00:56:242147
2148 return dns_config->ToValue();
2149}
2150
juliatuttle9fb7aeb2016-06-06 20:16:332151int HostResolverImpl::ResolveStaleFromCache(
2152 const RequestInfo& info,
2153 AddressList* addresses,
2154 HostCache::EntryStaleness* stale_info,
tfarina428341112016-09-22 13:38:202155 const NetLogWithSource& source_net_log) {
gab47aa7da2017-06-02 16:09:432156 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
juliatuttle9fb7aeb2016-06-06 20:16:332157 DCHECK(addresses);
2158 DCHECK(stale_info);
2159
2160 // Update the net log and notify registered observers.
2161 LogStartRequest(source_net_log, info);
2162
Matt Menke23b4bbf82017-07-20 19:11:292163 Key key;
2164 int rv =
2165 ResolveHelper(info, true, stale_info, source_net_log, addresses, &key);
juliatuttle9fb7aeb2016-06-06 20:16:332166 LogFinishRequest(source_net_log, info, rv);
2167 return rv;
2168}
2169
Miriam Gershenson2839ef112017-08-30 03:25:372170size_t HostResolverImpl::LastRestoredCacheSize() const {
2171 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
2172
2173 return cache_ ? cache_->last_restore_size() : 0;
2174}
2175
2176size_t HostResolverImpl::CacheSize() const {
2177 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
2178
2179 return cache_ ? cache_->size() : 0;
2180}
2181
mgershaf9a9232017-04-13 20:19:032182void HostResolverImpl::SetNoIPv6OnWifi(bool no_ipv6_on_wifi) {
gab47aa7da2017-06-02 16:09:432183 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
mgershaf9a9232017-04-13 20:19:032184 assume_ipv6_failure_on_wifi_ = no_ipv6_on_wifi;
2185}
2186
2187bool HostResolverImpl::GetNoIPv6OnWifi() {
2188 return assume_ipv6_failure_on_wifi_;
2189}
2190
Brad Lassey786929ad2018-02-21 20:54:272191void HostResolverImpl::SetRequestContext(URLRequestContext* context) {
2192 if (context != url_request_context_) {
2193 url_request_context_ = context;
2194 }
2195}
2196
2197void HostResolverImpl::ClearDnsOverHttpsServers() {
2198 if (dns_over_https_servers_.size() == 0)
2199 return;
2200
2201 dns_over_https_servers_.clear();
2202
2203 if (dns_client_.get() && dns_client_->GetConfig())
2204 UpdateDNSConfig(true);
2205}
2206
2207void HostResolverImpl::AddDnsOverHttpsServer(std::string spec, bool use_post) {
2208 GURL url(spec);
2209 if (!url.SchemeIs("https"))
2210 return;
2211
2212 dns_over_https_servers_.emplace_back(url, use_post);
2213
2214 if (dns_client_.get() && dns_client_->GetConfig())
2215 UpdateDNSConfig(true);
2216}
2217
[email protected]95a214c2011-08-04 21:50:402218bool HostResolverImpl::ResolveAsIP(const Key& key,
2219 const RequestInfo& info,
martijna23c8962016-03-04 18:18:512220 const IPAddress* ip_address,
[email protected]95a214c2011-08-04 21:50:402221 int* net_error,
2222 AddressList* addresses) {
2223 DCHECK(addresses);
2224 DCHECK(net_error);
martijna23c8962016-03-04 18:18:512225 if (ip_address == nullptr)
[email protected]95a214c2011-08-04 21:50:402226 return false;
2227
[email protected]95a214c2011-08-04 21:50:402228 *net_error = OK;
martijna23c8962016-03-04 18:18:512229 AddressFamily family = GetAddressFamily(*ip_address);
cbentzel1906f872015-06-05 16:25:252230 if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED &&
2231 key.address_family != family) {
[email protected]1c7cf3f82014-08-07 21:33:482232 // Don't return IPv6 addresses for IPv4 queries, and vice versa.
[email protected]95a214c2011-08-04 21:50:402233 *net_error = ERR_NAME_NOT_RESOLVED;
2234 } else {
martijna23c8962016-03-04 18:18:512235 *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port());
[email protected]7054e78f2012-05-07 21:44:562236 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)
2237 addresses->SetDefaultCanonicalName();
[email protected]95a214c2011-08-04 21:50:402238 }
2239 return true;
2240}
2241
2242bool HostResolverImpl::ServeFromCache(const Key& key,
2243 const RequestInfo& info,
[email protected]95a214c2011-08-04 21:50:402244 int* net_error,
juliatuttle9fb7aeb2016-06-06 20:16:332245 AddressList* addresses,
2246 bool allow_stale,
2247 HostCache::EntryStaleness* stale_info) {
[email protected]95a214c2011-08-04 21:50:402248 DCHECK(addresses);
2249 DCHECK(net_error);
juliatuttle9fb7aeb2016-06-06 20:16:332250 DCHECK(allow_stale == !!stale_info);
[email protected]95a214c2011-08-04 21:50:402251 if (!info.allow_cached_response() || !cache_.get())
2252 return false;
2253
juliatuttle9fb7aeb2016-06-06 20:16:332254 const HostCache::Entry* cache_entry;
2255 if (allow_stale)
2256 cache_entry = cache_->LookupStale(key, base::TimeTicks::Now(), stale_info);
2257 else
2258 cache_entry = cache_->Lookup(key, base::TimeTicks::Now());
[email protected]95a214c2011-08-04 21:50:402259 if (!cache_entry)
2260 return false;
2261
juliatuttle317860e2016-05-12 14:47:222262 *net_error = cache_entry->error();
[email protected]7054e78f2012-05-07 21:44:562263 if (*net_error == OK) {
[email protected]1339a2a22012-10-17 08:39:432264 if (cache_entry->has_ttl())
juliatuttle317860e2016-05-12 14:47:222265 RecordTTL(cache_entry->ttl());
2266 *addresses = EnsurePortOnAddressList(cache_entry->addresses(), info.port());
[email protected]7054e78f2012-05-07 21:44:562267 }
[email protected]95a214c2011-08-04 21:50:402268 return true;
2269}
2270
[email protected]78eac2a2012-03-14 19:09:272271bool HostResolverImpl::ServeFromHosts(const Key& key,
2272 const RequestInfo& info,
2273 AddressList* addresses) {
2274 DCHECK(addresses);
2275 if (!HaveDnsConfig())
2276 return false;
[email protected]05a79d42013-03-28 07:30:092277 addresses->clear();
2278
[email protected]cb507622012-03-23 16:17:062279 // HOSTS lookups are case-insensitive.
brettw8e2106d2015-08-11 19:30:222280 std::string hostname = base::ToLowerASCII(key.hostname);
[email protected]cb507622012-03-23 16:17:062281
[email protected]05a79d42013-03-28 07:30:092282 const DnsHosts& hosts = dns_client_->GetConfig()->hosts;
2283
[email protected]78eac2a2012-03-14 19:09:272284 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2285 // (glibc and c-ares) return the first matching line. We have more
2286 // flexibility, but lose implicit ordering.
[email protected]05a79d42013-03-28 07:30:092287 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2288 // necessary.
2289 if (key.address_family == ADDRESS_FAMILY_IPV6 ||
2290 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2291 DnsHosts::const_iterator it = hosts.find(
2292 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6));
2293 if (it != hosts.end())
2294 addresses->push_back(IPEndPoint(it->second, info.port()));
[email protected]78eac2a2012-03-14 19:09:272295 }
2296
[email protected]05a79d42013-03-28 07:30:092297 if (key.address_family == ADDRESS_FAMILY_IPV4 ||
2298 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2299 DnsHosts::const_iterator it = hosts.find(
2300 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4));
2301 if (it != hosts.end())
2302 addresses->push_back(IPEndPoint(it->second, info.port()));
2303 }
2304
[email protected]ec666ab22013-04-17 20:05:592305 // If got only loopback addresses and the family was restricted, resolve
2306 // again, without restrictions. See SystemHostResolverCall for rationale.
2307 if ((key.host_resolver_flags &
2308 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
2309 IsAllIPv4Loopback(*addresses)) {
2310 Key new_key(key);
2311 new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED;
2312 new_key.host_resolver_flags &=
2313 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2314 return ServeFromHosts(new_key, info, addresses);
2315 }
[email protected]05a79d42013-03-28 07:30:092316 return !addresses->empty();
[email protected]78eac2a2012-03-14 19:09:272317}
2318
estarkd1bc206e2015-06-20 00:44:392319bool HostResolverImpl::ServeLocalhost(const Key& key,
2320 const RequestInfo& info,
2321 AddressList* addresses) {
2322 AddressList resolved_addresses;
2323 if (!ResolveLocalHostname(key.hostname, info.port(), &resolved_addresses))
2324 return false;
2325
2326 addresses->clear();
2327
2328 for (const auto& address : resolved_addresses) {
2329 // Include the address if:
2330 // - caller didn't specify an address family, or
2331 // - caller specifically asked for the address family of this address, or
2332 // - this is an IPv6 address and caller specifically asked for IPv4 due
2333 // to lack of detected IPv6 support. (See SystemHostResolverCall for
2334 // rationale).
2335 if (key.address_family == ADDRESS_FAMILY_UNSPECIFIED ||
2336 key.address_family == address.GetFamily() ||
2337 (address.GetFamily() == ADDRESS_FAMILY_IPV6 &&
2338 key.address_family == ADDRESS_FAMILY_IPV4 &&
2339 (key.host_resolver_flags &
2340 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6))) {
2341 addresses->push_back(address);
2342 }
2343 }
2344
2345 return true;
2346}
2347
[email protected]16ee26d2012-03-08 03:34:352348void HostResolverImpl::CacheResult(const Key& key,
[email protected]1339a2a22012-10-17 08:39:432349 const HostCache::Entry& entry,
[email protected]16ee26d2012-03-08 03:34:352350 base::TimeDelta ttl) {
Miriam Gershensone3bc63022017-10-09 19:57:052351 // Don't cache an error unless it has a positive TTL.
2352 if (cache_.get() && (entry.error() == OK || ttl > base::TimeDelta()))
[email protected]1339a2a22012-10-17 08:39:432353 cache_->Set(key, entry, base::TimeTicks::Now(), ttl);
[email protected]ef4c40c2010-09-01 14:42:032354}
2355
[email protected]0f292de02012-02-01 22:28:202356void HostResolverImpl::RemoveJob(Job* job) {
2357 DCHECK(job);
avi9e72eb482016-12-09 21:10:542358 auto it = jobs_.find(job->key());
2359 if (it != jobs_.end() && it->second.get() == job) {
2360 it->second.release();
[email protected]16ee26d2012-03-08 03:34:352361 jobs_.erase(it);
avi9e72eb482016-12-09 21:10:542362 }
[email protected]b59ff372009-07-15 22:04:322363}
2364
[email protected]137af622010-02-05 02:14:352365HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest(
pauljensen19eb73422015-04-15 23:20:062366 const RequestInfo& info,
martijna23c8962016-03-04 18:18:512367 const IPAddress* ip_address,
tfarina428341112016-09-22 13:38:202368 const NetLogWithSource& net_log) {
[email protected]eaf3a3b2010-09-03 20:34:272369 HostResolverFlags effective_flags =
2370 info.host_resolver_flags() | additional_resolver_flags_;
[email protected]137af622010-02-05 02:14:352371 AddressFamily effective_address_family = info.address_family();
[email protected]9db6f702013-04-10 18:10:512372
mgersh3eba057d2017-02-28 18:50:562373 if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED &&
2374 // When resolving IPv4 literals, there's no need to probe for IPv6.
2375 // When resolving IPv6 literals, there's no benefit to artificially
2376 // limiting our resolution based on a probe. Prior logic ensures
2377 // that this query is UNSPECIFIED (see effective_address_family
2378 // check above) so the code requesting the resolution should be amenable
2379 // to receiving a IPv6 resolution.
2380 !use_local_ipv6_ && ip_address == nullptr && !IsIPv6Reachable(net_log)) {
2381 effective_address_family = ADDRESS_FAMILY_IPV4;
2382 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
[email protected]9db6f702013-04-10 18:10:512383 }
2384
estarkd1bc206e2015-06-20 00:44:392385 return Key(info.hostname(), effective_address_family, effective_flags);
[email protected]137af622010-02-05 02:14:352386}
2387
tfarina428341112016-09-22 13:38:202388bool HostResolverImpl::IsIPv6Reachable(const NetLogWithSource& net_log) {
mgershaf9a9232017-04-13 20:19:032389 // Don't bother checking if the device is on WiFi and IPv6 is assumed to not
2390 // work on WiFi.
2391 if (assume_ipv6_failure_on_wifi_ &&
2392 NetworkChangeNotifier::GetConnectionType() ==
2393 NetworkChangeNotifier::CONNECTION_WIFI) {
2394 return false;
2395 }
2396
eroman1a17ef32016-12-14 01:06:092397 // Cache the result for kIPv6ProbePeriodMs (measured from after
2398 // IsGloballyReachable() completes).
sergeyub8cdc212015-05-14 18:50:372399 bool cached = true;
eroman1a17ef32016-12-14 01:06:092400 if ((base::TimeTicks::Now() - last_ipv6_probe_time_).InMilliseconds() >
2401 kIPv6ProbePeriodMs) {
martijna23c8962016-03-04 18:18:512402 last_ipv6_probe_result_ =
2403 IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log);
eroman1a17ef32016-12-14 01:06:092404 last_ipv6_probe_time_ = base::TimeTicks::Now();
sergeyub8cdc212015-05-14 18:50:372405 cached = false;
2406 }
mikecirone8b85c432016-09-08 19:11:002407 net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK,
sergeyub8cdc212015-05-14 18:50:372408 base::Bind(&NetLogIPv6AvailableCallback,
2409 last_ipv6_probe_result_, cached));
2410 return last_ipv6_probe_result_;
2411}
2412
mgershaf9a9232017-04-13 20:19:032413bool HostResolverImpl::IsGloballyReachable(const IPAddress& dest,
2414 const NetLogWithSource& net_log) {
mgershaf9a9232017-04-13 20:19:032415 std::unique_ptr<DatagramClientSocket> socket(
2416 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
Sergey Ulanovcbdfc8852018-03-16 20:13:282417 DatagramSocket::DEFAULT_BIND, net_log.net_log(), net_log.source()));
mgershaf9a9232017-04-13 20:19:032418 int rv = socket->Connect(IPEndPoint(dest, 53));
2419 if (rv != OK)
2420 return false;
2421 IPEndPoint endpoint;
2422 rv = socket->GetLocalAddress(&endpoint);
2423 if (rv != OK)
2424 return false;
2425 DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily());
2426 const IPAddress& address = endpoint.address();
2427
2428 bool is_link_local =
2429 (address.bytes()[0] == 0xFE) && ((address.bytes()[1] & 0xC0) == 0x80);
2430 if (is_link_local)
2431 return false;
2432
2433 const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0};
2434 if (IPAddressStartsWith(address, kTeredoPrefix))
2435 return false;
2436
2437 return true;
2438}
2439
mmenke91c17162016-06-02 16:03:232440void HostResolverImpl::RunLoopbackProbeJob() {
Francois Doraya2d01ba2017-09-25 19:17:402441 // Run this asynchronously as it can take 40-100ms and should not block
2442 // initialization.
2443 base::PostTaskWithTraitsAndReplyWithResult(
2444 FROM_HERE,
2445 {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
2446 base::BindOnce(&HaveOnlyLoopbackAddresses),
2447 base::BindOnce(&HostResolverImpl::SetHaveOnlyLoopbackAddresses,
2448 weak_ptr_factory_.GetWeakPtr()));
mmenke91c17162016-06-02 16:03:232449}
2450
[email protected]35ddc282010-09-21 23:42:062451void HostResolverImpl::AbortAllInProgressJobs() {
[email protected]b3601bc22012-02-21 21:23:202452 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2453 // first collect and remove all running jobs from |jobs_|.
danakj22f90e72016-04-16 01:55:402454 std::vector<std::unique_ptr<Job>> jobs_to_abort;
avi9e72eb482016-12-09 21:10:542455 for (auto it = jobs_.begin(); it != jobs_.end();) {
2456 Job* job = it->second.get();
[email protected]0f292de02012-02-01 22:28:202457 if (job->is_running()) {
avi9e72eb482016-12-09 21:10:542458 jobs_to_abort.push_back(std::move(it->second));
[email protected]b3601bc22012-02-21 21:23:202459 jobs_.erase(it++);
[email protected]0f292de02012-02-01 22:28:202460 } else {
[email protected]b3601bc22012-02-21 21:23:202461 DCHECK(job->is_queued());
2462 ++it;
[email protected]0f292de02012-02-01 22:28:202463 }
[email protected]ef4c40c2010-09-01 14:42:032464 }
[email protected]b3601bc22012-02-21 21:23:202465
[email protected]daae1322013-09-05 18:26:502466 // Pause the dispatcher so it won't start any new dispatcher jobs while
2467 // aborting the old ones. This is needed so that it won't start the second
2468 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2469 // invalid.
[email protected]106ccd2c2014-06-17 09:21:002470 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2471 dispatcher_->SetLimits(
[email protected]daae1322013-09-05 18:26:502472 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
[email protected]70c04ab2013-08-22 16:05:122473
[email protected]57a48d32012-03-03 00:04:552474 // Life check to bail once |this| is deleted.
[email protected]4589a3a2012-09-20 20:57:072475 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]57a48d32012-03-03 00:04:552476
[email protected]16ee26d2012-03-08 03:34:352477 // Then Abort them.
[email protected]11fbca0b2013-06-02 23:37:212478 for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
[email protected]57a48d32012-03-03 00:04:552479 jobs_to_abort[i]->Abort();
olli.raulaa21e9eb72015-12-17 08:18:112480 ignore_result(jobs_to_abort[i].release());
[email protected]b3601bc22012-02-21 21:23:202481 }
[email protected]daae1322013-09-05 18:26:502482
2483 if (self)
[email protected]106ccd2c2014-06-17 09:21:002484 dispatcher_->SetLimits(limits);
[email protected]daae1322013-09-05 18:26:502485}
2486
2487void HostResolverImpl::AbortDnsTasks() {
2488 // Pause the dispatcher so it won't start any new dispatcher jobs while
2489 // aborting the old ones. This is needed so that it won't start the second
2490 // DnsTransaction for a job if the DnsConfig just changed.
[email protected]106ccd2c2014-06-17 09:21:002491 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2492 dispatcher_->SetLimits(
[email protected]daae1322013-09-05 18:26:502493 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2494
avi9e72eb482016-12-09 21:10:542495 for (auto it = jobs_.begin(); it != jobs_.end(); ++it)
[email protected]daae1322013-09-05 18:26:502496 it->second->AbortDnsTask();
[email protected]106ccd2c2014-06-17 09:21:002497 dispatcher_->SetLimits(limits);
[email protected]ef4c40c2010-09-01 14:42:032498}
2499
[email protected]78eac2a2012-03-14 19:09:272500void HostResolverImpl::TryServingAllJobsFromHosts() {
2501 if (!HaveDnsConfig())
2502 return;
2503
2504 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2505 // http://crbug.com/117655
2506
2507 // Life check to bail once |this| is deleted.
[email protected]4589a3a2012-09-20 20:57:072508 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]78eac2a2012-03-14 19:09:272509
avi9e72eb482016-12-09 21:10:542510 for (auto it = jobs_.begin(); self.get() && it != jobs_.end();) {
2511 Job* job = it->second.get();
[email protected]78eac2a2012-03-14 19:09:272512 ++it;
2513 // This could remove |job| from |jobs_|, but iterator will remain valid.
2514 job->ServeFromHosts();
2515 }
2516}
2517
[email protected]be1a48b2011-01-20 00:12:132518void HostResolverImpl::OnIPAddressChanged() {
sergeyub8cdc212015-05-14 18:50:372519 last_ipv6_probe_time_ = base::TimeTicks();
[email protected]12faa4c2012-11-06 04:44:182520 // Abandon all ProbeJobs.
2521 probe_weak_ptr_factory_.InvalidateWeakPtrs();
Matt Menkeef8f5cf82017-07-20 22:50:172522 if (cache_.get())
mgersh5bb3b9d2017-02-13 21:27:102523 cache_->OnNetworkChange();
Fabrice de Gans-Riberi7de47372018-05-08 20:23:472524#if (defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)) || \
2525 defined(OS_FUCHSIA)
mmenke91c17162016-06-02 16:03:232526 RunLoopbackProbeJob();
[email protected]be1a48b2011-01-20 00:12:132527#endif
2528 AbortAllInProgressJobs();
2529 // |this| may be deleted inside AbortAllInProgressJobs().
2530}
2531
ttuttlecf1158bf2016-03-18 16:37:442532void HostResolverImpl::OnConnectionTypeChanged(
2533 NetworkChangeNotifier::ConnectionType type) {
2534 proc_params_.unresponsive_delay =
2535 GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
2536 "DnsUnresponsiveDelayMsByConnectionType",
2537 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs),
2538 type);
2539}
2540
pauljensen101ed372015-04-17 00:11:422541void HostResolverImpl::OnInitialDNSConfigRead() {
2542 UpdateDNSConfig(false);
2543}
2544
[email protected]bb0e34542012-08-31 19:52:402545void HostResolverImpl::OnDNSChanged() {
pauljensen101ed372015-04-17 00:11:422546 UpdateDNSConfig(true);
2547}
2548
2549void HostResolverImpl::UpdateDNSConfig(bool config_changed) {
[email protected]bb0e34542012-08-31 19:52:402550 DnsConfig dns_config;
2551 NetworkChangeNotifier::GetDnsConfig(&dns_config);
[email protected]ec666ab22013-04-17 20:05:592552
[email protected]b4481b222012-03-16 17:13:112553 if (net_log_) {
mikecirone8b85c432016-09-08 19:11:002554 net_log_->AddGlobalEntry(NetLogEventType::DNS_CONFIG_CHANGED,
2555 base::Bind(&NetLogDnsConfigCallback, &dns_config));
[email protected]b4481b222012-03-16 17:13:112556 }
2557
[email protected]01b3b9d2012-08-13 16:18:142558 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
[email protected]d7b9a2b2012-05-31 22:31:192559 received_dns_config_ = dns_config.IsValid();
[email protected]c9fa8f312013-09-17 12:24:522560 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2561 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
[email protected]78eac2a2012-03-14 19:09:272562
[email protected]a8883e452012-11-17 05:58:062563 num_dns_failures_ = 0;
2564
[email protected]01b3b9d2012-08-13 16:18:142565 // We want a new DnsSession in place, before we Abort running Jobs, so that
2566 // the newly started jobs use the new config.
[email protected]f0f602bd2012-11-15 18:01:022567 if (dns_client_.get()) {
Miriam Gershenson44aafc122017-10-18 19:29:252568 // Make sure that if the update is an initial read, not a change, there
Miriam Gershenson668e8442017-10-31 20:31:532569 // wasn't already a DnsConfig or it's the same one.
2570 DCHECK(config_changed || !dns_client_->GetConfig() ||
2571 dns_client_->GetConfig()->Equals(dns_config));
Brad Lassey786929ad2018-02-21 20:54:272572 dns_config.dns_over_https_servers = dns_over_https_servers_;
[email protected]d7b9a2b2012-05-31 22:31:192573 dns_client_->SetConfig(dns_config);
Miriam Gershenson44aafc122017-10-18 19:29:252574 if (dns_client_->GetConfig())
[email protected]f0f602bd2012-11-15 18:01:022575 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
[email protected]f0f602bd2012-11-15 18:01:022576 }
[email protected]01b3b9d2012-08-13 16:18:142577
pauljensen101ed372015-04-17 00:11:422578 if (config_changed) {
2579 // If the DNS server has changed, existing cached info could be wrong so we
mgersh92b1af12017-03-29 15:49:232580 // have to expire our internal cache :( Note that OS level DNS caches, such
pauljensen101ed372015-04-17 00:11:422581 // as NSCD's cache should be dropped automatically by the OS when
2582 // resolv.conf changes so we don't need to do anything to clear that cache.
Matt Menkeef8f5cf82017-07-20 22:50:172583 if (cache_.get())
mgersh92b1af12017-03-29 15:49:232584 cache_->OnNetworkChange();
[email protected]01b3b9d2012-08-13 16:18:142585
pauljensen101ed372015-04-17 00:11:422586 // Life check to bail once |this| is deleted.
2587 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]f0f602bd2012-11-15 18:01:022588
pauljensen101ed372015-04-17 00:11:422589 // Existing jobs will have been sent to the original server so they need to
2590 // be aborted.
2591 AbortAllInProgressJobs();
[email protected]01b3b9d2012-08-13 16:18:142592
pauljensen101ed372015-04-17 00:11:422593 // |this| may be deleted inside AbortAllInProgressJobs().
2594 if (self.get())
2595 TryServingAllJobsFromHosts();
2596 }
[email protected]78eac2a2012-03-14 19:09:272597}
2598
2599bool HostResolverImpl::HaveDnsConfig() const {
[email protected]32b1dbcf2013-01-26 03:48:252600 // Use DnsClient only if it's fully configured and there is no override by
2601 // ScopedDefaultHostResolverProc.
2602 // The alternative is to use NetworkChangeNotifier to override DnsConfig,
2603 // but that would introduce construction order requirements for NCN and SDHRP.
wezb9820d42016-06-22 23:41:042604 return dns_client_ && dns_client_->GetConfig() &&
2605 (proc_params_.resolver_proc || !HostResolverProc::GetDefault());
[email protected]b3601bc22012-02-21 21:23:202606}
2607
[email protected]1ffdda82012-12-12 23:04:222608void HostResolverImpl::OnDnsTaskResolve(int net_error) {
[email protected]f0f602bd2012-11-15 18:01:022609 DCHECK(dns_client_);
[email protected]1ffdda82012-12-12 23:04:222610 if (net_error == OK) {
[email protected]f0f602bd2012-11-15 18:01:022611 num_dns_failures_ = 0;
2612 return;
2613 }
2614 ++num_dns_failures_;
2615 if (num_dns_failures_ < kMaximumDnsFailures)
2616 return;
[email protected]daae1322013-09-05 18:26:502617
2618 // Disable DnsClient until the next DNS change. Must be done before aborting
2619 // DnsTasks, since doing so may start new jobs.
[email protected]f0f602bd2012-11-15 18:01:022620 dns_client_->SetConfig(DnsConfig());
[email protected]daae1322013-09-05 18:26:502621
2622 // Switch jobs with active DnsTasks over to using ProcTasks.
2623 AbortDnsTasks();
2624
[email protected]f0f602bd2012-11-15 18:01:022625 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
Ilya Sherman0eb39802017-12-08 20:58:182626 base::UmaHistogramSparse("AsyncDNS.DnsClientDisabledReason",
2627 std::abs(net_error));
[email protected]f0f602bd2012-11-15 18:01:022628}
2629
danakj22f90e72016-04-16 01:55:402630void HostResolverImpl::SetDnsClient(std::unique_ptr<DnsClient> dns_client) {
[email protected]daae1322013-09-05 18:26:502631 // DnsClient and config must be updated before aborting DnsTasks, since doing
2632 // so may start new jobs.
dchengc7eeda422015-12-26 03:56:482633 dns_client_ = std::move(dns_client);
[email protected]daae1322013-09-05 18:26:502634 if (dns_client_ && !dns_client_->GetConfig() &&
2635 num_dns_failures_ < kMaximumDnsFailures) {
2636 DnsConfig dns_config;
2637 NetworkChangeNotifier::GetDnsConfig(&dns_config);
Brad Lassey786929ad2018-02-21 20:54:272638 dns_config.dns_over_https_servers = dns_over_https_servers_;
[email protected]daae1322013-09-05 18:26:502639 dns_client_->SetConfig(dns_config);
2640 num_dns_failures_ = 0;
2641 if (dns_client_->GetConfig())
2642 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
[email protected]a8883e452012-11-17 05:58:062643 }
[email protected]daae1322013-09-05 18:26:502644
2645 AbortDnsTasks();
[email protected]a8883e452012-11-17 05:58:062646}
2647
maksim.sisov31452af2016-07-27 06:38:102648HostResolverImpl::RequestImpl::~RequestImpl() {
2649 if (job_)
2650 job_->CancelRequest(this);
2651}
2652
2653void HostResolverImpl::RequestImpl::ChangeRequestPriority(
2654 RequestPriority priority) {
2655 job_->ChangeRequestPriority(this, priority);
2656}
2657
[email protected]b59ff372009-07-15 22:04:322658} // namespace net