blob: f23c0a40145ea80af2775fb3263c966e0cb1ef7e [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>
9#elif defined(OS_POSIX)
10#include <netdb.h>
11#endif
12
tfarinada9e1362017-03-14 16:49:0613#if defined(OS_POSIX)
14#include <netinet/in.h>
15#if !defined(OS_NACL)
16#include <net/if.h>
17#if !defined(OS_ANDROID)
18#include <ifaddrs.h>
19#endif // !defined(OS_ANDROID)
20#endif // !defined(OS_NACL)
21#endif // defined(OS_POSIX)
22
[email protected]68ad3ee2010-01-30 03:45:3923#include <cmath>
thestiga74ad2b2016-07-11 20:52:3624#include <memory>
[email protected]0f292de02012-02-01 22:28:2025#include <utility>
[email protected]21526002010-05-16 19:42:4626#include <vector>
[email protected]68ad3ee2010-01-30 03:45:3927
[email protected]33152acc2011-10-20 23:37:1228#include "base/bind.h"
[email protected]aa22b242011-11-16 18:58:2929#include "base/bind_helpers.h"
[email protected]0f292de02012-02-01 22:28:2030#include "base/callback.h"
maksim.sisov31452af2016-07-27 06:38:1031#include "base/callback_helpers.h"
[email protected]b59ff372009-07-15 22:04:3232#include "base/compiler_specific.h"
Brett Wilsonc6a0c822017-09-12 00:04:2933#include "base/containers/circular_deque.h"
[email protected]58580352010-10-26 04:07:5034#include "base/debug/debugger.h"
35#include "base/debug/stack_trace.h"
Avi Drissman13fc8932015-12-20 04:40:4636#include "base/macros.h"
mmenke91c17162016-06-02 16:03:2337#include "base/memory/ptr_util.h"
[email protected]1e9bbd22010-10-15 16:42:4538#include "base/metrics/field_trial.h"
Maks Orlovich5393c8682018-02-26 16:17:5039#include "base/metrics/field_trial_params.h"
Ilya Sherman0eb39802017-12-08 20:58:1840#include "base/metrics/histogram_functions.h"
davidbenad6fc442015-05-18 20:59:1341#include "base/metrics/histogram_macros.h"
anujk.sharma7c907f02015-05-29 02:55:4442#include "base/single_thread_task_runner.h"
[email protected]be528af2013-06-11 07:39:4843#include "base/strings/string_util.h"
[email protected]750b2f3c2013-06-07 18:41:0544#include "base/strings/utf_string_conversions.h"
Francois Doraya2d01ba2017-09-25 19:17:4045#include "base/task_scheduler/post_task.h"
46#include "base/threading/scoped_blocking_call.h"
gabf767595f2016-05-11 18:50:3547#include "base/threading/thread_task_runner_handle.h"
[email protected]66e96c42013-06-28 15:20:3148#include "base/time/time.h"
ssid6d6b40102016-04-05 18:59:5649#include "base/trace_event/trace_event.h"
[email protected]21526002010-05-16 19:42:4650#include "base/values.h"
[email protected]b3601bc22012-02-21 21:23:2051#include "net/base/address_family.h"
[email protected]b59ff372009-07-15 22:04:3252#include "net/base/address_list.h"
[email protected]ee094b82010-08-24 15:55:5153#include "net/base/host_port_pair.h"
martijna23c8962016-03-04 18:18:5154#include "net/base/ip_address.h"
[email protected]1c7cf3f82014-08-07 21:33:4855#include "net/base/ip_endpoint.h"
[email protected]2bb04442010-08-18 18:01:1556#include "net/base/net_errors.h"
xunjieli0b7f5b62016-12-06 20:43:4857#include "net/base/trace_constants.h"
tfarina9ed7f8c52016-02-19 17:50:1858#include "net/base/url_util.h"
[email protected]0adcb2b2012-08-15 21:30:4659#include "net/dns/address_sorter.h"
[email protected]78eac2a2012-03-14 19:09:2760#include "net/dns/dns_client.h"
[email protected]b3601bc22012-02-21 21:23:2061#include "net/dns/dns_config_service.h"
62#include "net/dns/dns_protocol.h"
tfarina47598f042015-10-07 23:08:3563#include "net/dns/dns_reloader.h"
[email protected]b3601bc22012-02-21 21:23:2064#include "net/dns/dns_response.h"
[email protected]b3601bc22012-02-21 21:23:2065#include "net/dns/dns_transaction.h"
tfarina77021d62015-10-11 20:19:0366#include "net/dns/dns_util.h"
[email protected]f2cb3cf2013-03-21 01:40:5367#include "net/dns/host_resolver_proc.h"
eroman87c53d62015-04-02 06:51:0768#include "net/log/net_log.h"
mikecironef22f9812016-10-04 03:40:1969#include "net/log/net_log_capture_mode.h"
mikecirone8b85c432016-09-08 19:11:0070#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1971#include "net/log/net_log_parameters_callback.h"
72#include "net/log/net_log_source.h"
mikecirone8b85c432016-09-08 19:11:0073#include "net/log/net_log_source_type.h"
mikecironef22f9812016-10-04 03:40:1974#include "net/log/net_log_with_source.h"
[email protected]9db6f702013-04-10 18:10:5175#include "net/socket/client_socket_factory.h"
tfarina5dd13c22016-11-16 12:08:2676#include "net/socket/datagram_client_socket.h"
pauljensen370f1c72015-02-17 16:59:1477#include "url/url_canon_ip.h"
[email protected]b59ff372009-07-15 22:04:3278
79#if defined(OS_WIN)
80#include "net/base/winsock_init.h"
81#endif
82
tfarinada9e1362017-03-14 16:49:0683#if defined(OS_ANDROID)
84#include "net/android/network_library.h"
85#endif
86
[email protected]b59ff372009-07-15 22:04:3287namespace net {
88
[email protected]e95d3aca2010-01-11 22:47:4389namespace {
90
ttuttlecf1158bf2016-03-18 16:37:4491// Default delay between calls to the system resolver for the same hostname.
92// (Can be overridden by field trial.)
93const int64_t kDnsDefaultUnresponsiveDelayMs = 6000;
94
[email protected]6e78dfb2011-07-28 21:34:4795// Limit the size of hostnames that will be resolved to combat issues in
96// some platform's resolvers.
97const size_t kMaxHostLength = 4096;
98
[email protected]a2730882012-01-21 00:56:2799// Default TTL for successful resolutions with ProcTask.
100const unsigned kCacheEntryTTLSeconds = 60;
101
[email protected]b3601bc22012-02-21 21:23:20102// Default TTL for unsuccessful resolutions with ProcTask.
103const unsigned kNegativeCacheEntryTTLSeconds = 0;
104
[email protected]895123222012-10-25 15:21:17105// Minimum TTL for successful resolutions with DnsTask.
106const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
107
sergeyub8cdc212015-05-14 18:50:37108// Time between IPv6 probes, i.e. for how long results of each IPv6 probe are
109// cached.
110const int kIPv6ProbePeriodMs = 1000;
111
112// Google DNS address used for IPv6 probes.
113const uint8_t kIPv6ProbeAddress[] =
114 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
116
[email protected]24f4bab2010-10-15 01:27:11117// We use a separate histogram name for each platform to facilitate the
118// display of error codes by their symbolic name (since each platform has
119// different mappings).
120const char kOSErrorsForGetAddrinfoHistogramName[] =
121#if defined(OS_WIN)
122 "Net.OSErrorsForGetAddrinfo_Win";
123#elif defined(OS_MACOSX)
124 "Net.OSErrorsForGetAddrinfo_Mac";
125#elif defined(OS_LINUX)
126 "Net.OSErrorsForGetAddrinfo_Linux";
127#else
128 "Net.OSErrorsForGetAddrinfo";
129#endif
130
[email protected]c89b2442011-05-26 14:28:27131// Gets a list of the likely error codes that getaddrinfo() can return
132// (non-exhaustive). These are the error codes that we will track via
133// a histogram.
134std::vector<int> GetAllGetAddrinfoOSErrors() {
135 int os_errors[] = {
136#if defined(OS_POSIX)
[email protected]23f771162011-06-02 18:37:51137#if !defined(OS_FREEBSD)
[email protected]39588992011-07-11 19:54:37138#if !defined(OS_ANDROID)
[email protected]c48aef92011-11-22 23:41:45139 // EAI_ADDRFAMILY has been declared obsolete in Android's and
140 // FreeBSD's netdb.h.
[email protected]c89b2442011-05-26 14:28:27141 EAI_ADDRFAMILY,
[email protected]39588992011-07-11 19:54:37142#endif
[email protected]c48aef92011-11-22 23:41:45143 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
[email protected]23f771162011-06-02 18:37:51144 EAI_NODATA,
145#endif
[email protected]c89b2442011-05-26 14:28:27146 EAI_AGAIN,
147 EAI_BADFLAGS,
148 EAI_FAIL,
149 EAI_FAMILY,
150 EAI_MEMORY,
[email protected]c89b2442011-05-26 14:28:27151 EAI_NONAME,
152 EAI_SERVICE,
153 EAI_SOCKTYPE,
154 EAI_SYSTEM,
155#elif defined(OS_WIN)
156 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
157 WSA_NOT_ENOUGH_MEMORY,
158 WSAEAFNOSUPPORT,
159 WSAEINVAL,
160 WSAESOCKTNOSUPPORT,
161 WSAHOST_NOT_FOUND,
162 WSANO_DATA,
163 WSANO_RECOVERY,
164 WSANOTINITIALISED,
165 WSATRY_AGAIN,
166 WSATYPE_NOT_FOUND,
167 // The following are not in doc, but might be to appearing in results :-(.
168 WSA_INVALID_HANDLE,
169#endif
170 };
171
172 // Ensure all errors are positive, as histogram only tracks positive values.
173 for (size_t i = 0; i < arraysize(os_errors); ++i) {
174 os_errors[i] = std::abs(os_errors[i]);
175 }
176
177 return base::CustomHistogram::ArrayToCustomRanges(os_errors,
178 arraysize(os_errors));
179}
180
[email protected]1def74c2012-03-22 20:07:00181enum DnsResolveStatus {
182 RESOLVE_STATUS_DNS_SUCCESS = 0,
183 RESOLVE_STATUS_PROC_SUCCESS,
184 RESOLVE_STATUS_FAIL,
[email protected]1d932852012-06-19 19:40:33185 RESOLVE_STATUS_SUSPECT_NETBIOS,
[email protected]1def74c2012-03-22 20:07:00186 RESOLVE_STATUS_MAX
187};
188
eroman91dd3602015-03-26 03:46:33189// ICANN uses this localhost address to indicate a name collision.
190//
191// The policy in Chromium is to fail host resolving if it resolves to
192// this special address.
193//
194// Not however that IP literals are exempt from this policy, so it is still
195// possible to navigate to http://127.0.53.53/ directly.
196//
197// For more details: https://www.icann.org/news/announcement-2-2014-08-01-en
martijna23c8962016-03-04 18:18:51198const uint8_t kIcanNameCollisionIp[] = {127, 0, 53, 53};
eroman91dd3602015-03-26 03:46:33199
eroman1efc237c2016-12-14 00:00:45200bool ContainsIcannNameCollisionIp(const AddressList& addr_list) {
201 for (const auto& endpoint : addr_list) {
202 const IPAddress& addr = endpoint.address();
203 if (addr.IsIPv4() && IPAddressStartsWith(addr, kIcanNameCollisionIp)) {
204 return true;
205 }
206 }
207 return false;
208}
209
[email protected]1def74c2012-03-22 20:07:00210void UmaAsyncDnsResolveStatus(DnsResolveStatus result) {
211 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
212 result,
213 RESOLVE_STATUS_MAX);
214}
215
[email protected]1d932852012-06-19 19:40:33216bool ResemblesNetBIOSName(const std::string& hostname) {
217 return (hostname.size() < 16) && (hostname.find('.') == std::string::npos);
218}
219
220// True if |hostname| ends with either ".local" or ".local.".
221bool ResemblesMulticastDNSName(const std::string& hostname) {
222 DCHECK(!hostname.empty());
223 const char kSuffix[] = ".local.";
224 const size_t kSuffixLen = sizeof(kSuffix) - 1;
225 const size_t kSuffixLenTrimmed = kSuffixLen - 1;
thestiga74ad2b2016-07-11 20:52:36226 if (hostname.back() == '.') {
[email protected]1d932852012-06-19 19:40:33227 return hostname.size() > kSuffixLen &&
228 !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix);
229 }
230 return hostname.size() > kSuffixLenTrimmed &&
231 !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed,
232 kSuffix, kSuffixLenTrimmed);
233}
234
[email protected]51b9a6b2012-06-25 21:50:29235// A macro to simplify code and readability.
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21236#define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
237 do { \
238 switch (priority) { \
239 case HIGHEST: \
Miriam Gershenson90d05e02017-09-28 19:29:28240 UMA_HISTOGRAM_LONG_TIMES_100(basename ".HIGHEST", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21241 break; \
242 case MEDIUM: \
Miriam Gershenson90d05e02017-09-28 19:29:28243 UMA_HISTOGRAM_LONG_TIMES_100(basename ".MEDIUM", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21244 break; \
245 case LOW: \
Miriam Gershenson90d05e02017-09-28 19:29:28246 UMA_HISTOGRAM_LONG_TIMES_100(basename ".LOW", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21247 break; \
248 case LOWEST: \
Miriam Gershenson90d05e02017-09-28 19:29:28249 UMA_HISTOGRAM_LONG_TIMES_100(basename ".LOWEST", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21250 break; \
251 case IDLE: \
Miriam Gershenson90d05e02017-09-28 19:29:28252 UMA_HISTOGRAM_LONG_TIMES_100(basename ".IDLE", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21253 break; \
254 case THROTTLED: \
Miriam Gershenson90d05e02017-09-28 19:29:28255 UMA_HISTOGRAM_LONG_TIMES_100(basename ".THROTTLED", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21256 break; \
257 } \
258 UMA_HISTOGRAM_LONG_TIMES_100(basename, time); \
[email protected]51b9a6b2012-06-25 21:50:29259 } while (0)
260
261// Record time from Request creation until a valid DNS response.
Miriam Gershenson90d05e02017-09-28 19:29:28262void RecordTotalTime(bool speculative,
263 bool from_cache,
[email protected]51b9a6b2012-06-25 21:50:29264 base::TimeDelta duration) {
Miriam Gershenson7ba03e12018-02-07 22:48:24265 if (!speculative) {
Miriam Gershenson90d05e02017-09-28 19:29:28266 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.TotalTime", duration);
Miriam Gershenson90d05e02017-09-28 19:29:28267
Miriam Gershenson7ba03e12018-02-07 22:48:24268 if (!from_cache)
Miriam Gershenson90d05e02017-09-28 19:29:28269 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.TotalTimeNotCached", duration);
[email protected]51b9a6b2012-06-25 21:50:29270 }
271}
272
[email protected]1339a2a22012-10-17 08:39:43273void RecordTTL(base::TimeDelta ttl) {
274 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl,
275 base::TimeDelta::FromSeconds(1),
276 base::TimeDelta::FromDays(1), 100);
277}
278
[email protected]16c2bd72013-06-28 01:19:22279bool ConfigureAsyncDnsNoFallbackFieldTrial() {
280 const bool kDefault = false;
281
282 // Configure the AsyncDns field trial as follows:
283 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
284 // groups AsyncDnsA and AsyncDnsB: return false,
285 // groups SystemDnsA and SystemDnsB: return false,
286 // otherwise (trial absent): return default.
287 std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
brettw3a2c6902015-07-06 19:43:29288 if (!group_name.empty()) {
289 return base::StartsWith(group_name, "AsyncDnsNoFallback",
290 base::CompareCase::INSENSITIVE_ASCII);
291 }
[email protected]16c2bd72013-06-28 01:19:22292 return kDefault;
293}
294
Maks Orlovich5393c8682018-02-26 16:17:50295const base::FeatureParam<base::TaskPriority>::Option prio_modes[] = {
296 {base::TaskPriority::USER_VISIBLE, "default"},
297 {base::TaskPriority::USER_BLOCKING, "user_blocking"}};
298const base::Feature kSystemResolverPriorityExperiment = {
299 "SystemResolverPriorityExperiment", base::FEATURE_DISABLED_BY_DEFAULT};
300const base::FeatureParam<base::TaskPriority> priority_mode{
301 &kSystemResolverPriorityExperiment, "mode",
302 base::TaskPriority::USER_VISIBLE, &prio_modes};
303
[email protected]d7b9a2b2012-05-31 22:31:19304//-----------------------------------------------------------------------------
305
Avi Drissman13fc8932015-12-20 04:40:46306AddressList EnsurePortOnAddressList(const AddressList& list, uint16_t port) {
[email protected]895123222012-10-25 15:21:17307 if (list.empty() || list.front().port() == port)
308 return list;
309 return AddressList::CopyWithPort(list, port);
[email protected]7054e78f2012-05-07 21:44:56310}
311
[email protected]ec666ab22013-04-17 20:05:59312// Returns true if |addresses| contains only IPv4 loopback addresses.
313bool IsAllIPv4Loopback(const AddressList& addresses) {
314 for (unsigned i = 0; i < addresses.size(); ++i) {
martijna23c8962016-03-04 18:18:51315 const IPAddress& address = addresses[i].address();
[email protected]ec666ab22013-04-17 20:05:59316 switch (addresses[i].GetFamily()) {
317 case ADDRESS_FAMILY_IPV4:
martijna23c8962016-03-04 18:18:51318 if (address.bytes()[0] != 127)
[email protected]ec666ab22013-04-17 20:05:59319 return false;
320 break;
321 case ADDRESS_FAMILY_IPV6:
322 return false;
323 default:
324 NOTREACHED();
325 return false;
326 }
327 }
328 return true;
329}
330
tfarinada9e1362017-03-14 16:49:06331// Returns true if it can determine that only loopback addresses are configured.
332// i.e. if only 127.0.0.1 and ::1 are routable.
333// Also returns false if it cannot determine this.
334bool HaveOnlyLoopbackAddresses() {
Francois Doraya2d01ba2017-09-25 19:17:40335 base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::WILL_BLOCK);
tfarinada9e1362017-03-14 16:49:06336#if defined(OS_ANDROID)
337 return android::HaveOnlyLoopbackAddresses();
338#elif defined(OS_NACL)
339 NOTIMPLEMENTED();
340 return false;
341#elif defined(OS_POSIX)
342 struct ifaddrs* interface_addr = NULL;
343 int rv = getifaddrs(&interface_addr);
344 if (rv != 0) {
345 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
346 return false;
347 }
348
349 bool result = true;
350 for (struct ifaddrs* interface = interface_addr;
351 interface != NULL;
352 interface = interface->ifa_next) {
353 if (!(IFF_UP & interface->ifa_flags))
354 continue;
355 if (IFF_LOOPBACK & interface->ifa_flags)
356 continue;
357 const struct sockaddr* addr = interface->ifa_addr;
358 if (!addr)
359 continue;
360 if (addr->sa_family == AF_INET6) {
361 // Safe cast since this is AF_INET6.
362 const struct sockaddr_in6* addr_in6 =
363 reinterpret_cast<const struct sockaddr_in6*>(addr);
364 const struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
365 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr))
366 continue;
367 }
368 if (addr->sa_family != AF_INET6 && addr->sa_family != AF_INET)
369 continue;
370
371 result = false;
372 break;
373 }
374 freeifaddrs(interface_addr);
375 return result;
376#elif defined(OS_WIN)
377 // TODO(wtc): implement with the GetAdaptersAddresses function.
378 NOTIMPLEMENTED();
379 return false;
380#else
381 NOTIMPLEMENTED();
382 return false;
383#endif // defined(various platforms)
384}
385
[email protected]cd565142012-06-12 16:21:45386// Creates NetLog parameters when the resolve failed.
danakj22f90e72016-04-16 01:55:40387std::unique_ptr<base::Value> NetLogProcTaskFailedCallback(
Avi Drissman13fc8932015-12-20 04:40:46388 uint32_t attempt_number,
eroman001c3742015-04-23 03:11:17389 int net_error,
390 int os_error,
391 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40392 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]cd565142012-06-12 16:21:45393 if (attempt_number)
394 dict->SetInteger("attempt_number", attempt_number);
[email protected]21526002010-05-16 19:42:46395
[email protected]cd565142012-06-12 16:21:45396 dict->SetInteger("net_error", net_error);
[email protected]13024882011-05-18 23:19:16397
[email protected]cd565142012-06-12 16:21:45398 if (os_error) {
399 dict->SetInteger("os_error", os_error);
[email protected]21526002010-05-16 19:42:46400#if defined(OS_POSIX)
[email protected]cd565142012-06-12 16:21:45401 dict->SetString("os_error_string", gai_strerror(os_error));
[email protected]21526002010-05-16 19:42:46402#elif defined(OS_WIN)
[email protected]cd565142012-06-12 16:21:45403 // Map the error code to a human-readable string.
wezb9820d42016-06-22 23:41:04404 LPWSTR error_string = nullptr;
Peter Kastingbe940e92014-11-20 23:14:08405 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
406 0, // Use the internal message table.
407 os_error,
408 0, // Use default language.
409 (LPWSTR)&error_string,
410 0, // Buffer size.
411 0); // Arguments (unused).
[email protected]ad65a3e2013-12-25 18:18:01412 dict->SetString("os_error_string", base::WideToUTF8(error_string));
[email protected]cd565142012-06-12 16:21:45413 LocalFree(error_string);
[email protected]21526002010-05-16 19:42:46414#endif
[email protected]21526002010-05-16 19:42:46415 }
416
dchengc7eeda422015-12-26 03:56:48417 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45418}
[email protected]a9813302012-04-28 09:29:28419
[email protected]cd565142012-06-12 16:21:45420// Creates NetLog parameters when the DnsTask failed.
danakj22f90e72016-04-16 01:55:40421std::unique_ptr<base::Value> NetLogDnsTaskFailedCallback(
estade5e5529d2015-05-21 20:59:11422 int net_error,
423 int dns_error,
424 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40425 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]cd565142012-06-12 16:21:45426 dict->SetInteger("net_error", net_error);
427 if (dns_error)
428 dict->SetInteger("dns_error", dns_error);
dchengc7eeda422015-12-26 03:56:48429 return std::move(dict);
thestiga74ad2b2016-07-11 20:52:36430}
[email protected]ee094b82010-08-24 15:55:51431
[email protected]cd565142012-06-12 16:21:45432// Creates NetLog parameters containing the information in a RequestInfo object,
mikecironef22f9812016-10-04 03:40:19433// along with the associated NetLogSource.
danakj22f90e72016-04-16 01:55:40434std::unique_ptr<base::Value> NetLogRequestInfoCallback(
estade5e5529d2015-05-21 20:59:11435 const HostResolver::RequestInfo* info,
436 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40437 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b3601bc22012-02-21 21:23:20438
[email protected]cd565142012-06-12 16:21:45439 dict->SetString("host", info->host_port_pair().ToString());
440 dict->SetInteger("address_family",
441 static_cast<int>(info->address_family()));
442 dict->SetBoolean("allow_cached_response", info->allow_cached_response());
443 dict->SetBoolean("is_speculative", info->is_speculative());
dchengc7eeda422015-12-26 03:56:48444 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45445}
[email protected]b3601bc22012-02-21 21:23:20446
[email protected]cd565142012-06-12 16:21:45447// Creates NetLog parameters for the creation of a HostResolverImpl::Job.
danakj22f90e72016-04-16 01:55:40448std::unique_ptr<base::Value> NetLogJobCreationCallback(
mikecironef22f9812016-10-04 03:40:19449 const NetLogSource& source,
estade5e5529d2015-05-21 20:59:11450 const std::string* host,
451 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40452 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
estade5e5529d2015-05-21 20:59:11453 source.AddToEventParameters(dict.get());
[email protected]cd565142012-06-12 16:21:45454 dict->SetString("host", *host);
dchengc7eeda422015-12-26 03:56:48455 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45456}
[email protected]a9813302012-04-28 09:29:28457
[email protected]cd565142012-06-12 16:21:45458// Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
danakj22f90e72016-04-16 01:55:40459std::unique_ptr<base::Value> NetLogJobAttachCallback(
mikecironef22f9812016-10-04 03:40:19460 const NetLogSource& source,
estade5e5529d2015-05-21 20:59:11461 RequestPriority priority,
462 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40463 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
estade5e5529d2015-05-21 20:59:11464 source.AddToEventParameters(dict.get());
[email protected]3b04d1f22013-10-16 00:23:56465 dict->SetString("priority", RequestPriorityToString(priority));
dchengc7eeda422015-12-26 03:56:48466 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45467}
[email protected]b3601bc22012-02-21 21:23:20468
[email protected]cd565142012-06-12 16:21:45469// Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
danakj22f90e72016-04-16 01:55:40470std::unique_ptr<base::Value> NetLogDnsConfigCallback(
estade5e5529d2015-05-21 20:59:11471 const DnsConfig* config,
472 NetLogCaptureMode /* capture_mode */) {
tfhef3618f2016-01-11 23:07:08473 return config->ToValue();
[email protected]cd565142012-06-12 16:21:45474}
[email protected]b4481b222012-03-16 17:13:11475
danakj22f90e72016-04-16 01:55:40476std::unique_ptr<base::Value> NetLogIPv6AvailableCallback(
estade5e5529d2015-05-21 20:59:11477 bool ipv6_available,
478 bool cached,
479 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40480 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
sergeyub8cdc212015-05-14 18:50:37481 dict->SetBoolean("ipv6_available", ipv6_available);
482 dict->SetBoolean("cached", cached);
dchengc7eeda422015-12-26 03:56:48483 return std::move(dict);
sergeyub8cdc212015-05-14 18:50:37484}
485
[email protected]0f292de02012-02-01 22:28:20486// The logging routines are defined here because some requests are resolved
487// without a Request object.
488
489// Logs when a request has just been started.
tfarina428341112016-09-22 13:38:20490void LogStartRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20491 const HostResolver::RequestInfo& info) {
mikecirone8b85c432016-09-08 19:11:00492 source_net_log.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST,
493 base::Bind(&NetLogRequestInfoCallback, &info));
[email protected]0f292de02012-02-01 22:28:20494}
495
496// Logs when a request has just completed (before its callback is run).
tfarina428341112016-09-22 13:38:20497void LogFinishRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20498 const HostResolver::RequestInfo& info,
[email protected]b3601bc22012-02-21 21:23:20499 int net_error) {
xunjieli26f90452014-11-10 16:23:02500 source_net_log.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00501 NetLogEventType::HOST_RESOLVER_IMPL_REQUEST, net_error);
[email protected]0f292de02012-02-01 22:28:20502}
503
504// Logs when a request has been cancelled.
tfarina428341112016-09-22 13:38:20505void LogCancelRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20506 const HostResolverImpl::RequestInfo& info) {
mikecirone8b85c432016-09-08 19:11:00507 source_net_log.AddEvent(NetLogEventType::CANCELLED);
508 source_net_log.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST);
[email protected]0f292de02012-02-01 22:28:20509}
510
[email protected]b59ff372009-07-15 22:04:32511//-----------------------------------------------------------------------------
512
[email protected]0f292de02012-02-01 22:28:20513// Keeps track of the highest priority.
514class PriorityTracker {
515 public:
[email protected]8c98d002012-07-18 19:02:27516 explicit PriorityTracker(RequestPriority initial_priority)
517 : highest_priority_(initial_priority), total_count_(0) {
[email protected]0f292de02012-02-01 22:28:20518 memset(counts_, 0, sizeof(counts_));
519 }
520
521 RequestPriority highest_priority() const {
522 return highest_priority_;
523 }
524
525 size_t total_count() const {
526 return total_count_;
527 }
528
529 void Add(RequestPriority req_priority) {
530 ++total_count_;
531 ++counts_[req_priority];
[email protected]31ae7ab2012-04-24 21:09:05532 if (highest_priority_ < req_priority)
[email protected]0f292de02012-02-01 22:28:20533 highest_priority_ = req_priority;
534 }
535
536 void Remove(RequestPriority req_priority) {
537 DCHECK_GT(total_count_, 0u);
538 DCHECK_GT(counts_[req_priority], 0u);
539 --total_count_;
540 --counts_[req_priority];
541 size_t i;
thestiga74ad2b2016-07-11 20:52:36542 for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i) {
543 }
[email protected]0f292de02012-02-01 22:28:20544 highest_priority_ = static_cast<RequestPriority>(i);
545
[email protected]31ae7ab2012-04-24 21:09:05546 // In absence of requests, default to MINIMUM_PRIORITY.
547 if (total_count_ == 0)
548 DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
[email protected]0f292de02012-02-01 22:28:20549 }
550
551 private:
552 RequestPriority highest_priority_;
553 size_t total_count_;
554 size_t counts_[NUM_PRIORITIES];
555};
556
juliatuttle9fb7aeb2016-06-06 20:16:33557void MakeNotStale(HostCache::EntryStaleness* stale_info) {
558 if (!stale_info)
559 return;
560 stale_info->expired_by = base::TimeDelta::FromSeconds(-1);
561 stale_info->network_changes = 0;
562 stale_info->stale_hits = 0;
563}
564
juliatuttled7fa5852016-07-29 13:16:57565// Persist data every five minutes (potentially, cache and learned RTT).
566const int64_t kPersistDelaySec = 300;
567
[email protected]c54a8912012-10-22 22:09:43568} // namespace
[email protected]0f292de02012-02-01 22:28:20569
570//-----------------------------------------------------------------------------
571
tfarina9ed7f8c52016-02-19 17:50:18572bool ResolveLocalHostname(base::StringPiece host,
573 uint16_t port,
574 AddressList* address_list) {
tfarina9ed7f8c52016-02-19 17:50:18575 address_list->clear();
576
577 bool is_local6;
578 if (!IsLocalHostname(host, &is_local6))
579 return false;
580
martijna23c8962016-03-04 18:18:51581 address_list->push_back(IPEndPoint(IPAddress::IPv6Localhost(), port));
tfarina9ed7f8c52016-02-19 17:50:18582 if (!is_local6) {
martijna23c8962016-03-04 18:18:51583 address_list->push_back(IPEndPoint(IPAddress::IPv4Localhost(), port));
tfarina9ed7f8c52016-02-19 17:50:18584 }
585
586 return true;
587}
588
[email protected]daae1322013-09-05 18:26:50589const unsigned HostResolverImpl::kMaximumDnsFailures = 16;
590
[email protected]0f292de02012-02-01 22:28:20591// Holds the data for a request that could not be completed synchronously.
592// It is owned by a Job. Canceled Requests are only marked as canceled rather
593// than removed from the Job's |requests_| list.
maksim.sisov31452af2016-07-27 06:38:10594class HostResolverImpl::RequestImpl : public HostResolver::Request {
[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.
maksim.sisov31452af2016-07-27 06:38:101260 if (!requests_.empty()) {
1261 // Log any remaining Requests as cancelled.
1262 for (RequestImpl* req : requests_) {
1263 DCHECK_EQ(this, req->job());
maksim.sisov31452af2016-07-27 06:38:101264 LogCancelRequest(req->source_net_log(), req->info());
maksim.sisov320d5962016-08-15 18:02:511265 req->OnJobCancelled(this);
maksim.sisov31452af2016-07-27 06:38:101266 }
1267 requests_.clear();
[email protected]b3601bc22012-02-21 21:23:201268 }
[email protected]68ad3ee2010-01-30 03:45:391269 }
1270
[email protected]daae1322013-09-05 18:26:501271 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1272 // of the queue.
1273 void Schedule(bool at_head) {
1274 DCHECK(!is_queued());
1275 PrioritizedDispatcher::Handle handle;
1276 if (!at_head) {
[email protected]106ccd2c2014-06-17 09:21:001277 handle = resolver_->dispatcher_->Add(this, priority());
[email protected]daae1322013-09-05 18:26:501278 } else {
[email protected]106ccd2c2014-06-17 09:21:001279 handle = resolver_->dispatcher_->AddAtHead(this, priority());
[email protected]daae1322013-09-05 18:26:501280 }
1281 // The dispatcher could have started |this| in the above call to Add, which
1282 // could have called Schedule again. In that case |handle| will be null,
1283 // but |handle_| may have been set by the other nested call to Schedule.
1284 if (!handle.is_null()) {
1285 DCHECK(handle_.is_null());
1286 handle_ = handle;
1287 }
[email protected]16ee26d2012-03-08 03:34:351288 }
1289
maksim.sisov31452af2016-07-27 06:38:101290 void AddRequest(RequestImpl* request) {
1291 DCHECK_EQ(key_.hostname, request->info().hostname());
[email protected]0f292de02012-02-01 22:28:201292
maksim.sisov31452af2016-07-27 06:38:101293 priority_tracker_.Add(request->priority());
[email protected]0f292de02012-02-01 22:28:201294
maksim.sisov31452af2016-07-27 06:38:101295 request->source_net_log().AddEvent(
mikecirone8b85c432016-09-08 19:11:001296 NetLogEventType::HOST_RESOLVER_IMPL_JOB_ATTACH,
[email protected]cd565142012-06-12 16:21:451297 net_log_.source().ToEventParametersCallback());
[email protected]0f292de02012-02-01 22:28:201298
1299 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001300 NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH,
maksim.sisov31452af2016-07-27 06:38:101301 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(),
[email protected]cd565142012-06-12 16:21:451302 priority()));
[email protected]0f292de02012-02-01 22:28:201303
Miriam Gershenson61604662017-09-28 23:51:111304 if (!request->info().is_speculative())
[email protected]0f292de02012-02-01 22:28:201305 had_non_speculative_request_ = true;
[email protected]b3601bc22012-02-21 21:23:201306
maksim.sisov31452af2016-07-27 06:38:101307 requests_.push_back(request);
[email protected]b3601bc22012-02-21 21:23:201308
[email protected]51b9a6b2012-06-25 21:50:291309 UpdatePriority();
[email protected]68ad3ee2010-01-30 03:45:391310 }
1311
maksim.sisov31452af2016-07-27 06:38:101312 void ChangeRequestPriority(RequestImpl* req, RequestPriority priority) {
juliatuttlec53b19a72016-05-05 13:51:311313 DCHECK_EQ(key_.hostname, req->info().hostname());
juliatuttlec53b19a72016-05-05 13:51:311314
1315 priority_tracker_.Remove(req->priority());
1316 req->set_priority(priority);
1317 priority_tracker_.Add(req->priority());
1318 UpdatePriority();
1319 }
1320
maksim.sisov31452af2016-07-27 06:38:101321 // Detach cancelled request. If it was the last active Request, also finishes
1322 // this Job.
1323 void CancelRequest(RequestImpl* request) {
1324 DCHECK_EQ(key_.hostname, request->info().hostname());
1325 DCHECK(!requests_.empty());
[email protected]16ee26d2012-03-08 03:34:351326
maksim.sisov31452af2016-07-27 06:38:101327 LogCancelRequest(request->source_net_log(), request->info());
[email protected]16ee26d2012-03-08 03:34:351328
maksim.sisov31452af2016-07-27 06:38:101329 priority_tracker_.Remove(request->priority());
1330 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001331 NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH,
maksim.sisov31452af2016-07-27 06:38:101332 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(),
1333 priority()));
[email protected]b3601bc22012-02-21 21:23:201334
[email protected]16ee26d2012-03-08 03:34:351335 if (num_active_requests() > 0) {
[email protected]51b9a6b2012-06-25 21:50:291336 UpdatePriority();
maksim.sisov31452af2016-07-27 06:38:101337 RemoveRequest(request);
[email protected]16ee26d2012-03-08 03:34:351338 } else {
1339 // If we were called from a Request's callback within CompleteRequests,
1340 // that Request could not have been cancelled, so num_active_requests()
1341 // could not be 0. Therefore, we are not in CompleteRequests().
[email protected]1339a2a22012-10-17 08:39:431342 CompleteRequestsWithError(OK /* cancelled */);
[email protected]b3601bc22012-02-21 21:23:201343 }
[email protected]68ad3ee2010-01-30 03:45:391344 }
1345
maksim.sisov31452af2016-07-27 06:38:101346 void RemoveRequest(RequestImpl* request) {
1347 auto it = std::find(requests_.begin(), requests_.end(), request);
1348 DCHECK(it != requests_.end());
1349 requests_.erase(it);
1350 }
1351
[email protected]7af985a2012-12-14 22:40:421352 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1353 // the job. This currently assumes the abort is due to a network change.
olli.raulaa21e9eb72015-12-17 08:18:111354 // TODO This should not delete |this|.
[email protected]0f292de02012-02-01 22:28:201355 void Abort() {
[email protected]0f292de02012-02-01 22:28:201356 DCHECK(is_running());
[email protected]7af985a2012-12-14 22:40:421357 CompleteRequestsWithError(ERR_NETWORK_CHANGED);
[email protected]b3601bc22012-02-21 21:23:201358 }
1359
[email protected]f0f602bd2012-11-15 18:01:021360 // If DnsTask present, abort it and fall back to ProcTask.
1361 void AbortDnsTask() {
1362 if (dns_task_) {
[email protected]daae1322013-09-05 18:26:501363 KillDnsTask();
[email protected]f0f602bd2012-11-15 18:01:021364 dns_task_error_ = OK;
1365 StartProcTask();
1366 }
1367 }
1368
[email protected]16ee26d2012-03-08 03:34:351369 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1370 // Completes all requests and destroys the job.
1371 void OnEvicted() {
1372 DCHECK(!is_running());
1373 DCHECK(is_queued());
1374 handle_.Reset();
1375
mikecirone8b85c432016-09-08 19:11:001376 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_EVICTED);
[email protected]16ee26d2012-03-08 03:34:351377
1378 // This signals to CompleteRequests that this job never ran.
[email protected]1339a2a22012-10-17 08:39:431379 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
[email protected]16ee26d2012-03-08 03:34:351380 }
1381
[email protected]78eac2a2012-03-14 19:09:271382 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1383 // this Job was destroyed.
1384 bool ServeFromHosts() {
1385 DCHECK_GT(num_active_requests(), 0u);
1386 AddressList addr_list;
1387 if (resolver_->ServeFromHosts(key(),
[email protected]3cb676a12012-06-30 15:46:031388 requests_.front()->info(),
[email protected]78eac2a2012-03-14 19:09:271389 &addr_list)) {
1390 // This will destroy the Job.
Rob Percival94f21ad2017-11-14 10:20:241391 CompleteRequests(
1392 MakeCacheEntry(OK, addr_list, HostCache::Entry::SOURCE_HOSTS),
1393 base::TimeDelta());
[email protected]78eac2a2012-03-14 19:09:271394 return true;
1395 }
1396 return false;
1397 }
1398
tanay.c6ddd5f6f2015-08-25 06:34:571399 const Key& key() const { return key_; }
[email protected]b4481b222012-03-16 17:13:111400
1401 bool is_queued() const {
1402 return !handle_.is_null();
1403 }
1404
1405 bool is_running() const {
1406 return is_dns_running() || is_proc_running();
1407 }
1408
[email protected]16ee26d2012-03-08 03:34:351409 private:
[email protected]daae1322013-09-05 18:26:501410 void KillDnsTask() {
1411 if (dns_task_) {
1412 ReduceToOneJobSlot();
1413 dns_task_.reset();
1414 }
1415 }
1416
1417 // Reduce the number of job slots occupied and queued in the dispatcher
1418 // to one. If the second Job slot is queued in the dispatcher, cancels the
1419 // queued job. Otherwise, the second Job has been started by the
1420 // PrioritizedDispatcher, so signals it is complete.
1421 void ReduceToOneJobSlot() {
1422 DCHECK_GE(num_occupied_job_slots_, 1u);
1423 if (is_queued()) {
[email protected]106ccd2c2014-06-17 09:21:001424 resolver_->dispatcher_->Cancel(handle_);
[email protected]daae1322013-09-05 18:26:501425 handle_.Reset();
1426 } else if (num_occupied_job_slots_ > 1) {
[email protected]106ccd2c2014-06-17 09:21:001427 resolver_->dispatcher_->OnJobFinished();
[email protected]daae1322013-09-05 18:26:501428 --num_occupied_job_slots_;
1429 }
1430 DCHECK_EQ(1u, num_occupied_job_slots_);
1431 }
1432
eroman0cd87b62016-12-14 19:13:451433 // MakeCacheEntry() and MakeCacheEntryWithTTL() are helpers to build a
1434 // HostCache::Entry(). The address list is omited from the cache entry
1435 // for errors.
1436 HostCache::Entry MakeCacheEntry(int net_error,
Rob Percival94f21ad2017-11-14 10:20:241437 const AddressList& addr_list,
1438 HostCache::Entry::Source source) const {
eroman0cd87b62016-12-14 19:13:451439 return HostCache::Entry(
1440 net_error,
Rob Percival94f21ad2017-11-14 10:20:241441 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(),
1442 source);
eroman0cd87b62016-12-14 19:13:451443 }
1444
1445 HostCache::Entry MakeCacheEntryWithTTL(int net_error,
1446 const AddressList& addr_list,
Rob Percival94f21ad2017-11-14 10:20:241447 HostCache::Entry::Source source,
eroman0cd87b62016-12-14 19:13:451448 base::TimeDelta ttl) const {
1449 return HostCache::Entry(
1450 net_error,
1451 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(),
Rob Percival94f21ad2017-11-14 10:20:241452 source, ttl);
eroman0cd87b62016-12-14 19:13:451453 }
1454
juliatuttlec53b19a72016-05-05 13:51:311455 AddressList MakeAddressListForRequest(const AddressList& list) const {
1456 if (requests_.empty())
1457 return list;
1458 return AddressList::CopyWithPort(list, requests_.front()->info().port());
1459 }
1460
[email protected]51b9a6b2012-06-25 21:50:291461 void UpdatePriority() {
1462 if (is_queued()) {
1463 if (priority() != static_cast<RequestPriority>(handle_.priority()))
1464 priority_change_time_ = base::TimeTicks::Now();
[email protected]106ccd2c2014-06-17 09:21:001465 handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
[email protected]51b9a6b2012-06-25 21:50:291466 }
1467 }
1468
[email protected]16ee26d2012-03-08 03:34:351469 // PriorityDispatch::Job:
dchengb03027d2014-10-21 12:00:201470 void Start() override {
[email protected]daae1322013-09-05 18:26:501471 DCHECK_LE(num_occupied_job_slots_, 1u);
1472
[email protected]70c04ab2013-08-22 16:05:121473 handle_.Reset();
[email protected]daae1322013-09-05 18:26:501474 ++num_occupied_job_slots_;
1475
1476 if (num_occupied_job_slots_ == 2) {
1477 StartSecondDnsTransaction();
1478 return;
1479 }
1480
1481 DCHECK(!is_running());
[email protected]0f292de02012-02-01 22:28:201482
mikecirone8b85c432016-09-08 19:11:001483 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_STARTED);
[email protected]0f292de02012-02-01 22:28:201484
[email protected]51b9a6b2012-06-25 21:50:291485 had_dns_config_ = resolver_->HaveDnsConfig();
1486
Miriam Gershenson61604662017-09-28 23:51:111487 start_time_ = base::TimeTicks::Now();
1488 base::TimeDelta queue_time = start_time_ - creation_time_;
1489 base::TimeDelta queue_time_after_change =
1490 start_time_ - priority_change_time_;
[email protected]51b9a6b2012-06-25 21:50:291491
Miriam Gershenson90d05e02017-09-28 19:29:281492 DNS_HISTOGRAM_BY_PRIORITY("Net.DNS.JobQueueTime", priority(), queue_time);
1493 DNS_HISTOGRAM_BY_PRIORITY("Net.DNS.JobQueueTimeAfterChange", priority(),
1494 queue_time_after_change);
[email protected]51b9a6b2012-06-25 21:50:291495
[email protected]443714fad2013-09-19 04:52:011496 bool system_only =
1497 (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0;
1498
[email protected]1d932852012-06-19 19:40:331499 // Caution: Job::Start must not complete synchronously.
[email protected]443714fad2013-09-19 04:52:011500 if (!system_only && had_dns_config_ &&
1501 !ResemblesMulticastDNSName(key_.hostname)) {
[email protected]b3601bc22012-02-21 21:23:201502 StartDnsTask();
1503 } else {
1504 StartProcTask();
1505 }
1506 }
1507
[email protected]b3601bc22012-02-21 21:23:201508 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
Francois Doraya2d01ba2017-09-25 19:17:401509 // the limits on |dispatcher_|. But in order to keep the number of
1510 // TaskScheduler threads low, we will need to use an "inner"
1511 // PrioritizedDispatcher with tighter limits.
[email protected]b3601bc22012-02-21 21:23:201512 void StartProcTask() {
[email protected]16ee26d2012-03-08 03:34:351513 DCHECK(!is_dns_running());
mmenke91c17162016-06-02 16:03:231514 proc_task_ =
1515 new ProcTask(key_, resolver_->proc_params_,
1516 base::Bind(&Job::OnProcTaskComplete,
1517 base::Unretained(this), base::TimeTicks::Now()),
Miriam Gershensone42adb22017-10-16 16:19:381518 proc_task_runner_, net_log_);
[email protected]0f292de02012-02-01 22:28:201519
[email protected]0f292de02012-02-01 22:28:201520 // Start() could be called from within Resolve(), hence it must NOT directly
1521 // call OnProcTaskComplete, for example, on synchronous failure.
1522 proc_task_->Start();
[email protected]68ad3ee2010-01-30 03:45:391523 }
1524
[email protected]0f292de02012-02-01 22:28:201525 // Called by ProcTask when it completes.
[email protected]e3bd4822012-10-23 18:01:371526 void OnProcTaskComplete(base::TimeTicks start_time,
1527 int net_error,
1528 const AddressList& addr_list) {
[email protected]b3601bc22012-02-21 21:23:201529 DCHECK(is_proc_running());
[email protected]68ad3ee2010-01-30 03:45:391530
[email protected]1d932852012-06-19 19:40:331531 if (dns_task_error_ != OK) {
[email protected]e3bd4822012-10-23 18:01:371532 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
[email protected]1def74c2012-03-22 20:07:001533 if (net_error == OK) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211534 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.FallbackSuccess", duration);
[email protected]1d932852012-06-19 19:40:331535 if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) &&
1536 ResemblesNetBIOSName(key_.hostname)) {
1537 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS);
1538 } else {
1539 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS);
1540 }
Ilya Sherman0eb39802017-12-08 20:58:181541 base::UmaHistogramSparse("Net.DNS.DnsTask.Errors",
1542 std::abs(dns_task_error_));
[email protected]1ffdda82012-12-12 23:04:221543 resolver_->OnDnsTaskResolve(dns_task_error_);
[email protected]1def74c2012-03-22 20:07:001544 } else {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211545 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.FallbackFail", duration);
[email protected]1def74c2012-03-22 20:07:001546 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
1547 }
1548 }
1549
eroman1efc237c2016-12-14 00:00:451550 if (ContainsIcannNameCollisionIp(addr_list))
1551 net_error = ERR_ICANN_NAME_COLLISION;
1552
[email protected]1339a2a22012-10-17 08:39:431553 base::TimeDelta ttl =
1554 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds);
[email protected]b3601bc22012-02-21 21:23:201555 if (net_error == OK)
1556 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds);
[email protected]68ad3ee2010-01-30 03:45:391557
Rob Percival94f21ad2017-11-14 10:20:241558 // Source unknown because the system resolver could have gotten it from a
1559 // hosts file, its own cache, a DNS lookup or somewhere else.
[email protected]895123222012-10-25 15:21:171560 // Don't store the |ttl| in cache since it's not obtained from the server.
Rob Percival94f21ad2017-11-14 10:20:241561 CompleteRequests(
1562 MakeCacheEntry(net_error, addr_list, HostCache::Entry::SOURCE_UNKNOWN),
1563 ttl);
[email protected]b3601bc22012-02-21 21:23:201564 }
1565
1566 void StartDnsTask() {
[email protected]78eac2a2012-03-14 19:09:271567 DCHECK(resolver_->HaveDnsConfig());
[email protected]daae1322013-09-05 18:26:501568 dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this,
1569 net_log_));
[email protected]b3601bc22012-02-21 21:23:201570
[email protected]daae1322013-09-05 18:26:501571 dns_task_->StartFirstTransaction();
1572 // Schedule a second transaction, if needed.
1573 if (dns_task_->needs_two_transactions())
1574 Schedule(true);
1575 }
1576
1577 void StartSecondDnsTransaction() {
1578 DCHECK(dns_task_->needs_two_transactions());
1579 dns_task_->StartSecondTransaction();
[email protected]16c2bd72013-06-28 01:19:221580 }
1581
1582 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
1583 // deleted before this callback. In this case dns_task is deleted as well,
1584 // so we use it as indicator whether Job is still valid.
1585 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
1586 base::TimeDelta duration,
1587 int net_error) {
Miriam Gershenson61604662017-09-28 23:51:111588 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.FailureTime", duration);
[email protected]16c2bd72013-06-28 01:19:221589
wezb9820d42016-06-22 23:41:041590 if (!dns_task)
[email protected]16c2bd72013-06-28 01:19:221591 return;
1592
Miriam Gershenson19faef812018-02-07 23:56:441593 if (duration < base::TimeDelta::FromMilliseconds(10)) {
1594 base::UmaHistogramSparse("Net.DNS.DnsTask.ErrorBeforeFallback.Fast",
1595 std::abs(net_error));
1596 } else {
1597 base::UmaHistogramSparse("Net.DNS.DnsTask.ErrorBeforeFallback.Slow",
1598 std::abs(net_error));
1599 }
[email protected]16c2bd72013-06-28 01:19:221600 dns_task_error_ = net_error;
1601
1602 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so.
1603 // http://crbug.com/117655
1604
1605 // TODO(szym): Some net errors indicate lack of connectivity. Starting
1606 // ProcTask in that case is a waste of time.
1607 if (resolver_->fallback_to_proctask_) {
[email protected]daae1322013-09-05 18:26:501608 KillDnsTask();
[email protected]16c2bd72013-06-28 01:19:221609 StartProcTask();
1610 } else {
1611 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
Miriam Gershenson66024d72017-12-05 04:30:321612 CompleteRequestsWithError(net_error);
[email protected]b3601bc22012-02-21 21:23:201613 }
1614 }
1615
[email protected]daae1322013-09-05 18:26:501616
1617 // HostResolverImpl::DnsTask::Delegate implementation:
1618
dchengb03027d2014-10-21 12:00:201619 void OnDnsTaskComplete(base::TimeTicks start_time,
1620 int net_error,
1621 const AddressList& addr_list,
1622 base::TimeDelta ttl) override {
[email protected]b3601bc22012-02-21 21:23:201623 DCHECK(is_dns_running());
[email protected]b3601bc22012-02-21 21:23:201624
[email protected]e3bd4822012-10-23 18:01:371625 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
[email protected]b3601bc22012-02-21 21:23:201626 if (net_error != OK) {
[email protected]16c2bd72013-06-28 01:19:221627 OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error);
[email protected]b3601bc22012-02-21 21:23:201628 return;
1629 }
Miriam Gershenson61604662017-09-28 23:51:111630
1631 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.SuccessTime", duration);
[email protected]b3601bc22012-02-21 21:23:201632
[email protected]1def74c2012-03-22 20:07:001633 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS);
[email protected]1339a2a22012-10-17 08:39:431634 RecordTTL(ttl);
[email protected]0adcb2b2012-08-15 21:30:461635
[email protected]1ffdda82012-12-12 23:04:221636 resolver_->OnDnsTaskResolve(OK);
[email protected]f0f602bd2012-11-15 18:01:021637
[email protected]895123222012-10-25 15:21:171638 base::TimeDelta bounded_ttl =
1639 std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds));
1640
eroman0cd87b62016-12-14 19:13:451641 if (ContainsIcannNameCollisionIp(addr_list)) {
1642 CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION);
1643 } else {
Rob Percival94f21ad2017-11-14 10:20:241644 CompleteRequests(MakeCacheEntryWithTTL(net_error, addr_list,
1645 HostCache::Entry::SOURCE_DNS, ttl),
eroman0cd87b62016-12-14 19:13:451646 bounded_ttl);
1647 }
[email protected]b3601bc22012-02-21 21:23:201648 }
1649
dchengb03027d2014-10-21 12:00:201650 void OnFirstDnsTransactionComplete() override {
[email protected]daae1322013-09-05 18:26:501651 DCHECK(dns_task_->needs_two_transactions());
1652 DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued());
1653 // No longer need to occupy two dispatcher slots.
1654 ReduceToOneJobSlot();
1655
1656 // We already have a job slot at the dispatcher, so if the second
1657 // transaction hasn't started, reuse it now instead of waiting in the queue
1658 // for the second slot.
1659 if (dns_task_->needs_another_transaction())
1660 dns_task_->StartSecondTransaction();
1661 }
1662
Brad Lassey786929ad2018-02-21 20:54:271663 URLRequestContext* url_request_context() override {
1664 return resolver_->url_request_context_;
1665 }
1666
Miriam Gershenson61604662017-09-28 23:51:111667 void RecordJobHistograms(int error) {
Miriam Gershensonc282ab42017-11-20 23:19:581668 // Used in UMA_HISTOGRAM_ENUMERATION. Do not renumber entries or reuse
1669 // deprecated values.
1670 enum Category {
1671 RESOLVE_SUCCESS = 0,
1672 RESOLVE_FAIL = 1,
1673 RESOLVE_SPECULATIVE_SUCCESS = 2,
1674 RESOLVE_SPECULATIVE_FAIL = 3,
1675 RESOLVE_ABORT = 4,
1676 RESOLVE_SPECULATIVE_ABORT = 5,
Miriam Gershenson61604662017-09-28 23:51:111677 RESOLVE_MAX, // Bounding value.
1678 };
1679 Category category = RESOLVE_MAX; // Illegal value for later DCHECK only.
1680
1681 base::TimeDelta duration = base::TimeTicks::Now() - start_time_;
1682 if (error == OK) {
1683 if (had_non_speculative_request_) {
1684 category = RESOLVE_SUCCESS;
1685 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime", duration);
1686 switch (key_.address_family) {
1687 case ADDRESS_FAMILY_IPV4:
1688 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.IPV4",
1689 duration);
1690 break;
1691 case ADDRESS_FAMILY_IPV6:
1692 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.IPV6",
1693 duration);
1694 break;
1695 case ADDRESS_FAMILY_UNSPECIFIED:
1696 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.UNSPEC",
1697 duration);
1698 break;
1699 }
1700 } else {
1701 category = RESOLVE_SPECULATIVE_SUCCESS;
Miriam Gershenson61604662017-09-28 23:51:111702 }
Miriam Gershensonc282ab42017-11-20 23:19:581703 } else if (error == ERR_NETWORK_CHANGED ||
1704 error == ERR_HOST_RESOLVER_QUEUE_TOO_LARGE) {
1705 category = had_non_speculative_request_ ? RESOLVE_ABORT
1706 : RESOLVE_SPECULATIVE_ABORT;
Miriam Gershenson61604662017-09-28 23:51:111707 } else {
1708 if (had_non_speculative_request_) {
1709 category = RESOLVE_FAIL;
1710 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime", duration);
1711 switch (key_.address_family) {
1712 case ADDRESS_FAMILY_IPV4:
Miriam Gershensonc282ab42017-11-20 23:19:581713 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.IPV4",
Miriam Gershenson61604662017-09-28 23:51:111714 duration);
1715 break;
1716 case ADDRESS_FAMILY_IPV6:
Miriam Gershensonc282ab42017-11-20 23:19:581717 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.IPV6",
Miriam Gershenson61604662017-09-28 23:51:111718 duration);
1719 break;
1720 case ADDRESS_FAMILY_UNSPECIFIED:
Miriam Gershensonc282ab42017-11-20 23:19:581721 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.UNSPEC",
Miriam Gershenson61604662017-09-28 23:51:111722 duration);
1723 break;
1724 }
1725 } else {
1726 category = RESOLVE_SPECULATIVE_FAIL;
Miriam Gershenson61604662017-09-28 23:51:111727 }
1728 }
1729 DCHECK_LT(static_cast<int>(category),
1730 static_cast<int>(RESOLVE_MAX)); // Be sure it was set.
1731 UMA_HISTOGRAM_ENUMERATION("Net.DNS.ResolveCategory", category, RESOLVE_MAX);
Miriam Gershensonaaf139582017-11-27 16:06:061732
1733 if (category == RESOLVE_FAIL || category == RESOLVE_ABORT) {
1734 if (duration < base::TimeDelta::FromMilliseconds(10))
Ilya Sherman0eb39802017-12-08 20:58:181735 base::UmaHistogramSparse("Net.DNS.ResolveError.Fast", std::abs(error));
Miriam Gershensonaaf139582017-11-27 16:06:061736 else
Ilya Sherman0eb39802017-12-08 20:58:181737 base::UmaHistogramSparse("Net.DNS.ResolveError.Slow", std::abs(error));
Miriam Gershensonaaf139582017-11-27 16:06:061738 }
Miriam Gershenson61604662017-09-28 23:51:111739 }
1740
[email protected]16ee26d2012-03-08 03:34:351741 // Performs Job's last rites. Completes all Requests. Deletes this.
[email protected]895123222012-10-25 15:21:171742 void CompleteRequests(const HostCache::Entry& entry,
1743 base::TimeDelta ttl) {
[email protected]11fbca0b2013-06-02 23:37:211744 CHECK(resolver_.get());
[email protected]b3601bc22012-02-21 21:23:201745
[email protected]16ee26d2012-03-08 03:34:351746 // This job must be removed from resolver's |jobs_| now to make room for a
1747 // new job with the same key in case one of the OnComplete callbacks decides
1748 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1749 // is done.
danakj22f90e72016-04-16 01:55:401750 std::unique_ptr<Job> self_deleter(this);
[email protected]16ee26d2012-03-08 03:34:351751
1752 resolver_->RemoveJob(this);
1753
[email protected]16ee26d2012-03-08 03:34:351754 if (is_running()) {
[email protected]16ee26d2012-03-08 03:34:351755 if (is_proc_running()) {
[email protected]daae1322013-09-05 18:26:501756 DCHECK(!is_queued());
[email protected]16ee26d2012-03-08 03:34:351757 proc_task_->Cancel();
wezb9820d42016-06-22 23:41:041758 proc_task_ = nullptr;
[email protected]16ee26d2012-03-08 03:34:351759 }
[email protected]daae1322013-09-05 18:26:501760 KillDnsTask();
[email protected]16ee26d2012-03-08 03:34:351761
1762 // Signal dispatcher that a slot has opened.
[email protected]106ccd2c2014-06-17 09:21:001763 resolver_->dispatcher_->OnJobFinished();
[email protected]16ee26d2012-03-08 03:34:351764 } else if (is_queued()) {
[email protected]106ccd2c2014-06-17 09:21:001765 resolver_->dispatcher_->Cancel(handle_);
[email protected]16ee26d2012-03-08 03:34:351766 handle_.Reset();
1767 }
1768
1769 if (num_active_requests() == 0) {
mikecirone8b85c432016-09-08 19:11:001770 net_log_.AddEvent(NetLogEventType::CANCELLED);
1771 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
[email protected]16ee26d2012-03-08 03:34:351772 OK);
1773 return;
1774 }
[email protected]b3601bc22012-02-21 21:23:201775
mikecirone8b85c432016-09-08 19:11:001776 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
juliatuttle317860e2016-05-12 14:47:221777 entry.error());
[email protected]68ad3ee2010-01-30 03:45:391778
juliatuttled7fa5852016-07-29 13:16:571779 resolver_->SchedulePersist();
1780
[email protected]78eac2a2012-03-14 19:09:271781 DCHECK(!requests_.empty());
1782
eroman1efc237c2016-12-14 00:00:451783 if (entry.error() == OK || entry.error() == ERR_ICANN_NAME_COLLISION) {
[email protected]d7b9a2b2012-05-31 22:31:191784 // Record this histogram here, when we know the system has a valid DNS
1785 // configuration.
[email protected]539df6c2012-06-19 21:21:291786 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1787 resolver_->received_dns_config_);
[email protected]d7b9a2b2012-05-31 22:31:191788 }
[email protected]16ee26d2012-03-08 03:34:351789
juliatuttle317860e2016-05-12 14:47:221790 bool did_complete = (entry.error() != ERR_NETWORK_CHANGED) &&
1791 (entry.error() != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
Miriam Gershensonc282ab42017-11-20 23:19:581792 if (did_complete)
[email protected]1339a2a22012-10-17 08:39:431793 resolver_->CacheResult(key_, entry, ttl);
Miriam Gershensonc282ab42017-11-20 23:19:581794
1795 RecordJobHistograms(entry.error());
[email protected]16ee26d2012-03-08 03:34:351796
maksim.sisov31452af2016-07-27 06:38:101797 // Complete all of the requests that were attached to the job and
1798 // detach them.
1799 while (!requests_.empty()) {
1800 RequestImpl* req = requests_.front();
1801 requests_.pop_front();
[email protected]0f292de02012-02-01 22:28:201802 DCHECK_EQ(this, req->job());
1803 // Update the net log and notify registered observers.
juliatuttle317860e2016-05-12 14:47:221804 LogFinishRequest(req->source_net_log(), req->info(), entry.error());
[email protected]51b9a6b2012-06-25 21:50:291805 if (did_complete) {
1806 // Record effective total time from creation to completion.
Miriam Gershenson90d05e02017-09-28 19:29:281807 RecordTotalTime(req->info().is_speculative(), false,
[email protected]51b9a6b2012-06-25 21:50:291808 base::TimeTicks::Now() - req->request_time());
1809 }
maksim.sisov31452af2016-07-27 06:38:101810 req->OnJobCompleted(this, entry.error(), entry.addresses());
[email protected]0f292de02012-02-01 22:28:201811
1812 // Check if the resolver was destroyed as a result of running the
1813 // callback. If it was, we could continue, but we choose to bail.
[email protected]11fbca0b2013-06-02 23:37:211814 if (!resolver_.get())
[email protected]0f292de02012-02-01 22:28:201815 return;
1816 }
1817 }
1818
[email protected]1339a2a22012-10-17 08:39:431819 // Convenience wrapper for CompleteRequests in case of failure.
1820 void CompleteRequestsWithError(int net_error) {
Rob Percival94f21ad2017-11-14 10:20:241821 CompleteRequests(HostCache::Entry(net_error, AddressList(),
1822 HostCache::Entry::SOURCE_UNKNOWN),
[email protected]895123222012-10-25 15:21:171823 base::TimeDelta());
[email protected]1339a2a22012-10-17 08:39:431824 }
1825
Brad Lassey786929ad2018-02-21 20:54:271826 RequestPriority priority() const override {
[email protected]b4481b222012-03-16 17:13:111827 return priority_tracker_.highest_priority();
1828 }
1829
1830 // Number of non-canceled requests in |requests_|.
1831 size_t num_active_requests() const {
1832 return priority_tracker_.total_count();
1833 }
1834
wezb9820d42016-06-22 23:41:041835 bool is_dns_running() const { return !!dns_task_; }
[email protected]b4481b222012-03-16 17:13:111836
wezb9820d42016-06-22 23:41:041837 bool is_proc_running() const { return !!proc_task_; }
[email protected]b4481b222012-03-16 17:13:111838
[email protected]0f292de02012-02-01 22:28:201839 base::WeakPtr<HostResolverImpl> resolver_;
1840
1841 Key key_;
1842
1843 // Tracks the highest priority across |requests_|.
1844 PriorityTracker priority_tracker_;
1845
Miriam Gershensone42adb22017-10-16 16:19:381846 // Task runner used for HostResolverProc.
1847 scoped_refptr<base::TaskRunner> proc_task_runner_;
1848
[email protected]0f292de02012-02-01 22:28:201849 bool had_non_speculative_request_;
1850
[email protected]51b9a6b2012-06-25 21:50:291851 // Distinguishes measurements taken while DnsClient was fully configured.
1852 bool had_dns_config_;
1853
[email protected]daae1322013-09-05 18:26:501854 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1855 unsigned num_occupied_job_slots_;
1856
[email protected]1d932852012-06-19 19:40:331857 // Result of DnsTask.
1858 int dns_task_error_;
[email protected]1def74c2012-03-22 20:07:001859
[email protected]51b9a6b2012-06-25 21:50:291860 const base::TimeTicks creation_time_;
1861 base::TimeTicks priority_change_time_;
Miriam Gershenson61604662017-09-28 23:51:111862 base::TimeTicks start_time_;
[email protected]51b9a6b2012-06-25 21:50:291863
tfarina428341112016-09-22 13:38:201864 NetLogWithSource net_log_;
[email protected]0f292de02012-02-01 22:28:201865
[email protected]b3601bc22012-02-21 21:23:201866 // Resolves the host using a HostResolverProc.
[email protected]0f292de02012-02-01 22:28:201867 scoped_refptr<ProcTask> proc_task_;
1868
[email protected]b3601bc22012-02-21 21:23:201869 // Resolves the host using a DnsTransaction.
danakj22f90e72016-04-16 01:55:401870 std::unique_ptr<DnsTask> dns_task_;
[email protected]b3601bc22012-02-21 21:23:201871
[email protected]0f292de02012-02-01 22:28:201872 // All Requests waiting for the result of this Job. Some can be canceled.
Brett Wilsonc6a0c822017-09-12 00:04:291873 base::circular_deque<RequestImpl*> requests_;
[email protected]0f292de02012-02-01 22:28:201874
[email protected]16ee26d2012-03-08 03:34:351875 // A handle used in |HostResolverImpl::dispatcher_|.
[email protected]0f292de02012-02-01 22:28:201876 PrioritizedDispatcher::Handle handle_;
[email protected]68ad3ee2010-01-30 03:45:391877};
1878
1879//-----------------------------------------------------------------------------
1880
[email protected]0f292de02012-02-01 22:28:201881HostResolverImpl::ProcTaskParams::ProcTaskParams(
[email protected]e95d3aca2010-01-11 22:47:431882 HostResolverProc* resolver_proc,
[email protected]0f292de02012-02-01 22:28:201883 size_t max_retry_attempts)
1884 : resolver_proc(resolver_proc),
1885 max_retry_attempts(max_retry_attempts),
ttuttlecf1158bf2016-03-18 16:37:441886 unresponsive_delay(
1887 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs)),
[email protected]0f292de02012-02-01 22:28:201888 retry_factor(2) {
[email protected]106ccd2c2014-06-17 09:21:001889 // Maximum of 4 retry attempts for host resolution.
1890 static const size_t kDefaultMaxRetryAttempts = 4u;
1891 if (max_retry_attempts == HostResolver::kDefaultRetryAttempts)
1892 max_retry_attempts = kDefaultMaxRetryAttempts;
[email protected]0f292de02012-02-01 22:28:201893}
1894
vmpstracd23b72016-02-26 21:08:551895HostResolverImpl::ProcTaskParams::ProcTaskParams(const ProcTaskParams& other) =
1896 default;
1897
Chris Watkins68b15032017-12-01 03:07:131898HostResolverImpl::ProcTaskParams::~ProcTaskParams() = default;
[email protected]0f292de02012-02-01 22:28:201899
[email protected]b59ff372009-07-15 22:04:321900HostResolverImpl::~HostResolverImpl() {
gab47aa7da2017-06-02 16:09:431901 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]daae1322013-09-05 18:26:501902 // Prevent the dispatcher from starting new jobs.
[email protected]106ccd2c2014-06-17 09:21:001903 dispatcher_->SetLimitsToZero();
avi9e72eb482016-12-09 21:10:541904 // It's now safe for Jobs to call KillDnsTask on destruction, because
[email protected]daae1322013-09-05 18:26:501905 // OnJobComplete will not start any new jobs.
avi9e72eb482016-12-09 21:10:541906 jobs_.clear();
[email protected]e95d3aca2010-01-11 22:47:431907
[email protected]232a5812011-03-04 22:42:081908 NetworkChangeNotifier::RemoveIPAddressObserver(this);
ttuttlecf1158bf2016-03-18 16:37:441909 NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
[email protected]bb0e34542012-08-31 19:52:401910 NetworkChangeNotifier::RemoveDNSObserver(this);
[email protected]b59ff372009-07-15 22:04:321911}
1912
[email protected]0f292de02012-02-01 22:28:201913void HostResolverImpl::SetMaxQueuedJobs(size_t value) {
[email protected]106ccd2c2014-06-17 09:21:001914 DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
[email protected]0f292de02012-02-01 22:28:201915 DCHECK_GT(value, 0u);
1916 max_queued_jobs_ = value;
[email protected]be1a48b2011-01-20 00:12:131917}
1918
[email protected]684970b2009-08-14 04:54:461919int HostResolverImpl::Resolve(const RequestInfo& info,
[email protected]5109c1952013-08-20 18:44:101920 RequestPriority priority,
[email protected]b59ff372009-07-15 22:04:321921 AddressList* addresses,
[email protected]aa22b242011-11-16 18:58:291922 const CompletionCallback& callback,
maksim.sisov31452af2016-07-27 06:38:101923 std::unique_ptr<Request>* out_req,
tfarina428341112016-09-22 13:38:201924 const NetLogWithSource& source_net_log) {
[email protected]95a214c2011-08-04 21:50:401925 DCHECK(addresses);
gab47aa7da2017-06-02 16:09:431926 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]aa22b242011-11-16 18:58:291927 DCHECK_EQ(false, callback.is_null());
maksim.sisov31452af2016-07-27 06:38:101928 DCHECK(out_req);
[email protected]1ac6af92010-06-03 21:00:141929
Chris Palmer9cbf4212017-07-19 01:54:551930 LogStartRequest(source_net_log, info);
pauljensen19eb73422015-04-15 23:20:061931
Matt Menke23b4bbf82017-07-20 19:11:291932 Key key;
1933 int rv = ResolveHelper(info, false, nullptr, source_net_log, addresses, &key);
[email protected]95a214c2011-08-04 21:50:401934 if (rv != ERR_DNS_CACHE_MISS) {
xunjieli26f90452014-11-10 16:23:021935 LogFinishRequest(source_net_log, info, rv);
Miriam Gershenson90d05e02017-09-28 19:29:281936 RecordTotalTime(info.is_speculative(), true, base::TimeDelta());
[email protected]95a214c2011-08-04 21:50:401937 return rv;
[email protected]38368712011-03-02 08:09:401938 }
1939
[email protected]0f292de02012-02-01 22:28:201940 // Next we need to attach our request to a "job". This job is responsible for
1941 // calling "getaddrinfo(hostname)" on a worker thread.
1942
avi9e72eb482016-12-09 21:10:541943 auto jobit = jobs_.find(key);
[email protected]0f292de02012-02-01 22:28:201944 Job* job;
1945 if (jobit == jobs_.end()) {
Miriam Gershensone42adb22017-10-16 16:19:381946 job = new Job(weak_ptr_factory_.GetWeakPtr(), key, priority,
1947 proc_task_runner_, source_net_log);
[email protected]daae1322013-09-05 18:26:501948 job->Schedule(false);
[email protected]0f292de02012-02-01 22:28:201949
1950 // Check for queue overflow.
[email protected]106ccd2c2014-06-17 09:21:001951 if (dispatcher_->num_queued_jobs() > max_queued_jobs_) {
1952 Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest());
[email protected]0f292de02012-02-01 22:28:201953 DCHECK(evicted);
[email protected]16ee26d2012-03-08 03:34:351954 evicted->OnEvicted(); // Deletes |evicted|.
[email protected]0f292de02012-02-01 22:28:201955 if (evicted == job) {
[email protected]0f292de02012-02-01 22:28:201956 rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE;
xunjieli26f90452014-11-10 16:23:021957 LogFinishRequest(source_net_log, info, rv);
[email protected]0f292de02012-02-01 22:28:201958 return rv;
1959 }
[email protected]0f292de02012-02-01 22:28:201960 }
avi9e72eb482016-12-09 21:10:541961 jobs_[key] = base::WrapUnique(job);
[email protected]0f292de02012-02-01 22:28:201962 } else {
avi9e72eb482016-12-09 21:10:541963 job = jobit->second.get();
[email protected]0f292de02012-02-01 22:28:201964 }
1965
1966 // Can't complete synchronously. Create and attach request.
Jeremy Roman0579ed62017-08-29 15:56:191967 auto req = std::make_unique<RequestImpl>(source_net_log, info, priority,
avi9e72eb482016-12-09 21:10:541968 callback, addresses, job);
maksim.sisov31452af2016-07-27 06:38:101969 job->AddRequest(req.get());
1970 *out_req = std::move(req);
[email protected]b59ff372009-07-15 22:04:321971
[email protected]0f292de02012-02-01 22:28:201972 // Completion happens during Job::CompleteRequests().
[email protected]b59ff372009-07-15 22:04:321973 return ERR_IO_PENDING;
1974}
1975
Francois Doraya2d01ba2017-09-25 19:17:401976HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log)
mmenke91c17162016-06-02 16:03:231977 : max_queued_jobs_(0),
1978 proc_params_(NULL, options.max_retry_attempts),
1979 net_log_(net_log),
1980 received_dns_config_(false),
1981 num_dns_failures_(0),
mgershaf9a9232017-04-13 20:19:031982 assume_ipv6_failure_on_wifi_(false),
mmenke91c17162016-06-02 16:03:231983 use_local_ipv6_(false),
1984 last_ipv6_probe_result_(true),
mmenke91c17162016-06-02 16:03:231985 additional_resolver_flags_(0),
1986 fallback_to_proctask_(true),
juliatuttled7fa5852016-07-29 13:16:571987 persist_initialized_(false),
mmenke91c17162016-06-02 16:03:231988 weak_ptr_factory_(this),
1989 probe_weak_ptr_factory_(this) {
1990 if (options.enable_caching)
1991 cache_ = HostCache::CreateDefaultCache();
1992
1993 PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits();
1994 dispatcher_.reset(new PrioritizedDispatcher(job_limits));
1995 max_queued_jobs_ = job_limits.total_jobs * 100u;
1996
1997 DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
1998
Miriam Gershensone42adb22017-10-16 16:19:381999 proc_task_runner_ = base::CreateTaskRunnerWithTraits(
Maks Orlovich5393c8682018-02-26 16:17:502000 {base::MayBlock(), priority_mode.Get(),
2001 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
Miriam Gershensone42adb22017-10-16 16:19:382002
mmenke91c17162016-06-02 16:03:232003#if defined(OS_WIN)
2004 EnsureWinsockInit();
2005#endif
2006#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
2007 RunLoopbackProbeJob();
2008#endif
2009 NetworkChangeNotifier::AddIPAddressObserver(this);
2010 NetworkChangeNotifier::AddConnectionTypeObserver(this);
2011 NetworkChangeNotifier::AddDNSObserver(this);
2012#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
Sergey Ulanov49085572017-07-10 23:25:462013 !defined(OS_ANDROID) && !defined(OS_FUCHSIA)
mmenke91c17162016-06-02 16:03:232014 EnsureDnsReloaderInit();
2015#endif
2016
2017 OnConnectionTypeChanged(NetworkChangeNotifier::GetConnectionType());
2018
2019 {
2020 DnsConfig dns_config;
2021 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2022 received_dns_config_ = dns_config.IsValid();
2023 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2024 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
2025 }
2026
2027 fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
2028}
2029
2030void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) {
2031 if (result) {
2032 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
2033 } else {
2034 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
2035 }
2036}
2037
Miriam Gershensone42adb22017-10-16 16:19:382038void HostResolverImpl::SetTaskRunnerForTesting(
2039 scoped_refptr<base::TaskRunner> task_runner) {
2040 proc_task_runner_ = std::move(task_runner);
2041}
2042
Matt Menke23b4bbf82017-07-20 19:11:292043int HostResolverImpl::ResolveHelper(const RequestInfo& info,
juliatuttle9fb7aeb2016-06-06 20:16:332044 bool allow_stale,
2045 HostCache::EntryStaleness* stale_info,
Matt Menke23b4bbf82017-07-20 19:11:292046 const NetLogWithSource& source_net_log,
2047 AddressList* addresses,
2048 Key* key) {
2049 IPAddress ip_address;
2050 IPAddress* ip_address_ptr = nullptr;
2051 if (ip_address.AssignFromIPLiteral(info.hostname())) {
2052 ip_address_ptr = &ip_address;
2053 } else {
2054 // Check that the caller supplied a valid hostname to resolve.
2055 if (!IsValidDNSDomain(info.hostname()))
2056 return ERR_NAME_NOT_RESOLVED;
2057 }
2058
2059 // Build a key that identifies the request in the cache and in the
2060 // outstanding jobs map.
2061 *key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log);
2062
juliatuttle9fb7aeb2016-06-06 20:16:332063 DCHECK(allow_stale == !!stale_info);
[email protected]95a214c2011-08-04 21:50:402064 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
2065 // On Windows it gives the default interface's address, whereas on Linux it
2066 // gives an error. We will make it fail on all platforms for consistency.
juliatuttle9fb7aeb2016-06-06 20:16:332067 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) {
2068 MakeNotStale(stale_info);
[email protected]95a214c2011-08-04 21:50:402069 return ERR_NAME_NOT_RESOLVED;
juliatuttle9fb7aeb2016-06-06 20:16:332070 }
[email protected]95a214c2011-08-04 21:50:402071
2072 int net_error = ERR_UNEXPECTED;
Matt Menke23b4bbf82017-07-20 19:11:292073 if (ResolveAsIP(*key, info, ip_address_ptr, &net_error, addresses)) {
juliatuttle9fb7aeb2016-06-06 20:16:332074 MakeNotStale(stale_info);
[email protected]95a214c2011-08-04 21:50:402075 return net_error;
juliatuttle9fb7aeb2016-06-06 20:16:332076 }
Mike Westda1c690e2017-08-12 05:57:162077
2078 // Special-case localhost names, as per the recommendations in
2079 // https://tools.ietf.org/html/draft-west-let-localhost-be-localhost.
2080 if (ServeLocalhost(*key, info, addresses)) {
2081 MakeNotStale(stale_info);
2082 return OK;
2083 }
2084
Matt Menke23b4bbf82017-07-20 19:11:292085 if (ServeFromCache(*key, info, &net_error, addresses, allow_stale,
juliatuttle9fb7aeb2016-06-06 20:16:332086 stale_info)) {
mgersh33d23c02017-03-29 20:24:252087 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_CACHE_HIT,
2088 addresses->CreateNetLogCallback());
juliatuttle9fb7aeb2016-06-06 20:16:332089 // |ServeFromCache()| will set |*stale_info| as needed.
[email protected]78eac2a2012-03-14 19:09:272090 return net_error;
2091 }
Mike Westda1c690e2017-08-12 05:57:162092
[email protected]78eac2a2012-03-14 19:09:272093 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2094 // http://crbug.com/117655
Matt Menke23b4bbf82017-07-20 19:11:292095 if (ServeFromHosts(*key, info, addresses)) {
mgersh33d23c02017-03-29 20:24:252096 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_HOSTS_HIT,
2097 addresses->CreateNetLogCallback());
juliatuttle9fb7aeb2016-06-06 20:16:332098 MakeNotStale(stale_info);
[email protected]78eac2a2012-03-14 19:09:272099 return OK;
2100 }
estarkd1bc206e2015-06-20 00:44:392101
[email protected]78eac2a2012-03-14 19:09:272102 return ERR_DNS_CACHE_MISS;
[email protected]95a214c2011-08-04 21:50:402103}
2104
2105int HostResolverImpl::ResolveFromCache(const RequestInfo& info,
2106 AddressList* addresses,
tfarina428341112016-09-22 13:38:202107 const NetLogWithSource& source_net_log) {
gab47aa7da2017-06-02 16:09:432108 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]95a214c2011-08-04 21:50:402109 DCHECK(addresses);
2110
[email protected]95a214c2011-08-04 21:50:402111 // Update the net log and notify registered observers.
xunjieli26f90452014-11-10 16:23:022112 LogStartRequest(source_net_log, info);
[email protected]95a214c2011-08-04 21:50:402113
Matt Menke23b4bbf82017-07-20 19:11:292114 Key key;
2115 int rv = ResolveHelper(info, false, nullptr, source_net_log, addresses, &key);
[email protected]95a214c2011-08-04 21:50:402116
xunjieli26f90452014-11-10 16:23:022117 LogFinishRequest(source_net_log, info, rv);
[email protected]95a214c2011-08-04 21:50:402118 return rv;
2119}
2120
[email protected]a8883e452012-11-17 05:58:062121void HostResolverImpl::SetDnsClientEnabled(bool enabled) {
gab47aa7da2017-06-02 16:09:432122 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]a8883e452012-11-17 05:58:062123#if defined(ENABLE_BUILT_IN_DNS)
2124 if (enabled && !dns_client_) {
2125 SetDnsClient(DnsClient::CreateClient(net_log_));
2126 } else if (!enabled && dns_client_) {
danakj22f90e72016-04-16 01:55:402127 SetDnsClient(std::unique_ptr<DnsClient>());
[email protected]a8883e452012-11-17 05:58:062128 }
2129#endif
2130}
2131
[email protected]489d1a82011-10-12 03:09:112132HostCache* HostResolverImpl::GetHostCache() {
2133 return cache_.get();
2134}
[email protected]95a214c2011-08-04 21:50:402135
Rob Percivalbc658a22017-12-13 08:24:422136bool HostResolverImpl::HasCached(base::StringPiece hostname,
2137 HostCache::Entry::Source* source_out,
2138 HostCache::EntryStaleness* stale_out) const {
2139 if (!cache_)
2140 return false;
2141
2142 return cache_->HasEntry(hostname, source_out, stale_out);
2143}
2144
danakj22f90e72016-04-16 01:55:402145std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const {
[email protected]17e92032012-03-29 00:56:242146 // Check if async DNS is disabled.
2147 if (!dns_client_.get())
tfhef3618f2016-01-11 23:07:082148 return nullptr;
[email protected]17e92032012-03-29 00:56:242149
2150 // Check if async DNS is enabled, but we currently have no configuration
2151 // for it.
2152 const DnsConfig* dns_config = dns_client_->GetConfig();
wezb9820d42016-06-22 23:41:042153 if (!dns_config)
Jeremy Roman0579ed62017-08-29 15:56:192154 return std::make_unique<base::DictionaryValue>();
[email protected]17e92032012-03-29 00:56:242155
2156 return dns_config->ToValue();
2157}
2158
juliatuttle9fb7aeb2016-06-06 20:16:332159int HostResolverImpl::ResolveStaleFromCache(
2160 const RequestInfo& info,
2161 AddressList* addresses,
2162 HostCache::EntryStaleness* stale_info,
tfarina428341112016-09-22 13:38:202163 const NetLogWithSource& source_net_log) {
gab47aa7da2017-06-02 16:09:432164 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
juliatuttle9fb7aeb2016-06-06 20:16:332165 DCHECK(addresses);
2166 DCHECK(stale_info);
2167
2168 // Update the net log and notify registered observers.
2169 LogStartRequest(source_net_log, info);
2170
Matt Menke23b4bbf82017-07-20 19:11:292171 Key key;
2172 int rv =
2173 ResolveHelper(info, true, stale_info, source_net_log, addresses, &key);
juliatuttle9fb7aeb2016-06-06 20:16:332174 LogFinishRequest(source_net_log, info, rv);
2175 return rv;
2176}
2177
Miriam Gershenson2839ef112017-08-30 03:25:372178size_t HostResolverImpl::LastRestoredCacheSize() const {
2179 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
2180
2181 return cache_ ? cache_->last_restore_size() : 0;
2182}
2183
2184size_t HostResolverImpl::CacheSize() const {
2185 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
2186
2187 return cache_ ? cache_->size() : 0;
2188}
2189
mgershaf9a9232017-04-13 20:19:032190void HostResolverImpl::SetNoIPv6OnWifi(bool no_ipv6_on_wifi) {
gab47aa7da2017-06-02 16:09:432191 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
mgershaf9a9232017-04-13 20:19:032192 assume_ipv6_failure_on_wifi_ = no_ipv6_on_wifi;
2193}
2194
2195bool HostResolverImpl::GetNoIPv6OnWifi() {
2196 return assume_ipv6_failure_on_wifi_;
2197}
2198
Brad Lassey786929ad2018-02-21 20:54:272199void HostResolverImpl::SetRequestContext(URLRequestContext* context) {
2200 if (context != url_request_context_) {
2201 url_request_context_ = context;
2202 }
2203}
2204
2205void HostResolverImpl::ClearDnsOverHttpsServers() {
2206 if (dns_over_https_servers_.size() == 0)
2207 return;
2208
2209 dns_over_https_servers_.clear();
2210
2211 if (dns_client_.get() && dns_client_->GetConfig())
2212 UpdateDNSConfig(true);
2213}
2214
2215void HostResolverImpl::AddDnsOverHttpsServer(std::string spec, bool use_post) {
2216 GURL url(spec);
2217 if (!url.SchemeIs("https"))
2218 return;
2219
2220 dns_over_https_servers_.emplace_back(url, use_post);
2221
2222 if (dns_client_.get() && dns_client_->GetConfig())
2223 UpdateDNSConfig(true);
2224}
2225
[email protected]95a214c2011-08-04 21:50:402226bool HostResolverImpl::ResolveAsIP(const Key& key,
2227 const RequestInfo& info,
martijna23c8962016-03-04 18:18:512228 const IPAddress* ip_address,
[email protected]95a214c2011-08-04 21:50:402229 int* net_error,
2230 AddressList* addresses) {
2231 DCHECK(addresses);
2232 DCHECK(net_error);
martijna23c8962016-03-04 18:18:512233 if (ip_address == nullptr)
[email protected]95a214c2011-08-04 21:50:402234 return false;
2235
[email protected]95a214c2011-08-04 21:50:402236 *net_error = OK;
martijna23c8962016-03-04 18:18:512237 AddressFamily family = GetAddressFamily(*ip_address);
cbentzel1906f872015-06-05 16:25:252238 if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED &&
2239 key.address_family != family) {
[email protected]1c7cf3f82014-08-07 21:33:482240 // Don't return IPv6 addresses for IPv4 queries, and vice versa.
[email protected]95a214c2011-08-04 21:50:402241 *net_error = ERR_NAME_NOT_RESOLVED;
2242 } else {
martijna23c8962016-03-04 18:18:512243 *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port());
[email protected]7054e78f2012-05-07 21:44:562244 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)
2245 addresses->SetDefaultCanonicalName();
[email protected]95a214c2011-08-04 21:50:402246 }
2247 return true;
2248}
2249
2250bool HostResolverImpl::ServeFromCache(const Key& key,
2251 const RequestInfo& info,
[email protected]95a214c2011-08-04 21:50:402252 int* net_error,
juliatuttle9fb7aeb2016-06-06 20:16:332253 AddressList* addresses,
2254 bool allow_stale,
2255 HostCache::EntryStaleness* stale_info) {
[email protected]95a214c2011-08-04 21:50:402256 DCHECK(addresses);
2257 DCHECK(net_error);
juliatuttle9fb7aeb2016-06-06 20:16:332258 DCHECK(allow_stale == !!stale_info);
[email protected]95a214c2011-08-04 21:50:402259 if (!info.allow_cached_response() || !cache_.get())
2260 return false;
2261
juliatuttle9fb7aeb2016-06-06 20:16:332262 const HostCache::Entry* cache_entry;
2263 if (allow_stale)
2264 cache_entry = cache_->LookupStale(key, base::TimeTicks::Now(), stale_info);
2265 else
2266 cache_entry = cache_->Lookup(key, base::TimeTicks::Now());
[email protected]95a214c2011-08-04 21:50:402267 if (!cache_entry)
2268 return false;
2269
juliatuttle317860e2016-05-12 14:47:222270 *net_error = cache_entry->error();
[email protected]7054e78f2012-05-07 21:44:562271 if (*net_error == OK) {
[email protected]1339a2a22012-10-17 08:39:432272 if (cache_entry->has_ttl())
juliatuttle317860e2016-05-12 14:47:222273 RecordTTL(cache_entry->ttl());
2274 *addresses = EnsurePortOnAddressList(cache_entry->addresses(), info.port());
[email protected]7054e78f2012-05-07 21:44:562275 }
[email protected]95a214c2011-08-04 21:50:402276 return true;
2277}
2278
[email protected]78eac2a2012-03-14 19:09:272279bool HostResolverImpl::ServeFromHosts(const Key& key,
2280 const RequestInfo& info,
2281 AddressList* addresses) {
2282 DCHECK(addresses);
2283 if (!HaveDnsConfig())
2284 return false;
[email protected]05a79d42013-03-28 07:30:092285 addresses->clear();
2286
[email protected]cb507622012-03-23 16:17:062287 // HOSTS lookups are case-insensitive.
brettw8e2106d2015-08-11 19:30:222288 std::string hostname = base::ToLowerASCII(key.hostname);
[email protected]cb507622012-03-23 16:17:062289
[email protected]05a79d42013-03-28 07:30:092290 const DnsHosts& hosts = dns_client_->GetConfig()->hosts;
2291
[email protected]78eac2a2012-03-14 19:09:272292 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2293 // (glibc and c-ares) return the first matching line. We have more
2294 // flexibility, but lose implicit ordering.
[email protected]05a79d42013-03-28 07:30:092295 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2296 // necessary.
2297 if (key.address_family == ADDRESS_FAMILY_IPV6 ||
2298 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2299 DnsHosts::const_iterator it = hosts.find(
2300 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6));
2301 if (it != hosts.end())
2302 addresses->push_back(IPEndPoint(it->second, info.port()));
[email protected]78eac2a2012-03-14 19:09:272303 }
2304
[email protected]05a79d42013-03-28 07:30:092305 if (key.address_family == ADDRESS_FAMILY_IPV4 ||
2306 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2307 DnsHosts::const_iterator it = hosts.find(
2308 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4));
2309 if (it != hosts.end())
2310 addresses->push_back(IPEndPoint(it->second, info.port()));
2311 }
2312
[email protected]ec666ab22013-04-17 20:05:592313 // If got only loopback addresses and the family was restricted, resolve
2314 // again, without restrictions. See SystemHostResolverCall for rationale.
2315 if ((key.host_resolver_flags &
2316 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
2317 IsAllIPv4Loopback(*addresses)) {
2318 Key new_key(key);
2319 new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED;
2320 new_key.host_resolver_flags &=
2321 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2322 return ServeFromHosts(new_key, info, addresses);
2323 }
[email protected]05a79d42013-03-28 07:30:092324 return !addresses->empty();
[email protected]78eac2a2012-03-14 19:09:272325}
2326
estarkd1bc206e2015-06-20 00:44:392327bool HostResolverImpl::ServeLocalhost(const Key& key,
2328 const RequestInfo& info,
2329 AddressList* addresses) {
2330 AddressList resolved_addresses;
2331 if (!ResolveLocalHostname(key.hostname, info.port(), &resolved_addresses))
2332 return false;
2333
2334 addresses->clear();
2335
2336 for (const auto& address : resolved_addresses) {
2337 // Include the address if:
2338 // - caller didn't specify an address family, or
2339 // - caller specifically asked for the address family of this address, or
2340 // - this is an IPv6 address and caller specifically asked for IPv4 due
2341 // to lack of detected IPv6 support. (See SystemHostResolverCall for
2342 // rationale).
2343 if (key.address_family == ADDRESS_FAMILY_UNSPECIFIED ||
2344 key.address_family == address.GetFamily() ||
2345 (address.GetFamily() == ADDRESS_FAMILY_IPV6 &&
2346 key.address_family == ADDRESS_FAMILY_IPV4 &&
2347 (key.host_resolver_flags &
2348 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6))) {
2349 addresses->push_back(address);
2350 }
2351 }
2352
2353 return true;
2354}
2355
[email protected]16ee26d2012-03-08 03:34:352356void HostResolverImpl::CacheResult(const Key& key,
[email protected]1339a2a22012-10-17 08:39:432357 const HostCache::Entry& entry,
[email protected]16ee26d2012-03-08 03:34:352358 base::TimeDelta ttl) {
Miriam Gershensone3bc63022017-10-09 19:57:052359 // Don't cache an error unless it has a positive TTL.
2360 if (cache_.get() && (entry.error() == OK || ttl > base::TimeDelta()))
[email protected]1339a2a22012-10-17 08:39:432361 cache_->Set(key, entry, base::TimeTicks::Now(), ttl);
[email protected]ef4c40c2010-09-01 14:42:032362}
2363
[email protected]0f292de02012-02-01 22:28:202364void HostResolverImpl::RemoveJob(Job* job) {
2365 DCHECK(job);
avi9e72eb482016-12-09 21:10:542366 auto it = jobs_.find(job->key());
2367 if (it != jobs_.end() && it->second.get() == job) {
2368 it->second.release();
[email protected]16ee26d2012-03-08 03:34:352369 jobs_.erase(it);
avi9e72eb482016-12-09 21:10:542370 }
[email protected]b59ff372009-07-15 22:04:322371}
2372
[email protected]137af622010-02-05 02:14:352373HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest(
pauljensen19eb73422015-04-15 23:20:062374 const RequestInfo& info,
martijna23c8962016-03-04 18:18:512375 const IPAddress* ip_address,
tfarina428341112016-09-22 13:38:202376 const NetLogWithSource& net_log) {
[email protected]eaf3a3b2010-09-03 20:34:272377 HostResolverFlags effective_flags =
2378 info.host_resolver_flags() | additional_resolver_flags_;
[email protected]137af622010-02-05 02:14:352379 AddressFamily effective_address_family = info.address_family();
[email protected]9db6f702013-04-10 18:10:512380
mgersh3eba057d2017-02-28 18:50:562381 if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED &&
2382 // When resolving IPv4 literals, there's no need to probe for IPv6.
2383 // When resolving IPv6 literals, there's no benefit to artificially
2384 // limiting our resolution based on a probe. Prior logic ensures
2385 // that this query is UNSPECIFIED (see effective_address_family
2386 // check above) so the code requesting the resolution should be amenable
2387 // to receiving a IPv6 resolution.
2388 !use_local_ipv6_ && ip_address == nullptr && !IsIPv6Reachable(net_log)) {
2389 effective_address_family = ADDRESS_FAMILY_IPV4;
2390 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
[email protected]9db6f702013-04-10 18:10:512391 }
2392
estarkd1bc206e2015-06-20 00:44:392393 return Key(info.hostname(), effective_address_family, effective_flags);
[email protected]137af622010-02-05 02:14:352394}
2395
tfarina428341112016-09-22 13:38:202396bool HostResolverImpl::IsIPv6Reachable(const NetLogWithSource& net_log) {
mgershaf9a9232017-04-13 20:19:032397 // Don't bother checking if the device is on WiFi and IPv6 is assumed to not
2398 // work on WiFi.
2399 if (assume_ipv6_failure_on_wifi_ &&
2400 NetworkChangeNotifier::GetConnectionType() ==
2401 NetworkChangeNotifier::CONNECTION_WIFI) {
2402 return false;
2403 }
2404
eroman1a17ef32016-12-14 01:06:092405 // Cache the result for kIPv6ProbePeriodMs (measured from after
2406 // IsGloballyReachable() completes).
sergeyub8cdc212015-05-14 18:50:372407 bool cached = true;
eroman1a17ef32016-12-14 01:06:092408 if ((base::TimeTicks::Now() - last_ipv6_probe_time_).InMilliseconds() >
2409 kIPv6ProbePeriodMs) {
martijna23c8962016-03-04 18:18:512410 last_ipv6_probe_result_ =
2411 IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log);
eroman1a17ef32016-12-14 01:06:092412 last_ipv6_probe_time_ = base::TimeTicks::Now();
sergeyub8cdc212015-05-14 18:50:372413 cached = false;
2414 }
mikecirone8b85c432016-09-08 19:11:002415 net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK,
sergeyub8cdc212015-05-14 18:50:372416 base::Bind(&NetLogIPv6AvailableCallback,
2417 last_ipv6_probe_result_, cached));
2418 return last_ipv6_probe_result_;
2419}
2420
mgershaf9a9232017-04-13 20:19:032421bool HostResolverImpl::IsGloballyReachable(const IPAddress& dest,
2422 const NetLogWithSource& net_log) {
mgershaf9a9232017-04-13 20:19:032423 std::unique_ptr<DatagramClientSocket> socket(
2424 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
Sergey Ulanovcbdfc8852018-03-16 20:13:282425 DatagramSocket::DEFAULT_BIND, net_log.net_log(), net_log.source()));
mgershaf9a9232017-04-13 20:19:032426 int rv = socket->Connect(IPEndPoint(dest, 53));
2427 if (rv != OK)
2428 return false;
2429 IPEndPoint endpoint;
2430 rv = socket->GetLocalAddress(&endpoint);
2431 if (rv != OK)
2432 return false;
2433 DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily());
2434 const IPAddress& address = endpoint.address();
2435
2436 bool is_link_local =
2437 (address.bytes()[0] == 0xFE) && ((address.bytes()[1] & 0xC0) == 0x80);
2438 if (is_link_local)
2439 return false;
2440
2441 const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0};
2442 if (IPAddressStartsWith(address, kTeredoPrefix))
2443 return false;
2444
2445 return true;
2446}
2447
mmenke91c17162016-06-02 16:03:232448void HostResolverImpl::RunLoopbackProbeJob() {
Francois Doraya2d01ba2017-09-25 19:17:402449 // Run this asynchronously as it can take 40-100ms and should not block
2450 // initialization.
2451 base::PostTaskWithTraitsAndReplyWithResult(
2452 FROM_HERE,
2453 {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
2454 base::BindOnce(&HaveOnlyLoopbackAddresses),
2455 base::BindOnce(&HostResolverImpl::SetHaveOnlyLoopbackAddresses,
2456 weak_ptr_factory_.GetWeakPtr()));
mmenke91c17162016-06-02 16:03:232457}
2458
[email protected]35ddc282010-09-21 23:42:062459void HostResolverImpl::AbortAllInProgressJobs() {
[email protected]b3601bc22012-02-21 21:23:202460 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2461 // first collect and remove all running jobs from |jobs_|.
danakj22f90e72016-04-16 01:55:402462 std::vector<std::unique_ptr<Job>> jobs_to_abort;
avi9e72eb482016-12-09 21:10:542463 for (auto it = jobs_.begin(); it != jobs_.end();) {
2464 Job* job = it->second.get();
[email protected]0f292de02012-02-01 22:28:202465 if (job->is_running()) {
avi9e72eb482016-12-09 21:10:542466 jobs_to_abort.push_back(std::move(it->second));
[email protected]b3601bc22012-02-21 21:23:202467 jobs_.erase(it++);
[email protected]0f292de02012-02-01 22:28:202468 } else {
[email protected]b3601bc22012-02-21 21:23:202469 DCHECK(job->is_queued());
2470 ++it;
[email protected]0f292de02012-02-01 22:28:202471 }
[email protected]ef4c40c2010-09-01 14:42:032472 }
[email protected]b3601bc22012-02-21 21:23:202473
[email protected]daae1322013-09-05 18:26:502474 // Pause the dispatcher so it won't start any new dispatcher jobs while
2475 // aborting the old ones. This is needed so that it won't start the second
2476 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2477 // invalid.
[email protected]106ccd2c2014-06-17 09:21:002478 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2479 dispatcher_->SetLimits(
[email protected]daae1322013-09-05 18:26:502480 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
[email protected]70c04ab2013-08-22 16:05:122481
[email protected]57a48d32012-03-03 00:04:552482 // Life check to bail once |this| is deleted.
[email protected]4589a3a2012-09-20 20:57:072483 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]57a48d32012-03-03 00:04:552484
[email protected]16ee26d2012-03-08 03:34:352485 // Then Abort them.
[email protected]11fbca0b2013-06-02 23:37:212486 for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
[email protected]57a48d32012-03-03 00:04:552487 jobs_to_abort[i]->Abort();
olli.raulaa21e9eb72015-12-17 08:18:112488 ignore_result(jobs_to_abort[i].release());
[email protected]b3601bc22012-02-21 21:23:202489 }
[email protected]daae1322013-09-05 18:26:502490
2491 if (self)
[email protected]106ccd2c2014-06-17 09:21:002492 dispatcher_->SetLimits(limits);
[email protected]daae1322013-09-05 18:26:502493}
2494
2495void HostResolverImpl::AbortDnsTasks() {
2496 // Pause the dispatcher so it won't start any new dispatcher jobs while
2497 // aborting the old ones. This is needed so that it won't start the second
2498 // DnsTransaction for a job if the DnsConfig just changed.
[email protected]106ccd2c2014-06-17 09:21:002499 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2500 dispatcher_->SetLimits(
[email protected]daae1322013-09-05 18:26:502501 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2502
avi9e72eb482016-12-09 21:10:542503 for (auto it = jobs_.begin(); it != jobs_.end(); ++it)
[email protected]daae1322013-09-05 18:26:502504 it->second->AbortDnsTask();
[email protected]106ccd2c2014-06-17 09:21:002505 dispatcher_->SetLimits(limits);
[email protected]ef4c40c2010-09-01 14:42:032506}
2507
[email protected]78eac2a2012-03-14 19:09:272508void HostResolverImpl::TryServingAllJobsFromHosts() {
2509 if (!HaveDnsConfig())
2510 return;
2511
2512 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2513 // http://crbug.com/117655
2514
2515 // Life check to bail once |this| is deleted.
[email protected]4589a3a2012-09-20 20:57:072516 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]78eac2a2012-03-14 19:09:272517
avi9e72eb482016-12-09 21:10:542518 for (auto it = jobs_.begin(); self.get() && it != jobs_.end();) {
2519 Job* job = it->second.get();
[email protected]78eac2a2012-03-14 19:09:272520 ++it;
2521 // This could remove |job| from |jobs_|, but iterator will remain valid.
2522 job->ServeFromHosts();
2523 }
2524}
2525
[email protected]be1a48b2011-01-20 00:12:132526void HostResolverImpl::OnIPAddressChanged() {
sergeyub8cdc212015-05-14 18:50:372527 last_ipv6_probe_time_ = base::TimeTicks();
[email protected]12faa4c2012-11-06 04:44:182528 // Abandon all ProbeJobs.
2529 probe_weak_ptr_factory_.InvalidateWeakPtrs();
Matt Menkeef8f5cf82017-07-20 22:50:172530 if (cache_.get())
mgersh5bb3b9d2017-02-13 21:27:102531 cache_->OnNetworkChange();
[email protected]7c466e92013-07-20 01:44:482532#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
mmenke91c17162016-06-02 16:03:232533 RunLoopbackProbeJob();
[email protected]be1a48b2011-01-20 00:12:132534#endif
2535 AbortAllInProgressJobs();
2536 // |this| may be deleted inside AbortAllInProgressJobs().
2537}
2538
ttuttlecf1158bf2016-03-18 16:37:442539void HostResolverImpl::OnConnectionTypeChanged(
2540 NetworkChangeNotifier::ConnectionType type) {
2541 proc_params_.unresponsive_delay =
2542 GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
2543 "DnsUnresponsiveDelayMsByConnectionType",
2544 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs),
2545 type);
2546}
2547
pauljensen101ed372015-04-17 00:11:422548void HostResolverImpl::OnInitialDNSConfigRead() {
2549 UpdateDNSConfig(false);
2550}
2551
[email protected]bb0e34542012-08-31 19:52:402552void HostResolverImpl::OnDNSChanged() {
pauljensen101ed372015-04-17 00:11:422553 UpdateDNSConfig(true);
2554}
2555
2556void HostResolverImpl::UpdateDNSConfig(bool config_changed) {
[email protected]bb0e34542012-08-31 19:52:402557 DnsConfig dns_config;
2558 NetworkChangeNotifier::GetDnsConfig(&dns_config);
[email protected]ec666ab22013-04-17 20:05:592559
[email protected]b4481b222012-03-16 17:13:112560 if (net_log_) {
mikecirone8b85c432016-09-08 19:11:002561 net_log_->AddGlobalEntry(NetLogEventType::DNS_CONFIG_CHANGED,
2562 base::Bind(&NetLogDnsConfigCallback, &dns_config));
[email protected]b4481b222012-03-16 17:13:112563 }
2564
[email protected]01b3b9d2012-08-13 16:18:142565 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
[email protected]d7b9a2b2012-05-31 22:31:192566 received_dns_config_ = dns_config.IsValid();
[email protected]c9fa8f312013-09-17 12:24:522567 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2568 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
[email protected]78eac2a2012-03-14 19:09:272569
[email protected]a8883e452012-11-17 05:58:062570 num_dns_failures_ = 0;
2571
[email protected]01b3b9d2012-08-13 16:18:142572 // We want a new DnsSession in place, before we Abort running Jobs, so that
2573 // the newly started jobs use the new config.
[email protected]f0f602bd2012-11-15 18:01:022574 if (dns_client_.get()) {
Miriam Gershenson44aafc122017-10-18 19:29:252575 // Make sure that if the update is an initial read, not a change, there
Miriam Gershenson668e8442017-10-31 20:31:532576 // wasn't already a DnsConfig or it's the same one.
2577 DCHECK(config_changed || !dns_client_->GetConfig() ||
2578 dns_client_->GetConfig()->Equals(dns_config));
Brad Lassey786929ad2018-02-21 20:54:272579 dns_config.dns_over_https_servers = dns_over_https_servers_;
[email protected]d7b9a2b2012-05-31 22:31:192580 dns_client_->SetConfig(dns_config);
Miriam Gershenson44aafc122017-10-18 19:29:252581 if (dns_client_->GetConfig())
[email protected]f0f602bd2012-11-15 18:01:022582 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
[email protected]f0f602bd2012-11-15 18:01:022583 }
[email protected]01b3b9d2012-08-13 16:18:142584
pauljensen101ed372015-04-17 00:11:422585 if (config_changed) {
2586 // If the DNS server has changed, existing cached info could be wrong so we
mgersh92b1af12017-03-29 15:49:232587 // have to expire our internal cache :( Note that OS level DNS caches, such
pauljensen101ed372015-04-17 00:11:422588 // as NSCD's cache should be dropped automatically by the OS when
2589 // resolv.conf changes so we don't need to do anything to clear that cache.
Matt Menkeef8f5cf82017-07-20 22:50:172590 if (cache_.get())
mgersh92b1af12017-03-29 15:49:232591 cache_->OnNetworkChange();
[email protected]01b3b9d2012-08-13 16:18:142592
pauljensen101ed372015-04-17 00:11:422593 // Life check to bail once |this| is deleted.
2594 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]f0f602bd2012-11-15 18:01:022595
pauljensen101ed372015-04-17 00:11:422596 // Existing jobs will have been sent to the original server so they need to
2597 // be aborted.
2598 AbortAllInProgressJobs();
[email protected]01b3b9d2012-08-13 16:18:142599
pauljensen101ed372015-04-17 00:11:422600 // |this| may be deleted inside AbortAllInProgressJobs().
2601 if (self.get())
2602 TryServingAllJobsFromHosts();
2603 }
[email protected]78eac2a2012-03-14 19:09:272604}
2605
2606bool HostResolverImpl::HaveDnsConfig() const {
[email protected]32b1dbcf2013-01-26 03:48:252607 // Use DnsClient only if it's fully configured and there is no override by
2608 // ScopedDefaultHostResolverProc.
2609 // The alternative is to use NetworkChangeNotifier to override DnsConfig,
2610 // but that would introduce construction order requirements for NCN and SDHRP.
wezb9820d42016-06-22 23:41:042611 return dns_client_ && dns_client_->GetConfig() &&
2612 (proc_params_.resolver_proc || !HostResolverProc::GetDefault());
[email protected]b3601bc22012-02-21 21:23:202613}
2614
[email protected]1ffdda82012-12-12 23:04:222615void HostResolverImpl::OnDnsTaskResolve(int net_error) {
[email protected]f0f602bd2012-11-15 18:01:022616 DCHECK(dns_client_);
[email protected]1ffdda82012-12-12 23:04:222617 if (net_error == OK) {
[email protected]f0f602bd2012-11-15 18:01:022618 num_dns_failures_ = 0;
2619 return;
2620 }
2621 ++num_dns_failures_;
2622 if (num_dns_failures_ < kMaximumDnsFailures)
2623 return;
[email protected]daae1322013-09-05 18:26:502624
2625 // Disable DnsClient until the next DNS change. Must be done before aborting
2626 // DnsTasks, since doing so may start new jobs.
[email protected]f0f602bd2012-11-15 18:01:022627 dns_client_->SetConfig(DnsConfig());
[email protected]daae1322013-09-05 18:26:502628
2629 // Switch jobs with active DnsTasks over to using ProcTasks.
2630 AbortDnsTasks();
2631
[email protected]f0f602bd2012-11-15 18:01:022632 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
Ilya Sherman0eb39802017-12-08 20:58:182633 base::UmaHistogramSparse("AsyncDNS.DnsClientDisabledReason",
2634 std::abs(net_error));
[email protected]f0f602bd2012-11-15 18:01:022635}
2636
danakj22f90e72016-04-16 01:55:402637void HostResolverImpl::SetDnsClient(std::unique_ptr<DnsClient> dns_client) {
[email protected]daae1322013-09-05 18:26:502638 // DnsClient and config must be updated before aborting DnsTasks, since doing
2639 // so may start new jobs.
dchengc7eeda422015-12-26 03:56:482640 dns_client_ = std::move(dns_client);
[email protected]daae1322013-09-05 18:26:502641 if (dns_client_ && !dns_client_->GetConfig() &&
2642 num_dns_failures_ < kMaximumDnsFailures) {
2643 DnsConfig dns_config;
2644 NetworkChangeNotifier::GetDnsConfig(&dns_config);
Brad Lassey786929ad2018-02-21 20:54:272645 dns_config.dns_over_https_servers = dns_over_https_servers_;
[email protected]daae1322013-09-05 18:26:502646 dns_client_->SetConfig(dns_config);
2647 num_dns_failures_ = 0;
2648 if (dns_client_->GetConfig())
2649 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
[email protected]a8883e452012-11-17 05:58:062650 }
[email protected]daae1322013-09-05 18:26:502651
2652 AbortDnsTasks();
[email protected]a8883e452012-11-17 05:58:062653}
2654
juliatuttled7fa5852016-07-29 13:16:572655void HostResolverImpl::InitializePersistence(
2656 const PersistCallback& persist_callback,
2657 std::unique_ptr<const base::Value> old_data) {
2658 DCHECK(!persist_initialized_);
2659 persist_callback_ = persist_callback;
2660 persist_initialized_ = true;
2661 if (old_data)
2662 ApplyPersistentData(std::move(old_data));
2663}
2664
juliatuttle7e8bf1c2016-08-02 19:09:092665void HostResolverImpl::ApplyPersistentData(
2666 std::unique_ptr<const base::Value> data) {}
2667
2668std::unique_ptr<const base::Value> HostResolverImpl::GetPersistentData() {
2669 return std::unique_ptr<const base::Value>();
2670}
2671
juliatuttled7fa5852016-07-29 13:16:572672void HostResolverImpl::SchedulePersist() {
2673 if (!persist_initialized_ || persist_timer_.IsRunning())
2674 return;
2675 persist_timer_.Start(
2676 FROM_HERE, base::TimeDelta::FromSeconds(kPersistDelaySec),
2677 base::Bind(&HostResolverImpl::DoPersist, weak_ptr_factory_.GetWeakPtr()));
2678}
2679
2680void HostResolverImpl::DoPersist() {
2681 DCHECK(persist_initialized_);
2682 persist_callback_.Run(GetPersistentData());
2683}
2684
maksim.sisov31452af2016-07-27 06:38:102685HostResolverImpl::RequestImpl::~RequestImpl() {
2686 if (job_)
2687 job_->CancelRequest(this);
2688}
2689
2690void HostResolverImpl::RequestImpl::ChangeRequestPriority(
2691 RequestPriority priority) {
2692 job_->ChangeRequestPriority(this, priority);
2693}
2694
[email protected]b59ff372009-07-15 22:04:322695} // namespace net