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

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

#include <MACAddress.h>

Classes

class  SimulationLifecycleListener
 

Public Member Functions

 MACAddress ()
 Default constructor initializes address bytes to zero. More...
 
 MACAddress (uint64 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 isUnspecified () const
 Returns true if all address bytes are zero. More...
 
std::string str () const
 Converts address to a hex string. More...
 
uint64 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 MACAddress makeMulticastAddress (IPv4Address addr)
 Form a MAC address for a multicast IPv4 address, see RFC 1112, section 6.4. 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...
 

Private Attributes

uint64 address
 

Static Private Attributes

static unsigned int autoAddressCtr
 
static bool simulationLifecycleListenerAdded
 

Detailed Description

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

Constructor & Destructor Documentation

inet::MACAddress::MACAddress ( )
inline

Default constructor initializes address bytes to zero.

71 { address = 0; }
uint64 address
Definition: MACAddress.h:39
inet::MACAddress::MACAddress ( uint64  bits)
inlineexplicit

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

76 { address = bits & MAC_ADDRESS_MASK; }
uint64 address
Definition: MACAddress.h:39
#define MAC_ADDRESS_MASK
Definition: MACAddress.h:28
inet::MACAddress::MACAddress ( const char *  hexstr)
inlineexplicit

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

82 { setAddress(hexstr); }
void setAddress(const char *hexstr)
Converts address value from hex string (12 hex digits, may also contain spaces, hyphens and colons) ...
Definition: MACAddress.cc:102
inet::MACAddress::MACAddress ( const MACAddress other)
inline

Copy constructor.

87 { address = other.address; }
uint64 address
Definition: MACAddress.h:39

Member Function Documentation

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

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

Referenced by inet::STP::compareBridgeIDs(), inet::RSTP::compareRSTPData(), inet::RSTP::handleIncomingFrame(), inet::MACAddressTable::MAC_compare::operator()(), inet::ieee80211::Ieee80211MgmtAP::MAC_compare::operator()(), and inet::RSTP::processBPDU().

132 {
133  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
134 }
uint64 address
Definition: MACAddress.h:39
InterfaceToken inet::MACAddress::formInterfaceIdentifier ( ) const

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

Referenced by inet::IdealMac::createInterfaceEntry(), inet::ieee80211::Ieee80211Mac::createInterfaceEntry(), inet::LMacLayer::createInterfaceEntry(), inet::CsmaCaMac::createInterfaceEntry(), inet::BMacLayer::createInterfaceEntry(), inet::EtherMACBase::createInterfaceEntry(), and inet::CSMA::createInterfaceEntry().

137 {
138  uint32 high = ((address >> 16) | 0xff) ^ 0x02000000;
139  uint32 low = (0xfe << 24) | (address & 0xffffff);
140  return InterfaceToken(low, high, 64);
141 }
uint32_t uint32
Definition: Compat.h:30
uint64 address
Definition: MACAddress.h:39
MACAddress inet::MACAddress::generateAutoAddress ( )
static

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

Referenced by inet::ieee80211::Ieee80211Mac::initialize(), inet::CsmaCaMac::initialize(), inet::IdealMac::initializeMACAddress(), inet::LMacLayer::initializeMACAddress(), inet::EtherMACBase::initializeMACAddress(), inet::BMacLayer::initializeMACAddress(), and inet::CSMA::initializeMACAddress().

144 {
146  // NOTE: EXECUTE_ON_STARTUP is too early and would add the listener to StaticEnv
147  getEnvir()->addLifecycleListener(new MACAddress::SimulationLifecycleListener());
149  }
150  ++autoAddressCtr;
151 
152  uint64 intAddr = 0x0AAA00000000ULL + (autoAddressCtr & 0xffffffffUL);
153  MACAddress addr(intAddr);
154  return addr;
155 }
static bool simulationLifecycleListenerAdded
Definition: MACAddress.h:41
uint64_t uint64
Definition: Compat.h:28
MACAddress()
Default constructor initializes address bytes to zero.
Definition: MACAddress.h:71
static unsigned int autoAddressCtr
Definition: MACAddress.h:40
unsigned char inet::MACAddress::getAddressByte ( unsigned int  k) const

Returns the kth byte of the address.

Referenced by getAddressBytes(), and str().

