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