basic_resolver_iterator.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // ip/basic_resolver_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_ITERATOR_HPP
  11. #define ASIO_IP_BASIC_RESOLVER_ITERATOR_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 <iterator>
  19. #include <string>
  20. #include <vector>
  21. #include "asio/detail/shared_ptr.hpp"
  22. #include "asio/detail/socket_ops.hpp"
  23. #include "asio/detail/socket_types.hpp"
  24. #include "asio/ip/basic_resolver_entry.hpp"
  25. #if defined(ASIO_WINDOWS_RUNTIME)
  26. # include "asio/detail/winrt_utils.hpp"
  27. #endif // defined(ASIO_WINDOWS_RUNTIME)
  28. #include "asio/detail/push_options.hpp"
  29. namespace asio {
  30. namespace ip {
  31. /// An iterator over the entries produced by a resolver.
  32. /**
  33. * The asio::ip::basic_resolver_iterator class template is used to define
  34. * iterators over the results returned by a resolver.
  35. *
  36. * The iterator's value_type, obtained when the iterator is dereferenced, is:
  37. * @code const basic_resolver_entry<InternetProtocol> @endcode
  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_iterator
  45. {
  46. public:
  47. /// The type used for the distance between two iterators.
  48. typedef std::ptrdiff_t difference_type;
  49. /// The type of the value pointed to by the iterator.
  50. typedef basic_resolver_entry<InternetProtocol> value_type;
  51. /// The type of the result of applying operator->() to the iterator.
  52. typedef const basic_resolver_entry<InternetProtocol>* pointer;
  53. /// The type of the result of applying operator*() to the iterator.
  54. typedef const basic_resolver_entry<InternetProtocol>& reference;
  55. /// The iterator category.
  56. typedef std::forward_iterator_tag iterator_category;
  57. /// Default constructor creates an end iterator.
  58. basic_resolver_iterator()
  59. : index_(0)
  60. {
  61. }
  62. /// Create an iterator from an addrinfo list returned by getaddrinfo.
  63. static basic_resolver_iterator create(
  64. asio::detail::addrinfo_type* address_info,
  65. const std::string& host_name, const std::string& service_name)
  66. {
  67. basic_resolver_iterator iter;
  68. if (!address_info)
  69. return iter;
  70. std::string actual_host_name = host_name;
  71. if (address_info->ai_canonname)
  72. actual_host_name = address_info->ai_canonname;
  73. iter.values_.reset(new values_type);
  74. while (address_info)
  75. {
  76. if (address_info->ai_family == ASIO_OS_DEF(AF_INET)
  77. || address_info->ai_family == ASIO_OS_DEF(AF_INET6))
  78. {
  79. using namespace std; // For memcpy.
  80. typename InternetProtocol::endpoint endpoint;
  81. endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
  82. memcpy(endpoint.data(), address_info->ai_addr,
  83. address_info->ai_addrlen);
  84. iter.values_->push_back(
  85. basic_resolver_entry<InternetProtocol>(endpoint,
  86. actual_host_name, service_name));
  87. }
  88. address_info = address_info->ai_next;
  89. }
  90. return iter;
  91. }
  92. /// Create an iterator from an endpoint, host name and service name.
  93. static basic_resolver_iterator create(
  94. const typename InternetProtocol::endpoint& endpoint,
  95. const std::string& host_name, const std::string& service_name)
  96. {
  97. basic_resolver_iterator iter;
  98. iter.values_.reset(new values_type);
  99. iter.values_->push_back(
  100. basic_resolver_entry<InternetProtocol>(
  101. endpoint, host_name, service_name));
  102. return iter;
  103. }
  104. /// Create an iterator from a sequence of endpoints, host and service name.
  105. template <typename EndpointIterator>
  106. static basic_resolver_iterator create(
  107. EndpointIterator begin, EndpointIterator end,
  108. const std::string& host_name, const std::string& service_name)
  109. {
  110. basic_resolver_iterator iter;
  111. if (begin != end)
  112. {
  113. iter.values_.reset(new values_type);
  114. for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
  115. {
  116. iter.values_->push_back(
  117. basic_resolver_entry<InternetProtocol>(
  118. *ep_iter, host_name, service_name));
  119. }
  120. }
  121. return iter;
  122. }
  123. #if defined(ASIO_WINDOWS_RUNTIME)
  124. /// Create an iterator from a Windows Runtime list of EndpointPair objects.
  125. static basic_resolver_iterator create(
  126. Windows::Foundation::Collections::IVectorView<
  127. Windows::Networking::EndpointPair^>^ endpoints,
  128. const asio::detail::addrinfo_type& hints,
  129. const std::string& host_name, const std::string& service_name)
  130. {
  131. basic_resolver_iterator iter;
  132. if (endpoints->Size)
  133. {
  134. iter.values_.reset(new values_type);
  135. for (unsigned int i = 0; i < endpoints->Size; ++i)
  136. {
  137. auto pair = endpoints->GetAt(i);
  138. if (hints.ai_family == ASIO_OS_DEF(AF_INET)
  139. && pair->RemoteHostName->Type
  140. != Windows::Networking::HostNameType::Ipv4)
  141. continue;
  142. if (hints.ai_family == ASIO_OS_DEF(AF_INET6)
  143. && pair->RemoteHostName->Type
  144. != Windows::Networking::HostNameType::Ipv6)
  145. continue;
  146. iter.values_->push_back(
  147. basic_resolver_entry<InternetProtocol>(
  148. typename InternetProtocol::endpoint(
  149. ip::address::from_string(
  150. asio::detail::winrt_utils::string(
  151. pair->RemoteHostName->CanonicalName)),
  152. asio::detail::winrt_utils::integer(
  153. pair->RemoteServiceName)),
  154. host_name, service_name));
  155. }
  156. }
  157. return iter;
  158. }
  159. #endif // defined(ASIO_WINDOWS_RUNTIME)
  160. /// Dereference an iterator.
  161. const basic_resolver_entry<InternetProtocol>& operator*() const
  162. {
  163. return dereference();
  164. }
  165. /// Dereference an iterator.
  166. const basic_resolver_entry<InternetProtocol>* operator->() const
  167. {
  168. return &dereference();
  169. }
  170. /// Increment operator (prefix).
  171. basic_resolver_iterator& operator++()
  172. {
  173. increment();
  174. return *this;
  175. }
  176. /// Increment operator (postfix).
  177. basic_resolver_iterator operator++(int)
  178. {
  179. basic_resolver_iterator tmp(*this);
  180. ++*this;
  181. return tmp;
  182. }
  183. /// Test two iterators for equality.
  184. friend bool operator==(const basic_resolver_iterator& a,
  185. const basic_resolver_iterator& b)
  186. {
  187. return a.equal(b);
  188. }
  189. /// Test two iterators for inequality.
  190. friend bool operator!=(const basic_resolver_iterator& a,
  191. const basic_resolver_iterator& b)
  192. {
  193. return !a.equal(b);
  194. }
  195. private:
  196. void increment()
  197. {
  198. if (++index_ == values_->size())
  199. {
  200. // Reset state to match a default constructed end iterator.
  201. values_.reset();
  202. index_ = 0;
  203. }
  204. }
  205. bool equal(const basic_resolver_iterator& other) const
  206. {
  207. if (!values_ && !other.values_)
  208. return true;
  209. if (values_ != other.values_)
  210. return false;
  211. return index_ == other.index_;
  212. }
  213. const basic_resolver_entry<InternetProtocol>& dereference() const
  214. {
  215. return (*values_)[index_];
  216. }
  217. typedef std::vector<basic_resolver_entry<InternetProtocol> > values_type;
  218. asio::detail::shared_ptr<values_type> values_;
  219. std::size_t index_;
  220. };
  221. } // namespace ip
  222. } // namespace asio
  223. #include "asio/detail/pop_options.hpp"
  224. #endif // ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP