rdpdr.c revision d65680efa46fa49e8bf14e67b29b782510ff934c
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
Copyright (C) Matthew Chapman 1999-2007
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Sun GPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Sun elects to use only
* the General Public License version 2 (GPLv2) at this time for any software where
* a choice of GPL license versions is made available with the language indicating
* that GPLv2 or any later version may be used, or where a choice of which version
* of the GPL is applied is otherwise unspecified.
*/
/*
Here are some resources, for your IRP hacking pleasure:
*/
#include <unistd.h>
#include <dirent.h> /* opendir, closedir, readdir */
#include <time.h>
#include <errno.h>
#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_QUERY_INFORMATION 0x05
#define IRP_MJ_SET_INFORMATION 0x06
#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
extern char g_hostname[16];
extern DEVICE_FNS serial_fns;
extern DEVICE_FNS printer_fns;
extern DEVICE_FNS parallel_fns;
extern DEVICE_FNS disk_fns;
#ifdef WITH_SCARD
extern DEVICE_FNS scard_fns;
#endif
extern FILEINFO g_fileinfo[];
extern RD_BOOL g_notify_stamp;
static VCHANNEL *rdpdr_channel;
/* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
/* Table with information about rdpdr 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, */
/* if multiple ios are being done on the same fd */
struct async_iorequest
{
long timeout, /* Total timeout */
itv_timeout; /* Interval timeout (between serial characters) */
};
struct async_iorequest *g_iorequest;
/* Return device_id for a given handle */
int
{
int i;
for (i = 0; i < RDPDR_MAX_DEVICES; i++)
{
return i;
}
return -1;
}
/* Converts a windows path to a unix path */
void
convert_to_unix_filename(char *filename)
{
char *p;
{
*p = '/';
}
}
static RD_BOOL
{
{
case DEVICE_TYPE_PARALLEL:
case DEVICE_TYPE_SERIAL:
case DEVICE_TYPE_PRINTER:
case DEVICE_TYPE_SCARD:
return False;
break;
case DEVICE_TYPE_DISK:
return False;
break;
}
return True;
}
/* Add a new io request to the table containing pending io requests so it won't block rdesktop */
static RD_BOOL
{
struct async_iorequest *iorq;
if (g_iorequest == NULL)
{
if (!g_iorequest)
return False;
g_iorequest->fd = 0;
}
iorq = g_iorequest;
{
/* create new element if needed */
{
return False;
}
}
iorq->partial_len = 0;
return True;
}
static void
rdpdr_send_connect(void)
{
STREAM s;
out_uint16_le(s, 5);
s_mark_end(s);
}
static void
rdpdr_send_name(void)
{
STREAM s;
if (NULL == g_rdpdr_clientname)
{
}
out_uint16_le(s, 0x72);
out_uint32(s, 0);
out_uint32_le(s, hostlen);
s_mark_end(s);
}
/* Returns the size of the payload of the announce packet */
static int
{
int size, i;
for (i = 0; i < g_num_devices; i++)
{
{
size += 0x18;
}
}
return size;
}
static void
rdpdr_send_available(void)
{
int i;
STREAM s;
for (i = 0; i < g_num_devices; i++)
{
out_uint32_le(s, i); /* RDP Device ID */
/* Is it possible to use share names longer than 8 chars?
/astrand */
switch (g_rdpdr_device[i].device_type)
{
case DEVICE_TYPE_PRINTER:
out_uint32_le(s, driverlen);
out_uint32_le(s, printerlen);
out_uint32_le(s, bloblen);
if (printerinfo->blob)
break;
default:
out_uint32(s, 0);
}
}
#if 0
out_uint32_le(s, 0);
out_uint8s(s, 3);
out_uint32(s, 0);
#endif
s_mark_end(s);
}
void
{
STREAM s;
#ifdef WITH_SCARD
#endif
out_uint32_le(s, device);
out_uint32_le(s, id);
out_uint32_le(s, status);
out_uint32_le(s, result);
s_mark_end(s);
/* JIF */
#ifdef WITH_DEBUG_RDP5
printf("--> rdpdr_send_completion\n");
/* hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
#endif
#ifdef WITH_SCARD
#endif
}
static void
{
length = 0,
desired_access = 0,
file,
id,
in_uint32_le(s, device);
in_uint32_le(s, file);
in_uint32_le(s, id);
in_uint32_le(s, major);
in_uint32_le(s, minor);
buffer_len = 0;
buffer[0] = 0;
{
case DEVICE_TYPE_SERIAL:
fns = &serial_fns;
rw_blocking = False;
break;
case DEVICE_TYPE_PARALLEL:
fns = ¶llel_fns;
rw_blocking = False;
break;
case DEVICE_TYPE_PRINTER:
fns = &printer_fns;
break;
case DEVICE_TYPE_DISK:
rw_blocking = False;
break;
case DEVICE_TYPE_SCARD:
#ifdef WITH_SCARD
rw_blocking = False;
break;
#endif
default:
return;
}
switch (major)
{
case IRP_MJ_CREATE:
in_uint32_le(s, error_mode);
in_uint32_le(s, share_mode);
in_uint32_le(s, disposition);
in_uint32_le(s, length);
{
}
else
{
filename[0] = 0;
}
{
break;
}
buffer_len = 1;
break;
case IRP_MJ_CLOSE:
{
break;
}
break;
case IRP_MJ_READ:
{
break;
}
in_uint32_le(s, length);
in_uint32_le(s, offset);
#if WITH_DEBUG_RDP5
#endif
{
break;
}
if (rw_blocking) /* Complete read immediately */
{
if (!buffer)
{
break;
}
buffer_len = result;
break;
}
/* Add request to table */
if (!pst_buf)
{
break;
}
{
break;
}
break;
case IRP_MJ_WRITE:
buffer_len = 1;
{
break;
}
in_uint32_le(s, length);
in_uint32_le(s, offset);
in_uint8s(s, 0x18);
#if WITH_DEBUG_RDP5
#endif
{
break;
}
if (rw_blocking) /* Complete immediately */
{
break;
}
/* Add to table */
if (!pst_buf)
{
break;
}
{
break;
}
break;
case IRP_MJ_QUERY_INFORMATION:
{
break;
}
in_uint32_le(s, info_level);
break;
case IRP_MJ_SET_INFORMATION:
{
break;
}
in_uint32_le(s, info_level);
break;
{
break;
}
in_uint32_le(s, info_level);
break;
case IRP_MJ_DIRECTORY_CONTROL:
{
break;
}
switch (minor)
{
case IRP_MN_QUERY_DIRECTORY:
in_uint32_le(s, info_level);
in_uint8s(s, 1);
in_uint32_le(s, length);
in_uint8s(s, 0x17);
{
length);
}
else
{
filename[0] = 0;
}
&out);
if (!buffer_len)
buffer_len++;
break;
/* JIF
unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
result = 0;
if (status == RD_STATUS_PENDING)
break;
default:
/* JIF */
}
break;
case IRP_MJ_DEVICE_CONTROL:
if (!fns->device_control)
{
break;
}
in_uint32_le(s, bytes_out);
in_uint32_le(s, bytes_in);
in_uint32_le(s, request);
in_uint8s(s, 0x14);
if (!buffer)
{
break;
}
#ifdef WITH_SCARD
#endif
/* Serial SERIAL_WAIT_ON_MASK */
if (status == RD_STATUS_PENDING)
{
{
break;
}
}
#ifdef WITH_SCARD
#endif
break;
case IRP_MJ_LOCK_CONTROL:
{
break;
}
in_uint32_le(s, info_level);
/* FIXME: Perhaps consider actually *do*
something here :-) */
break;
default:
break;
}
if (status != RD_STATUS_PENDING)
{
}
if (buffer)
}
static void
{
STREAM s;
out_uint32_le(s, 1);
out_uint32_le(s, 2);
out_uint16_le(s, 2);
out_uint16_le(s, 5);
out_uint16_le(s, 1);
out_uint16_le(s, 5);
out_uint16_le(s, 0xFFFF);
out_uint16_le(s, 0);
out_uint32_le(s, 0);
out_uint32_le(s, 3);
out_uint32_le(s, 0);
out_uint32_le(s, 0);
out_uint32_le(s, 1);
out_uint32_le(s, 1);
out_uint32_le(s, 1);
out_uint32_le(s, 1);
s_mark_end(s);
}
static void
{
#if WITH_DEBUG_RDP5
printf("--- rdpdr_process ---\n");
#endif
{
{
return;
}
{
return;
}
{
/* connect from server */
return;
}
{
/* connect to a specific resource */
#if WITH_DEBUG_RDP5
#endif
return;
}
{
/* server capability */
return;
}
}
{
{
return;
}
}
}
{
if (g_num_devices > 0)
{
channel_register("rdpdr",
}
return (rdpdr_channel != NULL);
}
/* Add file descriptors of pending io request to select() */
void
{
struct async_iorequest *iorq;
char c;
iorq = g_iorequest;
{
{
{
case IRP_MJ_READ:
/* Is this FD valid? FDs will
be invalid when
reconnecting. FIXME: Real
support for reconnects. */
/* Check if io request timeout is smaller than current (but not 0). */
&& (select_timeout == 0
{
/* Set new timeout */
break;
}
&& (select_timeout == 0
{
/* Set new timeout */
break;
}
break;
case IRP_MJ_WRITE:
/* FD still valid? See above. */
break;
break;
case IRP_MJ_DEVICE_CONTROL:
if (select_timeout > 5)
break;
}
}
}
}
struct async_iorequest *
{
if (!iorq)
return NULL;
if (prev)
{
}
else
{
/* Even if NULL */
}
return iorq;
}
/* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
static void
{
struct async_iorequest *iorq;
struct async_iorequest *prev;
if (timed_out)
{
/* check serial iv_timeout */
iorq = g_iorequest;
{
{
if ((iorq->partial_len > 0) &&
{
/* iv_timeout between 2 chars, send partial_len */
/*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
return;
}
else
{
break;
}
}
else
{
break;
}
if (iorq)
}
return;
}
iorq = g_iorequest;
{
{
{
case IRP_MJ_READ:
{
/* Read the data */
req_size =
iorq->partial_len);
/* never read larger chunks than 8k - chances are that it will block */
if ((long) result > 0)
{
}
#if WITH_DEBUG_RDP5
#endif
/* only delete link if all data has been transfered */
/* or if result was 0 and status success - EOF */
(result == 0))
{
#if WITH_DEBUG_RDP5
#endif
iorq->partial_len);
}
}
break;
case IRP_MJ_WRITE:
{
/* Write data. */
req_size =
iorq->partial_len);
/* never write larger chunks than 8k - chances are that it will block */
if ((long) result > 0)
{
}
#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 */
|| (result == 0))
{
#if WITH_DEBUG_RDP5
#endif
}
}
break;
case IRP_MJ_DEVICE_CONTROL:
{
}
break;
}
}
if (iorq)
}
/* Check notify */
iorq = g_iorequest;
{
{
{
case IRP_MJ_DIRECTORY_CONTROL:
{
if (g_notify_stamp)
{
if (status != RD_STATUS_PENDING)
{
status, 0,
NULL, 0);
iorq);
}
}
}
break;
}
}
if (iorq)
}
}
void
{
/* fist check event queue only,
any serial wait event must be done before read block will be sent
*/
}
/* Abort a pending io request for a given handle and major */
{
struct async_iorequest *iorq;
struct async_iorequest *prev;
iorq = g_iorequest;
{
/* Only remove from table when major is not set, or when correct major is supplied.
Abort read should not abort a write io request. */
{
result = 0;
1);
return True;
}
}
return False;
}