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