OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tunpersist.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_TUNPERSIST_H
13#define OPENVPN_TUN_PERSIST_TUNPERSIST_H
14
19
20namespace openvpn {
21
22// TunPersistTemplate adds persistence capabilities onto TunWrapTemplate,
23// in order to implement logic for the persist-tun directive.
24template <typename SCOPED_OBJ, typename STATE = TunProp::State::Ptr>
25class TunPersistTemplate : public TunWrapTemplate<SCOPED_OBJ>
26{
27 public:
29
30 TunPersistTemplate(const bool enable_persistence, const TunWrapObjRetain retain_obj, TunBuilderBase *tb)
31 : TunWrapTemplate<SCOPED_OBJ>(retain_obj),
32 enable_persistence_(enable_persistence),
33 tb_(tb),
34 use_persisted_tun_(false),
35 disconnect(false)
36 {
37 }
38
39 // Current persisted state
40 const STATE &state() const
41 {
42 return state_;
43 }
44
46 {
48 }
49
51 {
52 options_.clear();
53 }
54
60
62 {
63 disconnect = true;
64 }
65
66 // Current persisted options
67 const std::string &options()
68 {
69 return options_;
70 }
71
72 // Return true if we should use previously persisted
73 // tun socket descriptor/handle
74 bool use_persisted_tun(const IP::Addr server_addr,
75 const TunProp::Config &tun_prop,
76 const OptionList &opt)
77 {
78#if OPENVPN_DEBUG_TUN_BUILDER > 0
79 {
81 try
82 {
83 TunProp::configure_builder(capture.get(), nullptr, nullptr, server_addr, tun_prop, opt, nullptr, true);
84 OPENVPN_LOG("*** TUN BUILDER CAPTURE" << std::endl
85 << capture->to_string());
86 }
87 catch (const std::exception &e)
88 {
89 OPENVPN_LOG("*** TUN BUILDER CAPTURE exception: " << e.what());
90 }
91 }
92#endif
93
94 // In tun_persist mode, capture tun builder settings so we can
95 // compare them to previous persisted settings.
97 {
99 try
100 {
101 TunProp::configure_builder(copt_.get(), nullptr, nullptr, server_addr, tun_prop, opt, nullptr, true);
102 }
103 catch (const std::exception &)
104 {
105 copt_.reset();
106 }
107 }
108
109 // Check if previous tun session matches properties of to-be-created session
111 && copt_
112 && !options_.empty()
113 && options_ == copt_->to_string()
114 && (tb_ ? tb_->tun_builder_persist() : true));
115 return use_persisted_tun_;
116 }
117
118 // Possibly save tunnel fd/handle, state, and options.
119 bool persist_tun_state(const typename SCOPED_OBJ::base_type obj,
120 const STATE &state,
121 bool save_replace_sock = true)
122 {
124 {
126 }
128 {
129 state_ = state;
131 return true;
132 }
133 else
134 return false;
135 }
136
137 private:
139 {
140 if (tb_)
142 state_.reset();
143 options_ = "";
144 }
145
148 STATE state_;
149 std::string options_;
150
153
155};
156
157} // namespace openvpn
158#endif
The smart pointer class.
Definition rc.hpp:119
void reset() noexcept
Points this RCPtr<T> to nullptr safely.
Definition rc.hpp:290
T * get() const noexcept
Returns the raw pointer to the object T, or nullptr.
Definition rc.hpp:321
TunBuilder methods, loosely based on the Android VpnService.Builder abstraction.
Definition base.hpp:42
virtual bool tun_builder_persist()
Determines if the TUN interface can be persisted.
Definition base.hpp:379
virtual void tun_builder_teardown(bool disconnect)
Indicates that tunnel is being torn down.
Definition base.hpp:417
std::string to_string() const
Definition capture.hpp:567
const std::string & options()
RCPtr< TunPersistTemplate > Ptr
const STATE & state() const
TunBuilderCapture::Ptr copt_
bool persist_tun_state(const typename SCOPED_OBJ::base_type obj, const STATE &state, bool save_replace_sock=true)
TunPersistTemplate(const bool enable_persistence, const TunWrapObjRetain retain_obj, TunBuilderBase *tb)
TunBuilderBase *const tb_
bool use_persisted_tun(const IP::Addr server_addr, const TunProp::Config &tun_prop, const OptionList &opt)
static void configure_builder(TunBuilderBase *tb, State *state, SessionStats *stats, const IP::Addr &server_addr, const Config &config, const OptionList &opt, const EmulateExcludeRouteFactory *eer_factory, const bool quiet)
Definition tunprop.hpp:90
void save_replace_sock(const typename SCOPED_OBJ::base_type obj)
Definition tunwrap.hpp:113
bool obj_defined() const
Definition tunwrap.hpp:58
SCOPED_OBJ::base_type obj() const
Definition tunwrap.hpp:64
#define OPENVPN_LOG(args)
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
TunWrapObjRetain
Definition tunwrap.hpp:22