/[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 536 by matthewc, Fri Oct 31 04:29:57 2003 UTC revision 576 by stargo, Thu Jan 22 20:31:59 2004 UTC
# Line 1  Line 1 
1    #include <unistd.h>
2    #include <sys/types.h>
3    #include <time.h>
4  #include "rdesktop.h"  #include "rdesktop.h"
5    
6  #define IRP_MJ_CREATE           0x00  #define IRP_MJ_CREATE           0x00
# Line 6  Line 9 
9  #define IRP_MJ_WRITE            0x04  #define IRP_MJ_WRITE            0x04
10  #define IRP_MJ_DEVICE_CONTROL   0x0e  #define IRP_MJ_DEVICE_CONTROL   0x0e
11    
12    #define IRP_MJ_CREATE                   0x00
13    #define IRP_MJ_CLOSE                    0x02
14    #define IRP_MJ_READ                     0x03
15    #define IRP_MJ_WRITE                    0x04
16    #define IRP_MJ_QUERY_INFORMATION        0x05
17    #define IRP_MJ_SET_INFORMATION          0x06
18    #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
19    #define IRP_MJ_DIRECTORY_CONTROL        0x0c
20    #define IRP_MJ_DEVICE_CONTROL           0x0e
21    
22    #define IRP_MN_QUERY_DIRECTORY          0x01
23    #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02
24    
25    #define MAX_ASYNC_IO_REQUESTS   10
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    
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    
43    /* Used to store incoming io request, until they are ready to be completed */
44    struct async_iorequest
45    {
46            uint32 fd, major, minor, offset, device, id, length;
47            long timeout,           /* Total timeout */
48              itv_timeout;          /* Interval timeout (between serial characters) */
49            uint8 *buffer;
50            DEVICE_FNS *fns;
51    } g_iorequest[MAX_ASYNC_IO_REQUESTS];
52    
53    /* Return device_id for a given handle */
54    int
55    get_device_index(HANDLE handle)
56    {
57            int i;
58            for (i = 0; i < RDPDR_MAX_DEVICES; i++)
59            {
60                    if (g_rdpdr_device[i].handle == handle)
61                            return i;
62            }
63            return -1;
64    }
65    
66    /* Converts a windows path to a unix path */
67    void
68    convert_to_unix_filename(char *filename)
69    {
70            char *p;
71    
72            while ((p = strchr(filename, '\\')))
73            {
74                    *p = '/';
75            }
76    }
77    
78    /* Add a new io request to the table containing pending io requests so it won't block rdesktop */
79    BOOL
80    add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,
81                        DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer)
82    {
83            int i;
84            struct async_iorequest *iorq;
85    
86            for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)
87            {
88                    iorq = &g_iorequest[i];
89    
90                    if (iorq->fd == 0)
91                    {
92                            iorq->device = device;
93                            iorq->fd = file;
94                            iorq->id = id;
95                            iorq->major = major;
96                            iorq->length = length;
97                            iorq->fns = fns;
98                            iorq->timeout = total_timeout;
99                            iorq->itv_timeout = interval_timeout;
100                            iorq->buffer = buffer;
101                            return True;
102                    }
103            }
104            error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n");
105            return False;
106    }
107    
108    
109  void  void
110  rdpdr_send_connect(void)  rdpdr_send_connect(void)
111  {  {
# Line 27  rdpdr_send_connect(void) Line 121  rdpdr_send_connect(void)
121          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
122  }  }
123    
124    
125  void  void
126  rdpdr_send_name(void)  rdpdr_send_name(void)
127  {  {
# Line 45  rdpdr_send_name(void) Line 140  rdpdr_send_name(void)
140          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
141  }  }
142    
143    /* Returns the size of the payload of the announce packet */
144    int
145    announcedata_size()
146    {
147            int size, i;
148            PRINTER *printerinfo;
149    
150            size = 8;               //static announce size
151            size += g_num_devices * 0x14;
152    
153            for (i = 0; i < g_num_devices; i++)
154            {
155                    if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)
156                    {
157                            printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
158                            printerinfo->bloblen =
159                                    printercache_load_blob(printerinfo->printer, &(printerinfo->blob));
160    
161                            size += 0x18;
162                            size += 2 * strlen(printerinfo->driver) + 2;
163                            size += 2 * strlen(printerinfo->printer) + 2;
164                            size += printerinfo->bloblen;
165                    }
166            }
167    
168            return size;
169    }
170    
171  void  void
172  rdpdr_send_available(void)  rdpdr_send_available(void)
173  {  {
174    
175          uint8 magic[4] = "rDAD";          uint8 magic[4] = "rDAD";
176          char *driver = "Digital turbo PrintServer 20";  /* Fairly generic PostScript driver */          uint32 driverlen, printerlen, bloblen;
177          char *printer = "PostScript";          int i;
         uint32 driverlen = (strlen(driver) + 1) * 2;  
         uint32 printerlen = (strlen(printer) + 1) * 2;  
178          STREAM s;          STREAM s;
179            PRINTER *printerinfo;
180    
181          s = channel_init(rdpdr_channel, 8 + 20);          s = channel_init(rdpdr_channel, announcedata_size());
182          out_uint8a(s, magic, 4);          out_uint8a(s, magic, 4);
183          out_uint32_le(s, 1);    /* Number of devices */          out_uint32_le(s, g_num_devices);
184    
185  #if 1          for (i = 0; i < g_num_devices; i++)
186          out_uint32_le(s, 0x1);  /* Device type 0x1 - serial */          {
187          out_uint32_le(s, 0);    /* Handle */                  out_uint32_le(s, g_rdpdr_device[i].device_type);
188          out_uint8p(s, "COM2", 4);                  out_uint32_le(s, i);    /* RDP Device ID */
189          out_uint8s(s, 4);       /* Pad to 8 */                  out_uint8p(s, g_rdpdr_device[i].name, 8);
190          out_uint32(s, 0);  
191  #endif                  if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)
192  #if 0                  {
193          out_uint32_le(s, 0x2);  /* Device type 0x2 - parallel */                          printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
194          out_uint32_le(s, 0);  
195          out_uint8p(s, "LPT2", 4);                          driverlen = 2 * strlen(printerinfo->driver) + 2;
196          out_uint8s(s, 4);                          printerlen = 2 * strlen(printerinfo->printer) + 2;
197          out_uint32(s, 0);                          bloblen = printerinfo->bloblen;
198  #endif  
199  #if 1                          out_uint32_le(s, 24 + driverlen + printerlen + bloblen);        /* length of extra info */
200          out_uint32_le(s, 0x4);  /* Device type 0x4 - printer */                          out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
201          out_uint32_le(s, 1);                          out_uint8s(s, 8);       /* unknown */
202          out_uint8p(s, "PRN1", 4);                          out_uint32_le(s, driverlen);
203          out_uint8s(s, 4);                          out_uint32_le(s, printerlen);
204          out_uint32_le(s, 24 + driverlen + printerlen);  /* length of extra info */                          out_uint32_le(s, bloblen);
205          out_uint32_le(s, 2);    /* unknown */                          rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
206          out_uint8s(s, 8);       /* unknown */                          rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
207          out_uint32_le(s, driverlen);    /* length of driver name */                          out_uint8a(s, printerinfo->blob, bloblen);
208          out_uint32_le(s, printerlen);   /* length of printer name */  
209          out_uint32(s, 0);       /* unknown */                          xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
210          rdp_out_unistr(s, driver, driverlen - 2);                  }
211          rdp_out_unistr(s, printer, printerlen - 2);                  else
212  #endif                  {
213  #if 0                          out_uint32(s, 0);
214          out_uint32_le(s, 0x8);  /* Device type 0x8 - disk */                  }
215          out_uint32_le(s, 0);          }
         out_uint8p(s, "Z:", 2);  
         out_uint8s(s, 6);  
         out_uint32(s, 0);  
 #endif  
