summaryrefslogtreecommitdiffstats
blob: f9b7a98a026633bbcba74f6eb6ec03fd9b8a1fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//
// resolver_service.hpp
// ~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
#define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include <boost/asio/detail/push_options.hpp>

#include <boost/asio/detail/push_options.hpp>
#include <cstring>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/asio/detail/pop_options.hpp>

#include <boost/asio/error.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/basic_resolver_iterator.hpp>
#include <boost/asio/ip/basic_resolver_query.hpp>
#include <boost/asio/detail/bind_handler.hpp>
#include <boost/asio/detail/fenced_block.hpp>
#include <boost/asio/detail/mutex.hpp>
#include <boost/asio/detail/noncopyable.hpp>
#include <boost/asio/detail/operation.hpp>
#include <boost/asio/detail/service_base.hpp>
#include <boost/asio/detail/socket_ops.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/detail/thread.hpp>

namespace boost {
namespace asio {
namespace detail {

template <typename Protocol>
class resolver_service
  : public boost::asio::detail::service_base<resolver_service<Protocol> >
{
private:
  // Helper class to perform exception-safe cleanup of addrinfo objects.
  class auto_addrinfo
    : private boost::asio::detail::noncopyable
  {
  public:
    explicit auto_addrinfo(boost::asio::detail::addrinfo_type* ai)
      : ai_(ai)
    {
    }

    ~auto_addrinfo()
    {
      if (ai_)
        socket_ops::freeaddrinfo(ai_);
    }

    operator boost::asio::detail::addrinfo_type*()
    {
      return ai_;
    }

  private:
    boost::asio::detail::addrinfo_type* ai_;
  };

public:
  // The implementation type of the resolver. The shared pointer is used as a
  // cancellation token to indicate to the background thread that the operation
  // has been cancelled.
  typedef boost::shared_ptr<void> implementation_type;
  struct noop_deleter { void operator()(void*) {} };

  // The endpoint type.
  typedef typename Protocol::endpoint endpoint_type;

  // The query type.
  typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;

  // The iterator type.
  typedef boost::asio::ip::basic_resolver_iterator<Protocol> iterator_type;

  // Constructor.
  resolver_service(boost::asio::io_service& io_service)
    : boost::asio::detail::service_base<
        resolver_service<Protocol> >(io_service),
      mutex_(),
      io_service_impl_(boost::asio::use_service<io_service_impl>(io_service)),
      work_io_service_(new boost::asio::io_service),
      work_io_service_impl_(boost::asio::use_service<
          io_service_impl>(*work_io_service_)),
      work_(new boost::asio::io_service::work(*work_io_service_)),
      work_thread_(0)
  {
  }

  // Destructor.
  ~resolver_service()
  {
    shutdown_service();
  }

  // Destroy all user-defined handler objects owned by the service.
  void shutdown_service()
  {
    work_.reset();
    if (work_io_service_)
    {
      work_io_service_->stop();
      if (work_thread_)
      {
        work_thread_->join();
        work_thread_.reset();
      }
      work_io_service_.reset();
    }
  }

  // Construct a new resolver implementation.
  void construct(implementation_type& impl)
  {
    impl.reset(static_cast<void*>(0), noop_deleter());
  }

  // Destroy a resolver implementation.
  void destroy(implementation_type&)
  {
  }

  // Cancel pending asynchronous operations.
  void cancel(implementation_type& impl)
  {
    impl.reset(static_cast<void*>(0), noop_deleter());
  }

  // Resolve a query to a list of entries.
  iterator_type resolve(implementation_type&, const query_type& query,
      boost::system::error_code& ec)
  {
    boost::asio::detail::addrinfo_type* address_info = 0;
    std::string host_name = query.host_name();
    std::string service_name = query.service_name();
    boost::asio::detail::addrinfo_type hints = query.hints();

    socket_ops::getaddrinfo(!host_name.empty() ? host_name.c_str() : 0,
        service_name.c_str(), &hints, &address_info, ec);
    auto_addrinfo auto_address_info(address_info);

    if (ec)
      return iterator_type();

    return iterator_type::create(address_info, host_name, service_name);
  }

  template <typename Handler>
  class resolve_op
    : public operation
  {
  public:
    resolve_op(implementation_type impl, const query_type& query,
        io_service_impl& io_service_impl, Handler handler)
      : operation(&resolve_op::do_complete),
        impl_(impl),
        query_(query),
        io_service_impl_(io_service_impl),
        handler_(handler)
    {
    }

    static void do_complete(io_service_impl* owner, operation* base,
        boost::system::error_code /*ec*/, std::size_t /*bytes_transferred*/)
    {
      // Take ownership of the operation object.
      resolve_op* o(static_cast<resolve_op*>(base));
      typedef handler_alloc_traits<Handler, resolve_op> alloc_traits;
      handler_ptr<alloc_traits> ptr(o->handler_, o);

      if (owner)
      {
        if (owner != &o->io_service_impl_)
        {
          // The operation is being run on the worker io_service. Time to
          // perform the resolver operation.
        
          if (o->impl_.expired())
          {
            // THe operation has been cancelled.
            o->ec_ = boost::asio::error::operation_aborted;
          }
          else
          {
            // Perform the blocking host resolution operation.
            boost::asio::detail::addrinfo_type* address_info = 0;
            std::string host_name = o->query_.host_name();
            std::string service_name = o->query_.service_name();
            boost::asio::detail::addrinfo_type hints = o->query_.hints();
            socket_ops::getaddrinfo(!host_name.empty() ? host_name.c_str() : 0,
                service_name.c_str(), &hints, &address_info, o->ec_);
            auto_addrinfo auto_address_info(address_info);
            o->iter_ = iterator_type::create(
              address_info, host_name, service_name);
          }

          o->io_service_impl_.post_deferred_completion(o);
          ptr.release();
        }
        else
        {
          // The operation has been returned to the main io_serice. The
          // completion handler is ready to be delivered.

          // Make a copy of the handler so that the memory can be deallocated
          // before the upcall is made. Even if we're not about to make an
          // upcall, a sub-object of the handler may be the true owner of the
          // memory associated with the handler. Consequently, a local copy of
          // the handler is required to ensure that any owning sub-object
          // remains valid until after we have deallocated the memory here.
          detail::binder2<Handler, boost::system::error_code, iterator_type>
            handler(o->handler_, o->ec_, o->iter_);
          ptr.reset();
          boost::asio::detail::fenced_block b;
          boost_asio_handler_invoke_helpers::invoke(handler, handler);
        }
      }
    }

  private:
    boost::weak_ptr<void> impl_;
    query_type query_;
    io_service_impl& io_service_impl_;
    Handler handler_;
    boost::system::error_code ec_;
    iterator_type iter_;
  };

  // Asynchronously resolve a query to a list of entries.
  template <typename Handler>
  void async_resolve(implementation_type& impl, const query_type& query,
      Handler handler)
  {
    // Allocate and construct an operation to wrap the handler.
    typedef resolve_op<Handler> value_type;
    typedef handler_alloc_traits<Handler, value_type> alloc_traits;
    raw_handler_ptr<alloc_traits> raw_ptr(handler);
    handler_ptr<alloc_traits> ptr(raw_ptr,
        impl, query, io_service_impl_, handler);

    if (work_io_service_)
    {
      start_work_thread();
      io_service_impl_.work_started();
      work_io_service_impl_.post_immediate_completion(ptr.get());
      ptr.release();
    }
  }

  // Resolve an endpoint to a list of entries.
  iterator_type resolve(implementation_type&,
      const endpoint_type& endpoint, boost::system::error_code& ec)
  {
    // First try resolving with the service name. If that fails try resolving
    // but allow the service to be returned as a number.
    char host_name[NI_MAXHOST];
    char service_name[NI_MAXSERV];
    int flags = endpoint.protocol().type() == SOCK_DGRAM ? NI_DGRAM : 0;
    socket_ops::getnameinfo(endpoint.data(), endpoint.size(),
        host_name, NI_MAXHOST, service_name, NI_MAXSERV, flags, ec);
    if (ec)
    {
      flags |= NI_NUMERICSERV;
      socket_ops::getnameinfo(endpoint.data(), endpoint.size(),
          host_name, NI_MAXHOST, service_name, NI_MAXSERV, flags, ec);
    }

    if (ec)
      return iterator_type();

    return iterator_type::create(endpoint, host_name, service_name);
  }

  template <typename Handler>
  class resolve_endpoint_op
    : public operation
  {
  public:
    resolve_endpoint_op(implementation_type impl, const endpoint_type& ep,
        io_service_impl& io_service_impl, Handler handler)
      : operation(&resolve_endpoint_op::do_complete),
        impl_(impl),
        ep_(ep),
        io_service_impl_(io_service_impl),
        handler_(handler)
    {
    }

    static void do_complete(io_service_impl* owner, operation* base,
        boost::system::error_code /*ec*/, std::size_t /*bytes_transferred*/)
    {
      // Take ownership of the operation object.
      resolve_endpoint_op* o(static_cast<resolve_endpoint_op*>(base));
      typedef handler_alloc_traits<Handler, resolve_endpoint_op> alloc_traits;
      handler_ptr<alloc_traits> ptr(o->handler_, o);

      if (owner)
      {
        if (owner != &o->io_service_impl_)
        {
          // The operation is being run on the worker io_service. Time to
          // perform the resolver operation.
        
          if (o->impl_.expired())
          {
            // THe operation has been cancelled.
            o->ec_ = boost::asio::error::operation_aborted;
          }
          else
          {
            // Perform the blocking endoint resolution operation.
            char host_name[NI_MAXHOST];
            char service_name[NI_MAXSERV];
            int flags = o->ep_.protocol().type() == SOCK_DGRAM ? NI_DGRAM : 0;
            socket_ops::getnameinfo(o->ep_.data(), o->ep_.size(),
                host_name, NI_MAXHOST, service_name,
                NI_MAXSERV, flags, o->ec_);
            if (o->ec_)
            {
              flags |= NI_NUMERICSERV;
              socket_ops::getnameinfo(o->ep_.data(), o->ep_.size(),
                  host_name, NI_MAXHOST, service_name,
                  NI_MAXSERV, flags, o->ec_);
            }
            o->iter_ = iterator_type::create(o->ep_, host_name, service_name);
          }

          o->io_service_impl_.post_deferred_completion(o);
          ptr.release();
        }
        else
        {
          // The operation has been returned to the main io_serice. The
          // completion handler is ready to be delivered.

          // Make a copy of the handler so that the memory can be deallocated
          // before the upcall is made. Even if we're not about to make an
          // upcall, a sub-object of the handler may be the true owner of the
          // memory associated with the handler. Consequently, a local copy of
          // the handler is required to ensure that any owning sub-object
          // remains valid until after we have deallocated the memory here.
          detail::binder2<Handler, boost::system::error_code, iterator_type>
            handler(o->handler_, o->ec_, o->iter_);
          ptr.reset();
          boost::asio::detail::fenced_block b;
          boost_asio_handler_invoke_helpers::invoke(handler, handler);
        }
      }
    }

  private:
    boost::weak_ptr<void> impl_;
    endpoint_type ep_;
    io_service_impl& io_service_impl_;
    Handler handler_;
    boost::system::error_code ec_;
    iterator_type iter_;
  };

  // Asynchronously resolve an endpoint to a list of entries.
  template <typename Handler>
  void async_resolve(implementation_type& impl, const endpoint_type& endpoint,
      Handler handler)
  {
    // Allocate and construct an operation to wrap the handler.
    typedef resolve_endpoint_op<Handler> value_type;
    typedef handler_alloc_traits<Handler, value_type> alloc_traits;
    raw_handler_ptr<alloc_traits> raw_ptr(handler);
    handler_ptr<alloc_traits> ptr(raw_ptr,
        impl, endpoint, io_service_impl_, handler);

    if (work_io_service_)
    {
      start_work_thread();
      io_service_impl_.work_started();
      work_io_service_impl_.post_immediate_completion(ptr.get());
      ptr.release();
    }
  }

private:
  // Helper class to run the work io_service in a thread.
  class work_io_service_runner
  {
  public:
    work_io_service_runner(boost::asio::io_service& io_service)
      : io_service_(io_service) {}
    void operator()() { io_service_.run(); }
  private:
    boost::asio::io_service& io_service_;
  };

  // Start the work thread if it's not already running.
  void start_work_thread()
  {
    boost::asio::detail::mutex::scoped_lock lock(mutex_);
    if (!work_thread_)
    {
      work_thread_.reset(new boost::asio::detail::thread(
            work_io_service_runner(*work_io_service_)));
    }
  }

  // Mutex to protect access to internal data.
  boost::asio::detail::mutex mutex_;

  // The io_service implementation used to post completions.
  io_service_impl& io_service_impl_;

  // Private io_service used for performing asynchronous host resolution.
  boost::scoped_ptr<boost::asio::io_service> work_io_service_;

  // The work io_service implementation used to post completions.
  io_service_impl& work_io_service_impl_;

  // Work for the private io_service to perform.
  boost::scoped_ptr<boost::asio::io_service::work> work_;

  // Thread used for running the work io_service's run loop.
  boost::scoped_ptr<boost::asio::detail::thread> work_thread_;
};

} // namespace detail
} // namespace asio
} // namespace boost

#include <boost/asio/detail/pop_options.hpp>

#endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP