5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_IP_ADDRESS_V4_HPP
12 #define BOOST_ASIO_IP_ADDRESS_V4_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/detail/array.hpp>
21 #include <boost/asio/detail/socket_types.hpp>
22 #include <boost/asio/detail/winsock_init.hpp>
23 #include <boost/system/error_code.hpp>
25 #if !defined(BOOST_ASIO_NO_IOSTREAM)
27 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
29 #include <boost/asio/detail/push_options.hpp>
35 /// Implements IP version 4 style addresses.
37 * The boost::asio::ip::address_v4 class provides the ability to use and
38 * manipulate IP version 4 addresses.
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
47 /// The type used to represent an address as an array of bytes.
49 * @note This type is defined in terms of the C++0x template @c std::array
50 * when it is available. Otherwise, it uses @c boost:array.
52 #if defined(GENERATING_DOCUMENTATION)
53 typedef array<unsigned char, 4> bytes_type;
55 typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
58 /// Default constructor.
64 /// Construct an address from raw bytes.
65 BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
67 /// Construct an address from a unsigned long in host byte order.
68 BOOST_ASIO_DECL explicit address_v4(unsigned long addr);
71 address_v4(const address_v4& other)
76 #if defined(BOOST_ASIO_HAS_MOVE)
78 address_v4(address_v4&& other)
82 #endif // defined(BOOST_ASIO_HAS_MOVE)
84 /// Assign from another address.
85 address_v4& operator=(const address_v4& other)
91 #if defined(BOOST_ASIO_HAS_MOVE)
92 /// Move-assign from another address.
93 address_v4& operator=(address_v4&& other)
98 #endif // defined(BOOST_ASIO_HAS_MOVE)
100 /// Get the address in bytes, in network byte order.
101 BOOST_ASIO_DECL bytes_type to_bytes() const;
103 /// Get the address as an unsigned long in host byte order
104 BOOST_ASIO_DECL unsigned long to_ulong() const;
106 /// Get the address as a string in dotted decimal format.
107 BOOST_ASIO_DECL std::string to_string() const;
109 /// Get the address as a string in dotted decimal format.
110 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
112 /// Create an address from an IP address string in dotted decimal form.
113 BOOST_ASIO_DECL static address_v4 from_string(const char* str);
115 /// Create an address from an IP address string in dotted decimal form.
116 BOOST_ASIO_DECL static address_v4 from_string(
117 const char* str, boost::system::error_code& ec);
119 /// Create an address from an IP address string in dotted decimal form.
120 BOOST_ASIO_DECL static address_v4 from_string(const std::string& str);
122 /// Create an address from an IP address string in dotted decimal form.
123 BOOST_ASIO_DECL static address_v4 from_string(
124 const std::string& str, boost::system::error_code& ec);
126 /// Determine whether the address is a loopback address.
127 BOOST_ASIO_DECL bool is_loopback() const;
129 /// Determine whether the address is unspecified.
130 BOOST_ASIO_DECL bool is_unspecified() const;
132 /// Determine whether the address is a class A address.
133 BOOST_ASIO_DECL bool is_class_a() const;
135 /// Determine whether the address is a class B address.
136 BOOST_ASIO_DECL bool is_class_b() const;
138 /// Determine whether the address is a class C address.
139 BOOST_ASIO_DECL bool is_class_c() const;
141 /// Determine whether the address is a multicast address.
142 BOOST_ASIO_DECL bool is_multicast() const;
144 /// Compare two addresses for equality.
145 friend bool operator==(const address_v4& a1, const address_v4& a2)
147 return a1.addr_.s_addr == a2.addr_.s_addr;
150 /// Compare two addresses for inequality.
151 friend bool operator!=(const address_v4& a1, const address_v4& a2)
153 return a1.addr_.s_addr != a2.addr_.s_addr;
156 /// Compare addresses for ordering.
157 friend bool operator<(const address_v4& a1, const address_v4& a2)
159 return a1.to_ulong() < a2.to_ulong();
162 /// Compare addresses for ordering.
163 friend bool operator>(const address_v4& a1, const address_v4& a2)
165 return a1.to_ulong() > a2.to_ulong();
168 /// Compare addresses for ordering.
169 friend bool operator<=(const address_v4& a1, const address_v4& a2)
171 return a1.to_ulong() <= a2.to_ulong();
174 /// Compare addresses for ordering.
175 friend bool operator>=(const address_v4& a1, const address_v4& a2)
177 return a1.to_ulong() >= a2.to_ulong();
180 /// Obtain an address object that represents any address.
181 static address_v4 any()
186 /// Obtain an address object that represents the loopback address.
187 static address_v4 loopback()
189 return address_v4(0x7F000001);
192 /// Obtain an address object that represents the broadcast address.
193 static address_v4 broadcast()
195 return address_v4(0xFFFFFFFF);
198 /// Obtain an address object that represents the broadcast address that
199 /// corresponds to the specified address and netmask.
200 BOOST_ASIO_DECL static address_v4 broadcast(
201 const address_v4& addr, const address_v4& mask);
203 /// Obtain the netmask that corresponds to the address, based on its address
205 BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
208 // The underlying IPv4 address.
209 boost::asio::detail::in4_addr_type addr_;
212 #if !defined(BOOST_ASIO_NO_IOSTREAM)
214 /// Output an address as a string.
216 * Used to output a human-readable string for a specified address.
218 * @param os The output stream to which the string will be written.
220 * @param addr The address to be written.
222 * @return The output stream.
224 * @relates boost::asio::ip::address_v4
226 template <typename Elem, typename Traits>
227 std::basic_ostream<Elem, Traits>& operator<<(
228 std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
230 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
236 #include <boost/asio/detail/pop_options.hpp>
238 #include <boost/asio/ip/impl/address_v4.hpp>
239 #if defined(BOOST_ASIO_HEADER_ONLY)
240 # include <boost/asio/ip/impl/address_v4.ipp>
241 #endif // defined(BOOST_ASIO_HEADER_ONLY)
243 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP