address_v6.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // ip/address_v6.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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. /// Determine whether the address is a loopback address.
  108. /**
  109. * This function tests whether the address is the loopback address
  110. * <tt>::1</tt>.
  111. */
  112. BOOST_ASIO_DECL bool is_loopback() const noexcept;
  113. /// Determine whether the address is unspecified.
  114. /**
  115. * This function tests whether the address is the loopback address
  116. * <tt>::</tt>.
  117. */
  118. BOOST_ASIO_DECL bool is_unspecified() const noexcept;
  119. /// Determine whether the address is link local.
  120. BOOST_ASIO_DECL bool is_link_local() const noexcept;
  121. /// Determine whether the address is site local.
  122. BOOST_ASIO_DECL bool is_site_local() const noexcept;
  123. /// Determine whether the address is a mapped IPv4 address.
  124. BOOST_ASIO_DECL bool is_v4_mapped() const noexcept;
  125. /// Determine whether the address is a multicast address.
  126. BOOST_ASIO_DECL bool is_multicast() const noexcept;
  127. /// Determine whether the address is a global multicast address.
  128. BOOST_ASIO_DECL bool is_multicast_global() const noexcept;
  129. /// Determine whether the address is a link-local multicast address.
  130. BOOST_ASIO_DECL bool is_multicast_link_local() const noexcept;
  131. /// Determine whether the address is a node-local multicast address.
  132. BOOST_ASIO_DECL bool is_multicast_node_local() const noexcept;
  133. /// Determine whether the address is a org-local multicast address.
  134. BOOST_ASIO_DECL bool is_multicast_org_local() const noexcept;
  135. /// Determine whether the address is a site-local multicast address.
  136. BOOST_ASIO_DECL bool is_multicast_site_local() const noexcept;
  137. /// Compare two addresses for equality.
  138. BOOST_ASIO_DECL friend bool operator==(const address_v6& a1,
  139. const address_v6& a2) noexcept;
  140. /// Compare two addresses for inequality.
  141. friend bool operator!=(const address_v6& a1,
  142. const address_v6& a2) noexcept
  143. {
  144. return !(a1 == a2);
  145. }
  146. /// Compare addresses for ordering.
  147. BOOST_ASIO_DECL friend bool operator<(const address_v6& a1,
  148. const address_v6& a2) noexcept;
  149. /// Compare addresses for ordering.
  150. friend bool operator>(const address_v6& a1,
  151. const address_v6& a2) noexcept
  152. {
  153. return a2 < a1;
  154. }
  155. /// Compare addresses for ordering.
  156. friend bool operator<=(const address_v6& a1,
  157. const address_v6& a2) noexcept
  158. {
  159. return !(a2 < a1);
  160. }
  161. /// Compare addresses for ordering.
  162. friend bool operator>=(const address_v6& a1,
  163. const address_v6& a2) noexcept
  164. {
  165. return !(a1 < a2);
  166. }
  167. /// Obtain an address object that represents any address.
  168. /**
  169. * This functions returns an address that represents the "any" address
  170. * <tt>::</tt>.
  171. *
  172. * @returns A default-constructed @c address_v6 object.
  173. */
  174. static address_v6 any() noexcept
  175. {
  176. return address_v6();
  177. }
  178. /// Obtain an address object that represents the loopback address.
  179. /**
  180. * This function returns an address that represents the well-known loopback
  181. * address <tt>::1</tt>.
  182. */
  183. BOOST_ASIO_DECL static address_v6 loopback() noexcept;
  184. private:
  185. friend class basic_address_iterator<address_v6>;
  186. // The underlying IPv6 address.
  187. boost::asio::detail::in6_addr_type addr_;
  188. // The scope ID associated with the address.
  189. scope_id_type scope_id_;
  190. };
  191. /// Create an IPv6 address from raw bytes and scope ID.
  192. /**
  193. * @relates address_v6
  194. */
  195. inline address_v6 make_address_v6(const address_v6::bytes_type& bytes,
  196. scope_id_type scope_id = 0)
  197. {
  198. return address_v6(bytes, scope_id);
  199. }
  200. /// Create an IPv6 address from an IP address string.
  201. /**
  202. * @relates address_v6
  203. */
  204. BOOST_ASIO_DECL address_v6 make_address_v6(const char* str);
  205. /// Create an IPv6 address from an IP address string.
  206. /**
  207. * @relates address_v6
  208. */
  209. BOOST_ASIO_DECL address_v6 make_address_v6(const char* str,
  210. boost::system::error_code& ec) noexcept;
  211. /// Createan IPv6 address from an IP address string.
  212. /**
  213. * @relates address_v6
  214. */
  215. BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str);
  216. /// Create an IPv6 address from an IP address string.
  217. /**
  218. * @relates address_v6
  219. */
  220. BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str,
  221. boost::system::error_code& ec) noexcept;
  222. #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
  223. || defined(GENERATING_DOCUMENTATION)
  224. /// Create an IPv6 address from an IP address string.
  225. /**
  226. * @relates address_v6
  227. */
  228. BOOST_ASIO_DECL address_v6 make_address_v6(string_view str);
  229. /// Create an IPv6 address from an IP address string.
  230. /**
  231. * @relates address_v6
  232. */
  233. BOOST_ASIO_DECL address_v6 make_address_v6(string_view str,
  234. boost::system::error_code& ec) noexcept;
  235. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  236. // || defined(GENERATING_DOCUMENTATION)
  237. /// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
  238. /// addresses.
  239. enum v4_mapped_t { v4_mapped };
  240. /// Create an IPv4 address from a IPv4-mapped IPv6 address.
  241. /**
  242. * @relates address_v4
  243. */
  244. BOOST_ASIO_DECL address_v4 make_address_v4(
  245. v4_mapped_t, const address_v6& v6_addr);
  246. /// Create an IPv4-mapped IPv6 address from an IPv4 address.
  247. /**
  248. * @relates address_v6
  249. */
  250. BOOST_ASIO_DECL address_v6 make_address_v6(
  251. v4_mapped_t, const address_v4& v4_addr);
  252. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  253. /// Output an address as a string.
  254. /**
  255. * Used to output a human-readable string for a specified address.
  256. *
  257. * @param os The output stream to which the string will be written.
  258. *
  259. * @param addr The address to be written.
  260. *
  261. * @return The output stream.
  262. *
  263. * @relates boost::asio::ip::address_v6
  264. */
  265. template <typename Elem, typename Traits>
  266. std::basic_ostream<Elem, Traits>& operator<<(
  267. std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
  268. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  269. } // namespace ip
  270. } // namespace asio
  271. } // namespace boost
  272. namespace std {
  273. template <>
  274. struct hash<boost::asio::ip::address_v6>
  275. {
  276. std::size_t operator()(const boost::asio::ip::address_v6& addr)
  277. const noexcept
  278. {
  279. const boost::asio::ip::address_v6::bytes_type bytes = addr.to_bytes();
  280. std::size_t result = static_cast<std::size_t>(addr.scope_id());
  281. combine_4_bytes(result, &bytes[0]);
  282. combine_4_bytes(result, &bytes[4]);
  283. combine_4_bytes(result, &bytes[8]);
  284. combine_4_bytes(result, &bytes[12]);
  285. return result;
  286. }
  287. private:
  288. static void combine_4_bytes(std::size_t& seed, const unsigned char* bytes)
  289. {
  290. const std::size_t bytes_hash =
  291. (static_cast<std::size_t>(bytes[0]) << 24) |
  292. (static_cast<std::size_t>(bytes[1]) << 16) |
  293. (static_cast<std::size_t>(bytes[2]) << 8) |
  294. (static_cast<std::size_t>(bytes[3]));
  295. seed ^= bytes_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  296. }
  297. };
  298. } // namespace std
  299. #include <boost/asio/detail/pop_options.hpp>
  300. #include <boost/asio/ip/impl/address_v6.hpp>
  301. #if defined(BOOST_ASIO_HEADER_ONLY)
  302. # include <boost/asio/ip/impl/address_v6.ipp>
  303. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  304. #endif // BOOST_ASIO_IP_ADDRESS_V6_HPP