22 #include <freerdp/config.h>
24 #include <winpr/crt.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"
87 WINPR_ASSERT(cliprdr);
89 pos = Stream_GetPosition(s);
90 if ((pos < 8) || (pos > UINT32_MAX))
96 dataLen = (UINT32)(pos - 8);
97 Stream_SetPosition(s, 4);
98 Stream_Write_UINT32(s, dataLen);
100 WINPR_ASSERT(pos <= UINT32_MAX);
101 status = WTSVirtualChannelWrite(cliprdr->ChannelHandle, Stream_BufferAs(s,
char), (UINT32)pos,
103 rc = status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
105 Stream_Free(s, TRUE);
114 static UINT cliprdr_server_capabilities(CliprdrServerContext* context,
119 WINPR_ASSERT(context);
120 WINPR_ASSERT(capabilities);
124 if (capabilities->common.msgType != CB_CLIP_CAPS)
125 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, capabilities->common.msgType);
127 if (capabilities->cCapabilitiesSets > UINT16_MAX)
129 WLog_ERR(TAG,
"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_ERR(TAG,
"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_WARN(TAG,
"Unknown capability set type %08" PRIx16, cap->capabilitySetType);
168 if (!Stream_SafeSeek(s, cap->capabilitySetLength))
170 WLog_ERR(TAG,
"short stream");
171 Stream_Free(s, TRUE);
172 return ERROR_NO_DATA;
177 WLog_DBG(TAG,
"ServerCapabilities");
178 return cliprdr_server_packet_send(cliprdr, s);
186 static UINT cliprdr_server_monitor_ready(CliprdrServerContext* context,
192 WINPR_ASSERT(context);
193 WINPR_ASSERT(monitorReady);
197 if (monitorReady->common.msgType != CB_MONITOR_READY)
198 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, monitorReady->common.msgType);
200 s = cliprdr_packet_new(CB_MONITOR_READY, monitorReady->common.msgFlags,
201 monitorReady->common.dataLen);
205 WLog_ERR(TAG,
"cliprdr_packet_new failed!");
206 return ERROR_INTERNAL_ERROR;
209 WLog_DBG(TAG,
"ServerMonitorReady");
210 return cliprdr_server_packet_send(cliprdr, s);
218 static UINT cliprdr_server_format_list(CliprdrServerContext* context,
224 WINPR_ASSERT(context);
225 WINPR_ASSERT(formatList);
229 s = cliprdr_packet_format_list_new(formatList, context->useLongFormatNames, FALSE);
232 WLog_ERR(TAG,
"cliprdr_packet_format_list_new failed!");
233 return ERROR_INTERNAL_ERROR;
236 WLog_DBG(TAG,
"ServerFormatList: numFormats: %" PRIu32
"", formatList->numFormats);
237 return cliprdr_server_packet_send(cliprdr, s);
246 cliprdr_server_format_list_response(CliprdrServerContext* context,
252 WINPR_ASSERT(context);
253 WINPR_ASSERT(formatListResponse);
256 if (formatListResponse->common.msgType != CB_FORMAT_LIST_RESPONSE)
257 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, formatListResponse->common.msgType);
259 s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags,
260 formatListResponse->common.dataLen);
264 WLog_ERR(TAG,
"cliprdr_packet_new failed!");
265 return ERROR_INTERNAL_ERROR;
268 WLog_DBG(TAG,
"ServerFormatListResponse");
269 return cliprdr_server_packet_send(cliprdr, s);
277 static UINT cliprdr_server_lock_clipboard_data(CliprdrServerContext* context,
283 WINPR_ASSERT(context);
284 WINPR_ASSERT(lockClipboardData);
287 if (lockClipboardData->common.msgType != CB_LOCK_CLIPDATA)
288 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, lockClipboardData->common.msgType);
290 s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
293 WLog_ERR(TAG,
"cliprdr_packet_lock_clipdata_new failed!");
294 return ERROR_INTERNAL_ERROR;
297 WLog_DBG(TAG,
"ServerLockClipboardData: clipDataId: 0x%08" PRIX32
"",
298 lockClipboardData->clipDataId);
299 return cliprdr_server_packet_send(cliprdr, s);
308 cliprdr_server_unlock_clipboard_data(CliprdrServerContext* context,
314 WINPR_ASSERT(context);
315 WINPR_ASSERT(unlockClipboardData);
318 if (unlockClipboardData->common.msgType != CB_UNLOCK_CLIPDATA)
319 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, unlockClipboardData->common.msgType);
321 s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
325 WLog_ERR(TAG,
"cliprdr_packet_unlock_clipdata_new failed!");
326 return ERROR_INTERNAL_ERROR;
329 WLog_DBG(TAG,
"ServerUnlockClipboardData: clipDataId: 0x%08" PRIX32
"",
330 unlockClipboardData->clipDataId);
331 return cliprdr_server_packet_send(cliprdr, s);
339 static UINT cliprdr_server_format_data_request(CliprdrServerContext* context,
345 WINPR_ASSERT(context);
346 WINPR_ASSERT(formatDataRequest);
349 if (formatDataRequest->common.msgType != CB_FORMAT_DATA_REQUEST)
350 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, formatDataRequest->common.msgType);
352 s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, formatDataRequest->common.msgFlags,
353 formatDataRequest->common.dataLen);
357 WLog_ERR(TAG,
"cliprdr_packet_new failed!");
358 return ERROR_INTERNAL_ERROR;
361 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId);
362 WLog_DBG(TAG,
"ClientFormatDataRequest");
363 return cliprdr_server_packet_send(cliprdr, s);
372 cliprdr_server_format_data_response(CliprdrServerContext* context,
378 WINPR_ASSERT(context);
379 WINPR_ASSERT(formatDataResponse);
383 if (formatDataResponse->common.msgType != CB_FORMAT_DATA_RESPONSE)
384 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, formatDataResponse->common.msgType);
386 s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
387 formatDataResponse->common.dataLen);
391 WLog_ERR(TAG,
"cliprdr_packet_new failed!");
392 return ERROR_INTERNAL_ERROR;
395 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
396 WLog_DBG(TAG,
"ServerFormatDataResponse");
397 return cliprdr_server_packet_send(cliprdr, s);
406 cliprdr_server_file_contents_request(CliprdrServerContext* context,
412 WINPR_ASSERT(context);
413 WINPR_ASSERT(fileContentsRequest);
417 if (fileContentsRequest->common.msgType != CB_FILECONTENTS_REQUEST)
418 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, fileContentsRequest->common.msgType);
420 s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
423 WLog_ERR(TAG,
"cliprdr_packet_file_contents_request_new failed!");
424 return ERROR_INTERNAL_ERROR;
427 WLog_DBG(TAG,
"ServerFileContentsRequest: streamId: 0x%08" PRIX32
"",
428 fileContentsRequest->streamId);
429 return cliprdr_server_packet_send(cliprdr, s);
438 cliprdr_server_file_contents_response(CliprdrServerContext* context,
444 WINPR_ASSERT(context);
445 WINPR_ASSERT(fileContentsResponse);
449 if (fileContentsResponse->common.msgType != CB_FILECONTENTS_RESPONSE)
450 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, fileContentsResponse->common.msgType);
452 s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
455 WLog_ERR(TAG,
"cliprdr_packet_file_contents_response_new failed!");
456 return ERROR_INTERNAL_ERROR;
459 WLog_DBG(TAG,
"ServerFileContentsResponse: streamId: 0x%08" PRIX32
"",
460 fileContentsResponse->streamId);
461 return cliprdr_server_packet_send(cliprdr, s);
469 static UINT cliprdr_server_receive_general_capability(CliprdrServerContext* context,
wStream* s,
472 WINPR_ASSERT(context);
473 WINPR_ASSERT(cap_set);
475 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
476 return ERROR_INVALID_DATA;
478 Stream_Read_UINT32(s, cap_set->version);
479 Stream_Read_UINT32(s, cap_set->generalFlags);
481 if (context->useLongFormatNames)
482 context->useLongFormatNames =
483 (cap_set->generalFlags & CB_USE_LONG_FORMAT_NAMES) ? TRUE : FALSE;
485 if (context->streamFileClipEnabled)
486 context->streamFileClipEnabled =
487 (cap_set->generalFlags & CB_STREAM_FILECLIP_ENABLED) ? TRUE : FALSE;
489 if (context->fileClipNoFilePaths)
490 context->fileClipNoFilePaths =
491 (cap_set->generalFlags & CB_FILECLIP_NO_FILE_PATHS) ? TRUE : FALSE;
493 if (context->canLockClipData)
494 context->canLockClipData = (cap_set->generalFlags & CB_CAN_LOCK_CLIPDATA) ? TRUE : FALSE;
496 if (context->hasHugeFileSupport)
497 context->hasHugeFileSupport =
498 (cap_set->generalFlags & CB_HUGE_FILE_SUPPORT_ENABLED) ? TRUE : FALSE;
500 return CHANNEL_RC_OK;
508 static UINT cliprdr_server_receive_capabilities(CliprdrServerContext* context,
wStream* s,
511 UINT16 capabilitySetType = 0;
512 UINT16 capabilitySetLength = 0;
513 UINT error = ERROR_INVALID_DATA;
514 size_t cap_sets_size = 0;
518 WINPR_ASSERT(context);
519 WINPR_UNUSED(header);
521 WLog_DBG(TAG,
"CliprdrClientCapabilities");
522 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
523 return ERROR_INVALID_DATA;
525 Stream_Read_UINT16(s, capabilities.cCapabilitiesSets);
526 Stream_Seek_UINT16(s);
528 for (
size_t index = 0; index < capabilities.cCapabilitiesSets; index++)
531 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
533 Stream_Read_UINT16(s, capabilitySetType);
534 Stream_Read_UINT16(s, capabilitySetLength);
536 cap_sets_size += capabilitySetLength;
538 if (cap_sets_size > 0)
539 tmp = realloc(capabilities.capabilitySets, cap_sets_size);
542 WLog_ERR(TAG,
"capabilities.capabilitySets realloc failed!");
543 free(capabilities.capabilitySets);
544 return CHANNEL_RC_NO_MEMORY;
549 capSet = &(capabilities.capabilitySets[index]);
551 capSet->capabilitySetType = capabilitySetType;
552 capSet->capabilitySetLength = capabilitySetLength;
554 switch (capSet->capabilitySetType)
556 case CB_CAPSTYPE_GENERAL:
557 error = cliprdr_server_receive_general_capability(
562 "cliprdr_server_receive_general_capability failed with error %" PRIu32
570 WLog_ERR(TAG,
"unknown cliprdr capability set: %" PRIu16
"",
571 capSet->capabilitySetType);
576 error = CHANNEL_RC_OK;
577 IFCALLRET(context->ClientCapabilities, error, context, &capabilities);
579 free(capabilities.capabilitySets);
588 static UINT cliprdr_server_receive_temporary_directory(CliprdrServerContext* context,
wStream* s,
594 UINT error = CHANNEL_RC_OK;
596 WINPR_ASSERT(context);
597 WINPR_UNUSED(header);
600 WINPR_ASSERT(cliprdr);
602 if (!Stream_CheckAndLogRequiredLength(TAG, s,
603 ARRAYSIZE(cliprdr->temporaryDirectory) *
sizeof(WCHAR)))
604 return CHANNEL_RC_NO_MEMORY;
606 const WCHAR* wszTempDir = Stream_ConstPointer(s);
608 if (wszTempDir[ARRAYSIZE(cliprdr->temporaryDirectory) - 1] != 0)
610 WLog_ERR(TAG,
"wszTempDir[259] was not 0");
611 return ERROR_INVALID_DATA;
614 if (ConvertWCharNToUtf8(wszTempDir, ARRAYSIZE(cliprdr->temporaryDirectory),
615 cliprdr->temporaryDirectory,
616 ARRAYSIZE(cliprdr->temporaryDirectory)) < 0)
618 WLog_ERR(TAG,
"failed to convert temporary directory name");
619 return ERROR_INVALID_DATA;
622 length = strnlen(cliprdr->temporaryDirectory, ARRAYSIZE(cliprdr->temporaryDirectory));
624 if (length >= ARRAYSIZE(cliprdr->temporaryDirectory))
625 length = ARRAYSIZE(cliprdr->temporaryDirectory) - 1;
627 CopyMemory(tempDirectory.szTempDir, cliprdr->temporaryDirectory, length);
628 tempDirectory.szTempDir[length] =
'\0';
629 WLog_DBG(TAG,
"CliprdrTemporaryDirectory: %s", cliprdr->temporaryDirectory);
630 IFCALLRET(context->TempDirectory, error, context, &tempDirectory);
633 WLog_ERR(TAG,
"TempDirectory failed with error %" PRIu32
"!", error);
643 static UINT cliprdr_server_receive_format_list(CliprdrServerContext* context,
wStream* s,
647 UINT error = CHANNEL_RC_OK;
649 WINPR_ASSERT(context);
650 WINPR_ASSERT(header);
652 formatList.common.msgType = CB_FORMAT_LIST;
653 formatList.common.msgFlags = header->msgFlags;
654 formatList.common.dataLen = header->dataLen;
656 if ((error = cliprdr_read_format_list(s, &formatList, context->useLongFormatNames)))
659 WLog_DBG(TAG,
"ClientFormatList: numFormats: %" PRIu32
"", formatList.numFormats);
660 IFCALLRET(context->ClientFormatList, error, context, &formatList);
663 WLog_ERR(TAG,
"ClientFormatList failed with error %" PRIu32
"!", error);
666 cliprdr_free_format_list(&formatList);
675 static UINT cliprdr_server_receive_format_list_response(CliprdrServerContext* context,
wStream* s,
679 UINT error = CHANNEL_RC_OK;
681 WINPR_ASSERT(context);
682 WINPR_ASSERT(header);
685 WLog_DBG(TAG,
"CliprdrClientFormatListResponse");
686 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
687 formatListResponse.common.msgFlags = header->msgFlags;
688 formatListResponse.common.dataLen = header->dataLen;
689 IFCALLRET(context->ClientFormatListResponse, error, context, &formatListResponse);
692 WLog_ERR(TAG,
"ClientFormatListResponse failed with error %" PRIu32
"!", error);
702 static UINT cliprdr_server_receive_lock_clipdata(CliprdrServerContext* context,
wStream* s,
706 UINT error = CHANNEL_RC_OK;
708 WINPR_ASSERT(context);
709 WINPR_ASSERT(header);
711 WLog_DBG(TAG,
"CliprdrClientLockClipData");
713 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
714 return ERROR_INVALID_DATA;
716 lockClipboardData.common.msgType = CB_LOCK_CLIPDATA;
717 lockClipboardData.common.msgFlags = header->msgFlags;
718 lockClipboardData.common.dataLen = header->dataLen;
719 Stream_Read_UINT32(s, lockClipboardData.clipDataId);
720 IFCALLRET(context->ClientLockClipboardData, error, context, &lockClipboardData);
723 WLog_ERR(TAG,
"ClientLockClipboardData failed with error %" PRIu32
"!", error);
733 static UINT cliprdr_server_receive_unlock_clipdata(CliprdrServerContext* context,
wStream* s,
737 UINT error = CHANNEL_RC_OK;
739 WINPR_ASSERT(context);
740 WINPR_ASSERT(header);
742 WLog_DBG(TAG,
"CliprdrClientUnlockClipData");
744 unlockClipboardData.common.msgType = CB_UNLOCK_CLIPDATA;
745 unlockClipboardData.common.msgFlags = header->msgFlags;
746 unlockClipboardData.common.dataLen = header->dataLen;
748 if ((error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData)))
751 IFCALLRET(context->ClientUnlockClipboardData, error, context, &unlockClipboardData);
754 WLog_ERR(TAG,
"ClientUnlockClipboardData failed with error %" PRIu32
"!", error);
764 static UINT cliprdr_server_receive_format_data_request(CliprdrServerContext* context,
wStream* s,
768 UINT error = CHANNEL_RC_OK;
770 WINPR_ASSERT(context);
771 WINPR_ASSERT(header);
773 WLog_DBG(TAG,
"CliprdrClientFormatDataRequest");
774 formatDataRequest.common.msgType = CB_FORMAT_DATA_REQUEST;
775 formatDataRequest.common.msgFlags = header->msgFlags;
776 formatDataRequest.common.dataLen = header->dataLen;
778 if ((error = cliprdr_read_format_data_request(s, &formatDataRequest)))
781 context->lastRequestedFormatId = formatDataRequest.requestedFormatId;
782 IFCALLRET(context->ClientFormatDataRequest, error, context, &formatDataRequest);
785 WLog_ERR(TAG,
"ClientFormatDataRequest failed with error %" PRIu32
"!", error);
795 static UINT cliprdr_server_receive_format_data_response(CliprdrServerContext* context,
wStream* s,
799 UINT error = CHANNEL_RC_OK;
801 WINPR_ASSERT(context);
802 WINPR_ASSERT(header);
804 WLog_DBG(TAG,
"CliprdrClientFormatDataResponse");
805 formatDataResponse.common.msgType = CB_FORMAT_DATA_RESPONSE;
806 formatDataResponse.common.msgFlags = header->msgFlags;
807 formatDataResponse.common.dataLen = header->dataLen;
809 if ((error = cliprdr_read_format_data_response(s, &formatDataResponse)))
812 IFCALLRET(context->ClientFormatDataResponse, error, context, &formatDataResponse);
815 WLog_ERR(TAG,
"ClientFormatDataResponse failed with error %" PRIu32
"!", error);
825 static UINT cliprdr_server_receive_filecontents_request(CliprdrServerContext* context,
wStream* s,
829 UINT error = CHANNEL_RC_OK;
831 WINPR_ASSERT(context);
832 WINPR_ASSERT(header);
834 WLog_DBG(TAG,
"CliprdrClientFileContentsRequest");
835 request.common.msgType = CB_FILECONTENTS_REQUEST;
836 request.common.msgFlags = header->msgFlags;
837 request.common.dataLen = header->dataLen;
839 if ((error = cliprdr_read_file_contents_request(s, &request)))
842 if (!context->hasHugeFileSupport)
844 if (request.nPositionHigh > 0)
845 return ERROR_INVALID_DATA;
846 if ((UINT64)request.nPositionLow + request.cbRequested > UINT32_MAX)
847 return ERROR_INVALID_DATA;
849 IFCALLRET(context->ClientFileContentsRequest, error, context, &request);
852 WLog_ERR(TAG,
"ClientFileContentsRequest failed with error %" PRIu32
"!", error);
862 static UINT cliprdr_server_receive_filecontents_response(CliprdrServerContext* context,
wStream* s,
866 UINT error = CHANNEL_RC_OK;
868 WINPR_ASSERT(context);
869 WINPR_ASSERT(header);
871 WLog_DBG(TAG,
"CliprdrClientFileContentsResponse");
873 response.common.msgType = CB_FILECONTENTS_RESPONSE;
874 response.common.msgFlags = header->msgFlags;
875 response.common.dataLen = header->dataLen;
877 if ((error = cliprdr_read_file_contents_response(s, &response)))
880 IFCALLRET(context->ClientFileContentsResponse, error, context, &response);
883 WLog_ERR(TAG,
"ClientFileContentsResponse failed with error %" PRIu32
"!", error);
893 static UINT cliprdr_server_receive_pdu(CliprdrServerContext* context,
wStream* s,
898 WINPR_ASSERT(context);
899 WINPR_ASSERT(header);
901 char buffer1[64] = { 0 };
902 char buffer2[64] = { 0 };
903 WLog_DBG(TAG,
"CliprdrServerReceivePdu: msgType: %s, msgFlags: %s dataLen: %" PRIu32
"",
904 CB_MSG_TYPE_STRING(header->msgType, buffer1,
sizeof(buffer1)),
905 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2,
sizeof(buffer2)), header->dataLen);
907 switch (header->msgType)
910 if ((error = cliprdr_server_receive_capabilities(context, s, header)))
911 WLog_ERR(TAG,
"cliprdr_server_receive_capabilities failed with error %" PRIu32
"!",
916 case CB_TEMP_DIRECTORY:
917 if ((error = cliprdr_server_receive_temporary_directory(context, s, header)))
919 "cliprdr_server_receive_temporary_directory failed with error %" PRIu32
926 if ((error = cliprdr_server_receive_format_list(context, s, header)))
927 WLog_ERR(TAG,
"cliprdr_server_receive_format_list failed with error %" PRIu32
"!",
932 case CB_FORMAT_LIST_RESPONSE:
933 if ((error = cliprdr_server_receive_format_list_response(context, s, header)))
935 "cliprdr_server_receive_format_list_response failed with error %" PRIu32
941 case CB_LOCK_CLIPDATA:
942 if ((error = cliprdr_server_receive_lock_clipdata(context, s, header)))
943 WLog_ERR(TAG,
"cliprdr_server_receive_lock_clipdata failed with error %" PRIu32
"!",
948 case CB_UNLOCK_CLIPDATA:
949 if ((error = cliprdr_server_receive_unlock_clipdata(context, s, header)))
951 "cliprdr_server_receive_unlock_clipdata failed with error %" PRIu32
"!",
956 case CB_FORMAT_DATA_REQUEST:
957 if ((error = cliprdr_server_receive_format_data_request(context, s, header)))
959 "cliprdr_server_receive_format_data_request failed with error %" PRIu32
965 case CB_FORMAT_DATA_RESPONSE:
966 if ((error = cliprdr_server_receive_format_data_response(context, s, header)))
968 "cliprdr_server_receive_format_data_response failed with error %" PRIu32
974 case CB_FILECONTENTS_REQUEST:
975 if ((error = cliprdr_server_receive_filecontents_request(context, s, header)))
977 "cliprdr_server_receive_filecontents_request failed with error %" PRIu32
983 case CB_FILECONTENTS_RESPONSE:
984 if ((error = cliprdr_server_receive_filecontents_response(context, s, header)))
986 "cliprdr_server_receive_filecontents_response failed with error %" PRIu32
993 error = ERROR_INVALID_DATA;
994 WLog_ERR(TAG,
"Unexpected clipboard PDU type: %" PRIu16
"", header->msgType);
1006 static UINT cliprdr_server_init(CliprdrServerContext* context)
1008 UINT32 generalFlags = 0;
1014 WINPR_ASSERT(context);
1016 monitorReady.common.msgType = CB_MONITOR_READY;
1017 capabilities.common.msgType = CB_CLIP_CAPS;
1019 if (context->useLongFormatNames)
1020 generalFlags |= CB_USE_LONG_FORMAT_NAMES;
1022 if (context->streamFileClipEnabled)
1023 generalFlags |= CB_STREAM_FILECLIP_ENABLED;
1025 if (context->fileClipNoFilePaths)
1026 generalFlags |= CB_FILECLIP_NO_FILE_PATHS;
1028 if (context->canLockClipData)
1029 generalFlags |= CB_CAN_LOCK_CLIPDATA;
1031 if (context->hasHugeFileSupport)
1032 generalFlags |= CB_HUGE_FILE_SUPPORT_ENABLED;
1034 capabilities.common.msgType = CB_CLIP_CAPS;
1035 capabilities.common.msgFlags = 0;
1036 capabilities.common.dataLen = 4 + CB_CAPSTYPE_GENERAL_LEN;
1037 capabilities.cCapabilitiesSets = 1;
1039 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1040 generalCapabilitySet.capabilitySetLength = CB_CAPSTYPE_GENERAL_LEN;
1041 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1042 generalCapabilitySet.generalFlags = generalFlags;
1044 if ((error = context->ServerCapabilities(context, &capabilities)))
1046 WLog_ERR(TAG,
"ServerCapabilities failed with error %" PRIu32
"!", error);
1050 if ((error = context->MonitorReady(context, &monitorReady)))
1052 WLog_ERR(TAG,
"MonitorReady failed with error %" PRIu32
"!", error);
1064 static UINT cliprdr_server_read(CliprdrServerContext* context)
1067 size_t position = 0;
1068 DWORD BytesToRead = 0;
1069 DWORD BytesReturned = 0;
1075 WINPR_ASSERT(context);
1078 WINPR_ASSERT(cliprdr);
1082 if (Stream_GetPosition(s) < CLIPRDR_HEADER_LENGTH)
1085 BytesToRead = (UINT32)(CLIPRDR_HEADER_LENGTH - Stream_GetPosition(s));
1086 status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1088 if (status == WAIT_FAILED)
1090 error = GetLastError();
1091 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
1095 if (status == WAIT_TIMEOUT)
1096 return CHANNEL_RC_OK;
1098 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s), BytesToRead,
1101 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
1102 return ERROR_INTERNAL_ERROR;
1105 Stream_Seek(s, BytesReturned);
1108 if (Stream_GetPosition(s) >= CLIPRDR_HEADER_LENGTH)
1110 position = Stream_GetPosition(s);
1111 Stream_SetPosition(s, 0);
1112 Stream_Read_UINT16(s, header.msgType);
1113 Stream_Read_UINT16(s, header.msgFlags);
1114 Stream_Read_UINT32(s, header.dataLen);
1116 if (!Stream_EnsureCapacity(s, (header.dataLen + CLIPRDR_HEADER_LENGTH)))
1118 WLog_ERR(TAG,
"Stream_EnsureCapacity failed!");
1119 return CHANNEL_RC_NO_MEMORY;
1122 Stream_SetPosition(s, position);
1124 if (Stream_GetPosition(s) < (header.dataLen + CLIPRDR_HEADER_LENGTH))
1128 (UINT32)((header.dataLen + CLIPRDR_HEADER_LENGTH) - Stream_GetPosition(s));
1129 status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1131 if (status == WAIT_FAILED)
1133 error = GetLastError();
1134 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
1138 if (status == WAIT_TIMEOUT)
1139 return CHANNEL_RC_OK;
1141 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s), BytesToRead,
1144 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
1145 return ERROR_INTERNAL_ERROR;
1148 Stream_Seek(s, BytesReturned);
1151 if (Stream_GetPosition(s) >= (header.dataLen + CLIPRDR_HEADER_LENGTH))
1153 Stream_SetPosition(s, (header.dataLen + CLIPRDR_HEADER_LENGTH));
1154 Stream_SealLength(s);
1155 Stream_SetPosition(s, CLIPRDR_HEADER_LENGTH);
1157 if ((error = cliprdr_server_receive_pdu(context, s, &header)))
1159 WLog_ERR(TAG,
"cliprdr_server_receive_pdu failed with error code %" PRIu32
"!",
1164 Stream_SetPosition(s, 0);
1166 status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1168 if (status == WAIT_FAILED)
1170 error = GetLastError();
1171 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
1175 if (status == WAIT_TIMEOUT)
1176 return CHANNEL_RC_OK;
1181 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s), BytesToRead,
1184 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
1185 return ERROR_INTERNAL_ERROR;
1188 if (BytesReturned == 4)
1190 Stream_Read_UINT16(s, header.msgType);
1191 Stream_Read_UINT16(s, header.msgFlags);
1193 if (!header.msgType)
1196 Stream_SetPosition(s, 0);
1201 Stream_Seek(s, BytesReturned);
1206 return CHANNEL_RC_OK;
1209 static DWORD WINAPI cliprdr_server_thread(LPVOID arg)
1213 HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 };
1214 HANDLE ChannelEvent = NULL;
1215 CliprdrServerContext* context = (CliprdrServerContext*)arg;
1217 UINT error = CHANNEL_RC_OK;
1219 WINPR_ASSERT(context);
1222 WINPR_ASSERT(cliprdr);
1224 ChannelEvent = context->GetEventHandle(context);
1226 events[nCount++] = cliprdr->StopEvent;
1227 events[nCount++] = ChannelEvent;
1229 if (context->autoInitializationSequence)
1231 if ((error = cliprdr_server_init(context)))
1233 WLog_ERR(TAG,
"cliprdr_server_init failed with error %" PRIu32
"!", error);
1240 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1242 if (status == WAIT_FAILED)
1244 error = GetLastError();
1245 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
1249 status = WaitForSingleObject(cliprdr->StopEvent, 0);
1251 if (status == WAIT_FAILED)
1253 error = GetLastError();
1254 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
1258 if (status == WAIT_OBJECT_0)
1261 status = WaitForSingleObject(ChannelEvent, 0);
1263 if (status == WAIT_FAILED)
1265 error = GetLastError();
1266 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
1270 if (status == WAIT_OBJECT_0)
1272 if ((error = context->CheckEventHandle(context)))
1274 WLog_ERR(TAG,
"CheckEventHandle failed with error %" PRIu32
"!", error);
1282 if (error && context->rdpcontext)
1283 setChannelError(context->rdpcontext, error,
"cliprdr_server_thread reported an error");
1294 static UINT cliprdr_server_open(CliprdrServerContext* context)
1296 void* buffer = NULL;
1297 DWORD BytesReturned = 0;
1300 WINPR_ASSERT(context);
1303 WINPR_ASSERT(cliprdr);
1305 cliprdr->ChannelHandle =
1306 WTSVirtualChannelOpen(cliprdr->vcm, WTS_CURRENT_SESSION, CLIPRDR_SVC_CHANNEL_NAME);
1308 if (!cliprdr->ChannelHandle)
1310 WLog_ERR(TAG,
"WTSVirtualChannelOpen for cliprdr failed!");
1311 return ERROR_INTERNAL_ERROR;
1314 cliprdr->ChannelEvent = NULL;
1316 if (WTSVirtualChannelQuery(cliprdr->ChannelHandle, WTSVirtualEventHandle, &buffer,
1319 if (BytesReturned !=
sizeof(HANDLE))
1321 WLog_ERR(TAG,
"BytesReturned has not size of HANDLE!");
1322 return ERROR_INTERNAL_ERROR;
1325 CopyMemory(&(cliprdr->ChannelEvent), buffer,
sizeof(HANDLE));
1326 WTSFreeMemory(buffer);
1329 if (!cliprdr->ChannelEvent)
1331 WLog_ERR(TAG,
"WTSVirtualChannelQuery for cliprdr failed!");
1332 return ERROR_INTERNAL_ERROR;
1335 return CHANNEL_RC_OK;
1343 static UINT cliprdr_server_close(CliprdrServerContext* context)
1347 WINPR_ASSERT(context);
1350 WINPR_ASSERT(cliprdr);
1352 if (cliprdr->ChannelHandle)
1354 (void)WTSVirtualChannelClose(cliprdr->ChannelHandle);
1355 cliprdr->ChannelHandle = NULL;
1358 return CHANNEL_RC_OK;
1366 static UINT cliprdr_server_start(CliprdrServerContext* context)
1371 WINPR_ASSERT(context);
1374 WINPR_ASSERT(cliprdr);
1376 if (!cliprdr->ChannelHandle)
1378 if ((error = context->Open(context)))
1380 WLog_ERR(TAG,
"Open failed!");
1385 if (!(cliprdr->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
1387 WLog_ERR(TAG,
"CreateEvent failed!");
1388 return ERROR_INTERNAL_ERROR;
1391 if (!(cliprdr->Thread = CreateThread(NULL, 0, cliprdr_server_thread, (
void*)context, 0, NULL)))
1393 WLog_ERR(TAG,
"CreateThread failed!");
1394 (void)CloseHandle(cliprdr->StopEvent);
1395 cliprdr->StopEvent = NULL;
1396 return ERROR_INTERNAL_ERROR;
1399 return CHANNEL_RC_OK;
1407 static UINT cliprdr_server_stop(CliprdrServerContext* context)
1409 UINT error = CHANNEL_RC_OK;
1412 WINPR_ASSERT(context);
1415 WINPR_ASSERT(cliprdr);
1417 if (cliprdr->StopEvent)
1419 (void)SetEvent(cliprdr->StopEvent);
1421 if (WaitForSingleObject(cliprdr->Thread, INFINITE) == WAIT_FAILED)
1423 error = GetLastError();
1424 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
1428 (void)CloseHandle(cliprdr->Thread);
1429 (void)CloseHandle(cliprdr->StopEvent);
1432 if (cliprdr->ChannelHandle)
1433 return context->Close(context);
1438 static HANDLE cliprdr_server_get_event_handle(CliprdrServerContext* context)
1442 WINPR_ASSERT(context);
1445 WINPR_ASSERT(cliprdr);
1446 return cliprdr->ChannelEvent;
1454 static UINT cliprdr_server_check_event_handle(CliprdrServerContext* context)
1456 return cliprdr_server_read(context);
1459 CliprdrServerContext* cliprdr_server_context_new(HANDLE vcm)
1462 CliprdrServerContext* context = (CliprdrServerContext*)calloc(1,
sizeof(CliprdrServerContext));
1466 context->autoInitializationSequence = TRUE;
1467 context->Open = cliprdr_server_open;
1468 context->Close = cliprdr_server_close;
1469 context->Start = cliprdr_server_start;
1470 context->Stop = cliprdr_server_stop;
1471 context->GetEventHandle = cliprdr_server_get_event_handle;
1472 context->CheckEventHandle = cliprdr_server_check_event_handle;
1473 context->ServerCapabilities = cliprdr_server_capabilities;
1474 context->MonitorReady = cliprdr_server_monitor_ready;
1475 context->ServerFormatList = cliprdr_server_format_list;
1476 context->ServerFormatListResponse = cliprdr_server_format_list_response;
1477 context->ServerLockClipboardData = cliprdr_server_lock_clipboard_data;
1478 context->ServerUnlockClipboardData = cliprdr_server_unlock_clipboard_data;
1479 context->ServerFormatDataRequest = cliprdr_server_format_data_request;
1480 context->ServerFormatDataResponse = cliprdr_server_format_data_response;
1481 context->ServerFileContentsRequest = cliprdr_server_file_contents_request;
1482 context->ServerFileContentsResponse = cliprdr_server_file_contents_response;
1488 cliprdr->s = Stream_New(NULL, 4096);
1492 WLog_ERR(TAG,
"Stream_New failed!");
1493 free(context->handle);
1500 WLog_ERR(TAG,
"calloc failed!");
1509 void cliprdr_server_context_free(CliprdrServerContext* context)
1520 Stream_Free(cliprdr->s, TRUE);
1523 free(context->handle);