1 |
#include <unistd.h> |
#include <unistd.h> |
2 |
#include <sys/types.h> |
#include <sys/types.h> |
3 |
|
#include <sys/time.h> |
4 |
|
#include <dirent.h> /* opendir, closedir, readdir */ |
5 |
#include <time.h> |
#include <time.h> |
6 |
#include "rdesktop.h" |
#include "rdesktop.h" |
7 |
|
|
29 |
extern DEVICE_FNS printer_fns; |
extern DEVICE_FNS printer_fns; |
30 |
extern DEVICE_FNS parallel_fns; |
extern DEVICE_FNS parallel_fns; |
31 |
extern DEVICE_FNS disk_fns; |
extern DEVICE_FNS disk_fns; |
32 |
|
extern FILEINFO g_fileinfo[]; |
33 |
|
|
34 |
static VCHANNEL *rdpdr_channel; |
static VCHANNEL *rdpdr_channel; |
35 |
|
|
39 |
|
|
40 |
/* Table with information about rdpdr devices */ |
/* Table with information about rdpdr devices */ |
41 |
RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; |
RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; |
42 |
|
char * g_rdpdr_clientname = NULL; |
43 |
|
|
44 |
/* 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 */ |
45 |
/* 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, */ |
82 |
} |
} |
83 |
} |
} |
84 |
|
|
85 |
|
BOOL |
86 |
|
rdpdr_handle_ok(int device, int handle) |
87 |
|
{ |
88 |
|
switch (g_rdpdr_device[device].device_type) |
89 |
|
{ |
90 |
|
case DEVICE_TYPE_PARALLEL: |
91 |
|
case DEVICE_TYPE_SERIAL: |
92 |
|
case DEVICE_TYPE_PRINTER: |
93 |
|
case DEVICE_TYPE_SCARD: |
94 |
|
if (g_rdpdr_device[device].handle != handle) |
95 |
|
return False; |
96 |
|
break; |
97 |
|
case DEVICE_TYPE_DISK: |
98 |
|
if (g_fileinfo[handle].device_id != device) |
99 |
|
return False; |
100 |
|
break; |
101 |
|
} |
102 |
|
return True; |
103 |
|
} |
104 |
|
|
105 |
/* 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 */ |
106 |
BOOL |
BOOL |
107 |
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, |
108 |
DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer) |
DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer, |
109 |
|
uint32 offset) |
110 |
{ |
{ |
111 |
struct async_iorequest *iorq; |
struct async_iorequest *iorq; |
112 |
|
|
113 |
if (g_iorequest == NULL) |
if (g_iorequest == NULL) |
114 |
{ |
{ |
115 |
g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
116 |
|
if (!g_iorequest) |
117 |
|
return False; |
118 |
g_iorequest->fd = 0; |
g_iorequest->fd = 0; |
119 |
g_iorequest->next = NULL; |
g_iorequest->next = NULL; |
120 |
} |
} |
128 |
{ |
{ |
129 |
iorq->next = |
iorq->next = |
130 |
(struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
(struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
131 |
|
if (!iorq->next) |
132 |
|
return False; |
133 |
iorq->next->fd = 0; |
iorq->next->fd = 0; |
134 |
iorq->next->next = NULL; |
iorq->next->next = NULL; |
135 |
} |
} |
145 |
iorq->timeout = total_timeout; |
iorq->timeout = total_timeout; |
146 |
iorq->itv_timeout = interval_timeout; |
iorq->itv_timeout = interval_timeout; |
147 |
iorq->buffer = buffer; |
iorq->buffer = buffer; |
148 |
|
iorq->offset = offset; |
149 |
return True; |
return True; |
150 |
} |
} |
151 |
|
|
169 |
rdpdr_send_name(void) |
rdpdr_send_name(void) |
170 |
{ |
{ |
171 |
uint8 magic[4] = "rDNC"; |
uint8 magic[4] = "rDNC"; |
172 |
uint32 hostlen = (strlen(hostname) + 1) * 2; |
if (NULL == g_rdpdr_clientname) { |
173 |
|
g_rdpdr_clientname = hostname; |
174 |
|
} |
175 |
|
uint32 hostlen = (strlen(g_rdpdr_clientname) + 1) * 2; |
176 |
STREAM s; |
STREAM s; |
177 |
|
|
178 |
s = channel_init(rdpdr_channel, 16 + hostlen); |
s = channel_init(rdpdr_channel, 16 + hostlen); |
181 |
out_uint16_le(s, 0x72); |
out_uint16_le(s, 0x72); |
182 |
out_uint32(s, 0); |
out_uint32(s, 0); |
183 |
out_uint32_le(s, hostlen); |
out_uint32_le(s, hostlen); |
184 |
rdp_out_unistr(s, hostname, hostlen - 2); |
rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2); |
185 |
s_mark_end(s); |
s_mark_end(s); |
186 |
channel_send(s, rdpdr_channel); |
channel_send(s, rdpdr_channel); |
187 |
} |
} |
417 |
#if WITH_DEBUG_RDP5 |
#if WITH_DEBUG_RDP5 |
418 |
DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset)); |
DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset)); |
419 |
#endif |
#endif |
420 |
|
if (!rdpdr_handle_ok(device, file)) |
421 |
|
{ |
422 |
|
status = STATUS_INVALID_HANDLE; |
423 |
|
break; |
424 |
|
} |
425 |
|
|
426 |
if (rw_blocking) // Complete read immediately |
if (rw_blocking) // Complete read immediately |
427 |
{ |
{ |
428 |
buffer = (uint8 *) xrealloc((void *) buffer, length); |
buffer = (uint8 *) xrealloc((void *) buffer, length); |
429 |
|
if (!buffer) |
430 |
|
{ |
431 |
|
status = STATUS_CANCELLED; |
432 |
|
break; |
433 |
|
} |
434 |
status = fns->read(file, buffer, length, offset, &result); |
status = fns->read(file, buffer, length, offset, &result); |
435 |
buffer_len = result; |
buffer_len = result; |
436 |
break; |
break; |
438 |
|
|
439 |
// Add request to table |
// Add request to table |
440 |
pst_buf = (uint8 *) xmalloc(length); |
pst_buf = (uint8 *) xmalloc(length); |
441 |
|
if (!pst_buf) |
442 |
|
{ |
443 |
|
status = STATUS_CANCELLED; |
444 |
|
break; |
445 |
|
} |
446 |
serial_get_timeout(file, length, &total_timeout, &interval_timeout); |
serial_get_timeout(file, length, &total_timeout, &interval_timeout); |
447 |
if (add_async_iorequest |
if (add_async_iorequest |
448 |
(device, file, id, major, length, fns, total_timeout, interval_timeout, |
(device, file, id, major, length, fns, total_timeout, interval_timeout, |
449 |
pst_buf)) |
pst_buf, offset)) |
450 |
{ |
{ |
451 |
status = STATUS_PENDING; |
status = STATUS_PENDING; |
452 |
break; |
break; |
470 |
#if WITH_DEBUG_RDP5 |
#if WITH_DEBUG_RDP5 |
471 |
DEBUG(("RDPDR IRP Write (length: %d)\n", result)); |
DEBUG(("RDPDR IRP Write (length: %d)\n", result)); |
472 |
#endif |
#endif |
473 |
|
if (!rdpdr_handle_ok(device, file)) |
474 |
|
{ |
475 |
|
status = STATUS_INVALID_HANDLE; |
476 |
|
break; |
477 |
|
} |
478 |
|
|
479 |
if (rw_blocking) // Complete immediately |
if (rw_blocking) // Complete immediately |
480 |
{ |
{ |
481 |
status = fns->write(file, s->p, length, offset, &result); |
status = fns->write(file, s->p, length, offset, &result); |
484 |
|
|
485 |
// Add to table |
// Add to table |
486 |
pst_buf = (uint8 *) xmalloc(length); |
pst_buf = (uint8 *) xmalloc(length); |
487 |
|
if (!pst_buf) |
488 |
|
{ |
489 |
|
status = STATUS_CANCELLED; |
490 |
|
break; |
491 |
|
} |
492 |
|
|
493 |
in_uint8a(s, pst_buf, length); |
in_uint8a(s, pst_buf, length); |
494 |
|
|
495 |
if (add_async_iorequest |
if (add_async_iorequest |
496 |
(device, file, id, major, length, fns, 0, 0, pst_buf)) |
(device, file, id, major, length, fns, 0, 0, pst_buf, offset)) |
497 |
{ |
{ |
498 |
status = STATUS_PENDING; |
status = STATUS_PENDING; |
499 |
break; |
break; |
613 |
in_uint8s(s, 0x14); |
in_uint8s(s, 0x14); |
614 |
|
|
615 |
buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14); |
buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14); |
616 |
|
if (!buffer) |
617 |
|
{ |
618 |
|
status = STATUS_CANCELLED; |
619 |
|
break; |
620 |
|
} |
621 |
|
|
622 |
out.data = out.p = buffer; |
out.data = out.p = buffer; |
623 |
out.size = sizeof(buffer); |
out.size = sizeof(buffer); |
624 |
status = fns->device_control(file, request, s, &out); |
status = fns->device_control(file, request, s, &out); |
634 |
{ |
{ |
635 |
rdpdr_send_completion(device, id, status, result, buffer, buffer_len); |
rdpdr_send_completion(device, id, status, result, buffer, buffer_len); |
636 |
} |
} |
637 |
xfree(buffer); |
if (buffer) |
638 |
|
xfree(buffer); |
639 |
|
buffer = NULL; |
640 |
} |
} |
641 |
|
|
642 |
void |
void |
755 |
void |
void |
756 |
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) |
757 |
{ |
{ |
758 |
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). |
759 |
struct async_iorequest *iorq; |
struct async_iorequest *iorq; |
760 |
|
|
761 |
iorq = g_iorequest; |
iorq = g_iorequest; |
795 |
} |
} |
796 |
} |
} |
797 |
|
|
798 |
|
struct async_iorequest * |
799 |
|
rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq) |
800 |
|
{ |
801 |
|
if (!iorq) |
802 |
|
return NULL; |
803 |
|
|
804 |
|
if (iorq->buffer) |
805 |
|
xfree(iorq->buffer); |
806 |
|
if (prev) |
807 |
|
{ |
808 |
|
prev->next = iorq->next; |
809 |
|
xfree(iorq); |
810 |
|
iorq = prev->next; |
811 |
|
} |
812 |
|
else |
813 |
|
{ |
814 |
|
// Even if NULL |
815 |
|
g_iorequest = iorq->next; |
816 |
|
xfree(iorq); |
817 |
|
iorq = NULL; |
818 |
|
} |
819 |
|
return iorq; |
820 |
|
} |
821 |
|
|
822 |
/* 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 */ |
823 |
void |
void |
857 |
/* never read larger chunks than 8k - chances are that it will block */ |
/* never read larger chunks than 8k - chances are that it will block */ |
858 |
status = fns->read(iorq->fd, |
status = fns->read(iorq->fd, |
859 |
iorq->buffer + iorq->partial_len, |
iorq->buffer + iorq->partial_len, |
860 |
req_size, 0, &result); |
req_size, iorq->offset, &result); |
|
iorq->partial_len += result; |
|
861 |
|
|
862 |
|
if (result > 0) |
863 |
|
{ |
864 |
|
iorq->partial_len += result; |
865 |
|
iorq->offset += result; |
866 |
|
} |
867 |
#if WITH_DEBUG_RDP5 |
#if WITH_DEBUG_RDP5 |
868 |
DEBUG(("RDPDR: %d bytes of data read\n", result)); |
DEBUG(("RDPDR: %d bytes of data read\n", result)); |
869 |
#endif |
#endif |
875 |
#if WITH_DEBUG_RDP5 |
#if WITH_DEBUG_RDP5 |
876 |
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)); |
877 |
#endif |
#endif |
|
/* send the data */ |
|
|
status = STATUS_SUCCESS; |
|
878 |
rdpdr_send_completion(iorq->device, |
rdpdr_send_completion(iorq->device, |
879 |
iorq->id, status, |
iorq->id, status, |
880 |
iorq->partial_len, |
iorq->partial_len, |
881 |
iorq->buffer, |
iorq->buffer, |
882 |
iorq->partial_len); |
iorq->partial_len); |
883 |
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); |
|
|
} |
|
884 |
} |
} |
885 |
} |
} |
886 |
break; |
break; |
898 |
/* never write larger chunks than 8k - chances are that it will block */ |
/* never write larger chunks than 8k - chances are that it will block */ |
899 |
status = fns->write(iorq->fd, |
status = fns->write(iorq->fd, |
900 |
iorq->buffer + |
iorq->buffer + |
901 |
iorq->partial_len, req_size, 0, |
iorq->partial_len, req_size, |
902 |
&result); |
iorq->offset, &result); |
903 |
iorq->partial_len += result; |
|
904 |
|
if (result > 0) |
905 |
|
{ |
906 |
|
iorq->partial_len += result; |
907 |
|
iorq->offset += result; |
908 |
|
} |
909 |
|
|
910 |
#if WITH_DEBUG_RDP5 |
#if WITH_DEBUG_RDP5 |
911 |
DEBUG(("RDPDR: %d bytes of data written\n", |
DEBUG(("RDPDR: %d bytes of data written\n", |
912 |
result)); |
result)); |
919 |
#if WITH_DEBUG_RDP5 |
#if WITH_DEBUG_RDP5 |
920 |
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)); |
921 |
#endif |
#endif |
|
/* send a status success */ |
|
|
status = STATUS_SUCCESS; |
|
922 |
rdpdr_send_completion(iorq->device, |
rdpdr_send_completion(iorq->device, |
923 |
iorq->id, status, |
iorq->id, status, |
924 |
iorq->partial_len, (uint8*)"", |
iorq->partial_len, |
925 |
1); |
(uint8 *) "", 1); |
926 |
|
|
927 |
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); |
|
|
} |
|
928 |
} |
} |
929 |
} |
} |
930 |
break; |
break; |
932 |
|
|
933 |
} |
} |
934 |
prev = iorq; |
prev = iorq; |
935 |
iorq = iorq->next; |
if (iorq) |
936 |
|
iorq = iorq->next; |
937 |
} |
} |
938 |
|
|
939 |
} |
} |
955 |
if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) |
if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) |
956 |
{ |
{ |
957 |
result = 0; |
result = 0; |
958 |
rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8*)"", 1); |
rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "", |
959 |
xfree(iorq->buffer); |
1); |
960 |
iorq->fd = 0; |
|
961 |
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); |
|
|
} |
|
962 |
return True; |
return True; |
963 |
} |
} |
964 |
|
|