| 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/mock_host_resolver.h" | 5 #include "net/base/mock_host_resolver.h" |
| 6 | 6 |
| 7 #include "base/string_split.h" |
| 7 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 8 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 9 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 10 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 11 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
| 12 #include "net/base/sys_addrinfo.h" | 13 #include "net/base/sys_addrinfo.h" |
| 13 | 14 |
| 14 namespace net { | 15 namespace net { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 char* do_strdup(const char* src) { | 19 char* do_strdup(const char* src) { |
| 19 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 20 return _strdup(src); | 21 return _strdup(src); |
| 21 #else | 22 #else |
| 22 return strdup(src); | 23 return strdup(src); |
| 23 #endif | 24 #endif |
| 24 } | 25 } |
| 25 | 26 |
| 26 // Fills |*addrlist| with a socket address for |host| which should be an | 27 // Fills |*addrlist| with a socket address for |host_list| which should be a |
| 27 // IPv4 or IPv6 literal without enclosing brackets. If |canonical_name| is | 28 // comma-separated list of IPv4 or IPv6 literal(s) without enclosing brackets. |
| 28 // non-empty it is used as the DNS canonical name for the host. Returns OK on | 29 // If |canonical_name| is non-empty it is used as the DNS canonical name for |
| 29 // success, ERR_UNEXPECTED otherwise. | 30 // the host. Returns OK on success, ERR_UNEXPECTED otherwise. |
| 30 int CreateIPAddress(const std::string& host, | 31 int CreateIPAddressList(const std::string& host_list, |
| 31 const std::string& canonical_name, | 32 const std::string& canonical_name, |
| 32 AddressList* addrlist) { | 33 AddressList* addrlist) { |
| 33 IPAddressNumber ip_number; | 34 *addrlist = AddressList(); |
| 34 if (!ParseIPLiteralToNumber(host, &ip_number)) { | 35 std::vector<std::string> addresses; |
| 35 LOG(WARNING) << "Not a supported IP literal: " << host; | 36 base::SplitString(host_list, ',', &addresses); |
| 36 return ERR_UNEXPECTED; | 37 for (size_t index = 0; index < addresses.size(); ++index) { |
| 38 IPAddressNumber ip_number; |
| 39 if (!ParseIPLiteralToNumber(addresses[index], &ip_number)) { |
| 40 LOG(WARNING) << "Not a supported IP literal: " << addresses[index]; |
| 41 return ERR_UNEXPECTED; |
| 42 } |
| 43 |
| 44 AddressList result(ip_number, -1, false); |
| 45 struct addrinfo* ai = const_cast<struct addrinfo*>(result.head()); |
| 46 if (index == 0) |
| 47 ai->ai_canonname = do_strdup(canonical_name.c_str()); |
| 48 if (!addrlist->head()) |
| 49 addrlist->Copy(result.head(), false); |
| 50 else |
| 51 addrlist->Append(result.head()); |
| 37 } | 52 } |
| 38 | |
| 39 AddressList result(ip_number, -1, false); | |
| 40 struct addrinfo* ai = const_cast<struct addrinfo*>(result.head()); | |
| 41 ai->ai_canonname = do_strdup(canonical_name.c_str()); | |
| 42 *addrlist = result; | |
| 43 return OK; | 53 return OK; |
| 44 } | 54 } |
| 45 | 55 |
| 46 } // namespace | 56 } // namespace |
| 47 | 57 |
| 48 MockHostResolverBase::~MockHostResolverBase() {} | 58 MockHostResolverBase::~MockHostResolverBase() {} |
| 49 | 59 |
| 50 void MockHostResolverBase::Reset(HostResolverProc* interceptor) { | 60 void MockHostResolverBase::Reset(HostResolverProc* interceptor) { |
| 51 synchronous_mode_ = false; | 61 synchronous_mode_ = false; |
| 52 | 62 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 const std::string& canonical_name) { | 178 const std::string& canonical_name) { |
| 169 // Literals are always resolved to themselves by HostResolverImpl, | 179 // Literals are always resolved to themselves by HostResolverImpl, |
| 170 // consequently we do not support remapping them. | 180 // consequently we do not support remapping them. |
| 171 IPAddressNumber ip_number; | 181 IPAddressNumber ip_number; |
| 172 DCHECK(!ParseIPLiteralToNumber(host_pattern, &ip_number)); | 182 DCHECK(!ParseIPLiteralToNumber(host_pattern, &ip_number)); |
| 173 HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY | | 183 HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY | |
| 174 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; | 184 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 175 if (!canonical_name.empty()) | 185 if (!canonical_name.empty()) |
| 176 flags |= HOST_RESOLVER_CANONNAME; | 186 flags |= HOST_RESOLVER_CANONNAME; |
| 177 Rule rule(Rule::kResolverTypeIPLiteral, host_pattern, | 187 Rule rule(Rule::kResolverTypeIPLiteral, host_pattern, |
| 178 ADDRESS_FAMILY_UNSPECIFIED, flags, ip_literal, canonical_name, 0); | 188 ADDRESS_FAMILY_UNSPECIFIED, flags, ip_literal, canonical_name, |
| 189 0); |
| 179 rules_.push_back(rule); | 190 rules_.push_back(rule); |
| 180 } | 191 } |
| 181 | 192 |
| 182 void RuleBasedHostResolverProc::AddRuleWithLatency( | 193 void RuleBasedHostResolverProc::AddRuleWithLatency( |
| 183 const std::string& host_pattern, | 194 const std::string& host_pattern, |
| 184 const std::string& replacement, | 195 const std::string& replacement, |
| 185 int latency_ms) { | 196 int latency_ms) { |
| 186 DCHECK(!replacement.empty()); | 197 DCHECK(!replacement.empty()); |
| 187 HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY | | 198 HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY | |
| 188 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; | 199 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 // Apply the resolving function to the remapped hostname. | 248 // Apply the resolving function to the remapped hostname. |
| 238 switch (r->resolver_type) { | 249 switch (r->resolver_type) { |
| 239 case Rule::kResolverTypeFail: | 250 case Rule::kResolverTypeFail: |
| 240 return ERR_NAME_NOT_RESOLVED; | 251 return ERR_NAME_NOT_RESOLVED; |
| 241 case Rule::kResolverTypeSystem: | 252 case Rule::kResolverTypeSystem: |
| 242 return SystemHostResolverProc(effective_host, | 253 return SystemHostResolverProc(effective_host, |
| 243 address_family, | 254 address_family, |
| 244 host_resolver_flags, | 255 host_resolver_flags, |
| 245 addrlist, os_error); | 256 addrlist, os_error); |
| 246 case Rule::kResolverTypeIPLiteral: | 257 case Rule::kResolverTypeIPLiteral: |
| 247 return CreateIPAddress(effective_host, r->canonical_name, addrlist); | 258 return CreateIPAddressList(effective_host, |
| 259 r->canonical_name, |
| 260 addrlist); |
| 248 default: | 261 default: |
| 249 NOTREACHED(); | 262 NOTREACHED(); |
| 250 return ERR_UNEXPECTED; | 263 return ERR_UNEXPECTED; |
| 251 } | 264 } |
| 252 } | 265 } |
| 253 } | 266 } |
| 254 return ResolveUsingPrevious(host, address_family, | 267 return ResolveUsingPrevious(host, address_family, |
| 255 host_resolver_flags, addrlist, os_error); | 268 host_resolver_flags, addrlist, os_error); |
| 256 } | 269 } |
| 257 | 270 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 CHECK_EQ(old_proc, current_proc_); | 307 CHECK_EQ(old_proc, current_proc_); |
| 295 } | 308 } |
| 296 | 309 |
| 297 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 310 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 298 current_proc_ = proc; | 311 current_proc_ = proc; |
| 299 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 312 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 300 current_proc_->SetLastProc(previous_proc_); | 313 current_proc_->SetLastProc(previous_proc_); |
| 301 } | 314 } |
| 302 | 315 |
| 303 } // namespace net | 316 } // namespace net |
| OLD | NEW |