/[rdesktop]/jpeg/rdesktop/trunk/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 /jpeg/rdesktop/trunk/rdpdr.c

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

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

Legend:
Removed from v.432  
changed lines
  Added in v.590

  ViewVC Help
Powered by ViewVC 1.1.26