Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: net/base/ip_address_number.cc

Issue 1810183002: Migrate net/proxy/* to net::IPAddress. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments eroman Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/ip_address_number.h ('k') | net/base/ip_address_number_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_number.h" 5 #include "net/base/ip_address_number.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 address.begin() + arraysize(kIPv4MappedPrefix), 211 address.begin() + arraysize(kIPv4MappedPrefix),
212 kIPv4MappedPrefix); 212 kIPv4MappedPrefix);
213 } 213 }
214 214
215 IPAddressNumber ConvertIPv4MappedToIPv4(const IPAddressNumber& address) { 215 IPAddressNumber ConvertIPv4MappedToIPv4(const IPAddressNumber& address) {
216 DCHECK(IsIPv4Mapped(address)); 216 DCHECK(IsIPv4Mapped(address));
217 return IPAddressNumber(address.begin() + arraysize(kIPv4MappedPrefix), 217 return IPAddressNumber(address.begin() + arraysize(kIPv4MappedPrefix),
218 address.end()); 218 address.end());
219 } 219 }
220 220
221 bool ParseCIDRBlock(const std::string& cidr_literal,
222 IPAddressNumber* ip_number,
223 size_t* prefix_length_in_bits) {
224 // We expect CIDR notation to match one of these two templates:
225 // <IPv4-literal> "/" <number of bits>
226 // <IPv6-literal> "/" <number of bits>
227
228 std::vector<base::StringPiece> parts = base::SplitStringPiece(
229 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
230 if (parts.size() != 2)
231 return false;
232
233 // Parse the IP address.
234 if (!ParseIPLiteralToNumber(parts[0], ip_number))
235 return false;
236
237 // Parse the prefix length.
238 int number_of_bits = -1;
239 if (!base::StringToInt(parts[1], &number_of_bits))
240 return false;
241
242 // Make sure the prefix length is in a valid range.
243 if (number_of_bits < 0 ||
244 number_of_bits > static_cast<int>(ip_number->size() * 8))
245 return false;
246
247 *prefix_length_in_bits = static_cast<size_t>(number_of_bits);
248 return true;
249 }
250
251 bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number, 221 bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
252 const IPAddressNumber& ip_prefix, 222 const IPAddressNumber& ip_prefix,
253 size_t prefix_length_in_bits) { 223 size_t prefix_length_in_bits) {
254 // Both the input IP address and the prefix IP address should be 224 // Both the input IP address and the prefix IP address should be
255 // either IPv4 or IPv6. 225 // either IPv4 or IPv6.
256 DCHECK(ip_number.size() == 4 || ip_number.size() == 16); 226 DCHECK(ip_number.size() == 4 || ip_number.size() == 16);
257 DCHECK(ip_prefix.size() == 4 || ip_prefix.size() == 16); 227 DCHECK(ip_prefix.size() == 4 || ip_prefix.size() == 16);
258 228
259 DCHECK_LE(prefix_length_in_bits, ip_prefix.size() * 8); 229 DCHECK_LE(prefix_length_in_bits, ip_prefix.size() * 8);
260 230
(...skipping 28 matching lines...) Expand all
289 } 259 }
290 return a1.size() * CHAR_BIT; 260 return a1.size() * CHAR_BIT;
291 } 261 }
292 262
293 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 263 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
294 IPAddressNumber all_ones(mask.size(), 0xFF); 264 IPAddressNumber all_ones(mask.size(), 0xFF);
295 return CommonPrefixLength(mask, all_ones); 265 return CommonPrefixLength(mask, all_ones);
296 } 266 }
297 267
298 } // namespace net 268 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ip_address_number.h ('k') | net/base/ip_address_number_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698