1//
2// ip/address.hpp
3// ~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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_HPP
12#define BOOST_ASIO_IP_ADDRESS_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 <functional>
20#include <string>
21#include <boost/asio/detail/throw_exception.hpp>
22#include <boost/asio/detail/string_view.hpp>
23#include <boost/asio/detail/type_traits.hpp>
24#include <boost/system/error_code.hpp>
25#include <boost/asio/ip/address_v4.hpp>
26#include <boost/asio/ip/address_v6.hpp>
27#include <boost/asio/ip/bad_address_cast.hpp>
28
29#if !defined(BOOST_ASIO_NO_IOSTREAM)
30# include <iosfwd>
31#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
32
33#include <boost/asio/detail/push_options.hpp>
34
35namespace boost {
36namespace asio {
37namespace ip {
38
39/// Implements version-independent IP addresses.
40/**
41 * The boost::asio::ip::address class provides the ability to use either IP
42 * version 4 or version 6 addresses.
43 *
44 * @par Thread Safety
45 * @e Distinct @e objects: Safe.@n
46 * @e Shared @e objects: Unsafe.
47 */
48class address
49{
50public:
51 /// Default constructor.
52 BOOST_ASIO_DECL address() noexcept;
53
54 /// Construct an address from an IPv4 address.
55 BOOST_ASIO_DECL address(
56 const boost::asio::ip::address_v4& ipv4_address) noexcept;
57
58 /// Construct an address from an IPv6 address.
59 BOOST_ASIO_DECL address(
60 const boost::asio::ip::address_v6& ipv6_address) noexcept;
61
62 /// Copy constructor.
63 BOOST_ASIO_DECL address(const address& other) noexcept;
64
65 /// Move constructor.
66 BOOST_ASIO_DECL address(address&& other) noexcept;
67
68 /// Assign from another address.
69 BOOST_ASIO_DECL address& operator=(const address& other) noexcept;
70
71 /// Move-assign from another address.
72 BOOST_ASIO_DECL address& operator=(address&& other) noexcept;
73
74 /// Assign from an IPv4 address.
75 BOOST_ASIO_DECL address& operator=(
76 const boost::asio::ip::address_v4& ipv4_address) noexcept;
77
78 /// Assign from an IPv6 address.
79 BOOST_ASIO_DECL address& operator=(
80 const boost::asio::ip::address_v6& ipv6_address) noexcept;
81
82 /// Get whether the address is an IP version 4 address.
83 bool is_v4() const noexcept
84 {
85 return type_ == ipv4;
86 }
87
88 /// Get whether the address is an IP version 6 address.
89 bool is_v6() const noexcept
90 {
91 return type_ == ipv6;
92 }
93
94 /// Get the address as an IP version 4 address.
95 BOOST_ASIO_DECL boost::asio::ip::address_v4 to_v4() const;
96
97 /// Get the address as an IP version 6 address.
98 BOOST_ASIO_DECL boost::asio::ip::address_v6 to_v6() const;
99
100 /// Get the address as a string.
101 BOOST_ASIO_DECL std::string to_string() const;
102
103#if !defined(BOOST_ASIO_NO_DEPRECATED)
104 /// (Deprecated: Use other overload.) Get the address as a string.
105 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
106
107 /// (Deprecated: Use make_address().) Create an address from an IPv4 address
108 /// string in dotted decimal form, or from an IPv6 address in hexadecimal
109 /// notation.
110 static address from_string(const char* str);
111
112 /// (Deprecated: Use make_address().) Create an address from an IPv4 address
113 /// string in dotted decimal form, or from an IPv6 address in hexadecimal
114 /// notation.
115 static address from_string(const char* str, boost::system::error_code& ec);
116
117 /// (Deprecated: Use make_address().) Create an address from an IPv4 address
118 /// string in dotted decimal form, or from an IPv6 address in hexadecimal
119 /// notation.
120 static address from_string(const std::string& str);
121
122 /// (Deprecated: Use make_address().) Create an address from an IPv4 address
123 /// string in dotted decimal form, or from an IPv6 address in hexadecimal
124 /// notation.
125 static address from_string(
126 const std::string& str, boost::system::error_code& ec);
127#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
128
129 /// Determine whether the address is a loopback address.
130 BOOST_ASIO_DECL bool is_loopback() const noexcept;
131
132 /// Determine whether the address is unspecified.
133 BOOST_ASIO_DECL bool is_unspecified() const noexcept;
134
135 /// Determine whether the address is a multicast address.
136 BOOST_ASIO_DECL bool is_multicast() const noexcept;
137
138 /// Compare two addresses for equality.
139 BOOST_ASIO_DECL friend bool operator==(const address& a1,
140 const address& a2) noexcept;
141
142 /// Compare two addresses for inequality.
143 friend bool operator!=(const address& a1,
144 const address& a2) noexcept
145 {
146 return !(a1 == a2);
147 }
148
149 /// Compare addresses for ordering.
150 BOOST_ASIO_DECL friend bool operator<(const address& a1,
151 const address& a2) noexcept;
152
153 /// Compare addresses for ordering.
154 friend bool operator>(const address& a1,
155 const address& a2) noexcept
156 {
157 return a2 < a1;
158 }
159
160 /// Compare addresses for ordering.
161 friend bool operator<=(const address& a1,
162 const address& a2) noexcept
163 {
164 return !(a2 < a1);
165 }
166
167 /// Compare addresses for ordering.
168 friend bool operator>=(const address& a1,
169 const address& a2) noexcept
170 {
171 return !(a1 < a2);
172 }
173
174private:
175 // The type of the address.
176 enum { ipv4, ipv6 } type_;
177
178 // The underlying IPv4 address.
179 boost::asio::ip::address_v4 ipv4_address_;
180
181 // The underlying IPv6 address.
182 boost::asio::ip::address_v6 ipv6_address_;
183};
184
185/// Create an address from an IPv4 address string in dotted decimal form,
186/// or from an IPv6 address in hexadecimal notation.
187/**
188 * @relates address
189 */
190BOOST_ASIO_DECL address make_address(const char* str);
191
192/// Create an address from an IPv4 address string in dotted decimal form,
193/// or from an IPv6 address in hexadecimal notation.
194/**
195 * @relates address
196 */
197BOOST_ASIO_DECL address make_address(const char* str,
198 boost::system::error_code& ec) noexcept;
199
200/// Create an address from an IPv4 address string in dotted decimal form,
201/// or from an IPv6 address in hexadecimal notation.
202/**
203 * @relates address
204 */
205BOOST_ASIO_DECL address make_address(const std::string& str);
206
207/// Create an address from an IPv4 address string in dotted decimal form,
208/// or from an IPv6 address in hexadecimal notation.
209/**
210 * @relates address
211 */
212BOOST_ASIO_DECL address make_address(const std::string& str,
213 boost::system::error_code& ec) noexcept;
214
215#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
216 || defined(GENERATING_DOCUMENTATION)
217
218/// Create an address from an IPv4 address string in dotted decimal form,
219/// or from an IPv6 address in hexadecimal notation.
220/**
221 * @relates address
222 */
223BOOST_ASIO_DECL address make_address(string_view str);
224
225/// Create an address from an IPv4 address string in dotted decimal form,
226/// or from an IPv6 address in hexadecimal notation.
227/**
228 * @relates address
229 */
230BOOST_ASIO_DECL address make_address(string_view str,
231 boost::system::error_code& ec) noexcept;
232
233#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
234 // || defined(GENERATING_DOCUMENTATION)
235
236#if !defined(BOOST_ASIO_NO_IOSTREAM)
237
238/// Output an address as a string.
239/**
240 * Used to output a human-readable string for a specified address.
241 *
242 * @param os The output stream to which the string will be written.
243 *
244 * @param addr The address to be written.
245 *
246 * @return The output stream.
247 *
248 * @relates boost::asio::ip::address
249 */
250template <typename Elem, typename Traits>
251std::basic_ostream<Elem, Traits>& operator<<(
252 std::basic_ostream<Elem, Traits>& os, const address& addr);
253
254#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
255
256} // namespace ip
257} // namespace asio
258} // namespace boost
259
260namespace std {
261
262template <>
263struct hash<boost::asio::ip::address>
264{
265 std::size_t operator()(const boost::asio::ip::address& addr)
266 const noexcept
267 {
268 return addr.is_v4()
269 ? std::hash<boost::asio::ip::address_v4>()(addr.to_v4())
270 : std::hash<boost::asio::ip::address_v6>()(addr.to_v6());
271 }
272};
273
274} // namespace std
275
276#include <boost/asio/detail/pop_options.hpp>
277
278#include <boost/asio/ip/impl/address.hpp>
279#if defined(BOOST_ASIO_HEADER_ONLY)
280# include <boost/asio/ip/impl/address.ipp>
281#endif // defined(BOOST_ASIO_HEADER_ONLY)
282
283#endif // BOOST_ASIO_IP_ADDRESS_HPP
284

source code of boost/libs/asio/include/boost/asio/ip/address.hpp