address_v6.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // address_v6.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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_ADDRESS_V6_HPP
  11. #define BOOST_ASIO_IP_ADDRESS_V6_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstring>
  18. #include <string>
  19. #include <stdexcept>
  20. #include <typeinfo>
  21. #include <boost/array.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <boost/asio/detail/pop_options.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/detail/socket_ops.hpp>
  26. #include <boost/asio/detail/socket_types.hpp>
  27. #include <boost/asio/detail/throw_error.hpp>
  28. #include <boost/asio/ip/address_v4.hpp>
  29. namespace boost {
  30. namespace asio {
  31. namespace ip {
  32. /// Implements IP version 6 style addresses.
  33. /**
  34. * The boost::asio::ip::address_v6 class provides the ability to use and
  35. * manipulate IP version 6 addresses.
  36. *
  37. * @par Thread Safety
  38. * @e Distinct @e objects: Safe.@n
  39. * @e Shared @e objects: Unsafe.
  40. */
  41. class address_v6
  42. {
  43. public:
  44. /// The type used to represent an address as an array of bytes.
  45. typedef boost::array<unsigned char, 16> bytes_type;
  46. /// Default constructor.
  47. address_v6()
  48. : scope_id_(0)
  49. {
  50. boost::asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
  51. addr_ = tmp_addr;
  52. }
  53. /// Construct an address from raw bytes and scope ID.
  54. explicit address_v6(const bytes_type& bytes, unsigned long scope_id = 0)
  55. : scope_id_(scope_id)
  56. {
  57. #if UCHAR_MAX > 0xFF
  58. for (std::size_t i = 0; i < bytes.size(); ++i)
  59. {
  60. if (bytes[i] > 0xFF)
  61. {
  62. std::out_of_range ex("address_v6 from bytes_type");
  63. boost::throw_exception(ex);
  64. }
  65. }
  66. #endif // UCHAR_MAX > 0xFF
  67. using namespace std; // For memcpy.
  68. memcpy(addr_.s6_addr, bytes.elems, 16);
  69. }
  70. /// Copy constructor.
  71. address_v6(const address_v6& other)
  72. : addr_(other.addr_),
  73. scope_id_(other.scope_id_)
  74. {
  75. }
  76. /// Assign from another address.
  77. address_v6& operator=(const address_v6& other)
  78. {
  79. addr_ = other.addr_;
  80. scope_id_ = other.scope_id_;
  81. return *this;
  82. }
  83. /// The scope ID of the address.
  84. /**
  85. * Returns the scope ID associated with the IPv6 address.
  86. */
  87. unsigned long scope_id() const
  88. {
  89. return scope_id_;
  90. }
  91. /// The scope ID of the address.
  92. /**
  93. * Modifies the scope ID associated with the IPv6 address.
  94. */
  95. void scope_id(unsigned long id)
  96. {
  97. scope_id_ = id;
  98. }
  99. /// Get the address in bytes.
  100. bytes_type to_bytes() const
  101. {
  102. using namespace std; // For memcpy.
  103. bytes_type bytes;
  104. memcpy(bytes.elems, addr_.s6_addr, 16);
  105. return bytes;
  106. }
  107. /// Get the address as a string.
  108. std::string to_string() const
  109. {
  110. boost::system::error_code ec;
  111. std::string addr = to_string(ec);
  112. boost::asio::detail::throw_error(ec);
  113. return addr;
  114. }
  115. /// Get the address as a string.
  116. std::string to_string(boost::system::error_code& ec) const
  117. {
  118. char addr_str[boost::asio::detail::max_addr_v6_str_len];
  119. const char* addr =
  120. boost::asio::detail::socket_ops::inet_ntop(AF_INET6, &addr_, addr_str,
  121. boost::asio::detail::max_addr_v6_str_len, scope_id_, ec);
  122. if (addr == 0)
  123. return std::string();
  124. return addr;
  125. }
  126. /// Create an address from an IP address string.
  127. static address_v6 from_string(const char* str)
  128. {
  129. boost::system::error_code ec;
  130. address_v6 addr = from_string(str, ec);
  131. boost::asio::detail::throw_error(ec);
  132. return addr;
  133. }
  134. /// Create an address from an IP address string.
  135. static address_v6 from_string(const char* str, boost::system::error_code& ec)
  136. {
  137. address_v6 tmp;
  138. if (boost::asio::detail::socket_ops::inet_pton(
  139. AF_INET6, str, &tmp.addr_, &tmp.scope_id_, ec) <= 0)
  140. return address_v6();
  141. return tmp;
  142. }
  143. /// Create an address from an IP address string.
  144. static address_v6 from_string(const std::string& str)
  145. {
  146. return from_string(str.c_str());
  147. }
  148. /// Create an address from an IP address string.
  149. static address_v6 from_string(const std::string& str,
  150. boost::system::error_code& ec)
  151. {
  152. return from_string(str.c_str(), ec);
  153. }
  154. /// Converts an IPv4-mapped or IPv4-compatible address to an IPv4 address.
  155. address_v4 to_v4() const
  156. {
  157. if (!is_v4_mapped() && !is_v4_compatible())
  158. {
  159. std::bad_cast ex;
  160. boost::throw_exception(ex);
  161. }
  162. address_v4::bytes_type v4_bytes = { { addr_.s6_addr[12],
  163. addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } };
  164. return address_v4(v4_bytes);
  165. }
  166. /// Determine whether the address is a loopback address.
  167. bool is_loopback() const
  168. {
  169. #if defined(__BORLANDC__)
  170. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  171. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  172. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  173. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  174. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  175. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  176. && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
  177. && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 1));
  178. #else
  179. using namespace boost::asio::detail;
  180. return IN6_IS_ADDR_LOOPBACK(&addr_) != 0;
  181. #endif
  182. }
  183. /// Determine whether the address is unspecified.
  184. bool is_unspecified() const
  185. {
  186. #if defined(__BORLANDC__)
  187. return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0)
  188. && (addr_.s6_addr[2] == 0) && (addr_.s6_addr[3] == 0)
  189. && (addr_.s6_addr[4] == 0) && (addr_.s6_addr[5] == 0)
  190. && (addr_.s6_addr[6] == 0) && (addr_.s6_addr[7] == 0)
  191. && (addr_.s6_addr[8] == 0) && (addr_.s6_addr[9] == 0)
  192. && (addr_.s6_addr[10] == 0) && (addr_.s6_addr[11] == 0)
  193. && (addr_.s6_addr[12] == 0) && (addr_.s6_addr[13] == 0)
  194. && (addr_.s6_addr[14] == 0) && (addr_.s6_addr[15] == 0));
  195. #else
  196. using namespace boost::asio::detail;
  197. return IN6_IS_ADDR_UNSPECIFIED(&addr_) != 0;
  198. #endif
  199. }
  200. /// Determine whether the address is link local.
  201. bool is_link_local() const
  202. {
  203. using namespace boost::asio::detail;
  204. return IN6_IS_ADDR_LINKLOCAL(&addr_) != 0;
  205. }
  206. /// Determine whether the address is site local.
  207. bool is_site_local() const
  208. {
  209. using namespace boost::asio::detail;
  210. return IN6_IS_ADDR_SITELOCAL(&addr_) != 0;
  211. }
  212. /// Determine whether the address is a mapped IPv4 address.
  213. bool is_v4_mapped() const
  214. {
  215. using namespace boost::asio::detail;
  216. return IN6_IS_ADDR_V4MAPPED(&addr_) != 0;
  217. }
  218. /// Determine whether the address is an IPv4-compatible address.
  219. bool is_v4_compatible() const
  220. {
  221. using namespace boost::asio::detail;
  222. return IN6_IS_ADDR_V4COMPAT(&addr_) != 0;
  223. }
  224. /// Determine whether the address is a multicast address.
  225. bool is_multicast() const
  226. {
  227. using namespace boost::asio::detail;
  228. return IN6_IS_ADDR_MULTICAST(&addr_) != 0;
  229. }
  230. /// Determine whether the address is a global multicast address.
  231. bool is_multicast_global() const
  232. {
  233. using namespace boost::asio::detail;
  234. return IN6_IS_ADDR_MC_GLOBAL(&addr_) != 0;
  235. }
  236. /// Determine whether the address is a link-local multicast address.
  237. bool is_multicast_link_local() const
  238. {
  239. using namespace boost::asio::detail;
  240. return IN6_IS_ADDR_MC_LINKLOCAL(&addr_) != 0;
  241. }
  242. /// Determine whether the address is a node-local multicast address.
  243. bool is_multicast_node_local() const
  244. {
  245. using namespace boost::asio::detail;
  246. return IN6_IS_ADDR_MC_NODELOCAL(&addr_) != 0;
  247. }
  248. /// Determine whether the address is a org-local multicast address.
  249. bool is_multicast_org_local() const
  250. {
  251. using namespace boost::asio::detail;
  252. return IN6_IS_ADDR_MC_ORGLOCAL(&addr_) != 0;
  253. }
  254. /// Determine whether the address is a site-local multicast address.
  255. bool is_multicast_site_local() const
  256. {
  257. using namespace boost::asio::detail;
  258. return IN6_IS_ADDR_MC_SITELOCAL(&addr_) != 0;
  259. }
  260. /// Compare two addresses for equality.
  261. friend bool operator==(const address_v6& a1, const address_v6& a2)
  262. {
  263. using namespace std; // For memcmp.
  264. return memcmp(&a1.addr_, &a2.addr_,
  265. sizeof(boost::asio::detail::in6_addr_type)) == 0
  266. && a1.scope_id_ == a2.scope_id_;
  267. }
  268. /// Compare two addresses for inequality.
  269. friend bool operator!=(const address_v6& a1, const address_v6& a2)
  270. {
  271. using namespace std; // For memcmp.
  272. return memcmp(&a1.addr_, &a2.addr_,
  273. sizeof(boost::asio::detail::in6_addr_type)) != 0
  274. || a1.scope_id_ != a2.scope_id_;
  275. }
  276. /// Compare addresses for ordering.
  277. friend bool operator<(const address_v6& a1, const address_v6& a2)
  278. {
  279. using namespace std; // For memcmp.
  280. int memcmp_result = memcmp(&a1.addr_, &a2.addr_,
  281. sizeof(boost::asio::detail::in6_addr_type));
  282. if (memcmp_result < 0)
  283. return true;
  284. if (memcmp_result > 0)
  285. return false;
  286. return a1.scope_id_ < a2.scope_id_;
  287. }
  288. /// Compare addresses for ordering.
  289. friend bool operator>(const address_v6& a1, const address_v6& a2)
  290. {
  291. return a2 < a1;
  292. }
  293. /// Compare addresses for ordering.
  294. friend bool operator<=(const address_v6& a1, const address_v6& a2)
  295. {
  296. return !(a2 < a1);
  297. }
  298. /// Compare addresses for ordering.
  299. friend bool operator>=(const address_v6& a1, const address_v6& a2)
  300. {
  301. return !(a1 < a2);
  302. }
  303. /// Obtain an address object that represents any address.
  304. static address_v6 any()
  305. {
  306. return address_v6();
  307. }
  308. /// Obtain an address object that represents the loopback address.
  309. static address_v6 loopback()
  310. {
  311. address_v6 tmp;
  312. boost::asio::detail::in6_addr_type tmp_addr = IN6ADDR_LOOPBACK_INIT;
  313. tmp.addr_ = tmp_addr;
  314. return tmp;
  315. }
  316. /// Create an IPv4-mapped IPv6 address.
  317. static address_v6 v4_mapped(const address_v4& addr)
  318. {
  319. address_v4::bytes_type v4_bytes = addr.to_bytes();
  320. bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
  321. v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  322. return address_v6(v6_bytes);
  323. }
  324. /// Create an IPv4-compatible IPv6 address.
  325. static address_v6 v4_compatible(const address_v4& addr)
  326. {
  327. address_v4::bytes_type v4_bytes = addr.to_bytes();
  328. bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  329. v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } };
  330. return address_v6(v6_bytes);
  331. }
  332. private:
  333. // The underlying IPv6 address.
  334. boost::asio::detail::in6_addr_type addr_;
  335. // The scope ID associated with the address.
  336. unsigned long scope_id_;
  337. };
  338. /// Output an address as a string.
  339. /**
  340. * Used to output a human-readable string for a specified address.
  341. *
  342. * @param os The output stream to which the string will be written.
  343. *
  344. * @param addr The address to be written.
  345. *
  346. * @return The output stream.
  347. *
  348. * @relates boost::asio::ip::address_v6
  349. */
  350. template <typename Elem, typename Traits>
  351. std::basic_ostream<Elem, Traits>& operator<<(
  352. std::basic_ostream<Elem, Traits>& os, const address_v6& addr)
  353. {
  354. boost::system::error_code ec;
  355. std::string s = addr.to_string(ec);
  356. if (ec)
  357. {
  358. if (os.exceptions() & std::ios::failbit)
  359. boost::asio::detail::throw_error(ec);
  360. else
  361. os.setstate(std::ios_base::failbit);
  362. }
  363. else
  364. for (std::string::iterator i = s.begin(); i != s.end(); ++i)
  365. os << os.widen(*i);
  366. return os;
  367. }
  368. } // namespace ip
  369. } // namespace asio
  370. } // namespace boost
  371. #include <boost/asio/detail/pop_options.hpp>
  372. #endif // BOOST_ASIO_IP_ADDRESS_V6_HPP