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 IPAddressBytes { |
| 25 public: |
| 26 IPAddressBytes(); |
| 27 IPAddressBytes(const uint8_t* data, size_t data_len); |
| 28 IPAddressBytes(const IPAddressBytes& other); |
| 29 ~IPAddressBytes(); |
| 30 |
| 31 // Copys |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 | |
28 // network byte order. | |
29 explicit IPAddress(const std::vector<uint8_t>& address); | |
30 | |
31 IPAddress(const IPAddress& other); | 105 IPAddress(const IPAddress& other); |
32 | 106 |
33 // Copies the input address to |ip_address_|. The input is expected to be in | 107 // Copies the input address to |ip_address_|. The input is expected to be in |
34 // network byte order. | 108 // network byte order. |
35 template <size_t N> | 109 template <size_t N> |
36 IPAddress(const uint8_t(&address)[N]) | 110 IPAddress(const uint8_t(&address)[N]) |
37 : IPAddress(address, N) {} | 111 : IPAddress(address, N) {} |
38 | 112 |
39 // Copies the input address to |ip_address_| taking an additional length | 113 // Copies the input address to |ip_address_| taking an additional length |
40 // parameter. The input is expected to be in network byte order. | 114 // parameter. The input is expected to be in network byte order. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // Returns the canonical string representation of an IP address. | 169 // Returns the canonical string representation of an IP address. |
96 // For example: "192.168.0.1" or "::1". Returns the empty string when | 170 // For example: "192.168.0.1" or "::1". Returns the empty string when |
97 // |ip_address_| is invalid. | 171 // |ip_address_| is invalid. |
98 std::string ToString() const; | 172 std::string ToString() const; |
99 | 173 |
100 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. | 174 // 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. | 175 // Returns true on success and fills |ip_address_| with the numeric value. |
102 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) | 176 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) |
103 WARN_UNUSED_RESULT; | 177 WARN_UNUSED_RESULT; |
104 | 178 |
105 // Returns the underlying byte vector. | 179 // Returns the underlying bytes. |
106 const std::vector<uint8_t>& bytes() const { return ip_address_; }; | 180 const IPAddressBytes& bytes() const { return ip_address_; }; |
| 181 |
| 182 // Copies the bytes to a new vector. Generally callers should be using |
| 183 // |bytes()| and the IPAddressBytes abstraction. This method is provided as a |
| 184 // convenience for call sites that existed prior to the introduction of |
| 185 // IPAddressBytes. |
| 186 std::vector<uint8_t> CopyBytesToVector() const; |
107 | 187 |
108 // Returns an IPAddress instance representing the 127.0.0.1 address. | 188 // Returns an IPAddress instance representing the 127.0.0.1 address. |
109 static IPAddress IPv4Localhost(); | 189 static IPAddress IPv4Localhost(); |
110 | 190 |
111 // Returns an IPAddress instance representing the ::1 address. | 191 // Returns an IPAddress instance representing the ::1 address. |
112 static IPAddress IPv6Localhost(); | 192 static IPAddress IPv6Localhost(); |
113 | 193 |
114 // Returns an IPAddress made up of |num_zero_bytes| zeros. | 194 // Returns an IPAddress made up of |num_zero_bytes| zeros. |
115 static IPAddress AllZeros(size_t num_zero_bytes); | 195 static IPAddress AllZeros(size_t num_zero_bytes); |
116 | 196 |
117 // Returns an IPAddress instance representing the 0.0.0.0 address. | 197 // Returns an IPAddress instance representing the 0.0.0.0 address. |
118 static IPAddress IPv4AllZeros(); | 198 static IPAddress IPv4AllZeros(); |
119 | 199 |
120 // Returns an IPAddress instance representing the :: address. | 200 // Returns an IPAddress instance representing the :: address. |
121 static IPAddress IPv6AllZeros(); | 201 static IPAddress IPv6AllZeros(); |
122 | 202 |
123 bool operator==(const IPAddress& that) const; | 203 bool operator==(const IPAddress& that) const; |
124 bool operator!=(const IPAddress& that) const; | 204 bool operator!=(const IPAddress& that) const; |
125 bool operator<(const IPAddress& that) const; | 205 bool operator<(const IPAddress& that) const; |
126 | 206 |
127 private: | 207 private: |
128 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address | 208 IPAddressBytes ip_address_; |
129 // will have length kIPv6AddressSize. | |
130 std::vector<uint8_t> ip_address_; | |
131 | 209 |
132 // This class is copyable and assignable. | 210 // This class is copyable and assignable. |
133 }; | 211 }; |
134 | 212 |
135 using IPAddressList = std::vector<IPAddress>; | 213 using IPAddressList = std::vector<IPAddress>; |
136 | 214 |
137 // Returns the canonical string representation of an IP address along with its | 215 // Returns the canonical string representation of an IP address along with its |
138 // port. For example: "192.168.0.1:99" or "[::1]:80". | 216 // port. For example: "192.168.0.1:99" or "[::1]:80". |
139 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, | 217 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, |
140 uint16_t port); | 218 uint16_t port); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 template <size_t N> | 277 template <size_t N> |
200 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { | 278 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { |
201 if (address.size() < N) | 279 if (address.size() < N) |
202 return false; | 280 return false; |
203 return std::equal(prefix, prefix + N, address.bytes().begin()); | 281 return std::equal(prefix, prefix + N, address.bytes().begin()); |
204 } | 282 } |
205 | 283 |
206 } // namespace net | 284 } // namespace net |
207 | 285 |
208 #endif // NET_BASE_IP_ADDRESS_H_ | 286 #endif // NET_BASE_IP_ADDRESS_H_ |
OLD | NEW |