/[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 590 by n-ki, Thu Jan 29 12:27:21 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 22  Line 24 
24  #define IRP_MN_QUERY_DIRECTORY          0x01  #define IRP_MN_QUERY_DIRECTORY          0x01
25  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02
26    
 //#define MAX_ASYNC_IO_REQUESTS   10  
   
27  extern char hostname[16];  extern char hostname[16];
28  extern DEVICE_FNS serial_fns;  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 40  uint32 g_num_devices; Line 40  uint32 g_num_devices;
40  /* Table with information about rdpdr devices */  /* Table with information about rdpdr devices */
41  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
42    
 #if 0  
43  /* 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 */
44    /* using a linked list ensures that they are processed in the right order, */
45    /* if multiple ios are being done on the same fd */
46  struct async_iorequest  struct async_iorequest
47  {  {
48          uint32 fd, major, minor, offset, device, id, length;          uint32 fd, major, minor, offset, device, id, length, partial_len;
49          long timeout,           /* Total timeout */          long timeout,           /* Total timeout */
50            itv_timeout;          /* Interval timeout (between serial characters) */            itv_timeout;          /* Interval timeout (between serial characters) */
51          uint8 *buffer;          uint8 *buffer;
52          DEVICE_FNS *fns;          DEVICE_FNS *fns;
53  } g_iorequest[MAX_ASYNC_IO_REQUESTS];  
54  #endif          struct async_iorequest *next;   /* next element in list */
55    };
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 77  convert_to_unix_filename(char *filename) Line 81  convert_to_unix_filename(char *filename)
81          }          }
82  }  }
83    
84  #if 0  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  {  {
         int i;  
110          struct async_iorequest *iorq;          struct async_iorequest *iorq;
111    
112          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          if (g_iorequest == NULL)
113          {          {
114                  iorq = &g_iorequest[i];                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
115                    if (!g_iorequest)
116                  if (iorq->fd == 0)                          return False;
117                  {                  g_iorequest->fd = 0;
118                          iorq->device = device;                  g_iorequest->next = NULL;
119                          iorq->fd = file;          }
120                          iorq->id = id;  
121                          iorq->major = major;          iorq = g_iorequest;
122                          iorq->length = length;  
123                          iorq->fns = fns;          while (iorq->fd != 0)
124                          iorq->timeout = total_timeout;          {
125                          iorq->itv_timeout = interval_timeout;                  // create new element if needed
126                          iorq->buffer = buffer;                  if (iorq->next == NULL)
127                          return True;                  {
128                  }                          iorq->next =
129          }                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
130          error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n");                          if (!iorq->next)
131          return False;                                  return False;
132                            iorq->next->fd = 0;
133                            iorq->next->next = NULL;
134                    }
135                    iorq = iorq->next;
136            }
137            iorq->device = device;
138            iorq->fd = file;
139            iorq->id = id;
140            iorq->major = major;
141            iorq->length = length;
142            iorq->partial_len = 0;
143            iorq->fns = fns;
144            iorq->timeout = total_timeout;
145            iorq->itv_timeout = interval_timeout;
146            iorq->buffer = buffer;
147            iorq->offset = offset;
148            return True;
149  }  }
 #endif  
150    
151  void  void
152  rdpdr_send_connect(void)  rdpdr_send_connect(void)
# Line 210  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 290  rdpdr_process_irp(STREAM s) Line 330  rdpdr_process_irp(STREAM s)
330                  case DEVICE_TYPE_SERIAL:                  case DEVICE_TYPE_SERIAL:
331    
332                          fns = &serial_fns;                          fns = &serial_fns;
333                          /* should be async when aio is finished */                          rw_blocking = False;
                         /*rw_blocking = False; */  
334                          break;                          break;
335    
336                  case DEVICE_TYPE_PARALLEL:                  case DEVICE_TYPE_PARALLEL:
337    
338                          fns = &parallel_fns;                          fns = &parallel_fns;
339                          /* should be async when aio is finished */                          rw_blocking = False;
                         /*rw_blocking = False;*/  
