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