Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / net / dns / host_resolver_impl.cc
blob9020476c64c94ffd87d24cc2a58d646f172d36f0
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/message_loop/message_loop_proxy.h"
25 #include "base/metrics/field_trial.h"
26 #include "base/metrics/histogram.h"
27 #include "base/profiler/scoped_tracker.h"
28 #include "base/stl_util.h"
29 #include "base/strings/string_util.h"
30 #include "base/strings/utf_string_conversions.h"
31 #include "base/threading/worker_pool.h"
32 #include "base/time/time.h"
33 #include "base/values.h"
34 #include "net/base/address_family.h"
35 #include "net/base/address_list.h"
36 #include "net/base/dns_reloader.h"
37 #include "net/base/dns_util.h"
38 #include "net/base/host_port_pair.h"
39 #include "net/base/ip_endpoint.h"
40 #include "net/base/net_errors.h"
41 #include "net/base/net_util.h"
42 #include "net/dns/address_sorter.h"
43 #include "net/dns/dns_client.h"
44 #include "net/dns/dns_config_service.h"
45 #include "net/dns/dns_protocol.h"
46 #include "net/dns/dns_response.h"
47 #include "net/dns/dns_transaction.h"
48 #include "net/dns/host_resolver_proc.h"
49 #include "net/log/net_log.h"
50 #include "net/socket/client_socket_factory.h"
51 #include "net/udp/datagram_client_socket.h"
52 #include "url/url_canon_ip.h"
54 #if defined(OS_WIN)
55 #include "net/base/winsock_init.h"
56 #endif
58 namespace net {
60 namespace {
62 // Limit the size of hostnames that will be resolved to combat issues in
63 // some platform's resolvers.
64 const size_t kMaxHostLength = 4096;
66 // Default TTL for successful resolutions with ProcTask.
67 const unsigned kCacheEntryTTLSeconds = 60;
69 // Default TTL for unsuccessful resolutions with ProcTask.
70 const unsigned kNegativeCacheEntryTTLSeconds = 0;
72 // Minimum TTL for successful resolutions with DnsTask.
73 const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
75 const char kLocalhost[] = "localhost.";
77 // We use a separate histogram name for each platform to facilitate the
78 // display of error codes by their symbolic name (since each platform has
79 // different mappings).
80 const char kOSErrorsForGetAddrinfoHistogramName[] =
81 #if defined(OS_WIN)
82 "Net.OSErrorsForGetAddrinfo_Win";
83 #elif defined(OS_MACOSX)
84 "Net.OSErrorsForGetAddrinfo_Mac";
85 #elif defined(OS_LINUX)
86 "Net.OSErrorsForGetAddrinfo_Linux";
87 #else
88 "Net.OSErrorsForGetAddrinfo";
89 #endif
91 // Gets a list of the likely error codes that getaddrinfo() can return
92 // (non-exhaustive). These are the error codes that we will track via
93 // a histogram.
94 std::vector<int> GetAllGetAddrinfoOSErrors() {
95 int os_errors[] = {
96 #if defined(OS_POSIX)
97 #if !defined(OS_FREEBSD)
98 #if !defined(OS_ANDROID)
99 // EAI_ADDRFAMILY has been declared obsolete in Android's and
100 // FreeBSD's netdb.h.
101 EAI_ADDRFAMILY,
102 #endif
103 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
104 EAI_NODATA,
105 #endif
106 EAI_AGAIN,
107 EAI_BADFLAGS,
108 EAI_FAIL,
109 EAI_FAMILY,
110 EAI_MEMORY,
111 EAI_NONAME,
112 EAI_SERVICE,
113 EAI_SOCKTYPE,
114 EAI_SYSTEM,
115 #elif defined(OS_WIN)
116 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
117 WSA_NOT_ENOUGH_MEMORY,
118 WSAEAFNOSUPPORT,
119 WSAEINVAL,
120 WSAESOCKTNOSUPPORT,
121 WSAHOST_NOT_FOUND,
122 WSANO_DATA,
123 WSANO_RECOVERY,
124 WSANOTINITIALISED,
125 WSATRY_AGAIN,
126 WSATYPE_NOT_FOUND,
127 // The following are not in doc, but might be to appearing in results :-(.
128 WSA_INVALID_HANDLE,
129 #endif
132 // Ensure all errors are positive, as histogram only tracks positive values.
133 for (size_t i = 0; i < arraysize(os_errors); ++i) {
134 os_errors[i] = std::abs(os_errors[i]);
137 return base::CustomHistogram::ArrayToCustomRanges(os_errors,
138 arraysize(os_errors));
141 enum DnsResolveStatus {
142 RESOLVE_STATUS_DNS_SUCCESS = 0,
143 RESOLVE_STATUS_PROC_SUCCESS,
144 RESOLVE_STATUS_FAIL,
145 RESOLVE_STATUS_SUSPECT_NETBIOS,
146 RESOLVE_STATUS_MAX
149 // ICANN uses this localhost address to indicate a name collision.
151 // The policy in Chromium is to fail host resolving if it resolves to
152 // this special address.
154 // Not however that IP literals are exempt from this policy, so it is still
155 // possible to navigate to http://127.0.53.53/ directly.
157 // For more details: https://www.icann.org/news/announcement-2-2014-08-01-en
158 const unsigned char kIcanNameCollisionIp[] = {127, 0, 53, 53};
160 void UmaAsyncDnsResolveStatus(DnsResolveStatus result) {
161 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
162 result,
163 RESOLVE_STATUS_MAX);
166 bool ResemblesNetBIOSName(const std::string& hostname) {
167 return (hostname.size() < 16) && (hostname.find('.') == std::string::npos);
170 // True if |hostname| ends with either ".local" or ".local.".
171 bool ResemblesMulticastDNSName(const std::string& hostname) {
172 DCHECK(!hostname.empty());
173 const char kSuffix[] = ".local.";
174 const size_t kSuffixLen = sizeof(kSuffix) - 1;
175 const size_t kSuffixLenTrimmed = kSuffixLen - 1;
176 if (hostname[hostname.size() - 1] == '.') {
177 return hostname.size() > kSuffixLen &&
178 !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix);
180 return hostname.size() > kSuffixLenTrimmed &&
181 !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed,
182 kSuffix, kSuffixLenTrimmed);
185 // Attempts to connect a UDP socket to |dest|:53.
186 bool IsGloballyReachable(const IPAddressNumber& dest,
187 const BoundNetLog& net_log) {
188 scoped_ptr<DatagramClientSocket> socket(
189 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
190 DatagramSocket::DEFAULT_BIND,
191 RandIntCallback(),
192 net_log.net_log(),
193 net_log.source()));
194 int rv = socket->Connect(IPEndPoint(dest, 53));
195 if (rv != OK)
196 return false;
197 IPEndPoint endpoint;
198 rv = socket->GetLocalAddress(&endpoint);
199 if (rv != OK)
200 return false;
201 DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily());
202 const IPAddressNumber& address = endpoint.address();
203 bool is_link_local = (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80);
204 if (is_link_local)
205 return false;
206 const uint8 kTeredoPrefix[] = { 0x20, 0x01, 0, 0 };
207 bool is_teredo = std::equal(kTeredoPrefix,
208 kTeredoPrefix + arraysize(kTeredoPrefix),
209 address.begin());
210 if (is_teredo)
211 return false;
212 return true;
215 // Provide a common macro to simplify code and readability. We must use a
216 // macro as the underlying HISTOGRAM macro creates static variables.
217 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \
218 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100)
220 // A macro to simplify code and readability.
221 #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
222 do { \
223 switch (priority) { \
224 case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \
225 case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \
226 case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \
227 case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \
228 case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \
229 default: NOTREACHED(); break; \
231 DNS_HISTOGRAM(basename, time); \
232 } while (0)
234 // Record time from Request creation until a valid DNS response.
235 void RecordTotalTime(bool had_dns_config,
236 bool speculative,
237 base::TimeDelta duration) {
238 if (had_dns_config) {
239 if (speculative) {
240 DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration);
241 } else {
242 DNS_HISTOGRAM("AsyncDNS.TotalTime", duration);
244 } else {
245 if (speculative) {
246 DNS_HISTOGRAM("DNS.TotalTime_speculative", duration);
247 } else {
248 DNS_HISTOGRAM("DNS.TotalTime", duration);
253 void RecordTTL(base::TimeDelta ttl) {
254 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl,
255 base::TimeDelta::FromSeconds(1),
256 base::TimeDelta::FromDays(1), 100);
259 bool ConfigureAsyncDnsNoFallbackFieldTrial() {
260 const bool kDefault = false;
262 // Configure the AsyncDns field trial as follows:
263 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
264 // groups AsyncDnsA and AsyncDnsB: return false,
265 // groups SystemDnsA and SystemDnsB: return false,
266 // otherwise (trial absent): return default.
267 std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
268 if (!group_name.empty())
269 return StartsWithASCII(group_name, "AsyncDnsNoFallback", false);
270 return kDefault;
273 //-----------------------------------------------------------------------------
275 AddressList EnsurePortOnAddressList(const AddressList& list, uint16 port) {
276 if (list.empty() || list.front().port() == port)
277 return list;
278 return AddressList::CopyWithPort(list, port);
281 // Returns true if |addresses| contains only IPv4 loopback addresses.
282 bool IsAllIPv4Loopback(const AddressList& addresses) {
283 for (unsigned i = 0; i < addresses.size(); ++i) {
284 const IPAddressNumber& address = addresses[i].address();
285 switch (addresses[i].GetFamily()) {
286 case ADDRESS_FAMILY_IPV4:
287 if (address[0] != 127)
288 return false;
289 break;
290 case ADDRESS_FAMILY_IPV6:
291 return false;
292 default:
293 NOTREACHED();
294 return false;
297 return true;
300 // Creates NetLog parameters when the resolve failed.
301 base::Value* NetLogProcTaskFailedCallback(uint32 attempt_number,
302 int net_error,
303 int os_error,
304 NetLog::LogLevel /* log_level */) {
305 base::DictionaryValue* dict = new base::DictionaryValue();
306 if (attempt_number)
307 dict->SetInteger("attempt_number", attempt_number);
309 dict->SetInteger("net_error", net_error);
311 if (os_error) {
312 dict->SetInteger("os_error", os_error);
313 #if defined(OS_POSIX)
314 dict->SetString("os_error_string", gai_strerror(os_error));
315 #elif defined(OS_WIN)
316 // Map the error code to a human-readable string.
317 LPWSTR error_string = NULL;
318 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
319 0, // Use the internal message table.
320 os_error,
321 0, // Use default language.
322 (LPWSTR)&error_string,
323 0, // Buffer size.
324 0); // Arguments (unused).
325 dict->SetString("os_error_string", base::WideToUTF8(error_string));
326 LocalFree(error_string);
327 #endif
330 return dict;
333 // Creates NetLog parameters when the DnsTask failed.
334 base::Value* NetLogDnsTaskFailedCallback(int net_error,
335 int dns_error,
336 NetLog::LogLevel /* log_level */) {
337 base::DictionaryValue* dict = new base::DictionaryValue();
338 dict->SetInteger("net_error", net_error);
339 if (dns_error)
340 dict->SetInteger("dns_error", dns_error);
341 return dict;
344 // Creates NetLog parameters containing the information in a RequestInfo object,
345 // along with the associated NetLog::Source.
346 base::Value* NetLogRequestInfoCallback(const HostResolver::RequestInfo* info,
347 NetLog::LogLevel /* log_level */) {
348 base::DictionaryValue* dict = new base::DictionaryValue();
350 dict->SetString("host", info->host_port_pair().ToString());
351 dict->SetInteger("address_family",
352 static_cast<int>(info->address_family()));
353 dict->SetBoolean("allow_cached_response", info->allow_cached_response());
354 dict->SetBoolean("is_speculative", info->is_speculative());
355 return dict;
358 // Creates NetLog parameters for the creation of a HostResolverImpl::Job.
359 base::Value* NetLogJobCreationCallback(const NetLog::Source& source,
360 const std::string* host,
361 NetLog::LogLevel /* log_level */) {
362 base::DictionaryValue* dict = new base::DictionaryValue();
363 source.AddToEventParameters(dict);
364 dict->SetString("host", *host);
365 return dict;
368 // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
369 base::Value* NetLogJobAttachCallback(const NetLog::Source& source,
370 RequestPriority priority,
371 NetLog::LogLevel /* log_level */) {
372 base::DictionaryValue* dict = new base::DictionaryValue();
373 source.AddToEventParameters(dict);
374 dict->SetString("priority", RequestPriorityToString(priority));
375 return dict;
378 // Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
379 base::Value* NetLogDnsConfigCallback(const DnsConfig* config,
380 NetLog::LogLevel /* log_level */) {
381 return config->ToValue();
384 // The logging routines are defined here because some requests are resolved
385 // without a Request object.
387 // Logs when a request has just been started.
388 void LogStartRequest(const BoundNetLog& source_net_log,
389 const HostResolver::RequestInfo& info) {
390 source_net_log.BeginEvent(
391 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST,
392 base::Bind(&NetLogRequestInfoCallback, &info));
395 // Logs when a request has just completed (before its callback is run).
396 void LogFinishRequest(const BoundNetLog& source_net_log,
397 const HostResolver::RequestInfo& info,
398 int net_error) {
399 source_net_log.EndEventWithNetErrorCode(
400 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, net_error);
403 // Logs when a request has been cancelled.
404 void LogCancelRequest(const BoundNetLog& source_net_log,
405 const HostResolverImpl::RequestInfo& info) {
406 source_net_log.AddEvent(NetLog::TYPE_CANCELLED);
407 source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST);
410 //-----------------------------------------------------------------------------
412 // Keeps track of the highest priority.
413 class PriorityTracker {
414 public:
415 explicit PriorityTracker(RequestPriority initial_priority)
416 : highest_priority_(initial_priority), total_count_(0) {
417 memset(counts_, 0, sizeof(counts_));
420 RequestPriority highest_priority() const {
421 return highest_priority_;
424 size_t total_count() const {
425 return total_count_;
428 void Add(RequestPriority req_priority) {
429 ++total_count_;
430 ++counts_[req_priority];
431 if (highest_priority_ < req_priority)
432 highest_priority_ = req_priority;
435 void Remove(RequestPriority req_priority) {
436 DCHECK_GT(total_count_, 0u);
437 DCHECK_GT(counts_[req_priority], 0u);
438 --total_count_;
439 --counts_[req_priority];
440 size_t i;
441 for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i);
442 highest_priority_ = static_cast<RequestPriority>(i);
444 // In absence of requests, default to MINIMUM_PRIORITY.
445 if (total_count_ == 0)
446 DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
449 private:
450 RequestPriority highest_priority_;
451 size_t total_count_;
452 size_t counts_[NUM_PRIORITIES];
455 } // namespace
457 //-----------------------------------------------------------------------------
459 const unsigned HostResolverImpl::kMaximumDnsFailures = 16;
461 // Holds the data for a request that could not be completed synchronously.
462 // It is owned by a Job. Canceled Requests are only marked as canceled rather
463 // than removed from the Job's |requests_| list.
464 class HostResolverImpl::Request {
465 public:
466 Request(const BoundNetLog& source_net_log,
467 const RequestInfo& info,
468 RequestPriority priority,
469 const CompletionCallback& callback,
470 AddressList* addresses)
471 : source_net_log_(source_net_log),
472 info_(info),
473 priority_(priority),
474 job_(NULL),
475 callback_(callback),
476 addresses_(addresses),
477 request_time_(base::TimeTicks::Now()) {}
479 // Mark the request as canceled.
480 void MarkAsCanceled() {
481 job_ = NULL;
482 addresses_ = NULL;
483 callback_.Reset();
486 bool was_canceled() const {
487 return callback_.is_null();
490 void set_job(Job* job) {
491 DCHECK(job);
492 // Identify which job the request is waiting on.
493 job_ = job;
496 // Prepare final AddressList and call completion callback.
497 void OnComplete(int error, const AddressList& addr_list) {
498 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
499 tracked_objects::ScopedTracker tracking_profile(
500 FROM_HERE_WITH_EXPLICIT_FUNCTION(
501 "436634 HostResolverImpl::Request::OnComplete"));
503 DCHECK(!was_canceled());
504 if (error == OK)
505 *addresses_ = EnsurePortOnAddressList(addr_list, info_.port());
506 CompletionCallback callback = callback_;
507 MarkAsCanceled();
508 callback.Run(error);
511 Job* job() const {
512 return job_;
515 // NetLog for the source, passed in HostResolver::Resolve.
516 const BoundNetLog& source_net_log() {
517 return source_net_log_;
520 const RequestInfo& info() const {
521 return info_;
524 RequestPriority priority() const { return priority_; }
526 base::TimeTicks request_time() const { return request_time_; }
528 private:
529 const BoundNetLog source_net_log_;
531 // The request info that started the request.
532 const RequestInfo info_;
534 // TODO(akalin): Support reprioritization.
535 const RequestPriority priority_;
537 // The resolve job that this request is dependent on.
538 Job* job_;
540 // The user's callback to invoke when the request completes.
541 CompletionCallback callback_;
543 // The address list to save result into.
544 AddressList* addresses_;
546 const base::TimeTicks request_time_;
548 DISALLOW_COPY_AND_ASSIGN(Request);
551 //------------------------------------------------------------------------------
553 // Calls HostResolverProc on the WorkerPool. Performs retries if necessary.
555 // Whenever we try to resolve the host, we post a delayed task to check if host
556 // resolution (OnLookupComplete) is completed or not. If the original attempt
557 // hasn't completed, then we start another attempt for host resolution. We take
558 // the results from the first attempt that finishes and ignore the results from
559 // all other attempts.
561 // TODO(szym): Move to separate source file for testing and mocking.
563 class HostResolverImpl::ProcTask
564 : public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> {
565 public:
566 typedef base::Callback<void(int net_error,
567 const AddressList& addr_list)> Callback;
569 ProcTask(const Key& key,
570 const ProcTaskParams& params,
571 const Callback& callback,
572 const BoundNetLog& job_net_log)
573 : key_(key),
574 params_(params),
575 callback_(callback),
576 origin_loop_(base::MessageLoopProxy::current()),
577 attempt_number_(0),
578 completed_attempt_number_(0),
579 completed_attempt_error_(ERR_UNEXPECTED),
580 had_non_speculative_request_(false),
581 net_log_(job_net_log) {
582 if (!params_.resolver_proc.get())
583 params_.resolver_proc = HostResolverProc::GetDefault();
584 // If default is unset, use the system proc.
585 if (!params_.resolver_proc.get())
586 params_.resolver_proc = new SystemHostResolverProc();
589 void Start() {
590 DCHECK(origin_loop_->BelongsToCurrentThread());
591 net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK);
592 StartLookupAttempt();
595 // Cancels this ProcTask. It will be orphaned. Any outstanding resolve
596 // attempts running on worker threads will continue running. Only once all the
597 // attempts complete will the final reference to this ProcTask be released.
598 void Cancel() {
599 DCHECK(origin_loop_->BelongsToCurrentThread());
601 if (was_canceled() || was_completed())
602 return;
604 callback_.Reset();
605 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK);
608 void set_had_non_speculative_request() {
609 DCHECK(origin_loop_->BelongsToCurrentThread());
610 had_non_speculative_request_ = true;
613 bool was_canceled() const {
614 DCHECK(origin_loop_->BelongsToCurrentThread());
615 return callback_.is_null();
618 bool was_completed() const {
619 DCHECK(origin_loop_->BelongsToCurrentThread());
620 return completed_attempt_number_ > 0;
623 private:
624 friend class base::RefCountedThreadSafe<ProcTask>;
625 ~ProcTask() {}
627 void StartLookupAttempt() {
628 DCHECK(origin_loop_->BelongsToCurrentThread());
629 base::TimeTicks start_time = base::TimeTicks::Now();
630 ++attempt_number_;
631 // Dispatch the lookup attempt to a worker thread.
632 if (!base::WorkerPool::PostTask(
633 FROM_HERE,
634 base::Bind(&ProcTask::DoLookup, this, start_time, attempt_number_),
635 true)) {
636 NOTREACHED();
638 // Since we could be running within Resolve() right now, we can't just
639 // call OnLookupComplete(). Instead we must wait until Resolve() has
640 // returned (IO_PENDING).
641 origin_loop_->PostTask(
642 FROM_HERE,
643 base::Bind(&ProcTask::OnLookupComplete, this, AddressList(),
644 start_time, attempt_number_, ERR_UNEXPECTED, 0));
645 return;
648 net_log_.AddEvent(
649 NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED,
650 NetLog::IntegerCallback("attempt_number", attempt_number_));
652 // If we don't get the results within a given time, RetryIfNotComplete
653 // will start a new attempt on a different worker thread if none of our
654 // outstanding attempts have completed yet.
655 if (attempt_number_ <= params_.max_retry_attempts) {
656 origin_loop_->PostDelayedTask(
657 FROM_HERE,
658 base::Bind(&ProcTask::RetryIfNotComplete, this),
659 params_.unresponsive_delay);
663 // WARNING: This code runs inside a worker pool. The shutdown code cannot
664 // wait for it to finish, so we must be very careful here about using other
665 // objects (like MessageLoops, Singletons, etc). During shutdown these objects
666 // may no longer exist. Multiple DoLookups() could be running in parallel, so
667 // any state inside of |this| must not mutate .
668 void DoLookup(const base::TimeTicks& start_time,
669 const uint32 attempt_number) {
670 AddressList results;
671 int os_error = 0;
672 // Running on the worker thread
673 int error = params_.resolver_proc->Resolve(key_.hostname,
674 key_.address_family,
675 key_.host_resolver_flags,
676 &results,
677 &os_error);
679 // Fail the resolution if the result contains 127.0.53.53. See the comment
680 // block of kIcanNameCollisionIp for details on why.
681 for (const auto& it : results) {
682 const IPAddressNumber& cur = it.address();
683 if (cur.size() == arraysize(kIcanNameCollisionIp) &&
684 0 == memcmp(&cur.front(), kIcanNameCollisionIp, cur.size())) {
685 error = ERR_ICANN_NAME_COLLISION;
686 break;
690 origin_loop_->PostTask(
691 FROM_HERE,
692 base::Bind(&ProcTask::OnLookupComplete, this, results, start_time,
693 attempt_number, error, os_error));
696 // Makes next attempt if DoLookup() has not finished (runs on origin thread).
697 void RetryIfNotComplete() {
698 DCHECK(origin_loop_->BelongsToCurrentThread());
700 if (was_completed() || was_canceled())
701 return;
703 params_.unresponsive_delay *= params_.retry_factor;
704 StartLookupAttempt();
707 // Callback for when DoLookup() completes (runs on origin thread).
708 void OnLookupComplete(const AddressList& results,
709 const base::TimeTicks& start_time,
710 const uint32 attempt_number,
711 int error,
712 const int os_error) {
713 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
714 tracked_objects::ScopedTracker tracking_profile1(
715 FROM_HERE_WITH_EXPLICIT_FUNCTION(
716 "436634 HostResolverImpl::ProcTask::OnLookupComplete1"));
718 DCHECK(origin_loop_->BelongsToCurrentThread());
719 // If results are empty, we should return an error.
720 bool empty_list_on_ok = (error == OK && results.empty());
721 UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok);
722 if (empty_list_on_ok)
723 error = ERR_NAME_NOT_RESOLVED;
725 bool was_retry_attempt = attempt_number > 1;
727 // Ideally the following code would be part of host_resolver_proc.cc,
728 // however it isn't safe to call NetworkChangeNotifier from worker threads.
729 // So we do it here on the IO thread instead.
730 if (error != OK && NetworkChangeNotifier::IsOffline())
731 error = ERR_INTERNET_DISCONNECTED;
733 // If this is the first attempt that is finishing later, then record data
734 // for the first attempt. Won't contaminate with retry attempt's data.
735 if (!was_retry_attempt)
736 RecordPerformanceHistograms(start_time, error, os_error);
738 RecordAttemptHistograms(start_time, attempt_number, error, os_error);
740 if (was_canceled())
741 return;
743 NetLog::ParametersCallback net_log_callback;
744 if (error != OK) {
745 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
746 attempt_number,
747 error,
748 os_error);
749 } else {
750 net_log_callback = NetLog::IntegerCallback("attempt_number",
751 attempt_number);
753 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED,
754 net_log_callback);
756 if (was_completed())
757 return;
759 // Copy the results from the first worker thread that resolves the host.
760 results_ = results;
761 completed_attempt_number_ = attempt_number;
762 completed_attempt_error_ = error;
764 if (was_retry_attempt) {
765 // If retry attempt finishes before 1st attempt, then get stats on how
766 // much time is saved by having spawned an extra attempt.
767 retry_attempt_finished_time_ = base::TimeTicks::Now();
770 if (error != OK) {
771 net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
772 0, error, os_error);
773 } else {
774 net_log_callback = results_.CreateNetLogCallback();
776 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK,
777 net_log_callback);
779 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
780 tracked_objects::ScopedTracker tracking_profile2(
781 FROM_HERE_WITH_EXPLICIT_FUNCTION(
782 "436634 HostResolverImpl::ProcTask::OnLookupComplete2"));
784 callback_.Run(error, results_);
787 void RecordPerformanceHistograms(const base::TimeTicks& start_time,
788 const int error,
789 const int os_error) const {
790 DCHECK(origin_loop_->BelongsToCurrentThread());
791 enum Category { // Used in UMA_HISTOGRAM_ENUMERATION.
792 RESOLVE_SUCCESS,
793 RESOLVE_FAIL,
794 RESOLVE_SPECULATIVE_SUCCESS,
795 RESOLVE_SPECULATIVE_FAIL,
796 RESOLVE_MAX, // Bounding value.
798 int category = RESOLVE_MAX; // Illegal value for later DCHECK only.
800 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
801 if (error == OK) {
802 if (had_non_speculative_request_) {
803 category = RESOLVE_SUCCESS;
804 DNS_HISTOGRAM("DNS.ResolveSuccess", duration);
805 } else {
806 category = RESOLVE_SPECULATIVE_SUCCESS;
807 DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration);
810 // Log DNS lookups based on |address_family|. This will help us determine
811 // if IPv4 or IPv4/6 lookups are faster or slower.
812 switch(key_.address_family) {
813 case ADDRESS_FAMILY_IPV4:
814 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration);
815 break;
816 case ADDRESS_FAMILY_IPV6:
817 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration);
818 break;
819 case ADDRESS_FAMILY_UNSPECIFIED:
820 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration);
821 break;
823 } else {
824 if (had_non_speculative_request_) {
825 category = RESOLVE_FAIL;
826 DNS_HISTOGRAM("DNS.ResolveFail", duration);
827 } else {
828 category = RESOLVE_SPECULATIVE_FAIL;
829 DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration);
831 // Log DNS lookups based on |address_family|. This will help us determine
832 // if IPv4 or IPv4/6 lookups are faster or slower.
833 switch(key_.address_family) {
834 case ADDRESS_FAMILY_IPV4:
835 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration);
836 break;
837 case ADDRESS_FAMILY_IPV6:
838 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration);
839 break;
840 case ADDRESS_FAMILY_UNSPECIFIED:
841 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration);
842 break;
844 UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName,
845 std::abs(os_error),
846 GetAllGetAddrinfoOSErrors());
848 DCHECK_LT(category, static_cast<int>(RESOLVE_MAX)); // Be sure it was set.
850 UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category, RESOLVE_MAX);
853 void RecordAttemptHistograms(const base::TimeTicks& start_time,
854 const uint32 attempt_number,
855 const int error,
856 const int os_error) const {
857 DCHECK(origin_loop_->BelongsToCurrentThread());
858 bool first_attempt_to_complete =
859 completed_attempt_number_ == attempt_number;
860 bool is_first_attempt = (attempt_number == 1);
862 if (first_attempt_to_complete) {
863 // If this was first attempt to complete, then record the resolution
864 // status of the attempt.
865 if (completed_attempt_error_ == OK) {
866 UMA_HISTOGRAM_ENUMERATION(
867 "DNS.AttemptFirstSuccess", attempt_number, 100);
868 } else {
869 UMA_HISTOGRAM_ENUMERATION(
870 "DNS.AttemptFirstFailure", attempt_number, 100);
874 if (error == OK)
875 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100);
876 else
877 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100);
879 // If first attempt didn't finish before retry attempt, then calculate stats
880 // on how much time is saved by having spawned an extra attempt.
881 if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) {
882 DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry",
883 base::TimeTicks::Now() - retry_attempt_finished_time_);
886 if (was_canceled() || !first_attempt_to_complete) {
887 // Count those attempts which completed after the job was already canceled
888 // OR after the job was already completed by an earlier attempt (so in
889 // effect).
890 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100);
892 // Record if job is canceled.
893 if (was_canceled())
894 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100);
897 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
898 if (error == OK)
899 DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration);
900 else
901 DNS_HISTOGRAM("DNS.AttemptFailDuration", duration);
904 // Set on the origin thread, read on the worker thread.
905 Key key_;
907 // Holds an owning reference to the HostResolverProc that we are going to use.
908 // This may not be the current resolver procedure by the time we call
909 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
910 // reference ensures that it remains valid until we are done.
911 ProcTaskParams params_;
913 // The listener to the results of this ProcTask.
914 Callback callback_;
916 // Used to post ourselves onto the origin thread.
917 scoped_refptr<base::MessageLoopProxy> origin_loop_;
919 // Keeps track of the number of attempts we have made so far to resolve the
920 // host. Whenever we start an attempt to resolve the host, we increase this
921 // number.
922 uint32 attempt_number_;
924 // The index of the attempt which finished first (or 0 if the job is still in
925 // progress).
926 uint32 completed_attempt_number_;
928 // The result (a net error code) from the first attempt to complete.
929 int completed_attempt_error_;
931 // The time when retry attempt was finished.
932 base::TimeTicks retry_attempt_finished_time_;
934 // True if a non-speculative request was ever attached to this job
935 // (regardless of whether or not it was later canceled.
936 // This boolean is used for histogramming the duration of jobs used to
937 // service non-speculative requests.
938 bool had_non_speculative_request_;
940 AddressList results_;
942 BoundNetLog net_log_;
944 DISALLOW_COPY_AND_ASSIGN(ProcTask);
947 //-----------------------------------------------------------------------------
949 // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as
950 // it takes 40-100ms and should not block initialization.
951 class HostResolverImpl::LoopbackProbeJob {
952 public:
953 explicit LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver)
954 : resolver_(resolver),
955 result_(false) {
956 DCHECK(resolver.get());
957 const bool kIsSlow = true;
958 base::WorkerPool::PostTaskAndReply(
959 FROM_HERE,
960 base::Bind(&LoopbackProbeJob::DoProbe, base::Unretained(this)),
961 base::Bind(&LoopbackProbeJob::OnProbeComplete, base::Owned(this)),
962 kIsSlow);
965 virtual ~LoopbackProbeJob() {}
967 private:
968 // Runs on worker thread.
969 void DoProbe() {
970 result_ = HaveOnlyLoopbackAddresses();
973 void OnProbeComplete() {
974 if (!resolver_.get())
975 return;
976 resolver_->SetHaveOnlyLoopbackAddresses(result_);
979 // Used/set only on origin thread.
980 base::WeakPtr<HostResolverImpl> resolver_;
982 bool result_;
984 DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob);
987 //-----------------------------------------------------------------------------
989 // Resolves the hostname using DnsTransaction.
990 // TODO(szym): This could be moved to separate source file as well.
991 class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> {
992 public:
993 class Delegate {
994 public:
995 virtual void OnDnsTaskComplete(base::TimeTicks start_time,
996 int net_error,
997 const AddressList& addr_list,
998 base::TimeDelta ttl) = 0;
1000 // Called when the first of two jobs succeeds. If the first completed
1001 // transaction fails, this is not called. Also not called when the DnsTask
1002 // only needs to run one transaction.
1003 virtual void OnFirstDnsTransactionComplete() = 0;
1005 protected:
1006 Delegate() {}
1007 virtual ~Delegate() {}
1010 DnsTask(DnsClient* client,
1011 const Key& key,
1012 Delegate* delegate,
1013 const BoundNetLog& job_net_log)
1014 : client_(client),
1015 key_(key),
1016 delegate_(delegate),
1017 net_log_(job_net_log),
1018 num_completed_transactions_(0),
1019 task_start_time_(base::TimeTicks::Now()) {
1020 DCHECK(client);
1021 DCHECK(delegate_);
1024 bool needs_two_transactions() const {
1025 return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED;
1028 bool needs_another_transaction() const {
1029 return needs_two_transactions() && !transaction_aaaa_;
1032 void StartFirstTransaction() {
1033 DCHECK_EQ(0u, num_completed_transactions_);
1034 net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK);
1035 if (key_.address_family == ADDRESS_FAMILY_IPV6) {
1036 StartAAAA();
1037 } else {
1038 StartA();
1042 void StartSecondTransaction() {
1043 DCHECK(needs_two_transactions());
1044 StartAAAA();
1047 private:
1048 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();
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();
1062 scoped_ptr<DnsTransaction> CreateTransaction(AddressFamily family) {
1063 DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family);
1064 return client_->GetTransactionFactory()->CreateTransaction(
1065 key_.hostname,
1066 family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA :
1067 dns_protocol::kTypeA,
1068 base::Bind(&DnsTask::OnTransactionComplete, base::Unretained(this),
1069 base::TimeTicks::Now()),
1070 net_log_);
1073 void OnTransactionComplete(const base::TimeTicks& start_time,
1074 DnsTransaction* transaction,
1075 int net_error,
1076 const DnsResponse* response) {
1077 DCHECK(transaction);
1078 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
1079 if (net_error != OK) {
1080 DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration);
1081 OnFailure(net_error, DnsResponse::DNS_PARSE_OK);
1082 return;
1085 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration);
1086 switch (transaction->GetType()) {
1087 case dns_protocol::kTypeA:
1088 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration);
1089 break;
1090 case dns_protocol::kTypeAAAA:
1091 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration);
1092 break;
1095 AddressList addr_list;
1096 base::TimeDelta ttl;
1097 DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl);
1098 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
1099 result,
1100 DnsResponse::DNS_PARSE_RESULT_MAX);
1101 if (result != DnsResponse::DNS_PARSE_OK) {
1102 // Fail even if the other query succeeds.
1103 OnFailure(ERR_DNS_MALFORMED_RESPONSE, result);
1104 return;
1107 ++num_completed_transactions_;
1108 if (num_completed_transactions_ == 1) {
1109 ttl_ = ttl;
1110 } else {
1111 ttl_ = std::min(ttl_, ttl);
1114 if (transaction->GetType() == dns_protocol::kTypeA) {
1115 DCHECK_EQ(transaction_a_.get(), transaction);
1116 // Place IPv4 addresses after IPv6.
1117 addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end());
1118 } else {
1119 DCHECK_EQ(transaction_aaaa_.get(), transaction);
1120 // Place IPv6 addresses before IPv4.
1121 addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end());
1124 if (needs_two_transactions() && num_completed_transactions_ == 1) {
1125 // No need to repeat the suffix search.
1126 key_.hostname = transaction->GetHostname();
1127 delegate_->OnFirstDnsTransactionComplete();
1128 return;
1131 if (addr_list_.empty()) {
1132 // TODO(szym): Don't fallback to ProcTask in this case.
1133 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
1134 return;
1137 // If there are multiple addresses, and at least one is IPv6, need to sort
1138 // them. Note that IPv6 addresses are always put before IPv4 ones, so it's
1139 // sufficient to just check the family of the first address.
1140 if (addr_list_.size() > 1 &&
1141 addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) {
1142 // Sort addresses if needed. Sort could complete synchronously.
1143 client_->GetAddressSorter()->Sort(
1144 addr_list_,
1145 base::Bind(&DnsTask::OnSortComplete,
1146 AsWeakPtr(),
1147 base::TimeTicks::Now()));
1148 } else {
1149 OnSuccess(addr_list_);
1153 void OnSortComplete(base::TimeTicks start_time,
1154 bool success,
1155 const AddressList& addr_list) {
1156 if (!success) {
1157 DNS_HISTOGRAM("AsyncDNS.SortFailure",
1158 base::TimeTicks::Now() - start_time);
1159 OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK);
1160 return;
1163 DNS_HISTOGRAM("AsyncDNS.SortSuccess",
1164 base::TimeTicks::Now() - start_time);
1166 // AddressSorter prunes unusable destinations.
1167 if (addr_list.empty()) {
1168 LOG(WARNING) << "Address list empty after RFC3484 sort";
1169 OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
1170 return;
1173 OnSuccess(addr_list);
1176 void OnFailure(int net_error, DnsResponse::Result result) {
1177 DCHECK_NE(OK, net_error);
1178 net_log_.EndEvent(
1179 NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK,
1180 base::Bind(&NetLogDnsTaskFailedCallback, net_error, result));
1181 delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(),
1182 base::TimeDelta());
1185 void OnSuccess(const AddressList& addr_list) {
1186 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK,
1187 addr_list.CreateNetLogCallback());
1188 delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_);
1191 DnsClient* client_;
1192 Key key_;
1194 // The listener to the results of this DnsTask.
1195 Delegate* delegate_;
1196 const BoundNetLog net_log_;
1198 scoped_ptr<DnsTransaction> transaction_a_;
1199 scoped_ptr<DnsTransaction> transaction_aaaa_;
1201 unsigned num_completed_transactions_;
1203 // These are updated as each transaction completes.
1204 base::TimeDelta ttl_;
1205 // IPv6 addresses must appear first in the list.
1206 AddressList addr_list_;
1208 base::TimeTicks task_start_time_;
1210 DISALLOW_COPY_AND_ASSIGN(DnsTask);
1213 //-----------------------------------------------------------------------------
1215 // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch.
1216 class HostResolverImpl::Job : public PrioritizedDispatcher::Job,
1217 public HostResolverImpl::DnsTask::Delegate {
1218 public:
1219 // Creates new job for |key| where |request_net_log| is bound to the
1220 // request that spawned it.
1221 Job(const base::WeakPtr<HostResolverImpl>& resolver,
1222 const Key& key,
1223 RequestPriority priority,
1224 const BoundNetLog& source_net_log)
1225 : resolver_(resolver),
1226 key_(key),
1227 priority_tracker_(priority),
1228 had_non_speculative_request_(false),
1229 had_dns_config_(false),
1230 num_occupied_job_slots_(0),
1231 dns_task_error_(OK),
1232 creation_time_(base::TimeTicks::Now()),
1233 priority_change_time_(creation_time_),
1234 net_log_(BoundNetLog::Make(source_net_log.net_log(),
1235 NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)) {
1236 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB);
1238 net_log_.BeginEvent(
1239 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1240 base::Bind(&NetLogJobCreationCallback,
1241 source_net_log.source(),
1242 &key_.hostname));
1245 ~Job() override {
1246 if (is_running()) {
1247 // |resolver_| was destroyed with this Job still in flight.
1248 // Clean-up, record in the log, but don't run any callbacks.
1249 if (is_proc_running()) {
1250 proc_task_->Cancel();
1251 proc_task_ = NULL;
1253 // Clean up now for nice NetLog.
1254 KillDnsTask();
1255 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1256 ERR_ABORTED);
1257 } else if (is_queued()) {
1258 // |resolver_| was destroyed without running this Job.
1259 // TODO(szym): is there any benefit in having this distinction?
1260 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
1261 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB);
1263 // else CompleteRequests logged EndEvent.
1265 // Log any remaining Requests as cancelled.
1266 for (RequestsList::const_iterator it = requests_.begin();
1267 it != requests_.end(); ++it) {
1268 Request* req = *it;
1269 if (req->was_canceled())
1270 continue;
1271 DCHECK_EQ(this, req->job());
1272 LogCancelRequest(req->source_net_log(), req->info());
1276 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1277 // of the queue.
1278 void Schedule(bool at_head) {
1279 DCHECK(!is_queued());
1280 PrioritizedDispatcher::Handle handle;
1281 if (!at_head) {
1282 handle = resolver_->dispatcher_->Add(this, priority());
1283 } else {
1284 handle = resolver_->dispatcher_->AddAtHead(this, priority());
1286 // The dispatcher could have started |this| in the above call to Add, which
1287 // could have called Schedule again. In that case |handle| will be null,
1288 // but |handle_| may have been set by the other nested call to Schedule.
1289 if (!handle.is_null()) {
1290 DCHECK(handle_.is_null());
1291 handle_ = handle;
1295 void AddRequest(scoped_ptr<Request> req) {
1296 // .localhost queries are redirected to "localhost." to make sure
1297 // that they are never sent out on the network, per RFC 6761.
1298 if (IsLocalhostTLD(req->info().hostname())) {
1299 DCHECK_EQ(key_.hostname, kLocalhost);
1300 } else {
1301 DCHECK_EQ(key_.hostname, req->info().hostname());
1304 req->set_job(this);
1305 priority_tracker_.Add(req->priority());
1307 req->source_net_log().AddEvent(
1308 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH,
1309 net_log_.source().ToEventParametersCallback());
1311 net_log_.AddEvent(
1312 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH,
1313 base::Bind(&NetLogJobAttachCallback,
1314 req->source_net_log().source(),
1315 priority()));
1317 // TODO(szym): Check if this is still needed.
1318 if (!req->info().is_speculative()) {
1319 had_non_speculative_request_ = true;
1320 if (proc_task_.get())
1321 proc_task_->set_had_non_speculative_request();
1324 requests_.push_back(req.release());
1326 UpdatePriority();
1329 // Marks |req| as cancelled. If it was the last active Request, also finishes
1330 // this Job, marking it as cancelled, and deletes it.
1331 void CancelRequest(Request* req) {
1332 DCHECK_EQ(key_.hostname, req->info().hostname());
1333 DCHECK(!req->was_canceled());
1335 // Don't remove it from |requests_| just mark it canceled.
1336 req->MarkAsCanceled();
1337 LogCancelRequest(req->source_net_log(), req->info());
1339 priority_tracker_.Remove(req->priority());
1340 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH,
1341 base::Bind(&NetLogJobAttachCallback,
1342 req->source_net_log().source(),
1343 priority()));
1345 if (num_active_requests() > 0) {
1346 UpdatePriority();
1347 } else {
1348 // If we were called from a Request's callback within CompleteRequests,
1349 // that Request could not have been cancelled, so num_active_requests()
1350 // could not be 0. Therefore, we are not in CompleteRequests().
1351 CompleteRequestsWithError(OK /* cancelled */);
1355 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1356 // the job. This currently assumes the abort is due to a network change.
1357 void Abort() {
1358 DCHECK(is_running());
1359 CompleteRequestsWithError(ERR_NETWORK_CHANGED);
1362 // If DnsTask present, abort it and fall back to ProcTask.
1363 void AbortDnsTask() {
1364 if (dns_task_) {
1365 KillDnsTask();
1366 dns_task_error_ = OK;
1367 StartProcTask();
1371 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1372 // Completes all requests and destroys the job.
1373 void OnEvicted() {
1374 DCHECK(!is_running());
1375 DCHECK(is_queued());
1376 handle_.Reset();
1378 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED);
1380 // This signals to CompleteRequests that this job never ran.
1381 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
1384 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1385 // this Job was destroyed.
1386 bool ServeFromHosts() {
1387 DCHECK_GT(num_active_requests(), 0u);
1388 AddressList addr_list;
1389 if (resolver_->ServeFromHosts(key(),
1390 requests_.front()->info(),
1391 &addr_list)) {
1392 // This will destroy the Job.
1393 CompleteRequests(
1394 HostCache::Entry(OK, MakeAddressListForRequest(addr_list)),
1395 base::TimeDelta());
1396 return true;
1398 return false;
1401 const Key key() const {
1402 return key_;
1405 bool is_queued() const {
1406 return !handle_.is_null();
1409 bool is_running() const {
1410 return is_dns_running() || is_proc_running();
1413 private:
1414 void KillDnsTask() {
1415 if (dns_task_) {
1416 ReduceToOneJobSlot();
1417 dns_task_.reset();
1421 // Reduce the number of job slots occupied and queued in the dispatcher
1422 // to one. If the second Job slot is queued in the dispatcher, cancels the
1423 // queued job. Otherwise, the second Job has been started by the
1424 // PrioritizedDispatcher, so signals it is complete.
1425 void ReduceToOneJobSlot() {
1426 DCHECK_GE(num_occupied_job_slots_, 1u);
1427 if (is_queued()) {
1428 resolver_->dispatcher_->Cancel(handle_);
1429 handle_.Reset();
1430 } else if (num_occupied_job_slots_ > 1) {
1431 resolver_->dispatcher_->OnJobFinished();
1432 --num_occupied_job_slots_;
1434 DCHECK_EQ(1u, num_occupied_job_slots_);
1437 void UpdatePriority() {
1438 if (is_queued()) {
1439 if (priority() != static_cast<RequestPriority>(handle_.priority()))
1440 priority_change_time_ = base::TimeTicks::Now();
1441 handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
1445 AddressList MakeAddressListForRequest(const AddressList& list) const {
1446 if (requests_.empty())
1447 return list;
1448 return AddressList::CopyWithPort(list, requests_.front()->info().port());
1451 // PriorityDispatch::Job:
1452 void Start() override {
1453 DCHECK_LE(num_occupied_job_slots_, 1u);
1455 handle_.Reset();
1456 ++num_occupied_job_slots_;
1458 if (num_occupied_job_slots_ == 2) {
1459 StartSecondDnsTransaction();
1460 return;
1463 DCHECK(!is_running());
1465 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED);
1467 had_dns_config_ = resolver_->HaveDnsConfig();
1469 base::TimeTicks now = base::TimeTicks::Now();
1470 base::TimeDelta queue_time = now - creation_time_;
1471 base::TimeDelta queue_time_after_change = now - priority_change_time_;
1473 if (had_dns_config_) {
1474 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(),
1475 queue_time);
1476 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(),
1477 queue_time_after_change);
1478 } else {
1479 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time);
1480 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(),
1481 queue_time_after_change);
1484 bool system_only =
1485 (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0;
1487 // Caution: Job::Start must not complete synchronously.
1488 if (!system_only && had_dns_config_ &&
1489 !ResemblesMulticastDNSName(key_.hostname)) {
1490 StartDnsTask();
1491 } else {
1492 StartProcTask();
1496 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
1497 // the limits on |dispatcher_|. But in order to keep the number of WorkerPool
1498 // threads low, we will need to use an "inner" PrioritizedDispatcher with
1499 // tighter limits.
1500 void StartProcTask() {
1501 DCHECK(!is_dns_running());
1502 proc_task_ = new ProcTask(
1503 key_,
1504 resolver_->proc_params_,
1505 base::Bind(&Job::OnProcTaskComplete, base::Unretained(this),
1506 base::TimeTicks::Now()),
1507 net_log_);
1509 if (had_non_speculative_request_)
1510 proc_task_->set_had_non_speculative_request();
1511 // Start() could be called from within Resolve(), hence it must NOT directly
1512 // call OnProcTaskComplete, for example, on synchronous failure.
1513 proc_task_->Start();
1516 // Called by ProcTask when it completes.
1517 void OnProcTaskComplete(base::TimeTicks start_time,
1518 int net_error,
1519 const AddressList& addr_list) {
1520 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
1521 tracked_objects::ScopedTracker tracking_profile(
1522 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1523 "436634 HostResolverImpl::Job::OnProcTaskComplete"));
1525 DCHECK(is_proc_running());
1527 if (!resolver_->resolved_known_ipv6_hostname_ &&
1528 net_error == OK &&
1529 key_.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
1530 if (key_.hostname == "www.google.com") {
1531 resolver_->resolved_known_ipv6_hostname_ = true;
1532 bool got_ipv6_address = false;
1533 for (size_t i = 0; i < addr_list.size(); ++i) {
1534 if (addr_list[i].GetFamily() == ADDRESS_FAMILY_IPV6) {
1535 got_ipv6_address = true;
1536 break;
1539 UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address);
1543 if (dns_task_error_ != OK) {
1544 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
1545 if (net_error == OK) {
1546 DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration);
1547 if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) &&
1548 ResemblesNetBIOSName(key_.hostname)) {
1549 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS);
1550 } else {
1551 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS);
1553 UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.ResolveError",
1554 std::abs(dns_task_error_),
1555 GetAllErrorCodesForUma());
1556 resolver_->OnDnsTaskResolve(dns_task_error_);
1557 } else {
1558 DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration);
1559 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
1563 base::TimeDelta ttl =
1564 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds);
1565 if (net_error == OK)
1566 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds);
1568 // Don't store the |ttl| in cache since it's not obtained from the server.
1569 CompleteRequests(
1570 HostCache::Entry(net_error, MakeAddressListForRequest(addr_list)),
1571 ttl);
1574 void StartDnsTask() {
1575 DCHECK(resolver_->HaveDnsConfig());
1576 dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this,
1577 net_log_));
1579 dns_task_->StartFirstTransaction();
1580 // Schedule a second transaction, if needed.
1581 if (dns_task_->needs_two_transactions())
1582 Schedule(true);
1585 void StartSecondDnsTransaction() {
1586 DCHECK(dns_task_->needs_two_transactions());
1587 dns_task_->StartSecondTransaction();
1590 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
1591 // deleted before this callback. In this case dns_task is deleted as well,
1592 // so we use it as indicator whether Job is still valid.
1593 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
1594 base::TimeDelta duration,
1595 int net_error) {
1596 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration);
1598 if (dns_task == NULL)
1599 return;
1601 dns_task_error_ = net_error;
1603 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so.
1604 // http://crbug.com/117655
1606 // TODO(szym): Some net errors indicate lack of connectivity. Starting
1607 // ProcTask in that case is a waste of time.
1608 if (resolver_->fallback_to_proctask_) {
1609 KillDnsTask();
1610 StartProcTask();
1611 } else {
1612 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
1613 CompleteRequestsWithError(net_error);
1618 // HostResolverImpl::DnsTask::Delegate implementation:
1620 void OnDnsTaskComplete(base::TimeTicks start_time,
1621 int net_error,
1622 const AddressList& addr_list,
1623 base::TimeDelta ttl) override {
1624 DCHECK(is_dns_running());
1626 base::TimeDelta duration = base::TimeTicks::Now() - start_time;
1627 if (net_error != OK) {
1628 OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error);
1629 return;
1631 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration);
1632 // Log DNS lookups based on |address_family|.
1633 switch(key_.address_family) {
1634 case ADDRESS_FAMILY_IPV4:
1635 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration);
1636 break;
1637 case ADDRESS_FAMILY_IPV6:
1638 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration);
1639 break;
1640 case ADDRESS_FAMILY_UNSPECIFIED:
1641 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration);
1642 break;
1645 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS);
1646 RecordTTL(ttl);
1648 resolver_->OnDnsTaskResolve(OK);
1650 base::TimeDelta bounded_ttl =
1651 std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds));
1653 CompleteRequests(
1654 HostCache::Entry(net_error, MakeAddressListForRequest(addr_list), ttl),
1655 bounded_ttl);
1658 void OnFirstDnsTransactionComplete() override {
1659 DCHECK(dns_task_->needs_two_transactions());
1660 DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued());
1661 // No longer need to occupy two dispatcher slots.
1662 ReduceToOneJobSlot();
1664 // We already have a job slot at the dispatcher, so if the second
1665 // transaction hasn't started, reuse it now instead of waiting in the queue
1666 // for the second slot.
1667 if (dns_task_->needs_another_transaction())
1668 dns_task_->StartSecondTransaction();
1671 // Performs Job's last rites. Completes all Requests. Deletes this.
1672 void CompleteRequests(const HostCache::Entry& entry,
1673 base::TimeDelta ttl) {
1674 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
1675 tracked_objects::ScopedTracker tracking_profile1(
1676 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1677 "436634 HostResolverImpl::Job::CompleteRequests1"));
1679 CHECK(resolver_.get());
1681 // This job must be removed from resolver's |jobs_| now to make room for a
1682 // new job with the same key in case one of the OnComplete callbacks decides
1683 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1684 // is done.
1685 scoped_ptr<Job> self_deleter(this);
1687 resolver_->RemoveJob(this);
1689 if (is_running()) {
1690 if (is_proc_running()) {
1691 DCHECK(!is_queued());
1692 proc_task_->Cancel();
1693 proc_task_ = NULL;
1695 KillDnsTask();
1697 // Signal dispatcher that a slot has opened.
1698 resolver_->dispatcher_->OnJobFinished();
1699 } else if (is_queued()) {
1700 resolver_->dispatcher_->Cancel(handle_);
1701 handle_.Reset();
1704 if (num_active_requests() == 0) {
1705 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
1706 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1707 OK);
1708 return;
1711 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1712 entry.error);
1714 DCHECK(!requests_.empty());
1716 if (entry.error == OK) {
1717 // Record this histogram here, when we know the system has a valid DNS
1718 // configuration.
1719 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1720 resolver_->received_dns_config_);
1723 bool did_complete = (entry.error != ERR_NETWORK_CHANGED) &&
1724 (entry.error != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
1725 if (did_complete)
1726 resolver_->CacheResult(key_, entry, ttl);
1728 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
1729 tracked_objects::ScopedTracker tracking_profile2(
1730 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1731 "436634 HostResolverImpl::Job::CompleteRequests2"));
1733 // Complete all of the requests that were attached to the job.
1734 for (RequestsList::const_iterator it = requests_.begin();
1735 it != requests_.end(); ++it) {
1736 Request* req = *it;
1738 if (req->was_canceled())
1739 continue;
1741 DCHECK_EQ(this, req->job());
1742 // Update the net log and notify registered observers.
1743 LogFinishRequest(req->source_net_log(), req->info(), entry.error);
1744 if (did_complete) {
1745 // Record effective total time from creation to completion.
1746 RecordTotalTime(had_dns_config_, req->info().is_speculative(),
1747 base::TimeTicks::Now() - req->request_time());
1749 req->OnComplete(entry.error, entry.addrlist);
1751 // Check if the resolver was destroyed as a result of running the
1752 // callback. If it was, we could continue, but we choose to bail.
1753 if (!resolver_.get())
1754 return;
1758 // Convenience wrapper for CompleteRequests in case of failure.
1759 void CompleteRequestsWithError(int net_error) {
1760 CompleteRequests(HostCache::Entry(net_error, AddressList()),
1761 base::TimeDelta());
1764 RequestPriority priority() const {
1765 return priority_tracker_.highest_priority();
1768 // Number of non-canceled requests in |requests_|.
1769 size_t num_active_requests() const {
1770 return priority_tracker_.total_count();
1773 bool is_dns_running() const {
1774 return dns_task_.get() != NULL;
1777 bool is_proc_running() const {
1778 return proc_task_.get() != NULL;
1781 base::WeakPtr<HostResolverImpl> resolver_;
1783 Key key_;
1785 // Tracks the highest priority across |requests_|.
1786 PriorityTracker priority_tracker_;
1788 bool had_non_speculative_request_;
1790 // Distinguishes measurements taken while DnsClient was fully configured.
1791 bool had_dns_config_;
1793 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1794 unsigned num_occupied_job_slots_;
1796 // Result of DnsTask.
1797 int dns_task_error_;
1799 const base::TimeTicks creation_time_;
1800 base::TimeTicks priority_change_time_;
1802 BoundNetLog net_log_;
1804 // Resolves the host using a HostResolverProc.
1805 scoped_refptr<ProcTask> proc_task_;
1807 // Resolves the host using a DnsTransaction.
1808 scoped_ptr<DnsTask> dns_task_;
1810 // All Requests waiting for the result of this Job. Some can be canceled.
1811 RequestsList requests_;
1813 // A handle used in |HostResolverImpl::dispatcher_|.
1814 PrioritizedDispatcher::Handle handle_;
1817 //-----------------------------------------------------------------------------
1819 HostResolverImpl::ProcTaskParams::ProcTaskParams(
1820 HostResolverProc* resolver_proc,
1821 size_t max_retry_attempts)
1822 : resolver_proc(resolver_proc),
1823 max_retry_attempts(max_retry_attempts),
1824 unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)),
1825 retry_factor(2) {
1826 // Maximum of 4 retry attempts for host resolution.
1827 static const size_t kDefaultMaxRetryAttempts = 4u;
1828 if (max_retry_attempts == HostResolver::kDefaultRetryAttempts)
1829 max_retry_attempts = kDefaultMaxRetryAttempts;
1832 HostResolverImpl::ProcTaskParams::~ProcTaskParams() {}
1834 HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log)
1835 : max_queued_jobs_(0),
1836 proc_params_(NULL, options.max_retry_attempts),
1837 net_log_(net_log),
1838 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED),
1839 received_dns_config_(false),
1840 num_dns_failures_(0),
1841 probe_ipv6_support_(true),
1842 use_local_ipv6_(false),
1843 resolved_known_ipv6_hostname_(false),
1844 additional_resolver_flags_(0),
1845 fallback_to_proctask_(true),
1846 weak_ptr_factory_(this),
1847 probe_weak_ptr_factory_(this) {
1848 if (options.enable_caching)
1849 cache_ = HostCache::CreateDefaultCache();
1851 PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits();
1852 dispatcher_.reset(new PrioritizedDispatcher(job_limits));
1853 max_queued_jobs_ = job_limits.total_jobs * 100u;
1855 DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
1857 #if defined(OS_WIN)
1858 EnsureWinsockInit();
1859 #endif
1860 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
1861 new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr());
1862 #endif
1863 NetworkChangeNotifier::AddIPAddressObserver(this);
1864 NetworkChangeNotifier::AddDNSObserver(this);
1865 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
1866 !defined(OS_ANDROID)
1867 EnsureDnsReloaderInit();
1868 #endif
1871 DnsConfig dns_config;
1872 NetworkChangeNotifier::GetDnsConfig(&dns_config);
1873 received_dns_config_ = dns_config.IsValid();
1874 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
1875 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
1878 fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
1881 HostResolverImpl::~HostResolverImpl() {
1882 // Prevent the dispatcher from starting new jobs.
1883 dispatcher_->SetLimitsToZero();
1884 // It's now safe for Jobs to call KillDsnTask on destruction, because
1885 // OnJobComplete will not start any new jobs.
1886 STLDeleteValues(&jobs_);
1888 NetworkChangeNotifier::RemoveIPAddressObserver(this);
1889 NetworkChangeNotifier::RemoveDNSObserver(this);
1892 void HostResolverImpl::SetMaxQueuedJobs(size_t value) {
1893 DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
1894 DCHECK_GT(value, 0u);
1895 max_queued_jobs_ = value;
1898 int HostResolverImpl::Resolve(const RequestInfo& info,
1899 RequestPriority priority,
1900 AddressList* addresses,
1901 const CompletionCallback& callback,
1902 RequestHandle* out_req,
1903 const BoundNetLog& source_net_log) {
1904 DCHECK(addresses);
1905 DCHECK(CalledOnValidThread());
1906 DCHECK_EQ(false, callback.is_null());
1908 // Check that the caller supplied a valid hostname to resolve.
1909 std::string labeled_hostname;
1910 if (!DNSDomainFromDot(info.hostname(), &labeled_hostname))
1911 return ERR_NAME_NOT_RESOLVED;
1913 LogStartRequest(source_net_log, info);
1915 // Build a key that identifies the request in the cache and in the
1916 // outstanding jobs map.
1917 Key key = GetEffectiveKeyForRequest(info, source_net_log);
1919 int rv = ResolveHelper(key, info, addresses, source_net_log);
1920 if (rv != ERR_DNS_CACHE_MISS) {
1921 LogFinishRequest(source_net_log, info, rv);
1922 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta());
1923 return rv;
1926 // Next we need to attach our request to a "job". This job is responsible for
1927 // calling "getaddrinfo(hostname)" on a worker thread.
1929 JobMap::iterator jobit = jobs_.find(key);
1930 Job* job;
1931 if (jobit == jobs_.end()) {
1932 job =
1933 new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, source_net_log);
1934 job->Schedule(false);
1936 // Check for queue overflow.
1937 if (dispatcher_->num_queued_jobs() > max_queued_jobs_) {
1938 Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest());
1939 DCHECK(evicted);
1940 evicted->OnEvicted(); // Deletes |evicted|.
1941 if (evicted == job) {
1942 rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE;
1943 LogFinishRequest(source_net_log, info, rv);
1944 return rv;
1947 jobs_.insert(jobit, std::make_pair(key, job));
1948 } else {
1949 job = jobit->second;
1952 // Can't complete synchronously. Create and attach request.
1953 scoped_ptr<Request> req(new Request(
1954 source_net_log, info, priority, callback, addresses));
1955 if (out_req)
1956 *out_req = reinterpret_cast<RequestHandle>(req.get());
1958 job->AddRequest(req.Pass());
1959 // Completion happens during Job::CompleteRequests().
1960 return ERR_IO_PENDING;
1963 int HostResolverImpl::ResolveHelper(const Key& key,
1964 const RequestInfo& info,
1965 AddressList* addresses,
1966 const BoundNetLog& source_net_log) {
1967 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
1968 // On Windows it gives the default interface's address, whereas on Linux it
1969 // gives an error. We will make it fail on all platforms for consistency.
1970 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength)
1971 return ERR_NAME_NOT_RESOLVED;
1973 int net_error = ERR_UNEXPECTED;
1974 if (ResolveAsIP(key, info, &net_error, addresses))
1975 return net_error;
1976 if (ServeFromCache(key, info, &net_error, addresses)) {
1977 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT);
1978 return net_error;
1980 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
1981 // http://crbug.com/117655
1982 if (ServeFromHosts(key, info, addresses)) {
1983 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT);
1984 return OK;
1986 return ERR_DNS_CACHE_MISS;
1989 int HostResolverImpl::ResolveFromCache(const RequestInfo& info,
1990 AddressList* addresses,
1991 const BoundNetLog& source_net_log) {
1992 DCHECK(CalledOnValidThread());
1993 DCHECK(addresses);
1995 // Update the net log and notify registered observers.
1996 LogStartRequest(source_net_log, info);
1998 Key key = GetEffectiveKeyForRequest(info, source_net_log);
2000 int rv = ResolveHelper(key, info, addresses, source_net_log);
2001 LogFinishRequest(source_net_log, info, rv);
2002 return rv;
2005 void HostResolverImpl::CancelRequest(RequestHandle req_handle) {
2006 DCHECK(CalledOnValidThread());
2007 Request* req = reinterpret_cast<Request*>(req_handle);
2008 DCHECK(req);
2009 Job* job = req->job();
2010 DCHECK(job);
2011 job->CancelRequest(req);
2014 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) {
2015 DCHECK(CalledOnValidThread());
2016 default_address_family_ = address_family;
2017 probe_ipv6_support_ = false;
2020 AddressFamily HostResolverImpl::GetDefaultAddressFamily() const {
2021 return default_address_family_;
2024 void HostResolverImpl::SetDnsClientEnabled(bool enabled) {
2025 DCHECK(CalledOnValidThread());
2026 #if defined(ENABLE_BUILT_IN_DNS)
2027 if (enabled && !dns_client_) {
2028 SetDnsClient(DnsClient::CreateClient(net_log_));
2029 } else if (!enabled && dns_client_) {
2030 SetDnsClient(scoped_ptr<DnsClient>());
2032 #endif
2035 HostCache* HostResolverImpl::GetHostCache() {
2036 return cache_.get();
2039 base::Value* HostResolverImpl::GetDnsConfigAsValue() const {
2040 // Check if async DNS is disabled.
2041 if (!dns_client_.get())
2042 return NULL;
2044 // Check if async DNS is enabled, but we currently have no configuration
2045 // for it.
2046 const DnsConfig* dns_config = dns_client_->GetConfig();
2047 if (dns_config == NULL)
2048 return new base::DictionaryValue();
2050 return dns_config->ToValue();
2053 bool HostResolverImpl::ResolveAsIP(const Key& key,
2054 const RequestInfo& info,
2055 int* net_error,
2056 AddressList* addresses) {
2057 DCHECK(addresses);
2058 DCHECK(net_error);
2059 IPAddressNumber ip_number;
2060 if (!ParseIPLiteralToNumber(key.hostname, &ip_number))
2061 return false;
2063 DCHECK_EQ(key.host_resolver_flags &
2064 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY |
2065 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6),
2066 0) << " Unhandled flag";
2068 *net_error = OK;
2069 AddressFamily family = GetAddressFamily(ip_number);
2070 if (family == ADDRESS_FAMILY_IPV6 &&
2071 !probe_ipv6_support_ &&
2072 default_address_family_ == ADDRESS_FAMILY_IPV4) {
2073 // Don't return IPv6 addresses if default address family is set to IPv4,
2074 // and probes are disabled.
2075 *net_error = ERR_NAME_NOT_RESOLVED;
2076 } else if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED &&
2077 key.address_family != family) {
2078 // Don't return IPv6 addresses for IPv4 queries, and vice versa.
2079 *net_error = ERR_NAME_NOT_RESOLVED;
2080 } else {
2081 *addresses = AddressList::CreateFromIPAddress(ip_number, info.port());
2082 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)
2083 addresses->SetDefaultCanonicalName();
2085 return true;
2088 bool HostResolverImpl::ServeFromCache(const Key& key,
2089 const RequestInfo& info,
2090 int* net_error,
2091 AddressList* addresses) {
2092 DCHECK(addresses);
2093 DCHECK(net_error);
2094 if (!info.allow_cached_response() || !cache_.get())
2095 return false;
2097 const HostCache::Entry* cache_entry = cache_->Lookup(
2098 key, base::TimeTicks::Now());
2099 if (!cache_entry)
2100 return false;
2102 *net_error = cache_entry->error;
2103 if (*net_error == OK) {
2104 if (cache_entry->has_ttl())
2105 RecordTTL(cache_entry->ttl);
2106 *addresses = EnsurePortOnAddressList(cache_entry->addrlist, info.port());
2108 return true;
2111 bool HostResolverImpl::ServeFromHosts(const Key& key,
2112 const RequestInfo& info,
2113 AddressList* addresses) {
2114 DCHECK(addresses);
2115 if (!HaveDnsConfig())
2116 return false;
2117 addresses->clear();
2119 // HOSTS lookups are case-insensitive.
2120 std::string hostname = base::StringToLowerASCII(key.hostname);
2122 const DnsHosts& hosts = dns_client_->GetConfig()->hosts;
2124 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2125 // (glibc and c-ares) return the first matching line. We have more
2126 // flexibility, but lose implicit ordering.
2127 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2128 // necessary.
2129 if (key.address_family == ADDRESS_FAMILY_IPV6 ||
2130 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2131 DnsHosts::const_iterator it = hosts.find(
2132 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6));
2133 if (it != hosts.end())
2134 addresses->push_back(IPEndPoint(it->second, info.port()));
2137 if (key.address_family == ADDRESS_FAMILY_IPV4 ||
2138 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
2139 DnsHosts::const_iterator it = hosts.find(
2140 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4));
2141 if (it != hosts.end())
2142 addresses->push_back(IPEndPoint(it->second, info.port()));
2145 // If got only loopback addresses and the family was restricted, resolve
2146 // again, without restrictions. See SystemHostResolverCall for rationale.
2147 if ((key.host_resolver_flags &
2148 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
2149 IsAllIPv4Loopback(*addresses)) {
2150 Key new_key(key);
2151 new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED;
2152 new_key.host_resolver_flags &=
2153 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2154 return ServeFromHosts(new_key, info, addresses);
2156 return !addresses->empty();
2159 void HostResolverImpl::CacheResult(const Key& key,
2160 const HostCache::Entry& entry,
2161 base::TimeDelta ttl) {
2162 if (cache_.get())
2163 cache_->Set(key, entry, base::TimeTicks::Now(), ttl);
2166 void HostResolverImpl::RemoveJob(Job* job) {
2167 DCHECK(job);
2168 JobMap::iterator it = jobs_.find(job->key());
2169 if (it != jobs_.end() && it->second == job)
2170 jobs_.erase(it);
2173 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) {
2174 if (result) {
2175 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
2176 } else {
2177 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
2181 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest(
2182 const RequestInfo& info, const BoundNetLog& net_log) const {
2183 HostResolverFlags effective_flags =
2184 info.host_resolver_flags() | additional_resolver_flags_;
2185 AddressFamily effective_address_family = info.address_family();
2187 if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) {
2188 unsigned char ip_number[4];
2189 url::Component host_comp(0, info.hostname().size());
2190 int num_components;
2191 if (probe_ipv6_support_ && !use_local_ipv6_ &&
2192 // Don't bother IPv6 probing when resolving IPv4 literals.
2193 url::IPv4AddressToNumber(info.hostname().c_str(), host_comp, ip_number,
2194 &num_components) != url::CanonHostInfo::IPV4) {
2195 // Google DNS address.
2196 const uint8 kIPv6Address[] =
2197 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
2198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
2199 IPAddressNumber address(kIPv6Address,
2200 kIPv6Address + arraysize(kIPv6Address));
2201 BoundNetLog probe_net_log = BoundNetLog::Make(
2202 net_log.net_log(), NetLog::SOURCE_IPV6_REACHABILITY_CHECK);
2203 probe_net_log.BeginEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK,
2204 net_log.source().ToEventParametersCallback());
2205 bool rv6 = IsGloballyReachable(address, probe_net_log);
2206 probe_net_log.EndEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK);
2207 if (rv6) {
2208 net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_SUPPORTED);
2209 } else {
2210 effective_address_family = ADDRESS_FAMILY_IPV4;
2211 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2213 } else {
2214 effective_address_family = default_address_family_;
2218 std::string hostname = info.hostname();
2219 // Redirect .localhost queries to "localhost." to make sure that they
2220 // are never sent out on the network, per RFC 6761.
2221 if (IsLocalhostTLD(info.hostname()))
2222 hostname = kLocalhost;
2224 return Key(hostname, effective_address_family, effective_flags);
2227 void HostResolverImpl::AbortAllInProgressJobs() {
2228 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2229 // first collect and remove all running jobs from |jobs_|.
2230 ScopedVector<Job> jobs_to_abort;
2231 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) {
2232 Job* job = it->second;
2233 if (job->is_running()) {
2234 jobs_to_abort.push_back(job);
2235 jobs_.erase(it++);
2236 } else {
2237 DCHECK(job->is_queued());
2238 ++it;
2242 // Pause the dispatcher so it won't start any new dispatcher jobs while
2243 // aborting the old ones. This is needed so that it won't start the second
2244 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2245 // invalid.
2246 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2247 dispatcher_->SetLimits(
2248 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2250 // Life check to bail once |this| is deleted.
2251 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
2253 // Then Abort them.
2254 for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
2255 jobs_to_abort[i]->Abort();
2256 jobs_to_abort[i] = NULL;
2259 if (self)
2260 dispatcher_->SetLimits(limits);
2263 void HostResolverImpl::AbortDnsTasks() {
2264 // Pause the dispatcher so it won't start any new dispatcher jobs while
2265 // aborting the old ones. This is needed so that it won't start the second
2266 // DnsTransaction for a job if the DnsConfig just changed.
2267 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2268 dispatcher_->SetLimits(
2269 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2271 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
2272 it->second->AbortDnsTask();
2273 dispatcher_->SetLimits(limits);
2276 void HostResolverImpl::TryServingAllJobsFromHosts() {
2277 if (!HaveDnsConfig())
2278 return;
2280 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2281 // http://crbug.com/117655
2283 // Life check to bail once |this| is deleted.
2284 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
2286 for (JobMap::iterator it = jobs_.begin(); self.get() && it != jobs_.end();) {
2287 Job* job = it->second;
2288 ++it;
2289 // This could remove |job| from |jobs_|, but iterator will remain valid.
2290 job->ServeFromHosts();
2294 void HostResolverImpl::OnIPAddressChanged() {
2295 resolved_known_ipv6_hostname_ = false;
2296 // Abandon all ProbeJobs.
2297 probe_weak_ptr_factory_.InvalidateWeakPtrs();
2298 if (cache_.get())
2299 cache_->clear();
2300 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
2301 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr());
2302 #endif
2303 AbortAllInProgressJobs();
2304 // |this| may be deleted inside AbortAllInProgressJobs().
2307 void HostResolverImpl::OnDNSChanged() {
2308 DnsConfig dns_config;
2309 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2311 if (net_log_) {
2312 net_log_->AddGlobalEntry(
2313 NetLog::TYPE_DNS_CONFIG_CHANGED,
2314 base::Bind(&NetLogDnsConfigCallback, &dns_config));
2317 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
2318 received_dns_config_ = dns_config.IsValid();
2319 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2320 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
2322 num_dns_failures_ = 0;
2324 // We want a new DnsSession in place, before we Abort running Jobs, so that
2325 // the newly started jobs use the new config.
2326 if (dns_client_.get()) {
2327 dns_client_->SetConfig(dns_config);
2328 if (dns_client_->GetConfig())
2329 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2332 // If the DNS server has changed, existing cached info could be wrong so we
2333 // have to drop our internal cache :( Note that OS level DNS caches, such
2334 // as NSCD's cache should be dropped automatically by the OS when
2335 // resolv.conf changes so we don't need to do anything to clear that cache.
2336 if (cache_.get())
2337 cache_->clear();
2339 // Life check to bail once |this| is deleted.
2340 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
2342 // Existing jobs will have been sent to the original server so they need to
2343 // be aborted.
2344 AbortAllInProgressJobs();
2346 // |this| may be deleted inside AbortAllInProgressJobs().
2347 if (self.get())
2348 TryServingAllJobsFromHosts();
2351 bool HostResolverImpl::HaveDnsConfig() const {
2352 // Use DnsClient only if it's fully configured and there is no override by
2353 // ScopedDefaultHostResolverProc.
2354 // The alternative is to use NetworkChangeNotifier to override DnsConfig,
2355 // but that would introduce construction order requirements for NCN and SDHRP.
2356 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL) &&
2357 !(proc_params_.resolver_proc.get() == NULL &&
2358 HostResolverProc::GetDefault() != NULL);
2361 void HostResolverImpl::OnDnsTaskResolve(int net_error) {
2362 DCHECK(dns_client_);
2363 if (net_error == OK) {
2364 num_dns_failures_ = 0;
2365 return;
2367 ++num_dns_failures_;
2368 if (num_dns_failures_ < kMaximumDnsFailures)
2369 return;
2371 // Disable DnsClient until the next DNS change. Must be done before aborting
2372 // DnsTasks, since doing so may start new jobs.
2373 dns_client_->SetConfig(DnsConfig());
2375 // Switch jobs with active DnsTasks over to using ProcTasks.
2376 AbortDnsTasks();
2378 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
2379 UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.DnsClientDisabledReason",
2380 std::abs(net_error),
2381 GetAllErrorCodesForUma());
2384 void HostResolverImpl::SetDnsClient(scoped_ptr<DnsClient> dns_client) {
2385 // DnsClient and config must be updated before aborting DnsTasks, since doing
2386 // so may start new jobs.
2387 dns_client_ = dns_client.Pass();
2388 if (dns_client_ && !dns_client_->GetConfig() &&
2389 num_dns_failures_ < kMaximumDnsFailures) {
2390 DnsConfig dns_config;
2391 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2392 dns_client_->SetConfig(dns_config);
2393 num_dns_failures_ = 0;
2394 if (dns_client_->GetConfig())
2395 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2398 AbortDnsTasks();
2401 } // namespace net