2019-06-11 17:13:42 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* RDPEDISP Virtual Channel Extension
|
|
|
|
*
|
|
|
|
* Copyright 2019 Kobi Mizrachi <kmizrachi18@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
#include "disp_main.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2022-02-22 12:54:42 +03:00
|
|
|
#include <winpr/assert.h>
|
2019-06-11 17:13:42 +03:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
#include <winpr/stream.h>
|
|
|
|
#include <winpr/sysinfo.h>
|
|
|
|
#include <freerdp/channels/wtsvc.h>
|
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
|
|
|
#include <freerdp/server/disp.h>
|
|
|
|
#include "../disp_common.h"
|
|
|
|
|
|
|
|
#define TAG CHANNELS_TAG("rdpedisp.server")
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
|
|
|
|
static wStream* disp_server_single_packet_new(UINT32 type, UINT32 length)
|
|
|
|
{
|
|
|
|
UINT error;
|
|
|
|
DISPLAY_CONTROL_HEADER header;
|
|
|
|
wStream* s = Stream_New(NULL, DISPLAY_CONTROL_HEADER_LENGTH + length);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
header.type = type;
|
2021-10-16 22:04:33 +03:00
|
|
|
header.length = DISPLAY_CONTROL_HEADER_LENGTH + length;
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if ((error = disp_write_header(s, &header)))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "Failed to write header with error %" PRIu32 "!", error);
|
2019-06-11 17:13:42 +03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
error:
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:09:19 +03:00
|
|
|
static void disp_server_sanitize_monitor_layout(DISPLAY_CONTROL_MONITOR_LAYOUT* monitor)
|
|
|
|
{
|
|
|
|
if (monitor->PhysicalWidth < DISPLAY_CONTROL_MIN_PHYSICAL_MONITOR_WIDTH ||
|
|
|
|
monitor->PhysicalWidth > DISPLAY_CONTROL_MAX_PHYSICAL_MONITOR_WIDTH ||
|
|
|
|
monitor->PhysicalHeight < DISPLAY_CONTROL_MIN_PHYSICAL_MONITOR_HEIGHT ||
|
|
|
|
monitor->PhysicalHeight > DISPLAY_CONTROL_MAX_PHYSICAL_MONITOR_HEIGHT)
|
|
|
|
{
|
|
|
|
if (monitor->PhysicalWidth != 0 || monitor->PhysicalHeight != 0)
|
|
|
|
WLog_DBG(
|
|
|
|
TAG,
|
|
|
|
"Sanitizing invalid physical monitor size. Old physical monitor size: [%" PRIu32
|
|
|
|
", %" PRIu32 "]",
|
|
|
|
monitor->PhysicalWidth, monitor->PhysicalHeight);
|
|
|
|
|
|
|
|
monitor->PhysicalWidth = monitor->PhysicalHeight = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:54:42 +03:00
|
|
|
static BOOL disp_server_is_monitor_layout_valid(const DISPLAY_CONTROL_MONITOR_LAYOUT* monitor)
|
2019-06-11 17:13:42 +03:00
|
|
|
{
|
2022-02-22 12:54:42 +03:00
|
|
|
WINPR_ASSERT(monitor);
|
|
|
|
|
2019-06-11 17:13:42 +03:00
|
|
|
if (monitor->Width < DISPLAY_CONTROL_MIN_MONITOR_WIDTH ||
|
|
|
|
monitor->Width > DISPLAY_CONTROL_MAX_MONITOR_WIDTH)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "Received invalid value for monitor->Width: %" PRIu32 "", monitor->Width);
|
2019-06-11 17:13:42 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (monitor->Height < DISPLAY_CONTROL_MIN_MONITOR_HEIGHT ||
|
|
|
|
monitor->Height > DISPLAY_CONTROL_MAX_MONITOR_HEIGHT)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "Received invalid value for monitor->Height: %" PRIu32 "", monitor->Width);
|
2019-06-11 17:13:42 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (monitor->Orientation)
|
|
|
|
{
|
|
|
|
case ORIENTATION_LANDSCAPE:
|
|
|
|
case ORIENTATION_PORTRAIT:
|
|
|
|
case ORIENTATION_LANDSCAPE_FLIPPED:
|
|
|
|
case ORIENTATION_PORTRAIT_FLIPPED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "Received incorrect value for monitor->Orientation: %" PRIu32 "",
|
2019-06-11 17:13:42 +03:00
|
|
|
monitor->Orientation);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT disp_recv_display_control_monitor_layout_pdu(wStream* s, DispServerContext* context)
|
|
|
|
{
|
|
|
|
UINT32 error = CHANNEL_RC_OK;
|
|
|
|
UINT32 index;
|
2022-02-22 12:54:42 +03:00
|
|
|
DISPLAY_CONTROL_MONITOR_LAYOUT_PDU pdu = { 0 };
|
|
|
|
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
WINPR_ASSERT(context);
|
2019-06-11 17:13:42 +03:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
|
2019-06-11 17:13:42 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, pdu.MonitorLayoutSize); /* MonitorLayoutSize (4 bytes) */
|
|
|
|
|
|
|
|
if (pdu.MonitorLayoutSize != DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "MonitorLayoutSize is set to %" PRIu32 ". expected %" PRIu32 "",
|
|
|
|
pdu.MonitorLayoutSize, DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE);
|
2019-06-11 17:13:42 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, pdu.NumMonitors); /* NumMonitors (4 bytes) */
|
|
|
|
|
|
|
|
if (pdu.NumMonitors > context->MaxNumMonitors)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "NumMonitors (%" PRIu32 ")> server MaxNumMonitors (%" PRIu32 ")",
|
|
|
|
pdu.NumMonitors, context->MaxNumMonitors);
|
2019-06-11 17:13:42 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(
|
|
|
|
TAG, s, pdu.NumMonitors * 1ull * DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE))
|
2019-06-11 17:13:42 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pdu.Monitors = (DISPLAY_CONTROL_MONITOR_LAYOUT*)calloc(pdu.NumMonitors,
|
|
|
|
sizeof(DISPLAY_CONTROL_MONITOR_LAYOUT));
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if (!pdu.Monitors)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "disp_recv_display_control_monitor_layout_pdu(): calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "disp_recv_display_control_monitor_layout_pdu: NumMonitors=%" PRIu32 "",
|
2019-06-11 17:13:42 +03:00
|
|
|
pdu.NumMonitors);
|
|
|
|
|
|
|
|
for (index = 0; index < pdu.NumMonitors; index++)
|
|
|
|
{
|
2022-02-22 12:54:42 +03:00
|
|
|
DISPLAY_CONTROL_MONITOR_LAYOUT* monitor = &(pdu.Monitors[index]);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Read_UINT32(s, monitor->Flags); /* Flags (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->Left); /* Left (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->Top); /* Top (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->Width); /* Width (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->Height); /* Height (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->PhysicalWidth); /* PhysicalWidth (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->PhysicalHeight); /* PhysicalHeight (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->Orientation); /* Orientation (4 bytes) */
|
2019-06-11 17:13:42 +03:00
|
|
|
Stream_Read_UINT32(s, monitor->DesktopScaleFactor); /* DesktopScaleFactor (4 bytes) */
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Read_UINT32(s, monitor->DeviceScaleFactor); /* DeviceScaleFactor (4 bytes) */
|
2022-02-22 12:09:19 +03:00
|
|
|
|
|
|
|
disp_server_sanitize_monitor_layout(monitor);
|
2019-06-11 17:13:42 +03:00
|
|
|
WLog_DBG(TAG,
|
2019-11-06 17:24:51 +03:00
|
|
|
"\t%d : Flags: 0x%08" PRIX32 " Left/Top: (%" PRId32 ",%" PRId32 ") W/H=%" PRIu32
|
|
|
|
"x%" PRIu32 ")",
|
|
|
|
index, monitor->Flags, monitor->Left, monitor->Top, monitor->Width,
|
2019-06-11 17:13:42 +03:00
|
|
|
monitor->Height);
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG,
|
|
|
|
"\t PhysicalWidth: %" PRIu32 " PhysicalHeight: %" PRIu32 " Orientation: %" PRIu32
|
|
|
|
"",
|
|
|
|
monitor->PhysicalWidth, monitor->PhysicalHeight, monitor->Orientation);
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if (!disp_server_is_monitor_layout_valid(monitor))
|
|
|
|
{
|
|
|
|
error = ERROR_INVALID_DATA;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context)
|
|
|
|
IFCALLRET(context->DispMonitorLayout, error, context, &pdu);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(pdu.Monitors);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT disp_server_receive_pdu(DispServerContext* context, wStream* s)
|
|
|
|
{
|
|
|
|
UINT error = CHANNEL_RC_OK;
|
|
|
|
size_t beg, end;
|
2022-02-22 12:54:42 +03:00
|
|
|
DISPLAY_CONTROL_HEADER header = { 0 };
|
|
|
|
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
2019-06-11 17:13:42 +03:00
|
|
|
beg = Stream_GetPosition(s);
|
|
|
|
|
|
|
|
if ((error = disp_read_header(s, &header)))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "disp_read_header failed with error %" PRIu32 "!", error);
|
2019-06-11 17:13:42 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (header.type)
|
|
|
|
{
|
|
|
|
case DISPLAY_CONTROL_PDU_TYPE_MONITOR_LAYOUT:
|
|
|
|
if ((error = disp_recv_display_control_monitor_layout_pdu(s, context)))
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"disp_recv_display_control_monitor_layout_pdu "
|
|
|
|
"failed with error %" PRIu32 "!",
|
|
|
|
error);
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = CHANNEL_RC_BAD_PROC;
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "Received unknown PDU type: %" PRIu32 "", header.type);
|
2019-06-11 17:13:42 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = Stream_GetPosition(s);
|
|
|
|
|
|
|
|
if (end != (beg + header.length))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "Unexpected DISP pdu end: Actual: %d, Expected: %" PRIu32 "", end,
|
|
|
|
(beg + header.length));
|
2019-06-11 17:13:42 +03:00
|
|
|
Stream_SetPosition(s, (beg + header.length));
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT disp_server_handle_messages(DispServerContext* context)
|
|
|
|
{
|
|
|
|
DWORD BytesReturned;
|
|
|
|
void* buffer;
|
|
|
|
UINT ret = CHANNEL_RC_OK;
|
2022-02-22 12:54:42 +03:00
|
|
|
DispServerPrivate* priv;
|
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
priv = context->priv;
|
|
|
|
WINPR_ASSERT(priv);
|
|
|
|
|
|
|
|
s = priv->input_stream;
|
|
|
|
WINPR_ASSERT(s);
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
/* Check whether the dynamic channel is ready */
|
|
|
|
if (!priv->isReady)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (WTSVirtualChannelQuery(priv->disp_channel, WTSVirtualChannelReady, &buffer,
|
|
|
|
&BytesReturned) == FALSE)
|
2019-06-11 17:13:42 +03:00
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_NO_DATA)
|
|
|
|
return ERROR_NO_DATA;
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelQuery failed");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
priv->isReady = *((BOOL*)buffer);
|
2019-06-11 17:13:42 +03:00
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
}
|
|
|
|
|
2019-12-31 16:47:03 +03:00
|
|
|
/* Consume channel event only after the disp dynamic channel is ready */
|
2019-06-11 17:13:42 +03:00
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WTSVirtualChannelRead(priv->disp_channel, 0, NULL, 0, &BytesReturned))
|
2019-06-11 17:13:42 +03:00
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_NO_DATA)
|
|
|
|
return ERROR_NO_DATA;
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BytesReturned < 1)
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (WTSVirtualChannelRead(priv->disp_channel, 0, (PCHAR)Stream_Buffer(s), Stream_Capacity(s),
|
|
|
|
&BytesReturned) == FALSE)
|
2019-06-11 17:13:42 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_SetLength(s, BytesReturned);
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
|
|
|
|
while (Stream_GetPosition(s) < Stream_Length(s))
|
|
|
|
{
|
|
|
|
if ((ret = disp_server_receive_pdu(context, s)))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"disp_server_receive_pdu "
|
|
|
|
"failed with error %" PRIu32 "!",
|
|
|
|
ret);
|
2019-06-11 17:13:42 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI disp_server_thread_func(LPVOID arg)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DispServerContext* context = (DispServerContext*)arg;
|
2022-02-22 12:54:42 +03:00
|
|
|
DispServerPrivate* priv;
|
2019-06-11 17:13:42 +03:00
|
|
|
DWORD status;
|
2022-02-22 12:54:42 +03:00
|
|
|
DWORD nCount = 0;
|
|
|
|
HANDLE events[8] = { 0 };
|
2019-06-11 17:13:42 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2022-02-22 12:54:42 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
priv = context->priv;
|
|
|
|
WINPR_ASSERT(priv);
|
|
|
|
|
2019-06-11 17:13:42 +03:00
|
|
|
events[nCount++] = priv->stopEvent;
|
|
|
|
events[nCount++] = priv->channelEvent;
|
|
|
|
|
|
|
|
/* Main virtual channel loop. RDPEDISP do not need version negotiation */
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
|
2019-06-11 17:13:42 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop Event */
|
|
|
|
if (status == WAIT_OBJECT_0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((error = disp_server_handle_messages(context)))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "disp_server_handle_messages failed with error %" PRIu32 "", error);
|
2019-06-11 17:13:42 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitThread(error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT disp_server_open(DispServerContext* context)
|
|
|
|
{
|
|
|
|
UINT rc = ERROR_INTERNAL_ERROR;
|
2022-02-22 12:54:42 +03:00
|
|
|
DispServerPrivate* priv;
|
2019-06-11 17:13:42 +03:00
|
|
|
DWORD BytesReturned = 0;
|
|
|
|
PULONG pSessionId = NULL;
|
2022-02-22 12:54:42 +03:00
|
|
|
void* buffer = NULL;
|
2022-06-08 14:35:52 +03:00
|
|
|
UINT32 channelId;
|
|
|
|
BOOL status = TRUE;
|
2022-02-22 12:54:42 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
priv = context->priv;
|
|
|
|
WINPR_ASSERT(priv);
|
|
|
|
|
2019-06-11 17:13:42 +03:00
|
|
|
priv->SessionId = WTS_CURRENT_SESSION;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
|
|
|
|
(LPSTR*)&pSessionId, &BytesReturned) == FALSE)
|
2019-06-11 17:13:42 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
|
|
|
|
rc = ERROR_INTERNAL_ERROR;
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
priv->SessionId = (DWORD)*pSessionId;
|
2019-12-31 16:47:03 +03:00
|
|
|
WTSFreeMemory(pSessionId);
|
2019-11-06 17:24:51 +03:00
|
|
|
priv->disp_channel = (HANDLE)WTSVirtualChannelOpenEx(priv->SessionId, DISP_DVC_CHANNEL_NAME,
|
|
|
|
WTS_CHANNEL_OPTION_DYNAMIC);
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if (!priv->disp_channel)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed!");
|
|
|
|
rc = GetLastError();
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2022-06-08 14:35:52 +03:00
|
|
|
channelId = WTSChannelGetIdByHandle(priv->disp_channel);
|
|
|
|
|
|
|
|
IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "context->ChannelIdAssigned failed!");
|
|
|
|
rc = ERROR_INTERNAL_ERROR;
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2019-06-11 17:13:42 +03:00
|
|
|
/* Query for channel event handle */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WTSVirtualChannelQuery(priv->disp_channel, WTSVirtualEventHandle, &buffer,
|
|
|
|
&BytesReturned) ||
|
|
|
|
(BytesReturned != sizeof(HANDLE)))
|
2019-06-11 17:13:42 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"WTSVirtualChannelQuery failed "
|
|
|
|
"or invalid returned size(%" PRIu32 ")",
|
2019-06-11 17:13:42 +03:00
|
|
|
BytesReturned);
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
|
|
|
|
rc = ERROR_INTERNAL_ERROR;
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyMemory(&priv->channelEvent, buffer, sizeof(HANDLE));
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
|
|
|
|
if (priv->thread == NULL)
|
|
|
|
{
|
|
|
|
if (!(priv->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
rc = ERROR_INTERNAL_ERROR;
|
2022-04-28 11:49:42 +03:00
|
|
|
goto out_close;
|
2019-06-11 17:13:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(priv->thread =
|
|
|
|
CreateThread(NULL, 0, disp_server_thread_func, (void*)context, 0, NULL)))
|
2019-06-11 17:13:42 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
CloseHandle(priv->stopEvent);
|
|
|
|
priv->stopEvent = NULL;
|
|
|
|
rc = ERROR_INTERNAL_ERROR;
|
2022-04-28 11:49:42 +03:00
|
|
|
goto out_close;
|
2019-06-11 17:13:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
out_close:
|
|
|
|
WTSVirtualChannelClose(priv->disp_channel);
|
|
|
|
priv->disp_channel = NULL;
|
|
|
|
priv->channelEvent = NULL;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT disp_server_packet_send(DispServerContext* context, wStream* s)
|
|
|
|
{
|
|
|
|
UINT ret;
|
|
|
|
ULONG written;
|
|
|
|
|
2022-02-22 12:54:42 +03:00
|
|
|
WINPR_ASSERT(context);
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WTSVirtualChannelWrite(context->priv->disp_channel, (PCHAR)Stream_Buffer(s),
|
2019-06-11 17:13:42 +03:00
|
|
|
Stream_GetPosition(s), &written))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
|
|
|
|
ret = ERROR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (written < Stream_GetPosition(s))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
|
|
|
|
Stream_GetPosition(s));
|
2019-06-11 17:13:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = CHANNEL_RC_OK;
|
|
|
|
out:
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT disp_server_send_caps_pdu(DispServerContext* context)
|
|
|
|
{
|
2022-02-22 12:54:42 +03:00
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
s = disp_server_single_packet_new(DISPLAY_CONTROL_PDU_TYPE_CAPS, 12);
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "disp_server_single_packet_new failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT32(s, context->MaxNumMonitors); /* MaxNumMonitors (4 bytes) */
|
2019-06-11 17:13:42 +03:00
|
|
|
Stream_Write_UINT32(s, context->MaxMonitorAreaFactorA); /* MaxMonitorAreaFactorA (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, context->MaxMonitorAreaFactorB); /* MaxMonitorAreaFactorB (4 bytes) */
|
|
|
|
return disp_server_packet_send(context, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT disp_server_close(DispServerContext* context)
|
|
|
|
{
|
|
|
|
UINT error = CHANNEL_RC_OK;
|
2022-02-22 12:54:42 +03:00
|
|
|
DispServerPrivate* priv;
|
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
priv = context->priv;
|
|
|
|
WINPR_ASSERT(priv);
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if (priv->thread)
|
|
|
|
{
|
|
|
|
SetEvent(priv->stopEvent);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
|
2019-06-11 17:13:42 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(priv->thread);
|
|
|
|
CloseHandle(priv->stopEvent);
|
|
|
|
priv->thread = NULL;
|
|
|
|
priv->stopEvent = NULL;
|
|
|
|
}
|
|
|
|
|
2019-12-31 16:47:03 +03:00
|
|
|
if (priv->disp_channel)
|
|
|
|
{
|
|
|
|
WTSVirtualChannelClose(priv->disp_channel);
|
|
|
|
priv->disp_channel = NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-11 17:13:42 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
DispServerContext* disp_server_context_new(HANDLE vcm)
|
|
|
|
{
|
|
|
|
DispServerContext* context;
|
|
|
|
DispServerPrivate* priv;
|
2019-11-06 17:24:51 +03:00
|
|
|
context = (DispServerContext*)calloc(1, sizeof(DispServerContext));
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "disp_server_context_new(): calloc DispServerContext failed!");
|
2022-02-22 12:54:42 +03:00
|
|
|
goto fail;
|
2019-06-11 17:13:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
priv = context->priv = (DispServerPrivate*)calloc(1, sizeof(DispServerPrivate));
|
2019-06-11 17:13:42 +03:00
|
|
|
|
|
|
|
if (!context->priv)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "disp_server_context_new(): calloc DispServerPrivate failed!");
|
2022-02-22 12:54:42 +03:00
|
|
|
goto fail;
|
2019-06-11 17:13:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->input_stream = Stream_New(NULL, 4);
|
|
|
|
|
|
|
|
if (!priv->input_stream)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
2022-02-22 12:54:42 +03:00
|
|
|
goto fail;
|
2019-06-11 17:13:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
context->vcm = vcm;
|
|
|
|
context->Open = disp_server_open;
|
|
|
|
context->Close = disp_server_close;
|
|
|
|
context->DisplayControlCaps = disp_server_send_caps_pdu;
|
|
|
|
priv->isReady = FALSE;
|
|
|
|
return context;
|
2022-02-22 12:54:42 +03:00
|
|
|
fail:
|
|
|
|
disp_server_context_free(context);
|
2019-06-11 17:13:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disp_server_context_free(DispServerContext* context)
|
|
|
|
{
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (context->priv)
|
|
|
|
{
|
2022-02-22 12:54:42 +03:00
|
|
|
disp_server_close(context);
|
2019-06-11 17:13:42 +03:00
|
|
|
Stream_Free(context->priv->input_stream, TRUE);
|
|
|
|
free(context->priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(context);
|
|
|
|
}
|