diff --git a/channels/remdesk/CMakeLists.txt b/channels/remdesk/CMakeLists.txt index 23f1cf7b2..85bb0fb5f 100644 --- a/channels/remdesk/CMakeLists.txt +++ b/channels/remdesk/CMakeLists.txt @@ -17,6 +17,9 @@ define_channel("remdesk") +include_directories(common) +add_subdirectory(common) + if(WITH_CLIENT_CHANNELS) add_channel_client(${MODULE_PREFIX} ${CHANNEL_NAME}) endif() diff --git a/channels/remdesk/client/CMakeLists.txt b/channels/remdesk/client/CMakeLists.txt index 77decc9e3..1660d5018 100644 --- a/channels/remdesk/client/CMakeLists.txt +++ b/channels/remdesk/client/CMakeLists.txt @@ -23,7 +23,7 @@ set(${MODULE_PREFIX}_SRCS ) set(${MODULE_PREFIX}_LIBS - winpr freerdp + winpr freerdp remdesk-common ) add_channel_client_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} FALSE "VirtualChannelEntryEx") diff --git a/channels/remdesk/client/remdesk_main.c b/channels/remdesk/client/remdesk_main.c index 255bdaefe..8cfb27027 100644 --- a/channels/remdesk/client/remdesk_main.c +++ b/channels/remdesk/client/remdesk_main.c @@ -32,6 +32,7 @@ #include #include "remdesk_main.h" +#include "remdesk_common.h" /** * Function description @@ -129,108 +130,6 @@ static UINT remdesk_generate_expert_blob(remdeskPlugin* remdesk) return CHANNEL_RC_OK; } -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_read_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header) -{ - UINT32 ChannelNameLen = 0; - - WINPR_ASSERT(s); - WINPR_ASSERT(header); - - if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) - return ERROR_INVALID_DATA; - - Stream_Read_UINT32(s, ChannelNameLen); /* ChannelNameLen (4 bytes) */ - Stream_Read_UINT32(s, header->DataLength); /* DataLen (4 bytes) */ - - if (ChannelNameLen > 64) - { - WLog_ERR(TAG, "ChannelNameLen > 64!"); - return ERROR_INVALID_DATA; - } - - if ((ChannelNameLen % 2) != 0) - { - WLog_ERR(TAG, "ChannelNameLen %% 2) != 0 "); - return ERROR_INVALID_DATA; - } - - if (Stream_Read_UTF16_String_As_UTF8_Buffer(s, ChannelNameLen / sizeof(WCHAR), - header->ChannelName, - ARRAYSIZE(header->ChannelName)) < 0) - return ERROR_INTERNAL_ERROR; - - return CHANNEL_RC_OK; -} - -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_write_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header) -{ - WCHAR ChannelNameW[32] = { 0 }; - - WINPR_ASSERT(s); - WINPR_ASSERT(header); - - for (size_t index = 0; index < 32; index++) - { - ChannelNameW[index] = (WCHAR)header->ChannelName[index]; - } - - const size_t ChannelNameLen = - (strnlen(header->ChannelName, sizeof(header->ChannelName)) + 1) * 2; - WINPR_ASSERT(ChannelNameLen <= ARRAYSIZE(header->ChannelName)); - - Stream_Write_UINT32(s, (UINT32)ChannelNameLen); /* ChannelNameLen (4 bytes) */ - Stream_Write_UINT32(s, header->DataLength); /* DataLen (4 bytes) */ - Stream_Write(s, ChannelNameW, ChannelNameLen); /* ChannelName (variable) */ - return CHANNEL_RC_OK; -} - -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_write_ctl_header(wStream* s, REMDESK_CTL_HEADER* ctlHeader) -{ - WINPR_ASSERT(s); - WINPR_ASSERT(ctlHeader); - - const UINT error = remdesk_write_channel_header(s, &ctlHeader->ch); - if (error) - return error; - Stream_Write_UINT32(s, ctlHeader->msgType); /* msgType (4 bytes) */ - return CHANNEL_RC_OK; -} - -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_prepare_ctl_header(REMDESK_CTL_HEADER* ctlHeader, UINT32 msgType, - size_t msgSize) -{ - WINPR_ASSERT(ctlHeader); - - if (msgSize > UINT32_MAX - 4) - return ERROR_INVALID_PARAMETER; - - ctlHeader->msgType = msgType; - (void)sprintf_s(ctlHeader->ch.ChannelName, ARRAYSIZE(ctlHeader->ch.ChannelName), - REMDESK_CHANNEL_CTL_NAME); - ctlHeader->ch.DataLength = (UINT32)(4UL + msgSize); - return CHANNEL_RC_OK; -} - /** * Function description * diff --git a/channels/remdesk/common/CMakeLists.txt b/channels/remdesk/common/CMakeLists.txt new file mode 100644 index 000000000..f1006c7fd --- /dev/null +++ b/channels/remdesk/common/CMakeLists.txt @@ -0,0 +1,26 @@ +# FreeRDP: A Remote Desktop Protocol Implementation +# FreeRDP cmake build script +# +# Copyright 2024 Armin Novak +# Copyright 2024 Thincast Technologies GmbH +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set(SRCS + remdesk_common.h + remdesk_common.c) + +add_library(remdesk-common STATIC ${SRCS}) + +channel_install(remdesk-common ${FREERDP_ADDIN_PATH} "FreeRDPTargets") + diff --git a/channels/remdesk/common/remdesk_common.c b/channels/remdesk/common/remdesk_common.c new file mode 100644 index 000000000..854547798 --- /dev/null +++ b/channels/remdesk/common/remdesk_common.c @@ -0,0 +1,105 @@ +/** + * FreeRDP: A Remote Desktop Protocol Implementation + * Remote Assistance Virtual Channel - common components + * + * Copyright 2024 Armin Novak + * Copyright 2024 Thincast Technologies GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "remdesk_common.h" + +#include +#define TAG CHANNELS_TAG("remdesk.common") + +UINT remdesk_write_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header) +{ + WCHAR ChannelNameW[32] = { 0 }; + + WINPR_ASSERT(s); + WINPR_ASSERT(header); + + for (size_t index = 0; index < 32; index++) + { + ChannelNameW[index] = (WCHAR)header->ChannelName[index]; + } + + const size_t ChannelNameLen = + (strnlen(header->ChannelName, sizeof(header->ChannelName)) + 1) * 2; + WINPR_ASSERT(ChannelNameLen <= ARRAYSIZE(header->ChannelName)); + + Stream_Write_UINT32(s, (UINT32)ChannelNameLen); /* ChannelNameLen (4 bytes) */ + Stream_Write_UINT32(s, header->DataLength); /* DataLen (4 bytes) */ + Stream_Write(s, ChannelNameW, ChannelNameLen); /* ChannelName (variable) */ + return CHANNEL_RC_OK; +} + +UINT remdesk_write_ctl_header(wStream* s, const REMDESK_CTL_HEADER* ctlHeader) +{ + WINPR_ASSERT(ctlHeader); + const UINT error = remdesk_write_channel_header(s, &ctlHeader->ch); + + if (error != 0) + { + WLog_ERR(TAG, "remdesk_write_channel_header failed with error %" PRIu32 "!", error); + return error; + } + + Stream_Write_UINT32(s, ctlHeader->msgType); /* msgType (4 bytes) */ + return CHANNEL_RC_OK; +} + +UINT remdesk_read_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header) +{ + UINT32 ChannelNameLen = 0; + + if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) + return CHANNEL_RC_NO_MEMORY; + + Stream_Read_UINT32(s, ChannelNameLen); /* ChannelNameLen (4 bytes) */ + Stream_Read_UINT32(s, header->DataLength); /* DataLen (4 bytes) */ + + if (ChannelNameLen > 64) + { + WLog_ERR(TAG, "ChannelNameLen > 64!"); + return ERROR_INVALID_DATA; + } + + if ((ChannelNameLen % 2) != 0) + { + WLog_ERR(TAG, "(ChannelNameLen %% 2) != 0!"); + return ERROR_INVALID_DATA; + } + + if (Stream_Read_UTF16_String_As_UTF8_Buffer(s, ChannelNameLen / sizeof(WCHAR), + header->ChannelName, + ARRAYSIZE(header->ChannelName)) < 0) + return ERROR_INVALID_DATA; + + return CHANNEL_RC_OK; +} + +UINT remdesk_prepare_ctl_header(REMDESK_CTL_HEADER* ctlHeader, UINT32 msgType, size_t msgSize) +{ + WINPR_ASSERT(ctlHeader); + + if (msgSize > UINT32_MAX - 4) + return ERROR_INVALID_PARAMETER; + + ctlHeader->msgType = msgType; + (void)sprintf_s(ctlHeader->ch.ChannelName, ARRAYSIZE(ctlHeader->ch.ChannelName), + REMDESK_CHANNEL_CTL_NAME); + ctlHeader->ch.DataLength = (UINT32)(4UL + msgSize); + return CHANNEL_RC_OK; +} diff --git a/channels/remdesk/common/remdesk_common.h b/channels/remdesk/common/remdesk_common.h new file mode 100644 index 000000000..76dddd0f1 --- /dev/null +++ b/channels/remdesk/common/remdesk_common.h @@ -0,0 +1,32 @@ +/** + * FreeRDP: A Remote Desktop Protocol Implementation + * Remote Assistance Virtual Channel - common components + * + * Copyright 2024 Armin Novak + * Copyright 2024 Thincast Technologies GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +#include + +FREERDP_LOCAL UINT remdesk_write_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header); +FREERDP_LOCAL UINT remdesk_write_ctl_header(wStream* s, const REMDESK_CTL_HEADER* ctlHeader); +FREERDP_LOCAL UINT remdesk_read_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header); +FREERDP_LOCAL UINT remdesk_prepare_ctl_header(REMDESK_CTL_HEADER* ctlHeader, UINT32 msgType, + size_t msgSize); diff --git a/channels/remdesk/server/CMakeLists.txt b/channels/remdesk/server/CMakeLists.txt index f8a395e63..e868a0205 100644 --- a/channels/remdesk/server/CMakeLists.txt +++ b/channels/remdesk/server/CMakeLists.txt @@ -23,7 +23,7 @@ set(${MODULE_PREFIX}_SRCS ) set(${MODULE_PREFIX}_LIBS - winpr + winpr remdesk-common ) add_channel_server_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} FALSE "VirtualChannelEntry") diff --git a/channels/remdesk/server/remdesk_main.c b/channels/remdesk/server/remdesk_main.c index ad2a968a3..a8018e1d9 100644 --- a/channels/remdesk/server/remdesk_main.c +++ b/channels/remdesk/server/remdesk_main.c @@ -28,6 +28,7 @@ #include #include "remdesk_main.h" +#include "remdesk_common.h" /** * Function description @@ -44,98 +45,6 @@ static UINT remdesk_virtual_channel_write(RemdeskServerContext* context, wStream return (status) ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR; } -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_read_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header) -{ - UINT32 ChannelNameLen = 0; - - if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) - return CHANNEL_RC_NO_MEMORY; - - Stream_Read_UINT32(s, ChannelNameLen); /* ChannelNameLen (4 bytes) */ - Stream_Read_UINT32(s, header->DataLength); /* DataLen (4 bytes) */ - - if (ChannelNameLen > 64) - { - WLog_ERR(TAG, "ChannelNameLen > 64!"); - return ERROR_INVALID_DATA; - } - - if ((ChannelNameLen % 2) != 0) - { - WLog_ERR(TAG, "(ChannelNameLen %% 2) != 0!"); - return ERROR_INVALID_DATA; - } - - if (Stream_Read_UTF16_String_As_UTF8_Buffer(s, ChannelNameLen / sizeof(WCHAR), - header->ChannelName, - ARRAYSIZE(header->ChannelName)) < 0) - return ERROR_INVALID_DATA; - - return CHANNEL_RC_OK; -} - -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_write_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header) -{ - WCHAR ChannelNameW[32] = { 0 }; - - for (size_t index = 0; index < 32; index++) - { - ChannelNameW[index] = (WCHAR)header->ChannelName[index]; - } - - const size_t ChannelNameLen = - (strnlen(header->ChannelName, sizeof(header->ChannelName)) + 1ULL) * sizeof(WCHAR); - WINPR_ASSERT(ChannelNameLen <= UINT32_MAX); - Stream_Write_UINT32(s, (UINT32)ChannelNameLen); /* ChannelNameLen (4 bytes) */ - Stream_Write_UINT32(s, header->DataLength); /* DataLen (4 bytes) */ - Stream_Write(s, ChannelNameW, ChannelNameLen); /* ChannelName (variable) */ - return CHANNEL_RC_OK; -} - -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_write_ctl_header(wStream* s, REMDESK_CTL_HEADER* ctlHeader) -{ - UINT error = 0; - - if ((error = remdesk_write_channel_header(s, (REMDESK_CHANNEL_HEADER*)ctlHeader))) - { - WLog_ERR(TAG, "remdesk_write_channel_header failed with error %" PRIu32 "!", error); - return error; - } - - Stream_Write_UINT32(s, ctlHeader->msgType); /* msgType (4 bytes) */ - return CHANNEL_RC_OK; -} - -/** - * Function description - * - * @return 0 on success, otherwise a Win32 error code - */ -static UINT remdesk_prepare_ctl_header(REMDESK_CTL_HEADER* ctlHeader, UINT32 msgType, - UINT32 msgSize) -{ - ctlHeader->msgType = msgType; - (void)sprintf_s(ctlHeader->ch.ChannelName, ARRAYSIZE(ctlHeader->ch.ChannelName), - REMDESK_CHANNEL_CTL_NAME); - ctlHeader->ch.DataLength = 4 + msgSize; - return CHANNEL_RC_OK; -} - /** * Function description *