257 lines
7.3 KiB
C++
257 lines
7.3 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/MacAddress.h
|
|
*
|
|
* @brief MAC address class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MIDDLEWARE_MISC_MAC_ADDRESS_H)
|
|
#define ACTIV_MIDDLEWARE_MISC_MAC_ADDRESS_H
|
|
|
|
#include "ActivMiddleware/ActivBase/Ip/IpAddress.h"
|
|
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
|
|
#include "ActivMiddleware/ActivBase/MessageValidator.h"
|
|
#include "ActivMiddleware/ActivBase/StringException.h"
|
|
#include "ActivMiddleware/CxxYZ/functional.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! Mac address class.
|
|
class MacAddress
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
MacAddress();
|
|
|
|
//! Get the mac address data.
|
|
const void *Get() const;
|
|
|
|
/**
|
|
* @brief Set the mac address.
|
|
*
|
|
* @param pData pointer to mac address data, must be @a SIZE bytes large.
|
|
*/
|
|
void Set(const void *pData);
|
|
|
|
//! Is the MAC address valid?
|
|
bool IsValid() const;
|
|
|
|
//! Equality operator.
|
|
bool operator==(const MacAddress &rhs) const;
|
|
|
|
//! Less than operator.
|
|
bool operator<(const MacAddress &rhs) const;
|
|
|
|
//! Convert to a string representation.
|
|
String ToString() const;
|
|
|
|
/**
|
|
* @brief Serialize into a message builder.
|
|
*
|
|
* @param messageBuilder
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
StatusCode Serialize(MessageBuilder &messageBuilder) const;
|
|
|
|
/**
|
|
* @brief Deserialize from a message validator.
|
|
*
|
|
* @param messageValidator
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
StatusCode Deserialize(MessageValidator &messageValidator);
|
|
|
|
/**
|
|
* @brief Convert from a string representation.
|
|
*
|
|
* @param string
|
|
*
|
|
* @return a mac address.
|
|
*
|
|
* @throw std::exception
|
|
*/
|
|
static MacAddress FromString(const String &string);
|
|
|
|
/**
|
|
* @brief Convert from a multicast ip address.
|
|
*
|
|
* @param ipAddress
|
|
*
|
|
* @return a mac address.
|
|
*
|
|
* @throw std::exception
|
|
*/
|
|
static MacAddress FromMulticastIpAddress(const IpAddress &ipAddress);
|
|
|
|
//! The size of a mac address.
|
|
static const size_t SIZE = 6;
|
|
|
|
private:
|
|
uint8_t m_data[SIZE];
|
|
|
|
friend std::size_t hash_value(const Activ::MacAddress &macAddress);
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MacAddress::MacAddress()
|
|
{
|
|
std::memset(m_data, 0, SIZE);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const void *MacAddress::Get() const
|
|
{
|
|
return m_data;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void MacAddress::Set(const void *pData)
|
|
{
|
|
std::memcpy(m_data, pData, SIZE);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MacAddress::IsValid() const
|
|
{
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint8_t zeroData[SIZE] = { 0, 0, 0, 0, 0, 0 };
|
|
return (0 != std::memcmp(m_data, zeroData, SIZE));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MacAddress::operator==(const MacAddress &rhs) const
|
|
{
|
|
return (0 == std::memcmp(m_data, rhs.m_data, SIZE));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool MacAddress::operator<(const MacAddress &rhs) const
|
|
{
|
|
return (std::memcmp(m_data, rhs.m_data, SIZE) < 0);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline String MacAddress::ToString() const
|
|
{
|
|
char buffer[18];
|
|
(void) snprintf(&buffer[0], ACTIV_ARRAY_SIZE(buffer), "%02x-%02x-%02x-%02x-%02x-%02x",
|
|
uint32_t(m_data[0]),
|
|
uint32_t(m_data[1]),
|
|
uint32_t(m_data[2]),
|
|
uint32_t(m_data[3]),
|
|
uint32_t(m_data[4]),
|
|
uint32_t(m_data[5]));
|
|
|
|
return String(&buffer[0], ACTIV_ARRAY_SIZE(buffer));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MacAddress::Serialize(MessageBuilder &messageBuilder) const
|
|
{
|
|
return messageBuilder.AppendBuffer(m_data, SIZE);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MacAddress::Deserialize(MessageValidator &messageValidator)
|
|
{
|
|
return messageValidator.ValidateBytesCopy(SIZE, m_data);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MacAddress MacAddress::FromString(const String &string)
|
|
{
|
|
#define ACTIV_MAC_SCN SCNx8
|
|
typedef uint8_t ScanType;
|
|
|
|
const char *formatArray[] =
|
|
{
|
|
"%" ACTIV_MAC_SCN ":%" ACTIV_MAC_SCN ":%" ACTIV_MAC_SCN ":%" ACTIV_MAC_SCN ":%" ACTIV_MAC_SCN ":%" ACTIV_MAC_SCN "%*c",
|
|
"%" ACTIV_MAC_SCN "-%" ACTIV_MAC_SCN "-%" ACTIV_MAC_SCN "-%" ACTIV_MAC_SCN "-%" ACTIV_MAC_SCN "-%" ACTIV_MAC_SCN "%*c",
|
|
"%2" ACTIV_MAC_SCN "%2" ACTIV_MAC_SCN "%2" ACTIV_MAC_SCN "%2" ACTIV_MAC_SCN "%2" ACTIV_MAC_SCN "%2" ACTIV_MAC_SCN "%*c"
|
|
};
|
|
|
|
#undef ACTIV_MAC_SCN
|
|
|
|
for (size_t i = 0; i < ACTIV_ARRAY_SIZE(formatArray); ++i)
|
|
{
|
|
ScanType tmp[SIZE];
|
|
|
|
if (6 == ::sscanf(string.c_str(), formatArray[i], &(tmp[0]), &(tmp[1]), &(tmp[2]), &(tmp[3]), &(tmp[4]), &(tmp[5])))
|
|
{
|
|
MacAddress macAddress;
|
|
|
|
macAddress.Set(tmp);
|
|
|
|
return macAddress;
|
|
}
|
|
}
|
|
|
|
ACTIV_BOOST_THROW_EXCEPTION(StringException("invalid MAC address '" + string + "'"));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MacAddress MacAddress::FromMulticastIpAddress(const IpAddress &ipAddress)
|
|
{
|
|
if (ACTIV_UNLIKELY(AF_INET != ipAddress.GetFamily()))
|
|
ACTIV_BOOST_THROW_EXCEPTION(StringException("only mapping of an ipv4 multicast address onto MAC address is supported"));
|
|
|
|
// Map multicast ipv4 address onto MAC, see:
|
|
//
|
|
// http://www.tcpipguide.com/free/t_TCPIPAddressResolutionForIPMulticastAddresses.htm
|
|
const uint32_t value = NetworkToHostOrder(ipAddress.GetAddress().AddressIn.sin_addr.s_addr);
|
|
|
|
MacAddress macAddress;
|
|
macAddress.m_data[0] = 0x01;
|
|
macAddress.m_data[1] = 0x00;
|
|
macAddress.m_data[2] = 0x5e;
|
|
macAddress.m_data[3] = (value >> 16) & 0x7f;
|
|
macAddress.m_data[4] = (value >> 8) & 0xff;
|
|
macAddress.m_data[5] = value & 0xff;
|
|
|
|
return macAddress;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline std::size_t hash_value(const MacAddress &macAddress)
|
|
{
|
|
return Boost::hash_range(macAddress.m_data, macAddress.m_data + macAddress.SIZE);
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ACTIV_CXXYZ_HASH_NAMESPACE
|
|
{
|
|
|
|
template<>
|
|
struct hash<Activ::MacAddress>
|
|
{
|
|
size_t operator()(const Activ::MacAddress &macAddress) const
|
|
{
|
|
return hash_value(macAddress);
|
|
}
|
|
};
|
|
|
|
} // namespace ACTIV_CXXYZ_HASH_NAMESPACE
|
|
|
|
#endif // (!defined ACTIV_MIDDLEWARE_MISC_MAC_ADDRESS_H)
|