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_HOST_RESOLVER_IMPL_H_ 6 #define NET_DNS_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/time.h" 17 #include "net/base/capturing_net_log.h" 18 #include "net/base/net_export.h" 19 #include "net/base/network_change_notifier.h" 20 #include "net/base/prioritized_dispatcher.h" 21 #include "net/dns/host_cache.h" 22 #include "net/dns/host_resolver.h" 23 #include "net/dns/host_resolver_proc.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 SetDnsClientEnabled(bool enabled) OVERRIDE; 145 virtual HostCache* GetHostCache() OVERRIDE; 146 virtual base::Value* GetDnsConfigAsValue() const OVERRIDE; 147 148 private: 149 friend class HostResolverImplTest; 150 class Job; 151 class ProcTask; 152 class LoopbackProbeJob; 153 class DnsTask; 154 class Request; 155 typedef HostCache::Key Key; 156 typedef std::map<Key, Job*> JobMap; 157 typedef ScopedVector<Request> RequestsList; 158 159 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP 160 // literal, cache and HOSTS lookup (if enabled), returns OK if successful, 161 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is 162 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache and HOSTS. 163 int ResolveHelper(const Key& key, 164 const RequestInfo& info, 165 AddressList* addresses, 166 const BoundNetLog& request_net_log); 167 168 // Tries to resolve |key| as an IP, returns true and sets |net_error| if 169 // succeeds, returns false otherwise. 170 bool ResolveAsIP(const Key& key, 171 const RequestInfo& info, 172 int* net_error, 173 AddressList* addresses); 174 175 // If |key| is not found in cache returns false, otherwise returns 176 // true, sets |net_error| to the cached error code and fills |addresses| 177 // if it is a positive entry. 178 bool ServeFromCache(const Key& key, 179 const RequestInfo& info, 180 int* net_error, 181 AddressList* addresses); 182 183 // If we have a DnsClient with a valid DnsConfig, and |key| is found in the 184 // HOSTS file, returns true and fills |addresses|. Otherwise returns false. 185 bool ServeFromHosts(const Key& key, 186 const RequestInfo& info, 187 AddressList* addresses); 188 189 // Callback from HaveOnlyLoopbackAddresses probe. 190 void SetHaveOnlyLoopbackAddresses(bool result); 191 192 // Returns the (hostname, address_family) key to use for |info|, choosing an 193 // "effective" address family by inheriting the resolver's default address 194 // family when the request leaves it unspecified. 195 Key GetEffectiveKeyForRequest(const RequestInfo& info, 196 const BoundNetLog& net_log) const; 197 198 // Records the result in cache if cache is present. 199 void CacheResult(const Key& key, 200 const HostCache::Entry& entry, 201 base::TimeDelta ttl); 202 203 // Removes |job| from |jobs_|, only if it exists. 204 void RemoveJob(Job* job); 205 206 // Aborts all in progress jobs with ERR_NETWORK_CHANGED and notifies their 207 // requests. Might start new jobs. 208 void AbortAllInProgressJobs(); 209 210 // Attempts to serve each Job in |jobs_| from the HOSTS file if we have 211 // a DnsClient with a valid DnsConfig. 212 void TryServingAllJobsFromHosts(); 213 214 // NetworkChangeNotifier::IPAddressObserver: 215 virtual void OnIPAddressChanged() OVERRIDE; 216 217 // NetworkChangeNotifier::DNSObserver: 218 virtual void OnDNSChanged() OVERRIDE; 219 220 // True if have a DnsClient with a valid DnsConfig. 221 bool HaveDnsConfig() const; 222 223 // Called when a host name is successfully resolved and DnsTask was run on it 224 // and resulted in |net_error|. 225 void OnDnsTaskResolve(int net_error); 226 227 // Allows the tests to catch slots leaking out of the dispatcher. 228 size_t num_running_jobs_for_tests() const { 229 return dispatcher_.num_running_jobs(); 230 } 231 232 // Cache of host resolution results. 233 scoped_ptr<HostCache> cache_; 234 235 // Map from HostCache::Key to a Job. 236 JobMap jobs_; 237 238 // Starts Jobs according to their priority and the configured limits. 239 PrioritizedDispatcher dispatcher_; 240 241 // Limit on the maximum number of jobs queued in |dispatcher_|. 242 size_t max_queued_jobs_; 243 244 // Parameters for ProcTask. 245 ProcTaskParams proc_params_; 246 247 NetLog* net_log_; 248 249 // Address family to use when the request doesn't specify one. 250 AddressFamily default_address_family_; 251 252 base::WeakPtrFactory<HostResolverImpl> weak_ptr_factory_; 253 254 base::WeakPtrFactory<HostResolverImpl> probe_weak_ptr_factory_; 255 256 // If present, used by DnsTask and ServeFromHosts to resolve requests. 257 scoped_ptr<DnsClient> dns_client_; 258 259 // True if received valid config from |dns_config_service_|. Temporary, used 260 // to measure performance of DnsConfigService: http://crbug.com/125599 261 bool received_dns_config_; 262 263 // Number of consecutive failures of DnsTask, counted when fallback succeeds. 264 unsigned num_dns_failures_; 265 266 // True if probing is done for each Request to set address family. When false, 267 // explicit setting in |default_address_family_| is used. 268 bool probe_ipv6_support_; 269 270 // True iff ProcTask has successfully resolved a hostname known to have IPv6 271 // addresses using ADDRESS_FAMILY_UNSPECIFIED. Reset on IP address change. 272 bool resolved_known_ipv6_hostname_; 273 274 // Any resolver flags that should be added to a request by default. 275 HostResolverFlags additional_resolver_flags_; 276 277 // Allow fallback to ProcTask if DnsTask fails. 278 bool fallback_to_proctask_; 279 280 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); 281 }; 282 283 } // namespace net 284 285 #endif // NET_DNS_HOST_RESOLVER_IMPL_H_ 286