| 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_piece.h" | 7 #include "base/strings/string_piece.h" |
| 8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "net/base/ip_address_number.h" | 9 #include "net/base/ip_address_number.h" |
| 10 #include "net/base/parse_number.h" | 10 #include "net/base/parse_number.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 std::vector<base::StringPiece> parts = base::SplitStringPiece( | 173 std::vector<base::StringPiece> parts = base::SplitStringPiece( |
| 174 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 174 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 175 if (parts.size() != 2) | 175 if (parts.size() != 2) |
| 176 return false; | 176 return false; |
| 177 | 177 |
| 178 // Parse the IP address. | 178 // Parse the IP address. |
| 179 if (!ip_address->AssignFromIPLiteral(parts[0])) | 179 if (!ip_address->AssignFromIPLiteral(parts[0])) |
| 180 return false; | 180 return false; |
| 181 | 181 |
| 182 // Parse the prefix length. | 182 // Parse the prefix length. |
| 183 int number_of_bits = -1; | 183 uint32_t number_of_bits; |
| 184 if (!ParseNonNegativeDecimalInt(parts[1], &number_of_bits)) | 184 if (!ParseUint32(parts[1], &number_of_bits)) |
| 185 return false; | 185 return false; |
| 186 | 186 |
| 187 // Make sure the prefix length is in a valid range. | 187 // Make sure the prefix length is in a valid range. |
| 188 if (number_of_bits < 0 || | 188 if (number_of_bits > ip_address->size() * 8) |
| 189 number_of_bits > static_cast<int>(ip_address->size() * 8)) | |
| 190 return false; | 189 return false; |
| 191 | 190 |
| 192 *prefix_length_in_bits = static_cast<size_t>(number_of_bits); | 191 *prefix_length_in_bits = number_of_bits; |
| 193 return true; | 192 return true; |
| 194 } | 193 } |
| 195 | 194 |
| 196 bool ParseURLHostnameToAddress(const std::string& hostname, | 195 bool ParseURLHostnameToAddress(const std::string& hostname, |
| 197 IPAddress* ip_address) { | 196 IPAddress* ip_address) { |
| 198 if (hostname.size() >= 2 && hostname.front() == '[' && | 197 if (hostname.size() >= 2 && hostname.front() == '[' && |
| 199 hostname.back() == ']') { | 198 hostname.back() == ']') { |
| 200 // Strip the square brackets that surround IPv6 literals. | 199 // Strip the square brackets that surround IPv6 literals. |
| 201 auto ip_literal = | 200 auto ip_literal = |
| 202 base::StringPiece(hostname).substr(1, hostname.size() - 2); | 201 base::StringPiece(hostname).substr(1, hostname.size() - 2); |
| 203 return ip_address->AssignFromIPLiteral(ip_literal) && ip_address->IsIPv6(); | 202 return ip_address->AssignFromIPLiteral(ip_literal) && ip_address->IsIPv6(); |
| 204 } | 203 } |
| 205 | 204 |
| 206 return ip_address->AssignFromIPLiteral(hostname) && ip_address->IsIPv4(); | 205 return ip_address->AssignFromIPLiteral(hostname) && ip_address->IsIPv4(); |
| 207 } | 206 } |
| 208 | 207 |
| 209 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { | 208 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { |
| 210 return CommonPrefixLength(a1.bytes(), a2.bytes()); | 209 return CommonPrefixLength(a1.bytes(), a2.bytes()); |
| 211 } | 210 } |
| 212 | 211 |
| 213 unsigned MaskPrefixLength(const IPAddress& mask) { | 212 unsigned MaskPrefixLength(const IPAddress& mask) { |
| 214 return MaskPrefixLength(mask.bytes()); | 213 return MaskPrefixLength(mask.bytes()); |
| 215 } | 214 } |
| 216 | 215 |
| 217 } // namespace net | 216 } // namespace net |
| OLD | NEW |