From 3870c9a7e322a3ffbc6519670b5a0a647ad804c1 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Thu, 12 Aug 2021 19:10:46 -0400 Subject: [PATCH] Notify dialog windows when OpenVPN state changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use a custom message to pass state change notification from OpenVPN to all top level windows in the thread. Currently only the pending auth dialog responds to this message by closing when the state changes. The state change could be due to timeout, errors or success via out-of-band authentication which makes the dialog no longer valid. The case of CR_TEXT messages that do not require a response is handled in the next commit. See also issue #440 https://github.com/OpenVPN/openvpn-gui/issues/440 Signed-off-by: Selva Nair --- main.h | 1 + openvpn.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/main.h b/main.h index 072099a..bd2ac89 100644 --- a/main.h +++ b/main.h @@ -61,6 +61,7 @@ #define WM_OVPN_IMPORT (WM_APP + 20) #define WM_OVPN_RESCAN (WM_APP + 21) #define WM_OVPN_ECHOMSG (WM_APP + 22) +#define WM_OVPN_STATE (WM_APP + 23) /* bool definitions */ #define bool int diff --git a/openvpn.c b/openvpn.c index d29363c..6605155 100644 --- a/openvpn.c +++ b/openvpn.c @@ -248,6 +248,20 @@ parse_assigned_ip(connection_t *c, const char *msg) } } +/* + * Send a custom message to Window hwnd when state changes + * hwnd : handle of the window to which the message is sent + * lParam : pointer to the state string (char *) received from + * OpenVPN daemon (e., "CONNECTED") + * The function signature matches callback for EnumThreadWindows. + */ +BOOL +NotifyStateChange(HWND hwnd, LPARAM lParam) +{ + SendMessage(hwnd, WM_OVPN_STATE, 0, lParam); + return TRUE; +} + /* * Handle a state change notification from the OpenVPN management interface * Format ,,[],[][,] @@ -274,6 +288,9 @@ OnStateChange(connection_t *c, char *data) return; *pos = '\0'; + /* notify the all windows in the thread of state change */ + EnumThreadWindows(GetCurrentThreadId(), NotifyStateChange, (LPARAM) state); + if (strcmp(state, "CONNECTED") == 0) { parse_assigned_ip(c, pos + 1); @@ -764,6 +781,20 @@ GenericPassDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) } break; + case WM_OVPN_STATE: /* state change message is received from OpenVPN daemon */ + /* + * AUTH_PENDING immediately transitions to GET_CONFIG until + * auth succeeds or connection restarts/aborts. + * Close the CR_TEXT dialog if state changes to anything + * other than GET_CONFIG. The state is in lParam. + */ + param = (auth_param_t *) GetProp(hwndDlg, cfgProp); + if ((param->flags & FLAG_CR_TYPE_CRTEXT) + && strcmp((const char *) lParam, "GET_CONFIG")) + { + EndDialog(hwndDlg, LOWORD(wParam)); + } + return TRUE; case WM_CLOSE: EndDialog(hwndDlg, LOWORD(wParam)); return TRUE;