| 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 #ifndef NET_BASE_IP_ADDRESS_H_ | 5 #ifndef NET_BASE_IP_ADDRESS_H_ |
| 6 #define NET_BASE_IP_ADDRESS_H_ | 6 #define NET_BASE_IP_ADDRESS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <array> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 16 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| 21 // Helper class to represent the sequence of bytes in an IP address. |
| 22 // A vector<uint8_t> would be simpler but incurs heap allocation, so |
| 23 // IPAddressBytes uses a fixed size array. |
| 24 class NET_EXPORT IPAddressBytes { |
| 25 public: |
| 26 IPAddressBytes(); |
| 27 IPAddressBytes(const uint8_t* data, size_t data_len); |
| 28 IPAddressBytes(const IPAddressBytes& other); |
| 29 ~IPAddressBytes(); |
| 30 |
| 31 // Copies |data_len| elements from |data| into this object. |
| 32 void Assign(const uint8_t* data, size_t data_len); |
| 33 |
| 34 // Returns the number of elements in the underlying array. |
| 35 size_t size() const { return size_; } |
| 36 |
| 37 // Sets the size to be |size|. Does not actually change the size |
| 38 // of the underlying array or zero-initialize the bytes. |
| 39 void Resize(size_t size) { |
| 40 DCHECK_LE(size, 16u); |
| 41 size_ = static_cast<uint8_t>(size); |
| 42 } |
| 43 |
| 44 // Returns true if the underlying array is empty. |
| 45 bool empty() const { return size_ == 0; } |
| 46 |
| 47 // Returns a pointer to the underlying array of bytes. |
| 48 const uint8_t* data() const { return bytes_.data(); } |
| 49 uint8_t* data() { return bytes_.data(); } |
| 50 |
| 51 // Returns a pointer to the first element. |
| 52 const uint8_t* begin() const { return data(); } |
| 53 uint8_t* begin() { return data(); } |
| 54 |
| 55 // Returns a pointer past the last element. |
| 56 const uint8_t* end() const { return data() + size_; } |
| 57 uint8_t* end() { return data() + size_; } |
| 58 |
| 59 // Returns a reference to the last element. |
| 60 uint8_t& back() { |
| 61 DCHECK(!empty()); |
| 62 return bytes_[size_ - 1]; |
| 63 } |
| 64 const uint8_t& back() const { |
| 65 DCHECK(!empty()); |
| 66 return bytes_[size_ - 1]; |
| 67 } |
| 68 |
| 69 // Appends |val| to the end and increments the size. |
| 70 void push_back(uint8_t val) { |
| 71 DCHECK_GT(16, size_); |
| 72 bytes_[size_++] = val; |
| 73 } |
| 74 |
| 75 // Returns a reference to the byte at index |pos|. |
| 76 uint8_t& operator[](size_t pos) { |
| 77 DCHECK_LT(pos, size_); |
| 78 return bytes_[pos]; |
| 79 } |
| 80 const uint8_t& operator[](size_t pos) const { |
| 81 DCHECK_LT(pos, size_); |
| 82 return bytes_[pos]; |
| 83 } |
| 84 |
| 85 bool operator<(const IPAddressBytes& other) const; |
| 86 bool operator!=(const IPAddressBytes& other) const; |
| 87 bool operator==(const IPAddressBytes& other) const; |
| 88 |
| 89 private: |
| 90 // Underlying sequence of bytes |
| 91 std::array<uint8_t, 16> bytes_; |
| 92 |
| 93 // Number of elements in |bytes_|. Should be either kIPv4AddressSize |
| 94 // or kIPv6AddressSize or 0. |
| 95 uint8_t size_; |
| 96 }; |
| 97 |
| 20 class NET_EXPORT IPAddress { | 98 class NET_EXPORT IPAddress { |
| 21 public: | 99 public: |
| 22 enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 }; | 100 enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 }; |
| 23 | 101 |
| 24 // Creates a zero-sized, invalid address. | 102 // Creates a zero-sized, invalid address. |
| 25 IPAddress(); | 103 IPAddress(); |
| 26 | 104 |
| 27 // Copies the input address to |ip_address_|. The input is expected to be in | 105 IPAddress(const IPAddress& other); |
| 28 // network byte order. | |
| 29 explicit IPAddress(const std::vector<uint8_t>& address); | |
| 30 | 106 |
| 31 IPAddress(const IPAddress& other); | 107 // Copies the input address to |ip_address_|. |
| 108 explicit IPAddress(const IPAddressBytes& address); |
| 32 | 109 |
| 33 // Copies the input address to |ip_address_|. The input is expected to be in | 110 // Copies the input address to |ip_address_|. The input is expected to be in |
| 34 // network byte order. | 111 // network byte order. |
| 35 template <size_t N> | 112 template <size_t N> |
| 36 IPAddress(const uint8_t(&address)[N]) | 113 IPAddress(const uint8_t(&address)[N]) |
| 37 : IPAddress(address, N) {} | 114 : IPAddress(address, N) {} |
| 38 | 115 |
| 39 // Copies the input address to |ip_address_| taking an additional length | 116 // Copies the input address to |ip_address_| taking an additional length |
| 40 // parameter. The input is expected to be in network byte order. | 117 // parameter. The input is expected to be in network byte order. |
| 41 IPAddress(const uint8_t* address, size_t address_len); | 118 IPAddress(const uint8_t* address, size_t address_len); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // Returns the canonical string representation of an IP address. | 172 // Returns the canonical string representation of an IP address. |
| 96 // For example: "192.168.0.1" or "::1". Returns the empty string when | 173 // For example: "192.168.0.1" or "::1". Returns the empty string when |
| 97 // |ip_address_| is invalid. | 174 // |ip_address_| is invalid. |
| 98 std::string ToString() const; | 175 std::string ToString() const; |
| 99 | 176 |
| 100 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. | 177 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. |
| 101 // Returns true on success and fills |ip_address_| with the numeric value. | 178 // Returns true on success and fills |ip_address_| with the numeric value. |
| 102 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) | 179 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) |
| 103 WARN_UNUSED_RESULT; | 180 WARN_UNUSED_RESULT; |
| 104 | 181 |
| 105 // Returns the underlying byte vector. | 182 // Returns the underlying bytes. |
| 106 const std::vector<uint8_t>& bytes() const { return ip_address_; }; | 183 const IPAddressBytes& bytes() const { return ip_address_; }; |
| 184 |
| 185 // Copies the bytes to a new vector. Generally callers should be using |
| 186 // |bytes()| and the IPAddressBytes abstraction. This method is provided as a |
| 187 // convenience for call sites that existed prior to the introduction of |
| 188 // IPAddressBytes. |
| 189 std::vector<uint8_t> CopyBytesToVector() const; |
| 107 | 190 |
| 108 // Returns an IPAddress instance representing the 127.0.0.1 address. | 191 // Returns an IPAddress instance representing the 127.0.0.1 address. |
| 109 static IPAddress IPv4Localhost(); | 192 static IPAddress IPv4Localhost(); |
| 110 | 193 |
| 111 // Returns an IPAddress instance representing the ::1 address. | 194 // Returns an IPAddress instance representing the ::1 address. |
| 112 static IPAddress IPv6Localhost(); | 195 static IPAddress IPv6Localhost(); |
| 113 | 196 |
| 114 // Returns an IPAddress made up of |num_zero_bytes| zeros. | 197 // Returns an IPAddress made up of |num_zero_bytes| zeros. |
| 115 static IPAddress AllZeros(size_t num_zero_bytes); | 198 static IPAddress AllZeros(size_t num_zero_bytes); |
| 116 | 199 |
| 117 // Returns an IPAddress instance representing the 0.0.0.0 address. | 200 // Returns an IPAddress instance representing the 0.0.0.0 address. |
| 118 static IPAddress IPv4AllZeros(); | 201 static IPAddress IPv4AllZeros(); |
| 119 | 202 |
| 120 // Returns an IPAddress instance representing the :: address. | 203 // Returns an IPAddress instance representing the :: address. |
| 121 static IPAddress IPv6AllZeros(); | 204 static IPAddress IPv6AllZeros(); |
| 122 | 205 |
| 123 bool operator==(const IPAddress& that) const; | 206 bool operator==(const IPAddress& that) const; |
| 124 bool operator!=(const IPAddress& that) const; | 207 bool operator!=(const IPAddress& that) const; |
| 125 bool operator<(const IPAddress& that) const; | 208 bool operator<(const IPAddress& that) const; |
| 126 | 209 |
| 127 private: | 210 private: |
| 128 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address | 211 IPAddressBytes ip_address_; |
| 129 // will have length kIPv6AddressSize. | |
| 130 std::vector<uint8_t> ip_address_; | |
| 131 | 212 |
| 132 // This class is copyable and assignable. | 213 // This class is copyable and assignable. |
| 133 }; | 214 }; |
| 134 | 215 |
| 135 using IPAddressList = std::vector<IPAddress>; | 216 using IPAddressList = std::vector<IPAddress>; |
| 136 | 217 |
| 137 // Returns the canonical string representation of an IP address along with its | 218 // Returns the canonical string representation of an IP address along with its |
| 138 // port. For example: "192.168.0.1:99" or "[::1]:80". | 219 // port. For example: "192.168.0.1:99" or "[::1]:80". |
| 139 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, | 220 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, |
| 140 uint16_t port); | 221 uint16_t port); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 template <size_t N> | 280 template <size_t N> |
| 200 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { | 281 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { |
| 201 if (address.size() < N) | 282 if (address.size() < N) |
| 202 return false; | 283 return false; |
| 203 return std::equal(prefix, prefix + N, address.bytes().begin()); | 284 return std::equal(prefix, prefix + N, address.bytes().begin()); |
| 204 } | 285 } |
| 205 | 286 |
| 206 } // namespace net | 287 } // namespace net |
| 207 | 288 |
| 208 #endif // NET_BASE_IP_ADDRESS_H_ | 289 #endif // NET_BASE_IP_ADDRESS_H_ |
| OLD | NEW |