summaryrefslogtreecommitdiffstats
path: root/net/base/host_resolver.cc
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-05 19:04:16 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-05 19:04:16 +0000
commitc2f5dd7f69af7a59321e5acd1aa39fea5b2b2bfd (patch)
tree8cce9bcde93d6dad59addaf00bd6337d5925f37e /net/base/host_resolver.cc
parentedc5a3c48ee8425570aea15d1833062b78aafae0 (diff)
downloadchromium_src-c2f5dd7f69af7a59321e5acd1aa39fea5b2b2bfd.zip
chromium_src-c2f5dd7f69af7a59321e5acd1aa39fea5b2b2bfd.tar.gz
chromium_src-c2f5dd7f69af7a59321e5acd1aa39fea5b2b2bfd.tar.bz2
Use res_ninit for thread safety and a timer for reloading resolv.conf on Linux.
We will only reload resolv.conf once per second per thread when DNS lookups fail. This should match the behaviour of mozilla. [retry of r17530, passes Valgrind now] BUG=12740 Review URL: http://codereview.chromium.org/118061 Patch from Craig Schlenter <craig.schlenter@gmail.com>. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17750 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/host_resolver.cc')
-rw-r--r--net/base/host_resolver.cc78
1 files changed, 76 insertions, 2 deletions
diff --git a/net/base/host_resolver.cc b/net/base/host_resolver.cc
index 23a1c00..ec693ee 100644
--- a/net/base/host_resolver.cc
+++ b/net/base/host_resolver.cc
@@ -21,6 +21,12 @@
#include "net/base/address_list.h"
#include "net/base/net_errors.h"
+#if defined(OS_LINUX)
+#include "base/singleton.h"
+#include "base/thread_local_storage.h"
+#include "base/time.h"
+#endif
+
#if defined(OS_WIN)
#include "net/base/winsock_init.h"
#endif
@@ -40,6 +46,70 @@ HostMapper* SetHostMapper(HostMapper* value) {
return value;
}
+#if defined(OS_LINUX)
+// On Linux changes to /etc/resolv.conf can go unnoticed thus resulting in
+// DNS queries failing either because nameservers are unknown on startup
+// or because nameserver info has changed as a result of e.g. connecting to
+// a new network. Some distributions patch glibc to stat /etc/resolv.conf
+// to try to automatically detect such changes but these patches are not
+// universal and even patched systems such as Jaunty appear to need calls
+// to res_ninit to reload the nameserver information in different threads.
+//
+// We adopt the Mozilla solution here which is to call res_ninit when
+// lookups fail and to rate limit the reloading to once per second per
+// thread.
+
+// Keep a timer per calling thread to rate limit the calling of res_ninit.
+class DnsReloadTimer {
+ public:
+ DnsReloadTimer() {
+ tls_index_.Initialize(SlotReturnFunction);
+ }
+
+ ~DnsReloadTimer() { }
+
+ // Check if the timer for the calling thread has expired. When no
+ // timer exists for the calling thread, create one.
+ bool Expired() {
+ const base::TimeDelta kRetryTime = base::TimeDelta::FromSeconds(1);
+ base::TimeTicks now = base::TimeTicks::Now();
+ base::TimeTicks* timer_ptr =
+ static_cast<base::TimeTicks*>(tls_index_.Get());
+
+ if (!timer_ptr) {
+ timer_ptr = new base::TimeTicks();
+ *timer_ptr = base::TimeTicks::Now();
+ tls_index_.Set(timer_ptr);
+ // Return true to reload dns info on the first call for each thread.
+ return true;
+ } else if (now - *timer_ptr > kRetryTime) {
+ *timer_ptr = now;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ // Free the allocated timer.
+ static void SlotReturnFunction(void* data) {
+ base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data);
+ delete tls_data;
+ }
+
+ private:
+ // We use thread local storage to identify which base::TimeTicks to
+ // interact with.
+ static ThreadLocalStorage::Slot tls_index_ ;
+
+ DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer);
+};
+
+// A TLS slot to the TimeTicks for the current thread.
+// static
+ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED);
+
+#endif // defined(OS_LINUX)
+
static int HostResolverProc(
const std::string& host, const std::string& port, struct addrinfo** out) {
struct addrinfo hints = {0};
@@ -76,10 +146,14 @@ static int HostResolverProc(
int err = getaddrinfo(host.c_str(), port.c_str(), &hints, out);
#if defined(OS_LINUX)
+ net::DnsReloadTimer* dns_timer = Singleton<net::DnsReloadTimer>::get();
// If we fail, re-initialise the resolver just in case there have been any
// changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info.
- if (err && !res_init())
- err = getaddrinfo(host.c_str(), port.c_str(), &hints, out);
+ if (err && dns_timer->Expired()) {
+ res_nclose(&_res);
+ if (!res_ninit(&_res))
+ err = getaddrinfo(host.c_str(), port.c_str(), &hints, out);
+ }
#endif
return err ? ERR_NAME_NOT_RESOLVED : OK;