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.

address_v4.hpp 8.4KB

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