This source file includes following definitions.
- GetAllGetAddrinfoOSErrors
- UmaAsyncDnsResolveStatus
- ResemblesNetBIOSName
- ResemblesMulticastDNSName
- IsGloballyReachable
- RecordTotalTime
- RecordTTL
- ConfigureAsyncDnsNoFallbackFieldTrial
- EnsurePortOnAddressList
- IsAllIPv4Loopback
- NetLogProcTaskFailedCallback
- NetLogDnsTaskFailedCallback
- NetLogRequestInfoCallback
- NetLogJobCreationCallback
- NetLogJobAttachCallback
- NetLogDnsConfigCallback
- LogStartRequest
- LogFinishRequest
- LogCancelRequest
- total_count_
- highest_priority
- total_count
- Add
- Remove
- request_time_
- MarkAsCanceled
- was_canceled
- set_job
- OnComplete
- job
- source_net_log
- request_net_log
- info
- priority
- request_time
- net_log_
- Start
- Cancel
- set_had_non_speculative_request
- was_canceled
- was_completed
- StartLookupAttempt
- DoLookup
- RetryIfNotComplete
- OnLookupComplete
- RecordPerformanceHistograms
- RecordAttemptHistograms
- result_
- LoopbackProbeJob
- DoProbe
- OnProbeComplete
- task_start_time_
- needs_two_transactions
- needs_another_transaction
- StartFirstTransaction
- StartSecondTransaction
- StartA
- StartAAAA
- CreateTransaction
- OnTransactionComplete
- OnSortComplete
- OnFailure
- OnSuccess
- net_log_
- Job
- Schedule
- AddRequest
- CancelRequest
- Abort
- AbortDnsTask
- OnEvicted
- ServeFromHosts
- key
- is_queued
- is_running
- KillDnsTask
- ReduceToOneJobSlot
- UpdatePriority
- MakeAddressListForRequest
- Start
- StartProcTask
- OnProcTaskComplete
- StartDnsTask
- StartSecondDnsTransaction
- OnDnsTaskFailure
- OnDnsTaskComplete
- OnFirstDnsTransactionComplete
- CompleteRequests
- CompleteRequestsWithError
- priority
- num_active_requests
- is_dns_running
- is_proc_running
- retry_factor
- fallback_to_proctask_
- SetMaxQueuedJobs
- ResolveHelper
- ResolveFromCache
- CancelRequest
- SetDefaultAddressFamily
- GetDefaultAddressFamily
- SetDnsClientEnabled
- GetHostCache
- GetDnsConfigAsValue
- ResolveAsIP
- ServeFromCache
- ServeFromHosts
- CacheResult
- RemoveJob
- SetHaveOnlyLoopbackAddresses
- GetEffectiveKeyForRequest
- AbortAllInProgressJobs
- AbortDnsTasks
- TryServingAllJobsFromHosts
- OnIPAddressChanged
- OnDNSChanged
- HaveDnsConfig
- OnDnsTaskResolve
- SetDnsClient
#include "net/dns/host_resolver_impl.h"
#if defined(OS_WIN)
#include <Winsock2.h>
#elif defined(OS_POSIX)
#include <netdb.h>
#endif
#include <cmath>
#include <utility>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/debug/debugger.h"
#include "base/debug/stack_trace.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/worker_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/address_family.h"
#include "net/base/address_list.h"
#include "net/base/dns_reloader.h"
#include "net/base/dns_util.h"
#include "net/base/host_port_pair.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/base/net_util.h"
#include "net/dns/address_sorter.h"
#include "net/dns/dns_client.h"
#include "net/dns/dns_config_service.h"
#include "net/dns/dns_protocol.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_transaction.h"
#include "net/dns/host_resolver_proc.h"
#include "net/socket/client_socket_factory.h"
#include "net/udp/datagram_client_socket.h"
#if defined(OS_WIN)
#include "net/base/winsock_init.h"
#endif
namespace net {
namespace {
const size_t kMaxHostLength = 4096;
const unsigned kCacheEntryTTLSeconds = 60;
const unsigned kNegativeCacheEntryTTLSeconds = 0;
const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
const char kOSErrorsForGetAddrinfoHistogramName[] =
#if defined(OS_WIN)
"Net.OSErrorsForGetAddrinfo_Win";
#elif defined(OS_MACOSX)
"Net.OSErrorsForGetAddrinfo_Mac";
#elif defined(OS_LINUX)
"Net.OSErrorsForGetAddrinfo_Linux";
#else
"Net.OSErrorsForGetAddrinfo";
#endif
std::vector<int> GetAllGetAddrinfoOSErrors() {
int os_errors[] = {
#if defined(OS_POSIX)
#if !defined(OS_FREEBSD)
#if !defined(OS_ANDROID)
EAI_ADDRFAMILY,
#endif
EAI_NODATA,
#endif
EAI_AGAIN,
EAI_BADFLAGS,
EAI_FAIL,
EAI_FAMILY,
EAI_MEMORY,
EAI_NONAME,
EAI_SERVICE,
EAI_SOCKTYPE,
EAI_SYSTEM,
#elif defined(OS_WIN)
WSA_NOT_ENOUGH_MEMORY,
WSAEAFNOSUPPORT,
WSAEINVAL,
WSAESOCKTNOSUPPORT,
WSAHOST_NOT_FOUND,
WSANO_DATA,
WSANO_RECOVERY,
WSANOTINITIALISED,
WSATRY_AGAIN,
WSATYPE_NOT_FOUND,
WSA_INVALID_HANDLE,
#endif
};
for (size_t i = 0; i < arraysize(os_errors); ++i) {
os_errors[i] = std::abs(os_errors[i]);
}
return base::CustomHistogram::ArrayToCustomRanges(os_errors,
arraysize(os_errors));
}
enum DnsResolveStatus {
RESOLVE_STATUS_DNS_SUCCESS = 0,
RESOLVE_STATUS_PROC_SUCCESS,
RESOLVE_STATUS_FAIL,
RESOLVE_STATUS_SUSPECT_NETBIOS,
RESOLVE_STATUS_MAX
};
void UmaAsyncDnsResolveStatus(DnsResolveStatus result) {
UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus",
result,
RESOLVE_STATUS_MAX);
}
bool ResemblesNetBIOSName(const std::string& hostname) {
return (hostname.size() < 16) && (hostname.find('.') == std::string::npos);
}
bool ResemblesMulticastDNSName(const std::string& hostname) {
DCHECK(!hostname.empty());
const char kSuffix[] = ".local.";
const size_t kSuffixLen = sizeof(kSuffix) - 1;
const size_t kSuffixLenTrimmed = kSuffixLen - 1;
if (hostname[hostname.size() - 1] == '.') {
return hostname.size() > kSuffixLen &&
!hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix);
}
return hostname.size() > kSuffixLenTrimmed &&
!hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed,
kSuffix, kSuffixLenTrimmed);
}
bool IsGloballyReachable(const IPAddressNumber& dest,
const BoundNetLog& net_log) {
scoped_ptr<DatagramClientSocket> socket(
ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
DatagramSocket::DEFAULT_BIND,
RandIntCallback(),
net_log.net_log(),
net_log.source()));
int rv = socket->Connect(IPEndPoint(dest, 53));
if (rv != OK)
return false;
IPEndPoint endpoint;
rv = socket->GetLocalAddress(&endpoint);
if (rv != OK)
return false;
DCHECK(endpoint.GetFamily() == ADDRESS_FAMILY_IPV6);
const IPAddressNumber& address = endpoint.address();
bool is_link_local = (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80);
if (is_link_local)
return false;
const uint8 kTeredoPrefix[] = { 0x20, 0x01, 0, 0 };
bool is_teredo = std::equal(kTeredoPrefix,
kTeredoPrefix + arraysize(kTeredoPrefix),
address.begin());
if (is_teredo)
return false;
return true;
}
#define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \
base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100)
#define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \
do { \
switch (priority) { \
case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \
case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \
case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \
case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \
case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \
default: NOTREACHED(); break; \
} \
DNS_HISTOGRAM(basename, time); \
} while (0)
void RecordTotalTime(bool had_dns_config,
bool speculative,
base::TimeDelta duration) {
if (had_dns_config) {
if (speculative) {
DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration);
} else {
DNS_HISTOGRAM("AsyncDNS.TotalTime", duration);
}
} else {
if (speculative) {
DNS_HISTOGRAM("DNS.TotalTime_speculative", duration);
} else {
DNS_HISTOGRAM("DNS.TotalTime", duration);
}
}
}
void RecordTTL(base::TimeDelta ttl) {
UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl,
base::TimeDelta::FromSeconds(1),
base::TimeDelta::FromDays(1), 100);
}
bool ConfigureAsyncDnsNoFallbackFieldTrial() {
const bool kDefault = false;
std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
if (!group_name.empty())
return StartsWithASCII(group_name, "AsyncDnsNoFallback", false);
return kDefault;
}
AddressList EnsurePortOnAddressList(const AddressList& list, uint16 port) {
if (list.empty() || list.front().port() == port)
return list;
return AddressList::CopyWithPort(list, port);
}
bool IsAllIPv4Loopback(const AddressList& addresses) {
for (unsigned i = 0; i < addresses.size(); ++i) {
const IPAddressNumber& address = addresses[i].address();
switch (addresses[i].GetFamily()) {
case ADDRESS_FAMILY_IPV4:
if (address[0] != 127)
return false;
break;
case ADDRESS_FAMILY_IPV6:
return false;
default:
NOTREACHED();
return false;
}
}
return true;
}
base::Value* NetLogProcTaskFailedCallback(uint32 attempt_number,
int net_error,
int os_error,
NetLog::LogLevel ) {
base::DictionaryValue* dict = new base::DictionaryValue();
if (attempt_number)
dict->SetInteger("attempt_number", attempt_number);
dict->SetInteger("net_error", net_error);
if (os_error) {
dict->SetInteger("os_error", os_error);
#if defined(OS_POSIX)
dict->SetString("os_error_string", gai_strerror(os_error));
#elif defined(OS_WIN)
LPWSTR error_string = NULL;
int size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
0,
os_error,
0,
(LPWSTR)&error_string,
0,
0);
dict->SetString("os_error_string", base::WideToUTF8(error_string));
LocalFree(error_string);
#endif
}
return dict;
}
base::Value* NetLogDnsTaskFailedCallback(int net_error,
int dns_error,
NetLog::LogLevel ) {
base::DictionaryValue* dict = new base::DictionaryValue();
dict->SetInteger("net_error", net_error);
if (dns_error)
dict->SetInteger("dns_error", dns_error);
return dict;
};
base::Value* NetLogRequestInfoCallback(const NetLog::Source& source,
const HostResolver::RequestInfo* info,
NetLog::LogLevel ) {
base::DictionaryValue* dict = new base::DictionaryValue();
source.AddToEventParameters(dict);
dict->SetString("host", info->host_port_pair().ToString());
dict->SetInteger("address_family",
static_cast<int>(info->address_family()));
dict->SetBoolean("allow_cached_response", info->allow_cached_response());
dict->SetBoolean("is_speculative", info->is_speculative());
return dict;
}
base::Value* NetLogJobCreationCallback(const NetLog::Source& source,
const std::string* host,
NetLog::LogLevel ) {
base::DictionaryValue* dict = new base::DictionaryValue();
source.AddToEventParameters(dict);
dict->SetString("host", *host);
return dict;
}
base::Value* NetLogJobAttachCallback(const NetLog::Source& source,
RequestPriority priority,
NetLog::LogLevel ) {
base::DictionaryValue* dict = new base::DictionaryValue();
source.AddToEventParameters(dict);
dict->SetString("priority", RequestPriorityToString(priority));
return dict;
}
base::Value* NetLogDnsConfigCallback(const DnsConfig* config,
NetLog::LogLevel ) {
return config->ToValue();
}
void LogStartRequest(const BoundNetLog& source_net_log,
const BoundNetLog& request_net_log,
const HostResolver::RequestInfo& info) {
source_net_log.BeginEvent(
NetLog::TYPE_HOST_RESOLVER_IMPL,
request_net_log.source().ToEventParametersCallback());
request_net_log.BeginEvent(
NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST,
base::Bind(&NetLogRequestInfoCallback, source_net_log.source(), &info));
}
void LogFinishRequest(const BoundNetLog& source_net_log,
const BoundNetLog& request_net_log,
const HostResolver::RequestInfo& info,
int net_error) {
request_net_log.EndEventWithNetErrorCode(
NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, net_error);
source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL);
}
void LogCancelRequest(const BoundNetLog& source_net_log,
const BoundNetLog& request_net_log,
const HostResolverImpl::RequestInfo& info) {
request_net_log.AddEvent(NetLog::TYPE_CANCELLED);
request_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST);
source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL);
}
class PriorityTracker {
public:
explicit PriorityTracker(RequestPriority initial_priority)
: highest_priority_(initial_priority), total_count_(0) {
memset(counts_, 0, sizeof(counts_));
}
RequestPriority highest_priority() const {
return highest_priority_;
}
size_t total_count() const {
return total_count_;
}
void Add(RequestPriority req_priority) {
++total_count_;
++counts_[req_priority];
if (highest_priority_ < req_priority)
highest_priority_ = req_priority;
}
void Remove(RequestPriority req_priority) {
DCHECK_GT(total_count_, 0u);
DCHECK_GT(counts_[req_priority], 0u);
--total_count_;
--counts_[req_priority];
size_t i;
for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i);
highest_priority_ = static_cast<RequestPriority>(i);
if (total_count_ == 0)
DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
}
private:
RequestPriority highest_priority_;
size_t total_count_;
size_t counts_[NUM_PRIORITIES];
};
}
const unsigned HostResolverImpl::kMaximumDnsFailures = 16;
class HostResolverImpl::Request {
public:
Request(const BoundNetLog& source_net_log,
const BoundNetLog& request_net_log,
const RequestInfo& info,
RequestPriority priority,
const CompletionCallback& callback,
AddressList* addresses)
: source_net_log_(source_net_log),
request_net_log_(request_net_log),
info_(info),
priority_(priority),
job_(NULL),
callback_(callback),
addresses_(addresses),
request_time_(base::TimeTicks::Now()) {}
void MarkAsCanceled() {
job_ = NULL;
addresses_ = NULL;
callback_.Reset();
}
bool was_canceled() const {
return callback_.is_null();
}
void set_job(Job* job) {
DCHECK(job);
job_ = job;
}
void OnComplete(int error, const AddressList& addr_list) {
DCHECK(!was_canceled());
if (error == OK)
*addresses_ = EnsurePortOnAddressList(addr_list, info_.port());
CompletionCallback callback = callback_;
MarkAsCanceled();
callback.Run(error);
}
Job* job() const {
return job_;
}
const BoundNetLog& source_net_log() {
return source_net_log_;
}
const BoundNetLog& request_net_log() {
return request_net_log_;
}
const RequestInfo& info() const {
return info_;
}
RequestPriority priority() const { return priority_; }
base::TimeTicks request_time() const { return request_time_; }
private:
BoundNetLog source_net_log_;
BoundNetLog request_net_log_;
const RequestInfo info_;
const RequestPriority priority_;
Job* job_;
CompletionCallback callback_;
AddressList* addresses_;
const base::TimeTicks request_time_;
DISALLOW_COPY_AND_ASSIGN(Request);
};
class HostResolverImpl::ProcTask
: public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> {
public:
typedef base::Callback<void(int net_error,
const AddressList& addr_list)> Callback;
ProcTask(const Key& key,
const ProcTaskParams& params,
const Callback& callback,
const BoundNetLog& job_net_log)
: key_(key),
params_(params),
callback_(callback),
origin_loop_(base::MessageLoopProxy::current()),
attempt_number_(0),
completed_attempt_number_(0),
completed_attempt_error_(ERR_UNEXPECTED),
had_non_speculative_request_(false),
net_log_(job_net_log) {
if (!params_.resolver_proc.get())
params_.resolver_proc = HostResolverProc::GetDefault();
if (!params_.resolver_proc.get())
params_.resolver_proc = new SystemHostResolverProc();
}
void Start() {
DCHECK(origin_loop_->BelongsToCurrentThread());
net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK);
StartLookupAttempt();
}
void Cancel() {
DCHECK(origin_loop_->BelongsToCurrentThread());
if (was_canceled() || was_completed())
return;
callback_.Reset();
net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK);
}
void set_had_non_speculative_request() {
DCHECK(origin_loop_->BelongsToCurrentThread());
had_non_speculative_request_ = true;
}
bool was_canceled() const {
DCHECK(origin_loop_->BelongsToCurrentThread());
return callback_.is_null();
}
bool was_completed() const {
DCHECK(origin_loop_->BelongsToCurrentThread());
return completed_attempt_number_ > 0;
}
private:
friend class base::RefCountedThreadSafe<ProcTask>;
~ProcTask() {}
void StartLookupAttempt() {
DCHECK(origin_loop_->BelongsToCurrentThread());
base::TimeTicks start_time = base::TimeTicks::Now();
++attempt_number_;
if (!base::WorkerPool::PostTask(
FROM_HERE,
base::Bind(&ProcTask::DoLookup, this, start_time, attempt_number_),
true)) {
NOTREACHED();
origin_loop_->PostTask(
FROM_HERE,
base::Bind(&ProcTask::OnLookupComplete, this, AddressList(),
start_time, attempt_number_, ERR_UNEXPECTED, 0));
return;
}
net_log_.AddEvent(
NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED,
NetLog::IntegerCallback("attempt_number", attempt_number_));
if (attempt_number_ <= params_.max_retry_attempts) {
origin_loop_->PostDelayedTask(
FROM_HERE,
base::Bind(&ProcTask::RetryIfNotComplete, this),
params_.unresponsive_delay);
}
}
void DoLookup(const base::TimeTicks& start_time,
const uint32 attempt_number) {
AddressList results;
int os_error = 0;
int error = params_.resolver_proc->Resolve(key_.hostname,
key_.address_family,
key_.host_resolver_flags,
&results,
&os_error);
origin_loop_->PostTask(
FROM_HERE,
base::Bind(&ProcTask::OnLookupComplete, this, results, start_time,
attempt_number, error, os_error));
}
void RetryIfNotComplete() {
DCHECK(origin_loop_->BelongsToCurrentThread());
if (was_completed() || was_canceled())
return;
params_.unresponsive_delay *= params_.retry_factor;
StartLookupAttempt();
}
void OnLookupComplete(const AddressList& results,
const base::TimeTicks& start_time,
const uint32 attempt_number,
int error,
const int os_error) {
DCHECK(origin_loop_->BelongsToCurrentThread());
bool empty_list_on_ok = (error == OK && results.empty());
UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok);
if (empty_list_on_ok)
error = ERR_NAME_NOT_RESOLVED;
bool was_retry_attempt = attempt_number > 1;
if (error != OK && NetworkChangeNotifier::IsOffline())
error = ERR_INTERNET_DISCONNECTED;
if (!was_retry_attempt)
RecordPerformanceHistograms(start_time, error, os_error);
RecordAttemptHistograms(start_time, attempt_number, error, os_error);
if (was_canceled())
return;
NetLog::ParametersCallback net_log_callback;
if (error != OK) {
net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
attempt_number,
error,
os_error);
} else {
net_log_callback = NetLog::IntegerCallback("attempt_number",
attempt_number);
}
net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED,
net_log_callback);
if (was_completed())
return;
results_ = results;
completed_attempt_number_ = attempt_number;
completed_attempt_error_ = error;
if (was_retry_attempt) {
retry_attempt_finished_time_ = base::TimeTicks::Now();
}
if (error != OK) {
net_log_callback = base::Bind(&NetLogProcTaskFailedCallback,
0, error, os_error);
} else {
net_log_callback = results_.CreateNetLogCallback();
}
net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK,
net_log_callback);
callback_.Run(error, results_);
}
void RecordPerformanceHistograms(const base::TimeTicks& start_time,
const int error,
const int os_error) const {
DCHECK(origin_loop_->BelongsToCurrentThread());
enum Category {
RESOLVE_SUCCESS,
RESOLVE_FAIL,
RESOLVE_SPECULATIVE_SUCCESS,
RESOLVE_SPECULATIVE_FAIL,
RESOLVE_MAX,
};
int category = RESOLVE_MAX;
base::TimeDelta duration = base::TimeTicks::Now() - start_time;
if (error == OK) {
if (had_non_speculative_request_) {
category = RESOLVE_SUCCESS;
DNS_HISTOGRAM("DNS.ResolveSuccess", duration);
} else {
category = RESOLVE_SPECULATIVE_SUCCESS;
DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration);
}
switch(key_.address_family) {
case ADDRESS_FAMILY_IPV4:
DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration);
break;
case ADDRESS_FAMILY_IPV6:
DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration);
break;
case ADDRESS_FAMILY_UNSPECIFIED:
DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration);
break;
}
} else {
if (had_non_speculative_request_) {
category = RESOLVE_FAIL;
DNS_HISTOGRAM("DNS.ResolveFail", duration);
} else {
category = RESOLVE_SPECULATIVE_FAIL;
DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration);
}
switch(key_.address_family) {
case ADDRESS_FAMILY_IPV4:
DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration);
break;
case ADDRESS_FAMILY_IPV6:
DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration);
break;
case ADDRESS_FAMILY_UNSPECIFIED:
DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration);
break;
}
UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName,
std::abs(os_error),
GetAllGetAddrinfoOSErrors());
}
DCHECK_LT(category, static_cast<int>(RESOLVE_MAX));
UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category, RESOLVE_MAX);
}
void RecordAttemptHistograms(const base::TimeTicks& start_time,
const uint32 attempt_number,
const int error,
const int os_error) const {
DCHECK(origin_loop_->BelongsToCurrentThread());
bool first_attempt_to_complete =
completed_attempt_number_ == attempt_number;
bool is_first_attempt = (attempt_number == 1);
if (first_attempt_to_complete) {
if (completed_attempt_error_ == OK) {
UMA_HISTOGRAM_ENUMERATION(
"DNS.AttemptFirstSuccess", attempt_number, 100);
} else {
UMA_HISTOGRAM_ENUMERATION(
"DNS.AttemptFirstFailure", attempt_number, 100);
}
}
if (error == OK)
UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100);
else
UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100);
if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) {
DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry",
base::TimeTicks::Now() - retry_attempt_finished_time_);
}
if (was_canceled() || !first_attempt_to_complete) {
UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100);
if (was_canceled())
UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100);
}
base::TimeDelta duration = base::TimeTicks::Now() - start_time;
if (error == OK)
DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration);
else
DNS_HISTOGRAM("DNS.AttemptFailDuration", duration);
}
Key key_;
ProcTaskParams params_;
Callback callback_;
scoped_refptr<base::MessageLoopProxy> origin_loop_;
uint32 attempt_number_;
uint32 completed_attempt_number_;
int completed_attempt_error_;
base::TimeTicks retry_attempt_finished_time_;
bool had_non_speculative_request_;
AddressList results_;
BoundNetLog net_log_;
DISALLOW_COPY_AND_ASSIGN(ProcTask);
};
class HostResolverImpl::LoopbackProbeJob {
public:
explicit LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver)
: resolver_(resolver),
result_(false) {
DCHECK(resolver.get());
const bool kIsSlow = true;
base::WorkerPool::PostTaskAndReply(
FROM_HERE,
base::Bind(&LoopbackProbeJob::DoProbe, base::Unretained(this)),
base::Bind(&LoopbackProbeJob::OnProbeComplete, base::Owned(this)),
kIsSlow);
}
virtual ~LoopbackProbeJob() {}
private:
void DoProbe() {
result_ = HaveOnlyLoopbackAddresses();
}
void OnProbeComplete() {
if (!resolver_.get())
return;
resolver_->SetHaveOnlyLoopbackAddresses(result_);
}
base::WeakPtr<HostResolverImpl> resolver_;
bool result_;
DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob);
};
class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> {
public:
class Delegate {
public:
virtual void OnDnsTaskComplete(base::TimeTicks start_time,
int net_error,
const AddressList& addr_list,
base::TimeDelta ttl) = 0;
virtual void OnFirstDnsTransactionComplete() = 0;
protected:
Delegate() {}
virtual ~Delegate() {}
};
DnsTask(DnsClient* client,
const Key& key,
Delegate* delegate,
const BoundNetLog& job_net_log)
: client_(client),
key_(key),
delegate_(delegate),
net_log_(job_net_log),
num_completed_transactions_(0),
task_start_time_(base::TimeTicks::Now()) {
DCHECK(client);
DCHECK(delegate_);
}
bool needs_two_transactions() const {
return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED;
}
bool needs_another_transaction() const {
return needs_two_transactions() && !transaction_aaaa_;
}
void StartFirstTransaction() {
DCHECK_EQ(0u, num_completed_transactions_);
net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK);
if (key_.address_family == ADDRESS_FAMILY_IPV6) {
StartAAAA();
} else {
StartA();
}
}
void StartSecondTransaction() {
DCHECK(needs_two_transactions());
StartAAAA();
}
private:
void StartA() {
DCHECK(!transaction_a_);
DCHECK_NE(ADDRESS_FAMILY_IPV6, key_.address_family);
transaction_a_ = CreateTransaction(ADDRESS_FAMILY_IPV4);
transaction_a_->Start();
}
void StartAAAA() {
DCHECK(!transaction_aaaa_);
DCHECK_NE(ADDRESS_FAMILY_IPV4, key_.address_family);
transaction_aaaa_ = CreateTransaction(ADDRESS_FAMILY_IPV6);
transaction_aaaa_->Start();
}
scoped_ptr<DnsTransaction> CreateTransaction(AddressFamily family) {
DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family);
return client_->GetTransactionFactory()->CreateTransaction(
key_.hostname,
family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA :
dns_protocol::kTypeA,
base::Bind(&DnsTask::OnTransactionComplete, base::Unretained(this),
base::TimeTicks::Now()),
net_log_);
}
void OnTransactionComplete(const base::TimeTicks& start_time,
DnsTransaction* transaction,
int net_error,
const DnsResponse* response) {
DCHECK(transaction);
base::TimeDelta duration = base::TimeTicks::Now() - start_time;
if (net_error != OK) {
DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration);
OnFailure(net_error, DnsResponse::DNS_PARSE_OK);
return;
}
DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration);
switch (transaction->GetType()) {
case dns_protocol::kTypeA:
DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration);
break;
case dns_protocol::kTypeAAAA:
DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration);
break;
}
AddressList addr_list;
base::TimeDelta ttl;
DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl);
UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList",
result,
DnsResponse::DNS_PARSE_RESULT_MAX);
if (result != DnsResponse::DNS_PARSE_OK) {
OnFailure(ERR_DNS_MALFORMED_RESPONSE, result);
return;
}
++num_completed_transactions_;
if (num_completed_transactions_ == 1) {
ttl_ = ttl;
} else {
ttl_ = std::min(ttl_, ttl);
}
if (transaction->GetType() == dns_protocol::kTypeA) {
DCHECK_EQ(transaction_a_.get(), transaction);
addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end());
} else {
DCHECK_EQ(transaction_aaaa_.get(), transaction);
addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end());
}
if (needs_two_transactions() && num_completed_transactions_ == 1) {
key_.hostname = transaction->GetHostname();
delegate_->OnFirstDnsTransactionComplete();
return;
}
if (addr_list_.empty()) {
OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
return;
}
if (addr_list_.size() > 1 &&
addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) {
client_->GetAddressSorter()->Sort(
addr_list_,
base::Bind(&DnsTask::OnSortComplete,
AsWeakPtr(),
base::TimeTicks::Now()));
} else {
OnSuccess(addr_list_);
}
}
void OnSortComplete(base::TimeTicks start_time,
bool success,
const AddressList& addr_list) {
if (!success) {
DNS_HISTOGRAM("AsyncDNS.SortFailure",
base::TimeTicks::Now() - start_time);
OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK);
return;
}
DNS_HISTOGRAM("AsyncDNS.SortSuccess",
base::TimeTicks::Now() - start_time);
if (addr_list.empty()) {
LOG(WARNING) << "Address list empty after RFC3484 sort";
OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK);
return;
}
OnSuccess(addr_list);
}
void OnFailure(int net_error, DnsResponse::Result result) {
DCHECK_NE(OK, net_error);
net_log_.EndEvent(
NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK,
base::Bind(&NetLogDnsTaskFailedCallback, net_error, result));
delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(),
base::TimeDelta());
}
void OnSuccess(const AddressList& addr_list) {
net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK,
addr_list.CreateNetLogCallback());
delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_);
}
DnsClient* client_;
Key key_;
Delegate* delegate_;
const BoundNetLog net_log_;
scoped_ptr<DnsTransaction> transaction_a_;
scoped_ptr<DnsTransaction> transaction_aaaa_;
unsigned num_completed_transactions_;
base::TimeDelta ttl_;
AddressList addr_list_;
base::TimeTicks task_start_time_;
DISALLOW_COPY_AND_ASSIGN(DnsTask);
};
class HostResolverImpl::Job : public PrioritizedDispatcher::Job,
public HostResolverImpl::DnsTask::Delegate {
public:
Job(const base::WeakPtr<HostResolverImpl>& resolver,
const Key& key,
RequestPriority priority,
const BoundNetLog& request_net_log)
: resolver_(resolver),
key_(key),
priority_tracker_(priority),
had_non_speculative_request_(false),
had_dns_config_(false),
num_occupied_job_slots_(0),
dns_task_error_(OK),
creation_time_(base::TimeTicks::Now()),
priority_change_time_(creation_time_),
net_log_(BoundNetLog::Make(request_net_log.net_log(),
NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)) {
request_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB);
net_log_.BeginEvent(
NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
base::Bind(&NetLogJobCreationCallback,
request_net_log.source(),
&key_.hostname));
}
virtual ~Job() {
if (is_running()) {
if (is_proc_running()) {
proc_task_->Cancel();
proc_task_ = NULL;
}
KillDnsTask();
net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
ERR_ABORTED);
} else if (is_queued()) {
net_log_.AddEvent(NetLog::TYPE_CANCELLED);
net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB);
}
for (RequestsList::const_iterator it = requests_.begin();
it != requests_.end(); ++it) {
Request* req = *it;
if (req->was_canceled())
continue;
DCHECK_EQ(this, req->job());
LogCancelRequest(req->source_net_log(), req->request_net_log(),
req->info());
}
}
void Schedule(bool at_head) {
DCHECK(!is_queued());
PrioritizedDispatcher::Handle handle;
if (!at_head) {
handle = resolver_->dispatcher_.Add(this, priority());
} else {
handle = resolver_->dispatcher_.AddAtHead(this, priority());
}
if (!handle.is_null()) {
DCHECK(handle_.is_null());
handle_ = handle;
}
}
void AddRequest(scoped_ptr<Request> req) {
DCHECK_EQ(key_.hostname, req->info().hostname());
req->set_job(this);
priority_tracker_.Add(req->priority());
req->request_net_log().AddEvent(
NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH,
net_log_.source().ToEventParametersCallback());
net_log_.AddEvent(
NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH,
base::Bind(&NetLogJobAttachCallback,
req->request_net_log().source(),
priority()));
if (!req->info().is_speculative()) {
had_non_speculative_request_ = true;
if (proc_task_.get())
proc_task_->set_had_non_speculative_request();
}
requests_.push_back(req.release());
UpdatePriority();
}
void CancelRequest(Request* req) {
DCHECK_EQ(key_.hostname, req->info().hostname());
DCHECK(!req->was_canceled());
req->MarkAsCanceled();
LogCancelRequest(req->source_net_log(), req->request_net_log(),
req->info());
priority_tracker_.Remove(req->priority());
net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH,
base::Bind(&NetLogJobAttachCallback,
req->request_net_log().source(),
priority()));
if (num_active_requests() > 0) {
UpdatePriority();
} else {
CompleteRequestsWithError(OK );
}
}
void Abort() {
DCHECK(is_running());
CompleteRequestsWithError(ERR_NETWORK_CHANGED);
}
void AbortDnsTask() {
if (dns_task_) {
KillDnsTask();
dns_task_error_ = OK;
StartProcTask();
}
}
void OnEvicted() {
DCHECK(!is_running());
DCHECK(is_queued());
handle_.Reset();
net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED);
CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
}
bool ServeFromHosts() {
DCHECK_GT(num_active_requests(), 0u);
AddressList addr_list;
if (resolver_->ServeFromHosts(key(),
requests_.front()->info(),
&addr_list)) {
CompleteRequests(
HostCache::Entry(OK, MakeAddressListForRequest(addr_list)),
base::TimeDelta());
return true;
}
return false;
}
const Key key() const {
return key_;
}
bool is_queued() const {
return !handle_.is_null();
}
bool is_running() const {
return is_dns_running() || is_proc_running();
}
private:
void KillDnsTask() {
if (dns_task_) {
ReduceToOneJobSlot();
dns_task_.reset();
}
}
void ReduceToOneJobSlot() {
DCHECK_GE(num_occupied_job_slots_, 1u);
if (is_queued()) {
resolver_->dispatcher_.Cancel(handle_);
handle_.Reset();
} else if (num_occupied_job_slots_ > 1) {
resolver_->dispatcher_.OnJobFinished();
--num_occupied_job_slots_;
}
DCHECK_EQ(1u, num_occupied_job_slots_);
}
void UpdatePriority() {
if (is_queued()) {
if (priority() != static_cast<RequestPriority>(handle_.priority()))
priority_change_time_ = base::TimeTicks::Now();
handle_ = resolver_->dispatcher_.ChangePriority(handle_, priority());
}
}
AddressList MakeAddressListForRequest(const AddressList& list) const {
if (requests_.empty())
return list;
return AddressList::CopyWithPort(list, requests_.front()->info().port());
}
virtual void Start() OVERRIDE {
DCHECK_LE(num_occupied_job_slots_, 1u);
handle_.Reset();
++num_occupied_job_slots_;
if (num_occupied_job_slots_ == 2) {
StartSecondDnsTransaction();
return;
}
DCHECK(!is_running());
net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED);
had_dns_config_ = resolver_->HaveDnsConfig();
base::TimeTicks now = base::TimeTicks::Now();
base::TimeDelta queue_time = now - creation_time_;
base::TimeDelta queue_time_after_change = now - priority_change_time_;
if (had_dns_config_) {
DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(),
queue_time);
DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(),
queue_time_after_change);
} else {
DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time);
DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(),
queue_time_after_change);
}
bool system_only =
(key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0;
if (!system_only && had_dns_config_ &&
!ResemblesMulticastDNSName(key_.hostname)) {
StartDnsTask();
} else {
StartProcTask();
}
}
void StartProcTask() {
DCHECK(!is_dns_running());
proc_task_ = new ProcTask(
key_,
resolver_->proc_params_,
base::Bind(&Job::OnProcTaskComplete, base::Unretained(this),
base::TimeTicks::Now()),
net_log_);
if (had_non_speculative_request_)
proc_task_->set_had_non_speculative_request();
proc_task_->Start();
}
void OnProcTaskComplete(base::TimeTicks start_time,
int net_error,
const AddressList& addr_list) {
DCHECK(is_proc_running());
if (!resolver_->resolved_known_ipv6_hostname_ &&
net_error == OK &&
key_.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
if (key_.hostname == "www.google.com") {
resolver_->resolved_known_ipv6_hostname_ = true;
bool got_ipv6_address = false;
for (size_t i = 0; i < addr_list.size(); ++i) {
if (addr_list[i].GetFamily() == ADDRESS_FAMILY_IPV6) {
got_ipv6_address = true;
break;
}
}
UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address);
}
}
if (dns_task_error_ != OK) {
base::TimeDelta duration = base::TimeTicks::Now() - start_time;
if (net_error == OK) {
DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration);
if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) &&
ResemblesNetBIOSName(key_.hostname)) {
UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS);
} else {
UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS);
}
UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.ResolveError",
std::abs(dns_task_error_),
GetAllErrorCodesForUma());
resolver_->OnDnsTaskResolve(dns_task_error_);
} else {
DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration);
UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
}
}
base::TimeDelta ttl =
base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds);
if (net_error == OK)
ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds);
CompleteRequests(
HostCache::Entry(net_error, MakeAddressListForRequest(addr_list)),
ttl);
}
void StartDnsTask() {
DCHECK(resolver_->HaveDnsConfig());
dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this,
net_log_));
dns_task_->StartFirstTransaction();
if (dns_task_->needs_two_transactions())
Schedule(true);
}
void StartSecondDnsTransaction() {
DCHECK(dns_task_->needs_two_transactions());
dns_task_->StartSecondTransaction();
}
void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
base::TimeDelta duration,
int net_error) {
DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration);
if (dns_task == NULL)
return;
dns_task_error_ = net_error;
if (resolver_->fallback_to_proctask_) {
KillDnsTask();
StartProcTask();
} else {
UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL);
CompleteRequestsWithError(net_error);
}
}
virtual void OnDnsTaskComplete(base::TimeTicks start_time,
int net_error,
const AddressList& addr_list,
base::TimeDelta ttl) OVERRIDE {
DCHECK(is_dns_running());
base::TimeDelta duration = base::TimeTicks::Now() - start_time;
if (net_error != OK) {
OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error);
return;
}
DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration);
switch(key_.address_family) {
case ADDRESS_FAMILY_IPV4:
DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration);
break;
case ADDRESS_FAMILY_IPV6:
DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration);
break;
case ADDRESS_FAMILY_UNSPECIFIED:
DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration);
break;
}
UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS);
RecordTTL(ttl);
resolver_->OnDnsTaskResolve(OK);
base::TimeDelta bounded_ttl =
std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds));
CompleteRequests(
HostCache::Entry(net_error, MakeAddressListForRequest(addr_list), ttl),
bounded_ttl);
}
virtual void OnFirstDnsTransactionComplete() OVERRIDE {
DCHECK(dns_task_->needs_two_transactions());
DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued());
ReduceToOneJobSlot();
if (dns_task_->needs_another_transaction())
dns_task_->StartSecondTransaction();
}
void CompleteRequests(const HostCache::Entry& entry,
base::TimeDelta ttl) {
CHECK(resolver_.get());
scoped_ptr<Job> self_deleter(this);
resolver_->RemoveJob(this);
if (is_running()) {
if (is_proc_running()) {
DCHECK(!is_queued());
proc_task_->Cancel();
proc_task_ = NULL;
}
KillDnsTask();
resolver_->dispatcher_.OnJobFinished();
} else if (is_queued()) {
resolver_->dispatcher_.Cancel(handle_);
handle_.Reset();
}
if (num_active_requests() == 0) {
net_log_.AddEvent(NetLog::TYPE_CANCELLED);
net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
OK);
return;
}
net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
entry.error);
DCHECK(!requests_.empty());
if (entry.error == OK) {
UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig",
resolver_->received_dns_config_);
}
bool did_complete = (entry.error != ERR_NETWORK_CHANGED) &&
(entry.error != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE);
if (did_complete)
resolver_->CacheResult(key_, entry, ttl);
for (RequestsList::const_iterator it = requests_.begin();
it != requests_.end(); ++it) {
Request* req = *it;
if (req->was_canceled())
continue;
DCHECK_EQ(this, req->job());
LogFinishRequest(req->source_net_log(), req->request_net_log(),
req->info(), entry.error);
if (did_complete) {
RecordTotalTime(had_dns_config_, req->info().is_speculative(),
base::TimeTicks::Now() - req->request_time());
}
req->OnComplete(entry.error, entry.addrlist);
if (!resolver_.get())
return;
}
}
void CompleteRequestsWithError(int net_error) {
CompleteRequests(HostCache::Entry(net_error, AddressList()),
base::TimeDelta());
}
RequestPriority priority() const {
return priority_tracker_.highest_priority();
}
size_t num_active_requests() const {
return priority_tracker_.total_count();
}
bool is_dns_running() const {
return dns_task_.get() != NULL;
}
bool is_proc_running() const {
return proc_task_.get() != NULL;
}
base::WeakPtr<HostResolverImpl> resolver_;
Key key_;
PriorityTracker priority_tracker_;
bool had_non_speculative_request_;
bool had_dns_config_;
unsigned num_occupied_job_slots_;
int dns_task_error_;
const base::TimeTicks creation_time_;
base::TimeTicks priority_change_time_;
BoundNetLog net_log_;
scoped_refptr<ProcTask> proc_task_;
scoped_ptr<DnsTask> dns_task_;
RequestsList requests_;
PrioritizedDispatcher::Handle handle_;
};
HostResolverImpl::ProcTaskParams::ProcTaskParams(
HostResolverProc* resolver_proc,
size_t max_retry_attempts)
: resolver_proc(resolver_proc),
max_retry_attempts(max_retry_attempts),
unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)),
retry_factor(2) {
}
HostResolverImpl::ProcTaskParams::~ProcTaskParams() {}
HostResolverImpl::HostResolverImpl(
scoped_ptr<HostCache> cache,
const PrioritizedDispatcher::Limits& job_limits,
const ProcTaskParams& proc_params,
NetLog* net_log)
: cache_(cache.Pass()),
dispatcher_(job_limits),
max_queued_jobs_(job_limits.total_jobs * 100u),
proc_params_(proc_params),
net_log_(net_log),
default_address_family_(ADDRESS_FAMILY_UNSPECIFIED),
weak_ptr_factory_(this),
probe_weak_ptr_factory_(this),
received_dns_config_(false),
num_dns_failures_(0),
probe_ipv6_support_(true),
use_local_ipv6_(false),
resolved_known_ipv6_hostname_(false),
additional_resolver_flags_(0),
fallback_to_proctask_(true) {
DCHECK_GE(dispatcher_.num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
static const size_t kDefaultMaxRetryAttempts = 4u;
if (proc_params_.max_retry_attempts == HostResolver::kDefaultRetryAttempts)
proc_params_.max_retry_attempts = kDefaultMaxRetryAttempts;
#if defined(OS_WIN)
EnsureWinsockInit();
#endif
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr());
#endif
NetworkChangeNotifier::AddIPAddressObserver(this);
NetworkChangeNotifier::AddDNSObserver(this);
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
!defined(OS_ANDROID)
EnsureDnsReloaderInit();
#endif
{
DnsConfig dns_config;
NetworkChangeNotifier::GetDnsConfig(&dns_config);
received_dns_config_ = dns_config.IsValid();
use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
}
fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
}
HostResolverImpl::~HostResolverImpl() {
dispatcher_.SetLimitsToZero();
STLDeleteValues(&jobs_);
NetworkChangeNotifier::RemoveIPAddressObserver(this);
NetworkChangeNotifier::RemoveDNSObserver(this);
}
void HostResolverImpl::SetMaxQueuedJobs(size_t value) {
DCHECK_EQ(0u, dispatcher_.num_queued_jobs());
DCHECK_GT(value, 0u);
max_queued_jobs_ = value;
}
int HostResolverImpl::Resolve(const RequestInfo& info,
RequestPriority priority,
AddressList* addresses,
const CompletionCallback& callback,
RequestHandle* out_req,
const BoundNetLog& source_net_log) {
DCHECK(addresses);
DCHECK(CalledOnValidThread());
DCHECK_EQ(false, callback.is_null());
std::string labeled_hostname;
if (!DNSDomainFromDot(info.hostname(), &labeled_hostname))
return ERR_NAME_NOT_RESOLVED;
BoundNetLog request_net_log = BoundNetLog::Make(net_log_,
NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST);
LogStartRequest(source_net_log, request_net_log, info);
Key key = GetEffectiveKeyForRequest(info, request_net_log);
int rv = ResolveHelper(key, info, addresses, request_net_log);
if (rv != ERR_DNS_CACHE_MISS) {
LogFinishRequest(source_net_log, request_net_log, info, rv);
RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta());
return rv;
}
JobMap::iterator jobit = jobs_.find(key);
Job* job;
if (jobit == jobs_.end()) {
job =
new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, request_net_log);
job->Schedule(false);
if (dispatcher_.num_queued_jobs() > max_queued_jobs_) {
Job* evicted = static_cast<Job*>(dispatcher_.EvictOldestLowest());
DCHECK(evicted);
evicted->OnEvicted();
if (evicted == job) {
rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE;
LogFinishRequest(source_net_log, request_net_log, info, rv);
return rv;
}
}
jobs_.insert(jobit, std::make_pair(key, job));
} else {
job = jobit->second;
}
scoped_ptr<Request> req(new Request(
source_net_log, request_net_log, info, priority, callback, addresses));
if (out_req)
*out_req = reinterpret_cast<RequestHandle>(req.get());
job->AddRequest(req.Pass());
return ERR_IO_PENDING;
}
int HostResolverImpl::ResolveHelper(const Key& key,
const RequestInfo& info,
AddressList* addresses,
const BoundNetLog& request_net_log) {
if (info.hostname().empty() || info.hostname().size() > kMaxHostLength)
return ERR_NAME_NOT_RESOLVED;
int net_error = ERR_UNEXPECTED;
if (ResolveAsIP(key, info, &net_error, addresses))
return net_error;
if (ServeFromCache(key, info, &net_error, addresses)) {
request_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT);
return net_error;
}
if (ServeFromHosts(key, info, addresses)) {
request_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT);
return OK;
}
return ERR_DNS_CACHE_MISS;
}
int HostResolverImpl::ResolveFromCache(const RequestInfo& info,
AddressList* addresses,
const BoundNetLog& source_net_log) {
DCHECK(CalledOnValidThread());
DCHECK(addresses);
BoundNetLog request_net_log = BoundNetLog::Make(net_log_,
NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST);
LogStartRequest(source_net_log, request_net_log, info);
Key key = GetEffectiveKeyForRequest(info, request_net_log);
int rv = ResolveHelper(key, info, addresses, request_net_log);
LogFinishRequest(source_net_log, request_net_log, info, rv);
return rv;
}
void HostResolverImpl::CancelRequest(RequestHandle req_handle) {
DCHECK(CalledOnValidThread());
Request* req = reinterpret_cast<Request*>(req_handle);
DCHECK(req);
Job* job = req->job();
DCHECK(job);
job->CancelRequest(req);
}
void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) {
DCHECK(CalledOnValidThread());
default_address_family_ = address_family;
probe_ipv6_support_ = false;
}
AddressFamily HostResolverImpl::GetDefaultAddressFamily() const {
return default_address_family_;
}
void HostResolverImpl::SetDnsClientEnabled(bool enabled) {
DCHECK(CalledOnValidThread());
#if defined(ENABLE_BUILT_IN_DNS)
if (enabled && !dns_client_) {
SetDnsClient(DnsClient::CreateClient(net_log_));
} else if (!enabled && dns_client_) {
SetDnsClient(scoped_ptr<DnsClient>());
}
#endif
}
HostCache* HostResolverImpl::GetHostCache() {
return cache_.get();
}
base::Value* HostResolverImpl::GetDnsConfigAsValue() const {
if (!dns_client_.get())
return NULL;
const DnsConfig* dns_config = dns_client_->GetConfig();
if (dns_config == NULL)
return new base::DictionaryValue();
return dns_config->ToValue();
}
bool HostResolverImpl::ResolveAsIP(const Key& key,
const RequestInfo& info,
int* net_error,
AddressList* addresses) {
DCHECK(addresses);
DCHECK(net_error);
IPAddressNumber ip_number;
if (!ParseIPLiteralToNumber(key.hostname, &ip_number))
return false;
DCHECK_EQ(key.host_resolver_flags &
~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY |
HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6),
0) << " Unhandled flag";
bool ipv6_disabled = (default_address_family_ == ADDRESS_FAMILY_IPV4) &&
!probe_ipv6_support_;
*net_error = OK;
if ((ip_number.size() == kIPv6AddressSize) && ipv6_disabled) {
*net_error = ERR_NAME_NOT_RESOLVED;
} else {
*addresses = AddressList::CreateFromIPAddress(ip_number, info.port());
if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)
addresses->SetDefaultCanonicalName();
}
return true;
}
bool HostResolverImpl::ServeFromCache(const Key& key,
const RequestInfo& info,
int* net_error,
AddressList* addresses) {
DCHECK(addresses);
DCHECK(net_error);
if (!info.allow_cached_response() || !cache_.get())
return false;
const HostCache::Entry* cache_entry = cache_->Lookup(
key, base::TimeTicks::Now());
if (!cache_entry)
return false;
*net_error = cache_entry->error;
if (*net_error == OK) {
if (cache_entry->has_ttl())
RecordTTL(cache_entry->ttl);
*addresses = EnsurePortOnAddressList(cache_entry->addrlist, info.port());
}
return true;
}
bool HostResolverImpl::ServeFromHosts(const Key& key,
const RequestInfo& info,
AddressList* addresses) {
DCHECK(addresses);
if (!HaveDnsConfig())
return false;
addresses->clear();
std::string hostname = StringToLowerASCII(key.hostname);
const DnsHosts& hosts = dns_client_->GetConfig()->hosts;
if (key.address_family == ADDRESS_FAMILY_IPV6 ||
key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
DnsHosts::const_iterator it = hosts.find(
DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6));
if (it != hosts.end())
addresses->push_back(IPEndPoint(it->second, info.port()));
}
if (key.address_family == ADDRESS_FAMILY_IPV4 ||
key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
DnsHosts::const_iterator it = hosts.find(
DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4));
if (it != hosts.end())
addresses->push_back(IPEndPoint(it->second, info.port()));
}
if ((key.host_resolver_flags &
HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
IsAllIPv4Loopback(*addresses)) {
Key new_key(key);
new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED;
new_key.host_resolver_flags &=
~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
return ServeFromHosts(new_key, info, addresses);
}
return !addresses->empty();
}
void HostResolverImpl::CacheResult(const Key& key,
const HostCache::Entry& entry,
base::TimeDelta ttl) {
if (cache_.get())
cache_->Set(key, entry, base::TimeTicks::Now(), ttl);
}
void HostResolverImpl::RemoveJob(Job* job) {
DCHECK(job);
JobMap::iterator it = jobs_.find(job->key());
if (it != jobs_.end() && it->second == job)
jobs_.erase(it);
}
void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) {
if (result) {
additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
} else {
additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
}
}
HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest(
const RequestInfo& info, const BoundNetLog& net_log) const {
HostResolverFlags effective_flags =
info.host_resolver_flags() | additional_resolver_flags_;
AddressFamily effective_address_family = info.address_family();
if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) {
if (probe_ipv6_support_ && !use_local_ipv6_) {
base::TimeTicks start_time = base::TimeTicks::Now();
const uint8 kIPv6Address[] =
{ 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 };
IPAddressNumber address(kIPv6Address,
kIPv6Address + arraysize(kIPv6Address));
BoundNetLog probe_net_log = BoundNetLog::Make(
net_log.net_log(), NetLog::SOURCE_IPV6_REACHABILITY_CHECK);
probe_net_log.BeginEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK,
net_log.source().ToEventParametersCallback());
bool rv6 = IsGloballyReachable(address, probe_net_log);
probe_net_log.EndEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK);
if (rv6)
net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_SUPPORTED);
UMA_HISTOGRAM_TIMES("Net.IPv6ConnectDuration",
base::TimeTicks::Now() - start_time);
if (rv6) {
UMA_HISTOGRAM_BOOLEAN("Net.IPv6ConnectSuccessMatch",
default_address_family_ == ADDRESS_FAMILY_UNSPECIFIED);
} else {
UMA_HISTOGRAM_BOOLEAN("Net.IPv6ConnectFailureMatch",
default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED);
effective_address_family = ADDRESS_FAMILY_IPV4;
effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
}
} else {
effective_address_family = default_address_family_;
}
}
return Key(info.hostname(), effective_address_family, effective_flags);
}
void HostResolverImpl::AbortAllInProgressJobs() {
ScopedVector<Job> jobs_to_abort;
for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) {
Job* job = it->second;
if (job->is_running()) {
jobs_to_abort.push_back(job);
jobs_.erase(it++);
} else {
DCHECK(job->is_queued());
++it;
}
}
PrioritizedDispatcher::Limits limits = dispatcher_.GetLimits();
dispatcher_.SetLimits(
PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
jobs_to_abort[i]->Abort();
jobs_to_abort[i] = NULL;
}
if (self)
dispatcher_.SetLimits(limits);
}
void HostResolverImpl::AbortDnsTasks() {
PrioritizedDispatcher::Limits limits = dispatcher_.GetLimits();
dispatcher_.SetLimits(
PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
it->second->AbortDnsTask();
dispatcher_.SetLimits(limits);
}
void HostResolverImpl::TryServingAllJobsFromHosts() {
if (!HaveDnsConfig())
return;
base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
for (JobMap::iterator it = jobs_.begin(); self.get() && it != jobs_.end();) {
Job* job = it->second;
++it;
job->ServeFromHosts();
}
}
void HostResolverImpl::OnIPAddressChanged() {
resolved_known_ipv6_hostname_ = false;
probe_weak_ptr_factory_.InvalidateWeakPtrs();
if (cache_.get())
cache_->clear();
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr());
#endif
AbortAllInProgressJobs();
}
void HostResolverImpl::OnDNSChanged() {
DnsConfig dns_config;
NetworkChangeNotifier::GetDnsConfig(&dns_config);
if (net_log_) {
net_log_->AddGlobalEntry(
NetLog::TYPE_DNS_CONFIG_CHANGED,
base::Bind(&NetLogDnsConfigCallback, &dns_config));
}
received_dns_config_ = dns_config.IsValid();
use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
num_dns_failures_ = 0;
if (dns_client_.get()) {
dns_client_->SetConfig(dns_config);
if (dns_client_->GetConfig())
UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
}
if (cache_.get())
cache_->clear();
base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
AbortAllInProgressJobs();
if (self.get())
TryServingAllJobsFromHosts();
}
bool HostResolverImpl::HaveDnsConfig() const {
return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL) &&
!(proc_params_.resolver_proc.get() == NULL &&
HostResolverProc::GetDefault() != NULL);
}
void HostResolverImpl::OnDnsTaskResolve(int net_error) {
DCHECK(dns_client_);
if (net_error == OK) {
num_dns_failures_ = 0;
return;
}
++num_dns_failures_;
if (num_dns_failures_ < kMaximumDnsFailures)
return;
dns_client_->SetConfig(DnsConfig());
AbortDnsTasks();
UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false);
UMA_HISTOGRAM_CUSTOM_ENUMERATION("AsyncDNS.DnsClientDisabledReason",
std::abs(net_error),
GetAllErrorCodesForUma());
}
void HostResolverImpl::SetDnsClient(scoped_ptr<DnsClient> dns_client) {
dns_client_ = dns_client.Pass();
if (dns_client_ && !dns_client_->GetConfig() &&
num_dns_failures_ < kMaximumDnsFailures) {
DnsConfig dns_config;
NetworkChangeNotifier::GetDnsConfig(&dns_config);
dns_client_->SetConfig(dns_config);
num_dns_failures_ = 0;
if (dns_client_->GetConfig())
UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
}
AbortDnsTasks();
}
}