VirtualBox

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

最後變更 在這個檔案從37224是 37224,由 vboxsync 提交於 14 年 前

RDP/client: fix OSE

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

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