GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::networking::IPv4Address Class Reference

32-bit IP address of an IPv4 endpoint. More...

#include <GREM/networking/Endpoint.hpp>

Public Member Functions

constexpr IPv4Address (const in_addr &sin_addr) noexcept
 Construct an IPv4 address from a native in_addr structure.
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-decimal notation string.
constexpr Array< uint8_t, 4 > toBigEndianBytes () const noexcept
 Convert this IPv4 address to an array of 4 bytes in network byte order.
constexpr bool isAny () const noexcept
 Check if the value of this IPv4 address is equal to the ANY address (0.0.0.0).
constexpr bool isLoopback () const noexcept
 Check if this IPv4 address is a loopback address (starts with 127).
constexpr bool isPrivate () const noexcept
 Check if this IPv4 address is in a private address range.
constexpr in_addr * get () noexcept
 Get a pointer to the underlying native in_addr structure of the IPv4 address.
constexpr const in_addr * get () const noexcept
 Get a pointer to the underlying native in_addr structure of the IPv4 address.
constexpr bool operator== (IPv4Address other) const noexcept
 Compare this address to another for equality.
constexpr std::strong_ordering operator<=> (IPv4Address other) const noexcept
 Compare this address to another.

Static Public Member Functions

static Optional< IPv4Address > parse (CStringView string, std::error_code &errorCode) noexcept
 Parse an IPv4 address from an ASCII string in dot-decimal notation.
static IPv4Address parse (CStringView string)
 Parse an IPv4 address from an ASCII string in dot-decimal notation, throwing an exception on failure.
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 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, throwing an exception on failure.
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.
static IPv4Address resolve (CStringView host)
 Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup, throwing an exception on failure.
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 connectionless socket to the loopback address on port 9 (the "Discard" service) and querying its local address with getsockname().
static IPv4Address getLocalAddress ()
 Get the IPv4 address of the local host on the local network by calling connect() on a temporary connectionless socket to the loopback address on port 9 (the "Discard" service) and querying its local address with getsockname(), throwing an exception on failure.

Static Public Attributes

static const IPv4Address ANY {0, 0, 0, 0}
 Value representing any IPv4 address (0.0.0.0).
static const IPv4Address BROADCAST {255, 255, 255, 255}
 Value of the IPv4 broadcast address (255.255.255.255).
static const IPv4Address NONE {255, 255, 255, 255}
 Value representing no IPv4 address (255.255.255.255).
static const IPv4Address LOOPBACK {127, 0, 0, 1}
 Value of the standard IPv4 loopback address (127.0.0.1).

Detailed Description

32-bit IP address of an IPv4 endpoint.

Constructor & Destructor Documentation

◆ IPv4Address() [1/2]

grem::networking::IPv4Address::IPv4Address ( const in_addr & sin_addr)
inlineexplicitconstexprnoexcept

Construct an IPv4 address from a native in_addr structure.

Parameters
sin_addrin_addr structure to construct the IPv4 address from.

◆ IPv4Address() [2/2]

grem::networking::IPv4Address::IPv4Address ( uint8_t byte0,
uint8_t byte1,
uint8_t byte2,
uint8_t byte3 )
inlineconstexprnoexcept

Construct an IPv4 address from its 4 constituent bytes, specified in the same order as in a dot-decimal notation string.

Parameters
byte0first byte value.
byte1second byte value.
byte2third byte value.
byte3fourth byte value.

Member Function Documentation

◆ parse() [1/2]

Optional< IPv4Address > grem::networking::IPv4Address::parse ( CStringView string,
std::error_code & errorCode )
staticnodiscardnoexcept

Parse an IPv4 address from an ASCII string in dot-decimal notation.

Parameters
stringstring containing the four-decimal address to parse.
errorCodeerror code that is filled in on failure to parse a valid IPv4 address, or cleared on success.
Returns
the parsed IPv4 address, or an empty optional if parsing failed.

◆ parse() [2/2]

IPv4Address grem::networking::IPv4Address::parse ( CStringView string)
staticnodiscard

Parse an IPv4 address from an ASCII string in dot-decimal notation, throwing an exception on failure.

Parameters
stringstring containing the four-decimal address to parse.
Returns
the parsed IPv4 address.
Exceptions
networking::Erroron failure to parse a valid IPv4 address.
std::bad_allocon allocation failure.

◆ resolve() [1/4]

Optional< IPv4Address > grem::networking::IPv4Address::resolve ( const char * hostName,
const char * service,
std::error_code & errorCode )
staticnodiscard

Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup.

Parameters
hostNamenull-terminated domain name or address string of the host to look up, e.g. "example.com" or "127.0.0.1", or nullptr to look up by service only. Must not be nullptr if service is nullptr.
servicenull-terminated service or port string of the host to look up, e.g. "http" or "80", or nullptr to look up by host name only. Must not be nullptr if hostName is nullptr.
errorCodeerror code that is filled in on failure to resolve the host, or cleared on success.
Returns
the resolved IPv4 address, or an empty optional if resolution failed.

◆ resolve() [2/4]

IPv4Address grem::networking::IPv4Address::resolve ( const char * hostName,
const char * service )
staticnodiscard

Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup, throwing an exception on failure.

Parameters
hostNamenull-terminated domain name or address string of the host to look up, e.g. "example.com" or "127.0.0.1", or nullptr to look up by service only. Must not be nullptr if service is nullptr.
servicenull-terminated service or port string of the host to look up, e.g. "http" or "80", or nullptr to look up by host name only. Must not be nullptr if hostName is nullptr.
Returns
the resolved IPv4 address.
Exceptions
networking::Erroron failure to resolve the host.
std::bad_allocon allocation failure.

