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/strings/string_number_conversions.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/string_tokenizer.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "net/base/ip_pattern.h" | |
15 | |
16 namespace net { | |
17 | |
18 class IPMappingRules::MapRule { | |
19 public: | |
20 MapRule(const IPAddressNumber& address, | |
21 IPPattern* pattern, | |
22 MapRule* next) | |
23 : next_(next), | |
24 pattern_(pattern), | |
25 address_(address) { | |
26 } | |
27 | |
28 bool Match(const IPAddressNumber& address) const { | |
29 return pattern_->Match(address); | |
30 } | |
31 | |
32 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.
| |
33 MapRule* next() const { return next_; } | |
34 | |
35 private: | |
36 MapRule* next_; // Linked list of other rules. | |
37 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.
| |
38 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.
| |
39 DISALLOW_COPY_AND_ASSIGN(MapRule); | |
40 }; | |
41 | |
42 IPMappingRules::IPMappingRules() : first_rule_(NULL) {} | |
43 | |
44 IPMappingRules::~IPMappingRules() { | |
45 DeleteRules(); | |
46 } | |
47 | |
48 bool IPMappingRules::RewriteAddresses(AddressList* addresses) const { | |
49 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)
| |
50 next_rule != NULL; next_rule = next_rule->next()) { | |
51 for (AddressList::iterator address_it = addresses->begin(); | |
52 address_it != addresses->end(); ++address_it) { | |
53 if (!next_rule->Match(address_it->address())) { | |
54 continue; | |
55 } | |
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.
| |
56 // We have a match. | |
57 int port = address_it->port(); | |
58 addresses->insert(addresses->begin(), | |
59 IPEndPoint(next_rule->Address(), port)); | |
60 return true; | |
61 } | |
62 } | |
63 return false; | |
64 } | |
65 | |
66 bool IPMappingRules::AddRuleFromString(const std::string& rule_string) { | |
67 std::string trimmed; | |
68 TrimWhitespaceASCII(rule_string, TRIM_ALL, &trimmed); | |
69 if (trimmed == "") | |
wtc
2014/02/11 23:52:30
Nit: test trimmed.empty().
jar (doing other things)
2014/02/15 21:14:56
Done.
| |
70 return true; // Allow empty rules. | |
71 | |
72 std::vector<std::string> parts; | |
73 base::SplitString(trimmed, ' ', &parts); | |
74 | |
75 if (parts.size() != 3) { | |
76 DVLOG(1) << "Rule has wrong number of parts: " << rule_string; | |
77 return false; | |
78 } | |
79 | |
80 if (!LowerCaseEqualsASCII(parts[0], "preface")) { | |
81 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.
| |
82 return false; | |
83 } | |
84 | |
85 scoped_ptr<IPPattern> pattern(new IPPattern); | |
86 if (!pattern->ParsePattern(parts[1])) { | |
87 DVLOG(1) << "Unacceptable pattern: " << rule_string; | |
88 return false; | |
89 } | |
90 | |
91 IPAddressNumber address; | |
92 if (!ParseIPLiteralToNumber(parts[2], &address)) { | |
93 DVLOG(1) << "Invalid replacement address: " << rule_string; | |
94 return false; | |
95 } | |
96 | |
97 if (pattern->is_ipv4() != (address.size() == kIPv4AddressSize)) { | |
98 DVLOG(1) << "Mixture of IPv4 and IPv6: " << rule_string; | |
99 return false; | |
100 } | |
101 // Push a new MapRule, linking it to point at our previous list. | |
102 first_rule_ = new MapRule(address, pattern.release(), first_rule_); | |
103 return true; | |
104 } | |
105 | |
106 bool IPMappingRules::SetRulesFromString(const std::string& rules_string) { | |
107 DeleteRules(); | |
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 DeleteRules(); | |
114 return false; | |
115 } | |
116 } | |
117 return true; | |
118 } | |
119 | |
120 void IPMappingRules::DeleteRules() { | |
121 while (first_rule_ != NULL) { | |
122 MapRule* rule = first_rule_; | |
123 first_rule_ = first_rule_->next(); | |
124 delete rule; | |
125 } | |
126 } | |
127 } // namespace net | |
OLD | NEW |