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.

336 lines
9.3KB

  1. //
  2. // ip/address_v4.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_IP_ADDRESS_V4_HPP
  11. #define ASIO_IP_ADDRESS_V4_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <string>
  17. #include "asio/detail/array.hpp"
  18. #include "asio/detail/cstdint.hpp"
  19. #include "asio/detail/socket_types.hpp"
  20. #include "asio/detail/string_view.hpp"
  21. #include "asio/detail/winsock_init.hpp"
  22. #include "asio/error_code.hpp"
  23. #if !defined(ASIO_NO_IOSTREAM)
  24. # include <iosfwd>
  25. #endif // !defined(ASIO_NO_IOSTREAM)
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. namespace ip {
  29. /// Implements IP version 4 style addresses.
  30. /**
  31. * The asio::ip::address_v4 class provides the ability to use and
  32. * manipulate IP version 4 addresses.
  33. *
  34. * @par Thread Safety
  35. * @e Distinct @e objects: Safe.@n
  36. * @e Shared @e objects: Unsafe.
  37. */
  38. class address_v4
  39. {
  40. public:
  41. /// The type used to represent an address as an unsigned integer.
  42. typedef uint_least32_t uint_type;
  43. /// The type used to represent an address as an array of bytes.
  44. /**
  45. * @note This type is defined in terms of the C++0x template @c std::array
  46. * when it is available. Otherwise, it uses @c boost:array.
  47. */
  48. #if defined(GENERATING_DOCUMENTATION)
  49. typedef array<unsigned char, 4> bytes_type;
  50. #else
  51. typedef asio::detail::array<unsigned char, 4> bytes_type;
  52. #endif
  53. /// Default constructor.
  54. address_v4() ASIO_NOEXCEPT
  55. {
  56. addr_.s_addr = 0;
  57. }
  58. /// Construct an address from raw bytes.
  59. ASIO_DECL explicit address_v4(const bytes_type& bytes);
  60. /// Construct an address from an unsigned integer in host byte order.
  61. ASIO_DECL explicit address_v4(uint_type addr);
  62. /// Copy constructor.
  63. address_v4(const address_v4& other) ASIO_NOEXCEPT
  64. : addr_(other.addr_)
  65. {
  66. }
  67. #if defined(ASIO_HAS_MOVE)
  68. /// Move constructor.
  69. address_v4(address_v4&& other) ASIO_NOEXCEPT
  70. : addr_(other.addr_)
  71. {
  72. }
  73. #endif // defined(ASIO_HAS_MOVE)
  74. /// Assign from another address.
  75. address_v4& operator=(const address_v4& other) ASIO_NOEXCEPT
  76. {
  77. addr_ = other.addr_;
  78. return *this;
  79. }
  80. #if defined(ASIO_HAS_MOVE)
  81. /// Move-assign from another address.
  82. address_v4& operator=(address_v4&& other) ASIO_NOEXCEPT
  83. {
  84. addr_ = other.addr_;
  85. return *this;
  86. }
  87. #endif // defined(ASIO_HAS_MOVE)
  88. /// Get the address in bytes, in network byte order.
  89. ASIO_DECL bytes_type to_bytes() const ASIO_NOEXCEPT;
  90. /// Get the address as an unsigned integer in host byte order
  91. ASIO_DECL uint_type to_uint() const ASIO_NOEXCEPT;
  92. #if !defined(ASIO_NO_DEPRECATED)
  93. /// Get the address as an unsigned long in host byte order
  94. ASIO_DECL unsigned long to_ulong() const;
  95. #endif // !defined(ASIO_NO_DEPRECATED)
  96. /// Get the address as a string in dotted decimal format.
  97. ASIO_DECL std::string to_string() const;
  98. #if !defined(ASIO_NO_DEPRECATED)
  99. /// (Deprecated: Use other overload.) Get the address as a string in dotted
  100. /// decimal format.
  101. ASIO_DECL std::string to_string(asio::error_code& ec) const;
  102. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  103. /// string in dotted decimal form.
  104. static address_v4 from_string(const char* str);
  105. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  106. /// string in dotted decimal form.
  107. static address_v4 from_string(
  108. const char* str, asio::error_code& ec);
  109. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  110. /// string in dotted decimal form.
  111. static address_v4 from_string(const std::string& str);
  112. /// (Deprecated: Use make_address_v4().) Create an address from an IP address
  113. /// string in dotted decimal form.
  114. static address_v4 from_string(
  115. const std::string& str, asio::error_code& ec);
  116. #endif // !defined(ASIO_NO_DEPRECATED)
  117. /// Determine whether the address is a loopback address.
  118. ASIO_DECL bool is_loopback() const ASIO_NOEXCEPT;
  119. /// Determine whether the address is unspecified.
  120. ASIO_DECL bool is_unspecified() const ASIO_NOEXCEPT;
  121. #if !defined(ASIO_NO_DEPRECATED)
  122. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  123. /// class A address.
  124. ASIO_DECL bool is_class_a() const;
  125. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  126. /// class B address.
  127. ASIO_DECL bool is_class_b() const;
  128. /// (Deprecated: Use network_v4 class.) Determine whether the address is a
  129. /// class C address.
  130. ASIO_DECL bool is_class_c() const;
  131. #endif // !defined(ASIO_NO_DEPRECATED)
  132. /// Determine whether the address is a multicast address.
  133. ASIO_DECL bool is_multicast() const ASIO_NOEXCEPT;
  134. /// Compare two addresses for equality.
  135. friend bool operator==(const address_v4& a1,
  136. const address_v4& a2) ASIO_NOEXCEPT
  137. {
  138. return a1.addr_.s_addr == a2.addr_.s_addr;
  139. }
  140. /// Compare two addresses for inequality.
  141. friend bool operator!=(const address_v4& a1,
  142. const address_v4& a2) ASIO_NOEXCEPT
  143. {
  144. return a1.addr_.s_addr != a2.addr_.s_addr;
  145. }
  146. /// Compare addresses for ordering.
  147. friend bool operator<(const address_v4& a1,
  148. const address_v4& a2) ASIO_NOEXCEPT
  149. {
  150. return a1.to_uint() < a2.to_uint();
  151. }
  152. /// Compare addresses for ordering.
  153. friend bool operator>(const address_v4& a1,
  154. const address_v4& a2) ASIO_NOEXCEPT
  155. {
  156. return a1.to_uint() > a2.to_uint();
  157. }
  158. /// Compare addresses for ordering.
  159. friend bool operator<=(const address_v4& a1,
  160. const address_v4& a2) ASIO_NOEXCEPT
  161. {
  162. return a1.to_uint() <= a2.to_uint();
  163. }
  164. /// Compare addresses for ordering.
  165. friend bool operator>=(const address_v4& a1,
  166. const address_v4& a2) ASIO_NOEXCEPT
  167. {
  168. return a1.to_uint() >= a2.to_uint();
  169. }
  170. /// Obtain an address object that represents any address.
  171. static address_v4 any() ASIO_NOEXCEPT
  172. {
  173. return address_v4();
  174. }
  175. /// Obtain an address object that represents the loopback address.
  176. static address_v4 loopback() ASIO_NOEXCEPT
  177. {
  178. return address_v4(0x7F000001);
  179. }
  180. /// Obtain an address object that represents the broadcast address.
  181. static address_v4 broadcast() ASIO_NOEXCEPT
  182. {
  183. return address_v4(0xFFFFFFFF);
  184. }
  185. #if !defined(ASIO_NO_DEPRECATED)
  186. /// (Deprecated: Use network_v4 class.) Obtain an address object that
  187. /// represents the broadcast address that corresponds to the specified
  188. /// address and netmask.
  189. ASIO_DECL static address_v4 broadcast(
  190. const address_v4& addr, const address_v4& mask);
  191. /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
  192. /// to the address, based on its address class.
  193. ASIO_DECL static address_v4 netmask(const address_v4& addr);
  194. #endif // !defined(ASIO_NO_DEPRECATED)
  195. private:
  196. // The underlying IPv4 address.
  197. asio::detail::in4_addr_type addr_;
  198. };
  199. /// Create an IPv4 address from raw bytes in network order.
  200. /**
  201. * @relates address_v4
  202. */
  203. inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
  204. {
  205. return address_v4(bytes);
  206. }
  207. /// Create an IPv4 address from an unsigned integer in host byte order.
  208. /**
  209. * @relates address_v4
  210. */
  211. inline address_v4 make_address_v4(address_v4::uint_type addr)
  212. {
  213. return address_v4(addr);
  214. }
  215. /// Create an IPv4 address from an IP address string in dotted decimal form.
  216. /**
  217. * @relates address_v4
  218. */
  219. ASIO_DECL address_v4 make_address_v4(const char* str);
  220. /// Create an IPv4 address from an IP address string in dotted decimal form.
  221. /**
  222. * @relates address_v4
  223. */
  224. ASIO_DECL address_v4 make_address_v4(const char* str,
  225. asio::error_code& ec) ASIO_NOEXCEPT;
  226. /// Create an IPv4 address from an IP address string in dotted decimal form.
  227. /**
  228. * @relates address_v4
  229. */
  230. ASIO_DECL address_v4 make_address_v4(const std::string& str);
  231. /// Create an IPv4 address from an IP address string in dotted decimal form.
  232. /**
  233. * @relates address_v4
  234. */
  235. ASIO_DECL address_v4 make_address_v4(const std::string& str,
  236. asio::error_code& ec) ASIO_NOEXCEPT;
  237. #if defined(ASIO_HAS_STRING_VIEW) \
  238. || defined(GENERATING_DOCUMENTATION)
  239. /// Create an IPv4 address from an IP address string in dotted decimal form.
  240. /**
  241. * @relates address_v4
  242. */
  243. ASIO_DECL address_v4 make_address_v4(string_view str);
  244. /// Create an IPv4 address from an IP address string in dotted decimal form.
  245. /**
  246. * @relates address_v4
  247. */
  248. ASIO_DECL address_v4 make_address_v4(string_view str,
  249. asio::error_code& ec) ASIO_NOEXCEPT;
  250. #endif // defined(ASIO_HAS_STRING_VIEW)
  251. // || defined(GENERATING_DOCUMENTATION)
  252. #if !defined(ASIO_NO_IOSTREAM)
  253. /// Output an address as a string.
  254. /**
  255. * Used to output a human-readable string for a specified address.
  256. *
  257. * @param os The output stream to which the string will be written.
  258. *
  259. * @param addr The address to be written.
  260. *
  261. * @return The output stream.
  262. *
  263. * @relates asio::ip::address_v4
  264. */
  265. template <typename Elem, typename Traits>
  266. std::basic_ostream<Elem, Traits>& operator<<(
  267. std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
  268. #endif // !defined(ASIO_NO_IOSTREAM)
  269. } // namespace ip
  270. } // namespace asio
  271. #include "asio/detail/pop_options.hpp"
  272. #include "asio/ip/impl/address_v4.hpp"
  273. #if defined(ASIO_HEADER_ONLY)
  274. # include "asio/ip/impl/address_v4.ipp"
  275. #endif // defined(ASIO_HEADER_ONLY)
  276. #endif // ASIO_IP_ADDRESS_V4_HPP