diff --git a/src/node/eui64-address.cc b/src/node/eui64-address.cc new file mode 100644 index 000000000..b883fb362 --- /dev/null +++ b/src/node/eui64-address.cc @@ -0,0 +1,147 @@ +#include "eui48-address.h" +#include "address.h" +#include "ns3/assert.h" +#include +#include + +namespace ns3 { + +#define ASCII_a (0x41) +#define ASCII_z (0x5a) +#define ASCII_A (0x61) +#define ASCII_Z (0x7a) +#define ASCII_COLON (0x3a) +#define ASCII_ZERO (0x30) + +static char +AsciiToLowCase (char c) +{ + if (c >= ASCII_a && c <= ASCII_z) { + return c; + } else if (c >= ASCII_A && c <= ASCII_Z) { + return c + (ASCII_a - ASCII_A); + } else { + return c; + } +} + + +Eui64Address::Eui64Address () +{ + memset (m_address, 0, 8); +} +Eui64Address::Eui64Address (const char *str) +{ + int i = 0; + while (*str != 0 && i < 8) + { + uint8_t byte = 0; + while (*str != ASCII_COLON && *str != 0) + { + byte <<= 4; + char low = AsciiToLowCase (*str); + if (low >= ASCII_a) + { + byte |= low - ASCII_a + 10; + } + else + { + byte |= low - ASCII_ZERO; + } + str++; + } + m_address[i] = byte; + i++; + if (*str == 0) + { + break; + } + str++; + } + NS_ASSERT (i == 6); +} +void +Eui64Address::CopyFrom (const uint8_t buffer[8]) +{ + memcpy (m_address, buffer, 8); +} +void +Eui64Address::CopyTo (uint8_t buffer[8]) const +{ + memcpy (buffer, m_address, 8); +} + +bool +Eui64Address::IsMatchingType (const Address &address) +{ + return address.CheckCompatible (GetType (), 8); +} +Address +Eui64Address::ConvertTo (void) const +{ + return Address (GetType (), m_address, 8); +} +Eui64Address +Eui64Address::ConvertFrom (const Address &address) +{ + NS_ASSERT (address.CheckCompatible (GetType (), 8)); + Eui64Address retval; + address.CopyTo (retval.m_address); + return retval; +} +Eui64Address +Eui64Address::Allocate (void) +{ + static uint64_t id = 0; + id++; + Eui64Address address; + address.m_address[0] = (id >> 56) & 0xff; + address.m_address[1] = (id >> 48) & 0xff; + address.m_address[2] = (id >> 40) & 0xff; + address.m_address[3] = (id >> 32) & 0xff; + address.m_address[4] = (id >> 24) & 0xff; + address.m_address[5] = (id >> 16) & 0xff; + address.m_address[6] = (id >> 8) & 0xff; + address.m_address[7] = (id >> 0) & 0xff; + return address; +} +uint8_t +Eui64Address::GetType (void) +{ + static uint8_t type = Address::Register (); + return type; +} + +bool operator == (const Eui64Address &a, const Eui64Address &b) +{ + uint8_t ada[8]; + uint8_t adb[8]; + a.CopyTo (ada); + b.CopyTo (adb); + return memcmp (ada, adb, 8) == 0; +} +bool operator != (const Eui64Address &a, const Eui64Address &b) +{ + return ! (a == b); +} + +std::ostream& operator<< (std::ostream& os, const Eui64Address & address) +{ + uint8_t ad[8]; + address.CopyTo (ad); + + os.setf (std::ios::hex, std::ios::basefield); + std::cout.fill('0'); + for (uint8_t i=0; i < 7; i++) + { + os << std::setw(2) << (uint32_t)ad[i] << ":"; + } + // Final byte not suffixed by ":" + os << std::setw(2) << (uint32_t)ad[7]; + os.setf (std::ios::dec, std::ios::basefield); + std::cout.fill(' '); + return os; +} + + +} // namespace ns3 diff --git a/src/node/eui64-address.h b/src/node/eui64-address.h new file mode 100644 index 000000000..fb2b38177 --- /dev/null +++ b/src/node/eui64-address.h @@ -0,0 +1,71 @@ +#ifndef EUI64_ADDRESS_H +#define EUI64_ADDRESS_H + +#include +#include + +namespace ns3 { + +class Address; + +/** + * \brief an EUI-48 address + * + * This class can contain 48 bit IEEE addresses. + */ +class Eui64Address +{ +public: + Eui64Address (); + /** + * \param str a string representing the new Eui64Address + * + * The format of the string is "xx:xx:xx:xx:xx:xx" + */ + Eui64Address (const char *str); + + /** + * \param buffer address in network order + * + * Copy the input address to our internal buffer. + */ + void CopyFrom (const uint8_t buffer[8]); + /** + * \param buffer address in network order + * + * Copy the internal address to the input buffer. + */ + void CopyTo (uint8_t buffer[8]) const; + /** + * \returns a new Address instance + * + * Convert an instance of this class to a polymorphic Address instance. + */ + Address ConvertTo (void) const; + /** + * \returns true if the address matches, false otherwise. + */ + static bool IsMatchingType (const Address &address); + /** + * \param address a polymorphic address + * + * Convert a polymorphic address to an Eui64Address instance. + * The conversion performs a type check. + */ + static Eui64Address ConvertFrom (const Address &address); + /** + * Allocate a new Eui64Address. + */ + static Eui64Address Allocate (void); +private: + static uint8_t GetType (void); + uint8_t m_address[8]; +}; + +bool operator == (const Eui64Address &a, const Eui64Address &b); +bool operator != (const Eui64Address &a, const Eui64Address &b); +std::ostream& operator<< (std::ostream& os, const Eui64Address & address); + +} // namespace ns3 + +#endif /* EUI64_ADDRESS_H */