◆ resolve() [3/4]

Optional< IPv4Address > grem::networking::IPv4Address::resolve ( CStringView host,
std::error_code & errorCode )
staticnodiscard

Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup.

Parameters
hostcombined host name and service string separated by a colon, e.g. "example.com:http" or "127.0.0.1:80", or a host name-only string with no colon, e.g. "example.com" or "127.0.0.1".
errorCodeerror code that is filled in on failure to resolve the host, or cleared on success.
Returns
the resolved IPv4 address, or an empty optional if resolution failed.
Exceptions
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.

◆ resolve() [4/4]

IPv4Address grem::networking::IPv4Address::resolve ( CStringView host)
staticnodiscard

Resolve the IPv4 address of a host through the Domain Name System (DNS) using a getaddrinfo() lookup, throwing an exception on failure.

Parameters
hostcombined host name and service string separated by a colon, e.g. "example.com:http" or "127.0.0.1:80", or a host name-only string with no colon, e.g. "example.com" or "127.0.0.1".
Returns
the resolved IPv4 address.
Exceptions
networking::Erroron failure to resolve the host.
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.

◆ getLocalAddress() [1/2]

Optional< IPv4Address > grem::networking::IPv4Address::getLocalAddress ( std::error_code & errorCode)
staticnodiscard

Get the IPv4 address of the local host on the local network by calling connect() on a temporary connectionless socket to the loopback address on port 9 (the "Discard" service) and querying its local address with getsockname().

Parameters
errorCodeerror code that is filled in on failure to get the local address, or cleared on success.
Returns
the local IPv4 address (typically a private Class C address like "192.168.1.2"), or an empty optional on failure.

◆ getLocalAddress() [2/2]

IPv4Address grem::networking::IPv4Address::getLocalAddress ( )
staticnodiscard

Get the IPv4 address of the local host on the local network by calling connect() on a temporary connectionless socket to the loopback address on port 9 (the "Discard" service) and querying its local address with getsockname(), throwing an exception on failure.

Returns
the local IPv4 address (typically a private Class C address like "192.168.1.2").
Exceptions
networking::Erroron failure to get the local address.
std::bad_allocon allocation failure.

◆ toBigEndianBytes()

Array< uint8_t, 4 > grem::networking::IPv4Address::toBigEndianBytes ( ) const
inlinenodiscardconstexprnoexcept

Convert this IPv4 address to an array of 4 bytes in network byte order.

Returns
a big-endian array of the 4 byte values of the address.

◆ isAny()

bool grem::networking::IPv4Address::isAny ( ) const
inlinenodiscardconstexprnoexcept

Check if the value of this IPv4 address is equal to the ANY address (0.0.0.0).

Returns
*this == IPv4Address::ANY.

◆ isLoopback()

bool grem::networking::IPv4Address::isLoopback ( ) const
inlinenodiscardconstexprnoexcept

Check if this IPv4 address is a loopback address (starts with 127).

Returns
true if this is a loopback address, false otherwise.

◆ isPrivate()

bool grem::networking::IPv4Address::isPrivate ( ) const
inlinenodiscardconstexprnoexcept

Check if this IPv4 address is in a private address range.

The address ranges reserved for private networks are:

  • 10.0.0.0 - 10.255.255.255
  • 172.16.0.0 - 172.31.255.255
  • 192.168.0.0 - 192.168.255.255
Returns
true if this is a private address, false otherwise.

◆ get() [1/2]

in_addr * grem::networking::IPv4Address::get ( )
inlinenodiscardconstexprnoexcept

Get a pointer to the underlying native in_addr structure of the IPv4 address.

Returns
a non-owning pointer to the underlying in_addr structure.

◆ get() [2/2]

const in_addr * grem::networking::IPv4Address::get ( ) const
inlinenodiscardconstexprnoexcept

Get a pointer to the underlying native in_addr structure of the IPv4 address.

Returns
a non-owning read-only pointer to the underlying in_addr structure.

◆ operator==()

bool grem::networking::IPv4Address::operator== ( IPv4Address other) const
inlinenodiscardconstexprnoexcept

Compare this address to another for equality.

Parameters
otherthe address to compare this one to.
Returns
true if the addresses are equal, false otherwise.

◆ operator<=>()

std::strong_ordering grem::networking::IPv4Address::operator<=> ( IPv4Address other) const
inlinenodiscardconstexprnoexcept

Compare this address to another.

Parameters
otherthe address to compare this one to.
Returns
a strong ordering between the two addresses, based on the lexicographical ordering of their big-endian byte value arrays.

Member Data Documentation

◆ ANY

IPv4Address grem::networking::IPv4Address::ANY {0, 0, 0, 0}
inlinestaticconstexpr

Value representing any IPv4 address (0.0.0.0).

◆ BROADCAST

IPv4Address grem::networking::IPv4Address::BROADCAST {255, 255, 255, 255}
inlinestaticconstexpr

Value of the IPv4 broadcast address (255.255.255.255).

◆ NONE

IPv4Address grem::networking::IPv4Address::NONE {255, 255, 255, 255}
inlinestaticconstexpr

Value representing no IPv4 address (255.255.255.255).

◆ LOOPBACK

IPv4Address grem::networking::IPv4Address::LOOPBACK {127, 0, 0, 1}
inlinestaticconstexpr

Value of the standard IPv4 loopback address (127.0.0.1).


The documentation for this class was generated from the following file: