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_H_
6 #define NET_DNS_DNS_CONFIG_SERVICE_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "base/time/time.h"
17 #include "base/timer/timer.h"
18 // Needed on shared build with MSVS2010 to avoid multiple definitions of
19 // std::vector<IPEndPoint>.
20 #include "net/base/address_list.h"
21 #include "net/base/ip_endpoint.h" // win requires size of IPEndPoint
22 #include "net/base/net_export.h"
23 #include "net/dns/dns_hosts.h"
31 // Always use 1 second timeout (followed by binary exponential backoff).
32 // TODO(szym): Remove code which reads timeout from system.
33 const unsigned kDnsTimeoutSeconds
= 1;
35 // Classifies nameserver address lists for histograms.
36 class NET_EXPORT_PRIVATE NameServerClassifier
{
38 // This is used in a histogram (AsyncDNS.NameServersType); add new entries
39 // right before MAX_VALUE.
40 enum NameServersType
{
41 NAME_SERVERS_TYPE_NONE
,
42 NAME_SERVERS_TYPE_GOOGLE_PUBLIC_DNS
,
43 NAME_SERVERS_TYPE_PRIVATE
,
44 NAME_SERVERS_TYPE_PUBLIC
,
45 NAME_SERVERS_TYPE_MIXED
,
46 NAME_SERVERS_TYPE_MAX_VALUE
49 NameServerClassifier();
50 ~NameServerClassifier();
52 NameServersType
GetNameServersType(
53 const std::vector
<IPEndPoint
>& nameservers
) const;
56 struct NameServerTypeRule
;
58 void AddRule(const char* pattern_string
, NameServersType type
);
59 NameServersType
GetNameServerType(const IPAddressNumber
& address
) const;
60 static NameServersType
MergeNameServersTypes(NameServersType a
,
63 ScopedVector
<NameServerTypeRule
> rules_
;
66 // DnsConfig stores configuration of the system resolver.
67 struct NET_EXPORT_PRIVATE DnsConfig
{
71 bool Equals(const DnsConfig
& d
) const;
73 bool EqualsIgnoreHosts(const DnsConfig
& d
) const;
75 void CopyIgnoreHosts(const DnsConfig
& src
);
77 // Returns a Value representation of |this|. Caller takes ownership of the
78 // returned Value. For performance reasons, the Value only contains the
79 // number of hosts rather than the full list.
80 base::Value
* ToValue() const;
82 bool IsValid() const {
83 return !nameservers
.empty();
86 // List of name server addresses.
87 std::vector
<IPEndPoint
> nameservers
;
88 // Suffix search list; used on first lookup when number of dots in given name
89 // is less than |ndots|.
90 std::vector
<std::string
> search
;
94 // True if there are options set in the system configuration that are not yet
95 // supported by DnsClient.
96 bool unhandled_options
;
98 // AppendToMultiLabelName: is suffix search performed for multi-label names?
99 // True, except on Windows where it can be configured.
100 bool append_to_multi_label_name
;
102 // Indicates that source port randomization is required. This uses additional
103 // resources on some platforms.
104 bool randomize_ports
;
106 // Resolver options; see man resolv.conf.
108 // Minimum number of dots before global resolution precedes |search|.
110 // Time between retransmissions, see res_state.retrans.
111 base::TimeDelta timeout
;
112 // Maximum number of attempts, see res_state.retry.
114 // Round robin entries in |nameservers| for subsequent requests.
116 // Enable EDNS0 extensions.
119 // Indicates system configuration uses local IPv6 connectivity, e.g.,
120 // DirectAccess. This is exposed for HostResolver to skip IPv6 probes,
121 // as it may cause them to return incorrect results.
125 // Service for reading system DNS settings, on demand or when signalled by
126 // internal watchers and NetworkChangeNotifier.
127 class NET_EXPORT_PRIVATE DnsConfigService
128 : NON_EXPORTED_BASE(public base::NonThreadSafe
) {
130 // Callback interface for the client, called on the same thread as
131 // ReadConfig() and WatchConfig().
132 typedef base::Callback
<void(const DnsConfig
& config
)> CallbackType
;
134 // Creates the platform-specific DnsConfigService.
135 static scoped_ptr
<DnsConfigService
> CreateSystemService();
138 virtual ~DnsConfigService();
140 // Attempts to read the configuration. Will run |callback| when succeeded.
141 // Can be called at most once.
142 void ReadConfig(const CallbackType
& callback
);
144 // Registers systems watchers. Will attempt to read config after watch starts,
145 // but only if watchers started successfully. Will run |callback| iff config
146 // changes from last call or has to be withdrawn. Can be called at most once.
147 // Might require MessageLoopForIO.
148 void WatchConfig(const CallbackType
& callback
);
152 DNS_CONFIG_WATCH_STARTED
= 0,
153 DNS_CONFIG_WATCH_FAILED_TO_START_CONFIG
,
154 DNS_CONFIG_WATCH_FAILED_TO_START_HOSTS
,
155 DNS_CONFIG_WATCH_FAILED_CONFIG
,
156 DNS_CONFIG_WATCH_FAILED_HOSTS
,
157 DNS_CONFIG_WATCH_MAX
,
160 // Immediately attempts to read the current configuration.
161 virtual void ReadNow() = 0;
162 // Registers system watchers. Returns true iff succeeds.
163 virtual bool StartWatching() = 0;
165 // Called when the current config (except hosts) has changed.
166 void InvalidateConfig();
167 // Called when the current hosts have changed.
168 void InvalidateHosts();
170 // Called with new config. |config|.hosts is ignored.
171 void OnConfigRead(const DnsConfig
& config
);
172 // Called with new hosts. Rest of the config is assumed unchanged.
173 void OnHostsRead(const DnsHosts
& hosts
);
175 void set_watch_failed(bool value
) { watch_failed_
= value
; }
178 // The timer counts from the last Invalidate* until complete config is read.
181 // Called when the config becomes complete. Stops the timer.
182 void OnCompleteConfig();
184 CallbackType callback_
;
186 DnsConfig dns_config_
;
188 // True if any of the necessary watchers failed. In that case, the service
189 // will communicate changes via OnTimeout, but will only send empty DnsConfig.
191 // True after On*Read, before Invalidate*. Tells if the config is complete.
194 // True if receiver needs to be updated when the config becomes complete.
196 // True if the last config sent was empty (instead of |dns_config_|).
197 // Set when |timer_| expires.
198 bool last_sent_empty_
;
200 // Initialized and updated on Invalidate* call.
201 base::TimeTicks last_invalidate_config_time_
;
202 base::TimeTicks last_invalidate_hosts_time_
;
203 // Initialized and updated when |timer_| expires.
204 base::TimeTicks last_sent_empty_time_
;
206 // Started in Invalidate*, cleared in On*Read.
207 base::OneShotTimer
<DnsConfigService
> timer_
;
209 NameServerClassifier classifier_
;
211 DISALLOW_COPY_AND_ASSIGN(DnsConfigService
);
216 #endif // NET_DNS_DNS_CONFIG_SERVICE_H_