/[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 612 - (hide annotations)
Mon Feb 23 09:58:16 2004 UTC (20 years, 3 months ago) by n-ki
File MIME type: text/plain
File size: 20603 byte(s)
better error handling in rdpdr and disk.c - partial merge of Volker Mildes patch.

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

  ViewVC Help
Powered by ViewVC 1.1.26