| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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_address.h" | 5 #include "net/base/ip_address.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "net/base/ip_address_number.h" | 10 #include "net/base/ip_address_number.h" |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 | 151 |
| 152 std::vector<base::StringPiece> parts = base::SplitStringPiece( | 152 std::vector<base::StringPiece> parts = base::SplitStringPiece( |
| 153 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 153 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 154 if (parts.size() != 2) | 154 if (parts.size() != 2) |
| 155 return false; | 155 return false; |
| 156 | 156 |
| 157 // Parse the IP address. | 157 // Parse the IP address. |
| 158 if (!ip_address->AssignFromIPLiteral(parts[0])) | 158 if (!ip_address->AssignFromIPLiteral(parts[0])) |
| 159 return false; | 159 return false; |
| 160 | 160 |
| 161 // TODO(martijnc): Find a more general solution for the overly permissive |
| 162 // base::StringToInt() parsing. https://crbug.com/596523. |
| 163 const base::StringPiece& prefix_length = parts[1]; |
| 164 if (prefix_length.starts_with("+")) |
| 165 return false; |
| 166 |
| 161 // Parse the prefix length. | 167 // Parse the prefix length. |
| 162 int number_of_bits = -1; | 168 int number_of_bits = -1; |
| 163 if (!base::StringToInt(parts[1], &number_of_bits)) | 169 if (!base::StringToInt(prefix_length, &number_of_bits)) |
| 164 return false; | 170 return false; |
| 165 | 171 |
| 166 // Make sure the prefix length is in a valid range. | 172 // Make sure the prefix length is in a valid range. |
| 167 if (number_of_bits < 0 || | 173 if (number_of_bits < 0 || |
| 168 number_of_bits > static_cast<int>(ip_address->size() * 8)) | 174 number_of_bits > static_cast<int>(ip_address->size() * 8)) |
| 169 return false; | 175 return false; |
| 170 | 176 |
| 171 *prefix_length_in_bits = static_cast<size_t>(number_of_bits); | 177 *prefix_length_in_bits = static_cast<size_t>(number_of_bits); |
| 172 return true; | 178 return true; |
| 173 } | 179 } |
| 174 | 180 |
| 175 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { | 181 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { |
| 176 return CommonPrefixLength(a1.bytes(), a2.bytes()); | 182 return CommonPrefixLength(a1.bytes(), a2.bytes()); |
| 177 } | 183 } |
| 178 | 184 |
| 179 unsigned MaskPrefixLength(const IPAddress& mask) { | 185 unsigned MaskPrefixLength(const IPAddress& mask) { |
| 180 return MaskPrefixLength(mask.bytes()); | 186 return MaskPrefixLength(mask.bytes()); |
| 181 } | 187 } |
| 182 | 188 |
| 183 } // namespace net | 189 } // namespace net |
| OLD | NEW |