]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/ip/address_v4.hpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / boost / asio / ip / address_v4.hpp
1 //
2 // ip/address_v4.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
11 #ifndef BOOST_ASIO_IP_ADDRESS_V4_HPP
12 #define BOOST_ASIO_IP_ADDRESS_V4_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <string>
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>
26
27 #if defined(BOOST_ASIO_HAS_STD_HASH)
28 # include <functional>
29 #endif // defined(BOOST_ASIO_HAS_STD_HASH)
30
31 #if !defined(BOOST_ASIO_NO_IOSTREAM)
32 # include <iosfwd>
33 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
34
35 #include <boost/asio/detail/push_options.hpp>
36
37 namespace boost {
38 namespace asio {
39 namespace ip {
40
41 /// Implements IP version 4 style addresses.
42 /**
43 * The boost::asio::ip::address_v4 class provides the ability to use and
44 * manipulate IP version 4 addresses.
45 *
46 * @par Thread Safety
47 * @e Distinct @e objects: Safe.@n
48 * @e Shared @e objects: Unsafe.
49 */
50 class address_v4
51 {
52 public:
53 /// The type used to represent an address as an unsigned integer.
54 typedef uint_least32_t uint_type;
55
56 /// The type used to represent an address as an array of bytes.
57 /**
58 * @note This type is defined in terms of the C++0x template @c std::array
59 * when it is available. Otherwise, it uses @c boost:array.
60 */
61 #if defined(GENERATING_DOCUMENTATION)
62 typedef array<unsigned char, 4> bytes_type;
63 #else
64 typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
65 #endif
66
67 /// Default constructor.
68 /**
69 * Initialises the @c address_v4 object such that:
70 * @li <tt>to_bytes()</tt> yields <tt>{0, 0, 0, 0}</tt>; and
71 * @li <tt>to_uint() == 0</tt>.
72 */
73 address_v4() BOOST_ASIO_NOEXCEPT
74 {
75 addr_.s_addr = 0;
76 }
77
78 /// Construct an address from raw bytes.
79 /**
80 * Initialises the @c address_v4 object such that <tt>to_bytes() ==
81 * bytes</tt>.
82 *
83 * @throws out_of_range Thrown if any element in @c bytes is not in the range
84 * <tt>0 - 0xFF</tt>. Note that no range checking is required for platforms
85 * where <tt>std::numeric_limits<unsigned char>::max()</tt> is <tt>0xFF</tt>.
86 */
87 BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
88
89 /// Construct an address from an unsigned integer in host byte order.
90 /**
91 * Initialises the @c address_v4 object such that <tt>to_uint() == addr</tt>.
92 */
93 BOOST_ASIO_DECL explicit address_v4(uint_type addr);
94
95 /// Copy constructor.
96 address_v4(const address_v4& other) BOOST_ASIO_NOEXCEPT
97 : addr_(other.addr_)
98 {
99 }
100
101 #if defined(BOOST_ASIO_HAS_MOVE)
102 /// Move constructor.
103 address_v4(address_v4&& other) BOOST_ASIO_NOEXCEPT
104 : addr_(other.addr_)
105 {
106 }
107 #endif // defined(BOOST_ASIO_HAS_MOVE)
108
109 /// Assign from another address.
110 address_v4& operator=(const address_v4& other) BOOST_ASIO_NOEXCEPT
111 {
112 addr_ = other.addr_;
113 return *this;
114 }
115
116 #if defined(BOOST_ASIO_HAS_MOVE)
117 /// Move-assign from another address.
118 address_v4& operator=(address_v4&& other) BOOST_ASIO_NOEXCEPT
119 {
120 addr_ = other.addr_;
121 return *this;
122 }
123 #endif // defined(BOOST_ASIO_HAS_MOVE)
124
125 /// Get the address in bytes, in network byte order.
126 BOOST_ASIO_DECL bytes_type to_bytes() const BOOST_ASIO_NOEXCEPT;
127
128 /// Get the address as an unsigned integer in host byte order
129 BOOST_ASIO_DECL uint_type to_uint() const BOOST_ASIO_NOEXCEPT;
130
131 #if !defined(BOOST_ASIO_NO_DEPRECATED)
132 /// Get the address as an unsigned long in host byte order
133 BOOST_ASIO_DECL unsigned long to_ulong() const;
134 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
135
136 /// Get the address as a string in dotted decimal format.
137 BOOST_ASIO_DECL std::string to_string() const;
138
139 #if !defined(BOOST_ASIO_NO_DEPRECATED)
140 /// (Deprecated: Use other overload.) Get the address as a string in dotted
141 /// decimal format.
142 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
143
144 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
145 /// string in dotted decimal form.
146 static address_v4 from_string(const char* str);
147
148 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
149 /// string in dotted decimal form.
150 static address_v4 from_string(
151 const char* str, boost::system::error_code& ec);
152
153 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
154 /// string in dotted decimal form.
155 static address_v4 from_string(const std::string& str);
156
157 /// (Deprecated: Use make_address_v4().) Create an address from an IP address
158 /// string in dotted decimal form.
159 static address_v4 from_string(
160 const std::string& str, boost::system::error_code& ec);
161 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
162
163 /// Determine whether the address is a loopback address.
164 /**
165 * This function tests whether the address is in the address block
166 * <tt>127.0.0.0/8</tt>, which corresponds to the address range
167 * <tt>127.0.0.0 - 127.255.255.255</tt>.
168 *
169 * @returns <tt>(to_uint() & 0xFF000000) == 0x7F000000</tt>.
170 */
171 BOOST_ASIO_DECL bool is_loopback() const BOOST_ASIO_NOEXCEPT;
172
173 /// Determine whether the address is unspecified.
174 /**
175 * This function tests whether the address is the unspecified address
176 * <tt>0.0.0.0</tt>.
177 *
178 * @returns <tt>to_uint() == 0</tt>.
179 */
180 BOOST_ASIO_DECL bool is_unspecified() const BOOST_ASIO_NOEXCEPT;
181
182 #if !defined(BOOST_ASIO_NO_DEPRECATED)
183 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
184 /// class A address.
185 BOOST_ASIO_DECL bool is_class_a() const;
186
187 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
188 /// class B address.
189 BOOST_ASIO_DECL bool is_class_b() const;
190
191 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
192 /// class C address.
193 BOOST_ASIO_DECL bool is_class_c() const;
194 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
195
196 /// Determine whether the address is a multicast address.
197 /**
198 * This function tests whether the address is in the multicast address block
199 * <tt>224.0.0.0/4</tt>, which corresponds to the address range
200 * <tt>224.0.0.0 - 239.255.255.255</tt>.
201 *
202 * @returns <tt>(to_uint() & 0xF0000000) == 0xE0000000</tt>.
203 */
204 BOOST_ASIO_DECL bool is_multicast() const BOOST_ASIO_NOEXCEPT;
205
206 /// Compare two addresses for equality.
207 friend bool operator==(const address_v4& a1,
208 const address_v4& a2) BOOST_ASIO_NOEXCEPT
209 {
210 return a1.addr_.s_addr == a2.addr_.s_addr;
211 }
212
213 /// Compare two addresses for inequality.
214 friend bool operator!=(const address_v4& a1,
215 const address_v4& a2) BOOST_ASIO_NOEXCEPT
216 {
217 return a1.addr_.s_addr != a2.addr_.s_addr;
218 }
219
220 /// Compare addresses for ordering.
221 /**
222 * Compares two addresses in host byte order.
223 *
224 * @returns <tt>a1.to_uint() < a2.to_uint()</tt>.
225 */
226 friend bool operator<(const address_v4& a1,
227 const address_v4& a2) BOOST_ASIO_NOEXCEPT
228 {
229 return a1.to_uint() < a2.to_uint();
230 }
231
232 /// Compare addresses for ordering.
233 /**
234 * Compares two addresses in host byte order.
235 *
236 * @returns <tt>a1.to_uint() > a2.to_uint()</tt>.
237 */
238 friend bool operator>(const address_v4& a1,
239 const address_v4& a2) BOOST_ASIO_NOEXCEPT
240 {
241 return a1.to_uint() > a2.to_uint();
242 }
243
244 /// Compare addresses for ordering.
245 /**
246 * Compares two addresses in host byte order.
247 *
248 * @returns <tt>a1.to_uint() <= a2.to_uint()</tt>.
249 */
250 friend bool operator<=(const address_v4& a1,
251 const address_v4& a2) BOOST_ASIO_NOEXCEPT
252 {
253 return a1.to_uint() <= a2.to_uint();
254 }
255
256 /// Compare addresses for ordering.
257 /**
258 * Compares two addresses in host byte order.
259 *
260 * @returns <tt>a1.to_uint() >= a2.to_uint()</tt>.
261 */
262 friend bool operator>=(const address_v4& a1,
263 const address_v4& a2) BOOST_ASIO_NOEXCEPT
264 {
265 return a1.to_uint() >= a2.to_uint();
266 }
267
268 /// Obtain an address object that represents any address.
269 /**
270 * This functions returns an address that represents the "any" address
271 * <tt>0.0.0.0</tt>.
272 *
273 * @returns A default-constructed @c address_v4 object.
274 */
275 static address_v4 any() BOOST_ASIO_NOEXCEPT
276 {
277 return address_v4();
278 }
279
280 /// Obtain an address object that represents the loopback address.
281 /**
282 * This function returns an address that represents the well-known loopback
283 * address <tt>127.0.0.1</tt>.
284 *
285 * @returns <tt>address_v4(0x7F000001)</tt>.
286 */
287 static address_v4 loopback() BOOST_ASIO_NOEXCEPT
288 {
289 return address_v4(0x7F000001);
290 }
291
292 /// Obtain an address object that represents the broadcast address.
293 /**
294 * This function returns an address that represents the broadcast address
295 * <tt>255.255.255.255</tt>.
296 *
297 * @returns <tt>address_v4(0xFFFFFFFF)</tt>.
298 */
299 static address_v4 broadcast() BOOST_ASIO_NOEXCEPT
300 {
301 return address_v4(0xFFFFFFFF);
302 }
303
304 #if !defined(BOOST_ASIO_NO_DEPRECATED)
305 /// (Deprecated: Use network_v4 class.) Obtain an address object that
306 /// represents the broadcast address that corresponds to the specified
307 /// address and netmask.
308 BOOST_ASIO_DECL static address_v4 broadcast(
309 const address_v4& addr, const address_v4& mask);
310
311 /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
312 /// to the address, based on its address class.
313 BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
314 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
315
316 private:
317 // The underlying IPv4 address.
318 boost::asio::detail::in4_addr_type addr_;
319 };
320
321 /// Create an IPv4 address from raw bytes in network order.
322 /**
323 * @relates address_v4
324 */
325 inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
326 {
327 return address_v4(bytes);
328 }
329
330 /// Create an IPv4 address from an unsigned integer in host byte order.
331 /**
332 * @relates address_v4
333 */
334 inline address_v4 make_address_v4(address_v4::uint_type addr)
335 {
336 return address_v4(addr);
337 }
338
339 /// Create an IPv4 address from an IP address string in dotted decimal form.
340 /**
341 * @relates address_v4
342 */
343 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
344
345 /// Create an IPv4 address from an IP address string in dotted decimal form.
346 /**
347 * @relates address_v4
348 */
349 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
350 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
351
352 /// Create an IPv4 address from an IP address string in dotted decimal form.
353 /**
354 * @relates address_v4
355 */
356 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
357
358 /// Create an IPv4 address from an IP address string in dotted decimal form.
359 /**
360 * @relates address_v4
361 */
362 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
363 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
364
365 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
366 || defined(GENERATING_DOCUMENTATION)
367
368 /// Create an IPv4 address from an IP address string in dotted decimal form.
369 /**
370 * @relates address_v4
371 */
372 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
373
374 /// Create an IPv4 address from an IP address string in dotted decimal form.
375 /**
376 * @relates address_v4
377 */
378 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
379 boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
380
381 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
382 // || defined(GENERATING_DOCUMENTATION)
383
384 #if !defined(BOOST_ASIO_NO_IOSTREAM)
385
386 /// Output an address as a string.
387 /**
388 * Used to output a human-readable string for a specified address.
389 *
390 * @param os The output stream to which the string will be written.
391 *
392 * @param addr The address to be written.
393 *
394 * @return The output stream.
395 *
396 * @relates boost::asio::ip::address_v4
397 */
398 template <typename Elem, typename Traits>
399 std::basic_ostream<Elem, Traits>& operator<<(
400 std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
401
402 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
403
404 } // namespace ip
405 } // namespace asio
406 } // namespace boost
407
408 #if defined(BOOST_ASIO_HAS_STD_HASH)
409 namespace std {
410
411 template <>
412 struct hash<boost::asio::ip::address_v4>
413 {
414 std::size_t operator()(const boost::asio::ip::address_v4& addr)
415 const BOOST_ASIO_NOEXCEPT
416 {
417 return std::hash<unsigned int>()(addr.to_uint());
418 }
419 };
420
421 } // namespace std
422 #endif // defined(BOOST_ASIO_HAS_STD_HASH)
423
424 #include <boost/asio/detail/pop_options.hpp>
425
426 #include <boost/asio/ip/impl/address_v4.hpp>
427 #if defined(BOOST_ASIO_HEADER_ONLY)
428 # include <boost/asio/ip/impl/address_v4.ipp>
429 #endif // defined(BOOST_ASIO_HEADER_ONLY)
430
431 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP