diff --git a/javacli/OpenVPNClientThread.java b/javacli/OpenVPNClientThread.java index 3a7c8f2d..4a84e82a 100644 --- a/javacli/OpenVPNClientThread.java +++ b/javacli/OpenVPNClientThread.java @@ -66,7 +66,7 @@ public class OpenVPNClientThread extends ClientAPI_OpenVPNClient implements Runn boolean tun_builder_set_mtu(int mtu); boolean tun_builder_set_session_name(String name); int tun_builder_establish(); - void tun_builder_teardown(); + void tun_builder_teardown(boolean disconnect); } public OpenVPNClientThread() { @@ -337,9 +337,9 @@ public class OpenVPNClientThread extends ClientAPI_OpenVPNClient implements Runn } @Override - public void tun_builder_teardown() { + public void tun_builder_teardown(boolean disconnect) { TunBuilder tb = tun_builder; if (tb != null) - tb.tun_builder_teardown(); + tb.tun_builder_teardown(disconnect); } } diff --git a/openvpn/client/cliconnect.hpp b/openvpn/client/cliconnect.hpp index a73b252d..b88c8c48 100644 --- a/openvpn/client/cliconnect.hpp +++ b/openvpn/client/cliconnect.hpp @@ -129,7 +129,10 @@ namespace openvpn { if (pre_resolve) pre_resolve->cancel(); if (client) - client->stop(false); + { + client->tun_set_disconnect(); + client->stop(false); + } cancel_timers(); asio_work.reset(); diff --git a/openvpn/client/cliproto.hpp b/openvpn/client/cliproto.hpp index eb35c8f2..a8c9750d 100644 --- a/openvpn/client/cliproto.hpp +++ b/openvpn/client/cliproto.hpp @@ -185,6 +185,12 @@ namespace openvpn { Base::send_explicit_exit_notify(); } + void tun_set_disconnect() + { + if (tun) + tun->set_disconnect(); + } + void stop(const bool call_terminate_callback) { if (!halt) @@ -193,12 +199,12 @@ namespace openvpn { housekeeping_timer.cancel(); push_request_timer.cancel(); inactive_timer.cancel(); - if (tun) - tun->stop(); - if (transport) - transport->stop(); if (notify_callback && call_terminate_callback) notify_callback->client_proto_terminate(); + if (tun) + tun->stop(); // call after client_proto_terminate() so it can call back to tun_set_disconnect + if (transport) + transport->stop(); } } diff --git a/openvpn/tun/builder/base.hpp b/openvpn/tun/builder/base.hpp index 19b9eaf9..a96a7de2 100644 --- a/openvpn/tun/builder/base.hpp +++ b/openvpn/tun/builder/base.hpp @@ -191,8 +191,10 @@ namespace openvpn { { } - // Called just before tunnel socket is closed - virtual void tun_builder_teardown() {} + // Indicates that tunnel is being torn down. + // If disconnect == true, then the teardown is occurring + // prior to final disconnect. + virtual void tun_builder_teardown(bool disconnect) {} virtual ~TunBuilderBase() {} }; diff --git a/openvpn/tun/builder/client.hpp b/openvpn/tun/builder/client.hpp index 2f50914d..39b1b044 100644 --- a/openvpn/tun/builder/client.hpp +++ b/openvpn/tun/builder/client.hpp @@ -224,6 +224,12 @@ namespace openvpn { return ""; } + virtual void set_disconnect() + { + if (tun_persist) + tun_persist->set_disconnect(); + } + virtual void stop() { stop_(); } virtual ~Client() { stop_(); } diff --git a/openvpn/tun/client/tunbase.hpp b/openvpn/tun/client/tunbase.hpp index da0bc700..7613c124 100644 --- a/openvpn/tun/client/tunbase.hpp +++ b/openvpn/tun/client/tunbase.hpp @@ -42,6 +42,7 @@ namespace openvpn { virtual void client_start(const OptionList&, TransportClient&) = 0; virtual void stop() = 0; + virtual void set_disconnect() = 0; virtual bool tun_send(BufferAllocated& buf) = 0; // return true if send succeeded virtual std::string tun_name() const = 0; virtual std::string vpn_ip4() const = 0; diff --git a/openvpn/tun/mac/client/tuncli.hpp b/openvpn/tun/mac/client/tuncli.hpp index cd37174f..41b23ce5 100644 --- a/openvpn/tun/mac/client/tuncli.hpp +++ b/openvpn/tun/mac/client/tuncli.hpp @@ -643,6 +643,10 @@ namespace openvpn { return ""; } + virtual void set_disconnect() + { + } + virtual void stop() { stop_(); } virtual ~Client() { stop_(); } diff --git a/openvpn/tun/persist/tunpersist.hpp b/openvpn/tun/persist/tunpersist.hpp index f8087a54..1c3d4085 100644 --- a/openvpn/tun/persist/tunpersist.hpp +++ b/openvpn/tun/persist/tunpersist.hpp @@ -40,7 +40,8 @@ namespace openvpn { : TunWrapTemplate(retain_obj), enable_persistence_(enable_persistence), tb_(tb), - use_persisted_tun_(false) + use_persisted_tun_(false), + disconnect(false) { } @@ -61,6 +62,11 @@ namespace openvpn { TunWrapTemplate::close(); } + void set_disconnect() + { + disconnect = true; + } + // Current persisted options const std::string& options() { @@ -131,7 +137,7 @@ namespace openvpn { void close_local() { if (tb_) - tb_->tun_builder_teardown(); + tb_->tun_builder_teardown(disconnect); state_.reset(); options_ = ""; } @@ -143,6 +149,8 @@ namespace openvpn { TunBuilderCapture::Ptr copt_; bool use_persisted_tun_; + + bool disconnect; }; } diff --git a/openvpn/tun/win/client/tuncli.hpp b/openvpn/tun/win/client/tuncli.hpp index 03a5d792..5beccaac 100644 --- a/openvpn/tun/win/client/tuncli.hpp +++ b/openvpn/tun/win/client/tuncli.hpp @@ -253,6 +253,10 @@ namespace openvpn { return ""; } + virtual void set_disconnect() + { + } + virtual void stop() { stop_(); } virtual ~Client() { stop_(); }