host_resolver_proc.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "net/dns/host_resolver_proc.h"
  5. #include <tuple>
  6. #include "build/build_config.h"
  7. #include "base/check.h"
  8. #include "base/threading/scoped_blocking_call.h"
  9. #include "net/base/address_family.h"
  10. #include "net/base/address_list.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/base/sys_addrinfo.h"
  13. #include "net/dns/address_info.h"
  14. #include "net/dns/dns_reloader.h"
  15. #include "net/dns/dns_util.h"
  16. #include "net/dns/host_resolver.h"
  17. #if BUILDFLAG(IS_OPENBSD)
  18. #define AI_ADDRCONFIG 0
  19. #endif
  20. namespace net {
  21. HostResolverProc* HostResolverProc::default_proc_ = nullptr;
  22. HostResolverProc::HostResolverProc(scoped_refptr<HostResolverProc> previous,
  23. bool allow_fallback_to_system_or_default)
  24. : allow_fallback_to_system_(allow_fallback_to_system_or_default) {
  25. SetPreviousProc(previous);
  26. // Implicitly fall-back to the global default procedure.
  27. if (!previous && allow_fallback_to_system_or_default)
  28. SetPreviousProc(default_proc_);
  29. }
  30. HostResolverProc::~HostResolverProc() = default;
  31. int HostResolverProc::Resolve(const std::string& host,
  32. AddressFamily address_family,
  33. HostResolverFlags host_resolver_flags,
  34. AddressList* addrlist,
  35. int* os_error,
  36. handles::NetworkHandle network) {
  37. if (network == handles::kInvalidNetworkHandle)
  38. return Resolve(host, address_family, host_resolver_flags, addrlist,
  39. os_error);
  40. NOTIMPLEMENTED();
  41. return ERR_NOT_IMPLEMENTED;
  42. }
  43. int HostResolverProc::ResolveUsingPrevious(
  44. const std::string& host,
  45. AddressFamily address_family,
  46. HostResolverFlags host_resolver_flags,
  47. AddressList* addrlist,
  48. int* os_error) {
  49. if (previous_proc_.get()) {
  50. return previous_proc_->Resolve(
  51. host, address_family, host_resolver_flags, addrlist, os_error);
  52. }
  53. // If `allow_fallback_to_system_` is false there is no final fallback. It must
  54. // be ensured that the Procs can handle any allowed requests. If this check
  55. // fails while using MockHostResolver or RuleBasedHostResolverProc, it means
  56. // none of the configured rules matched a host resolution request.
  57. CHECK(allow_fallback_to_system_);
  58. // Final fallback is the system resolver.
  59. return SystemHostResolverCall(host, address_family, host_resolver_flags,
  60. addrlist, os_error);
  61. }
  62. void HostResolverProc::SetPreviousProc(scoped_refptr<HostResolverProc> proc) {
  63. auto current_previous = std::move(previous_proc_);
  64. // Now that we've guaranteed |this| is the last proc in a chain, we can
  65. // detect potential cycles using GetLastProc().
  66. previous_proc_ = (GetLastProc(proc.get()) == this)
  67. ? std::move(current_previous)
  68. : std::move(proc);
  69. }
  70. void HostResolverProc::SetLastProc(scoped_refptr<HostResolverProc> proc) {
  71. GetLastProc(this)->SetPreviousProc(std::move(proc));
  72. }
  73. // static
  74. HostResolverProc* HostResolverProc::GetLastProc(HostResolverProc* proc) {
  75. if (proc == nullptr)
  76. return nullptr;
  77. HostResolverProc* last_proc = proc;
  78. while (last_proc->previous_proc_.get() != nullptr)
  79. last_proc = last_proc->previous_proc_.get();
  80. return last_proc;
  81. }
  82. // static
  83. HostResolverProc* HostResolverProc::SetDefault(HostResolverProc* proc) {
  84. HostResolverProc* old = default_proc_;
  85. default_proc_ = proc;
  86. return old;
  87. }
  88. // static
  89. HostResolverProc* HostResolverProc::GetDefault() {
  90. return default_proc_;
  91. }
  92. namespace {
  93. int AddressFamilyToAF(AddressFamily address_family) {
  94. switch (address_family) {
  95. case ADDRESS_FAMILY_IPV4:
  96. return AF_INET;
  97. case ADDRESS_FAMILY_IPV6:
  98. return AF_INET6;
  99. case ADDRESS_FAMILY_UNSPECIFIED:
  100. return AF_UNSPEC;
  101. }
  102. }
  103. } // namespace
  104. int SystemHostResolverCall(const std::string& host,
  105. AddressFamily address_family,
  106. HostResolverFlags host_resolver_flags,
  107. AddressList* addrlist,
  108. int* os_error_opt,
  109. handles::NetworkHandle network) {
  110. // |host| should be a valid domain name. HostResolverImpl::Resolve has checks
  111. // to fail early if this is not the case.
  112. DCHECK(IsValidDNSDomain(host));
  113. struct addrinfo hints = {0};
  114. hints.ai_family = AddressFamilyToAF(address_family);
  115. #if BUILDFLAG(IS_WIN)
  116. // DO NOT USE AI_ADDRCONFIG ON WINDOWS.
  117. //
  118. // The following comment in <winsock2.h> is the best documentation I found
  119. // on AI_ADDRCONFIG for Windows:
  120. // Flags used in "hints" argument to getaddrinfo()
  121. // - AI_ADDRCONFIG is supported starting with Vista
  122. // - default is AI_ADDRCONFIG ON whether the flag is set or not
  123. // because the performance penalty in not having ADDRCONFIG in
  124. // the multi-protocol stack environment is severe;
  125. // this defaulting may be disabled by specifying the AI_ALL flag,
  126. // in that case AI_ADDRCONFIG must be EXPLICITLY specified to
  127. // enable ADDRCONFIG behavior
  128. //
  129. // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the
  130. // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo
  131. // to fail with WSANO_DATA (11004) for "localhost", probably because of the
  132. // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page:
  133. // The IPv4 or IPv6 loopback address is not considered a valid global
  134. // address.
  135. // See http://crbug.com/5234.
  136. //
  137. // OpenBSD does not support it, either.
  138. hints.ai_flags = 0;
  139. #else
  140. hints.ai_flags = AI_ADDRCONFIG;
  141. #endif
  142. // On Linux AI_ADDRCONFIG doesn't consider loopback addresses, even if only
  143. // loopback addresses are configured. So don't use it when there are only
  144. // loopback addresses.
  145. if (host_resolver_flags & HOST_RESOLVER_LOOPBACK_ONLY)
  146. hints.ai_flags &= ~AI_ADDRCONFIG;
  147. if (host_resolver_flags & HOST_RESOLVER_CANONNAME)
  148. hints.ai_flags |= AI_CANONNAME;
  149. #if BUILDFLAG(IS_WIN)
  150. // See crbug.com/1176970. Flag not documented (other than the declaration
  151. // comment in ws2def.h) but confirmed by Microsoft to work for this purpose
  152. // and be safe.
  153. if (host_resolver_flags & HOST_RESOLVER_AVOID_MULTICAST)
  154. hints.ai_flags |= AI_DNS_ONLY;
  155. #endif // BUILDFLAG(IS_WIN)
  156. // Restrict result set to only this socket type to avoid duplicates.
  157. hints.ai_socktype = SOCK_STREAM;
  158. // This function can block for a long time. Use ScopedBlockingCall to increase
  159. // the current thread pool's capacity and thus avoid reducing CPU usage by the
  160. // current process during that time.
  161. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  162. base::BlockingType::WILL_BLOCK);
  163. #if BUILDFLAG(IS_POSIX) && \
  164. !(BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OPENBSD) || BUILDFLAG(IS_ANDROID))
  165. DnsReloaderMaybeReload();
  166. #endif
  167. auto [ai, err, os_error] = AddressInfo::Get(host, hints, nullptr, network);
  168. bool should_retry = false;
  169. // If the lookup was restricted (either by address family, or address
  170. // detection), and the results where all localhost of a single family,
  171. // maybe we should retry. There were several bugs related to these
  172. // issues, for example http://crbug.com/42058 and http://crbug.com/49024
  173. if ((hints.ai_family != AF_UNSPEC || hints.ai_flags & AI_ADDRCONFIG) && ai &&
  174. ai->IsAllLocalhostOfOneFamily()) {
  175. if (host_resolver_flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) {
  176. hints.ai_family = AF_UNSPEC;
  177. should_retry = true;
  178. }
  179. if (hints.ai_flags & AI_ADDRCONFIG) {
  180. hints.ai_flags &= ~AI_ADDRCONFIG;
  181. should_retry = true;
  182. }
  183. }
  184. if (should_retry) {
  185. std::tie(ai, err, os_error) =
  186. AddressInfo::Get(host, hints, nullptr, network);
  187. }
  188. if (os_error_opt)
  189. *os_error_opt = os_error;
  190. if (!ai)
  191. return err;
  192. *addrlist = ai->CreateAddressList();
  193. return OK;
  194. }
  195. SystemHostResolverProc::SystemHostResolverProc() : HostResolverProc(nullptr) {}
  196. int SystemHostResolverProc::Resolve(const std::string& hostname,
  197. AddressFamily address_family,
  198. HostResolverFlags host_resolver_flags,
  199. AddressList* addr_list,
  200. int* os_error,
  201. handles::NetworkHandle network) {
  202. return SystemHostResolverCall(hostname, address_family, host_resolver_flags,
  203. addr_list, os_error, network);
  204. }
  205. int SystemHostResolverProc::Resolve(const std::string& hostname,
  206. AddressFamily address_family,
  207. HostResolverFlags host_resolver_flags,
  208. AddressList* addr_list,
  209. int* os_error) {
  210. return Resolve(hostname, address_family, host_resolver_flags, addr_list,
  211. os_error, handles::kInvalidNetworkHandle);
  212. }
  213. SystemHostResolverProc::~SystemHostResolverProc() = default;
  214. ProcTaskParams::ProcTaskParams(scoped_refptr<HostResolverProc> resolver_proc,
  215. size_t in_max_retry_attempts)
  216. : resolver_proc(std::move(resolver_proc)),
  217. max_retry_attempts(in_max_retry_attempts),
  218. unresponsive_delay(kDnsDefaultUnresponsiveDelay) {
  219. // Maximum of 4 retry attempts for host resolution.
  220. static const size_t kDefaultMaxRetryAttempts = 4u;
  221. if (max_retry_attempts == HostResolver::ManagerOptions::kDefaultRetryAttempts)
  222. max_retry_attempts = kDefaultMaxRetryAttempts;
  223. }
  224. ProcTaskParams::ProcTaskParams(const ProcTaskParams& other) = default;
  225. ProcTaskParams::~ProcTaskParams() = default;
  226. } // namespace net