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

IEEE 802 MAC (Ethernet hardware) address. More...

#include <macaddress.h>

Public Types

using DataFormat = Array< uint8_t, 6 >
 Raw 6-byte storage format for a MAC address.
 

Public Member Functions

 MacAddress ()
 Default constructor. Creates a null (00:00:00:00:00:00) address.
 
 MacAddress (const DataFormat &bytes)
 Constructs from raw 6-byte data.
 
 MacAddress (uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f)
 Constructs from six individual octets.
 
bool isNull () const
 Returns true if all bytes are zero.
 
bool isBroadcast () const
 Returns true if this is the Ethernet broadcast address (ff:ff:ff:ff:ff:ff).
 
bool isMulticast () const
 Returns true if this is a group (multicast) address.
 
bool isGroupMulticast () const
 Returns true if this is a group multicast address but NOT broadcast.
 
bool isIpv4Multicast () const
 Returns true if this is an IPv4 multicast MAC address.
 
bool isIpv6Multicast () const
 Returns true if this is an IPv6 multicast MAC address.
 
bool isUnicast () const
 Returns true if this is a unicast address.
 
bool isLocallyAdministered () const
 Returns true if this is a locally-administered address.
 
const DataFormatdata () const
 Returns a const reference to the raw 6-byte data.
 
const uint8_traw () const
 Returns a pointer to the raw byte data.
 
uint8_t octet (int index) const
 Returns a single octet of the address.
 
String toString () const
 Returns a colon-separated hex string.
 
String toString (char separator) const
 Returns a hex string with a custom separator.
 
bool operator== (const MacAddress &other) const
 Returns true if both addresses are equal.
 
bool operator!= (const MacAddress &other) const
 Returns true if the addresses are not equal.
 
bool operator< (const MacAddress &other) const
 Less-than comparison for ordering (lexicographic).
 

Static Public Member Functions

static Result< MacAddressfromString (const String &str)
 Parses a MAC address from a string.
 
static MacAddress broadcast ()
 Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff).
 
static MacAddress fromIpv4Multicast (const Ipv4Address &addr)
 Computes the multicast MAC address for an IPv4 multicast address.
 
static MacAddress fromIpv6Multicast (const Ipv6Address &addr)
 Computes the multicast MAC address for an IPv6 multicast address.
 

Detailed Description

IEEE 802 MAC (Ethernet hardware) address.

Simple value type representing a 48-bit MAC address. Provides parsing, formatting, and classification (broadcast, multicast, locally-administered).

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 [mac, err] = MacAddress::fromString("aa:bb:cc:dd:ee:ff");
if(mac.isMulticast()) { ... }
String str = mac.toString(); // "aa:bb:cc:dd:ee:ff"
Dynamic array container wrapping std::vector.
Definition list.h:40
static Result< MacAddress > fromString(const String &str)
Parses a MAC address from a string.
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35

Constructor & Destructor Documentation

◆ MacAddress() [1/2]

MacAddress::MacAddress ( const DataFormat bytes)
inlineexplicit

Constructs from raw 6-byte data.

Parameters
bytesThe 6 bytes of the MAC address.

◆ MacAddress() [2/2]

MacAddress::MacAddress ( uint8_t  a,
uint8_t  b,
uint8_t  c,
uint8_t  d,
uint8_t  e,
uint8_t  f 
)
inline

Constructs from six individual octets.

Parameters
aFirst octet.
bSecond octet.
cThird octet.
dFourth octet.
eFifth octet.
fSixth octet.

Member Function Documentation

◆ data()

const DataFormat & MacAddress::data ( ) const
inline

Returns a const reference to the raw 6-byte data.

Returns
The underlying DataFormat array.

◆ fromIpv4Multicast()

static MacAddress MacAddress::fromIpv4Multicast ( const Ipv4Address addr)
static

Computes the multicast MAC address for an IPv4 multicast address.

