/src/FreeRDP/channels/serial/client/serial_main.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * FreeRDP: A Remote Desktop Protocol Implementation  | 
3  |  |  * Serial Port Device Service Virtual Channel  | 
4  |  |  *  | 
5  |  |  * Copyright 2011 O.S. Systems Software Ltda.  | 
6  |  |  * Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>  | 
7  |  |  * Copyright 2014 Hewlett-Packard Development Company, L.P.  | 
8  |  |  *  | 
9  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
10  |  |  * you may not use this file except in compliance with the License.  | 
11  |  |  * You may obtain a copy of the License at  | 
12  |  |  *  | 
13  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
14  |  |  *  | 
15  |  |  * Unless required by applicable law or agreed to in writing, software  | 
16  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
17  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
18  |  |  * See the License for the specific language governing permissions and  | 
19  |  |  * limitations under the License.  | 
20  |  |  */  | 
21  |  |  | 
22  |  | #include <freerdp/config.h>  | 
23  |  |  | 
24  |  | #include <errno.h>  | 
25  |  | #include <stdio.h>  | 
26  |  | #include <stdint.h>  | 
27  |  | #include <stdlib.h>  | 
28  |  | #include <string.h>  | 
29  |  |  | 
30  |  | #include <winpr/collections.h>  | 
31  |  | #include <winpr/comm.h>  | 
32  |  | #include <winpr/crt.h>  | 
33  |  | #include <winpr/stream.h>  | 
34  |  | #include <winpr/synch.h>  | 
35  |  | #include <winpr/thread.h>  | 
36  |  | #include <winpr/wlog.h>  | 
37  |  | #include <winpr/assert.h>  | 
38  |  |  | 
39  |  | #include <freerdp/freerdp.h>  | 
40  |  | #include <freerdp/channels/rdpdr.h>  | 
41  |  | #include <freerdp/channels/log.h>  | 
42  |  | #include <freerdp/utils/rdpdr_utils.h>  | 
43  |  |  | 
44  | 0  | #define TAG CHANNELS_TAG("serial.client") | 
45  |  |  | 
46  | 0  | #define MAX_IRP_THREADS 5  | 
47  |  |  | 
48  |  | typedef struct  | 
49  |  | { | 
50  |  |   DEVICE device;  | 
51  |  |   BOOL permissive;  | 
52  |  |   SERIAL_DRIVER_ID ServerSerialDriverId;  | 
53  |  |   HANDLE* hComm;  | 
54  |  |  | 
55  |  |   wLog* log;  | 
56  |  |   HANDLE MainThread;  | 
57  |  |   wMessageQueue* MainIrpQueue;  | 
58  |  |  | 
59  |  |   /* one thread per pending IRP and indexed according their CompletionId */  | 
60  |  |   wListDictionary* IrpThreads;  | 
61  |  |   CRITICAL_SECTION TerminatingIrpThreadsLock;  | 
62  |  |   rdpContext* rdpcontext;  | 
63  |  | } SERIAL_DEVICE;  | 
64  |  |  | 
65  |  | typedef struct  | 
66  |  | { | 
67  |  |   SERIAL_DEVICE* serial;  | 
68  |  |   IRP* irp;  | 
69  |  | } IRP_THREAD_DATA;  | 
70  |  |  | 
71  |  | static void close_terminated_irp_thread_handles(SERIAL_DEVICE* serial, BOOL forceClose);  | 
72  |  | static UINT32 GetLastErrorToIoStatus(SERIAL_DEVICE* serial)  | 
73  | 0  | { | 
74  |  |   /* http://msdn.microsoft.com/en-us/library/ff547466%28v=vs.85%29.aspx#generic_status_values_for_serial_device_control_requests  | 
75  |  |    */  | 
76  | 0  |   switch (GetLastError())  | 
77  | 0  |   { | 
78  | 0  |     case ERROR_BAD_DEVICE:  | 
79  | 0  |       return STATUS_INVALID_DEVICE_REQUEST;  | 
80  |  |  | 
81  | 0  |     case ERROR_CALL_NOT_IMPLEMENTED:  | 
82  | 0  |       return STATUS_NOT_IMPLEMENTED;  | 
83  |  |  | 
84  | 0  |     case ERROR_CANCELLED:  | 
85  | 0  |       return STATUS_CANCELLED;  | 
86  |  |  | 
87  | 0  |     case ERROR_INSUFFICIENT_BUFFER:  | 
88  | 0  |       return STATUS_BUFFER_TOO_SMALL; /* NB: STATUS_BUFFER_SIZE_TOO_SMALL not defined  */  | 
89  |  |  | 
90  | 0  |     case ERROR_INVALID_DEVICE_OBJECT_PARAMETER: /* eg: SerCx2.sys' _purge() */  | 
91  | 0  |       return STATUS_INVALID_DEVICE_STATE;  | 
92  |  |  | 
93  | 0  |     case ERROR_INVALID_HANDLE:  | 
94  | 0  |       return STATUS_INVALID_DEVICE_REQUEST;  | 
95  |  |  | 
96  | 0  |     case ERROR_INVALID_PARAMETER:  | 
97  | 0  |       return STATUS_INVALID_PARAMETER;  | 
98  |  |  | 
99  | 0  |     case ERROR_IO_DEVICE:  | 
100  | 0  |       return STATUS_IO_DEVICE_ERROR;  | 
101  |  |  | 
102  | 0  |     case ERROR_IO_PENDING:  | 
103  | 0  |       return STATUS_PENDING;  | 
104  |  |  | 
105  | 0  |     case ERROR_NOT_SUPPORTED:  | 
106  | 0  |       return STATUS_NOT_SUPPORTED;  | 
107  |  |  | 
108  | 0  |     case ERROR_TIMEOUT:  | 
109  | 0  |       return STATUS_TIMEOUT;  | 
110  |  |       /* no default */  | 
111  | 0  |   }  | 
112  |  |  | 
113  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "unexpected last-error: 0x%08" PRIX32 "", GetLastError());  | 
114  | 0  |   return STATUS_UNSUCCESSFUL;  | 
115  | 0  | }  | 
116  |  |  | 
117  |  | static UINT serial_process_irp_create(SERIAL_DEVICE* serial, IRP* irp)  | 
118  | 0  | { | 
119  | 0  |   DWORD DesiredAccess = 0;  | 
120  | 0  |   DWORD SharedAccess = 0;  | 
121  | 0  |   DWORD CreateDisposition = 0;  | 
122  | 0  |   UINT32 PathLength = 0;  | 
123  |  | 
  | 
124  | 0  |   WINPR_ASSERT(serial);  | 
125  | 0  |   WINPR_ASSERT(irp);  | 
126  |  |  | 
127  | 0  |   if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32))  | 
128  | 0  |     return ERROR_INVALID_DATA;  | 
129  |  |  | 
130  | 0  |   Stream_Read_UINT32(irp->input, DesiredAccess);     /* DesiredAccess (4 bytes) */  | 
131  | 0  |   Stream_Seek_UINT64(irp->input);                    /* AllocationSize (8 bytes) */  | 
132  | 0  |   Stream_Seek_UINT32(irp->input);                    /* FileAttributes (4 bytes) */  | 
133  | 0  |   Stream_Read_UINT32(irp->input, SharedAccess);      /* SharedAccess (4 bytes) */  | 
134  | 0  |   Stream_Read_UINT32(irp->input, CreateDisposition); /* CreateDisposition (4 bytes) */  | 
135  | 0  |   Stream_Seek_UINT32(irp->input);                    /* CreateOptions (4 bytes) */  | 
136  | 0  |   Stream_Read_UINT32(irp->input, PathLength);        /* PathLength (4 bytes) */  | 
137  |  | 
  | 
