basic_resolver_results.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // ip/basic_resolver_results.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. /// Move constructor.
  80. basic_resolver_results(basic_resolver_results&& other)
  81. : basic_resolver_iterator<InternetProtocol>(
  82. static_cast<basic_resolver_results&&>(other))
  83. {
  84. }
  85. /// Assignment operator.
  86. basic_resolver_results& operator=(const basic_resolver_results& other)
  87. {
  88. basic_resolver_iterator<InternetProtocol>::operator=(other);
  89. return *this;
  90. }
  91. /// Move-assignment operator.
  92. basic_resolver_results& operator=(basic_resolver_results&& other)
  93. {
  94. basic_resolver_iterator<InternetProtocol>::operator=(
  95. static_cast<basic_resolver_results&&>(other));
  96. return *this;
  97. }
  98. #if !defined(GENERATING_DOCUMENTATION)
  99. // Create results from an addrinfo list returned by getaddrinfo.
  100. static basic_resolver_results create(
  101. boost::asio::detail::addrinfo_type* address_info,
  102. const std::string& host_name, const std::string& service_name)
  103. {
  104. basic_resolver_results results;
  105. if (!address_info)
  106. return results;
  107. std::string actual_host_name = host_name;
  108. if (address_info->ai_canonname)
  109. actual_host_name = address_info->ai_canonname;
  110. results.values_.reset(new values_type);
  111. while (address_info)
  112. {
  113. if (address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET)
  114. || address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET6))
  115. {
  116. using namespace std; // For memcpy.
  117. typename InternetProtocol::endpoint endpoint;
  118. endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
  119. memcpy(endpoint.data(), address_info->ai_addr,
  120. address_info->ai_addrlen);
  121. results.values_->push_back(
  122. basic_resolver_entry<InternetProtocol>(endpoint,
  123. actual_host_name, service_name));
  124. }
  125. address_info = address_info->ai_next;
  126. }
  127. return results;
  128. }
  129. // Create results from an endpoint, host name and service name.
  130. static basic_resolver_results create(const endpoint_type& endpoint,
  131. const std::string& host_name, const std::string& service_name)
  132. {
  133. basic_resolver_results results;
  134. results.values_.reset(new values_type);
  135. results.values_->push_back(
  136. basic_resolver_entry<InternetProtocol>(
  137. endpoint, host_name, service_name));
  138. return results;
  139. }
  140. // Create results from a sequence of endpoints, host and service name.
  141. template <typename EndpointIterator>
  142. static basic_resolver_results create(
  143. EndpointIterator begin, EndpointIterator end,
  144. const std::string& host_name, const std::string& service_name)
  145. {
  146. basic_resolver_results results;
  147. if (begin != end)
  148. {
  149. results.values_.reset(new values_type);
  150. for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
  151. {
  152. results.values_->push_back(
  153. basic_resolver_entry<InternetProtocol>(
  154. *ep_iter, host_name, service_name));
  155. }
  156. }
  157. return results;
  158. }
  159. # if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  160. // Create results from a Windows Runtime list of EndpointPair objects.
  161. static basic_resolver_results create(
  162. Windows::Foundation::Collections::IVectorView<
  163. Windows::Networking::EndpointPair^>^ endpoints,
  164. const boost::asio::detail::addrinfo_type& hints,
  165. const std::string& host_name, const std::string& service_name)
  166. {
  167. basic_resolver_results results;
  168. if (endpoints->Size)
  169. {
  170. results.values_.reset(new values_type);
  171. for (unsigned int i = 0; i < endpoints->Size; ++i)
  172. {
  173. auto pair = endpoints->GetAt(i);
  174. if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET)
  175. && pair->RemoteHostName->Type
  176. != Windows::Networking::HostNameType::Ipv4)
  177. continue;
  178. if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET6)
  179. && pair->RemoteHostName->Type
  180. != Windows::Networking::HostNameType::Ipv6)
  181. continue;
  182. results.values_->push_back(
  183. basic_resolver_entry<InternetProtocol>(
  184. typename InternetProtocol::endpoint(
  185. ip::make_address(
  186. boost::asio::detail::winrt_utils::string(
  187. pair->RemoteHostName->CanonicalName)),
  188. boost::asio::detail::winrt_utils::integer(
  189. pair->RemoteServiceName)),
  190. host_name, service_name));
  191. }
  192. }
  193. return results;
  194. }
  195. # endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  196. #endif // !defined(GENERATING_DOCUMENTATION)
  197. /// Get the number of entries in the results range.
  198. size_type size() const noexcept
  199. {
  200. return this->values_ ? this->values_->size() : 0;
  201. }
  202. /// Get the maximum number of entries permitted in a results range.
  203. size_type max_size() const noexcept
  204. {
  205. return this->values_ ? this->values_->max_size() : values_type().max_size();
  206. }
  207. /// Determine whether the results range is empty.
  208. bool empty() const noexcept
  209. {
  210. return this->values_ ? this->values_->empty() : true;
  211. }
  212. /// Obtain a begin iterator for the results range.
  213. const_iterator begin() const
  214. {
  215. basic_resolver_results tmp(*this);
  216. tmp.index_ = 0;
  217. return static_cast<basic_resolver_results&&>(tmp);
  218. }
  219. /// Obtain an end iterator for the results range.
  220. const_iterator end() const
  221. {
  222. return const_iterator();
  223. }
  224. /// Obtain a begin iterator for the results range.
  225. const_iterator cbegin() const
  226. {
  227. return begin();
  228. }
  229. /// Obtain an end iterator for the results range.
  230. const_iterator cend() const
  231. {
  232. return end();
  233. }
  234. /// Swap the results range with another.
  235. void swap(basic_resolver_results& that) noexcept
  236. {
  237. if (this != &that)
  238. {
  239. this->values_.swap(that.values_);
  240. std::size_t index = this->index_;
  241. this->index_ = that.index_;
  242. that.index_ = index;
  243. }
  244. }
  245. /// Test two iterators for equality.
  246. friend bool operator==(const basic_resolver_results& a,
  247. const basic_resolver_results& b)
  248. {
  249. return a.equal(b);
  250. }
  251. /// Test two iterators for inequality.
  252. friend bool operator!=(const basic_resolver_results& a,
  253. const basic_resolver_results& b)
  254. {
  255. return !a.equal(b);
  256. }
  257. private:
  258. typedef std::vector<basic_resolver_entry<InternetProtocol>> values_type;
  259. };
  260. } // namespace ip
  261. } // namespace asio
  262. } // namespace boost
  263. #include <boost/asio/detail/pop_options.hpp>
  264. #endif // BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP