OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tcp.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
16
17namespace openvpn::Acceptor {
18
19struct TCP : public Base
20{
21 typedef RCPtr<TCP> Ptr;
22
23 TCP(openvpn_io::io_context &io_context)
24 : acceptor(io_context)
25 {
26 }
27
28 void async_accept(ListenerBase *listener,
29 const size_t acceptor_index,
30 openvpn_io::io_context &io_context) override
31 {
32 AsioPolySock::TCP::Ptr sock(new AsioPolySock::TCP(io_context, acceptor_index));
33 acceptor.async_accept(sock->socket,
34 [listener = ListenerBase::Ptr(listener), sock](const openvpn_io::error_code &error) mutable
35 { listener->handle_accept(std::move(sock), error); });
36 }
37
38 void close() override
39 {
40#ifdef OPENVPN_DEBUG_ACCEPT
41 OPENVPN_LOG("ACCEPTOR CLOSE " << local_endpoint);
42#endif
43 acceptor.close();
44 }
45
46 enum
47 {
48 // start at (1<<24) to avoid conflicting with SSLConst flags
49 DISABLE_REUSE_ADDR = (1 << 24),
50 REUSE_PORT = (1 << 25),
51
53 };
54 void set_socket_options(unsigned int flags)
55 {
56 static_assert(int(FIRST) > int(SSLConst::LAST), "TCP flags in conflict with SSL flags");
57
58#if defined(OPENVPN_PLATFORM_WIN)
59 // set Windows socket flags
61 acceptor.set_option(openvpn_io::ip::tcp::acceptor::reuse_address(true));
62#else
63 // set Unix socket flags
64 {
65 const int fd = acceptor.native_handle();
66 if (flags & REUSE_PORT)
67 SockOpt::reuseport(fd);
71 }
72#endif
73 }
74
75 // filter all but socket option flags
76 static unsigned int sockopt_flags(const unsigned int flags)
77 {
79 }
80
81 openvpn_io::ip::tcp::endpoint local_endpoint;
82 openvpn_io::ip::tcp::acceptor acceptor;
83};
84
85} // namespace openvpn::Acceptor
The smart pointer class.
Definition rc.hpp:119
#define OPENVPN_LOG(args)
void set_cloexec(const int fd)
Definition sockopt.hpp:57
void reuseaddr(const int fd)
Definition sockopt.hpp:41
RCPtr< TCP > Ptr
Definition tcp.hpp:21
void async_accept(ListenerBase *listener, const size_t acceptor_index, openvpn_io::io_context &io_context) override
Definition tcp.hpp:28
TCP(openvpn_io::io_context &io_context)
Definition tcp.hpp:23
openvpn_io::ip::tcp::endpoint local_endpoint
Definition tcp.hpp:81
openvpn_io::ip::tcp::acceptor acceptor
Definition tcp.hpp:82
static unsigned int sockopt_flags(const unsigned int flags)
Definition tcp.hpp:76
void close() override
Definition tcp.hpp:38
void set_socket_options(unsigned int flags)
Definition tcp.hpp:54
reroute_gw flags