| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_impl.h" | 5 #include "net/base/host_resolver_impl.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <Winsock2.h> | 8 #include <Winsock2.h> |
| 9 #elif defined(OS_POSIX) | 9 #elif defined(OS_POSIX) |
| 10 #include <netdb.h> | 10 #include <netdb.h> |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include "net/base/net_util.h" | 37 #include "net/base/net_util.h" |
| 38 | 38 |
| 39 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
| 40 #include "net/base/winsock_init.h" | 40 #include "net/base/winsock_init.h" |
| 41 #endif | 41 #endif |
| 42 | 42 |
| 43 namespace net { | 43 namespace net { |
| 44 | 44 |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 // Helper to create an AddressList that has a particular port. It has an | |
| 48 // optimization to avoid allocating a new address linked list when the | |
| 49 // port is already what we want. | |
| 50 AddressList CreateAddressListUsingPort(const AddressList& src, int port) { | |
| 51 if (src.GetPort() == port) | |
| 52 return src; | |
| 53 | |
| 54 AddressList out = src; | |
| 55 out.SetPort(port); | |
| 56 return out; | |
| 57 } | |
| 58 | |
| 59 // Helper to mutate the linked list contained by AddressList to the given | 47 // Helper to mutate the linked list contained by AddressList to the given |
| 60 // port. Note that in general this is dangerous since the AddressList's | 48 // port. Note that in general this is dangerous since the AddressList's |
| 61 // data might be shared (and you should use AddressList::SetPort). | 49 // data might be shared (and you should use AddressList::SetPort). |
| 62 // | 50 // |
| 63 // However since we allocated the AddressList ourselves we can safely | 51 // However since we allocated the AddressList ourselves we can safely |
| 64 // do this optimization and avoid reallocating the list. | 52 // do this optimization and avoid reallocating the list. |
| 65 void MutableSetPort(int port, AddressList* addrlist) { | 53 void MutableSetPort(int port, AddressList* addrlist) { |
| 66 struct addrinfo* mutable_head = | 54 struct addrinfo* mutable_head = |
| 67 const_cast<struct addrinfo*>(addrlist->head()); | 55 const_cast<struct addrinfo*>(addrlist->head()); |
| 68 SetPortForAllAddrinfos(mutable_head, port); | 56 SetPortForAllAddrinfos(mutable_head, port); |
| 69 } | 57 } |
| 70 | 58 |
| 71 // We use a separate histogram name for each platform to facilitate the | 59 // We use a separate histogram name for each platform to facilitate the |
| 72 // display of error codes by their symbolic name (since each platform has | 60 // display of error codes by their symbolic name (since each platform has |
| 73 // different mappings). | 61 // different mappings). |
| 74 const char kOSErrorsForGetAddrinfoHistogramName[] = | 62 const char kOSErrorsForGetAddrinfoHistogramName[] = |
| 75 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
| 76 "Net.OSErrorsForGetAddrinfo_Win"; | 64 "Net.OSErrorsForGetAddrinfo_Win"; |
| 77 #elif defined(OS_MACOSX) | 65 #elif defined(OS_MACOSX) |
| 78 "Net.OSErrorsForGetAddrinfo_Mac"; | 66 "Net.OSErrorsForGetAddrinfo_Mac"; |
| 79 #elif defined(OS_LINUX) | 67 #elif defined(OS_LINUX) |
| 80 "Net.OSErrorsForGetAddrinfo_Linux"; | 68 "Net.OSErrorsForGetAddrinfo_Linux"; |
| 81 #else | 69 #else |
| 82 "Net.OSErrorsForGetAddrinfo"; | 70 "Net.OSErrorsForGetAddrinfo"; |
| 83 #endif | 71 #endif |
| 84 | 72 |
| 85 HostCache* CreateDefaultCache() { | |
| 86 static const size_t kMaxHostCacheEntries = 100; | |
| 87 | |
| 88 HostCache* cache = new HostCache( | |
| 89 kMaxHostCacheEntries, | |
| 90 base::TimeDelta::FromMinutes(1), | |
| 91 base::TimeDelta::FromSeconds(0)); // Disable caching of failed DNS. | |
| 92 | |
| 93 return cache; | |
| 94 } | |
| 95 | |
| 96 // Gets a list of the likely error codes that getaddrinfo() can return | 73 // Gets a list of the likely error codes that getaddrinfo() can return |
| 97 // (non-exhaustive). These are the error codes that we will track via | 74 // (non-exhaustive). These are the error codes that we will track via |
| 98 // a histogram. | 75 // a histogram. |
| 99 std::vector<int> GetAllGetAddrinfoOSErrors() { | 76 std::vector<int> GetAllGetAddrinfoOSErrors() { |
| 100 int os_errors[] = { | 77 int os_errors[] = { |
| 101 #if defined(OS_POSIX) | 78 #if defined(OS_POSIX) |
| 102 #if !defined(OS_FREEBSD) | 79 #if !defined(OS_FREEBSD) |
| 103 #if !defined(OS_ANDROID) | 80 #if !defined(OS_ANDROID) |
| 104 // EAI_ADDRFAMILY has been declared obsolete in Android's netdb.h. | 81 // EAI_ADDRFAMILY has been declared obsolete in Android's netdb.h. |
| 105 EAI_ADDRFAMILY, | 82 EAI_ADDRFAMILY, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // Maximum of 8 concurrent resolver threads. | 127 // Maximum of 8 concurrent resolver threads. |
| 151 // Some routers (or resolvers) appear to start to provide host-not-found if | 128 // Some routers (or resolvers) appear to start to provide host-not-found if |
| 152 // too many simultaneous resolutions are pending. This number needs to be | 129 // too many simultaneous resolutions are pending. This number needs to be |
| 153 // further optimized, but 8 is what FF currently does. | 130 // further optimized, but 8 is what FF currently does. |
| 154 static const size_t kDefaultMaxJobs = 8u; | 131 static const size_t kDefaultMaxJobs = 8u; |
| 155 | 132 |
| 156 if (max_concurrent_resolves == HostResolver::kDefaultParallelism) | 133 if (max_concurrent_resolves == HostResolver::kDefaultParallelism) |
| 157 max_concurrent_resolves = kDefaultMaxJobs; | 134 max_concurrent_resolves = kDefaultMaxJobs; |
| 158 | 135 |
| 159 HostResolverImpl* resolver = | 136 HostResolverImpl* resolver = |
| 160 new HostResolverImpl(NULL, CreateDefaultCache(), max_concurrent_resolves, | 137 new HostResolverImpl(NULL, HostCache::CreateDefaultCache(), |
| 161 max_retry_attempts, net_log); | 138 max_concurrent_resolves, max_retry_attempts, net_log); |
| 162 | 139 |
| 163 return resolver; | 140 return resolver; |
| 164 } | 141 } |
| 165 | 142 |
| 166 static int ResolveAddrInfo(HostResolverProc* resolver_proc, | 143 static int ResolveAddrInfo(HostResolverProc* resolver_proc, |
| 167 const std::string& host, | 144 const std::string& host, |
| 168 AddressFamily address_family, | 145 AddressFamily address_family, |
| 169 HostResolverFlags host_resolver_flags, | 146 HostResolverFlags host_resolver_flags, |
| 170 AddressList* out, | 147 AddressList* out, |
| 171 int* os_error) { | 148 int* os_error) { |
| (...skipping 1455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1627 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; | 1604 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 1628 } else { | 1605 } else { |
| 1629 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; | 1606 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; |
| 1630 } | 1607 } |
| 1631 #endif | 1608 #endif |
| 1632 AbortAllInProgressJobs(); | 1609 AbortAllInProgressJobs(); |
| 1633 // |this| may be deleted inside AbortAllInProgressJobs(). | 1610 // |this| may be deleted inside AbortAllInProgressJobs(). |
| 1634 } | 1611 } |
| 1635 | 1612 |
| 1636 } // namespace net | 1613 } // namespace net |
| OLD | NEW |