/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 435 by astrand, Wed Jul 9 09:18:20 2003 UTC revision 646 by forsberg, Fri Apr 2 15:34:38 2004 UTC
# Line 1  Line 1 
1    #include <unistd.h>
2    #include <sys/types.h>
3    #include <sys/time.h>
4    #include <dirent.h>             /* opendir, closedir, readdir */
5    #include <time.h>
6  #include "rdesktop.h"  #include "rdesktop.h"
7    
8  #define IRP_MJ_CREATE           0x00  #define IRP_MJ_CREATE           0x00
# Line 6  Line 11 
11  #define IRP_MJ_WRITE            0x04  #define IRP_MJ_WRITE            0x04
12  #define IRP_MJ_DEVICE_CONTROL   0x0e  #define IRP_MJ_DEVICE_CONTROL   0x0e
13    
14    #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  extern char hostname[16];  extern char hostname[16];
28  extern DEVICE_FNS serial_fns;  extern DEVICE_FNS serial_fns;
29  extern DEVICE_FNS printer_fns;  extern DEVICE_FNS printer_fns;
30    extern DEVICE_FNS parallel_fns;
31    extern DEVICE_FNS disk_fns;
32    extern FILEINFO g_fileinfo[];
33    
34  static VCHANNEL *rdpdr_channel;  static VCHANNEL *rdpdr_channel;
35    
36    /* 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    char * g_rdpdr_clientname = NULL;
43    
44    /* Used to store incoming io request, until they are ready to be completed */
45    /* 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    struct async_iorequest
48    {
49            uint32 fd, major, minor, offset, device, id, length, partial_len;
50            long timeout,           /* Total timeout */
51              itv_timeout;          /* Interval timeout (between serial characters) */
52            uint8 *buffer;
53            DEVICE_FNS *fns;
54    
55            struct async_iorequest *next;   /* next element in list */
56    };
57    
58    struct async_iorequest *g_iorequest;
59    
60    /* 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    void
75    convert_to_unix_filename(char *filename)
76    {
77            char *p;
78    
79            while ((p = strchr(filename, '\\')))
80            {
81                    *p = '/';
82            }
83    }
84    
85    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    /* 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                        DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
109                        uint32 offset)
110    {
111            struct async_iorequest *iorq;
112    
113            if (g_iorequest == NULL)
114            {
115                    g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
116                    if (!g_iorequest)
117                            return False;
118                    g_iorequest->fd = 0;
119                    g_iorequest->next = NULL;
120            }
121    
122            iorq = g_iorequest;
123    
124            while (iorq->fd != 0)
125            {
126                    // create new element if needed
127                    if (iorq->next == NULL)
128                    {
129                            iorq->next =
130                                    (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
131                            if (!iorq->next)
132                                    return False;
133                            iorq->next->fd = 0;
134                            iorq->next->next = NULL;
135                    }
136                    iorq = iorq->next;
137            }
138            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            iorq->offset = offset;
149            return True;
150    }
151    
152  void  void
153  rdpdr_send_connect(void)  rdpdr_send_connect(void)
154  {  {
# Line 27  rdpdr_send_connect(void) Line 164  rdpdr_send_connect(void)
164          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
165  }  }
166    
167    
168  void  void
169  rdpdr_send_name(void)  rdpdr_send_name(void)
170  {  {
171          uint8 magic[4] = "rDNC";          uint8 magic[4] = "rDNC";
172          uint32 hostlen = (strlen(hostname) + 1) * 2;          if (NULL == g_rdpdr_clientname) {
173              g_rdpdr_clientname = hostname;
174            }
175            uint32 hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
176          STREAM s;          STREAM s;
177    
178          s = channel_init(rdpdr_channel, 16 + hostlen);          s = channel_init(rdpdr_channel, 16 + hostlen);
# Line 40  rdpdr_send_name(void) Line 181  rdpdr_send_name(void)
181          out_uint16_le(s, 0x72);          out_uint16_le(s, 0x72);
182          out_uint32(s, 0);          out_uint32(s, 0);
183          out_uint32_le(s, hostlen);          out_uint32_le(s, hostlen);
184          rdp_out_unistr(s, hostname, hostlen - 2);          rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
185          s_mark_end(s);          s_mark_end(s);
186          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
187  }  }
188    
189    /* 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  void  void
218  rdpdr_send_available(void)  rdpdr_send_available(void)
219  {  {
220    
221          uint8 magic[4] = "rDAD";          uint8 magic[4] = "rDAD";
222          char *driver = "Digital turbo PrintServer 20";  /* Fairly generic PostScript driver */          uint32 driverlen, printerlen, bloblen;
223          char *printer = "PostScript";          int i;
         uint32 driverlen = (strlen(driver) + 1) * 2;  
         uint32 printerlen = (strlen(printer) + 1) * 2;  
