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_macros.h"
9 #include "base/values.h"
10 #include "net/base/ip_endpoint.h"
11 #include "net/base/ip_pattern.h"
15 NameServerClassifier::NameServerClassifier() {
16 // Google Public DNS addresses from:
17 // https://developers.google.com/speed/public-dns/docs/using
18 AddRule("8.8.8.8", NAME_SERVERS_TYPE_GOOGLE_PUBLIC_DNS
);
19 AddRule("8.8.4.4", NAME_SERVERS_TYPE_GOOGLE_PUBLIC_DNS
);
20 AddRule("2001:4860:4860:0:0:0:0:8888", NAME_SERVERS_TYPE_GOOGLE_PUBLIC_DNS
),
21 AddRule("2001:4860:4860:0:0:0:0:8844", NAME_SERVERS_TYPE_GOOGLE_PUBLIC_DNS
),
23 // Count localhost as private, since we don't know what upstream it uses:
24 AddRule("127.*.*.*", NAME_SERVERS_TYPE_PRIVATE
);
25 AddRule("0:0:0:0:0:0:0:1", NAME_SERVERS_TYPE_PRIVATE
);
27 // RFC 1918 private addresses:
28 AddRule("10.*.*.*", NAME_SERVERS_TYPE_PRIVATE
);
29 AddRule("172.[16-31].*.*", NAME_SERVERS_TYPE_PRIVATE
);
30 AddRule("192.168.*.*", NAME_SERVERS_TYPE_PRIVATE
);
32 // IPv4 link-local addresses:
33 AddRule("169.254.*.*", NAME_SERVERS_TYPE_PRIVATE
);
35 // IPv6 link-local addresses:
36 AddRule("fe80:*:*:*:*:*:*:*", NAME_SERVERS_TYPE_PRIVATE
);
38 // Anything else counts as public:
39 AddRule("*.*.*.*", NAME_SERVERS_TYPE_PUBLIC
);
40 AddRule("*:*:*:*:*:*:*:*", NAME_SERVERS_TYPE_PUBLIC
);
43 NameServerClassifier::~NameServerClassifier() {}
45 NameServerClassifier::NameServersType
NameServerClassifier::GetNameServersType(
46 const std::vector
<IPEndPoint
>& nameservers
) const {
47 NameServersType type
= NAME_SERVERS_TYPE_NONE
;
48 for (std::vector
<IPEndPoint
>::const_iterator it
= nameservers
.begin();
49 it
!= nameservers
.end();
51 type
= MergeNameServersTypes(type
, GetNameServerType(it
->address()));
56 struct NameServerClassifier::NameServerTypeRule
{
57 NameServerTypeRule(const char* pattern_string
, NameServersType type
)
59 bool parsed
= pattern
.ParsePattern(pattern_string
);
67 void NameServerClassifier::AddRule(const char* pattern_string
,
68 NameServersType address_type
) {
69 rules_
.push_back(new NameServerTypeRule(pattern_string
, address_type
));
72 NameServerClassifier::NameServersType
NameServerClassifier::GetNameServerType(
73 const IPAddressNumber
& address
) const {
74 for (ScopedVector
<NameServerTypeRule
>::const_iterator it
= rules_
.begin();
77 if ((*it
)->pattern
.Match(address
))
81 return NAME_SERVERS_TYPE_NONE
;
84 NameServerClassifier::NameServersType
85 NameServerClassifier::MergeNameServersTypes(NameServersType a
,
87 if (a
== NAME_SERVERS_TYPE_NONE
)
89 if (b
== NAME_SERVERS_TYPE_NONE
)
93 return NAME_SERVERS_TYPE_MIXED
;
96 // Default values are taken from glibc resolv.h except timeout which is set to
97 // |kDnsTimeoutSeconds|.
98 DnsConfig::DnsConfig()
99 : unhandled_options(false),
100 append_to_multi_label_name(true),
101 randomize_ports(false),
103 timeout(base::TimeDelta::FromSeconds(kDnsTimeoutSeconds
)),
107 use_local_ipv6(false) {}
109 DnsConfig::~DnsConfig() {}
111 bool DnsConfig::Equals(const DnsConfig
& d
) const {
112 return EqualsIgnoreHosts(d
) && (hosts
== d
.hosts
);
115 bool DnsConfig::EqualsIgnoreHosts(const DnsConfig
& d
) const {
116 return (nameservers
== d
.nameservers
) &&
117 (search
== d
.search
) &&
118 (unhandled_options
== d
.unhandled_options
) &&
119 (append_to_multi_label_name
== d
.append_to_multi_label_name
) &&
120 (ndots
== d
.ndots
) &&
121 (timeout
== d
.timeout
) &&
122 (attempts
== d
.attempts
) &&
123 (rotate
== d
.rotate
) &&
124 (edns0
== d
.edns0
) &&
125 (use_local_ipv6
== d
.use_local_ipv6
);
128 void DnsConfig::CopyIgnoreHosts(const DnsConfig
& d
) {
129 nameservers
= d
.nameservers
;
131 unhandled_options
= d
.unhandled_options
;
132 append_to_multi_label_name
= d
.append_to_multi_label_name
;
135 attempts
= d
.attempts
;
138 use_local_ipv6
= d
.use_local_ipv6
;
141 base::Value
* DnsConfig::ToValue() const {
142 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
144 base::ListValue
* list
= new base::ListValue();
145 for (size_t i
= 0; i
< nameservers
.size(); ++i
)
146 list
->Append(new base::StringValue(nameservers
[i
].ToString()));
147 dict
->Set("nameservers", list
);
149 list
= new base::ListValue();
150 for (size_t i
= 0; i
< search
.size(); ++i
)
151 list
->Append(new base::StringValue(search
[i
]));
152 dict
->Set("search", list
);
154 dict
->SetBoolean("unhandled_options", unhandled_options
);
155 dict
->SetBoolean("append_to_multi_label_name", append_to_multi_label_name
);
156 dict
->SetInteger("ndots", ndots
);
157 dict
->SetDouble("timeout", timeout
.InSecondsF());
158 dict
->SetInteger("attempts", attempts
);
159 dict
->SetBoolean("rotate", rotate
);
160 dict
->SetBoolean("edns0", edns0
);
161 dict
->SetBoolean("use_local_ipv6", use_local_ipv6
);
162 dict
->SetInteger("num_hosts", hosts
.size());
164 return dict
.release();
167 DnsConfigService::DnsConfigService()
168 : watch_failed_(false),
172 last_sent_empty_(true) {}
174 DnsConfigService::~DnsConfigService() {
177 void DnsConfigService::ReadConfig(const CallbackType
& callback
) {
178 DCHECK(CalledOnValidThread());
179 DCHECK(!callback
.is_null());
180 DCHECK(callback_
.is_null());
181 callback_
= callback
;
185 void DnsConfigService::WatchConfig(const CallbackType
& callback
) {
186 DCHECK(CalledOnValidThread());
187 DCHECK(!callback
.is_null());
188 DCHECK(callback_
.is_null());
189 callback_
= callback
;
190 watch_failed_
= !StartWatching();
194 void DnsConfigService::InvalidateConfig() {
195 DCHECK(CalledOnValidThread());
196 base::TimeTicks now
= base::TimeTicks::Now();
197 if (!last_invalidate_config_time_
.is_null()) {
198 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval",
199 now
- last_invalidate_config_time_
);
201 last_invalidate_config_time_
= now
;
204 have_config_
= false;
208 void DnsConfigService::InvalidateHosts() {
209 DCHECK(CalledOnValidThread());
210 base::TimeTicks now
= base::TimeTicks::Now();
211 if (!last_invalidate_hosts_time_
.is_null()) {
212 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.HostsNotifyInterval",
213 now
- last_invalidate_hosts_time_
);
215 last_invalidate_hosts_time_
= now
;
222 void DnsConfigService::OnConfigRead(const DnsConfig
& config
) {
223 DCHECK(CalledOnValidThread());
224 DCHECK(config
.IsValid());
226 bool changed
= false;
227 if (!config
.EqualsIgnoreHosts(dns_config_
)) {
228 dns_config_
.CopyIgnoreHosts(config
);
232 if (!changed
&& !last_sent_empty_time_
.is_null()) {
233 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval",
234 base::TimeTicks::Now() - last_sent_empty_time_
);
236 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed
);
237 UMA_HISTOGRAM_ENUMERATION(
238 "AsyncDNS.NameServersType",
239 classifier_
.GetNameServersType(dns_config_
.nameservers
),
240 NameServerClassifier::NAME_SERVERS_TYPE_MAX_VALUE
);
243 if (have_hosts_
|| watch_failed_
)
247 void DnsConfigService::OnHostsRead(const DnsHosts
& hosts
) {
248 DCHECK(CalledOnValidThread());
250 bool changed
= false;
251 if (hosts
!= dns_config_
.hosts
) {
252 dns_config_
.hosts
= hosts
;
256 if (!changed
&& !last_sent_empty_time_
.is_null()) {
257 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval",
258 base::TimeTicks::Now() - last_sent_empty_time_
);
260 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed
);
263 if (have_config_
|| watch_failed_
)
267 void DnsConfigService::StartTimer() {
268 DCHECK(CalledOnValidThread());
269 if (last_sent_empty_
) {
270 DCHECK(!timer_
.IsRunning());
271 return; // No need to withdraw again.
275 // Give it a short timeout to come up with a valid config. Otherwise withdraw
276 // the config from the receiver. The goal is to avoid perceivable network
277 // outage (when using the wrong config) but at the same time avoid
278 // unnecessary Job aborts in HostResolverImpl. The signals come from multiple
279 // sources so it might receive multiple events during a config change.
281 // DHCP and user-induced changes are on the order of seconds, so 150ms should
282 // not add perceivable delay. On the other hand, config readers should finish
283 // within 150ms with the rare exception of I/O block or extra large HOSTS.
284 const base::TimeDelta kTimeout
= base::TimeDelta::FromMilliseconds(150);
286 timer_
.Start(FROM_HERE
,
289 &DnsConfigService::OnTimeout
);
292 void DnsConfigService::OnTimeout() {
293 DCHECK(CalledOnValidThread());
294 DCHECK(!last_sent_empty_
);
295 // Indicate that even if there is no change in On*Read, we will need to
296 // update the receiver when the config becomes complete.
298 // Empty config is considered invalid.
299 last_sent_empty_
= true;
300 last_sent_empty_time_
= base::TimeTicks::Now();
301 callback_
.Run(DnsConfig());
304 void DnsConfigService::OnCompleteConfig() {
308 need_update_
= false;
309 last_sent_empty_
= false;
311 // If a watch failed, the config may not be accurate, so report empty.
312 callback_
.Run(DnsConfig());
314 callback_
.Run(dns_config_
);