network_v4.hpp 6.8 KB

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