address.ipp 4.9 KB

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