OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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::MapRule { | |
22 public: | |
23 MapRule(const IPAddressNumber& address, | |
24 IPPattern* pattern) | |
25 : pattern_(pattern), | |
26 replacement_address_(address) { | |
27 } | |
28 | |
29 bool Match(const IPAddressNumber& address) const { | |
30 return pattern_->Match(address); | |
31 } | |
32 | |
33 const IPAddressNumber& replacement_address() const { | |
34 return replacement_address_; | |
35 } | |
36 | |
37 private: | |
38 scoped_ptr<IPPattern> pattern_; // The pattern we can match. | |
39 IPAddressNumber replacement_address_; // The replacement address we provide. | |
40 DISALLOW_COPY_AND_ASSIGN(MapRule); | |
41 }; | |
42 | |
43 IPMappingRules::IPMappingRules() {} | |
44 | |
45 IPMappingRules::~IPMappingRules() { | |
46 ::STLDeleteElements(&rules_); | |
Ryan Hamilton
2014/02/19 00:11:48
nit: do you need :: here?
wtc
2014/02/19 00:24:11
Nit: we don't usually use :: in this case.
Also o
jar (doing other things)
2014/02/19 02:47:04
MSVC *was* complaining (when I didn't qualify the
| |
47 } | |
48 | |
49 bool IPMappingRules::RewriteAddresses(AddressList* addresses) const { | |
50 // Last rule pushed into the list has the highest priority, and is tested | |
51 // first. | |
52 for (RuleList::const_reverse_iterator rule_it = rules_.rbegin(); | |
53 rule_it != rules_.rend(); ++rule_it) { | |
54 for (AddressList::iterator address_it = addresses->begin(); | |
55 address_it != addresses->end(); ++address_it) { | |
56 if (!(*rule_it)->Match(address_it->address())) | |
57 continue; | |
58 // We have a match. | |
59 int port = address_it->port(); | |
60 addresses->insert(addresses->begin(), | |
61 IPEndPoint((*rule_it)->replacement_address(), port)); | |
62 return true; | |
63 } | |
64 } | |
65 return false; | |
66 } | |
67 | |
68 bool IPMappingRules::AddRuleFromString(const std::string& rule_string) { | |
69 std::string trimmed; | |
70 TrimWhitespaceASCII(rule_string, TRIM_ALL, &trimmed); | |
71 if (trimmed.empty()) | |
72 return true; // Allow empty rules. | |
73 | |
74 std::vector<std::string> parts; | |
75 base::SplitString(trimmed, ' ', &parts); | |
76 | |
77 if (parts.size() != 3) { | |
78 DVLOG(1) << "Rule has wrong number of parts: " << rule_string; | |
79 return false; | |
80 } | |
81 | |
82 if (!LowerCaseEqualsASCII(parts[0], "preface")) { | |
83 DVLOG(1) << "Uknown directive: " << rule_string; | |
wtc
2014/02/19 00:24:11
Typo: Uknown => Unknown
jar (doing other things)
2014/02/19 02:47:04
Done.
| |
84 return false; | |
85 } | |
86 | |
87 scoped_ptr<IPPattern> pattern(new IPPattern); | |
88 if (!pattern->ParsePattern(parts[1])) { | |
89 DVLOG(1) << "Unacceptable pattern: " << rule_string; | |
90 return false; | |
91 } | |
92 | |
93 IPAddressNumber address; | |
94 if (!ParseIPLiteralToNumber(parts[2], &address)) { | |
95 DVLOG(1) << "Invalid replacement address: " << rule_string; | |
96 return false; | |
97 } | |
98 | |
99 if (pattern->is_ipv4() != (address.size() == kIPv4AddressSize)) { | |
100 DVLOG(1) << "Mixture of IPv4 and IPv6: " << rule_string; | |
101 return false; | |
102 } | |
103 // Push a new MapRule, linking it to point at our previous list. | |
104 rules_.push_back(new MapRule(address, pattern.release())); | |
105 return true; | |
106 } | |
107 | |
108 bool IPMappingRules::SetRulesFromString(const std::string& rules_string) { | |
109 ::STLDeleteElements(&rules_); | |
110 | |
111 base::StringTokenizer rules(rules_string, ";"); | |
112 while (rules.GetNext()) { | |
113 if (!AddRuleFromString(rules.token())) { | |
114 DVLOG(1) << "Failed parsing rule: " << rules.token(); | |
115 ::STLDeleteElements(&rules_); | |
116 return false; | |
117 } | |
118 } | |
119 return true; | |
120 } | |
121 | |
122 } // namespace net | |
OLD | NEW |