138  | 0  |   if (!Stream_SafeSeek(irp->input, PathLength)) /* Path (variable) */  | 
139  | 0  |     return ERROR_INVALID_DATA;  | 
140  |  |  | 
141  | 0  |   WINPR_ASSERT(PathLength == 0); /* MS-RDPESP 2.2.2.2 */  | 
142  | 0  | #ifndef _WIN32  | 
143  |  |   /* Windows 2012 server sends on a first call :  | 
144  |  |    *     DesiredAccess     = 0x00100080: SYNCHRONIZE | FILE_READ_ATTRIBUTES  | 
145  |  |    *     SharedAccess      = 0x00000007: FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ  | 
146  |  |    *     CreateDisposition = 0x00000001: CREATE_NEW  | 
147  |  |    *  | 
148  |  |    * then Windows 2012 sends :  | 
149  |  |    *     DesiredAccess     = 0x00120089: SYNCHRONIZE | READ_CONTROL | FILE_READ_ATTRIBUTES |  | 
150  |  |    * FILE_READ_EA | FILE_READ_DATA SharedAccess      = 0x00000007: FILE_SHARE_DELETE |  | 
151  |  |    * FILE_SHARE_WRITE | FILE_SHARE_READ CreateDisposition = 0x00000001: CREATE_NEW  | 
152  |  |    *  | 
153  |  |    * WINPR_ASSERT(DesiredAccess == (GENERIC_READ | GENERIC_WRITE));  | 
154  |  |    * WINPR_ASSERT(SharedAccess == 0);  | 
155  |  |    * WINPR_ASSERT(CreateDisposition == OPEN_EXISTING);  | 
156  |  |    *  | 
157  |  |    */  | 
158  | 0  |   WLog_Print(serial->log, WLOG_DEBUG,  | 
159  | 0  |              "DesiredAccess: 0x%" PRIX32 ", SharedAccess: 0x%" PRIX32  | 
160  | 0  |              ", CreateDisposition: 0x%" PRIX32 "",  | 
161  | 0  |              DesiredAccess, SharedAccess, CreateDisposition);  | 
162  |  |   /* FIXME: As of today only the flags below are supported by CommCreateFileA: */  | 
163  | 0  |   DesiredAccess = GENERIC_READ | GENERIC_WRITE;  | 
164  | 0  |   SharedAccess = 0;  | 
165  | 0  |   CreateDisposition = OPEN_EXISTING;  | 
166  | 0  | #endif  | 
167  | 0  |   serial->hComm =  | 
168  | 0  |       CreateFile(serial->device.name, DesiredAccess, SharedAccess, NULL, /* SecurityAttributes */  | 
169  | 0  |                  CreateDisposition, 0,                                   /* FlagsAndAttributes */  | 
170  | 0  |                  NULL);                                                  /* TemplateFile */  | 
171  |  | 
  | 
172  | 0  |   if (!serial->hComm || (serial->hComm == INVALID_HANDLE_VALUE))  | 
173  | 0  |   { | 
174  | 0  |     WLog_Print(serial->log, WLOG_WARN, "CreateFile failure: %s last-error: 0x%08" PRIX32 "",  | 
175  | 0  |                serial->device.name, GetLastError());  | 
176  | 0  |     irp->IoStatus = STATUS_UNSUCCESSFUL;  | 
177  | 0  |     goto error_handle;  | 
178  | 0  |   }  | 
179  |  |  | 
180  | 0  |   _comm_setServerSerialDriver(serial->hComm, serial->ServerSerialDriverId);  | 
181  | 0  |   _comm_set_permissive(serial->hComm, serial->permissive);  | 
182  |  |   /* NOTE: binary mode/raw mode required for the redirection. On  | 
183  |  |    * Linux, CommCreateFileA forces this setting.  | 
184  |  |    */  | 
185  |  |   /* ZeroMemory(&dcb, sizeof(DCB)); */  | 
186  |  |   /* dcb.DCBlength = sizeof(DCB); */  | 
187  |  |   /* GetCommState(serial->hComm, &dcb); */  | 
188  |  |   /* dcb.fBinary = TRUE; */  | 
189  |  |   /* SetCommState(serial->hComm, &dcb); */  | 
190  | 0  |   WINPR_ASSERT(irp->FileId == 0);  | 
191  | 0  |   irp->FileId = irp->devman->id_sequence++; /* FIXME: why not ((WINPR_COMM*)hComm)->fd? */  | 
192  | 0  |   irp->IoStatus = STATUS_SUCCESS;  | 
193  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "%s (DeviceId: %" PRIu32 ", FileId: %" PRIu32 ") created.",  | 
194  | 0  |              serial->device.name, irp->device->id, irp->FileId);  | 
195  | 0  | error_handle:  | 
196  | 0  |   Stream_Write_UINT32(irp->output, irp->FileId); /* FileId (4 bytes) */  | 
197  | 0  |   Stream_Write_UINT8(irp->output, 0);            /* Information (1 byte) */  | 
198  | 0  |   return CHANNEL_RC_OK;  | 
199  | 0  | }  | 
200  |  |  | 
201  |  | static UINT serial_process_irp_close(SERIAL_DEVICE* serial, IRP* irp)  | 
202  | 0  | { | 
203  | 0  |   WINPR_ASSERT(serial);  | 
204  | 0  |   WINPR_ASSERT(irp);  | 
205  |  |  | 
206  | 0  |   if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32))  | 
207  | 0  |     return ERROR_INVALID_DATA;  | 
208  |  |  | 
209  | 0  |   Stream_Seek(irp->input, 32); /* Padding (32 bytes) */  | 
210  |  | 
  | 
211  | 0  |   close_terminated_irp_thread_handles(serial, TRUE);  | 
212  |  | 
  | 
