File: asiopolysock.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 (377 lines) | stat: -rw-r--r-- 9,055 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
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
//    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
//

// Asio polymorphic socket for handling TCP
// and unix domain sockets.

#ifndef OPENVPN_ASIO_ASIOPOLYSOCK_H
#define OPENVPN_ASIO_ASIOPOLYSOCK_H

#include <openvpn/io/io.hpp>

#include <openvpn/common/platform.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/size.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/common/function.hpp>
#include <openvpn/common/to_string.hpp>
#include <openvpn/common/sockopt.hpp>
#include <openvpn/addr/ip.hpp>

#if defined(OPENVPN_POLYSOCK_SUPPORTS_ALT_ROUTING)
#include <openvpn/asio/alt_routing.hpp>
#elif defined(OPENVPN_POLYSOCK_SUPPORTS_BIND)
#include <openvpn/asio/asioboundsock.hpp>
#endif

#ifdef ASIO_HAS_LOCAL_SOCKETS
#include <openvpn/common/peercred.hpp>
#endif

namespace openvpn::AsioPolySock {
// for shutdown()
enum ShutdownFlags
{
    SHUTDOWN_SEND = (1 << 0),
    SHUTDOWN_RECV = (1 << 1),
};

class Base : public RC<thread_unsafe_refcount>
{
  public:
    typedef RCPtr<Base> Ptr;

    virtual void async_send(const openvpn_io::const_buffer &buf,
                            Function<void(const openvpn_io::error_code &, const size_t)> &&callback) = 0;

    virtual void async_receive(const openvpn_io::mutable_buffer &buf,
                               Function<void(const openvpn_io::error_code &, const size_t)> &&callback) = 0;

    virtual std::string remote_endpoint_str() const = 0;
    virtual bool remote_ip_port(IP::Addr &addr, unsigned int &port) const = 0;

    virtual void non_blocking(const bool state) = 0;

    virtual void close() = 0;

    virtual void shutdown(const unsigned int flags)
    {
    }

    virtual void tcp_nodelay()
    {
    }
    virtual void set_cloexec()
    {
    }

    virtual openvpn_io::detail::socket_type native_handle()
    {
        return -1;
    }

#ifdef ASIO_HAS_LOCAL_SOCKETS
    virtual bool peercreds(SockOpt::Creds &cr)
    {
        return false;
    }
#endif

#if defined(OPENVPN_POLYSOCK_SUPPORTS_ALT_ROUTING)
    virtual bool alt_routing_enabled() const
    {
        return false;
    }
#endif

    virtual bool is_open() const = 0;
    virtual bool is_local() const = 0;

    size_t index() const
    {
        return index_;
    }

  protected:
    Base(const size_t index)
        : index_(index)
    {
    }

  private:
    size_t index_;
};

struct TCP : public Base
{
    typedef RCPtr<TCP> Ptr;

    TCP(openvpn_io::io_context &io_context,
        const size_t index)
        : Base(index),
          socket(io_context)
    {
    }

    void async_send(const openvpn_io::const_buffer &buf,
                    Function<void(const openvpn_io::error_code &, const size_t)> &&callback) override
    {
        socket.async_send(buf, std::move(callback));
    }

    void async_receive(const openvpn_io::mutable_buffer &buf,
                       Function<void(const openvpn_io::error_code &, const size_t)> &&callback) override
    {
        socket.async_receive(buf, std::move(callback));
    }

#if !defined(OPENVPN_POLYSOCK_SUPPORTS_ALT_ROUTING)
    std::string remote_endpoint_str() const override
    {
        try
        {
            return "TCP " + openvpn::to_string(socket.remote_endpoint());
        }
        catch (const std::exception &)
        {
            return "TCP";
        }
    }
#endif

    bool remote_ip_port(IP::Addr &addr, unsigned int &port) const override
    {
        try
        {
            addr = IP::Addr::from_asio(socket.remote_endpoint().address());
            port = socket.remote_endpoint().port();
            return true;
        }
        catch (const std::exception &)
        {
            return false;
        }
    }

    void non_blocking(const bool state) override
    {
        socket.non_blocking(state);
    }

    void tcp_nodelay() override
    {
        socket.set_option(openvpn_io::ip::tcp::no_delay(true));
    }

#if !defined(OPENVPN_PLATFORM_WIN)
    void set_cloexec() override
    {
        const int fd = socket.native_handle();
        if (fd >= 0)
            SockOpt::set_cloexec(fd);
    }
#endif

