/[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 646 - (hide annotations)
Fri Apr 2 15:34:38 2004 UTC (20 years, 2 months ago) by forsberg
File MIME type: text/plain
File size: 21318 byte(s)
Made it possible to set the client name displayed to the right of the
redirected disks (in Windows explorer) using -r:clientname=<client name>.

1 stargo 570 #include <unistd.h>
2 stargo 576 #include <sys/types.h>
3 stargo 609 #include <sys/time.h>
4 n-ki 627 #include <dirent.h> /* opendir, closedir, readdir */
5 stargo 576 #include <time.h>
6 matthewc 432 #include "rdesktop.h"
7    
8     #define IRP_MJ_CREATE 0x00
9     #define IRP_MJ_CLOSE 0x02
10     #define IRP_MJ_READ 0x03
11     #define IRP_MJ_WRITE 0x04
12     #define IRP_MJ_DEVICE_CONTROL 0x0e
13    
14 n-ki 569 #define IRP_MJ_CREATE 0x00
15     #define IRP_MJ_CLOSE 0x02
16     #define IRP_MJ_READ 0x03
17     #define IRP_MJ_WRITE 0x04
18     #define IRP_MJ_QUERY_INFORMATION 0x05
19     #define IRP_MJ_SET_INFORMATION 0x06
20     #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
21     #define IRP_MJ_DIRECTORY_CONTROL 0x0c
22     #define IRP_MJ_DEVICE_CONTROL 0x0e
23    
24     #define IRP_MN_QUERY_DIRECTORY 0x01
25     #define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02
26    
27 matthewc 432 extern char hostname[16];
28     extern DEVICE_FNS serial_fns;
29     extern DEVICE_FNS printer_fns;
30 n-ki 569 extern DEVICE_FNS parallel_fns;
31     extern DEVICE_FNS disk_fns;
32 n-ki 627 extern FILEINFO g_fileinfo[];
33 matthewc 432
34     static VCHANNEL *rdpdr_channel;
35    
36 n-ki 569 /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
37     HANDLE g_min_timeout_fd;
38     uint32 g_num_devices;
39    
40     /* Table with information about rdpdr devices */
41     RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
42 forsberg 646 char * g_rdpdr_clientname = NULL;
43 n-ki 569
44     /* Used to store incoming io request, until they are ready to be completed */
45 n-ki 592 /* using a linked list ensures that they are processed in the right order, */
46     /* if multiple ios are being done on the same fd */
47 n-ki 569 struct async_iorequest
48     {
49 n-ki 592 uint32 fd, major, minor, offset, device, id, length, partial_len;
50 n-ki 569 long timeout, /* Total timeout */
51     itv_timeout; /* Interval timeout (between serial characters) */
52     uint8 *buffer;
53     DEVICE_FNS *fns;
54    
55 n-ki 592 struct async_iorequest *next; /* next element in list */
56 n-ki 593 };
57 n-ki 592
58 n-ki 593 struct async_iorequest *g_iorequest;
59    
60 n-ki 569 /* Return device_id for a given handle */
61     int
62     get_device_index(HANDLE handle)
63     {
64     int i;
65     for (i = 0; i < RDPDR_MAX_DEVICES; i++)
66     {
67     if (g_rdpdr_device[i].handle == handle)
68     return i;
69     }
70     return -1;
71     }
72    
73     /* Converts a windows path to a unix path */
74 matthewc 432 void
75 n-ki 569 convert_to_unix_filename(char *filename)
76     {
77     char *p;
78    
79     while ((p = strchr(filename, '\\')))
80     {
81     *p = '/';
82     }
83     }
84    
85 n-ki 627 BOOL
86     rdpdr_handle_ok(int device, int handle)
87     {
88     switch (g_rdpdr_device[device].device_type)
89     {
90     case DEVICE_TYPE_PARALLEL:
91     case DEVICE_TYPE_SERIAL:
92     case DEVICE_TYPE_PRINTER:
93     case DEVICE_TYPE_SCARD:
94     if (g_rdpdr_device[device].handle != handle)
95     return False;
96     break;
97     case DEVICE_TYPE_DISK:
98     if (g_fileinfo[handle].device_id != device)
99     return False;
100     break;
101     }
102     return True;
103     }
104    
105 n-ki 569 /* Add a new io request to the table containing pending io requests so it won't block rdesktop */
106     BOOL
107     add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,
108 n-ki 613 DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
109     uint32 offset)
110 n-ki 569 {
111     struct async_iorequest *iorq;
112    
113 n-ki 593 if (g_iorequest == NULL)
114     {
115     g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
116 n-ki 612 if (!g_iorequest)
117     return False;
118 n-ki 593 g_iorequest->fd = 0;
119     g_iorequest->next = NULL;
120     }
121    
122     iorq = g_iorequest;
123    
124 n-ki 592 while (iorq->fd != 0)
125 n-ki 569 {
126 n-ki 592 // create new element if needed
127     if (iorq->next == NULL)
128 n-ki 569 {
129 n-ki 592 iorq->next =
130     (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
131 n-ki 612 if (!iorq->next)
132     return False;
133 n-ki 593 iorq->next->fd = 0;
134     iorq->next->next = NULL;
135 n-ki 592 }
136 n-ki 593 iorq = iorq->next;
137 n-ki 569 }
138 n-ki 592 iorq->device = device;
139     iorq->fd = file;
140     iorq->id = id;
141     iorq->major = major;
142     iorq->length = length;
143     iorq->partial_len = 0;
144     iorq->fns = fns;
145     iorq->timeout = total_timeout;
146     iorq->itv_timeout = interval_timeout;
147     iorq->buffer = buffer;
148 n-ki 613 iorq->offset = offset;
149 n-ki 592 return True;
150 n-ki 569 }
151    
152     void
153 matthewc 432 rdpdr_send_connect(void)
154     {
155     uint8 magic[4] = "rDCC";
156     STREAM s;
157    
158     s = channel_init(rdpdr_channel, 12);
159     out_uint8a(s, magic, 4);
160 astrand 435 out_uint16_le(s, 1); /* unknown */
161 matthewc 432 out_uint16_le(s, 5);
162 astrand 435 out_uint32_be(s, 0x815ed39d); /* IP address (use 127.0.0.1) 0x815ed39d */
163 matthewc 432 s_mark_end(s);
164     channel_send(s, rdpdr_channel);
165     }
166    
167 n-ki 569
168 matthewc 432 void
169     rdpdr_send_name(void)
170     {
171     uint8 magic[4] = "rDNC";
172 forsberg 646 if (NULL == g_rdpdr_clientname) {
173     g_rdpdr_clientname = hostname;
174     }
175     uint32 hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
176 matthewc 432 STREAM s;
177    
178 astrand 435 s = channel_init(rdpdr_channel, 16 + hostlen);
179 matthewc 432 out_uint8a(s, magic, 4);
180 astrand 435 out_uint16_le(s, 0x63); /* unknown */
181 matthewc 432 out_uint16_le(s, 0x72);
182     out_uint32(s, 0);
183     out_uint32_le(s, hostlen);
184 forsberg 646 rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
185 matthewc 432 s_mark_end(s);
186     channel_send(s, rdpdr_channel);
187     }
188    
189 n-ki 569 /* Returns the size of the payload of the announce packet */
190     int
191     announcedata_size()
192     {
193     int size, i;
194     PRINTER *printerinfo;
195    
196     size = 8; //static announce size
197     size += g_num_devices * 0x14;
198    
199     for (i = 0; i < g_num_devices; i++)
200     {
201     if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)
202     {
203     printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
204     printerinfo->bloblen =
205     printercache_load_blob(printerinfo->printer, &(printerinfo->blob));
206    
207     size += 0x18;
208     size += 2 * strlen(printerinfo->driver) + 2;
209     size += 2 * strlen(printerinfo->printer) + 2;
210     size += printerinfo->bloblen;
211     }
212     }
213    
214     return size;
215     }
216    
217 matthewc 432 void
218     rdpdr_send_available(void)
219     {
220 n-ki 569
221 matthewc 432 uint8 magic[4] = "rDAD";
222 n-ki 569 uint32 driverlen, printerlen, bloblen;
223     int i;
224 matthewc 432 STREAM s;
225 n-ki 569 PRINTER *printerinfo;
226 matthewc 432
227 n-ki 569 s = channel_init(rdpdr_channel, announcedata_size());
228 matthewc 432 out_uint8a(s, magic, 4);
229 n-ki 569 out_uint32_le(s, g_num_devices);
230 matthewc 432
231 n-ki 569 for (i = 0; i < g_num_devices; i++)
232     {
233     out_uint32_le(s, g_rdpdr_device[i].device_type);
234     out_uint32_le(s, i); /* RDP Device ID */
235     out_uint8p(s, g_rdpdr_device[i].name, 8);
236    
237 n-ki 585 switch (g_rdpdr_device[i].device_type)
238 n-ki 569 {
239 n-ki 585 case DEVICE_TYPE_PRINTER:
240     printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
241 n-ki 569
242 n-ki 585 driverlen = 2 * strlen(printerinfo->driver) + 2;
243     printerlen = 2 * strlen(printerinfo->printer) + 2;
244     bloblen = printerinfo->bloblen;
245 n-ki 569
246 n-ki 585 out_uint32_le(s, 24 + driverlen + printerlen + bloblen); /* length of extra info */
247     out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
248     out_uint8s(s, 8); /* unknown */
249     out_uint32_le(s, driverlen);
250     out_uint32_le(s, printerlen);
251     out_uint32_le(s, bloblen);
252     rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
253     rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
254     out_uint8a(s, printerinfo->blob, bloblen);
255 n-ki 569
256 n-ki 598 if (printerinfo->blob)
257     xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */
258 n-ki 585 break;
259     default:
260     out_uint32(s, 0);
261 n-ki 569 }
262     }
263 matthewc 432 #if 0
264 astrand 435 out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */
265 matthewc 432 out_uint32_le(s, 0);
266     out_uint8p(s, "SCARD", 5);
267     out_uint8s(s, 3);
268     out_uint32(s, 0);
269     #endif
270    
271     s_mark_end(s);
272     channel_send(s, rdpdr_channel);
273     }
274    
275     void
276 astrand 435 rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,
277     uint32 length)
278 matthewc 432 {
279     uint8 magic[4] = "rDCI";
280     STREAM s;
281    
282     s = channel_init(rdpdr_channel, 20 + length);
283     out_uint8a(s, magic, 4);
284     out_uint32_le(s, device);
285     out_uint32_le(s, id);
286     out_uint32_le(s, status);
287     out_uint32_le(s, result);
288     out_uint8p(s, buffer, length);
289     s_mark_end(s);
290 n-ki 569 /* JIF
291     hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
292 matthewc 432 channel_send(s, rdpdr_channel);
293     }
294    
295     static void
296     rdpdr_process_irp(STREAM s)
297     {
298 n-ki 569 uint32 result = 0,
299     length = 0,
300     desired_access = 0,
301     request,
302     file,
303     info_level,
304     buffer_len,
305     id,
306     major,
307     minor,
308     device,
309     offset,
310     bytes_in,
311     bytes_out,
312     error_mode,
313     share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
314    
315     char filename[256];
316     uint8 *buffer, *pst_buf;
317 matthewc 432 struct stream out;
318     DEVICE_FNS *fns;
319 n-ki 569 BOOL rw_blocking = True;
320     NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;
321 matthewc 432
322     in_uint32_le(s, device);
323     in_uint32_le(s, file);
324     in_uint32_le(s, id);
325     in_uint32_le(s, major);
326     in_uint32_le(s, minor);
327    
328 n-ki 569 buffer_len = 0;
329     buffer = (uint8 *) xmalloc(1024);
330     buffer[0] = 0;
331 matthewc 432
332 n-ki 569 switch (g_rdpdr_device[device].device_type)
333 matthewc 432 {
334 n-ki 569 case DEVICE_TYPE_SERIAL:
335    
336 matthewc 432 fns = &serial_fns;
337 n-ki 592 rw_blocking = False;
338 n-ki 569 break;
339    
340     case DEVICE_TYPE_PARALLEL:
341    
342     fns = &parallel_fns;
343 n-ki 592 rw_blocking = False;
344 n-ki 569 break;
345    
346     case DEVICE_TYPE_PRINTER:
347    
348 matthewc 432 fns = &printer_fns;
349 n-ki 569 break;
350    
351     case DEVICE_TYPE_DISK:
352    
353 n-ki 593 fns = &disk_fns;
354 n-ki 595 rw_blocking = False;
355 n-ki 569 break;
356    
357     case DEVICE_TYPE_SCARD:
358 matthewc 432 default:
359 n-ki 569
360 matthewc 432 error("IRP for bad device %ld\n", device);
361     return;
362     }
363    
364     switch (major)
365     {
366     case IRP_MJ_CREATE:
367 n-ki 569
368     in_uint32_be(s, desired_access);
369     in_uint8s(s, 0x08); // unknown
370     in_uint32_le(s, error_mode);
371     in_uint32_le(s, share_mode);
372     in_uint32_le(s, disposition);
373     in_uint32_le(s, flags_and_attributes);
374     in_uint32_le(s, length);
375    
376     if (length && (length / 2) < 256)
377     {
378     rdp_in_unistr(s, filename, length);
379     convert_to_unix_filename(filename);
380     }
381     else
382     {
383     filename[0] = 0;
384     }
385    
386     if (!fns->create)
387     {
388     status = STATUS_NOT_SUPPORTED;
389     break;
390     }
391    
392     status = fns->create(device, desired_access, share_mode, disposition,
393     flags_and_attributes, filename, &result);
394     buffer_len = 1;
395 matthewc 432 break;
396    
397     case IRP_MJ_CLOSE:
398 n-ki 569 if (!fns->close)
399     {
400     status = STATUS_NOT_SUPPORTED;
401     break;
402     }
403    
404     status = fns->close(file);
405 matthewc 432 break;
406    
407     case IRP_MJ_READ:
408 n-ki 569
409     if (!fns->read)
410 matthewc 432 {
411 n-ki 569 status = STATUS_NOT_SUPPORTED;
412     break;
413     }
414    
415     in_uint32_le(s, length);
416     in_uint32_le(s, offset);
417     #if WITH_DEBUG_RDP5
418     DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
419     #endif
420 n-ki 627 if (!rdpdr_handle_ok(device, file))
421     {
422     status = STATUS_INVALID_HANDLE;
423     break;
424     }
425    
426 n-ki 592 if (rw_blocking) // Complete read immediately
427     {
428 n-ki 569 buffer = (uint8 *) xrealloc((void *) buffer, length);
429 n-ki 612 if (!buffer)
430     {
431     status = STATUS_CANCELLED;
432     break;
433     }
434 n-ki 569 status = fns->read(file, buffer, length, offset, &result);
435 matthewc 432 buffer_len = result;
436 n-ki 569 break;
437 n-ki 592 }
438 n-ki 569
439     // Add request to table
440     pst_buf = (uint8 *) xmalloc(length);
441 n-ki 612 if (!pst_buf)
442     {
443     status = STATUS_CANCELLED;
444     break;
445     }
446 n-ki 569 serial_get_timeout(file, length, &total_timeout, &interval_timeout);
447     if (add_async_iorequest
448     (device, file, id, major, length, fns, total_timeout, interval_timeout,
449 n-ki 613 pst_buf, offset))
450 n-ki 569 {
451     status = STATUS_PENDING;
452     break;
453     }
454    
455     status = STATUS_CANCELLED;
456 matthewc 432 break;
457     case IRP_MJ_WRITE:
458 n-ki 569
459     buffer_len = 1;
460    
461     if (!fns->write)
462     {
463     status = STATUS_NOT_SUPPORTED;
464     break;
465     }
466    
467     in_uint32_le(s, length);
468     in_uint32_le(s, offset);
469     in_uint8s(s, 0x18);
470     #if WITH_DEBUG_RDP5
471     DEBUG(("RDPDR IRP Write (length: %d)\n", result));
472     #endif
473 n-ki 627 if (!rdpdr_handle_ok(device, file))
474     {
475     status = STATUS_INVALID_HANDLE;
476     break;
477     }
478    
479 n-ki 592 if (rw_blocking) // Complete immediately
480     {
481 n-ki 569 status = fns->write(file, s->p, length, offset, &result);
482     break;
483 n-ki 592 }
484    
485 n-ki 569 // Add to table
486     pst_buf = (uint8 *) xmalloc(length);
487 n-ki 612 if (!pst_buf)
488     {
489     status = STATUS_CANCELLED;
490     break;
491     }
492    
493 n-ki 569 in_uint8a(s, pst_buf, length);
494    
495     if (add_async_iorequest
496 n-ki 613 (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
497 n-ki 569 {
498     status = STATUS_PENDING;
499     break;
500     }
501    
502     status = STATUS_CANCELLED;
503 matthewc 432 break;
504    
505 n-ki 569 case IRP_MJ_QUERY_INFORMATION:
506    
507     if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
508     {
509     status = STATUS_INVALID_HANDLE;
510     break;
511     }
512     in_uint32_le(s, info_level);
513    
514     out.data = out.p = buffer;
515     out.size = sizeof(buffer);
516     status = disk_query_information(file, info_level, &out);
517     result = buffer_len = out.p - out.data;
518    
519     break;
520    
521     case IRP_MJ_SET_INFORMATION:
522    
523     if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
524     {
525     status = STATUS_INVALID_HANDLE;
526     break;
527     }
528    
529     in_uint32_le(s, info_level);
530    
531     out.data = out.p = buffer;
532     out.size = sizeof(buffer);
533     status = disk_set_information(file, info_level, s, &out);
534     result = buffer_len = out.p - out.data;
535     break;
536    
537     case IRP_MJ_QUERY_VOLUME_INFORMATION:
538    
539     if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
540     {
541     status = STATUS_INVALID_HANDLE;
542     break;
543     }
544    
545     in_uint32_le(s, info_level);
546    
547     out.data = out.p = buffer;
548     out.size = sizeof(buffer);
549     status = disk_query_volume_information(file, info_level, &out);
550     result = buffer_len = out.p - out.data;
551     break;
552    
553     case IRP_MJ_DIRECTORY_CONTROL:
554    
555     if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
556     {
557     status = STATUS_INVALID_HANDLE;
558     break;
559     }
560    
561     switch (minor)
562     {
563     case IRP_MN_QUERY_DIRECTORY:
564    
565     in_uint32_le(s, info_level);
566     in_uint8s(s, 1);
567     in_uint32_le(s, length);
568     in_uint8s(s, 0x17);
569     if (length && length < 2 * 255)
570     {
571     rdp_in_unistr(s, filename, length);
572     convert_to_unix_filename(filename);
573     }
574     else
575     {
576     filename[0] = 0;
577     }
578     out.data = out.p = buffer;
579     out.size = sizeof(buffer);
580     status = disk_query_directory(file, info_level, filename,
581     &out);
582     result = buffer_len = out.p - out.data;
583     if (!buffer_len)
584     buffer_len++;
585     break;
586    
587     case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
588    
589     /* JIF
590     unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
591     status = STATUS_PENDING; // Don't send completion packet
592     break;
593    
594     default:
595    
596     status = STATUS_INVALID_PARAMETER;
597     /* JIF
598     unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */
599     }
600     break;
601    
602 matthewc 432 case IRP_MJ_DEVICE_CONTROL:
603 n-ki 569
604     if (!fns->device_control)
605 matthewc 432 {
606 n-ki 569 status = STATUS_NOT_SUPPORTED;
607     break;
608 matthewc 432 }
609 n-ki 569
610     in_uint32_le(s, bytes_out);
611     in_uint32_le(s, bytes_in);
612     in_uint32_le(s, request);
613     in_uint8s(s, 0x14);
614    
615     buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
616 n-ki 612 if (!buffer)
617     {
618     status = STATUS_CANCELLED;
619     break;
620     }
621    
622 n-ki 569 out.data = out.p = buffer;
623     out.size = sizeof(buffer);
624     status = fns->device_control(file, request, s, &out);
625     result = buffer_len = out.p - out.data;
626 matthewc 432 break;
627    
628     default:
629     unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
630     break;
631     }
632    
633 n-ki 569 if (status != STATUS_PENDING)
634     {
635     rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
636     }
637 n-ki 612 if (buffer)
638     xfree(buffer);
639     buffer = NULL;
640 matthewc 432 }
641    
642 n-ki 569 void
643     rdpdr_send_clientcapabilty(void)
644     {
645     uint8 magic[4] = "rDPC";
646     STREAM s;
647    
648     s = channel_init(rdpdr_channel, 0x50);
649     out_uint8a(s, magic, 4);
650     out_uint32_le(s, 5); /* count */
651     out_uint16_le(s, 1); /* first */
652     out_uint16_le(s, 0x28); /* length */
653     out_uint32_le(s, 1);
654     out_uint32_le(s, 2);
655     out_uint16_le(s, 2);
656     out_uint16_le(s, 5);
657     out_uint16_le(s, 1);
658     out_uint16_le(s, 5);
659     out_uint16_le(s, 0xFFFF);
660     out_uint16_le(s, 0);
661     out_uint32_le(s, 0);
662     out_uint32_le(s, 3);
663     out_uint32_le(s, 0);
664     out_uint32_le(s, 0);
665     out_uint16_le(s, 2); /* second */
666     out_uint16_le(s, 8); /* length */
667     out_uint32_le(s, 1);
668     out_uint16_le(s, 3); /* third */
669     out_uint16_le(s, 8); /* length */
670     out_uint32_le(s, 1);
671     out_uint16_le(s, 4); /* fourth */
672     out_uint16_le(s, 8); /* length */
673     out_uint32_le(s, 1);
674     out_uint16_le(s, 5); /* fifth */
675     out_uint16_le(s, 8); /* length */
676     out_uint32_le(s, 1);
677    
678     s_mark_end(s);
679     channel_send(s, rdpdr_channel);
680     }
681    
682 matthewc 432 static void
683     rdpdr_process(STREAM s)
684     {
685     uint32 handle;
686 matthewc 536 uint8 *magic;
687 matthewc 432
688 n-ki 569 #if WITH_DEBUG_RDP5
689     printf("--- rdpdr_process ---\n");
690 astrand 435 hexdump(s->p, s->end - s->p);
691 n-ki 569 #endif
692 matthewc 432 in_uint8p(s, magic, 4);
693    
694     if ((magic[0] == 'r') && (magic[1] == 'D'))
695     {
696     if ((magic[2] == 'R') && (magic[3] == 'I'))
697     {
698     rdpdr_process_irp(s);
699     return;
700     }
701     if ((magic[2] == 'n') && (magic[3] == 'I'))
702     {
703     rdpdr_send_connect();
704     rdpdr_send_name();
705     return;
706     }
707 n-ki 569 if ((magic[2] == 'C') && (magic[3] == 'C'))
708 matthewc 432 {
709     /* connect from server */
710 n-ki 569 rdpdr_send_clientcapabilty();
711     rdpdr_send_available();
712 matthewc 432 return;
713     }
714 n-ki 569 if ((magic[2] == 'r') && (magic[3] == 'd'))
715 matthewc 432 {
716     /* connect to a specific resource */
717     in_uint32(s, handle);
718 n-ki 569 #if WITH_DEBUG_RDP5
719     DEBUG(("RDPDR: Server connected to resource %d\n", handle));
720     #endif
721 matthewc 432 return;
722     }
723 n-ki 569 if ((magic[2] == 'P') && (magic[3] == 'S'))
724     {
725     /* server capability */
726     return;
727     }
728 matthewc 432 }
729 n-ki 569 if ((magic[0] == 'R') && (magic[1] == 'P'))
730     {
731     if ((magic[2] == 'C') && (magic[3] == 'P'))
732     {
733     printercache_process(s);
734     return;
735     }
736     }
737 matthewc 432 unimpl("RDPDR packet type %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]);
738     }
739    
740     BOOL
741 n-ki 569 rdpdr_init()
742 matthewc 432 {
743 n-ki 569 if (g_num_devices > 0)
744     {
745 astrand 580 rdpdr_channel =
746     channel_register("rdpdr",
747     CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
748     rdpdr_process);
749 n-ki 569 }
750    
751 matthewc 432 return (rdpdr_channel != NULL);
752     }
753 n-ki 569
754     /* Add file descriptors of pending io request to select() */
755     void
756     rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout)
757     {
758 stargo 607 uint32 select_timeout = 0; // Timeout value to be used for select() (in millisecons).
759 n-ki 569 struct async_iorequest *iorq;
760    
761 n-ki 593 iorq = g_iorequest;
762 n-ki 592 while (iorq != NULL)
763 n-ki 569 {
764 n-ki 592 if (iorq->fd != 0)
765 n-ki 569 {
766     switch (iorq->major)
767     {
768     case IRP_MJ_READ:
769    
770     FD_SET(iorq->fd, rfds);
771    
772     // Check if io request timeout is smaller than current (but not 0).
773     if (iorq->timeout
774     && (select_timeout == 0
775     || iorq->timeout < select_timeout))
776     {
777     // Set new timeout
778     select_timeout = iorq->timeout;
779     g_min_timeout_fd = iorq->fd; /* Remember fd */
780     tv->tv_sec = select_timeout / 1000;
781     tv->tv_usec = (select_timeout % 1000) * 1000;
782     *timeout = True;
783     }
784     break;
785    
786     case IRP_MJ_WRITE:
787     FD_SET(iorq->fd, wfds);
788     break;
789    
790     }
791     *n = MAX(*n, iorq->fd);
792     }
793 n-ki 592
794     iorq = iorq->next;
795 n-ki 569 }
796     }
797    
798 n-ki 627 struct async_iorequest *
799     rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
800     {
801     if (!iorq)
802     return NULL;
803 n-ki 590
804 n-ki 627 if (iorq->buffer)
805     xfree(iorq->buffer);
806     if (prev)
807     {
808     prev->next = iorq->next;
809     xfree(iorq);
810     iorq = prev->next;
811     }
812     else
813     {
814     // Even if NULL
815     g_iorequest = iorq->next;
816     xfree(iorq);
817     iorq = NULL;
818     }
819     return iorq;
820     }
821    
822 n-ki 569 /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
823     void
824     rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
825     {
826     NTSTATUS status;
827 n-ki 592 uint32 result = 0;
828 n-ki 569 DEVICE_FNS *fns;
829     struct async_iorequest *iorq;
830 n-ki 592 struct async_iorequest *prev;
831 n-ki 595 uint32 req_size = 0;
832 n-ki 569
833     if (timed_out)
834     {
835     rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);
836     return;
837     }
838    
839 n-ki 593 iorq = g_iorequest;
840 n-ki 592 prev = NULL;
841     while (iorq != NULL)
842 n-ki 569 {
843     if (iorq->fd != 0)
844     {
845     switch (iorq->major)
846     {
847     case IRP_MJ_READ:
848     if (FD_ISSET(iorq->fd, rfds))
849     {
850 n-ki 592 /* Read the data */
851 n-ki 569 fns = iorq->fns;
852 n-ki 593
853 n-ki 595 req_size =
854     (iorq->length - iorq->partial_len) >
855     8192 ? 8192 : (iorq->length -
856     iorq->partial_len);
857 n-ki 593 /* never read larger chunks than 8k - chances are that it will block */
858 n-ki 592 status = fns->read(iorq->fd,
859     iorq->buffer + iorq->partial_len,
860 n-ki 613 req_size, iorq->offset, &result);
861 n-ki 595
862 n-ki 627 if (result > 0)
863     {
864     iorq->partial_len += result;
865     iorq->offset += result;
866     }
867 n-ki 569 #if WITH_DEBUG_RDP5
868     DEBUG(("RDPDR: %d bytes of data read\n", result));
869     #endif
870 n-ki 592 /* only delete link if all data has been transfered */
871 n-ki 595 /* or if result was 0 and status success - EOF */
872     if ((iorq->partial_len == iorq->length) ||
873     (result == 0))
874 n-ki 592 {
875 n-ki 595 #if WITH_DEBUG_RDP5
876     DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
877     #endif
878 n-ki 592 rdpdr_send_completion(iorq->device,
879     iorq->id, status,
880 n-ki 595 iorq->partial_len,
881     iorq->buffer,
882     iorq->partial_len);
883 n-ki 627 iorq = rdpdr_remove_iorequest(prev, iorq);
884 n-ki 592 }
885 n-ki 569 }
886     break;
887     case IRP_MJ_WRITE:
888     if (FD_ISSET(iorq->fd, wfds))
889     {
890 n-ki 592 /* Write data. */
891 n-ki 569 fns = iorq->fns;
892 n-ki 593
893 n-ki 595 req_size =
894     (iorq->length - iorq->partial_len) >
895     8192 ? 8192 : (iorq->length -
896     iorq->partial_len);
897    
898 n-ki 593 /* never write larger chunks than 8k - chances are that it will block */
899 n-ki 592 status = fns->write(iorq->fd,
900     iorq->buffer +
901 n-ki 613 iorq->partial_len, req_size,
902     iorq->offset, &result);
903 n-ki 627
904     if (result > 0)
905     {
906     iorq->partial_len += result;
907     iorq->offset += result;
908     }
909    
910 n-ki 592 #if WITH_DEBUG_RDP5
911     DEBUG(("RDPDR: %d bytes of data written\n",
912     result));
913     #endif
914     /* only delete link if all data has been transfered */
915 n-ki 595 /* or we couldn't write */
916     if ((iorq->partial_len == iorq->length)
917     || (result == 0))
918 n-ki 592 {
919 n-ki 595 #if WITH_DEBUG_RDP5
920     DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
921     #endif
922 n-ki 592 rdpdr_send_completion(iorq->device,
923     iorq->id, status,
924 astrand 608 iorq->partial_len,
925     (uint8 *) "", 1);
926 n-ki 569
927 n-ki 627 iorq = rdpdr_remove_iorequest(prev, iorq);
928 n-ki 592 }
929 n-ki 569 }
930     break;
931     }
932 n-ki 592
933 n-ki 569 }
934 n-ki 592 prev = iorq;
935 n-ki 614 if (iorq)
936     iorq = iorq->next;
937 n-ki 569 }
938 n-ki 592
939 n-ki 569 }
940    
941     /* Abort a pending io request for a given handle and major */
942     BOOL
943     rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
944     {
945     uint32 result;
946     struct async_iorequest *iorq;
947 n-ki 592 struct async_iorequest *prev;
948 n-ki 569
949 stargo 602 iorq = g_iorequest;
950 n-ki 592 prev = NULL;
951     while (iorq != NULL)
952 n-ki 569 {
953     // Only remove from table when major is not set, or when correct major is supplied.
954     // Abort read should not abort a write io request.
955     if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
956     {
957     result = 0;
958 astrand 608 rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
959     1);
960 n-ki 627
961     iorq = rdpdr_remove_iorequest(prev, iorq);
962 n-ki 569 return True;
963     }
964 n-ki 592
965     prev = iorq;
966     iorq = iorq->next;
967 n-ki 569 }
968 n-ki 592
969 n-ki 569 return False;
970     }

  ViewVC Help
Powered by ViewVC 1.1.26