/[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 576 by stargo, Thu Jan 22 20:31:59 2004 UTC revision 612 by n-ki, Mon Feb 23 09:58:16 2004 UTC
# Line 1  Line 1 
1  #include <unistd.h>  #include <unistd.h>
2  #include <sys/types.h>  #include <sys/types.h>
3    #include <sys/time.h>
4  #include <time.h>  #include <time.h>
5  #include "rdesktop.h"  #include "rdesktop.h"
6    
# Line 22  Line 23 
23  #define IRP_MN_QUERY_DIRECTORY          0x01  #define IRP_MN_QUERY_DIRECTORY          0x01
24  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02
25    
 #define MAX_ASYNC_IO_REQUESTS   10  
   
26  extern char hostname[16];  extern char hostname[16];
27  extern DEVICE_FNS serial_fns;  extern DEVICE_FNS serial_fns;
28  extern DEVICE_FNS printer_fns;  extern DEVICE_FNS printer_fns;
29  extern DEVICE_FNS parallel_fns;  extern DEVICE_FNS parallel_fns;
30  extern DEVICE_FNS disk_fns;  extern DEVICE_FNS disk_fns;
31    
   
32  static VCHANNEL *rdpdr_channel;  static VCHANNEL *rdpdr_channel;
33    
34  /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */  /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
# Line 41  uint32 g_num_devices; Line 39  uint32 g_num_devices;
39  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
40    
41  /* Used to store incoming io request, until they are ready to be completed */  /* Used to store incoming io request, until they are ready to be completed */
42    /* using a linked list ensures that they are processed in the right order, */
43    /* if multiple ios are being done on the same fd */
44  struct async_iorequest  struct async_iorequest
45  {  {
46          uint32 fd, major, minor, offset, device, id, length;          uint32 fd, major, minor, offset, device, id, length, partial_len;
47          long timeout,           /* Total timeout */          long timeout,           /* Total timeout */
48            itv_timeout;          /* Interval timeout (between serial characters) */            itv_timeout;          /* Interval timeout (between serial characters) */
49          uint8 *buffer;          uint8 *buffer;
50          DEVICE_FNS *fns;          DEVICE_FNS *fns;
51  } g_iorequest[MAX_ASYNC_IO_REQUESTS];  
52            struct async_iorequest *next;   /* next element in list */
53    };
54    
55    struct async_iorequest *g_iorequest;
56    
57  /* Return device_id for a given handle */  /* Return device_id for a given handle */
58  int  int
# Line 78  convert_to_unix_filename(char *filename) Line 82  convert_to_unix_filename(char *filename)
82  /* Add a new io request to the table containing pending io requests so it won't block rdesktop */  /* Add a new io request to the table containing pending io requests so it won't block rdesktop */
83  BOOL  BOOL
84  add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,  add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,
85                      DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer)                      DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer)
86  {  {
         int i;  
87          struct async_iorequest *iorq;          struct async_iorequest *iorq;
88    
89          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          if (g_iorequest == NULL)
90          {          {
91                  iorq = &g_iorequest[i];                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
92                    if (!g_iorequest)
93                  if (iorq->fd == 0)                          return False;
94                  {                  g_iorequest->fd = 0;
95                          iorq->device = device;                  g_iorequest->next = NULL;
96                          iorq->fd = file;          }
97                          iorq->id = id;  
98                          iorq->major = major;          iorq = g_iorequest;
99                          iorq->length = length;  
100                          iorq->fns = fns;          while (iorq->fd != 0)
101                          iorq->timeout = total_timeout;          {
102                          iorq->itv_timeout = interval_timeout;                  // create new element if needed
103                          iorq->buffer = buffer;                  if (iorq->next == NULL)
104                          return True;                  {
105                  }                          iorq->next =
106          }                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
107          error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n");                          if (!iorq->next)
108          return False;                                  return False;
109                            iorq->next->fd = 0;
110                            iorq->next->next = NULL;
111                    }
112                    iorq = iorq->next;
113            }
114            iorq->device = device;
115            iorq->fd = file;
116            iorq->id = id;
117            iorq->major = major;
118            iorq->length = length;
119            iorq->partial_len = 0;
120            iorq->fns = fns;
121            iorq->timeout = total_timeout;
122            iorq->itv_timeout = interval_timeout;
123            iorq->buffer = buffer;
124            return True;
125  }  }
126    
   
