[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] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 15 | #include "base/threading/non_thread_safe.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 16 | #include "base/time.h" |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 17 | #include "base/timer.h" |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 18 | // Needed on shared build with MSVS2010 to avoid multiple definitions of |
19 | // std::vector<IPEndPoint>. | ||||
20 | #include "net/base/address_list.h" | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 21 | #include "net/base/ip_endpoint.h" // win requires size of IPEndPoint |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 22 | #include "net/base/network_change_notifier.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 23 | #include "net/base/net_export.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 24 | #include "net/dns/dns_hosts.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 25 | |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 26 | namespace base { |
27 | class Value; | ||||
28 | } | ||||
29 | |||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 30 | namespace net { |
31 | |||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 32 | // DnsConfig stores configuration of the system resolver. |
33 | struct NET_EXPORT_PRIVATE DnsConfig { | ||||
34 | DnsConfig(); | ||||
35 | virtual ~DnsConfig(); | ||||
36 | |||||
37 | bool Equals(const DnsConfig& d) const; | ||||
38 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 39 | bool EqualsIgnoreHosts(const DnsConfig& d) const; |
40 | |||||
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 41 | void CopyIgnoreHosts(const DnsConfig& src); |
42 | |||||
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 43 | // Returns a Value representation of |this|. Caller takes ownership of the |
44 | // returned Value. For performance reasons, the Value only contains the | ||||
45 | // number of hosts rather than the full list. | ||||
46 | base::Value* ToValue() const; | ||||
47 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 48 | bool IsValid() const { |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 49 | return !nameservers.empty(); |
50 | } | ||||
51 | |||||
52 | // List of name server addresses. | ||||
53 | std::vector<IPEndPoint> nameservers; | ||||
54 | // Suffix search list; used on first lookup when number of dots in given name | ||||
55 | // is less than |ndots|. | ||||
56 | std::vector<std::string> search; | ||||
57 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 58 | DnsHosts hosts; |
59 | |||||
[email protected] | fea484e | 2012-02-07 23:21:57 | [diff] [blame] | 60 | // AppendToMultiLabelName: is suffix search performed for multi-label names? |
61 | // True, except on Windows where it can be configured. | ||||
62 | bool append_to_multi_label_name; | ||||
63 | |||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 64 | // Resolver options; see man resolv.conf. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 65 | |
66 | // Minimum number of dots before global resolution precedes |search|. | ||||
67 | int ndots; | ||||
68 | // Time between retransmissions, see res_state.retrans. | ||||
69 | base::TimeDelta timeout; | ||||
[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 70 | // Maximum number of attempts, see res_state.retry. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 71 | int attempts; |
72 | // Round robin entries in |nameservers| for subsequent requests. | ||||
73 | bool rotate; | ||||
74 | // Enable EDNS0 extensions. | ||||
75 | bool edns0; | ||||
76 | }; | ||||
77 | |||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 78 | |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 79 | // Service for reading system DNS settings, on demand or when signalled by |
80 | // NetworkChangeNotifier. | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 81 | class NET_EXPORT_PRIVATE DnsConfigService |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 82 | : NON_EXPORTED_BASE(public base::NonThreadSafe), |
83 | public NetworkChangeNotifier::DNSObserver { | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 84 | public: |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 85 | // Callback interface for the client, called on the same thread as Read() and |
86 | // Watch(). | ||||
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 87 | typedef base::Callback<void(const DnsConfig& config)> CallbackType; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 88 | |
89 | // Creates the platform-specific DnsConfigService. | ||||
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 90 | static scoped_ptr<DnsConfigService> CreateSystemService(); |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 91 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 92 | DnsConfigService(); |
93 | virtual ~DnsConfigService(); | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 94 | |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 95 | // Attempts to read the configuration. Will run |callback| when succeeded. |
96 | // Can be called at most once. | ||||
97 | void Read(const CallbackType& callback); | ||||
98 | |||||
99 | // Registers for notifications at NetworkChangeNotifier. Will attempt to read | ||||
100 | // config after watch is started by NetworkChangeNotifier. Will run |callback| | ||||
101 | // iff config changes from last call or should be withdrawn. | ||||
102 | // Can be called at most once. | ||||
103 | virtual void Watch(const CallbackType& callback); | ||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 104 | |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 105 | protected: |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 106 | friend class DnsHostsReader; |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 107 | |
108 | // Called when the current config (except hosts) has changed. | ||||
109 | void InvalidateConfig(); | ||||
110 | // Called when the current hosts have changed. | ||||
111 | void InvalidateHosts(); | ||||
112 | |||||
113 | // Called with new config. |config|.hosts is ignored. | ||||
114 | void OnConfigRead(const DnsConfig& config); | ||||
115 | // Called with new hosts. Rest of the config is assumed unchanged. | ||||
116 | void OnHostsRead(const DnsHosts& hosts); | ||||
117 | |||||
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 118 | // NetworkChangeNotifier::DNSObserver: |
119 | // Must be defined by implementations. | ||||
120 | virtual void OnDNSChanged(unsigned detail) OVERRIDE = 0; | ||||
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 121 | |
122 | private: | ||||
123 | void StartTimer(); | ||||
124 | // Called when the timer expires. | ||||
125 | void OnTimeout(); | ||||
126 | // Called when the config becomes complete. | ||||
127 | void OnCompleteConfig(); | ||||
128 | |||||
129 | CallbackType callback_; | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 130 | |
131 | DnsConfig dns_config_; | ||||
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 132 | |
[email protected] | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 133 | // True after On*Read, before Invalidate*. Tells if the config is complete. |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 134 | bool have_config_; |
135 | bool have_hosts_; | ||||
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 136 | // True if receiver needs to be updated when the config becomes complete. |
137 | bool need_update_; | ||||
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 138 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 139 | // Started in Invalidate*, cleared in On*Read. |
140 | base::OneShotTimer<DnsConfigService> timer_; | ||||
141 | |||||
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 142 | DISALLOW_COPY_AND_ASSIGN(DnsConfigService); |
143 | }; | ||||
144 | |||||
145 | } // namespace net | ||||
146 | |||||
147 | #endif // NET_DNS_DNS_CONFIG_SERVICE_H_ |