endpoint.ipp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // ip/detail/impl/endpoint.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_DETAIL_IMPL_ENDPOINT_IPP
  11. #define BOOST_ASIO_IP_DETAIL_IMPL_ENDPOINT_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. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  18. # include <sstream>
  19. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  20. #include <boost/asio/detail/socket_ops.hpp>
  21. #include <boost/asio/detail/throw_error.hpp>
  22. #include <boost/asio/error.hpp>
  23. #include <boost/asio/ip/detail/endpoint.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace ip {
  28. namespace detail {
  29. endpoint::endpoint() BOOST_ASIO_NOEXCEPT
  30. : data_()
  31. {
  32. data_.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  33. data_.v4.sin_port = 0;
  34. data_.v4.sin_addr.s_addr = BOOST_ASIO_OS_DEF(INADDR_ANY);
  35. }
  36. endpoint::endpoint(int family, unsigned short port_num) BOOST_ASIO_NOEXCEPT
  37. : data_()
  38. {
  39. using namespace std; // For memcpy.
  40. if (family == BOOST_ASIO_OS_DEF(AF_INET))
  41. {
  42. data_.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  43. data_.v4.sin_port =
  44. boost::asio::detail::socket_ops::host_to_network_short(port_num);
  45. data_.v4.sin_addr.s_addr = BOOST_ASIO_OS_DEF(INADDR_ANY);
  46. }
  47. else
  48. {
  49. data_.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  50. data_.v6.sin6_port =
  51. boost::asio::detail::socket_ops::host_to_network_short(port_num);
  52. data_.v6.sin6_flowinfo = 0;
  53. data_.v6.sin6_addr.s6_addr[0] = 0; data_.v6.sin6_addr.s6_addr[1] = 0;
  54. data_.v6.sin6_addr.s6_addr[2] = 0; data_.v6.sin6_addr.s6_addr[3] = 0;
  55. data_.v6.sin6_addr.s6_addr[4] = 0; data_.v6.sin6_addr.s6_addr[5] = 0;
  56. data_.v6.sin6_addr.s6_addr[6] = 0; data_.v6.sin6_addr.s6_addr[7] = 0;
  57. data_.v6.sin6_addr.s6_addr[8] = 0; data_.v6.sin6_addr.s6_addr[9] = 0;
  58. data_.v6.sin6_addr.s6_addr[10] = 0; data_.v6.sin6_addr.s6_addr[11] = 0;
  59. data_.v6.sin6_addr.s6_addr[12] = 0; data_.v6.sin6_addr.s6_addr[13] = 0;
  60. data_.v6.sin6_addr.s6_addr[14] = 0; data_.v6.sin6_addr.s6_addr[15] = 0;
  61. data_.v6.sin6_scope_id = 0;
  62. }
  63. }
  64. endpoint::endpoint(const boost::asio::ip::address& addr,
  65. unsigned short port_num) BOOST_ASIO_NOEXCEPT
  66. : data_()
  67. {
  68. using namespace std; // For memcpy.
  69. if (addr.is_v4())
  70. {
  71. data_.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  72. data_.v4.sin_port =
  73. boost::asio::detail::socket_ops::host_to_network_short(port_num);
  74. data_.v4.sin_addr.s_addr =
  75. boost::asio::detail::socket_ops::host_to_network_long(
  76. addr.to_v4().to_uint());
  77. }
  78. else
  79. {
  80. data_.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  81. data_.v6.sin6_port =
  82. boost::asio::detail::socket_ops::host_to_network_short(port_num);
  83. data_.v6.sin6_flowinfo = 0;
  84. boost::asio::ip::address_v6 v6_addr = addr.to_v6();
  85. boost::asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
  86. memcpy(data_.v6.sin6_addr.s6_addr, bytes.data(), 16);
  87. data_.v6.sin6_scope_id =
  88. static_cast<boost::asio::detail::u_long_type>(
  89. v6_addr.scope_id());
  90. }
  91. }
  92. void endpoint::resize(std::size_t new_size)
  93. {
  94. if (new_size > sizeof(boost::asio::detail::sockaddr_storage_type))
  95. {
  96. boost::system::error_code ec(boost::asio::error::invalid_argument);
  97. boost::asio::detail::throw_error(ec);
  98. }
  99. }
  100. unsigned short endpoint::port() const BOOST_ASIO_NOEXCEPT
  101. {
  102. if (is_v4())
  103. {
  104. return boost::asio::detail::socket_ops::network_to_host_short(
  105. data_.v4.sin_port);
  106. }
  107. else
  108. {
  109. return boost::asio::detail::socket_ops::network_to_host_short(
  110. data_.v6.sin6_port);
  111. }
  112. }
  113. void endpoint::port(unsigned short port_num) BOOST_ASIO_NOEXCEPT
  114. {
  115. if (is_v4())
  116. {
  117. data_.v4.sin_port
  118. = boost::asio::detail::socket_ops::host_to_network_short(port_num);
  119. }
  120. else
  121. {
  122. data_.v6.sin6_port
  123. = boost::asio::detail::socket_ops::host_to_network_short(port_num);
  124. }
  125. }
  126. boost::asio::ip::address endpoint::address() const BOOST_ASIO_NOEXCEPT
  127. {
  128. using namespace std; // For memcpy.
  129. if (is_v4())
  130. {
  131. return boost::asio::ip::address_v4(
  132. boost::asio::detail::socket_ops::network_to_host_long(
  133. data_.v4.sin_addr.s_addr));
  134. }
  135. else
  136. {
  137. boost::asio::ip::address_v6::bytes_type bytes;
  138. #if defined(BOOST_ASIO_HAS_STD_ARRAY)
  139. memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
  140. #else // defined(BOOST_ASIO_HAS_STD_ARRAY)
  141. memcpy(bytes.elems, data_.v6.sin6_addr.s6_addr, 16);
  142. #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
  143. return boost::asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
  144. }
  145. }
  146. void endpoint::address(const boost::asio::ip::address& addr) BOOST_ASIO_NOEXCEPT
  147. {
  148. endpoint tmp_endpoint(addr, port());
  149. data_ = tmp_endpoint.data_;
  150. }
  151. bool operator==(const endpoint& e1, const endpoint& e2) BOOST_ASIO_NOEXCEPT
  152. {
  153. return e1.address() == e2.address() && e1.port() == e2.port();
  154. }
  155. bool operator<(const endpoint& e1, const endpoint& e2) BOOST_ASIO_NOEXCEPT
  156. {
  157. if (e1.address() < e2.address())
  158. return true;
  159. if (e1.address() != e2.address())
  160. return false;
  161. return e1.port() < e2.port();
  162. }
  163. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  164. std::string endpoint::to_string() const
  165. {
  166. std::ostringstream tmp_os;
  167. tmp_os.imbue(std::locale::classic());
  168. if (is_v4())
  169. tmp_os << address();
  170. else
  171. tmp_os << '[' << address() << ']';
  172. tmp_os << ':' << port();
  173. return tmp_os.str();
  174. }
  175. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  176. } // namespace detail
  177. } // namespace ip
  178. } // namespace asio
  179. } // namespace boost
  180. #include <boost/asio/detail/pop_options.hpp>
  181. #endif // BOOST_ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP