diff --git a/include/ip_address.h b/include/ip_address.h index 6ae4877..0a2ad56 100644 --- a/include/ip_address.h +++ b/include/ip_address.h @@ -33,110 +33,123 @@ #include #include #include +#include "cxxstd.h" namespace Tins { +/** + * \class IPv4Address + * \brief Abstraction of an IPv4 address. + */ +class IPv4Address { +public: /** - * \class IPv4Address - * \brief Abstraction of an IPv4 address. + * The address size. */ - class IPv4Address { - public: - /** - * The address size. - */ - static const size_t address_size = sizeof(uint32_t); - - /** - * \brief Constructor taking a const char*. - * - * Constructs an IPv4Address from a dotted-notation address - * cstring. If the pointer provided is null, then a default - * IPv4Address object is constructed, which corresponds to - * the 0.0.0.0 address. - * - * \param ip const char* containing the dotted-notation address. - */ - IPv4Address(const char *ip = 0); - - /** - * \brief Constructor taking a std::string. - * - * Constructs an IPv4Address from a dotted-notation std::strings - * - * \param ip std::string containing the dotted-notation address. - */ - IPv4Address(const std::string &ip); - - /** - * \brief Constructor taking a IP address represented as a - * big endian integer. - * - * This constructor should be used internally by PDUs that - * handle IP addresses. The provided integer must be - * be in big endian. - */ - explicit IPv4Address(uint32_t ip); - - /** - * \brief User defined conversion to big endian integral value. - */ - operator uint32_t() const; - - /** - * \brief Retrieve the string representation of this address. - * - * \return std::string containing the representation of this address. - */ - std::string to_string() const; - - /** - * \brief Compare this IPv4Address for equality. - * - * \param rhs The address to be compared. - * \return bool indicating whether this address equals rhs. - */ - bool operator==(const IPv4Address &rhs) const { - return ip_addr == rhs.ip_addr; - } - - /** - * \brief Compare this IPv4Address for inequality. - * - * \param rhs The address to be compared. - * \return bool indicating whether this address is distinct - * from rhs. - */ - bool operator!=(const IPv4Address &rhs) const { - return !(*this == rhs); - } - - /** - * \brief Compare this IPv4Address for less-than inequality. - * - * \param rhs The address to be compared. - * \return bool indicating whether this address is less-than rhs. - */ - bool operator< (const IPv4Address &rhs) const { - return ip_addr < rhs.ip_addr; - } - - /** - * \brief Writes this address to a std::ostream. - * - * This method writes addr in a dotted-string notation address - * to the std::ostream argument. - * - * \param output The std::ostream in which to write the address. - * \param addr The IPv4Address to be written. - * \return std::stream& pointing to output. - */ - friend std::ostream &operator<<(std::ostream &output, const IPv4Address &addr); - private: - uint32_t ip_to_int(const std::string &ip); + static const size_t address_size = sizeof(uint32_t); - uint32_t ip_addr; - }; -} + /** + * \brief Constructor taking a const char*. + * + * Constructs an IPv4Address from a dotted-notation address + * cstring. If the pointer provided is null, then a default + * IPv4Address object is constructed, which corresponds to + * the 0.0.0.0 address. + * + * \param ip const char* containing the dotted-notation address. + */ + IPv4Address(const char *ip = 0); + + /** + * \brief Constructor taking a std::string. + * + * Constructs an IPv4Address from a dotted-notation std::strings + * + * \param ip std::string containing the dotted-notation address. + */ + IPv4Address(const std::string &ip); + + /** + * \brief Constructor taking a IP address represented as a + * big endian integer. + * + * This constructor should be used internally by PDUs that + * handle IP addresses. The provided integer must be + * be in big endian. + */ + explicit IPv4Address(uint32_t ip); + + /** + * \brief User defined conversion to big endian integral value. + */ + operator uint32_t() const; + + /** + * \brief Retrieve the string representation of this address. + * + * \return std::string containing the representation of this address. + */ + std::string to_string() const; + + /** + * \brief Compare this IPv4Address for equality. + * + * \param rhs The address to be compared. + * \return bool indicating whether this address equals rhs. + */ + bool operator==(const IPv4Address &rhs) const { + return ip_addr == rhs.ip_addr; + } + + /** + * \brief Compare this IPv4Address for inequality. + * + * \param rhs The address to be compared. + * \return bool indicating whether this address is distinct + * from rhs. + */ + bool operator!=(const IPv4Address &rhs) const { + return !(*this == rhs); + } + + /** + * \brief Compare this IPv4Address for less-than inequality. + * + * \param rhs The address to be compared. + * \return bool indicating whether this address is less-than rhs. + */ + bool operator< (const IPv4Address &rhs) const { + return ip_addr < rhs.ip_addr; + } + + /** + * \brief Writes this address to a std::ostream. + * + * This method writes addr in a dotted-string notation address + * to the std::ostream argument. + * + * \param output The std::ostream in which to write the address. + * \param addr The IPv4Address to be written. + * \return std::stream& pointing to output. + */ + friend std::ostream &operator<<(std::ostream &output, const IPv4Address &addr); +private: + uint32_t ip_to_int(const std::string &ip); + + uint32_t ip_addr; +}; +} //namespace Tins + +#if TINS_IS_CXX11 +namespace std +{ +template<> +struct hash { + size_t operator()(const Tins::IPv4Address &addr) const { + return std::hash()(addr); + } +}; +} // namespace std +#endif #endif // TINS_IPADDRESS_H diff --git a/include/ipv6_address.h b/include/ipv6_address.h index 11856d2..8d4499a 100644 --- a/include/ipv6_address.h +++ b/include/ipv6_address.h @@ -33,6 +33,7 @@ #include #include #include +#include "cxxstd.h" namespace Tins { class IPv6Address { @@ -192,6 +193,18 @@ private: uint8_t address[address_size]; }; -} +} //namespace Tins + +#if TINS_IS_CXX11 +namespace std +{ +template<> +struct hash { + size_t operator()(const Tins::IPv6Address &addr) const { + return std::hash()(addr.to_string()); + } +}; +} // namespace std +#endif #endif // TINS_IPV6_ADDRESS