22 #include <freerdp/config.h>
26 #include <winpr/assert.h>
28 #include <freerdp/log.h>
29 #include <freerdp/channels/drdynvc.h>
34 #define TAG FREERDP_TAG("core.client")
38 freerdp_channel_handle_fkt_t fkt;
45 static WINPR_TLS freerdp* g_Instance = NULL;
48 static volatile LONG g_OpenHandleSeq = 1;
51 static INIT_ONCE g_ChannelHandlesOnce = INIT_ONCE_STATIC_INIT;
52 static wHashTable* g_ChannelHandles = NULL;
54 static BOOL freerdp_channels_process_message_free(wMessage* message, DWORD type);
56 static CHANNEL_OPEN_DATA* freerdp_channels_find_channel_open_data_by_name(rdpChannels* channels,
59 for (
int index = 0; index < channels->openDataCount; index++)
63 if (strncmp(name, pChannelOpenData->name, CHANNEL_NAME_LEN + 1) == 0)
64 return pChannelOpenData;
71 static rdpMcsChannel* freerdp_channels_find_channel_by_name(rdpRdp* rdp,
const char* name)
80 for (UINT32 index = 0; index < mcs->channelCount; index++)
82 rdpMcsChannel* channel = &mcs->channels[index];
84 if (strncmp(name, channel->Name, CHANNEL_NAME_LEN + 1) == 0)
93 static rdpMcsChannel* freerdp_channels_find_channel_by_id(rdpRdp* rdp, UINT16 channel_id)
95 rdpMcsChannel* channel = NULL;
103 for (UINT32 index = 0; index < mcs->channelCount; index++)
105 channel = &mcs->channels[index];
107 if (channel->ChannelId == channel_id)
116 static void channel_queue_message_free(wMessage* msg)
120 if (!msg || (msg->id != 0))
127 static void channel_queue_free(
void* obj)
129 wMessage* msg = (wMessage*)obj;
130 freerdp_channels_process_message_free(msg, CHANNEL_EVENT_WRITE_CANCELLED);
131 channel_queue_message_free(msg);
134 static BOOL CALLBACK init_channel_handles_table(
PINIT_ONCE once, PVOID param, PVOID* context)
136 g_ChannelHandles = HashTable_New(TRUE);
140 static void* channel_event_entry_clone(
const void* data)
142 const ChannelEventEntry* entry = data;
146 ChannelEventEntry* copy = calloc(1,
sizeof(ChannelEventEntry));
153 rdpChannels* freerdp_channels_new(freerdp* instance)
156 rdpChannels* channels = NULL;
157 channels = (rdpChannels*)calloc(1,
sizeof(rdpChannels));
162 InitOnceExecuteOnce(&g_ChannelHandlesOnce, init_channel_handles_table, NULL, NULL);
164 if (!g_ChannelHandles)
166 if (!InitializeCriticalSectionAndSpinCount(&channels->channelsLock, 4000))
169 channels->instance = instance;
170 channels->queue = MessageQueue_New(NULL);
172 if (!channels->queue)
175 obj = MessageQueue_Object(channels->queue);
176 obj->fnObjectFree = channel_queue_free;
178 channels->channelEvents = HashTable_New(FALSE);
179 if (!channels->channelEvents)
182 obj = HashTable_ValueObject(channels->channelEvents);
184 obj->fnObjectFree = free;
185 obj->fnObjectNew = channel_event_entry_clone;
188 WINPR_PRAGMA_DIAG_PUSH
189 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
190 freerdp_channels_free(channels);
191 WINPR_PRAGMA_DIAG_POP
195 void freerdp_channels_free(rdpChannels* channels)
200 HashTable_Free(channels->channelEvents);
202 DeleteCriticalSection(&channels->channelsLock);
206 MessageQueue_Free(channels->queue);
207 channels->queue = NULL;
218 static UINT freerdp_drdynvc_on_channel_connected(DrdynvcClientContext* context,
const char* name,
221 UINT status = CHANNEL_RC_OK;
222 ChannelConnectedEventArgs e = { 0 };
223 rdpChannels* channels = (rdpChannels*)context->custom;
224 freerdp* instance = channels->instance;
225 EventArgsInit(&e,
"freerdp");
227 e.pInterface = pInterface;
228 PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e);
237 static UINT freerdp_drdynvc_on_channel_disconnected(DrdynvcClientContext* context,
const char* name,
240 UINT status = CHANNEL_RC_OK;
241 ChannelDisconnectedEventArgs e = { 0 };
242 rdpChannels* channels = (rdpChannels*)context->custom;
243 freerdp* instance = channels->instance;
244 EventArgsInit(&e,
"freerdp");
246 e.pInterface = pInterface;
247 PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e);
251 static UINT freerdp_drdynvc_on_channel_attached(DrdynvcClientContext* context,
const char* name,
254 UINT status = CHANNEL_RC_OK;
255 ChannelAttachedEventArgs e = { 0 };
256 rdpChannels* channels = (rdpChannels*)context->custom;
257 freerdp* instance = channels->instance;
258 EventArgsInit(&e,
"freerdp");
260 e.pInterface = pInterface;
261 PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e);
265 static UINT freerdp_drdynvc_on_channel_detached(DrdynvcClientContext* context,
const char* name,
268 UINT status = CHANNEL_RC_OK;
269 ChannelDetachedEventArgs e = { 0 };
270 rdpChannels* channels = (rdpChannels*)context->custom;
271 freerdp* instance = channels->instance;
272 EventArgsInit(&e,
"freerdp");
274 e.pInterface = pInterface;
275 PubSub_OnChannelDetached(instance->context->pubSub, instance->context, &e);
279 void freerdp_channels_register_instance(rdpChannels* channels, freerdp* instance)
282 g_Instance = instance;
289 UINT freerdp_channels_pre_connect(rdpChannels* channels, freerdp* instance)
291 UINT error = CHANNEL_RC_OK;
294 MessageQueue_Clear(channels->queue);
296 for (
int index = 0; index < channels->clientDataCount; index++)
298 pChannelClientData = &channels->clientDataList[index];
300 if (pChannelClientData->pChannelInitEventProc)
302 pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
303 CHANNEL_EVENT_INITIALIZED, 0, 0);
305 else if (pChannelClientData->pChannelInitEventProcEx)
307 pChannelClientData->pChannelInitEventProcEx(pChannelClientData->lpUserParam,
308 pChannelClientData->pInitHandle,
309 CHANNEL_EVENT_INITIALIZED, 0, 0);
312 if (CHANNEL_RC_OK != getChannelError(instance->context))
319 UINT freerdp_channels_attach(freerdp* instance)
321 UINT error = CHANNEL_RC_OK;
322 const char* hostname = NULL;
323 size_t hostnameLength = 0;
324 rdpChannels* channels = NULL;
327 WINPR_ASSERT(instance);
328 WINPR_ASSERT(instance->context);
329 WINPR_ASSERT(instance->context->settings);
331 channels = instance->context->channels;
333 WINPR_ASSERT(hostname);
334 hostnameLength = strnlen(hostname, MAX_PATH);
336 for (
int index = 0; index < channels->clientDataCount; index++)
343 ChannelAttachedEventArgs e = { 0 };
347 pChannelClientData = &channels->clientDataList[index];
349 if (pChannelClientData->pChannelInitEventProc)
352 pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
353 CHANNEL_EVENT_ATTACHED, cnv.pv,
354 (UINT)hostnameLength);
356 else if (pChannelClientData->pChannelInitEventProcEx)
358 pChannelClientData->pChannelInitEventProcEx(
359 pChannelClientData->lpUserParam, pChannelClientData->pInitHandle,
360 CHANNEL_EVENT_ATTACHED, cnv.pv, (UINT)hostnameLength);
363 if (getChannelError(instance->context) != CHANNEL_RC_OK)
366 pChannelOpenData = &channels->openDataList[index];
367 EventArgsInit(&e,
"freerdp");
368 e.name = pChannelOpenData->name;
369 e.pInterface = pChannelOpenData->pInterface;
370 PubSub_OnChannelAttached(instance->context->pubSub, instance->context, &e);
377 UINT freerdp_channels_detach(freerdp* instance)
379 UINT error = CHANNEL_RC_OK;
380 const char* hostname = NULL;
381 size_t hostnameLength = 0;
382 rdpChannels* channels = NULL;
383 rdpContext* context = NULL;
386 WINPR_ASSERT(instance);
388 context = instance->context;
389 WINPR_ASSERT(context);
391 channels = context->channels;
392 WINPR_ASSERT(channels);
394 WINPR_ASSERT(context->settings);
396 WINPR_ASSERT(hostname);
397 hostnameLength = strnlen(hostname, MAX_PATH);
399 for (
int index = 0; index < channels->clientDataCount; index++)
407 ChannelDetachedEventArgs e = { 0 };
411 pChannelClientData = &channels->clientDataList[index];
413 if (pChannelClientData->pChannelInitEventProc)
415 pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
416 CHANNEL_EVENT_DETACHED, cnv.pv,
417 (UINT)hostnameLength);
419 else if (pChannelClientData->pChannelInitEventProcEx)
421 pChannelClientData->pChannelInitEventProcEx(
422 pChannelClientData->lpUserParam, pChannelClientData->pInitHandle,
423 CHANNEL_EVENT_DETACHED, cnv.pv, (UINT)hostnameLength);
426 if (getChannelError(context) != CHANNEL_RC_OK)
429 pChannelOpenData = &channels->openDataList[index];
430 EventArgsInit(&e,
"freerdp");
431 e.name = pChannelOpenData->name;
432 e.pInterface = pChannelOpenData->pInterface;
433 PubSub_OnChannelDetached(context->pubSub, context, &e);
445 UINT freerdp_channels_post_connect(rdpChannels* channels, freerdp* instance)
447 UINT error = CHANNEL_RC_OK;
448 const char* hostname = NULL;
449 size_t hostnameLength = 0;
452 WINPR_ASSERT(channels);
453 WINPR_ASSERT(instance);
454 WINPR_ASSERT(instance->context);
455 WINPR_ASSERT(instance->context->settings);
457 channels->connected = TRUE;
459 WINPR_ASSERT(hostname);
460 hostnameLength = strnlen(hostname, MAX_PATH);
462 for (
int index = 0; index < channels->clientDataCount; index++)
469 ChannelConnectedEventArgs e = { 0 };
471 pChannelClientData = &channels->clientDataList[index];
474 if (pChannelClientData->pChannelInitEventProc)
476 pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
477 CHANNEL_EVENT_CONNECTED, cnv.pb,
478 (UINT)hostnameLength);
480 else if (pChannelClientData->pChannelInitEventProcEx)
482 pChannelClientData->pChannelInitEventProcEx(
483 pChannelClientData->lpUserParam, pChannelClientData->pInitHandle,
484 CHANNEL_EVENT_CONNECTED, cnv.pb, (UINT)hostnameLength);
487 error = getChannelError(instance->context);
488 if (error != CHANNEL_RC_OK)
491 pChannelOpenData = &channels->openDataList[index];
492 EventArgsInit(&e,
"freerdp");
493 e.name = pChannelOpenData->name;
494 e.pInterface = pChannelOpenData->pInterface;
495 PubSub_OnChannelConnected(instance->context->pubSub, instance->context, &e);
498 channels->drdynvc = (DrdynvcClientContext*)freerdp_channels_get_static_channel_interface(
499 channels, DRDYNVC_SVC_CHANNEL_NAME);
501 if (channels->drdynvc)
503 channels->drdynvc->custom = (
void*)channels;
504 channels->drdynvc->OnChannelConnected = freerdp_drdynvc_on_channel_connected;
505 channels->drdynvc->OnChannelDisconnected = freerdp_drdynvc_on_channel_disconnected;
506 channels->drdynvc->OnChannelAttached = freerdp_drdynvc_on_channel_attached;
507 channels->drdynvc->OnChannelDetached = freerdp_drdynvc_on_channel_detached;
514 BOOL freerdp_channels_data(freerdp* instance, UINT16 channelId,
const BYTE* cdata,
size_t dataSize,
515 UINT32 flags,
size_t totalSize)
518 rdpChannels* channels = NULL;
519 rdpMcsChannel* channel = NULL;
528 if (!instance || !data.pcb)
530 WLog_ERR(TAG,
"(%p, %" PRIu16
", %p, 0x%08x): Invalid arguments", instance, channelId,
535 mcs = instance->context->rdp->mcs;
536 channels = instance->context->channels;
538 if (!channels || !mcs)
543 for (UINT32 index = 0; index < mcs->channelCount; index++)
545 rdpMcsChannel* cur = &mcs->channels[index];
547 if (cur->ChannelId == channelId)
559 pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, channel->Name);
561 if (!pChannelOpenData)
566 if (pChannelOpenData->pChannelOpenEventProc)
568 pChannelOpenData->pChannelOpenEventProc(pChannelOpenData->OpenHandle,
569 CHANNEL_EVENT_DATA_RECEIVED, data.pb,
570 (UINT32)dataSize, (UINT32)totalSize, flags);
572 else if (pChannelOpenData->pChannelOpenEventProcEx)
574 pChannelOpenData->pChannelOpenEventProcEx(
575 pChannelOpenData->lpUserParam, pChannelOpenData->OpenHandle,
576 CHANNEL_EVENT_DATA_RECEIVED, data.pb, (UINT32)dataSize, (UINT32)totalSize, flags);
582 UINT16 freerdp_channels_get_id_by_name(freerdp* instance,
const char* channel_name)
584 if (!instance || !channel_name)
587 rdpMcsChannel* mcsChannel =
588 freerdp_channels_find_channel_by_name(instance->context->rdp, channel_name);
592 return mcsChannel->ChannelId;
595 const char* freerdp_channels_get_name_by_id(freerdp* instance, UINT16 channelId)
597 rdpMcsChannel* mcsChannel = NULL;
601 mcsChannel = freerdp_channels_find_channel_by_id(instance->context->rdp, channelId);
605 return mcsChannel->Name;
608 BOOL freerdp_channels_process_message_free(wMessage* message, DWORD type)
610 if (message->id == WMQ_QUIT)
615 if (message->id == 0)
623 pChannelOpenData = item->pChannelOpenData;
625 if (pChannelOpenData->pChannelOpenEventProc)
627 pChannelOpenData->pChannelOpenEventProc(pChannelOpenData->OpenHandle, type,
628 item->UserData, item->DataLength,
629 item->DataLength, 0);
631 else if (pChannelOpenData->pChannelOpenEventProcEx)
633 pChannelOpenData->pChannelOpenEventProcEx(
634 pChannelOpenData->lpUserParam, pChannelOpenData->OpenHandle, type, item->UserData,
635 item->DataLength, item->DataLength, 0);
642 static BOOL freerdp_channels_process_message(freerdp* instance, wMessage* message)
647 WINPR_ASSERT(instance);
648 WINPR_ASSERT(message);
650 if (message->id == WMQ_QUIT)
652 else if (message->id == 0)
654 rdpMcsChannel* channel = NULL;
661 pChannelOpenData = item->pChannelOpenData;
662 if (pChannelOpenData->flags != 2)
664 freerdp_channels_process_message_free(message, CHANNEL_EVENT_WRITE_CANCELLED);
668 freerdp_channels_find_channel_by_name(instance->context->rdp, pChannelOpenData->name);
671 ret = instance->SendChannelData(instance, channel->ChannelId, item->Data,
675 if (!freerdp_channels_process_message_free(message, CHANNEL_EVENT_WRITE_COMPLETE))
681 IFCALL(message->Free, message);
688 static BOOL freerdp_channels_process_sync(rdpChannels* channels, freerdp* instance)
691 wMessage message = { 0 };
693 WINPR_ASSERT(channels);
695 while (MessageQueue_Peek(channels->queue, &message, TRUE))
697 if (!freerdp_channels_process_message(instance, &message))
707 #if defined(WITH_FREERDP_DEPRECATED)
708 BOOL freerdp_channels_get_fds(rdpChannels* channels, freerdp* instance,
void** read_fds,
709 int* read_count,
void** write_fds,
int* write_count)
712 pfd = GetEventWaitObject(MessageQueue_Event(channels->queue));
716 read_fds[*read_count] = pfd;
724 void* freerdp_channels_get_static_channel_interface(rdpChannels* channels,
const char* name)
726 void* pInterface = NULL;
728 freerdp_channels_find_channel_open_data_by_name(channels, name);
730 if (pChannelOpenData)
731 pInterface = pChannelOpenData->pInterface;
736 HANDLE freerdp_channels_get_event_handle(freerdp* instance)
739 return INVALID_HANDLE_VALUE;
741 WINPR_ASSERT(instance->context);
743 rdpChannels* channels = instance->context->channels;
744 WINPR_ASSERT(channels);
746 return MessageQueue_Event(channels->queue);
749 static BOOL channels_process(
const void* key,
void* value,
void* arg)
751 ChannelEventEntry* entry = value;
752 rdpContext* context = arg;
758 return entry->fkt(context, entry->userdata);
761 int freerdp_channels_process_pending_messages(freerdp* instance)
766 WINPR_ASSERT(instance->context);
768 rdpChannels* channels = instance->context->channels;
769 WINPR_ASSERT(channels);
771 const DWORD status = WaitForSingleObject(MessageQueue_Event(channels->queue), 0);
772 if (status == WAIT_OBJECT_0)
774 if (!freerdp_channels_process_sync(channels, instance))
778 if (!HashTable_Foreach(channels->channelEvents, channels_process, instance->context))
787 BOOL freerdp_channels_check_fds(rdpChannels* channels, freerdp* instance)
789 WINPR_ASSERT(channels);
790 WINPR_UNUSED(channels);
792 const int rc = freerdp_channels_process_pending_messages(instance);
796 BOOL freerdp_client_channel_register(rdpChannels* channels, HANDLE handle,
797 freerdp_channel_handle_fkt_t fkt,
void* userdata)
799 if (!channels || (handle == INVALID_HANDLE_VALUE) || !fkt)
801 WLog_ERR(TAG,
"Invalid function arguments (channels=%p, handle=%p, fkt=%p, userdata=%p",
802 channels, handle, fkt, userdata);
806 ChannelEventEntry entry = { .fkt = fkt, .userdata = userdata };
807 return HashTable_Insert(channels->channelEvents, handle, &entry);
810 BOOL freerdp_client_channel_unregister(rdpChannels* channels, HANDLE handle)
812 if (!channels || (handle == INVALID_HANDLE_VALUE))
814 WLog_ERR(TAG,
"Invalid function arguments (channels=%p, handle=%p", channels, handle);
818 return HashTable_Remove(channels->channelEvents, handle);
821 SSIZE_T freerdp_client_channel_get_registered_event_handles(rdpChannels* channels, HANDLE* events,
826 WINPR_ASSERT(channels);
827 WINPR_ASSERT(events || (count == 0));
829 HashTable_Lock(channels->channelEvents);
830 size_t len = HashTable_Count(channels->channelEvents);
833 ULONG_PTR* keys = NULL;
834 const size_t nrKeys = HashTable_GetKeys(channels->channelEvents, &keys);
835 if ((nrKeys <= SSIZE_MAX) && (nrKeys == len))
837 for (
size_t x = 0; x < nrKeys; x++)
839 HANDLE cur = (HANDLE)keys[x];
842 rc = (SSIZE_T)nrKeys;
846 HashTable_Unlock(channels->channelEvents);
850 UINT freerdp_channels_disconnect(rdpChannels* channels, freerdp* instance)
852 UINT error = CHANNEL_RC_OK;
856 WINPR_ASSERT(channels);
858 if (!channels->connected)
861 (void)freerdp_channels_check_fds(channels, instance);
864 for (
int index = 0; index < channels->clientDataCount; index++)
866 ChannelDisconnectedEventArgs e = { 0 };
867 pChannelClientData = &channels->clientDataList[index];
869 if (pChannelClientData->pChannelInitEventProc)
871 pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
872 CHANNEL_EVENT_DISCONNECTED, 0, 0);
874 else if (pChannelClientData->pChannelInitEventProcEx)
876 pChannelClientData->pChannelInitEventProcEx(pChannelClientData->lpUserParam,
877 pChannelClientData->pInitHandle,
878 CHANNEL_EVENT_DISCONNECTED, 0, 0);
881 if (getChannelError(instance->context) != CHANNEL_RC_OK)
884 pChannelOpenData = &channels->openDataList[index];
885 EventArgsInit(&e,
"freerdp");
886 e.name = pChannelOpenData->name;
887 e.pInterface = pChannelOpenData->pInterface;
888 PubSub_OnChannelDisconnected(instance->context->pubSub, instance->context, &e);
891 channels->connected = FALSE;
894 (void)freerdp_channels_check_fds(channels, instance);
898 void freerdp_channels_close(rdpChannels* channels, freerdp* instance)
903 WINPR_ASSERT(channels);
904 WINPR_ASSERT(instance);
906 MessageQueue_PostQuit(channels->queue, 0);
907 (void)freerdp_channels_check_fds(channels, instance);
910 for (
int index = 0; index < channels->clientDataCount; index++)
912 pChannelClientData = &channels->clientDataList[index];
914 if (pChannelClientData->pChannelInitEventProc)
916 pChannelClientData->pChannelInitEventProc(pChannelClientData->pInitHandle,
917 CHANNEL_EVENT_TERMINATED, 0, 0);
919 else if (pChannelClientData->pChannelInitEventProcEx)
921 pChannelClientData->pChannelInitEventProcEx(pChannelClientData->lpUserParam,
922 pChannelClientData->pInitHandle,
923 CHANNEL_EVENT_TERMINATED, 0, 0);
927 for (
int index = 0; index < channels->openDataCount; index++)
929 pChannelOpenData = &channels->openDataList[index];
930 HashTable_Remove(g_ChannelHandles, (
void*)(UINT_PTR)pChannelOpenData->OpenHandle);
933 channels->openDataCount = 0;
934 channels->initDataCount = 0;
935 channels->clientDataCount = 0;
937 WINPR_ASSERT(instance->context);
938 WINPR_ASSERT(instance->context->settings);
939 instance->context->settings->ChannelCount = 0;
943 static UINT VCAPITYPE FreeRDP_VirtualChannelInitEx(
944 LPVOID lpUserParam, LPVOID clientContext, LPVOID pInitHandle,
PCHANNEL_DEF pChannel,
945 INT channelCount, ULONG versionRequested, PCHANNEL_INIT_EVENT_EX_FN pChannelInitEventProcEx)
947 rdpSettings* settings = NULL;
950 rdpChannels* channels = NULL;
953 return CHANNEL_RC_BAD_INIT_HANDLE;
956 return CHANNEL_RC_BAD_CHANNEL;
958 if ((channelCount <= 0) || !pChannelInitEventProcEx)
959 return CHANNEL_RC_INITIALIZATION_ERROR;
962 WINPR_ASSERT(pChannelInitData);
964 channels = pChannelInitData->channels;
965 WINPR_ASSERT(channels);
967 if (!channels->can_call_init)
968 return CHANNEL_RC_NOT_IN_VIRTUALCHANNELENTRY;
970 if ((channels->openDataCount + channelCount) > CHANNEL_MAX_COUNT)
971 return CHANNEL_RC_TOO_MANY_CHANNELS;
973 if (channels->connected)
974 return CHANNEL_RC_ALREADY_CONNECTED;
976 if (versionRequested != VIRTUAL_CHANNEL_VERSION_WIN2000)
980 for (
int index = 0; index < channelCount; index++)
984 if (freerdp_channels_find_channel_open_data_by_name(channels, pChannelDef->name) != 0)
986 return CHANNEL_RC_BAD_CHANNEL;
990 pChannelInitData->pInterface = clientContext;
991 pChannelClientData = &channels->clientDataList[channels->clientDataCount];
992 pChannelClientData->pChannelInitEventProcEx = pChannelInitEventProcEx;
993 pChannelClientData->pInitHandle = pInitHandle;
994 pChannelClientData->lpUserParam = lpUserParam;
995 channels->clientDataCount++;
997 WINPR_ASSERT(channels->instance);
998 WINPR_ASSERT(channels->instance->context);
999 settings = channels->instance->context->settings;
1000 WINPR_ASSERT(settings);
1002 for (
int index = 0; index < channelCount; index++)
1005 CHANNEL_OPEN_DATA* pChannelOpenData = &channels->openDataList[channels->openDataCount];
1007 WINPR_ASSERT(pChannelOpenData);
1009 const LONG hdl = InterlockedIncrement(&g_OpenHandleSeq);
1010 pChannelOpenData->OpenHandle = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1011 pChannelOpenData->channels = channels;
1012 pChannelOpenData->lpUserParam = lpUserParam;
1013 if (!HashTable_Insert(g_ChannelHandles, (
void*)(UINT_PTR)pChannelOpenData->OpenHandle,
1014 (
void*)pChannelOpenData))
1016 pChannelInitData->pInterface = NULL;
1017 return CHANNEL_RC_INITIALIZATION_ERROR;
1019 pChannelOpenData->flags = 1;
1020 strncpy(pChannelOpenData->name, pChannelDef->name, CHANNEL_NAME_LEN);
1021 pChannelOpenData->options = pChannelDef->options;
1024 WINPR_ASSERT(max >= CHANNEL_MAX_COUNT);
1025 if (settings->ChannelCount < max)
1027 CHANNEL_DEF* channel = freerdp_settings_get_pointer_array_writable(
1028 settings, FreeRDP_ChannelDefArray, settings->ChannelCount);
1031 strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
1032 channel->options = pChannelDef->options;
1033 settings->ChannelCount++;
1036 channels->openDataCount++;
1039 return CHANNEL_RC_OK;
1042 static UINT VCAPITYPE FreeRDP_VirtualChannelInit(LPVOID* ppInitHandle,
PCHANNEL_DEF pChannel,
1043 INT channelCount, ULONG versionRequested,
1044 PCHANNEL_INIT_EVENT_FN pChannelInitEventProc)
1047 rdpSettings* settings = NULL;
1052 rdpChannels* channels = NULL;
1056 if (!g_Instance || !g_Instance->context)
1057 return CHANNEL_RC_NOT_INITIALIZED;
1059 channels = g_Instance->context->channels;
1061 if (!ppInitHandle || !channels)
1062 return CHANNEL_RC_BAD_INIT_HANDLE;
1065 return CHANNEL_RC_BAD_CHANNEL;
1067 if ((channelCount <= 0) || !pChannelInitEventProc)
1068 return CHANNEL_RC_INITIALIZATION_ERROR;
1070 pChannelInitData = &(channels->initDataList[channels->initDataCount]);
1071 *ppInitHandle = pChannelInitData;
1072 channels->initDataCount++;
1073 pChannelInitData->channels = channels;
1074 pChannelInitData->pInterface = NULL;
1076 if (!channels->can_call_init)
1077 return CHANNEL_RC_NOT_IN_VIRTUALCHANNELENTRY;
1079 if (channels->openDataCount + channelCount > CHANNEL_MAX_COUNT)
1080 return CHANNEL_RC_TOO_MANY_CHANNELS;
1082 if (channels->connected)
1083 return CHANNEL_RC_ALREADY_CONNECTED;
1085 if (versionRequested != VIRTUAL_CHANNEL_VERSION_WIN2000)
1089 for (
int index = 0; index < channelCount; index++)
1091 pChannelDef = &pChannel[index];
1093 if (freerdp_channels_find_channel_open_data_by_name(channels, pChannelDef->name) != 0)
1095 return CHANNEL_RC_BAD_CHANNEL;
1099 pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1100 pChannelClientData->pChannelInitEventProc = pChannelInitEventProc;
1101 pChannelClientData->pInitHandle = *ppInitHandle;
1102 channels->clientDataCount++;
1103 settings = channels->instance->context->settings;
1105 for (
int index = 0; index < channelCount; index++)
1109 pChannelDef = &pChannel[index];
1111 pChannelOpenData = &channels->openDataList[channels->openDataCount];
1113 const LONG hdl = InterlockedIncrement(&g_OpenHandleSeq);
1114 pChannelOpenData->OpenHandle = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1115 pChannelOpenData->channels = channels;
1116 if (!HashTable_Insert(g_ChannelHandles, (
void*)(UINT_PTR)pChannelOpenData->OpenHandle,
1117 (
void*)pChannelOpenData))
1118 return CHANNEL_RC_INITIALIZATION_ERROR;
1119 pChannelOpenData->flags = 1;
1120 strncpy(pChannelOpenData->name, pChannelDef->name, CHANNEL_NAME_LEN);
1121 pChannelOpenData->options = pChannelDef->options;
1123 if (ChannelCount < CHANNEL_MAX_COUNT)
1125 channel = freerdp_settings_get_pointer_array_writable(settings, FreeRDP_ChannelDefArray,
1127 strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
1128 channel->options = pChannelDef->options;
1130 return ERROR_INTERNAL_ERROR;
1133 channels->openDataCount++;
1136 return CHANNEL_RC_OK;
1139 static UINT VCAPITYPE
1140 FreeRDP_VirtualChannelOpenEx(LPVOID pInitHandle, LPDWORD pOpenHandle, PCHAR pChannelName,
1141 PCHANNEL_OPEN_EVENT_EX_FN pChannelOpenEventProcEx)
1143 void* pInterface = NULL;
1144 rdpChannels* channels = NULL;
1148 channels = pChannelInitData->channels;
1149 pInterface = pChannelInitData->pInterface;
1152 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1154 if (!pChannelOpenEventProcEx)
1155 return CHANNEL_RC_BAD_PROC;
1157 if (!channels->connected)
1158 return CHANNEL_RC_NOT_CONNECTED;
1160 pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, pChannelName);
1162 if (!pChannelOpenData)
1163 return CHANNEL_RC_UNKNOWN_CHANNEL_NAME;
1165 if (pChannelOpenData->flags == 2)
1166 return CHANNEL_RC_ALREADY_OPEN;
1168 pChannelOpenData->flags = 2;
1169 pChannelOpenData->pInterface = pInterface;
1170 pChannelOpenData->pChannelOpenEventProcEx = pChannelOpenEventProcEx;
1171 *pOpenHandle = pChannelOpenData->OpenHandle;
1172 return CHANNEL_RC_OK;
1175 static UINT VCAPITYPE FreeRDP_VirtualChannelOpen(LPVOID pInitHandle, LPDWORD pOpenHandle,
1177 PCHANNEL_OPEN_EVENT_FN pChannelOpenEventProc)
1179 void* pInterface = NULL;
1180 rdpChannels* channels = NULL;
1184 channels = pChannelInitData->channels;
1185 pInterface = pChannelInitData->pInterface;
1188 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1190 if (!pChannelOpenEventProc)
1191 return CHANNEL_RC_BAD_PROC;
1193 if (!channels->connected)
1194 return CHANNEL_RC_NOT_CONNECTED;
1196 pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, pChannelName);
1198 if (!pChannelOpenData)
1199 return CHANNEL_RC_UNKNOWN_CHANNEL_NAME;
1201 if (pChannelOpenData->flags == 2)
1202 return CHANNEL_RC_ALREADY_OPEN;
1204 pChannelOpenData->flags = 2;
1205 pChannelOpenData->pInterface = pInterface;
1206 pChannelOpenData->pChannelOpenEventProc = pChannelOpenEventProc;
1207 *pOpenHandle = pChannelOpenData->OpenHandle;
1208 return CHANNEL_RC_OK;
1211 static UINT VCAPITYPE FreeRDP_VirtualChannelCloseEx(LPVOID pInitHandle, DWORD openHandle)
1216 return CHANNEL_RC_BAD_INIT_HANDLE;
1218 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1220 if (!pChannelOpenData)
1221 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1223 if (pChannelOpenData->flags != 2)
1224 return CHANNEL_RC_NOT_OPEN;
1226 pChannelOpenData->flags = 0;
1227 return CHANNEL_RC_OK;
1230 static UINT VCAPITYPE FreeRDP_VirtualChannelClose(DWORD openHandle)
1234 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1236 if (!pChannelOpenData)
1237 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1239 if (pChannelOpenData->flags != 2)
1240 return CHANNEL_RC_NOT_OPEN;
1242 pChannelOpenData->flags = 0;
1243 return CHANNEL_RC_OK;
1246 static UINT VCAPITYPE FreeRDP_VirtualChannelWriteEx(LPVOID pInitHandle, DWORD openHandle,
1247 LPVOID pData, ULONG dataLength,
1250 rdpChannels* channels = NULL;
1254 wMessage message = { 0 };
1257 return CHANNEL_RC_BAD_INIT_HANDLE;
1260 channels = pChannelInitData->channels;
1263 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1265 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1267 if (!pChannelOpenData)
1268 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1270 if (!channels->connected)
1271 return CHANNEL_RC_NOT_CONNECTED;
1274 return CHANNEL_RC_NULL_DATA;
1277 return CHANNEL_RC_ZERO_LENGTH;
1279 if (pChannelOpenData->flags != 2)
1280 return CHANNEL_RC_NOT_OPEN;
1284 if (!pChannelOpenEvent)
1285 return CHANNEL_RC_NO_MEMORY;
1287 pChannelOpenEvent->Data = pData;
1288 pChannelOpenEvent->DataLength = dataLength;
1289 pChannelOpenEvent->UserData = pUserData;
1290 pChannelOpenEvent->pChannelOpenData = pChannelOpenData;
1291 message.context = channels;
1293 message.wParam = pChannelOpenEvent;
1294 message.lParam = NULL;
1295 message.Free = channel_queue_message_free;
1297 if (!MessageQueue_Dispatch(channels->queue, &message))
1299 free(pChannelOpenEvent);
1300 return CHANNEL_RC_NO_MEMORY;
1303 return CHANNEL_RC_OK;
1306 static UINT VCAPITYPE FreeRDP_VirtualChannelWrite(DWORD openHandle, LPVOID pData, ULONG dataLength,
1309 wMessage message = { 0 };
1312 rdpChannels* channels = NULL;
1314 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1316 if (!pChannelOpenData)
1317 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1319 channels = pChannelOpenData->channels;
1321 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1323 if (!channels->connected)
1324 return CHANNEL_RC_NOT_CONNECTED;
1327 return CHANNEL_RC_NULL_DATA;
1330 return CHANNEL_RC_ZERO_LENGTH;
1332 if (pChannelOpenData->flags != 2)
1333 return CHANNEL_RC_NOT_OPEN;
1337 if (!pChannelOpenEvent)
1338 return CHANNEL_RC_NO_MEMORY;
1340 pChannelOpenEvent->Data = pData;
1341 pChannelOpenEvent->DataLength = dataLength;
1342 pChannelOpenEvent->UserData = pUserData;
1343 pChannelOpenEvent->pChannelOpenData = pChannelOpenData;
1344 message.context = channels;
1346 message.wParam = pChannelOpenEvent;
1347 message.lParam = NULL;
1348 message.Free = channel_queue_message_free;
1350 if (!MessageQueue_Dispatch(channels->queue, &message))
1352 free(pChannelOpenEvent);
1353 return CHANNEL_RC_NO_MEMORY;
1356 return CHANNEL_RC_OK;
1359 static BOOL freerdp_channels_is_loaded(rdpChannels* channels, PVIRTUALCHANNELENTRY entry)
1361 for (
int i = 0; i < channels->clientDataCount; i++)
1365 if (pChannelClientData->entry == entry)
1372 static BOOL freerdp_channels_is_loaded_ex(rdpChannels* channels, PVIRTUALCHANNELENTRYEX entryEx)
1374 for (
int i = 0; i < channels->clientDataCount; i++)
1378 if (pChannelClientData->entryEx == entryEx)
1385 int freerdp_channels_client_load(rdpChannels* channels, rdpSettings* settings,
1386 PVIRTUALCHANNELENTRY entry,
void* data)
1392 WINPR_ASSERT(channels);
1393 WINPR_ASSERT(channels->instance);
1394 WINPR_ASSERT(channels->instance->context);
1395 WINPR_ASSERT(entry);
1397 if (channels->clientDataCount + 1 > CHANNEL_MAX_COUNT)
1399 WLog_ERR(TAG,
"error: too many channels");
1403 if (freerdp_channels_is_loaded(channels, entry))
1405 WLog_WARN(TAG,
"Skipping, channel already loaded");
1409 pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1410 pChannelClientData->entry = entry;
1412 EntryPoints.cbSize =
sizeof(EntryPoints);
1413 EntryPoints.protocolVersion = VIRTUAL_CHANNEL_VERSION_WIN2000;
1414 EntryPoints.pVirtualChannelInit = FreeRDP_VirtualChannelInit;
1415 EntryPoints.pVirtualChannelOpen = FreeRDP_VirtualChannelOpen;
1416 EntryPoints.pVirtualChannelClose = FreeRDP_VirtualChannelClose;
1417 EntryPoints.pVirtualChannelWrite = FreeRDP_VirtualChannelWrite;
1418 EntryPoints.MagicNumber = FREERDP_CHANNEL_MAGIC_NUMBER;
1419 EntryPoints.pExtendedData = data;
1420 EntryPoints.context = channels->instance->context;
1422 channels->can_call_init = TRUE;
1423 EnterCriticalSection(&channels->channelsLock);
1424 status = pChannelClientData->entry((PCHANNEL_ENTRY_POINTS)&EntryPoints);
1425 LeaveCriticalSection(&channels->channelsLock);
1427 channels->can_call_init = FALSE;
1431 WLog_ERR(TAG,
"error: channel export function call failed");
1438 int freerdp_channels_client_load_ex(rdpChannels* channels, rdpSettings* settings,
1439 PVIRTUALCHANNELENTRYEX entryEx,
void* data)
1442 void* pInitHandle = NULL;
1447 WINPR_ASSERT(channels);
1448 WINPR_ASSERT(channels->instance);
1449 WINPR_ASSERT(channels->instance->context);
1450 WINPR_ASSERT(entryEx);
1452 if (channels->clientDataCount + 1 > CHANNEL_MAX_COUNT)
1454 WLog_ERR(TAG,
"error: too many channels");
1458 if (freerdp_channels_is_loaded_ex(channels, entryEx))
1460 WLog_WARN(TAG,
"Skipping, channel already loaded");
1464 pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1465 pChannelClientData->entryEx = entryEx;
1466 pChannelInitData = &(channels->initDataList[channels->initDataCount++]);
1467 pInitHandle = pChannelInitData;
1468 pChannelInitData->channels = channels;
1469 EntryPointsEx.cbSize =
sizeof(EntryPointsEx);
1470 EntryPointsEx.protocolVersion = VIRTUAL_CHANNEL_VERSION_WIN2000;
1471 EntryPointsEx.pVirtualChannelInitEx = FreeRDP_VirtualChannelInitEx;
1472 EntryPointsEx.pVirtualChannelOpenEx = FreeRDP_VirtualChannelOpenEx;
1473 EntryPointsEx.pVirtualChannelCloseEx = FreeRDP_VirtualChannelCloseEx;
1474 EntryPointsEx.pVirtualChannelWriteEx = FreeRDP_VirtualChannelWriteEx;
1475 EntryPointsEx.MagicNumber = FREERDP_CHANNEL_MAGIC_NUMBER;
1476 EntryPointsEx.pExtendedData = data;
1477 EntryPointsEx.context = channels->instance->context;
1479 channels->can_call_init = TRUE;
1480 EnterCriticalSection(&channels->channelsLock);
1481 status = pChannelClientData->entryEx((PCHANNEL_ENTRY_POINTS_EX)&EntryPointsEx, pInitHandle);
1482 LeaveCriticalSection(&channels->channelsLock);
1484 channels->can_call_init = FALSE;
1488 WLog_ERR(TAG,
"error: channel export function call failed");
1499 int freerdp_channels_load_plugin(rdpChannels* channels, rdpSettings* settings,
const char* name,
1502 PVIRTUALCHANNELENTRY entry =
1503 freerdp_load_channel_addin_entry(name, NULL, NULL, FREERDP_ADDIN_CHANNEL_STATIC);
1508 return freerdp_channels_client_load(channels, settings, entry, data);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
This struct contains function pointer to initialize/free objects.