network_v6.hpp 6.3 KB

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