COROIO: NNet::TAddress Class Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TAddress Class Reference

A class representing an IPv4 or IPv6 address (with port). More...

#include <address.hpp>

Public Member Functions

 TAddress (const std::string &addr, int port)
 Constructs an address from a string representation and port.
 
 TAddress (sockaddr_in addr)
 Constructs an address from an IPv4 sockaddr_in structure.
 
 TAddress (sockaddr_in6 addr)
 Constructs an address from an IPv6 sockaddr_in6 structure.
 
 TAddress (sockaddr *addr, socklen_t len)
 Constructs an address from a generic sockaddr pointer.
 
 TAddress ()=default
 Default constructor. Creates an empty address.
 
const std::variant< sockaddr_in, sockaddr_in6 > & Addr () const
 Retrieves the internal variant storing the address.
 
std::pair< const sockaddr *, int > RawAddr () const
 Returns a pointer to the raw sockaddr structure and its size in bytes.
 
bool operator== (const TAddress &other) const
 Equality operator for TAddress.
 
int Domain () const
 Gets the domain (address family) of the stored address.
 
TAddress WithPort (int port) const
 Returns a new TAddress with the same IP but a different port.
 
std::string ToString () const
 Converts the address to a human-readable string format.
 

Detailed Description

A class representing an IPv4 or IPv6 address (with port).

This class acts as a generic container for both IPv4 (sockaddr_in) and IPv6 (sockaddr_in6) addresses, providing a uniform interface for common operations (e.g., string conversion, retrieving raw data).

Constructor & Destructor Documentation

◆ TAddress() [1/5]

NNet::TAddress::TAddress ( const std::string & addr,
int port )

Constructs an address from a string representation and port.

Parameters
addrA string containing an IPv4 or IPv6 address in standard notation (e.g., "127.0.0.1" or "::1").
portThe port number associated with the address.
Exceptions
std::runtime_errorThrown if the provided string cannot be resolved to a valid IP address.

◆ TAddress() [2/5]

NNet::TAddress::TAddress ( sockaddr_in addr)

Constructs an address from an IPv4 sockaddr_in structure.

Parameters
addrA valid sockaddr_in (IPv4) structure containing an IP and port.

◆ TAddress() [3/5]

NNet::TAddress::TAddress ( sockaddr_in6 addr)

Constructs an address from an IPv6 sockaddr_in6 structure.

Parameters
addrA valid sockaddr_in6 (IPv6) structure containing an IP and port.

◆ TAddress() [4/5]

NNet::TAddress::TAddress ( sockaddr * addr,
socklen_t len )

Constructs an address from a generic sockaddr pointer.

The provided pointer is assumed to point to either a sockaddr_in (for IPv4) or a sockaddr_in6 (for IPv6). The len parameter should match the actual size of the corresponding address structure.

Parameters
addrPointer to a valid sockaddr struct (IPv4 or IPv6).
lenThe size of the given addr structure.

◆ TAddress() [5/5]

NNet::TAddress::TAddress ( )
default

Default constructor. Creates an empty address.

The address variant is not set to IPv4 or IPv6 in this case. Attempting to use domain-specific functionality on an empty address can lead to undefined behavior, so ensure you assign or construct it properly before use.

Member Function Documentation

◆ Addr()

const std::variant< sockaddr_in, sockaddr_in6 > & NNet::TAddress::Addr ( ) const

Retrieves the internal variant storing the address.

The variant can hold either an IPv4 (sockaddr_in) or an IPv6 (sockaddr_in6) structure.

Returns
A constant reference to the variant holding the address.

◆ Domain()

int NNet::TAddress::Domain ( ) const

Gets the domain (address family) of the stored address.

Returns AF_INET for IPv4 or AF_INET6 for IPv6.

Returns
The address family (e.g., AF_INET or AF_INET6).

◆ operator==()

bool NNet::TAddress::operator== ( const TAddress & other) const

Equality operator for TAddress.

Two addresses are considered equal if they share the same protocol family (IPv4 vs. IPv6) and identical IP and port values.

Parameters
otherAnother TAddress instance to compare with.
Returns
True if both addresses are equal; otherwise false.

◆ RawAddr()

std::pair< const sockaddr *, int > NNet::TAddress::RawAddr ( ) const

Returns a pointer to the raw sockaddr structure and its size in bytes.

Useful when calling low-level socket functions (e.g., connect, bind, sendto) that require a pointer to a sockaddr. The size indicates whether it is IPv4 or IPv6.

Returns
A std::pair containing:
  • first: A pointer to the sockaddr (casted to sockaddr*).
  • second: The size of the sockaddr structure.

◆ ToString()

std::string NNet::TAddress::ToString ( ) const

Converts the address to a human-readable string format.

Examples:

  • IPv4: "127.0.0.1:8080"
  • IPv6: "[::1]:9090"
Returns
A string containing the IP address and port.

◆ WithPort()

TAddress NNet::TAddress::WithPort ( int port) const

Returns a new TAddress with the same IP but a different port.

This is a convenience method for changing only the port, leaving the IP portion unmodified.

Parameters
portThe new port to set.
Returns
A new TAddress instance with the same IP but updated port.

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