FreeRDP
Loading...
Searching...
No Matches
parallel_main.c
1
22
23#include <freerdp/config.h>
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <fcntl.h>
30#include <errno.h>
31
32#ifndef _WIN32
33#include <termios.h>
34#include <strings.h>
35#include <sys/ioctl.h>
36#endif
37
38#ifdef __LINUX__
39#include <linux/ppdev.h>
40#include <linux/parport.h>
41#endif
42
43#include <winpr/crt.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>
50
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>
57
58#define TAG CHANNELS_TAG("drive.client")
59
60typedef struct
61{
62 DEVICE device;
63
64 int file;
65 char* path;
66 UINT32 id;
67
68 HANDLE thread;
69 wMessageQueue* queue;
70 rdpContext* rdpcontext;
71 wLog* log;
72} PARALLEL_DEVICE;
73
79static UINT parallel_process_irp_create(PARALLEL_DEVICE* parallel, IRP* irp)
80{
81 char* path = nullptr;
82 UINT32 PathLength = 0;
83
84 WINPR_ASSERT(parallel);
85 WINPR_ASSERT(irp);
86
87 if (!Stream_SafeSeek(irp->input, 28))
88 return ERROR_INVALID_DATA;
89 /* DesiredAccess(4) AllocationSize(8), FileAttributes(4) */
90 /* SharedAccess(4) CreateDisposition(4), CreateOptions(4) */
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);
100 if (!path)
101 return CHANNEL_RC_NO_MEMORY;
102
103 if (parallel->file >= 0)
104 {
105 WLog_ERR(TAG, "Duplicate create request for %s. Aborting.", path);
106 free(path);
107 return ERROR_INVALID_DATA;
108 }
109 parallel->id = irp->devman->id_sequence++;
110 parallel->file = open(parallel->path, O_RDWR);
111
112 if (parallel->file < 0)
113 {
114 irp->IoStatus = STATUS_ACCESS_DENIED;
115 parallel->id = 0;
116 }
117 else
118 {
119 /* all read and write operations should be non-blocking */
120 if (fcntl(parallel->file, F_SETFL, O_NONBLOCK) == -1)
121 {
122 }
123 }
124
125 Stream_Write_UINT32(irp->output, parallel->id);
126 Stream_Write_UINT8(irp->output, 0);
127 free(path);
128 return CHANNEL_RC_OK;
129}
130
136static UINT parallel_process_irp_close(PARALLEL_DEVICE* parallel, IRP* irp)
137{
138 WINPR_ASSERT(parallel);
139 WINPR_ASSERT(irp);
140
141 (void)close(parallel->file);
142 parallel->file = -1;
143
144 Stream_Zero(irp->output, 5); /* Padding(5) */
145 return CHANNEL_RC_OK;
146}
147
153static UINT parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
154{
155 UINT32 Length = 0;
156 UINT64 Offset = 0;
157 ssize_t status = 0;
158 BYTE* buffer = nullptr;
159
160 WINPR_ASSERT(parallel);
161 WINPR_ASSERT(irp);
162
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);
167 (void)Offset; /* [MS-RDPESP] 3.2.5.1.4 Processing a Server Read Request Message
168 * ignored */
169
170 if (parallel->file < 0)
171 {
172 WLog_ERR(TAG, "Device not open, aborting");
173 return ERROR_DEVICE_NOT_AVAILABLE;
174 }
175
176 buffer = (BYTE*)calloc(Length, sizeof(BYTE));
177
178 if (!buffer)
179 {
180 WLog_Print(parallel->log, WLOG_ERROR, "malloc failed!");
181 return CHANNEL_RC_NO_MEMORY;
182 }
183
184 status = read(parallel->file, buffer, Length);
185
186 if ((status < 0) || (status > UINT32_MAX))
187 {
188 irp->IoStatus = STATUS_UNSUCCESSFUL;
189 free(buffer);
190 buffer = nullptr;
191 Length = 0;
192 }
193 else
194 {
195 Length = (UINT32)status;
196 }
197
198 Stream_Write_UINT32(irp->output, Length);
199
200 if (Length > 0)
201 {
202 if (!Stream_EnsureRemainingCapacity(irp->output, Length))
203 {
204 WLog_Print(parallel->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
205 free(buffer);
206 return CHANNEL_RC_NO_MEMORY;
207 }
208
209 Stream_Write(irp->output, buffer, Length);
210 }
211
212 free(buffer);
213 return CHANNEL_RC_OK;
214}
215
221static UINT parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp)
222{
223 UINT32 Length = 0;
224 UINT64 Offset = 0;
225
226 WINPR_ASSERT(parallel);
227 WINPR_ASSERT(irp);
228
229 if (!Stream_CheckAndLogRequiredLength(TAG, irp->input, 12))
230 return ERROR_INVALID_DATA;
231
232 Stream_Read_UINT32(irp->input, Length);
233 Stream_Read_UINT64(irp->input, Offset);
234 (void)Offset; /* [MS-RDPESP] 3.2.5.1.5 Processing a Server Write Request Message
235 * ignore offset */
236 if (!Stream_SafeSeek(irp->input, 20)) /* Padding */
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;
241
242 if (parallel->file < 0)
243 {
244 WLog_ERR(TAG, "Device not open, aborting");
245 return ERROR_DEVICE_NOT_AVAILABLE;
246 }
247
248 wStream sbuffer = WINPR_C_ARRAY_INIT;
249 wStream* sub = Stream_StaticConstInit(&sbuffer, ptr, Length);
250 while (Stream_GetRemainingLength(sub) > 0)
251 {
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);
255
256 if ((status < 0) || ((size_t)status > len))
257 {
258 irp->IoStatus = STATUS_UNSUCCESSFUL;
259 Length = 0;
260 break;
261 }
262
263 if (!Stream_SafeSeek(irp->input, WINPR_ASSERTING_INT_CAST(size_t, status)))
264 return ERROR_INVALID_BLOCK_LENGTH;
265 }
266
267 Stream_Write_UINT32(irp->output, Length);
268 Stream_Write_UINT8(irp->output, 0); /* Padding */
269 return CHANNEL_RC_OK;
270}
271
277static UINT parallel_process_irp_device_control(WINPR_ATTR_UNUSED PARALLEL_DEVICE* parallel,
278 IRP* irp)
279{
280 WINPR_ASSERT(parallel);
281 WINPR_ASSERT(irp);
282
283 Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength */
284 return CHANNEL_RC_OK;
285}
286
292
293static UINT parallel_eval(UINT error, IRP* irp)
294{
295 WINPR_ASSERT(irp);
296 if (error == CHANNEL_RC_OK)
297 {
298 WINPR_ASSERT(irp->Complete);
299 return irp->Complete(irp);
300 }
301
302 WLog_ERR(TAG, "IRP %s failed with %" PRIu32, rdpdr_irp_string(irp->MajorFunction), error);
303 WINPR_ASSERT(irp->Discard);
304 irp->Discard(irp);
305 return error;
306}
307
308static UINT parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
309{
310 UINT error = ERROR_INTERNAL_ERROR;
311
312 WINPR_ASSERT(parallel);
313 WINPR_ASSERT(irp);
314
315 switch (irp->MajorFunction)
316 {
317 case IRP_MJ_CREATE:
318 error = parallel_process_irp_create(parallel, irp);
319 break;
320
321 case IRP_MJ_CLOSE:
322 error = parallel_process_irp_close(parallel, irp);
323 break;
324
325 case IRP_MJ_READ:
326 error = parallel_process_irp_read(parallel, irp);
327 break;
328
329 case IRP_MJ_WRITE:
330 error = parallel_process_irp_write(parallel, irp);
331 break;
332
333 case IRP_MJ_DEVICE_CONTROL:
334 error = parallel_process_irp_device_control(parallel, irp);
335 break;
336
337 default:
338 irp->IoStatus = STATUS_NOT_SUPPORTED;
339 error = CHANNEL_RC_OK;
340 break;
341 }
342
343 error = parallel_eval(error, irp);
344
345 DWORD level = WLOG_TRACE;
346 if (error)
347 level = WLOG_WARN;
348
349 WLog_Print(parallel->log, level,
350 "[%s|0x%08" PRIx32 "] completed with %s [0x%08" PRIx32 "] (IoStatus %s [0x%08" PRIx32
351 "])",
352 rdpdr_irp_string(irp->MajorFunction), irp->MajorFunction, WTSErrorToString(error),
353 error, NtStatus2Tag(irp->IoStatus), WINPR_CXX_COMPAT_CAST(UINT32, irp->IoStatus));
354
355 return error;
356}
357
358static DWORD WINAPI parallel_thread_func(LPVOID arg)
359{
360 PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)arg;
361 UINT error = CHANNEL_RC_OK;
362
363 WINPR_ASSERT(parallel);
364 while (1)
365 {
366 if (!MessageQueue_Wait(parallel->queue))
367 {
368 WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_Wait failed!");
369 error = ERROR_INTERNAL_ERROR;
370 break;
371 }
372
373 wMessage message = WINPR_C_ARRAY_INIT;
374 if (!MessageQueue_Peek(parallel->queue, &message, TRUE))
375 {
376 WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_Peek failed!");
377 error = ERROR_INTERNAL_ERROR;
378 break;
379 }
380
381 if (message.id == WMQ_QUIT)
382 break;
383
384 IRP* irp = (IRP*)message.wParam;
385
386 error = parallel_process_irp(parallel, irp);
387 if (error)
388 {
389 WLog_Print(parallel->log, WLOG_ERROR,
390 "parallel_process_irp failed with error %" PRIu32 "!", error);
391 break;
392 }
393 }
394
395 if (error && parallel->rdpcontext)
396 setChannelError(parallel->rdpcontext, error, "parallel_thread_func reported an error");
397
398 ExitThread(error);
399 return error;
400}
401
407static UINT parallel_irp_request(DEVICE* device, IRP* irp)
408{
409 PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device;
410
411 WINPR_ASSERT(parallel);
412
413 if (!MessageQueue_Post(parallel->queue, nullptr, 0, (void*)irp, nullptr))
414 {
415 WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_Post failed!");
416 irp->Discard(irp);
417 return ERROR_INTERNAL_ERROR;
418 }
419
420 return CHANNEL_RC_OK;
421}
422
428static UINT parallel_free_int(PARALLEL_DEVICE* parallel)
429{
430 if (parallel)
431 {
432 if (!MessageQueue_PostQuit(parallel->queue, 0) ||
433 (WaitForSingleObject(parallel->thread, INFINITE) == WAIT_FAILED))
434 {
435 const UINT error = GetLastError();
436 WLog_Print(parallel->log, WLOG_ERROR,
437 "WaitForSingleObject failed with error %" PRIu32 "!", error);
438 }
439
440 (void)CloseHandle(parallel->thread);
441 Stream_Free(parallel->device.data, TRUE);
442 MessageQueue_Free(parallel->queue);
443 }
444 free(parallel);
445 return CHANNEL_RC_OK;
446}
447
448static UINT parallel_free(DEVICE* device)
449{
450 if (device)
451 return parallel_free_int((PARALLEL_DEVICE*)device);
452 return CHANNEL_RC_OK;
453}
454
455static void parallel_message_free(void* obj)
456{
457 wMessage* msg = obj;
458 if (!msg)
459 return;
460 if (msg->id != 0)
461 return;
462
463 IRP* irp = (IRP*)msg->wParam;
464 if (!irp)
465 return;
466 WINPR_ASSERT(irp->Discard);
467 irp->Discard(irp);
468}
469
475FREERDP_ENTRY_POINT(
476 UINT VCAPITYPE parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))
477{
478 PARALLEL_DEVICE* parallel = nullptr;
479 UINT error = 0;
480
481 WINPR_ASSERT(pEntryPoints);
482
483 RDPDR_PARALLEL* device = (RDPDR_PARALLEL*)pEntryPoints->device;
484 WINPR_ASSERT(device);
485
486 wLog* log = WLog_Get(TAG);
487 WINPR_ASSERT(log);
488
489 char* name = device->device.Name;
490 char* path = device->Path;
491
492 if (!name || (name[0] == '*') || !path)
493 {
494 /* TODO: implement auto detection of parallel ports */
495 WLog_Print(log, WLOG_WARN, "Autodetection not implemented, no ports will be redirected");
496 return CHANNEL_RC_INITIALIZATION_ERROR;
497 }
498
499 if (name[0] && path[0])
500 {
501 parallel = (PARALLEL_DEVICE*)calloc(1, sizeof(PARALLEL_DEVICE));
502
503 if (!parallel)
504 {
505 WLog_Print(log, WLOG_ERROR, "calloc failed!");
506 return CHANNEL_RC_NO_MEMORY;
507 }
508
509 parallel->file = -1;
510 parallel->log = log;
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);
518
519 if (!parallel->device.data)
520 {
521 WLog_Print(parallel->log, WLOG_ERROR, "Stream_New failed!");
522 error = CHANNEL_RC_NO_MEMORY;
523 goto error_out;
524 }
525
526 for (size_t i = 0; i <= length; i++)
527 Stream_Write_INT8(parallel->device.data, name[i] < 0 ? '_' : name[i]);
528
529 parallel->path = path;
530 parallel->queue = MessageQueue_New(nullptr);
531
532 if (!parallel->queue)
533 {
534 WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_New failed!");
535 error = CHANNEL_RC_NO_MEMORY;
536 goto error_out;
537 }
538
539 wObject* obj = MessageQueue_Object(parallel->queue);
540 WINPR_ASSERT(obj);
541 obj->fnObjectFree = parallel_message_free;
542
543 error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &parallel->device);
544 if (error)
545 {
546 WLog_Print(parallel->log, WLOG_ERROR, "RegisterDevice failed with error %" PRIu32 "!",
547 error);
548 goto error_out;
549 }
550
551 parallel->thread = CreateThread(nullptr, 0, parallel_thread_func, parallel, 0, nullptr);
552 if (!parallel->thread)
553 {
554 WLog_Print(parallel->log, WLOG_ERROR, "CreateThread failed!");
555 error = ERROR_INTERNAL_ERROR;
556 goto error_out;
557 }
558 }
559
560 return CHANNEL_RC_OK;
561error_out:
562 parallel_free_int(parallel);
563 return error;
564}
This struct contains function pointer to initialize/free objects.
Definition collections.h:52