/[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 747 by astrand, Mon Aug 9 13:50:41 2004 UTC
# Line 1  Line 1 
1    /* -*- c-basic-offset: 8 -*-
2       rdesktop: A Remote Desktop Protocol client.
3       Copyright (C) Matthew Chapman 1999-2004
4    
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9    
10       This program is distributed in the hope that it will be useful,
11       but WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13       GNU General Public License for more details.
14    
15       You should have received a copy of the GNU General Public License
16       along with this program; if not, write to the Free Software
17       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18    */
19    
20    /*
21      Here are some resources, for your IRP hacking pleasure:
22    
23      http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/winddk.h?view=markup
24    
25      http://win32.mvps.org/ntfs/streams.cpp
26    
27      http://www.acc.umu.se/~bosse/ntifs.h
28    
29      http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/File/
30    
31      http://us1.samba.org/samba/ftp/specs/smb-nt01.txt
32    
33      http://www.osronline.com/
34    */
35    
36  #include <unistd.h>  #include <unistd.h>
37  #include <sys/types.h>  #include <sys/types.h>
38    #include <sys/time.h>
39    #include <dirent.h>             /* opendir, closedir, readdir */
40  #include <time.h>  #include <time.h>
41    #include <errno.h>
42  #include "rdesktop.h"  #include "rdesktop.h"
43    
 #define IRP_MJ_CREATE           0x00  
 #define IRP_MJ_CLOSE            0x02  
 #define IRP_MJ_READ             0x03  
 #define IRP_MJ_WRITE            0x04  
 #define IRP_MJ_DEVICE_CONTROL   0x0e  
   
44  #define IRP_MJ_CREATE                   0x00  #define IRP_MJ_CREATE                   0x00
45  #define IRP_MJ_CLOSE                    0x02  #define IRP_MJ_CLOSE                    0x02
46  #define IRP_MJ_READ                     0x03  #define IRP_MJ_READ                     0x03
# Line 18  Line 50 
50  #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a  #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
51  #define IRP_MJ_DIRECTORY_CONTROL        0x0c  #define IRP_MJ_DIRECTORY_CONTROL        0x0c
52  #define IRP_MJ_DEVICE_CONTROL           0x0e  #define IRP_MJ_DEVICE_CONTROL           0x0e
53    #define IRP_MJ_LOCK_CONTROL             0x11
54    
55  #define IRP_MN_QUERY_DIRECTORY          0x01  #define IRP_MN_QUERY_DIRECTORY          0x01
56  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02  #define IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02
57    
58  //#define MAX_ASYNC_IO_REQUESTS   10  extern char g_hostname[16];
   
 extern char hostname[16];  
59  extern DEVICE_FNS serial_fns;  extern DEVICE_FNS serial_fns;
60  extern DEVICE_FNS printer_fns;  extern DEVICE_FNS printer_fns;
61  extern DEVICE_FNS parallel_fns;  extern DEVICE_FNS parallel_fns;
62  extern DEVICE_FNS disk_fns;  extern DEVICE_FNS disk_fns;
63    extern FILEINFO g_fileinfo[];
64    
65  static VCHANNEL *rdpdr_channel;  static VCHANNEL *rdpdr_channel;
66    
# Line 39  uint32 g_num_devices; Line 70  uint32 g_num_devices;
70    
71  /* Table with information about rdpdr devices */  /* Table with information about rdpdr devices */
72  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
73    char *g_rdpdr_clientname = NULL;
74    
 #if 0  
75  /* 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 */
76    /* using a linked list ensures that they are processed in the right order, */
77    /* if multiple ios are being done on the same fd */
78  struct async_iorequest  struct async_iorequest
79  {  {
80          uint32 fd, major, minor, offset, device, id, length;          uint32 fd, major, minor, offset, device, id, length, partial_len;
81          long timeout,           /* Total timeout */          long timeout,           /* Total timeout */
82            itv_timeout;          /* Interval timeout (between serial characters) */            itv_timeout;          /* Interval timeout (between serial characters) */
83          uint8 *buffer;          uint8 *buffer;
84          DEVICE_FNS *fns;          DEVICE_FNS *fns;
85  } g_iorequest[MAX_ASYNC_IO_REQUESTS];  
86  #endif          struct async_iorequest *next;   /* next element in list */
87    };
88    
89    struct async_iorequest *g_iorequest;
90    
91  /* Return device_id for a given handle */  /* Return device_id for a given handle */
92  int  int
# Line 77  convert_to_unix_filename(char *filename) Line 113  convert_to_unix_filename(char *filename)
113          }          }
114  }  }
115    
116  #if 0  static BOOL
117    rdpdr_handle_ok(int device, int handle)
118    {
119            switch (g_rdpdr_device[device].device_type)
120            {
121                    case DEVICE_TYPE_PARALLEL:
122                    case DEVICE_TYPE_SERIAL:
123                    case DEVICE_TYPE_PRINTER:
124                    case DEVICE_TYPE_SCARD:
125                            if (g_rdpdr_device[device].handle != handle)
126                                    return False;
127                            break;
128                    case DEVICE_TYPE_DISK:
129                            if (g_fileinfo[handle].device_id != device)
130                                    return False;
131                            break;
132            }
133            return True;
134    }
135    
136  /* 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 */
137  BOOL  static BOOL
138  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,
139                      DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer)                      DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
140                        uint32 offset)
141  {  {
         int i;  
142          struct async_iorequest *iorq;          struct async_iorequest *iorq;
143    
144          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          if (g_iorequest == NULL)
145          {          {
146                  iorq = &g_iorequest[i];                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
147                    if (!g_iorequest)
148                            return False;
149                    g_iorequest->fd = 0;
150                    g_iorequest->next = NULL;
151            }
152    
153                  if (iorq->fd == 0)          iorq = g_iorequest;
154    
155            while (iorq->fd != 0)
156            {
157                    // create new element if needed
158                    if (iorq->next == NULL)
159                  {                  {
160                          iorq->device = device;                          iorq->next =
161                          iorq->fd = file;                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
162                          iorq->id = id;                          if (!iorq->next)
163                          iorq->major = major;                                  return False;
164                          iorq->length = length;                          iorq->next->fd = 0;
165                          iorq->fns = fns;                          iorq->next->next = NULL;
                         iorq->timeout = total_timeout;  
                         iorq->itv_timeout = interval_timeout;  
                         iorq->buffer = buffer;  
                         return True;  
166                  }                  }
167                    iorq = iorq->next;
168          }          }
169          error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n");          iorq->device = device;
170          return False;          iorq->fd = file;
171            iorq->id = id;
172            iorq->major = major;
173            iorq->length = length;
174            iorq->partial_len = 0;
175            iorq->fns = fns;
176            iorq->timeout = total_timeout;
177            iorq->itv_timeout = interval_timeout;
178            iorq->buffer = buffer;
179            iorq->offset = offset;
180            return True;
181  }  }
 #endif  
