22#include <freerdp/config.h>
25#include <winpr/assert.h>
26#include <winpr/print.h>
27#include <winpr/stream.h>
29#include <freerdp/freerdp.h>
30#include <freerdp/channels/log.h>
31#include "cliprdr_main.h"
32#include "../cliprdr_common.h"
85 WINPR_ASSERT(cliprdr);
87 const size_t pos = Stream_GetPosition(s);
88 if ((pos < 8) || (pos > UINT32_MAX))
94 const UINT32 dataLen = (UINT32)(pos - 8);
95 if (!Stream_SetPosition(s, 4))
97 Stream_Write_UINT32(s, dataLen);
99 WINPR_ASSERT(pos <= UINT32_MAX);
100 status = WTSVirtualChannelWrite(cliprdr->ChannelHandle, Stream_BufferAs(s,
char), (UINT32)pos,
102 rc = status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
104 Stream_Free(s, TRUE);
113static UINT cliprdr_server_capabilities(CliprdrServerContext* context,
118 WINPR_ASSERT(context);
119 WINPR_ASSERT(capabilities);
123 if (capabilities->common.msgType != CB_CLIP_CAPS)
124 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
125 capabilities->common.msgType);
127 if (capabilities->cCapabilitiesSets > UINT16_MAX)
129 WLog_Print(cliprdr->log, WLOG_ERROR,
"Invalid number of capability sets in clipboard caps");
130 return ERROR_INVALID_PARAMETER;
133 wStream* s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
137 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
138 return ERROR_INTERNAL_ERROR;
141 Stream_Write_UINT16(s,
142 (UINT16)capabilities->cCapabilitiesSets);
143 Stream_Write_UINT16(s, 0);
144 for (UINT32 x = 0; x < capabilities->cCapabilitiesSets; x++)
148 offset += cap->capabilitySetLength;
150 switch (cap->capabilitySetType)
152 case CB_CAPSTYPE_GENERAL:
157 s, generalCapabilitySet->capabilitySetType);
159 s, generalCapabilitySet->capabilitySetLength);
160 Stream_Write_UINT32(s, generalCapabilitySet->version);
162 s, generalCapabilitySet->generalFlags);
167 WLog_Print(cliprdr->log, WLOG_WARN,
"Unknown capability set type %08" PRIx16,
168 cap->capabilitySetType);
169 if (!Stream_SafeZero(s, cap->capabilitySetLength))
171 WLog_Print(cliprdr->log, WLOG_ERROR,
"short stream");
172 Stream_Free(s, TRUE);
173 return ERROR_NO_DATA;
178 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerCapabilities");
179 return cliprdr_server_packet_send(cliprdr, s);
187static UINT cliprdr_server_monitor_ready(CliprdrServerContext* context,
190 WINPR_ASSERT(context);
191 WINPR_ASSERT(monitorReady);
195 if (monitorReady->common.msgType != CB_MONITOR_READY)
196 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
197 monitorReady->common.msgType);
199 wStream* s = cliprdr_packet_new(CB_MONITOR_READY, monitorReady->common.msgFlags,
200 monitorReady->common.dataLen);
204 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
205 return ERROR_INTERNAL_ERROR;
208 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerMonitorReady");
209 return cliprdr_server_packet_send(cliprdr, s);
217static UINT cliprdr_server_format_list(CliprdrServerContext* context,
220 WINPR_ASSERT(context);
221 WINPR_ASSERT(formatList);
225 wStream* s = cliprdr_packet_format_list_new(formatList, context->useLongFormatNames, FALSE);
228 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_format_list_new failed!");
229 return ERROR_INTERNAL_ERROR;
232 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFormatList: numFormats: %" PRIu32
"",
233 formatList->numFormats);
234 return cliprdr_server_packet_send(cliprdr, s);
243cliprdr_server_format_list_response(CliprdrServerContext* context,
246 WINPR_ASSERT(context);
247 WINPR_ASSERT(formatListResponse);
250 if (formatListResponse->common.msgType != CB_FORMAT_LIST_RESPONSE)
251 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
252 formatListResponse->common.msgType);
254 wStream* s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags,
255 formatListResponse->common.dataLen);
259 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
260 return ERROR_INTERNAL_ERROR;
263 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFormatListResponse");
264 return cliprdr_server_packet_send(cliprdr, s);
272static UINT cliprdr_server_lock_clipboard_data(CliprdrServerContext* context,
275 WINPR_ASSERT(context);
276 WINPR_ASSERT(lockClipboardData);
279 if (lockClipboardData->common.msgType != CB_LOCK_CLIPDATA)
280 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
281 lockClipboardData->common.msgType);
283 wStream* s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
286 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_lock_clipdata_new failed!");
287 return ERROR_INTERNAL_ERROR;
290 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerLockClipboardData: clipDataId: 0x%08" PRIX32
"",
291 lockClipboardData->clipDataId);
292 return cliprdr_server_packet_send(cliprdr, s);
301cliprdr_server_unlock_clipboard_data(CliprdrServerContext* context,
304 WINPR_ASSERT(context);
305 WINPR_ASSERT(unlockClipboardData);
308 if (unlockClipboardData->common.msgType != CB_UNLOCK_CLIPDATA)
309 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
310 unlockClipboardData->common.msgType);
312 wStream* s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
316 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_unlock_clipdata_new failed!");
317 return ERROR_INTERNAL_ERROR;
320 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerUnlockClipboardData: clipDataId: 0x%08" PRIX32
"",
321 unlockClipboardData->clipDataId);
322 return cliprdr_server_packet_send(cliprdr, s);
330static UINT cliprdr_server_format_data_request(CliprdrServerContext* context,
333 WINPR_ASSERT(context);
334 WINPR_ASSERT(formatDataRequest);
337 if (formatDataRequest->common.msgType != CB_FORMAT_DATA_REQUEST)
338 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
339 formatDataRequest->common.msgType);
342 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, 0, 4);
346 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
347 return ERROR_INTERNAL_ERROR;
350 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId);
351 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatDataRequest");
352 return cliprdr_server_packet_send(cliprdr, s);
361cliprdr_server_format_data_response(CliprdrServerContext* context,
364 WINPR_ASSERT(context);
365 WINPR_ASSERT(formatDataResponse);
369 if (formatDataResponse->common.msgType != CB_FORMAT_DATA_RESPONSE)
370 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
371 formatDataResponse->common.msgType);
373 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
374 formatDataResponse->common.dataLen);
378 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
379 return ERROR_INTERNAL_ERROR;
382 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
383 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFormatDataResponse");
384 return cliprdr_server_packet_send(cliprdr, s);
393cliprdr_server_file_contents_request(CliprdrServerContext* context,
396 WINPR_ASSERT(context);
397 WINPR_ASSERT(fileContentsRequest);
401 if (fileContentsRequest->common.msgType != CB_FILECONTENTS_REQUEST)
402 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
403 fileContentsRequest->common.msgType);
405 wStream* s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
408 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_request_new failed!");
409 return ERROR_INTERNAL_ERROR;
412 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFileContentsRequest: streamId: 0x%08" PRIX32
"",
413 fileContentsRequest->streamId);
414 return cliprdr_server_packet_send(cliprdr, s);
423cliprdr_server_file_contents_response(CliprdrServerContext* context,
426 WINPR_ASSERT(context);
427 WINPR_ASSERT(fileContentsResponse);
430 WINPR_ASSERT(cliprdr);
432 if (fileContentsResponse->common.msgType != CB_FILECONTENTS_RESPONSE)
433 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
434 fileContentsResponse->common.msgType);
436 wStream* s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
439 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_response_new failed!");
440 return ERROR_INTERNAL_ERROR;
443 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFileContentsResponse: streamId: 0x%08" PRIX32
"",
444 fileContentsResponse->streamId);
445 return cliprdr_server_packet_send(cliprdr, s);
453static UINT cliprdr_server_receive_general_capability(CliprdrServerContext* context,
wStream* s,
456 WINPR_ASSERT(context);
457 WINPR_ASSERT(cap_set);
460 WINPR_ASSERT(cliprdr);
464 WLog_Print(cliprdr->log, WLOG_ERROR,
465 "invalid capabilitySetLength %" PRIu16
" != %" PRIuz
466 " (capabilitySetType=CB_CAPSTYPE_GENERAL)",
468 return ERROR_INVALID_DATA;
471 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
472 return ERROR_INVALID_DATA;
474 Stream_Read_UINT32(s, cap_set->version);
475 Stream_Read_UINT32(s, cap_set->generalFlags);
477 if (context->useLongFormatNames)
478 context->useLongFormatNames = (cap_set->generalFlags & CB_USE_LONG_FORMAT_NAMES) != 0;
480 if (context->streamFileClipEnabled)
481 context->streamFileClipEnabled = (cap_set->generalFlags & CB_STREAM_FILECLIP_ENABLED) != 0;
483 if (context->fileClipNoFilePaths)
484 context->fileClipNoFilePaths = (cap_set->generalFlags & CB_FILECLIP_NO_FILE_PATHS) != 0;
486 if (context->canLockClipData)
487 context->canLockClipData = (cap_set->generalFlags & CB_CAN_LOCK_CLIPDATA) != 0;
489 if (context->hasHugeFileSupport)
490 context->hasHugeFileSupport = (cap_set->generalFlags & CB_HUGE_FILE_SUPPORT_ENABLED) != 0;
492 return CHANNEL_RC_OK;
496static BOOL cliprdr_capabilities_contain_capset(wLog* log,
const CLIPRDR_CAPABILITIES* capabilities,
497 size_t capabilitiesSize,
500 WINPR_ASSERT(capabilities);
502 const BYTE* offset = (
const BYTE*)capabilities->capabilitySets;
503 size_t byteOffset = 0;
504 for (UINT32 x = 0; x < capabilities->cCapabilitiesSets; x++)
508 byteOffset += cur->capabilitySetLength;
509 if (byteOffset > capabilitiesSize)
512 offset += cur->capabilitySetLength;
514 if (cur->capabilitySetType == capset->capabilitySetType)
516 WLog_Print(log, WLOG_ERROR,
"duplicate CLIPRDR_CAPABILITY_SET %" PRIu16,
517 cur->capabilitySetType);
529static UINT cliprdr_server_receive_capabilities(CliprdrServerContext* context,
wStream* s,
532 UINT error = ERROR_INVALID_DATA;
533 size_t cap_sets_size = 0;
536 WINPR_ASSERT(context);
537 WINPR_UNUSED(header);
540 WINPR_ASSERT(cliprdr);
542 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientCapabilities");
543 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
544 return ERROR_INVALID_DATA;
546 const UINT16 cCapabilitiesSets = Stream_Get_UINT16(s);
547 Stream_Seek_UINT16(s);
549 for (
size_t index = 0; index < cCapabilitiesSets; index++)
551 const size_t cap_set_offset = cap_sets_size;
554 const UINT16 capabilitySetType = Stream_Get_UINT16(s);
555 const UINT16 capabilitySetLength = Stream_Get_UINT16(s);
559 WLog_Print(cliprdr->log, WLOG_ERROR,
560 "invalid capabilitySetLength %" PRIu16
" < %" PRIuz
561 " (capabilitySetType=%" PRIu16
")",
566 if (!Stream_CheckAndLogRequiredLength(
570 cap_sets_size += capabilitySetLength;
575 WLog_Print(cliprdr->log, WLOG_ERROR,
"capabilities.capabilitySets realloc failed!");
576 free(capabilities.capabilitySets);
577 return CHANNEL_RC_NO_MEMORY;
580 capabilities.capabilitySets = tmp;
585 capSet->capabilitySetType = capabilitySetType;
586 capSet->capabilitySetLength = capabilitySetLength;
588 if (cliprdr_capabilities_contain_capset(cliprdr->log, &capabilities, cap_sets_size, capSet))
591 capabilities.cCapabilitiesSets++;
593 switch (capSet->capabilitySetType)
595 case CB_CAPSTYPE_GENERAL:
596 error = cliprdr_server_receive_general_capability(
601 cliprdr->log, WLOG_ERROR,
602 "cliprdr_server_receive_general_capability failed with error %" PRIu32
"",
609 WLog_Print(cliprdr->log, WLOG_ERROR,
"unknown cliprdr capability set: %" PRIu16
"",
610 capSet->capabilitySetType);
615 error = CHANNEL_RC_OK;
616 IFCALLRET(context->ClientCapabilities, error, context, &capabilities);
618 free(capabilities.capabilitySets);
627static UINT cliprdr_server_receive_temporary_directory(CliprdrServerContext* context,
wStream* s,
630 UINT error = CHANNEL_RC_OK;
632 WINPR_ASSERT(context);
633 WINPR_UNUSED(header);
636 WINPR_ASSERT(cliprdr);
638 if (!Stream_CheckAndLogRequiredLength(TAG, s,
639 ARRAYSIZE(cliprdr->temporaryDirectory) *
sizeof(WCHAR)))
640 return CHANNEL_RC_NO_MEMORY;
642 const WCHAR* wszTempDir = Stream_ConstPointer(s);
644 if (wszTempDir[ARRAYSIZE(cliprdr->temporaryDirectory) - 1] != 0)
646 WLog_Print(cliprdr->log, WLOG_ERROR,
"wszTempDir[259] was not 0");
647 return ERROR_INVALID_DATA;
650 if (ConvertWCharNToUtf8(wszTempDir, ARRAYSIZE(cliprdr->temporaryDirectory),
651 cliprdr->temporaryDirectory,
652 ARRAYSIZE(cliprdr->temporaryDirectory)) < 0)
654 WLog_Print(cliprdr->log, WLOG_ERROR,
"failed to convert temporary directory name");
655 return ERROR_INVALID_DATA;
658 size_t length = strnlen(cliprdr->temporaryDirectory, ARRAYSIZE(cliprdr->temporaryDirectory));
660 if (length >= ARRAYSIZE(cliprdr->temporaryDirectory))
661 length = ARRAYSIZE(cliprdr->temporaryDirectory) - 1;
664 CopyMemory(tempDirectory.szTempDir, cliprdr->temporaryDirectory, length);
665 tempDirectory.szTempDir[length] =
'\0';
666 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrTemporaryDirectory: %s",
667 cliprdr->temporaryDirectory);
668 IFCALLRET(context->TempDirectory, error, context, &tempDirectory);
671 WLog_Print(cliprdr->log, WLOG_ERROR,
"TempDirectory failed with error %" PRIu32
"!", error);
681static UINT cliprdr_server_receive_format_list(CliprdrServerContext* context,
wStream* s,
684 UINT error = CHANNEL_RC_OK;
686 WINPR_ASSERT(context);
687 WINPR_ASSERT(header);
690 .msgFlags = header->msgFlags,
691 .dataLen = header->dataLen },
693 .formats =
nullptr };
694 wLog* log = WLog_Get(TAG);
695 if ((error = cliprdr_read_format_list(log, s, &formatList, context->useLongFormatNames)))
698 WLog_Print(log, WLOG_DEBUG,
"ClientFormatList: numFormats: %" PRIu32
"", formatList.numFormats);
699 IFCALLRET(context->ClientFormatList, error, context, &formatList);
702 WLog_Print(log, WLOG_ERROR,
"ClientFormatList failed with error %" PRIu32
"!", error);
705 cliprdr_free_format_list(&formatList);
714static UINT cliprdr_server_receive_format_list_response(CliprdrServerContext* context,
wStream* s,
717 UINT error = CHANNEL_RC_OK;
719 WINPR_ASSERT(context);
720 WINPR_ASSERT(header);
723 WINPR_ASSERT(cliprdr);
726 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFormatListResponse");
729 CB_FORMAT_LIST_RESPONSE,
730 .msgFlags = header->msgFlags,
731 .dataLen = header->dataLen } };
732 IFCALLRET(context->ClientFormatListResponse, error, context, &formatListResponse);
735 WLog_Print(cliprdr->log, WLOG_ERROR,
736 "ClientFormatListResponse failed with error %" PRIu32
"!", error);
746static UINT cliprdr_server_receive_lock_clipdata(CliprdrServerContext* context,
wStream* s,
749 UINT error = CHANNEL_RC_OK;
751 WINPR_ASSERT(context);
752 WINPR_ASSERT(header);
755 WINPR_ASSERT(cliprdr);
757 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientLockClipData");
759 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
760 return ERROR_INVALID_DATA;
763 .msgFlags = header->msgFlags,
764 .dataLen = header->dataLen },
767 Stream_Read_UINT32(s, lockClipboardData.clipDataId);
768 IFCALLRET(context->ClientLockClipboardData, error, context, &lockClipboardData);
771 WLog_Print(cliprdr->log, WLOG_ERROR,
772 "ClientLockClipboardData failed with error %" PRIu32
"!", error);
782static UINT cliprdr_server_receive_unlock_clipdata(CliprdrServerContext* context,
wStream* s,
785 WINPR_ASSERT(context);
786 WINPR_ASSERT(header);
789 WINPR_ASSERT(cliprdr);
791 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientUnlockClipData");
794 .msgFlags = header->msgFlags,
795 .dataLen = header->dataLen },
798 UINT error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData);
802 IFCALLRET(context->ClientUnlockClipboardData, error, context, &unlockClipboardData);
805 WLog_Print(cliprdr->log, WLOG_ERROR,
806 "ClientUnlockClipboardData failed with error %" PRIu32
"!", error);
816static UINT cliprdr_server_receive_format_data_request(CliprdrServerContext* context,
wStream* s,
819 WINPR_ASSERT(context);
820 WINPR_ASSERT(header);
823 WINPR_ASSERT(cliprdr);
825 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFormatDataRequest");
828 .msgFlags = header->msgFlags,
829 .dataLen = header->dataLen },
830 .requestedFormatId = 0 };
832 UINT error = cliprdr_read_format_data_request(s, &formatDataRequest);
836 context->lastRequestedFormatId = formatDataRequest.requestedFormatId;
837 IFCALLRET(context->ClientFormatDataRequest, error, context, &formatDataRequest);
840 WLog_Print(cliprdr->log, WLOG_ERROR,
841 "ClientFormatDataRequest failed with error %" PRIu32
"!", error);
851static UINT cliprdr_server_receive_format_data_response(CliprdrServerContext* context,
wStream* s,
854 WINPR_ASSERT(context);
855 WINPR_ASSERT(header);
858 WINPR_ASSERT(cliprdr);
860 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFormatDataResponse");
863 CB_FORMAT_DATA_RESPONSE,
864 .msgFlags = header->msgFlags,
865 .dataLen = header->dataLen },
866 .requestedFormatData =
nullptr };
868 UINT error = cliprdr_read_format_data_response(s, &formatDataResponse);
872 IFCALLRET(context->ClientFormatDataResponse, error, context, &formatDataResponse);
875 WLog_Print(cliprdr->log, WLOG_ERROR,
876 "ClientFormatDataResponse failed with error %" PRIu32
"!", error);
886static UINT cliprdr_server_receive_filecontents_request(CliprdrServerContext* context,
wStream* s,
889 WINPR_ASSERT(context);
890 WINPR_ASSERT(header);
893 WINPR_ASSERT(cliprdr);
895 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFileContentsRequest");
898 .msgFlags = header->msgFlags,
899 .dataLen = header->dataLen },
906 .haveClipDataId = FALSE,
909 UINT error = cliprdr_read_file_contents_request(s, &request);
913 if (!context->hasHugeFileSupport)
915 if (request.nPositionHigh > 0)
916 return ERROR_INVALID_DATA;
917 if ((UINT64)request.nPositionLow + request.cbRequested > UINT32_MAX)
918 return ERROR_INVALID_DATA;
920 IFCALLRET(context->ClientFileContentsRequest, error, context, &request);
923 WLog_Print(cliprdr->log, WLOG_ERROR,
924 "ClientFileContentsRequest failed with error %" PRIu32
"!", error);
934static UINT cliprdr_server_receive_filecontents_response(CliprdrServerContext* context,
wStream* s,
937 UINT error = CHANNEL_RC_OK;
939 WINPR_ASSERT(context);
940 WINPR_ASSERT(header);
943 WINPR_ASSERT(cliprdr);
945 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFileContentsResponse");
948 .msgFlags = header->msgFlags,
949 .dataLen = header->dataLen },
952 .requestedData =
nullptr };
954 if ((error = cliprdr_read_file_contents_response(s, &response)))
957 IFCALLRET(context->ClientFileContentsResponse, error, context, &response);
960 WLog_Print(cliprdr->log, WLOG_ERROR,
961 "ClientFileContentsResponse failed with error %" PRIu32
"!", error);
971static UINT cliprdr_server_receive_pdu(CliprdrServerContext* context,
wStream* s,
976 WINPR_ASSERT(context);
977 WINPR_ASSERT(header);
980 WINPR_ASSERT(cliprdr);
983 char buffer1[64] = WINPR_C_ARRAY_INIT;
984 char buffer2[64] = WINPR_C_ARRAY_INIT;
985 WLog_Print(cliprdr->log, WLOG_DEBUG,
986 "CliprdrServerReceivePdu: msgType: %s, msgFlags: %s dataLen: %" PRIu32
"",
987 CB_MSG_TYPE_STRING(header->msgType, buffer1,
sizeof(buffer1)),
988 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2,
sizeof(buffer2)),
992 const size_t start = Stream_GetPosition(s);
993 switch (header->msgType)
996 error = cliprdr_server_receive_capabilities(context, s, header);
999 case CB_TEMP_DIRECTORY:
1000 error = cliprdr_server_receive_temporary_directory(context, s, header);
1003 case CB_FORMAT_LIST:
1004 error = cliprdr_server_receive_format_list(context, s, header);
1007 case CB_FORMAT_LIST_RESPONSE:
1008 error = cliprdr_server_receive_format_list_response(context, s, header);
1011 case CB_LOCK_CLIPDATA:
1012 error = cliprdr_server_receive_lock_clipdata(context, s, header);
1015 case CB_UNLOCK_CLIPDATA:
1016 error = cliprdr_server_receive_unlock_clipdata(context, s, header);
1019 case CB_FORMAT_DATA_REQUEST:
1020 error = cliprdr_server_receive_format_data_request(context, s, header);
1023 case CB_FORMAT_DATA_RESPONSE:
1024 error = cliprdr_server_receive_format_data_response(context, s, header);
1027 case CB_FILECONTENTS_REQUEST:
1028 error = cliprdr_server_receive_filecontents_request(context, s, header);
1031 case CB_FILECONTENTS_RESPONSE:
1032 error = cliprdr_server_receive_filecontents_response(context, s, header);
1036 error = ERROR_INVALID_DATA;
1037 WLog_Print(cliprdr->log, WLOG_ERROR,
"Unexpected clipboard PDU type: %" PRIu16
"",
1042 const size_t end = Stream_GetPosition(s);
1043 const size_t diff = end - start;
1044 if (diff != header->dataLen)
1046 char buffer1[64] = WINPR_C_ARRAY_INIT;
1048 TAG,
"[%s] Mismatch: header told length is %" PRIu32
", but actually read %" PRIuz,
1049 CB_MSG_TYPE_STRING(header->msgType, buffer1,
sizeof(buffer1)), header->dataLen, diff);
1052 const size_t rem = Stream_GetRemainingLength(s);
1055 char buffer1[64] = WINPR_C_ARRAY_INIT;
1056 char buffer2[64] = WINPR_C_ARRAY_INIT;
1058 TAG,
"msgType: %s, msgFlags: %s dataLen: %" PRIu32
": %" PRIuz
" bytes not parsed",
1059 CB_MSG_TYPE_STRING(header->msgType, buffer1,
sizeof(buffer1)),
1060 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2,
sizeof(buffer2)), header->dataLen, rem);
1070static UINT cliprdr_server_init(CliprdrServerContext* context)
1072 UINT32 generalFlags = 0;
1076 WINPR_ASSERT(context);
1079 WINPR_ASSERT(cliprdr);
1081 monitorReady.common.msgType = CB_MONITOR_READY;
1083 if (context->useLongFormatNames)
1084 generalFlags |= CB_USE_LONG_FORMAT_NAMES;
1086 if (context->streamFileClipEnabled)
1087 generalFlags |= CB_STREAM_FILECLIP_ENABLED;
1089 if (context->fileClipNoFilePaths)
1090 generalFlags |= CB_FILECLIP_NO_FILE_PATHS;
1092 if (context->canLockClipData)
1093 generalFlags |= CB_CAN_LOCK_CLIPDATA;
1095 if (context->hasHugeFileSupport)
1096 generalFlags |= CB_HUGE_FILE_SUPPORT_ENABLED;
1099 CB_CAPSTYPE_GENERAL,
1100 .capabilitySetLength =
1101 CB_CAPSTYPE_GENERAL_LEN,
1102 .version = CB_CAPS_VERSION_2,
1103 .generalFlags = generalFlags };
1107 .dataLen = 4 + CB_CAPSTYPE_GENERAL_LEN },
1108 .cCapabilitiesSets = 1,
1112 if ((error = context->ServerCapabilities(context, &capabilities)))
1114 WLog_Print(cliprdr->log, WLOG_ERROR,
"ServerCapabilities failed with error %" PRIu32
"!",
1119 if ((error = context->MonitorReady(context, &monitorReady)))
1121 WLog_Print(cliprdr->log, WLOG_ERROR,
"MonitorReady failed with error %" PRIu32
"!", error);
1133static UINT cliprdr_server_read(CliprdrServerContext* context)
1135 WINPR_ASSERT(context);
1138 WINPR_ASSERT(cliprdr);
1141 wStream* s = ChannelPduTracker_poll(cliprdr->channelPduTracker, &ok);
1144 return ERROR_INVALID_DATA;
1147 return CHANNEL_RC_OK;
1149 UINT ret = ERROR_INVALID_DATA;
1150 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 8))
1154 .msgType = Stream_Get_UINT16(s),
1155 .msgFlags = Stream_Get_UINT16(s),
1156 .dataLen = Stream_Get_UINT32(s)
1159 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, header.dataLen))
1162 ret = cliprdr_server_receive_pdu(context, s, &header);
1163 if (ret != CHANNEL_RC_OK)
1165 char buffer1[128] = WINPR_C_ARRAY_INIT;
1166 char buffer2[128] = WINPR_C_ARRAY_INIT;
1167 WLog_Print(cliprdr->log, WLOG_ERROR,
1168 "cliprdr_server_receive_pdu [%s|%s] failed with error code %" PRIu32
"!",
1169 CB_MSG_TYPE_STRING(header.msgType, buffer1,
sizeof(buffer1)),
1170 CB_MSG_FLAGS_STRING(header.msgFlags, buffer2,
sizeof(buffer2)), ret);
1178static DWORD WINAPI cliprdr_server_thread(LPVOID arg)
1182 HANDLE events[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1183 CliprdrServerContext* context = (CliprdrServerContext*)arg;
1184 UINT error = CHANNEL_RC_OK;
1186 WINPR_ASSERT(context);
1189 WINPR_ASSERT(cliprdr);
1191 HANDLE ChannelEvent = context->GetEventHandle(context);
1193 events[nCount++] = cliprdr->StopEvent;
1194 events[nCount++] = ChannelEvent;
1196 if (context->autoInitializationSequence)
1198 if ((error = cliprdr_server_init(context)))
1200 WLog_Print(cliprdr->log, WLOG_ERROR,
1201 "cliprdr_server_init failed with error %" PRIu32
"!", error);
1208 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1210 if (status == WAIT_FAILED)
1212 error = GetLastError();
1213 WLog_Print(cliprdr->log, WLOG_ERROR,
1214 "WaitForMultipleObjects failed with error %" PRIu32
"", error);
1218 status = WaitForSingleObject(cliprdr->StopEvent, 0);
1220 if (status == WAIT_FAILED)
1222 error = GetLastError();
1223 WLog_Print(cliprdr->log, WLOG_ERROR,
1224 "WaitForSingleObject failed with error %" PRIu32
"", error);
1228 if (status == WAIT_OBJECT_0)
1231 status = WaitForSingleObject(ChannelEvent, 0);
1233 if (status == WAIT_FAILED)
1235 error = GetLastError();
1236 WLog_Print(cliprdr->log, WLOG_ERROR,
1237 "WaitForSingleObject failed with error %" PRIu32
"", error);
1241 if (status == WAIT_OBJECT_0)
1243 if ((error = context->CheckEventHandle(context)))
1245 WLog_Print(cliprdr->log, WLOG_ERROR,
1246 "CheckEventHandle failed with error %" PRIu32
"!", error);
1254 if (error && context->rdpcontext)
1255 setChannelError(context->rdpcontext, error,
"cliprdr_server_thread reported an error");
1266static UINT cliprdr_server_open(CliprdrServerContext* context)
1268 WINPR_ASSERT(context);
1271 WINPR_ASSERT(cliprdr);
1273 PULONG pSessionId =
nullptr;
1274 DWORD BytesReturned = 0;
1276 if (!WTSQuerySessionInformationA(cliprdr->vcm, WTS_CURRENT_SESSION, WTSSessionId,
1277 (LPSTR*)&pSessionId, &BytesReturned))
1278 return CHANNEL_RC_BAD_CHANNEL;
1280 DWORD SessionId = (DWORD)*pSessionId;
1281 WTSFreeMemory(pSessionId);
1283 cliprdr->ChannelHandle =
1284 WTSVirtualChannelOpenEx(SessionId, CLIPRDR_SVC_CHANNEL_NAME, CHANNEL_OPTION_SHOW_PROTOCOL);
1285 if (!cliprdr->ChannelHandle)
1287 WLog_Print(cliprdr->log, WLOG_ERROR,
"WTSVirtualChannelOpen for cliprdr failed!");
1288 return ERROR_INTERNAL_ERROR;
1291 WINPR_ASSERT(cliprdr->channelPduTracker ==
nullptr);
1292 cliprdr->channelPduTracker = ChannelPduTracker_new(cliprdr->ChannelHandle);
1293 if (!cliprdr->channelPduTracker)
1295 WLog_Print(cliprdr->log, WLOG_ERROR,
"ChannelPduTracker_new for cliprdr failed!");
1296 return ERROR_INTERNAL_ERROR;
1299 cliprdr->ChannelEvent =
nullptr;
1301 void* buffer =
nullptr;
1303 if (WTSVirtualChannelQuery(cliprdr->ChannelHandle, WTSVirtualEventHandle, &buffer,
1306 if (BytesReturned !=
sizeof(HANDLE))
1308 WLog_Print(cliprdr->log, WLOG_ERROR,
"BytesReturned has not size of HANDLE!");
1309 return ERROR_INTERNAL_ERROR;
1312 cliprdr->ChannelEvent = *(HANDLE*)buffer;
1313 WTSFreeMemory(buffer);
1316 if (!cliprdr->ChannelEvent)
1318 WLog_Print(cliprdr->log, WLOG_ERROR,
"WTSVirtualChannelQuery for cliprdr failed!");
1319 return ERROR_INTERNAL_ERROR;
1322 return CHANNEL_RC_OK;
1330static UINT cliprdr_server_close(CliprdrServerContext* context)
1332 WINPR_ASSERT(context);
1335 WINPR_ASSERT(cliprdr);
1337 ChannelPduTracker_free(cliprdr->channelPduTracker);
1338 cliprdr->channelPduTracker =
nullptr;
1340 if (cliprdr->ChannelHandle)
1342 (void)WTSVirtualChannelClose(cliprdr->ChannelHandle);
1343 cliprdr->ChannelHandle =
nullptr;
1346 return CHANNEL_RC_OK;
1354static UINT cliprdr_server_start(CliprdrServerContext* context)
1358 WINPR_ASSERT(context);
1361 WINPR_ASSERT(cliprdr);
1363 if (!cliprdr->ChannelHandle)
1365 error = context->Open(context);
1368 WLog_Print(cliprdr->log, WLOG_ERROR,
"Open failed!");
1373 cliprdr->StopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1374 if (!cliprdr->StopEvent)
1376 WLog_Print(cliprdr->log, WLOG_ERROR,
"CreateEvent failed!");
1377 return ERROR_INTERNAL_ERROR;
1380 cliprdr->Thread = CreateThread(
nullptr, 0, cliprdr_server_thread, (
void*)context, 0,
nullptr);
1381 if (!cliprdr->Thread)
1383 WLog_Print(cliprdr->log, WLOG_ERROR,
"CreateThread failed!");
1384 (void)CloseHandle(cliprdr->StopEvent);
1385 cliprdr->StopEvent =
nullptr;
1386 return ERROR_INTERNAL_ERROR;
1389 return CHANNEL_RC_OK;
1397static UINT cliprdr_server_stop(CliprdrServerContext* context)
1399 UINT error = CHANNEL_RC_OK;
1401 WINPR_ASSERT(context);
1404 WINPR_ASSERT(cliprdr);
1406 if (cliprdr->StopEvent)
1408 (void)SetEvent(cliprdr->StopEvent);
1410 if (WaitForSingleObject(cliprdr->Thread, INFINITE) == WAIT_FAILED)
1412 error = GetLastError();
1413 WLog_Print(cliprdr->log, WLOG_ERROR,
1414 "WaitForSingleObject failed with error %" PRIu32
"", error);
1418 (void)CloseHandle(cliprdr->Thread);
1419 (void)CloseHandle(cliprdr->StopEvent);
1422 if (cliprdr->ChannelHandle)
1423 return context->Close(context);
1428static HANDLE cliprdr_server_get_event_handle(CliprdrServerContext* context)
1430 WINPR_ASSERT(context);
1433 WINPR_ASSERT(cliprdr);
1434 return cliprdr->ChannelEvent;
1442static UINT cliprdr_server_check_event_handle(CliprdrServerContext* context)
1444 return cliprdr_server_read(context);
1447CliprdrServerContext* cliprdr_server_context_new(HANDLE vcm)
1449 CliprdrServerContext* context = (CliprdrServerContext*)calloc(1,
sizeof(CliprdrServerContext));
1454 context->autoInitializationSequence = TRUE;
1455 context->Open = cliprdr_server_open;
1456 context->Close = cliprdr_server_close;
1457 context->Start = cliprdr_server_start;
1458 context->Stop = cliprdr_server_stop;
1459 context->GetEventHandle = cliprdr_server_get_event_handle;
1460 context->CheckEventHandle = cliprdr_server_check_event_handle;
1461 context->ServerCapabilities = cliprdr_server_capabilities;
1462 context->MonitorReady = cliprdr_server_monitor_ready;
1463 context->ServerFormatList = cliprdr_server_format_list;
1464 context->ServerFormatListResponse = cliprdr_server_format_list_response;
1465 context->ServerLockClipboardData = cliprdr_server_lock_clipboard_data;
1466 context->ServerUnlockClipboardData = cliprdr_server_unlock_clipboard_data;
1467 context->ServerFormatDataRequest = cliprdr_server_format_data_request;
1468 context->ServerFormatDataResponse = cliprdr_server_format_data_response;
1469 context->ServerFileContentsRequest = cliprdr_server_file_contents_request;
1470 context->ServerFileContentsResponse = cliprdr_server_file_contents_response;
1477 cliprdr->log = WLog_Get(TAG);
1483 cliprdr_server_context_free(context);
1487void cliprdr_server_context_free(CliprdrServerContext* context)
1492 free(context->handle);