address_v6.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // ip/address_v6.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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/socket_types.hpp"
  19. #include "asio/detail/winsock_init.hpp"
  20. #include "asio/error_code.hpp"
  21. #include "asio/ip/address_v4.hpp"
  22. #if !defined(ASIO_NO_IOSTREAM)
  23. # include <iosfwd>
  24. #endif // !defined(ASIO_NO_IOSTREAM)
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace ip {
  28. /// Implements IP version 6 style addresses.
  29. /**
  30. * The asio::ip::address_v6 class provides the ability to use and
  31. * manipulate IP version 6 addresses.
  32. *
  33. * @par Thread Safety
  34. * @e Distinct @e objects: Safe.@n
  35. * @e Shared @e objects: Unsafe.
  36. */
  37. class address_v6
  38. {
  39. public:
  40. /// The type used to represent an address as an array of bytes.
  41. /**
  42. * @note This type is defined in terms of the C++0x template @c std::array
  43. * when it is available. Otherwise, it uses @c boost:array.
  44. */
  45. #if defined(GENERATING_DOCUMENTATION)
  46. typedef array<unsigned char, 16> bytes_type;
  47. #else
  48. typedef asio::detail::array<unsigned char, 16> bytes_type;
  49. #endif
  50. /// Default constructor.
  51. ASIO_DECL address_v6();
  52. /// Construct an address from raw bytes and scope ID.
  53. ASIO_DECL explicit address_v6(const bytes_type& bytes,
  54. unsigned long scope_id = 0);
  55. /// Copy constructor.
  56. ASIO_DECL address_v6(const address_v6& other);
  57. #if defined(ASIO_HAS_MOVE)
  58. /// Move constructor.
  59. ASIO_DECL address_v6(address_v6&& other);
  60. #endif // defined(ASIO_HAS_MOVE)
  61. /// Assign from another address.
  62. ASIO_DECL address_v6& operator=(const address_v6& other);
  63. #if defined(ASIO_HAS_MOVE)
  64. /// Move-assign from another address.
  65. ASIO_DECL address_v6& operator=(address_v6&& other);
  66. #endif // defined(ASIO_HAS_MOVE)
  67. /// The scope ID of the address.
  68. /**
  69. * Returns the scope ID associated with the IPv6 address.
  70. */
  71. unsigned long scope_id() const
  72. {
  73. return scope_id_;
  74. }
  75. /// The scope ID of the address.
  76. /**
  77. * Modifies the scope ID associated with the IPv6 address.
  78. */
  79. void scope_id(unsigned long id)
  80. {
  81. scope_id_ = id;
  82. }
  83. /// Get the address in bytes, in network byte order.
  84. ASIO_DECL bytes_type to_bytes() const;
  85. /// Get the address as a string.
  86. ASIO_DECL std::string to_string() const;
  87. /// Get the address as a string.
  88. ASIO_DECL std::string to_string(asio::error_code& ec) const;
  89. /// Create an address from an IP address string.
  90. ASIO_DECL static address_v6 from_string(const char* str);
  91. /// Create an address from an IP address string.
  92. ASIO_DECL static address_v6 from_string(
  93. const char* str, asio::error_code& ec);
  94. /// Create an address from an IP address string.
  95. ASIO_DECL static address_v6 from_string(const std::string& str);
  96. /// Create an address from an IP address string.
  97. ASIO_DECL static address_v6 from_string(
  98. const std::string& str, asio::error_code& ec);
  99. /// Converts an IPv4-mapped or IPv4-compatible address to an IPv4 address.
  100. ASIO_DECL address_v4 to_v4() const;
  101. /// Determine whether the address is a loopback address.
  102. ASIO_DECL bool is_loopback() const;
  103. /// Determine whether the address is unspecified.
  104. ASIO_DECL bool is_unspecified() const;
  105. /// Determine whether the address is link local.
  106. ASIO_DECL bool is_link_local() const;
  107. /// Determine whether the address is site local.
  108. ASIO_DECL bool is_site_local() const;
  109. /// Determine whether the address is a mapped IPv4 address.
  110. ASIO_DECL bool is_v4_mapped() const;
  111. /// Determine whether the address is an IPv4-compatible address.
  112. ASIO_DECL bool is_v4_compatible() const;
  113. /// Determine whether the address is a multicast address.
  114. ASIO_DECL bool is_multicast() const;
  115. /// Determine whether the address is a global multicast address.
  116. ASIO_DECL bool is_multicast_global() const;
  117. /// Determine whether the address is a link-local multicast address.
  118. ASIO_DECL bool is_multicast_link_local() const;
  119. /// Determine whether the address is a node-local multicast address.
  120. ASIO_DECL bool is_multicast_node_local() const;
  121. /// Determine whether the address is a org-local multicast address.
  122. ASIO_DECL bool is_multicast_org_local() const;
  123. /// Determine whether the address is a site-local multicast address.
  124. ASIO_DECL bool is_multicast_site_local() const;
  125. /// Compare two addresses for equality.
  126. ASIO_DECL friend bool operator==(
  127. const address_v6& a1, const address_v6& a2);
  128. /// Compare two addresses for inequality.
  129. friend bool operator!=(const address_v6& a1, const address_v6& a2)
  130. {
  131. return !(a1 == a2);
  132. }
  133. /// Compare addresses for ordering.
  134. ASIO_DECL friend bool operator<(
  135. const address_v6& a1, const address_v6& a2);
  136. /// Compare addresses for ordering.
  137. friend bool operator>(const address_v6& a1, const address_v6& a2)
  138. {
  139. return a2 < a1;
  140. }
  141. /// Compare addresses for ordering.
  142. friend bool operator<=(const address_v6& a1, const address_v6& a2)
  143. {
  144. return !(a2 < a1);
  145. }
  146. /// Compare addresses for ordering.
  147. friend bool operator>=(const address_v6& a1, const address_v6& a2)
  148. {
  149. return !(a1 < a2);
  150. }
  151. /// Obtain an address object that represents any address.
  152. static address_v6 any()
  153. {
  154. return address_v6();
  155. }
  156. /// Obtain an address object that represents the loopback address.
  157. ASIO_DECL static address_v6 loopback();
  158. /// Create an IPv4-mapped IPv6 address.
  159. ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
  160. /// Create an IPv4-compatible IPv6 address.
  161. ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
  162. private:
  163. // The underlying IPv6 address.
  164. asio::detail::in6_addr_type addr_;
  165. // The scope ID associated with the address.
  166. unsigned long scope_id_;
  167. };
  168. #if !defined(ASIO_NO_IOSTREAM)
  169. /// Output an address as a string.
  170. /**
  171. * Used to output a human-readable string for a specified address.
  172. *
  173. * @param os The output stream to which the string will be written.
  174. *
  175. * @param addr The address to be written.
  176. *
  177. * @return The output stream.
  178. *
  179. * @relates asio::ip::address_v6
  180. */
  181. template <typename Elem, typename Traits>
  182. std::basic_ostream<Elem, Traits>& operator<<(
  183. std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
  184. #endif // !defined(ASIO_NO_IOSTREAM)
  185. } // namespace ip
  186. } // namespace asio
  187. #include "asio/detail/pop_options.hpp"
  188. #include "asio/ip/impl/address_v6.hpp"
  189. #if defined(ASIO_HEADER_ONLY)
  190. # include "asio/ip/impl/address_v6.ipp"
  191. #endif // defined(ASIO_HEADER_ONLY)
  192. #endif // ASIO_IP_ADDRESS_V6_HPP