[channels,remdesk] create common components

Extract common functions from server and client channel and move it to a
static library linked by both
This commit is contained in:
akallabeth 2024-11-11 10:59:35 +01:00
parent 75ff6db4cd
commit 0c35fd1a48
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5
8 changed files with 170 additions and 196 deletions

View File

@ -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()

View File

@ -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")

View File

@ -32,6 +32,7 @@
#include <freerdp/client/remdesk.h>
#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
*

View File

@ -0,0 +1,26 @@
# FreeRDP: A Remote Desktop Protocol Implementation
# FreeRDP cmake build script
#
# Copyright 2024 Armin Novak <armin.novak@thincast.com>
# 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")

View File

@ -0,0 +1,105 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Remote Assistance Virtual Channel - common components
*
* Copyright 2024 Armin Novak <armin.novak@thincast.com>
* 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 <freerdp/channels/log.h>
#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;
}

View File

@ -0,0 +1,32 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Remote Assistance Virtual Channel - common components
*
* Copyright 2024 Armin Novak <armin.novak@thincast.com>
* 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 <winpr/wtypes.h>
#include <winpr/stream.h>
#include <freerdp/channels/remdesk.h>
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);

View File

@ -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")

View File

@ -28,6 +28,7 @@
#include <freerdp/freerdp.h>
#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
*