// // address.hpp // ~~~~~~~~~~~ // // Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_ASIO_IPV4_ADDRESS_HPP #define BOOST_ASIO_IPV4_ADDRESS_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include #include #include #include namespace boost { namespace asio { namespace ipv4 { /// Implements IP version 4 style addresses. /** * The boost::asio::ipv4::address class provides the ability to use and * manipulate IP version 4 addresses. * * @par Thread Safety: * @e Distinct @e objects: Safe.@n * @e Shared @e objects: Unsafe. */ class address { public: /// Default constructor. address() { addr_.s_addr = 0; } /// Construct an address from a unsigned long in host byte order. address(unsigned long addr) { addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(addr); } /// Construct an address using an IP address string in dotted decimal form. address(const char* host) { if (boost::asio::detail::socket_ops::inet_pton(AF_INET, host, &addr_) <= 0) { boost::asio::error e(boost::asio::detail::socket_ops::get_error()); boost::throw_exception(e); } } /// Construct an address using an IP address string in dotted decimal form. address(const std::string& host) { if (boost::asio::detail::socket_ops::inet_pton( AF_INET, host.c_str(), &addr_) <= 0) { boost::asio::error e(boost::asio::detail::socket_ops::get_error()); boost::throw_exception(e); } } /// Copy constructor. address(const address& other) : addr_(other.addr_) { } /// Assign from another address. address& operator=(const address& other) { addr_ = other.addr_; return *this; } /// Assign from an unsigned long. address& operator=(unsigned long addr) { addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(addr); return *this; } /// Assign from an IP address string in dotted decimal form. address& operator=(const char* addr) { address tmp(addr); addr_ = tmp.addr_; return *this; } /// Assign from an IP address string in dotted decimal form. address& operator=(const std::string& addr) { address tmp(addr); addr_ = tmp.addr_; return *this; } /// Get the address as an unsigned long in host byte order unsigned long to_ulong() const { return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr); } /// Get the address as a string in dotted decimal format. std::string to_string() const { char addr_str[boost::asio::detail::max_addr_str_len]; const char* addr = boost::asio::detail::socket_ops::inet_ntop(AF_INET, &addr_, addr_str, boost::asio::detail::max_addr_str_len); if (addr == 0) { boost::asio::error e(boost::asio::detail::socket_ops::get_error()); boost::throw_exception(e); } return addr; } /// Determine whether the address is a class A address. bool is_class_A() const { return IN_CLASSA(to_ulong()); } /// Determine whether the address is a class B address. bool is_class_B() const { return IN_CLASSB(to_ulong()); } /// Determine whether the address is a class C address. bool is_class_C() const { return IN_CLASSC(to_ulong()); } /// Determine whether the address is a class D address. bool is_class_D() const { return IN_CLASSD(to_ulong()); } /// Determine whether the address is a multicast address. bool is_multicast() const { return IN_MULTICAST(to_ulong()); } /// Compare two addresses for equality. friend bool operator==(const address& a1, const address& a2) { return a1.addr_.s_addr == a2.addr_.s_addr; } /// Compare two addresses for inequality. friend bool operator!=(const address& a1, const address& a2) { return a1.addr_.s_addr != a2.addr_.s_addr; } /// Compare addresses for ordering. friend bool operator<(const address& a1, const address& a2) { return a1.to_ulong() < a2.to_ulong(); } /// Obtain an address object that represents any address. static address any() { return address(static_cast(INADDR_ANY)); } /// Obtain an address object that represents the loopback address. static address loopback() { return address(static_cast(INADDR_LOOPBACK)); } /// Obtain an address object that represents the broadcast address. static address broadcast() { return address(static_cast(INADDR_BROADCAST)); } private: // The underlying IPv4 address. in_addr addr_; }; /// Output an address as a string. /** * Used to output a human-readable string for a specified address. * * @param os The output stream to which the string will be written. * * @param addr The address to be written. * * @return The output stream. * * @relates tcp::endpoint */ template Ostream& operator<<(Ostream& os, const address& addr) { os << addr.to_string(); return os; } } // namespace ipv4 } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_IPV4_ADDRESS_HPP