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.

237 lines
6.1KB

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