| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/mock_host_resolver.h" | 5 #include "net/base/mock_host_resolver.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/string_split.h" | 14 #include "base/string_split.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 17 #include "net/base/host_cache.h" | 17 #include "net/base/host_cache.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 20 #include "net/base/sys_addrinfo.h" | |
| 21 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
| 22 | 21 |
| 23 namespace net { | 22 namespace net { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 // Cache size for the MockCachingHostResolver. | 26 // Cache size for the MockCachingHostResolver. |
| 28 const unsigned kMaxCacheEntries = 100; | 27 const unsigned kMaxCacheEntries = 100; |
| 29 // TTL for the successful resolutions. Failures are not cached. | 28 // TTL for the successful resolutions. Failures are not cached. |
| 30 const unsigned kCacheEntryTTLSeconds = 60; | 29 const unsigned kCacheEntryTTLSeconds = 60; |
| 31 | 30 |
| 32 } // namespace | 31 } // namespace |
| 33 | 32 |
| 34 int ParseAddressList(const std::string& host_list, | 33 int ParseAddressList(const std::string& host_list, |
| 35 const std::string& canonical_name, | 34 const std::string& canonical_name, |
| 36 AddressList* addrlist) { | 35 AddressList* addrlist) { |
| 37 *addrlist = AddressList(); | 36 *addrlist = AddressList(); |
| 38 std::vector<std::string> addresses; | 37 std::vector<std::string> addresses; |
| 39 base::SplitString(host_list, ',', &addresses); | 38 base::SplitString(host_list, ',', &addresses); |
| 39 addrlist->set_canonical_name(canonical_name); |
| 40 for (size_t index = 0; index < addresses.size(); ++index) { | 40 for (size_t index = 0; index < addresses.size(); ++index) { |
| 41 IPAddressNumber ip_number; | 41 IPAddressNumber ip_number; |
| 42 if (!ParseIPLiteralToNumber(addresses[index], &ip_number)) { | 42 if (!ParseIPLiteralToNumber(addresses[index], &ip_number)) { |
| 43 LOG(WARNING) << "Not a supported IP literal: " << addresses[index]; | 43 LOG(WARNING) << "Not a supported IP literal: " << addresses[index]; |
| 44 return ERR_UNEXPECTED; | 44 return ERR_UNEXPECTED; |
| 45 } | 45 } |
| 46 | 46 addrlist->push_back(IPEndPoint(ip_number, -1)); |
| 47 AddressList result = AddressList::CreateFromIPAddress(ip_number, -1); | |
| 48 struct addrinfo* ai = const_cast<struct addrinfo*>(result.head()); | |
| 49 if (index == 0) | |
| 50 ai->ai_canonname = base::strdup(canonical_name.c_str()); | |
| 51 if (!addrlist->head()) | |
| 52 *addrlist = AddressList::CreateByCopyingFirstAddress(result.head()); | |
| 53 else | |
| 54 addrlist->Append(result.head()); | |
| 55 } | 47 } |
| 56 return OK; | 48 return OK; |
| 57 } | 49 } |
| 58 | 50 |
| 59 struct MockHostResolverBase::Request { | 51 struct MockHostResolverBase::Request { |
| 60 Request(const RequestInfo& req_info, | 52 Request(const RequestInfo& req_info, |
| 61 AddressList* addr, | 53 AddressList* addr, |
| 62 const CompletionCallback& cb) | 54 const CompletionCallback& cb) |
| 63 : info(req_info), addresses(addr), callback(cb) {} | 55 : info(req_info), addresses(addr), callback(cb) {} |
| 64 RequestInfo info; | 56 RequestInfo info; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 122 |
| 131 if (use_caching) { | 123 if (use_caching) { |
| 132 cache_.reset(new HostCache(kMaxCacheEntries)); | 124 cache_.reset(new HostCache(kMaxCacheEntries)); |
| 133 } | 125 } |
| 134 } | 126 } |
| 135 | 127 |
| 136 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, | 128 int MockHostResolverBase::ResolveFromIPLiteralOrCache(const RequestInfo& info, |
| 137 AddressList* addresses) { | 129 AddressList* addresses) { |
| 138 IPAddressNumber ip; | 130 IPAddressNumber ip; |
| 139 if (ParseIPLiteralToNumber(info.hostname(), &ip)) { | 131 if (ParseIPLiteralToNumber(info.hostname(), &ip)) { |
| 140 *addresses = AddressList::CreateFromIPAddressWithCname( | 132 *addresses = AddressList::CreateFromIPAddress(ip, info.port()); |
| 141 ip, info.port(), info.host_resolver_flags() & HOST_RESOLVER_CANONNAME); | 133 if (info.host_resolver_flags() & HOST_RESOLVER_CANONNAME) |
| 134 addresses->SetDefaultCanonicalName(); |
| 142 return OK; | 135 return OK; |
| 143 } | 136 } |
| 144 int rv = ERR_DNS_CACHE_MISS; | 137 int rv = ERR_DNS_CACHE_MISS; |
| 145 if (cache_.get() && info.allow_cached_response()) { | 138 if (cache_.get() && info.allow_cached_response()) { |
| 146 HostCache::Key key(info.hostname(), | 139 HostCache::Key key(info.hostname(), |
| 147 info.address_family(), | 140 info.address_family(), |
| 148 info.host_resolver_flags()); | 141 info.host_resolver_flags()); |
| 149 const HostCache::Entry* entry = cache_->Lookup(key, base::TimeTicks::Now()); | 142 const HostCache::Entry* entry = cache_->Lookup(key, base::TimeTicks::Now()); |
| 150 if (entry) { | 143 if (entry) { |
| 151 rv = entry->error; | 144 rv = entry->error; |
| 152 if (rv == OK) | 145 if (rv == OK) { |
| 153 *addresses = CreateAddressListUsingPort(entry->addrlist, info.port()); | 146 *addresses = entry->addrlist; |
| 147 SetPortOnAddressList(info.port(), addresses); |
| 148 } |
| 154 } | 149 } |
| 155 } | 150 } |
| 156 return rv; | 151 return rv; |
| 157 } | 152 } |
| 158 | 153 |
| 159 int MockHostResolverBase::ResolveProc(size_t id, | 154 int MockHostResolverBase::ResolveProc(size_t id, |
| 160 const RequestInfo& info, | 155 const RequestInfo& info, |
| 161 AddressList* addresses) { | 156 AddressList* addresses) { |
| 162 AddressList addr; | 157 AddressList addr; |
| 163 int rv = proc_->Resolve(info.hostname(), | 158 int rv = proc_->Resolve(info.hostname(), |
| 164 info.address_family(), | 159 info.address_family(), |
| 165 info.host_resolver_flags(), | 160 info.host_resolver_flags(), |
| 166 &addr, | 161 &addr, |
| 167 NULL); | 162 NULL); |
| 168 if (cache_.get()) { | 163 if (cache_.get()) { |
| 169 HostCache::Key key(info.hostname(), | 164 HostCache::Key key(info.hostname(), |
| 170 info.address_family(), | 165 info.address_family(), |
| 171 info.host_resolver_flags()); | 166 info.host_resolver_flags()); |
| 172 // Storing a failure with TTL 0 so that it overwrites previous value. | 167 // Storing a failure with TTL 0 so that it overwrites previous value. |
| 173 base::TimeDelta ttl; | 168 base::TimeDelta ttl; |
| 174 if (rv == OK) | 169 if (rv == OK) |
| 175 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); | 170 ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); |
| 176 cache_->Set(key, rv, addr, base::TimeTicks::Now(), ttl); | 171 cache_->Set(key, rv, addr, base::TimeTicks::Now(), ttl); |
| 177 } | 172 } |
| 178 if (rv == OK) | 173 if (rv == OK) { |
| 179 *addresses = CreateAddressListUsingPort(addr, info.port()); | 174 *addresses = addr; |
| 175 SetPortOnAddressList(info.port(), addresses); |
| 176 } |
| 180 return rv; | 177 return rv; |
| 181 } | 178 } |
| 182 | 179 |
| 183 void MockHostResolverBase::ResolveNow(size_t id) { | 180 void MockHostResolverBase::ResolveNow(size_t id) { |
| 184 RequestMap::iterator it = requests_.find(id); | 181 RequestMap::iterator it = requests_.find(id); |
| 185 if (it == requests_.end()) | 182 if (it == requests_.end()) |
| 186 return; // was canceled | 183 return; // was canceled |
| 187 | 184 |
| 188 scoped_ptr<Request> req(it->second); | 185 scoped_ptr<Request> req(it->second); |
| 189 requests_.erase(it); | 186 requests_.erase(it); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 CHECK_EQ(old_proc, current_proc_); | 390 CHECK_EQ(old_proc, current_proc_); |
| 394 } | 391 } |
| 395 | 392 |
| 396 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 393 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 397 current_proc_ = proc; | 394 current_proc_ = proc; |
| 398 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 395 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 399 current_proc_->SetLastProc(previous_proc_); | 396 current_proc_->SetLastProc(previous_proc_); |
| 400 } | 397 } |
| 401 | 398 |
| 402 } // namespace net | 399 } // namespace net |
| OLD | NEW |