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_V6_HPP
12 #define BOOST_ASIO_IP_ADDRESS_V6_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/socket_types.hpp>
22 #include <boost/asio/detail/string_view.hpp>
23 #include <boost/asio/detail/winsock_init.hpp>
24 #include <boost/system/error_code.hpp>
25 #include <boost/asio/ip/address_v4.hpp>
27 #if !defined(BOOST_ASIO_NO_IOSTREAM)
29 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
31 #include <boost/asio/detail/push_options.hpp>
37 template <typename> class basic_address_iterator;
39 /// Implements IP version 6 style addresses.
41 * The boost::asio::ip::address_v6 class provides the ability to use and
42 * manipulate IP version 6 addresses.
45 * @e Distinct @e objects: Safe.@n
46 * @e Shared @e objects: Unsafe.
51 /// The type used to represent an address as an array of bytes.
53 * @note This type is defined in terms of the C++0x template @c std::array
54 * when it is available. Otherwise, it uses @c boost:array.
56 #if defined(GENERATING_DOCUMENTATION)
57 typedef array<unsigned char, 16> bytes_type;
59 typedef boost::asio::detail::array<unsigned char, 16> bytes_type;
62 /// Default constructor.
63 BOOST_ASIO_DECL address_v6();
65 /// Construct an address from raw bytes and scope ID.
66 BOOST_ASIO_DECL explicit address_v6(const bytes_type& bytes,
67 unsigned long scope_id = 0);
70 BOOST_ASIO_DECL address_v6(const address_v6& other);
72 #if defined(BOOST_ASIO_HAS_MOVE)
74 BOOST_ASIO_DECL address_v6(address_v6&& other);
75 #endif // defined(BOOST_ASIO_HAS_MOVE)
77 /// Assign from another address.
78 BOOST_ASIO_DECL address_v6& operator=(const address_v6& other);
80 #if defined(BOOST_ASIO_HAS_MOVE)
81 /// Move-assign from another address.
82 BOOST_ASIO_DECL address_v6& operator=(address_v6&& other);
83 #endif // defined(BOOST_ASIO_HAS_MOVE)
85 /// The scope ID of the address.
87 * Returns the scope ID associated with the IPv6 address.
89 unsigned long scope_id() const
94 /// The scope ID of the address.
96 * Modifies the scope ID associated with the IPv6 address.
98 void scope_id(unsigned long id)
103 /// Get the address in bytes, in network byte order.
104 BOOST_ASIO_DECL bytes_type to_bytes() const;
106 /// Get the address as a string.
107 BOOST_ASIO_DECL std::string to_string() const;
109 #if !defined(BOOST_ASIO_NO_DEPRECATED)
110 /// (Deprecated: Use other overload.) Get the address as a string.
111 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
113 /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
115 static address_v6 from_string(const char* str);
117 /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
119 static address_v6 from_string(
120 const char* str, boost::system::error_code& ec);
122 /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
124 static address_v6 from_string(const std::string& str);
126 /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
128 static address_v6 from_string(
129 const std::string& str, boost::system::error_code& ec);
131 /// (Deprecated: Use make_address_v4().) Converts an IPv4-mapped or
132 /// IPv4-compatible address to an IPv4 address.
133 BOOST_ASIO_DECL address_v4 to_v4() const;
134 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
136 /// Determine whether the address is a loopback address.
137 BOOST_ASIO_DECL bool is_loopback() const;
139 /// Determine whether the address is unspecified.
140 BOOST_ASIO_DECL bool is_unspecified() const;
142 /// Determine whether the address is link local.
143 BOOST_ASIO_DECL bool is_link_local() const;
145 /// Determine whether the address is site local.
146 BOOST_ASIO_DECL bool is_site_local() const;
148 /// Determine whether the address is a mapped IPv4 address.
149 BOOST_ASIO_DECL bool is_v4_mapped() const;
151 #if !defined(BOOST_ASIO_NO_DEPRECATED)
152 /// (Deprecated: No replacement.) Determine whether the address is an
153 /// IPv4-compatible address.
154 BOOST_ASIO_DECL bool is_v4_compatible() const;
155 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
157 /// Determine whether the address is a multicast address.
158 BOOST_ASIO_DECL bool is_multicast() const;
160 /// Determine whether the address is a global multicast address.
161 BOOST_ASIO_DECL bool is_multicast_global() const;
163 /// Determine whether the address is a link-local multicast address.
164 BOOST_ASIO_DECL bool is_multicast_link_local() const;
166 /// Determine whether the address is a node-local multicast address.
167 BOOST_ASIO_DECL bool is_multicast_node_local() const;
169 /// Determine whether the address is a org-local multicast address.
170 BOOST_ASIO_DECL bool is_multicast_org_local() const;
172 /// Determine whether the address is a site-local multicast address.
173 BOOST_ASIO_DECL bool is_multicast_site_local() const;
175 /// Compare two addresses for equality.
176 BOOST_ASIO_DECL friend bool operator==(
177 const address_v6& a1, const address_v6& a2);
179 /// Compare two addresses for inequality.
180 friend bool operator!=(const address_v6& a1, const address_v6& a2)
185 /// Compare addresses for ordering.
186 BOOST_ASIO_DECL friend bool operator<(
187 const address_v6& a1, const address_v6& a2);
189 /// Compare addresses for ordering.
190 friend bool operator>(const address_v6& a1, const address_v6& a2)
195 /// Compare addresses for ordering.
196 friend bool operator<=(const address_v6& a1, const address_v6& a2)
201 /// Compare addresses for ordering.
202 friend bool operator>=(const address_v6& a1, const address_v6& a2)
207 /// Obtain an address object that represents any address.
208 static address_v6 any()
213 /// Obtain an address object that represents the loopback address.
214 BOOST_ASIO_DECL static address_v6 loopback();
216 #if !defined(BOOST_ASIO_NO_DEPRECATED)
217 /// (Deprecated: Use make_address_v6().) Create an IPv4-mapped IPv6 address.
218 BOOST_ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
220 /// (Deprecated: No replacement.) Create an IPv4-compatible IPv6 address.
221 BOOST_ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
222 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
225 friend class basic_address_iterator<address_v6>;
227 // The underlying IPv6 address.
228 boost::asio::detail::in6_addr_type addr_;
230 // The scope ID associated with the address.
231 unsigned long scope_id_;
234 /// Create an IPv6 address from raw bytes and scope ID.
236 * @relates address_v6
238 inline address_v6 make_address_v6(const address_v6::bytes_type& bytes,
239 unsigned long scope_id = 0)
241 return address_v6(bytes, scope_id);
244 /// Create an IPv6 address from an IP address string.
246 * @relates address_v6
248 BOOST_ASIO_DECL address_v6 make_address_v6(const char* str);
250 /// Create an IPv6 address from an IP address string.
252 * @relates address_v6
254 BOOST_ASIO_DECL address_v6 make_address_v6(
255 const char* str, boost::system::error_code& ec);
257 /// Createan IPv6 address from an IP address string.
259 * @relates address_v6
261 BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str);
263 /// Create an IPv6 address from an IP address string.
265 * @relates address_v6
267 BOOST_ASIO_DECL address_v6 make_address_v6(
268 const std::string& str, boost::system::error_code& ec);
270 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
271 || defined(GENERATING_DOCUMENTATION)
273 /// Create an IPv6 address from an IP address string.
275 * @relates address_v6
277 BOOST_ASIO_DECL address_v6 make_address_v6(string_view str);
279 /// Create an IPv6 address from an IP address string.
281 * @relates address_v6
283 BOOST_ASIO_DECL address_v6 make_address_v6(
284 string_view str, boost::system::error_code& ec);
286 #endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
287 // || defined(GENERATING_DOCUMENTATION)
289 /// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
291 enum v4_mapped_t { v4_mapped };
293 /// Create an IPv4 address from a IPv4-mapped IPv6 address.
295 * @relates address_v4
297 BOOST_ASIO_DECL address_v4 make_address_v4(
298 v4_mapped_t, const address_v6& v6_addr);
300 /// Create an IPv4-mapped IPv6 address from an IPv4 address.
302 * @relates address_v6
304 BOOST_ASIO_DECL address_v6 make_address_v6(
305 v4_mapped_t, const address_v4& v4_addr);
307 #if !defined(BOOST_ASIO_NO_IOSTREAM)
309 /// Output an address as a string.
311 * Used to output a human-readable string for a specified address.
313 * @param os The output stream to which the string will be written.
315 * @param addr The address to be written.
317 * @return The output stream.
319 * @relates boost::asio::ip::address_v6
321 template <typename Elem, typename Traits>
322 std::basic_ostream<Elem, Traits>& operator<<(
323 std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
325 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
331 #include <boost/asio/detail/pop_options.hpp>
333 #include <boost/asio/ip/impl/address_v6.hpp>
334 #if defined(BOOST_ASIO_HEADER_ONLY)
335 # include <boost/asio/ip/impl/address_v6.ipp>
336 #endif // defined(BOOST_ASIO_HEADER_ONLY)
338 #endif // BOOST_ASIO_IP_ADDRESS_V6_HPP