1 |
|
#include <unistd.h> |
2 |
|
#include <sys/types.h> |
3 |
|
#include <time.h> |
4 |
#include "rdesktop.h" |
#include "rdesktop.h" |
5 |
|
|
6 |
#define IRP_MJ_CREATE 0x00 |
#define IRP_MJ_CREATE 0x00 |
22 |
#define IRP_MN_QUERY_DIRECTORY 0x01 |
#define IRP_MN_QUERY_DIRECTORY 0x01 |
23 |
#define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02 |
#define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02 |
24 |
|
|
|
#define MAX_ASYNC_IO_REQUESTS 10 |
|
|
|
|
25 |
extern char hostname[16]; |
extern char hostname[16]; |
26 |
extern DEVICE_FNS serial_fns; |
extern DEVICE_FNS serial_fns; |
27 |
extern DEVICE_FNS printer_fns; |
extern DEVICE_FNS printer_fns; |
28 |
extern DEVICE_FNS parallel_fns; |
extern DEVICE_FNS parallel_fns; |
29 |
extern DEVICE_FNS disk_fns; |
extern DEVICE_FNS disk_fns; |
30 |
|
|
|
|
|
31 |
static VCHANNEL *rdpdr_channel; |
static VCHANNEL *rdpdr_channel; |
32 |
|
|
33 |
/* 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 */ |
38 |
RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; |
RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; |
39 |
|
|
40 |
/* 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 */ |
41 |
|
/* using a linked list ensures that they are processed in the right order, */ |
42 |
|
/* if multiple ios are being done on the same fd */ |
43 |
struct async_iorequest |
struct async_iorequest |
44 |
{ |
{ |
45 |
uint32 fd, major, minor, offset, device, id, length; |
uint32 fd, major, minor, offset, device, id, length, partial_len; |
46 |
long timeout, /* Total timeout */ |
long timeout, /* Total timeout */ |
47 |
itv_timeout; /* Interval timeout (between serial characters) */ |
itv_timeout; /* Interval timeout (between serial characters) */ |
48 |
uint8 *buffer; |
uint8 *buffer; |
49 |
DEVICE_FNS *fns; |
DEVICE_FNS *fns; |
50 |
} g_iorequest[MAX_ASYNC_IO_REQUESTS]; |
|
51 |
|
struct async_iorequest *next; /* next element in list */ |
52 |
|
} g_iorequest; |
53 |
|
|
54 |
/* Return device_id for a given handle */ |
/* Return device_id for a given handle */ |
55 |
int |
int |
81 |
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, |
82 |
DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer) |
DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer) |
83 |
{ |
{ |
|
int i; |
|
84 |
struct async_iorequest *iorq; |
struct async_iorequest *iorq; |
85 |
|
|
86 |
for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) |
iorq = &g_iorequest; |
87 |
|
while (iorq->fd != 0) |
88 |
{ |
{ |
89 |
iorq = &g_iorequest[i]; |
// create new element if needed |
90 |
|
if (iorq->next == NULL) |
91 |
|
iorq->next = |
92 |
|
(struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
93 |
|
|
94 |
|
iorq = iorq->next; |
95 |
|
} |
96 |
|
|
97 |
if (iorq->fd == 0) |
/* first element is special since it doesn't get deleted */ |
98 |
|
/* don't want to get io out of order */ |
99 |
|
if (g_iorequest.fd == 0) |
100 |
|
{ |
101 |
|
iorq = &g_iorequest; |
102 |
|
/* look for first occurrence of fd */ |
103 |
|
while (iorq->next != NULL) |
104 |
{ |
{ |
105 |
iorq->device = device; |
if (iorq->fd == file) |
106 |
iorq->fd = file; |
break; |
107 |
iorq->id = id; |
iorq = iorq->next; |
|
iorq->major = major; |
|
|
iorq->length = length; |
|
|
iorq->fns = fns; |
|
|
iorq->timeout = total_timeout; |
|
|
iorq->itv_timeout = interval_timeout; |
|
|
iorq->buffer = buffer; |
|
|
return True; |
|
108 |
} |
} |
109 |
|
/* if same create new link at end of chain instead */ |
110 |
|
if (iorq->fd == file) |
111 |
|
{ |
112 |
|
while (iorq->next != NULL) |
113 |
|
iorq = iorq->next; |
114 |
|
iorq->next = |
115 |
|
(struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
116 |
|
iorq = iorq->next; |
117 |
|
} |
118 |
|
else |
119 |
|
iorq = &g_iorequest; /* didn't find fs use first entry */ |
120 |
} |
} |
|
error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n"); |
|
|
return False; |
|
|
} |
|
121 |
|
|
122 |
|
iorq->device = device; |
123 |
|
iorq->fd = file; |
124 |
|
iorq->id = id; |
125 |
|
iorq->major = major; |
126 |
|
iorq->length = length; |
127 |
|
iorq->partial_len = 0; |
128 |
|
iorq->fns = fns; |
129 |
|
iorq->timeout = total_timeout; |
130 |
|
iorq->itv_timeout = interval_timeout; |
131 |
|
iorq->buffer = buffer; |
132 |
|
return True; |
133 |
|
} |
134 |
|
|
135 |
void |
void |
136 |
rdpdr_send_connect(void) |
rdpdr_send_connect(void) |
214 |
out_uint32_le(s, i); /* RDP Device ID */ |
out_uint32_le(s, i); /* RDP Device ID */ |
215 |
out_uint8p(s, g_rdpdr_device[i].name, 8); |
out_uint8p(s, g_rdpdr_device[i].name, 8); |
216 |
|
|
217 |
if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER) |
switch (g_rdpdr_device[i].device_type) |
218 |
{ |
{ |
219 |
printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data; |
case DEVICE_TYPE_PRINTER: |
220 |
|
printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data; |
221 |
|
|
222 |
driverlen = 2 * strlen(printerinfo->driver) + 2; |
driverlen = 2 * strlen(printerinfo->driver) + 2; |
223 |
printerlen = 2 * strlen(printerinfo->printer) + 2; |
printerlen = 2 * strlen(printerinfo->printer) + 2; |
224 |
bloblen = printerinfo->bloblen; |
bloblen = printerinfo->bloblen; |
225 |
|
|
226 |
out_uint32_le(s, 24 + driverlen + printerlen + bloblen); /* length of extra info */ |
out_uint32_le(s, 24 + driverlen + printerlen + bloblen); /* length of extra info */ |
227 |
out_uint32_le(s, printerinfo->default_printer ? 2 : 0); |
out_uint32_le(s, printerinfo->default_printer ? 2 : 0); |
228 |
out_uint8s(s, 8); /* unknown */ |
out_uint8s(s, 8); /* unknown */ |
229 |
out_uint32_le(s, driverlen); |
out_uint32_le(s, driverlen); |
230 |
out_uint32_le(s, printerlen); |
out_uint32_le(s, printerlen); |
231 |
out_uint32_le(s, bloblen); |
out_uint32_le(s, bloblen); |
232 |
rdp_out_unistr(s, printerinfo->driver, driverlen - 2); |
rdp_out_unistr(s, printerinfo->driver, driverlen - 2); |
233 |
rdp_out_unistr(s, printerinfo->printer, printerlen - 2); |
rdp_out_unistr(s, printerinfo->printer, printerlen - 2); |
234 |
out_uint8a(s, printerinfo->blob, bloblen); |
out_uint8a(s, printerinfo->blob, bloblen); |
235 |
|
|
236 |
xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */ |
xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */ |
237 |
} |
break; |
238 |
else |
default: |
239 |
{ |
out_uint32(s, 0); |
|
out_uint32(s, 0); |
|
240 |
} |
} |
241 |
} |
} |
242 |
#if 0 |
#if 0 |
308 |
buffer = (uint8 *) xmalloc(1024); |
buffer = (uint8 *) xmalloc(1024); |
309 |
buffer[0] = 0; |
buffer[0] = 0; |
310 |
|
|
|
|
|
311 |
switch (g_rdpdr_device[device].device_type) |
switch (g_rdpdr_device[device].device_type) |
312 |
{ |
{ |
313 |
case DEVICE_TYPE_SERIAL: |
case DEVICE_TYPE_SERIAL: |
329 |
|
|
330 |
case DEVICE_TYPE_DISK: |
case DEVICE_TYPE_DISK: |
331 |
|
|
332 |
|
/*rw_blocking = False; */ |
333 |
fns = &disk_fns; |
fns = &disk_fns; |
334 |
break; |
break; |
335 |
|
|
417 |
|
|
418 |
status = STATUS_CANCELLED; |
status = STATUS_CANCELLED; |
419 |
break; |
break; |
|
|
|
420 |
case IRP_MJ_WRITE: |
case IRP_MJ_WRITE: |
421 |
|
|
422 |
buffer_len = 1; |
buffer_len = 1; |
686 |
{ |
{ |
687 |
if (g_num_devices > 0) |
if (g_num_devices > 0) |
688 |
{ |
{ |
689 |
rdpdr_channel = channel_register("rdpdr", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP, rdpdr_process); |
rdpdr_channel = |
690 |
|
channel_register("rdpdr", |
691 |
|
CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP, |
692 |
|
rdpdr_process); |
693 |
} |
} |
694 |
|
|
695 |
return (rdpdr_channel != NULL); |
return (rdpdr_channel != NULL); |
699 |
void |
void |
700 |
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) |
701 |
{ |
{ |
|
int i; |
|
702 |
long select_timeout = 0; // Timeout value to be used for select() (in millisecons). |
long select_timeout = 0; // Timeout value to be used for select() (in millisecons). |
703 |
struct async_iorequest *iorq; |
struct async_iorequest *iorq; |
704 |
|
|
705 |
for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) |
iorq = &g_iorequest; |
706 |
|
while (iorq != NULL) |
707 |
{ |
{ |
708 |
iorq = &g_iorequest[i]; |
if (iorq->fd != 0) |
|
|
|
|
if (iorq->fd != 0) // Found a pending io request |
|
709 |
{ |
{ |
710 |
switch (iorq->major) |
switch (iorq->major) |
711 |
{ |
{ |
728 |
break; |
break; |
729 |
|
|
730 |
case IRP_MJ_WRITE: |
case IRP_MJ_WRITE: |
|
|
|
731 |
FD_SET(iorq->fd, wfds); |
FD_SET(iorq->fd, wfds); |
732 |
break; |
break; |
733 |
|
|
734 |
} |
} |
735 |
*n = MAX(*n, iorq->fd); |
*n = MAX(*n, iorq->fd); |
736 |
} |
} |
737 |
|
|
738 |
|
iorq = iorq->next; |
739 |
} |
} |
740 |
} |
} |
741 |
|
|
742 |
|
|
743 |
/* 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 */ |
744 |
void |
void |
745 |
rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out) |
rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out) |
746 |
{ |
{ |
|
int i; |
|
747 |
NTSTATUS status; |
NTSTATUS status; |
748 |
uint32 result = 0, buffer_len = 0; |
uint32 result = 0; |
749 |
DEVICE_FNS *fns; |
DEVICE_FNS *fns; |
750 |
struct async_iorequest *iorq; |
struct async_iorequest *iorq; |
751 |
|
struct async_iorequest *prev; |
752 |
|
|
753 |
if (timed_out) |
if (timed_out) |
754 |
{ |
{ |
756 |
return; |
return; |
757 |
} |
} |
758 |
|
|
759 |
// Walk through array of pending io_rq's |
iorq = &g_iorequest; |
760 |
for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) |
prev = NULL; |
761 |
|
while (iorq != NULL) |
762 |
{ |
{ |
|
iorq = &g_iorequest[i]; |
|
763 |
|
|
764 |
if (iorq->fd != 0) |
if (iorq->fd != 0) |
765 |
{ |
{ |
766 |
switch (iorq->major) |
switch (iorq->major) |
767 |
{ |
{ |
|
|
|
768 |
case IRP_MJ_READ: |
case IRP_MJ_READ: |
|
|
|
769 |
if (FD_ISSET(iorq->fd, rfds)) |
if (FD_ISSET(iorq->fd, rfds)) |
770 |
{ |
{ |
771 |
// Read, and send data. |
/* Read the data */ |
772 |
fns = iorq->fns; |
fns = iorq->fns; |
773 |
status = fns->read(iorq->fd, iorq->buffer, |
status = fns->read(iorq->fd, |
774 |
iorq->length, 0, &result); |
iorq->buffer + iorq->partial_len, |
775 |
buffer_len = result; |
iorq->length - iorq->partial_len, |
776 |
|
0, &result); |
777 |
rdpdr_send_completion(iorq->device, iorq->id, |
iorq->partial_len += result; |
|
status, result, iorq->buffer, |
|
|
buffer_len); |
|
778 |
#if WITH_DEBUG_RDP5 |
#if WITH_DEBUG_RDP5 |
779 |
DEBUG(("RDPDR: %d bytes of data read\n", result)); |
DEBUG(("RDPDR: %d bytes of data read\n", result)); |
780 |
#endif |
#endif |
781 |
xfree(iorq->buffer); |
/* only delete link if all data has been transfered */ |
782 |
iorq->fd = 0; |
if (iorq->partial_len == iorq->length) |
783 |
|
{ |
784 |
|
/* send the data */ |
785 |
|
status = STATUS_SUCCESS; |
786 |
|
rdpdr_send_completion(iorq->device, |
787 |
|
iorq->id, status, |
788 |
|
iorq->length, |
789 |
|
iorq->buffer, result); |
790 |
|
|
791 |
|
xfree(iorq->buffer); |
792 |
|
iorq->fd = 0; |
793 |
|
if (prev != NULL) |
794 |
|
{ |
795 |
|
prev->next = iorq->next; |
796 |
|
xfree(iorq); |
797 |
|
} |
798 |
|
} |
799 |
} |
} |
800 |
break; |
break; |
|
|
|
801 |
case IRP_MJ_WRITE: |
case IRP_MJ_WRITE: |
|
|
|
802 |
if (FD_ISSET(iorq->fd, wfds)) |
if (FD_ISSET(iorq->fd, wfds)) |
803 |
{ |
{ |
804 |
// Write data and send completion. |
/* Write data. */ |
805 |
fns = iorq->fns; |
fns = iorq->fns; |
806 |
status = fns->write(iorq->fd, iorq->buffer, |
status = fns->write(iorq->fd, |
807 |
iorq->length, 0, &result); |
iorq->buffer + |
808 |
rdpdr_send_completion(iorq->device, iorq->id, |
iorq->partial_len, |
809 |
status, result, "", 1); |
iorq->length - |
810 |
|
iorq->partial_len, 0, &result); |
811 |
xfree(iorq->buffer); |
iorq->partial_len += result; |
812 |
iorq->fd = 0; |
#if WITH_DEBUG_RDP5 |
813 |
|
DEBUG(("RDPDR: %d bytes of data written\n", |
814 |
|
result)); |
815 |
|
#endif |
816 |
|
/* only delete link if all data has been transfered */ |
817 |
|
if (iorq->partial_len == iorq->length) |
818 |
|
{ |
819 |
|
/* send a status success */ |
820 |
|
status = STATUS_SUCCESS; |
821 |
|
rdpdr_send_completion(iorq->device, |
822 |
|
iorq->id, status, |
823 |
|
iorq->length, "", 1); |
824 |
|
|
825 |
|
xfree(iorq->buffer); |
826 |
|
iorq->fd = 0; |
827 |
|
if (prev != NULL) |
828 |
|
{ |
829 |
|
prev->next = iorq->next; |
830 |
|
xfree(iorq); |
831 |
|
} |
832 |
|
} |
833 |
} |
} |
834 |
break; |
break; |
835 |
} |
} |
836 |
|
|
837 |
} |
} |
838 |
|
prev = iorq; |
839 |
|
iorq = iorq->next; |
840 |
} |
} |
841 |
|
|
842 |
} |
} |
843 |
|
|
844 |
/* Abort a pending io request for a given handle and major */ |
/* Abort a pending io request for a given handle and major */ |
846 |
rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status) |
rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status) |
847 |
{ |
{ |
848 |
uint32 result; |
uint32 result; |
|
int i; |
|
849 |
struct async_iorequest *iorq; |
struct async_iorequest *iorq; |
850 |
|
struct async_iorequest *prev; |
851 |
|
|
852 |
for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) |
iorq = &g_iorequest; |
853 |
|
prev = NULL; |
854 |
|
while (iorq != NULL) |
855 |
{ |
{ |
|
iorq = &g_iorequest[i]; |
|
|
|
|
856 |
// 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. |
857 |
// Abort read should not abort a write io request. |
// Abort read should not abort a write io request. |
858 |
if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) |
if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) |
861 |
rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1); |
rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1); |
862 |
xfree(iorq->buffer); |
xfree(iorq->buffer); |
863 |
iorq->fd = 0; |
iorq->fd = 0; |
864 |
|
if (prev != NULL) |
865 |
|
{ |
866 |
|
prev->next = iorq->next; |
867 |
|
xfree(iorq); |
868 |
|
} |
869 |
return True; |
return True; |
870 |
} |
} |
871 |
|
|
872 |
|
prev = iorq; |
873 |
|
iorq = iorq->next; |
874 |
} |
} |
875 |
|
|
876 |
return False; |
return False; |
877 |
} |
} |