address_v6.ipp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // ip/impl/address_v6.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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 BOOST_ASIO_IP_IMPL_ADDRESS_V6_IPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <cstring>
  17. #include <stdexcept>
  18. #include <typeinfo>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/detail/throw_exception.hpp>
  22. #include <boost/asio/error.hpp>
  23. #include <boost/asio/ip/address_v6.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  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. boost::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(BOOST_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(BOOST_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(BOOST_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(BOOST_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(BOOST_ASIO_HAS_STD_ARRAY)
  81. memcpy(bytes.data(), addr_.s6_addr, 16);
  82. #else // defined(BOOST_ASIO_HAS_STD_ARRAY)
  83. memcpy(bytes.elems, addr_.s6_addr, 16);
  84. #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
  85. return bytes;
  86. }
  87. std::string address_v6::to_string() const
  88. {
  89. boost::system::error_code ec;
  90. std::string addr = to_string(ec);
  91. boost::asio::detail::throw_error(ec);
  92. return addr;
  93. }
  94. std::string address_v6::to_string(boost::system::error_code& ec) const
  95. {
  96. char addr_str[boost::asio::detail::max_addr_v6_str_len];
  97. const char* addr =
  98. boost::asio::detail::socket_ops::inet_ntop(
  99. BOOST_ASIO_OS_DEF(AF_INET6), &addr_, addr_str,
  100. boost::asio::detail::max_addr_v6_str_len, scope_id_, ec);
  101. if (addr == 0)
  102. return std::string();
  103. return addr;
  104. }
  105. address_v6 address_v6::from_string(const char* str)
  106. {
  107. boost::system::error_code ec;
  108. address_v6 addr = from_string(str, ec);
  109. boost::asio::detail::throw_error(ec);
  110. return addr;
  111. }
  112. address_v6 address_v6::from_string(
  113. const char* str, boost::system::error_code& ec)
  114. {
  115. address_v6 tmp;
  116. if (boost::asio::detail::socket_ops::inet_pton(
  117. BOOST_ASIO_OS_DEF(AF_INET6), str, &tmp.addr_, &tmp.scope_id_, ec) <= 0)
  118. return address_v6();
  119. return tmp;
  120. }
  121. address_v6 address_v6::from_string(const std::string& str)
  122. {
  123. return from_string(str.c_str());
  124. }
  125. address_v6 address_v6::from_string(
  126. const std::string& str, boost::system::error_code& ec)
  127. {
  128. return from_string(str.c_str(), ec);
  129. }
  130. address_v4 address_v6::to_v4() const
  131. {
  132. if (!is_v4_mapped() && !is_v4_compatible())
  133. {
  134. std::bad_cast ex;
  135. boost::asio::detail::throw_exception(ex);
  136. }
  137. address_v4::bytes_type v4_bytes = { { addr_.s6_addr[12],
  138. addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } };
  139. return address_v4(v4_bytes);
  140. }
  141. bool address_v6::is_loopback() const
  142. {
  143. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  144. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  145. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  146. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  147. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  148. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  149. && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
  150. && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 1));
  151. }
  152. bool address_v6::is_unspecified() const
  153. {
  154. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  155. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  156. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  157. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  158. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  159. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  160. && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
  161. && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 0));
  162. }
  163. bool address_v6::is_link_local() const
  164. {
  165. return ((addr_.s6_addr[0] == 0xfe) && ((addr_.s6_addr[1] & 0xc0) == 0x80));
  166. }
  167. bool address_v6::is_site_local() const
  168. {
  169. return ((addr_.s6_addr[0] == 0xfe) && ((addr_.s6_addr[1] & 0xc0) == 0xc0));
  170. }
  171. bool address_v6::is_v4_mapped() const
  172. {
  173. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  174. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  175. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  176. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  177. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  178. && (addr_.s6_addr[10] == 0xff) && (addr_.s6_addr[11] == 0xff));
  179. }
  180. bool address_v6::is_v4_compatible() const
  181. {
  182. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  183. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  184. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  185. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  186. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  187. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  188. && !((addr_.s6_addr[12] == 0)
  189. && (addr_.s6_addr[13] == 0)
  190. && (addr_.s6_addr[14] == 0)
  191. && ((addr_.s6_addr[15] == 0) || (addr_.s6_addr[15] == 1))));
  192. }
  193. bool address_v6::is_multicast() const
  194. {
  195. return (addr_.s6_addr[0] == 0xff);
  196. }
  197. bool address_v6::is_multicast_global() const
  198. {
  199. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x0e));
  200. }
  201. bool address_v6::is_multicast_link_local() const
  202. {
  203. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x02));
  204. }
  205. bool address_v6::is_multicast_node_local() const
  206. {
  207. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x01));
  208. }
  209. bool address_v6::is_multicast_org_local() const
  210. {
  211. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x08));
  212. }
  213. bool address_v6::is_multicast_site_local() const
  214. {
  215. return ((addr_.s6_addr[0] == 0xff) && ((addr_.s6_addr[1] & 0x0f) == 0x05));
  216. }
  217. bool operator==(const address_v6& a1, const address_v6& a2)
  218. {
  219. using namespace std; // For memcmp.
  220. return memcmp(&a1.addr_, &a2.addr_,
  221. sizeof(boost::asio::detail::in6_addr_type)) == 0
  222. && a1.scope_id_ == a2.scope_id_;
  223. }
  224. bool operator<(const address_v6& a1, const address_v6& a2)
  225. {
  226. using namespace std; // For memcmp.
  227. int memcmp_result = memcmp(&a1.addr_, &a2.addr_,
  228. sizeof(boost::asio::detail::in6_addr_type));
  229. if (memcmp_result < 0)
  230. return true;
  231. if (memcmp_result > 0)
  232. return false;
  233. return a1.scope_id_ < a2.scope_id_;
  234. }
  235. address_v6 address_v6::loopback()
  236. {
  237. address_v6 tmp;
  238. tmp.addr_.s6_addr[15] = 1;
  239. return tmp;
  240. }
  241. address_v6 address_v6::v4_mapped(const address_v4& addr)
  242. {
  243. address_v4::bytes_type v4_bytes = addr.to_bytes();
  244. bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
  245. v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  246. return address_v6(v6_bytes);
  247. }
  248. address_v6 address_v6::v4_compatible(const address_v4& addr)
  249. {
  250. address_v4::bytes_type v4_bytes = addr.to_bytes();
  251. bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  252. v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  253. return address_v6(v6_bytes);
  254. }
  255. } // namespace ip
  256. } // namespace asio
  257. } // namespace boost
  258. #include <boost/asio/detail/pop_options.hpp>
  259. #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V6_IPP