/[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 1309 by stargo, Wed Nov 1 20:52:01 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>
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  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  static VCHANNEL *rdpdr_channel;  static VCHANNEL *rdpdr_channel;
70    
71  /* 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 */
72  HANDLE g_min_timeout_fd;  NTHANDLE g_min_timeout_fd;
73  uint32 g_num_devices;  uint32 g_num_devices;
74    
75  /* Table with information about rdpdr devices */  /* Table with information about rdpdr devices */
76  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];  RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
77    char *g_rdpdr_clientname = NULL;
78    
79  /* 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 */
80  /* 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 55  struct async_iorequest *g_iorequest; Line 94  struct async_iorequest *g_iorequest;
94    
95  /* Return device_id for a given handle */  /* Return device_id for a given handle */
96  int  int
97  get_device_index(HANDLE handle)  get_device_index(NTHANDLE handle)
98  {  {
99          int i;          int i;
100          for (i = 0; i < RDPDR_MAX_DEVICES; i++)          for (i = 0; i < RDPDR_MAX_DEVICES; i++)
# Line 78  convert_to_unix_filename(char *filename) Line 117  convert_to_unix_filename(char *filename)
117          }          }
118  }  }
119    
120    static BOOL
121    rdpdr_handle_ok(int device, int handle)
122    {
123            switch (g_rdpdr_device[device].device_type)
124            {
125                    case DEVICE_TYPE_PARALLEL:
126                    case DEVICE_TYPE_SERIAL:
127                    case DEVICE_TYPE_PRINTER:
128                    case DEVICE_TYPE_SCARD:
129                            if (g_rdpdr_device[device].handle != handle)
130                                    return False;
131                            break;
132                    case DEVICE_TYPE_DISK:
133                            if (g_fileinfo[handle].device_id != device)
134                                    return False;
135                            break;
136            }
137            return True;
138    }
139    
140  /* 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 */
141  BOOL  static BOOL
142  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,
143                      DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer)                      DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
144                        uint32 offset)
145  {  {
146          struct async_iorequest *iorq;          struct async_iorequest *iorq;
147    
148          if (g_iorequest == NULL)          if (g_iorequest == NULL)
149          {          {
150                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));                  g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
151                    if (!g_iorequest)
152                            return False;
153                  g_iorequest->fd = 0;                  g_iorequest->fd = 0;
154                  g_iorequest->next = NULL;                  g_iorequest->next = NULL;
155          }          }
# Line 96  add_async_iorequest(uint32 device, uint3 Line 158  add_async_iorequest(uint32 device, uint3
158    
159          while (iorq->fd != 0)          while (iorq->fd != 0)
160          {          {
161                  // create new element if needed                  /* create new element if needed */
162                  if (iorq->next == NULL)                  if (iorq->next == NULL)
163                  {                  {
164                          iorq->next =                          iorq->next =
165                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));                                  (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
166                            if (!iorq->next)
167                                    return False;
168                          iorq->next->fd = 0;                          iorq->next->fd = 0;
169                          iorq->next->next = NULL;                          iorq->next->next = NULL;
170                  }                  }
# Line 116  add_async_iorequest(uint32 device, uint3 Line 180  add_async_iorequest(uint32 device, uint3
180          iorq->timeout = total_timeout;          iorq->timeout = total_timeout;
181          iorq->itv_timeout = interval_timeout;          iorq->itv_timeout = interval_timeout;
182          iorq->buffer = buffer;          iorq->buffer = buffer;
183            iorq->offset = offset;
184          return True;          return True;
185  }  }
186    
187  void  static void
188  rdpdr_send_connect(void)  rdpdr_send_connect(void)
189  {  {
190          uint8 magic[4] = "rDCC";          uint8 magic[4] = "rDCC";
# Line 135  rdpdr_send_connect(void) Line 200  rdpdr_send_connect(void)
200  }  }
201    
202    
203  void  static void
204  rdpdr_send_name(void)  rdpdr_send_name(void)
205  {  {
206          uint8 magic[4] = "rDNC";          uint8 magic[4] = "rDNC";
         uint32 hostlen = (strlen(hostname) + 1) * 2;  
207          STREAM s;          STREAM s;
208            uint32 hostlen;
209    
210            if (NULL == g_rdpdr_clientname)
211            {
212                    g_rdpdr_clientname = g_hostname;
213            }
214            hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
215    
216          s = channel_init(rdpdr_channel, 16 + hostlen);          s = channel_init(rdpdr_channel, 16 + hostlen);
217          out_uint8a(s, magic, 4);          out_uint8a(s, magic, 4);
# Line 148  rdpdr_send_name(void) Line 219  rdpdr_send_name(void)
219          out_uint16_le(s, 0x72);          out_uint16_le(s, 0x72);
220          out_uint32(s, 0);          out_uint32(s, 0);
221          out_uint32_le(s, hostlen);          out_uint32_le(s, hostlen);
222          rdp_out_unistr(s, hostname, hostlen - 2);          rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
223          s_mark_end(s);          s_mark_end(s);
224          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
225  }  }
226    
227  /* Returns the size of the payload of the announce packet */  /* Returns the size of the payload of the announce packet */
228  int  static int
229  announcedata_size()  announcedata_size()
230  {  {
231          int size, i;          int size, i;
232          PRINTER *printerinfo;          PRINTER *printerinfo;
233    
234          size = 8;               //static announce size          size = 8;               /* static announce size */
235          size += g_num_devices * 0x14;          size += g_num_devices * 0x14;
236    
237          for (i = 0; i < g_num_devices; i++)          for (i = 0; i < g_num_devices; i++)
# Line 181  announcedata_size() Line 252  announcedata_size()
252          return size;          return size;
253  }  }
254    
255  void  static void
256  rdpdr_send_available(void)  rdpdr_send_available(void)
257  {  {
258    
# Line 199  rdpdr_send_available(void) Line 270  rdpdr_send_available(void)
270          {          {
271                  out_uint32_le(s, g_rdpdr_device[i].device_type);                  out_uint32_le(s, g_rdpdr_device[i].device_type);
272                  out_uint32_le(s, i);    /* RDP Device ID */                  out_uint32_le(s, i);    /* RDP Device ID */
273                    /* Is it possible to use share names longer than 8 chars?
274                       /astrand */
275                  out_uint8p(s, g_rdpdr_device[i].name, 8);                  out_uint8p(s, g_rdpdr_device[i].name, 8);
276    
277                  switch (g_rdpdr_device[i].device_type)                  switch (g_rdpdr_device[i].device_type)
# Line 254  rdpdr_send_completion(uint32 device, uin Line 327  rdpdr_send_completion(uint32 device, uin
327          out_uint32_le(s, result);          out_uint32_le(s, result);
328          out_uint8p(s, buffer, length);          out_uint8p(s, buffer, length);
329          s_mark_end(s);          s_mark_end(s);
330          /* JIF          /* JIF */
331             hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */  #ifdef WITH_DEBUG_RDP5
332            printf("--> rdpdr_send_completion\n");
333            /* hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
334    #endif
335          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
336  }  }
337    
# Line 279  rdpdr_process_irp(STREAM s) Line 355  rdpdr_process_irp(STREAM s)
355                  error_mode,                  error_mode,
356                  share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;                  share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
357    
358          char filename[256];          char filename[PATH_MAX];
359          uint8 *buffer, *pst_buf;          uint8 *buffer, *pst_buf;
360          struct stream out;          struct stream out;
361          DEVICE_FNS *fns;          DEVICE_FNS *fns;
# Line 322  rdpdr_process_irp(STREAM s) Line 398  rdpdr_process_irp(STREAM s)
398                          break;                          break;
399    
400                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
401    #ifdef WITH_SCARD
402                            fns = &scard_fns;
403                            rw_blocking = False;
404                            break;
405    #endif
406                  default:                  default:
407    
408                          error("IRP for bad device %ld\n", device);                          error("IRP for bad device %ld\n", device);
# Line 333  rdpdr_process_irp(STREAM s) Line 414  rdpdr_process_irp(STREAM s)
414                  case IRP_MJ_CREATE:                  case IRP_MJ_CREATE:
415    
416                          in_uint32_be(s, desired_access);                          in_uint32_be(s, desired_access);
417                          in_uint8s(s, 0x08);     // unknown                          in_uint8s(s, 0x08);     /* unknown */
418                          in_uint32_le(s, error_mode);                          in_uint32_le(s, error_mode);
419                          in_uint32_le(s, share_mode);                          in_uint32_le(s, share_mode);
420                          in_uint32_le(s, disposition);                          in_uint32_le(s, disposition);
# Line 384  rdpdr_process_irp(STREAM s) Line 465  rdpdr_process_irp(STREAM s)
465  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
466                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));                          DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
467  #endif  #endif
468                          if (rw_blocking)        // Complete read immediately                          if (!rdpdr_handle_ok(device, file))
469                            {
470                                    status = STATUS_INVALID_HANDLE;
471                                    break;
472                            }
473    
474                            if (rw_blocking)        /* Complete read immediately */
475                          {                          {
476                                  buffer = (uint8 *) xrealloc((void *) buffer, length);                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
477                                    if (!buffer)
478                                    {
479                                            status = STATUS_CANCELLED;
480                                            break;
481                                    }
482                                  status = fns->read(file, buffer, length, offset, &result);                                  status = fns->read(file, buffer, length, offset, &result);
483                                  buffer_len = result;                                  buffer_len = result;
484                                  break;                                  break;
485                          }                          }
486    
487                          // Add request to table                          /* Add request to table */
488                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
489                            if (!pst_buf)
490                            {
491                                    status = STATUS_CANCELLED;
492                                    break;
493                            }
494                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);                          serial_get_timeout(file, length, &total_timeout, &interval_timeout);
495                          if (add_async_iorequest                          if (add_async_iorequest
496                              (device, file, id, major, length, fns, total_timeout, interval_timeout,                              (device, file, id, major, length, fns, total_timeout, interval_timeout,
497                               pst_buf))                               pst_buf, offset))
498                          {                          {
499                                  status = STATUS_PENDING;                                  status = STATUS_PENDING;
500                                  break;                                  break;
# Line 421  rdpdr_process_irp(STREAM s) Line 518  rdpdr_process_irp(STREAM s)
518  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
519                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));                          DEBUG(("RDPDR IRP Write (length: %d)\n", result));
520  #endif  #endif
521                          if (rw_blocking)        // Complete immediately                          if (!rdpdr_handle_ok(device, file))
522                            {
523                                    status = STATUS_INVALID_HANDLE;
524                                    break;
525                            }
526    
527                            if (rw_blocking)        /* Complete immediately */
528                          {                          {
529                                  status = fns->write(file, s->p, length, offset, &result);                                  status = fns->write(file, s->p, length, offset, &result);
530                                  break;                                  break;
531                          }                          }
532    
533                          // Add to table                          /* Add to table */
534                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
535                            if (!pst_buf)
536                            {
537                                    status = STATUS_CANCELLED;
538                                    break;
539                            }
540    
541                          in_uint8a(s, pst_buf, length);                          in_uint8a(s, pst_buf, length);
542    
543                          if (add_async_iorequest                          if (add_async_iorequest
544                              (device, file, id, major, length, fns, 0, 0, pst_buf))                              (device, file, id, major, length, fns, 0, 0, pst_buf, offset))
545                          {                          {
546                                  status = STATUS_PENDING;                                  status = STATUS_PENDING;
547                                  break;                                  break;
# Line 526  rdpdr_process_irp(STREAM s) Line 635  rdpdr_process_irp(STREAM s)
635                                  case IRP_MN_NOTIFY_CHANGE_DIRECTORY:                                  case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
636    
637                                          /* JIF                                          /* JIF
638                                             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);  */
639                                          status = STATUS_PENDING;        // Don't send completion packet  
640                                            in_uint32_le(s, info_level);    /* notify mask */
641    
642                                            g_notify_stamp = True;
643    
644                                            status = disk_create_notify(file, info_level);
645                                            result = 0;
646    
647                                            if (status == STATUS_PENDING)
648                                                    add_async_iorequest(device, file, id, major, length,
649                                                                        fns, 0, 0, NULL, 0);
650                                          break;                                          break;
651    
652                                  default:                                  default:
653    
654                                          status = STATUS_INVALID_PARAMETER;                                          status = STATUS_INVALID_PARAMETER;
655                                          /* JIF                                          /* JIF */
656                                             unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */                                          unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
657                          }                          }
658                          break;                          break;
659    
# Line 552  rdpdr_process_irp(STREAM s) Line 671  rdpdr_process_irp(STREAM s)
671                          in_uint8s(s, 0x14);                          in_uint8s(s, 0x14);
672    
673                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);                          buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
674                            if (!buffer)
675                            {
676                                    status = STATUS_CANCELLED;
677                                    break;
678                            }
679    
680                          out.data = out.p = buffer;                          out.data = out.p = buffer;
681                          out.size = sizeof(buffer);                          out.size = sizeof(buffer);
682    #ifdef  WITH_SCARD_DEBUG
683                            printf("[SMART-CARD TRACE]\n");
684                            printf("device 0x%.8x\n", device);
685                            printf("file   0x%.8x\n", file);
686                            printf("id     0x%.8x\n", id);
687    #endif
688    #ifdef WITH_SCARD
689                            scardSetInfo(device, id, bytes_out + 0x14);
690    #endif
691                          status = fns->device_control(file, request, s, &out);                          status = fns->device_control(file, request, s, &out);
692                          result = buffer_len = out.p - out.data;                          result = buffer_len = out.p - out.data;
693    #ifdef  WITH_SCARD_DEBUG
694                            printf("[SMART-CARD TRACE] OUT 0x%.8x\n", status);
695    #endif
696                            /* Serial SERIAL_WAIT_ON_MASK */
697                            if (status == STATUS_PENDING)
698                            {
699                                    if (add_async_iorequest
700                                        (device, file, id, major, length, fns, 0, 0, NULL, 0))
701                                    {
702                                            status = STATUS_PENDING;
703                                            break;
704                                    }
705                            }
706    #ifdef WITH_SCARD
707                            else if (status == (STATUS_PENDING | 0xC0000000))
708                                    status = STATUS_PENDING;
709    #endif
710                            break;
711    
712    
713                    case IRP_MJ_LOCK_CONTROL:
714    
715                            if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
716                            {
717                                    status = STATUS_INVALID_HANDLE;
718                                    break;
719                            }
720    
721                            in_uint32_le(s, info_level);
722    
723                            out.data = out.p = buffer;
724                            out.size = sizeof(buffer);
725                            /* FIXME: Perhaps consider actually *do*
726                               something here :-) */
727                            status = STATUS_SUCCESS;
728                            result = buffer_len = out.p - out.data;
729                          break;                          break;
730    
731                  default:                  default:
# Line 567  rdpdr_process_irp(STREAM s) Line 737  rdpdr_process_irp(STREAM s)
737          {          {
738                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);                  rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
739          }          }
740          xfree(buffer);          if (buffer)
741                    xfree(buffer);
742            buffer = NULL;
743  }  }
744    
745  void  static void
746  rdpdr_send_clientcapabilty(void)  rdpdr_send_clientcapabilty(void)
747  {  {
748          uint8 magic[4] = "rDPC";          uint8 magic[4] = "rDPC";
# Line 687  rdpdr_init() Line 858  rdpdr_init()
858  void  void
859  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)
860  {  {
861          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). */
862          struct async_iorequest *iorq;          struct async_iorequest *iorq;
863            char c;
864    
865          iorq = g_iorequest;          iorq = g_iorequest;
866          while (iorq != NULL)          while (iorq != NULL)
# Line 698  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 870  rdpdr_add_fds(int *n, fd_set * rfds, fd_
870                          switch (iorq->major)                          switch (iorq->major)
871                          {                          {
872                                  case IRP_MJ_READ:                                  case IRP_MJ_READ:
873                                            /* Is this FD valid? FDs will
874                                               be invalid when
875                                               reconnecting. FIXME: Real
876                                               support for reconnects. */
877    
878                                          FD_SET(iorq->fd, rfds);                                          FD_SET(iorq->fd, rfds);
879                                            *n = MAX(*n, iorq->fd);
880    
881                                          // Check if io request timeout is smaller than current (but not 0).                                          /* Check if io request timeout is smaller than current (but not 0). */
882                                          if (iorq->timeout                                          if (iorq->timeout
883                                              && (select_timeout == 0                                              && (select_timeout == 0
884                                                  || iorq->timeout < select_timeout))                                                  || iorq->timeout < select_timeout))
885                                          {                                          {
886                                                  // Set new timeout                                                  /* Set new timeout */
887                                                  select_timeout = iorq->timeout;                                                  select_timeout = iorq->timeout;
888                                                  g_min_timeout_fd = iorq->fd;    /* Remember fd */                                                  g_min_timeout_fd = iorq->fd;    /* Remember fd */
889                                                  tv->tv_sec = select_timeout / 1000;                                                  tv->tv_sec = select_timeout / 1000;
890                                                  tv->tv_usec = (select_timeout % 1000) * 1000;                                                  tv->tv_usec = (select_timeout % 1000) * 1000;
891                                                  *timeout = True;                                                  *timeout = True;
892                                                    break;
893                                            }
894                                            if (iorq->itv_timeout && iorq->partial_len > 0
895                                                && (select_timeout == 0
896                                                    || iorq->itv_timeout < select_timeout))
897                                            {
898                                                    /* Set new timeout */
899                                                    select_timeout = iorq->itv_timeout;
900                                                    g_min_timeout_fd = iorq->fd;    /* Remember fd */
901                                                    tv->tv_sec = select_timeout / 1000;
902                                                    tv->tv_usec = (select_timeout % 1000) * 1000;
903                                                    *timeout = True;
904                                                    break;
905                                          }                                          }
906                                          break;                                          break;
907    
908                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
909                                            /* FD still valid? See above. */
910                                            if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
911                                                    break;
912    
913                                          FD_SET(iorq->fd, wfds);                                          FD_SET(iorq->fd, wfds);
914                                            *n = MAX(*n, iorq->fd);
915                                            break;
916    
917                                    case IRP_MJ_DEVICE_CONTROL:
918                                            if (select_timeout > 5)
919                                                    select_timeout = 5;     /* serial event queue */
920                                          break;                                          break;
921    
922                          }                          }
923                          *n = MAX(*n, iorq->fd);  
924                  }                  }
925    
926                  iorq = iorq->next;                  iorq = iorq->next;
927          }          }
928  }  }
929    
930    struct async_iorequest *
931    rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
932    {
933            if (!iorq)
934                    return NULL;
935    
936            if (iorq->buffer)
937                    xfree(iorq->buffer);
938            if (prev)
939            {
940                    prev->next = iorq->next;
941                    xfree(iorq);
942                    iorq = prev->next;
943            }
944            else
945            {
946                    /* Even if NULL */
947                    g_iorequest = iorq->next;
948                    xfree(iorq);
949                    iorq = NULL;
950            }
951            return iorq;
952    }
953    
954  /* 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 */
955  void  static void
956  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  _rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
957  {  {
958          NTSTATUS status;          NTSTATUS status;
959          uint32 result = 0;          uint32 result = 0;
# Line 738  rdpdr_check_fds(fd_set * rfds, fd_set * Line 961  rdpdr_check_fds(fd_set * rfds, fd_set *
961          struct async_iorequest *iorq;          struct async_iorequest *iorq;
962          struct async_iorequest *prev;          struct async_iorequest *prev;
963          uint32 req_size = 0;          uint32 req_size = 0;
964            uint32 buffer_len;
965            struct stream out;
966            uint8 *buffer = NULL;
967    
968    
969          if (timed_out)          if (timed_out)
970          {          {
971                    /* check serial iv_timeout */
972    
973                    iorq = g_iorequest;
974                    prev = NULL;
975                    while (iorq != NULL)
976                    {
977                            if (iorq->fd == g_min_timeout_fd)
978                            {
979                                    if ((iorq->partial_len > 0) &&
980                                        (g_rdpdr_device[iorq->device].device_type ==
981                                         DEVICE_TYPE_SERIAL))
982                                    {
983    
984                                            /* iv_timeout between 2 chars, send partial_len */
985                                            /*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
986                                            rdpdr_send_completion(iorq->device,
987                                                                  iorq->id, STATUS_SUCCESS,
988                                                                  iorq->partial_len,
989                                                                  iorq->buffer, iorq->partial_len);
990                                            iorq = rdpdr_remove_iorequest(prev, iorq);
991                                            return;
992                                    }
993                                    else
994                                    {
995                                            break;
996                                    }
997    
998                            }
999                            else
1000                            {
1001                                    break;
1002                            }
1003    
1004    
1005                            prev = iorq;
1006                            if (iorq)
1007                                    iorq = iorq->next;
1008    
1009                    }
1010    
1011                  rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);                  rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);
1012                  return;                  return;
1013          }          }
# Line 766  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1033  rdpdr_check_fds(fd_set * rfds, fd_set *
1033                                                  /* never read larger chunks than 8k - chances are that it will block */                                                  /* never read larger chunks than 8k - chances are that it will block */
1034                                                  status = fns->read(iorq->fd,                                                  status = fns->read(iorq->fd,
1035                                                                     iorq->buffer + iorq->partial_len,                                                                     iorq->buffer + iorq->partial_len,
1036                                                                     req_size, 0, &result);                                                                     req_size, iorq->offset, &result);
                                                 iorq->partial_len += result;  
1037    
1038                                                    if ((long) result > 0)
1039                                                    {
1040                                                            iorq->partial_len += result;
1041                                                            iorq->offset += result;
1042                                                    }
1043  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1044                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));                                                  DEBUG(("RDPDR: %d bytes of data read\n", result));
1045  #endif  #endif
# Line 780  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1051  rdpdr_check_fds(fd_set * rfds, fd_set *
1051  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1052                                                          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));
1053  #endif  #endif
                                                         /* send the data */  
                                                         status = STATUS_SUCCESS;  
1054                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
1055                                                                                iorq->id, status,                                                                                iorq->id, status,
1056                                                                                iorq->partial_len,                                                                                iorq->partial_len,
1057                                                                                iorq->buffer,                                                                                iorq->buffer,
1058                                                                                iorq->partial_len);                                                                                iorq->partial_len);
1059                                                          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);  
                                                         }  
