1 //
2 // detail/resolver_service.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 
11 #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
21 
22 #include <boost/asio/ip/basic_resolver_iterator.hpp>
23 #include <boost/asio/ip/basic_resolver_query.hpp>
24 #include <boost/asio/detail/addressof.hpp>
25 #include <boost/asio/detail/resolve_endpoint_op.hpp>
26 #include <boost/asio/detail/resolve_op.hpp>
27 #include <boost/asio/detail/resolver_service_base.hpp>
28 
29 #include <boost/asio/detail/push_options.hpp>
30 
31 namespace boost {
32 namespace asio {
33 namespace detail {
34 
35 template <typename Protocol>
36 class resolver_service : public resolver_service_base
37 {
38 public:
39   // The implementation type of the resolver. A cancellation token is used to
40   // indicate to the background thread that the operation has been cancelled.
41   typedef socket_ops::shared_cancel_token_type implementation_type;
42 
43   // The endpoint type.
44   typedef typename Protocol::endpoint endpoint_type;
45 
46   // The query type.
47   typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
48 
49   // The iterator type.
50   typedef boost::asio::ip::basic_resolver_iterator<Protocol> iterator_type;
51 
52   // Constructor.
resolver_service(boost::asio::io_service & io_service)53   resolver_service(boost::asio::io_service& io_service)
54     : resolver_service_base(io_service)
55   {
56   }
57 
58   // Resolve a query to a list of entries.
resolve(implementation_type &,const query_type & query,boost::system::error_code & ec)59   iterator_type resolve(implementation_type&, const query_type& query,
60       boost::system::error_code& ec)
61   {
62     boost::asio::detail::addrinfo_type* address_info = 0;
63 
64     socket_ops::getaddrinfo(query.host_name().c_str(),
65         query.service_name().c_str(), query.hints(), &address_info, ec);
66     auto_addrinfo auto_address_info(address_info);
67 
68     return ec ? iterator_type() : iterator_type::create(
69         address_info, query.host_name(), query.service_name());
70   }
71 
72   // Asynchronously resolve a query to a list of entries.
73   template <typename Handler>
async_resolve(implementation_type & impl,const query_type & query,Handler & handler)74   void async_resolve(implementation_type& impl,
75       const query_type& query, Handler& handler)
76   {
77     // Allocate and construct an operation to wrap the handler.
78     typedef resolve_op<Protocol, Handler> op;
79     typename op::ptr p = { boost::asio::detail::addressof(handler),
80       boost_asio_handler_alloc_helpers::allocate(
81         sizeof(op), handler), 0 };
82     p.p = new (p.v) op(impl, query, io_service_impl_, handler);
83 
84     BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve"));
85 
86     start_resolve_op(p.p);
87     p.v = p.p = 0;
88   }
89 
90   // Resolve an endpoint to a list of entries.
resolve(implementation_type &,const endpoint_type & endpoint,boost::system::error_code & ec)91   iterator_type resolve(implementation_type&,
92       const endpoint_type& endpoint, boost::system::error_code& ec)
93   {
94     char host_name[NI_MAXHOST];
95     char service_name[NI_MAXSERV];
96     socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
97         host_name, NI_MAXHOST, service_name, NI_MAXSERV,
98         endpoint.protocol().type(), ec);
99 
100     return ec ? iterator_type() : iterator_type::create(
101         endpoint, host_name, service_name);
102   }
103 
104   // Asynchronously resolve an endpoint to a list of entries.
105   template <typename Handler>
async_resolve(implementation_type & impl,const endpoint_type & endpoint,Handler & handler)106   void async_resolve(implementation_type& impl,
107       const endpoint_type& endpoint, Handler& handler)
108   {
109     // Allocate and construct an operation to wrap the handler.
110     typedef resolve_endpoint_op<Protocol, Handler> op;
111     typename op::ptr p = { boost::asio::detail::addressof(handler),
112       boost_asio_handler_alloc_helpers::allocate(
113         sizeof(op), handler), 0 };
114     p.p = new (p.v) op(impl, endpoint, io_service_impl_, handler);
115 
116     BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve"));
117 
118     start_resolve_op(p.p);
119     p.v = p.p = 0;
120   }
121 };
122 
123 } // namespace detail
124 } // namespace asio
125 } // namespace boost
126 
127 #include <boost/asio/detail/pop_options.hpp>
128 
129 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
130 
131 #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
132