/[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 613 - (hide annotations)
Mon Feb 23 10:34:18 2004 UTC (20 years, 2 months ago) by n-ki
File MIME type: text/plain
File size: 20746 byte(s)
correct handling of offset - partial merge from volker mildes patch. at date only disk.c uses offset, so there wont be any problem.

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

  ViewVC Help
Powered by ViewVC 1.1.26