1060                                                  }                                                  }
1061                                          }                                          }
1062                                          break;                                          break;
# Line 817  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1074  rdpdr_check_fds(fd_set * rfds, fd_set *
1074                                                  /* never write larger chunks than 8k - chances are that it will block */                                                  /* never write larger chunks than 8k - chances are that it will block */
1075                                                  status = fns->write(iorq->fd,                                                  status = fns->write(iorq->fd,
1076                                                                      iorq->buffer +                                                                      iorq->buffer +
1077                                                                      iorq->partial_len, req_size, 0,                                                                      iorq->partial_len, req_size,
1078                                                                      &result);                                                                      iorq->offset, &result);
1079                                                  iorq->partial_len += result;  
1080                                                    if ((long) result > 0)
1081                                                    {
1082                                                            iorq->partial_len += result;
1083                                                            iorq->offset += result;
1084                                                    }
1085    
1086  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1087                                                  DEBUG(("RDPDR: %d bytes of data written\n",                                                  DEBUG(("RDPDR: %d bytes of data written\n",
1088                                                         result));                                                         result));
# Line 832  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1095  rdpdr_check_fds(fd_set * rfds, fd_set *
1095  #if WITH_DEBUG_RDP5  #if WITH_DEBUG_RDP5
1096                                                          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));
1097  #endif  #endif
                                                         /* send a status success */  
                                                         status = STATUS_SUCCESS;  
1098                                                          rdpdr_send_completion(iorq->device,                                                          rdpdr_send_completion(iorq->device,
1099                                                                                iorq->id, status,                                                                                iorq->id, status,
1100                                                                                iorq->partial_len, (uint8*)"",                                                                                iorq->partial_len,
1101                                                                                1);                                                                                (uint8 *) "", 1);
1102    
1103                                                          xfree(iorq->buffer);                                                          iorq = rdpdr_remove_iorequest(prev, iorq);
1104                                                          iorq->fd = 0;                                                  }
1105                                                          if (prev != NULL)                                          }
1106                                                          {                                          break;
1107                                                                  prev->next = iorq->next;                                  case IRP_MJ_DEVICE_CONTROL:
1108                                                                  xfree(iorq);                                          if (serial_get_event(iorq->fd, &result))
1109                                                          }                                          {
1110                                                          else                                                  buffer = (uint8 *) xrealloc((void *) buffer, 0x14);
1111                                                    out.data = out.p = buffer;
1112                                                    out.size = sizeof(buffer);
1113                                                    out_uint32_le(&out, result);
1114                                                    result = buffer_len = out.p - out.data;
1115                                                    status = STATUS_SUCCESS;
1116                                                    rdpdr_send_completion(iorq->device, iorq->id,
1117                                                                          status, result, buffer,
1118                                                                          buffer_len);
1119                                                    xfree(buffer);
1120                                                    iorq = rdpdr_remove_iorequest(prev, iorq);
1121                                            }
1122    
1123                                            break;
1124                            }
1125    
1126                    }
1127                    prev = iorq;
1128                    if (iorq)
1129                            iorq = iorq->next;
1130            }
1131    
1132            /* Check notify */
1133            iorq = g_iorequest;
1134            prev = NULL;
1135            while (iorq != NULL)
1136            {
1137                    if (iorq->fd != 0)
1138                    {
1139                            switch (iorq->major)
1140                            {
1141    
1142                                    case IRP_MJ_DIRECTORY_CONTROL:
1143                                            if (g_rdpdr_device[iorq->device].device_type ==
1144                                                DEVICE_TYPE_DISK)
1145                                            {
1146    
1147                                                    if (g_notify_stamp)
1148                                                    {
1149                                                            g_notify_stamp = False;
1150                                                            status = disk_check_notify(iorq->fd);
1151                                                            if (status != STATUS_PENDING)
1152                                                          {                                                          {
1153                                                                  // Even if NULL                                                                  rdpdr_send_completion(iorq->device,
1154                                                                  g_iorequest = iorq->next;                                                                                        iorq->id,
1155                                                                  xfree(iorq);                                                                                        status, 0,
1156                                                                                          NULL, 0);
1157                                                                    iorq = rdpdr_remove_iorequest(prev,
1158                                                                                                  iorq);
1159                                                          }                                                          }
1160                                                  }                                                  }
1161                                          }                                          }
1162                                          break;                                          break;
                         }  
