FreeRDP/channels/drdynvc/client/drdynvc_main.c

489 lines
11 KiB
C
Raw Normal View History

/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* Dynamic Virtual Channel
*
* Copyright 2010-2011 Vic Lee
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/crt.h>
#include <winpr/stream.h>
#include <freerdp/constants.h>
#include "dvcman.h"
#include "drdynvc_types.h"
#include "drdynvc_main.h"
2014-06-17 21:34:23 +04:00
static int drdynvc_write_variable_uint(wStream* s, UINT32 val)
{
int cb;
if (val <= 0xFF)
{
cb = 0;
2014-06-17 21:34:23 +04:00
Stream_Write_UINT8(s, val);
}
else if (val <= 0xFFFF)
{
cb = 1;
2014-06-17 21:34:23 +04:00
Stream_Write_UINT16(s, val);
}
else
{
2012-04-25 22:27:38 +04:00
cb = 2;
2014-06-17 21:34:23 +04:00
Stream_Write_UINT32(s, val);
}
return cb;
}
2014-06-17 21:34:23 +04:00
int drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId, BYTE* data, UINT32 dataSize)
{
wStream* data_out;
2012-10-09 11:26:39 +04:00
UINT32 pos = 0;
UINT32 cbChId;
UINT32 cbLen;
2014-06-17 21:34:23 +04:00
UINT32 chunkLength;
int status;
2014-06-17 21:34:23 +04:00
DEBUG_DVC("ChannelId=%d size=%d", ChannelId, dataSize);
2012-04-25 22:27:38 +04:00
if (drdynvc->channel_error != CHANNEL_RC_OK)
return 1;
2013-05-09 01:48:30 +04:00
data_out = Stream_New(NULL, CHANNEL_CHUNK_LENGTH);
Stream_SetPosition(data_out, 1);
cbChId = drdynvc_write_variable_uint(data_out, ChannelId);
2014-06-17 21:34:23 +04:00
if (dataSize == 0)
{
pos = Stream_GetPosition(data_out);
Stream_SetPosition(data_out, 0);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT8(data_out, 0x40 | cbChId);
Stream_SetPosition(data_out, pos);
2014-06-17 21:34:23 +04:00
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
}
2014-06-17 21:34:23 +04:00
else if (dataSize <= CHANNEL_CHUNK_LENGTH - pos)
{
pos = Stream_GetPosition(data_out);
Stream_SetPosition(data_out, 0);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT8(data_out, 0x30 | cbChId);
Stream_SetPosition(data_out, pos);
2014-06-17 21:34:23 +04:00
Stream_Write(data_out, data, dataSize);
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
}
else
{
/* Fragment the data */
2014-06-17 21:34:23 +04:00
cbLen = drdynvc_write_variable_uint(data_out, dataSize);
pos = Stream_GetPosition(data_out);
Stream_SetPosition(data_out, 0);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT8(data_out, 0x20 | cbChId | (cbLen << 2));
Stream_SetPosition(data_out, pos);
2014-06-17 21:34:23 +04:00
chunkLength = CHANNEL_CHUNK_LENGTH - pos;
Stream_Write(data_out, data, chunkLength);
data += chunkLength;
dataSize -= chunkLength;
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
2014-06-17 21:34:23 +04:00
while (status == CHANNEL_RC_OK && dataSize > 0)
{
2013-05-09 01:48:30 +04:00
data_out = Stream_New(NULL, CHANNEL_CHUNK_LENGTH);
Stream_SetPosition(data_out, 1);
cbChId = drdynvc_write_variable_uint(data_out, ChannelId);
pos = Stream_GetPosition(data_out);
Stream_SetPosition(data_out, 0);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT8(data_out, 0x30 | cbChId);
Stream_SetPosition(data_out, pos);
2014-06-17 21:34:23 +04:00
chunkLength = dataSize;
if (chunkLength > CHANNEL_CHUNK_LENGTH - pos)
chunkLength = CHANNEL_CHUNK_LENGTH - pos;
Stream_Write(data_out, data, chunkLength);
data += chunkLength;
dataSize -= chunkLength;
status = svc_plugin_send((rdpSvcPlugin*)drdynvc, data_out);
}
}
2014-06-17 21:34:23 +04:00
if (status != CHANNEL_RC_OK)
{
2014-06-17 21:34:23 +04:00
drdynvc->channel_error = status;
DEBUG_WARN("VirtualChannelWrite failed %d", status);
return 1;
}
return 0;
}
int drdynvc_push_event(drdynvcPlugin* drdynvc, wMessage* event)
{
2014-06-17 21:34:23 +04:00
int status;
2014-06-17 21:34:23 +04:00
status = svc_plugin_send_event((rdpSvcPlugin*) drdynvc, event);
2014-06-17 21:34:23 +04:00
if (status != CHANNEL_RC_OK)
{
2014-06-17 21:34:23 +04:00
DEBUG_WARN("pVirtualChannelEventPush failed %d", status);
return 1;
}
return 0;
}
static int drdynvc_send_capability_response(drdynvcPlugin* drdynvc)
{
int status;
2014-06-17 21:34:23 +04:00
wStream* s;
2014-06-17 21:34:23 +04:00
s = Stream_New(NULL, 4);
Stream_Write_UINT16(s, 0x0050); /* Cmd+Sp+cbChId+Pad. Note: MSTSC sends 0x005c */
Stream_Write_UINT16(s, drdynvc->version);
2014-06-17 21:34:23 +04:00
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, s);
if (status != CHANNEL_RC_OK)
{
DEBUG_WARN("VirtualChannelWrite failed %d", status);
return 1;
}
return status;
}
static int drdynvc_process_capability_request(drdynvcPlugin* drdynvc, int Sp, int cbChId, wStream* s)
{
int status;
DEBUG_DVC("Sp=%d cbChId=%d", Sp, cbChId);
Stream_Seek(s, 1); /* pad */
2013-05-09 00:09:16 +04:00
Stream_Read_UINT16(s, drdynvc->version);
2013-05-10 23:57:22 +04:00
/* RDP8 servers offer version 3, though Microsoft forgot to document it
* in their early documents. It behaves the same as version 2.
*/
if ((drdynvc->version == 2) || (drdynvc->version == 3))
{
2013-05-09 00:09:16 +04:00
Stream_Read_UINT16(s, drdynvc->PriorityCharge0);
Stream_Read_UINT16(s, drdynvc->PriorityCharge1);
Stream_Read_UINT16(s, drdynvc->PriorityCharge2);
Stream_Read_UINT16(s, drdynvc->PriorityCharge3);
}
status = drdynvc_send_capability_response(drdynvc);
drdynvc->channel_error = status;
drdynvc->state = DRDYNVC_STATE_READY;
2012-04-25 22:27:38 +04:00
return 0;
}
2014-06-17 21:34:23 +04:00
static UINT32 drdynvc_read_variable_uint(wStream* s, int cbLen)
{
2012-10-09 11:26:39 +04:00
UINT32 val;
switch (cbLen)
{
case 0:
2014-06-17 21:34:23 +04:00
Stream_Read_UINT8(s, val);
break;
case 1:
2014-06-17 21:34:23 +04:00
Stream_Read_UINT16(s, val);
break;
default:
2014-06-17 21:34:23 +04:00
Stream_Read_UINT32(s, val);
break;
}
return val;
}
static int drdynvc_process_create_request(drdynvcPlugin* drdynvc, int Sp, int cbChId, wStream* s)
{
int pos;
int status;
2012-10-09 11:26:39 +04:00
UINT32 ChannelId;
wStream* data_out;
2014-06-03 17:49:00 +04:00
int channel_status;
if (drdynvc->state == DRDYNVC_STATE_CAPABILITIES)
{
/**
* For some reason the server does not always send the
* capabilities pdu as it should. When this happens,
* send a capabilities response.
*/
drdynvc->version = 3;
drdynvc_send_capability_response(drdynvc);
drdynvc->state = DRDYNVC_STATE_READY;
}
ChannelId = drdynvc_read_variable_uint(s, cbChId);
pos = Stream_GetPosition(s);
DEBUG_DVC("ChannelId=%d ChannelName=%s", ChannelId, Stream_Pointer(s));
2014-06-03 17:49:00 +04:00
channel_status = dvcman_create_channel(drdynvc->channel_mgr, ChannelId, (char*) Stream_Pointer(s));
2013-05-09 01:48:30 +04:00
data_out = Stream_New(NULL, pos + 4);
2013-05-09 00:09:16 +04:00
Stream_Write_UINT8(data_out, 0x10 | cbChId);
Stream_SetPosition(s, 1);
2013-05-09 00:27:21 +04:00
Stream_Copy(data_out, s, pos - 1);
2014-06-03 17:49:00 +04:00
if (channel_status == 0)
{
DEBUG_DVC("channel created");
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(data_out, 0);
}
else
{
DEBUG_DVC("no listener");
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(data_out, (UINT32)(-1));
}
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
if (status != CHANNEL_RC_OK)
{
DEBUG_WARN("VirtualChannelWrite failed %d", status);
return 1;
}
2014-06-03 17:49:00 +04:00
if (channel_status == 0)
{
dvcman_open_channel(drdynvc->channel_mgr, ChannelId);
}
return 0;
}
static int drdynvc_process_data_first(drdynvcPlugin* drdynvc, int Sp, int cbChId, wStream* s)
{
int status;
UINT32 Length;
UINT32 ChannelId;
ChannelId = drdynvc_read_variable_uint(s, cbChId);
Length = drdynvc_read_variable_uint(s, Sp);
DEBUG_DVC("ChannelId=%d Length=%d", ChannelId, Length);
status = dvcman_receive_channel_data_first(drdynvc->channel_mgr, ChannelId, Length);
if (status)
return status;
return dvcman_receive_channel_data(drdynvc->channel_mgr, ChannelId, s);
}
static int drdynvc_process_data(drdynvcPlugin* drdynvc, int Sp, int cbChId, wStream* s)
{
2012-10-09 11:26:39 +04:00
UINT32 ChannelId;
ChannelId = drdynvc_read_variable_uint(s, cbChId);
DEBUG_DVC("ChannelId=%d", ChannelId);
return dvcman_receive_channel_data(drdynvc->channel_mgr, ChannelId, s);
}
static int drdynvc_process_close_request(drdynvcPlugin* drdynvc, int Sp, int cbChId, wStream* s)
{
2012-10-09 11:26:39 +04:00
UINT32 ChannelId;
wStream* data_out;
int value;
int error;
ChannelId = drdynvc_read_variable_uint(s, cbChId);
DEBUG_DVC("ChannelId=%d", ChannelId);
dvcman_close_channel(drdynvc->channel_mgr, ChannelId);
data_out = Stream_New(NULL, 4);
value = (CLOSE_REQUEST_PDU << 4) | (cbChId & 0x03);
Stream_Write_UINT8(data_out, value);
drdynvc_write_variable_uint(data_out, ChannelId);
error = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
if (error != CHANNEL_RC_OK)
{
DEBUG_WARN("VirtualChannelWrite failed %d", error);
return 1;
}
drdynvc->channel_error = error;
return 0;
}
static void drdynvc_process_receive(rdpSvcPlugin* plugin, wStream* s)
{
int value;
int Cmd;
int Sp;
int cbChId;
drdynvcPlugin* drdynvc = (drdynvcPlugin*) plugin;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT8(s, value);
Cmd = (value & 0xf0) >> 4;
Sp = (value & 0x0c) >> 2;
cbChId = (value & 0x03) >> 0;
DEBUG_DVC("Cmd=0x%x", Cmd);
switch (Cmd)
{
case CAPABILITY_REQUEST_PDU:
drdynvc_process_capability_request(drdynvc, Sp, cbChId, s);
break;
case CREATE_REQUEST_PDU:
drdynvc_process_create_request(drdynvc, Sp, cbChId, s);
break;
case DATA_FIRST_PDU:
drdynvc_process_data_first(drdynvc, Sp, cbChId, s);
break;
case DATA_PDU:
drdynvc_process_data(drdynvc, Sp, cbChId, s);
break;
case CLOSE_REQUEST_PDU:
drdynvc_process_close_request(drdynvc, Sp, cbChId, s);
break;
default:
DEBUG_WARN("unknown drdynvc cmd 0x%x", Cmd);
break;
}
}
static void drdynvc_process_connect(rdpSvcPlugin* plugin)
{
2014-02-11 07:12:13 +04:00
UINT32 index;
ADDIN_ARGV* args;
rdpSettings* settings;
drdynvcPlugin* drdynvc = (drdynvcPlugin*)plugin;
DEBUG_DVC("connecting");
drdynvc->channel_mgr = dvcman_new(drdynvc);
2012-04-25 22:27:38 +04:00
drdynvc->channel_error = 0;
settings = (rdpSettings*) ((rdpSvcPlugin*) plugin)->channel_entry_points.pExtendedData;
for (index = 0; index < settings->DynamicChannelCount; index++)
{
args = settings->DynamicChannelArray[index];
dvcman_load_addin(drdynvc->channel_mgr, args);
}
dvcman_init(drdynvc->channel_mgr);
drdynvc->state = DRDYNVC_STATE_CAPABILITIES;
}
static void drdynvc_process_event(rdpSvcPlugin* plugin, wMessage* event)
{
freerdp_event_free(event);
}
static void drdynvc_process_terminate(rdpSvcPlugin* plugin)
{
drdynvcPlugin* drdynvc = (drdynvcPlugin*) plugin;
DEBUG_DVC("terminating");
if (drdynvc->channel_mgr)
dvcman_free(drdynvc->channel_mgr);
svc_plugin_terminate(plugin);
free(drdynvc);
}
/**
* Channel Client Interface
*/
int drdynvc_get_version(DrdynvcClientContext* context)
{
drdynvcPlugin* drdynvc = (drdynvcPlugin*) context->handle;
return drdynvc->version;
}
/* drdynvc is always built-in */
#define VirtualChannelEntry drdynvc_VirtualChannelEntry
2014-03-04 03:28:31 +04:00
BOOL VCAPITYPE VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
{
drdynvcPlugin* _p;
DrdynvcClientContext* context;
CHANNEL_ENTRY_POINTS_FREERDP* pEntryPointsEx;
_p = (drdynvcPlugin*) calloc(1, sizeof(drdynvcPlugin));
2014-06-17 21:34:23 +04:00
if (!_p)
return FALSE;
_p->plugin.channel_def.options =
CHANNEL_OPTION_INITIALIZED |
CHANNEL_OPTION_ENCRYPT_RDP |
CHANNEL_OPTION_COMPRESS_RDP;
strcpy(_p->plugin.channel_def.name, "drdynvc");
_p->state = DRDYNVC_STATE_INITIAL;
_p->plugin.connect_callback = drdynvc_process_connect;
_p->plugin.receive_callback = drdynvc_process_receive;
_p->plugin.event_callback = drdynvc_process_event;
_p->plugin.terminate_callback = drdynvc_process_terminate;
pEntryPointsEx = (CHANNEL_ENTRY_POINTS_FREERDP*) pEntryPoints;
if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP)) &&
(pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
{
2014-06-17 21:34:23 +04:00
context = (DrdynvcClientContext*) calloc(1, sizeof(DrdynvcClientContext));
if (!context)
return -1;
context->handle = (void*) _p;
_p->context = context;
context->GetVersion = drdynvc_get_version;
*(pEntryPointsEx->ppInterface) = (void*) context;
}
svc_plugin_init((rdpSvcPlugin*) _p, pEntryPoints);
return 1;
}