213  | 0  |   if (!CloseHandle(serial->hComm))  | 
214  | 0  |   { | 
215  | 0  |     WLog_Print(serial->log, WLOG_WARN, "CloseHandle failure: %s (%" PRIu32 ") closed.",  | 
216  | 0  |                serial->device.name, irp->device->id);  | 
217  | 0  |     irp->IoStatus = STATUS_UNSUCCESSFUL;  | 
218  | 0  |     goto error_handle;  | 
219  | 0  |   }  | 
220  |  |  | 
221  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "%s (DeviceId: %" PRIu32 ", FileId: %" PRIu32 ") closed.",  | 
222  | 0  |              serial->device.name, irp->device->id, irp->FileId);  | 
223  | 0  |   irp->IoStatus = STATUS_SUCCESS;  | 
224  | 0  | error_handle:  | 
225  | 0  |   serial->hComm = NULL;  | 
226  | 0  |   Stream_Zero(irp->output, 5); /* Padding (5 bytes) */  | 
227  | 0  |   return CHANNEL_RC_OK;  | 
228  | 0  | }  | 
229  |  |  | 
230  |  | /**  | 
231  |  |  * Function description  | 
232  |  |  *  | 
233  |  |  * @return 0 on success, otherwise a Win32 error code  | 
234  |  |  */  | 
235  |  | static UINT serial_process_irp_read(SERIAL_DEVICE* serial, IRP* irp)  | 
236  | 0  | { | 
237  | 0  |   UINT32 Length = 0;  | 
238  | 0  |   UINT64 Offset = 0;  | 
239  | 0  |   BYTE* buffer = NULL;  | 
240  | 0  |   DWORD nbRead = 0;  | 
241  |  | 
  | 
242  | 0  |   WINPR_ASSERT(serial);  | 
243  | 0  |   WINPR_ASSERT(irp);  | 
244  |  |  | 
245  | 0  |   if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32))  | 
246  | 0  |     return ERROR_INVALID_DATA;  | 
247  |  |  | 
248  | 0  |   Stream_Read_UINT32(irp->input, Length); /* Length (4 bytes) */  | 
249  | 0  |   Stream_Read_UINT64(irp->input, Offset); /* Offset (8 bytes) */  | 
250  | 0  |   (void)Offset; /* [MS-RDPESP] 3.2.5.1.4 Processing a Server Read Request Message  | 
251  |  |                  * ignored */  | 
252  | 0  |   Stream_Seek(irp->input, 20);            /* Padding (20 bytes) */  | 
253  | 0  |   buffer = (BYTE*)calloc(Length, sizeof(BYTE));  | 
254  |  | 
  | 
255  | 0  |   if (buffer == NULL)  | 
256  | 0  |   { | 
257  | 0  |     irp->IoStatus = STATUS_NO_MEMORY;  | 
258  | 0  |     goto error_handle;  | 
259  | 0  |   }  | 
260  |  |  | 
261  |  |   /* MS-RDPESP 3.2.5.1.4: If the Offset field is not set to 0, the value MUST be ignored  | 
262  |  |    * WINPR_ASSERT(Offset == 0);  | 
263  |  |    */  | 
264  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "reading %" PRIu32 " bytes from %s", Length,  | 
265  | 0  |              serial->device.name);  | 
266  |  |  | 
267  |  |   /* FIXME: CommReadFile to be replaced by ReadFile */  | 
268  | 0  |   if (CommReadFile(serial->hComm, buffer, Length, &nbRead, NULL))  | 
269  | 0  |   { | 
270  | 0  |     irp->IoStatus = STATUS_SUCCESS;  | 
271  | 0  |   }  | 
272  | 0  |   else  | 
273  | 0  |   { | 
274  | 0  |     WLog_Print(serial->log, WLOG_DEBUG,  | 
275  | 0  |                "read failure to %s, nbRead=%" PRIu32 ", last-error: 0x%08" PRIX32 "",  | 
276  | 0  |                serial->device.name, nbRead, GetLastError());  | 
277  | 0  |     irp->IoStatus = GetLastErrorToIoStatus(serial);  | 
278  | 0  |   }  | 
279  |  | 
  | 
280  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "%" PRIu32 " bytes read from %s", nbRead,  | 
281  | 0  |              serial->device.name);  | 
282  | 0  | error_handle:  | 
283  | 0  |   Stream_Write_UINT32(irp->output, nbRead); /* Length (4 bytes) */  | 
284  |  | 
  | 
285  | 0  |   if (nbRead > 0)  | 
286  | 0  |   { | 
287  | 0  |     if (!Stream_EnsureRemainingCapacity(irp->output, nbRead))  | 
288  | 0  |     { | 
289  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");  | 
290  | 0  |       free(buffer);  | 
291  | 0  |       return CHANNEL_RC_NO_MEMORY;  | 
292  | 0  |     }  | 
293  |  |  | 
294  | 0  |     Stream_Write(irp->output, buffer, nbRead); /* ReadData */  | 
295  | 0  |   }  | 
296  |  |  | 
297  | 0  |   free(buffer);  | 
298  | 0  |   return CHANNEL_RC_OK;  | 
299  | 0  | }  | 
300  |  |  | 
301  |  | static UINT serial_process_irp_write(SERIAL_DEVICE* serial, IRP* irp)  | 
302  | 0  | { | 
303  | 0  |   UINT32 Length = 0;  | 
304  | 0  |   UINT64 Offset = 0;  | 
305  | 0  |   DWORD nbWritten = 0;  | 
306  |  | 
  | 
307  | 0  |   WINPR_ASSERT(serial);  | 
308  | 0  |   WINPR_ASSERT(irp);  | 
309  |  |  | 
310  | 0  |   if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32))  | 
311  | 0  |     return ERROR_INVALID_DATA;  | 
312  |  |  | 
313  | 0  |   Stream_Read_UINT32(irp->input, Length); /* Length (4 bytes) */  | 
314  | 0  |   Stream_Read_UINT64(irp->input, Offset); /* Offset (8 bytes) */  | 
315  | 0  |   (void)Offset; /* [MS-RDPESP] 3.2.5.1.4 Processing a Server Read Request Message  | 
316  |  |                  * ignored */  | 
317  | 0  |   if (!Stream_SafeSeek(irp->input, 20))   /* Padding (20 bytes) */  | 
318  | 0  |     return ERROR_INVALID_DATA;  | 
319  |  |  | 
320  |  |   /* MS-RDPESP 3.2.5.1.5: The Offset field is ignored  | 
321  |  |    * WINPR_ASSERT(Offset == 0);  | 
322  |  |    *  | 
323  |  |    * Using a serial printer, noticed though this field could be  | 
324  |  |    * set.  | 
325  |  |    */  | 
326  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "writing %" PRIu32 " bytes to %s", Length,  | 
327  | 0  |              serial->device.name);  | 
328  |  | 
  | 
329  | 0  |   const void* ptr = Stream_ConstPointer(irp->input);  | 
330  | 0  |   if (!Stream_SafeSeek(irp->input, Length))  | 
331  | 0  |     return ERROR_INVALID_DATA;  | 
332  |  |   /* FIXME: CommWriteFile to be replaced by WriteFile */  | 
333  | 0  |   if (CommWriteFile(serial->hComm, ptr, Length, &nbWritten, NULL))  | 
334  | 0  |   { | 
335  | 0  |     irp->IoStatus = STATUS_SUCCESS;  | 
336  | 0  |   }  | 
337  | 0  |   else  | 
338  | 0  |   { | 
339  | 0  |     WLog_Print(serial->log, WLOG_DEBUG,  | 
340  | 0  |                "write failure to %s, nbWritten=%" PRIu32 ", last-error: 0x%08" PRIX32 "",  | 
341  | 0  |                serial->device.name, nbWritten, GetLastError());  | 
342  | 0  |     irp->IoStatus = GetLastErrorToIoStatus(serial);  | 
343  | 0  |   }  | 
344  |  | 
  | 
