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