blob: cfa0aa8eacd4a3e87625ed38852ded918061c583 [file] [log] [blame]
[email protected]d0d49dd82012-01-26 00:03:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d84316a2011-08-18 04:41:212// 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_H_
6#define NET_DNS_DNS_CONFIG_SERVICE_H_
[email protected]d84316a2011-08-18 04:41:217
[email protected]6998a662011-09-03 00:17:298#include <map>
[email protected]d84316a2011-08-18 04:41:219#include <string>
10#include <vector>
11
[email protected]6998a662011-09-03 00:17:2912#include "base/gtest_prod_util.h"
[email protected]b3601bc22012-02-21 21:23:2013#include "base/memory/scoped_ptr.h"
[email protected]76d7f722011-10-10 17:22:4114#include "base/threading/non_thread_safe.h"
[email protected]d84316a2011-08-18 04:41:2115#include "base/time.h"
[email protected]b4481b222012-03-16 17:13:1116#include "base/timer.h"
[email protected]7054e78f2012-05-07 21:44:5617// Needed on shared build with MSVS2010 to avoid multiple definitions of
18// std::vector<IPEndPoint>.
19#include "net/base/address_list.h"
[email protected]6998a662011-09-03 00:17:2920#include "net/base/ip_endpoint.h" // win requires size of IPEndPoint
[email protected]d84316a2011-08-18 04:41:2121#include "net/base/net_export.h"
[email protected]6998a662011-09-03 00:17:2922#include "net/dns/dns_hosts.h"
[email protected]d84316a2011-08-18 04:41:2123
[email protected]17e92032012-03-29 00:56:2424namespace base {
25class Value;
26}
27
[email protected]d84316a2011-08-18 04:41:2128namespace net {
29
[email protected]05b4a322012-08-15 01:26:4530// Always use 1 second timeout (followed by binary exponential backoff).
31// TODO(szym): Remove code which reads timeout from system.
32const unsigned kDnsTimeoutSeconds = 1;
33
[email protected]d84316a2011-08-18 04:41:2134// DnsConfig stores configuration of the system resolver.
35struct NET_EXPORT_PRIVATE DnsConfig {
36 DnsConfig();
37 virtual ~DnsConfig();
38
39 bool Equals(const DnsConfig& d) const;
40
[email protected]6998a662011-09-03 00:17:2941 bool EqualsIgnoreHosts(const DnsConfig& d) const;
42
[email protected]b4481b222012-03-16 17:13:1143 void CopyIgnoreHosts(const DnsConfig& src);
44
[email protected]17e92032012-03-29 00:56:2445 // 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;
49
[email protected]6998a662011-09-03 00:17:2950 bool IsValid() const {
[email protected]d84316a2011-08-18 04:41:2151 return !nameservers.empty();
52 }
53
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;
59
[email protected]6998a662011-09-03 00:17:2960 DnsHosts hosts;
61
[email protected]fea484e2012-02-07 23:21:5762 // 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;
65
[email protected]d84316a2011-08-18 04:41:2166 // Resolver options; see man resolv.conf.
[email protected]d84316a2011-08-18 04:41:2167
68 // Minimum number of dots before global resolution precedes |search|.
69 int ndots;
70 // Time between retransmissions, see res_state.retrans.
71 base::TimeDelta timeout;
[email protected]d0d49dd82012-01-26 00:03:5972 // Maximum number of attempts, see res_state.retry.
[email protected]d84316a2011-08-18 04:41:2173 int attempts;
74 // Round robin entries in |nameservers| for subsequent requests.
75 bool rotate;
76 // Enable EDNS0 extensions.
77 bool edns0;
78};
79
[email protected]6998a662011-09-03 00:17:2980
[email protected]05aad32d2012-05-16 18:10:5381// Service for reading system DNS settings, on demand or when signalled by
[email protected]bb0e34542012-08-31 19:52:4082// internal watchers and NetworkChangeNotifier.
[email protected]6998a662011-09-03 00:17:2983class NET_EXPORT_PRIVATE DnsConfigService
[email protected]bb0e34542012-08-31 19:52:4084 : NON_EXPORTED_BASE(public base::NonThreadSafe) {
[email protected]d84316a2011-08-18 04:41:2185 public:
[email protected]bb0e34542012-08-31 19:52:4086 // Callback interface for the client, called on the same thread as
87 // ReadConfig() and WatchConfig().
[email protected]b4481b222012-03-16 17:13:1188 typedef base::Callback<void(const DnsConfig& config)> CallbackType;
[email protected]d84316a2011-08-18 04:41:2189
90 // Creates the platform-specific DnsConfigService.
[email protected]b3601bc22012-02-21 21:23:2091 static scoped_ptr<DnsConfigService> CreateSystemService();
[email protected]d84316a2011-08-18 04:41:2192
[email protected]6998a662011-09-03 00:17:2993 DnsConfigService();
94 virtual ~DnsConfigService();
[email protected]d84316a2011-08-18 04:41:2195
[email protected]05aad32d2012-05-16 18:10:5396 // Attempts to read the configuration. Will run |callback| when succeeded.
97 // Can be called at most once.
[email protected]bb0e34542012-08-31 19:52:4098 void ReadConfig(const CallbackType& callback);
[email protected]05aad32d2012-05-16 18:10:5399
[email protected]bb0e34542012-08-31 19:52:40100 // Registers systems watchers. Will attempt to read config after watch starts,
101 // but only if watchers started successfully. Will run |callback| iff config
102 // changes from last call or has to be withdrawn. Can be called at most once.
103 // Might require MessageLoopForIO.
104 void WatchConfig(const CallbackType& callback);
[email protected]d84316a2011-08-18 04:41:21105
[email protected]6998a662011-09-03 00:17:29106 protected:
[email protected]bb0e34542012-08-31 19:52:40107 // Immediately attempts to read the current configuration.
108 virtual void ReadNow() = 0;
109 // Registers system watchers. Returns true iff succeeds.
110 virtual bool StartWatching() = 0;
111
[email protected]b4481b222012-03-16 17:13:11112 // Called when the current config (except hosts) has changed.
113 void InvalidateConfig();
114 // Called when the current hosts have changed.
115 void InvalidateHosts();
116
117 // Called with new config. |config|.hosts is ignored.
118 void OnConfigRead(const DnsConfig& config);
119 // Called with new hosts. Rest of the config is assumed unchanged.
120 void OnHostsRead(const DnsHosts& hosts);
121
[email protected]bb0e34542012-08-31 19:52:40122 void set_watch_failed(bool value) { watch_failed_ = value; }
[email protected]b4481b222012-03-16 17:13:11123
124 private:
[email protected]bb0e34542012-08-31 19:52:40125 // The timer counts from the last Invalidate* until complete config is read.
[email protected]b4481b222012-03-16 17:13:11126 void StartTimer();
[email protected]b4481b222012-03-16 17:13:11127 void OnTimeout();
[email protected]bb0e34542012-08-31 19:52:40128 // Called when the config becomes complete. Stops the timer.
[email protected]b4481b222012-03-16 17:13:11129 void OnCompleteConfig();
130
131 CallbackType callback_;
[email protected]6998a662011-09-03 00:17:29132
133 DnsConfig dns_config_;
[email protected]b4481b222012-03-16 17:13:11134
[email protected]bb0e34542012-08-31 19:52:40135 // True if any of the necessary watchers failed. In that case, the service
136 // will communicate changes via OnTimeout, but will only send empty DnsConfig.
137 bool watch_failed_;
[email protected]05aad32d2012-05-16 18:10:53138 // True after On*Read, before Invalidate*. Tells if the config is complete.
[email protected]6998a662011-09-03 00:17:29139 bool have_config_;
140 bool have_hosts_;
[email protected]b4481b222012-03-16 17:13:11141 // True if receiver needs to be updated when the config becomes complete.
142 bool need_update_;
[email protected]163d9b22012-06-12 19:50:36143 // True if the last config sent was empty (instead of |dns_config_|).
[email protected]539df6c2012-06-19 21:21:29144 // Set when |timer_| expires.
[email protected]163d9b22012-06-12 19:50:36145 bool last_sent_empty_;
[email protected]6998a662011-09-03 00:17:29146
[email protected]539df6c2012-06-19 21:21:29147 // Initialized and updated on Invalidate* call.
148 base::TimeTicks last_invalidate_config_time_;
149 base::TimeTicks last_invalidate_hosts_time_;
150 // Initialized and updated when |timer_| expires.
151 base::TimeTicks last_sent_empty_time_;
152
[email protected]b4481b222012-03-16 17:13:11153 // Started in Invalidate*, cleared in On*Read.
154 base::OneShotTimer<DnsConfigService> timer_;
155
[email protected]d84316a2011-08-18 04:41:21156 DISALLOW_COPY_AND_ASSIGN(DnsConfigService);
157};
158
159} // namespace net
160
161#endif // NET_DNS_DNS_CONFIG_SERVICE_H_