| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/base/ip_mapping_rules.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_split.h" | |
| 14 #include "base/strings/string_tokenizer.h" | |
| 15 #include "base/strings/string_util.h" | |
| 16 #include "net/base/address_list.h" | |
| 17 #include "net/base/ip_pattern.h" | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 class IPMappingRules::Rule { | |
| 22 public: | |
| 23 Rule(const IPAddressNumber& address, IPPattern* pattern) | |
| 24 : pattern_(pattern), | |
| 25 replacement_address_(address) { | |
| 26 } | |
| 27 | |
| 28 bool Match(const IPAddressNumber& address) const { | |
| 29 return pattern_->Match(address); | |
| 30 } | |
| 31 | |
| 32 const IPAddressNumber& replacement_address() const { | |
| 33 return replacement_address_; | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 scoped_ptr<IPPattern> pattern_; // The pattern we can match. | |
| 38 IPAddressNumber replacement_address_; // The replacement address we provide. | |
| 39 DISALLOW_COPY_AND_ASSIGN(Rule); | |
| 40 }; | |
| 41 | |
| 42 IPMappingRules::IPMappingRules() {} | |
| 43 | |
| 44 IPMappingRules::~IPMappingRules() { | |
| 45 STLDeleteElements(&rules_); | |
| 46 } | |
| 47 | |
| 48 bool IPMappingRules::RewriteAddresses(AddressList* addresses) const { | |
| 49 // Last rule pushed into the list has the highest priority, and is tested | |
| 50 // first. | |
| 51 for (RuleList::const_reverse_iterator rule_it = rules_.rbegin(); | |
| 52 rule_it != rules_.rend(); ++rule_it) { | |
| 53 for (AddressList::iterator address_it = addresses->begin(); | |
| 54 address_it != addresses->end(); ++address_it) { | |
| 55 if (!(*rule_it)->Match(address_it->address())) | |
| 56 continue; | |
| 57 // We have a match. | |
| 58 int port = address_it->port(); | |
| 59 addresses->insert(addresses->begin(), | |
| 60 IPEndPoint((*rule_it)->replacement_address(), port)); | |
| 61 return true; | |
| 62 } | |
| 63 } | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 bool IPMappingRules::AddRuleFromString(const std::string& rule_string) { | |
| 68 std::string trimmed; | |
| 69 base::TrimWhitespaceASCII(rule_string, base::TRIM_ALL, &trimmed); | |
| 70 if (trimmed.empty()) | |
| 71 return true; // Allow empty rules. | |
| 72 | |
| 73 std::vector<std::string> parts; | |
| 74 base::SplitString(trimmed, ' ', &parts); | |
| 75 | |
| 76 if (parts.size() != 3) { | |
| 77 DVLOG(1) << "Rule has wrong number of parts: " << rule_string; | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 if (!LowerCaseEqualsASCII(parts[0], "preface")) { | |
| 82 DVLOG(1) << "Unknown directive: " << rule_string; | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 scoped_ptr<IPPattern> pattern(new IPPattern); | |
| 87 if (!pattern->ParsePattern(parts[1])) { | |
| 88 DVLOG(1) << "Unacceptable pattern: " << rule_string; | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 IPAddressNumber address; | |
| 93 if (!ParseIPLiteralToNumber(parts[2], &address)) { | |
| 94 DVLOG(1) << "Invalid replacement address: " << rule_string; | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 if (pattern->is_ipv4() != (address.size() == kIPv4AddressSize)) { | |
| 99 DVLOG(1) << "Mixture of IPv4 and IPv6: " << rule_string; | |
| 100 return false; | |
| 101 } | |
| 102 rules_.push_back(new Rule(address, pattern.release())); | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 bool IPMappingRules::SetRulesFromString(const std::string& rules_string) { | |
| 107 STLDeleteElements(&rules_); | |
| 108 | |
| 109 base::StringTokenizer rules(rules_string, ";"); | |
| 110 while (rules.GetNext()) { | |
| 111 if (!AddRuleFromString(rules.token())) { | |
| 112 DVLOG(1) << "Failed parsing rule: " << rules.token(); | |
| 113 STLDeleteElements(&rules_); | |
| 114 return false; | |
| 115 } | |
| 116 } | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 } // namespace net | |
| OLD | NEW |