127  void  void
128  rdpdr_send_connect(void)  rdpdr_send_connect(void)
129  {  {
# Line 188  rdpdr_send_available(void) Line 206  rdpdr_send_available(void)
206                  out_uint32_le(s, i);    /* RDP Device ID */                  out_uint32_le(s, i);    /* RDP Device ID */
207                  out_uint8p(s, g_rdpdr_device[i].name, 8);                  out_uint8p(s, g_rdpdr_device[i].name, 8);
208    
209                  if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)                  switch (g_rdpdr_device[i].device_type)
210                  {                  {
211                          printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;                          case DEVICE_TYPE_PRINTER:
212                                    printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
213    
214                          driverlen = 2 * strlen(printerinfo->driver) + 2;                                  driverlen = 2 * strlen(printerinfo->driver) + 2;
215                          printerlen = 2 * strlen(printerinfo->printer) + 2;                                  printerlen = 2 * strlen(printerinfo->printer) + 2;
216                          bloblen = printerinfo->bloblen;                                  bloblen = printerinfo->bloblen;
217    
218                          out_uint32_le(s, 24 + driverlen + printerlen + bloblen);        /* length of extra info */                                  out_uint32_le(s, 24 + driverlen + printerlen + bloblen);        /* length of extra info */
219                          out_uint32_le(s, printerinfo->default_printer ? 2 : 0);                                  out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
220                          out_uint8s(s, 8);       /* unknown */                                  out_uint8s(s, 8);       /* unknown */
221                          out_uint32_le(s, driverlen);                                  out_uint32_le(s, driverlen);
222                          out_uint32_le(s, printerlen);                                  out_uint32_le(s, printerlen);
223                          out_uint32_le(s, bloblen);                                  out_uint32_le(s, bloblen);
224                          rdp_out_unistr(s, printerinfo->driver, driverlen - 2);                                  rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
225                          rdp_out_unistr(s, printerinfo->printer, printerlen - 2);                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
226                          out_uint8a(s, printerinfo->blob, bloblen);                                  out_uint8a(s, printerinfo->blob, bloblen);
227    
228                          xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */                                  if (printerinfo->blob)
229                  }                                          xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
230                  else                                  break;
231                  {                          default:
232                          out_uint32(s, 0);                                  out_uint32(s, 0);
233                  }                  }
234          }          }
235  #if 0  #if 0
# Line 282  rdpdr_process_irp(STREAM s) Line 301  rdpdr_process_irp(STREAM s)
301          buffer = (uint8 *) xmalloc(1024);          buffer = (uint8 *) xmalloc(1024);
302          buffer[0] = 0;          buffer[0] = 0;
303    
   
