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/stl_util.h"
28 #include "base/strings/string_util.h"
29 #include "base/strings/utf_string_conversions.h"
30 #include "base/threading/worker_pool.h"
31 #include "base/time/time.h"
32 #include "base/values.h"
33 #include "net/base/address_family.h"
34 #include "net/base/address_list.h"
35 #include "net/base/dns_reloader.h"
36 #include "net/base/dns_util.h"
37 #include "net/base/host_port_pair.h"
38 #include "net/base/net_errors.h"
39 #include "net/base/net_log.h"
40 #include "net/base/net_util.h"
41 #include "net/dns/address_sorter.h"
42 #include "net/dns/dns_client.h"
43 #include "net/dns/dns_config_service.h"
44 #include "net/dns/dns_protocol.h"
45 #include "net/dns/dns_response.h"
46 #include "net/dns/dns_transaction.h"
47 #include "net/dns/host_resolver_proc.h"
48 #include "net/socket/client_socket_factory.h"
49 #include "net/udp/datagram_client_socket.h"
52 #include "net/base/winsock_init.h"
59 // Limit the size of hostnames that will be resolved to combat issues in
60 // some platform's resolvers.
61 const size_t kMaxHostLength
= 4096;
63 // Default TTL for successful resolutions with ProcTask.
64 const unsigned kCacheEntryTTLSeconds
= 60;
66 // Default TTL for unsuccessful resolutions with ProcTask.
67 const unsigned kNegativeCacheEntryTTLSeconds
= 0;
69 // Minimum TTL for successful resolutions with DnsTask.
70 const unsigned kMinimumTTLSeconds
= kCacheEntryTTLSeconds
;
72 // We use a separate histogram name for each platform to facilitate the
73 // display of error codes by their symbolic name (since each platform has
74 // different mappings).
75 const char kOSErrorsForGetAddrinfoHistogramName
[] =
77 "Net.OSErrorsForGetAddrinfo_Win";
78 #elif defined(OS_MACOSX)
79 "Net.OSErrorsForGetAddrinfo_Mac";
80 #elif defined(OS_LINUX)
81 "Net.OSErrorsForGetAddrinfo_Linux";
83 "Net.OSErrorsForGetAddrinfo";
86 // Gets a list of the likely error codes that getaddrinfo() can return
87 // (non-exhaustive). These are the error codes that we will track via
89 std::vector
<int> GetAllGetAddrinfoOSErrors() {
92 #if !defined(OS_FREEBSD)
93 #if !defined(OS_ANDROID)
94 // EAI_ADDRFAMILY has been declared obsolete in Android's and
98 // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h.
110 #elif defined(OS_WIN)
111 // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
112 WSA_NOT_ENOUGH_MEMORY
,
122 // The following are not in doc, but might be to appearing in results :-(.
127 // Ensure all errors are positive, as histogram only tracks positive values.
128 for (size_t i
= 0; i
< arraysize(os_errors
); ++i
) {
129 os_errors
[i
] = std::abs(os_errors
[i
]);
132 return base::CustomHistogram::ArrayToCustomRanges(os_errors
,
133 arraysize(os_errors
));
136 enum DnsResolveStatus
{
137 RESOLVE_STATUS_DNS_SUCCESS
= 0,
138 RESOLVE_STATUS_PROC_SUCCESS
,
140 RESOLVE_STATUS_SUSPECT_NETBIOS
,
144 void UmaAsyncDnsResolveStatus(DnsResolveStatus result
) {
145 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
150 bool ResemblesNetBIOSName(const std::string
& hostname
) {
151 return (hostname
.size() < 16) && (hostname
.find('.') == std::string::npos
);
154 // True if |hostname| ends with either ".local" or ".local.".
155 bool ResemblesMulticastDNSName(const std::string
& hostname
) {
156 DCHECK(!hostname
.empty());
157 const char kSuffix
[] = ".local.";
158 const size_t kSuffixLen
= sizeof(kSuffix
) - 1;
159 const size_t kSuffixLenTrimmed
= kSuffixLen
- 1;
160 if (hostname
[hostname
.size() - 1] == '.') {
161 return hostname
.size() > kSuffixLen
&&
162 !hostname
.compare(hostname
.size() - kSuffixLen
, kSuffixLen
, kSuffix
);
164 return hostname
.size() > kSuffixLenTrimmed
&&
165 !hostname
.compare(hostname
.size() - kSuffixLenTrimmed
, kSuffixLenTrimmed
,
166 kSuffix
, kSuffixLenTrimmed
);
169 // Attempts to connect a UDP socket to |dest|:53.
170 bool IsGloballyReachable(const IPAddressNumber
& dest
,
171 const BoundNetLog
& net_log
) {
172 scoped_ptr
<DatagramClientSocket
> socket(
173 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
174 DatagramSocket::DEFAULT_BIND
,
178 int rv
= socket
->Connect(IPEndPoint(dest
, 53));
182 rv
= socket
->GetLocalAddress(&endpoint
);
185 DCHECK(endpoint
.GetFamily() == ADDRESS_FAMILY_IPV6
);
186 const IPAddressNumber
& address
= endpoint
.address();
187 bool is_link_local
= (address
[0] == 0xFE) && ((address
[1] & 0xC0) == 0x80);
190 const uint8 kTeredoPrefix
[] = { 0x20, 0x01, 0, 0 };
191 bool is_teredo
= std::equal(kTeredoPrefix
,
192 kTeredoPrefix
+ arraysize(kTeredoPrefix
),
199 // Provide a common macro to simplify code and readability. We must use a
200 // macro as the underlying HISTOGRAM macro creates static variables.
201 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \
202 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100)
204 // A macro to simplify code and readability.
205 #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
207 switch (priority) { \
208 case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \
209 case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \
210 case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \
211 case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \
212 case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \
213 default: NOTREACHED(); break; \
215 DNS_HISTOGRAM(basename, time); \
218 // Record time from Request creation until a valid DNS response.
219 void RecordTotalTime(bool had_dns_config
,
221 base::TimeDelta duration
) {
222 if (had_dns_config
) {
224 DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration
);
226 DNS_HISTOGRAM("AsyncDNS.TotalTime", duration
);
230 DNS_HISTOGRAM("DNS.TotalTime_speculative", duration
);
232 DNS_HISTOGRAM("DNS.TotalTime", duration
);
237 void RecordTTL(base::TimeDelta ttl
) {
238 UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl
,
239 base::TimeDelta::FromSeconds(1),
240 base::TimeDelta::FromDays(1), 100);
243 bool ConfigureAsyncDnsNoFallbackFieldTrial() {
244 const bool kDefault
= false;
246 // Configure the AsyncDns field trial as follows:
247 // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
248 // groups AsyncDnsA and AsyncDnsB: return false,
249 // groups SystemDnsA and SystemDnsB: return false,
250 // otherwise (trial absent): return default.
251 std::string group_name
= base::FieldTrialList::FindFullName("AsyncDns");
252 if (!group_name
.empty())
253 return StartsWithASCII(group_name
, "AsyncDnsNoFallback", false);
257 //-----------------------------------------------------------------------------
259 AddressList
EnsurePortOnAddressList(const AddressList
& list
, uint16 port
) {
260 if (list
.empty() || list
.front().port() == port
)
262 return AddressList::CopyWithPort(list
, port
);
265 // Returns true if |addresses| contains only IPv4 loopback addresses.
266 bool IsAllIPv4Loopback(const AddressList
& addresses
) {
267 for (unsigned i
= 0; i
< addresses
.size(); ++i
) {
268 const IPAddressNumber
& address
= addresses
[i
].address();
269 switch (addresses
[i
].GetFamily()) {
270 case ADDRESS_FAMILY_IPV4
:
271 if (address
[0] != 127)
274 case ADDRESS_FAMILY_IPV6
:
284 // Creates NetLog parameters when the resolve failed.
285 base::Value
* NetLogProcTaskFailedCallback(uint32 attempt_number
,
288 NetLog::LogLevel
/* log_level */) {
289 base::DictionaryValue
* dict
= new base::DictionaryValue();
291 dict
->SetInteger("attempt_number", attempt_number
);
293 dict
->SetInteger("net_error", net_error
);
296 dict
->SetInteger("os_error", os_error
);
297 #if defined(OS_POSIX)
298 dict
->SetString("os_error_string", gai_strerror(os_error
));
299 #elif defined(OS_WIN)
300 // Map the error code to a human-readable string.
301 LPWSTR error_string
= NULL
;
302 int size
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
303 FORMAT_MESSAGE_FROM_SYSTEM
,
304 0, // Use the internal message table.
306 0, // Use default language.
307 (LPWSTR
)&error_string
,
309 0); // Arguments (unused).
310 dict
->SetString("os_error_string", base::WideToUTF8(error_string
));
311 LocalFree(error_string
);
318 // Creates NetLog parameters when the DnsTask failed.
319 base::Value
* NetLogDnsTaskFailedCallback(int net_error
,
321 NetLog::LogLevel
/* log_level */) {
322 base::DictionaryValue
* dict
= new base::DictionaryValue();
323 dict
->SetInteger("net_error", net_error
);
325 dict
->SetInteger("dns_error", dns_error
);
329 // Creates NetLog parameters containing the information in a RequestInfo object,
330 // along with the associated NetLog::Source.
331 base::Value
* NetLogRequestInfoCallback(const NetLog::Source
& source
,
332 const HostResolver::RequestInfo
* info
,
333 NetLog::LogLevel
/* log_level */) {
334 base::DictionaryValue
* dict
= new base::DictionaryValue();
335 source
.AddToEventParameters(dict
);
337 dict
->SetString("host", info
->host_port_pair().ToString());
338 dict
->SetInteger("address_family",
339 static_cast<int>(info
->address_family()));
340 dict
->SetBoolean("allow_cached_response", info
->allow_cached_response());
341 dict
->SetBoolean("is_speculative", info
->is_speculative());
345 // Creates NetLog parameters for the creation of a HostResolverImpl::Job.
346 base::Value
* NetLogJobCreationCallback(const NetLog::Source
& source
,
347 const std::string
* host
,
348 NetLog::LogLevel
/* log_level */) {
349 base::DictionaryValue
* dict
= new base::DictionaryValue();
350 source
.AddToEventParameters(dict
);
351 dict
->SetString("host", *host
);
355 // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events.
356 base::Value
* NetLogJobAttachCallback(const NetLog::Source
& source
,
357 RequestPriority priority
,
358 NetLog::LogLevel
/* log_level */) {
359 base::DictionaryValue
* dict
= new base::DictionaryValue();
360 source
.AddToEventParameters(dict
);
361 dict
->SetString("priority", RequestPriorityToString(priority
));
365 // Creates NetLog parameters for the DNS_CONFIG_CHANGED event.
366 base::Value
* NetLogDnsConfigCallback(const DnsConfig
* config
,
367 NetLog::LogLevel
/* log_level */) {
368 return config
->ToValue();
371 // The logging routines are defined here because some requests are resolved
372 // without a Request object.
374 // Logs when a request has just been started.
375 void LogStartRequest(const BoundNetLog
& source_net_log
,
376 const BoundNetLog
& request_net_log
,
377 const HostResolver::RequestInfo
& info
) {
378 source_net_log
.BeginEvent(
379 NetLog::TYPE_HOST_RESOLVER_IMPL
,
380 request_net_log
.source().ToEventParametersCallback());
382 request_net_log
.BeginEvent(
383 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
,
384 base::Bind(&NetLogRequestInfoCallback
, source_net_log
.source(), &info
));
387 // Logs when a request has just completed (before its callback is run).
388 void LogFinishRequest(const BoundNetLog
& source_net_log
,
389 const BoundNetLog
& request_net_log
,
390 const HostResolver::RequestInfo
& info
,
392 request_net_log
.EndEventWithNetErrorCode(
393 NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
, net_error
);
394 source_net_log
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL
);
397 // Logs when a request has been cancelled.
398 void LogCancelRequest(const BoundNetLog
& source_net_log
,
399 const BoundNetLog
& request_net_log
,
400 const HostResolverImpl::RequestInfo
& info
) {
401 request_net_log
.AddEvent(NetLog::TYPE_CANCELLED
);
402 request_net_log
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST
);
403 source_net_log
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL
);
406 //-----------------------------------------------------------------------------
408 // Keeps track of the highest priority.
409 class PriorityTracker
{
411 explicit PriorityTracker(RequestPriority initial_priority
)
412 : highest_priority_(initial_priority
), total_count_(0) {
413 memset(counts_
, 0, sizeof(counts_
));
416 RequestPriority
highest_priority() const {
417 return highest_priority_
;
420 size_t total_count() const {
424 void Add(RequestPriority req_priority
) {
426 ++counts_
[req_priority
];
427 if (highest_priority_
< req_priority
)
428 highest_priority_
= req_priority
;
431 void Remove(RequestPriority req_priority
) {
432 DCHECK_GT(total_count_
, 0u);
433 DCHECK_GT(counts_
[req_priority
], 0u);
435 --counts_
[req_priority
];
437 for (i
= highest_priority_
; i
> MINIMUM_PRIORITY
&& !counts_
[i
]; --i
);
438 highest_priority_
= static_cast<RequestPriority
>(i
);
440 // In absence of requests, default to MINIMUM_PRIORITY.
441 if (total_count_
== 0)
442 DCHECK_EQ(MINIMUM_PRIORITY
, highest_priority_
);
446 RequestPriority highest_priority_
;
448 size_t counts_
[NUM_PRIORITIES
];
453 //-----------------------------------------------------------------------------
455 const unsigned HostResolverImpl::kMaximumDnsFailures
= 16;
457 // Holds the data for a request that could not be completed synchronously.
458 // It is owned by a Job. Canceled Requests are only marked as canceled rather
459 // than removed from the Job's |requests_| list.
460 class HostResolverImpl::Request
{
462 Request(const BoundNetLog
& source_net_log
,
463 const BoundNetLog
& request_net_log
,
464 const RequestInfo
& info
,
465 RequestPriority priority
,
466 const CompletionCallback
& callback
,
467 AddressList
* addresses
)
468 : source_net_log_(source_net_log
),
469 request_net_log_(request_net_log
),
474 addresses_(addresses
),
475 request_time_(base::TimeTicks::Now()) {}
477 // Mark the request as canceled.
478 void MarkAsCanceled() {
484 bool was_canceled() const {
485 return callback_
.is_null();
488 void set_job(Job
* job
) {
490 // Identify which job the request is waiting on.
494 // Prepare final AddressList and call completion callback.
495 void OnComplete(int error
, const AddressList
& addr_list
) {
496 DCHECK(!was_canceled());
498 *addresses_
= EnsurePortOnAddressList(addr_list
, info_
.port());
499 CompletionCallback callback
= callback_
;
508 // NetLog for the source, passed in HostResolver::Resolve.
509 const BoundNetLog
& source_net_log() {
510 return source_net_log_
;
513 // NetLog for this request.
514 const BoundNetLog
& request_net_log() {
515 return request_net_log_
;
518 const RequestInfo
& info() const {
522 RequestPriority
priority() const { return priority_
; }
524 base::TimeTicks
request_time() const { return request_time_
; }
527 BoundNetLog source_net_log_
;
528 BoundNetLog request_net_log_
;
530 // The request info that started the request.
531 const RequestInfo info_
;
533 // TODO(akalin): Support reprioritization.
534 const RequestPriority priority_
;
536 // The resolve job that this request is dependent on.
539 // The user's callback to invoke when the request completes.
540 CompletionCallback callback_
;
542 // The address list to save result into.
543 AddressList
* addresses_
;
545 const base::TimeTicks request_time_
;
547 DISALLOW_COPY_AND_ASSIGN(Request
);
550 //------------------------------------------------------------------------------
552 // Calls HostResolverProc on the WorkerPool. Performs retries if necessary.
554 // Whenever we try to resolve the host, we post a delayed task to check if host
555 // resolution (OnLookupComplete) is completed or not. If the original attempt
556 // hasn't completed, then we start another attempt for host resolution. We take
557 // the results from the first attempt that finishes and ignore the results from
558 // all other attempts.
560 // TODO(szym): Move to separate source file for testing and mocking.
562 class HostResolverImpl::ProcTask
563 : public base::RefCountedThreadSafe
<HostResolverImpl::ProcTask
> {
565 typedef base::Callback
<void(int net_error
,
566 const AddressList
& addr_list
)> Callback
;
568 ProcTask(const Key
& key
,
569 const ProcTaskParams
& params
,
570 const Callback
& callback
,
571 const BoundNetLog
& job_net_log
)
575 origin_loop_(base::MessageLoopProxy::current()),
577 completed_attempt_number_(0),
578 completed_attempt_error_(ERR_UNEXPECTED
),
579 had_non_speculative_request_(false),
580 net_log_(job_net_log
) {
581 if (!params_
.resolver_proc
.get())
582 params_
.resolver_proc
= HostResolverProc::GetDefault();
583 // If default is unset, use the system proc.
584 if (!params_
.resolver_proc
.get())
585 params_
.resolver_proc
= new SystemHostResolverProc();
589 DCHECK(origin_loop_
->BelongsToCurrentThread());
590 net_log_
.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK
);
591 StartLookupAttempt();
594 // Cancels this ProcTask. It will be orphaned. Any outstanding resolve
595 // attempts running on worker threads will continue running. Only once all the
596 // attempts complete will the final reference to this ProcTask be released.
598 DCHECK(origin_loop_
->BelongsToCurrentThread());
600 if (was_canceled() || was_completed())
604 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK
);
607 void set_had_non_speculative_request() {
608 DCHECK(origin_loop_
->BelongsToCurrentThread());
609 had_non_speculative_request_
= true;
612 bool was_canceled() const {
613 DCHECK(origin_loop_
->BelongsToCurrentThread());
614 return callback_
.is_null();
617 bool was_completed() const {
618 DCHECK(origin_loop_
->BelongsToCurrentThread());
619 return completed_attempt_number_
> 0;
623 friend class base::RefCountedThreadSafe
<ProcTask
>;
626 void StartLookupAttempt() {
627 DCHECK(origin_loop_
->BelongsToCurrentThread());
628 base::TimeTicks start_time
= base::TimeTicks::Now();
630 // Dispatch the lookup attempt to a worker thread.
631 if (!base::WorkerPool::PostTask(
633 base::Bind(&ProcTask::DoLookup
, this, start_time
, attempt_number_
),
637 // Since we could be running within Resolve() right now, we can't just
638 // call OnLookupComplete(). Instead we must wait until Resolve() has
639 // returned (IO_PENDING).
640 origin_loop_
->PostTask(
642 base::Bind(&ProcTask::OnLookupComplete
, this, AddressList(),
643 start_time
, attempt_number_
, ERR_UNEXPECTED
, 0));
648 NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED
,
649 NetLog::IntegerCallback("attempt_number", attempt_number_
));
651 // If we don't get the results within a given time, RetryIfNotComplete
652 // will start a new attempt on a different worker thread if none of our
653 // outstanding attempts have completed yet.
654 if (attempt_number_
<= params_
.max_retry_attempts
) {
655 origin_loop_
->PostDelayedTask(
657 base::Bind(&ProcTask::RetryIfNotComplete
, this),
658 params_
.unresponsive_delay
);
662 // WARNING: This code runs inside a worker pool. The shutdown code cannot
663 // wait for it to finish, so we must be very careful here about using other
664 // objects (like MessageLoops, Singletons, etc). During shutdown these objects
665 // may no longer exist. Multiple DoLookups() could be running in parallel, so
666 // any state inside of |this| must not mutate .
667 void DoLookup(const base::TimeTicks
& start_time
,
668 const uint32 attempt_number
) {
671 // Running on the worker thread
672 int error
= params_
.resolver_proc
->Resolve(key_
.hostname
,
674 key_
.host_resolver_flags
,
678 origin_loop_
->PostTask(
680 base::Bind(&ProcTask::OnLookupComplete
, this, results
, start_time
,
681 attempt_number
, error
, os_error
));
684 // Makes next attempt if DoLookup() has not finished (runs on origin thread).
685 void RetryIfNotComplete() {
686 DCHECK(origin_loop_
->BelongsToCurrentThread());
688 if (was_completed() || was_canceled())
691 params_
.unresponsive_delay
*= params_
.retry_factor
;
692 StartLookupAttempt();
695 // Callback for when DoLookup() completes (runs on origin thread).
696 void OnLookupComplete(const AddressList
& results
,
697 const base::TimeTicks
& start_time
,
698 const uint32 attempt_number
,
700 const int os_error
) {
701 DCHECK(origin_loop_
->BelongsToCurrentThread());
702 // If results are empty, we should return an error.
703 bool empty_list_on_ok
= (error
== OK
&& results
.empty());
704 UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok
);
705 if (empty_list_on_ok
)
706 error
= ERR_NAME_NOT_RESOLVED
;
708 bool was_retry_attempt
= attempt_number
> 1;
710 // Ideally the following code would be part of host_resolver_proc.cc,
711 // however it isn't safe to call NetworkChangeNotifier from worker threads.
712 // So we do it here on the IO thread instead.
713 if (error
!= OK
&& NetworkChangeNotifier::IsOffline())
714 error
= ERR_INTERNET_DISCONNECTED
;
716 // If this is the first attempt that is finishing later, then record data
717 // for the first attempt. Won't contaminate with retry attempt's data.
718 if (!was_retry_attempt
)
719 RecordPerformanceHistograms(start_time
, error
, os_error
);
721 RecordAttemptHistograms(start_time
, attempt_number
, error
, os_error
);
726 NetLog::ParametersCallback net_log_callback
;
728 net_log_callback
= base::Bind(&NetLogProcTaskFailedCallback
,
733 net_log_callback
= NetLog::IntegerCallback("attempt_number",
736 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED
,
742 // Copy the results from the first worker thread that resolves the host.
744 completed_attempt_number_
= attempt_number
;
745 completed_attempt_error_
= error
;
747 if (was_retry_attempt
) {
748 // If retry attempt finishes before 1st attempt, then get stats on how
749 // much time is saved by having spawned an extra attempt.
750 retry_attempt_finished_time_
= base::TimeTicks::Now();
754 net_log_callback
= base::Bind(&NetLogProcTaskFailedCallback
,
757 net_log_callback
= results_
.CreateNetLogCallback();
759 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK
,
762 callback_
.Run(error
, results_
);
765 void RecordPerformanceHistograms(const base::TimeTicks
& start_time
,
767 const int os_error
) const {
768 DCHECK(origin_loop_
->BelongsToCurrentThread());
769 enum Category
{ // Used in HISTOGRAM_ENUMERATION.
772 RESOLVE_SPECULATIVE_SUCCESS
,
773 RESOLVE_SPECULATIVE_FAIL
,
774 RESOLVE_MAX
, // Bounding value.
776 int category
= RESOLVE_MAX
; // Illegal value for later DCHECK only.
778 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
780 if (had_non_speculative_request_
) {
781 category
= RESOLVE_SUCCESS
;
782 DNS_HISTOGRAM("DNS.ResolveSuccess", duration
);
784 category
= RESOLVE_SPECULATIVE_SUCCESS
;
785 DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration
);
788 // Log DNS lookups based on |address_family|. This will help us determine
789 // if IPv4 or IPv4/6 lookups are faster or slower.
790 switch(key_
.address_family
) {
791 case ADDRESS_FAMILY_IPV4
:
792 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration
);
794 case ADDRESS_FAMILY_IPV6
:
795 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration
);
797 case ADDRESS_FAMILY_UNSPECIFIED
:
798 DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration
);
802 if (had_non_speculative_request_
) {
803 category
= RESOLVE_FAIL
;
804 DNS_HISTOGRAM("DNS.ResolveFail", duration
);
806 category
= RESOLVE_SPECULATIVE_FAIL
;
807 DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration
);
809 // Log DNS lookups based on |address_family|. This will help us determine
810 // if IPv4 or IPv4/6 lookups are faster or slower.
811 switch(key_
.address_family
) {
812 case ADDRESS_FAMILY_IPV4
:
813 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration
);
815 case ADDRESS_FAMILY_IPV6
:
816 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration
);
818 case ADDRESS_FAMILY_UNSPECIFIED
:
819 DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration
);
822 UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName
,
824 GetAllGetAddrinfoOSErrors());
826 DCHECK_LT(category
, static_cast<int>(RESOLVE_MAX
)); // Be sure it was set.
828 UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category
, RESOLVE_MAX
);
831 void RecordAttemptHistograms(const base::TimeTicks
& start_time
,
832 const uint32 attempt_number
,
834 const int os_error
) const {
835 DCHECK(origin_loop_
->BelongsToCurrentThread());
836 bool first_attempt_to_complete
=
837 completed_attempt_number_
== attempt_number
;
838 bool is_first_attempt
= (attempt_number
== 1);
840 if (first_attempt_to_complete
) {
841 // If this was first attempt to complete, then record the resolution
842 // status of the attempt.
843 if (completed_attempt_error_
== OK
) {
844 UMA_HISTOGRAM_ENUMERATION(
845 "DNS.AttemptFirstSuccess", attempt_number
, 100);
847 UMA_HISTOGRAM_ENUMERATION(
848 "DNS.AttemptFirstFailure", attempt_number
, 100);
853 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number
, 100);
855 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number
, 100);
857 // If first attempt didn't finish before retry attempt, then calculate stats
858 // on how much time is saved by having spawned an extra attempt.
859 if (!first_attempt_to_complete
&& is_first_attempt
&& !was_canceled()) {
860 DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry",
861 base::TimeTicks::Now() - retry_attempt_finished_time_
);
864 if (was_canceled() || !first_attempt_to_complete
) {
865 // Count those attempts which completed after the job was already canceled
866 // OR after the job was already completed by an earlier attempt (so in
868 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number
, 100);
870 // Record if job is canceled.
872 UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number
, 100);
875 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
877 DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration
);
879 DNS_HISTOGRAM("DNS.AttemptFailDuration", duration
);
882 // Set on the origin thread, read on the worker thread.
885 // Holds an owning reference to the HostResolverProc that we are going to use.
886 // This may not be the current resolver procedure by the time we call
887 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
888 // reference ensures that it remains valid until we are done.
889 ProcTaskParams params_
;
891 // The listener to the results of this ProcTask.
894 // Used to post ourselves onto the origin thread.
895 scoped_refptr
<base::MessageLoopProxy
> origin_loop_
;
897 // Keeps track of the number of attempts we have made so far to resolve the
898 // host. Whenever we start an attempt to resolve the host, we increase this
900 uint32 attempt_number_
;
902 // The index of the attempt which finished first (or 0 if the job is still in
904 uint32 completed_attempt_number_
;
906 // The result (a net error code) from the first attempt to complete.
907 int completed_attempt_error_
;
909 // The time when retry attempt was finished.
910 base::TimeTicks retry_attempt_finished_time_
;
912 // True if a non-speculative request was ever attached to this job
913 // (regardless of whether or not it was later canceled.
914 // This boolean is used for histogramming the duration of jobs used to
915 // service non-speculative requests.
916 bool had_non_speculative_request_
;
918 AddressList results_
;
920 BoundNetLog net_log_
;
922 DISALLOW_COPY_AND_ASSIGN(ProcTask
);
925 //-----------------------------------------------------------------------------
927 // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as
928 // it takes 40-100ms and should not block initialization.
929 class HostResolverImpl::LoopbackProbeJob
{
931 explicit LoopbackProbeJob(const base::WeakPtr
<HostResolverImpl
>& resolver
)
932 : resolver_(resolver
),
934 DCHECK(resolver
.get());
935 const bool kIsSlow
= true;
936 base::WorkerPool::PostTaskAndReply(
938 base::Bind(&LoopbackProbeJob::DoProbe
, base::Unretained(this)),
939 base::Bind(&LoopbackProbeJob::OnProbeComplete
, base::Owned(this)),
943 virtual ~LoopbackProbeJob() {}
946 // Runs on worker thread.
948 result_
= HaveOnlyLoopbackAddresses();
951 void OnProbeComplete() {
952 if (!resolver_
.get())
954 resolver_
->SetHaveOnlyLoopbackAddresses(result_
);
957 // Used/set only on origin thread.
958 base::WeakPtr
<HostResolverImpl
> resolver_
;
962 DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob
);
965 //-----------------------------------------------------------------------------
967 // Resolves the hostname using DnsTransaction.
968 // TODO(szym): This could be moved to separate source file as well.
969 class HostResolverImpl::DnsTask
: public base::SupportsWeakPtr
<DnsTask
> {
973 virtual void OnDnsTaskComplete(base::TimeTicks start_time
,
975 const AddressList
& addr_list
,
976 base::TimeDelta ttl
) = 0;
978 // Called when the first of two jobs succeeds. If the first completed
979 // transaction fails, this is not called. Also not called when the DnsTask
980 // only needs to run one transaction.
981 virtual void OnFirstDnsTransactionComplete() = 0;
985 virtual ~Delegate() {}
988 DnsTask(DnsClient
* client
,
991 const BoundNetLog
& job_net_log
)
995 net_log_(job_net_log
),
996 num_completed_transactions_(0),
997 task_start_time_(base::TimeTicks::Now()) {
1002 bool needs_two_transactions() const {
1003 return key_
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
;
1006 bool needs_another_transaction() const {
1007 return needs_two_transactions() && !transaction_aaaa_
;
1010 void StartFirstTransaction() {
1011 DCHECK_EQ(0u, num_completed_transactions_
);
1012 net_log_
.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
);
1013 if (key_
.address_family
== ADDRESS_FAMILY_IPV6
) {
1020 void StartSecondTransaction() {
1021 DCHECK(needs_two_transactions());
1027 DCHECK(!transaction_a_
);
1028 DCHECK_NE(ADDRESS_FAMILY_IPV6
, key_
.address_family
);
1029 transaction_a_
= CreateTransaction(ADDRESS_FAMILY_IPV4
);
1030 transaction_a_
->Start();
1034 DCHECK(!transaction_aaaa_
);
1035 DCHECK_NE(ADDRESS_FAMILY_IPV4
, key_
.address_family
);
1036 transaction_aaaa_
= CreateTransaction(ADDRESS_FAMILY_IPV6
);
1037 transaction_aaaa_
->Start();
1040 scoped_ptr
<DnsTransaction
> CreateTransaction(AddressFamily family
) {
1041 DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED
, family
);
1042 return client_
->GetTransactionFactory()->CreateTransaction(
1044 family
== ADDRESS_FAMILY_IPV6
? dns_protocol::kTypeAAAA
:
1045 dns_protocol::kTypeA
,
1046 base::Bind(&DnsTask::OnTransactionComplete
, base::Unretained(this),
1047 base::TimeTicks::Now()),
1051 void OnTransactionComplete(const base::TimeTicks
& start_time
,
1052 DnsTransaction
* transaction
,
1054 const DnsResponse
* response
) {
1055 DCHECK(transaction
);
1056 base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
1057 if (net_error
!= OK
) {
1058 DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration
);
1059 OnFailure(net_error
, DnsResponse::DNS_PARSE_OK
);
1063 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration
);
1064 switch (transaction
->GetType()) {
1065 case dns_protocol::kTypeA
:
1066 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration
);
1068 case dns_protocol::kTypeAAAA
:
1069 DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration
);
1073 AddressList addr_list
;
1074 base::TimeDelta ttl
;
1075 DnsResponse::Result result
= response
->ParseToAddressList(&addr_list
, &ttl
);
1076 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
1078 DnsResponse::DNS_PARSE_RESULT_MAX
);
1079 if (result
!= DnsResponse::DNS_PARSE_OK
) {
1080 // Fail even if the other query succeeds.
1081 OnFailure(ERR_DNS_MALFORMED_RESPONSE
, result
);
1085 ++num_completed_transactions_
;
1086 if (num_completed_transactions_
== 1) {
1089 ttl_
= std::min(ttl_
, ttl
);
1092 if (transaction
->GetType() == dns_protocol::kTypeA
) {
1093 DCHECK_EQ(transaction_a_
.get(), transaction
);
1094 // Place IPv4 addresses after IPv6.
1095 addr_list_
.insert(addr_list_
.end(), addr_list
.begin(), addr_list
.end());
1097 DCHECK_EQ(transaction_aaaa_
.get(), transaction
);
1098 // Place IPv6 addresses before IPv4.
1099 addr_list_
.insert(addr_list_
.begin(), addr_list
.begin(), addr_list
.end());
1102 if (needs_two_transactions() && num_completed_transactions_
== 1) {
1103 // No need to repeat the suffix search.
1104 key_
.hostname
= transaction
->GetHostname();
1105 delegate_
->OnFirstDnsTransactionComplete();
1109 if (addr_list_
.empty()) {
1110 // TODO(szym): Don't fallback to ProcTask in this case.
1111 OnFailure(ERR_NAME_NOT_RESOLVED
, DnsResponse::DNS_PARSE_OK
);
1115 // If there are multiple addresses, and at least one is IPv6, need to sort
1116 // them. Note that IPv6 addresses are always put before IPv4 ones, so it's
1117 // sufficient to just check the family of the first address.
1118 if (addr_list_
.size() > 1 &&
1119 addr_list_
[0].GetFamily() == ADDRESS_FAMILY_IPV6
) {
1120 // Sort addresses if needed. Sort could complete synchronously.
1121 client_
->GetAddressSorter()->Sort(
1123 base::Bind(&DnsTask::OnSortComplete
,
1125 base::TimeTicks::Now()));
1127 OnSuccess(addr_list_
);
1131 void OnSortComplete(base::TimeTicks start_time
,
1133 const AddressList
& addr_list
) {
1135 DNS_HISTOGRAM("AsyncDNS.SortFailure",
1136 base::TimeTicks::Now() - start_time
);
1137 OnFailure(ERR_DNS_SORT_ERROR
, DnsResponse::DNS_PARSE_OK
);
1141 DNS_HISTOGRAM("AsyncDNS.SortSuccess",
1142 base::TimeTicks::Now() - start_time
);
1144 // AddressSorter prunes unusable destinations.
1145 if (addr_list
.empty()) {
1146 LOG(WARNING
) << "Address list empty after RFC3484 sort";
1147 OnFailure(ERR_NAME_NOT_RESOLVED
, DnsResponse::DNS_PARSE_OK
);
1151 OnSuccess(addr_list
);
1154 void OnFailure(int net_error
, DnsResponse::Result result
) {
1155 DCHECK_NE(OK
, net_error
);
1157 NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
,
1158 base::Bind(&NetLogDnsTaskFailedCallback
, net_error
, result
));
1159 delegate_
->OnDnsTaskComplete(task_start_time_
, net_error
, AddressList(),
1163 void OnSuccess(const AddressList
& addr_list
) {
1164 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK
,
1165 addr_list
.CreateNetLogCallback());
1166 delegate_
->OnDnsTaskComplete(task_start_time_
, OK
, addr_list
, ttl_
);
1172 // The listener to the results of this DnsTask.
1173 Delegate
* delegate_
;
1174 const BoundNetLog net_log_
;
1176 scoped_ptr
<DnsTransaction
> transaction_a_
;
1177 scoped_ptr
<DnsTransaction
> transaction_aaaa_
;
1179 unsigned num_completed_transactions_
;
1181 // These are updated as each transaction completes.
1182 base::TimeDelta ttl_
;
1183 // IPv6 addresses must appear first in the list.
1184 AddressList addr_list_
;
1186 base::TimeTicks task_start_time_
;
1188 DISALLOW_COPY_AND_ASSIGN(DnsTask
);
1191 //-----------------------------------------------------------------------------
1193 // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch.
1194 class HostResolverImpl::Job
: public PrioritizedDispatcher::Job
,
1195 public HostResolverImpl::DnsTask::Delegate
{
1197 // Creates new job for |key| where |request_net_log| is bound to the
1198 // request that spawned it.
1199 Job(const base::WeakPtr
<HostResolverImpl
>& resolver
,
1201 RequestPriority priority
,
1202 const BoundNetLog
& request_net_log
)
1203 : resolver_(resolver
),
1205 priority_tracker_(priority
),
1206 had_non_speculative_request_(false),
1207 had_dns_config_(false),
1208 num_occupied_job_slots_(0),
1209 dns_task_error_(OK
),
1210 creation_time_(base::TimeTicks::Now()),
1211 priority_change_time_(creation_time_
),
1212 net_log_(BoundNetLog::Make(request_net_log
.net_log(),
1213 NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB
)) {
1214 request_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB
);
1216 net_log_
.BeginEvent(
1217 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1218 base::Bind(&NetLogJobCreationCallback
,
1219 request_net_log
.source(),
1225 // |resolver_| was destroyed with this Job still in flight.
1226 // Clean-up, record in the log, but don't run any callbacks.
1227 if (is_proc_running()) {
1228 proc_task_
->Cancel();
1231 // Clean up now for nice NetLog.
1233 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1235 } else if (is_queued()) {
1236 // |resolver_| was destroyed without running this Job.
1237 // TODO(szym): is there any benefit in having this distinction?
1238 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
);
1239 net_log_
.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
);
1241 // else CompleteRequests logged EndEvent.
1243 // Log any remaining Requests as cancelled.
1244 for (RequestsList::const_iterator it
= requests_
.begin();
1245 it
!= requests_
.end(); ++it
) {
1247 if (req
->was_canceled())
1249 DCHECK_EQ(this, req
->job());
1250 LogCancelRequest(req
->source_net_log(), req
->request_net_log(),
1255 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1257 void Schedule(bool at_head
) {
1258 DCHECK(!is_queued());
1259 PrioritizedDispatcher::Handle handle
;
1261 handle
= resolver_
->dispatcher_
.Add(this, priority());
1263 handle
= resolver_
->dispatcher_
.AddAtHead(this, priority());
1265 // The dispatcher could have started |this| in the above call to Add, which
1266 // could have called Schedule again. In that case |handle| will be null,
1267 // but |handle_| may have been set by the other nested call to Schedule.
1268 if (!handle
.is_null()) {
1269 DCHECK(handle_
.is_null());
1274 void AddRequest(scoped_ptr
<Request
> req
) {
1275 DCHECK_EQ(key_
.hostname
, req
->info().hostname());
1278 priority_tracker_
.Add(req
->priority());
1280 req
->request_net_log().AddEvent(
1281 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH
,
1282 net_log_
.source().ToEventParametersCallback());
1285 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH
,
1286 base::Bind(&NetLogJobAttachCallback
,
1287 req
->request_net_log().source(),
1290 // TODO(szym): Check if this is still needed.
1291 if (!req
->info().is_speculative()) {
1292 had_non_speculative_request_
= true;
1293 if (proc_task_
.get())
1294 proc_task_
->set_had_non_speculative_request();
1297 requests_
.push_back(req
.release());
1302 // Marks |req| as cancelled. If it was the last active Request, also finishes
1303 // this Job, marking it as cancelled, and deletes it.
1304 void CancelRequest(Request
* req
) {
1305 DCHECK_EQ(key_
.hostname
, req
->info().hostname());
1306 DCHECK(!req
->was_canceled());
1308 // Don't remove it from |requests_| just mark it canceled.
1309 req
->MarkAsCanceled();
1310 LogCancelRequest(req
->source_net_log(), req
->request_net_log(),
1313 priority_tracker_
.Remove(req
->priority());
1314 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH
,
1315 base::Bind(&NetLogJobAttachCallback
,
1316 req
->request_net_log().source(),
1319 if (num_active_requests() > 0) {
1322 // If we were called from a Request's callback within CompleteRequests,
1323 // that Request could not have been cancelled, so num_active_requests()
1324 // could not be 0. Therefore, we are not in CompleteRequests().
1325 CompleteRequestsWithError(OK
/* cancelled */);
1329 // Called from AbortAllInProgressJobs. Completes all requests and destroys
1330 // the job. This currently assumes the abort is due to a network change.
1332 DCHECK(is_running());
1333 CompleteRequestsWithError(ERR_NETWORK_CHANGED
);
1336 // If DnsTask present, abort it and fall back to ProcTask.
1337 void AbortDnsTask() {
1340 dns_task_error_
= OK
;
1345 // Called by HostResolverImpl when this job is evicted due to queue overflow.
1346 // Completes all requests and destroys the job.
1348 DCHECK(!is_running());
1349 DCHECK(is_queued());
1352 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED
);
1354 // This signals to CompleteRequests that this job never ran.
1355 CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
);
1358 // Attempts to serve the job from HOSTS. Returns true if succeeded and
1359 // this Job was destroyed.
1360 bool ServeFromHosts() {
1361 DCHECK_GT(num_active_requests(), 0u);
1362 AddressList addr_list
;
1363 if (resolver_
->ServeFromHosts(key(),
1364 requests_
.front()->info(),
1366 // This will destroy the Job.
1368 HostCache::Entry(OK
, MakeAddressListForRequest(addr_list
)),
1375 const Key
key() const {
1379 bool is_queued() const {
1380 return !handle_
.is_null();
1383 bool is_running() const {
1384 return is_dns_running() || is_proc_running();
1388 void KillDnsTask() {
1390 ReduceToOneJobSlot();
1395 // Reduce the number of job slots occupied and queued in the dispatcher
1396 // to one. If the second Job slot is queued in the dispatcher, cancels the
1397 // queued job. Otherwise, the second Job has been started by the
1398 // PrioritizedDispatcher, so signals it is complete.
1399 void ReduceToOneJobSlot() {
1400 DCHECK_GE(num_occupied_job_slots_
, 1u);
1402 resolver_
->dispatcher_
.Cancel(handle_
);
1404 } else if (num_occupied_job_slots_
> 1) {
1405 resolver_
->dispatcher_
.OnJobFinished();
1406 --num_occupied_job_slots_
;
1408 DCHECK_EQ(1u, num_occupied_job_slots_
);
1411 void UpdatePriority() {
1413 if (priority() != static_cast<RequestPriority
>(handle_
.priority()))
1414 priority_change_time_
= base::TimeTicks::Now();
1415 handle_
= resolver_
->dispatcher_
.ChangePriority(handle_
, priority());
1419 AddressList
MakeAddressListForRequest(const AddressList
& list
) const {
1420 if (requests_
.empty())
1422 return AddressList::CopyWithPort(list
, requests_
.front()->info().port());
1425 // PriorityDispatch::Job:
1426 virtual void Start() OVERRIDE
{
1427 DCHECK_LE(num_occupied_job_slots_
, 1u);
1430 ++num_occupied_job_slots_
;
1432 if (num_occupied_job_slots_
== 2) {
1433 StartSecondDnsTransaction();
1437 DCHECK(!is_running());
1439 net_log_
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED
);
1441 had_dns_config_
= resolver_
->HaveDnsConfig();
1443 base::TimeTicks now
= base::TimeTicks::Now();
1444 base::TimeDelta queue_time
= now
- creation_time_
;
1445 base::TimeDelta queue_time_after_change
= now
- priority_change_time_
;
1447 if (had_dns_config_
) {
1448 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(),
1450 DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(),
1451 queue_time_after_change
);
1453 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time
);
1454 DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(),
1455 queue_time_after_change
);
1459 (key_
.host_resolver_flags
& HOST_RESOLVER_SYSTEM_ONLY
) != 0;
1461 // Caution: Job::Start must not complete synchronously.
1462 if (!system_only
&& had_dns_config_
&&
1463 !ResemblesMulticastDNSName(key_
.hostname
)) {
1470 // TODO(szym): Since DnsTransaction does not consume threads, we can increase
1471 // the limits on |dispatcher_|. But in order to keep the number of WorkerPool
1472 // threads low, we will need to use an "inner" PrioritizedDispatcher with
1474 void StartProcTask() {
1475 DCHECK(!is_dns_running());
1476 proc_task_
= new ProcTask(
1478 resolver_
->proc_params_
,
1479 base::Bind(&Job::OnProcTaskComplete
, base::Unretained(this),
1480 base::TimeTicks::Now()),
1483 if (had_non_speculative_request_
)
1484 proc_task_
->set_had_non_speculative_request();
1485 // Start() could be called from within Resolve(), hence it must NOT directly
1486 // call OnProcTaskComplete, for example, on synchronous failure.
1487 proc_task_
->Start();
1490 // Called by ProcTask when it completes.
1491 void OnProcTaskComplete(base::TimeTicks start_time
,
1493 const AddressList
& addr_list
) {
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 virtual 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 virtual 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 CHECK(resolver_
.get());
1645 // This job must be removed from resolver's |jobs_| now to make room for a
1646 // new job with the same key in case one of the OnComplete callbacks decides
1647 // to spawn one. Consequently, the job deletes itself when CompleteRequests
1649 scoped_ptr
<Job
> self_deleter(this);
1651 resolver_
->RemoveJob(this);
1654 if (is_proc_running()) {
1655 DCHECK(!is_queued());
1656 proc_task_
->Cancel();
1661 // Signal dispatcher that a slot has opened.
1662 resolver_
->dispatcher_
.OnJobFinished();
1663 } else if (is_queued()) {
1664 resolver_
->dispatcher_
.Cancel(handle_
);
1668 if (num_active_requests() == 0) {
1669 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
);
1670 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1675 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB
,
1678 DCHECK(!requests_
.empty());
1680 if (entry
.error
== OK
) {
1681 // Record this histogram here, when we know the system has a valid DNS
1683 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
1684 resolver_
->received_dns_config_
);
1687 bool did_complete
= (entry
.error
!= ERR_NETWORK_CHANGED
) &&
1688 (entry
.error
!= ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
);
1690 resolver_
->CacheResult(key_
, entry
, ttl
);
1692 // Complete all of the requests that were attached to the job.
1693 for (RequestsList::const_iterator it
= requests_
.begin();
1694 it
!= requests_
.end(); ++it
) {
1697 if (req
->was_canceled())
1700 DCHECK_EQ(this, req
->job());
1701 // Update the net log and notify registered observers.
1702 LogFinishRequest(req
->source_net_log(), req
->request_net_log(),
1703 req
->info(), entry
.error
);
1705 // Record effective total time from creation to completion.
1706 RecordTotalTime(had_dns_config_
, req
->info().is_speculative(),
1707 base::TimeTicks::Now() - req
->request_time());
1709 req
->OnComplete(entry
.error
, entry
.addrlist
);
1711 // Check if the resolver was destroyed as a result of running the
1712 // callback. If it was, we could continue, but we choose to bail.
1713 if (!resolver_
.get())
1718 // Convenience wrapper for CompleteRequests in case of failure.
1719 void CompleteRequestsWithError(int net_error
) {
1720 CompleteRequests(HostCache::Entry(net_error
, AddressList()),
1724 RequestPriority
priority() const {
1725 return priority_tracker_
.highest_priority();
1728 // Number of non-canceled requests in |requests_|.
1729 size_t num_active_requests() const {
1730 return priority_tracker_
.total_count();
1733 bool is_dns_running() const {
1734 return dns_task_
.get() != NULL
;
1737 bool is_proc_running() const {
1738 return proc_task_
.get() != NULL
;
1741 base::WeakPtr
<HostResolverImpl
> resolver_
;
1745 // Tracks the highest priority across |requests_|.
1746 PriorityTracker priority_tracker_
;
1748 bool had_non_speculative_request_
;
1750 // Distinguishes measurements taken while DnsClient was fully configured.
1751 bool had_dns_config_
;
1753 // Number of slots occupied by this Job in resolver's PrioritizedDispatcher.
1754 unsigned num_occupied_job_slots_
;
1756 // Result of DnsTask.
1757 int dns_task_error_
;
1759 const base::TimeTicks creation_time_
;
1760 base::TimeTicks priority_change_time_
;
1762 BoundNetLog net_log_
;
1764 // Resolves the host using a HostResolverProc.
1765 scoped_refptr
<ProcTask
> proc_task_
;
1767 // Resolves the host using a DnsTransaction.
1768 scoped_ptr
<DnsTask
> dns_task_
;
1770 // All Requests waiting for the result of this Job. Some can be canceled.
1771 RequestsList requests_
;
1773 // A handle used in |HostResolverImpl::dispatcher_|.
1774 PrioritizedDispatcher::Handle handle_
;
1777 //-----------------------------------------------------------------------------
1779 HostResolverImpl::ProcTaskParams::ProcTaskParams(
1780 HostResolverProc
* resolver_proc
,
1781 size_t max_retry_attempts
)
1782 : resolver_proc(resolver_proc
),
1783 max_retry_attempts(max_retry_attempts
),
1784 unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)),
1788 HostResolverImpl::ProcTaskParams::~ProcTaskParams() {}
1790 HostResolverImpl::HostResolverImpl(
1791 scoped_ptr
<HostCache
> cache
,
1792 const PrioritizedDispatcher::Limits
& job_limits
,
1793 const ProcTaskParams
& proc_params
,
1795 : cache_(cache
.Pass()),
1796 dispatcher_(job_limits
),
1797 max_queued_jobs_(job_limits
.total_jobs
* 100u),
1798 proc_params_(proc_params
),
1800 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED
),
1801 weak_ptr_factory_(this),
1802 probe_weak_ptr_factory_(this),
1803 received_dns_config_(false),
1804 num_dns_failures_(0),
1805 probe_ipv6_support_(true),
1806 use_local_ipv6_(false),
1807 resolved_known_ipv6_hostname_(false),
1808 additional_resolver_flags_(0),
1809 fallback_to_proctask_(true) {
1811 DCHECK_GE(dispatcher_
.num_priorities(), static_cast<size_t>(NUM_PRIORITIES
));
1813 // Maximum of 4 retry attempts for host resolution.
1814 static const size_t kDefaultMaxRetryAttempts
= 4u;
1816 if (proc_params_
.max_retry_attempts
== HostResolver::kDefaultRetryAttempts
)
1817 proc_params_
.max_retry_attempts
= kDefaultMaxRetryAttempts
;
1820 EnsureWinsockInit();
1822 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
1823 new LoopbackProbeJob(weak_ptr_factory_
.GetWeakPtr());
1825 NetworkChangeNotifier::AddIPAddressObserver(this);
1826 NetworkChangeNotifier::AddDNSObserver(this);
1827 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
1828 !defined(OS_ANDROID)
1829 EnsureDnsReloaderInit();
1833 DnsConfig dns_config
;
1834 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
1835 received_dns_config_
= dns_config
.IsValid();
1836 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
1837 use_local_ipv6_
= !dns_config
.IsValid() || dns_config
.use_local_ipv6
;
1840 fallback_to_proctask_
= !ConfigureAsyncDnsNoFallbackFieldTrial();
1843 HostResolverImpl::~HostResolverImpl() {
1844 // Prevent the dispatcher from starting new jobs.
1845 dispatcher_
.SetLimitsToZero();
1846 // It's now safe for Jobs to call KillDsnTask on destruction, because
1847 // OnJobComplete will not start any new jobs.
1848 STLDeleteValues(&jobs_
);
1850 NetworkChangeNotifier::RemoveIPAddressObserver(this);
1851 NetworkChangeNotifier::RemoveDNSObserver(this);
1854 void HostResolverImpl::SetMaxQueuedJobs(size_t value
) {
1855 DCHECK_EQ(0u, dispatcher_
.num_queued_jobs());
1856 DCHECK_GT(value
, 0u);
1857 max_queued_jobs_
= value
;
1860 int HostResolverImpl::Resolve(const RequestInfo
& info
,
1861 RequestPriority priority
,
1862 AddressList
* addresses
,
1863 const CompletionCallback
& callback
,
1864 RequestHandle
* out_req
,
1865 const BoundNetLog
& source_net_log
) {
1867 DCHECK(CalledOnValidThread());
1868 DCHECK_EQ(false, callback
.is_null());
1870 // Check that the caller supplied a valid hostname to resolve.
1871 std::string labeled_hostname
;
1872 if (!DNSDomainFromDot(info
.hostname(), &labeled_hostname
))
1873 return ERR_NAME_NOT_RESOLVED
;
1875 // Make a log item for the request.
1876 BoundNetLog request_net_log
= BoundNetLog::Make(net_log_
,
1877 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST
);
1879 LogStartRequest(source_net_log
, request_net_log
, info
);
1881 // Build a key that identifies the request in the cache and in the
1882 // outstanding jobs map.
1883 Key key
= GetEffectiveKeyForRequest(info
, request_net_log
);
1885 int rv
= ResolveHelper(key
, info
, addresses
, request_net_log
);
1886 if (rv
!= ERR_DNS_CACHE_MISS
) {
1887 LogFinishRequest(source_net_log
, request_net_log
, info
, rv
);
1888 RecordTotalTime(HaveDnsConfig(), info
.is_speculative(), base::TimeDelta());
1892 // Next we need to attach our request to a "job". This job is responsible for
1893 // calling "getaddrinfo(hostname)" on a worker thread.
1895 JobMap::iterator jobit
= jobs_
.find(key
);
1897 if (jobit
== jobs_
.end()) {
1899 new Job(weak_ptr_factory_
.GetWeakPtr(), key
, priority
, request_net_log
);
1900 job
->Schedule(false);
1902 // Check for queue overflow.
1903 if (dispatcher_
.num_queued_jobs() > max_queued_jobs_
) {
1904 Job
* evicted
= static_cast<Job
*>(dispatcher_
.EvictOldestLowest());
1906 evicted
->OnEvicted(); // Deletes |evicted|.
1907 if (evicted
== job
) {
1908 rv
= ERR_HOST_RESOLVER_QUEUE_TOO_LARGE
;
1909 LogFinishRequest(source_net_log
, request_net_log
, info
, rv
);
1913 jobs_
.insert(jobit
, std::make_pair(key
, job
));
1915 job
= jobit
->second
;
1918 // Can't complete synchronously. Create and attach request.
1919 scoped_ptr
<Request
> req(new Request(
1920 source_net_log
, request_net_log
, info
, priority
, callback
, addresses
));
1922 *out_req
= reinterpret_cast<RequestHandle
>(req
.get());
1924 job
->AddRequest(req
.Pass());
1925 // Completion happens during Job::CompleteRequests().
1926 return ERR_IO_PENDING
;
1929 int HostResolverImpl::ResolveHelper(const Key
& key
,
1930 const RequestInfo
& info
,
1931 AddressList
* addresses
,
1932 const BoundNetLog
& request_net_log
) {
1933 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
1934 // On Windows it gives the default interface's address, whereas on Linux it
1935 // gives an error. We will make it fail on all platforms for consistency.
1936 if (info
.hostname().empty() || info
.hostname().size() > kMaxHostLength
)
1937 return ERR_NAME_NOT_RESOLVED
;
1939 int net_error
= ERR_UNEXPECTED
;
1940 if (ResolveAsIP(key
, info
, &net_error
, addresses
))
1942 if (ServeFromCache(key
, info
, &net_error
, addresses
)) {
1943 request_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT
);
1946 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
1947 // http://crbug.com/117655
1948 if (ServeFromHosts(key
, info
, addresses
)) {
1949 request_net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT
);
1952 return ERR_DNS_CACHE_MISS
;
1955 int HostResolverImpl::ResolveFromCache(const RequestInfo
& info
,
1956 AddressList
* addresses
,
1957 const BoundNetLog
& source_net_log
) {
1958 DCHECK(CalledOnValidThread());
1961 // Make a log item for the request.
1962 BoundNetLog request_net_log
= BoundNetLog::Make(net_log_
,
1963 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST
);
1965 // Update the net log and notify registered observers.
1966 LogStartRequest(source_net_log
, request_net_log
, info
);
1968 Key key
= GetEffectiveKeyForRequest(info
, request_net_log
);
1970 int rv
= ResolveHelper(key
, info
, addresses
, request_net_log
);
1971 LogFinishRequest(source_net_log
, request_net_log
, info
, rv
);
1975 void HostResolverImpl::CancelRequest(RequestHandle req_handle
) {
1976 DCHECK(CalledOnValidThread());
1977 Request
* req
= reinterpret_cast<Request
*>(req_handle
);
1979 Job
* job
= req
->job();
1981 job
->CancelRequest(req
);
1984 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family
) {
1985 DCHECK(CalledOnValidThread());
1986 default_address_family_
= address_family
;
1987 probe_ipv6_support_
= false;
1990 AddressFamily
HostResolverImpl::GetDefaultAddressFamily() const {
1991 return default_address_family_
;
1994 void HostResolverImpl::SetDnsClientEnabled(bool enabled
) {
1995 DCHECK(CalledOnValidThread());
1996 #if defined(ENABLE_BUILT_IN_DNS)
1997 if (enabled
&& !dns_client_
) {
1998 SetDnsClient(DnsClient::CreateClient(net_log_
));
1999 } else if (!enabled
&& dns_client_
) {
2000 SetDnsClient(scoped_ptr
<DnsClient
>());
2005 HostCache
* HostResolverImpl::GetHostCache() {
2006 return cache_
.get();
2009 base::Value
* HostResolverImpl::GetDnsConfigAsValue() const {
2010 // Check if async DNS is disabled.
2011 if (!dns_client_
.get())
2014 // Check if async DNS is enabled, but we currently have no configuration
2016 const DnsConfig
* dns_config
= dns_client_
->GetConfig();
2017 if (dns_config
== NULL
)
2018 return new base::DictionaryValue();
2020 return dns_config
->ToValue();
2023 bool HostResolverImpl::ResolveAsIP(const Key
& key
,
2024 const RequestInfo
& info
,
2026 AddressList
* addresses
) {
2029 IPAddressNumber ip_number
;
2030 if (!ParseIPLiteralToNumber(key
.hostname
, &ip_number
))
2033 DCHECK_EQ(key
.host_resolver_flags
&
2034 ~(HOST_RESOLVER_CANONNAME
| HOST_RESOLVER_LOOPBACK_ONLY
|
2035 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
),
2036 0) << " Unhandled flag";
2037 bool ipv6_disabled
= (default_address_family_
== ADDRESS_FAMILY_IPV4
) &&
2038 !probe_ipv6_support_
;
2040 if ((ip_number
.size() == kIPv6AddressSize
) && ipv6_disabled
) {
2041 *net_error
= ERR_NAME_NOT_RESOLVED
;
2043 *addresses
= AddressList::CreateFromIPAddress(ip_number
, info
.port());
2044 if (key
.host_resolver_flags
& HOST_RESOLVER_CANONNAME
)
2045 addresses
->SetDefaultCanonicalName();
2050 bool HostResolverImpl::ServeFromCache(const Key
& key
,
2051 const RequestInfo
& info
,
2053 AddressList
* addresses
) {
2056 if (!info
.allow_cached_response() || !cache_
.get())
2059 const HostCache::Entry
* cache_entry
= cache_
->Lookup(
2060 key
, base::TimeTicks::Now());
2064 *net_error
= cache_entry
->error
;
2065 if (*net_error
== OK
) {
2066 if (cache_entry
->has_ttl())
2067 RecordTTL(cache_entry
->ttl
);
2068 *addresses
= EnsurePortOnAddressList(cache_entry
->addrlist
, info
.port());
2073 bool HostResolverImpl::ServeFromHosts(const Key
& key
,
2074 const RequestInfo
& info
,
2075 AddressList
* addresses
) {
2077 if (!HaveDnsConfig())
2081 // HOSTS lookups are case-insensitive.
2082 std::string hostname
= StringToLowerASCII(key
.hostname
);
2084 const DnsHosts
& hosts
= dns_client_
->GetConfig()->hosts
;
2086 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
2087 // (glibc and c-ares) return the first matching line. We have more
2088 // flexibility, but lose implicit ordering.
2089 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
2091 if (key
.address_family
== ADDRESS_FAMILY_IPV6
||
2092 key
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
2093 DnsHosts::const_iterator it
= hosts
.find(
2094 DnsHostsKey(hostname
, ADDRESS_FAMILY_IPV6
));
2095 if (it
!= hosts
.end())
2096 addresses
->push_back(IPEndPoint(it
->second
, info
.port()));
2099 if (key
.address_family
== ADDRESS_FAMILY_IPV4
||
2100 key
.address_family
== ADDRESS_FAMILY_UNSPECIFIED
) {
2101 DnsHosts::const_iterator it
= hosts
.find(
2102 DnsHostsKey(hostname
, ADDRESS_FAMILY_IPV4
));
2103 if (it
!= hosts
.end())
2104 addresses
->push_back(IPEndPoint(it
->second
, info
.port()));
2107 // If got only loopback addresses and the family was restricted, resolve
2108 // again, without restrictions. See SystemHostResolverCall for rationale.
2109 if ((key
.host_resolver_flags
&
2110 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
) &&
2111 IsAllIPv4Loopback(*addresses
)) {
2113 new_key
.address_family
= ADDRESS_FAMILY_UNSPECIFIED
;
2114 new_key
.host_resolver_flags
&=
2115 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
;
2116 return ServeFromHosts(new_key
, info
, addresses
);
2118 return !addresses
->empty();
2121 void HostResolverImpl::CacheResult(const Key
& key
,
2122 const HostCache::Entry
& entry
,
2123 base::TimeDelta ttl
) {
2125 cache_
->Set(key
, entry
, base::TimeTicks::Now(), ttl
);
2128 void HostResolverImpl::RemoveJob(Job
* job
) {
2130 JobMap::iterator it
= jobs_
.find(job
->key());
2131 if (it
!= jobs_
.end() && it
->second
== job
)
2135 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result
) {
2137 additional_resolver_flags_
|= HOST_RESOLVER_LOOPBACK_ONLY
;
2139 additional_resolver_flags_
&= ~HOST_RESOLVER_LOOPBACK_ONLY
;
2143 HostResolverImpl::Key
HostResolverImpl::GetEffectiveKeyForRequest(
2144 const RequestInfo
& info
, const BoundNetLog
& net_log
) const {
2145 HostResolverFlags effective_flags
=
2146 info
.host_resolver_flags() | additional_resolver_flags_
;
2147 AddressFamily effective_address_family
= info
.address_family();
2149 if (info
.address_family() == ADDRESS_FAMILY_UNSPECIFIED
) {
2150 if (probe_ipv6_support_
&& !use_local_ipv6_
) {
2151 base::TimeTicks start_time
= base::TimeTicks::Now();
2152 // Google DNS address.
2153 const uint8 kIPv6Address
[] =
2154 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
2155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
2156 IPAddressNumber
address(kIPv6Address
,
2157 kIPv6Address
+ arraysize(kIPv6Address
));
2158 BoundNetLog probe_net_log
= BoundNetLog::Make(
2159 net_log
.net_log(), NetLog::SOURCE_IPV6_REACHABILITY_CHECK
);
2160 probe_net_log
.BeginEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK
,
2161 net_log
.source().ToEventParametersCallback());
2162 bool rv6
= IsGloballyReachable(address
, probe_net_log
);
2163 probe_net_log
.EndEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK
);
2165 net_log
.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_SUPPORTED
);
2167 UMA_HISTOGRAM_TIMES("Net.IPv6ConnectDuration",
2168 base::TimeTicks::Now() - start_time
);
2170 UMA_HISTOGRAM_BOOLEAN("Net.IPv6ConnectSuccessMatch",
2171 default_address_family_
== ADDRESS_FAMILY_UNSPECIFIED
);
2173 UMA_HISTOGRAM_BOOLEAN("Net.IPv6ConnectFailureMatch",
2174 default_address_family_
!= ADDRESS_FAMILY_UNSPECIFIED
);
2176 effective_address_family
= ADDRESS_FAMILY_IPV4
;
2177 effective_flags
|= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
;
2180 effective_address_family
= default_address_family_
;
2184 return Key(info
.hostname(), effective_address_family
, effective_flags
);
2187 void HostResolverImpl::AbortAllInProgressJobs() {
2188 // In Abort, a Request callback could spawn new Jobs with matching keys, so
2189 // first collect and remove all running jobs from |jobs_|.
2190 ScopedVector
<Job
> jobs_to_abort
;
2191 for (JobMap::iterator it
= jobs_
.begin(); it
!= jobs_
.end(); ) {
2192 Job
* job
= it
->second
;
2193 if (job
->is_running()) {
2194 jobs_to_abort
.push_back(job
);
2197 DCHECK(job
->is_queued());
2202 // Pause the dispatcher so it won't start any new dispatcher jobs while
2203 // aborting the old ones. This is needed so that it won't start the second
2204 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2206 PrioritizedDispatcher::Limits limits
= dispatcher_
.GetLimits();
2207 dispatcher_
.SetLimits(
2208 PrioritizedDispatcher::Limits(limits
.reserved_slots
.size(), 0));
2210 // Life check to bail once |this| is deleted.
2211 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2214 for (size_t i
= 0; self
.get() && i
< jobs_to_abort
.size(); ++i
) {
2215 jobs_to_abort
[i
]->Abort();
2216 jobs_to_abort
[i
] = NULL
;
2220 dispatcher_
.SetLimits(limits
);
2223 void HostResolverImpl::AbortDnsTasks() {
2224 // Pause the dispatcher so it won't start any new dispatcher jobs while
2225 // aborting the old ones. This is needed so that it won't start the second
2226 // DnsTransaction for a job if the DnsConfig just changed.
2227 PrioritizedDispatcher::Limits limits
= dispatcher_
.GetLimits();
2228 dispatcher_
.SetLimits(
2229 PrioritizedDispatcher::Limits(limits
.reserved_slots
.size(), 0));
2231 for (JobMap::iterator it
= jobs_
.begin(); it
!= jobs_
.end(); ++it
)
2232 it
->second
->AbortDnsTask();
2233 dispatcher_
.SetLimits(limits
);
2236 void HostResolverImpl::TryServingAllJobsFromHosts() {
2237 if (!HaveDnsConfig())
2240 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2241 // http://crbug.com/117655
2243 // Life check to bail once |this| is deleted.
2244 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2246 for (JobMap::iterator it
= jobs_
.begin(); self
.get() && it
!= jobs_
.end();) {
2247 Job
* job
= it
->second
;
2249 // This could remove |job| from |jobs_|, but iterator will remain valid.
2250 job
->ServeFromHosts();
2254 void HostResolverImpl::OnIPAddressChanged() {
2255 resolved_known_ipv6_hostname_
= false;
2256 // Abandon all ProbeJobs.
2257 probe_weak_ptr_factory_
.InvalidateWeakPtrs();
2260 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
2261 new LoopbackProbeJob(probe_weak_ptr_factory_
.GetWeakPtr());
2263 AbortAllInProgressJobs();
2264 // |this| may be deleted inside AbortAllInProgressJobs().
2267 void HostResolverImpl::OnDNSChanged() {
2268 DnsConfig dns_config
;
2269 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
2272 net_log_
->AddGlobalEntry(
2273 NetLog::TYPE_DNS_CONFIG_CHANGED
,
2274 base::Bind(&NetLogDnsConfigCallback
, &dns_config
));
2277 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
2278 received_dns_config_
= dns_config
.IsValid();
2279 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
2280 use_local_ipv6_
= !dns_config
.IsValid() || dns_config
.use_local_ipv6
;
2282 num_dns_failures_
= 0;
2284 // We want a new DnsSession in place, before we Abort running Jobs, so that
2285 // the newly started jobs use the new config.
2286 if (dns_client_
.get()) {
2287 dns_client_
->SetConfig(dns_config
);
2288 if (dns_client_
->GetConfig())
2289 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2292 // If the DNS server has changed, existing cached info could be wrong so we
2293 // have to drop our internal cache :( Note that OS level DNS caches, such
2294 // as NSCD's cache should be dropped automatically by the OS when
2295 // resolv.conf changes so we don't need to do anything to clear that cache.
2299 // Life check to bail once |this| is deleted.
2300 base::WeakPtr
<HostResolverImpl
> self
= weak_ptr_factory_
.GetWeakPtr();
2302 // Existing jobs will have been sent to the original server so they need to
2304 AbortAllInProgressJobs();
2306 // |this| may be deleted inside AbortAllInProgressJobs().
2308 TryServingAllJobsFromHosts();
2311 bool HostResolverImpl::HaveDnsConfig() const {
2312 // Use DnsClient only if it's fully configured and there is no override by
2313 // ScopedDefaultHostResolverProc.
2314 // The alternative is to use NetworkChangeNotifier to override DnsConfig,
2315 // but that would introduce construction order requirements for NCN and SDHRP.
2316 return (dns_client_
.get() != NULL
) && (dns_client_
->GetConfig() != NULL
) &&
2317 !(proc_params_
.resolver_proc
.get() == NULL
&&
2318 HostResolverProc::GetDefault() != NULL
);
2321 void HostResolverImpl::OnDnsTaskResolve(int net_error
) {
2322 DCHECK(dns_client_
);
2323 if (net_error
== OK
) {
2324 num_dns_failures_
= 0;
2327 ++num_dns_failures_
;
2328 if (num_dns_failures_
< kMaximumDnsFailures
)
2331 // Disable DnsClient until the next DNS change. Must be done before aborting
2332 // DnsTasks, since doing so may start new jobs.
2333 dns_client_
->SetConfig(DnsConfig());
2335 // Switch jobs with active DnsTasks over to using ProcTasks.
2338 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
2339 UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.DnsClientDisabledReason",
2340 std::abs(net_error
),
2341 GetAllErrorCodesForUma());
2344 void HostResolverImpl::SetDnsClient(scoped_ptr
<DnsClient
> dns_client
) {
2345 // DnsClient and config must be updated before aborting DnsTasks, since doing
2346 // so may start new jobs.
2347 dns_client_
= dns_client
.Pass();
2348 if (dns_client_
&& !dns_client_
->GetConfig() &&
2349 num_dns_failures_
< kMaximumDnsFailures
) {
2350 DnsConfig dns_config
;
2351 NetworkChangeNotifier::GetDnsConfig(&dns_config
);
2352 dns_client_
->SetConfig(dns_config
);
2353 num_dns_failures_
= 0;
2354 if (dns_client_
->GetConfig())
2355 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);