summaryrefslogtreecommitdiffstats
path: root/channels/disp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--channels/disp/CMakeLists.txt26
-rw-r--r--channels/disp/ChannelOptions.cmake12
-rw-r--r--channels/disp/client/CMakeLists.txt32
-rw-r--r--channels/disp/client/disp_main.c323
-rw-r--r--channels/disp/client/disp_main.h36
-rw-r--r--channels/disp/disp_common.c55
-rw-r--r--channels/disp/disp_common.h32
-rw-r--r--channels/disp/server/CMakeLists.txt32
-rw-r--r--channels/disp/server/disp_main.c632
-rw-r--r--channels/disp/server/disp_main.h37
10 files changed, 1217 insertions, 0 deletions
diff --git a/channels/disp/CMakeLists.txt b/channels/disp/CMakeLists.txt
new file mode 100644
index 0000000..44afe99
--- /dev/null
+++ b/channels/disp/CMakeLists.txt
@@ -0,0 +1,26 @@
+# FreeRDP: A Remote Desktop Protocol Implementation
+# FreeRDP cmake build script
+#
+# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@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.
+
+define_channel("disp")
+
+if(WITH_CLIENT_CHANNELS)
+ add_channel_client(${MODULE_PREFIX} ${CHANNEL_NAME})
+endif()
+
+if(WITH_SERVER_CHANNELS)
+ add_channel_server(${MODULE_PREFIX} ${CHANNEL_NAME})
+endif()
diff --git a/channels/disp/ChannelOptions.cmake b/channels/disp/ChannelOptions.cmake
new file mode 100644
index 0000000..0e254ad
--- /dev/null
+++ b/channels/disp/ChannelOptions.cmake
@@ -0,0 +1,12 @@
+
+set(OPTION_DEFAULT OFF)
+set(OPTION_CLIENT_DEFAULT ON)
+set(OPTION_SERVER_DEFAULT OFF)
+
+define_channel_options(NAME "disp" TYPE "dynamic"
+ DESCRIPTION "Display Update Virtual Channel Extension"
+ SPECIFICATIONS "[MS-RDPEDISP]"
+ DEFAULT ${OPTION_DEFAULT})
+
+define_channel_client_options(${OPTION_CLIENT_DEFAULT})
+define_channel_server_options(${OPTION_SERVER_DEFAULT})
diff --git a/channels/disp/client/CMakeLists.txt b/channels/disp/client/CMakeLists.txt
new file mode 100644
index 0000000..d1e1f98
--- /dev/null
+++ b/channels/disp/client/CMakeLists.txt
@@ -0,0 +1,32 @@
+# FreeRDP: A Remote Desktop Protocol Implementation
+# FreeRDP cmake build script
+#
+# Copyright 2013 Marc-Andre Moreau <marcandre.moreau@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.
+
+define_channel_client("disp")
+
+set(${MODULE_PREFIX}_SRCS
+ disp_main.c
+ disp_main.h
+ ../disp_common.c
+ ../disp_common.h
+)
+
+set(${MODULE_PREFIX}_LIBS
+ winpr
+)
+include_directories(..)
+
+add_channel_client_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} TRUE "DVCPluginEntry")
diff --git a/channels/disp/client/disp_main.c b/channels/disp/client/disp_main.c
new file mode 100644
index 0000000..f018240
--- /dev/null
+++ b/channels/disp/client/disp_main.c
@@ -0,0 +1,323 @@
+/**
+ * FreeRDP: A Remote Desktop Protocol Implementation
+ * Display Update Virtual Channel Extension
+ *
+ * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2015 Thincast Technologies GmbH
+ * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
+ * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.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.
+ */
+
+#include <freerdp/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <winpr/crt.h>
+#include <winpr/assert.h>
+#include <winpr/synch.h>
+#include <winpr/print.h>
+#include <winpr/thread.h>
+#include <winpr/stream.h>
+#include <winpr/sysinfo.h>
+#include <winpr/cmdline.h>
+#include <winpr/collections.h>
+
+#include <freerdp/addin.h>
+#include <freerdp/client/channels.h>
+
+#include "disp_main.h"
+#include "../disp_common.h"
+
+typedef struct
+{
+ GENERIC_DYNVC_PLUGIN base;
+
+ DispClientContext* context;
+ UINT32 MaxNumMonitors;
+ UINT32 MaxMonitorAreaFactorA;
+ UINT32 MaxMonitorAreaFactorB;
+} DISP_PLUGIN;
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT
+disp_send_display_control_monitor_layout_pdu(GENERIC_CHANNEL_CALLBACK* callback, UINT32 NumMonitors,
+ const DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
+{
+ UINT status = 0;
+ wStream* s = NULL;
+ DISP_PLUGIN* disp = NULL;
+ UINT32 MonitorLayoutSize = 0;
+ DISPLAY_CONTROL_HEADER header = { 0 };
+
+ WINPR_ASSERT(callback);
+ WINPR_ASSERT(Monitors || (NumMonitors == 0));
+
+ disp = (DISP_PLUGIN*)callback->plugin;
+ WINPR_ASSERT(disp);
+
+ MonitorLayoutSize = DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE;
+ header.length = 8 + 8 + (NumMonitors * MonitorLayoutSize);
+ header.type = DISPLAY_CONTROL_PDU_TYPE_MONITOR_LAYOUT;
+
+ s = Stream_New(NULL, header.length);
+
+ if (!s)
+ {
+ WLog_ERR(TAG, "Stream_New failed!");
+ return CHANNEL_RC_NO_MEMORY;
+ }
+
+ if ((status = disp_write_header(s, &header)))
+ {
+ WLog_ERR(TAG, "Failed to write header with error %" PRIu32 "!", status);
+ goto out;
+ }
+
+ if (NumMonitors > disp->MaxNumMonitors)
+ NumMonitors = disp->MaxNumMonitors;
+
+ Stream_Write_UINT32(s, MonitorLayoutSize); /* MonitorLayoutSize (4 bytes) */
+ Stream_Write_UINT32(s, NumMonitors); /* NumMonitors (4 bytes) */
+ WLog_DBG(TAG, "NumMonitors=%" PRIu32 "", NumMonitors);
+
+ for (UINT32 index = 0; index < NumMonitors; index++)
+ {
+ DISPLAY_CONTROL_MONITOR_LAYOUT current = Monitors[index];
+ current.Width -= (current.Width % 2);
+
+ if (current.Width < 200)
+ current.Width = 200;
+
+ if (current.Width > 8192)
+ current.Width = 8192;
+
+ if (current.Width % 2)
+ current.Width++;
+
+ if (current.Height < 200)
+ current.Height = 200;
+
+ if (current.Height > 8192)
+ current.Height = 8192;
+
+ Stream_Write_UINT32(s, current.Flags); /* Flags (4 bytes) */
+ Stream_Write_UINT32(s, current.Left); /* Left (4 bytes) */
+ Stream_Write_UINT32(s, current.Top); /* Top (4 bytes) */
+ Stream_Write_UINT32(s, current.Width); /* Width (4 bytes) */
+ Stream_Write_UINT32(s, current.Height); /* Height (4 bytes) */
+ Stream_Write_UINT32(s, current.PhysicalWidth); /* PhysicalWidth (4 bytes) */
+ Stream_Write_UINT32(s, current.PhysicalHeight); /* PhysicalHeight (4 bytes) */
+ Stream_Write_UINT32(s, current.Orientation); /* Orientation (4 bytes) */
+ Stream_Write_UINT32(s, current.DesktopScaleFactor); /* DesktopScaleFactor (4 bytes) */
+ Stream_Write_UINT32(s, current.DeviceScaleFactor); /* DeviceScaleFactor (4 bytes) */
+ WLog_DBG(TAG,
+ "\t%" PRIu32 " : Flags: 0x%08" PRIX32 " Left/Top: (%" PRId32 ",%" PRId32
+ ") W/H=%" PRIu32 "x%" PRIu32 ")",
+ index, current.Flags, current.Left, current.Top, current.Width, current.Height);
+ WLog_DBG(TAG,
+ "\t PhysicalWidth: %" PRIu32 " PhysicalHeight: %" PRIu32 " Orientation: %" PRIu32
+ "",
+ current.PhysicalWidth, current.PhysicalHeight, current.Orientation);
+ }
+
+out:
+ Stream_SealLength(s);
+ status = callback->channel->Write(callback->channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
+ NULL);
+ Stream_Free(s, TRUE);
+ return status;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT disp_recv_display_control_caps_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
+{
+ DISP_PLUGIN* disp = NULL;
+ DispClientContext* context = NULL;
+ UINT ret = CHANNEL_RC_OK;
+
+ WINPR_ASSERT(callback);
+ WINPR_ASSERT(s);
+
+ disp = (DISP_PLUGIN*)callback->plugin;
+ WINPR_ASSERT(disp);
+
+ context = disp->context;
+ WINPR_ASSERT(context);
+
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
+ return ERROR_INVALID_DATA;
+
+ Stream_Read_UINT32(s, disp->MaxNumMonitors); /* MaxNumMonitors (4 bytes) */
+ Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorA); /* MaxMonitorAreaFactorA (4 bytes) */
+ Stream_Read_UINT32(s, disp->MaxMonitorAreaFactorB); /* MaxMonitorAreaFactorB (4 bytes) */
+
+ if (context->DisplayControlCaps)
+ ret = context->DisplayControlCaps(context, disp->MaxNumMonitors,
+ disp->MaxMonitorAreaFactorA, disp->MaxMonitorAreaFactorB);
+
+ return ret;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT disp_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
+{
+ UINT32 error = 0;
+ DISPLAY_CONTROL_HEADER header = { 0 };
+
+ WINPR_ASSERT(callback);
+ WINPR_ASSERT(s);
+
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
+ return ERROR_INVALID_DATA;
+
+ if ((error = disp_read_header(s, &header)))
+ {
+ WLog_ERR(TAG, "disp_read_header failed with error %" PRIu32 "!", error);
+ return error;
+ }
+
+ if (!Stream_EnsureRemainingCapacity(s, header.length))
+ {
+ WLog_ERR(TAG, "not enough remaining data");
+ return ERROR_INVALID_DATA;
+ }
+
+ switch (header.type)
+ {
+ case DISPLAY_CONTROL_PDU_TYPE_CAPS:
+ return disp_recv_display_control_caps_pdu(callback, s);
+
+ default:
+ WLog_ERR(TAG, "Type %" PRIu32 " not recognized!", header.type);
+ return ERROR_INTERNAL_ERROR;
+ }
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT disp_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
+{
+ GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
+ return disp_recv_pdu(callback, data);
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT disp_on_close(IWTSVirtualChannelCallback* pChannelCallback)
+{
+ free(pChannelCallback);
+ return CHANNEL_RC_OK;
+}
+
+/**
+ * Channel Client Interface
+ */
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT disp_send_monitor_layout(DispClientContext* context, UINT32 NumMonitors,
+ DISPLAY_CONTROL_MONITOR_LAYOUT* Monitors)
+{
+ DISP_PLUGIN* disp = NULL;
+ GENERIC_CHANNEL_CALLBACK* callback = NULL;
+
+ WINPR_ASSERT(context);
+
+ disp = (DISP_PLUGIN*)context->handle;
+ WINPR_ASSERT(disp);
+
+ callback = disp->base.listener_callback->channel_callback;
+
+ return disp_send_display_control_monitor_layout_pdu(callback, NumMonitors, Monitors);
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT disp_plugin_initialize(GENERIC_DYNVC_PLUGIN* base, rdpContext* rcontext,
+ rdpSettings* settings)
+{
+ DispClientContext* context = NULL;
+ DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
+
+ WINPR_ASSERT(disp);
+ disp->MaxNumMonitors = 16;
+ disp->MaxMonitorAreaFactorA = 8192;
+ disp->MaxMonitorAreaFactorB = 8192;
+
+ context = (DispClientContext*)calloc(1, sizeof(DispClientContext));
+ if (!context)
+ {
+ WLog_Print(base->log, WLOG_ERROR, "unable to allocate DispClientContext");
+ return CHANNEL_RC_NO_MEMORY;
+ }
+
+ context->handle = (void*)disp;
+ context->SendMonitorLayout = disp_send_monitor_layout;
+
+ disp->base.iface.pInterface = disp->context = context;
+
+ return CHANNEL_RC_OK;
+}
+
+static void disp_plugin_terminated(GENERIC_DYNVC_PLUGIN* base)
+{
+ DISP_PLUGIN* disp = (DISP_PLUGIN*)base;
+
+ WINPR_ASSERT(disp);
+
+ free(disp->context);
+}
+
+static const IWTSVirtualChannelCallback disp_callbacks = { disp_on_data_received, NULL, /* Open */
+ disp_on_close, NULL };
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+FREERDP_ENTRY_POINT(UINT disp_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
+{
+ return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, DISP_DVC_CHANNEL_NAME,
+ sizeof(DISP_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
+ &disp_callbacks, disp_plugin_initialize,
+ disp_plugin_terminated);
+}
diff --git a/channels/disp/client/disp_main.h b/channels/disp/client/disp_main.h
new file mode 100644
index 0000000..d5ce446
--- /dev/null
+++ b/channels/disp/client/disp_main.h
@@ -0,0 +1,36 @@
+/**
+ * FreeRDP: A Remote Desktop Protocol Implementation
+ * Display Update Virtual Channel Extension
+ *
+ * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
+ * Copyright 2015 Thincast Technologies GmbH
+ * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.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.
+ */
+
+#ifndef FREERDP_CHANNEL_DISP_CLIENT_MAIN_H
+#define FREERDP_CHANNEL_DISP_CLIENT_MAIN_H
+
+#include <freerdp/config.h>
+
+#include <freerdp/dvc.h>
+#include <freerdp/types.h>
+#include <freerdp/addin.h>
+#include <freerdp/channels/log.h>
+
+#include <freerdp/client/disp.h>
+
+#define TAG CHANNELS_TAG("disp.client")
+
+#endif /* FREERDP_CHANNEL_DISP_CLIENT_MAIN_H */
diff --git a/channels/disp/disp_common.c b/channels/disp/disp_common.c
new file mode 100644
index 0000000..9387691
--- /dev/null
+++ b/channels/disp/disp_common.c
@@ -0,0 +1,55 @@
+/**
+ * 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.
+ */
+
+#include <freerdp/config.h>
+
+#include <winpr/crt.h>
+#include <winpr/stream.h>
+#include <freerdp/channels/log.h>
+
+#define TAG CHANNELS_TAG("disp.common")
+
+#include "disp_common.h"
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+UINT disp_read_header(wStream* s, DISPLAY_CONTROL_HEADER* header)
+{
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
+ return ERROR_INVALID_DATA;
+
+ Stream_Read_UINT32(s, header->type);
+ Stream_Read_UINT32(s, header->length);
+ return CHANNEL_RC_OK;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+UINT disp_write_header(wStream* s, const DISPLAY_CONTROL_HEADER* header)
+{
+ Stream_Write_UINT32(s, header->type);
+ Stream_Write_UINT32(s, header->length);
+ return CHANNEL_RC_OK;
+}
diff --git a/channels/disp/disp_common.h b/channels/disp/disp_common.h
new file mode 100644
index 0000000..386b8b3
--- /dev/null
+++ b/channels/disp/disp_common.h
@@ -0,0 +1,32 @@
+/**
+ * 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.
+ */
+
+#ifndef FREERDP_CHANNEL_DISP_COMMON_H
+#define FREERDP_CHANNEL_DISP_COMMON_H
+
+#include <winpr/crt.h>
+#include <winpr/stream.h>
+
+#include <freerdp/channels/disp.h>
+#include <freerdp/api.h>
+
+FREERDP_LOCAL UINT disp_read_header(wStream* s, DISPLAY_CONTROL_HEADER* header);
+FREERDP_LOCAL UINT disp_write_header(wStream* s, const DISPLAY_CONTROL_HEADER* header);
+
+#endif /* FREERDP_CHANNEL_DISP_COMMON_H */
diff --git a/channels/disp/server/CMakeLists.txt b/channels/disp/server/CMakeLists.txt
new file mode 100644
index 0000000..3c0e3b6
--- /dev/null
+++ b/channels/disp/server/CMakeLists.txt
@@ -0,0 +1,32 @@
+# FreeRDP: A Remote Desktop Protocol Implementation
+# FreeRDP cmake build script
+#
+# 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.
+
+define_channel_server("disp")
+
+set(${MODULE_PREFIX}_SRCS
+ disp_main.c
+ disp_main.h
+ ../disp_common.c
+ ../disp_common.h
+)
+
+set(${MODULE_PREFIX}_LIBS
+ freerdp
+)
+include_directories(..)
+
+add_channel_server_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} FALSE "DVCPluginEntry")
diff --git a/channels/disp/server/disp_main.c b/channels/disp/server/disp_main.c
new file mode 100644
index 0000000..2bb6e7b
--- /dev/null
+++ b/channels/disp/server/disp_main.c
@@ -0,0 +1,632 @@
+/**
+ * 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.
+ */
+
+#include <freerdp/config.h>
+
+#include "disp_main.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <winpr/crt.h>
+#include <winpr/assert.h>
+#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 = 0;
+ 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;
+ header.length = DISPLAY_CONTROL_HEADER_LENGTH + length;
+
+ if ((error = disp_write_header(s, &header)))
+ {
+ WLog_ERR(TAG, "Failed to write header with error %" PRIu32 "!", error);
+ goto error;
+ }
+
+ return s;
+error:
+ Stream_Free(s, TRUE);
+ return NULL;
+}
+
+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;
+ }
+}
+
+static BOOL disp_server_is_monitor_layout_valid(const DISPLAY_CONTROL_MONITOR_LAYOUT* monitor)
+{
+ WINPR_ASSERT(monitor);
+
+ if (monitor->Width < DISPLAY_CONTROL_MIN_MONITOR_WIDTH ||
+ monitor->Width > DISPLAY_CONTROL_MAX_MONITOR_WIDTH)
+ {
+ WLog_WARN(TAG, "Received invalid value for monitor->Width: %" PRIu32 "", monitor->Width);
+ return FALSE;
+ }
+
+ if (monitor->Height < DISPLAY_CONTROL_MIN_MONITOR_HEIGHT ||
+ monitor->Height > DISPLAY_CONTROL_MAX_MONITOR_HEIGHT)
+ {
+ WLog_WARN(TAG, "Received invalid value for monitor->Height: %" PRIu32 "", monitor->Width);
+ return FALSE;
+ }
+
+ switch (monitor->Orientation)
+ {
+ case ORIENTATION_LANDSCAPE:
+ case ORIENTATION_PORTRAIT:
+ case ORIENTATION_LANDSCAPE_FLIPPED:
+ case ORIENTATION_PORTRAIT_FLIPPED:
+ break;
+
+ default:
+ WLog_WARN(TAG, "Received incorrect value for monitor->Orientation: %" PRIu32 "",
+ monitor->Orientation);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static UINT disp_recv_display_control_monitor_layout_pdu(wStream* s, DispServerContext* context)
+{
+ UINT32 error = CHANNEL_RC_OK;
+ DISPLAY_CONTROL_MONITOR_LAYOUT_PDU pdu = { 0 };
+
+ WINPR_ASSERT(s);
+ WINPR_ASSERT(context);
+
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
+ return ERROR_INVALID_DATA;
+
+ Stream_Read_UINT32(s, pdu.MonitorLayoutSize); /* MonitorLayoutSize (4 bytes) */
+
+ if (pdu.MonitorLayoutSize != DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE)
+ {
+ WLog_ERR(TAG, "MonitorLayoutSize is set to %" PRIu32 ". expected %" PRIu32 "",
+ pdu.MonitorLayoutSize, DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE);
+ return ERROR_INVALID_DATA;
+ }
+
+ Stream_Read_UINT32(s, pdu.NumMonitors); /* NumMonitors (4 bytes) */
+
+ if (pdu.NumMonitors > context->MaxNumMonitors)
+ {
+ WLog_ERR(TAG, "NumMonitors (%" PRIu32 ")> server MaxNumMonitors (%" PRIu32 ")",
+ pdu.NumMonitors, context->MaxNumMonitors);
+ return ERROR_INVALID_DATA;
+ }
+
+ if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, pdu.NumMonitors,
+ DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE))
+ return ERROR_INVALID_DATA;
+
+ pdu.Monitors = (DISPLAY_CONTROL_MONITOR_LAYOUT*)calloc(pdu.NumMonitors,
+ sizeof(DISPLAY_CONTROL_MONITOR_LAYOUT));
+
+ if (!pdu.Monitors)
+ {
+ WLog_ERR(TAG, "disp_recv_display_control_monitor_layout_pdu(): calloc failed!");
+ return CHANNEL_RC_NO_MEMORY;
+ }
+
+ WLog_DBG(TAG, "disp_recv_display_control_monitor_layout_pdu: NumMonitors=%" PRIu32 "",
+ pdu.NumMonitors);
+
+ for (UINT32 index = 0; index < pdu.NumMonitors; index++)
+ {
+ DISPLAY_CONTROL_MONITOR_LAYOUT* monitor = &(pdu.Monitors[index]);
+
+ 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) */
+ Stream_Read_UINT32(s, monitor->DesktopScaleFactor); /* DesktopScaleFactor (4 bytes) */
+ Stream_Read_UINT32(s, monitor->DeviceScaleFactor); /* DeviceScaleFactor (4 bytes) */
+
+ disp_server_sanitize_monitor_layout(monitor);
+ WLog_DBG(TAG,
+ "\t%" PRIu32 " : Flags: 0x%08" PRIX32 " Left/Top: (%" PRId32 ",%" PRId32
+ ") W/H=%" PRIu32 "x%" PRIu32 ")",
+ index, monitor->Flags, monitor->Left, monitor->Top, monitor->Width,
+ monitor->Height);
+ WLog_DBG(TAG,
+ "\t PhysicalWidth: %" PRIu32 " PhysicalHeight: %" PRIu32 " Orientation: %" PRIu32
+ "",
+ monitor->PhysicalWidth, monitor->PhysicalHeight, monitor->Orientation);
+
+ 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 = 0;
+ size_t end = 0;
+ DISPLAY_CONTROL_HEADER header = { 0 };
+
+ WINPR_ASSERT(s);
+ WINPR_ASSERT(context);
+
+ beg = Stream_GetPosition(s);
+
+ if ((error = disp_read_header(s, &header)))
+ {
+ WLog_ERR(TAG, "disp_read_header failed with error %" PRIu32 "!", error);
+ return error;
+ }
+
+ switch (header.type)
+ {
+ case DISPLAY_CONTROL_PDU_TYPE_MONITOR_LAYOUT:
+ if ((error = disp_recv_display_control_monitor_layout_pdu(s, context)))
+ WLog_ERR(TAG,
+ "disp_recv_display_control_monitor_layout_pdu "
+ "failed with error %" PRIu32 "!",
+ error);
+
+ break;
+
+ default:
+ error = CHANNEL_RC_BAD_PROC;
+ WLog_WARN(TAG, "Received unknown PDU type: %" PRIu32 "", header.type);
+ break;
+ }
+
+ end = Stream_GetPosition(s);
+
+ if (end != (beg + header.length))
+ {
+ WLog_ERR(TAG, "Unexpected DISP pdu end: Actual: %" PRIuz ", Expected: %" PRIuz "", end,
+ (beg + header.length));
+ Stream_SetPosition(s, (beg + header.length));
+ }
+
+ return error;
+}
+
+static UINT disp_server_handle_messages(DispServerContext* context)
+{
+ DWORD BytesReturned = 0;
+ void* buffer = NULL;
+ UINT ret = CHANNEL_RC_OK;
+ DispServerPrivate* priv = NULL;
+ wStream* s = NULL;
+
+ WINPR_ASSERT(context);
+
+ priv = context->priv;
+ WINPR_ASSERT(priv);
+
+ s = priv->input_stream;
+ WINPR_ASSERT(s);
+
+ /* Check whether the dynamic channel is ready */
+ if (!priv->isReady)
+ {
+ if (WTSVirtualChannelQuery(priv->disp_channel, WTSVirtualChannelReady, &buffer,
+ &BytesReturned) == FALSE)
+ {
+ if (GetLastError() == ERROR_NO_DATA)
+ return ERROR_NO_DATA;
+
+ WLog_ERR(TAG, "WTSVirtualChannelQuery failed");
+ return ERROR_INTERNAL_ERROR;
+ }
+
+ priv->isReady = *((BOOL*)buffer);
+ WTSFreeMemory(buffer);
+ }
+
+ /* Consume channel event only after the disp dynamic channel is ready */
+ Stream_SetPosition(s, 0);
+
+ if (!WTSVirtualChannelRead(priv->disp_channel, 0, NULL, 0, &BytesReturned))
+ {
+ 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;
+ }
+
+ if (WTSVirtualChannelRead(priv->disp_channel, 0, (PCHAR)Stream_Buffer(s), Stream_Capacity(s),
+ &BytesReturned) == FALSE)
+ {
+ 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)))
+ {
+ WLog_ERR(TAG,
+ "disp_server_receive_pdu "
+ "failed with error %" PRIu32 "!",
+ ret);
+ return ret;
+ }
+ }
+
+ return ret;
+}
+
+static DWORD WINAPI disp_server_thread_func(LPVOID arg)
+{
+ DispServerContext* context = (DispServerContext*)arg;
+ DispServerPrivate* priv = NULL;
+ DWORD status = 0;
+ DWORD nCount = 0;
+ HANDLE events[8] = { 0 };
+ UINT error = CHANNEL_RC_OK;
+
+ WINPR_ASSERT(context);
+
+ priv = context->priv;
+ WINPR_ASSERT(priv);
+
+ 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();
+ WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
+ break;
+ }
+
+ /* Stop Event */
+ if (status == WAIT_OBJECT_0)
+ break;
+
+ if ((error = disp_server_handle_messages(context)))
+ {
+ WLog_ERR(TAG, "disp_server_handle_messages failed with error %" PRIu32 "", error);
+ 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;
+ DispServerPrivate* priv = NULL;
+ DWORD BytesReturned = 0;
+ PULONG pSessionId = NULL;
+ void* buffer = NULL;
+ UINT32 channelId = 0;
+ BOOL status = TRUE;
+
+ WINPR_ASSERT(context);
+
+ priv = context->priv;
+ WINPR_ASSERT(priv);
+
+ priv->SessionId = WTS_CURRENT_SESSION;
+
+ if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
+ (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
+ {
+ WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
+ rc = ERROR_INTERNAL_ERROR;
+ goto out_close;
+ }
+
+ priv->SessionId = (DWORD)*pSessionId;
+ WTSFreeMemory(pSessionId);
+ priv->disp_channel = (HANDLE)WTSVirtualChannelOpenEx(priv->SessionId, DISP_DVC_CHANNEL_NAME,
+ WTS_CHANNEL_OPTION_DYNAMIC);
+
+ if (!priv->disp_channel)
+ {
+ WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed!");
+ rc = GetLastError();
+ goto out_close;
+ }
+
+ 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;
+ }
+
+ /* Query for channel event handle */
+ if (!WTSVirtualChannelQuery(priv->disp_channel, WTSVirtualEventHandle, &buffer,
+ &BytesReturned) ||
+ (BytesReturned != sizeof(HANDLE)))
+ {
+ WLog_ERR(TAG,
+ "WTSVirtualChannelQuery failed "
+ "or invalid returned size(%" PRIu32 ")",
+ 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;
+ goto out_close;
+ }
+
+ if (!(priv->thread =
+ CreateThread(NULL, 0, disp_server_thread_func, (void*)context, 0, NULL)))
+ {
+ WLog_ERR(TAG, "CreateEvent failed!");
+ CloseHandle(priv->stopEvent);
+ priv->stopEvent = NULL;
+ rc = ERROR_INTERNAL_ERROR;
+ goto out_close;
+ }
+ }
+
+ 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 = 0;
+ ULONG written = 0;
+
+ WINPR_ASSERT(context);
+ WINPR_ASSERT(s);
+
+ if (!WTSVirtualChannelWrite(context->priv->disp_channel, (PCHAR)Stream_Buffer(s),
+ Stream_GetPosition(s), &written))
+ {
+ WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
+ ret = ERROR_INTERNAL_ERROR;
+ goto out;
+ }
+
+ if (written < Stream_GetPosition(s))
+ {
+ WLog_WARN(TAG, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
+ Stream_GetPosition(s));
+ }
+
+ 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)
+{
+ wStream* s = NULL;
+
+ WINPR_ASSERT(context);
+
+ s = disp_server_single_packet_new(DISPLAY_CONTROL_PDU_TYPE_CAPS, 12);
+
+ if (!s)
+ {
+ WLog_ERR(TAG, "disp_server_single_packet_new failed!");
+ return CHANNEL_RC_NO_MEMORY;
+ }
+
+ Stream_Write_UINT32(s, context->MaxNumMonitors); /* MaxNumMonitors (4 bytes) */
+ 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;
+ DispServerPrivate* priv = NULL;
+
+ WINPR_ASSERT(context);
+
+ priv = context->priv;
+ WINPR_ASSERT(priv);
+
+ if (priv->thread)
+ {
+ SetEvent(priv->stopEvent);
+
+ if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
+ {
+ error = GetLastError();
+ WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
+ return error;
+ }
+
+ CloseHandle(priv->thread);
+ CloseHandle(priv->stopEvent);
+ priv->thread = NULL;
+ priv->stopEvent = NULL;
+ }
+
+ if (priv->disp_channel)
+ {
+ WTSVirtualChannelClose(priv->disp_channel);
+ priv->disp_channel = NULL;
+ }
+
+ return error;
+}
+
+DispServerContext* disp_server_context_new(HANDLE vcm)
+{
+ DispServerContext* context = NULL;
+ DispServerPrivate* priv = NULL;
+ context = (DispServerContext*)calloc(1, sizeof(DispServerContext));
+
+ if (!context)
+ {
+ WLog_ERR(TAG, "disp_server_context_new(): calloc DispServerContext failed!");
+ goto fail;
+ }
+
+ priv = context->priv = (DispServerPrivate*)calloc(1, sizeof(DispServerPrivate));
+
+ if (!context->priv)
+ {
+ WLog_ERR(TAG, "disp_server_context_new(): calloc DispServerPrivate failed!");
+ goto fail;
+ }
+
+ priv->input_stream = Stream_New(NULL, 4);
+
+ if (!priv->input_stream)
+ {
+ WLog_ERR(TAG, "Stream_New failed!");
+ goto fail;
+ }
+
+ 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;
+fail:
+ WINPR_PRAGMA_DIAG_PUSH
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
+ disp_server_context_free(context);
+ WINPR_PRAGMA_DIAG_POP
+ return NULL;
+}
+
+void disp_server_context_free(DispServerContext* context)
+{
+ if (!context)
+ return;
+
+ if (context->priv)
+ {
+ disp_server_close(context);
+ Stream_Free(context->priv->input_stream, TRUE);
+ free(context->priv);
+ }
+
+ free(context);
+}
diff --git a/channels/disp/server/disp_main.h b/channels/disp/server/disp_main.h
new file mode 100644
index 0000000..920a508
--- /dev/null
+++ b/channels/disp/server/disp_main.h
@@ -0,0 +1,37 @@
+/**
+ * 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.
+ */
+
+#ifndef FREERDP_CHANNEL_DISP_SERVER_MAIN_H
+#define FREERDP_CHANNEL_DISP_SERVER_MAIN_H
+
+#include <freerdp/server/disp.h>
+
+struct s_disp_server_private
+{
+ BOOL isReady;
+ wStream* input_stream;
+ HANDLE channelEvent;
+ HANDLE thread;
+ HANDLE stopEvent;
+ DWORD SessionId;
+
+ void* disp_channel;
+};
+
+#endif /* FREERDP_CHANNEL_DISP_SERVER_MAIN_H */