summaryrefslogtreecommitdiff
path: root/noncore/net/opierdesktop/mcs.cpp
Unidiff
Diffstat (limited to 'noncore/net/opierdesktop/mcs.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/net/opierdesktop/mcs.cpp394
1 files changed, 394 insertions, 0 deletions
diff --git a/noncore/net/opierdesktop/mcs.cpp b/noncore/net/opierdesktop/mcs.cpp
new file mode 100644
index 0000000..8877d62
--- a/dev/null
+++ b/noncore/net/opierdesktop/mcs.cpp
@@ -0,0 +1,394 @@
1/*
2 rdesktop: A Remote Desktop Protocol client.
3 Protocol services - Multipoint Communications Service
4 Copyright (C) Matthew Chapman 1999-2002
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include "rdesktop.h"
22
23uint16 mcs_userid;
24
25/* Parse an ASN.1 BER header */
26static BOOL
27ber_parse_header(STREAM s, int tagval, unsigned int *length)
28{
29 int tag, len;
30
31 if (tagval > 0xff)
32 {
33 in_uint16_be(s, tag);
34 }
35 else
36 {
37 in_uint8(s, tag)}
38
39 if (tag != tagval)
40 {
41 error("expected tag %d, got %d\n", tagval, tag);
42 return False;
43 }
44
45 in_uint8(s, len);
46
47 if (len & 0x80)
48 {
49 len &= ~0x80;
50 *length = 0;
51 while (len--)
52 next_be(s, *length);
53 }
54 else
55 *length = len;
56
57 return s_check(s);
58}
59
60/* Output an ASN.1 BER header */
61static void
62ber_out_header(STREAM s, int tagval, int length)
63{
64 if (tagval > 0xff)
65 {
66 out_uint16_be(s, tagval);
67 }
68 else
69 {
70 out_uint8(s, tagval);
71 }
72
73 if (length >= 0x80)
74 {
75 out_uint8(s, 0x82);
76 out_uint16_be(s, length);
77 }
78 else
79 out_uint8(s, length);
80}
81
82/* Output an ASN.1 BER integer */
83static void
84ber_out_integer(STREAM s, int value)
85{
86 ber_out_header(s, BER_TAG_INTEGER, 2);
87 out_uint16_be(s, value);
88}
89
90/* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
91static void
92mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
93{
94 ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
95 ber_out_integer(s, max_channels);
96 ber_out_integer(s, max_users);
97 ber_out_integer(s, max_tokens);
98 ber_out_integer(s, 1);/* num_priorities */
99 ber_out_integer(s, 0);/* min_throughput */
100 ber_out_integer(s, 1);/* max_height */
101 ber_out_integer(s, max_pdusize);
102 ber_out_integer(s, 2);/* ver_protocol */
103}
104
105/* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
106static BOOL
107mcs_parse_domain_params(STREAM s)
108{
109 unsigned int length;
110
111 ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
112 in_uint8s(s, length);
113
114 return s_check(s);
115}
116
117/* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */
118static void
119mcs_send_connect_initial(STREAM mcs_data)
120{
121 int datalen = mcs_data->end - mcs_data->data;
122 int length = 7 + 3 * 34 + 4 + datalen;
123 STREAM s;
124
125 s = iso_init(length + 5);
126
127 ber_out_header(s, MCS_CONNECT_INITIAL, length);
128 ber_out_header(s, BER_TAG_OCTET_STRING, 0);/* calling domain */
129 ber_out_header(s, BER_TAG_OCTET_STRING, 0);/* called domain */
130
131 ber_out_header(s, BER_TAG_BOOLEAN, 1);
132 out_uint8(s, 0xff);/* upward flag */
133
134 mcs_out_domain_params(s, 2, 2, 0, 0xffff);/* target params */
135 mcs_out_domain_params(s, 1, 1, 1, 0x420);/* min params */
136 mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff);/* max params */
137
138 ber_out_header(s, BER_TAG_OCTET_STRING, datalen);
139 out_uint8p(s, mcs_data->data, datalen);
140
141 s_mark_end(s);
142 iso_send(s);
143}
144
145/* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
146static BOOL
147mcs_recv_connect_response(STREAM mcs_data)
148{
149 uint8 result;
150 unsigned int length;
151 STREAM s;
152
153 s = iso_recv();
154 if (s == NULL)
155 return False;
156
157 ber_parse_header(s, MCS_CONNECT_RESPONSE, &length);
158
159 ber_parse_header(s, BER_TAG_RESULT, &length);
160 in_uint8(s, result);
161 if (result != 0)
162 {
163 error("MCS connect: %d\n", result);
164 return False;
165 }
166
167 ber_parse_header(s, BER_TAG_INTEGER, &length);
168 in_uint8s(s, length);/* connect id */
169 mcs_parse_domain_params(s);
170
171 ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
172 if (length > mcs_data->size)
173 {
174 error("MCS data length %d\n", length);
175 length = mcs_data->size;
176 }
177
178 in_uint8a(s, mcs_data->data, length);
179 mcs_data->p = mcs_data->data;
180 mcs_data->end = mcs_data->data + length;
181
182 return s_check_end(s);
183}
184
185/* Send an EDrq message (ASN.1 PER) */
186static void
187mcs_send_edrq(void)
188{
189 STREAM s;
190
191 s = iso_init(5);
192
193 out_uint8(s, (MCS_EDRQ << 2));
194 out_uint16_be(s, 1);/* height */
195 out_uint16_be(s, 1);/* interval */
196
197 s_mark_end(s);
198 iso_send(s);
199}
200
201/* Send an AUrq message (ASN.1 PER) */
202static void
203mcs_send_aurq(void)
204{
205 STREAM s;
206
207 s = iso_init(1);
208
209 out_uint8(s, (MCS_AURQ << 2));
210
211 s_mark_end(s);
212 iso_send(s);
213}
214
215/* Expect a AUcf message (ASN.1 PER) */
216static BOOL
217mcs_recv_aucf(uint16 * mcs_userid)
218{
219 uint8 opcode, result;
220 STREAM s;
221
222 s = iso_recv();
223 if (s == NULL)
224 return False;
225
226 in_uint8(s, opcode);
227 if ((opcode >> 2) != MCS_AUCF)
228 {
229 error("expected AUcf, got %d\n", opcode);
230 return False;
231 }
232
233 in_uint8(s, result);
234 if (result != 0)
235 {
236 error("AUrq: %d\n", result);
237 return False;
238 }
239
240 if (opcode & 2)
241 in_uint16_be(s, *mcs_userid);
242
243 return s_check_end(s);
244}
245
246/* Send a CJrq message (ASN.1 PER) */
247static void
248mcs_send_cjrq(uint16 chanid)
249{
250 STREAM s;
251
252 s = iso_init(5);
253
254 out_uint8(s, (MCS_CJRQ << 2));
255 out_uint16_be(s, mcs_userid);
256 out_uint16_be(s, chanid);
257
258 s_mark_end(s);
259 iso_send(s);
260}
261
262/* Expect a CJcf message (ASN.1 PER) */
263static BOOL
264mcs_recv_cjcf(void)
265{
266 uint8 opcode, result;
267 STREAM s;
268
269 s = iso_recv();
270 if (s == NULL)
271 return False;
272
273 in_uint8(s, opcode);
274 if ((opcode >> 2) != MCS_CJCF)
275 {
276 error("expected CJcf, got %d\n", opcode);
277 return False;
278 }
279
280 in_uint8(s, result);
281 if (result != 0)
282 {
283 error("CJrq: %d\n", result);
284 return False;
285 }
286
287 in_uint8s(s, 4);/* mcs_userid, req_chanid */
288 if (opcode & 2)
289 in_uint8s(s, 2);/* join_chanid */
290
291 return s_check_end(s);
292}
293
294/* Initialise an MCS transport data packet */
295STREAM
296mcs_init(int length)
297{
298 STREAM s;
299
300 s = iso_init(length + 8);
301 s_push_layer(s, mcs_hdr, 8);
302
303 return s;
304}
305
306/* Send an MCS transport data packet */
307void
308mcs_send(STREAM s)
309{
310 uint16 length;
311
312 s_pop_layer(s, mcs_hdr);
313 length = s->end - s->p - 8;
314 length |= 0x8000;
315
316 out_uint8(s, (MCS_SDRQ << 2));
317 out_uint16_be(s, mcs_userid);
318 out_uint16_be(s, MCS_GLOBAL_CHANNEL);
319 out_uint8(s, 0x70);/* flags */
320 out_uint16_be(s, length);
321
322 iso_send(s);
323}
324
325/* Receive an MCS transport data packet */
326STREAM
327mcs_recv(void)
328{
329 uint8 opcode, appid, length;
330 STREAM s;
331
332 s = iso_recv();
333 if (s == NULL)
334 return NULL;
335
336 in_uint8(s, opcode);
337 appid = opcode >> 2;
338 if (appid != MCS_SDIN)
339 {
340 if (appid != MCS_DPUM)
341 {
342 error("expected data, got %d\n", opcode);
343 }
344 return NULL;
345 }
346
347 in_uint8s(s, 5);/* userid, chanid, flags */
348 in_uint8(s, length);
349 if (length & 0x80)
350 in_uint8s(s, 1);/* second byte of length */
351
352 return s;
353}
354
355/* Establish a connection up to the MCS layer */
356BOOL
357mcs_connect(char *server, STREAM mcs_data)
358{
359 if (!iso_connect(server))
360 {
361 return False;
362 }
363
364 mcs_send_connect_initial(mcs_data);
365 if (!mcs_recv_connect_response(mcs_data))
366 goto error;
367
368 mcs_send_edrq();
369
370 mcs_send_aurq();
371 if (!mcs_recv_aucf(&mcs_userid))
372 goto error;
373
374 mcs_send_cjrq(mcs_userid + 1001);
375 if (!mcs_recv_cjcf())
376 goto error;
377
378 mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
379 if (!mcs_recv_cjcf())
380 goto error;
381
382 return True;
383
384 error:
385 iso_disconnect();
386 return False;
387}
388
389/* Disconnect from the MCS layer */
390void
391mcs_disconnect(void)
392{
393 iso_disconnect();
394}