EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
SockAddr Class Reference

Overview

A wrapper for the EtcPal socket address type.

Provides C++ syntactic sugar for working with socket addresses, which contain an IP address and port.

Public Member Functions

ETCPAL_CONSTEXPR_14 SockAddr () noexcept
 Constructs an invalid SockAddr by default.
 
constexpr SockAddr (const EtcPalSockAddr &c_sa) noexcept
 Construct a SockAddr copied from an instance of the C EtcPalSockAddr type.
 
SockAddroperator= (const EtcPalSockAddr &c_sa) noexcept
 Assign an instance of the C EtcPalSockAddr type to an instance of this class.
 
ETCPAL_CONSTEXPR_14 SockAddr (uint32_t v4_data, uint16_t port) noexcept
 Construct a SockAddr from a raw 32-bit IPv4 representation and port number. More...
 
 SockAddr (const uint8_t *v6_data, uint16_t port) noexcept
 Construct a SockAddr from a raw 16-byte IPv6 representation and port number. More...
 
 SockAddr (const uint8_t *v6_data, unsigned long scope_id, uint16_t port) noexcept
 Construct a Sockaddr from a raw 16-byte IPv6 representation with scope ID and port number. More...
 
ETCPAL_CONSTEXPR_14 SockAddr (IpAddr ip, uint16_t port) noexcept
 Construct a SockAddr from an IpAddr and port number. More...
 
constexpr const EtcPalSockAddrget () const noexcept
 Get a const reference to the underlying C type.
 
ETCPAL_CONSTEXPR_14 EtcPalSockAddrget () noexcept
 Get a mutable reference to the underlying C type.
 
std::string ToString () const
 Convert the IP address and port to a string representation. More...
 
constexpr IpAddr ip () const noexcept
 Get the IP address from the SockAddr.
 
constexpr uint16_t port () const noexcept
 Get the port number from the SockAddr.
 
constexpr uint32_t v4_data () const noexcept
 Get the raw 32-bit representation of the SockAddr's IPv4 address. More...
 
constexpr const uint8_t * v6_data () const noexcept
 Get the raw 16-byte array representation of the SockAddr's IPv6 address. More...
 
std::array< uint8_t, ETCPAL_IPV6_BYTESToV6Array () const
 Get a 16-byte std::array representation of the SockAddr's IPv6 address. More...
 
constexpr unsigned long scope_id () const noexcept
 Get the scope ID of the SockAddr's IPv6 address. More...
 
constexpr bool IsValid () const noexcept
 Whether a SockAddr contains a valid IPv4 or IPv6 address.
 
constexpr IpAddrType type () const noexcept
 Get the type of the SockAddr's IP address.
 
constexpr etcpal_iptype_t raw_type () const noexcept
 Get the underlying etcpal C type of the SockAddr's IP address.
 
constexpr bool IsV4 () const noexcept
 Whether a SockAddr contains a valid IPv4 address.
 
constexpr bool IsV6 () const noexcept
 Whether a SockAddr contains a valid IPv6 address.
 
bool IsLinkLocal () const noexcept
 Whether a SockAddr contains a link-local address.
 
bool IsLoopback () const noexcept
 Whether a SockAddr contains a loopback address.
 
bool IsMulticast () const noexcept
 Whether a SockAddr contains a multicast address.
 
bool IsWildcard () const noexcept
 Whether a SockAddr contains a wildcard address. More...
 
void SetAddress (uint32_t v4_data) noexcept
 Set the IPv4 address data. More...
 
void SetAddress (const uint8_t *v6_data) noexcept
 Set the IPv6 address data. More...
 
void SetAddress (const uint8_t *v6_data, unsigned long scope_id) noexcept
 Set the IPv6 address data and scope ID. More...
 
void SetAddress (const IpAddr &ip) noexcept
 Set the IP address using an IpAddr instance. More...
 
void SetPort (uint16_t port) noexcept
 Set the port. More...
 

Constructor & Destructor Documentation

◆ SockAddr() [1/4]

ETCPAL_CONSTEXPR_14_OR_INLINE SockAddr ( uint32_t  v4_data,
uint16_t  port 
)
noexcept

Construct a SockAddr from a raw 32-bit IPv4 representation and port number.

Parameters
[in]v4_dataThe address data in host byte order.
[in]portThe port number in host byte order.

