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.0KB

  1. //
  2. // ip/impl/address_v6.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_V6_IPP
  11. #define ASIO_IP_IMPL_ADDRESS_V6_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 <cstring>
  17. #include <stdexcept>
  18. #include <typeinfo>
  19. #include "asio/detail/socket_ops.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #include "asio/detail/throw_exception.hpp"
  22. #include "asio/error.hpp"
  23. #include "asio/ip/address_v6.hpp"
  24. #include "asio/ip/bad_address_cast.hpp"
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace ip {
  28. address_v6::address_v6()
  29. : addr_(),
  30. scope_id_(0)
  31. {
  32. }
  33. address_v6::address_v6(const address_v6::bytes_type& bytes,
  34. unsigned long scope)
  35. : scope_id_(scope)
  36. {
  37. #if UCHAR_MAX > 0xFF
  38. for (std::size_t i = 0; i < bytes.size(); ++i)
  39. {
  40. if (bytes[i] > 0xFF)
  41. {
  42. std::out_of_range ex("address_v6 from bytes_type");
  43. asio::detail::throw_exception(ex);
  44. }
  45. }
  46. #endif // UCHAR_MAX > 0xFF
  47. using namespace std; // For memcpy.
  48. memcpy(addr_.s6_addr, bytes.data(), 16);
  49. }
  50. address_v6::address_v6(const address_v6& other)
  51. : addr_(other.addr_),
  52. scope_id_(other.scope_id_)
  53. {
  54. }
  55. #if defined(ASIO_HAS_MOVE)
  56. address_v6::address_v6(address_v6&& other)
  57. : addr_(other.addr_),
  58. scope_id_(other.scope_id_)
  59. {
  60. }
  61. #endif // defined(ASIO_HAS_MOVE)
  62. address_v6& address_v6::operator=(const address_v6& other)
  63. {
  64. addr_ = other.addr_;
  65. scope_id_ = other.scope_id_;
  66. return *this;
  67. }
  68. #if defined(ASIO_HAS_MOVE)
  69. address_v6& address_v6::operator=(address_v6&& other)
  70. {
  71. addr_ = other.addr_;
  72. scope_id_ = other.scope_id_;
  73. return *this;
  74. }
  75. #endif // defined(ASIO_HAS_MOVE)
  76. address_v6::bytes_type address_v6::to_bytes() const
  77. {
  78. using namespace std; // For memcpy.
  79. bytes_type bytes;
  80. #if defined(ASIO_HAS_STD_ARRAY)
  81. memcpy(bytes.data(), addr_.s6_addr, 16);
  82. #else // defined(ASIO_HAS_STD_ARRAY)
  83. memcpy(bytes.elems, addr_.s6_addr, 16);
  84. #endif // defined(ASIO_HAS_STD_ARRAY)
  85. return bytes;
  86. }
  87. std::string address_v6::to_string() const
  88. {
  89. asio::error_code ec;
  90. char addr_str[asio::detail::max_addr_v6_str_len];
  91. const char* addr =
  92. asio::detail::socket_ops::inet_ntop(
  93. ASIO_OS_DEF(AF_INET6), &addr_, addr_str,
  94. asio::detail::max_addr_v6_str_len, scope_id_, ec);
  95. if (addr == 0)
  96. asio::detail::throw_error(ec);
  97. return addr;
  98. }
  99. #if !defined(ASIO_NO_DEPRECATED)
  100. std::string address_v6::to_string(asio::error_code& ec) const
  101. {
  102. char addr_str[asio::detail::max_addr_v6_str_len];
  103. const char* addr =
  104. asio::detail::socket_ops::inet_ntop(
  105. ASIO_OS_DEF(AF_INET6), &addr_, addr_str,
  106. asio::detail::max_addr_v6_str_len, scope_id_, ec);
  107. if (addr == 0)
  108. return std::string();
  109. return addr;
  110. }
  111. address_v4 address_v6::to_v4() const
  112. {
  113. if (!is_v4_mapped() && !is_v4_compatible())
  114. {
  115. bad_address_cast ex;
  116. asio::detail::throw_exception(ex);
  117. }
  118. address_v4::bytes_type v4_bytes = { { addr_.s6_addr[12],
  119. addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } };
  120. return address_v4(v4_bytes);
  121. }
  122. #endif // !defined(ASIO_NO_DEPRECATED)
  123. bool address_v6::is_loopback() const
  124. {
  125. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  126. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  127. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  128. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  129. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  130. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  131. && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
  132. && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 1));
  133. }
  134. bool address_v6::is_unspecified() const
  135. {
  136. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  137. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  138. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  139. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  140. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  141. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  142. && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
  143. && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 0));
  144. }
  145. bool address_v6::is_link_local() const
  146. {
  147. return ((addr_.s6_addr[0] == 0xfe) && ((addr_.s6_addr[1] & 0xc0) == 0x80));
  148. }
  149. bool address_v6::is_site_local() const
  150. {
  151. return ((addr_.s6_addr[0] == 0xfe) && ((addr_.s6_addr[1] & 0xc0) == 0xc0));
  152. }
  153. bool address_v6::is_v4_mapped() const
  154. {
  155. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  156. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  157. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  158. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  159. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  160. && (addr_.s6_addr[10] == 0xff) && (addr_.s6_addr[11] == 0xff));
  161. }
  162. #if !defined(ASIO_NO_DEPRECATED)
  163. bool address_v6::is_v4_compatible() const
  164. {
  165. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  166. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  167. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  168. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  169. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  170. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  171. && !((addr_.s6_addr[12] == 0)
  172. && (addr_.s6_addr[13] == 0)
  173. && (addr_.s6_addr[14] == 0)
  174. && ((addr_.s6_addr[15] == 0) || (addr_.s6_addr[15] == 1))));
  175. }
  176. #endif // !defined(ASIO_NO_DEPRECATED)
  177. bool address_v6::is_multicast() const
  178. {
  179. return (addr_.s6_addr[0] == 0xff);
  180. }
  181. bool address_v6::is_multicast_global() const
  182. {
  183. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x0e));
  184. }
  185. bool address_v6::is_multicast_link_local() const
  186. {
  187. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x02));
  188. }
  189. bool address_v6::is_multicast_node_local() const
  190. {
  191. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x01));
  192. }
  193. bool address_v6::is_multicast_org_local() const
  194. {
  195. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x08));
  196. }
  197. bool address_v6::is_multicast_site_local() const
  198. {
  199. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x05));
  200. }
  201. bool operator==(const address_v6& a1, const address_v6& a2)
  202. {
  203. using namespace std; // For memcmp.
  204. return memcmp(&a1.addr_, &a2.addr_,
  205. sizeof(asio::detail::in6_addr_type)) == 0
  206. && a1.scope_id_ == a2.scope_id_;
  207. }
  208. bool operator<(const address_v6& a1, const address_v6& a2)
  209. {
  210. using namespace std; // For memcmp.
  211. int memcmp_result = memcmp(&a1.addr_, &a2.addr_,
  212. sizeof(asio::detail::in6_addr_type));
  213. if (memcmp_result < 0)
  214. return true;
  215. if (memcmp_result > 0)
  216. return false;
  217. return a1.scope_id_ < a2.scope_id_;
  218. }
  219. address_v6 address_v6::loopback()
  220. {
  221. address_v6 tmp;
  222. tmp.addr_.s6_addr[15] = 1;
  223. return tmp;
  224. }
  225. #if !defined(ASIO_NO_DEPRECATED)
  226. address_v6 address_v6::v4_mapped(const address_v4& addr)
  227. {
  228. address_v4::bytes_type v4_bytes = addr.to_bytes();
  229. bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
  230. v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  231. return address_v6(v6_bytes);
  232. }
  233. address_v6 address_v6::v4_compatible(const address_v4& addr)
  234. {
  235. address_v4::bytes_type v4_bytes = addr.to_bytes();
  236. bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  237. v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  238. return address_v6(v6_bytes);
  239. }
  240. #endif // !defined(ASIO_NO_DEPRECATED)
  241. address_v6 make_address_v6(const char* str)
  242. {
  243. asio::error_code ec;
  244. address_v6 addr = make_address_v6(str, ec);
  245. asio::detail::throw_error(ec);
  246. return addr;
  247. }
  248. address_v6 make_address_v6(
  249. const char* str, asio::error_code& ec)
  250. {
  251. address_v6::bytes_type bytes;
  252. unsigned long scope_id = 0;
  253. if (asio::detail::socket_ops::inet_pton(
  254. ASIO_OS_DEF(AF_INET6), str, &bytes[0], &scope_id, ec) <= 0)
  255. return address_v6();
  256. return address_v6(bytes, scope_id);
  257. }
  258. address_v6 make_address_v6(const std::string& str)
  259. {
  260. return make_address_v6(str.c_str());
  261. }
  262. address_v6 make_address_v6(
  263. const std::string& str, asio::error_code& ec)
  264. {
  265. return make_address_v6(str.c_str(), ec);
  266. }
  267. address_v4 make_address_v4(
  268. v4_mapped_t, const address_v6& v6_addr)
  269. {
  270. if (!v6_addr.is_v4_mapped())
  271. {
  272. bad_address_cast ex;
  273. asio::detail::throw_exception(ex);
  274. }
  275. address_v6::bytes_type v6_bytes = v6_addr.to_bytes();
  276. address_v4::bytes_type v4_bytes = { { v6_bytes[12],
  277. v6_bytes[13], v6_bytes[14], v6_bytes[15] } };
  278. return address_v4(v4_bytes);
  279. }
  280. address_v6 make_address_v6(
  281. v4_mapped_t, const address_v4& v4_addr)
  282. {
  283. address_v4::bytes_type v4_bytes = v4_addr.to_bytes();
  284. address_v6::bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  285. 0xFF, 0xFF, v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  286. return address_v6(v6_bytes);
  287. }
  288. } // namespace ip
  289. } // namespace asio
  290. #include "asio/detail/pop_options.hpp"
  291. #endif // ASIO_IP_IMPL_ADDRESS_V6_IPP