/[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 592 by n-ki, Fri Jan 30 14:10:32 2004 UTC revision 627 by n-ki, Thu Mar 4 08:24:40 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 <dirent.h>             /* opendir, closedir, readdir */
5  #include <time.h>  #include <time.h>
6  #include "rdesktop.h"  #include "rdesktop.h"
7    
# Line 27  extern DEVICE_FNS serial_fns; Line 29  extern DEVICE_FNS serial_fns;
29  extern DEVICE_FNS printer_fns;  extern DEVICE_FNS printer_fns;
30  extern DEVICE_FNS parallel_fns;  extern DEVICE_FNS parallel_fns;
31  extern DEVICE_FNS disk_fns;  extern DEVICE_FNS disk_fns;
32    extern FILEINFO g_fileinfo[];
33    
34  static VCHANNEL *rdpdr_channel;  static VCHANNEL *rdpdr_channel;
35    
# Line 49  struct async_iorequest Line 52  struct async_iorequest
52          DEVICE_FNS *fns;          DEVICE_FNS *fns;
53    
54          struct async_iorequest *next;   /* next element in list */          struct async_iorequest *next;   /* next element in list */
55  } g_iorequest;  };
56    
57    struct async_iorequest *g_iorequest;
58    
59  /* Return device_id for a given handle */  /* Return device_id for a given handle */
60  int  int
# Line 76  convert_to_unix_filename(char *filename) Line 81  convert_to_unix_filename(char *filename)
81          }          }
82  }  }
83    
84    BOOL
85    rdpdr_handle_ok(int device, int handle)
86    {
87            switch (g_rdpdr_device[device].device_type)
88            {
89                    case DEVICE_TYPE_PARALLEL:
90                    case DEVICE_TYPE_SERIAL:
91                    case DEVICE_TYPE_PRINTER:
92                    case DEVICE_TYPE_SCARD:
93                            if (g_rdpdr_device[device].handle != handle)
94                                    return False;
95                            break;
96                    case DEVICE_TYPE_DISK:
97                            if (g_fileinfo[handle].device_id != device)
98                                    return False;
99                            break;
100            }
101            return True;
102    }
103    
104  /* 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 */
105  BOOL  BOOL
106  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,
107                      DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer)                      DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
108                        uint32 offset)
109  {  {
110          struct async_iorequest *iorq;          struct async_iorequest *iorq;
111    
112          iorq = &g_iorequest;          if (g_iorequest == NULL)
113            {
114                    g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
115                    if (!g_iorequest)
116                            return False;
117                    g_iorequest->fd = 0;
118                    g_iorequest->next = NULL;
119            }
120    
121            iorq = g_iorequest;
122    
123          while (iorq->fd != 0)          while (iorq->fd != 0)
124          {          {
125                  // create new element if needed                  // create new element if needed
126                  if (iorq->next == NULL)                  if (iorq->next == NULL)
                         iorq->next =  
                                 (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));  
   
                 iorq = iorq->next;  
         }  
   
         /* first element is special since it doesn't get deleted */  
         /* don't want to get io out of order */  
         if (g_iorequest.fd == 0)  
         {  
                 iorq = &g_iorequest;  
                 /* look for first occurrence of fd */  
                 while (iorq->next != NULL)  
127                  {                  {
                         if (iorq->fd == file)  
                                 break;  
                         iorq = iorq->next;  
                 }  
                 /* if same create new link at end of chain instead */  
                 if (iorq->fd == file)  
                 {  
                         while (iorq->next != NULL)  
                                 iorq = iorq->next;  
128                          iorq->next =                          iorq->next =
129                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
130                          iorq = iorq->next;                          if (!iorq->next)
131                                    return False;
132                            iorq->next->fd = 0;
133                            iorq->next->next = NULL;
134                  }                  }
135                  else                  iorq = iorq->next;
                         iorq = &g_iorequest;    /* didn't find fs use first entry */  
136          }          }
   
137          iorq->device = device;          iorq->device = device;
138          iorq->fd = file;          iorq->fd = file;
139          iorq->id = id;          iorq->id = id;
# Line 129  add_async_iorequest(uint32 device, uint3 Line 144  add_async_iorequest(uint32 device, uint3
144          iorq->timeout = total_timeout;          iorq->timeout = total_timeout;
145          iorq->itv_timeout = interval_timeout;          iorq->itv_timeout = interval_timeout;
146          iorq->buffer = buffer;          iorq->buffer = buffer;
147            iorq->offset = offset;
148          return True;          return True;
149  }  }
150    
# Line 233  rdpdr_send_available(void) Line 249  rdpdr_send_available(void)
249                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
250                                  out_uint8a(s, printerinfo->blob, bloblen);                                  out_uint8a(s, printerinfo->blob, bloblen);
251    
252                                  xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */                                  if (printerinfo->blob)
253                                            xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
254                                  break;                                  break;
255                          default:                          default:
256                                  out_uint32(s, 0);                                  out_uint32(s, 0);
# Line 329  rdpdr_process_irp(STREAM s) Line 346  rdpdr_process_irp(STREAM s)
346    
347                  case DEVICE_TYPE_DISK:                  case DEVICE_TYPE_DISK:
348    
                         /*rw_blocking = False; */  
