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);
304 WINPR_ASSERT(cliprdr);
307 WLog_Print(cliprdr->log, WLOG_DEBUG,
"FileContentsRequest");
309 request.common.msgType = CB_FILECONTENTS_REQUEST;
310 request.common.msgFlags = flags;
311 request.common.dataLen = length;
313 UINT error = cliprdr_read_file_contents_request(s, &request);
317 if (!cliprdr->streamFileClipEnabled)
319 WLog_Print(cliprdr->log, WLOG_WARN,
320 "clipboard::filecontents requested, but feature not announced! aborting.");
321 return ERROR_INVALID_DATA;
326 if ((mask & (CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES)) == 0)
328 WLog_Print(cliprdr->log, WLOG_WARN,
"local -> remote file copy disabled, ignoring request");
329 return cliprdr_send_error_response(cliprdr, CB_FILECONTENTS_RESPONSE);
331 IFCALLRET(context->ServerFileContentsRequest, error, context, &request);
334 WLog_Print(cliprdr->log, WLOG_ERROR,
335 "ServerFileContentsRequest failed with error %" PRIu32
"!", error);
345static UINT cliprdr_process_filecontents_response(
cliprdrPlugin* cliprdr,
wStream* s, UINT32 length,
349 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
351 WINPR_ASSERT(cliprdr);
354 WLog_Print(cliprdr->log, WLOG_DEBUG,
"FileContentsResponse");
356 if (!cliprdr->streamFileClipEnabled)
359 cliprdr->log, WLOG_WARN,
360 "clipboard::filecontents_response requested, but feature not announced! aborting.");
361 return ERROR_INVALID_DATA;
364 response.common.msgType = CB_FILECONTENTS_RESPONSE;
365 response.common.msgFlags = flags;
366 response.common.dataLen = length;
368 UINT error = cliprdr_read_file_contents_response(s, &response);
372 IFCALLRET(context->ServerFileContentsResponse, error, context, &response);
375 WLog_Print(cliprdr->log, WLOG_ERROR,
376 "ServerFileContentsResponse failed with error %" PRIu32
"!", error);
390 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
391 UINT error = CHANNEL_RC_OK;
393 WINPR_ASSERT(cliprdr);
396 WLog_Print(cliprdr->log, WLOG_DEBUG,
"LockClipData");
398 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 4))
399 return ERROR_INVALID_DATA;
401 if (!cliprdr->canLockClipData)
403 WLog_Print(cliprdr->log, WLOG_WARN,
404 "clipboard::lock requested, but feature not announced! aborting.");
405 return ERROR_INVALID_DATA;
408 lockClipboardData.common.msgType = CB_LOCK_CLIPDATA;
409 lockClipboardData.common.msgFlags = flags;
410 lockClipboardData.common.dataLen = length;
411 Stream_Read_UINT32(s, lockClipboardData.clipDataId);
412 IFCALLRET(context->ServerLockClipboardData, error, context, &lockClipboardData);
415 WLog_Print(cliprdr->log, WLOG_ERROR,
416 "ServerLockClipboardData failed with error %" PRIu32
"!", error);
430 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
432 WINPR_ASSERT(cliprdr);
435 WLog_Print(cliprdr->log, WLOG_DEBUG,
"UnlockClipData");
437 if (!cliprdr->canLockClipData)
439 WLog_Print(cliprdr->log, WLOG_WARN,
440 "clipboard::unlock requested, but feature not announced! aborting.");
441 return ERROR_INVALID_DATA;
444 UINT error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData);
448 unlockClipboardData.common.msgType = CB_UNLOCK_CLIPDATA;
449 unlockClipboardData.common.msgFlags = flags;
450 unlockClipboardData.common.dataLen = length;
452 IFCALLRET(context->ServerUnlockClipboardData, error, context, &unlockClipboardData);
455 WLog_Print(cliprdr->log, WLOG_ERROR,
456 "ServerUnlockClipboardData failed with error %" PRIu32
"!", error);
466static UINT cliprdr_order_recv(LPVOID userdata,
wStream* s)
474 WINPR_ASSERT(cliprdr);
477 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 8))
478 return ERROR_INVALID_DATA;
480 Stream_Read_UINT16(s, msgType);
481 Stream_Read_UINT16(s, msgFlags);
482 Stream_Read_UINT32(s, dataLen);
484 const BOOL failedResponse =
485 (msgFlags & CB_RESPONSE_FAIL) &&
486 ((msgType == CB_FORMAT_DATA_RESPONSE) || (msgType == CB_FORMAT_LIST_RESPONSE) ||
487 (msgType == CB_FILECONTENTS_RESPONSE));
497 const size_t available = Stream_GetRemainingLength(s);
498 if (dataLen > available)
500 WLog_Print(cliprdr->log, WLOG_WARN,
501 "failed response (msgType: %" PRIu16
") claims dataLen: %" PRIu32
502 " but only %" PRIuz
" bytes follow",
503 msgType, dataLen, available);
504 dataLen = WINPR_ASSERTING_INT_CAST(UINT32, available);
507 else if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, dataLen))
508 return ERROR_INVALID_DATA;
510 char buffer1[64] = WINPR_C_ARRAY_INIT;
511 char buffer2[64] = WINPR_C_ARRAY_INIT;
512 WLog_Print(cliprdr->log, WLOG_DEBUG,
513 "msgType: %s (%" PRIu16
"), msgFlags: %s dataLen: %" PRIu32
"",
514 CB_MSG_TYPE_STRING(msgType, buffer1,
sizeof(buffer1)), msgType,
515 CB_MSG_FLAGS_STRING(msgFlags, buffer2,
sizeof(buffer2)), dataLen);
520 if ((error = cliprdr_process_clip_caps(cliprdr, s, dataLen, msgFlags)))
521 WLog_Print(cliprdr->log, WLOG_ERROR,
522 "cliprdr_process_clip_caps failed with error %" PRIu32
"!", error);
526 case CB_MONITOR_READY:
527 if ((error = cliprdr_process_monitor_ready(cliprdr, s, dataLen, msgFlags)))
528 WLog_Print(cliprdr->log, WLOG_ERROR,
529 "cliprdr_process_monitor_ready failed with error %" PRIu32
"!", error);
534 if ((error = cliprdr_process_format_list(cliprdr, s, dataLen, msgFlags)))
535 WLog_Print(cliprdr->log, WLOG_ERROR,
536 "cliprdr_process_format_list failed with error %" PRIu32
"!", error);
540 case CB_FORMAT_LIST_RESPONSE:
541 if ((error = cliprdr_process_format_list_response(cliprdr, s, dataLen, msgFlags)))
542 WLog_Print(cliprdr->log, WLOG_ERROR,
543 "cliprdr_process_format_list_response failed with error %" PRIu32
"!",
548 case CB_FORMAT_DATA_REQUEST:
549 if ((error = cliprdr_process_format_data_request(cliprdr, s, dataLen, msgFlags)))
550 WLog_Print(cliprdr->log, WLOG_ERROR,
551 "cliprdr_process_format_data_request failed with error %" PRIu32
"!",
556 case CB_FORMAT_DATA_RESPONSE:
557 if ((error = cliprdr_process_format_data_response(cliprdr, s, dataLen, msgFlags)))
558 WLog_Print(cliprdr->log, WLOG_ERROR,
559 "cliprdr_process_format_data_response failed with error %" PRIu32
"!",
564 case CB_FILECONTENTS_REQUEST:
565 if ((error = cliprdr_process_filecontents_request(cliprdr, s, dataLen, msgFlags)))
566 WLog_Print(cliprdr->log, WLOG_ERROR,
567 "cliprdr_process_filecontents_request failed with error %" PRIu32
"!",
572 case CB_FILECONTENTS_RESPONSE:
573 if ((error = cliprdr_process_filecontents_response(cliprdr, s, dataLen, msgFlags)))
574 WLog_Print(cliprdr->log, WLOG_ERROR,
575 "cliprdr_process_filecontents_response failed with error %" PRIu32
"!",
580 case CB_LOCK_CLIPDATA:
581 if ((error = cliprdr_process_lock_clipdata(cliprdr, s, dataLen, msgFlags)))
582 WLog_Print(cliprdr->log, WLOG_ERROR,
583 "cliprdr_process_lock_clipdata failed with error %" PRIu32
"!", error);
587 case CB_UNLOCK_CLIPDATA:
588 if ((error = cliprdr_process_unlock_clipdata(cliprdr, s, dataLen, msgFlags)))
589 WLog_Print(cliprdr->log, WLOG_ERROR,
590 "cliprdr_process_unlock_clipdata failed with error %" PRIu32
"!", error);
595 error = CHANNEL_RC_BAD_PROC;
596 WLog_Print(cliprdr->log, WLOG_ERROR,
"unknown msgType %" PRIu16
"", msgType);
600 Stream_Free(s, TRUE);
613static UINT cliprdr_client_capabilities(CliprdrClientContext* context,
621 WINPR_ASSERT(context);
624 WINPR_ASSERT(cliprdr);
626 s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
630 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
631 return ERROR_INTERNAL_ERROR;
634 Stream_Write_UINT16(s, 1);
635 Stream_Write_UINT16(s, 0);
637 capabilities->capabilitySets);
638 Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetType);
639 Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetLength);
640 Stream_Write_UINT32(s, generalCapabilitySet->version);
641 flags = generalCapabilitySet->generalFlags;
647 if (!cliprdr->useLongFormatNames)
648 flags &= (uint32_t)~CB_USE_LONG_FORMAT_NAMES;
649 if (!cliprdr->streamFileClipEnabled)
650 flags &= (uint32_t)~CB_STREAM_FILECLIP_ENABLED;
651 if (!cliprdr->fileClipNoFilePaths)
652 flags &= (uint32_t)~CB_FILECLIP_NO_FILE_PATHS;
653 if (!cliprdr->canLockClipData)
654 flags &= (uint32_t)~CB_CAN_LOCK_CLIPDATA;
655 if (!cliprdr->hasHugeFileSupport)
656 flags &= (uint32_t)~CB_HUGE_FILE_SUPPORT_ENABLED;
658 cliprdr->useLongFormatNames = (flags & CB_USE_LONG_FORMAT_NAMES) != 0;
659 cliprdr->streamFileClipEnabled = (flags & CB_STREAM_FILECLIP_ENABLED) != 0;
660 cliprdr->fileClipNoFilePaths = (flags & CB_FILECLIP_NO_FILE_PATHS) != 0;
661 cliprdr->canLockClipData = (flags & CB_CAN_LOCK_CLIPDATA) != 0;
662 cliprdr->hasHugeFileSupport = (flags & CB_HUGE_FILE_SUPPORT_ENABLED) != 0;
664 Stream_Write_UINT32(s, flags);
665 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientCapabilities");
667 return cliprdr_packet_send(cliprdr, s);
675static UINT cliprdr_temp_directory(CliprdrClientContext* context,
681 WINPR_ASSERT(context);
682 WINPR_ASSERT(tempDirectory);
685 WINPR_ASSERT(cliprdr);
687 const size_t tmpDirCharLen =
sizeof(tempDirectory->szTempDir) /
sizeof(WCHAR);
688 s = cliprdr_packet_new(CB_TEMP_DIRECTORY, 0, tmpDirCharLen *
sizeof(WCHAR));
692 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
693 return ERROR_INTERNAL_ERROR;
696 if (Stream_Write_UTF16_String_From_UTF8(s, tmpDirCharLen - 1, tempDirectory->szTempDir,
697 ARRAYSIZE(tempDirectory->szTempDir), TRUE) < 0)
699 Stream_Free(s, TRUE);
700 return ERROR_INTERNAL_ERROR;
704 Stream_Write_UINT16(s, 0);
706 WLog_Print(cliprdr->log, WLOG_DEBUG,
"TempDirectory: %s", tempDirectory->szTempDir);
707 return cliprdr_packet_send(cliprdr, s);
715static UINT cliprdr_client_format_list(CliprdrClientContext* context,
718 WINPR_ASSERT(context);
719 WINPR_ASSERT(formatList);
722 WINPR_ASSERT(cliprdr);
725 const UINT32 mask = CB_RESPONSE_OK | CB_RESPONSE_FAIL;
726 if ((formatList->common.msgFlags & mask) != 0)
727 WLog_Print(cliprdr->log, WLOG_WARN,
728 "Sending clipboard request with invalid flags msgFlags = 0x%08" PRIx32
729 ". Correct in your client!",
730 formatList->common.msgFlags & mask);
736 formatList, mask, CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES);
738 const uint32_t level = WLOG_DEBUG;
739 if (WLog_IsLevelActive(cliprdr->log, level))
741 WLog_Print(cliprdr->log, level,
"ClientFormatList: numFormats: %" PRIu32
"",
742 formatList->numFormats);
743 for (
size_t x = 0; x < filterList.numFormats; x++)
746 WLog_Print(cliprdr->log, level,
"[%" PRIuz
"]: id=0x%08" PRIx32
" [%s|%s]", x,
747 format->formatId, ClipboardGetFormatIdString(format->formatId),
752 wStream* s = cliprdr_packet_format_list_new(&filterList, cliprdr->useLongFormatNames, FALSE);
753 cliprdr_free_format_list(&filterList);
757 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_format_list_new failed!");
758 return ERROR_INTERNAL_ERROR;
761 return cliprdr_packet_send(cliprdr, s);
770cliprdr_client_format_list_response(CliprdrClientContext* context,
776 WINPR_ASSERT(context);
777 WINPR_ASSERT(formatListResponse);
780 WINPR_ASSERT(cliprdr);
782 s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags, 0);
786 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
787 return ERROR_INTERNAL_ERROR;
790 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatListResponse");
791 return cliprdr_packet_send(cliprdr, s);
799static UINT cliprdr_client_lock_clipboard_data(CliprdrClientContext* context,
805 WINPR_ASSERT(context);
806 WINPR_ASSERT(lockClipboardData);
809 WINPR_ASSERT(cliprdr);
811 s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
815 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_lock_clipdata_new failed!");
816 return ERROR_INTERNAL_ERROR;
819 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientLockClipboardData: clipDataId: 0x%08" PRIX32
"",
820 lockClipboardData->clipDataId);
821 return cliprdr_packet_send(cliprdr, s);
830cliprdr_client_unlock_clipboard_data(CliprdrClientContext* context,
836 WINPR_ASSERT(context);
837 WINPR_ASSERT(unlockClipboardData);
840 WINPR_ASSERT(cliprdr);
842 s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
846 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_unlock_clipdata_new failed!");
847 return ERROR_INTERNAL_ERROR;
850 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientUnlockClipboardData: clipDataId: 0x%08" PRIX32
"",
851 unlockClipboardData->clipDataId);
852 return cliprdr_packet_send(cliprdr, s);
860static UINT cliprdr_client_format_data_request(CliprdrClientContext* context,
863 WINPR_ASSERT(context);
864 WINPR_ASSERT(formatDataRequest);
867 WINPR_ASSERT(cliprdr);
871 if ((mask & (CLIPRDR_FLAG_REMOTE_TO_LOCAL | CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES)) == 0)
873 WLog_Print(cliprdr->log, WLOG_WARN,
"remote -> local copy disabled, ignoring request");
874 return CHANNEL_RC_OK;
877 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, 0, 4);
880 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
881 return ERROR_INTERNAL_ERROR;
884 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId);
885 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatDataRequest(0x%08" PRIx32
" [%s])",
886 formatDataRequest->requestedFormatId,
887 ClipboardGetFormatIdString(formatDataRequest->requestedFormatId));
888 return cliprdr_packet_send(cliprdr, s);
897cliprdr_client_format_data_response(CliprdrClientContext* context,
900 WINPR_ASSERT(context);
901 WINPR_ASSERT(formatDataResponse);
904 WINPR_ASSERT(cliprdr);
908 (CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES)) != 0);
910 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
911 formatDataResponse->common.dataLen);
915 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
916 return ERROR_INTERNAL_ERROR;
919 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
920 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatDataResponse");
921 return cliprdr_packet_send(cliprdr, s);
930cliprdr_client_file_contents_request(CliprdrClientContext* context,
933 WINPR_ASSERT(context);
934 WINPR_ASSERT(fileContentsRequest);
938 return ERROR_INTERNAL_ERROR;
940 if (!cliprdr->streamFileClipEnabled)
942 WLog_Print(cliprdr->log, WLOG_WARN,
943 "clipboard::filecontents requested, but feature not announced! aborting.");
944 return ERROR_INVALID_DATA;
949 if ((mask & CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES) == 0)
951 WLog_Print(cliprdr->log, WLOG_WARN,
"remote -> local file copy disabled, ignoring request");
952 return CHANNEL_RC_OK;
955 if (!cliprdr->hasHugeFileSupport)
957 if (((UINT64)fileContentsRequest->cbRequested + fileContentsRequest->nPositionLow) >
959 return ERROR_INVALID_PARAMETER;
960 if (fileContentsRequest->nPositionHigh != 0)
961 return ERROR_INVALID_PARAMETER;
964 wStream* s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
968 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_request_new failed!");
969 return ERROR_INTERNAL_ERROR;
972 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFileContentsRequest: streamId: 0x%08" PRIX32
"",
973 fileContentsRequest->streamId);
974 return cliprdr_packet_send(cliprdr, s);
983cliprdr_client_file_contents_response(CliprdrClientContext* context,
986 WINPR_ASSERT(context);
987 WINPR_ASSERT(fileContentsResponse);
990 WINPR_ASSERT(cliprdr);
992 if (!cliprdr->streamFileClipEnabled)
995 cliprdr->log, WLOG_WARN,
996 "clipboard::filecontents_response requested, but feature not announced! aborting.");
997 return ERROR_INVALID_DATA;
1002 if ((mask & CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES) == 0)
1003 return cliprdr_send_error_response(cliprdr, CB_FILECONTENTS_RESPONSE);
1005 wStream* s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
1009 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_response_new failed!");
1010 return ERROR_INTERNAL_ERROR;
1013 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFileContentsResponse: streamId: 0x%08" PRIX32
"",
1014 fileContentsResponse->streamId);
1015 return cliprdr_packet_send(cliprdr, s);
1018static VOID VCAPITYPE cliprdr_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1019 UINT event, LPVOID pData,
1020 UINT32 dataLength, UINT32 totalLength,
1023 UINT error = CHANNEL_RC_OK;
1025 WINPR_ASSERT(cliprdr);
1029 case CHANNEL_EVENT_DATA_RECEIVED:
1030 if (cliprdr->OpenHandle != openHandle)
1032 WLog_Print(cliprdr->log, WLOG_ERROR,
"error no match");
1035 if ((error = channel_client_post_message(cliprdr->MsgsHandle, pData, dataLength,
1036 totalLength, dataFlags)))
1037 WLog_Print(cliprdr->log, WLOG_ERROR,
"failed with error %" PRIu32
"", error);
1041 case CHANNEL_EVENT_WRITE_CANCELLED:
1042 case CHANNEL_EVENT_WRITE_COMPLETE:
1045 Stream_Free(s, TRUE);
1049 case CHANNEL_EVENT_USER:
1055 if (error && cliprdr->context->rdpcontext)
1056 setChannelError(cliprdr->context->rdpcontext, error,
1057 "cliprdr_virtual_channel_open_event_ex reported an error");
1065static UINT cliprdr_virtual_channel_event_connected(
cliprdrPlugin* cliprdr,
1066 WINPR_ATTR_UNUSED LPVOID pData,
1067 WINPR_ATTR_UNUSED UINT32 dataLength)
1070 WINPR_ASSERT(cliprdr);
1071 WINPR_ASSERT(cliprdr->context);
1073 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelOpenEx);
1074 status = cliprdr->channelEntryPoints.pVirtualChannelOpenEx(
1075 cliprdr->InitHandle, &cliprdr->OpenHandle, cliprdr->channelDef.name,
1076 cliprdr_virtual_channel_open_event_ex);
1077 if (status != CHANNEL_RC_OK)
1080 cliprdr->MsgsHandle = channel_client_create_handler(
1081 cliprdr->context->rdpcontext, cliprdr, cliprdr_order_recv, CLIPRDR_SVC_CHANNEL_NAME);
1082 if (!cliprdr->MsgsHandle)
1083 return ERROR_INTERNAL_ERROR;
1093static UINT cliprdr_virtual_channel_event_disconnected(
cliprdrPlugin* cliprdr)
1097 WINPR_ASSERT(cliprdr);
1099 channel_client_quit_handler(cliprdr->MsgsHandle);
1100 cliprdr->MsgsHandle =
nullptr;
1102 if (cliprdr->OpenHandle == 0)
1103 return CHANNEL_RC_OK;
1105 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelCloseEx);
1106 rc = cliprdr->channelEntryPoints.pVirtualChannelCloseEx(cliprdr->InitHandle,
1107 cliprdr->OpenHandle);
1109 if (CHANNEL_RC_OK != rc)
1111 WLog_Print(cliprdr->log, WLOG_ERROR,
"pVirtualChannelClose failed with %s [%08" PRIX32
"]",
1112 WTSErrorToString(rc), rc);
1116 cliprdr->OpenHandle = 0;
1118 return CHANNEL_RC_OK;
1126static UINT cliprdr_virtual_channel_event_terminated(
cliprdrPlugin* cliprdr)
1128 WINPR_ASSERT(cliprdr);
1130 cliprdr->InitHandle =
nullptr;
1131 free(cliprdr->context);
1133 return CHANNEL_RC_OK;
1136static VOID VCAPITYPE cliprdr_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1137 UINT event, LPVOID pData,
1140 UINT error = CHANNEL_RC_OK;
1142 WINPR_ASSERT(cliprdr);
1144 if (cliprdr->InitHandle != pInitHandle)
1146 WLog_Print(cliprdr->log, WLOG_ERROR,
"error no match");
1152 case CHANNEL_EVENT_CONNECTED:
1153 if ((error = cliprdr_virtual_channel_event_connected(cliprdr, pData, dataLength)))
1154 WLog_Print(cliprdr->log, WLOG_ERROR,
1155 "cliprdr_virtual_channel_event_connected failed with error %" PRIu32
"!",
1160 case CHANNEL_EVENT_DISCONNECTED:
1161 if ((error = cliprdr_virtual_channel_event_disconnected(cliprdr)))
1162 WLog_Print(cliprdr->log, WLOG_ERROR,
1163 "cliprdr_virtual_channel_event_disconnected failed with error %" PRIu32
1169 case CHANNEL_EVENT_TERMINATED:
1170 if ((error = cliprdr_virtual_channel_event_terminated(cliprdr)))
1171 WLog_Print(cliprdr->log, WLOG_ERROR,
1172 "cliprdr_virtual_channel_event_terminated failed with error %" PRIu32
1180 if (error && cliprdr->context->rdpcontext)
1181 setChannelError(cliprdr->context->rdpcontext, error,
1182 "cliprdr_virtual_channel_init_event reported an error");
1186#define VirtualChannelEntryEx cliprdr_VirtualChannelEntryEx
1188FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS_EX pEntryPoints,
1195 wLog* log = WLog_Get(CHANNELS_TAG(
"cliprdr.client"));
1200 WLog_Print(log, WLOG_ERROR,
"calloc failed!");
1205 cliprdr->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP |
1206 CHANNEL_OPTION_COMPRESS_RDP | CHANNEL_OPTION_SHOW_PROTOCOL;
1207 (void)sprintf_s(cliprdr->channelDef.name, ARRAYSIZE(cliprdr->channelDef.name),
1208 CLIPRDR_SVC_CHANNEL_NAME);
1210 WINPR_ASSERT(pEntryPointsEx);
1213 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1215 CliprdrClientContext* context =
1216 (CliprdrClientContext*)calloc(1,
sizeof(CliprdrClientContext));
1220 WLog_Print(cliprdr->log, WLOG_ERROR,
"calloc failed!");
1225 context->handle = (
void*)cliprdr;
1226 context->custom =
nullptr;
1227 context->ClientCapabilities = cliprdr_client_capabilities;
1228 context->TempDirectory = cliprdr_temp_directory;
1229 context->ClientFormatList = cliprdr_client_format_list;
1230 context->ClientFormatListResponse = cliprdr_client_format_list_response;
1231 context->ClientLockClipboardData = cliprdr_client_lock_clipboard_data;
1232 context->ClientUnlockClipboardData = cliprdr_client_unlock_clipboard_data;
1233 context->ClientFormatDataRequest = cliprdr_client_format_data_request;
1234 context->ClientFormatDataResponse = cliprdr_client_format_data_response;
1235 context->ClientFileContentsRequest = cliprdr_client_file_contents_request;
1236 context->ClientFileContentsResponse = cliprdr_client_file_contents_response;
1237 cliprdr->context = context;
1238 context->rdpcontext = pEntryPointsEx->context;
1241 WLog_Print(cliprdr->log, WLOG_DEBUG,
"VirtualChannelEntryEx");
1242 CopyMemory(&(cliprdr->channelEntryPoints), pEntryPoints,
1244 cliprdr->InitHandle = pInitHandle;
1245 rc = cliprdr->channelEntryPoints.pVirtualChannelInitEx(
1246 cliprdr, cliprdr->context, pInitHandle, &cliprdr->channelDef, 1,
1247 VIRTUAL_CHANNEL_VERSION_WIN2000, cliprdr_virtual_channel_init_event_ex);
1249 if (CHANNEL_RC_OK != rc)
1251 WLog_Print(cliprdr->log, WLOG_ERROR,
"pVirtualChannelInit failed with %s [%08" PRIX32
"]",
1252 WTSErrorToString(rc), rc);
1253 free(cliprdr->context);
1258 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.