340                          break;                          break;
341    
342                  case DEVICE_TYPE_PRINTER:                  case DEVICE_TYPE_PRINTER:
# Line 309  rdpdr_process_irp(STREAM s) Line 347  rdpdr_process_irp(STREAM s)
347                  case DEVICE_TYPE_DISK:                  case DEVICE_TYPE_DISK:
348    
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 374  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 (rw_blocking)        // Complete read immediately                          if (!rdpdr_handle_ok(device, file))
417  //                      {                          {
418                                    status = STATUS_INVALID_HANDLE;
419                                    break;
420                            }
421    
422                            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;
433  //                      }                          }
434    
 #if 0  
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 396  rdpdr_process_irp(STREAM s) Line 450  rdpdr_process_irp(STREAM s)
450    
451                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
452                          break;                          break;
 #endif  
453                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
454    
455                          buffer_len = 1;                          buffer_len = 1;
# Line 413  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 (rw_blocking)        // Complete immediately                          if (!rdpdr_handle_ok(device, file))
470  //                      {                          {
471                                    status = STATUS_INVALID_HANDLE;
472                                    break;
473                            }
474    
475                            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);
478                                  break;                                  break;
479  //                      }                          }
480  #if 0  
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 432  rdpdr_process_irp(STREAM s) Line 497  rdpdr_process_irp(STREAM s)
497    
498                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
499                          break;                          break;
 #endif  
500    
501                  case IRP_MJ_QUERY_INFORMATION:                  case IRP_MJ_QUERY_INFORMATION:
502    
# Line 545  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 560  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 676  rdpdr_init() Line 747  rdpdr_init()
747          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
748  }  }
749    
 #if 0  
