23#include <freerdp/config.h>
25#include <winpr/wtypes.h>
26#include <winpr/assert.h>
28#include <winpr/print.h>
29#include <winpr/clipboard.h>
31#include <freerdp/types.h>
32#include <freerdp/constants.h>
33#include <freerdp/freerdp.h>
34#include <freerdp/client/cliprdr.h>
36#include "../../../channels/client/addin.h"
38#include "cliprdr_main.h"
39#include "cliprdr_format.h"
40#include "../cliprdr_common.h"
42const char type_FileGroupDescriptorW[] =
"FileGroupDescriptorW";
43const char type_FileContents[] =
"FileContents";
45CliprdrClientContext* cliprdr_get_client_interface(
cliprdrPlugin* cliprdr)
47 CliprdrClientContext* pInterface =
nullptr;
52 pInterface = (CliprdrClientContext*)cliprdr->channelEntryPoints.pInterface;
63 UINT status = ERROR_INVALID_DATA;
65 WINPR_ASSERT(cliprdr);
68 const size_t pos = Stream_GetPosition(s);
69 WINPR_ASSERT(pos >= 8ULL);
70 WINPR_ASSERT(pos <= UINT32_MAX - 8);
72 const uint32_t dataLen = WINPR_ASSERTING_INT_CAST(uint32_t, pos - 8UL);
74 if (!Stream_SetPosition(s, 4))
76 Stream_Write_UINT32(s, dataLen);
77 if (!Stream_SetPosition(s, pos))
80 WLog_Print(cliprdr->log, WLOG_DEBUG,
"Cliprdr Sending (%" PRIuz
" bytes)", pos);
83 status = CHANNEL_RC_BAD_INIT_HANDLE;
86 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelWriteEx);
87 status = cliprdr->channelEntryPoints.pVirtualChannelWriteEx(
88 cliprdr->InitHandle, cliprdr->OpenHandle, Stream_Buffer(s),
89 (UINT32)Stream_GetPosition(s), s);
93 if (status != CHANNEL_RC_OK)
96 WLog_Print(cliprdr->log, WLOG_ERROR,
"VirtualChannelWrite failed with %s [%08" PRIX32
"]",
97 WTSErrorToString(status), status);
103UINT cliprdr_send_error_response(
cliprdrPlugin* cliprdr, UINT16 type)
105 wStream* s = cliprdr_packet_new(type, CB_RESPONSE_FAIL, 0);
108 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
109 return ERROR_OUTOFMEMORY;
112 return cliprdr_packet_send(cliprdr, s);
115static void cliprdr_print_general_capability_flags(wLog* log, UINT32 flags)
117 WLog_Print(log, WLOG_DEBUG,
"generalFlags (0x%08" PRIX32
") {", flags);
119 if (flags & CB_USE_LONG_FORMAT_NAMES)
120 WLog_Print(log, WLOG_DEBUG,
"\tCB_USE_LONG_FORMAT_NAMES");
122 if (flags & CB_STREAM_FILECLIP_ENABLED)
123 WLog_Print(log, WLOG_DEBUG,
"\tCB_STREAM_FILECLIP_ENABLED");
125 if (flags & CB_FILECLIP_NO_FILE_PATHS)
126 WLog_Print(log, WLOG_DEBUG,
"\tCB_FILECLIP_NO_FILE_PATHS");
128 if (flags & CB_CAN_LOCK_CLIPDATA)
129 WLog_Print(log, WLOG_DEBUG,
"\tCB_CAN_LOCK_CLIPDATA");
131 if (flags & CB_HUGE_FILE_SUPPORT_ENABLED)
132 WLog_Print(log, WLOG_DEBUG,
"\tCB_HUGE_FILE_SUPPORT_ENABLED");
134 WLog_Print(log, WLOG_DEBUG,
"}");
145 UINT32 generalFlags = 0;
148 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
149 UINT error = CHANNEL_RC_OK;
151 WINPR_ASSERT(cliprdr);
156 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_get_client_interface failed!");
157 return ERROR_INTERNAL_ERROR;
160 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 8))
161 return ERROR_INVALID_DATA;
163 Stream_Read_UINT32(s, version);
164 Stream_Read_UINT32(s, generalFlags);
165 WLog_Print(cliprdr->log, WLOG_DEBUG,
"Version: %" PRIu32
"", version);
167 cliprdr_print_general_capability_flags(cliprdr->log, generalFlags);
169 cliprdr->useLongFormatNames = (generalFlags & CB_USE_LONG_FORMAT_NAMES) != 0;
170 cliprdr->streamFileClipEnabled = (generalFlags & CB_STREAM_FILECLIP_ENABLED) != 0;
171 cliprdr->fileClipNoFilePaths = (generalFlags & CB_FILECLIP_NO_FILE_PATHS) != 0;
172 cliprdr->canLockClipData = (generalFlags & CB_CAN_LOCK_CLIPDATA) != 0;
173 cliprdr->hasHugeFileSupport = (generalFlags & CB_HUGE_FILE_SUPPORT_ENABLED) != 0;
174 cliprdr->capabilitiesReceived = TRUE;
176 capabilities.common.msgType = CB_CLIP_CAPS;
177 capabilities.cCapabilitiesSets = 1;
179 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
180 generalCapabilitySet.capabilitySetLength = 12;
181 generalCapabilitySet.version = version;
182 generalCapabilitySet.generalFlags = generalFlags;
183 IFCALLRET(context->ServerCapabilities, error, context, &capabilities);
186 WLog_Print(cliprdr->log, WLOG_ERROR,
"ServerCapabilities failed with error %" PRIu32
"!",
198 WINPR_ATTR_UNUSED UINT32 length,
199 WINPR_ATTR_UNUSED UINT16 flags)
201 UINT16 lengthCapability = 0;
202 UINT16 cCapabilitiesSets = 0;
203 UINT16 capabilitySetType = 0;
204 UINT error = CHANNEL_RC_OK;
206 WINPR_ASSERT(cliprdr);
209 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 4))
210 return ERROR_INVALID_DATA;
212 Stream_Read_UINT16(s, cCapabilitiesSets);
213 Stream_Seek_UINT16(s);
214 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerCapabilities");
216 for (UINT16 index = 0; index < cCapabilitiesSets; index++)
218 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 4))
219 return ERROR_INVALID_DATA;
221 Stream_Read_UINT16(s, capabilitySetType);
222 Stream_Read_UINT16(s, lengthCapability);
224 if ((lengthCapability < 4) ||
225 (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, lengthCapability - 4U)))
226 return ERROR_INVALID_DATA;
228 switch (capabilitySetType)
230 case CB_CAPSTYPE_GENERAL:
231 if ((error = cliprdr_process_general_capability(cliprdr, s)))
233 WLog_Print(cliprdr->log, WLOG_ERROR,
234 "cliprdr_process_general_capability failed with error %" PRIu32
"!",
242 WLog_Print(cliprdr->log, WLOG_ERROR,
"unknown cliprdr capability set: %" PRIu16
"",
244 return CHANNEL_RC_BAD_PROC;
257 UINT32 length, UINT16 flags)
260 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
261 UINT error = CHANNEL_RC_OK;
263 WINPR_ASSERT(cliprdr);
266 WLog_Print(cliprdr->log, WLOG_DEBUG,
"MonitorReady");
268 if (!cliprdr->capabilitiesReceived)
276 cliprdr->useLongFormatNames = FALSE;
277 cliprdr->streamFileClipEnabled = FALSE;
278 cliprdr->fileClipNoFilePaths = TRUE;
279 cliprdr->canLockClipData = FALSE;
282 monitorReady.common.msgType = CB_MONITOR_READY;
283 monitorReady.common.msgFlags = flags;
284 monitorReady.common.dataLen = length;
285 IFCALLRET(context->MonitorReady, error, context, &monitorReady);
288 WLog_Print(cliprdr->log, WLOG_ERROR,
"MonitorReady failed with error %" PRIu32
"!", error);
298static UINT cliprdr_process_filecontents_request(
cliprdrPlugin* cliprdr,
wStream* s, UINT32 length,
302 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
303 UINT error = CHANNEL_RC_OK;
305 WINPR_ASSERT(cliprdr);
308 WLog_Print(cliprdr->log, WLOG_DEBUG,
"FileContentsRequest");
310 request.common.msgType = CB_FILECONTENTS_REQUEST;
311 request.common.msgFlags = flags;
312 request.common.dataLen = length;
314 if ((error = cliprdr_read_file_contents_request(s, &request)))
319 if ((mask & (CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES)) == 0)
321 WLog_Print(cliprdr->log, WLOG_WARN,
"local -> remote file copy disabled, ignoring request");
322 return cliprdr_send_error_response(cliprdr, CB_FILECONTENTS_RESPONSE);
324 IFCALLRET(context->ServerFileContentsRequest, error, context, &request);
327 WLog_Print(cliprdr->log, WLOG_ERROR,
328 "ServerFileContentsRequest failed with error %" PRIu32
"!", error);
338static UINT cliprdr_process_filecontents_response(
cliprdrPlugin* cliprdr,
wStream* s, UINT32 length,
342 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
343 UINT error = CHANNEL_RC_OK;
345 WINPR_ASSERT(cliprdr);
348 WLog_Print(cliprdr->log, WLOG_DEBUG,
"FileContentsResponse");
350 response.common.msgType = CB_FILECONTENTS_RESPONSE;
351 response.common.msgFlags = flags;
352 response.common.dataLen = length;
354 if ((error = cliprdr_read_file_contents_response(s, &response)))
357 IFCALLRET(context->ServerFileContentsResponse, error, context, &response);
360 WLog_Print(cliprdr->log, WLOG_ERROR,
361 "ServerFileContentsResponse failed with error %" PRIu32
"!", error);
375 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
376 UINT error = CHANNEL_RC_OK;
378 WINPR_ASSERT(cliprdr);
381 WLog_Print(cliprdr->log, WLOG_DEBUG,
"LockClipData");
383 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 4))
384 return ERROR_INVALID_DATA;
386 lockClipboardData.common.msgType = CB_LOCK_CLIPDATA;
387 lockClipboardData.common.msgFlags = flags;
388 lockClipboardData.common.dataLen = length;
389 Stream_Read_UINT32(s, lockClipboardData.clipDataId);
390 IFCALLRET(context->ServerLockClipboardData, error, context, &lockClipboardData);
393 WLog_Print(cliprdr->log, WLOG_ERROR,
394 "ServerLockClipboardData failed with error %" PRIu32
"!", error);
408 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
409 UINT error = CHANNEL_RC_OK;
411 WINPR_ASSERT(cliprdr);
414 WLog_Print(cliprdr->log, WLOG_DEBUG,
"UnlockClipData");
416 if ((error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData)))
419 unlockClipboardData.common.msgType = CB_UNLOCK_CLIPDATA;
420 unlockClipboardData.common.msgFlags = flags;
421 unlockClipboardData.common.dataLen = length;
423 IFCALLRET(context->ServerUnlockClipboardData, error, context, &unlockClipboardData);
426 WLog_Print(cliprdr->log, WLOG_ERROR,
427 "ServerUnlockClipboardData failed with error %" PRIu32
"!", error);
437static UINT cliprdr_order_recv(LPVOID userdata,
wStream* s)
445 WINPR_ASSERT(cliprdr);
448 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 8))
449 return ERROR_INVALID_DATA;
451 Stream_Read_UINT16(s, msgType);
452 Stream_Read_UINT16(s, msgFlags);
453 Stream_Read_UINT32(s, dataLen);
455 const BOOL failedResponse =
456 (msgFlags & CB_RESPONSE_FAIL) &&
457 ((msgType == CB_FORMAT_DATA_RESPONSE) || (msgType == CB_FORMAT_LIST_RESPONSE) ||
458 (msgType == CB_FILECONTENTS_RESPONSE));
468 const size_t available = Stream_GetRemainingLength(s);
469 if (dataLen > available)
471 WLog_Print(cliprdr->log, WLOG_WARN,
472 "failed response (msgType: %" PRIu16
") claims dataLen: %" PRIu32
473 " but only %" PRIuz
" bytes follow",
474 msgType, dataLen, available);
475 dataLen = WINPR_ASSERTING_INT_CAST(UINT32, available);
478 else if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, dataLen))
479 return ERROR_INVALID_DATA;
481 char buffer1[64] = WINPR_C_ARRAY_INIT;
482 char buffer2[64] = WINPR_C_ARRAY_INIT;
483 WLog_Print(cliprdr->log, WLOG_DEBUG,
484 "msgType: %s (%" PRIu16
"), msgFlags: %s dataLen: %" PRIu32
"",
485 CB_MSG_TYPE_STRING(msgType, buffer1,
sizeof(buffer1)), msgType,
486 CB_MSG_FLAGS_STRING(msgFlags, buffer2,
sizeof(buffer2)), dataLen);
491 if ((error = cliprdr_process_clip_caps(cliprdr, s, dataLen, msgFlags)))
492 WLog_Print(cliprdr->log, WLOG_ERROR,
493 "cliprdr_process_clip_caps failed with error %" PRIu32
"!", error);
497 case CB_MONITOR_READY:
498 if ((error = cliprdr_process_monitor_ready(cliprdr, s, dataLen, msgFlags)))
499 WLog_Print(cliprdr->log, WLOG_ERROR,
500 "cliprdr_process_monitor_ready failed with error %" PRIu32
"!", error);
505 if ((error = cliprdr_process_format_list(cliprdr, s, dataLen, msgFlags)))
506 WLog_Print(cliprdr->log, WLOG_ERROR,
507 "cliprdr_process_format_list failed with error %" PRIu32
"!", error);
511 case CB_FORMAT_LIST_RESPONSE:
512 if ((error = cliprdr_process_format_list_response(cliprdr, s, dataLen, msgFlags)))
513 WLog_Print(cliprdr->log, WLOG_ERROR,
514 "cliprdr_process_format_list_response failed with error %" PRIu32
"!",
519 case CB_FORMAT_DATA_REQUEST:
520 if ((error = cliprdr_process_format_data_request(cliprdr, s, dataLen, msgFlags)))
521 WLog_Print(cliprdr->log, WLOG_ERROR,
522 "cliprdr_process_format_data_request failed with error %" PRIu32
"!",
527 case CB_FORMAT_DATA_RESPONSE:
528 if ((error = cliprdr_process_format_data_response(cliprdr, s, dataLen, msgFlags)))
529 WLog_Print(cliprdr->log, WLOG_ERROR,
530 "cliprdr_process_format_data_response failed with error %" PRIu32
"!",
535 case CB_FILECONTENTS_REQUEST:
536 if ((error = cliprdr_process_filecontents_request(cliprdr, s, dataLen, msgFlags)))
537 WLog_Print(cliprdr->log, WLOG_ERROR,
538 "cliprdr_process_filecontents_request failed with error %" PRIu32
"!",
543 case CB_FILECONTENTS_RESPONSE:
544 if ((error = cliprdr_process_filecontents_response(cliprdr, s, dataLen, msgFlags)))
545 WLog_Print(cliprdr->log, WLOG_ERROR,
546 "cliprdr_process_filecontents_response failed with error %" PRIu32
"!",
551 case CB_LOCK_CLIPDATA:
552 if ((error = cliprdr_process_lock_clipdata(cliprdr, s, dataLen, msgFlags)))
553 WLog_Print(cliprdr->log, WLOG_ERROR,
554 "cliprdr_process_lock_clipdata failed with error %" PRIu32
"!", error);
558 case CB_UNLOCK_CLIPDATA:
559 if ((error = cliprdr_process_unlock_clipdata(cliprdr, s, dataLen, msgFlags)))
560 WLog_Print(cliprdr->log, WLOG_ERROR,
561 "cliprdr_process_unlock_clipdata failed with error %" PRIu32
"!", error);
566 error = CHANNEL_RC_BAD_PROC;
567 WLog_Print(cliprdr->log, WLOG_ERROR,
"unknown msgType %" PRIu16
"", msgType);
571 Stream_Free(s, TRUE);
584static UINT cliprdr_client_capabilities(CliprdrClientContext* context,
592 WINPR_ASSERT(context);
595 WINPR_ASSERT(cliprdr);
597 s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
601 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
602 return ERROR_INTERNAL_ERROR;
605 Stream_Write_UINT16(s, 1);
606 Stream_Write_UINT16(s, 0);
608 capabilities->capabilitySets);
609 Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetType);
610 Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetLength);
611 Stream_Write_UINT32(s, generalCapabilitySet->version);
612 flags = generalCapabilitySet->generalFlags;
618 if (!cliprdr->useLongFormatNames)
619 flags &= (uint32_t)~CB_USE_LONG_FORMAT_NAMES;
620 if (!cliprdr->streamFileClipEnabled)
621 flags &= (uint32_t)~CB_STREAM_FILECLIP_ENABLED;
622 if (!cliprdr->fileClipNoFilePaths)
623 flags &= (uint32_t)~CB_FILECLIP_NO_FILE_PATHS;
624 if (!cliprdr->canLockClipData)
625 flags &= (uint32_t)~CB_CAN_LOCK_CLIPDATA;
626 if (!cliprdr->hasHugeFileSupport)
627 flags &= (uint32_t)~CB_HUGE_FILE_SUPPORT_ENABLED;
629 cliprdr->useLongFormatNames = (flags & CB_USE_LONG_FORMAT_NAMES) != 0;
630 cliprdr->streamFileClipEnabled = (flags & CB_STREAM_FILECLIP_ENABLED) != 0;
631 cliprdr->fileClipNoFilePaths = (flags & CB_FILECLIP_NO_FILE_PATHS) != 0;
632 cliprdr->canLockClipData = (flags & CB_CAN_LOCK_CLIPDATA) != 0;
633 cliprdr->hasHugeFileSupport = (flags & CB_HUGE_FILE_SUPPORT_ENABLED) != 0;
635 Stream_Write_UINT32(s, flags);
636 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientCapabilities");
638 return cliprdr_packet_send(cliprdr, s);
646static UINT cliprdr_temp_directory(CliprdrClientContext* context,
652 WINPR_ASSERT(context);
653 WINPR_ASSERT(tempDirectory);
656 WINPR_ASSERT(cliprdr);
658 const size_t tmpDirCharLen =
sizeof(tempDirectory->szTempDir) /
sizeof(WCHAR);
659 s = cliprdr_packet_new(CB_TEMP_DIRECTORY, 0, tmpDirCharLen *
sizeof(WCHAR));
663 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
664 return ERROR_INTERNAL_ERROR;
667 if (Stream_Write_UTF16_String_From_UTF8(s, tmpDirCharLen - 1, tempDirectory->szTempDir,
668 ARRAYSIZE(tempDirectory->szTempDir), TRUE) < 0)
670 Stream_Free(s, TRUE);
671 return ERROR_INTERNAL_ERROR;
675 Stream_Write_UINT16(s, 0);
677 WLog_Print(cliprdr->log, WLOG_DEBUG,
"TempDirectory: %s", tempDirectory->szTempDir);
678 return cliprdr_packet_send(cliprdr, s);
686static UINT cliprdr_client_format_list(CliprdrClientContext* context,
692 WINPR_ASSERT(context);
693 WINPR_ASSERT(formatList);
696 WINPR_ASSERT(cliprdr);
699 const UINT32 mask = CB_RESPONSE_OK | CB_RESPONSE_FAIL;
700 if ((formatList->common.msgFlags & mask) != 0)
701 WLog_Print(cliprdr->log, WLOG_WARN,
702 "Sending clipboard request with invalid flags msgFlags = 0x%08" PRIx32
703 ". Correct in your client!",
704 formatList->common.msgFlags & mask);
710 formatList, mask, CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES);
712 const uint32_t level = WLOG_DEBUG;
713 if (WLog_IsLevelActive(cliprdr->log, level))
715 WLog_Print(cliprdr->log, level,
"ClientFormatList: numFormats: %" PRIu32
"",
716 formatList->numFormats);
717 for (
size_t x = 0; x < filterList.numFormats; x++)
720 WLog_Print(cliprdr->log, level,
"[%" PRIuz
"]: id=0x%08" PRIx32
" [%s|%s]", x,
721 format->formatId, ClipboardGetFormatIdString(format->formatId),
726 s = cliprdr_packet_format_list_new(&filterList, cliprdr->useLongFormatNames, FALSE);
727 cliprdr_free_format_list(&filterList);
731 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_format_list_new failed!");
732 return ERROR_INTERNAL_ERROR;
735 return cliprdr_packet_send(cliprdr, s);
744cliprdr_client_format_list_response(CliprdrClientContext* context,
750 WINPR_ASSERT(context);
751 WINPR_ASSERT(formatListResponse);
754 WINPR_ASSERT(cliprdr);
756 s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags, 0);
760 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
761 return ERROR_INTERNAL_ERROR;
764 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatListResponse");
765 return cliprdr_packet_send(cliprdr, s);
773static UINT cliprdr_client_lock_clipboard_data(CliprdrClientContext* context,
779 WINPR_ASSERT(context);
780 WINPR_ASSERT(lockClipboardData);
783 WINPR_ASSERT(cliprdr);
785 s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
789 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_lock_clipdata_new failed!");
790 return ERROR_INTERNAL_ERROR;
793 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientLockClipboardData: clipDataId: 0x%08" PRIX32
"",
794 lockClipboardData->clipDataId);
795 return cliprdr_packet_send(cliprdr, s);
804cliprdr_client_unlock_clipboard_data(CliprdrClientContext* context,
810 WINPR_ASSERT(context);
811 WINPR_ASSERT(unlockClipboardData);
814 WINPR_ASSERT(cliprdr);
816 s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
820 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_unlock_clipdata_new failed!");
821 return ERROR_INTERNAL_ERROR;
824 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientUnlockClipboardData: clipDataId: 0x%08" PRIX32
"",
825 unlockClipboardData->clipDataId);
826 return cliprdr_packet_send(cliprdr, s);
834static UINT cliprdr_client_format_data_request(CliprdrClientContext* context,
837 WINPR_ASSERT(context);
838 WINPR_ASSERT(formatDataRequest);
841 WINPR_ASSERT(cliprdr);
845 if ((mask & (CLIPRDR_FLAG_REMOTE_TO_LOCAL | CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES)) == 0)
847 WLog_Print(cliprdr->log, WLOG_WARN,
"remote -> local copy disabled, ignoring request");
848 return CHANNEL_RC_OK;
851 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, 0, 4);
854 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
855 return ERROR_INTERNAL_ERROR;
858 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId);
859 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatDataRequest(0x%08" PRIx32
" [%s])",
860 formatDataRequest->requestedFormatId,
861 ClipboardGetFormatIdString(formatDataRequest->requestedFormatId));
862 return cliprdr_packet_send(cliprdr, s);
871cliprdr_client_format_data_response(CliprdrClientContext* context,
874 WINPR_ASSERT(context);
875 WINPR_ASSERT(formatDataResponse);
878 WINPR_ASSERT(cliprdr);
882 (CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES)) != 0);
884 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
885 formatDataResponse->common.dataLen);
889 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
890 return ERROR_INTERNAL_ERROR;
893 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
894 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatDataResponse");
895 return cliprdr_packet_send(cliprdr, s);
904cliprdr_client_file_contents_request(CliprdrClientContext* context,
909 WINPR_ASSERT(context);
910 WINPR_ASSERT(fileContentsRequest);
914 return ERROR_INTERNAL_ERROR;
918 if ((mask & CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES) == 0)
920 WLog_Print(cliprdr->log, WLOG_WARN,
"remote -> local file copy disabled, ignoring request");
921 return CHANNEL_RC_OK;
924 if (!cliprdr->hasHugeFileSupport)
926 if (((UINT64)fileContentsRequest->cbRequested + fileContentsRequest->nPositionLow) >
928 return ERROR_INVALID_PARAMETER;
929 if (fileContentsRequest->nPositionHigh != 0)
930 return ERROR_INVALID_PARAMETER;
933 s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
937 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_request_new failed!");
938 return ERROR_INTERNAL_ERROR;
941 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFileContentsRequest: streamId: 0x%08" PRIX32
"",
942 fileContentsRequest->streamId);
943 return cliprdr_packet_send(cliprdr, s);
952cliprdr_client_file_contents_response(CliprdrClientContext* context,
958 WINPR_ASSERT(context);
959 WINPR_ASSERT(fileContentsResponse);
962 WINPR_ASSERT(cliprdr);
966 if ((mask & CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES) == 0)
967 return cliprdr_send_error_response(cliprdr, CB_FILECONTENTS_RESPONSE);
969 s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
973 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_response_new failed!");
974 return ERROR_INTERNAL_ERROR;
977 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFileContentsResponse: streamId: 0x%08" PRIX32
"",
978 fileContentsResponse->streamId);
979 return cliprdr_packet_send(cliprdr, s);
982static VOID VCAPITYPE cliprdr_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
983 UINT event, LPVOID pData,
984 UINT32 dataLength, UINT32 totalLength,
987 UINT error = CHANNEL_RC_OK;
989 WINPR_ASSERT(cliprdr);
993 case CHANNEL_EVENT_DATA_RECEIVED:
994 if (cliprdr->OpenHandle != openHandle)
996 WLog_Print(cliprdr->log, WLOG_ERROR,
"error no match");
999 if ((error = channel_client_post_message(cliprdr->MsgsHandle, pData, dataLength,
1000 totalLength, dataFlags)))
1001 WLog_Print(cliprdr->log, WLOG_ERROR,
"failed with error %" PRIu32
"", error);
1005 case CHANNEL_EVENT_WRITE_CANCELLED:
1006 case CHANNEL_EVENT_WRITE_COMPLETE:
1009 Stream_Free(s, TRUE);
1013 case CHANNEL_EVENT_USER:
1019 if (error && cliprdr->context->rdpcontext)
1020 setChannelError(cliprdr->context->rdpcontext, error,
1021 "cliprdr_virtual_channel_open_event_ex reported an error");
1029static UINT cliprdr_virtual_channel_event_connected(
cliprdrPlugin* cliprdr,
1030 WINPR_ATTR_UNUSED LPVOID pData,
1031 WINPR_ATTR_UNUSED UINT32 dataLength)
1034 WINPR_ASSERT(cliprdr);
1035 WINPR_ASSERT(cliprdr->context);
1037 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelOpenEx);
1038 status = cliprdr->channelEntryPoints.pVirtualChannelOpenEx(
1039 cliprdr->InitHandle, &cliprdr->OpenHandle, cliprdr->channelDef.name,
1040 cliprdr_virtual_channel_open_event_ex);
1041 if (status != CHANNEL_RC_OK)
1044 cliprdr->MsgsHandle = channel_client_create_handler(
1045 cliprdr->context->rdpcontext, cliprdr, cliprdr_order_recv, CLIPRDR_SVC_CHANNEL_NAME);
1046 if (!cliprdr->MsgsHandle)
1047 return ERROR_INTERNAL_ERROR;
1057static UINT cliprdr_virtual_channel_event_disconnected(
cliprdrPlugin* cliprdr)
1061 WINPR_ASSERT(cliprdr);
1063 channel_client_quit_handler(cliprdr->MsgsHandle);
1064 cliprdr->MsgsHandle =
nullptr;
1066 if (cliprdr->OpenHandle == 0)
1067 return CHANNEL_RC_OK;
1069 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelCloseEx);
1070 rc = cliprdr->channelEntryPoints.pVirtualChannelCloseEx(cliprdr->InitHandle,
1071 cliprdr->OpenHandle);
1073 if (CHANNEL_RC_OK != rc)
1075 WLog_Print(cliprdr->log, WLOG_ERROR,
"pVirtualChannelClose failed with %s [%08" PRIX32
"]",
1076 WTSErrorToString(rc), rc);
1080 cliprdr->OpenHandle = 0;
1082 return CHANNEL_RC_OK;
1090static UINT cliprdr_virtual_channel_event_terminated(
cliprdrPlugin* cliprdr)
1092 WINPR_ASSERT(cliprdr);
1094 cliprdr->InitHandle =
nullptr;
1095 free(cliprdr->context);
1097 return CHANNEL_RC_OK;
1100static VOID VCAPITYPE cliprdr_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1101 UINT event, LPVOID pData,
1104 UINT error = CHANNEL_RC_OK;
1106 WINPR_ASSERT(cliprdr);
1108 if (cliprdr->InitHandle != pInitHandle)
1110 WLog_Print(cliprdr->log, WLOG_ERROR,
"error no match");
1116 case CHANNEL_EVENT_CONNECTED:
1117 if ((error = cliprdr_virtual_channel_event_connected(cliprdr, pData, dataLength)))
1118 WLog_Print(cliprdr->log, WLOG_ERROR,
1119 "cliprdr_virtual_channel_event_connected failed with error %" PRIu32
"!",
1124 case CHANNEL_EVENT_DISCONNECTED:
1125 if ((error = cliprdr_virtual_channel_event_disconnected(cliprdr)))
1126 WLog_Print(cliprdr->log, WLOG_ERROR,
1127 "cliprdr_virtual_channel_event_disconnected failed with error %" PRIu32
1133 case CHANNEL_EVENT_TERMINATED:
1134 if ((error = cliprdr_virtual_channel_event_terminated(cliprdr)))
1135 WLog_Print(cliprdr->log, WLOG_ERROR,
1136 "cliprdr_virtual_channel_event_terminated failed with error %" PRIu32
1144 if (error && cliprdr->context->rdpcontext)
1145 setChannelError(cliprdr->context->rdpcontext, error,
1146 "cliprdr_virtual_channel_init_event reported an error");
1150#define VirtualChannelEntryEx cliprdr_VirtualChannelEntryEx
1152FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints,
1159 wLog* log = WLog_Get(CHANNELS_TAG(
"cliprdr.client"));
1164 WLog_Print(log, WLOG_ERROR,
"calloc failed!");
1169 cliprdr->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP |
1170 CHANNEL_OPTION_COMPRESS_RDP | CHANNEL_OPTION_SHOW_PROTOCOL;
1171 (void)sprintf_s(cliprdr->channelDef.name, ARRAYSIZE(cliprdr->channelDef.name),
1172 CLIPRDR_SVC_CHANNEL_NAME);
1174 WINPR_ASSERT(pEntryPointsEx);
1177 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1179 CliprdrClientContext* context =
1180 (CliprdrClientContext*)calloc(1,
sizeof(CliprdrClientContext));
1184 WLog_Print(cliprdr->log, WLOG_ERROR,
"calloc failed!");
1189 context->handle = (
void*)cliprdr;
1190 context->custom =
nullptr;
1191 context->ClientCapabilities = cliprdr_client_capabilities;
1192 context->TempDirectory = cliprdr_temp_directory;
1193 context->ClientFormatList = cliprdr_client_format_list;
1194 context->ClientFormatListResponse = cliprdr_client_format_list_response;
1195 context->ClientLockClipboardData = cliprdr_client_lock_clipboard_data;
1196 context->ClientUnlockClipboardData = cliprdr_client_unlock_clipboard_data;
1197 context->ClientFormatDataRequest = cliprdr_client_format_data_request;
1198 context->ClientFormatDataResponse = cliprdr_client_format_data_response;
1199 context->ClientFileContentsRequest = cliprdr_client_file_contents_request;
1200 context->ClientFileContentsResponse = cliprdr_client_file_contents_response;
1201 cliprdr->context = context;
1202 context->rdpcontext = pEntryPointsEx->context;
1205 WLog_Print(cliprdr->log, WLOG_DEBUG,
"VirtualChannelEntryEx");
1206 CopyMemory(&(cliprdr->channelEntryPoints), pEntryPoints,
1208 cliprdr->InitHandle = pInitHandle;
1209 rc = cliprdr->channelEntryPoints.pVirtualChannelInitEx(
1210 cliprdr, cliprdr->context, pInitHandle, &cliprdr->channelDef, 1,
1211 VIRTUAL_CHANNEL_VERSION_WIN2000, cliprdr_virtual_channel_init_event_ex);
1213 if (CHANNEL_RC_OK != rc)
1215 WLog_Print(cliprdr->log, WLOG_ERROR,
"pVirtualChannelInit failed with %s [%08" PRIX32
"]",
1216 WTSErrorToString(rc), rc);
1217 free(cliprdr->context);
1222 cliprdr->channelEntryPoints.pInterface = cliprdr->context;
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.