182    
183  void  static void
184  rdpdr_send_connect(void)  rdpdr_send_connect(void)
185  {  {
186          uint8 magic[4] = "rDCC";          uint8 magic[4] = "rDCC";
# Line 125  rdpdr_send_connect(void) Line 196  rdpdr_send_connect(void)
196  }  }
197    
198    
199  void  static void
200  rdpdr_send_name(void)  rdpdr_send_name(void)
201  {  {
202          uint8 magic[4] = "rDNC";          uint8 magic[4] = "rDNC";
         uint32 hostlen = (strlen(hostname) + 1) * 2;  
203          STREAM s;          STREAM s;
204            uint32 hostlen;
205    
206            if (NULL == g_rdpdr_clientname)
207            {
208                    g_rdpdr_clientname = g_hostname;
209            }
210            hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
211    
212          s = channel_init(rdpdr_channel, 16 + hostlen);          s = channel_init(rdpdr_channel, 16 + hostlen);
213          out_uint8a(s, magic, 4);          out_uint8a(s, magic, 4);
# Line 138  rdpdr_send_name(void) Line 215  rdpdr_send_name(void)
215          out_uint16_le(s, 0x72);          out_uint16_le(s, 0x72);
216          out_uint32(s, 0);          out_uint32(s, 0);
217          out_uint32_le(s, hostlen);          out_uint32_le(s, hostlen);
218          rdp_out_unistr(s, hostname, hostlen - 2);          rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
219          s_mark_end(s);          s_mark_end(s);
220          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
221  }  }
222    
223  /* Returns the size of the payload of the announce packet */  /* Returns the size of the payload of the announce packet */
224  int  static int
225  announcedata_size()  announcedata_size()
226  {  {
227          int size, i;          int size, i;
# Line 171  announcedata_size() Line 248  announcedata_size()
248          return size;          return size;
249  }  }
250    
251  void  static void
252  rdpdr_send_available(void)  rdpdr_send_available(void)
253  {  {
254    
# Line 189  rdpdr_send_available(void) Line 266  rdpdr_send_available(void)
266          {          {
267                  out_uint32_le(s, g_rdpdr_device[i].device_type);                  out_uint32_le(s, g_rdpdr_device[i].device_type);
268                  out_uint32_le(s, i);    /* RDP Device ID */                  out_uint32_le(s, i);    /* RDP Device ID */
269                    /* Is it possible to use share names longer than 8 chars?
270                       /astrand */
271                  out_uint8p(s, g_rdpdr_device[i].name, 8);                  out_uint8p(s, g_rdpdr_device[i].name, 8);
272    
273                  switch (g_rdpdr_device[i].device_type)                  switch (g_rdpdr_device[i].device_type)
# Line 210  rdpdr_send_available(void) Line 289  rdpdr_send_available(void)
289                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);                                  rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
290                                  out_uint8a(s, printerinfo->blob, bloblen);                                  out_uint8a(s, printerinfo->blob, bloblen);
291    
292                                  xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */                                  if (printerinfo->blob)
293                                            xfree(printerinfo->blob);       /* Blob is sent twice if reconnecting */
294                                  break;                                  break;
295                          default:                          default:
296                                  out_uint32(s, 0);                                  out_uint32(s, 0);
# Line 228  rdpdr_send_available(void) Line 308  rdpdr_send_available(void)
308          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
309  }  }
310    
311  void  static void
312  rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,  rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,
313                        uint32 length)                        uint32 length)
314  {  {
# Line 290  rdpdr_process_irp(STREAM s) Line 370  rdpdr_process_irp(STREAM s)
370                  case DEVICE_TYPE_SERIAL:                  case DEVICE_TYPE_SERIAL:
371    
372                          fns = &serial_fns;                          fns = &serial_fns;
373                          /* should be async when aio is finished */                          rw_blocking = False;
                         /*rw_blocking = False; */  
374                          break;                          break;
375    
376                  case DEVICE_TYPE_PARALLEL:                  case DEVICE_TYPE_PARALLEL:
377    
378                          fns = &parallel_fns;                          fns = &parallel_fns;
379                          /* should be async when aio is finished */                          rw_blocking = False;
                         /*rw_blocking = False;*/  
380                          break;                          break;
381    
382                  case DEVICE_TYPE_PRINTER:                  case DEVICE_TYPE_PRINTER:
# Line 309  rdpdr_process_irp(STREAM s) Line 387  rdpdr_process_irp(STREAM s)
387                  case DEVICE_TYPE_DISK:                  case DEVICE_TYPE_DISK:
388    
389                          fns = &disk_fns;                          fns = &disk_fns;
390                            rw_blocking = False;
391                          break;                          break;
392    
393                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
# Line 374  rdpdr_process_irp(STREAM s) Line 453  rdpdr_process_irp(STREAM s)
453  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
454                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
455  #endif  #endif
456  //                      if (rw_blocking)        // Complete read immediately                          if (!rdpdr_handle_ok(device, file))
457  //                      {                          {
458                                    status = STATUS_INVALID_HANDLE;
459                                    break;
460                            }
461    
462                            if (rw_blocking)        // Complete read immediately
463                            {
464                                  buffer = (uint8 *) xrealloc((void *) buffer, length);                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
465                                    if (!buffer)
466                                    {
467                                            status = STATUS_CANCELLED;
468                                            break;
469                                    }
470                                  status = fns->read(file, buffer, length, offset, &result);                                  status = fns->read(file, buffer, length, offset, &result);
471                                  buffer_len = result;                                  buffer_len = result;
472                                  break;                                  break;
473  //                      }                          }
474    
 #if 0  
475                          // Add request to table                          // Add request to table
476                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
477                            if (!pst_buf)
478                            {
479                                    status = STATUS_CANCELLED;
480                                    break;
481                            }
482                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);
483                          if (add_async_iorequest                          if (add_async_iorequest
484                              (device, file, id, major, length, fns, total_timeout, interval_timeout,                              (device, file, id, major, length, fns, total_timeout, interval_timeout,
485                               pst_buf))                               pst_buf, offset))
486                          {                          {
487                                  status = STATUS_PENDING;                                  status = STATUS_PENDING;
488                                  break;                                  break;
# Line 396  rdpdr_process_irp(STREAM s) Line 490  rdpdr_process_irp(STREAM s)
490    
491                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
492                          break;                          break;
 #endif  
493                  case IRP_MJ_WRITE:                  case IRP_MJ_WRITE:
494    
495                          buffer_len = 1;                          buffer_len = 1;
# Line 413  rdpdr_process_irp(STREAM s) Line 506  rdpdr_process_irp(STREAM s)
506  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
507                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));
508  #endif  #endif
509  //                      if (rw_blocking)        // Complete immediately                          if (!rdpdr_handle_ok(device, file))
510  //                      {                          {
511                                    status = STATUS_INVALID_HANDLE;
512                                    break;
513                            }
514    
515                            if (rw_blocking)        // Complete immediately
516                            {
517                                  status = fns->write(file, s->p, length, offset, &result);                                  status = fns->write(file, s->p, length, offset, &result);
518                                  break;                                  break;
519  //                      }                          }
520  #if 0  
521                          // Add to table                          // Add to table
522                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
523                            if (!pst_buf)
524                            {
525                                    status = STATUS_CANCELLED;
526                                    break;
527                            }
528    
529                          in_uint8a(s, pst_buf, length);                          in_uint8a(s, pst_buf, length);
530    
531                          if (add_async_iorequest                          if (add_async_iorequest
532                              (device, file, id, major, length, fns, 0, 0, pst_buf))                              (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
533                          {                          {
534                                  status = STATUS_PENDING;                                  status = STATUS_PENDING;
535                                  break;                                  break;
# Line 432  rdpdr_process_irp(STREAM s) Line 537  rdpdr_process_irp(STREAM s)
537    
538                          status = STATUS_CANCELLED;                          status = STATUS_CANCELLED;
539                          break;                          break;
 #endif  
540    
541                  case IRP_MJ_QUERY_INFORMATION:                  case IRP_MJ_QUERY_INFORMATION:
542    
# Line 545  rdpdr_process_irp(STREAM s) Line 649  rdpdr_process_irp(STREAM s)
649                          in_uint8s(s, 0x14);                          in_uint8s(s, 0x14);
650    
651                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
652                            if (!buffer)
653                            {
654                                    status = STATUS_CANCELLED;
655                                    break;
656                            }
657    
658                          out.data = out.p = buffer;                          out.data = out.p = buffer;
659                          out.size = sizeof(buffer);                          out.size = sizeof(buffer);
660                          status = fns->device_control(file, request, s, &out);                          status = fns->device_control(file, request, s, &out);
661                          result = buffer_len = out.p - out.data;                          result = buffer_len = out.p - out.data;
662                          break;                          break;
663    
664    
665                    case IRP_MJ_LOCK_CONTROL:
666    
667                            if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
668                            {
669                                    status = STATUS_INVALID_HANDLE;
670                                    break;
671                            }
672    
673                            in_uint32_le(s, info_level);
674    
675                            out.data = out.p = buffer;
676                            out.size = sizeof(buffer);
677                            /* FIXME: Perhaps consider actually *do*
678                               something here :-) */
679                            status = STATUS_SUCCESS;
680                            result = buffer_len = out.p - out.data;
681                            break;
682    
683                  default:                  default:
684                          unimpl("IRP major=0x%x minor=0x%x\n", major, minor);                          unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
685                          break;                          break;
# Line 560  rdpdr_process_irp(STREAM s) Line 689  rdpdr_process_irp(STREAM s)
689          {          {
690                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
691          }          }
692          xfree(buffer);          if (buffer)
693                    xfree(buffer);
694            buffer = NULL;
695  }  }
696    
697  void  static void
698  rdpdr_send_clientcapabilty(void)  rdpdr_send_clientcapabilty(void)
699  {  {
700          uint8 magic[4] = "rDPC";          uint8 magic[4] = "rDPC";
# Line 676  rdpdr_init() Line 806  rdpdr_init()
806          return (rdpdr_channel != NULL);          return (rdpdr_channel != NULL);
807  }  }
808    
 #if 0  
809  /* Add file descriptors of pending io request to select() */  /* Add file descriptors of pending io request to select() */
810  void  void
811  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)
812  {  {
813          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).  
814          struct async_iorequest *iorq;          struct async_iorequest *iorq;
815            char c;
816    
817          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
818            while (iorq != NULL)
819          {          {
820                  iorq = &g_iorequest[i];                  if (iorq->fd != 0)
   
                 if (iorq->fd != 0)      // Found a pending io request  
821                  {                  {
822                          switch (iorq->major)                          switch (iorq->major)
823                          {                          {
824                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
825                                            /* Is this FD valid? FDs will
826                                               be invalid when
827                                               reconnecting. FIXME: Real
828                                               support for reconnects. */
829    
830                                            if ((read(iorq->fd, &c, 0) != 0) && (errno == EBADF))
831                                                    break;
832    
833                                          FD_SET(iorq->fd, rfds);                                          FD_SET(iorq->fd, rfds);
834                                            *n = MAX(*n, iorq->fd);
835    
836                                          // Check if io request timeout is smaller than current (but not 0).                                          // Check if io request timeout is smaller than current (but not 0).
837                                          if (iorq->timeout                                          if (iorq->timeout
# Line 709  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 845  rdpdr_add_fds(int *n, fd_set * rfds, fd_
845                                                  tv->tv_usec = (select_timeout % 1000) * 1000;                                                  tv->tv_usec = (select_timeout % 1000) * 1000;
846                                                  *timeout = True;                                                  *timeout = True;
847                                          }                                          }
848    
849                                          break;                                          break;
850    
851                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
852                                            /* FD still valid? See above. */
853                                            if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
854                                                    break;
855    
856                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
857                                            *n = MAX(*n, iorq->fd);
858                                          break;                                          break;
859    
860                          }                          }
861                          *n = MAX(*n, iorq->fd);  
862                  }                  }
863    
864                    iorq = iorq->next;
865          }          }
866  }  }
867    
868    struct async_iorequest *
869    rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
870    {
871            if (!iorq)
872                    return NULL;
873    
874            if (iorq->buffer)
875                    xfree(iorq->buffer);
876            if (prev)
877            {
878                    prev->next = iorq->next;
879                    xfree(iorq);
880                    iorq = prev->next;
881            }
882            else
883            {
884                    // Even if NULL
885                    g_iorequest = iorq->next;
886                    xfree(iorq);
887                    iorq = NULL;
888            }
889            return iorq;
890    }
891    
892  /* 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 */
893  void  void
894  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
895  {  {
         int i;  
896          NTSTATUS status;          NTSTATUS status;
897          uint32 result = 0, buffer_len = 0;          uint32 result = 0;
898          DEVICE_FNS *fns;          DEVICE_FNS *fns;
899          struct async_iorequest *iorq;          struct async_iorequest *iorq;
900            struct async_iorequest *prev;
901            uint32 req_size = 0;
902    
903          if (timed_out)          if (timed_out)
904          {          {
# Line 739  rdpdr_check_fds(fd_set * rfds, fd_set * Line 906  rdpdr_check_fds(fd_set * rfds, fd_set *
906                  return;                  return;
907          }          }
908    
909          // Walk through array of pending io_rq's          iorq = g_iorequest;
910          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          prev = NULL;
911            while (iorq != NULL)
912          {          {
                 iorq = &g_iorequest[i];  
   
913                  if (iorq->fd != 0)                  if (iorq->fd != 0)
914                  {                  {
915                          switch (iorq->major)                          switch (iorq->major)
916                          {                          {
   
917                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
   
918                                          if (FD_ISSET(iorq->fd, rfds))                                          if (FD_ISSET(iorq->fd, rfds))
919                                          {                                          {
920                                                  // Read, and send data.                                                  /* Read the data */
921                                                  fns = iorq->fns;                                                  fns = iorq->fns;
922                                                  status = fns->read(iorq->fd, iorq->buffer,  
923                                                                     iorq->length, 0, &result);                                                  req_size =
924                                                  buffer_len = result;                                                          (iorq->length - iorq->partial_len) >
925                                                            8192 ? 8192 : (iorq->length -
926                                                  rdpdr_send_completion(iorq->device, iorq->id,                                                                         iorq->partial_len);
927                                                                        status, result, iorq->buffer,                                                  /* never read larger chunks than 8k - chances are that it will block */
928                                                                        buffer_len);                                                  status = fns->read(iorq->fd,
929                                                                       iorq->buffer + iorq->partial_len,
930                                                                       req_size, iorq->offset, &result);
931    
932                                                    if (result > 0)
933                                                    {
934                                                            iorq->partial_len += result;
935                                                            iorq->offset += result;
936                                                    }
937  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
938                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
939  #endif  #endif
940                                                  xfree(iorq->buffer);                                                  /* only delete link if all data has been transfered */
941                                                  iorq->fd = 0;                                                  /* or if result was 0 and status success - EOF      */
942                                                    if ((iorq->partial_len == iorq->length) ||
943                                                        (result == 0))
944                                                    {
945    #if WITH_DEBUG_RDP5
946                                                            DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
947    #endif
948                                                            rdpdr_send_completion(iorq->device,
949                                                                                  iorq->id, status,
950                                                                                  iorq->partial_len,
951                                                                                  iorq->buffer,
952                                                                                  iorq->partial_len);
953                                                            iorq = rdpdr_remove_iorequest(prev, iorq);
954                                                    }
955                                          }                                          }
956                                          break;                                          break;
   
957                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
   
958                                          if (FD_ISSET(iorq->fd, wfds))                                          if (FD_ISSET(iorq->fd, wfds))
959                                          {                                          {
960                                                  // Write data and send completion.                                                  /* Write data. */
961                                                  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);  
962    
963                                                  xfree(iorq->buffer);                                                  req_size =
964                                                  iorq->fd = 0;                                                          (iorq->length - iorq->partial_len) >
965                                                            8192 ? 8192 : (iorq->length -
966                                                                           iorq->partial_len);
967    
968                                                    /* never write larger chunks than 8k - chances are that it will block */
969                                                    status = fns->write(iorq->fd,
970                                                                        iorq->buffer +
971                                                                        iorq->partial_len, req_size,
972                                                                        iorq->offset, &result);
973    
974                                                    if (result > 0)
975                                                    {
976                                                            iorq->partial_len += result;
977                                                            iorq->offset += result;
978                                                    }
979    
980    #if WITH_DEBUG_RDP5
981                                                    DEBUG(("RDPDR: %d bytes of data written\n",
982                                                           result));
983    #endif
984                                                    /* only delete link if all data has been transfered */
985                                                    /* or we couldn't write */
986                                                    if ((iorq->partial_len == iorq->length)
987                                                        || (result == 0))
988                                                    {
989    #if WITH_DEBUG_RDP5
990                                                            DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
991    #endif
992                                                            rdpdr_send_completion(iorq->device,
993                                                                                  iorq->id, status,
994                                                                                  iorq->partial_len,
995                                                                                  (uint8 *) "", 1);
996    
997                                                            iorq = rdpdr_remove_iorequest(prev, iorq);
998                                                    }
999                                          }                                          }
1000                                          break;                                          break;
1001                          }                          }
1002    
1003                  }                  }
1004                    prev = iorq;
1005                    if (iorq)
1006                            iorq = iorq->next;
1007          }          }
1008    
1009  }  }
1010    
1011  /* 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 1013  BOOL
1013  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
1014  {  {
1015          uint32 result;          uint32 result;
         int i;  
1016          struct async_iorequest *iorq;          struct async_iorequest *iorq;
1017            struct async_iorequest *prev;
1018    
1019          for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++)          iorq = g_iorequest;
1020            prev = NULL;
1021            while (iorq != NULL)
1022          {          {
                 iorq = &g_iorequest[i];  
   
1023                  // 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.
1024                  // Abort read should not abort a write io request.                  // Abort read should not abort a write io request.
1025                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1026                  {                  {
1027                          result = 0;                          result = 0;
1028                          rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1);                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
1029                          xfree(iorq->buffer);                                                1);
1030                          iorq->fd = 0;  
1031                            iorq = rdpdr_remove_iorequest(prev, iorq);
1032                          return True;                          return True;
1033                  }                  }
1034    
1035                    prev = iorq;
1036                    iorq = iorq->next;
1037          }          }
1038    
1039          return False;          return False;
1040  }  }
 #endif  

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

  ViewVC Help
Powered by ViewVC 1.1.26