/Src/Dependencies/Boost/boost/asio/ip/detail/impl/endpoint.ipp

http://hadesmem.googlecode.com/ · C++ Header · 203 lines · 173 code · 21 blank · 9 comment · 16 complexity · 784749798229f05372c53a62c3441a40 MD5 · raw file

  1. //
  2. // ip/detail/impl/endpoint.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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_NO_IOSTREAM)
  18. # include <sstream>
  19. #endif // !defined(BOOST_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 = AF_INET;
  33. data_.v4.sin_port = 0;
  34. data_.v4.sin_addr.s_addr = INADDR_ANY;
  35. }
  36. endpoint::endpoint(int family, unsigned short port_num)
  37. : data_()
  38. {
  39. using namespace std; // For memcpy.
  40. if (family == PF_INET)
  41. {
  42. data_.v4.sin_family = 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 = INADDR_ANY;
  46. }
  47. else
  48. {
  49. data_.v6.sin6_family = 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 = 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_ulong());
  77. }
  78. else
  79. {
  80. data_.v6.sin6_family = 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 = v6_addr.scope_id();
  88. }
  89. }
  90. void endpoint::resize(std::size_t new_size)
  91. {
  92. if (new_size > sizeof(boost::asio::detail::sockaddr_storage_type))
  93. {
  94. boost::system::error_code ec(boost::asio::error::invalid_argument);
  95. boost::asio::detail::throw_error(ec);
  96. }
  97. }
  98. unsigned short endpoint::port() const
  99. {
  100. if (is_v4())
  101. {
  102. return boost::asio::detail::socket_ops::network_to_host_short(
  103. data_.v4.sin_port);
  104. }
  105. else
  106. {
  107. return boost::asio::detail::socket_ops::network_to_host_short(
  108. data_.v6.sin6_port);
  109. }
  110. }
  111. void endpoint::port(unsigned short port_num)
  112. {
  113. if (is_v4())
  114. {
  115. data_.v4.sin_port
  116. = boost::asio::detail::socket_ops::host_to_network_short(port_num);
  117. }
  118. else
  119. {
  120. data_.v6.sin6_port
  121. = boost::asio::detail::socket_ops::host_to_network_short(port_num);
  122. }
  123. }
  124. boost::asio::ip::address endpoint::address() const
  125. {
  126. using namespace std; // For memcpy.
  127. if (is_v4())
  128. {
  129. return boost::asio::ip::address_v4(
  130. boost::asio::detail::socket_ops::network_to_host_long(
  131. data_.v4.sin_addr.s_addr));
  132. }
  133. else
  134. {
  135. boost::asio::ip::address_v6::bytes_type bytes;
  136. #if defined(BOOST_ASIO_HAS_STD_ARRAY)
  137. memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
  138. #else // defined(BOOST_ASIO_HAS_STD_ARRAY)
  139. memcpy(bytes.elems, data_.v6.sin6_addr.s6_addr, 16);
  140. #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
  141. return boost::asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
  142. }
  143. }
  144. void endpoint::address(const boost::asio::ip::address& addr)
  145. {
  146. endpoint tmp_endpoint(addr, port());
  147. data_ = tmp_endpoint.data_;
  148. }
  149. bool operator==(const endpoint& e1, const endpoint& e2)
  150. {
  151. return e1.address() == e2.address() && e1.port() == e2.port();
  152. }
  153. bool operator<(const endpoint& e1, const endpoint& e2)
  154. {
  155. if (e1.address() < e2.address())
  156. return true;
  157. if (e1.address() != e2.address())
  158. return false;
  159. return e1.port() < e2.port();
  160. }
  161. #if !defined(BOOST_NO_IOSTREAM)
  162. std::string endpoint::to_string(boost::system::error_code& ec) const
  163. {
  164. std::string a = address().to_string(ec);
  165. if (ec)
  166. return std::string();
  167. std::ostringstream tmp_os;
  168. tmp_os.imbue(std::locale::classic());
  169. if (is_v4())
  170. tmp_os << a;
  171. else
  172. tmp_os << '[' << a << ']';
  173. tmp_os << ':' << port();
  174. return tmp_os.str();
  175. }
  176. #endif // !defined(BOOST_NO_IOSTREAM)
  177. } // namespace detail
  178. } // namespace ip
  179. } // namespace asio
  180. } // namespace boost
  181. #include <boost/asio/detail/pop_options.hpp>
  182. #endif // BOOST_ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP