5 // Copyright (c) 2003-2017 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/cstdint.hpp>
22 #include <boost/asio/detail/socket_types.hpp>
23 #include <boost/asio/detail/string_view.hpp>
24 #include <boost/asio/detail/winsock_init.hpp>
25 #include <boost/system/error_code.hpp>
27 #if !defined(BOOST_ASIO_NO_IOSTREAM)
29 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
31 #include <boost/asio/detail/push_options.hpp>
37 /// Implements IP version 4 style addresses.
39 * The boost::asio::ip::address_v4 class provides the ability to use and
40 * manipulate IP version 4 addresses.
43 * @e Distinct @e objects: Safe.@n
44 * @e Shared @e objects: Unsafe.
49 /// The type used to represent an address as an unsigned integer.
50 typedef uint_least32_t uint_type;
52 /// The type used to represent an address as an array of bytes.
54 * @note This type is defined in terms of the C++0x template @c std::array
55 * when it is available. Otherwise, it uses @c boost:array.
57 #if defined(GENERATING_DOCUMENTATION)
58 typedef array<unsigned char, 4> bytes_type;
60 typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
63 /// Default constructor.
69 /// Construct an address from raw bytes.
70 BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
72 /// Construct an address from an unsigned integer in host byte order.
73 BOOST_ASIO_DECL explicit address_v4(uint_type addr);
76 address_v4(const address_v4& other)
81 #if defined(BOOST_ASIO_HAS_MOVE)
83 address_v4(address_v4&& other)
87 #endif // defined(BOOST_ASIO_HAS_MOVE)
89 /// Assign from another address.
90 address_v4& operator=(const address_v4& other)
96 #if defined(BOOST_ASIO_HAS_MOVE)
97 /// Move-assign from another address.
98 address_v4& operator=(address_v4&& other)
103 #endif // defined(BOOST_ASIO_HAS_MOVE)
105 /// Get the address in bytes, in network byte order.
106 BOOST_ASIO_DECL bytes_type to_bytes() const;
108 /// Get the address as an unsigned integer in host byte order
109 BOOST_ASIO_DECL uint_type to_uint() const;
111 #if !defined(BOOST_ASIO_NO_DEPRECATED)
112 /// Get the address as an unsigned long in host byte order
113 BOOST_ASIO_DECL unsigned long to_ulong() const;
114 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
116 /// Get the address as a string in dotted decimal format.
117 BOOST_ASIO_DECL std::string to_string() const;
119 #if !defined(BOOST_ASIO_NO_DEPRECATED)
120 /// (Deprecated: Use other overload.) Get the address as a string in dotted
122 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
124 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
125 /// string in dotted decimal form.
126 static address_v4 from_string(const char* str);
128 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
129 /// string in dotted decimal form.
130 static address_v4 from_string(
131 const char* str, boost::system::error_code& ec);
133 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
134 /// string in dotted decimal form.
135 static address_v4 from_string(const std::string& str);
137 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
138 /// string in dotted decimal form.
139 static address_v4 from_string(
140 const std::string& str, boost::system::error_code& ec);
141 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
143 /// Determine whether the address is a loopback address.
144 BOOST_ASIO_DECL bool is_loopback() const;
146 /// Determine whether the address is unspecified.
147 BOOST_ASIO_DECL bool is_unspecified() const;
149 #if !defined(BOOST_ASIO_NO_DEPRECATED)
150 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
152 BOOST_ASIO_DECL bool is_class_a() const;
154 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
156 BOOST_ASIO_DECL bool is_class_b() const;
158 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
160 BOOST_ASIO_DECL bool is_class_c() const;
161 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
163 /// Determine whether the address is a multicast address.
164 BOOST_ASIO_DECL bool is_multicast() const;
166 /// Compare two addresses for equality.
167 friend bool operator==(const address_v4& a1, const address_v4& a2)
169 return a1.addr_.s_addr == a2.addr_.s_addr;
172 /// Compare two addresses for inequality.
173 friend bool operator!=(const address_v4& a1, const address_v4& a2)
175 return a1.addr_.s_addr != a2.addr_.s_addr;
178 /// Compare addresses for ordering.
179 friend bool operator<(const address_v4& a1, const address_v4& a2)
181 return a1.to_uint() < a2.to_uint();
184 /// Compare addresses for ordering.
185 friend bool operator>(const address_v4& a1, const address_v4& a2)
187 return a1.to_uint() > a2.to_uint();
190 /// Compare addresses for ordering.
191 friend bool operator<=(const address_v4& a1, const address_v4& a2)
193 return a1.to_uint() <= a2.to_uint();
196 /// Compare addresses for ordering.
197 friend bool operator>=(const address_v4& a1, const address_v4& a2)
199 return a1.to_uint() >= a2.to_uint();
202 /// Obtain an address object that represents any address.
203 static address_v4 any()
208 /// Obtain an address object that represents the loopback address.
209 static address_v4 loopback()
211 return address_v4(0x7F000001);
214 /// Obtain an address object that represents the broadcast address.
215 static address_v4 broadcast()
217 return address_v4(0xFFFFFFFF);
220 #if !defined(BOOST_ASIO_NO_DEPRECATED)
221 /// (Deprecated: Use network_v4 class.) Obtain an address object that
222 /// represents the broadcast address that corresponds to the specified
223 /// address and netmask.
224 BOOST_ASIO_DECL static address_v4 broadcast(
225 const address_v4& addr, const address_v4& mask);
227 /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
228 /// to the address, based on its address class.
229 BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
230 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
233 // The underlying IPv4 address.
234 boost::asio::detail::in4_addr_type addr_;
237 /// Create an IPv4 address from raw bytes in network order.
239 * @relates address_v4
241 inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
243 return address_v4(bytes);
246 /// Create an IPv4 address from an unsigned integer in host byte order.
248 * @relates address_v4
250 inline address_v4 make_address_v4(address_v4::uint_type addr)
252 return address_v4(addr);
255 /// Create an IPv4 address from an IP address string in dotted decimal form.
257 * @relates address_v4
259 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
261 /// Create an IPv4 address from an IP address string in dotted decimal form.
263 * @relates address_v4
265 BOOST_ASIO_DECL address_v4 make_address_v4(
266 const char* str, boost::system::error_code& ec);
268 /// Create an IPv4 address from an IP address string in dotted decimal form.
270 * @relates address_v4
272 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
274 /// Create an IPv4 address from an IP address string in dotted decimal form.
276 * @relates address_v4
278 BOOST_ASIO_DECL address_v4 make_address_v4(
279 const std::string& str, boost::system::error_code& ec);
281 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
282 || defined(GENERATING_DOCUMENTATION)
284 /// Create an IPv4 address from an IP address string in dotted decimal form.
286 * @relates address_v4
288 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
290 /// Create an IPv4 address from an IP address string in dotted decimal form.
292 * @relates address_v4
294 BOOST_ASIO_DECL address_v4 make_address_v4(
295 string_view str, boost::system::error_code& ec);
297 #endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
298 // || defined(GENERATING_DOCUMENTATION)
300 #if !defined(BOOST_ASIO_NO_IOSTREAM)
302 /// Output an address as a string.
304 * Used to output a human-readable string for a specified address.
306 * @param os The output stream to which the string will be written.
308 * @param addr The address to be written.
310 * @return The output stream.
312 * @relates boost::asio::ip::address_v4
314 template <typename Elem, typename Traits>
315 std::basic_ostream<Elem, Traits>& operator<<(
316 std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
318 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
324 #include <boost/asio/detail/pop_options.hpp>
326 #include <boost/asio/ip/impl/address_v4.hpp>
327 #if defined(BOOST_ASIO_HEADER_ONLY)
328 # include <boost/asio/ip/impl/address_v4.ipp>
329 #endif // defined(BOOST_ASIO_HEADER_ONLY)
331 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP