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

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: General cleanup 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
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..1f16bc39eeefa53d594ac607105e263563bbd4aa
--- /dev/null
+++ b/net/base/ip_mapping_rules.cc
@@ -0,0 +1,127 @@
+// 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/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/ip_pattern.h"
+
+namespace net {
+
+class IPMappingRules::MapRule {
+ public:
+ MapRule(const IPAddressNumber& address,
+ IPPattern* pattern,
+ MapRule* next)
+ : next_(next),
+ pattern_(pattern),
+ address_(address) {
+ }
+
+ bool Match(const IPAddressNumber& address) const {
+ return pattern_->Match(address);
+ }
+
+ const IPAddressNumber& Address() const { return address_; }
wtc 2014/02/11 23:52:30 Nit: this getter method should be lowercase "addre
jar (doing other things) 2014/02/15 21:14:56 Done.
+ MapRule* next() const { return next_; }
+
+ private:
+ MapRule* next_; // Linked list of other rules.
+ scoped_ptr<IPPattern> pattern_; // The pattern we can match.
wtc 2014/02/11 23:52:30 You should be able to declare this member as IPP
jar (doing other things) 2014/02/15 21:14:56 Done.
+ IPAddressNumber address_; // The replacement address we provide.
wtc 2014/02/11 23:52:30 Nit: I suggest renaming this member "replacement_a
jar (doing other things) 2014/02/15 21:14:56 Done.
+ DISALLOW_COPY_AND_ASSIGN(MapRule);
+};
+
+IPMappingRules::IPMappingRules() : first_rule_(NULL) {}
+
+IPMappingRules::~IPMappingRules() {
+ DeleteRules();
+}
+
+bool IPMappingRules::RewriteAddresses(AddressList* addresses) const {
+ for (MapRule* next_rule = first_rule_;
wtc 2014/02/11 23:52:30 Nit: I suggest "next_rule" be renamed "rule".
jar (doing other things) 2014/02/15 21:14:56 Done (plus new container refector)
+ next_rule != NULL; next_rule = next_rule->next()) {
+ for (AddressList::iterator address_it = addresses->begin();
+ address_it != addresses->end(); ++address_it) {
+ if (!next_rule->Match(address_it->address())) {
+ continue;
+ }
wtc 2014/02/11 23:52:30 Nit: omit curly braces.
jar (doing other things) 2014/02/15 21:14:56 I thought net/* style was to *always* use curlies.
+ // We have a match.
+ int port = address_it->port();
+ addresses->insert(addresses->begin(),
+ IPEndPoint(next_rule->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 == "")
wtc 2014/02/11 23:52:30 Nit: test trimmed.empty().
jar (doing other things) 2014/02/15 21:14:56 Done.
+ 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) << "Rule missing directive: " << rule_string;
wtc 2014/02/11 23:52:30 The error message should be "Unknown directive".
jar (doing other things) 2014/02/15 21:14:56 Done.
+ 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;
+ }
+ // Push a new MapRule, linking it to point at our previous list.
+ first_rule_ = new MapRule(address, pattern.release(), first_rule_);
+ return true;
+}
+
+bool IPMappingRules::SetRulesFromString(const std::string& rules_string) {
+ DeleteRules();
+
+ base::StringTokenizer rules(rules_string, ";");
+ while (rules.GetNext()) {
+ if (!AddRuleFromString(rules.token())) {
+ DVLOG(1) << "Failed parsing rule: " << rules.token();
+ DeleteRules();
+ return false;
+ }
+ }
+ return true;
+}
+
+void IPMappingRules::DeleteRules() {
+ while (first_rule_ != NULL) {
+ MapRule* rule = first_rule_;
+ first_rule_ = first_rule_->next();
+ delete rule;
+ }
+}
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698