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 rdpMcsChannel* mcsChannel = NULL;
585 if (!instance || !channel_name)
588 mcsChannel = 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 pChannelOpenData->OpenHandle = InterlockedIncrement(&g_OpenHandleSeq);
1010 pChannelOpenData->channels = channels;
1011 pChannelOpenData->lpUserParam = lpUserParam;
1012 if (!HashTable_Insert(g_ChannelHandles, (
void*)(UINT_PTR)pChannelOpenData->OpenHandle,
1013 (
void*)pChannelOpenData))
1015 pChannelInitData->pInterface = NULL;
1016 return CHANNEL_RC_INITIALIZATION_ERROR;
1018 pChannelOpenData->flags = 1;
1019 strncpy(pChannelOpenData->name, pChannelDef->name, CHANNEL_NAME_LEN);
1020 pChannelOpenData->options = pChannelDef->options;
1023 WINPR_ASSERT(max >= CHANNEL_MAX_COUNT);
1024 if (settings->ChannelCount < max)
1026 CHANNEL_DEF* channel = freerdp_settings_get_pointer_array_writable(
1027 settings, FreeRDP_ChannelDefArray, settings->ChannelCount);
1030 strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
1031 channel->options = pChannelDef->options;
1032 settings->ChannelCount++;
1035 channels->openDataCount++;
1038 return CHANNEL_RC_OK;
1041 static UINT VCAPITYPE FreeRDP_VirtualChannelInit(LPVOID* ppInitHandle,
PCHANNEL_DEF pChannel,
1042 INT channelCount, ULONG versionRequested,
1043 PCHANNEL_INIT_EVENT_FN pChannelInitEventProc)
1046 rdpSettings* settings = NULL;
1051 rdpChannels* channels = NULL;
1055 if (!g_Instance || !g_Instance->context)
1056 return CHANNEL_RC_NOT_INITIALIZED;
1058 channels = g_Instance->context->channels;
1060 if (!ppInitHandle || !channels)
1061 return CHANNEL_RC_BAD_INIT_HANDLE;
1064 return CHANNEL_RC_BAD_CHANNEL;
1066 if ((channelCount <= 0) || !pChannelInitEventProc)
1067 return CHANNEL_RC_INITIALIZATION_ERROR;
1069 pChannelInitData = &(channels->initDataList[channels->initDataCount]);
1070 *ppInitHandle = pChannelInitData;
1071 channels->initDataCount++;
1072 pChannelInitData->channels = channels;
1073 pChannelInitData->pInterface = NULL;
1075 if (!channels->can_call_init)
1076 return CHANNEL_RC_NOT_IN_VIRTUALCHANNELENTRY;
1078 if (channels->openDataCount + channelCount > CHANNEL_MAX_COUNT)
1079 return CHANNEL_RC_TOO_MANY_CHANNELS;
1081 if (channels->connected)
1082 return CHANNEL_RC_ALREADY_CONNECTED;
1084 if (versionRequested != VIRTUAL_CHANNEL_VERSION_WIN2000)
1088 for (
int index = 0; index < channelCount; index++)
1090 pChannelDef = &pChannel[index];
1092 if (freerdp_channels_find_channel_open_data_by_name(channels, pChannelDef->name) != 0)
1094 return CHANNEL_RC_BAD_CHANNEL;
1098 pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1099 pChannelClientData->pChannelInitEventProc = pChannelInitEventProc;
1100 pChannelClientData->pInitHandle = *ppInitHandle;
1101 channels->clientDataCount++;
1102 settings = channels->instance->context->settings;
1104 for (
int index = 0; index < channelCount; index++)
1108 pChannelDef = &pChannel[index];
1109 pChannelOpenData = &channels->openDataList[channels->openDataCount];
1110 pChannelOpenData->OpenHandle = InterlockedIncrement(&g_OpenHandleSeq);
1111 pChannelOpenData->channels = channels;
1112 if (!HashTable_Insert(g_ChannelHandles, (
void*)(UINT_PTR)pChannelOpenData->OpenHandle,
1113 (
void*)pChannelOpenData))
1114 return CHANNEL_RC_INITIALIZATION_ERROR;
1115 pChannelOpenData->flags = 1;
1116 strncpy(pChannelOpenData->name, pChannelDef->name, CHANNEL_NAME_LEN);
1117 pChannelOpenData->options = pChannelDef->options;
1119 if (ChannelCount < CHANNEL_MAX_COUNT)
1121 channel = freerdp_settings_get_pointer_array_writable(settings, FreeRDP_ChannelDefArray,
1123 strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
1124 channel->options = pChannelDef->options;
1126 return ERROR_INTERNAL_ERROR;
1129 channels->openDataCount++;
1132 return CHANNEL_RC_OK;
1135 static UINT VCAPITYPE
1136 FreeRDP_VirtualChannelOpenEx(LPVOID pInitHandle, LPDWORD pOpenHandle, PCHAR pChannelName,
1137 PCHANNEL_OPEN_EVENT_EX_FN pChannelOpenEventProcEx)
1139 void* pInterface = NULL;
1140 rdpChannels* channels = NULL;
1144 channels = pChannelInitData->channels;
1145 pInterface = pChannelInitData->pInterface;
1148 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1150 if (!pChannelOpenEventProcEx)
1151 return CHANNEL_RC_BAD_PROC;
1153 if (!channels->connected)
1154 return CHANNEL_RC_NOT_CONNECTED;
1156 pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, pChannelName);
1158 if (!pChannelOpenData)
1159 return CHANNEL_RC_UNKNOWN_CHANNEL_NAME;
1161 if (pChannelOpenData->flags == 2)
1162 return CHANNEL_RC_ALREADY_OPEN;
1164 pChannelOpenData->flags = 2;
1165 pChannelOpenData->pInterface = pInterface;
1166 pChannelOpenData->pChannelOpenEventProcEx = pChannelOpenEventProcEx;
1167 *pOpenHandle = pChannelOpenData->OpenHandle;
1168 return CHANNEL_RC_OK;
1171 static UINT VCAPITYPE FreeRDP_VirtualChannelOpen(LPVOID pInitHandle, LPDWORD pOpenHandle,
1173 PCHANNEL_OPEN_EVENT_FN pChannelOpenEventProc)
1175 void* pInterface = NULL;
1176 rdpChannels* channels = NULL;
1180 channels = pChannelInitData->channels;
1181 pInterface = pChannelInitData->pInterface;
1184 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1186 if (!pChannelOpenEventProc)
1187 return CHANNEL_RC_BAD_PROC;
1189 if (!channels->connected)
1190 return CHANNEL_RC_NOT_CONNECTED;
1192 pChannelOpenData = freerdp_channels_find_channel_open_data_by_name(channels, pChannelName);
1194 if (!pChannelOpenData)
1195 return CHANNEL_RC_UNKNOWN_CHANNEL_NAME;
1197 if (pChannelOpenData->flags == 2)
1198 return CHANNEL_RC_ALREADY_OPEN;
1200 pChannelOpenData->flags = 2;
1201 pChannelOpenData->pInterface = pInterface;
1202 pChannelOpenData->pChannelOpenEventProc = pChannelOpenEventProc;
1203 *pOpenHandle = pChannelOpenData->OpenHandle;
1204 return CHANNEL_RC_OK;
1207 static UINT VCAPITYPE FreeRDP_VirtualChannelCloseEx(LPVOID pInitHandle, DWORD openHandle)
1212 return CHANNEL_RC_BAD_INIT_HANDLE;
1214 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1216 if (!pChannelOpenData)
1217 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1219 if (pChannelOpenData->flags != 2)
1220 return CHANNEL_RC_NOT_OPEN;
1222 pChannelOpenData->flags = 0;
1223 return CHANNEL_RC_OK;
1226 static UINT VCAPITYPE FreeRDP_VirtualChannelClose(DWORD openHandle)
1230 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1232 if (!pChannelOpenData)
1233 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1235 if (pChannelOpenData->flags != 2)
1236 return CHANNEL_RC_NOT_OPEN;
1238 pChannelOpenData->flags = 0;
1239 return CHANNEL_RC_OK;
1242 static UINT VCAPITYPE FreeRDP_VirtualChannelWriteEx(LPVOID pInitHandle, DWORD openHandle,
1243 LPVOID pData, ULONG dataLength,
1246 rdpChannels* channels = NULL;
1250 wMessage message = { 0 };
1253 return CHANNEL_RC_BAD_INIT_HANDLE;
1256 channels = pChannelInitData->channels;
1259 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1261 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1263 if (!pChannelOpenData)
1264 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1266 if (!channels->connected)
1267 return CHANNEL_RC_NOT_CONNECTED;
1270 return CHANNEL_RC_NULL_DATA;
1273 return CHANNEL_RC_ZERO_LENGTH;
1275 if (pChannelOpenData->flags != 2)
1276 return CHANNEL_RC_NOT_OPEN;
1280 if (!pChannelOpenEvent)
1281 return CHANNEL_RC_NO_MEMORY;
1283 pChannelOpenEvent->Data = pData;
1284 pChannelOpenEvent->DataLength = dataLength;
1285 pChannelOpenEvent->UserData = pUserData;
1286 pChannelOpenEvent->pChannelOpenData = pChannelOpenData;
1287 message.context = channels;
1289 message.wParam = pChannelOpenEvent;
1290 message.lParam = NULL;
1291 message.Free = channel_queue_message_free;
1293 if (!MessageQueue_Dispatch(channels->queue, &message))
1295 free(pChannelOpenEvent);
1296 return CHANNEL_RC_NO_MEMORY;
1299 return CHANNEL_RC_OK;
1302 static UINT VCAPITYPE FreeRDP_VirtualChannelWrite(DWORD openHandle, LPVOID pData, ULONG dataLength,
1305 wMessage message = { 0 };
1308 rdpChannels* channels = NULL;
1310 pChannelOpenData = HashTable_GetItemValue(g_ChannelHandles, (
void*)(UINT_PTR)openHandle);
1312 if (!pChannelOpenData)
1313 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1315 channels = pChannelOpenData->channels;
1317 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1319 if (!channels->connected)
1320 return CHANNEL_RC_NOT_CONNECTED;
1323 return CHANNEL_RC_NULL_DATA;
1326 return CHANNEL_RC_ZERO_LENGTH;
1328 if (pChannelOpenData->flags != 2)
1329 return CHANNEL_RC_NOT_OPEN;
1333 if (!pChannelOpenEvent)
1334 return CHANNEL_RC_NO_MEMORY;
1336 pChannelOpenEvent->Data = pData;
1337 pChannelOpenEvent->DataLength = dataLength;
1338 pChannelOpenEvent->UserData = pUserData;
1339 pChannelOpenEvent->pChannelOpenData = pChannelOpenData;
1340 message.context = channels;
1342 message.wParam = pChannelOpenEvent;
1343 message.lParam = NULL;
1344 message.Free = channel_queue_message_free;
1346 if (!MessageQueue_Dispatch(channels->queue, &message))
1348 free(pChannelOpenEvent);
1349 return CHANNEL_RC_NO_MEMORY;
1352 return CHANNEL_RC_OK;
1355 static BOOL freerdp_channels_is_loaded(rdpChannels* channels, PVIRTUALCHANNELENTRY entry)
1357 for (
int i = 0; i < channels->clientDataCount; i++)
1361 if (pChannelClientData->entry == entry)
1368 static BOOL freerdp_channels_is_loaded_ex(rdpChannels* channels, PVIRTUALCHANNELENTRYEX entryEx)
1370 for (
int i = 0; i < channels->clientDataCount; i++)
1374 if (pChannelClientData->entryEx == entryEx)
1381 int freerdp_channels_client_load(rdpChannels* channels, rdpSettings* settings,
1382 PVIRTUALCHANNELENTRY entry,
void* data)
1388 WINPR_ASSERT(channels);
1389 WINPR_ASSERT(channels->instance);
1390 WINPR_ASSERT(channels->instance->context);
1391 WINPR_ASSERT(entry);
1393 if (channels->clientDataCount + 1 > CHANNEL_MAX_COUNT)
1395 WLog_ERR(TAG,
"error: too many channels");
1399 if (freerdp_channels_is_loaded(channels, entry))
1401 WLog_WARN(TAG,
"Skipping, channel already loaded");
1405 pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1406 pChannelClientData->entry = entry;
1408 EntryPoints.cbSize =
sizeof(EntryPoints);
1409 EntryPoints.protocolVersion = VIRTUAL_CHANNEL_VERSION_WIN2000;
1410 EntryPoints.pVirtualChannelInit = FreeRDP_VirtualChannelInit;
1411 EntryPoints.pVirtualChannelOpen = FreeRDP_VirtualChannelOpen;
1412 EntryPoints.pVirtualChannelClose = FreeRDP_VirtualChannelClose;
1413 EntryPoints.pVirtualChannelWrite = FreeRDP_VirtualChannelWrite;
1414 EntryPoints.MagicNumber = FREERDP_CHANNEL_MAGIC_NUMBER;
1415 EntryPoints.pExtendedData = data;
1416 EntryPoints.context = channels->instance->context;
1418 channels->can_call_init = TRUE;
1419 EnterCriticalSection(&channels->channelsLock);
1420 status = pChannelClientData->entry((PCHANNEL_ENTRY_POINTS)&EntryPoints);
1421 LeaveCriticalSection(&channels->channelsLock);
1423 channels->can_call_init = FALSE;
1427 WLog_ERR(TAG,
"error: channel export function call failed");
1434 int freerdp_channels_client_load_ex(rdpChannels* channels, rdpSettings* settings,
1435 PVIRTUALCHANNELENTRYEX entryEx,
void* data)
1438 void* pInitHandle = NULL;
1443 WINPR_ASSERT(channels);
1444 WINPR_ASSERT(channels->instance);
1445 WINPR_ASSERT(channels->instance->context);
1446 WINPR_ASSERT(entryEx);
1448 if (channels->clientDataCount + 1 > CHANNEL_MAX_COUNT)
1450 WLog_ERR(TAG,
"error: too many channels");
1454 if (freerdp_channels_is_loaded_ex(channels, entryEx))
1456 WLog_WARN(TAG,
"Skipping, channel already loaded");
1460 pChannelClientData = &channels->clientDataList[channels->clientDataCount];
1461 pChannelClientData->entryEx = entryEx;
1462 pChannelInitData = &(channels->initDataList[channels->initDataCount++]);
1463 pInitHandle = pChannelInitData;
1464 pChannelInitData->channels = channels;
1465 EntryPointsEx.cbSize =
sizeof(EntryPointsEx);
1466 EntryPointsEx.protocolVersion = VIRTUAL_CHANNEL_VERSION_WIN2000;
1467 EntryPointsEx.pVirtualChannelInitEx = FreeRDP_VirtualChannelInitEx;
1468 EntryPointsEx.pVirtualChannelOpenEx = FreeRDP_VirtualChannelOpenEx;
1469 EntryPointsEx.pVirtualChannelCloseEx = FreeRDP_VirtualChannelCloseEx;
1470 EntryPointsEx.pVirtualChannelWriteEx = FreeRDP_VirtualChannelWriteEx;
1471 EntryPointsEx.MagicNumber = FREERDP_CHANNEL_MAGIC_NUMBER;
1472 EntryPointsEx.pExtendedData = data;
1473 EntryPointsEx.context = channels->instance->context;
1475 channels->can_call_init = TRUE;
1476 EnterCriticalSection(&channels->channelsLock);
1477 status = pChannelClientData->entryEx((PCHANNEL_ENTRY_POINTS_EX)&EntryPointsEx, pInitHandle);
1478 LeaveCriticalSection(&channels->channelsLock);
1480 channels->can_call_init = FALSE;
1484 WLog_ERR(TAG,
"error: channel export function call failed");
1495 int freerdp_channels_load_plugin(rdpChannels* channels, rdpSettings* settings,
const char* name,
1498 PVIRTUALCHANNELENTRY entry =
1499 freerdp_load_channel_addin_entry(name, NULL, NULL, FREERDP_ADDIN_CHANNEL_STATIC);
1504 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.