File: generic.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (72 lines) | stat: -rw-r--r-- 2,338 bytes parent folder | download
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
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

#ifndef OPENVPN_CLIENT_ASYNC_RESOLVE_GENERIC_H
#define OPENVPN_CLIENT_ASYNC_RESOLVE_GENERIC_H

#include <openvpn/common/bigmutex.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/common/hostport.hpp>


namespace openvpn {
template <typename RESOLVER_TYPE>
class AsyncResolvable : public virtual RC<thread_unsafe_refcount>
{
  private:
    typedef RCPtr<AsyncResolvable> Ptr;

    openvpn_io::io_context &io_context;
    RESOLVER_TYPE resolver;

  public:
    using resolver_type = RESOLVER_TYPE;
    using results_type = typename RESOLVER_TYPE::results_type;

    AsyncResolvable(openvpn_io::io_context &io_context_arg)
        : io_context(io_context_arg),
          resolver(io_context_arg)
    {
    }

    virtual void resolve_callback(const openvpn_io::error_code &error,
                                  results_type results) = 0;

    // This implementation assumes that the i/o reactor provides an asynchronous
    // DNS resolution routine using its own primitives and that doesn't require
    // us to take care of any non-interruptible opration (i.e. getaddrinfo() in
    // case of ASIO).
    //
    // For example, iOS implements aync_resolve using GCD and CFHost. This
    // implementation satisfies the constraints mentioned above
    virtual void async_resolve_name(const std::string &host, const std::string &port)
    {
        resolver.async_resolve(host,
                               port,
                               [self = Ptr(this)](const openvpn_io::error_code &error, results_type results)
                               {
	  OPENVPN_ASYNC_HANDLER;
	  self->resolve_callback(error, results); });
    }

    // no-op: needed to provide the same class signature of the ASIO version
    void async_resolve_lock()
    {
    }

    void async_resolve_cancel()
    {
        resolver.cancel();
    }
};
} // namespace openvpn

#endif /* OPENVPN_CLIENT_ASYNC_RESOLVE_GENERIC_H */