/[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 602 by stargo, Sat Feb 7 17:32:21 2004 UTC
# Line 22  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 41  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 80  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 188  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 */                                  if (printerinfo->blob)
224                  }                                          xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
225                  else                                  break;
226                  {                          default:
227                          out_uint32(s, 0);                                  out_uint32(s, 0);
228                  }                  }
229          }          }
230  #if 0  #if 0
# Line 282  rdpdr_process_irp(STREAM s) Line 296  rdpdr_process_irp(STREAM s)
296          buffer = (uint8 *) xmalloc(1024);          buffer = (uint8 *) xmalloc(1024);
297          buffer[0] = 0;          buffer[0] = 0;
298    
   
299          switch (g_rdpdr_device[device].device_type)          switch (g_rdpdr_device[device].device_type)
300          {          {
301                  case DEVICE_TYPE_SERIAL:                  case DEVICE_TYPE_SERIAL:
# Line 305  rdpdr_process_irp(STREAM s) Line 318  rdpdr_process_irp(STREAM s)
318                  case DEVICE_TYPE_DISK:                  case DEVICE_TYPE_DISK:
319    
320                          fns = &disk_fns;                          fns = &disk_fns;
321                            rw_blocking = False;
322                          break;                          break;
323    
324                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
# Line 391  rdpdr_process_irp(STREAM s) Line 405  rdpdr_process_irp(STREAM s)
405    
406                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
407                          break;                          break;
   
408                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
409    
410                          buffer_len = 1;                          buffer_len = 1;
# Line 661  rdpdr_init() Line 674  rdpdr_init()
674  {  {
675          if (g_num_devices > 0)          if (g_num_devices > 0)
676          {          {
677                  rdpdr_channel = channel_register("rdpdr", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP, rdpdr_process);                  rdpdr_channel =
678                            channel_register("rdpdr",
679                                             CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
680                                             rdpdr_process);
681          }          }
682    
683          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
# Line 671  rdpdr_init() Line 687  rdpdr_init()
687  void  void
688  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)
689  {  {
         int i;  
690          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).
691          struct async_iorequest *iorq;          struct async_iorequest *iorq;
692    
693          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
694            while (iorq != NULL)
695          {          {
696                  iorq = &g_iorequest[i];                  if (iorq->fd != 0)
   
                 if (iorq->fd != 0)      // Found a pending io request  
697                  {                  {
698                          switch (iorq->major)                          switch (iorq->major)
699                          {                          {
# Line 702  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 716  rdpdr_add_fds(int *n, fd_set * rfds, fd_
716                                          break;                                          break;
717    
718                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
719                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
720                                          break;                                          break;
721    
722                          }                          }
723                          *n = MAX(*n, iorq->fd);                          *n = MAX(*n, iorq->fd);
724                  }                  }
725    
726                    iorq = iorq->next;
727          }          }
728  }  }
729    
730    
731  /* 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 */
732  void  void
733  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
734  {  {
         int i;  
735          NTSTATUS status;          NTSTATUS status;
736          uint32 result = 0, buffer_len = 0;          uint32 result = 0;
737          DEVICE_FNS *fns;          DEVICE_FNS *fns;
738          struct async_iorequest *iorq;          struct async_iorequest *iorq;
739            struct async_iorequest *prev;
740            uint32 req_size = 0;
741    
742          if (timed_out)          if (timed_out)
743          {          {
# Line 728  rdpdr_check_fds(fd_set * rfds, fd_set * Line 745  rdpdr_check_fds(fd_set * rfds, fd_set *
745                  return;                  return;
746          }          }
747    
748          // Walk through array of pending io_rq's          iorq = g_iorequest;
749          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          prev = NULL;
750            while (iorq != NULL)
751          {          {
                 iorq = &g_iorequest[i];  
   
752                  if (iorq->fd != 0)                  if (iorq->fd != 0)
753                  {                  {
754                          switch (iorq->major)                          switch (iorq->major)
755                          {                          {
   
756                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
   
757                                          if (FD_ISSET(iorq->fd, rfds))                                          if (FD_ISSET(iorq->fd, rfds))
758                                          {                                          {
759                                                  // Read, and send data.                                                  /* Read the data */
760                                                  fns = iorq->fns;                                                  fns = iorq->fns;
761                                                  status = fns->read(iorq->fd, iorq->buffer,  
762                                                                     iorq->length, 0, &result);                                                  req_size =
763                                                  buffer_len = result;                                                          (iorq->length - iorq->partial_len) >
764                                                            8192 ? 8192 : (iorq->length -
765                                                  rdpdr_send_completion(iorq->device, iorq->id,                                                                         iorq->partial_len);
766                                                                        status, result, iorq->buffer,                                                  /* never read larger chunks than 8k - chances are that it will block */
767                                                                        buffer_len);                                                  status = fns->read(iorq->fd,
768                                                                       iorq->buffer + iorq->partial_len,
769                                                                       req_size, 0, &result);
770                                                    iorq->partial_len += result;
771    
772  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
773                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
774  #endif  #endif
775                                                  xfree(iorq->buffer);                                                  /* only delete link if all data has been transfered */
776                                                  iorq->fd = 0;                                                  /* or if result was 0 and status success - EOF      */
777                                                    if ((iorq->partial_len == iorq->length) ||
778                                                        (result == 0))
779                                                    {
780    #if WITH_DEBUG_RDP5
781                                                            DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
782    #endif
783                                                            /* send the data */
784                                                            status = STATUS_SUCCESS;
785                                                            rdpdr_send_completion(iorq->device,
786                                                                                  iorq->id, status,
787                                                                                  iorq->partial_len,
788                                                                                  iorq->buffer,
789                                                                                  iorq->partial_len);
790                                                            xfree(iorq->buffer);
791                                                            iorq->fd = 0;
792                                                            if (prev != NULL)
793                                                            {
794                                                                    prev->next = iorq->next;
795                                                                    xfree(iorq);
796                                                            }
797                                                            else
798                                                            {
799                                                                    // Even if NULL
800                                                                    g_iorequest = iorq->next;
801                                                                    xfree(iorq);
802                                                            }
803                                                    }
804                                          }                                          }
805                                          break;                                          break;
   
806                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
807                                          if (FD_ISSET(iorq->fd, wfds))                                          if (FD_ISSET(iorq->fd, wfds))
808                                          {                                          {
809                                                  // Write data and send completion.                                                  /* Write data. */
810                                                  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);  
811    
812                                                  xfree(iorq->buffer);                                                  req_size =
813                                                  iorq->fd = 0;                                                          (iorq->length - iorq->partial_len) >
814                                                            8192 ? 8192 : (iorq->length -
815                                                                           iorq->partial_len);
816    
817                                                    /* never write larger chunks than 8k - chances are that it will block */
818                                                    status = fns->write(iorq->fd,
819                                                                        iorq->buffer +
820                                                                        iorq->partial_len, req_size, 0,
821                                                                        &result);
822                                                    iorq->partial_len += result;
823    #if WITH_DEBUG_RDP5
824                                                    DEBUG(("RDPDR: %d bytes of data written\n",
825                                                           result));
826    #endif
827                                                    /* only delete link if all data has been transfered */
828                                                    /* or we couldn't write */
829                                                    if ((iorq->partial_len == iorq->length)
830                                                        || (result == 0))
831                                                    {
832    #if WITH_DEBUG_RDP5
833                                                            DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
834    #endif
835                                                            /* send a status success */
836                                                            status = STATUS_SUCCESS;
837                                                            rdpdr_send_completion(iorq->device,
838                                                                                  iorq->id, status,
839                                                                                  iorq->partial_len, (uint8*)"",
840                                                                                  1);
841    
842                                                            xfree(iorq->buffer);
843                                                            iorq->fd = 0;
844                                                            if (prev != NULL)
845                                                            {
846                                                                    prev->next = iorq->next;
847                                                                    xfree(iorq);
848                                                            }
849                                                            else
850                                                            {
851                                                                    // Even if NULL
852                                                                    g_iorequest = iorq->next;
853                                                                    xfree(iorq);
854                                                            }
855                                                    }
856                                          }                                          }
857                                          break;                                          break;
858                          }                          }
859    
860                  }                  }
861                    prev = iorq;
862                    iorq = iorq->next;
863          }          }
864    
865  }  }
866    
867  /* 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 869  BOOL
869  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
870  {  {
871          uint32 result;          uint32 result;
         int i;  
872          struct async_iorequest *iorq;          struct async_iorequest *iorq;
873            struct async_iorequest *prev;
874    
875          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
876            prev = NULL;
877            while (iorq != NULL)
878          {          {
                 iorq = &g_iorequest[i];  
   
879                  // 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.
880                  // Abort read should not abort a write io request.                  // Abort read should not abort a write io request.
881                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
882                  {                  {
883                          result = 0;                          result = 0;
884                          rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8*)"", 1);
885                          xfree(iorq->buffer);                          xfree(iorq->buffer);
886                          iorq->fd = 0;                          iorq->fd = 0;
887                            if (prev != NULL)
888                            {
889                                    prev->next = iorq->next;
890                                    xfree(iorq);
891                            }
892                            else
893                            {
894                                    // Even if NULL
895                                    g_iorequest = iorq->next;
896                                    xfree(iorq);
897                            }
898                          return True;                          return True;
899                  }                  }
900    
901                    prev = iorq;
902                    iorq = iorq->next;
903          }          }
904    
905          return False;          return False;
906  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26