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

Side by Side Diff: net/base/ip_address.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
OLDNEW
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"
8 #include "base/strings/string_piece.h"
9 #include "base/strings/string_split.h"
7 #include "net/base/ip_address_number.h" 10 #include "net/base/ip_address_number.h"
8 #include "url/gurl.h" 11 #include "url/gurl.h"
9 #include "url/url_canon_ip.h" 12 #include "url/url_canon_ip.h"
10 13
11 namespace net { 14 namespace net {
12 15
13 IPAddress::IPAddress() {} 16 IPAddress::IPAddress() {}
14 17
15 IPAddress::IPAddress(const IPAddressNumber& address) : ip_address_(address) {} 18 IPAddress::IPAddress(const IPAddressNumber& address) : ip_address_(address) {}
16 19
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 return IPAddress(ConvertIPv4MappedToIPv4(address.bytes())); 135 return IPAddress(ConvertIPv4MappedToIPv4(address.bytes()));
133 } 136 }
134 137
135 bool IPAddressMatchesPrefix(const IPAddress& ip_address, 138 bool IPAddressMatchesPrefix(const IPAddress& ip_address,
136 const IPAddress& ip_prefix, 139 const IPAddress& ip_prefix,
137 size_t prefix_length_in_bits) { 140 size_t prefix_length_in_bits) {
138 return IPNumberMatchesPrefix(ip_address.bytes(), ip_prefix.bytes(), 141 return IPNumberMatchesPrefix(ip_address.bytes(), ip_prefix.bytes(),
139 prefix_length_in_bits); 142 prefix_length_in_bits);
140 } 143 }
141 144
145 bool ParseCIDRBlock(const std::string& cidr_literal,
146 IPAddress* ip_address,
147 size_t* prefix_length_in_bits) {
148 // We expect CIDR notation to match one of these two templates:
149 // <IPv4-literal> "/" <number of bits>
150 // <IPv6-literal> "/" <number of bits>
151
152 std::vector<base::StringPiece> parts = base::SplitStringPiece(
153 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
154 if (parts.size() != 2)
155 return false;
156
157 // Parse the IP address.
158 if (!ip_address->AssignFromIPLiteral(parts[0]))
159 return false;
160
161 // Parse the prefix length.
162 int number_of_bits = -1;
163 if (!base::StringToInt(parts[1], &number_of_bits))
164 return false;
165
166 // Make sure the prefix length is in a valid range.
167 if (number_of_bits < 0 ||
168 number_of_bits > static_cast<int>(ip_address->size() * 8))
169 return false;
170
171 *prefix_length_in_bits = static_cast<size_t>(number_of_bits);
172 return true;
173 }
174
142 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { 175 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) {
143 return CommonPrefixLength(a1.bytes(), a2.bytes()); 176 return CommonPrefixLength(a1.bytes(), a2.bytes());
144 } 177 }
145 178
146 unsigned MaskPrefixLength(const IPAddress& mask) { 179 unsigned MaskPrefixLength(const IPAddress& mask) {
147 return MaskPrefixLength(mask.bytes()); 180 return MaskPrefixLength(mask.bytes());
148 } 181 }
149 182
150 } // namespace net 183 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698