349                          fns = &disk_fns;                          fns = &disk_fns;
350                            rw_blocking = False;
351                          break;                          break;
352    
353                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
# Line 396  rdpdr_process_irp(STREAM s) Line 413  rdpdr_process_irp(STREAM s)
413  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
414                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
415  #endif  #endif
416                            if (!rdpdr_handle_ok(device, file))
417                            {
418                                    status = STATUS_INVALID_HANDLE;
419                                    break;
420                            }
421    
422                          if (rw_blocking)        // Complete read immediately                          if (rw_blocking)        // Complete read immediately
423                          {                          {
424                                  buffer = (uint8 *) xrealloc((void *) buffer, length);                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
425                                    if (!buffer)
426                                    {
427                                            status = STATUS_CANCELLED;
428                                            break;
429                                    }
430                                  status = fns->read(file, buffer, length, offset, &result);                                  status = fns->read(file, buffer, length, offset, &result);
431                                  buffer_len = result;                                  buffer_len = result;
432                                  break;                                  break;
# Line 406  rdpdr_process_irp(STREAM s) Line 434  rdpdr_process_irp(STREAM s)
434    
435                          // Add request to table                          // Add request to table
436                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
437                            if (!pst_buf)
438                            {
439                                    status = STATUS_CANCELLED;
440                                    break;
441                            }
442                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);
443                          if (add_async_iorequest                          if (add_async_iorequest
444                              (device, file, id, major, length, fns, total_timeout, interval_timeout,                              (device, file, id, major, length, fns, total_timeout, interval_timeout,
445                               pst_buf))                               pst_buf, offset))
446                          {                          {
447                                  status = STATUS_PENDING;                                  status = STATUS_PENDING;
448                                  break;                                  break;
# Line 433  rdpdr_process_irp(STREAM s) Line 466  rdpdr_process_irp(STREAM s)
466  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
467                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));
468  #endif  #endif
469                            if (!rdpdr_handle_ok(device, file))
470                            {
471                                    status = STATUS_INVALID_HANDLE;
472                                    break;
473                            }
474    
475                          if (rw_blocking)        // Complete immediately                          if (rw_blocking)        // Complete immediately
476                          {                          {
477                                  status = fns->write(file, s->p, length, offset, &result);                                  status = fns->write(file, s->p, length, offset, &result);
# Line 441  rdpdr_process_irp(STREAM s) Line 480  rdpdr_process_irp(STREAM s)
480    
481                          // Add to table                          // Add to table
482                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
483                            if (!pst_buf)
484                            {
485                                    status = STATUS_CANCELLED;
486                                    break;
487                            }
488    
489                          in_uint8a(s, pst_buf, length);                          in_uint8a(s, pst_buf, length);
490    
491                          if (add_async_iorequest                          if (add_async_iorequest
492                              (device, file, id, major, length, fns, 0, 0, pst_buf))                              (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
493                          {                          {
494                                  status = STATUS_PENDING;                                  status = STATUS_PENDING;
495                                  break;                                  break;
# Line 564  rdpdr_process_irp(STREAM s) Line 609  rdpdr_process_irp(STREAM s)
609                          in_uint8s(s, 0x14);                          in_uint8s(s, 0x14);
610    
611                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
612                            if (!buffer)
613                            {
614                                    status = STATUS_CANCELLED;
615                                    break;
616                            }
617    
618                          out.data = out.p = buffer;                          out.data = out.p = buffer;
619                          out.size = sizeof(buffer);                          out.size = sizeof(buffer);
620                          status = fns->device_control(file, request, s, &out);                          status = fns->device_control(file, request, s, &out);
# Line 579  rdpdr_process_irp(STREAM s) Line 630  rdpdr_process_irp(STREAM s)
630          {          {
631                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
632          }          }
633          xfree(buffer);          if (buffer)
634                    xfree(buffer);
635            buffer = NULL;
636  }  }
637    
638  void  void
# Line 699  rdpdr_init() Line 751  rdpdr_init()
751  void  void
752  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)
753  {  {
754          long select_timeout = 0;        // Timeout value to be used for select() (in millisecons).          uint32 select_timeout = 0;      // Timeout value to be used for select() (in millisecons).
755          struct async_iorequest *iorq;          struct async_iorequest *iorq;
756    
757          iorq = &g_iorequest;          iorq = g_iorequest;
758          while (iorq != NULL)          while (iorq != NULL)
759          {          {
760                  if (iorq->fd != 0)                  if (iorq->fd != 0)
# Line 739  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 791  rdpdr_add_fds(int *n, fd_set * rfds, fd_
791          }          }
792  }  }
793    
794    struct async_iorequest *
795    rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
796    {
797            if (!iorq)
798                    return NULL;
799    
800            if (iorq->buffer)
801                    xfree(iorq->buffer);
802            if (prev)
803            {
804                    prev->next = iorq->next;
805                    xfree(iorq);
806                    iorq = prev->next;
807            }
808            else
809            {
810                    // Even if NULL
811                    g_iorequest = iorq->next;
812                    xfree(iorq);
813                    iorq = NULL;
814            }
815            return iorq;
816    }
817    
818  /* 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 */
819  void  void
# Line 749  rdpdr_check_fds(fd_set * rfds, fd_set * Line 824  rdpdr_check_fds(fd_set * rfds, fd_set *
824          DEVICE_FNS *fns;          DEVICE_FNS *fns;
825          struct async_iorequest *iorq;          struct async_iorequest *iorq;
826          struct async_iorequest *prev;          struct async_iorequest *prev;
827            uint32 req_size = 0;
828    
829          if (timed_out)          if (timed_out)
830          {          {
# Line 756  rdpdr_check_fds(fd_set * rfds, fd_set * Line 832  rdpdr_check_fds(fd_set * rfds, fd_set *
832                  return;                  return;
833          }          }
834    
835          iorq = &g_iorequest;          iorq = g_iorequest;
836          prev = NULL;          prev = NULL;
837          while (iorq != NULL)          while (iorq != NULL)
838          {          {
   
839                  if (iorq->fd != 0)                  if (iorq->fd != 0)
840                  {                  {
841                          switch (iorq->major)                          switch (iorq->major)
# Line 770  rdpdr_check_fds(fd_set * rfds, fd_set * Line 845  rdpdr_check_fds(fd_set * rfds, fd_set *
845                                          {                                          {
846                                                  /* Read the data */                                                  /* Read the data */
847                                                  fns = iorq->fns;                                                  fns = iorq->fns;
848    
849                                                    req_size =
850                                                            (iorq->length - iorq->partial_len) >
851                                                            8192 ? 8192 : (iorq->length -
852                                                                           iorq->partial_len);
853                                                    /* never read larger chunks than 8k - chances are that it will block */
854                                                  status = fns->read(iorq->fd,                                                  status = fns->read(iorq->fd,
855                                                                     iorq->buffer + iorq->partial_len,                                                                     iorq->buffer + iorq->partial_len,
856                                                                     iorq->length - iorq->partial_len,                                                                     req_size, iorq->offset, &result);
857                                                                     0, &result);  
858                                                  iorq->partial_len += result;                                                  if (result > 0)
859                                                    {
860                                                            iorq->partial_len += result;
861                                                            iorq->offset += result;
862                                                    }
863  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
864                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
865  #endif  #endif
866                                                  /* only delete link if all data has been transfered */                                                  /* only delete link if all data has been transfered */
867                                                  if (iorq->partial_len == iorq->length)                                                  /* or if result was 0 and status success - EOF      */
868                                                    if ((iorq->partial_len == iorq->length) ||
869                                                        (result == 0))
870                                                  {                                                  {
871                                                          /* send the data */  #if WITH_DEBUG_RDP5
872                                                          status = STATUS_SUCCESS;                                                          DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
873    #endif
874                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
875                                                                                iorq->id, status,                                                                                iorq->id, status,
876                                                                                iorq->length,                                                                                iorq->partial_len,
877                                                                                iorq->buffer, result);                                                                                iorq->buffer,
878                                                                                  iorq->partial_len);
879                                                          xfree(iorq->buffer);                                                          iorq = rdpdr_remove_iorequest(prev, iorq);
                                                         iorq->fd = 0;  
                                                         if (prev != NULL)  
                                                         {  
                                                                 prev->next = iorq->next;  
                                                                 xfree(iorq);  
                                                         }  
880                                                  }                                                  }
881                                          }                                          }
882                                          break;                                          break;
# Line 803  rdpdr_check_fds(fd_set * rfds, fd_set * Line 885  rdpdr_check_fds(fd_set * rfds, fd_set *
885                                          {                                          {
886                                                  /* Write data. */                                                  /* Write data. */
887                                                  fns = iorq->fns;                                                  fns = iorq->fns;
888    
889                                                    req_size =
890                                                            (iorq->length - iorq->partial_len) >
891                                                            8192 ? 8192 : (iorq->length -
892                                                                           iorq->partial_len);
893    
894                                                    /* never write larger chunks than 8k - chances are that it will block */
895                                                  status = fns->write(iorq->fd,                                                  status = fns->write(iorq->fd,
896                                                                      iorq->buffer +                                                                      iorq->buffer +
897                                                                      iorq->partial_len,                                                                      iorq->partial_len, req_size,
898                                                                      iorq->length -                                                                      iorq->offset, &result);
899                                                                      iorq->partial_len, 0, &result);  
900                                                  iorq->partial_len += result;                                                  if (result > 0)
901                                                    {
902                                                            iorq->partial_len += result;
903                                                            iorq->offset += result;
904                                                    }
905    
906  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
907                                                  DEBUG(("RDPDR: %d bytes of data written\n",                                                  DEBUG(("RDPDR: %d bytes of data written\n",
908                                                         result));                                                         result));
909  #endif  #endif
910                                                  /* only delete link if all data has been transfered */                                                  /* only delete link if all data has been transfered */
911                                                  if (iorq->partial_len == iorq->length)                                                  /* or we couldn't write */
912                                                    if ((iorq->partial_len == iorq->length)
913                                                        || (result == 0))
914                                                  {                                                  {
915                                                          /* send a status success */  #if WITH_DEBUG_RDP5
916                                                          status = STATUS_SUCCESS;                                                          DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
917    #endif
918                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
919                                                                                iorq->id, status,                                                                                iorq->id, status,
920                                                                                iorq->length, "", 1);                                                                                iorq->partial_len,
921                                                                                  (uint8 *) "", 1);
922    
923                                                          xfree(iorq->buffer);                                                          iorq = rdpdr_remove_iorequest(prev, iorq);
                                                         iorq->fd = 0;  
                                                         if (prev != NULL)  
                                                         {  
                                                                 prev->next = iorq->next;  
                                                                 xfree(iorq);  
                                                         }  
924                                                  }                                                  }
925                                          }                                          }
926                                          break;                                          break;
# Line 836  rdpdr_check_fds(fd_set * rfds, fd_set * Line 928  rdpdr_check_fds(fd_set * rfds, fd_set *
928    
929                  }                  }
930                  prev = iorq;                  prev = iorq;
931                  iorq = iorq->next;                  if (iorq)
932                            iorq = iorq->next;
933          }          }
934    
935  }  }
# Line 849  rdpdr_abort_io(uint32 fd, uint32 major, Line 942  rdpdr_abort_io(uint32 fd, uint32 major,
942          struct async_iorequest *iorq;          struct async_iorequest *iorq;
943          struct async_iorequest *prev;          struct async_iorequest *prev;
944    
945          iorq = &g_iorequest;          iorq = g_iorequest;
946          prev = NULL;          prev = NULL;
947          while (iorq != NULL)          while (iorq != NULL)
948          {          {
# Line 858  rdpdr_abort_io(uint32 fd, uint32 major, Line 951  rdpdr_abort_io(uint32 fd, uint32 major,
951                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
952                  {                  {
953                          result = 0;                          result = 0;
954                          rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
955                          xfree(iorq->buffer);                                                1);
956                          iorq->fd = 0;  
957                          if (prev != NULL)                          iorq = rdpdr_remove_iorequest(prev, iorq);
                         {  
                                 prev->next = iorq->next;  
                                 xfree(iorq);  
                         }  
958                          return True;                          return True;
959                  }                  }
960    

Legend:
Removed from v.592  
changed lines
  Added in v.627

  ViewVC Help
Powered by ViewVC 1.1.26