/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpdr.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/rdpdr.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 646 - (show annotations)
Fri Apr 2 15:34:38 2004 UTC (20 years, 1 month ago) by forsberg
File MIME type: text/plain
File size: 21318 byte(s)
Made it possible to set the client name displayed to the right of the
redirected disks (in Windows explorer) using -r:clientname=<client name>.

1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/time.h>
4 #include <dirent.h> /* opendir, closedir, readdir */
5 #include <time.h>
6 #include "rdesktop.h"
7
8 #define IRP_MJ_CREATE 0x00
9 #define IRP_MJ_CLOSE 0x02
10 #define IRP_MJ_READ 0x03
11 #define IRP_MJ_WRITE 0x04
12 #define IRP_MJ_DEVICE_CONTROL 0x0e
13
14 #define IRP_MJ_CREATE 0x00
15 #define IRP_MJ_CLOSE 0x02
16 #define IRP_MJ_READ 0x03
17 #define IRP_MJ_WRITE 0x04
18 #define IRP_MJ_QUERY_INFORMATION 0x05
19 #define IRP_MJ_SET_INFORMATION 0x06
20 #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
21 #define IRP_MJ_DIRECTORY_CONTROL 0x0c
22 #define IRP_MJ_DEVICE_CONTROL 0x0e
23
24 #define IRP_MN_QUERY_DIRECTORY 0x01
25 #define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02
26
27 extern char hostname[16];
28 extern DEVICE_FNS serial_fns;
29 extern DEVICE_FNS printer_fns;
30 extern DEVICE_FNS parallel_fns;
31 extern DEVICE_FNS disk_fns;
32 extern FILEINFO g_fileinfo[];
33
34 static VCHANNEL *rdpdr_channel;
35
36 /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
37 HANDLE g_min_timeout_fd;
38 uint32 g_num_devices;
39
40 /* Table with information about rdpdr devices */
41 RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
42 char * g_rdpdr_clientname = NULL;
43
44 /* Used to store incoming io request, until they are ready to be completed */
45 /* using a linked list ensures that they are processed in the right order, */
46 /* if multiple ios are being done on the same fd */
47 struct async_iorequest
48 {
49 uint32 fd, major, minor, offset, device, id, length, partial_len;
50 long timeout, /* Total timeout */
51 itv_timeout; /* Interval timeout (between serial characters) */
52 uint8 *buffer;
53 DEVICE_FNS *fns;
54
55 struct async_iorequest *next; /* next element in list */
56 };
57
58 struct async_iorequest *g_iorequest;
59
60 /* Return device_id for a given handle */
61 int
62 get_device_index(HANDLE handle)
63 {
64 int i;
65 for (i = 0; i < RDPDR_MAX_DEVICES; i++)
66 {
67 if (g_rdpdr_device[i].handle == handle)
68 return i;
69 }
70 return -1;
71 }
72
73 /* Converts a windows path to a unix path */
74 void
75 convert_to_unix_filename(char *filename)
76 {
77 char *p;
78
79 while ((p = strchr(filename, '\\')))
80 {
81 *p = '/';
82 }
83 }
84
85 BOOL
86 rdpdr_handle_ok(int device, int handle)
87 {
88 switch (g_rdpdr_device[device].device_type)
89 {
90 case DEVICE_TYPE_PARALLEL:
91 case DEVICE_TYPE_SERIAL:
92 case DEVICE_TYPE_PRINTER:
93 case DEVICE_TYPE_SCARD:
94 if (g_rdpdr_device[device].handle != handle)
95 return False;
96 break;
97 case DEVICE_TYPE_DISK:
98 if (g_fileinfo[handle].device_id != device)
99 return False;
100 break;
101 }
102 return True;
103 }
104
105 /* Add a new io request to the table containing pending io requests so it won't block rdesktop */
106 BOOL
107 add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,
108 DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
109 uint32 offset)
110 {
111 struct async_iorequest *iorq;
112
113 if (g_iorequest == NULL)
114 {
115 g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
116 if (!g_iorequest)
117 return False;
118 g_iorequest->fd = 0;
119 g_iorequest->next = NULL;
120 }
121
122 iorq = g_iorequest;
123
124 while (iorq->fd != 0)
125 {
126 // create new element if needed
127 if (iorq->next == NULL)
128 {
129 iorq->next =
130 (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
131 if (!iorq->next)
132 return False;
133 iorq->next->fd = 0;
134 iorq->next->next = NULL;
135 }
136 iorq = iorq->next;
137 }
138 iorq->device = device;
139 iorq->fd = file;
140 iorq->id = id;
141 iorq->major = major;
142 iorq->length = length;
143 iorq->partial_len = 0;
144 iorq->fns = fns;
145 iorq->timeout = total_timeout;
146 iorq->itv_timeout = interval_timeout;
147 iorq->buffer = buffer;
148 iorq->offset = offset;
149 return True;
150 }
151
152 void
153 rdpdr_send_connect(void)
154 {
155 uint8 magic[4] = "rDCC";
156 STREAM s;
157
158 s = channel_init(rdpdr_channel, 12);
159 out_uint8a(s, magic, 4);
160 out_uint16_le(s, 1); /* unknown */
161 out_uint16_le(s, 5);
162 out_uint32_be(s, 0x815ed39d); /* IP address (use 127.0.0.1) 0x815ed39d */
163 s_mark_end(s);
164 channel_send(s, rdpdr_channel);
165 }
166
167
168 void
169 rdpdr_send_name(void)
170 {
171 uint8 magic[4] = "rDNC";
172 if (NULL == g_rdpdr_clientname) {
173 g_rdpdr_clientname = hostname;
174 }
175 uint32 hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
176 STREAM s;
177
178 s = channel_init(rdpdr_channel, 16 + hostlen);
179 out_uint8a(s, magic, 4);
180 out_uint16_le(s, 0x63); /* unknown */
181 out_uint16_le(s, 0x72);
182 out_uint32(s, 0);
183 out_uint32_le(s, hostlen);
184 rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
185 s_mark_end(s);
186 channel_send(s, rdpdr_channel);
187 }
188
189 /* Returns the size of the payload of the announce packet */
190 int
191 announcedata_size()
192 {
193 int size, i;
194 PRINTER *printerinfo;
195
196 size = 8; //static announce size
197 size += g_num_devices * 0x14;
198
199 for (i = 0; i < g_num_devices; i++)
200 {
201 if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)
202 {
203 printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
204 printerinfo->bloblen =
205 printercache_load_blob(printerinfo->printer, &(printerinfo->blob));
206
207 size += 0x18;
208 size += 2 * strlen(printerinfo->driver) + 2;
209 size += 2 * strlen(printerinfo->printer) + 2;
210 size += printerinfo->bloblen;
211 }
212 }
213
214 return size;
215 }
216
217 void
218 rdpdr_send_available(void)
219 {
220
221 uint8 magic[4] = "rDAD";
222 uint32 driverlen, printerlen, bloblen;
223 int i;
224 STREAM s;
225 PRINTER *printerinfo;
226
227 s = channel_init(rdpdr_channel, announcedata_size());
228 out_uint8a(s, magic, 4);
229 out_uint32_le(s, g_num_devices);
230
231 for (i = 0; i < g_num_devices; i++)
232 {
233 out_uint32_le(s, g_rdpdr_device[i].device_type);
234 out_uint32_le(s, i); /* RDP Device ID */
235 out_uint8p(s, g_rdpdr_device[i].name, 8);
236
237 switch (g_rdpdr_device[i].device_type)
238 {
239 case DEVICE_TYPE_PRINTER:
240 printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
241
242 driverlen = 2 * strlen(printerinfo->driver) + 2;
243 printerlen = 2 * strlen(printerinfo->printer) + 2;
244 bloblen = printerinfo->bloblen;
245
246 out_uint32_le(s, 24 + driverlen + printerlen + bloblen); /* length of extra info */
247 out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
248 out_uint8s(s, 8); /* unknown */
249 out_uint32_le(s, driverlen);
250 out_uint32_le(s, printerlen);
251 out_uint32_le(s, bloblen);
252 rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
253 rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
254 out_uint8a(s, printerinfo->blob, bloblen);
255
256 if (printerinfo->blob)
257 xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */
258 break;
259 default:
260 out_uint32(s, 0);
261 }
262 }
263 #if 0
264 out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */
265 out_uint32_le(s, 0);
266 out_uint8p(s, "SCARD", 5);
267 out_uint8s(s, 3);
268 out_uint32(s, 0);
269 #endif
270
271 s_mark_end(s);
272 channel_send(s, rdpdr_channel);
273 }
274
275 void
276 rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,
277 uint32 length)
278 {
279 uint8 magic[4] = "rDCI";
280 STREAM s;
281
282 s = channel_init(rdpdr_channel, 20 + length);
283 out_uint8a(s, magic, 4);
284 out_uint32_le(s, device);
285 out_uint32_le(s, id);
286 out_uint32_le(s, status);
287 out_uint32_le(s, result);
288 out_uint8p(s, buffer, length);
289 s_mark_end(s);
290 /* JIF
291 hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
292 channel_send(s, rdpdr_channel);
293 }
294
295 static void
296 rdpdr_process_irp(STREAM s)
297 {
298 uint32 result = 0,
299 length = 0,
300 desired_access = 0,
301 request,
302 file,
303 info_level,
304 buffer_len,
305 id,
306 major,
307 minor,
308 device,
309 offset,
310 bytes_in,
311 bytes_out,
312 error_mode,
313 share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
314
315 char filename[256];
316 uint8 *buffer, *pst_buf;
317 struct stream out;
318 DEVICE_FNS *fns;
319 BOOL rw_blocking = True;
320 NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;
321
322 in_uint32_le(s, device);
323 in_uint32_le(s, file);
324 in_uint32_le(s, id);
325 in_uint32_le(s, major);
326 in_uint32_le(s, minor);
327
328 buffer_len = 0;
329 buffer = (uint8 *) xmalloc(1024);
330 buffer[0] = 0;
331
332 switch (g_rdpdr_device[device].device_type)
333 {
334 case DEVICE_TYPE_SERIAL:
335
336 fns = &serial_fns;
337 rw_blocking = False;
338 break;
339
340 case DEVICE_TYPE_PARALLEL:
341
342 fns = &parallel_fns;
343 rw_blocking = False;
344 break;
345
346 case DEVICE_TYPE_PRINTER:
347
348 fns = &printer_fns;
349 break;
350
351 case DEVICE_TYPE_DISK:
352
353 fns = &disk_fns;
354 rw_blocking = False;
355 break;
356
357 case DEVICE_TYPE_SCARD:
358 default:
359
360 error("IRP for bad device %ld\n", device);
361 return;
362 }
363
364 switch (major)
365 {
366 case IRP_MJ_CREATE:
367
368 in_uint32_be(s, desired_access);
369 in_uint8s(s, 0x08); // unknown
370 in_uint32_le(s, error_mode);
371 in_uint32_le(s, share_mode);
372 in_uint32_le(s, disposition);
373 in_uint32_le(s, flags_and_attributes);
374 in_uint32_le(s, length);
375
376 if (length && (length / 2) < 256)
377 {
378 rdp_in_unistr(s, filename, length);
379 convert_to_unix_filename(filename);
380 }
381 else
382 {
383 filename[0] = 0;
384 }
385
386 if (!fns->create)
387 {
388 status = STATUS_NOT_SUPPORTED;
389 break;
390 }
391
392 status = fns->create(device, desired_access, share_mode, disposition,
393 flags_and_attributes, filename, &result);
394 buffer_len = 1;
395 break;
396
397 case IRP_MJ_CLOSE:
398 if (!fns->close)
399 {
400 status = STATUS_NOT_SUPPORTED;
401 break;
402 }
403
404 status = fns->close(file);
405 break;
406
407 case IRP_MJ_READ:
408
409 if (!fns->read)
410 {
411 status = STATUS_NOT_SUPPORTED;
412 break;
413 }
414
415 in_uint32_le(s, length);
416 in_uint32_le(s, offset);
417 #if WITH_DEBUG_RDP5
418 DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
419 #endif
420 if (!rdpdr_handle_ok(device, file))
421 {
422 status = STATUS_INVALID_HANDLE;
423 break;
424 }
425
426 if (rw_blocking) // Complete read immediately
427 {
428 buffer = (uint8 *) xrealloc((void *) buffer, length);
429 if (!buffer)
430 {
431 status = STATUS_CANCELLED;
432 break;
433 }
434 status = fns->read(file, buffer, length, offset, &result);
435 buffer_len = result;
436 break;
437 }
438
439 // Add request to table
440 pst_buf = (uint8 *) xmalloc(length);
441 if (!pst_buf)
442 {
443 status = STATUS_CANCELLED;
444 break;
445 }
446 serial_get_timeout(file, length, &total_timeout, &interval_timeout);
447 if (add_async_iorequest
448 (device, file, id, major, length, fns, total_timeout, interval_timeout,
449 pst_buf, offset))
450 {
451 status = STATUS_PENDING;
452 break;
453 }
454
455 status = STATUS_CANCELLED;
456 break;
457 case IRP_MJ_WRITE:
458
459 buffer_len = 1;
460
461 if (!fns->write)
462 {
463 status = STATUS_NOT_SUPPORTED;
464 break;
465 }
466
467 in_uint32_le(s, length);
468 in_uint32_le(s, offset);
469 in_uint8s(s, 0x18);
470 #if WITH_DEBUG_RDP5
471 DEBUG(("RDPDR IRP Write (length: %d)\n", result));
472 #endif
473 if (!rdpdr_handle_ok(device, file))
474 {
475 status = STATUS_INVALID_HANDLE;
476 break;
477 }
478
479 if (rw_blocking) // Complete immediately
480 {
481 status = fns->write(file, s->p, length, offset, &result);
482 break;
483 }
484
485 // Add to table
486 pst_buf = (uint8 *) xmalloc(length);
487 if (!pst_buf)
488 {
489 status = STATUS_CANCELLED;
490 break;
491 }
492
493 in_uint8a(s, pst_buf, length);
494
495 if (add_async_iorequest
496 (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
497 {
498 status = STATUS_PENDING;
499 break;
500 }
501
502 status = STATUS_CANCELLED;
503 break;
504
505 case IRP_MJ_QUERY_INFORMATION:
506
507 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
508 {
509 status = STATUS_INVALID_HANDLE;
510 break;
511 }
512 in_uint32_le(s, info_level);
513
514 out.data = out.p = buffer;
515 out.size = sizeof(buffer);
516 status = disk_query_information(file, info_level, &out);
517 result = buffer_len = out.p - out.data;
518
519 break;
520
521 case IRP_MJ_SET_INFORMATION:
522
523 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
524 {
525 status = STATUS_INVALID_HANDLE;
526 break;
527 }
528
529 in_uint32_le(s, info_level);
530
531 out.data = out.p = buffer;
532 out.size = sizeof(buffer);
533 status = disk_set_information(file, info_level, s, &out);
534 result = buffer_len = out.p - out.data;
535 break;
536
537 case IRP_MJ_QUERY_VOLUME_INFORMATION:
538
539 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
540 {
541 status = STATUS_INVALID_HANDLE;
542 break;
543 }
544
545 in_uint32_le(s, info_level);
546
547 out.data = out.p = buffer;
548 out.size = sizeof(buffer);
549 status = disk_query_volume_information(file, info_level, &out);
550 result = buffer_len = out.p - out.data;
551 break;
552
553 case IRP_MJ_DIRECTORY_CONTROL:
554
555 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
556 {
557 status = STATUS_INVALID_HANDLE;
558 break;
559 }
560
561 switch (minor)
562 {
563 case IRP_MN_QUERY_DIRECTORY:
564
565 in_uint32_le(s, info_level);
566 in_uint8s(s, 1);
567 in_uint32_le(s, length);
568 in_uint8s(s, 0x17);
569 if (length && length < 2 * 255)
570 {
571 rdp_in_unistr(s, filename, length);
572 convert_to_unix_filename(filename);
573 }
574 else
575 {
576 filename[0] = 0;
577 }
578 out.data = out.p = buffer;
579 out.size = sizeof(buffer);
580 status = disk_query_directory(file, info_level, filename,
581 &out);
582 result = buffer_len = out.p - out.data;
583 if (!buffer_len)
584 buffer_len++;
585 break;
586
587 case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
588
589 /* JIF
590 unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
591 status = STATUS_PENDING; // Don't send completion packet
592 break;
593
594 default:
595
596 status = STATUS_INVALID_PARAMETER;
597 /* JIF
598 unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */
599 }
600 break;
601
602 case IRP_MJ_DEVICE_CONTROL:
603
604 if (!fns->device_control)
605 {
606 status = STATUS_NOT_SUPPORTED;
607 break;
608 }
609
610 in_uint32_le(s, bytes_out);
611 in_uint32_le(s, bytes_in);
612 in_uint32_le(s, request);
613 in_uint8s(s, 0x14);
614
615 buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
616 if (!buffer)
617 {
618 status = STATUS_CANCELLED;
619 break;
620 }
621
622 out.data = out.p = buffer;
623 out.size = sizeof(buffer);
624 status = fns->device_control(file, request, s, &out);
625 result = buffer_len = out.p - out.data;
626 break;
627
628 default:
629 unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
630 break;
631 }
632
633 if (status != STATUS_PENDING)
634 {
635 rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
636 }
637 if (buffer)
638 xfree(buffer);
639 buffer = NULL;
640 }
641
642 void
643 rdpdr_send_clientcapabilty(void)
644 {
645 uint8 magic[4] = "rDPC";
646 STREAM s;
647
648 s = channel_init(rdpdr_channel, 0x50);
649 out_uint8a(s, magic, 4);
650 out_uint32_le(s, 5); /* count */
651 out_uint16_le(s, 1); /* first */
652 out_uint16_le(s, 0x28); /* length */
653 out_uint32_le(s, 1);
654 out_uint32_le(s, 2);
655 out_uint16_le(s, 2);
656 out_uint16_le(s, 5);
657 out_uint16_le(s, 1);
658 out_uint16_le(s, 5);
659 out_uint16_le(s, 0xFFFF);
660 out_uint16_le(s, 0);
661 out_uint32_le(s, 0);
662 out_uint32_le(s, 3);
663 out_uint32_le(s, 0);
664 out_uint32_le(s, 0);
665 out_uint16_le(s, 2); /* second */
666 out_uint16_le(s, 8); /* length */
667 out_uint32_le(s, 1);
668 out_uint16_le(s, 3); /* third */
669 out_uint16_le(s, 8); /* length */
670 out_uint32_le(s, 1);
671 out_uint16_le(s, 4); /* fourth */
672 out_uint16_le(s, 8); /* length */
673 out_uint32_le(s, 1);
674 out_uint16_le(s, 5); /* fifth */
675 out_uint16_le(s, 8); /* length */
676 out_uint32_le(s, 1);
677
678 s_mark_end(s);
679 channel_send(s, rdpdr_channel);
680 }
681
682 static void
683 rdpdr_process(STREAM s)
684 {
685 uint32 handle;
686 uint8 *magic;
687
688 #if WITH_DEBUG_RDP5
689 printf("--- rdpdr_process ---\n");
690 hexdump(s->p, s->end - s->p);
691 #endif
692 in_uint8p(s, magic, 4);
693
694 if ((magic[0] == 'r') && (magic[1] == 'D'))
695 {
696 if ((magic[2] == 'R') && (magic[3] == 'I'))
697 {
698 rdpdr_process_irp(s);
699 return;
700 }
701 if ((magic[2] == 'n') && (magic[3] == 'I'))
702 {
703 rdpdr_send_connect();
704 rdpdr_send_name();
705 return;
706 }
707 if ((magic[2] == 'C') && (magic[3] == 'C'))
708 {
709 /* connect from server */
710 rdpdr_send_clientcapabilty();
711 rdpdr_send_available();
712 return;
713 }
714 if ((magic[2] == 'r') && (magic[3] == 'd'))
715 {
716 /* connect to a specific resource */
717 in_uint32(s, handle);
718 #if WITH_DEBUG_RDP5
719 DEBUG(("RDPDR: Server connected to resource %d\n", handle));
720 #endif
721 return;
722 }
723 if ((magic[2] == 'P') && (magic[3] == 'S'))
724 {
725 /* server capability */
726 return;
727 }
728 }
729 if ((magic[0] == 'R') && (magic[1] == 'P'))
730 {
731 if ((magic[2] == 'C') && (magic[3] == 'P'))
732 {
733 printercache_process(s);
734 return;
735 }
736 }
737 unimpl("RDPDR packet type %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
738 }
739
740 BOOL
741 rdpdr_init()
742 {
743 if (g_num_devices > 0)
744 {
745 rdpdr_channel =
746 channel_register("rdpdr",
747 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
748 rdpdr_process);
749 }
750
751 return (rdpdr_channel != NULL);
752 }
753
754 /* Add file descriptors of pending io request to select() */
755 void
756 rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout)
757 {
758 uint32 select_timeout = 0; // Timeout value to be used for select() (in millisecons).
759 struct async_iorequest *iorq;
760
761 iorq = g_iorequest;
762 while (iorq != NULL)
763 {
764 if (iorq->fd != 0)
765 {
766 switch (iorq->major)
767 {
768 case IRP_MJ_READ:
769
770 FD_SET(iorq->fd, rfds);
771
772 // Check if io request timeout is smaller than current (but not 0).
773 if (iorq->timeout
774 && (select_timeout == 0
775 || iorq->timeout < select_timeout))
776 {
777 // Set new timeout
778 select_timeout = iorq->timeout;
779 g_min_timeout_fd = iorq->fd; /* Remember fd */
780 tv->tv_sec = select_timeout / 1000;
781 tv->tv_usec = (select_timeout % 1000) * 1000;
782 *timeout = True;
783 }
784 break;
785
786 case IRP_MJ_WRITE:
787 FD_SET(iorq->fd, wfds);
788 break;
789
790 }
791 *n = MAX(*n, iorq->fd);
792 }
793
794 iorq = iorq->next;
795 }
796 }
797
798 struct async_iorequest *
799 rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
800 {
801 if (!iorq)
802 return NULL;
803
804 if (iorq->buffer)
805 xfree(iorq->buffer);
806 if (prev)
807 {
808 prev->next = iorq->next;
809 xfree(iorq);
810 iorq = prev->next;
811 }
812 else
813 {
814 // Even if NULL
815 g_iorequest = iorq->next;
816 xfree(iorq);
817 iorq = NULL;
818 }
819 return iorq;
820 }
821
822 /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
823 void
824 rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
825 {
826 NTSTATUS status;
827 uint32 result = 0;
828 DEVICE_FNS *fns;
829 struct async_iorequest *iorq;
830 struct async_iorequest *prev;
831 uint32 req_size = 0;
832
833 if (timed_out)
834 {
835 rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);
836 return;
837 }
838
839 iorq = g_iorequest;
840 prev = NULL;
841 while (iorq != NULL)
842 {
843 if (iorq->fd != 0)
844 {
845 switch (iorq->major)
846 {
847 case IRP_MJ_READ:
848 if (FD_ISSET(iorq->fd, rfds))
849 {
850 /* Read the data */
851 fns = iorq->fns;
852
853 req_size =
854 (iorq->length - iorq->partial_len) >
855 8192 ? 8192 : (iorq->length -
856 iorq->partial_len);
857 /* never read larger chunks than 8k - chances are that it will block */
858 status = fns->read(iorq->fd,
859 iorq->buffer + iorq->partial_len,
860 req_size, iorq->offset, &result);
861
862 if (result > 0)
863 {
864 iorq->partial_len += result;
865 iorq->offset += result;
866 }
867 #if WITH_DEBUG_RDP5
868 DEBUG(("RDPDR: %d bytes of data read\n", result));
869 #endif
870 /* only delete link if all data has been transfered */
871 /* or if result was 0 and status success - EOF */
872 if ((iorq->partial_len == iorq->length) ||
873 (result == 0))
874 {
875 #if WITH_DEBUG_RDP5
876 DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
877 #endif
878 rdpdr_send_completion(iorq->device,
879 iorq->id, status,
880 iorq->partial_len,
881 iorq->buffer,
882 iorq->partial_len);
883 iorq = rdpdr_remove_iorequest(prev, iorq);
884 }
885 }
886 break;
887 case IRP_MJ_WRITE:
888 if (FD_ISSET(iorq->fd, wfds))
889 {
890 /* Write data. */
891 fns = iorq->fns;
892
893 req_size =
894 (iorq->length - iorq->partial_len) >
895 8192 ? 8192 : (iorq->length -
896 iorq->partial_len);
897
898 /* never write larger chunks than 8k - chances are that it will block */
899 status = fns->write(iorq->fd,
900 iorq->buffer +
901 iorq->partial_len, req_size,
902 iorq->offset, &result);
903
904 if (result > 0)
905 {
906 iorq->partial_len += result;
907 iorq->offset += result;
908 }
909
910 #if WITH_DEBUG_RDP5
911 DEBUG(("RDPDR: %d bytes of data written\n",
912 result));
913 #endif
914 /* only delete link if all data has been transfered */
915 /* or we couldn't write */
916 if ((iorq->partial_len == iorq->length)
917 || (result == 0))
918 {
919 #if WITH_DEBUG_RDP5
920 DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
921 #endif
922 rdpdr_send_completion(iorq->device,
923 iorq->id, status,
924 iorq->partial_len,
925 (uint8 *) "", 1);
926
927 iorq = rdpdr_remove_iorequest(prev, iorq);
928 }
929 }
930 break;
931 }
932
933 }
934 prev = iorq;
935 if (iorq)
936 iorq = iorq->next;
937 }
938
939 }
940
941 /* Abort a pending io request for a given handle and major */
942 BOOL
943 rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
944 {
945 uint32 result;
946 struct async_iorequest *iorq;
947 struct async_iorequest *prev;
948
949 iorq = g_iorequest;
950 prev = NULL;
951 while (iorq != NULL)
952 {
953 // Only remove from table when major is not set, or when correct major is supplied.
954 // Abort read should not abort a write io request.
955 if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
956 {
957 result = 0;
958 rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
959 1);
960
961 iorq = rdpdr_remove_iorequest(prev, iorq);
962 return True;
963 }
964
965 prev = iorq;
966 iorq = iorq->next;
967 }
968
969 return False;
970 }

  ViewVC Help
Powered by ViewVC 1.1.26