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_win.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_path_watcher.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/metrics/histogram.h"
18 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/synchronization/lock.h"
22 #include "base/threading/non_thread_safe.h"
23 #include "base/threading/thread_restrictions.h"
24 #include "base/time/time.h"
25 #include "base/win/object_watcher.h"
26 #include "base/win/registry.h"
27 #include "base/win/windows_version.h"
28 #include "net/base/net_util.h"
29 #include "net/base/network_change_notifier.h"
30 #include "net/dns/dns_hosts.h"
31 #include "net/dns/dns_protocol.h"
32 #include "net/dns/serial_worker.h"
33 #include "url/url_canon.h"
35 #pragma comment(lib, "iphlpapi.lib")
43 // Interval between retries to parse config. Used only until parsing succeeds.
44 const int kRetryIntervalSeconds
= 5;
46 // Registry key paths.
47 const wchar_t* const kTcpipPath
=
48 L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters";
49 const wchar_t* const kTcpip6Path
=
50 L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters";
51 const wchar_t* const kDnscachePath
=
52 L
"SYSTEM\\CurrentControlSet\\Services\\Dnscache\\Parameters";
53 const wchar_t* const kPolicyPath
=
54 L
"SOFTWARE\\Policies\\Microsoft\\Windows NT\\DNSClient";
55 const wchar_t* const kPrimaryDnsSuffixPath
=
56 L
"SOFTWARE\\Policies\\Microsoft\\System\\DNSClient";
57 const wchar_t* const kNRPTPath
=
58 L
"SOFTWARE\\Policies\\Microsoft\\Windows NT\\DNSClient\\DnsPolicyConfig";
60 enum HostsParseWinResult
{
61 HOSTS_PARSE_WIN_OK
= 0,
62 HOSTS_PARSE_WIN_UNREADABLE_HOSTS_FILE
,
63 HOSTS_PARSE_WIN_COMPUTER_NAME_FAILED
,
64 HOSTS_PARSE_WIN_IPHELPER_FAILED
,
65 HOSTS_PARSE_WIN_BAD_ADDRESS
,
66 HOSTS_PARSE_WIN_MAX
// Bounding values for enumeration.
69 // Convenience for reading values using RegKey.
70 class RegistryReader
: public base::NonThreadSafe
{
72 explicit RegistryReader(const wchar_t* key
) {
73 // Ignoring the result. |key_.Valid()| will catch failures.
74 key_
.Open(HKEY_LOCAL_MACHINE
, key
, KEY_QUERY_VALUE
);
77 bool ReadString(const wchar_t* name
,
78 DnsSystemSettings::RegString
* out
) const {
79 DCHECK(CalledOnValidThread());
82 // Assume that if the |key_| is invalid then the key is missing.
85 LONG result
= key_
.ReadValue(name
, &out
->value
);
86 if (result
== ERROR_SUCCESS
) {
90 return (result
== ERROR_FILE_NOT_FOUND
);
93 bool ReadDword(const wchar_t* name
,
94 DnsSystemSettings::RegDword
* out
) const {
95 DCHECK(CalledOnValidThread());
98 // Assume that if the |key_| is invalid then the key is missing.
101 LONG result
= key_
.ReadValueDW(name
, &out
->value
);
102 if (result
== ERROR_SUCCESS
) {
106 return (result
== ERROR_FILE_NOT_FOUND
);
110 base::win::RegKey key_
;
112 DISALLOW_COPY_AND_ASSIGN(RegistryReader
);
115 // Wrapper for GetAdaptersAddresses. Returns NULL if failed.
116 scoped_ptr
<IP_ADAPTER_ADDRESSES
, base::FreeDeleter
> ReadIpHelper(ULONG flags
) {
117 base::ThreadRestrictions::AssertIOAllowed();
119 scoped_ptr
<IP_ADAPTER_ADDRESSES
, base::FreeDeleter
> out
;
120 ULONG len
= 15000; // As recommended by MSDN for GetAdaptersAddresses.
121 UINT rv
= ERROR_BUFFER_OVERFLOW
;
122 // Try up to three times.
123 for (unsigned tries
= 0; (tries
< 3) && (rv
== ERROR_BUFFER_OVERFLOW
);
125 out
.reset(static_cast<PIP_ADAPTER_ADDRESSES
>(malloc(len
)));
126 rv
= GetAdaptersAddresses(AF_UNSPEC
, flags
, NULL
, out
.get(), &len
);
133 // Converts a base::string16 domain name to ASCII, possibly using punycode.
134 // Returns true if the conversion succeeds and output is not empty. In case of
135 // failure, |domain| might become dirty.
136 bool ParseDomainASCII(const base::string16
& widestr
, std::string
* domain
) {
141 // Check if already ASCII.
142 if (base::IsStringASCII(widestr
)) {
143 *domain
= base::UTF16ToASCII(widestr
);
147 // Otherwise try to convert it from IDN to punycode.
148 const int kInitialBufferSize
= 256;
149 url::RawCanonOutputT
<base::char16
, kInitialBufferSize
> punycode
;
150 if (!url::IDNToASCII(widestr
.data(), widestr
.length(), &punycode
))
153 // |punycode_output| should now be ASCII; convert it to a std::string.
154 // (We could use UTF16ToASCII() instead, but that requires an extra string
155 // copy. Since ASCII is a subset of UTF8 the following is equivalent).
156 bool success
= base::UTF16ToUTF8(punycode
.data(), punycode
.length(), domain
);
158 DCHECK(base::IsStringASCII(*domain
));
159 return success
&& !domain
->empty();
162 bool ReadDevolutionSetting(const RegistryReader
& reader
,
163 DnsSystemSettings::DevolutionSetting
* setting
) {
164 return reader
.ReadDword(L
"UseDomainNameDevolution", &setting
->enabled
) &&
165 reader
.ReadDword(L
"DomainNameDevolutionLevel", &setting
->level
);
168 // Reads DnsSystemSettings from IpHelper and registry.
169 ConfigParseWinResult
ReadSystemSettings(DnsSystemSettings
* settings
) {
170 settings
->addresses
= ReadIpHelper(GAA_FLAG_SKIP_ANYCAST
|
171 GAA_FLAG_SKIP_UNICAST
|
172 GAA_FLAG_SKIP_MULTICAST
|
173 GAA_FLAG_SKIP_FRIENDLY_NAME
);
174 if (!settings
->addresses
.get())
175 return CONFIG_PARSE_WIN_READ_IPHELPER
;
177 RegistryReader
tcpip_reader(kTcpipPath
);
178 RegistryReader
tcpip6_reader(kTcpip6Path
);
179 RegistryReader
dnscache_reader(kDnscachePath
);
180 RegistryReader
policy_reader(kPolicyPath
);
181 RegistryReader
primary_dns_suffix_reader(kPrimaryDnsSuffixPath
);
183 if (!policy_reader
.ReadString(L
"SearchList",
184 &settings
->policy_search_list
)) {
185 return CONFIG_PARSE_WIN_READ_POLICY_SEARCHLIST
;
188 if (!tcpip_reader
.ReadString(L
"SearchList", &settings
->tcpip_search_list
))
189 return CONFIG_PARSE_WIN_READ_TCPIP_SEARCHLIST
;
191 if (!tcpip_reader
.ReadString(L
"Domain", &settings
->tcpip_domain
))
192 return CONFIG_PARSE_WIN_READ_DOMAIN
;
194 if (!ReadDevolutionSetting(policy_reader
, &settings
->policy_devolution
))
195 return CONFIG_PARSE_WIN_READ_POLICY_DEVOLUTION
;
197 if (!ReadDevolutionSetting(dnscache_reader
, &settings
->dnscache_devolution
))
198 return CONFIG_PARSE_WIN_READ_DNSCACHE_DEVOLUTION
;
200 if (!ReadDevolutionSetting(tcpip_reader
, &settings
->tcpip_devolution
))
201 return CONFIG_PARSE_WIN_READ_TCPIP_DEVOLUTION
;
203 if (!policy_reader
.ReadDword(L
"AppendToMultiLabelName",
204 &settings
->append_to_multi_label_name
)) {
205 return CONFIG_PARSE_WIN_READ_APPEND_MULTILABEL
;
208 if (!primary_dns_suffix_reader
.ReadString(L
"PrimaryDnsSuffix",
209 &settings
->primary_dns_suffix
)) {
210 return CONFIG_PARSE_WIN_READ_PRIMARY_SUFFIX
;
213 base::win::RegistryKeyIterator
nrpt_rules(HKEY_LOCAL_MACHINE
, kNRPTPath
);
214 settings
->have_name_resolution_policy
= (nrpt_rules
.SubkeyCount() > 0);
216 return CONFIG_PARSE_WIN_OK
;
219 // Default address of "localhost" and local computer name can be overridden
220 // by the HOSTS file, but if it's not there, then we need to fill it in.
221 HostsParseWinResult
AddLocalhostEntries(DnsHosts
* hosts
) {
222 const unsigned char kIPv4Localhost
[] = { 127, 0, 0, 1 };
223 const unsigned char kIPv6Localhost
[] = { 0, 0, 0, 0, 0, 0, 0, 0,
224 0, 0, 0, 0, 0, 0, 0, 1 };
225 IPAddressNumber
loopback_ipv4(kIPv4Localhost
,
226 kIPv4Localhost
+ arraysize(kIPv4Localhost
));
227 IPAddressNumber
loopback_ipv6(kIPv6Localhost
,
228 kIPv6Localhost
+ arraysize(kIPv6Localhost
));
230 // This does not override any pre-existing entries from the HOSTS file.
231 hosts
->insert(std::make_pair(DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4
),
233 hosts
->insert(std::make_pair(DnsHostsKey("localhost", ADDRESS_FAMILY_IPV6
),
236 WCHAR buffer
[MAX_PATH
];
237 DWORD size
= MAX_PATH
;
238 std::string localname
;
239 if (!GetComputerNameExW(ComputerNameDnsHostname
, buffer
, &size
) ||
240 !ParseDomainASCII(buffer
, &localname
)) {
241 return HOSTS_PARSE_WIN_COMPUTER_NAME_FAILED
;
243 StringToLowerASCII(&localname
);
246 hosts
->count(DnsHostsKey(localname
, ADDRESS_FAMILY_IPV4
)) > 0;
248 hosts
->count(DnsHostsKey(localname
, ADDRESS_FAMILY_IPV6
)) > 0;
250 if (have_ipv4
&& have_ipv6
)
251 return HOSTS_PARSE_WIN_OK
;
253 scoped_ptr
<IP_ADAPTER_ADDRESSES
, base::FreeDeleter
> addresses
=
254 ReadIpHelper(GAA_FLAG_SKIP_ANYCAST
|
255 GAA_FLAG_SKIP_DNS_SERVER
|
256 GAA_FLAG_SKIP_MULTICAST
|
257 GAA_FLAG_SKIP_FRIENDLY_NAME
);
258 if (!addresses
.get())
259 return HOSTS_PARSE_WIN_IPHELPER_FAILED
;
261 // The order of adapters is the network binding order, so stick to the
262 // first good adapter for each family.
263 for (const IP_ADAPTER_ADDRESSES
* adapter
= addresses
.get();
264 adapter
!= NULL
&& (!have_ipv4
|| !have_ipv6
);
265 adapter
= adapter
->Next
) {
266 if (adapter
->OperStatus
!= IfOperStatusUp
)
268 if (adapter
->IfType
== IF_TYPE_SOFTWARE_LOOPBACK
)
271 for (const IP_ADAPTER_UNICAST_ADDRESS
* address
=
272 adapter
->FirstUnicastAddress
;
274 address
= address
->Next
) {
276 if (!ipe
.FromSockAddr(address
->Address
.lpSockaddr
,
277 address
->Address
.iSockaddrLength
)) {
278 return HOSTS_PARSE_WIN_BAD_ADDRESS
;
280 if (!have_ipv4
&& (ipe
.GetFamily() == ADDRESS_FAMILY_IPV4
)) {
282 (*hosts
)[DnsHostsKey(localname
, ADDRESS_FAMILY_IPV4
)] = ipe
.address();
283 } else if (!have_ipv6
&& (ipe
.GetFamily() == ADDRESS_FAMILY_IPV6
)) {
285 (*hosts
)[DnsHostsKey(localname
, ADDRESS_FAMILY_IPV6
)] = ipe
.address();
289 return HOSTS_PARSE_WIN_OK
;
292 // Watches a single registry key for changes.
293 class RegistryWatcher
: public base::win::ObjectWatcher::Delegate
,
294 public base::NonThreadSafe
{
296 typedef base::Callback
<void(bool succeeded
)> CallbackType
;
299 bool Watch(const wchar_t* key
, const CallbackType
& callback
) {
300 DCHECK(CalledOnValidThread());
301 DCHECK(!callback
.is_null());
302 DCHECK(callback_
.is_null());
303 callback_
= callback
;
304 if (key_
.Open(HKEY_LOCAL_MACHINE
, key
, KEY_NOTIFY
) != ERROR_SUCCESS
)
306 if (key_
.StartWatching() != ERROR_SUCCESS
)
308 if (!watcher_
.StartWatching(key_
.watch_event(), this))
313 virtual void OnObjectSignaled(HANDLE object
) OVERRIDE
{
314 DCHECK(CalledOnValidThread());
315 bool succeeded
= (key_
.StartWatching() == ERROR_SUCCESS
) &&
316 watcher_
.StartWatching(key_
.watch_event(), this);
317 if (!succeeded
&& key_
.Valid()) {
318 watcher_
.StopWatching();
322 if (!callback_
.is_null())
323 callback_
.Run(succeeded
);
327 CallbackType callback_
;
328 base::win::RegKey key_
;
329 base::win::ObjectWatcher watcher_
;
331 DISALLOW_COPY_AND_ASSIGN(RegistryWatcher
);
334 // Returns true iff |address| is DNS address from IPv6 stateless discovery,
335 // i.e., matches fec0:0:0:ffff::{1,2,3}.
336 // http://tools.ietf.org/html/draft-ietf-ipngwg-dns-discovery
337 bool IsStatelessDiscoveryAddress(const IPAddressNumber
& address
) {
338 if (address
.size() != kIPv6AddressSize
)
340 const uint8 kPrefix
[] = {
341 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344 return std::equal(kPrefix
, kPrefix
+ arraysize(kPrefix
),
345 address
.begin()) && (address
.back() < 4);
348 // Returns the path to the HOSTS file.
349 base::FilePath
GetHostsPath() {
350 TCHAR buffer
[MAX_PATH
];
351 UINT rc
= GetSystemDirectory(buffer
, MAX_PATH
);
352 DCHECK(0 < rc
&& rc
< MAX_PATH
);
353 return base::FilePath(buffer
).Append(
354 FILE_PATH_LITERAL("drivers\\etc\\hosts"));
357 void ConfigureSuffixSearch(const DnsSystemSettings
& settings
,
359 // SearchList takes precedence, so check it first.
360 if (settings
.policy_search_list
.set
) {
361 std::vector
<std::string
> search
;
362 if (ParseSearchList(settings
.policy_search_list
.value
, &search
)) {
363 config
->search
.swap(search
);
366 // Even if invalid, the policy disables the user-specified setting below.
367 } else if (settings
.tcpip_search_list
.set
) {
368 std::vector
<std::string
> search
;
369 if (ParseSearchList(settings
.tcpip_search_list
.value
, &search
)) {
370 config
->search
.swap(search
);
375 // In absence of explicit search list, suffix search is:
376 // [primary suffix, connection-specific suffix, devolution of primary suffix].
377 // Primary suffix can be set by policy (primary_dns_suffix) or
378 // user setting (tcpip_domain).
380 // The policy (primary_dns_suffix) can be edited via Group Policy Editor
381 // (gpedit.msc) at Local Computer Policy => Computer Configuration
382 // => Administrative Template => Network => DNS Client => Primary DNS Suffix.
384 // The user setting (tcpip_domain) can be configurred at Computer Name in
386 std::string primary_suffix
;
387 if ((settings
.primary_dns_suffix
.set
&&
388 ParseDomainASCII(settings
.primary_dns_suffix
.value
, &primary_suffix
)) ||
389 (settings
.tcpip_domain
.set
&&
390 ParseDomainASCII(settings
.tcpip_domain
.value
, &primary_suffix
))) {
391 // Primary suffix goes in front.
392 config
->search
.insert(config
->search
.begin(), primary_suffix
);
394 return; // No primary suffix, hence no devolution.
397 // Devolution is determined by precedence: policy > dnscache > tcpip.
398 // |enabled|: UseDomainNameDevolution and |level|: DomainNameDevolutionLevel
399 // are overridden independently.
400 DnsSystemSettings::DevolutionSetting devolution
= settings
.policy_devolution
;
402 if (!devolution
.enabled
.set
)
403 devolution
.enabled
= settings
.dnscache_devolution
.enabled
;
404 if (!devolution
.enabled
.set
)
405 devolution
.enabled
= settings
.tcpip_devolution
.enabled
;
406 if (devolution
.enabled
.set
&& (devolution
.enabled
.value
== 0))
407 return; // Devolution disabled.
409 // By default devolution is enabled.
411 if (!devolution
.level
.set
)
412 devolution
.level
= settings
.dnscache_devolution
.level
;
413 if (!devolution
.level
.set
)
414 devolution
.level
= settings
.tcpip_devolution
.level
;
416 // After the recent update, Windows will try to determine a safe default
417 // value by comparing the forest root domain (FRD) to the primary suffix.
418 // See http://support.microsoft.com/kb/957579 for details.
419 // For now, if the level is not set, we disable devolution, assuming that
420 // we will fallback to the system getaddrinfo anyway. This might cause
421 // performance loss for resolutions which depend on the system default
422 // devolution setting.
424 // If the level is explicitly set below 2, devolution is disabled.
425 if (!devolution
.level
.set
|| devolution
.level
.value
< 2)
426 return; // Devolution disabled.
428 // Devolve the primary suffix. This naive logic matches the observed
429 // behavior (see also ParseSearchList). If a suffix is not valid, it will be
430 // discarded when the fully-qualified name is converted to DNS format.
432 unsigned num_dots
= std::count(primary_suffix
.begin(),
433 primary_suffix
.end(), '.');
435 for (size_t offset
= 0; num_dots
>= devolution
.level
.value
; --num_dots
) {
436 offset
= primary_suffix
.find('.', offset
+ 1);
437 config
->search
.push_back(primary_suffix
.substr(offset
+ 1));
443 bool ParseSearchList(const base::string16
& value
,
444 std::vector
<std::string
>* output
) {
451 // If the list includes an empty hostname (",," or ", ,"), it is terminated.
452 // Although nslookup and network connection property tab ignore such
453 // fragments ("a,b,,c" becomes ["a", "b", "c"]), our reference is getaddrinfo
454 // (which sees ["a", "b"]). WMI queries also return a matching search list.
455 std::vector
<base::string16
> woutput
;
456 base::SplitString(value
, ',', &woutput
);
457 for (size_t i
= 0; i
< woutput
.size(); ++i
) {
458 // Convert non-ASCII to punycode, although getaddrinfo does not properly
459 // handle such suffixes.
460 const base::string16
& t
= woutput
[i
];
462 if (!ParseDomainASCII(t
, &parsed
))
464 output
->push_back(parsed
);
466 return !output
->empty();
469 ConfigParseWinResult
ConvertSettingsToDnsConfig(
470 const DnsSystemSettings
& settings
,
472 *config
= DnsConfig();
474 // Use GetAdapterAddresses to get effective DNS server order and
475 // connection-specific DNS suffix. Ignore disconnected and loopback adapters.
476 // The order of adapters is the network binding order, so stick to the
477 // first good adapter.
478 for (const IP_ADAPTER_ADDRESSES
* adapter
= settings
.addresses
.get();
479 adapter
!= NULL
&& config
->nameservers
.empty();
480 adapter
= adapter
->Next
) {
481 if (adapter
->OperStatus
!= IfOperStatusUp
)
483 if (adapter
->IfType
== IF_TYPE_SOFTWARE_LOOPBACK
)
486 for (const IP_ADAPTER_DNS_SERVER_ADDRESS
* address
=
487 adapter
->FirstDnsServerAddress
;
489 address
= address
->Next
) {
491 if (ipe
.FromSockAddr(address
->Address
.lpSockaddr
,
492 address
->Address
.iSockaddrLength
)) {
493 if (IsStatelessDiscoveryAddress(ipe
.address()))
495 // Override unset port.
497 ipe
= IPEndPoint(ipe
.address(), dns_protocol::kDefaultPort
);
498 config
->nameservers
.push_back(ipe
);
500 return CONFIG_PARSE_WIN_BAD_ADDRESS
;
504 // IP_ADAPTER_ADDRESSES in Vista+ has a search list at |FirstDnsSuffix|,
505 // but it came up empty in all trials.
506 // |DnsSuffix| stores the effective connection-specific suffix, which is
507 // obtained via DHCP (regkey: Tcpip\Parameters\Interfaces\{XXX}\DhcpDomain)
508 // or specified by the user (regkey: Tcpip\Parameters\Domain).
509 std::string dns_suffix
;
510 if (ParseDomainASCII(adapter
->DnsSuffix
, &dns_suffix
))
511 config
->search
.push_back(dns_suffix
);
514 if (config
->nameservers
.empty())
515 return CONFIG_PARSE_WIN_NO_NAMESERVERS
; // No point continuing.
517 // Windows always tries a multi-label name "as is" before using suffixes.
520 if (!settings
.append_to_multi_label_name
.set
) {
521 // The default setting is true for XP, false for Vista+.
522 if (base::win::GetVersion() >= base::win::VERSION_VISTA
) {
523 config
->append_to_multi_label_name
= false;
525 config
->append_to_multi_label_name
= true;
528 config
->append_to_multi_label_name
=
529 (settings
.append_to_multi_label_name
.value
!= 0);
532 ConfigParseWinResult result
= CONFIG_PARSE_WIN_OK
;
533 if (settings
.have_name_resolution_policy
) {
534 config
->unhandled_options
= true;
535 // TODO(szym): only set this to true if NRPT has DirectAccess rules.
536 config
->use_local_ipv6
= true;
537 result
= CONFIG_PARSE_WIN_UNHANDLED_OPTIONS
;
540 ConfigureSuffixSearch(settings
, config
);
544 // Watches registry and HOSTS file for changes. Must live on a thread which
546 class DnsConfigServiceWin::Watcher
547 : public NetworkChangeNotifier::IPAddressObserver
{
549 explicit Watcher(DnsConfigServiceWin
* service
) : service_(service
) {}
551 NetworkChangeNotifier::RemoveIPAddressObserver(this);
555 RegistryWatcher::CallbackType callback
=
556 base::Bind(&DnsConfigServiceWin::OnConfigChanged
,
557 base::Unretained(service_
));
561 // The Tcpip key must be present.
562 if (!tcpip_watcher_
.Watch(kTcpipPath
, callback
)) {
563 LOG(ERROR
) << "DNS registry watch failed to start.";
565 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus",
566 DNS_CONFIG_WATCH_FAILED_TO_START_CONFIG
,
567 DNS_CONFIG_WATCH_MAX
);
570 // Watch for IPv6 nameservers.
571 tcpip6_watcher_
.Watch(kTcpip6Path
, callback
);
573 // DNS suffix search list and devolution can be configured via group
574 // policy which sets this registry key. If the key is missing, the policy
575 // does not apply, and the DNS client uses Tcpip and Dnscache settings.
576 // If a policy is installed, DnsConfigService will need to be restarted.
579 dnscache_watcher_
.Watch(kDnscachePath
, callback
);
580 policy_watcher_
.Watch(kPolicyPath
, callback
);
582 if (!hosts_watcher_
.Watch(GetHostsPath(), false,
583 base::Bind(&Watcher::OnHostsChanged
,
584 base::Unretained(this)))) {
585 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus",
586 DNS_CONFIG_WATCH_FAILED_TO_START_HOSTS
,
587 DNS_CONFIG_WATCH_MAX
);
588 LOG(ERROR
) << "DNS hosts watch failed to start.";
591 // Also need to observe changes to local non-loopback IP for DnsHosts.
592 NetworkChangeNotifier::AddIPAddressObserver(this);
598 void OnHostsChanged(const base::FilePath
& path
, bool error
) {
600 NetworkChangeNotifier::RemoveIPAddressObserver(this);
601 service_
->OnHostsChanged(!error
);
604 // NetworkChangeNotifier::IPAddressObserver:
605 virtual void OnIPAddressChanged() OVERRIDE
{
606 // Need to update non-loopback IP of local host.
607 service_
->OnHostsChanged(true);
610 DnsConfigServiceWin
* service_
;
612 RegistryWatcher tcpip_watcher_
;
613 RegistryWatcher tcpip6_watcher_
;
614 RegistryWatcher dnscache_watcher_
;
615 RegistryWatcher policy_watcher_
;
616 base::FilePathWatcher hosts_watcher_
;
618 DISALLOW_COPY_AND_ASSIGN(Watcher
);
621 // Reads config from registry and IpHelper. All work performed on WorkerPool.
622 class DnsConfigServiceWin::ConfigReader
: public SerialWorker
{
624 explicit ConfigReader(DnsConfigServiceWin
* service
)
629 virtual ~ConfigReader() {}
631 virtual void DoWork() OVERRIDE
{
632 // Should be called on WorkerPool.
633 base::TimeTicks start_time
= base::TimeTicks::Now();
634 DnsSystemSettings settings
= {};
635 ConfigParseWinResult result
= ReadSystemSettings(&settings
);
636 if (result
== CONFIG_PARSE_WIN_OK
)
637 result
= ConvertSettingsToDnsConfig(settings
, &dns_config_
);
638 success_
= (result
== CONFIG_PARSE_WIN_OK
||
639 result
== CONFIG_PARSE_WIN_UNHANDLED_OPTIONS
);
640 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ConfigParseWin",
641 result
, CONFIG_PARSE_WIN_MAX
);
642 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigParseResult", success_
);
643 UMA_HISTOGRAM_TIMES("AsyncDNS.ConfigParseDuration",
644 base::TimeTicks::Now() - start_time
);
647 virtual void OnWorkFinished() OVERRIDE
{
648 DCHECK(loop()->BelongsToCurrentThread());
649 DCHECK(!IsCancelled());
651 service_
->OnConfigRead(dns_config_
);
653 LOG(WARNING
) << "Failed to read DnsConfig.";
654 // Try again in a while in case DnsConfigWatcher missed the signal.
655 base::MessageLoop::current()->PostDelayedTask(
657 base::Bind(&ConfigReader::WorkNow
, this),
658 base::TimeDelta::FromSeconds(kRetryIntervalSeconds
));
662 DnsConfigServiceWin
* service_
;
663 // Written in DoWork(), read in OnWorkFinished(). No locking required.
664 DnsConfig dns_config_
;
668 // Reads hosts from HOSTS file and fills in localhost and local computer name if
669 // necessary. All work performed on WorkerPool.
670 class DnsConfigServiceWin::HostsReader
: public SerialWorker
{
672 explicit HostsReader(DnsConfigServiceWin
* service
)
673 : path_(GetHostsPath()),
679 virtual ~HostsReader() {}
681 virtual void DoWork() OVERRIDE
{
682 base::TimeTicks start_time
= base::TimeTicks::Now();
683 HostsParseWinResult result
= HOSTS_PARSE_WIN_UNREADABLE_HOSTS_FILE
;
684 if (ParseHostsFile(path_
, &hosts_
))
685 result
= AddLocalhostEntries(&hosts_
);
686 success_
= (result
== HOSTS_PARSE_WIN_OK
);
687 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.HostsParseWin",
688 result
, HOSTS_PARSE_WIN_MAX
);
689 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostParseResult", success_
);
690 UMA_HISTOGRAM_TIMES("AsyncDNS.HostsParseDuration",
691 base::TimeTicks::Now() - start_time
);
694 virtual void OnWorkFinished() OVERRIDE
{
695 DCHECK(loop()->BelongsToCurrentThread());
697 service_
->OnHostsRead(hosts_
);
699 LOG(WARNING
) << "Failed to read DnsHosts.";
703 const base::FilePath path_
;
704 DnsConfigServiceWin
* service_
;
705 // Written in DoWork, read in OnWorkFinished, no locking necessary.
709 DISALLOW_COPY_AND_ASSIGN(HostsReader
);
712 DnsConfigServiceWin::DnsConfigServiceWin()
713 : config_reader_(new ConfigReader(this)),
714 hosts_reader_(new HostsReader(this)) {}
716 DnsConfigServiceWin::~DnsConfigServiceWin() {
717 config_reader_
->Cancel();
718 hosts_reader_
->Cancel();
721 void DnsConfigServiceWin::ReadNow() {
722 config_reader_
->WorkNow();
723 hosts_reader_
->WorkNow();
726 bool DnsConfigServiceWin::StartWatching() {
727 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139
728 watcher_
.reset(new Watcher(this));
729 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", DNS_CONFIG_WATCH_STARTED
,
730 DNS_CONFIG_WATCH_MAX
);
731 return watcher_
->Watch();
734 void DnsConfigServiceWin::OnConfigChanged(bool succeeded
) {
737 config_reader_
->WorkNow();
739 LOG(ERROR
) << "DNS config watch failed.";
740 set_watch_failed(true);
741 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus",
742 DNS_CONFIG_WATCH_FAILED_CONFIG
,
743 DNS_CONFIG_WATCH_MAX
);
747 void DnsConfigServiceWin::OnHostsChanged(bool succeeded
) {
750 hosts_reader_
->WorkNow();
752 LOG(ERROR
) << "DNS hosts watch failed.";
753 set_watch_failed(true);
754 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus",
755 DNS_CONFIG_WATCH_FAILED_HOSTS
,
756 DNS_CONFIG_WATCH_MAX
);
760 } // namespace internal
763 scoped_ptr
<DnsConfigService
> DnsConfigService::CreateSystemService() {
764 return scoped_ptr
<DnsConfigService
>(new internal::DnsConfigServiceWin());