address_v6.hpp 12 KB

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