2 // ip/basic_resolver_iterator.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP
12 #define BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
24 #include <boost/asio/detail/shared_ptr.hpp>
25 #include <boost/asio/detail/socket_ops.hpp>
26 #include <boost/asio/detail/socket_types.hpp>
27 #include <boost/asio/ip/basic_resolver_entry.hpp>
29 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
30 # include <boost/asio/detail/winrt_utils.hpp>
31 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
33 #include <boost/asio/detail/push_options.hpp>
39 /// An iterator over the entries produced by a resolver.
41 * The boost::asio::ip::basic_resolver_iterator class template is used to define
42 * iterators over the results returned by a resolver.
44 * The iterator's value_type, obtained when the iterator is dereferenced, is:
45 * @code const basic_resolver_entry<InternetProtocol> @endcode
48 * @e Distinct @e objects: Safe.@n
49 * @e Shared @e objects: Unsafe.
51 template <typename InternetProtocol>
52 class basic_resolver_iterator
55 /// The type used for the distance between two iterators.
56 typedef std::ptrdiff_t difference_type;
58 /// The type of the value pointed to by the iterator.
59 typedef basic_resolver_entry<InternetProtocol> value_type;
61 /// The type of the result of applying operator->() to the iterator.
62 typedef const basic_resolver_entry<InternetProtocol>* pointer;
64 /// The type of the result of applying operator*() to the iterator.
65 typedef const basic_resolver_entry<InternetProtocol>& reference;
67 /// The iterator category.
68 typedef std::forward_iterator_tag iterator_category;
70 /// Default constructor creates an end iterator.
71 basic_resolver_iterator()
76 /// Create an iterator from an addrinfo list returned by getaddrinfo.
77 static basic_resolver_iterator create(
78 boost::asio::detail::addrinfo_type* address_info,
79 const std::string& host_name, const std::string& service_name)
81 basic_resolver_iterator iter;
85 std::string actual_host_name = host_name;
86 if (address_info->ai_canonname)
87 actual_host_name = address_info->ai_canonname;
89 iter.values_.reset(new values_type);
93 if (address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET)
94 || address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET6))
96 using namespace std; // For memcpy.
97 typename InternetProtocol::endpoint endpoint;
98 endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
99 memcpy(endpoint.data(), address_info->ai_addr,
100 address_info->ai_addrlen);
101 iter.values_->push_back(
102 basic_resolver_entry<InternetProtocol>(endpoint,
103 actual_host_name, service_name));
105 address_info = address_info->ai_next;
111 /// Create an iterator from an endpoint, host name and service name.
112 static basic_resolver_iterator create(
113 const typename InternetProtocol::endpoint& endpoint,
114 const std::string& host_name, const std::string& service_name)
116 basic_resolver_iterator iter;
117 iter.values_.reset(new values_type);
118 iter.values_->push_back(
119 basic_resolver_entry<InternetProtocol>(
120 endpoint, host_name, service_name));
124 /// Create an iterator from a sequence of endpoints, host and service name.
125 template <typename EndpointIterator>
126 static basic_resolver_iterator create(
127 EndpointIterator begin, EndpointIterator end,
128 const std::string& host_name, const std::string& service_name)
130 basic_resolver_iterator iter;
133 iter.values_.reset(new values_type);
134 for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
136 iter.values_->push_back(
137 basic_resolver_entry<InternetProtocol>(
138 *ep_iter, host_name, service_name));
144 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
145 /// Create an iterator from a Windows Runtime list of EndpointPair objects.
146 static basic_resolver_iterator create(
147 Windows::Foundation::Collections::IVectorView<
148 Windows::Networking::EndpointPair^>^ endpoints,
149 const boost::asio::detail::addrinfo_type& hints,
150 const std::string& host_name, const std::string& service_name)
152 basic_resolver_iterator iter;
155 iter.values_.reset(new values_type);
156 for (unsigned int i = 0; i < endpoints->Size; ++i)
158 auto pair = endpoints->GetAt(i);
160 if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET)
161 && pair->RemoteHostName->Type
162 != Windows::Networking::HostNameType::Ipv4)
165 if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET6)
166 && pair->RemoteHostName->Type
167 != Windows::Networking::HostNameType::Ipv6)
170 iter.values_->push_back(
171 basic_resolver_entry<InternetProtocol>(
172 typename InternetProtocol::endpoint(
173 ip::address::from_string(
174 boost::asio::detail::winrt_utils::string(
175 pair->RemoteHostName->CanonicalName)),
176 boost::asio::detail::winrt_utils::integer(
177 pair->RemoteServiceName)),
178 host_name, service_name));
183 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
185 /// Dereference an iterator.
186 const basic_resolver_entry<InternetProtocol>& operator*() const
188 return dereference();
191 /// Dereference an iterator.
192 const basic_resolver_entry<InternetProtocol>* operator->() const
194 return &dereference();
197 /// Increment operator (prefix).
198 basic_resolver_iterator& operator++()
204 /// Increment operator (postfix).
205 basic_resolver_iterator operator++(int)
207 basic_resolver_iterator tmp(*this);
212 /// Test two iterators for equality.
213 friend bool operator==(const basic_resolver_iterator& a,
214 const basic_resolver_iterator& b)
219 /// Test two iterators for inequality.
220 friend bool operator!=(const basic_resolver_iterator& a,
221 const basic_resolver_iterator& b)
229 if (++index_ == values_->size())
231 // Reset state to match a default constructed end iterator.
237 bool equal(const basic_resolver_iterator& other) const
239 if (!values_ && !other.values_)
241 if (values_ != other.values_)
243 return index_ == other.index_;
246 const basic_resolver_entry<InternetProtocol>& dereference() const
248 return (*values_)[index_];
251 typedef std::vector<basic_resolver_entry<InternetProtocol> > values_type;
252 boost::asio::detail::shared_ptr<values_type> values_;
260 #include <boost/asio/detail/pop_options.hpp>
262 #endif // BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP