VirtualBox

source: vbox/trunk/src/VBox/RDP/client/rdpdr.c@ 10753

Last change on this file since 10753 was 9902, checked in by vboxsync, 17 years ago

Added rdesktop 1.6.0.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.7 KB
Line 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Copyright (C) Matthew Chapman 1999-2007
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 Here are some resources, for your IRP hacking pleasure:
22
23 http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/winddk.h?view=markup
24
25 http://win32.mvps.org/ntfs/streams.cpp
26
27 http://www.acc.umu.se/~bosse/ntifs.h
28
29 http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/File/
30
31 http://us1.samba.org/samba/ftp/specs/smb-nt01.txt
32
33 http://www.osronline.com/
34*/
35
36#include <unistd.h>
37#include <sys/types.h>
38#include <sys/time.h>
39#include <dirent.h> /* opendir, closedir, readdir */
40#include <time.h>
41#include <errno.h>
42#include "rdesktop.h"
43
44#define IRP_MJ_CREATE 0x00
45#define IRP_MJ_CLOSE 0x02
46#define IRP_MJ_READ 0x03
47#define IRP_MJ_WRITE 0x04
48#define IRP_MJ_QUERY_INFORMATION 0x05
49#define IRP_MJ_SET_INFORMATION 0x06
50#define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
51#define IRP_MJ_DIRECTORY_CONTROL 0x0c
52#define IRP_MJ_DEVICE_CONTROL 0x0e
53#define IRP_MJ_LOCK_CONTROL 0x11
54
55#define IRP_MN_QUERY_DIRECTORY 0x01
56#define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02
57
58extern char g_hostname[16];
59extern DEVICE_FNS serial_fns;
60extern DEVICE_FNS printer_fns;
61extern DEVICE_FNS parallel_fns;
62extern DEVICE_FNS disk_fns;
63#ifdef WITH_SCARD
64extern DEVICE_FNS scard_fns;
65#endif
66extern FILEINFO g_fileinfo[];
67extern RD_BOOL g_notify_stamp;
68
69static VCHANNEL *rdpdr_channel;
70
71/* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
72RD_NTHANDLE g_min_timeout_fd;
73uint32 g_num_devices;
74
75/* Table with information about rdpdr devices */
76RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
77char *g_rdpdr_clientname = NULL;
78
79/* Used to store incoming io request, until they are ready to be completed */
80/* using a linked list ensures that they are processed in the right order, */
81/* if multiple ios are being done on the same fd */
82struct async_iorequest
83{
84 uint32 fd, major, minor, offset, device, id, length, partial_len;
85 long timeout, /* Total timeout */
86 itv_timeout; /* Interval timeout (between serial characters) */
87 uint8 *buffer;
88 DEVICE_FNS *fns;
89
90 struct async_iorequest *next; /* next element in list */
91};
92
93struct async_iorequest *g_iorequest;
94
95/* Return device_id for a given handle */
96int
97get_device_index(RD_NTHANDLE handle)
98{
99 int i;
100 for (i = 0; i < RDPDR_MAX_DEVICES; i++)
101 {
102 if (g_rdpdr_device[i].handle == handle)
103 return i;
104 }
105 return -1;
106}
107
108/* Converts a windows path to a unix path */
109void
110convert_to_unix_filename(char *filename)
111{
112 char *p;
113
114 while ((p = strchr(filename, '\\')))
115 {
116 *p = '/';
117 }
118}
119
120static RD_BOOL
121rdpdr_handle_ok(int device, int handle)
122{
123 switch (g_rdpdr_device[device].device_type)
124 {
125 case DEVICE_TYPE_PARALLEL:
126 case DEVICE_TYPE_SERIAL:
127 case DEVICE_TYPE_PRINTER:
128 case DEVICE_TYPE_SCARD:
129 if (g_rdpdr_device[device].handle != handle)
130 return False;
131 break;
132 case DEVICE_TYPE_DISK:
133 if (g_fileinfo[handle].device_id != device)
134 return False;
135 break;
136 }
137 return True;
138}
139
140/* Add a new io request to the table containing pending io requests so it won't block rdesktop */
141static RD_BOOL
142add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,
143 DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
144 uint32 offset)
145{
146 struct async_iorequest *iorq;
147
148 if (g_iorequest == NULL)
149 {
150 g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
151 if (!g_iorequest)
152 return False;
153 g_iorequest->fd = 0;
154 g_iorequest->next = NULL;
155 }
156
157 iorq = g_iorequest;
158
159 while (iorq->fd != 0)
160 {
161 /* create new element if needed */
162 if (iorq->next == NULL)
163 {
164 iorq->next =
165 (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
166 if (!iorq->next)
167 return False;
168 iorq->next->fd = 0;
169 iorq->next->next = NULL;
170 }
171 iorq = iorq->next;
172 }
173 iorq->device = device;
174 iorq->fd = file;
175 iorq->id = id;
176 iorq->major = major;
177 iorq->length = length;
178 iorq->partial_len = 0;
179 iorq->fns = fns;
180 iorq->timeout = total_timeout;
181 iorq->itv_timeout = interval_timeout;
182 iorq->buffer = buffer;
183 iorq->offset = offset;
184 return True;
185}
186
187static void
188rdpdr_send_connect(void)
189{
190 uint8 magic[4] = "rDCC";
191 STREAM s;
192
193 s = channel_init(rdpdr_channel, 12);
194 out_uint8a(s, magic, 4);
195 out_uint16_le(s, 1); /* unknown */
196 out_uint16_le(s, 5);
197 out_uint32_be(s, 0x815ed39d); /* IP address (use 127.0.0.1) 0x815ed39d */
198 s_mark_end(s);
199 channel_send(s, rdpdr_channel);
200}
201
202
203static void
204rdpdr_send_name(void)
205{
206 uint8 magic[4] = "rDNC";
207 STREAM s;
208 uint32 hostlen;
209
210 if (NULL == g_rdpdr_clientname)
211 {
212 g_rdpdr_clientname = g_hostname;
213 }
214 hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
215
216 s = channel_init(rdpdr_channel, 16 + hostlen);
217 out_uint8a(s, magic, 4);
218 out_uint16_le(s, 0x63); /* unknown */
219 out_uint16_le(s, 0x72);
220 out_uint32(s, 0);
221 out_uint32_le(s, hostlen);
222 rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
223 s_mark_end(s);
224 channel_send(s, rdpdr_channel);
225}
226
227/* Returns the size of the payload of the announce packet */
228static int
229announcedata_size()
230{
231 int size, i;
232 PRINTER *printerinfo;
233
234 size = 8; /* static announce size */
235 size += g_num_devices * 0x14;
236
237 for (i = 0; i < g_num_devices; i++)
238 {
239 if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)
240 {
241 printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
242 printerinfo->bloblen =
243 printercache_load_blob(printerinfo->printer, &(printerinfo->blob));
244
245 size += 0x18;
246 size += 2 * strlen(printerinfo->driver) + 2;
247 size += 2 * strlen(printerinfo->printer) + 2;
248 size += printerinfo->bloblen;
249 }
250 }
251
252 return size;
253}
254
255static void
256rdpdr_send_available(void)
257{
258
259 uint8 magic[4] = "rDAD";
260 uint32 driverlen, printerlen, bloblen;
261 int i;
262 STREAM s;
263 PRINTER *printerinfo;
264
265 s = channel_init(rdpdr_channel, announcedata_size());
266 out_uint8a(s, magic, 4);
267 out_uint32_le(s, g_num_devices);
268
269 for (i = 0; i < g_num_devices; i++)
270 {
271 out_uint32_le(s, g_rdpdr_device[i].device_type);
272 out_uint32_le(s, i); /* RDP Device ID */
273 /* Is it possible to use share names longer than 8 chars?
274 /astrand */
275 out_uint8p(s, g_rdpdr_device[i].name, 8);
276
277 switch (g_rdpdr_device[i].device_type)
278 {
279 case DEVICE_TYPE_PRINTER:
280 printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
281
282 driverlen = 2 * strlen(printerinfo->driver) + 2;
283 printerlen = 2 * strlen(printerinfo->printer) + 2;
284 bloblen = printerinfo->bloblen;
285
286 out_uint32_le(s, 24 + driverlen + printerlen + bloblen); /* length of extra info */
287 out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
288 out_uint8s(s, 8); /* unknown */
289 out_uint32_le(s, driverlen);
290 out_uint32_le(s, printerlen);
291 out_uint32_le(s, bloblen);
292 rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
293 rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
294 out_uint8a(s, printerinfo->blob, bloblen);
295
296 if (printerinfo->blob)
297 xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */
298 break;
299 default:
300 out_uint32(s, 0);
301 }
302 }
303#if 0
304 out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */
305 out_uint32_le(s, 0);
306 out_uint8p(s, "SCARD", 5);
307 out_uint8s(s, 3);
308 out_uint32(s, 0);
309#endif
310
311 s_mark_end(s);
312 channel_send(s, rdpdr_channel);
313}
314
315void
316rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,
317 uint32 length)
318{
319 uint8 magic[4] = "rDCI";
320 STREAM s;
321
322#ifdef WITH_SCARD
323 scard_lock(SCARD_LOCK_RDPDR);
324#endif
325 s = channel_init(rdpdr_channel, 20 + length);
326 out_uint8a(s, magic, 4);
327 out_uint32_le(s, device);
328 out_uint32_le(s, id);
329 out_uint32_le(s, status);
330 out_uint32_le(s, result);
331 out_uint8p(s, buffer, length);
332 s_mark_end(s);
333 /* JIF */
334#ifdef WITH_DEBUG_RDP5
335 printf("--> rdpdr_send_completion\n");
336 /* hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
337#endif
338 channel_send(s, rdpdr_channel);
339#ifdef WITH_SCARD
340 scard_unlock(SCARD_LOCK_RDPDR);
341#endif
342}
343
344static void
345rdpdr_process_irp(STREAM s)
346{
347 uint32 result = 0,
348 length = 0,
349 desired_access = 0,
350 request,
351 file,
352 info_level,
353 buffer_len,
354 id,
355 major,
356 minor,
357 device,
358 offset,
359 bytes_in,
360 bytes_out,
361 error_mode,
362 share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
363
364 char filename[PATH_MAX];
365 uint8 *buffer, *pst_buf;
366 struct stream out;
367 DEVICE_FNS *fns;
368 RD_BOOL rw_blocking = True;
369 RD_NTSTATUS status = RD_STATUS_INVALID_DEVICE_REQUEST;
370
371 in_uint32_le(s, device);
372 in_uint32_le(s, file);
373 in_uint32_le(s, id);
374 in_uint32_le(s, major);
375 in_uint32_le(s, minor);
376
377 buffer_len = 0;
378 buffer = (uint8 *) xmalloc(1024);
379 buffer[0] = 0;
380
381 switch (g_rdpdr_device[device].device_type)
382 {
383 case DEVICE_TYPE_SERIAL:
384
385 fns = &serial_fns;
386 rw_blocking = False;
387 break;
388
389 case DEVICE_TYPE_PARALLEL:
390
391 fns = &parallel_fns;
392 rw_blocking = False;
393 break;
394
395 case DEVICE_TYPE_PRINTER:
396
397 fns = &printer_fns;
398 break;
399
400 case DEVICE_TYPE_DISK:
401
402 fns = &disk_fns;
403 rw_blocking = False;
404 break;
405
406 case DEVICE_TYPE_SCARD:
407#ifdef WITH_SCARD
408 fns = &scard_fns;
409 rw_blocking = False;
410 break;
411#endif
412 default:
413
414 error("IRP for bad device %ld\n", device);
415 return;
416 }
417
418 switch (major)
419 {
420 case IRP_MJ_CREATE:
421
422 in_uint32_be(s, desired_access);
423 in_uint8s(s, 0x08); /* unknown */
424 in_uint32_le(s, error_mode);
425 in_uint32_le(s, share_mode);
426 in_uint32_le(s, disposition);
427 in_uint32_le(s, flags_and_attributes);
428 in_uint32_le(s, length);
429
430 if (length && (length / 2) < 256)
431 {
432 rdp_in_unistr(s, filename, sizeof(filename), length);
433 convert_to_unix_filename(filename);
434 }
435 else
436 {
437 filename[0] = 0;
438 }
439
440 if (!fns->create)
441 {
442 status = RD_STATUS_NOT_SUPPORTED;
443 break;
444 }
445
446 status = fns->create(device, desired_access, share_mode, disposition,
447 flags_and_attributes, filename, &result);
448 buffer_len = 1;
449 break;
450
451 case IRP_MJ_CLOSE:
452 if (!fns->close)
453 {
454 status = RD_STATUS_NOT_SUPPORTED;
455 break;
456 }
457
458 status = fns->close(file);
459 break;
460
461 case IRP_MJ_READ:
462
463 if (!fns->read)
464 {
465 status = RD_STATUS_NOT_SUPPORTED;
466 break;
467 }
468
469 in_uint32_le(s, length);
470 in_uint32_le(s, offset);
471#if WITH_DEBUG_RDP5
472 DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
473#endif
474 if (!rdpdr_handle_ok(device, file))
475 {
476 status = RD_STATUS_INVALID_HANDLE;
477 break;
478 }
479
480 if (rw_blocking) /* Complete read immediately */
481 {
482 buffer = (uint8 *) xrealloc((void *) buffer, length);
483 if (!buffer)
484 {
485 status = RD_STATUS_CANCELLED;
486 break;
487 }
488 status = fns->read(file, buffer, length, offset, &result);
489 buffer_len = result;
490 break;
491 }
492
493 /* Add request to table */
494 pst_buf = (uint8 *) xmalloc(length);
495 if (!pst_buf)
496 {
497 status = RD_STATUS_CANCELLED;
498 break;
499 }
500 serial_get_timeout(file, length, &total_timeout, &interval_timeout);
501 if (add_async_iorequest
502 (device, file, id, major, length, fns, total_timeout, interval_timeout,
503 pst_buf, offset))
504 {
505 status = RD_STATUS_PENDING;
506 break;
507 }
508
509 status = RD_STATUS_CANCELLED;
510 break;
511 case IRP_MJ_WRITE:
512
513 buffer_len = 1;
514
515 if (!fns->write)
516 {
517 status = RD_STATUS_NOT_SUPPORTED;
518 break;
519 }
520
521 in_uint32_le(s, length);
522 in_uint32_le(s, offset);
523 in_uint8s(s, 0x18);
524#if WITH_DEBUG_RDP5
525 DEBUG(("RDPDR IRP Write (length: %d)\n", result));
526#endif
527 if (!rdpdr_handle_ok(device, file))
528 {
529 status = RD_STATUS_INVALID_HANDLE;
530 break;
531 }
532
533 if (rw_blocking) /* Complete immediately */
534 {
535 status = fns->write(file, s->p, length, offset, &result);
536 break;
537 }
538
539 /* Add to table */
540 pst_buf = (uint8 *) xmalloc(length);
541 if (!pst_buf)
542 {
543 status = RD_STATUS_CANCELLED;
544 break;
545 }
546
547 in_uint8a(s, pst_buf, length);
548
549 if (add_async_iorequest
550 (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
551 {
552 status = RD_STATUS_PENDING;
553 break;
554 }
555
556 status = RD_STATUS_CANCELLED;
557 break;
558
559 case IRP_MJ_QUERY_INFORMATION:
560
561 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
562 {
563 status = RD_STATUS_INVALID_HANDLE;
564 break;
565 }
566 in_uint32_le(s, info_level);
567
568 out.data = out.p = buffer;
569 out.size = sizeof(buffer);
570 status = disk_query_information(file, info_level, &out);
571 result = buffer_len = out.p - out.data;
572
573 break;
574
575 case IRP_MJ_SET_INFORMATION:
576
577 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
578 {
579 status = RD_STATUS_INVALID_HANDLE;
580 break;
581 }
582
583 in_uint32_le(s, info_level);
584
585 out.data = out.p = buffer;
586 out.size = sizeof(buffer);
587 status = disk_set_information(file, info_level, s, &out);
588 result = buffer_len = out.p - out.data;
589 break;
590
591 case IRP_MJ_QUERY_VOLUME_INFORMATION:
592
593 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
594 {
595 status = RD_STATUS_INVALID_HANDLE;
596 break;
597 }
598
599 in_uint32_le(s, info_level);
600
601 out.data = out.p = buffer;
602 out.size = sizeof(buffer);
603 status = disk_query_volume_information(file, info_level, &out);
604 result = buffer_len = out.p - out.data;
605 break;
606
607 case IRP_MJ_DIRECTORY_CONTROL:
608
609 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
610 {
611 status = RD_STATUS_INVALID_HANDLE;
612 break;
613 }
614
615 switch (minor)
616 {
617 case IRP_MN_QUERY_DIRECTORY:
618
619 in_uint32_le(s, info_level);
620 in_uint8s(s, 1);
621 in_uint32_le(s, length);
622 in_uint8s(s, 0x17);
623 if (length && length < 2 * 255)
624 {
625 rdp_in_unistr(s, filename, sizeof(filename),
626 length);
627 convert_to_unix_filename(filename);
628 }
629 else
630 {
631 filename[0] = 0;
632 }
633 out.data = out.p = buffer;
634 out.size = sizeof(buffer);
635 status = disk_query_directory(file, info_level, filename,
636 &out);
637 result = buffer_len = out.p - out.data;
638 if (!buffer_len)
639 buffer_len++;
640 break;
641
642 case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
643
644 /* JIF
645 unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
646
647 in_uint32_le(s, info_level); /* notify mask */
648
649 status = disk_create_notify(file, info_level);
650 result = 0;
651
652 if (status == RD_STATUS_PENDING)
653 add_async_iorequest(device, file, id, major, length,
654 fns, 0, 0, NULL, 0);
655 break;
656
657 default:
658
659 status = RD_STATUS_INVALID_PARAMETER;
660 /* JIF */
661 unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
662 }
663 break;
664
665 case IRP_MJ_DEVICE_CONTROL:
666
667 if (!fns->device_control)
668 {
669 status = RD_STATUS_NOT_SUPPORTED;
670 break;
671 }
672
673 in_uint32_le(s, bytes_out);
674 in_uint32_le(s, bytes_in);
675 in_uint32_le(s, request);
676 in_uint8s(s, 0x14);
677
678 buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
679 if (!buffer)
680 {
681 status = RD_STATUS_CANCELLED;
682 break;
683 }
684
685 out.data = out.p = buffer;
686 out.size = sizeof(buffer);
687
688#ifdef WITH_SCARD
689 scardSetInfo(device, id, bytes_out + 0x14);
690#endif
691 status = fns->device_control(file, request, s, &out);
692 result = buffer_len = out.p - out.data;
693
694 /* Serial SERIAL_WAIT_ON_MASK */
695 if (status == RD_STATUS_PENDING)
696 {
697 if (add_async_iorequest
698 (device, file, id, major, length, fns, 0, 0, NULL, 0))
699 {
700 status = RD_STATUS_PENDING;
701 break;
702 }
703 }
704#ifdef WITH_SCARD
705 else if (status == (RD_STATUS_PENDING | 0xC0000000))
706 status = RD_STATUS_PENDING;
707#endif
708 break;
709
710
711 case IRP_MJ_LOCK_CONTROL:
712
713 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
714 {
715 status = RD_STATUS_INVALID_HANDLE;
716 break;
717 }
718
719 in_uint32_le(s, info_level);
720
721 out.data = out.p = buffer;
722 out.size = sizeof(buffer);
723 /* FIXME: Perhaps consider actually *do*
724 something here :-) */
725 status = RD_STATUS_SUCCESS;
726 result = buffer_len = out.p - out.data;
727 break;
728
729 default:
730 unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
731 break;
732 }
733
734 if (status != RD_STATUS_PENDING)
735 {
736 rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
737 }
738 if (buffer)
739 xfree(buffer);
740 buffer = NULL;
741}
742
743static void
744rdpdr_send_clientcapabilty(void)
745{
746 uint8 magic[4] = "rDPC";
747 STREAM s;
748
749 s = channel_init(rdpdr_channel, 0x50);
750 out_uint8a(s, magic, 4);
751 out_uint32_le(s, 5); /* count */
752 out_uint16_le(s, 1); /* first */
753 out_uint16_le(s, 0x28); /* length */
754 out_uint32_le(s, 1);
755 out_uint32_le(s, 2);
756 out_uint16_le(s, 2);
757 out_uint16_le(s, 5);
758 out_uint16_le(s, 1);
759 out_uint16_le(s, 5);
760 out_uint16_le(s, 0xFFFF);
761 out_uint16_le(s, 0);
762 out_uint32_le(s, 0);
763 out_uint32_le(s, 3);
764 out_uint32_le(s, 0);
765 out_uint32_le(s, 0);
766 out_uint16_le(s, 2); /* second */
767 out_uint16_le(s, 8); /* length */
768 out_uint32_le(s, 1);
769 out_uint16_le(s, 3); /* third */
770 out_uint16_le(s, 8); /* length */
771 out_uint32_le(s, 1);
772 out_uint16_le(s, 4); /* fourth */
773 out_uint16_le(s, 8); /* length */
774 out_uint32_le(s, 1);
775 out_uint16_le(s, 5); /* fifth */
776 out_uint16_le(s, 8); /* length */
777 out_uint32_le(s, 1);
778
779 s_mark_end(s);
780 channel_send(s, rdpdr_channel);
781}
782
783static void
784rdpdr_process(STREAM s)
785{
786 uint32 handle;
787 uint8 *magic;
788
789#if WITH_DEBUG_RDP5
790 printf("--- rdpdr_process ---\n");
791 hexdump(s->p, s->end - s->p);
792#endif
793 in_uint8p(s, magic, 4);
794
795 if ((magic[0] == 'r') && (magic[1] == 'D'))
796 {
797 if ((magic[2] == 'R') && (magic[3] == 'I'))
798 {
799 rdpdr_process_irp(s);
800 return;
801 }
802 if ((magic[2] == 'n') && (magic[3] == 'I'))
803 {
804 rdpdr_send_connect();
805 rdpdr_send_name();
806 return;
807 }
808 if ((magic[2] == 'C') && (magic[3] == 'C'))
809 {
810 /* connect from server */
811 rdpdr_send_clientcapabilty();
812 rdpdr_send_available();
813 return;
814 }
815 if ((magic[2] == 'r') && (magic[3] == 'd'))
816 {
817 /* connect to a specific resource */
818 in_uint32(s, handle);
819#if WITH_DEBUG_RDP5
820 DEBUG(("RDPDR: Server connected to resource %d\n", handle));
821#endif
822 return;
823 }
824 if ((magic[2] == 'P') && (magic[3] == 'S'))
825 {
826 /* server capability */
827 return;
828 }
829 }
830 if ((magic[0] == 'R') && (magic[1] == 'P'))
831 {
832 if ((magic[2] == 'C') && (magic[3] == 'P'))
833 {
834 printercache_process(s);
835 return;
836 }
837 }
838 unimpl("RDPDR packet type %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
839}
840
841RD_BOOL
842rdpdr_init()
843{
844 if (g_num_devices > 0)
845 {
846 rdpdr_channel =
847 channel_register("rdpdr",
848 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
849 rdpdr_process);
850 }
851
852 return (rdpdr_channel != NULL);
853}
854
855/* Add file descriptors of pending io request to select() */
856void
857rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, RD_BOOL * timeout)
858{
859 uint32 select_timeout = 0; /* Timeout value to be used for select() (in millisecons). */
860 struct async_iorequest *iorq;
861 char c;
862
863 iorq = g_iorequest;
864 while (iorq != NULL)
865 {
866 if (iorq->fd != 0)
867 {
868 switch (iorq->major)
869 {
870 case IRP_MJ_READ:
871 /* Is this FD valid? FDs will
872 be invalid when
873 reconnecting. FIXME: Real
874 support for reconnects. */
875
876 FD_SET(iorq->fd, rfds);
877 *n = MAX(*n, iorq->fd);
878
879 /* Check if io request timeout is smaller than current (but not 0). */
880 if (iorq->timeout
881 && (select_timeout == 0
882 || iorq->timeout < select_timeout))
883 {
884 /* Set new timeout */
885 select_timeout = iorq->timeout;
886 g_min_timeout_fd = iorq->fd; /* Remember fd */
887 tv->tv_sec = select_timeout / 1000;
888 tv->tv_usec = (select_timeout % 1000) * 1000;
889 *timeout = True;
890 break;
891 }
892 if (iorq->itv_timeout && iorq->partial_len > 0
893 && (select_timeout == 0
894 || iorq->itv_timeout < select_timeout))
895 {
896 /* Set new timeout */
897 select_timeout = iorq->itv_timeout;
898 g_min_timeout_fd = iorq->fd; /* Remember fd */
899 tv->tv_sec = select_timeout / 1000;
900 tv->tv_usec = (select_timeout % 1000) * 1000;
901 *timeout = True;
902 break;
903 }
904 break;
905
906 case IRP_MJ_WRITE:
907 /* FD still valid? See above. */
908 if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
909 break;
910
911 FD_SET(iorq->fd, wfds);
912 *n = MAX(*n, iorq->fd);
913 break;
914
915 case IRP_MJ_DEVICE_CONTROL:
916 if (select_timeout > 5)
917 select_timeout = 5; /* serial event queue */
918 break;
919
920 }
921
922 }
923
924 iorq = iorq->next;
925 }
926}
927
928struct async_iorequest *
929rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
930{
931 if (!iorq)
932 return NULL;
933
934 if (iorq->buffer)
935 xfree(iorq->buffer);
936 if (prev)
937 {
938 prev->next = iorq->next;
939 xfree(iorq);
940 iorq = prev->next;
941 }
942 else
943 {
944 /* Even if NULL */
945 g_iorequest = iorq->next;
946 xfree(iorq);
947 iorq = NULL;
948 }
949 return iorq;
950}
951
952/* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
953static void
954_rdpdr_check_fds(fd_set * rfds, fd_set * wfds, RD_BOOL timed_out)
955{
956 RD_NTSTATUS status;
957 uint32 result = 0;
958 DEVICE_FNS *fns;
959 struct async_iorequest *iorq;
960 struct async_iorequest *prev;
961 uint32 req_size = 0;
962 uint32 buffer_len;
963 struct stream out;
964 uint8 *buffer = NULL;
965
966
967 if (timed_out)
968 {
969 /* check serial iv_timeout */
970
971 iorq = g_iorequest;
972 prev = NULL;
973 while (iorq != NULL)
974 {
975 if (iorq->fd == g_min_timeout_fd)
976 {
977 if ((iorq->partial_len > 0) &&
978 (g_rdpdr_device[iorq->device].device_type ==
979 DEVICE_TYPE_SERIAL))
980 {
981
982 /* iv_timeout between 2 chars, send partial_len */
983 /*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
984 rdpdr_send_completion(iorq->device,
985 iorq->id, RD_STATUS_SUCCESS,
986 iorq->partial_len,
987 iorq->buffer, iorq->partial_len);
988 iorq = rdpdr_remove_iorequest(prev, iorq);
989 return;
990 }
991 else
992 {
993 break;
994 }
995
996 }
997 else
998 {
999 break;
1000 }
1001
1002
1003 prev = iorq;
1004 if (iorq)
1005 iorq = iorq->next;
1006
1007 }
1008
1009 rdpdr_abort_io(g_min_timeout_fd, 0, RD_STATUS_TIMEOUT);
1010 return;
1011 }
1012
1013 iorq = g_iorequest;
1014 prev = NULL;
1015 while (iorq != NULL)
1016 {
1017 if (iorq->fd != 0)
1018 {
1019 switch (iorq->major)
1020 {
1021 case IRP_MJ_READ:
1022 if (FD_ISSET(iorq->fd, rfds))
1023 {
1024 /* Read the data */
1025 fns = iorq->fns;
1026
1027 req_size =
1028 (iorq->length - iorq->partial_len) >
1029 8192 ? 8192 : (iorq->length -
1030 iorq->partial_len);
1031 /* never read larger chunks than 8k - chances are that it will block */
1032 status = fns->read(iorq->fd,
1033 iorq->buffer + iorq->partial_len,
1034 req_size, iorq->offset, &result);
1035
1036 if ((long) result > 0)
1037 {
1038 iorq->partial_len += result;
1039 iorq->offset += result;
1040 }
1041#if WITH_DEBUG_RDP5
1042 DEBUG(("RDPDR: %d bytes of data read\n", result));
1043#endif
1044 /* only delete link if all data has been transfered */
1045 /* or if result was 0 and status success - EOF */
1046 if ((iorq->partial_len == iorq->length) ||
1047 (result == 0))
1048 {
1049#if WITH_DEBUG_RDP5
1050 DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
1051#endif
1052 rdpdr_send_completion(iorq->device,
1053 iorq->id, status,
1054 iorq->partial_len,
1055 iorq->buffer,
1056 iorq->partial_len);
1057 iorq = rdpdr_remove_iorequest(prev, iorq);
1058 }
1059 }
1060 break;
1061 case IRP_MJ_WRITE:
1062 if (FD_ISSET(iorq->fd, wfds))
1063 {
1064 /* Write data. */
1065 fns = iorq->fns;
1066
1067 req_size =
1068 (iorq->length - iorq->partial_len) >
1069 8192 ? 8192 : (iorq->length -
1070 iorq->partial_len);
1071
1072 /* never write larger chunks than 8k - chances are that it will block */
1073 status = fns->write(iorq->fd,
1074 iorq->buffer +
1075 iorq->partial_len, req_size,
1076 iorq->offset, &result);
1077
1078 if ((long) result > 0)
1079 {
1080 iorq->partial_len += result;
1081 iorq->offset += result;
1082 }
1083
1084#if WITH_DEBUG_RDP5
1085 DEBUG(("RDPDR: %d bytes of data written\n",
1086 result));
1087#endif
1088 /* only delete link if all data has been transfered */
1089 /* or we couldn't write */
1090 if ((iorq->partial_len == iorq->length)
1091 || (result == 0))
1092 {
1093#if WITH_DEBUG_RDP5
1094 DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
1095#endif
1096 rdpdr_send_completion(iorq->device,
1097 iorq->id, status,
1098 iorq->partial_len,
1099 (uint8 *) "", 1);
1100
1101 iorq = rdpdr_remove_iorequest(prev, iorq);
1102 }
1103 }
1104 break;
1105 case IRP_MJ_DEVICE_CONTROL:
1106 if (serial_get_event(iorq->fd, &result))
1107 {
1108 buffer = (uint8 *) xrealloc((void *) buffer, 0x14);
1109 out.data = out.p = buffer;
1110 out.size = sizeof(buffer);
1111 out_uint32_le(&out, result);
1112 result = buffer_len = out.p - out.data;
1113 status = RD_STATUS_SUCCESS;
1114 rdpdr_send_completion(iorq->device, iorq->id,
1115 status, result, buffer,
1116 buffer_len);
1117 xfree(buffer);
1118 iorq = rdpdr_remove_iorequest(prev, iorq);
1119 }
1120
1121 break;
1122 }
1123
1124 }
1125 prev = iorq;
1126 if (iorq)
1127 iorq = iorq->next;
1128 }
1129
1130 /* Check notify */
1131 iorq = g_iorequest;
1132 prev = NULL;
1133 while (iorq != NULL)
1134 {
1135 if (iorq->fd != 0)
1136 {
1137 switch (iorq->major)
1138 {
1139
1140 case IRP_MJ_DIRECTORY_CONTROL:
1141 if (g_rdpdr_device[iorq->device].device_type ==
1142 DEVICE_TYPE_DISK)
1143 {
1144
1145 if (g_notify_stamp)
1146 {
1147 g_notify_stamp = False;
1148 status = disk_check_notify(iorq->fd);
1149 if (status != RD_STATUS_PENDING)
1150 {
1151 rdpdr_send_completion(iorq->device,
1152 iorq->id,
1153 status, 0,
1154 NULL, 0);
1155 iorq = rdpdr_remove_iorequest(prev,
1156 iorq);
1157 }
1158 }
1159 }
1160 break;
1161
1162
1163
1164 }
1165 }
1166
1167 prev = iorq;
1168 if (iorq)
1169 iorq = iorq->next;
1170 }
1171
1172}
1173
1174void
1175rdpdr_check_fds(fd_set * rfds, fd_set * wfds, RD_BOOL timed_out)
1176{
1177 fd_set dummy;
1178
1179
1180 FD_ZERO(&dummy);
1181
1182
1183 /* fist check event queue only,
1184 any serial wait event must be done before read block will be sent
1185 */
1186
1187 _rdpdr_check_fds(&dummy, &dummy, False);
1188 _rdpdr_check_fds(rfds, wfds, timed_out);
1189}
1190
1191
1192/* Abort a pending io request for a given handle and major */
1193RD_BOOL
1194rdpdr_abort_io(uint32 fd, uint32 major, RD_NTSTATUS status)
1195{
1196 uint32 result;
1197 struct async_iorequest *iorq;
1198 struct async_iorequest *prev;
1199
1200 iorq = g_iorequest;
1201 prev = NULL;
1202 while (iorq != NULL)
1203 {
1204 /* Only remove from table when major is not set, or when correct major is supplied.
1205 Abort read should not abort a write io request. */
1206 if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1207 {
1208 result = 0;
1209 rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
1210 1);
1211
1212 iorq = rdpdr_remove_iorequest(prev, iorq);
1213 return True;
1214 }
1215
1216 prev = iorq;
1217 iorq = iorq->next;
1218 }
1219
1220 return False;
1221}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette