|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "fly/types/numeric/endian.hpp" |
| 4 | +#include "fly/types/string/string_lexer.hpp" |
| 5 | + |
| 6 | +#include <array> |
| 7 | +#include <compare> |
| 8 | +#include <cstdint> |
| 9 | +#include <limits> |
| 10 | +#include <optional> |
| 11 | +#include <ostream> |
| 12 | +#include <string> |
| 13 | + |
| 14 | +namespace fly::net { |
| 15 | + |
| 16 | +/** |
| 17 | + * Class to store an IPv4 address in network order, and to provide convenient access to its data as |
| 18 | + * required by various network APIs. |
| 19 | + * |
| 20 | + * @author Timothy Flynn ([email protected]) |
| 21 | + * @version February 13, 2021 |
| 22 | + */ |
| 23 | +class IPv4Address |
| 24 | +{ |
| 25 | +public: |
| 26 | + using address_type = std::array<std::uint8_t, 4>; |
| 27 | + using int_type = std::uint32_t; |
| 28 | + |
| 29 | + /** |
| 30 | + * Default constructor. Initializes the IPv4 address to 0.0.0.0. |
| 31 | + */ |
| 32 | + IPv4Address() = default; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructor. Create an IPv4 address from a 4-part array of octets in decimal format. The |
| 36 | + * array should be ordered such that index 0 is the first octet and index 3 is the fourth octet. |
| 37 | + * |
| 38 | + * @param address The 4-part array of octets to initialize the IPv4 address from. |
| 39 | + */ |
| 40 | + explicit constexpr IPv4Address(const address_type &address) noexcept; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor. Create an IPv4 address from a network-order 32-bit value. |
| 44 | + * |
| 45 | + * @param address The network-order address to initialize the IPv4 address from. |
| 46 | + */ |
| 47 | + explicit constexpr IPv4Address(int_type address) noexcept; |
| 48 | + |
| 49 | + IPv4Address(const IPv4Address &) = default; |
| 50 | + IPv4Address(IPv4Address &&) = default; |
| 51 | + |
| 52 | + IPv4Address &operator=(const IPv4Address &) = default; |
| 53 | + IPv4Address &operator=(IPv4Address &&) = default; |
| 54 | + |
| 55 | + /** |
| 56 | + * @return An IPv4 address representing INADDR_ANY. |
| 57 | + */ |
| 58 | + static constexpr IPv4Address in_addr_any(); |
| 59 | + |
| 60 | + /** |
| 61 | + * @return An IPv4 address representing INADDR_BROADCAST. |
| 62 | + */ |
| 63 | + static constexpr IPv4Address in_addr_broadcast(); |
| 64 | + |
| 65 | + /** |
| 66 | + * @return An IPv4 address representing INADDR_LOOPBACK. |
| 67 | + */ |
| 68 | + static constexpr IPv4Address in_addr_loopback(); |
| 69 | + |
| 70 | + /** |
| 71 | + * Construct an IPv4 address from a string in dot-decimal notation. |
| 72 | + * |
| 73 | + * The provided string must betwen one and four octets, inclusive. If the string contains less |
| 74 | + * than four octets, the last octet is treated as an integer of as many bytes as are required to |
| 75 | + * fill out the address to four octets. Thus, the string "127.65530" is converted to the IPv4 |
| 76 | + * address 127.0.255.250. |
| 77 | + * |
| 78 | + * @param address The string in dot-decimal notation to initialize the IPv4 address from. |
| 79 | + * |
| 80 | + * @return If successful, the constructed IPv4 address. Otherwise, an uninitialized value. |
| 81 | + */ |
| 82 | + static constexpr std::optional<IPv4Address> from_string(std::string_view address); |
| 83 | + |
| 84 | + /** |
| 85 | + * Convert the IPv4 address to a four octet string in dot-decimal notation. |
| 86 | + * |
| 87 | + * @return The IPv4 address in dot-decimal notation. |
| 88 | + */ |
| 89 | + std::string to_string() const; |
| 90 | + |
| 91 | + /** |
| 92 | + * @return The IPv4 address as an integer in network order. |
| 93 | + */ |
| 94 | + constexpr int_type network_order() const; |
| 95 | + |
| 96 | + /** |
| 97 | + * @return The IPv4 address as an integer in host order. |
| 98 | + */ |
| 99 | + constexpr int_type host_order() const; |
| 100 | + |
| 101 | + /** |
| 102 | + * Three-way-comparison operator. Defaulted to perform the comparison on the network-order IPv4 |
| 103 | + * address. |
| 104 | + */ |
| 105 | + auto operator<=>(const IPv4Address &) const = default; |
| 106 | + |
| 107 | + /** |
| 108 | + * Stream an IPv4 address as a four octet string in dot-decimal notation. |
| 109 | + * |
| 110 | + * @param stream A reference to the output stream. |
| 111 | + * @param address The IPv4 address to stream. |
| 112 | + * |
| 113 | + * @return A reference to the output stream. |
| 114 | + */ |
| 115 | + friend std::ostream &operator<<(std::ostream &stream, const IPv4Address &address); |
| 116 | + |
| 117 | +private: |
| 118 | + int_type m_address {0}; |
| 119 | +}; |
| 120 | + |
| 121 | +//================================================================================================== |
| 122 | +constexpr IPv4Address::IPv4Address(const address_type &address) noexcept |
| 123 | +{ |
| 124 | + m_address |= static_cast<int_type>(address[0]); |
| 125 | + m_address |= static_cast<int_type>(address[1] << 8); |
| 126 | + m_address |= static_cast<int_type>(address[2] << 16); |
| 127 | + m_address |= static_cast<int_type>(address[3] << 24); |
| 128 | +} |
| 129 | + |
| 130 | +//================================================================================================== |
| 131 | +constexpr IPv4Address::IPv4Address(int_type address) noexcept : m_address(address) |
| 132 | +{ |
| 133 | +} |
| 134 | + |
| 135 | +//================================================================================================== |
| 136 | +constexpr IPv4Address IPv4Address::in_addr_any() |
| 137 | +{ |
| 138 | + return IPv4Address(0x00'00'00'00); |
| 139 | +} |
| 140 | + |
| 141 | +//================================================================================================== |
| 142 | +constexpr IPv4Address IPv4Address::in_addr_broadcast() |
| 143 | +{ |
| 144 | + return IPv4Address(0xff'ff'ff'ff); |
| 145 | +} |
| 146 | + |
| 147 | +//================================================================================================== |
| 148 | +constexpr IPv4Address IPv4Address::in_addr_loopback() |
| 149 | +{ |
| 150 | + return IPv4Address(0x01'00'00'7f); |
| 151 | +} |
| 152 | + |
| 153 | +//================================================================================================== |
| 154 | +constexpr std::optional<IPv4Address> IPv4Address::from_string(std::string_view address) |
| 155 | +{ |
| 156 | + constexpr const auto s_max32 = static_cast<std::uint64_t>(std::numeric_limits<int_type>::max()); |
| 157 | + constexpr const auto s_decimal = '.'; |
| 158 | + |
| 159 | + fly::BasicStringLexer<std::string> lexer(std::move(address)); |
| 160 | + |
| 161 | + std::array<int_type, 4> parts {}; |
| 162 | + std::size_t index = 0; |
| 163 | + |
| 164 | + do |
| 165 | + { |
| 166 | + if (const auto segment = lexer.consume_number(); segment && (*segment <= s_max32)) |
| 167 | + { |
| 168 | + parts[index++] = static_cast<int_type>(*segment); |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + return std::nullopt; |
| 173 | + } |
| 174 | + } while ((index < parts.size()) && lexer.consume_if(s_decimal)); |
| 175 | + |
| 176 | + std::optional<int_type> host_address; |
| 177 | + |
| 178 | + if (index == 1) |
| 179 | + { |
| 180 | + host_address = parts[0]; |
| 181 | + } |
| 182 | + else if (index == 2) |
| 183 | + { |
| 184 | + if ((parts[0] <= 0xff) && (parts[1] <= 0xff'ff'ff)) |
| 185 | + { |
| 186 | + host_address = (parts[0] << 24) | parts[1]; |
| 187 | + } |
| 188 | + } |
| 189 | + else if (index == 3) |
| 190 | + { |
| 191 | + if ((parts[0] <= 0xff) && (parts[1] <= 0xff) && (parts[2] <= 0xff'ff)) |
| 192 | + { |
| 193 | + host_address = (parts[0] << 24) | (parts[1] << 16) | parts[2]; |
| 194 | + } |
| 195 | + } |
| 196 | + else if (index == 4) |
| 197 | + { |
| 198 | + if ((parts[0] <= 0xff) && (parts[1] <= 0xff) && (parts[2] <= 0xff) && (parts[3] <= 0xff)) |
| 199 | + { |
| 200 | + host_address = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + if (!host_address || lexer.peek()) |
| 205 | + { |
| 206 | + return std::nullopt; |
| 207 | + } |
| 208 | + |
| 209 | + return IPv4Address(fly::endian_swap_if_non_native<std::endian::big>(*host_address)); |
| 210 | +} |
| 211 | + |
| 212 | +//================================================================================================== |
| 213 | +constexpr auto IPv4Address::network_order() const -> int_type |
| 214 | +{ |
| 215 | + return m_address; |
| 216 | +} |
| 217 | + |
| 218 | +//================================================================================================== |
| 219 | +constexpr auto IPv4Address::host_order() const -> int_type |
| 220 | +{ |
| 221 | + return fly::endian_swap_if_non_native<std::endian::big>(m_address); |
| 222 | +} |
| 223 | + |
| 224 | +} // namespace fly::net |
0 commit comments