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"
9 #elif defined(OS_POSIX)
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"
55 #include "net/base/winsock_init.h"
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
[] =
82 "Net.OSErrorsForGetAddrinfo_Win";
83 #elif defined(OS_MACOSX)
84 "Net.OSErrorsForGetAddrinfo_Mac";
85 #elif defined(OS_LINUX)
86 "Net.OSErrorsForGetAddrinfo_Linux";
88 "Net.OSErrorsForGetAddrinfo";
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
94 std::vector
<int> GetAllGetAddrinfoOSErrors() {
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.
103 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
115 #elif defined(OS_WIN)
116 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
117 WSA_NOT_ENOUGH_MEMORY
,
127 // The following are not in doc, but might be to appearing in results :-(.
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
,
145 RESOLVE_STATUS_SUSPECT_NETBIOS
,
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",
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 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
189 tracked_objects::ScopedTracker
tracking_profile_1(
190 FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 IsGloballyReachable"));
192 scoped_ptr
<DatagramClientSocket
> socket(
193 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
194 DatagramSocket::DEFAULT_BIND
,
198 int rv
= socket
->Connect(IPEndPoint(dest
, 53));
202 rv
= socket
->GetLocalAddress(&endpoint
);
205 DCHECK_EQ(ADDRESS_FAMILY_IPV6
, endpoint
.GetFamily());
206 const IPAddressNumber
& address
= endpoint
.address();
207 bool is_link_local
= (address
[0] == 0xFE) && ((address
[1] & 0xC0) == 0x80);
210 const uint8 kTeredoPrefix
[] = { 0x20, 0x01, 0, 0 };
211 bool is_teredo
= std::equal(kTeredoPrefix
,
212 kTeredoPrefix
+ arraysize(kTeredoPrefix
),
219 // Provide a common macro to simplify code and readability. We must use a
220 // macro as the underlying HISTOGRAM macro creates static variables.
221 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \
222 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100)
224 // A macro to simplify code and readability.
225 #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
227 switch (priority) { \
228 case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \
229 case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \
230 case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \
231 case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \
232 case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \
233 default: NOTREACHED(); break; \
235 DNS_HISTOGRAM(basename, time); \
238 // Record time from Request creation until a valid DNS response.
239 void RecordTotalTime(bool had_dns_config
,
241 base::TimeDelta duration
) {
242 if (had_dns_config
) {
244 DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration
);
246 DNS_HISTOGRAM("AsyncDNS.TotalTime", duration
);
250 DNS_HISTOGRAM("DNS.TotalTime_speculative", duration
);
252 DNS_HISTOGRAM("DNS.TotalTime", duration
);
257 void RecordTTL(base::TimeDelta ttl
) {
258 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl
,
259 base::TimeDelta::FromSeconds(1),
260 base::TimeDelta::FromDays(1), 100);
263 bool ConfigureAsyncDnsNoFallbackFieldTrial() {
264 const bool kDefault
= false;
266 // Configure the AsyncDns field trial as follows:
267 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
268 // groups AsyncDnsA and AsyncDnsB: return false,
269 // groups SystemDnsA and SystemDnsB: return false,
270 // otherwise (trial absent): return default.
271 std::string group_name
= base::FieldTrialList::FindFullName("AsyncDns");
272 if (!group_name
.empty())
273 return StartsWithASCII(group_name
, "AsyncDnsNoFallback", false);
277 //-----------------------------------------------------------------------------
279 AddressList
EnsurePortOnAddressList(const AddressList
& list
, uint16 port
) {
280 if (list
.empty() || list
.front().port() == port
)
282 return AddressList::CopyWithPort(list
, port
);
285 // Returns true if |addresses| contains only IPv4 loopback addresses.
286 bool IsAllIPv4Loopback(const AddressList
& addresses
) {
287 for (unsigned i
= 0; i
< addresses
.size(); ++i
) {
288 const IPAddressNumber
& address
= addresses
[i
].address();
289 switch (addresses
[i
].GetFamily()) {
290 case ADDRESS_FAMILY_IPV4
:
291 if (address
[0] != 127)
294 case ADDRESS_FAMILY_IPV6
:
304 // Creates NetLog parameters when the resolve failed.
305 base::Value
* NetLogProcTaskFailedCallback(
306 uint32 attempt_number
,
309 NetLogCaptureMode
/* capture_mode */) {
310 base::DictionaryValue
* dict
= new base::DictionaryValue();
312 dict
->SetInteger("attempt_number", attempt_number
);
314 dict
->SetInteger("net_error", net_error
);
317 dict
->SetInteger("os_error", os_error
);
318 #if defined(OS_POSIX)
319 dict
->SetString("os_error_string", gai_strerror(os_error
));
320 #elif defined(OS_WIN)
321 // Map the error code to a human-readable string.
322 LPWSTR error_string
= NULL
;
323 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
324 0, // Use the internal message table.
326 0, // Use default language.
327 (LPWSTR
)&error_string
,
329 0); // Arguments (unused).
330 dict
->SetString("os_error_string", base::WideToUTF8(error_string
));
331 LocalFree(error_string
);
338 // Creates NetLog parameters when the DnsTask failed.
339 base::Value
* NetLogDnsTaskFailedCallback(int net_error
,
341 NetLogCaptureMode
/* capture_mode */) {
342 base::DictionaryValue
* dict
= new base::DictionaryValue();
343 dict
->SetInteger("net_error", net_error
);
345 dict
->SetInteger("dns_error", dns_error
);
349 // Creates NetLog parameters containing the information in a RequestInfo object,
350 // along with the associated NetLog::Source.
351 base::Value
* NetLogRequestInfoCallback(const HostResolver::RequestInfo
* info
,
352 NetLogCaptureMode
/* capture_mode */) {
353 base::DictionaryValue
* dict
= new base::DictionaryValue();
355 dict
->SetString("host", info
->host_port_pair().ToString());
356 dict
->SetInteger("address_family",
357 static_cast<int>(info
->address_family()));
358 dict
->SetBoolean("allow_cached_response", info
->allow_cached_response());
359 dict
->SetBoolean("is_speculative", info
->is_speculative());
363 // Creates NetLog parameters for the creation of a HostResolverImpl::Job.
364 base::Value
* NetLogJobCreationCallback(const NetLog::Source
& source
,
365 const std::string
* host
,
366 NetLogCaptureMode
/* capture_mode */) {
367 base::DictionaryValue
* dict
= new base::DictionaryValue();
368 source
.AddToEventParameters(dict
);
369 dict
->SetString("host", *host
);
373 // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
374 base::Value
* NetLogJobAttachCallback(const NetLog::Source
& source
,
375 RequestPriority priority
,
376 NetLogCaptureMode
/* capture_mode */) {
377 base::DictionaryValue
* dict
= new base::DictionaryValue();
378 source
.AddToEventParameters(dict
);
379 dict
->SetString("priority", RequestPriorityToString(priority
));
383 // Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
384 base::Value
* NetLogDnsConfigCallback(const DnsConfig
* config
,
385 NetLogCaptureMode
/* capture_mode */) {
386 return config
->ToValue();
389 // The logging routines are defined here because some requests are resolved
390 // without a Request object.
392 // Logs when a request has just been started.
393 void LogStartRequest(const BoundNetLog
& source_net_log
,
394 const HostResolver::RequestInfo
& info
) {
395 source_net_log
.BeginEvent(
396 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
,
397 base::Bind(&NetLogRequestInfoCallback
, &info
));
400 // Logs when a request has just completed (before its callback is run).
401 void LogFinishRequest(const BoundNetLog
& source_net_log
,
402 const HostResolver::RequestInfo
& info
,
404 source_net_log
.EndEventWithNetErrorCode(
405 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
, net_error
);
408 // Logs when a request has been cancelled.
409 void LogCancelRequest(const BoundNetLog
& source_net_log
,
410 const HostResolverImpl::RequestInfo
& info
) {
411 source_net_log
.AddEvent(NetLog::TYPE_CANCELLED
);
412 source_net_log
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
);
415 //-----------------------------------------------------------------------------
417 // Keeps track of the highest priority.
418 class PriorityTracker
{
420 explicit PriorityTracker(RequestPriority initial_priority
)
421 : highest_priority_(initial_priority
), total_count_(0) {
422 memset(counts_
, 0, sizeof(counts_
));
425 RequestPriority
highest_priority() const {
426 return highest_priority_
;
429 size_t total_count() const {
433 void Add(RequestPriority req_priority
) {
435 ++counts_
[req_priority
];
436 if (highest_priority_
< req_priority
)
437 highest_priority_
= req_priority
;
440 void Remove(RequestPriority req_priority
) {
441 DCHECK_GT(total_count_
, 0u);
442 DCHECK_GT(counts_
[req_priority
], 0u);
444 --counts_
[req_priority
];
446 for (i
= highest_priority_
; i
> MINIMUM_PRIORITY
&& !counts_
[i
]; --i
);
447 highest_priority_
= static_cast<RequestPriority
>(i
);
449 // In absence of requests, default to MINIMUM_PRIORITY.
450 if (total_count_
== 0)
451 DCHECK_EQ(MINIMUM_PRIORITY
, highest_priority_
);
455 RequestPriority highest_priority_
;
457 size_t counts_
[NUM_PRIORITIES
];
462 //-----------------------------------------------------------------------------
464 const unsigned HostResolverImpl::kMaximumDnsFailures
= 16;
466 // Holds the data for a request that could not be completed synchronously.
467 // It is owned by a Job. Canceled Requests are only marked as canceled rather
468 // than removed from the Job's |requests_| list.
469 class HostResolverImpl::Request
{
471 Request(const BoundNetLog
& source_net_log
,
472 const RequestInfo
& info
,
473 RequestPriority priority
,
474 const CompletionCallback
& callback
,
475 AddressList
* addresses
)
476 : source_net_log_(source_net_log
),
481 addresses_(addresses
),
482 request_time_(base::TimeTicks::Now()) {}
484 // Mark the request as canceled.
485 void MarkAsCanceled() {
491 bool was_canceled() const {
492 return callback_
.is_null();
495 void set_job(Job
* job
) {
497 // Identify which job the request is waiting on.
501 // Prepare final AddressList and call completion callback.
502 void OnComplete(int error
, const AddressList
& addr_list
) {
503 DCHECK(!was_canceled());
505 *addresses_
= EnsurePortOnAddressList(addr_list
, info_
.port());
506 CompletionCallback callback
= callback_
;
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 {
524 RequestPriority
priority() const { return priority_
; }
526 base::TimeTicks
request_time() const { return request_time_
; }
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.
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
> {
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
)
576 origin_loop_(base::MessageLoopProxy::current()),
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();
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.
599 DCHECK(origin_loop_
->BelongsToCurrentThread());
601 if (was_canceled() || was_completed())
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;
624 friend class base::RefCountedThreadSafe
<ProcTask
>;
627 void StartLookupAttempt() {
628 DCHECK(origin_loop_
->BelongsToCurrentThread());
629 base::TimeTicks start_time
= base::TimeTicks::Now();
631 // Dispatch the lookup attempt to a worker thread.
632 if (!base::WorkerPool::PostTask(
634 base::Bind(&ProcTask::DoLookup
, this, start_time
, attempt_number_
),
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(
643 base::Bind(&ProcTask::OnLookupComplete
, this, AddressList(),
644 start_time
, attempt_number_
, ERR_UNEXPECTED
, 0));
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(
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
) {
672 // Running on the worker thread
673 int error
= params_
.resolver_proc
->Resolve(key_
.hostname
,
675 key_
.host_resolver_flags
,
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
;
690 origin_loop_
->PostTask(
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())
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
,
712 const int os_error
) {
713 DCHECK(origin_loop_
->BelongsToCurrentThread());
714 // If results are empty, we should return an error.
715 bool empty_list_on_ok
= (error
== OK
&& results
.empty());
716 UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok
);
717 if (empty_list_on_ok
)
718 error
= ERR_NAME_NOT_RESOLVED
;
720 bool was_retry_attempt
= attempt_number
> 1;
722 // Ideally the following code would be part of host_resolver_proc.cc,
723 // however it isn't safe to call NetworkChangeNotifier from worker threads.
724 // So we do it here on the IO thread instead.
725 if (error
!= OK
&& NetworkChangeNotifier::IsOffline())
726 error
= ERR_INTERNET_DISCONNECTED
;
728 // If this is the first attempt that is finishing later, then record data
729 // for the first attempt. Won't contaminate with retry attempt's data.
730 if (!was_retry_attempt
)
731 RecordPerformanceHistograms(start_time
, error
, os_error
);
733 RecordAttemptHistograms(start_time
, attempt_number
, error
, os_error
);
738 NetLog::ParametersCallback net_log_callback
;
740 net_log_callback
= base::Bind(&NetLogProcTaskFailedCallback
,
745 net_log_callback
= NetLog::IntegerCallback("attempt_number",
748 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED
,
754 // Copy the results from the first worker thread that resolves the host.
756 completed_attempt_number_
= attempt_number
;
757 completed_attempt_error_
= error
;
759 if (was_retry_attempt
) {
760 // If retry attempt finishes before 1st attempt, then get stats on how
761 // much time is saved by having spawned an extra attempt.
762 retry_attempt_finished_time_
= base::TimeTicks::Now();
766 net_log_callback
= base::Bind(&NetLogProcTaskFailedCallback
,
769 net_log_callback
= results_
.CreateNetLogCallback();
771 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK
,
774 callback_
.Run(error
, results_
);
777 void RecordPerformanceHistograms(const base::TimeTicks
& start_time
,
779 const int os_error
) const {
780 DCHECK(origin_loop_
->BelongsToCurrentThread());
781 enum Category
{ // Used in UMA_HISTOGRAM_ENUMERATION.
784 RESOLVE_SPECULATIVE_SUCCESS
,
785 RESOLVE_SPECULATIVE_FAIL
,
786 RESOLVE_MAX
, // Bounding value.
788 int category
= RESOLVE_MAX
; // Illegal value for later DCHECK only.
790 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
792 if (had_non_speculative_request_
) {
793 category
= RESOLVE_SUCCESS
;
794 DNS_HISTOGRAM("DNS.ResolveSuccess", duration
);
796 category
= RESOLVE_SPECULATIVE_SUCCESS
;
797 DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration
);
800 // Log DNS lookups based on |address_family|. This will help us determine
801 // if IPv4 or IPv4/6 lookups are faster or slower.
802 switch(key_
.address_family
) {
803 case ADDRESS_FAMILY_IPV4
:
804 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration
);
806 case ADDRESS_FAMILY_IPV6
:
807 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration
);
809 case ADDRESS_FAMILY_UNSPECIFIED
:
810 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration
);
814 if (had_non_speculative_request_
) {
815 category
= RESOLVE_FAIL
;
816 DNS_HISTOGRAM("DNS.ResolveFail", duration
);
818 category
= RESOLVE_SPECULATIVE_FAIL
;
819 DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration
);
821 // Log DNS lookups based on |address_family|. This will help us determine
822 // if IPv4 or IPv4/6 lookups are faster or slower.
823 switch(key_
.address_family
) {
824 case ADDRESS_FAMILY_IPV4
:
825 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration
);
827 case ADDRESS_FAMILY_IPV6
:
828 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration
);
830 case ADDRESS_FAMILY_UNSPECIFIED
:
831 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration
);
834 UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName
,
836 GetAllGetAddrinfoOSErrors());
838 DCHECK_LT(category
, static_cast<int>(RESOLVE_MAX
)); // Be sure it was set.
840 UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category
, RESOLVE_MAX
);
843 void RecordAttemptHistograms(const base::TimeTicks
& start_time
,
844 const uint32 attempt_number
,
846 const int os_error
) const {
847 DCHECK(origin_loop_
->BelongsToCurrentThread());
848 bool first_attempt_to_complete
=
849 completed_attempt_number_
== attempt_number
;
850 bool is_first_attempt
= (attempt_number
== 1);
852 if (first_attempt_to_complete
) {
853 // If this was first attempt to complete, then record the resolution
854 // status of the attempt.
855 if (completed_attempt_error_
== OK
) {
856 UMA_HISTOGRAM_ENUMERATION(
857 "DNS.AttemptFirstSuccess", attempt_number
, 100);
859 UMA_HISTOGRAM_ENUMERATION(
860 "DNS.AttemptFirstFailure", attempt_number
, 100);
865 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number
, 100);
867 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number
, 100);
869 // If first attempt didn't finish before retry attempt, then calculate stats
870 // on how much time is saved by having spawned an extra attempt.
871 if (!first_attempt_to_complete
&& is_first_attempt
&& !was_canceled()) {
872 DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry",
873 base::TimeTicks::Now() - retry_attempt_finished_time_
);
876 if (was_canceled() || !first_attempt_to_complete
) {
877 // Count those attempts which completed after the job was already canceled
878 // OR after the job was already completed by an earlier attempt (so in
880 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number
, 100);
882 // Record if job is canceled.
884 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number
, 100);
887 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
889 DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration
);
891 DNS_HISTOGRAM("DNS.AttemptFailDuration", duration
);
894 // Set on the origin thread, read on the worker thread.
897 // Holds an owning reference to the HostResolverProc that we are going to use.
898 // This may not be the current resolver procedure by the time we call
899 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
900 // reference ensures that it remains valid until we are done.
901 ProcTaskParams params_
;
903 // The listener to the results of this ProcTask.
906 // Used to post ourselves onto the origin thread.
907 scoped_refptr
<base::MessageLoopProxy
> origin_loop_
;
909 // Keeps track of the number of attempts we have made so far to resolve the
910 // host. Whenever we start an attempt to resolve the host, we increase this
912 uint32 attempt_number_
;
914 // The index of the attempt which finished first (or 0 if the job is still in
916 uint32 completed_attempt_number_
;
918 // The result (a net error code) from the first attempt to complete.
919 int completed_attempt_error_
;
921 // The time when retry attempt was finished.
922 base::TimeTicks retry_attempt_finished_time_
;
924 // True if a non-speculative request was ever attached to this job
925 // (regardless of whether or not it was later canceled.
926 // This boolean is used for histogramming the duration of jobs used to
927 // service non-speculative requests.
928 bool had_non_speculative_request_
;
930 AddressList results_
;
932 BoundNetLog net_log_
;
934 DISALLOW_COPY_AND_ASSIGN(ProcTask
);
937 //-----------------------------------------------------------------------------
939 // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as
940 // it takes 40-100ms and should not block initialization.
941 class HostResolverImpl::LoopbackProbeJob
{
943 explicit LoopbackProbeJob(const base::WeakPtr
<HostResolverImpl
>& resolver
)
944 : resolver_(resolver
),
946 DCHECK(resolver
.get());
947 const bool kIsSlow
= true;
948 base::WorkerPool::PostTaskAndReply(
950 base::Bind(&LoopbackProbeJob::DoProbe
, base::Unretained(this)),
951 base::Bind(&LoopbackProbeJob::OnProbeComplete
, base::Owned(this)),
955 virtual ~LoopbackProbeJob() {}
958 // Runs on worker thread.
960 result_
= HaveOnlyLoopbackAddresses();
963 void OnProbeComplete() {
964 if (!resolver_
.get())
966 resolver_
->SetHaveOnlyLoopbackAddresses(result_
);
969 // Used/set only on origin thread.
970 base::WeakPtr
<HostResolverImpl
> resolver_
;
974 DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob
);
977 //-----------------------------------------------------------------------------
979 // Resolves the hostname using DnsTransaction.
980 // TODO(szym): This could be moved to separate source file as well.
981 class HostResolverImpl::DnsTask
: public base::SupportsWeakPtr
<DnsTask
> {
985 virtual void OnDnsTaskComplete(base::TimeTicks start_time
,
987 const AddressList
& addr_list
,
988 base::TimeDelta ttl
) = 0;
990 // Called when the first of two jobs succeeds. If the first completed
991 // transaction fails, this is not called. Also not called when the DnsTask
992 // only needs to run one transaction.
993 virtual void OnFirstDnsTransactionComplete() = 0;
997 virtual ~Delegate() {}
1000 DnsTask(DnsClient
* client
,
1003 const BoundNetLog
& job_net_log
)
1006 delegate_(delegate
),
1007 net_log_(job_net_log
),
1008 num_completed_transactions_(0),
1009 task_start_time_(base::TimeTicks::Now()) {
1014 bool needs_two_transactions() const {
1015 return key_
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
;
1018 bool needs_another_transaction() const {
1019 return needs_two_transactions() && !transaction_aaaa_
;
1022 void StartFirstTransaction() {
1023 DCHECK_EQ(0u, num_completed_transactions_
);
1024 net_log_
.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
);
1025 if (key_
.address_family
== ADDRESS_FAMILY_IPV6
) {
1032 void StartSecondTransaction() {
1033 DCHECK(needs_two_transactions());
1039 DCHECK(!transaction_a_
);
1040 DCHECK_NE(ADDRESS_FAMILY_IPV6
, key_
.address_family
);
1041 transaction_a_
= CreateTransaction(ADDRESS_FAMILY_IPV4
);
1042 transaction_a_
->Start();
1046 DCHECK(!transaction_aaaa_
);
1047 DCHECK_NE(ADDRESS_FAMILY_IPV4
, key_
.address_family
);
1048 transaction_aaaa_
= CreateTransaction(ADDRESS_FAMILY_IPV6
);
1049 transaction_aaaa_
->Start();
1052 scoped_ptr
<DnsTransaction
> CreateTransaction(AddressFamily family
) {
1053 DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED
, family
);
1054 return client_
->GetTransactionFactory()->CreateTransaction(
1056 family
== ADDRESS_FAMILY_IPV6
? dns_protocol::kTypeAAAA
:
1057 dns_protocol::kTypeA
,
1058 base::Bind(&DnsTask::OnTransactionComplete
, base::Unretained(this),
1059 base::TimeTicks::Now()),
1063 void OnTransactionComplete(const base::TimeTicks
& start_time
,
1064 DnsTransaction
* transaction
,
1066 const DnsResponse
* response
) {
1067 DCHECK(transaction
);
1068 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
1069 if (net_error
!= OK
) {
1070 DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration
);
1071 OnFailure(net_error
, DnsResponse::DNS_PARSE_OK
);
1075 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration
);
1076 switch (transaction
->GetType()) {
1077 case dns_protocol::kTypeA
:
1078 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration
);
1080 case dns_protocol::kTypeAAAA
:
1081 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration
);
1085 AddressList addr_list
;
1086 base::TimeDelta ttl
;
1087 DnsResponse::Result result
= response
->ParseToAddressList(&addr_list
, &ttl
);
1088 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
1090 DnsResponse::DNS_PARSE_RESULT_MAX
);
1091 if (result
!= DnsResponse::DNS_PARSE_OK
) {
1092 // Fail even if the other query succeeds.
1093 OnFailure(ERR_DNS_MALFORMED_RESPONSE
, result
);
1097 ++num_completed_transactions_
;
1098 if (num_completed_transactions_
== 1) {
1101 ttl_
= std::min(ttl_
, ttl
);
1104 if (transaction
->GetType() == dns_protocol::kTypeA
) {
1105 DCHECK_EQ(transaction_a_
.get(), transaction
);
1106 // Place IPv4 addresses after IPv6.
1107 addr_list_
.insert(addr_list_
.end(), addr_list
.begin(), addr_list
.end());
1109 DCHECK_EQ(transaction_aaaa_
.get(), transaction
);
1110 // Place IPv6 addresses before IPv4.
1111 addr_list_
.insert(addr_list_
.begin(), addr_list
.begin(), addr_list
.end());
1114 if (needs_two_transactions() && num_completed_transactions_
== 1) {
1115 // No need to repeat the suffix search.
1116 key_
.hostname
= transaction
->GetHostname();
1117 delegate_
->OnFirstDnsTransactionComplete();
1121 if (addr_list_
.empty()) {
1122 // TODO(szym): Don't fallback to ProcTask in this case.
1123 OnFailure(ERR_NAME_NOT_RESOLVED
, DnsResponse::DNS_PARSE_OK
);
1127 // If there are multiple addresses, and at least one is IPv6, need to sort
1128 // them. Note that IPv6 addresses are always put before IPv4 ones, so it's
1129 // sufficient to just check the family of the first address.
1130 if (addr_list_
.size() > 1 &&
1131 addr_list_
[0].GetFamily() == ADDRESS_FAMILY_IPV6
) {
1132 // Sort addresses if needed. Sort could complete synchronously.
1133 client_
->GetAddressSorter()->Sort(
1135 base::Bind(&DnsTask::OnSortComplete
,
1137 base::TimeTicks::Now()));
1139 OnSuccess(addr_list_
);
1143 void OnSortComplete(base::TimeTicks start_time
,
1145 const AddressList
& addr_list
) {
1147 DNS_HISTOGRAM("AsyncDNS.SortFailure",
1148 base::TimeTicks::Now() - start_time
);
1149 OnFailure(ERR_DNS_SORT_ERROR
, DnsResponse::DNS_PARSE_OK
);
1153 DNS_HISTOGRAM("AsyncDNS.SortSuccess",
1154 base::TimeTicks::Now() - start_time
);
1156 // AddressSorter prunes unusable destinations.
1157 if (addr_list
.empty()) {
1158 LOG(WARNING
) << "Address list empty after RFC3484 sort";
1159 OnFailure(ERR_NAME_NOT_RESOLVED
, DnsResponse::DNS_PARSE_OK
);
1163 OnSuccess(addr_list
);
1166 void OnFailure(int net_error
, DnsResponse::Result result
) {
1167 DCHECK_NE(OK
, net_error
);
1169 NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
,
1170 base::Bind(&NetLogDnsTaskFailedCallback
, net_error
, result
));
1171 delegate_
->OnDnsTaskComplete(task_start_time_
, net_error
, AddressList(),
1175 void OnSuccess(const AddressList
& addr_list
) {
1176 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
,
1177 addr_list
.CreateNetLogCallback());
1178 delegate_
->OnDnsTaskComplete(task_start_time_
, OK
, addr_list
, ttl_
);
1184 // The listener to the results of this DnsTask.
1185 Delegate
* delegate_
;
1186 const BoundNetLog net_log_
;
1188 scoped_ptr
<DnsTransaction
> transaction_a_
;
1189 scoped_ptr
<DnsTransaction
> transaction_aaaa_
;
1191 unsigned num_completed_transactions_
;
1193 // These are updated as each transaction completes.
1194 base::TimeDelta ttl_
;
1195 // IPv6 addresses must appear first in the list.
1196 AddressList addr_list_
;
1198 base::TimeTicks task_start_time_
;
1200 DISALLOW_COPY_AND_ASSIGN(DnsTask
);
1203 //-----------------------------------------------------------------------------
1205 // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch.
1206 class HostResolverImpl::Job
: public PrioritizedDispatcher::Job
,
1207 public HostResolverImpl::DnsTask::Delegate
{
1209 // Creates new job for |key| where |request_net_log| is bound to the
1210 // request that spawned it.
1211 Job(const base::WeakPtr
<HostResolverImpl
>& resolver
,
1213 RequestPriority priority
,
1214 const BoundNetLog
& source_net_log
)
1215 : resolver_(resolver
),
1217 priority_tracker_(priority
),
1218 had_non_speculative_request_(false),
1219 had_dns_config_(false),
1220 num_occupied_job_slots_(0),
1221 dns_task_error_(OK
),
1222 creation_time_(base::TimeTicks::Now()),
1223 priority_change_time_(creation_time_
),
1224 net_log_(BoundNetLog::Make(source_net_log
.net_log(),
1225 NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB
)) {
1226 source_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB
);
1228 net_log_
.BeginEvent(
1229 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1230 base::Bind(&NetLogJobCreationCallback
,
1231 source_net_log
.source(),
1237 // |resolver_| was destroyed with this Job still in flight.
1238 // Clean-up, record in the log, but don't run any callbacks.
1239 if (is_proc_running()) {
1240 proc_task_
->Cancel();
1243 // Clean up now for nice NetLog.
1245 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1247 } else if (is_queued()) {
1248 // |resolver_| was destroyed without running this Job.
1249 // TODO(szym): is there any benefit in having this distinction?
1250 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
);
1251 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
);
1253 // else CompleteRequests logged EndEvent.
1255 // Log any remaining Requests as cancelled.
1256 for (RequestsList::const_iterator it
= requests_
.begin();
1257 it
!= requests_
.end(); ++it
) {
1259 if (req
->was_canceled())
1261 DCHECK_EQ(this, req
->job());
1262 LogCancelRequest(req
->source_net_log(), req
->info());
1266 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1268 void Schedule(bool at_head
) {
1269 DCHECK(!is_queued());
1270 PrioritizedDispatcher::Handle handle
;
1272 handle
= resolver_
->dispatcher_
->Add(this, priority());
1274 handle
= resolver_
->dispatcher_
->AddAtHead(this, priority());
1276 // The dispatcher could have started |this| in the above call to Add, which
1277 // could have called Schedule again. In that case |handle| will be null,
1278 // but |handle_| may have been set by the other nested call to Schedule.
1279 if (!handle
.is_null()) {
1280 DCHECK(handle_
.is_null());
1285 void AddRequest(scoped_ptr
<Request
> req
) {
1286 // .localhost queries are redirected to "localhost." to make sure
1287 // that they are never sent out on the network, per RFC 6761.
1288 if (IsLocalhostTLD(req
->info().hostname())) {
1289 DCHECK_EQ(key_
.hostname
, kLocalhost
);
1291 DCHECK_EQ(key_
.hostname
, req
->info().hostname());
1295 priority_tracker_
.Add(req
->priority());
1297 req
->source_net_log().AddEvent(
1298 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH
,
1299 net_log_
.source().ToEventParametersCallback());
1302 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH
,
1303 base::Bind(&NetLogJobAttachCallback
,
1304 req
->source_net_log().source(),
1307 // TODO(szym): Check if this is still needed.
1308 if (!req
->info().is_speculative()) {
1309 had_non_speculative_request_
= true;
1310 if (proc_task_
.get())
1311 proc_task_
->set_had_non_speculative_request();
1314 requests_
.push_back(req
.release());
1319 // Marks |req| as cancelled. If it was the last active Request, also finishes
1320 // this Job, marking it as cancelled, and deletes it.
1321 void CancelRequest(Request
* req
) {
1322 DCHECK_EQ(key_
.hostname
, req
->info().hostname());
1323 DCHECK(!req
->was_canceled());
1325 // Don't remove it from |requests_| just mark it canceled.
1326 req
->MarkAsCanceled();
1327 LogCancelRequest(req
->source_net_log(), req
->info());
1329 priority_tracker_
.Remove(req
->priority());
1330 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH
,
1331 base::Bind(&NetLogJobAttachCallback
,
1332 req
->source_net_log().source(),
1335 if (num_active_requests() > 0) {
1338 // If we were called from a Request's callback within CompleteRequests,
1339 // that Request could not have been cancelled, so num_active_requests()
1340 // could not be 0. Therefore, we are not in CompleteRequests().
1341 CompleteRequestsWithError(OK
/* cancelled */);
1345 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1346 // the job. This currently assumes the abort is due to a network change.
1348 DCHECK(is_running());
1349 CompleteRequestsWithError(ERR_NETWORK_CHANGED
);
1352 // If DnsTask present, abort it and fall back to ProcTask.
1353 void AbortDnsTask() {
1356 dns_task_error_
= OK
;
1361 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1362 // Completes all requests and destroys the job.
1364 DCHECK(!is_running());
1365 DCHECK(is_queued());
1368 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED
);
1370 // This signals to CompleteRequests that this job never ran.
1371 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
);
1374 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1375 // this Job was destroyed.
1376 bool ServeFromHosts() {
1377 DCHECK_GT(num_active_requests(), 0u);
1378 AddressList addr_list
;
1379 if (resolver_
->ServeFromHosts(key(),
1380 requests_
.front()->info(),
1382 // This will destroy the Job.
1384 HostCache::Entry(OK
, MakeAddressListForRequest(addr_list
)),
1391 const Key
key() const {
1395 bool is_queued() const {
1396 return !handle_
.is_null();
1399 bool is_running() const {
1400 return is_dns_running() || is_proc_running();
1404 void KillDnsTask() {
1406 ReduceToOneJobSlot();
1411 // Reduce the number of job slots occupied and queued in the dispatcher
1412 // to one. If the second Job slot is queued in the dispatcher, cancels the
1413 // queued job. Otherwise, the second Job has been started by the
1414 // PrioritizedDispatcher, so signals it is complete.
1415 void ReduceToOneJobSlot() {
1416 DCHECK_GE(num_occupied_job_slots_
, 1u);
1418 resolver_
->dispatcher_
->Cancel(handle_
);
1420 } else if (num_occupied_job_slots_
> 1) {
1421 resolver_
->dispatcher_
->OnJobFinished();
1422 --num_occupied_job_slots_
;
1424 DCHECK_EQ(1u, num_occupied_job_slots_
);
1427 void UpdatePriority() {
1429 if (priority() != static_cast<RequestPriority
>(handle_
.priority()))
1430 priority_change_time_
= base::TimeTicks::Now();
1431 handle_
= resolver_
->dispatcher_
->ChangePriority(handle_
, priority());
1435 AddressList
MakeAddressListForRequest(const AddressList
& list
) const {
1436 if (requests_
.empty())
1438 return AddressList::CopyWithPort(list
, requests_
.front()->info().port());
1441 // PriorityDispatch::Job:
1442 void Start() override
{
1443 DCHECK_LE(num_occupied_job_slots_
, 1u);
1446 ++num_occupied_job_slots_
;
1448 if (num_occupied_job_slots_
== 2) {
1449 StartSecondDnsTransaction();
1453 DCHECK(!is_running());
1455 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED
);
1457 had_dns_config_
= resolver_
->HaveDnsConfig();
1459 base::TimeTicks now
= base::TimeTicks::Now();
1460 base::TimeDelta queue_time
= now
- creation_time_
;
1461 base::TimeDelta queue_time_after_change
= now
- priority_change_time_
;
1463 if (had_dns_config_
) {
1464 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(),
1466 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(),
1467 queue_time_after_change
);
1469 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time
);
1470 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(),
1471 queue_time_after_change
);
1475 (key_
.host_resolver_flags
& HOST_RESOLVER_SYSTEM_ONLY
) != 0;
1477 // Caution: Job::Start must not complete synchronously.
1478 if (!system_only
&& had_dns_config_
&&
1479 !ResemblesMulticastDNSName(key_
.hostname
)) {
1486 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
1487 // the limits on |dispatcher_|. But in order to keep the number of WorkerPool
1488 // threads low, we will need to use an "inner" PrioritizedDispatcher with
1490 void StartProcTask() {
1491 DCHECK(!is_dns_running());
1492 proc_task_
= new ProcTask(
1494 resolver_
->proc_params_
,
1495 base::Bind(&Job::OnProcTaskComplete
, base::Unretained(this),
1496 base::TimeTicks::Now()),
1499 if (had_non_speculative_request_
)
1500 proc_task_
->set_had_non_speculative_request();
1501 // Start() could be called from within Resolve(), hence it must NOT directly
1502 // call OnProcTaskComplete, for example, on synchronous failure.
1503 proc_task_
->Start();
1506 // Called by ProcTask when it completes.
1507 void OnProcTaskComplete(base::TimeTicks start_time
,
1509 const AddressList
& addr_list
) {
1510 DCHECK(is_proc_running());
1512 if (!resolver_
->resolved_known_ipv6_hostname_
&&
1514 key_
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
1515 if (key_
.hostname
== "www.google.com") {
1516 resolver_
->resolved_known_ipv6_hostname_
= true;
1517 bool got_ipv6_address
= false;
1518 for (size_t i
= 0; i
< addr_list
.size(); ++i
) {
1519 if (addr_list
[i
].GetFamily() == ADDRESS_FAMILY_IPV6
) {
1520 got_ipv6_address
= true;
1524 UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address
);
1528 if (dns_task_error_
!= OK
) {
1529 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
1530 if (net_error
== OK
) {
1531 DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration
);
1532 if ((dns_task_error_
== ERR_NAME_NOT_RESOLVED
) &&
1533 ResemblesNetBIOSName(key_
.hostname
)) {
1534 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS
);
1536 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS
);
1538 UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.ResolveError",
1539 std::abs(dns_task_error_
),
1540 GetAllErrorCodesForUma());
1541 resolver_
->OnDnsTaskResolve(dns_task_error_
);
1543 DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration
);
1544 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL
);
1548 base::TimeDelta ttl
=
1549 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds
);
1550 if (net_error
== OK
)
1551 ttl
= base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds
);
1553 // Don't store the |ttl| in cache since it's not obtained from the server.
1555 HostCache::Entry(net_error
, MakeAddressListForRequest(addr_list
)),
1559 void StartDnsTask() {
1560 DCHECK(resolver_
->HaveDnsConfig());
1561 dns_task_
.reset(new DnsTask(resolver_
->dns_client_
.get(), key_
, this,
1564 dns_task_
->StartFirstTransaction();
1565 // Schedule a second transaction, if needed.
1566 if (dns_task_
->needs_two_transactions())
1570 void StartSecondDnsTransaction() {
1571 DCHECK(dns_task_
->needs_two_transactions());
1572 dns_task_
->StartSecondTransaction();
1575 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
1576 // deleted before this callback. In this case dns_task is deleted as well,
1577 // so we use it as indicator whether Job is still valid.
1578 void OnDnsTaskFailure(const base::WeakPtr
<DnsTask
>& dns_task
,
1579 base::TimeDelta duration
,
1581 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration
);
1583 if (dns_task
== NULL
)
1586 dns_task_error_
= net_error
;
1588 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so.
1589 // http://crbug.com/117655
1591 // TODO(szym): Some net errors indicate lack of connectivity. Starting
1592 // ProcTask in that case is a waste of time.
1593 if (resolver_
->fallback_to_proctask_
) {
1597 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL
);
1598 CompleteRequestsWithError(net_error
);
1603 // HostResolverImpl::DnsTask::Delegate implementation:
1605 void OnDnsTaskComplete(base::TimeTicks start_time
,
1607 const AddressList
& addr_list
,
1608 base::TimeDelta ttl
) override
{
1609 DCHECK(is_dns_running());
1611 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
1612 if (net_error
!= OK
) {
1613 OnDnsTaskFailure(dns_task_
->AsWeakPtr(), duration
, net_error
);
1616 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration
);
1617 // Log DNS lookups based on |address_family|.
1618 switch(key_
.address_family
) {
1619 case ADDRESS_FAMILY_IPV4
:
1620 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration
);
1622 case ADDRESS_FAMILY_IPV6
:
1623 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration
);
1625 case ADDRESS_FAMILY_UNSPECIFIED
:
1626 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration
);
1630 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS
);
1633 resolver_
->OnDnsTaskResolve(OK
);
1635 base::TimeDelta bounded_ttl
=
1636 std::max(ttl
, base::TimeDelta::FromSeconds(kMinimumTTLSeconds
));
1639 HostCache::Entry(net_error
, MakeAddressListForRequest(addr_list
), ttl
),
1643 void OnFirstDnsTransactionComplete() override
{
1644 DCHECK(dns_task_
->needs_two_transactions());
1645 DCHECK_EQ(dns_task_
->needs_another_transaction(), is_queued());
1646 // No longer need to occupy two dispatcher slots.
1647 ReduceToOneJobSlot();
1649 // We already have a job slot at the dispatcher, so if the second
1650 // transaction hasn't started, reuse it now instead of waiting in the queue
1651 // for the second slot.
1652 if (dns_task_
->needs_another_transaction())
1653 dns_task_
->StartSecondTransaction();
1656 // Performs Job's last rites. Completes all Requests. Deletes this.
1657 void CompleteRequests(const HostCache::Entry
& entry
,
1658 base::TimeDelta ttl
) {
1659 CHECK(resolver_
.get());
1661 // This job must be removed from resolver's |jobs_| now to make room for a
1662 // new job with the same key in case one of the OnComplete callbacks decides
1663 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1665 scoped_ptr
<Job
> self_deleter(this);
1667 resolver_
->RemoveJob(this);
1670 if (is_proc_running()) {
1671 DCHECK(!is_queued());
1672 proc_task_
->Cancel();
1677 // Signal dispatcher that a slot has opened.
1678 resolver_
->dispatcher_
->OnJobFinished();
1679 } else if (is_queued()) {
1680 resolver_
->dispatcher_
->Cancel(handle_
);
1684 if (num_active_requests() == 0) {
1685 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
);
1686 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1691 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1694 DCHECK(!requests_
.empty());
1696 if (entry
.error
== OK
) {
1697 // Record this histogram here, when we know the system has a valid DNS
1699 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1700 resolver_
->received_dns_config_
);
1703 bool did_complete
= (entry
.error
!= ERR_NETWORK_CHANGED
) &&
1704 (entry
.error
!= ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
);
1706 resolver_
->CacheResult(key_
, entry
, ttl
);
1708 // Complete all of the requests that were attached to the job.
1709 for (RequestsList::const_iterator it
= requests_
.begin();
1710 it
!= requests_
.end(); ++it
) {
1713 if (req
->was_canceled())
1716 DCHECK_EQ(this, req
->job());
1717 // Update the net log and notify registered observers.
1718 LogFinishRequest(req
->source_net_log(), req
->info(), entry
.error
);
1720 // Record effective total time from creation to completion.
1721 RecordTotalTime(had_dns_config_
, req
->info().is_speculative(),
1722 base::TimeTicks::Now() - req
->request_time());
1724 req
->OnComplete(entry
.error
, entry
.addrlist
);
1726 // Check if the resolver was destroyed as a result of running the
1727 // callback. If it was, we could continue, but we choose to bail.
1728 if (!resolver_
.get())
1733 // Convenience wrapper for CompleteRequests in case of failure.
1734 void CompleteRequestsWithError(int net_error
) {
1735 CompleteRequests(HostCache::Entry(net_error
, AddressList()),
1739 RequestPriority
priority() const {
1740 return priority_tracker_
.highest_priority();
1743 // Number of non-canceled requests in |requests_|.
1744 size_t num_active_requests() const {
1745 return priority_tracker_
.total_count();
1748 bool is_dns_running() const {
1749 return dns_task_
.get() != NULL
;
1752 bool is_proc_running() const {
1753 return proc_task_
.get() != NULL
;
1756 base::WeakPtr
<HostResolverImpl
> resolver_
;
1760 // Tracks the highest priority across |requests_|.
1761 PriorityTracker priority_tracker_
;
1763 bool had_non_speculative_request_
;
1765 // Distinguishes measurements taken while DnsClient was fully configured.
1766 bool had_dns_config_
;
1768 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1769 unsigned num_occupied_job_slots_
;
1771 // Result of DnsTask.
1772 int dns_task_error_
;
1774 const base::TimeTicks creation_time_
;
1775 base::TimeTicks priority_change_time_
;
1777 BoundNetLog net_log_
;
1779 // Resolves the host using a HostResolverProc.
1780 scoped_refptr
<ProcTask
> proc_task_
;
1782 // Resolves the host using a DnsTransaction.
1783 scoped_ptr
<DnsTask
> dns_task_
;
1785 // All Requests waiting for the result of this Job. Some can be canceled.
1786 RequestsList requests_
;
1788 // A handle used in |HostResolverImpl::dispatcher_|.
1789 PrioritizedDispatcher::Handle handle_
;
1792 //-----------------------------------------------------------------------------
1794 HostResolverImpl::ProcTaskParams::ProcTaskParams(
1795 HostResolverProc
* resolver_proc
,
1796 size_t max_retry_attempts
)
1797 : resolver_proc(resolver_proc
),
1798 max_retry_attempts(max_retry_attempts
),
1799 unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)),
1801 // Maximum of 4 retry attempts for host resolution.
1802 static const size_t kDefaultMaxRetryAttempts
= 4u;
1803 if (max_retry_attempts
== HostResolver::kDefaultRetryAttempts
)
1804 max_retry_attempts
= kDefaultMaxRetryAttempts
;
1807 HostResolverImpl::ProcTaskParams::~ProcTaskParams() {}
1809 HostResolverImpl::HostResolverImpl(const Options
& options
, NetLog
* net_log
)
1810 : max_queued_jobs_(0),
1811 proc_params_(NULL
, options
.max_retry_attempts
),
1813 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED
),
1814 received_dns_config_(false),
1815 num_dns_failures_(0),
1816 probe_ipv6_support_(true),
1817 use_local_ipv6_(false),
1818 resolved_known_ipv6_hostname_(false),
1819 additional_resolver_flags_(0),
1820 fallback_to_proctask_(true),
1821 weak_ptr_factory_(this),
1822 probe_weak_ptr_factory_(this) {
1823 if (options
.enable_caching
)
1824 cache_
= HostCache::CreateDefaultCache();
1826 PrioritizedDispatcher::Limits job_limits
= options
.GetDispatcherLimits();
1827 dispatcher_
.reset(new PrioritizedDispatcher(job_limits
));
1828 max_queued_jobs_
= job_limits
.total_jobs
* 100u;
1830 DCHECK_GE(dispatcher_
->num_priorities(), static_cast<size_t>(NUM_PRIORITIES
));
1833 EnsureWinsockInit();
1835 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
1836 new LoopbackProbeJob(weak_ptr_factory_
.GetWeakPtr());
1838 NetworkChangeNotifier::AddIPAddressObserver(this);
1839 NetworkChangeNotifier::AddDNSObserver(this);
1840 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
1841 !defined(OS_ANDROID)
1842 EnsureDnsReloaderInit();
1846 DnsConfig dns_config
;
1847 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
1848 received_dns_config_
= dns_config
.IsValid();
1849 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
1850 use_local_ipv6_
= !dns_config
.IsValid() || dns_config
.use_local_ipv6
;
1853 fallback_to_proctask_
= !ConfigureAsyncDnsNoFallbackFieldTrial();
1856 HostResolverImpl::~HostResolverImpl() {
1857 // Prevent the dispatcher from starting new jobs.
1858 dispatcher_
->SetLimitsToZero();
1859 // It's now safe for Jobs to call KillDsnTask on destruction, because
1860 // OnJobComplete will not start any new jobs.
1861 STLDeleteValues(&jobs_
);
1863 NetworkChangeNotifier::RemoveIPAddressObserver(this);
1864 NetworkChangeNotifier::RemoveDNSObserver(this);
1867 void HostResolverImpl::SetMaxQueuedJobs(size_t value
) {
1868 DCHECK_EQ(0u, dispatcher_
->num_queued_jobs());
1869 DCHECK_GT(value
, 0u);
1870 max_queued_jobs_
= value
;
1873 int HostResolverImpl::Resolve(const RequestInfo
& info
,
1874 RequestPriority priority
,
1875 AddressList
* addresses
,
1876 const CompletionCallback
& callback
,
1877 RequestHandle
* out_req
,
1878 const BoundNetLog
& source_net_log
) {
1880 DCHECK(CalledOnValidThread());
1881 DCHECK_EQ(false, callback
.is_null());
1883 // Check that the caller supplied a valid hostname to resolve.
1884 std::string labeled_hostname
;
1885 if (!DNSDomainFromDot(info
.hostname(), &labeled_hostname
))
1886 return ERR_NAME_NOT_RESOLVED
;
1888 LogStartRequest(source_net_log
, info
);
1890 IPAddressNumber ip_number
;
1891 IPAddressNumber
* ip_number_ptr
= nullptr;
1892 if (ParseIPLiteralToNumber(info
.hostname(), &ip_number
))
1893 ip_number_ptr
= &ip_number
;
1895 // Build a key that identifies the request in the cache and in the
1896 // outstanding jobs map.
1897 Key key
= GetEffectiveKeyForRequest(info
, ip_number_ptr
, source_net_log
);
1899 int rv
= ResolveHelper(key
, info
, ip_number_ptr
, addresses
, source_net_log
);
1900 if (rv
!= ERR_DNS_CACHE_MISS
) {
1901 LogFinishRequest(source_net_log
, info
, rv
);
1902 RecordTotalTime(HaveDnsConfig(), info
.is_speculative(), base::TimeDelta());
1906 // Next we need to attach our request to a "job". This job is responsible for
1907 // calling "getaddrinfo(hostname)" on a worker thread.
1909 JobMap::iterator jobit
= jobs_
.find(key
);
1911 if (jobit
== jobs_
.end()) {
1913 new Job(weak_ptr_factory_
.GetWeakPtr(), key
, priority
, source_net_log
);
1914 job
->Schedule(false);
1916 // Check for queue overflow.
1917 if (dispatcher_
->num_queued_jobs() > max_queued_jobs_
) {
1918 Job
* evicted
= static_cast<Job
*>(dispatcher_
->EvictOldestLowest());
1920 evicted
->OnEvicted(); // Deletes |evicted|.
1921 if (evicted
== job
) {
1922 rv
= ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
;
1923 LogFinishRequest(source_net_log
, info
, rv
);
1927 jobs_
.insert(jobit
, std::make_pair(key
, job
));
1929 job
= jobit
->second
;
1932 // Can't complete synchronously. Create and attach request.
1933 scoped_ptr
<Request
> req(new Request(
1934 source_net_log
, info
, priority
, callback
, addresses
));
1936 *out_req
= reinterpret_cast<RequestHandle
>(req
.get());
1938 job
->AddRequest(req
.Pass());
1939 // Completion happens during Job::CompleteRequests().
1940 return ERR_IO_PENDING
;
1943 int HostResolverImpl::ResolveHelper(const Key
& key
,
1944 const RequestInfo
& info
,
1945 const IPAddressNumber
* ip_number
,
1946 AddressList
* addresses
,
1947 const BoundNetLog
& source_net_log
) {
1948 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
1949 // On Windows it gives the default interface's address, whereas on Linux it
1950 // gives an error. We will make it fail on all platforms for consistency.
1951 if (info
.hostname().empty() || info
.hostname().size() > kMaxHostLength
)
1952 return ERR_NAME_NOT_RESOLVED
;
1954 int net_error
= ERR_UNEXPECTED
;
1955 if (ResolveAsIP(key
, info
, ip_number
, &net_error
, addresses
))
1957 if (ServeFromCache(key
, info
, &net_error
, addresses
)) {
1958 source_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT
);
1961 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
1962 // http://crbug.com/117655
1963 if (ServeFromHosts(key
, info
, addresses
)) {
1964 source_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT
);
1967 return ERR_DNS_CACHE_MISS
;
1970 int HostResolverImpl::ResolveFromCache(const RequestInfo
& info
,
1971 AddressList
* addresses
,
1972 const BoundNetLog
& source_net_log
) {
1973 DCHECK(CalledOnValidThread());
1976 // Update the net log and notify registered observers.
1977 LogStartRequest(source_net_log
, info
);
1979 IPAddressNumber ip_number
;
1980 IPAddressNumber
* ip_number_ptr
= nullptr;
1981 if (ParseIPLiteralToNumber(info
.hostname(), &ip_number
))
1982 ip_number_ptr
= &ip_number
;
1984 Key key
= GetEffectiveKeyForRequest(info
, ip_number_ptr
, source_net_log
);
1986 int rv
= ResolveHelper(key
, info
, ip_number_ptr
, addresses
, source_net_log
);
1987 LogFinishRequest(source_net_log
, info
, rv
);
1991 void HostResolverImpl::CancelRequest(RequestHandle req_handle
) {
1992 DCHECK(CalledOnValidThread());
1993 Request
* req
= reinterpret_cast<Request
*>(req_handle
);
1995 Job
* job
= req
->job();
1997 job
->CancelRequest(req
);
2000 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family
) {
2001 DCHECK(CalledOnValidThread());
2002 default_address_family_
= address_family
;
2003 probe_ipv6_support_
= false;
2006 AddressFamily
HostResolverImpl::GetDefaultAddressFamily() const {
2007 return default_address_family_
;
2010 void HostResolverImpl::SetDnsClientEnabled(bool enabled
) {
2011 DCHECK(CalledOnValidThread());
2012 #if defined(ENABLE_BUILT_IN_DNS)
2013 if (enabled
&& !dns_client_
) {
2014 SetDnsClient(DnsClient::CreateClient(net_log_
));
2015 } else if (!enabled
&& dns_client_
) {
2016 SetDnsClient(scoped_ptr
<DnsClient
>());
2021 HostCache
* HostResolverImpl::GetHostCache() {
2022 return cache_
.get();
2025 base::Value
* HostResolverImpl::GetDnsConfigAsValue() const {
2026 // Check if async DNS is disabled.
2027 if (!dns_client_
.get())
2030 // Check if async DNS is enabled, but we currently have no configuration
2032 const DnsConfig
* dns_config
= dns_client_
->GetConfig();
2033 if (dns_config
== NULL
)
2034 return new base::DictionaryValue();
2036 return dns_config
->ToValue();
2039 bool HostResolverImpl::ResolveAsIP(const Key
& key
,
2040 const RequestInfo
& info
,
2041 const IPAddressNumber
* ip_number
,
2043 AddressList
* addresses
) {
2046 if (ip_number
== nullptr)
2049 DCHECK_EQ(key
.host_resolver_flags
&
2050 ~(HOST_RESOLVER_CANONNAME
| HOST_RESOLVER_LOOPBACK_ONLY
|
2051 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
),
2052 0) << " Unhandled flag";
2055 AddressFamily family
= GetAddressFamily(*ip_number
);
2056 if (family
== ADDRESS_FAMILY_IPV6
&&
2057 !probe_ipv6_support_
&&
2058 default_address_family_
== ADDRESS_FAMILY_IPV4
) {
2059 // Don't return IPv6 addresses if default address family is set to IPv4,
2060 // and probes are disabled.
2061 *net_error
= ERR_NAME_NOT_RESOLVED
;
2062 } else if (key
.address_family
!= ADDRESS_FAMILY_UNSPECIFIED
&&
2063 key
.address_family
!= family
) {
2064 // Don't return IPv6 addresses for IPv4 queries, and vice versa.
2065 *net_error
= ERR_NAME_NOT_RESOLVED
;
2067 *addresses
= AddressList::CreateFromIPAddress(*ip_number
, info
.port());
2068 if (key
.host_resolver_flags
& HOST_RESOLVER_CANONNAME
)
2069 addresses
->SetDefaultCanonicalName();
2074 bool HostResolverImpl::ServeFromCache(const Key
& key
,
2075 const RequestInfo
& info
,
2077 AddressList
* addresses
) {
2080 if (!info
.allow_cached_response() || !cache_
.get())
2083 const HostCache::Entry
* cache_entry
= cache_
->Lookup(
2084 key
, base::TimeTicks::Now());
2088 *net_error
= cache_entry
->error
;
2089 if (*net_error
== OK
) {
2090 if (cache_entry
->has_ttl())
2091 RecordTTL(cache_entry
->ttl
);
2092 *addresses
= EnsurePortOnAddressList(cache_entry
->addrlist
, info
.port());
2097 bool HostResolverImpl::ServeFromHosts(const Key
& key
,
2098 const RequestInfo
& info
,
2099 AddressList
* addresses
) {
2101 if (!HaveDnsConfig())
2105 // HOSTS lookups are case-insensitive.
2106 std::string hostname
= base::StringToLowerASCII(key
.hostname
);
2108 const DnsHosts
& hosts
= dns_client_
->GetConfig()->hosts
;
2110 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2111 // (glibc and c-ares) return the first matching line. We have more
2112 // flexibility, but lose implicit ordering.
2113 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2115 if (key
.address_family
== ADDRESS_FAMILY_IPV6
||
2116 key
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
2117 DnsHosts::const_iterator it
= hosts
.find(
2118 DnsHostsKey(hostname
, ADDRESS_FAMILY_IPV6
));
2119 if (it
!= hosts
.end())
2120 addresses
->push_back(IPEndPoint(it
->second
, info
.port()));
2123 if (key
.address_family
== ADDRESS_FAMILY_IPV4
||
2124 key
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
2125 DnsHosts::const_iterator it
= hosts
.find(
2126 DnsHostsKey(hostname
, ADDRESS_FAMILY_IPV4
));
2127 if (it
!= hosts
.end())
2128 addresses
->push_back(IPEndPoint(it
->second
, info
.port()));
2131 // If got only loopback addresses and the family was restricted, resolve
2132 // again, without restrictions. See SystemHostResolverCall for rationale.
2133 if ((key
.host_resolver_flags
&
2134 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
) &&
2135 IsAllIPv4Loopback(*addresses
)) {
2137 new_key
.address_family
= ADDRESS_FAMILY_UNSPECIFIED
;
2138 new_key
.host_resolver_flags
&=
2139 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
;
2140 return ServeFromHosts(new_key
, info
, addresses
);
2142 return !addresses
->empty();
2145 void HostResolverImpl::CacheResult(const Key
& key
,
2146 const HostCache::Entry
& entry
,
2147 base::TimeDelta ttl
) {
2149 cache_
->Set(key
, entry
, base::TimeTicks::Now(), ttl
);
2152 void HostResolverImpl::RemoveJob(Job
* job
) {
2154 JobMap::iterator it
= jobs_
.find(job
->key());
2155 if (it
!= jobs_
.end() && it
->second
== job
)
2159 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result
) {
2161 additional_resolver_flags_
|= HOST_RESOLVER_LOOPBACK_ONLY
;
2163 additional_resolver_flags_
&= ~HOST_RESOLVER_LOOPBACK_ONLY
;
2167 HostResolverImpl::Key
HostResolverImpl::GetEffectiveKeyForRequest(
2168 const RequestInfo
& info
,
2169 const IPAddressNumber
* ip_number
,
2170 const BoundNetLog
& net_log
) const {
2171 HostResolverFlags effective_flags
=
2172 info
.host_resolver_flags() | additional_resolver_flags_
;
2173 AddressFamily effective_address_family
= info
.address_family();
2175 if (info
.address_family() == ADDRESS_FAMILY_UNSPECIFIED
) {
2176 if (probe_ipv6_support_
&& !use_local_ipv6_
&&
2177 // When resolving IPv4 literals, there's no need to probe for IPv6.
2178 // When resolving IPv6 literals, there's no benefit to artificially
2179 // limiting our resolution based on a probe. Prior logic ensures
2180 // that this query is UNSPECIFIED (see info.address_family()
2181 // check above) and that |default_address_family_| is UNSPECIFIED
2182 // (|prove_ipv6_support_| is false if |default_address_family_| is
2183 // set) so the code requesting the resolution should be amenable to
2184 // receiving a IPv6 resolution.
2185 ip_number
== nullptr) {
2186 // Google DNS address.
2187 const uint8 kIPv6Address
[] =
2188 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
2189 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
2190 IPAddressNumber
address(kIPv6Address
,
2191 kIPv6Address
+ arraysize(kIPv6Address
));
2192 bool rv6
= IsGloballyReachable(address
, net_log
);
2193 net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK
,
2194 NetLog::BoolCallback("ipv6_available", rv6
));
2196 effective_address_family
= ADDRESS_FAMILY_IPV4
;
2197 effective_flags
|= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
;
2200 effective_address_family
= default_address_family_
;
2204 std::string hostname
= info
.hostname();
2205 // Redirect .localhost queries to "localhost." to make sure that they
2206 // are never sent out on the network, per RFC 6761.
2207 if (IsLocalhostTLD(info
.hostname()))
2208 hostname
= kLocalhost
;
2210 return Key(hostname
, effective_address_family
, effective_flags
);
2213 void HostResolverImpl::AbortAllInProgressJobs() {
2214 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2215 // first collect and remove all running jobs from |jobs_|.
2216 ScopedVector
<Job
> jobs_to_abort
;
2217 for (JobMap::iterator it
= jobs_
.begin(); it
!= jobs_
.end(); ) {
2218 Job
* job
= it
->second
;
2219 if (job
->is_running()) {
2220 jobs_to_abort
.push_back(job
);
2223 DCHECK(job
->is_queued());
2228 // Pause the dispatcher so it won't start any new dispatcher jobs while
2229 // aborting the old ones. This is needed so that it won't start the second
2230 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2232 PrioritizedDispatcher::Limits limits
= dispatcher_
->GetLimits();
2233 dispatcher_
->SetLimits(
2234 PrioritizedDispatcher::Limits(limits
.reserved_slots
.size(), 0));
2236 // Life check to bail once |this| is deleted.
2237 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2240 for (size_t i
= 0; self
.get() && i
< jobs_to_abort
.size(); ++i
) {
2241 jobs_to_abort
[i
]->Abort();
2242 jobs_to_abort
[i
] = NULL
;
2246 dispatcher_
->SetLimits(limits
);
2249 void HostResolverImpl::AbortDnsTasks() {
2250 // Pause the dispatcher so it won't start any new dispatcher jobs while
2251 // aborting the old ones. This is needed so that it won't start the second
2252 // DnsTransaction for a job if the DnsConfig just changed.
2253 PrioritizedDispatcher::Limits limits
= dispatcher_
->GetLimits();
2254 dispatcher_
->SetLimits(
2255 PrioritizedDispatcher::Limits(limits
.reserved_slots
.size(), 0));
2257 for (JobMap::iterator it
= jobs_
.begin(); it
!= jobs_
.end(); ++it
)
2258 it
->second
->AbortDnsTask();
2259 dispatcher_
->SetLimits(limits
);
2262 void HostResolverImpl::TryServingAllJobsFromHosts() {
2263 if (!HaveDnsConfig())
2266 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2267 // http://crbug.com/117655
2269 // Life check to bail once |this| is deleted.
2270 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2272 for (JobMap::iterator it
= jobs_
.begin(); self
.get() && it
!= jobs_
.end();) {
2273 Job
* job
= it
->second
;
2275 // This could remove |job| from |jobs_|, but iterator will remain valid.
2276 job
->ServeFromHosts();
2280 void HostResolverImpl::OnIPAddressChanged() {
2281 resolved_known_ipv6_hostname_
= false;
2282 // Abandon all ProbeJobs.
2283 probe_weak_ptr_factory_
.InvalidateWeakPtrs();
2286 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
2287 new LoopbackProbeJob(probe_weak_ptr_factory_
.GetWeakPtr());
2289 AbortAllInProgressJobs();
2290 // |this| may be deleted inside AbortAllInProgressJobs().
2293 void HostResolverImpl::OnInitialDNSConfigRead() {
2294 UpdateDNSConfig(false);
2297 void HostResolverImpl::OnDNSChanged() {
2298 UpdateDNSConfig(true);
2301 void HostResolverImpl::UpdateDNSConfig(bool config_changed
) {
2302 DnsConfig dns_config
;
2303 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
2306 net_log_
->AddGlobalEntry(
2307 NetLog::TYPE_DNS_CONFIG_CHANGED
,
2308 base::Bind(&NetLogDnsConfigCallback
, &dns_config
));
2311 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
2312 received_dns_config_
= dns_config
.IsValid();
2313 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2314 use_local_ipv6_
= !dns_config
.IsValid() || dns_config
.use_local_ipv6
;
2316 num_dns_failures_
= 0;
2318 // We want a new DnsSession in place, before we Abort running Jobs, so that
2319 // the newly started jobs use the new config.
2320 if (dns_client_
.get()) {
2321 dns_client_
->SetConfig(dns_config
);
2322 if (dns_client_
->GetConfig()) {
2323 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2324 // If we just switched DnsClients, restart jobs using new resolver.
2325 // TODO(pauljensen): Is this necessary?
2326 config_changed
= true;
2330 if (config_changed
) {
2331 // If the DNS server has changed, existing cached info could be wrong so we
2332 // have to drop our internal cache :( Note that OS level DNS caches, such
2333 // as NSCD's cache should be dropped automatically by the OS when
2334 // resolv.conf changes so we don't need to do anything to clear that cache.
2338 // Life check to bail once |this| is deleted.
2339 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2341 // Existing jobs will have been sent to the original server so they need to
2343 AbortAllInProgressJobs();
2345 // |this| may be deleted inside AbortAllInProgressJobs().
2347 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;
2367 ++num_dns_failures_
;
2368 if (num_dns_failures_
< kMaximumDnsFailures
)
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.
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);