34 {
35  if (k >= MAC_ADDRESS_SIZE)
36  throw cRuntimeError("Array of size 6 indexed with %d", k);
37  int offset = (MAC_ADDRESS_SIZE - k - 1) * 8;
38  return 0xff & (address >> offset);
39 }
uint64 address
Definition: MACAddress.h:39
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
const double k
Definition: QAM16Modulation.cc:24
void inet::MACAddress::getAddressBytes ( unsigned char *  addrbytes) const

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

Referenced by inet::serializer::Buffer::writeMACAddress().

109 {
110  for (int i = 0; i < MAC_ADDRESS_SIZE; i++)
111  addrbytes[i] = getAddressByte(i);
112 }
unsigned char getAddressByte(unsigned int k) const
Returns the kth byte of the address.
Definition: MACAddress.cc:33
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
void inet::MACAddress::getAddressBytes ( char *  addrbytes) const
inline

Referenced by getAddressBytes().

125 { getAddressBytes((unsigned char *)addrbytes); }
void getAddressBytes(unsigned char *addrbytes) const
Copies the address to the given pointer (array of 6 unsigned chars).
Definition: MACAddress.cc:108
unsigned int inet::MACAddress::getAddressSize ( ) const
inline

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

97 { return MAC_ADDRESS_SIZE; }
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
uint64 inet::MACAddress::getInt ( ) const
inline

Converts address to 48 bits integer.

161 { return address; }
uint64 address
Definition: MACAddress.h:39
bool inet::MACAddress::isMulticast ( ) const
inline

