]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/ip/address_v4.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / ip / address_v4.hpp
1 //
2 // ip/address_v4.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_NO_IOSTREAM)
28 # include <iosfwd>
29 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
30
31 #include <boost/asio/detail/push_options.hpp>
32
33 namespace boost {
34 namespace asio {
35 namespace ip {
36
37 /// Implements IP version 4 style addresses.
38 /**
39 * The boost::asio::ip::address_v4 class provides the ability to use and
40 * manipulate IP version 4 addresses.
41 *
42 * @par Thread Safety
43 * @e Distinct @e objects: Safe.@n
44 * @e Shared @e objects: Unsafe.
45 */
46 class address_v4
47 {
48 public:
49 /// The type used to represent an address as an unsigned integer.
50 typedef uint_least32_t uint_type;
51
52 /// The type used to represent an address as an array of bytes.
53 /**
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.
56 */
57 #if defined(GENERATING_DOCUMENTATION)
58 typedef array<unsigned char, 4> bytes_type;
59 #else
60 typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
61 #endif
62
63 /// Default constructor.
64 address_v4()
65 {
66 addr_.s_addr = 0;
67 }
68
69 /// Construct an address from raw bytes.
70 BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
71
72 /// Construct an address from an unsigned integer in host byte order.
73 BOOST_ASIO_DECL explicit address_v4(uint_type addr);
74
75 /// Copy constructor.
76 address_v4(const address_v4& other)
77 : addr_(other.addr_)
78 {
79 }
80
81 #if defined(BOOST_ASIO_HAS_MOVE)
82 /// Move constructor.
83 address_v4(address_v4&& other)
84 : addr_(other.addr_)
85 {
86 }
87 #endif // defined(BOOST_ASIO_HAS_MOVE)
88
89 /// Assign from another address.
90 address_v4& operator=(const address_v4& other)
91 {
92 addr_ = other.addr_;
93 return *this;
94 }
95
96 #if defined(BOOST_ASIO_HAS_MOVE)
97 /// Move-assign from another address.
98 address_v4& operator=(address_v4&& other)
99 {
100 addr_ = other.addr_;
101 return *this;
102 }
103 #endif // defined(BOOST_ASIO_HAS_MOVE)
104
105 /// Get the address in bytes, in network byte order.
106 BOOST_ASIO_DECL bytes_type to_bytes() const;
107
108 /// Get the address as an unsigned integer in host byte order
109 BOOST_ASIO_DECL uint_type to_uint() const;
110
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)
115
116 /// Get the address as a string in dotted decimal format.
117 BOOST_ASIO_DECL std::string to_string() const;
118
119 #if !defined(BOOST_ASIO_NO_DEPRECATED)
120 /// (Deprecated: Use other overload.) Get the address as a string in dotted
121 /// decimal format.
122 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
123
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);
127
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);
132
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);
136
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)
142
143 /// Determine whether the address is a loopback address.
144 BOOST_ASIO_DECL bool is_loopback() const;
145
146 /// Determine whether the address is unspecified.
147 BOOST_ASIO_DECL bool is_unspecified() const;
148
149 #if !defined(BOOST_ASIO_NO_DEPRECATED)
150 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
151 /// class A address.
152 BOOST_ASIO_DECL bool is_class_a() const;
153
154 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
155 /// class B address.
156 BOOST_ASIO_DECL bool is_class_b() const;
157
158 /// (Deprecated: Use network_v4 class.) Determine whether the address is a
159 /// class C address.
160 BOOST_ASIO_DECL bool is_class_c() const;
161 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
162
163 /// Determine whether the address is a multicast address.
164 BOOST_ASIO_DECL bool is_multicast() const;
165
166 /// Compare two addresses for equality.
167 friend bool operator==(const address_v4& a1, const address_v4& a2)
168 {
169 return a1.addr_.s_addr == a2.addr_.s_addr;
170 }
171
172 /// Compare two addresses for inequality.
173 friend bool operator!=(const address_v4& a1, const address_v4& a2)
174 {
175 return a1.addr_.s_addr != a2.addr_.s_addr;
176 }
177
178 /// Compare addresses for ordering.
179 friend bool operator<(const address_v4& a1, const address_v4& a2)
180 {
181 return a1.to_uint() < a2.to_uint();
182 }
183
184 /// Compare addresses for ordering.
185 friend bool operator>(const address_v4& a1, const address_v4& a2)
186 {
187 return a1.to_uint() > a2.to_uint();
188 }
189
190 /// Compare addresses for ordering.
191 friend bool operator<=(const address_v4& a1, const address_v4& a2)
192 {
193 return a1.to_uint() <= a2.to_uint();
194 }
195
196 /// Compare addresses for ordering.
197 friend bool operator>=(const address_v4& a1, const address_v4& a2)
198 {
199 return a1.to_uint() >= a2.to_uint();
200 }
201
202 /// Obtain an address object that represents any address.
203 static address_v4 any()
204 {
205 return address_v4();
206 }
207
208 /// Obtain an address object that represents the loopback address.
209 static address_v4 loopback()
210 {
211 return address_v4(0x7F000001);
212 }
213
214 /// Obtain an address object that represents the broadcast address.
215 static address_v4 broadcast()
216 {
217 return address_v4(0xFFFFFFFF);
218 }
219
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);
226
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)
231
232 private:
233 // The underlying IPv4 address.
234 boost::asio::detail::in4_addr_type addr_;
235 };
236
237 /// Create an IPv4 address from raw bytes in network order.
238 /**
239 * @relates address_v4
240 */
241 inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
242 {
243 return address_v4(bytes);
244 }
245
246 /// Create an IPv4 address from an unsigned integer in host byte order.
247 /**
248 * @relates address_v4
249 */
250 inline address_v4 make_address_v4(address_v4::uint_type addr)
251 {
252 return address_v4(addr);
253 }
254
255 /// Create an IPv4 address from an IP address string in dotted decimal form.
256 /**
257 * @relates address_v4
258 */
259 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
260
261 /// Create an IPv4 address from an IP address string in dotted decimal form.
262 /**
263 * @relates address_v4
264 */
265 BOOST_ASIO_DECL address_v4 make_address_v4(
266 const char* str, boost::system::error_code& ec);
267
268 /// Create an IPv4 address from an IP address string in dotted decimal form.
269 /**
270 * @relates address_v4
271 */
272 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
273
274 /// Create an IPv4 address from an IP address string in dotted decimal form.
275 /**
276 * @relates address_v4
277 */
278 BOOST_ASIO_DECL address_v4 make_address_v4(
279 const std::string& str, boost::system::error_code& ec);
280
281 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
282 || defined(GENERATING_DOCUMENTATION)
283
284 /// Create an IPv4 address from an IP address string in dotted decimal form.
285 /**
286 * @relates address_v4
287 */
288 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
289
290 /// Create an IPv4 address from an IP address string in dotted decimal form.
291 /**
292 * @relates address_v4
293 */
294 BOOST_ASIO_DECL address_v4 make_address_v4(
295 string_view str, boost::system::error_code& ec);
296
297 #endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
298 // || defined(GENERATING_DOCUMENTATION)
299
300 #if !defined(BOOST_ASIO_NO_IOSTREAM)
301
302 /// Output an address as a string.
303 /**
304 * Used to output a human-readable string for a specified address.
305 *
306 * @param os The output stream to which the string will be written.
307 *
308 * @param addr The address to be written.
309 *
310 * @return The output stream.
311 *
312 * @relates boost::asio::ip::address_v4
313 */
314 template <typename Elem, typename Traits>
315 std::basic_ostream<Elem, Traits>& operator<<(
316 std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
317
318 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
319
320 } // namespace ip
321 } // namespace asio
322 } // namespace boost
323
324 #include <boost/asio/detail/pop_options.hpp>
325
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)
330
331 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP