VirtualBox

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

最後變更 在這個檔案從48955是 38904,由 vboxsync 提交於 13 年 前

some small memory leaks in error cases and additional NULL checks

  • 屬性 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 xfree(buffer);
426 return;
427 }
428
429 switch (major)
430 {
431 case IRP_MJ_CREATE:
432
433 in_uint32_be(s, desired_access);
434 in_uint8s(s, 0x08); /* unknown */
435 in_uint32_le(s, error_mode);
436 in_uint32_le(s, share_mode);
437 in_uint32_le(s, disposition);
438 in_uint32_le(s, flags_and_attributes);
439 in_uint32_le(s, length);
440
441 if (length && (length / 2) < 256)
442 {
443 rdp_in_unistr(s, filename, sizeof(filename), length);
444 convert_to_unix_filename(filename);
445 }
446 else
447 {
448 filename[0] = 0;
449 }
450
451 if (!fns->create)
452 {
453 status = RD_STATUS_NOT_SUPPORTED;
454 break;
455 }
456
457 status = fns->create(device, desired_access, share_mode, disposition,
458 flags_and_attributes, filename, &result);
459 buffer_len = 1;
460 break;
461
462 case IRP_MJ_CLOSE:
463 if (!fns->close)
464 {
465 status = RD_STATUS_NOT_SUPPORTED;
466 break;
467 }
468
469 status = fns->close(file);
470 break;
471
472 case IRP_MJ_READ:
473
474 if (!fns->read)
475 {
476 status = RD_STATUS_NOT_SUPPORTED;
477 break;
478 }
479
480 in_uint32_le(s, length);
481 in_uint32_le(s, offset);
482#if WITH_DEBUG_RDP5
483 DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
484#endif
485 if (!rdpdr_handle_ok(device, file))
486 {
487 status = RD_STATUS_INVALID_HANDLE;
488 break;
489 }
490
491 if (rw_blocking) /* Complete read immediately */
492 {
493 buffer = (uint8 *) xrealloc((void *) buffer, length);
494 if (!buffer)
495 {
496 status = RD_STATUS_CANCELLED;
497 break;
498 }
499 status = fns->read(file, buffer, length, offset, &result);
500 buffer_len = result;
501 break;
502 }
503
504 /* Add request to table */
505 pst_buf = (uint8 *) xmalloc(length);
506 if (!pst_buf)
507 {
508 status = RD_STATUS_CANCELLED;
509 break;
510 }
511 serial_get_timeout(file, length, &total_timeout, &interval_timeout);
512 if (add_async_iorequest
513 (device, file, id, major, length, fns, total_timeout, interval_timeout,
514 pst_buf, offset))
515 {
516 status = RD_STATUS_PENDING;
517 break;
518 }
519
520 status = RD_STATUS_CANCELLED;
521 break;
522 case IRP_MJ_WRITE:
523
524 buffer_len = 1;
525
526 if (!fns->write)
527 {
528 status = RD_STATUS_NOT_SUPPORTED;
529 break;
530 }
531
532 in_uint32_le(s, length);
533 in_uint32_le(s, offset);
534 in_uint8s(s, 0x18);
535#if WITH_DEBUG_RDP5
536 DEBUG(("RDPDR IRP Write (length: %d)\n", result));
537#endif
538 if (!rdpdr_handle_ok(device, file))
539 {
540 status = RD_STATUS_INVALID_HANDLE;
541 break;
542 }
543
544 if (rw_blocking) /* Complete immediately */
545 {
546 status = fns->write(file, s->p, length, offset, &result);
547 break;
548 }
549
550 /* Add to table */
551 pst_buf = (uint8 *) xmalloc(length);
552 if (!pst_buf)
553 {
554 status = RD_STATUS_CANCELLED;
555 break;
556 }
557
558 in_uint8a(s, pst_buf, length);
559
560 if (add_async_iorequest
561 (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
562 {
563 status = RD_STATUS_PENDING;
564 break;
565 }
566
567 status = RD_STATUS_CANCELLED;
568 break;
569
570 case IRP_MJ_QUERY_INFORMATION:
571
572 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
573 {
574 status = RD_STATUS_INVALID_HANDLE;
575 break;
576 }
577 in_uint32_le(s, info_level);
578
579 out.data = out.p = buffer;
580 out.size = sizeof(buffer);
581 status = disk_query_information(file, info_level, &out);
582 result = buffer_len = out.p - out.data;
583
584 break;
585
586 case IRP_MJ_SET_INFORMATION:
587
588 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
589 {
590 status = RD_STATUS_INVALID_HANDLE;
591 break;
592 }
593
594 in_uint32_le(s, info_level);
595
596 out.data = out.p = buffer;
597 out.size = sizeof(buffer);
598 status = disk_set_information(file, info_level, s, &out);
599 result = buffer_len = out.p - out.data;
600 break;
601
602 case IRP_MJ_QUERY_VOLUME_INFORMATION:
603
604 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
605 {
606 status = RD_STATUS_INVALID_HANDLE;
607 break;
608 }
609
610 in_uint32_le(s, info_level);
611
612 out.data = out.p = buffer;
613 out.size = sizeof(buffer);
614 status = disk_query_volume_information(file, info_level, &out);
615 result = buffer_len = out.p - out.data;
616 break;
617
618 case IRP_MJ_DIRECTORY_CONTROL:
619
620 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
621 {
622 status = RD_STATUS_INVALID_HANDLE;
623 break;
624 }
625
626 switch (minor)
627 {
628 case IRP_MN_QUERY_DIRECTORY:
629
630 in_uint32_le(s, info_level);
631 in_uint8s(s, 1);
632 in_uint32_le(s, length);
633 in_uint8s(s, 0x17);
634 if (length && length < 2 * 255)
635 {
636 rdp_in_unistr(s, filename, sizeof(filename),
637 length);
638 convert_to_unix_filename(filename);
639 }
640 else
641 {
642 filename[0] = 0;
643 }
644 out.data = out.p = buffer;
645 out.size = sizeof(buffer);
646 status = disk_query_directory(file, info_level, filename,
647 &out);
648 result = buffer_len = out.p - out.data;
649 if (!buffer_len)
650 buffer_len++;
651 break;
652
653 case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
654
655 /* JIF
656 unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
657
658 in_uint32_le(s, info_level); /* notify mask */
659
660 status = disk_create_notify(file, info_level);
661 result = 0;
662
663 if (status == RD_STATUS_PENDING)
664 add_async_iorequest(device, file, id, major, length,
665 fns, 0, 0, NULL, 0);
666 break;
667
668 default:
669
670 status = RD_STATUS_INVALID_PARAMETER;
671 /* JIF */
672 unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
673 }
674 break;
675
676 case IRP_MJ_DEVICE_CONTROL:
677
678 if (!fns->device_control)
679 {
680 status = RD_STATUS_NOT_SUPPORTED;
681 break;
682 }
683
684 in_uint32_le(s, bytes_out);
685 in_uint32_le(s, bytes_in);
686 in_uint32_le(s, request);
687 in_uint8s(s, 0x14);
688
689 buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
690 if (!buffer)
691 {
692 status = RD_STATUS_CANCELLED;
693 break;
694 }
695
696 out.data = out.p = buffer;
697 out.size = sizeof(buffer);
698
699#ifdef WITH_SCARD
700 scardSetInfo(device, id, bytes_out + 0x14);
701#endif
702 status = fns->device_control(file, request, s, &out);
703 result = buffer_len = out.p - out.data;
704
705 /* Serial SERIAL_WAIT_ON_MASK */
706 if (status == RD_STATUS_PENDING)
707 {
708 if (add_async_iorequest
709 (device, file, id, major, length, fns, 0, 0, NULL, 0))
710 {
711 status = RD_STATUS_PENDING;
712 break;
713 }
714 }
715#ifdef WITH_SCARD
716 else if (status == (RD_STATUS_PENDING | 0xC0000000))
717 status = RD_STATUS_PENDING;
718#endif
719 break;
720
721
722 case IRP_MJ_LOCK_CONTROL:
723
724 if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
725 {
726 status = RD_STATUS_INVALID_HANDLE;
727 break;
728 }
729
730 in_uint32_le(s, info_level);
731
732 out.data = out.p = buffer;
733 out.size = sizeof(buffer);
734 /* FIXME: Perhaps consider actually *do*
735 something here :-) */
736 status = RD_STATUS_SUCCESS;
737 result = buffer_len = out.p - out.data;
738 break;
739
740 default:
741 unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
742 break;
743 }
744
745 if (status != RD_STATUS_PENDING)
746 {
747 rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
748 }
749 if (buffer)
750 xfree(buffer);
751 buffer = NULL;
752}
753
754static void
755rdpdr_send_clientcapability(void)
756{
757 uint8 magic[4] = "rDPC";
758 STREAM s;
759
760 s = channel_init(rdpdr_channel, 0x50);
761 out_uint8a(s, magic, 4);
762 out_uint32_le(s, 5); /* count */
763 out_uint16_le(s, 1); /* first */
764 out_uint16_le(s, 0x28); /* length */
765 out_uint32_le(s, 1);
766 out_uint32_le(s, 2);
767 out_uint16_le(s, 2);
768 out_uint16_le(s, 5);
769 out_uint16_le(s, 1);
770 out_uint16_le(s, 5);
771 out_uint16_le(s, 0xFFFF);
772 out_uint16_le(s, 0);
773 out_uint32_le(s, 0);
774 out_uint32_le(s, 3);
775 out_uint32_le(s, 0);
776 out_uint32_le(s, 0);
777 out_uint16_le(s, 2); /* second */
778 out_uint16_le(s, 8); /* length */
779 out_uint32_le(s, 1);
780 out_uint16_le(s, 3); /* third */
781 out_uint16_le(s, 8); /* length */
782 out_uint32_le(s, 1);
783 out_uint16_le(s, 4); /* fourth */
784 out_uint16_le(s, 8); /* length */
785 out_uint32_le(s, 1);
786 out_uint16_le(s, 5); /* fifth */
787 out_uint16_le(s, 8); /* length */
788 out_uint32_le(s, 1);
789
790 s_mark_end(s);
791 channel_send(s, rdpdr_channel);
792}
793
794static void
795rdpdr_process(STREAM s)
796{
797 uint32 handle;
798 uint8 *magic;
799
800#if WITH_DEBUG_RDP5
801 printf("--- rdpdr_process ---\n");
802 hexdump(s->p, s->end - s->p);
803#endif
804 in_uint8p(s, magic, 4);
805
806 if ((magic[0] == 'r') && (magic[1] == 'D'))
807 {
808 if ((magic[2] == 'R') && (magic[3] == 'I'))
809 {
810 rdpdr_process_irp(s);
811 return;
812 }
813 if ((magic[2] == 'n') && (magic[3] == 'I'))
814 {
815 rdpdr_send_connect();
816 rdpdr_send_name();
817 return;
818 }
819 if ((magic[2] == 'C') && (magic[3] == 'C'))
820 {
821 /* connect from server */
822 rdpdr_send_clientcapability();
823 rdpdr_send_available();
824 return;
825 }
826 if ((magic[2] == 'r') && (magic[3] == 'd'))
827 {
828 /* connect to a specific resource */
829 in_uint32(s, handle);
830#if WITH_DEBUG_RDP5
831 DEBUG(("RDPDR: Server connected to resource %d\n", handle));
832#endif
833 return;
834 }
835 if ((magic[2] == 'P') && (magic[3] == 'S'))
836 {
837 /* server capability */
838 return;
839 }
840 }
841 if ((magic[0] == 'R') && (magic[1] == 'P'))
842 {
843 if ((magic[2] == 'C') && (magic[3] == 'P'))
844 {
845 printercache_process(s);
846 return;
847 }
848 }
849 unimpl("RDPDR packet type %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
850}
851
852RD_BOOL
853rdpdr_init()
854{
855 rdpdr_channel =
856 channel_register("rdpdr",
857 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
858 rdpdr_process);
859
860 return (rdpdr_channel != NULL);
861}
862
863/* Add file descriptors of pending io request to select() */
864void
865rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, RD_BOOL * timeout)
866{
867 uint32 select_timeout = 0; /* Timeout value to be used for select() (in millisecons). */
868 struct async_iorequest *iorq;
869 char c;
870
871 iorq = g_iorequest;
872 while (iorq != NULL)
873 {
874 if (iorq->fd != 0)
875 {
876 switch (iorq->major)
877 {
878 case IRP_MJ_READ:
879 /* Is this FD valid? FDs will
880 be invalid when
881 reconnecting. FIXME: Real
882 support for reconnects. */
883
884 FD_SET(iorq->fd, rfds);
885 *n = MAX(*n, iorq->fd);
886
887 /* Check if io request timeout is smaller than current (but not 0). */
888 if (iorq->timeout
889 && (select_timeout == 0
890 || iorq->timeout < select_timeout))
891 {
892 /* Set new timeout */
893 select_timeout = iorq->timeout;
894 g_min_timeout_fd = iorq->fd; /* Remember fd */
895 tv->tv_sec = select_timeout / 1000;
896 tv->tv_usec = (select_timeout % 1000) * 1000;
897 *timeout = True;
898 break;
899 }
900 if (iorq->itv_timeout && iorq->partial_len > 0
901 && (select_timeout == 0
902 || iorq->itv_timeout < select_timeout))
903 {
904 /* Set new timeout */
905 select_timeout = iorq->itv_timeout;
906 g_min_timeout_fd = iorq->fd; /* Remember fd */
907 tv->tv_sec = select_timeout / 1000;
908 tv->tv_usec = (select_timeout % 1000) * 1000;
909 *timeout = True;
910 break;
911 }
912 break;
913
914 case IRP_MJ_WRITE:
915 /* FD still valid? See above. */
916 if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
917 break;
918
919 FD_SET(iorq->fd, wfds);
920 *n = MAX(*n, iorq->fd);
921 break;
922
923 case IRP_MJ_DEVICE_CONTROL:
924 if (select_timeout > 5)
925 select_timeout = 5; /* serial event queue */
926 break;
927
928 }
929
930 }
931
932 iorq = iorq->next;
933 }
934}
935
936struct async_iorequest *
937rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
938{
939 if (!iorq)
940 return NULL;
941
942 if (iorq->buffer)
943 xfree(iorq->buffer);
944 if (prev)
945 {
946 prev->next = iorq->next;
947 xfree(iorq);
948 iorq = prev->next;
949 }
950 else
951 {
952 /* Even if NULL */
953 g_iorequest = iorq->next;
954 xfree(iorq);
955 iorq = NULL;
956 }
957 return iorq;
958}
959
960/* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
961static void
962_rdpdr_check_fds(fd_set * rfds, fd_set * wfds, RD_BOOL timed_out)
963{
964 RD_NTSTATUS status;
965 uint32 result = 0;
966 DEVICE_FNS *fns;
967 struct async_iorequest *iorq;
968 struct async_iorequest *prev;
969 uint32 req_size = 0;
970 uint32 buffer_len;
971 struct stream out;
972 uint8 *buffer = NULL;
973
974
975 if (timed_out)
976 {
977 /* check serial iv_timeout */
978
979 iorq = g_iorequest;
980 prev = NULL;
981 while (iorq != NULL)
982 {
983 if (iorq->fd == g_min_timeout_fd)
984 {
985 if ((iorq->partial_len > 0) &&
986 (g_rdpdr_device[iorq->device].device_type ==
987 DEVICE_TYPE_SERIAL))
988 {
989
990 /* iv_timeout between 2 chars, send partial_len */
991 /*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
992 rdpdr_send_completion(iorq->device,
993 iorq->id, RD_STATUS_SUCCESS,
994 iorq->partial_len,
995 iorq->buffer, iorq->partial_len);
996 iorq = rdpdr_remove_iorequest(prev, iorq);
997 return;
998 }
999 else
1000 {
1001 break;
1002 }
1003
1004 }
1005 else
1006 {
1007 break;
1008 }
1009
1010
1011 prev = iorq;
1012 if (iorq)
1013 iorq = iorq->next;
1014
1015 }
1016
1017 rdpdr_abort_io(g_min_timeout_fd, 0, RD_STATUS_TIMEOUT);
1018 return;
1019 }
1020
1021 iorq = g_iorequest;
1022 prev = NULL;
1023 while (iorq != NULL)
1024 {
1025 if (iorq->fd != 0)
1026 {
1027 switch (iorq->major)
1028 {
1029 case IRP_MJ_READ:
1030 if (FD_ISSET(iorq->fd, rfds))
1031 {
1032 /* Read the data */
1033 fns = iorq->fns;
1034
1035 req_size =
1036 (iorq->length - iorq->partial_len) >
1037 8192 ? 8192 : (iorq->length -
1038 iorq->partial_len);
1039 /* never read larger chunks than 8k - chances are that it will block */
1040 status = fns->read(iorq->fd,
1041 iorq->buffer + iorq->partial_len,
1042 req_size, iorq->offset, &result);
1043
1044 if ((long) result > 0)
1045 {
1046 iorq->partial_len += result;
1047 iorq->offset += result;
1048 }
1049#if WITH_DEBUG_RDP5
1050 DEBUG(("RDPDR: %d bytes of data read\n", result));
1051#endif
1052 /* only delete link if all data has been transfered */
1053 /* or if result was 0 and status success - EOF */
1054 if ((iorq->partial_len == iorq->length) ||
1055 (result == 0))
1056 {
1057#if WITH_DEBUG_RDP5
1058 DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
1059#endif
1060 rdpdr_send_completion(iorq->device,
1061 iorq->id, status,
1062 iorq->partial_len,
1063 iorq->buffer,
1064 iorq->partial_len);
1065 iorq = rdpdr_remove_iorequest(prev, iorq);
1066 }
1067 }
1068 break;
1069 case IRP_MJ_WRITE:
1070 if (FD_ISSET(iorq->fd, wfds))
1071 {
1072 /* Write data. */
1073 fns = iorq->fns;
1074
1075 req_size =
1076 (iorq->length - iorq->partial_len) >
1077 8192 ? 8192 : (iorq->length -
1078 iorq->partial_len);
1079
1080 /* never write larger chunks than 8k - chances are that it will block */
1081 status = fns->write(iorq->fd,
1082 iorq->buffer +
1083 iorq->partial_len, req_size,
1084 iorq->offset, &result);
1085
1086 if ((long) result > 0)
1087 {
1088 iorq->partial_len += result;
1089 iorq->offset += result;
1090 }
1091
1092#if WITH_DEBUG_RDP5
1093 DEBUG(("RDPDR: %d bytes of data written\n",
1094 result));
1095#endif
1096 /* only delete link if all data has been transfered */
1097 /* or we couldn't write */
1098 if ((iorq->partial_len == iorq->length)
1099 || (result == 0))
1100 {
1101#if WITH_DEBUG_RDP5
1102 DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
1103#endif
1104 rdpdr_send_completion(iorq->device,
1105 iorq->id, status,
1106 iorq->partial_len,
1107 (uint8 *) "", 1);
1108
1109 iorq = rdpdr_remove_iorequest(prev, iorq);
1110 }
1111 }
1112 break;
1113 case IRP_MJ_DEVICE_CONTROL:
1114 if (serial_get_event(iorq->fd, &result))
1115 {
1116 buffer = (uint8 *) xrealloc((void *) buffer, 0x14);
1117 out.data = out.p = buffer;
1118 out.size = sizeof(buffer);
1119 out_uint32_le(&out, result);
1120 result = buffer_len = out.p - out.data;
1121 status = RD_STATUS_SUCCESS;
1122 rdpdr_send_completion(iorq->device, iorq->id,
1123 status, result, buffer,
1124 buffer_len);
1125 xfree(buffer);
1126 iorq = rdpdr_remove_iorequest(prev, iorq);
1127 }
1128
1129 break;
1130 }
1131
1132 }
1133 prev = iorq;
1134 if (iorq)
1135 iorq = iorq->next;
1136 }
1137
1138 /* Check notify */
1139 iorq = g_iorequest;
1140 prev = NULL;
1141 while (iorq != NULL)
1142 {
1143 if (iorq->fd != 0)
1144 {
1145 switch (iorq->major)
1146 {
1147
1148 case IRP_MJ_DIRECTORY_CONTROL:
1149 if (g_rdpdr_device[iorq->device].device_type ==
1150 DEVICE_TYPE_DISK)
1151 {
1152
1153 if (g_notify_stamp)
1154 {
1155 g_notify_stamp = False;
1156 status = disk_check_notify(iorq->fd);
1157 if (status != RD_STATUS_PENDING)
1158 {
1159 rdpdr_send_completion(iorq->device,
1160 iorq->id,
1161 status, 0,
1162 NULL, 0);
1163 iorq = rdpdr_remove_iorequest(prev,
1164 iorq);
1165 }
1166 }
1167 }
1168 break;
1169
1170
1171
1172 }
1173 }
1174
1175 prev = iorq;
1176 if (iorq)
1177 iorq = iorq->next;
1178 }
1179
1180}
1181
1182void
1183rdpdr_check_fds(fd_set * rfds, fd_set * wfds, RD_BOOL timed_out)
1184{
1185 fd_set dummy;
1186
1187
1188 FD_ZERO(&dummy);
1189
1190
1191 /* fist check event queue only,
1192 any serial wait event must be done before read block will be sent
1193 */
1194
1195 _rdpdr_check_fds(&dummy, &dummy, False);
1196 _rdpdr_check_fds(rfds, wfds, timed_out);
1197}
1198
1199
1200/* Abort a pending io request for a given handle and major */
1201RD_BOOL
1202rdpdr_abort_io(uint32 fd, uint32 major, RD_NTSTATUS status)
1203{
1204 uint32 result;
1205 struct async_iorequest *iorq;
1206 struct async_iorequest *prev;
1207
1208 iorq = g_iorequest;
1209 prev = NULL;
1210 while (iorq != NULL)
1211 {
1212 /* Only remove from table when major is not set, or when correct major is supplied.
1213 Abort read should not abort a write io request. */
1214 if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1215 {
1216 result = 0;
1217 rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
1218 1);
1219
1220 iorq = rdpdr_remove_iorequest(prev, iorq);
1221 return True;
1222 }
1223
1224 prev = iorq;
1225 iorq = iorq->next;
1226 }
1227
1228 return False;
1229}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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