345  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "%" PRIu32 " bytes written to %s", nbWritten,  | 
346  | 0  |              serial->device.name);  | 
347  | 0  |   Stream_Write_UINT32(irp->output, nbWritten); /* Length (4 bytes) */  | 
348  | 0  |   Stream_Write_UINT8(irp->output, 0);          /* Padding (1 byte) */  | 
349  | 0  |   return CHANNEL_RC_OK;  | 
350  | 0  | }  | 
351  |  |  | 
352  |  | /**  | 
353  |  |  * Function description  | 
354  |  |  *  | 
355  |  |  * @return 0 on success, otherwise a Win32 error code  | 
356  |  |  */  | 
357  |  | static UINT serial_process_irp_device_control(SERIAL_DEVICE* serial, IRP* irp)  | 
358  | 0  | { | 
359  | 0  |   UINT32 IoControlCode = 0;  | 
360  | 0  |   UINT32 InputBufferLength = 0;  | 
361  | 0  |   BYTE* InputBuffer = NULL;  | 
362  | 0  |   UINT32 OutputBufferLength = 0;  | 
363  | 0  |   BYTE* OutputBuffer = NULL;  | 
364  | 0  |   DWORD BytesReturned = 0;  | 
365  |  | 
  | 
366  | 0  |   WINPR_ASSERT(serial);  | 
367  | 0  |   WINPR_ASSERT(irp);  | 
368  |  |  | 
369  | 0  |   if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, 32))  | 
370  | 0  |     return ERROR_INVALID_DATA;  | 
371  |  |  | 
372  | 0  |   Stream_Read_UINT32(irp->input, OutputBufferLength); /* OutputBufferLength (4 bytes) */  | 
373  | 0  |   Stream_Read_UINT32(irp->input, InputBufferLength);  /* InputBufferLength (4 bytes) */  | 
374  | 0  |   Stream_Read_UINT32(irp->input, IoControlCode);      /* IoControlCode (4 bytes) */  | 
375  | 0  |   Stream_Seek(irp->input, 20);                        /* Padding (20 bytes) */  | 
376  |  | 
  | 
377  | 0  |   if (!Stream_CheckAndLogRequiredLengthWLog(serial->log, irp->input, InputBufferLength))  | 
378  | 0  |     return ERROR_INVALID_DATA;  | 
379  |  |  | 
380  | 0  |   OutputBuffer = (BYTE*)calloc(OutputBufferLength, sizeof(BYTE));  | 
381  |  | 
  | 
382  | 0  |   if (OutputBuffer == NULL)  | 
383  | 0  |   { | 
384  | 0  |     irp->IoStatus = STATUS_NO_MEMORY;  | 
385  | 0  |     goto error_handle;  | 
386  | 0  |   }  | 
387  |  |  | 
388  | 0  |   InputBuffer = (BYTE*)calloc(InputBufferLength, sizeof(BYTE));  | 
389  |  | 
  | 
390  | 0  |   if (InputBuffer == NULL)  | 
391  | 0  |   { | 
392  | 0  |     irp->IoStatus = STATUS_NO_MEMORY;  | 
393  | 0  |     goto error_handle;  | 
394  | 0  |   }  | 
395  |  |  | 
396  | 0  |   Stream_Read(irp->input, InputBuffer, InputBufferLength);  | 
397  | 0  |   WLog_Print(serial->log, WLOG_DEBUG,  | 
398  | 0  |              "CommDeviceIoControl: CompletionId=%" PRIu32 ", IoControlCode=[0x%" PRIX32 "] %s",  | 
399  | 0  |              irp->CompletionId, IoControlCode, _comm_serial_ioctl_name(IoControlCode));  | 
400  |  |  | 
401  |  |   /* FIXME: CommDeviceIoControl to be replaced by DeviceIoControl() */  | 
402  | 0  |   if (CommDeviceIoControl(serial->hComm, IoControlCode, InputBuffer, InputBufferLength,  | 
403  | 0  |                           OutputBuffer, OutputBufferLength, &BytesReturned, NULL))  | 
404  | 0  |   { | 
405  |  |     /* WLog_Print(serial->log, WLOG_DEBUG, "CommDeviceIoControl: CompletionId=%"PRIu32",  | 
406  |  |      * IoControlCode=[0x%"PRIX32"] %s done", irp->CompletionId, IoControlCode,  | 
407  |  |      * _comm_serial_ioctl_name(IoControlCode)); */  | 
408  | 0  |     irp->IoStatus = STATUS_SUCCESS;  | 
409  | 0  |   }  | 
410  | 0  |   else  | 
411  | 0  |   { | 
412  | 0  |     WLog_Print(serial->log, WLOG_DEBUG,  | 
413  | 0  |                "CommDeviceIoControl failure: IoControlCode=[0x%" PRIX32  | 
414  | 0  |                "] %s, last-error: 0x%08" PRIX32 "",  | 
415  | 0  |                IoControlCode, _comm_serial_ioctl_name(IoControlCode), GetLastError());  | 
416  | 0  |     irp->IoStatus = GetLastErrorToIoStatus(serial);  | 
417  | 0  |   }  | 
418  |  | 
  | 
419  | 0  | error_handle:  | 
420  |  |   /* FIXME: find out whether it's required or not to get  | 
421  |  |    * BytesReturned == OutputBufferLength when  | 
422  |  |    * CommDeviceIoControl returns FALSE */  | 
423  | 0  |   WINPR_ASSERT(OutputBufferLength == BytesReturned);  | 
424  | 0  |   Stream_Write_UINT32(irp->output, BytesReturned); /* OutputBufferLength (4 bytes) */  | 
425  |  | 
  | 
426  | 0  |   if (BytesReturned > 0)  | 
427  | 0  |   { | 
428  | 0  |     if (!Stream_EnsureRemainingCapacity(irp->output, BytesReturned))  | 
429  | 0  |     { | 
430  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");  | 
431  | 0  |       free(InputBuffer);  | 
432  | 0  |       free(OutputBuffer);  | 
433  | 0  |       return CHANNEL_RC_NO_MEMORY;  | 
434  | 0  |     }  | 
435  |  |  | 
436  | 0  |     Stream_Write(irp->output, OutputBuffer, BytesReturned); /* OutputBuffer */  | 
437  | 0  |   }  | 
438  |  |  | 
439  |  |   /* FIXME: Why at least Windows 2008R2 gets lost with this  | 
440  |  |    * extra byte and likely on a IOCTL_SERIAL_SET_BAUD_RATE? The  | 
441  |  |    * extra byte is well required according MS-RDPEFS  | 
442  |  |    * 2.2.1.5.5 */  | 
443  |  |   /* else */  | 
444  |  |   /* { */ | 
445  |  |   /*  Stream_Write_UINT8(irp->output, 0); /\* Padding (1 byte) *\/ */  | 
446  |  |   /* } */  | 
447  | 0  |   free(InputBuffer);  | 
448  | 0  |   free(OutputBuffer);  | 
449  | 0  |   return CHANNEL_RC_OK;  | 
450  | 0  | }  | 
451  |  |  | 
452  |  | /**  | 
453  |  |  * Function description  | 
454  |  |  *  | 
455  |  |  * @return 0 on success, otherwise a Win32 error code  | 
456  |  |  */  | 
457  |  | static UINT serial_process_irp(SERIAL_DEVICE* serial, IRP* irp)  | 
458  | 0  | { | 
459  | 0  |   UINT error = CHANNEL_RC_OK;  | 
460  |  | 
  | 
461  | 0  |   WINPR_ASSERT(serial);  | 
462  | 0  |   WINPR_ASSERT(irp);  | 
463  |  |  | 
464  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "IRP MajorFunction: %s, MinorFunction: 0x%08" PRIX32 "\n",  | 
465  | 0  |              rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction);  | 
466  |  | 
  | 
