basic_resolver_iterator.hpp 7.6 KB

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