blob: 7a29f958f5d309ff34c321740e78009d79e1caec [file] [log] [blame]
[email protected]a2730882012-01-21 00:56:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b59ff372009-07-15 22:04:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f2cb3cf2013-03-21 01:40:535#include "net/dns/host_resolver_impl.h"
[email protected]b59ff372009-07-15 22:04:326
[email protected]21526002010-05-16 19:42:467#if defined(OS_WIN)
8#include <Winsock2.h>
Fabrice de Gans-Riberi7de47372018-05-08 20:23:479#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]21526002010-05-16 19:42:4610#include <netdb.h>
tfarinada9e1362017-03-14 16:49:0611#include <netinet/in.h>
12#if !defined(OS_NACL)
13#include <net/if.h>
14#if !defined(OS_ANDROID)
15#include <ifaddrs.h>
16#endif // !defined(OS_ANDROID)
17#endif // !defined(OS_NACL)
Fabrice de Gans-Riberi7de47372018-05-08 20:23:4718#endif // defined(OS_WIN)
tfarinada9e1362017-03-14 16:49:0619
[email protected]68ad3ee2010-01-30 03:45:3920#include <cmath>
thestiga74ad2b2016-07-11 20:52:3621#include <memory>
[email protected]0f292de02012-02-01 22:28:2022#include <utility>
[email protected]21526002010-05-16 19:42:4623#include <vector>
[email protected]68ad3ee2010-01-30 03:45:3924
[email protected]33152acc2011-10-20 23:37:1225#include "base/bind.h"
[email protected]aa22b242011-11-16 18:58:2926#include "base/bind_helpers.h"
[email protected]0f292de02012-02-01 22:28:2027#include "base/callback.h"
maksim.sisov31452af2016-07-27 06:38:1028#include "base/callback_helpers.h"
[email protected]b59ff372009-07-15 22:04:3229#include "base/compiler_specific.h"
David Benjamina5677192018-04-23 23:25:4230#include "base/containers/linked_list.h"
[email protected]58580352010-10-26 04:07:5031#include "base/debug/debugger.h"
32#include "base/debug/stack_trace.h"
Avi Drissman13fc8932015-12-20 04:40:4633#include "base/macros.h"
mmenke91c17162016-06-02 16:03:2334#include "base/memory/ptr_util.h"
[email protected]1e9bbd22010-10-15 16:42:4535#include "base/metrics/field_trial.h"
Maks Orlovich5393c8682018-02-26 16:17:5036#include "base/metrics/field_trial_params.h"
Ilya Sherman0eb39802017-12-08 20:58:1837#include "base/metrics/histogram_functions.h"
davidbenad6fc442015-05-18 20:59:1338#include "base/metrics/histogram_macros.h"
anujk.sharma7c907f02015-05-29 02:55:4439#include "base/single_thread_task_runner.h"
[email protected]be528af2013-06-11 07:39:4840#include "base/strings/string_util.h"
[email protected]750b2f3c2013-06-07 18:41:0541#include "base/strings/utf_string_conversions.h"
Francois Doraya2d01ba2017-09-25 19:17:4042#include "base/task_scheduler/post_task.h"
43#include "base/threading/scoped_blocking_call.h"
gabf767595f2016-05-11 18:50:3544#include "base/threading/thread_task_runner_handle.h"
Misha Efimovb99e7da2018-05-30 16:59:0245#include "base/time/default_tick_clock.h"
[email protected]66e96c42013-06-28 15:20:3146#include "base/time/time.h"
ssid6d6b40102016-04-05 18:59:5647#include "base/trace_event/trace_event.h"
[email protected]21526002010-05-16 19:42:4648#include "base/values.h"
Fabrice de Gans-Riberi7de47372018-05-08 20:23:4749#include "build/build_config.h"
[email protected]b3601bc22012-02-21 21:23:2050#include "net/base/address_family.h"
[email protected]b59ff372009-07-15 22:04:3251#include "net/base/address_list.h"
[email protected]ee094b82010-08-24 15:55:5152#include "net/base/host_port_pair.h"
martijna23c8962016-03-04 18:18:5153#include "net/base/ip_address.h"
[email protected]1c7cf3f82014-08-07 21:33:4854#include "net/base/ip_endpoint.h"
[email protected]2bb04442010-08-18 18:01:1555#include "net/base/net_errors.h"
xunjieli0b7f5b62016-12-06 20:43:4856#include "net/base/trace_constants.h"
tfarina9ed7f8c52016-02-19 17:50:1857#include "net/base/url_util.h"
[email protected]0adcb2b2012-08-15 21:30:4658#include "net/dns/address_sorter.h"
[email protected]78eac2a2012-03-14 19:09:2759#include "net/dns/dns_client.h"
[email protected]b3601bc22012-02-21 21:23:2060#include "net/dns/dns_config_service.h"
61#include "net/dns/dns_protocol.h"
tfarina47598f042015-10-07 23:08:3562#include "net/dns/dns_reloader.h"
[email protected]b3601bc22012-02-21 21:23:2063#include "net/dns/dns_response.h"
[email protected]b3601bc22012-02-21 21:23:2064#include "net/dns/dns_transaction.h"
tfarina77021d62015-10-11 20:19:0365#include "net/dns/dns_util.h"
[email protected]f2cb3cf2013-03-21 01:40:5366#include "net/dns/host_resolver_proc.h"
eroman87c53d62015-04-02 06:51:0767#include "net/log/net_log.h"
mikecironef22f9812016-10-04 03:40:1968#include "net/log/net_log_capture_mode.h"
mikecirone8b85c432016-09-08 19:11:0069#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1970#include "net/log/net_log_parameters_callback.h"
71#include "net/log/net_log_source.h"
mikecirone8b85c432016-09-08 19:11:0072#include "net/log/net_log_source_type.h"
mikecironef22f9812016-10-04 03:40:1973#include "net/log/net_log_with_source.h"
[email protected]9db6f702013-04-10 18:10:5174#include "net/socket/client_socket_factory.h"
tfarina5dd13c22016-11-16 12:08:2675#include "net/socket/datagram_client_socket.h"
pauljensen370f1c72015-02-17 16:59:1476#include "url/url_canon_ip.h"
[email protected]b59ff372009-07-15 22:04:3277
78#if defined(OS_WIN)
79#include "net/base/winsock_init.h"
80#endif
81
tfarinada9e1362017-03-14 16:49:0682#if defined(OS_ANDROID)
83#include "net/android/network_library.h"
84#endif
85
[email protected]b59ff372009-07-15 22:04:3286namespace net {
87
[email protected]e95d3aca2010-01-11 22:47:4388namespace {
89
ttuttlecf1158bf2016-03-18 16:37:4490// Default delay between calls to the system resolver for the same hostname.
91// (Can be overridden by field trial.)
92const int64_t kDnsDefaultUnresponsiveDelayMs = 6000;
93
[email protected]6e78dfb2011-07-28 21:34:4794// Limit the size of hostnames that will be resolved to combat issues in
95// some platform's resolvers.
96const size_t kMaxHostLength = 4096;
97
[email protected]a2730882012-01-21 00:56:2798// Default TTL for successful resolutions with ProcTask.
99const unsigned kCacheEntryTTLSeconds = 60;
100
[email protected]b3601bc22012-02-21 21:23:20101// Default TTL for unsuccessful resolutions with ProcTask.
102const unsigned kNegativeCacheEntryTTLSeconds = 0;
103
[email protected]895123222012-10-25 15:21:17104// Minimum TTL for successful resolutions with DnsTask.
105const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
106
sergeyub8cdc212015-05-14 18:50:37107// Time between IPv6 probes, i.e. for how long results of each IPv6 probe are
108// cached.
109const int kIPv6ProbePeriodMs = 1000;
110
111// Google DNS address used for IPv6 probes.
112const uint8_t kIPv6ProbeAddress[] =
113 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
115
[email protected]24f4bab2010-10-15 01:27:11116// We use a separate histogram name for each platform to facilitate the
117// display of error codes by their symbolic name (since each platform has
118// different mappings).
119const char kOSErrorsForGetAddrinfoHistogramName[] =
120#if defined(OS_WIN)
121 "Net.OSErrorsForGetAddrinfo_Win";
122#elif defined(OS_MACOSX)
123 "Net.OSErrorsForGetAddrinfo_Mac";
124#elif defined(OS_LINUX)
125 "Net.OSErrorsForGetAddrinfo_Linux";
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47126#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]24f4bab2010-10-15 01:27:11127 "Net.OSErrorsForGetAddrinfo";
128#endif
129
[email protected]c89b2442011-05-26 14:28:27130// Gets a list of the likely error codes that getaddrinfo() can return
131// (non-exhaustive). These are the error codes that we will track via
132// a histogram.
133std::vector<int> GetAllGetAddrinfoOSErrors() {
134 int os_errors[] = {
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47135#if defined(OS_WIN)
136 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
137 WSA_NOT_ENOUGH_MEMORY,
138 WSAEAFNOSUPPORT,
139 WSAEINVAL,
140 WSAESOCKTNOSUPPORT,
141 WSAHOST_NOT_FOUND,
142 WSANO_DATA,
143 WSANO_RECOVERY,
144 WSANOTINITIALISED,
145 WSATRY_AGAIN,
146 WSATYPE_NOT_FOUND,
147 // The following are not in doc, but might be to appearing in results :-(.
148 WSA_INVALID_HANDLE,
149#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]23f771162011-06-02 18:37:51150#if !defined(OS_FREEBSD)
[email protected]39588992011-07-11 19:54:37151#if !defined(OS_ANDROID)
[email protected]c48aef92011-11-22 23:41:45152 // EAI_ADDRFAMILY has been declared obsolete in Android's and
153 // FreeBSD's netdb.h.
[email protected]c89b2442011-05-26 14:28:27154 EAI_ADDRFAMILY,
[email protected]39588992011-07-11 19:54:37155#endif
[email protected]c48aef92011-11-22 23:41:45156 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
[email protected]23f771162011-06-02 18:37:51157 EAI_NODATA,
158#endif
[email protected]c89b2442011-05-26 14:28:27159 EAI_AGAIN,
160 EAI_BADFLAGS,
161 EAI_FAIL,
162 EAI_FAMILY,
163 EAI_MEMORY,
[email protected]c89b2442011-05-26 14:28:27164 EAI_NONAME,
165 EAI_SERVICE,
166 EAI_SOCKTYPE,
167 EAI_SYSTEM,
[email protected]c89b2442011-05-26 14:28:27168#endif
169 };
170
171 // Ensure all errors are positive, as histogram only tracks positive values.
172 for (size_t i = 0; i < arraysize(os_errors); ++i) {
173 os_errors[i] = std::abs(os_errors[i]);
174 }
175
Ryan Sleevi9c79c3542018-05-12 01:38:09176 return base::CustomHistogram::ArrayToCustomEnumRanges(os_errors);
[email protected]c89b2442011-05-26 14:28:27177}
178
[email protected]1def74c2012-03-22 20:07:00179enum DnsResolveStatus {
180 RESOLVE_STATUS_DNS_SUCCESS = 0,
181 RESOLVE_STATUS_PROC_SUCCESS,
182 RESOLVE_STATUS_FAIL,
[email protected]1d932852012-06-19 19:40:33183 RESOLVE_STATUS_SUSPECT_NETBIOS,
[email protected]1def74c2012-03-22 20:07:00184 RESOLVE_STATUS_MAX
185};
186
eroman91dd3602015-03-26 03:46:33187// ICANN uses this localhost address to indicate a name collision.
188//
189// The policy in Chromium is to fail host resolving if it resolves to
190// this special address.
191//
192// Not however that IP literals are exempt from this policy, so it is still
193// possible to navigate to http://127.0.53.53/ directly.
194//
195// For more details: https://www.icann.org/news/announcement-2-2014-08-01-en
martijna23c8962016-03-04 18:18:51196const uint8_t kIcanNameCollisionIp[] = {127, 0, 53, 53};
eroman91dd3602015-03-26 03:46:33197
eroman1efc237c2016-12-14 00:00:45198bool ContainsIcannNameCollisionIp(const AddressList& addr_list) {
199 for (const auto& endpoint : addr_list) {
200 const IPAddress& addr = endpoint.address();
201 if (addr.IsIPv4() && IPAddressStartsWith(addr, kIcanNameCollisionIp)) {
202 return true;
203 }
204 }
205 return false;
206}
207
[email protected]1def74c2012-03-22 20:07:00208void UmaAsyncDnsResolveStatus(DnsResolveStatus result) {
209 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
210 result,
211 RESOLVE_STATUS_MAX);
212}
213
[email protected]1d932852012-06-19 19:40:33214bool ResemblesNetBIOSName(const std::string& hostname) {
215 return (hostname.size() < 16) && (hostname.find('.') == std::string::npos);
216}
217
218// True if |hostname| ends with either ".local" or ".local.".
219bool ResemblesMulticastDNSName(const std::string& hostname) {
220 DCHECK(!hostname.empty());
221 const char kSuffix[] = ".local.";
222 const size_t kSuffixLen = sizeof(kSuffix) - 1;
223 const size_t kSuffixLenTrimmed = kSuffixLen - 1;
thestiga74ad2b2016-07-11 20:52:36224 if (hostname.back() == '.') {
[email protected]1d932852012-06-19 19:40:33225 return hostname.size() > kSuffixLen &&
226 !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix);
227 }
228 return hostname.size() > kSuffixLenTrimmed &&
229 !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed,
230 kSuffix, kSuffixLenTrimmed);
231}
232
[email protected]51b9a6b2012-06-25 21:50:29233// A macro to simplify code and readability.
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21234#define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
235 do { \
236 switch (priority) { \
237 case HIGHEST: \
Miriam Gershenson90d05e02017-09-28 19:29:28238 UMA_HISTOGRAM_LONG_TIMES_100(basename ".HIGHEST", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21239 break; \
240 case MEDIUM: \
Miriam Gershenson90d05e02017-09-28 19:29:28241 UMA_HISTOGRAM_LONG_TIMES_100(basename ".MEDIUM", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21242 break; \
243 case LOW: \
Miriam Gershenson90d05e02017-09-28 19:29:28244 UMA_HISTOGRAM_LONG_TIMES_100(basename ".LOW", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21245 break; \
246 case LOWEST: \
Miriam Gershenson90d05e02017-09-28 19:29:28247 UMA_HISTOGRAM_LONG_TIMES_100(basename ".LOWEST", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21248 break; \
249 case IDLE: \
Miriam Gershenson90d05e02017-09-28 19:29:28250 UMA_HISTOGRAM_LONG_TIMES_100(basename ".IDLE", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21251 break; \
252 case THROTTLED: \
Miriam Gershenson90d05e02017-09-28 19:29:28253 UMA_HISTOGRAM_LONG_TIMES_100(basename ".THROTTLED", time); \
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21254 break; \
255 } \
256 UMA_HISTOGRAM_LONG_TIMES_100(basename, time); \
[email protected]51b9a6b2012-06-25 21:50:29257 } while (0)
258
259// Record time from Request creation until a valid DNS response.
Miriam Gershenson90d05e02017-09-28 19:29:28260void RecordTotalTime(bool speculative,
261 bool from_cache,
[email protected]51b9a6b2012-06-25 21:50:29262 base::TimeDelta duration) {
Miriam Gershenson7ba03e12018-02-07 22:48:24263 if (!speculative) {
Miriam Gershenson90d05e02017-09-28 19:29:28264 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.TotalTime", duration);
Miriam Gershenson90d05e02017-09-28 19:29:28265
Miriam Gershenson7ba03e12018-02-07 22:48:24266 if (!from_cache)
Miriam Gershenson90d05e02017-09-28 19:29:28267 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.TotalTimeNotCached", duration);
[email protected]51b9a6b2012-06-25 21:50:29268 }
269}
270
[email protected]1339a2a22012-10-17 08:39:43271void RecordTTL(base::TimeDelta ttl) {
272 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl,
273 base::TimeDelta::FromSeconds(1),
274 base::TimeDelta::FromDays(1), 100);
275}
276
[email protected]16c2bd72013-06-28 01:19:22277bool ConfigureAsyncDnsNoFallbackFieldTrial() {
278 const bool kDefault = false;
279
280 // Configure the AsyncDns field trial as follows:
281 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
282 // groups AsyncDnsA and AsyncDnsB: return false,
283 // groups SystemDnsA and SystemDnsB: return false,
284 // otherwise (trial absent): return default.
285 std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
brettw3a2c6902015-07-06 19:43:29286 if (!group_name.empty()) {
287 return base::StartsWith(group_name, "AsyncDnsNoFallback",
288 base::CompareCase::INSENSITIVE_ASCII);
289 }
[email protected]16c2bd72013-06-28 01:19:22290 return kDefault;
291}
292
Maks Orlovich5393c8682018-02-26 16:17:50293const base::FeatureParam<base::TaskPriority>::Option prio_modes[] = {
294 {base::TaskPriority::USER_VISIBLE, "default"},
295 {base::TaskPriority::USER_BLOCKING, "user_blocking"}};
296const base::Feature kSystemResolverPriorityExperiment = {
297 "SystemResolverPriorityExperiment", base::FEATURE_DISABLED_BY_DEFAULT};
298const base::FeatureParam<base::TaskPriority> priority_mode{
299 &kSystemResolverPriorityExperiment, "mode",
300 base::TaskPriority::USER_VISIBLE, &prio_modes};
301
[email protected]d7b9a2b2012-05-31 22:31:19302//-----------------------------------------------------------------------------
303
Avi Drissman13fc8932015-12-20 04:40:46304AddressList EnsurePortOnAddressList(const AddressList& list, uint16_t port) {
[email protected]895123222012-10-25 15:21:17305 if (list.empty() || list.front().port() == port)
306 return list;
307 return AddressList::CopyWithPort(list, port);
[email protected]7054e78f2012-05-07 21:44:56308}
309
[email protected]ec666ab22013-04-17 20:05:59310// Returns true if |addresses| contains only IPv4 loopback addresses.
311bool IsAllIPv4Loopback(const AddressList& addresses) {
312 for (unsigned i = 0; i < addresses.size(); ++i) {
martijna23c8962016-03-04 18:18:51313 const IPAddress& address = addresses[i].address();
[email protected]ec666ab22013-04-17 20:05:59314 switch (addresses[i].GetFamily()) {
315 case ADDRESS_FAMILY_IPV4:
martijna23c8962016-03-04 18:18:51316 if (address.bytes()[0] != 127)
[email protected]ec666ab22013-04-17 20:05:59317 return false;
318 break;
319 case ADDRESS_FAMILY_IPV6:
320 return false;
321 default:
322 NOTREACHED();
323 return false;
324 }
325 }
326 return true;
327}
328
tfarinada9e1362017-03-14 16:49:06329// Returns true if it can determine that only loopback addresses are configured.
330// i.e. if only 127.0.0.1 and ::1 are routable.
331// Also returns false if it cannot determine this.
332bool HaveOnlyLoopbackAddresses() {
Francois Doraya2d01ba2017-09-25 19:17:40333 base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::WILL_BLOCK);
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47334#if defined(OS_WIN)
335 // TODO(wtc): implement with the GetAdaptersAddresses function.
336 NOTIMPLEMENTED();
337 return false;
338#elif defined(OS_ANDROID)
tfarinada9e1362017-03-14 16:49:06339 return android::HaveOnlyLoopbackAddresses();
340#elif defined(OS_NACL)
341 NOTIMPLEMENTED();
342 return false;
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47343#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
tfarinada9e1362017-03-14 16:49:06344 struct ifaddrs* interface_addr = NULL;
345 int rv = getifaddrs(&interface_addr);
346 if (rv != 0) {
347 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
348 return false;
349 }
350
351 bool result = true;
352 for (struct ifaddrs* interface = interface_addr;
353 interface != NULL;
354 interface = interface->ifa_next) {
355 if (!(IFF_UP & interface->ifa_flags))
356 continue;
357 if (IFF_LOOPBACK & interface->ifa_flags)
358 continue;
359 const struct sockaddr* addr = interface->ifa_addr;
360 if (!addr)
361 continue;
362 if (addr->sa_family == AF_INET6) {
363 // Safe cast since this is AF_INET6.
364 const struct sockaddr_in6* addr_in6 =
365 reinterpret_cast<const struct sockaddr_in6*>(addr);
366 const struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
367 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr))
368 continue;
369 }
370 if (addr->sa_family != AF_INET6 && addr->sa_family != AF_INET)
371 continue;
372
373 result = false;
374 break;
375 }
376 freeifaddrs(interface_addr);
377 return result;
tfarinada9e1362017-03-14 16:49:06378#endif // defined(various platforms)
379}
380
[email protected]cd565142012-06-12 16:21:45381// Creates NetLog parameters when the resolve failed.
danakj22f90e72016-04-16 01:55:40382std::unique_ptr<base::Value> NetLogProcTaskFailedCallback(
Avi Drissman13fc8932015-12-20 04:40:46383 uint32_t attempt_number,
eroman001c3742015-04-23 03:11:17384 int net_error,
385 int os_error,
386 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40387 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]cd565142012-06-12 16:21:45388 if (attempt_number)
389 dict->SetInteger("attempt_number", attempt_number);
[email protected]21526002010-05-16 19:42:46390
[email protected]cd565142012-06-12 16:21:45391 dict->SetInteger("net_error", net_error);
[email protected]13024882011-05-18 23:19:16392
[email protected]cd565142012-06-12 16:21:45393 if (os_error) {
394 dict->SetInteger("os_error", os_error);
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47395#if defined(OS_WIN)
[email protected]cd565142012-06-12 16:21:45396 // Map the error code to a human-readable string.
wezb9820d42016-06-22 23:41:04397 LPWSTR error_string = nullptr;
Peter Kastingbe940e92014-11-20 23:14:08398 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
399 0, // Use the internal message table.
400 os_error,
401 0, // Use default language.
402 (LPWSTR)&error_string,
403 0, // Buffer size.
404 0); // Arguments (unused).
[email protected]ad65a3e2013-12-25 18:18:01405 dict->SetString("os_error_string", base::WideToUTF8(error_string));
[email protected]cd565142012-06-12 16:21:45406 LocalFree(error_string);
Fabrice de Gans-Riberi7de47372018-05-08 20:23:47407#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
408 dict->SetString("os_error_string", gai_strerror(os_error));
[email protected]21526002010-05-16 19:42:46409#endif
[email protected]21526002010-05-16 19:42:46410 }
411
dchengc7eeda422015-12-26 03:56:48412 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45413}
[email protected]a9813302012-04-28 09:29:28414
[email protected]cd565142012-06-12 16:21:45415// Creates NetLog parameters when the DnsTask failed.
danakj22f90e72016-04-16 01:55:40416std::unique_ptr<base::Value> NetLogDnsTaskFailedCallback(
estade5e5529d2015-05-21 20:59:11417 int net_error,
418 int dns_error,
419 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40420 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]cd565142012-06-12 16:21:45421 dict->SetInteger("net_error", net_error);
422 if (dns_error)
423 dict->SetInteger("dns_error", dns_error);
dchengc7eeda422015-12-26 03:56:48424 return std::move(dict);
thestiga74ad2b2016-07-11 20:52:36425}
[email protected]ee094b82010-08-24 15:55:51426
[email protected]cd565142012-06-12 16:21:45427// Creates NetLog parameters containing the information in a RequestInfo object,
mikecironef22f9812016-10-04 03:40:19428// along with the associated NetLogSource.
danakj22f90e72016-04-16 01:55:40429std::unique_ptr<base::Value> NetLogRequestInfoCallback(
estade5e5529d2015-05-21 20:59:11430 const HostResolver::RequestInfo* info,
431 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40432 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b3601bc22012-02-21 21:23:20433
[email protected]cd565142012-06-12 16:21:45434 dict->SetString("host", info->host_port_pair().ToString());
435 dict->SetInteger("address_family",
436 static_cast<int>(info->address_family()));
437 dict->SetBoolean("allow_cached_response", info->allow_cached_response());
438 dict->SetBoolean("is_speculative", info->is_speculative());
dchengc7eeda422015-12-26 03:56:48439 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45440}
[email protected]b3601bc22012-02-21 21:23:20441
[email protected]cd565142012-06-12 16:21:45442// Creates NetLog parameters for the creation of a HostResolverImpl::Job.
danakj22f90e72016-04-16 01:55:40443std::unique_ptr<base::Value> NetLogJobCreationCallback(
mikecironef22f9812016-10-04 03:40:19444 const NetLogSource& source,
estade5e5529d2015-05-21 20:59:11445 const std::string* host,
446 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40447 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
estade5e5529d2015-05-21 20:59:11448 source.AddToEventParameters(dict.get());
[email protected]cd565142012-06-12 16:21:45449 dict->SetString("host", *host);
dchengc7eeda422015-12-26 03:56:48450 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45451}
[email protected]a9813302012-04-28 09:29:28452
[email protected]cd565142012-06-12 16:21:45453// Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
danakj22f90e72016-04-16 01:55:40454std::unique_ptr<base::Value> NetLogJobAttachCallback(
mikecironef22f9812016-10-04 03:40:19455 const NetLogSource& source,
estade5e5529d2015-05-21 20:59:11456 RequestPriority priority,
457 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40458 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
estade5e5529d2015-05-21 20:59:11459 source.AddToEventParameters(dict.get());
[email protected]3b04d1f22013-10-16 00:23:56460 dict->SetString("priority", RequestPriorityToString(priority));
dchengc7eeda422015-12-26 03:56:48461 return std::move(dict);
[email protected]cd565142012-06-12 16:21:45462}
[email protected]b3601bc22012-02-21 21:23:20463
[email protected]cd565142012-06-12 16:21:45464// Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
danakj22f90e72016-04-16 01:55:40465std::unique_ptr<base::Value> NetLogDnsConfigCallback(
estade5e5529d2015-05-21 20:59:11466 const DnsConfig* config,
467 NetLogCaptureMode /* capture_mode */) {
tfhef3618f2016-01-11 23:07:08468 return config->ToValue();
[email protected]cd565142012-06-12 16:21:45469}
[email protected]b4481b222012-03-16 17:13:11470
danakj22f90e72016-04-16 01:55:40471std::unique_ptr<base::Value> NetLogIPv6AvailableCallback(
estade5e5529d2015-05-21 20:59:11472 bool ipv6_available,
473 bool cached,
474 NetLogCaptureMode /* capture_mode */) {
danakj22f90e72016-04-16 01:55:40475 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
sergeyub8cdc212015-05-14 18:50:37476 dict->SetBoolean("ipv6_available", ipv6_available);
477 dict->SetBoolean("cached", cached);
dchengc7eeda422015-12-26 03:56:48478 return std::move(dict);
sergeyub8cdc212015-05-14 18:50:37479}
480
[email protected]0f292de02012-02-01 22:28:20481// The logging routines are defined here because some requests are resolved
482// without a Request object.
483
484// Logs when a request has just been started.
tfarina428341112016-09-22 13:38:20485void LogStartRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20486 const HostResolver::RequestInfo& info) {
mikecirone8b85c432016-09-08 19:11:00487 source_net_log.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST,
488 base::Bind(&NetLogRequestInfoCallback, &info));
[email protected]0f292de02012-02-01 22:28:20489}
490
491// Logs when a request has just completed (before its callback is run).
tfarina428341112016-09-22 13:38:20492void LogFinishRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20493 const HostResolver::RequestInfo& info,
[email protected]b3601bc22012-02-21 21:23:20494 int net_error) {
xunjieli26f90452014-11-10 16:23:02495 source_net_log.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00496 NetLogEventType::HOST_RESOLVER_IMPL_REQUEST, net_error);
[email protected]0f292de02012-02-01 22:28:20497}
498
499// Logs when a request has been cancelled.
tfarina428341112016-09-22 13:38:20500void LogCancelRequest(const NetLogWithSource& source_net_log,
[email protected]0f292de02012-02-01 22:28:20501 const HostResolverImpl::RequestInfo& info) {
mikecirone8b85c432016-09-08 19:11:00502 source_net_log.AddEvent(NetLogEventType::CANCELLED);
503 source_net_log.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_REQUEST);
[email protected]0f292de02012-02-01 22:28:20504}
505
[email protected]b59ff372009-07-15 22:04:32506//-----------------------------------------------------------------------------
507
[email protected]0f292de02012-02-01 22:28:20508// Keeps track of the highest priority.
509class PriorityTracker {
510 public:
[email protected]8c98d002012-07-18 19:02:27511 explicit PriorityTracker(RequestPriority initial_priority)
512 : highest_priority_(initial_priority), total_count_(0) {
[email protected]0f292de02012-02-01 22:28:20513 memset(counts_, 0, sizeof(counts_));
514 }
515
516 RequestPriority highest_priority() const {
517 return highest_priority_;
518 }
519
520 size_t total_count() const {
521 return total_count_;
522 }
523
524 void Add(RequestPriority req_priority) {
525 ++total_count_;
526 ++counts_[req_priority];
[email protected]31ae7ab2012-04-24 21:09:05527 if (highest_priority_ < req_priority)
[email protected]0f292de02012-02-01 22:28:20528 highest_priority_ = req_priority;
529 }
530
531 void Remove(RequestPriority req_priority) {
532 DCHECK_GT(total_count_, 0u);
533 DCHECK_GT(counts_[req_priority], 0u);
534 --total_count_;
535 --counts_[req_priority];
536 size_t i;
thestiga74ad2b2016-07-11 20:52:36537 for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i) {
538 }
[email protected]0f292de02012-02-01 22:28:20539 highest_priority_ = static_cast<RequestPriority>(i);
540
[email protected]31ae7ab2012-04-24 21:09:05541 // In absence of requests, default to MINIMUM_PRIORITY.
542 if (total_count_ == 0)
543 DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
[email protected]0f292de02012-02-01 22:28:20544 }
545
546 private:
547 RequestPriority highest_priority_;
548 size_t total_count_;
549 size_t counts_[NUM_PRIORITIES];
550};
551
juliatuttle9fb7aeb2016-06-06 20:16:33552void MakeNotStale(HostCache::EntryStaleness* stale_info) {
553 if (!stale_info)
554 return;
555 stale_info->expired_by = base::TimeDelta::FromSeconds(-1);
556 stale_info->network_changes = 0;
557 stale_info->stale_hits = 0;
558}
559
[email protected]c54a8912012-10-22 22:09:43560} // namespace
[email protected]0f292de02012-02-01 22:28:20561
562//-----------------------------------------------------------------------------
563
tfarina9ed7f8c52016-02-19 17:50:18564bool ResolveLocalHostname(base::StringPiece host,
565 uint16_t port,
566 AddressList* address_list) {
tfarina9ed7f8c52016-02-19 17:50:18567 address_list->clear();
568
569 bool is_local6;
570 if (!IsLocalHostname(host, &is_local6))
571 return false;
572
martijna23c8962016-03-04 18:18:51573 address_list->push_back(IPEndPoint(IPAddress::IPv6Localhost(), port));
tfarina9ed7f8c52016-02-19 17:50:18574 if (!is_local6) {
martijna23c8962016-03-04 18:18:51575 address_list->push_back(IPEndPoint(IPAddress::IPv4Localhost(), port));
tfarina9ed7f8c52016-02-19 17:50:18576 }
577
578 return true;
579}
580
[email protected]daae1322013-09-05 18:26:50581const unsigned HostResolverImpl::kMaximumDnsFailures = 16;
582
Eric Roman91aab1e62018-04-24 20:31:32583// Holds the callback and request parameters for an outstanding request.
584//
585// The RequestImpl is owned by the end user of host resolution. Deletion prior
586// to the request having completed means the request was cancelled by the
587// caller.
588//
589// Both the RequestImpl and its associated Job hold non-owning pointers to each
590// other. Care must be taken to clear the corresponding pointer when
591// cancellation is initiated by the Job (OnJobCancelled) vs by the end user
592// (~RequestImpl).
David Benjamina5677192018-04-23 23:25:42593class HostResolverImpl::RequestImpl
594 : public HostResolver::Request,
595 public base::LinkNode<HostResolverImpl::RequestImpl> {
[email protected]b59ff372009-07-15 22:04:32596 public:
tfarina428341112016-09-22 13:38:20597 RequestImpl(const NetLogWithSource& source_net_log,
maksim.sisov31452af2016-07-27 06:38:10598 const RequestInfo& info,
599 RequestPriority priority,
600 const CompletionCallback& callback,
601 AddressList* addresses,
Misha Efimovb99e7da2018-05-30 16:59:02602 Job* job,
603 base::TimeTicks request_time)
[email protected]ee094b82010-08-24 15:55:51604 : source_net_log_(source_net_log),
[email protected]54e13772009-08-14 03:01:09605 info_(info),
[email protected]5109c1952013-08-20 18:44:10606 priority_(priority),
maksim.sisov31452af2016-07-27 06:38:10607 job_(job),
[email protected]54e13772009-08-14 03:01:09608 callback_(callback),
[email protected]51b9a6b2012-06-25 21:50:29609 addresses_(addresses),
Misha Efimovb99e7da2018-05-30 16:59:02610 request_time_(request_time) {}
[email protected]b59ff372009-07-15 22:04:32611
maksim.sisov31452af2016-07-27 06:38:10612 ~RequestImpl() override;
613
614 void ChangeRequestPriority(RequestPriority priority) override;
615
616 void OnJobCancelled(Job* job) {
617 DCHECK_EQ(job_, job);
wezb9820d42016-06-22 23:41:04618 job_ = nullptr;
619 addresses_ = nullptr;
[email protected]aa22b242011-11-16 18:58:29620 callback_.Reset();
[email protected]b59ff372009-07-15 22:04:32621 }
622
[email protected]0f292de02012-02-01 22:28:20623 // Prepare final AddressList and call completion callback.
maksim.sisov31452af2016-07-27 06:38:10624 void OnJobCompleted(Job* job, int error, const AddressList& addr_list) {
625 DCHECK_EQ(job_, job);
[email protected]895123222012-10-25 15:21:17626 if (error == OK)
627 *addresses_ = EnsurePortOnAddressList(addr_list, info_.port());
maksim.sisov31452af2016-07-27 06:38:10628 job_ = nullptr;
629 addresses_ = nullptr;
630 base::ResetAndReturn(&callback_).Run(error);
[email protected]b59ff372009-07-15 22:04:32631 }
632
[email protected]b59ff372009-07-15 22:04:32633 Job* job() const {
634 return job_;
635 }
636
[email protected]0f292de02012-02-01 22:28:20637 // NetLog for the source, passed in HostResolver::Resolve.
tfarina428341112016-09-22 13:38:20638 const NetLogWithSource& source_net_log() { return source_net_log_; }
[email protected]ee094b82010-08-24 15:55:51639
[email protected]b59ff372009-07-15 22:04:32640 const RequestInfo& info() const {
641 return info_;
642 }
643
[email protected]5109c1952013-08-20 18:44:10644 RequestPriority priority() const { return priority_; }
juliatuttlec53b19a72016-05-05 13:51:31645 void set_priority(RequestPriority priority) { priority_ = priority; }
[email protected]5109c1952013-08-20 18:44:10646
647 base::TimeTicks request_time() const { return request_time_; }
[email protected]51b9a6b2012-06-25 21:50:29648
[email protected]b59ff372009-07-15 22:04:32649 private:
tfarina428341112016-09-22 13:38:20650 const NetLogWithSource source_net_log_;
[email protected]54e13772009-08-14 03:01:09651
[email protected]b59ff372009-07-15 22:04:32652 // The request info that started the request.
[email protected]5109c1952013-08-20 18:44:10653 const RequestInfo info_;
654
juliatuttlec53b19a72016-05-05 13:51:31655 RequestPriority priority_;
[email protected]b59ff372009-07-15 22:04:32656
[email protected]0f292de02012-02-01 22:28:20657 // The resolve job that this request is dependent on.
[email protected]b59ff372009-07-15 22:04:32658 Job* job_;
659
660 // The user's callback to invoke when the request completes.
[email protected]aa22b242011-11-16 18:58:29661 CompletionCallback callback_;
[email protected]b59ff372009-07-15 22:04:32662
663 // The address list to save result into.
664 AddressList* addresses_;
665
[email protected]51b9a6b2012-06-25 21:50:29666 const base::TimeTicks request_time_;
667
maksim.sisov31452af2016-07-27 06:38:10668 DISALLOW_COPY_AND_ASSIGN(RequestImpl);
[email protected]b59ff372009-07-15 22:04:32669};
670
[email protected]1e9bbd22010-10-15 16:42:45671//------------------------------------------------------------------------------
672
Francois Doraya2d01ba2017-09-25 19:17:40673// Calls HostResolverProc in TaskScheduler. Performs retries if necessary.
[email protected]0f292de02012-02-01 22:28:20674//
Miriam Gershenson02592182018-03-22 17:42:37675// In non-test code, the HostResolverProc is always SystemHostResolverProc,
676// which calls a platform API that implements host resolution.
677//
[email protected]0f292de02012-02-01 22:28:20678// Whenever we try to resolve the host, we post a delayed task to check if host
679// resolution (OnLookupComplete) is completed or not. If the original attempt
680// hasn't completed, then we start another attempt for host resolution. We take
681// the results from the first attempt that finishes and ignore the results from
682// all other attempts.
683//
684// TODO(szym): Move to separate source file for testing and mocking.
685//
686class HostResolverImpl::ProcTask
687 : public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> {
[email protected]b59ff372009-07-15 22:04:32688 public:
[email protected]b3601bc22012-02-21 21:23:20689 typedef base::Callback<void(int net_error,
690 const AddressList& addr_list)> Callback;
[email protected]b59ff372009-07-15 22:04:32691
[email protected]0f292de02012-02-01 22:28:20692 ProcTask(const Key& key,
693 const ProcTaskParams& params,
694 const Callback& callback,
Miriam Gershensone42adb22017-10-16 16:19:38695 scoped_refptr<base::TaskRunner> proc_task_runner,
Misha Efimovb99e7da2018-05-30 16:59:02696 const NetLogWithSource& job_net_log,
697 const base::TickClock* tick_clock)
[email protected]0f292de02012-02-01 22:28:20698 : key_(key),
699 params_(params),
700 callback_(callback),
mmenke91c17162016-06-02 16:03:23701 network_task_runner_(base::ThreadTaskRunnerHandle::Get()),
Miriam Gershensone42adb22017-10-16 16:19:38702 proc_task_runner_(std::move(proc_task_runner)),
[email protected]0f292de02012-02-01 22:28:20703 attempt_number_(0),
704 completed_attempt_number_(0),
705 completed_attempt_error_(ERR_UNEXPECTED),
Misha Efimovb99e7da2018-05-30 16:59:02706 net_log_(job_net_log),
707 tick_clock_(tick_clock) {
[email protected]90499482013-06-01 00:39:50708 if (!params_.resolver_proc.get())
[email protected]0f292de02012-02-01 22:28:20709 params_.resolver_proc = HostResolverProc::GetDefault();
710 // If default is unset, use the system proc.
[email protected]90499482013-06-01 00:39:50711 if (!params_.resolver_proc.get())
[email protected]1ee9afa12013-04-16 14:18:06712 params_.resolver_proc = new SystemHostResolverProc();
[email protected]b59ff372009-07-15 22:04:32713 }
714
[email protected]b59ff372009-07-15 22:04:32715 void Start() {
mmenke91c17162016-06-02 16:03:23716 DCHECK(network_task_runner_->BelongsToCurrentThread());
mikecirone8b85c432016-09-08 19:11:00717 net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK);
[email protected]189163e2011-05-11 01:48:54718 StartLookupAttempt();
719 }
[email protected]252b699b2010-02-05 21:38:06720
[email protected]0f292de02012-02-01 22:28:20721 // Cancels this ProcTask. It will be orphaned. Any outstanding resolve
mmenke91c17162016-06-02 16:03:23722 // attempts running on worker thread will continue running. Only once all the
[email protected]0f292de02012-02-01 22:28:20723 // attempts complete will the final reference to this ProcTask be released.
724 void Cancel() {
mmenke91c17162016-06-02 16:03:23725 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0f292de02012-02-01 22:28:20726
[email protected]0adcb2b2012-08-15 21:30:46727 if (was_canceled() || was_completed())
[email protected]0f292de02012-02-01 22:28:20728 return;
729
[email protected]0f292de02012-02-01 22:28:20730 callback_.Reset();
mikecirone8b85c432016-09-08 19:11:00731 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK);
[email protected]0f292de02012-02-01 22:28:20732 }
733
[email protected]0f292de02012-02-01 22:28:20734 bool was_canceled() const {
mmenke91c17162016-06-02 16:03:23735 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0f292de02012-02-01 22:28:20736 return callback_.is_null();
737 }
738
739 bool was_completed() const {
mmenke91c17162016-06-02 16:03:23740 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0f292de02012-02-01 22:28:20741 return completed_attempt_number_ > 0;
742 }
743
744 private:
[email protected]a9813302012-04-28 09:29:28745 friend class base::RefCountedThreadSafe<ProcTask>;
Chris Watkins68b15032017-12-01 03:07:13746 ~ProcTask() = default;
[email protected]a9813302012-04-28 09:29:28747
[email protected]189163e2011-05-11 01:48:54748 void StartLookupAttempt() {
mmenke91c17162016-06-02 16:03:23749 DCHECK(network_task_runner_->BelongsToCurrentThread());
Misha Efimovb99e7da2018-05-30 16:59:02750 base::TimeTicks start_time = tick_clock_->NowTicks();
[email protected]189163e2011-05-11 01:48:54751 ++attempt_number_;
752 // Dispatch the lookup attempt to a worker thread.
Miriam Gershensone42adb22017-10-16 16:19:38753 proc_task_runner_->PostTask(
Francois Doraya2d01ba2017-09-25 19:17:40754 FROM_HERE,
Francois Doraya2d01ba2017-09-25 19:17:40755 base::Bind(&ProcTask::DoLookup, this, start_time, attempt_number_));
[email protected]13024882011-05-18 23:19:16756
mikecirone8b85c432016-09-08 19:11:00757 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_ATTEMPT_STARTED,
tfarina5e24b242015-10-27 13:11:28758 NetLog::IntCallback("attempt_number", attempt_number_));
[email protected]13024882011-05-18 23:19:16759
mmenke91c17162016-06-02 16:03:23760 // If the results aren't received within a given time, RetryIfNotComplete
761 // will start a new attempt if none of the outstanding attempts have
762 // completed yet.
[email protected]0f292de02012-02-01 22:28:20763 if (attempt_number_ <= params_.max_retry_attempts) {
mmenke91c17162016-06-02 16:03:23764 network_task_runner_->PostDelayedTask(
765 FROM_HERE, base::Bind(&ProcTask::RetryIfNotComplete, this),
[email protected]7e560102012-03-08 20:58:42766 params_.unresponsive_delay);
[email protected]06ef6d92011-05-19 04:24:58767 }
[email protected]b59ff372009-07-15 22:04:32768 }
769
Francois Doraya2d01ba2017-09-25 19:17:40770 // WARNING: This code runs in TaskScheduler with CONTINUE_ON_SHUTDOWN. The
771 // shutdown code cannot wait for it to finish, so this code must be very
772 // careful about using other objects (like MessageLoops, Singletons, etc).
773 // During shutdown these objects may no longer exist. Multiple DoLookups()
774 // could be running in parallel, so any state inside of |this| must not
775 // mutate.
[email protected]189163e2011-05-11 01:48:54776 void DoLookup(const base::TimeTicks& start_time,
Avi Drissman13fc8932015-12-20 04:40:46777 const uint32_t attempt_number) {
[email protected]189163e2011-05-11 01:48:54778 AddressList results;
779 int os_error = 0;
[email protected]0f292de02012-02-01 22:28:20780 int error = params_.resolver_proc->Resolve(key_.hostname,
781 key_.address_family,
782 key_.host_resolver_flags,
783 &results,
784 &os_error);
[email protected]b59ff372009-07-15 22:04:32785
mmenke91c17162016-06-02 16:03:23786 network_task_runner_->PostTask(
787 FROM_HERE, base::Bind(&ProcTask::OnLookupComplete, this, results,
788 start_time, attempt_number, error, os_error));
[email protected]189163e2011-05-11 01:48:54789 }
790
mmenke91c17162016-06-02 16:03:23791 // Makes next attempt if DoLookup() has not finished.
[email protected]0f292de02012-02-01 22:28:20792 void RetryIfNotComplete() {
mmenke91c17162016-06-02 16:03:23793 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]189163e2011-05-11 01:48:54794
[email protected]0f292de02012-02-01 22:28:20795 if (was_completed() || was_canceled())
[email protected]189163e2011-05-11 01:48:54796 return;
797
[email protected]0f292de02012-02-01 22:28:20798 params_.unresponsive_delay *= params_.retry_factor;
[email protected]189163e2011-05-11 01:48:54799 StartLookupAttempt();
[email protected]b59ff372009-07-15 22:04:32800 }
801
anujk.sharma7c907f02015-05-29 02:55:44802 // Callback for when DoLookup() completes (runs on task runner thread).
[email protected]189163e2011-05-11 01:48:54803 void OnLookupComplete(const AddressList& results,
804 const base::TimeTicks& start_time,
Avi Drissman13fc8932015-12-20 04:40:46805 const uint32_t attempt_number,
[email protected]189163e2011-05-11 01:48:54806 int error,
807 const int os_error) {
xunjieli0b7f5b62016-12-06 20:43:48808 TRACE_EVENT0(kNetTracingCategory, "ProcTask::OnLookupComplete");
mmenke91c17162016-06-02 16:03:23809 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]49b70b222013-05-07 21:24:23810 // If results are empty, we should return an error.
811 bool empty_list_on_ok = (error == OK && results.empty());
[email protected]49b70b222013-05-07 21:24:23812 if (empty_list_on_ok)
813 error = ERR_NAME_NOT_RESOLVED;
[email protected]189163e2011-05-11 01:48:54814
815 bool was_retry_attempt = attempt_number > 1;
816
[email protected]2d3b7762010-10-09 00:35:47817 // Ideally the following code would be part of host_resolver_proc.cc,
[email protected]b3601bc22012-02-21 21:23:20818 // however it isn't safe to call NetworkChangeNotifier from worker threads.
mmenke91c17162016-06-02 16:03:23819 // So do it here on the IO thread instead.
[email protected]189163e2011-05-11 01:48:54820 if (error != OK && NetworkChangeNotifier::IsOffline())
821 error = ERR_INTERNET_DISCONNECTED;
[email protected]2d3b7762010-10-09 00:35:47822
[email protected]189163e2011-05-11 01:48:54823 RecordAttemptHistograms(start_time, attempt_number, error, os_error);
[email protected]f2d8c4212010-02-02 00:56:35824
[email protected]0f292de02012-02-01 22:28:20825 if (was_canceled())
[email protected]b59ff372009-07-15 22:04:32826 return;
827
mikecironef22f9812016-10-04 03:40:19828 NetLogParametersCallback net_log_callback;
[email protected]0f292de02012-02-01 22:28:20829 if (error != OK) {
[email protected]cd565142012-06-12 16:21:45830 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
831 attempt_number,
832 error,
833 os_error);
[email protected]0f292de02012-02-01 22:28:20834 } else {
tfarina5e24b242015-10-27 13:11:28835 net_log_callback = NetLog::IntCallback("attempt_number", attempt_number);
[email protected]0f292de02012-02-01 22:28:20836 }
mikecirone8b85c432016-09-08 19:11:00837 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_ATTEMPT_FINISHED,
[email protected]cd565142012-06-12 16:21:45838 net_log_callback);
[email protected]0f292de02012-02-01 22:28:20839
840 if (was_completed())
841 return;
842
Miriam Gershenson61604662017-09-28 23:51:11843 RecordTaskHistograms(start_time, error, os_error);
844
[email protected]0f292de02012-02-01 22:28:20845 // Copy the results from the first worker thread that resolves the host.
846 results_ = results;
847 completed_attempt_number_ = attempt_number;
848 completed_attempt_error_ = error;
849
[email protected]e87b8b512011-06-14 22:12:52850 if (was_retry_attempt) {
851 // If retry attempt finishes before 1st attempt, then get stats on how
852 // much time is saved by having spawned an extra attempt.
Misha Efimovb99e7da2018-05-30 16:59:02853 retry_attempt_finished_time_ = tick_clock_->NowTicks();
[email protected]e87b8b512011-06-14 22:12:52854 }
855
[email protected]189163e2011-05-11 01:48:54856 if (error != OK) {
[email protected]cd565142012-06-12 16:21:45857 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
858 0, error, os_error);
[email protected]ee094b82010-08-24 15:55:51859 } else {
[email protected]cd565142012-06-12 16:21:45860 net_log_callback = results_.CreateNetLogCallback();
[email protected]ee094b82010-08-24 15:55:51861 }
mikecirone8b85c432016-09-08 19:11:00862 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_PROC_TASK,
[email protected]cd565142012-06-12 16:21:45863 net_log_callback);
[email protected]ee094b82010-08-24 15:55:51864
[email protected]b3601bc22012-02-21 21:23:20865 callback_.Run(error, results_);
[email protected]b59ff372009-07-15 22:04:32866 }
867
Miriam Gershenson61604662017-09-28 23:51:11868 void RecordTaskHistograms(const base::TimeTicks& start_time,
869 const int error,
870 const int os_error) const {
mmenke91c17162016-06-02 16:03:23871 DCHECK(network_task_runner_->BelongsToCurrentThread());
Misha Efimovb99e7da2018-05-30 16:59:02872 base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
Miriam Gershenson61604662017-09-28 23:51:11873 if (error == OK)
874 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ProcTask.SuccessTime", duration);
875 else
876 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ProcTask.FailureTime", duration);
[email protected]7e96d792011-06-10 17:08:23877
Miriam Gershenson61604662017-09-28 23:51:11878 UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName,
879 std::abs(os_error),
880 GetAllGetAddrinfoOSErrors());
[email protected]1e9bbd22010-10-15 16:42:45881 }
882
[email protected]189163e2011-05-11 01:48:54883 void RecordAttemptHistograms(const base::TimeTicks& start_time,
Avi Drissman13fc8932015-12-20 04:40:46884 const uint32_t attempt_number,
[email protected]189163e2011-05-11 01:48:54885 const int error,
886 const int os_error) const {
mmenke91c17162016-06-02 16:03:23887 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]189163e2011-05-11 01:48:54888 bool first_attempt_to_complete =
889 completed_attempt_number_ == attempt_number;
[email protected]e87b8b512011-06-14 22:12:52890 bool is_first_attempt = (attempt_number == 1);
[email protected]1e9bbd22010-10-15 16:42:45891
[email protected]189163e2011-05-11 01:48:54892 if (first_attempt_to_complete) {
893 // If this was first attempt to complete, then record the resolution
894 // status of the attempt.
895 if (completed_attempt_error_ == OK) {
896 UMA_HISTOGRAM_ENUMERATION(
897 "DNS.AttemptFirstSuccess", attempt_number, 100);
898 } else {
899 UMA_HISTOGRAM_ENUMERATION(
900 "DNS.AttemptFirstFailure", attempt_number, 100);
901 }
902 }
903
904 if (error == OK)
905 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100);
906 else
907 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100);
908
[email protected]e87b8b512011-06-14 22:12:52909 // If first attempt didn't finish before retry attempt, then calculate stats
910 // on how much time is saved by having spawned an extra attempt.
[email protected]0f292de02012-02-01 22:28:20911 if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21912 UMA_HISTOGRAM_LONG_TIMES_100(
913 "DNS.AttemptTimeSavedByRetry",
Misha Efimovb99e7da2018-05-30 16:59:02914 tick_clock_->NowTicks() - retry_attempt_finished_time_);
[email protected]e87b8b512011-06-14 22:12:52915 }
916
[email protected]0f292de02012-02-01 22:28:20917 if (was_canceled() || !first_attempt_to_complete) {
[email protected]189163e2011-05-11 01:48:54918 // Count those attempts which completed after the job was already canceled
919 // OR after the job was already completed by an earlier attempt (so in
920 // effect).
921 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100);
922
[email protected]0f292de02012-02-01 22:28:20923 // Record if job is canceled.
924 if (was_canceled())
[email protected]189163e2011-05-11 01:48:54925 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100);
926 }
927
Misha Efimovb99e7da2018-05-30 16:59:02928 base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
[email protected]189163e2011-05-11 01:48:54929 if (error == OK)
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21930 UMA_HISTOGRAM_LONG_TIMES_100("DNS.AttemptSuccessDuration", duration);
[email protected]189163e2011-05-11 01:48:54931 else
Miriam Gershenson4e7c9ecf2017-09-26 23:17:21932 UMA_HISTOGRAM_LONG_TIMES_100("DNS.AttemptFailDuration", duration);
[email protected]189163e2011-05-11 01:48:54933 }
[email protected]1e9bbd22010-10-15 16:42:45934
anujk.sharma7c907f02015-05-29 02:55:44935 // Set on the task runner thread, read on the worker thread.
[email protected]123ab1e32009-10-21 19:12:57936 Key key_;
[email protected]b59ff372009-07-15 22:04:32937
[email protected]0f292de02012-02-01 22:28:20938 // Holds an owning reference to the HostResolverProc that we are going to use.
[email protected]b59ff372009-07-15 22:04:32939 // This may not be the current resolver procedure by the time we call
940 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
941 // reference ensures that it remains valid until we are done.
[email protected]0f292de02012-02-01 22:28:20942 ProcTaskParams params_;
[email protected]b59ff372009-07-15 22:04:32943
[email protected]0f292de02012-02-01 22:28:20944 // The listener to the results of this ProcTask.
945 Callback callback_;
946
mmenke91c17162016-06-02 16:03:23947 // Used to post events onto the network thread.
948 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
Miriam Gershensone42adb22017-10-16 16:19:38949 // Used to post blocking HostResolverProc tasks.
950 scoped_refptr<base::TaskRunner> proc_task_runner_;
[email protected]189163e2011-05-11 01:48:54951
952 // Keeps track of the number of attempts we have made so far to resolve the
953 // host. Whenever we start an attempt to resolve the host, we increase this
954 // number.
Avi Drissman13fc8932015-12-20 04:40:46955 uint32_t attempt_number_;
[email protected]189163e2011-05-11 01:48:54956
957 // The index of the attempt which finished first (or 0 if the job is still in
958 // progress).
Avi Drissman13fc8932015-12-20 04:40:46959 uint32_t completed_attempt_number_;
[email protected]189163e2011-05-11 01:48:54960
961 // The result (a net error code) from the first attempt to complete.
962 int completed_attempt_error_;
[email protected]252b699b2010-02-05 21:38:06963
[email protected]e87b8b512011-06-14 22:12:52964 // The time when retry attempt was finished.
965 base::TimeTicks retry_attempt_finished_time_;
966
[email protected]b59ff372009-07-15 22:04:32967 AddressList results_;
968
tfarina428341112016-09-22 13:38:20969 NetLogWithSource net_log_;
[email protected]ee094b82010-08-24 15:55:51970
Misha Efimovb99e7da2018-05-30 16:59:02971 const base::TickClock* tick_clock_;
972
[email protected]0f292de02012-02-01 22:28:20973 DISALLOW_COPY_AND_ASSIGN(ProcTask);
[email protected]b59ff372009-07-15 22:04:32974};
975
976//-----------------------------------------------------------------------------
977
Miriam Gershenson02592182018-03-22 17:42:37978// Resolves the hostname using DnsTransaction, which is a full implementation of
979// a DNS stub resolver. One DnsTransaction is created for each resolution
980// needed, which for AF_UNSPEC resolutions includes both A and AAAA. The
981// transactions are scheduled separately and started separately.
982//
[email protected]b3601bc22012-02-21 21:23:20983// TODO(szym): This could be moved to separate source file as well.
[email protected]0adcb2b2012-08-15 21:30:46984class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> {
[email protected]b3601bc22012-02-21 21:23:20985 public:
[email protected]daae1322013-09-05 18:26:50986 class Delegate {
987 public:
988 virtual void OnDnsTaskComplete(base::TimeTicks start_time,
989 int net_error,
990 const AddressList& addr_list,
991 base::TimeDelta ttl) = 0;
992
993 // Called when the first of two jobs succeeds. If the first completed
994 // transaction fails, this is not called. Also not called when the DnsTask
995 // only needs to run one transaction.
996 virtual void OnFirstDnsTransactionComplete() = 0;
997
Brad Lassey786929ad2018-02-21 20:54:27998 virtual URLRequestContext* url_request_context() = 0;
999 virtual RequestPriority priority() const = 0;
1000
[email protected]daae1322013-09-05 18:26:501001 protected:
Chris Watkins68b15032017-12-01 03:07:131002 Delegate() = default;
1003 virtual ~Delegate() = default;
[email protected]daae1322013-09-05 18:26:501004 };
[email protected]b3601bc22012-02-21 21:23:201005
[email protected]0adcb2b2012-08-15 21:30:461006 DnsTask(DnsClient* client,
[email protected]b3601bc22012-02-21 21:23:201007 const Key& key,
[email protected]daae1322013-09-05 18:26:501008 Delegate* delegate,
Misha Efimovb99e7da2018-05-30 16:59:021009 const NetLogWithSource& job_net_log,
1010 const base::TickClock* tick_clock)
[email protected]0adcb2b2012-08-15 21:30:461011 : client_(client),
[email protected]daae1322013-09-05 18:26:501012 key_(key),
1013 delegate_(delegate),
1014 net_log_(job_net_log),
1015 num_completed_transactions_(0),
Misha Efimovb99e7da2018-05-30 16:59:021016 tick_clock_(tick_clock),
1017 task_start_time_(tick_clock_->NowTicks()) {
[email protected]0adcb2b2012-08-15 21:30:461018 DCHECK(client);
[email protected]daae1322013-09-05 18:26:501019 DCHECK(delegate_);
[email protected]1affed62013-08-21 03:24:501020 }
1021
[email protected]daae1322013-09-05 18:26:501022 bool needs_two_transactions() const {
1023 return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED;
1024 }
1025
1026 bool needs_another_transaction() const {
1027 return needs_two_transactions() && !transaction_aaaa_;
1028 }
1029
1030 void StartFirstTransaction() {
1031 DCHECK_EQ(0u, num_completed_transactions_);
mikecirone8b85c432016-09-08 19:11:001032 net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK);
[email protected]daae1322013-09-05 18:26:501033 if (key_.address_family == ADDRESS_FAMILY_IPV6) {
1034 StartAAAA();
1035 } else {
1036 StartA();
1037 }
1038 }
1039
1040 void StartSecondTransaction() {
1041 DCHECK(needs_two_transactions());
1042 StartAAAA();
[email protected]70c04ab2013-08-22 16:05:121043 }
1044
Brad Lassey2e8f185d2018-05-21 22:25:211045 base::TimeDelta ttl() { return ttl_; }
1046
[email protected]70c04ab2013-08-22 16:05:121047 private:
[email protected]daae1322013-09-05 18:26:501048 void StartA() {
1049 DCHECK(!transaction_a_);
1050 DCHECK_NE(ADDRESS_FAMILY_IPV6, key_.address_family);
1051 transaction_a_ = CreateTransaction(ADDRESS_FAMILY_IPV4);
1052 transaction_a_->Start();
1053 }
1054
1055 void StartAAAA() {
1056 DCHECK(!transaction_aaaa_);
1057 DCHECK_NE(ADDRESS_FAMILY_IPV4, key_.address_family);
1058 transaction_aaaa_ = CreateTransaction(ADDRESS_FAMILY_IPV6);
1059 transaction_aaaa_->Start();
1060 }
1061
danakj22f90e72016-04-16 01:55:401062 std::unique_ptr<DnsTransaction> CreateTransaction(AddressFamily family) {
[email protected]daae1322013-09-05 18:26:501063 DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family);
Brad Lassey786929ad2018-02-21 20:54:271064 std::unique_ptr<DnsTransaction> trans =
1065 client_->GetTransactionFactory()->CreateTransaction(
1066 key_.hostname,
1067 family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA
1068 : dns_protocol::kTypeA,
Brad Lasseydba0a84b2018-02-23 22:18:531069 base::BindOnce(&DnsTask::OnTransactionComplete,
Misha Efimovb99e7da2018-05-30 16:59:021070 base::Unretained(this), tick_clock_->NowTicks()),
Brad Lassey786929ad2018-02-21 20:54:271071 net_log_);
1072 trans->SetRequestContext(delegate_->url_request_context());
1073 trans->SetRequestPriority(delegate_->priority());
1074 return trans;
[email protected]daae1322013-09-05 18:26:501075 }
1076
1077 void OnTransactionComplete(const base::TimeTicks& start_time,
[email protected]1def74c2012-03-22 20:07:001078 DnsTransaction* transaction,
[email protected]b3601bc22012-02-21 21:23:201079 int net_error,
1080 const DnsResponse* response) {
[email protected]add76532012-03-30 14:47:471081 DCHECK(transaction);
Misha Efimovb99e7da2018-05-30 16:59:021082 base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
Brad Lassey2e8f185d2018-05-21 22:25:211083 if (net_error != OK && !(net_error == ERR_NAME_NOT_RESOLVED && response &&
1084 response->IsValid())) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211085 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionFailure", duration);
[email protected]0adcb2b2012-08-15 21:30:461086 OnFailure(net_error, DnsResponse::DNS_PARSE_OK);
1087 return;
[email protected]6c411902012-08-14 22:36:361088 }
[email protected]0adcb2b2012-08-15 21:30:461089
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211090 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionSuccess", duration);
[email protected]02cd6982013-01-10 20:12:511091 switch (transaction->GetType()) {
1092 case dns_protocol::kTypeA:
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211093 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionSuccess_A", duration);
[email protected]02cd6982013-01-10 20:12:511094 break;
1095 case dns_protocol::kTypeAAAA:
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211096 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.TransactionSuccess_AAAA",
1097 duration);
[email protected]02cd6982013-01-10 20:12:511098 break;
1099 }
[email protected]daae1322013-09-05 18:26:501100
[email protected]0adcb2b2012-08-15 21:30:461101 AddressList addr_list;
1102 base::TimeDelta ttl;
1103 DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl);
1104 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
1105 result,
1106 DnsResponse::DNS_PARSE_RESULT_MAX);
1107 if (result != DnsResponse::DNS_PARSE_OK) {
1108 // Fail even if the other query succeeds.
1109 OnFailure(ERR_DNS_MALFORMED_RESPONSE, result);
1110 return;
1111 }
1112
[email protected]daae1322013-09-05 18:26:501113 ++num_completed_transactions_;
1114 if (num_completed_transactions_ == 1) {
1115 ttl_ = ttl;
[email protected]0adcb2b2012-08-15 21:30:461116 } else {
[email protected]daae1322013-09-05 18:26:501117 ttl_ = std::min(ttl_, ttl);
[email protected]0adcb2b2012-08-15 21:30:461118 }
1119
[email protected]daae1322013-09-05 18:26:501120 if (transaction->GetType() == dns_protocol::kTypeA) {
1121 DCHECK_EQ(transaction_a_.get(), transaction);
1122 // Place IPv4 addresses after IPv6.
1123 addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end());
1124 } else {
1125 DCHECK_EQ(transaction_aaaa_.get(), transaction);
1126 // Place IPv6 addresses before IPv4.
1127 addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end());
1128 }
1129
1130 if (needs_two_transactions() && num_completed_transactions_ == 1) {
1131 // No need to repeat the suffix search.
1132 key_.hostname = transaction->GetHostname();
1133 delegate_->OnFirstDnsTransactionComplete();
1134 return;
1135 }
1136
1137 if (addr_list_.empty()) {
[email protected]70c04ab2013-08-22 16:05:121138 // TODO(szym): Don't fallback to ProcTask in this case.
1139 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
[email protected]0adcb2b2012-08-15 21:30:461140 return;
1141 }
1142
[email protected]daae1322013-09-05 18:26:501143 // If there are multiple addresses, and at least one is IPv6, need to sort
1144 // them. Note that IPv6 addresses are always put before IPv4 ones, so it's
1145 // sufficient to just check the family of the first address.
1146 if (addr_list_.size() > 1 &&
1147 addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) {
1148 // Sort addresses if needed. Sort could complete synchronously.
[email protected]0adcb2b2012-08-15 21:30:461149 client_->GetAddressSorter()->Sort(
Misha Efimovb99e7da2018-05-30 16:59:021150 addr_list_, base::Bind(&DnsTask::OnSortComplete, AsWeakPtr(),
1151 tick_clock_->NowTicks()));
[email protected]0adcb2b2012-08-15 21:30:461152 } else {
[email protected]daae1322013-09-05 18:26:501153 OnSuccess(addr_list_);
[email protected]0adcb2b2012-08-15 21:30:461154 }
1155 }
1156
1157 void OnSortComplete(base::TimeTicks start_time,
[email protected]0adcb2b2012-08-15 21:30:461158 bool success,
1159 const AddressList& addr_list) {
1160 if (!success) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211161 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.SortFailure",
Misha Efimovb99e7da2018-05-30 16:59:021162 tick_clock_->NowTicks() - start_time);
[email protected]0adcb2b2012-08-15 21:30:461163 OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK);
1164 return;
1165 }
1166
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211167 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.SortSuccess",
Misha Efimovb99e7da2018-05-30 16:59:021168 tick_clock_->NowTicks() - start_time);
[email protected]0adcb2b2012-08-15 21:30:461169
1170 // AddressSorter prunes unusable destinations.
1171 if (addr_list.empty()) {
1172 LOG(WARNING) << "Address list empty after RFC3484 sort";
1173 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
1174 return;
1175 }
1176
[email protected]daae1322013-09-05 18:26:501177 OnSuccess(addr_list);
[email protected]0adcb2b2012-08-15 21:30:461178 }
1179
1180 void OnFailure(int net_error, DnsResponse::Result result) {
1181 DCHECK_NE(OK, net_error);
[email protected]cd565142012-06-12 16:21:451182 net_log_.EndEvent(
mikecirone8b85c432016-09-08 19:11:001183 NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK,
[email protected]cd565142012-06-12 16:21:451184 base::Bind(&NetLogDnsTaskFailedCallback, net_error, result));
Brad Lassey2e8f185d2018-05-21 22:25:211185 base::TimeDelta ttl = ttl_ < base::TimeDelta::FromSeconds(
1186 std::numeric_limits<uint32_t>::max()) &&
1187 num_completed_transactions_ > 0
1188 ? ttl_
1189 : base::TimeDelta::FromSeconds(0);
Miriam Gershenson66024d72017-12-05 04:30:321190 delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(),
Brad Lassey2e8f185d2018-05-21 22:25:211191 ttl);
[email protected]b3601bc22012-02-21 21:23:201192 }
1193
[email protected]daae1322013-09-05 18:26:501194 void OnSuccess(const AddressList& addr_list) {
mikecirone8b85c432016-09-08 19:11:001195 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_DNS_TASK,
[email protected]0adcb2b2012-08-15 21:30:461196 addr_list.CreateNetLogCallback());
[email protected]daae1322013-09-05 18:26:501197 delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_);
[email protected]0adcb2b2012-08-15 21:30:461198 }
1199
1200 DnsClient* client_;
[email protected]daae1322013-09-05 18:26:501201 Key key_;
1202
[email protected]b3601bc22012-02-21 21:23:201203 // The listener to the results of this DnsTask.
[email protected]daae1322013-09-05 18:26:501204 Delegate* delegate_;
tfarina428341112016-09-22 13:38:201205 const NetLogWithSource net_log_;
[email protected]b3601bc22012-02-21 21:23:201206
danakj22f90e72016-04-16 01:55:401207 std::unique_ptr<DnsTransaction> transaction_a_;
1208 std::unique_ptr<DnsTransaction> transaction_aaaa_;
[email protected]0adcb2b2012-08-15 21:30:461209
[email protected]daae1322013-09-05 18:26:501210 unsigned num_completed_transactions_;
1211
1212 // These are updated as each transaction completes.
1213 base::TimeDelta ttl_;
1214 // IPv6 addresses must appear first in the list.
1215 AddressList addr_list_;
1216
Misha Efimovb99e7da2018-05-30 16:59:021217 const base::TickClock* tick_clock_;
[email protected]daae1322013-09-05 18:26:501218 base::TimeTicks task_start_time_;
[email protected]0adcb2b2012-08-15 21:30:461219
1220 DISALLOW_COPY_AND_ASSIGN(DnsTask);
[email protected]b3601bc22012-02-21 21:23:201221};
1222
1223//-----------------------------------------------------------------------------
1224
[email protected]0f292de02012-02-01 22:28:201225// Aggregates all Requests for the same Key. Dispatched via PriorityDispatch.
[email protected]daae1322013-09-05 18:26:501226class HostResolverImpl::Job : public PrioritizedDispatcher::Job,
1227 public HostResolverImpl::DnsTask::Delegate {
[email protected]68ad3ee2010-01-30 03:45:391228 public:
[email protected]0f292de02012-02-01 22:28:201229 // Creates new job for |key| where |request_net_log| is bound to the
[email protected]16ee26d2012-03-08 03:34:351230 // request that spawned it.
[email protected]12faa4c2012-11-06 04:44:181231 Job(const base::WeakPtr<HostResolverImpl>& resolver,
[email protected]0f292de02012-02-01 22:28:201232 const Key& key,
[email protected]8c98d002012-07-18 19:02:271233 RequestPriority priority,
Miriam Gershensone42adb22017-10-16 16:19:381234 scoped_refptr<base::TaskRunner> proc_task_runner,
Misha Efimovb99e7da2018-05-30 16:59:021235 const NetLogWithSource& source_net_log,
1236 const base::TickClock* tick_clock)
[email protected]12faa4c2012-11-06 04:44:181237 : resolver_(resolver),
[email protected]0f292de02012-02-01 22:28:201238 key_(key),
[email protected]8c98d002012-07-18 19:02:271239 priority_tracker_(priority),
Miriam Gershensone42adb22017-10-16 16:19:381240 proc_task_runner_(std::move(proc_task_runner)),
[email protected]0f292de02012-02-01 22:28:201241 had_non_speculative_request_(false),
[email protected]51b9a6b2012-06-25 21:50:291242 had_dns_config_(false),
[email protected]daae1322013-09-05 18:26:501243 num_occupied_job_slots_(0),
[email protected]1d932852012-06-19 19:40:331244 dns_task_error_(OK),
Misha Efimovb99e7da2018-05-30 16:59:021245 tick_clock_(tick_clock),
1246 creation_time_(tick_clock_->NowTicks()),
[email protected]51b9a6b2012-06-25 21:50:291247 priority_change_time_(creation_time_),
tfarina428341112016-09-22 13:38:201248 net_log_(
1249 NetLogWithSource::Make(source_net_log.net_log(),
mikecirone8b85c432016-09-08 19:11:001250 NetLogSourceType::HOST_RESOLVER_IMPL_JOB)) {
1251 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_CREATE_JOB);
[email protected]0f292de02012-02-01 22:28:201252
mikecirone8b85c432016-09-08 19:11:001253 net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
1254 base::Bind(&NetLogJobCreationCallback,
1255 source_net_log.source(), &key_.hostname));
[email protected]68ad3ee2010-01-30 03:45:391256 }
1257
dchengb03027d2014-10-21 12:00:201258 ~Job() override {
[email protected]b3601bc22012-02-21 21:23:201259 if (is_running()) {
1260 // |resolver_| was destroyed with this Job still in flight.
1261 // Clean-up, record in the log, but don't run any callbacks.
1262 if (is_proc_running()) {
[email protected]0f292de02012-02-01 22:28:201263 proc_task_->Cancel();
wezb9820d42016-06-22 23:41:041264 proc_task_ = nullptr;
[email protected]0f292de02012-02-01 22:28:201265 }
[email protected]16ee26d2012-03-08 03:34:351266 // Clean up now for nice NetLog.
[email protected]daae1322013-09-05 18:26:501267 KillDnsTask();
mikecirone8b85c432016-09-08 19:11:001268 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
[email protected]b3601bc22012-02-21 21:23:201269 ERR_ABORTED);
1270 } else if (is_queued()) {
[email protected]57a48d32012-03-03 00:04:551271 // |resolver_| was destroyed without running this Job.
[email protected]16ee26d2012-03-08 03:34:351272 // TODO(szym): is there any benefit in having this distinction?
mikecirone8b85c432016-09-08 19:11:001273 net_log_.AddEvent(NetLogEventType::CANCELLED);
1274 net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB);
[email protected]68ad3ee2010-01-30 03:45:391275 }
[email protected]b3601bc22012-02-21 21:23:201276 // else CompleteRequests logged EndEvent.
David Benjamina5677192018-04-23 23:25:421277 while (!requests_.empty()) {
maksim.sisov31452af2016-07-27 06:38:101278 // Log any remaining Requests as cancelled.
David Benjamina5677192018-04-23 23:25:421279 RequestImpl* req = requests_.head()->value();
1280 req->RemoveFromList();
1281 DCHECK_EQ(this, req->job());
1282 LogCancelRequest(req->source_net_log(), req->info());
1283 req->OnJobCancelled(this);
[email protected]b3601bc22012-02-21 21:23:201284 }
[email protected]68ad3ee2010-01-30 03:45:391285 }
1286
[email protected]daae1322013-09-05 18:26:501287 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1288 // of the queue.
1289 void Schedule(bool at_head) {
1290 DCHECK(!is_queued());
1291 PrioritizedDispatcher::Handle handle;
1292 if (!at_head) {
[email protected]106ccd2c2014-06-17 09:21:001293 handle = resolver_->dispatcher_->Add(this, priority());
[email protected]daae1322013-09-05 18:26:501294 } else {
[email protected]106ccd2c2014-06-17 09:21:001295 handle = resolver_->dispatcher_->AddAtHead(this, priority());
[email protected]daae1322013-09-05 18:26:501296 }
1297 // The dispatcher could have started |this| in the above call to Add, which
1298 // could have called Schedule again. In that case |handle| will be null,
1299 // but |handle_| may have been set by the other nested call to Schedule.
1300 if (!handle.is_null()) {
1301 DCHECK(handle_.is_null());
1302 handle_ = handle;
1303 }
[email protected]16ee26d2012-03-08 03:34:351304 }
1305
maksim.sisov31452af2016-07-27 06:38:101306 void AddRequest(RequestImpl* request) {
1307 DCHECK_EQ(key_.hostname, request->info().hostname());
[email protected]0f292de02012-02-01 22:28:201308
maksim.sisov31452af2016-07-27 06:38:101309 priority_tracker_.Add(request->priority());
[email protected]0f292de02012-02-01 22:28:201310
maksim.sisov31452af2016-07-27 06:38:101311 request->source_net_log().AddEvent(
mikecirone8b85c432016-09-08 19:11:001312 NetLogEventType::HOST_RESOLVER_IMPL_JOB_ATTACH,
[email protected]cd565142012-06-12 16:21:451313 net_log_.source().ToEventParametersCallback());
[email protected]0f292de02012-02-01 22:28:201314
1315 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001316 NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH,
maksim.sisov31452af2016-07-27 06:38:101317 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(),
[email protected]cd565142012-06-12 16:21:451318 priority()));
[email protected]0f292de02012-02-01 22:28:201319
Miriam Gershenson61604662017-09-28 23:51:111320 if (!request->info().is_speculative())
[email protected]0f292de02012-02-01 22:28:201321 had_non_speculative_request_ = true;
[email protected]b3601bc22012-02-21 21:23:201322
David Benjamina5677192018-04-23 23:25:421323 requests_.Append(request);
[email protected]b3601bc22012-02-21 21:23:201324
[email protected]51b9a6b2012-06-25 21:50:291325 UpdatePriority();
[email protected]68ad3ee2010-01-30 03:45:391326 }
1327
maksim.sisov31452af2016-07-27 06:38:101328 void ChangeRequestPriority(RequestImpl* req, RequestPriority priority) {
juliatuttlec53b19a72016-05-05 13:51:311329 DCHECK_EQ(key_.hostname, req->info().hostname());
juliatuttlec53b19a72016-05-05 13:51:311330
1331 priority_tracker_.Remove(req->priority());
1332 req->set_priority(priority);
1333 priority_tracker_.Add(req->priority());
1334 UpdatePriority();
1335 }
1336
maksim.sisov31452af2016-07-27 06:38:101337 // Detach cancelled request. If it was the last active Request, also finishes
1338 // this Job.
1339 void CancelRequest(RequestImpl* request) {
1340 DCHECK_EQ(key_.hostname, request->info().hostname());
1341 DCHECK(!requests_.empty());
[email protected]16ee26d2012-03-08 03:34:351342
maksim.sisov31452af2016-07-27 06:38:101343 LogCancelRequest(request->source_net_log(), request->info());
[email protected]16ee26d2012-03-08 03:34:351344
maksim.sisov31452af2016-07-27 06:38:101345 priority_tracker_.Remove(request->priority());
1346 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001347 NetLogEventType::HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH,
maksim.sisov31452af2016-07-27 06:38:101348 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(),
1349 priority()));
[email protected]b3601bc22012-02-21 21:23:201350
[email protected]16ee26d2012-03-08 03:34:351351 if (num_active_requests() > 0) {
[email protected]51b9a6b2012-06-25 21:50:291352 UpdatePriority();
David Benjamina5677192018-04-23 23:25:421353 request->RemoveFromList();
[email protected]16ee26d2012-03-08 03:34:351354 } else {
1355 // If we were called from a Request's callback within CompleteRequests,
1356 // that Request could not have been cancelled, so num_active_requests()
1357 // could not be 0. Therefore, we are not in CompleteRequests().
[email protected]1339a2a22012-10-17 08:39:431358 CompleteRequestsWithError(OK /* cancelled */);
[email protected]b3601bc22012-02-21 21:23:201359 }
[email protected]68ad3ee2010-01-30 03:45:391360 }
1361
[email protected]7af985a2012-12-14 22:40:421362 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1363 // the job. This currently assumes the abort is due to a network change.
olli.raulaa21e9eb72015-12-17 08:18:111364 // TODO This should not delete |this|.
[email protected]0f292de02012-02-01 22:28:201365 void Abort() {
[email protected]0f292de02012-02-01 22:28:201366 DCHECK(is_running());
[email protected]7af985a2012-12-14 22:40:421367 CompleteRequestsWithError(ERR_NETWORK_CHANGED);
[email protected]b3601bc22012-02-21 21:23:201368 }
1369
[email protected]f0f602bd2012-11-15 18:01:021370 // If DnsTask present, abort it and fall back to ProcTask.
1371 void AbortDnsTask() {
1372 if (dns_task_) {
[email protected]daae1322013-09-05 18:26:501373 KillDnsTask();
[email protected]f0f602bd2012-11-15 18:01:021374 dns_task_error_ = OK;
1375 StartProcTask();
1376 }
1377 }
1378
[email protected]16ee26d2012-03-08 03:34:351379 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1380 // Completes all requests and destroys the job.
1381 void OnEvicted() {
1382 DCHECK(!is_running());
1383 DCHECK(is_queued());
1384 handle_.Reset();
1385
mikecirone8b85c432016-09-08 19:11:001386 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_EVICTED);
[email protected]16ee26d2012-03-08 03:34:351387
1388 // This signals to CompleteRequests that this job never ran.
[email protected]1339a2a22012-10-17 08:39:431389 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
[email protected]16ee26d2012-03-08 03:34:351390 }
1391
[email protected]78eac2a2012-03-14 19:09:271392 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1393 // this Job was destroyed.
1394 bool ServeFromHosts() {
1395 DCHECK_GT(num_active_requests(), 0u);
1396 AddressList addr_list;
David Benjamina5677192018-04-23 23:25:421397 if (resolver_->ServeFromHosts(key(), requests_.head()->value()->info(),
[email protected]78eac2a2012-03-14 19:09:271398 &addr_list)) {
1399 // This will destroy the Job.
Rob Percival94f21ad2017-11-14 10:20:241400 CompleteRequests(
1401 MakeCacheEntry(OK, addr_list, HostCache::Entry::SOURCE_HOSTS),
1402 base::TimeDelta());
[email protected]78eac2a2012-03-14 19:09:271403 return true;
1404 }
1405 return false;
1406 }
1407
tanay.c6ddd5f6f2015-08-25 06:34:571408 const Key& key() const { return key_; }
[email protected]b4481b222012-03-16 17:13:111409
1410 bool is_queued() const {
1411 return !handle_.is_null();
1412 }
1413
1414 bool is_running() const {
1415 return is_dns_running() || is_proc_running();
1416 }
1417
[email protected]16ee26d2012-03-08 03:34:351418 private:
[email protected]daae1322013-09-05 18:26:501419 void KillDnsTask() {
1420 if (dns_task_) {
1421 ReduceToOneJobSlot();
1422 dns_task_.reset();
1423 }
1424 }
1425
1426 // Reduce the number of job slots occupied and queued in the dispatcher
1427 // to one. If the second Job slot is queued in the dispatcher, cancels the
1428 // queued job. Otherwise, the second Job has been started by the
1429 // PrioritizedDispatcher, so signals it is complete.
1430 void ReduceToOneJobSlot() {
1431 DCHECK_GE(num_occupied_job_slots_, 1u);
1432 if (is_queued()) {
[email protected]106ccd2c2014-06-17 09:21:001433 resolver_->dispatcher_->Cancel(handle_);
[email protected]daae1322013-09-05 18:26:501434 handle_.Reset();
1435 } else if (num_occupied_job_slots_ > 1) {
[email protected]106ccd2c2014-06-17 09:21:001436 resolver_->dispatcher_->OnJobFinished();
[email protected]daae1322013-09-05 18:26:501437 --num_occupied_job_slots_;
1438 }
1439 DCHECK_EQ(1u, num_occupied_job_slots_);
1440 }
1441
eroman0cd87b62016-12-14 19:13:451442 // MakeCacheEntry() and MakeCacheEntryWithTTL() are helpers to build a
1443 // HostCache::Entry(). The address list is omited from the cache entry
1444 // for errors.
1445 HostCache::Entry MakeCacheEntry(int net_error,
Rob Percival94f21ad2017-11-14 10:20:241446 const AddressList& addr_list,
1447 HostCache::Entry::Source source) const {
eroman0cd87b62016-12-14 19:13:451448 return HostCache::Entry(
1449 net_error,
Rob Percival94f21ad2017-11-14 10:20:241450 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(),
1451 source);
eroman0cd87b62016-12-14 19:13:451452 }
1453
1454 HostCache::Entry MakeCacheEntryWithTTL(int net_error,
1455 const AddressList& addr_list,
Rob Percival94f21ad2017-11-14 10:20:241456 HostCache::Entry::Source source,
eroman0cd87b62016-12-14 19:13:451457 base::TimeDelta ttl) const {
1458 return HostCache::Entry(
1459 net_error,
1460 net_error == OK ? MakeAddressListForRequest(addr_list) : AddressList(),
Rob Percival94f21ad2017-11-14 10:20:241461 source, ttl);
eroman0cd87b62016-12-14 19:13:451462 }
1463
juliatuttlec53b19a72016-05-05 13:51:311464 AddressList MakeAddressListForRequest(const AddressList& list) const {
1465 if (requests_.empty())
1466 return list;
David Benjamina5677192018-04-23 23:25:421467 return AddressList::CopyWithPort(list,
1468 requests_.head()->value()->info().port());
juliatuttlec53b19a72016-05-05 13:51:311469 }
1470
[email protected]51b9a6b2012-06-25 21:50:291471 void UpdatePriority() {
1472 if (is_queued()) {
1473 if (priority() != static_cast<RequestPriority>(handle_.priority()))
Misha Efimovb99e7da2018-05-30 16:59:021474 priority_change_time_ = tick_clock_->NowTicks();
[email protected]106ccd2c2014-06-17 09:21:001475 handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
[email protected]51b9a6b2012-06-25 21:50:291476 }
1477 }
1478
[email protected]16ee26d2012-03-08 03:34:351479 // PriorityDispatch::Job:
dchengb03027d2014-10-21 12:00:201480 void Start() override {
[email protected]daae1322013-09-05 18:26:501481 DCHECK_LE(num_occupied_job_slots_, 1u);
1482
[email protected]70c04ab2013-08-22 16:05:121483 handle_.Reset();
[email protected]daae1322013-09-05 18:26:501484 ++num_occupied_job_slots_;
1485
1486 if (num_occupied_job_slots_ == 2) {
1487 StartSecondDnsTransaction();
1488 return;
1489 }
1490
1491 DCHECK(!is_running());
[email protected]0f292de02012-02-01 22:28:201492
mikecirone8b85c432016-09-08 19:11:001493 net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_JOB_STARTED);
[email protected]0f292de02012-02-01 22:28:201494
[email protected]51b9a6b2012-06-25 21:50:291495 had_dns_config_ = resolver_->HaveDnsConfig();
1496
Misha Efimovb99e7da2018-05-30 16:59:021497 start_time_ = tick_clock_->NowTicks();
Miriam Gershenson61604662017-09-28 23:51:111498 base::TimeDelta queue_time = start_time_ - creation_time_;
1499 base::TimeDelta queue_time_after_change =
1500 start_time_ - priority_change_time_;
[email protected]51b9a6b2012-06-25 21:50:291501
Miriam Gershenson90d05e02017-09-28 19:29:281502 DNS_HISTOGRAM_BY_PRIORITY("Net.DNS.JobQueueTime", priority(), queue_time);
1503 DNS_HISTOGRAM_BY_PRIORITY("Net.DNS.JobQueueTimeAfterChange", priority(),
1504 queue_time_after_change);
[email protected]51b9a6b2012-06-25 21:50:291505
[email protected]443714fad2013-09-19 04:52:011506 bool system_only =
1507 (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0;
1508
[email protected]1d932852012-06-19 19:40:331509 // Caution: Job::Start must not complete synchronously.
[email protected]443714fad2013-09-19 04:52:011510 if (!system_only && had_dns_config_ &&
1511 !ResemblesMulticastDNSName(key_.hostname)) {
[email protected]b3601bc22012-02-21 21:23:201512 StartDnsTask();
1513 } else {
1514 StartProcTask();
1515 }
1516 }
1517
[email protected]b3601bc22012-02-21 21:23:201518 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
Francois Doraya2d01ba2017-09-25 19:17:401519 // the limits on |dispatcher_|. But in order to keep the number of
1520 // TaskScheduler threads low, we will need to use an "inner"
1521 // PrioritizedDispatcher with tighter limits.
[email protected]b3601bc22012-02-21 21:23:201522 void StartProcTask() {
[email protected]16ee26d2012-03-08 03:34:351523 DCHECK(!is_dns_running());
Misha Efimovb99e7da2018-05-30 16:59:021524 proc_task_ = new ProcTask(
1525 key_, resolver_->proc_params_,
1526 base::Bind(&Job::OnProcTaskComplete, base::Unretained(this),
1527 tick_clock_->NowTicks()),
1528 proc_task_runner_, net_log_, tick_clock_);
[email protected]0f292de02012-02-01 22:28:201529
[email protected]0f292de02012-02-01 22:28:201530 // Start() could be called from within Resolve(), hence it must NOT directly
1531 // call OnProcTaskComplete, for example, on synchronous failure.
1532 proc_task_->Start();
[email protected]68ad3ee2010-01-30 03:45:391533 }
1534
[email protected]0f292de02012-02-01 22:28:201535 // Called by ProcTask when it completes.
[email protected]e3bd4822012-10-23 18:01:371536 void OnProcTaskComplete(base::TimeTicks start_time,
1537 int net_error,
1538 const AddressList& addr_list) {
[email protected]b3601bc22012-02-21 21:23:201539 DCHECK(is_proc_running());
[email protected]68ad3ee2010-01-30 03:45:391540
[email protected]1d932852012-06-19 19:40:331541 if (dns_task_error_ != OK) {
Misha Efimovb99e7da2018-05-30 16:59:021542 base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
[email protected]1def74c2012-03-22 20:07:001543 if (net_error == OK) {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211544 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.FallbackSuccess", duration);
[email protected]1d932852012-06-19 19:40:331545 if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) &&
1546 ResemblesNetBIOSName(key_.hostname)) {
1547 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS);
1548 } else {
1549 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS);
1550 }
Ilya Sherman0eb39802017-12-08 20:58:181551 base::UmaHistogramSparse("Net.DNS.DnsTask.Errors",
1552 std::abs(dns_task_error_));
[email protected]1ffdda82012-12-12 23:04:221553 resolver_->OnDnsTaskResolve(dns_task_error_);
[email protected]1def74c2012-03-22 20:07:001554 } else {
Miriam Gershenson4e7c9ecf2017-09-26 23:17:211555 UMA_HISTOGRAM_LONG_TIMES_100("AsyncDNS.FallbackFail", duration);
[email protected]1def74c2012-03-22 20:07:001556 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
1557 }
1558 }
1559
eroman1efc237c2016-12-14 00:00:451560 if (ContainsIcannNameCollisionIp(addr_list))
1561 net_error = ERR_ICANN_NAME_COLLISION;
1562
[email protected]1339a2a22012-10-17 08:39:431563 base::TimeDelta ttl =
1564 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds);
[email protected]b3601bc22012-02-21 21:23:201565 if (net_error == OK)
1566 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds);
[email protected]68ad3ee2010-01-30 03:45:391567
Rob Percival94f21ad2017-11-14 10:20:241568 // Source unknown because the system resolver could have gotten it from a
1569 // hosts file, its own cache, a DNS lookup or somewhere else.
[email protected]895123222012-10-25 15:21:171570 // Don't store the |ttl| in cache since it's not obtained from the server.
Rob Percival94f21ad2017-11-14 10:20:241571 CompleteRequests(
1572 MakeCacheEntry(net_error, addr_list, HostCache::Entry::SOURCE_UNKNOWN),
1573 ttl);
[email protected]b3601bc22012-02-21 21:23:201574 }
1575
1576 void StartDnsTask() {
[email protected]78eac2a2012-03-14 19:09:271577 DCHECK(resolver_->HaveDnsConfig());
[email protected]daae1322013-09-05 18:26:501578 dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this,
Misha Efimovb99e7da2018-05-30 16:59:021579 net_log_, tick_clock_));
[email protected]b3601bc22012-02-21 21:23:201580
[email protected]daae1322013-09-05 18:26:501581 dns_task_->StartFirstTransaction();
1582 // Schedule a second transaction, if needed.
1583 if (dns_task_->needs_two_transactions())
1584 Schedule(true);
1585 }
1586
1587 void StartSecondDnsTransaction() {
1588 DCHECK(dns_task_->needs_two_transactions());
1589 dns_task_->StartSecondTransaction();
[email protected]16c2bd72013-06-28 01:19:221590 }
1591
1592 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
1593 // deleted before this callback. In this case dns_task is deleted as well,
1594 // so we use it as indicator whether Job is still valid.
1595 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
1596 base::TimeDelta duration,
1597 int net_error) {
Miriam Gershenson61604662017-09-28 23:51:111598 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.FailureTime", duration);
[email protected]16c2bd72013-06-28 01:19:221599
wezb9820d42016-06-22 23:41:041600 if (!dns_task)
[email protected]16c2bd72013-06-28 01:19:221601 return;
1602
Miriam Gershenson19faef812018-02-07 23:56:441603 if (duration < base::TimeDelta::FromMilliseconds(10)) {
1604 base::UmaHistogramSparse("Net.DNS.DnsTask.ErrorBeforeFallback.Fast",
1605 std::abs(net_error));
1606 } else {
1607 base::UmaHistogramSparse("Net.DNS.DnsTask.ErrorBeforeFallback.Slow",
1608 std::abs(net_error));
1609 }
[email protected]16c2bd72013-06-28 01:19:221610 dns_task_error_ = net_error;
1611
1612 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so.
1613 // http://crbug.com/117655
1614
1615 // TODO(szym): Some net errors indicate lack of connectivity. Starting
1616 // ProcTask in that case is a waste of time.
1617 if (resolver_->fallback_to_proctask_) {
[email protected]daae1322013-09-05 18:26:501618 KillDnsTask();
[email protected]16c2bd72013-06-28 01:19:221619 StartProcTask();
1620 } else {
1621 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
Brad Lassey2e8f185d2018-05-21 22:25:211622 // If the ttl is max, we didn't get one from the record, so set it to 0
1623 base::TimeDelta ttl =
1624 dns_task->ttl() < base::TimeDelta::FromSeconds(
1625 std::numeric_limits<uint32_t>::max())
1626 ? dns_task->ttl()
1627 : base::TimeDelta::FromSeconds(0);
1628 CompleteRequests(
1629 HostCache::Entry(net_error, AddressList(),
1630 HostCache::Entry::Source::SOURCE_UNKNOWN, ttl),
1631 ttl);
[email protected]b3601bc22012-02-21 21:23:201632 }
1633 }
1634
[email protected]daae1322013-09-05 18:26:501635
1636 // HostResolverImpl::DnsTask::Delegate implementation:
1637
dchengb03027d2014-10-21 12:00:201638 void OnDnsTaskComplete(base::TimeTicks start_time,
1639 int net_error,
1640 const AddressList& addr_list,
1641 base::TimeDelta ttl) override {
[email protected]b3601bc22012-02-21 21:23:201642 DCHECK(is_dns_running());
[email protected]b3601bc22012-02-21 21:23:201643
Misha Efimovb99e7da2018-05-30 16:59:021644 base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
[email protected]b3601bc22012-02-21 21:23:201645 if (net_error != OK) {
[email protected]16c2bd72013-06-28 01:19:221646 OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error);
[email protected]b3601bc22012-02-21 21:23:201647 return;
1648 }
Miriam Gershenson61604662017-09-28 23:51:111649
1650 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.SuccessTime", duration);
[email protected]b3601bc22012-02-21 21:23:201651
[email protected]1def74c2012-03-22 20:07:001652 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS);
[email protected]1339a2a22012-10-17 08:39:431653 RecordTTL(ttl);
[email protected]0adcb2b2012-08-15 21:30:461654
[email protected]1ffdda82012-12-12 23:04:221655 resolver_->OnDnsTaskResolve(OK);
[email protected]f0f602bd2012-11-15 18:01:021656
[email protected]895123222012-10-25 15:21:171657 base::TimeDelta bounded_ttl =
1658 std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds));
1659
eroman0cd87b62016-12-14 19:13:451660 if (ContainsIcannNameCollisionIp(addr_list)) {
1661 CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION);
1662 } else {
Rob Percival94f21ad2017-11-14 10:20:241663 CompleteRequests(MakeCacheEntryWithTTL(net_error, addr_list,
1664 HostCache::Entry::SOURCE_DNS, ttl),
eroman0cd87b62016-12-14 19:13:451665 bounded_ttl);
1666 }
[email protected]b3601bc22012-02-21 21:23:201667 }
1668
dchengb03027d2014-10-21 12:00:201669 void OnFirstDnsTransactionComplete() override {
[email protected]daae1322013-09-05 18:26:501670 DCHECK(dns_task_->needs_two_transactions());
1671 DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued());
1672 // No longer need to occupy two dispatcher slots.
1673 ReduceToOneJobSlot();
1674
1675 // We already have a job slot at the dispatcher, so if the second
1676 // transaction hasn't started, reuse it now instead of waiting in the queue
1677 // for the second slot.
1678 if (dns_task_->needs_another_transaction())
1679 dns_task_->StartSecondTransaction();
1680 }
1681
Brad Lassey786929ad2018-02-21 20:54:271682 URLRequestContext* url_request_context() override {
1683 return resolver_->url_request_context_;
1684 }
1685
Miriam Gershenson61604662017-09-28 23:51:111686 void RecordJobHistograms(int error) {
Miriam Gershensonc282ab42017-11-20 23:19:581687 // Used in UMA_HISTOGRAM_ENUMERATION. Do not renumber entries or reuse
1688 // deprecated values.
1689 enum Category {
1690 RESOLVE_SUCCESS = 0,
1691 RESOLVE_FAIL = 1,
1692 RESOLVE_SPECULATIVE_SUCCESS = 2,
1693 RESOLVE_SPECULATIVE_FAIL = 3,
1694 RESOLVE_ABORT = 4,
1695 RESOLVE_SPECULATIVE_ABORT = 5,
Miriam Gershenson61604662017-09-28 23:51:111696 RESOLVE_MAX, // Bounding value.
1697 };
1698 Category category = RESOLVE_MAX; // Illegal value for later DCHECK only.
1699
Misha Efimovb99e7da2018-05-30 16:59:021700 base::TimeDelta duration = tick_clock_->NowTicks() - start_time_;
Miriam Gershenson61604662017-09-28 23:51:111701 if (error == OK) {
1702 if (had_non_speculative_request_) {
1703 category = RESOLVE_SUCCESS;
1704 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime", duration);
1705 switch (key_.address_family) {
1706 case ADDRESS_FAMILY_IPV4:
1707 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.IPV4",
1708 duration);
1709 break;
1710 case ADDRESS_FAMILY_IPV6:
1711 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.IPV6",
1712 duration);
1713 break;
1714 case ADDRESS_FAMILY_UNSPECIFIED:
1715 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime.UNSPEC",
1716 duration);
1717 break;
1718 }
1719 } else {
1720 category = RESOLVE_SPECULATIVE_SUCCESS;
Miriam Gershenson61604662017-09-28 23:51:111721 }
Miriam Gershensonc282ab42017-11-20 23:19:581722 } else if (error == ERR_NETWORK_CHANGED ||
1723 error == ERR_HOST_RESOLVER_QUEUE_TOO_LARGE) {
1724 category = had_non_speculative_request_ ? RESOLVE_ABORT
1725 : RESOLVE_SPECULATIVE_ABORT;
Miriam Gershenson61604662017-09-28 23:51:111726 } else {
1727 if (had_non_speculative_request_) {
1728 category = RESOLVE_FAIL;
1729 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime", duration);
1730 switch (key_.address_family) {
1731 case ADDRESS_FAMILY_IPV4:
Miriam Gershensonc282ab42017-11-20 23:19:581732 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.IPV4",
Miriam Gershenson61604662017-09-28 23:51:111733 duration);
1734 break;
1735 case ADDRESS_FAMILY_IPV6:
Miriam Gershensonc282ab42017-11-20 23:19:581736 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.IPV6",
Miriam Gershenson61604662017-09-28 23:51:111737 duration);
1738 break;
1739 case ADDRESS_FAMILY_UNSPECIFIED:
Miriam Gershensonc282ab42017-11-20 23:19:581740 UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime.UNSPEC",
Miriam Gershenson61604662017-09-28 23:51:111741 duration);
1742 break;
1743 }
1744 } else {
1745 category = RESOLVE_SPECULATIVE_FAIL;
Miriam Gershenson61604662017-09-28 23:51:111746 }
1747 }
1748 DCHECK_LT(static_cast<int>(category),
1749 static_cast<int>(RESOLVE_MAX)); // Be sure it was set.
1750 UMA_HISTOGRAM_ENUMERATION("Net.DNS.ResolveCategory", category, RESOLVE_MAX);
Miriam Gershensonaaf139582017-11-27 16:06:061751
1752 if (category == RESOLVE_FAIL || category == RESOLVE_ABORT) {
1753 if (duration < base::TimeDelta::FromMilliseconds(10))
Ilya Sherman0eb39802017-12-08 20:58:181754 base::UmaHistogramSparse("Net.DNS.ResolveError.Fast", std::abs(error));
Miriam Gershensonaaf139582017-11-27 16:06:061755 else
Ilya Sherman0eb39802017-12-08 20:58:181756 base::UmaHistogramSparse("Net.DNS.ResolveError.Slow", std::abs(error));
Miriam Gershensonaaf139582017-11-27 16:06:061757 }
Miriam Gershenson61604662017-09-28 23:51:111758 }
1759
[email protected]16ee26d2012-03-08 03:34:351760 // Performs Job's last rites. Completes all Requests. Deletes this.
[email protected]895123222012-10-25 15:21:171761 void CompleteRequests(const HostCache::Entry& entry,
1762 base::TimeDelta ttl) {
[email protected]11fbca0b2013-06-02 23:37:211763 CHECK(resolver_.get());
[email protected]b3601bc22012-02-21 21:23:201764
[email protected]16ee26d2012-03-08 03:34:351765 // This job must be removed from resolver's |jobs_| now to make room for a
1766 // new job with the same key in case one of the OnComplete callbacks decides
1767 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1768 // is done.
danakj22f90e72016-04-16 01:55:401769 std::unique_ptr<Job> self_deleter(this);
[email protected]16ee26d2012-03-08 03:34:351770
1771 resolver_->RemoveJob(this);
1772
[email protected]16ee26d2012-03-08 03:34:351773 if (is_running()) {
[email protected]16ee26d2012-03-08 03:34:351774 if (is_proc_running()) {
[email protected]daae1322013-09-05 18:26:501775 DCHECK(!is_queued());
[email protected]16ee26d2012-03-08 03:34:351776 proc_task_->Cancel();
wezb9820d42016-06-22 23:41:041777 proc_task_ = nullptr;
[email protected]16ee26d2012-03-08 03:34:351778 }
[email protected]daae1322013-09-05 18:26:501779 KillDnsTask();
[email protected]16ee26d2012-03-08 03:34:351780
1781 // Signal dispatcher that a slot has opened.
[email protected]106ccd2c2014-06-17 09:21:001782 resolver_->dispatcher_->OnJobFinished();
[email protected]16ee26d2012-03-08 03:34:351783 } else if (is_queued()) {
[email protected]106ccd2c2014-06-17 09:21:001784 resolver_->dispatcher_->Cancel(handle_);
[email protected]16ee26d2012-03-08 03:34:351785 handle_.Reset();
1786 }
1787
1788 if (num_active_requests() == 0) {
mikecirone8b85c432016-09-08 19:11:001789 net_log_.AddEvent(NetLogEventType::CANCELLED);
1790 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
[email protected]16ee26d2012-03-08 03:34:351791 OK);
1792 return;
1793 }
[email protected]b3601bc22012-02-21 21:23:201794
mikecirone8b85c432016-09-08 19:11:001795 net_log_.EndEventWithNetErrorCode(NetLogEventType::HOST_RESOLVER_IMPL_JOB,
juliatuttle317860e2016-05-12 14:47:221796 entry.error());
[email protected]68ad3ee2010-01-30 03:45:391797
[email protected]78eac2a2012-03-14 19:09:271798 DCHECK(!requests_.empty());
1799
eroman1efc237c2016-12-14 00:00:451800 if (entry.error() == OK || entry.error() == ERR_ICANN_NAME_COLLISION) {
[email protected]d7b9a2b2012-05-31 22:31:191801 // Record this histogram here, when we know the system has a valid DNS
1802 // configuration.
[email protected]539df6c2012-06-19 21:21:291803 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1804 resolver_->received_dns_config_);
[email protected]d7b9a2b2012-05-31 22:31:191805 }
[email protected]16ee26d2012-03-08 03:34:351806
juliatuttle317860e2016-05-12 14:47:221807 bool did_complete = (entry.error() != ERR_NETWORK_CHANGED) &&
1808 (entry.error() != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
Miriam Gershensonc282ab42017-11-20 23:19:581809 if (did_complete)
[email protected]1339a2a22012-10-17 08:39:431810 resolver_->CacheResult(key_, entry, ttl);
Miriam Gershensonc282ab42017-11-20 23:19:581811
1812 RecordJobHistograms(entry.error());
[email protected]16ee26d2012-03-08 03:34:351813
maksim.sisov31452af2016-07-27 06:38:101814 // Complete all of the requests that were attached to the job and
1815 // detach them.
1816 while (!requests_.empty()) {
David Benjamina5677192018-04-23 23:25:421817 RequestImpl* req = requests_.head()->value();
1818 req->RemoveFromList();
[email protected]0f292de02012-02-01 22:28:201819 DCHECK_EQ(this, req->job());
1820 // Update the net log and notify registered observers.
juliatuttle317860e2016-05-12 14:47:221821 LogFinishRequest(req->source_net_log(), req->info(), entry.error());
[email protected]51b9a6b2012-06-25 21:50:291822 if (did_complete) {
1823 // Record effective total time from creation to completion.
Miriam Gershenson90d05e02017-09-28 19:29:281824 RecordTotalTime(req->info().is_speculative(), false,
Misha Efimovb99e7da2018-05-30 16:59:021825 tick_clock_->NowTicks() - req->request_time());
[email protected]51b9a6b2012-06-25 21:50:291826 }
maksim.sisov31452af2016-07-27 06:38:101827 req->OnJobCompleted(this, entry.error(), entry.addresses());
[email protected]0f292de02012-02-01 22:28:201828
1829 // Check if the resolver was destroyed as a result of running the
1830 // callback. If it was, we could continue, but we choose to bail.
[email protected]11fbca0b2013-06-02 23:37:211831 if (!resolver_.get())
[email protected]0f292de02012-02-01 22:28:201832 return;
1833 }
1834 }
1835
[email protected]1339a2a22012-10-17 08:39:431836 // Convenience wrapper for CompleteRequests in case of failure.
1837 void CompleteRequestsWithError(int net_error) {
Rob Percival94f21ad2017-11-14 10:20:241838 CompleteRequests(HostCache::Entry(net_error, AddressList(),
1839 HostCache::Entry::SOURCE_UNKNOWN),
[email protected]895123222012-10-25 15:21:171840 base::TimeDelta());
[email protected]1339a2a22012-10-17 08:39:431841 }
1842
Brad Lassey786929ad2018-02-21 20:54:271843 RequestPriority priority() const override {
[email protected]b4481b222012-03-16 17:13:111844 return priority_tracker_.highest_priority();
1845 }
1846
1847 // Number of non-canceled requests in |requests_|.
1848 size_t num_active_requests() const {
1849 return priority_tracker_.total_count();
1850 }
1851
wezb9820d42016-06-22 23:41:041852 bool is_dns_running() const { return !!dns_task_; }
[email protected]b4481b222012-03-16 17:13:111853
wezb9820d42016-06-22 23:41:041854 bool is_proc_running() const { return !!proc_task_; }
[email protected]b4481b222012-03-16 17:13:111855
[email protected]0f292de02012-02-01 22:28:201856 base::WeakPtr<HostResolverImpl> resolver_;
1857
1858 Key key_;
1859
1860 // Tracks the highest priority across |requests_|.
1861 PriorityTracker priority_tracker_;
1862
Miriam Gershensone42adb22017-10-16 16:19:381863 // Task runner used for HostResolverProc.
1864 scoped_refptr<base::TaskRunner> proc_task_runner_;
1865
[email protected]0f292de02012-02-01 22:28:201866 bool had_non_speculative_request_;
1867
[email protected]51b9a6b2012-06-25 21:50:291868 // Distinguishes measurements taken while DnsClient was fully configured.
1869 bool had_dns_config_;
1870
[email protected]daae1322013-09-05 18:26:501871 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1872 unsigned num_occupied_job_slots_;
1873
[email protected]1d932852012-06-19 19:40:331874 // Result of DnsTask.
1875 int dns_task_error_;
[email protected]1def74c2012-03-22 20:07:001876
Misha Efimovb99e7da2018-05-30 16:59:021877 const base::TickClock* tick_clock_;
[email protected]51b9a6b2012-06-25 21:50:291878 const base::TimeTicks creation_time_;
1879 base::TimeTicks priority_change_time_;
Miriam Gershenson61604662017-09-28 23:51:111880 base::TimeTicks start_time_;
[email protected]51b9a6b2012-06-25 21:50:291881
tfarina428341112016-09-22 13:38:201882 NetLogWithSource net_log_;
[email protected]0f292de02012-02-01 22:28:201883
[email protected]b3601bc22012-02-21 21:23:201884 // Resolves the host using a HostResolverProc.
[email protected]0f292de02012-02-01 22:28:201885 scoped_refptr<ProcTask> proc_task_;
1886
[email protected]b3601bc22012-02-21 21:23:201887 // Resolves the host using a DnsTransaction.
danakj22f90e72016-04-16 01:55:401888 std::unique_ptr<DnsTask> dns_task_;
[email protected]b3601bc22012-02-21 21:23:201889
[email protected]0f292de02012-02-01 22:28:201890 // All Requests waiting for the result of this Job. Some can be canceled.
David Benjamina5677192018-04-23 23:25:421891 base::LinkedList<RequestImpl> requests_;
[email protected]0f292de02012-02-01 22:28:201892
[email protected]16ee26d2012-03-08 03:34:351893 // A handle used in |HostResolverImpl::dispatcher_|.
[email protected]0f292de02012-02-01 22:28:201894 PrioritizedDispatcher::Handle handle_;
[email protected]68ad3ee2010-01-30 03:45:391895};
1896
1897//-----------------------------------------------------------------------------
1898
[email protected]0f292de02012-02-01 22:28:201899HostResolverImpl::ProcTaskParams::ProcTaskParams(
[email protected]e95d3aca2010-01-11 22:47:431900 HostResolverProc* resolver_proc,
[email protected]0f292de02012-02-01 22:28:201901 size_t max_retry_attempts)
1902 : resolver_proc(resolver_proc),
1903 max_retry_attempts(max_retry_attempts),
ttuttlecf1158bf2016-03-18 16:37:441904 unresponsive_delay(
1905 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs)),
[email protected]0f292de02012-02-01 22:28:201906 retry_factor(2) {
[email protected]106ccd2c2014-06-17 09:21:001907 // Maximum of 4 retry attempts for host resolution.
1908 static const size_t kDefaultMaxRetryAttempts = 4u;
1909 if (max_retry_attempts == HostResolver::kDefaultRetryAttempts)
1910 max_retry_attempts = kDefaultMaxRetryAttempts;
[email protected]0f292de02012-02-01 22:28:201911}
1912
vmpstracd23b72016-02-26 21:08:551913HostResolverImpl::ProcTaskParams::ProcTaskParams(const ProcTaskParams& other) =
1914 default;
1915
Chris Watkins68b15032017-12-01 03:07:131916HostResolverImpl::ProcTaskParams::~ProcTaskParams() = default;
[email protected]0f292de02012-02-01 22:28:201917
[email protected]b59ff372009-07-15 22:04:321918HostResolverImpl::~HostResolverImpl() {
gab47aa7da2017-06-02 16:09:431919 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]daae1322013-09-05 18:26:501920 // Prevent the dispatcher from starting new jobs.
[email protected]106ccd2c2014-06-17 09:21:001921 dispatcher_->SetLimitsToZero();
avi9e72eb482016-12-09 21:10:541922 // It's now safe for Jobs to call KillDnsTask on destruction, because
[email protected]daae1322013-09-05 18:26:501923 // OnJobComplete will not start any new jobs.
avi9e72eb482016-12-09 21:10:541924 jobs_.clear();
[email protected]e95d3aca2010-01-11 22:47:431925
[email protected]232a5812011-03-04 22:42:081926 NetworkChangeNotifier::RemoveIPAddressObserver(this);
ttuttlecf1158bf2016-03-18 16:37:441927 NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
[email protected]bb0e34542012-08-31 19:52:401928 NetworkChangeNotifier::RemoveDNSObserver(this);
[email protected]b59ff372009-07-15 22:04:321929}
1930
[email protected]0f292de02012-02-01 22:28:201931void HostResolverImpl::SetMaxQueuedJobs(size_t value) {
[email protected]106ccd2c2014-06-17 09:21:001932 DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
[email protected]0f292de02012-02-01 22:28:201933 DCHECK_GT(value, 0u);
1934 max_queued_jobs_ = value;
[email protected]be1a48b2011-01-20 00:12:131935}
1936
[email protected]684970b2009-08-14 04:54:461937int HostResolverImpl::Resolve(const RequestInfo& info,
[email protected]5109c1952013-08-20 18:44:101938 RequestPriority priority,
[email protected]b59ff372009-07-15 22:04:321939 AddressList* addresses,
[email protected]aa22b242011-11-16 18:58:291940 const CompletionCallback& callback,
maksim.sisov31452af2016-07-27 06:38:101941 std::unique_ptr<Request>* out_req,
tfarina428341112016-09-22 13:38:201942 const NetLogWithSource& source_net_log) {
[email protected]95a214c2011-08-04 21:50:401943 DCHECK(addresses);
gab47aa7da2017-06-02 16:09:431944 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]aa22b242011-11-16 18:58:291945 DCHECK_EQ(false, callback.is_null());
maksim.sisov31452af2016-07-27 06:38:101946 DCHECK(out_req);
[email protected]1ac6af92010-06-03 21:00:141947
Chris Palmer9cbf4212017-07-19 01:54:551948 LogStartRequest(source_net_log, info);
pauljensen19eb73422015-04-15 23:20:061949
Matt Menke23b4bbf82017-07-20 19:11:291950 Key key;
1951 int rv = ResolveHelper(info, false, nullptr, source_net_log, addresses, &key);
[email protected]95a214c2011-08-04 21:50:401952 if (rv != ERR_DNS_CACHE_MISS) {
xunjieli26f90452014-11-10 16:23:021953 LogFinishRequest(source_net_log, info, rv);
Miriam Gershenson90d05e02017-09-28 19:29:281954 RecordTotalTime(info.is_speculative(), true, base::TimeDelta());
[email protected]95a214c2011-08-04 21:50:401955 return rv;
[email protected]38368712011-03-02 08:09:401956 }
1957
[email protected]0f292de02012-02-01 22:28:201958 // Next we need to attach our request to a "job". This job is responsible for
1959 // calling "getaddrinfo(hostname)" on a worker thread.
1960
avi9e72eb482016-12-09 21:10:541961 auto jobit = jobs_.find(key);
[email protected]0f292de02012-02-01 22:28:201962 Job* job;
1963 if (jobit == jobs_.end()) {
Miriam Gershensone42adb22017-10-16 16:19:381964 job = new Job(weak_ptr_factory_.GetWeakPtr(), key, priority,
Misha Efimovb99e7da2018-05-30 16:59:021965 proc_task_runner_, source_net_log, tick_clock_);
[email protected]daae1322013-09-05 18:26:501966 job->Schedule(false);
[email protected]0f292de02012-02-01 22:28:201967
1968 // Check for queue overflow.
[email protected]106ccd2c2014-06-17 09:21:001969 if (dispatcher_->num_queued_jobs() > max_queued_jobs_) {
1970 Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest());
[email protected]0f292de02012-02-01 22:28:201971 DCHECK(evicted);
[email protected]16ee26d2012-03-08 03:34:351972 evicted->OnEvicted(); // Deletes |evicted|.
[email protected]0f292de02012-02-01 22:28:201973 if (evicted == job) {
[email protected]0f292de02012-02-01 22:28:201974 rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE;
xunjieli26f90452014-11-10 16:23:021975 LogFinishRequest(source_net_log, info, rv);
[email protected]0f292de02012-02-01 22:28:201976 return rv;
1977 }
[email protected]0f292de02012-02-01 22:28:201978 }
avi9e72eb482016-12-09 21:10:541979 jobs_[key] = base::WrapUnique(job);
[email protected]0f292de02012-02-01 22:28:201980 } else {
avi9e72eb482016-12-09 21:10:541981 job = jobit->second.get();
[email protected]0f292de02012-02-01 22:28:201982 }
1983
1984 // Can't complete synchronously. Create and attach request.
Misha Efimovb99e7da2018-05-30 16:59:021985 auto req =
1986 std::make_unique<RequestImpl>(source_net_log, info, priority, callback,
1987 addresses, job, tick_clock_->NowTicks());
maksim.sisov31452af2016-07-27 06:38:101988 job->AddRequest(req.get());
1989 *out_req = std::move(req);
[email protected]b59ff372009-07-15 22:04:321990
[email protected]0f292de02012-02-01 22:28:201991 // Completion happens during Job::CompleteRequests().
[email protected]b59ff372009-07-15 22:04:321992 return ERR_IO_PENDING;
1993}
1994
Francois Doraya2d01ba2017-09-25 19:17:401995HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log)
mmenke91c17162016-06-02 16:03:231996 : max_queued_jobs_(0),
1997 proc_params_(NULL, options.max_retry_attempts),
1998 net_log_(net_log),
1999 received_dns_config_(false),
2000 num_dns_failures_(0),
mgershaf9a9232017-04-13 20:19:032001 assume_ipv6_failure_on_wifi_(false),
mmenke91c17162016-06-02 16:03:232002 use_local_ipv6_(false),
2003 last_ipv6_probe_result_(true),
mmenke91c17162016-06-02 16:03:232004 additional_resolver_flags_(0),
2005 fallback_to_proctask_(true),
Misha Efimovb99e7da2018-05-30 16:59:022006 tick_clock_(base::DefaultTickClock::GetInstance()),
mmenke91c17162016-06-02 16:03:232007 weak_ptr_factory_(this),
2008 probe_weak_ptr_factory_(this) {
2009 if (options.enable_caching)
2010 cache_ = HostCache::CreateDefaultCache();
2011
2012 PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits();
2013 dispatcher_.reset(new PrioritizedDispatcher(job_limits));
2014 max_queued_jobs_ = job_limits.total_jobs * 100u;
2015
2016 DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
2017
Miriam Gershensone42adb22017-10-16 16:19:382018 proc_task_runner_ = base::CreateTaskRunnerWithTraits(
Maks Orlovich5393c8682018-02-26 16:17:502019 {base::MayBlock(), priority_mode.Get(),
2020 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
Miriam Gershensone42adb22017-10-16 16:19:382021
mmenke91c17162016-06-02 16:03:232022#if defined(OS_WIN)
2023 EnsureWinsockInit();
2024#endif
Fabrice de Gans-Riberi7de47372018-05-08 20:23:472025#if (defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)) || \
2026 defined(OS_FUCHSIA)
mmenke91c17162016-06-02 16:03:232027 RunLoopbackProbeJob();
2028#endif
2029 NetworkChangeNotifier::AddIPAddressObserver(this);
2030 NetworkChangeNotifier::AddConnectionTypeObserver(this);
2031 NetworkChangeNotifier::AddDNSObserver(this);
2032#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
Fabrice de Gans-Riberi65421f62018-05-22 23:16:182033 !defined(OS_ANDROID)
mmenke91c17162016-06-02 16:03:232034 EnsureDnsReloaderInit();
2035#endif
2036
2037 OnConnectionTypeChanged(NetworkChangeNotifier::GetConnectionType());
2038
2039 {
2040 DnsConfig dns_config;
2041 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2042 received_dns_config_ = dns_config.IsValid();
2043 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2044 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
2045 }
2046
2047 fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
2048}
2049
2050void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) {
2051 if (result) {
2052 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
2053 } else {
2054 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
2055 }
2056}
2057
Miriam Gershensone42adb22017-10-16 16:19:382058void HostResolverImpl::SetTaskRunnerForTesting(
2059 scoped_refptr<base::TaskRunner> task_runner) {
2060 proc_task_runner_ = std::move(task_runner);
2061}
2062
Matt Menke23b4bbf82017-07-20 19:11:292063int HostResolverImpl::ResolveHelper(const RequestInfo& info,
juliatuttle9fb7aeb2016-06-06 20:16:332064 bool allow_stale,
2065 HostCache::EntryStaleness* stale_info,
Matt Menke23b4bbf82017-07-20 19:11:292066 const NetLogWithSource& source_net_log,
2067 AddressList* addresses,
2068 Key* key) {
2069 IPAddress ip_address;
2070 IPAddress* ip_address_ptr = nullptr;
2071 if (ip_address.AssignFromIPLiteral(info.hostname())) {
2072 ip_address_ptr = &ip_address;
2073 } else {
2074 // Check that the caller supplied a valid hostname to resolve.
2075 if (!IsValidDNSDomain(info.hostname()))
2076 return ERR_NAME_NOT_RESOLVED;
2077 }
2078
2079 // Build a key that identifies the request in the cache and in the
2080 // outstanding jobs map.
2081 *key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log);
2082
juliatuttle9fb7aeb2016-06-06 20:16:332083 DCHECK(allow_stale == !!stale_info);
[email protected]95a214c2011-08-04 21:50:402084 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
2085 // On Windows it gives the default interface's address, whereas on Linux it
2086 // gives an error. We will make it fail on all platforms for consistency.
juliatuttle9fb7aeb2016-06-06 20:16:332087 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) {
2088 MakeNotStale(stale_info);
[email protected]95a214c2011-08-04 21:50:402089 return ERR_NAME_NOT_RESOLVED;
juliatuttle9fb7aeb2016-06-06 20:16:332090 }
[email protected]95a214c2011-08-04 21:50:402091
2092 int net_error = ERR_UNEXPECTED;
Matt Menke23b4bbf82017-07-20 19:11:292093 if (ResolveAsIP(*key, info, ip_address_ptr, &net_error, addresses)) {
juliatuttle9fb7aeb2016-06-06 20:16:332094 MakeNotStale(stale_info);
[email protected]95a214c2011-08-04 21:50:402095 return net_error;
juliatuttle9fb7aeb2016-06-06 20:16:332096 }
Mike Westda1c690e2017-08-12 05:57:162097
2098 // Special-case localhost names, as per the recommendations in
2099 // https://tools.ietf.org/html/draft-west-let-localhost-be-localhost.
2100 if (ServeLocalhost(*key, info, addresses)) {
2101 MakeNotStale(stale_info);
2102 return OK;
2103 }
2104
Matt Menke23b4bbf82017-07-20 19:11:292105 if (ServeFromCache(*key, info, &net_error, addresses, allow_stale,
juliatuttle9fb7aeb2016-06-06 20:16:332106 stale_info)) {
mgersh33d23c02017-03-29 20:24:252107 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_CACHE_HIT,
2108 addresses->CreateNetLogCallback());
juliatuttle9fb7aeb2016-06-06 20:16:332109 // |ServeFromCache()| will set |*stale_info| as needed.
[email protected]78eac2a2012-03-14 19:09:272110 return net_error;
2111 }
Mike Westda1c690e2017-08-12 05:57:162112
[email protected]78eac2a2012-03-14 19:09:272113 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2114 // http://crbug.com/117655
Matt Menke23b4bbf82017-07-20 19:11:292115 if (ServeFromHosts(*key, info, addresses)) {
mgersh33d23c02017-03-29 20:24:252116 source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_HOSTS_HIT,
2117 addresses->CreateNetLogCallback());
juliatuttle9fb7aeb2016-06-06 20:16:332118 MakeNotStale(stale_info);
[email protected]78eac2a2012-03-14 19:09:272119 return OK;
2120 }
estarkd1bc206e2015-06-20 00:44:392121
[email protected]78eac2a2012-03-14 19:09:272122 return ERR_DNS_CACHE_MISS;
[email protected]95a214c2011-08-04 21:50:402123}
2124
2125int HostResolverImpl::ResolveFromCache(const RequestInfo& info,
2126 AddressList* addresses,
tfarina428341112016-09-22 13:38:202127 const NetLogWithSource& source_net_log) {
gab47aa7da2017-06-02 16:09:432128 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]95a214c2011-08-04 21:50:402129 DCHECK(addresses);
2130
[email protected]95a214c2011-08-04 21:50:402131 // Update the net log and notify registered observers.
xunjieli26f90452014-11-10 16:23:022132 LogStartRequest(source_net_log, info);
[email protected]95a214c2011-08-04 21:50:402133
Matt Menke23b4bbf82017-07-20 19:11:292134 Key key;
2135 int rv = ResolveHelper(info, false, nullptr, source_net_log, addresses, &key);
[email protected]95a214c2011-08-04 21:50:402136
xunjieli26f90452014-11-10 16:23:022137 LogFinishRequest(source_net_log, info, rv);
[email protected]95a214c2011-08-04 21:50:402138 return rv;
2139}
2140
[email protected]a8883e452012-11-17 05:58:062141void HostResolverImpl::SetDnsClientEnabled(bool enabled) {
gab47aa7da2017-06-02 16:09:432142 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
[email protected]a8883e452012-11-17 05:58:062143#if defined(ENABLE_BUILT_IN_DNS)
2144 if (enabled && !dns_client_) {
2145 SetDnsClient(DnsClient::CreateClient(net_log_));
2146 } else if (!enabled && dns_client_) {
danakj22f90e72016-04-16 01:55:402147 SetDnsClient(std::unique_ptr<DnsClient>());
[email protected]a8883e452012-11-17 05:58:062148 }
2149#endif
2150}
2151
[email protected]489d1a82011-10-12 03:09:112152HostCache* HostResolverImpl::GetHostCache() {
2153 return cache_.get();
2154}
[email protected]95a214c2011-08-04 21:50:402155
Rob Percivalbc658a22017-12-13 08:24:422156bool HostResolverImpl::HasCached(base::StringPiece hostname,
2157 HostCache::Entry::Source* source_out,
2158 HostCache::EntryStaleness* stale_out) const {
2159 if (!cache_)
2160 return false;
2161
2162 return cache_->HasEntry(hostname, source_out, stale_out);
2163}
2164
danakj22f90e72016-04-16 01:55:402165std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const {
[email protected]17e92032012-03-29 00:56:242166 // Check if async DNS is disabled.
2167 if (!dns_client_.get())
tfhef3618f2016-01-11 23:07:082168 return nullptr;
[email protected]17e92032012-03-29 00:56:242169
2170 // Check if async DNS is enabled, but we currently have no configuration
2171 // for it.
2172 const DnsConfig* dns_config = dns_client_->GetConfig();
wezb9820d42016-06-22 23:41:042173 if (!dns_config)
Jeremy Roman0579ed62017-08-29 15:56:192174 return std::make_unique<base::DictionaryValue>();
[email protected]17e92032012-03-29 00:56:242175
2176 return dns_config->ToValue();
2177}
2178
juliatuttle9fb7aeb2016-06-06 20:16:332179int HostResolverImpl::ResolveStaleFromCache(
2180 const RequestInfo& info,
2181 AddressList* addresses,
2182 HostCache::EntryStaleness* stale_info,
tfarina428341112016-09-22 13:38:202183 const NetLogWithSource& source_net_log) {
gab47aa7da2017-06-02 16:09:432184 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
juliatuttle9fb7aeb2016-06-06 20:16:332185 DCHECK(addresses);
2186 DCHECK(stale_info);
2187
2188 // Update the net log and notify registered observers.
2189 LogStartRequest(source_net_log, info);
2190
Matt Menke23b4bbf82017-07-20 19:11:292191 Key key;
2192 int rv =
2193 ResolveHelper(info, true, stale_info, source_net_log, addresses, &key);
juliatuttle9fb7aeb2016-06-06 20:16:332194 LogFinishRequest(source_net_log, info, rv);
2195 return rv;
2196}
2197
Miriam Gershenson2839ef112017-08-30 03:25:372198size_t HostResolverImpl::LastRestoredCacheSize() const {
2199 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
2200
2201 return cache_ ? cache_->last_restore_size() : 0;
2202}
2203
2204size_t HostResolverImpl::CacheSize() const {
2205 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
2206
2207 return cache_ ? cache_->size() : 0;
2208}
2209
mgershaf9a9232017-04-13 20:19:032210void HostResolverImpl::SetNoIPv6OnWifi(bool no_ipv6_on_wifi) {
gab47aa7da2017-06-02 16:09:432211 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
mgershaf9a9232017-04-13 20:19:032212 assume_ipv6_failure_on_wifi_ = no_ipv6_on_wifi;
2213}
2214
2215bool HostResolverImpl::GetNoIPv6OnWifi() {
2216 return assume_ipv6_failure_on_wifi_;
2217}
2218
Brad Lassey786929ad2018-02-21 20:54:272219void HostResolverImpl::SetRequestContext(URLRequestContext* context) {
2220 if (context != url_request_context_) {
2221 url_request_context_ = context;
2222 }
2223}
2224
Misha Efimovb99e7da2018-05-30 16:59:022225void HostResolverImpl::SetTickClockForTesting(
2226 const base::TickClock* tick_clock) {
2227 tick_clock_ = tick_clock;
2228 cache_->set_tick_clock_for_testing(tick_clock);
2229}
2230
Brad Lassey786929ad2018-02-21 20:54:272231void HostResolverImpl::ClearDnsOverHttpsServers() {
2232 if (dns_over_https_servers_.size() == 0)
2233 return;
2234
2235 dns_over_https_servers_.clear();
2236
2237 if (dns_client_.get() && dns_client_->GetConfig())
2238 UpdateDNSConfig(true);
2239}
2240
2241void HostResolverImpl::AddDnsOverHttpsServer(std::string spec, bool use_post) {
2242 GURL url(spec);
2243 if (!url.SchemeIs("https"))
2244 return;
2245
2246 dns_over_https_servers_.emplace_back(url, use_post);
2247
2248 if (dns_client_.get() && dns_client_->GetConfig())
2249 UpdateDNSConfig(true);
2250}
2251
[email protected]95a214c2011-08-04 21:50:402252bool HostResolverImpl::ResolveAsIP(const Key& key,
2253 const RequestInfo& info,
martijna23c8962016-03-04 18:18:512254 const IPAddress* ip_address,
[email protected]95a214c2011-08-04 21:50:402255 int* net_error,
2256 AddressList* addresses) {
2257 DCHECK(addresses);
2258 DCHECK(net_error);
martijna23c8962016-03-04 18:18:512259 if (ip_address == nullptr)
[email protected]95a214c2011-08-04 21:50:402260 return false;
2261
[email protected]95a214c2011-08-04 21:50:402262 *net_error = OK;
martijna23c8962016-03-04 18:18:512263 AddressFamily family = GetAddressFamily(*ip_address);
cbentzel1906f872015-06-05 16:25:252264 if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED &&
2265 key.address_family != family) {
[email protected]1c7cf3f82014-08-07 21:33:482266 // Don't return IPv6 addresses for IPv4 queries, and vice versa.
[email protected]95a214c2011-08-04 21:50:402267 *net_error = ERR_NAME_NOT_RESOLVED;
2268 } else {
martijna23c8962016-03-04 18:18:512269 *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port());
[email protected]7054e78f2012-05-07 21:44:562270 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)
2271 addresses->SetDefaultCanonicalName();
[email protected]95a214c2011-08-04 21:50:402272 }
2273 return true;
2274}
2275
2276bool HostResolverImpl::ServeFromCache(const Key& key,
2277 const RequestInfo& info,
[email protected]95a214c2011-08-04 21:50:402278 int* net_error,
juliatuttle9fb7aeb2016-06-06 20:16:332279 AddressList* addresses,
2280 bool allow_stale,
2281 HostCache::EntryStaleness* stale_info) {
[email protected]95a214c2011-08-04 21:50:402282 DCHECK(addresses);
2283 DCHECK(net_error);
juliatuttle9fb7aeb2016-06-06 20:16:332284 DCHECK(allow_stale == !!stale_info);
[email protected]95a214c2011-08-04 21:50:402285 if (!info.allow_cached_response() || !cache_.get())
2286 return false;
2287
juliatuttle9fb7aeb2016-06-06 20:16:332288 const HostCache::Entry* cache_entry;
2289 if (allow_stale)
Misha Efimovb99e7da2018-05-30 16:59:022290 cache_entry = cache_->LookupStale(key, tick_clock_->NowTicks(), stale_info);
juliatuttle9fb7aeb2016-06-06 20:16:332291 else
Misha Efimovb99e7da2018-05-30 16:59:022292 cache_entry = cache_->Lookup(key, tick_clock_->NowTicks());
[email protected]95a214c2011-08-04 21:50:402293 if (!cache_entry)
2294 return false;
2295
juliatuttle317860e2016-05-12 14:47:222296 *net_error = cache_entry->error();
[email protected]7054e78f2012-05-07 21:44:562297 if (*net_error == OK) {
[email protected]1339a2a22012-10-17 08:39:432298 if (cache_entry->has_ttl())
juliatuttle317860e2016-05-12 14:47:222299 RecordTTL(cache_entry->ttl());
2300 *addresses = EnsurePortOnAddressList(cache_entry->addresses(), info.port());
[email protected]7054e78f2012-05-07 21:44:562301 }
[email protected]95a214c2011-08-04 21:50:402302 return true;
2303}
2304
[email protected]78eac2a2012-03-14 19:09:272305bool HostResolverImpl::ServeFromHosts(const Key& key,
2306 const RequestInfo& info,
2307 AddressList* addresses) {
2308 DCHECK(addresses);
2309 if (!HaveDnsConfig())
2310 return false;
[email protected]05a79d42013-03-28 07:30:092311 addresses->clear();
2312
[email protected]cb507622012-03-23 16:17:062313 // HOSTS lookups are case-insensitive.
brettw8e2106d2015-08-11 19:30:222314 std::string hostname = base::ToLowerASCII(key.hostname);
[email protected]cb507622012-03-23 16:17:062315
[email protected]05a79d42013-03-28 07:30:092316 const DnsHosts& hosts = dns_client_->GetConfig()->hosts;
2317
[email protected]78eac2a2012-03-14 19:09:272318 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2319 // (glibc and c-ares) return the first matching line. We have more
2320 // flexibility, but lose implicit ordering.
[email protected]05a79d42013-03-28 07:30:092321 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2322 // necessary.
2323 if (key.address_family == ADDRESS_FAMILY_IPV6 ||
2324 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2325 DnsHosts::const_iterator it = hosts.find(
2326 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6));
2327 if (it != hosts.end())
2328 addresses->push_back(IPEndPoint(it->second, info.port()));
[email protected]78eac2a2012-03-14 19:09:272329 }
2330
[email protected]05a79d42013-03-28 07:30:092331 if (key.address_family == ADDRESS_FAMILY_IPV4 ||
2332 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2333 DnsHosts::const_iterator it = hosts.find(
2334 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4));
2335 if (it != hosts.end())
2336 addresses->push_back(IPEndPoint(it->second, info.port()));
2337 }
2338
[email protected]ec666ab22013-04-17 20:05:592339 // If got only loopback addresses and the family was restricted, resolve
2340 // again, without restrictions. See SystemHostResolverCall for rationale.
2341 if ((key.host_resolver_flags &
2342 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
2343 IsAllIPv4Loopback(*addresses)) {
2344 Key new_key(key);
2345 new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED;
2346 new_key.host_resolver_flags &=
2347 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2348 return ServeFromHosts(new_key, info, addresses);
2349 }
[email protected]05a79d42013-03-28 07:30:092350 return !addresses->empty();
[email protected]78eac2a2012-03-14 19:09:272351}
2352
estarkd1bc206e2015-06-20 00:44:392353bool HostResolverImpl::ServeLocalhost(const Key& key,
2354 const RequestInfo& info,
2355 AddressList* addresses) {
2356 AddressList resolved_addresses;
2357 if (!ResolveLocalHostname(key.hostname, info.port(), &resolved_addresses))
2358 return false;
2359
2360 addresses->clear();
2361
2362 for (const auto& address : resolved_addresses) {
2363 // Include the address if:
2364 // - caller didn't specify an address family, or
2365 // - caller specifically asked for the address family of this address, or
2366 // - this is an IPv6 address and caller specifically asked for IPv4 due
2367 // to lack of detected IPv6 support. (See SystemHostResolverCall for
2368 // rationale).
2369 if (key.address_family == ADDRESS_FAMILY_UNSPECIFIED ||
2370 key.address_family == address.GetFamily() ||
2371 (address.GetFamily() == ADDRESS_FAMILY_IPV6 &&
2372 key.address_family == ADDRESS_FAMILY_IPV4 &&
2373 (key.host_resolver_flags &
2374 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6))) {
2375 addresses->push_back(address);
2376 }
2377 }
2378
2379 return true;
2380}
2381
[email protected]16ee26d2012-03-08 03:34:352382void HostResolverImpl::CacheResult(const Key& key,
[email protected]1339a2a22012-10-17 08:39:432383 const HostCache::Entry& entry,
[email protected]16ee26d2012-03-08 03:34:352384 base::TimeDelta ttl) {
Miriam Gershensone3bc63022017-10-09 19:57:052385 // Don't cache an error unless it has a positive TTL.
2386 if (cache_.get() && (entry.error() == OK || ttl > base::TimeDelta()))
Misha Efimovb99e7da2018-05-30 16:59:022387 cache_->Set(key, entry, tick_clock_->NowTicks(), ttl);
[email protected]ef4c40c2010-09-01 14:42:032388}
2389
[email protected]0f292de02012-02-01 22:28:202390void HostResolverImpl::RemoveJob(Job* job) {
2391 DCHECK(job);
avi9e72eb482016-12-09 21:10:542392 auto it = jobs_.find(job->key());
2393 if (it != jobs_.end() && it->second.get() == job) {
2394 it->second.release();
[email protected]16ee26d2012-03-08 03:34:352395 jobs_.erase(it);
avi9e72eb482016-12-09 21:10:542396 }
[email protected]b59ff372009-07-15 22:04:322397}
2398
[email protected]137af622010-02-05 02:14:352399HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest(
pauljensen19eb73422015-04-15 23:20:062400 const RequestInfo& info,
martijna23c8962016-03-04 18:18:512401 const IPAddress* ip_address,
tfarina428341112016-09-22 13:38:202402 const NetLogWithSource& net_log) {
[email protected]eaf3a3b2010-09-03 20:34:272403 HostResolverFlags effective_flags =
2404 info.host_resolver_flags() | additional_resolver_flags_;
[email protected]137af622010-02-05 02:14:352405 AddressFamily effective_address_family = info.address_family();
[email protected]9db6f702013-04-10 18:10:512406
mgersh3eba057d2017-02-28 18:50:562407 if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED &&
2408 // When resolving IPv4 literals, there's no need to probe for IPv6.
2409 // When resolving IPv6 literals, there's no benefit to artificially
2410 // limiting our resolution based on a probe. Prior logic ensures
2411 // that this query is UNSPECIFIED (see effective_address_family
2412 // check above) so the code requesting the resolution should be amenable
2413 // to receiving a IPv6 resolution.
2414 !use_local_ipv6_ && ip_address == nullptr && !IsIPv6Reachable(net_log)) {
2415 effective_address_family = ADDRESS_FAMILY_IPV4;
2416 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
[email protected]9db6f702013-04-10 18:10:512417 }
2418
estarkd1bc206e2015-06-20 00:44:392419 return Key(info.hostname(), effective_address_family, effective_flags);
[email protected]137af622010-02-05 02:14:352420}
2421
tfarina428341112016-09-22 13:38:202422bool HostResolverImpl::IsIPv6Reachable(const NetLogWithSource& net_log) {
mgershaf9a9232017-04-13 20:19:032423 // Don't bother checking if the device is on WiFi and IPv6 is assumed to not
2424 // work on WiFi.
2425 if (assume_ipv6_failure_on_wifi_ &&
2426 NetworkChangeNotifier::GetConnectionType() ==
2427 NetworkChangeNotifier::CONNECTION_WIFI) {
2428 return false;
2429 }
2430
eroman1a17ef32016-12-14 01:06:092431 // Cache the result for kIPv6ProbePeriodMs (measured from after
2432 // IsGloballyReachable() completes).
sergeyub8cdc212015-05-14 18:50:372433 bool cached = true;
Misha Efimovb99e7da2018-05-30 16:59:022434 if ((tick_clock_->NowTicks() - last_ipv6_probe_time_).InMilliseconds() >
eroman1a17ef32016-12-14 01:06:092435 kIPv6ProbePeriodMs) {
martijna23c8962016-03-04 18:18:512436 last_ipv6_probe_result_ =
2437 IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log);
Misha Efimovb99e7da2018-05-30 16:59:022438 last_ipv6_probe_time_ = tick_clock_->NowTicks();
sergeyub8cdc212015-05-14 18:50:372439 cached = false;
2440 }
mikecirone8b85c432016-09-08 19:11:002441 net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK,
sergeyub8cdc212015-05-14 18:50:372442 base::Bind(&NetLogIPv6AvailableCallback,
2443 last_ipv6_probe_result_, cached));
2444 return last_ipv6_probe_result_;
2445}
2446
mgershaf9a9232017-04-13 20:19:032447bool HostResolverImpl::IsGloballyReachable(const IPAddress& dest,
2448 const NetLogWithSource& net_log) {
mgershaf9a9232017-04-13 20:19:032449 std::unique_ptr<DatagramClientSocket> socket(
2450 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
Sergey Ulanovcbdfc8852018-03-16 20:13:282451 DatagramSocket::DEFAULT_BIND, net_log.net_log(), net_log.source()));
mgershaf9a9232017-04-13 20:19:032452 int rv = socket->Connect(IPEndPoint(dest, 53));
2453 if (rv != OK)
2454 return false;
2455 IPEndPoint endpoint;
2456 rv = socket->GetLocalAddress(&endpoint);
2457 if (rv != OK)
2458 return false;
2459 DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily());
2460 const IPAddress& address = endpoint.address();
2461
2462 bool is_link_local =
2463 (address.bytes()[0] == 0xFE) && ((address.bytes()[1] & 0xC0) == 0x80);
2464 if (is_link_local)
2465 return false;
2466
2467 const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0};
2468 if (IPAddressStartsWith(address, kTeredoPrefix))
2469 return false;
2470
2471 return true;
2472}
2473
mmenke91c17162016-06-02 16:03:232474void HostResolverImpl::RunLoopbackProbeJob() {
Francois Doraya2d01ba2017-09-25 19:17:402475 // Run this asynchronously as it can take 40-100ms and should not block
2476 // initialization.
2477 base::PostTaskWithTraitsAndReplyWithResult(
2478 FROM_HERE,
2479 {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
2480 base::BindOnce(&HaveOnlyLoopbackAddresses),
2481 base::BindOnce(&HostResolverImpl::SetHaveOnlyLoopbackAddresses,
2482 weak_ptr_factory_.GetWeakPtr()));
mmenke91c17162016-06-02 16:03:232483}
2484
[email protected]35ddc282010-09-21 23:42:062485void HostResolverImpl::AbortAllInProgressJobs() {
[email protected]b3601bc22012-02-21 21:23:202486 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2487 // first collect and remove all running jobs from |jobs_|.
danakj22f90e72016-04-16 01:55:402488 std::vector<std::unique_ptr<Job>> jobs_to_abort;
avi9e72eb482016-12-09 21:10:542489 for (auto it = jobs_.begin(); it != jobs_.end();) {
2490 Job* job = it->second.get();
[email protected]0f292de02012-02-01 22:28:202491 if (job->is_running()) {
avi9e72eb482016-12-09 21:10:542492 jobs_to_abort.push_back(std::move(it->second));
[email protected]b3601bc22012-02-21 21:23:202493 jobs_.erase(it++);
[email protected]0f292de02012-02-01 22:28:202494 } else {
[email protected]b3601bc22012-02-21 21:23:202495 DCHECK(job->is_queued());
2496 ++it;
[email protected]0f292de02012-02-01 22:28:202497 }
[email protected]ef4c40c2010-09-01 14:42:032498 }
[email protected]b3601bc22012-02-21 21:23:202499
[email protected]daae1322013-09-05 18:26:502500 // Pause the dispatcher so it won't start any new dispatcher jobs while
2501 // aborting the old ones. This is needed so that it won't start the second
2502 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2503 // invalid.
[email protected]106ccd2c2014-06-17 09:21:002504 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2505 dispatcher_->SetLimits(
[email protected]daae1322013-09-05 18:26:502506 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
[email protected]70c04ab2013-08-22 16:05:122507
[email protected]57a48d32012-03-03 00:04:552508 // Life check to bail once |this| is deleted.
[email protected]4589a3a2012-09-20 20:57:072509 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]57a48d32012-03-03 00:04:552510
[email protected]16ee26d2012-03-08 03:34:352511 // Then Abort them.
[email protected]11fbca0b2013-06-02 23:37:212512 for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
[email protected]57a48d32012-03-03 00:04:552513 jobs_to_abort[i]->Abort();
olli.raulaa21e9eb72015-12-17 08:18:112514 ignore_result(jobs_to_abort[i].release());
[email protected]b3601bc22012-02-21 21:23:202515 }
[email protected]daae1322013-09-05 18:26:502516
2517 if (self)
[email protected]106ccd2c2014-06-17 09:21:002518 dispatcher_->SetLimits(limits);
[email protected]daae1322013-09-05 18:26:502519}
2520
2521void HostResolverImpl::AbortDnsTasks() {
2522 // Pause the dispatcher so it won't start any new dispatcher jobs while
2523 // aborting the old ones. This is needed so that it won't start the second
2524 // DnsTransaction for a job if the DnsConfig just changed.
[email protected]106ccd2c2014-06-17 09:21:002525 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2526 dispatcher_->SetLimits(
[email protected]daae1322013-09-05 18:26:502527 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2528
avi9e72eb482016-12-09 21:10:542529 for (auto it = jobs_.begin(); it != jobs_.end(); ++it)
[email protected]daae1322013-09-05 18:26:502530 it->second->AbortDnsTask();
[email protected]106ccd2c2014-06-17 09:21:002531 dispatcher_->SetLimits(limits);
[email protected]ef4c40c2010-09-01 14:42:032532}
2533
[email protected]78eac2a2012-03-14 19:09:272534void HostResolverImpl::TryServingAllJobsFromHosts() {
2535 if (!HaveDnsConfig())
2536 return;
2537
2538 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2539 // http://crbug.com/117655
2540
2541 // Life check to bail once |this| is deleted.
[email protected]4589a3a2012-09-20 20:57:072542 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]78eac2a2012-03-14 19:09:272543
avi9e72eb482016-12-09 21:10:542544 for (auto it = jobs_.begin(); self.get() && it != jobs_.end();) {
2545 Job* job = it->second.get();
[email protected]78eac2a2012-03-14 19:09:272546 ++it;
2547 // This could remove |job| from |jobs_|, but iterator will remain valid.
2548 job->ServeFromHosts();
2549 }
2550}
2551
[email protected]be1a48b2011-01-20 00:12:132552void HostResolverImpl::OnIPAddressChanged() {
sergeyub8cdc212015-05-14 18:50:372553 last_ipv6_probe_time_ = base::TimeTicks();
[email protected]12faa4c2012-11-06 04:44:182554 // Abandon all ProbeJobs.
2555 probe_weak_ptr_factory_.InvalidateWeakPtrs();
Matt Menkeef8f5cf82017-07-20 22:50:172556 if (cache_.get())
mgersh5bb3b9d2017-02-13 21:27:102557 cache_->OnNetworkChange();
Fabrice de Gans-Riberi7de47372018-05-08 20:23:472558#if (defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)) || \
2559 defined(OS_FUCHSIA)
mmenke91c17162016-06-02 16:03:232560 RunLoopbackProbeJob();
[email protected]be1a48b2011-01-20 00:12:132561#endif
2562 AbortAllInProgressJobs();
2563 // |this| may be deleted inside AbortAllInProgressJobs().
2564}
2565
ttuttlecf1158bf2016-03-18 16:37:442566void HostResolverImpl::OnConnectionTypeChanged(
2567 NetworkChangeNotifier::ConnectionType type) {
2568 proc_params_.unresponsive_delay =
2569 GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
2570 "DnsUnresponsiveDelayMsByConnectionType",
2571 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs),
2572 type);
2573}
2574
pauljensen101ed372015-04-17 00:11:422575void HostResolverImpl::OnInitialDNSConfigRead() {
2576 UpdateDNSConfig(false);
2577}
2578
[email protected]bb0e34542012-08-31 19:52:402579void HostResolverImpl::OnDNSChanged() {
pauljensen101ed372015-04-17 00:11:422580 UpdateDNSConfig(true);
2581}
2582
2583void HostResolverImpl::UpdateDNSConfig(bool config_changed) {
[email protected]bb0e34542012-08-31 19:52:402584 DnsConfig dns_config;
2585 NetworkChangeNotifier::GetDnsConfig(&dns_config);
[email protected]ec666ab22013-04-17 20:05:592586
[email protected]b4481b222012-03-16 17:13:112587 if (net_log_) {
mikecirone8b85c432016-09-08 19:11:002588 net_log_->AddGlobalEntry(NetLogEventType::DNS_CONFIG_CHANGED,
2589 base::Bind(&NetLogDnsConfigCallback, &dns_config));
[email protected]b4481b222012-03-16 17:13:112590 }
2591
[email protected]01b3b9d2012-08-13 16:18:142592 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
[email protected]d7b9a2b2012-05-31 22:31:192593 received_dns_config_ = dns_config.IsValid();
[email protected]c9fa8f312013-09-17 12:24:522594 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2595 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
[email protected]78eac2a2012-03-14 19:09:272596
[email protected]a8883e452012-11-17 05:58:062597 num_dns_failures_ = 0;
2598
[email protected]01b3b9d2012-08-13 16:18:142599 // We want a new DnsSession in place, before we Abort running Jobs, so that
2600 // the newly started jobs use the new config.
[email protected]f0f602bd2012-11-15 18:01:022601 if (dns_client_.get()) {
Miriam Gershenson44aafc122017-10-18 19:29:252602 // Make sure that if the update is an initial read, not a change, there
Miriam Gershenson668e8442017-10-31 20:31:532603 // wasn't already a DnsConfig or it's the same one.
2604 DCHECK(config_changed || !dns_client_->GetConfig() ||
2605 dns_client_->GetConfig()->Equals(dns_config));
Brad Lassey786929ad2018-02-21 20:54:272606 dns_config.dns_over_https_servers = dns_over_https_servers_;
[email protected]d7b9a2b2012-05-31 22:31:192607 dns_client_->SetConfig(dns_config);
Miriam Gershenson44aafc122017-10-18 19:29:252608 if (dns_client_->GetConfig())
[email protected]f0f602bd2012-11-15 18:01:022609 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
[email protected]f0f602bd2012-11-15 18:01:022610 }
[email protected]01b3b9d2012-08-13 16:18:142611
pauljensen101ed372015-04-17 00:11:422612 if (config_changed) {
2613 // If the DNS server has changed, existing cached info could be wrong so we
mgersh92b1af12017-03-29 15:49:232614 // have to expire our internal cache :( Note that OS level DNS caches, such
pauljensen101ed372015-04-17 00:11:422615 // as NSCD's cache should be dropped automatically by the OS when
2616 // resolv.conf changes so we don't need to do anything to clear that cache.
Matt Menkeef8f5cf82017-07-20 22:50:172617 if (cache_.get())
mgersh92b1af12017-03-29 15:49:232618 cache_->OnNetworkChange();
[email protected]01b3b9d2012-08-13 16:18:142619
pauljensen101ed372015-04-17 00:11:422620 // Life check to bail once |this| is deleted.
2621 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
[email protected]f0f602bd2012-11-15 18:01:022622
pauljensen101ed372015-04-17 00:11:422623 // Existing jobs will have been sent to the original server so they need to
2624 // be aborted.
2625 AbortAllInProgressJobs();
[email protected]01b3b9d2012-08-13 16:18:142626
pauljensen101ed372015-04-17 00:11:422627 // |this| may be deleted inside AbortAllInProgressJobs().
2628 if (self.get())
2629 TryServingAllJobsFromHosts();
2630 }
[email protected]78eac2a2012-03-14 19:09:272631}
2632
2633bool HostResolverImpl::HaveDnsConfig() const {
[email protected]32b1dbcf2013-01-26 03:48:252634 // Use DnsClient only if it's fully configured and there is no override by
2635 // ScopedDefaultHostResolverProc.
2636 // The alternative is to use NetworkChangeNotifier to override DnsConfig,
2637 // but that would introduce construction order requirements for NCN and SDHRP.
wezb9820d42016-06-22 23:41:042638 return dns_client_ && dns_client_->GetConfig() &&
2639 (proc_params_.resolver_proc || !HostResolverProc::GetDefault());
[email protected]b3601bc22012-02-21 21:23:202640}
2641
[email protected]1ffdda82012-12-12 23:04:222642void HostResolverImpl::OnDnsTaskResolve(int net_error) {
[email protected]f0f602bd2012-11-15 18:01:022643 DCHECK(dns_client_);
[email protected]1ffdda82012-12-12 23:04:222644 if (net_error == OK) {
[email protected]f0f602bd2012-11-15 18:01:022645 num_dns_failures_ = 0;
2646 return;
2647 }
2648 ++num_dns_failures_;
2649 if (num_dns_failures_ < kMaximumDnsFailures)
2650 return;
[email protected]daae1322013-09-05 18:26:502651
2652 // Disable DnsClient until the next DNS change. Must be done before aborting
2653 // DnsTasks, since doing so may start new jobs.
[email protected]f0f602bd2012-11-15 18:01:022654 dns_client_->SetConfig(DnsConfig());
[email protected]daae1322013-09-05 18:26:502655
2656 // Switch jobs with active DnsTasks over to using ProcTasks.
2657 AbortDnsTasks();
2658
[email protected]f0f602bd2012-11-15 18:01:022659 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
Ilya Sherman0eb39802017-12-08 20:58:182660 base::UmaHistogramSparse("AsyncDNS.DnsClientDisabledReason",
2661 std::abs(net_error));
[email protected]f0f602bd2012-11-15 18:01:022662}
2663
danakj22f90e72016-04-16 01:55:402664void HostResolverImpl::SetDnsClient(std::unique_ptr<DnsClient> dns_client) {
[email protected]daae1322013-09-05 18:26:502665 // DnsClient and config must be updated before aborting DnsTasks, since doing
2666 // so may start new jobs.
dchengc7eeda422015-12-26 03:56:482667 dns_client_ = std::move(dns_client);
[email protected]daae1322013-09-05 18:26:502668 if (dns_client_ && !dns_client_->GetConfig() &&
2669 num_dns_failures_ < kMaximumDnsFailures) {
2670 DnsConfig dns_config;
2671 NetworkChangeNotifier::GetDnsConfig(&dns_config);
Brad Lassey786929ad2018-02-21 20:54:272672 dns_config.dns_over_https_servers = dns_over_https_servers_;
[email protected]daae1322013-09-05 18:26:502673 dns_client_->SetConfig(dns_config);
2674 num_dns_failures_ = 0;
2675 if (dns_client_->GetConfig())
2676 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
[email protected]a8883e452012-11-17 05:58:062677 }
[email protected]daae1322013-09-05 18:26:502678
2679 AbortDnsTasks();
[email protected]a8883e452012-11-17 05:58:062680}
2681
maksim.sisov31452af2016-07-27 06:38:102682HostResolverImpl::RequestImpl::~RequestImpl() {
2683 if (job_)
2684 job_->CancelRequest(this);
2685}
2686
2687void HostResolverImpl::RequestImpl::ChangeRequestPriority(
2688 RequestPriority priority) {
2689 job_->ChangeRequestPriority(this, priority);
2690}
2691
[email protected]b59ff372009-07-15 22:04:322692} // namespace net