Chromium Code Reviews| 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 const base::StringPiece& prefix_length = parts[1]; | |
|
eroman
2016/03/21 17:28:31
Can you add a TODO linking to crbug.com/596523 ?
martijnc
2016/03/21 18:29:04
Done.
| |
| 162 if (prefix_length.starts_with("+")) | |
| 163 return false; | |
| 164 | |
| 161 // Parse the prefix length. | 165 // Parse the prefix length. |
| 162 int number_of_bits = -1; | 166 int number_of_bits = -1; |
| 163 if (!base::StringToInt(parts[1], &number_of_bits)) | 167 if (!base::StringToInt(prefix_length, &number_of_bits)) |
| 164 return false; | 168 return false; |
| 165 | 169 |
| 166 // Make sure the prefix length is in a valid range. | 170 // Make sure the prefix length is in a valid range. |
| 167 if (number_of_bits < 0 || | 171 if (number_of_bits < 0 || |
| 168 number_of_bits > static_cast<int>(ip_address->size() * 8)) | 172 number_of_bits > static_cast<int>(ip_address->size() * 8)) |
| 169 return false; | 173 return false; |
| 170 | 174 |
| 171 *prefix_length_in_bits = static_cast<size_t>(number_of_bits); | 175 *prefix_length_in_bits = static_cast<size_t>(number_of_bits); |
| 172 return true; | 176 return true; |
| 173 } | 177 } |
| 174 | 178 |
| 175 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { | 179 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { |
| 176 return CommonPrefixLength(a1.bytes(), a2.bytes()); | 180 return CommonPrefixLength(a1.bytes(), a2.bytes()); |
| 177 } | 181 } |
| 178 | 182 |
| 179 unsigned MaskPrefixLength(const IPAddress& mask) { | 183 unsigned MaskPrefixLength(const IPAddress& mask) { |
| 180 return MaskPrefixLength(mask.bytes()); | 184 return MaskPrefixLength(mask.bytes()); |
| 181 } | 185 } |
| 182 | 186 |
| 183 } // namespace net | 187 } // namespace net |
| OLD | NEW |