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

Combined IPv4Address and PortNumber. More...

#include <GREM/networking/Endpoint.hpp>

Public Member Functions

constexpr IPv4Endpoint (IPv4Address address, PortNumber port=0) noexcept
 Construct an IPv4 endpoint from an IPv4 address and port number.
constexpr IPv4Endpoint (const sockaddr_in &addr) noexcept
 Construct an IPv4 endpoint from a native sockaddr_in structure.
constexpr void setAddress (IPv4Address newAddress) noexcept
 Set the address of the endpoint.
constexpr void setPortNumber (PortNumber newPortNumber) noexcept
 Set the port number of the endpoint.
constexpr IPv4Address getAddress () const noexcept
 Get the address of the endpoint.
constexpr PortNumber getPortNumber () const noexcept
 Get the port number of the endpoint.
constexpr bool isAddressAny () const noexcept
 Check if the value of this endpoint's IPv4 address is equal to the ANY address (0.0.0.0).
constexpr bool isAddressLoopback () const noexcept
 Check if this endpoint's IPv4 address is a loopback address (starts with 127).
constexpr bool isAddressPrivate () const noexcept
 Check if this endpoint's IPv4 address is in a private address range.
constexpr sockaddr_in * get () noexcept
 Get a pointer to the underlying native sockaddr_in structure of the IPv4 endpoint.
constexpr const sockaddr_in * get () const noexcept
 Get a pointer to the underlying native sockaddr_in structure of the IPv4 endpoint.
constexpr bool operator== (IPv4Endpoint other) const noexcept
 Compare this endpoint to another for equality.
constexpr std::strong_ordering operator<=> (IPv4Endpoint other) const noexcept
 Compare this endpoint to another.

Static Public Member Functions

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 ASCII string.
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 ASCII string, throwing an exception on failure.
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 number.
static IPv4Endpoint parse (CStringView string)
 Parse an IPv4 address from an ASCII string in dot-decimal notation with an optional decimal port number, throwing an exception on failure.
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 getaddrinfo() lookup.
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 getaddrinfo() lookup, throwing an exception on failure.
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 getaddrinfo() lookup.
static IPv4Endpoint resolve (CStringView host)
 Resolve the IPv4 address and port number of a host through the Domain Name System (DNS) using a getaddrinfo() lookup, throwing an exception on failure.
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 temporary connectionless socket to the loopback address on port 9 (the "Discard" service) and querying its local address with getsockname().
static IPv4Endpoint getLocalEndpoint ()
 Get the IPv4 address and port 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.

Detailed Description

Combined IPv4Address and PortNumber.

Constructor & Destructor Documentation

◆ IPv4Endpoint() [1/2]

grem::networking::IPv4Endpoint::IPv4Endpoint ( IPv4Address address,
PortNumber port = 0 )
inlineexplicitconstexprnoexcept

Construct an IPv4 endpoint from an IPv4 address and port number.

Parameters
addressIPv4 address of the endpoint.
portport number of the endpoint.

◆ IPv4Endpoint() [2/2]

grem::networking::IPv4Endpoint::IPv4Endpoint ( const sockaddr_in & addr)
inlineexplicitconstexprnoexcept

Construct an IPv4 endpoint from a native sockaddr_in structure.

Parameters
addrsockaddr_in structure to construct the IPv4 endpoint from.

Member Function Documentation

◆ parse() [1/4]

Optional< IPv4Endpoint > grem::networking::IPv4Endpoint::parse ( CStringView addressString,
CStringView portNumberString,
std::error_code & errorCode )
staticnodiscardnoexcept

Parse an IPv4 address from an ASCII string in dot-decimal notation, and a port number from a decimal ASCII string.

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

◆ parse() [2/4]

IPv4Endpoint grem::networking::IPv4Endpoint::parse ( CStringView addressString,
CStringView portNumberString )
staticnodiscard

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

Parameters
addressStringstring containing the four-decimal address to parse.
portNumberStringstring containing the decimal port number to parse.
Returns
the parsed IPv4 endpoint.
Exceptions
networking::Erroron failure to parse a valid IPv4 address or port number.
std::bad_allocon allocation failure.

