libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
NetworkAddress Class Reference

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< NetworkAddressfromString (const String &str)
 Parses a string into a NetworkAddress.
 
static Result< NetworkAddressfromSockAddr (const struct sockaddr *addr, size_t len)
 Constructs from a POSIX/Windows sockaddr structure.
 

Detailed Description

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.

Example
// Resolved addresses
NetworkAddress a(Ipv4Address(192, 168, 1, 1));
CHECK(a.isIPv4());
CHECK(a.isResolved());
// Unresolved hostname
NetworkAddress b("ndi-source.local");
CHECK(b.isHostname());
CHECK_FALSE(b.isResolved());
// Auto-detection from string
auto [addr, err] = NetworkAddress::fromString("10.0.0.1");
CHECK(addr.isIPv4());
IPv4 network address.
Definition ipv4address.h:51
Dynamic array container wrapping std::vector.
Definition list.h:40
High-level network address that can represent IPv4, IPv6, or an unresolved hostname.
Definition networkaddress.h:64
static Result< NetworkAddress > fromString(const String &str)
Parses a string into a NetworkAddress.

Member Enumeration Documentation

◆ Type

The kind of address stored.

Enumerator
None 

No address set.

IPv4 

Resolved IPv4 address.

IPv6 

Resolved IPv6 address.

Hostname 

Unresolved hostname string.

Constructor & Destructor Documentation

◆ NetworkAddress() [1/3]

NetworkAddress::NetworkAddress ( const Ipv4Address addr)
inline

Constructs from an IPv4 address.

Parameters
addrThe IPv4 address.

◆ NetworkAddress() [2/3]

NetworkAddress::NetworkAddress ( const Ipv6Address addr)
inline

Constructs from an IPv6 address.

Parameters
addrThe IPv6 address.

◆ NetworkAddress() [3/3]

NetworkAddress::NetworkAddress ( const String hostname)
inlineexplicit

Constructs from a hostname string (unresolved).

Parameters
hostnameThe hostname to store.

Member Function Documentation

◆ fromSockAddr()

static Result< NetworkAddress > NetworkAddress::fromSockAddr ( const struct sockaddr addr,
size_t  len 
)
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.

Parameters
addrPointer to a sockaddr (sockaddr_in or sockaddr_in6).
lenLength of the sockaddr structure.
Returns
A Result containing the address and Error::Ok, or a null address and Error::Invalid if the sockaddr is not a recognized address family.

◆ fromString()

static Result< NetworkAddress > NetworkAddress::fromString ( const String str)
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).

Parameters
strThe string to parse.
Returns
A Result containing the address and Error::Ok, or a null address and Error::Invalid if the string is empty.

◆ hostname()

String NetworkAddress::hostname ( ) const

Returns the stored hostname.

Returns
The hostname string, or an empty string if not a hostname.

◆ isLinkLocal()

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.

◆ isLoopback()

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".

◆ isMulticast()

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.

◆ toIpv4()

Ipv4Address NetworkAddress::toIpv4 ( ) const

Returns the stored IPv4 address.

Returns
The Ipv4Address, or a null Ipv4Address if not IPv4.

◆ toIpv6()

Ipv6Address NetworkAddress::toIpv6 ( ) const

Returns the stored IPv6 address.

Returns
The Ipv6Address, or a null Ipv6Address if not IPv6.

◆ toSockAddr()

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.

Parameters
[out]storageThe structure to fill.
Returns
The size of the filled sockaddr structure (sizeof(sockaddr_in) or sizeof(sockaddr_in6)), or 0 if the address is null or unresolved.

◆ toString()

String NetworkAddress::toString ( ) const

Returns a string representation.

Returns the dotted-quad for IPv4, colon-hex for IPv6, or the hostname string.

Returns
The address as a string.

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