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_H_ 6 #define NET_DNS_DNS_CONFIG_SERVICE_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 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/time.h" 16 #include "base/timer/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" 23 24 namespace base { 25 class Value; 26 } 27 28 namespace net { 29 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 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 41 bool EqualsIgnoreHosts(const DnsConfig& d) const; 42 43 void CopyIgnoreHosts(const DnsConfig& src); 44 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 50 bool IsValid() const { 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 60 DnsHosts hosts; 61 62 // True if there are options set in the system configuration that are not yet 63 // supported by DnsClient. 64 bool unhandled_options; 65 66 // AppendToMultiLabelName: is suffix search performed for multi-label names? 67 // True, except on Windows where it can be configured. 68 bool append_to_multi_label_name; 69 70 // Indicates that source port randomization is required. This uses additional 71 // resources on some platforms. 72 bool randomize_ports; 73 74 // Resolver options; see man resolv.conf. 75 76 // Minimum number of dots before global resolution precedes |search|. 77 int ndots; 78 // Time between retransmissions, see res_state.retrans. 79 base::TimeDelta timeout; 80 // Maximum number of attempts, see res_state.retry. 81 int attempts; 82 // Round robin entries in |nameservers| for subsequent requests. 83 bool rotate; 84 // Enable EDNS0 extensions. 85 bool edns0; 86 87 // Indicates system configuration uses local IPv6 connectivity, e.g., 88 // DirectAccess. This is exposed for HostResolver to skip IPv6 probes, 89 // as it may cause them to return incorrect results. 90 bool use_local_ipv6; 91 }; 92 93 94 // Service for reading system DNS settings, on demand or when signalled by 95 // internal watchers and NetworkChangeNotifier. 96 class NET_EXPORT_PRIVATE DnsConfigService 97 : NON_EXPORTED_BASE(public base::NonThreadSafe) { 98 public: 99 // Callback interface for the client, called on the same thread as 100 // ReadConfig() and WatchConfig(). 101 typedef base::Callback<void(const DnsConfig& config)> CallbackType; 102 103 // Creates the platform-specific DnsConfigService. 104 static scoped_ptr<DnsConfigService> CreateSystemService(); 105 106 DnsConfigService(); 107 virtual ~DnsConfigService(); 108 109 // Attempts to read the configuration. Will run |callback| when succeeded. 110 // Can be called at most once. 111 void ReadConfig(const CallbackType& callback); 112 113 // Registers systems watchers. Will attempt to read config after watch starts, 114 // but only if watchers started successfully. Will run |callback| iff config 115 // changes from last call or has to be withdrawn. Can be called at most once. 116 // Might require MessageLoopForIO. 117 void WatchConfig(const CallbackType& callback); 118 119 protected: 120 enum WatchStatus { 121 DNS_CONFIG_WATCH_STARTED = 0, 122 DNS_CONFIG_WATCH_FAILED_TO_START_CONFIG, 123 DNS_CONFIG_WATCH_FAILED_TO_START_HOSTS, 124 DNS_CONFIG_WATCH_FAILED_CONFIG, 125 DNS_CONFIG_WATCH_FAILED_HOSTS, 126 DNS_CONFIG_WATCH_MAX, 127 }; 128 129 // Immediately attempts to read the current configuration. 130 virtual void ReadNow() = 0; 131 // Registers system watchers. Returns true iff succeeds. 132 virtual bool StartWatching() = 0; 133 134 // Called when the current config (except hosts) has changed. 135 void InvalidateConfig(); 136 // Called when the current hosts have changed. 137 void InvalidateHosts(); 138 139 // Called with new config. |config|.hosts is ignored. 140 void OnConfigRead(const DnsConfig& config); 141 // Called with new hosts. Rest of the config is assumed unchanged. 142 void OnHostsRead(const DnsHosts& hosts); 143 144 void set_watch_failed(bool value) { watch_failed_ = value; } 145 146 private: 147 // The timer counts from the last Invalidate* until complete config is read. 148 void StartTimer(); 149 void OnTimeout(); 150 // Called when the config becomes complete. Stops the timer. 151 void OnCompleteConfig(); 152 153 CallbackType callback_; 154 155 DnsConfig dns_config_; 156 157 // True if any of the necessary watchers failed. In that case, the service 158 // will communicate changes via OnTimeout, but will only send empty DnsConfig. 159 bool watch_failed_; 160 // True after On*Read, before Invalidate*. Tells if the config is complete. 161 bool have_config_; 162 bool have_hosts_; 163 // True if receiver needs to be updated when the config becomes complete. 164 bool need_update_; 165 // True if the last config sent was empty (instead of |dns_config_|). 166 // Set when |timer_| expires. 167 bool last_sent_empty_; 168 169 // Initialized and updated on Invalidate* call. 170 base::TimeTicks last_invalidate_config_time_; 171 base::TimeTicks last_invalidate_hosts_time_; 172 // Initialized and updated when |timer_| expires. 173 base::TimeTicks last_sent_empty_time_; 174 175 // Started in Invalidate*, cleared in On*Read. 176 base::OneShotTimer<DnsConfigService> timer_; 177 178 DISALLOW_COPY_AND_ASSIGN(DnsConfigService); 179 }; 180 181 } // namespace net 182 183 #endif // NET_DNS_DNS_CONFIG_SERVICE_H_ 184