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 #ifndef NET_DNS_DNS_CONFIG_SERVICE_WIN_H_
6 #define NET_DNS_DNS_CONFIG_SERVICE_WIN_H_
8 // The sole purpose of dns_config_service_win.h is for unittests so we just
9 // include these headers here.
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/string16.h"
19 #include "net/base/net_export.h"
20 #include "net/dns/dns_config_service.h"
22 // The general effort of DnsConfigServiceWin is to configure |nameservers| and
23 // |search| in DnsConfig. The settings are stored in the Windows registry, but
24 // to simplify the task we use the IP Helper API wherever possible. That API
25 // yields the complete and ordered |nameservers|, but to determine |search| we
26 // need to use the registry. On Windows 7, WMI does return the correct |search|
27 // but on earlier versions it is insufficient.
29 // Experimental evaluation of Windows behavior suggests that domain parsing is
30 // naive. Domain suffixes in |search| are not validated until they are appended
31 // to the resolved name. We attempt to replicate this behavior.
37 // Registry key paths.
38 const wchar_t* const kTcpipPath
=
39 L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters";
40 const wchar_t* const kTcpip6Path
=
41 L
"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters";
42 const wchar_t* const kDnscachePath
=
43 L
"SYSTEM\\CurrentControlSet\\Services\\Dnscache\\Parameters";
44 const wchar_t* const kPolicyPath
=
45 L
"SOFTWARE\\Policies\\Microsoft\\Windows NT\\DNSClient";
47 // Returns the path to the HOSTS file.
48 FilePath
GetHostsPath();
50 // Parses |value| as search list (comma-delimited list of domain names) from
51 // a registry key and stores it in |out|. Returns true on success. Empty
52 // entries (e.g., "chromium.org,,org") terminate the list. Non-ascii hostnames
53 // are converted to punycode.
54 bool NET_EXPORT_PRIVATE
ParseSearchList(const string16
& value
,
55 std::vector
<std::string
>* out
);
57 // All relevant settings read from registry and IP Helper. This isolates our
58 // logic from system calls and is exposed for unit tests. Keep it an aggregate
59 // struct for easy initialization.
60 struct NET_EXPORT_PRIVATE DnsSystemSettings
{
61 // The |set| flag distinguishes between empty and unset values.
72 struct DevolutionSetting
{
73 // UseDomainNameDevolution
75 // DomainNameDevolutionLevel
79 // Filled in by GetAdapterAddresses. Note that the alternative
80 // GetNetworkParams does not include IPv6 addresses.
81 scoped_ptr_malloc
<IP_ADAPTER_ADDRESSES
> addresses
;
83 // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\SearchList
84 RegString policy_search_list
;
85 // SYSTEM\CurrentControlSet\Tcpip\Parameters\SearchList
86 RegString tcpip_search_list
;
87 // SYSTEM\CurrentControlSet\Tcpip\Parameters\Domain
88 RegString tcpip_domain
;
89 // SOFTWARE\Policies\Microsoft\System\DNSClient\PrimaryDnsSuffix
90 RegString primary_dns_suffix
;
92 // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient
93 DevolutionSetting policy_devolution
;
94 // SYSTEM\CurrentControlSet\Dnscache\Parameters
95 DevolutionSetting dnscache_devolution
;
96 // SYSTEM\CurrentControlSet\Tcpip\Parameters
97 DevolutionSetting tcpip_devolution
;
99 // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\AppendToMultiLabelName
100 RegDword append_to_multi_label_name
;
103 enum ConfigParseWinResult
{
104 CONFIG_PARSE_WIN_OK
= 0,
105 CONFIG_PARSE_WIN_READ_IPHELPER
,
106 CONFIG_PARSE_WIN_READ_POLICY_SEARCHLIST
,
107 CONFIG_PARSE_WIN_READ_TCPIP_SEARCHLIST
,
108 CONFIG_PARSE_WIN_READ_DOMAIN
,
109 CONFIG_PARSE_WIN_READ_POLICY_DEVOLUTION
,
110 CONFIG_PARSE_WIN_READ_DNSCACHE_DEVOLUTION
,
111 CONFIG_PARSE_WIN_READ_TCPIP_DEVOLUTION
,
112 CONFIG_PARSE_WIN_READ_APPEND_MULTILABEL
,
113 CONFIG_PARSE_WIN_READ_PRIMARY_SUFFIX
,
114 CONFIG_PARSE_WIN_BAD_ADDRESS
,
115 CONFIG_PARSE_WIN_NO_NAMESERVERS
,
116 CONFIG_PARSE_WIN_MAX
// Bounding values for enumeration.
119 // Fills in |dns_config| from |settings|. Exposed for tests.
120 ConfigParseWinResult NET_EXPORT_PRIVATE
ConvertSettingsToDnsConfig(
121 const DnsSystemSettings
& settings
,
122 DnsConfig
* dns_config
);
124 // Use DnsConfigService::CreateSystemService to use it outside of tests.
125 class NET_EXPORT_PRIVATE DnsConfigServiceWin
: public DnsConfigService
{
127 DnsConfigServiceWin();
128 virtual ~DnsConfigServiceWin();
136 virtual void ReadNow() OVERRIDE
;
137 virtual bool StartWatching() OVERRIDE
;
139 void OnConfigChanged(bool succeeded
);
140 void OnHostsChanged(bool succeeded
);
142 scoped_ptr
<Watcher
> watcher_
;
143 scoped_refptr
<ConfigReader
> config_reader_
;
144 scoped_refptr
<HostsReader
> hosts_reader_
;
146 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceWin
);
149 } // namespace internal
153 #endif // NET_DNS_DNS_CONFIG_SERVICE_WIN_H_