address_v6.hpp 12 KB

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