#include <MACAddress.h>
Public Member Functions | |
MACAddress () | |
MACAddress (const char *hexstr) | |
MACAddress (const MACAddress &other) | |
MACAddress & | operator= (const MACAddress &other) |
unsigned int | getAddressSize () const |
unsigned char | getAddressByte (unsigned int k) const |
void | setAddressByte (unsigned int k, unsigned char addrbyte) |
bool | tryParse (const char *hexstr) |
void | setAddress (const char *hexstr) |
unsigned char * | getAddressBytes () |
void | setAddressBytes (unsigned char *addrbytes) |
void | setBroadcast () |
bool | isBroadcast () const |
bool | isMulticast () const |
bool | isUnspecified () const |
std::string | str () const |
bool | equals (const MACAddress &other) const |
bool | operator== (const MACAddress &other) const |
bool | operator!= (const MACAddress &other) const |
int | compareTo (const MACAddress &other) const |
InterfaceToken | formInterfaceIdentifier () const |
Static Public Member Functions | |
static MACAddress | generateAutoAddress () |
Static Public Attributes | |
static const MACAddress | UNSPECIFIED_ADDRESS |
static const MACAddress | BROADCAST_ADDRESS |
Private Attributes | |
unsigned char | address [6] |
Static Private Attributes | |
static unsigned int | autoAddressCtr |
MACAddress::MACAddress | ( | ) |
MACAddress::MACAddress | ( | const char * | hexstr | ) |
Constructor which accepts a hex string (12 hex digits, may also contain spaces, hyphens and colons)
00070 { 00071 setAddress(hexstr); 00072 }
MACAddress::MACAddress | ( | const MACAddress & | other | ) | [inline] |
MACAddress & MACAddress::operator= | ( | const MACAddress & | other | ) |
Assignment.
00075 { 00076 memcpy(address, other.address, MAC_ADDRESS_BYTES); 00077 return *this; 00078 }
unsigned char MACAddress::getAddressByte | ( | unsigned int | k | ) | const |
Returns the kth byte of the address.
00086 { 00087 if (k>=6) throw cRuntimeError("Array of size 6 indexed with %d", k); 00088 return address[k]; 00089 }
void MACAddress::setAddressByte | ( | unsigned int | k, | |
unsigned char | addrbyte | |||
) |
Sets the kth byte of the address.
00092 { 00093 if (k>=6) throw cRuntimeError("Array of size 6 indexed with %d", k); 00094 address[k] = addrbyte; 00095 }
bool 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 EtherAppCli::resolveDestMACAddress(), and setAddress().
00098 { 00099 if (!hexstr) 00100 return false; 00101 00102 // check syntax 00103 int numHexDigits = 0; 00104 for (const char *s = hexstr; *s; s++) { 00105 if (isxdigit(*s)) 00106 numHexDigits++; 00107 else if (*s!=' ' && *s!=':' && *s!='-') 00108 return false; // wrong syntax 00109 } 00110 if (numHexDigits != 2*MAC_ADDRESS_BYTES) 00111 return false; 00112 00113 // convert 00114 hextobin(hexstr, address, MAC_ADDRESS_BYTES); 00115 return true; 00116 }
void MACAddress::setAddress | ( | const char * | hexstr | ) |
Converts address value from hex string (12 hex digits, may also contain spaces, hyphens and colons)
Referenced by Ieee80211MgmtSTASimplified::initialize(), Ieee80211MgmtBase::initialize(), Ieee80211Mac::initialize(), EtherMACBase::initializeMACAddress(), MACAddress(), Mac80211::registerInterface(), CSMAMacLayer::registerInterface(), and EtherAppCli::resolveDestMACAddress().
00119 { 00120 if (!tryParse(hexstr)) 00121 throw cRuntimeError("MACAddress: wrong address syntax '%s': 12 hex digits expected, with optional embedded spaces, hyphens or colons", hexstr); 00122 }
unsigned char* MACAddress::getAddressBytes | ( | ) | [inline] |
Returns pointer to internal binary representation of address (array of 6 unsigned chars).
00099 {return address;}
void MACAddress::setAddressBytes | ( | unsigned char * | addrbytes | ) |
Sets address bytes. The argument should point to an array of 6 unsigned chars.
Referenced by generateAutoAddress(), and ARP::processOutboundPacket().
00125 { 00126 memcpy(address, addrbytes, MAC_ADDRESS_BYTES); 00127 }
void MACAddress::setBroadcast | ( | ) |
bool MACAddress::isBroadcast | ( | ) | const |
bool MACAddress::isMulticast | ( | ) | const [inline] |
Returns true this is a multicast logical address (starts with bit 1).
00119 {return address[0]&0x80;};
bool MACAddress::isUnspecified | ( | ) | const |
Returns true if all address bytes are zero.
Referenced by EtherAppCli::initialize(), ARP::processARPPacket(), IPv6NeighbourDiscovery::processIPv6Datagram(), IPv6NeighbourDiscovery::processNAForIncompleteNCEState(), IPv6NeighbourDiscovery::processNAForOtherNCEStates(), IPv6NeighbourDiscovery::processNSWithSpecifiedSrcAddr(), Ieee80211MgmtSTA::processScanCommand(), ARP::sendARPRequest(), and IPv6::sendDatagramToOutput().
00140 { 00141 return !(address[0] || address[1] || address[2] || address[3] || address[4] || address[5]); 00142 }
std::string MACAddress::str | ( | ) | const |
Converts address to a hex string.
Referenced by Ieee80211MgmtSTA::handleAssociationResponseFrame(), Ieee80211Mac::initialize(), EtherMACBase::initializeMACAddress(), operator<<(), Ieee80211MgmtSTA::processAssociateCommand(), Ieee80211MgmtSTA::processAuthenticateCommand(), Ieee80211MgmtSTA::processDeauthenticateCommand(), Mac80211::registerInterface(), CSMAMacLayer::registerInterface(), Ieee80211MgmtSTA::startAssociation(), and Ieee80211MgmtSTA::startAuthentication().
00145 { 00146 char buf[20]; 00147 char *s = buf; 00148 for (int i=0; i<MAC_ADDRESS_BYTES; i++, s+=3) 00149 sprintf(s,"%2.2X-",address[i]); 00150 *(s-1)='\0'; 00151 return std::string(buf); 00152 }
bool MACAddress::equals | ( | const MACAddress & | other | ) | const |
Returns true if the two addresses are equal.
Referenced by IPv6NeighbourDiscovery::processNAForOtherNCEStates(), IPv6NeighbourDiscovery::processNSWithSpecifiedSrcAddr(), and IPv6NeighbourDiscovery::processRAForRouterUpdates().
00155 { 00156 return memcmp(address, other.address, MAC_ADDRESS_BYTES)==0; 00157 }
bool MACAddress::operator== | ( | const MACAddress & | other | ) | const [inline] |
bool MACAddress::operator!= | ( | const MACAddress & | other | ) | const [inline] |
int MACAddress::compareTo | ( | const MACAddress & | other | ) | const |
Returns -1, 0 or 1 as result of comparison of 2 addresses.
Referenced by MACRelayUnitBase::MAC_compare::operator()(), and Ieee80211MgmtAP::MAC_compare::operator()().
00160 { 00161 return memcmp(address, other.address, MAC_ADDRESS_BYTES); 00162 }
InterfaceToken MACAddress::formInterfaceIdentifier | ( | ) | const |
Create interface identifier (IEEE EUI-64) which can be used by IPv6 stateless address autoconfiguration.
Referenced by Mac80211::registerInterface(), Ieee80211Mac::registerInterface(), EtherMACBase::registerInterface(), and CSMAMacLayer::registerInterface().
00165 { 00166 const unsigned char *b = address; 00167 uint32 high = (b[0]<<24) | (b[1]<<16) | (b[2]<<8) | 0xff; 00168 uint32 low = (0xfe<<24) | (b[3]<<16) | (b[4]<<8) | b[5]; 00169 return InterfaceToken(low, high, 64); 00170 }
MACAddress MACAddress::generateAutoAddress | ( | ) | [static] |
Generates a unique address which begins with 0a:aa and ends in a unique suffix.
Referenced by Ieee80211Mac::initialize(), EtherMACBase::initializeMACAddress(), Mac80211::registerInterface(), and CSMAMacLayer::registerInterface().
00173 { 00174 ++autoAddressCtr; 00175 00176 unsigned char addrbytes[6]; 00177 addrbytes[0] = 0x0A; 00178 addrbytes[1] = 0xAA; 00179 addrbytes[2] = (autoAddressCtr>>24)&0xff; 00180 addrbytes[3] = (autoAddressCtr>>16)&0xff; 00181 addrbytes[4] = (autoAddressCtr>>8)&0xff; 00182 addrbytes[5] = (autoAddressCtr)&0xff; 00183 00184 MACAddress addr; 00185 addr.setAddressBytes(addrbytes); 00186 return addr; 00187 }
unsigned char MACAddress::address[6] [private] |
unsigned int MACAddress::autoAddressCtr [static, private] |
Referenced by generateAutoAddress().
const MACAddress MACAddress::UNSPECIFIED_ADDRESS [static] |
Returns the unspecified (null) MAC address
Referenced by IPv6NeighbourDiscovery::resolveNeighbour().
const MACAddress MACAddress::BROADCAST_ADDRESS [static] |
Returns the broadcast (ff:ff:ff:ff:ff:ff) MAC address
Referenced by IPv6::fragmentAndRoute(), Ieee80211MgmtSTA::processScanCommand(), IPv6::routeMulticastPacket(), and Ieee80211MgmtAP::sendBeacon().