/[rdesktop]/jpeg/rdesktop/trunk/mcs.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /jpeg/rdesktop/trunk/mcs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1507 - (hide annotations)
Mon Jul 20 16:45:11 2009 UTC (14 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 9002 byte(s)
branch for integration of Daniel Jarboe <daniel.jarboe(at)gmail.com>
patches for jpeg
1 forsberg 348 /* -*- c-basic-offset: 8 -*-
2 matty 3 rdesktop: A Remote Desktop Protocol client.
3     Protocol services - Multipoint Communications Service
4 jsorg71 1475 Copyright (C) Matthew Chapman 1999-2008
5 jsorg71 1365
6 matty 3 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 jsorg71 1365
11 matty 3 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 jsorg71 1365
16 matty 3 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 matty 10 #include "rdesktop.h"
22 matty 3
23 jsorg71 381 uint16 g_mcs_userid;
24 matthewc 432 extern VCHANNEL g_channels[];
25     extern unsigned int g_num_channels;
26 matty 10
27     /* Parse an ASN.1 BER header */
28 jsorg71 1372 static RD_BOOL
29 matty 25 ber_parse_header(STREAM s, int tagval, int *length)
30 matty 3 {
31 matty 10 int tag, len;
32 matty 3
33 matty 10 if (tagval > 0xff)
34 matty 3 {
35 matty 10 in_uint16_be(s, tag);
36 matty 3 }
37 matty 10 else
38 matty 3 {
39 jsorg71 1379 in_uint8(s, tag);
40     }
41 matty 3
42 matty 10 if (tag != tagval)
43 matty 3 {
44 matty 30 error("expected tag %d, got %d\n", tagval, tag);
45 matty 10 return False;
46 matty 3 }
47    
48 matty 10 in_uint8(s, len);
49 matty 3
50 matty 10 if (len & 0x80)
51 matty 3 {
52 matty 10 len &= ~0x80;
53     *length = 0;
54     while (len--)
55     next_be(s, *length);
56 matty 3 }
57 matty 24 else
58     *length = len;
59 matty 3
60 matty 10 return s_check(s);
61 matty 3 }
62    
63 matty 10 /* Output an ASN.1 BER header */
64 matty 25 static void
65     ber_out_header(STREAM s, int tagval, int length)
66 matty 3 {
67 matty 10 if (tagval > 0xff)
68 matty 3 {
69 matty 10 out_uint16_be(s, tagval);
70 matty 3 }
71 matty 10 else
72     {
73     out_uint8(s, tagval);
74     }
75 matty 3
76 matty 10 if (length >= 0x80)
77 matty 3 {
78 matty 10 out_uint8(s, 0x82);
79     out_uint16_be(s, length);
80 matty 3 }
81 matty 24 else
82     out_uint8(s, length);
83 matty 3 }
84    
85 matty 10 /* Output an ASN.1 BER integer */
86 matty 25 static void
87     ber_out_integer(STREAM s, int value)
88 matty 3 {
89 matty 10 ber_out_header(s, BER_TAG_INTEGER, 2);
90     out_uint16_be(s, value);
91 matty 3 }
92    
93 matty 10 /* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
94 matty 25 static void
95 astrand 82 mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
96 matty 3 {
97 matty 10 ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
98     ber_out_integer(s, max_channels);
99     ber_out_integer(s, max_users);
100     ber_out_integer(s, max_tokens);
101 matty 24 ber_out_integer(s, 1); /* num_priorities */
102     ber_out_integer(s, 0); /* min_throughput */
103     ber_out_integer(s, 1); /* max_height */
104 matty 10 ber_out_integer(s, max_pdusize);
105 matty 24 ber_out_integer(s, 2); /* ver_protocol */
106 matty 3 }
107    
108 matty 10 /* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
109 jsorg71 1372 static RD_BOOL
110 matty 25 mcs_parse_domain_params(STREAM s)
111 matty 3 {
112 matty 10 int length;
113 matty 3
114 matty 10 ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
115     in_uint8s(s, length);
116    
117     return s_check(s);
118 matty 3 }
119    
120 matty 10 /* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */
121 matty 25 static void
122     mcs_send_connect_initial(STREAM mcs_data)
123 matty 3 {
124 matty 10 int datalen = mcs_data->end - mcs_data->data;
125 forsberg 348 int length = 9 + 3 * 34 + 4 + datalen;
126 matty 10 STREAM s;
127 matty 3
128 matty 10 s = iso_init(length + 5);
129 matty 3
130 matty 10 ber_out_header(s, MCS_CONNECT_INITIAL, length);
131 forsberg 348 ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* calling domain */
132     out_uint8(s, 1);
133     ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* called domain */
134     out_uint8(s, 1);
135 matty 3
136 matty 10 ber_out_header(s, BER_TAG_BOOLEAN, 1);
137 matty 24 out_uint8(s, 0xff); /* upward flag */
138 matty 3
139 forsberg 348 mcs_out_domain_params(s, 34, 2, 0, 0xffff); /* target params */
140 matty 24 mcs_out_domain_params(s, 1, 1, 1, 0x420); /* min params */
141     mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff); /* max params */
142 matty 3
143 matty 10 ber_out_header(s, BER_TAG_OCTET_STRING, datalen);
144     out_uint8p(s, mcs_data->data, datalen);
145 matty 3
146 matty 10 s_mark_end(s);
147     iso_send(s);
148 matty 3 }
149    
150 matty 10 /* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
151 jsorg71 1372 static RD_BOOL
152 matty 25 mcs_recv_connect_response(STREAM mcs_data)
153 matty 3 {
154 matty 10 uint8 result;
155     int length;
156     STREAM s;
157 matty 3
158 jsorg71 733 s = iso_recv(NULL);
159 matty 10 if (s == NULL)
160 matty 7 return False;
161    
162 matty 10 ber_parse_header(s, MCS_CONNECT_RESPONSE, &length);
163 matty 3
164 matty 10 ber_parse_header(s, BER_TAG_RESULT, &length);
165     in_uint8(s, result);
166     if (result != 0)
167 matty 7 {
168 matty 30 error("MCS connect: %d\n", result);
169 matty 3 return False;
170     }
171    
172 matty 10 ber_parse_header(s, BER_TAG_INTEGER, &length);
173 matty 24 in_uint8s(s, length); /* connect id */
174 matty 10 mcs_parse_domain_params(s);
175    
176     ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
177 matty 3
178 forsberg 348 sec_process_mcs_data(s);
179     /*
180     if (length > mcs_data->size)
181     {
182 jsorg71 381 error("MCS data length %d, expected %d\n", length,
183 forsberg 348 mcs_data->size);
184     length = mcs_data->size;
185     }
186 matty 3
187 forsberg 348 in_uint8a(s, mcs_data->data, length);
188     mcs_data->p = mcs_data->data;
189     mcs_data->end = mcs_data->data + length;
190     */
191 matty 10 return s_check_end(s);
192 matty 3 }
193    
194 matty 10 /* Send an EDrq message (ASN.1 PER) */
195 matty 25 static void
196 matthewc 192 mcs_send_edrq(void)
197 matty 3 {
198 matty 10 STREAM s;
199 matty 3
200 matty 10 s = iso_init(5);
201 matty 3
202 matty 10 out_uint8(s, (MCS_EDRQ << 2));
203 matty 24 out_uint16_be(s, 1); /* height */
204     out_uint16_be(s, 1); /* interval */
205 matty 3
206 matty 10 s_mark_end(s);
207     iso_send(s);
208 matty 3 }
209    
210 matty 10 /* Send an AUrq message (ASN.1 PER) */
211 matty 25 static void
212 matthewc 192 mcs_send_aurq(void)
213 matty 3 {
214 matty 10 STREAM s;
215 matty 3
216 matty 10 s = iso_init(1);
217 matty 3
218 matty 10 out_uint8(s, (MCS_AURQ << 2));
219 matty 3
220 matty 10 s_mark_end(s);
221     iso_send(s);
222 matty 3 }
223    
224 matty 10 /* Expect a AUcf message (ASN.1 PER) */
225 jsorg71 1372 static RD_BOOL
226 astrand 64 mcs_recv_aucf(uint16 * mcs_userid)
227 matty 3 {
228 matty 10 uint8 opcode, result;
229     STREAM s;
230 matty 3
231 jsorg71 733 s = iso_recv(NULL);
232 matty 10 if (s == NULL)
233 matty 3 return False;
234    
235 matty 10 in_uint8(s, opcode);
236     if ((opcode >> 2) != MCS_AUCF)
237 matty 3 {
238 matty 30 error("expected AUcf, got %d\n", opcode);
239 matty 3 return False;
240     }
241    
242 matty 10 in_uint8(s, result);
243     if (result != 0)
244     {
245 matty 30 error("AUrq: %d\n", result);
246 matty 10 return False;
247     }
248 matty 3
249 matty 10 if (opcode & 2)
250     in_uint16_be(s, *mcs_userid);
251 matty 3
252 matty 10 return s_check_end(s);
253 matty 3 }
254    
255 matty 10 /* Send a CJrq message (ASN.1 PER) */
256 matty 25 static void
257     mcs_send_cjrq(uint16 chanid)
258 matty 3 {
259 matty 10 STREAM s;
260 matty 3
261 forsberg 409 DEBUG_RDP5(("Sending CJRQ for channel #%d\n", chanid));
262    
263 matty 10 s = iso_init(5);
264 matty 3
265 matty 10 out_uint8(s, (MCS_CJRQ << 2));
266 jsorg71 381 out_uint16_be(s, g_mcs_userid);
267 matty 10 out_uint16_be(s, chanid);
268    
269     s_mark_end(s);
270     iso_send(s);
271 matty 3 }
272    
273 matty 10 /* Expect a CJcf message (ASN.1 PER) */
274 jsorg71 1372 static RD_BOOL
275 matthewc 192 mcs_recv_cjcf(void)
276 matty 3 {
277 matty 10 uint8 opcode, result;
278     STREAM s;
279 matty 3
280 jsorg71 733 s = iso_recv(NULL);
281 matty 10 if (s == NULL)
282     return False;
283 matty 3
284 matty 10 in_uint8(s, opcode);
285     if ((opcode >> 2) != MCS_CJCF)
286     {
287 matty 30 error("expected CJcf, got %d\n", opcode);
288 matty 10 return False;
289     }
290 matty 3
291 matty 10 in_uint8(s, result);
292     if (result != 0)
293 matty 3 {
294 matty 30 error("CJrq: %d\n", result);
295 matty 3 return False;
296     }
297    
298 matty 24 in_uint8s(s, 4); /* mcs_userid, req_chanid */
299 matty 10 if (opcode & 2)
300 matty 24 in_uint8s(s, 2); /* join_chanid */
301 matty 3
302 matty 10 return s_check_end(s);
303 matty 3 }
304    
305 matty 10 /* Initialise an MCS transport data packet */
306 matty 25 STREAM
307     mcs_init(int length)
308 matty 3 {
309 matty 10 STREAM s;
310 matty 3
311 matty 10 s = iso_init(length + 8);
312     s_push_layer(s, mcs_hdr, 8);
313 matty 3
314 matty 10 return s;
315 matty 3 }
316    
317 forsberg 409 /* Send an MCS transport data packet to a specific channel */
318 matty 25 void
319 forsberg 409 mcs_send_to_channel(STREAM s, uint16 channel)
320 matty 3 {
321 matty 10 uint16 length;
322 matty 3
323 matty 10 s_pop_layer(s, mcs_hdr);
324     length = s->end - s->p - 8;
325     length |= 0x8000;
326 matty 3
327 matty 10 out_uint8(s, (MCS_SDRQ << 2));
328 jsorg71 381 out_uint16_be(s, g_mcs_userid);
329 forsberg 409 out_uint16_be(s, channel);
330 matty 24 out_uint8(s, 0x70); /* flags */
331 matty 10 out_uint16_be(s, length);
332 matty 3
333 matty 10 iso_send(s);
334 matty 3 }
335    
336 forsberg 409 /* Send an MCS transport data packet to the global channel */
337     void
338     mcs_send(STREAM s)
339     {
340     mcs_send_to_channel(s, MCS_GLOBAL_CHANNEL);
341     }
342    
343 matty 10 /* Receive an MCS transport data packet */
344 matty 25 STREAM
345 jsorg71 733 mcs_recv(uint16 * channel, uint8 * rdpver)
346 matty 3 {
347 matty 10 uint8 opcode, appid, length;
348     STREAM s;
349 matty 3
350 jsorg71 733 s = iso_recv(rdpver);
351 matty 10 if (s == NULL)
352     return NULL;
353 jsorg71 733 if (rdpver != NULL)
354     if (*rdpver != 3)
355     return s;
356 matty 10 in_uint8(s, opcode);
357     appid = opcode >> 2;
358     if (appid != MCS_SDIN)
359 matty 3 {
360 matty 10 if (appid != MCS_DPUM)
361     {
362 matty 30 error("expected data, got %d\n", opcode);
363 matty 10 }
364     return NULL;
365 matty 3 }
366 forsberg 348 in_uint8s(s, 2); /* userid */
367     in_uint16_be(s, *channel);
368     in_uint8s(s, 1); /* flags */
369 matty 10 in_uint8(s, length);
370     if (length & 0x80)
371 matty 24 in_uint8s(s, 1); /* second byte of length */
372 matty 10 return s;
373 matty 3 }
374    
375 matty 10 /* Establish a connection up to the MCS layer */
376 jsorg71 1372 RD_BOOL
377 forsberg 348 mcs_connect(char *server, STREAM mcs_data, char *username)
378 matty 3 {
379 matthewc 432 unsigned int i;
380    
381 forsberg 348 if (!iso_connect(server, username))
382 matty 3 return False;
383    
384 matty 10 mcs_send_connect_initial(mcs_data);
385     if (!mcs_recv_connect_response(mcs_data))
386     goto error;
387 matty 3
388 matty 10 mcs_send_edrq();
389 matty 3
390 matty 10 mcs_send_aurq();
391 jsorg71 381 if (!mcs_recv_aucf(&g_mcs_userid))
392 matty 10 goto error;
393 matty 3
394 forsberg 409 mcs_send_cjrq(g_mcs_userid + MCS_USERCHANNEL_BASE);
395    
396 matty 10 if (!mcs_recv_cjcf())
397     goto error;
398 matty 3
399 matty 10 mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
400     if (!mcs_recv_cjcf())
401     goto error;
402 matty 3
403 matthewc 432 for (i = 0; i < g_num_channels; i++)
404 forsberg 348 {
405 matthewc 432 mcs_send_cjrq(g_channels[i].mcs_id);
406     if (!mcs_recv_cjcf())
407     goto error;
408 forsberg 348 }
409 matty 10 return True;
410 matty 7
411 matty 24 error:
412 matty 10 iso_disconnect();
413     return False;
414 matty 3 }
415 matty 7
416 astrand 977 /* Establish a connection up to the MCS layer */
417 jsorg71 1372 RD_BOOL
418 astrand 977 mcs_reconnect(char *server, STREAM mcs_data)
419     {
420     unsigned int i;
421    
422     if (!iso_reconnect(server))
423     return False;
424    
425     mcs_send_connect_initial(mcs_data);
426     if (!mcs_recv_connect_response(mcs_data))
427     goto error;
428    
429     mcs_send_edrq();
430    
431     mcs_send_aurq();
432     if (!mcs_recv_aucf(&g_mcs_userid))
433     goto error;
434    
435     mcs_send_cjrq(g_mcs_userid + MCS_USERCHANNEL_BASE);
436    
437     if (!mcs_recv_cjcf())
438     goto error;
439    
440     mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
441     if (!mcs_recv_cjcf())
442     goto error;
443    
444     for (i = 0; i < g_num_channels; i++)
445     {
446     mcs_send_cjrq(g_channels[i].mcs_id);
447     if (!mcs_recv_cjcf())
448     goto error;
449     }
450     return True;
451    
452     error:
453     iso_disconnect();
454     return False;
455     }
456    
457 matty 10 /* Disconnect from the MCS layer */
458 matty 25 void
459 matthewc 192 mcs_disconnect(void)
460 matty 10 {
461     iso_disconnect();
462     }
463 astrand 977
464     /* reset the state of the mcs layer */
465     void
466     mcs_reset_state(void)
467     {
468     g_mcs_userid = 0;
469     iso_reset_state();
470     }

  ViewVC Help
Powered by ViewVC 1.1.26