/[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 569 by n-ki, Wed Jan 21 14:40:40 2004 UTC revision 595 by n-ki, Tue Feb 3 14:02:59 2004 UTC
# Line 1  Line 1 
1    #include <unistd.h>
2    #include <sys/types.h>
3    #include <time.h>
4  #include "rdesktop.h"  #include "rdesktop.h"
5    
6  #define IRP_MJ_CREATE           0x00  #define IRP_MJ_CREATE           0x00
# Line 19  Line 22 
22  #define IRP_MN_QUERY_DIRECTORY          0x01  #define IRP_MN_QUERY_DIRECTORY          0x01
23  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02
24    
 #define MAX_ASYNC_IO_REQUESTS   10  
   
25  extern char hostname[16];  extern char hostname[16];
26  extern DEVICE_FNS serial_fns;  extern DEVICE_FNS serial_fns;
27  extern DEVICE_FNS printer_fns;  extern DEVICE_FNS printer_fns;
28  extern DEVICE_FNS parallel_fns;  extern DEVICE_FNS parallel_fns;
29  extern DEVICE_FNS disk_fns;  extern DEVICE_FNS disk_fns;
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 */  /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
# Line 38  uint32 g_num_devices; Line 38  uint32 g_num_devices;
38  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
39    
40  /* 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 */
41    /* using a linked list ensures that they are processed in the right order, */
42    /* if multiple ios are being done on the same fd */
43  struct async_iorequest  struct async_iorequest
44  {  {
45          uint32 fd, major, minor, offset, device, id, length;          uint32 fd, major, minor, offset, device, id, length, partial_len;
46          long timeout,           /* Total timeout */          long timeout,           /* Total timeout */
47            itv_timeout;          /* Interval timeout (between serial characters) */            itv_timeout;          /* Interval timeout (between serial characters) */
48          uint8 *buffer;          uint8 *buffer;
49          DEVICE_FNS *fns;          DEVICE_FNS *fns;
50  } g_iorequest[MAX_ASYNC_IO_REQUESTS];  
51            struct async_iorequest *next;   /* next element in list */
52    };
53    
54    struct async_iorequest *g_iorequest;
55    
56  /* Return device_id for a given handle */  /* Return device_id for a given handle */
57  int  int
# Line 77  BOOL Line 83  BOOL
83  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,
84                      DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer)                      DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer)
85  {  {
         int i;  
86          struct async_iorequest *iorq;          struct async_iorequest *iorq;
87    
88          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          if (g_iorequest == NULL)
89          {          {
90                  iorq = &g_iorequest[i];                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
91                    g_iorequest->fd = 0;
92                  if (iorq->fd == 0)                  g_iorequest->next = NULL;
93                  {          }
94                          iorq->device = device;  
95                          iorq->fd = file;          iorq = g_iorequest;
96                          iorq->id = id;  
97                          iorq->major = major;          while (iorq->fd != 0)
98                          iorq->length = length;          {
99                          iorq->fns = fns;                  // create new element if needed
100                          iorq->timeout = total_timeout;                  if (iorq->next == NULL)
101                          iorq->itv_timeout = interval_timeout;                  {
102                          iorq->buffer = buffer;                          iorq->next =
103                          return True;                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
104                  }                          iorq->next->fd = 0;
105          }                          iorq->next->next = NULL;
106          error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n");                  }
107          return False;                  iorq = iorq->next;
108            }
109            iorq->device = device;
110            iorq->fd = file;
111            iorq->id = id;
112            iorq->major = major;
113            iorq->length = length;
114            iorq->partial_len = 0;
115            iorq->fns = fns;
116            iorq->timeout = total_timeout;
117            iorq->itv_timeout = interval_timeout;
118            iorq->buffer = buffer;
119            return True;
120  }  }
121    
   
122  void  void
123  rdpdr_send_connect(void)  rdpdr_send_connect(void)
124  {  {
# Line 185  rdpdr_send_available(void) Line 201  rdpdr_send_available(void)
201                  out_uint32_le(s, i);    /* RDP Device ID */                  out_uint32_le(s, i);    /* RDP Device ID */
202                  out_uint8p(s, g_rdpdr_device[i].name, 8);                  out_uint8p(s, g_rdpdr_device[i].name, 8);
203    
204                  if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)                  switch (g_rdpdr_device[i].device_type)
205                  {                  {
206                          printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;                          case DEVICE_TYPE_PRINTER:
207                                    printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
208    
209                          driverlen = 2 * strlen(printerinfo->driver) + 2;                                  driverlen = 2 * strlen(printerinfo->driver) + 2;
210                          printerlen = 2 * strlen(printerinfo->printer) + 2;                                  printerlen = 2 * strlen(printerinfo->printer) + 2;
211                          bloblen = printerinfo->bloblen;                                  bloblen = printerinfo->bloblen;
212    
213                          out_uint32_le(s, 24 + driverlen + printerlen + bloblen);        /* length of extra info */                                  out_uint32_le(s, 24 + driverlen + printerlen + bloblen);        /* length of extra info */
214                          out_uint32_le(s, printerinfo->default_printer ? 2 : 0);                                  out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
215                          out_uint8s(s, 8);       /* unknown */                                  out_uint8s(s, 8);       /* unknown */
216                          out_uint32_le(s, driverlen);                                  out_uint32_le(s, driverlen);
217                          out_uint32_le(s, printerlen);                                  out_uint32_le(s, printerlen);
218                          out_uint32_le(s, bloblen);                                  out_uint32_le(s, bloblen);
219                          rdp_out_unistr(s, printerinfo->driver, driverlen - 2);                                  rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
220                          rdp_out_unistr(s, printerinfo->printer, printerlen - 2);                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
221                          out_uint8a(s, printerinfo->blob, bloblen);                                  out_uint8a(s, printerinfo->blob, bloblen);
222    
223                          xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */                                  xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
224                  }                                  break;
225                  else                          default:
226                  {                                  out_uint32(s, 0);
                         out_uint32(s, 0);  
227                  }                  }
228          }          }
229  #if 0  #if 0
# Line 279  rdpdr_process_irp(STREAM s) Line 295  rdpdr_process_irp(STREAM s)
295          buffer = (uint8 *) xmalloc(1024);          buffer = (uint8 *) xmalloc(1024);
296          buffer[0] = 0;          buffer[0] = 0;
297    
   