467  | 0  |   switch (irp->MajorFunction)  | 
468  | 0  |   { | 
469  | 0  |     case IRP_MJ_CREATE:  | 
470  | 0  |       error = serial_process_irp_create(serial, irp);  | 
471  | 0  |       break;  | 
472  |  |  | 
473  | 0  |     case IRP_MJ_CLOSE:  | 
474  | 0  |       error = serial_process_irp_close(serial, irp);  | 
475  | 0  |       break;  | 
476  |  |  | 
477  | 0  |     case IRP_MJ_READ:  | 
478  | 0  |       error = serial_process_irp_read(serial, irp);  | 
479  | 0  |       break;  | 
480  |  |  | 
481  | 0  |     case IRP_MJ_WRITE:  | 
482  | 0  |       error = serial_process_irp_write(serial, irp);  | 
483  | 0  |       break;  | 
484  |  |  | 
485  | 0  |     case IRP_MJ_DEVICE_CONTROL:  | 
486  | 0  |       error = serial_process_irp_device_control(serial, irp);  | 
487  | 0  |       break;  | 
488  |  |  | 
489  | 0  |     default:  | 
490  | 0  |       irp->IoStatus = STATUS_NOT_SUPPORTED;  | 
491  | 0  |       break;  | 
492  | 0  |   }  | 
493  |  |  | 
494  | 0  |   DWORD level = WLOG_TRACE;  | 
495  | 0  |   if (error)  | 
496  | 0  |     level = WLOG_WARN;  | 
497  |  | 
  | 
498  | 0  |   WLog_Print(serial->log, level,  | 
499  | 0  |              "[%s|0x%08" PRIx32 "] completed with %s [0x%08" PRIx32 "] (IoStatus %s [0x%08" PRIx32  | 
500  | 0  |              "])",  | 
501  | 0  |              rdpdr_irp_string(irp->MajorFunction), irp->MajorFunction, WTSErrorToString(error),  | 
502  | 0  |              error, NtStatus2Tag(irp->IoStatus), irp->IoStatus);  | 
503  |  | 
  | 
504  | 0  |   return error;  | 
505  | 0  | }  | 
506  |  |  | 
507  |  | static DWORD WINAPI irp_thread_func(LPVOID arg)  | 
508  | 0  | { | 
509  | 0  |   IRP_THREAD_DATA* data = (IRP_THREAD_DATA*)arg;  | 
510  | 0  |   UINT error = 0;  | 
511  |  | 
  | 
512  | 0  |   WINPR_ASSERT(data);  | 
513  | 0  |   WINPR_ASSERT(data->serial);  | 
514  | 0  |   WINPR_ASSERT(data->irp);  | 
515  |  |  | 
516  |  |   /* blocks until the end of the request */  | 
517  | 0  |   if ((error = serial_process_irp(data->serial, data->irp)))  | 
518  | 0  |   { | 
519  | 0  |     WLog_Print(data->serial->log, WLOG_ERROR,  | 
520  | 0  |                "serial_process_irp failed with error %" PRIu32 "", error);  | 
521  | 0  |     goto error_out;  | 
522  | 0  |   }  | 
523  |  |  | 
524  | 0  |   EnterCriticalSection(&data->serial->TerminatingIrpThreadsLock);  | 
525  | 0  |   error = data->irp->Complete(data->irp);  | 
526  | 0  |   LeaveCriticalSection(&data->serial->TerminatingIrpThreadsLock);  | 
527  | 0  | error_out:  | 
528  |  | 
  | 
529  | 0  |   if (error && data->serial->rdpcontext)  | 
530  | 0  |     setChannelError(data->serial->rdpcontext, error, "irp_thread_func reported an error");  | 
531  |  |  | 
532  |  |   /* NB: At this point, the server might already being reusing  | 
533  |  |    * the CompletionId whereas the thread is not yet  | 
534  |  |    * terminated */  | 
535  | 0  |   free(data);  | 
536  | 0  |   ExitThread(error);  | 
537  | 0  |   return error;  | 
538  | 0  | }  | 
539  |  |  | 
540  |  | static void close_unterminated_irp_thread(wListDictionary* list, wLog* log, ULONG_PTR id)  | 
541  | 0  | { | 
542  | 0  |   WINPR_ASSERT(list);  | 
543  | 0  |   HANDLE self = _GetCurrentThread();  | 
544  | 0  |   HANDLE cirpThread = ListDictionary_GetItemValue(list, (void*)id);  | 
545  | 0  |   if (self == cirpThread)  | 
546  | 0  |     WLog_Print(log, WLOG_DEBUG, "Skipping termination of own IRP thread");  | 
547  | 0  |   else  | 
548  | 0  |     ListDictionary_Remove(list, (void*)id);  | 
549  | 0  | }  | 
550  |  |  | 
551  |  | static void close_terminated_irp_thread(wListDictionary* list, wLog* log, ULONG_PTR id)  | 
552  | 0  | { | 
553  | 0  |   WINPR_ASSERT(list);  | 
554  |  |  | 
555  | 0  |   HANDLE cirpThread = ListDictionary_GetItemValue(list, (void*)id);  | 
556  |  |   /* FIXME: not quite sure a zero timeout is a good thing to check whether a thread is  | 
557  |  |    * stil alived or not */  | 
558  | 0  |   const DWORD waitResult = WaitForSingleObject(cirpThread, 0);  | 
559  |  | 
  | 
560  | 0  |   if (waitResult == WAIT_OBJECT_0)  | 
561  | 0  |     ListDictionary_Remove(list, (void*)id);  | 
562  | 0  |   else if (waitResult != WAIT_TIMEOUT)  | 
563  | 0  |   { | 
564  |  |     /* unexpected thread state */  | 
565  | 0  |     WLog_Print(log, WLOG_WARN, "WaitForSingleObject, got an unexpected result=0x%" PRIX32 "\n",  | 
566  | 0  |                waitResult);  | 
567  | 0  |   }  | 
568  | 0  | }  | 
569  |  |  | 
570  |  | void close_terminated_irp_thread_handles(SERIAL_DEVICE* serial, BOOL forceClose)  | 
571  | 0  | { | 
572  | 0  |   WINPR_ASSERT(serial);  | 
573  |  |  | 
574  | 0  |   EnterCriticalSection(&serial->TerminatingIrpThreadsLock);  | 
575  |  | 
  | 
576  | 0  |   ULONG_PTR* ids = NULL;  | 
577  | 0  |   const size_t nbIds = ListDictionary_GetKeys(serial->IrpThreads, &ids);  | 
578  |  | 
  | 
579  | 0  |   for (size_t i = 0; i < nbIds; i++)  | 
580  | 0  |   { | 
581  | 0  |     ULONG_PTR id = ids[i];  | 
582  | 0  |     if (forceClose)  | 
583  | 0  |       close_unterminated_irp_thread(serial->IrpThreads, serial->log, id);  | 
584  | 0  |     else  | 
585  | 0  |       close_terminated_irp_thread(serial->IrpThreads, serial->log, id);  | 
586  | 0  |   }  | 
587  |  | 
  | 
588  | 0  |   free(ids);  | 
589  |  | 
  | 
590  | 0  |   LeaveCriticalSection(&serial->TerminatingIrpThreadsLock);  | 
591  | 0  | }  | 
592  |  |  | 
593  |  | static void create_irp_thread(SERIAL_DEVICE* serial, IRP* irp)  | 
594  | 0  | { | 
595  | 0  |   IRP_THREAD_DATA* data = NULL;  | 
596  | 0  |   HANDLE irpThread = NULL;  | 
597  | 0  |   HANDLE previousIrpThread = NULL;  | 
598  | 0  |   uintptr_t key = 0;  | 
599  |  | 
  | 
600  | 0  |   WINPR_ASSERT(serial);  | 
601  | 0  |   WINPR_ASSERT(irp);  | 
602  |  |  | 
603  | 0  |   close_terminated_irp_thread_handles(serial, FALSE);  | 
604  |  |  | 
605  |  |   /* NB: At this point and thanks to the synchronization we're  | 
606  |  |    * sure that the incoming IRP uses well a recycled  | 
607  |  |    * CompletionId or the server sent again an IRP already posted  | 
608  |  |    * which didn't get yet a response (this later server behavior  | 
609  |  |    * at least observed with IOCTL_SERIAL_WAIT_ON_MASK and  | 
610  |  |    * mstsc.exe).  | 
611  |  |    *  | 
612  |  |    * FIXME: behavior documented somewhere? behavior not yet  | 
613  |  |    * observed with FreeRDP).  | 
614  |  |    */  | 
615  | 0  |   key = irp->CompletionId + 1ull;  | 
616  | 0  |   previousIrpThread = ListDictionary_GetItemValue(serial->IrpThreads, (void*)key);  | 
617  |  | 
  | 
618  | 0  |   if (previousIrpThread)  | 
619  | 0  |   { | 
620  |  |     /* Thread still alived <=> Request still pending */  | 
621  | 0  |     WLog_Print(serial->log, WLOG_DEBUG,  | 
622  | 0  |                "IRP recall: IRP with the CompletionId=%" PRIu32 " not yet completed!",  | 
623  | 0  |                irp->CompletionId);  | 
624  | 0  |     WINPR_ASSERT(FALSE); /* unimplemented */  | 
625  |  |     /* TODO: WINPR_ASSERTs that previousIrpThread handles well  | 
626  |  |      * the same request by checking more details. Need an  | 
627  |  |      * access to the IRP object used by previousIrpThread  | 
628  |  |      */  | 
629  |  |     /* TODO: taking over the pending IRP or sending a kind  | 
630  |  |      * of wake up signal to accelerate the pending  | 
631  |  |      * request  | 
632  |  |      *  | 
633  |  |      * To be considered:  | 
634  |  |      *   if (IoControlCode == IOCTL_SERIAL_WAIT_ON_MASK) { | 
635  |  |      *       pComm->PendingEvents |= SERIAL_EV_FREERDP_*;  | 
636  |  |      *   }  | 
637  |  |      */  | 
638  | 0  |     irp->Discard(irp);  | 
639  | 0  |     return;  | 
640  | 0  |   }  | 
641  |  |  | 
642  | 0  |   if (ListDictionary_Count(serial->IrpThreads) >= MAX_IRP_THREADS)  | 
643  | 0  |   { | 
644  | 0  |     WLog_Print(serial->log, WLOG_WARN,  | 
645  | 0  |                "Number of IRP threads threshold reached: %" PRIuz ", keep on anyway",  | 
646  | 0  |                ListDictionary_Count(serial->IrpThreads));  | 
647  | 0  |     WINPR_ASSERT(FALSE); /* unimplemented */  | 
648  |  |                          /* TODO: MAX_IRP_THREADS has been thought to avoid a  | 
649  |  |                           * flooding of pending requests. Use  | 
650  |  |                           * WaitForMultipleObjects() when available in winpr  | 
651  |  |                           * for threads.  | 
652  |  |                           */  | 
653  | 0  |   }  | 
654  |  |  | 
655  |  |   /* error_handle to be used ... */  | 
656  | 0  |   data = (IRP_THREAD_DATA*)calloc(1, sizeof(IRP_THREAD_DATA));  | 
657  |  | 
  | 
658  | 0  |   if (data == NULL)  | 
659  | 0  |   { | 
660  | 0  |     WLog_Print(serial->log, WLOG_WARN, "Could not allocate a new IRP_THREAD_DATA.");  | 
661  | 0  |     goto error_handle;  | 
662  | 0  |   }  | 
663  |  |  | 
664  | 0  |   data->serial = serial;  | 
665  | 0  |   data->irp = irp;  | 
666  |  |   /* data freed by irp_thread_func */  | 
667  | 0  |   irpThread = CreateThread(NULL, 0, irp_thread_func, (void*)data, CREATE_SUSPENDED, NULL);  | 
668  |  | 
  | 
669  | 0  |   if (irpThread == INVALID_HANDLE_VALUE)  | 
670  | 0  |   { | 
671  | 0  |     WLog_Print(serial->log, WLOG_WARN, "Could not allocate a new IRP thread.");  | 
672  | 0  |     goto error_handle;  | 
673  | 0  |   }  | 
674  |  |  | 
675  | 0  |   key = irp->CompletionId + 1ull;  | 
676  |  | 
  | 
677  | 0  |   if (!ListDictionary_Add(serial->IrpThreads, (void*)key, irpThread))  | 
678  | 0  |   { | 
679  | 0  |     WLog_Print(serial->log, WLOG_ERROR, "ListDictionary_Add failed!");  | 
680  | 0  |     goto error_handle;  | 
681  | 0  |   }  | 
682  |  |  | 
683  | 0  |   ResumeThread(irpThread);  | 
684  |  | 
  | 
685  | 0  |   return;  | 
686  | 0  | error_handle:  | 
687  | 0  |   if (irpThread)  | 
688  | 0  |     CloseHandle(irpThread);  | 
689  | 0  |   irp->IoStatus = STATUS_NO_MEMORY;  | 
690  | 0  |   irp->Complete(irp);  | 
691  | 0  |   free(data);  | 
692  | 0  | }  | 
693  |  |  | 
694  |  | static DWORD WINAPI serial_thread_func(LPVOID arg)  | 
695  | 0  | { | 
696  | 0  |   IRP* irp = NULL;  | 
697  | 0  |   wMessage message = { 0 }; | 
698  | 0  |   SERIAL_DEVICE* serial = (SERIAL_DEVICE*)arg;  | 
699  | 0  |   UINT error = CHANNEL_RC_OK;  | 
700  |  | 
  | 
701  | 0  |   WINPR_ASSERT(serial);  | 
702  |  |  | 
703  | 0  |   while (1)  | 
704  | 0  |   { | 
705  | 0  |     if (!MessageQueue_Wait(serial->MainIrpQueue))  | 
706  | 0  |     { | 
707  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_Wait failed!");  | 
708  | 0  |       error = ERROR_INTERNAL_ERROR;  | 
709  | 0  |       break;  | 
710  | 0  |     }  | 
711  |  |  | 
712  | 0  |     if (!MessageQueue_Peek(serial->MainIrpQueue, &message, TRUE))  | 
713  | 0  |     { | 
714  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_Peek failed!");  | 
715  | 0  |       error = ERROR_INTERNAL_ERROR;  | 
716  | 0  |       break;  | 
717  | 0  |     }  | 
718  |  |  | 
719  | 0  |     if (message.id == WMQ_QUIT)  | 
720  | 0  |       break;  | 
721  |  |  | 
722  | 0  |     irp = (IRP*)message.wParam;  | 
723  |  | 
  | 
724  | 0  |     if (irp)  | 
725  | 0  |       create_irp_thread(serial, irp);  | 
726  | 0  |   }  | 
727  |  | 
  | 
728  | 0  |   ListDictionary_Clear(serial->IrpThreads);  | 
729  | 0  |   if (error && serial->rdpcontext)  | 
730  | 0  |     setChannelError(serial->rdpcontext, error, "serial_thread_func reported an error");  | 
731  |  | 
  | 
732  | 0  |   ExitThread(error);  | 
733  | 0  |   return error;  | 
734  | 0  | }  | 
735  |  |  | 
736  |  | /**  | 
737  |  |  * Function description  | 
738  |  |  *  | 
739  |  |  * @return 0 on success, otherwise a Win32 error code  | 
740  |  |  */  | 
741  |  | static UINT serial_irp_request(DEVICE* device, IRP* irp)  | 
742  | 0  | { | 
743  | 0  |   SERIAL_DEVICE* serial = (SERIAL_DEVICE*)device;  | 
744  | 0  |   WINPR_ASSERT(irp != NULL);  | 
745  | 0  |   WINPR_ASSERT(serial);  | 
746  |  |  | 
747  | 0  |   if (irp == NULL)  | 
748  | 0  |     return CHANNEL_RC_OK;  | 
749  |  |  | 
750  |  |   /* NB: ENABLE_ASYNCIO is set, (MS-RDPEFS 2.2.2.7.2) this  | 
751  |  |    * allows the server to send multiple simultaneous read or  | 
752  |  |    * write requests.  | 
753  |  |    */  | 
754  |  |  | 
755  | 0  |   if (!MessageQueue_Post(serial->MainIrpQueue, NULL, 0, (void*)irp, NULL))  | 
756  | 0  |   { | 
757  | 0  |     WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_Post failed!");  | 
758  | 0  |     return ERROR_INTERNAL_ERROR;  | 
759  | 0  |   }  | 
760  |  |  | 
761  | 0  |   return CHANNEL_RC_OK;  | 
762  | 0  | }  | 
763  |  |  | 
764  |  | /**  | 
765  |  |  * Function description  | 
766  |  |  *  | 
767  |  |  * @return 0 on success, otherwise a Win32 error code  | 
768  |  |  */  | 
769  |  | static UINT serial_free(DEVICE* device)  | 
770  | 0  | { | 
771  | 0  |   UINT error = 0;  | 
772  | 0  |   SERIAL_DEVICE* serial = (SERIAL_DEVICE*)device;  | 
773  | 0  |   if (!serial)  | 
774  | 0  |     return CHANNEL_RC_OK;  | 
775  |  |  | 
776  | 0  |   WLog_Print(serial->log, WLOG_DEBUG, "freeing");  | 
777  | 0  |   if (serial->MainIrpQueue)  | 
778  | 0  |     MessageQueue_PostQuit(serial->MainIrpQueue, 0);  | 
779  |  | 
  | 
780  | 0  |   if (serial->MainThread)  | 
781  | 0  |   { | 
782  | 0  |     if (WaitForSingleObject(serial->MainThread, INFINITE) == WAIT_FAILED)  | 
783  | 0  |     { | 
784  | 0  |       error = GetLastError();  | 
785  | 0  |       WLog_Print(serial->log, WLOG_ERROR,  | 
786  | 0  |                  "WaitForSingleObject failed with error %" PRIu32 "!", error);  | 
787  | 0  |     }  | 
788  | 0  |     CloseHandle(serial->MainThread);  | 
789  | 0  |   }  | 
790  |  | 
  | 
791  | 0  |   if (serial->hComm)  | 
792  | 0  |     CloseHandle(serial->hComm);  | 
793  |  |  | 
794  |  |   /* Clean up resources */  | 
795  | 0  |   Stream_Free(serial->device.data, TRUE);  | 
796  | 0  |   MessageQueue_Free(serial->MainIrpQueue);  | 
797  | 0  |   ListDictionary_Free(serial->IrpThreads);  | 
798  | 0  |   DeleteCriticalSection(&serial->TerminatingIrpThreadsLock);  | 
799  | 0  |   free(serial);  | 
800  | 0  |   return CHANNEL_RC_OK;  | 
801  | 0  | }  | 
802  |  |  | 
803  |  | static void serial_message_free(void* obj)  | 
804  | 0  | { | 
805  | 0  |   wMessage* msg = obj;  | 
806  | 0  |   if (!msg)  | 
807  | 0  |     return;  | 
808  | 0  |   if (msg->id != 0)  | 
809  | 0  |     return;  | 
810  |  |  | 
811  | 0  |   IRP* irp = (IRP*)msg->wParam;  | 
812  | 0  |   if (!irp)  | 
813  | 0  |     return;  | 
814  | 0  |   WINPR_ASSERT(irp->Discard);  | 
815  | 0  |   irp->Discard(irp);  | 
816  | 0  | }  | 
817  |  |  | 
818  |  | static void irp_thread_close(void* arg)  | 
819  | 0  | { | 
820  | 0  |   HANDLE hdl = arg;  | 
821  | 0  |   if (hdl)  | 
822  | 0  |   { | 
823  | 0  |     HANDLE thz = _GetCurrentThread();  | 
824  | 0  |     if (thz == hdl)  | 
825  | 0  |       WLog_WARN(TAG, "closing self, ignoring...");  | 
826  | 0  |     else  | 
827  | 0  |     { | 
828  | 0  |       TerminateThread(hdl, 0);  | 
829  | 0  |       WaitForSingleObject(hdl, INFINITE);  | 
830  | 0  |       CloseHandle(hdl);  | 
831  | 0  |     }  | 
832  | 0  |   }  | 
833  | 0  | }  | 
834  |  |  | 
835  |  | /**  | 
836  |  |  * Function description  | 
837  |  |  *  | 
838  |  |  * @return 0 on success, otherwise a Win32 error code  | 
839  |  |  */  | 
840  |  | FREERDP_ENTRY_POINT(  | 
841  |  |     UINT VCAPITYPE serial_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))  | 
842  | 0  | { | 
843  | 0  |   size_t len = 0;  | 
844  | 0  |   SERIAL_DEVICE* serial = NULL;  | 
845  | 0  |   UINT error = CHANNEL_RC_OK;  | 
846  |  | 
  | 
847  | 0  |   WINPR_ASSERT(pEntryPoints);  | 
848  |  |  | 
849  | 0  |   RDPDR_SERIAL* device = (RDPDR_SERIAL*)pEntryPoints->device;  | 
850  | 0  |   WINPR_ASSERT(device);  | 
851  |  |  | 
852  | 0  |   wLog* log = WLog_Get(TAG);  | 
853  | 0  |   const char* name = device->device.Name;  | 
854  | 0  |   const char* path = device->Path;  | 
855  | 0  |   const char* driver = device->Driver;  | 
856  |  | 
  | 
857  | 0  |   if (!name || (name[0] == '*'))  | 
858  | 0  |   { | 
859  |  |     /* TODO: implement auto detection of serial ports */  | 
860  | 0  |     WLog_Print(log, WLOG_WARN,  | 
861  | 0  |                "Serial port autodetection not implemented, nothing will be redirected!");  | 
862  | 0  |     return CHANNEL_RC_OK;  | 
863  | 0  |   }  | 
864  |  |  | 
865  | 0  |   if ((name && name[0]) && (path && path[0]))  | 
866  | 0  |   { | 
867  | 0  |     WLog_Print(log, WLOG_DEBUG, "Defining %s as %s", name, path);  | 
868  |  | 
  | 
869  | 0  |     if (!DefineCommDevice(name /* eg: COM1 */, path /* eg: /dev/ttyS0 */))  | 
870  | 0  |     { | 
871  | 0  |       DWORD status = GetLastError();  | 
872  | 0  |       WLog_Print(log, WLOG_ERROR, "DefineCommDevice failed with %08" PRIx32, status);  | 
873  | 0  |       return ERROR_INTERNAL_ERROR;  | 
874  | 0  |     }  | 
875  |  |  | 
876  | 0  |     serial = (SERIAL_DEVICE*)calloc(1, sizeof(SERIAL_DEVICE));  | 
877  |  | 
  | 
878  | 0  |     if (!serial)  | 
879  | 0  |     { | 
880  | 0  |       WLog_Print(log, WLOG_ERROR, "calloc failed!");  | 
881  | 0  |       return CHANNEL_RC_NO_MEMORY;  | 
882  | 0  |     }  | 
883  |  |  | 
884  | 0  |     serial->log = log;  | 
885  | 0  |     serial->device.type = RDPDR_DTYP_SERIAL;  | 
886  | 0  |     serial->device.name = name;  | 
887  | 0  |     serial->device.IRPRequest = serial_irp_request;  | 
888  | 0  |     serial->device.Free = serial_free;  | 
889  | 0  |     serial->rdpcontext = pEntryPoints->rdpcontext;  | 
890  | 0  |     len = strlen(name);  | 
891  | 0  |     serial->device.data = Stream_New(NULL, len + 1);  | 
892  |  | 
  | 
893  | 0  |     if (!serial->device.data)  | 
894  | 0  |     { | 
895  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "calloc failed!");  | 
896  | 0  |       error = CHANNEL_RC_NO_MEMORY;  | 
897  | 0  |       goto error_out;  | 
898  | 0  |     }  | 
899  |  |  | 
900  | 0  |     for (size_t i = 0; i <= len; i++)  | 
901  | 0  |       Stream_Write_UINT8(serial->device.data, name[i] < 0 ? '_' : name[i]);  | 
902  |  | 
  | 
903  | 0  |     if (driver != NULL)  | 
904  | 0  |     { | 
905  | 0  |       if (_stricmp(driver, "Serial") == 0)  | 
906  | 0  |         serial->ServerSerialDriverId = SerialDriverSerialSys;  | 
907  | 0  |       else if (_stricmp(driver, "SerCx") == 0)  | 
908  | 0  |         serial->ServerSerialDriverId = SerialDriverSerCxSys;  | 
909  | 0  |       else if (_stricmp(driver, "SerCx2") == 0)  | 
910  | 0  |         serial->ServerSerialDriverId = SerialDriverSerCx2Sys;  | 
911  | 0  |       else  | 
912  | 0  |       { | 
913  | 0  |         WLog_Print(serial->log, WLOG_WARN, "Unknown server's serial driver: %s.", driver);  | 
914  | 0  |         WLog_Print(serial->log, WLOG_WARN,  | 
915  | 0  |                    "Valid options are: 'Serial' (default), 'SerCx' and 'SerCx2'");  | 
916  | 0  |         goto error_out;  | 
917  | 0  |       }  | 
918  | 0  |     }  | 
919  | 0  |     else  | 
920  | 0  |     { | 
921  |  |       /* default driver */  | 
922  | 0  |       serial->ServerSerialDriverId = SerialDriverSerialSys;  | 
923  | 0  |     }  | 
924  |  |  | 
925  | 0  |     if (device->Permissive != NULL)  | 
926  | 0  |     { | 
927  | 0  |       if (_stricmp(device->Permissive, "permissive") == 0)  | 
928  | 0  |       { | 
929  | 0  |         serial->permissive = TRUE;  | 
930  | 0  |       }  | 
931  | 0  |       else  | 
932  | 0  |       { | 
933  | 0  |         WLog_Print(serial->log, WLOG_WARN, "Unknown flag: %s", device->Permissive);  | 
934  | 0  |         goto error_out;  | 
935  | 0  |       }  | 
936  | 0  |     }  | 
937  |  |  | 
938  | 0  |     WLog_Print(serial->log, WLOG_DEBUG, "Server's serial driver: %s (id: %d)", driver,  | 
939  | 0  |                serial->ServerSerialDriverId);  | 
940  |  | 
  | 
941  | 0  |     serial->MainIrpQueue = MessageQueue_New(NULL);  | 
942  |  | 
  | 
943  | 0  |     if (!serial->MainIrpQueue)  | 
944  | 0  |     { | 
945  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "MessageQueue_New failed!");  | 
946  | 0  |       error = CHANNEL_RC_NO_MEMORY;  | 
947  | 0  |       goto error_out;  | 
948  | 0  |     }  | 
949  |  |  | 
950  | 0  |     { | 
951  | 0  |       wObject* obj = MessageQueue_Object(serial->MainIrpQueue);  | 
952  | 0  |       WINPR_ASSERT(obj);  | 
953  | 0  |       obj->fnObjectFree = serial_message_free;  | 
954  | 0  |     }  | 
955  |  |  | 
956  |  |     /* IrpThreads content only modified by create_irp_thread() */  | 
957  | 0  |     serial->IrpThreads = ListDictionary_New(FALSE);  | 
958  |  | 
  | 