224          STREAM s;          STREAM s;
225            PRINTER *printerinfo;
226    
227          s = channel_init(rdpdr_channel, 8 + 20);          s = channel_init(rdpdr_channel, announcedata_size());
228          out_uint8a(s, magic, 4);          out_uint8a(s, magic, 4);
229          out_uint32_le(s, 1);    /* Number of devices */          out_uint32_le(s, g_num_devices);
230    
231  #if 1          for (i = 0; i < g_num_devices; i++)
232          out_uint32_le(s, 0x1);  /* Device type 0x1 - serial */          {
233          out_uint32_le(s, 0);    /* Handle */                  out_uint32_le(s, g_rdpdr_device[i].device_type);
234          out_uint8p(s, "COM2", 4);                  out_uint32_le(s, i);    /* RDP Device ID */
235          out_uint8s(s, 4);       /* Pad to 8 */                  out_uint8p(s, g_rdpdr_device[i].name, 8);
236          out_uint32(s, 0);  
237  #endif                  switch (g_rdpdr_device[i].device_type)
238  #if 0                  {
239          out_uint32_le(s, 0x2);  /* Device type 0x2 - parallel */                          case DEVICE_TYPE_PRINTER:
240          out_uint32_le(s, 0);                                  printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
241          out_uint8p(s, "LPT2", 4);  
242          out_uint8s(s, 4);                                  driverlen = 2 * strlen(printerinfo->driver) + 2;
243          out_uint32(s, 0);                                  printerlen = 2 * strlen(printerinfo->printer) + 2;
244  #endif                                  bloblen = printerinfo->bloblen;
245  #if 1  
246          out_uint32_le(s, 0x4);  /* Device type 0x4 - printer */                                  out_uint32_le(s, 24 + driverlen + printerlen + bloblen);        /* length of extra info */
247          out_uint32_le(s, 1);                                  out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
248          out_uint8p(s, "PRN1", 4);                                  out_uint8s(s, 8);       /* unknown */
249          out_uint8s(s, 4);                                  out_uint32_le(s, driverlen);
250          out_uint32_le(s, 24 + driverlen + printerlen);  /* length of extra info */                                  out_uint32_le(s, printerlen);
251          out_uint32_le(s, 2);    /* unknown */                                  out_uint32_le(s, bloblen);
252          out_uint8s(s, 8);       /* unknown */                                  rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
253          out_uint32_le(s, driverlen);    /* length of driver name */                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
254          out_uint32_le(s, printerlen);   /* length of printer name */                                  out_uint8a(s, printerinfo->blob, bloblen);
255          out_uint32(s, 0);       /* unknown */  
256          rdp_out_unistr(s, driver, driverlen - 2);                                  if (printerinfo->blob)
257          rdp_out_unistr(s, printer, printerlen - 2);                                          xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
258  #endif                                  break;
259  #if 0                          default:
260          out_uint32_le(s, 0x8);  /* Device type 0x8 - disk */                                  out_uint32(s, 0);
261          out_uint32_le(s, 0);                  }
262          out_uint8p(s, "Z:", 2);          }
         out_uint8s(s, 6);  
         out_uint32(s, 0);  
 #endif  
