endpoint.ipp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // ip/detail/impl/endpoint.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_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()
  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)
  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)
  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. static_cast<boost::asio::detail::u_long_type>(
  77. addr.to_v4().to_ulong()));
  78. }
  79. else
  80. {
  81. data_.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
  82. data_.v6.sin6_port =
  83. boost::asio::detail::socket_ops::host_to_network_short(port_num);
  84. data_.v6.sin6_flowinfo = 0;
  85. boost::asio::ip::address_v6 v6_addr = addr.to_v6();
  86. boost::asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
  87. memcpy(data_.v6.sin6_addr.s6_addr, bytes.data(), 16);
  88. data_.v6.sin6_scope_id =
  89. static_cast<boost::asio::detail::u_long_type>(
  90. v6_addr.scope_id());
  91. }
  92. }
  93. void endpoint::resize(std::size_t new_size)
  94. {
  95. if (new_size > sizeof(boost::asio::detail::sockaddr_storage_type))
  96. {
  97. boost::system::error_code ec(boost::asio::error::invalid_argument);
  98. boost::asio::detail::throw_error(ec);
  99. }
  100. }
  101. unsigned short endpoint::port() const
  102. {
  103. if (is_v4())
  104. {
  105. return boost::asio::detail::socket_ops::network_to_host_short(
  106. data_.v4.sin_port);
  107. }
  108. else
  109. {
  110. return boost::asio::detail::socket_ops::network_to_host_short(
  111. data_.v6.sin6_port);
  112. }
  113. }
  114. void endpoint::port(unsigned short port_num)
  115. {
  116. if (is_v4())
  117. {
  118. data_.v4.sin_port
  119. = boost::asio::detail::socket_ops::host_to_network_short(port_num);
  120. }
  121. else
  122. {
  123. data_.v6.sin6_port
  124. = boost::asio::detail::socket_ops::host_to_network_short(port_num);
  125. }
  126. }
  127. boost::asio::ip::address endpoint::address() const
  128. {
  129. using namespace std; // For memcpy.
  130. if (is_v4())
  131. {
  132. return boost::asio::ip::address_v4(
  133. boost::asio::detail::socket_ops::network_to_host_long(
  134. data_.v4.sin_addr.s_addr));
  135. }
  136. else
  137. {
  138. boost::asio::ip::address_v6::bytes_type bytes;
  139. #if defined(BOOST_ASIO_HAS_STD_ARRAY)
  140. memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
  141. #else // defined(BOOST_ASIO_HAS_STD_ARRAY)
  142. memcpy(bytes.elems, data_.v6.sin6_addr.s6_addr, 16);
  143. #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
  144. return boost::asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
  145. }
  146. }
  147. void endpoint::address(const boost::asio::ip::address& addr)
  148. {
  149. endpoint tmp_endpoint(addr, port());
  150. data_ = tmp_endpoint.data_;
  151. }
  152. bool operator==(const endpoint& e1, const endpoint& e2)
  153. {
  154. return e1.address() == e2.address() && e1.port() == e2.port();
  155. }
  156. bool operator<(const endpoint& e1, const endpoint& e2)
  157. {
  158. if (e1.address() < e2.address())
  159. return true;
  160. if (e1.address() != e2.address())
  161. return false;
  162. return e1.port() < e2.port();
  163. }
  164. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  165. std::string endpoint::to_string(boost::system::error_code& ec) const
  166. {
  167. std::string a = address().to_string(ec);
  168. if (ec)
  169. return std::string();
  170. std::ostringstream tmp_os;
  171. tmp_os.imbue(std::locale::classic());
  172. if (is_v4())
  173. tmp_os << a;
  174. else
  175. tmp_os << '[' << a << ']';
  176. tmp_os << ':' << port();
  177. return tmp_os.str();
  178. }
  179. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  180. } // namespace detail
  181. } // namespace ip
  182. } // namespace asio
  183. } // namespace boost
  184. #include <boost/asio/detail/pop_options.hpp>
  185. #endif // BOOST_ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP