[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_ | ||||
7 | #pragma once | ||||
8 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 9 | #include <map> |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 10 | #include <string> |
11 | #include <vector> | ||||
12 | |||||
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 13 | #include "base/file_path.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
15 | #include "base/observer_list.h" | ||||
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 16 | #include "base/threading/non_thread_safe.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 17 | #include "base/time.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 18 | #include "net/base/ip_endpoint.h" // win requires size of IPEndPoint |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 19 | #include "net/base/net_export.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 20 | #include "net/dns/dns_hosts.h" |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 21 | #include "net/dns/serial_worker.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 22 | |
23 | namespace net { | ||||
24 | |||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 25 | // DnsConfig stores configuration of the system resolver. |
26 | struct NET_EXPORT_PRIVATE DnsConfig { | ||||
27 | DnsConfig(); | ||||
28 | virtual ~DnsConfig(); | ||||
29 | |||||
30 | bool Equals(const DnsConfig& d) const; | ||||
31 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 32 | bool EqualsIgnoreHosts(const DnsConfig& d) const; |
33 | |||||
34 | bool IsValid() const { | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 35 | return !nameservers.empty(); |
36 | } | ||||
37 | |||||
38 | // List of name server addresses. | ||||
39 | std::vector<IPEndPoint> nameservers; | ||||
40 | // Suffix search list; used on first lookup when number of dots in given name | ||||
41 | // is less than |ndots|. | ||||
[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame^] | 42 | // TODO(szym): Filter out duplicate entries from this list. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 43 | std::vector<std::string> search; |
44 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 45 | DnsHosts hosts; |
46 | |||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 47 | // Resolver options; see man resolv.conf. |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 48 | // TODO(szym): implement DNS Devolution for windows |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 49 | |
50 | // Minimum number of dots before global resolution precedes |search|. | ||||
51 | int ndots; | ||||
52 | // Time between retransmissions, see res_state.retrans. | ||||
53 | base::TimeDelta timeout; | ||||
[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame^] | 54 | // Maximum number of attempts, see res_state.retry. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 55 | int attempts; |
56 | // Round robin entries in |nameservers| for subsequent requests. | ||||
57 | bool rotate; | ||||
58 | // Enable EDNS0 extensions. | ||||
59 | bool edns0; | ||||
60 | }; | ||||
61 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 62 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 63 | // Service for watching when the system DNS settings have changed. |
64 | // Depending on the platform, watches files in /etc/ or win registry. | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 65 | class NET_EXPORT_PRIVATE DnsConfigService |
66 | : NON_EXPORTED_BASE(public base::NonThreadSafe) { | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 67 | public: |
68 | // Callback interface for the client. The observer is called on the same | ||||
69 | // thread as Watch(). Observer must outlive the service. | ||||
70 | class Observer { | ||||
71 | public: | ||||
72 | virtual ~Observer() {} | ||||
73 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 74 | // Called only when |dns_config| is different from the last call. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 75 | virtual void OnConfigChanged(const DnsConfig& dns_config) = 0; |
76 | }; | ||||
77 | |||||
78 | // Creates the platform-specific DnsConfigService. | ||||
79 | static DnsConfigService* CreateSystemService(); | ||||
80 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 81 | DnsConfigService(); |
82 | virtual ~DnsConfigService(); | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 83 | |
84 | // Immediately starts watching system configuration for changes and attempts | ||||
85 | // to read the configuration. For some platform implementations, the current | ||||
86 | // thread must have an IO loop (for base::files::FilePathWatcher). | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 87 | virtual void Watch() {} |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 88 | |
89 | // If a config is available, |observer| will immediately be called with | ||||
90 | // OnConfigChanged. | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 91 | void AddObserver(Observer* observer); |
92 | void RemoveObserver(Observer* observer); | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 93 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 94 | protected: |
95 | FRIEND_TEST_ALL_PREFIXES(DnsConfigServiceTest, NotifyOnChange); | ||||
96 | friend class DnsHostsReader; | ||||
97 | // To be called with new config. |config|.hosts is ignored. | ||||
98 | virtual void OnConfigRead(const DnsConfig& config); | ||||
99 | // To be called with new hosts. Rest of the config is assumed unchanged. | ||||
100 | virtual void OnHostsRead(const DnsHosts& hosts); | ||||
101 | |||||
102 | DnsConfig dns_config_; | ||||
103 | // True after first OnConfigRead and OnHostsRead; indicate complete config. | ||||
104 | bool have_config_; | ||||
105 | bool have_hosts_; | ||||
106 | |||||
107 | ObserverList<Observer> observers_; | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 108 | private: |
109 | DISALLOW_COPY_AND_ASSIGN(DnsConfigService); | ||||
110 | }; | ||||
111 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 112 | // A WatchingFileReader that reads a HOSTS file and notifies |
113 | // DnsConfigService::OnHostsRead(). | ||||
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 114 | // Client should call Cancel() when |service| is going away. |
115 | class NET_EXPORT_PRIVATE DnsHostsReader | ||||
116 | : NON_EXPORTED_BASE(public SerialWorker) { | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 117 | public: |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 118 | DnsHostsReader(const FilePath& path, DnsConfigService* service); |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 119 | |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 120 | virtual void DoWork() OVERRIDE; |
121 | virtual void OnWorkFinished() OVERRIDE; | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 122 | |
123 | private: | ||||
124 | virtual ~DnsHostsReader(); | ||||
125 | |||||
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 126 | FilePath path_; |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 127 | DnsConfigService* service_; |
128 | // Written in DoRead, read in OnReadFinished, no locking necessary. | ||||
129 | DnsHosts dns_hosts_; | ||||
130 | bool success_; | ||||
131 | |||||
132 | DISALLOW_COPY_AND_ASSIGN(DnsHostsReader); | ||||
133 | }; | ||||
134 | |||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 135 | } // namespace net |
136 | |||||
137 | #endif // NET_DNS_DNS_CONFIG_SERVICE_H_ |