| 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 "googleurl/src/url_canon_ip.h" | 10 #include "googleurl/src/url_canon_ip.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 // Fills |addrlist| with a socket address for |host| which should be an | 16 |
| 17 // IPv6 literal. Returns OK on success. | 17 // Fills |*addrlist| with a socket address for |host| which should be an |
| 18 int ResolveIPV6LiteralUsingGURL(const std::string& host, | 18 // IPv6 literal without enclosing brackets. If |canonical_name| is non-empty |
| 19 AddressList* addrlist) { | 19 // it is used as the DNS canonical name for the host. Returns OK on success, |
| 20 // ERR_UNEXPECTED otherwise. |
| 21 int CreateIPv6Address(const std::string& host, |
| 22 const std::string& canonical_name, |
| 23 AddressList* addrlist) { |
| 20 // GURL expects the hostname to be surrounded with brackets. | 24 // GURL expects the hostname to be surrounded with brackets. |
| 21 std::string host_brackets = "[" + host + "]"; | 25 std::string host_brackets = "[" + host + "]"; |
| 22 url_parse::Component host_comp(0, host_brackets.size()); | 26 url_parse::Component host_comp(0, host_brackets.size()); |
| 23 | 27 |
| 24 // Try parsing the hostname as an IPv6 literal. | 28 // Try parsing the hostname as an IPv6 literal. |
| 25 unsigned char ipv6_addr[16]; // 128 bits. | 29 unsigned char ipv6_addr[16]; // 128 bits. |
| 26 bool ok = url_canon::IPv6AddressToNumber(host_brackets.data(), | 30 bool ok = url_canon::IPv6AddressToNumber(host_brackets.data(), |
| 27 host_comp, | 31 host_comp, |
| 28 ipv6_addr); | 32 ipv6_addr); |
| 29 if (!ok) { | 33 if (!ok) { |
| 30 LOG(WARNING) << "Not an IPv6 literal: " << host; | 34 LOG(WARNING) << "Not an IPv6 literal: " << host; |
| 31 return ERR_UNEXPECTED; | 35 return ERR_UNEXPECTED; |
| 32 } | 36 } |
| 33 | 37 |
| 34 *addrlist = AddressList::CreateIPv6Address(ipv6_addr); | 38 *addrlist = AddressList::CreateIPv6Address(ipv6_addr, canonical_name); |
| 35 return OK; | 39 return OK; |
| 36 } | 40 } |
| 37 | 41 |
| 42 // Fills |*addrlist| with a socket address for |host| which should be an |
| 43 // IPv4 literal. If |canonical_name| is non-empty it is used as the DNS |
| 44 // canonical name for the host. Returns OK on success, ERR_UNEXPECTED otherwise. |
| 45 int CreateIPv4Address(const std::string& host, |
| 46 const std::string& canonical_name, |
| 47 AddressList* addrlist) { |
| 48 unsigned char ipv4_addr[4]; |
| 49 url_parse::Component host_comp(0, host.size()); |
| 50 int num_components; |
| 51 url_canon::CanonHostInfo::Family family = url_canon::IPv4AddressToNumber( |
| 52 host.data(), host_comp, ipv4_addr, &num_components); |
| 53 if (family != url_canon::CanonHostInfo::IPV4) { |
| 54 LOG(WARNING) << "Not an IPv4 literal: " << host; |
| 55 return ERR_UNEXPECTED; |
| 56 } |
| 57 *addrlist = AddressList::CreateIPv4Address(ipv4_addr, canonical_name); |
| 58 return OK; |
| 59 } |
| 60 |
| 38 } // namespace | 61 } // namespace |
| 39 | 62 |
| 40 MockHostResolverBase::MockHostResolverBase(bool use_caching) | 63 MockHostResolverBase::MockHostResolverBase(bool use_caching) |
| 41 : use_caching_(use_caching) { | 64 : use_caching_(use_caching) { |
| 42 Reset(NULL); | 65 Reset(NULL); |
| 43 } | 66 } |
| 44 | 67 |
| 45 int MockHostResolverBase::Resolve(const RequestInfo& info, | 68 int MockHostResolverBase::Resolve(const RequestInfo& info, |
| 46 AddressList* addresses, | 69 AddressList* addresses, |
| 47 CompletionCallback* callback, | 70 CompletionCallback* callback, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 impl_ = new HostResolverImpl(proc, cache, NULL, 50u); | 120 impl_ = new HostResolverImpl(proc, cache, NULL, 50u); |
| 98 } | 121 } |
| 99 | 122 |
| 100 //----------------------------------------------------------------------------- | 123 //----------------------------------------------------------------------------- |
| 101 | 124 |
| 102 struct RuleBasedHostResolverProc::Rule { | 125 struct RuleBasedHostResolverProc::Rule { |
| 103 enum ResolverType { | 126 enum ResolverType { |
| 104 kResolverTypeFail, | 127 kResolverTypeFail, |
| 105 kResolverTypeSystem, | 128 kResolverTypeSystem, |
| 106 kResolverTypeIPV6Literal, | 129 kResolverTypeIPV6Literal, |
| 130 kResolverTypeIPV4Literal, |
| 107 }; | 131 }; |
| 108 | 132 |
| 109 ResolverType resolver_type; | 133 ResolverType resolver_type; |
| 110 std::string host_pattern; | 134 std::string host_pattern; |
| 111 AddressFamily address_family; | 135 AddressFamily address_family; |
| 136 HostResolverFlags host_resolver_flags; |
| 112 std::string replacement; | 137 std::string replacement; |
| 138 std::string canonical_name; |
| 113 int latency_ms; // In milliseconds. | 139 int latency_ms; // In milliseconds. |
| 114 | 140 |
| 115 Rule(ResolverType resolver_type, | 141 Rule(ResolverType resolver_type, |
| 116 const std::string& host_pattern, | 142 const std::string& host_pattern, |
| 117 AddressFamily address_family, | 143 AddressFamily address_family, |
| 144 HostResolverFlags host_resolver_flags, |
| 118 const std::string& replacement, | 145 const std::string& replacement, |
| 146 const std::string& canonical_name, |
| 119 int latency_ms) | 147 int latency_ms) |
| 120 : resolver_type(resolver_type), | 148 : resolver_type(resolver_type), |
| 121 host_pattern(host_pattern), | 149 host_pattern(host_pattern), |
| 122 address_family(address_family), | 150 address_family(address_family), |
| 151 host_resolver_flags(host_resolver_flags), |
| 123 replacement(replacement), | 152 replacement(replacement), |
| 153 canonical_name(canonical_name), |
| 124 latency_ms(latency_ms) {} | 154 latency_ms(latency_ms) {} |
| 125 }; | 155 }; |
| 126 | 156 |
| 127 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) | 157 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) |
| 128 : HostResolverProc(previous) { | 158 : HostResolverProc(previous) { |
| 129 } | 159 } |
| 130 | 160 |
| 131 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { | 161 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { |
| 132 } | 162 } |
| 133 | 163 |
| 134 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, | 164 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, |
| 135 const std::string& replacement) { | 165 const std::string& replacement) { |
| 136 AddRuleForAddressFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, | 166 AddRuleForAddressFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, |
| 137 replacement); | 167 replacement); |
| 138 } | 168 } |
| 139 | 169 |
| 140 void RuleBasedHostResolverProc::AddRuleForAddressFamily( | 170 void RuleBasedHostResolverProc::AddRuleForAddressFamily( |
| 141 const std::string& host_pattern, | 171 const std::string& host_pattern, |
| 142 AddressFamily address_family, | 172 AddressFamily address_family, |
| 143 const std::string& replacement) { | 173 const std::string& replacement) { |
| 144 DCHECK(!replacement.empty()); | 174 DCHECK(!replacement.empty()); |
| 145 Rule rule(Rule::kResolverTypeSystem, host_pattern, | 175 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
| 146 address_family, replacement, 0); | 176 address_family, 0, replacement, "", 0); |
| 177 rules_.push_back(rule); |
| 178 } |
| 179 |
| 180 void RuleBasedHostResolverProc::AddIPv4Rule(const std::string& host_pattern, |
| 181 const std::string& ipv4_literal, |
| 182 const std::string& canonical_name) { |
| 183 Rule rule(Rule::kResolverTypeIPV4Literal, |
| 184 host_pattern, |
| 185 ADDRESS_FAMILY_UNSPECIFIED, |
| 186 canonical_name.empty() ? 0 : HOST_RESOLVER_CANONNAME, |
| 187 ipv4_literal, |
| 188 canonical_name, |
| 189 0); |
| 147 rules_.push_back(rule); | 190 rules_.push_back(rule); |
| 148 } | 191 } |
| 149 | 192 |
| 150 void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern, | 193 void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern, |
| 151 const std::string& ipv6_literal) { | 194 const std::string& ipv6_literal, |
| 195 const std::string& canonical_name) { |
| 152 Rule rule(Rule::kResolverTypeIPV6Literal, | 196 Rule rule(Rule::kResolverTypeIPV6Literal, |
| 153 host_pattern, | 197 host_pattern, |
| 154 ADDRESS_FAMILY_UNSPECIFIED, | 198 ADDRESS_FAMILY_UNSPECIFIED, |
| 199 canonical_name.empty() ? 0 : HOST_RESOLVER_CANONNAME, |
| 155 ipv6_literal, | 200 ipv6_literal, |
| 201 canonical_name, |
| 156 0); | 202 0); |
| 157 rules_.push_back(rule); | 203 rules_.push_back(rule); |
| 158 } | 204 } |
| 159 | 205 |
| 160 void RuleBasedHostResolverProc::AddRuleWithLatency( | 206 void RuleBasedHostResolverProc::AddRuleWithLatency( |
| 161 const std::string& host_pattern, | 207 const std::string& host_pattern, |
| 162 const std::string& replacement, | 208 const std::string& replacement, |
| 163 int latency_ms) { | 209 int latency_ms) { |
| 164 DCHECK(!replacement.empty()); | 210 DCHECK(!replacement.empty()); |
| 165 Rule rule(Rule::kResolverTypeSystem, host_pattern, | 211 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
| 166 ADDRESS_FAMILY_UNSPECIFIED, replacement, latency_ms); | 212 ADDRESS_FAMILY_UNSPECIFIED, 0, replacement, "", latency_ms); |
| 167 rules_.push_back(rule); | 213 rules_.push_back(rule); |
| 168 } | 214 } |
| 169 | 215 |
| 170 void RuleBasedHostResolverProc::AllowDirectLookup( | 216 void RuleBasedHostResolverProc::AllowDirectLookup( |
| 171 const std::string& host_pattern) { | 217 const std::string& host_pattern) { |
| 172 Rule rule(Rule::kResolverTypeSystem, host_pattern, | 218 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
| 173 ADDRESS_FAMILY_UNSPECIFIED, "", 0); | 219 ADDRESS_FAMILY_UNSPECIFIED, 0, "", "", 0); |
| 174 rules_.push_back(rule); | 220 rules_.push_back(rule); |
| 175 } | 221 } |
| 176 | 222 |
| 177 void RuleBasedHostResolverProc::AddSimulatedFailure( | 223 void RuleBasedHostResolverProc::AddSimulatedFailure( |
| 178 const std::string& host_pattern) { | 224 const std::string& host_pattern) { |
| 179 Rule rule(Rule::kResolverTypeFail, host_pattern, | 225 Rule rule(Rule::kResolverTypeFail, host_pattern, |
| 180 ADDRESS_FAMILY_UNSPECIFIED, "", 0); | 226 ADDRESS_FAMILY_UNSPECIFIED, 0, "", "", 0); |
| 181 rules_.push_back(rule); | 227 rules_.push_back(rule); |
| 182 } | 228 } |
| 183 | 229 |
| 184 int RuleBasedHostResolverProc::Resolve(const std::string& host, | 230 int RuleBasedHostResolverProc::Resolve(const std::string& host, |
| 185 AddressFamily address_family, | 231 AddressFamily address_family, |
| 186 HostResolverFlags host_resolver_flags, | 232 HostResolverFlags host_resolver_flags, |
| 187 AddressList* addrlist) { | 233 AddressList* addrlist) { |
| 188 RuleList::iterator r; | 234 RuleList::iterator r; |
| 189 for (r = rules_.begin(); r != rules_.end(); ++r) { | 235 for (r = rules_.begin(); r != rules_.end(); ++r) { |
| 190 bool matches_address_family = | 236 bool matches_address_family = |
| 191 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || | 237 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || |
| 192 r->address_family == address_family; | 238 r->address_family == address_family; |
| 193 | 239 // Flags match if all of the bitflags in host_resolver_flags are enabled |
| 194 if (matches_address_family && MatchPatternASCII(host, r->host_pattern)) { | 240 // in the rule's host_resolver_flags. However, the rule may have additional |
| 241 // flags specified, in which case the flags should still be considered a |
| 242 // match. |
| 243 bool matches_flags = (r->host_resolver_flags & host_resolver_flags) == |
| 244 host_resolver_flags; |
| 245 if (matches_flags && matches_address_family && |
| 246 MatchPatternASCII(host, r->host_pattern)) { |
| 195 if (r->latency_ms != 0) | 247 if (r->latency_ms != 0) |
| 196 PlatformThread::Sleep(r->latency_ms); | 248 PlatformThread::Sleep(r->latency_ms); |
| 197 | 249 |
| 198 // Remap to a new host. | 250 // Remap to a new host. |
| 199 const std::string& effective_host = | 251 const std::string& effective_host = |
| 200 r->replacement.empty() ? host : r->replacement; | 252 r->replacement.empty() ? host : r->replacement; |
| 201 | 253 |
| 202 // Apply the resolving function to the remapped hostname. | 254 // Apply the resolving function to the remapped hostname. |
| 203 switch (r->resolver_type) { | 255 switch (r->resolver_type) { |
| 204 case Rule::kResolverTypeFail: | 256 case Rule::kResolverTypeFail: |
| 205 return ERR_NAME_NOT_RESOLVED; | 257 return ERR_NAME_NOT_RESOLVED; |
| 206 case Rule::kResolverTypeSystem: | 258 case Rule::kResolverTypeSystem: |
| 207 return SystemHostResolverProc(effective_host, | 259 return SystemHostResolverProc(effective_host, |
| 208 address_family, | 260 address_family, |
| 209 host_resolver_flags, | 261 host_resolver_flags, |
| 210 addrlist); | 262 addrlist); |
| 211 case Rule::kResolverTypeIPV6Literal: | 263 case Rule::kResolverTypeIPV6Literal: |
| 212 return ResolveIPV6LiteralUsingGURL(effective_host, addrlist); | 264 return CreateIPv6Address(effective_host, r->canonical_name, addrlist); |
| 265 case Rule::kResolverTypeIPV4Literal: |
| 266 return CreateIPv4Address(effective_host, r->canonical_name, addrlist); |
| 213 default: | 267 default: |
| 214 NOTREACHED(); | 268 NOTREACHED(); |
| 215 return ERR_UNEXPECTED; | 269 return ERR_UNEXPECTED; |
| 216 } | 270 } |
| 217 } | 271 } |
| 218 } | 272 } |
| 219 return ResolveUsingPrevious(host, address_family, | 273 return ResolveUsingPrevious(host, address_family, |
| 220 host_resolver_flags, addrlist); | 274 host_resolver_flags, addrlist); |
| 221 } | 275 } |
| 222 | 276 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 233 CHECK_EQ(old_proc, current_proc_); | 287 CHECK_EQ(old_proc, current_proc_); |
| 234 } | 288 } |
| 235 | 289 |
| 236 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 290 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
| 237 current_proc_ = proc; | 291 current_proc_ = proc; |
| 238 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 292 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
| 239 current_proc_->SetLastProc(previous_proc_); | 293 current_proc_->SetLastProc(previous_proc_); |
| 240 } | 294 } |
| 241 | 295 |
| 242 } // namespace net | 296 } // namespace net |
| OLD | NEW |