address_v6.hpp 11 KB

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