--- sourceforge.net/trunk/rdesktop/rdpdr.c 2004/02/07 17:32:21 602 +++ sourceforge.net/trunk/rdesktop/rdpdr.c 2004/04/05 19:23:08 647 @@ -1,5 +1,7 @@ #include #include +#include +#include /* opendir, closedir, readdir */ #include #include "rdesktop.h" @@ -27,6 +29,7 @@ extern DEVICE_FNS printer_fns; extern DEVICE_FNS parallel_fns; extern DEVICE_FNS disk_fns; +extern FILEINFO g_fileinfo[]; static VCHANNEL *rdpdr_channel; @@ -36,6 +39,7 @@ /* Table with information about rdpdr devices */ RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; +char * g_rdpdr_clientname = NULL; /* 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, */ @@ -78,16 +82,39 @@ } } +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) { struct async_iorequest *iorq; if (g_iorequest == NULL) { g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); + if (!g_iorequest) + return False; g_iorequest->fd = 0; g_iorequest->next = NULL; } @@ -101,6 +128,8 @@ { iorq->next = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); + if (!iorq->next) + return False; iorq->next->fd = 0; iorq->next->next = NULL; } @@ -116,6 +145,7 @@ iorq->timeout = total_timeout; iorq->itv_timeout = interval_timeout; iorq->buffer = buffer; + iorq->offset = offset; return True; } @@ -139,8 +169,13 @@ 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); @@ -148,7 +183,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); } @@ -384,9 +419,20 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset)); #endif + 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; @@ -394,10 +440,15 @@ // 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; @@ -421,6 +472,12 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR IRP Write (length: %d)\n", result)); #endif + 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); @@ -429,10 +486,16 @@ // 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; @@ -552,6 +615,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); @@ -567,8 +636,9 @@ { rdpdr_send_completion(device, id, status, result, buffer, buffer_len); } - xfree(buffer); - + if (buffer) + xfree(buffer); + buffer = NULL; } void @@ -687,7 +757,7 @@ void rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout) { - 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; iorq = g_iorequest; @@ -727,6 +797,29 @@ } } +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 @@ -766,9 +859,13 @@ /* never read larger chunks than 8k - chances are that it will block */ status = fns->read(iorq->fd, iorq->buffer + iorq->partial_len, - req_size, 0, &result); - iorq->partial_len += result; + 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 @@ -780,26 +877,12 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length)); #endif - /* send the data */ - status = STATUS_SUCCESS; rdpdr_send_completion(iorq->device, iorq->id, status, iorq->partial_len, iorq->buffer, iorq->partial_len); - xfree(iorq->buffer); - iorq->fd = 0; - if (prev != NULL) - { - prev->next = iorq->next; - xfree(iorq); - } - else - { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); - } + iorq = rdpdr_remove_iorequest(prev, iorq); } } break; @@ -817,9 +900,15 @@ /* never write larger chunks than 8k - chances are that it will block */ status = fns->write(iorq->fd, iorq->buffer + - iorq->partial_len, req_size, 0, - &result); - iorq->partial_len += result; + 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)); @@ -832,26 +921,12 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length)); #endif - /* send a status success */ - status = STATUS_SUCCESS; rdpdr_send_completion(iorq->device, iorq->id, status, - iorq->partial_len, (uint8*)"", - 1); + iorq->partial_len, + (uint8 *) "", 1); - xfree(iorq->buffer); - iorq->fd = 0; - if (prev != NULL) - { - prev->next = iorq->next; - xfree(iorq); - } - else - { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); - } + iorq = rdpdr_remove_iorequest(prev, iorq); } } break; @@ -859,7 +934,8 @@ } prev = iorq; - iorq = iorq->next; + if (iorq) + iorq = iorq->next; } } @@ -881,20 +957,10 @@ if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) { result = 0; - rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8*)"", 1); - xfree(iorq->buffer); - iorq->fd = 0; - if (prev != NULL) - { - prev->next = iorq->next; - xfree(iorq); - } - else - { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); - } + rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "", + 1); + + iorq = rdpdr_remove_iorequest(prev, iorq); return True; }