304          switch (g_rdpdr_device[device].device_type)          switch (g_rdpdr_device[device].device_type)
305          {          {
306                  case DEVICE_TYPE_SERIAL:                  case DEVICE_TYPE_SERIAL:
# Line 305  rdpdr_process_irp(STREAM s) Line 323  rdpdr_process_irp(STREAM s)
323                  case DEVICE_TYPE_DISK:                  case DEVICE_TYPE_DISK:
324    
325                          fns = &disk_fns;                          fns = &disk_fns;
326                            rw_blocking = False;
327                          break;                          break;
328    
329                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
# Line 373  rdpdr_process_irp(STREAM s) Line 392  rdpdr_process_irp(STREAM s)
392                          if (rw_blocking)        // Complete read immediately                          if (rw_blocking)        // Complete read immediately
393                          {                          {
394                                  buffer = (uint8 *) xrealloc((void *) buffer, length);                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
395                                    if (!buffer)
396                                    {
397                                            status = STATUS_CANCELLED;
398                                            break;
399                                    }
400                                  status = fns->read(file, buffer, length, offset, &result);                                  status = fns->read(file, buffer, length, offset, &result);
401                                  buffer_len = result;                                  buffer_len = result;
402                                  break;                                  break;
# Line 380  rdpdr_process_irp(STREAM s) Line 404  rdpdr_process_irp(STREAM s)
404    
405                          // Add request to table                          // Add request to table
406                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
407                            if (!pst_buf)
408                            {
409                                    status = STATUS_CANCELLED;
410                                    break;
411                            }
412                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);
413                          if (add_async_iorequest                          if (add_async_iorequest
414                              (device, file, id, major, length, fns, total_timeout, interval_timeout,                              (device, file, id, major, length, fns, total_timeout, interval_timeout,
# Line 391  rdpdr_process_irp(STREAM s) Line 420  rdpdr_process_irp(STREAM s)
420    
421                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
422                          break;                          break;
   
423                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
424    
425                          buffer_len = 1;                          buffer_len = 1;
# Line 416  rdpdr_process_irp(STREAM s) Line 444  rdpdr_process_irp(STREAM s)
444    
445                          // Add to table                          // Add to table
446                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
447                            if (!pst_buf)
448                            {
449                                    status = STATUS_CANCELLED;
450                                    break;
451                            }
452    
453                          in_uint8a(s, pst_buf, length);                          in_uint8a(s, pst_buf, length);
454    
455                          if (add_async_iorequest                          if (add_async_iorequest
# Line 539  rdpdr_process_irp(STREAM s) Line 573  rdpdr_process_irp(STREAM s)
573                          in_uint8s(s, 0x14);                          in_uint8s(s, 0x14);
574    
575                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
576                            if (!buffer)
577                            {
578                                    status = STATUS_CANCELLED;
579                                    break;
580                            }
581    
582                          out.data = out.p = buffer;                          out.data = out.p = buffer;
583                          out.size = sizeof(buffer);                          out.size = sizeof(buffer);
584                          status = fns->device_control(file, request, s, &out);                          status = fns->device_control(file, request, s, &out);
# Line 554  rdpdr_process_irp(STREAM s) Line 594  rdpdr_process_irp(STREAM s)
594          {          {
595                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
596          }          }
597          xfree(buffer);          if (buffer)
598                    xfree(buffer);
599            buffer = NULL;
600  }  }
601    
602  void  void
# Line 661  rdpdr_init() Line 702  rdpdr_init()
702  {  {
703          if (g_num_devices > 0)          if (g_num_devices > 0)
704          {          {
705                  rdpdr_channel = channel_register("rdpdr", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP, rdpdr_process);                  rdpdr_channel =
706                            channel_register("rdpdr",
707                                             CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
708                                             rdpdr_process);
709          }          }
710    
711          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
# Line 671  rdpdr_init() Line 715  rdpdr_init()
715  void  void
716  rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout)  rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout)
717  {  {
718          int i;          uint32 select_timeout = 0;      // Timeout value to be used for select() (in millisecons).
         long select_timeout = 0;        // Timeout value to be used for select() (in millisecons).  
719          struct async_iorequest *iorq;          struct async_iorequest *iorq;
720    
721          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
722            while (iorq != NULL)
723          {          {
724                  iorq = &g_iorequest[i];                  if (iorq->fd != 0)
   
                 if (iorq->fd != 0)      // Found a pending io request  
725                  {                  {
726                          switch (iorq->major)                          switch (iorq->major)
727                          {                          {
# Line 702  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 744  rdpdr_add_fds(int *n, fd_set * rfds, fd_
744                                          break;                                          break;
745    
746                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
747                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
748                                          break;                                          break;
749    
750                          }                          }
751                          *n = MAX(*n, iorq->fd);                          *n = MAX(*n, iorq->fd);
752                  }                  }
753    
754                    iorq = iorq->next;
755          }          }
756  }  }
757    
758    
759  /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */  /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
760  void  void
761  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
762  {  {
         int i;  
763          NTSTATUS status;          NTSTATUS status;
764          uint32 result = 0, buffer_len = 0;          uint32 result = 0;
765          DEVICE_FNS *fns;          DEVICE_FNS *fns;
766          struct async_iorequest *iorq;          struct async_iorequest *iorq;
767            struct async_iorequest *prev;
768            uint32 req_size = 0;
769    
770          if (timed_out)          if (timed_out)
771          {          {
# Line 728  rdpdr_check_fds(fd_set * rfds, fd_set * Line 773  rdpdr_check_fds(fd_set * rfds, fd_set *
773                  return;                  return;
774          }          }
775    
776          // Walk through array of pending io_rq's          iorq = g_iorequest;
777          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          prev = NULL;
778            while (iorq != NULL)
779          {          {
                 iorq = &g_iorequest[i];  
   
780                  if (iorq->fd != 0)                  if (iorq->fd != 0)
781                  {                  {
782                          switch (iorq->major)                          switch (iorq->major)
783                          {                          {
   
784                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
   
785                                          if (FD_ISSET(iorq->fd, rfds))                                          if (FD_ISSET(iorq->fd, rfds))
786                                          {                                          {
787                                                  // Read, and send data.                                                  /* Read the data */
788                                                  fns = iorq->fns;                                                  fns = iorq->fns;
789                                                  status = fns->read(iorq->fd, iorq->buffer,  
790                                                                     iorq->length, 0, &result);                                                  req_size =
791                                                  buffer_len = result;                                                          (iorq->length - iorq->partial_len) >
792                                                            8192 ? 8192 : (iorq->length -
793                                                  rdpdr_send_completion(iorq->device, iorq->id,                                                                         iorq->partial_len);
794                                                                        status, result, iorq->buffer,                                                  /* never read larger chunks than 8k - chances are that it will block */
795                                                                        buffer_len);                                                  status = fns->read(iorq->fd,
796                                                                       iorq->buffer + iorq->partial_len,
797                                                                       req_size, 0, &result);
798                                                    iorq->partial_len += result;
799    
800  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
801                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
802  #endif  #endif
803                                                  xfree(iorq->buffer);                                                  /* only delete link if all data has been transfered */
804                                                  iorq->fd = 0;                                                  /* or if result was 0 and status success - EOF      */
805                                                    if ((iorq->partial_len == iorq->length) ||
806                                                        (result == 0))
807                                                    {
808    #if WITH_DEBUG_RDP5
809                                                            DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
810    #endif
811                                                            /* send the data */
812                                                            status = STATUS_SUCCESS;
813                                                            rdpdr_send_completion(iorq->device,
814                                                                                  iorq->id, status,
815                                                                                  iorq->partial_len,
816                                                                                  iorq->buffer,
817                                                                                  iorq->partial_len);
818                                                            xfree(iorq->buffer);
819                                                            iorq->fd = 0;
820                                                            if (prev != NULL)
821                                                            {
822                                                                    prev->next = iorq->next;
823                                                                    xfree(iorq);
824                                                            }
825                                                            else
826                                                            {
827                                                                    // Even if NULL
828                                                                    g_iorequest = iorq->next;
829                                                                    xfree(iorq);
830                                                            }
831                                                    }
832                                          }                                          }
833                                          break;                                          break;
   
834                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
835                                          if (FD_ISSET(iorq->fd, wfds))                                          if (FD_ISSET(iorq->fd, wfds))
836                                          {                                          {
837                                                  // Write data and send completion.                                                  /* Write data. */
838                                                  fns = iorq->fns;                                                  fns = iorq->fns;
                                                 status = fns->write(iorq->fd, iorq->buffer,  
                                                                     iorq->length, 0, &result);  
                                                 rdpdr_send_completion(iorq->device, iorq->id,  
                                                                       status, result, "", 1);  
839    
840                                                  xfree(iorq->buffer);                                                  req_size =
841                                                  iorq->fd = 0;                                                          (iorq->length - iorq->partial_len) >
842                                                            8192 ? 8192 : (iorq->length -
843                                                                           iorq->partial_len);
844    
845                                                    /* never write larger chunks than 8k - chances are that it will block */
846                                                    status = fns->write(iorq->fd,
847                                                                        iorq->buffer +
848                                                                        iorq->partial_len, req_size, 0,
849                                                                        &result);
850                                                    iorq->partial_len += result;
851    #if WITH_DEBUG_RDP5
852                                                    DEBUG(("RDPDR: %d bytes of data written\n",
853                                                           result));
854    #endif
855                                                    /* only delete link if all data has been transfered */
856                                                    /* or we couldn't write */
857                                                    if ((iorq->partial_len == iorq->length)
858                                                        || (result == 0))
859                                                    {
860    #if WITH_DEBUG_RDP5
861                                                            DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
862    #endif
863                                                            /* send a status success */
864                                                            status = STATUS_SUCCESS;
865                                                            rdpdr_send_completion(iorq->device,
866                                                                                  iorq->id, status,
867                                                                                  iorq->partial_len,
868                                                                                  (uint8 *) "", 1);
869    
870                                                            xfree(iorq->buffer);
871                                                            iorq->fd = 0;
872                                                            if (prev != NULL)
873                                                            {
874                                                                    prev->next = iorq->next;
875                                                                    xfree(iorq);
876                                                            }
877                                                            else
878                                                            {
879                                                                    // Even if NULL
880                                                                    g_iorequest = iorq->next;
881                                                                    xfree(iorq);
882                                                            }
883                                                    }
884                                          }                                          }
885                                          break;                                          break;
886                          }                          }
887    
888                  }                  }
889                    prev = iorq;
890                    iorq = iorq->next;
891          }          }
892    
893  }  }
894    
895  /* Abort a pending io request for a given handle and major */  /* Abort a pending io request for a given handle and major */
# Line 784  BOOL Line 897  BOOL
897  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
898  {  {
899          uint32 result;          uint32 result;
         int i;  
900          struct async_iorequest *iorq;          struct async_iorequest *iorq;
901            struct async_iorequest *prev;
902    
903          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
904            prev = NULL;
905            while (iorq != NULL)
906          {          {
                 iorq = &g_iorequest[i];  
   
907                  // Only remove from table when major is not set, or when correct major is supplied.                  // Only remove from table when major is not set, or when correct major is supplied.
908                  // Abort read should not abort a write io request.                  // Abort read should not abort a write io request.
909                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
910                  {                  {
911                          result = 0;                          result = 0;
912                          rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
913                                                  1);
914                          xfree(iorq->buffer);                          xfree(iorq->buffer);
915                          iorq->fd = 0;                          iorq->fd = 0;
916                            if (prev != NULL)
917                            {
918                                    prev->next = iorq->next;
919                                    xfree(iorq);
920                            }
921                            else
922                            {
923                                    // Even if NULL
924                                    g_iorequest = iorq->next;
925                                    xfree(iorq);
926                            }
927                          return True;                          return True;
928                  }                  }
929    
930                    prev = iorq;
931                    iorq = iorq->next;
932          }          }
933    
934          return False;          return False;
935  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26