22 #include <freerdp/config.h>
24 #include <winpr/crt.h>
25 #include <winpr/stream.h>
26 #include <winpr/interlocked.h>
28 #include <freerdp/freerdp.h>
29 #include <freerdp/channels/drdynvc.h>
30 #include <freerdp/utils/drdynvc.h>
32 #include "drdynvc_main.h"
34 #define TAG CHANNELS_TAG("drdynvc.client")
37 static UINT dvcman_channel_close(
DVCMAN_CHANNEL* channel, BOOL perRequest, BOOL fromHashTableFn);
38 static void dvcman_free(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr);
39 static UINT drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId,
const BYTE* data,
40 UINT32 dataSize, BOOL* close);
41 static UINT drdynvc_send(drdynvcPlugin* drdynvc,
wStream* s);
46 free(listener->channel_name);
55 static UINT dvcman_get_configuration(IWTSListener* pListener,
void** ppPropertyBag)
57 WINPR_ASSERT(ppPropertyBag);
58 WINPR_UNUSED(pListener);
59 *ppPropertyBag = NULL;
60 return ERROR_INTERNAL_ERROR;
68 static UINT dvcman_create_listener(IWTSVirtualChannelManager* pChannelMgr,
69 const char* pszChannelName, ULONG ulFlags,
70 IWTSListenerCallback* pListenerCallback,
71 IWTSListener** ppListener)
77 WLog_DBG(TAG,
"create_listener: %" PRIuz
".%s.", HashTable_Count(dvcman->listeners) + 1,
83 WLog_ERR(TAG,
"calloc failed!");
84 return CHANNEL_RC_NO_MEMORY;
87 listener->iface.GetConfiguration = dvcman_get_configuration;
88 listener->iface.pInterface = NULL;
89 listener->dvcman = dvcman;
90 listener->channel_name = _strdup(pszChannelName);
92 if (!listener->channel_name)
94 WLog_ERR(TAG,
"_strdup failed!");
95 dvcman_wtslistener_free(listener);
96 return CHANNEL_RC_NO_MEMORY;
99 listener->flags = ulFlags;
100 listener->listener_callback = pListenerCallback;
103 *ppListener = (IWTSListener*)listener;
105 if (!HashTable_Insert(dvcman->listeners, listener->channel_name, listener))
107 dvcman_wtslistener_free(listener);
108 return ERROR_INTERNAL_ERROR;
112 return CHANNEL_RC_OK;
115 static UINT dvcman_destroy_listener(IWTSVirtualChannelManager* pChannelMgr, IWTSListener* pListener)
119 WINPR_UNUSED(pChannelMgr);
123 DVCMAN* dvcman = listener->dvcman;
125 HashTable_Remove(dvcman->listeners, listener->channel_name);
128 return CHANNEL_RC_OK;
136 static UINT dvcman_register_plugin(IDRDYNVC_ENTRY_POINTS* pEntryPoints,
const char* name,
139 WINPR_ASSERT(pEntryPoints);
142 WINPR_ASSERT(dvcman);
143 if (!ArrayList_Append(dvcman->plugin_names, name))
144 return ERROR_INTERNAL_ERROR;
145 if (!ArrayList_Append(dvcman->plugins, pPlugin))
146 return ERROR_INTERNAL_ERROR;
148 WLog_DBG(TAG,
"register_plugin: num_plugins %" PRIuz, ArrayList_Count(dvcman->plugins));
149 return CHANNEL_RC_OK;
152 static IWTSPlugin* dvcman_get_plugin(IDRDYNVC_ENTRY_POINTS* pEntryPoints,
const char* name)
154 IWTSPlugin* plugin = NULL;
157 WINPR_ASSERT(pEntryPoints);
159 if (!dvcman || !pEntryPoints || !name)
162 nc = ArrayList_Count(dvcman->plugin_names);
163 pc = ArrayList_Count(dvcman->plugins);
167 ArrayList_Lock(dvcman->plugin_names);
168 ArrayList_Lock(dvcman->plugins);
169 for (
size_t i = 0; i < pc; i++)
171 const char* cur = ArrayList_GetItem(dvcman->plugin_names, i);
172 if (strcmp(cur, name) == 0)
174 plugin = ArrayList_GetItem(dvcman->plugins, i);
178 ArrayList_Unlock(dvcman->plugin_names);
179 ArrayList_Unlock(dvcman->plugins);
183 static const ADDIN_ARGV* dvcman_get_plugin_data(IDRDYNVC_ENTRY_POINTS* pEntryPoints)
185 WINPR_ASSERT(pEntryPoints);
189 static rdpContext* dvcman_get_rdp_context(IDRDYNVC_ENTRY_POINTS* pEntryPoints)
193 return entry->context;
196 static rdpSettings* dvcman_get_rdp_settings(IDRDYNVC_ENTRY_POINTS* pEntryPoints)
198 rdpContext* context = dvcman_get_rdp_context(pEntryPoints);
199 WINPR_ASSERT(context);
201 return context->settings;
204 static UINT32 dvcman_get_channel_id(IWTSVirtualChannel* channel)
208 return dvc->channel_id;
211 static const char* dvcman_get_channel_name(IWTSVirtualChannel* channel)
215 return dvc->channel_name;
218 static DVCMAN_CHANNEL* dvcman_get_channel_by_id(IWTSVirtualChannelManager* pChannelMgr,
219 UINT32 ChannelId, BOOL doRef)
224 WINPR_ASSERT(dvcman);
225 HashTable_Lock(dvcman->channelsById);
226 dvcChannel = HashTable_GetItemValue(dvcman->channelsById, &ChannelId);
230 InterlockedIncrement(&dvcChannel->refCounter);
233 HashTable_Unlock(dvcman->channelsById);
237 static IWTSVirtualChannel* dvcman_find_channel_by_id(IWTSVirtualChannelManager* pChannelMgr,
240 DVCMAN_CHANNEL* channel = dvcman_get_channel_by_id(pChannelMgr, ChannelId, FALSE);
244 return &channel->iface;
247 static void dvcman_plugin_terminate(
void* plugin)
249 IWTSPlugin* pPlugin = plugin;
251 WINPR_ASSERT(pPlugin);
252 UINT error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Terminated, pPlugin);
253 if (error != CHANNEL_RC_OK)
254 WLog_ERR(TAG,
"Terminated failed with error %" PRIu32
"!", error);
257 static void wts_listener_free(
void* arg)
260 dvcman_wtslistener_free(listener);
263 static BOOL channelIdMatch(
const void* k1,
const void* k2)
267 return *((
const UINT32*)k1) == *((
const UINT32*)k2);
270 static UINT32 channelIdHash(
const void*
id)
273 return *((
const UINT32*)
id);
276 static void channelByIdCleanerFn(
void* value)
281 dvcman_channel_close(channel, FALSE, TRUE);
282 dvcman_channel_free(channel);
286 static IWTSVirtualChannelManager* dvcman_new(drdynvcPlugin* plugin)
294 dvcman->iface.CreateListener = dvcman_create_listener;
295 dvcman->iface.DestroyListener = dvcman_destroy_listener;
296 dvcman->iface.FindChannelById = dvcman_find_channel_by_id;
297 dvcman->iface.GetChannelId = dvcman_get_channel_id;
298 dvcman->iface.GetChannelName = dvcman_get_channel_name;
299 dvcman->drdynvc = plugin;
300 dvcman->channelsById = HashTable_New(TRUE);
302 if (!dvcman->channelsById)
305 HashTable_SetHashFunction(dvcman->channelsById, channelIdHash);
306 obj = HashTable_KeyObject(dvcman->channelsById);
308 obj->fnObjectEquals = channelIdMatch;
310 obj = HashTable_ValueObject(dvcman->channelsById);
312 obj->fnObjectFree = channelByIdCleanerFn;
314 dvcman->pool = StreamPool_New(TRUE, 10);
318 dvcman->listeners = HashTable_New(TRUE);
319 if (!dvcman->listeners)
321 HashTable_SetHashFunction(dvcman->listeners, HashTable_StringHash);
323 obj = HashTable_KeyObject(dvcman->listeners);
324 obj->fnObjectEquals = HashTable_StringCompare;
326 obj = HashTable_ValueObject(dvcman->listeners);
327 obj->fnObjectFree = wts_listener_free;
329 dvcman->plugin_names = ArrayList_New(TRUE);
330 if (!dvcman->plugin_names)
332 obj = ArrayList_Object(dvcman->plugin_names);
333 obj->fnObjectNew = winpr_ObjectStringClone;
334 obj->fnObjectFree = winpr_ObjectStringFree;
336 dvcman->plugins = ArrayList_New(TRUE);
337 if (!dvcman->plugins)
339 obj = ArrayList_Object(dvcman->plugins);
340 obj->fnObjectFree = dvcman_plugin_terminate;
341 return &dvcman->iface;
343 dvcman_free(plugin, &dvcman->iface);
352 static UINT dvcman_load_addin(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr,
355 WINPR_ASSERT(drdynvc);
356 WINPR_ASSERT(pChannelMgr);
358 WINPR_ASSERT(context);
360 WLog_Print(drdynvc->log, WLOG_INFO,
"Loading Dynamic Virtual Channel %s", args->argv[0]);
362 PVIRTUALCHANNELENTRY pvce =
363 freerdp_load_channel_addin_entry(args->argv[0], NULL, NULL, FREERDP_ADDIN_CHANNEL_DYNAMIC);
364 PDVC_PLUGIN_ENTRY pDVCPluginEntry = WINPR_FUNC_PTR_CAST(pvce, PDVC_PLUGIN_ENTRY);
370 entryPoints.iface.RegisterPlugin = dvcman_register_plugin;
371 entryPoints.iface.GetPlugin = dvcman_get_plugin;
372 entryPoints.iface.GetPluginData = dvcman_get_plugin_data;
373 entryPoints.iface.GetRdpSettings = dvcman_get_rdp_settings;
374 entryPoints.iface.GetRdpContext = dvcman_get_rdp_context;
375 entryPoints.dvcman = (
DVCMAN*)pChannelMgr;
376 entryPoints.args = args;
377 entryPoints.context = context;
378 return pDVCPluginEntry(&entryPoints.iface);
381 return ERROR_INVALID_FUNCTION;
389 if (channel->dvc_data)
390 Stream_Release(channel->dvc_data);
392 DeleteCriticalSection(&(channel->lock));
393 free(channel->channel_name);
399 WINPR_ASSERT(channel);
400 if (InterlockedDecrement(&channel->refCounter))
403 DVCMAN* dvcman = channel->dvcman;
405 HashTable_Remove(dvcman->channelsById, &channel->channel_id);
410 WINPR_ASSERT(channel);
411 DVCMAN* dvcman = channel->dvcman;
412 drdynvcPlugin* drdynvc = dvcman->drdynvc;
413 wStream* s = StreamPool_Take(dvcman->pool, 5);
417 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
418 return CHANNEL_RC_NO_MEMORY;
421 Stream_Write_UINT8(s, (CLOSE_REQUEST_PDU << 4) | 0x02);
422 Stream_Write_UINT32(s, channel->channel_id);
423 return drdynvc_send(drdynvc, s);
428 WINPR_ASSERT(channel);
430 IWTSVirtualChannelCallback* cb = channel->channel_callback;
431 const char* name = channel->channel_name;
432 const UINT32
id = channel->channel_id;
435 if (cb->OnOpen || cb->OnClose)
437 if (!cb->OnOpen || !cb->OnClose)
438 WLog_WARN(TAG,
"{%s:%" PRIu32
"} OnOpen=%p, OnClose=%p", name,
id, cb->OnOpen,
445 WINPR_ASSERT(channel);
448 IWTSVirtualChannelCallback* cb = channel->channel_callback;
451 check_open_close_receive(channel);
452 WINPR_ASSERT(cb->OnDataReceived);
453 return cb->OnDataReceived(cb, data);
456 static UINT dvcman_channel_close(
DVCMAN_CHANNEL* channel, BOOL perRequest, BOOL fromHashTableFn)
458 UINT error = CHANNEL_RC_OK;
459 DrdynvcClientContext* context = NULL;
461 WINPR_ASSERT(channel);
462 switch (channel->state)
464 case DVC_CHANNEL_INIT:
466 case DVC_CHANNEL_RUNNING:
469 drdynvcPlugin* drdynvc = channel->dvcman->drdynvc;
470 WINPR_ASSERT(drdynvc);
471 context = drdynvc->context;
473 WLog_Print(drdynvc->log, WLOG_DEBUG,
"sending close confirm for '%s'",
474 channel->channel_name);
476 error = dvcchannel_send_close(channel);
477 if (error != CHANNEL_RC_OK)
479 const char* msg =
"error when sending close confirm for '%s'";
481 msg =
"error when sending closeRequest for '%s'";
483 WLog_Print(drdynvc->log, WLOG_DEBUG, msg, channel->channel_name);
487 channel->state = DVC_CHANNEL_CLOSED;
489 IWTSVirtualChannelCallback* cb = channel->channel_callback;
492 check_open_close_receive(channel);
493 IFCALL(cb->OnClose, cb);
496 channel->channel_callback = NULL;
498 if (channel->dvcman && channel->dvcman->drdynvc)
502 IFCALLRET(context->OnChannelDisconnected, error, context, channel->channel_name,
503 channel->pInterface);
507 if (!fromHashTableFn)
508 dvcman_channel_unref(channel);
510 case DVC_CHANNEL_CLOSED:
520 IWTSVirtualChannelManager* pChannelMgr, UINT32 ChannelId,
521 const char* ChannelName)
525 WINPR_ASSERT(drdynvc);
526 WINPR_ASSERT(pChannelMgr);
532 channel->dvcman = (
DVCMAN*)pChannelMgr;
533 channel->channel_id = ChannelId;
534 channel->refCounter = 1;
535 channel->state = DVC_CHANNEL_INIT;
536 channel->channel_name = _strdup(ChannelName);
538 if (!channel->channel_name)
541 if (!InitializeCriticalSectionEx(&(channel->lock), 0, 0))
546 dvcman_channel_free(channel);
550 static void dvcman_clear(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr)
554 WINPR_ASSERT(dvcman);
555 WINPR_UNUSED(drdynvc);
557 HashTable_Clear(dvcman->channelsById);
558 ArrayList_Clear(dvcman->plugins);
559 ArrayList_Clear(dvcman->plugin_names);
560 HashTable_Clear(dvcman->listeners);
562 static void dvcman_free(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr)
566 WINPR_ASSERT(dvcman);
567 WINPR_UNUSED(drdynvc);
569 HashTable_Free(dvcman->channelsById);
570 ArrayList_Free(dvcman->plugins);
571 ArrayList_Free(dvcman->plugin_names);
572 HashTable_Free(dvcman->listeners);
574 StreamPool_Free(dvcman->pool);
583 static UINT dvcman_init(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr)
586 UINT error = CHANNEL_RC_OK;
588 WINPR_ASSERT(dvcman);
589 ArrayList_Lock(dvcman->plugins);
590 for (
size_t i = 0; i < ArrayList_Count(dvcman->plugins); i++)
592 IWTSPlugin* pPlugin = ArrayList_GetItem(dvcman->plugins, i);
594 error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Initialize, pPlugin, pChannelMgr);
595 if (error != CHANNEL_RC_OK)
597 WLog_Print(drdynvc->log, WLOG_ERROR,
"Initialize failed with error %" PRIu32
"!",
604 ArrayList_Unlock(dvcman->plugins);
613 static UINT dvcman_write_channel(IWTSVirtualChannel* pChannel, ULONG cbSize,
const BYTE* pBuffer,
620 WINPR_UNUSED(pReserved);
621 if (!channel || !channel->dvcman)
622 return CHANNEL_RC_BAD_CHANNEL;
624 EnterCriticalSection(&(channel->lock));
626 drdynvc_write_data(channel->dvcman->drdynvc, channel->channel_id, pBuffer, cbSize, &close);
627 LeaveCriticalSection(&(channel->lock));
630 dvcman_channel_close(channel, FALSE, FALSE);
640 static UINT dvcman_close_channel_iface(IWTSVirtualChannel* pChannel)
645 return CHANNEL_RC_BAD_CHANNEL;
647 WLog_DBG(TAG,
"close_channel_iface: id=%" PRIu32
"", channel->channel_id);
648 return dvcman_channel_close(channel, FALSE, FALSE);
656 static DVCMAN_CHANNEL* dvcman_create_channel(drdynvcPlugin* drdynvc,
657 IWTSVirtualChannelManager* pChannelMgr,
658 UINT32 ChannelId,
const char* ChannelName, UINT* res)
662 DrdynvcClientContext* context = NULL;
665 IWTSVirtualChannelCallback* pCallback = NULL;
667 WINPR_ASSERT(dvcman);
670 HashTable_Lock(dvcman->listeners);
671 listener = (
DVCMAN_LISTENER*)HashTable_GetItemValue(dvcman->listeners, ChannelName);
674 *res = ERROR_NOT_FOUND;
678 channel = dvcman_get_channel_by_id(pChannelMgr, ChannelId, FALSE);
681 switch (channel->state)
683 case DVC_CHANNEL_RUNNING:
684 WLog_Print(drdynvc->log, WLOG_ERROR,
685 "Protocol error: Duplicated ChannelId %" PRIu32
" (%s)!", ChannelId,
687 *res = CHANNEL_RC_ALREADY_OPEN;
690 case DVC_CHANNEL_CLOSED:
691 case DVC_CHANNEL_INIT:
693 WLog_Print(drdynvc->log, WLOG_ERROR,
"not expecting a createChannel from state %d",
695 *res = CHANNEL_RC_INITIALIZATION_ERROR;
701 if (!(channel = dvcman_channel_new(drdynvc, pChannelMgr, ChannelId, ChannelName)))
703 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_channel_new failed!");
704 *res = CHANNEL_RC_NO_MEMORY;
709 if (!HashTable_Insert(dvcman->channelsById, &channel->channel_id, channel))
711 WLog_Print(drdynvc->log, WLOG_ERROR,
"unable to register channel in our channel list");
712 *res = ERROR_INTERNAL_ERROR;
713 dvcman_channel_free(channel);
718 channel->iface.Write = dvcman_write_channel;
719 channel->iface.Close = dvcman_close_channel_iface;
722 *res = listener->listener_callback->OnNewChannelConnection(
723 listener->listener_callback, &channel->iface, NULL, &bAccept, &pCallback);
725 if (*res != CHANNEL_RC_OK)
727 WLog_Print(drdynvc->log, WLOG_ERROR,
728 "OnNewChannelConnection failed with error %" PRIu32
"!", *res);
729 *res = ERROR_INTERNAL_ERROR;
730 dvcman_channel_unref(channel);
736 WLog_Print(drdynvc->log, WLOG_ERROR,
"OnNewChannelConnection returned with bAccept FALSE!");
737 *res = ERROR_INTERNAL_ERROR;
738 dvcman_channel_unref(channel);
743 WLog_Print(drdynvc->log, WLOG_DEBUG,
"listener %s created new channel %" PRIu32
"",
744 listener->channel_name, channel->channel_id);
745 channel->state = DVC_CHANNEL_RUNNING;
746 channel->channel_callback = pCallback;
747 channel->pInterface = listener->iface.pInterface;
748 context = dvcman->drdynvc->context;
750 IFCALLRET(context->OnChannelConnected, *res, context, ChannelName, listener->iface.pInterface);
751 if (*res != CHANNEL_RC_OK)
753 WLog_Print(drdynvc->log, WLOG_ERROR,
754 "context.OnChannelConnected failed with error %" PRIu32
"", *res);
758 HashTable_Unlock(dvcman->listeners);
768 static UINT dvcman_open_channel(drdynvcPlugin* drdynvc,
DVCMAN_CHANNEL* channel)
770 IWTSVirtualChannelCallback* pCallback = NULL;
771 UINT error = CHANNEL_RC_OK;
773 WINPR_ASSERT(drdynvc);
774 WINPR_ASSERT(channel);
775 if (channel->state == DVC_CHANNEL_RUNNING)
777 pCallback = channel->channel_callback;
779 if (pCallback->OnOpen)
781 check_open_close_receive(channel);
782 error = pCallback->OnOpen(pCallback);
785 WLog_Print(drdynvc->log, WLOG_ERROR,
"OnOpen failed with error %" PRIu32
"!",
791 WLog_Print(drdynvc->log, WLOG_DEBUG,
"open_channel: ChannelId %" PRIu32
"",
792 channel->channel_id);
804 static UINT dvcman_receive_channel_data_first(
DVCMAN_CHANNEL* channel, UINT32 length)
806 WINPR_ASSERT(channel);
807 WINPR_ASSERT(channel->dvcman);
808 if (channel->dvc_data)
809 Stream_Release(channel->dvc_data);
811 channel->dvc_data = StreamPool_Take(channel->dvcman->pool, length);
813 if (!channel->dvc_data)
815 drdynvcPlugin* drdynvc = channel->dvcman->drdynvc;
816 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
817 return CHANNEL_RC_NO_MEMORY;
820 channel->dvc_data_length = length;
821 return CHANNEL_RC_OK;
830 UINT32 ThreadingFlags)
832 UINT status = CHANNEL_RC_OK;
833 size_t dataSize = Stream_GetRemainingLength(data);
835 WINPR_ASSERT(channel);
836 WINPR_ASSERT(channel->dvcman);
837 if (channel->dvc_data)
839 drdynvcPlugin* drdynvc = channel->dvcman->drdynvc;
842 if (Stream_GetPosition(channel->dvc_data) + dataSize > channel->dvc_data_length)
844 WLog_Print(drdynvc->log, WLOG_ERROR,
"data exceeding declared length!");
845 Stream_Release(channel->dvc_data);
846 channel->dvc_data = NULL;
847 status = ERROR_INVALID_DATA;
851 Stream_Copy(data, channel->dvc_data, dataSize);
853 if (Stream_GetPosition(channel->dvc_data) >= channel->dvc_data_length)
855 Stream_SealLength(channel->dvc_data);
856 Stream_SetPosition(channel->dvc_data, 0);
858 status = dvcman_call_on_receive(channel, channel->dvc_data);
859 Stream_Release(channel->dvc_data);
860 channel->dvc_data = NULL;
864 status = dvcman_call_on_receive(channel, data);
870 static UINT8 drdynvc_write_variable_uint(
wStream* s, UINT32 val)
877 Stream_Write_UINT8(s, (UINT8)val);
879 else if (val <= 0xFFFF)
882 Stream_Write_UINT16(s, (UINT16)val);
887 Stream_Write_UINT32(s, val);
898 static UINT drdynvc_send(drdynvcPlugin* drdynvc,
wStream* s)
903 status = CHANNEL_RC_BAD_CHANNEL_HANDLE;
906 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelWriteEx);
907 status = drdynvc->channelEntryPoints.pVirtualChannelWriteEx(
908 drdynvc->InitHandle, drdynvc->OpenHandle, Stream_Buffer(s),
909 (UINT32)Stream_GetPosition(s), s);
915 return CHANNEL_RC_OK;
917 case CHANNEL_RC_NOT_CONNECTED:
919 return CHANNEL_RC_OK;
921 case CHANNEL_RC_BAD_CHANNEL_HANDLE:
923 WLog_ERR(TAG,
"VirtualChannelWriteEx failed with CHANNEL_RC_BAD_CHANNEL_HANDLE");
928 WLog_Print(drdynvc->log, WLOG_ERROR,
929 "VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
930 WTSErrorToString(status), status);
940 static UINT drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId,
const BYTE* data,
941 UINT32 dataSize, BOOL* close)
947 unsigned long chunkLength = 0;
948 UINT status = CHANNEL_RC_BAD_INIT_HANDLE;
952 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
954 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
955 WINPR_ASSERT(dvcman);
957 WLog_Print(drdynvc->log, WLOG_TRACE,
"write_data: ChannelId=%" PRIu32
" size=%" PRIu32
"",
958 ChannelId, dataSize);
959 data_out = StreamPool_Take(dvcman->pool, CHANNEL_CHUNK_LENGTH);
963 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
964 return CHANNEL_RC_NO_MEMORY;
967 Stream_SetPosition(data_out, 1);
968 cbChId = drdynvc_write_variable_uint(data_out, ChannelId);
969 pos = Stream_GetPosition(data_out);
975 Stream_Release(data_out);
977 else if (dataSize <= CHANNEL_CHUNK_LENGTH - pos)
979 Stream_SetPosition(data_out, 0);
980 Stream_Write_UINT8(data_out, (DATA_PDU << 4) | cbChId);
981 Stream_SetPosition(data_out, pos);
982 Stream_Write(data_out, data, dataSize);
983 status = drdynvc_send(drdynvc, data_out);
988 cbLen = drdynvc_write_variable_uint(data_out, dataSize);
989 pos = Stream_GetPosition(data_out);
990 Stream_SetPosition(data_out, 0);
991 Stream_Write_UINT8(data_out, (DATA_FIRST_PDU << 4) | cbChId | (cbLen << 2));
992 Stream_SetPosition(data_out, pos);
993 chunkLength = CHANNEL_CHUNK_LENGTH - pos;
994 Stream_Write(data_out, data, chunkLength);
996 dataSize -= chunkLength;
997 status = drdynvc_send(drdynvc, data_out);
999 while (status == CHANNEL_RC_OK && dataSize > 0)
1001 data_out = StreamPool_Take(dvcman->pool, CHANNEL_CHUNK_LENGTH);
1005 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
1006 return CHANNEL_RC_NO_MEMORY;
1009 Stream_SetPosition(data_out, 1);
1010 cbChId = drdynvc_write_variable_uint(data_out, ChannelId);
1011 pos = Stream_GetPosition(data_out);
1012 Stream_SetPosition(data_out, 0);
1013 Stream_Write_UINT8(data_out, (DATA_PDU << 4) | cbChId);
1014 Stream_SetPosition(data_out, pos);
1015 chunkLength = dataSize;
1017 if (chunkLength > CHANNEL_CHUNK_LENGTH - pos)
1018 chunkLength = CHANNEL_CHUNK_LENGTH - pos;
1020 Stream_Write(data_out, data, chunkLength);
1021 data += chunkLength;
1022 dataSize -= chunkLength;
1023 status = drdynvc_send(drdynvc, data_out);
1027 if (status != CHANNEL_RC_OK)
1029 WLog_Print(drdynvc->log, WLOG_ERROR,
"VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1030 WTSErrorToString(status), status);
1034 return CHANNEL_RC_OK;
1042 static UINT drdynvc_send_capability_response(drdynvcPlugin* drdynvc)
1049 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1051 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1052 WINPR_ASSERT(dvcman);
1054 WLog_Print(drdynvc->log, WLOG_TRACE,
"capability_response");
1055 s = StreamPool_Take(dvcman->pool, 4);
1059 WLog_Print(drdynvc->log, WLOG_ERROR,
"Stream_Ndrdynvc_write_variable_uintew failed!");
1060 return CHANNEL_RC_NO_MEMORY;
1063 Stream_Write_UINT16(s, 0x0050);
1064 Stream_Write_UINT16(s, drdynvc->version);
1065 status = drdynvc_send(drdynvc, s);
1067 if (status != CHANNEL_RC_OK)
1069 WLog_Print(drdynvc->log, WLOG_ERROR,
"VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1070 WTSErrorToString(status), status);
1081 static UINT drdynvc_process_capability_request(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
1087 return CHANNEL_RC_BAD_INIT_HANDLE;
1089 if (!Stream_CheckAndLogRequiredLength(TAG, s, 3))
1090 return ERROR_INVALID_DATA;
1092 WLog_Print(drdynvc->log, WLOG_TRACE,
"capability_request Sp=%d cbChId=%d", Sp, cbChId);
1094 Stream_Read_UINT16(s, drdynvc->version);
1099 if ((drdynvc->version == 2) || (drdynvc->version == 3))
1101 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1102 return ERROR_INVALID_DATA;
1104 Stream_Read_UINT16(s, drdynvc->PriorityCharge0);
1105 Stream_Read_UINT16(s, drdynvc->PriorityCharge1);
1106 Stream_Read_UINT16(s, drdynvc->PriorityCharge2);
1107 Stream_Read_UINT16(s, drdynvc->PriorityCharge3);
1110 status = drdynvc_send_capability_response(drdynvc);
1111 drdynvc->state = DRDYNVC_STATE_READY;
1115 static UINT32 drdynvc_cblen_to_bytes(
int cbLen)
1130 static UINT32 drdynvc_read_variable_uint(
wStream* s,
int cbLen)
1137 Stream_Read_UINT8(s, val);
1141 Stream_Read_UINT16(s, val);
1145 Stream_Read_UINT32(s, val);
1157 static UINT drdynvc_process_create_request(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
wStream* s)
1161 UINT channel_status = 0;
1164 UINT32 retStatus = 0;
1168 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1170 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1171 WINPR_ASSERT(dvcman);
1173 if (drdynvc->state == DRDYNVC_STATE_CAPABILITIES)
1180 drdynvc->version = 3;
1182 if ((status = drdynvc_send_capability_response(drdynvc)))
1184 WLog_Print(drdynvc->log, WLOG_ERROR,
"drdynvc_send_capability_response failed!");
1188 drdynvc->state = DRDYNVC_STATE_READY;
1191 if (!Stream_CheckAndLogRequiredLength(TAG, s, drdynvc_cblen_to_bytes(cbChId)))
1192 return ERROR_INVALID_DATA;
1194 const UINT32 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1195 const size_t pos = Stream_GetPosition(s);
1196 const char* name = Stream_ConstPointer(s);
1197 const size_t length = Stream_GetRemainingLength(s);
1199 if (strnlen(name, length) >= length)
1200 return ERROR_INVALID_DATA;
1202 WLog_Print(drdynvc->log, WLOG_DEBUG,
1203 "process_create_request: ChannelId=%" PRIu32
" ChannelName=%s", ChannelId, name);
1205 data_out = StreamPool_Take(dvcman->pool, pos + 4);
1208 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
1209 return CHANNEL_RC_NO_MEMORY;
1212 Stream_Write_UINT8(data_out, (CREATE_REQUEST_PDU << 4) | cbChId);
1213 Stream_SetPosition(s, 1);
1214 Stream_Copy(s, data_out, pos - 1);
1217 dvcman_create_channel(drdynvc, drdynvc->channel_mgr, ChannelId, name, &channel_status);
1218 switch (channel_status)
1221 WLog_Print(drdynvc->log, WLOG_DEBUG,
"channel created");
1224 case CHANNEL_RC_NO_MEMORY:
1225 WLog_Print(drdynvc->log, WLOG_DEBUG,
"not enough memory for channel creation");
1226 retStatus = STATUS_NO_MEMORY;
1228 case ERROR_NOT_FOUND:
1229 WLog_Print(drdynvc->log, WLOG_DEBUG,
"no listener for '%s'", name);
1230 retStatus = (UINT32)0xC0000001;
1233 WLog_Print(drdynvc->log, WLOG_DEBUG,
"channel creation error");
1234 retStatus = (UINT32)0xC0000001;
1237 Stream_Write_UINT32(data_out, retStatus);
1239 status = drdynvc_send(drdynvc, data_out);
1240 if (status != CHANNEL_RC_OK)
1242 WLog_Print(drdynvc->log, WLOG_ERROR,
"VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1243 WTSErrorToString(status), status);
1244 dvcman_channel_unref(channel);
1248 if (channel_status == CHANNEL_RC_OK)
1250 if ((status = dvcman_open_channel(drdynvc, channel)))
1252 WLog_Print(drdynvc->log, WLOG_ERROR,
1253 "dvcman_open_channel failed with error %" PRIu32
"!", status);
1266 static UINT drdynvc_process_data_first(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
wStream* s,
1267 UINT32 ThreadingFlags)
1269 UINT status = CHANNEL_RC_OK;
1271 UINT32 ChannelId = 0;
1274 WINPR_ASSERT(drdynvc);
1275 if (!Stream_CheckAndLogRequiredLength(
1276 TAG, s, drdynvc_cblen_to_bytes(cbChId) + drdynvc_cblen_to_bytes(Sp)))
1277 return ERROR_INVALID_DATA;
1279 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1280 Length = drdynvc_read_variable_uint(s, Sp);
1281 WLog_Print(drdynvc->log, WLOG_TRACE,
1282 "process_data_first: Sp=%d cbChId=%d, ChannelId=%" PRIu32
" Length=%" PRIu32
"", Sp,
1283 cbChId, ChannelId, Length);
1285 channel = dvcman_get_channel_by_id(drdynvc->channel_mgr, ChannelId, TRUE);
1293 WLog_Print(drdynvc->log, WLOG_ERROR,
"ChannelId %" PRIu32
" not found!", ChannelId);
1294 return CHANNEL_RC_OK;
1297 if (channel->state != DVC_CHANNEL_RUNNING)
1300 status = dvcman_receive_channel_data_first(channel, Length);
1302 if (status == CHANNEL_RC_OK)
1303 status = dvcman_receive_channel_data(channel, s, ThreadingFlags);
1305 if (status != CHANNEL_RC_OK)
1306 status = dvcman_channel_close(channel, FALSE, FALSE);
1309 dvcman_channel_unref(channel);
1318 static UINT drdynvc_process_data(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
wStream* s,
1319 UINT32 ThreadingFlags)
1321 UINT32 ChannelId = 0;
1323 UINT status = CHANNEL_RC_OK;
1325 WINPR_ASSERT(drdynvc);
1326 if (!Stream_CheckAndLogRequiredLength(TAG, s, drdynvc_cblen_to_bytes(cbChId)))
1327 return ERROR_INVALID_DATA;
1329 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1330 WLog_Print(drdynvc->log, WLOG_TRACE,
"process_data: Sp=%d cbChId=%d, ChannelId=%" PRIu32
"", Sp,
1333 channel = dvcman_get_channel_by_id(drdynvc->channel_mgr, ChannelId, TRUE);
1341 WLog_Print(drdynvc->log, WLOG_ERROR,
"ChannelId %" PRIu32
" not found!", ChannelId);
1342 return CHANNEL_RC_OK;
1345 if (channel->state != DVC_CHANNEL_RUNNING)
1348 status = dvcman_receive_channel_data(channel, s, ThreadingFlags);
1349 if (status != CHANNEL_RC_OK)
1350 status = dvcman_channel_close(channel, FALSE, FALSE);
1353 dvcman_channel_unref(channel);
1362 static UINT drdynvc_process_close_request(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
wStream* s)
1364 UINT32 ChannelId = 0;
1367 WINPR_ASSERT(drdynvc);
1368 if (!Stream_CheckAndLogRequiredLength(TAG, s, drdynvc_cblen_to_bytes(cbChId)))
1369 return ERROR_INVALID_DATA;
1371 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1372 WLog_Print(drdynvc->log, WLOG_DEBUG,
1373 "process_close_request: Sp=%d cbChId=%d, ChannelId=%" PRIu32
"", Sp, cbChId,
1376 channel = dvcman_get_channel_by_id(drdynvc->channel_mgr, ChannelId, TRUE);
1379 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_close_request channel %" PRIu32
" not present",
1381 return CHANNEL_RC_OK;
1384 dvcman_channel_close(channel, TRUE, FALSE);
1385 dvcman_channel_unref(channel);
1386 return CHANNEL_RC_OK;
1394 static UINT drdynvc_order_recv(drdynvcPlugin* drdynvc,
wStream* s, UINT32 ThreadingFlags)
1398 WINPR_ASSERT(drdynvc);
1399 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
1400 return ERROR_INVALID_DATA;
1402 Stream_Read_UINT8(s, value);
1403 const UINT8 Cmd = (value & 0xf0) >> 4;
1404 const UINT8 Sp = (value & 0x0c) >> 2;
1405 const UINT8 cbChId = (value & 0x03) >> 0;
1406 WLog_Print(drdynvc->log, WLOG_TRACE,
"order_recv: Cmd=%s, Sp=%" PRIu8
" cbChId=%" PRIu8,
1407 drdynvc_get_packet_type(Cmd), Sp, cbChId);
1411 case CAPABILITY_REQUEST_PDU:
1412 return drdynvc_process_capability_request(drdynvc, Sp, cbChId, s);
1414 case CREATE_REQUEST_PDU:
1415 return drdynvc_process_create_request(drdynvc, Sp, cbChId, s);
1417 case DATA_FIRST_PDU:
1418 return drdynvc_process_data_first(drdynvc, Sp, cbChId, s, ThreadingFlags);
1421 return drdynvc_process_data(drdynvc, Sp, cbChId, s, ThreadingFlags);
1423 case CLOSE_REQUEST_PDU:
1424 return drdynvc_process_close_request(drdynvc, Sp, cbChId, s);
1427 WLog_Print(drdynvc->log, WLOG_ERROR,
"unknown drdynvc cmd 0x%x", Cmd);
1428 return ERROR_INTERNAL_ERROR;
1437 static UINT drdynvc_virtual_channel_event_data_received(drdynvcPlugin* drdynvc,
void* pData,
1438 UINT32 dataLength, UINT32 totalLength,
1443 WINPR_ASSERT(drdynvc);
1444 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1446 return CHANNEL_RC_OK;
1449 if (dataFlags & CHANNEL_FLAG_FIRST)
1452 if (drdynvc->data_in)
1453 Stream_Release(drdynvc->data_in);
1455 drdynvc->data_in = StreamPool_Take(mgr->pool, totalLength);
1458 if (!(data_in = drdynvc->data_in))
1460 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
1461 return CHANNEL_RC_NO_MEMORY;
1464 if (!Stream_EnsureRemainingCapacity(data_in, dataLength))
1466 WLog_Print(drdynvc->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
1467 Stream_Release(drdynvc->data_in);
1468 drdynvc->data_in = NULL;
1469 return ERROR_INTERNAL_ERROR;
1472 Stream_Write(data_in, pData, dataLength);
1474 if (dataFlags & CHANNEL_FLAG_LAST)
1476 const size_t cap = Stream_Capacity(data_in);
1477 const size_t pos = Stream_GetPosition(data_in);
1480 WLog_Print(drdynvc->log, WLOG_ERROR,
"drdynvc_plugin_process_received: read error");
1481 return ERROR_INVALID_DATA;
1484 drdynvc->data_in = NULL;
1485 Stream_SealLength(data_in);
1486 Stream_SetPosition(data_in, 0);
1490 if (!MessageQueue_Post(drdynvc->queue, NULL, 0, (
void*)data_in, NULL))
1492 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_Post failed!");
1493 return ERROR_INTERNAL_ERROR;
1498 UINT error = drdynvc_order_recv(drdynvc, data_in, TRUE);
1499 Stream_Release(data_in);
1503 WLog_Print(drdynvc->log, WLOG_WARN,
1504 "drdynvc_order_recv failed with error %" PRIu32
"!", error);
1509 return CHANNEL_RC_OK;
1512 static void VCAPITYPE drdynvc_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1513 UINT event, LPVOID pData,
1514 UINT32 dataLength, UINT32 totalLength,
1517 UINT error = CHANNEL_RC_OK;
1518 drdynvcPlugin* drdynvc = (drdynvcPlugin*)lpUserParam;
1520 WINPR_ASSERT(drdynvc);
1523 case CHANNEL_EVENT_DATA_RECEIVED:
1524 if (!drdynvc || (drdynvc->OpenHandle != openHandle))
1526 WLog_ERR(TAG,
"drdynvc_virtual_channel_open_event: error no match");
1529 if ((error = drdynvc_virtual_channel_event_data_received(drdynvc, pData, dataLength,
1530 totalLength, dataFlags)))
1531 WLog_Print(drdynvc->log, WLOG_ERROR,
1532 "drdynvc_virtual_channel_event_data_received failed with error %" PRIu32
1538 case CHANNEL_EVENT_WRITE_CANCELLED:
1539 case CHANNEL_EVENT_WRITE_COMPLETE:
1546 case CHANNEL_EVENT_USER:
1552 if (error && drdynvc && drdynvc->rdpcontext)
1553 setChannelError(drdynvc->rdpcontext, error,
1554 "drdynvc_virtual_channel_open_event reported an error");
1557 static DWORD WINAPI drdynvc_virtual_channel_client_thread(LPVOID arg)
1561 wMessage message = { 0 };
1562 UINT error = CHANNEL_RC_OK;
1563 drdynvcPlugin* drdynvc = (drdynvcPlugin*)arg;
1567 ExitThread((DWORD)CHANNEL_RC_BAD_CHANNEL_HANDLE);
1568 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1573 if (!MessageQueue_Wait(drdynvc->queue))
1575 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_Wait failed!");
1576 error = ERROR_INTERNAL_ERROR;
1580 if (!MessageQueue_Peek(drdynvc->queue, &message, TRUE))
1582 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_Peek failed!");
1583 error = ERROR_INTERNAL_ERROR;
1587 if (message.id == WMQ_QUIT)
1590 if (message.id == 0)
1592 UINT32 ThreadingFlags = TRUE;
1593 data = (
wStream*)message.wParam;
1595 if ((error = drdynvc_order_recv(drdynvc, data, ThreadingFlags)))
1597 WLog_Print(drdynvc->log, WLOG_WARN,
1598 "drdynvc_order_recv failed with error %" PRIu32
"!", error);
1601 Stream_Release(data);
1611 HashTable_Clear(drdynvcMgr->channelsById);
1614 if (error && drdynvc->rdpcontext)
1615 setChannelError(drdynvc->rdpcontext, error,
1616 "drdynvc_virtual_channel_client_thread reported an error");
1618 ExitThread((DWORD)error);
1622 static void drdynvc_queue_object_free(
void* obj)
1625 wMessage* msg = (wMessage*)obj;
1627 if (!msg || (msg->id != 0))
1636 static UINT drdynvc_virtual_channel_event_initialized(drdynvcPlugin* drdynvc, LPVOID pData,
1640 WINPR_UNUSED(pData);
1641 WINPR_UNUSED(dataLength);
1646 drdynvc->queue = MessageQueue_New(NULL);
1648 if (!drdynvc->queue)
1650 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_New failed!");
1654 obj = MessageQueue_Object(drdynvc->queue);
1655 obj->fnObjectFree = drdynvc_queue_object_free;
1656 drdynvc->channel_mgr = dvcman_new(drdynvc);
1658 if (!drdynvc->channel_mgr)
1660 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_new failed!");
1664 return CHANNEL_RC_OK;
1666 return ERROR_INTERNAL_ERROR;
1674 static UINT drdynvc_virtual_channel_event_connected(drdynvcPlugin* drdynvc, LPVOID pData,
1679 rdpSettings* settings = NULL;
1681 WINPR_ASSERT(drdynvc);
1682 WINPR_UNUSED(pData);
1683 WINPR_UNUSED(dataLength);
1686 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1688 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelOpenEx);
1689 status = drdynvc->channelEntryPoints.pVirtualChannelOpenEx(
1690 drdynvc->InitHandle, &drdynvc->OpenHandle, drdynvc->channelDef.name,
1691 drdynvc_virtual_channel_open_event_ex);
1693 if (status != CHANNEL_RC_OK)
1695 WLog_Print(drdynvc->log, WLOG_ERROR,
"pVirtualChannelOpen failed with %s [%08" PRIX32
"]",
1696 WTSErrorToString(status), status);
1700 WINPR_ASSERT(drdynvc->rdpcontext);
1701 settings = drdynvc->rdpcontext->settings;
1702 WINPR_ASSERT(settings);
1704 for (UINT32 index = 0;
1708 freerdp_settings_get_pointer_array(settings, FreeRDP_DynamicChannelArray, index);
1709 error = dvcman_load_addin(drdynvc, drdynvc->channel_mgr, args, drdynvc->rdpcontext);
1711 if (CHANNEL_RC_OK != error)
1715 if ((error = dvcman_init(drdynvc, drdynvc->channel_mgr)))
1717 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_init failed with error %" PRIu32
"!", error);
1721 drdynvc->state = DRDYNVC_STATE_CAPABILITIES;
1725 if (!(drdynvc->thread = CreateThread(NULL, 0, drdynvc_virtual_channel_client_thread,
1726 (
void*)drdynvc, 0, NULL)))
1728 error = ERROR_INTERNAL_ERROR;
1729 WLog_Print(drdynvc->log, WLOG_ERROR,
"CreateThread failed!");
1733 if (!SetThreadPriority(drdynvc->thread, THREAD_PRIORITY_HIGHEST))
1734 WLog_Print(drdynvc->log, WLOG_WARN,
"SetThreadPriority failed, ignoring.");
1746 static UINT drdynvc_virtual_channel_event_disconnected(drdynvcPlugin* drdynvc)
1751 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1753 if (drdynvc->OpenHandle == 0)
1754 return CHANNEL_RC_OK;
1758 if (!MessageQueue_PostQuit(drdynvc->queue, 0))
1760 status = GetLastError();
1761 WLog_Print(drdynvc->log, WLOG_ERROR,
1762 "MessageQueue_PostQuit failed with error %" PRIu32
"", status);
1767 if (drdynvc->thread)
1769 if (WaitForSingleObject(drdynvc->thread, INFINITE) != WAIT_OBJECT_0)
1771 status = GetLastError();
1772 WLog_Print(drdynvc->log, WLOG_ERROR,
1773 "WaitForSingleObject failed with error %" PRIu32
"", status);
1777 (void)CloseHandle(drdynvc->thread);
1778 drdynvc->thread = NULL;
1788 HashTable_Clear(drdynvcMgr->channelsById);
1792 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelCloseEx);
1793 status = drdynvc->channelEntryPoints.pVirtualChannelCloseEx(drdynvc->InitHandle,
1794 drdynvc->OpenHandle);
1796 if (status != CHANNEL_RC_OK)
1798 WLog_Print(drdynvc->log, WLOG_ERROR,
"pVirtualChannelClose failed with %s [%08" PRIX32
"]",
1799 WTSErrorToString(status), status);
1802 dvcman_clear(drdynvc, drdynvc->channel_mgr);
1804 MessageQueue_Clear(drdynvc->queue);
1805 drdynvc->OpenHandle = 0;
1807 if (drdynvc->data_in)
1809 Stream_Release(drdynvc->data_in);
1810 drdynvc->data_in = NULL;
1821 static UINT drdynvc_virtual_channel_event_terminated(drdynvcPlugin* drdynvc)
1824 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1826 MessageQueue_Free(drdynvc->queue);
1827 drdynvc->queue = NULL;
1829 if (drdynvc->channel_mgr)
1831 dvcman_free(drdynvc, drdynvc->channel_mgr);
1832 drdynvc->channel_mgr = NULL;
1834 drdynvc->InitHandle = 0;
1835 free(drdynvc->context);
1837 return CHANNEL_RC_OK;
1840 static UINT drdynvc_virtual_channel_event_attached(drdynvcPlugin* drdynvc)
1842 UINT error = CHANNEL_RC_OK;
1846 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1848 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1851 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1853 ArrayList_Lock(dvcman->plugins);
1854 for (
size_t i = 0; i < ArrayList_Count(dvcman->plugins); i++)
1856 IWTSPlugin* pPlugin = ArrayList_GetItem(dvcman->plugins, i);
1858 error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Attached, pPlugin);
1859 if (error != CHANNEL_RC_OK)
1861 WLog_Print(drdynvc->log, WLOG_ERROR,
"Attach failed with error %" PRIu32
"!", error);
1867 ArrayList_Unlock(dvcman->plugins);
1871 static UINT drdynvc_virtual_channel_event_detached(drdynvcPlugin* drdynvc)
1873 UINT error = CHANNEL_RC_OK;
1877 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1879 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1882 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1884 ArrayList_Lock(dvcman->plugins);
1885 for (
size_t i = 0; i < ArrayList_Count(dvcman->plugins); i++)
1887 IWTSPlugin* pPlugin = ArrayList_GetItem(dvcman->plugins, i);
1889 error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Detached, pPlugin);
1890 if (error != CHANNEL_RC_OK)
1892 WLog_Print(drdynvc->log, WLOG_ERROR,
"Detach failed with error %" PRIu32
"!", error);
1898 ArrayList_Unlock(dvcman->plugins);
1903 static VOID VCAPITYPE drdynvc_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1904 UINT event, LPVOID pData,
1907 UINT error = CHANNEL_RC_OK;
1908 drdynvcPlugin* drdynvc = (drdynvcPlugin*)lpUserParam;
1910 if (!drdynvc || (drdynvc->InitHandle != pInitHandle))
1912 WLog_ERR(TAG,
"drdynvc_virtual_channel_init_event: error no match");
1918 case CHANNEL_EVENT_INITIALIZED:
1919 error = drdynvc_virtual_channel_event_initialized(drdynvc, pData, dataLength);
1921 case CHANNEL_EVENT_CONNECTED:
1922 if ((error = drdynvc_virtual_channel_event_connected(drdynvc, pData, dataLength)))
1923 WLog_Print(drdynvc->log, WLOG_ERROR,
1924 "drdynvc_virtual_channel_event_connected failed with error %" PRIu32
"",
1929 case CHANNEL_EVENT_DISCONNECTED:
1930 if ((error = drdynvc_virtual_channel_event_disconnected(drdynvc)))
1931 WLog_Print(drdynvc->log, WLOG_ERROR,
1932 "drdynvc_virtual_channel_event_disconnected failed with error %" PRIu32
1938 case CHANNEL_EVENT_TERMINATED:
1939 if ((error = drdynvc_virtual_channel_event_terminated(drdynvc)))
1940 WLog_Print(drdynvc->log, WLOG_ERROR,
1941 "drdynvc_virtual_channel_event_terminated failed with error %" PRIu32
"",
1946 case CHANNEL_EVENT_ATTACHED:
1947 if ((error = drdynvc_virtual_channel_event_attached(drdynvc)))
1948 WLog_Print(drdynvc->log, WLOG_ERROR,
1949 "drdynvc_virtual_channel_event_attached failed with error %" PRIu32
"",
1954 case CHANNEL_EVENT_DETACHED:
1955 if ((error = drdynvc_virtual_channel_event_detached(drdynvc)))
1956 WLog_Print(drdynvc->log, WLOG_ERROR,
1957 "drdynvc_virtual_channel_event_detached failed with error %" PRIu32
"",
1966 if (error && drdynvc->rdpcontext)
1967 setChannelError(drdynvc->rdpcontext, error,
1968 "drdynvc_virtual_channel_init_event_ex reported an error");
1975 static int drdynvc_get_version(DrdynvcClientContext* context)
1977 WINPR_ASSERT(context);
1978 drdynvcPlugin* drdynvc = (drdynvcPlugin*)context->handle;
1979 WINPR_ASSERT(drdynvc);
1980 return drdynvc->version;
1984 #define VirtualChannelEntryEx drdynvc_VirtualChannelEntryEx
1986 FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints,
1990 drdynvcPlugin* drdynvc = NULL;
1991 DrdynvcClientContext* context = NULL;
1993 drdynvc = (drdynvcPlugin*)calloc(1,
sizeof(drdynvcPlugin));
1995 WINPR_ASSERT(pEntryPoints);
1998 WLog_ERR(TAG,
"calloc failed!");
2002 drdynvc->channelDef.options =
2003 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
2004 (void)sprintf_s(drdynvc->channelDef.name, ARRAYSIZE(drdynvc->channelDef.name),
2005 DRDYNVC_SVC_CHANNEL_NAME);
2006 drdynvc->state = DRDYNVC_STATE_INITIAL;
2010 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
2012 context = (DrdynvcClientContext*)calloc(1,
sizeof(DrdynvcClientContext));
2016 WLog_Print(drdynvc->log, WLOG_ERROR,
"calloc failed!");
2021 context->handle = (
void*)drdynvc;
2022 context->custom = NULL;
2023 drdynvc->context = context;
2024 context->GetVersion = drdynvc_get_version;
2025 drdynvc->rdpcontext = pEntryPointsEx->context;
2027 FreeRDP_TransportDumpReplay) &&
2029 FreeRDP_SynchronousDynamicChannels))
2030 drdynvc->async = TRUE;
2033 drdynvc->log = WLog_Get(TAG);
2034 WLog_Print(drdynvc->log, WLOG_DEBUG,
"VirtualChannelEntryEx");
2035 CopyMemory(&(drdynvc->channelEntryPoints), pEntryPoints,
2037 drdynvc->InitHandle = pInitHandle;
2039 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelInitEx);
2040 rc = drdynvc->channelEntryPoints.pVirtualChannelInitEx(
2041 drdynvc, context, pInitHandle, &drdynvc->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
2042 drdynvc_virtual_channel_init_event_ex);
2044 if (CHANNEL_RC_OK != rc)
2046 WLog_Print(drdynvc->log, WLOG_ERROR,
"pVirtualChannelInit failed with %s [%08" PRIX32
"]",
2047 WTSErrorToString(rc), rc);
2048 free(drdynvc->context);
2053 drdynvc->channelEntryPoints.pInterface = context;
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
This struct contains function pointer to initialize/free objects.