Fix mouse warp with 2x displays
[chromium-blink-merge.git] / net / dns / host_resolver_impl.cc
blobd3f83655356922b58551ca13adbec1bf14c157d5
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/dns/host_resolver_impl.h"
7 #if defined(OS_WIN)
8 #include <Winsock2.h>
9 #elif defined(OS_POSIX)
10 #include <netdb.h>
11 #endif
13 #include <cmath>
14 #include <utility>
15 #include <vector>
17 #include "base/basictypes.h"
18 #include "base/bind.h"
19 #include "base/bind_helpers.h"
20 #include "base/callback.h"
21 #include "base/compiler_specific.h"
22 #include "base/debug/debugger.h"
23 #include "base/debug/stack_trace.h"
24 #include "base/metrics/field_trial.h"
25 #include "base/metrics/histogram_macros.h"
26 #include "base/metrics/sparse_histogram.h"
27 #include "base/profiler/scoped_tracker.h"
28 #include "base/single_thread_task_runner.h"
29 #include "base/stl_util.h"
30 #include "base/strings/string_util.h"
31 #include "base/strings/utf_string_conversions.h"
32 #include "base/thread_task_runner_handle.h"
33 #include "base/threading/worker_pool.h"
34 #include "base/time/time.h"
35 #include "base/values.h"
36 #include "net/base/address_family.h"
37 #include "net/base/address_list.h"
38 #include "net/base/dns_reloader.h"
39 #include "net/base/dns_util.h"
40 #include "net/base/host_port_pair.h"
41 #include "net/base/ip_endpoint.h"
42 #include "net/base/net_errors.h"
43 #include "net/base/net_util.h"
44 #include "net/dns/address_sorter.h"
45 #include "net/dns/dns_client.h"
46 #include "net/dns/dns_config_service.h"
47 #include "net/dns/dns_protocol.h"
48 #include "net/dns/dns_response.h"
49 #include "net/dns/dns_transaction.h"
50 #include "net/dns/host_resolver_proc.h"
51 #include "net/log/net_log.h"
52 #include "net/socket/client_socket_factory.h"
53 #include "net/udp/datagram_client_socket.h"
54 #include "url/url_canon_ip.h"
56 #if defined(OS_WIN)
57 #include "net/base/winsock_init.h"
58 #endif
60 namespace net {
62 namespace {
64 // Limit the size of hostnames that will be resolved to combat issues in
65 // some platform's resolvers.
66 const size_t kMaxHostLength = 4096;
68 // Default TTL for successful resolutions with ProcTask.
69 const unsigned kCacheEntryTTLSeconds = 60;
71 // Default TTL for unsuccessful resolutions with ProcTask.
72 const unsigned kNegativeCacheEntryTTLSeconds = 0;
74 // Minimum TTL for successful resolutions with DnsTask.
75 const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
77 // Time between IPv6 probes, i.e. for how long results of each IPv6 probe are
78 // cached.
79 const int kIPv6ProbePeriodMs = 1000;
81 // Google DNS address used for IPv6 probes.
82 const uint8_t kIPv6ProbeAddress[] =
83 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
86 // We use a separate histogram name for each platform to facilitate the
87 // display of error codes by their symbolic name (since each platform has
88 // different mappings).
89 const char kOSErrorsForGetAddrinfoHistogramName[] =
90 #if defined(OS_WIN)
91 "Net.OSErrorsForGetAddrinfo_Win";
92 #elif defined(OS_MACOSX)
93 "Net.OSErrorsForGetAddrinfo_Mac";
94 #elif defined(OS_LINUX)
95 "Net.OSErrorsForGetAddrinfo_Linux";
96 #else
97 "Net.OSErrorsForGetAddrinfo";
98 #endif
100 // Gets a list of the likely error codes that getaddrinfo() can return
101 // (non-exhaustive). These are the error codes that we will track via
102 // a histogram.
103 std::vector<int> GetAllGetAddrinfoOSErrors() {
104 int os_errors[] = {
105 #if defined(OS_POSIX)
106 #if !defined(OS_FREEBSD)
107 #if !defined(OS_ANDROID)
108 // EAI_ADDRFAMILY has been declared obsolete in Android's and
109 // FreeBSD's netdb.h.
110 EAI_ADDRFAMILY,
111 #endif
112 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
113 EAI_NODATA,
114 #endif
115 EAI_AGAIN,
116 EAI_BADFLAGS,
117 EAI_FAIL,
118 EAI_FAMILY,
119 EAI_MEMORY,
120 EAI_NONAME,
121 EAI_SERVICE,
122 EAI_SOCKTYPE,
123 EAI_SYSTEM,
124 #elif defined(OS_WIN)
125 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
126 WSA_NOT_ENOUGH_MEMORY,
127 WSAEAFNOSUPPORT,
128 WSAEINVAL,
129 WSAESOCKTNOSUPPORT,
130 WSAHOST_NOT_FOUND,
131 WSANO_DATA,
132 WSANO_RECOVERY,
133 WSANOTINITIALISED,
134 WSATRY_AGAIN,
135 WSATYPE_NOT_FOUND,
136 // The following are not in doc, but might be to appearing in results :-(.
137 WSA_INVALID_HANDLE,
138 #endif
141 // Ensure all errors are positive, as histogram only tracks positive values.
142 for (size_t i = 0; i < arraysize(os_errors); ++i) {
143 os_errors[i] = std::abs(os_errors[i]);
146 return base::CustomHistogram::ArrayToCustomRanges(os_errors,
147 arraysize(os_errors));
150 enum DnsResolveStatus {
151 RESOLVE_STATUS_DNS_SUCCESS = 0,
152 RESOLVE_STATUS_PROC_SUCCESS,
153 RESOLVE_STATUS_FAIL,
154 RESOLVE_STATUS_SUSPECT_NETBIOS,
155 RESOLVE_STATUS_MAX
158 // ICANN uses this localhost address to indicate a name collision.
160 // The policy in Chromium is to fail host resolving if it resolves to
161 // this special address.
163 // Not however that IP literals are exempt from this policy, so it is still
164 // possible to navigate to http://127.0.53.53/ directly.
166 // For more details: https://www.icann.org/news/announcement-2-2014-08-01-en
167 const unsigned char kIcanNameCollisionIp[] = {127, 0, 53, 53};
169 void UmaAsyncDnsResolveStatus(DnsResolveStatus result) {
170 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
171 result,
172 RESOLVE_STATUS_MAX);
175 bool ResemblesNetBIOSName(const std::string& hostname) {
176 return (hostname.size() < 16) && (hostname.find('.') == std::string::npos);
179 // True if |hostname| ends with either ".local" or ".local.".
180 bool ResemblesMulticastDNSName(const std::string& hostname) {
181 DCHECK(!hostname.empty());
182 const char kSuffix[] = ".local.";
183 const size_t kSuffixLen = sizeof(kSuffix) - 1;
184 const size_t kSuffixLenTrimmed = kSuffixLen - 1;
185 if (hostname[hostname.size() - 1] == '.') {
186 return hostname.size() > kSuffixLen &&
187 !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix);
189 return hostname.size() > kSuffixLenTrimmed &&
190 !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed,
191 kSuffix, kSuffixLenTrimmed);
194 // Attempts to connect a UDP socket to |dest|:53.
195 bool IsGloballyReachable(const IPAddressNumber& dest,
196 const BoundNetLog& net_log) {
197 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
198 tracked_objects::ScopedTracker tracking_profile_1(
199 FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 IsGloballyReachable"));
201 scoped_ptr<DatagramClientSocket> socket(
202 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
203 DatagramSocket::DEFAULT_BIND,
204 RandIntCallback(),
205 net_log.net_log(),
206 net_log.source()));
207 int rv = socket->Connect(IPEndPoint(dest, 53));
208 if (rv != OK)
209 return false;
210 IPEndPoint endpoint;
211 rv = socket->GetLocalAddress(&endpoint);
212 if (rv != OK)
213 return false;
214 DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily());
215 const IPAddressNumber& address = endpoint.address();
216 bool is_link_local = (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80);
217 if (is_link_local)
218 return false;
219 const uint8 kTeredoPrefix[] = { 0x20, 0x01, 0, 0 };
220 bool is_teredo = std::equal(kTeredoPrefix,
221 kTeredoPrefix + arraysize(kTeredoPrefix),
222 address.begin());
223 if (is_teredo)
224 return false;
225 return true;
228 // Provide a common macro to simplify code and readability. We must use a
229 // macro as the underlying HISTOGRAM macro creates static variables.
230 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \
231 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100)
233 // A macro to simplify code and readability.
234 #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
235 do { \
236 switch (priority) { \
237 case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \
238 case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \
239 case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \
240 case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \
241 case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \
242 default: NOTREACHED(); break; \
244 DNS_HISTOGRAM(basename, time); \
245 } while (0)
247 // Record time from Request creation until a valid DNS response.
248 void RecordTotalTime(bool had_dns_config,
249 bool speculative,
250 base::TimeDelta duration) {
251 if (had_dns_config) {
252 if (speculative) {
253 DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration);
254 } else {
255 DNS_HISTOGRAM("AsyncDNS.TotalTime", duration);
257 } else {
258 if (speculative) {
259 DNS_HISTOGRAM("DNS.TotalTime_speculative", duration);
260 } else {
261 DNS_HISTOGRAM("DNS.TotalTime", duration);
266 void RecordTTL(base::TimeDelta ttl) {
267 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl,
268 base::TimeDelta::FromSeconds(1),
269 base::TimeDelta::FromDays(1), 100);
272 bool ConfigureAsyncDnsNoFallbackFieldTrial() {
273 const bool kDefault = false;
275 // Configure the AsyncDns field trial as follows:
276 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
277 // groups AsyncDnsA and AsyncDnsB: return false,
278 // groups SystemDnsA and SystemDnsB: return false,
279 // otherwise (trial absent): return default.
280 std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
281 if (!group_name.empty()) {
282 return base::StartsWith(group_name, "AsyncDnsNoFallback",
283 base::CompareCase::INSENSITIVE_ASCII);
285 return kDefault;
288 //-----------------------------------------------------------------------------
290 AddressList EnsurePortOnAddressList(const AddressList& list, uint16 port) {
291 if (list.empty() || list.front().port() == port)
292 return list;
293 return AddressList::CopyWithPort(list, port);
296 // Returns true if |addresses| contains only IPv4 loopback addresses.
297 bool IsAllIPv4Loopback(const AddressList& addresses) {
298 for (unsigned i = 0; i < addresses.size(); ++i) {
299 const IPAddressNumber& address = addresses[i].address();
300 switch (addresses[i].GetFamily()) {
301 case ADDRESS_FAMILY_IPV4:
302 if (address[0] != 127)
303 return false;
304 break;
305 case ADDRESS_FAMILY_IPV6:
306 return false;
307 default:
308 NOTREACHED();
309 return false;
312 return true;
315 // Creates NetLog parameters when the resolve failed.
316 scoped_ptr<base::Value> NetLogProcTaskFailedCallback(
317 uint32 attempt_number,
318 int net_error,
319 int os_error,
320 NetLogCaptureMode /* capture_mode */) {
321 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
322 if (attempt_number)
323 dict->SetInteger("attempt_number", attempt_number);
325 dict->SetInteger("net_error", net_error);
327 if (os_error) {
328 dict->SetInteger("os_error", os_error);
329 #if defined(OS_POSIX)
330 dict->SetString("os_error_string", gai_strerror(os_error));
331 #elif defined(OS_WIN)
332 // Map the error code to a human-readable string.
333 LPWSTR error_string = NULL;
334 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
335 0, // Use the internal message table.
336 os_error,
337 0, // Use default language.
338 (LPWSTR)&error_string,
339 0, // Buffer size.
340 0); // Arguments (unused).
341 dict->SetString("os_error_string", base::WideToUTF8(error_string));
342 LocalFree(error_string);
343 #endif
346 return dict.Pass();
349 // Creates NetLog parameters when the DnsTask failed.
350 scoped_ptr<base::Value> NetLogDnsTaskFailedCallback(
351 int net_error,
352 int dns_error,
353 NetLogCaptureMode /* capture_mode */) {
354 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
355 dict->SetInteger("net_error", net_error);
356 if (dns_error)
357 dict->SetInteger("dns_error", dns_error);
358 return dict.Pass();
361 // Creates NetLog parameters containing the information in a RequestInfo object,
362 // along with the associated NetLog::Source.
363 scoped_ptr<base::Value> NetLogRequestInfoCallback(
364 const HostResolver::RequestInfo* info,
365 NetLogCaptureMode /* capture_mode */) {
366 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
368 dict->SetString("host", info->host_port_pair().ToString());
369 dict->SetInteger("address_family",
370 static_cast<int>(info->address_family()));
371 dict->SetBoolean("allow_cached_response", info->allow_cached_response());
372 dict->SetBoolean("is_speculative", info->is_speculative());
373 return dict.Pass();
376 // Creates NetLog parameters for the creation of a HostResolverImpl::Job.
377 scoped_ptr<base::Value> NetLogJobCreationCallback(
378 const NetLog::Source& source,
379 const std::string* host,
380 NetLogCaptureMode /* capture_mode */) {
381 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
382 source.AddToEventParameters(dict.get());
383 dict->SetString("host", *host);
384 return dict.Pass();
387 // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
388 scoped_ptr<base::Value> NetLogJobAttachCallback(
389 const NetLog::Source& source,
390 RequestPriority priority,
391 NetLogCaptureMode /* capture_mode */) {
392 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
393 source.AddToEventParameters(dict.get());
394 dict->SetString("priority", RequestPriorityToString(priority));
395 return dict.Pass();
398 // Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
399 scoped_ptr<base::Value> NetLogDnsConfigCallback(
400 const DnsConfig* config,
401 NetLogCaptureMode /* capture_mode */) {
402 return make_scoped_ptr(config->ToValue());
405 scoped_ptr<base::Value> NetLogIPv6AvailableCallback(
406 bool ipv6_available,
407 bool cached,
408 NetLogCaptureMode /* capture_mode */) {
409 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
410 dict->SetBoolean("ipv6_available", ipv6_available);
411 dict->SetBoolean("cached", cached);
412 return dict.Pass();
415 // The logging routines are defined here because some requests are resolved
416 // without a Request object.
418 // Logs when a request has just been started.
419 void LogStartRequest(const BoundNetLog& source_net_log,
420 const HostResolver::RequestInfo& info) {
421 source_net_log.BeginEvent(
422 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST,
423 base::Bind(&NetLogRequestInfoCallback, &info));
426 // Logs when a request has just completed (before its callback is run).
427 void LogFinishRequest(const BoundNetLog& source_net_log,
428 const HostResolver::RequestInfo& info,
429 int net_error) {
430 source_net_log.EndEventWithNetErrorCode(
431 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, net_error);
434 // Logs when a request has been cancelled.
435 void LogCancelRequest(const BoundNetLog& source_net_log,
436 const HostResolverImpl::RequestInfo& info) {
437 source_net_log.AddEvent(NetLog::TYPE_CANCELLED);
438 source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST);
441 //-----------------------------------------------------------------------------
443 // Keeps track of the highest priority.
444 class PriorityTracker {
445 public:
446 explicit PriorityTracker(RequestPriority initial_priority)
447 : highest_priority_(initial_priority), total_count_(0) {
448 memset(counts_, 0, sizeof(counts_));
451 RequestPriority highest_priority() const {
452 return highest_priority_;
455 size_t total_count() const {
456 return total_count_;
459 void Add(RequestPriority req_priority) {
460 ++total_count_;
461 ++counts_[req_priority];
462 if (highest_priority_ < req_priority)
463 highest_priority_ = req_priority;
466 void Remove(RequestPriority req_priority) {
467 DCHECK_GT(total_count_, 0u);
468 DCHECK_GT(counts_[req_priority], 0u);
469 --total_count_;
470 --counts_[req_priority];
471 size_t i;
472 for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i);
473 highest_priority_ = static_cast<RequestPriority>(i);
475 // In absence of requests, default to MINIMUM_PRIORITY.
476 if (total_count_ == 0)
477 DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
480 private:
481 RequestPriority highest_priority_;
482 size_t total_count_;
483 size_t counts_[NUM_PRIORITIES];
486 } // namespace
488 //-----------------------------------------------------------------------------
490 const unsigned HostResolverImpl::kMaximumDnsFailures = 16;
492 // Holds the data for a request that could not be completed synchronously.
493 // It is owned by a Job. Canceled Requests are only marked as canceled rather
494 // than removed from the Job's |requests_| list.
495 class HostResolverImpl::Request {
496 public:
497 Request(const BoundNetLog& source_net_log,
498 const RequestInfo& info,
499 RequestPriority priority,
500 const CompletionCallback& callback,
501 AddressList* addresses)
502 : source_net_log_(source_net_log),
503 info_(info),
504 priority_(priority),
505 job_(NULL),
506 callback_(callback),
507 addresses_(addresses),
508 request_time_(base::TimeTicks::Now()) {}
510 // Mark the request as canceled.
511 void MarkAsCanceled() {
512 job_ = NULL;
513 addresses_ = NULL;
514 callback_.Reset();
517 bool was_canceled() const {
518 return callback_.is_null();
521 void set_job(Job* job) {
522 DCHECK(job);
523 // Identify which job the request is waiting on.
524 job_ = job;
527 // Prepare final AddressList and call completion callback.
528 void OnComplete(int error, const AddressList& addr_list) {
529 DCHECK(!was_canceled());
530 if (error == OK)
531 *addresses_ = EnsurePortOnAddressList(addr_list, info_.port());
532 CompletionCallback callback = callback_;
533 MarkAsCanceled();
534 callback.Run(error);
537 Job* job() const {
538 return job_;
541 // NetLog for the source, passed in HostResolver::Resolve.
542 const BoundNetLog& source_net_log() {
543 return source_net_log_;
546 const RequestInfo& info() const {
547 return info_;
550 RequestPriority priority() const { return priority_; }
552 base::TimeTicks request_time() const { return request_time_; }
554 private:
555 const BoundNetLog source_net_log_;
557 // The request info that started the request.
558 const RequestInfo info_;
560 // TODO(akalin): Support reprioritization.
561 const RequestPriority priority_;
563 // The resolve job that this request is dependent on.
564 Job* job_;
566 // The user's callback to invoke when the request completes.
567 CompletionCallback callback_;
569 // The address list to save result into.
570 AddressList* addresses_;
572 const base::TimeTicks request_time_;
574 DISALLOW_COPY_AND_ASSIGN(Request);
577 //------------------------------------------------------------------------------
579 // Calls HostResolverProc on the WorkerPool. Performs retries if necessary.
581 // Whenever we try to resolve the host, we post a delayed task to check if host
582 // resolution (OnLookupComplete) is completed or not. If the original attempt
583 // hasn't completed, then we start another attempt for host resolution. We take
584 // the results from the first attempt that finishes and ignore the results from
585 // all other attempts.
587 // TODO(szym): Move to separate source file for testing and mocking.
589 class HostResolverImpl::ProcTask
590 : public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> {
591 public:
592 typedef base::Callback<void(int net_error,
593 const AddressList& addr_list)> Callback;
595 ProcTask(const Key& key,
596 const ProcTaskParams& params,
597 const Callback& callback,
598 const BoundNetLog& job_net_log)
599 : key_(key),
600 params_(params),
601 callback_(callback),
602 task_runner_(base::ThreadTaskRunnerHandle::Get()),
603 attempt_number_(0),
604 completed_attempt_number_(0),
605 completed_attempt_error_(ERR_UNEXPECTED),
606 had_non_speculative_request_(false),
607 net_log_(job_net_log) {
608 if (!params_.resolver_proc.get())
609 params_.resolver_proc = HostResolverProc::GetDefault();
610 // If default is unset, use the system proc.
611 if (!params_.resolver_proc.get())
612 params_.resolver_proc = new SystemHostResolverProc();
615 void Start() {
616 DCHECK(task_runner_->BelongsToCurrentThread());
617 net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK);
618 StartLookupAttempt();
621 // Cancels this ProcTask. It will be orphaned. Any outstanding resolve
622 // attempts running on worker threads will continue running. Only once all the
623 // attempts complete will the final reference to this ProcTask be released.
624 void Cancel() {
625 DCHECK(task_runner_->BelongsToCurrentThread());
627 if (was_canceled() || was_completed())
628 return;
630 callback_.Reset();
631 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK);
634 void set_had_non_speculative_request() {
635 DCHECK(task_runner_->BelongsToCurrentThread());
636 had_non_speculative_request_ = true;
639 bool was_canceled() const {
640 DCHECK(task_runner_->BelongsToCurrentThread());
641 return callback_.is_null();
644 bool was_completed() const {
645 DCHECK(task_runner_->BelongsToCurrentThread());
646 return completed_attempt_number_ > 0;
649 private:
650 friend class base::RefCountedThreadSafe<ProcTask>;
651 ~ProcTask() {}
653 void StartLookupAttempt() {
654 DCHECK(task_runner_->BelongsToCurrentThread());
655 base::TimeTicks start_time = base::TimeTicks::Now();
656 ++attempt_number_;
657 // Dispatch the lookup attempt to a worker thread.
658 if (!base::WorkerPool::PostTask(
659 FROM_HERE,
660 base::Bind(&ProcTask::DoLookup, this, start_time, attempt_number_),
661 true)) {
662 NOTREACHED();
664 // Since we could be running within Resolve() right now, we can't just
665 // call OnLookupComplete(). Instead we must wait until Resolve() has
666 // returned (IO_PENDING).
667 task_runner_->PostTask(FROM_HERE,
668 base::Bind(&ProcTask::OnLookupComplete,
669 this,
670 AddressList(),
671 start_time,
672 attempt_number_,
673 ERR_UNEXPECTED,
674 0));
675 return;
678 net_log_.AddEvent(
679 NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED,
680 NetLog::IntegerCallback("attempt_number", attempt_number_));
682 // If we don't get the results within a given time, RetryIfNotComplete
683 // will start a new attempt on a different worker thread if none of our
684 // outstanding attempts have completed yet.
685 if (attempt_number_ <= params_.max_retry_attempts) {
686 task_runner_->PostDelayedTask(
687 FROM_HERE,
688 base::Bind(&ProcTask::RetryIfNotComplete, this),
689 params_.unresponsive_delay);
693 // WARNING: This code runs inside a worker pool. The shutdown code cannot
694 // wait for it to finish, so we must be very careful here about using other
695 // objects (like MessageLoops, Singletons, etc). During shutdown these objects
696 // may no longer exist. Multiple DoLookups() could be running in parallel, so
697 // any state inside of |this| must not mutate .
698 void DoLookup(const base::TimeTicks& start_time,
699 const uint32 attempt_number) {
700 AddressList results;
701 int os_error = 0;
702 // Running on the worker thread
703 int error = params_.resolver_proc->Resolve(key_.hostname,
704 key_.address_family,
705 key_.host_resolver_flags,
706 &results,
707 &os_error);
709 // Fail the resolution if the result contains 127.0.53.53. See the comment
710 // block of kIcanNameCollisionIp for details on why.
711 for (const auto& it : results) {
712 const IPAddressNumber& cur = it.address();
713 if (cur.size() == arraysize(kIcanNameCollisionIp) &&
714 0 == memcmp(&cur.front(), kIcanNameCollisionIp, cur.size())) {
715 error = ERR_ICANN_NAME_COLLISION;
716 break;
720 task_runner_->PostTask(FROM_HERE,
721 base::Bind(&ProcTask::OnLookupComplete,
722 this,
723 results,
724 start_time,
725 attempt_number,
726 error,
727 os_error));
730 // Makes next attempt if DoLookup() has not finished (runs on task runner
731 // thread).
732 void RetryIfNotComplete() {
733 DCHECK(task_runner_->BelongsToCurrentThread());
735 if (was_completed() || was_canceled())
736 return;
738 params_.unresponsive_delay *= params_.retry_factor;
739 StartLookupAttempt();
742 // Callback for when DoLookup() completes (runs on task runner thread).
743 void OnLookupComplete(const AddressList& results,
744 const base::TimeTicks& start_time,
745 const uint32 attempt_number,
746 int error,
747 const int os_error) {
748 DCHECK(task_runner_->BelongsToCurrentThread());
749 // If results are empty, we should return an error.
750 bool empty_list_on_ok = (error == OK && results.empty());
751 UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok);
752 if (empty_list_on_ok)
753 error = ERR_NAME_NOT_RESOLVED;
755 bool was_retry_attempt = attempt_number > 1;
757 // Ideally the following code would be part of host_resolver_proc.cc,
758 // however it isn't safe to call NetworkChangeNotifier from worker threads.
759 // So we do it here on the IO thread instead.
760 if (error != OK && NetworkChangeNotifier::IsOffline())
761 error = ERR_INTERNET_DISCONNECTED;
763 // If this is the first attempt that is finishing later, then record data
764 // for the first attempt. Won't contaminate with retry attempt's data.
765 if (!was_retry_attempt)
766 RecordPerformanceHistograms(start_time, error, os_error);
768 RecordAttemptHistograms(start_time, attempt_number, error, os_error);
770 if (was_canceled())
771 return;
773 NetLog::ParametersCallback net_log_callback;
774 if (error != OK) {
775 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
776 attempt_number,
777 error,
778 os_error);
779 } else {
780 net_log_callback = NetLog::IntegerCallback("attempt_number",
781 attempt_number);
783 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED,
784 net_log_callback);
786 if (was_completed())
787 return;
789 // Copy the results from the first worker thread that resolves the host.
790 results_ = results;
791 completed_attempt_number_ = attempt_number;
792 completed_attempt_error_ = error;
794 if (was_retry_attempt) {
795 // If retry attempt finishes before 1st attempt, then get stats on how
796 // much time is saved by having spawned an extra attempt.
797 retry_attempt_finished_time_ = base::TimeTicks::Now();
800 if (error != OK) {
801 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
802 0, error, os_error);
803 } else {
804 net_log_callback = results_.CreateNetLogCallback();
806 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK,
807 net_log_callback);
809 callback_.Run(error, results_);
812 void RecordPerformanceHistograms(const base::TimeTicks& start_time,
813 const int error,
814 const int os_error) const {
815 DCHECK(task_runner_->BelongsToCurrentThread());
816 enum Category { // Used in UMA_HISTOGRAM_ENUMERATION.
817 RESOLVE_SUCCESS,
818 RESOLVE_FAIL,
819 RESOLVE_SPECULATIVE_SUCCESS,
820 RESOLVE_SPECULATIVE_FAIL,
821 RESOLVE_MAX, // Bounding value.
823 int category = RESOLVE_MAX; // Illegal value for later DCHECK only.
825 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
826 if (error == OK) {
827 if (had_non_speculative_request_) {
828 category = RESOLVE_SUCCESS;
829 DNS_HISTOGRAM("DNS.ResolveSuccess", duration);
830 } else {
831 category = RESOLVE_SPECULATIVE_SUCCESS;
832 DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration);
835 // Log DNS lookups based on |address_family|. This will help us determine
836 // if IPv4 or IPv4/6 lookups are faster or slower.
837 switch(key_.address_family) {
838 case ADDRESS_FAMILY_IPV4:
839 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration);
840 break;
841 case ADDRESS_FAMILY_IPV6:
842 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration);
843 break;
844 case ADDRESS_FAMILY_UNSPECIFIED:
845 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration);
846 break;
848 } else {
849 if (had_non_speculative_request_) {
850 category = RESOLVE_FAIL;
851 DNS_HISTOGRAM("DNS.ResolveFail", duration);
852 } else {
853 category = RESOLVE_SPECULATIVE_FAIL;
854 DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration);
856 // Log DNS lookups based on |address_family|. This will help us determine
857 // if IPv4 or IPv4/6 lookups are faster or slower.
858 switch(key_.address_family) {
859 case ADDRESS_FAMILY_IPV4:
860 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration);
861 break;
862 case ADDRESS_FAMILY_IPV6:
863 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration);
864 break;
865 case ADDRESS_FAMILY_UNSPECIFIED:
866 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration);
867 break;
869 UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName,
870 std::abs(os_error),
871 GetAllGetAddrinfoOSErrors());
873 DCHECK_LT(category, static_cast<int>(RESOLVE_MAX)); // Be sure it was set.
875 UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category, RESOLVE_MAX);
878 void RecordAttemptHistograms(const base::TimeTicks& start_time,
879 const uint32 attempt_number,
880 const int error,
881 const int os_error) const {
882 DCHECK(task_runner_->BelongsToCurrentThread());
883 bool first_attempt_to_complete =
884 completed_attempt_number_ == attempt_number;
885 bool is_first_attempt = (attempt_number == 1);
887 if (first_attempt_to_complete) {
888 // If this was first attempt to complete, then record the resolution
889 // status of the attempt.
890 if (completed_attempt_error_ == OK) {
891 UMA_HISTOGRAM_ENUMERATION(
892 "DNS.AttemptFirstSuccess", attempt_number, 100);
893 } else {
894 UMA_HISTOGRAM_ENUMERATION(
895 "DNS.AttemptFirstFailure", attempt_number, 100);
899 if (error == OK)
900 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100);
901 else
902 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100);
904 // If first attempt didn't finish before retry attempt, then calculate stats
905 // on how much time is saved by having spawned an extra attempt.
906 if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) {
907 DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry",
908 base::TimeTicks::Now() - retry_attempt_finished_time_);
911 if (was_canceled() || !first_attempt_to_complete) {
912 // Count those attempts which completed after the job was already canceled
913 // OR after the job was already completed by an earlier attempt (so in
914 // effect).
915 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100);
917 // Record if job is canceled.
918 if (was_canceled())
919 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100);
922 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
923 if (error == OK)
924 DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration);
925 else
926 DNS_HISTOGRAM("DNS.AttemptFailDuration", duration);
929 // Set on the task runner thread, read on the worker thread.
930 Key key_;
932 // Holds an owning reference to the HostResolverProc that we are going to use.
933 // This may not be the current resolver procedure by the time we call
934 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
935 // reference ensures that it remains valid until we are done.
936 ProcTaskParams params_;
938 // The listener to the results of this ProcTask.
939 Callback callback_;
941 // Used to post ourselves onto the task runner thread.
942 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
944 // Keeps track of the number of attempts we have made so far to resolve the
945 // host. Whenever we start an attempt to resolve the host, we increase this
946 // number.
947 uint32 attempt_number_;
949 // The index of the attempt which finished first (or 0 if the job is still in
950 // progress).
951 uint32 completed_attempt_number_;
953 // The result (a net error code) from the first attempt to complete.
954 int completed_attempt_error_;
956 // The time when retry attempt was finished.
957 base::TimeTicks retry_attempt_finished_time_;
959 // True if a non-speculative request was ever attached to this job
960 // (regardless of whether or not it was later canceled.
961 // This boolean is used for histogramming the duration of jobs used to
962 // service non-speculative requests.
963 bool had_non_speculative_request_;
965 AddressList results_;
967 BoundNetLog net_log_;
969 DISALLOW_COPY_AND_ASSIGN(ProcTask);
972 //-----------------------------------------------------------------------------
974 // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as
975 // it takes 40-100ms and should not block initialization.
976 class HostResolverImpl::LoopbackProbeJob {
977 public:
978 explicit LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver)
979 : resolver_(resolver),
980 result_(false) {
981 DCHECK(resolver.get());
982 const bool kIsSlow = true;
983 base::WorkerPool::PostTaskAndReply(
984 FROM_HERE,
985 base::Bind(&LoopbackProbeJob::DoProbe, base::Unretained(this)),
986 base::Bind(&LoopbackProbeJob::OnProbeComplete, base::Owned(this)),
987 kIsSlow);
990 virtual ~LoopbackProbeJob() {}
992 private:
993 // Runs on worker thread.
994 void DoProbe() {
995 result_ = HaveOnlyLoopbackAddresses();
998 void OnProbeComplete() {
999 if (!resolver_.get())
1000 return;
1001 resolver_->SetHaveOnlyLoopbackAddresses(result_);
1004 // Used/set only on task runner thread.
1005 base::WeakPtr<HostResolverImpl> resolver_;
1007 bool result_;
1009 DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob);
1012 //-----------------------------------------------------------------------------
1014 // Resolves the hostname using DnsTransaction.
1015 // TODO(szym): This could be moved to separate source file as well.
1016 class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> {
1017 public:
1018 class Delegate {
1019 public:
1020 virtual void OnDnsTaskComplete(base::TimeTicks start_time,
1021 int net_error,
1022 const AddressList& addr_list,
1023 base::TimeDelta ttl) = 0;
1025 // Called when the first of two jobs succeeds. If the first completed
1026 // transaction fails, this is not called. Also not called when the DnsTask
1027 // only needs to run one transaction.
1028 virtual void OnFirstDnsTransactionComplete() = 0;
1030 protected:
1031 Delegate() {}
1032 virtual ~Delegate() {}
1035 DnsTask(DnsClient* client,
1036 const Key& key,
1037 Delegate* delegate,
1038 const BoundNetLog& job_net_log)
1039 : client_(client),
1040 key_(key),
1041 delegate_(delegate),
1042 net_log_(job_net_log),
1043 num_completed_transactions_(0),
1044 task_start_time_(base::TimeTicks::Now()) {
1045 DCHECK(client);
1046 DCHECK(delegate_);
1049 bool needs_two_transactions() const {
1050 return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED;
1053 bool needs_another_transaction() const {
1054 return needs_two_transactions() && !transaction_aaaa_;
1057 void StartFirstTransaction() {
1058 DCHECK_EQ(0u, num_completed_transactions_);
1059 net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK);
1060 if (key_.address_family == ADDRESS_FAMILY_IPV6) {
1061 StartAAAA();
1062 } else {
1063 StartA();
1067 void StartSecondTransaction() {
1068 DCHECK(needs_two_transactions());
1069 StartAAAA();
1072 private:
1073 void StartA() {
1074 DCHECK(!transaction_a_);
1075 DCHECK_NE(ADDRESS_FAMILY_IPV6, key_.address_family);
1076 transaction_a_ = CreateTransaction(ADDRESS_FAMILY_IPV4);
1077 transaction_a_->Start();
1080 void StartAAAA() {
1081 DCHECK(!transaction_aaaa_);
1082 DCHECK_NE(ADDRESS_FAMILY_IPV4, key_.address_family);
1083 transaction_aaaa_ = CreateTransaction(ADDRESS_FAMILY_IPV6);
1084 transaction_aaaa_->Start();
1087 scoped_ptr<DnsTransaction> CreateTransaction(AddressFamily family) {
1088 DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family);
1089 return client_->GetTransactionFactory()->CreateTransaction(
1090 key_.hostname,
1091 family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA :
1092 dns_protocol::kTypeA,
1093 base::Bind(&DnsTask::OnTransactionComplete, base::Unretained(this),
1094 base::TimeTicks::Now()),
1095 net_log_);
1098 void OnTransactionComplete(const base::TimeTicks& start_time,
1099 DnsTransaction* transaction,
1100 int net_error,
1101 const DnsResponse* response) {
1102 DCHECK(transaction);
1103 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
1104 if (net_error != OK) {
1105 DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration);
1106 OnFailure(net_error, DnsResponse::DNS_PARSE_OK);
1107 return;
1110 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration);
1111 switch (transaction->GetType()) {
1112 case dns_protocol::kTypeA:
1113 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration);
1114 break;
1115 case dns_protocol::kTypeAAAA:
1116 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration);
1117 break;
1120 AddressList addr_list;
1121 base::TimeDelta ttl;
1122 DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl);
1123 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
1124 result,
1125 DnsResponse::DNS_PARSE_RESULT_MAX);
1126 if (result != DnsResponse::DNS_PARSE_OK) {
1127 // Fail even if the other query succeeds.
1128 OnFailure(ERR_DNS_MALFORMED_RESPONSE, result);
1129 return;
1132 ++num_completed_transactions_;
1133 if (num_completed_transactions_ == 1) {
1134 ttl_ = ttl;
1135 } else {
1136 ttl_ = std::min(ttl_, ttl);
1139 if (transaction->GetType() == dns_protocol::kTypeA) {
1140 DCHECK_EQ(transaction_a_.get(), transaction);
1141 // Place IPv4 addresses after IPv6.
1142 addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end());
1143 } else {
1144 DCHECK_EQ(transaction_aaaa_.get(), transaction);
1145 // Place IPv6 addresses before IPv4.
1146 addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end());
1149 if (needs_two_transactions() && num_completed_transactions_ == 1) {
1150 // No need to repeat the suffix search.
1151 key_.hostname = transaction->GetHostname();
1152 delegate_->OnFirstDnsTransactionComplete();
1153 return;
1156 if (addr_list_.empty()) {
1157 // TODO(szym): Don't fallback to ProcTask in this case.
1158 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
1159 return;
1162 // If there are multiple addresses, and at least one is IPv6, need to sort
1163 // them. Note that IPv6 addresses are always put before IPv4 ones, so it's
1164 // sufficient to just check the family of the first address.
1165 if (addr_list_.size() > 1 &&
1166 addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) {
1167 // Sort addresses if needed. Sort could complete synchronously.
1168 client_->GetAddressSorter()->Sort(
1169 addr_list_,
1170 base::Bind(&DnsTask::OnSortComplete,
1171 AsWeakPtr(),
1172 base::TimeTicks::Now()));
1173 } else {
1174 OnSuccess(addr_list_);
1178 void OnSortComplete(base::TimeTicks start_time,
1179 bool success,
1180 const AddressList& addr_list) {
1181 if (!success) {
1182 DNS_HISTOGRAM("AsyncDNS.SortFailure",
1183 base::TimeTicks::Now() - start_time);
1184 OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK);
1185 return;
1188 DNS_HISTOGRAM("AsyncDNS.SortSuccess",
1189 base::TimeTicks::Now() - start_time);
1191 // AddressSorter prunes unusable destinations.
1192 if (addr_list.empty()) {
1193 LOG(WARNING) << "Address list empty after RFC3484 sort";
1194 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
1195 return;
1198 OnSuccess(addr_list);
1201 void OnFailure(int net_error, DnsResponse::Result result) {
1202 DCHECK_NE(OK, net_error);
1203 net_log_.EndEvent(
1204 NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK,
1205 base::Bind(&NetLogDnsTaskFailedCallback, net_error, result));
1206 delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(),
1207 base::TimeDelta());
1210 void OnSuccess(const AddressList& addr_list) {
1211 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK,
1212 addr_list.CreateNetLogCallback());
1213 delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_);
1216 DnsClient* client_;
1217 Key key_;
1219 // The listener to the results of this DnsTask.
1220 Delegate* delegate_;
1221 const BoundNetLog net_log_;
1223 scoped_ptr<DnsTransaction> transaction_a_;
1224 scoped_ptr<DnsTransaction> transaction_aaaa_;
1226 unsigned num_completed_transactions_;
1228 // These are updated as each transaction completes.
1229 base::TimeDelta ttl_;
1230 // IPv6 addresses must appear first in the list.
1231 AddressList addr_list_;
1233 base::TimeTicks task_start_time_;
1235 DISALLOW_COPY_AND_ASSIGN(DnsTask);
1238 //-----------------------------------------------------------------------------
1240 // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch.
1241 class HostResolverImpl::Job : public PrioritizedDispatcher::Job,
1242 public HostResolverImpl::DnsTask::Delegate {
1243 public:
1244 // Creates new job for |key| where |request_net_log| is bound to the
1245 // request that spawned it.
1246 Job(const base::WeakPtr<HostResolverImpl>& resolver,
1247 const Key& key,
1248 RequestPriority priority,
1249 const BoundNetLog& source_net_log)
1250 : resolver_(resolver),
1251 key_(key),
1252 priority_tracker_(priority),
1253 had_non_speculative_request_(false),
1254 had_dns_config_(false),
1255 num_occupied_job_slots_(0),
1256 dns_task_error_(OK),
1257 creation_time_(base::TimeTicks::Now()),
1258 priority_change_time_(creation_time_),
1259 net_log_(BoundNetLog::Make(source_net_log.net_log(),
1260 NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)) {
1261 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB);
1263 net_log_.BeginEvent(
1264 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1265 base::Bind(&NetLogJobCreationCallback,
1266 source_net_log.source(),
1267 &key_.hostname));
1270 ~Job() override {
1271 if (is_running()) {
1272 // |resolver_| was destroyed with this Job still in flight.
1273 // Clean-up, record in the log, but don't run any callbacks.
1274 if (is_proc_running()) {
1275 proc_task_->Cancel();
1276 proc_task_ = NULL;
1278 // Clean up now for nice NetLog.
1279 KillDnsTask();
1280 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1281 ERR_ABORTED);
1282 } else if (is_queued()) {
1283 // |resolver_| was destroyed without running this Job.
1284 // TODO(szym): is there any benefit in having this distinction?
1285 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
1286 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB);
1288 // else CompleteRequests logged EndEvent.
1290 // Log any remaining Requests as cancelled.
1291 for (RequestsList::const_iterator it = requests_.begin();
1292 it != requests_.end(); ++it) {
1293 Request* req = *it;
1294 if (req->was_canceled())
1295 continue;
1296 DCHECK_EQ(this, req->job());
1297 LogCancelRequest(req->source_net_log(), req->info());
1301 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1302 // of the queue.
1303 void Schedule(bool at_head) {
1304 DCHECK(!is_queued());
1305 PrioritizedDispatcher::Handle handle;
1306 if (!at_head) {
1307 handle = resolver_->dispatcher_->Add(this, priority());
1308 } else {
1309 handle = resolver_->dispatcher_->AddAtHead(this, priority());
1311 // The dispatcher could have started |this| in the above call to Add, which
1312 // could have called Schedule again. In that case |handle| will be null,
1313 // but |handle_| may have been set by the other nested call to Schedule.
1314 if (!handle.is_null()) {
1315 DCHECK(handle_.is_null());
1316 handle_ = handle;
1320 void AddRequest(scoped_ptr<Request> req) {
1321 DCHECK_EQ(key_.hostname, req->info().hostname());
1323 req->set_job(this);
1324 priority_tracker_.Add(req->priority());
1326 req->source_net_log().AddEvent(
1327 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH,
1328 net_log_.source().ToEventParametersCallback());
1330 net_log_.AddEvent(
1331 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH,
1332 base::Bind(&NetLogJobAttachCallback,
1333 req->source_net_log().source(),
1334 priority()));
1336 // TODO(szym): Check if this is still needed.
1337 if (!req->info().is_speculative()) {
1338 had_non_speculative_request_ = true;
1339 if (proc_task_.get())
1340 proc_task_->set_had_non_speculative_request();
1343 requests_.push_back(req.Pass());
1345 UpdatePriority();
1348 // Marks |req| as cancelled. If it was the last active Request, also finishes
1349 // this Job, marking it as cancelled, and deletes it.
1350 void CancelRequest(Request* req) {
1351 DCHECK_EQ(key_.hostname, req->info().hostname());
1352 DCHECK(!req->was_canceled());
1354 // Don't remove it from |requests_| just mark it canceled.
1355 req->MarkAsCanceled();
1356 LogCancelRequest(req->source_net_log(), req->info());
1358 priority_tracker_.Remove(req->priority());
1359 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH,
1360 base::Bind(&NetLogJobAttachCallback,
1361 req->source_net_log().source(),
1362 priority()));
1364 if (num_active_requests() > 0) {
1365 UpdatePriority();
1366 } else {
1367 // If we were called from a Request's callback within CompleteRequests,
1368 // that Request could not have been cancelled, so num_active_requests()
1369 // could not be 0. Therefore, we are not in CompleteRequests().
1370 CompleteRequestsWithError(OK /* cancelled */);
1374 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1375 // the job. This currently assumes the abort is due to a network change.
1376 void Abort() {
1377 DCHECK(is_running());
1378 CompleteRequestsWithError(ERR_NETWORK_CHANGED);
1381 // If DnsTask present, abort it and fall back to ProcTask.
1382 void AbortDnsTask() {
1383 if (dns_task_) {
1384 KillDnsTask();
1385 dns_task_error_ = OK;
1386 StartProcTask();
1390 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1391 // Completes all requests and destroys the job.
1392 void OnEvicted() {
1393 DCHECK(!is_running());
1394 DCHECK(is_queued());
1395 handle_.Reset();
1397 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED);
1399 // This signals to CompleteRequests that this job never ran.
1400 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
1403 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1404 // this Job was destroyed.
1405 bool ServeFromHosts() {
1406 DCHECK_GT(num_active_requests(), 0u);
1407 AddressList addr_list;
1408 if (resolver_->ServeFromHosts(key(),
1409 requests_.front()->info(),
1410 &addr_list)) {
1411 // This will destroy the Job.
1412 CompleteRequests(
1413 HostCache::Entry(OK, MakeAddressListForRequest(addr_list)),
1414 base::TimeDelta());
1415 return true;
1417 return false;
1420 const Key key() const {
1421 return key_;
1424 bool is_queued() const {
1425 return !handle_.is_null();
1428 bool is_running() const {
1429 return is_dns_running() || is_proc_running();
1432 private:
1433 void KillDnsTask() {
1434 if (dns_task_) {
1435 ReduceToOneJobSlot();
1436 dns_task_.reset();
1440 // Reduce the number of job slots occupied and queued in the dispatcher
1441 // to one. If the second Job slot is queued in the dispatcher, cancels the
1442 // queued job. Otherwise, the second Job has been started by the
1443 // PrioritizedDispatcher, so signals it is complete.
1444 void ReduceToOneJobSlot() {
1445 DCHECK_GE(num_occupied_job_slots_, 1u);
1446 if (is_queued()) {
1447 resolver_->dispatcher_->Cancel(handle_);
1448 handle_.Reset();
1449 } else if (num_occupied_job_slots_ > 1) {
1450 resolver_->dispatcher_->OnJobFinished();
1451 --num_occupied_job_slots_;
1453 DCHECK_EQ(1u, num_occupied_job_slots_);
1456 void UpdatePriority() {
1457 if (is_queued()) {
1458 if (priority() != static_cast<RequestPriority>(handle_.priority()))
1459 priority_change_time_ = base::TimeTicks::Now();
1460 handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
1464 AddressList MakeAddressListForRequest(const AddressList& list) const {
1465 if (requests_.empty())
1466 return list;
1467 return AddressList::CopyWithPort(list, requests_.front()->info().port());
1470 // PriorityDispatch::Job:
1471 void Start() override {
1472 DCHECK_LE(num_occupied_job_slots_, 1u);
1474 handle_.Reset();
1475 ++num_occupied_job_slots_;
1477 if (num_occupied_job_slots_ == 2) {
1478 StartSecondDnsTransaction();
1479 return;
1482 DCHECK(!is_running());
1484 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED);
1486 had_dns_config_ = resolver_->HaveDnsConfig();
1488 base::TimeTicks now = base::TimeTicks::Now();
1489 base::TimeDelta queue_time = now - creation_time_;
1490 base::TimeDelta queue_time_after_change = now - priority_change_time_;
1492 if (had_dns_config_) {
1493 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(),
1494 queue_time);
1495 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(),
1496 queue_time_after_change);
1497 } else {
1498 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time);
1499 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(),
1500 queue_time_after_change);
1503 bool system_only =
1504 (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0;
1506 // Caution: Job::Start must not complete synchronously.
1507 if (!system_only && had_dns_config_ &&
1508 !ResemblesMulticastDNSName(key_.hostname)) {
1509 StartDnsTask();
1510 } else {
1511 StartProcTask();
1515 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
1516 // the limits on |dispatcher_|. But in order to keep the number of WorkerPool
1517 // threads low, we will need to use an "inner" PrioritizedDispatcher with
1518 // tighter limits.
1519 void StartProcTask() {
1520 DCHECK(!is_dns_running());
1521 proc_task_ = new ProcTask(
1522 key_,
1523 resolver_->proc_params_,
1524 base::Bind(&Job::OnProcTaskComplete, base::Unretained(this),
1525 base::TimeTicks::Now()),
1526 net_log_);
1528 if (had_non_speculative_request_)
1529 proc_task_->set_had_non_speculative_request();
1530 // Start() could be called from within Resolve(), hence it must NOT directly
1531 // call OnProcTaskComplete, for example, on synchronous failure.
1532 proc_task_->Start();
1535 // Called by ProcTask when it completes.
1536 void OnProcTaskComplete(base::TimeTicks start_time,
1537 int net_error,
1538 const AddressList& addr_list) {
1539 DCHECK(is_proc_running());
1541 if (!resolver_->resolved_known_ipv6_hostname_ &&
1542 net_error == OK &&
1543 key_.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
1544 if (key_.hostname == "www.google.com") {
1545 resolver_->resolved_known_ipv6_hostname_ = true;
1546 bool got_ipv6_address = false;
1547 for (size_t i = 0; i < addr_list.size(); ++i) {
1548 if (addr_list[i].GetFamily() == ADDRESS_FAMILY_IPV6) {
1549 got_ipv6_address = true;
1550 break;
1553 UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address);
1557 if (dns_task_error_ != OK) {
1558 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
1559 if (net_error == OK) {
1560 DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration);
1561 if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) &&
1562 ResemblesNetBIOSName(key_.hostname)) {
1563 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS);
1564 } else {
1565 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS);
1567 UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.ResolveError",
1568 std::abs(dns_task_error_));
1569 resolver_->OnDnsTaskResolve(dns_task_error_);
1570 } else {
1571 DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration);
1572 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
1576 base::TimeDelta ttl =
1577 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds);
1578 if (net_error == OK)
1579 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds);
1581 // Don't store the |ttl| in cache since it's not obtained from the server.
1582 CompleteRequests(
1583 HostCache::Entry(net_error, MakeAddressListForRequest(addr_list)),
1584 ttl);
1587 void StartDnsTask() {
1588 DCHECK(resolver_->HaveDnsConfig());
1589 dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this,
1590 net_log_));
1592 dns_task_->StartFirstTransaction();
1593 // Schedule a second transaction, if needed.
1594 if (dns_task_->needs_two_transactions())
1595 Schedule(true);
1598 void StartSecondDnsTransaction() {
1599 DCHECK(dns_task_->needs_two_transactions());
1600 dns_task_->StartSecondTransaction();
1603 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
1604 // deleted before this callback. In this case dns_task is deleted as well,
1605 // so we use it as indicator whether Job is still valid.
1606 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
1607 base::TimeDelta duration,
1608 int net_error) {
1609 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration);
1611 if (dns_task == NULL)
1612 return;
1614 dns_task_error_ = net_error;
1616 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so.
1617 // http://crbug.com/117655
1619 // TODO(szym): Some net errors indicate lack of connectivity. Starting
1620 // ProcTask in that case is a waste of time.
1621 if (resolver_->fallback_to_proctask_) {
1622 KillDnsTask();
1623 StartProcTask();
1624 } else {
1625 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
1626 CompleteRequestsWithError(net_error);
1631 // HostResolverImpl::DnsTask::Delegate implementation:
1633 void OnDnsTaskComplete(base::TimeTicks start_time,
1634 int net_error,
1635 const AddressList& addr_list,
1636 base::TimeDelta ttl) override {
1637 DCHECK(is_dns_running());
1639 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
1640 if (net_error != OK) {
1641 OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error);
1642 return;
1644 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration);
1645 // Log DNS lookups based on |address_family|.
1646 switch(key_.address_family) {
1647 case ADDRESS_FAMILY_IPV4:
1648 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration);
1649 break;
1650 case ADDRESS_FAMILY_IPV6:
1651 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration);
1652 break;
1653 case ADDRESS_FAMILY_UNSPECIFIED:
1654 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration);
1655 break;
1658 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS);
1659 RecordTTL(ttl);
1661 resolver_->OnDnsTaskResolve(OK);
1663 base::TimeDelta bounded_ttl =
1664 std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds));
1666 CompleteRequests(
1667 HostCache::Entry(net_error, MakeAddressListForRequest(addr_list), ttl),
1668 bounded_ttl);
1671 void OnFirstDnsTransactionComplete() override {
1672 DCHECK(dns_task_->needs_two_transactions());
1673 DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued());
1674 // No longer need to occupy two dispatcher slots.
1675 ReduceToOneJobSlot();
1677 // We already have a job slot at the dispatcher, so if the second
1678 // transaction hasn't started, reuse it now instead of waiting in the queue
1679 // for the second slot.
1680 if (dns_task_->needs_another_transaction())
1681 dns_task_->StartSecondTransaction();
1684 // Performs Job's last rites. Completes all Requests. Deletes this.
1685 void CompleteRequests(const HostCache::Entry& entry,
1686 base::TimeDelta ttl) {
1687 CHECK(resolver_.get());
1689 // This job must be removed from resolver's |jobs_| now to make room for a
1690 // new job with the same key in case one of the OnComplete callbacks decides
1691 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1692 // is done.
1693 scoped_ptr<Job> self_deleter(this);
1695 resolver_->RemoveJob(this);
1697 if (is_running()) {
1698 if (is_proc_running()) {
1699 DCHECK(!is_queued());
1700 proc_task_->Cancel();
1701 proc_task_ = NULL;
1703 KillDnsTask();
1705 // Signal dispatcher that a slot has opened.
1706 resolver_->dispatcher_->OnJobFinished();
1707 } else if (is_queued()) {
1708 resolver_->dispatcher_->Cancel(handle_);
1709 handle_.Reset();
1712 if (num_active_requests() == 0) {
1713 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
1714 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1715 OK);
1716 return;
1719 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1720 entry.error);
1722 DCHECK(!requests_.empty());
1724 if (entry.error == OK) {
1725 // Record this histogram here, when we know the system has a valid DNS
1726 // configuration.
1727 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1728 resolver_->received_dns_config_);
1731 bool did_complete = (entry.error != ERR_NETWORK_CHANGED) &&
1732 (entry.error != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
1733 if (did_complete)
1734 resolver_->CacheResult(key_, entry, ttl);
1736 // Complete all of the requests that were attached to the job.
1737 for (RequestsList::const_iterator it = requests_.begin();
1738 it != requests_.end(); ++it) {
1739 Request* req = *it;
1741 if (req->was_canceled())
1742 continue;
1744 DCHECK_EQ(this, req->job());
1745 // Update the net log and notify registered observers.
1746 LogFinishRequest(req->source_net_log(), req->info(), entry.error);
1747 if (did_complete) {
1748 // Record effective total time from creation to completion.
1749 RecordTotalTime(had_dns_config_, req->info().is_speculative(),
1750 base::TimeTicks::Now() - req->request_time());
1752 req->OnComplete(entry.error, entry.addrlist);
1754 // Check if the resolver was destroyed as a result of running the
1755 // callback. If it was, we could continue, but we choose to bail.
1756 if (!resolver_.get())
1757 return;
1761 // Convenience wrapper for CompleteRequests in case of failure.
1762 void CompleteRequestsWithError(int net_error) {
1763 CompleteRequests(HostCache::Entry(net_error, AddressList()),
1764 base::TimeDelta());
1767 RequestPriority priority() const {
1768 return priority_tracker_.highest_priority();
1771 // Number of non-canceled requests in |requests_|.
1772 size_t num_active_requests() const {
1773 return priority_tracker_.total_count();
1776 bool is_dns_running() const {
1777 return dns_task_.get() != NULL;
1780 bool is_proc_running() const {
1781 return proc_task_.get() != NULL;
1784 base::WeakPtr<HostResolverImpl> resolver_;
1786 Key key_;
1788 // Tracks the highest priority across |requests_|.
1789 PriorityTracker priority_tracker_;
1791 bool had_non_speculative_request_;
1793 // Distinguishes measurements taken while DnsClient was fully configured.
1794 bool had_dns_config_;
1796 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1797 unsigned num_occupied_job_slots_;
1799 // Result of DnsTask.
1800 int dns_task_error_;
1802 const base::TimeTicks creation_time_;
1803 base::TimeTicks priority_change_time_;
1805 BoundNetLog net_log_;
1807 // Resolves the host using a HostResolverProc.
1808 scoped_refptr<ProcTask> proc_task_;
1810 // Resolves the host using a DnsTransaction.
1811 scoped_ptr<DnsTask> dns_task_;
1813 // All Requests waiting for the result of this Job. Some can be canceled.
1814 RequestsList requests_;
1816 // A handle used in |HostResolverImpl::dispatcher_|.
1817 PrioritizedDispatcher::Handle handle_;
1820 //-----------------------------------------------------------------------------
1822 HostResolverImpl::ProcTaskParams::ProcTaskParams(
1823 HostResolverProc* resolver_proc,
1824 size_t max_retry_attempts)
1825 : resolver_proc(resolver_proc),
1826 max_retry_attempts(max_retry_attempts),
1827 unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)),
1828 retry_factor(2) {
1829 // Maximum of 4 retry attempts for host resolution.
1830 static const size_t kDefaultMaxRetryAttempts = 4u;
1831 if (max_retry_attempts == HostResolver::kDefaultRetryAttempts)
1832 max_retry_attempts = kDefaultMaxRetryAttempts;
1835 HostResolverImpl::ProcTaskParams::~ProcTaskParams() {}
1837 HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log)
1838 : max_queued_jobs_(0),
1839 proc_params_(NULL, options.max_retry_attempts),
1840 net_log_(net_log),
1841 received_dns_config_(false),
1842 num_dns_failures_(0),
1843 use_local_ipv6_(false),
1844 last_ipv6_probe_result_(true),
1845 resolved_known_ipv6_hostname_(false),
1846 additional_resolver_flags_(0),
1847 fallback_to_proctask_(true),
1848 weak_ptr_factory_(this),
1849 probe_weak_ptr_factory_(this) {
1850 if (options.enable_caching)
1851 cache_ = HostCache::CreateDefaultCache();
1853 PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits();
1854 dispatcher_.reset(new PrioritizedDispatcher(job_limits));
1855 max_queued_jobs_ = job_limits.total_jobs * 100u;
1857 DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
1859 #if defined(OS_WIN)
1860 EnsureWinsockInit();
1861 #endif
1862 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
1863 new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr());
1864 #endif
1865 NetworkChangeNotifier::AddIPAddressObserver(this);
1866 NetworkChangeNotifier::AddDNSObserver(this);
1867 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
1868 !defined(OS_ANDROID)
1869 EnsureDnsReloaderInit();
1870 #endif
1873 DnsConfig dns_config;
1874 NetworkChangeNotifier::GetDnsConfig(&dns_config);
1875 received_dns_config_ = dns_config.IsValid();
1876 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
1877 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
1880 fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
1883 HostResolverImpl::~HostResolverImpl() {
1884 // Prevent the dispatcher from starting new jobs.
1885 dispatcher_->SetLimitsToZero();
1886 // It's now safe for Jobs to call KillDsnTask on destruction, because
1887 // OnJobComplete will not start any new jobs.
1888 STLDeleteValues(&jobs_);
1890 NetworkChangeNotifier::RemoveIPAddressObserver(this);
1891 NetworkChangeNotifier::RemoveDNSObserver(this);
1894 void HostResolverImpl::SetMaxQueuedJobs(size_t value) {
1895 DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
1896 DCHECK_GT(value, 0u);
1897 max_queued_jobs_ = value;
1900 int HostResolverImpl::Resolve(const RequestInfo& info,
1901 RequestPriority priority,
1902 AddressList* addresses,
1903 const CompletionCallback& callback,
1904 RequestHandle* out_req,
1905 const BoundNetLog& source_net_log) {
1906 DCHECK(addresses);
1907 DCHECK(CalledOnValidThread());
1908 DCHECK_EQ(false, callback.is_null());
1910 // Check that the caller supplied a valid hostname to resolve.
1911 std::string labeled_hostname;
1912 if (!DNSDomainFromDot(info.hostname(), &labeled_hostname))
1913 return ERR_NAME_NOT_RESOLVED;
1915 LogStartRequest(source_net_log, info);
1917 IPAddressNumber ip_number;
1918 IPAddressNumber* ip_number_ptr = nullptr;
1919 if (ParseIPLiteralToNumber(info.hostname(), &ip_number))
1920 ip_number_ptr = &ip_number;
1922 // Build a key that identifies the request in the cache and in the
1923 // outstanding jobs map.
1924 Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log);
1926 int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log);
1927 if (rv != ERR_DNS_CACHE_MISS) {
1928 LogFinishRequest(source_net_log, info, rv);
1929 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta());
1930 return rv;
1933 // Next we need to attach our request to a "job". This job is responsible for
1934 // calling "getaddrinfo(hostname)" on a worker thread.
1936 JobMap::iterator jobit = jobs_.find(key);
1937 Job* job;
1938 if (jobit == jobs_.end()) {
1939 job =
1940 new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, source_net_log);
1941 job->Schedule(false);
1943 // Check for queue overflow.
1944 if (dispatcher_->num_queued_jobs() > max_queued_jobs_) {
1945 Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest());
1946 DCHECK(evicted);
1947 evicted->OnEvicted(); // Deletes |evicted|.
1948 if (evicted == job) {
1949 rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE;
1950 LogFinishRequest(source_net_log, info, rv);
1951 return rv;
1954 jobs_.insert(jobit, std::make_pair(key, job));
1955 } else {
1956 job = jobit->second;
1959 // Can't complete synchronously. Create and attach request.
1960 scoped_ptr<Request> req(new Request(
1961 source_net_log, info, priority, callback, addresses));
1962 if (out_req)
1963 *out_req = reinterpret_cast<RequestHandle>(req.get());
1965 job->AddRequest(req.Pass());
1966 // Completion happens during Job::CompleteRequests().
1967 return ERR_IO_PENDING;
1970 int HostResolverImpl::ResolveHelper(const Key& key,
1971 const RequestInfo& info,
1972 const IPAddressNumber* ip_number,
1973 AddressList* addresses,
1974 const BoundNetLog& source_net_log) {
1975 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
1976 // On Windows it gives the default interface's address, whereas on Linux it
1977 // gives an error. We will make it fail on all platforms for consistency.
1978 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength)
1979 return ERR_NAME_NOT_RESOLVED;
1981 int net_error = ERR_UNEXPECTED;
1982 if (ResolveAsIP(key, info, ip_number, &net_error, addresses))
1983 return net_error;
1984 if (ServeFromCache(key, info, &net_error, addresses)) {
1985 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT);
1986 return net_error;
1988 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
1989 // http://crbug.com/117655
1990 if (ServeFromHosts(key, info, addresses)) {
1991 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT);
1992 return OK;
1995 if (ServeLocalhost(key, info, addresses))
1996 return OK;
1998 return ERR_DNS_CACHE_MISS;
2001 int HostResolverImpl::ResolveFromCache(const RequestInfo& info,
2002 AddressList* addresses,
2003 const BoundNetLog& source_net_log) {
2004 DCHECK(CalledOnValidThread());
2005 DCHECK(addresses);
2007 // Update the net log and notify registered observers.
2008 LogStartRequest(source_net_log, info);
2010 IPAddressNumber ip_number;
2011 IPAddressNumber* ip_number_ptr = nullptr;
2012 if (ParseIPLiteralToNumber(info.hostname(), &ip_number))
2013 ip_number_ptr = &ip_number;
2015 Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log);
2017 int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log);
2018 LogFinishRequest(source_net_log, info, rv);
2019 return rv;
2022 void HostResolverImpl::CancelRequest(RequestHandle req_handle) {
2023 DCHECK(CalledOnValidThread());
2024 Request* req = reinterpret_cast<Request*>(req_handle);
2025 DCHECK(req);
2026 Job* job = req->job();
2027 DCHECK(job);
2028 job->CancelRequest(req);
2031 void HostResolverImpl::SetDnsClientEnabled(bool enabled) {
2032 DCHECK(CalledOnValidThread());
2033 #if defined(ENABLE_BUILT_IN_DNS)
2034 if (enabled && !dns_client_) {
2035 SetDnsClient(DnsClient::CreateClient(net_log_));
2036 } else if (!enabled && dns_client_) {
2037 SetDnsClient(scoped_ptr<DnsClient>());
2039 #endif
2042 HostCache* HostResolverImpl::GetHostCache() {
2043 return cache_.get();
2046 base::Value* HostResolverImpl::GetDnsConfigAsValue() const {
2047 // Check if async DNS is disabled.
2048 if (!dns_client_.get())
2049 return NULL;
2051 // Check if async DNS is enabled, but we currently have no configuration
2052 // for it.
2053 const DnsConfig* dns_config = dns_client_->GetConfig();
2054 if (dns_config == NULL)
2055 return new base::DictionaryValue();
2057 return dns_config->ToValue();
2060 bool HostResolverImpl::ResolveAsIP(const Key& key,
2061 const RequestInfo& info,
2062 const IPAddressNumber* ip_number,
2063 int* net_error,
2064 AddressList* addresses) {
2065 DCHECK(addresses);
2066 DCHECK(net_error);
2067 if (ip_number == nullptr)
2068 return false;
2070 DCHECK_EQ(key.host_resolver_flags &
2071 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY |
2072 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6),
2073 0) << " Unhandled flag";
2075 *net_error = OK;
2076 AddressFamily family = GetAddressFamily(*ip_number);
2077 if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED &&
2078 key.address_family != family) {
2079 // Don't return IPv6 addresses for IPv4 queries, and vice versa.
2080 *net_error = ERR_NAME_NOT_RESOLVED;
2081 } else {
2082 *addresses = AddressList::CreateFromIPAddress(*ip_number, info.port());
2083 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)
2084 addresses->SetDefaultCanonicalName();
2086 return true;
2089 bool HostResolverImpl::ServeFromCache(const Key& key,
2090 const RequestInfo& info,
2091 int* net_error,
2092 AddressList* addresses) {
2093 DCHECK(addresses);
2094 DCHECK(net_error);
2095 if (!info.allow_cached_response() || !cache_.get())
2096 return false;
2098 const HostCache::Entry* cache_entry = cache_->Lookup(
2099 key, base::TimeTicks::Now());
2100 if (!cache_entry)
2101 return false;
2103 *net_error = cache_entry->error;
2104 if (*net_error == OK) {
2105 if (cache_entry->has_ttl())
2106 RecordTTL(cache_entry->ttl);
2107 *addresses = EnsurePortOnAddressList(cache_entry->addrlist, info.port());
2109 return true;
2112 bool HostResolverImpl::ServeFromHosts(const Key& key,
2113 const RequestInfo& info,
2114 AddressList* addresses) {
2115 DCHECK(addresses);
2116 if (!HaveDnsConfig())
2117 return false;
2118 addresses->clear();
2120 // HOSTS lookups are case-insensitive.
2121 std::string hostname = base::StringToLowerASCII(key.hostname);
2123 const DnsHosts& hosts = dns_client_->GetConfig()->hosts;
2125 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2126 // (glibc and c-ares) return the first matching line. We have more
2127 // flexibility, but lose implicit ordering.
2128 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2129 // necessary.
2130 if (key.address_family == ADDRESS_FAMILY_IPV6 ||
2131 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2132 DnsHosts::const_iterator it = hosts.find(
2133 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6));
2134 if (it != hosts.end())
2135 addresses->push_back(IPEndPoint(it->second, info.port()));
2138 if (key.address_family == ADDRESS_FAMILY_IPV4 ||
2139 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2140 DnsHosts::const_iterator it = hosts.find(
2141 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4));
2142 if (it != hosts.end())
2143 addresses->push_back(IPEndPoint(it->second, info.port()));
2146 // If got only loopback addresses and the family was restricted, resolve
2147 // again, without restrictions. See SystemHostResolverCall for rationale.
2148 if ((key.host_resolver_flags &
2149 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
2150 IsAllIPv4Loopback(*addresses)) {
2151 Key new_key(key);
2152 new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED;
2153 new_key.host_resolver_flags &=
2154 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2155 return ServeFromHosts(new_key, info, addresses);
2157 return !addresses->empty();
2160 bool HostResolverImpl::ServeLocalhost(const Key& key,
2161 const RequestInfo& info,
2162 AddressList* addresses) {
2163 AddressList resolved_addresses;
2164 if (!ResolveLocalHostname(key.hostname, info.port(), &resolved_addresses))
2165 return false;
2167 addresses->clear();
2169 for (const auto& address : resolved_addresses) {
2170 // Include the address if:
2171 // - caller didn't specify an address family, or
2172 // - caller specifically asked for the address family of this address, or
2173 // - this is an IPv6 address and caller specifically asked for IPv4 due
2174 // to lack of detected IPv6 support. (See SystemHostResolverCall for
2175 // rationale).
2176 if (key.address_family == ADDRESS_FAMILY_UNSPECIFIED ||
2177 key.address_family == address.GetFamily() ||
2178 (address.GetFamily() == ADDRESS_FAMILY_IPV6 &&
2179 key.address_family == ADDRESS_FAMILY_IPV4 &&
2180 (key.host_resolver_flags &
2181 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6))) {
2182 addresses->push_back(address);
2186 return true;
2189 void HostResolverImpl::CacheResult(const Key& key,
2190 const HostCache::Entry& entry,
2191 base::TimeDelta ttl) {
2192 if (cache_.get())
2193 cache_->Set(key, entry, base::TimeTicks::Now(), ttl);
2196 void HostResolverImpl::RemoveJob(Job* job) {
2197 DCHECK(job);
2198 JobMap::iterator it = jobs_.find(job->key());
2199 if (it != jobs_.end() && it->second == job)
2200 jobs_.erase(it);
2203 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) {
2204 if (result) {
2205 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
2206 } else {
2207 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
2211 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest(
2212 const RequestInfo& info,
2213 const IPAddressNumber* ip_number,
2214 const BoundNetLog& net_log) {
2215 HostResolverFlags effective_flags =
2216 info.host_resolver_flags() | additional_resolver_flags_;
2217 AddressFamily effective_address_family = info.address_family();
2219 if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) {
2220 if (!use_local_ipv6_ &&
2221 // When resolving IPv4 literals, there's no need to probe for IPv6.
2222 // When resolving IPv6 literals, there's no benefit to artificially
2223 // limiting our resolution based on a probe. Prior logic ensures
2224 // that this query is UNSPECIFIED (see info.address_family()
2225 // check above) so the code requesting the resolution should be amenable
2226 // to receiving a IPv6 resolution.
2227 ip_number == nullptr) {
2228 if (!IsIPv6Reachable(net_log)) {
2229 effective_address_family = ADDRESS_FAMILY_IPV4;
2230 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2235 return Key(info.hostname(), effective_address_family, effective_flags);
2238 bool HostResolverImpl::IsIPv6Reachable(const BoundNetLog& net_log) {
2239 base::TimeTicks now = base::TimeTicks::Now();
2240 bool cached = true;
2241 if ((now - last_ipv6_probe_time_).InMilliseconds() > kIPv6ProbePeriodMs) {
2242 IPAddressNumber address(kIPv6ProbeAddress,
2243 kIPv6ProbeAddress + arraysize(kIPv6ProbeAddress));
2244 last_ipv6_probe_result_ = IsGloballyReachable(address, net_log);
2245 last_ipv6_probe_time_ = now;
2246 cached = false;
2248 net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK,
2249 base::Bind(&NetLogIPv6AvailableCallback,
2250 last_ipv6_probe_result_, cached));
2251 return last_ipv6_probe_result_;
2254 void HostResolverImpl::AbortAllInProgressJobs() {
2255 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2256 // first collect and remove all running jobs from |jobs_|.
2257 ScopedVector<Job> jobs_to_abort;
2258 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) {
2259 Job* job = it->second;
2260 if (job->is_running()) {
2261 jobs_to_abort.push_back(job);
2262 jobs_.erase(it++);
2263 } else {
2264 DCHECK(job->is_queued());
2265 ++it;
2269 // Pause the dispatcher so it won't start any new dispatcher jobs while
2270 // aborting the old ones. This is needed so that it won't start the second
2271 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2272 // invalid.
2273 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2274 dispatcher_->SetLimits(
2275 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2277 // Life check to bail once |this| is deleted.
2278 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
2280 // Then Abort them.
2281 for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
2282 jobs_to_abort[i]->Abort();
2283 jobs_to_abort[i] = NULL;
2286 if (self)
2287 dispatcher_->SetLimits(limits);
2290 void HostResolverImpl::AbortDnsTasks() {
2291 // Pause the dispatcher so it won't start any new dispatcher jobs while
2292 // aborting the old ones. This is needed so that it won't start the second
2293 // DnsTransaction for a job if the DnsConfig just changed.
2294 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2295 dispatcher_->SetLimits(
2296 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2298 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
2299 it->second->AbortDnsTask();
2300 dispatcher_->SetLimits(limits);
2303 void HostResolverImpl::TryServingAllJobsFromHosts() {
2304 if (!HaveDnsConfig())
2305 return;
2307 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2308 // http://crbug.com/117655
2310 // Life check to bail once |this| is deleted.
2311 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
2313 for (JobMap::iterator it = jobs_.begin(); self.get() && it != jobs_.end();) {
2314 Job* job = it->second;
2315 ++it;
2316 // This could remove |job| from |jobs_|, but iterator will remain valid.
2317 job->ServeFromHosts();
2321 void HostResolverImpl::OnIPAddressChanged() {
2322 resolved_known_ipv6_hostname_ = false;
2323 last_ipv6_probe_time_ = base::TimeTicks();
2324 // Abandon all ProbeJobs.
2325 probe_weak_ptr_factory_.InvalidateWeakPtrs();
2326 if (cache_.get())
2327 cache_->clear();
2328 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
2329 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr());
2330 #endif
2331 AbortAllInProgressJobs();
2332 // |this| may be deleted inside AbortAllInProgressJobs().
2335 void HostResolverImpl::OnInitialDNSConfigRead() {
2336 UpdateDNSConfig(false);
2339 void HostResolverImpl::OnDNSChanged() {
2340 UpdateDNSConfig(true);
2343 void HostResolverImpl::UpdateDNSConfig(bool config_changed) {
2344 DnsConfig dns_config;
2345 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2347 if (net_log_) {
2348 net_log_->AddGlobalEntry(
2349 NetLog::TYPE_DNS_CONFIG_CHANGED,
2350 base::Bind(&NetLogDnsConfigCallback, &dns_config));
2353 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
2354 received_dns_config_ = dns_config.IsValid();
2355 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2356 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
2358 num_dns_failures_ = 0;
2360 // We want a new DnsSession in place, before we Abort running Jobs, so that
2361 // the newly started jobs use the new config.
2362 if (dns_client_.get()) {
2363 dns_client_->SetConfig(dns_config);
2364 if (dns_client_->GetConfig()) {
2365 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2366 // If we just switched DnsClients, restart jobs using new resolver.
2367 // TODO(pauljensen): Is this necessary?
2368 config_changed = true;
2372 if (config_changed) {
2373 // If the DNS server has changed, existing cached info could be wrong so we
2374 // have to drop our internal cache :( Note that OS level DNS caches, such
2375 // as NSCD's cache should be dropped automatically by the OS when
2376 // resolv.conf changes so we don't need to do anything to clear that cache.
2377 if (cache_.get())
2378 cache_->clear();
2380 // Life check to bail once |this| is deleted.
2381 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
2383 // Existing jobs will have been sent to the original server so they need to
2384 // be aborted.
2385 AbortAllInProgressJobs();
2387 // |this| may be deleted inside AbortAllInProgressJobs().
2388 if (self.get())
2389 TryServingAllJobsFromHosts();
2393 bool HostResolverImpl::HaveDnsConfig() const {
2394 // Use DnsClient only if it's fully configured and there is no override by
2395 // ScopedDefaultHostResolverProc.
2396 // The alternative is to use NetworkChangeNotifier to override DnsConfig,
2397 // but that would introduce construction order requirements for NCN and SDHRP.
2398 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL) &&
2399 !(proc_params_.resolver_proc.get() == NULL &&
2400 HostResolverProc::GetDefault() != NULL);
2403 void HostResolverImpl::OnDnsTaskResolve(int net_error) {
2404 DCHECK(dns_client_);
2405 if (net_error == OK) {
2406 num_dns_failures_ = 0;
2407 return;
2409 ++num_dns_failures_;
2410 if (num_dns_failures_ < kMaximumDnsFailures)
2411 return;
2413 // Disable DnsClient until the next DNS change. Must be done before aborting
2414 // DnsTasks, since doing so may start new jobs.
2415 dns_client_->SetConfig(DnsConfig());
2417 // Switch jobs with active DnsTasks over to using ProcTasks.
2418 AbortDnsTasks();
2420 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
2421 UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.DnsClientDisabledReason",
2422 std::abs(net_error));
2425 void HostResolverImpl::SetDnsClient(scoped_ptr<DnsClient> dns_client) {
2426 // DnsClient and config must be updated before aborting DnsTasks, since doing
2427 // so may start new jobs.
2428 dns_client_ = dns_client.Pass();
2429 if (dns_client_ && !dns_client_->GetConfig() &&
2430 num_dns_failures_ < kMaximumDnsFailures) {
2431 DnsConfig dns_config;
2432 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2433 dns_client_->SetConfig(dns_config);
2434 num_dns_failures_ = 0;
2435 if (dns_client_->GetConfig())
2436 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2439 AbortDnsTasks();
2442 } // namespace net