-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipv4_address.hpp
235 lines (200 loc) · 7.18 KB
/
ipv4_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
#pragma once
#include "fly/types/numeric/endian.hpp"
#include "fly/types/string/format.hpp"
#include "fly/types/string/lexer.hpp"
#include <array>
#include <compare>
#include <cstdint>
#include <limits>
#include <optional>
#include <string>
namespace fly::net {
/**
* Class to store an IPv4 address in network order, and to provide convenient access to its data as
* required by various network APIs.
*
* @author Timothy Flynn ([email protected])
* @version February 13, 2021
*/
class IPv4Address
{
public:
using address_type = std::array<std::uint8_t, 4>;
using int_type = std::uint32_t;
/**
* Default constructor. Initializes the IPv4 address to 0.0.0.0.
*/
IPv4Address() = default;
/**
* Constructor. Create an IPv4 address from a 4-part array of octets in decimal format. The
* array should be ordered such that index 0 is the first octet and index 3 is the fourth octet.
*
* @param address The 4-part array of octets to initialize the IPv4 address from.
*/
explicit constexpr IPv4Address(address_type const &address) noexcept;
/**
* Constructor. Create an IPv4 address from a network-order 32-bit value.
*
* @param address The network-order address to initialize the IPv4 address from.
*/
explicit constexpr IPv4Address(int_type address) noexcept;
IPv4Address(IPv4Address const &) = default;
IPv4Address(IPv4Address &&) = default;
IPv4Address &operator=(IPv4Address const &) = default;
IPv4Address &operator=(IPv4Address &&) = default;
/**
* @return An IPv4 address representing INADDR_ANY.
*/
static constexpr IPv4Address in_addr_any();
/**
* @return An IPv4 address representing INADDR_BROADCAST.
*/
static constexpr IPv4Address in_addr_broadcast();
/**
* @return An IPv4 address representing INADDR_LOOPBACK.
*/
static constexpr IPv4Address in_addr_loopback();
/**
* Construct an IPv4 address from a string in dot-decimal notation.
*
* The provided string must betwen one and four octets, inclusive. If the string contains less
* than four octets, the last octet is treated as an integer of as many bytes as are required to
* fill out the address to four octets. Thus, the string "127.65530" is converted to the IPv4
* address 127.0.255.250.
*
* @param address The string in dot-decimal notation to initialize the IPv4 address from.
*
* @return If successful, the constructed IPv4 address. Otherwise, an uninitialized value.
*/
static constexpr std::optional<IPv4Address> from_string(std::string_view address);
/**
* @return The IPv4 address as an integer in network order.
*/
constexpr int_type network_order() const;
/**
* @return The IPv4 address as an integer in host order.
*/
constexpr int_type host_order() const;
/**
* Three-way-comparison operator. Defaulted to perform the comparison on the network-order IPv4
* address.
*/
auto operator<=>(IPv4Address const &) const = default;
private:
int_type m_address {0};
};
//==================================================================================================
constexpr IPv4Address::IPv4Address(address_type const &address) noexcept
{
m_address |= static_cast<int_type>(address[0]);
m_address |= static_cast<int_type>(address[1] << 8);
m_address |= static_cast<int_type>(address[2] << 16);
m_address |= static_cast<int_type>(address[3] << 24);
}
//==================================================================================================
constexpr IPv4Address::IPv4Address(int_type address) noexcept :
m_address(address)
{
}
//==================================================================================================
constexpr IPv4Address IPv4Address::in_addr_any()
{
return IPv4Address(0x00'00'00'00);
}
//==================================================================================================
constexpr IPv4Address IPv4Address::in_addr_broadcast()
{
return IPv4Address(0xff'ff'ff'ff);
}
//==================================================================================================
constexpr IPv4Address IPv4Address::in_addr_loopback()
{
return IPv4Address(0x01'00'00'7f);
}
//==================================================================================================
constexpr std::optional<IPv4Address> IPv4Address::from_string(std::string_view address)
{
constexpr auto const s_max32 = static_cast<std::uint64_t>(std::numeric_limits<int_type>::max());
constexpr auto const s_decimal = '.';
fly::Lexer lexer(std::move(address));
std::array<int_type, 4> parts {};
std::size_t index = 0;
do
{
if (auto const segment = lexer.consume_number(); segment && (*segment <= s_max32))
{
parts[index++] = static_cast<int_type>(*segment);
}
else
{
return std::nullopt;
}
} while ((index < parts.size()) && lexer.consume_if(s_decimal));
std::optional<int_type> host_address;
if (index == 1)
{
host_address = parts[0];
}
else if (index == 2)
{
if ((parts[0] <= 0xff) && (parts[1] <= 0xff'ff'ff))
{
host_address = (parts[0] << 24) | parts[1];
}
}
else if (index == 3)
{
if ((parts[0] <= 0xff) && (parts[1] <= 0xff) && (parts[2] <= 0xff'ff))
{
host_address = (parts[0] << 24) | (parts[1] << 16) | parts[2];
}
}
else if (index == 4)
{
if ((parts[0] <= 0xff) && (parts[1] <= 0xff) && (parts[2] <= 0xff) && (parts[3] <= 0xff))
{
host_address = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
}
}
if (!host_address || lexer.peek())
{
return std::nullopt;
}
return IPv4Address(fly::endian_swap_if_non_native<std::endian::big>(*host_address));
}
//==================================================================================================
constexpr auto IPv4Address::network_order() const -> int_type
{
return m_address;
}
//==================================================================================================
constexpr auto IPv4Address::host_order() const -> int_type
{
return fly::endian_swap_if_non_native<std::endian::big>(m_address);
}
} // namespace fly::net
//==================================================================================================
template <>
struct fly::string::Formatter<fly::net::IPv4Address>
{
/**
* Format an IPv4 address as a four octet string in dot-decimal notation.
*
* @tparam FormatContext The type of the formatting context.
*
* @param address The IPv4 address to format.
* @param context The context holding the formatting state.
*/
template <typename FormatContext>
void format(fly::net::IPv4Address const &address, FormatContext &context)
{
auto const network_order = address.network_order();
fly::string::format_to(
context.out(),
"{}.{}.{}.{}",
network_order & 0xff,
(network_order >> 8) & 0xff,
(network_order >> 16) & 0xff,
(network_order >> 24) & 0xff);
}
};