-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipv6_address.hpp
302 lines (255 loc) · 10.1 KB
/
ipv6_address.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#pragma once
#include "fly/fly.hpp"
#include "fly/types/string/format.hpp"
#include "fly/types/string/lexer.hpp"
#include <algorithm>
#include <array>
#include <compare>
#include <cstdint>
#include <limits>
#include <optional>
#include <string>
namespace fly::net {
/**
* Class to store an IPv6 address in an array, and to provide convenient access to its data as
* required by various network APIs.
*
* @author Timothy Flynn ([email protected])
* @version February 13, 2021
*/
class IPv6Address
{
public:
using address_type = std::array<std::uint8_t, 16>;
/**
* Default constructor. Initializes the IPv6 address to ::.
*/
IPv6Address() = default;
/**
* Constructor. Create an IPv6 address from a 16-part array of octets. The array should be
* ordered such that index 0 is the first octet and index 15 is the sixteenth octet.
*
* @param address The 16-part array of octets to initialize the IPv6 address from.
*/
explicit constexpr IPv6Address(address_type const &address) noexcept;
/**
* Constructor. Create an IPv6 address from a 16-part array of octets. The array should be
* ordered such that index 0 is the first octet and index 15 is the sixteenth octet.
*
* @param address The 16-part array of octets to initialize the IPv6 address from.
*/
explicit constexpr IPv6Address(address_type &&address) noexcept;
/**
* Constructor. Create an IPv6 address from a 16-part array of octets. The array should be
* ordered such that index 0 is the first octet and index 15 is the sixteenth octet.
*
* @param address The 16-part array of octets to initialize the IPv6 address from.
*/
explicit constexpr IPv6Address(address_type::value_type const (&address)[16]) noexcept;
IPv6Address(IPv6Address const &) = default;
IPv6Address(IPv6Address &&) = default;
IPv6Address &operator=(IPv6Address const &) = default;
IPv6Address &operator=(IPv6Address &&) = default;
/**
* @return An IPv6 address representing IN6ADDR_ANY.
*/
static constexpr IPv6Address in_addr_any();
/**
* @return An IPv6 address representing IN6ADDR_LOOPBACK.
*/
static constexpr IPv6Address in_addr_loopback();
/**
* Construct an IPv6 address from a string of hexadectets.
*
* The provided string must be fully formed or use shorthand form. In shorthand form, each
* hexadectet may have leading zeros removed (that is, the hexadectet "001a" may be provided as
* just "1a"). Further, consecutive hexadectets of zeros may be replaced with two colons (that
* is, the hexadectets "1:0:0:1" may be provided as "1::1"); this replacement may only be used
* once in the string.
*
* @param address The string to initialize the IPv6 address from.
*
* @return If successful, the constructed IPv6 address. Otherwise, an uninitialized value.
*/
static constexpr std::optional<IPv6Address> from_string(std::string_view address);
/**
* Copy the IPv6 address into a 16-part array.
*
* @param address The 16-part array of octets to copy the IPv6 address into.
*/
constexpr void copy(address_type::value_type (&address)[16]) const;
#if defined(FLY_MACOS)
/**
* Comparison operators. Apple's Clang does not fully support the three-way comparison operator,
* so these must be manually defined.
*/
constexpr bool operator==(IPv6Address const &address) const;
constexpr bool operator!=(IPv6Address const &address) const;
constexpr bool operator<(IPv6Address const &address) const;
constexpr bool operator<=(IPv6Address const &address) const;
constexpr bool operator>(IPv6Address const &address) const;
constexpr bool operator>=(IPv6Address const &address) const;
#else
/**
* Three-way-comparison operator. Defaulted to perform the comparison on the IPv6 array data.
*/
auto operator<=>(IPv6Address const &) const = default;
#endif
private:
static constexpr std::size_t s_address_size = std::tuple_size_v<address_type>;
address_type m_address {};
};
//==================================================================================================
constexpr IPv6Address::IPv6Address(address_type const &address) noexcept :
m_address {address}
{
}
//==================================================================================================
constexpr IPv6Address::IPv6Address(address_type &&address) noexcept :
m_address {std::move(address)}
{
}
//==================================================================================================
constexpr IPv6Address::IPv6Address(address_type::value_type const (&address)[16]) noexcept
{
std::copy(std::begin(address), std::end(address), m_address.begin());
}
//==================================================================================================
constexpr IPv6Address IPv6Address::in_addr_any()
{
address_type address {};
return IPv6Address(std::move(address));
}
//==================================================================================================
constexpr IPv6Address IPv6Address::in_addr_loopback()
{
address_type address {};
address.back() = 0x01;
return IPv6Address(std::move(address));
}
//==================================================================================================
constexpr std::optional<IPv6Address> IPv6Address::from_string(std::string_view address)
{
constexpr auto const s_max16 =
static_cast<std::uint64_t>(std::numeric_limits<std::uint16_t>::max());
constexpr auto const s_colon = ':';
fly::Lexer lexer(std::move(address));
address_type parts {};
std::optional<std::size_t> index_after_short_form;
std::size_t index = 0;
do
{
if (lexer.consume_if(s_colon))
{
if (lexer.consume_if(s_colon))
{
if (index_after_short_form)
{
return std::nullopt;
}
index_after_short_form = index;
}
}
else if (auto const segment = lexer.consume_hex_number(); segment && (*segment <= s_max16))
{
parts[index++] = static_cast<address_type::value_type>((*segment >> 8) & 0xff);
parts[index++] = static_cast<address_type::value_type>(*segment & 0xff);
}
else
{
return std::nullopt;
}
} while (lexer.peek() && (index < s_address_size));
if (lexer.peek() || ((index != s_address_size) && !index_after_short_form))
{
return std::nullopt;
}
else if (index_after_short_form)
{
auto const start = static_cast<std::ptrdiff_t>(s_address_size - index);
auto const end = static_cast<std::ptrdiff_t>(*index_after_short_form);
std::rotate(parts.rbegin(), parts.rbegin() + start, parts.rend() - end);
}
return IPv6Address(std::move(parts));
}
//==================================================================================================
constexpr void IPv6Address::copy(address_type::value_type (&address)[16]) const
{
std::copy(m_address.begin(), m_address.end(), std::begin(address));
}
#if defined(FLY_MACOS)
//==================================================================================================
constexpr bool IPv6Address::operator==(IPv6Address const &address) const
{
return m_address == address.m_address;
}
//==================================================================================================
constexpr bool IPv6Address::operator!=(IPv6Address const &address) const
{
return m_address != address.m_address;
}
//==================================================================================================
constexpr bool IPv6Address::operator<(IPv6Address const &address) const
{
return m_address < address.m_address;
}
//==================================================================================================
constexpr bool IPv6Address::operator<=(IPv6Address const &address) const
{
return m_address <= address.m_address;
}
//==================================================================================================
constexpr bool IPv6Address::operator>(IPv6Address const &address) const
{
return m_address > address.m_address;
}
//==================================================================================================
constexpr bool IPv6Address::operator>=(IPv6Address const &address) const
{
return m_address >= address.m_address;
}
#endif
} // namespace fly::net
//==================================================================================================
template <>
struct fly::string::Formatter<fly::net::IPv6Address>
{
/**
* Format an IPv6 address as a string in shorthand form.
*
* @tparam FormatContext The type of the formatting context.
*
* @param address The IPv6 address to format.
* @param context The context holding the formatting state.
*/
template <typename FormatContext>
void format(fly::net::IPv6Address const &address, FormatContext &context)
{
static constexpr std::size_t s_address_size =
std::tuple_size_v<fly::net::IPv6Address::address_type>;
fly::net::IPv6Address::address_type::value_type parts[s_address_size] {};
address.copy(parts);
auto join_segments = [&parts](std::size_t index) -> std::uint16_t {
return (parts[index] << 8) | parts[index + 1];
};
bool used_short_form = false;
for (std::size_t i = 0; i < s_address_size;)
{
std::uint16_t const segment = join_segments(i);
if ((segment == 0) && !used_short_form)
{
do
{
i += 2;
} while ((i < s_address_size) && (join_segments(i) == 0));
fly::string::format_to(context.out(), "{}", (i < s_address_size) ? ":" : "::");
used_short_form = true;
}
else
{
fly::string::format_to(context.out(), "{:.{}}{:x}", ":", (i > 0) ? 1 : 0, segment);
i += 2;
}
}
}
};