[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/host_resolver_proc.h" |
| 6 | |
| 7 | #include "build/build_config.h" |
| 8 | |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 9 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 10 | #include <resolv.h> |
| 11 | #endif |
| 12 | |
| 13 | #include "base/logging.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 14 | #include "net/base/address_list.h" |
[email protected] | 37e658b | 2010-07-28 17:23:04 | [diff] [blame] | 15 | #include "net/base/dns_reload_timer.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 16 | #include "net/base/net_errors.h" |
[email protected] | a540c2d | 2009-12-12 00:47:37 | [diff] [blame] | 17 | #include "net/base/sys_addrinfo.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 18 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 19 | namespace net { |
| 20 | |
| 21 | HostResolverProc* HostResolverProc::default_proc_ = NULL; |
| 22 | |
| 23 | HostResolverProc::HostResolverProc(HostResolverProc* previous) { |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 24 | SetPreviousProc(previous); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 25 | |
| 26 | // Implicitly fall-back to the global default procedure. |
| 27 | if (!previous) |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 28 | SetPreviousProc(default_proc_); |
| 29 | } |
| 30 | |
| 31 | void HostResolverProc::SetPreviousProc(HostResolverProc* proc) { |
| 32 | HostResolverProc* current_previous = previous_proc_; |
| 33 | previous_proc_ = NULL; |
| 34 | // Now that we've guaranteed |this| is the last proc in a chain, we can |
| 35 | // detect potential cycles using GetLastProc(). |
| 36 | previous_proc_ = (GetLastProc(proc) == this) ? current_previous : proc; |
| 37 | } |
| 38 | |
| 39 | void HostResolverProc::SetLastProc(HostResolverProc* proc) { |
| 40 | GetLastProc(this)->SetPreviousProc(proc); |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | HostResolverProc* HostResolverProc::GetLastProc(HostResolverProc* proc) { |
| 45 | if (proc == NULL) |
| 46 | return NULL; |
| 47 | HostResolverProc* last_proc = proc; |
| 48 | while (last_proc->previous_proc_ != NULL) |
| 49 | last_proc = last_proc->previous_proc_; |
| 50 | return last_proc; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // static |
| 54 | HostResolverProc* HostResolverProc::SetDefault(HostResolverProc* proc) { |
| 55 | HostResolverProc* old = default_proc_; |
| 56 | default_proc_ = proc; |
| 57 | return old; |
| 58 | } |
| 59 | |
| 60 | // static |
| 61 | HostResolverProc* HostResolverProc::GetDefault() { |
| 62 | return default_proc_; |
| 63 | } |
| 64 | |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 65 | int HostResolverProc::ResolveUsingPrevious( |
| 66 | const std::string& host, |
| 67 | AddressFamily address_family, |
| 68 | HostResolverFlags host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 69 | AddressList* addrlist, |
| 70 | int* os_error) { |
| 71 | if (previous_proc_) { |
| 72 | return previous_proc_->Resolve(host, address_family, host_resolver_flags, |
| 73 | addrlist, os_error); |
| 74 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 75 | |
| 76 | // Final fallback is the system resolver. |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 77 | return SystemHostResolverProc(host, address_family, host_resolver_flags, |
| 78 | addrlist, os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 81 | int SystemHostResolverProc(const std::string& host, |
| 82 | AddressFamily address_family, |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 83 | HostResolverFlags host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 84 | AddressList* addrlist, |
| 85 | int* os_error) { |
[email protected] | 3eac24a2 | 2010-08-07 01:13:28 | [diff] [blame^] | 86 | static const size_t kMaxHostLength = 4096; |
| 87 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 88 | if (os_error) |
| 89 | *os_error = 0; |
| 90 | |
[email protected] | 4154786 | 2009-08-17 20:47:02 | [diff] [blame] | 91 | // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
| 92 | // On Windows it gives the default interface's address, whereas on Linux it |
| 93 | // gives an error. We will make it fail on all platforms for consistency. |
| 94 | if (host.empty()) |
| 95 | return ERR_NAME_NOT_RESOLVED; |
| 96 | |
[email protected] | 3eac24a2 | 2010-08-07 01:13:28 | [diff] [blame^] | 97 | // Limit the size of hostnames that will be resolved to combat issues in some |
| 98 | // platform's resolvers. |
| 99 | if (host.size() > kMaxHostLength) |
| 100 | return ERR_NAME_NOT_RESOLVED; |
| 101 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 102 | struct addrinfo* ai = NULL; |
| 103 | struct addrinfo hints = {0}; |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 104 | |
| 105 | switch (address_family) { |
[email protected] | dd03033 | 2009-10-23 04:35:31 | [diff] [blame] | 106 | case ADDRESS_FAMILY_IPV4: |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 107 | hints.ai_family = AF_INET; |
| 108 | break; |
[email protected] | dd03033 | 2009-10-23 04:35:31 | [diff] [blame] | 109 | case ADDRESS_FAMILY_IPV6: |
| 110 | hints.ai_family = AF_INET6; |
| 111 | break; |
| 112 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 113 | hints.ai_family = AF_UNSPEC; |
| 114 | break; |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 115 | default: |
[email protected] | dd03033 | 2009-10-23 04:35:31 | [diff] [blame] | 116 | NOTREACHED(); |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 117 | hints.ai_family = AF_UNSPEC; |
| 118 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 119 | |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 120 | #if defined(OS_WIN) || defined(OS_OPENBSD) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 121 | // DO NOT USE AI_ADDRCONFIG ON WINDOWS. |
| 122 | // |
| 123 | // The following comment in <winsock2.h> is the best documentation I found |
| 124 | // on AI_ADDRCONFIG for Windows: |
| 125 | // Flags used in "hints" argument to getaddrinfo() |
| 126 | // - AI_ADDRCONFIG is supported starting with Vista |
| 127 | // - default is AI_ADDRCONFIG ON whether the flag is set or not |
| 128 | // because the performance penalty in not having ADDRCONFIG in |
| 129 | // the multi-protocol stack environment is severe; |
| 130 | // this defaulting may be disabled by specifying the AI_ALL flag, |
| 131 | // in that case AI_ADDRCONFIG must be EXPLICITLY specified to |
| 132 | // enable ADDRCONFIG behavior |
| 133 | // |
| 134 | // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the |
| 135 | // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo |
| 136 | // to fail with WSANO_DATA (11004) for "localhost", probably because of the |
| 137 | // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page: |
| 138 | // The IPv4 or IPv6 loopback address is not considered a valid global |
| 139 | // address. |
| 140 | // See http://crbug.com/5234. |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 141 | // |
| 142 | // OpenBSD does not support it, either. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 143 | hints.ai_flags = 0; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 144 | #else |
| 145 | hints.ai_flags = AI_ADDRCONFIG; |
| 146 | #endif |
| 147 | |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 148 | // On Linux AI_ADDRCONFIG doesn't consider loopback addreses, even if only |
| 149 | // loopback addresses are configured. So don't use it when there are only |
| 150 | // loopback addresses. |
| 151 | if (host_resolver_flags & HOST_RESOLVER_LOOPBACK_ONLY) |
| 152 | hints.ai_flags &= ~AI_ADDRCONFIG; |
| 153 | |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 154 | if (host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 155 | hints.ai_flags |= AI_CANONNAME; |
| 156 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 157 | // Restrict result set to only this socket type to avoid duplicates. |
| 158 | hints.ai_socktype = SOCK_STREAM; |
| 159 | |
[email protected] | f820596a | 2009-11-25 00:15:38 | [diff] [blame] | 160 | int err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 161 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 162 | // If we fail, re-initialise the resolver just in case there have been any |
| 163 | // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. |
[email protected] | 37e658b | 2010-07-28 17:23:04 | [diff] [blame] | 164 | if (err && DnsReloadTimerHasExpired()) { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 165 | res_nclose(&_res); |
| 166 | if (!res_ninit(&_res)) |
| 167 | err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
| 168 | } |
| 169 | #endif |
| 170 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 171 | if (err) { |
| 172 | if (os_error) { |
| 173 | #if defined(OS_WIN) |
| 174 | *os_error = WSAGetLastError(); |
| 175 | #else |
| 176 | *os_error = err; |
| 177 | #endif |
| 178 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 179 | return ERR_NAME_NOT_RESOLVED; |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 180 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 181 | |
| 182 | addrlist->Adopt(ai); |
| 183 | return OK; |
| 184 | } |
| 185 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 186 | } // namespace net |