OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tunwrap.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#ifndef OPENVPN_TUN_PERSIST_TUNWRAP_H
13#define OPENVPN_TUN_PERSIST_TUNWRAP_H
14
17
18namespace openvpn {
19
20// defines how the new tun/fd handle replaces old one and close() behaviour
22{
23 // close the old handle, then replace it with a new handle and
24 // perform cleanup on close
26
27 // replace the old handle with a new one without closing the old one and
28 // don't perform cleanup on close - used in iOS
29 RETAIN,
30
31 // same as NO_RETAIN, but don't replace the old handle if it is already defined.
32 // used by dco-win where we need to perform cleanup on close _and_ cannot do replace -
33 // old and new handles are the same (we got handle before establishing connection,
34 // since dco-win also implements transport) and replacing means closing the old handle -
35 // which would mean that we loose peer state in the driver
37};
38
39// TunWrapTemplate is used client-side to store the underlying tun
40// interface fd/handle. SCOPED_OBJ is generally a ScopedFD (unix) or a
41// ScopedHANDLE (Windows). It can also be a ScopedAsioStream.
42template <typename SCOPED_OBJ>
43class TunWrapTemplate : public RC<thread_unsafe_refcount>
44{
45 public:
47
49 : retain_obj_(retain_obj)
50 {
51 }
52
54 {
55 close();
56 }
57
58 bool obj_defined() const
59 {
60 return obj_.defined();
61 }
62
63 // Current persisted tun fd/handle
64 typename SCOPED_OBJ::base_type obj() const
65 {
66 return obj_();
67 }
68
69 bool destructor_defined() const
70 {
71 return bool(destruct_);
72 }
73
74 // destruct object performs cleanup prior to TAP device
75 // HANDLE close, such as removing added routes.
76 void add_destructor(const DestructorBase::Ptr &destruct)
77 {
79 destruct_ = destruct;
80 }
81
83 {
84 try
85 {
86 if (destruct_)
87 {
88 std::ostringstream os;
89 destruct_->destroy(os);
90 OPENVPN_LOG_STRING(os.str());
92 }
93 }
94 catch (const std::exception &e)
95 {
96 OPENVPN_LOG("TunWrap destructor exception: " << e.what());
97 }
98 }
99
100 void close()
101 {
103 obj_.release();
104 else
105 {
107 obj_.close();
108 }
109 }
110
111 // replace the old handle with a new one, the replacement behavior
112 // is determined by retain_obj_ enum.
113 void save_replace_sock(const typename SCOPED_OBJ::base_type obj)
114 {
116 obj_.replace(obj);
118 obj_.reset(obj);
119 }
120
121 private:
124 SCOPED_OBJ obj_;
125};
126
127} // namespace openvpn
128#endif
The smart pointer class.
Definition rc.hpp:119
void reset() noexcept
Points this RCPtr<T> to nullptr safely.
Definition rc.hpp:290
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912
bool destructor_defined() const
Definition tunwrap.hpp:69
RCPtr< TunWrapTemplate > Ptr
Definition tunwrap.hpp:46
void save_replace_sock(const typename SCOPED_OBJ::base_type obj)
Definition tunwrap.hpp:113
void add_destructor(const DestructorBase::Ptr &destruct)
Definition tunwrap.hpp:76
TunWrapTemplate(const TunWrapObjRetain retain_obj)
Definition tunwrap.hpp:48
bool obj_defined() const
Definition tunwrap.hpp:58
const TunWrapObjRetain retain_obj_
Definition tunwrap.hpp:122
DestructorBase::Ptr destruct_
Definition tunwrap.hpp:123
SCOPED_OBJ::base_type obj() const
Definition tunwrap.hpp:64
#define OPENVPN_LOG(args)
#define OPENVPN_LOG_STRING(str)
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
TunWrapObjRetain
Definition tunwrap.hpp:22
virtual void destroy(std::ostream &os)=0