263  #if 0  #if 0
264          out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */          out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */
265          out_uint32_le(s, 0);          out_uint32_le(s, 0);
# Line 121  rdpdr_send_completion(uint32 device, uin Line 287  rdpdr_send_completion(uint32 device, uin
287          out_uint32_le(s, result);          out_uint32_le(s, result);
288          out_uint8p(s, buffer, length);          out_uint8p(s, buffer, length);
289          s_mark_end(s);          s_mark_end(s);
290          hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8);          /* JIF
291               hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
292          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
293  }  }
294    
295  static void  static void
296  rdpdr_process_irp(STREAM s)  rdpdr_process_irp(STREAM s)
297  {  {
298          uint32 device, file, id, major, minor;          uint32 result = 0,
299          NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;                  length = 0,
300          uint32 result = 0, length, request, bytes_in, bytes_out;                  desired_access = 0,
301          uint8 buffer[256];                  request,
302          uint32 buffer_len = 1;                  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          struct stream out;          struct stream out;
318          DEVICE_FNS *fns;          DEVICE_FNS *fns;
319            BOOL rw_blocking = True;
320            NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;
321    
322          in_uint32_le(s, device);          in_uint32_le(s, device);
323          in_uint32_le(s, file);          in_uint32_le(s, file);
# Line 142  rdpdr_process_irp(STREAM s) Line 325  rdpdr_process_irp(STREAM s)
325          in_uint32_le(s, major);          in_uint32_le(s, major);
326          in_uint32_le(s, minor);          in_uint32_le(s, minor);
327    
328          memset(buffer, 0, sizeof(buffer));          buffer_len = 0;
329            buffer = (uint8 *) xmalloc(1024);
330            buffer[0] = 0;
331    
332          /* FIXME: this should probably be a more dynamic mapping */          switch (g_rdpdr_device[device].device_type)
         switch (device)  