Maps an IPv4 multicast address (224.0.0.0/4) to its corresponding Ethernet multicast MAC address per IANA (RFC 1112). The mapping uses the OUI 01:00:5e and the low-order 23 bits of the IPv4 address, meaning that 32 different multicast IP addresses map to each MAC.

Parameters
addrAn IPv4 multicast address.
Returns
The corresponding MAC address, or a null MAC if addr is not multicast.

◆ fromIpv6Multicast()

static MacAddress MacAddress::fromIpv6Multicast ( const Ipv6Address addr)
static

Computes the multicast MAC address for an IPv6 multicast address.

Maps an IPv6 multicast address (ff00::/8) to its corresponding Ethernet multicast MAC address per RFC 2464. The mapping uses the prefix 33:33 and the low-order 32 bits of the IPv6 address.

Parameters
addrAn IPv6 multicast address.
Returns
The corresponding MAC address, or a null MAC if addr is not multicast.

◆ fromString()

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

Parses a MAC address from a string.

Accepts colon-separated ("aa:bb:cc:dd:ee:ff") or hyphen-separated ("aa-bb-cc-dd-ee-ff") hexadecimal notation. Case-insensitive.

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

◆ isBroadcast()

bool MacAddress::isBroadcast ( ) const
inline

Returns true if this is the Ethernet broadcast address (ff:ff:ff:ff:ff:ff).

This is the Layer 2 broadcast address used for both Ethernet broadcast frames and IP-level broadcast (255.255.255.255). Note that the broadcast address also has the group/multicast bit set, so isMulticast() returns true for the broadcast address.

◆ isGroupMulticast()

bool MacAddress::isGroupMulticast ( ) const
inline

Returns true if this is a group multicast address but NOT broadcast.

Returns true when the I/G bit is set and the address is not ff:ff:ff:ff:ff:ff. This is the typical check for "is this a multicast destination" in the practical sense.

◆ isIpv4Multicast()

bool MacAddress::isIpv4Multicast ( ) const
inline

Returns true if this is an IPv4 multicast MAC address.

IPv4 multicast maps to the IANA OUI 01:00:5e with the high bit of the fourth octet clear, giving the range 01:00:5e:00:00:00 through 01:00:5e:7f:ff:ff (RFC 1112).

◆ isIpv6Multicast()

bool MacAddress::isIpv6Multicast ( ) const
inline

Returns true if this is an IPv6 multicast MAC address.

IPv6 multicast maps to the prefix 33:33 followed by the low-order 32 bits of the IPv6 address (RFC 2464).

◆ isLocallyAdministered()

bool MacAddress::isLocallyAdministered ( ) const
inline

Returns true if this is a locally-administered address.

The second-least-significant bit of the first octet indicates local administration.

◆ isMulticast()

bool MacAddress::isMulticast ( ) const
inline

Returns true if this is a group (multicast) address.

A MAC address is a group address when the least-significant bit of the first octet is set (IEEE 802 I/G bit). This includes both multicast addresses and the broadcast address (ff:ff:ff:ff:ff:ff). Use isBroadcast() to distinguish, or isGroupMulticast() to exclude broadcast.

◆ isUnicast()

bool MacAddress::isUnicast ( ) const
inline

Returns true if this is a unicast address.

A unicast address has the I/G bit clear (not multicast, not broadcast).

◆ octet()

uint8_t MacAddress::octet ( int  index) const
inline

Returns a single octet of the address.

Parameters
indexOctet index (0-5).
Returns
The octet value, or 0 if index is out of range.

◆ raw()

const uint8_t * MacAddress::raw ( ) const
inline

Returns a pointer to the raw byte data.

Returns
A pointer to the first byte.

◆ toString() [1/2]

String MacAddress::toString ( ) const

Returns a colon-separated hex string.

Returns
A String like "aa:bb:cc:dd:ee:ff" (lowercase).

◆ toString() [2/2]

String MacAddress::toString ( char  separator) const

Returns a hex string with a custom separator.

Parameters
separatorThe character to place between octets.
Returns
A String like "aa-bb-cc-dd-ee-ff".

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