959  | 0  |     if (!serial->IrpThreads)  | 
960  | 0  |     { | 
961  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "ListDictionary_New failed!");  | 
962  | 0  |       error = CHANNEL_RC_NO_MEMORY;  | 
963  | 0  |       goto error_out;  | 
964  | 0  |     }  | 
965  |  |  | 
966  | 0  |     { | 
967  | 0  |       wObject* obj = ListDictionary_ValueObject(serial->IrpThreads);  | 
968  | 0  |       WINPR_ASSERT(obj);  | 
969  | 0  |       obj->fnObjectFree = irp_thread_close;  | 
970  | 0  |     }  | 
971  |  |  | 
972  | 0  |     InitializeCriticalSection(&serial->TerminatingIrpThreadsLock);  | 
973  |  | 
  | 
974  | 0  |     error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &serial->device);  | 
975  | 0  |     if (error != CHANNEL_RC_OK)  | 
976  | 0  |     { | 
977  | 0  |       WLog_Print(serial->log, WLOG_ERROR,  | 
978  | 0  |                  "EntryPoints->RegisterDevice failed with error %" PRIu32 "!", error);  | 
979  | 0  |       goto error_out;  | 
980  | 0  |     }  | 
981  |  |  | 
982  | 0  |     serial->MainThread = CreateThread(NULL, 0, serial_thread_func, serial, 0, NULL);  | 
983  | 0  |     if (!serial->MainThread)  | 
984  | 0  |     { | 
985  | 0  |       WLog_Print(serial->log, WLOG_ERROR, "CreateThread failed!");  | 
986  | 0  |       error = ERROR_INTERNAL_ERROR;  | 
987  | 0  |       goto error_out;  | 
988  | 0  |     }  | 
989  | 0  |   }  | 
990  |  |  | 
991  | 0  |   return error;  | 
992  | 0  | error_out:  | 
993  | 0  |   if (serial)  | 
994  | 0  |     serial_free(&serial->device);  | 
995  | 0  |   return error;  | 
996  | 0  | }  |