channels.c revision d65680efa46fa49e8bf14e67b29b782510ff934c
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/* -*- c-basic-offset: 8 -*-
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync rdesktop: A Remote Desktop Protocol client.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Protocol services - Virtual channels
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Copyright (C) Erik Forsberg <forsberg@cendio.se> 2003
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Copyright (C) Matthew Chapman 2003-2007
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync This program is free software; you can redistribute it and/or modify
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync it under the terms of the GNU General Public License as published by
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync the Free Software Foundation; either version 2 of the License, or
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync (at your option) any later version.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync This program is distributed in the hope that it will be useful,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync but WITHOUT ANY WARRANTY; without even the implied warranty of
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync GNU General Public License for more details.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync You should have received a copy of the GNU General Public License
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync along with this program; if not, write to the Free Software
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync*/
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/*
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Sun GPL Disclaimer: For the avoidance of doubt, except that if any license choice
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * other than GPL or LGPL is available it will apply instead, Sun elects to use only
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * the General Public License version 2 (GPLv2) at this time for any software where
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * a choice of GPL license versions is made available with the language indicating
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * that GPLv2 or any later version may be used, or where a choice of which version
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * of the GPL is applied is otherwise unspecified.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#include "rdesktop.h"
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#define MAX_CHANNELS 6
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#define CHANNEL_CHUNK_LENGTH 1600
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#define CHANNEL_FLAG_FIRST 0x01
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#define CHANNEL_FLAG_LAST 0x02
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#define CHANNEL_FLAG_SHOW_PROTOCOL 0x10
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncextern RD_BOOL g_use_rdp5;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncextern RD_BOOL g_encryption;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncVCHANNEL g_channels[MAX_CHANNELS];
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncunsigned int g_num_channels;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/* FIXME: We should use the information in TAG_SRV_CHANNELS to map RDP5
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync channels to MCS channels.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync The format of TAG_SRV_CHANNELS seems to be
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync global_channel_no (uint16le)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync number_of_other_channels (uint16le)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync ..followed by uint16les for the other channels.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync*/
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncVCHANNEL *
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncchannel_register(char *name, uint32 flags, void (*callback) (STREAM))
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync{
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync VCHANNEL *channel;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (!g_use_rdp5)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return NULL;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (g_num_channels >= MAX_CHANNELS)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync error("Channel table full, increase MAX_CHANNELS\n");
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return NULL;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync channel = &g_channels[g_num_channels];
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync channel->mcs_id = MCS_GLOBAL_CHANNEL + 1 + g_num_channels;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync strncpy(channel->name, name, 8);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync channel->flags = flags;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync channel->process = callback;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync g_num_channels++;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return channel;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync}
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncSTREAM
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncchannel_init(VCHANNEL * channel, uint32 length)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync{
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync STREAM s;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync s = sec_init(g_encryption ? SEC_ENCRYPT : 0, length + 8);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync s_push_layer(s, channel_hdr, 8);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync return s;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync}
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncvoid
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncchannel_send(STREAM s, VCHANNEL * channel)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync{
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync uint32 length, flags;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync uint32 thislength, remaining;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync uint8 *data;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#ifdef WITH_SCARD
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync scard_lock(SCARD_LOCK_CHANNEL);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#endif
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* first fragment sent in-place */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync s_pop_layer(s, channel_hdr);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync length = s->end - s->p - 8;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync DEBUG_CHANNEL(("channel_send, length = %d\n", length));
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync thislength = MIN(length, CHANNEL_CHUNK_LENGTH);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/* Note: In the original clipboard implementation, this number was
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync 1592, not 1600. However, I don't remember the reason and 1600 seems
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync to work so.. This applies only to *this* length, not the length of
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync continuation or ending packets. */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync remaining = length - thislength;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync flags = (remaining == 0) ? CHANNEL_FLAG_FIRST | CHANNEL_FLAG_LAST : CHANNEL_FLAG_FIRST;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (channel->flags & CHANNEL_OPTION_SHOW_PROTOCOL)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync flags |= CHANNEL_FLAG_SHOW_PROTOCOL;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync out_uint32_le(s, length);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync out_uint32_le(s, flags);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync data = s->end = s->p + thislength;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync DEBUG_CHANNEL(("Sending %d bytes with FLAG_FIRST\n", thislength));
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync sec_send_to_channel(s, g_encryption ? SEC_ENCRYPT : 0, channel->mcs_id);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* subsequent segments copied (otherwise would have to generate headers backwards) */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync while (remaining > 0)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync thislength = MIN(remaining, CHANNEL_CHUNK_LENGTH);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync remaining -= thislength;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync flags = (remaining == 0) ? CHANNEL_FLAG_LAST : 0;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (channel->flags & CHANNEL_OPTION_SHOW_PROTOCOL)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync flags |= CHANNEL_FLAG_SHOW_PROTOCOL;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync DEBUG_CHANNEL(("Sending %d bytes with flags %d\n", thislength, flags));
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync s = sec_init(g_encryption ? SEC_ENCRYPT : 0, thislength + 8);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync out_uint32_le(s, length);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync out_uint32_le(s, flags);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync out_uint8p(s, data, thislength);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync s_mark_end(s);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync sec_send_to_channel(s, g_encryption ? SEC_ENCRYPT : 0, channel->mcs_id);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync data += thislength;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync }
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync#ifdef WITH_SCARD
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync scard_unlock(SCARD_LOCK_CHANNEL);
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync#endif
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync}
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsyncvoid
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsyncchannel_process(STREAM s, uint16 mcs_channel)
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync{
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync uint32 length, flags;
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync uint32 thislength;
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync VCHANNEL *channel = NULL;
8a5e0b7c43d7ee861689984edc2abd86b9ed4456vboxsync unsigned int i;
a4ae8b6f30e62ce1edef9e5edf7c215b1f69915cvboxsync STREAM in;
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync for (i = 0; i < g_num_channels; i++)
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync {
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync channel = &g_channels[i];
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync if (channel->mcs_id == mcs_channel)
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync break;
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync }
d68511fc6827e32ca38d7bbaed520c34b6c9f050vboxsync
7e032664d31552364e83b411950d6e7c96b0b880vboxsync if (i >= g_num_channels)
7e032664d31552364e83b411950d6e7c96b0b880vboxsync return;
befdf0665bb07332302fed3ded8f2bc6a9695c52vboxsync
ca551aca153d6df494985b5281c573ba2e3eb474vboxsync in_uint32_le(s, length);
ca551aca153d6df494985b5281c573ba2e3eb474vboxsync in_uint32_le(s, flags);
10183353efa12aa94e0f3dc78e0f1c47f7928e8dvboxsync if ((flags & CHANNEL_FLAG_FIRST) && (flags & CHANNEL_FLAG_LAST))
10183353efa12aa94e0f3dc78e0f1c47f7928e8dvboxsync {
13f76705071cb6db2cd84c495ccb53e0daebd308vboxsync /* single fragment - pass straight up */
716eca86fb214d6bf59f8837d35f5e812729940avboxsync channel->process(s);
af8d59d05d72f134aeea62712f1286b369807d52vboxsync }
af8d59d05d72f134aeea62712f1286b369807d52vboxsync else
af8d59d05d72f134aeea62712f1286b369807d52vboxsync {
af8d59d05d72f134aeea62712f1286b369807d52vboxsync /* add fragment to defragmentation buffer */
af8d59d05d72f134aeea62712f1286b369807d52vboxsync in = &channel->in;
af8d59d05d72f134aeea62712f1286b369807d52vboxsync if (flags & CHANNEL_FLAG_FIRST)
4b7babecff1c8fd1421ffc2d0b3df33fda523b42vboxsync {
4b7babecff1c8fd1421ffc2d0b3df33fda523b42vboxsync if (length > in->size)
4b7babecff1c8fd1421ffc2d0b3df33fda523b42vboxsync {
4b7babecff1c8fd1421ffc2d0b3df33fda523b42vboxsync in->data = (uint8 *) xrealloc(in->data, length);
4b7babecff1c8fd1421ffc2d0b3df33fda523b42vboxsync in->size = length;
4b7babecff1c8fd1421ffc2d0b3df33fda523b42vboxsync }
7b6926b2bf44f326f40e1d9d1ce33a4dff0a2c67vboxsync in->p = in->data;
}
thislength = MIN(s->end - s->p, in->data + in->size - in->p);
memcpy(in->p, s->p, thislength);
in->p += thislength;
if (flags & CHANNEL_FLAG_LAST)
{
in->end = in->p;
in->p = in->data;
channel->process(in);
}
}
}