5 // Copyright (c) 2003-2020 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.
64 address_v4() BOOST_ASIO_NOEXCEPT
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) BOOST_ASIO_NOEXCEPT
81 #if defined(BOOST_ASIO_HAS_MOVE)
83 address_v4(address_v4&& other) BOOST_ASIO_NOEXCEPT
87 #endif // defined(BOOST_ASIO_HAS_MOVE)
89 /// Assign from another address.
90 address_v4& operator=(const address_v4& other) BOOST_ASIO_NOEXCEPT
96 #if defined(BOOST_ASIO_HAS_MOVE)
97 /// Move-assign from another address.
98 address_v4& operator=(address_v4&& other) BOOST_ASIO_NOEXCEPT
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 BOOST_ASIO_NOEXCEPT;
108 /// Get the address as an unsigned integer in host byte order
109 BOOST_ASIO_DECL uint_type to_uint() const BOOST_ASIO_NOEXCEPT;
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 BOOST_ASIO_NOEXCEPT;
146 /// Determine whether the address is unspecified.
147 BOOST_ASIO_DECL bool is_unspecified() const BOOST_ASIO_NOEXCEPT;
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 BOOST_ASIO_NOEXCEPT;
166 /// Compare two addresses for equality.
167 friend bool operator==(const address_v4& a1,
168 const address_v4& a2) BOOST_ASIO_NOEXCEPT
170 return a1.addr_.s_addr == a2.addr_.s_addr;
173 /// Compare two addresses for inequality.
174 friend bool operator!=(const address_v4& a1,
175 const address_v4& a2) BOOST_ASIO_NOEXCEPT
177 return a1.addr_.s_addr != a2.addr_.s_addr;
180 /// Compare addresses for ordering.
181 friend bool operator<(const address_v4& a1,
182 const address_v4& a2) BOOST_ASIO_NOEXCEPT
184 return a1.to_uint() < a2.to_uint();
187 /// Compare addresses for ordering.
188 friend bool operator>(const address_v4& a1,
189 const address_v4& a2) BOOST_ASIO_NOEXCEPT
191 return a1.to_uint() > a2.to_uint();
194 /// Compare addresses for ordering.
195 friend bool operator<=(const address_v4& a1,
196 const address_v4& a2) BOOST_ASIO_NOEXCEPT
198 return a1.to_uint() <= a2.to_uint();
201 /// Compare addresses for ordering.
202 friend bool operator>=(const address_v4& a1,
203 const address_v4& a2) BOOST_ASIO_NOEXCEPT
205 return a1.to_uint() >= a2.to_uint();
208 /// Obtain an address object that represents any address.
209 static address_v4 any() BOOST_ASIO_NOEXCEPT
214 /// Obtain an address object that represents the loopback address.
215 static address_v4 loopback() BOOST_ASIO_NOEXCEPT
217 return address_v4(0x7F000001);
220 /// Obtain an address object that represents the broadcast address.
221 static address_v4 broadcast() BOOST_ASIO_NOEXCEPT
223 return address_v4(0xFFFFFFFF);
226 #if !defined(BOOST_ASIO_NO_DEPRECATED)
227 /// (Deprecated: Use network_v4 class.) Obtain an address object that
228 /// represents the broadcast address that corresponds to the specified
229 /// address and netmask.
230 BOOST_ASIO_DECL static address_v4 broadcast(
231 const address_v4& addr, const address_v4& mask);
233 /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
234 /// to the address, based on its address class.
235 BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
236 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
239 // The underlying IPv4 address.
240 boost::asio::detail::in4_addr_type addr_;
243 /// Create an IPv4 address from raw bytes in network order.
245 * @relates address_v4
247 inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
249 return address_v4(bytes);
252 /// Create an IPv4 address from an unsigned integer in host byte order.
254 * @relates address_v4
256 inline address_v4 make_address_v4(address_v4::uint_type addr)
258 return address_v4(addr);
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(const char* str);
267 /// Create an IPv4 address from an IP address string in dotted decimal form.
269 * @relates address_v4
271 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
272 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
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(const std::string& str);
280 /// Create an IPv4 address from an IP address string in dotted decimal form.
282 * @relates address_v4
284 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
285 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
287 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
288 || defined(GENERATING_DOCUMENTATION)
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(string_view str);
296 /// Create an IPv4 address from an IP address string in dotted decimal form.
298 * @relates address_v4
300 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
301 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
303 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
304 // || defined(GENERATING_DOCUMENTATION)
306 #if !defined(BOOST_ASIO_NO_IOSTREAM)
308 /// Output an address as a string.
310 * Used to output a human-readable string for a specified address.
312 * @param os The output stream to which the string will be written.
314 * @param addr The address to be written.
316 * @return The output stream.
318 * @relates boost::asio::ip::address_v4
320 template <typename Elem, typename Traits>
321 std::basic_ostream<Elem, Traits>& operator<<(
322 std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
324 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
330 #include <boost/asio/detail/pop_options.hpp>
332 #include <boost/asio/ip/impl/address_v4.hpp>
333 #if defined(BOOST_ASIO_HEADER_ONLY)
334 # include <boost/asio/ip/impl/address_v4.ipp>
335 #endif // defined(BOOST_ASIO_HEADER_ONLY)
337 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP