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/threading/non_thread_safe.h"
15 #include "base/time.h"
16 #include "base/timer.h"
17 // Needed on shared build with MSVS2010 to avoid multiple definitions of
18 // std::vector<IPEndPoint>.
19 #include "net/base/address_list.h"
20 #include "net/base/ip_endpoint.h" // win requires size of IPEndPoint
21 #include "net/base/net_export.h"
22 #include "net/dns/dns_hosts.h"
30 // Always use 1 second timeout (followed by binary exponential backoff).
31 // TODO(szym): Remove code which reads timeout from system.
32 const unsigned kDnsTimeoutSeconds
= 1;
34 // DnsConfig stores configuration of the system resolver.
35 struct NET_EXPORT_PRIVATE DnsConfig
{
39 bool Equals(const DnsConfig
& d
) const;
41 bool EqualsIgnoreHosts(const DnsConfig
& d
) const;
43 void CopyIgnoreHosts(const DnsConfig
& src
);
45 // Returns a Value representation of |this|. Caller takes ownership of the
46 // returned Value. For performance reasons, the Value only contains the
47 // number of hosts rather than the full list.
48 base::Value
* ToValue() const;
50 bool IsValid() const {
51 return !nameservers
.empty();
54 // List of name server addresses.
55 std::vector
<IPEndPoint
> nameservers
;
56 // Suffix search list; used on first lookup when number of dots in given name
57 // is less than |ndots|.
58 std::vector
<std::string
> search
;
62 // AppendToMultiLabelName: is suffix search performed for multi-label names?
63 // True, except on Windows where it can be configured.
64 bool append_to_multi_label_name
;
66 // Indicates that source port randomization is required. This uses additional
67 // resources on some platforms.
70 // Resolver options; see man resolv.conf.
72 // Minimum number of dots before global resolution precedes |search|.
74 // Time between retransmissions, see res_state.retrans.
75 base::TimeDelta timeout
;
76 // Maximum number of attempts, see res_state.retry.
78 // Round robin entries in |nameservers| for subsequent requests.
80 // Enable EDNS0 extensions.
85 // Service for reading system DNS settings, on demand or when signalled by
86 // internal watchers and NetworkChangeNotifier.
87 class NET_EXPORT_PRIVATE DnsConfigService
88 : NON_EXPORTED_BASE(public base::NonThreadSafe
) {
90 // Callback interface for the client, called on the same thread as
91 // ReadConfig() and WatchConfig().
92 typedef base::Callback
<void(const DnsConfig
& config
)> CallbackType
;
94 // Creates the platform-specific DnsConfigService.
95 static scoped_ptr
<DnsConfigService
> CreateSystemService();
98 virtual ~DnsConfigService();
100 // Attempts to read the configuration. Will run |callback| when succeeded.
101 // Can be called at most once.
102 void ReadConfig(const CallbackType
& callback
);
104 // Registers systems watchers. Will attempt to read config after watch starts,
105 // but only if watchers started successfully. Will run |callback| iff config
106 // changes from last call or has to be withdrawn. Can be called at most once.
107 // Might require MessageLoopForIO.
108 void WatchConfig(const CallbackType
& callback
);
112 DNS_CONFIG_WATCH_STARTED
= 0,
113 DNS_CONFIG_WATCH_FAILED_TO_START_CONFIG
,
114 DNS_CONFIG_WATCH_FAILED_TO_START_HOSTS
,
115 DNS_CONFIG_WATCH_FAILED_CONFIG
,
116 DNS_CONFIG_WATCH_FAILED_HOSTS
,
117 DNS_CONFIG_WATCH_MAX
,
120 // Immediately attempts to read the current configuration.
121 virtual void ReadNow() = 0;
122 // Registers system watchers. Returns true iff succeeds.
123 virtual bool StartWatching() = 0;
125 // Called when the current config (except hosts) has changed.
126 void InvalidateConfig();
127 // Called when the current hosts have changed.
128 void InvalidateHosts();
130 // Called with new config. |config|.hosts is ignored.
131 void OnConfigRead(const DnsConfig
& config
);
132 // Called with new hosts. Rest of the config is assumed unchanged.
133 void OnHostsRead(const DnsHosts
& hosts
);
135 void set_watch_failed(bool value
) { watch_failed_
= value
; }
138 // The timer counts from the last Invalidate* until complete config is read.
141 // Called when the config becomes complete. Stops the timer.
142 void OnCompleteConfig();
144 CallbackType callback_
;
146 DnsConfig dns_config_
;
148 // True if any of the necessary watchers failed. In that case, the service
149 // will communicate changes via OnTimeout, but will only send empty DnsConfig.
151 // True after On*Read, before Invalidate*. Tells if the config is complete.
154 // True if receiver needs to be updated when the config becomes complete.
156 // True if the last config sent was empty (instead of |dns_config_|).
157 // Set when |timer_| expires.
158 bool last_sent_empty_
;
160 // Initialized and updated on Invalidate* call.
161 base::TimeTicks last_invalidate_config_time_
;
162 base::TimeTicks last_invalidate_hosts_time_
;
163 // Initialized and updated when |timer_| expires.
164 base::TimeTicks last_sent_empty_time_
;
166 // Started in Invalidate*, cleared in On*Read.
167 base::OneShotTimer
<DnsConfigService
> timer_
;
169 DISALLOW_COPY_AND_ASSIGN(DnsConfigService
);
174 #endif // NET_DNS_DNS_CONFIG_SERVICE_H_