endpoint.ipp 5.3 KB

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