|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include <iostream> |
| 4 | +#include <array> |
| 5 | +#include <cstdint> |
| 6 | +#include <cstring> |
| 7 | +#include <limits> |
| 8 | +#include <compare> |
| 9 | +#include <algorithm> |
| 10 | +#include <format> |
4 | 11 |
|
5 | 12 | namespace ipxp { |
6 | 13 |
|
7 | | -struct IpAddress {}; |
| 14 | +union IPAddress { |
| 15 | + std::array<uint8_t, 16> u8; |
| 16 | + std::array<uint16_t, 8> u16; |
| 17 | + std::array<uint32_t, 4> u32; |
| 18 | + std::array<uint64_t, 2> u64; |
8 | 19 |
|
9 | | -inline std::ostream& operator<<(std::ostream& os, const IpAddress& ip) |
10 | | -{ |
11 | | - (void) ip; |
| 20 | + constexpr IPAddress() noexcept |
| 21 | + { |
| 22 | + std::memset(&u8, 0, sizeof(IPAddress)); |
| 23 | + } |
| 24 | + |
| 25 | + constexpr IPAddress(const uint32_t ipv4) noexcept |
| 26 | + { |
| 27 | + u32[0] = ipv4; |
| 28 | + u32[1] = 0; |
| 29 | + u32[2] = u32[3] = std::numeric_limits<uint32_t>::max(); |
| 30 | + } |
| 31 | + |
| 32 | + constexpr IPAddress(const std::span<const std::byte, 16>& ipv6) noexcept |
| 33 | + { |
| 34 | + std::copy( |
| 35 | + ipv6.begin(), ipv6.end(), reinterpret_cast<std::byte*>(u8.data())); |
| 36 | + } |
| 37 | + |
| 38 | + constexpr bool isIPv4() const noexcept |
| 39 | + { |
| 40 | + return u32[1] == 0 && |
| 41 | + u32[2] == std::numeric_limits<uint32_t>::max() && |
| 42 | + u32[3] == std::numeric_limits<uint32_t>::max(); |
| 43 | + } |
| 44 | + |
| 45 | + constexpr bool isIPv6() const noexcept |
| 46 | + { |
| 47 | + return !isIPv4(); |
| 48 | + } |
| 49 | + |
| 50 | + constexpr bool operator==(const IPAddress& other) const noexcept |
| 51 | + { |
| 52 | + return u8 == other.u8; |
| 53 | + } |
12 | 54 |
|
13 | | - os << "IpAddress{}"; |
14 | | - return os; |
| 55 | + //constexpr std::strong_ordering operator<=>(const IPAddress& other) const noexcept = default; |
| 56 | + |
| 57 | + constexpr IPAddress& operator=(const IPAddress& other) noexcept |
| 58 | + { |
| 59 | + if (this != &other) { |
| 60 | + u8 = other.u8; |
| 61 | + } |
| 62 | + return *this; |
| 63 | + } |
| 64 | + |
| 65 | + constexpr std::size_t size() const noexcept |
| 66 | + { |
| 67 | + return isIPv4() ? 4 : 16; |
| 68 | + } |
| 69 | + |
| 70 | + std::string toString() const noexcept |
| 71 | + { |
| 72 | + std::string res; |
| 73 | + constexpr std::size_t MAX_IP_AS_TEXT_SIZE = 30; |
| 74 | + res.reserve(MAX_IP_AS_TEXT_SIZE); |
| 75 | + |
| 76 | + if (isIPv4()) { |
| 77 | + std::for_each_n(reinterpret_cast<const std::byte*>(u8.data()), size(), |
| 78 | + [&](const std::byte ipByte) { |
| 79 | + std::format_to(std::back_inserter(res), "{}.", static_cast<int>(ipByte)); |
| 80 | + }); |
| 81 | + } else { |
| 82 | + std::for_each_n(reinterpret_cast<const std::byte*>(u8.data()), size(), |
| 83 | + [&](const std::byte ipByte) { |
| 84 | + std::format_to(std::back_inserter(res), "{:02x}:", static_cast<int>(ipByte)); |
| 85 | + }); |
| 86 | + } |
| 87 | + res.pop_back(); |
| 88 | + return res; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +inline std::ostream& operator<<(std::ostream& os, const IPAddress& ip) |
| 93 | +{ |
| 94 | + return os << ip.toString(); |
15 | 95 | } |
16 | 96 |
|
17 | 97 | } // namespace ipxp |
0 commit comments