- add sources.
[platform/framework/web/crosswalk.git] / src / net / dns / dns_config_service_win.h
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.
4
5 #ifndef NET_DNS_DNS_CONFIG_SERVICE_WIN_H_
6 #define NET_DNS_DNS_CONFIG_SERVICE_WIN_H_
7
8 // The sole purpose of dns_config_service_win.h is for unittests so we just
9 // include these headers here.
10 #include <winsock2.h>
11 #include <iphlpapi.h>
12
13 #include <string>
14 #include <vector>
15
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/strings/string16.h"
19 #include "net/base/net_export.h"
20 #include "net/dns/dns_config_service.h"
21
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.
28 //
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.
32
33 namespace net {
34
35 namespace internal {
36
37 // Parses |value| as search list (comma-delimited list of domain names) from
38 // a registry key and stores it in |out|. Returns true on success. Empty
39 // entries (e.g., "chromium.org,,org") terminate the list. Non-ascii hostnames
40 // are converted to punycode.
41 bool NET_EXPORT_PRIVATE ParseSearchList(const base::string16& value,
42                                         std::vector<std::string>* out);
43
44 // All relevant settings read from registry and IP Helper. This isolates our
45 // logic from system calls and is exposed for unit tests. Keep it an aggregate
46 // struct for easy initialization.
47 struct NET_EXPORT_PRIVATE DnsSystemSettings {
48   // The |set| flag distinguishes between empty and unset values.
49   struct RegString {
50     bool set;
51     base::string16 value;
52   };
53
54   struct RegDword {
55     bool set;
56     DWORD value;
57   };
58
59   struct DevolutionSetting {
60     // UseDomainNameDevolution
61     RegDword enabled;
62     // DomainNameDevolutionLevel
63     RegDword level;
64   };
65
66   // Filled in by GetAdapterAddresses. Note that the alternative
67   // GetNetworkParams does not include IPv6 addresses.
68   scoped_ptr_malloc<IP_ADAPTER_ADDRESSES> addresses;
69
70   // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\SearchList
71   RegString policy_search_list;
72   // SYSTEM\CurrentControlSet\Tcpip\Parameters\SearchList
73   RegString tcpip_search_list;
74   // SYSTEM\CurrentControlSet\Tcpip\Parameters\Domain
75   RegString tcpip_domain;
76   // SOFTWARE\Policies\Microsoft\System\DNSClient\PrimaryDnsSuffix
77   RegString primary_dns_suffix;
78
79   // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient
80   DevolutionSetting policy_devolution;
81   // SYSTEM\CurrentControlSet\Dnscache\Parameters
82   DevolutionSetting dnscache_devolution;
83   // SYSTEM\CurrentControlSet\Tcpip\Parameters
84   DevolutionSetting tcpip_devolution;
85
86   // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\AppendToMultiLabelName
87   RegDword append_to_multi_label_name;
88
89   // True when the Name Resolution Policy Table (NRPT) has at least one rule:
90   // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\DnsPolicyConfig\Rule*
91   bool have_name_resolution_policy;
92 };
93
94 enum ConfigParseWinResult {
95   CONFIG_PARSE_WIN_OK = 0,
96   CONFIG_PARSE_WIN_READ_IPHELPER,
97   CONFIG_PARSE_WIN_READ_POLICY_SEARCHLIST,
98   CONFIG_PARSE_WIN_READ_TCPIP_SEARCHLIST,
99   CONFIG_PARSE_WIN_READ_DOMAIN,
100   CONFIG_PARSE_WIN_READ_POLICY_DEVOLUTION,
101   CONFIG_PARSE_WIN_READ_DNSCACHE_DEVOLUTION,
102   CONFIG_PARSE_WIN_READ_TCPIP_DEVOLUTION,
103   CONFIG_PARSE_WIN_READ_APPEND_MULTILABEL,
104   CONFIG_PARSE_WIN_READ_PRIMARY_SUFFIX,
105   CONFIG_PARSE_WIN_BAD_ADDRESS,
106   CONFIG_PARSE_WIN_NO_NAMESERVERS,
107   CONFIG_PARSE_WIN_UNHANDLED_OPTIONS,
108   CONFIG_PARSE_WIN_MAX  // Bounding values for enumeration.
109 };
110
111 // Fills in |dns_config| from |settings|. Exposed for tests.
112 ConfigParseWinResult NET_EXPORT_PRIVATE ConvertSettingsToDnsConfig(
113     const DnsSystemSettings& settings,
114     DnsConfig* dns_config);
115
116 // Use DnsConfigService::CreateSystemService to use it outside of tests.
117 class NET_EXPORT_PRIVATE DnsConfigServiceWin : public DnsConfigService {
118  public:
119   DnsConfigServiceWin();
120   virtual ~DnsConfigServiceWin();
121
122  private:
123   class Watcher;
124   class ConfigReader;
125   class HostsReader;
126
127   // DnsConfigService:
128   virtual void ReadNow() OVERRIDE;
129   virtual bool StartWatching() OVERRIDE;
130
131   void OnConfigChanged(bool succeeded);
132   void OnHostsChanged(bool succeeded);
133
134   scoped_ptr<Watcher> watcher_;
135   scoped_refptr<ConfigReader> config_reader_;
136   scoped_refptr<HostsReader> hosts_reader_;
137
138   DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceWin);
139 };
140
141 }  // namespace internal
142
143 }  // namespace net
144
145 #endif  // NET_DNS_DNS_CONFIG_SERVICE_WIN_H_
146