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;
94 const size_t tlen =
sizeof(wtsChannelMessage) + len;
95 wtsChannelMessage* messageCtx = (wtsChannelMessage*)malloc(tlen);
99 WINPR_ASSERT(channel->channelId <= UINT16_MAX);
100 messageCtx->channelId = WINPR_ASSERTING_INT_CAST(UINT16, channel->channelId);
101 messageCtx->length = WINPR_ASSERTING_INT_CAST(UINT32, len);
102 messageCtx->offset = 0;
103 BYTE* buffer = (BYTE*)(&messageCtx[1]);
104 CopyMemory(buffer, Buffer1, Length1);
106 WINPR_ASSERT(Buffer2 || (Length2 == 0));
109 CopyMemory(buffer, Buffer2, Length2);
111 return MessageQueue_Post(channel->queue, messageCtx, 0,
nullptr,
nullptr);
114static BOOL wts_queue_send_item(rdpPeerChannel* channel, BYTE* Buffer, UINT32 Length)
116 BYTE* buffer =
nullptr;
119 WINPR_ASSERT(channel);
120 WINPR_ASSERT(channel->vcm);
124 WINPR_ASSERT(channel->channelId <= UINT16_MAX);
125 const UINT16 channelId = (UINT16)channel->channelId;
126 return MessageQueue_Post(channel->vcm->queue, (
void*)(UINT_PTR)channelId, 0, (
void*)buffer,
127 (
void*)(UINT_PTR)length);
130static unsigned wts_read_variable_uint(
wStream* s,
int cbLen, UINT32* val)
137 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
140 Stream_Read_UINT8(s, *val);
144 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
147 Stream_Read_UINT16(s, *val);
151 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
154 Stream_Read_UINT32(s, *val);
158 WLog_ERR(TAG,
"invalid wts variable uint len %d", cbLen);
163static BOOL wts_read_drdynvc_capabilities_response(rdpPeerChannel* channel,
wStream* s)
167 WINPR_ASSERT(channel);
168 WINPR_ASSERT(channel->vcm);
169 const size_t length = Stream_GetRemainingLength(s);
173 Stream_Seek_UINT8(s);
174 Stream_Read_UINT16(s, Version);
175 DEBUG_DVC(
"Version: %" PRIu16
"", Version);
179 WLog_ERR(TAG,
"invalid version %" PRIu16
" for DRDYNVC", Version);
184 vcm->drdynvc_state = DRDYNVC_STATE_READY;
187 vcm->dvc_spoken_version = MAX(Version, 1);
189 return SetEvent(MessageQueue_Event(vcm->queue));
192static BOOL wts_read_drdynvc_create_response(rdpPeerChannel* channel,
wStream* s)
194 UINT32 CreationStatus = 0;
197 WINPR_ASSERT(channel);
199 const size_t length = Stream_GetRemainingLength(s);
200 if ((length < 4) || (length > UINT32_MAX))
203 Stream_Read_UINT32(s, CreationStatus);
205 if ((INT32)CreationStatus < 0)
207 DEBUG_DVC(
"ChannelId %" PRIu32
" creation failed (%" PRId32
")", channel->channelId,
208 (INT32)CreationStatus);
209 channel->dvc_open_state = DVC_OPEN_STATE_FAILED;
213 DEBUG_DVC(
"ChannelId %" PRIu32
" creation succeeded", channel->channelId);
214 channel->dvc_open_state = DVC_OPEN_STATE_SUCCEEDED;
217 channel->creationStatus = (INT32)CreationStatus;
220 IFCALLRET(vcm->dvc_creation_status, status, vcm->dvc_creation_status_userdata,
221 channel->channelId, (INT32)CreationStatus);
223 WLog_ERR(TAG,
"vcm->dvc_creation_status failed!");
225 wts_tunnel_channel_opened(vcm);
229static BOOL wts_read_drdynvc_data_first(rdpPeerChannel* channel,
wStream* s,
int cbLen)
231 WINPR_ASSERT(channel);
234 const UINT32 value = wts_read_variable_uint(s, cbLen, &channel->dvc_total_length);
236 if ((value == 0) || (channel->dvc_total_length == 0))
239 const size_t length = Stream_GetRemainingLength(s);
240 if (length > channel->dvc_total_length)
243 Stream_ResetPosition(channel->receiveData);
245 if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
248 Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
252static BOOL wts_read_drdynvc_data(rdpPeerChannel* channel,
wStream* s)
256 WINPR_ASSERT(channel);
259 const size_t length = Stream_GetRemainingLength(s);
260 if (channel->dvc_total_length > 0)
262 const size_t pos = Stream_GetPosition(channel->receiveData);
263 if (pos > SIZE_MAX - length)
266 if (pos + length > channel->dvc_total_length)
268 channel->dvc_total_length = 0;
269 WLog_ERR(TAG,
"incorrect fragment data, discarded.");
273 if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
276 Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
278 if (Stream_GetPosition(channel->receiveData) >= channel->dvc_total_length)
280 ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData),
281 channel->dvc_total_length,
nullptr, 0);
282 channel->dvc_total_length = 0;
289 ret = wts_queue_receive_data(channel, Stream_ConstPointer(s), length,
nullptr, 0);
295static void wts_read_drdynvc_close_response(rdpPeerChannel* channel)
297 WINPR_ASSERT(channel);
298 DEBUG_DVC(
"ChannelId %" PRIu32
" close response", channel->channelId);
299 channel->dvc_open_state = DVC_OPEN_STATE_CLOSED;
300 MessageQueue_PostQuit(channel->queue, 0);
304static BOOL wts_read_drdynvc_pdu_ready_guarded(
wStream* s, UINT8 Cmd, UINT8 Sp, UINT32 ChannelId,
309 case CREATE_REQUEST_PDU:
310 return wts_read_drdynvc_create_response(dvc, s);
313 if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
316 "ChannelId %" PRIu32
" did not open successfully. "
317 "Ignoring DYNVC_DATA_FIRST PDU",
322 return wts_read_drdynvc_data_first(dvc, s, Sp);
325 if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
328 "ChannelId %" PRIu32
" did not open successfully. "
329 "Ignoring DYNVC_DATA PDU",
334 return wts_read_drdynvc_data(dvc, s);
336 case CLOSE_REQUEST_PDU:
337 wts_read_drdynvc_close_response(dvc);
340 case DATA_FIRST_COMPRESSED_PDU:
341 case DATA_COMPRESSED_PDU:
342 WLog_ERR(TAG,
"Compressed data not handled");
345 case SOFT_SYNC_RESPONSE_PDU:
346 WLog_ERR(TAG,
"SoftSync response not handled yet(and rather strange to receive "
347 "that packet as our code doesn't send SoftSync requests");
350 case SOFT_SYNC_REQUEST_PDU:
351 WLog_ERR(TAG,
"Not expecting a SoftSyncRequest on the server");
355 WLog_ERR(TAG,
"Cmd %d not recognized.", Cmd);
362static BOOL wts_read_drdynvc_pdu_ready(rdpPeerChannel* channel,
wStream* s, UINT8 Cmd, UINT8 Sp,
363 UINT8 cbChId, BOOL fromTunnel)
365 WINPR_ASSERT(channel);
367 BOOL haveChannelId = 0;
370 case SOFT_SYNC_REQUEST_PDU:
371 case SOFT_SYNC_RESPONSE_PDU:
372 haveChannelId = FALSE;
375 haveChannelId = TRUE;
379 UINT32 ChannelId = 0;
380 rdpPeerChannel* dvc =
nullptr;
381 wHashTable* table =
nullptr;
384 const unsigned val = wts_read_variable_uint(s, cbChId, &ChannelId);
388 DEBUG_DVC(
"Cmd %s ChannelId %" PRIu32
" length %" PRIuz
"", drdynvc_get_packet_type(Cmd),
389 ChannelId, Stream_GetRemainingLength(s));
390 table = channel->vcm->dynamicVirtualChannels;
393 HashTable_Lock(table);
394 dvc = wts_get_dvc_channel_by_id(channel->vcm, ChannelId);
397 DEBUG_DVC(
"ChannelId %" PRIu32
" does not exist.", ChannelId);
398 HashTable_Unlock(table);
403 if (fromTunnel && !dvc->tunnel)
405 WLog_WARN(TAG,
"%s for channel %" PRIu32
" on the tunnel, which it does not use",
406 drdynvc_get_packet_type(Cmd), ChannelId);
407 HashTable_Unlock(table);
412 if (wts_tunnel_is_soft_sync_response(Cmd))
413 return wts_tunnel_read_soft_sync_response(channel->vcm, s);
415 const BOOL rc = wts_read_drdynvc_pdu_ready_guarded(s, Cmd, Sp, ChannelId, dvc);
417 HashTable_Unlock(table);
422static BOOL wts_read_drdynvc_pdu(rdpPeerChannel* channel)
424 WINPR_ASSERT(channel);
425 WINPR_ASSERT(channel->vcm);
427 const size_t slength = Stream_GetPosition(channel->receiveData);
428 if ((slength < 1) || (slength > UINT32_MAX))
431 Stream_ResetPosition(channel->receiveData);
432 wStream buffer = WINPR_C_ARRAY_INIT;
433 wStream* s = Stream_StaticConstInit(&buffer, Stream_Buffer(channel->receiveData), slength);
434 const UINT8 value = Stream_Get_UINT8(s);
435 const UINT8 Cmd = (value & 0xf0) >> 4;
436 const UINT8 Sp = (value & 0x0c) >> 2;
437 const UINT8 cbChId = (value & 0x03) >> 0;
439 if (Cmd == CAPABILITY_REQUEST_PDU)
440 return wts_read_drdynvc_capabilities_response(channel, s);
442 if (channel->vcm->drdynvc_state == DRDYNVC_STATE_READY)
443 return wts_read_drdynvc_pdu_ready(channel, s, Cmd, Sp, cbChId, FALSE);
445 WLog_ERR(TAG,
"received Cmd %d but channel is not ready.", Cmd);
450static int wts_write_variable_uint(
wStream* s, UINT32 val)
458 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, val));
460 else if (val <= 0xFFFF)
463 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, val));
468 Stream_Write_UINT32(s, val);
474static void wts_write_drdynvc_header(
wStream* s, BYTE Cmd, UINT32 ChannelId)
478 BYTE* bm = Stream_PointerAs(s, BYTE);
479 Stream_Seek_UINT8(s);
480 const int cbChId = wts_write_variable_uint(s, ChannelId);
481 *bm = (((Cmd & 0x0F) << 4) | cbChId) & 0xFF;
484static BOOL wts_write_drdynvc_create_request(
wStream* s, UINT32 ChannelId,
const char* ChannelName)
489 WINPR_ASSERT(ChannelName);
491 wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
492 len = strlen(ChannelName) + 1;
494 if (!Stream_EnsureRemainingCapacity(s, len))
497 Stream_Write(s, ChannelName, len);
501static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId,
const BYTE* data,
502 size_t s, UINT32 flags,
size_t t)
506 const size_t totalSize = t;
508 WINPR_ASSERT(channel);
509 WINPR_ASSERT(channel->vcm);
510 WINPR_UNUSED(channelId);
512 if (channel->channelFlags & CHANNEL_OPTION_SHOW_PROTOCOL)
514 BOOL firstPass = TRUE;
518 const UINT32 payloadLen = (size > CHANNEL_CHUNK_LENGTH)
519 ? CHANNEL_CHUNK_LENGTH
520 : WINPR_ASSERTING_INT_CAST(UINT32, size);
530 if (flags & CHANNEL_FLAG_FIRST)
534 newFlags = CHANNEL_FLAG_FIRST;
538 if (!size && (flags & CHANNEL_FLAG_LAST))
539 newFlags |= CHANNEL_FLAG_LAST;
543 channel->offset += payloadLen;
544 if ((channel->total != t) || (channel->offset > t))
546 WLog_ERR(TAG,
"channelId %" PRIu16
" received %" PRIuz
" but limit is %" PRIuz,
547 channelId, channel->offset, t);
551 if (!wts_queue_receive_data(channel, &header,
sizeof(header), data, payloadLen))
559 if ((flags & CHANNEL_FLAG_FIRST) != 0)
561 Stream_ResetPosition(channel->receiveData);
564 if (!Stream_EnsureRemainingCapacity(channel->receiveData, size))
567 Stream_Write(channel->receiveData, data, size);
569 if ((flags & CHANNEL_FLAG_LAST) != 0)
571 if (Stream_GetPosition(channel->receiveData) != totalSize)
574 if (channel == channel->vcm->drdynvc_channel)
576 ret = wts_read_drdynvc_pdu(channel);
580 const size_t pos = Stream_GetPosition(channel->receiveData);
581 if (pos > UINT32_MAX)
584 ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData), pos,
588 Stream_ResetPosition(channel->receiveData);
590 else if (Stream_GetPosition(channel->receiveData) >= totalSize)
596static BOOL WTSReceiveChannelData(freerdp_peer* client, UINT16 channelId,
const BYTE* data,
597 size_t size, UINT32 flags,
size_t totalSize)
599 rdpMcs* mcs =
nullptr;
601 WINPR_ASSERT(client);
602 WINPR_ASSERT(client->context);
603 WINPR_ASSERT(client->context->rdp);
605 mcs = client->context->rdp->mcs;
608 for (UINT32 i = 0; i < mcs->channelCount; i++)
610 rdpMcsChannel* cur = &mcs->channels[i];
611 if (cur->ChannelId == channelId)
613 rdpPeerChannel* channel = (rdpPeerChannel*)cur->handle;
616 return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize);
620 WLog_WARN(TAG,
"unknown channelId %" PRIu16
" ignored", channelId);
625#if defined(WITH_FREERDP_DEPRECATED)
626void WTSVirtualChannelManagerGetFileDescriptor(HANDLE hServer,
void** fds,
int* fds_count)
632 WINPR_ASSERT(fds_count);
634 fd = GetEventWaitObject(MessageQueue_Event(vcm->queue));
638 fds[*fds_count] = fd;
644BOOL WTSVirtualChannelManagerOpen(HANDLE hServer)
651 if (vcm->drdynvc_state == DRDYNVC_STATE_NONE)
653 rdpPeerChannel* channel =
nullptr;
656 vcm->drdynvc_state = DRDYNVC_STATE_INITIALIZED;
657 channel = (rdpPeerChannel*)WTSVirtualChannelOpen((HANDLE)vcm, WTS_CURRENT_SESSION,
658 DRDYNVC_SVC_CHANNEL_NAME);
662 BYTE capaBuffer[12] = WINPR_C_ARRAY_INIT;
663 wStream staticS = WINPR_C_ARRAY_INIT;
664 wStream* s = Stream_StaticInit(&staticS, capaBuffer,
sizeof(capaBuffer));
666 vcm->drdynvc_channel = channel;
667 vcm->dvc_spoken_version = 1;
668 Stream_Write_UINT8(s, 0x50);
669 Stream_Write_UINT8(s, 0x00);
670 Stream_Write_UINT16(s, 0x0001);
674 const size_t pos = Stream_GetPosition(s);
675 WINPR_ASSERT(pos <= UINT32_MAX);
677 if (!WTSVirtualChannelWrite(channel, (PCHAR)capaBuffer, (UINT32)pos, &written))
685BOOL WTSVirtualChannelManagerCheckFileDescriptorEx(HANDLE hServer, BOOL autoOpen)
687 wMessage message = WINPR_C_ARRAY_INIT;
691 if (!hServer || hServer == INVALID_HANDLE_VALUE)
698 if (!WTSVirtualChannelManagerOpen(hServer))
702 if (!wts_tunnel_start_soft_sync_if_due(vcm))
705 while (MessageQueue_Peek(vcm->queue, &message, TRUE))
707 BYTE* buffer =
nullptr;
709 UINT16 channelId = 0;
710 channelId = (UINT16)(UINT_PTR)message.context;
711 buffer = (BYTE*)message.wParam;
712 length = (UINT32)(UINT_PTR)message.lParam;
714 WINPR_ASSERT(vcm->client);
715 WINPR_ASSERT(vcm->client->SendChannelData);
716 if (!wts_tunnel_send_message(vcm, message.id, buffer, length, &status) &&
717 !vcm->client->SendChannelData(vcm->client, channelId, buffer, length))
728 return wts_tunnel_after_drain(vcm, status);
731BOOL WTSVirtualChannelManagerCheckFileDescriptor(HANDLE hServer)
733 return WTSVirtualChannelManagerCheckFileDescriptorEx(hServer, TRUE);
736HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer)
740 return MessageQueue_Event(vcm->queue);
743static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs,
const char* channel_name)
745 if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN + 1))
748 for (UINT32 index = 0; index < mcs->channelCount; index++)
750 rdpMcsChannel* mchannel = &mcs->channels[index];
751 if (mchannel->joined)
753 if (_strnicmp(mchannel->Name, channel_name, CHANNEL_NAME_LEN + 1) == 0)
761static rdpMcsChannel* wts_get_joined_channel_by_id(rdpMcs* mcs,
const UINT16 channel_id)
763 if (!mcs || !channel_id)
766 WINPR_ASSERT(mcs->channels);
767 for (UINT32 index = 0; index < mcs->channelCount; index++)
769 rdpMcsChannel* mchannel = &mcs->channels[index];
770 if (mchannel->joined)
772 if (mchannel->ChannelId == channel_id)
773 return &mcs->channels[index];
780BOOL WTSIsChannelJoinedByName(freerdp_peer* client,
const char* channel_name)
782 if (!client || !client->context || !client->context->rdp)
785 return (wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name) !=
nullptr);
788BOOL WTSIsChannelJoinedById(freerdp_peer* client, UINT16 channel_id)
790 if (!client || !client->context || !client->context->rdp)
793 return (wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id) !=
nullptr);
796BOOL WTSVirtualChannelManagerIsChannelJoined(HANDLE hServer,
const char* name)
800 if (!vcm || !vcm->rdp)
803 return (wts_get_joined_channel_by_name(vcm->rdp->mcs, name) !=
nullptr);
806BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer)
810 return vcm->drdynvc_state;
813void WTSVirtualChannelManagerSetDVCCreationCallback(HANDLE hServer, psDVCCreationStatusCallback cb,
820 vcm->dvc_creation_status = cb;
821 vcm->dvc_creation_status_userdata = userdata;
824UINT16 WTSChannelGetId(freerdp_peer* client,
const char* channel_name)
826 rdpMcsChannel* channel =
nullptr;
828 WINPR_ASSERT(channel_name);
829 if (!client || !client->context || !client->context->rdp)
832 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
837 return channel->ChannelId;
840UINT32 WTSChannelGetIdByHandle(HANDLE hChannelHandle)
842 rdpPeerChannel* channel = hChannelHandle;
844 WINPR_ASSERT(channel);
846 return channel->channelId;
849BOOL WTSChannelSetHandleByName(freerdp_peer* client,
const char* channel_name,
void* handle)
851 rdpMcsChannel* channel =
nullptr;
853 WINPR_ASSERT(channel_name);
854 if (!client || !client->context || !client->context->rdp)
857 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
862 channel->handle = handle;
866BOOL WTSChannelSetHandleById(freerdp_peer* client, UINT16 channel_id,
void* handle)
868 rdpMcsChannel* channel =
nullptr;
870 if (!client || !client->context || !client->context->rdp)
873 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
878 channel->handle = handle;
882void* WTSChannelGetHandleByName(freerdp_peer* client,
const char* channel_name)
884 rdpMcsChannel* channel =
nullptr;
886 WINPR_ASSERT(channel_name);
887 if (!client || !client->context || !client->context->rdp)
890 channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
895 return channel->handle;
898void* WTSChannelGetHandleById(freerdp_peer* client, UINT16 channel_id)
900 rdpMcsChannel* channel =
nullptr;
902 if (!client || !client->context || !client->context->rdp)
905 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
910 return channel->handle;
913const char* WTSChannelGetName(freerdp_peer* client, UINT16 channel_id)
915 rdpMcsChannel* channel =
nullptr;
917 if (!client || !client->context || !client->context->rdp)
920 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
925 return (
const char*)channel->Name;
928char** WTSGetAcceptedChannelNames(freerdp_peer* client,
size_t* count)
930 rdpMcs* mcs =
nullptr;
931 char** names =
nullptr;
933 if (!client || !client->context || !count)
936 WINPR_ASSERT(client->context->rdp);
937 mcs = client->context->rdp->mcs;
939 *count = mcs->channelCount;
941 names = (
char**)calloc(mcs->channelCount,
sizeof(
char*));
945 for (UINT32 index = 0; index < mcs->channelCount; index++)
947 rdpMcsChannel* mchannel = &mcs->channels[index];
948 names[index] = mchannel->Name;
954INT64 WTSChannelGetOptions(freerdp_peer* client, UINT16 channel_id)
956 rdpMcsChannel* channel =
nullptr;
958 if (!client || !client->context || !client->context->rdp)
961 channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
966 return (INT64)channel->options;
969BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
970 WINPR_ATTR_UNUSED ULONG TargetLogonId,
971 WINPR_ATTR_UNUSED BYTE HotkeyVk,
972 WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
974 WLog_ERR(
"TODO",
"TODO: implement");
978BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
979 WINPR_ATTR_UNUSED ULONG TargetLogonId,
980 WINPR_ATTR_UNUSED BYTE HotkeyVk,
981 WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
983 WLog_ERR(
"TODO",
"TODO: implement");
987BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
988 WINPR_ATTR_UNUSED ULONG TargetLogonId,
989 WINPR_ATTR_UNUSED BYTE HotkeyVk,
990 WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
991 WINPR_ATTR_UNUSED DWORD flags)
993 WLog_ERR(
"TODO",
"TODO: implement");
997BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
998 WINPR_ATTR_UNUSED ULONG TargetLogonId,
999 WINPR_ATTR_UNUSED BYTE HotkeyVk,
1000 WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
1001 WINPR_ATTR_UNUSED DWORD flags)
1003 WLog_ERR(
"TODO",
"TODO: implement");
1007BOOL WINAPI FreeRDP_WTSStopRemoteControlSession(WINPR_ATTR_UNUSED ULONG LogonId)
1009 WLog_ERR(
"TODO",
"TODO: implement");
1013BOOL WINAPI FreeRDP_WTSConnectSessionW(WINPR_ATTR_UNUSED ULONG LogonId,
1014 WINPR_ATTR_UNUSED ULONG TargetLogonId,
1015 WINPR_ATTR_UNUSED PWSTR pPassword,
1016 WINPR_ATTR_UNUSED BOOL bWait)
1018 WLog_ERR(
"TODO",
"TODO: implement");
1022BOOL WINAPI FreeRDP_WTSConnectSessionA(WINPR_ATTR_UNUSED ULONG LogonId,
1023 WINPR_ATTR_UNUSED ULONG TargetLogonId,
1024 WINPR_ATTR_UNUSED PSTR pPassword,
1025 WINPR_ATTR_UNUSED BOOL bWait)
1027 WLog_ERR(
"TODO",
"TODO: implement");
1031BOOL WINAPI FreeRDP_WTSEnumerateServersW(WINPR_ATTR_UNUSED LPWSTR pDomainName,
1032 WINPR_ATTR_UNUSED DWORD Reserved,
1033 WINPR_ATTR_UNUSED DWORD Version,
1034 WINPR_ATTR_UNUSED PWTS_SERVER_INFOW* ppServerInfo,
1035 WINPR_ATTR_UNUSED DWORD* pCount)
1037 WLog_ERR(
"TODO",
"TODO: implement");
1041BOOL WINAPI FreeRDP_WTSEnumerateServersA(WINPR_ATTR_UNUSED LPSTR pDomainName,
1042 WINPR_ATTR_UNUSED DWORD Reserved,
1043 WINPR_ATTR_UNUSED DWORD Version,
1044 WINPR_ATTR_UNUSED PWTS_SERVER_INFOA* ppServerInfo,
1045 WINPR_ATTR_UNUSED DWORD* pCount)
1047 WLog_ERR(
"TODO",
"TODO: implement");
1051HANDLE WINAPI FreeRDP_WTSOpenServerW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1053 WLog_ERR(
"TODO",
"TODO: implement");
1054 return INVALID_HANDLE_VALUE;
1057static void wts_virtual_channel_manager_free_message(
void* obj)
1059 wMessage* msg = (wMessage*)obj;
1063 BYTE* buffer = (BYTE*)msg->wParam;
1070static void channel_free(rdpPeerChannel* channel)
1072 server_channel_common_free(channel);
1075static void array_channel_free(
void* ptr)
1077 rdpPeerChannel* channel = ptr;
1078 channel_free(channel);
1081static BOOL dynChannelMatch(
const void* v1,
const void* v2)
1083 const UINT32* p1 = (
const UINT32*)v1;
1084 const UINT32* p2 = (
const UINT32*)v2;
1088static UINT32 channelId_Hash(
const void* key)
1090 const UINT32* v = (
const UINT32*)key;
1094static void clearHandles(
void)
1096 HashTable_Free(g_ServerHandles);
1097 g_ServerHandles =
nullptr;
1100static BOOL CALLBACK initializeHandles(WINPR_ATTR_UNUSED PINIT_ONCE once,
1101 WINPR_ATTR_UNUSED PVOID param,
1102 WINPR_ATTR_UNUSED PVOID* context)
1104 WINPR_ASSERT(g_ServerHandles ==
nullptr);
1105 g_ServerHandles = HashTable_New(TRUE);
1106 (void)winpr_atexit(clearHandles);
1107 return g_ServerHandles !=
nullptr;
1110static bool setup(
void)
1112 return InitOnceExecuteOnce(&g_HandleInitializer, initializeHandles,
nullptr,
nullptr);
1119 HashTable_Lock(g_ServerHandles);
1123#ifdef __clang_analyzer__
1124 const BOOL valid = vcm !=
nullptr;
1126 const BOOL valid = (vcm !=
nullptr) && (vcm != INVALID_HANDLE_VALUE);
1130 HashTable_Remove(g_ServerHandles, (
void*)(UINT_PTR)vcm->SessionId);
1132 wts_tunnel_free(vcm);
1134 HashTable_Free(vcm->dynamicVirtualChannels);
1136 if (vcm->drdynvc_channel)
1139 (void)WTSVirtualChannelClose(vcm->drdynvc_channel);
1140 vcm->drdynvc_channel =
nullptr;
1143 MessageQueue_Free(vcm->queue);
1146 HashTable_Unlock(g_ServerHandles);
1149HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName)
1151 wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1153 rdpContext* context = WINPR_PACKED_ALIGN_CAST(rdpContext*, pServerName);
1155 if (!setup() || !context)
1156 return INVALID_HANDLE_VALUE;
1158 freerdp_peer* client = context->peer;
1162 SetLastError(ERROR_INVALID_DATA);
1163 return INVALID_HANDLE_VALUE;
1171 vcm->client = client;
1172 vcm->rdp = context->rdp;
1174 queueCallbacks.
fnObjectFree = wts_virtual_channel_manager_free_message;
1175 vcm->queue = MessageQueue_New(&queueCallbacks);
1180 vcm->dvc_channel_id_seq = 0;
1181 vcm->dynamicVirtualChannels = HashTable_New(TRUE);
1183 if (!vcm->dynamicVirtualChannels)
1186 if (!HashTable_SetHashFunction(vcm->dynamicVirtualChannels, channelId_Hash))
1190 wObject* obj = HashTable_ValueObject(vcm->dynamicVirtualChannels);
1192 obj->fnObjectFree = array_channel_free;
1194 obj = HashTable_KeyObject(vcm->dynamicVirtualChannels);
1195 obj->fnObjectEquals = dynChannelMatch;
1197 client->ReceiveChannelData = WTSReceiveChannelData;
1199 if (!wts_tunnel_new(vcm, context, wts_read_drdynvc_pdu_ready))
1202 HashTable_Lock(g_ServerHandles);
1203 vcm->SessionId = g_SessionId++;
1205 HashTable_Insert(g_ServerHandles, (
void*)(UINT_PTR)vcm->SessionId, (
void*)vcm);
1206 HashTable_Unlock(g_ServerHandles);
1211 HANDLE hServer = (HANDLE)vcm;
1215 wtsCloseVCM(vcm,
false);
1216 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1217 return INVALID_HANDLE_VALUE;
1220HANDLE WINAPI FreeRDP_WTSOpenServerExW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1222 WLog_ERR(
"TODO",
"TODO: implement");
1223 return INVALID_HANDLE_VALUE;
1226HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName)
1228 return FreeRDP_WTSOpenServerA(pServerName);
1231VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer)
1234 wtsCloseVCM(vcm,
true);
1237BOOL WINAPI FreeRDP_WTSEnumerateSessionsW(WINPR_ATTR_UNUSED HANDLE hServer,
1238 WINPR_ATTR_UNUSED DWORD Reserved,
1239 WINPR_ATTR_UNUSED DWORD Version,
1240 WINPR_ATTR_UNUSED PWTS_SESSION_INFOW* ppSessionInfo,
1241 WINPR_ATTR_UNUSED DWORD* pCount)
1243 WLog_ERR(
"TODO",
"TODO: implement");
1247BOOL WINAPI FreeRDP_WTSEnumerateSessionsA(WINPR_ATTR_UNUSED HANDLE hServer,
1248 WINPR_ATTR_UNUSED DWORD Reserved,
1249 WINPR_ATTR_UNUSED DWORD Version,
1250 WINPR_ATTR_UNUSED PWTS_SESSION_INFOA* ppSessionInfo,
1251 WINPR_ATTR_UNUSED DWORD* pCount)
1253 WLog_ERR(
"TODO",
"TODO: implement");
1257BOOL WINAPI FreeRDP_WTSEnumerateSessionsExW(WINPR_ATTR_UNUSED HANDLE hServer,
1258 WINPR_ATTR_UNUSED DWORD* pLevel,
1259 WINPR_ATTR_UNUSED DWORD Filter,
1260 WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1W* ppSessionInfo,
1261 WINPR_ATTR_UNUSED DWORD* pCount)
1263 WLog_ERR(
"TODO",
"TODO: implement");
1267BOOL WINAPI FreeRDP_WTSEnumerateSessionsExA(WINPR_ATTR_UNUSED HANDLE hServer,
1268 WINPR_ATTR_UNUSED DWORD* pLevel,
1269 WINPR_ATTR_UNUSED DWORD Filter,
1270 WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1A* ppSessionInfo,
1271 WINPR_ATTR_UNUSED DWORD* pCount)
1273 WLog_ERR(
"TODO",
"TODO: implement");
1277BOOL WINAPI FreeRDP_WTSEnumerateProcessesW(WINPR_ATTR_UNUSED HANDLE hServer,
1278 WINPR_ATTR_UNUSED DWORD Reserved,
1279 WINPR_ATTR_UNUSED DWORD Version,
1280 WINPR_ATTR_UNUSED PWTS_PROCESS_INFOW* ppProcessInfo,
1281 WINPR_ATTR_UNUSED DWORD* pCount)
1283 WLog_ERR(
"TODO",
"TODO: implement");
1287BOOL WINAPI FreeRDP_WTSEnumerateProcessesA(WINPR_ATTR_UNUSED HANDLE hServer,
1288 WINPR_ATTR_UNUSED DWORD Reserved,
1289 WINPR_ATTR_UNUSED DWORD Version,
1290 WINPR_ATTR_UNUSED PWTS_PROCESS_INFOA* ppProcessInfo,
1291 WINPR_ATTR_UNUSED DWORD* pCount)
1293 WLog_ERR(
"TODO",
"TODO: implement");
1297BOOL WINAPI FreeRDP_WTSTerminateProcess(WINPR_ATTR_UNUSED HANDLE hServer,
1298 WINPR_ATTR_UNUSED DWORD ProcessId,
1299 WINPR_ATTR_UNUSED DWORD ExitCode)
1301 WLog_ERR(
"TODO",
"TODO: implement");
1305BOOL WINAPI FreeRDP_WTSQuerySessionInformationW(WINPR_ATTR_UNUSED HANDLE hServer,
1306 WINPR_ATTR_UNUSED DWORD SessionId,
1307 WINPR_ATTR_UNUSED WTS_INFO_CLASS WTSInfoClass,
1308 WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1309 WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1311 WLog_ERR(
"TODO",
"TODO: implement");
1315BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1316 WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer,
1317 DWORD* pBytesReturned)
1319 DWORD BytesReturned = 0;
1326 if (WTSInfoClass == WTSSessionId)
1328 ULONG* pBuffer =
nullptr;
1329 BytesReturned =
sizeof(ULONG);
1330 pBuffer = (ULONG*)malloc(
sizeof(BytesReturned));
1334 SetLastError(g_err_oom);
1338 *pBuffer = vcm->SessionId;
1339 *ppBuffer = (LPSTR)pBuffer;
1340 *pBytesReturned = BytesReturned;
1347BOOL WINAPI FreeRDP_WTSQueryUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1348 WINPR_ATTR_UNUSED LPWSTR pUserName,
1349 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1350 WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1351 WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1353 WLog_ERR(
"TODO",
"TODO: implement");
1357BOOL WINAPI FreeRDP_WTSQueryUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1358 WINPR_ATTR_UNUSED LPSTR pUserName,
1359 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1360 WINPR_ATTR_UNUSED LPSTR* ppBuffer,
1361 WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1363 WLog_ERR(
"TODO",
"TODO: implement");
1367BOOL WINAPI FreeRDP_WTSSetUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1368 WINPR_ATTR_UNUSED LPWSTR pUserName,
1369 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1370 WINPR_ATTR_UNUSED LPWSTR pBuffer,
1371 WINPR_ATTR_UNUSED DWORD DataLength)
1373 WLog_ERR(
"TODO",
"TODO: implement");
1377BOOL WINAPI FreeRDP_WTSSetUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1378 WINPR_ATTR_UNUSED LPSTR pUserName,
1379 WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1380 WINPR_ATTR_UNUSED LPSTR pBuffer,
1381 WINPR_ATTR_UNUSED DWORD DataLength)
1383 WLog_ERR(
"TODO",
"TODO: implement");
1388FreeRDP_WTSSendMessageW(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1389 WINPR_ATTR_UNUSED LPWSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1390 WINPR_ATTR_UNUSED LPWSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1391 WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1392 WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1394 WLog_ERR(
"TODO",
"TODO: implement");
1399FreeRDP_WTSSendMessageA(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1400 WINPR_ATTR_UNUSED LPSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1401 WINPR_ATTR_UNUSED LPSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1402 WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1403 WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1405 WLog_ERR(
"TODO",
"TODO: implement");
1409BOOL WINAPI FreeRDP_WTSDisconnectSession(WINPR_ATTR_UNUSED HANDLE hServer,
1410 WINPR_ATTR_UNUSED DWORD SessionId,
1411 WINPR_ATTR_UNUSED BOOL bWait)
1413 WLog_ERR(
"TODO",
"TODO: implement");
1417BOOL WINAPI FreeRDP_WTSLogoffSession(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_WTSShutdownSystem(WINPR_ATTR_UNUSED HANDLE hServer,
1426 WINPR_ATTR_UNUSED DWORD ShutdownFlag)
1428 WLog_ERR(
"TODO",
"TODO: implement");
1432BOOL WINAPI FreeRDP_WTSWaitSystemEvent(WINPR_ATTR_UNUSED HANDLE hServer,
1433 WINPR_ATTR_UNUSED DWORD EventMask,
1434 WINPR_ATTR_UNUSED DWORD* pEventFlags)
1436 WLog_ERR(
"TODO",
"TODO: implement");
1440static void peer_channel_queue_free_message(
void* obj)
1442 wMessage* msg = (wMessage*)obj;
1447 msg->context =
nullptr;
1451 UINT32 ChannelId, UINT16 index, UINT16 type,
size_t chunkSize,
1452 const char* name, UINT32 flags)
1454 wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1455 queueCallbacks.
fnObjectFree = peer_channel_queue_free_message;
1457 rdpPeerChannel* channel =
1458 server_channel_common_new(client, index, ChannelId, chunkSize, &queueCallbacks, name);
1461 WINPR_ASSERT(client);
1467 channel->channelType = type;
1468 channel->creationStatus =
1469 (type == RDP_PEER_CHANNEL_TYPE_SVC) ? ERROR_SUCCESS : ERROR_OPERATION_IN_PROGRESS;
1470 channel->channelFlags = flags;
1474 channel_free(channel);
1478static HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenStatic(HANDLE hServer,
1479 WINPR_ATTR_UNUSED DWORD SessionId,
1480 LPSTR pVirtualName, UINT32 Flags)
1485 SetLastError(ERROR_INVALID_DATA);
1489 freerdp_peer* client = vcm->client;
1490 WINPR_ASSERT(client);
1492 rdpContext* context = client->context;
1493 WINPR_ASSERT(context);
1494 WINPR_ASSERT(context->rdp);
1495 WINPR_ASSERT(context->settings);
1497 rdpMcs* mcs = context->rdp->mcs;
1500 size_t length = strnlen(pVirtualName, CHANNEL_NAME_LEN + 1);
1501 if (length > CHANNEL_NAME_LEN)
1503 SetLastError(ERROR_NOT_FOUND);
1507 rdpMcsChannel* joined_channel =
nullptr;
1509 for (; index < mcs->channelCount; index++)
1511 rdpMcsChannel* mchannel = &mcs->channels[index];
1512 if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
1514 joined_channel = mchannel;
1519 if (!joined_channel)
1521 SetLastError(ERROR_NOT_FOUND);
1525 rdpPeerChannel* channel = (rdpPeerChannel*)joined_channel->handle;
1528 const UINT32 VCChunkSize =
1531 WINPR_ASSERT(index <= UINT16_MAX);
1532 channel = channel_new(vcm, client, joined_channel->ChannelId, (UINT16)index,
1533 RDP_PEER_CHANNEL_TYPE_SVC, VCChunkSize, pVirtualName, Flags);
1538 joined_channel->handle = channel;
1541 HANDLE hChannelHandle = (HANDLE)channel;
1542 return hChannelHandle;
1544 channel_free(channel);
1545 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1549HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId, LPSTR pVirtualName)
1551 return FreeRDP_WTSVirtualChannelOpenStatic(hServer, SessionId, pVirtualName, 0);
1554HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName, DWORD flags)
1557 rdpPeerChannel* channel =
nullptr;
1558 BOOL joined = FALSE;
1564 if (SessionId == WTS_CURRENT_SESSION)
1567 HashTable_Lock(g_ServerHandles);
1569 g_ServerHandles, (
void*)(UINT_PTR)SessionId);
1574 if (!(flags & WTS_CHANNEL_OPTION_DYNAMIC))
1576 HashTable_Unlock(g_ServerHandles);
1577 return FreeRDP_WTSVirtualChannelOpenStatic((HANDLE)vcm, SessionId, pVirtualName, flags);
1580 freerdp_peer* client = vcm->client;
1581 WINPR_ASSERT(client);
1582 WINPR_ASSERT(client->context);
1583 WINPR_ASSERT(client->context->rdp);
1585 rdpMcs* mcs = client->context->rdp->mcs;
1588 for (UINT32 index = 0; index < mcs->channelCount; index++)
1590 rdpMcsChannel* mchannel = &mcs->channels[index];
1591 if (mchannel->joined &&
1592 (strncmp(mchannel->Name, DRDYNVC_SVC_CHANNEL_NAME, CHANNEL_NAME_LEN + 1) == 0))
1601 SetLastError(ERROR_NOT_FOUND);
1605 if (!vcm->drdynvc_channel || (vcm->drdynvc_state != DRDYNVC_STATE_READY))
1607 SetLastError(ERROR_NOT_READY);
1611 WINPR_ASSERT(client);
1612 WINPR_ASSERT(client->context);
1613 WINPR_ASSERT(client->context->settings);
1615 const UINT32 VCChunkSize =
1618 channel_new(vcm, client, 0, 0, RDP_PEER_CHANNEL_TYPE_DVC, VCChunkSize, pVirtualName, flags);
1622 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1626 const LONG hdl = InterlockedIncrement(&vcm->dvc_channel_id_seq);
1627 channel->channelId = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1629 if (!HashTable_Insert(vcm->dynamicVirtualChannels, &channel->channelId, channel))
1631 channel_free(channel);
1635 s = Stream_New(
nullptr, 64);
1640 if (!wts_write_drdynvc_create_request(s, channel->channelId, pVirtualName))
1643 BOOL onTunnel = FALSE;
1644 if (!wts_tunnel_open_channel(vcm, channel, s, &onTunnel))
1648 const size_t pos = Stream_GetPosition(s);
1649 WINPR_ASSERT(pos <= UINT32_MAX);
1650 if (!WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s,
char), (UINT32)pos,
1656 Stream_Free(s, TRUE);
1657 HashTable_Unlock(g_ServerHandles);
1661 Stream_Free(s, TRUE);
1664 HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1666 wts_tunnel_channel_opened(vcm);
1668 HashTable_Unlock(g_ServerHandles);
1670 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1674BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
1677 rdpMcs* mcs =
nullptr;
1679 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1687 WINPR_ASSERT(vcm->client);
1688 WINPR_ASSERT(vcm->client->context);
1689 WINPR_ASSERT(vcm->client->context->rdp);
1690 mcs = vcm->client->context->rdp->mcs;
1692 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1694 if (channel->index < mcs->channelCount)
1696 rdpMcsChannel* cur = &mcs->channels[channel->index];
1697 rdpPeerChannel* peerChannel = (rdpPeerChannel*)cur->handle;
1698 channel_free(peerChannel);
1699 cur->handle =
nullptr;
1704 if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
1707 s = Stream_New(
nullptr, 8);
1711 WLog_ERR(TAG,
"Stream_New failed!");
1716 wts_write_drdynvc_header(s, CLOSE_REQUEST_PDU, channel->channelId);
1718 const size_t pos = Stream_GetPosition(s);
1719 WINPR_ASSERT(pos <= UINT32_MAX);
1722 EnterCriticalSection(&channel->writeLock);
1723 if (!wts_tunnel_close_request(channel, Stream_Buffer(s), pos, &ret))
1724 ret = WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s,
char),
1725 (UINT32)pos, &written);
1726 LeaveCriticalSection(&channel->writeLock);
1727 Stream_Free(s, TRUE);
1732 const BOOL waited = (channel->dvc_open_state == DVC_OPEN_STATE_NONE);
1733 HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1735 wts_tunnel_channel_opened(vcm);
1742BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, WINPR_ATTR_UNUSED ULONG TimeOut,
1743 PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead)
1745 BYTE* buffer =
nullptr;
1746 wMessage message = WINPR_C_ARRAY_INIT;
1747 wtsChannelMessage* messageCtx =
nullptr;
1748 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1750 WINPR_ASSERT(channel);
1752 if (!MessageQueue_Peek(channel->queue, &message, FALSE))
1754 SetLastError(ERROR_NO_DATA);
1759 messageCtx = message.context;
1761 if (messageCtx ==
nullptr)
1764 buffer = (BYTE*)(messageCtx + 1);
1765 *pBytesRead = messageCtx->length - messageCtx->offset;
1767 if (Buffer ==
nullptr || BufferSize == 0)
1772 if (*pBytesRead > BufferSize)
1773 *pBytesRead = BufferSize;
1775 CopyMemory(Buffer, buffer + messageCtx->offset, *pBytesRead);
1776 messageCtx->offset += *pBytesRead;
1778 if (messageCtx->offset >= messageCtx->length)
1780 const int rc = MessageQueue_Peek(channel->queue, &message, TRUE);
1781 peer_channel_queue_free_message(&message);
1789BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG uLength,
1790 PULONG pBytesWritten)
1796 BYTE* buffer =
nullptr;
1797 size_t totalWritten = 0;
1798 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1804 EnterCriticalSection(&channel->writeLock);
1805 WINPR_ASSERT(channel->vcm);
1806 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1808 buffer = (BYTE*)malloc(uLength);
1812 SetLastError(g_err_oom);
1816 CopyMemory(buffer, Buffer, uLength);
1817 totalWritten = uLength;
1818 if (!wts_queue_send_item(channel, buffer, uLength))
1821 else if (!channel->vcm->drdynvc_channel || (channel->vcm->drdynvc_state != DRDYNVC_STATE_READY))
1823 DEBUG_DVC(
"drdynvc not ready");
1830 size_t Length = uLength;
1833 s = Stream_New(
nullptr, DVC_MAX_DATA_PDU_SIZE);
1837 WLog_ERR(TAG,
"Stream_New failed!");
1838 SetLastError(g_err_oom);
1842 buffer = Stream_Buffer(s);
1843 Stream_Seek_UINT8(s);
1844 cbChId = wts_write_variable_uint(s, channel->channelId);
1846 if (first && (Length > Stream_GetRemainingLength(s)))
1848 cbLen = wts_write_variable_uint(s, WINPR_ASSERTING_INT_CAST(uint32_t, Length));
1849 buffer[0] = ((DATA_FIRST_PDU << 4) | (cbLen << 2) | cbChId) & 0xFF;
1853 buffer[0] = ((DATA_PDU << 4) | cbChId) & 0xFF;
1857 size_t written = Stream_GetRemainingLength(s);
1859 if (written > Length)
1862 Stream_Write(s, Buffer, written);
1863 const size_t length = Stream_GetPosition(s);
1864 Stream_Free(s, FALSE);
1865 if (length > UINT32_MAX)
1869 totalWritten += written;
1870 BOOL queued = FALSE;
1871 if (!wts_tunnel_queue_send_item(channel, buffer, (UINT32)length, &queued))
1872 queued = wts_queue_send_item(channel->vcm->drdynvc_channel, buffer, (UINT32)length);
1879 *pBytesWritten = WINPR_ASSERTING_INT_CAST(uint32_t, totalWritten);
1883 LeaveCriticalSection(&channel->writeLock);
1887BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1889 WLog_ERR(
"TODO",
"TODO: implement");
1893BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1895 WLog_ERR(
"TODO",
"TODO: implement");
1899BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS WtsVirtualClass,
1900 PVOID* ppBuffer, DWORD* pBytesReturned)
1902 void* pfd =
nullptr;
1904 void* fds[10] = WINPR_C_ARRAY_INIT;
1905 HANDLE hEvent =
nullptr;
1907 BOOL status = FALSE;
1908 rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1910 WINPR_ASSERT(channel);
1912 switch ((UINT32)WtsVirtualClass)
1914 case WTSVirtualFileHandle:
1915 hEvent = MessageQueue_Event(channel->queue);
1916 pfd = GetEventWaitObject(hEvent);
1920 fds[fds_count] = pfd;
1924 *ppBuffer = malloc(
sizeof(
void*));
1928 SetLastError(g_err_oom);
1932 CopyMemory(*ppBuffer, (
void*)&fds[0],
sizeof(
void*));
1933 *pBytesReturned =
sizeof(
void*);
1939 case WTSVirtualEventHandle:
1940 hEvent = MessageQueue_Event(channel->queue);
1942 *ppBuffer = malloc(
sizeof(HANDLE));
1946 SetLastError(g_err_oom);
1950 CopyMemory(*ppBuffer, (
void*)&hEvent,
sizeof(HANDLE));
1951 *pBytesReturned =
sizeof(
void*);
1957 case WTSVirtualChannelReady:
1958 if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1965 switch (channel->dvc_open_state)
1967 case DVC_OPEN_STATE_NONE:
1972 case DVC_OPEN_STATE_SUCCEEDED:
1978 *ppBuffer =
nullptr;
1979 *pBytesReturned = 0;
1984 *ppBuffer = malloc(
sizeof(BOOL));
1988 SetLastError(g_err_oom);
1993 CopyMemory(*ppBuffer, &bval,
sizeof(BOOL));
1994 *pBytesReturned =
sizeof(BOOL);
1998 case WTSVirtualChannelOpenStatus:
2000 INT32 value = channel->creationStatus;
2003 *ppBuffer = malloc(
sizeof(value));
2006 SetLastError(g_err_oom);
2011 CopyMemory(*ppBuffer, &value,
sizeof(value));
2012 *pBytesReturned =
sizeof(value);
2023VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory)
2028BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
2029 WINPR_ATTR_UNUSED PVOID pMemory,
2030 WINPR_ATTR_UNUSED ULONG NumberOfEntries)
2032 WLog_ERR(
"TODO",
"TODO: implement");
2036BOOL WINAPI FreeRDP_WTSFreeMemoryExA(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_WTSRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd,
2045 WINPR_ATTR_UNUSED DWORD dwFlags)
2047 WLog_ERR(
"TODO",
"TODO: implement");
2051BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd)
2053 WLog_ERR(
"TODO",
"TODO: implement");
2057BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2058 WINPR_ATTR_UNUSED HWND hWnd,
2059 WINPR_ATTR_UNUSED DWORD dwFlags)
2061 WLog_ERR(
"TODO",
"TODO: implement");
2065BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2066 WINPR_ATTR_UNUSED HWND hWnd)
2068 WLog_ERR(
"TODO",
"TODO: implement");
2072BOOL WINAPI FreeRDP_WTSQueryUserToken(WINPR_ATTR_UNUSED ULONG SessionId,
2073 WINPR_ATTR_UNUSED PHANDLE phToken)
2075 WLog_ERR(
"TODO",
"TODO: implement");
2079BOOL WINAPI FreeRDP_WTSEnumerateProcessesExW(WINPR_ATTR_UNUSED HANDLE hServer,
2080 WINPR_ATTR_UNUSED DWORD* pLevel,
2081 WINPR_ATTR_UNUSED DWORD SessionId,
2082 WINPR_ATTR_UNUSED LPWSTR* ppProcessInfo,
2083 WINPR_ATTR_UNUSED DWORD* pCount)
2085 WLog_ERR(
"TODO",
"TODO: implement");
2089BOOL WINAPI FreeRDP_WTSEnumerateProcessesExA(WINPR_ATTR_UNUSED HANDLE hServer,
2090 WINPR_ATTR_UNUSED DWORD* pLevel,
2091 WINPR_ATTR_UNUSED DWORD SessionId,
2092 WINPR_ATTR_UNUSED LPSTR* ppProcessInfo,
2093 WINPR_ATTR_UNUSED DWORD* pCount)
2095 WLog_ERR(
"TODO",
"TODO: implement");
2099BOOL WINAPI FreeRDP_WTSEnumerateListenersW(WINPR_ATTR_UNUSED HANDLE hServer,
2100 WINPR_ATTR_UNUSED PVOID pReserved,
2101 WINPR_ATTR_UNUSED DWORD Reserved,
2102 WINPR_ATTR_UNUSED PWTSLISTENERNAMEW pListeners,
2103 WINPR_ATTR_UNUSED DWORD* pCount)
2105 WLog_ERR(
"TODO",
"TODO: implement");
2109BOOL WINAPI FreeRDP_WTSEnumerateListenersA(WINPR_ATTR_UNUSED HANDLE hServer,
2110 WINPR_ATTR_UNUSED PVOID pReserved,
2111 WINPR_ATTR_UNUSED DWORD Reserved,
2112 WINPR_ATTR_UNUSED PWTSLISTENERNAMEA pListeners,
2113 WINPR_ATTR_UNUSED DWORD* pCount)
2115 WLog_ERR(
"TODO",
"TODO: implement");
2119BOOL WINAPI FreeRDP_WTSQueryListenerConfigW(WINPR_ATTR_UNUSED HANDLE hServer,
2120 WINPR_ATTR_UNUSED PVOID pReserved,
2121 WINPR_ATTR_UNUSED DWORD Reserved,
2122 WINPR_ATTR_UNUSED LPWSTR pListenerName,
2123 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer)
2125 WLog_ERR(
"TODO",
"TODO: implement");
2129BOOL WINAPI FreeRDP_WTSQueryListenerConfigA(WINPR_ATTR_UNUSED HANDLE hServer,
2130 WINPR_ATTR_UNUSED PVOID pReserved,
2131 WINPR_ATTR_UNUSED DWORD Reserved,
2132 WINPR_ATTR_UNUSED LPSTR pListenerName,
2133 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer)
2135 WLog_ERR(
"TODO",
"TODO: implement");
2139BOOL WINAPI FreeRDP_WTSCreateListenerW(WINPR_ATTR_UNUSED HANDLE hServer,
2140 WINPR_ATTR_UNUSED PVOID pReserved,
2141 WINPR_ATTR_UNUSED DWORD Reserved,
2142 WINPR_ATTR_UNUSED LPWSTR pListenerName,
2143 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer,
2144 WINPR_ATTR_UNUSED DWORD flag)
2146 WLog_ERR(
"TODO",
"TODO: implement");
2150BOOL WINAPI FreeRDP_WTSCreateListenerA(WINPR_ATTR_UNUSED HANDLE hServer,
2151 WINPR_ATTR_UNUSED PVOID pReserved,
2152 WINPR_ATTR_UNUSED DWORD Reserved,
2153 WINPR_ATTR_UNUSED LPSTR pListenerName,
2154 WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer,
2155 WINPR_ATTR_UNUSED DWORD flag)
2157 WLog_ERR(
"TODO",
"TODO: implement");
2161BOOL WINAPI FreeRDP_WTSSetListenerSecurityW(
2162 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2163 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2164 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2165 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2167 WLog_ERR(
"TODO",
"TODO: implement");
2171BOOL WINAPI FreeRDP_WTSSetListenerSecurityA(
2172 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2173 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2174 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2175 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2177 WLog_ERR(
"TODO",
"TODO: implement");
2181BOOL WINAPI FreeRDP_WTSGetListenerSecurityW(
2182 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2183 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2184 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2185 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2186 WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2188 WLog_ERR(
"TODO",
"TODO: implement");
2192BOOL WINAPI FreeRDP_WTSGetListenerSecurityA(
2193 WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2194 WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2195 WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2196 WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2197 WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2199 WLog_ERR(
"TODO",
"TODO: implement");
2203BOOL CDECL FreeRDP_WTSEnableChildSessions(WINPR_ATTR_UNUSED BOOL bEnable)
2205 WLog_ERR(
"TODO",
"TODO: implement");
2209BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(WINPR_ATTR_UNUSED PBOOL pbEnabled)
2211 WLog_ERR(
"TODO",
"TODO: implement");
2215BOOL CDECL FreeRDP_WTSGetChildSessionId(WINPR_ATTR_UNUSED PULONG pSessionId)
2217 WLog_ERR(
"TODO",
"TODO: implement");
2221DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(
void)
2223 WLog_ERR(
"TODO",
"TODO: implement");
2226BOOL WINAPI FreeRDP_WTSLogoffUser(WINPR_ATTR_UNUSED HANDLE hServer)
2228 WLog_ERR(
"TODO",
"TODO: implement");
2232BOOL WINAPI FreeRDP_WTSLogonUser(WINPR_ATTR_UNUSED HANDLE hServer,
2233 WINPR_ATTR_UNUSED LPCSTR username,
2234 WINPR_ATTR_UNUSED LPCSTR password, WINPR_ATTR_UNUSED LPCSTR domain)
2236 WLog_ERR(
"TODO",
"TODO: implement");
2240void server_channel_common_free(rdpPeerChannel* channel)
2244 MessageQueue_Free(channel->queue);
2245 Stream_Free(channel->receiveData, TRUE);
2246 DeleteCriticalSection(&channel->writeLock);
2250rdpPeerChannel* server_channel_common_new(freerdp_peer* client, UINT16 index, UINT32 channelId,
2251 size_t chunkSize,
const wObject* callback,
2254 rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1,
sizeof(rdpPeerChannel));
2258 InitializeCriticalSection(&channel->writeLock);
2260 channel->receiveData = Stream_New(
nullptr, chunkSize);
2261 if (!channel->receiveData)
2264 channel->queue = MessageQueue_New(callback);
2265 if (!channel->queue)
2268 channel->index = index;
2269 channel->client = client;
2270 channel->channelId = channelId;
2271 strncpy(channel->channelName, name, ARRAYSIZE(channel->channelName) - 1);
2274 WINPR_PRAGMA_DIAG_PUSH
2275 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2276 server_channel_common_free(channel);
2277 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