FreeRDP
Loading...
Searching...
No Matches
comm_io.c
1
19
20#include <winpr/config.h>
21
22#include <winpr/assert.h>
23#include <errno.h>
24#include <termios.h>
25#include <unistd.h>
26
27#include <winpr/io.h>
28#include <winpr/wlog.h>
29#include <winpr/wtypes.h>
30
31#include "comm.h"
32
33BOOL _comm_set_permissive(HANDLE hDevice, BOOL permissive)
34{
35 WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
36
37 if (!CommIsHandled(hDevice))
38 return FALSE;
39
40 pComm->permissive = permissive;
41 return TRUE;
42}
43
44/* Computes VTIME in deciseconds from Ti in milliseconds */
45static UCHAR svtime(ULONG Ti)
46{
47 /* FIXME: look for an equivalent math function otherwise let
48 * do the compiler do the optimization */
49 if (Ti == 0)
50 return 0;
51 else if (Ti < 100)
52 return 1;
53 else if (Ti > 25500)
54 return 255; /* 0xFF */
55 else
56 return (UCHAR)(Ti / 100);
57}
58
68BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
69 LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
70{
71 WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
72 int biggestFd = -1;
73 fd_set read_set;
74 int nbFds = 0;
75 COMMTIMEOUTS* pTimeouts = nullptr;
76 UCHAR vmin = 0;
77 UCHAR vtime = 0;
78 LONGLONG Tmax = 0;
79 struct timeval tmaxTimeout;
80 struct timeval* pTmaxTimeout = nullptr;
81 struct termios currentTermios;
82
83 if (!CommIsHandled(hDevice))
84 return FALSE;
85
86 EnterCriticalSection(&pComm->ReadLock); /* KISSer by the function's beginning */
87
88 if (lpOverlapped != nullptr)
89 {
90 SetLastError(ERROR_NOT_SUPPORTED);
91 goto return_false;
92 }
93
94 if (lpNumberOfBytesRead == nullptr)
95 {
96 SetLastError(
97 ERROR_INVALID_PARAMETER); /* since we doesn't support lpOverlapped != nullptr */
98 goto return_false;
99 }
100
101 *lpNumberOfBytesRead = 0; /* will be adjusted if required ... */
102
103 if (nNumberOfBytesToRead <= 0) /* N */
104 {
105 goto return_true; /* FIXME: or FALSE? */
106 }
107
108 if (tcgetattr(pComm->fd, &currentTermios) < 0)
109 {
110 SetLastError(ERROR_IO_DEVICE);
111 goto return_false;
112 }
113
114 if (currentTermios.c_lflag & ICANON)
115 {
116 CommLog_Print(WLOG_WARN, "Canonical mode not supported"); /* the timeout could not be set */
117 SetLastError(ERROR_NOT_SUPPORTED);
118 goto return_false;
119 }
120
121 /* http://msdn.microsoft.com/en-us/library/hh439614%28v=vs.85%29.aspx
122 * http://msdn.microsoft.com/en-us/library/windows/hardware/hh439614%28v=vs.85%29.aspx
123 *
124 * ReadIntervalTimeout | ReadTotalTimeoutMultiplier | ReadTotalTimeoutConstant | VMIN | VTIME |
125 * TMAX | 0 | 0 | 0 | N | 0 |
126 * INDEF | Blocks for N bytes available. 0< Ti <MAXULONG | 0 | 0 |
127 * N | Ti | INDEF | Blocks on first byte, then use Ti between bytes. MAXULONG | 0 | 0
128 * | 0 | 0 | 0 | Returns immediately with bytes available (don't block) MAXULONG |
129 * MAXULONG | 0< Tc <MAXULONG | N | 0 | Tc | Blocks on first byte
130 * during Tc or returns immediately with bytes available MAXULONG | m |
131 * MAXULONG | | Invalid 0 | m | 0< Tc
132 * <MAXULONG | N | 0 | Tmax | Blocks on first byte during Tmax or returns
133 * immediately with bytes available 0< Ti <MAXULONG | m | 0<
134 * Tc <MAXULONG | N | Ti | Tmax | Blocks on first byte, then use Ti between bytes.
135 * Tmax is used for the whole system call.
136 */
137 /* NB: timeouts are in milliseconds, VTIME are in deciseconds and is an unsigned char */
138 /* FIXME: double check whether open(pComm->fd_read_event, O_NONBLOCK) doesn't conflict with
139 * above use cases */
140 pTimeouts = &(pComm->timeouts);
141
142 if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
143 (pTimeouts->ReadTotalTimeoutConstant == MAXULONG))
144 {
145 CommLog_Print(
146 WLOG_WARN,
147 "ReadIntervalTimeout and ReadTotalTimeoutConstant cannot be both set to MAXULONG");
148 SetLastError(ERROR_INVALID_PARAMETER);
149 goto return_false;
150 }
151
152 /* VMIN */
153
154 if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
155 (pTimeouts->ReadTotalTimeoutMultiplier == 0) && (pTimeouts->ReadTotalTimeoutConstant == 0))
156 {
157 vmin = 0;
158 }
159 else
160 {
161 /* N */
162 /* vmin = nNumberOfBytesToRead < 256 ? nNumberOfBytesToRead : 255;*/ /* 0xFF */
163 /* NB: we might wait endlessly with vmin=N, prefer to
164 * force vmin=1 and return with bytes
165 * available. FIXME: is a feature disarded here? */
166 vmin = 1;
167 }
168
169 /* VTIME */
170
171 if ((pTimeouts->ReadIntervalTimeout > 0) && (pTimeouts->ReadIntervalTimeout < MAXULONG))
172 {
173 /* Ti */
174 vtime = svtime(pTimeouts->ReadIntervalTimeout);
175 }
176
177 /* TMAX */
178 pTmaxTimeout = &tmaxTimeout;
179
180 if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
181 (pTimeouts->ReadTotalTimeoutMultiplier == MAXULONG))
182 {
183 /* Tc */
184 Tmax = pTimeouts->ReadTotalTimeoutConstant;
185 }
186 else
187 {
188 /* Tmax */
189 Tmax = 1ll * nNumberOfBytesToRead * pTimeouts->ReadTotalTimeoutMultiplier +
190 1ll * pTimeouts->ReadTotalTimeoutConstant;
191
192 /* INDEFinitely */
193 if ((Tmax == 0) && (pTimeouts->ReadIntervalTimeout < MAXULONG) &&
194 (pTimeouts->ReadTotalTimeoutMultiplier == 0))
195 pTmaxTimeout = nullptr;
196 }
197
198 if ((currentTermios.c_cc[VMIN] != vmin) || (currentTermios.c_cc[VTIME] != vtime))
199 {
200 currentTermios.c_cc[VMIN] = vmin;
201 currentTermios.c_cc[VTIME] = vtime;
202
203 if (tcsetattr(pComm->fd, TCSANOW, &currentTermios) < 0)
204 {
205 CommLog_Print(WLOG_WARN,
206 "CommReadFile failure, could not apply new timeout values: VMIN=%" PRIu8
207 ", VTIME=%" PRIu8 "",
208 vmin, vtime);
209 SetLastError(ERROR_IO_DEVICE);
210 goto return_false;
211 }
212 }
213
214 /* wait indefinitely if pTmaxTimeout is nullptr */
215
216 if (pTmaxTimeout != nullptr)
217 {
218 ZeroMemory(pTmaxTimeout, sizeof(struct timeval));
219
220 if (Tmax > 0) /* return immdiately if Tmax == 0 */
221 {
222 pTmaxTimeout->tv_sec = Tmax / 1000; /* s */
223 pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000; /* us */
224 }
225 }
226
227 /* FIXME: had expected eventfd_write() to return EAGAIN when
228 * there is no eventfd_read() but this not the case. */
229 /* discard a possible and no more relevant event */
230#if defined(WINPR_HAVE_SYS_EVENTFD_H)
231 {
232 eventfd_t val = 0;
233 (void)eventfd_read(pComm->fd_read_event, &val);
234 }
235#endif
236 biggestFd = pComm->fd_read;
237
238 if (pComm->fd_read_event > biggestFd)
239 biggestFd = pComm->fd_read_event;
240
241 /* an fd_set only holds descriptors below FD_SETSIZE and the port may have been opened
242 * after many other files (e.g. on a redirected drive), so this is not a programming error */
243 if ((pComm->fd_read_event >= FD_SETSIZE) || (pComm->fd_read >= FD_SETSIZE))
244 {
245 CommLog_Print(WLOG_WARN, "CommReadFile failed, descriptor above FD_SETSIZE=%d", FD_SETSIZE);
246 SetLastError(ERROR_IO_DEVICE);
247 goto return_false;
248 }
249
250 FD_ZERO(&read_set);
251 FD_SET(pComm->fd_read_event, &read_set);
252 FD_SET(pComm->fd_read, &read_set);
253 nbFds = select(biggestFd + 1, &read_set, nullptr, nullptr, pTmaxTimeout);
254
255 if (nbFds < 0)
256 {
257 char ebuffer[256] = WINPR_C_ARRAY_INIT;
258 CommLog_Print(WLOG_WARN, "select() failure, errno=[%d] %s\n", errno,
259 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
260 SetLastError(ERROR_IO_DEVICE);
261 goto return_false;
262 }
263
264 if (nbFds == 0)
265 {
266 /* timeout */
267 SetLastError(ERROR_TIMEOUT);
268 goto return_false;
269 }
270
271 /* read_set */
272
273 if (FD_ISSET(pComm->fd_read_event, &read_set))
274 {
275#if defined(WINPR_HAVE_SYS_EVENTFD_H)
276 eventfd_t event = 0;
277
278 if (eventfd_read(pComm->fd_read_event, &event) < 0)
279 {
280 if (errno == EAGAIN)
281 {
282 WINPR_ASSERT(FALSE); /* not quite sure this should ever happen */
283 /* keep on */
284 }
285 else
286 {
287 char ebuffer[256] = WINPR_C_ARRAY_INIT;
288 CommLog_Print(WLOG_WARN,
289 "unexpected error on reading fd_read_event, errno=[%d] %s\n", errno,
290 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
291 /* FIXME: goto return_false ? */
292 }
293
294 WINPR_ASSERT(errno == EAGAIN);
295 }
296
297 /* The eventfd is a counter: several WINPR_PURGE_RXABORT written by
298 * concurrent purge() requests before this read add up. No other event
299 * expected so far, so any non zero value is an abort. */
300 if (event != 0)
301 {
302 SetLastError(ERROR_CANCELLED);
303 goto return_false;
304 }
305#endif
306 }
307
308 if (FD_ISSET(pComm->fd_read, &read_set))
309 {
310 ssize_t nbRead = read(pComm->fd_read, lpBuffer, nNumberOfBytesToRead);
311
312 if ((nbRead < 0) || (nbRead > nNumberOfBytesToRead))
313 {
314 char ebuffer[256] = WINPR_C_ARRAY_INIT;
315 CommLog_Print(WLOG_WARN,
316 "CommReadFile failed, ReadIntervalTimeout=%" PRIu32
317 ", ReadTotalTimeoutMultiplier=%" PRIu32
318 ", ReadTotalTimeoutConstant=%" PRIu32 " VMIN=%u, VTIME=%u",
319 pTimeouts->ReadIntervalTimeout, pTimeouts->ReadTotalTimeoutMultiplier,
320 pTimeouts->ReadTotalTimeoutConstant, currentTermios.c_cc[VMIN],
321 currentTermios.c_cc[VTIME]);
322 CommLog_Print(
323 WLOG_WARN, "CommReadFile failed, nNumberOfBytesToRead=%" PRIu32 ", errno=[%d] %s",
324 nNumberOfBytesToRead, errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
325
326 if (errno == EAGAIN)
327 {
328 /* keep on */
329 goto return_true; /* expect a read-loop to be implemented on the server side */
330 }
331 else if (errno == EBADF)
332 {
333 SetLastError(ERROR_BAD_DEVICE); /* STATUS_INVALID_DEVICE_REQUEST */
334 goto return_false;
335 }
336 else
337 {
338 WINPR_ASSERT(FALSE);
339 SetLastError(ERROR_IO_DEVICE);
340 goto return_false;
341 }
342 }
343
344 if (nbRead == 0)
345 {
346 /* termios timeout */
347 SetLastError(ERROR_TIMEOUT);
348 goto return_false;
349 }
350
351 *lpNumberOfBytesRead = WINPR_ASSERTING_INT_CAST(UINT32, nbRead);
352
353 EnterCriticalSection(&pComm->EventsLock);
354 if (pComm->PendingEvents & SERIAL_EV_WINPR_WAITING)
355 {
356 if (pComm->eventChar != '\0' &&
357 memchr(lpBuffer, pComm->eventChar, WINPR_ASSERTING_INT_CAST(size_t, nbRead)))
358 pComm->PendingEvents |= SERIAL_EV_RXCHAR;
359 }
360 LeaveCriticalSection(&pComm->EventsLock);
361 goto return_true;
362 }
363
364 WINPR_ASSERT(FALSE);
365 *lpNumberOfBytesRead = 0;
366return_false:
367 LeaveCriticalSection(&pComm->ReadLock);
368 return FALSE;
369return_true:
370 LeaveCriticalSection(&pComm->ReadLock);
371 return TRUE;
372}
373
381BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
382 LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
383{
384 WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
385 struct timeval tmaxTimeout;
386 struct timeval* pTmaxTimeout = nullptr;
387
388 if (!CommIsHandled(hDevice))
389 return FALSE;
390
391 EnterCriticalSection(&pComm->WriteLock); /* KISSer by the function's beginning */
392
393 if (lpOverlapped != nullptr)
394 {
395 SetLastError(ERROR_NOT_SUPPORTED);
396 goto return_false;
397 }
398
399 if (lpNumberOfBytesWritten == nullptr)
400 {
401 SetLastError(
402 ERROR_INVALID_PARAMETER); /* since we doesn't support lpOverlapped != nullptr */
403 goto return_false;
404 }
405
406 *lpNumberOfBytesWritten = 0; /* will be adjusted if required ... */
407
408 if (nNumberOfBytesToWrite <= 0)
409 {
410 goto return_true; /* FIXME: or FALSE? */
411 }
412
413 /* FIXME: had expected eventfd_write() to return EAGAIN when
414 * there is no eventfd_read() but this not the case. */
415 /* discard a possible and no more relevant event */
416
417#if defined(WINPR_HAVE_SYS_EVENTFD_H)
418 {
419 eventfd_t val = 0;
420 (void)eventfd_read(pComm->fd_write_event, &val);
421 }
422#endif
423
424 {
425 /* ms */
426 const LONGLONG Tmax =
427 1ll * nNumberOfBytesToWrite * pComm->timeouts.WriteTotalTimeoutMultiplier +
428 1ll * pComm->timeouts.WriteTotalTimeoutConstant;
429 /* NB: select() may update the timeout argument to indicate
430 * how much time was left. Keep the timeout variable out of
431 * the while() */
432 pTmaxTimeout = &tmaxTimeout;
433 ZeroMemory(pTmaxTimeout, sizeof(struct timeval));
434
435 if (Tmax > 0)
436 {
437 pTmaxTimeout->tv_sec = Tmax / 1000; /* s */
438 pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000; /* us */
439 }
440 else if ((pComm->timeouts.WriteTotalTimeoutMultiplier == 0) &&
441 (pComm->timeouts.WriteTotalTimeoutConstant == 0))
442 {
443 pTmaxTimeout = nullptr;
444 }
445 }
446
447 /* else return immdiately */
448
449 while (*lpNumberOfBytesWritten < nNumberOfBytesToWrite)
450 {
451 int biggestFd = -1;
452 fd_set event_set;
453 fd_set write_set;
454 int nbFds = 0;
455 biggestFd = pComm->fd_write;
456
457 if (pComm->fd_write_event > biggestFd)
458 biggestFd = pComm->fd_write_event;
459
460 /* see CommReadFile() */
461 if ((pComm->fd_write_event >= FD_SETSIZE) || (pComm->fd_write >= FD_SETSIZE))
462 {
463 CommLog_Print(WLOG_WARN, "CommWriteFile failed, descriptor above FD_SETSIZE=%d",
464 FD_SETSIZE);
465 SetLastError(ERROR_IO_DEVICE);
466 goto return_false;
467 }
468
469 FD_ZERO(&event_set);
470 FD_ZERO(&write_set);
471 FD_SET(pComm->fd_write_event, &event_set);
472 FD_SET(pComm->fd_write, &write_set);
473 nbFds = select(biggestFd + 1, &event_set, &write_set, nullptr, pTmaxTimeout);
474
475 if (nbFds < 0)
476 {
477 char ebuffer[256] = WINPR_C_ARRAY_INIT;
478 CommLog_Print(WLOG_WARN, "select() failure, errno=[%d] %s\n", errno,
479 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
480 SetLastError(ERROR_IO_DEVICE);
481 goto return_false;
482 }
483
484 if (nbFds == 0)
485 {
486 /* timeout */
487 SetLastError(ERROR_TIMEOUT);
488 goto return_false;
489 }
490
491 /* event_set */
492
493 if (FD_ISSET(pComm->fd_write_event, &event_set))
494 {
495#if defined(WINPR_HAVE_SYS_EVENTFD_H)
496 eventfd_t event = 0;
497
498 if (eventfd_read(pComm->fd_write_event, &event) < 0)
499 {
500 if (errno == EAGAIN)
501 {
502 WINPR_ASSERT(FALSE); /* not quite sure this should ever happen */
503 /* keep on */
504 }
505 else
506 {
507 char ebuffer[256] = WINPR_C_ARRAY_INIT;
508 CommLog_Print(WLOG_WARN,
509 "unexpected error on reading fd_write_event, errno=[%d] %s\n",
510 errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
511 /* FIXME: goto return_false ? */
512 }
513
514 WINPR_ASSERT(errno == EAGAIN);
515 }
516
517 /* The eventfd is a counter: several WINPR_PURGE_TXABORT written by
518 * concurrent purge() requests before this read add up. No other event
519 * expected so far, so any non zero value is an abort. */
520 if (event != 0)
521 {
522 SetLastError(ERROR_CANCELLED);
523 goto return_false;
524 }
525#endif
526 }
527
528 /* write_set */
529
530 if (FD_ISSET(pComm->fd_write, &write_set))
531 {
532 ssize_t nbWritten = 0;
533 const BYTE* ptr = lpBuffer;
534 nbWritten = write(pComm->fd_write, &ptr[*lpNumberOfBytesWritten],
535 nNumberOfBytesToWrite - (*lpNumberOfBytesWritten));
536
537 if (nbWritten < 0)
538 {
539 char ebuffer[256] = WINPR_C_ARRAY_INIT;
540 CommLog_Print(WLOG_WARN,
541 "CommWriteFile failed after %" PRIu32
542 " bytes written, errno=[%d] %s\n",
543 *lpNumberOfBytesWritten, errno,
544 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
545
546 if (errno == EAGAIN)
547 {
548 /* keep on */
549 continue;
550 }
551 else if (errno == EBADF)
552 {
553 SetLastError(ERROR_BAD_DEVICE); /* STATUS_INVALID_DEVICE_REQUEST */
554 goto return_false;
555 }
556 else
557 {
558 WINPR_ASSERT(FALSE);
559 SetLastError(ERROR_IO_DEVICE);
560 goto return_false;
561 }
562 }
563
564 *lpNumberOfBytesWritten += nbWritten;
565 }
566 } /* while */
567
568 /* FIXME: this call to tcdrain() doesn't look correct and
569 * might hide a bug but was required while testing a serial
570 * printer. Its driver was expecting the modem line status
571 * SERIAL_MSR_DSR true after the sending which was never
572 * happening otherwise. A purge was also done before each
573 * Write operation. The serial port was opened with:
574 * DesiredAccess=0x0012019F. The printer worked fine with
575 * mstsc. */
576 tcdrain(pComm->fd_write);
577
578return_true:
579 LeaveCriticalSection(&pComm->WriteLock);
580 return TRUE;
581
582return_false:
583 LeaveCriticalSection(&pComm->WriteLock);
584 return FALSE;
585}