216  #if 0  #if 0
217          out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */          out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */
218          out_uint32_le(s, 0);          out_uint32_le(s, 0);
# Line 121  rdpdr_send_completion(uint32 device, uin Line 240  rdpdr_send_completion(uint32 device, uin
240          out_uint32_le(s, result);          out_uint32_le(s, result);
241          out_uint8p(s, buffer, length);          out_uint8p(s, buffer, length);
242          s_mark_end(s);          s_mark_end(s);
243          hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8);          /* JIF
244               hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
245          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
246  }  }
247    
248  static void  static void
249  rdpdr_process_irp(STREAM s)  rdpdr_process_irp(STREAM s)
250  {  {
251          uint32 device, file, id, major, minor;          uint32 result = 0,
252          NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;                  length = 0,
253          uint32 result = 0, length, request, bytes_in, bytes_out;                  desired_access = 0,
254          uint8 buffer[256];                  request,
255          uint32 buffer_len = 1;                  file,
256                    info_level,
257                    buffer_len,
258                    id,
259                    major,
260                    minor,
261                    device,
262                    offset,
263                    bytes_in,
264                    bytes_out,
265                    error_mode,
266                    share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
267    
268            char filename[256];
269            uint8 *buffer, *pst_buf;
270          struct stream out;          struct stream out;
271          DEVICE_FNS *fns;          DEVICE_FNS *fns;
272            BOOL rw_blocking = True;
273            NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;
274    
275          in_uint32_le(s, device);          in_uint32_le(s, device);
276          in_uint32_le(s, file);          in_uint32_le(s, file);
# Line 142  rdpdr_process_irp(STREAM s) Line 278  rdpdr_process_irp(STREAM s)
278          in_uint32_le(s, major);          in_uint32_le(s, major);
279          in_uint32_le(s, minor);          in_uint32_le(s, minor);
280    
281          memset(buffer, 0, sizeof(buffer));          buffer_len = 0;
282            buffer = (uint8 *) xmalloc(1024);
283            buffer[0] = 0;
284    
285    
286          /* FIXME: this should probably be a more dynamic mapping */          switch (g_rdpdr_device[device].device_type)
         switch (device)  
