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> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 HostResolverProc* old = default_proc_; | 60 HostResolverProc* old = default_proc_; |
61 default_proc_ = proc; | 61 default_proc_ = proc; |
62 return old; | 62 return old; |
63 } | 63 } |
64 | 64 |
65 // static | 65 // static |
66 HostResolverProc* HostResolverProc::GetDefault() { | 66 HostResolverProc* HostResolverProc::GetDefault() { |
67 return default_proc_; | 67 return default_proc_; |
68 } | 68 } |
69 | 69 |
70 int HostResolverProc::ResolveUsingPrevious( | 70 int HostResolverProc::ResolveUsingPrevious(const std::string& host, |
71 const std::string& host, | 71 AddressFamily address_family, |
72 AddressFamily address_family, | 72 AddressList* addrlist) { |
73 HostResolverFlags host_resolver_flags, | |
74 AddressList* addrlist) { | |
75 if (previous_proc_) | 73 if (previous_proc_) |
76 return previous_proc_->Resolve(host, address_family, | 74 return previous_proc_->Resolve(host, address_family, addrlist); |
77 host_resolver_flags, addrlist); | |
78 | 75 |
79 // Final fallback is the system resolver. | 76 // Final fallback is the system resolver. |
80 return SystemHostResolverProc(host, address_family, | 77 return SystemHostResolverProc(host, address_family, addrlist); |
81 host_resolver_flags, addrlist); | |
82 } | 78 } |
83 | 79 |
84 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 80 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
85 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting | 81 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting |
86 // in DNS queries failing either because nameservers are unknown on startup | 82 // in DNS queries failing either because nameservers are unknown on startup |
87 // or because nameserver info has changed as a result of e.g. connecting to | 83 // or because nameserver info has changed as a result of e.g. connecting to |
88 // a new network. Some distributions patch glibc to stat /etc/resolv.conf | 84 // a new network. Some distributions patch glibc to stat /etc/resolv.conf |
89 // to try to automatically detect such changes but these patches are not | 85 // to try to automatically detect such changes but these patches are not |
90 // universal and even patched systems such as Jaunty appear to need calls | 86 // universal and even patched systems such as Jaunty appear to need calls |
91 // to res_ninit to reload the nameserver information in different threads. | 87 // to res_ninit to reload the nameserver information in different threads. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 }; | 145 }; |
150 | 146 |
151 // A TLS slot to the TimeTicks for the current thread. | 147 // A TLS slot to the TimeTicks for the current thread. |
152 // static | 148 // static |
153 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); | 149 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); |
154 | 150 |
155 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 151 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
156 | 152 |
157 int SystemHostResolverProc(const std::string& host, | 153 int SystemHostResolverProc(const std::string& host, |
158 AddressFamily address_family, | 154 AddressFamily address_family, |
159 HostResolverFlags host_resolver_flags, | |
160 AddressList* addrlist) { | 155 AddressList* addrlist) { |
161 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. | 156 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
162 // On Windows it gives the default interface's address, whereas on Linux it | 157 // On Windows it gives the default interface's address, whereas on Linux it |
163 // gives an error. We will make it fail on all platforms for consistency. | 158 // gives an error. We will make it fail on all platforms for consistency. |
164 if (host.empty()) | 159 if (host.empty()) |
165 return ERR_NAME_NOT_RESOLVED; | 160 return ERR_NAME_NOT_RESOLVED; |
166 | 161 |
167 struct addrinfo* ai = NULL; | 162 struct addrinfo* ai = NULL; |
168 struct addrinfo hints = {0}; | 163 struct addrinfo hints = {0}; |
169 | 164 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 // The IPv4 or IPv6 loopback address is not considered a valid global | 198 // The IPv4 or IPv6 loopback address is not considered a valid global |
204 // address. | 199 // address. |
205 // See http://crbug.com/5234. | 200 // See http://crbug.com/5234. |
206 // | 201 // |
207 // OpenBSD does not support it, either. | 202 // OpenBSD does not support it, either. |
208 hints.ai_flags = 0; | 203 hints.ai_flags = 0; |
209 #else | 204 #else |
210 hints.ai_flags = AI_ADDRCONFIG; | 205 hints.ai_flags = AI_ADDRCONFIG; |
211 #endif | 206 #endif |
212 | 207 |
213 if (host_resolver_flags & HOST_RESOLVER_FLAGS_CANONNAME) | |
214 hints.ai_flags |= AI_CANONNAME; | |
215 | |
216 // Restrict result set to only this socket type to avoid duplicates. | 208 // Restrict result set to only this socket type to avoid duplicates. |
217 hints.ai_socktype = SOCK_STREAM; | 209 hints.ai_socktype = SOCK_STREAM; |
218 | 210 |
219 int err = getaddrinfo(host.c_str(), NULL, &hints, &ai); | 211 int err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
220 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 212 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
221 net::DnsReloadTimer* dns_timer = Singleton<net::DnsReloadTimer>::get(); | 213 net::DnsReloadTimer* dns_timer = Singleton<net::DnsReloadTimer>::get(); |
222 // If we fail, re-initialise the resolver just in case there have been any | 214 // If we fail, re-initialise the resolver just in case there have been any |
223 // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. | 215 // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. |
224 if (err && dns_timer->Expired()) { | 216 if (err && dns_timer->Expired()) { |
225 res_nclose(&_res); | 217 res_nclose(&_res); |
226 if (!res_ninit(&_res)) | 218 if (!res_ninit(&_res)) |
227 err = getaddrinfo(host.c_str(), NULL, &hints, &ai); | 219 err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
228 } | 220 } |
229 #endif | 221 #endif |
230 | 222 |
231 if (err) | 223 if (err) |
232 return ERR_NAME_NOT_RESOLVED; | 224 return ERR_NAME_NOT_RESOLVED; |
233 | 225 |
234 addrlist->Adopt(ai); | 226 addrlist->Adopt(ai); |
235 return OK; | 227 return OK; |
236 } | 228 } |
237 | 229 |
238 } // namespace net | 230 } // namespace net |
OLD | NEW |