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 : unhandled_options(false),
18 append_to_multi_label_name(true),
19 randomize_ports(false),
21 timeout(base::TimeDelta::FromSeconds(kDnsTimeoutSeconds
)),
25 use_local_ipv6(false) {}
27 DnsConfig::~DnsConfig() {}
29 bool DnsConfig::Equals(const DnsConfig
& d
) const {
30 return EqualsIgnoreHosts(d
) && (hosts
== d
.hosts
);
33 bool DnsConfig::EqualsIgnoreHosts(const DnsConfig
& d
) const {
34 return (nameservers
== d
.nameservers
) &&
35 (search
== d
.search
) &&
36 (unhandled_options
== d
.unhandled_options
) &&
37 (append_to_multi_label_name
== d
.append_to_multi_label_name
) &&
39 (timeout
== d
.timeout
) &&
40 (attempts
== d
.attempts
) &&
41 (rotate
== d
.rotate
) &&
43 (use_local_ipv6
== d
.use_local_ipv6
);
46 void DnsConfig::CopyIgnoreHosts(const DnsConfig
& d
) {
47 nameservers
= d
.nameservers
;
49 unhandled_options
= d
.unhandled_options
;
50 append_to_multi_label_name
= d
.append_to_multi_label_name
;
53 attempts
= d
.attempts
;
56 use_local_ipv6
= d
.use_local_ipv6
;
59 base::Value
* DnsConfig::ToValue() const {
60 base::DictionaryValue
* dict
= new base::DictionaryValue();
62 base::ListValue
* list
= new base::ListValue();
63 for (size_t i
= 0; i
< nameservers
.size(); ++i
)
64 list
->Append(new base::StringValue(nameservers
[i
].ToString()));
65 dict
->Set("nameservers", list
);
67 list
= new base::ListValue();
68 for (size_t i
= 0; i
< search
.size(); ++i
)
69 list
->Append(new base::StringValue(search
[i
]));
70 dict
->Set("search", list
);
72 dict
->SetBoolean("unhandled_options", unhandled_options
);
73 dict
->SetBoolean("append_to_multi_label_name", append_to_multi_label_name
);
74 dict
->SetInteger("ndots", ndots
);
75 dict
->SetDouble("timeout", timeout
.InSecondsF());
76 dict
->SetInteger("attempts", attempts
);
77 dict
->SetBoolean("rotate", rotate
);
78 dict
->SetBoolean("edns0", edns0
);
79 dict
->SetBoolean("use_local_ipv6", use_local_ipv6
);
80 dict
->SetInteger("num_hosts", hosts
.size());
86 DnsConfigService::DnsConfigService()
87 : watch_failed_(false),
91 last_sent_empty_(true) {}
93 DnsConfigService::~DnsConfigService() {
96 void DnsConfigService::ReadConfig(const CallbackType
& callback
) {
97 DCHECK(CalledOnValidThread());
98 DCHECK(!callback
.is_null());
99 DCHECK(callback_
.is_null());
100 callback_
= callback
;
104 void DnsConfigService::WatchConfig(const CallbackType
& callback
) {
105 DCHECK(CalledOnValidThread());
106 DCHECK(!callback
.is_null());
107 DCHECK(callback_
.is_null());
108 callback_
= callback
;
109 watch_failed_
= !StartWatching();
113 void DnsConfigService::InvalidateConfig() {
114 DCHECK(CalledOnValidThread());
115 base::TimeTicks now
= base::TimeTicks::Now();
116 if (!last_invalidate_config_time_
.is_null()) {
117 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval",
118 now
- last_invalidate_config_time_
);
120 last_invalidate_config_time_
= now
;
123 have_config_
= false;
127 void DnsConfigService::InvalidateHosts() {
128 DCHECK(CalledOnValidThread());
129 base::TimeTicks now
= base::TimeTicks::Now();
130 if (!last_invalidate_hosts_time_
.is_null()) {
131 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.HostsNotifyInterval",
132 now
- last_invalidate_hosts_time_
);
134 last_invalidate_hosts_time_
= now
;
141 void DnsConfigService::OnConfigRead(const DnsConfig
& config
) {
142 DCHECK(CalledOnValidThread());
143 DCHECK(config
.IsValid());
145 bool changed
= false;
146 if (!config
.EqualsIgnoreHosts(dns_config_
)) {
147 dns_config_
.CopyIgnoreHosts(config
);
151 if (!changed
&& !last_sent_empty_time_
.is_null()) {
152 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval",
153 base::TimeTicks::Now() - last_sent_empty_time_
);
155 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed
);
158 if (have_hosts_
|| watch_failed_
)
162 void DnsConfigService::OnHostsRead(const DnsHosts
& hosts
) {
163 DCHECK(CalledOnValidThread());
165 bool changed
= false;
166 if (hosts
!= dns_config_
.hosts
) {
167 dns_config_
.hosts
= hosts
;
171 if (!changed
&& !last_sent_empty_time_
.is_null()) {
172 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval",
173 base::TimeTicks::Now() - last_sent_empty_time_
);
175 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed
);
178 if (have_config_
|| watch_failed_
)
182 void DnsConfigService::StartTimer() {
183 DCHECK(CalledOnValidThread());
184 if (last_sent_empty_
) {
185 DCHECK(!timer_
.IsRunning());
186 return; // No need to withdraw again.
190 // Give it a short timeout to come up with a valid config. Otherwise withdraw
191 // the config from the receiver. The goal is to avoid perceivable network
192 // outage (when using the wrong config) but at the same time avoid
193 // unnecessary Job aborts in HostResolverImpl. The signals come from multiple
194 // sources so it might receive multiple events during a config change.
196 // DHCP and user-induced changes are on the order of seconds, so 150ms should
197 // not add perceivable delay. On the other hand, config readers should finish
198 // within 150ms with the rare exception of I/O block or extra large HOSTS.
199 const base::TimeDelta kTimeout
= base::TimeDelta::FromMilliseconds(150);
201 timer_
.Start(FROM_HERE
,
204 &DnsConfigService::OnTimeout
);
207 void DnsConfigService::OnTimeout() {
208 DCHECK(CalledOnValidThread());
209 DCHECK(!last_sent_empty_
);
210 // Indicate that even if there is no change in On*Read, we will need to
211 // update the receiver when the config becomes complete.
213 // Empty config is considered invalid.
214 last_sent_empty_
= true;
215 last_sent_empty_time_
= base::TimeTicks::Now();
216 callback_
.Run(DnsConfig());
219 void DnsConfigService::OnCompleteConfig() {
223 need_update_
= false;
224 last_sent_empty_
= false;
226 // If a watch failed, the config may not be accurate, so report empty.
227 callback_
.Run(DnsConfig());
229 callback_
.Run(dns_config_
);