basic_resolver_results.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // ip/basic_resolver_results.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_RESOLVER_RESULTS_HPP
  11. #define BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_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/config.hpp>
  16. #include <cstddef>
  17. #include <cstring>
  18. #include <boost/asio/detail/socket_ops.hpp>
  19. #include <boost/asio/detail/socket_types.hpp>
  20. #include <boost/asio/ip/basic_resolver_iterator.hpp>
  21. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  22. # include <boost/asio/detail/winrt_utils.hpp>
  23. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace ip {
  28. /// A range of entries produced by a resolver.
  29. /**
  30. * The boost::asio::ip::basic_resolver_results class template is used to define
  31. * a range over the results returned by a resolver.
  32. *
  33. * The iterator's value_type, obtained when a results iterator is dereferenced,
  34. * is: @code const basic_resolver_entry<InternetProtocol> @endcode
  35. *
  36. * @note For backward compatibility, basic_resolver_results is derived from
  37. * basic_resolver_iterator. This derivation is deprecated.
  38. *
  39. * @par Thread Safety
  40. * @e Distinct @e objects: Safe.@n
  41. * @e Shared @e objects: Unsafe.
  42. */
  43. template <typename InternetProtocol>
  44. class basic_resolver_results
  45. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  46. : public basic_resolver_iterator<InternetProtocol>
  47. #else // !defined(BOOST_ASIO_NO_DEPRECATED)
  48. : private basic_resolver_iterator<InternetProtocol>
  49. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  50. {
  51. public:
  52. /// The protocol type associated with the results.
  53. typedef InternetProtocol protocol_type;
  54. /// The endpoint type associated with the results.
  55. typedef typename protocol_type::endpoint endpoint_type;
  56. /// The type of a value in the results range.
  57. typedef basic_resolver_entry<protocol_type> value_type;
  58. /// The type of a const reference to a value in the range.
  59. typedef const value_type& const_reference;
  60. /// The type of a non-const reference to a value in the range.
  61. typedef value_type& reference;
  62. /// The type of an iterator into the range.
  63. typedef basic_resolver_iterator<protocol_type> const_iterator;
  64. /// The type of an iterator into the range.
  65. typedef const_iterator iterator;
  66. /// Type used to represent the distance between two iterators in the range.
  67. typedef std::ptrdiff_t difference_type;
  68. /// Type used to represent a count of the elements in the range.
  69. typedef std::size_t size_type;
  70. /// Default constructor creates an empty range.
  71. basic_resolver_results()
  72. {
  73. }
  74. /// Copy constructor.
  75. basic_resolver_results(const basic_resolver_results& other)
  76. : basic_resolver_iterator<InternetProtocol>(other)
  77. {
  78. }
  79. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  80. /// Move constructor.
  81. basic_resolver_results(basic_resolver_results&& other)
  82. : basic_resolver_iterator<InternetProtocol>(
  83. BOOST_ASIO_MOVE_CAST(basic_resolver_results)(other))
  84. {
  85. }
  86. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  87. /// Assignment operator.
  88. basic_resolver_results& operator=(const basic_resolver_results& other)
  89. {
  90. basic_resolver_iterator<InternetProtocol>::operator=(other);
  91. return *this;
  92. }
  93. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  94. /// Move-assignment operator.
  95. basic_resolver_results& operator=(basic_resolver_results&& other)
  96. {
  97. basic_resolver_iterator<InternetProtocol>::operator=(
  98. BOOST_ASIO_MOVE_CAST(basic_resolver_results)(other));
  99. return *this;
  100. }
  101. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  102. #if !defined(GENERATING_DOCUMENTATION)
  103. // Create results from an addrinfo list returned by getaddrinfo.
  104. static basic_resolver_results create(
  105. boost::asio::detail::addrinfo_type* address_info,
  106. const std::string& host_name, const std::string& service_name)
  107. {
  108. basic_resolver_results results;
  109. if (!address_info)
  110. return results;
  111. std::string actual_host_name = host_name;
  112. if (address_info->ai_canonname)
  113. actual_host_name = address_info->ai_canonname;
  114. results.values_.reset(new values_type);
  115. while (address_info)
  116. {
  117. if (address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET)
  118. || address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET6))
  119. {
  120. using namespace std; // For memcpy.
  121. typename InternetProtocol::endpoint endpoint;
  122. endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
  123. memcpy(endpoint.data(), address_info->ai_addr,
  124. address_info->ai_addrlen);
  125. results.values_->push_back(
  126. basic_resolver_entry<InternetProtocol>(endpoint,
  127. actual_host_name, service_name));
  128. }
  129. address_info = address_info->ai_next;
  130. }
  131. return results;
  132. }
  133. // Create results from an endpoint, host name and service name.
  134. static basic_resolver_results create(const endpoint_type& endpoint,
  135. const std::string& host_name, const std::string& service_name)
  136. {
  137. basic_resolver_results results;
  138. results.values_.reset(new values_type);
  139. results.values_->push_back(
  140. basic_resolver_entry<InternetProtocol>(
  141. endpoint, host_name, service_name));
  142. return results;
  143. }
  144. // Create results from a sequence of endpoints, host and service name.
  145. template <typename EndpointIterator>
  146. static basic_resolver_results create(
  147. EndpointIterator begin, EndpointIterator end,
  148. const std::string& host_name, const std::string& service_name)
  149. {
  150. basic_resolver_results results;
  151. if (begin != end)
  152. {
  153. results.values_.reset(new values_type);
  154. for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
  155. {
  156. results.values_->push_back(
  157. basic_resolver_entry<InternetProtocol>(
  158. *ep_iter, host_name, service_name));
  159. }
  160. }
  161. return results;
  162. }
  163. # if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  164. // Create results from a Windows Runtime list of EndpointPair objects.
  165. static basic_resolver_results create(
  166. Windows::Foundation::Collections::IVectorView<
  167. Windows::Networking::EndpointPair^>^ endpoints,
  168. const boost::asio::detail::addrinfo_type& hints,
  169. const std::string& host_name, const std::string& service_name)
  170. {
  171. basic_resolver_results results;
  172. if (endpoints->Size)
  173. {
  174. results.values_.reset(new values_type);
  175. for (unsigned int i = 0; i < endpoints->Size; ++i)
  176. {
  177. auto pair = endpoints->GetAt(i);
  178. if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET)
  179. && pair->RemoteHostName->Type
  180. != Windows::Networking::HostNameType::Ipv4)
  181. continue;
  182. if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET6)
  183. && pair->RemoteHostName->Type
  184. != Windows::Networking::HostNameType::Ipv6)
  185. continue;
  186. results.values_->push_back(
  187. basic_resolver_entry<InternetProtocol>(
  188. typename InternetProtocol::endpoint(
  189. ip::make_address(
  190. boost::asio::detail::winrt_utils::string(
  191. pair->RemoteHostName->CanonicalName)),
  192. boost::asio::detail::winrt_utils::integer(
  193. pair->RemoteServiceName)),
  194. host_name, service_name));
  195. }
  196. }
  197. return results;
  198. }
  199. # endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  200. #endif // !defined(GENERATING_DOCUMENTATION)
  201. /// Get the number of entries in the results range.
  202. size_type size() const BOOST_ASIO_NOEXCEPT
  203. {
  204. return this->values_ ? this->values_->size() : 0;
  205. }
  206. /// Get the maximum number of entries permitted in a results range.
  207. size_type max_size() const BOOST_ASIO_NOEXCEPT
  208. {
  209. return this->values_ ? this->values_->max_size() : values_type().max_size();
  210. }
  211. /// Determine whether the results range is empty.
  212. bool empty() const BOOST_ASIO_NOEXCEPT
  213. {
  214. return this->values_ ? this->values_->empty() : true;
  215. }
  216. /// Obtain a begin iterator for the results range.
  217. const_iterator begin() const
  218. {
  219. basic_resolver_results tmp(*this);
  220. tmp.index_ = 0;
  221. return BOOST_ASIO_MOVE_CAST(basic_resolver_results)(tmp);
  222. }
  223. /// Obtain an end iterator for the results range.
  224. const_iterator end() const
  225. {
  226. return const_iterator();
  227. }
  228. /// Obtain a begin iterator for the results range.
  229. const_iterator cbegin() const
  230. {
  231. return begin();
  232. }
  233. /// Obtain an end iterator for the results range.
  234. const_iterator cend() const
  235. {
  236. return end();
  237. }
  238. /// Swap the results range with another.
  239. void swap(basic_resolver_results& that) BOOST_ASIO_NOEXCEPT
  240. {
  241. if (this != &that)
  242. {
  243. this->values_.swap(that.values_);
  244. std::size_t index = this->index_;
  245. this->index_ = that.index_;
  246. that.index_ = index;
  247. }
  248. }
  249. /// Test two iterators for equality.
  250. friend bool operator==(const basic_resolver_results& a,
  251. const basic_resolver_results& b)
  252. {
  253. return a.equal(b);
  254. }
  255. /// Test two iterators for inequality.
  256. friend bool operator!=(const basic_resolver_results& a,
  257. const basic_resolver_results& b)
  258. {
  259. return !a.equal(b);
  260. }
  261. private:
  262. typedef std::vector<basic_resolver_entry<InternetProtocol> > values_type;
  263. };
  264. } // namespace ip
  265. } // namespace asio
  266. } // namespace boost
  267. #include <boost/asio/detail/pop_options.hpp>
  268. #endif // BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP