| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/host_resolver_proc.h" | 5 #include "net/base/host_resolver_proc.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 9 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 10 #include <resolv.h> | 10 #include <resolv.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/time.h" | |
| 15 #include "net/base/address_list.h" | 14 #include "net/base/address_list.h" |
| 15 #include "net/base/dns_reload_timer.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/sys_addrinfo.h" | 17 #include "net/base/sys_addrinfo.h" |
| 18 | 18 |
| 19 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 20 #include "base/singleton.h" | |
| 21 #include "base/thread_local_storage.h" | |
| 22 #endif | |
| 23 | |
| 24 namespace net { | 19 namespace net { |
| 25 | 20 |
| 26 HostResolverProc* HostResolverProc::default_proc_ = NULL; | 21 HostResolverProc* HostResolverProc::default_proc_ = NULL; |
| 27 | 22 |
| 28 HostResolverProc::HostResolverProc(HostResolverProc* previous) { | 23 HostResolverProc::HostResolverProc(HostResolverProc* previous) { |
| 29 SetPreviousProc(previous); | 24 SetPreviousProc(previous); |
| 30 | 25 |
| 31 // Implicitly fall-back to the global default procedure. | 26 // Implicitly fall-back to the global default procedure. |
| 32 if (!previous) | 27 if (!previous) |
| 33 SetPreviousProc(default_proc_); | 28 SetPreviousProc(default_proc_); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 if (previous_proc_) { | 71 if (previous_proc_) { |
| 77 return previous_proc_->Resolve(host, address_family, host_resolver_flags, | 72 return previous_proc_->Resolve(host, address_family, host_resolver_flags, |
| 78 addrlist, os_error); | 73 addrlist, os_error); |
| 79 } | 74 } |
| 80 | 75 |
| 81 // Final fallback is the system resolver. | 76 // Final fallback is the system resolver. |
| 82 return SystemHostResolverProc(host, address_family, host_resolver_flags, | 77 return SystemHostResolverProc(host, address_family, host_resolver_flags, |
| 83 addrlist, os_error); | 78 addrlist, os_error); |
| 84 } | 79 } |
| 85 | 80 |
| 86 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | |
| 87 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting | |
| 88 // in DNS queries failing either because nameservers are unknown on startup | |
| 89 // or because nameserver info has changed as a result of e.g. connecting to | |
| 90 // a new network. Some distributions patch glibc to stat /etc/resolv.conf | |
| 91 // to try to automatically detect such changes but these patches are not | |
| 92 // universal and even patched systems such as Jaunty appear to need calls | |
| 93 // to res_ninit to reload the nameserver information in different threads. | |
| 94 // | |
| 95 // We adopt the Mozilla solution here which is to call res_ninit when | |
| 96 // lookups fail and to rate limit the reloading to once per second per | |
| 97 // thread. | |
| 98 // | |
| 99 // OpenBSD does not have thread-safe res_ninit/res_nclose so we can't do | |
| 100 // the same trick there. | |
| 101 | |
| 102 // Keep a timer per calling thread to rate limit the calling of res_ninit. | |
| 103 class DnsReloadTimer { | |
| 104 public: | |
| 105 // Check if the timer for the calling thread has expired. When no | |
| 106 // timer exists for the calling thread, create one. | |
| 107 bool Expired() { | |
| 108 const base::TimeDelta kRetryTime = base::TimeDelta::FromSeconds(1); | |
| 109 base::TimeTicks now = base::TimeTicks::Now(); | |
| 110 base::TimeTicks* timer_ptr = | |
| 111 static_cast<base::TimeTicks*>(tls_index_.Get()); | |
| 112 | |
| 113 if (!timer_ptr) { | |
| 114 timer_ptr = new base::TimeTicks(); | |
| 115 *timer_ptr = base::TimeTicks::Now(); | |
| 116 tls_index_.Set(timer_ptr); | |
| 117 // Return true to reload dns info on the first call for each thread. | |
| 118 return true; | |
| 119 } else if (now - *timer_ptr > kRetryTime) { | |
| 120 *timer_ptr = now; | |
| 121 return true; | |
| 122 } else { | |
| 123 return false; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 // Free the allocated timer. | |
| 128 static void SlotReturnFunction(void* data) { | |
| 129 base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data); | |
| 130 delete tls_data; | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 friend struct DefaultSingletonTraits<DnsReloadTimer>; | |
| 135 | |
| 136 DnsReloadTimer() { | |
| 137 // During testing the DnsReloadTimer Singleton may be created and destroyed | |
| 138 // multiple times. Initialize the ThreadLocalStorage slot only once. | |
| 139 if (!tls_index_.initialized()) | |
| 140 tls_index_.Initialize(SlotReturnFunction); | |
| 141 } | |
| 142 | |
| 143 ~DnsReloadTimer() { | |
| 144 } | |
| 145 | |
| 146 // We use thread local storage to identify which base::TimeTicks to | |
| 147 // interact with. | |
| 148 static ThreadLocalStorage::Slot tls_index_ ; | |
| 149 | |
| 150 DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer); | |
| 151 }; | |
| 152 | |
| 153 // A TLS slot to the TimeTicks for the current thread. | |
| 154 // static | |
| 155 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); | |
| 156 | |
| 157 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | |
| 158 | |
| 159 int SystemHostResolverProc(const std::string& host, | 81 int SystemHostResolverProc(const std::string& host, |
| 160 AddressFamily address_family, | 82 AddressFamily address_family, |
| 161 HostResolverFlags host_resolver_flags, | 83 HostResolverFlags host_resolver_flags, |
| 162 AddressList* addrlist, | 84 AddressList* addrlist, |
| 163 int* os_error) { | 85 int* os_error) { |
| 164 if (os_error) | 86 if (os_error) |
| 165 *os_error = 0; | 87 *os_error = 0; |
| 166 | 88 |
| 167 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. | 89 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
| 168 // On Windows it gives the default interface's address, whereas on Linux it | 90 // On Windows it gives the default interface's address, whereas on Linux it |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 hints.ai_flags &= ~AI_ADDRCONFIG; | 145 hints.ai_flags &= ~AI_ADDRCONFIG; |
| 224 | 146 |
| 225 if (host_resolver_flags & HOST_RESOLVER_CANONNAME) | 147 if (host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 226 hints.ai_flags |= AI_CANONNAME; | 148 hints.ai_flags |= AI_CANONNAME; |
| 227 | 149 |
| 228 // Restrict result set to only this socket type to avoid duplicates. | 150 // Restrict result set to only this socket type to avoid duplicates. |
| 229 hints.ai_socktype = SOCK_STREAM; | 151 hints.ai_socktype = SOCK_STREAM; |
| 230 | 152 |
| 231 int err = getaddrinfo(host.c_str(), NULL, &hints, &ai); | 153 int err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
| 232 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 154 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
| 233 net::DnsReloadTimer* dns_timer = Singleton<net::DnsReloadTimer>::get(); | |
| 234 // If we fail, re-initialise the resolver just in case there have been any | 155 // If we fail, re-initialise the resolver just in case there have been any |
| 235 // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. | 156 // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. |
| 236 if (err && dns_timer->Expired()) { | 157 if (err && DnsReloadTimerHasExpired()) { |
| 237 res_nclose(&_res); | 158 res_nclose(&_res); |
| 238 if (!res_ninit(&_res)) | 159 if (!res_ninit(&_res)) |
| 239 err = getaddrinfo(host.c_str(), NULL, &hints, &ai); | 160 err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
| 240 } | 161 } |
| 241 #endif | 162 #endif |
| 242 | 163 |
| 243 if (err) { | 164 if (err) { |
| 244 if (os_error) { | 165 if (os_error) { |
| 245 #if defined(OS_WIN) | 166 #if defined(OS_WIN) |
| 246 *os_error = WSAGetLastError(); | 167 *os_error = WSAGetLastError(); |
| 247 #else | 168 #else |
| 248 *os_error = err; | 169 *os_error = err; |
| 249 #endif | 170 #endif |
| 250 } | 171 } |
| 251 return ERR_NAME_NOT_RESOLVED; | 172 return ERR_NAME_NOT_RESOLVED; |
| 252 } | 173 } |
| 253 | 174 |
| 254 addrlist->Adopt(ai); | 175 addrlist->Adopt(ai); |
| 255 return OK; | 176 return OK; |
| 256 } | 177 } |
| 257 | 178 |
| 258 } // namespace net | 179 } // namespace net |
| OLD | NEW |