◆ SockAddr() [2/4]

SockAddr ( const uint8_t *  v6_data,
uint16_t  port 
)
inlinenoexcept

Construct a SockAddr from a raw 16-byte IPv6 representation and port number.

Parameters
[in]v6_dataPointer to the address data, an array of length ETCPAL_IPV6_BYTES.
[in]portThe port number in host byte order.

◆ SockAddr() [3/4]

SockAddr ( const uint8_t *  v6_data,
unsigned long  scope_id,
uint16_t  port 
)
inlinenoexcept

Construct a Sockaddr from a raw 16-byte IPv6 representation with scope ID and port number.

See IpAddr::scope_id() for more information on scope IDs.

Parameters
[in]v6_dataPointer to the address data, an array of length ETCPAL_IPV6_BYTES.
[in]scope_idThe Scope ID value.
[in]portThe port number in host byte order.

◆ SockAddr() [4/4]

ETCPAL_CONSTEXPR_14_OR_INLINE SockAddr ( IpAddr  ip,
uint16_t  port 
)
noexcept

Construct a SockAddr from an IpAddr and port number.

Parameters
[in]ipThe IP address.
[in]portThe port number in host byte order.

Member Function Documentation

◆ IsWildcard()

bool IsWildcard ( ) const
inlinenoexcept

Whether a SockAddr contains a wildcard address.

See etcpal_ip_is_wildcard() for more information.

◆ scope_id()

constexpr unsigned long scope_id ( ) const
constexprnoexcept

Get the scope ID of the SockAddr's IPv6 address.

Scope IDs (sometimes referred to as Zone IDs or Zone Indices) are a lightly-documented portion of IPv6 addressing, but one concrete use is to indicate the network interface index of a link-local IPv6 address. See also Network Interface Indexes.

This function will return undefined data if the SockAddr's type is V4 or Invalid.

Returns
The scope ID.

◆ SetAddress() [1/4]

void SetAddress ( const IpAddr ip)
inlinenoexcept

Set the IP address using an IpAddr instance.

Parameters
[in]ipIpAddr to copy into this SockAddr's underlying IP address.

◆ SetAddress() [2/4]

void SetAddress ( const uint8_t *  v6_data)
inlinenoexcept

Set the IPv6 address data.

Automatically converts this address's type to V6.

Parameters
[in]v6_dataPointer to the address data, an array of length ETCPAL_IPV6_BYTES.

◆ SetAddress() [3/4]

void SetAddress ( const uint8_t *  v6_data,
unsigned long  scope_id 
)
inlinenoexcept

Set the IPv6 address data and scope ID.

See IpAddr::scope_id() for more information.

Parameters
[in]v6_dataPointer to the address data, an array of length ETCPAL_IPV6_BYTES.
[in]scope_idThe address's scope ID.

◆ SetAddress() [4/4]

void SetAddress ( uint32_t  v4_data)
inlinenoexcept

Set the IPv4 address data.

Automatically converts this address's type to V4.

Parameters
[in]v4_dataThe address data in host byte order.

◆ SetPort()

void SetPort ( uint16_t  port)
inlinenoexcept

Set the port.

Parameters
[in]portThe port number.

◆ ToString()

std::string ToString ( ) const
inline

Convert the IP address and port to a string representation.

The string will be of the form ddd.ddd.ddd.ddd:ppppp for IPv4 addresses, and [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:ppppp for IPv6 addresses (common conventions and rules for compressing these representations will also apply).

◆ ToV6Array()

std::array< uint8_t, ETCPAL_IPV6_BYTES > ToV6Array ( ) const
inline

Get a 16-byte std::array representation of the SockAddr's IPv6 address.

Copies the data into a new array. This function will return undefined data if the SockAddr's type is V4 or Invalid.

◆ v4_data()

constexpr uint32_t v4_data ( ) const
constexprnoexcept

Get the raw 32-bit representation of the SockAddr's IPv4 address.

This function will return undefined data if the SockAddr's type is V6 or Invalid.

Returns
The address data in host byte order.

◆ v6_data()

constexpr const uint8_t * v6_data ( ) const
constexprnoexcept

Get the raw 16-byte array representation of the SockAddr's IPv6 address.

The returned pointer has the same lifetime as this SockAddr instance. This function will return undefined data if the SockAddr's type is V4 or Invalid.


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