GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Endpoint.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_NETWORKING_ENDPOINT_HPP
5#define GREM_NETWORKING_ENDPOINT_HPP
6
8
20
21#include <compare> // std::strong_ordering
22#include <cstddef> // std::size_t
23#include <functional> // std::hash
24#include <new> // std::launder
25#include <system_error> // std::error_code
26#include <utility> // std::in_place
27
29
30namespace detail {
31
32inline constexpr in6_addr IPV6_ADDRESS_ANY = IN6ADDR_ANY_INIT;
33inline constexpr in6_addr IPV6_ADDRESS_LOOPBACK = IN6ADDR_LOOPBACK_INIT;
34
35} // namespace detail
36
42using PortNumber = uint16_t;
43
47using ScopeID = uint32_t;
48
52enum class AddressFamily : short { // NOLINT(performance-enum-size)
53 IPv4 = AF_INET,
54 IPv6 = AF_INET6,
55};
56
61public:
62 static const IPv4Address ANY;
63 static const IPv4Address BROADCAST;
64 static const IPv4Address NONE;
65 static const IPv4Address LOOPBACK;
66
76 [[nodiscard]] GREM_API(networking) static Optional<IPv4Address> parse(CStringView string, std::error_code& errorCode) noexcept;
77
89 [[nodiscard]] GREM_API(networking) static IPv4Address parse(CStringView string);
90
107 [[nodiscard]] GREM_API(networking) static Optional<IPv4Address> resolve(const char* hostName, const char* service, std::error_code& errorCode);
108
125 [[nodiscard]] GREM_API(networking) static IPv4Address resolve(const char* hostName, const char* service);
126
144 [[nodiscard]] GREM_API(networking) static Optional<IPv4Address> resolve(CStringView host, std::error_code& errorCode);
145
161 [[nodiscard]] GREM_API(networking) static IPv4Address resolve(CStringView host);
162
175 [[nodiscard]] GREM_API(networking) static Optional<IPv4Address> getLocalAddress(std::error_code& errorCode);
176
189 [[nodiscard]] GREM_API(networking) static IPv4Address getLocalAddress();
190
196 constexpr explicit IPv4Address(const in_addr& sin_addr) noexcept
197 : sin_addr(sin_addr) {}
198
208 constexpr IPv4Address(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3) noexcept
209#ifdef _WIN32
210 : sin_addr{.S_un{.S_addr = (static_cast<uint32_t>(byte3) << 24) | //
211 (static_cast<uint32_t>(byte2) << 16) | //
212 (static_cast<uint32_t>(byte1) << 8) | //
213 (static_cast<uint32_t>(byte0) << 0)}}
214#else
215 : sin_addr{.s_addr = (static_cast<uint32_t>(byte3) << 24) | //
216 (static_cast<uint32_t>(byte2) << 16) | //
217 (static_cast<uint32_t>(byte1) << 8) | //
218 (static_cast<uint32_t>(byte0) << 0)}
219#endif
220 {
221 }
222
228 [[nodiscard]] constexpr Array<uint8_t, 4> toBigEndianBytes() const noexcept {
229 const uint8_t byte0 = static_cast<uint8_t>((static_cast<uint32_t>(sin_addr.s_addr) >> 0) & 0xFF);
230 const uint8_t byte1 = static_cast<uint8_t>((static_cast<uint32_t>(sin_addr.s_addr) >> 8) & 0xFF);
231 const uint8_t byte2 = static_cast<uint8_t>((static_cast<uint32_t>(sin_addr.s_addr) >> 16) & 0xFF);
232 const uint8_t byte3 = static_cast<uint8_t>((static_cast<uint32_t>(sin_addr.s_addr) >> 24) & 0xFF);
233 return {byte0, byte1, byte2, byte3};
234 }
235
242 [[nodiscard]] constexpr bool isAny() const noexcept {
243 // Note: We can't use IPv4Address::ANY directly here while remaining constexpr, since its constexpr value is not defined yet.
244 // (Its value has to be defined after the class definition since IPv4Address is an incomplete type until then.)
245 return *this == IPv4Address{0, 0, 0, 0};
246 }
247
253 [[nodiscard]] constexpr bool isLoopback() const noexcept {
254 const uint8_t byte0 = static_cast<uint8_t>((static_cast<uint32_t>(sin_addr.s_addr) >> 0) & 0xFF);
255 return byte0 == 127;
256 }
257
268 [[nodiscard]] constexpr bool isPrivate() const noexcept {
269 const uint8_t byte0 = static_cast<uint8_t>((static_cast<uint32_t>(sin_addr.s_addr) >> 0) & 0xFF);
270 const uint8_t byte1 = static_cast<uint8_t>((static_cast<uint32_t>(sin_addr.s_addr) >> 8) & 0xFF);
271 return byte0 == 10 || (byte0 == 172 && byte1 >= 16 && byte1 < 32) || (byte0 == 192 && byte1 == 168);
272 }
273
280 [[nodiscard]] constexpr in_addr* get() noexcept {
281 return &sin_addr;
282 }
283
291 [[nodiscard]] constexpr const in_addr* get() const noexcept {
292 return &sin_addr;
293 }
294
302 [[nodiscard]] constexpr bool operator==(IPv4Address other) const noexcept {
303 return toBigEndianBytes() == other.toBigEndianBytes();
304 }
305
314 [[nodiscard]] constexpr std::strong_ordering operator<=>(IPv4Address other) const noexcept {
315 return toBigEndianBytes() <=> other.toBigEndianBytes();
316 }
317
318private:
319 constexpr explicit IPv4Address(uint32_t address) noexcept
320 : sin_addr{} {
321 sin_addr.s_addr = (static_cast<uint32_t>(address << 24) & uint32_t{0xFF000000}) | //
322 (static_cast<uint32_t>(address << 8) & uint32_t{0x00FF0000}) | //
323 (static_cast<uint32_t>(address >> 8) & uint32_t{0x0000FF00}) | //
324 (static_cast<uint32_t>(address >> 24) & uint32_t{0x000000FF});
325 }
326
327 in_addr sin_addr;
328};
329
330inline constexpr IPv4Address IPv4Address::ANY{0, 0, 0, 0};
331inline constexpr IPv4Address IPv4Address::BROADCAST{255, 255, 255, 255};
332inline constexpr IPv4Address IPv4Address::NONE{255, 255, 255, 255};
333inline constexpr IPv4Address IPv4Address::LOOPBACK{127, 0, 0, 1};
334
339public:
340 static const IPv6Address ANY;
341 static const IPv6Address LOOPBACK;
342
352 [[nodiscard]] GREM_API(networking) static Optional<IPv6Address> parse(CStringView string, std::error_code& errorCode) noexcept;
353
365 [[nodiscard]] GREM_API(networking) static IPv6Address parse(CStringView string);
366
383 [[nodiscard]] GREM_API(networking) static Optional<IPv6Address> resolve(const char* hostName, const char* service, std::error_code& errorCode);
384
401 [[nodiscard]] GREM_API(networking) static IPv6Address resolve(const char* hostName, const char* service);
402
421 [[nodiscard]] GREM_API(networking) static Optional<IPv6Address> resolve(CStringView host, std::error_code& errorCode);
422
439 [[nodiscard]] GREM_API(networking) static IPv6Address resolve(CStringView host);
440
451 [[nodiscard]] GREM_API(networking) static Optional<IPv6Address> getLocalAddress(std::error_code& errorCode);
452
464 [[nodiscard]] GREM_API(networking) static IPv6Address getLocalAddress();
465
471 constexpr explicit IPv6Address(const in6_addr& sin6_addr) noexcept
472 : sin6_addr(sin6_addr) {}
473
495 constexpr IPv6Address(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6, uint8_t byte7, uint8_t byte8, uint8_t byte9,
496 uint8_t byte10, uint8_t byte11, uint8_t byte12, uint8_t byte13, uint8_t byte14, uint8_t byte15) noexcept
497 : sin6_addr(bit_cast<in6_addr>(Array<uint8_t, 16>{byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8, byte9, byte10, byte11, byte12, byte13, byte14, byte15})) {}
498
504 [[nodiscard]] constexpr Array<uint8_t, 16> toBigEndianBytes() const noexcept {
505 return bit_cast<Array<uint8_t, 16>>(sin6_addr);
506 }
507
513 [[nodiscard]] constexpr bool isAny() const noexcept {
514 // Note: We can't use IPv6Address::ANY directly here while remaining constexpr, since its constexpr value is not defined yet.
515 // (Its value has to be defined after the class definition since IPv6Address is an incomplete type until then.)
516 return *this == IPv6Address{detail::IPV6_ADDRESS_ANY};
517 }
518
524 [[nodiscard]] constexpr bool isLoopback() const noexcept {
525 // Note: We can't use IPv6Address::LOOPBACK directly here while remaining constexpr, since its constexpr value is not defined yet.
526 // (Its value has to be defined after the class definition since IPv6Address is an incomplete type until then.)
527 return *this == IPv6Address{detail::IPV6_ADDRESS_LOOPBACK};
528 }
529
536 [[nodiscard]] constexpr in6_addr* get() noexcept {
537 return &sin6_addr;
538 }
539
547 [[nodiscard]] constexpr const in6_addr* get() const noexcept {
548 return &sin6_addr;
549 }
550
558 [[nodiscard]] constexpr bool operator==(const IPv6Address& other) const noexcept {
559 return toBigEndianBytes() == other.toBigEndianBytes();
560 }
561
570 [[nodiscard]] constexpr std::strong_ordering operator<=>(const IPv6Address& other) const noexcept {
571 return toBigEndianBytes() <=> other.toBigEndianBytes();
572 }
573
574private:
575 in6_addr sin6_addr;
576};
577
578inline constexpr IPv6Address IPv6Address::ANY{detail::IPV6_ADDRESS_ANY};
579inline constexpr IPv6Address IPv6Address::LOOPBACK{detail::IPV6_ADDRESS_LOOPBACK};
580
585public:
598 [[nodiscard]] GREM_API(networking) static Optional<IPv4Endpoint> parse(CStringView addressString, CStringView portNumberString, std::error_code& errorCode) noexcept;
599
615 [[nodiscard]] GREM_API(networking) static IPv4Endpoint parse(CStringView addressString, CStringView portNumberString);
616
628 [[nodiscard]] GREM_API(networking) static Optional<IPv4Endpoint> parse(CStringView string, std::error_code& errorCode) noexcept;
629
643 [[nodiscard]] GREM_API(networking) static IPv4Endpoint parse(CStringView string);
644
661 [[nodiscard]] GREM_API(networking) static Optional<IPv4Endpoint> resolve(const char* hostName, const char* service, std::error_code& errorCode);
662
680 [[nodiscard]] GREM_API(networking) static IPv4Endpoint resolve(const char* hostName, const char* service);
681
699 [[nodiscard]] GREM_API(networking) static Optional<IPv4Endpoint> resolve(CStringView host, std::error_code& errorCode);
700
717 [[nodiscard]] GREM_API(networking) static IPv4Endpoint resolve(CStringView host);
718
731 [[nodiscard]] GREM_API(networking) static Optional<IPv4Endpoint> getLocalEndpoint(std::error_code& errorCode);
732
745 [[nodiscard]] GREM_API(networking) static IPv4Endpoint getLocalEndpoint();
746
753 constexpr explicit IPv4Endpoint(IPv4Address address, PortNumber port = 0) noexcept
754 : addr{} {
755 addr.sin_family = AF_INET;
756#ifdef __APPLE__
757 addr.sin_len = sizeof(addr);
758#endif
759 addr.sin_addr = *address.get();
760 addr.sin_port = convertHostEndianToBigEndian(port);
761 }
762
768 constexpr explicit IPv4Endpoint(const sockaddr_in& addr) noexcept
769 : addr(addr) {}
770
776 constexpr void setAddress(IPv4Address newAddress) noexcept {
777 addr.sin_addr = *newAddress.get();
778 }
779
785 constexpr void setPortNumber(PortNumber newPortNumber) noexcept {
786 addr.sin_port = convertHostEndianToBigEndian(decltype(addr.sin_port){newPortNumber});
787 }
788
794 [[nodiscard]] constexpr IPv4Address getAddress() const noexcept {
795 return IPv4Address{addr.sin_addr};
796 }
797
803 [[nodiscard]] constexpr PortNumber getPortNumber() const noexcept {
804 return PortNumber{convertBigEndianToHostEndian(addr.sin_port)};
805 }
806
813 [[nodiscard]] constexpr bool isAddressAny() const noexcept {
814 return getAddress().isAny();
815 }
816
824 [[nodiscard]] constexpr bool isAddressLoopback() const noexcept {
825 return getAddress().isLoopback();
826 }
827
839 [[nodiscard]] constexpr bool isAddressPrivate() const noexcept {
840 return getAddress().isPrivate();
841 }
842
849 [[nodiscard]] constexpr sockaddr_in* get() noexcept {
850 return &addr;
851 }
852
860 [[nodiscard]] constexpr const sockaddr_in* get() const noexcept {
861 return &addr;
862 }
863
871 [[nodiscard]] constexpr bool operator==(IPv4Endpoint other) const noexcept {
872 return getAddress() == other.getAddress() && getPortNumber() == other.getPortNumber();
873 }
874
884 [[nodiscard]] constexpr std::strong_ordering operator<=>(IPv4Endpoint other) const noexcept {
885 return (getAddress() == other.getAddress()) ? getPortNumber() <=> other.getPortNumber() : getAddress() <=> other.getAddress();
886 }
887
888private:
889 sockaddr_in addr;
890};
891
896public:
910 [[nodiscard]] GREM_API(networking) static Optional<IPv6Endpoint> parse(CStringView addressString, CStringView portNumberString, std::error_code& errorCode) noexcept;
911
927 [[nodiscard]] GREM_API(networking) static IPv6Endpoint parse(CStringView addressString, CStringView portNumberString);
928
941 [[nodiscard]] GREM_API(networking) static Optional<IPv6Endpoint> parse(CStringView string, std::error_code& errorCode) noexcept;
942
957 [[nodiscard]] GREM_API(networking) static IPv6Endpoint parse(CStringView string);
958
975 [[nodiscard]] GREM_API(networking) static Optional<IPv6Endpoint> resolve(const char* hostName, const char* service, std::error_code& errorCode);
976
994 [[nodiscard]] GREM_API(networking) static IPv6Endpoint resolve(const char* hostName, const char* service);
995
1013 [[nodiscard]] GREM_API(networking) static Optional<IPv6Endpoint> resolve(CStringView host, std::error_code& errorCode);
1014
1031 [[nodiscard]] GREM_API(networking) static IPv6Endpoint resolve(CStringView host);
1032
1043 [[nodiscard]] GREM_API(networking) static Optional<IPv6Endpoint> getLocalEndpoint(std::error_code& errorCode);
1044
1056 [[nodiscard]] GREM_API(networking) static IPv6Endpoint getLocalEndpoint();
1057
1066 constexpr explicit IPv6Endpoint(const IPv6Address& address, PortNumber port = 0, ScopeID scopeID = 0) noexcept
1067 : addr{} {
1068 addr.sin6_family = AF_INET6;
1069#ifdef __APPLE__
1070 addr.sin_len = sizeof(addr);
1071#endif
1072 addr.sin6_addr = *address.get();
1073 addr.sin6_port = convertHostEndianToBigEndian(port);
1074 addr.sin6_scope_id = scopeID;
1075 }
1076
1082 constexpr explicit IPv6Endpoint(const sockaddr_in6& addr) noexcept
1083 : addr(addr) {}
1084
1090 constexpr void setAddress(const IPv6Address& newAddress) noexcept {
1091 addr.sin6_addr = *newAddress.get();
1092 }
1093
1099 constexpr void setPortNumber(PortNumber newPortNumber) noexcept {
1100 addr.sin6_port = convertHostEndianToBigEndian(decltype(addr.sin6_port){newPortNumber});
1101 }
1102
1108 constexpr void setScopeID(ScopeID newScopeID) noexcept {
1109 addr.sin6_scope_id = newScopeID;
1110 }
1111
1117 [[nodiscard]] constexpr IPv6Address getAddress() const noexcept {
1118 return IPv6Address{addr.sin6_addr};
1119 }
1120
1126 [[nodiscard]] constexpr PortNumber getPortNumber() const noexcept {
1127 return PortNumber{convertBigEndianToHostEndian(addr.sin6_port)};
1128 }
1129
1135 [[nodiscard]] constexpr ScopeID getScopeID() const noexcept {
1136 return addr.sin6_scope_id;
1137 }
1138
1145 [[nodiscard]] constexpr bool isAddressAny() const noexcept {
1146 return getAddress().isAny();
1147 }
1148
1155 [[nodiscard]] constexpr bool isAddressLoopback() const noexcept {
1156 return getAddress().isLoopback();
1157 }
1158
1165 [[nodiscard]] constexpr sockaddr_in6* get() noexcept {
1166 return &addr;
1167 }
1168
1176 [[nodiscard]] constexpr const sockaddr_in6* get() const noexcept {
1177 return &addr;
1178 }
1179
1188 [[nodiscard]] constexpr bool operator==(const IPv6Endpoint& other) const noexcept {
1189 return getAddress() == other.getAddress() && getPortNumber() == other.getPortNumber() && getScopeID() == other.getScopeID();
1190 }
1191
1202 [[nodiscard]] constexpr std::strong_ordering operator<=>(const IPv6Endpoint& other) const noexcept {
1203 return (getAddress() == other.getAddress()) ? (getPortNumber() == other.getPortNumber()) ? getScopeID() <=> other.getScopeID() : getPortNumber() <=> other.getPortNumber()
1204 : getAddress() <=> other.getAddress();
1205 }
1206
1207private:
1208 sockaddr_in6 addr;
1209};
1210
1216public:
1227 [[nodiscard]] static Endpoint any(AddressFamily family, PortNumber portNumber = 0) noexcept {
1228 switch (family) {
1229 case AddressFamily::IPv4: return Endpoint{IPv4Address::ANY, portNumber};
1230 case AddressFamily::IPv6: return Endpoint{IPv6Address::ANY, portNumber};
1231 }
1232 unreachable();
1233 }
1234
1246 [[nodiscard]] static Endpoint loopback(AddressFamily family, PortNumber portNumber = 0) noexcept {
1247 switch (family) {
1248 case AddressFamily::IPv4: return Endpoint{IPv4Address::LOOPBACK, portNumber};
1249 case AddressFamily::IPv6: return Endpoint{IPv6Address::LOOPBACK, portNumber};
1250 }
1251 unreachable();
1252 }
1253
1267 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> parse(CStringView addressString, CStringView portNumberString, std::error_code& errorCode) noexcept;
1268
1284 [[nodiscard]] GREM_API(networking) static Endpoint parse(CStringView addressString, CStringView portNumberString);
1285
1300 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> parse(AddressFamily family, CStringView addressString, CStringView portNumberString,
1301 std::error_code& errorCode) noexcept;
1302
1320 [[nodiscard]] GREM_API(networking) static Endpoint parse(AddressFamily family, CStringView addressString, CStringView portNumberString);
1321
1335 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> parse(CStringView string, std::error_code& errorCode) noexcept;
1336
1351 [[nodiscard]] GREM_API(networking) static Endpoint parse(CStringView string);
1352
1367 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> parse(AddressFamily family, CStringView string, std::error_code& errorCode) noexcept;
1368
1385 [[nodiscard]] GREM_API(networking) static Endpoint parse(AddressFamily family, CStringView string);
1386
1403 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> resolve(const char* hostName, const char* service, std::error_code& errorCode);
1404
1423 [[nodiscard]] GREM_API(networking) static Endpoint resolve(const char* hostName, const char* service);
1424
1443 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> resolve(AddressFamily family, const char* hostName, const char* service, std::error_code& errorCode);
1444
1464 [[nodiscard]] GREM_API(networking) static Endpoint resolve(AddressFamily family, const char* hostName, const char* service);
1465
1482 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> resolve(CStringView host, std::error_code& errorCode);
1483
1500 [[nodiscard]] GREM_API(networking) static Endpoint resolve(CStringView host);
1501
1520 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> resolve(AddressFamily family, CStringView host, std::error_code& errorCode);
1521
1539 [[nodiscard]] GREM_API(networking) static Endpoint resolve(AddressFamily family, CStringView host);
1540
1553 [[nodiscard]] GREM_API(networking) static Optional<Endpoint> getLocalEndpoint(AddressFamily family, std::error_code& errorCode);
1554
1568 [[nodiscard]] GREM_API(networking) static Endpoint getLocalEndpoint(AddressFamily family);
1569
1580 template <typename SockAddr>
1581 Endpoint(const SockAddr* addrStorage, socklen_t length)
1583 GREM_ASSERT(addrStorage);
1584 GREM_ASSERT(length > 0);
1585 GREM_ASSERT(static_cast<size_t>(length) <= sizeof(sockaddr_storage));
1586 memcpy(storage, addrStorage, static_cast<size_t>(length));
1587 }
1588
1594 Endpoint(const IPv4Endpoint& endpoint) noexcept {
1595 memcpy(storage, endpoint.get(), sizeof(*endpoint.get()));
1596 }
1597
1603 Endpoint(const IPv6Endpoint& endpoint) noexcept {
1604 memcpy(storage, endpoint.get(), sizeof(*endpoint.get()));
1605 }
1606
1613 explicit Endpoint(const IPv4Address& address, PortNumber port = 0) noexcept
1614 : Endpoint(IPv4Endpoint{address, port}) {}
1615
1623 explicit Endpoint(const IPv6Address& address, PortNumber port = 0, ScopeID scopeID = 0) noexcept
1624 : Endpoint(IPv6Endpoint{address, port, scopeID}) {}
1625
1631 explicit Endpoint(const sockaddr_in& addr) noexcept
1632 : Endpoint(IPv4Endpoint{addr}) {}
1633
1639 explicit Endpoint(const sockaddr_in6& addr) noexcept
1640 : Endpoint(IPv6Endpoint{addr}) {}
1641
1643 ~Endpoint() = default;
1644
1646 Endpoint(const Endpoint& other) noexcept {
1647 memcpy(storage, other.storage, sizeof(other.storage));
1648 }
1649
1651 Endpoint(Endpoint&& other) noexcept
1652 : Endpoint(other) {} // NOLINT(performance-move-constructor-init)
1653
1655 Endpoint& operator=(const Endpoint& other) noexcept {
1656 memcpy(storage, other.storage, sizeof(other.storage));
1657 return *this;
1658 }
1659
1661 Endpoint& operator=(Endpoint&& other) noexcept {
1662 return *this = other;
1663 }
1664
1670 void setPortNumber(PortNumber newPortNumber) noexcept {
1671 switch (getAddressFamily()) {
1672 case AddressFamily::IPv4: *this = Endpoint{*getIPv4Address(), newPortNumber}; break;
1673 case AddressFamily::IPv6: *this = Endpoint{*getIPv6Address(), newPortNumber}; break;
1674 }
1675 }
1676
1682 [[nodiscard]] AddressFamily getAddressFamily() const noexcept {
1683 short family{};
1684 memcpy(&family, storage, sizeof(family));
1685 return static_cast<AddressFamily>(family);
1686 }
1687
1693 [[nodiscard]] socklen_t getLength() const noexcept {
1694 switch (getAddressFamily()) {
1695 case AddressFamily::IPv4: return static_cast<socklen_t>(sizeof(sockaddr_in));
1696 case AddressFamily::IPv6: return static_cast<socklen_t>(sizeof(sockaddr_in6));
1697 }
1698 return 0;
1699 }
1700
1707 [[nodiscard]] Optional<IPv4Endpoint> getIPv4Endpoint() const noexcept {
1709 ? Optional<IPv4Endpoint>{std::in_place, *std::launder(reinterpret_cast<const sockaddr_in*>(storage))}
1711 }
1712
1719 [[nodiscard]] Optional<IPv6Endpoint> getIPv6Endpoint() const noexcept {
1721 ? Optional<IPv6Endpoint>{std::in_place, *std::launder(reinterpret_cast<const sockaddr_in6*>(storage))}
1723 }
1724
1731 [[nodiscard]] Optional<IPv4Address> getIPv4Address() const noexcept {
1732 return (getAddressFamily() == AddressFamily::IPv4) ? Optional<IPv4Address>{std::in_place, getIPv4Endpoint()->getAddress()} : Optional<IPv4Address>{};
1733 }
1734
1741 [[nodiscard]] Optional<IPv6Address> getIPv6Address() const noexcept {
1742 return (getAddressFamily() == AddressFamily::IPv6) ? Optional<IPv6Address>{std::in_place, getIPv6Endpoint()->getAddress()} : Optional<IPv6Address>{};
1743 }
1744
1751 [[nodiscard]] PortNumber getPortNumber() const noexcept {
1752 switch (getAddressFamily()) {
1753 case AddressFamily::IPv4: return getIPv4Endpoint()->getPortNumber();
1754 case AddressFamily::IPv6: return getIPv6Endpoint()->getPortNumber();
1755 }
1756 return 0;
1757 }
1758
1765 [[nodiscard]] ScopeID getScopeID() const noexcept {
1766 switch (getAddressFamily()) {
1767 case AddressFamily::IPv4: break;
1768 case AddressFamily::IPv6: return getIPv6Endpoint()->getScopeID();
1769 }
1770 return 0;
1771 }
1772
1779 [[nodiscard]] bool isAddressAny() const noexcept {
1780 switch (getAddressFamily()) {
1781 case AddressFamily::IPv4: return getIPv4Endpoint()->isAddressAny();
1782 case AddressFamily::IPv6: return getIPv6Endpoint()->isAddressAny();
1783 }
1784 return false;
1785 }
1786
1793 [[nodiscard]] bool isAddressLoopback() const noexcept {
1794 switch (getAddressFamily()) {
1795 case AddressFamily::IPv4: return getIPv4Endpoint()->isAddressLoopback();
1796 case AddressFamily::IPv6: return getIPv6Endpoint()->isAddressLoopback();
1797 }
1798 return false;
1799 }
1800
1806 [[nodiscard]] sockaddr* get() noexcept {
1807 return reinterpret_cast<sockaddr*>(storage);
1808 }
1809
1816 [[nodiscard]] const sockaddr* get() const noexcept {
1817 return reinterpret_cast<const sockaddr*>(storage);
1818 }
1819
1827 [[nodiscard]] bool operator==(const Endpoint& other) const noexcept {
1828 const AddressFamily family = getAddressFamily();
1829 const AddressFamily otherFamily = other.getAddressFamily();
1830 if (family == otherFamily) {
1831 switch (family) {
1832 case AddressFamily::IPv4: return *getIPv4Endpoint() == *other.getIPv4Endpoint();
1833 case AddressFamily::IPv6: return *getIPv6Endpoint() == *other.getIPv6Endpoint();
1834 }
1835 return true;
1836 }
1837 return false;
1838 }
1839
1847 [[nodiscard]] std::strong_ordering operator<=>(const Endpoint& other) const noexcept {
1848 const AddressFamily family = getAddressFamily();
1849 const AddressFamily otherFamily = other.getAddressFamily();
1850 if (family == otherFamily) {
1851 switch (family) {
1852 case AddressFamily::IPv4: return *getIPv4Endpoint() <=> *other.getIPv4Endpoint();
1853 case AddressFamily::IPv6: return *getIPv6Endpoint() <=> *other.getIPv6Endpoint();
1854 }
1855 return std::strong_ordering::equal;
1856 }
1857 return family <=> otherFamily;
1858 }
1859
1860private:
1861 alignas(sockaddr_storage) byte storage[sizeof(sockaddr_storage)];
1862};
1863
1864} // namespace grem::networking
1865
1866template <>
1867struct std::hash<grem::networking::IPv4Address> {
1868 [[nodiscard]] std::size_t operator()(const grem::networking::IPv4Address& address) const {
1869 return grem::getHash(grem::convertBigEndianToHostEndian(static_cast<uint32_t>(address.get()->s_addr)));
1870 }
1871};
1872
1873template <>
1874struct std::hash<grem::networking::IPv6Address> {
1875 [[nodiscard]] std::size_t operator()(const grem::networking::IPv6Address& address) const {
1876 const grem::Array<grem::uint8_t, 16> bytes = address.toBigEndianBytes();
1877 return grem::getRangeHash(bytes);
1878 }
1879};
1880
1881template <>
1882struct std::hash<grem::networking::IPv4Endpoint> {
1883 [[nodiscard]] std::size_t operator()(const grem::networking::IPv4Endpoint& endpoint) const {
1884 return grem::getHash(endpoint.getAddress(), endpoint.getPortNumber());
1885 }
1886};
1887
1888template <>
1889struct std::hash<grem::networking::IPv6Endpoint> {
1890 [[nodiscard]] std::size_t operator()(const grem::networking::IPv6Endpoint& endpoint) const {
1891 return grem::getHash(endpoint.getAddress(), endpoint.getPortNumber());
1892 }
1893};
1894
1895template <>
1896struct std::hash<grem::networking::Endpoint> {
1897 [[nodiscard]] std::size_t operator()(const grem::networking::Endpoint& endpoint) const {
1898 switch (endpoint.getAddressFamily()) {
1901 }
1902 return 0;
1903 }
1904};
1905
1906template <>
1908 void formatTo(FormatOutput& output, const grem::networking::IPv4Address& value) const {
1909 std::error_code errorCode{};
1911 if (errorCode) {
1912 throw grem::networking::Error{errorCode};
1913 }
1914
1916 const char* string = inet_ntop(AF_INET, value.get(), buffer.data(), static_cast<socklen_t>(buffer.size()));
1917 if (!string) {
1919 }
1921 }
1922};
1923
1924template <>
1926 void formatTo(FormatOutput& output, const grem::networking::IPv6Address& value) const {
1927 std::error_code errorCode{};
1929 if (errorCode) {
1930 throw grem::networking::Error{errorCode};
1931 }
1932
1934 const char* string = inet_ntop(AF_INET6, value.get(), buffer.data(), static_cast<socklen_t>(buffer.size()));
1935 if (!string) {
1937 }
1939 }
1940};
1941
1942template <>
1944 bool alwaysIncludePort = false;
1945
1946 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
1947 if (*p == '0') {
1948 alwaysIncludePort = true;
1949 ++p;
1950 }
1951 return p;
1952 }
1953
1954 void formatTo(FormatOutput& output, const grem::networking::IPv4Endpoint& value) const {
1955 const grem::networking::IPv4Address address = value.getAddress();
1956 const grem::networking::PortNumber portNumber = value.getPortNumber();
1958 if (alwaysIncludePort || portNumber != 0) {
1959 output.append(":");
1961 }
1962 }
1963};
1964
1965template <>
1967 bool skipBrackets = false;
1968 bool alwaysIncludePort = false;
1969
1970 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
1971 if (*p == 'n') {
1972 skipBrackets = true;
1973 ++p;
1974 }
1975 if (*p == '0') {
1976 alwaysIncludePort = true;
1977 ++p;
1978 }
1979 return p;
1980 }
1981
1982 void formatTo(FormatOutput& output, const grem::networking::IPv6Endpoint& value) const {
1983 const grem::networking::IPv6Address address = value.getAddress();
1984 const grem::networking::PortNumber portNumber = value.getPortNumber();
1985 if (!skipBrackets || alwaysIncludePort || portNumber != 0) {
1986 output.append("[");
1987 }
1989 if (!skipBrackets || alwaysIncludePort || portNumber != 0) {
1990 output.append("]");
1991 }
1992 if (alwaysIncludePort || portNumber != 0) {
1993 output.append(":");
1995 }
1996 }
1997};
1998
1999template <>
2001 bool skipBrackets = false;
2002 bool alwaysIncludePort = false;
2003
2004 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
2005 if (*p == 'n') {
2006 skipBrackets = true;
2007 ++p;
2008 }
2009 if (*p == '0') {
2010 alwaysIncludePort = true;
2011 ++p;
2012 }
2013 return p;
2014 }
2015
2016 void formatTo(FormatOutput& output, const grem::networking::Endpoint& value) const {
2017 switch (value.getAddressFamily()) {
2020 formatter.alwaysIncludePort = alwaysIncludePort;
2021 formatter.formatTo(output, *value.getIPv4Endpoint());
2022 break;
2023 }
2026 formatter.skipBrackets = skipBrackets;
2027 formatter.alwaysIncludePort = alwaysIncludePort;
2028 formatter.formatTo(output, *value.getIPv6Endpoint());
2029 break;
2030 }
2031 }
2032 }
2033};
2034
2035#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition formatting.hpp:41
virtual constexpr void append(StringView string)=0
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Combined network address and port number (and potentially other info) that identifies a host,...
Definition Endpoint.hpp:1215
static Optional< Endpoint > resolve(const char *hostName, const char *service, std::error_code &errorCode)
Resolve the endpoint address and port number of a host through the Domain Name System (DNS) using a g...
static Endpoint parse(AddressFamily family, CStringView string)
Parse an endpoint address of a specific address family type from an ASCII string representation with ...
static Endpoint resolve(AddressFamily family, CStringView host)
Resolve the endpoint address and port number of a host with a specific address family type through th...
static Endpoint loopback(AddressFamily family, PortNumber portNumber=0) noexcept
Create an endpoint with the LOOPBACK address (0.0.0.0 or ::) for a given address family.
Definition Endpoint.hpp:1246
socklen_t getLength() const noexcept
Get the size of the endpoint's underlying native address structure.
Definition Endpoint.hpp:1693
bool isAddressAny() const noexcept
Check if the value of this endpoint's address is equal to the ANY address (0.0.0.0 or ::).
Definition Endpoint.hpp:1779
Endpoint(const SockAddr *addrStorage, socklen_t length)
Construct an endpoint from a native sockaddr_storage buffer.
Definition Endpoint.hpp:1581
static Optional< Endpoint > parse(CStringView string, std::error_code &errorCode) noexcept
Parse an endpoint address from an ASCII string representation with an optional decimal port number.
Endpoint(const Endpoint &other) noexcept
Copy constructor.
Definition Endpoint.hpp:1646
static Optional< Endpoint > getLocalEndpoint(AddressFamily family, std::error_code &errorCode)
Get the endpoint address and port of the local host by calling connect() on a temporary connectionles...
Endpoint(const IPv6Address &address, PortNumber port=0, ScopeID scopeID=0) noexcept
Construct an endpoint from an IPv4 address and port number.
Definition Endpoint.hpp:1623
static Endpoint getLocalEndpoint(AddressFamily family)
Get the endpoint address and port of the local host by calling connect() on a temporary connectionles...
Endpoint(Endpoint &&other) noexcept
Move constructor.
Definition Endpoint.hpp:1651
bool operator==(const Endpoint &other) const noexcept
Compare this endpoint to another for equality.
Definition Endpoint.hpp:1827
void setPortNumber(PortNumber newPortNumber) noexcept
Set the port number of the endpoint.
Definition Endpoint.hpp:1670
std::strong_ordering operator<=>(const Endpoint &other) const noexcept
Compare this endpoint to another.
Definition Endpoint.hpp:1847
Endpoint(const sockaddr_in6 &addr) noexcept
Construct an IPv6 endpoint from a native sockaddr_in6 structure.
Definition Endpoint.hpp:1639
static Optional< Endpoint > resolve(AddressFamily family, CStringView host, std::error_code &errorCode)
Resolve the endpoint address and port number of a host with a specific address family type through th...
static Optional< Endpoint > parse(AddressFamily family, CStringView string, std::error_code &errorCode) noexcept
Parse an endpoint address of a specific address family type from an ASCII string representation with ...
Endpoint(const IPv4Address &address, PortNumber port=0) noexcept
Construct an endpoint from an IPv4 address and port number.
Definition Endpoint.hpp:1613
static Optional< Endpoint > parse(CStringView addressString, CStringView portNumberString, std::error_code &errorCode) noexcept
Parse an endpoint address from an ASCII string representation, and a port number from a decimal ASCII...
static Optional< Endpoint > parse(AddressFamily family, CStringView addressString, CStringView portNumberString, std::error_code &errorCode) noexcept
Parse an endpoint address of a specific address family type from an ASCII string representation,...
Endpoint(const sockaddr_in &addr) noexcept
Construct an IPv4 endpoint from a native sockaddr_in structure.
Definition Endpoint.hpp:1631
static Endpoint resolve(CStringView host)
Resolve the endpoint address and port number of a host through the Domain Name System (DNS) using a g...
~Endpoint()=default
Destructor.
Endpoint(const IPv4Endpoint &endpoint) noexcept
Implicitly convert or construct a generic endpoint from an IPv4 endpoint.
Definition Endpoint.hpp:1594
Optional< IPv6Address > getIPv6Address() const noexcept
Get the IPv6 address of this endpoint.
Definition Endpoint.hpp:1741
sockaddr * get() noexcept
Get a pointer to the underlying native address structure of the endpoint.
Definition Endpoint.hpp:1806
Endpoint & operator=(const Endpoint &other) noexcept
Copy assignment.
Definition Endpoint.hpp:1655
AddressFamily getAddressFamily() const noexcept
Get the address family of the endpoint.
Definition Endpoint.hpp:1682
ScopeID getScopeID() const noexcept
Get the scope/zone ID of the endpoint.
Definition Endpoint.hpp:1765
Endpoint(const IPv6Endpoint &endpoint) noexcept
Implicitly convert or construct a generic endpoint from an IPv6 endpoint.
Definition Endpoint.hpp:1603
static Optional< Endpoint > resolve(AddressFamily family, const char *hostName, const char *service, std::error_code &errorCode)
Resolve the endpoint address and port number of a host with a specific address family type through th...
Endpoint & operator=(Endpoint &&other) noexcept
Move assignment.
Definition Endpoint.hpp:1661
static Endpoint resolve(AddressFamily family, const char *hostName, const char *service)
Resolve the endpoint address and port number of a host with a specific address family type through th...
static Endpoint parse(CStringView string)
Parse an endpoint address from an ASCII string representation with an optional decimal port number,...
static Endpoint resolve(const char *hostName, const char *service)
Resolve the endpoint address and port number of a host through the Domain Name System (DNS) using a g...
const sockaddr * get() const noexcept
Get a pointer to the underlying native address structure of the endpoint.
Definition Endpoint.hpp:1816
static Optional< Endpoint > resolve(CStringView host, std::error_code &errorCode)
Resolve the endpoint address and port number of a host through the Domain Name System (DNS) using a g...
static Endpoint parse(CStringView addressString, CStringView portNumberString)
Parse an endpoint address from an ASCII string representation, and a port number from a decimal ASCII...
static Endpoint parse(AddressFamily family, CStringView addressString, CStringView portNumberString)
Parse an endpoint address of a specific address family type from an ASCII string representation,...
bool isAddressLoopback() const noexcept
Check if the value of this endpoint's address is a loopback address (127.X.X.X or ::1).
Definition Endpoint.hpp:1793
Optional< IPv4Address > getIPv4Address() const noexcept
Get the IPv4 address of this endpoint.
Definition Endpoint.hpp:1731
Optional< IPv4Endpoint > getIPv4Endpoint() const noexcept
Get the IPv4 endpoint stored by this endpoint.
Definition Endpoint.hpp:1707
PortNumber getPortNumber() const noexcept
Get the port number of the endpoint.
Definition Endpoint.hpp:1751
Optional< IPv6Endpoint > getIPv6Endpoint() const noexcept
Get the IPv6 endpoint stored by this endpoint.
Definition Endpoint.hpp:1719
static Endpoint any(AddressFamily family, PortNumber portNumber=0) noexcept
Create an endpoint with the ANY address (0.0.0.0 or ::) for a given address family.
Definition Endpoint.hpp:1227
32-bit IP address of an IPv4 endpoint.
Definition Endpoint.hpp:60
constexpr IPv4Address(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3) noexcept
Construct an IPv4 address from its 4 constituent bytes, specified in the same order as in a dot-decim...
Definition Endpoint.hpp:208
constexpr bool isPrivate() const noexcept
Check if this IPv4 address is in a private address range.
Definition Endpoint.hpp:268
constexpr IPv4Address(const in_addr &sin_addr) noexcept
Construct an IPv4 address from a native in_addr structure.
Definition Endpoint.hpp:196
static Optional< IPv4Address > getLocalAddress(std::error_code &errorCode)
Get the IPv4 address of the local host on the local network by calling connect() on a temporary conne...
constexpr bool isLoopback() const noexcept
Check if this IPv4 address is a loopback address (starts with 127).
Definition Endpoint.hpp:253
static IPv4Address resolve(const char *hostName, const char *service)
Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup,...
constexpr in_addr * get() noexcept
Get a pointer to the underlying native in_addr structure of the IPv4 address.
Definition Endpoint.hpp:280
static const IPv4Address ANY
Value representing any IPv4 address (0.0.0.0).
Definition Endpoint.hpp:62
static Optional< IPv4Address > resolve(CStringView host, std::error_code &errorCode)
Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup.
constexpr const in_addr * get() const noexcept
Get a pointer to the underlying native in_addr structure of the IPv4 address.
Definition Endpoint.hpp:291
static const IPv4Address NONE
Value representing no IPv4 address (255.255.255.255).
Definition Endpoint.hpp:64
static const IPv4Address BROADCAST
Value of the IPv4 broadcast address (255.255.255.255).
Definition Endpoint.hpp:63
constexpr bool isAny() const noexcept
Check if the value of this IPv4 address is equal to the ANY address (0.0.0.0).
Definition Endpoint.hpp:242
constexpr Array< uint8_t, 4 > toBigEndianBytes() const noexcept
Convert this IPv4 address to an array of 4 bytes in network byte order.
Definition Endpoint.hpp:228
static Optional< IPv4Address > parse(CStringView string, std::error_code &errorCode) noexcept
Parse an IPv4 address from an ASCII string in dot-decimal notation.
static Optional< IPv4Address > resolve(const char *hostName, const char *service, std::error_code &errorCode)
Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup.
static const IPv4Address LOOPBACK
Value of the standard IPv4 loopback address (127.0.0.1).
Definition Endpoint.hpp:65
constexpr bool operator==(IPv4Address other) const noexcept
Compare this address to another for equality.
Definition Endpoint.hpp:302
static IPv4Address parse(CStringView string)
Parse an IPv4 address from an ASCII string in dot-decimal notation, throwing an exception on failure.
static IPv4Address resolve(CStringView host)
Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup,...
static IPv4Address getLocalAddress()
Get the IPv4 address of the local host on the local network by calling connect() on a temporary conne...
constexpr std::strong_ordering operator<=>(IPv4Address other) const noexcept
Compare this address to another.
Definition Endpoint.hpp:314
Combined IPv4Address and PortNumber.
Definition Endpoint.hpp:584
static IPv4Endpoint getLocalEndpoint()
Get the IPv4 address and port of the local host on the local network by calling connect() on a tempor...
constexpr IPv4Endpoint(IPv4Address address, PortNumber port=0) noexcept
Construct an IPv4 endpoint from an IPv4 address and port number.
Definition Endpoint.hpp:753
constexpr IPv4Address getAddress() const noexcept
Get the address of the endpoint.
Definition Endpoint.hpp:794
constexpr IPv4Endpoint(const sockaddr_in &addr) noexcept
Construct an IPv4 endpoint from a native sockaddr_in structure.
Definition Endpoint.hpp:768
constexpr PortNumber getPortNumber() const noexcept
Get the port number of the endpoint.
Definition Endpoint.hpp:803
constexpr const sockaddr_in * get() const noexcept
Get a pointer to the underlying native sockaddr_in structure of the IPv4 endpoint.
Definition Endpoint.hpp:860
static Optional< IPv4Endpoint > getLocalEndpoint(std::error_code &errorCode)
Get the IPv4 address and port of the local host on the local network by calling connect() on a tempor...
static Optional< IPv4Endpoint > resolve(CStringView host, std::error_code &errorCode)
Resolve the IPv4 address and port number of a host through the Domain Name System (DNS) using a getad...
static Optional< IPv4Endpoint > parse(CStringView addressString, CStringView portNumberString, std::error_code &errorCode) noexcept
Parse an IPv4 address from an ASCII string in dot-decimal notation, and a port number from a decimal ...
constexpr void setPortNumber(PortNumber newPortNumber) noexcept
Set the port number of the endpoint.
Definition Endpoint.hpp:785
constexpr void setAddress(IPv4Address newAddress) noexcept
Set the address of the endpoint.
Definition Endpoint.hpp:776
constexpr sockaddr_in * get() noexcept
Get a pointer to the underlying native sockaddr_in structure of the IPv4 endpoint.
Definition Endpoint.hpp:849
constexpr bool isAddressPrivate() const noexcept
Check if this endpoint's IPv4 address is in a private address range.
Definition Endpoint.hpp:839
static Optional< IPv4Endpoint > resolve(const char *hostName, const char *service, std::error_code &errorCode)
Resolve the IPv4 address and port number of a host through the Domain Name System (DNS) using a getad...
constexpr std::strong_ordering operator<=>(IPv4Endpoint other) const noexcept
Compare this endpoint to another.
Definition Endpoint.hpp:884
static IPv4Endpoint resolve(CStringView host)
Resolve the IPv4 address and port number of a host through the Domain Name System (DNS) using a getad...
constexpr bool isAddressAny() const noexcept
Check if the value of this endpoint's IPv4 address is equal to the ANY address (0....
Definition Endpoint.hpp:813
constexpr bool isAddressLoopback() const noexcept
Check if this endpoint's IPv4 address is a loopback address (starts with 127).
Definition Endpoint.hpp:824
static IPv4Endpoint resolve(const char *hostName, const char *service)
Resolve the IPv4 address and port number of a host through the Domain Name System (DNS) using a getad...
constexpr bool operator==(IPv4Endpoint other) const noexcept
Compare this endpoint to another for equality.
Definition Endpoint.hpp:871
static IPv4Endpoint parse(CStringView string)
Parse an IPv4 address from an ASCII string in dot-decimal notation with an optional decimal port numb...
static IPv4Endpoint parse(CStringView addressString, CStringView portNumberString)
Parse an IPv4 address from an ASCII string in dot-decimal notation, and a port number from a decimal ...
static Optional< IPv4Endpoint > parse(CStringView string, std::error_code &errorCode) noexcept
Parse an IPv4 address from an ASCII string in dot-decimal notation with an optional decimal port numb...
128-bit IP address of an IPv6 endpoint.
Definition Endpoint.hpp:338
constexpr IPv6Address(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6, uint8_t byte7, uint8_t byte8, uint8_t byte9, uint8_t byte10, uint8_t byte11, uint8_t byte12, uint8_t byte13, uint8_t byte14, uint8_t byte15) noexcept
Construct an IPv6 address from its 16 constituent bytes, specified in the same order as in a string r...
Definition Endpoint.hpp:495
static const IPv6Address ANY
Value representing any IPv6 address (::).
Definition Endpoint.hpp:340
constexpr std::strong_ordering operator<=>(const IPv6Address &other) const noexcept
Compare this address to another.
Definition Endpoint.hpp:570
constexpr bool isLoopback() const noexcept
Check if this IPv6 address is equal to the LOOPBACK address (::1).
Definition Endpoint.hpp:524
constexpr IPv6Address(const in6_addr &sin6_addr) noexcept
Construct an IPv6 address from a native in6_addr structure.
Definition Endpoint.hpp:471
static Optional< IPv6Address > resolve(CStringView host, std::error_code &errorCode)
Resolve the IPv6 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup.
static Optional< IPv6Address > resolve(const char *hostName, const char *service, std::error_code &errorCode)
Resolve the IPv6 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup.
static const IPv6Address LOOPBACK
Value of the IPv6 loopback address (::1).
Definition Endpoint.hpp:341
static IPv6Address resolve(const char *hostName, const char *service)
Resolve the IPv6 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup,...
constexpr bool operator==(const IPv6Address &other) const noexcept
Compare this address to another for equality.
Definition Endpoint.hpp:558
static IPv6Address getLocalAddress()
Get the IPv6 address of the local host by calling connect() on a temporary connectionless socket to t...
static IPv6Address resolve(CStringView host)
Resolve the IPv6 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup,...
static Optional< IPv6Address > getLocalAddress(std::error_code &errorCode)
Get the IPv6 address of the local host by calling connect() on a temporary connectionless socket to t...
constexpr Array< uint8_t, 16 > toBigEndianBytes() const noexcept
Convert this IPv6 address to an array of 16 bytes in network byte order.
Definition Endpoint.hpp:504
constexpr const in6_addr * get() const noexcept
Get a pointer to the underlying native in6_addr structure of the IPv6 address.
Definition Endpoint.hpp:547
static IPv6Address parse(CStringView string)
Parse an IPv6 address from an ASCII string representation, throwing an exception on failure.
constexpr in6_addr * get() noexcept
Get a pointer to the underlying native in6_addr structure of the IPv6 address.
Definition Endpoint.hpp:536
static Optional< IPv6Address > parse(CStringView string, std::error_code &errorCode) noexcept
Parse an IPv6 address from an ASCII string representation.
constexpr bool isAny() const noexcept
Check if the value of this IPv4 address is equal to the ANY address (::).
Definition Endpoint.hpp:513
Combined IPv6Address, PortNumber and ScopeID.
Definition Endpoint.hpp:895
constexpr std::strong_ordering operator<=>(const IPv6Endpoint &other) const noexcept
Compare this endpoint to another.
Definition Endpoint.hpp:1202
static IPv6Endpoint getLocalEndpoint()
Get the IPv6 address and port of the local host by calling connect() on a temporary connectionless so...
constexpr void setAddress(const IPv6Address &newAddress) noexcept
Set the address of the endpoint.
Definition Endpoint.hpp:1090
constexpr PortNumber getPortNumber() const noexcept
Get the port number of the endpoint.
Definition Endpoint.hpp:1126
static IPv6Endpoint resolve(CStringView host)
Resolve the IPv6 address and port number of a host through the Domain Name System (DNS) using a getad...
static IPv6Endpoint resolve(const char *hostName, const char *service)
Resolve the IPv6 address and port number of a host through the Domain Name System (DNS) using a getad...
constexpr bool isAddressLoopback() const noexcept
Check if the value of this endpoint's IPv6 address is equal to the LOOPBACK address (::1).
Definition Endpoint.hpp:1155
constexpr void setPortNumber(PortNumber newPortNumber) noexcept
Set the port number of the endpoint.
Definition Endpoint.hpp:1099
static Optional< IPv6Endpoint > resolve(CStringView host, std::error_code &errorCode)
Resolve the IPv6 address and port number of a host through the Domain Name System (DNS) using a getad...
constexpr const sockaddr_in6 * get() const noexcept
Get a pointer to the underlying native sockaddr_in6 structure of the IPv6 endpoint.
Definition Endpoint.hpp:1176
constexpr sockaddr_in6 * get() noexcept
Get a pointer to the underlying native sockaddr_in6 structure of the IPv6 endpoint.
Definition Endpoint.hpp:1165
static Optional< IPv6Endpoint > parse(CStringView addressString, CStringView portNumberString, std::error_code &errorCode) noexcept
Parse an IPv6 address from an ASCII string representation, and a port number from a decimal ASCII str...
constexpr bool isAddressAny() const noexcept
Check if the value of this endpoint's IPv6 address is equal to the ANY address (::).
Definition Endpoint.hpp:1145
static IPv6Endpoint parse(CStringView addressString, CStringView portNumberString)
Parse an IPv6 address from an ASCII string representation, and a port number from a decimal ASCII str...
static IPv6Endpoint parse(CStringView string)
Parse an IPv6 address from an ASCII string representation with an optional decimal port number,...
constexpr IPv6Endpoint(const sockaddr_in6 &addr) noexcept
Construct an IPv6 endpoint from a native sockaddr_in6 structure.
Definition Endpoint.hpp:1082
constexpr void setScopeID(ScopeID newScopeID) noexcept
Set the scope/zone ID of the endpoint.
Definition Endpoint.hpp:1108
constexpr IPv6Address getAddress() const noexcept
Get the address of the endpoint.
Definition Endpoint.hpp:1117
constexpr IPv6Endpoint(const IPv6Address &address, PortNumber port=0, ScopeID scopeID=0) noexcept
Construct an IPv6 endpoint from an IPv6 address, port number and scope/zone ID.
Definition Endpoint.hpp:1066
constexpr ScopeID getScopeID() const noexcept
Get the scope/zone ID of the endpoint.
Definition Endpoint.hpp:1135
constexpr bool operator==(const IPv6Endpoint &other) const noexcept
Compare this endpoint to another for equality.
Definition Endpoint.hpp:1188
static Optional< IPv6Endpoint > parse(CStringView string, std::error_code &errorCode) noexcept
Parse an IPv6 address from an ASCII string representation with an optional decimal port number.
static Optional< IPv6Endpoint > resolve(const char *hostName, const char *service, std::error_code &errorCode)
Resolve the IPv6 address and port number of a host through the Domain Name System (DNS) using a getad...
static Optional< IPv6Endpoint > getLocalEndpoint(std::error_code &errorCode)
Get the IPv6 address and port of the local host by calling connect() on a temporary connectionless so...
Definition concepts.hpp:47
Definition concepts.hpp:35
Definition Endpoint.hpp:28
void ensurePlatformInitialized(std::error_code &errorCode)
Initialize the underlying platform of the native socket API if it has not already been initialized.
AddressFamily
Address family of an IP endpoint.
Definition Endpoint.hpp:52
@ IPv4
IPv4.
Definition Endpoint.hpp:53
@ IPv6
IPv6.
Definition Endpoint.hpp:54
uint32_t ScopeID
IPv6 scope/zone ID.
Definition Endpoint.hpp:47
uint16_t PortNumber
IP port number (0-65535).
Definition Endpoint.hpp:42
Definition aliases.hpp:9
GREM_ALWAYS_INLINE constexpr T convertHostEndianToBigEndian(T value)
Convert a value in host-endian byte order to the corresponding value in big-endian byte order.
Definition fundamentals.hpp:486
GREM_ALWAYS_INLINE constexpr T convertBigEndianToHostEndian(T value)
Convert a value in big-endian byte order to the corresponding value in host-endian byte order.
Definition fundamentals.hpp:523
void unreachable()
Definition assertions.hpp:125
constexpr std::size_t getRangeHash(input_range auto &&range)
Definition algorithms.hpp:151
constexpr std::size_t getHash(const T &value, const Ts &... otherValues)
Definition algorithms.hpp:147
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a)
Definition math.hpp:2001
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Definition Array.hpp:20
constexpr size_type size() const noexcept
Definition Array.hpp:139
constexpr pointer data() noexcept
Definition Array.hpp:79
bool skipBrackets
Definition Endpoint.hpp:2001
bool alwaysIncludePort
Definition Endpoint.hpp:2002
void formatTo(FormatOutput &output, const grem::networking::Endpoint &value) const
Definition Endpoint.hpp:2016
constexpr const char * parseFormatSpecification(const char *p)
Definition Endpoint.hpp:2004
void formatTo(FormatOutput &output, const grem::networking::IPv4Address &value) const
Definition Endpoint.hpp:1908
void formatTo(FormatOutput &output, const grem::networking::IPv4Endpoint &value) const
Definition Endpoint.hpp:1954
constexpr const char * parseFormatSpecification(const char *p)
Definition Endpoint.hpp:1946
bool alwaysIncludePort
Definition Endpoint.hpp:1944
void formatTo(FormatOutput &output, const grem::networking::IPv6Address &value) const
Definition Endpoint.hpp:1926
void formatTo(FormatOutput &output, const grem::networking::IPv6Endpoint &value) const
Definition Endpoint.hpp:1982
constexpr const char * parseFormatSpecification(const char *p)
Definition Endpoint.hpp:1970
bool skipBrackets
Definition Endpoint.hpp:1967
bool alwaysIncludePort
Definition Endpoint.hpp:1968
Definition formatting.hpp:47
void formatTo(FormatOutput &output, const auto &value) const
Definition formatting.hpp:56
Exception type for domain-specific errors originating from the grem::networking module.
Definition Error.hpp:22
static std::error_code getLastErrorCode() noexcept
std::size_t operator()(const grem::networking::Endpoint &endpoint) const
Definition Endpoint.hpp:1897
std::size_t operator()(const grem::networking::IPv4Address &address) const
Definition Endpoint.hpp:1868
std::size_t operator()(const grem::networking::IPv4Endpoint &endpoint) const
Definition Endpoint.hpp:1883
std::size_t operator()(const grem::networking::IPv6Address &address) const
Definition Endpoint.hpp:1875
std::size_t operator()(const grem::networking::IPv6Endpoint &endpoint) const
Definition Endpoint.hpp:1890