/[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 710 by jsorg71, Tue Jun 15 22:17:08 2004 UTC revision 1328 by stargo, Fri Nov 3 23:51:35 2006 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Copyright (C) Matthew Chapman 1999-2004     Copyright (C) Matthew Chapman 1999-2005
4    
5     This program is free software; you can redistribute it and/or modify     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     it under the terms of the GNU General Public License as published by
# Line 60  extern DEVICE_FNS serial_fns; Line 60  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[];  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 */
# Line 90  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 154  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 =
# Line 227  announcedata_size() Line 231  announcedata_size()
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 266  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 306  rdpdr_send_available(void) Line 312  rdpdr_send_available(void)
312          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
313  }  }
314    
315  static void  void
316  rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,  rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,
317                        uint32 length)                        uint32 length)
318  {  {
319          uint8 magic[4] = "rDCI";          uint8 magic[4] = "rDCI";
320          STREAM s;          STREAM s;
321    
322    #ifdef WITH_SCARD
323            scard_lock(SCARD_LOCK_RDPDR);
324    #endif
325          s = channel_init(rdpdr_channel, 20 + length);          s = channel_init(rdpdr_channel, 20 + length);
326          out_uint8a(s, magic, 4);          out_uint8a(s, magic, 4);
327          out_uint32_le(s, device);          out_uint32_le(s, device);
# Line 321  rdpdr_send_completion(uint32 device, uin Line 330  rdpdr_send_completion(uint32 device, uin
330          out_uint32_le(s, result);          out_uint32_le(s, result);
331          out_uint8p(s, buffer, length);          out_uint8p(s, buffer, length);
332          s_mark_end(s);          s_mark_end(s);
333          /* JIF          /* JIF */
334             hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */  #ifdef WITH_DEBUG_RDP5
335            printf("--> rdpdr_send_completion\n");
336            /* hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
337    #endif
338          channel_send(s, rdpdr_channel);          channel_send(s, rdpdr_channel);
339    #ifdef WITH_SCARD
340            scard_unlock(SCARD_LOCK_RDPDR);
341    #endif
342  }  }
343    
344  static void  static void
# Line 346  rdpdr_process_irp(STREAM s) Line 361  rdpdr_process_irp(STREAM s)
361                  error_mode,                  error_mode,
362                  share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;                  share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
363    
364          char filename[256];          char filename[PATH_MAX];
365          uint8 *buffer, *pst_buf;          uint8 *buffer, *pst_buf;
366          struct stream out;          struct stream out;
367          DEVICE_FNS *fns;          DEVICE_FNS *fns;
# Line 389  rdpdr_process_irp(STREAM s) Line 404  rdpdr_process_irp(STREAM s)
404                          break;                          break;
405    
406                  case DEVICE_TYPE_SCARD:                  case DEVICE_TYPE_SCARD:
407    #ifdef WITH_SCARD
408                            fns = &scard_fns;
409                            rw_blocking = False;
410                            break;
411    #endif
412                  default:                  default:
413    
414                          error("IRP for bad device %ld\n", device);                          error("IRP for bad device %ld\n", device);
# Line 400  rdpdr_process_irp(STREAM s) Line 420  rdpdr_process_irp(STREAM s)
420                  case IRP_MJ_CREATE:                  case IRP_MJ_CREATE:
421    
422                          in_uint32_be(s, desired_access);                          in_uint32_be(s, desired_access);
423                          in_uint8s(s, 0x08);     // unknown                          in_uint8s(s, 0x08);     /* unknown */
424                          in_uint32_le(s, error_mode);                          in_uint32_le(s, error_mode);
425                          in_uint32_le(s, share_mode);                          in_uint32_le(s, share_mode);
426                          in_uint32_le(s, disposition);                          in_uint32_le(s, disposition);
# Line 457  rdpdr_process_irp(STREAM s) Line 477  rdpdr_process_irp(STREAM s)
477                                  break;                                  break;
478                          }                          }
479    
480                          if (rw_blocking)        // Complete read immediately                          if (rw_blocking)        /* Complete read immediately */
481                          {                          {
482                                  buffer = (uint8 *) xrealloc((void *) buffer, length);                                  buffer = (uint8 *) xrealloc((void *) buffer, length);
483                                  if (!buffer)                                  if (!buffer)
# Line 470  rdpdr_process_irp(STREAM s) Line 490  rdpdr_process_irp(STREAM s)
490                                  break;                                  break;
491                          }                          }
492    
493                          // Add request to table                          /* Add request to table */
494                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
495                          if (!pst_buf)                          if (!pst_buf)
496                          {                          {
# Line 510  rdpdr_process_irp(STREAM s) Line 530  rdpdr_process_irp(STREAM s)
530                                  break;                                  break;
531                          }                          }
532    
533                          if (rw_blocking)        // Complete immediately                          if (rw_blocking)        /* Complete immediately */
534                          {                          {
535                                  status = fns->write(file, s->p, length, offset, &result);                                  status = fns->write(file, s->p, length, offset, &result);
536                                  break;                                  break;
537                          }                          }
538    
539                          // Add to table                          /* Add to table */
540                          pst_buf = (uint8 *) xmalloc(length);                          pst_buf = (uint8 *) xmalloc(length);
541                          if (!pst_buf)                          if (!pst_buf)
542                          {                          {
# Line 621  rdpdr_process_irp(STREAM s) Line 641  rdpdr_process_irp(STREAM s)
641                                  case IRP_MN_NOTIFY_CHANGE_DIRECTORY:                                  case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
642    
643                                          /* JIF                                          /* JIF
644                                             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);  */
645                                          status = STATUS_PENDING;        // Don't send completion packet  
646                                            in_uint32_le(s, info_level);    /* notify mask */
647    
648                                            g_notify_stamp = True;
649    
650                                            status = disk_create_notify(file, info_level);
651                                            result = 0;
652    
653                                            if (status == STATUS_PENDING)
654                                                    add_async_iorequest(device, file, id, major, length,
655                                                                        fns, 0, 0, NULL, 0);
656                                          break;                                          break;
657    
658                                  default:                                  default:
659    
660                                          status = STATUS_INVALID_PARAMETER;                                          status = STATUS_INVALID_PARAMETER;
661                                          /* JIF                                          /* JIF */
662                                             unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */                                          unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
663                          }                          }
664                          break;                          break;
665    
# Line 655  rdpdr_process_irp(STREAM s) Line 685  rdpdr_process_irp(STREAM s)
685    
686                          out.data = out.p = buffer;                          out.data = out.p = buffer;
687                          out.size = sizeof(buffer);                          out.size = sizeof(buffer);
688    
689                            DEBUG_SCARD(("[SMART-CARD TRACE]\n"));
690                            DEBUG_SCARD(("device 0x%.8x\n", device));
691                            DEBUG_SCARD(("file   0x%.8x\n", file));
692                            DEBUG_SCARD(("id     0x%.8x\n", id));
693    #ifdef WITH_SCARD
694                            scardSetInfo(device, id, bytes_out + 0x14);
695    #endif
696                          status = fns->device_control(file, request, s, &out);                          status = fns->device_control(file, request, s, &out);
697                          result = buffer_len = out.p - out.data;                          result = buffer_len = out.p - out.data;
698    
699                            DEBUG_SCARD(("[SMART-CARD TRACE] OUT 0x%.8x\n", status));
700    
701                            /* Serial SERIAL_WAIT_ON_MASK */
702                            if (status == STATUS_PENDING)
703                            {
704                                    if (add_async_iorequest
705                                        (device, file, id, major, length, fns, 0, 0, NULL, 0))
706                                    {
707                                            status = STATUS_PENDING;
708                                            break;
709                                    }
710                            }
711    #ifdef WITH_SCARD
712                            else if (status == (STATUS_PENDING | 0xC0000000))
713                                    status = STATUS_PENDING;
714    #endif
715                          break;                          break;
716    
717    
# Line 808  rdpdr_init() Line 863  rdpdr_init()
863  void  void
864  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)
865  {  {
866          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). */
867          struct async_iorequest *iorq;          struct async_iorequest *iorq;
868          char c;          char c;
869    
# Line 825  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 880  rdpdr_add_fds(int *n, fd_set * rfds, fd_
880                                             reconnecting. FIXME: Real                                             reconnecting. FIXME: Real
881                                             support for reconnects. */                                             support for reconnects. */
882    
                                         if ((read(iorq->fd, &c, 0) != 0) && (errno == EBADF))  
                                                 break;  
   
883                                          FD_SET(iorq->fd, rfds);                                          FD_SET(iorq->fd, rfds);
884                                          *n = MAX(*n, iorq->fd);                                          *n = MAX(*n, iorq->fd);
885    
886                                          // Check if io request timeout is smaller than current (but not 0).                                          /* Check if io request timeout is smaller than current (but not 0). */
887                                          if (iorq->timeout                                          if (iorq->timeout
888                                              && (select_timeout == 0                                              && (select_timeout == 0
889                                                  || iorq->timeout < select_timeout))                                                  || iorq->timeout < select_timeout))
890                                          {                                          {
891                                                  // Set new timeout                                                  /* Set new timeout */
892                                                  select_timeout = iorq->timeout;                                                  select_timeout = iorq->timeout;
893                                                  g_min_timeout_fd = iorq->fd;    /* Remember fd */                                                  g_min_timeout_fd = iorq->fd;    /* Remember fd */
894                                                  tv->tv_sec = select_timeout / 1000;                                                  tv->tv_sec = select_timeout / 1000;
895                                                  tv->tv_usec = (select_timeout % 1000) * 1000;                                                  tv->tv_usec = (select_timeout % 1000) * 1000;
896                                                  *timeout = True;                                                  *timeout = True;
897                                                    break;
898                                            }
899                                            if (iorq->itv_timeout && iorq->partial_len > 0
900                                                && (select_timeout == 0
901                                                    || iorq->itv_timeout < select_timeout))
902                                            {
903                                                    /* Set new timeout */
904                                                    select_timeout = iorq->itv_timeout;
905                                                    g_min_timeout_fd = iorq->fd;    /* Remember fd */
906                                                    tv->tv_sec = select_timeout / 1000;
907                                                    tv->tv_usec = (select_timeout % 1000) * 1000;
908                                                    *timeout = True;
909                                                    break;
910                                          }                                          }
   
911                                          break;                                          break;
912    
913                                  case IRP_MJ_WRITE:                                  case IRP_MJ_WRITE:
# Line 855  rdpdr_add_fds(int *n, fd_set * rfds, fd_ Line 919  rdpdr_add_fds(int *n, fd_set * rfds, fd_
919                                          *n = MAX(*n, iorq->fd);                                          *n = MAX(*n, iorq->fd);
920                                          break;                                          break;
921    
922                                    case IRP_MJ_DEVICE_CONTROL:
923                                            if (select_timeout > 5)
924                                                    select_timeout = 5;     /* serial event queue */
925                                            break;
926    
927                          }                          }
928    
929                  }                  }
# Line 879  rdpdr_remove_iorequest(struct async_iore Line 948  rdpdr_remove_iorequest(struct async_iore
948          }          }
949          else          else
950          {          {
951                  // Even if NULL                  /* Even if NULL */
952                  g_iorequest = iorq->next;                  g_iorequest = iorq->next;
953                  xfree(iorq);                  xfree(iorq);
954                  iorq = NULL;                  iorq = NULL;
# Line 888  rdpdr_remove_iorequest(struct async_iore Line 957  rdpdr_remove_iorequest(struct async_iore
957  }  }
958    
959  /* 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 */
960  void  static void
961  rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)  _rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
962  {  {
963          NTSTATUS status;          NTSTATUS status;
964          uint32 result = 0;          uint32 result = 0;
# Line 897  rdpdr_check_fds(fd_set * rfds, fd_set * Line 966  rdpdr_check_fds(fd_set * rfds, fd_set *
966          struct async_iorequest *iorq;          struct async_iorequest *iorq;
967          struct async_iorequest *prev;          struct async_iorequest *prev;
968          uint32 req_size = 0;          uint32 req_size = 0;
969            uint32 buffer_len;
970            struct stream out;
971            uint8 *buffer = NULL;
972    
973    
974          if (timed_out)          if (timed_out)
975          {          {
976                    /* check serial iv_timeout */
977    
978                    iorq = g_iorequest;
979                    prev = NULL;
980                    while (iorq != NULL)
981                    {
982                            if (iorq->fd == g_min_timeout_fd)
983                            {
984                                    if ((iorq->partial_len > 0) &&
985                                        (g_rdpdr_device[iorq->device].device_type ==
986                                         DEVICE_TYPE_SERIAL))
987                                    {
988    
989                                            /* iv_timeout between 2 chars, send partial_len */
990                                            /*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
991                                            rdpdr_send_completion(iorq->device,
992                                                                  iorq->id, STATUS_SUCCESS,
993                                                                  iorq->partial_len,
994                                                                  iorq->buffer, iorq->partial_len);
995                                            iorq = rdpdr_remove_iorequest(prev, iorq);
996                                            return;
997                                    }
998                                    else
999                                    {
1000                                            break;
1001                                    }
1002    
1003                            }
1004                            else
1005                            {
1006                                    break;
1007                            }
1008    
1009    
1010                            prev = iorq;
1011                            if (iorq)
1012                                    iorq = iorq->next;
1013    
1014                    }
1015    
1016                  rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);                  rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT);
1017                  return;                  return;
1018          }          }
# Line 927  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1040  rdpdr_check_fds(fd_set * rfds, fd_set *
1040                                                                     iorq->buffer + iorq->partial_len,                                                                     iorq->buffer + iorq->partial_len,
1041                                                                     req_size, iorq->offset, &result);                                                                     req_size, iorq->offset, &result);
1042    
1043                                                  if (result > 0)                                                  if ((long) result > 0)
1044                                                  {                                                  {
1045                                                          iorq->partial_len += result;                                                          iorq->partial_len += result;
1046                                                          iorq->offset += result;                                                          iorq->offset += result;
# Line 969  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1082  rdpdr_check_fds(fd_set * rfds, fd_set *
1082                                                                      iorq->partial_len, req_size,                                                                      iorq->partial_len, req_size,
1083                                                                      iorq->offset, &result);                                                                      iorq->offset, &result);
1084    
1085                                                  if (result > 0)                                                  if ((long) result > 0)
1086                                                  {                                                  {
1087                                                          iorq->partial_len += result;                                                          iorq->partial_len += result;
1088                                                          iorq->offset += result;                                                          iorq->offset += result;
# Line 996  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1109  rdpdr_check_fds(fd_set * rfds, fd_set *
1109                                                  }                                                  }
1110                                          }                                          }
1111                                          break;                                          break;
1112                                    case IRP_MJ_DEVICE_CONTROL:
1113                                            if (serial_get_event(iorq->fd, &result))
1114                                            {
1115                                                    buffer = (uint8 *) xrealloc((void *) buffer, 0x14);
1116                                                    out.data = out.p = buffer;
1117                                                    out.size = sizeof(buffer);
1118                                                    out_uint32_le(&out, result);
1119                                                    result = buffer_len = out.p - out.data;
1120                                                    status = STATUS_SUCCESS;
1121                                                    rdpdr_send_completion(iorq->device, iorq->id,
1122                                                                          status, result, buffer,
1123                                                                          buffer_len);
1124                                                    xfree(buffer);
1125                                                    iorq = rdpdr_remove_iorequest(prev, iorq);
1126                                            }
1127    
1128                                            break;
1129                          }                          }
1130    
1131                  }                  }
# Line 1004  rdpdr_check_fds(fd_set * rfds, fd_set * Line 1134  rdpdr_check_fds(fd_set * rfds, fd_set *
1134                          iorq = iorq->next;                          iorq = iorq->next;
1135          }          }
1136    
1137            /* Check notify */
1138            iorq = g_iorequest;
1139            prev = NULL;
1140            while (iorq != NULL)
1141            {
1142                    if (iorq->fd != 0)
1143                    {
1144                            switch (iorq->major)
1145                            {
1146    
1147                                    case IRP_MJ_DIRECTORY_CONTROL:
1148                                            if (g_rdpdr_device[iorq->device].device_type ==
1149                                                DEVICE_TYPE_DISK)
1150                                            {
1151    
1152                                                    if (g_notify_stamp)
1153                                                    {
1154                                                            g_notify_stamp = False;
1155                                                            status = disk_check_notify(iorq->fd);
1156                                                            if (status != STATUS_PENDING)
1157                                                            {
1158                                                                    rdpdr_send_completion(iorq->device,
1159                                                                                          iorq->id,
1160                                                                                          status, 0,
1161                                                                                          NULL, 0);
1162                                                                    iorq = rdpdr_remove_iorequest(prev,
1163                                                                                                  iorq);
1164                                                            }
1165                                                    }
1166                                            }
1167                                            break;
1168    
1169    
1170    
1171                            }
1172                    }
1173    
1174                    prev = iorq;
1175                    if (iorq)
1176                            iorq = iorq->next;
1177            }
1178    
1179  }  }
1180    
1181    void
1182    rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out)
1183    {
1184            fd_set dummy;
1185    
1186    
1187            FD_ZERO(&dummy);
1188    
1189    
1190            /* fist check event queue only,
1191               any serial wait event must be done before read block will be sent
1192             */
1193    
1194            _rdpdr_check_fds(&dummy, &dummy, False);
1195            _rdpdr_check_fds(rfds, wfds, timed_out);
1196    }
1197    
1198    
1199  /* Abort a pending io request for a given handle and major */  /* Abort a pending io request for a given handle and major */
1200  BOOL  BOOL
1201  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)  rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status)
# Line 1018  rdpdr_abort_io(uint32 fd, uint32 major, Line 1208  rdpdr_abort_io(uint32 fd, uint32 major,
1208          prev = NULL;          prev = NULL;
1209          while (iorq != NULL)          while (iorq != NULL)
1210          {          {
1211                  // 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.
1212                  // Abort read should not abort a write io request.                     Abort read should not abort a write io request. */
1213                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))                  if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1214                  {                  {
1215                          result = 0;                          result = 0;

Legend:
Removed from v.710  
changed lines
  Added in v.1328

  ViewVC Help
Powered by ViewVC 1.1.26