Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Unified Diff: net/base/ip_mapping_rules.cc

Issue 156963003: Support replacement of IP address resolutions via command line flag (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to comments by wtc and rch Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/ip_mapping_rules.h ('k') | net/base/ip_mapping_rules_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/ip_mapping_rules.cc
diff --git a/net/base/ip_mapping_rules.cc b/net/base/ip_mapping_rules.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6c05ddde24bd2b5174e4a6a6caccc8e36365a8c2
--- /dev/null
+++ b/net/base/ip_mapping_rules.cc
@@ -0,0 +1,120 @@
+// Copyright (c) 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;
+ TrimWhitespaceASCII(rule_string, 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
« no previous file with comments | « net/base/ip_mapping_rules.h ('k') | net/base/ip_mapping_rules_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698