network: (fixes #1005) simplify and sanitize mac addresses parsing from strings
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user