address.ipp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // ip/impl/address.ipp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_IPP
  11. #define BOOST_ASIO_IP_IMPL_ADDRESS_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 <typeinfo>
  17. #include <boost/asio/detail/throw_error.hpp>
  18. #include <boost/asio/detail/throw_exception.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/ip/address.hpp>
  21. #include <boost/asio/ip/bad_address_cast.hpp>
  22. #include <boost/system/system_error.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace ip {
  27. address::address()
  28. : type_(ipv4),
  29. ipv4_address_(),
  30. ipv6_address_()
  31. {
  32. }
  33. address::address(const boost::asio::ip::address_v4& ipv4_address)
  34. : type_(ipv4),
  35. ipv4_address_(ipv4_address),
  36. ipv6_address_()
  37. {
  38. }
  39. address::address(const boost::asio::ip::address_v6& ipv6_address)
  40. : type_(ipv6),
  41. ipv4_address_(),
  42. ipv6_address_(ipv6_address)
  43. {
  44. }
  45. address::address(const address& other)
  46. : type_(other.type_),
  47. ipv4_address_(other.ipv4_address_),
  48. ipv6_address_(other.ipv6_address_)
  49. {
  50. }
  51. #if defined(BOOST_ASIO_HAS_MOVE)
  52. address::address(address&& other)
  53. : type_(other.type_),
  54. ipv4_address_(other.ipv4_address_),
  55. ipv6_address_(other.ipv6_address_)
  56. {
  57. }
  58. #endif // defined(BOOST_ASIO_HAS_MOVE)
  59. address& address::operator=(const address& other)
  60. {
  61. type_ = other.type_;
  62. ipv4_address_ = other.ipv4_address_;
  63. ipv6_address_ = other.ipv6_address_;
  64. return *this;
  65. }
  66. #if defined(BOOST_ASIO_HAS_MOVE)
  67. address& address::operator=(address&& other)
  68. {
  69. type_ = other.type_;
  70. ipv4_address_ = other.ipv4_address_;
  71. ipv6_address_ = other.ipv6_address_;
  72. return *this;
  73. }
  74. #endif // defined(BOOST_ASIO_HAS_MOVE)
  75. address& address::operator=(const boost::asio::ip::address_v4& ipv4_address)
  76. {
  77. type_ = ipv4;
  78. ipv4_address_ = ipv4_address;
  79. ipv6_address_ = boost::asio::ip::address_v6();
  80. return *this;
  81. }
  82. address& address::operator=(const boost::asio::ip::address_v6& ipv6_address)
  83. {
  84. type_ = ipv6;
  85. ipv4_address_ = boost::asio::ip::address_v4();
  86. ipv6_address_ = ipv6_address;
  87. return *this;
  88. }
  89. address make_address(const char* str)
  90. {
  91. boost::system::error_code ec;
  92. address addr = make_address(str, ec);
  93. boost::asio::detail::throw_error(ec);
  94. return addr;
  95. }
  96. address make_address(const char* str, boost::system::error_code& ec)
  97. {
  98. boost::asio::ip::address_v6 ipv6_address =
  99. boost::asio::ip::make_address_v6(str, ec);
  100. if (!ec)
  101. return address(ipv6_address);
  102. boost::asio::ip::address_v4 ipv4_address =
  103. boost::asio::ip::make_address_v4(str, ec);
  104. if (!ec)
  105. return address(ipv4_address);
  106. return address();
  107. }
  108. address make_address(const std::string& str)
  109. {
  110. return make_address(str.c_str());
  111. }
  112. address make_address(const std::string& str,
  113. boost::system::error_code& ec)
  114. {
  115. return make_address(str.c_str(), ec);
  116. }
  117. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  118. address make_address(string_view str)
  119. {
  120. return make_address(static_cast<std::string>(str));
  121. }
  122. address make_address(string_view str,
  123. boost::system::error_code& ec)
  124. {
  125. return make_address(static_cast<std::string>(str), ec);
  126. }
  127. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  128. boost::asio::ip::address_v4 address::to_v4() const
  129. {
  130. if (type_ != ipv4)
  131. {
  132. bad_address_cast ex;
  133. boost::asio::detail::throw_exception(ex);
  134. }
  135. return ipv4_address_;
  136. }
  137. boost::asio::ip::address_v6 address::to_v6() const
  138. {
  139. if (type_ != ipv6)
  140. {
  141. bad_address_cast ex;
  142. boost::asio::detail::throw_exception(ex);
  143. }
  144. return ipv6_address_;
  145. }
  146. std::string address::to_string() const
  147. {
  148. if (type_ == ipv6)
  149. return ipv6_address_.to_string();
  150. return ipv4_address_.to_string();
  151. }
  152. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  153. std::string address::to_string(boost::system::error_code& ec) const
  154. {
  155. if (type_ == ipv6)
  156. return ipv6_address_.to_string(ec);
  157. return ipv4_address_.to_string(ec);
  158. }
  159. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  160. bool address::is_loopback() const
  161. {
  162. return (type_ == ipv4)
  163. ? ipv4_address_.is_loopback()
  164. : ipv6_address_.is_loopback();
  165. }
  166. bool address::is_unspecified() const
  167. {
  168. return (type_ == ipv4)
  169. ? ipv4_address_.is_unspecified()
  170. : ipv6_address_.is_unspecified();
  171. }
  172. bool address::is_multicast() const
  173. {
  174. return (type_ == ipv4)
  175. ? ipv4_address_.is_multicast()
  176. : ipv6_address_.is_multicast();
  177. }
  178. bool operator==(const address& a1, const address& a2)
  179. {
  180. if (a1.type_ != a2.type_)
  181. return false;
  182. if (a1.type_ == address::ipv6)
  183. return a1.ipv6_address_ == a2.ipv6_address_;
  184. return a1.ipv4_address_ == a2.ipv4_address_;
  185. }
  186. bool operator<(const address& a1, const address& a2)
  187. {
  188. if (a1.type_ < a2.type_)
  189. return true;
  190. if (a1.type_ > a2.type_)
  191. return false;
  192. if (a1.type_ == address::ipv6)
  193. return a1.ipv6_address_ < a2.ipv6_address_;
  194. return a1.ipv4_address_ < a2.ipv4_address_;
  195. }
  196. } // namespace ip
  197. } // namespace asio
  198. } // namespace boost
  199. #include <boost/asio/detail/pop_options.hpp>
  200. #endif // BOOST_ASIO_IP_IMPL_ADDRESS_IPP