/[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 613 by n-ki, Mon Feb 23 10:34:18 2004 UTC revision 1327 by stargo, Fri Nov 3 19:56:42 2006 UTC
# Line 1  Line 1 
1    /* -*- c-basic-offset: 8 -*-
2       rdesktop: A Remote Desktop Protocol client.
3       Copyright (C) Matthew Chapman 1999-2005
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>  #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 19  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  extern char hostname[16];  extern char g_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    #ifdef WITH_SCARD
64    extern DEVICE_FNS scard_fns;
65    #endif
66    extern FILEINFO g_fileinfo[];
67    extern BOOL g_notify_stamp;
68    
69    #ifdef WITH_SCARD
70    VCHANNEL *rdpdr_channel;
71    #else
72  static VCHANNEL *rdpdr_channel;  static VCHANNEL *rdpdr_channel;
73    #endif
74    
75  /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */  /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
76  HANDLE g_min_timeout_fd;  NTHANDLE g_min_timeout_fd;
77  uint32 g_num_devices;  uint32 g_num_devices;
78    
79  /* Table with information about rdpdr devices */  /* Table with information about rdpdr devices */
80  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
81    char *g_rdpdr_clientname = NULL;
82    
83  /* 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 */
84  /* using a linked list ensures that they are processed in the right order, */  /* using a linked list ensures that they are processed in the right order, */
# Line 56  struct async_iorequest *g_iorequest; Line 98  struct async_iorequest *g_iorequest;
98    
99  /* Return device_id for a given handle */  /* Return device_id for a given handle */
100  int  int
101  get_device_index(HANDLE handle)  get_device_index(NTHANDLE handle)
102  {  {
103          int i;          int i;
104          for (i = 0; i < RDPDR_MAX_DEVICES; i++)          for (i = 0; i < RDPDR_MAX_DEVICES; i++)
# Line 79  convert_to_unix_filename(char *filename) Line 121  convert_to_unix_filename(char *filename)
121          }          }
122  }  }
123    
124    static BOOL
125    rdpdr_handle_ok(int device, int handle)
126    {
127            switch (g_rdpdr_device[device].device_type)
128            {
129                    case DEVICE_TYPE_PARALLEL:
130                    case DEVICE_TYPE_SERIAL:
131                    case DEVICE_TYPE_PRINTER:
132                    case DEVICE_TYPE_SCARD:
133                            if (g_rdpdr_device[device].handle != handle)
134                                    return False;
135                            break;
136                    case DEVICE_TYPE_DISK:
137                            if (g_fileinfo[handle].device_id != device)
138                                    return False;
139                            break;
140            }
141            return True;
142    }
143    
144  /* 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 */
145  BOOL  static BOOL
146  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,
147                      DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,                      DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
148                      uint32 offset)                      uint32 offset)
# Line 100  add_async_iorequest(uint32 device, uint3 Line 162  add_async_iorequest(uint32 device, uint3
162    
163          while (iorq->fd != 0)          while (iorq->fd != 0)
164          {          {
165                  // create new element if needed                  /* create new element if needed */
166                  if (iorq->next == NULL)                  if (iorq->next == NULL)
167                  {                  {
168                          iorq->next =                          iorq->next =
# Line 126  add_async_iorequest(uint32 device, uint3 Line 188  add_async_iorequest(uint32 device, uint3
188          return True;          return True;
189  }  }
190    
191  void  static void
192  rdpdr_send_connect(void)  rdpdr_send_connect(void)
193  {  {
194          uint8 magic[4] = "rDCC";          uint8 magic[4] = "rDCC";
# Line 142  rdpdr_send_connect(void) Line 204  rdpdr_send_connect(void)
204  }  }
205    
206    
207  void  static void
208  rdpdr_send_name(void)  rdpdr_send_name(void)
209  {  {
210          uint8 magic[4] = "rDNC";          uint8 magic[4] = "rDNC";
         uint32 hostlen = (strlen(hostname) + 1) * 2;  
211          STREAM s;          STREAM s;
212            uint32 hostlen;
213    
214            if (NULL == g_rdpdr_clientname)
215            {
216                    g_rdpdr_clientname = g_hostname;
217            }
218            hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
219    
220          s = channel_init(rdpdr_channel, 16 + hostlen);          s = channel_init(rdpdr_channel, 16 + hostlen);
221          out_uint8a(s, magic, 4);          out_uint8a(s, magic, 4);
# Line 155  rdpdr_send_name(void) Line 223  rdpdr_send_name(void)
223          out_uint16_le(s, 0x72);          out_uint16_le(s, 0x72);
224          out_uint32(s, 0);          out_uint32(s, 0);
225          out_uint32_le(s, hostlen);          out_uint32_le(s, hostlen);
226          rdp_out_unistr(s, hostname, hostlen - 2);          rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
227          s_mark_end(s);          s_mark_end(s);
228          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
229  }  }
230    
231  /* Returns the size of the payload of the announce packet */  /* Returns the size of the payload of the announce packet */
232  int  static int
233  announcedata_size()  announcedata_size()
234  {  {
235          int size, i;          int size, i;
236          PRINTER *printerinfo;          PRINTER *printerinfo;
237    
238          size = 8;               //static announce size          size = 8;               /* static announce size */
239          size += g_num_devices * 0x14;          size += g_num_devices * 0x14;
240    
241          for (i = 0; i < g_num_devices; i++)          for (i = 0; i < g_num_devices; i++)
# Line 188  announcedata_size() Line 256  announcedata_size()
256          return size;          return size;
257  }  }
258    
259  void  static void
260  rdpdr_send_available(void)  rdpdr_send_available(void)
261  {  {
262    
# Line 206  rdpdr_send_available(void) Line 274  rdpdr_send_available(void)
274          {          {
275                  out_uint32_le(s, g_rdpdr_device[i].device_type);                  out_uint32_le(s, g_rdpdr_device[i].device_type);
276                  out_uint32_le(s, i);    /* RDP Device ID */                  out_uint32_le(s, i);    /* RDP Device ID */
277                    /* Is it possible to use share names longer than 8 chars?
278                       /astrand */
279                  out_uint8p(s, g_rdpdr_device[i].name, 8);                  out_uint8p(s, g_rdpdr_device[i].name, 8);
280    
281                  switch (g_rdpdr_device[i].device_type)                  switch (g_rdpdr_device[i].device_type)
# Line 261  rdpdr_send_completion(uint32 device, uin Line 331  rdpdr_send_completion(uint32 device, uin
331          out_uint32_le(s, result);          out_uint32_le(s, result);
332          out_uint8p(s, buffer, length);          out_uint8p(s, buffer, length);
333          s_mark_end(s);          s_mark_end(s);
334          /* JIF          /* JIF */
335             hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */  #ifdef WITH_DEBUG_RDP5
336            printf("--> rdpdr_send_completion\n");
337            /* hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
338    #endif
339          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
340  }  }
341    
# Line 286  rdpdr_process_irp(STREAM s) Line 359  rdpdr_process_irp(STREAM s)
359                  error_mode,                  error_mode,
360                  share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;                  share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
361    
362          char filename[256];          char filename[PATH_MAX];
363          uint8 *buffer, *pst_buf;          uint8 *buffer, *pst_buf;
364          struct stream out;          struct stream out;
365          DEVICE_FNS *fns;          DEVICE_FNS *fns;
# Line 329  rdpdr_process_irp(STREAM s) Line 402  rdpdr_process_irp(STREAM s)
402                          break;                          break;
403    
404                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
405    #ifdef WITH_SCARD
406                            fns = &scard_fns;
407                            rw_blocking = False;
408                            break;
409    #endif
410                  default:                  default:
411    
412                          error("IRP for bad device %ld\n", device);                          error("IRP for bad device %ld\n", device);
# Line 340  rdpdr_process_irp(STREAM s) Line 418  rdpdr_process_irp(STREAM s)
418                  case IRP_MJ_CREATE:                  case IRP_MJ_CREATE:
419    
420                          in_uint32_be(s, desired_access);                          in_uint32_be(s, desired_access);
421                          in_uint8s(s, 0x08);     // unknown                          in_uint8s(s, 0x08);     /* unknown */
422                          in_uint32_le(s, error_mode);                          in_uint32_le(s, error_mode);
423                          in_uint32_le(s, share_mode);                          in_uint32_le(s, share_mode);
424                          in_uint32_le(s, disposition);                          in_uint32_le(s, disposition);
# Line 391  rdpdr_process_irp(STREAM s) Line 469  rdpdr_process_irp(STREAM s)
469  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
470                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
471  #endif  #endif
472                          if (rw_blocking)        // Complete read immediately                          if (!rdpdr_handle_ok(device, file))
473                            {
474                                    status = STATUS_INVALID_HANDLE;
475                                    break;
476                            }
477    
478                            if (rw_blocking)        /* Complete read immediately */
479                          {                          {
480                                  buffer = (uint8 *) xrealloc((void *) buffer, length);                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
481                                  if (!buffer)                                  if (!buffer)
# Line 404  rdpdr_process_irp(STREAM s) Line 488  rdpdr_process_irp(STREAM s)
488                                  break;                                  break;
489                          }                          }
490    
491                          // Add request to table                          /* Add request to table */
492                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
493                          if (!pst_buf)                          if (!pst_buf)
494                          {                          {
# Line 438  rdpdr_process_irp(STREAM s) Line 522  rdpdr_process_irp(STREAM s)
522  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
523                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));
524  #endif  #endif
525                          if (rw_blocking)        // Complete immediately                          if (!rdpdr_handle_ok(device, file))
526                            {
527                                    status = STATUS_INVALID_HANDLE;
528                                    break;
529                            }
530    
531                            if (rw_blocking)        /* Complete immediately */
532                          {                          {
533                                  status = fns->write(file, s->p, length, offset, &result);                                  status = fns->write(file, s->p, length, offset, &result);
534                                  break;                                  break;
535                          }                          }
536    
537                          // Add to table                          /* Add to table */
538                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
539                          if (!pst_buf)                          if (!pst_buf)
540                          {                          {
# Line 549  rdpdr_process_irp(STREAM s) Line 639  rdpdr_process_irp(STREAM s)
639                                  case IRP_MN_NOTIFY_CHANGE_DIRECTORY:                                  case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
640    
641                                          /* JIF                                          /* JIF
642                                             unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */                                             unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor);  */
643                                          status = STATUS_PENDING;        // Don't send completion packet  
644                                            in_uint32_le(s, info_level);    /* notify mask */
645    
646                                            g_notify_stamp = True;
647    
648                                            status = disk_create_notify(file, info_level);
649                                            result = 0;
650    
651                                            if (status == STATUS_PENDING)
652                                                    add_async_iorequest(device, file, id, major, length,
653                                                                        fns, 0, 0, NULL, 0);
654                                          break;                                          break;
655    
656                                  default:                                  default:
657    
658                                          status = STATUS_INVALID_PARAMETER;                                          status = STATUS_INVALID_PARAMETER;
659                                          /* JIF                                          /* JIF */
660                                             unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */                                          unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
661                          }                          }
662                          break;                          break;
663    
# Line 583  rdpdr_process_irp(STREAM s) Line 683  rdpdr_process_irp(STREAM s)
683    
684                          out.data = out.p = buffer;                          out.data = out.p = buffer;
685                          out.size = sizeof(buffer);                          out.size = sizeof(buffer);
686    
687                            DEBUG_SCARD(("[SMART-CARD TRACE]\n"));
688                            DEBUG_SCARD(("device 0x%.8x\n", device));
689                            DEBUG_SCARD(("file   0x%.8x\n", file));
690                            DEBUG_SCARD(("id     0x%.8x\n", id));
691    #ifdef WITH_SCARD
692                            scardSetInfo(device, id, bytes_out + 0x14);
693    #endif
694                          status = fns->device_control(file, request, s, &out);                          status = fns->device_control(file, request, s, &out);
695                          result = buffer_len = out.p - out.data;                          result = buffer_len = out.p - out.data;
696    
697                            DEBUG_SCARD(("[SMART-CARD TRACE] OUT 0x%.8x\n", status));
698    
699                            /* Serial SERIAL_WAIT_ON_MASK */
700                            if (status == STATUS_PENDING)
701                            {
702                                    if (add_async_iorequest
703                                        (device, file, id, major, length, fns, 0, 0, NULL, 0))
704                                    {
705                                            status = STATUS_PENDING;
706                                            break;
707                                    }
708                            }
709    #ifdef WITH_SCARD
710                            else if (status == (STATUS_PENDING | 0xC0000000))
711                                    status = STATUS_PENDING;
712    #endif
713                            break;
714    
715    
716                    case IRP_MJ_LOCK_CONTROL:
717    
718                            if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
719                            {
720                                    status = STATUS_INVALID_HANDLE;
721                                    break;
722                            }
723    
724                            in_uint32_le(s, info_level);
725    
726                            out.data = out.p = buffer;
727                            out.size = sizeof(buffer);
728                            /* FIXME: Perhaps consider actually *do*
729                               something here :-) */
730                            status = STATUS_SUCCESS;
731                            result = buffer_len = out.p - out.data;
732                          break;                          break;
733    
734                  default:                  default:
# Line 601  rdpdr_process_irp(STREAM s) Line 745  rdpdr_process_irp(STREAM s)
745          buffer = NULL;          buffer = NULL;
746  }  }
747    
748  void  static void
749  rdpdr_send_clientcapabilty(void)  rdpdr_send_clientcapabilty(void)
750  {  {
751          uint8 magic[4] = "rDPC";          uint8 magic[4] = "rDPC";
# Line 717  rdpdr_init() Line 861  rdpdr_init()
861  void  void
862  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)
863  {  {
864          uint32 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). */
865          struct async_iorequest *iorq;          struct async_iorequest *iorq;
866            char c;
867    
868          iorq = g_iorequest;          iorq = g_iorequest;
869          while (iorq != NULL)          while (iorq != NULL)
# Line 728  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 873  rdpdr_add_fds(int *n, fd_set * rfds, fd_
873                          switch (iorq->major)                          switch (iorq->major)
874                          {                          {
875                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
876                                            /* Is this FD valid? FDs will
877                                               be invalid when
878                                               reconnecting. FIXME: Real
879                                               support for reconnects. */
880    
881                                          FD_SET(iorq->fd, rfds);                                          FD_SET(iorq->fd, rfds);
882                                            *n = MAX(*n, iorq->fd);
883    
884                                          // Check if io request timeout is smaller than current (but not 0).                                          /* Check if io request timeout is smaller than current (but not 0). */
885                                          if (iorq->timeout                                          if (iorq->timeout
886                                              && (select_timeout == 0                                              && (select_timeout == 0
887                                                  || iorq->timeout < select_timeout))                                                  || iorq->timeout < select_timeout))
888                                          {                                          {
889                                                  // Set new timeout                                                  /* Set new timeout */
890                                                  select_timeout = iorq->timeout;                                                  select_timeout = iorq->timeout;
891                                                  g_min_timeout_fd = iorq->fd;    /* Remember fd */                                                  g_min_timeout_fd = iorq->fd;    /* Remember fd */
892                                                  tv->tv_sec = select_timeout / 1000;                                                  tv->tv_sec = select_timeout / 1000;
893                                                  tv->tv_usec = (select_timeout % 1000) * 1000;                                                  tv->tv_usec = (select_timeout % 1000) * 1000;
894                                                  *timeout = True;                                                  *timeout = True;
895                                                    break;
896                                            }
897                                            if (iorq->itv_timeout && iorq->partial_len > 0
898                                                && (select_timeout == 0
899                                                    || iorq->itv_timeout < select_timeout))
900                                            {
901                                                    /* Set new timeout */
902                                                    select_timeout = iorq->itv_timeout;
903                                                    g_min_timeout_fd = iorq->fd;    /* Remember fd */
904                                                    tv->tv_sec = select_timeout / 1000;
905                                                    tv->tv_usec = (select_timeout % 1000) * 1000;
906                                                    *timeout = True;
907                                                    break;
908                                          }                                          }
909                                          break;                                          break;
910    
911                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
912                                            /* FD still valid? See above. */
913                                            if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
914                                                    break;
915    
916                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
917                                            *n = MAX(*n, iorq->fd);
918                                            break;
919    
920                                    case IRP_MJ_DEVICE_CONTROL:
921                                            if (select_timeout > 5)
922                                                    select_timeout = 5;     /* serial event queue */
923                                          break;                                          break;
924    
925                          }                          }
926                          *n = MAX(*n, iorq->fd);  
927                  }                  }
928    
929                  iorq = iorq->next;                  iorq = iorq->next;
930          }          }
931  }  }
932    
933    struct async_iorequest *
934    rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
935    {
936            if (!iorq)
937                    return NULL;
938    
939            if (iorq->buffer)
940                    xfree(iorq->buffer);
941            if (prev)
942            {
943                    prev->next = iorq->next;
944                    xfree(iorq);
945                    iorq = prev->next;
946            }
947            else
948            {
949                    /* Even if NULL */
950                    g_iorequest = iorq->next;
951                    xfree(iorq);
952                    iorq = NULL;
953            }
954            return iorq;
955    }
956    
957  /* 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 */
958  void  static void
959  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  _rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
960  {  {
961          NTSTATUS status;          NTSTATUS status;
962          uint32 result = 0;          uint32 result = 0;
# Line 768  rdpdr_check_fds(fd_set * rfds, fd_set * Line 964  rdpdr_check_fds(fd_set * rfds, fd_set *
964          struct async_iorequest *iorq;          struct async_iorequest *iorq;
965          struct async_iorequest *prev;          struct async_iorequest *prev;
966          uint32 req_size = 0;          uint32 req_size = 0;
967            uint32 buffer_len;
968            struct stream out;
969            uint8 *buffer = NULL;
970    
971    
972          if (timed_out)          if (timed_out)
973          {          {
974                    /* check serial iv_timeout */
975    
976                    iorq = g_iorequest;
977                    prev = NULL;
978                    while (iorq != NULL)
979                    {
980                            if (iorq->fd == g_min_timeout_fd)
981                            {
982                                    if ((iorq->partial_len > 0) &&
983                                        (g_rdpdr_device[iorq->device].device_type ==
984                                         DEVICE_TYPE_SERIAL))
985                                    {
986    
987                                            /* iv_timeout between 2 chars, send partial_len */
988                                            /*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
989                                            rdpdr_send_completion(iorq->device,
990                                                                  iorq->id, STATUS_SUCCESS,
991                                                                  iorq->partial_len,
992                                                                  iorq->buffer, iorq->partial_len);
993                                            iorq = rdpdr_remove_iorequest(prev, iorq);
994                                            return;
995                                    }
996                                    else
997                                    {
998                                            break;
999                                    }
1000    
1001                            }
1002                            else
1003                            {
1004                                    break;
1005                            }
1006    
1007    
1008                            prev = iorq;
1009                            if (iorq)
1010                                    iorq = iorq->next;
1011    
1012                    }
1013    
1014                  rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);                  rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);
1015                  return;                  return;
1016          }          }
# Line 797  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1037  rdpdr_check_fds(fd_set * rfds, fd_set *
1037                                                  status = fns->read(iorq->fd,                                                  status = fns->read(iorq->fd,
1038                                                                     iorq->buffer + iorq->partial_len,                                                                     iorq->buffer + iorq->partial_len,
1039                                                                     req_size, iorq->offset, &result);                                                                     req_size, iorq->offset, &result);
                                                 iorq->partial_len += result;  
                                                 iorq->offset += result;  
1040    
1041                                                    if ((long) result > 0)
1042                                                    {
1043                                                            iorq->partial_len += result;
1044                                                            iorq->offset += result;
1045                                                    }
1046  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1047                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
1048  #endif  #endif
# Line 811  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1054  rdpdr_check_fds(fd_set * rfds, fd_set *
1054  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1055                                                          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));
1056  #endif  #endif
                                                         /* send the data */  
                                                         status = STATUS_SUCCESS;  
1057                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
1058                                                                                iorq->id, status,                                                                                iorq->id, status,
1059                                                                                iorq->partial_len,                                                                                iorq->partial_len,
1060                                                                                iorq->buffer,                                                                                iorq->buffer,
1061                                                                                iorq->partial_len);                                                                                iorq->partial_len);
1062                                                          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);  
                                                         }  
1063                                                  }                                                  }
1064                                          }                                          }
1065                                          break;                                          break;
# Line 850  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1079  rdpdr_check_fds(fd_set * rfds, fd_set *
1079                                                                      iorq->buffer +                                                                      iorq->buffer +
1080                                                                      iorq->partial_len, req_size,                                                                      iorq->partial_len, req_size,
1081                                                                      iorq->offset, &result);                                                                      iorq->offset, &result);
1082                                                  iorq->partial_len += result;  
1083                                                  iorq->offset += result;                                                  if ((long) result > 0)
1084                                                    {
1085                                                            iorq->partial_len += result;
1086                                                            iorq->offset += result;
1087                                                    }
1088    
1089  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1090                                                  DEBUG(("RDPDR: %d bytes of data written\n",                                                  DEBUG(("RDPDR: %d bytes of data written\n",
1091                                                         result));                                                         result));
# Line 864  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1098  rdpdr_check_fds(fd_set * rfds, fd_set *
1098  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1099                                                          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));
1100  #endif  #endif
                                                         /* send a status success */  
                                                         status = STATUS_SUCCESS;  
1101                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
1102                                                                                iorq->id, status,                                                                                iorq->id, status,
1103                                                                                iorq->partial_len,                                                                                iorq->partial_len,
1104                                                                                (uint8 *) "", 1);                                                                                (uint8 *) "", 1);
1105    
1106                                                          xfree(iorq->buffer);                                                          iorq = rdpdr_remove_iorequest(prev, iorq);
1107                                                          iorq->fd = 0;                                                  }
1108                                                          if (prev != NULL)                                          }
1109                                                          {                                          break;
1110                                                                  prev->next = iorq->next;                                  case IRP_MJ_DEVICE_CONTROL:
1111                                                                  xfree(iorq);                                          if (serial_get_event(iorq->fd, &result))
1112                                                          }                                          {
1113                                                          else                                                  buffer = (uint8 *) xrealloc((void *) buffer, 0x14);
1114                                                    out.data = out.p = buffer;
1115                                                    out.size = sizeof(buffer);
1116                                                    out_uint32_le(&out, result);
1117                                                    result = buffer_len = out.p - out.data;
1118                                                    status = STATUS_SUCCESS;
1119                                                    rdpdr_send_completion(iorq->device, iorq->id,
1120                                                                          status, result, buffer,
1121                                                                          buffer_len);
1122                                                    xfree(buffer);
1123                                                    iorq = rdpdr_remove_iorequest(prev, iorq);
1124                                            }
1125    
1126                                            break;
1127                            }
1128    
1129                    }
1130                    prev = iorq;
1131                    if (iorq)
1132                            iorq = iorq->next;
1133            }
1134    
1135            /* Check notify */
1136            iorq = g_iorequest;
1137            prev = NULL;
1138            while (iorq != NULL)
1139            {
1140                    if (iorq->fd != 0)
1141                    {
1142                            switch (iorq->major)
1143                            {
1144    
1145                                    case IRP_MJ_DIRECTORY_CONTROL:
1146                                            if (g_rdpdr_device[iorq->device].device_type ==
1147                                                DEVICE_TYPE_DISK)
1148                                            {
1149    
1150                                                    if (g_notify_stamp)
1151                                                    {
1152                                                            g_notify_stamp = False;
1153                                                            status = disk_check_notify(iorq->fd);
1154                                                            if (status != STATUS_PENDING)
1155                                                          {                                                          {
1156                                                                  // Even if NULL                                                                  rdpdr_send_completion(iorq->device,
1157                                                                  g_iorequest = iorq->next;                                                                                        iorq->id,
1158                                                                  xfree(iorq);                                                                                        status, 0,
1159                                                                                          NULL, 0);
1160                                                                    iorq = rdpdr_remove_iorequest(prev,
1161                                                                                                  iorq);
1162                                                          }                                                          }
1163                                                  }                                                  }
1164                                          }                                          }
1165                                          break;                                          break;
                         }  
