Commit 3dc5d7ef authored by rch@chromium.org's avatar rch@chromium.org

Revert "Support replacement of IP address resolutions via command line flag"

Original CL: http://crrev.com/156963003

Preserved ip_pattern* which is now being used by the DnsConfigService.

BUG=347382

Review URL: https://codereview.chromium.org/215623003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@260230 0039d316-1c4b-4281-b951-d872f2087c98
parent 3cfe9374
......@@ -48,7 +48,6 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/cookie_store_factory.h"
#include "net/base/host_mapping_rules.h"
#include "net/base/ip_mapping_rules.h"
#include "net/base/net_util.h"
#include "net/base/network_time_notifier.h"
#include "net/base/sdch_manager.h"
......@@ -61,7 +60,6 @@
#include "net/dns/host_cache.h"
#include "net/dns/host_resolver.h"
#include "net/dns/mapped_host_resolver.h"
#include "net/dns/mapped_ip_resolver.h"
#include "net/ftp/ftp_network_layer.h"
#include "net/http/http_auth_filter.h"
#include "net/http/http_auth_handler_factory.h"
......@@ -201,28 +199,17 @@ scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) {
global_host_resolver->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4);
}
// If hostname or IP remappings were specified on the command-line, layer
// these rules on top of the real host resolver. Hostname remapping allows
// forwarding of all requests to hosts (matching a pattern) through a
// designated test server. IP remapping allows for all IP resolutions that
// match a given pattern, such as those destined for a specific CDN, to be
// instead directed to a specific/alternate IP address.
if (command_line.HasSwitch(switches::kHostResolverRules)) {
scoped_ptr<net::MappedHostResolver> remapped_resolver(
new net::MappedHostResolver(global_host_resolver.Pass()));
remapped_resolver->SetRulesFromString(
command_line.GetSwitchValueASCII(switches::kHostResolverRules));
global_host_resolver = remapped_resolver.Pass();
}
if (command_line.HasSwitch(switches::kIpResolverRules)) {
scoped_ptr<net::MappedIPResolver> remapped_resolver(
new net::MappedIPResolver(global_host_resolver.Pass()));
remapped_resolver->SetRulesFromString(
command_line.GetSwitchValueASCII(switches::kIpResolverRules));
global_host_resolver = remapped_resolver.Pass();
}
return global_host_resolver.Pass();
// If hostname remappings were specified on the command-line, layer these
// rules on top of the real host resolver. This allows forwarding all requests
// through a designated test server.
if (!command_line.HasSwitch(switches::kHostResolverRules))
return global_host_resolver.PassAs<net::HostResolver>();
scoped_ptr<net::MappedHostResolver> remapped_resolver(
new net::MappedHostResolver(global_host_resolver.Pass()));
remapped_resolver->SetRulesFromString(
command_line.GetSwitchValueASCII(switches::kHostResolverRules));
return remapped_resolver.PassAs<net::HostResolver>();
}
// TODO(willchan): Remove proxy script fetcher context since it's not necessary
......
......@@ -676,10 +676,6 @@ const char kIgnoreGpuBlacklist[] = "ignore-gpu-blacklist";
// Run the GPU process as a thread in the browser process.
const char kInProcessGPU[] = "in-process-gpu";
// These mappings only apply to the host resolver, and can insert additional
// resolutions in front of resolutions that match a pattern.
const char kIpResolverRules[] = "ip-resolver-rules";
// Specifies the flags passed to JS engine
const char kJavaScriptFlags[] = "js-flags";
......
......@@ -196,7 +196,6 @@ CONTENT_EXPORT extern const char kHostResolverRules[];
CONTENT_EXPORT extern const char kIgnoreCertificateErrors[];
CONTENT_EXPORT extern const char kIgnoreGpuBlacklist[];
extern const char kInProcessGPU[];
CONTENT_EXPORT extern const char kIpResolverRules[];
CONTENT_EXPORT extern const char kJavaScriptFlags[];
extern const char kLoadPlugin[];
CONTENT_EXPORT extern const char kLogGpuControlListDecisions[];
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/ip_mapping_rules.h"
#include <vector>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "net/base/address_list.h"
#include "net/base/ip_pattern.h"
namespace net {
class IPMappingRules::Rule {
public:
Rule(const IPAddressNumber& address, IPPattern* pattern)
: pattern_(pattern),
replacement_address_(address) {
}
bool Match(const IPAddressNumber& address) const {
return pattern_->Match(address);
}
const IPAddressNumber& replacement_address() const {
return replacement_address_;
}
private:
scoped_ptr<IPPattern> pattern_; // The pattern we can match.
IPAddressNumber replacement_address_; // The replacement address we provide.
DISALLOW_COPY_AND_ASSIGN(Rule);
};
IPMappingRules::IPMappingRules() {}
IPMappingRules::~IPMappingRules() {
STLDeleteElements(&rules_);
}
bool IPMappingRules::RewriteAddresses(AddressList* addresses) const {
// Last rule pushed into the list has the highest priority, and is tested
// first.
for (RuleList::const_reverse_iterator rule_it = rules_.rbegin();
rule_it != rules_.rend(); ++rule_it) {
for (AddressList::iterator address_it = addresses->begin();
address_it != addresses->end(); ++address_it) {
if (!(*rule_it)->Match(address_it->address()))
continue;
// We have a match.
int port = address_it->port();
addresses->insert(addresses->begin(),
IPEndPoint((*rule_it)->replacement_address(), port));
return true;
}
}
return false;
}
bool IPMappingRules::AddRuleFromString(const std::string& rule_string) {
std::string trimmed;
base::TrimWhitespaceASCII(rule_string, base::TRIM_ALL, &trimmed);
if (trimmed.empty())
return true; // Allow empty rules.
std::vector<std::string> parts;
base::SplitString(trimmed, ' ', &parts);
if (parts.size() != 3) {
DVLOG(1) << "Rule has wrong number of parts: " << rule_string;
return false;
}
if (!LowerCaseEqualsASCII(parts[0], "preface")) {
DVLOG(1) << "Unknown directive: " << rule_string;
return false;
}
scoped_ptr<IPPattern> pattern(new IPPattern);
if (!pattern->ParsePattern(parts[1])) {
DVLOG(1) << "Unacceptable pattern: " << rule_string;
return false;
}
IPAddressNumber address;
if (!ParseIPLiteralToNumber(parts[2], &address)) {
DVLOG(1) << "Invalid replacement address: " << rule_string;
return false;
}
if (pattern->is_ipv4() != (address.size() == kIPv4AddressSize)) {
DVLOG(1) << "Mixture of IPv4 and IPv6: " << rule_string;
return false;
}
rules_.push_back(new Rule(address, pattern.release()));
return true;
}
bool IPMappingRules::SetRulesFromString(const std::string& rules_string) {
STLDeleteElements(&rules_);
base::StringTokenizer rules(rules_string, ";");
while (rules.GetNext()) {
if (!AddRuleFromString(rules.token())) {
DVLOG(1) << "Failed parsing rule: " << rules.token();
STLDeleteElements(&rules_);
return false;
}
}
return true;
}
} // namespace net
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_BASE_IP_MAPPING_RULES_H_
#define NET_BASE_IP_MAPPING_RULES_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "net/base/net_export.h"
namespace net {
class AddressList;
// This class contains a list of rules, that can be used to process (Rewrite) a
// set of DNS resolutions (IP addresses) in accordance with those rules.
// Currently the only rules supported use a pattern match against some given IP
// addresses, and may, if there is a match, place one new IP address at the
// start of the rewritten address list (re: the "PREFACE" directive). This
// supports a common use case where the matching detects an address in a given
// edge network server group, and the inserted address points to a server that
// is expected to handle all requests that would otherwise have gone to the
// given group of servers.
class NET_EXPORT_PRIVATE IPMappingRules {
public:
IPMappingRules();
~IPMappingRules();
// Modifies |*addresses| based on the current rules. Returns true if
// |addresses| was modified, false otherwise.
bool RewriteAddresses(AddressList* addresses) const;
// Adds a rule to this mapper.
// Rules are evaluated against an address list until a first matching rule is
// found that applies, causing a modification of the address list.
// Each rule consists of one or more of the following pattern and action
// directives.
//
// RULE: CHANGE_DIRECTIVE | EMPTY
// CHANGE_DIRECTIVE: "PREFACE" SPACE IP_PATTERN SPACE IP_LITERAL
// SPACE: " "
// EMPTY:
//
// IP_LITERAL: IP_V6_LITERAL | IP_V4_LITERAL
//
// IP_V4_LITERAL: OCTET "." OCTET "." OCTET "." OCTET
// OCTET: decimal number in range 0 ... 255
//
// IP_V6_LITERAL: HEX_COMPONENT | IP_V6_LITERAL ":" HEX_COMPONENT
// HEX_COMPONENT: hexadecimal values with no more than 4 hex digits
//
// IP_PATTERN: IP_V6_PATTERN | IP_V4_PATTERN
//
// IP_V6_PATTERN: HEX_PATTERN | IP_V6_PATTERN ":" HEX_PATTERN
// HEX_PATTERN: HEX_COMPONENT | "[" HEX_GROUP_PATTERN "]" | "*"
// HEX_GROUP_PATTERN: HEX_RANGE | HEX_GROUP_PATTERN "," HEX_RANGE
// HEX_RANGE: HEX_COMPONENT | HEX_COMPONENT "-" HEX_COMPONENT
//
// IP_V4_PATTERN: OCTET_PATTERN "." OCTET_PATTERN "." OCTET_PATTERN
// "." OCTET_PATTERN
// OCTET_PATTERN: OCTET | "[" OCTET_GROUP_PATTERN "]" | "*"
// OCTET_GROUP_PATTERN: OCTET_RANGE | OCTET_GROUP_PATTERN "," OCTET_RANGE
// OCTET_RANGE: OCTET | OCTET "-" OCTET
//
// All literals are required to have all their components specified (4
// components for IPv4, and 8 for IPv6). Specifically, there is currently no
// support for elided compenents in an IPv6 address (e.g., "::").
// Returns true if the rule was successfully parsed and added.
bool AddRuleFromString(const std::string& rule_string);
// Sets the rules from a semicolon separated list of rules.
// Returns true if all the rules were successfully parsed and added.
bool SetRulesFromString(const std::string& rules_string);
private:
class Rule;
typedef std::vector<Rule*> RuleList;
// We own all rules in this list, and are responsible for their destruction.
RuleList rules_;
DISALLOW_COPY_AND_ASSIGN(IPMappingRules);
};
} // namespace net
#endif // NET_BASE_IP_MAPPING_RULES_H_
This diff is collapsed.
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/mapped_ip_resolver.h"
#include "base/strings/string_util.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
namespace net {
MappedIPResolver::MappedIPResolver(scoped_ptr<HostResolver> impl)
: impl_(impl.Pass()),
weak_factory_(this) {
}
MappedIPResolver::~MappedIPResolver() {}
int MappedIPResolver::Resolve(const RequestInfo& info,
RequestPriority priority,
AddressList* addresses,
const CompletionCallback& callback,
RequestHandle* out_req,
const BoundNetLog& net_log) {
CompletionCallback new_callback = base::Bind(&MappedIPResolver::ApplyRules,
weak_factory_.GetWeakPtr(),
callback, addresses);
int rv = impl_->Resolve(info, priority, addresses, new_callback, out_req,
net_log);
if (rv == OK)
rules_.RewriteAddresses(addresses);
return rv;
}
int MappedIPResolver::ResolveFromCache(const RequestInfo& info,
AddressList* addresses,
const BoundNetLog& net_log) {
int rv = impl_->ResolveFromCache(info, addresses, net_log);
if (rv == OK)
rules_.RewriteAddresses(addresses);
return rv;
}
void MappedIPResolver::CancelRequest(RequestHandle req) {
impl_->CancelRequest(req);
}
void MappedIPResolver::SetDnsClientEnabled(bool enabled) {
impl_->SetDnsClientEnabled(enabled);
}
HostCache* MappedIPResolver::GetHostCache() {
return impl_->GetHostCache();
}
base::Value* MappedIPResolver::GetDnsConfigAsValue() const {
return impl_->GetDnsConfigAsValue();
}
void MappedIPResolver::ApplyRules(const CompletionCallback& original_callback,
AddressList* addresses,
int rv) const {
if (rv == OK)
rules_.RewriteAddresses(addresses);
original_callback.Run(rv);
}
} // namespace net
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_DNS_MAPPED_IP_RESOLVER_H_
#define NET_DNS_MAPPED_IP_RESOLVER_H_
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/base/ip_mapping_rules.h"
#include "net/base/net_export.h"
#include "net/dns/host_resolver.h"
namespace net {
// This class wraps an existing HostResolver instance, but modifies the
// resolution response by inserting or replacing IP addresses before returning
// it. Currently, the only directive suported is a "PREFACE" directive which
// (when a match exists) inserts a single IP address at the start of a list.
class NET_EXPORT MappedIPResolver : public HostResolver {
public:
// Creates a MappedIPResolver that forwards all of its requests through
// |impl|.
explicit MappedIPResolver(scoped_ptr<HostResolver> impl);
virtual ~MappedIPResolver();
// Adds a rule to our IP mapper rules_.
// The most recently added rule "has priority" and will be used first (in
// preference to) any previous rules. Once one rule is found that matches,
// no other rules will be considered.
// See ip_mapping_rules.h for syntax and semantics.
// Returns true if the rule was successfully parsed and added.
bool AddRuleFromString(const std::string& rule_string) {
return rules_.AddRuleFromString(rule_string);
}
// Takes a semicolon separated list of rules, and assigns them to this
// resolver, discarding any previously added or set rules.
void SetRulesFromString(const std::string& rules_string) {
rules_.SetRulesFromString(rules_string);
}
// HostResolver methods:
virtual int Resolve(const RequestInfo& info,
RequestPriority priority,
AddressList* addresses,
const CompletionCallback& callback,
RequestHandle* out_req,
const BoundNetLog& net_log) OVERRIDE;
virtual int ResolveFromCache(const RequestInfo& info,
AddressList* addresses,
const BoundNetLog& net_log) OVERRIDE;
virtual void CancelRequest(RequestHandle req) OVERRIDE;
virtual void SetDnsClientEnabled(bool enabled) OVERRIDE;
virtual HostCache* GetHostCache() OVERRIDE;
virtual base::Value* GetDnsConfigAsValue() const OVERRIDE;
private:
// Modify the list of resolution |addresses| according to |rules_|, and then
// calls the |original_callback| with network error code |rv|.
void ApplyRules(const CompletionCallback& original_callback,
AddressList* addresses,
int rv) const;
scoped_ptr<HostResolver> impl_;
IPMappingRules rules_;
base::WeakPtrFactory<MappedIPResolver> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(MappedIPResolver);
};
} // namespace net
#endif // NET_DNS_MAPPED_IP_RESOLVER_H_
......@@ -136,8 +136,6 @@
'base/iovec.h',
'base/ip_endpoint.cc',
'base/ip_endpoint.h',
'base/ip_mapping_rules.cc',
'base/ip_mapping_rules.h',
'base/ip_pattern.cc',
'base/ip_pattern.h',
'base/keygen_handler.cc',
......@@ -490,8 +488,6 @@
'dns/host_resolver_proc.h',
'dns/mapped_host_resolver.cc',
'dns/mapped_host_resolver.h',
'dns/mapped_ip_resolver.cc',
'dns/mapped_ip_resolver.h',
'dns/mdns_cache.cc',
'dns/mdns_cache.h',
'dns/mdns_client.cc',
......@@ -1706,7 +1702,6 @@
'base/host_mapping_rules_unittest.cc',
'base/host_port_pair_unittest.cc',
'base/ip_endpoint_unittest.cc',
'base/ip_mapping_rules_unittest.cc',
'base/ip_pattern_unittest.cc',
'base/keygen_handler_unittest.cc',
'base/mime_sniffer_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment