22 #include <freerdp/config.h>
24 #include <winpr/crt.h>
25 #include <winpr/cast.h>
26 #include <winpr/stream.h>
27 #include <winpr/interlocked.h>
29 #include <freerdp/freerdp.h>
30 #include <freerdp/channels/drdynvc.h>
31 #include <freerdp/utils/drdynvc.h>
33 #include "drdynvc_main.h"
35 #define TAG CHANNELS_TAG("drdynvc.client")
38 static UINT dvcman_channel_close(
DVCMAN_CHANNEL* channel, BOOL perRequest, BOOL fromHashTableFn);
39 static void dvcman_free(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr);
40 static UINT drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId,
const BYTE* data,
41 UINT32 dataSize, BOOL* close);
42 static UINT drdynvc_send(drdynvcPlugin* drdynvc,
wStream* s);
47 free(listener->channel_name);
56 static UINT dvcman_get_configuration(IWTSListener* pListener,
void** ppPropertyBag)
58 WINPR_ASSERT(ppPropertyBag);
59 WINPR_UNUSED(pListener);
60 *ppPropertyBag = NULL;
61 return ERROR_INTERNAL_ERROR;
69 static UINT dvcman_create_listener(IWTSVirtualChannelManager* pChannelMgr,
70 const char* pszChannelName, ULONG ulFlags,
71 IWTSListenerCallback* pListenerCallback,
72 IWTSListener** ppListener)
78 WLog_DBG(TAG,
"create_listener: %" PRIuz
".%s.", HashTable_Count(dvcman->listeners) + 1,
84 WLog_ERR(TAG,
"calloc failed!");
85 return CHANNEL_RC_NO_MEMORY;
88 listener->iface.GetConfiguration = dvcman_get_configuration;
89 listener->iface.pInterface = NULL;
90 listener->dvcman = dvcman;
91 listener->channel_name = _strdup(pszChannelName);
93 if (!listener->channel_name)
95 WLog_ERR(TAG,
"_strdup failed!");
96 dvcman_wtslistener_free(listener);
97 return CHANNEL_RC_NO_MEMORY;
100 listener->flags = ulFlags;
101 listener->listener_callback = pListenerCallback;
104 *ppListener = (IWTSListener*)listener;
106 if (!HashTable_Insert(dvcman->listeners, listener->channel_name, listener))
108 dvcman_wtslistener_free(listener);
109 return ERROR_INTERNAL_ERROR;
113 return CHANNEL_RC_OK;
116 static UINT dvcman_destroy_listener(IWTSVirtualChannelManager* pChannelMgr, IWTSListener* pListener)
120 WINPR_UNUSED(pChannelMgr);
124 DVCMAN* dvcman = listener->dvcman;
126 HashTable_Remove(dvcman->listeners, listener->channel_name);
129 return CHANNEL_RC_OK;
137 static UINT dvcman_register_plugin(IDRDYNVC_ENTRY_POINTS* pEntryPoints,
const char* name,
140 WINPR_ASSERT(pEntryPoints);
143 WINPR_ASSERT(dvcman);
144 if (!ArrayList_Append(dvcman->plugin_names, name))
145 return ERROR_INTERNAL_ERROR;
146 if (!ArrayList_Append(dvcman->plugins, pPlugin))
147 return ERROR_INTERNAL_ERROR;
149 WLog_DBG(TAG,
"register_plugin: num_plugins %" PRIuz, ArrayList_Count(dvcman->plugins));
150 return CHANNEL_RC_OK;
153 static IWTSPlugin* dvcman_get_plugin(IDRDYNVC_ENTRY_POINTS* pEntryPoints,
const char* name)
155 IWTSPlugin* plugin = NULL;
158 WINPR_ASSERT(pEntryPoints);
160 if (!dvcman || !pEntryPoints || !name)
163 nc = ArrayList_Count(dvcman->plugin_names);
164 pc = ArrayList_Count(dvcman->plugins);
168 ArrayList_Lock(dvcman->plugin_names);
169 ArrayList_Lock(dvcman->plugins);
170 for (
size_t i = 0; i < pc; i++)
172 const char* cur = ArrayList_GetItem(dvcman->plugin_names, i);
173 if (strcmp(cur, name) == 0)
175 plugin = ArrayList_GetItem(dvcman->plugins, i);
179 ArrayList_Unlock(dvcman->plugin_names);
180 ArrayList_Unlock(dvcman->plugins);
184 static const ADDIN_ARGV* dvcman_get_plugin_data(IDRDYNVC_ENTRY_POINTS* pEntryPoints)
186 WINPR_ASSERT(pEntryPoints);
190 static rdpContext* dvcman_get_rdp_context(IDRDYNVC_ENTRY_POINTS* pEntryPoints)
194 return entry->context;
197 static rdpSettings* dvcman_get_rdp_settings(IDRDYNVC_ENTRY_POINTS* pEntryPoints)
199 rdpContext* context = dvcman_get_rdp_context(pEntryPoints);
200 WINPR_ASSERT(context);
202 return context->settings;
205 static UINT32 dvcman_get_channel_id(IWTSVirtualChannel* channel)
209 return dvc->channel_id;
212 static const char* dvcman_get_channel_name(IWTSVirtualChannel* channel)
216 return dvc->channel_name;
219 static DVCMAN_CHANNEL* dvcman_get_channel_by_id(IWTSVirtualChannelManager* pChannelMgr,
220 UINT32 ChannelId, BOOL doRef)
225 WINPR_ASSERT(dvcman);
226 HashTable_Lock(dvcman->channelsById);
227 dvcChannel = HashTable_GetItemValue(dvcman->channelsById, &ChannelId);
231 InterlockedIncrement(&dvcChannel->refCounter);
234 HashTable_Unlock(dvcman->channelsById);
238 static IWTSVirtualChannel* dvcman_find_channel_by_id(IWTSVirtualChannelManager* pChannelMgr,
241 DVCMAN_CHANNEL* channel = dvcman_get_channel_by_id(pChannelMgr, ChannelId, FALSE);
245 return &channel->iface;
248 static void dvcman_plugin_terminate(
void* plugin)
250 IWTSPlugin* pPlugin = plugin;
252 WINPR_ASSERT(pPlugin);
253 UINT error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Terminated, pPlugin);
254 if (error != CHANNEL_RC_OK)
255 WLog_ERR(TAG,
"Terminated failed with error %" PRIu32
"!", error);
258 static void wts_listener_free(
void* arg)
261 dvcman_wtslistener_free(listener);
264 static BOOL channelIdMatch(
const void* k1,
const void* k2)
268 return *((
const UINT32*)k1) == *((
const UINT32*)k2);
271 static UINT32 channelIdHash(
const void*
id)
274 return *((
const UINT32*)
id);
277 static void channelByIdCleanerFn(
void* value)
282 dvcman_channel_close(channel, FALSE, TRUE);
283 dvcman_channel_free(channel);
287 static IWTSVirtualChannelManager* dvcman_new(drdynvcPlugin* plugin)
295 dvcman->iface.CreateListener = dvcman_create_listener;
296 dvcman->iface.DestroyListener = dvcman_destroy_listener;
297 dvcman->iface.FindChannelById = dvcman_find_channel_by_id;
298 dvcman->iface.GetChannelId = dvcman_get_channel_id;
299 dvcman->iface.GetChannelName = dvcman_get_channel_name;
300 dvcman->drdynvc = plugin;
301 dvcman->channelsById = HashTable_New(TRUE);
303 if (!dvcman->channelsById)
306 HashTable_SetHashFunction(dvcman->channelsById, channelIdHash);
307 obj = HashTable_KeyObject(dvcman->channelsById);
309 obj->fnObjectEquals = channelIdMatch;
311 obj = HashTable_ValueObject(dvcman->channelsById);
313 obj->fnObjectFree = channelByIdCleanerFn;
315 dvcman->pool = StreamPool_New(TRUE, 10);
319 dvcman->listeners = HashTable_New(TRUE);
320 if (!dvcman->listeners)
322 HashTable_SetHashFunction(dvcman->listeners, HashTable_StringHash);
324 obj = HashTable_KeyObject(dvcman->listeners);
325 obj->fnObjectEquals = HashTable_StringCompare;
327 obj = HashTable_ValueObject(dvcman->listeners);
328 obj->fnObjectFree = wts_listener_free;
330 dvcman->plugin_names = ArrayList_New(TRUE);
331 if (!dvcman->plugin_names)
333 obj = ArrayList_Object(dvcman->plugin_names);
334 obj->fnObjectNew = winpr_ObjectStringClone;
335 obj->fnObjectFree = winpr_ObjectStringFree;
337 dvcman->plugins = ArrayList_New(TRUE);
338 if (!dvcman->plugins)
340 obj = ArrayList_Object(dvcman->plugins);
341 obj->fnObjectFree = dvcman_plugin_terminate;
342 return &dvcman->iface;
344 dvcman_free(plugin, &dvcman->iface);
353 static UINT dvcman_load_addin(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr,
356 WINPR_ASSERT(drdynvc);
357 WINPR_ASSERT(pChannelMgr);
359 WINPR_ASSERT(context);
361 WLog_Print(drdynvc->log, WLOG_INFO,
"Loading Dynamic Virtual Channel %s", args->argv[0]);
363 PVIRTUALCHANNELENTRY pvce =
364 freerdp_load_channel_addin_entry(args->argv[0], NULL, NULL, FREERDP_ADDIN_CHANNEL_DYNAMIC);
365 PDVC_PLUGIN_ENTRY pDVCPluginEntry = WINPR_FUNC_PTR_CAST(pvce, PDVC_PLUGIN_ENTRY);
371 entryPoints.iface.RegisterPlugin = dvcman_register_plugin;
372 entryPoints.iface.GetPlugin = dvcman_get_plugin;
373 entryPoints.iface.GetPluginData = dvcman_get_plugin_data;
374 entryPoints.iface.GetRdpSettings = dvcman_get_rdp_settings;
375 entryPoints.iface.GetRdpContext = dvcman_get_rdp_context;
376 entryPoints.dvcman = (
DVCMAN*)pChannelMgr;
377 entryPoints.args = args;
378 entryPoints.context = context;
379 return pDVCPluginEntry(&entryPoints.iface);
382 return ERROR_INVALID_FUNCTION;
390 if (channel->dvc_data)
391 Stream_Release(channel->dvc_data);
393 DeleteCriticalSection(&(channel->lock));
394 free(channel->channel_name);
400 WINPR_ASSERT(channel);
401 if (InterlockedDecrement(&channel->refCounter))
404 DVCMAN* dvcman = channel->dvcman;
406 HashTable_Remove(dvcman->channelsById, &channel->channel_id);
411 WINPR_ASSERT(channel);
412 DVCMAN* dvcman = channel->dvcman;
413 drdynvcPlugin* drdynvc = dvcman->drdynvc;
414 wStream* s = StreamPool_Take(dvcman->pool, 5);
418 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
419 return CHANNEL_RC_NO_MEMORY;
422 Stream_Write_UINT8(s, (CLOSE_REQUEST_PDU << 4) | 0x02);
423 Stream_Write_UINT32(s, channel->channel_id);
424 return drdynvc_send(drdynvc, s);
429 WINPR_ASSERT(channel);
431 IWTSVirtualChannelCallback* cb = channel->channel_callback;
432 const char* name = channel->channel_name;
433 const UINT32
id = channel->channel_id;
436 if (cb->OnOpen || cb->OnClose)
438 if (!cb->OnOpen || !cb->OnClose)
439 WLog_WARN(TAG,
"{%s:%" PRIu32
"} OnOpen=%p, OnClose=%p", name,
id, cb->OnOpen,
446 WINPR_ASSERT(channel);
449 IWTSVirtualChannelCallback* cb = channel->channel_callback;
452 check_open_close_receive(channel);
453 WINPR_ASSERT(cb->OnDataReceived);
454 return cb->OnDataReceived(cb, data);
457 static UINT dvcman_channel_close(
DVCMAN_CHANNEL* channel, BOOL perRequest, BOOL fromHashTableFn)
459 UINT error = CHANNEL_RC_OK;
460 DrdynvcClientContext* context = NULL;
462 WINPR_ASSERT(channel);
463 switch (channel->state)
465 case DVC_CHANNEL_INIT:
467 case DVC_CHANNEL_RUNNING:
470 drdynvcPlugin* drdynvc = channel->dvcman->drdynvc;
471 WINPR_ASSERT(drdynvc);
472 context = drdynvc->context;
474 WLog_Print(drdynvc->log, WLOG_DEBUG,
"sending close confirm for '%s'",
475 channel->channel_name);
477 error = dvcchannel_send_close(channel);
478 if (error != CHANNEL_RC_OK)
480 const char* msg =
"error when sending close confirm for '%s'";
482 msg =
"error when sending closeRequest for '%s'";
484 WLog_Print(drdynvc->log, WLOG_DEBUG, msg, channel->channel_name);
488 channel->state = DVC_CHANNEL_CLOSED;
490 IWTSVirtualChannelCallback* cb = channel->channel_callback;
493 check_open_close_receive(channel);
494 IFCALL(cb->OnClose, cb);
497 channel->channel_callback = NULL;
499 if (channel->dvcman && channel->dvcman->drdynvc)
503 IFCALLRET(context->OnChannelDisconnected, error, context, channel->channel_name,
504 channel->pInterface);
508 if (!fromHashTableFn)
509 dvcman_channel_unref(channel);
511 case DVC_CHANNEL_CLOSED:
521 IWTSVirtualChannelManager* pChannelMgr, UINT32 ChannelId,
522 const char* ChannelName)
526 WINPR_ASSERT(drdynvc);
527 WINPR_ASSERT(pChannelMgr);
533 channel->dvcman = (
DVCMAN*)pChannelMgr;
534 channel->channel_id = ChannelId;
535 channel->refCounter = 1;
536 channel->state = DVC_CHANNEL_INIT;
537 channel->channel_name = _strdup(ChannelName);
539 if (!channel->channel_name)
542 if (!InitializeCriticalSectionEx(&(channel->lock), 0, 0))
547 dvcman_channel_free(channel);
551 static void dvcman_clear(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr)
555 WINPR_ASSERT(dvcman);
556 WINPR_UNUSED(drdynvc);
558 HashTable_Clear(dvcman->channelsById);
559 ArrayList_Clear(dvcman->plugins);
560 ArrayList_Clear(dvcman->plugin_names);
561 HashTable_Clear(dvcman->listeners);
563 static void dvcman_free(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr)
567 WINPR_ASSERT(dvcman);
568 WINPR_UNUSED(drdynvc);
570 HashTable_Free(dvcman->channelsById);
571 ArrayList_Free(dvcman->plugins);
572 ArrayList_Free(dvcman->plugin_names);
573 HashTable_Free(dvcman->listeners);
575 StreamPool_Free(dvcman->pool);
584 static UINT dvcman_init(drdynvcPlugin* drdynvc, IWTSVirtualChannelManager* pChannelMgr)
587 UINT error = CHANNEL_RC_OK;
589 WINPR_ASSERT(dvcman);
590 ArrayList_Lock(dvcman->plugins);
591 for (
size_t i = 0; i < ArrayList_Count(dvcman->plugins); i++)
593 IWTSPlugin* pPlugin = ArrayList_GetItem(dvcman->plugins, i);
595 error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Initialize, pPlugin, pChannelMgr);
596 if (error != CHANNEL_RC_OK)
598 WLog_Print(drdynvc->log, WLOG_ERROR,
"Initialize failed with error %" PRIu32
"!",
605 ArrayList_Unlock(dvcman->plugins);
614 static UINT dvcman_write_channel(IWTSVirtualChannel* pChannel, ULONG cbSize,
const BYTE* pBuffer,
621 WINPR_UNUSED(pReserved);
622 if (!channel || !channel->dvcman)
623 return CHANNEL_RC_BAD_CHANNEL;
625 EnterCriticalSection(&(channel->lock));
627 drdynvc_write_data(channel->dvcman->drdynvc, channel->channel_id, pBuffer, cbSize, &close);
628 LeaveCriticalSection(&(channel->lock));
631 dvcman_channel_close(channel, FALSE, FALSE);
641 static UINT dvcman_close_channel_iface(IWTSVirtualChannel* pChannel)
646 return CHANNEL_RC_BAD_CHANNEL;
648 WLog_DBG(TAG,
"close_channel_iface: id=%" PRIu32
"", channel->channel_id);
649 return dvcman_channel_close(channel, FALSE, FALSE);
657 static DVCMAN_CHANNEL* dvcman_create_channel(drdynvcPlugin* drdynvc,
658 IWTSVirtualChannelManager* pChannelMgr,
659 UINT32 ChannelId,
const char* ChannelName, UINT* res)
663 DrdynvcClientContext* context = NULL;
666 IWTSVirtualChannelCallback* pCallback = NULL;
668 WINPR_ASSERT(dvcman);
671 HashTable_Lock(dvcman->listeners);
672 listener = (
DVCMAN_LISTENER*)HashTable_GetItemValue(dvcman->listeners, ChannelName);
675 *res = ERROR_NOT_FOUND;
679 channel = dvcman_get_channel_by_id(pChannelMgr, ChannelId, FALSE);
682 switch (channel->state)
684 case DVC_CHANNEL_RUNNING:
685 WLog_Print(drdynvc->log, WLOG_ERROR,
686 "Protocol error: Duplicated ChannelId %" PRIu32
" (%s)!", ChannelId,
688 *res = CHANNEL_RC_ALREADY_OPEN;
691 case DVC_CHANNEL_CLOSED:
692 case DVC_CHANNEL_INIT:
694 WLog_Print(drdynvc->log, WLOG_ERROR,
"not expecting a createChannel from state %d",
696 *res = CHANNEL_RC_INITIALIZATION_ERROR;
702 if (!(channel = dvcman_channel_new(drdynvc, pChannelMgr, ChannelId, ChannelName)))
704 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_channel_new failed!");
705 *res = CHANNEL_RC_NO_MEMORY;
710 if (!HashTable_Insert(dvcman->channelsById, &channel->channel_id, channel))
712 WLog_Print(drdynvc->log, WLOG_ERROR,
"unable to register channel in our channel list");
713 *res = ERROR_INTERNAL_ERROR;
714 dvcman_channel_free(channel);
719 channel->iface.Write = dvcman_write_channel;
720 channel->iface.Close = dvcman_close_channel_iface;
723 *res = listener->listener_callback->OnNewChannelConnection(
724 listener->listener_callback, &channel->iface, NULL, &bAccept, &pCallback);
726 if (*res != CHANNEL_RC_OK)
728 WLog_Print(drdynvc->log, WLOG_ERROR,
729 "OnNewChannelConnection failed with error %" PRIu32
"!", *res);
730 *res = ERROR_INTERNAL_ERROR;
731 dvcman_channel_unref(channel);
737 WLog_Print(drdynvc->log, WLOG_ERROR,
"OnNewChannelConnection returned with bAccept FALSE!");
738 *res = ERROR_INTERNAL_ERROR;
739 dvcman_channel_unref(channel);
744 WLog_Print(drdynvc->log, WLOG_DEBUG,
"listener %s created new channel %" PRIu32
"",
745 listener->channel_name, channel->channel_id);
746 channel->state = DVC_CHANNEL_RUNNING;
747 channel->channel_callback = pCallback;
748 channel->pInterface = listener->iface.pInterface;
749 context = dvcman->drdynvc->context;
751 IFCALLRET(context->OnChannelConnected, *res, context, ChannelName, listener->iface.pInterface);
752 if (*res != CHANNEL_RC_OK)
754 WLog_Print(drdynvc->log, WLOG_ERROR,
755 "context.OnChannelConnected failed with error %" PRIu32
"", *res);
759 HashTable_Unlock(dvcman->listeners);
769 static UINT dvcman_open_channel(drdynvcPlugin* drdynvc,
DVCMAN_CHANNEL* channel)
771 IWTSVirtualChannelCallback* pCallback = NULL;
772 UINT error = CHANNEL_RC_OK;
774 WINPR_ASSERT(drdynvc);
775 WINPR_ASSERT(channel);
776 if (channel->state == DVC_CHANNEL_RUNNING)
778 pCallback = channel->channel_callback;
780 if (pCallback->OnOpen)
782 check_open_close_receive(channel);
783 error = pCallback->OnOpen(pCallback);
786 WLog_Print(drdynvc->log, WLOG_ERROR,
"OnOpen failed with error %" PRIu32
"!",
792 WLog_Print(drdynvc->log, WLOG_DEBUG,
"open_channel: ChannelId %" PRIu32
"",
793 channel->channel_id);
805 static UINT dvcman_receive_channel_data_first(
DVCMAN_CHANNEL* channel, UINT32 length)
807 WINPR_ASSERT(channel);
808 WINPR_ASSERT(channel->dvcman);
809 if (channel->dvc_data)
810 Stream_Release(channel->dvc_data);
812 channel->dvc_data = StreamPool_Take(channel->dvcman->pool, length);
814 if (!channel->dvc_data)
816 drdynvcPlugin* drdynvc = channel->dvcman->drdynvc;
817 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
818 return CHANNEL_RC_NO_MEMORY;
821 channel->dvc_data_length = length;
822 return CHANNEL_RC_OK;
831 UINT32 ThreadingFlags)
833 UINT status = CHANNEL_RC_OK;
834 size_t dataSize = Stream_GetRemainingLength(data);
836 WINPR_ASSERT(channel);
837 WINPR_ASSERT(channel->dvcman);
838 if (channel->dvc_data)
840 drdynvcPlugin* drdynvc = channel->dvcman->drdynvc;
843 if (Stream_GetPosition(channel->dvc_data) + dataSize > channel->dvc_data_length)
845 WLog_Print(drdynvc->log, WLOG_ERROR,
"data exceeding declared length!");
846 Stream_Release(channel->dvc_data);
847 channel->dvc_data = NULL;
848 status = ERROR_INVALID_DATA;
852 Stream_Copy(data, channel->dvc_data, dataSize);
854 if (Stream_GetPosition(channel->dvc_data) >= channel->dvc_data_length)
856 Stream_SealLength(channel->dvc_data);
857 Stream_SetPosition(channel->dvc_data, 0);
859 status = dvcman_call_on_receive(channel, channel->dvc_data);
860 Stream_Release(channel->dvc_data);
861 channel->dvc_data = NULL;
865 status = dvcman_call_on_receive(channel, data);
871 static UINT8 drdynvc_write_variable_uint(
wStream* s, UINT32 val)
878 Stream_Write_UINT8(s, (UINT8)val);
880 else if (val <= 0xFFFF)
883 Stream_Write_UINT16(s, (UINT16)val);
888 Stream_Write_UINT32(s, val);
899 static UINT drdynvc_send(drdynvcPlugin* drdynvc,
wStream* s)
904 status = CHANNEL_RC_BAD_CHANNEL_HANDLE;
907 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelWriteEx);
908 status = drdynvc->channelEntryPoints.pVirtualChannelWriteEx(
909 drdynvc->InitHandle, drdynvc->OpenHandle, Stream_Buffer(s),
910 (UINT32)Stream_GetPosition(s), s);
916 return CHANNEL_RC_OK;
918 case CHANNEL_RC_NOT_CONNECTED:
920 return CHANNEL_RC_OK;
922 case CHANNEL_RC_BAD_CHANNEL_HANDLE:
924 WLog_ERR(TAG,
"VirtualChannelWriteEx failed with CHANNEL_RC_BAD_CHANNEL_HANDLE");
929 WLog_Print(drdynvc->log, WLOG_ERROR,
930 "VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
931 WTSErrorToString(status), status);
941 static UINT drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId,
const BYTE* data,
942 UINT32 dataSize, BOOL* close)
948 unsigned long chunkLength = 0;
949 UINT status = CHANNEL_RC_BAD_INIT_HANDLE;
953 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
955 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
956 WINPR_ASSERT(dvcman);
958 WLog_Print(drdynvc->log, WLOG_TRACE,
"write_data: ChannelId=%" PRIu32
" size=%" PRIu32
"",
959 ChannelId, dataSize);
960 data_out = StreamPool_Take(dvcman->pool, CHANNEL_CHUNK_LENGTH);
964 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
965 return CHANNEL_RC_NO_MEMORY;
968 Stream_SetPosition(data_out, 1);
969 cbChId = drdynvc_write_variable_uint(data_out, ChannelId);
970 pos = Stream_GetPosition(data_out);
976 Stream_Release(data_out);
978 else if (dataSize <= CHANNEL_CHUNK_LENGTH - pos)
980 Stream_SetPosition(data_out, 0);
981 Stream_Write_UINT8(data_out, (DATA_PDU << 4) | cbChId);
982 Stream_SetPosition(data_out, pos);
983 Stream_Write(data_out, data, dataSize);
984 status = drdynvc_send(drdynvc, data_out);
989 cbLen = drdynvc_write_variable_uint(data_out, dataSize);
990 pos = Stream_GetPosition(data_out);
991 Stream_SetPosition(data_out, 0);
993 const INT32 pdu = (DATA_FIRST_PDU << 4) | cbChId | (cbLen << 2);
994 Stream_Write_UINT8(data_out, WINPR_ASSERTING_INT_CAST(UINT8, pdu));
995 Stream_SetPosition(data_out, pos);
996 chunkLength = CHANNEL_CHUNK_LENGTH - pos;
997 Stream_Write(data_out, data, chunkLength);
999 dataSize -= chunkLength;
1000 status = drdynvc_send(drdynvc, data_out);
1002 while (status == CHANNEL_RC_OK && dataSize > 0)
1004 data_out = StreamPool_Take(dvcman->pool, CHANNEL_CHUNK_LENGTH);
1008 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
1009 return CHANNEL_RC_NO_MEMORY;
1012 Stream_SetPosition(data_out, 1);
1013 cbChId = drdynvc_write_variable_uint(data_out, ChannelId);
1014 pos = Stream_GetPosition(data_out);
1015 Stream_SetPosition(data_out, 0);
1016 Stream_Write_UINT8(data_out, (DATA_PDU << 4) | cbChId);
1017 Stream_SetPosition(data_out, pos);
1018 chunkLength = dataSize;
1020 if (chunkLength > CHANNEL_CHUNK_LENGTH - pos)
1021 chunkLength = CHANNEL_CHUNK_LENGTH - pos;
1023 Stream_Write(data_out, data, chunkLength);
1024 data += chunkLength;
1025 dataSize -= chunkLength;
1026 status = drdynvc_send(drdynvc, data_out);
1030 if (status != CHANNEL_RC_OK)
1032 WLog_Print(drdynvc->log, WLOG_ERROR,
"VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1033 WTSErrorToString(status), status);
1037 return CHANNEL_RC_OK;
1045 static UINT drdynvc_send_capability_response(drdynvcPlugin* drdynvc)
1052 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1054 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1055 WINPR_ASSERT(dvcman);
1057 WLog_Print(drdynvc->log, WLOG_TRACE,
"capability_response");
1058 s = StreamPool_Take(dvcman->pool, 4);
1062 WLog_Print(drdynvc->log, WLOG_ERROR,
"Stream_Ndrdynvc_write_variable_uintew failed!");
1063 return CHANNEL_RC_NO_MEMORY;
1066 Stream_Write_UINT16(s, 0x0050);
1067 Stream_Write_UINT16(s, drdynvc->version);
1068 status = drdynvc_send(drdynvc, s);
1070 if (status != CHANNEL_RC_OK)
1072 WLog_Print(drdynvc->log, WLOG_ERROR,
"VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1073 WTSErrorToString(status), status);
1084 static UINT drdynvc_process_capability_request(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
1090 return CHANNEL_RC_BAD_INIT_HANDLE;
1092 if (!Stream_CheckAndLogRequiredLength(TAG, s, 3))
1093 return ERROR_INVALID_DATA;
1095 WLog_Print(drdynvc->log, WLOG_TRACE,
"capability_request Sp=%d cbChId=%d", Sp, cbChId);
1097 Stream_Read_UINT16(s, drdynvc->version);
1102 if ((drdynvc->version == 2) || (drdynvc->version == 3))
1104 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1105 return ERROR_INVALID_DATA;
1107 Stream_Read_UINT16(s, drdynvc->PriorityCharge0);
1108 Stream_Read_UINT16(s, drdynvc->PriorityCharge1);
1109 Stream_Read_UINT16(s, drdynvc->PriorityCharge2);
1110 Stream_Read_UINT16(s, drdynvc->PriorityCharge3);
1113 status = drdynvc_send_capability_response(drdynvc);
1114 drdynvc->state = DRDYNVC_STATE_READY;
1118 static UINT32 drdynvc_cblen_to_bytes(
int cbLen)
1133 static UINT32 drdynvc_read_variable_uint(
wStream* s,
int cbLen)
1140 Stream_Read_UINT8(s, val);
1144 Stream_Read_UINT16(s, val);
1148 Stream_Read_UINT32(s, val);
1160 static UINT drdynvc_process_create_request(drdynvcPlugin* drdynvc, UINT8 Sp, UINT8 cbChId,
1165 UINT channel_status = 0;
1168 INT32 retStatus = 0;
1172 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1174 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1175 WINPR_ASSERT(dvcman);
1177 if (drdynvc->state == DRDYNVC_STATE_CAPABILITIES)
1184 drdynvc->version = 3;
1186 if ((status = drdynvc_send_capability_response(drdynvc)))
1188 WLog_Print(drdynvc->log, WLOG_ERROR,
"drdynvc_send_capability_response failed!");
1192 drdynvc->state = DRDYNVC_STATE_READY;
1195 if (!Stream_CheckAndLogRequiredLength(TAG, s, drdynvc_cblen_to_bytes(cbChId)))
1196 return ERROR_INVALID_DATA;
1198 const UINT32 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1199 const size_t pos = Stream_GetPosition(s);
1200 const char* name = Stream_ConstPointer(s);
1201 const size_t length = Stream_GetRemainingLength(s);
1203 if (strnlen(name, length) >= length)
1204 return ERROR_INVALID_DATA;
1206 WLog_Print(drdynvc->log, WLOG_DEBUG,
1207 "process_create_request: ChannelId=%" PRIu32
" ChannelName=%s", ChannelId, name);
1209 data_out = StreamPool_Take(dvcman->pool, pos + 4);
1212 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
1213 return CHANNEL_RC_NO_MEMORY;
1216 Stream_Write_UINT8(data_out, (CREATE_REQUEST_PDU << 4) | cbChId);
1217 Stream_SetPosition(s, 1);
1218 Stream_Copy(s, data_out, pos - 1);
1221 dvcman_create_channel(drdynvc, drdynvc->channel_mgr, ChannelId, name, &channel_status);
1222 switch (channel_status)
1225 WLog_Print(drdynvc->log, WLOG_DEBUG,
"channel created");
1228 case CHANNEL_RC_NO_MEMORY:
1229 WLog_Print(drdynvc->log, WLOG_DEBUG,
"not enough memory for channel creation");
1230 retStatus = STATUS_NO_MEMORY;
1232 case ERROR_NOT_FOUND:
1233 WLog_Print(drdynvc->log, WLOG_DEBUG,
"no listener for '%s'", name);
1234 retStatus = STATUS_NOT_FOUND;
1237 WLog_Print(drdynvc->log, WLOG_DEBUG,
"channel creation error");
1238 retStatus = STATUS_UNSUCCESSFUL;
1241 Stream_Write_INT32(data_out, retStatus);
1243 status = drdynvc_send(drdynvc, data_out);
1244 if (status != CHANNEL_RC_OK)
1246 WLog_Print(drdynvc->log, WLOG_ERROR,
"VirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1247 WTSErrorToString(status), status);
1248 dvcman_channel_unref(channel);
1252 if (channel_status == CHANNEL_RC_OK)
1254 if ((status = dvcman_open_channel(drdynvc, channel)))
1256 WLog_Print(drdynvc->log, WLOG_ERROR,
1257 "dvcman_open_channel failed with error %" PRIu32
"!", status);
1270 static UINT drdynvc_process_data_first(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
wStream* s,
1271 UINT32 ThreadingFlags)
1273 UINT status = CHANNEL_RC_OK;
1275 UINT32 ChannelId = 0;
1278 WINPR_ASSERT(drdynvc);
1279 if (!Stream_CheckAndLogRequiredLength(
1280 TAG, s, drdynvc_cblen_to_bytes(cbChId) + drdynvc_cblen_to_bytes(Sp)))
1281 return ERROR_INVALID_DATA;
1283 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1284 Length = drdynvc_read_variable_uint(s, Sp);
1285 WLog_Print(drdynvc->log, WLOG_TRACE,
1286 "process_data_first: Sp=%d cbChId=%d, ChannelId=%" PRIu32
" Length=%" PRIu32
"", Sp,
1287 cbChId, ChannelId, Length);
1289 channel = dvcman_get_channel_by_id(drdynvc->channel_mgr, ChannelId, TRUE);
1297 WLog_Print(drdynvc->log, WLOG_ERROR,
"ChannelId %" PRIu32
" not found!", ChannelId);
1298 return CHANNEL_RC_OK;
1301 if (channel->state != DVC_CHANNEL_RUNNING)
1304 status = dvcman_receive_channel_data_first(channel, Length);
1306 if (status == CHANNEL_RC_OK)
1307 status = dvcman_receive_channel_data(channel, s, ThreadingFlags);
1309 if (status != CHANNEL_RC_OK)
1310 status = dvcman_channel_close(channel, FALSE, FALSE);
1313 dvcman_channel_unref(channel);
1322 static UINT drdynvc_process_data(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
wStream* s,
1323 UINT32 ThreadingFlags)
1325 UINT32 ChannelId = 0;
1327 UINT status = CHANNEL_RC_OK;
1329 WINPR_ASSERT(drdynvc);
1330 if (!Stream_CheckAndLogRequiredLength(TAG, s, drdynvc_cblen_to_bytes(cbChId)))
1331 return ERROR_INVALID_DATA;
1333 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1334 WLog_Print(drdynvc->log, WLOG_TRACE,
"process_data: Sp=%d cbChId=%d, ChannelId=%" PRIu32
"", Sp,
1337 channel = dvcman_get_channel_by_id(drdynvc->channel_mgr, ChannelId, TRUE);
1345 WLog_Print(drdynvc->log, WLOG_ERROR,
"ChannelId %" PRIu32
" not found!", ChannelId);
1346 return CHANNEL_RC_OK;
1349 if (channel->state != DVC_CHANNEL_RUNNING)
1352 status = dvcman_receive_channel_data(channel, s, ThreadingFlags);
1353 if (status != CHANNEL_RC_OK)
1354 status = dvcman_channel_close(channel, FALSE, FALSE);
1357 dvcman_channel_unref(channel);
1366 static UINT drdynvc_process_close_request(drdynvcPlugin* drdynvc,
int Sp,
int cbChId,
wStream* s)
1368 UINT32 ChannelId = 0;
1371 WINPR_ASSERT(drdynvc);
1372 if (!Stream_CheckAndLogRequiredLength(TAG, s, drdynvc_cblen_to_bytes(cbChId)))
1373 return ERROR_INVALID_DATA;
1375 ChannelId = drdynvc_read_variable_uint(s, cbChId);
1376 WLog_Print(drdynvc->log, WLOG_DEBUG,
1377 "process_close_request: Sp=%d cbChId=%d, ChannelId=%" PRIu32
"", Sp, cbChId,
1380 channel = dvcman_get_channel_by_id(drdynvc->channel_mgr, ChannelId, TRUE);
1383 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_close_request channel %" PRIu32
" not present",
1385 return CHANNEL_RC_OK;
1388 dvcman_channel_close(channel, TRUE, FALSE);
1389 dvcman_channel_unref(channel);
1390 return CHANNEL_RC_OK;
1398 static UINT drdynvc_order_recv(drdynvcPlugin* drdynvc,
wStream* s, UINT32 ThreadingFlags)
1402 WINPR_ASSERT(drdynvc);
1403 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
1404 return ERROR_INVALID_DATA;
1406 Stream_Read_UINT8(s, value);
1407 const UINT8 Cmd = (value & 0xf0) >> 4;
1408 const UINT8 Sp = (value & 0x0c) >> 2;
1409 const UINT8 cbChId = (value & 0x03) >> 0;
1410 WLog_Print(drdynvc->log, WLOG_TRACE,
"order_recv: Cmd=%s, Sp=%" PRIu8
" cbChId=%" PRIu8,
1411 drdynvc_get_packet_type(Cmd), Sp, cbChId);
1415 case CAPABILITY_REQUEST_PDU:
1416 return drdynvc_process_capability_request(drdynvc, Sp, cbChId, s);
1418 case CREATE_REQUEST_PDU:
1419 return drdynvc_process_create_request(drdynvc, Sp, cbChId, s);
1421 case DATA_FIRST_PDU:
1422 return drdynvc_process_data_first(drdynvc, Sp, cbChId, s, ThreadingFlags);
1425 return drdynvc_process_data(drdynvc, Sp, cbChId, s, ThreadingFlags);
1427 case CLOSE_REQUEST_PDU:
1428 return drdynvc_process_close_request(drdynvc, Sp, cbChId, s);
1431 WLog_Print(drdynvc->log, WLOG_ERROR,
"unknown drdynvc cmd 0x%x", Cmd);
1432 return ERROR_INTERNAL_ERROR;
1441 static UINT drdynvc_virtual_channel_event_data_received(drdynvcPlugin* drdynvc,
void* pData,
1442 UINT32 dataLength, UINT32 totalLength,
1447 WINPR_ASSERT(drdynvc);
1448 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1450 return CHANNEL_RC_OK;
1453 if (dataFlags & CHANNEL_FLAG_FIRST)
1456 if (drdynvc->data_in)
1457 Stream_Release(drdynvc->data_in);
1459 drdynvc->data_in = StreamPool_Take(mgr->pool, totalLength);
1462 if (!(data_in = drdynvc->data_in))
1464 WLog_Print(drdynvc->log, WLOG_ERROR,
"StreamPool_Take failed!");
1465 return CHANNEL_RC_NO_MEMORY;
1468 if (!Stream_EnsureRemainingCapacity(data_in, dataLength))
1470 WLog_Print(drdynvc->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
1471 Stream_Release(drdynvc->data_in);
1472 drdynvc->data_in = NULL;
1473 return ERROR_INTERNAL_ERROR;
1476 Stream_Write(data_in, pData, dataLength);
1478 if (dataFlags & CHANNEL_FLAG_LAST)
1480 const size_t cap = Stream_Capacity(data_in);
1481 const size_t pos = Stream_GetPosition(data_in);
1484 WLog_Print(drdynvc->log, WLOG_ERROR,
"drdynvc_plugin_process_received: read error");
1485 return ERROR_INVALID_DATA;
1488 drdynvc->data_in = NULL;
1489 Stream_SealLength(data_in);
1490 Stream_SetPosition(data_in, 0);
1494 if (!MessageQueue_Post(drdynvc->queue, NULL, 0, (
void*)data_in, NULL))
1496 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_Post failed!");
1497 return ERROR_INTERNAL_ERROR;
1502 UINT error = drdynvc_order_recv(drdynvc, data_in, TRUE);
1503 Stream_Release(data_in);
1507 WLog_Print(drdynvc->log, WLOG_WARN,
1508 "drdynvc_order_recv failed with error %" PRIu32
"!", error);
1513 return CHANNEL_RC_OK;
1516 static void VCAPITYPE drdynvc_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1517 UINT event, LPVOID pData,
1518 UINT32 dataLength, UINT32 totalLength,
1521 UINT error = CHANNEL_RC_OK;
1522 drdynvcPlugin* drdynvc = (drdynvcPlugin*)lpUserParam;
1524 WINPR_ASSERT(drdynvc);
1527 case CHANNEL_EVENT_DATA_RECEIVED:
1528 if (!drdynvc || (drdynvc->OpenHandle != openHandle))
1530 WLog_ERR(TAG,
"drdynvc_virtual_channel_open_event: error no match");
1533 if ((error = drdynvc_virtual_channel_event_data_received(drdynvc, pData, dataLength,
1534 totalLength, dataFlags)))
1535 WLog_Print(drdynvc->log, WLOG_ERROR,
1536 "drdynvc_virtual_channel_event_data_received failed with error %" PRIu32
1542 case CHANNEL_EVENT_WRITE_CANCELLED:
1543 case CHANNEL_EVENT_WRITE_COMPLETE:
1550 case CHANNEL_EVENT_USER:
1556 if (error && drdynvc && drdynvc->rdpcontext)
1557 setChannelError(drdynvc->rdpcontext, error,
1558 "drdynvc_virtual_channel_open_event reported an error");
1561 static DWORD WINAPI drdynvc_virtual_channel_client_thread(LPVOID arg)
1565 wMessage message = { 0 };
1566 UINT error = CHANNEL_RC_OK;
1567 drdynvcPlugin* drdynvc = (drdynvcPlugin*)arg;
1571 ExitThread((DWORD)CHANNEL_RC_BAD_CHANNEL_HANDLE);
1572 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1577 if (!MessageQueue_Wait(drdynvc->queue))
1579 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_Wait failed!");
1580 error = ERROR_INTERNAL_ERROR;
1584 if (!MessageQueue_Peek(drdynvc->queue, &message, TRUE))
1586 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_Peek failed!");
1587 error = ERROR_INTERNAL_ERROR;
1591 if (message.id == WMQ_QUIT)
1594 if (message.id == 0)
1596 UINT32 ThreadingFlags = TRUE;
1597 data = (
wStream*)message.wParam;
1599 if ((error = drdynvc_order_recv(drdynvc, data, ThreadingFlags)))
1601 WLog_Print(drdynvc->log, WLOG_WARN,
1602 "drdynvc_order_recv failed with error %" PRIu32
"!", error);
1605 Stream_Release(data);
1615 HashTable_Clear(drdynvcMgr->channelsById);
1618 if (error && drdynvc->rdpcontext)
1619 setChannelError(drdynvc->rdpcontext, error,
1620 "drdynvc_virtual_channel_client_thread reported an error");
1622 ExitThread((DWORD)error);
1626 static void drdynvc_queue_object_free(
void* obj)
1629 wMessage* msg = (wMessage*)obj;
1631 if (!msg || (msg->id != 0))
1640 static UINT drdynvc_virtual_channel_event_initialized(drdynvcPlugin* drdynvc, LPVOID pData,
1644 WINPR_UNUSED(pData);
1645 WINPR_UNUSED(dataLength);
1650 drdynvc->queue = MessageQueue_New(NULL);
1652 if (!drdynvc->queue)
1654 WLog_Print(drdynvc->log, WLOG_ERROR,
"MessageQueue_New failed!");
1658 obj = MessageQueue_Object(drdynvc->queue);
1659 obj->fnObjectFree = drdynvc_queue_object_free;
1660 drdynvc->channel_mgr = dvcman_new(drdynvc);
1662 if (!drdynvc->channel_mgr)
1664 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_new failed!");
1668 return CHANNEL_RC_OK;
1670 return ERROR_INTERNAL_ERROR;
1678 static UINT drdynvc_virtual_channel_event_connected(drdynvcPlugin* drdynvc, LPVOID pData,
1683 rdpSettings* settings = NULL;
1685 WINPR_ASSERT(drdynvc);
1686 WINPR_UNUSED(pData);
1687 WINPR_UNUSED(dataLength);
1690 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1692 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelOpenEx);
1693 status = drdynvc->channelEntryPoints.pVirtualChannelOpenEx(
1694 drdynvc->InitHandle, &drdynvc->OpenHandle, drdynvc->channelDef.name,
1695 drdynvc_virtual_channel_open_event_ex);
1697 if (status != CHANNEL_RC_OK)
1699 WLog_Print(drdynvc->log, WLOG_ERROR,
"pVirtualChannelOpen failed with %s [%08" PRIX32
"]",
1700 WTSErrorToString(status), status);
1704 WINPR_ASSERT(drdynvc->rdpcontext);
1705 settings = drdynvc->rdpcontext->settings;
1706 WINPR_ASSERT(settings);
1708 for (UINT32 index = 0;
1712 freerdp_settings_get_pointer_array(settings, FreeRDP_DynamicChannelArray, index);
1713 error = dvcman_load_addin(drdynvc, drdynvc->channel_mgr, args, drdynvc->rdpcontext);
1715 if (CHANNEL_RC_OK != error)
1719 if ((error = dvcman_init(drdynvc, drdynvc->channel_mgr)))
1721 WLog_Print(drdynvc->log, WLOG_ERROR,
"dvcman_init failed with error %" PRIu32
"!", error);
1725 drdynvc->state = DRDYNVC_STATE_CAPABILITIES;
1729 if (!(drdynvc->thread = CreateThread(NULL, 0, drdynvc_virtual_channel_client_thread,
1730 (
void*)drdynvc, 0, NULL)))
1732 error = ERROR_INTERNAL_ERROR;
1733 WLog_Print(drdynvc->log, WLOG_ERROR,
"CreateThread failed!");
1737 if (!SetThreadPriority(drdynvc->thread, THREAD_PRIORITY_HIGHEST))
1738 WLog_Print(drdynvc->log, WLOG_WARN,
"SetThreadPriority failed, ignoring.");
1750 static UINT drdynvc_virtual_channel_event_disconnected(drdynvcPlugin* drdynvc)
1755 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1757 if (drdynvc->OpenHandle == 0)
1758 return CHANNEL_RC_OK;
1762 if (!MessageQueue_PostQuit(drdynvc->queue, 0))
1764 status = GetLastError();
1765 WLog_Print(drdynvc->log, WLOG_ERROR,
1766 "MessageQueue_PostQuit failed with error %" PRIu32
"", status);
1771 if (drdynvc->thread)
1773 if (WaitForSingleObject(drdynvc->thread, INFINITE) != WAIT_OBJECT_0)
1775 status = GetLastError();
1776 WLog_Print(drdynvc->log, WLOG_ERROR,
1777 "WaitForSingleObject failed with error %" PRIu32
"", status);
1781 (void)CloseHandle(drdynvc->thread);
1782 drdynvc->thread = NULL;
1792 HashTable_Clear(drdynvcMgr->channelsById);
1796 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelCloseEx);
1797 status = drdynvc->channelEntryPoints.pVirtualChannelCloseEx(drdynvc->InitHandle,
1798 drdynvc->OpenHandle);
1800 if (status != CHANNEL_RC_OK)
1802 WLog_Print(drdynvc->log, WLOG_ERROR,
"pVirtualChannelClose failed with %s [%08" PRIX32
"]",
1803 WTSErrorToString(status), status);
1806 dvcman_clear(drdynvc, drdynvc->channel_mgr);
1808 MessageQueue_Clear(drdynvc->queue);
1809 drdynvc->OpenHandle = 0;
1811 if (drdynvc->data_in)
1813 Stream_Release(drdynvc->data_in);
1814 drdynvc->data_in = NULL;
1825 static UINT drdynvc_virtual_channel_event_terminated(drdynvcPlugin* drdynvc)
1828 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1830 MessageQueue_Free(drdynvc->queue);
1831 drdynvc->queue = NULL;
1833 if (drdynvc->channel_mgr)
1835 dvcman_free(drdynvc, drdynvc->channel_mgr);
1836 drdynvc->channel_mgr = NULL;
1838 drdynvc->InitHandle = 0;
1839 free(drdynvc->context);
1841 return CHANNEL_RC_OK;
1844 static UINT drdynvc_virtual_channel_event_attached(drdynvcPlugin* drdynvc)
1846 UINT error = CHANNEL_RC_OK;
1850 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1852 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1855 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1857 ArrayList_Lock(dvcman->plugins);
1858 for (
size_t i = 0; i < ArrayList_Count(dvcman->plugins); i++)
1860 IWTSPlugin* pPlugin = ArrayList_GetItem(dvcman->plugins, i);
1862 error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Attached, pPlugin);
1863 if (error != CHANNEL_RC_OK)
1865 WLog_Print(drdynvc->log, WLOG_ERROR,
"Attach failed with error %" PRIu32
"!", error);
1871 ArrayList_Unlock(dvcman->plugins);
1875 static UINT drdynvc_virtual_channel_event_detached(drdynvcPlugin* drdynvc)
1877 UINT error = CHANNEL_RC_OK;
1881 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1883 dvcman = (
DVCMAN*)drdynvc->channel_mgr;
1886 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
1888 ArrayList_Lock(dvcman->plugins);
1889 for (
size_t i = 0; i < ArrayList_Count(dvcman->plugins); i++)
1891 IWTSPlugin* pPlugin = ArrayList_GetItem(dvcman->plugins, i);
1893 error = IFCALLRESULT(CHANNEL_RC_OK, pPlugin->Detached, pPlugin);
1894 if (error != CHANNEL_RC_OK)
1896 WLog_Print(drdynvc->log, WLOG_ERROR,
"Detach failed with error %" PRIu32
"!", error);
1902 ArrayList_Unlock(dvcman->plugins);
1907 static VOID VCAPITYPE drdynvc_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1908 UINT event, LPVOID pData,
1911 UINT error = CHANNEL_RC_OK;
1912 drdynvcPlugin* drdynvc = (drdynvcPlugin*)lpUserParam;
1914 if (!drdynvc || (drdynvc->InitHandle != pInitHandle))
1916 WLog_ERR(TAG,
"drdynvc_virtual_channel_init_event: error no match");
1922 case CHANNEL_EVENT_INITIALIZED:
1923 error = drdynvc_virtual_channel_event_initialized(drdynvc, pData, dataLength);
1925 case CHANNEL_EVENT_CONNECTED:
1926 if ((error = drdynvc_virtual_channel_event_connected(drdynvc, pData, dataLength)))
1927 WLog_Print(drdynvc->log, WLOG_ERROR,
1928 "drdynvc_virtual_channel_event_connected failed with error %" PRIu32
"",
1933 case CHANNEL_EVENT_DISCONNECTED:
1934 if ((error = drdynvc_virtual_channel_event_disconnected(drdynvc)))
1935 WLog_Print(drdynvc->log, WLOG_ERROR,
1936 "drdynvc_virtual_channel_event_disconnected failed with error %" PRIu32
1942 case CHANNEL_EVENT_TERMINATED:
1943 if ((error = drdynvc_virtual_channel_event_terminated(drdynvc)))
1944 WLog_Print(drdynvc->log, WLOG_ERROR,
1945 "drdynvc_virtual_channel_event_terminated failed with error %" PRIu32
"",
1950 case CHANNEL_EVENT_ATTACHED:
1951 if ((error = drdynvc_virtual_channel_event_attached(drdynvc)))
1952 WLog_Print(drdynvc->log, WLOG_ERROR,
1953 "drdynvc_virtual_channel_event_attached failed with error %" PRIu32
"",
1958 case CHANNEL_EVENT_DETACHED:
1959 if ((error = drdynvc_virtual_channel_event_detached(drdynvc)))
1960 WLog_Print(drdynvc->log, WLOG_ERROR,
1961 "drdynvc_virtual_channel_event_detached failed with error %" PRIu32
"",
1970 if (error && drdynvc->rdpcontext)
1971 setChannelError(drdynvc->rdpcontext, error,
1972 "drdynvc_virtual_channel_init_event_ex reported an error");
1979 static int drdynvc_get_version(DrdynvcClientContext* context)
1981 WINPR_ASSERT(context);
1982 drdynvcPlugin* drdynvc = (drdynvcPlugin*)context->handle;
1983 WINPR_ASSERT(drdynvc);
1984 return drdynvc->version;
1988 #define VirtualChannelEntryEx drdynvc_VirtualChannelEntryEx
1990 FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints,
1994 drdynvcPlugin* drdynvc = NULL;
1995 DrdynvcClientContext* context = NULL;
1997 drdynvc = (drdynvcPlugin*)calloc(1,
sizeof(drdynvcPlugin));
1999 WINPR_ASSERT(pEntryPoints);
2002 WLog_ERR(TAG,
"calloc failed!");
2006 drdynvc->channelDef.options =
2007 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP | CHANNEL_OPTION_COMPRESS_RDP;
2008 (void)sprintf_s(drdynvc->channelDef.name, ARRAYSIZE(drdynvc->channelDef.name),
2009 DRDYNVC_SVC_CHANNEL_NAME);
2010 drdynvc->state = DRDYNVC_STATE_INITIAL;
2014 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
2016 context = (DrdynvcClientContext*)calloc(1,
sizeof(DrdynvcClientContext));
2020 WLog_Print(drdynvc->log, WLOG_ERROR,
"calloc failed!");
2025 context->handle = (
void*)drdynvc;
2026 context->custom = NULL;
2027 drdynvc->context = context;
2028 context->GetVersion = drdynvc_get_version;
2029 drdynvc->rdpcontext = pEntryPointsEx->context;
2031 FreeRDP_TransportDumpReplay) &&
2033 FreeRDP_SynchronousDynamicChannels))
2034 drdynvc->async = TRUE;
2037 drdynvc->log = WLog_Get(TAG);
2038 WLog_Print(drdynvc->log, WLOG_DEBUG,
"VirtualChannelEntryEx");
2039 CopyMemory(&(drdynvc->channelEntryPoints), pEntryPoints,
2041 drdynvc->InitHandle = pInitHandle;
2043 WINPR_ASSERT(drdynvc->channelEntryPoints.pVirtualChannelInitEx);
2044 rc = drdynvc->channelEntryPoints.pVirtualChannelInitEx(
2045 drdynvc, context, pInitHandle, &drdynvc->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
2046 drdynvc_virtual_channel_init_event_ex);
2048 if (CHANNEL_RC_OK != rc)
2050 WLog_Print(drdynvc->log, WLOG_ERROR,
"pVirtualChannelInit failed with %s [%08" PRIX32
"]",
2051 WTSErrorToString(rc), rc);
2052 free(drdynvc->context);
2057 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.