2011-08-04 19:22:58 +04:00
|
|
|
/**
|
2012-10-09 05:00:07 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Device Redirection Virtual Channel
|
2011-08-04 19:22:58 +04:00
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
2012-10-09 05:00:07 +04:00
|
|
|
* Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2011-08-04 19:22:58 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-08-05 06:24:46 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-10-08 06:53:24 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-10-17 23:42:51 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <freerdp/types.h>
|
2012-10-07 18:21:54 +04:00
|
|
|
#include <freerdp/constants.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2011-08-05 06:24:46 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-05 11:43:48 +04:00
|
|
|
#include "rdpdr_capabilities.h"
|
2012-10-09 05:00:07 +04:00
|
|
|
|
2011-08-05 21:54:40 +04:00
|
|
|
#include "devman.h"
|
|
|
|
#include "irp.h"
|
2012-10-09 04:26:11 +04:00
|
|
|
|
2011-08-05 11:43:48 +04:00
|
|
|
#include "rdpdr_main.h"
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
static void rdpdr_process_connect(rdpdrPlugin* rdpdr)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
UINT32 index;
|
2012-11-09 04:01:52 +04:00
|
|
|
RDPDR_DEVICE* device;
|
|
|
|
rdpSettings* settings;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
rdpdr->devman = devman_new(rdpdr);
|
2013-10-18 01:39:48 +04:00
|
|
|
settings = (rdpSettings*) rdpdr->channelEntryPoints.pExtendedData;
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
strncpy(rdpdr->computerName, settings->ComputerName, sizeof(rdpdr->computerName) - 1);
|
2012-10-07 18:21:54 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
for (index = 0; index < settings->DeviceCount; index++)
|
|
|
|
{
|
|
|
|
device = settings->DeviceArray[index];
|
|
|
|
devman_load_device_service(rdpdr->devman, device);
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
static void rdpdr_process_server_announce_request(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-05 06:24:46 +04:00
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT16(s, rdpdr->versionMajor);
|
|
|
|
Stream_Read_UINT16(s, rdpdr->versionMinor);
|
|
|
|
Stream_Read_UINT32(s, rdpdr->clientID);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_send_client_announce_reply(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
s = Stream_New(NULL, 12);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_CLIENTID_CONFIRM);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT16(s, rdpdr->versionMajor);
|
|
|
|
Stream_Write_UINT16(s, rdpdr->versionMinor);
|
|
|
|
Stream_Write_UINT32(s, (UINT32) rdpdr->clientID);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
rdpdr_send(rdpdr, s);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2013-01-11 00:30:32 +04:00
|
|
|
WCHAR* computerNameW = NULL;
|
2012-02-10 05:04:27 +04:00
|
|
|
size_t computerNameLenW;
|
2011-08-05 06:24:46 +04:00
|
|
|
|
|
|
|
if (!rdpdr->computerName[0])
|
|
|
|
gethostname(rdpdr->computerName, sizeof(rdpdr->computerName) - 1);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2012-12-17 19:20:25 +04:00
|
|
|
computerNameLenW = ConvertToUnicode(CP_UTF8, 0, rdpdr->computerName, -1, &computerNameW, 0) * 2;
|
2012-09-24 03:49:13 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
s = Stream_New(NULL, 16 + computerNameLenW + 2);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_CLIENT_NAME);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT32(s, 1); /* unicodeFlag, 0 for ASCII and 1 for Unicode */
|
|
|
|
Stream_Write_UINT32(s, 0); /* codePage, must be set to zero */
|
|
|
|
Stream_Write_UINT32(s, computerNameLenW + 2); /* computerNameLen, including null terminator */
|
|
|
|
Stream_Write(s, computerNameW, computerNameLenW);
|
|
|
|
Stream_Write_UINT16(s, 0); /* null terminator */
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(computerNameW);
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
rdpdr_send(rdpdr, s);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
static void rdpdr_process_server_clientid_confirm(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 versionMajor;
|
|
|
|
UINT16 versionMinor;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 clientID;
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT16(s, versionMajor);
|
|
|
|
Stream_Read_UINT16(s, versionMinor);
|
|
|
|
Stream_Read_UINT32(s, clientID);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
if (versionMajor != rdpdr->versionMajor || versionMinor != rdpdr->versionMinor)
|
|
|
|
{
|
|
|
|
rdpdr->versionMajor = versionMajor;
|
|
|
|
rdpdr->versionMinor = versionMinor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientID != rdpdr->clientID)
|
|
|
|
{
|
|
|
|
rdpdr->clientID = clientID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 00:30:36 +04:00
|
|
|
static void rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL userLoggedOn)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2012-02-10 05:04:27 +04:00
|
|
|
int i;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE c;
|
2013-10-18 00:30:36 +04:00
|
|
|
int pos;
|
|
|
|
int index;
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 count;
|
2011-08-05 18:44:06 +04:00
|
|
|
int data_len;
|
|
|
|
int count_pos;
|
2012-02-10 05:04:27 +04:00
|
|
|
DEVICE* device;
|
2013-10-18 00:30:36 +04:00
|
|
|
int keyCount;
|
|
|
|
ULONG_PTR* pKeys;
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
s = Stream_New(NULL, 256);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_ANNOUNCE);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2014-02-10 10:06:11 +04:00
|
|
|
count_pos = (int) Stream_GetPosition(s);
|
2011-08-05 18:44:06 +04:00
|
|
|
count = 0;
|
2013-10-18 00:30:36 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Seek_UINT32(s); /* deviceCount */
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2013-10-18 00:30:36 +04:00
|
|
|
pKeys = NULL;
|
|
|
|
keyCount = ListDictionary_GetKeys(rdpdr->devman->devices, &pKeys);
|
|
|
|
|
|
|
|
for (index = 0; index < keyCount; index++)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2013-10-18 00:30:36 +04:00
|
|
|
device = (DEVICE*) ListDictionary_GetItemValue(rdpdr->devman->devices, (void*) pKeys[index]);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. versionMinor 0x0005 doesn't send PAKID_CORE_USER_LOGGEDON
|
|
|
|
* so all devices should be sent regardless of user_loggedon
|
|
|
|
* 2. smartcard devices should be always sent
|
|
|
|
* 3. other devices are sent only after user_loggedon
|
|
|
|
*/
|
2012-11-21 18:30:06 +04:00
|
|
|
|
|
|
|
if ((rdpdr->versionMinor == 0x0005) ||
|
2013-10-18 00:30:36 +04:00
|
|
|
(device->type == RDPDR_DTYP_SMARTCARD) || userLoggedOn)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
data_len = (int) (device->data == NULL ? 0 : Stream_GetPosition(device->data));
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_EnsureRemainingCapacity(s, 20 + data_len);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT32(s, device->type); /* deviceType */
|
|
|
|
Stream_Write_UINT32(s, device->id); /* deviceID */
|
|
|
|
strncpy((char*) Stream_Pointer(s), device->name, 8);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Peek_UINT8(s, c);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
if (c > 0x7F)
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT8(s, '_');
|
2011-08-05 18:44:06 +04:00
|
|
|
else
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Seek_UINT8(s);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT32(s, data_len);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
if (data_len > 0)
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write(s, Stream_Buffer(device->data), data_len);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
count++;
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "registered device #%d: %s (type=%d id=%d)\n",
|
2011-08-05 18:44:06 +04:00
|
|
|
count, device->name, device->type, device->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (pKeys)
|
|
|
|
free(pKeys);
|
|
|
|
|
2014-02-10 10:06:11 +04:00
|
|
|
pos = (int) Stream_GetPosition(s);
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_SetPosition(s, count_pos);
|
|
|
|
Stream_Write_UINT32(s, count);
|
|
|
|
Stream_SetPosition(s, pos);
|
|
|
|
Stream_SealLength(s);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
rdpdr_send(rdpdr, s);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
static BOOL rdpdr_process_irp(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-05 21:54:40 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
irp = irp_new(rdpdr->devman, s);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
if (!irp)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-05 21:54:40 +04:00
|
|
|
|
|
|
|
IFCALL(irp->device->IRPRequest, irp->device, irp);
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-05 21:54:40 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
static void rdpdr_process_receive(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 component;
|
|
|
|
UINT16 packetID;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 deviceID;
|
|
|
|
UINT32 status;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT16(s, component);
|
|
|
|
Stream_Read_UINT16(s, packetID);
|
2011-08-04 19:22:58 +04:00
|
|
|
|
|
|
|
if (component == RDPDR_CTYP_CORE)
|
|
|
|
{
|
|
|
|
switch (packetID)
|
|
|
|
{
|
|
|
|
case PAKID_CORE_SERVER_ANNOUNCE:
|
2013-10-17 22:19:33 +04:00
|
|
|
rdpdr_process_server_announce_request(rdpdr, s);
|
2011-08-05 06:24:46 +04:00
|
|
|
rdpdr_send_client_announce_reply(rdpdr);
|
|
|
|
rdpdr_send_client_name_request(rdpdr);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_SERVER_CAPABILITY:
|
2013-10-17 22:19:33 +04:00
|
|
|
rdpdr_process_capability_request(rdpdr, s);
|
2011-08-05 11:43:48 +04:00
|
|
|
rdpdr_send_capability_response(rdpdr);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_CLIENTID_CONFIRM:
|
2013-10-17 22:19:33 +04:00
|
|
|
rdpdr_process_server_clientid_confirm(rdpdr, s);
|
2012-10-09 10:31:28 +04:00
|
|
|
rdpdr_send_device_list_announce_request(rdpdr, FALSE);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_USER_LOGGEDON:
|
2012-10-09 10:31:28 +04:00
|
|
|
rdpdr_send_device_list_announce_request(rdpdr, TRUE);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_DEVICE_REPLY:
|
|
|
|
/* connect to a specific resource */
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT32(s, deviceID);
|
|
|
|
Stream_Read_UINT32(s, status);
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_DEVICE_IOREQUEST:
|
2013-10-17 22:19:33 +04:00
|
|
|
if (rdpdr_process_irp(rdpdr, s))
|
|
|
|
s = NULL;
|
2011-08-04 19:22:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (component == RDPDR_CTYP_PRN)
|
|
|
|
{
|
2013-10-17 23:42:51 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-17 23:42:51 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 23:20:04 +04:00
|
|
|
|
|
|
|
/****************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
static wListDictionary* g_InitHandles;
|
|
|
|
static wListDictionary* g_OpenHandles;
|
|
|
|
|
|
|
|
void rdpdr_add_init_handle_data(void* pInitHandle, void* pUserData)
|
|
|
|
{
|
|
|
|
if (!g_InitHandles)
|
|
|
|
g_InitHandles = ListDictionary_New(TRUE);
|
|
|
|
|
|
|
|
ListDictionary_Add(g_InitHandles, pInitHandle, pUserData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* rdpdr_get_init_handle_data(void* pInitHandle)
|
|
|
|
{
|
|
|
|
void* pUserData = NULL;
|
|
|
|
pUserData = ListDictionary_GetItemValue(g_InitHandles, pInitHandle);
|
|
|
|
return pUserData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdpdr_remove_init_handle_data(void* pInitHandle)
|
|
|
|
{
|
|
|
|
ListDictionary_Remove(g_InitHandles, pInitHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdpdr_add_open_handle_data(DWORD openHandle, void* pUserData)
|
|
|
|
{
|
|
|
|
void* pOpenHandle = (void*) (size_t) openHandle;
|
|
|
|
|
|
|
|
if (!g_OpenHandles)
|
|
|
|
g_OpenHandles = ListDictionary_New(TRUE);
|
|
|
|
|
|
|
|
ListDictionary_Add(g_OpenHandles, pOpenHandle, pUserData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* rdpdr_get_open_handle_data(DWORD openHandle)
|
|
|
|
{
|
|
|
|
void* pUserData = NULL;
|
|
|
|
void* pOpenHandle = (void*) (size_t) openHandle;
|
|
|
|
pUserData = ListDictionary_GetItemValue(g_OpenHandles, pOpenHandle);
|
|
|
|
return pUserData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdpdr_remove_open_handle_data(DWORD openHandle)
|
|
|
|
{
|
|
|
|
void* pOpenHandle = (void*) (size_t) openHandle;
|
|
|
|
ListDictionary_Remove(g_OpenHandles, pOpenHandle);
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
int rdpdr_send(rdpdrPlugin* rdpdr, wStream* s)
|
|
|
|
{
|
|
|
|
UINT32 status = 0;
|
2013-10-17 23:42:51 +04:00
|
|
|
rdpdrPlugin* plugin = (rdpdrPlugin*) rdpdr;
|
2013-10-17 22:19:33 +04:00
|
|
|
|
|
|
|
if (!plugin)
|
2014-02-10 10:06:11 +04:00
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
status = CHANNEL_RC_BAD_INIT_HANDLE;
|
2014-02-10 10:06:11 +04:00
|
|
|
}
|
2013-10-17 22:19:33 +04:00
|
|
|
else
|
2014-02-10 10:06:11 +04:00
|
|
|
{
|
2013-10-18 01:39:48 +04:00
|
|
|
status = plugin->channelEntryPoints.pVirtualChannelWrite(plugin->OpenHandle,
|
2014-02-10 10:06:11 +04:00
|
|
|
Stream_Buffer(s), (UINT32) Stream_GetPosition(s), s);
|
|
|
|
}
|
2013-10-17 22:19:33 +04:00
|
|
|
|
|
|
|
if (status != CHANNEL_RC_OK)
|
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
fprintf(stderr, "rdpdr_send: VirtualChannelWrite failed %d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
static void rdpdr_virtual_channel_event_data_received(rdpdrPlugin* rdpdr,
|
2013-10-17 23:20:04 +04:00
|
|
|
void* pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
|
|
|
|
{
|
|
|
|
wStream* data_in;
|
|
|
|
|
|
|
|
if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
|
|
|
|
* This flag is only valid in server-to-client virtual channel traffic. It MUST be
|
|
|
|
* ignored in client-to-server data." Thus it would be best practice to cease data
|
|
|
|
* transmission. However, simply returning here avoids a crash.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataFlags & CHANNEL_FLAG_FIRST)
|
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
if (rdpdr->data_in != NULL)
|
|
|
|
Stream_Free(rdpdr->data_in, TRUE);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->data_in = Stream_New(NULL, totalLength);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
data_in = rdpdr->data_in;
|
2013-10-17 23:20:04 +04:00
|
|
|
Stream_EnsureRemainingCapacity(data_in, (int) dataLength);
|
|
|
|
Stream_Write(data_in, pData, dataLength);
|
|
|
|
|
|
|
|
if (dataFlags & CHANNEL_FLAG_LAST)
|
|
|
|
{
|
|
|
|
if (Stream_Capacity(data_in) != Stream_GetPosition(data_in))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "svc_plugin_process_received: read error\n");
|
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->data_in = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
Stream_SealLength(data_in);
|
|
|
|
Stream_SetPosition(data_in, 0);
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
MessageQueue_Post(rdpdr->MsgPipe->In, NULL, 0, (void*) data_in, NULL);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:27:36 +04:00
|
|
|
static VOID rdpdr_virtual_channel_open_event(DWORD openHandle, UINT event,
|
|
|
|
LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr = (rdpdrPlugin*) rdpdr_get_open_handle_data(openHandle);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (!rdpdr)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
fprintf(stderr, "rdpdr_virtual_channel_open_event: error no match\n");
|
2013-10-17 23:20:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case CHANNEL_EVENT_DATA_RECEIVED:
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_virtual_channel_event_data_received(rdpdr, pData, dataLength, totalLength, dataFlags);
|
2013-10-17 23:20:04 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_WRITE_COMPLETE:
|
|
|
|
Stream_Free((wStream*) pData, TRUE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_USER:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* rdpdr_virtual_channel_client_thread(void* arg)
|
|
|
|
{
|
|
|
|
wStream* data;
|
|
|
|
wMessage message;
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*) arg;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_process_connect(rdpdr);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
if (!MessageQueue_Wait(rdpdr->MsgPipe->In))
|
2013-10-17 23:20:04 +04:00
|
|
|
break;
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (MessageQueue_Peek(rdpdr->MsgPipe->In, &message, TRUE))
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
if (message.id == WMQ_QUIT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (message.id == 0)
|
|
|
|
{
|
|
|
|
data = (wStream*) message.wParam;
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_process_receive(rdpdr, data);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:27:36 +04:00
|
|
|
static void rdpdr_virtual_channel_event_connected(rdpdrPlugin* rdpdr, LPVOID pData, UINT32 dataLength)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
UINT32 status;
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
status = rdpdr->channelEntryPoints.pVirtualChannelOpen(rdpdr->InitHandle,
|
|
|
|
&rdpdr->OpenHandle, rdpdr->channelDef.name, rdpdr_virtual_channel_open_event);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_add_open_handle_data(rdpdr->OpenHandle, rdpdr);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
|
|
|
if (status != CHANNEL_RC_OK)
|
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
fprintf(stderr, "rdpdr_virtual_channel_event_connected: open failed: status: %d\n", status);
|
2013-10-17 23:20:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->MsgPipe = MessagePipe_New();
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) rdpdr_virtual_channel_client_thread, (void*) rdpdr, 0, NULL);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
static void rdpdr_virtual_channel_event_terminated(rdpdrPlugin* rdpdr)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
MessagePipe_PostQuit(rdpdr->MsgPipe, 0);
|
|
|
|
WaitForSingleObject(rdpdr->thread, INFINITE);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
MessagePipe_Free(rdpdr->MsgPipe);
|
|
|
|
CloseHandle(rdpdr->thread);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->channelEntryPoints.pVirtualChannelClose(rdpdr->OpenHandle);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (rdpdr->data_in)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
Stream_Free(rdpdr->data_in, TRUE);
|
|
|
|
rdpdr->data_in = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (rdpdr->devman)
|
|
|
|
{
|
|
|
|
devman_free(rdpdr->devman);
|
|
|
|
rdpdr->devman = NULL;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_remove_open_handle_data(rdpdr->OpenHandle);
|
|
|
|
rdpdr_remove_init_handle_data(rdpdr->InitHandle);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_virtual_channel_init_event(void* pInitHandle, UINT32 event, void* pData, UINT32 dataLength)
|
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr = (rdpdrPlugin*) rdpdr_get_init_handle_data(pInitHandle);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (!rdpdr)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
fprintf(stderr, "rdpdr_virtual_channel_init_event: error no match\n");
|
2013-10-17 23:20:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case CHANNEL_EVENT_CONNECTED:
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_virtual_channel_event_connected(rdpdr, pData, dataLength);
|
2013-10-17 23:20:04 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_DISCONNECTED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_TERMINATED:
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_virtual_channel_event_terminated(rdpdr);
|
2013-10-17 23:20:04 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 06:48:17 +04:00
|
|
|
/* rdpdr is always built-in */
|
2012-10-08 06:53:24 +04:00
|
|
|
#define VirtualChannelEntry rdpdr_VirtualChannelEntry
|
|
|
|
|
2012-11-01 07:04:31 +04:00
|
|
|
int VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
|
2012-10-08 06:53:24 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2014-02-17 02:27:36 +04:00
|
|
|
rdpdr = (rdpdrPlugin*) calloc(1, sizeof(rdpdrPlugin));
|
|
|
|
|
|
|
|
if (!rdpdr)
|
|
|
|
return -1;
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->channelDef.options =
|
2012-10-08 06:53:24 +04:00
|
|
|
CHANNEL_OPTION_INITIALIZED |
|
|
|
|
CHANNEL_OPTION_ENCRYPT_RDP |
|
|
|
|
CHANNEL_OPTION_COMPRESS_RDP;
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
strcpy(rdpdr->channelDef.name, "rdpdr");
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2014-02-17 02:27:36 +04:00
|
|
|
CopyMemory(&(rdpdr->channelEntryPoints), pEntryPoints, sizeof(CHANNEL_ENTRY_POINTS_EX));
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->channelEntryPoints.pVirtualChannelInit(&rdpdr->InitHandle,
|
|
|
|
&rdpdr->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000, rdpdr_virtual_channel_init_event);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_add_init_handle_data(rdpdr->InitHandle, (void*) rdpdr);
|
2012-10-08 06:53:24 +04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|