basic_resolver_results.hpp 9.2 KB

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