Returns true if this is a multicast logical address (first byte's lsb is 1).

Referenced by inet::ieee80211::PendingQueue::cmpMgmtOverMulticastOverUnicast(), inet::ieee80211::OriginatorProtectionMechanism::computeDataFrameDurationField(), inet::ieee80211::SingleProtectionMechanism::computeDataOrMgmtFrameDurationField(), inet::ieee80211::RateSelection::computeDataOrMgmtFrameMode(), inet::ieee80211::QoSRateSelection::computeDataOrMgmtFrameMode(), inet::ieee80211::OriginatorProtectionMechanism::computeMgmtFrameDurationField(), inet::IdealMac::dropFrameNotForUs(), inet::EtherMACBase::dropFrameNotForUs(), inet::ieee80211::QoSSequenceNumberAssignment::getCacheType(), inet::ieee80211::Ieee80211MgmtAP::handleDataFrame(), inet::ieee80211::Ieee80211MgmtAP::handleUpperMessage(), inet::ieee80211::HcfFs::hasMoreTxOpsAndMulticast(), inet::ieee80211::RecipientAckPolicy::isAckNeeded(), inet::ieee80211::RecipientQoSAckPolicy::isAckNeeded(), inet::ieee80211::OriginatorQoSAckPolicy::isAckNeeded(), inet::ieee80211::DcfFs::isBroadcastManagementOrGroupDataSequenceNeeded(), inet::ieee80211::FrameSequenceContext::isForUs(), inet::ieee80211::Dcf::isForUs(), inet::ieee80211::Hcf::isForUs(), inet::L3Address::isMulticast(), inet::ieee80211::RtsPolicy::isRtsNeeded(), inet::ieee80211::QoSRtsPolicy::isRtsNeeded(), inet::L3Address::isUnicast(), inet::ieee80211::Hcf::originatorProcessTransmittedFrame(), and inet::ieee80211::HcfFs::selectHcfSequence().

146 { return getAddressByte(0) & 0x01; };
unsigned char getAddressByte(unsigned int k) const
Returns the kth byte of the address.
Definition: MACAddress.cc:33
bool inet::MACAddress::isUnspecified ( ) const
inline

Returns true if all address bytes are zero.

Referenced by inet::ieee80211::Ieee80211MgmtAPBase::distributeReceivedDataFrame(), inet::ieee80211::Ieee80211MgmtSTASimplified::encapsulate(), inet::L3AddressResolver::getInterfaceMACAddress(), inet::GlobalARP::getL3AddressFor(), inet::ARP::getL3AddressFor(), inet::WiseRoute::handleLowerPacket(), inet::EtherTrafGen::handleMessage(), inet::EtherAppCli::handleMessage(), inet::EtherEncap::handleSendPause(), inet::EtherLLC::handleSendPause(), inet::ieee80211::Ieee80211MgmtSTASimplified::handleUpperMessage(), inet::CsmaCaMac::handleUpperPacket(), inet::WiseRoute::handleUpperPacket(), inet::EthernetApplication::initialize(), inet::L3Address::isUnspecified(), inet::ARP::processARPPacket(), inet::ieee80211::Ieee80211AgentSTA::processAssociateConfirm(), inet::EtherMACFullDuplex::processFrameFromUpperLayer(), inet::EtherMAC::processFrameFromUpperLayer(), inet::IPv6NeighbourDiscovery::processNAForIncompleteNCEState(), inet::IPv6NeighbourDiscovery::processNAForOtherNCEStates(), inet::IPv6NeighbourDiscovery::processNSWithSpecifiedSrcAddr(), inet::IPv6NeighbourDiscovery::processRAForRouterUpdates(), inet::ieee80211::Ieee80211MgmtSTA::processScanCommand(), inet::ieee80211::Ieee80211Mac::processUpperFrame(), inet::IPv6::resolveMACAddressAndSendPacket(), inet::ARP::sendARPRequest(), inet::IPv6::sendDatagramToOutput(), inet::IPv4::sendDatagramToOutput(), inet::IPv6NeighbourDiscovery::sendSolicitedNA(), inet::EtherMACFullDuplex::startFrameTransmission(), inet::EtherMAC::startFrameTransmission(), inet::IPv6NeighbourDiscovery::validateNSPacket(), and inet::IPv6NeighbourDiscovery::validateRSPacket().

151 { return address == 0; }
uint64 address
Definition: MACAddress.h:39
MACAddress inet::MACAddress::makeMulticastAddress ( IPv4Address  addr)
static

Form a MAC address for a multicast IPv4 address, see RFC 1112, section 6.4.

Referenced by inet::IPv4::resolveNextHopMacAddress().

159 {
160  ASSERT(addr.isMulticast());
161 
162  MACAddress macAddr;
163  macAddr.setAddressByte(0, 0x01);
164  macAddr.setAddressByte(1, 0x00);
165  macAddr.setAddressByte(2, 0x5e);
166  macAddr.setAddressByte(3, addr.getDByte(1) & 0x7f);
167  macAddr.setAddressByte(4, addr.getDByte(2));
168  macAddr.setAddressByte(5, addr.getDByte(3));
169  return macAddr;
170 }
MACAddress()
Default constructor initializes address bytes to zero.
Definition: MACAddress.h:71
bool inet::MACAddress::operator!= ( const MACAddress other) const
inline

Returns true if the two addresses are not equal.

176 { return address != other.address; }
uint64 address
Definition: MACAddress.h:39
bool inet::MACAddress::operator< ( const MACAddress other) const
inline
200 { return address < other.address; }
uint64 address
Definition: MACAddress.h:39
MACAddress& inet::MACAddress::operator= ( const MACAddress other)
inline

Assignment.

92 { address = other.address; return *this; }
uint64 address
Definition: MACAddress.h:39
bool inet::MACAddress::operator== ( const MACAddress other) const
inline

Returns true if the two addresses are equal.

171 { return address == other.address; }
uint64 address
Definition: MACAddress.h:39
bool inet::MACAddress::operator> ( const MACAddress other) const
inline
202 { return address > other.address; }
uint64 address
Definition: MACAddress.h:39
void inet::MACAddress::setAddress ( const char *  hexstr)

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

Referenced by inet::ieee80211::Ieee80211MgmtSTASimplified::initialize(), inet::ieee80211::Ieee80211MgmtBase::initialize(), inet::ieee80211::Ieee80211Mac::initialize(), inet::CsmaCaMac::initialize(), inet::IdealMac::initializeMACAddress(), inet::LMacLayer::initializeMACAddress(), inet::EtherMACBase::initializeMACAddress(), inet::BMacLayer::initializeMACAddress(), inet::CSMA::initializeMACAddress(), inet::EthernetApplication::resolveDestMACAddress(), inet::EtherTrafGen::resolveDestMACAddress(), and inet::EtherAppCli::resolveDestMACAddress().

103 {
104  if (!tryParse(hexstr))
105  throw cRuntimeError("MACAddress: wrong address syntax '%s': 12 hex digits expected, with optional embedded spaces, hyphens or colons", hexstr);
106 }
bool tryParse(const char *hexstr)
Sets the address and returns true if the syntax of the string is correct.
Definition: MACAddress.cc:49
void inet::MACAddress::setAddressByte ( unsigned int  k,
unsigned char  addrbyte 
)

Sets the kth byte of the address.

Referenced by makeMulticastAddress(), inet::GenericARP::mapMulticastAddress(), inet::serializer::Buffer::readMACAddress(), setAddressBytes(), and tryParse().

42 {
43  if (k >= MAC_ADDRESS_SIZE)
44  throw cRuntimeError("Array of size 6 indexed with %d", k);
45  int offset = (MAC_ADDRESS_SIZE - k - 1) * 8;
46  address = (address & (~(((uint64)0xff) << offset))) | (((uint64)addrbyte) << offset);
47 }
uint64_t uint64
Definition: Compat.h:28
uint64 address
Definition: MACAddress.h:39
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
const double k
Definition: QAM16Modulation.cc:24
void inet::MACAddress::setAddressBytes ( unsigned char *  addrbytes)

Sets address bytes.

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

115 {
116  address = 0; // clear top 16 bits too that setAddressByte() calls skip
117  for (int i = 0; i < MAC_ADDRESS_SIZE; i++)
118  setAddressByte(i, addrbytes[i]);
119 }
uint64 address
Definition: MACAddress.h:39
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
void setAddressByte(unsigned int k, unsigned char addrbyte)
Sets the kth byte of the address.
Definition: MACAddress.cc:41
void inet::MACAddress::setAddressBytes ( char *  addrbytes)
inline

Referenced by setAddressBytes().

131 { setAddressBytes((unsigned char *)addrbytes); }
void setAddressBytes(unsigned char *addrbytes)
Sets address bytes.
Definition: MACAddress.cc:114
void inet::MACAddress::setBroadcast ( )
inline

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

uint64 address
Definition: MACAddress.h:39
#define MAC_ADDRESS_MASK
Definition: MACAddress.h:28
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.)

