address_v6.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // ip/address_v6.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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();
  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);
  60. #if defined(BOOST_ASIO_HAS_MOVE)
  61. /// Move constructor.
  62. BOOST_ASIO_DECL address_v6(address_v6&& other);
  63. #endif // defined(BOOST_ASIO_HAS_MOVE)
  64. /// Assign from another address.
  65. BOOST_ASIO_DECL address_v6& operator=(const address_v6& other);
  66. #if defined(BOOST_ASIO_HAS_MOVE)
  67. /// Move-assign from another address.
  68. BOOST_ASIO_DECL address_v6& operator=(address_v6&& other);
  69. #endif // defined(BOOST_ASIO_HAS_MOVE)
  70. /// The scope ID of the address.
  71. /**
  72. * Returns the scope ID associated with the IPv6 address.
  73. */
  74. unsigned long scope_id() const
  75. {
  76. return scope_id_;
  77. }
  78. /// The scope ID of the address.
  79. /**
  80. * Modifies the scope ID associated with the IPv6 address.
  81. */
  82. void scope_id(unsigned long id)
  83. {
  84. scope_id_ = id;
  85. }
  86. /// Get the address in bytes, in network byte order.
  87. BOOST_ASIO_DECL bytes_type to_bytes() const;
  88. /// Get the address as a string.
  89. BOOST_ASIO_DECL std::string to_string() const;
  90. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  91. /// (Deprecated: Use other overload.) Get the address as a string.
  92. BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
  93. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  94. /// address string.
  95. static address_v6 from_string(const char* str);
  96. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  97. /// address string.
  98. static address_v6 from_string(
  99. const char* str, boost::system::error_code& ec);
  100. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  101. /// address string.
  102. static address_v6 from_string(const std::string& str);
  103. /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
  104. /// address string.
  105. static address_v6 from_string(
  106. const std::string& str, boost::system::error_code& ec);
  107. /// (Deprecated: Use make_address_v4().) Converts an IPv4-mapped or
  108. /// IPv4-compatible address to an IPv4 address.
  109. BOOST_ASIO_DECL address_v4 to_v4() const;
  110. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  111. /// Determine whether the address is a loopback address.
  112. BOOST_ASIO_DECL bool is_loopback() const;
  113. /// Determine whether the address is unspecified.
  114. BOOST_ASIO_DECL bool is_unspecified() const;
  115. /// Determine whether the address is link local.
  116. BOOST_ASIO_DECL bool is_link_local() const;
  117. /// Determine whether the address is site local.
  118. BOOST_ASIO_DECL bool is_site_local() const;
  119. /// Determine whether the address is a mapped IPv4 address.
  120. BOOST_ASIO_DECL bool is_v4_mapped() const;
  121. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  122. /// (Deprecated: No replacement.) Determine whether the address is an
  123. /// IPv4-compatible address.
  124. BOOST_ASIO_DECL bool is_v4_compatible() const;
  125. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  126. /// Determine whether the address is a multicast address.
  127. BOOST_ASIO_DECL bool is_multicast() const;
  128. /// Determine whether the address is a global multicast address.
  129. BOOST_ASIO_DECL bool is_multicast_global() const;
  130. /// Determine whether the address is a link-local multicast address.
  131. BOOST_ASIO_DECL bool is_multicast_link_local() const;
  132. /// Determine whether the address is a node-local multicast address.
  133. BOOST_ASIO_DECL bool is_multicast_node_local() const;
  134. /// Determine whether the address is a org-local multicast address.
  135. BOOST_ASIO_DECL bool is_multicast_org_local() const;
  136. /// Determine whether the address is a site-local multicast address.
  137. BOOST_ASIO_DECL bool is_multicast_site_local() const;
  138. /// Compare two addresses for equality.
  139. BOOST_ASIO_DECL friend bool operator==(
  140. const address_v6& a1, const address_v6& a2);
  141. /// Compare two addresses for inequality.
  142. friend bool operator!=(const address_v6& a1, const address_v6& a2)
  143. {
  144. return !(a1 == a2);
  145. }
  146. /// Compare addresses for ordering.
  147. BOOST_ASIO_DECL friend bool operator<(
  148. const address_v6& a1, const address_v6& a2);
  149. /// Compare addresses for ordering.
  150. friend bool operator>(const address_v6& a1, const address_v6& a2)
  151. {
  152. return a2 < a1;
  153. }
  154. /// Compare addresses for ordering.
  155. friend bool operator<=(const address_v6& a1, const address_v6& a2)
  156. {
  157. return !(a2 < a1);
  158. }
  159. /// Compare addresses for ordering.
  160. friend bool operator>=(const address_v6& a1, const address_v6& a2)
  161. {
  162. return !(a1 < a2);
  163. }
  164. /// Obtain an address object that represents any address.
  165. static address_v6 any()
  166. {
  167. return address_v6();
  168. }
  169. /// Obtain an address object that represents the loopback address.
  170. BOOST_ASIO_DECL static address_v6 loopback();
  171. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  172. /// (Deprecated: Use make_address_v6().) Create an IPv4-mapped IPv6 address.
  173. BOOST_ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
  174. /// (Deprecated: No replacement.) Create an IPv4-compatible IPv6 address.
  175. BOOST_ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
  176. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  177. private:
  178. friend class basic_address_iterator<address_v6>;
  179. // The underlying IPv6 address.
  180. boost::asio::detail::in6_addr_type addr_;
  181. // The scope ID associated with the address.
  182. unsigned long scope_id_;
  183. };
  184. /// Create an IPv6 address from raw bytes and scope ID.
  185. /**
  186. * @relates address_v6
  187. */
  188. inline address_v6 make_address_v6(const address_v6::bytes_type& bytes,
  189. unsigned long scope_id = 0)
  190. {
  191. return address_v6(bytes, scope_id);
  192. }
  193. /// Create an IPv6 address from an IP address string.
  194. /**
  195. * @relates address_v6
  196. */
  197. BOOST_ASIO_DECL address_v6 make_address_v6(const char* str);
  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(
  203. const char* str, boost::system::error_code& ec);
  204. /// Createan IPv6 address from an IP address string.
  205. /**
  206. * @relates address_v6
  207. */
  208. BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str);
  209. /// Create an IPv6 address from an IP address string.
  210. /**
  211. * @relates address_v6
  212. */
  213. BOOST_ASIO_DECL address_v6 make_address_v6(
  214. const std::string& str, boost::system::error_code& ec);
  215. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  216. || defined(GENERATING_DOCUMENTATION)
  217. /// Create an IPv6 address from an IP address string.
  218. /**
  219. * @relates address_v6
  220. */
  221. BOOST_ASIO_DECL address_v6 make_address_v6(string_view str);
  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(
  227. string_view str, boost::system::error_code& ec);
  228. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  229. // || defined(GENERATING_DOCUMENTATION)
  230. /// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
  231. /// addresses.
  232. enum v4_mapped_t { v4_mapped };
  233. /// Create an IPv4 address from a IPv4-mapped IPv6 address.
  234. /**
  235. * @relates address_v4
  236. */
  237. BOOST_ASIO_DECL address_v4 make_address_v4(
  238. v4_mapped_t, const address_v6& v6_addr);
  239. /// Create an IPv4-mapped IPv6 address from an IPv4 address.
  240. /**
  241. * @relates address_v6
  242. */
  243. BOOST_ASIO_DECL address_v6 make_address_v6(
  244. v4_mapped_t, const address_v4& v4_addr);
  245. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  246. /// Output an address as a string.
  247. /**
  248. * Used to output a human-readable string for a specified address.
  249. *
  250. * @param os The output stream to which the string will be written.
  251. *
  252. * @param addr The address to be written.
  253. *
  254. * @return The output stream.
  255. *
  256. * @relates boost::asio::ip::address_v6
  257. */
  258. template <typename Elem, typename Traits>
  259. std::basic_ostream<Elem, Traits>& operator<<(
  260. std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
  261. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  262. } // namespace ip
  263. } // namespace asio
  264. } // namespace boost
  265. #include <boost/asio/detail/pop_options.hpp>
  266. #include <boost/asio/ip/impl/address_v6.hpp>
  267. #if defined(BOOST_ASIO_HEADER_ONLY)
  268. # include <boost/asio/ip/impl/address_v6.ipp>
  269. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  270. #endif // BOOST_ASIO_IP_ADDRESS_V6_HPP