12 #ifndef __UT_IPADDRESS_H__
13 #define __UT_IPADDRESS_H__
44 std::memcpy(&myAddress, addr.data(), 4);
52 convert_(i0, i1, i2, i3);
55 myAddress(addr.myAddress)
59 myAddress = addr.myAddress;
65 return myAddress == other.myAddress;
69 return !(*
this == other);
73 return toUInt() < other.
toUInt();
77 return other.
toUInt() < toUInt();
81 return !(other < *
this);
85 return !(*
this < other);
94 return (toUInt() & 0xFF000000) == 0x7F000000;
98 return (toUInt() & 0xF0000000) == 0xE0000000;
103 std::memcpy(bytes.data(), &myAddress, 4);
129 void convert_(uint32_t addr);
147 return std::memcmp(myAddress.data(), other.myAddress.data(), 16) == 0;
151 return !(*
this == other);
155 return myAddress < other.myAddress;
159 return other < *
this;
163 return !(other < *
this);
167 return !(*
this < other);
172 return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
173 && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
174 && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
175 && myAddress[9] == 0 && myAddress[10] == 0 && myAddress[11] == 0
176 && myAddress[12] == 0 && myAddress[13] == 0 && myAddress[14] == 0
177 && myAddress[15] == 1;
181 return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
182 && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
183 && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
184 && myAddress[9] == 0 && myAddress[10] == 0 && myAddress[11] == 0
185 && myAddress[12] == 0 && myAddress[13] == 0 && myAddress[14] == 0
186 && myAddress[15] == 0;
190 return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
191 && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
192 && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
193 && myAddress[9] == 0 && myAddress[10] == 0xff
194 && myAddress[11] == 0xff;
198 return myAddress[0] == 0xfe && ((myAddress[1] & 0xc0) == 0x80);
202 return myAddress[0] == 0xfe && ((myAddress[1] & 0xc0) == 0xc0);
206 return myAddress[0] == 0xff;
210 return UT_IpAddressV6({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
222 myAddress[12], myAddress[13], myAddress[14], myAddress[15]);
259 if (myType == Type::ipv4)
261 myIP.myIPv4 = ip.myIP.myIPv4;
265 myIP.myIPv6 = ip.myIP.myIPv6;
283 return myIP.myIPv4 == other.myIP.myIPv4;
285 return other.
isV6() && myIP.myIPv6 == other.myIP.myIPv6;
289 return !(*
this == other);
295 if (myType == Type::ipv4)
297 myIP.myIPv4 = ip.myIP.myIPv4;
301 myIP.myIPv6 = ip.myIP.myIPv6;
323 return myIP.myIPv4.isUnspecified();
324 return myIP.myIPv6.isUnspecified();
329 return myIP.myIPv4.isLoopback();
330 return myIP.myIPv6.isLoopback();
348 return myIP.myIPv4.toString();
349 return myIP.myIPv6.toString();
389 static constexpr uint32_t host_address_len = 32;
393 myPrefixLength(host_address_len)
396 : myAddress(address), myPrefixLength(prefix_length)
398 UT_ASSERT(myPrefixLength <= host_address_len);
401 : myAddress(net.myAddress), myPrefixLength(net.myPrefixLength)
406 myAddress = other.myAddress;
407 myPrefixLength = other.myPrefixLength;
412 return myAddress == other.myAddress
413 && myPrefixLength == other.myPrefixLength;
417 return !(*
this == other);
440 str.
format(
"{}/{}", myAddress.toString(), myPrefixLength);
450 uint16_t myPrefixLength;
457 static constexpr uint32_t host_address_len = 128;
461 myPrefixLength(host_address_len)
464 : myAddress(address), myPrefixLength(prefix_length)
466 UT_ASSERT(myPrefixLength <= host_address_len);
469 : myAddress(other.myAddress), myPrefixLength(other.myPrefixLength)
475 myAddress = other.myAddress;
476 myPrefixLength = other.myPrefixLength;
481 return myAddress == net.myAddress
482 && myPrefixLength == net.myPrefixLength;
503 str.
format(
"{}/{}", myAddress.toString(), myPrefixLength);
512 uint32_t myPrefixLength;
515 #endif // __UT_IPADDRESS_H__
SYS_NO_DISCARD_RESULT UT_StringHolder toString() const
SYS_NO_DISCARD_RESULT bool isV4Mapped() const
bool operator>(const UT_IpAddressV4 &other) const
UT_IpAddressV4(const bytes_t &addr)
UT_IpAddress(const UT_IpAddress &ip)
SYS_NO_DISCARD_RESULT UT_StringHolder toString() const
UT_IpAddress(const UT_IpAddressV6 &ip)
UT_IpAddressV4(const UT_IpAddressV4 &addr)
static SYS_NO_DISCARD_RESULT UT_IpNetworkV4 loopback()
SYS_NO_DISCARD_RESULT UT_StringHolder toString() const
UT_IpAddress(const UT_IpAddressV4::bytes_t &addr)
This represents a Ipv4 address.
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 loopback()
bool operator>=(const UT_IpAddressV6 &other) const
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 broadcast()
bool operator==(const UT_IpAddressV6 &other) const
SYS_NO_DISCARD_RESULT bool isUnspecified() const
SYS_NO_DISCARD_RESULT const address_t & address() const
SYS_NO_DISCARD_RESULT uint32_t prefixLength() const
static SYS_NO_DISCARD_RESULT UT_IpAddressV6 fromString(const UT_StringRef &str)
UT_IpAddress(unsigned char i0, unsigned char i1, unsigned char i2, unsigned char i3)
bool operator==(const UT_IpAddress &other) const
SYS_NO_DISCARD_RESULT bool isHost() const
bool operator<=(const UT_IpAddressV4 &other) const
bool operator==(const UT_IpAddressV4 &other) const
UT_IpAddressV4(uint32_t addr)
bool operator!=(const UT_IpAddress &other) const
UT_IpNetworkV4 & operator=(const UT_IpNetworkV4 &other)
SYS_NO_DISCARD_RESULT UT_IpNetworkV4 canonical() const
SYS_NO_DISCARD_RESULT bool isMulticast() const
UT_IpAddress & operator=(const UT_IpAddress &ip)
bool operator>=(const UT_IpAddressV4 &other) const
This represents either an Ipv4 address or an Ipv6 address.
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 any()
std::array< T, N > UT_FixedArray
UT_IpAddressV4 & operator=(const UT_IpAddressV4 &addr)
SYS_NO_DISCARD_RESULT bool isLoopback() const
SYS_FORCE_INLINE exint findCharIndex(char c) const
Find the location of the character (or -1 if not found)
UT_IpNetworkV6(const address_t &address, uint32_t prefix_length)
SYS_NO_DISCARD_RESULT bool isV6() const
SYS_NO_DISCARD_RESULT UT_IpAddressV4 toV4() const
UT_IpAddressV4(unsigned char i0, unsigned char i1, unsigned char i2, unsigned char i3)
UT_IpAddress & operator=(const UT_IpAddressV6 &ip)
bool operator!=(const UT_IpNetworkV6 &net) const
bool operator<(const UT_IpAddressV4 &other) const
static SYS_NO_DISCARD_RESULT UT_IpAddressV4 fromString(const UT_StringRef &str)
bool operator==(const UT_IpNetworkV6 &net) const
#define SYS_NO_DISCARD_RESULT
UT_IpAddress(const UT_IpAddressV4 &ip)
UT_IpAddress(const UT_IpAddressV6::bytes_t &addr)
SYS_NO_DISCARD_RESULT bool isSiteLocal() const
bool operator>(const UT_IpAddressV6 &other) const
UT_IpNetworkV4(const address_t &address, uint16 prefix_length)
bool operator!=(const UT_IpAddressV4 &other) const
SYS_NO_DISCARD_RESULT bool isLoopback() const
size_t format(const char *fmt, const Args &...args)
Format a string using the same formatting codes as UTformat.
bool operator<(const UT_IpAddressV6 &other) const
UT_IpNetworkV6(const UT_IpNetworkV6 &other)
SYS_NO_DISCARD_RESULT address_t network() const
SYS_NO_DISCARD_RESULT const address_t & address() const
bool operator!=(const UT_IpNetworkV4 &other) const
UT_FixedArray< uint8, 4 > bytes_t
SYS_NO_DISCARD_RESULT UT_IpNetworkV6 canonical() const
SYS_NO_DISCARD_RESULT UT_IpAddressV4 toV4() const
UT_IpNetworkV4(const UT_IpNetworkV4 &net)
SYS_NO_DISCARD_RESULT bool isV4() const
This represents a Ipv6 address.
static SYS_NO_DISCARD_RESULT UT_IpAddress fromString(const UT_StringRef &str)
SYS_NO_DISCARD_RESULT UT_IpAddressV6 toV6() const
SYS_NO_DISCARD_RESULT uint32_t toUInt() const
SYS_NO_DISCARD_RESULT bool isHost() const
UT_FixedArray< uint8, 16 > bytes_t
SYS_NO_DISCARD_RESULT bool isUnspecified() const
SYS_NO_DISCARD_RESULT const bytes_t toBytes() const
SYS_NO_DISCARD_RESULT uint16_t prefixLength() const
bool operator==(const UT_IpNetworkV4 &other) const
UT_IpNetworkV6 & operator=(const UT_IpNetworkV6 &other)
SYS_NO_DISCARD_RESULT bool isMulticast() const
bool operator!=(const UT_IpAddressV6 &other) const
static SYS_NO_DISCARD_RESULT UT_IpAddressV6 loopback()
SYS_NO_DISCARD_RESULT bool isLinkLocal() const
UT_IpAddressV6(const bytes_t &addr)
bool operator<=(const UT_IpAddressV6 &other) const
UT_IpAddress & operator=(const UT_IpAddressV4 &ip)
SYS_NO_DISCARD_RESULT bool isLoopback() const
SYS_NO_DISCARD_RESULT const bytes_t & toBytes() const
static SYS_NO_DISCARD_RESULT UT_IpNetworkV6 loopback()
SYS_NO_DISCARD_RESULT bool isUnspecified() const