forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipv6_address.hpp
More file actions
245 lines (208 loc) · 7.11 KB
/
Copy pathipv6_address.hpp
File metadata and controls
245 lines (208 loc) · 7.11 KB
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_NET_IPV6_ADDRESS_HPP_INCLUDED
#define CPPCORO_NET_IPV6_ADDRESS_HPP_INCLUDED
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
namespace cppcoro::net
{
class ipv4_address;
class ipv6_address
{
using bytes_t = std::uint8_t[16];
public:
constexpr ipv6_address();
explicit constexpr ipv6_address(
std::uint64_t subnetPrefix,
std::uint64_t interfaceIdentifier);
constexpr ipv6_address(
std::uint16_t part0,
std::uint16_t part1,
std::uint16_t part2,
std::uint16_t part3,
std::uint16_t part4,
std::uint16_t part5,
std::uint16_t part6,
std::uint16_t part7);
explicit constexpr ipv6_address(
const std::uint16_t(&parts)[8]);
explicit constexpr ipv6_address(
const std::uint8_t(&bytes)[16]);
constexpr const bytes_t& bytes() const { return m_bytes; }
constexpr std::uint64_t subnet_prefix() const;
constexpr std::uint64_t interface_identifier() const;
/// Get the IPv6 unspedified address :: (all zeroes).
static constexpr ipv6_address unspecified();
/// Get the IPv6 loopback address ::1.
static constexpr ipv6_address loopback();
/// Parse a string representation of an IPv6 address.
///
/// \param string
/// The string to parse.
/// Must be in ASCII, UTF-8 or Latin-1 encoding.
///
/// \return
/// The IP address if successful, otherwise std::nullopt if the string
/// could not be parsed as an IPv4 address.
static std::optional<ipv6_address> from_string(std::string_view string) noexcept;
/// Convert the IP address to contracted string form.
///
/// Address is broken up into 16-bit parts, with each part represended in 1-4
/// lower-case hexadecimal with leading zeroes omitted. Parts are separated
/// by separated by a ':'. The longest contiguous run of zero parts is contracted
/// to "::".
///
/// For example:
/// ipv6_address::unspecified() -> "::"
/// ipv6_address::loopback() -> "::1"
/// ipv6_address(0x0011223344556677, 0x8899aabbccddeeff) ->
/// "11:2233:4455:6677:8899:aabb:ccdd:eeff"
/// ipv6_address(0x0102030400000000, 0x003fc447ab991011) ->
/// "102:304::3f:c447:ab99:1011"
std::string to_string() const;
constexpr bool operator==(const ipv6_address& other) const;
constexpr bool operator!=(const ipv6_address& other) const;
constexpr bool operator<(const ipv6_address& other) const;
constexpr bool operator>(const ipv6_address& other) const;
constexpr bool operator<=(const ipv6_address& other) const;
constexpr bool operator>=(const ipv6_address& other) const;
private:
alignas(std::uint64_t) std::uint8_t m_bytes[16];
};
constexpr ipv6_address::ipv6_address()
: m_bytes{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0 }
{}
constexpr ipv6_address::ipv6_address(
std::uint64_t subnetPrefix,
std::uint64_t interfaceIdentifier)
: m_bytes{
static_cast<std::uint8_t>(subnetPrefix >> 56),
static_cast<std::uint8_t>(subnetPrefix >> 48),
static_cast<std::uint8_t>(subnetPrefix >> 40),
static_cast<std::uint8_t>(subnetPrefix >> 32),
static_cast<std::uint8_t>(subnetPrefix >> 24),
static_cast<std::uint8_t>(subnetPrefix >> 16),
static_cast<std::uint8_t>(subnetPrefix >> 8),
static_cast<std::uint8_t>(subnetPrefix),
static_cast<std::uint8_t>(interfaceIdentifier >> 56),
static_cast<std::uint8_t>(interfaceIdentifier >> 48),
static_cast<std::uint8_t>(interfaceIdentifier >> 40),
static_cast<std::uint8_t>(interfaceIdentifier >> 32),
static_cast<std::uint8_t>(interfaceIdentifier >> 24),
static_cast<std::uint8_t>(interfaceIdentifier >> 16),
static_cast<std::uint8_t>(interfaceIdentifier >> 8),
static_cast<std::uint8_t>(interfaceIdentifier) }
{}
constexpr ipv6_address::ipv6_address(
std::uint16_t part0,
std::uint16_t part1,
std::uint16_t part2,
std::uint16_t part3,
std::uint16_t part4,
std::uint16_t part5,
std::uint16_t part6,
std::uint16_t part7)
: m_bytes{
static_cast<std::uint8_t>(part0 >> 8),
static_cast<std::uint8_t>(part0),
static_cast<std::uint8_t>(part1 >> 8),
static_cast<std::uint8_t>(part1),
static_cast<std::uint8_t>(part2 >> 8),
static_cast<std::uint8_t>(part2),
static_cast<std::uint8_t>(part3 >> 8),
static_cast<std::uint8_t>(part3),
static_cast<std::uint8_t>(part4 >> 8),
static_cast<std::uint8_t>(part4),
static_cast<std::uint8_t>(part5 >> 8),
static_cast<std::uint8_t>(part5),
static_cast<std::uint8_t>(part6 >> 8),
static_cast<std::uint8_t>(part6),
static_cast<std::uint8_t>(part7 >> 8),
static_cast<std::uint8_t>(part7) }
{}
constexpr ipv6_address::ipv6_address(
const std::uint16_t(&parts)[8])
: ipv6_address(
parts[0], parts[1], parts[2], parts[3],
parts[4], parts[5], parts[6], parts[7])
{}
constexpr ipv6_address::ipv6_address(const std::uint8_t(&bytes)[16])
: m_bytes{
bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11],
bytes[12], bytes[13], bytes[14], bytes[15] }
{}
constexpr std::uint64_t ipv6_address::subnet_prefix() const
{
return
static_cast<std::uint64_t>(m_bytes[0]) << 56 |
static_cast<std::uint64_t>(m_bytes[1]) << 48 |
static_cast<std::uint64_t>(m_bytes[2]) << 40 |
static_cast<std::uint64_t>(m_bytes[3]) << 32 |
static_cast<std::uint64_t>(m_bytes[4]) << 24 |
static_cast<std::uint64_t>(m_bytes[5]) << 16 |
static_cast<std::uint64_t>(m_bytes[6]) << 8 |
static_cast<std::uint64_t>(m_bytes[7]);
}
constexpr std::uint64_t ipv6_address::interface_identifier() const
{
return
static_cast<std::uint64_t>(m_bytes[8]) << 56 |
static_cast<std::uint64_t>(m_bytes[9]) << 48 |
static_cast<std::uint64_t>(m_bytes[10]) << 40 |
static_cast<std::uint64_t>(m_bytes[11]) << 32 |
static_cast<std::uint64_t>(m_bytes[12]) << 24 |
static_cast<std::uint64_t>(m_bytes[13]) << 16 |
static_cast<std::uint64_t>(m_bytes[14]) << 8 |
static_cast<std::uint64_t>(m_bytes[15]);
}
constexpr ipv6_address ipv6_address::unspecified()
{
return ipv6_address{};
}
constexpr ipv6_address ipv6_address::loopback()
{
return ipv6_address{ 0, 0, 0, 0, 0, 0, 0, 1 };
}
constexpr bool ipv6_address::operator==(const ipv6_address& other) const
{
for (int i = 0; i < 16; ++i)
{
if (m_bytes[i] != other.m_bytes[i]) return false;
}
return true;
}
constexpr bool ipv6_address::operator!=(const ipv6_address& other) const
{
return !(*this == other);
}
constexpr bool ipv6_address::operator<(const ipv6_address& other) const
{
for (int i = 0; i < 16; ++i)
{
if (m_bytes[i] != other.m_bytes[i])
return m_bytes[i] < other.m_bytes[i];
}
return false;
}
constexpr bool ipv6_address::operator>(const ipv6_address& other) const
{
return (other < *this);
}
constexpr bool ipv6_address::operator<=(const ipv6_address& other) const
{
return !(other < *this);
}
constexpr bool ipv6_address::operator>=(const ipv6_address& other) const
{
return !(*this < other);
}
}
#endif