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