/[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 590 by n-ki, Thu Jan 29 12:27:21 2004 UTC revision 609 by stargo, Mon Feb 16 20:28:09 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 40  uint32 g_num_devices; Line 38  uint32 g_num_devices;
38  /* Table with information about rdpdr devices */  /* Table with information about rdpdr devices */
39  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
40    
 #if 0  
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  #endif          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 77  convert_to_unix_filename(char *filename) Line 79  convert_to_unix_filename(char *filename)
79          }          }
80  }  }
81    
 #if 0  
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                    g_iorequest->fd = 0;
93                  if (iorq->fd == 0)                  g_iorequest->next = NULL;
94                  {          }
95                          iorq->device = device;  
96                          iorq->fd = file;          iorq = g_iorequest;
97                          iorq->id = id;  
98                          iorq->major = major;          while (iorq->fd != 0)
99                          iorq->length = length;          {
100                          iorq->fns = fns;                  // create new element if needed
101                          iorq->timeout = total_timeout;                  if (iorq->next == NULL)
102                          iorq->itv_timeout = interval_timeout;                  {
103                          iorq->buffer = buffer;                          iorq->next =
104                          return True;                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
105                  }                          iorq->next->fd = 0;
106          }                          iorq->next->next = NULL;
107          error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n");                  }
108          return False;                  iorq = iorq->next;
109            }
110            iorq->device = device;
111            iorq->fd = file;
112            iorq->id = id;
113            iorq->major = major;
114            iorq->length = length;
115            iorq->partial_len = 0;
116            iorq->fns = fns;
117            iorq->timeout = total_timeout;
118            iorq->itv_timeout = interval_timeout;
119            iorq->buffer = buffer;
120            return True;
121  }  }
 #endif  
122    
123  void  void
124  rdpdr_send_connect(void)  rdpdr_send_connect(void)
# Line 210  rdpdr_send_available(void) Line 221  rdpdr_send_available(void)
221                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
222                                  out_uint8a(s, printerinfo->blob, bloblen);                                  out_uint8a(s, printerinfo->blob, bloblen);
223    
224                                  xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */                                  if (printerinfo->blob)
225                                            xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
226                                  break;                                  break;
227                          default:                          default:
228                                  out_uint32(s, 0);                                  out_uint32(s, 0);
# Line 290  rdpdr_process_irp(STREAM s) Line 302  rdpdr_process_irp(STREAM s)
302                  case DEVICE_TYPE_SERIAL:                  case DEVICE_TYPE_SERIAL:
303    
304                          fns = &serial_fns;                          fns = &serial_fns;
305                          /* should be async when aio is finished */                          rw_blocking = False;
                         /*rw_blocking = False; */  
306                          break;                          break;
307    
308                  case DEVICE_TYPE_PARALLEL:                  case DEVICE_TYPE_PARALLEL:
309    
310                          fns = &parallel_fns;                          fns = &parallel_fns;
311                          /* should be async when aio is finished */                          rw_blocking = False;
                         /*rw_blocking = False;*/  
312                          break;                          break;
313    
314                  case DEVICE_TYPE_PRINTER:                  case DEVICE_TYPE_PRINTER:
# Line 309  rdpdr_process_irp(STREAM s) Line 319  rdpdr_process_irp(STREAM s)
319                  case DEVICE_TYPE_DISK:                  case DEVICE_TYPE_DISK:
320    
321                          fns = &disk_fns;                          fns = &disk_fns;
322                            rw_blocking = False;
323                          break;                          break;
324    
325                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
# Line 374  rdpdr_process_irp(STREAM s) Line 385  rdpdr_process_irp(STREAM s)
385  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
386                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
387  #endif  #endif
388  //                      if (rw_blocking)        // Complete read immediately                          if (rw_blocking)        // Complete read immediately
389  //                      {                          {
390                                  buffer = (uint8 *) xrealloc((void *) buffer, length);                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
391                                  status = fns->read(file, buffer, length, offset, &result);                                  status = fns->read(file, buffer, length, offset, &result);
392                                  buffer_len = result;                                  buffer_len = result;
393                                  break;                                  break;
394  //                      }                          }
395    
 #if 0  
396                          // Add request to table                          // Add request to table
397                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
398                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);
# Line 396  rdpdr_process_irp(STREAM s) Line 406  rdpdr_process_irp(STREAM s)
406    
407                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
408                          break;                          break;
 #endif  
