/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (hide annotations)
Wed May 10 07:36:34 2000 UTC (24 years, 1 month ago) by matty
File MIME type: text/plain
File size: 13133 byte(s)
This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches.

1 matty 3 /*
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     return (iso_recv(conn)) && mcs_io_data(&conn->in, &data, request);
180     }
181    
182     /* Initialise a DOMAIN_PARAMS structure */
183     void mcs_make_domain_params(DOMAIN_PARAMS *dp, uint16 max_channels,
184     uint16 max_users, uint16 max_tokens, uint16 max_pdusize)
185     {
186     dp->max_channels = max_channels;
187     dp->max_users = max_users;
188     dp->max_tokens = max_tokens;
189     dp->num_priorities = 1;
190     dp->min_throughput = 0;
191     dp->max_height = 1;
192     dp->max_pdusize = max_pdusize;
193     dp->ver_protocol = 2;
194     }
195    
196     /* RDP-specific 'user data'. Let's just get this right for now - to be
197     decoded later. */
198     char precanned_connect_userdata[] = {
199     0x00,0x05,0x00,0x14,0x7c,0x00,0x01,0x80,0x9e,0x00,0x08,0x00,0x10,0x00,
200     0x01,0xc0,0x00,0x44,0x75,0x63,0x61,0x80,0x90,0x01,0xc0,0x88,0x00,0x01,
201     0x00,0x08,0x00,0x80,0x02,0xe0,0x01,0x01,0xca,0x03,0xaa,0x09,0x04,0x00,
202     0x00,0xa3,0x01,0x00,0x00,0x52,0x00,0x45,0x00,0x53,0x00,0x31,0x00,0x2d,
203     0x00,0x4e,0x00,0x45,0x00,0x57,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
204     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
205     0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
206     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
207     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
208     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
209     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
210     0x00,0x01,0xca,0x00,0x00,0x02,0xc0,0x08,0x00,0x00,0x00,0x00,0x00 };
211    
212     /* Initialise a MCS_CONNECT_INITIAL structure */
213     void mcs_make_connect_initial(MCS_CONNECT_INITIAL *mci)
214     {
215     mci->calling_domain.length = 0;
216     mci->calling_domain.data = NULL;
217    
218     mci->called_domain.length = 0;
219     mci->called_domain.data = NULL;
220    
221     mci->upward_flag = 0xff;
222    
223     mcs_make_domain_params(&mci->target_params, 2, 2, 0, 0xffff);
224     mcs_make_domain_params(&mci->minimum_params, 1, 1, 1, 0x420);
225     mcs_make_domain_params(&mci->maximum_params, 0xffff, 0xfc17, 0xffff,
226     0xffff);
227    
228     mci->user_data.length = sizeof(precanned_connect_userdata);
229     mci->user_data.data = precanned_connect_userdata;
230    
231     mci->length = 2*2 + 3 + 3*34 + 4 + mci->user_data.length;
232     }
233    
234     /* Marshall/demarshall an ASN.1 BER header */
235     BOOL ber_io_header(STREAM s, BOOL islong, int tagval, int *length)
236     {
237     uint16 word_tag;
238     uint8 byte_tag;
239     uint16 word_len;
240     uint8 byte_len;
241     uint8 byte_int;
242     int tag;
243     BOOL res;
244    
245     /* Read/write tag */
246     if (islong) {
247     word_tag = tagval;
248     res = msb_io_uint16(s, &word_tag);
249     tag = word_tag;
250     } else {
251     byte_tag = tagval;
252     res = prs_io_uint8(s, &byte_tag);
253     tag = byte_tag;
254     }
255    
256     if (!res || (tag != tagval)) {
257     fprintf(stderr, "Invalid ASN.1 tag\n");
258     return False;
259     }
260    
261     /* Read/write length */
262     if (s->marshall)
263     {
264     if (*length >= 0x80)
265     {
266     byte_len = 0x82;
267     word_len = (uint16)*length;
268     res = prs_io_uint8(s, &byte_len);
269     res = res ? msb_io_uint16(s, &word_len) : False;
270     }
271     else
272     {
273     byte_len = (uint8)*length;
274     res = prs_io_uint8(s, &byte_len);
275     }
276     }
277     else
278     {
279     if (!prs_io_uint8(s, &byte_len))
280     return False;
281    
282     if (byte_len & 0x80)
283     {
284     byte_len &= ~0x80;
285     *length = 0;
286     while (byte_len--)
287     {
288     if (!prs_io_uint8(s, &byte_int))
289     return False;
290    
291     *length <<= 8;
292     *length += byte_int;
293     }
294     }
295     else *length = byte_len;
296     }
297    
298     return res;
299     }
300    
301     /* Marshall/demarshall an octet string (ASN.1 BER) */
302     BOOL ber_io_octet_string(STREAM s, OCTET_STRING *os)
303     {
304     if (!ber_io_header(s, False, 4, &os->length))
305     return False;
306    
307     if (os->length > s->end - s->offset)
308     return False;
309    
310     if (s->marshall)
311     {
312     memcpy(s->data + s->offset, os->data, os->length);
313     }
314     else
315     {
316     os->data = malloc(os->length);
317     memcpy(os->data, s->data + s->offset, os->length);
318     }
319    
320     s->offset += os->length;
321     return True;
322     }
323    
324     /* Marshall/demarshall an integer (ASN.1 BER) */
325     BOOL ber_io_integer(STREAM s, uint16 *word_int)
326     {
327     int length = 2;
328     uint8 byte_int;
329     BOOL res;
330    
331     if (!ber_io_header(s, False, 2, &length))
332     return False;
333    
334     if (s->marshall)
335     {
336     res = msb_io_uint16(s, word_int);
337     }
338     else
339     {
340     *word_int = 0;
341     while (length--)
342     {
343     if (!prs_io_uint8(s, &byte_int))
344     return False;
345    
346     *word_int <<= 8;
347     *word_int += byte_int;
348     }
349     }
350    
351     return res;
352     }
353    
354     /* Marshall/demarshall a simple uint8 type (ASN.1 BER) */
355     BOOL ber_io_uint8(STREAM s, uint8 *i, int tagval)
356     {
357     int length = 1;
358    
359     if (!ber_io_header(s, False, tagval, &length))
360     return False;
361    
362     if (length != 1)
363     {
364     fprintf(stderr, "Wrong length for simple type\n");
365     return False;
366     }
367    
368     return prs_io_uint8(s, i);
369     }
370    
371     /* Marshall/demarshall a DOMAIN_PARAMS structure (ASN.1 BER) */
372     BOOL mcs_io_domain_params(STREAM s, DOMAIN_PARAMS *dp)
373     {
374     int length = 32;
375     BOOL res;
376    
377     res = ber_io_header(s, False, 0x30, &length);
378     res = res ? ber_io_integer(s, &dp->max_channels ) : False;
379     res = res ? ber_io_integer(s, &dp->max_users ) : False;
380     res = res ? ber_io_integer(s, &dp->max_tokens ) : False;
381     res = res ? ber_io_integer(s, &dp->num_priorities) : False;
382     res = res ? ber_io_integer(s, &dp->min_throughput) : False;
383     res = res ? ber_io_integer(s, &dp->max_height ) : False;
384     res = res ? ber_io_integer(s, &dp->max_pdusize ) : False;
385     res = res ? ber_io_integer(s, &dp->ver_protocol ) : False;
386    
387     return res;
388     }
389    
390     /* Marshall/demarshall a MCS_CONNECT_INITIAL structure (ASN.1 BER) */
391     BOOL mcs_io_connect_initial(STREAM s, MCS_CONNECT_INITIAL *mci)
392     {
393     BOOL res;
394    
395     res = ber_io_header(s, True, 0x7f65, &mci->length);
396     res = res ? ber_io_octet_string (s, &mci->calling_domain) : False;
397     res = res ? ber_io_octet_string (s, &mci->called_domain ) : False;
398     res = res ? ber_io_uint8 (s, &mci->upward_flag, 1) : False;
399     res = res ? mcs_io_domain_params(s, &mci->target_params ) : False;
400     res = res ? mcs_io_domain_params(s, &mci->minimum_params) : False;
401     res = res ? mcs_io_domain_params(s, &mci->maximum_params) : False;
402     res = res ? ber_io_octet_string (s, &mci->user_data ) : False;
403    
404     return res;
405     }
406    
407     /* Marshall/demarshall a MCS_CONNECT_RESPONSE structure (ASN.1 BER) */
408     BOOL mcs_io_connect_response(STREAM s, MCS_CONNECT_RESPONSE *mcr)
409     {
410     BOOL res;
411    
412     res = ber_io_header(s, True, 0x7f66, &mcr->length);
413     res = res ? ber_io_uint8 (s, &mcr->result, 10 ) : False;
414     res = res ? ber_io_integer (s, &mcr->connect_id ) : False;
415     res = res ? mcs_io_domain_params(s, &mcr->domain_params) : False;
416     res = res ? ber_io_octet_string (s, &mcr->user_data ) : False;
417    
418     return res;
419     }
420    
421     /* Marshall/demarshall an EDrq structure (ASN.1 PER) */
422     BOOL mcs_io_edrq(STREAM s, MCS_EDRQ *edrq)
423     {
424     uint8 opcode = (1) << 2;
425     uint8 pkt_opcode = opcode;
426     BOOL res;
427    
428     res = prs_io_uint8(s, &pkt_opcode);
429     if (pkt_opcode != opcode)
430     {
431     fprintf(stderr, "Expected EDrq, received %x\n", pkt_opcode);
432     return False;
433     }
434    
435     res = res ? msb_io_uint16(s, &edrq->height ) : False;
436     res = res ? msb_io_uint16(s, &edrq->interval) : False;
437    
438     return res;
439     }
440    
441     /* Marshall/demarshall an AUrq structure (ASN.1 PER) */
442     BOOL mcs_io_aurq(STREAM s, MCS_AURQ *aurq)
443     {
444     uint8 opcode = (10) << 2;
445     uint8 pkt_opcode = opcode;
446     BOOL res;
447    
448     res = prs_io_uint8(s, &pkt_opcode);
449     if (pkt_opcode != opcode)
450     {
451     fprintf(stderr, "Expected AUrq, received %x\n", pkt_opcode);
452     return False;
453     }
454    
455     return res;
456     }
457    
458     /* Marshall/demarshall an AUcf structure (ASN.1 PER) */
459     BOOL mcs_io_aucf(STREAM s, MCS_AUCF *aucf)
460     {
461     uint8 opcode = (11) << 2;
462     uint8 pkt_opcode = opcode | 2;
463     BOOL res;
464    
465     res = prs_io_uint8(s, &pkt_opcode);
466     if ((pkt_opcode & 0xfc) != opcode)
467     {
468     fprintf(stderr, "Expected AUcf, received %x\n", pkt_opcode);
469     return False;
470     }
471    
472     res = res ? prs_io_uint8 (s, &aucf->result) : False;
473     if (pkt_opcode & 2)
474     res = res ? msb_io_uint16(s, &aucf->userid) : False;
475    
476     return res;
477     }
478    
479     /* Marshall/demarshall an CJrq structure (ASN.1 PER) */
480     BOOL mcs_io_cjrq(STREAM s, MCS_CJRQ *cjrq)
481     {
482     uint8 opcode = (14) << 2;
483     uint8 pkt_opcode = opcode;
484     BOOL res;
485    
486     res = prs_io_uint8(s, &pkt_opcode);
487     if (pkt_opcode != opcode)
488     {
489     fprintf(stderr, "Expected CJrq, received %x\n", pkt_opcode);
490     return False;
491     }
492    
493     res = res ? msb_io_uint16(s, &cjrq->userid) : False;
494     res = res ? msb_io_uint16(s, &cjrq->chanid) : False;
495    
496     return res;
497     }
498    
499     /* Marshall/demarshall an CJcf structure (ASN.1 PER) */
500     BOOL mcs_io_cjcf(STREAM s, MCS_CJCF *cjcf)
501     {
502     uint8 opcode = (15) << 2;
503     uint8 pkt_opcode = opcode | 2;
504     BOOL res;
505    
506     res = prs_io_uint8(s, &pkt_opcode);
507     if ((pkt_opcode & 0xfc) != opcode)
508     {
509     fprintf(stderr, "Expected CJcf, received %x\n", pkt_opcode);
510     return False;
511     }
512    
513     res = res ? prs_io_uint8 (s, &cjcf->result) : False;
514     res = res ? msb_io_uint16(s, &cjcf->userid) : False;
515     res = res ? msb_io_uint16(s, &cjcf->req_chanid) : False;
516     if (pkt_opcode & 2)
517     res = res ? msb_io_uint16(s, &cjcf->join_chanid) : False;
518    
519     return res;
520     }
521    
522     /* Marshall/demarshall an SDrq or SDin packet (ASN.1 PER) */
523     BOOL mcs_io_data(STREAM s, MCS_DATA *dt, BOOL request)
524     {
525     uint8 opcode = (request ? 25 : 26) << 2;
526     uint8 pkt_opcode = opcode;
527     BOOL res;
528    
529     res = prs_io_uint8(s, &pkt_opcode);
530     if (pkt_opcode != opcode)
531     {
532     fprintf(stderr, "Expected MCS data, received %x\n", pkt_opcode);
533     return False;
534     }
535    
536     dt->length |= 0x8000;
537    
538     res = res ? msb_io_uint16(s, &dt->userid) : False;
539     res = res ? msb_io_uint16(s, &dt->chanid) : False;
540     res = res ? prs_io_uint8 (s, &dt->flags ) : False;
541     res = res ? msb_io_uint16(s, &dt->length) : False;
542    
543     return res;
544     }

  ViewVC Help
Powered by ViewVC 1.1.26