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

Legend:
Removed from v.555  
changed lines
  Added in v.569

  ViewVC Help
Powered by ViewVC 1.1.26