333          {          {
334                  case 0:                  case DEVICE_TYPE_SERIAL:
335    
336                          fns = &serial_fns;                          fns = &serial_fns;
337                  case 1:                          rw_blocking = False;
338                            break;
339    
340                    case DEVICE_TYPE_PARALLEL:
341    
342                            fns = &parallel_fns;
343                            rw_blocking = False;
344                            break;
345    
346                    case DEVICE_TYPE_PRINTER:
347    
348                          fns = &printer_fns;                          fns = &printer_fns;
349                            break;
350    
351                    case DEVICE_TYPE_DISK:
352    
353                            fns = &disk_fns;
354                            rw_blocking = False;
355                            break;
356    
357                    case DEVICE_TYPE_SCARD:
358                  default:                  default:
359    
360                          error("IRP for bad device %ld\n", device);                          error("IRP for bad device %ld\n", device);
361                          return;                          return;
362          }          }
# Line 159  rdpdr_process_irp(STREAM s) Line 364  rdpdr_process_irp(STREAM s)
364          switch (major)          switch (major)
365          {          {
366                  case IRP_MJ_CREATE:                  case IRP_MJ_CREATE:
367                          if (fns->create)  
368                                  status = fns->create(&result);                          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                          break;                          break;
396    
397                  case IRP_MJ_CLOSE:                  case IRP_MJ_CLOSE:
398                          if (fns->close)                          if (!fns->close)
399                                  status = fns->close(file);                          {
400                                    status = STATUS_NOT_SUPPORTED;
401                                    break;
402                            }
403    
404                            status = fns->close(file);
405                          break;                          break;
406    
407                  case IRP_MJ_READ:                  case IRP_MJ_READ:
408                          if (fns->read)  
409                            if (!fns->read)
410                            {
411                                    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                            if (!rdpdr_handle_ok(device, file))
421                            {
422                                    status = STATUS_INVALID_HANDLE;
423                                    break;
424                            }
425    
426                            if (rw_blocking)        // Complete read immediately
427                          {                          {
428                                  if (length > sizeof(buffer))                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
429                                          length = sizeof(buffer);                                  if (!buffer)
430                                  status = fns->read(file, buffer, length, &result);                                  {
431                                            status = STATUS_CANCELLED;
432                                            break;
433                                    }
434                                    status = fns->read(file, buffer, length, offset, &result);
435                                  buffer_len = result;                                  buffer_len = result;
436                                    break;
437                            }
438    
439                            // Add request to table
440                            pst_buf = (uint8 *) xmalloc(length);
441                            if (!pst_buf)
442                            {
443                                    status = STATUS_CANCELLED;
444                                    break;
445                            }
446                            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                                 pst_buf, offset))
450                            {
451                                    status = STATUS_PENDING;
452                                    break;
453                          }                          }
                         break;  
454    
455                            status = STATUS_CANCELLED;
456                            break;
457                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
458                          if (fns->write)  
459                                  status = fns->write(file, s->p, length, &result);                          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                            if (!rdpdr_handle_ok(device, file))
474                            {
475                                    status = STATUS_INVALID_HANDLE;
476                                    break;
477                            }
478    
479                            if (rw_blocking)        // Complete immediately
480                            {
481                                    status = fns->write(file, s->p, length, offset, &result);
482                                    break;
483                            }
484    
485                            // Add to table
486                            pst_buf = (uint8 *) xmalloc(length);
487                            if (!pst_buf)
488                            {
489                                    status = STATUS_CANCELLED;
490                                    break;
491                            }
492    
493                            in_uint8a(s, pst_buf, length);
494    
495                            if (add_async_iorequest
496                                (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
497                            {
498                                    status = STATUS_PENDING;
499                                    break;
500                            }
501    
502                            status = STATUS_CANCELLED;
503                            break;
504    
505                    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;                          break;
601    
602                  case IRP_MJ_DEVICE_CONTROL:                  case IRP_MJ_DEVICE_CONTROL:
603                          if (fns->device_control)  
604                            if (!fns->device_control)
605                            {
606                                    status = STATUS_NOT_SUPPORTED;
607                                    break;
608                            }
609    
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                            if (!buffer)
617                          {                          {
618                                  in_uint32_le(s, bytes_out);                                  status = STATUS_CANCELLED;
619                                  in_uint32_le(s, bytes_in);                                  break;
                                 in_uint32_le(s, request);  
                                 in_uint8s(s, 0x14);  
                                 out.data = out.p = buffer;  
                                 out.size = sizeof(buffer);  
                                 status = fns->device_control(file, request, s, &out);  
                                 result = buffer_len = out.p - out.data;  
620                          }                          }
621    
622                            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                          break;                          break;
627    
628                  default:                  default:
# Line 202  rdpdr_process_irp(STREAM s) Line 630  rdpdr_process_irp(STREAM s)
630                          break;                          break;
631          }          }
632    
633          rdpdr_send_completion(device, id, status, result, buffer, buffer_len);          if (status != STATUS_PENDING)
634            {
635                    rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
636            }
637            if (buffer)
638                    xfree(buffer);
639            buffer = NULL;
640    }
641    
642    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  static void  static void
683  rdpdr_process(STREAM s)  rdpdr_process(STREAM s)
684  {  {
685          uint32 handle;          uint32 handle;
686          char *magic;          uint8 *magic;
687    
688          printf("rdpdr_process\n");  #if WITH_DEBUG_RDP5
689            printf("--- rdpdr_process ---\n");
690          hexdump(s->p, s->end - s->p);          hexdump(s->p, s->end - s->p);
691    #endif
692          in_uint8p(s, magic, 4);          in_uint8p(s, magic, 4);
693    
694          if ((magic[0] == 'r') && (magic[1] == 'D'))          if ((magic[0] == 'r') && (magic[1] == 'D'))
# Line 226  rdpdr_process(STREAM s) Line 702  rdpdr_process(STREAM s)
702                  {                  {
703                          rdpdr_send_connect();                          rdpdr_send_connect();
704                          rdpdr_send_name();                          rdpdr_send_name();
                         rdpdr_send_available();  
705                          return;                          return;
706                  }                  }
707                  else if ((magic[2] == 'C') && (magic[3] == 'C'))                  if ((magic[2] == 'C') && (magic[3] == 'C'))
708                  {                  {
709                          /* connect from server */                          /* connect from server */
710                            rdpdr_send_clientcapabilty();
711                            rdpdr_send_available();
712                          return;                          return;
713                  }                  }
714                  else if ((magic[2] == 'r') && (magic[3] == 'd'))                  if ((magic[2] == 'r') && (magic[3] == 'd'))
715                  {                  {
716                          /* connect to a specific resource */                          /* connect to a specific resource */
717                          in_uint32(s, handle);                          in_uint32(s, handle);
718                          printf("Server connected to resource %d\n", handle);  #if WITH_DEBUG_RDP5
719                            DEBUG(("RDPDR: Server connected to resource %d\n", handle));
720    #endif
721                            return;
722                    }
723                    if ((magic[2] == 'P') && (magic[3] == 'S'))
724                    {
725                            /* server capability */
726                            return;
727                    }
728            }
729            if ((magic[0] == 'R') && (magic[1] == 'P'))
730            {
731                    if ((magic[2] == 'C') && (magic[3] == 'P'))
732                    {
733                            printercache_process(s);
734                          return;                          return;
735                  }                  }
736          }          }
# Line 246  rdpdr_process(STREAM s) Line 738  rdpdr_process(STREAM s)
738  }  }
739    
740  BOOL  BOOL
741  rdpdr_init(void)  rdpdr_init()
742  {  {
743          rdpdr_channel =          if (g_num_devices > 0)
744                  channel_register("rdpdr", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,          {
745                                   rdpdr_process);                  rdpdr_channel =
746                            channel_register("rdpdr",
747                                             CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
748                                             rdpdr_process);
749            }
750    
751          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
752  }  }
753    
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            uint32 select_timeout = 0;      // Timeout value to be used for select() (in millisecons).
759            struct async_iorequest *iorq;
760    
761            iorq = g_iorequest;
762            while (iorq != NULL)
763            {
764                    if (iorq->fd != 0)
765                    {
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    
794                    iorq = iorq->next;
795            }
796    }
797    
798    struct async_iorequest *
799    rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
800    {
801            if (!iorq)
802                    return NULL;
803    
804            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    /* 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            uint32 result = 0;
828            DEVICE_FNS *fns;
829            struct async_iorequest *iorq;
830            struct async_iorequest *prev;
831            uint32 req_size = 0;
832    
833            if (timed_out)
834            {
835                    rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);
836                    return;
837            }
838    
839            iorq = g_iorequest;
840            prev = NULL;
841            while (iorq != NULL)
842            {
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                                                    /* Read the data */
851                                                    fns = iorq->fns;
852    
853                                                    req_size =
854                                                            (iorq->length - iorq->partial_len) >
855                                                            8192 ? 8192 : (iorq->length -
856                                                                           iorq->partial_len);
857                                                    /* never read larger chunks than 8k - chances are that it will block */
858                                                    status = fns->read(iorq->fd,
859                                                                       iorq->buffer + iorq->partial_len,
860                                                                       req_size, iorq->offset, &result);
861    
862                                                    if (result > 0)
863                                                    {
864                                                            iorq->partial_len += result;
865                                                            iorq->offset += result;
866                                                    }
867    #if WITH_DEBUG_RDP5
868                                                    DEBUG(("RDPDR: %d bytes of data read\n", result));
869    #endif
870                                                    /* only delete link if all data has been transfered */
871                                                    /* or if result was 0 and status success - EOF      */
872                                                    if ((iorq->partial_len == iorq->length) ||
873                                                        (result == 0))
874                                                    {
875    #if WITH_DEBUG_RDP5
876                                                            DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
877    #endif
878                                                            rdpdr_send_completion(iorq->device,
879                                                                                  iorq->id, status,
880                                                                                  iorq->partial_len,
881                                                                                  iorq->buffer,
882                                                                                  iorq->partial_len);
883                                                            iorq = rdpdr_remove_iorequest(prev, iorq);
884                                                    }
885                                            }
886                                            break;
887                                    case IRP_MJ_WRITE:
888                                            if (FD_ISSET(iorq->fd, wfds))
889                                            {
890                                                    /* Write data. */
891                                                    fns = iorq->fns;
892    
893                                                    req_size =
894                                                            (iorq->length - iorq->partial_len) >
895                                                            8192 ? 8192 : (iorq->length -
896                                                                           iorq->partial_len);
897    
898                                                    /* never write larger chunks than 8k - chances are that it will block */
899                                                    status = fns->write(iorq->fd,
900                                                                        iorq->buffer +
901                                                                        iorq->partial_len, req_size,
902                                                                        iorq->offset, &result);
903    
904                                                    if (result > 0)
905                                                    {
906                                                            iorq->partial_len += result;
907                                                            iorq->offset += result;
908                                                    }
909    
910    #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                                                    /* or we couldn't write */
916                                                    if ((iorq->partial_len == iorq->length)
917                                                        || (result == 0))
918                                                    {
919    #if WITH_DEBUG_RDP5
920                                                            DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
921    #endif
922                                                            rdpdr_send_completion(iorq->device,
923                                                                                  iorq->id, status,
924                                                                                  iorq->partial_len,
925                                                                                  (uint8 *) "", 1);
926    
927                                                            iorq = rdpdr_remove_iorequest(prev, iorq);
928                                                    }
929                                            }
930                                            break;
931                            }
932    
933                    }
934                    prev = iorq;
935                    if (iorq)
936                            iorq = iorq->next;
937            }
938    
939    }
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            struct async_iorequest *prev;
948    
949            iorq = g_iorequest;
950            prev = NULL;
951            while (iorq != NULL)
952            {
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                            rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
959                                                  1);
960    
961                            iorq = rdpdr_remove_iorequest(prev, iorq);
962                            return True;
963                    }
964    
965                    prev = iorq;
966                    iorq = iorq->next;
967            }
968    
969            return False;
970    }

Legend:
Removed from v.435  
changed lines
  Added in v.646

  ViewVC Help
Powered by ViewVC 1.1.26