address_v6.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // ip/address_v6.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #ifndef BOOST_ASIO_IP_ADDRESS_V6_HPP
  11. #define BOOST_ASIO_IP_ADDRESS_V6_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <string>
  17. #include <boost/asio/detail/array.hpp>
  18. #include <boost/asio/detail/socket_types.hpp>
  19. #include <boost/asio/detail/string_view.hpp>
  20. #include <boost/asio/detail/winsock_init.hpp>
  21. #include <boost/system/error_code.hpp>
  22. #include <boost/asio/ip/address_v4.hpp>
  23. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  24. # include <iosfwd>
  25. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace ip {
  30. template <typename> class basic_address_iterator;
  31. /// Implements IP version 6 style addresses.
  32. /**
  33. * The boost::asio::ip::address_v6 class provides the ability to use and
  34. * manipulate IP version 6 addresses.
  35. *
  36. * @par Thread Safety
  37. * @e Distinct @e objects: Safe.@n
  38. * @e Shared @e objects: Unsafe.
  39. */
  40. class address_v6
  41. {
  42. public:
  43. /// The type used to represent an address as an array of bytes.
  44. /**
  45. * @note This type is defined in terms of the C++0x template @c std::array
  46. * when it is available. Otherwise, it uses @c boost:array.
  47. */
  48. #if defined(GENERATING_DOCUMENTATION)
  49. typedef array<unsigned char, 16> bytes_type;
  50. #else
  51. typedef boost::asio::detail::array<unsigned char, 16> bytes_type;
  52. #endif
  53. /// Default constructor.
  54. BOOST_ASIO_DECL address_v6() BOOST_ASIO_NOEXCEPT;
  55. /// Construct an address from raw bytes and scope ID.
  56. BOOST_ASIO_DECL explicit address_v6(const bytes_type& bytes,
  57. unsigned long scope_id = 0);
  58. /// Copy constructor.
  59. BOOST_ASIO_DECL address_v6(const address_v6& other) BOOST_ASIO_NOEXCEPT;
  60. #if defined(BOOST_ASIO_HAS_MOVE)
  61. /// Move constructor.
  62. BOOST_ASIO_DECL address_v6(address_v6&& other) BOOST_ASIO_NOEXCEPT;
  63. #endif // defined(BOOST_ASIO_HAS_MOVE)
  64. /// Assign from another address.
  65. BOOST_ASIO_DECL address_v6& operator=(
  66. const address_v6& other) BOOST_ASIO_NOEXCEPT;
  67. #if defined(BOOST_ASIO_HAS_MOVE)
  68. /// Move-assign from another address.
  69. BOOST_ASIO_DECL address_v6& operator=(address_v6&& other) BOOST_ASIO_NOEXCEPT;
  70. #endif // defined(BOOST_ASIO_HAS_MOVE)
  71. /// The scope ID of the address.
  72. /**
  73. * Returns the scope ID associated with the IPv6 address.
  74. */
  75. unsigned long scope_id() const BOOST_ASIO_NOEXCEPT
  76. {
  77. return scope_id_;
  78. }
  79. /// The scope ID of the address.
  80. /**
  81. * Modifies the scope ID associated with the IPv6 address.
  82. */
  83. void scope_id(unsigned long id) BOOST_ASIO_NOEXCEPT
  84. {
  85. scope_id_ = id;
  86. }
  87. /// Get the address in bytes, in network byte order.
  88. BOOST_ASIO_DECL bytes_type to_bytes() const BOOST_ASIO_NOEXCEPT;
  89. /// Get the address as a string.
  90. BOOST_ASIO_DECL std::string to_string() const;
  91. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  92. /// (Deprecated: Use other overload.) Get the address as a string.
  93. BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
  94. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  95. /// address string.
  96. static address_v6 from_string(const char* str);
  97. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  98. /// address string.
  99. static address_v6 from_string(
  100. const char* str, boost::system::error_code& ec);
  101. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  102. /// address string.
  103. static address_v6 from_string(const std::string& str);
  104. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  105. /// address string.
  106. static address_v6 from_string(
  107. const std::string& str, boost::system::error_code& ec);
  108. /// (Deprecated: Use make_address_v4().) Converts an IPv4-mapped or
  109. /// IPv4-compatible address to an IPv4 address.
  110. BOOST_ASIO_DECL address_v4 to_v4() const;
  111. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  112. /// Determine whether the address is a loopback address.
  113. BOOST_ASIO_DECL bool is_loopback() const BOOST_ASIO_NOEXCEPT;
  114. /// Determine whether the address is unspecified.
  115. BOOST_ASIO_DECL bool is_unspecified() const BOOST_ASIO_NOEXCEPT;
  116. /// Determine whether the address is link local.
  117. BOOST_ASIO_DECL bool is_link_local() const BOOST_ASIO_NOEXCEPT;
  118. /// Determine whether the address is site local.
  119. BOOST_ASIO_DECL bool is_site_local() const BOOST_ASIO_NOEXCEPT;
  120. /// Determine whether the address is a mapped IPv4 address.
  121. BOOST_ASIO_DECL bool is_v4_mapped() const BOOST_ASIO_NOEXCEPT;
  122. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  123. /// (Deprecated: No replacement.) Determine whether the address is an
  124. /// IPv4-compatible address.
  125. BOOST_ASIO_DECL bool is_v4_compatible() const;
  126. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  127. /// Determine whether the address is a multicast address.
  128. BOOST_ASIO_DECL bool is_multicast() const BOOST_ASIO_NOEXCEPT;
  129. /// Determine whether the address is a global multicast address.
  130. BOOST_ASIO_DECL bool is_multicast_global() const BOOST_ASIO_NOEXCEPT;
  131. /// Determine whether the address is a link-local multicast address.
  132. BOOST_ASIO_DECL bool is_multicast_link_local() const BOOST_ASIO_NOEXCEPT;
  133. /// Determine whether the address is a node-local multicast address.
  134. BOOST_ASIO_DECL bool is_multicast_node_local() const BOOST_ASIO_NOEXCEPT;
  135. /// Determine whether the address is a org-local multicast address.
  136. BOOST_ASIO_DECL bool is_multicast_org_local() const BOOST_ASIO_NOEXCEPT;
  137. /// Determine whether the address is a site-local multicast address.
  138. BOOST_ASIO_DECL bool is_multicast_site_local() const BOOST_ASIO_NOEXCEPT;
  139. /// Compare two addresses for equality.
  140. BOOST_ASIO_DECL friend bool operator==(const address_v6& a1,
  141. const address_v6& a2) BOOST_ASIO_NOEXCEPT;
  142. /// Compare two addresses for inequality.
  143. friend bool operator!=(const address_v6& a1,
  144. const address_v6& a2) BOOST_ASIO_NOEXCEPT
  145. {
  146. return !(a1 == a2);
  147. }
  148. /// Compare addresses for ordering.
  149. BOOST_ASIO_DECL friend bool operator<(const address_v6& a1,
  150. const address_v6& a2) BOOST_ASIO_NOEXCEPT;
  151. /// Compare addresses for ordering.
  152. friend bool operator>(const address_v6& a1,
  153. const address_v6& a2) BOOST_ASIO_NOEXCEPT
  154. {
  155. return a2 < a1;
  156. }
  157. /// Compare addresses for ordering.
  158. friend bool operator<=(const address_v6& a1,
  159. const address_v6& a2) BOOST_ASIO_NOEXCEPT
  160. {
  161. return !(a2 < a1);
  162. }
  163. /// Compare addresses for ordering.
  164. friend bool operator>=(const address_v6& a1,
  165. const address_v6& a2) BOOST_ASIO_NOEXCEPT
  166. {
  167. return !(a1 < a2);
  168. }
  169. /// Obtain an address object that represents any address.
  170. static address_v6 any() BOOST_ASIO_NOEXCEPT
  171. {
  172. return address_v6();
  173. }
  174. /// Obtain an address object that represents the loopback address.
  175. BOOST_ASIO_DECL static address_v6 loopback() BOOST_ASIO_NOEXCEPT;
  176. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  177. /// (Deprecated: Use make_address_v6().) Create an IPv4-mapped IPv6 address.
  178. BOOST_ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
  179. /// (Deprecated: No replacement.) Create an IPv4-compatible IPv6 address.
  180. BOOST_ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
  181. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  182. private:
  183. friend class basic_address_iterator<address_v6>;
  184. // The underlying IPv6 address.
  185. boost::asio::detail::in6_addr_type addr_;
  186. // The scope ID associated with the address.
  187. unsigned long scope_id_;
  188. };
  189. /// Create an IPv6 address from raw bytes and scope ID.
  190. /**
  191. * @relates address_v6
  192. */
  193. inline address_v6 make_address_v6(const address_v6::bytes_type& bytes,
  194. unsigned long scope_id = 0)
  195. {
  196. return address_v6(bytes, scope_id);
  197. }
  198. /// Create an IPv6 address from an IP address string.
  199. /**
  200. * @relates address_v6
  201. */
  202. BOOST_ASIO_DECL address_v6 make_address_v6(const char* str);
  203. /// Create an IPv6 address from an IP address string.
  204. /**
  205. * @relates address_v6
  206. */
  207. BOOST_ASIO_DECL address_v6 make_address_v6(const char* str,
  208. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  209. /// Createan IPv6 address from an IP address string.
  210. /**
  211. * @relates address_v6
  212. */
  213. BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str);
  214. /// Create an IPv6 address from an IP address string.
  215. /**
  216. * @relates address_v6
  217. */
  218. BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str,
  219. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  220. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  221. || defined(GENERATING_DOCUMENTATION)
  222. /// Create an IPv6 address from an IP address string.
  223. /**
  224. * @relates address_v6
  225. */
  226. BOOST_ASIO_DECL address_v6 make_address_v6(string_view str);
  227. /// Create an IPv6 address from an IP address string.
  228. /**
  229. * @relates address_v6
  230. */
  231. BOOST_ASIO_DECL address_v6 make_address_v6(string_view str,
  232. boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
  233. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  234. // || defined(GENERATING_DOCUMENTATION)
  235. /// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
  236. /// addresses.
  237. enum v4_mapped_t { v4_mapped };
  238. /// Create an IPv4 address from a IPv4-mapped IPv6 address.
  239. /**
  240. * @relates address_v4
  241. */
  242. BOOST_ASIO_DECL address_v4 make_address_v4(
  243. v4_mapped_t, const address_v6& v6_addr);
  244. /// Create an IPv4-mapped IPv6 address from an IPv4 address.
  245. /**
  246. * @relates address_v6
  247. */
  248. BOOST_ASIO_DECL address_v6 make_address_v6(
  249. v4_mapped_t, const address_v4& v4_addr);
  250. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  251. /// Output an address as a string.
  252. /**
  253. * Used to output a human-readable string for a specified address.
  254. *
  255. * @param os The output stream to which the string will be written.
  256. *
  257. * @param addr The address to be written.
  258. *
  259. * @return The output stream.
  260. *
  261. * @relates boost::asio::ip::address_v6
  262. */
  263. template <typename Elem, typename Traits>
  264. std::basic_ostream<Elem, Traits>& operator<<(
  265. std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
  266. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  267. } // namespace ip
  268. } // namespace asio
  269. } // namespace boost
  270. #include <boost/asio/detail/pop_options.hpp>
  271. #include <boost/asio/ip/impl/address_v6.hpp>
  272. #if defined(BOOST_ASIO_HEADER_ONLY)
  273. # include <boost/asio/ip/impl/address_v6.ipp>
  274. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  275. #endif // BOOST_ASIO_IP_ADDRESS_V6_HPP