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. | |
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).
NNet::TAddress::TAddress | ( | const std::string & | addr, |
int | port ) |
Constructs an address from a string representation and port.
addr | A string containing an IPv4 or IPv6 address in standard notation (e.g., "127.0.0.1" or "::1"). |
port | The port number associated with the address. |
std::runtime_error | Thrown if the provided string cannot be resolved to a valid IP address. |
NNet::TAddress::TAddress | ( | sockaddr_in | addr | ) |
Constructs an address from an IPv4 sockaddr_in structure.
addr | A valid sockaddr_in (IPv4) structure containing an IP and port. |
NNet::TAddress::TAddress | ( | sockaddr_in6 | addr | ) |
Constructs an address from an IPv6 sockaddr_in6 structure.
addr | A valid sockaddr_in6 (IPv6) structure containing an IP and port. |
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.
addr | Pointer to a valid sockaddr struct (IPv4 or IPv6). |
len | The size of the given addr structure. |
|
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.
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.
int NNet::TAddress::Domain | ( | ) | const |
Gets the domain (address family) of the stored address.
Returns AF_INET for IPv4 or AF_INET6 for IPv6.
bool NNet::TAddress::operator== | ( | const TAddress & | other | ) | const |
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.
first
: A pointer to the sockaddr (casted to sockaddr*).second
: The size of the sockaddr structure. std::string NNet::TAddress::ToString | ( | ) | const |
Converts the address to a human-readable string format.
Examples:
TAddress NNet::TAddress::WithPort | ( | int | port | ) | const |