Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
6.7KB

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