298          switch (g_rdpdr_device[device].device_type)          switch (g_rdpdr_device[device].device_type)
299          {          {
300                  case DEVICE_TYPE_SERIAL:                  case DEVICE_TYPE_SERIAL:
# Line 302  rdpdr_process_irp(STREAM s) Line 317  rdpdr_process_irp(STREAM s)
317                  case DEVICE_TYPE_DISK:                  case DEVICE_TYPE_DISK:
318    
319                          fns = &disk_fns;                          fns = &disk_fns;
320                            rw_blocking = False;
321                          break;                          break;
322    
323                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
# Line 388  rdpdr_process_irp(STREAM s) Line 404  rdpdr_process_irp(STREAM s)
404    
405                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
406                          break;                          break;
   
407                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
408    
409                          buffer_len = 1;                          buffer_len = 1;
# Line 658  rdpdr_init() Line 673  rdpdr_init()
673  {  {
674          if (g_num_devices > 0)          if (g_num_devices > 0)
675          {          {
676                  rdpdr_channel = channel_register("rdpdr", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP, rdpdr_process);                  rdpdr_channel =
677                            channel_register("rdpdr",
678                                             CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
679                                             rdpdr_process);
680          }          }
681    
682          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
# Line 668  rdpdr_init() Line 686  rdpdr_init()
686  void  void
687  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)
688  {  {
         int i;  
689          long 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).
690          struct async_iorequest *iorq;          struct async_iorequest *iorq;
691    
692          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
693            while (iorq != NULL)
694          {          {
695                  iorq = &g_iorequest[i];                  if (iorq->fd != 0)
   
                 if (iorq->fd != 0)      // Found a pending io request  
696                  {                  {
697                          switch (iorq->major)                          switch (iorq->major)
698                          {                          {
# Line 699  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 715  rdpdr_add_fds(int *n, fd_set * rfds, fd_
715                                          break;                                          break;
716    
717                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
718                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
719                                          break;                                          break;
720    
721                          }                          }
722                          *n = MAX(*n, iorq->fd);                          *n = MAX(*n, iorq->fd);
723                  }                  }
724    
725                    iorq = iorq->next;
726          }          }
727  }  }
728    
729    
730  /* 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 */
731  void  void
732  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
733  {  {
         int i;  
734          NTSTATUS status;          NTSTATUS status;
735          uint32 result = 0, buffer_len = 0;          uint32 result = 0;
736          DEVICE_FNS *fns;          DEVICE_FNS *fns;
737          struct async_iorequest *iorq;          struct async_iorequest *iorq;
738            struct async_iorequest *prev;
739            uint32 req_size = 0;
740    
741          if (timed_out)          if (timed_out)
742          {          {
# Line 725  rdpdr_check_fds(fd_set * rfds, fd_set * Line 744  rdpdr_check_fds(fd_set * rfds, fd_set *
744                  return;                  return;
745          }          }
746    
747          // Walk through array of pending io_rq's          iorq = g_iorequest;
748          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          prev = NULL;
749            while (iorq != NULL)
750          {          {
                 iorq = &g_iorequest[i];  
   
751                  if (iorq->fd != 0)                  if (iorq->fd != 0)
752                  {                  {
753                          switch (iorq->major)                          switch (iorq->major)
754                          {                          {
   
755                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
   
756                                          if (FD_ISSET(iorq->fd, rfds))                                          if (FD_ISSET(iorq->fd, rfds))
757                                          {                                          {
758                                                  // Read, and send data.                                                  /* Read the data */
759                                                  fns = iorq->fns;                                                  fns = iorq->fns;
760                                                  status = fns->read(iorq->fd, iorq->buffer,  
761                                                                     iorq->length, 0, &result);                                                  req_size =
762                                                  buffer_len = result;                                                          (iorq->length - iorq->partial_len) >
763                                                            8192 ? 8192 : (iorq->length -
764                                                  rdpdr_send_completion(iorq->device, iorq->id,                                                                         iorq->partial_len);
765                                                                        status, result, iorq->buffer,                                                  /* never read larger chunks than 8k - chances are that it will block */
766                                                                        buffer_len);                                                  status = fns->read(iorq->fd,
767                                                                       iorq->buffer + iorq->partial_len,
768                                                                       req_size, 0, &result);
769                                                    iorq->partial_len += result;
770    
771  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
772                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
773  #endif  #endif
774                                                  xfree(iorq->buffer);                                                  /* only delete link if all data has been transfered */
775                                                  iorq->fd = 0;                                                  /* or if result was 0 and status success - EOF      */
776                                                    if ((iorq->partial_len == iorq->length) ||
777                                                        (result == 0))
778                                                    {
779    #if WITH_DEBUG_RDP5
780                                                            DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
781    #endif
782                                                            /* send the data */
783                                                            status = STATUS_SUCCESS;
784                                                            rdpdr_send_completion(iorq->device,
785                                                                                  iorq->id, status,
786                                                                                  iorq->partial_len,
787                                                                                  iorq->buffer,
788                                                                                  iorq->partial_len);
789                                                            xfree(iorq->buffer);
790                                                            iorq->fd = 0;
791                                                            if (prev != NULL)
792                                                            {
793                                                                    prev->next = iorq->next;
794                                                                    xfree(iorq);
795                                                            }
796                                                            else
797                                                            {
798                                                                    // Even if NULL
799                                                                    g_iorequest = iorq->next;
800                                                                    xfree(iorq);
801                                                            }
802                                                    }
803                                          }                                          }
804                                          break;                                          break;
   
805                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
806                                          if (FD_ISSET(iorq->fd, wfds))                                          if (FD_ISSET(iorq->fd, wfds))
807                                          {                                          {
808                                                  // Write data and send completion.                                                  /* Write data. */
809                                                  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);  
810    
811                                                  xfree(iorq->buffer);                                                  req_size =
812                                                  iorq->fd = 0;                                                          (iorq->length - iorq->partial_len) >
813                                                            8192 ? 8192 : (iorq->length -
814                                                                           iorq->partial_len);
815    
816                                                    /* never write larger chunks than 8k - chances are that it will block */
817                                                    status = fns->write(iorq->fd,
818                                                                        iorq->buffer +
819                                                                        iorq->partial_len, req_size, 0,
820                                                                        &result);
821                                                    iorq->partial_len += result;
822    #if WITH_DEBUG_RDP5
823                                                    DEBUG(("RDPDR: %d bytes of data written\n",
824                                                           result));
825    #endif
826                                                    /* only delete link if all data has been transfered */
827                                                    /* or we couldn't write */
828                                                    if ((iorq->partial_len == iorq->length)
829                                                        || (result == 0))
830                                                    {
831    #if WITH_DEBUG_RDP5
832                                                            DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
833    #endif
834                                                            /* send a status success */
835                                                            status = STATUS_SUCCESS;
836                                                            rdpdr_send_completion(iorq->device,
837                                                                                  iorq->id, status,
838                                                                                  iorq->partial_len, "",
839                                                                                  1);
840    
841                                                            xfree(iorq->buffer);
842                                                            iorq->fd = 0;
843                                                            if (prev != NULL)
844                                                            {
845                                                                    prev->next = iorq->next;
846                                                                    xfree(iorq);
847                                                            }
848                                                            else
849                                                            {
850                                                                    // Even if NULL
851                                                                    g_iorequest = iorq->next;
852                                                                    xfree(iorq);
853                                                            }
854                                                    }
855                                          }                                          }
856                                          break;                                          break;
857                          }                          }
858    
859                  }                  }
860                    prev = iorq;
861                    iorq = iorq->next;
862          }          }
863    
864  }  }
865    
866  /* Abort a pending io request for a given handle and major */  /* Abort a pending io request for a given handle and major */
# Line 781  BOOL Line 868  BOOL
868  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
869  {  {
870          uint32 result;          uint32 result;
         int i;  
871          struct async_iorequest *iorq;          struct async_iorequest *iorq;
872            struct async_iorequest *prev;
873    
874          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = &g_iorequest;
875            prev = NULL;
876            while (iorq != NULL)
877          {          {
                 iorq = &g_iorequest[i];  
   
878                  // 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.
879                  // Abort read should not abort a write io request.                  // Abort read should not abort a write io request.
880                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
# Line 796  rdpdr_abort_io(uint32 fd, uint32 major, Line 883  rdpdr_abort_io(uint32 fd, uint32 major,
883                          rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);                          rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);
884                          xfree(iorq->buffer);                          xfree(iorq->buffer);
885                          iorq->fd = 0;                          iorq->fd = 0;
886                            if (prev != NULL)
887                            {
888                                    prev->next = iorq->next;
889                                    xfree(iorq);
890                            }
891                            else
892                            {
893                                    // Even if NULL
894                                    g_iorequest = iorq->next;
895                                    xfree(iorq);
896                            }
897                          return True;                          return True;
898                  }                  }
899    
900                    prev = iorq;
901                    iorq = iorq->next;
902          }          }
903    
904          return False;          return False;
905  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26