23#include <freerdp/config.h>
39#include <linux/ppdev.h>
40#include <linux/parport.h>
44#include <winpr/assert.h>
45#include <winpr/synch.h>
46#include <winpr/thread.h>
47#include <winpr/stream.h>
48#include <winpr/collections.h>
49#include <winpr/interlocked.h>
51#include <freerdp/types.h>
52#include <freerdp/freerdp.h>
53#include <freerdp/constants.h>
54#include <freerdp/channels/rdpdr.h>
55#include <freerdp/channels/log.h>
56#include <freerdp/utils/rdpdr_utils.h>
58#define TAG CHANNELS_TAG("drive.client")
70 rdpContext* rdpcontext;
79static UINT parallel_process_irp_create(PARALLEL_DEVICE* parallel, IRP* irp)
82 UINT32 PathLength = 0;
84 WINPR_ASSERT(parallel);
87 if (!Stream_SafeSeek(irp->input, 28))
88 return ERROR_INVALID_DATA;
91 if (!Stream_CheckAndLogRequiredLength(TAG, irp->input, 4))
92 return ERROR_INVALID_DATA;
93 Stream_Read_UINT32(irp->input, PathLength);
94 if (PathLength <
sizeof(WCHAR))
95 return ERROR_INVALID_DATA;
96 const WCHAR* ptr = Stream_ConstPointer(irp->input);
97 if (!Stream_SafeSeek(irp->input, PathLength))
98 return ERROR_INVALID_DATA;
99 path = ConvertWCharNToUtf8Alloc(ptr, PathLength /
sizeof(WCHAR),
nullptr);
101 return CHANNEL_RC_NO_MEMORY;
103 if (parallel->file >= 0)
105 WLog_ERR(TAG,
"Duplicate create request for %s. Aborting.", path);
107 return ERROR_INVALID_DATA;
109 parallel->id = irp->devman->id_sequence++;
110 parallel->file = open(parallel->path, O_RDWR);
112 if (parallel->file < 0)
114 irp->IoStatus = STATUS_ACCESS_DENIED;
120 if (fcntl(parallel->file, F_SETFL, O_NONBLOCK) == -1)
125 Stream_Write_UINT32(irp->output, parallel->id);
126 Stream_Write_UINT8(irp->output, 0);
128 return CHANNEL_RC_OK;
136static UINT parallel_process_irp_close(PARALLEL_DEVICE* parallel, IRP* irp)
138 WINPR_ASSERT(parallel);
141 (void)close(parallel->file);
144 Stream_Zero(irp->output, 5);
145 return CHANNEL_RC_OK;
153static UINT parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
158 BYTE* buffer =
nullptr;
160 WINPR_ASSERT(parallel);
163 if (!Stream_CheckAndLogRequiredLength(TAG, irp->input, 12))
164 return ERROR_INVALID_DATA;
165 Stream_Read_UINT32(irp->input, Length);
166 Stream_Read_UINT64(irp->input, Offset);
170 if (parallel->file < 0)
172 WLog_ERR(TAG,
"Device not open, aborting");
173 return ERROR_DEVICE_NOT_AVAILABLE;
176 buffer = (BYTE*)calloc(Length,
sizeof(BYTE));
180 WLog_Print(parallel->log, WLOG_ERROR,
"malloc failed!");
181 return CHANNEL_RC_NO_MEMORY;
184 status = read(parallel->file, buffer, Length);
186 if ((status < 0) || (status > UINT32_MAX))
188 irp->IoStatus = STATUS_UNSUCCESSFUL;
195 Length = (UINT32)status;
198 Stream_Write_UINT32(irp->output, Length);
202 if (!Stream_EnsureRemainingCapacity(irp->output, Length))
204 WLog_Print(parallel->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
206 return CHANNEL_RC_NO_MEMORY;
209 Stream_Write(irp->output, buffer, Length);
213 return CHANNEL_RC_OK;
221static UINT parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp)
226 WINPR_ASSERT(parallel);
229 if (!Stream_CheckAndLogRequiredLength(TAG, irp->input, 12))
230 return ERROR_INVALID_DATA;
232 Stream_Read_UINT32(irp->input, Length);
233 Stream_Read_UINT64(irp->input, Offset);
236 if (!Stream_SafeSeek(irp->input, 20))
237 return ERROR_INVALID_DATA;
238 const void* ptr = Stream_ConstPointer(irp->input);
239 if (!Stream_SafeSeek(irp->input, Length))
240 return ERROR_INVALID_DATA;
242 if (parallel->file < 0)
244 WLog_ERR(TAG,
"Device not open, aborting");
245 return ERROR_DEVICE_NOT_AVAILABLE;
248 wStream sbuffer = WINPR_C_ARRAY_INIT;
249 wStream* sub = Stream_StaticConstInit(&sbuffer, ptr, Length);
250 while (Stream_GetRemainingLength(sub) > 0)
252 const size_t len = Stream_GetRemainingLength(sub);
253 const void* cptr = Stream_Pointer(sub);
254 const ssize_t status = write(parallel->file, cptr, len);
256 if ((status < 0) || ((
size_t)status > len))
258 irp->IoStatus = STATUS_UNSUCCESSFUL;
263 if (!Stream_SafeSeek(irp->input, WINPR_ASSERTING_INT_CAST(
size_t, status)))
264 return ERROR_INVALID_BLOCK_LENGTH;
267 Stream_Write_UINT32(irp->output, Length);
268 Stream_Write_UINT8(irp->output, 0);
269 return CHANNEL_RC_OK;
277static UINT parallel_process_irp_device_control(WINPR_ATTR_UNUSED PARALLEL_DEVICE* parallel,
280 WINPR_ASSERT(parallel);
283 Stream_Write_UINT32(irp->output, 0);
284 return CHANNEL_RC_OK;
293static UINT parallel_eval(UINT error, IRP* irp)
296 if (error == CHANNEL_RC_OK)
298 WINPR_ASSERT(irp->Complete);
299 return irp->Complete(irp);
302 WLog_ERR(TAG,
"IRP %s failed with %" PRIu32, rdpdr_irp_string(irp->MajorFunction), error);
303 WINPR_ASSERT(irp->Discard);
308static UINT parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
310 UINT error = ERROR_INTERNAL_ERROR;
312 WINPR_ASSERT(parallel);
315 switch (irp->MajorFunction)
318 error = parallel_process_irp_create(parallel, irp);
322 error = parallel_process_irp_close(parallel, irp);
326 error = parallel_process_irp_read(parallel, irp);
330 error = parallel_process_irp_write(parallel, irp);
333 case IRP_MJ_DEVICE_CONTROL:
334 error = parallel_process_irp_device_control(parallel, irp);
338 irp->IoStatus = STATUS_NOT_SUPPORTED;
339 error = CHANNEL_RC_OK;
343 error = parallel_eval(error, irp);
345 DWORD level = WLOG_TRACE;
349 WLog_Print(parallel->log, level,
350 "[%s|0x%08" PRIx32
"] completed with %s [0x%08" PRIx32
"] (IoStatus %s [0x%08" PRIx32
352 rdpdr_irp_string(irp->MajorFunction), irp->MajorFunction, WTSErrorToString(error),
353 error, NtStatus2Tag(irp->IoStatus), WINPR_CXX_COMPAT_CAST(UINT32, irp->IoStatus));
358static DWORD WINAPI parallel_thread_func(LPVOID arg)
360 PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)arg;
361 UINT error = CHANNEL_RC_OK;
363 WINPR_ASSERT(parallel);
366 if (!MessageQueue_Wait(parallel->queue))
368 WLog_Print(parallel->log, WLOG_ERROR,
"MessageQueue_Wait failed!");
369 error = ERROR_INTERNAL_ERROR;
373 wMessage message = WINPR_C_ARRAY_INIT;
374 if (!MessageQueue_Peek(parallel->queue, &message, TRUE))
376 WLog_Print(parallel->log, WLOG_ERROR,
"MessageQueue_Peek failed!");
377 error = ERROR_INTERNAL_ERROR;
381 if (message.id == WMQ_QUIT)
384 IRP* irp = (IRP*)message.wParam;
386 error = parallel_process_irp(parallel, irp);
389 WLog_Print(parallel->log, WLOG_ERROR,
390 "parallel_process_irp failed with error %" PRIu32
"!", error);
395 if (error && parallel->rdpcontext)
396 setChannelError(parallel->rdpcontext, error,
"parallel_thread_func reported an error");
407static UINT parallel_irp_request(DEVICE* device, IRP* irp)
409 PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device;
411 WINPR_ASSERT(parallel);
413 if (!MessageQueue_Post(parallel->queue,
nullptr, 0, (
void*)irp,
nullptr))
415 WLog_Print(parallel->log, WLOG_ERROR,
"MessageQueue_Post failed!");
417 return ERROR_INTERNAL_ERROR;
420 return CHANNEL_RC_OK;
428static UINT parallel_free_int(PARALLEL_DEVICE* parallel)
432 if (!MessageQueue_PostQuit(parallel->queue, 0) ||
433 (WaitForSingleObject(parallel->thread, INFINITE) == WAIT_FAILED))
435 const UINT error = GetLastError();
436 WLog_Print(parallel->log, WLOG_ERROR,
437 "WaitForSingleObject failed with error %" PRIu32
"!", error);
440 (void)CloseHandle(parallel->thread);
441 Stream_Free(parallel->device.data, TRUE);
442 MessageQueue_Free(parallel->queue);
445 return CHANNEL_RC_OK;
448static UINT parallel_free(DEVICE* device)
451 return parallel_free_int((PARALLEL_DEVICE*)device);
452 return CHANNEL_RC_OK;
455static void parallel_message_free(
void* obj)
463 IRP* irp = (IRP*)msg->wParam;
466 WINPR_ASSERT(irp->Discard);
476 UINT VCAPITYPE parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))
478 PARALLEL_DEVICE* parallel =
nullptr;
481 WINPR_ASSERT(pEntryPoints);
484 WINPR_ASSERT(device);
486 wLog* log = WLog_Get(TAG);
489 char* name = device->device.Name;
490 char* path = device->Path;
492 if (!name || (name[0] ==
'*') || !path)
495 WLog_Print(log, WLOG_WARN,
"Autodetection not implemented, no ports will be redirected");
496 return CHANNEL_RC_INITIALIZATION_ERROR;
499 if (name[0] && path[0])
501 parallel = (PARALLEL_DEVICE*)calloc(1,
sizeof(PARALLEL_DEVICE));
505 WLog_Print(log, WLOG_ERROR,
"calloc failed!");
506 return CHANNEL_RC_NO_MEMORY;
511 parallel->device.type = RDPDR_DTYP_PARALLEL;
512 parallel->device.name = name;
513 parallel->device.IRPRequest = parallel_irp_request;
514 parallel->device.Free = parallel_free;
515 parallel->rdpcontext = pEntryPoints->rdpcontext;
516 const size_t length = strlen(name);
517 parallel->device.data = Stream_New(
nullptr, length + 1);
519 if (!parallel->device.data)
521 WLog_Print(parallel->log, WLOG_ERROR,
"Stream_New failed!");
522 error = CHANNEL_RC_NO_MEMORY;
526 for (
size_t i = 0; i <= length; i++)
527 Stream_Write_INT8(parallel->device.data, name[i] < 0 ?
'_' : name[i]);
529 parallel->path = path;
530 parallel->queue = MessageQueue_New(
nullptr);
532 if (!parallel->queue)
534 WLog_Print(parallel->log, WLOG_ERROR,
"MessageQueue_New failed!");
535 error = CHANNEL_RC_NO_MEMORY;
539 wObject* obj = MessageQueue_Object(parallel->queue);
541 obj->fnObjectFree = parallel_message_free;
543 error = pEntryPoints->RegisterDevice(pEntryPoints->devman, ¶llel->device);
546 WLog_Print(parallel->log, WLOG_ERROR,
"RegisterDevice failed with error %" PRIu32
"!",
551 parallel->thread = CreateThread(
nullptr, 0, parallel_thread_func, parallel, 0,
nullptr);
552 if (!parallel->thread)
554 WLog_Print(parallel->log, WLOG_ERROR,
"CreateThread failed!");
555 error = ERROR_INTERNAL_ERROR;
560 return CHANNEL_RC_OK;
562 parallel_free_int(parallel);
This struct contains function pointer to initialize/free objects.