network_v6.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // ip/network_v6.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_IP_NETWORK_V6_HPP
  12. #define ASIO_IP_NETWORK_V6_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #include <string>
  18. #include "asio/detail/string_view.hpp"
  19. #include "asio/error_code.hpp"
  20. #include "asio/ip/address_v6_range.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace ip {
  24. /// Represents an IPv6 network.
  25. /**
  26. * The asio::ip::network_v6 class provides the ability to use and
  27. * manipulate IP version 6 networks.
  28. *
  29. * @par Thread Safety
  30. * @e Distinct @e objects: Safe.@n
  31. * @e Shared @e objects: Unsafe.
  32. */
  33. class network_v6
  34. {
  35. public:
  36. /// Default constructor.
  37. network_v6() noexcept
  38. : address_(),
  39. prefix_length_(0)
  40. {
  41. }
  42. /// Construct a network based on the specified address and prefix length.
  43. ASIO_DECL network_v6(const address_v6& addr,
  44. unsigned short prefix_len);
  45. /// Copy constructor.
  46. network_v6(const network_v6& other) noexcept
  47. : address_(other.address_),
  48. prefix_length_(other.prefix_length_)
  49. {
  50. }
  51. /// Move constructor.
  52. network_v6(network_v6&& other) noexcept
  53. : address_(static_cast<address_v6&&>(other.address_)),
  54. prefix_length_(other.prefix_length_)
  55. {
  56. }
  57. /// Assign from another network.
  58. network_v6& operator=(const network_v6& other) noexcept
  59. {
  60. address_ = other.address_;
  61. prefix_length_ = other.prefix_length_;
  62. return *this;
  63. }
  64. /// Move-assign from another network.
  65. network_v6& operator=(network_v6&& other) noexcept
  66. {
  67. address_ = static_cast<address_v6&&>(other.address_);
  68. prefix_length_ = other.prefix_length_;
  69. return *this;
  70. }
  71. /// Obtain the address object specified when the network object was created.
  72. address_v6 address() const noexcept
  73. {
  74. return address_;
  75. }
  76. /// Obtain the prefix length that was specified when the network object was
  77. /// created.
  78. unsigned short prefix_length() const noexcept
  79. {
  80. return prefix_length_;
  81. }
  82. /// Obtain an address object that represents the network address.
  83. ASIO_DECL address_v6 network() const noexcept;
  84. /// Obtain an address range corresponding to the hosts in the network.
  85. ASIO_DECL address_v6_range hosts() const noexcept;
  86. /// Obtain the true network address, omitting any host bits.
  87. network_v6 canonical() const noexcept
  88. {
  89. return network_v6(network(), prefix_length());
  90. }
  91. /// Test if network is a valid host address.
  92. bool is_host() const noexcept
  93. {
  94. return prefix_length_ == 128;
  95. }
  96. /// Test if a network is a real subnet of another network.
  97. ASIO_DECL bool is_subnet_of(const network_v6& other) const;
  98. /// Get the network as an address in dotted decimal format.
  99. ASIO_DECL std::string to_string() const;
  100. /// Get the network as an address in dotted decimal format.
  101. ASIO_DECL std::string to_string(asio::error_code& ec) const;
  102. /// Compare two networks for equality.
  103. friend bool operator==(const network_v6& a, const network_v6& b)
  104. {
  105. return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
  106. }
  107. /// Compare two networks for inequality.
  108. friend bool operator!=(const network_v6& a, const network_v6& b)
  109. {
  110. return !(a == b);
  111. }
  112. private:
  113. address_v6 address_;
  114. unsigned short prefix_length_;
  115. };
  116. /// Create an IPv6 network from an address and prefix length.
  117. /**
  118. * @relates address_v6
  119. */
  120. inline network_v6 make_network_v6(
  121. const address_v6& addr, unsigned short prefix_len)
  122. {
  123. return network_v6(addr, prefix_len);
  124. }
  125. /// Create an IPv6 network from a string containing IP address and prefix
  126. /// length.
  127. /**
  128. * @relates network_v6
  129. */
  130. ASIO_DECL network_v6 make_network_v6(const char* str);
  131. /// Create an IPv6 network from a string containing IP address and prefix
  132. /// length.
  133. /**
  134. * @relates network_v6
  135. */
  136. ASIO_DECL network_v6 make_network_v6(
  137. const char* str, asio::error_code& ec);
  138. /// Create an IPv6 network from a string containing IP address and prefix
  139. /// length.
  140. /**
  141. * @relates network_v6
  142. */
  143. ASIO_DECL network_v6 make_network_v6(const std::string& str);
  144. /// Create an IPv6 network from a string containing IP address and prefix
  145. /// length.
  146. /**
  147. * @relates network_v6
  148. */
  149. ASIO_DECL network_v6 make_network_v6(
  150. const std::string& str, asio::error_code& ec);
  151. #if defined(ASIO_HAS_STRING_VIEW) \
  152. || defined(GENERATING_DOCUMENTATION)
  153. /// Create an IPv6 network from a string containing IP address and prefix
  154. /// length.
  155. /**
  156. * @relates network_v6
  157. */
  158. ASIO_DECL network_v6 make_network_v6(string_view str);
  159. /// Create an IPv6 network from a string containing IP address and prefix
  160. /// length.
  161. /**
  162. * @relates network_v6
  163. */
  164. ASIO_DECL network_v6 make_network_v6(
  165. string_view str, asio::error_code& ec);
  166. #endif // defined(ASIO_HAS_STRING_VIEW)
  167. // || defined(GENERATING_DOCUMENTATION)
  168. #if !defined(ASIO_NO_IOSTREAM)
  169. /// Output a network as a string.
  170. /**
  171. * Used to output a human-readable string for a specified network.
  172. *
  173. * @param os The output stream to which the string will be written.
  174. *
  175. * @param net The network 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 network_v6& net);
  184. #endif // !defined(ASIO_NO_IOSTREAM)
  185. } // namespace ip
  186. } // namespace asio
  187. #include "asio/detail/pop_options.hpp"
  188. #include "asio/ip/impl/network_v6.hpp"
  189. #if defined(ASIO_HEADER_ONLY)
  190. # include "asio/ip/impl/network_v6.ipp"
  191. #endif // defined(ASIO_HEADER_ONLY)
  192. #endif // ASIO_IP_NETWORK_V6_HPP