510 lines
13 KiB
C++
510 lines
13 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Ip/IpAddress.h
|
|
*
|
|
* @brief Header file for ip address class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_IP_IP_ADDRESS_H)
|
|
#define ACTIV_BASE_IP_IP_ADDRESS_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/ActivBase/MessageValidator.h"
|
|
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
|
|
|
|
#include <vector>
|
|
#include <cstring>
|
|
|
|
#if (defined ACTIV_PLATFORM_POSIX)
|
|
#include <sys/socket.h>
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
class IpAddressMask;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Ip address information.
|
|
*
|
|
* Currently supports ipv4 and ipv6 addresses.
|
|
*/
|
|
class IpAddress
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Address union.
|
|
*/
|
|
union AddressUnion
|
|
{
|
|
sockaddr Address;
|
|
sockaddr_in AddressIn; ///< Ipv4 address.
|
|
sockaddr_in6 AddressIn6; ///< Ipv6 address.
|
|
};
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* The socket type and protocol are initialized to 0.
|
|
*
|
|
* @param family the address family.
|
|
*/
|
|
IpAddress(const int family = AF_UNSPEC);
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param socketType the socket type.
|
|
* @param protocol the socket protocol.
|
|
* @param family the address family.
|
|
*/
|
|
IpAddress(const int socketType, const int protocol, const int family = AF_UNSPEC);
|
|
|
|
/**
|
|
* @brief Compare two IpAddress objects.
|
|
*
|
|
* Port number is included in the comparison.
|
|
*
|
|
* @param rhs reference to an IpAddress object to compare.
|
|
*
|
|
* @retval -ve if this is less than rhs.
|
|
* @retval 0 if this equal to rhs.
|
|
* @retval +ve if this is greater than rhs.
|
|
*/
|
|
int Compare(const IpAddress &rhs) const;
|
|
|
|
/**
|
|
* @brief Equality operator.
|
|
*
|
|
* Port number is included in the comparison.
|
|
*
|
|
* @param rhs reference to an IpAddress object to compare.
|
|
*
|
|
* @retval true if this is equal to rhs.
|
|
* @retval false if this is not equal to rhs.
|
|
*/
|
|
bool operator==(const IpAddress &rhs) const;
|
|
|
|
/**
|
|
* @brief Inequality operator.
|
|
*
|
|
* Port number is included in the comparison.
|
|
*
|
|
* @param rhs reference to an IpAddress object to compare.
|
|
*
|
|
* @retval true if this is not equal to rhs.
|
|
* @retval false if this is equal to rhs.
|
|
*/
|
|
bool operator!=(const IpAddress &rhs) const;
|
|
|
|
/**
|
|
* @brief Less than operator.
|
|
*
|
|
* Port number is included in the comparison.
|
|
*
|
|
* @param rhs reference to an IpAddress object to compare.
|
|
*
|
|
* @retval true if this is less than rhs.
|
|
* @retval false if this is greater than or equal to rhs.
|
|
*/
|
|
bool operator<(const IpAddress &rhs) const;
|
|
|
|
/**
|
|
* @brief Convert address to a string.
|
|
*
|
|
* @param shouldShowPort whether to include the port number in the string.
|
|
*
|
|
* @return The string form of the address, or an empty string is returned if the address cannot be converted.
|
|
*/
|
|
ACTIV_BASE_API String ToString(const bool shouldShowPort = true) const;
|
|
|
|
/**
|
|
* @brief Get the socket family.
|
|
*
|
|
* @return The socket family.
|
|
*/
|
|
sa_family_t GetFamily() const;
|
|
|
|
/**
|
|
* @brief Set the socket family.
|
|
*
|
|
* @param family
|
|
*/
|
|
void SetFamily(const sa_family_t family);
|
|
|
|
/**
|
|
* @brief Get the socket address length.
|
|
*
|
|
* @return The socket address length.
|
|
*/
|
|
socklen_t GetLength() const;
|
|
|
|
/**
|
|
* @brief Set the socket address length.
|
|
*
|
|
* @param length
|
|
*/
|
|
void SetLength(const socklen_t length);
|
|
|
|
/**
|
|
* @brief Get reference to the Address union.
|
|
*
|
|
* @return Address reference.
|
|
*/
|
|
AddressUnion &GetAddress();
|
|
|
|
/**
|
|
* @brief Get const reference to the Address union.
|
|
*
|
|
* @return Address reference.
|
|
*/
|
|
const AddressUnion &GetAddress() const;
|
|
|
|
/**
|
|
* @brief Get the socket type.
|
|
*
|
|
* @return The socket type.
|
|
*/
|
|
int GetType() const;
|
|
|
|
/**
|
|
* @brief Set the socket type.
|
|
*
|
|
* @param type
|
|
*/
|
|
void SetType(const int type);
|
|
|
|
/**
|
|
* @brief Get the socket protocol.
|
|
*
|
|
* @return The socket protocol.
|
|
*/
|
|
int GetProtocol() const;
|
|
|
|
/**
|
|
* @brief Set the socket protocol.
|
|
*
|
|
* @param protocol
|
|
*/
|
|
void SetProtocol(const int protocol);
|
|
|
|
/**
|
|
* @brief Clear the socket address.
|
|
*
|
|
* All bytes of the Address union are set to 0.
|
|
*/
|
|
void Clear();
|
|
|
|
/**
|
|
* @brief Get the port number, in local endian.
|
|
*
|
|
* @return Port number.
|
|
*
|
|
* @throw Exception if the address isn't an ipv4 or ipv6 address.
|
|
*/
|
|
ACTIV_BASE_API uint16_t GetPort() const;
|
|
|
|
/**
|
|
* @brief Set the port number.
|
|
*
|
|
* @param port Port number in local endian.
|
|
*
|
|
* @throw Exception if the address isn't an ipv4 or ipv6 address.
|
|
*/
|
|
ACTIV_BASE_API void SetPort(const uint16_t port);
|
|
|
|
#if (!defined ACTIV_LITE_FRAMEWORK)
|
|
|
|
/**
|
|
* @brief Get the interface index of a local ipv6 address.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API uint32_t GetInterfaceIndex() const;
|
|
|
|
/**
|
|
* @brief Is this a loopback address?
|
|
*
|
|
* @return Whether this is a loopback address.
|
|
*
|
|
* @throw Exception if the address isn't an ipv4 or ipv6 address.
|
|
*/
|
|
ACTIV_BASE_API bool IsLoopback() const;
|
|
|
|
/**
|
|
* @brief Is this a local address on the machine?
|
|
*
|
|
* The current implementation of this function is not optimized and should be used sparingly.
|
|
*
|
|
* @return Whether this is a local address.
|
|
*/
|
|
ACTIV_BASE_API bool IsLocal() const;
|
|
|
|
#endif // (!defined ACTIV_LITE_FRAMEWORK)
|
|
|
|
/**
|
|
* @brief Is this a link-local address?
|
|
*
|
|
* @return Whether this is a link-local address.
|
|
*/
|
|
ACTIV_BASE_API bool IsLinkLocal() const;
|
|
|
|
/**
|
|
* @brief Is this an unspecified address (aka an 'any' address)?
|
|
*
|
|
* @return Whether this is an unspecified address.
|
|
*
|
|
* @throw Exception if the address isn't an ipv4 or ipv6 address.
|
|
*/
|
|
ACTIV_BASE_API bool IsUnspecified() const;
|
|
|
|
/**
|
|
* @brief Is this a multicast address?
|
|
*
|
|
* @return Whether this is a multicast address.
|
|
*
|
|
* @throw Exception if the address isn't an ipv4 or ipv6 address.
|
|
*/
|
|
ACTIV_BASE_API bool IsMulticast() const;
|
|
|
|
/**
|
|
* @brief Is this address on the same network as the provided address and mask?
|
|
*
|
|
* @param network
|
|
*
|
|
* @return Boolean
|
|
*/
|
|
ACTIV_BASE_API bool IsOnNetwork(const IpAddressMask &network) const;
|
|
|
|
/**
|
|
* @brief Serialize.
|
|
*
|
|
* @param messageBuilder
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
ACTIV_BASE_API StatusCode Serialize(MessageBuilder &messageBuilder) const;
|
|
|
|
/**
|
|
* @brief Deserialize.
|
|
*
|
|
* @param messageValidator
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
ACTIV_BASE_API StatusCode Deserialize(MessageValidator &messageValidator);
|
|
|
|
private:
|
|
AddressUnion m_address; ///< Union of ipv4 and ipv6 addresses; use as appropriate based on Address.sa_family.
|
|
byte_t m_addressLength; ///< Size of useful info in m_address.
|
|
byte_t m_type; ///< Socket type.
|
|
byte_t m_protocol; ///< Protocol specific to address family.
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Type of list of ip addresses.
|
|
*/
|
|
typedef std::vector<IpAddress> IpAddressList;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IpAddress::IpAddress(const int family) :
|
|
m_addressLength(0),
|
|
m_type(0),
|
|
m_protocol(0)
|
|
{
|
|
std::memset(&m_address, 0, sizeof(m_address));
|
|
m_address.Address.sa_family = family;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IpAddress::IpAddress(const int socketType, const int protocol, const int family) :
|
|
m_addressLength(0),
|
|
m_type(static_cast<byte_t>(socketType)),
|
|
m_protocol(static_cast<byte_t>(protocol))
|
|
{
|
|
std::memset(&m_address, 0, sizeof(m_address));
|
|
m_address.Address.sa_family = family;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline int IpAddress::Compare(const IpAddress &rhs) const
|
|
{
|
|
if (m_addressLength != rhs.m_addressLength)
|
|
{
|
|
const byte_t compareLength = GetMinimum<byte_t>(m_addressLength, rhs.m_addressLength);
|
|
const int compareValue = std::memcmp(&m_address, &rhs.m_address, compareLength);
|
|
|
|
if (0 == compareValue)
|
|
return (m_addressLength == compareLength) ? -1 : 1;
|
|
else
|
|
return compareValue;
|
|
}
|
|
else
|
|
{
|
|
return std::memcmp(&m_address, &rhs.m_address, m_addressLength);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool IpAddress::operator==(const IpAddress &rhs) const
|
|
{
|
|
return 0 == Compare(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool IpAddress::operator!=(const IpAddress &rhs) const
|
|
{
|
|
return 0 != Compare(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool IpAddress::operator<(const IpAddress &rhs) const
|
|
{
|
|
return Compare(rhs) < 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline sa_family_t IpAddress::GetFamily() const
|
|
{
|
|
return m_address.Address.sa_family;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IpAddress::SetFamily(const sa_family_t family)
|
|
{
|
|
m_address.Address.sa_family = family;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline socklen_t IpAddress::GetLength() const
|
|
{
|
|
return m_addressLength;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IpAddress::SetLength(const socklen_t length)
|
|
{
|
|
m_addressLength = static_cast<byte_t>(length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IpAddress::AddressUnion &IpAddress::GetAddress()
|
|
{
|
|
return m_address;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const IpAddress::AddressUnion &IpAddress::GetAddress() const
|
|
{
|
|
return m_address;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline int IpAddress::GetType() const
|
|
{
|
|
return m_type;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IpAddress::SetType(const int type)
|
|
{
|
|
m_type = static_cast<byte_t>(type);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline int IpAddress::GetProtocol() const
|
|
{
|
|
return m_protocol;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IpAddress::SetProtocol(const int protocol)
|
|
{
|
|
m_protocol = static_cast<byte_t>(protocol);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IpAddress::Clear()
|
|
{
|
|
std::memset(&m_address, 0, sizeof(m_address));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Extension of IpAddress with netmask length.
|
|
*/
|
|
class IpAddressMask : public IpAddress
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* The socket type, protocol and netmask length are initialized to 0.
|
|
*
|
|
* @param family the address family.
|
|
*/
|
|
IpAddressMask(const int family = AF_UNSPEC);
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param ipAddress
|
|
* @param netMaskLength
|
|
*/
|
|
IpAddressMask(const IpAddress &ipAddress, const size_t netMaskLength);
|
|
|
|
size_t m_netMaskLength; ///< Length of the network mask, in bits.
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Type of list of ip address masks.
|
|
*/
|
|
typedef std::vector<IpAddressMask> IpAddressMaskList;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IpAddressMask::IpAddressMask(const int family) :
|
|
IpAddress(family),
|
|
m_netMaskLength(0)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IpAddressMask::IpAddressMask(const IpAddress &ipAddress, const size_t netMaskLength) :
|
|
IpAddress(ipAddress),
|
|
m_netMaskLength(netMaskLength)
|
|
{
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_IP_IP_ADDRESS_H)
|