/[rdesktop]/sourceforge.net/trunk/rdesktop/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

Contents of /sourceforge.net/trunk/rdesktop/mcs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Fri Jul 7 09:40:03 2000 UTC (23 years, 11 months ago) by matty
File MIME type: text/plain
File size: 13595 byte(s)
Miscellaneous updates: implemented some more protocol features including
colour maps. Started on a new bitmap decompression engine which is not
completely working yet - however I am going back on the road so I am
committing now.

1 /*
2 rdesktop: A Remote Desktop Protocol client.
3 Protocol services - Multipoint Communications Service
4 Copyright (C) Matthew Chapman 1999-2000
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 "includes.h"
22
23 /* Establish a connection up to the MCS layer */
24 HCONN mcs_connect(char *server)
25 {
26 HCONN conn;
27 MCS_CONNECT_RESPONSE mcr;
28 MCS_AUCF aucf;
29
30 if ((conn = iso_connect(server)) == NULL)
31 return NULL;
32
33 mcs_send_connect_initial(conn);
34 if (!iso_recv(conn) || !mcs_io_connect_response(&conn->in, &mcr))
35 {
36 fprintf(stderr, "MCS error, expected Connect-Response\n");
37 iso_disconnect(conn);
38 return NULL;
39 }
40
41 if (mcr.result != 0)
42 {
43 fprintf(stderr, "MCS-Connect-Initial failed, result %d\n",
44 mcr.result);
45 iso_disconnect(conn);
46 return NULL;
47 }
48
49 mcs_send_edrq(conn);
50
51 mcs_send_aurq(conn);
52 if (!iso_recv(conn) || !mcs_io_aucf(&conn->in, &aucf))
53 {
54 fprintf(stderr, "MCS error, expected AUcf\n");
55 mcs_disconnect(conn);
56 return NULL;
57 }
58
59 if (aucf.result != 0)
60 {
61 fprintf(stderr, "AUrq failed, result %d\n", mcr.result);
62 mcs_disconnect(conn);
63 return NULL;
64 }
65
66 conn->mcs_userid = aucf.userid;
67
68 if (!mcs_join_channel(conn, aucf.userid + 1001)
69 || !mcs_join_channel(conn, MCS_GLOBAL_CHANNEL))
70 {
71 mcs_disconnect(conn);
72 return NULL;
73 }
74
75 return conn;
76 }
77
78 BOOL mcs_join_channel(HCONN conn, uint16 chanid)
79 {
80 MCS_CJCF cjcf;
81
82 mcs_send_cjrq(conn, chanid);
83 if (!iso_recv(conn) || !mcs_io_cjcf(&conn->in, &cjcf))
84 {
85 fprintf(stderr, "MCS error, expected CJcf\n");
86 return False;
87 }
88
89 if (cjcf.result != 0)
90 {
91 fprintf(stderr, "CJrq failed, result %d\n", cjcf.result);
92 return False;
93 }
94
95 return True;
96 }
97
98 /* Disconnect from the MCS layer */
99 void mcs_disconnect(HCONN conn)
100 {
101 /* Not complete */
102 iso_disconnect(conn);
103 }
104
105 /* Send a Connect-Initial message */
106 void mcs_send_connect_initial(HCONN conn)
107 {
108 MCS_CONNECT_INITIAL mci;
109
110 iso_init(conn);
111 mcs_make_connect_initial(&mci);
112 mcs_io_connect_initial(&conn->out, &mci);
113 MARK_END(conn->out);
114 iso_send(conn);
115 }
116
117 /* Send a EDrq message */
118 void mcs_send_edrq(HCONN conn)
119 {
120 MCS_EDRQ edrq;
121
122 iso_init(conn);
123 edrq.height = edrq.interval = 1;
124 mcs_io_edrq(&conn->out, &edrq);
125 MARK_END(conn->out);
126 iso_send(conn);
127 }
128
129 /* Send a AUrq message */
130 void mcs_send_aurq(HCONN conn)
131 {
132 MCS_AURQ aurq;
133
134 iso_init(conn);
135 mcs_io_aurq(&conn->out, &aurq);
136 MARK_END(conn->out);
137 iso_send(conn);
138 }
139
140 /* Send a CJrq message */
141 void mcs_send_cjrq(HCONN conn, uint16 chanid)
142 {
143 MCS_CJRQ cjrq;
144
145 iso_init(conn);
146 cjrq.userid = conn->mcs_userid;
147 cjrq.chanid = chanid;
148 mcs_io_cjrq(&conn->out, &cjrq);
149 MARK_END(conn->out);
150 iso_send(conn);
151 }
152
153 /* Initialise MCS transport data packet */
154 void mcs_init_data(HCONN conn)
155 {
156 iso_init(conn);
157 PUSH_LAYER(conn->out, mcs_offset, 8);
158 }
159
160 /* Transmit MCS transport data packet */
161 void mcs_send_data(HCONN conn, uint16 chanid, BOOL request)
162 {
163 MCS_DATA dt;
164
165 POP_LAYER(conn->out, mcs_offset);
166 dt.userid = conn->mcs_userid;
167 dt.chanid = chanid;
168 dt.flags = 0x70;
169 dt.length = conn->out.end - conn->out.offset - 8;
170 mcs_io_data(&conn->out, &dt, request);
171 iso_send(conn);
172 }
173
174 /* Receive a message on the MCS layer */
175 BOOL mcs_recv(HCONN conn, BOOL request)
176 {
177 MCS_DATA data;
178
179 if (!iso_recv(conn) || !mcs_io_data(&conn->in, &data, request))
180 return False;
181
182 #ifdef MCS_DEBUG
183 fprintf(stderr, "MCS packet\n");
184 dump_data(conn->in.data+conn->in.offset, conn->in.end-conn->in.offset);
185 #endif
186
187 conn->in.rdp_offset = conn->in.offset;
188 return True;
189 }
190
191 /* Initialise a DOMAIN_PARAMS structure */
192 void mcs_make_domain_params(DOMAIN_PARAMS *dp, uint16 max_channels,
193 uint16 max_users, uint16 max_tokens, uint16 max_pdusize)
194 {
195 dp->max_channels = max_channels;
196 dp->max_users = max_users;
197 dp->max_tokens = max_tokens;
198 dp->num_priorities = 1;
199 dp->min_throughput = 0;
200 dp->max_height = 1;
201 dp->max_pdusize = max_pdusize;
202 dp->ver_protocol = 2;
203 }
204
205 /* RDP-specific 'user data'. Let's just get this right for now - to be
206 decoded later. */
207 char precanned_connect_userdata[] = {
208 0x00,0x05,0x00,0x14,0x7c,0x00,0x01,0x80,0x9e,0x00,0x08,0x00,0x10,0x00,
209 0x01,0xc0,0x00,0x44,0x75,0x63,0x61,0x80,0x90,0x01,0xc0,0x88,0x00,0x01,
210 0x00,0x08,0x00,0x80,0x02,0xe0,0x01,0x01,0xca,0x03,0xaa,0x09,0x04,0x00,
211 0x00,0xa3,0x01,0x00,0x00,0x52,0x00,0x45,0x00,0x53,0x00,0x31,0x00,0x2d,
212 0x00,0x4e,0x00,0x45,0x00,0x57,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
213 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
214 0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
215 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
216 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
217 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
218 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
219 0x00,0x01,0xca,0x00,0x00,0x02,0xc0,0x08,0x00,0x00,0x00,0x00,0x00 };
220
221 /* Initialise a MCS_CONNECT_INITIAL structure */
222 void mcs_make_connect_initial(MCS_CONNECT_INITIAL *mci)
223 {
224 mci->calling_domain.length = 0;
225 mci->calling_domain.data = NULL;
226
227 mci->called_domain.length = 0;
228 mci->called_domain.data = NULL;
229
230 mci->upward_flag = 0xff;
231
232 mcs_make_domain_params(&mci->target_params, 2, 2, 0, 0xffff);
233 mcs_make_domain_params(&mci->minimum_params, 1, 1, 1, 0x420);
234 mcs_make_domain_params(&mci->maximum_params, 0xffff, 0xfc17, 0xffff,
235 0xffff);
236
237 mci->user_data.length = sizeof(precanned_connect_userdata);
238 mci->user_data.data = precanned_connect_userdata;
239
240 mci->length = 2*2 + 3 + 3*34 + 4 + mci->user_data.length;
241 }
242
243 /* Marshall/demarshall an ASN.1 BER header */
244 BOOL ber_io_header(STREAM s, BOOL islong, int tagval, int *length)
245 {
246 uint16 word_tag;
247 uint8 byte_tag;
248 uint16 word_len;
249 uint8 byte_len;
250 uint8 byte_int;
251 int tag;
252 BOOL res;
253
254 /* Read/write tag */
255 if (islong)
256 {
257 word_tag = tagval;
258 res = msb_io_uint16(s, &word_tag);
259 tag = word_tag;
260 }
261 else
262 {
263 byte_tag = tagval;
264 res = prs_io_uint8(s, &byte_tag);
265 tag = byte_tag;
266 }
267
268 if (!res || (tag != tagval))
269 {
270 fprintf(stderr, "Invalid ASN.1 tag\n");
271 return False;
272 }
273
274 /* Read/write length */
275 if (s->marshall)
276 {
277 if (*length >= 0x80)
278 {
279 byte_len = 0x82;
280 word_len = (uint16)*length;
281 res = prs_io_uint8(s, &byte_len);
282 res = res ? msb_io_uint16(s, &word_len) : False;
283 }
284 else
285 {
286 byte_len = (uint8)*length;
287 res = prs_io_uint8(s, &byte_len);
288 }
289 }
290 else
291 {
292 if (!prs_io_uint8(s, &byte_len))
293 return False;
294
295 if (byte_len & 0x80)
296 {
297 byte_len &= ~0x80;
298 *length = 0;
299 while (byte_len--)
300 {
301 if (!prs_io_uint8(s, &byte_int))
302 return False;
303
304 *length <<= 8;
305 *length += byte_int;
306 }
307 }
308 else *length = byte_len;
309 }
310
311 return res;
312 }
313
314 /* Marshall/demarshall an octet string (ASN.1 BER) */
315 BOOL ber_io_octet_string(STREAM s, OCTET_STRING *os)
316 {
317 if (!ber_io_header(s, False, 4, &os->length))
318 return False;
319
320 if (os->length > s->end - s->offset)
321 return False;
322
323 if (s->marshall)
324 {
325 memcpy(s->data + s->offset, os->data, os->length);
326 }
327 else
328 {
329 os->data = malloc(os->length);
330 memcpy(os->data, s->data + s->offset, os->length);
331 }
332
333 s->offset += os->length;
334 return True;
335 }
336
337 /* Marshall/demarshall an integer (ASN.1 BER) */
338 BOOL ber_io_integer(STREAM s, uint16 *word_int)
339 {
340 int length = 2;
341 uint8 byte_int;
342 BOOL res;
343
344 if (!ber_io_header(s, False, 2, &length))
345 return False;
346
347 if (s->marshall)
348 {
349 res = msb_io_uint16(s, word_int);
350 }
351 else
352 {
353 *word_int = 0;
354 while (length--)
355 {
356 if (!prs_io_uint8(s, &byte_int))
357 return False;
358
359 *word_int <<= 8;
360 *word_int += byte_int;
361 }
362 }
363
364 return res;
365 }
366
367 /* Marshall/demarshall a simple uint8 type (ASN.1 BER) */
368 BOOL ber_io_uint8(STREAM s, uint8 *i, int tagval)
369 {
370 int length = 1;
371
372 if (!ber_io_header(s, False, tagval, &length))
373 return False;
374
375 if (length != 1)
376 {
377 fprintf(stderr, "Wrong length for simple type\n");
378 return False;
379 }
380
381 return prs_io_uint8(s, i);
382 }
383
384 /* Marshall/demarshall a DOMAIN_PARAMS structure (ASN.1 BER) */
385 BOOL mcs_io_domain_params(STREAM s, DOMAIN_PARAMS *dp)
386 {
387 int length = 32;
388 BOOL res;
389
390 res = ber_io_header(s, False, 0x30, &length);
391 res = res ? ber_io_integer(s, &dp->max_channels ) : False;
392 res = res ? ber_io_integer(s, &dp->max_users ) : False;
393 res = res ? ber_io_integer(s, &dp->max_tokens ) : False;
394 res = res ? ber_io_integer(s, &dp->num_priorities) : False;
395 res = res ? ber_io_integer(s, &dp->min_throughput) : False;
396 res = res ? ber_io_integer(s, &dp->max_height ) : False;
397 res = res ? ber_io_integer(s, &dp->max_pdusize ) : False;
398 res = res ? ber_io_integer(s, &dp->ver_protocol ) : False;
399
400 return res;
401 }
402
403 /* Marshall/demarshall a MCS_CONNECT_INITIAL structure (ASN.1 BER) */
404 BOOL mcs_io_connect_initial(STREAM s, MCS_CONNECT_INITIAL *mci)
405 {
406 BOOL res;
407
408 res = ber_io_header(s, True, 0x7f65, &mci->length);
409 res = res ? ber_io_octet_string (s, &mci->calling_domain) : False;
410 res = res ? ber_io_octet_string (s, &mci->called_domain ) : False;
411 res = res ? ber_io_uint8 (s, &mci->upward_flag, 1) : False;
412 res = res ? mcs_io_domain_params(s, &mci->target_params ) : False;
413 res = res ? mcs_io_domain_params(s, &mci->minimum_params) : False;
414 res = res ? mcs_io_domain_params(s, &mci->maximum_params) : False;
415 res = res ? ber_io_octet_string (s, &mci->user_data ) : False;
416
417 return res;
418 }
419
420 /* Marshall/demarshall a MCS_CONNECT_RESPONSE structure (ASN.1 BER) */
421 BOOL mcs_io_connect_response(STREAM s, MCS_CONNECT_RESPONSE *mcr)
422 {
423 BOOL res;
424
425 res = ber_io_header(s, True, 0x7f66, &mcr->length);
426 res = res ? ber_io_uint8 (s, &mcr->result, 10 ) : False;
427 res = res ? ber_io_integer (s, &mcr->connect_id ) : False;
428 res = res ? mcs_io_domain_params(s, &mcr->domain_params) : False;
429 res = res ? ber_io_octet_string (s, &mcr->user_data ) : False;
430
431 return res;
432 }
433
434 /* Marshall/demarshall an EDrq structure (ASN.1 PER) */
435 BOOL mcs_io_edrq(STREAM s, MCS_EDRQ *edrq)
436 {
437 uint8 opcode = (1) << 2;
438 uint8 pkt_opcode = opcode;
439 BOOL res;
440
441 res = prs_io_uint8(s, &pkt_opcode);
442 if (pkt_opcode != opcode)
443 {
444 fprintf(stderr, "Expected EDrq, received %x\n", pkt_opcode);
445 return False;
446 }
447
448 res = res ? msb_io_uint16(s, &edrq->height ) : False;
449 res = res ? msb_io_uint16(s, &edrq->interval) : False;
450
451 return res;
452 }
453
454 /* Marshall/demarshall an AUrq structure (ASN.1 PER) */
455 BOOL mcs_io_aurq(STREAM s, MCS_AURQ *aurq)
456 {
457 uint8 opcode = (10) << 2;
458 uint8 pkt_opcode = opcode;
459 BOOL res;
460
461 res = prs_io_uint8(s, &pkt_opcode);
462 if (pkt_opcode != opcode)
463 {
464 fprintf(stderr, "Expected AUrq, received %x\n", pkt_opcode);
465 return False;
466 }
467
468 return res;
469 }
470
471 /* Marshall/demarshall an AUcf structure (ASN.1 PER) */
472 BOOL mcs_io_aucf(STREAM s, MCS_AUCF *aucf)
473 {
474 uint8 opcode = (11) << 2;
475 uint8 pkt_opcode = opcode | 2;
476 BOOL res;
477
478 res = prs_io_uint8(s, &pkt_opcode);
479 if ((pkt_opcode & 0xfc) != opcode)
480 {
481 fprintf(stderr, "Expected AUcf, received %x\n", pkt_opcode);
482 return False;
483 }
484
485 res = res ? prs_io_uint8 (s, &aucf->result) : False;
486 if (pkt_opcode & 2)
487 res = res ? msb_io_uint16(s, &aucf->userid) : False;
488
489 return res;
490 }
491
492 /* Marshall/demarshall an CJrq structure (ASN.1 PER) */
493 BOOL mcs_io_cjrq(STREAM s, MCS_CJRQ *cjrq)
494 {
495 uint8 opcode = (14) << 2;
496 uint8 pkt_opcode = opcode;
497 BOOL res;
498
499 res = prs_io_uint8(s, &pkt_opcode);
500 if (pkt_opcode != opcode)
501 {
502 fprintf(stderr, "Expected CJrq, received %x\n", pkt_opcode);
503 return False;
504 }
505
506 res = res ? msb_io_uint16(s, &cjrq->userid) : False;
507 res = res ? msb_io_uint16(s, &cjrq->chanid) : False;
508
509 return res;
510 }
511
512 /* Marshall/demarshall an CJcf structure (ASN.1 PER) */
513 BOOL mcs_io_cjcf(STREAM s, MCS_CJCF *cjcf)
514 {
515 uint8 opcode = (15) << 2;
516 uint8 pkt_opcode = opcode | 2;
517 BOOL res;
518
519 res = prs_io_uint8(s, &pkt_opcode);
520 if ((pkt_opcode & 0xfc) != opcode)
521 {
522 fprintf(stderr, "Expected CJcf, received %x\n", pkt_opcode);
523 return False;
524 }
525
526 res = res ? prs_io_uint8 (s, &cjcf->result) : False;
527 res = res ? msb_io_uint16(s, &cjcf->userid) : False;
528 res = res ? msb_io_uint16(s, &cjcf->req_chanid) : False;
529 if (pkt_opcode & 2)
530 res = res ? msb_io_uint16(s, &cjcf->join_chanid) : False;
531
532 return res;
533 }
534
535 /* Marshall/demarshall an SDrq or SDin packet (ASN.1 PER) */
536 BOOL mcs_io_data(STREAM s, MCS_DATA *dt, BOOL request)
537 {
538 uint8 opcode = (request ? 25 : 26) << 2;
539 uint8 pkt_opcode = opcode;
540 uint8 byte1, byte2;
541 BOOL res;
542
543 res = prs_io_uint8(s, &pkt_opcode);
544 if (pkt_opcode != opcode)
545 {
546 fprintf(stderr, "Expected MCS data, received %x\n", pkt_opcode);
547 return False;
548 }
549
550 dt->length |= 0x8000;
551
552 res = res ? msb_io_uint16(s, &dt->userid) : False;
553 res = res ? msb_io_uint16(s, &dt->chanid) : False;
554 res = res ? prs_io_uint8 (s, &dt->flags ) : False;
555
556 if (s->marshall)
557 {
558 res = res ? msb_io_uint16(s, &dt->length) : False;
559 }
560 else
561 {
562 res = res ? prs_io_uint8(s, &byte1) : False;
563 if (byte1 & 0x80)
564 {
565 res = res ? prs_io_uint8(s, &byte2) : False;
566 dt->length = ((byte1 & ~0x80) << 8) + byte2;
567 }
568 else dt->length = byte1;
569 }
570
571 return res;
572 }
573

  ViewVC Help
Powered by ViewVC 1.1.26