    void shutdown(const unsigned int flags) override
    {
        if (flags & SHUTDOWN_SEND)
            socket.shutdown(openvpn_io::ip::tcp::socket::shutdown_send);
        else if (flags & SHUTDOWN_RECV)
            socket.shutdown(openvpn_io::ip::tcp::socket::shutdown_receive);
    }

    void close() override
    {
        socket.close();
    }

    bool is_open() const override
    {
        return socket.is_open();
    }

    bool is_local() const override
    {
        return false;
    }

    openvpn_io::detail::socket_type native_handle() override
    {
        return socket.native_handle();
    }

#if defined(OPENVPN_POLYSOCK_SUPPORTS_ALT_ROUTING)
    std::string remote_endpoint_str() const override
    {
        const char *proto = (socket.alt_routing_enabled() ? "TCP ALT " : "TCP ");
        return proto + socket.to_string();
    }

    bool alt_routing_enabled() const override
    {
        return socket.alt_routing_enabled();
    }

    AltRouting::Socket socket;
#elif defined(OPENVPN_POLYSOCK_SUPPORTS_BIND)
    AsioBoundSocket::Socket socket;
#else
    openvpn_io::ip::tcp::socket socket;
#endif
};

#ifdef ASIO_HAS_LOCAL_SOCKETS
struct Unix : public Base
{
    typedef RCPtr<Unix> Ptr;

    Unix(openvpn_io::io_context &io_context,
         const size_t index)
        : Base(index),
          socket(io_context)
    {
    }

    void async_send(const openvpn_io::const_buffer &buf,
                    Function<void(const openvpn_io::error_code &, const size_t)> &&callback) override
    {
        socket.async_send(buf, std::move(callback));
    }

    void async_receive(const openvpn_io::mutable_buffer &buf,
                       Function<void(const openvpn_io::error_code &, const size_t)> &&callback) override
    {
        socket.async_receive(buf, std::move(callback));
    }

    std::string remote_endpoint_str() const override
    {
        return "LOCAL";
    }

    bool remote_ip_port(IP::Addr &, unsigned int &) const override
    {
        return false;
    }

    void non_blocking(const bool state) override
    {
        socket.non_blocking(state);
    }

    bool peercreds(SockOpt::Creds &cr) override
    {
        return SockOpt::peercreds(socket.native_handle(), cr);
    }

    void set_cloexec() override
    {
        const int fd = socket.native_handle();
        if (fd >= 0)
            SockOpt::set_cloexec(fd);
    }

#if !defined(OPENVPN_PLATFORM_MAC)
    // shutdown() throws "socket is not connected" exception
    // on macos if another side has called close() - this behavior
    // breaks communication with agent, and hence disabled
    void shutdown(const unsigned int flags) override
    {
        if (flags & SHUTDOWN_SEND)
            socket.shutdown(openvpn_io::ip::tcp::socket::shutdown_send);
        else if (flags & SHUTDOWN_RECV)
            socket.shutdown(openvpn_io::ip::tcp::socket::shutdown_receive);
    }
#endif

    void close() override
    {
        socket.close();
    }

    bool is_open() const override
    {
        return socket.is_open();
    }

    bool is_local() const override
    {
        return true;
    }

    openvpn_io::detail::socket_type native_handle() override
    {
        return socket.native_handle();
    }

    openvpn_io::local::stream_protocol::socket socket;
};
#endif

#if defined(OPENVPN_PLATFORM_WIN)
struct NamedPipe : public Base
{
    typedef RCPtr<NamedPipe> Ptr;

    NamedPipe(openvpn_io::windows::stream_handle &&handle_arg,
              const size_t index)
        : Base(index),
          handle(std::move(handle_arg))
    {
    }

    void async_send(const openvpn_io::const_buffer &buf,
                    Function<void(const openvpn_io::error_code &, const size_t)> &&callback) override
    {
        handle.async_write_some(buf, std::move(callback));
    }

    void async_receive(const openvpn_io::mutable_buffer &buf,
                       Function<void(const openvpn_io::error_code &, const size_t)> &&callback) override
    {
        handle.async_read_some(buf, std::move(callback));
    }

    std::string remote_endpoint_str() const override
    {
        return "NAMED_PIPE";
    }

    bool remote_ip_port(IP::Addr &, unsigned int &) const override
    {
        return false;
    }

    void non_blocking(const bool state) override
    {
    }

    void close() override
    {
        handle.close();
    }

    bool is_open() const override
    {
        return handle.is_open();
    }

    bool is_local() const override
    {
        return true;
    }

    openvpn_io::windows::stream_handle handle;
};
#endif
} // namespace openvpn::AsioPolySock

#endif