INET Framework for OMNeT++/OMNEST
inet::MacAddress Class Reference

Stores an IEEE 802 MAC address (6 octets = 48 bits). More...

#include <MacAddress.h>

Public Member Functions

 MacAddress ()
 Default constructor initializes address bytes to zero. More...
 
 MacAddress (uint64_t bits)
 Initializes the address from the lower 48 bits of the 64-bit argument. More...
 
 MacAddress (const char *hexstr)
 Constructor which accepts a hex string (12 hex digits, may also contain spaces, hyphens and colons) More...
 
 MacAddress (const MacAddress &other)
 Copy constructor. More...
 
MacAddressoperator= (const MacAddress &other)
 Assignment. More...
 
unsigned int getAddressSize () const
 Returns the address size in bytes, that is, 6. More...
 
unsigned char getAddressByte (unsigned int k) const
 Returns the kth byte of the address. More...
 
void setAddressByte (unsigned int k, unsigned char addrbyte)
 Sets the kth byte of the address. More...
 
bool tryParse (const char *hexstr)
 Sets the address and returns true if the syntax of the string is correct. More...
 
void setAddress (const char *hexstr)
 Converts address value from hex string (12 hex digits, may also contain spaces, hyphens and colons) More...
 
void getAddressBytes (unsigned char *addrbytes) const
 Copies the address to the given pointer (array of 6 unsigned chars). More...
 
void getAddressBytes (char *addrbytes) const
 
void setAddressBytes (unsigned char *addrbytes)
 Sets address bytes. More...
 
void setAddressBytes (char *addrbytes)
 
void setBroadcast ()
 Sets the address to the broadcast address (hex ff:ff:ff:ff:ff:ff). More...
 
bool isBroadcast () const
 Returns true if this is the broadcast address (hex ff:ff:ff:ff:ff:ff). More...
 
