/[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 602 by stargo, Sat Feb 7 17:32: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 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 78  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          if (g_iorequest == NULL)          if (g_iorequest == NULL)
113          {          {
114                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
115                    if (!g_iorequest)
116                            return False;
117                  g_iorequest->fd = 0;                  g_iorequest->fd = 0;
118                  g_iorequest->next = NULL;                  g_iorequest->next = NULL;
119          }          }
# Line 101  add_async_iorequest(uint32 device, uint3 Line 127  add_async_iorequest(uint32 device, uint3
127                  {                  {
128                          iorq->next =                          iorq->next =
129                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
130                            if (!iorq->next)
131                                    return False;
132                          iorq->next->fd = 0;                          iorq->next->fd = 0;
133                          iorq->next->next = NULL;                          iorq->next->next = NULL;
134                  }                  }
# Line 116  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 384  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 394  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 421  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 429  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 552  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 567  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 687  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;
# Line 727  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 766  rdpdr_check_fds(fd_set * rfds, fd_set * Line 853  rdpdr_check_fds(fd_set * rfds, fd_set *
853                                                  /* never read larger chunks than 8k - chances are that it will block */                                                  /* 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                                                                     req_size, 0, &result);                                                                     req_size, iorq->offset, &result);
                                                 iorq->partial_len += 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
# Line 780  rdpdr_check_fds(fd_set * rfds, fd_set * Line 871  rdpdr_check_fds(fd_set * rfds, fd_set *
871  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
872                                                          DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));                                                          DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
873  #endif  #endif
                                                         /* send the data */  
                                                         status = STATUS_SUCCESS;  
874                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
875                                                                                iorq->id, status,                                                                                iorq->id, status,
876                                                                                iorq->partial_len,                                                                                iorq->partial_len,
877                                                                                iorq->buffer,                                                                                iorq->buffer,
878                                                                                iorq->partial_len);                                                                                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);  
                                                         }  
                                                         else  
                                                         {  
                                                                 // Even if NULL  
                                                                 g_iorequest = iorq->next;  
                                                                 xfree(iorq);  
                                                         }  
880                                                  }                                                  }
881                                          }                                          }
882                                          break;                                          break;
# Line 817  rdpdr_check_fds(fd_set * rfds, fd_set * Line 894  rdpdr_check_fds(fd_set * rfds, fd_set *
894                                                  /* never write larger chunks than 8k - chances are that it will block */                                                  /* 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, req_size, 0,                                                                      iorq->partial_len, req_size,
898                                                                      &result);                                                                      iorq->offset, &result);
899                                                  iorq->partial_len += result;  
900                                                    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));
# Line 832  rdpdr_check_fds(fd_set * rfds, fd_set * Line 915  rdpdr_check_fds(fd_set * rfds, fd_set *
915  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
916                                                          DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));                                                          DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
917  #endif  #endif
                                                         /* send a status success */  
                                                         status = STATUS_SUCCESS;  
918                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
919                                                                                iorq->id, status,                                                                                iorq->id, status,
920                                                                                iorq->partial_len, (uint8*)"",                                                                                iorq->partial_len,
921                                                                                1);                                                                                (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);  
                                                         }  
                                                         else  
                                                         {  
                                                                 // Even if NULL  
                                                                 g_iorequest = iorq->next;  
                                                                 xfree(iorq);  
                                                         }  
924                                                  }                                                  }
925                                          }                                          }
926                                          break;                                          break;
# Line 859  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 881  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, (uint8*)"", 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);  
                         }  
                         else  
                         {  
                                 // Even if NULL  
                                 g_iorequest = iorq->next;  
                                 xfree(iorq);  
                         }  
958                          return True;                          return True;
959                  }                  }
960    

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

  ViewVC Help
Powered by ViewVC 1.1.26