network_v6.hpp 6.1 KB

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