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

IPv4 network address. More...

#include <ipv4address.h>

Public Member Functions

 Ipv4Address ()
 Default constructor. Creates a null (0.0.0.0) address.
 
 Ipv4Address (uint32_t networkOrder)
 Constructs from a 32-bit integer in network byte order.
 
 Ipv4Address (uint8_t a, uint8_t b, uint8_t c, uint8_t d)
 Constructs from four octets.
 
bool isNull () const
 Returns true if the address is 0.0.0.0.
 
bool isLoopback () const
 Returns true if the address is in the 127.0.0.0/8 range.
 
bool isMulticast () const
 Returns true if the address is in the 224.0.0.0/4 multicast range.
 
bool isLinkLocal () const
 Returns true if the address is in the 169.254.0.0/16 link-local range.
 
bool isPrivate () const
 Returns true if the address is in a private range.
 
bool isBroadcast () const
 Returns true if the address is 255.255.255.255.
 
bool isInSubnet (Ipv4Address network, Ipv4Address mask) const
 Returns true if this address is within the given subnet.
 
bool isInSubnet (Ipv4Address network, int prefixLen) const
 Returns true if this address is within the given subnet.
 
uint32_t toUint32 () const
 Returns the address as a 32-bit integer in network byte order.
 
uint8_t octet (int index) const
 Returns a single octet of the address.
 
String toString () const
 Returns a dotted-quad string representation.
 
Ipv6Address toIpv6Mapped () const
 Converts to an IPv4-mapped IPv6 address (::%ffff:a.b.c.d).
 
MacAddress multicastMac () const
 Returns the Ethernet multicast MAC address for this IPv4 multicast address.
 
Error toSockAddr (struct sockaddr_in *sa) const
 Fills a sockaddr_in with this address.
 
bool operator== (const Ipv4Address &other) const
 Returns true if both addresses are equal.
 
bool operator!= (const Ipv4Address &other) const
 Returns true if the addresses are not equal.
 
bool operator< (const Ipv4Address &other) const
 Less-than comparison for ordering.
 

Static Public Member Functions

static Result< Ipv4AddressfromString (const String &str)
 Parses an IPv4 address from dotted-quad notation.
 
static Ipv4Address fromUint32 (uint32_t networkOrder)
 Creates an address from a 32-bit integer in network byte order.
 
static Ipv4Address any ()
 Returns the INADDR_ANY address (0.0.0.0).
 
static Ipv4Address loopback ()
 Returns the loopback address (127.0.0.1).
 
static Ipv4Address broadcast ()
 Returns the broadcast address (255.255.255.255).
 
static Result< Ipv4AddressfromSockAddr (const struct sockaddr_in *sa)
 Constructs from a sockaddr_in structure.
 

Detailed Description

IPv4 network address.

Simple value type representing a 32-bit IPv4 address stored in network byte order (big-endian). Provides parsing, formatting, subnet checks, and classification (loopback, multicast, private, etc.).

This class is purely computational and requires no platform-specific headers or system calls.

This class is not thread-safe. Concurrent access to a single instance requires external synchronization.

Example
auto [addr, err] = Ipv4Address::fromString("192.168.1.1");
if(addr.isPrivate()) { ... }
String str = addr.toString(); // "192.168.1.1"
static Result< Ipv4Address > fromString(const String &str)
Parses an IPv4 address from dotted-quad notation.
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35

Constructor & Destructor Documentation

◆ Ipv4Address() [1/2]

Ipv4Address::Ipv4Address ( uint32_t  networkOrder)
inlineexplicit

Constructs from a 32-bit integer in network byte order.

Parameters
networkOrderThe address in network byte order (big-endian).

◆ Ipv4Address() [2/2]

Ipv4Address::Ipv4Address ( uint8_t  a,
uint8_t  b,
uint8_t  c,
uint8_t  d 
)
inline

Constructs from four octets.

Parameters
aFirst octet (most significant).
bSecond octet.
cThird octet.
dFourth octet (least significant).

Member Function Documentation

◆ fromSockAddr()

static Result< Ipv4Address > Ipv4Address::fromSockAddr ( const struct sockaddr_in sa)
static

Constructs from a sockaddr_in structure.

Parameters
saPointer to a sockaddr_in. Must have sin_family == AF_INET.
Returns
A Result containing the address and Error::Ok, or Error::Invalid if the pointer is null.

◆ fromString()

static Result< Ipv4Address > Ipv4Address::fromString ( const String str)
static

Parses an IPv4 address from dotted-quad notation.

Parameters
strThe string to parse (e.g. "192.168.1.1").
Returns
A Result containing the parsed address and Error::Ok, or a null address and Error::Invalid on parse failure.

◆ fromUint32()

static Ipv4Address Ipv4Address::fromUint32 ( uint32_t  networkOrder)
inlinestatic

Creates an address from a 32-bit integer in network byte order.

Parameters
networkOrderThe address in network byte order.
Returns
The corresponding Ipv4Address.

◆ isInSubnet() [1/2]

bool Ipv4Address::isInSubnet ( Ipv4Address  network,
int  prefixLen 
) const

Returns true if this address is within the given subnet.

Parameters
networkThe network address.
prefixLenThe prefix length (0-32).
Returns
True if the address matches the network prefix.

◆ isInSubnet() [2/2]

bool Ipv4Address::isInSubnet ( Ipv4Address  network,
Ipv4Address  mask 
) const
inline

Returns true if this address is within the given subnet.

Parameters
networkThe network address.
maskThe subnet mask.
Returns
True if (this & mask) == (network & mask).

◆ isPrivate()

bool Ipv4Address::isPrivate ( ) const
inline

Returns true if the address is in a private range.

Checks for 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16.

◆ multicastMac()

MacAddress Ipv4Address::multicastMac ( ) const

Returns the Ethernet multicast MAC address for this IPv4 multicast address.

Maps this address to its IANA multicast MAC (01:00:5e + low 23 bits) per RFC 1112. Only 23 of the 28 multicast group bits are used, so 32 different multicast IPs share each MAC address. Use this to check for multicast MAC collisions:

if(addrA.multicastMac() == addrB.multicastMac()) { ... }
Returns
The corresponding MacAddress, or a null MacAddress if this address is not multicast.

◆ octet()

uint8_t Ipv4Address::octet ( int  index) const
inline

Returns a single octet of the address.

Parameters
indexOctet index (0 = most significant, 3 = least significant).
Returns
The octet value, or 0 if index is out of range.

◆ toIpv6Mapped()

Ipv6Address Ipv4Address::toIpv6Mapped ( ) const

Converts to an IPv4-mapped IPv6 address (::%ffff:a.b.c.d).

Returns
The corresponding Ipv6Address.

◆ toSockAddr()

Error Ipv4Address::toSockAddr ( struct sockaddr_in sa) const

Fills a sockaddr_in with this address.

Sets sin_family to AF_INET and sin_addr. The port field (sin_port) is set to zero; callers should set it separately.

Parameters
[out]saThe structure to fill.
Returns
Error::Ok on success, Error::Invalid if sa is null.

◆ toString()

String Ipv4Address::toString ( ) const

Returns a dotted-quad string representation.

Returns
A String like "192.168.1.1".

◆ toUint32()

uint32_t Ipv4Address::toUint32 ( ) const
inline

Returns the address as a 32-bit integer in network byte order.

Returns
The raw 32-bit address (big-endian).

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