address_v6.hpp 13 KB

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