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" |
16 #include "base/containers/stack_container.h" | |
15 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
16 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
17 | 19 |
18 namespace net { | 20 namespace net { |
19 | 21 |
20 class NET_EXPORT IPAddress { | 22 class NET_EXPORT IPAddress { |
21 public: | 23 public: |
22 enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 }; | 24 enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 }; |
23 | 25 |
26 // Helper class to represent the sequence of bytes in an IP address. | |
27 // A vector<uint8_t> would be simpler but incurs heap allocation, so | |
28 // IPAddressBytes uses a fixed size array. | |
29 class IPAddressBytes { | |
30 public: | |
31 IPAddressBytes(); | |
32 IPAddressBytes(const uint8_t* data, size_t data_len); | |
33 IPAddressBytes(const IPAddressBytes& other); | |
34 ~IPAddressBytes(); | |
35 | |
36 // Returns the number of elements in the underlying array. | |
37 size_t size() const { return size_; } | |
38 | |
39 // Sets the size to be |size|. Does not actually change the size | |
40 // 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.
| |
41 void Resize(size_t size) { | |
42 DCHECK_LE(size, 16u); | |
43 size_ = static_cast<uint8_t>(size); | |
44 } | |
45 | |
46 // Returns true if the underlying array is empty. | |
47 bool empty() const { return size_ == 0; } | |
48 | |
49 // Returns a pointer to the underlying array of bytes. | |
50 const uint8_t* data() const { return bytes_.data(); } | |
51 uint8_t* data() { return bytes_.data(); } | |
52 | |
53 // Returns a pointer to the first element. | |
54 const uint8_t* begin() const { return data(); } | |
55 | |
56 // Returns a pointer past the last element. | |
57 const uint8_t* end() const { 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, kIPv6AddressSize> bytes_; | |
92 | |
93 // Number of elements in |bytes_|. Should be either kIPv4AddressSize | |
94 // or kIPv6AddressSize or 0. | |
95 uint8_t size_; | |
96 }; | |
97 | |
24 // Creates a zero-sized, invalid address. | 98 // Creates a zero-sized, invalid address. |
25 IPAddress(); | 99 IPAddress(); |
26 | 100 |
27 // Copies the input address to |ip_address_|. The input is expected to be in | 101 // Copies the input address to |ip_address_|. The input is expected to be in |
28 // network byte order. | 102 // network byte order. |
29 explicit IPAddress(const std::vector<uint8_t>& address); | 103 explicit IPAddress(const std::vector<uint8_t>& address); |
30 | 104 |
105 // Copies the input address to |ip_address_|. The input is expected to be in | |
106 // network byte order. | |
107 explicit IPAddress(const base::StackVector<uint8_t, 16>& address); | |
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 |
34 // network byte order. | 112 // network byte order. |
35 template <size_t N> | 113 template <size_t N> |
36 IPAddress(const uint8_t(&address)[N]) | 114 IPAddress(const uint8_t(&address)[N]) |
37 : IPAddress(address, N) {} | 115 : IPAddress(address, N) {} |
38 | 116 |
39 // Copies the input address to |ip_address_| taking an additional length | 117 // Copies the input address to |ip_address_| taking an additional length |
40 // parameter. The input is expected to be in network byte order. | 118 // 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. | 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. Since this allocates a new | |
187 // 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.
| |
188 std::vector<uint8_t> BytesAsVector() const; | |
107 | 189 |
108 // Returns an IPAddress instance representing the 127.0.0.1 address. | 190 // Returns an IPAddress instance representing the 127.0.0.1 address. |
109 static IPAddress IPv4Localhost(); | 191 static IPAddress IPv4Localhost(); |
110 | 192 |
111 // Returns an IPAddress instance representing the ::1 address. | 193 // Returns an IPAddress instance representing the ::1 address. |
112 static IPAddress IPv6Localhost(); | 194 static IPAddress IPv6Localhost(); |
113 | 195 |
114 // Returns an IPAddress made up of |num_zero_bytes| zeros. | 196 // Returns an IPAddress made up of |num_zero_bytes| zeros. |
115 static IPAddress AllZeros(size_t num_zero_bytes); | 197 static IPAddress AllZeros(size_t num_zero_bytes); |
116 | 198 |
117 // Returns an IPAddress instance representing the 0.0.0.0 address. | 199 // Returns an IPAddress instance representing the 0.0.0.0 address. |
118 static IPAddress IPv4AllZeros(); | 200 static IPAddress IPv4AllZeros(); |
119 | 201 |
120 // Returns an IPAddress instance representing the :: address. | 202 // Returns an IPAddress instance representing the :: address. |
121 static IPAddress IPv6AllZeros(); | 203 static IPAddress IPv6AllZeros(); |
122 | 204 |
123 bool operator==(const IPAddress& that) const; | 205 bool operator==(const IPAddress& that) const; |
124 bool operator!=(const IPAddress& that) const; | 206 bool operator!=(const IPAddress& that) const; |
125 bool operator<(const IPAddress& that) const; | 207 bool operator<(const IPAddress& that) const; |
126 | 208 |
127 private: | 209 private: |
128 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address | 210 IPAddressBytes ip_address_; |
129 // will have length kIPv6AddressSize. | |
130 std::vector<uint8_t> ip_address_; | |
131 | 211 |
132 // This class is copyable and assignable. | 212 // This class is copyable and assignable. |
133 }; | 213 }; |
134 | 214 |
135 using IPAddressList = std::vector<IPAddress>; | 215 using IPAddressList = std::vector<IPAddress>; |
136 | 216 |
137 // Returns the canonical string representation of an IP address along with its | 217 // Returns the canonical string representation of an IP address along with its |
138 // port. For example: "192.168.0.1:99" or "[::1]:80". | 218 // port. For example: "192.168.0.1:99" or "[::1]:80". |
139 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, | 219 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, |
140 uint16_t port); | 220 uint16_t port); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 template <size_t N> | 279 template <size_t N> |
200 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { | 280 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { |
201 if (address.size() < N) | 281 if (address.size() < N) |
202 return false; | 282 return false; |
203 return std::equal(prefix, prefix + N, address.bytes().begin()); | 283 return std::equal(prefix, prefix + N, address.bytes().begin()); |
204 } | 284 } |
205 | 285 |
206 } // namespace net | 286 } // namespace net |
207 | 287 |
208 #endif // NET_BASE_IP_ADDRESS_H_ | 288 #endif // NET_BASE_IP_ADDRESS_H_ |
OLD | NEW |