Referenced by inet::EthernetApplication::resolveDestMACAddress(), inet::EtherTrafGen::resolveDestMACAddress(), inet::EtherAppCli::resolveDestMACAddress(), setAddress(), and inet::L3Address::tryParse().

50 {
51  if (!hexstr)
52  return false;
53 
54  // check syntax
55  int numHexDigits = 0;
56  for (const char *s = hexstr; *s; s++) {
57  if (isxdigit(*s))
58  numHexDigits++;
59  else if (*s != ' ' && *s != ':' && *s != '-')
60  return false; // wrong syntax
61  }
62  if (numHexDigits != 2 * MAC_ADDRESS_SIZE)
63  return false;
64 
65  // Converts hex string into the address
66  // if hext string is shorter, address is filled with zeros;
67  // Non-hex characters are discarded before conversion.
68  address = 0; // clear top 16 bits too that setAddressByte() calls skip
69  int k = 0;
70  const char *s = hexstr;
71  for (int pos = 0; pos < MAC_ADDRESS_SIZE; pos++) {
72  if (!s || !*s) {
73  setAddressByte(pos, 0);
74  }
75  else {
76  while (*s && !isxdigit(*s))
77  s++;
78  if (!*s) {
79  setAddressByte(pos, 0);
80  continue;
81  }
82  unsigned char d = isdigit(*s) ? (*s - '0') : islower(*s) ? (*s - 'a' + 10) : (*s - 'A' + 10);
83  d = d << 4;
84  s++;
85 
86  while (*s && !isxdigit(*s))
87  s++;
88  if (!*s) {
89  setAddressByte(pos, 0);
90  continue;
91  }
92  d += isdigit(*s) ? (*s - '0') : islower(*s) ? (*s - 'a' + 10) : (*s - 'A' + 10);
93  s++;
94 
95  setAddressByte(pos, d);
96  k++;
97  }
98  }
99  return true;
100 }
uint64 address
Definition: MACAddress.h:39
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
value< double, units::s > s
Definition: Units.h:1049
const double k
Definition: QAM16Modulation.cc:24
void setAddressByte(unsigned int k, unsigned char addrbyte)
Sets the kth byte of the address.
Definition: MACAddress.cc:41

Member Data Documentation

unsigned int inet::MACAddress::autoAddressCtr
staticprivate

Referenced by generateAutoAddress().

const MACAddress inet::MACAddress::MULTICAST_PAUSE_ADDRESS
static

The special multicast PAUSE MAC address, 01:80:C2:00:00:01.

Referenced by inet::EtherMACBase::dropFrameNotForUs(), inet::EtherEncap::handleSendPause(), and inet::EtherLLC::handleSendPause().

bool inet::MACAddress::simulationLifecycleListenerAdded
staticprivate

Referenced by generateAutoAddress().

const MACAddress inet::MACAddress::STP_MULTICAST_ADDRESS
static

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