22#include <freerdp/config.h>
29#include <winpr/atexit.h>
30#include <winpr/wtypes.h>
32#include <winpr/synch.h>
33#include <winpr/stream.h>
34#include <winpr/assert.h>
35#include <winpr/cast.h>
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>
46#include "server_tunnel.h"
48#define TAG FREERDP_TAG("core.server")
50#define DEBUG_DVC(...) WLog_DBG(TAG, __VA_ARGS__)
52#define DEBUG_DVC(...) \
58#define DVC_MAX_DATA_PDU_SIZE 1600
68static const DWORD g_err_oom = WINPR_CXX_COMPAT_CAST(DWORD, E_OUTOFMEMORY);
70static DWORD g_SessionId = 1;
71static wHashTable* g_ServerHandles =
nullptr;
72static INIT_ONCE g_HandleInitializer = INIT_ONCE_STATIC_INIT;
77 return HashTable_GetItemValue(vcm->dynamicVirtualChannels, &ChannelId);
80static BOOL wts_queue_receive_data(rdpPeerChannel* channel,
const void* Buffer1,
size_t Length1,
81 const void* Buffer2,
size_t Length2)
83 WINPR_ASSERT(channel);
85 if (Length1 > UINT32_MAX -
sizeof(wtsChannelMessage))
87 if (Length2 > UINT32_MAX -
sizeof(wtsChannelMessage) - Length1)
89 const size_t len = Length1 + Length2;
96 if (channel->channelId > UINT16_MAX)
99 const size_t tlen =
sizeof(wtsChannelMessage) + len;
100 wtsChannelMessage* messageCtx = (wtsChannelMessage*)malloc(tlen);
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);
111 WINPR_ASSERT(Buffer2 || (Length2 == 0));
114 CopyMemory(buffer, Buffer2, Length2);
116 return MessageQueue_Post(channel->queue, messageCtx, 0,
nullptr,
nullptr);
119static BOOL wts_queue_send_item(rdpPeerChannel* channel, BYTE* Buffer, UINT32 Length)
121 BYTE* buffer =
nullptr;
124 WINPR_ASSERT(channel);
125 WINPR_ASSERT(channel->vcm);
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);
135static unsigned wts_read_variable_uint(
wStream* s,
int cbLen, UINT32* val)
142 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
145 Stream_Read_UINT8(s, *val);
149 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
152 Stream_Read_UINT16(s, *val);
156 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
159 Stream_Read_UINT32(s, *val);
163 WLog_ERR(TAG,
"invalid wts variable uint len %d", cbLen);
168static BOOL wts_read_drdynvc_capabilities_response(rdpPeerChannel* channel,
wStream* s)
172 WINPR_ASSERT(channel);
173 WINPR_ASSERT(channel->vcm);
174 const size_t length = Stream_GetRemainingLength(s);
178 Stream_Seek_UINT8(s);
179 Stream_Read_UINT16(s, Version);
180 DEBUG_DVC(
"Version: %" PRIu16
"", Version);
184 WLog_ERR(TAG,
"invalid version %" PRIu16
" for DRDYNVC", Version);
189 vcm->drdynvc_state = DRDYNVC_STATE_READY;
192 vcm->dvc_spoken_version = MAX(Version, 1);
197 return MessageQueue_Post(vcm->queue,
nullptr, 0,
nullptr,
nullptr);
200static BOOL wts_read_drdynvc_create_response(rdpPeerChannel* channel,
wStream* s)
202 UINT32 CreationStatus = 0;
205 WINPR_ASSERT(channel);
207 const size_t length = Stream_GetRemainingLength(s);
208 if ((length < 4) || (length > UINT32_MAX))
211 Stream_Read_UINT32(s, CreationStatus);
213 if ((INT32)CreationStatus < 0)
215 DEBUG_DVC(
"ChannelId %" PRIu32
" creation failed (%" PRId32
")", channel->channelId,
216 (INT32)CreationStatus);
217 channel->dvc_open_state = DVC_OPEN_STATE_FAILED;
221 DEBUG_DVC(
"ChannelId %" PRIu32
" creation succeeded", channel->channelId);
222 channel->dvc_open_state = DVC_OPEN_STATE_SUCCEEDED;
225 channel->creationStatus = (INT32)CreationStatus;
228 IFCALLRET(vcm->dvc_creation_status, status, vcm->dvc_creation_status_userdata,
229 channel->channelId, (INT32)CreationStatus);
231 WLog_ERR(TAG,
"vcm->dvc_creation_status failed!");
233 wts_tunnel_channel_opened(vcm);
237static BOOL wts_read_drdynvc_data_first(rdpPeerChannel* channel,
wStream* s,
int cbLen)
239 WINPR_ASSERT(channel);
242 const UINT32 value = wts_read_variable_uint(s, cbLen, &channel->dvc_total_length);
244 if ((value == 0) || (channel->dvc_total_length == 0))
247 const size_t length = Stream_GetRemainingLength(s);
248 if (length > channel->dvc_total_length)
251 Stream_ResetPosition(channel->receiveData);
253 if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
256 Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
260static BOOL wts_read_drdynvc_data(rdpPeerChannel* channel,
wStream* s)
264 WINPR_ASSERT(channel);
267 const size_t length = Stream_GetRemainingLength(s);
268 if (channel->dvc_total_length > 0)
270 const size_t pos = Stream_GetPosition(channel->receiveData);
271 if (pos > SIZE_MAX - length)
274 if (pos + length > channel->dvc_total_length)
276 channel->dvc_total_length = 0;
277 WLog_ERR(TAG,
"incorrect fragment data, discarded.");
281 if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
284 Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
286 if (Stream_GetPosition(channel->receiveData) >= channel->dvc_total_length)
288 ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData),
289 channel->dvc_total_length,
nullptr, 0);
290 channel->dvc_total_length = 0;
297 ret = wts_queue_receive_data(channel, Stream_ConstPointer(s), length,
nullptr, 0);
303static void wts_read_drdynvc_close_response(rdpPeerChannel* channel)
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);
312static BOOL wts_read_drdynvc_pdu_ready_guarded(
wStream* s, UINT8 Cmd, UINT8 Sp, UINT32 ChannelId,
317 case CREATE_REQUEST_PDU:
318 return wts_read_drdynvc_create_response(dvc, s);
321 if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
324 "ChannelId %" PRIu32
" did not open successfully. "
325 "Ignoring DYNVC_DATA_FIRST PDU",
330 return wts_read_drdynvc_data_first(dvc, s, Sp);
333 if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
336 "ChannelId %" PRIu32
" did not open successfully. "
337 "Ignoring DYNVC_DATA PDU",
342 return wts_read_drdynvc_data(dvc, s);
344 case CLOSE_REQUEST_PDU:
345 wts_read_drdynvc_close_response(dvc);
348 case DATA_FIRST_COMPRESSED_PDU:
349 case DATA_COMPRESSED_PDU:
350 WLog_ERR(TAG,
"Compressed data not handled");
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");
358 case SOFT_SYNC_REQUEST_PDU:
359 WLog_ERR(TAG,
"Not expecting a SoftSyncRequest on the server");
363 WLog_ERR(TAG,
"Cmd %d not recognized.", Cmd);
370static BOOL wts_read_drdynvc_pdu_ready(rdpPeerChannel* channel,
wStream* s, UINT8 Cmd, UINT8 Sp,
371 UINT8 cbChId, BOOL fromTunnel)
373 WINPR_ASSERT(channel);
375 BOOL haveChannelId = 0;
378 case SOFT_SYNC_REQUEST_PDU:
379 case SOFT_SYNC_RESPONSE_PDU:
380 haveChannelId = FALSE;
383 haveChannelId = TRUE;
387 UINT32 ChannelId = 0;
388 rdpPeerChannel* dvc =
nullptr;
389 wHashTable* table =
nullptr;
392 const unsigned val = wts_read_variable_uint(s, cbChId, &ChannelId);
396 DEBUG_DVC(
"Cmd %s ChannelId %" PRIu32
" length %" PRIuz
"", drdynvc_get_packet_type(Cmd),
397 ChannelId, Stream_GetRemainingLength(s));
398 table = channel->vcm->dynamicVirtualChannels;
401 HashTable_Lock(table);
402 dvc = wts_get_dvc_channel_by_id(channel->vcm, ChannelId);
405 DEBUG_DVC(
"ChannelId %" PRIu32
" does not exist.", ChannelId);
406 HashTable_Unlock(table);
411 if (fromTunnel && !dvc->tunnel)
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);
420 if (wts_tunnel_is_soft_sync_response(Cmd))
421 return wts_tunnel_read_soft_sync_response(channel->vcm, s);
423 const BOOL rc = wts_read_drdynvc_pdu_ready_guarded(s, Cmd, Sp, ChannelId, dvc);
425 HashTable_Unlock(table);
430static BOOL wts_read_drdynvc_pdu(rdpPeerChannel* channel)
432 WINPR_ASSERT(channel);
433 WINPR_ASSERT(channel->vcm);
435 const size_t slength = Stream_GetPosition(channel->receiveData);
436 if ((slength < 1) || (slength > UINT32_MAX))
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;
447 if (Cmd == CAPABILITY_REQUEST_PDU)
448 return wts_read_drdynvc_capabilities_response(channel, s);
450 if (channel->vcm->drdynvc_state == DRDYNVC_STATE_READY)
451 return wts_read_drdynvc_pdu_ready(channel, s, Cmd, Sp, cbChId, FALSE);
453 WLog_ERR(TAG,
"received Cmd %d but channel is not ready.", Cmd);
458static int wts_write_variable_uint(
wStream* s, UINT32 val)
466 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, val));
468 else if (val <= 0xFFFF)
471 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, val));
476 Stream_Write_UINT32(s, val);
482static void wts_write_drdynvc_header(
wStream* s, BYTE Cmd, UINT32 ChannelId)
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;
492static BOOL wts_write_drdynvc_create_request(
wStream* s, UINT32 ChannelId,
const char* ChannelName)
497 WINPR_ASSERT(ChannelName);
499 wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
500 len = strlen(ChannelName) + 1;
502 if (!Stream_EnsureRemainingCapacity(s, len))
505 Stream_Write(s, ChannelName, len);
509static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId,
const BYTE* data,
510 size_t s, UINT32 flags,
size_t t)
514 const size_t totalSize = t;
516 WINPR_ASSERT(channel);
517 WINPR_ASSERT(channel->vcm);
518 WINPR_UNUSED(channelId);
520 if (channel->channelFlags & CHANNEL_OPTION_SHOW_PROTOCOL)
522 BOOL firstPass = TRUE;
526 const UINT32 payloadLen = (size > CHANNEL_CHUNK_LENGTH)
527 ? CHANNEL_CHUNK_LENGTH
528 : WINPR_ASSERTING_INT_CAST(UINT32, size);
538 if (flags & CHANNEL_FLAG_FIRST)
542 newFlags = CHANNEL_FLAG_FIRST;
546 if (!size && (flags & CHANNEL_FLAG_LAST))
547 newFlags |= CHANNEL_FLAG_LAST;
551 channel->offset += payloadLen;
552 if ((channel->total != t) || (channel->offset > t))
554 WLog_ERR(TAG,
"channelId %" PRIu16
" received %" PRIuz
" but limit is %" PRIuz,
555 channelId, channel->offset, t);
559 if (!wts_queue_receive_data(channel, &header,
sizeof(header), data, payloadLen))
567 if ((flags & CHANNEL_FLAG_FIRST) != 0)
569 Stream_ResetPosition(channel->receiveData);
572 if (!Stream_EnsureRemainingCapacity(channel->receiveData, size))
575 Stream_Write(channel->receiveData, data, size);
577 if ((flags & CHANNEL_FLAG_LAST) != 0)
579 if (Stream_GetPosition(channel->receiveData) != totalSize)
582 if (channel == channel->vcm->drdynvc_channel)
584 ret = wts_read_drdynvc_pdu(channel);
588 const size_t pos = Stream_GetPosition(channel->receiveData);
589 if (pos > UINT32_MAX)
592 ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData), pos,
596 Stream_ResetPosition(channel->receiveData);
598 else if (Stream_GetPosition(channel->receiveData) >= totalSize)
604static BOOL WTSReceiveChannelData(freerdp_peer* client, UINT16 channelId,
const BYTE* data,
605 size_t size, UINT32 flags,
size_t totalSize)
607 rdpMcs* mcs =
nullptr;
609 WINPR_ASSERT(client);
610 WINPR_ASSERT(client->context);
611 WINPR_ASSERT(client->context->rdp);
613 mcs = client->context->rdp->mcs;
616 for (UINT32 i = 0; i < mcs->channelCount; i++)
618 rdpMcsChannel* cur = &mcs->channels[i];
619 if (cur->ChannelId == channelId)
621 rdpPeerChannel* channel = (rdpPeerChannel*)cur->handle;
624 return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize);
628 WLog_WARN(TAG,
"unknown channelId %" PRIu16
" ignored", channelId);
633#if defined(WITH_FREERDP_DEPRECATED)
634void WTSVirtualChannelManagerGetFileDescriptor(HANDLE hServer,
void** fds,
int* fds_count)
640 WINPR_ASSERT(fds_count);
642 fd = GetEventWaitObject(MessageQueue_Event(vcm->queue));
646 fds[*fds_count] = fd;
652BOOL WTSVirtualChannelManagerOpen(HANDLE hServer)
659 if (vcm->drdynvc_state == DRDYNVC_STATE_NONE)
661 rdpPeerChannel* channel =
nullptr;
664 vcm->drdynvc_state = DRDYNVC_STATE_INITIALIZED;
665 channel = (rdpPeerChannel*)WTSVirtualChannelOpen((HANDLE)vcm, WTS_CURRENT_SESSION,
666 DRDYNVC_SVC_CHANNEL_NAME);
670 BYTE capaBuffer[12] = WINPR_C_ARRAY_INIT;
671 wStream staticS = WINPR_C_ARRAY_INIT;
672 wStream* s = Stream_StaticInit(&staticS, capaBuffer,
sizeof(capaBuffer));
674 vcm->drdynvc_channel = channel;
675 vcm->dvc_spoken_version = 1;
676 Stream_Write_UINT8(s, 0x50);
677 Stream_Write_UINT8(s, 0x00);
678 Stream_Write_UINT16(s, 0x0001);
682 const size_t pos = Stream_GetPosition(s);
683 WINPR_ASSERT(pos <= UINT32_MAX);
685 if (!WTSVirtualChannelWrite(channel, (PCHAR)capaBuffer, (UINT32)pos, &written))
693BOOL WTSVirtualChannelManagerCheckFileDescriptorEx(HANDLE hServer, BOOL autoOpen)
695 wMessage message = WINPR_C_ARRAY_INIT;
698 if (!hServer || hServer == INVALID_HANDLE_VALUE)
705 if (!WTSVirtualChannelManagerOpen(hServer))
709 if (!wts_tunnel_start_soft_sync_if_due(vcm))
712 while (MessageQueue_Peek(vcm->queue, &message, TRUE))
714 const UINT16 channelId = (UINT16)(UINT_PTR)message.context;
715 BYTE* buffer = (BYTE*)message.wParam;
716 const UINT32 length = (UINT32)(UINT_PTR)message.lParam;
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))
736 return wts_tunnel_after_drain(vcm, status);
739BOOL WTSVirtualChannelManagerCheckFileDescriptor(HANDLE hServer)
741 return WTSVirtualChannelManagerCheckFileDescriptorEx(hServer, TRUE);
744HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer)
748 return MessageQueue_Event(vcm->queue);
751static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs,
const char* channel_name)
753 if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN + 1))
756 for (UINT32 index = 0; index < mcs->channelCount; index++)
758 rdpMcsChannel* mchannel = &mcs->channels[index];
759 if (mchannel->joined)
761 if (_strnicmp(mchannel->Name, channel_name, CHANNEL_NAME_LEN + 1) == 0)
769static rdpMcsChannel* wts_get_joined_channel_by_id(rdpMcs* mcs,
const UINT16 channel_id)
771 if (!mcs || !channel_id)
774 WINPR_ASSERT(mcs->channels);
775 for (UINT32 index = 0; index < mcs->channelCount; index++)
777 rdpMcsChannel* mchannel = &mcs->channels[index];
778 if (mchannel->joined)
780 if (mchannel->ChannelId == channel_id)
781 return &mcs->channels[index];
788BOOL WTSIsChannelJoinedByName(freerdp_peer* client,
const char* channel_name)
790 if (!client || !client->context || !client->context->rdp)
793 return (wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name) !=
nullptr);
796BOOL WTSIsChannelJoinedById(freerdp_peer* client, UINT16 channel_id)
798 if (!client || !client->context || !client->context->rdp)
801 return (wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id) !=
nullptr);
804BOOL WTSVirtualChannelManagerIsChannelJoined(HANDLE hServer,
const char* name)
808 if (!vcm || !vcm->rdp)
811 return (wts_get_joined_channel_by_name(vcm->rdp->mcs, name) !=
nullptr);
814BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer)
818 return vcm->drdynvc_state;
821void WTSVirtualChannelManagerSetDVCCreationCallback(HANDLE hServer, psDVCCreationStatusCallback cb,
828 vcm->dvc_creation_status = cb;
829 vcm->dvc_creation_status_userdata = userdata;
832UINT16 WTSChannelGetId(freerdp_peer* client,
const char* channel_name)
834 rdpMcsChannel* channel =
nullptr;
836 WINPR_ASSERT(channel_name);
837 if (!client || !client->context || !client->context->rdp)
840 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
845 return channel->ChannelId;
848UINT32 WTSChannelGetIdByHandle(HANDLE hChannelHandle)
850 rdpPeerChannel* channel = hChannelHandle;
852 WINPR_ASSERT(channel);
854 return channel->channelId;
857BOOL WTSChannelSetHandleByName(freerdp_peer* client,
const char* channel_name,
void* handle)
859 rdpMcsChannel* channel =
nullptr;
861 WINPR_ASSERT(channel_name);
862 if (!client || !client->context || !client->context->rdp)
865 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
870 channel->handle = handle;
874BOOL WTSChannelSetHandleById(freerdp_peer* client, UINT16 channel_id,
void* handle)
876 rdpMcsChannel* channel =
nullptr;
878 if (!client || !client->context || !client->context->rdp)
881 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
886 channel->handle = handle;
890void* WTSChannelGetHandleByName(freerdp_peer* client,
const char* channel_name)
892 rdpMcsChannel* channel =
nullptr;
894 WINPR_ASSERT(channel_name);
895 if (!client || !client->context || !client->context->rdp)
898 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
903 return channel->handle;
906void* WTSChannelGetHandleById(freerdp_peer* client, UINT16 channel_id)
908 rdpMcsChannel* channel =
nullptr;
910 if (!client || !client->context || !client->context->rdp)
913 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
918 return channel->handle;
921const char* WTSChannelGetName(freerdp_peer* client, UINT16 channel_id)
923 rdpMcsChannel* channel =
nullptr;
925 if (!client || !client->context || !client->context->rdp)
928 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
933 return (
const char*)channel->Name;
936char** WTSGetAcceptedChannelNames(freerdp_peer* client,
size_t* count)
938 rdpMcs* mcs =
nullptr;
939 char** names =
nullptr;
941 if (!client || !client->context || !count)
944 WINPR_ASSERT(client->context->rdp);
945 mcs = client->context->rdp->mcs;
947 *count = mcs->channelCount;
949 names = (
char**)calloc(mcs->channelCount,
sizeof(
char*));
953 for (UINT32 index = 0; index < mcs->channelCount; index++)
955 rdpMcsChannel* mchannel = &mcs->channels[index];
956 names[index] = mchannel->Name;
962INT64 WTSChannelGetOptions(freerdp_peer* client, UINT16 channel_id)
964 rdpMcsChannel* channel =
nullptr;
966 if (!client || !client->context || !client->context->rdp)
969 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
974 return (INT64)channel->options;
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)
982 WLog_ERR(
"TODO",
"TODO: implement");
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)
991 WLog_ERR(
"TODO",
"TODO: implement");
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)
1001 WLog_ERR(
"TODO",
"TODO: implement");
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)
1011 WLog_ERR(
"TODO",
"TODO: implement");
1015BOOL WINAPI FreeRDP_WTSStopRemoteControlSession(WINPR_ATTR_UNUSED ULONG LogonId)
1017 WLog_ERR(
"TODO",
"TODO: implement");
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)
1026 WLog_ERR(
"TODO",
"TODO: implement");
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)
1035 WLog_ERR(
"TODO",
"TODO: implement");
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)
1045 WLog_ERR(
"TODO",
"TODO: implement");
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)
1055 WLog_ERR(
"TODO",
"TODO: implement");
1059HANDLE WINAPI FreeRDP_WTSOpenServerW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1061 WLog_ERR(
"TODO",
"TODO: implement");
1062 return INVALID_HANDLE_VALUE;
1065static void wts_virtual_channel_manager_free_message(
void* obj)
1067 wMessage* msg = (wMessage*)obj;
1071 BYTE* buffer = (BYTE*)msg->wParam;
1078static void channel_free(rdpPeerChannel* channel)
1080 server_channel_common_free(channel);
1083static void array_channel_free(
void* ptr)
1085 rdpPeerChannel* channel = ptr;
1086 channel_free(channel);
1089static BOOL dynChannelMatch(
const void* v1,
const void* v2)
1091 const UINT32* p1 = (
const UINT32*)v1;
1092 const UINT32* p2 = (
const UINT32*)v2;
1096static UINT32 channelId_Hash(
const void* key)
1098 const UINT32* v = (
const UINT32*)key;
1102static void clearHandles(
void)
1104 HashTable_Free(g_ServerHandles);
1105 g_ServerHandles =
nullptr;
1108static BOOL CALLBACK initializeHandles(WINPR_ATTR_UNUSED PINIT_ONCE once,
1109 WINPR_ATTR_UNUSED PVOID param,
1110 WINPR_ATTR_UNUSED PVOID* context)
1112 WINPR_ASSERT(g_ServerHandles ==
nullptr);
1113 g_ServerHandles = HashTable_New(TRUE);
1114 (void)winpr_atexit(clearHandles);
1115 return g_ServerHandles !=
nullptr;
1118static bool setup(
void)
1120 return InitOnceExecuteOnce(&g_HandleInitializer, initializeHandles,
nullptr,
nullptr);
1127 HashTable_Lock(g_ServerHandles);
1131#ifdef __clang_analyzer__
1132 const BOOL valid = vcm !=
nullptr;
1134 const BOOL valid = (vcm !=
nullptr) && (vcm != INVALID_HANDLE_VALUE);
1138 HashTable_Remove(g_ServerHandles, (
void*)(UINT_PTR)vcm->SessionId);
1140 wts_tunnel_free(vcm);
1142 HashTable_Free(vcm->dynamicVirtualChannels);
1144 if (vcm->drdynvc_channel)
1147 (void)WTSVirtualChannelClose(vcm->drdynvc_channel);
1148 vcm->drdynvc_channel =
nullptr;
1151 MessageQueue_Free(vcm->queue);
1154 HashTable_Unlock(g_ServerHandles);
1157HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName)
1159 wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1161 rdpContext* context = WINPR_PACKED_ALIGN_CAST(rdpContext*, pServerName);
1163 if (!setup() || !context)
1164 return INVALID_HANDLE_VALUE;
1166 freerdp_peer* client = context->peer;
1170 SetLastError(ERROR_INVALID_DATA);
1171 return INVALID_HANDLE_VALUE;
1179 vcm->client = client;
1180 vcm->rdp = context->rdp;
1182 queueCallbacks.
fnObjectFree = wts_virtual_channel_manager_free_message;
1183 vcm->queue = MessageQueue_New(&queueCallbacks);
1188 vcm->dvc_channel_id_seq = 0;
1189 vcm->dynamicVirtualChannels = HashTable_New(TRUE);
1191 if (!vcm->dynamicVirtualChannels)
1194 if (!HashTable_SetHashFunction(vcm->dynamicVirtualChannels, channelId_Hash))
1198 wObject* obj = HashTable_ValueObject(vcm->dynamicVirtualChannels);
1200 obj->fnObjectFree = array_channel_free;
1202 obj = HashTable_KeyObject(vcm->dynamicVirtualChannels);
1203 obj->fnObjectEquals = dynChannelMatch;
1205 client->ReceiveChannelData = WTSReceiveChannelData;
1207 if (!wts_tunnel_new(vcm, context, wts_read_drdynvc_pdu_ready))
1210 HashTable_Lock(g_ServerHandles);
1211 vcm->SessionId = g_SessionId++;
1213 HashTable_Insert(g_ServerHandles, (
void*)(UINT_PTR)vcm->SessionId, (
void*)vcm);
1214 HashTable_Unlock(g_ServerHandles);
1219 HANDLE hServer = (HANDLE)vcm;
1223 wtsCloseVCM(vcm,
false);
1224 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1225 return INVALID_HANDLE_VALUE;
1228HANDLE WINAPI FreeRDP_WTSOpenServerExW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1230 WLog_ERR(
"TODO",
"TODO: implement");
1231 return INVALID_HANDLE_VALUE;
1234HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName)
1236 return FreeRDP_WTSOpenServerA(pServerName);
1239VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer)
1242 wtsCloseVCM(vcm,
true);
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)
1251 WLog_ERR(
"TODO",
"TODO: implement");
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)
1261 WLog_ERR(
"TODO",
"TODO: implement");
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)
1271 WLog_ERR(
"TODO",
"TODO: implement");
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)
1281 WLog_ERR(
"TODO",
"TODO: implement");
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)
1291 WLog_ERR(
"TODO",
"TODO: implement");
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)
1301 WLog_ERR(
"TODO",
"TODO: implement");
1305BOOL WINAPI FreeRDP_WTSTerminateProcess(WINPR_ATTR_UNUSED HANDLE hServer,
1306 WINPR_ATTR_UNUSED DWORD ProcessId,
1307 WINPR_ATTR_UNUSED DWORD ExitCode)
1309 WLog_ERR(
"TODO",
"TODO: implement");
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)
1319 WLog_ERR(
"TODO",
"TODO: implement");
1323BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1324 WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer,
1325 DWORD* pBytesReturned)
1327 DWORD BytesReturned = 0;
1334 if (WTSInfoClass == WTSSessionId)
1336 ULONG* pBuffer =
nullptr;
1337 BytesReturned =
sizeof(ULONG);
1338 pBuffer = (ULONG*)malloc(
sizeof(BytesReturned));
1342 SetLastError(g_err_oom);
1346 *pBuffer = vcm->SessionId;
1347 *ppBuffer = (LPSTR)pBuffer;
1348 *pBytesReturned = BytesReturned;
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)
1361 WLog_ERR(
"TODO",
"TODO: implement");
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)
1371 WLog_ERR(
"TODO",
"TODO: implement");
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)
1381 WLog_ERR(
"TODO",
"TODO: implement");
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)
1391 WLog_ERR(
"TODO",
"TODO: implement");
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)
1402 WLog_ERR(
"TODO",
"TODO: implement");
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)
1413 WLog_ERR(
"TODO",
"TODO: implement");
1417BOOL WINAPI FreeRDP_WTSDisconnectSession(WINPR_ATTR_UNUSED HANDLE hServer,
1418 WINPR_ATTR_UNUSED DWORD SessionId,
1419 WINPR_ATTR_UNUSED BOOL bWait)
1421 WLog_ERR(
"TODO",
"TODO: implement");
1425BOOL WINAPI FreeRDP_WTSLogoffSession(WINPR_ATTR_UNUSED HANDLE hServer,
1426 WINPR_ATTR_UNUSED DWORD SessionId,
1427 WINPR_ATTR_UNUSED BOOL bWait)
1429 WLog_ERR(
"TODO",
"TODO: implement");
1433BOOL WINAPI FreeRDP_WTSShutdownSystem(WINPR_ATTR_UNUSED HANDLE hServer,
1434 WINPR_ATTR_UNUSED DWORD ShutdownFlag)
1436 WLog_ERR(
"TODO",
"TODO: implement");
1440BOOL WINAPI FreeRDP_WTSWaitSystemEvent(WINPR_ATTR_UNUSED HANDLE hServer,
1441 WINPR_ATTR_UNUSED DWORD EventMask,
1442 WINPR_ATTR_UNUSED DWORD* pEventFlags)
1444 WLog_ERR(
"TODO",
"TODO: implement");
1448static void peer_channel_queue_free_message(
void* obj)
1450 wMessage* msg = (wMessage*)obj;
1455 msg->context =
nullptr;
1459 UINT32 ChannelId, UINT16 index, UINT16 type,
size_t chunkSize,
1460 const char* name, UINT32 flags)
1462 wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1463 queueCallbacks.
fnObjectFree = peer_channel_queue_free_message;
1465 rdpPeerChannel* channel =
1466 server_channel_common_new(client, index, ChannelId, chunkSize, &queueCallbacks, name);
1469 WINPR_ASSERT(client);
1475 channel->channelType = type;
1476 channel->creationStatus =
1477 (type == RDP_PEER_CHANNEL_TYPE_SVC) ? ERROR_SUCCESS : ERROR_OPERATION_IN_PROGRESS;
1478 channel->channelFlags = flags;
1482 channel_free(channel);
1486static HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenStatic(HANDLE hServer,
1487 WINPR_ATTR_UNUSED DWORD SessionId,
1488 LPSTR pVirtualName, UINT32 Flags)
1493 SetLastError(ERROR_INVALID_DATA);
1497 freerdp_peer* client = vcm->client;
1498 WINPR_ASSERT(client);
1500 rdpContext* context = client->context;
1501 WINPR_ASSERT(context);
1502 WINPR_ASSERT(context->rdp);
1503 WINPR_ASSERT(context->settings);
1505 rdpMcs* mcs = context->rdp->mcs;
1508 size_t length = strnlen(pVirtualName, CHANNEL_NAME_LEN + 1);
1509 if (length > CHANNEL_NAME_LEN)
1511 SetLastError(ERROR_NOT_FOUND);
1515 rdpMcsChannel* joined_channel =
nullptr;
1517 for (; index < mcs->channelCount; index++)
1519 rdpMcsChannel* mchannel = &mcs->channels[index];
1520 if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
1522 joined_channel = mchannel;
1527 if (!joined_channel)
1529 SetLastError(ERROR_NOT_FOUND);
1533 rdpPeerChannel* channel = (rdpPeerChannel*)joined_channel->handle;
1536 const UINT32 VCChunkSize =
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);
1546 joined_channel->handle = channel;
1549 HANDLE hChannelHandle = (HANDLE)channel;
1550 return hChannelHandle;
1552 channel_free(channel);
1553 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1557HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId, LPSTR pVirtualName)
1559 return FreeRDP_WTSVirtualChannelOpenStatic(hServer, SessionId, pVirtualName, 0);
1562HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName, DWORD flags)
1565 rdpPeerChannel* channel =
nullptr;
1566 BOOL joined = FALSE;
1572 if (SessionId == WTS_CURRENT_SESSION)
1575 HashTable_Lock(g_ServerHandles);
1577 g_ServerHandles, (
void*)(UINT_PTR)SessionId);
1582 if (!(flags & WTS_CHANNEL_OPTION_DYNAMIC))
1584 HashTable_Unlock(g_ServerHandles);
1585 return FreeRDP_WTSVirtualChannelOpenStatic((HANDLE)vcm, SessionId, pVirtualName, flags);
1588 freerdp_peer* client = vcm->client;
1589 WINPR_ASSERT(client);
1590 WINPR_ASSERT(client->context);
1591 WINPR_ASSERT(client->context->rdp);
1593 rdpMcs* mcs = client->context->rdp->mcs;
1596 for (UINT32 index = 0; index < mcs->channelCount; index++)
1598 rdpMcsChannel* mchannel = &mcs->channels[index];
1599 if (mchannel->joined &&
1600 (strncmp(mchannel->Name, DRDYNVC_SVC_CHANNEL_NAME, CHANNEL_NAME_LEN + 1) == 0))
1609 SetLastError(ERROR_NOT_FOUND);
1613 if (!vcm->drdynvc_channel || (vcm->drdynvc_state != DRDYNVC_STATE_READY))
1615 SetLastError(ERROR_NOT_READY);
1619 WINPR_ASSERT(client);
1620 WINPR_ASSERT(client->context);
1621 WINPR_ASSERT(client->context->settings);
1623 const UINT32 VCChunkSize =
1626 channel_new(vcm, client, 0, 0, RDP_PEER_CHANNEL_TYPE_DVC, VCChunkSize, pVirtualName, flags);
1630 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1634 const LONG hdl = InterlockedIncrement(&vcm->dvc_channel_id_seq);
1635 channel->channelId = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1637 if (!HashTable_Insert(vcm->dynamicVirtualChannels, &channel->channelId, channel))
1639 channel_free(channel);
1643 s = Stream_New(
nullptr, 64);
1648 if (!wts_write_drdynvc_create_request(s, channel->channelId, pVirtualName))
1651 BOOL onTunnel = FALSE;
1652 if (!wts_tunnel_open_channel(vcm, channel, s, &onTunnel))
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,
1664 Stream_Free(s, TRUE);
1665 HashTable_Unlock(g_ServerHandles);
1669 Stream_Free(s, TRUE);
1672 HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1674 wts_tunnel_channel_opened(vcm);
1676 HashTable_Unlock(g_ServerHandles);
1678 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1682BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
1685 rdpMcs* mcs =
nullptr;
1687 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
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;
1700 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1702 if (channel->index < mcs->channelCount)
1704 rdpMcsChannel* cur = &mcs->channels[channel->index];
1705 rdpPeerChannel* peerChannel = (rdpPeerChannel*)cur->handle;
1706 channel_free(peerChannel);
1707 cur->handle =
nullptr;
1712 if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
1715 s = Stream_New(
nullptr, 8);
1719 WLog_ERR(TAG,
"Stream_New failed!");
1724 wts_write_drdynvc_header(s, CLOSE_REQUEST_PDU, channel->channelId);
1726 const size_t pos = Stream_GetPosition(s);
1727 WINPR_ASSERT(pos <= UINT32_MAX);
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);
1740 const BOOL waited = (channel->dvc_open_state == DVC_OPEN_STATE_NONE);
1741 HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1743 wts_tunnel_channel_opened(vcm);
1750BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, WINPR_ATTR_UNUSED ULONG TimeOut,
1751 PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead)
1753 BYTE* buffer =
nullptr;
1754 wMessage message = WINPR_C_ARRAY_INIT;
1755 wtsChannelMessage* messageCtx =
nullptr;
1756 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1758 WINPR_ASSERT(channel);
1760 if (!MessageQueue_Peek(channel->queue, &message, FALSE))
1762 SetLastError(ERROR_NO_DATA);
1767 messageCtx = message.context;
1769 if (messageCtx ==
nullptr)
1772 buffer = (BYTE*)(messageCtx + 1);
1773 *pBytesRead = messageCtx->length - messageCtx->offset;
1775 if (Buffer ==
nullptr || BufferSize == 0)
1780 if (*pBytesRead > BufferSize)
1781 *pBytesRead = BufferSize;
1783 CopyMemory(Buffer, buffer + messageCtx->offset, *pBytesRead);
1784 messageCtx->offset += *pBytesRead;
1786 if (messageCtx->offset >= messageCtx->length)
1788 const int rc = MessageQueue_Peek(channel->queue, &message, TRUE);
1789 peer_channel_queue_free_message(&message);
1797BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG uLength,
1798 PULONG pBytesWritten)
1804 BYTE* buffer =
nullptr;
1805 size_t totalWritten = 0;
1806 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1812 EnterCriticalSection(&channel->writeLock);
1813 WINPR_ASSERT(channel->vcm);
1814 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1816 buffer = (BYTE*)malloc(uLength);
1820 SetLastError(g_err_oom);
1824 CopyMemory(buffer, Buffer, uLength);
1825 totalWritten = uLength;
1826 if (!wts_queue_send_item(channel, buffer, uLength))
1829 else if (!channel->vcm->drdynvc_channel || (channel->vcm->drdynvc_state != DRDYNVC_STATE_READY))
1831 DEBUG_DVC(
"drdynvc not ready");
1838 size_t Length = uLength;
1841 s = Stream_New(
nullptr, DVC_MAX_DATA_PDU_SIZE);
1845 WLog_ERR(TAG,
"Stream_New failed!");
1846 SetLastError(g_err_oom);
1850 buffer = Stream_Buffer(s);
1851 Stream_Seek_UINT8(s);
1852 cbChId = wts_write_variable_uint(s, channel->channelId);
1854 if (first && (Length > Stream_GetRemainingLength(s)))
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;
1861 buffer[0] = ((DATA_PDU << 4) | cbChId) & 0xFF;
1865 size_t written = Stream_GetRemainingLength(s);
1867 if (written > Length)
1870 Stream_Write(s, Buffer, written);
1871 const size_t length = Stream_GetPosition(s);
1872 Stream_Free(s, FALSE);
1873 if (length > UINT32_MAX)
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);
1887 *pBytesWritten = WINPR_ASSERTING_INT_CAST(uint32_t, totalWritten);
1891 LeaveCriticalSection(&channel->writeLock);
1895BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1897 WLog_ERR(
"TODO",
"TODO: implement");
1901BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1903 WLog_ERR(
"TODO",
"TODO: implement");
1907BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS WtsVirtualClass,
1908 PVOID* ppBuffer, DWORD* pBytesReturned)
1910 void* pfd =
nullptr;
1912 void* fds[10] = WINPR_C_ARRAY_INIT;
1913 HANDLE hEvent =
nullptr;
1915 BOOL status = FALSE;
1916 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1918 WINPR_ASSERT(channel);
1920 switch ((UINT32)WtsVirtualClass)
1922 case WTSVirtualFileHandle:
1923 hEvent = MessageQueue_Event(channel->queue);
1924 pfd = GetEventWaitObject(hEvent);
1928 fds[fds_count] = pfd;
1932 *ppBuffer = malloc(
sizeof(
void*));
1936 SetLastError(g_err_oom);
1940 CopyMemory(*ppBuffer, (
void*)&fds[0],
sizeof(
void*));
1941 *pBytesReturned =
sizeof(
void*);
1947 case WTSVirtualEventHandle:
1948 hEvent = MessageQueue_Event(channel->queue);
1950 *ppBuffer = malloc(
sizeof(HANDLE));
1954 SetLastError(g_err_oom);
1958 CopyMemory(*ppBuffer, (
void*)&hEvent,
sizeof(HANDLE));
1959 *pBytesReturned =
sizeof(
void*);
1965 case WTSVirtualChannelReady:
1966 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1973 switch (channel->dvc_open_state)
1975 case DVC_OPEN_STATE_NONE:
1980 case DVC_OPEN_STATE_SUCCEEDED:
1986 *ppBuffer =
nullptr;
1987 *pBytesReturned = 0;
1992 *ppBuffer = malloc(
sizeof(BOOL));
1996 SetLastError(g_err_oom);
2001 CopyMemory(*ppBuffer, &bval,
sizeof(BOOL));
2002 *pBytesReturned =
sizeof(BOOL);
2006 case WTSVirtualChannelOpenStatus:
2008 INT32 value = channel->creationStatus;
2011 *ppBuffer = malloc(
sizeof(value));
2014 SetLastError(g_err_oom);
2019 CopyMemory(*ppBuffer, &value,
sizeof(value));
2020 *pBytesReturned =
sizeof(value);
2031VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory)
2036BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
2037 WINPR_ATTR_UNUSED PVOID pMemory,
2038 WINPR_ATTR_UNUSED ULONG NumberOfEntries)
2040 WLog_ERR(
"TODO",
"TODO: implement");
2044BOOL WINAPI FreeRDP_WTSFreeMemoryExA(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
2045 WINPR_ATTR_UNUSED PVOID pMemory,
2046 WINPR_ATTR_UNUSED ULONG NumberOfEntries)
2048 WLog_ERR(
"TODO",
"TODO: implement");
2052BOOL WINAPI FreeRDP_WTSRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd,
2053 WINPR_ATTR_UNUSED DWORD dwFlags)
2055 WLog_ERR(
"TODO",
"TODO: implement");
2059BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd)
2061 WLog_ERR(
"TODO",
"TODO: implement");
2065BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2066 WINPR_ATTR_UNUSED HWND hWnd,
2067 WINPR_ATTR_UNUSED DWORD dwFlags)
2069 WLog_ERR(
"TODO",
"TODO: implement");
2073BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2074 WINPR_ATTR_UNUSED HWND hWnd)
2076 WLog_ERR(
"TODO",
"TODO: implement");
2080BOOL WINAPI FreeRDP_WTSQueryUserToken(WINPR_ATTR_UNUSED ULONG SessionId,
2081 WINPR_ATTR_UNUSED PHANDLE phToken)
2083 WLog_ERR(
"TODO",
"TODO: implement");
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)
2093 WLog_ERR(
"TODO",
"TODO: implement");
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)
2103 WLog_ERR(
"TODO",
"TODO: implement");
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)
2113 WLog_ERR(
"TODO",
"TODO: implement");
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)
2123 WLog_ERR(
"TODO",
"TODO: implement");
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)
2133 WLog_ERR(
"TODO",
"TODO: implement");
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)
2143 WLog_ERR(
"TODO",
"TODO: implement");
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)
2154 WLog_ERR(
"TODO",
"TODO: implement");
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)
2165 WLog_ERR(
"TODO",
"TODO: implement");
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)
2175 WLog_ERR(
"TODO",
"TODO: implement");
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)
2185 WLog_ERR(
"TODO",
"TODO: implement");
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)
2196 WLog_ERR(
"TODO",
"TODO: implement");
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)
2207 WLog_ERR(
"TODO",
"TODO: implement");
2211BOOL CDECL FreeRDP_WTSEnableChildSessions(WINPR_ATTR_UNUSED BOOL bEnable)
2213 WLog_ERR(
"TODO",
"TODO: implement");
2217BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(WINPR_ATTR_UNUSED PBOOL pbEnabled)
2219 WLog_ERR(
"TODO",
"TODO: implement");
2223BOOL CDECL FreeRDP_WTSGetChildSessionId(WINPR_ATTR_UNUSED PULONG pSessionId)
2225 WLog_ERR(
"TODO",
"TODO: implement");
2229DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(
void)
2231 WLog_ERR(
"TODO",
"TODO: implement");
2234BOOL WINAPI FreeRDP_WTSLogoffUser(WINPR_ATTR_UNUSED HANDLE hServer)
2236 WLog_ERR(
"TODO",
"TODO: implement");
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)
2244 WLog_ERR(
"TODO",
"TODO: implement");
2248void server_channel_common_free(rdpPeerChannel* channel)
2252 MessageQueue_Free(channel->queue);
2253 Stream_Free(channel->receiveData, TRUE);
2254 DeleteCriticalSection(&channel->writeLock);
2258rdpPeerChannel* server_channel_common_new(freerdp_peer* client, UINT16 index, UINT32 channelId,
2259 size_t chunkSize,
const wObject* callback,
2262 rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1,
sizeof(rdpPeerChannel));
2266 InitializeCriticalSection(&channel->writeLock);
2268 channel->receiveData = Stream_New(
nullptr, chunkSize);
2269 if (!channel->receiveData)
2272 channel->queue = MessageQueue_New(callback);
2273 if (!channel->queue)
2276 channel->index = index;
2277 channel->client = client;
2278 channel->channelId = channelId;
2279 strncpy(channel->channelName, name, ARRAYSIZE(channel->channelName) - 1);
2282 WINPR_PRAGMA_DIAG_PUSH
2283 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2284 server_channel_common_free(channel);
2285 WINPR_PRAGMA_DIAG_POP
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.
OBJECT_FREE_FN fnObjectFree