#include <MACAddress.h>
Public Member Functions | |
MACAddress () | |
MACAddress (const char *hexstr) | |
MACAddress (const MACAddress &other) | |
MACAddress & | operator= (const MACAddress &other) |
unsigned int | getAddressArraySize () const |
unsigned char | getAddress (unsigned int k) const |
void | setAddress (unsigned int k, unsigned char addrbyte) |
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 hex string or the string "auto".
00075 { 00076 setAddress(hexstr); 00077 }
MACAddress::MACAddress | ( | const MACAddress & | other | ) | [inline] |
int MACAddress::compareTo | ( | const MACAddress & | other | ) | const |
Returns -1, 0 or 1 as result of comparison of 2 addresses.
00146 { 00147 return memcmp(address, other.address, MAC_ADDRESS_BYTES); 00148 }
bool MACAddress::equals | ( | const MACAddress & | other | ) | const |
Returns true if the two addresses are equal.
00141 { 00142 return memcmp(address, other.address, MAC_ADDRESS_BYTES)==0; 00143 }
InterfaceToken MACAddress::formInterfaceIdentifier | ( | ) | const |
Create interface identifier (IEEE EUI-64) which can be used by IPv6 stateless address autoconfiguration.
00151 { 00152 const unsigned char *b = address; 00153 uint32 high = (b[0]<<24) | (b[1]<<16) | (b[2]<<8) | 0xff; 00154 uint32 low = (0xfe<<24) | (b[3]<<16) | (b[4]<<8) | b[5]; 00155 return InterfaceToken(low, high, 64); 00156 }
MACAddress MACAddress::generateAutoAddress | ( | ) | [static] |
Generates a unique address which begins with 0a:aa and ends in a unique suffix.
00159 { 00160 ++autoAddressCtr; 00161 00162 unsigned char addrbytes[6]; 00163 addrbytes[0] = 0x0A; 00164 addrbytes[1] = 0xAA; 00165 addrbytes[2] = (autoAddressCtr>>24)&0xff; 00166 addrbytes[3] = (autoAddressCtr>>16)&0xff; 00167 addrbytes[4] = (autoAddressCtr>>8)&0xff; 00168 addrbytes[5] = (autoAddressCtr)&0xff; 00169 00170 MACAddress addr; 00171 addr.setAddressBytes(addrbytes); 00172 return addr; 00173 }
unsigned char MACAddress::getAddress | ( | unsigned int | k | ) | const |
Returns the kth byte of the address.
00091 { 00092 if (k>=6) throw new cException("Array of size 6 indexed with %d", k); 00093 return address[k]; 00094 }
unsigned char* MACAddress::getAddressBytes | ( | ) | [inline] |
Returns pointer to internal binary representation of address (array of 6 unsigned chars).
00092 {return address;}
bool MACAddress::isBroadcast | ( | ) | const |
bool MACAddress::isMulticast | ( | ) | const [inline] |
Returns true this is a multicast logical address (starts with bit 1).
00112 {return address[0]&0x80;};
bool MACAddress::isUnspecified | ( | ) | const |
bool MACAddress::operator!= | ( | const MACAddress & | other | ) | const [inline] |
MACAddress & MACAddress::operator= | ( | const MACAddress & | other | ) |
Assignment.
00080 { 00081 memcpy(address, other.address, MAC_ADDRESS_BYTES); 00082 return *this; 00083 }
bool MACAddress::operator== | ( | const MACAddress & | other | ) | const [inline] |
void MACAddress::setAddress | ( | const char * | hexstr | ) |
Converts address value from hex string. The string "auto" is also accepted, it'll generate a unique address starting with "A0 00".
00103 { 00104 if (!hexstr) 00105 throw new cException("MACAddress::setAddress(const char *): got null pointer"); 00106 if (hextobin(hexstr, address, MAC_ADDRESS_BYTES)!=MAC_ADDRESS_BYTES) 00107 throw new cException("MACAddress::setAddress(const char *): hex string \"%s\" too short, should be 12 hex digits", hexstr); 00108 }
void MACAddress::setAddress | ( | unsigned int | k, | |
unsigned char | addrbyte | |||
) |
Sets the kth byte of the address.
00097 { 00098 if (k>=6) throw new cException("Array of size 6 indexed with %d", k); 00099 address[k] = addrbyte; 00100 }
void MACAddress::setAddressBytes | ( | unsigned char * | addrbytes | ) |
Sets address bytes. The argument should point to an array of 6 unsigned chars.
00111 { 00112 memcpy(address, addrbytes, MAC_ADDRESS_BYTES); 00113 }
void MACAddress::setBroadcast | ( | ) |
std::string MACAddress::str | ( | ) | const |
Converts address to a hex string.
00131 { 00132 char buf[20]; 00133 char *s = buf; 00134 for (int i=0; i<MAC_ADDRESS_BYTES; i++, s+=3) 00135 sprintf(s,"%2.2X-",address[i]); 00136 *(s-1)='\0'; 00137 return std::string(buf); 00138 }
unsigned char MACAddress::address[6] [private] |
unsigned int MACAddress::autoAddressCtr [static, private] |
const MACAddress MACAddress::BROADCAST_ADDRESS [static] |
Returns the broadcast (ff:ff:ff:ff:ff:ff) MAC address
const MACAddress MACAddress::UNSPECIFIED_ADDRESS [static] |
Returns the unspecified (null) MAC address