287          {          {
288                  case 0:                  case DEVICE_TYPE_SERIAL:
289    
290                          fns = &serial_fns;                          fns = &serial_fns;
291                  case 1:                          rw_blocking = False;
292                            break;
293    
294                    case DEVICE_TYPE_PARALLEL:
295    
296                            fns = &parallel_fns;
297                            rw_blocking = False;
298                            break;
299    
300                    case DEVICE_TYPE_PRINTER:
301    
302                          fns = &printer_fns;                          fns = &printer_fns;
303                            break;
304    
305                    case DEVICE_TYPE_DISK:
306    
307                            fns = &disk_fns;
308                            break;
309    
310                    case DEVICE_TYPE_SCARD:
311                  default:                  default:
312    
313                          error("IRP for bad device %ld\n", device);                          error("IRP for bad device %ld\n", device);
314                          return;                          return;
315          }          }
# Line 159  rdpdr_process_irp(STREAM s) Line 317  rdpdr_process_irp(STREAM s)
317          switch (major)          switch (major)
318          {          {
319                  case IRP_MJ_CREATE:                  case IRP_MJ_CREATE:
320                          if (fns->create)  
321                                  status = fns->create(&result);                          in_uint32_be(s, desired_access);
322                            in_uint8s(s, 0x08);     // unknown
323                            in_uint32_le(s, error_mode);
324                            in_uint32_le(s, share_mode);
325                            in_uint32_le(s, disposition);
326                            in_uint32_le(s, flags_and_attributes);
327                            in_uint32_le(s, length);
328    
329                            if (length && (length / 2) < 256)
330                            {
331                                    rdp_in_unistr(s, filename, length);
332                                    convert_to_unix_filename(filename);
333                            }
334                            else
335                            {
336                                    filename[0] = 0;
337                            }
338    
339                            if (!fns->create)
340                            {
341                                    status = STATUS_NOT_SUPPORTED;
342                                    break;
343                            }
344    
345                            status = fns->create(device, desired_access, share_mode, disposition,
346                                                 flags_and_attributes, filename, &result);
347                            buffer_len = 1;
348                          break;                          break;
349    
350                  case IRP_MJ_CLOSE:                  case IRP_MJ_CLOSE:
351                          if (fns->close)                          if (!fns->close)
352                                  status = fns->close(file);                          {
353                                    status = STATUS_NOT_SUPPORTED;
354                                    break;
355                            }
356    
357                            status = fns->close(file);
358                          break;                          break;
359    
360                  case IRP_MJ_READ:                  case IRP_MJ_READ:
361                          if (fns->read)  
362                            if (!fns->read)
363                          {                          {
364                                  if (length > sizeof(buffer))                                  status = STATUS_NOT_SUPPORTED;
365                                          length = sizeof(buffer);                                  break;
366                                  status = fns->read(file, buffer, length, &result);                          }
367    
368                            in_uint32_le(s, length);
369                            in_uint32_le(s, offset);
370    #if WITH_DEBUG_RDP5
371                            DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
372    #endif
373                            if (rw_blocking)        // Complete read immediately
374                            {
375                                    buffer = (uint8 *) xrealloc((void *) buffer, length);
376                                    status = fns->read(file, buffer, length, offset, &result);
377                                  buffer_len = result;                                  buffer_len = result;
378                                    break;
379                            }
380    
381                            // Add request to table
382                            pst_buf = (uint8 *) xmalloc(length);
383                            serial_get_timeout(file, length, &total_timeout, &interval_timeout);
384                            if (add_async_iorequest
385                                (device, file, id, major, length, fns, total_timeout, interval_timeout,
386                                 pst_buf))
387                            {
388                                    status = STATUS_PENDING;
389                                    break;
390                          }                          }
391    
392                            status = STATUS_CANCELLED;
393                          break;                          break;
394    
395                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
396                          if (fns->write)  
397                                  status = fns->write(file, s->p, length, &result);                          buffer_len = 1;
398    
399                            if (!fns->write)
400                            {
401                                    status = STATUS_NOT_SUPPORTED;
402                                    break;
403                            }
404    
405                            in_uint32_le(s, length);
406                            in_uint32_le(s, offset);
407                            in_uint8s(s, 0x18);
408    #if WITH_DEBUG_RDP5
409                            DEBUG(("RDPDR IRP Write (length: %d)\n", result));
410    #endif
411                            if (rw_blocking)        // Complete immediately
412                            {
413                                    status = fns->write(file, s->p, length, offset, &result);
414                                    break;
415                            }
416    
417                            // Add to table
418                            pst_buf = (uint8 *) xmalloc(length);
419                            in_uint8a(s, pst_buf, length);
420    
421                            if (add_async_iorequest
422                                (device, file, id, major, length, fns, 0, 0, pst_buf))
423                            {
424                                    status = STATUS_PENDING;
425                                    break;
426                            }
427    
428                            status = STATUS_CANCELLED;
429                            break;
430    
431                    case IRP_MJ_QUERY_INFORMATION:
432    
433                            if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
434                            {
435                                    status = STATUS_INVALID_HANDLE;
436                                    break;
437                            }
438                            in_uint32_le(s, info_level);
439    
440                            out.data = out.p = buffer;
441                            out.size = sizeof(buffer);
442                            status = disk_query_information(file, info_level, &out);
443                            result = buffer_len = out.p - out.data;
444    
445                            break;
446    
447                    case IRP_MJ_SET_INFORMATION:
448    
449                            if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
450                            {
451                                    status = STATUS_INVALID_HANDLE;
452                                    break;
453                            }
454    
455                            in_uint32_le(s, info_level);
456    
457                            out.data = out.p = buffer;
458                            out.size = sizeof(buffer);
459                            status = disk_set_information(file, info_level, s, &out);
460                            result = buffer_len = out.p - out.data;
461                            break;
462    
463                    case IRP_MJ_QUERY_VOLUME_INFORMATION:
464    
465                            if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
466                            {
467                                    status = STATUS_INVALID_HANDLE;
468                                    break;
469                            }
470    
471                            in_uint32_le(s, info_level);
472    
473                            out.data = out.p = buffer;
474                            out.size = sizeof(buffer);
475                            status = disk_query_volume_information(file, info_level, &out);
476                            result = buffer_len = out.p - out.data;
477                            break;
478    
479                    case IRP_MJ_DIRECTORY_CONTROL:
480    
481                            if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
482                            {
483                                    status = STATUS_INVALID_HANDLE;
484                                    break;
485                            }
486    
487                            switch (minor)
488                            {
489                                    case IRP_MN_QUERY_DIRECTORY:
490    
491                                            in_uint32_le(s, info_level);
492                                            in_uint8s(s, 1);
493                                            in_uint32_le(s, length);
494                                            in_uint8s(s, 0x17);
495                                            if (length && length < 2 * 255)
496                                            {
497                                                    rdp_in_unistr(s, filename, length);
498                                                    convert_to_unix_filename(filename);
499                                            }
500                                            else
501                                            {
502                                                    filename[0] = 0;
503                                            }
504                                            out.data = out.p = buffer;
505                                            out.size = sizeof(buffer);
506                                            status = disk_query_directory(file, info_level, filename,
507                                                                          &out);
508                                            result = buffer_len = out.p - out.data;
509                                            if (!buffer_len)
510                                                    buffer_len++;
511                                            break;
512    
513                                    case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
514    
515                                            /* JIF
516                                               unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
517                                            status = STATUS_PENDING;        // Don't send completion packet
518                                            break;
519    
520                                    default:
521    
522                                            status = STATUS_INVALID_PARAMETER;
523                                            /* JIF
524                                               unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */
525                            }
526                          break;                          break;
527    
528                  case IRP_MJ_DEVICE_CONTROL:                  case IRP_MJ_DEVICE_CONTROL:
529                          if (fns->device_control)  
530                            if (!fns->device_control)
531                          {                          {
532                                  in_uint32_le(s, bytes_out);                                  status = STATUS_NOT_SUPPORTED;
533                                  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;  
534                          }                          }
535    
536                            in_uint32_le(s, bytes_out);
537                            in_uint32_le(s, bytes_in);
538                            in_uint32_le(s, request);
539                            in_uint8s(s, 0x14);
540    
541                            buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
542                            out.data = out.p = buffer;
543                            out.size = sizeof(buffer);
544                            status = fns->device_control(file, request, s, &out);
545                            result = buffer_len = out.p - out.data;
546                          break;                          break;
547    
548                  default:                  default:
# Line 202  rdpdr_process_irp(STREAM s) Line 550  rdpdr_process_irp(STREAM s)
550                          break;                          break;
551          }          }
552    
553          rdpdr_send_completion(device, id, status, result, buffer, buffer_len);          if (status != STATUS_PENDING)
554            {
555                    rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
556            }
557            xfree(buffer);
558    
559    }
560    
561    void
562    rdpdr_send_clientcapabilty(void)
563    {
564            uint8 magic[4] = "rDPC";
565            STREAM s;
566    
567            s = channel_init(rdpdr_channel, 0x50);
568            out_uint8a(s, magic, 4);
569            out_uint32_le(s, 5);    /* count */
570            out_uint16_le(s, 1);    /* first */
571            out_uint16_le(s, 0x28); /* length */
572            out_uint32_le(s, 1);
573            out_uint32_le(s, 2);
574            out_uint16_le(s, 2);
575            out_uint16_le(s, 5);
576            out_uint16_le(s, 1);
577            out_uint16_le(s, 5);
578            out_uint16_le(s, 0xFFFF);
579            out_uint16_le(s, 0);
580            out_uint32_le(s, 0);
581            out_uint32_le(s, 3);
582            out_uint32_le(s, 0);
583            out_uint32_le(s, 0);
584            out_uint16_le(s, 2);    /* second */
585            out_uint16_le(s, 8);    /* length */
586            out_uint32_le(s, 1);
587            out_uint16_le(s, 3);    /* third */
588            out_uint16_le(s, 8);    /* length */
589            out_uint32_le(s, 1);
590            out_uint16_le(s, 4);    /* fourth */
591            out_uint16_le(s, 8);    /* length */
592            out_uint32_le(s, 1);
593            out_uint16_le(s, 5);    /* fifth */
594            out_uint16_le(s, 8);    /* length */
595            out_uint32_le(s, 1);
596    
597            s_mark_end(s);
598            channel_send(s, rdpdr_channel);
599  }  }
600    
601  static void  static void
# Line 211  rdpdr_process(STREAM s) Line 604  rdpdr_process(STREAM s)
604          uint32 handle;          uint32 handle;
605          uint8 *magic;          uint8 *magic;
606    
607          printf("rdpdr_process\n");  #if WITH_DEBUG_RDP5
608            printf("--- rdpdr_process ---\n");
609          hexdump(s->p, s->end - s->p);          hexdump(s->p, s->end - s->p);
610    #endif
611          in_uint8p(s, magic, 4);          in_uint8p(s, magic, 4);
612    
613          if ((magic[0] == 'r') && (magic[1] == 'D'))          if ((magic[0] == 'r') && (magic[1] == 'D'))
# Line 226  rdpdr_process(STREAM s) Line 621  rdpdr_process(STREAM s)
621                  {                  {
622                          rdpdr_send_connect();                          rdpdr_send_connect();
623                          rdpdr_send_name();                          rdpdr_send_name();
                         rdpdr_send_available();  
624                          return;                          return;
625                  }                  }
626                  else if ((magic[2] == 'C') && (magic[3] == 'C'))                  if ((magic[2] == 'C') && (magic[3] == 'C'))
627                  {                  {
628                          /* connect from server */                          /* connect from server */
629                            rdpdr_send_clientcapabilty();
630                            rdpdr_send_available();
631                          return;                          return;
632                  }                  }
633                  else if ((magic[2] == 'r') && (magic[3] == 'd'))                  if ((magic[2] == 'r') && (magic[3] == 'd'))
634                  {                  {
635                          /* connect to a specific resource */                          /* connect to a specific resource */
636                          in_uint32(s, handle);                          in_uint32(s, handle);
637                          printf("Server connected to resource %d\n", handle);  #if WITH_DEBUG_RDP5
638                            DEBUG(("RDPDR: Server connected to resource %d\n", handle));
639    #endif
640                            return;
641                    }
642                    if ((magic[2] == 'P') && (magic[3] == 'S'))
643                    {
644                            /* server capability */
645                            return;
646                    }
647            }
648            if ((magic[0] == 'R') && (magic[1] == 'P'))
649            {
650                    if ((magic[2] == 'C') && (magic[3] == 'P'))
651                    {
652                            printercache_process(s);
653                          return;                          return;
654                  }                  }
655          }          }
# Line 246  rdpdr_process(STREAM s) Line 657  rdpdr_process(STREAM s)
657  }  }
658    
659  BOOL  BOOL
660  rdpdr_init(void)  rdpdr_init()
661  {  {
662          rdpdr_channel =          if (g_num_devices > 0)
663                  channel_register("rdpdr", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,          {
664                                   rdpdr_process);                  rdpdr_channel = channel_register("rdpdr", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP, rdpdr_process);
665            }
666    
667          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
668  }  }
669    
670    /* Add file descriptors of pending io request to select() */
671    void
672    rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout)
673    {
674            int i;
675            long select_timeout = 0;        // Timeout value to be used for select() (in millisecons).
676            struct async_iorequest *iorq;
677    
678            for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)
679            {
680                    iorq = &g_iorequest[i];
681    
682                    if (iorq->fd != 0)      // Found a pending io request
683                    {
684                            switch (iorq->major)
685                            {
686                                    case IRP_MJ_READ:
687    
688                                            FD_SET(iorq->fd, rfds);
689    
690                                            // Check if io request timeout is smaller than current (but not 0).
691                                            if (iorq->timeout
692                                                && (select_timeout == 0
693                                                    || iorq->timeout < select_timeout))
694                                            {
695                                                    // Set new timeout
696                                                    select_timeout = iorq->timeout;
697                                                    g_min_timeout_fd = iorq->fd;    /* Remember fd */
698                                                    tv->tv_sec = select_timeout / 1000;
699                                                    tv->tv_usec = (select_timeout % 1000) * 1000;
700                                                    *timeout = True;
701                                            }
702                                            break;
703    
704                                    case IRP_MJ_WRITE:
705    
706                                            FD_SET(iorq->fd, wfds);
707                                            break;
708    
709                            }
710                            *n = MAX(*n, iorq->fd);
711                    }
712            }
713    }
714    
715    /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
716    void
717    rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
718    {
719            int i;
720            NTSTATUS status;
721            uint32 result = 0, buffer_len = 0;
722            DEVICE_FNS *fns;
723            struct async_iorequest *iorq;
724    
725            if (timed_out)
726            {
727                    rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);
728                    return;
729            }
730    
731            // Walk through array of pending io_rq's
732            for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)
733            {
734                    iorq = &g_iorequest[i];
735    
736                    if (iorq->fd != 0)
737                    {
738                            switch (iorq->major)
739                            {
740    
741                                    case IRP_MJ_READ:
742    
743                                            if (FD_ISSET(iorq->fd, rfds))
744                                            {
745                                                    // Read, and send data.
746                                                    fns = iorq->fns;
747                                                    status = fns->read(iorq->fd, iorq->buffer,
748                                                                       iorq->length, 0, &result);
749                                                    buffer_len = result;
750    
751                                                    rdpdr_send_completion(iorq->device, iorq->id,
752                                                                          status, result, iorq->buffer,
753                                                                          buffer_len);
754    #if WITH_DEBUG_RDP5
755                                                    DEBUG(("RDPDR: %d bytes of data read\n", result));
756    #endif
757                                                    xfree(iorq->buffer);
758                                                    iorq->fd = 0;
759                                            }
760                                            break;
761    
762                                    case IRP_MJ_WRITE:
763    
764                                            if (FD_ISSET(iorq->fd, wfds))
765                                            {
766                                                    // Write data and send completion.
767                                                    fns = iorq->fns;
768                                                    status = fns->write(iorq->fd, iorq->buffer,
769                                                                        iorq->length, 0, &result);
770                                                    rdpdr_send_completion(iorq->device, iorq->id,
771                                                                          status, result, "", 1);
772    
773                                                    xfree(iorq->buffer);
774                                                    iorq->fd = 0;
775                                            }
776                                            break;
777                            }
778                    }
779            }
780    }
781    
782    /* Abort a pending io request for a given handle and major */
783    BOOL
784    rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
785    {
786            uint32 result;
787            int i;
788            struct async_iorequest *iorq;
789    
790            for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)
791            {
792                    iorq = &g_iorequest[i];
793    
794                    // Only remove from table when major is not set, or when correct major is supplied.
795                    // Abort read should not abort a write io request.
796                    if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
797                    {
798                            result = 0;
799                            rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);
800                            xfree(iorq->buffer);
801                            iorq->fd = 0;
802                            return True;
803                    }
804            }
805            return False;
806    }

Legend:
Removed from v.536  
changed lines
  Added in v.576

  ViewVC Help
Powered by ViewVC 1.1.26