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);
173 const bool pure_v6_loopback
174 = myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
175 && myAddress[3] == 0 && myAddress[4] == 0
176 && myAddress[5] == 0 && myAddress[6] == 0
177 && myAddress[7] == 0 && myAddress[8] == 0
178 && myAddress[9] == 0 && myAddress[10] == 0
179 && myAddress[11] == 0 && myAddress[12] == 0
180 && myAddress[13] == 0 && myAddress[14] == 0
181 && myAddress[15] == 1;
182 if (pure_v6_loopback)
188 return isV4Mapped() && toV4().isLoopback();
192 return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
193 && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
194 && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
195 && myAddress[9] == 0 && myAddress[10] == 0 && myAddress[11] == 0
196 && myAddress[12] == 0 && myAddress[13] == 0 && myAddress[14] == 0
197 && myAddress[15] == 0;
201 return myAddress[0] == 0 && myAddress[1] == 0 && myAddress[2] == 0
202 && myAddress[3] == 0 && myAddress[4] == 0 && myAddress[5] == 0
203 && myAddress[6] == 0 && myAddress[7] == 0 && myAddress[8] == 0
204 && myAddress[9] == 0 && myAddress[10] == 0xff
205 && myAddress[11] == 0xff;
209 return myAddress[0] == 0xfe && ((myAddress[1] & 0xc0) == 0x80);
213 return myAddress[0] == 0xfe && ((myAddress[1] & 0xc0) == 0xc0);
217 return myAddress[0] == 0xff;
221 return UT_IpAddressV6({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
233 myAddress[12], myAddress[13], myAddress[14], myAddress[15]);
270 if (myType == Type::ipv4)
272 myIP.myIPv4 = ip.myIP.myIPv4;
276 myIP.myIPv6 = ip.myIP.myIPv6;
294 return myIP.myIPv4 == other.myIP.myIPv4;
296 return other.
isV6() && myIP.myIPv6 == other.myIP.myIPv6;
300 return !(*
this == other);
306 if (myType == Type::ipv4)
308 myIP.myIPv4 = ip.myIP.myIPv4;
312 myIP.myIPv6 = ip.myIP.myIPv6;
334 return myIP.myIPv4.isUnspecified();
335 return myIP.myIPv6.isUnspecified();
340 return myIP.myIPv4.isLoopback();
341 return myIP.myIPv6.isLoopback();
359 return myIP.myIPv4.toString();
360 return myIP.myIPv6.toString();
400 static constexpr uint32_t host_address_len = 32;
404 myPrefixLength(host_address_len)
407 : myAddress(address), myPrefixLength(prefix_length)
409 UT_ASSERT(myPrefixLength <= host_address_len);
412 : myAddress(net.myAddress), myPrefixLength(net.myPrefixLength)
417 myAddress = other.myAddress;
418 myPrefixLength = other.myPrefixLength;
423 return myAddress == other.myAddress
424 && myPrefixLength == other.myPrefixLength;
428 return !(*
this == other);
451 str.
format(
"{}/{}", myAddress.toString(), myPrefixLength);
461 uint16_t myPrefixLength;
468 static constexpr uint32_t host_address_len = 128;
472 myPrefixLength(host_address_len)
475 : myAddress(address), myPrefixLength(prefix_length)
477 UT_ASSERT(myPrefixLength <= host_address_len);
480 : myAddress(other.myAddress), myPrefixLength(other.myPrefixLength)
486 myAddress = other.myAddress;
487 myPrefixLength = other.myPrefixLength;
492 return myAddress == net.myAddress
493 && myPrefixLength == net.myPrefixLength;
514 str.
format(
"{}/{}", myAddress.toString(), myPrefixLength);
523 uint32_t myPrefixLength;
526 #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