1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Protocol services - Virtual channels |
4 |
Copyright (C) Erik Forsberg <forsberg@cendio.se> 2003 |
5 |
Copyright (C) Matthew Chapman 2003 |
6 |
|
7 |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
21 |
|
22 |
#include "rdesktop.h" |
23 |
|
24 |
#define MAX_CHANNELS 4 |
25 |
#define CHANNEL_CHUNK_LENGTH 1600 |
26 |
#define CHANNEL_FLAG_FIRST 0x01 |
27 |
#define CHANNEL_FLAG_LAST 0x02 |
28 |
#define CHANNEL_FLAG_SHOW_PROTOCOL 0x10 |
29 |
|
30 |
extern BOOL g_use_rdp5; |
31 |
extern BOOL g_encryption; |
32 |
|
33 |
VCHANNEL g_channels[MAX_CHANNELS]; |
34 |
unsigned int g_num_channels; |
35 |
|
36 |
/* FIXME: We should use the information in TAG_SRV_CHANNELS to map RDP5 |
37 |
channels to MCS channels. |
38 |
|
39 |
The format of TAG_SRV_CHANNELS seems to be |
40 |
|
41 |
global_channel_no (uint16le) |
42 |
number_of_other_channels (uint16le) |
43 |
..followed by uint16les for the other channels. |
44 |
*/ |
45 |
|
46 |
VCHANNEL * |
47 |
channel_register(char *name, uint32 flags, void (*callback) (STREAM)) |
48 |
{ |
49 |
VCHANNEL *channel; |
50 |
|
51 |
if (!g_use_rdp5) |
52 |
return NULL; |
53 |
|
54 |
if (g_num_channels >= MAX_CHANNELS) |
55 |
{ |
56 |
error("Channel table full, increase MAX_CHANNELS\n"); |
57 |
return NULL; |
58 |
} |
59 |
|
60 |
channel = &g_channels[g_num_channels]; |
61 |
channel->mcs_id = MCS_GLOBAL_CHANNEL + 1 + g_num_channels; |
62 |
strncpy(channel->name, name, 8); |
63 |
channel->flags = flags; |
64 |
channel->process = callback; |
65 |
g_num_channels++; |
66 |
return channel; |
67 |
} |
68 |
|
69 |
STREAM |
70 |
channel_init(VCHANNEL * channel, uint32 length) |
71 |
{ |
72 |
STREAM s; |
73 |
|
74 |
s = sec_init(g_encryption ? SEC_ENCRYPT : 0, length + 8); |
75 |
s_push_layer(s, channel_hdr, 8); |
76 |
return s; |
77 |
} |
78 |
|
79 |
void |
80 |
channel_send(STREAM s, VCHANNEL * channel) |
81 |
{ |
82 |
uint32 length, flags; |
83 |
uint32 thislength, remaining; |
84 |
char *data; |
85 |
|
86 |
/* first fragment sent in-place */ |
87 |
s_pop_layer(s, channel_hdr); |
88 |
length = s->end - s->p - 8; |
89 |
|
90 |
thislength = MIN(length, CHANNEL_CHUNK_LENGTH); |
91 |
remaining = length - thislength; |
92 |
flags = (remaining == 0) ? CHANNEL_FLAG_FIRST | CHANNEL_FLAG_LAST : CHANNEL_FLAG_FIRST; |
93 |
if (channel->flags & CHANNEL_OPTION_SHOW_PROTOCOL) |
94 |
flags |= CHANNEL_FLAG_SHOW_PROTOCOL; |
95 |
|
96 |
out_uint32_le(s, length); |
97 |
out_uint32_le(s, flags); |
98 |
data = s->end = s->p + thislength; |
99 |
sec_send_to_channel(s, g_encryption ? SEC_ENCRYPT : 0, channel->mcs_id); |
100 |
|
101 |
/* subsequent segments copied (otherwise would have to generate headers backwards) */ |
102 |
while (remaining > 0) |
103 |
{ |
104 |
thislength = MIN(remaining, CHANNEL_CHUNK_LENGTH); |
105 |
remaining -= thislength; |
106 |
flags = (remaining == 0) ? CHANNEL_FLAG_LAST : 0; |
107 |
|
108 |
s = sec_init(g_encryption ? SEC_ENCRYPT : 0, thislength + 8); |
109 |
out_uint32_le(s, length); |
110 |
out_uint32_le(s, flags); |
111 |
out_uint8p(s, data, thislength); |
112 |
s_mark_end(s); |
113 |
sec_send_to_channel(s, g_encryption ? SEC_ENCRYPT : 0, channel->mcs_id); |
114 |
|
115 |
data += thislength; |
116 |
} |
117 |
} |
118 |
|
119 |
void |
120 |
channel_process(STREAM s, uint16 mcs_channel) |
121 |
{ |
122 |
uint32 length, flags; |
123 |
uint32 thislength; |
124 |
VCHANNEL *channel; |
125 |
unsigned int i; |
126 |
STREAM in; |
127 |
|
128 |
for (i = 0; i < g_num_channels; i++) |
129 |
{ |
130 |
channel = &g_channels[i]; |
131 |
if (channel->mcs_id == mcs_channel) |
132 |
break; |
133 |
} |
134 |
|
135 |
if (i >= g_num_channels) |
136 |
return; |
137 |
|
138 |
in_uint32_le(s, length); |
139 |
in_uint32_le(s, flags); |
140 |
if ((flags & CHANNEL_FLAG_FIRST) && (flags & CHANNEL_FLAG_LAST)) |
141 |
{ |
142 |
/* single fragment - pass straight up */ |
143 |
channel->process(s); |
144 |
} |
145 |
else |
146 |
{ |
147 |
/* add fragment to defragmentation buffer */ |
148 |
in = &channel->in; |
149 |
if (flags & CHANNEL_FLAG_FIRST) |
150 |
{ |
151 |
if (length > in->size) |
152 |
{ |
153 |
in->data = xrealloc(in->data, length); |
154 |
in->size = length; |
155 |
} |
156 |
in->p = in->data; |
157 |
} |
158 |
|
159 |
thislength = s->end - s->p; |
160 |
memcpy(in->p, s->p, thislength); |
161 |
s->p += thislength; |
162 |
s->end += thislength; |
163 |
|
164 |
if (flags & CHANNEL_FLAG_LAST) |
165 |
{ |
166 |
in->p = in->data; |
167 |
channel->process(in); |
168 |
} |
169 |
} |
170 |
} |