[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 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_H_ |
| 6 | #define NET_DNS_DNS_CONFIG_SERVICE_H_ |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 7 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 8 | #include <map> |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 14 | #include "base/threading/non_thread_safe.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 15 | #include "base/time.h" |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 16 | #include "base/timer.h" |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 17 | // Needed on shared build with MSVS2010 to avoid multiple definitions of |
| 18 | // std::vector<IPEndPoint>. |
| 19 | #include "net/base/address_list.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 20 | #include "net/base/ip_endpoint.h" // win requires size of IPEndPoint |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 21 | #include "net/base/net_export.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 22 | #include "net/dns/dns_hosts.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 23 | |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 24 | namespace base { |
| 25 | class Value; |
| 26 | } |
| 27 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 28 | namespace net { |
| 29 | |
[email protected] | 05b4a32 | 2012-08-15 01:26:45 | [diff] [blame] | 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; |
| 33 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 34 | // DnsConfig stores configuration of the system resolver. |
| 35 | struct NET_EXPORT_PRIVATE DnsConfig { |
| 36 | DnsConfig(); |
| 37 | virtual ~DnsConfig(); |
| 38 | |
| 39 | bool Equals(const DnsConfig& d) const; |
| 40 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 41 | bool EqualsIgnoreHosts(const DnsConfig& d) const; |
| 42 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 43 | void CopyIgnoreHosts(const DnsConfig& src); |
| 44 | |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 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; |
| 49 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 50 | bool IsValid() const { |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 51 | 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] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 60 | DnsHosts hosts; |
| 61 | |
[email protected] | fea484e | 2012-02-07 23:21:57 | [diff] [blame] | 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; |
| 65 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 66 | // Resolver options; see man resolv.conf. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 67 | |
| 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] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 72 | // Maximum number of attempts, see res_state.retry. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 73 | 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] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 80 | |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 81 | // Service for reading system DNS settings, on demand or when signalled by |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 82 | // internal watchers and NetworkChangeNotifier. |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 83 | class NET_EXPORT_PRIVATE DnsConfigService |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 84 | : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 85 | public: |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 86 | // Callback interface for the client, called on the same thread as |
| 87 | // ReadConfig() and WatchConfig(). |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 88 | typedef base::Callback<void(const DnsConfig& config)> CallbackType; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 89 | |
| 90 | // Creates the platform-specific DnsConfigService. |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 91 | static scoped_ptr<DnsConfigService> CreateSystemService(); |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 92 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 93 | DnsConfigService(); |
| 94 | virtual ~DnsConfigService(); |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 95 | |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 96 | // Attempts to read the configuration. Will run |callback| when succeeded. |
| 97 | // Can be called at most once. |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 98 | void ReadConfig(const CallbackType& callback); |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 99 | |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 100 | // 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] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 105 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 106 | protected: |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 107 | // 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] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 112 | // 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] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 122 | void set_watch_failed(bool value) { watch_failed_ = value; } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 123 | |
| 124 | private: |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 125 | // The timer counts from the last Invalidate* until complete config is read. |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 126 | void StartTimer(); |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 127 | void OnTimeout(); |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 128 | // Called when the config becomes complete. Stops the timer. |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 129 | void OnCompleteConfig(); |
| 130 | |
| 131 | CallbackType callback_; |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 132 | |
| 133 | DnsConfig dns_config_; |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 134 | |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 135 | // 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] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 138 | // True after On*Read, before Invalidate*. Tells if the config is complete. |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 139 | bool have_config_; |
| 140 | bool have_hosts_; |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 141 | // True if receiver needs to be updated when the config becomes complete. |
| 142 | bool need_update_; |
[email protected] | 163d9b2 | 2012-06-12 19:50:36 | [diff] [blame] | 143 | // True if the last config sent was empty (instead of |dns_config_|). |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 144 | // Set when |timer_| expires. |
[email protected] | 163d9b2 | 2012-06-12 19:50:36 | [diff] [blame] | 145 | bool last_sent_empty_; |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 146 | |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 147 | // 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] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 153 | // Started in Invalidate*, cleared in On*Read. |
| 154 | base::OneShotTimer<DnsConfigService> timer_; |
| 155 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(DnsConfigService); |
| 157 | }; |
| 158 | |
| 159 | } // namespace net |
| 160 | |
| 161 | #endif // NET_DNS_DNS_CONFIG_SERVICE_H_ |