/[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 651 - (hide annotations)
Thu Apr 15 20:12:42 2004 UTC (20 years, 1 month ago) by astrand
File MIME type: text/plain
File size: 21229 byte(s)
Indent fixes

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

  ViewVC Help
Powered by ViewVC 1.1.26