basic_resolver_results.hpp 9.0 KB

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