High-level network address that can represent IPv4, IPv6, or an unresolved hostname. More...
#include <networkaddress.h>
Public Types | |
| enum | Type { None = 0 , IPv4 , IPv6 , Hostname } |
| The kind of address stored. More... | |
Public Member Functions | |
| NetworkAddress ()=default | |
| Default constructor. Creates a null address. | |
| NetworkAddress (const Ipv4Address &addr) | |
| Constructs from an IPv4 address. | |
| NetworkAddress (const Ipv6Address &addr) | |
| Constructs from an IPv6 address. | |
| NetworkAddress (const String &hostname) | |
| Constructs from a hostname string (unresolved). | |
| Type | type () const |
| Returns the type of address stored. | |
| bool | isNull () const |
| Returns true if no address is stored. | |
| bool | isIPv4 () const |
| Returns true if this holds an IPv4 address. | |
| bool | isIPv6 () const |
| Returns true if this holds an IPv6 address. | |
| bool | isHostname () const |
| Returns true if this holds an unresolved hostname. | |
| bool | isResolved () const |
| Returns true if this holds a resolved IP address (IPv4 or IPv6). | |
| Ipv4Address | toIpv4 () const |
| Returns the stored IPv4 address. | |
| Ipv6Address | toIpv6 () const |
| Returns the stored IPv6 address. | |
| String | hostname () const |
| Returns the stored hostname. | |
| bool | isLoopback () const |
| Returns true if the address is a loopback address. | |
| bool | isMulticast () const |
| Returns true if the address is a multicast address. | |
| bool | isLinkLocal () const |
| Returns true if the address is link-local. | |
| String | toString () const |
| Returns a string representation. | |
| size_t | toSockAddr (struct sockaddr_storage *storage) const |
| Fills a sockaddr_storage with this address. | |
| bool | operator== (const NetworkAddress &other) const |
| Equality comparison. | |
| bool | operator!= (const NetworkAddress &other) const |
| Inequality comparison. | |
Static Public Member Functions | |
| static Result< NetworkAddress > | fromString (const String &str) |
| Parses a string into a NetworkAddress. | |
| static Result< NetworkAddress > | fromSockAddr (const struct sockaddr *addr, size_t len) |
| Constructs from a POSIX/Windows sockaddr structure. | |
High-level network address that can represent IPv4, IPv6, or an unresolved hostname.
NetworkAddress is a polymorphic address type that holds either a resolved IP address (Ipv4Address or Ipv6Address) or an unresolved hostname string. It uses std::variant internally for type-safe storage without virtual dispatch.
When constructed from a string, it first attempts to parse as an IPv4 literal, then IPv6, and finally stores the string as an unresolved hostname. DNS resolution is not performed automatically; use DnsResolver to resolve hostname addresses.
This class is not thread-safe. Concurrent access to a single instance requires external synchronization.
|
inline |
Constructs from an IPv4 address.
| addr | The IPv4 address. |
|
inline |
Constructs from an IPv6 address.
| addr | The IPv6 address. |
Constructs from a hostname string (unresolved).
| hostname | The hostname to store. |
|
static |
Constructs from a POSIX/Windows sockaddr structure.
Extracts the IP address from a sockaddr_in or sockaddr_in6. The port is ignored; use the port from the sockaddr separately.
| addr | Pointer to a sockaddr (sockaddr_in or sockaddr_in6). |
| len | Length of the sockaddr structure. |
|
static |
Parses a string into a NetworkAddress.
Tries IPv4 literal, then IPv6 literal, then stores as hostname. Never fails (any non-empty string is a valid hostname).
| str | The string to parse. |
| String NetworkAddress::hostname | ( | ) | const |
Returns the stored hostname.
| bool NetworkAddress::isLinkLocal | ( | ) | const |
Returns true if the address is link-local.
Returns true for 169.254.0.0/16 (IPv4) or fe80::/10 (IPv6). Always returns false for hostnames.
| bool NetworkAddress::isLoopback | ( | ) | const |
Returns true if the address is a loopback address.
Returns true for 127.0.0.0/8, ::1, or the hostname "localhost".
| bool NetworkAddress::isMulticast | ( | ) | const |
Returns true if the address is a multicast address.
Returns true for 224.0.0.0/4 (IPv4) or ff00::/8 (IPv6). Always returns false for hostnames.
| Ipv4Address NetworkAddress::toIpv4 | ( | ) | const |
Returns the stored IPv4 address.
| Ipv6Address NetworkAddress::toIpv6 | ( | ) | const |
Returns the stored IPv6 address.
| size_t NetworkAddress::toSockAddr | ( | struct sockaddr_storage * | storage | ) | const |
Fills a sockaddr_storage with this address.
Only works for resolved IPv4 or IPv6 addresses. The port field in the sockaddr is set to zero; callers should set it separately via sin_port / sin6_port.
| [out] | storage | The structure to fill. |
| String NetworkAddress::toString | ( | ) | const |
Returns a string representation.
Returns the dotted-quad for IPv4, colon-hex for IPv6, or the hostname string.