basic_endpoint.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // basic_endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_BASIC_ENDPOINT_HPP
  11. #define ASIO_IP_BASIC_ENDPOINT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include <boost/detail/workaround.hpp>
  20. #include <cstring>
  21. #if !defined(BOOST_NO_IOSTREAM)
  22. # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  23. # include <ostream>
  24. # endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  25. # include <sstream>
  26. #endif // !defined(BOOST_NO_IOSTREAM)
  27. #include "asio/detail/pop_options.hpp"
  28. #include "asio/error.hpp"
  29. #include "asio/ip/address.hpp"
  30. #include "asio/detail/socket_ops.hpp"
  31. #include "asio/detail/socket_types.hpp"
  32. namespace asio {
  33. namespace ip {
  34. /// Describes an endpoint for a version-independent IP socket.
  35. /**
  36. * The asio::ip::basic_endpoint class template describes an endpoint that
  37. * may be associated with a particular socket.
  38. *
  39. * @par Thread Safety
  40. * @e Distinct @e objects: Safe.@n
  41. * @e Shared @e objects: Unsafe.
  42. *
  43. * @par Concepts:
  44. * Endpoint.
  45. */
  46. template <typename InternetProtocol>
  47. class basic_endpoint
  48. {
  49. public:
  50. /// The protocol type associated with the endpoint.
  51. typedef InternetProtocol protocol_type;
  52. /// The type of the endpoint structure. This type is dependent on the
  53. /// underlying implementation of the socket layer.
  54. #if defined(GENERATING_DOCUMENTATION)
  55. typedef implementation_defined data_type;
  56. #else
  57. typedef asio::detail::socket_addr_type data_type;
  58. #endif
  59. /// Default constructor.
  60. basic_endpoint()
  61. : data_()
  62. {
  63. data_.v4.sin_family = AF_INET;
  64. data_.v4.sin_port = 0;
  65. data_.v4.sin_addr.s_addr = INADDR_ANY;
  66. }
  67. /// Construct an endpoint using a port number, specified in the host's byte
  68. /// order. The IP address will be the any address (i.e. INADDR_ANY or
  69. /// in6addr_any). This constructor would typically be used for accepting new
  70. /// connections.
  71. /**
  72. * @par Examples
  73. * To initialise an IPv4 TCP endpoint for port 1234, use:
  74. * @code
  75. * asio::ip::tcp::endpoint ep(asio::ip::tcp::v4(), 1234);
  76. * @endcode
  77. *
  78. * To specify an IPv6 UDP endpoint for port 9876, use:
  79. * @code
  80. * asio::ip::udp::endpoint ep(asio::ip::udp::v6(), 9876);
  81. * @endcode
  82. */
  83. basic_endpoint(const InternetProtocol& protocol, unsigned short port_num)
  84. : data_()
  85. {
  86. using namespace std; // For memcpy.
  87. if (protocol.family() == PF_INET)
  88. {
  89. data_.v4.sin_family = AF_INET;
  90. data_.v4.sin_port =
  91. asio::detail::socket_ops::host_to_network_short(port_num);
  92. data_.v4.sin_addr.s_addr = INADDR_ANY;
  93. }
  94. else
  95. {
  96. data_.v6.sin6_family = AF_INET6;
  97. data_.v6.sin6_port =
  98. asio::detail::socket_ops::host_to_network_short(port_num);
  99. data_.v6.sin6_flowinfo = 0;
  100. asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
  101. data_.v6.sin6_addr = tmp_addr;
  102. data_.v6.sin6_scope_id = 0;
  103. }
  104. }
  105. /// Construct an endpoint using a port number and an IP address. This
  106. /// constructor may be used for accepting connections on a specific interface
  107. /// or for making a connection to a remote endpoint.
  108. basic_endpoint(const asio::ip::address& addr, unsigned short port_num)
  109. : data_()
  110. {
  111. using namespace std; // For memcpy.
  112. if (addr.is_v4())
  113. {
  114. data_.v4.sin_family = AF_INET;
  115. data_.v4.sin_port =
  116. asio::detail::socket_ops::host_to_network_short(port_num);
  117. data_.v4.sin_addr.s_addr =
  118. asio::detail::socket_ops::host_to_network_long(
  119. addr.to_v4().to_ulong());
  120. }
  121. else
  122. {
  123. data_.v6.sin6_family = AF_INET6;
  124. data_.v6.sin6_port =
  125. asio::detail::socket_ops::host_to_network_short(port_num);
  126. data_.v6.sin6_flowinfo = 0;
  127. asio::ip::address_v6 v6_addr = addr.to_v6();
  128. asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
  129. memcpy(data_.v6.sin6_addr.s6_addr, bytes.elems, 16);
  130. data_.v6.sin6_scope_id = v6_addr.scope_id();
  131. }
  132. }
  133. /// Copy constructor.
  134. basic_endpoint(const basic_endpoint& other)
  135. : data_(other.data_)
  136. {
  137. }
  138. /// Assign from another endpoint.
  139. basic_endpoint& operator=(const basic_endpoint& other)
  140. {
  141. data_ = other.data_;
  142. return *this;
  143. }
  144. /// The protocol associated with the endpoint.
  145. protocol_type protocol() const
  146. {
  147. if (is_v4())
  148. return InternetProtocol::v4();
  149. return InternetProtocol::v6();
  150. }
  151. /// Get the underlying endpoint in the native type.
  152. data_type* data()
  153. {
  154. return &data_.base;
  155. }
  156. /// Get the underlying endpoint in the native type.
  157. const data_type* data() const
  158. {
  159. return &data_.base;
  160. }
  161. /// Get the underlying size of the endpoint in the native type.
  162. std::size_t size() const
  163. {
  164. if (is_v4())
  165. return sizeof(asio::detail::sockaddr_in4_type);
  166. else
  167. return sizeof(asio::detail::sockaddr_in6_type);
  168. }
  169. /// Set the underlying size of the endpoint in the native type.
  170. void resize(std::size_t size)
  171. {
  172. if (size > sizeof(asio::detail::sockaddr_storage_type))
  173. {
  174. asio::system_error e(asio::error::invalid_argument);
  175. boost::throw_exception(e);
  176. }
  177. }
  178. /// Get the capacity of the endpoint in the native type.
  179. std::size_t capacity() const
  180. {
  181. return sizeof(asio::detail::sockaddr_storage_type);
  182. }
  183. /// Get the port associated with the endpoint. The port number is always in
  184. /// the host's byte order.
  185. unsigned short port() const
  186. {
  187. if (is_v4())
  188. {
  189. return asio::detail::socket_ops::network_to_host_short(
  190. data_.v4.sin_port);
  191. }
  192. else
  193. {
  194. return asio::detail::socket_ops::network_to_host_short(
  195. data_.v6.sin6_port);
  196. }
  197. }
  198. /// Set the port associated with the endpoint. The port number is always in
  199. /// the host's byte order.
  200. void port(unsigned short port_num)
  201. {
  202. if (is_v4())
  203. {
  204. data_.v4.sin_port
  205. = asio::detail::socket_ops::host_to_network_short(port_num);
  206. }
  207. else
  208. {
  209. data_.v6.sin6_port
  210. = asio::detail::socket_ops::host_to_network_short(port_num);
  211. }
  212. }
  213. /// Get the IP address associated with the endpoint.
  214. asio::ip::address address() const
  215. {
  216. using namespace std; // For memcpy.
  217. if (is_v4())
  218. {
  219. return asio::ip::address_v4(
  220. asio::detail::socket_ops::network_to_host_long(
  221. data_.v4.sin_addr.s_addr));
  222. }
  223. else
  224. {
  225. asio::ip::address_v6::bytes_type bytes;
  226. memcpy(bytes.elems, data_.v6.sin6_addr.s6_addr, 16);
  227. return asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
  228. }
  229. }
  230. /// Set the IP address associated with the endpoint.
  231. void address(const asio::ip::address& addr)
  232. {
  233. basic_endpoint<InternetProtocol> tmp_endpoint(addr, port());
  234. data_ = tmp_endpoint.data_;
  235. }
  236. /// Compare two endpoints for equality.
  237. friend bool operator==(const basic_endpoint<InternetProtocol>& e1,
  238. const basic_endpoint<InternetProtocol>& e2)
  239. {
  240. return e1.address() == e2.address() && e1.port() == e2.port();
  241. }
  242. /// Compare two endpoints for inequality.
  243. friend bool operator!=(const basic_endpoint<InternetProtocol>& e1,
  244. const basic_endpoint<InternetProtocol>& e2)
  245. {
  246. return e1.address() != e2.address() || e1.port() != e2.port();
  247. }
  248. /// Compare endpoints for ordering.
  249. friend bool operator<(const basic_endpoint<InternetProtocol>& e1,
  250. const basic_endpoint<InternetProtocol>& e2)
  251. {
  252. if (e1.address() < e2.address())
  253. return true;
  254. if (e1.address() != e2.address())
  255. return false;
  256. return e1.port() < e2.port();
  257. }
  258. private:
  259. // Helper function to determine whether the endpoint is IPv4.
  260. bool is_v4() const
  261. {
  262. return data_.base.sa_family == AF_INET;
  263. }
  264. // The underlying IP socket address.
  265. union data_union
  266. {
  267. asio::detail::socket_addr_type base;
  268. asio::detail::sockaddr_storage_type storage;
  269. asio::detail::sockaddr_in4_type v4;
  270. asio::detail::sockaddr_in6_type v6;
  271. } data_;
  272. };
  273. #if !defined(BOOST_NO_IOSTREAM)
  274. /// Output an endpoint as a string.
  275. /**
  276. * Used to output a human-readable string for a specified endpoint.
  277. *
  278. * @param os The output stream to which the string will be written.
  279. *
  280. * @param endpoint The endpoint to be written.
  281. *
  282. * @return The output stream.
  283. *
  284. * @relates asio::ip::basic_endpoint
  285. */
  286. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  287. template <typename InternetProtocol>
  288. std::ostream& operator<<(std::ostream& os,
  289. const basic_endpoint<InternetProtocol>& endpoint)
  290. {
  291. const address& addr = endpoint.address();
  292. asio::error_code ec;
  293. std::string a = addr.to_string(ec);
  294. if (ec)
  295. {
  296. if (os.exceptions() & std::ios::failbit)
  297. asio::detail::throw_error(ec);
  298. else
  299. os.setstate(std::ios_base::failbit);
  300. }
  301. else
  302. {
  303. std::ostringstream tmp_os;
  304. tmp_os.imbue(std::locale::classic());
  305. if (addr.is_v4())
  306. tmp_os << a;
  307. else
  308. tmp_os << '[' << a << ']';
  309. tmp_os << ':' << endpoint.port();
  310. os << tmp_os.str();
  311. }
  312. return os;
  313. }
  314. #else // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  315. template <typename Elem, typename Traits, typename InternetProtocol>
  316. std::basic_ostream<Elem, Traits>& operator<<(
  317. std::basic_ostream<Elem, Traits>& os,
  318. const basic_endpoint<InternetProtocol>& endpoint)
  319. {
  320. const address& addr = endpoint.address();
  321. asio::error_code ec;
  322. std::string a = addr.to_string(ec);
  323. if (ec)
  324. {
  325. if (os.exceptions() & std::ios::failbit)
  326. asio::detail::throw_error(ec);
  327. else
  328. os.setstate(std::ios_base::failbit);
  329. }
  330. else
  331. {
  332. std::ostringstream tmp_os;
  333. tmp_os.imbue(std::locale::classic());
  334. if (addr.is_v4())
  335. tmp_os << a;
  336. else
  337. tmp_os << '[' << a << ']';
  338. tmp_os << ':' << endpoint.port();
  339. os << tmp_os.str();
  340. }
  341. return os;
  342. }
  343. #endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  344. #endif // !defined(BOOST_NO_IOSTREAM)
  345. } // namespace ip
  346. } // namespace asio
  347. #include "asio/detail/pop_options.hpp"
  348. #endif // ASIO_IP_BASIC_ENDPOINT_HPP