VirtualBox

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

Last change on this file since 35532 was 33656, checked in by vboxsync, 14 years ago

*: rebrand Sun (L)GPL disclaimers

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