1166    
1167    
1168    
1169                            }
1170                  }                  }
1171    
1172                  prev = iorq;                  prev = iorq;
1173                  iorq = iorq->next;                  if (iorq)
1174                            iorq = iorq->next;
1175          }          }
1176    
1177  }  }
1178    
1179    void
1180    rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
1181    {
1182            fd_set dummy;
1183    
1184    
1185            FD_ZERO(&dummy);
1186    
1187    
1188            /* fist check event queue only,
1189               any serial wait event must be done before read block will be sent
1190             */
1191    
1192            _rdpdr_check_fds(&dummy, &dummy, False);
1193            _rdpdr_check_fds(rfds, wfds, timed_out);
1194    }
1195    
1196    
1197  /* Abort a pending io request for a given handle and major */  /* Abort a pending io request for a given handle and major */
1198  BOOL  BOOL
1199  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
# Line 908  rdpdr_abort_io(uint32 fd, uint32 major, Line 1206  rdpdr_abort_io(uint32 fd, uint32 major,
1206          prev = NULL;          prev = NULL;
1207          while (iorq != NULL)          while (iorq != NULL)
1208          {          {
1209                  // 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.
1210                  // Abort read should not abort a write io request.                     Abort read should not abort a write io request. */
1211                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1212                  {                  {
1213                          result = 0;                          result = 0;
1214                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
1215                                                1);                                                1);
1216                          xfree(iorq->buffer);  
1217                          iorq->fd = 0;                          iorq = rdpdr_remove_iorequest(prev, iorq);
                         if (prev != NULL)  
                         {  
                                 prev->next = iorq->next;  
                                 xfree(iorq);  
                         }  
                         else  
                         {  
                                 // Even if NULL  
                                 g_iorequest = iorq->next;  
                                 xfree(iorq);  
                         }  
1218                          return True;                          return True;
1219                  }                  }
1220    

Legend:
Removed from v.613  
changed lines
  Added in v.1327

  ViewVC Help
Powered by ViewVC 1.1.26