diff --git a/src/network/utils/mac16-address.cc b/src/network/utils/mac16-address.cc index aab6fb07e..ec9d95bb1 100644 --- a/src/network/utils/mac16-address.cc +++ b/src/network/utils/mac16-address.cc @@ -37,42 +37,18 @@ ATTRIBUTE_HELPER_CPP(Mac16Address); uint64_t Mac16Address::m_allocationIndex = 0; -Mac16Address::Mac16Address() -{ - NS_LOG_FUNCTION(this); - memset(m_address, 0, 2); -} - Mac16Address::Mac16Address(const char* str) { NS_LOG_FUNCTION(this << str); - int i = 0; - while (*str != 0 && i < 2) - { - uint8_t byte = 0; - while (*str != ':' && *str != 0) - { - byte <<= 4; - char low = std::tolower(*str); - if (low >= 'a') - { - byte |= low - 'a' + 10; - } - else - { - byte |= low - '0'; - } - str++; - } - m_address[i] = byte; - i++; - if (*str == 0) - { - break; - } - str++; - } - NS_ASSERT(i == 2); + NS_ASSERT_MSG(strlen(str) <= 5, "Mac16Address: illegal string (too long) " << str); + + unsigned int bytes[2]; + int charsRead = 0; + + int i = sscanf(str, "%02x:%02x%n", bytes, bytes + 1, &charsRead); + NS_ASSERT_MSG(i == 2 && !str[charsRead], "Mac16Address: illegal string " << str); + + std::copy(std::begin(bytes), std::end(bytes), std::begin(m_address)); } Mac16Address::Mac16Address(uint16_t addr) diff --git a/src/network/utils/mac16-address.h b/src/network/utils/mac16-address.h index 6cecc71e9..75299d76e 100644 --- a/src/network/utils/mac16-address.h +++ b/src/network/utils/mac16-address.h @@ -43,10 +43,11 @@ class Address; class Mac16Address { public: - Mac16Address(); + Mac16Address() = default; /** * \param str a string representing the new Mac16Address * + * The format of the string is "xx:xx" */ Mac16Address(const char* str); @@ -228,7 +229,7 @@ class Mac16Address friend std::istream& operator>>(std::istream& is, Mac16Address& address); static uint64_t m_allocationIndex; //!< Address allocation index - uint8_t m_address[2]; //!< address value + uint8_t m_address[2]{0}; //!< Address value }; ATTRIBUTE_HELPER_HEADER(Mac16Address); diff --git a/src/network/utils/mac48-address.cc b/src/network/utils/mac48-address.cc index e6ccd90b0..40a7cab13 100644 --- a/src/network/utils/mac48-address.cc +++ b/src/network/utils/mac48-address.cc @@ -36,42 +36,26 @@ ATTRIBUTE_HELPER_CPP(Mac48Address); uint64_t Mac48Address::m_allocationIndex = 0; -Mac48Address::Mac48Address() -{ - NS_LOG_FUNCTION(this); - std::memset(m_address, 0, 6); -} - Mac48Address::Mac48Address(const char* str) { NS_LOG_FUNCTION(this << str); - int i = 0; - while (*str != 0 && i < 6) - { - uint8_t byte = 0; - while (*str != ':' && *str != 0) - { - byte <<= 4; - char low = std::tolower(*str); - if (low >= 'a') - { - byte |= low - 'a' + 10; - } - else - { - byte |= low - '0'; - } - str++; - } - m_address[i] = byte; - i++; - if (*str == 0) - { - break; - } - str++; - } - NS_ASSERT(i == 6); + NS_ASSERT_MSG(strlen(str) <= 17, "Mac48Address: illegal string (too long) " << str); + + unsigned int bytes[6]; + int charsRead = 0; + + int i = sscanf(str, + "%02x:%02x:%02x:%02x:%02x:%02x%n", + bytes, + bytes + 1, + bytes + 2, + bytes + 3, + bytes + 4, + bytes + 5, + &charsRead); + NS_ASSERT_MSG(i == 6 && !str[charsRead], "Mac48Address: illegal string " << str); + + std::copy(std::begin(bytes), std::end(bytes), std::begin(m_address)); } void diff --git a/src/network/utils/mac48-address.h b/src/network/utils/mac48-address.h index 0ae7f9542..72fbf7aa4 100644 --- a/src/network/utils/mac48-address.h +++ b/src/network/utils/mac48-address.h @@ -45,7 +45,7 @@ class Address; class Mac48Address { public: - Mac48Address(); + Mac48Address() = default; /** * \param str a string representing the new Mac48Address * @@ -213,7 +213,7 @@ class Mac48Address friend std::istream& operator>>(std::istream& is, Mac48Address& address); static uint64_t m_allocationIndex; //!< Address allocation index - uint8_t m_address[6]; //!< address value + uint8_t m_address[6]{0}; //!< Address value }; ATTRIBUTE_HELPER_HEADER(Mac48Address); diff --git a/src/network/utils/mac64-address.cc b/src/network/utils/mac64-address.cc index 5d354691e..5f073b9bf 100644 --- a/src/network/utils/mac64-address.cc +++ b/src/network/utils/mac64-address.cc @@ -36,42 +36,28 @@ ATTRIBUTE_HELPER_CPP(Mac64Address); uint64_t Mac64Address::m_allocationIndex = 0; -Mac64Address::Mac64Address() -{ - NS_LOG_FUNCTION(this); - std::memset(m_address, 0, 8); -} - Mac64Address::Mac64Address(const char* str) { NS_LOG_FUNCTION(this << str); - int i = 0; - while (*str != 0 && i < 8) - { - uint8_t byte = 0; - while (*str != ':' && *str != 0) - { - byte <<= 4; - char low = std::tolower(*str); - if (low >= 'a') - { - byte |= low - 'a' + 10; - } - else - { - byte |= low - '0'; - } - str++; - } - m_address[i] = byte; - i++; - if (*str == 0) - { - break; - } - str++; - } - NS_ASSERT(i == 8); + NS_ASSERT_MSG(strlen(str) <= 23, "Mac64Address: illegal string (too long) " << str); + + unsigned int bytes[8]; + int charsRead = 0; + + int i = sscanf(str, + "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x%n", + bytes, + bytes + 1, + bytes + 2, + bytes + 3, + bytes + 4, + bytes + 5, + bytes + 6, + bytes + 7, + &charsRead); + NS_ASSERT_MSG(i == 8 && !str[charsRead], "Mac64Address: illegal string " << str); + + std::copy(std::begin(bytes), std::end(bytes), std::begin(m_address)); } Mac64Address::Mac64Address(uint64_t addr) diff --git a/src/network/utils/mac64-address.h b/src/network/utils/mac64-address.h index e1489188d..97d8d2cc4 100644 --- a/src/network/utils/mac64-address.h +++ b/src/network/utils/mac64-address.h @@ -45,7 +45,7 @@ class Address; class Mac64Address { public: - Mac64Address(); + Mac64Address() = default; /** * \param str a string representing the new Mac64Address * @@ -180,7 +180,7 @@ class Mac64Address friend std::istream& operator>>(std::istream& is, Mac64Address& address); static uint64_t m_allocationIndex; //!< Address allocation index - uint8_t m_address[8]; //!< address value + uint8_t m_address[8]{0}; //!< Address value }; /** diff --git a/src/network/utils/mac8-address.cc b/src/network/utils/mac8-address.cc index e6c8627d1..74e63ab37 100644 --- a/src/network/utils/mac8-address.cc +++ b/src/network/utils/mac8-address.cc @@ -30,11 +30,6 @@ NS_LOG_COMPONENT_DEFINE("Mac8Address"); uint8_t Mac8Address::m_allocationIndex = 0; -Mac8Address::Mac8Address() -{ - m_address = 255; -} - Mac8Address::Mac8Address(uint8_t addr) : m_address(addr) { diff --git a/src/network/utils/mac8-address.h b/src/network/utils/mac8-address.h index 07ca00c15..9335d899f 100644 --- a/src/network/utils/mac8-address.h +++ b/src/network/utils/mac8-address.h @@ -43,8 +43,7 @@ class Address; class Mac8Address { public: - /** Constructor */ - Mac8Address(); + Mac8Address() = default; /** * Create Mac8Address object with address addr. * @@ -132,7 +131,7 @@ class Mac8Address private: static uint8_t m_allocationIndex; //!< Address allocation index - uint8_t m_address; //!< The address. + uint8_t m_address{255}; //!< The address. /** * Get the Mac8Address type.