1163    
1164    
1165    
1166                            }
1167                  }                  }
1168    
1169                  prev = iorq;                  prev = iorq;
1170                  iorq = iorq->next;                  if (iorq)
1171                            iorq = iorq->next;
1172          }          }
1173    
1174  }  }
1175    
1176    void
1177    rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
1178    {
1179            fd_set dummy;
1180    
1181    
1182            FD_ZERO(&dummy);
1183    
1184    
1185            /* fist check event queue only,
1186               any serial wait event must be done before read block will be sent
1187             */
1188    
1189            _rdpdr_check_fds(&dummy, &dummy, False);
1190            _rdpdr_check_fds(rfds, wfds, timed_out);
1191    }
1192    
1193    
1194  /* Abort a pending io request for a given handle and major */  /* Abort a pending io request for a given handle and major */
1195  BOOL  BOOL
1196  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
# Line 876  rdpdr_abort_io(uint32 fd, uint32 major, Line 1203  rdpdr_abort_io(uint32 fd, uint32 major,
1203          prev = NULL;          prev = NULL;
1204          while (iorq != NULL)          while (iorq != NULL)
1205          {          {
1206                  // 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.
1207                  // Abort read should not abort a write io request.                     Abort read should not abort a write io request. */
1208                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1209                  {                  {
1210                          result = 0;                          result = 0;
1211                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8*)"", 1);                          rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
1212                          xfree(iorq->buffer);                                                1);
1213                          iorq->fd = 0;  
1214                          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);  
                         }  
1215                          return True;                          return True;
1216                  }                  }
1217    

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

  ViewVC Help
Powered by ViewVC 1.1.26