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. Zeros out all elements in [0, |size|). | |
eroman
2017/05/17 20:48:16
This is not the same way that std::vector<>::resiz
Ryan Hamilton
2017/05/17 23:32:37
Ah, ok. Renamed to Resize().
| |
40 void resize(size_t size) { | |
41 DCHECK_LE(size, 16u); | |
42 if (size != 0) | |
43 memset(bytes_.data(), 0, size); | |
44 size_ = static_cast<uint8_t>(size); | |
45 } | |
46 | |
47 // Returns true if the underlying array is empty. | |
48 bool empty() const { return size_ == 0; } | |
49 | |
50 // Returns a pointer to the underlying array of bytes. | |
51 const uint8_t* data() const { return bytes_.data(); } | |
52 uint8_t* data() { return bytes_.data(); } | |
53 | |
54 // Returns a pointer to the first element. | |
55 const uint8_t* begin() const { return data(); } | |
56 | |
57 // Returns a pointer past the last element. | |
58 const uint8_t* end() const { return data() + size_; } | |
59 | |
60 // Returns a reference to the last element. | |
61 uint8_t& back() { | |
62 DCHECK(!empty()); | |
63 return bytes_[size_ - 1]; | |
64 } | |
65 const uint8_t& back() const { | |
66 DCHECK(!empty()); | |
67 return bytes_[size_ - 1]; | |
68 } | |
69 | |
70 // Appends |val| to the end and increments the size. | |
71 void push_back(uint8_t val) { | |
72 DCHECK_GT(16, size_); | |
73 bytes_[size_++] = val; | |
74 } | |
75 | |
76 // Returns a reference to the byte at index |pos|. | |
77 uint8_t& operator[](size_t pos) { | |
78 DCHECK_LT(pos, size_); | |
79 return bytes_[pos]; | |
80 } | |
81 const uint8_t& operator[](size_t pos) const { | |
82 DCHECK_LT(pos, size_); | |
83 return bytes_[pos]; | |
84 } | |
85 | |
86 private: | |
87 friend bool operator>(const IPAddressBytes& lhs, const IPAddressBytes& rhs); | |
88 friend bool operator<(const IPAddressBytes& lhs, const IPAddressBytes& rhs); | |
89 friend bool operator!=(const IPAddressBytes& lhs, | |
90 const IPAddressBytes& rhs); | |
91 friend bool operator==(const IPAddressBytes& lhs, | |
92 const IPAddressBytes& rhs); | |
93 | |
94 // Underlying sequence of bytes | |
95 std::array<uint8_t, kIPv6AddressSize> bytes_; | |
96 | |
97 // Number of elements in |bytes_|. Should be either kIPv4AddressSize | |
98 // or kIPv6AddressSize or 0. | |
99 uint8_t size_; | |
100 }; | |
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 // Copies the input address to |ip_address_|. The input is expected to be in |
28 // network byte order. | 106 // network byte order. |
29 explicit IPAddress(const std::vector<uint8_t>& address); | 107 explicit IPAddress(const std::vector<uint8_t>& address); |
30 | 108 |
31 IPAddress(const IPAddress& other); | 109 IPAddress(const IPAddress& other); |
32 | 110 |
33 // Copies the input address to |ip_address_|. The input is expected to be in | 111 // Copies the input address to |ip_address_|. The input is expected to be in |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 // Returns the canonical string representation of an IP address. | 173 // Returns the canonical string representation of an IP address. |
96 // For example: "192.168.0.1" or "::1". Returns the empty string when | 174 // For example: "192.168.0.1" or "::1". Returns the empty string when |
97 // |ip_address_| is invalid. | 175 // |ip_address_| is invalid. |
98 std::string ToString() const; | 176 std::string ToString() const; |
99 | 177 |
100 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. | 178 // 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. | 179 // Returns true on success and fills |ip_address_| with the numeric value. |
102 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) | 180 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) |
103 WARN_UNUSED_RESULT; | 181 WARN_UNUSED_RESULT; |
104 | 182 |
105 // Returns the underlying byte vector. | 183 // Returns the underlying bytes. |
106 const std::vector<uint8_t>& bytes() const { return ip_address_; }; | 184 const IPAddressBytes& bytes() const { return ip_address_; }; |
185 | |
186 // Returns the underlying bytes as a vector. | |
187 std::vector<uint8_t> BytesAsVector() const; | |
107 | 188 |
108 // Returns an IPAddress instance representing the 127.0.0.1 address. | 189 // Returns an IPAddress instance representing the 127.0.0.1 address. |
109 static IPAddress IPv4Localhost(); | 190 static IPAddress IPv4Localhost(); |
110 | 191 |
111 // Returns an IPAddress instance representing the ::1 address. | 192 // Returns an IPAddress instance representing the ::1 address. |
112 static IPAddress IPv6Localhost(); | 193 static IPAddress IPv6Localhost(); |
113 | 194 |
114 // Returns an IPAddress made up of |num_zero_bytes| zeros. | 195 // Returns an IPAddress made up of |num_zero_bytes| zeros. |
115 static IPAddress AllZeros(size_t num_zero_bytes); | 196 static IPAddress AllZeros(size_t num_zero_bytes); |
116 | 197 |
117 // Returns an IPAddress instance representing the 0.0.0.0 address. | 198 // Returns an IPAddress instance representing the 0.0.0.0 address. |
118 static IPAddress IPv4AllZeros(); | 199 static IPAddress IPv4AllZeros(); |
119 | 200 |
120 // Returns an IPAddress instance representing the :: address. | 201 // Returns an IPAddress instance representing the :: address. |
121 static IPAddress IPv6AllZeros(); | 202 static IPAddress IPv6AllZeros(); |
122 | 203 |
123 bool operator==(const IPAddress& that) const; | 204 bool operator==(const IPAddress& that) const; |
124 bool operator!=(const IPAddress& that) const; | 205 bool operator!=(const IPAddress& that) const; |
125 bool operator<(const IPAddress& that) const; | 206 bool operator<(const IPAddress& that) const; |
126 | 207 |
127 private: | 208 private: |
128 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address | 209 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address |
129 // will have length kIPv6AddressSize. | 210 // will have length kIPv6AddressSize. |
130 std::vector<uint8_t> ip_address_; | 211 IPAddressBytes ip_address_; |
131 | 212 |
132 // This class is copyable and assignable. | 213 // This class is copyable and assignable. |
133 }; | 214 }; |
134 | 215 |
216 bool operator>(const IPAddress::IPAddressBytes& lhs, | |
217 const IPAddress::IPAddressBytes& rhs); | |
218 bool operator<(const IPAddress::IPAddressBytes& lhs, | |
219 const IPAddress::IPAddressBytes& rhs); | |
220 bool operator!=(const IPAddress::IPAddressBytes& lhs, | |
221 const IPAddress::IPAddressBytes& rhs); | |
222 bool operator==(const IPAddress::IPAddressBytes& lhs, | |
223 const IPAddress::IPAddressBytes& rhs); | |
224 | |
135 using IPAddressList = std::vector<IPAddress>; | 225 using IPAddressList = std::vector<IPAddress>; |
136 | 226 |
137 // Returns the canonical string representation of an IP address along with its | 227 // Returns the canonical string representation of an IP address along with its |
138 // port. For example: "192.168.0.1:99" or "[::1]:80". | 228 // port. For example: "192.168.0.1:99" or "[::1]:80". |
139 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, | 229 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, |
140 uint16_t port); | 230 uint16_t port); |
141 | 231 |
142 // Returns the address as a sequence of bytes in network-byte-order. | 232 // Returns the address as a sequence of bytes in network-byte-order. |
143 NET_EXPORT std::string IPAddressToPackedString(const IPAddress& address); | 233 NET_EXPORT std::string IPAddressToPackedString(const IPAddress& address); |
144 | 234 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 template <size_t N> | 289 template <size_t N> |
200 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { | 290 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { |
201 if (address.size() < N) | 291 if (address.size() < N) |
202 return false; | 292 return false; |
203 return std::equal(prefix, prefix + N, address.bytes().begin()); | 293 return std::equal(prefix, prefix + N, address.bytes().begin()); |
204 } | 294 } |
205 | 295 |
206 } // namespace net | 296 } // namespace net |
207 | 297 |
208 #endif // NET_BASE_IP_ADDRESS_H_ | 298 #endif // NET_BASE_IP_ADDRESS_H_ |
OLD | NEW |