OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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.h" | 5 #include "net/base/host_resolver.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <ws2tcpip.h> | 8 #include <ws2tcpip.h> |
9 #include <wspiapi.h> // Needed for Win2k compat. | 9 #include <wspiapi.h> // Needed for Win2k compat. |
10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
11 #include <netdb.h> | 11 #include <netdb.h> |
12 #include <sys/socket.h> | 12 #include <sys/socket.h> |
13 #endif | 13 #endif |
14 #if defined(OS_LINUX) | 14 #if defined(OS_LINUX) |
15 #include <resolv.h> | 15 #include <resolv.h> |
16 #endif | 16 #endif |
17 | 17 |
18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/worker_pool.h" | 20 #include "base/worker_pool.h" |
21 #include "net/base/address_list.h" | 21 #include "net/base/address_list.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 | 23 |
| 24 #if defined(OS_LINUX) |
| 25 #include "base/singleton.h" |
| 26 #include "base/thread_local_storage.h" |
| 27 #include "base/time.h" |
| 28 #endif |
| 29 |
24 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
25 #include "net/base/winsock_init.h" | 31 #include "net/base/winsock_init.h" |
26 #endif | 32 #endif |
27 | 33 |
28 namespace net { | 34 namespace net { |
29 | 35 |
30 //----------------------------------------------------------------------------- | 36 //----------------------------------------------------------------------------- |
31 | 37 |
32 static HostMapper* host_mapper; | 38 static HostMapper* host_mapper; |
33 | 39 |
34 std::string HostMapper::MapUsingPrevious(const std::string& host) { | 40 std::string HostMapper::MapUsingPrevious(const std::string& host) { |
35 return previous_mapper_.get() ? previous_mapper_->Map(host) : host; | 41 return previous_mapper_.get() ? previous_mapper_->Map(host) : host; |
36 } | 42 } |
37 | 43 |
38 HostMapper* SetHostMapper(HostMapper* value) { | 44 HostMapper* SetHostMapper(HostMapper* value) { |
39 std::swap(host_mapper, value); | 45 std::swap(host_mapper, value); |
40 return value; | 46 return value; |
41 } | 47 } |
42 | 48 |
| 49 #if defined(OS_LINUX) |
| 50 // On Linux changes to /etc/resolv.conf can go unnoticed thus resulting in |
| 51 // DNS queries failing either because nameservers are unknown on startup |
| 52 // or because nameserver info has changed as a result of e.g. connecting to |
| 53 // a new network. Some distributions patch glibc to stat /etc/resolv.conf |
| 54 // to try to automatically detect such changes but these patches are not |
| 55 // universal and even patched systems such as Jaunty appear to need calls |
| 56 // to res_ninit to reload the nameserver information in different threads. |
| 57 // |
| 58 // We adopt the Mozilla solution here which is to call res_ninit when |
| 59 // lookups fail and to rate limit the reloading to once per second per |
| 60 // thread. |
| 61 |
| 62 // Keep a timer per calling thread to rate limit the calling of res_ninit. |
| 63 class DnsReloadTimer { |
| 64 public: |
| 65 DnsReloadTimer() { |
| 66 tls_index_.Initialize(SlotReturnFunction); |
| 67 } |
| 68 |
| 69 ~DnsReloadTimer() { } |
| 70 |
| 71 // Check if the timer for the calling thread has expired. When no |
| 72 // timer exists for the calling thread, create one. |
| 73 bool Expired() { |
| 74 const base::TimeDelta kRetryTime = base::TimeDelta::FromSeconds(1); |
| 75 base::TimeTicks now = base::TimeTicks::Now(); |
| 76 base::TimeTicks* timer_ptr = |
| 77 static_cast<base::TimeTicks*>(tls_index_.Get()); |
| 78 |
| 79 if (!timer_ptr) { |
| 80 timer_ptr = new base::TimeTicks(); |
| 81 *timer_ptr = base::TimeTicks::Now(); |
| 82 tls_index_.Set(timer_ptr); |
| 83 // Return true to reload dns info on the first call for each thread. |
| 84 return true; |
| 85 } else if (now - *timer_ptr > kRetryTime) { |
| 86 *timer_ptr = now; |
| 87 return true; |
| 88 } else { |
| 89 return false; |
| 90 } |
| 91 } |
| 92 |
| 93 // Free the allocated timer. |
| 94 static void SlotReturnFunction(void* data) { |
| 95 base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data); |
| 96 delete tls_data; |
| 97 } |
| 98 |
| 99 private: |
| 100 // We use thread local storage to identify which base::TimeTicks to |
| 101 // interact with. |
| 102 static ThreadLocalStorage::Slot tls_index_ ; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer); |
| 105 }; |
| 106 |
| 107 // A TLS slot to the TimeTicks for the current thread. |
| 108 // static |
| 109 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); |
| 110 |
| 111 #endif // defined(OS_LINUX) |
| 112 |
43 static int HostResolverProc( | 113 static int HostResolverProc( |
44 const std::string& host, const std::string& port, struct addrinfo** out) { | 114 const std::string& host, const std::string& port, struct addrinfo** out) { |
45 struct addrinfo hints = {0}; | 115 struct addrinfo hints = {0}; |
46 hints.ai_family = AF_UNSPEC; | 116 hints.ai_family = AF_UNSPEC; |
47 | 117 |
48 #if defined(OS_WIN) | 118 #if defined(OS_WIN) |
49 // DO NOT USE AI_ADDRCONFIG ON WINDOWS. | 119 // DO NOT USE AI_ADDRCONFIG ON WINDOWS. |
50 // | 120 // |
51 // The following comment in <winsock2.h> is the best documentation I found | 121 // The following comment in <winsock2.h> is the best documentation I found |
52 // on AI_ADDRCONFIG for Windows: | 122 // on AI_ADDRCONFIG for Windows: |
(...skipping 16 matching lines...) Expand all Loading... |
69 hints.ai_flags = 0; | 139 hints.ai_flags = 0; |
70 #else | 140 #else |
71 hints.ai_flags = AI_ADDRCONFIG; | 141 hints.ai_flags = AI_ADDRCONFIG; |
72 #endif | 142 #endif |
73 | 143 |
74 // Restrict result set to only this socket type to avoid duplicates. | 144 // Restrict result set to only this socket type to avoid duplicates. |
75 hints.ai_socktype = SOCK_STREAM; | 145 hints.ai_socktype = SOCK_STREAM; |
76 | 146 |
77 int err = getaddrinfo(host.c_str(), port.c_str(), &hints, out); | 147 int err = getaddrinfo(host.c_str(), port.c_str(), &hints, out); |
78 #if defined(OS_LINUX) | 148 #if defined(OS_LINUX) |
| 149 net::DnsReloadTimer* dns_timer = Singleton<net::DnsReloadTimer>::get(); |
79 // If we fail, re-initialise the resolver just in case there have been any | 150 // If we fail, re-initialise the resolver just in case there have been any |
80 // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. | 151 // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. |
81 if (err && !res_init()) | 152 if (err && dns_timer->Expired()) { |
82 err = getaddrinfo(host.c_str(), port.c_str(), &hints, out); | 153 res_nclose(&_res); |
| 154 if (!res_ninit(&_res)) |
| 155 err = getaddrinfo(host.c_str(), port.c_str(), &hints, out); |
| 156 } |
83 #endif | 157 #endif |
84 | 158 |
85 return err ? ERR_NAME_NOT_RESOLVED : OK; | 159 return err ? ERR_NAME_NOT_RESOLVED : OK; |
86 } | 160 } |
87 | 161 |
88 static int ResolveAddrInfo(HostMapper* mapper, const std::string& host, | 162 static int ResolveAddrInfo(HostMapper* mapper, const std::string& host, |
89 const std::string& port, struct addrinfo** out) { | 163 const std::string& port, struct addrinfo** out) { |
90 if (mapper) { | 164 if (mapper) { |
91 std::string mapped_host = mapper->Map(host); | 165 std::string mapped_host = mapper->Map(host); |
92 | 166 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 NewRunnableMethod(request_.get(), &Request::DoLookup), true)) { | 308 NewRunnableMethod(request_.get(), &Request::DoLookup), true)) { |
235 NOTREACHED(); | 309 NOTREACHED(); |
236 request_ = NULL; | 310 request_ = NULL; |
237 return ERR_FAILED; | 311 return ERR_FAILED; |
238 } | 312 } |
239 | 313 |
240 return ERR_IO_PENDING; | 314 return ERR_IO_PENDING; |
241 } | 315 } |
242 | 316 |
243 } // namespace net | 317 } // namespace net |
OLD | NEW |