| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/ip_pattern.h" | 5 #include "net/base/ip_pattern.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 uint32 minimum; | 28 uint32 minimum; |
| 29 uint32 maximum; | 29 uint32 maximum; |
| 30 }; | 30 }; |
| 31 typedef std::vector<Range> RangeVector; | 31 typedef std::vector<Range> RangeVector; |
| 32 | 32 |
| 33 RangeVector ranges_; | 33 RangeVector ranges_; |
| 34 | 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(ComponentPattern); | 35 DISALLOW_COPY_AND_ASSIGN(ComponentPattern); |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 IPPattern::ComponentPattern::ComponentPattern() {} | 38 IPPattern::ComponentPattern::ComponentPattern() { |
| 39 } |
| 39 | 40 |
| 40 void IPPattern::ComponentPattern::AppendRange(uint32 min, uint32 max) { | 41 void IPPattern::ComponentPattern::AppendRange(uint32 min, uint32 max) { |
| 41 ranges_.push_back(Range(min, max)); | 42 ranges_.push_back(Range(min, max)); |
| 42 } | 43 } |
| 43 | 44 |
| 44 bool IPPattern::ComponentPattern::Match(uint32 value) const { | 45 bool IPPattern::ComponentPattern::Match(uint32 value) const { |
| 45 // Simple linear search should be fine, as we usually only have very few | 46 // Simple linear search should be fine, as we usually only have very few |
| 46 // distinct ranges to test. | 47 // distinct ranges to test. |
| 47 for (RangeVector::const_iterator range_it = ranges_.begin(); | 48 for (RangeVector::const_iterator range_it = ranges_.begin(); |
| 48 range_it != ranges_.end(); ++range_it) { | 49 range_it != ranges_.end(); ++range_it) { |
| 49 if (range_it->maximum >= value && range_it->minimum <= value) | 50 if (range_it->maximum >= value && range_it->minimum <= value) |
| 50 return true; | 51 return true; |
| 51 } | 52 } |
| 52 return false; | 53 return false; |
| 53 } | 54 } |
| 54 | 55 |
| 55 IPPattern::IPPattern() : is_ipv4_(true) {} | 56 IPPattern::IPPattern() : is_ipv4_(true) { |
| 57 } |
| 56 | 58 |
| 57 IPPattern::~IPPattern() { | 59 IPPattern::~IPPattern() { |
| 58 STLDeleteElements(&component_patterns_); | 60 STLDeleteElements(&component_patterns_); |
| 59 } | 61 } |
| 60 | 62 |
| 61 bool IPPattern::Match(const IPAddressNumber& address) const { | 63 bool IPPattern::Match(const IPAddressNumber& address) const { |
| 62 if (ip_mask_.empty()) | 64 if (ip_mask_.empty()) |
| 63 return false; | 65 return false; |
| 64 bool address_is_ipv4 = address.size() == kIPv4AddressSize; | 66 bool address_is_ipv4 = address.size() == kIPv4AddressSize; |
| 65 if (address_is_ipv4 != is_ipv4_) | 67 if (address_is_ipv4 != is_ipv4_) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 124 } |
| 123 // Now we know the size() is at least 2. | 125 // Now we know the size() is at least 2. |
| 124 if (component_it->size() == 2) { | 126 if (component_it->size() == 2) { |
| 125 DVLOG(1) << "Empty bracket: " << ip_pattern; | 127 DVLOG(1) << "Empty bracket: " << ip_pattern; |
| 126 return false; | 128 return false; |
| 127 } | 129 } |
| 128 // We'll need a pattern to match this bracketed component. | 130 // We'll need a pattern to match this bracketed component. |
| 129 scoped_ptr<ComponentPattern> component_pattern(new ComponentPattern); | 131 scoped_ptr<ComponentPattern> component_pattern(new ComponentPattern); |
| 130 // Trim leading and trailing bracket before calling for parsing. | 132 // Trim leading and trailing bracket before calling for parsing. |
| 131 if (!ParseComponentPattern(base::StringPiece(component_it->data() + 1, | 133 if (!ParseComponentPattern(base::StringPiece(component_it->data() + 1, |
| 132 component_it->size() - 2), component_pattern.get())) { | 134 component_it->size() - 2), |
| 135 component_pattern.get())) { |
| 133 return false; | 136 return false; |
| 134 } | 137 } |
| 135 ip_mask_.push_back(false); | 138 ip_mask_.push_back(false); |
| 136 component_patterns_.push_back(component_pattern.release()); | 139 component_patterns_.push_back(component_pattern.release()); |
| 137 } | 140 } |
| 138 return true; | 141 return true; |
| 139 } | 142 } |
| 140 | 143 |
| 141 bool IPPattern::ParseComponentPattern(const base::StringPiece& text, | 144 bool IPPattern::ParseComponentPattern(const base::StringPiece& text, |
| 142 ComponentPattern* pattern) const { | 145 ComponentPattern* pattern) const { |
| 143 // We're given a comma separated set of ranges, some of which may be simple | 146 // We're given a comma separated set of ranges, some of which may be simple |
| 144 // constants. | 147 // constants. |
| 145 Strings ranges; | 148 Strings ranges; |
| 146 base::SplitString(text.as_string(), ',', &ranges); | 149 base::SplitString(text.as_string(), ',', &ranges); |
| 147 for (Strings::iterator range_it = ranges.begin(); | 150 for (Strings::iterator range_it = ranges.begin(); range_it != ranges.end(); |
| 148 range_it != ranges.end(); ++range_it) { | 151 ++range_it) { |
| 149 base::StringTokenizer range_pair(*range_it, "-"); | 152 base::StringTokenizer range_pair(*range_it, "-"); |
| 150 uint32 min = 0; | 153 uint32 min = 0; |
| 151 range_pair.GetNext(); | 154 range_pair.GetNext(); |
| 152 if (!ValueTextToInt(range_pair.token(), &min)) | 155 if (!ValueTextToInt(range_pair.token(), &min)) |
| 153 return false; | 156 return false; |
| 154 uint32 max = min; // Sometimes we have no distinct max. | 157 uint32 max = min; // Sometimes we have no distinct max. |
| 155 if (range_pair.GetNext()) { | 158 if (range_pair.GetNext()) { |
| 156 if (!ValueTextToInt(range_pair.token(), &max)) | 159 if (!ValueTextToInt(range_pair.token(), &max)) |
| 157 return false; | 160 return false; |
| 158 } | 161 } |
| 159 if (range_pair.GetNext()) { | 162 if (range_pair.GetNext()) { |
| 160 // Too many "-" in this range specifier. | 163 // Too many "-" in this range specifier. |
| 161 DVLOG(1) << "Too many hyphens in range: "; | 164 DVLOG(1) << "Too many hyphens in range: "; |
| 162 return false; | 165 return false; |
| 163 } | 166 } |
| 164 pattern->AppendRange(min, max); | 167 pattern->AppendRange(min, max); |
| 165 } | 168 } |
| 166 return true; | 169 return true; |
| 167 } | 170 } |
| 168 | 171 |
| 169 bool IPPattern::ValueTextToInt(const base::StringPiece& input, | 172 bool IPPattern::ValueTextToInt(const base::StringPiece& input, |
| 170 uint32* output) const { | 173 uint32* output) const { |
| 171 bool ok = is_ipv4_ ? base::StringToUint(input, output) : | 174 bool ok = is_ipv4_ ? base::StringToUint(input, output) |
| 172 base::HexStringToUInt(input, output); | 175 : base::HexStringToUInt(input, output); |
| 173 if (!ok) { | 176 if (!ok) { |
| 174 DVLOG(1) << "Could not convert value to number: " << input; | 177 DVLOG(1) << "Could not convert value to number: " << input; |
| 175 return false; | 178 return false; |
| 176 } | 179 } |
| 177 if (is_ipv4_ && *output > 255u) { | 180 if (is_ipv4_ && *output > 255u) { |
| 178 DVLOG(1) << "IPv4 component greater than 255"; | 181 DVLOG(1) << "IPv4 component greater than 255"; |
| 179 return false; | 182 return false; |
| 180 } | 183 } |
| 181 if (!is_ipv4_ && *output > 0xFFFFu) { | 184 if (!is_ipv4_ && *output > 0xFFFFu) { |
| 182 DVLOG(1) << "IPv6 component greater than 0xFFFF"; | 185 DVLOG(1) << "IPv6 component greater than 0xFFFF"; |
| 183 return false; | 186 return false; |
| 184 } | 187 } |
| 185 return ok; | 188 return ok; |
| 186 } | 189 } |
| 187 | 190 |
| 188 } // namespace net | 191 } // namespace net |
| OLD | NEW |