OLD | NEW |
| (Empty) |
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_BASE_HOST_RESOLVER_IMPL_H_ | |
6 #define NET_BASE_HOST_RESOLVER_IMPL_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/scoped_vector.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/threading/non_thread_safe.h" | |
16 #include "base/time.h" | |
17 #include "net/base/capturing_net_log.h" | |
18 #include "net/base/host_cache.h" | |
19 #include "net/base/host_resolver.h" | |
20 #include "net/base/host_resolver_proc.h" | |
21 #include "net/base/net_export.h" | |
22 #include "net/base/network_change_notifier.h" | |
23 #include "net/base/prioritized_dispatcher.h" | |
24 | |
25 namespace net { | |
26 | |
27 class BoundNetLog; | |
28 class DnsClient; | |
29 class NetLog; | |
30 | |
31 // For each hostname that is requested, HostResolver creates a | |
32 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask | |
33 // which runs the given HostResolverProc on a WorkerPool thread. If requests for | |
34 // that same host are made during the job's lifetime, they are attached to the | |
35 // existing job rather than creating a new one. This avoids doing parallel | |
36 // resolves for the same host. | |
37 // | |
38 // The way these classes fit together is illustrated by: | |
39 // | |
40 // | |
41 // +----------- HostResolverImpl -------------+ | |
42 // | | | | |
43 // Job Job Job | |
44 // (for host1, fam1) (for host2, fam2) (for hostx, famx) | |
45 // / | | / | | / | | | |
46 // Request ... Request Request ... Request Request ... Request | |
47 // (port1) (port2) (port3) (port4) (port5) (portX) | |
48 // | |
49 // When a HostResolverImpl::Job finishes, the callbacks of each waiting request | |
50 // are run on the origin thread. | |
51 // | |
52 // Thread safety: This class is not threadsafe, and must only be called | |
53 // from one thread! | |
54 // | |
55 // The HostResolverImpl enforces limits on the maximum number of concurrent | |
56 // threads using PrioritizedDispatcher::Limits. | |
57 // | |
58 // Jobs are ordered in the queue based on their priority and order of arrival. | |
59 class NET_EXPORT HostResolverImpl | |
60 : public HostResolver, | |
61 NON_EXPORTED_BASE(public base::NonThreadSafe), | |
62 public NetworkChangeNotifier::IPAddressObserver, | |
63 public NetworkChangeNotifier::DNSObserver { | |
64 public: | |
65 // Parameters for ProcTask which resolves hostnames using HostResolveProc. | |
66 // | |
67 // |resolver_proc| is used to perform the actual resolves; it must be | |
68 // thread-safe since it is run from multiple worker threads. If | |
69 // |resolver_proc| is NULL then the default host resolver procedure is | |
70 // used (which is SystemHostResolverProc except if overridden). | |
71 // | |
72 // For each attempt, we could start another attempt if host is not resolved | |
73 // within |unresponsive_delay| time. We keep attempting to resolve the host | |
74 // for |max_retry_attempts|. For every retry attempt, we grow the | |
75 // |unresponsive_delay| by the |retry_factor| amount (that is retry interval | |
76 // is multiplied by the retry factor each time). Once we have retried | |
77 // |max_retry_attempts|, we give up on additional attempts. | |
78 // | |
79 struct NET_EXPORT_PRIVATE ProcTaskParams { | |
80 // Sets up defaults. | |
81 ProcTaskParams(HostResolverProc* resolver_proc, size_t max_retry_attempts); | |
82 | |
83 ~ProcTaskParams(); | |
84 | |
85 // The procedure to use for resolving host names. This will be NULL, except | |
86 // in the case of unit-tests which inject custom host resolving behaviors. | |
87 scoped_refptr<HostResolverProc> resolver_proc; | |
88 | |
89 // Maximum number retry attempts to resolve the hostname. | |
90 // Pass HostResolver::kDefaultRetryAttempts to choose a default value. | |
91 size_t max_retry_attempts; | |
92 | |
93 // This is the limit after which we make another attempt to resolve the host | |
94 // if the worker thread has not responded yet. | |
95 base::TimeDelta unresponsive_delay; | |
96 | |
97 // Factor to grow |unresponsive_delay| when we re-re-try. | |
98 uint32 retry_factor; | |
99 }; | |
100 | |
101 // Creates a HostResolver that first uses the local cache |cache|, and then | |
102 // falls back to |proc_params.resolver_proc|. | |
103 // | |
104 // If |cache| is NULL, then no caching is used. Otherwise we take | |
105 // ownership of the |cache| pointer, and will free it during destruction. | |
106 // | |
107 // |job_limits| specifies the maximum number of jobs that the resolver will | |
108 // run at once. This upper-bounds the total number of outstanding | |
109 // DNS transactions (not counting retransmissions and retries). | |
110 // | |
111 // |net_log| must remain valid for the life of the HostResolverImpl. | |
112 HostResolverImpl(scoped_ptr<HostCache> cache, | |
113 const PrioritizedDispatcher::Limits& job_limits, | |
114 const ProcTaskParams& proc_params, | |
115 NetLog* net_log); | |
116 | |
117 // If any completion callbacks are pending when the resolver is destroyed, | |
118 // the host resolutions are cancelled, and the completion callbacks will not | |
119 // be called. | |
120 virtual ~HostResolverImpl(); | |
121 | |
122 // Configures maximum number of Jobs in the queue. Exposed for testing. | |
123 // Only allowed when the queue is empty. | |
124 void SetMaxQueuedJobs(size_t value); | |
125 | |
126 // Set the DnsClient to be used for resolution. In case of failure, the | |
127 // HostResolverProc from ProcTaskParams will be queried. If the DnsClient is | |
128 // not pre-configured with a valid DnsConfig, a new config is fetched from | |
129 // NetworkChangeNotifier. | |
130 void SetDnsClient(scoped_ptr<DnsClient> dns_client); | |
131 | |
132 // HostResolver methods: | |
133 virtual int Resolve(const RequestInfo& info, | |
134 AddressList* addresses, | |
135 const CompletionCallback& callback, | |
136 RequestHandle* out_req, | |
137 const BoundNetLog& source_net_log) OVERRIDE; | |
138 virtual int ResolveFromCache(const RequestInfo& info, | |
139 AddressList* addresses, | |
140 const BoundNetLog& source_net_log) OVERRIDE; | |
141 virtual void CancelRequest(RequestHandle req) OVERRIDE; | |
142 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; | |
143 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; | |
144 virtual void ProbeIPv6Support() OVERRIDE; | |
145 virtual void SetDnsClientEnabled(bool enabled) OVERRIDE; | |
146 virtual HostCache* GetHostCache() OVERRIDE; | |
147 virtual base::Value* GetDnsConfigAsValue() const OVERRIDE; | |
148 | |
149 private: | |
150 friend class HostResolverImplTest; | |
151 class Job; | |
152 class ProcTask; | |
153 class IPv6ProbeJob; | |
154 class LoopbackProbeJob; | |
155 class DnsTask; | |
156 class Request; | |
157 typedef HostCache::Key Key; | |
158 typedef std::map<Key, Job*> JobMap; | |
159 typedef ScopedVector<Request> RequestsList; | |
160 | |
161 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP | |
162 // literal, cache and HOSTS lookup (if enabled), returns OK if successful, | |
163 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is | |
164 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache and HOSTS. | |
165 int ResolveHelper(const Key& key, | |
166 const RequestInfo& info, | |
167 AddressList* addresses, | |
168 const BoundNetLog& request_net_log); | |
169 | |
170 // Tries to resolve |key| as an IP, returns true and sets |net_error| if | |
171 // succeeds, returns false otherwise. | |
172 bool ResolveAsIP(const Key& key, | |
173 const RequestInfo& info, | |
174 int* net_error, | |
175 AddressList* addresses); | |
176 | |
177 // If |key| is not found in cache returns false, otherwise returns | |
178 // true, sets |net_error| to the cached error code and fills |addresses| | |
179 // if it is a positive entry. | |
180 bool ServeFromCache(const Key& key, | |
181 const RequestInfo& info, | |
182 int* net_error, | |
183 AddressList* addresses); | |
184 | |
185 // If we have a DnsClient with a valid DnsConfig, and |key| is found in the | |
186 // HOSTS file, returns true and fills |addresses|. Otherwise returns false. | |
187 bool ServeFromHosts(const Key& key, | |
188 const RequestInfo& info, | |
189 AddressList* addresses); | |
190 | |
191 // Callback from IPv6 probe activity. | |
192 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); | |
193 | |
194 // Callback from HaveOnlyLoopbackAddresses probe. | |
195 void SetHaveOnlyLoopbackAddresses(bool result); | |
196 | |
197 // Returns the (hostname, address_family) key to use for |info|, choosing an | |
198 // "effective" address family by inheriting the resolver's default address | |
199 // family when the request leaves it unspecified. | |
200 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; | |
201 | |
202 // Records the result in cache if cache is present. | |
203 void CacheResult(const Key& key, | |
204 const HostCache::Entry& entry, | |
205 base::TimeDelta ttl); | |
206 | |
207 // Removes |job| from |jobs_|, only if it exists. | |
208 void RemoveJob(Job* job); | |
209 | |
210 // Aborts all in progress jobs with ERR_NETWORK_CHANGED and notifies their | |
211 // requests. Might start new jobs. | |
212 void AbortAllInProgressJobs(); | |
213 | |
214 // Attempts to serve each Job in |jobs_| from the HOSTS file if we have | |
215 // a DnsClient with a valid DnsConfig. | |
216 void TryServingAllJobsFromHosts(); | |
217 | |
218 // NetworkChangeNotifier::IPAddressObserver: | |
219 virtual void OnIPAddressChanged() OVERRIDE; | |
220 | |
221 // NetworkChangeNotifier::DNSObserver: | |
222 virtual void OnDNSChanged() OVERRIDE; | |
223 | |
224 // True if have a DnsClient with a valid DnsConfig. | |
225 bool HaveDnsConfig() const; | |
226 | |
227 // Called when a host name is successfully resolved and DnsTask was run on it | |
228 // and resulted in |net_error|. | |
229 void OnDnsTaskResolve(int net_error); | |
230 | |
231 // Allows the tests to catch slots leaking out of the dispatcher. | |
232 size_t num_running_jobs_for_tests() const { | |
233 return dispatcher_.num_running_jobs(); | |
234 } | |
235 | |
236 // Cache of host resolution results. | |
237 scoped_ptr<HostCache> cache_; | |
238 | |
239 // Map from HostCache::Key to a Job. | |
240 JobMap jobs_; | |
241 | |
242 // Starts Jobs according to their priority and the configured limits. | |
243 PrioritizedDispatcher dispatcher_; | |
244 | |
245 // Limit on the maximum number of jobs queued in |dispatcher_|. | |
246 size_t max_queued_jobs_; | |
247 | |
248 // Parameters for ProcTask. | |
249 ProcTaskParams proc_params_; | |
250 | |
251 NetLog* net_log_; | |
252 | |
253 // Address family to use when the request doesn't specify one. | |
254 AddressFamily default_address_family_; | |
255 | |
256 base::WeakPtrFactory<HostResolverImpl> weak_ptr_factory_; | |
257 | |
258 base::WeakPtrFactory<HostResolverImpl> probe_weak_ptr_factory_; | |
259 | |
260 // If present, used by DnsTask and ServeFromHosts to resolve requests. | |
261 scoped_ptr<DnsClient> dns_client_; | |
262 | |
263 // True if received valid config from |dns_config_service_|. Temporary, used | |
264 // to measure performance of DnsConfigService: http://crbug.com/125599 | |
265 bool received_dns_config_; | |
266 | |
267 // Number of consecutive failures of DnsTask, counted when fallback succeeds. | |
268 unsigned num_dns_failures_; | |
269 | |
270 // Indicate if probing is done after each network change event to set address | |
271 // family. When false, explicit setting of address family is used and results | |
272 // of the IPv6 probe job are ignored. | |
273 bool ipv6_probe_monitoring_; | |
274 | |
275 // True iff ProcTask has successfully resolved a hostname known to have IPv6 | |
276 // addresses using ADDRESS_FAMILY_UNSPECIFIED. Reset on IP address change. | |
277 bool resolved_known_ipv6_hostname_; | |
278 | |
279 // Any resolver flags that should be added to a request by default. | |
280 HostResolverFlags additional_resolver_flags_; | |
281 | |
282 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); | |
283 }; | |
284 | |
285 } // namespace net | |
286 | |
287 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ | |
OLD | NEW |