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_log.h"
42 #include "net/base/net_util.h"
43 #include "net/dns/address_sorter.h"
44 #include "net/dns/dns_client.h"
45 #include "net/dns/dns_config_service.h"
46 #include "net/dns/dns_protocol.h"
47 #include "net/dns/dns_response.h"
48 #include "net/dns/dns_transaction.h"
49 #include "net/dns/host_resolver_proc.h"
50 #include "net/socket/client_socket_factory.h"
51 #include "net/udp/datagram_client_socket.h"
54 #include "net/base/winsock_init.h"
61 // Limit the size of hostnames that will be resolved to combat issues in
62 // some platform's resolvers.
63 const size_t kMaxHostLength
= 4096;
65 // Default TTL for successful resolutions with ProcTask.
66 const unsigned kCacheEntryTTLSeconds
= 60;
68 // Default TTL for unsuccessful resolutions with ProcTask.
69 const unsigned kNegativeCacheEntryTTLSeconds
= 0;
71 // Minimum TTL for successful resolutions with DnsTask.
72 const unsigned kMinimumTTLSeconds
= kCacheEntryTTLSeconds
;
74 // We use a separate histogram name for each platform to facilitate the
75 // display of error codes by their symbolic name (since each platform has
76 // different mappings).
77 const char kOSErrorsForGetAddrinfoHistogramName
[] =
79 "Net.OSErrorsForGetAddrinfo_Win";
80 #elif defined(OS_MACOSX)
81 "Net.OSErrorsForGetAddrinfo_Mac";
82 #elif defined(OS_LINUX)
83 "Net.OSErrorsForGetAddrinfo_Linux";
85 "Net.OSErrorsForGetAddrinfo";
88 // Gets a list of the likely error codes that getaddrinfo() can return
89 // (non-exhaustive). These are the error codes that we will track via
91 std::vector
<int> GetAllGetAddrinfoOSErrors() {
94 #if !defined(OS_FREEBSD)
95 #if !defined(OS_ANDROID)
96 // EAI_ADDRFAMILY has been declared obsolete in Android's and
100 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
112 #elif defined(OS_WIN)
113 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
114 WSA_NOT_ENOUGH_MEMORY
,
124 // The following are not in doc, but might be to appearing in results :-(.
129 // Ensure all errors are positive, as histogram only tracks positive values.
130 for (size_t i
= 0; i
< arraysize(os_errors
); ++i
) {
131 os_errors
[i
] = std::abs(os_errors
[i
]);
134 return base::CustomHistogram::ArrayToCustomRanges(os_errors
,
135 arraysize(os_errors
));
138 enum DnsResolveStatus
{
139 RESOLVE_STATUS_DNS_SUCCESS
= 0,
140 RESOLVE_STATUS_PROC_SUCCESS
,
142 RESOLVE_STATUS_SUSPECT_NETBIOS
,
146 void UmaAsyncDnsResolveStatus(DnsResolveStatus result
) {
147 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
152 bool ResemblesNetBIOSName(const std::string
& hostname
) {
153 return (hostname
.size() < 16) && (hostname
.find('.') == std::string::npos
);
156 // True if |hostname| ends with either ".local" or ".local.".
157 bool ResemblesMulticastDNSName(const std::string
& hostname
) {
158 DCHECK(!hostname
.empty());
159 const char kSuffix
[] = ".local.";
160 const size_t kSuffixLen
= sizeof(kSuffix
) - 1;
161 const size_t kSuffixLenTrimmed
= kSuffixLen
- 1;
162 if (hostname
[hostname
.size() - 1] == '.') {
163 return hostname
.size() > kSuffixLen
&&
164 !hostname
.compare(hostname
.size() - kSuffixLen
, kSuffixLen
, kSuffix
);
166 return hostname
.size() > kSuffixLenTrimmed
&&
167 !hostname
.compare(hostname
.size() - kSuffixLenTrimmed
, kSuffixLenTrimmed
,
168 kSuffix
, kSuffixLenTrimmed
);
171 // Attempts to connect a UDP socket to |dest|:53.
172 bool IsGloballyReachable(const IPAddressNumber
& dest
,
173 const BoundNetLog
& net_log
) {
174 scoped_ptr
<DatagramClientSocket
> socket(
175 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
176 DatagramSocket::DEFAULT_BIND
,
180 int rv
= socket
->Connect(IPEndPoint(dest
, 53));
184 rv
= socket
->GetLocalAddress(&endpoint
);
187 DCHECK_EQ(ADDRESS_FAMILY_IPV6
, endpoint
.GetFamily());
188 const IPAddressNumber
& address
= endpoint
.address();
189 bool is_link_local
= (address
[0] == 0xFE) && ((address
[1] & 0xC0) == 0x80);
192 const uint8 kTeredoPrefix
[] = { 0x20, 0x01, 0, 0 };
193 bool is_teredo
= std::equal(kTeredoPrefix
,
194 kTeredoPrefix
+ arraysize(kTeredoPrefix
),
201 // Provide a common macro to simplify code and readability. We must use a
202 // macro as the underlying HISTOGRAM macro creates static variables.
203 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \
204 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100)
206 // A macro to simplify code and readability.
207 #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
209 switch (priority) { \
210 case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \
211 case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \
212 case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \
213 case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \
214 case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \
215 default: NOTREACHED(); break; \
217 DNS_HISTOGRAM(basename, time); \
220 // Record time from Request creation until a valid DNS response.
221 void RecordTotalTime(bool had_dns_config
,
223 base::TimeDelta duration
) {
224 if (had_dns_config
) {
226 DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration
);
228 DNS_HISTOGRAM("AsyncDNS.TotalTime", duration
);
232 DNS_HISTOGRAM("DNS.TotalTime_speculative", duration
);
234 DNS_HISTOGRAM("DNS.TotalTime", duration
);
239 void RecordTTL(base::TimeDelta ttl
) {
240 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl
,
241 base::TimeDelta::FromSeconds(1),
242 base::TimeDelta::FromDays(1), 100);
245 bool ConfigureAsyncDnsNoFallbackFieldTrial() {
246 const bool kDefault
= false;
248 // Configure the AsyncDns field trial as follows:
249 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
250 // groups AsyncDnsA and AsyncDnsB: return false,
251 // groups SystemDnsA and SystemDnsB: return false,
252 // otherwise (trial absent): return default.
253 std::string group_name
= base::FieldTrialList::FindFullName("AsyncDns");
254 if (!group_name
.empty())
255 return StartsWithASCII(group_name
, "AsyncDnsNoFallback", false);
259 //-----------------------------------------------------------------------------
261 AddressList
EnsurePortOnAddressList(const AddressList
& list
, uint16 port
) {
262 if (list
.empty() || list
.front().port() == port
)
264 return AddressList::CopyWithPort(list
, port
);
267 // Returns true if |addresses| contains only IPv4 loopback addresses.
268 bool IsAllIPv4Loopback(const AddressList
& addresses
) {
269 for (unsigned i
= 0; i
< addresses
.size(); ++i
) {
270 const IPAddressNumber
& address
= addresses
[i
].address();
271 switch (addresses
[i
].GetFamily()) {
272 case ADDRESS_FAMILY_IPV4
:
273 if (address
[0] != 127)
276 case ADDRESS_FAMILY_IPV6
:
286 // Creates NetLog parameters when the resolve failed.
287 base::Value
* NetLogProcTaskFailedCallback(uint32 attempt_number
,
290 NetLog::LogLevel
/* log_level */) {
291 base::DictionaryValue
* dict
= new base::DictionaryValue();
293 dict
->SetInteger("attempt_number", attempt_number
);
295 dict
->SetInteger("net_error", net_error
);
298 dict
->SetInteger("os_error", os_error
);
299 #if defined(OS_POSIX)
300 dict
->SetString("os_error_string", gai_strerror(os_error
));
301 #elif defined(OS_WIN)
302 // Map the error code to a human-readable string.
303 LPWSTR error_string
= NULL
;
304 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
305 0, // Use the internal message table.
307 0, // Use default language.
308 (LPWSTR
)&error_string
,
310 0); // Arguments (unused).
311 dict
->SetString("os_error_string", base::WideToUTF8(error_string
));
312 LocalFree(error_string
);
319 // Creates NetLog parameters when the DnsTask failed.
320 base::Value
* NetLogDnsTaskFailedCallback(int net_error
,
322 NetLog::LogLevel
/* log_level */) {
323 base::DictionaryValue
* dict
= new base::DictionaryValue();
324 dict
->SetInteger("net_error", net_error
);
326 dict
->SetInteger("dns_error", dns_error
);
330 // Creates NetLog parameters containing the information in a RequestInfo object,
331 // along with the associated NetLog::Source.
332 base::Value
* NetLogRequestInfoCallback(const HostResolver::RequestInfo
* info
,
333 NetLog::LogLevel
/* log_level */) {
334 base::DictionaryValue
* dict
= new base::DictionaryValue();
336 dict
->SetString("host", info
->host_port_pair().ToString());
337 dict
->SetInteger("address_family",
338 static_cast<int>(info
->address_family()));
339 dict
->SetBoolean("allow_cached_response", info
->allow_cached_response());
340 dict
->SetBoolean("is_speculative", info
->is_speculative());
344 // Creates NetLog parameters for the creation of a HostResolverImpl::Job.
345 base::Value
* NetLogJobCreationCallback(const NetLog::Source
& source
,
346 const std::string
* host
,
347 NetLog::LogLevel
/* log_level */) {
348 base::DictionaryValue
* dict
= new base::DictionaryValue();
349 source
.AddToEventParameters(dict
);
350 dict
->SetString("host", *host
);
354 // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
355 base::Value
* NetLogJobAttachCallback(const NetLog::Source
& source
,
356 RequestPriority priority
,
357 NetLog::LogLevel
/* log_level */) {
358 base::DictionaryValue
* dict
= new base::DictionaryValue();
359 source
.AddToEventParameters(dict
);
360 dict
->SetString("priority", RequestPriorityToString(priority
));
364 // Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
365 base::Value
* NetLogDnsConfigCallback(const DnsConfig
* config
,
366 NetLog::LogLevel
/* log_level */) {
367 return config
->ToValue();
370 // The logging routines are defined here because some requests are resolved
371 // without a Request object.
373 // Logs when a request has just been started.
374 void LogStartRequest(const BoundNetLog
& source_net_log
,
375 const HostResolver::RequestInfo
& info
) {
376 source_net_log
.BeginEvent(
377 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
,
378 base::Bind(&NetLogRequestInfoCallback
, &info
));
381 // Logs when a request has just completed (before its callback is run).
382 void LogFinishRequest(const BoundNetLog
& source_net_log
,
383 const HostResolver::RequestInfo
& info
,
385 source_net_log
.EndEventWithNetErrorCode(
386 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
, net_error
);
389 // Logs when a request has been cancelled.
390 void LogCancelRequest(const BoundNetLog
& source_net_log
,
391 const HostResolverImpl::RequestInfo
& info
) {
392 source_net_log
.AddEvent(NetLog::TYPE_CANCELLED
);
393 source_net_log
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
);
396 //-----------------------------------------------------------------------------
398 // Keeps track of the highest priority.
399 class PriorityTracker
{
401 explicit PriorityTracker(RequestPriority initial_priority
)
402 : highest_priority_(initial_priority
), total_count_(0) {
403 memset(counts_
, 0, sizeof(counts_
));
406 RequestPriority
highest_priority() const {
407 return highest_priority_
;
410 size_t total_count() const {
414 void Add(RequestPriority req_priority
) {
416 ++counts_
[req_priority
];
417 if (highest_priority_
< req_priority
)
418 highest_priority_
= req_priority
;
421 void Remove(RequestPriority req_priority
) {
422 DCHECK_GT(total_count_
, 0u);
423 DCHECK_GT(counts_
[req_priority
], 0u);
425 --counts_
[req_priority
];
427 for (i
= highest_priority_
; i
> MINIMUM_PRIORITY
&& !counts_
[i
]; --i
);
428 highest_priority_
= static_cast<RequestPriority
>(i
);
430 // In absence of requests, default to MINIMUM_PRIORITY.
431 if (total_count_
== 0)
432 DCHECK_EQ(MINIMUM_PRIORITY
, highest_priority_
);
436 RequestPriority highest_priority_
;
438 size_t counts_
[NUM_PRIORITIES
];
443 //-----------------------------------------------------------------------------
445 const unsigned HostResolverImpl::kMaximumDnsFailures
= 16;
447 // Holds the data for a request that could not be completed synchronously.
448 // It is owned by a Job. Canceled Requests are only marked as canceled rather
449 // than removed from the Job's |requests_| list.
450 class HostResolverImpl::Request
{
452 Request(const BoundNetLog
& source_net_log
,
453 const RequestInfo
& info
,
454 RequestPriority priority
,
455 const CompletionCallback
& callback
,
456 AddressList
* addresses
)
457 : source_net_log_(source_net_log
),
462 addresses_(addresses
),
463 request_time_(base::TimeTicks::Now()) {}
465 // Mark the request as canceled.
466 void MarkAsCanceled() {
472 bool was_canceled() const {
473 return callback_
.is_null();
476 void set_job(Job
* job
) {
478 // Identify which job the request is waiting on.
482 // Prepare final AddressList and call completion callback.
483 void OnComplete(int error
, const AddressList
& addr_list
) {
484 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
485 tracked_objects::ScopedTracker
tracking_profile(
486 FROM_HERE_WITH_EXPLICIT_FUNCTION(
487 "436634 HostResolverImpl::Request::OnComplete"));
489 DCHECK(!was_canceled());
491 *addresses_
= EnsurePortOnAddressList(addr_list
, info_
.port());
492 CompletionCallback callback
= callback_
;
501 // NetLog for the source, passed in HostResolver::Resolve.
502 const BoundNetLog
& source_net_log() {
503 return source_net_log_
;
506 const RequestInfo
& info() const {
510 RequestPriority
priority() const { return priority_
; }
512 base::TimeTicks
request_time() const { return request_time_
; }
515 const BoundNetLog source_net_log_
;
517 // The request info that started the request.
518 const RequestInfo info_
;
520 // TODO(akalin): Support reprioritization.
521 const RequestPriority priority_
;
523 // The resolve job that this request is dependent on.
526 // The user's callback to invoke when the request completes.
527 CompletionCallback callback_
;
529 // The address list to save result into.
530 AddressList
* addresses_
;
532 const base::TimeTicks request_time_
;
534 DISALLOW_COPY_AND_ASSIGN(Request
);
537 //------------------------------------------------------------------------------
539 // Calls HostResolverProc on the WorkerPool. Performs retries if necessary.
541 // Whenever we try to resolve the host, we post a delayed task to check if host
542 // resolution (OnLookupComplete) is completed or not. If the original attempt
543 // hasn't completed, then we start another attempt for host resolution. We take
544 // the results from the first attempt that finishes and ignore the results from
545 // all other attempts.
547 // TODO(szym): Move to separate source file for testing and mocking.
549 class HostResolverImpl::ProcTask
550 : public base::RefCountedThreadSafe
<HostResolverImpl::ProcTask
> {
552 typedef base::Callback
<void(int net_error
,
553 const AddressList
& addr_list
)> Callback
;
555 ProcTask(const Key
& key
,
556 const ProcTaskParams
& params
,
557 const Callback
& callback
,
558 const BoundNetLog
& job_net_log
)
562 origin_loop_(base::MessageLoopProxy::current()),
564 completed_attempt_number_(0),
565 completed_attempt_error_(ERR_UNEXPECTED
),
566 had_non_speculative_request_(false),
567 net_log_(job_net_log
) {
568 if (!params_
.resolver_proc
.get())
569 params_
.resolver_proc
= HostResolverProc::GetDefault();
570 // If default is unset, use the system proc.
571 if (!params_
.resolver_proc
.get())
572 params_
.resolver_proc
= new SystemHostResolverProc();
576 DCHECK(origin_loop_
->BelongsToCurrentThread());
577 net_log_
.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK
);
578 StartLookupAttempt();
581 // Cancels this ProcTask. It will be orphaned. Any outstanding resolve
582 // attempts running on worker threads will continue running. Only once all the
583 // attempts complete will the final reference to this ProcTask be released.
585 DCHECK(origin_loop_
->BelongsToCurrentThread());
587 if (was_canceled() || was_completed())
591 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK
);
594 void set_had_non_speculative_request() {
595 DCHECK(origin_loop_
->BelongsToCurrentThread());
596 had_non_speculative_request_
= true;
599 bool was_canceled() const {
600 DCHECK(origin_loop_
->BelongsToCurrentThread());
601 return callback_
.is_null();
604 bool was_completed() const {
605 DCHECK(origin_loop_
->BelongsToCurrentThread());
606 return completed_attempt_number_
> 0;
610 friend class base::RefCountedThreadSafe
<ProcTask
>;
613 void StartLookupAttempt() {
614 DCHECK(origin_loop_
->BelongsToCurrentThread());
615 base::TimeTicks start_time
= base::TimeTicks::Now();
617 // Dispatch the lookup attempt to a worker thread.
618 if (!base::WorkerPool::PostTask(
620 base::Bind(&ProcTask::DoLookup
, this, start_time
, attempt_number_
),
624 // Since we could be running within Resolve() right now, we can't just
625 // call OnLookupComplete(). Instead we must wait until Resolve() has
626 // returned (IO_PENDING).
627 origin_loop_
->PostTask(
629 base::Bind(&ProcTask::OnLookupComplete
, this, AddressList(),
630 start_time
, attempt_number_
, ERR_UNEXPECTED
, 0));
635 NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED
,
636 NetLog::IntegerCallback("attempt_number", attempt_number_
));
638 // If we don't get the results within a given time, RetryIfNotComplete
639 // will start a new attempt on a different worker thread if none of our
640 // outstanding attempts have completed yet.
641 if (attempt_number_
<= params_
.max_retry_attempts
) {
642 origin_loop_
->PostDelayedTask(
644 base::Bind(&ProcTask::RetryIfNotComplete
, this),
645 params_
.unresponsive_delay
);
649 // WARNING: This code runs inside a worker pool. The shutdown code cannot
650 // wait for it to finish, so we must be very careful here about using other
651 // objects (like MessageLoops, Singletons, etc). During shutdown these objects
652 // may no longer exist. Multiple DoLookups() could be running in parallel, so
653 // any state inside of |this| must not mutate .
654 void DoLookup(const base::TimeTicks
& start_time
,
655 const uint32 attempt_number
) {
658 // Running on the worker thread
659 int error
= params_
.resolver_proc
->Resolve(key_
.hostname
,
661 key_
.host_resolver_flags
,
665 origin_loop_
->PostTask(
667 base::Bind(&ProcTask::OnLookupComplete
, this, results
, start_time
,
668 attempt_number
, error
, os_error
));
671 // Makes next attempt if DoLookup() has not finished (runs on origin thread).
672 void RetryIfNotComplete() {
673 DCHECK(origin_loop_
->BelongsToCurrentThread());
675 if (was_completed() || was_canceled())
678 params_
.unresponsive_delay
*= params_
.retry_factor
;
679 StartLookupAttempt();
682 // Callback for when DoLookup() completes (runs on origin thread).
683 void OnLookupComplete(const AddressList
& results
,
684 const base::TimeTicks
& start_time
,
685 const uint32 attempt_number
,
687 const int os_error
) {
688 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
689 tracked_objects::ScopedTracker
tracking_profile1(
690 FROM_HERE_WITH_EXPLICIT_FUNCTION(
691 "436634 HostResolverImpl::ProcTask::OnLookupComplete1"));
693 DCHECK(origin_loop_
->BelongsToCurrentThread());
694 // If results are empty, we should return an error.
695 bool empty_list_on_ok
= (error
== OK
&& results
.empty());
696 UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok
);
697 if (empty_list_on_ok
)
698 error
= ERR_NAME_NOT_RESOLVED
;
700 bool was_retry_attempt
= attempt_number
> 1;
702 // Ideally the following code would be part of host_resolver_proc.cc,
703 // however it isn't safe to call NetworkChangeNotifier from worker threads.
704 // So we do it here on the IO thread instead.
705 if (error
!= OK
&& NetworkChangeNotifier::IsOffline())
706 error
= ERR_INTERNET_DISCONNECTED
;
708 // If this is the first attempt that is finishing later, then record data
709 // for the first attempt. Won't contaminate with retry attempt's data.
710 if (!was_retry_attempt
)
711 RecordPerformanceHistograms(start_time
, error
, os_error
);
713 RecordAttemptHistograms(start_time
, attempt_number
, error
, os_error
);
718 NetLog::ParametersCallback net_log_callback
;
720 net_log_callback
= base::Bind(&NetLogProcTaskFailedCallback
,
725 net_log_callback
= NetLog::IntegerCallback("attempt_number",
728 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED
,
734 // Copy the results from the first worker thread that resolves the host.
736 completed_attempt_number_
= attempt_number
;
737 completed_attempt_error_
= error
;
739 if (was_retry_attempt
) {
740 // If retry attempt finishes before 1st attempt, then get stats on how
741 // much time is saved by having spawned an extra attempt.
742 retry_attempt_finished_time_
= base::TimeTicks::Now();
746 net_log_callback
= base::Bind(&NetLogProcTaskFailedCallback
,
749 net_log_callback
= results_
.CreateNetLogCallback();
751 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK
,
754 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
755 tracked_objects::ScopedTracker
tracking_profile2(
756 FROM_HERE_WITH_EXPLICIT_FUNCTION(
757 "436634 HostResolverImpl::ProcTask::OnLookupComplete2"));
759 callback_
.Run(error
, results_
);
762 void RecordPerformanceHistograms(const base::TimeTicks
& start_time
,
764 const int os_error
) const {
765 DCHECK(origin_loop_
->BelongsToCurrentThread());
766 enum Category
{ // Used in UMA_HISTOGRAM_ENUMERATION.
769 RESOLVE_SPECULATIVE_SUCCESS
,
770 RESOLVE_SPECULATIVE_FAIL
,
771 RESOLVE_MAX
, // Bounding value.
773 int category
= RESOLVE_MAX
; // Illegal value for later DCHECK only.
775 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
777 if (had_non_speculative_request_
) {
778 category
= RESOLVE_SUCCESS
;
779 DNS_HISTOGRAM("DNS.ResolveSuccess", duration
);
781 category
= RESOLVE_SPECULATIVE_SUCCESS
;
782 DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration
);
785 // Log DNS lookups based on |address_family|. This will help us determine
786 // if IPv4 or IPv4/6 lookups are faster or slower.
787 switch(key_
.address_family
) {
788 case ADDRESS_FAMILY_IPV4
:
789 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration
);
791 case ADDRESS_FAMILY_IPV6
:
792 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration
);
794 case ADDRESS_FAMILY_UNSPECIFIED
:
795 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration
);
799 if (had_non_speculative_request_
) {
800 category
= RESOLVE_FAIL
;
801 DNS_HISTOGRAM("DNS.ResolveFail", duration
);
803 category
= RESOLVE_SPECULATIVE_FAIL
;
804 DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration
);
806 // Log DNS lookups based on |address_family|. This will help us determine
807 // if IPv4 or IPv4/6 lookups are faster or slower.
808 switch(key_
.address_family
) {
809 case ADDRESS_FAMILY_IPV4
:
810 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration
);
812 case ADDRESS_FAMILY_IPV6
:
813 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration
);
815 case ADDRESS_FAMILY_UNSPECIFIED
:
816 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration
);
819 UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName
,
821 GetAllGetAddrinfoOSErrors());
823 DCHECK_LT(category
, static_cast<int>(RESOLVE_MAX
)); // Be sure it was set.
825 UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category
, RESOLVE_MAX
);
828 void RecordAttemptHistograms(const base::TimeTicks
& start_time
,
829 const uint32 attempt_number
,
831 const int os_error
) const {
832 DCHECK(origin_loop_
->BelongsToCurrentThread());
833 bool first_attempt_to_complete
=
834 completed_attempt_number_
== attempt_number
;
835 bool is_first_attempt
= (attempt_number
== 1);
837 if (first_attempt_to_complete
) {
838 // If this was first attempt to complete, then record the resolution
839 // status of the attempt.
840 if (completed_attempt_error_
== OK
) {
841 UMA_HISTOGRAM_ENUMERATION(
842 "DNS.AttemptFirstSuccess", attempt_number
, 100);
844 UMA_HISTOGRAM_ENUMERATION(
845 "DNS.AttemptFirstFailure", attempt_number
, 100);
850 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number
, 100);
852 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number
, 100);
854 // If first attempt didn't finish before retry attempt, then calculate stats
855 // on how much time is saved by having spawned an extra attempt.
856 if (!first_attempt_to_complete
&& is_first_attempt
&& !was_canceled()) {
857 DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry",
858 base::TimeTicks::Now() - retry_attempt_finished_time_
);
861 if (was_canceled() || !first_attempt_to_complete
) {
862 // Count those attempts which completed after the job was already canceled
863 // OR after the job was already completed by an earlier attempt (so in
865 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number
, 100);
867 // Record if job is canceled.
869 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number
, 100);
872 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
874 DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration
);
876 DNS_HISTOGRAM("DNS.AttemptFailDuration", duration
);
879 // Set on the origin thread, read on the worker thread.
882 // Holds an owning reference to the HostResolverProc that we are going to use.
883 // This may not be the current resolver procedure by the time we call
884 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
885 // reference ensures that it remains valid until we are done.
886 ProcTaskParams params_
;
888 // The listener to the results of this ProcTask.
891 // Used to post ourselves onto the origin thread.
892 scoped_refptr
<base::MessageLoopProxy
> origin_loop_
;
894 // Keeps track of the number of attempts we have made so far to resolve the
895 // host. Whenever we start an attempt to resolve the host, we increase this
897 uint32 attempt_number_
;
899 // The index of the attempt which finished first (or 0 if the job is still in
901 uint32 completed_attempt_number_
;
903 // The result (a net error code) from the first attempt to complete.
904 int completed_attempt_error_
;
906 // The time when retry attempt was finished.
907 base::TimeTicks retry_attempt_finished_time_
;
909 // True if a non-speculative request was ever attached to this job
910 // (regardless of whether or not it was later canceled.
911 // This boolean is used for histogramming the duration of jobs used to
912 // service non-speculative requests.
913 bool had_non_speculative_request_
;
915 AddressList results_
;
917 BoundNetLog net_log_
;
919 DISALLOW_COPY_AND_ASSIGN(ProcTask
);
922 //-----------------------------------------------------------------------------
924 // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as
925 // it takes 40-100ms and should not block initialization.
926 class HostResolverImpl::LoopbackProbeJob
{
928 explicit LoopbackProbeJob(const base::WeakPtr
<HostResolverImpl
>& resolver
)
929 : resolver_(resolver
),
931 DCHECK(resolver
.get());
932 const bool kIsSlow
= true;
933 base::WorkerPool::PostTaskAndReply(
935 base::Bind(&LoopbackProbeJob::DoProbe
, base::Unretained(this)),
936 base::Bind(&LoopbackProbeJob::OnProbeComplete
, base::Owned(this)),
940 virtual ~LoopbackProbeJob() {}
943 // Runs on worker thread.
945 result_
= HaveOnlyLoopbackAddresses();
948 void OnProbeComplete() {
949 if (!resolver_
.get())
951 resolver_
->SetHaveOnlyLoopbackAddresses(result_
);
954 // Used/set only on origin thread.
955 base::WeakPtr
<HostResolverImpl
> resolver_
;
959 DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob
);
962 //-----------------------------------------------------------------------------
964 // Resolves the hostname using DnsTransaction.
965 // TODO(szym): This could be moved to separate source file as well.
966 class HostResolverImpl::DnsTask
: public base::SupportsWeakPtr
<DnsTask
> {
970 virtual void OnDnsTaskComplete(base::TimeTicks start_time
,
972 const AddressList
& addr_list
,
973 base::TimeDelta ttl
) = 0;
975 // Called when the first of two jobs succeeds. If the first completed
976 // transaction fails, this is not called. Also not called when the DnsTask
977 // only needs to run one transaction.
978 virtual void OnFirstDnsTransactionComplete() = 0;
982 virtual ~Delegate() {}
985 DnsTask(DnsClient
* client
,
988 const BoundNetLog
& job_net_log
)
992 net_log_(job_net_log
),
993 num_completed_transactions_(0),
994 task_start_time_(base::TimeTicks::Now()) {
999 bool needs_two_transactions() const {
1000 return key_
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
;
1003 bool needs_another_transaction() const {
1004 return needs_two_transactions() && !transaction_aaaa_
;
1007 void StartFirstTransaction() {
1008 DCHECK_EQ(0u, num_completed_transactions_
);
1009 net_log_
.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
);
1010 if (key_
.address_family
== ADDRESS_FAMILY_IPV6
) {
1017 void StartSecondTransaction() {
1018 DCHECK(needs_two_transactions());
1024 DCHECK(!transaction_a_
);
1025 DCHECK_NE(ADDRESS_FAMILY_IPV6
, key_
.address_family
);
1026 transaction_a_
= CreateTransaction(ADDRESS_FAMILY_IPV4
);
1027 transaction_a_
->Start();
1031 DCHECK(!transaction_aaaa_
);
1032 DCHECK_NE(ADDRESS_FAMILY_IPV4
, key_
.address_family
);
1033 transaction_aaaa_
= CreateTransaction(ADDRESS_FAMILY_IPV6
);
1034 transaction_aaaa_
->Start();
1037 scoped_ptr
<DnsTransaction
> CreateTransaction(AddressFamily family
) {
1038 DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED
, family
);
1039 return client_
->GetTransactionFactory()->CreateTransaction(
1041 family
== ADDRESS_FAMILY_IPV6
? dns_protocol::kTypeAAAA
:
1042 dns_protocol::kTypeA
,
1043 base::Bind(&DnsTask::OnTransactionComplete
, base::Unretained(this),
1044 base::TimeTicks::Now()),
1048 void OnTransactionComplete(const base::TimeTicks
& start_time
,
1049 DnsTransaction
* transaction
,
1051 const DnsResponse
* response
) {
1052 DCHECK(transaction
);
1053 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
1054 if (net_error
!= OK
) {
1055 DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration
);
1056 OnFailure(net_error
, DnsResponse::DNS_PARSE_OK
);
1060 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration
);
1061 switch (transaction
->GetType()) {
1062 case dns_protocol::kTypeA
:
1063 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration
);
1065 case dns_protocol::kTypeAAAA
:
1066 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration
);
1070 AddressList addr_list
;
1071 base::TimeDelta ttl
;
1072 DnsResponse::Result result
= response
->ParseToAddressList(&addr_list
, &ttl
);
1073 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
1075 DnsResponse::DNS_PARSE_RESULT_MAX
);
1076 if (result
!= DnsResponse::DNS_PARSE_OK
) {
1077 // Fail even if the other query succeeds.
1078 OnFailure(ERR_DNS_MALFORMED_RESPONSE
, result
);
1082 ++num_completed_transactions_
;
1083 if (num_completed_transactions_
== 1) {
1086 ttl_
= std::min(ttl_
, ttl
);
1089 if (transaction
->GetType() == dns_protocol::kTypeA
) {
1090 DCHECK_EQ(transaction_a_
.get(), transaction
);
1091 // Place IPv4 addresses after IPv6.
1092 addr_list_
.insert(addr_list_
.end(), addr_list
.begin(), addr_list
.end());
1094 DCHECK_EQ(transaction_aaaa_
.get(), transaction
);
1095 // Place IPv6 addresses before IPv4.
1096 addr_list_
.insert(addr_list_
.begin(), addr_list
.begin(), addr_list
.end());
1099 if (needs_two_transactions() && num_completed_transactions_
== 1) {
1100 // No need to repeat the suffix search.
1101 key_
.hostname
= transaction
->GetHostname();
1102 delegate_
->OnFirstDnsTransactionComplete();
1106 if (addr_list_
.empty()) {
1107 // TODO(szym): Don't fallback to ProcTask in this case.
1108 OnFailure(ERR_NAME_NOT_RESOLVED
, DnsResponse::DNS_PARSE_OK
);
1112 // If there are multiple addresses, and at least one is IPv6, need to sort
1113 // them. Note that IPv6 addresses are always put before IPv4 ones, so it's
1114 // sufficient to just check the family of the first address.
1115 if (addr_list_
.size() > 1 &&
1116 addr_list_
[0].GetFamily() == ADDRESS_FAMILY_IPV6
) {
1117 // Sort addresses if needed. Sort could complete synchronously.
1118 client_
->GetAddressSorter()->Sort(
1120 base::Bind(&DnsTask::OnSortComplete
,
1122 base::TimeTicks::Now()));
1124 OnSuccess(addr_list_
);
1128 void OnSortComplete(base::TimeTicks start_time
,
1130 const AddressList
& addr_list
) {
1132 DNS_HISTOGRAM("AsyncDNS.SortFailure",
1133 base::TimeTicks::Now() - start_time
);
1134 OnFailure(ERR_DNS_SORT_ERROR
, DnsResponse::DNS_PARSE_OK
);
1138 DNS_HISTOGRAM("AsyncDNS.SortSuccess",
1139 base::TimeTicks::Now() - start_time
);
1141 // AddressSorter prunes unusable destinations.
1142 if (addr_list
.empty()) {
1143 LOG(WARNING
) << "Address list empty after RFC3484 sort";
1144 OnFailure(ERR_NAME_NOT_RESOLVED
, DnsResponse::DNS_PARSE_OK
);
1148 OnSuccess(addr_list
);
1151 void OnFailure(int net_error
, DnsResponse::Result result
) {
1152 DCHECK_NE(OK
, net_error
);
1154 NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
,
1155 base::Bind(&NetLogDnsTaskFailedCallback
, net_error
, result
));
1156 delegate_
->OnDnsTaskComplete(task_start_time_
, net_error
, AddressList(),
1160 void OnSuccess(const AddressList
& addr_list
) {
1161 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
,
1162 addr_list
.CreateNetLogCallback());
1163 delegate_
->OnDnsTaskComplete(task_start_time_
, OK
, addr_list
, ttl_
);
1169 // The listener to the results of this DnsTask.
1170 Delegate
* delegate_
;
1171 const BoundNetLog net_log_
;
1173 scoped_ptr
<DnsTransaction
> transaction_a_
;
1174 scoped_ptr
<DnsTransaction
> transaction_aaaa_
;
1176 unsigned num_completed_transactions_
;
1178 // These are updated as each transaction completes.
1179 base::TimeDelta ttl_
;
1180 // IPv6 addresses must appear first in the list.
1181 AddressList addr_list_
;
1183 base::TimeTicks task_start_time_
;
1185 DISALLOW_COPY_AND_ASSIGN(DnsTask
);
1188 //-----------------------------------------------------------------------------
1190 // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch.
1191 class HostResolverImpl::Job
: public PrioritizedDispatcher::Job
,
1192 public HostResolverImpl::DnsTask::Delegate
{
1194 // Creates new job for |key| where |request_net_log| is bound to the
1195 // request that spawned it.
1196 Job(const base::WeakPtr
<HostResolverImpl
>& resolver
,
1198 RequestPriority priority
,
1199 const BoundNetLog
& source_net_log
)
1200 : resolver_(resolver
),
1202 priority_tracker_(priority
),
1203 had_non_speculative_request_(false),
1204 had_dns_config_(false),
1205 num_occupied_job_slots_(0),
1206 dns_task_error_(OK
),
1207 creation_time_(base::TimeTicks::Now()),
1208 priority_change_time_(creation_time_
),
1209 net_log_(BoundNetLog::Make(source_net_log
.net_log(),
1210 NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB
)) {
1211 source_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB
);
1213 net_log_
.BeginEvent(
1214 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1215 base::Bind(&NetLogJobCreationCallback
,
1216 source_net_log
.source(),
1222 // |resolver_| was destroyed with this Job still in flight.
1223 // Clean-up, record in the log, but don't run any callbacks.
1224 if (is_proc_running()) {
1225 proc_task_
->Cancel();
1228 // Clean up now for nice NetLog.
1230 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1232 } else if (is_queued()) {
1233 // |resolver_| was destroyed without running this Job.
1234 // TODO(szym): is there any benefit in having this distinction?
1235 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
);
1236 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
);
1238 // else CompleteRequests logged EndEvent.
1240 // Log any remaining Requests as cancelled.
1241 for (RequestsList::const_iterator it
= requests_
.begin();
1242 it
!= requests_
.end(); ++it
) {
1244 if (req
->was_canceled())
1246 DCHECK_EQ(this, req
->job());
1247 LogCancelRequest(req
->source_net_log(), req
->info());
1251 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1253 void Schedule(bool at_head
) {
1254 DCHECK(!is_queued());
1255 PrioritizedDispatcher::Handle handle
;
1257 handle
= resolver_
->dispatcher_
->Add(this, priority());
1259 handle
= resolver_
->dispatcher_
->AddAtHead(this, priority());
1261 // The dispatcher could have started |this| in the above call to Add, which
1262 // could have called Schedule again. In that case |handle| will be null,
1263 // but |handle_| may have been set by the other nested call to Schedule.
1264 if (!handle
.is_null()) {
1265 DCHECK(handle_
.is_null());
1270 void AddRequest(scoped_ptr
<Request
> req
) {
1271 DCHECK_EQ(key_
.hostname
, req
->info().hostname());
1274 priority_tracker_
.Add(req
->priority());
1276 req
->source_net_log().AddEvent(
1277 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH
,
1278 net_log_
.source().ToEventParametersCallback());
1281 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH
,
1282 base::Bind(&NetLogJobAttachCallback
,
1283 req
->source_net_log().source(),
1286 // TODO(szym): Check if this is still needed.
1287 if (!req
->info().is_speculative()) {
1288 had_non_speculative_request_
= true;
1289 if (proc_task_
.get())
1290 proc_task_
->set_had_non_speculative_request();
1293 requests_
.push_back(req
.release());
1298 // Marks |req| as cancelled. If it was the last active Request, also finishes
1299 // this Job, marking it as cancelled, and deletes it.
1300 void CancelRequest(Request
* req
) {
1301 DCHECK_EQ(key_
.hostname
, req
->info().hostname());
1302 DCHECK(!req
->was_canceled());
1304 // Don't remove it from |requests_| just mark it canceled.
1305 req
->MarkAsCanceled();
1306 LogCancelRequest(req
->source_net_log(), req
->info());
1308 priority_tracker_
.Remove(req
->priority());
1309 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH
,
1310 base::Bind(&NetLogJobAttachCallback
,
1311 req
->source_net_log().source(),
1314 if (num_active_requests() > 0) {
1317 // If we were called from a Request's callback within CompleteRequests,
1318 // that Request could not have been cancelled, so num_active_requests()
1319 // could not be 0. Therefore, we are not in CompleteRequests().
1320 CompleteRequestsWithError(OK
/* cancelled */);
1324 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1325 // the job. This currently assumes the abort is due to a network change.
1327 DCHECK(is_running());
1328 CompleteRequestsWithError(ERR_NETWORK_CHANGED
);
1331 // If DnsTask present, abort it and fall back to ProcTask.
1332 void AbortDnsTask() {
1335 dns_task_error_
= OK
;
1340 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1341 // Completes all requests and destroys the job.
1343 DCHECK(!is_running());
1344 DCHECK(is_queued());
1347 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED
);
1349 // This signals to CompleteRequests that this job never ran.
1350 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
);
1353 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1354 // this Job was destroyed.
1355 bool ServeFromHosts() {
1356 DCHECK_GT(num_active_requests(), 0u);
1357 AddressList addr_list
;
1358 if (resolver_
->ServeFromHosts(key(),
1359 requests_
.front()->info(),
1361 // This will destroy the Job.
1363 HostCache::Entry(OK
, MakeAddressListForRequest(addr_list
)),
1370 const Key
key() const {
1374 bool is_queued() const {
1375 return !handle_
.is_null();
1378 bool is_running() const {
1379 return is_dns_running() || is_proc_running();
1383 void KillDnsTask() {
1385 ReduceToOneJobSlot();
1390 // Reduce the number of job slots occupied and queued in the dispatcher
1391 // to one. If the second Job slot is queued in the dispatcher, cancels the
1392 // queued job. Otherwise, the second Job has been started by the
1393 // PrioritizedDispatcher, so signals it is complete.
1394 void ReduceToOneJobSlot() {
1395 DCHECK_GE(num_occupied_job_slots_
, 1u);
1397 resolver_
->dispatcher_
->Cancel(handle_
);
1399 } else if (num_occupied_job_slots_
> 1) {
1400 resolver_
->dispatcher_
->OnJobFinished();
1401 --num_occupied_job_slots_
;
1403 DCHECK_EQ(1u, num_occupied_job_slots_
);
1406 void UpdatePriority() {
1408 if (priority() != static_cast<RequestPriority
>(handle_
.priority()))
1409 priority_change_time_
= base::TimeTicks::Now();
1410 handle_
= resolver_
->dispatcher_
->ChangePriority(handle_
, priority());
1414 AddressList
MakeAddressListForRequest(const AddressList
& list
) const {
1415 if (requests_
.empty())
1417 return AddressList::CopyWithPort(list
, requests_
.front()->info().port());
1420 // PriorityDispatch::Job:
1421 void Start() override
{
1422 DCHECK_LE(num_occupied_job_slots_
, 1u);
1425 ++num_occupied_job_slots_
;
1427 if (num_occupied_job_slots_
== 2) {
1428 StartSecondDnsTransaction();
1432 DCHECK(!is_running());
1434 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED
);
1436 had_dns_config_
= resolver_
->HaveDnsConfig();
1438 base::TimeTicks now
= base::TimeTicks::Now();
1439 base::TimeDelta queue_time
= now
- creation_time_
;
1440 base::TimeDelta queue_time_after_change
= now
- priority_change_time_
;
1442 if (had_dns_config_
) {
1443 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(),
1445 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(),
1446 queue_time_after_change
);
1448 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time
);
1449 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(),
1450 queue_time_after_change
);
1454 (key_
.host_resolver_flags
& HOST_RESOLVER_SYSTEM_ONLY
) != 0;
1456 // Caution: Job::Start must not complete synchronously.
1457 if (!system_only
&& had_dns_config_
&&
1458 !ResemblesMulticastDNSName(key_
.hostname
)) {
1465 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
1466 // the limits on |dispatcher_|. But in order to keep the number of WorkerPool
1467 // threads low, we will need to use an "inner" PrioritizedDispatcher with
1469 void StartProcTask() {
1470 DCHECK(!is_dns_running());
1471 proc_task_
= new ProcTask(
1473 resolver_
->proc_params_
,
1474 base::Bind(&Job::OnProcTaskComplete
, base::Unretained(this),
1475 base::TimeTicks::Now()),
1478 if (had_non_speculative_request_
)
1479 proc_task_
->set_had_non_speculative_request();
1480 // Start() could be called from within Resolve(), hence it must NOT directly
1481 // call OnProcTaskComplete, for example, on synchronous failure.
1482 proc_task_
->Start();
1485 // Called by ProcTask when it completes.
1486 void OnProcTaskComplete(base::TimeTicks start_time
,
1488 const AddressList
& addr_list
) {
1489 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
1490 tracked_objects::ScopedTracker
tracking_profile(
1491 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1492 "436634 HostResolverImpl::Job::OnProcTaskComplete"));
1494 DCHECK(is_proc_running());
1496 if (!resolver_
->resolved_known_ipv6_hostname_
&&
1498 key_
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
1499 if (key_
.hostname
== "www.google.com") {
1500 resolver_
->resolved_known_ipv6_hostname_
= true;
1501 bool got_ipv6_address
= false;
1502 for (size_t i
= 0; i
< addr_list
.size(); ++i
) {
1503 if (addr_list
[i
].GetFamily() == ADDRESS_FAMILY_IPV6
) {
1504 got_ipv6_address
= true;
1508 UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address
);
1512 if (dns_task_error_
!= OK
) {
1513 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
1514 if (net_error
== OK
) {
1515 DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration
);
1516 if ((dns_task_error_
== ERR_NAME_NOT_RESOLVED
) &&
1517 ResemblesNetBIOSName(key_
.hostname
)) {
1518 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS
);
1520 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS
);
1522 UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.ResolveError",
1523 std::abs(dns_task_error_
),
1524 GetAllErrorCodesForUma());
1525 resolver_
->OnDnsTaskResolve(dns_task_error_
);
1527 DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration
);
1528 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL
);
1532 base::TimeDelta ttl
=
1533 base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds
);
1534 if (net_error
== OK
)
1535 ttl
= base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds
);
1537 // Don't store the |ttl| in cache since it's not obtained from the server.
1539 HostCache::Entry(net_error
, MakeAddressListForRequest(addr_list
)),
1543 void StartDnsTask() {
1544 DCHECK(resolver_
->HaveDnsConfig());
1545 dns_task_
.reset(new DnsTask(resolver_
->dns_client_
.get(), key_
, this,
1548 dns_task_
->StartFirstTransaction();
1549 // Schedule a second transaction, if needed.
1550 if (dns_task_
->needs_two_transactions())
1554 void StartSecondDnsTransaction() {
1555 DCHECK(dns_task_
->needs_two_transactions());
1556 dns_task_
->StartSecondTransaction();
1559 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
1560 // deleted before this callback. In this case dns_task is deleted as well,
1561 // so we use it as indicator whether Job is still valid.
1562 void OnDnsTaskFailure(const base::WeakPtr
<DnsTask
>& dns_task
,
1563 base::TimeDelta duration
,
1565 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration
);
1567 if (dns_task
== NULL
)
1570 dns_task_error_
= net_error
;
1572 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so.
1573 // http://crbug.com/117655
1575 // TODO(szym): Some net errors indicate lack of connectivity. Starting
1576 // ProcTask in that case is a waste of time.
1577 if (resolver_
->fallback_to_proctask_
) {
1581 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL
);
1582 CompleteRequestsWithError(net_error
);
1587 // HostResolverImpl::DnsTask::Delegate implementation:
1589 void OnDnsTaskComplete(base::TimeTicks start_time
,
1591 const AddressList
& addr_list
,
1592 base::TimeDelta ttl
) override
{
1593 DCHECK(is_dns_running());
1595 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
1596 if (net_error
!= OK
) {
1597 OnDnsTaskFailure(dns_task_
->AsWeakPtr(), duration
, net_error
);
1600 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration
);
1601 // Log DNS lookups based on |address_family|.
1602 switch(key_
.address_family
) {
1603 case ADDRESS_FAMILY_IPV4
:
1604 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration
);
1606 case ADDRESS_FAMILY_IPV6
:
1607 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration
);
1609 case ADDRESS_FAMILY_UNSPECIFIED
:
1610 DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration
);
1614 UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS
);
1617 resolver_
->OnDnsTaskResolve(OK
);
1619 base::TimeDelta bounded_ttl
=
1620 std::max(ttl
, base::TimeDelta::FromSeconds(kMinimumTTLSeconds
));
1623 HostCache::Entry(net_error
, MakeAddressListForRequest(addr_list
), ttl
),
1627 void OnFirstDnsTransactionComplete() override
{
1628 DCHECK(dns_task_
->needs_two_transactions());
1629 DCHECK_EQ(dns_task_
->needs_another_transaction(), is_queued());
1630 // No longer need to occupy two dispatcher slots.
1631 ReduceToOneJobSlot();
1633 // We already have a job slot at the dispatcher, so if the second
1634 // transaction hasn't started, reuse it now instead of waiting in the queue
1635 // for the second slot.
1636 if (dns_task_
->needs_another_transaction())
1637 dns_task_
->StartSecondTransaction();
1640 // Performs Job's last rites. Completes all Requests. Deletes this.
1641 void CompleteRequests(const HostCache::Entry
& entry
,
1642 base::TimeDelta ttl
) {
1643 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
1644 tracked_objects::ScopedTracker
tracking_profile1(
1645 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1646 "436634 HostResolverImpl::Job::CompleteRequests1"));
1648 CHECK(resolver_
.get());
1650 // This job must be removed from resolver's |jobs_| now to make room for a
1651 // new job with the same key in case one of the OnComplete callbacks decides
1652 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1654 scoped_ptr
<Job
> self_deleter(this);
1656 resolver_
->RemoveJob(this);
1659 if (is_proc_running()) {
1660 DCHECK(!is_queued());
1661 proc_task_
->Cancel();
1666 // Signal dispatcher that a slot has opened.
1667 resolver_
->dispatcher_
->OnJobFinished();
1668 } else if (is_queued()) {
1669 resolver_
->dispatcher_
->Cancel(handle_
);
1673 if (num_active_requests() == 0) {
1674 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
);
1675 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1680 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1683 DCHECK(!requests_
.empty());
1685 if (entry
.error
== OK
) {
1686 // Record this histogram here, when we know the system has a valid DNS
1688 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1689 resolver_
->received_dns_config_
);
1692 bool did_complete
= (entry
.error
!= ERR_NETWORK_CHANGED
) &&
1693 (entry
.error
!= ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
);
1695 resolver_
->CacheResult(key_
, entry
, ttl
);
1697 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
1698 tracked_objects::ScopedTracker
tracking_profile2(
1699 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1700 "436634 HostResolverImpl::Job::CompleteRequests2"));
1702 // Complete all of the requests that were attached to the job.
1703 for (RequestsList::const_iterator it
= requests_
.begin();
1704 it
!= requests_
.end(); ++it
) {
1707 if (req
->was_canceled())
1710 DCHECK_EQ(this, req
->job());
1711 // Update the net log and notify registered observers.
1712 LogFinishRequest(req
->source_net_log(), req
->info(), entry
.error
);
1714 // Record effective total time from creation to completion.
1715 RecordTotalTime(had_dns_config_
, req
->info().is_speculative(),
1716 base::TimeTicks::Now() - req
->request_time());
1718 req
->OnComplete(entry
.error
, entry
.addrlist
);
1720 // Check if the resolver was destroyed as a result of running the
1721 // callback. If it was, we could continue, but we choose to bail.
1722 if (!resolver_
.get())
1727 // Convenience wrapper for CompleteRequests in case of failure.
1728 void CompleteRequestsWithError(int net_error
) {
1729 CompleteRequests(HostCache::Entry(net_error
, AddressList()),
1733 RequestPriority
priority() const {
1734 return priority_tracker_
.highest_priority();
1737 // Number of non-canceled requests in |requests_|.
1738 size_t num_active_requests() const {
1739 return priority_tracker_
.total_count();
1742 bool is_dns_running() const {
1743 return dns_task_
.get() != NULL
;
1746 bool is_proc_running() const {
1747 return proc_task_
.get() != NULL
;
1750 base::WeakPtr
<HostResolverImpl
> resolver_
;
1754 // Tracks the highest priority across |requests_|.
1755 PriorityTracker priority_tracker_
;
1757 bool had_non_speculative_request_
;
1759 // Distinguishes measurements taken while DnsClient was fully configured.
1760 bool had_dns_config_
;
1762 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1763 unsigned num_occupied_job_slots_
;
1765 // Result of DnsTask.
1766 int dns_task_error_
;
1768 const base::TimeTicks creation_time_
;
1769 base::TimeTicks priority_change_time_
;
1771 BoundNetLog net_log_
;
1773 // Resolves the host using a HostResolverProc.
1774 scoped_refptr
<ProcTask
> proc_task_
;
1776 // Resolves the host using a DnsTransaction.
1777 scoped_ptr
<DnsTask
> dns_task_
;
1779 // All Requests waiting for the result of this Job. Some can be canceled.
1780 RequestsList requests_
;
1782 // A handle used in |HostResolverImpl::dispatcher_|.
1783 PrioritizedDispatcher::Handle handle_
;
1786 //-----------------------------------------------------------------------------
1788 HostResolverImpl::ProcTaskParams::ProcTaskParams(
1789 HostResolverProc
* resolver_proc
,
1790 size_t max_retry_attempts
)
1791 : resolver_proc(resolver_proc
),
1792 max_retry_attempts(max_retry_attempts
),
1793 unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)),
1795 // Maximum of 4 retry attempts for host resolution.
1796 static const size_t kDefaultMaxRetryAttempts
= 4u;
1797 if (max_retry_attempts
== HostResolver::kDefaultRetryAttempts
)
1798 max_retry_attempts
= kDefaultMaxRetryAttempts
;
1801 HostResolverImpl::ProcTaskParams::~ProcTaskParams() {}
1803 HostResolverImpl::HostResolverImpl(const Options
& options
, NetLog
* net_log
)
1804 : max_queued_jobs_(0),
1805 proc_params_(NULL
, options
.max_retry_attempts
),
1807 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED
),
1808 received_dns_config_(false),
1809 num_dns_failures_(0),
1810 probe_ipv6_support_(true),
1811 use_local_ipv6_(false),
1812 resolved_known_ipv6_hostname_(false),
1813 additional_resolver_flags_(0),
1814 fallback_to_proctask_(true),
1815 weak_ptr_factory_(this),
1816 probe_weak_ptr_factory_(this) {
1817 if (options
.enable_caching
)
1818 cache_
= HostCache::CreateDefaultCache();
1820 PrioritizedDispatcher::Limits job_limits
= options
.GetDispatcherLimits();
1821 dispatcher_
.reset(new PrioritizedDispatcher(job_limits
));
1822 max_queued_jobs_
= job_limits
.total_jobs
* 100u;
1824 DCHECK_GE(dispatcher_
->num_priorities(), static_cast<size_t>(NUM_PRIORITIES
));
1827 EnsureWinsockInit();
1829 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
1830 new LoopbackProbeJob(weak_ptr_factory_
.GetWeakPtr());
1832 NetworkChangeNotifier::AddIPAddressObserver(this);
1833 NetworkChangeNotifier::AddDNSObserver(this);
1834 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
1835 !defined(OS_ANDROID)
1836 EnsureDnsReloaderInit();
1840 DnsConfig dns_config
;
1841 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
1842 received_dns_config_
= dns_config
.IsValid();
1843 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
1844 use_local_ipv6_
= !dns_config
.IsValid() || dns_config
.use_local_ipv6
;
1847 fallback_to_proctask_
= !ConfigureAsyncDnsNoFallbackFieldTrial();
1850 HostResolverImpl::~HostResolverImpl() {
1851 // Prevent the dispatcher from starting new jobs.
1852 dispatcher_
->SetLimitsToZero();
1853 // It's now safe for Jobs to call KillDsnTask on destruction, because
1854 // OnJobComplete will not start any new jobs.
1855 STLDeleteValues(&jobs_
);
1857 NetworkChangeNotifier::RemoveIPAddressObserver(this);
1858 NetworkChangeNotifier::RemoveDNSObserver(this);
1861 void HostResolverImpl::SetMaxQueuedJobs(size_t value
) {
1862 DCHECK_EQ(0u, dispatcher_
->num_queued_jobs());
1863 DCHECK_GT(value
, 0u);
1864 max_queued_jobs_
= value
;
1867 int HostResolverImpl::Resolve(const RequestInfo
& info
,
1868 RequestPriority priority
,
1869 AddressList
* addresses
,
1870 const CompletionCallback
& callback
,
1871 RequestHandle
* out_req
,
1872 const BoundNetLog
& source_net_log
) {
1874 DCHECK(CalledOnValidThread());
1875 DCHECK_EQ(false, callback
.is_null());
1877 // Check that the caller supplied a valid hostname to resolve.
1878 std::string labeled_hostname
;
1879 if (!DNSDomainFromDot(info
.hostname(), &labeled_hostname
))
1880 return ERR_NAME_NOT_RESOLVED
;
1882 LogStartRequest(source_net_log
, info
);
1884 // Build a key that identifies the request in the cache and in the
1885 // outstanding jobs map.
1886 Key key
= GetEffectiveKeyForRequest(info
, source_net_log
);
1888 int rv
= ResolveHelper(key
, info
, addresses
, source_net_log
);
1889 if (rv
!= ERR_DNS_CACHE_MISS
) {
1890 LogFinishRequest(source_net_log
, info
, rv
);
1891 RecordTotalTime(HaveDnsConfig(), info
.is_speculative(), base::TimeDelta());
1895 // Next we need to attach our request to a "job". This job is responsible for
1896 // calling "getaddrinfo(hostname)" on a worker thread.
1898 JobMap::iterator jobit
= jobs_
.find(key
);
1900 if (jobit
== jobs_
.end()) {
1902 new Job(weak_ptr_factory_
.GetWeakPtr(), key
, priority
, source_net_log
);
1903 job
->Schedule(false);
1905 // Check for queue overflow.
1906 if (dispatcher_
->num_queued_jobs() > max_queued_jobs_
) {
1907 Job
* evicted
= static_cast<Job
*>(dispatcher_
->EvictOldestLowest());
1909 evicted
->OnEvicted(); // Deletes |evicted|.
1910 if (evicted
== job
) {
1911 rv
= ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
;
1912 LogFinishRequest(source_net_log
, info
, rv
);
1916 jobs_
.insert(jobit
, std::make_pair(key
, job
));
1918 job
= jobit
->second
;
1921 // Can't complete synchronously. Create and attach request.
1922 scoped_ptr
<Request
> req(new Request(
1923 source_net_log
, info
, priority
, callback
, addresses
));
1925 *out_req
= reinterpret_cast<RequestHandle
>(req
.get());
1927 job
->AddRequest(req
.Pass());
1928 // Completion happens during Job::CompleteRequests().
1929 return ERR_IO_PENDING
;
1932 int HostResolverImpl::ResolveHelper(const Key
& key
,
1933 const RequestInfo
& info
,
1934 AddressList
* addresses
,
1935 const BoundNetLog
& source_net_log
) {
1936 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
1937 // On Windows it gives the default interface's address, whereas on Linux it
1938 // gives an error. We will make it fail on all platforms for consistency.
1939 if (info
.hostname().empty() || info
.hostname().size() > kMaxHostLength
)
1940 return ERR_NAME_NOT_RESOLVED
;
1942 int net_error
= ERR_UNEXPECTED
;
1943 if (ResolveAsIP(key
, info
, &net_error
, addresses
))
1945 if (ServeFromCache(key
, info
, &net_error
, addresses
)) {
1946 source_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT
);
1949 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
1950 // http://crbug.com/117655
1951 if (ServeFromHosts(key
, info
, addresses
)) {
1952 source_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT
);
1955 return ERR_DNS_CACHE_MISS
;
1958 int HostResolverImpl::ResolveFromCache(const RequestInfo
& info
,
1959 AddressList
* addresses
,
1960 const BoundNetLog
& source_net_log
) {
1961 DCHECK(CalledOnValidThread());
1964 // Update the net log and notify registered observers.
1965 LogStartRequest(source_net_log
, info
);
1967 Key key
= GetEffectiveKeyForRequest(info
, source_net_log
);
1969 int rv
= ResolveHelper(key
, info
, addresses
, source_net_log
);
1970 LogFinishRequest(source_net_log
, info
, rv
);
1974 void HostResolverImpl::CancelRequest(RequestHandle req_handle
) {
1975 DCHECK(CalledOnValidThread());
1976 Request
* req
= reinterpret_cast<Request
*>(req_handle
);
1978 Job
* job
= req
->job();
1980 job
->CancelRequest(req
);
1983 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family
) {
1984 DCHECK(CalledOnValidThread());
1985 default_address_family_
= address_family
;
1986 probe_ipv6_support_
= false;
1989 AddressFamily
HostResolverImpl::GetDefaultAddressFamily() const {
1990 return default_address_family_
;
1993 void HostResolverImpl::SetDnsClientEnabled(bool enabled
) {
1994 DCHECK(CalledOnValidThread());
1995 #if defined(ENABLE_BUILT_IN_DNS)
1996 if (enabled
&& !dns_client_
) {
1997 SetDnsClient(DnsClient::CreateClient(net_log_
));
1998 } else if (!enabled
&& dns_client_
) {
1999 SetDnsClient(scoped_ptr
<DnsClient
>());
2004 HostCache
* HostResolverImpl::GetHostCache() {
2005 return cache_
.get();
2008 base::Value
* HostResolverImpl::GetDnsConfigAsValue() const {
2009 // Check if async DNS is disabled.
2010 if (!dns_client_
.get())
2013 // Check if async DNS is enabled, but we currently have no configuration
2015 const DnsConfig
* dns_config
= dns_client_
->GetConfig();
2016 if (dns_config
== NULL
)
2017 return new base::DictionaryValue();
2019 return dns_config
->ToValue();
2022 bool HostResolverImpl::ResolveAsIP(const Key
& key
,
2023 const RequestInfo
& info
,
2025 AddressList
* addresses
) {
2028 IPAddressNumber ip_number
;
2029 if (!ParseIPLiteralToNumber(key
.hostname
, &ip_number
))
2032 DCHECK_EQ(key
.host_resolver_flags
&
2033 ~(HOST_RESOLVER_CANONNAME
| HOST_RESOLVER_LOOPBACK_ONLY
|
2034 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
),
2035 0) << " Unhandled flag";
2038 AddressFamily family
= GetAddressFamily(ip_number
);
2039 if (family
== ADDRESS_FAMILY_IPV6
&&
2040 !probe_ipv6_support_
&&
2041 default_address_family_
== ADDRESS_FAMILY_IPV4
) {
2042 // Don't return IPv6 addresses if default address family is set to IPv4,
2043 // and probes are disabled.
2044 *net_error
= ERR_NAME_NOT_RESOLVED
;
2045 } else if (key
.address_family
!= ADDRESS_FAMILY_UNSPECIFIED
&&
2046 key
.address_family
!= family
) {
2047 // Don't return IPv6 addresses for IPv4 queries, and vice versa.
2048 *net_error
= ERR_NAME_NOT_RESOLVED
;
2050 *addresses
= AddressList::CreateFromIPAddress(ip_number
, info
.port());
2051 if (key
.host_resolver_flags
& HOST_RESOLVER_CANONNAME
)
2052 addresses
->SetDefaultCanonicalName();
2057 bool HostResolverImpl::ServeFromCache(const Key
& key
,
2058 const RequestInfo
& info
,
2060 AddressList
* addresses
) {
2063 if (!info
.allow_cached_response() || !cache_
.get())
2066 const HostCache::Entry
* cache_entry
= cache_
->Lookup(
2067 key
, base::TimeTicks::Now());
2071 *net_error
= cache_entry
->error
;
2072 if (*net_error
== OK
) {
2073 if (cache_entry
->has_ttl())
2074 RecordTTL(cache_entry
->ttl
);
2075 *addresses
= EnsurePortOnAddressList(cache_entry
->addrlist
, info
.port());
2080 bool HostResolverImpl::ServeFromHosts(const Key
& key
,
2081 const RequestInfo
& info
,
2082 AddressList
* addresses
) {
2084 if (!HaveDnsConfig())
2088 // HOSTS lookups are case-insensitive.
2089 std::string hostname
= base::StringToLowerASCII(key
.hostname
);
2091 const DnsHosts
& hosts
= dns_client_
->GetConfig()->hosts
;
2093 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2094 // (glibc and c-ares) return the first matching line. We have more
2095 // flexibility, but lose implicit ordering.
2096 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2098 if (key
.address_family
== ADDRESS_FAMILY_IPV6
||
2099 key
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
2100 DnsHosts::const_iterator it
= hosts
.find(
2101 DnsHostsKey(hostname
, ADDRESS_FAMILY_IPV6
));
2102 if (it
!= hosts
.end())
2103 addresses
->push_back(IPEndPoint(it
->second
, info
.port()));
2106 if (key
.address_family
== ADDRESS_FAMILY_IPV4
||
2107 key
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
2108 DnsHosts::const_iterator it
= hosts
.find(
2109 DnsHostsKey(hostname
, ADDRESS_FAMILY_IPV4
));
2110 if (it
!= hosts
.end())
2111 addresses
->push_back(IPEndPoint(it
->second
, info
.port()));
2114 // If got only loopback addresses and the family was restricted, resolve
2115 // again, without restrictions. See SystemHostResolverCall for rationale.
2116 if ((key
.host_resolver_flags
&
2117 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
) &&
2118 IsAllIPv4Loopback(*addresses
)) {
2120 new_key
.address_family
= ADDRESS_FAMILY_UNSPECIFIED
;
2121 new_key
.host_resolver_flags
&=
2122 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
;
2123 return ServeFromHosts(new_key
, info
, addresses
);
2125 return !addresses
->empty();
2128 void HostResolverImpl::CacheResult(const Key
& key
,
2129 const HostCache::Entry
& entry
,
2130 base::TimeDelta ttl
) {
2132 cache_
->Set(key
, entry
, base::TimeTicks::Now(), ttl
);
2135 void HostResolverImpl::RemoveJob(Job
* job
) {
2137 JobMap::iterator it
= jobs_
.find(job
->key());
2138 if (it
!= jobs_
.end() && it
->second
== job
)
2142 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result
) {
2144 additional_resolver_flags_
|= HOST_RESOLVER_LOOPBACK_ONLY
;
2146 additional_resolver_flags_
&= ~HOST_RESOLVER_LOOPBACK_ONLY
;
2150 HostResolverImpl::Key
HostResolverImpl::GetEffectiveKeyForRequest(
2151 const RequestInfo
& info
, const BoundNetLog
& net_log
) const {
2152 HostResolverFlags effective_flags
=
2153 info
.host_resolver_flags() | additional_resolver_flags_
;
2154 AddressFamily effective_address_family
= info
.address_family();
2156 if (info
.address_family() == ADDRESS_FAMILY_UNSPECIFIED
) {
2157 if (probe_ipv6_support_
&& !use_local_ipv6_
) {
2158 // Google DNS address.
2159 const uint8 kIPv6Address
[] =
2160 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
2161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
2162 IPAddressNumber
address(kIPv6Address
,
2163 kIPv6Address
+ arraysize(kIPv6Address
));
2164 BoundNetLog probe_net_log
= BoundNetLog::Make(
2165 net_log
.net_log(), NetLog::SOURCE_IPV6_REACHABILITY_CHECK
);
2166 probe_net_log
.BeginEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK
,
2167 net_log
.source().ToEventParametersCallback());
2168 bool rv6
= IsGloballyReachable(address
, probe_net_log
);
2169 probe_net_log
.EndEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK
);
2171 net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_SUPPORTED
);
2174 UMA_HISTOGRAM_BOOLEAN("Net.IPv6ConnectSuccessMatch",
2175 default_address_family_
== ADDRESS_FAMILY_UNSPECIFIED
);
2177 UMA_HISTOGRAM_BOOLEAN("Net.IPv6ConnectFailureMatch",
2178 default_address_family_
!= ADDRESS_FAMILY_UNSPECIFIED
);
2180 effective_address_family
= ADDRESS_FAMILY_IPV4
;
2181 effective_flags
|= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
;
2184 effective_address_family
= default_address_family_
;
2188 return Key(info
.hostname(), effective_address_family
, effective_flags
);
2191 void HostResolverImpl::AbortAllInProgressJobs() {
2192 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2193 // first collect and remove all running jobs from |jobs_|.
2194 ScopedVector
<Job
> jobs_to_abort
;
2195 for (JobMap::iterator it
= jobs_
.begin(); it
!= jobs_
.end(); ) {
2196 Job
* job
= it
->second
;
2197 if (job
->is_running()) {
2198 jobs_to_abort
.push_back(job
);
2201 DCHECK(job
->is_queued());
2206 // Pause the dispatcher so it won't start any new dispatcher jobs while
2207 // aborting the old ones. This is needed so that it won't start the second
2208 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2210 PrioritizedDispatcher::Limits limits
= dispatcher_
->GetLimits();
2211 dispatcher_
->SetLimits(
2212 PrioritizedDispatcher::Limits(limits
.reserved_slots
.size(), 0));
2214 // Life check to bail once |this| is deleted.
2215 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2218 for (size_t i
= 0; self
.get() && i
< jobs_to_abort
.size(); ++i
) {
2219 jobs_to_abort
[i
]->Abort();
2220 jobs_to_abort
[i
] = NULL
;
2224 dispatcher_
->SetLimits(limits
);
2227 void HostResolverImpl::AbortDnsTasks() {
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 if the DnsConfig just changed.
2231 PrioritizedDispatcher::Limits limits
= dispatcher_
->GetLimits();
2232 dispatcher_
->SetLimits(
2233 PrioritizedDispatcher::Limits(limits
.reserved_slots
.size(), 0));
2235 for (JobMap::iterator it
= jobs_
.begin(); it
!= jobs_
.end(); ++it
)
2236 it
->second
->AbortDnsTask();
2237 dispatcher_
->SetLimits(limits
);
2240 void HostResolverImpl::TryServingAllJobsFromHosts() {
2241 if (!HaveDnsConfig())
2244 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2245 // http://crbug.com/117655
2247 // Life check to bail once |this| is deleted.
2248 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2250 for (JobMap::iterator it
= jobs_
.begin(); self
.get() && it
!= jobs_
.end();) {
2251 Job
* job
= it
->second
;
2253 // This could remove |job| from |jobs_|, but iterator will remain valid.
2254 job
->ServeFromHosts();
2258 void HostResolverImpl::OnIPAddressChanged() {
2259 resolved_known_ipv6_hostname_
= false;
2260 // Abandon all ProbeJobs.
2261 probe_weak_ptr_factory_
.InvalidateWeakPtrs();
2264 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
2265 new LoopbackProbeJob(probe_weak_ptr_factory_
.GetWeakPtr());
2267 AbortAllInProgressJobs();
2268 // |this| may be deleted inside AbortAllInProgressJobs().
2271 void HostResolverImpl::OnDNSChanged() {
2272 DnsConfig dns_config
;
2273 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
2276 net_log_
->AddGlobalEntry(
2277 NetLog::TYPE_DNS_CONFIG_CHANGED
,
2278 base::Bind(&NetLogDnsConfigCallback
, &dns_config
));
2281 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
2282 received_dns_config_
= dns_config
.IsValid();
2283 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2284 use_local_ipv6_
= !dns_config
.IsValid() || dns_config
.use_local_ipv6
;
2286 num_dns_failures_
= 0;
2288 // We want a new DnsSession in place, before we Abort running Jobs, so that
2289 // the newly started jobs use the new config.
2290 if (dns_client_
.get()) {
2291 dns_client_
->SetConfig(dns_config
);
2292 if (dns_client_
->GetConfig())
2293 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2296 // If the DNS server has changed, existing cached info could be wrong so we
2297 // have to drop our internal cache :( Note that OS level DNS caches, such
2298 // as NSCD's cache should be dropped automatically by the OS when
2299 // resolv.conf changes so we don't need to do anything to clear that cache.
2303 // Life check to bail once |this| is deleted.
2304 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2306 // Existing jobs will have been sent to the original server so they need to
2308 AbortAllInProgressJobs();
2310 // |this| may be deleted inside AbortAllInProgressJobs().
2312 TryServingAllJobsFromHosts();
2315 bool HostResolverImpl::HaveDnsConfig() const {
2316 // Use DnsClient only if it's fully configured and there is no override by
2317 // ScopedDefaultHostResolverProc.
2318 // The alternative is to use NetworkChangeNotifier to override DnsConfig,
2319 // but that would introduce construction order requirements for NCN and SDHRP.
2320 return (dns_client_
.get() != NULL
) && (dns_client_
->GetConfig() != NULL
) &&
2321 !(proc_params_
.resolver_proc
.get() == NULL
&&
2322 HostResolverProc::GetDefault() != NULL
);
2325 void HostResolverImpl::OnDnsTaskResolve(int net_error
) {
2326 DCHECK(dns_client_
);
2327 if (net_error
== OK
) {
2328 num_dns_failures_
= 0;
2331 ++num_dns_failures_
;
2332 if (num_dns_failures_
< kMaximumDnsFailures
)
2335 // Disable DnsClient until the next DNS change. Must be done before aborting
2336 // DnsTasks, since doing so may start new jobs.
2337 dns_client_
->SetConfig(DnsConfig());
2339 // Switch jobs with active DnsTasks over to using ProcTasks.
2342 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
2343 UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.DnsClientDisabledReason",
2344 std::abs(net_error
),
2345 GetAllErrorCodesForUma());
2348 void HostResolverImpl::SetDnsClient(scoped_ptr
<DnsClient
> dns_client
) {
2349 // DnsClient and config must be updated before aborting DnsTasks, since doing
2350 // so may start new jobs.
2351 dns_client_
= dns_client
.Pass();
2352 if (dns_client_
&& !dns_client_
->GetConfig() &&
2353 num_dns_failures_
< kMaximumDnsFailures
) {
2354 DnsConfig dns_config
;
2355 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
2356 dns_client_
->SetConfig(dns_config
);
2357 num_dns_failures_
= 0;
2358 if (dns_client_
->GetConfig())
2359 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);