network_v4.hpp 6.6 KB

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