basic_endpoint.hpp 10 KB

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