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/dns_config_service.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/values.h"
10 #include "net/base/ip_endpoint.h"
14 // Default values are taken from glibc resolv.h except timeout which is set to
15 // |kDnsTimeoutSeconds|.
16 DnsConfig::DnsConfig()
17 : append_to_multi_label_name(true),
19 timeout(base::TimeDelta::FromSeconds(kDnsTimeoutSeconds
)),
24 DnsConfig::~DnsConfig() {}
26 bool DnsConfig::Equals(const DnsConfig
& d
) const {
27 return EqualsIgnoreHosts(d
) && (hosts
== d
.hosts
);
30 bool DnsConfig::EqualsIgnoreHosts(const DnsConfig
& d
) const {
31 return (nameservers
== d
.nameservers
) &&
32 (search
== d
.search
) &&
33 (append_to_multi_label_name
== d
.append_to_multi_label_name
) &&
35 (timeout
== d
.timeout
) &&
36 (attempts
== d
.attempts
) &&
37 (rotate
== d
.rotate
) &&
41 void DnsConfig::CopyIgnoreHosts(const DnsConfig
& d
) {
42 nameservers
= d
.nameservers
;
44 append_to_multi_label_name
= d
.append_to_multi_label_name
;
47 attempts
= d
.attempts
;
52 base::Value
* DnsConfig::ToValue() const {
53 DictionaryValue
* dict
= new DictionaryValue();
55 ListValue
* list
= new ListValue();
56 for (size_t i
= 0; i
< nameservers
.size(); ++i
)
57 list
->Append(Value::CreateStringValue(nameservers
[i
].ToString()));
58 dict
->Set("nameservers", list
);
60 list
= new ListValue();
61 for (size_t i
= 0; i
< search
.size(); ++i
)
62 list
->Append(Value::CreateStringValue(search
[i
]));
63 dict
->Set("search", list
);
65 dict
->SetBoolean("append_to_multi_label_name", append_to_multi_label_name
);
66 dict
->SetInteger("ndots", ndots
);
67 dict
->SetDouble("timeout", timeout
.InSecondsF());
68 dict
->SetInteger("attempts", attempts
);
69 dict
->SetBoolean("rotate", rotate
);
70 dict
->SetBoolean("edns0", edns0
);
71 dict
->SetInteger("num_hosts", hosts
.size());
77 DnsConfigService::DnsConfigService()
78 : watch_failed_(false),
82 last_sent_empty_(true) {}
84 DnsConfigService::~DnsConfigService() {
87 void DnsConfigService::ReadConfig(const CallbackType
& callback
) {
88 DCHECK(CalledOnValidThread());
89 DCHECK(!callback
.is_null());
90 DCHECK(callback_
.is_null());
95 void DnsConfigService::WatchConfig(const CallbackType
& callback
) {
96 DCHECK(CalledOnValidThread());
97 DCHECK(!callback
.is_null());
98 DCHECK(callback_
.is_null());
100 watch_failed_
= !StartWatching();
104 void DnsConfigService::InvalidateConfig() {
105 DCHECK(CalledOnValidThread());
106 base::TimeTicks now
= base::TimeTicks::Now();
107 if (!last_invalidate_config_time_
.is_null()) {
108 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval",
109 now
- last_invalidate_config_time_
);
111 last_invalidate_config_time_
= now
;
114 have_config_
= false;
118 void DnsConfigService::InvalidateHosts() {
119 DCHECK(CalledOnValidThread());
120 base::TimeTicks now
= base::TimeTicks::Now();
121 if (!last_invalidate_hosts_time_
.is_null()) {
122 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.HostsNotifyInterval",
123 now
- last_invalidate_hosts_time_
);
125 last_invalidate_hosts_time_
= now
;
132 void DnsConfigService::OnConfigRead(const DnsConfig
& config
) {
133 DCHECK(CalledOnValidThread());
134 DCHECK(config
.IsValid());
136 bool changed
= false;
137 if (!config
.EqualsIgnoreHosts(dns_config_
)) {
138 dns_config_
.CopyIgnoreHosts(config
);
142 if (!changed
&& !last_sent_empty_time_
.is_null()) {
143 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval",
144 base::TimeTicks::Now() - last_sent_empty_time_
);
146 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed
);
149 if (have_hosts_
|| watch_failed_
)
153 void DnsConfigService::OnHostsRead(const DnsHosts
& hosts
) {
154 DCHECK(CalledOnValidThread());
156 bool changed
= false;
157 if (hosts
!= dns_config_
.hosts
) {
158 dns_config_
.hosts
= hosts
;
162 if (!changed
&& !last_sent_empty_time_
.is_null()) {
163 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval",
164 base::TimeTicks::Now() - last_sent_empty_time_
);
166 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed
);
169 if (have_config_
|| watch_failed_
)
173 void DnsConfigService::StartTimer() {
174 DCHECK(CalledOnValidThread());
175 if (last_sent_empty_
) {
176 DCHECK(!timer_
.IsRunning());
177 return; // No need to withdraw again.
181 // Give it a short timeout to come up with a valid config. Otherwise withdraw
182 // the config from the receiver. The goal is to avoid perceivable network
183 // outage (when using the wrong config) but at the same time avoid
184 // unnecessary Job aborts in HostResolverImpl. The signals come from multiple
185 // sources so it might receive multiple events during a config change.
187 // DHCP and user-induced changes are on the order of seconds, so 100ms should
188 // not add perceivable delay. On the other hand, config readers should finish
189 // within 100ms with the rare exception of I/O block or extra large HOSTS.
190 const base::TimeDelta kTimeout
= base::TimeDelta::FromMilliseconds(100);
192 timer_
.Start(FROM_HERE
,
195 &DnsConfigService::OnTimeout
);
198 void DnsConfigService::OnTimeout() {
199 DCHECK(CalledOnValidThread());
200 DCHECK(!last_sent_empty_
);
201 // Indicate that even if there is no change in On*Read, we will need to
202 // update the receiver when the config becomes complete.
204 // Empty config is considered invalid.
205 last_sent_empty_
= true;
206 last_sent_empty_time_
= base::TimeTicks::Now();
207 callback_
.Run(DnsConfig());
210 void DnsConfigService::OnCompleteConfig() {
214 need_update_
= false;
215 last_sent_empty_
= false;
217 // If a watch failed, the config may not be accurate, so report empty.
218 callback_
.Run(DnsConfig());
220 callback_
.Run(dns_config_
);