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_util.h" | 7 #include "base/string_util.h" |
8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
12 #include "net/base/sys_addrinfo.h" | |
13 | 12 |
14 namespace net { | 13 namespace net { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 // Fills |*addrlist| with a socket address for |host| which should be an | 17 // Fills |*addrlist| with a socket address for |host| which should be an |
19 // IPv4 or IPv6 literal without enclosing brackets. If |canonical_name| is | 18 // IPv4 or IPv6 literal without enclosing brackets. If |canonical_name| is |
20 // non-empty it is used as the DNS canonical name for the host. Returns OK on | 19 // non-empty it is used as the DNS canonical name for the host. Returns OK on |
21 // success, ERR_UNEXPECTED otherwise. | 20 // success, ERR_UNEXPECTED otherwise. |
22 int CreateIPAddress(const std::string& host, | 21 int CreateIPAddress(const std::string& host, |
23 const std::string& canonical_name, | 22 const std::string& canonical_name, |
24 AddressList* addrlist) { | 23 AddressList* addrlist) { |
25 IPAddressNumber ip_number; | 24 IPAddressNumber ip_number; |
26 if (!ParseIPLiteralToNumber(host, &ip_number)) { | 25 if (!ParseIPLiteralToNumber(host, &ip_number)) { |
27 LOG(WARNING) << "Not a supported IP literal: " << host; | 26 LOG(WARNING) << "Not a supported IP literal: " << host; |
28 return ERR_UNEXPECTED; | 27 return ERR_UNEXPECTED; |
29 } | 28 } |
30 | 29 |
31 AddressList result(ip_number, -1, false); | 30 if (ip_number.size() == 4) { |
32 struct addrinfo* ai = const_cast<struct addrinfo*>(result.head()); | 31 *addrlist = AddressList::CreateIPv4Address(&ip_number[0], canonical_name); |
33 ai->ai_canonname = strdup(canonical_name.c_str()); | 32 } else if (ip_number.size() == 16) { |
34 *addrlist = result; | 33 *addrlist = AddressList::CreateIPv6Address(&ip_number[0], canonical_name); |
| 34 } else { |
| 35 NOTREACHED(); |
| 36 return ERR_UNEXPECTED; |
| 37 } |
| 38 |
35 return OK; | 39 return OK; |
36 } | 40 } |
37 | 41 |
38 } // namespace | 42 } // namespace |
39 | 43 |
40 MockHostResolverBase::MockHostResolverBase(bool use_caching) | 44 MockHostResolverBase::MockHostResolverBase(bool use_caching) |
41 : use_caching_(use_caching) { | 45 : use_caching_(use_caching) { |
42 Reset(NULL); | 46 Reset(NULL); |
43 } | 47 } |
44 | 48 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 DCHECK(!replacement.empty()); | 154 DCHECK(!replacement.empty()); |
151 Rule rule(Rule::kResolverTypeSystem, host_pattern, | 155 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
152 address_family, 0, replacement, "", 0); | 156 address_family, 0, replacement, "", 0); |
153 rules_.push_back(rule); | 157 rules_.push_back(rule); |
154 } | 158 } |
155 | 159 |
156 void RuleBasedHostResolverProc::AddIPLiteralRule( | 160 void RuleBasedHostResolverProc::AddIPLiteralRule( |
157 const std::string& host_pattern, | 161 const std::string& host_pattern, |
158 const std::string& ip_literal, | 162 const std::string& ip_literal, |
159 const std::string& canonical_name) { | 163 const std::string& canonical_name) { |
160 // Literals are always resolved to themselves by HostResolverImpl, | |
161 // consequently we do not support remapping them. | |
162 IPAddressNumber ip_number; | |
163 DCHECK(!ParseIPLiteralToNumber(host_pattern, &ip_number)); | |
164 Rule rule(Rule::kResolverTypeIPLiteral, | 164 Rule rule(Rule::kResolverTypeIPLiteral, |
165 host_pattern, | 165 host_pattern, |
166 ADDRESS_FAMILY_UNSPECIFIED, | 166 ADDRESS_FAMILY_UNSPECIFIED, |
167 canonical_name.empty() ? 0 : HOST_RESOLVER_CANONNAME, | 167 canonical_name.empty() ? 0 : HOST_RESOLVER_CANONNAME, |
168 ip_literal, | 168 ip_literal, |
169 canonical_name, | 169 canonical_name, |
170 0); | 170 0); |
171 rules_.push_back(rule); | 171 rules_.push_back(rule); |
172 } | 172 } |
173 | 173 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 CHECK_EQ(old_proc, current_proc_); | 254 CHECK_EQ(old_proc, current_proc_); |
255 } | 255 } |
256 | 256 |
257 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 257 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
258 current_proc_ = proc; | 258 current_proc_ = proc; |
259 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 259 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
260 current_proc_->SetLastProc(previous_proc_); | 260 current_proc_->SetLastProc(previous_proc_); |
261 } | 261 } |
262 | 262 |
263 } // namespace net | 263 } // namespace net |
OLD | NEW |