FreeRDP
Loading...
Searching...
No Matches
server/disp_main.c
1
19
20#include <freerdp/config.h>
21
22#include "disp_main.h"
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <winpr/crt.h>
28#include <winpr/assert.h>
29#include <winpr/synch.h>
30#include <winpr/thread.h>
31#include <winpr/stream.h>
32#include <winpr/sysinfo.h>
33#include <freerdp/channels/wtsvc.h>
34#include <freerdp/channels/log.h>
35
36#include <freerdp/server/disp.h>
37#include "../disp_common.h"
38
39#define TAG CHANNELS_TAG("rdpedisp.server")
40
46
47static wStream* disp_server_single_packet_new(UINT32 type, UINT32 length)
48{
49 UINT error = 0;
51 wStream* s = Stream_New(nullptr, DISPLAY_CONTROL_HEADER_LENGTH + length);
52
53 if (!s)
54 {
55 WLog_ERR(TAG, "Stream_New failed!");
56 goto error;
57 }
58
59 header.type = type;
60 header.length = DISPLAY_CONTROL_HEADER_LENGTH + length;
61
62 if ((error = disp_write_header(s, &header)))
63 {
64 WLog_ERR(TAG, "Failed to write header with error %" PRIu32 "!", error);
65 goto error;
66 }
67
68 return s;
69error:
70 Stream_Free(s, TRUE);
71 return nullptr;
72}
73
74static void disp_server_sanitize_monitor_layout(DISPLAY_CONTROL_MONITOR_LAYOUT* monitor)
75{
76 if (monitor->PhysicalWidth < DISPLAY_CONTROL_MIN_PHYSICAL_MONITOR_WIDTH ||
77 monitor->PhysicalWidth > DISPLAY_CONTROL_MAX_PHYSICAL_MONITOR_WIDTH ||
78 monitor->PhysicalHeight < DISPLAY_CONTROL_MIN_PHYSICAL_MONITOR_HEIGHT ||
79 monitor->PhysicalHeight > DISPLAY_CONTROL_MAX_PHYSICAL_MONITOR_HEIGHT)
80 {
81 if (monitor->PhysicalWidth != 0 || monitor->PhysicalHeight != 0)
82 WLog_DBG(
83 TAG,
84 "Sanitizing invalid physical monitor size. Old physical monitor size: [%" PRIu32
85 ", %" PRIu32 "]",
86 monitor->PhysicalWidth, monitor->PhysicalHeight);
87
88 monitor->PhysicalWidth = monitor->PhysicalHeight = 0;
89 }
90}
91
92static BOOL disp_server_is_monitor_layout_valid(const DISPLAY_CONTROL_MONITOR_LAYOUT* monitor)
93{
94 WINPR_ASSERT(monitor);
95
96 if (monitor->Width < DISPLAY_CONTROL_MIN_MONITOR_WIDTH ||
97 monitor->Width > DISPLAY_CONTROL_MAX_MONITOR_WIDTH)
98 {
99 WLog_WARN(TAG, "Received invalid value for monitor->Width: %" PRIu32 "", monitor->Width);
100 return FALSE;
101 }
102
103 if (monitor->Height < DISPLAY_CONTROL_MIN_MONITOR_HEIGHT ||
104 monitor->Height > DISPLAY_CONTROL_MAX_MONITOR_HEIGHT)
105 {
106 WLog_WARN(TAG, "Received invalid value for monitor->Height: %" PRIu32 "", monitor->Height);
107 return FALSE;
108 }
109
110 switch (monitor->Orientation)
111 {
112 case ORIENTATION_LANDSCAPE:
113 case ORIENTATION_PORTRAIT:
114 case ORIENTATION_LANDSCAPE_FLIPPED:
115 case ORIENTATION_PORTRAIT_FLIPPED:
116 break;
117
118 default:
119 WLog_WARN(TAG, "Received incorrect value for monitor->Orientation: %" PRIu32 "",
120 monitor->Orientation);
121 return FALSE;
122 }
123
124 return TRUE;
125}
126
127static UINT disp_recv_display_control_monitor_layout_pdu(wStream* s, DispServerContext* context)
128{
129 UINT32 error = CHANNEL_RC_OK;
130 DISPLAY_CONTROL_MONITOR_LAYOUT_PDU pdu = WINPR_C_ARRAY_INIT;
131
132 WINPR_ASSERT(s);
133 WINPR_ASSERT(context);
134
135 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
136 return ERROR_INVALID_DATA;
137
138 Stream_Read_UINT32(s, pdu.MonitorLayoutSize); /* MonitorLayoutSize (4 bytes) */
139
140 if (pdu.MonitorLayoutSize != DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE)
141 {
142 WLog_ERR(TAG, "MonitorLayoutSize is set to %" PRIu32 ". expected %d", pdu.MonitorLayoutSize,
143 DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE);
144 return ERROR_INVALID_DATA;
145 }
146
147 Stream_Read_UINT32(s, pdu.NumMonitors); /* NumMonitors (4 bytes) */
148
149 if (pdu.NumMonitors > context->MaxNumMonitors)
150 {
151 WLog_ERR(TAG, "NumMonitors (%" PRIu32 ")> server MaxNumMonitors (%" PRIu32 ")",
152 pdu.NumMonitors, context->MaxNumMonitors);
153 return ERROR_INVALID_DATA;
154 }
155
156 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, pdu.NumMonitors,
157 DISPLAY_CONTROL_MONITOR_LAYOUT_SIZE))
158 return ERROR_INVALID_DATA;
159
160 pdu.Monitors = (DISPLAY_CONTROL_MONITOR_LAYOUT*)calloc(pdu.NumMonitors,
162
163 if (!pdu.Monitors)
164 {
165 WLog_ERR(TAG, "disp_recv_display_control_monitor_layout_pdu(): calloc failed!");
166 return CHANNEL_RC_NO_MEMORY;
167 }
168
169 WLog_DBG(TAG, "disp_recv_display_control_monitor_layout_pdu: NumMonitors=%" PRIu32 "",
170 pdu.NumMonitors);
171
172 for (UINT32 index = 0; index < pdu.NumMonitors; index++)
173 {
174 DISPLAY_CONTROL_MONITOR_LAYOUT* monitor = &(pdu.Monitors[index]);
175
176 Stream_Read_UINT32(s, monitor->Flags); /* Flags (4 bytes) */
177 Stream_Read_INT32(s, monitor->Left); /* Left (4 bytes) */
178 Stream_Read_INT32(s, monitor->Top); /* Top (4 bytes) */
179 Stream_Read_UINT32(s, monitor->Width); /* Width (4 bytes) */
180 Stream_Read_UINT32(s, monitor->Height); /* Height (4 bytes) */
181 Stream_Read_UINT32(s, monitor->PhysicalWidth); /* PhysicalWidth (4 bytes) */
182 Stream_Read_UINT32(s, monitor->PhysicalHeight); /* PhysicalHeight (4 bytes) */
183 Stream_Read_UINT32(s, monitor->Orientation); /* Orientation (4 bytes) */
184 Stream_Read_UINT32(s, monitor->DesktopScaleFactor); /* DesktopScaleFactor (4 bytes) */
185 Stream_Read_UINT32(s, monitor->DeviceScaleFactor); /* DeviceScaleFactor (4 bytes) */
186
187 disp_server_sanitize_monitor_layout(monitor);
188 WLog_DBG(TAG,
189 "\t%" PRIu32 " : Flags: 0x%08" PRIX32 " Left/Top: (%" PRId32 ",%" PRId32
190 ") W/H=%" PRIu32 "x%" PRIu32 ")",
191 index, monitor->Flags, monitor->Left, monitor->Top, monitor->Width,
192 monitor->Height);
193 WLog_DBG(TAG,
194 "\t PhysicalWidth: %" PRIu32 " PhysicalHeight: %" PRIu32 " Orientation: %" PRIu32
195 "",
196 monitor->PhysicalWidth, monitor->PhysicalHeight, monitor->Orientation);
197
198 if (!disp_server_is_monitor_layout_valid(monitor))
199 {
200 error = ERROR_INVALID_DATA;
201 goto out;
202 }
203 }
204
205 if (context)
206 IFCALLRET(context->DispMonitorLayout, error, context, &pdu);
207
208out:
209 free(pdu.Monitors);
210 return error;
211}
212
213static UINT disp_server_receive_pdu(DispServerContext* context, wStream* data)
214{
215 UINT error = CHANNEL_RC_OK;
216 DISPLAY_CONTROL_HEADER header = WINPR_C_ARRAY_INIT;
217
218 WINPR_ASSERT(data);
219 WINPR_ASSERT(context);
220
221 size_t packetLen = 0;
222 if ((error = disp_read_header(data, &header, &packetLen)))
223 {
224 WLog_ERR(TAG, "disp_read_header failed with error %" PRIu32 "!", error);
225 return error;
226 }
227
228 wStream sbuffer = WINPR_C_ARRAY_INIT;
229 wStream* s = Stream_StaticConstInit(&sbuffer, Stream_Pointer(data), packetLen);
230 Stream_Seek(data, packetLen);
231
232 switch (header.type)
233 {
234 case DISPLAY_CONTROL_PDU_TYPE_MONITOR_LAYOUT:
235 if ((error = disp_recv_display_control_monitor_layout_pdu(s, context)))
236 WLog_ERR(TAG,
237 "disp_recv_display_control_monitor_layout_pdu "
238 "failed with error %" PRIu32 "!",
239 error);
240
241 break;
242
243 default:
244 error = CHANNEL_RC_BAD_PROC;
245 WLog_WARN(TAG, "Received unknown PDU type: %" PRIu32 "", header.type);
246 break;
247 }
248
249 const size_t end = Stream_GetPosition(s);
250
251 if (end != packetLen)
252 {
253 WLog_ERR(TAG, "Unexpected DISP pdu end: Actual: %" PRIuz ", Expected: %" PRIuz "", end,
254 packetLen);
255 }
256
257 return error;
258}
259
260static UINT disp_server_handle_messages(DispServerContext* context)
261{
262 DWORD BytesReturned = 0;
263 void* buffer = nullptr;
264 UINT ret = CHANNEL_RC_OK;
265 DispServerPrivate* priv = nullptr;
266 wStream* s = nullptr;
267
268 WINPR_ASSERT(context);
269
270 priv = context->priv;
271 WINPR_ASSERT(priv);
272
273 s = priv->input_stream;
274 WINPR_ASSERT(s);
275
276 /* Check whether the dynamic channel is ready */
277 if (!priv->isReady)
278 {
279 if (WTSVirtualChannelQuery(priv->disp_channel, WTSVirtualChannelReady, &buffer,
280 &BytesReturned) == FALSE)
281 {
282 if (GetLastError() == ERROR_NO_DATA)
283 return ERROR_NO_DATA;
284
285 WLog_ERR(TAG, "WTSVirtualChannelQuery failed");
286 return ERROR_INTERNAL_ERROR;
287 }
288
289 priv->isReady = *((BOOL*)buffer);
290 WTSFreeMemory(buffer);
291 }
292
293 /* Consume channel event only after the disp dynamic channel is ready */
294 Stream_ResetPosition(s);
295
296 if (!WTSVirtualChannelRead(priv->disp_channel, 0, nullptr, 0, &BytesReturned))
297 {
298 if (GetLastError() == ERROR_NO_DATA)
299 return ERROR_NO_DATA;
300
301 WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
302 return ERROR_INTERNAL_ERROR;
303 }
304
305 if (BytesReturned < 1)
306 return CHANNEL_RC_OK;
307
308 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
309 {
310 WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
311 return CHANNEL_RC_NO_MEMORY;
312 }
313
314 const size_t cap = Stream_Capacity(s);
315 if (cap > UINT32_MAX)
316 return CHANNEL_RC_NO_BUFFER;
317
318 if (WTSVirtualChannelRead(priv->disp_channel, 0, Stream_BufferAs(s, char), (ULONG)cap,
319 &BytesReturned) == FALSE)
320 {
321 WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
322 return ERROR_INTERNAL_ERROR;
323 }
324
325 if (!Stream_SetLength(s, BytesReturned))
326 return ERROR_INTERNAL_ERROR;
327
328 Stream_ResetPosition(s);
329
330 while (Stream_GetPosition(s) < Stream_Length(s))
331 {
332 if ((ret = disp_server_receive_pdu(context, s)))
333 {
334 WLog_ERR(TAG,
335 "disp_server_receive_pdu "
336 "failed with error %" PRIu32 "!",
337 ret);
338 return ret;
339 }
340 }
341
342 return ret;
343}
344
345static DWORD WINAPI disp_server_thread_func(LPVOID arg)
346{
347 DispServerContext* context = (DispServerContext*)arg;
348 DispServerPrivate* priv = nullptr;
349 DWORD status = 0;
350 DWORD nCount = 0;
351 HANDLE events[8] = WINPR_C_ARRAY_INIT;
352 UINT error = CHANNEL_RC_OK;
353
354 WINPR_ASSERT(context);
355
356 priv = context->priv;
357 WINPR_ASSERT(priv);
358
359 /* Wait for the client to take the channel. It says nothing on it until it has
360 * the caps, so the channel event would not fire. */
361 while (!priv->isReady)
362 {
363 void* buffer = nullptr;
364 DWORD BytesReturned = 0;
365
366 if (WaitForSingleObject(priv->stopEvent, 100) == WAIT_OBJECT_0)
367 goto out;
368
369 if (!WTSVirtualChannelQuery(priv->disp_channel, WTSVirtualChannelReady, &buffer,
370 &BytesReturned))
371 {
372 WLog_INFO(TAG, "the client did not take the channel");
373 goto out;
374 }
375
376 priv->isReady = *((BOOL*)buffer);
377 WTSFreeMemory(buffer);
378 }
379
380 error = context->DisplayControlCaps(context);
381 if (error != CHANNEL_RC_OK)
382 {
383 WLog_ERR(TAG, "DisplayControlCaps failed with error %" PRIu32 "", error);
384 goto out;
385 }
386
387 events[nCount++] = priv->stopEvent;
388 events[nCount++] = priv->channelEvent;
389
390 /* Main virtual channel loop. RDPEDISP do not need version negotiation */
391 while (TRUE)
392 {
393 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
394
395 if (status == WAIT_FAILED)
396 {
397 error = GetLastError();
398 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
399 break;
400 }
401
402 /* Stop Event */
403 if (status == WAIT_OBJECT_0)
404 break;
405
406 if ((error = disp_server_handle_messages(context)))
407 {
408 WLog_ERR(TAG, "disp_server_handle_messages failed with error %" PRIu32 "", error);
409 break;
410 }
411 }
412
413out:
414 ExitThread(error);
415 return error;
416}
417
423static UINT disp_server_open(DispServerContext* context)
424{
425 UINT rc = ERROR_INTERNAL_ERROR;
426 DispServerPrivate* priv = nullptr;
427 DWORD BytesReturned = 0;
428 PULONG pSessionId = nullptr;
429 void* buffer = nullptr;
430 UINT32 channelId = 0;
431 BOOL status = TRUE;
432
433 WINPR_ASSERT(context);
434
435 priv = context->priv;
436 WINPR_ASSERT(priv);
437
438 priv->SessionId = WTS_CURRENT_SESSION;
439
440 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
441 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
442 {
443 WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
444 rc = ERROR_INTERNAL_ERROR;
445 goto out_close;
446 }
447
448 priv->SessionId = (DWORD)*pSessionId;
449 WTSFreeMemory(pSessionId);
450 priv->disp_channel =
451 WTSVirtualChannelOpenEx(priv->SessionId, DISP_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC);
452
453 if (!priv->disp_channel)
454 {
455 WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed!");
456 rc = GetLastError();
457 goto out_close;
458 }
459
460 channelId = WTSChannelGetIdByHandle(priv->disp_channel);
461
462 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
463 if (!status)
464 {
465 WLog_ERR(TAG, "context->ChannelIdAssigned failed!");
466 rc = ERROR_INTERNAL_ERROR;
467 goto out_close;
468 }
469
470 /* Query for channel event handle */
471 if (!WTSVirtualChannelQuery(priv->disp_channel, WTSVirtualEventHandle, &buffer,
472 &BytesReturned) ||
473 (BytesReturned != sizeof(HANDLE)))
474 {
475 WLog_ERR(TAG,
476 "WTSVirtualChannelQuery failed "
477 "or invalid returned size(%" PRIu32 ")",
478 BytesReturned);
479
480 if (buffer)
481 WTSFreeMemory(buffer);
482
483 rc = ERROR_INTERNAL_ERROR;
484 goto out_close;
485 }
486
487 priv->channelEvent = *(HANDLE*)buffer;
488 WTSFreeMemory(buffer);
489
490 if (priv->thread == nullptr)
491 {
492 if (!(priv->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
493 {
494 WLog_ERR(TAG, "CreateEvent failed!");
495 rc = ERROR_INTERNAL_ERROR;
496 goto out_close;
497 }
498
499 if (!(priv->thread =
500 CreateThread(nullptr, 0, disp_server_thread_func, (void*)context, 0, nullptr)))
501 {
502 WLog_ERR(TAG, "CreateEvent failed!");
503 (void)CloseHandle(priv->stopEvent);
504 priv->stopEvent = nullptr;
505 rc = ERROR_INTERNAL_ERROR;
506 goto out_close;
507 }
508 }
509
510 return CHANNEL_RC_OK;
511out_close:
512 (void)WTSVirtualChannelClose(priv->disp_channel);
513 priv->disp_channel = nullptr;
514 priv->channelEvent = nullptr;
515 return rc;
516}
517
518static UINT disp_server_packet_send(DispServerContext* context, wStream* s)
519{
520 UINT ret = 0;
521 ULONG written = 0;
522
523 WINPR_ASSERT(context);
524 WINPR_ASSERT(s);
525
526 const size_t pos = Stream_GetPosition(s);
527
528 WINPR_ASSERT(pos <= UINT32_MAX);
529 if (!WTSVirtualChannelWrite(context->priv->disp_channel, Stream_BufferAs(s, char), (UINT32)pos,
530 &written))
531 {
532 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
533 ret = ERROR_INTERNAL_ERROR;
534 goto out;
535 }
536
537 if (written < Stream_GetPosition(s))
538 {
539 WLog_WARN(TAG, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
540 Stream_GetPosition(s));
541 }
542
543 ret = CHANNEL_RC_OK;
544out:
545 Stream_Free(s, TRUE);
546 return ret;
547}
548
554static UINT disp_server_send_caps_pdu(DispServerContext* context)
555{
556 wStream* s = nullptr;
557
558 WINPR_ASSERT(context);
559
560 s = disp_server_single_packet_new(DISPLAY_CONTROL_PDU_TYPE_CAPS, 12);
561
562 if (!s)
563 {
564 WLog_ERR(TAG, "disp_server_single_packet_new failed!");
565 return CHANNEL_RC_NO_MEMORY;
566 }
567
568 Stream_Write_UINT32(s, context->MaxNumMonitors); /* MaxNumMonitors (4 bytes) */
569 Stream_Write_UINT32(s, context->MaxMonitorAreaFactorA); /* MaxMonitorAreaFactorA (4 bytes) */
570 Stream_Write_UINT32(s, context->MaxMonitorAreaFactorB); /* MaxMonitorAreaFactorB (4 bytes) */
571 return disp_server_packet_send(context, s);
572}
573
579static UINT disp_server_close(DispServerContext* context)
580{
581 UINT error = CHANNEL_RC_OK;
582 DispServerPrivate* priv = nullptr;
583
584 WINPR_ASSERT(context);
585
586 priv = context->priv;
587 WINPR_ASSERT(priv);
588
589 if (priv->thread)
590 {
591 (void)SetEvent(priv->stopEvent);
592
593 if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
594 {
595 error = GetLastError();
596 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
597 return error;
598 }
599
600 (void)CloseHandle(priv->thread);
601 (void)CloseHandle(priv->stopEvent);
602 priv->thread = nullptr;
603 priv->stopEvent = nullptr;
604 }
605
606 if (priv->disp_channel)
607 {
608 (void)WTSVirtualChannelClose(priv->disp_channel);
609 priv->disp_channel = nullptr;
610 }
611
612 return error;
613}
614
615DispServerContext* disp_server_context_new(HANDLE vcm)
616{
617 DispServerContext* context = nullptr;
618 DispServerPrivate* priv = nullptr;
619 context = (DispServerContext*)calloc(1, sizeof(DispServerContext));
620
621 if (!context)
622 {
623 WLog_ERR(TAG, "disp_server_context_new(): calloc DispServerContext failed!");
624 goto fail;
625 }
626
627 priv = context->priv = (DispServerPrivate*)calloc(1, sizeof(DispServerPrivate));
628
629 if (!context->priv)
630 {
631 WLog_ERR(TAG, "disp_server_context_new(): calloc DispServerPrivate failed!");
632 goto fail;
633 }
634
635 priv->input_stream = Stream_New(nullptr, 4);
636
637 if (!priv->input_stream)
638 {
639 WLog_ERR(TAG, "Stream_New failed!");
640 goto fail;
641 }
642
643 context->vcm = vcm;
644 context->Open = disp_server_open;
645 context->Close = disp_server_close;
646 context->DisplayControlCaps = disp_server_send_caps_pdu;
647 priv->isReady = FALSE;
648 return context;
649fail:
650 WINPR_PRAGMA_DIAG_PUSH
651 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
652 disp_server_context_free(context);
653 WINPR_PRAGMA_DIAG_POP
654 return nullptr;
655}
656
657void disp_server_context_free(DispServerContext* context)
658{
659 if (!context)
660 return;
661
662 if (context->priv)
663 {
664 disp_server_close(context);
665 Stream_Free(context->priv->input_stream, TRUE);
666 free(context->priv);
667 }
668
669 free(context);
670}