409                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
410    
411                          buffer_len = 1;                          buffer_len = 1;
# Line 413  rdpdr_process_irp(STREAM s) Line 422  rdpdr_process_irp(STREAM s)
422  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
423                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));
424  #endif  #endif
425  //                      if (rw_blocking)        // Complete immediately                          if (rw_blocking)        // Complete immediately
426  //                      {                          {
427                                  status = fns->write(file, s->p, length, offset, &result);                                  status = fns->write(file, s->p, length, offset, &result);
428                                  break;                                  break;
429  //                      }                          }
430  #if 0  
431                          // Add to table                          // Add to table
432                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
433                          in_uint8a(s, pst_buf, length);                          in_uint8a(s, pst_buf, length);
# Line 432  rdpdr_process_irp(STREAM s) Line 441  rdpdr_process_irp(STREAM s)
441    
442                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
443                          break;                          break;
 #endif  
444    
445                  case IRP_MJ_QUERY_INFORMATION:                  case IRP_MJ_QUERY_INFORMATION:
446    
# Line 676  rdpdr_init() Line 684  rdpdr_init()
684          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
685  }  }
686    
 #if 0  
687  /* Add file descriptors of pending io request to select() */  /* Add file descriptors of pending io request to select() */
688  void  void
689  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)
690  {  {
691          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).  
692          struct async_iorequest *iorq;          struct async_iorequest *iorq;
693    
694          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
695            while (iorq != NULL)
696          {          {
697                  iorq = &g_iorequest[i];                  if (iorq->fd != 0)
   
                 if (iorq->fd != 0)      // Found a pending io request  
698                  {                  {
699                          switch (iorq->major)                          switch (iorq->major)
700                          {                          {
# Line 712  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 717  rdpdr_add_fds(int *n, fd_set * rfds, fd_
717                                          break;                                          break;
718    
719                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
720                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
721                                          break;                                          break;
722    
723                          }                          }
724                          *n = MAX(*n, iorq->fd);                          *n = MAX(*n, iorq->fd);
725                  }                  }
726    
727                    iorq = iorq->next;
728          }          }
729  }  }
730    
# Line 727  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 733  rdpdr_add_fds(int *n, fd_set * rfds, fd_
733  void  void
734  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
735  {  {
         int i;  
736          NTSTATUS status;          NTSTATUS status;
737          uint32 result = 0, buffer_len = 0;          uint32 result = 0;
738          DEVICE_FNS *fns;          DEVICE_FNS *fns;
739          struct async_iorequest *iorq;          struct async_iorequest *iorq;
740            struct async_iorequest *prev;
741            uint32 req_size = 0;
742    
743          if (timed_out)          if (timed_out)
744          {          {
# Line 739  rdpdr_check_fds(fd_set * rfds, fd_set * Line 746  rdpdr_check_fds(fd_set * rfds, fd_set *
746                  return;                  return;
747          }          }
748    
749          // Walk through array of pending io_rq's          iorq = g_iorequest;
750          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          prev = NULL;
751            while (iorq != NULL)
752          {          {
                 iorq = &g_iorequest[i];  
   
753                  if (iorq->fd != 0)                  if (iorq->fd != 0)
754                  {                  {
755                          switch (iorq->major)                          switch (iorq->major)
756                          {                          {
   
757                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
   
758                                          if (FD_ISSET(iorq->fd, rfds))                                          if (FD_ISSET(iorq->fd, rfds))
759                                          {                                          {
760                                                  // Read, and send data.                                                  /* Read the data */
761                                                  fns = iorq->fns;                                                  fns = iorq->fns;
762                                                  status = fns->read(iorq->fd, iorq->buffer,  
763                                                                     iorq->length, 0, &result);                                                  req_size =
764                                                  buffer_len = result;                                                          (iorq->length - iorq->partial_len) >
765                                                            8192 ? 8192 : (iorq->length -
766                                                  rdpdr_send_completion(iorq->device, iorq->id,                                                                         iorq->partial_len);
767                                                                        status, result, iorq->buffer,                                                  /* never read larger chunks than 8k - chances are that it will block */
768                                                                        buffer_len);                                                  status = fns->read(iorq->fd,
769                                                                       iorq->buffer + iorq->partial_len,
770                                                                       req_size, 0, &result);
771                                                    iorq->partial_len += result;
772    
773  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
774                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
775  #endif  #endif
776                                                  xfree(iorq->buffer);                                                  /* only delete link if all data has been transfered */
777                                                  iorq->fd = 0;                                                  /* or if result was 0 and status success - EOF      */
778                                                    if ((iorq->partial_len == iorq->length) ||
779                                                        (result == 0))
780                                                    {
781    #if WITH_DEBUG_RDP5
782                                                            DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
783    #endif
784                                                            /* send the data */
785                                                            status = STATUS_SUCCESS;
786                                                            rdpdr_send_completion(iorq->device,
787                                                                                  iorq->id, status,
788                                                                                  iorq->partial_len,
789                                                                                  iorq->buffer,
790                                                                                  iorq->partial_len);
791                                                            xfree(iorq->buffer);
792                                                            iorq->fd = 0;
793                                                            if (prev != NULL)
794                                                            {
795                                                                    prev->next = iorq->next;
796                                                                    xfree(iorq);
797                                                            }
798                                                            else
799                                                            {
800                                                                    // Even if NULL
801                                                                    g_iorequest = iorq->next;
802                                                                    xfree(iorq);
803                                                            }
804                                                    }
805                                          }                                          }
806                                          break;                                          break;
   
807                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
808                                          if (FD_ISSET(iorq->fd, wfds))                                          if (FD_ISSET(iorq->fd, wfds))
809                                          {                                          {
810                                                  // Write data and send completion.                                                  /* Write data. */
811                                                  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);  
812    
813                                                  xfree(iorq->buffer);                                                  req_size =
814                                                  iorq->fd = 0;                                                          (iorq->length - iorq->partial_len) >
815                                                            8192 ? 8192 : (iorq->length -
816                                                                           iorq->partial_len);
817    
818                                                    /* never write larger chunks than 8k - chances are that it will block */
819                                                    status = fns->write(iorq->fd,
820                                                                        iorq->buffer +
821                                                                        iorq->partial_len, req_size, 0,
822                                                                        &result);
823                                                    iorq->partial_len += result;
824    #if WITH_DEBUG_RDP5
825                                                    DEBUG(("RDPDR: %d bytes of data written\n",
826                                                           result));
827    #endif
828                                                    /* only delete link if all data has been transfered */
829                                                    /* or we couldn't write */
830                                                    if ((iorq->partial_len == iorq->length)
831                                                        || (result == 0))
832                                                    {
833    #if WITH_DEBUG_RDP5
834                                                            DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
835    #endif
836                                                            /* send a status success */
837                                                            status = STATUS_SUCCESS;
838                                                            rdpdr_send_completion(iorq->device,
839                                                                                  iorq->id, status,
840                                                                                  iorq->partial_len,
841                                                                                  (uint8 *) "", 1);
842    
843                                                            xfree(iorq->buffer);
844                                                            iorq->fd = 0;
845                                                            if (prev != NULL)
846                                                            {
847                                                                    prev->next = iorq->next;
848                                                                    xfree(iorq);
849                                                            }
850                                                            else
851                                                            {
852                                                                    // Even if NULL
853                                                                    g_iorequest = iorq->next;
854                                                                    xfree(iorq);
855                                                            }
856                                                    }
857                                          }                                          }
858                                          break;                                          break;
859                          }                          }
860    
861                  }                  }
862                    prev = iorq;
863                    iorq = iorq->next;
864          }          }
865    
866  }  }
867    
868  /* Abort a pending io request for a given handle and major */  /* Abort a pending io request for a given handle and major */
# Line 795  BOOL Line 870  BOOL
870  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
871  {  {
872          uint32 result;          uint32 result;
         int i;  
873          struct async_iorequest *iorq;          struct async_iorequest *iorq;
874            struct async_iorequest *prev;
875    
876          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
877            prev = NULL;
878            while (iorq != NULL)
879          {          {
                 iorq = &g_iorequest[i];  
   
880                  // 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.
881                  // Abort read should not abort a write io request.                  // Abort read should not abort a write io request.
882                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
883                  {                  {
884                          result = 0;                          result = 0;
885                          rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
886                                                  1);
887                          xfree(iorq->buffer);                          xfree(iorq->buffer);
888                          iorq->fd = 0;                          iorq->fd = 0;
889                            if (prev != NULL)
890                            {
891                                    prev->next = iorq->next;
892                                    xfree(iorq);
893                            }
894                            else
895                            {
896                                    // Even if NULL
897                                    g_iorequest = iorq->next;
898                                    xfree(iorq);
899                            }
900                          return True;                          return True;
901                  }                  }
902    
903                    prev = iorq;
904                    iorq = iorq->next;
905          }          }
906    
907          return False;          return False;
908  }  }
 #endif  

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

  ViewVC Help
Powered by ViewVC 1.1.26