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 <limits.h> | 7 #include <algorithm> |
| 8 #include <climits> | |
| 8 | 9 |
| 10 #include "base/containers/stack_container.h" | |
| 9 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 10 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 11 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 12 #include "net/base/parse_number.h" | 14 #include "net/base/parse_number.h" |
| 13 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 14 #include "url/url_canon_ip.h" | 16 #include "url/url_canon_ip.h" |
| 15 | 17 |
| 18 namespace net { | |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 // The prefix for IPv6 mapped IPv4 addresses. | 21 // The prefix for IPv6 mapped IPv4 addresses. |
| 19 // https://tools.ietf.org/html/rfc4291#section-2.5.5.2 | 22 // https://tools.ietf.org/html/rfc4291#section-2.5.5.2 |
| 20 const uint8_t kIPv4MappedPrefix[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF}; | 23 const uint8_t kIPv4MappedPrefix[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF}; |
| 21 | 24 |
| 22 // Note that this function assumes: | 25 // Note that this function assumes: |
| 23 // * |ip_address| is at least |prefix_length_in_bits| (bits) long; | 26 // * |ip_address| is at least |prefix_length_in_bits| (bits) long; |
| 24 // * |ip_prefix| is at least |prefix_length_in_bits| (bits) long. | 27 // * |ip_prefix| is at least |prefix_length_in_bits| (bits) long. |
| 25 bool IPAddressPrefixCheck(const std::vector<uint8_t>& ip_address, | 28 bool IPAddressPrefixCheck(const IPAddress::IPAddressBytes& ip_address, |
| 26 const uint8_t* ip_prefix, | 29 const uint8_t* ip_prefix, |
| 27 size_t prefix_length_in_bits) { | 30 size_t prefix_length_in_bits) { |
| 28 // Compare all the bytes that fall entirely within the prefix. | 31 // Compare all the bytes that fall entirely within the prefix. |
| 29 size_t num_entire_bytes_in_prefix = prefix_length_in_bits / 8; | 32 size_t num_entire_bytes_in_prefix = prefix_length_in_bits / 8; |
| 30 for (size_t i = 0; i < num_entire_bytes_in_prefix; ++i) { | 33 for (size_t i = 0; i < num_entire_bytes_in_prefix; ++i) { |
| 31 if (ip_address[i] != ip_prefix[i]) | 34 if (ip_address[i] != ip_prefix[i]) |
| 32 return false; | 35 return false; |
| 33 } | 36 } |
| 34 | 37 |
| 35 // In case the prefix was not a multiple of 8, there will be 1 byte | 38 // In case the prefix was not a multiple of 8, there will be 1 byte |
| 36 // which is only partially masked. | 39 // which is only partially masked. |
| 37 size_t remaining_bits = prefix_length_in_bits % 8; | 40 size_t remaining_bits = prefix_length_in_bits % 8; |
| 38 if (remaining_bits != 0) { | 41 if (remaining_bits != 0) { |
| 39 uint8_t mask = 0xFF << (8 - remaining_bits); | 42 uint8_t mask = 0xFF << (8 - remaining_bits); |
| 40 size_t i = num_entire_bytes_in_prefix; | 43 size_t i = num_entire_bytes_in_prefix; |
| 41 if ((ip_address[i] & mask) != (ip_prefix[i] & mask)) | 44 if ((ip_address[i] & mask) != (ip_prefix[i] & mask)) |
| 42 return false; | 45 return false; |
| 43 } | 46 } |
| 44 return true; | 47 return true; |
| 45 } | 48 } |
| 46 | 49 |
| 47 // Returns true if |ip_address| matches any of the reserved IPv4 ranges. This | 50 // Returns true if |ip_address| matches any of the reserved IPv4 ranges. This |
| 48 // method operates on a blacklist of reserved IPv4 ranges. Some ranges are | 51 // method operates on a blacklist of reserved IPv4 ranges. Some ranges are |
| 49 // consolidated. | 52 // consolidated. |
| 50 // Sources for info: | 53 // Sources for info: |
| 51 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml | 54 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml |
| 52 // www.iana.org/assignments/iana-ipv4-special-registry/ | 55 // www.iana.org/assignments/iana-ipv4-special-registry/ |
| 53 // iana-ipv4-special-registry.xhtml | 56 // iana-ipv4-special-registry.xhtml |
| 54 bool IsReservedIPv4(const std::vector<uint8_t>& ip_address) { | 57 bool IsReservedIPv4(const IPAddress::IPAddressBytes& ip_address) { |
| 55 // Different IP versions have different range reservations. | 58 // Different IP versions have different range reservations. |
| 56 DCHECK_EQ(net::IPAddress::kIPv4AddressSize, ip_address.size()); | 59 DCHECK_EQ(IPAddress::kIPv4AddressSize, ip_address.size()); |
| 57 struct { | 60 struct { |
| 58 const uint8_t address[4]; | 61 const uint8_t address[4]; |
| 59 size_t prefix_length_in_bits; | 62 size_t prefix_length_in_bits; |
| 60 } static const kReservedIPv4Ranges[] = { | 63 } static const kReservedIPv4Ranges[] = { |
| 61 {{0, 0, 0, 0}, 8}, {{10, 0, 0, 0}, 8}, {{100, 64, 0, 0}, 10}, | 64 {{0, 0, 0, 0}, 8}, {{10, 0, 0, 0}, 8}, {{100, 64, 0, 0}, 10}, |
| 62 {{127, 0, 0, 0}, 8}, {{169, 254, 0, 0}, 16}, {{172, 16, 0, 0}, 12}, | 65 {{127, 0, 0, 0}, 8}, {{169, 254, 0, 0}, 16}, {{172, 16, 0, 0}, 12}, |
| 63 {{192, 0, 2, 0}, 24}, {{192, 88, 99, 0}, 24}, {{192, 168, 0, 0}, 16}, | 66 {{192, 0, 2, 0}, 24}, {{192, 88, 99, 0}, 24}, {{192, 168, 0, 0}, 16}, |
| 64 {{198, 18, 0, 0}, 15}, {{198, 51, 100, 0}, 24}, {{203, 0, 113, 0}, 24}, | 67 {{198, 18, 0, 0}, 15}, {{198, 51, 100, 0}, 24}, {{203, 0, 113, 0}, 24}, |
| 65 {{224, 0, 0, 0}, 3}}; | 68 {{224, 0, 0, 0}, 3}}; |
| 66 | 69 |
| 67 for (const auto& range : kReservedIPv4Ranges) { | 70 for (const auto& range : kReservedIPv4Ranges) { |
| 68 if (IPAddressPrefixCheck(ip_address, range.address, | 71 if (IPAddressPrefixCheck(ip_address, range.address, |
| 69 range.prefix_length_in_bits)) { | 72 range.prefix_length_in_bits)) { |
| 70 return true; | 73 return true; |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 | 76 |
| 74 return false; | 77 return false; |
| 75 } | 78 } |
| 76 | 79 |
| 77 // Returns true if |ip_address| matches any of the reserved IPv6 ranges. This | 80 // Returns true if |ip_address| matches any of the reserved IPv6 ranges. This |
| 78 // method operates on a whitelist of non-reserved IPv6 ranges. All IPv6 | 81 // method operates on a whitelist of non-reserved IPv6 ranges. All IPv6 |
| 79 // addresses outside these ranges are reserved. | 82 // addresses outside these ranges are reserved. |
| 80 // Sources for info: | 83 // Sources for info: |
| 81 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml | 84 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml |
| 82 bool IsReservedIPv6(const std::vector<uint8_t>& ip_address) { | 85 bool IsReservedIPv6(const IPAddress::IPAddressBytes& ip_address) { |
| 83 // Different IP versions have different range reservations. | 86 // Different IP versions have different range reservations. |
| 84 DCHECK_EQ(net::IPAddress::kIPv6AddressSize, ip_address.size()); | 87 DCHECK_EQ(IPAddress::kIPv6AddressSize, ip_address.size()); |
| 85 struct { | 88 struct { |
| 86 const uint8_t address_prefix[2]; | 89 const uint8_t address_prefix[2]; |
| 87 size_t prefix_length_in_bits; | 90 size_t prefix_length_in_bits; |
| 88 } static const kPublicIPv6Ranges[] = { | 91 } static const kPublicIPv6Ranges[] = { |
| 89 // 2000::/3 -- Global Unicast | 92 // 2000::/3 -- Global Unicast |
| 90 {{0x20, 0}, 3}, | 93 {{0x20, 0}, 3}, |
| 91 // ff00::/8 -- Multicast | 94 // ff00::/8 -- Multicast |
| 92 {{0xff, 0}, 8}, | 95 {{0xff, 0}, 8}, |
| 93 }; | 96 }; |
| 94 | 97 |
| 95 for (const auto& range : kPublicIPv6Ranges) { | 98 for (const auto& range : kPublicIPv6Ranges) { |
| 96 if (IPAddressPrefixCheck(ip_address, range.address_prefix, | 99 if (IPAddressPrefixCheck(ip_address, range.address_prefix, |
| 97 range.prefix_length_in_bits)) { | 100 range.prefix_length_in_bits)) { |
| 98 return false; | 101 return false; |
| 99 } | 102 } |
| 100 } | 103 } |
| 101 | 104 |
| 102 return true; | 105 return true; |
| 103 } | 106 } |
| 104 | 107 |
| 105 bool ParseIPLiteralToBytes(const base::StringPiece& ip_literal, | 108 bool ParseIPLiteralToBytes(const base::StringPiece& ip_literal, |
| 106 std::vector<uint8_t>* bytes) { | 109 IPAddress::IPAddressBytes* bytes) { |
| 107 // |ip_literal| could be either an IPv4 or an IPv6 literal. If it contains | 110 // |ip_literal| could be either an IPv4 or an IPv6 literal. If it contains |
| 108 // a colon however, it must be an IPv6 address. | 111 // a colon however, it must be an IPv6 address. |
| 109 if (ip_literal.find(':') != base::StringPiece::npos) { | 112 if (ip_literal.find(':') != base::StringPiece::npos) { |
| 110 // GURL expects IPv6 hostnames to be surrounded with brackets. | 113 // GURL expects IPv6 hostnames to be surrounded with brackets. |
| 111 std::string host_brackets = "["; | 114 std::string host_brackets = "["; |
| 112 ip_literal.AppendToString(&host_brackets); | 115 ip_literal.AppendToString(&host_brackets); |
| 113 host_brackets.push_back(']'); | 116 host_brackets.push_back(']'); |
| 114 url::Component host_comp(0, host_brackets.size()); | 117 url::Component host_comp(0, host_brackets.size()); |
| 115 | 118 |
| 116 // Try parsing the hostname as an IPv6 literal. | 119 // Try parsing the hostname as an IPv6 literal. |
| 117 bytes->resize(16); // 128 bits. | 120 bytes->Resize(16); // 128 bits. |
| 118 return url::IPv6AddressToNumber(host_brackets.data(), host_comp, | 121 return url::IPv6AddressToNumber(host_brackets.data(), host_comp, |
| 119 bytes->data()); | 122 bytes->data()); |
| 120 } | 123 } |
| 121 | 124 |
| 122 // Otherwise the string is an IPv4 address. | 125 // Otherwise the string is an IPv4 address. |
| 123 bytes->resize(4); // 32 bits. | 126 bytes->Resize(4); // 32 bits. |
| 124 url::Component host_comp(0, ip_literal.size()); | 127 url::Component host_comp(0, ip_literal.size()); |
| 125 int num_components; | 128 int num_components; |
| 126 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( | 129 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( |
| 127 ip_literal.data(), host_comp, bytes->data(), &num_components); | 130 ip_literal.data(), host_comp, bytes->data(), &num_components); |
| 128 return family == url::CanonHostInfo::IPV4; | 131 return family == url::CanonHostInfo::IPV4; |
| 129 } | 132 } |
| 130 | 133 |
| 131 } // namespace | 134 } // namespace |
| 132 | 135 |
| 133 namespace net { | 136 IPAddress::IPAddressBytes::IPAddressBytes() : size_(0) {} |
| 137 | |
| 138 IPAddress::IPAddressBytes::IPAddressBytes(const uint8_t* data, size_t data_len) | |
| 139 : size_(data_len) { | |
| 140 CHECK_GE(16u, data_len); | |
| 141 std::copy_n(data, data_len, bytes_.data()); | |
| 142 } | |
| 143 | |
| 144 IPAddress::IPAddressBytes::~IPAddressBytes() {} | |
| 145 IPAddress::IPAddressBytes::IPAddressBytes( | |
| 146 IPAddress::IPAddressBytes const& other) = default; | |
| 147 | |
| 148 bool IPAddress::IPAddressBytes::operator<( | |
| 149 const IPAddress::IPAddressBytes& other) const { | |
| 150 if (size_ < other.size_) | |
|
eroman
2017/05/19 22:00:07
optional: A shorter version:
if (size_ == othe
Ryan Hamilton
2017/05/20 03:21:44
Done.
| |
| 151 return true; | |
| 152 if (size_ > other.size_) | |
| 153 return false; | |
| 154 return std::lexicographical_compare(begin(), end(), other.begin(), | |
| 155 other.end()); | |
| 156 } | |
| 157 | |
| 158 bool IPAddress::IPAddressBytes::operator==( | |
| 159 const IPAddress::IPAddressBytes& other) const { | |
| 160 return size_ == other.size_ && std::equal(begin(), end(), other.begin()); | |
| 161 } | |
| 162 | |
| 163 bool IPAddress::IPAddressBytes::operator!=( | |
| 164 const IPAddress::IPAddressBytes& other) const { | |
| 165 return !(*this == other); | |
| 166 } | |
| 134 | 167 |
| 135 IPAddress::IPAddress() {} | 168 IPAddress::IPAddress() {} |
| 136 | 169 |
| 137 IPAddress::IPAddress(const std::vector<uint8_t>& address) | |
| 138 : ip_address_(address) {} | |
| 139 | |
| 140 IPAddress::IPAddress(const IPAddress& other) = default; | 170 IPAddress::IPAddress(const IPAddress& other) = default; |
| 141 | 171 |
| 142 IPAddress::IPAddress(const uint8_t* address, size_t address_len) | 172 IPAddress::IPAddress(const uint8_t* address, size_t address_len) |
| 143 : ip_address_(address, address + address_len) {} | 173 : ip_address_(address, address_len) {} |
| 144 | 174 |
| 145 IPAddress::IPAddress(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) { | 175 IPAddress::IPAddress(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) { |
| 146 ip_address_.reserve(4); | |
| 147 ip_address_.push_back(b0); | 176 ip_address_.push_back(b0); |
| 148 ip_address_.push_back(b1); | 177 ip_address_.push_back(b1); |
| 149 ip_address_.push_back(b2); | 178 ip_address_.push_back(b2); |
| 150 ip_address_.push_back(b3); | 179 ip_address_.push_back(b3); |
| 151 } | 180 } |
| 152 | 181 |
| 153 IPAddress::IPAddress(uint8_t b0, | 182 IPAddress::IPAddress(uint8_t b0, |
| 154 uint8_t b1, | 183 uint8_t b1, |
| 155 uint8_t b2, | 184 uint8_t b2, |
| 156 uint8_t b3, | 185 uint8_t b3, |
| 157 uint8_t b4, | 186 uint8_t b4, |
| 158 uint8_t b5, | 187 uint8_t b5, |
| 159 uint8_t b6, | 188 uint8_t b6, |
| 160 uint8_t b7, | 189 uint8_t b7, |
| 161 uint8_t b8, | 190 uint8_t b8, |
| 162 uint8_t b9, | 191 uint8_t b9, |
| 163 uint8_t b10, | 192 uint8_t b10, |
| 164 uint8_t b11, | 193 uint8_t b11, |
| 165 uint8_t b12, | 194 uint8_t b12, |
| 166 uint8_t b13, | 195 uint8_t b13, |
| 167 uint8_t b14, | 196 uint8_t b14, |
| 168 uint8_t b15) { | 197 uint8_t b15) { |
| 169 const uint8_t address[] = {b0, b1, b2, b3, b4, b5, b6, b7, | 198 ip_address_.push_back(b0); |
| 170 b8, b9, b10, b11, b12, b13, b14, b15}; | 199 ip_address_.push_back(b1); |
| 171 ip_address_ = std::vector<uint8_t>(std::begin(address), std::end(address)); | 200 ip_address_.push_back(b2); |
| 201 ip_address_.push_back(b3); | |
| 202 ip_address_.push_back(b4); | |
| 203 ip_address_.push_back(b5); | |
| 204 ip_address_.push_back(b6); | |
| 205 ip_address_.push_back(b7); | |
| 206 ip_address_.push_back(b8); | |
| 207 ip_address_.push_back(b9); | |
| 208 ip_address_.push_back(b10); | |
| 209 ip_address_.push_back(b11); | |
| 210 ip_address_.push_back(b12); | |
| 211 ip_address_.push_back(b13); | |
| 212 ip_address_.push_back(b14); | |
| 213 ip_address_.push_back(b15); | |
| 172 } | 214 } |
| 173 | 215 |
| 174 IPAddress::~IPAddress() {} | 216 IPAddress::~IPAddress() {} |
| 175 | 217 |
| 176 bool IPAddress::IsIPv4() const { | 218 bool IPAddress::IsIPv4() const { |
| 177 return ip_address_.size() == kIPv4AddressSize; | 219 return ip_address_.size() == kIPv4AddressSize; |
| 178 } | 220 } |
| 179 | 221 |
| 180 bool IPAddress::IsIPv6() const { | 222 bool IPAddress::IsIPv6() const { |
| 181 return ip_address_.size() == kIPv6AddressSize; | 223 return ip_address_.size() == kIPv6AddressSize; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 201 } | 243 } |
| 202 | 244 |
| 203 return !empty(); | 245 return !empty(); |
| 204 } | 246 } |
| 205 | 247 |
| 206 bool IPAddress::IsIPv4MappedIPv6() const { | 248 bool IPAddress::IsIPv4MappedIPv6() const { |
| 207 return IsIPv6() && IPAddressStartsWith(*this, kIPv4MappedPrefix); | 249 return IsIPv6() && IPAddressStartsWith(*this, kIPv4MappedPrefix); |
| 208 } | 250 } |
| 209 | 251 |
| 210 bool IPAddress::AssignFromIPLiteral(const base::StringPiece& ip_literal) { | 252 bool IPAddress::AssignFromIPLiteral(const base::StringPiece& ip_literal) { |
| 211 std::vector<uint8_t> number; | 253 IPAddressBytes number; |
| 212 | 254 |
| 255 // TODO(rch): change the contract so ip_address_ is cleared on failure, | |
| 256 // to avoid needing this temporary at all. | |
| 213 if (!ParseIPLiteralToBytes(ip_literal, &number)) | 257 if (!ParseIPLiteralToBytes(ip_literal, &number)) |
| 214 return false; | 258 return false; |
| 215 | 259 |
| 216 std::swap(number, ip_address_); | 260 ip_address_ = number; |
| 217 return true; | 261 return true; |
| 218 } | 262 } |
| 219 | 263 |
| 264 std::vector<uint8_t> IPAddress::CopyBytesToVector() const { | |
| 265 return std::vector<uint8_t>(ip_address_.begin(), ip_address_.end()); | |
| 266 } | |
| 267 | |
| 220 // static | 268 // static |
| 221 IPAddress IPAddress::IPv4Localhost() { | 269 IPAddress IPAddress::IPv4Localhost() { |
| 222 static const uint8_t kLocalhostIPv4[] = {127, 0, 0, 1}; | 270 static const uint8_t kLocalhostIPv4[] = {127, 0, 0, 1}; |
| 223 return IPAddress(kLocalhostIPv4); | 271 return IPAddress(kLocalhostIPv4); |
| 224 } | 272 } |
| 225 | 273 |
| 226 // static | 274 // static |
| 227 IPAddress IPAddress::IPv6Localhost() { | 275 IPAddress IPAddress::IPv6Localhost() { |
| 228 static const uint8_t kLocalhostIPv6[] = {0, 0, 0, 0, 0, 0, 0, 0, | 276 static const uint8_t kLocalhostIPv6[] = {0, 0, 0, 0, 0, 0, 0, 0, |
| 229 0, 0, 0, 0, 0, 0, 0, 1}; | 277 0, 0, 0, 0, 0, 0, 0, 1}; |
| 230 return IPAddress(kLocalhostIPv6); | 278 return IPAddress(kLocalhostIPv6); |
| 231 } | 279 } |
| 232 | 280 |
| 233 // static | 281 // static |
| 234 IPAddress IPAddress::AllZeros(size_t num_zero_bytes) { | 282 IPAddress IPAddress::AllZeros(size_t num_zero_bytes) { |
| 235 return IPAddress(std::vector<uint8_t>(num_zero_bytes)); | 283 CHECK_LE(num_zero_bytes, 16u); |
| 284 IPAddress result; | |
| 285 for (size_t i = 0; i < num_zero_bytes; ++i) | |
| 286 result.ip_address_.push_back(0u); | |
| 287 return result; | |
| 236 } | 288 } |
| 237 | 289 |
| 238 // static | 290 // static |
| 239 IPAddress IPAddress::IPv4AllZeros() { | 291 IPAddress IPAddress::IPv4AllZeros() { |
| 240 return AllZeros(kIPv4AddressSize); | 292 return AllZeros(kIPv4AddressSize); |
| 241 } | 293 } |
| 242 | 294 |
| 243 // static | 295 // static |
| 244 IPAddress IPAddress::IPv6AllZeros() { | 296 IPAddress IPAddress::IPv6AllZeros() { |
| 245 return AllZeros(kIPv6AddressSize); | 297 return AllZeros(kIPv6AddressSize); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 | 342 |
| 291 std::string IPAddressToPackedString(const IPAddress& address) { | 343 std::string IPAddressToPackedString(const IPAddress& address) { |
| 292 return std::string(reinterpret_cast<const char*>(address.bytes().data()), | 344 return std::string(reinterpret_cast<const char*>(address.bytes().data()), |
| 293 address.size()); | 345 address.size()); |
| 294 } | 346 } |
| 295 | 347 |
| 296 IPAddress ConvertIPv4ToIPv4MappedIPv6(const IPAddress& address) { | 348 IPAddress ConvertIPv4ToIPv4MappedIPv6(const IPAddress& address) { |
| 297 DCHECK(address.IsIPv4()); | 349 DCHECK(address.IsIPv4()); |
| 298 // IPv4-mapped addresses are formed by: | 350 // IPv4-mapped addresses are formed by: |
| 299 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>. | 351 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>. |
| 300 std::vector<uint8_t> bytes; | 352 base::StackVector<uint8_t, 16> bytes; |
| 301 bytes.reserve(16); | 353 bytes->insert(bytes->end(), std::begin(kIPv4MappedPrefix), |
| 302 bytes.insert(bytes.end(), std::begin(kIPv4MappedPrefix), | 354 std::end(kIPv4MappedPrefix)); |
| 303 std::end(kIPv4MappedPrefix)); | 355 bytes->insert(bytes->end(), address.bytes().begin(), address.bytes().end()); |
| 304 bytes.insert(bytes.end(), address.bytes().begin(), address.bytes().end()); | 356 return IPAddress(bytes->data(), bytes->size()); |
| 305 return IPAddress(bytes); | |
| 306 } | 357 } |
| 307 | 358 |
| 308 IPAddress ConvertIPv4MappedIPv6ToIPv4(const IPAddress& address) { | 359 IPAddress ConvertIPv4MappedIPv6ToIPv4(const IPAddress& address) { |
| 309 DCHECK(address.IsIPv4MappedIPv6()); | 360 DCHECK(address.IsIPv4MappedIPv6()); |
| 310 | 361 |
| 311 return IPAddress(std::vector<uint8_t>( | 362 base::StackVector<uint8_t, 16> bytes; |
| 312 address.bytes().begin() + arraysize(kIPv4MappedPrefix), | 363 bytes->insert(bytes->end(), |
| 313 address.bytes().end())); | 364 address.bytes().begin() + arraysize(kIPv4MappedPrefix), |
| 365 address.bytes().end()); | |
| 366 return IPAddress(bytes->data(), bytes->size()); | |
| 314 } | 367 } |
| 315 | 368 |
| 316 bool IPAddressMatchesPrefix(const IPAddress& ip_address, | 369 bool IPAddressMatchesPrefix(const IPAddress& ip_address, |
| 317 const IPAddress& ip_prefix, | 370 const IPAddress& ip_prefix, |
| 318 size_t prefix_length_in_bits) { | 371 size_t prefix_length_in_bits) { |
| 319 // Both the input IP address and the prefix IP address should be either IPv4 | 372 // Both the input IP address and the prefix IP address should be either IPv4 |
| 320 // or IPv6. | 373 // or IPv6. |
| 321 DCHECK(ip_address.IsValid()); | 374 DCHECK(ip_address.IsValid()); |
| 322 DCHECK(ip_prefix.IsValid()); | 375 DCHECK(ip_prefix.IsValid()); |
| 323 | 376 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 if (diff & (1 << (CHAR_BIT - 1))) | 444 if (diff & (1 << (CHAR_BIT - 1))) |
| 392 return i * CHAR_BIT + j; | 445 return i * CHAR_BIT + j; |
| 393 diff <<= 1; | 446 diff <<= 1; |
| 394 } | 447 } |
| 395 NOTREACHED(); | 448 NOTREACHED(); |
| 396 } | 449 } |
| 397 return a1.size() * CHAR_BIT; | 450 return a1.size() * CHAR_BIT; |
| 398 } | 451 } |
| 399 | 452 |
| 400 unsigned MaskPrefixLength(const IPAddress& mask) { | 453 unsigned MaskPrefixLength(const IPAddress& mask) { |
| 401 std::vector<uint8_t> all_ones(mask.size(), 0xFF); | 454 base::StackVector<uint8_t, 16> all_ones; |
| 402 return CommonPrefixLength(mask, IPAddress(all_ones)); | 455 all_ones->resize(mask.size(), 0xFF); |
| 456 return CommonPrefixLength(mask, | |
| 457 IPAddress(all_ones->data(), all_ones->size())); | |
| 403 } | 458 } |
| 404 | 459 |
| 405 } // namespace net | 460 } // namespace net |
| OLD | NEW |