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