network_v4.hpp 7.1 KB

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