750  /* Add file descriptors of pending io request to select() */  /* Add file descriptors of pending io request to select() */
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          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).  
755          struct async_iorequest *iorq;          struct async_iorequest *iorq;
756    
757          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
758            while (iorq != NULL)
759          {          {
760                  iorq = &g_iorequest[i];                  if (iorq->fd != 0)
   
                 if (iorq->fd != 0)      // Found a pending io request  
761                  {                  {
762                          switch (iorq->major)                          switch (iorq->major)
763                          {                          {
# Line 712  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 780  rdpdr_add_fds(int *n, fd_set * rfds, fd_
780                                          break;                                          break;
781    
782                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
783                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
784                                          break;                                          break;
785    
786                          }                          }
787                          *n = MAX(*n, iorq->fd);                          *n = MAX(*n, iorq->fd);
788                  }                  }
789    
790                    iorq = iorq->next;
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
820  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
821  {  {
         int i;  
822          NTSTATUS status;          NTSTATUS status;
823          uint32 result = 0, buffer_len = 0;          uint32 result = 0;
824          DEVICE_FNS *fns;          DEVICE_FNS *fns;
825          struct async_iorequest *iorq;          struct async_iorequest *iorq;
826            struct async_iorequest *prev;
827            uint32 req_size = 0;
828    
829          if (timed_out)          if (timed_out)
830          {          {
# Line 739  rdpdr_check_fds(fd_set * rfds, fd_set * Line 832  rdpdr_check_fds(fd_set * rfds, fd_set *
832                  return;                  return;
833          }          }
834    
835          // Walk through array of pending io_rq's          iorq = g_iorequest;
836          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          prev = NULL;
837            while (iorq != NULL)
838          {          {
                 iorq = &g_iorequest[i];  
   
839                  if (iorq->fd != 0)                  if (iorq->fd != 0)
840                  {                  {
841                          switch (iorq->major)                          switch (iorq->major)
842                          {                          {
   
843                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
   
844                                          if (FD_ISSET(iorq->fd, rfds))                                          if (FD_ISSET(iorq->fd, rfds))
845                                          {                                          {
846                                                  // Read, and send data.                                                  /* Read the data */
847                                                  fns = iorq->fns;                                                  fns = iorq->fns;
848                                                  status = fns->read(iorq->fd, iorq->buffer,  
849                                                                     iorq->length, 0, &result);                                                  req_size =
850                                                  buffer_len = result;                                                          (iorq->length - iorq->partial_len) >
851                                                            8192 ? 8192 : (iorq->length -
852                                                  rdpdr_send_completion(iorq->device, iorq->id,                                                                         iorq->partial_len);
853                                                                        status, result, iorq->buffer,                                                  /* never read larger chunks than 8k - chances are that it will block */
854                                                                        buffer_len);                                                  status = fns->read(iorq->fd,
855                                                                       iorq->buffer + iorq->partial_len,
856                                                                       req_size, iorq->offset, &result);
857    
858                                                    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                                                  xfree(iorq->buffer);                                                  /* only delete link if all data has been transfered */
867                                                  iorq->fd = 0;                                                  /* or if result was 0 and status success - EOF      */
868                                                    if ((iorq->partial_len == iorq->length) ||
869                                                        (result == 0))
870                                                    {
871    #if WITH_DEBUG_RDP5
872                                                            DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
873    #endif
874                                                            rdpdr_send_completion(iorq->device,
875                                                                                  iorq->id, status,
876                                                                                  iorq->partial_len,
877                                                                                  iorq->buffer,
878                                                                                  iorq->partial_len);
879                                                            iorq = rdpdr_remove_iorequest(prev, iorq);
880                                                    }
881                                          }                                          }
882                                          break;                                          break;
   
883                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
884                                          if (FD_ISSET(iorq->fd, wfds))                                          if (FD_ISSET(iorq->fd, wfds))
885                                          {                                          {
886                                                  // Write data and send completion.                                                  /* Write data. */
887                                                  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);  
888    
889                                                  xfree(iorq->buffer);                                                  req_size =
890                                                  iorq->fd = 0;                                                          (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,
896                                                                        iorq->buffer +
897                                                                        iorq->partial_len, req_size,
898                                                                        iorq->offset, &result);
899    
900                                                    if (result > 0)
901                                                    {
902                                                            iorq->partial_len += result;
903                                                            iorq->offset += result;
904                                                    }
905    
906    #if WITH_DEBUG_RDP5
907                                                    DEBUG(("RDPDR: %d bytes of data written\n",
908                                                           result));
909    #endif
910                                                    /* only delete link if all data has been transfered */
911                                                    /* or we couldn't write */
912                                                    if ((iorq->partial_len == iorq->length)
913                                                        || (result == 0))
914                                                    {
915    #if WITH_DEBUG_RDP5
916                                                            DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
917    #endif
918                                                            rdpdr_send_completion(iorq->device,
919                                                                                  iorq->id, status,
920                                                                                  iorq->partial_len,
921                                                                                  (uint8 *) "", 1);
922    
923                                                            iorq = rdpdr_remove_iorequest(prev, iorq);
924                                                    }
925                                          }                                          }
926                                          break;                                          break;
927                          }                          }
928    
929                  }                  }
930                    prev = iorq;
931                    if (iorq)
932                            iorq = iorq->next;
933          }          }
934    
935  }  }
936    
937  /* 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 939  BOOL
939  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
940  {  {
941          uint32 result;          uint32 result;
         int i;  
942          struct async_iorequest *iorq;          struct async_iorequest *iorq;
943            struct async_iorequest *prev;
944    
945          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
946            prev = NULL;
947            while (iorq != NULL)
948          {          {
                 iorq = &g_iorequest[i];  
   
949                  // 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.
950                  // Abort read should not abort a write io request.                  // Abort read should not abort a write io request.
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                            iorq = rdpdr_remove_iorequest(prev, iorq);
958                          return True;                          return True;
959                  }                  }
960    
961                    prev = iorq;
962                    iorq = iorq->next;
963          }          }
964    
965          return False;          return False;
966  }  }
 #endif  

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

  ViewVC Help
Powered by ViewVC 1.1.26