From ef8c9e48e8acf61e16cb9fd547799067c87b20da Mon Sep 17 00:00:00 2001 From: akallabeth Date: Thu, 7 Mar 2024 14:26:58 +0100 Subject: [PATCH] [core,utils] unify channel reloading Add the wrapper function utils_reload_channels that unifies the channel cleanup and channel initialization sequence required on connect, redirect and gateway policy apply --- libfreerdp/core/connection.c | 11 +++-------- libfreerdp/core/freerdp.c | 16 ++++------------ libfreerdp/core/utils.c | 30 ++++++++++++++++++------------ libfreerdp/core/utils.h | 2 ++ 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/libfreerdp/core/connection.c b/libfreerdp/core/connection.c index 3abfa932b..fe927fca9 100644 --- a/libfreerdp/core/connection.c +++ b/libfreerdp/core/connection.c @@ -622,11 +622,10 @@ BOOL rdp_client_redirect(rdpRdp* rdp) if (!rdp_client_disconnect_and_clear(rdp)) return FALSE; + /* Only disconnect & close the channels here. + * they will be discarded and recreated after the new settings have been applied. */ freerdp_channels_disconnect(rdp->context->channels, rdp->context->instance); freerdp_channels_close(rdp->context->channels, rdp->context->instance); - freerdp_channels_free(rdp->context->channels); - rdp->context->channels = freerdp_channels_new(rdp->context->instance); - WINPR_ASSERT(rdp->context->channels); if (rdp_redirection_apply_settings(rdp) != 0) return FALSE; @@ -684,14 +683,10 @@ BOOL rdp_client_redirect(rdpRdp* rdp) if (!IFCALLRESULT(TRUE, rdp->context->instance->Redirect, rdp->context->instance)) return FALSE; - BOOL ok = IFCALLRESULT(TRUE, rdp->context->instance->LoadChannels, rdp->context->instance); + BOOL ok = utils_reload_channels(rdp->context); if (!ok) return FALSE; - if (CHANNEL_RC_OK != - freerdp_channels_pre_connect(rdp->context->channels, rdp->context->instance)) - return FALSE; - status = rdp_client_connect(rdp); if (status) diff --git a/libfreerdp/core/freerdp.c b/libfreerdp/core/freerdp.c index 29d907ac4..56cd445f0 100644 --- a/libfreerdp/core/freerdp.c +++ b/libfreerdp/core/freerdp.c @@ -84,7 +84,6 @@ static void sig_abort_connect(int signum, const char* signame, void* ctx) static int freerdp_connect_begin(freerdp* instance) { BOOL rc = 0; - UINT status2 = CHANNEL_RC_OK; rdpRdp* rdp = NULL; BOOL status = TRUE; rdpSettings* settings = NULL; @@ -121,16 +120,9 @@ static int freerdp_connect_begin(freerdp* instance) freerdp_settings_print_warnings(settings); if (status) - { - if (!rdp_set_backup_settings(rdp)) - return 0; - - WINPR_ASSERT(instance->LoadChannels); - if (!instance->LoadChannels(instance)) - return 0; - - status2 = freerdp_channels_pre_connect(instance->context->channels, instance); - } + status = rdp_set_backup_settings(rdp); + if (status) + status = utils_reload_channels(instance->context); KeyboardLayout = freerdp_settings_get_uint32(settings, FreeRDP_KeyboardLayout); switch (KeyboardLayout) @@ -151,7 +143,7 @@ static int freerdp_connect_begin(freerdp* instance) break; } - if (!status || (status2 != CHANNEL_RC_OK)) + if (!status) { rdpContext* context = instance->context; WINPR_ASSERT(context); diff --git a/libfreerdp/core/utils.c b/libfreerdp/core/utils.c index 09a5b261f..f414611d8 100644 --- a/libfreerdp/core/utils.c +++ b/libfreerdp/core/utils.c @@ -360,18 +360,7 @@ static BOOL disable_pnp(rdpSettings* settings) static BOOL apply_gw_policy(rdpContext* context) { WINPR_ASSERT(context); - - freerdp_channels_disconnect(context->channels, context->instance); - freerdp_channels_close(context->channels, context->instance); - freerdp_channels_free(context->channels); - context->channels = freerdp_channels_new(context->instance); - WINPR_ASSERT(context->channels); - - BOOL rc = TRUE; - IFCALLRET(context->instance->LoadChannels, rc, context->instance); - if (rc) - return freerdp_channels_pre_connect(context->channels, context->instance) == CHANNEL_RC_OK; - return rc; + return utils_reload_channels(context); } BOOL utils_apply_gateway_policy(wLog* log, rdpContext* context, UINT32 flags, const char* module) @@ -474,3 +463,20 @@ char* utils_redir_flags_to_string(UINT32 flags, char* buffer, size_t size) winpr_str_append("{", buffer, size, "}"); return buffer; } + +BOOL utils_reload_channels(rdpContext* context) +{ + WINPR_ASSERT(context); + + freerdp_channels_disconnect(context->channels, context->instance); + freerdp_channels_close(context->channels, context->instance); + freerdp_channels_free(context->channels); + context->channels = freerdp_channels_new(context->instance); + WINPR_ASSERT(context->channels); + + BOOL rc = TRUE; + IFCALLRET(context->instance->LoadChannels, rc, context->instance); + if (rc) + return freerdp_channels_pre_connect(context->channels, context->instance) == CHANNEL_RC_OK; + return rc; +} diff --git a/libfreerdp/core/utils.h b/libfreerdp/core/utils.h index 55f21f676..54a785632 100644 --- a/libfreerdp/core/utils.h +++ b/libfreerdp/core/utils.h @@ -59,4 +59,6 @@ const char* utils_is_vsock(const char* hostname); BOOL utils_apply_gateway_policy(wLog* log, rdpContext* context, UINT32 flags, const char* module); char* utils_redir_flags_to_string(UINT32 flags, char* buffer, size_t size); +BOOL utils_reload_channels(rdpContext* context); + #endif /* FREERDP_LIB_CORE_UTILS_H */