--- sourceforge.net/trunk/rdesktop/rdpdr.c 2004/01/29 12:27:21 590 +++ sourceforge.net/trunk/rdesktop/rdpdr.c 2004/04/15 20:12:42 651 @@ -1,14 +1,10 @@ #include #include +#include +#include /* opendir, closedir, readdir */ #include #include "rdesktop.h" -#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 - #define IRP_MJ_CREATE 0x00 #define IRP_MJ_CLOSE 0x02 #define IRP_MJ_READ 0x03 @@ -18,18 +14,17 @@ #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a #define IRP_MJ_DIRECTORY_CONTROL 0x0c #define IRP_MJ_DEVICE_CONTROL 0x0e +#define IRP_MJ_LOCK_CONTROL 0x11 #define IRP_MN_QUERY_DIRECTORY 0x01 #define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02 -//#define MAX_ASYNC_IO_REQUESTS 10 - extern char hostname[16]; extern DEVICE_FNS serial_fns; extern DEVICE_FNS printer_fns; extern DEVICE_FNS parallel_fns; extern DEVICE_FNS disk_fns; - +extern FILEINFO g_fileinfo[]; static VCHANNEL *rdpdr_channel; @@ -39,18 +34,23 @@ /* Table with information about rdpdr devices */ RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; +char *g_rdpdr_clientname = NULL; -#if 0 /* Used to store incoming io request, until they are ready to be completed */ +/* using a linked list ensures that they are processed in the right order, */ +/* if multiple ios are being done on the same fd */ struct async_iorequest { - uint32 fd, major, minor, offset, device, id, length; + uint32 fd, major, minor, offset, device, id, length, partial_len; long timeout, /* Total timeout */ itv_timeout; /* Interval timeout (between serial characters) */ uint8 *buffer; DEVICE_FNS *fns; -} g_iorequest[MAX_ASYNC_IO_REQUESTS]; -#endif + + struct async_iorequest *next; /* next element in list */ +}; + +struct async_iorequest *g_iorequest; /* Return device_id for a given handle */ int @@ -77,37 +77,72 @@ } } -#if 0 +BOOL +rdpdr_handle_ok(int device, int handle) +{ + switch (g_rdpdr_device[device].device_type) + { + case DEVICE_TYPE_PARALLEL: + case DEVICE_TYPE_SERIAL: + case DEVICE_TYPE_PRINTER: + case DEVICE_TYPE_SCARD: + if (g_rdpdr_device[device].handle != handle) + return False; + break; + case DEVICE_TYPE_DISK: + if (g_fileinfo[handle].device_id != device) + return False; + break; + } + return True; +} + /* Add a new io request to the table containing pending io requests so it won't block rdesktop */ BOOL add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length, - DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer) + DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer, + uint32 offset) { - int i; struct async_iorequest *iorq; - for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) + if (g_iorequest == NULL) { - iorq = &g_iorequest[i]; - - if (iorq->fd == 0) - { - iorq->device = device; - iorq->fd = file; - iorq->id = id; - iorq->major = major; - iorq->length = length; - iorq->fns = fns; - iorq->timeout = total_timeout; - iorq->itv_timeout = interval_timeout; - iorq->buffer = buffer; - return True; - } - } - error("IO request table full. Increase MAX_ASYNC_IO_REQUESTS in rdpdr.c!\n"); - return False; + g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); + if (!g_iorequest) + return False; + g_iorequest->fd = 0; + g_iorequest->next = NULL; + } + + iorq = g_iorequest; + + while (iorq->fd != 0) + { + // create new element if needed + if (iorq->next == NULL) + { + iorq->next = + (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); + if (!iorq->next) + return False; + iorq->next->fd = 0; + iorq->next->next = NULL; + } + iorq = iorq->next; + } + iorq->device = device; + iorq->fd = file; + iorq->id = id; + iorq->major = major; + iorq->length = length; + iorq->partial_len = 0; + iorq->fns = fns; + iorq->timeout = total_timeout; + iorq->itv_timeout = interval_timeout; + iorq->buffer = buffer; + iorq->offset = offset; + return True; } -#endif void rdpdr_send_connect(void) @@ -129,8 +164,14 @@ rdpdr_send_name(void) { uint8 magic[4] = "rDNC"; - uint32 hostlen = (strlen(hostname) + 1) * 2; STREAM s; + uint32 hostlen; + + if (NULL == g_rdpdr_clientname) + { + g_rdpdr_clientname = hostname; + } + hostlen = (strlen(g_rdpdr_clientname) + 1) * 2; s = channel_init(rdpdr_channel, 16 + hostlen); out_uint8a(s, magic, 4); @@ -138,7 +179,7 @@ out_uint16_le(s, 0x72); out_uint32(s, 0); out_uint32_le(s, hostlen); - rdp_out_unistr(s, hostname, hostlen - 2); + rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2); s_mark_end(s); channel_send(s, rdpdr_channel); } @@ -210,7 +251,8 @@ rdp_out_unistr(s, printerinfo->printer, printerlen - 2); out_uint8a(s, printerinfo->blob, bloblen); - xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */ + if (printerinfo->blob) + xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */ break; default: out_uint32(s, 0); @@ -290,15 +332,13 @@ case DEVICE_TYPE_SERIAL: fns = &serial_fns; - /* should be async when aio is finished */ - /*rw_blocking = False; */ + rw_blocking = False; break; case DEVICE_TYPE_PARALLEL: fns = ¶llel_fns; - /* should be async when aio is finished */ - /*rw_blocking = False;*/ + rw_blocking = False; break; case DEVICE_TYPE_PRINTER: @@ -309,6 +349,7 @@ case DEVICE_TYPE_DISK: fns = &disk_fns; + rw_blocking = False; break; case DEVICE_TYPE_SCARD: @@ -374,21 +415,36 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset)); #endif -// if (rw_blocking) // Complete read immediately -// { + if (!rdpdr_handle_ok(device, file)) + { + status = STATUS_INVALID_HANDLE; + break; + } + + if (rw_blocking) // Complete read immediately + { buffer = (uint8 *) xrealloc((void *) buffer, length); + if (!buffer) + { + status = STATUS_CANCELLED; + break; + } status = fns->read(file, buffer, length, offset, &result); buffer_len = result; break; -// } + } -#if 0 // Add request to table pst_buf = (uint8 *) xmalloc(length); + if (!pst_buf) + { + status = STATUS_CANCELLED; + break; + } serial_get_timeout(file, length, &total_timeout, &interval_timeout); if (add_async_iorequest (device, file, id, major, length, fns, total_timeout, interval_timeout, - pst_buf)) + pst_buf, offset)) { status = STATUS_PENDING; break; @@ -396,7 +452,6 @@ status = STATUS_CANCELLED; break; -#endif case IRP_MJ_WRITE: buffer_len = 1; @@ -413,18 +468,30 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR IRP Write (length: %d)\n", result)); #endif -// if (rw_blocking) // Complete immediately -// { + if (!rdpdr_handle_ok(device, file)) + { + status = STATUS_INVALID_HANDLE; + break; + } + + if (rw_blocking) // Complete immediately + { status = fns->write(file, s->p, length, offset, &result); break; -// } -#if 0 + } + // Add to table pst_buf = (uint8 *) xmalloc(length); + if (!pst_buf) + { + status = STATUS_CANCELLED; + break; + } + in_uint8a(s, pst_buf, length); if (add_async_iorequest - (device, file, id, major, length, fns, 0, 0, pst_buf)) + (device, file, id, major, length, fns, 0, 0, pst_buf, offset)) { status = STATUS_PENDING; break; @@ -432,7 +499,6 @@ status = STATUS_CANCELLED; break; -#endif case IRP_MJ_QUERY_INFORMATION: @@ -545,6 +611,12 @@ in_uint8s(s, 0x14); buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14); + if (!buffer) + { + status = STATUS_CANCELLED; + break; + } + out.data = out.p = buffer; out.size = sizeof(buffer); status = fns->device_control(file, request, s, &out); @@ -560,8 +632,9 @@ { rdpdr_send_completion(device, id, status, result, buffer, buffer_len); } - xfree(buffer); - + if (buffer) + xfree(buffer); + buffer = NULL; } void @@ -676,20 +749,17 @@ return (rdpdr_channel != NULL); } -#if 0 /* Add file descriptors of pending io request to select() */ void rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout) { - int i; - 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). struct async_iorequest *iorq; - for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) + iorq = g_iorequest; + while (iorq != NULL) { - iorq = &g_iorequest[i]; - - if (iorq->fd != 0) // Found a pending io request + if (iorq->fd != 0) { switch (iorq->major) { @@ -712,26 +782,51 @@ break; case IRP_MJ_WRITE: - FD_SET(iorq->fd, wfds); break; } *n = MAX(*n, iorq->fd); } + + iorq = iorq->next; } } +struct async_iorequest * +rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq) +{ + if (!iorq) + return NULL; + + if (iorq->buffer) + xfree(iorq->buffer); + if (prev) + { + prev->next = iorq->next; + xfree(iorq); + iorq = prev->next; + } + else + { + // Even if NULL + g_iorequest = iorq->next; + xfree(iorq); + iorq = NULL; + } + return iorq; +} /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */ void rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out) { - int i; NTSTATUS status; - uint32 result = 0, buffer_len = 0; + uint32 result = 0; DEVICE_FNS *fns; struct async_iorequest *iorq; + struct async_iorequest *prev; + uint32 req_size = 0; if (timed_out) { @@ -739,55 +834,106 @@ return; } - // Walk through array of pending io_rq's - for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) + iorq = g_iorequest; + prev = NULL; + while (iorq != NULL) { - iorq = &g_iorequest[i]; - if (iorq->fd != 0) { switch (iorq->major) { - case IRP_MJ_READ: - if (FD_ISSET(iorq->fd, rfds)) { - // Read, and send data. + /* Read the data */ fns = iorq->fns; - status = fns->read(iorq->fd, iorq->buffer, - iorq->length, 0, &result); - buffer_len = result; - - rdpdr_send_completion(iorq->device, iorq->id, - status, result, iorq->buffer, - buffer_len); + + req_size = + (iorq->length - iorq->partial_len) > + 8192 ? 8192 : (iorq->length - + iorq->partial_len); + /* never read larger chunks than 8k - chances are that it will block */ + status = fns->read(iorq->fd, + iorq->buffer + iorq->partial_len, + req_size, iorq->offset, &result); + + if (result > 0) + { + iorq->partial_len += result; + iorq->offset += result; + } #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: %d bytes of data read\n", result)); #endif - xfree(iorq->buffer); - iorq->fd = 0; + /* only delete link if all data has been transfered */ + /* or if result was 0 and status success - EOF */ + if ((iorq->partial_len == iorq->length) || + (result == 0)) + { +#if WITH_DEBUG_RDP5 + DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length)); +#endif + rdpdr_send_completion(iorq->device, + iorq->id, status, + iorq->partial_len, + iorq->buffer, + iorq->partial_len); + iorq = rdpdr_remove_iorequest(prev, iorq); + } } break; - case IRP_MJ_WRITE: - if (FD_ISSET(iorq->fd, wfds)) { - // Write data and send completion. + /* Write data. */ fns = iorq->fns; - status = fns->write(iorq->fd, iorq->buffer, - iorq->length, 0, &result); - rdpdr_send_completion(iorq->device, iorq->id, - status, result, "", 1); - xfree(iorq->buffer); - iorq->fd = 0; + req_size = + (iorq->length - iorq->partial_len) > + 8192 ? 8192 : (iorq->length - + iorq->partial_len); + + /* never write larger chunks than 8k - chances are that it will block */ + status = fns->write(iorq->fd, + iorq->buffer + + iorq->partial_len, req_size, + iorq->offset, &result); + + if (result > 0) + { + iorq->partial_len += result; + iorq->offset += result; + } + +#if WITH_DEBUG_RDP5 + DEBUG(("RDPDR: %d bytes of data written\n", + result)); +#endif + /* only delete link if all data has been transfered */ + /* or we couldn't write */ + if ((iorq->partial_len == iorq->length) + || (result == 0)) + { +#if WITH_DEBUG_RDP5 + DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length)); +#endif + rdpdr_send_completion(iorq->device, + iorq->id, status, + iorq->partial_len, + (uint8 *) "", 1); + + iorq = rdpdr_remove_iorequest(prev, iorq); + } } break; } + } + prev = iorq; + if (iorq) + iorq = iorq->next; } + } /* Abort a pending io request for a given handle and major */ @@ -795,24 +941,28 @@ rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status) { uint32 result; - int i; struct async_iorequest *iorq; + struct async_iorequest *prev; - for (i = 0; i < MAX_ASYNC_IO_REQUESTS; i++) + iorq = g_iorequest; + prev = NULL; + while (iorq != NULL) { - iorq = &g_iorequest[i]; - // Only remove from table when major is not set, or when correct major is supplied. // Abort read should not abort a write io request. if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) { result = 0; - rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 1); - xfree(iorq->buffer); - iorq->fd = 0; + rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "", + 1); + + iorq = rdpdr_remove_iorequest(prev, iorq); return True; } + + prev = iorq; + iorq = iorq->next; } + return False; } -#endif