Chromium Code Reviews| Index: net/base/ip_address.h |
| diff --git a/net/base/ip_address.h b/net/base/ip_address.h |
| index e326e78da6e08a75f97fc4647c2c7252d7bd321a..aeb53821d9134cf23694f291f16976eba847329f 100644 |
| --- a/net/base/ip_address.h |
| +++ b/net/base/ip_address.h |
| @@ -8,10 +8,12 @@ |
| #include <stddef.h> |
| #include <stdint.h> |
| +#include <array> |
| #include <string> |
| #include <vector> |
| #include "base/compiler_specific.h" |
| +#include "base/containers/stack_container.h" |
| #include "base/strings/string_piece.h" |
| #include "net/base/net_export.h" |
| @@ -21,6 +23,78 @@ class NET_EXPORT IPAddress { |
| public: |
| enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 }; |
| + // Helper class to represent the sequence of bytes in an IP address. |
| + // A vector<uint8_t> would be simpler but incurs heap allocation, so |
| + // IPAddressBytes uses a fixed size array. |
| + class IPAddressBytes { |
| + public: |
| + IPAddressBytes(); |
| + IPAddressBytes(const uint8_t* data, size_t data_len); |
| + IPAddressBytes(const IPAddressBytes& other); |
| + ~IPAddressBytes(); |
| + |
| + // Returns the number of elements in the underlying array. |
| + size_t size() const { return size_; } |
| + |
| + // Sets the size to be |size|. Does not actually change the size |
| + // of the underlying array or zero-initialze the bytes. |
|
eroman
2017/05/19 17:37:54
typo: initialize
Ryan Hamilton
2017/05/19 21:30:46
Done.
|
| + void Resize(size_t size) { |
| + DCHECK_LE(size, 16u); |
| + size_ = static_cast<uint8_t>(size); |
| + } |
| + |
| + // Returns true if the underlying array is empty. |
| + bool empty() const { return size_ == 0; } |
| + |
| + // Returns a pointer to the underlying array of bytes. |
| + const uint8_t* data() const { return bytes_.data(); } |
| + uint8_t* data() { return bytes_.data(); } |
| + |
| + // Returns a pointer to the first element. |
| + const uint8_t* begin() const { return data(); } |
| + |
| + // Returns a pointer past the last element. |
| + const uint8_t* end() const { return data() + size_; } |
| + |
| + // Returns a reference to the last element. |
| + uint8_t& back() { |
| + DCHECK(!empty()); |
| + return bytes_[size_ - 1]; |
| + } |
| + const uint8_t& back() const { |
| + DCHECK(!empty()); |
| + return bytes_[size_ - 1]; |
| + } |
| + |
| + // Appends |val| to the end and increments the size. |
| + void push_back(uint8_t val) { |
| + DCHECK_GT(16, size_); |
| + bytes_[size_++] = val; |
| + } |
| + |
| + // Returns a reference to the byte at index |pos|. |
| + uint8_t& operator[](size_t pos) { |
| + DCHECK_LT(pos, size_); |
| + return bytes_[pos]; |
| + } |
| + const uint8_t& operator[](size_t pos) const { |
| + DCHECK_LT(pos, size_); |
| + return bytes_[pos]; |
| + } |
| + |
| + bool operator<(const IPAddressBytes& other) const; |
| + bool operator!=(const IPAddressBytes& other) const; |
| + bool operator==(const IPAddressBytes& other) const; |
| + |
| + private: |
| + // Underlying sequence of bytes |
| + std::array<uint8_t, kIPv6AddressSize> bytes_; |
| + |
| + // Number of elements in |bytes_|. Should be either kIPv4AddressSize |
| + // or kIPv6AddressSize or 0. |
| + uint8_t size_; |
| + }; |
| + |
| // Creates a zero-sized, invalid address. |
| IPAddress(); |
| @@ -28,6 +102,10 @@ class NET_EXPORT IPAddress { |
| // network byte order. |
| explicit IPAddress(const std::vector<uint8_t>& address); |
| + // Copies the input address to |ip_address_|. The input is expected to be in |
| + // network byte order. |
| + explicit IPAddress(const base::StackVector<uint8_t, 16>& address); |
| + |
| IPAddress(const IPAddress& other); |
| // Copies the input address to |ip_address_|. The input is expected to be in |
| @@ -102,8 +180,12 @@ class NET_EXPORT IPAddress { |
| bool AssignFromIPLiteral(const base::StringPiece& ip_literal) |
| WARN_UNUSED_RESULT; |
| - // Returns the underlying byte vector. |
| - const std::vector<uint8_t>& bytes() const { return ip_address_; }; |
| + // Returns the underlying bytes. |
| + const IPAddressBytes& bytes() const { return ip_address_; }; |
| + |
| + // Returns the underlying bytes as a vector. Since this allocates a new |
| + // vector, this method should be avoided unless absolutely necessary. |
|
eroman
2017/05/19 17:37:54
optional: "Copies the bytes to a new vector. Gener
Ryan Hamilton
2017/05/19 21:30:46
Done.
|
| + std::vector<uint8_t> BytesAsVector() const; |
| // Returns an IPAddress instance representing the 127.0.0.1 address. |
| static IPAddress IPv4Localhost(); |
| @@ -125,9 +207,7 @@ class NET_EXPORT IPAddress { |
| bool operator<(const IPAddress& that) const; |
| private: |
| - // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address |
| - // will have length kIPv6AddressSize. |
| - std::vector<uint8_t> ip_address_; |
| + IPAddressBytes ip_address_; |
| // This class is copyable and assignable. |
| }; |