bool isMulticast () const
 Returns true if this is a multicast logical address (first byte's lsb is 1). More...
 
bool isLocal () const
 Returns true if this is a local address (first byte's second less significant bit is 1). More...
 
bool isUnspecified () const
 Returns true if all address bytes are zero. More...
 
std::string str () const
 Converts address to a hex string. More...
 
uint64_t getInt () const
 Converts address to 48 bits integer. More...
 
bool equals (const MacAddress &other) const
 Returns true if the two addresses are equal. More...
 
bool operator== (const MacAddress &other) const
 Returns true if the two addresses are equal. More...
 
bool operator!= (const MacAddress &other) const
 Returns true if the two addresses are not equal. More...
 
int compareTo (const MacAddress &other) const
 Returns -1, 0 or 1 as result of comparison of 2 addresses. More...
 
InterfaceToken formInterfaceIdentifier () const
 Create interface identifier (IEEE EUI-64) which can be used by IPv6 stateless address autoconfiguration. More...
 
bool operator< (const MacAddress &other) const
 
bool operator> (const MacAddress &other) const
 

Static Public Member Functions

static MacAddress generateAutoAddress ()
 Generates a unique address which begins with 0a:aa and ends in a unique suffix. More...
 

Static Public Attributes

static const MacAddress UNSPECIFIED_ADDRESS
 The unspecified MAC address, 00:00:00:00:00:00. More...
 
static const MacAddress BROADCAST_ADDRESS
 The broadcast MAC address, ff:ff:ff:ff:ff:ff. More...
 
static const MacAddress MULTICAST_PAUSE_ADDRESS
 The special multicast PAUSE MAC address, 01:80:C2:00:00:01. More...
 
static const MacAddress STP_MULTICAST_ADDRESS
 The spanning tree protocol bridge's multicast address, 01:80:C2:00:00:00. More...
 
static const MacAddress CDP_MULTICAST_ADDRESS
 The Cisco discovery protocol bridge's multicast address, 01:00:0C:CC:CC:CC. More...
 
static const MacAddress LLDP_MULTICAST_ADDRESS
 The Local link discovery protocol bridge's multicast address, 01:80:C2:00:00:0E. More...
 

Private Attributes

uint64_t address
 

Detailed Description

Stores an IEEE 802 MAC address (6 octets = 48 bits).

Constructor & Destructor Documentation

◆ MacAddress() [1/4]

inet::MacAddress::MacAddress ( )
inline

Default constructor initializes address bytes to zero.

51 { address = 0; }

Referenced by generateAutoAddress().

◆ MacAddress() [2/4]

inet::MacAddress::MacAddress ( uint64_t  bits)
inlineexplicit

Initializes the address from the lower 48 bits of the 64-bit argument.

56 { address = bits & MAC_ADDRESS_MASK; }

◆ MacAddress() [3/4]

inet::MacAddress::MacAddress ( const char *  hexstr)
inlineexplicit

Constructor which accepts a hex string (12 hex digits, may also contain spaces, hyphens and colons)

62 { setAddress(hexstr); }

◆ MacAddress() [4/4]

inet::MacAddress::MacAddress ( const MacAddress other)
inline

Copy constructor.

67 { address = other.address; }

Member Function Documentation

◆ compareTo()

int inet::MacAddress::compareTo ( const MacAddress other) const

Returns -1, 0 or 1 as result of comparison of 2 addresses.

122 {
123  return (address < other.address) ? -1 : (address == other.address) ? 0 : 1; // note: "return address-other.address" is not OK because 64-bit result does not fit into the return type
124 }

Referenced by inet::Stp::compareBridgeIDs(), inet::Rstp::compareRSTPData(), inet::Rstp::handleIncomingFrame(), inet::ieee80211::Ieee80211MgmtAp::MacCompare::operator()(), and inet::MacForwardingTable::MacCompare::operator()().

◆ equals()

bool inet::MacAddress::equals ( const MacAddress other) const
inline

◆ formInterfaceIdentifier()

InterfaceToken inet::MacAddress::formInterfaceIdentifier ( ) const

Create interface identifier (IEEE EUI-64) which can be used by IPv6 stateless address autoconfiguration.

127 {
128  uint32_t high = ((address >> 16) | 0xff) ^ 0x02000000;
129  uint32_t low = (0xfe << 24) | (address & 0xffffff);
130  return InterfaceToken(low, high, 64);
131 }

Referenced by inet::ShortcutMac::configureNetworkInterface(), inet::AckingMac::configureNetworkInterface(), inet::XMac::configureNetworkInterface(), inet::CsmaCaMac::configureNetworkInterface(), inet::LMac::configureNetworkInterface(), inet::BMac::configureNetworkInterface(), inet::Ieee802154Mac::configureNetworkInterface(), inet::VirtualTunnel::initialize(), and inet::NetworkInterface::initialize().

◆ generateAutoAddress()

MacAddress inet::MacAddress::generateAutoAddress ( )
static

Generates a unique address which begins with 0a:aa and ends in a unique suffix.

134 {
135  static SimulationRunUniqueNumberGenerator<uint64_t> counter;
136  uint64_t raw = 0x0AAA00000000ULL + (counter.getNextValue() & 0xffffffffUL) + getEnvir()->getParsimProcId() * 0x000100000000ULL;
137  return MacAddress(raw);
138 }

Referenced by inet::VirtualTunnel::initialize(), inet::NetworkInterface::initialize(), and inet::MacProtocolBase::parseMacAddressParameter().

◆ getAddressByte()

unsigned char inet::MacAddress::getAddressByte ( unsigned int  k) const

Returns the kth byte of the address.

24 {
25  if (k >= MAC_ADDRESS_SIZE)
26  throw cRuntimeError("Array of size 6 indexed with %d", k);
27  int offset = (MAC_ADDRESS_SIZE - k - 1) * 8;
28  return 0xff & (address >> offset);
29 }

Referenced by getAddressBytes(), str(), and inet::MemoryOutputStream::writeMacAddress().

◆ getAddressBytes() [1/2]

void inet::MacAddress::getAddressBytes ( char *  addrbytes) const
inline
105 { getAddressBytes(reinterpret_cast<unsigned char *>(addrbytes)); }

Referenced by getAddressBytes().

◆ getAddressBytes() [2/2]

void inet::MacAddress::getAddressBytes ( unsigned char *  addrbytes) const

Copies the address to the given pointer (array of 6 unsigned chars).

99 {
100  for (int i = 0; i < MAC_ADDRESS_SIZE; i++)
101  addrbytes[i] = getAddressByte(i);
102 }

◆ getAddressSize()

unsigned int inet::MacAddress::getAddressSize ( ) const
inline

Returns the address size in bytes, that is, 6.

77 { return MAC_ADDRESS_SIZE; }

◆ getInt()

uint64_t inet::MacAddress::getInt ( ) const
inline

Converts address to 48 bits integer.

146 { return address; }

Referenced by inet::doParsimPacking(), and inet::L3Address::set().

◆ isBroadcast()

◆ isLocal()

bool inet::MacAddress::isLocal ( ) const
inline

Returns true if this is a local address (first byte's second less significant bit is 1).

131 { return getAddressByte(0) & 0x02; };

◆ isMulticast()

◆ isUnspecified()

◆ operator!=()

bool inet::MacAddress::operator!= ( const MacAddress other) const
inline

Returns true if the two addresses are not equal.

161 { return address != other.address; }

◆ operator<()

bool inet::MacAddress::operator< ( const MacAddress other) const
inline
180 { return address < other.address; }

◆ operator=()

MacAddress& inet::MacAddress::operator= ( const MacAddress other)
inline

Assignment.

72 { address = other.address; return *this; }

◆ operator==()

bool inet::MacAddress::operator== ( const MacAddress other) const
inline

Returns true if the two addresses are equal.

156 { return address == other.address; }

◆ operator>()

bool inet::MacAddress::operator> ( const MacAddress other) const
inline
182 { return address > other.address; }

◆ setAddress()

void inet::MacAddress::setAddress ( const char *  hexstr)

Converts address value from hex string (12 hex digits, may also contain spaces, hyphens and colons)

93 {
94  if (!tryParse(hexstr))
95  throw cRuntimeError("MacAddress: wrong address syntax '%s': 12 hex digits expected, with optional embedded spaces, hyphens or colons", hexstr);
96 }

Referenced by inet::MacProtocolBase::parseMacAddressParameter().

◆ setAddressByte()

void inet::MacAddress::setAddressByte ( unsigned int  k,
unsigned char  addrbyte 
)

Sets the kth byte of the address.

32 {
33  if (k >= MAC_ADDRESS_SIZE)
34  throw cRuntimeError("Array of size 6 indexed with %d", k);
35  int offset = (MAC_ADDRESS_SIZE - k - 1) * 8;
36  address = (address & (~(((uint64_t)0xff) << offset))) | (((uint64_t)addrbyte) << offset);
37 }

Referenced by inet::L3Address::mapToMulticastMacAddress(), inet::Ipv6Address::mapToMulticastMacAddress(), inet::Ipv4Address::mapToMulticastMacAddress(), inet::MemoryInputStream::readMacAddress(), setAddressBytes(), and tryParse().

◆ setAddressBytes() [1/2]

void inet::MacAddress::setAddressBytes ( char *  addrbytes)
inline
111 { setAddressBytes(reinterpret_cast<unsigned char *>(addrbytes)); }

Referenced by setAddressBytes().

◆ setAddressBytes() [2/2]

void inet::MacAddress::setAddressBytes ( unsigned char *  addrbytes)

Sets address bytes.

The argument should point to an array of 6 unsigned chars.

105 {
106  address = 0; // clear top 16 bits too that setAddressByte() calls skip
107  for (int i = 0; i < MAC_ADDRESS_SIZE; i++)
108  setAddressByte(i, addrbytes[i]);
109 }

◆ setBroadcast()

void inet::MacAddress::setBroadcast ( )
inline

Sets the address to the broadcast address (hex ff:ff:ff:ff:ff:ff).

◆ str()

◆ tryParse()

bool inet::MacAddress::tryParse ( const char *  hexstr)

Sets the address and returns true if the syntax of the string is correct.

(See setAddress() for the syntax.)

40 {
41  if (!hexstr)
42  return false;
43 
44  // check syntax
45  int numHexDigits = 0;
46  for (const char *s = hexstr; *s; s++) {
47  if (isxdigit(*s))
48  numHexDigits++;
49  else if (*s != ' ' && *s != ':' && *s != '-')
50  return false; // wrong syntax
51  }
52  if (numHexDigits != 2 * MAC_ADDRESS_SIZE)
53  return false;
54 
55  // Converts hex string into the address
56  // if hext string is shorter, address is filled with zeros;
57  // Non-hex characters are discarded before conversion.
58  address = 0; // clear top 16 bits too that setAddressByte() calls skip
59  int k = 0;
60  const char *s = hexstr;
61  for (int pos = 0; pos < MAC_ADDRESS_SIZE; pos++) {
62  if (!s || !*s) {
63  setAddressByte(pos, 0);
64  }
65  else {
66  while (*s && !isxdigit(*s))
67  s++;
68  if (!*s) {
69  setAddressByte(pos, 0);
70  continue;
71  }
72  unsigned char d = isdigit(*s) ? (*s - '0') : islower(*s) ? (*s - 'a' + 10) : (*s - 'A' + 10);
73  d = d << 4;
74  s++;
75 
76  while (*s && !isxdigit(*s))
77  s++;
78  if (!*s) {
79  setAddressByte(pos, 0);
80  continue;
81  }
82  d += isdigit(*s) ? (*s - '0') : islower(*s) ? (*s - 'a' + 10) : (*s - 'A' + 10);
83  s++;
84 
85  setAddressByte(pos, d);
86  k++;
87  }
88  }
89  return true;
90 }

Referenced by inet::EtherAppClient::resolveDestMacAddress(), setAddress(), inet::L3Address::tryParse(), and inet::L3AddressResolver::tryParse().

Member Data Documentation

◆ address

◆ BROADCAST_ADDRESS

◆ CDP_MULTICAST_ADDRESS

const MacAddress inet::MacAddress::CDP_MULTICAST_ADDRESS
static

The Cisco discovery protocol bridge's multicast address, 01:00:0C:CC:CC:CC.

◆ LLDP_MULTICAST_ADDRESS

const MacAddress inet::MacAddress::LLDP_MULTICAST_ADDRESS
static

The Local link discovery protocol bridge's multicast address, 01:80:C2:00:00:0E.

◆ MULTICAST_PAUSE_ADDRESS

const MacAddress inet::MacAddress::MULTICAST_PAUSE_ADDRESS
static

◆ STP_MULTICAST_ADDRESS

const MacAddress inet::MacAddress::STP_MULTICAST_ADDRESS
static

The spanning tree protocol bridge's multicast address, 01:80:C2:00:00:00.

Referenced by inet::Stp::generateTCN(), inet::Stp::handleBPDU(), inet::Rstp::initialize(), inet::Stp::initialize(), inet::Rstp::sendBPDU(), and inet::Rstp::sendTCNtoRoot().

◆ UNSPECIFIED_ADDRESS


The documentation for this class was generated from the following files:
inet::MacAddress::MacAddress
MacAddress()
Default constructor initializes address bytes to zero.
Definition: MacAddress.h:51
inet::MacAddress::address
uint64_t address
Definition: MacAddress.h:27
MAC_ADDRESS_SIZE
#define MAC_ADDRESS_SIZE
Definition: MacAddress.h:16
inet::MacAddress::tryParse
bool tryParse(const char *hexstr)
Sets the address and returns true if the syntax of the string is correct.
Definition: MacAddress.cc:39
inet::MacAddress::getAddressBytes
void getAddressBytes(unsigned char *addrbytes) const
Copies the address to the given pointer (array of 6 unsigned chars).
Definition: MacAddress.cc:98
inet::MacAddress::setAddressBytes
void setAddressBytes(unsigned char *addrbytes)
Sets address bytes.
Definition: MacAddress.cc:104
inet::MacAddress::setAddress
void setAddress(const char *hexstr)
Converts address value from hex string (12 hex digits, may also contain spaces, hyphens and colons)
Definition: MacAddress.cc:92
inet::units::values::s
value< double, units::s > s
Definition: Units.h:1235
inet::physicallayer::k
const double k
Definition: Qam1024Modulation.cc:14
MAC_ADDRESS_MASK
#define MAC_ADDRESS_MASK
Definition: MacAddress.h:17
inet::MacAddress::getAddressByte
unsigned char getAddressByte(unsigned int k) const
Returns the kth byte of the address.
Definition: MacAddress.cc:23
inet::MacAddress::setAddressByte
void setAddressByte(unsigned int k, unsigned char addrbyte)
Sets the kth byte of the address.
Definition: MacAddress.cc:31