]> git.sesse.net Git - rdpsrv/blob - mcs.c
Handle MCS_CONNECT_INITIAL.
[rdpsrv] / mcs.c
1 /* -*- c-basic-offset: 8 -*-
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
23 uint16 g_mcs_userid;
24 extern VCHANNEL g_channels[];
25 extern unsigned int g_num_channels;
26
27 /* Parse an ASN.1 BER header */
28 static BOOL
29 ber_parse_header(STREAM s, int tagval, int *length)
30 {
31         int tag, len;
32
33         if (tagval > 0xff)
34         {
35                 in_uint16_be(s, tag);
36         }
37         else
38         {
39         in_uint8(s, tag)}
40
41         if (tag != tagval)
42         {
43                 error("expected tag %d, got %d\n", tagval, tag);
44                 return False;
45         }
46
47         in_uint8(s, len);
48
49         if (len & 0x80)
50         {
51                 len &= ~0x80;
52                 *length = 0;
53                 while (len--)
54                         next_be(s, *length);
55         }
56         else
57                 *length = len;
58
59         return s_check(s);
60 }
61
62 /* Output an ASN.1 BER header */
63 static void
64 ber_out_header(STREAM s, int tagval, int length)
65 {
66         if (tagval > 0xff)
67         {
68                 out_uint16_be(s, tagval);
69         }
70         else
71         {
72                 out_uint8(s, tagval);
73         }
74
75         if (length >= 0x80)
76         {
77                 out_uint8(s, 0x82);
78                 out_uint16_be(s, length);
79         }
80         else
81                 out_uint8(s, length);
82 }
83
84 /* Output an ASN.1 BER integer */
85 static void
86 ber_out_integer(STREAM s, int value)
87 {
88         ber_out_header(s, BER_TAG_INTEGER, 2);
89         out_uint16_be(s, value);
90 }
91
92 /* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
93 static void
94 mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
95 {
96         ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
97         ber_out_integer(s, max_channels);
98         ber_out_integer(s, max_users);
99         ber_out_integer(s, max_tokens);
100         ber_out_integer(s, 1);  /* num_priorities */
101         ber_out_integer(s, 0);  /* min_throughput */
102         ber_out_integer(s, 1);  /* max_height */
103         ber_out_integer(s, max_pdusize);
104         ber_out_integer(s, 2);  /* ver_protocol */
105 }
106
107 /* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
108 static BOOL
109 mcs_parse_domain_params(STREAM s)
110 {
111         int length;
112
113         ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
114         in_uint8s(s, length);
115
116         return s_check(s);
117 }
118
119 /* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */
120 static void
121 mcs_send_connect_initial(STREAM mcs_data)
122 {
123         int datalen = mcs_data->end - mcs_data->data;
124         int length = 9 + 3 * 34 + 4 + datalen;
125         STREAM s;
126
127         s = iso_init(length + 5);
128
129         ber_out_header(s, MCS_CONNECT_INITIAL, length);
130         ber_out_header(s, BER_TAG_OCTET_STRING, 1);     /* calling domain */
131         out_uint8(s, 1);
132         ber_out_header(s, BER_TAG_OCTET_STRING, 1);     /* called domain */
133         out_uint8(s, 1);
134
135         ber_out_header(s, BER_TAG_BOOLEAN, 1);
136         out_uint8(s, 0xff);     /* upward flag */
137
138         mcs_out_domain_params(s, 34, 2, 0, 0xffff);     /* target params */
139         mcs_out_domain_params(s, 1, 1, 1, 0x420);       /* min params */
140         mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff);       /* max params */
141
142         ber_out_header(s, BER_TAG_OCTET_STRING, datalen);
143         out_uint8p(s, mcs_data->data, datalen);
144
145         s_mark_end(s);
146         iso_send(s);
147 }
148
149 /* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
150 BOOL
151 mcs_recv_connect_initial()
152 {
153         uint8 result;
154         int length;
155         STREAM s;
156         char *buf;
157
158         s = iso_recv();
159         if (s == NULL)
160                 return False;
161
162         ber_parse_header(s, MCS_CONNECT_INITIAL, &length);
163         printf("parsing MCS_CONNECT_INITIAL (len=%u)\n", length);
164         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);   /* calling domain */
165         in_uint8(s, result);
166         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);   /* called domain */
167         in_uint8(s, result);
168         
169         ber_parse_header(s, BER_TAG_BOOLEAN, &length);
170         in_uint8(s, result);
171
172         mcs_parse_domain_params(s);
173         mcs_parse_domain_params(s);
174         mcs_parse_domain_params(s);
175
176         ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
177         in_uint8p(s, buf, length);
178
179         printf("Data from MCS connect: '%*s'\n", length, buf);
180         
181         return s_check_end(s);
182 }
183
184 /* Send an EDrq message (ASN.1 PER) */
185 static void
186 mcs_send_edrq(void)
187 {
188         STREAM s;
189
190         s = iso_init(5);
191
192         out_uint8(s, (MCS_EDRQ << 2));
193         out_uint16_be(s, 1);    /* height */
194         out_uint16_be(s, 1);    /* interval */
195
196         s_mark_end(s);
197         iso_send(s);
198 }
199
200 /* Send an AUrq message (ASN.1 PER) */
201 static void
202 mcs_send_aurq(void)
203 {
204         STREAM s;
205
206         s = iso_init(1);
207
208         out_uint8(s, (MCS_AURQ << 2));
209
210         s_mark_end(s);
211         iso_send(s);
212 }
213
214 /* Expect a AUcf message (ASN.1 PER) */
215 static BOOL
216 mcs_recv_aucf(uint16 * mcs_userid)
217 {
218         uint8 opcode, result;
219         STREAM s;
220
221         s = iso_recv();
222         if (s == NULL)
223                 return False;
224
225         in_uint8(s, opcode);
226         if ((opcode >> 2) != MCS_AUCF)
227         {
228                 error("expected AUcf, got %d\n", opcode);
229                 return False;
230         }
231
232         in_uint8(s, result);
233         if (result != 0)
234         {
235                 error("AUrq: %d\n", result);
236                 return False;
237         }
238
239         if (opcode & 2)
240                 in_uint16_be(s, *mcs_userid);
241
242         return s_check_end(s);
243 }
244
245 /* Send a CJrq message (ASN.1 PER) */
246 static void
247 mcs_send_cjrq(uint16 chanid)
248 {
249         STREAM s;
250
251         DEBUG_RDP5(("Sending CJRQ for channel #%d\n", chanid));
252
253         s = iso_init(5);
254
255         out_uint8(s, (MCS_CJRQ << 2));
256         out_uint16_be(s, g_mcs_userid);
257         out_uint16_be(s, chanid);
258
259         s_mark_end(s);
260         iso_send(s);
261 }
262
263 /* Expect a CJcf message (ASN.1 PER) */
264 static BOOL
265 mcs_recv_cjcf(void)
266 {
267         uint8 opcode, result;
268         STREAM s;
269
270         s = iso_recv();
271         if (s == NULL)
272                 return False;
273
274         in_uint8(s, opcode);
275         if ((opcode >> 2) != MCS_CJCF)
276         {
277                 error("expected CJcf, got %d\n", opcode);
278                 return False;
279         }
280
281         in_uint8(s, result);
282         if (result != 0)
283         {
284                 error("CJrq: %d\n", result);
285                 return False;
286         }
287
288         in_uint8s(s, 4);        /* mcs_userid, req_chanid */
289         if (opcode & 2)
290                 in_uint8s(s, 2);        /* join_chanid */
291
292         return s_check_end(s);
293 }
294
295 /* Initialise an MCS transport data packet */
296 STREAM
297 mcs_init(int length)
298 {
299         STREAM s;
300
301         s = iso_init(length + 8);
302         s_push_layer(s, mcs_hdr, 8);
303
304         return s;
305 }
306
307 /* Send an MCS transport data packet to a specific channel */
308 void
309 mcs_send_to_channel(STREAM s, uint16 channel)
310 {
311         uint16 length;
312
313         s_pop_layer(s, mcs_hdr);
314         length = s->end - s->p - 8;
315         length |= 0x8000;
316
317         out_uint8(s, (MCS_SDRQ << 2));
318         out_uint16_be(s, g_mcs_userid);
319         out_uint16_be(s, channel);
320         out_uint8(s, 0x70);     /* flags */
321         out_uint16_be(s, length);
322
323         iso_send(s);
324 }
325
326 /* Send an MCS transport data packet to the global channel */
327 void
328 mcs_send(STREAM s)
329 {
330         mcs_send_to_channel(s, MCS_GLOBAL_CHANNEL);
331 }
332
333 /* Receive an MCS transport data packet */
334 STREAM
335 mcs_recv(uint16 * channel)
336 {
337         uint8 opcode, appid, length;
338         STREAM s;
339
340         s = iso_recv();
341         if (s == NULL)
342                 return NULL;
343
344         in_uint8(s, opcode);
345         appid = opcode >> 2;
346         if (appid != MCS_SDIN)
347         {
348                 if (appid != MCS_DPUM)
349                 {
350                         error("expected data, got %d\n", opcode);
351                 }
352                 return NULL;
353         }
354
355         in_uint8s(s, 2);        /* userid */
356         in_uint16_be(s, *channel);
357         in_uint8s(s, 1);        /* flags */
358         in_uint8(s, length);
359         if (length & 0x80)
360                 in_uint8s(s, 1);        /* second byte of length */
361
362         return s;
363 }
364
365 /* Disconnect from the MCS layer */
366 void
367 mcs_disconnect(void)
368 {
369         iso_disconnect();
370 }