Chris@16: // Chris@16: // ip/address_v4.hpp Chris@16: // ~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_IP_ADDRESS_V4_HPP Chris@16: #define BOOST_ASIO_IP_ADDRESS_V4_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: # include Chris@16: #endif // !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace ip { Chris@16: Chris@16: /// Implements IP version 4 style addresses. Chris@16: /** Chris@16: * The boost::asio::ip::address_v4 class provides the ability to use and Chris@16: * manipulate IP version 4 addresses. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: */ Chris@16: class address_v4 Chris@16: { Chris@16: public: Chris@16: /// The type used to represent an address as an array of bytes. Chris@16: /** Chris@16: * @note This type is defined in terms of the C++0x template @c std::array Chris@16: * when it is available. Otherwise, it uses @c boost:array. Chris@16: */ Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef array bytes_type; Chris@16: #else Chris@16: typedef boost::asio::detail::array bytes_type; Chris@16: #endif Chris@16: Chris@16: /// Default constructor. Chris@16: address_v4() Chris@16: { Chris@16: addr_.s_addr = 0; Chris@16: } Chris@16: Chris@16: /// Construct an address from raw bytes. Chris@16: BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes); Chris@16: Chris@16: /// Construct an address from a unsigned long in host byte order. Chris@16: BOOST_ASIO_DECL explicit address_v4(unsigned long addr); Chris@16: Chris@16: /// Copy constructor. Chris@16: address_v4(const address_v4& other) Chris@16: : addr_(other.addr_) Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: /// Move constructor. Chris@16: address_v4(address_v4&& other) Chris@16: : addr_(other.addr_) Chris@16: { Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: /// Assign from another address. Chris@16: address_v4& operator=(const address_v4& other) Chris@16: { Chris@16: addr_ = other.addr_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: /// Move-assign from another address. Chris@16: address_v4& operator=(address_v4&& other) Chris@16: { Chris@16: addr_ = other.addr_; Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: /// Get the address in bytes, in network byte order. Chris@16: BOOST_ASIO_DECL bytes_type to_bytes() const; Chris@16: Chris@16: /// Get the address as an unsigned long in host byte order Chris@16: BOOST_ASIO_DECL unsigned long to_ulong() const; Chris@16: Chris@16: /// Get the address as a string in dotted decimal format. Chris@16: BOOST_ASIO_DECL std::string to_string() const; Chris@16: Chris@16: /// Get the address as a string in dotted decimal format. Chris@16: BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const; Chris@16: Chris@16: /// Create an address from an IP address string in dotted decimal form. Chris@16: BOOST_ASIO_DECL static address_v4 from_string(const char* str); Chris@16: Chris@16: /// Create an address from an IP address string in dotted decimal form. Chris@16: BOOST_ASIO_DECL static address_v4 from_string( Chris@16: const char* str, boost::system::error_code& ec); Chris@16: Chris@16: /// Create an address from an IP address string in dotted decimal form. Chris@16: BOOST_ASIO_DECL static address_v4 from_string(const std::string& str); Chris@16: Chris@16: /// Create an address from an IP address string in dotted decimal form. Chris@16: BOOST_ASIO_DECL static address_v4 from_string( Chris@16: const std::string& str, boost::system::error_code& ec); Chris@16: Chris@16: /// Determine whether the address is a loopback address. Chris@16: BOOST_ASIO_DECL bool is_loopback() const; Chris@16: Chris@16: /// Determine whether the address is unspecified. Chris@16: BOOST_ASIO_DECL bool is_unspecified() const; Chris@16: Chris@16: /// Determine whether the address is a class A address. Chris@16: BOOST_ASIO_DECL bool is_class_a() const; Chris@16: Chris@16: /// Determine whether the address is a class B address. Chris@16: BOOST_ASIO_DECL bool is_class_b() const; Chris@16: Chris@16: /// Determine whether the address is a class C address. Chris@16: BOOST_ASIO_DECL bool is_class_c() const; Chris@16: Chris@16: /// Determine whether the address is a multicast address. Chris@16: BOOST_ASIO_DECL bool is_multicast() const; Chris@16: Chris@16: /// Compare two addresses for equality. Chris@16: friend bool operator==(const address_v4& a1, const address_v4& a2) Chris@16: { Chris@16: return a1.addr_.s_addr == a2.addr_.s_addr; Chris@16: } Chris@16: Chris@16: /// Compare two addresses for inequality. Chris@16: friend bool operator!=(const address_v4& a1, const address_v4& a2) Chris@16: { Chris@16: return a1.addr_.s_addr != a2.addr_.s_addr; Chris@16: } Chris@16: Chris@16: /// Compare addresses for ordering. Chris@16: friend bool operator<(const address_v4& a1, const address_v4& a2) Chris@16: { Chris@16: return a1.to_ulong() < a2.to_ulong(); Chris@16: } Chris@16: Chris@16: /// Compare addresses for ordering. Chris@16: friend bool operator>(const address_v4& a1, const address_v4& a2) Chris@16: { Chris@16: return a1.to_ulong() > a2.to_ulong(); Chris@16: } Chris@16: Chris@16: /// Compare addresses for ordering. Chris@16: friend bool operator<=(const address_v4& a1, const address_v4& a2) Chris@16: { Chris@16: return a1.to_ulong() <= a2.to_ulong(); Chris@16: } Chris@16: Chris@16: /// Compare addresses for ordering. Chris@16: friend bool operator>=(const address_v4& a1, const address_v4& a2) Chris@16: { Chris@16: return a1.to_ulong() >= a2.to_ulong(); Chris@16: } Chris@16: Chris@16: /// Obtain an address object that represents any address. Chris@16: static address_v4 any() Chris@16: { Chris@16: return address_v4(); Chris@16: } Chris@16: Chris@16: /// Obtain an address object that represents the loopback address. Chris@16: static address_v4 loopback() Chris@16: { Chris@16: return address_v4(0x7F000001); Chris@16: } Chris@16: Chris@16: /// Obtain an address object that represents the broadcast address. Chris@16: static address_v4 broadcast() Chris@16: { Chris@16: return address_v4(0xFFFFFFFF); Chris@16: } Chris@16: Chris@16: /// Obtain an address object that represents the broadcast address that Chris@16: /// corresponds to the specified address and netmask. Chris@16: BOOST_ASIO_DECL static address_v4 broadcast( Chris@16: const address_v4& addr, const address_v4& mask); Chris@16: Chris@16: /// Obtain the netmask that corresponds to the address, based on its address Chris@16: /// class. Chris@16: BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr); Chris@16: Chris@16: private: Chris@16: // The underlying IPv4 address. Chris@16: boost::asio::detail::in4_addr_type addr_; Chris@16: }; Chris@16: Chris@16: #if !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: Chris@16: /// Output an address as a string. Chris@16: /** Chris@16: * Used to output a human-readable string for a specified address. Chris@16: * Chris@16: * @param os The output stream to which the string will be written. Chris@16: * Chris@16: * @param addr The address to be written. Chris@16: * Chris@16: * @return The output stream. Chris@16: * Chris@16: * @relates boost::asio::ip::address_v4 Chris@16: */ Chris@16: template Chris@16: std::basic_ostream& operator<<( Chris@16: std::basic_ostream& os, const address_v4& addr); Chris@16: Chris@16: #endif // !defined(BOOST_ASIO_NO_IOSTREAM) Chris@16: Chris@16: } // namespace ip Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #if defined(BOOST_ASIO_HEADER_ONLY) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_HEADER_ONLY) Chris@16: Chris@16: #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP