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.

196 lines
4.7KB

  1. //
  2. // ip/impl/address_v4.ipp
  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_IMPL_ADDRESS_V4_IPP
  11. #define ASIO_IP_IMPL_ADDRESS_V4_IPP
  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 <climits>
  17. #include <limits>
  18. #include <stdexcept>
  19. #include "asio/error.hpp"
  20. #include "asio/detail/socket_ops.hpp"
  21. #include "asio/detail/throw_error.hpp"
  22. #include "asio/detail/throw_exception.hpp"
  23. #include "asio/ip/address_v4.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace ip {
  27. address_v4::address_v4(const address_v4::bytes_type& bytes)
  28. {
  29. #if UCHAR_MAX > 0xFF
  30. if (bytes[0] > 0xFF || bytes[1] > 0xFF
  31. || bytes[2] > 0xFF || bytes[3] > 0xFF)
  32. {
  33. std::out_of_range ex("address_v4 from bytes_type");
  34. asio::detail::throw_exception(ex);
  35. }
  36. #endif // UCHAR_MAX > 0xFF
  37. using namespace std; // For memcpy.
  38. memcpy(&addr_.s_addr, bytes.data(), 4);
  39. }
  40. address_v4::address_v4(address_v4::uint_type addr)
  41. {
  42. if ((std::numeric_limits<uint_type>::max)() > 0xFFFFFFFF)
  43. {
  44. std::out_of_range ex("address_v4 from unsigned integer");
  45. asio::detail::throw_exception(ex);
  46. }
  47. addr_.s_addr = asio::detail::socket_ops::host_to_network_long(
  48. static_cast<asio::detail::u_long_type>(addr));
  49. }
  50. address_v4::bytes_type address_v4::to_bytes() const
  51. {
  52. using namespace std; // For memcpy.
  53. bytes_type bytes;
  54. #if defined(ASIO_HAS_STD_ARRAY)
  55. memcpy(bytes.data(), &addr_.s_addr, 4);
  56. #else // defined(ASIO_HAS_STD_ARRAY)
  57. memcpy(bytes.elems, &addr_.s_addr, 4);
  58. #endif // defined(ASIO_HAS_STD_ARRAY)
  59. return bytes;
  60. }
  61. address_v4::uint_type address_v4::to_uint() const
  62. {
  63. return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  64. }
  65. #if !defined(ASIO_NO_DEPRECATED)
  66. unsigned long address_v4::to_ulong() const
  67. {
  68. return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  69. }
  70. #endif // !defined(ASIO_NO_DEPRECATED)
  71. std::string address_v4::to_string() const
  72. {
  73. asio::error_code ec;
  74. char addr_str[asio::detail::max_addr_v4_str_len];
  75. const char* addr =
  76. asio::detail::socket_ops::inet_ntop(
  77. ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  78. asio::detail::max_addr_v4_str_len, 0, ec);
  79. if (addr == 0)
  80. asio::detail::throw_error(ec);
  81. return addr;
  82. }
  83. #if !defined(ASIO_NO_DEPRECATED)
  84. std::string address_v4::to_string(asio::error_code& ec) const
  85. {
  86. char addr_str[asio::detail::max_addr_v4_str_len];
  87. const char* addr =
  88. asio::detail::socket_ops::inet_ntop(
  89. ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  90. asio::detail::max_addr_v4_str_len, 0, ec);
  91. if (addr == 0)
  92. return std::string();
  93. return addr;
  94. }
  95. #endif // !defined(ASIO_NO_DEPRECATED)
  96. bool address_v4::is_loopback() const
  97. {
  98. return (to_uint() & 0xFF000000) == 0x7F000000;
  99. }
  100. bool address_v4::is_unspecified() const
  101. {
  102. return to_uint() == 0;
  103. }
  104. #if !defined(ASIO_NO_DEPRECATED)
  105. bool address_v4::is_class_a() const
  106. {
  107. return (to_uint() & 0x80000000) == 0;
  108. }
  109. bool address_v4::is_class_b() const
  110. {
  111. return (to_uint() & 0xC0000000) == 0x80000000;
  112. }
  113. bool address_v4::is_class_c() const
  114. {
  115. return (to_uint() & 0xE0000000) == 0xC0000000;
  116. }
  117. #endif // !defined(ASIO_NO_DEPRECATED)
  118. bool address_v4::is_multicast() const
  119. {
  120. return (to_uint() & 0xF0000000) == 0xE0000000;
  121. }
  122. #if !defined(ASIO_NO_DEPRECATED)
  123. address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
  124. {
  125. return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF));
  126. }
  127. address_v4 address_v4::netmask(const address_v4& addr)
  128. {
  129. if (addr.is_class_a())
  130. return address_v4(0xFF000000);
  131. if (addr.is_class_b())
  132. return address_v4(0xFFFF0000);
  133. if (addr.is_class_c())
  134. return address_v4(0xFFFFFF00);
  135. return address_v4(0xFFFFFFFF);
  136. }
  137. #endif // !defined(ASIO_NO_DEPRECATED)
  138. address_v4 make_address_v4(const char* str)
  139. {
  140. asio::error_code ec;
  141. address_v4 addr = make_address_v4(str, ec);
  142. asio::detail::throw_error(ec);
  143. return addr;
  144. }
  145. address_v4 make_address_v4(
  146. const char* str, asio::error_code& ec)
  147. {
  148. address_v4::bytes_type bytes;
  149. if (asio::detail::socket_ops::inet_pton(
  150. ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0)
  151. return address_v4();
  152. return address_v4(bytes);
  153. }
  154. address_v4 make_address_v4(const std::string& str)
  155. {
  156. return make_address_v4(str.c_str());
  157. }
  158. address_v4 make_address_v4(
  159. const std::string& str, asio::error_code& ec)
  160. {
  161. return make_address_v4(str.c_str(), ec);
  162. }
  163. } // namespace ip
  164. } // namespace asio
  165. #include "asio/detail/pop_options.hpp"
  166. #endif // ASIO_IP_IMPL_ADDRESS_V4_IPP