1//
2// detail/resolver_service.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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_query.hpp>
23#include <boost/asio/ip/basic_resolver_results.hpp>
24#include <boost/asio/detail/concurrency_hint.hpp>
25#include <boost/asio/detail/memory.hpp>
26#include <boost/asio/detail/resolve_endpoint_op.hpp>
27#include <boost/asio/detail/resolve_query_op.hpp>
28#include <boost/asio/detail/resolver_service_base.hpp>
29
30#include <boost/asio/detail/push_options.hpp>
31
32namespace boost {
33namespace asio {
34namespace detail {
35
36template <typename Protocol>
37class resolver_service :
38 public execution_context_service_base<resolver_service<Protocol>>,
39 public resolver_service_base
40{
41public:
42 // The implementation type of the resolver. A cancellation token is used to
43 // indicate to the background thread that the operation has been cancelled.
44 typedef socket_ops::shared_cancel_token_type implementation_type;
45
46 // The endpoint type.
47 typedef typename Protocol::endpoint endpoint_type;
48
49 // The query type.
50 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
51
52 // The results type.
53 typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
54
55 // Constructor.
56 resolver_service(execution_context& context)
57 : execution_context_service_base<resolver_service<Protocol>>(context),
58 resolver_service_base(context)
59 {
60 }
61
62 // Destroy all user-defined handler objects owned by the service.
63 void shutdown()
64 {
65 this->base_shutdown();
66 }
67
68 // Perform any fork-related housekeeping.
69 void notify_fork(execution_context::fork_event fork_ev)
70 {
71 this->base_notify_fork(fork_ev);
72 }
73
74 // Resolve a query to a list of entries.
75 results_type resolve(implementation_type&, const query_type& qry,
76 boost::system::error_code& ec)
77 {
78 boost::asio::detail::addrinfo_type* address_info = 0;
79
80 socket_ops::getaddrinfo(host: qry.host_name().c_str(),
81 service: qry.service_name().c_str(), hints: qry.hints(), result: &address_info, ec);
82 auto_addrinfo auto_address_info(address_info);
83
84 BOOST_ASIO_ERROR_LOCATION(ec);
85 return ec ? results_type() : results_type::create(
86 address_info, qry.host_name(), qry.service_name());
87 }
88
89 // Asynchronously resolve a query to a list of entries.
90 template <typename Handler, typename IoExecutor>
91 void async_resolve(implementation_type& impl, const query_type& qry,
92 Handler& handler, const IoExecutor& io_ex)
93 {
94 // Allocate and construct an operation to wrap the handler.
95 typedef resolve_query_op<Protocol, Handler, IoExecutor> op;
96 typename op::ptr p = { boost::asio::detail::addressof(handler),
97 op::ptr::allocate(handler), 0 };
98 p.p = new (p.v) op(impl, qry, scheduler_, handler, io_ex);
99
100 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
101 *p.p, "resolver", &impl, 0, "async_resolve"));
102
103 start_resolve_op(op: p.p);
104 p.v = p.p = 0;
105 }
106
107 // Resolve an endpoint to a list of entries.
108 results_type resolve(implementation_type&,
109 const endpoint_type& endpoint, boost::system::error_code& ec)
110 {
111 char host_name[NI_MAXHOST];
112 char service_name[NI_MAXSERV];
113 socket_ops::sync_getnameinfo(addr: endpoint.data(), addrlen: endpoint.size(),
114 host: host_name, NI_MAXHOST, serv: service_name, NI_MAXSERV,
115 sock_type: endpoint.protocol().type(), ec);
116
117 BOOST_ASIO_ERROR_LOCATION(ec);
118 return ec ? results_type() : results_type::create(
119 endpoint, host_name, service_name);
120 }
121
122 // Asynchronously resolve an endpoint to a list of entries.
123 template <typename Handler, typename IoExecutor>
124 void async_resolve(implementation_type& impl, const endpoint_type& endpoint,
125 Handler& handler, const IoExecutor& io_ex)
126 {
127 // Allocate and construct an operation to wrap the handler.
128 typedef resolve_endpoint_op<Protocol, Handler, IoExecutor> op;
129 typename op::ptr p = { boost::asio::detail::addressof(handler),
130 op::ptr::allocate(handler), 0 };
131 p.p = new (p.v) op(impl, endpoint, scheduler_, handler, io_ex);
132
133 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
134 *p.p, "resolver", &impl, 0, "async_resolve"));
135
136 start_resolve_op(op: p.p);
137 p.v = p.p = 0;
138 }
139};
140
141} // namespace detail
142} // namespace asio
143} // namespace boost
144
145#include <boost/asio/detail/pop_options.hpp>
146
147#endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
148
149#endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
150

source code of boost/libs/asio/include/boost/asio/detail/resolver_service.hpp