libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
macaddress.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
11#include <cstring>
13#include <promeki/core/string.h>
14#include <promeki/core/result.h>
15#include <promeki/core/array.h>
16
18
19class Ipv4Address;
20class Ipv6Address;
21class TextStream;
22
45 public:
48
61
64
77
89
91 MacAddress() : _addr{} { }
92
97 explicit MacAddress(const DataFormat &bytes) : _addr(bytes) { }
98
109 _addr{DataFormat{std::array<uint8_t, 6>{a, b, c, d, e, f}}}
110 {
111 }
112
114 bool isNull() const { return _addr.isZero(); }
115
124 bool isBroadcast() const {
125 for(size_t i = 0; i < 6; ++i) {
126 if(_addr[i] != 0xFF) return false;
127 }
128 return true;
129 }
130
140 bool isMulticast() const { return (_addr[0] & 0x01) != 0; }
141
149 bool isGroupMulticast() const { return isMulticast() && !isBroadcast(); }
150
158 bool isIpv4Multicast() const {
159 return _addr[0] == 0x01 && _addr[1] == 0x00 &&
160 _addr[2] == 0x5E && (_addr[3] & 0x80) == 0;
161 }
162
169 bool isIpv6Multicast() const {
170 return _addr[0] == 0x33 && _addr[1] == 0x33;
171 }
172
178 bool isUnicast() const { return !isMulticast(); }
179
186 bool isLocallyAdministered() const { return (_addr[0] & 0x02) != 0; }
187
192 const DataFormat &data() const { return _addr; }
193
198 const uint8_t *raw() const { return _addr.data(); }
199
205 uint8_t octet(int index) const {
206 if(index < 0 || index > 5) return 0;
207 return _addr[index];
208 }
209
215
222
224 bool operator==(const MacAddress &other) const { return _addr == other._addr; }
226 bool operator!=(const MacAddress &other) const { return _addr != other._addr; }
228 bool operator<(const MacAddress &other) const {
229 return std::memcmp(_addr.data(), other._addr.data(), 6) < 0;
230 }
231
232 private:
233 DataFormat _addr;
234};
235
238
IPv4 network address.
Definition ipv4address.h:51
IPv6 network address.
Definition ipv6address.h:52
Dynamic array container wrapping std::vector.
Definition list.h:40
T * data() noexcept
Returns a pointer to the underlying contiguous storage.
Definition list.h:286
IEEE 802 MAC (Ethernet hardware) address.
Definition macaddress.h:44
MacAddress(const DataFormat &bytes)
Constructs from raw 6-byte data.
Definition macaddress.h:97
MacAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f)
Constructs from six individual octets.
Definition macaddress.h:108
bool isBroadcast() const
Returns true if this is the Ethernet broadcast address (ff:ff:ff:ff:ff:ff).
Definition macaddress.h:124
bool isIpv4Multicast() const
Returns true if this is an IPv4 multicast MAC address.
Definition macaddress.h:158
bool isUnicast() const
Returns true if this is a unicast address.
Definition macaddress.h:178
static MacAddress broadcast()
Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff).
const DataFormat & data() const
Returns a const reference to the raw 6-byte data.
Definition macaddress.h:192
bool operator==(const MacAddress &other) const
Returns true if both addresses are equal.
Definition macaddress.h:224
MacAddress()
Default constructor. Creates a null (00:00:00:00:00:00) address.
Definition macaddress.h:91
bool isNull() const
Returns true if all bytes are zero.
Definition macaddress.h:114
bool isLocallyAdministered() const
Returns true if this is a locally-administered address.
Definition macaddress.h:186
String toString(char separator) const
Returns a hex string with a custom separator.
bool operator<(const MacAddress &other) const
Less-than comparison for ordering (lexicographic).
Definition macaddress.h:228
uint8_t octet(int index) const
Returns a single octet of the address.
Definition macaddress.h:205
const uint8_t * raw() const
Returns a pointer to the raw byte data.
Definition macaddress.h:198
bool isMulticast() const
Returns true if this is a group (multicast) address.
Definition macaddress.h:140
String toString() const
Returns a colon-separated hex string.
static MacAddress fromIpv4Multicast(const Ipv4Address &addr)
Computes the multicast MAC address for an IPv4 multicast address.
bool isIpv6Multicast() const
Returns true if this is an IPv6 multicast MAC address.
Definition macaddress.h:169
bool operator!=(const MacAddress &other) const
Returns true if the addresses are not equal.
Definition macaddress.h:226
bool isGroupMulticast() const
Returns true if this is a group multicast address but NOT broadcast.
Definition macaddress.h:149
static MacAddress fromIpv6Multicast(const Ipv6Address &addr)
Computes the multicast MAC address for an IPv6 multicast address.
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
Formatted text I/O with encoding awareness.
Definition textstream.h:54
TextStream & operator<<(TextStream &stream, const MacAddress &addr)
Writes a colon-separated MAC address to the stream.
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19