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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1364 - (hide annotations)
Thu Jan 4 04:55:56 2007 UTC (17 years, 5 months ago) by jsorg71
File MIME type: text/plain
File size: 27572 byte(s)
RD_ prefix and win32 compatibility for core files

1 astrand 657 /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.
3 stargo 828 Copyright (C) Matthew Chapman 1999-2005
4 astrand 657
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 stargo 570 #include <unistd.h>
37 stargo 576 #include <sys/types.h>
38 stargo 609 #include <sys/time.h>
39 n-ki 627 #include <dirent.h> /* opendir, closedir, readdir */
40 stargo 576 #include <time.h>
41 astrand 674 #include <errno.h>
42 matthewc 432 #include "rdesktop.h"
43    
44 n-ki 569 #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 astrand 650 #define IRP_MJ_LOCK_CONTROL 0x11
54 n-ki 569
55     #define IRP_MN_QUERY_DIRECTORY 0x01
56     #define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02
57    
58 jsorg71 710 extern char g_hostname[16];
59 matthewc 432 extern DEVICE_FNS serial_fns;
60     extern DEVICE_FNS printer_fns;
61 n-ki 569 extern DEVICE_FNS parallel_fns;
62     extern DEVICE_FNS disk_fns;
63 stargo 1309 #ifdef WITH_SCARD
64     extern DEVICE_FNS scard_fns;
65     #endif
66 n-ki 627 extern FILEINFO g_fileinfo[];
67 stargo 795 extern BOOL g_notify_stamp;
68 astrand 673
69 matthewc 432 static VCHANNEL *rdpdr_channel;
70    
71 n-ki 569 /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
72 jsorg71 1364 RD_NTHANDLE g_min_timeout_fd;
73 n-ki 569 uint32 g_num_devices;
74    
75     /* Table with information about rdpdr devices */
76     RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
77 astrand 651 char *g_rdpdr_clientname = NULL;
78 n-ki 569
79     /* Used to store incoming io request, until they are ready to be completed */
80 n-ki 592 /* using a linked list ensures that they are processed in the right order, */
81     /* if multiple ios are being done on the same fd */
82 n-ki 569 struct async_iorequest
83     {
84 n-ki 592 uint32 fd, major, minor, offset, device, id, length, partial_len;
85 n-ki 569 long timeout, /* Total timeout */
86     itv_timeout; /* Interval timeout (between serial characters) */
87     uint8 *buffer;
88     DEVICE_FNS *fns;
89    
90 n-ki 592 struct async_iorequest *next; /* next element in list */
91 n-ki 593 };
92 n-ki 592
93 n-ki 593 struct async_iorequest *g_iorequest;
94    
95 n-ki 569 /* Return device_id for a given handle */
96     int
97 jsorg71 1364 get_device_index(RD_NTHANDLE handle)
98 n-ki 569 {
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 */
109 matthewc 432 void
110 n-ki 569 convert_to_unix_filename(char *filename)
111     {
112     char *p;
113    
114     while ((p = strchr(filename, '\\')))
115     {
116     *p = '/';
117     }
118     }
119    
120 astrand 663 static BOOL
121 n-ki 627 rdpdr_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 n-ki 569 /* Add a new io request to the table containing pending io requests so it won't block rdesktop */
141 astrand 663 static BOOL
142 n-ki 569 add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,
143 n-ki 613 DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
144     uint32 offset)
145 n-ki 569 {
146     struct async_iorequest *iorq;
147    
148 n-ki 593 if (g_iorequest == NULL)
149     {
150     g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
151 n-ki 612 if (!g_iorequest)
152     return False;
153 n-ki 593 g_iorequest->fd = 0;
154     g_iorequest->next = NULL;
155     }
156    
157     iorq = g_iorequest;
158    
159 n-ki 592 while (iorq->fd != 0)
160 n-ki 569 {
161 stargo 865 /* create new element if needed */
162 n-ki 592 if (iorq->next == NULL)
163 n-ki 569 {
164 n-ki 592 iorq->next =
165     (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
166 n-ki 612 if (!iorq->next)
167     return False;
168 n-ki 593 iorq->next->fd = 0;
169     iorq->next->next = NULL;
170 n-ki 592 }
171 n-ki 593 iorq = iorq->next;
172 n-ki 569 }
173 n-ki 592 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 n-ki 613 iorq->offset = offset;
184 n-ki 592 return True;
185 n-ki 569 }
186    
187 astrand 663 static void
188 matthewc 432 rdpdr_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 astrand 435 out_uint16_le(s, 1); /* unknown */
196 matthewc 432 out_uint16_le(s, 5);
197 astrand 435 out_uint32_be(s, 0x815ed39d); /* IP address (use 127.0.0.1) 0x815ed39d */
198 matthewc 432 s_mark_end(s);
199     channel_send(s, rdpdr_channel);
200     }
201    
202 n-ki 569
203 astrand 663 static void
204 matthewc 432 rdpdr_send_name(void)
205     {
206     uint8 magic[4] = "rDNC";
207 astrand 647 STREAM s;
208     uint32 hostlen;
209    
210 astrand 651 if (NULL == g_rdpdr_clientname)
211     {
212 jsorg71 710 g_rdpdr_clientname = g_hostname;
213 forsberg 646 }
214 astrand 647 hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
215 matthewc 432
216 astrand 435 s = channel_init(rdpdr_channel, 16 + hostlen);
217 matthewc 432 out_uint8a(s, magic, 4);
218 astrand 435 out_uint16_le(s, 0x63); /* unknown */
219 matthewc 432 out_uint16_le(s, 0x72);
220     out_uint32(s, 0);
221     out_uint32_le(s, hostlen);
222 forsberg 646 rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
223 matthewc 432 s_mark_end(s);
224     channel_send(s, rdpdr_channel);
225     }
226    
227 n-ki 569 /* Returns the size of the payload of the announce packet */
228 astrand 663 static int
229 n-ki 569 announcedata_size()
230     {
231     int size, i;
232     PRINTER *printerinfo;
233    
234 stargo 865 size = 8; /* static announce size */
235 n-ki 569 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    
255 astrand 663 static void
256 matthewc 432 rdpdr_send_available(void)
257     {
258 n-ki 569
259 matthewc 432 uint8 magic[4] = "rDAD";
260 n-ki 569 uint32 driverlen, printerlen, bloblen;
261     int i;
262 matthewc 432 STREAM s;
263 n-ki 569 PRINTER *printerinfo;
264 matthewc 432
265 n-ki 569 s = channel_init(rdpdr_channel, announcedata_size());
266 matthewc 432 out_uint8a(s, magic, 4);
267 n-ki 569 out_uint32_le(s, g_num_devices);
268 matthewc 432
269 n-ki 569 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 astrand 747 /* Is it possible to use share names longer than 8 chars?
274     /astrand */
275 n-ki 569 out_uint8p(s, g_rdpdr_device[i].name, 8);
276    
277 n-ki 585 switch (g_rdpdr_device[i].device_type)
278 n-ki 569 {
279 n-ki 585 case DEVICE_TYPE_PRINTER:
280     printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
281 n-ki 569
282 n-ki 585 driverlen = 2 * strlen(printerinfo->driver) + 2;
283     printerlen = 2 * strlen(printerinfo->printer) + 2;
284     bloblen = printerinfo->bloblen;
285 n-ki 569
286 n-ki 585 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 n-ki 569
296 n-ki 598 if (printerinfo->blob)
297     xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */
298 n-ki 585 break;
299     default:
300     out_uint32(s, 0);
301 n-ki 569 }
302     }
303 matthewc 432 #if 0
304 astrand 435 out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */
305 matthewc 432 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    
315 stargo 1309 void
316 astrand 435 rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,
317     uint32 length)
318 matthewc 432 {
319     uint8 magic[4] = "rDCI";
320     STREAM s;
321    
322 stargo 1328 #ifdef WITH_SCARD
323     scard_lock(SCARD_LOCK_RDPDR);
324     #endif
325 matthewc 432 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 stargo 795 /* JIF */
334     #ifdef WITH_DEBUG_RDP5
335     printf("--> rdpdr_send_completion\n");
336 stargo 865 /* hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
337 stargo 795 #endif
338 matthewc 432 channel_send(s, rdpdr_channel);
339 stargo 1328 #ifdef WITH_SCARD
340     scard_unlock(SCARD_LOCK_RDPDR);
341     #endif
342 matthewc 432 }
343    
344     static void
345     rdpdr_process_irp(STREAM s)
346     {
347 n-ki 569 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 stargo 978 char filename[PATH_MAX];
365 n-ki 569 uint8 *buffer, *pst_buf;
366 matthewc 432 struct stream out;
367     DEVICE_FNS *fns;
368 n-ki 569 BOOL rw_blocking = True;
369 jsorg71 1364 RD_NTSTATUS status = RD_STATUS_INVALID_DEVICE_REQUEST;
370 matthewc 432
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 n-ki 569 buffer_len = 0;
378     buffer = (uint8 *) xmalloc(1024);
379     buffer[0] = 0;
380 matthewc 432
381 n-ki 569 switch (g_rdpdr_device[device].device_type)
382 matthewc 432 {
383 n-ki 569 case DEVICE_TYPE_SERIAL:
384    
385 matthewc 432 fns = &serial_fns;
386 n-ki 592 rw_blocking = False;
387 n-ki 569 break;
388    
389     case DEVICE_TYPE_PARALLEL:
390    
391     fns = &parallel_fns;
392 n-ki 592 rw_blocking = False;
393 n-ki 569 break;
394    
395     case DEVICE_TYPE_PRINTER:
396    
397 matthewc 432 fns = &printer_fns;
398 n-ki 569 break;
399    
400     case DEVICE_TYPE_DISK:
401    
402 n-ki 593 fns = &disk_fns;
403 n-ki 595 rw_blocking = False;
404 n-ki 569 break;
405    
406     case DEVICE_TYPE_SCARD:
407 stargo 1309 #ifdef WITH_SCARD
408     fns = &scard_fns;
409     rw_blocking = False;
410     break;
411     #endif
412 matthewc 432 default:
413 n-ki 569
414 matthewc 432 error("IRP for bad device %ld\n", device);
415     return;
416     }
417    
418     switch (major)
419     {
420     case IRP_MJ_CREATE:
421 n-ki 569
422     in_uint32_be(s, desired_access);
423 stargo 865 in_uint8s(s, 0x08); /* unknown */
424 n-ki 569 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, length);
433     convert_to_unix_filename(filename);
434     }
435     else
436     {
437     filename[0] = 0;
438     }
439    
440     if (!fns->create)
441     {
442 jsorg71 1364 status = RD_STATUS_NOT_SUPPORTED;
443 n-ki 569 break;
444     }
445    
446     status = fns->create(device, desired_access, share_mode, disposition,
447     flags_and_attributes, filename, &result);
448     buffer_len = 1;
449 matthewc 432 break;
450    
451     case IRP_MJ_CLOSE:
452 n-ki 569 if (!fns->close)
453     {
454 jsorg71 1364 status = RD_STATUS_NOT_SUPPORTED;
455 n-ki 569 break;
456     }
457    
458     status = fns->close(file);
459 matthewc 432 break;
460    
461     case IRP_MJ_READ:
462 n-ki 569
463     if (!fns->read)
464 matthewc 432 {
465 jsorg71 1364 status = RD_STATUS_NOT_SUPPORTED;
466 n-ki 569 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 n-ki 627 if (!rdpdr_handle_ok(device, file))
475     {
476 jsorg71 1364 status = RD_STATUS_INVALID_HANDLE;
477 n-ki 627 break;
478     }
479    
480 stargo 865 if (rw_blocking) /* Complete read immediately */
481 n-ki 592 {
482 n-ki 569 buffer = (uint8 *) xrealloc((void *) buffer, length);
483 n-ki 612 if (!buffer)
484     {
485 jsorg71 1364 status = RD_STATUS_CANCELLED;
486 n-ki 612 break;
487     }
488 n-ki 569 status = fns->read(file, buffer, length, offset, &result);
489 matthewc 432 buffer_len = result;
490 n-ki 569 break;
491 n-ki 592 }
492 n-ki 569
493 stargo 865 /* Add request to table */
494 n-ki 569 pst_buf = (uint8 *) xmalloc(length);
495 n-ki 612 if (!pst_buf)
496     {
497 jsorg71 1364 status = RD_STATUS_CANCELLED;
498 n-ki 612 break;
499     }
500 n-ki 569 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 n-ki 613 pst_buf, offset))
504 n-ki 569 {
505 jsorg71 1364 status = RD_STATUS_PENDING;
506 n-ki 569 break;
507     }
508    
509 jsorg71 1364 status = RD_STATUS_CANCELLED;
510 matthewc 432 break;
511     case IRP_MJ_WRITE:
512 n-ki 569
513     buffer_len = 1;
514    
515     if (!fns->write)
516     {
517 jsorg71 1364 status = RD_STATUS_NOT_SUPPORTED;
518 n-ki 569 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 n-ki 627 if (!rdpdr_handle_ok(device, file))
528     {
529 jsorg71 1364 status = RD_STATUS_INVALID_HANDLE;
530 n-ki 627 break;
531     }
532    
533 stargo 865 if (rw_blocking) /* Complete immediately */
534 n-ki 592 {
535 n-ki 569 status = fns->write(file, s->p, length, offset, &result);
536     break;
537 n-ki 592 }
538    
539 stargo 865 /* Add to table */
540 n-ki 569 pst_buf = (uint8 *) xmalloc(length);
541 n-ki 612 if (!pst_buf)
542     {
543 jsorg71 1364 status = RD_STATUS_CANCELLED;
544 n-ki 612 break;
545     }
546    
547 n-ki 569 in_uint8a(s, pst_buf, length);
548    
549     if (add_async_iorequest
550 n-ki 613 (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
551 n-ki 569 {
552 jsorg71 1364 status = RD_STATUS_PENDING;
553 n-ki 569 break;
554     }
555    
556 jsorg71 1364 status = RD_STATUS_CANCELLED;
557 matthewc 432 break;
558    
559 n-ki 569 case IRP_MJ_QUERY_INFORMATION:
560    
561     if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
562     {
563 jsorg71 1364 status = RD_STATUS_INVALID_HANDLE;
564 n-ki 569 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 jsorg71 1364 status = RD_STATUS_INVALID_HANDLE;
580 n-ki 569 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 jsorg71 1364 status = RD_STATUS_INVALID_HANDLE;
596 n-ki 569 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 jsorg71 1364 status = RD_STATUS_INVALID_HANDLE;
612 n-ki 569 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, length);
626     convert_to_unix_filename(filename);
627     }
628     else
629     {
630     filename[0] = 0;
631     }
632     out.data = out.p = buffer;
633     out.size = sizeof(buffer);
634     status = disk_query_directory(file, info_level, filename,
635     &out);
636     result = buffer_len = out.p - out.data;
637     if (!buffer_len)
638     buffer_len++;
639     break;
640    
641     case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
642    
643     /* JIF
644 stargo 795 unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
645    
646 stargo 865 in_uint32_le(s, info_level); /* notify mask */
647 stargo 795
648     g_notify_stamp = True;
649    
650     status = disk_create_notify(file, info_level);
651     result = 0;
652    
653 jsorg71 1364 if (status == RD_STATUS_PENDING)
654 stargo 795 add_async_iorequest(device, file, id, major, length,
655     fns, 0, 0, NULL, 0);
656 n-ki 569 break;
657    
658     default:
659    
660 jsorg71 1364 status = RD_STATUS_INVALID_PARAMETER;
661 stargo 795 /* JIF */
662     unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
663 n-ki 569 }
664     break;
665    
666 matthewc 432 case IRP_MJ_DEVICE_CONTROL:
667 n-ki 569
668     if (!fns->device_control)
669 matthewc 432 {
670 jsorg71 1364 status = RD_STATUS_NOT_SUPPORTED;
671 n-ki 569 break;
672 matthewc 432 }
673 n-ki 569
674     in_uint32_le(s, bytes_out);
675     in_uint32_le(s, bytes_in);
676     in_uint32_le(s, request);
677     in_uint8s(s, 0x14);
678    
679     buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
680 n-ki 612 if (!buffer)
681     {
682 jsorg71 1364 status = RD_STATUS_CANCELLED;
683 n-ki 612 break;
684     }
685    
686 n-ki 569 out.data = out.p = buffer;
687     out.size = sizeof(buffer);
688 stargo 1319
689     DEBUG_SCARD(("[SMART-CARD TRACE]\n"));
690     DEBUG_SCARD(("device 0x%.8x\n", device));
691     DEBUG_SCARD(("file 0x%.8x\n", file));
692     DEBUG_SCARD(("id 0x%.8x\n", id));
693 stargo 1309 #ifdef WITH_SCARD
694     scardSetInfo(device, id, bytes_out + 0x14);
695     #endif
696 n-ki 569 status = fns->device_control(file, request, s, &out);
697     result = buffer_len = out.p - out.data;
698 stargo 1319
699     DEBUG_SCARD(("[SMART-CARD TRACE] OUT 0x%.8x\n", status));
700    
701 stargo 795 /* Serial SERIAL_WAIT_ON_MASK */
702 jsorg71 1364 if (status == RD_STATUS_PENDING)
703 stargo 795 {
704     if (add_async_iorequest
705     (device, file, id, major, length, fns, 0, 0, NULL, 0))
706     {
707 jsorg71 1364 status = RD_STATUS_PENDING;
708 stargo 795 break;
709     }
710     }
711 stargo 1309 #ifdef WITH_SCARD
712 jsorg71 1364 else if (status == (RD_STATUS_PENDING | 0xC0000000))
713     status = RD_STATUS_PENDING;
714 stargo 1309 #endif
715 matthewc 432 break;
716    
717 astrand 660
718     case IRP_MJ_LOCK_CONTROL:
719    
720     if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
721     {
722 jsorg71 1364 status = RD_STATUS_INVALID_HANDLE;
723 astrand 660 break;
724     }
725    
726     in_uint32_le(s, info_level);
727    
728     out.data = out.p = buffer;
729     out.size = sizeof(buffer);
730     /* FIXME: Perhaps consider actually *do*
731     something here :-) */
732 jsorg71 1364 status = RD_STATUS_SUCCESS;
733 astrand 660 result = buffer_len = out.p - out.data;
734     break;
735    
736 matthewc 432 default:
737     unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
738     break;
739     }
740    
741 jsorg71 1364 if (status != RD_STATUS_PENDING)
742 n-ki 569 {
743     rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
744     }
745 n-ki 612 if (buffer)
746     xfree(buffer);
747     buffer = NULL;
748 matthewc 432 }
749    
750 astrand 663 static void
751 n-ki 569 rdpdr_send_clientcapabilty(void)
752     {
753     uint8 magic[4] = "rDPC";
754     STREAM s;
755    
756     s = channel_init(rdpdr_channel, 0x50);
757     out_uint8a(s, magic, 4);
758     out_uint32_le(s, 5); /* count */
759     out_uint16_le(s, 1); /* first */
760     out_uint16_le(s, 0x28); /* length */
761     out_uint32_le(s, 1);
762     out_uint32_le(s, 2);
763     out_uint16_le(s, 2);
764     out_uint16_le(s, 5);
765     out_uint16_le(s, 1);
766     out_uint16_le(s, 5);
767     out_uint16_le(s, 0xFFFF);
768     out_uint16_le(s, 0);
769     out_uint32_le(s, 0);
770     out_uint32_le(s, 3);
771     out_uint32_le(s, 0);
772     out_uint32_le(s, 0);
773     out_uint16_le(s, 2); /* second */
774     out_uint16_le(s, 8); /* length */
775     out_uint32_le(s, 1);
776     out_uint16_le(s, 3); /* third */
777     out_uint16_le(s, 8); /* length */
778     out_uint32_le(s, 1);
779     out_uint16_le(s, 4); /* fourth */
780     out_uint16_le(s, 8); /* length */
781     out_uint32_le(s, 1);
782     out_uint16_le(s, 5); /* fifth */
783     out_uint16_le(s, 8); /* length */
784     out_uint32_le(s, 1);
785    
786     s_mark_end(s);
787     channel_send(s, rdpdr_channel);
788     }
789    
790 matthewc 432 static void
791     rdpdr_process(STREAM s)
792     {
793     uint32 handle;
794 matthewc 536 uint8 *magic;
795 matthewc 432
796 n-ki 569 #if WITH_DEBUG_RDP5
797     printf("--- rdpdr_process ---\n");
798 astrand 435 hexdump(s->p, s->end - s->p);
799 n-ki 569 #endif
800 matthewc 432 in_uint8p(s, magic, 4);
801    
802     if ((magic[0] == 'r') && (magic[1] == 'D'))
803     {
804     if ((magic[2] == 'R') && (magic[3] == 'I'))
805     {
806     rdpdr_process_irp(s);
807     return;
808     }
809     if ((magic[2] == 'n') && (magic[3] == 'I'))
810     {
811     rdpdr_send_connect();
812     rdpdr_send_name();
813     return;
814     }
815 n-ki 569 if ((magic[2] == 'C') && (magic[3] == 'C'))
816 matthewc 432 {
817     /* connect from server */
818 n-ki 569 rdpdr_send_clientcapabilty();
819     rdpdr_send_available();
820 matthewc 432 return;
821     }
822 n-ki 569 if ((magic[2] == 'r') && (magic[3] == 'd'))
823 matthewc 432 {
824     /* connect to a specific resource */
825     in_uint32(s, handle);
826 n-ki 569 #if WITH_DEBUG_RDP5
827     DEBUG(("RDPDR: Server connected to resource %d\n", handle));
828     #endif
829 matthewc 432 return;
830     }
831 n-ki 569 if ((magic[2] == 'P') && (magic[3] == 'S'))
832     {
833     /* server capability */
834     return;
835     }
836 matthewc 432 }
837 n-ki 569 if ((magic[0] == 'R') && (magic[1] == 'P'))
838     {
839     if ((magic[2] == 'C') && (magic[3] == 'P'))
840     {
841     printercache_process(s);
842     return;
843     }
844     }
845 matthewc 432 unimpl("RDPDR packet type %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
846     }
847    
848     BOOL
849 n-ki 569 rdpdr_init()
850 matthewc 432 {
851 n-ki 569 if (g_num_devices > 0)
852     {
853 astrand 580 rdpdr_channel =
854     channel_register("rdpdr",
855     CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
856     rdpdr_process);
857 n-ki 569 }
858    
859 matthewc 432 return (rdpdr_channel != NULL);
860     }
861 n-ki 569
862     /* Add file descriptors of pending io request to select() */
863     void
864     rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout)
865     {
866 stargo 865 uint32 select_timeout = 0; /* Timeout value to be used for select() (in millisecons). */
867 n-ki 569 struct async_iorequest *iorq;
868 astrand 662 char c;
869 n-ki 569
870 n-ki 593 iorq = g_iorequest;
871 n-ki 592 while (iorq != NULL)
872 n-ki 569 {
873 astrand 673 if (iorq->fd != 0)
874 n-ki 569 {
875     switch (iorq->major)
876     {
877     case IRP_MJ_READ:
878 astrand 673 /* Is this FD valid? FDs will
879     be invalid when
880     reconnecting. FIXME: Real
881     support for reconnects. */
882 n-ki 569
883     FD_SET(iorq->fd, rfds);
884 astrand 673 *n = MAX(*n, iorq->fd);
885 n-ki 569
886 stargo 795 /* Check if io request timeout is smaller than current (but not 0). */
887 n-ki 569 if (iorq->timeout
888     && (select_timeout == 0
889     || iorq->timeout < select_timeout))
890     {
891 stargo 795 /* Set new timeout */
892 n-ki 569 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 stargo 795 break;
898 n-ki 569 }
899 stargo 795 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 n-ki 569 break;
912    
913     case IRP_MJ_WRITE:
914 astrand 673 /* FD still valid? See above. */
915 astrand 674 if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
916 astrand 673 break;
917    
918 n-ki 569 FD_SET(iorq->fd, wfds);
919 astrand 673 *n = MAX(*n, iorq->fd);
920 n-ki 569 break;
921    
922 stargo 795 case IRP_MJ_DEVICE_CONTROL:
923     if (select_timeout > 5)
924     select_timeout = 5; /* serial event queue */
925     break;
926    
927 n-ki 569 }
928 astrand 673
929 n-ki 569 }
930 n-ki 592
931     iorq = iorq->next;
932 n-ki 569 }
933     }
934    
935 n-ki 627 struct async_iorequest *
936     rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
937     {
938     if (!iorq)
939     return NULL;
940 n-ki 590
941 n-ki 627 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 stargo 865 /* Even if NULL */
952 n-ki 627 g_iorequest = iorq->next;
953     xfree(iorq);
954     iorq = NULL;
955     }
956     return iorq;
957     }
958    
959 n-ki 569 /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
960 astrand 948 static void
961 stargo 795 _rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
962 n-ki 569 {
963 jsorg71 1364 RD_NTSTATUS status;
964 n-ki 592 uint32 result = 0;
965 n-ki 569 DEVICE_FNS *fns;
966     struct async_iorequest *iorq;
967 n-ki 592 struct async_iorequest *prev;
968 n-ki 595 uint32 req_size = 0;
969 stargo 795 uint32 buffer_len;
970     struct stream out;
971     uint8 *buffer = NULL;
972 n-ki 569
973 stargo 795
974 n-ki 569 if (timed_out)
975     {
976 stargo 795 /* 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 astrand 879 /*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
991 stargo 795 rdpdr_send_completion(iorq->device,
992 jsorg71 1364 iorq->id, RD_STATUS_SUCCESS,
993 stargo 795 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 jsorg71 1364 rdpdr_abort_io(g_min_timeout_fd, 0, RD_STATUS_TIMEOUT);
1017 n-ki 569 return;
1018     }
1019    
1020 n-ki 593 iorq = g_iorequest;
1021 n-ki 592 prev = NULL;
1022     while (iorq != NULL)
1023 n-ki 569 {
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 n-ki 592 /* Read the data */
1032 n-ki 569 fns = iorq->fns;
1033 n-ki 593
1034 n-ki 595 req_size =
1035     (iorq->length - iorq->partial_len) >
1036     8192 ? 8192 : (iorq->length -
1037     iorq->partial_len);
1038 n-ki 593 /* never read larger chunks than 8k - chances are that it will block */
1039 n-ki 592 status = fns->read(iorq->fd,
1040     iorq->buffer + iorq->partial_len,
1041 n-ki 613 req_size, iorq->offset, &result);
1042 n-ki 595
1043 stargo 795 if ((long) result > 0)
1044 n-ki 627 {
1045     iorq->partial_len += result;
1046     iorq->offset += result;
1047     }
1048 n-ki 569 #if WITH_DEBUG_RDP5
1049     DEBUG(("RDPDR: %d bytes of data read\n", result));
1050     #endif
1051 n-ki 592 /* only delete link if all data has been transfered */
1052 n-ki 595 /* or if result was 0 and status success - EOF */
1053     if ((iorq->partial_len == iorq->length) ||
1054 stargo 795 (result == 0))
1055 n-ki 592 {
1056 n-ki 595 #if WITH_DEBUG_RDP5
1057     DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
1058     #endif
1059 n-ki 592 rdpdr_send_completion(iorq->device,
1060     iorq->id, status,
1061 n-ki 595 iorq->partial_len,
1062     iorq->buffer,
1063     iorq->partial_len);
1064 n-ki 627 iorq = rdpdr_remove_iorequest(prev, iorq);
1065 n-ki 592 }
1066 n-ki 569 }
1067     break;
1068     case IRP_MJ_WRITE:
1069     if (FD_ISSET(iorq->fd, wfds))
1070     {
1071 n-ki 592 /* Write data. */
1072 n-ki 569 fns = iorq->fns;
1073 n-ki 593
1074 n-ki 595 req_size =
1075     (iorq->length - iorq->partial_len) >
1076     8192 ? 8192 : (iorq->length -
1077     iorq->partial_len);
1078    
1079 n-ki 593 /* never write larger chunks than 8k - chances are that it will block */
1080 n-ki 592 status = fns->write(iorq->fd,
1081     iorq->buffer +
1082 n-ki 613 iorq->partial_len, req_size,
1083     iorq->offset, &result);
1084 n-ki 627
1085 stargo 795 if ((long) result > 0)
1086 n-ki 627 {
1087     iorq->partial_len += result;
1088     iorq->offset += result;
1089     }
1090    
1091 n-ki 592 #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 n-ki 595 /* or we couldn't write */
1097     if ((iorq->partial_len == iorq->length)
1098     || (result == 0))
1099 n-ki 592 {
1100 n-ki 595 #if WITH_DEBUG_RDP5
1101     DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
1102     #endif
1103 n-ki 592 rdpdr_send_completion(iorq->device,
1104     iorq->id, status,
1105 astrand 608 iorq->partial_len,
1106     (uint8 *) "", 1);
1107 n-ki 569
1108 n-ki 627 iorq = rdpdr_remove_iorequest(prev, iorq);
1109 n-ki 592 }
1110 n-ki 569 }
1111     break;
1112 stargo 795 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 jsorg71 1364 status = RD_STATUS_SUCCESS;
1121 stargo 795 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 n-ki 569 }
1130 n-ki 592
1131 n-ki 569 }
1132 n-ki 592 prev = iorq;
1133 n-ki 614 if (iorq)
1134     iorq = iorq->next;
1135 n-ki 569 }
1136 n-ki 592
1137 stargo 795 /* 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 jsorg71 1364 if (status != RD_STATUS_PENDING)
1157 stargo 795 {
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 n-ki 569 }
1180    
1181 stargo 795 void
1182     rdpdr_check_fds(fd_set * rfds, fd_set * wfds, 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 n-ki 569 /* Abort a pending io request for a given handle and major */
1200     BOOL
1201 jsorg71 1364 rdpdr_abort_io(uint32 fd, uint32 major, RD_NTSTATUS status)
1202 n-ki 569 {
1203     uint32 result;
1204     struct async_iorequest *iorq;
1205 n-ki 592 struct async_iorequest *prev;
1206 n-ki 569
1207 stargo 602 iorq = g_iorequest;
1208 n-ki 592 prev = NULL;
1209     while (iorq != NULL)
1210 n-ki 569 {
1211 stargo 865 /* 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 n-ki 569 if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1214     {
1215     result = 0;
1216 astrand 608 rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
1217     1);
1218 n-ki 627
1219     iorq = rdpdr_remove_iorequest(prev, iorq);
1220 n-ki 569 return True;
1221     }
1222 n-ki 592
1223     prev = iorq;
1224     iorq = iorq->next;
1225 n-ki 569 }
1226 n-ki 592
1227 n-ki 569 return False;
1228     }

  ViewVC Help
Powered by ViewVC 1.1.26