FreeRDP
Loading...
Searching...
No Matches
libfreerdp/core/server.c
1
21
22#include <freerdp/config.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdint.h>
28
29#include <winpr/atexit.h>
30#include <winpr/wtypes.h>
31#include <winpr/crt.h>
32#include <winpr/synch.h>
33#include <winpr/stream.h>
34#include <winpr/assert.h>
35#include <winpr/cast.h>
36
37#include <freerdp/log.h>
38#include <freerdp/constants.h>
39#include <freerdp/server/channels.h>
40#include <freerdp/channels/drdynvc.h>
41#include <freerdp/utils/drdynvc.h>
42
43#include "rdp.h"
44
45#include "server.h"
46#include "server_tunnel.h"
47
48#define TAG FREERDP_TAG("core.server")
49#ifdef WITH_DEBUG_DVC
50#define DEBUG_DVC(...) WLog_DBG(TAG, __VA_ARGS__)
51#else
52#define DEBUG_DVC(...) \
53 do \
54 { \
55 } while (0)
56#endif
57
58#define DVC_MAX_DATA_PDU_SIZE 1600
59
60typedef struct
61{
62 UINT16 channelId;
63 UINT16 reserved;
64 UINT32 length;
65 UINT32 offset;
66} wtsChannelMessage;
67
68static const DWORD g_err_oom = WINPR_CXX_COMPAT_CAST(DWORD, E_OUTOFMEMORY);
69
70static DWORD g_SessionId = 1;
71static wHashTable* g_ServerHandles = nullptr;
72static INIT_ONCE g_HandleInitializer = INIT_ONCE_STATIC_INIT;
73
74static rdpPeerChannel* wts_get_dvc_channel_by_id(WTSVirtualChannelManager* vcm, UINT32 ChannelId)
75{
76 WINPR_ASSERT(vcm);
77 return HashTable_GetItemValue(vcm->dynamicVirtualChannels, &ChannelId);
78}
79
80static BOOL wts_queue_receive_data(rdpPeerChannel* channel, const void* Buffer1, size_t Length1,
81 const void* Buffer2, size_t Length2)
82{
83 WINPR_ASSERT(channel);
84
85 if (Length1 > UINT32_MAX - sizeof(wtsChannelMessage))
86 return FALSE;
87 if (Length2 > UINT32_MAX - sizeof(wtsChannelMessage) - Length1)
88 return FALSE;
89 const size_t len = Length1 + Length2;
90
91 /* Ignore empty messages. */
92 if (len == 0)
93 return TRUE;
94 const size_t tlen = sizeof(wtsChannelMessage) + len;
95 wtsChannelMessage* messageCtx = (wtsChannelMessage*)malloc(tlen);
96 if (!messageCtx)
97 return FALSE;
98
99 WINPR_ASSERT(channel->channelId <= UINT16_MAX);
100 messageCtx->channelId = WINPR_ASSERTING_INT_CAST(UINT16, channel->channelId);
101 messageCtx->length = WINPR_ASSERTING_INT_CAST(UINT32, len);
102 messageCtx->offset = 0;
103 BYTE* buffer = (BYTE*)(&messageCtx[1]);
104 CopyMemory(buffer, Buffer1, Length1);
105
106 WINPR_ASSERT(Buffer2 || (Length2 == 0));
107 buffer += Length1;
108 if (Buffer2)
109 CopyMemory(buffer, Buffer2, Length2);
110
111 return MessageQueue_Post(channel->queue, messageCtx, 0, nullptr, nullptr);
112}
113
114static BOOL wts_queue_send_item(rdpPeerChannel* channel, BYTE* Buffer, UINT32 Length)
115{
116 BYTE* buffer = nullptr;
117 UINT32 length = 0;
118
119 WINPR_ASSERT(channel);
120 WINPR_ASSERT(channel->vcm);
121 buffer = Buffer;
122 length = Length;
123
124 WINPR_ASSERT(channel->channelId <= UINT16_MAX);
125 const UINT16 channelId = (UINT16)channel->channelId;
126 return MessageQueue_Post(channel->vcm->queue, (void*)(UINT_PTR)channelId, 0, (void*)buffer,
127 (void*)(UINT_PTR)length);
128}
129
130static unsigned wts_read_variable_uint(wStream* s, int cbLen, UINT32* val)
131{
132 WINPR_ASSERT(s);
133 WINPR_ASSERT(val);
134 switch (cbLen)
135 {
136 case 0:
137 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
138 return 0;
139
140 Stream_Read_UINT8(s, *val);
141 return 1;
142
143 case 1:
144 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
145 return 0;
146
147 Stream_Read_UINT16(s, *val);
148 return 2;
149
150 case 2:
151 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
152 return 0;
153
154 Stream_Read_UINT32(s, *val);
155 return 4;
156
157 default:
158 WLog_ERR(TAG, "invalid wts variable uint len %d", cbLen);
159 return 0;
160 }
161}
162
163static BOOL wts_read_drdynvc_capabilities_response(rdpPeerChannel* channel, wStream* s)
164{
165 UINT16 Version = 0;
166
167 WINPR_ASSERT(channel);
168 WINPR_ASSERT(channel->vcm);
169 const size_t length = Stream_GetRemainingLength(s);
170 if (length < 3)
171 return FALSE;
172
173 Stream_Seek_UINT8(s); /* Pad (1 byte) */
174 Stream_Read_UINT16(s, Version);
175 DEBUG_DVC("Version: %" PRIu16 "", Version);
176
177 if (Version < 1)
178 {
179 WLog_ERR(TAG, "invalid version %" PRIu16 " for DRDYNVC", Version);
180 return FALSE;
181 }
182
183 WTSVirtualChannelManager* vcm = channel->vcm;
184 vcm->drdynvc_state = DRDYNVC_STATE_READY;
185
186 /* we only support version 1 for now (no compression yet) */
187 vcm->dvc_spoken_version = MAX(Version, 1);
188
189 return SetEvent(MessageQueue_Event(vcm->queue));
190}
191
192static BOOL wts_read_drdynvc_create_response(rdpPeerChannel* channel, wStream* s)
193{
194 UINT32 CreationStatus = 0;
195 BOOL status = TRUE;
196
197 WINPR_ASSERT(channel);
198 WINPR_ASSERT(s);
199 const size_t length = Stream_GetRemainingLength(s);
200 if ((length < 4) || (length > UINT32_MAX))
201 return FALSE;
202
203 Stream_Read_UINT32(s, CreationStatus);
204
205 if ((INT32)CreationStatus < 0)
206 {
207 DEBUG_DVC("ChannelId %" PRIu32 " creation failed (%" PRId32 ")", channel->channelId,
208 (INT32)CreationStatus);
209 channel->dvc_open_state = DVC_OPEN_STATE_FAILED;
210 }
211 else
212 {
213 DEBUG_DVC("ChannelId %" PRIu32 " creation succeeded", channel->channelId);
214 channel->dvc_open_state = DVC_OPEN_STATE_SUCCEEDED;
215 }
216
217 channel->creationStatus = (INT32)CreationStatus;
218 /* the callback may close the channel, which frees it */
219 WTSVirtualChannelManager* vcm = channel->vcm;
220 IFCALLRET(vcm->dvc_creation_status, status, vcm->dvc_creation_status_userdata,
221 channel->channelId, (INT32)CreationStatus);
222 if (!status)
223 WLog_ERR(TAG, "vcm->dvc_creation_status failed!");
224
225 wts_tunnel_channel_opened(vcm);
226 return status;
227}
228
229static BOOL wts_read_drdynvc_data_first(rdpPeerChannel* channel, wStream* s, int cbLen)
230{
231 WINPR_ASSERT(channel);
232 WINPR_ASSERT(s);
233
234 const UINT32 value = wts_read_variable_uint(s, cbLen, &channel->dvc_total_length);
235
236 if ((value == 0) || (channel->dvc_total_length == 0))
237 return FALSE;
238
239 const size_t length = Stream_GetRemainingLength(s);
240 if (length > channel->dvc_total_length)
241 return FALSE;
242
243 Stream_ResetPosition(channel->receiveData);
244
245 if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
246 return FALSE;
247
248 Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
249 return TRUE;
250}
251
252static BOOL wts_read_drdynvc_data(rdpPeerChannel* channel, wStream* s)
253{
254 BOOL ret = FALSE;
255
256 WINPR_ASSERT(channel);
257 WINPR_ASSERT(s);
258
259 const size_t length = Stream_GetRemainingLength(s);
260 if (channel->dvc_total_length > 0)
261 {
262 const size_t pos = Stream_GetPosition(channel->receiveData);
263 if (pos > SIZE_MAX - length)
264 return FALSE;
265
266 if (pos + length > channel->dvc_total_length)
267 {
268 channel->dvc_total_length = 0;
269 WLog_ERR(TAG, "incorrect fragment data, discarded.");
270 return FALSE;
271 }
272
273 if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
274 return FALSE;
275
276 Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
277
278 if (Stream_GetPosition(channel->receiveData) >= channel->dvc_total_length)
279 {
280 ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData),
281 channel->dvc_total_length, nullptr, 0);
282 channel->dvc_total_length = 0;
283 }
284 else
285 ret = TRUE;
286 }
287 else
288 {
289 ret = wts_queue_receive_data(channel, Stream_ConstPointer(s), length, nullptr, 0);
290 }
291
292 return ret;
293}
294
295static void wts_read_drdynvc_close_response(rdpPeerChannel* channel)
296{
297 WINPR_ASSERT(channel);
298 DEBUG_DVC("ChannelId %" PRIu32 " close response", channel->channelId);
299 channel->dvc_open_state = DVC_OPEN_STATE_CLOSED;
300 MessageQueue_PostQuit(channel->queue, 0);
301}
302
303WINPR_ATTR_NODISCARD
304static BOOL wts_read_drdynvc_pdu_ready_guarded(wStream* s, UINT8 Cmd, UINT8 Sp, UINT32 ChannelId,
305 rdpPeerChannel* dvc)
306{
307 switch (Cmd)
308 {
309 case CREATE_REQUEST_PDU:
310 return wts_read_drdynvc_create_response(dvc, s);
311
312 case DATA_FIRST_PDU:
313 if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
314 {
315 WLog_ERR(TAG,
316 "ChannelId %" PRIu32 " did not open successfully. "
317 "Ignoring DYNVC_DATA_FIRST PDU",
318 ChannelId);
319 return TRUE;
320 }
321
322 return wts_read_drdynvc_data_first(dvc, s, Sp);
323
324 case DATA_PDU:
325 if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
326 {
327 WLog_ERR(TAG,
328 "ChannelId %" PRIu32 " did not open successfully. "
329 "Ignoring DYNVC_DATA PDU",
330 ChannelId);
331 return TRUE;
332 }
333
334 return wts_read_drdynvc_data(dvc, s);
335
336 case CLOSE_REQUEST_PDU:
337 wts_read_drdynvc_close_response(dvc);
338 break;
339
340 case DATA_FIRST_COMPRESSED_PDU:
341 case DATA_COMPRESSED_PDU:
342 WLog_ERR(TAG, "Compressed data not handled");
343 break;
344
345 case SOFT_SYNC_RESPONSE_PDU:
346 WLog_ERR(TAG, "SoftSync response not handled yet(and rather strange to receive "
347 "that packet as our code doesn't send SoftSync requests");
348 break;
349
350 case SOFT_SYNC_REQUEST_PDU:
351 WLog_ERR(TAG, "Not expecting a SoftSyncRequest on the server");
352 return FALSE;
353
354 default:
355 WLog_ERR(TAG, "Cmd %d not recognized.", Cmd);
356 break;
357 }
358 return TRUE;
359}
360
361WINPR_ATTR_NODISCARD
362static BOOL wts_read_drdynvc_pdu_ready(rdpPeerChannel* channel, wStream* s, UINT8 Cmd, UINT8 Sp,
363 UINT8 cbChId, BOOL fromTunnel)
364{
365 WINPR_ASSERT(channel);
366
367 BOOL haveChannelId = 0;
368 switch (Cmd)
369 {
370 case SOFT_SYNC_REQUEST_PDU:
371 case SOFT_SYNC_RESPONSE_PDU:
372 haveChannelId = FALSE;
373 break;
374 default:
375 haveChannelId = TRUE;
376 break;
377 }
378
379 UINT32 ChannelId = 0;
380 rdpPeerChannel* dvc = nullptr;
381 wHashTable* table = nullptr;
382 if (haveChannelId)
383 {
384 const unsigned val = wts_read_variable_uint(s, cbChId, &ChannelId);
385 if (val == 0)
386 return FALSE;
387
388 DEBUG_DVC("Cmd %s ChannelId %" PRIu32 " length %" PRIuz "", drdynvc_get_packet_type(Cmd),
389 ChannelId, Stream_GetRemainingLength(s));
390 table = channel->vcm->dynamicVirtualChannels;
391 if (!table)
392 return FALSE;
393 HashTable_Lock(table);
394 dvc = wts_get_dvc_channel_by_id(channel->vcm, ChannelId);
395 if (!dvc)
396 {
397 DEBUG_DVC("ChannelId %" PRIu32 " does not exist.", ChannelId);
398 HashTable_Unlock(table);
399 return TRUE;
400 }
401 /* [MS-RDPEDYC] 3.2.5.3.2: A channel remains on its assigned transport; the tunnel only
402 * carries channels that were opened on it or moved via Soft-Sync. */
403 if (fromTunnel && !dvc->tunnel)
404 {
405 WLog_WARN(TAG, "%s for channel %" PRIu32 " on the tunnel, which it does not use",
406 drdynvc_get_packet_type(Cmd), ChannelId);
407 HashTable_Unlock(table);
408 return FALSE;
409 }
410 }
411
412 if (wts_tunnel_is_soft_sync_response(Cmd))
413 return wts_tunnel_read_soft_sync_response(channel->vcm, s);
414
415 const BOOL rc = wts_read_drdynvc_pdu_ready_guarded(s, Cmd, Sp, ChannelId, dvc);
416 if (table)
417 HashTable_Unlock(table);
418 return rc;
419}
420
421WINPR_ATTR_NODISCARD
422static BOOL wts_read_drdynvc_pdu(rdpPeerChannel* channel)
423{
424 WINPR_ASSERT(channel);
425 WINPR_ASSERT(channel->vcm);
426
427 const size_t slength = Stream_GetPosition(channel->receiveData);
428 if ((slength < 1) || (slength > UINT32_MAX))
429 return FALSE;
430
431 Stream_ResetPosition(channel->receiveData);
432 wStream buffer = WINPR_C_ARRAY_INIT;
433 wStream* s = Stream_StaticConstInit(&buffer, Stream_Buffer(channel->receiveData), slength);
434 const UINT8 value = Stream_Get_UINT8(s);
435 const UINT8 Cmd = (value & 0xf0) >> 4;
436 const UINT8 Sp = (value & 0x0c) >> 2;
437 const UINT8 cbChId = (value & 0x03) >> 0;
438
439 if (Cmd == CAPABILITY_REQUEST_PDU)
440 return wts_read_drdynvc_capabilities_response(channel, s);
441
442 if (channel->vcm->drdynvc_state == DRDYNVC_STATE_READY)
443 return wts_read_drdynvc_pdu_ready(channel, s, Cmd, Sp, cbChId, FALSE);
444 else
445 WLog_ERR(TAG, "received Cmd %d but channel is not ready.", Cmd);
446
447 return TRUE;
448}
449
450static int wts_write_variable_uint(wStream* s, UINT32 val)
451{
452 int cb = 0;
453
454 WINPR_ASSERT(s);
455 if (val <= 0xFF)
456 {
457 cb = 0;
458 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, val));
459 }
460 else if (val <= 0xFFFF)
461 {
462 cb = 1;
463 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, val));
464 }
465 else
466 {
467 cb = 2;
468 Stream_Write_UINT32(s, val);
469 }
470
471 return cb;
472}
473
474static void wts_write_drdynvc_header(wStream* s, BYTE Cmd, UINT32 ChannelId)
475{
476 WINPR_ASSERT(s);
477
478 BYTE* bm = Stream_PointerAs(s, BYTE);
479 Stream_Seek_UINT8(s);
480 const int cbChId = wts_write_variable_uint(s, ChannelId);
481 *bm = (((Cmd & 0x0F) << 4) | cbChId) & 0xFF;
482}
483
484static BOOL wts_write_drdynvc_create_request(wStream* s, UINT32 ChannelId, const char* ChannelName)
485{
486 size_t len = 0;
487
488 WINPR_ASSERT(s);
489 WINPR_ASSERT(ChannelName);
490
491 wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
492 len = strlen(ChannelName) + 1;
493
494 if (!Stream_EnsureRemainingCapacity(s, len))
495 return FALSE;
496
497 Stream_Write(s, ChannelName, len);
498 return TRUE;
499}
500
501static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId, const BYTE* data,
502 size_t s, UINT32 flags, size_t t)
503{
504 BOOL ret = TRUE;
505 size_t size = s;
506 const size_t totalSize = t;
507
508 WINPR_ASSERT(channel);
509 WINPR_ASSERT(channel->vcm);
510 WINPR_UNUSED(channelId);
511
512 if (channel->channelFlags & CHANNEL_OPTION_SHOW_PROTOCOL)
513 {
514 BOOL firstPass = TRUE;
515
516 while (size)
517 {
518 const UINT32 payloadLen = (size > CHANNEL_CHUNK_LENGTH)
519 ? CHANNEL_CHUNK_LENGTH
520 : WINPR_ASSERTING_INT_CAST(UINT32, size);
521 size -= payloadLen;
522
523 /* here we skip other flags than CHANNEL_FLAG_FIRST and CHANNEL_FLAG_LAST
524 * as it's the only ones treated by ChannelPduTracker.
525 */
526 UINT32 newFlags = 0;
527 if (firstPass)
528 {
529 firstPass = FALSE;
530 if (flags & CHANNEL_FLAG_FIRST)
531 {
532 channel->offset = 0;
533 channel->total = t;
534 newFlags = CHANNEL_FLAG_FIRST;
535 }
536 }
537
538 if (!size && (flags & CHANNEL_FLAG_LAST))
539 newFlags |= CHANNEL_FLAG_LAST;
540
541 const CHANNEL_PDU_HEADER header = { .length = payloadLen, .flags = newFlags };
542
543 channel->offset += payloadLen;
544 if ((channel->total != t) || (channel->offset > t))
545 {
546 WLog_ERR(TAG, "channelId %" PRIu16 " received %" PRIuz " but limit is %" PRIuz,
547 channelId, channel->offset, t);
548 return FALSE;
549 }
550
551 if (!wts_queue_receive_data(channel, &header, sizeof(header), data, payloadLen))
552 return FALSE;
553
554 data += payloadLen;
555 }
556 return TRUE;
557 }
558
559 if ((flags & CHANNEL_FLAG_FIRST) != 0)
560 {
561 Stream_ResetPosition(channel->receiveData);
562 }
563
564 if (!Stream_EnsureRemainingCapacity(channel->receiveData, size))
565 return FALSE;
566
567 Stream_Write(channel->receiveData, data, size);
568
569 if ((flags & CHANNEL_FLAG_LAST) != 0)
570 {
571 if (Stream_GetPosition(channel->receiveData) != totalSize)
572 return FALSE;
573
574 if (channel == channel->vcm->drdynvc_channel)
575 {
576 ret = wts_read_drdynvc_pdu(channel);
577 }
578 else
579 {
580 const size_t pos = Stream_GetPosition(channel->receiveData);
581 if (pos > UINT32_MAX)
582 ret = FALSE;
583 else
584 ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData), pos,
585 nullptr, 0);
586 }
587
588 Stream_ResetPosition(channel->receiveData);
589 }
590 else if (Stream_GetPosition(channel->receiveData) >= totalSize)
591 return FALSE;
592
593 return ret;
594}
595
596static BOOL WTSReceiveChannelData(freerdp_peer* client, UINT16 channelId, const BYTE* data,
597 size_t size, UINT32 flags, size_t totalSize)
598{
599 rdpMcs* mcs = nullptr;
600
601 WINPR_ASSERT(client);
602 WINPR_ASSERT(client->context);
603 WINPR_ASSERT(client->context->rdp);
604
605 mcs = client->context->rdp->mcs;
606 WINPR_ASSERT(mcs);
607
608 for (UINT32 i = 0; i < mcs->channelCount; i++)
609 {
610 rdpMcsChannel* cur = &mcs->channels[i];
611 if (cur->ChannelId == channelId)
612 {
613 rdpPeerChannel* channel = (rdpPeerChannel*)cur->handle;
614
615 if (channel)
616 return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize);
617 }
618 }
619
620 WLog_WARN(TAG, "unknown channelId %" PRIu16 " ignored", channelId);
621
622 return TRUE;
623}
624
625#if defined(WITH_FREERDP_DEPRECATED)
626void WTSVirtualChannelManagerGetFileDescriptor(HANDLE hServer, void** fds, int* fds_count)
627{
628 void* fd = nullptr;
630 WINPR_ASSERT(vcm);
631 WINPR_ASSERT(fds);
632 WINPR_ASSERT(fds_count);
633
634 fd = GetEventWaitObject(MessageQueue_Event(vcm->queue));
635
636 if (fd)
637 {
638 fds[*fds_count] = fd;
639 (*fds_count)++;
640 }
641}
642#endif
643
644BOOL WTSVirtualChannelManagerOpen(HANDLE hServer)
645{
647
648 if (!vcm)
649 return FALSE;
650
651 if (vcm->drdynvc_state == DRDYNVC_STATE_NONE)
652 {
653 rdpPeerChannel* channel = nullptr;
654
655 /* Initialize drdynvc channel once and only once. */
656 vcm->drdynvc_state = DRDYNVC_STATE_INITIALIZED;
657 channel = (rdpPeerChannel*)WTSVirtualChannelOpen((HANDLE)vcm, WTS_CURRENT_SESSION,
658 DRDYNVC_SVC_CHANNEL_NAME);
659
660 if (channel)
661 {
662 BYTE capaBuffer[12] = WINPR_C_ARRAY_INIT;
663 wStream staticS = WINPR_C_ARRAY_INIT;
664 wStream* s = Stream_StaticInit(&staticS, capaBuffer, sizeof(capaBuffer));
665
666 vcm->drdynvc_channel = channel;
667 vcm->dvc_spoken_version = 1;
668 Stream_Write_UINT8(s, 0x50); /* Cmd=5 sp=0 cbId=0 */
669 Stream_Write_UINT8(s, 0x00); /* Pad */
670 Stream_Write_UINT16(s, 0x0001); /* Version */
671
672 /* TODO: shall implement version 2 and 3 */
673
674 const size_t pos = Stream_GetPosition(s);
675 WINPR_ASSERT(pos <= UINT32_MAX);
676 ULONG written = 0;
677 if (!WTSVirtualChannelWrite(channel, (PCHAR)capaBuffer, (UINT32)pos, &written))
678 return FALSE;
679 }
680 }
681
682 return TRUE;
683}
684
685BOOL WTSVirtualChannelManagerCheckFileDescriptorEx(HANDLE hServer, BOOL autoOpen)
686{
687 wMessage message = WINPR_C_ARRAY_INIT;
688 BOOL status = TRUE;
689 WTSVirtualChannelManager* vcm = nullptr;
690
691 if (!hServer || hServer == INVALID_HANDLE_VALUE)
692 return FALSE;
693
694 vcm = (WTSVirtualChannelManager*)hServer;
695
696 if (autoOpen)
697 {
698 if (!WTSVirtualChannelManagerOpen(hServer))
699 return FALSE;
700 }
701
702 if (!wts_tunnel_start_soft_sync_if_due(vcm))
703 return FALSE;
704
705 while (MessageQueue_Peek(vcm->queue, &message, TRUE))
706 {
707 BYTE* buffer = nullptr;
708 UINT32 length = 0;
709 UINT16 channelId = 0;
710 channelId = (UINT16)(UINT_PTR)message.context;
711 buffer = (BYTE*)message.wParam;
712 length = (UINT32)(UINT_PTR)message.lParam;
713
714 WINPR_ASSERT(vcm->client);
715 WINPR_ASSERT(vcm->client->SendChannelData);
716 if (!wts_tunnel_send_message(vcm, message.id, buffer, length, &status) &&
717 !vcm->client->SendChannelData(vcm->client, channelId, buffer, length))
718 {
719 status = FALSE;
720 }
721
722 free(buffer);
723
724 if (!status)
725 break;
726 }
727
728 return wts_tunnel_after_drain(vcm, status);
729}
730
731BOOL WTSVirtualChannelManagerCheckFileDescriptor(HANDLE hServer)
732{
733 return WTSVirtualChannelManagerCheckFileDescriptorEx(hServer, TRUE);
734}
735
736HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer)
737{
739 WINPR_ASSERT(vcm);
740 return MessageQueue_Event(vcm->queue);
741}
742
743static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs, const char* channel_name)
744{
745 if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN + 1))
746 return nullptr;
747
748 for (UINT32 index = 0; index < mcs->channelCount; index++)
749 {
750 rdpMcsChannel* mchannel = &mcs->channels[index];
751 if (mchannel->joined)
752 {
753 if (_strnicmp(mchannel->Name, channel_name, CHANNEL_NAME_LEN + 1) == 0)
754 return mchannel;
755 }
756 }
757
758 return nullptr;
759}
760
761static rdpMcsChannel* wts_get_joined_channel_by_id(rdpMcs* mcs, const UINT16 channel_id)
762{
763 if (!mcs || !channel_id)
764 return nullptr;
765
766 WINPR_ASSERT(mcs->channels);
767 for (UINT32 index = 0; index < mcs->channelCount; index++)
768 {
769 rdpMcsChannel* mchannel = &mcs->channels[index];
770 if (mchannel->joined)
771 {
772 if (mchannel->ChannelId == channel_id)
773 return &mcs->channels[index];
774 }
775 }
776
777 return nullptr;
778}
779
780BOOL WTSIsChannelJoinedByName(freerdp_peer* client, const char* channel_name)
781{
782 if (!client || !client->context || !client->context->rdp)
783 return FALSE;
784
785 return (wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name) != nullptr);
786}
787
788BOOL WTSIsChannelJoinedById(freerdp_peer* client, UINT16 channel_id)
789{
790 if (!client || !client->context || !client->context->rdp)
791 return FALSE;
792
793 return (wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id) != nullptr);
794}
795
796BOOL WTSVirtualChannelManagerIsChannelJoined(HANDLE hServer, const char* name)
797{
799
800 if (!vcm || !vcm->rdp)
801 return FALSE;
802
803 return (wts_get_joined_channel_by_name(vcm->rdp->mcs, name) != nullptr);
804}
805
806BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer)
807{
809 WINPR_ASSERT(vcm);
810 return vcm->drdynvc_state;
811}
812
813void WTSVirtualChannelManagerSetDVCCreationCallback(HANDLE hServer, psDVCCreationStatusCallback cb,
814 void* userdata)
815{
816 WTSVirtualChannelManager* vcm = hServer;
817
818 WINPR_ASSERT(vcm);
819
820 vcm->dvc_creation_status = cb;
821 vcm->dvc_creation_status_userdata = userdata;
822}
823
824UINT16 WTSChannelGetId(freerdp_peer* client, const char* channel_name)
825{
826 rdpMcsChannel* channel = nullptr;
827
828 WINPR_ASSERT(channel_name);
829 if (!client || !client->context || !client->context->rdp)
830 return 0;
831
832 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
833
834 if (!channel)
835 return 0;
836
837 return channel->ChannelId;
838}
839
840UINT32 WTSChannelGetIdByHandle(HANDLE hChannelHandle)
841{
842 rdpPeerChannel* channel = hChannelHandle;
843
844 WINPR_ASSERT(channel);
845
846 return channel->channelId;
847}
848
849BOOL WTSChannelSetHandleByName(freerdp_peer* client, const char* channel_name, void* handle)
850{
851 rdpMcsChannel* channel = nullptr;
852
853 WINPR_ASSERT(channel_name);
854 if (!client || !client->context || !client->context->rdp)
855 return FALSE;
856
857 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
858
859 if (!channel)
860 return FALSE;
861
862 channel->handle = handle;
863 return TRUE;
864}
865
866BOOL WTSChannelSetHandleById(freerdp_peer* client, UINT16 channel_id, void* handle)
867{
868 rdpMcsChannel* channel = nullptr;
869
870 if (!client || !client->context || !client->context->rdp)
871 return FALSE;
872
873 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
874
875 if (!channel)
876 return FALSE;
877
878 channel->handle = handle;
879 return TRUE;
880}
881
882void* WTSChannelGetHandleByName(freerdp_peer* client, const char* channel_name)
883{
884 rdpMcsChannel* channel = nullptr;
885
886 WINPR_ASSERT(channel_name);
887 if (!client || !client->context || !client->context->rdp)
888 return nullptr;
889
890 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
891
892 if (!channel)
893 return nullptr;
894
895 return channel->handle;
896}
897
898void* WTSChannelGetHandleById(freerdp_peer* client, UINT16 channel_id)
899{
900 rdpMcsChannel* channel = nullptr;
901
902 if (!client || !client->context || !client->context->rdp)
903 return nullptr;
904
905 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
906
907 if (!channel)
908 return nullptr;
909
910 return channel->handle;
911}
912
913const char* WTSChannelGetName(freerdp_peer* client, UINT16 channel_id)
914{
915 rdpMcsChannel* channel = nullptr;
916
917 if (!client || !client->context || !client->context->rdp)
918 return nullptr;
919
920 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
921
922 if (!channel)
923 return nullptr;
924
925 return (const char*)channel->Name;
926}
927
928char** WTSGetAcceptedChannelNames(freerdp_peer* client, size_t* count)
929{
930 rdpMcs* mcs = nullptr;
931 char** names = nullptr;
932
933 if (!client || !client->context || !count)
934 return nullptr;
935
936 WINPR_ASSERT(client->context->rdp);
937 mcs = client->context->rdp->mcs;
938 WINPR_ASSERT(mcs);
939 *count = mcs->channelCount;
940
941 names = (char**)calloc(mcs->channelCount, sizeof(char*));
942 if (!names)
943 return nullptr;
944
945 for (UINT32 index = 0; index < mcs->channelCount; index++)
946 {
947 rdpMcsChannel* mchannel = &mcs->channels[index];
948 names[index] = mchannel->Name;
949 }
950
951 return names;
952}
953
954INT64 WTSChannelGetOptions(freerdp_peer* client, UINT16 channel_id)
955{
956 rdpMcsChannel* channel = nullptr;
957
958 if (!client || !client->context || !client->context->rdp)
959 return -1;
960
961 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
962
963 if (!channel)
964 return -1;
965
966 return (INT64)channel->options;
967}
968
969BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
970 WINPR_ATTR_UNUSED ULONG TargetLogonId,
971 WINPR_ATTR_UNUSED BYTE HotkeyVk,
972 WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
973{
974 WLog_ERR("TODO", "TODO: implement");
975 return FALSE;
976}
977
978BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
979 WINPR_ATTR_UNUSED ULONG TargetLogonId,
980 WINPR_ATTR_UNUSED BYTE HotkeyVk,
981 WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
982{
983 WLog_ERR("TODO", "TODO: implement");
984 return FALSE;
985}
986
987BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
988 WINPR_ATTR_UNUSED ULONG TargetLogonId,
989 WINPR_ATTR_UNUSED BYTE HotkeyVk,
990 WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
991 WINPR_ATTR_UNUSED DWORD flags)
992{
993 WLog_ERR("TODO", "TODO: implement");
994 return FALSE;
995}
996
997BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
998 WINPR_ATTR_UNUSED ULONG TargetLogonId,
999 WINPR_ATTR_UNUSED BYTE HotkeyVk,
1000 WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
1001 WINPR_ATTR_UNUSED DWORD flags)
1002{
1003 WLog_ERR("TODO", "TODO: implement");
1004 return FALSE;
1005}
1006
1007BOOL WINAPI FreeRDP_WTSStopRemoteControlSession(WINPR_ATTR_UNUSED ULONG LogonId)
1008{
1009 WLog_ERR("TODO", "TODO: implement");
1010 return FALSE;
1011}
1012
1013BOOL WINAPI FreeRDP_WTSConnectSessionW(WINPR_ATTR_UNUSED ULONG LogonId,
1014 WINPR_ATTR_UNUSED ULONG TargetLogonId,
1015 WINPR_ATTR_UNUSED PWSTR pPassword,
1016 WINPR_ATTR_UNUSED BOOL bWait)
1017{
1018 WLog_ERR("TODO", "TODO: implement");
1019 return FALSE;
1020}
1021
1022BOOL WINAPI FreeRDP_WTSConnectSessionA(WINPR_ATTR_UNUSED ULONG LogonId,
1023 WINPR_ATTR_UNUSED ULONG TargetLogonId,
1024 WINPR_ATTR_UNUSED PSTR pPassword,
1025 WINPR_ATTR_UNUSED BOOL bWait)
1026{
1027 WLog_ERR("TODO", "TODO: implement");
1028 return FALSE;
1029}
1030
1031BOOL WINAPI FreeRDP_WTSEnumerateServersW(WINPR_ATTR_UNUSED LPWSTR pDomainName,
1032 WINPR_ATTR_UNUSED DWORD Reserved,
1033 WINPR_ATTR_UNUSED DWORD Version,
1034 WINPR_ATTR_UNUSED PWTS_SERVER_INFOW* ppServerInfo,
1035 WINPR_ATTR_UNUSED DWORD* pCount)
1036{
1037 WLog_ERR("TODO", "TODO: implement");
1038 return FALSE;
1039}
1040
1041BOOL WINAPI FreeRDP_WTSEnumerateServersA(WINPR_ATTR_UNUSED LPSTR pDomainName,
1042 WINPR_ATTR_UNUSED DWORD Reserved,
1043 WINPR_ATTR_UNUSED DWORD Version,
1044 WINPR_ATTR_UNUSED PWTS_SERVER_INFOA* ppServerInfo,
1045 WINPR_ATTR_UNUSED DWORD* pCount)
1046{
1047 WLog_ERR("TODO", "TODO: implement");
1048 return FALSE;
1049}
1050
1051HANDLE WINAPI FreeRDP_WTSOpenServerW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1052{
1053 WLog_ERR("TODO", "TODO: implement");
1054 return INVALID_HANDLE_VALUE;
1055}
1056
1057static void wts_virtual_channel_manager_free_message(void* obj)
1058{
1059 wMessage* msg = (wMessage*)obj;
1060
1061 if (msg)
1062 {
1063 BYTE* buffer = (BYTE*)msg->wParam;
1064
1065 if (buffer)
1066 free(buffer);
1067 }
1068}
1069
1070static void channel_free(rdpPeerChannel* channel)
1071{
1072 server_channel_common_free(channel);
1073}
1074
1075static void array_channel_free(void* ptr)
1076{
1077 rdpPeerChannel* channel = ptr;
1078 channel_free(channel);
1079}
1080
1081static BOOL dynChannelMatch(const void* v1, const void* v2)
1082{
1083 const UINT32* p1 = (const UINT32*)v1;
1084 const UINT32* p2 = (const UINT32*)v2;
1085 return *p1 == *p2;
1086}
1087
1088static UINT32 channelId_Hash(const void* key)
1089{
1090 const UINT32* v = (const UINT32*)key;
1091 return *v;
1092}
1093
1094static void clearHandles(void)
1095{
1096 HashTable_Free(g_ServerHandles);
1097 g_ServerHandles = nullptr;
1098}
1099
1100static BOOL CALLBACK initializeHandles(WINPR_ATTR_UNUSED PINIT_ONCE once,
1101 WINPR_ATTR_UNUSED PVOID param,
1102 WINPR_ATTR_UNUSED PVOID* context)
1103{
1104 WINPR_ASSERT(g_ServerHandles == nullptr);
1105 g_ServerHandles = HashTable_New(TRUE);
1106 (void)winpr_atexit(clearHandles);
1107 return g_ServerHandles != nullptr;
1108}
1109
1110static bool setup(void)
1111{
1112 return InitOnceExecuteOnce(&g_HandleInitializer, initializeHandles, nullptr, nullptr);
1113}
1114
1115static void wtsCloseVCM(WTSVirtualChannelManager* vcm, bool closeDrdynvc)
1116{
1117 WINPR_ASSERT(vcm);
1118
1119 HashTable_Lock(g_ServerHandles);
1120
1121/* clang analyzer does not like the check for INVALID_HANDLE_VALUE and considers the path not taken,
1122 * leading to false positives on memory leaks. */
1123#ifdef __clang_analyzer__
1124 const BOOL valid = vcm != nullptr;
1125#else
1126 const BOOL valid = (vcm != nullptr) && (vcm != INVALID_HANDLE_VALUE);
1127#endif
1128 if (valid)
1129 {
1130 HashTable_Remove(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId);
1131
1132 wts_tunnel_free(vcm);
1133
1134 HashTable_Free(vcm->dynamicVirtualChannels);
1135
1136 if (vcm->drdynvc_channel)
1137 {
1138 if (closeDrdynvc)
1139 (void)WTSVirtualChannelClose(vcm->drdynvc_channel);
1140 vcm->drdynvc_channel = nullptr;
1141 }
1142
1143 MessageQueue_Free(vcm->queue);
1144 free(vcm);
1145 }
1146 HashTable_Unlock(g_ServerHandles);
1147}
1148
1149HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName)
1150{
1151 wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1152
1153 rdpContext* context = WINPR_PACKED_ALIGN_CAST(rdpContext*, pServerName);
1154
1155 if (!setup() || !context)
1156 return INVALID_HANDLE_VALUE;
1157
1158 freerdp_peer* client = context->peer;
1159
1160 if (!client)
1161 {
1162 SetLastError(ERROR_INVALID_DATA);
1163 return INVALID_HANDLE_VALUE;
1164 }
1165
1166 WTSVirtualChannelManager* vcm = calloc(1, sizeof(WTSVirtualChannelManager));
1167
1168 if (!vcm)
1169 goto fail;
1170
1171 vcm->client = client;
1172 vcm->rdp = context->rdp;
1173
1174 queueCallbacks.fnObjectFree = wts_virtual_channel_manager_free_message;
1175 vcm->queue = MessageQueue_New(&queueCallbacks);
1176
1177 if (!vcm->queue)
1178 goto fail;
1179
1180 vcm->dvc_channel_id_seq = 0;
1181 vcm->dynamicVirtualChannels = HashTable_New(TRUE);
1182
1183 if (!vcm->dynamicVirtualChannels)
1184 goto fail;
1185
1186 if (!HashTable_SetHashFunction(vcm->dynamicVirtualChannels, channelId_Hash))
1187 goto fail;
1188
1189 {
1190 wObject* obj = HashTable_ValueObject(vcm->dynamicVirtualChannels);
1191 WINPR_ASSERT(obj);
1192 obj->fnObjectFree = array_channel_free;
1193
1194 obj = HashTable_KeyObject(vcm->dynamicVirtualChannels);
1195 obj->fnObjectEquals = dynChannelMatch;
1196 }
1197 client->ReceiveChannelData = WTSReceiveChannelData;
1198
1199 if (!wts_tunnel_new(vcm, context, wts_read_drdynvc_pdu_ready))
1200 goto fail;
1201 {
1202 HashTable_Lock(g_ServerHandles);
1203 vcm->SessionId = g_SessionId++;
1204 const BOOL rc =
1205 HashTable_Insert(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId, (void*)vcm);
1206 HashTable_Unlock(g_ServerHandles);
1207 if (!rc)
1208 goto fail;
1209 }
1210
1211 HANDLE hServer = (HANDLE)vcm;
1212 return hServer;
1213
1214fail:
1215 wtsCloseVCM(vcm, false);
1216 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1217 return INVALID_HANDLE_VALUE;
1218}
1219
1220HANDLE WINAPI FreeRDP_WTSOpenServerExW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1221{
1222 WLog_ERR("TODO", "TODO: implement");
1223 return INVALID_HANDLE_VALUE;
1224}
1225
1226HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName)
1227{
1228 return FreeRDP_WTSOpenServerA(pServerName);
1229}
1230
1231VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer)
1232{
1234 wtsCloseVCM(vcm, true);
1235}
1236
1237BOOL WINAPI FreeRDP_WTSEnumerateSessionsW(WINPR_ATTR_UNUSED HANDLE hServer,
1238 WINPR_ATTR_UNUSED DWORD Reserved,
1239 WINPR_ATTR_UNUSED DWORD Version,
1240 WINPR_ATTR_UNUSED PWTS_SESSION_INFOW* ppSessionInfo,
1241 WINPR_ATTR_UNUSED DWORD* pCount)
1242{
1243 WLog_ERR("TODO", "TODO: implement");
1244 return FALSE;
1245}
1246
1247BOOL WINAPI FreeRDP_WTSEnumerateSessionsA(WINPR_ATTR_UNUSED HANDLE hServer,
1248 WINPR_ATTR_UNUSED DWORD Reserved,
1249 WINPR_ATTR_UNUSED DWORD Version,
1250 WINPR_ATTR_UNUSED PWTS_SESSION_INFOA* ppSessionInfo,
1251 WINPR_ATTR_UNUSED DWORD* pCount)
1252{
1253 WLog_ERR("TODO", "TODO: implement");
1254 return FALSE;
1255}
1256
1257BOOL WINAPI FreeRDP_WTSEnumerateSessionsExW(WINPR_ATTR_UNUSED HANDLE hServer,
1258 WINPR_ATTR_UNUSED DWORD* pLevel,
1259 WINPR_ATTR_UNUSED DWORD Filter,
1260 WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1W* ppSessionInfo,
1261 WINPR_ATTR_UNUSED DWORD* pCount)
1262{
1263 WLog_ERR("TODO", "TODO: implement");
1264 return FALSE;
1265}
1266
1267BOOL WINAPI FreeRDP_WTSEnumerateSessionsExA(WINPR_ATTR_UNUSED HANDLE hServer,
1268 WINPR_ATTR_UNUSED DWORD* pLevel,
1269 WINPR_ATTR_UNUSED DWORD Filter,
1270 WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1A* ppSessionInfo,
1271 WINPR_ATTR_UNUSED DWORD* pCount)
1272{
1273 WLog_ERR("TODO", "TODO: implement");
1274 return FALSE;
1275}
1276
1277BOOL WINAPI FreeRDP_WTSEnumerateProcessesW(WINPR_ATTR_UNUSED HANDLE hServer,
1278 WINPR_ATTR_UNUSED DWORD Reserved,
1279 WINPR_ATTR_UNUSED DWORD Version,
1280 WINPR_ATTR_UNUSED PWTS_PROCESS_INFOW* ppProcessInfo,
1281 WINPR_ATTR_UNUSED DWORD* pCount)
1282{
1283 WLog_ERR("TODO", "TODO: implement");
1284 return FALSE;
1285}
1286
1287BOOL WINAPI FreeRDP_WTSEnumerateProcessesA(WINPR_ATTR_UNUSED HANDLE hServer,
1288 WINPR_ATTR_UNUSED DWORD Reserved,
1289 WINPR_ATTR_UNUSED DWORD Version,
1290 WINPR_ATTR_UNUSED PWTS_PROCESS_INFOA* ppProcessInfo,
1291 WINPR_ATTR_UNUSED DWORD* pCount)
1292{
1293 WLog_ERR("TODO", "TODO: implement");
1294 return FALSE;
1295}
1296
1297BOOL WINAPI FreeRDP_WTSTerminateProcess(WINPR_ATTR_UNUSED HANDLE hServer,
1298 WINPR_ATTR_UNUSED DWORD ProcessId,
1299 WINPR_ATTR_UNUSED DWORD ExitCode)
1300{
1301 WLog_ERR("TODO", "TODO: implement");
1302 return FALSE;
1303}
1304
1305BOOL WINAPI FreeRDP_WTSQuerySessionInformationW(WINPR_ATTR_UNUSED HANDLE hServer,
1306 WINPR_ATTR_UNUSED DWORD SessionId,
1307 WINPR_ATTR_UNUSED WTS_INFO_CLASS WTSInfoClass,
1308 WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1309 WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1310{
1311 WLog_ERR("TODO", "TODO: implement");
1312 return FALSE;
1313}
1314
1315BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1316 WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer,
1317 DWORD* pBytesReturned)
1318{
1319 DWORD BytesReturned = 0;
1320 WTSVirtualChannelManager* vcm = nullptr;
1321 vcm = (WTSVirtualChannelManager*)hServer;
1322
1323 if (!vcm)
1324 return FALSE;
1325
1326 if (WTSInfoClass == WTSSessionId)
1327 {
1328 ULONG* pBuffer = nullptr;
1329 BytesReturned = sizeof(ULONG);
1330 pBuffer = (ULONG*)malloc(sizeof(BytesReturned));
1331
1332 if (!pBuffer)
1333 {
1334 SetLastError(g_err_oom);
1335 return FALSE;
1336 }
1337
1338 *pBuffer = vcm->SessionId;
1339 *ppBuffer = (LPSTR)pBuffer;
1340 *pBytesReturned = BytesReturned;
1341 return TRUE;
1342 }
1343
1344 return FALSE;
1345}
1346
1347BOOL WINAPI FreeRDP_WTSQueryUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1348 WINPR_ATTR_UNUSED LPWSTR pUserName,
1349 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1350 WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1351 WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1352{
1353 WLog_ERR("TODO", "TODO: implement");
1354 return FALSE;
1355}
1356
1357BOOL WINAPI FreeRDP_WTSQueryUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1358 WINPR_ATTR_UNUSED LPSTR pUserName,
1359 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1360 WINPR_ATTR_UNUSED LPSTR* ppBuffer,
1361 WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1362{
1363 WLog_ERR("TODO", "TODO: implement");
1364 return FALSE;
1365}
1366
1367BOOL WINAPI FreeRDP_WTSSetUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1368 WINPR_ATTR_UNUSED LPWSTR pUserName,
1369 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1370 WINPR_ATTR_UNUSED LPWSTR pBuffer,
1371 WINPR_ATTR_UNUSED DWORD DataLength)
1372{
1373 WLog_ERR("TODO", "TODO: implement");
1374 return FALSE;
1375}
1376
1377BOOL WINAPI FreeRDP_WTSSetUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1378 WINPR_ATTR_UNUSED LPSTR pUserName,
1379 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1380 WINPR_ATTR_UNUSED LPSTR pBuffer,
1381 WINPR_ATTR_UNUSED DWORD DataLength)
1382{
1383 WLog_ERR("TODO", "TODO: implement");
1384 return FALSE;
1385}
1386
1387BOOL WINAPI
1388FreeRDP_WTSSendMessageW(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1389 WINPR_ATTR_UNUSED LPWSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1390 WINPR_ATTR_UNUSED LPWSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1391 WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1392 WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1393{
1394 WLog_ERR("TODO", "TODO: implement");
1395 return FALSE;
1396}
1397
1398BOOL WINAPI
1399FreeRDP_WTSSendMessageA(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1400 WINPR_ATTR_UNUSED LPSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1401 WINPR_ATTR_UNUSED LPSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1402 WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1403 WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1404{
1405 WLog_ERR("TODO", "TODO: implement");
1406 return FALSE;
1407}
1408
1409BOOL WINAPI FreeRDP_WTSDisconnectSession(WINPR_ATTR_UNUSED HANDLE hServer,
1410 WINPR_ATTR_UNUSED DWORD SessionId,
1411 WINPR_ATTR_UNUSED BOOL bWait)
1412{
1413 WLog_ERR("TODO", "TODO: implement");
1414 return FALSE;
1415}
1416
1417BOOL WINAPI FreeRDP_WTSLogoffSession(WINPR_ATTR_UNUSED HANDLE hServer,
1418 WINPR_ATTR_UNUSED DWORD SessionId,
1419 WINPR_ATTR_UNUSED BOOL bWait)
1420{
1421 WLog_ERR("TODO", "TODO: implement");
1422 return FALSE;
1423}
1424
1425BOOL WINAPI FreeRDP_WTSShutdownSystem(WINPR_ATTR_UNUSED HANDLE hServer,
1426 WINPR_ATTR_UNUSED DWORD ShutdownFlag)
1427{
1428 WLog_ERR("TODO", "TODO: implement");
1429 return FALSE;
1430}
1431
1432BOOL WINAPI FreeRDP_WTSWaitSystemEvent(WINPR_ATTR_UNUSED HANDLE hServer,
1433 WINPR_ATTR_UNUSED DWORD EventMask,
1434 WINPR_ATTR_UNUSED DWORD* pEventFlags)
1435{
1436 WLog_ERR("TODO", "TODO: implement");
1437 return FALSE;
1438}
1439
1440static void peer_channel_queue_free_message(void* obj)
1441{
1442 wMessage* msg = (wMessage*)obj;
1443 if (!msg)
1444 return;
1445
1446 free(msg->context);
1447 msg->context = nullptr;
1448}
1449
1450static rdpPeerChannel* channel_new(WTSVirtualChannelManager* vcm, freerdp_peer* client,
1451 UINT32 ChannelId, UINT16 index, UINT16 type, size_t chunkSize,
1452 const char* name, UINT32 flags)
1453{
1454 wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1455 queueCallbacks.fnObjectFree = peer_channel_queue_free_message;
1456
1457 rdpPeerChannel* channel =
1458 server_channel_common_new(client, index, ChannelId, chunkSize, &queueCallbacks, name);
1459
1460 WINPR_ASSERT(vcm);
1461 WINPR_ASSERT(client);
1462
1463 if (!channel)
1464 goto fail;
1465
1466 channel->vcm = vcm;
1467 channel->channelType = type;
1468 channel->creationStatus =
1469 (type == RDP_PEER_CHANNEL_TYPE_SVC) ? ERROR_SUCCESS : ERROR_OPERATION_IN_PROGRESS;
1470 channel->channelFlags = flags;
1471
1472 return channel;
1473fail:
1474 channel_free(channel);
1475 return nullptr;
1476}
1477
1478static HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenStatic(HANDLE hServer,
1479 WINPR_ATTR_UNUSED DWORD SessionId,
1480 LPSTR pVirtualName, UINT32 Flags)
1481{
1483 if (!vcm)
1484 {
1485 SetLastError(ERROR_INVALID_DATA);
1486 return nullptr;
1487 }
1488
1489 freerdp_peer* client = vcm->client;
1490 WINPR_ASSERT(client);
1491
1492 rdpContext* context = client->context;
1493 WINPR_ASSERT(context);
1494 WINPR_ASSERT(context->rdp);
1495 WINPR_ASSERT(context->settings);
1496
1497 rdpMcs* mcs = context->rdp->mcs;
1498 WINPR_ASSERT(mcs);
1499
1500 size_t length = strnlen(pVirtualName, CHANNEL_NAME_LEN + 1);
1501 if (length > CHANNEL_NAME_LEN)
1502 {
1503 SetLastError(ERROR_NOT_FOUND);
1504 return nullptr;
1505 }
1506
1507 rdpMcsChannel* joined_channel = nullptr;
1508 UINT32 index = 0;
1509 for (; index < mcs->channelCount; index++)
1510 {
1511 rdpMcsChannel* mchannel = &mcs->channels[index];
1512 if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
1513 {
1514 joined_channel = mchannel;
1515 break;
1516 }
1517 }
1518
1519 if (!joined_channel)
1520 {
1521 SetLastError(ERROR_NOT_FOUND);
1522 return nullptr;
1523 }
1524
1525 rdpPeerChannel* channel = (rdpPeerChannel*)joined_channel->handle;
1526 if (!channel)
1527 {
1528 const UINT32 VCChunkSize =
1529 freerdp_settings_get_uint32(context->settings, FreeRDP_VCChunkSize);
1530
1531 WINPR_ASSERT(index <= UINT16_MAX);
1532 channel = channel_new(vcm, client, joined_channel->ChannelId, (UINT16)index,
1533 RDP_PEER_CHANNEL_TYPE_SVC, VCChunkSize, pVirtualName, Flags);
1534
1535 if (!channel)
1536 goto fail;
1537
1538 joined_channel->handle = channel;
1539 }
1540
1541 HANDLE hChannelHandle = (HANDLE)channel;
1542 return hChannelHandle;
1543fail:
1544 channel_free(channel);
1545 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1546 return nullptr;
1547}
1548
1549HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId, LPSTR pVirtualName)
1550{
1551 return FreeRDP_WTSVirtualChannelOpenStatic(hServer, SessionId, pVirtualName, 0);
1552}
1553
1554HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName, DWORD flags)
1555{
1556 wStream* s = nullptr;
1557 rdpPeerChannel* channel = nullptr;
1558 BOOL joined = FALSE;
1559 ULONG written = 0;
1560
1561 if (!setup())
1562 return nullptr;
1563
1564 if (SessionId == WTS_CURRENT_SESSION)
1565 return nullptr;
1566
1567 HashTable_Lock(g_ServerHandles);
1568 WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)HashTable_GetItemValue(
1569 g_ServerHandles, (void*)(UINT_PTR)SessionId);
1570
1571 if (!vcm)
1572 goto end;
1573
1574 if (!(flags & WTS_CHANNEL_OPTION_DYNAMIC))
1575 {
1576 HashTable_Unlock(g_ServerHandles);
1577 return FreeRDP_WTSVirtualChannelOpenStatic((HANDLE)vcm, SessionId, pVirtualName, flags);
1578 }
1579
1580 freerdp_peer* client = vcm->client;
1581 WINPR_ASSERT(client);
1582 WINPR_ASSERT(client->context);
1583 WINPR_ASSERT(client->context->rdp);
1584
1585 rdpMcs* mcs = client->context->rdp->mcs;
1586 WINPR_ASSERT(mcs);
1587
1588 for (UINT32 index = 0; index < mcs->channelCount; index++)
1589 {
1590 rdpMcsChannel* mchannel = &mcs->channels[index];
1591 if (mchannel->joined &&
1592 (strncmp(mchannel->Name, DRDYNVC_SVC_CHANNEL_NAME, CHANNEL_NAME_LEN + 1) == 0))
1593 {
1594 joined = TRUE;
1595 break;
1596 }
1597 }
1598
1599 if (!joined)
1600 {
1601 SetLastError(ERROR_NOT_FOUND);
1602 goto end;
1603 }
1604
1605 if (!vcm->drdynvc_channel || (vcm->drdynvc_state != DRDYNVC_STATE_READY))
1606 {
1607 SetLastError(ERROR_NOT_READY);
1608 goto end;
1609 }
1610
1611 WINPR_ASSERT(client);
1612 WINPR_ASSERT(client->context);
1613 WINPR_ASSERT(client->context->settings);
1614
1615 const UINT32 VCChunkSize =
1616 freerdp_settings_get_uint32(client->context->settings, FreeRDP_VCChunkSize);
1617 channel =
1618 channel_new(vcm, client, 0, 0, RDP_PEER_CHANNEL_TYPE_DVC, VCChunkSize, pVirtualName, flags);
1619
1620 if (!channel)
1621 {
1622 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1623 goto end;
1624 }
1625
1626 const LONG hdl = InterlockedIncrement(&vcm->dvc_channel_id_seq);
1627 channel->channelId = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1628
1629 if (!HashTable_Insert(vcm->dynamicVirtualChannels, &channel->channelId, channel))
1630 {
1631 channel_free(channel);
1632 channel = nullptr;
1633 goto fail;
1634 }
1635 s = Stream_New(nullptr, 64);
1636
1637 if (!s)
1638 goto fail;
1639
1640 if (!wts_write_drdynvc_create_request(s, channel->channelId, pVirtualName))
1641 goto fail;
1642
1643 BOOL onTunnel = FALSE;
1644 if (!wts_tunnel_open_channel(vcm, channel, s, &onTunnel))
1645 goto fail;
1646 if (!onTunnel)
1647 {
1648 const size_t pos = Stream_GetPosition(s);
1649 WINPR_ASSERT(pos <= UINT32_MAX);
1650 if (!WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char), (UINT32)pos,
1651 &written))
1652 goto fail;
1653 }
1654
1655end:
1656 Stream_Free(s, TRUE);
1657 HashTable_Unlock(g_ServerHandles);
1658 return channel;
1659
1660fail:
1661 Stream_Free(s, TRUE);
1662 if (channel)
1663 {
1664 HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1665 /* Soft-Sync may have been waiting for this channel's Create Response */
1666 wts_tunnel_channel_opened(vcm);
1667 }
1668 HashTable_Unlock(g_ServerHandles);
1669
1670 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1671 return nullptr;
1672}
1673
1674BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
1675{
1676 wStream* s = nullptr;
1677 rdpMcs* mcs = nullptr;
1678
1679 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1680 BOOL ret = TRUE;
1681
1682 if (channel)
1683 {
1684 WTSVirtualChannelManager* vcm = channel->vcm;
1685
1686 WINPR_ASSERT(vcm);
1687 WINPR_ASSERT(vcm->client);
1688 WINPR_ASSERT(vcm->client->context);
1689 WINPR_ASSERT(vcm->client->context->rdp);
1690 mcs = vcm->client->context->rdp->mcs;
1691
1692 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1693 {
1694 if (channel->index < mcs->channelCount)
1695 {
1696 rdpMcsChannel* cur = &mcs->channels[channel->index];
1697 rdpPeerChannel* peerChannel = (rdpPeerChannel*)cur->handle;
1698 channel_free(peerChannel);
1699 cur->handle = nullptr;
1700 }
1701 }
1702 else
1703 {
1704 if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
1705 {
1706 ULONG written = 0;
1707 s = Stream_New(nullptr, 8);
1708
1709 if (!s)
1710 {
1711 WLog_ERR(TAG, "Stream_New failed!");
1712 ret = FALSE;
1713 }
1714 else
1715 {
1716 wts_write_drdynvc_header(s, CLOSE_REQUEST_PDU, channel->channelId);
1717
1718 const size_t pos = Stream_GetPosition(s);
1719 WINPR_ASSERT(pos <= UINT32_MAX);
1720 /* Soft-Sync holds this lock while moving the channel to ensure the Close
1721 * Request is sent on the same transport as preceding data. */
1722 EnterCriticalSection(&channel->writeLock);
1723 if (!wts_tunnel_close_request(channel, Stream_Buffer(s), pos, &ret))
1724 ret = WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char),
1725 (UINT32)pos, &written);
1726 LeaveCriticalSection(&channel->writeLock);
1727 Stream_Free(s, TRUE);
1728 }
1729 }
1730 /* Soft-Sync waits for channels lacking a Create Response; removing this
1731 * channel may allow it to proceed. */
1732 const BOOL waited = (channel->dvc_open_state == DVC_OPEN_STATE_NONE);
1733 HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1734 if (waited)
1735 wts_tunnel_channel_opened(vcm);
1736 }
1737 }
1738
1739 return ret;
1740}
1741
1742BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, WINPR_ATTR_UNUSED ULONG TimeOut,
1743 PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead)
1744{
1745 BYTE* buffer = nullptr;
1746 wMessage message = WINPR_C_ARRAY_INIT;
1747 wtsChannelMessage* messageCtx = nullptr;
1748 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1749
1750 WINPR_ASSERT(channel);
1751
1752 if (!MessageQueue_Peek(channel->queue, &message, FALSE))
1753 {
1754 SetLastError(ERROR_NO_DATA);
1755 *pBytesRead = 0;
1756 return FALSE;
1757 }
1758
1759 messageCtx = message.context;
1760
1761 if (messageCtx == nullptr)
1762 return FALSE;
1763
1764 buffer = (BYTE*)(messageCtx + 1);
1765 *pBytesRead = messageCtx->length - messageCtx->offset;
1766
1767 if (Buffer == nullptr || BufferSize == 0)
1768 {
1769 return TRUE;
1770 }
1771
1772 if (*pBytesRead > BufferSize)
1773 *pBytesRead = BufferSize;
1774
1775 CopyMemory(Buffer, buffer + messageCtx->offset, *pBytesRead);
1776 messageCtx->offset += *pBytesRead;
1777
1778 if (messageCtx->offset >= messageCtx->length)
1779 {
1780 const int rc = MessageQueue_Peek(channel->queue, &message, TRUE);
1781 peer_channel_queue_free_message(&message);
1782 if (rc < 0)
1783 return FALSE;
1784 }
1785
1786 return TRUE;
1787}
1788
1789BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG uLength,
1790 PULONG pBytesWritten)
1791{
1792 wStream* s = nullptr;
1793 int cbLen = 0;
1794 int cbChId = 0;
1795 int first = 0;
1796 BYTE* buffer = nullptr;
1797 size_t totalWritten = 0;
1798 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1799 BOOL ret = FALSE;
1800
1801 if (!channel)
1802 return FALSE;
1803
1804 EnterCriticalSection(&channel->writeLock);
1805 WINPR_ASSERT(channel->vcm);
1806 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1807 {
1808 buffer = (BYTE*)malloc(uLength);
1809
1810 if (!buffer)
1811 {
1812 SetLastError(g_err_oom);
1813 goto fail;
1814 }
1815
1816 CopyMemory(buffer, Buffer, uLength);
1817 totalWritten = uLength;
1818 if (!wts_queue_send_item(channel, buffer, uLength))
1819 goto fail;
1820 }
1821 else if (!channel->vcm->drdynvc_channel || (channel->vcm->drdynvc_state != DRDYNVC_STATE_READY))
1822 {
1823 DEBUG_DVC("drdynvc not ready");
1824 goto fail;
1825 }
1826 else
1827 {
1828 first = TRUE;
1829
1830 size_t Length = uLength;
1831 while (Length > 0)
1832 {
1833 s = Stream_New(nullptr, DVC_MAX_DATA_PDU_SIZE);
1834
1835 if (!s)
1836 {
1837 WLog_ERR(TAG, "Stream_New failed!");
1838 SetLastError(g_err_oom);
1839 goto fail;
1840 }
1841
1842 buffer = Stream_Buffer(s);
1843 Stream_Seek_UINT8(s);
1844 cbChId = wts_write_variable_uint(s, channel->channelId);
1845
1846 if (first && (Length > Stream_GetRemainingLength(s)))
1847 {
1848 cbLen = wts_write_variable_uint(s, WINPR_ASSERTING_INT_CAST(uint32_t, Length));
1849 buffer[0] = ((DATA_FIRST_PDU << 4) | (cbLen << 2) | cbChId) & 0xFF;
1850 }
1851 else
1852 {
1853 buffer[0] = ((DATA_PDU << 4) | cbChId) & 0xFF;
1854 }
1855
1856 first = FALSE;
1857 size_t written = Stream_GetRemainingLength(s);
1858
1859 if (written > Length)
1860 written = Length;
1861
1862 Stream_Write(s, Buffer, written);
1863 const size_t length = Stream_GetPosition(s);
1864 Stream_Free(s, FALSE);
1865 if (length > UINT32_MAX)
1866 goto fail;
1867 Length -= written;
1868 Buffer += written;
1869 totalWritten += written;
1870 BOOL queued = FALSE;
1871 if (!wts_tunnel_queue_send_item(channel, buffer, (UINT32)length, &queued))
1872 queued = wts_queue_send_item(channel->vcm->drdynvc_channel, buffer, (UINT32)length);
1873 if (!queued)
1874 goto fail;
1875 }
1876 }
1877
1878 if (pBytesWritten)
1879 *pBytesWritten = WINPR_ASSERTING_INT_CAST(uint32_t, totalWritten);
1880
1881 ret = TRUE;
1882fail:
1883 LeaveCriticalSection(&channel->writeLock);
1884 return ret;
1885}
1886
1887BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1888{
1889 WLog_ERR("TODO", "TODO: implement");
1890 return TRUE;
1891}
1892
1893BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1894{
1895 WLog_ERR("TODO", "TODO: implement");
1896 return TRUE;
1897}
1898
1899BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS WtsVirtualClass,
1900 PVOID* ppBuffer, DWORD* pBytesReturned)
1901{
1902 void* pfd = nullptr;
1903 BOOL bval = 0;
1904 void* fds[10] = WINPR_C_ARRAY_INIT;
1905 HANDLE hEvent = nullptr;
1906 int fds_count = 0;
1907 BOOL status = FALSE;
1908 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1909
1910 WINPR_ASSERT(channel);
1911
1912 switch ((UINT32)WtsVirtualClass)
1913 {
1914 case WTSVirtualFileHandle:
1915 hEvent = MessageQueue_Event(channel->queue);
1916 pfd = GetEventWaitObject(hEvent);
1917
1918 if (pfd)
1919 {
1920 fds[fds_count] = pfd;
1921 (fds_count)++;
1922 }
1923
1924 *ppBuffer = malloc(sizeof(void*));
1925
1926 if (!*ppBuffer)
1927 {
1928 SetLastError(g_err_oom);
1929 }
1930 else
1931 {
1932 CopyMemory(*ppBuffer, (void*)&fds[0], sizeof(void*));
1933 *pBytesReturned = sizeof(void*);
1934 status = TRUE;
1935 }
1936
1937 break;
1938
1939 case WTSVirtualEventHandle:
1940 hEvent = MessageQueue_Event(channel->queue);
1941
1942 *ppBuffer = malloc(sizeof(HANDLE));
1943
1944 if (!*ppBuffer)
1945 {
1946 SetLastError(g_err_oom);
1947 }
1948 else
1949 {
1950 CopyMemory(*ppBuffer, (void*)&hEvent, sizeof(HANDLE));
1951 *pBytesReturned = sizeof(void*);
1952 status = TRUE;
1953 }
1954
1955 break;
1956
1957 case WTSVirtualChannelReady:
1958 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1959 {
1960 bval = TRUE;
1961 status = TRUE;
1962 }
1963 else
1964 {
1965 switch (channel->dvc_open_state)
1966 {
1967 case DVC_OPEN_STATE_NONE:
1968 bval = FALSE;
1969 status = TRUE;
1970 break;
1971
1972 case DVC_OPEN_STATE_SUCCEEDED:
1973 bval = TRUE;
1974 status = TRUE;
1975 break;
1976
1977 default:
1978 *ppBuffer = nullptr;
1979 *pBytesReturned = 0;
1980 return FALSE;
1981 }
1982 }
1983
1984 *ppBuffer = malloc(sizeof(BOOL));
1985
1986 if (!*ppBuffer)
1987 {
1988 SetLastError(g_err_oom);
1989 status = FALSE;
1990 }
1991 else
1992 {
1993 CopyMemory(*ppBuffer, &bval, sizeof(BOOL));
1994 *pBytesReturned = sizeof(BOOL);
1995 }
1996
1997 break;
1998 case WTSVirtualChannelOpenStatus:
1999 {
2000 INT32 value = channel->creationStatus;
2001 status = TRUE;
2002
2003 *ppBuffer = malloc(sizeof(value));
2004 if (!*ppBuffer)
2005 {
2006 SetLastError(g_err_oom);
2007 status = FALSE;
2008 }
2009 else
2010 {
2011 CopyMemory(*ppBuffer, &value, sizeof(value));
2012 *pBytesReturned = sizeof(value);
2013 }
2014 break;
2015 }
2016 default:
2017 break;
2018 }
2019
2020 return status;
2021}
2022
2023VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory)
2024{
2025 free(pMemory);
2026}
2027
2028BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
2029 WINPR_ATTR_UNUSED PVOID pMemory,
2030 WINPR_ATTR_UNUSED ULONG NumberOfEntries)
2031{
2032 WLog_ERR("TODO", "TODO: implement");
2033 return FALSE;
2034}
2035
2036BOOL WINAPI FreeRDP_WTSFreeMemoryExA(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
2037 WINPR_ATTR_UNUSED PVOID pMemory,
2038 WINPR_ATTR_UNUSED ULONG NumberOfEntries)
2039{
2040 WLog_ERR("TODO", "TODO: implement");
2041 return FALSE;
2042}
2043
2044BOOL WINAPI FreeRDP_WTSRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd,
2045 WINPR_ATTR_UNUSED DWORD dwFlags)
2046{
2047 WLog_ERR("TODO", "TODO: implement");
2048 return FALSE;
2049}
2050
2051BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd)
2052{
2053 WLog_ERR("TODO", "TODO: implement");
2054 return FALSE;
2055}
2056
2057BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2058 WINPR_ATTR_UNUSED HWND hWnd,
2059 WINPR_ATTR_UNUSED DWORD dwFlags)
2060{
2061 WLog_ERR("TODO", "TODO: implement");
2062 return FALSE;
2063}
2064
2065BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2066 WINPR_ATTR_UNUSED HWND hWnd)
2067{
2068 WLog_ERR("TODO", "TODO: implement");
2069 return FALSE;
2070}
2071
2072BOOL WINAPI FreeRDP_WTSQueryUserToken(WINPR_ATTR_UNUSED ULONG SessionId,
2073 WINPR_ATTR_UNUSED PHANDLE phToken)
2074{
2075 WLog_ERR("TODO", "TODO: implement");
2076 return FALSE;
2077}
2078
2079BOOL WINAPI FreeRDP_WTSEnumerateProcessesExW(WINPR_ATTR_UNUSED HANDLE hServer,
2080 WINPR_ATTR_UNUSED DWORD* pLevel,
2081 WINPR_ATTR_UNUSED DWORD SessionId,
2082 WINPR_ATTR_UNUSED LPWSTR* ppProcessInfo,
2083 WINPR_ATTR_UNUSED DWORD* pCount)
2084{
2085 WLog_ERR("TODO", "TODO: implement");
2086 return FALSE;
2087}
2088
2089BOOL WINAPI FreeRDP_WTSEnumerateProcessesExA(WINPR_ATTR_UNUSED HANDLE hServer,
2090 WINPR_ATTR_UNUSED DWORD* pLevel,
2091 WINPR_ATTR_UNUSED DWORD SessionId,
2092 WINPR_ATTR_UNUSED LPSTR* ppProcessInfo,
2093 WINPR_ATTR_UNUSED DWORD* pCount)
2094{
2095 WLog_ERR("TODO", "TODO: implement");
2096 return FALSE;
2097}
2098
2099BOOL WINAPI FreeRDP_WTSEnumerateListenersW(WINPR_ATTR_UNUSED HANDLE hServer,
2100 WINPR_ATTR_UNUSED PVOID pReserved,
2101 WINPR_ATTR_UNUSED DWORD Reserved,
2102 WINPR_ATTR_UNUSED PWTSLISTENERNAMEW pListeners,
2103 WINPR_ATTR_UNUSED DWORD* pCount)
2104{
2105 WLog_ERR("TODO", "TODO: implement");
2106 return FALSE;
2107}
2108
2109BOOL WINAPI FreeRDP_WTSEnumerateListenersA(WINPR_ATTR_UNUSED HANDLE hServer,
2110 WINPR_ATTR_UNUSED PVOID pReserved,
2111 WINPR_ATTR_UNUSED DWORD Reserved,
2112 WINPR_ATTR_UNUSED PWTSLISTENERNAMEA pListeners,
2113 WINPR_ATTR_UNUSED DWORD* pCount)
2114{
2115 WLog_ERR("TODO", "TODO: implement");
2116 return FALSE;
2117}
2118
2119BOOL WINAPI FreeRDP_WTSQueryListenerConfigW(WINPR_ATTR_UNUSED HANDLE hServer,
2120 WINPR_ATTR_UNUSED PVOID pReserved,
2121 WINPR_ATTR_UNUSED DWORD Reserved,
2122 WINPR_ATTR_UNUSED LPWSTR pListenerName,
2123 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer)
2124{
2125 WLog_ERR("TODO", "TODO: implement");
2126 return FALSE;
2127}
2128
2129BOOL WINAPI FreeRDP_WTSQueryListenerConfigA(WINPR_ATTR_UNUSED HANDLE hServer,
2130 WINPR_ATTR_UNUSED PVOID pReserved,
2131 WINPR_ATTR_UNUSED DWORD Reserved,
2132 WINPR_ATTR_UNUSED LPSTR pListenerName,
2133 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer)
2134{
2135 WLog_ERR("TODO", "TODO: implement");
2136 return FALSE;
2137}
2138
2139BOOL WINAPI FreeRDP_WTSCreateListenerW(WINPR_ATTR_UNUSED HANDLE hServer,
2140 WINPR_ATTR_UNUSED PVOID pReserved,
2141 WINPR_ATTR_UNUSED DWORD Reserved,
2142 WINPR_ATTR_UNUSED LPWSTR pListenerName,
2143 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer,
2144 WINPR_ATTR_UNUSED DWORD flag)
2145{
2146 WLog_ERR("TODO", "TODO: implement");
2147 return FALSE;
2148}
2149
2150BOOL WINAPI FreeRDP_WTSCreateListenerA(WINPR_ATTR_UNUSED HANDLE hServer,
2151 WINPR_ATTR_UNUSED PVOID pReserved,
2152 WINPR_ATTR_UNUSED DWORD Reserved,
2153 WINPR_ATTR_UNUSED LPSTR pListenerName,
2154 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer,
2155 WINPR_ATTR_UNUSED DWORD flag)
2156{
2157 WLog_ERR("TODO", "TODO: implement");
2158 return FALSE;
2159}
2160
2161BOOL WINAPI FreeRDP_WTSSetListenerSecurityW(
2162 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2163 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2164 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2165 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2166{
2167 WLog_ERR("TODO", "TODO: implement");
2168 return FALSE;
2169}
2170
2171BOOL WINAPI FreeRDP_WTSSetListenerSecurityA(
2172 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2173 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2174 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2175 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2176{
2177 WLog_ERR("TODO", "TODO: implement");
2178 return FALSE;
2179}
2180
2181BOOL WINAPI FreeRDP_WTSGetListenerSecurityW(
2182 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2183 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2184 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2185 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2186 WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2187{
2188 WLog_ERR("TODO", "TODO: implement");
2189 return FALSE;
2190}
2191
2192BOOL WINAPI FreeRDP_WTSGetListenerSecurityA(
2193 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2194 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2195 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2196 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2197 WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2198{
2199 WLog_ERR("TODO", "TODO: implement");
2200 return FALSE;
2201}
2202
2203BOOL CDECL FreeRDP_WTSEnableChildSessions(WINPR_ATTR_UNUSED BOOL bEnable)
2204{
2205 WLog_ERR("TODO", "TODO: implement");
2206 return FALSE;
2207}
2208
2209BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(WINPR_ATTR_UNUSED PBOOL pbEnabled)
2210{
2211 WLog_ERR("TODO", "TODO: implement");
2212 return FALSE;
2213}
2214
2215BOOL CDECL FreeRDP_WTSGetChildSessionId(WINPR_ATTR_UNUSED PULONG pSessionId)
2216{
2217 WLog_ERR("TODO", "TODO: implement");
2218 return FALSE;
2219}
2220
2221DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(void)
2222{
2223 WLog_ERR("TODO", "TODO: implement");
2224 return 0xFFFFFFFF;
2225}
2226BOOL WINAPI FreeRDP_WTSLogoffUser(WINPR_ATTR_UNUSED HANDLE hServer)
2227{
2228 WLog_ERR("TODO", "TODO: implement");
2229 return FALSE;
2230}
2231
2232BOOL WINAPI FreeRDP_WTSLogonUser(WINPR_ATTR_UNUSED HANDLE hServer,
2233 WINPR_ATTR_UNUSED LPCSTR username,
2234 WINPR_ATTR_UNUSED LPCSTR password, WINPR_ATTR_UNUSED LPCSTR domain)
2235{
2236 WLog_ERR("TODO", "TODO: implement");
2237 return FALSE;
2238}
2239
2240void server_channel_common_free(rdpPeerChannel* channel)
2241{
2242 if (!channel)
2243 return;
2244 MessageQueue_Free(channel->queue);
2245 Stream_Free(channel->receiveData, TRUE);
2246 DeleteCriticalSection(&channel->writeLock);
2247 free(channel);
2248}
2249
2250rdpPeerChannel* server_channel_common_new(freerdp_peer* client, UINT16 index, UINT32 channelId,
2251 size_t chunkSize, const wObject* callback,
2252 const char* name)
2253{
2254 rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1, sizeof(rdpPeerChannel));
2255 if (!channel)
2256 return nullptr;
2257
2258 InitializeCriticalSection(&channel->writeLock);
2259
2260 channel->receiveData = Stream_New(nullptr, chunkSize);
2261 if (!channel->receiveData)
2262 goto fail;
2263
2264 channel->queue = MessageQueue_New(callback);
2265 if (!channel->queue)
2266 goto fail;
2267
2268 channel->index = index;
2269 channel->client = client;
2270 channel->channelId = channelId;
2271 strncpy(channel->channelName, name, ARRAYSIZE(channel->channelName) - 1);
2272 return channel;
2273fail:
2274 WINPR_PRAGMA_DIAG_PUSH
2275 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2276 server_channel_common_free(channel);
2277 WINPR_PRAGMA_DIAG_POP
2278 return nullptr;
2279}
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
This struct contains function pointer to initialize/free objects.
Definition collections.h:52
OBJECT_FREE_FN fnObjectFree
Definition collections.h:59