address_v6.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // ip/address_v6.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 <boost/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(BOOST_NO_IOSTREAM)
  23. # include <iosfwd>
  24. #endif // !defined(BOOST_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. typedef boost::array<unsigned char, 16> bytes_type;
  42. /// Default constructor.
  43. ASIO_DECL address_v6();
  44. /// Construct an address from raw bytes and scope ID.
  45. ASIO_DECL explicit address_v6(const bytes_type& bytes,
  46. unsigned long scope_id = 0);
  47. /// Copy constructor.
  48. ASIO_DECL address_v6(const address_v6& other);
  49. /// Assign from another address.
  50. ASIO_DECL address_v6& operator=(const address_v6& other);
  51. /// The scope ID of the address.
  52. /**
  53. * Returns the scope ID associated with the IPv6 address.
  54. */
  55. unsigned long scope_id() const
  56. {
  57. return scope_id_;
  58. }
  59. /// The scope ID of the address.
  60. /**
  61. * Modifies the scope ID associated with the IPv6 address.
  62. */
  63. void scope_id(unsigned long id)
  64. {
  65. scope_id_ = id;
  66. }
  67. /// Get the address in bytes, in network byte order.
  68. ASIO_DECL bytes_type to_bytes() const;
  69. /// Get the address as a string.
  70. ASIO_DECL std::string to_string() const;
  71. /// Get the address as a string.
  72. ASIO_DECL std::string to_string(asio::error_code& ec) const;
  73. /// Create an address from an IP address string.
  74. ASIO_DECL static address_v6 from_string(const char* str);
  75. /// Create an address from an IP address string.
  76. ASIO_DECL static address_v6 from_string(
  77. const char* str, asio::error_code& ec);
  78. /// Create an address from an IP address string.
  79. ASIO_DECL static address_v6 from_string(const std::string& str);
  80. /// Create an address from an IP address string.
  81. ASIO_DECL static address_v6 from_string(
  82. const std::string& str, asio::error_code& ec);
  83. /// Converts an IPv4-mapped or IPv4-compatible address to an IPv4 address.
  84. ASIO_DECL address_v4 to_v4() const;
  85. /// Determine whether the address is a loopback address.
  86. ASIO_DECL bool is_loopback() const;
  87. /// Determine whether the address is unspecified.
  88. ASIO_DECL bool is_unspecified() const;
  89. /// Determine whether the address is link local.
  90. ASIO_DECL bool is_link_local() const;
  91. /// Determine whether the address is site local.
  92. ASIO_DECL bool is_site_local() const;
  93. /// Determine whether the address is a mapped IPv4 address.
  94. ASIO_DECL bool is_v4_mapped() const;
  95. /// Determine whether the address is an IPv4-compatible address.
  96. ASIO_DECL bool is_v4_compatible() const;
  97. /// Determine whether the address is a multicast address.
  98. ASIO_DECL bool is_multicast() const;
  99. /// Determine whether the address is a global multicast address.
  100. ASIO_DECL bool is_multicast_global() const;
  101. /// Determine whether the address is a link-local multicast address.
  102. ASIO_DECL bool is_multicast_link_local() const;
  103. /// Determine whether the address is a node-local multicast address.
  104. ASIO_DECL bool is_multicast_node_local() const;
  105. /// Determine whether the address is a org-local multicast address.
  106. ASIO_DECL bool is_multicast_org_local() const;
  107. /// Determine whether the address is a site-local multicast address.
  108. ASIO_DECL bool is_multicast_site_local() const;
  109. /// Compare two addresses for equality.
  110. ASIO_DECL friend bool operator==(
  111. const address_v6& a1, const address_v6& a2);
  112. /// Compare two addresses for inequality.
  113. friend bool operator!=(const address_v6& a1, const address_v6& a2)
  114. {
  115. return !(a1 == a2);
  116. }
  117. /// Compare addresses for ordering.
  118. ASIO_DECL friend bool operator<(
  119. const address_v6& a1, const address_v6& a2);
  120. /// Compare addresses for ordering.
  121. friend bool operator>(const address_v6& a1, const address_v6& a2)
  122. {
  123. return a2 < a1;
  124. }
  125. /// Compare addresses for ordering.
  126. friend bool operator<=(const address_v6& a1, const address_v6& a2)
  127. {
  128. return !(a2 < a1);
  129. }
  130. /// Compare addresses for ordering.
  131. friend bool operator>=(const address_v6& a1, const address_v6& a2)
  132. {
  133. return !(a1 < a2);
  134. }
  135. /// Obtain an address object that represents any address.
  136. static address_v6 any()
  137. {
  138. return address_v6();
  139. }
  140. /// Obtain an address object that represents the loopback address.
  141. ASIO_DECL static address_v6 loopback();
  142. /// Create an IPv4-mapped IPv6 address.
  143. ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
  144. /// Create an IPv4-compatible IPv6 address.
  145. ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
  146. private:
  147. // The underlying IPv6 address.
  148. asio::detail::in6_addr_type addr_;
  149. // The scope ID associated with the address.
  150. unsigned long scope_id_;
  151. };
  152. #if !defined(BOOST_NO_IOSTREAM)
  153. /// Output an address as a string.
  154. /**
  155. * Used to output a human-readable string for a specified address.
  156. *
  157. * @param os The output stream to which the string will be written.
  158. *
  159. * @param addr The address to be written.
  160. *
  161. * @return The output stream.
  162. *
  163. * @relates asio::ip::address_v6
  164. */
  165. template <typename Elem, typename Traits>
  166. std::basic_ostream<Elem, Traits>& operator<<(
  167. std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
  168. #endif // !defined(BOOST_NO_IOSTREAM)
  169. } // namespace ip
  170. } // namespace asio
  171. #include "asio/detail/pop_options.hpp"
  172. #include "asio/ip/impl/address_v6.hpp"
  173. #if defined(ASIO_HEADER_ONLY)
  174. # include "asio/ip/impl/address_v6.ipp"
  175. #endif // defined(ASIO_HEADER_ONLY)
  176. #endif // ASIO_IP_ADDRESS_V6_HPP