◆ parse() [3/4]

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

Parse an IPv4 address from an ASCII string in dot-decimal notation with an optional decimal port number.

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

◆ parse() [4/4]

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

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

Parameters
stringstring containing the four-decimal address to parse, and potentially a decimal port number separated by a colon.
Returns
the parsed IPv4 endpoint.
Exceptions
networking::Erroron failure to parse a valid IPv4 address or port number.
std::bad_allocon allocation failure.

◆ resolve() [1/4]

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

Resolve the IPv4 address and port number 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 endpoint, or an empty optional if resolution failed.

◆ resolve() [2/4]

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

Resolve the IPv4 address and port number 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 endpoint.
Exceptions
networking::Erroron failure to resolve the host.
std::bad_allocon allocation failure.

◆ resolve() [3/4]

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

Resolve the IPv4 address and port number 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 endpoint, 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]

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

Resolve the IPv4 address and port number 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 endpoint.
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.

◆ getLocalEndpoint() [1/2]

Optional< IPv4Endpoint > grem::networking::IPv4Endpoint::getLocalEndpoint ( std::error_code & errorCode)
staticnodiscard

Get the IPv4 address and port 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 endpoint (typically with a private Class C address like "192.168.1.2"), or an empty optional on failure.

◆ getLocalEndpoint() [2/2]

IPv4Endpoint grem::networking::IPv4Endpoint::getLocalEndpoint ( )
staticnodiscard

Get the IPv4 address and port 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 endpoint (typically with a private Class C address like "192.168.1.2").
Exceptions
networking::Erroron failure to get the local address.
std::bad_allocon allocation failure.

◆ setAddress()

void grem::networking::IPv4Endpoint::setAddress ( IPv4Address newAddress)
inlineconstexprnoexcept

Set the address of the endpoint.

Parameters
newAddressnew address to set.

◆ setPortNumber()

void grem::networking::IPv4Endpoint::setPortNumber ( PortNumber newPortNumber)
inlineconstexprnoexcept

Set the port number of the endpoint.

Parameters
newPortNumbernew port number to set.

◆ getAddress()

IPv4Address grem::networking::IPv4Endpoint::getAddress ( ) const
inlinenodiscardconstexprnoexcept

Get the address of the endpoint.

Returns
the endpoint's address.

◆ getPortNumber()

PortNumber grem::networking::IPv4Endpoint::getPortNumber ( ) const
inlinenodiscardconstexprnoexcept

Get the port number of the endpoint.

Returns
the endpoint's port number.

◆ isAddressAny()

bool grem::networking::IPv4Endpoint::isAddressAny ( ) const
inlinenodiscardconstexprnoexcept

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

Returns
getAddress().isAny().

◆ isAddressLoopback()

bool grem::networking::IPv4Endpoint::isAddressLoopback ( ) const
inlinenodiscardconstexprnoexcept

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

Returns
true if the endpoint's address is a loopback address, false otherwise.

◆ isAddressPrivate()

bool grem::networking::IPv4Endpoint::isAddressPrivate ( ) const
inlinenodiscardconstexprnoexcept

Check if this endpoint's 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 the endpoint's address is a private address, false otherwise.

◆ get() [1/2]

sockaddr_in * grem::networking::IPv4Endpoint::get ( )
inlinenodiscardconstexprnoexcept

Get a pointer to the underlying native sockaddr_in structure of the IPv4 endpoint.

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

◆ get() [2/2]

const sockaddr_in * grem::networking::IPv4Endpoint::get ( ) const
inlinenodiscardconstexprnoexcept

Get a pointer to the underlying native sockaddr_in structure of the IPv4 endpoint.

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

◆ operator==()

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

Compare this endpoint to another for equality.

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

◆ operator<=>()

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

Compare this endpoint to another.

Parameters
otherthe endpoint to compare this one to.
Returns
a strong ordering between the two endpoints, based on the lexicographical ordering of their addresses' big-endian byte value arrays, or their port numbers if the addresses are equal.

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