18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include "includes.h" |
#include "rdesktop.h" |
22 |
|
|
23 |
/* Establish a connection up to the MCS layer */ |
uint16 mcs_userid; |
|
HCONN mcs_connect(char *server) |
|
|
{ |
|
|
HCONN conn; |
|
|
MCS_CONNECT_RESPONSE mcr; |
|
|
MCS_AUCF aucf; |
|
24 |
|
|
25 |
if ((conn = iso_connect(server)) == NULL) |
/* Parse an ASN.1 BER header */ |
26 |
return NULL; |
static BOOL ber_parse_header(STREAM s, int tagval, int *length) |
27 |
|
{ |
28 |
|
int tag, len; |
29 |
|
|
30 |
mcs_send_connect_initial(conn); |
if (tagval > 0xff) |
|
if (!iso_recv(conn) || !mcs_io_connect_response(&conn->in, &mcr)) |
|
31 |
{ |
{ |
32 |
fprintf(stderr, "MCS error, expected Connect-Response\n"); |
in_uint16_be(s, tag); |
|
iso_disconnect(conn); |
|
|
return NULL; |
|
33 |
} |
} |
34 |
|
else |
|
if (mcr.result != 0) |
|
|
{ |
|
|
fprintf(stderr, "MCS-Connect-Initial failed, result %d\n", |
|
|
mcr.result); |
|
|
iso_disconnect(conn); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
mcs_send_edrq(conn); |
|
|
|
|
|
mcs_send_aurq(conn); |
|
|
if (!iso_recv(conn) || !mcs_io_aucf(&conn->in, &aucf)) |
|
35 |
{ |
{ |
36 |
fprintf(stderr, "MCS error, expected AUcf\n"); |
in_uint8(s, tag) |
|
mcs_disconnect(conn); |
|
|
return NULL; |
|
37 |
} |
} |
38 |
|
|
39 |
if (aucf.result != 0) |
if (tag != tagval) |
40 |
{ |
{ |
41 |
fprintf(stderr, "AUrq failed, result %d\n", mcr.result); |
ERROR("expected tag %d, got %d\n", tagval, tag); |
42 |
mcs_disconnect(conn); |
return False; |
|
return NULL; |
|
43 |
} |
} |
44 |
|
|
45 |
conn->mcs_userid = aucf.userid; |
in_uint8(s, len); |
46 |
|
|
47 |
if (!mcs_join_channel(conn, aucf.userid + 1001) |
if (len & 0x80) |
|
|| !mcs_join_channel(conn, MCS_GLOBAL_CHANNEL)) |
|
48 |
{ |
{ |
49 |
mcs_disconnect(conn); |
len &= ~0x80; |
50 |
return NULL; |
*length = 0; |
51 |
|
while (len--) |
52 |
|
next_be(s, *length); |
53 |
} |
} |
54 |
|
else *length = len; |
55 |
|
|
56 |
return conn; |
return s_check(s); |
57 |
} |
} |
58 |
|
|
59 |
BOOL mcs_join_channel(HCONN conn, uint16 chanid) |
/* Output an ASN.1 BER header */ |
60 |
|
static void ber_out_header(STREAM s, int tagval, int length) |
61 |
{ |
{ |
62 |
MCS_CJCF cjcf; |
if (tagval > 0xff) |
|
|
|
|
mcs_send_cjrq(conn, chanid); |
|
|
if (!iso_recv(conn) || !mcs_io_cjcf(&conn->in, &cjcf)) |
|
63 |
{ |
{ |
64 |
fprintf(stderr, "MCS error, expected CJcf\n"); |
out_uint16_be(s, tagval); |
|
return False; |
|
65 |
} |
} |
66 |
|
else |
|
if (cjcf.result != 0) |
|
67 |
{ |
{ |
68 |
fprintf(stderr, "CJrq failed, result %d\n", cjcf.result); |
out_uint8(s, tagval); |
|
return False; |
|
69 |
} |
} |
70 |
|
|
71 |
return True; |
if (length >= 0x80) |
72 |
|
{ |
73 |
|
out_uint8(s, 0x82); |
74 |
|
out_uint16_be(s, length); |
75 |
|
} |
76 |
|
else out_uint8(s, length); |
77 |
} |
} |
78 |
|
|
79 |
/* Disconnect from the MCS layer */ |
/* Output an ASN.1 BER integer */ |
80 |
void mcs_disconnect(HCONN conn) |
static void ber_out_integer(STREAM s, int value) |
81 |
{ |
{ |
82 |
/* Not complete */ |
ber_out_header(s, BER_TAG_INTEGER, 2); |
83 |
iso_disconnect(conn); |
out_uint16_be(s, value); |
84 |
} |
} |
85 |
|
|
86 |
/* Send a Connect-Initial message */ |
/* Output a DOMAIN_PARAMS structure (ASN.1 BER) */ |
87 |
void mcs_send_connect_initial(HCONN conn) |
static void mcs_out_domain_params(STREAM s, int max_channels, int max_users, |
88 |
|
int max_tokens, int max_pdusize) |
89 |
{ |
{ |
90 |
MCS_CONNECT_INITIAL mci; |
ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32); |
91 |
|
ber_out_integer(s, max_channels); |
92 |
iso_init(conn); |
ber_out_integer(s, max_users); |
93 |
mcs_make_connect_initial(&mci); |
ber_out_integer(s, max_tokens); |
94 |
mcs_io_connect_initial(&conn->out, &mci); |
ber_out_integer(s, 1); /* num_priorities */ |
95 |
MARK_END(conn->out); |
ber_out_integer(s, 0); /* min_throughput */ |
96 |
iso_send(conn); |
ber_out_integer(s, 1); /* max_height */ |
97 |
|
ber_out_integer(s, max_pdusize); |
98 |
|
ber_out_integer(s, 2); /* ver_protocol */ |
99 |
} |
} |
100 |
|
|
101 |
/* Send a EDrq message */ |
/* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */ |
102 |
void mcs_send_edrq(HCONN conn) |
static BOOL mcs_parse_domain_params(STREAM s) |
103 |
{ |
{ |
104 |
MCS_EDRQ edrq; |
int length; |
105 |
|
|
106 |
iso_init(conn); |
ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length); |
107 |
edrq.height = edrq.interval = 1; |
in_uint8s(s, length); |
|
mcs_io_edrq(&conn->out, &edrq); |
|
|
MARK_END(conn->out); |
|
|
iso_send(conn); |
|
|
} |
|
|
|
|
|
/* Send a AUrq message */ |
|
|
void mcs_send_aurq(HCONN conn) |
|
|
{ |
|
|
MCS_AURQ aurq; |
|
108 |
|
|
109 |
iso_init(conn); |
return s_check(s); |
|
mcs_io_aurq(&conn->out, &aurq); |
|
|
MARK_END(conn->out); |
|
|
iso_send(conn); |
|
110 |
} |
} |
111 |
|
|
112 |
/* Send a CJrq message */ |
/* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */ |
113 |
void mcs_send_cjrq(HCONN conn, uint16 chanid) |
static void mcs_send_connect_initial(STREAM mcs_data) |
114 |
{ |
{ |
115 |
MCS_CJRQ cjrq; |
int datalen = mcs_data->end - mcs_data->data; |
116 |
|
int length = 7 + 3*34 + 4 + datalen; |
117 |
|
STREAM s; |
118 |
|
|
119 |
iso_init(conn); |
s = iso_init(length + 5); |
|
cjrq.userid = conn->mcs_userid; |
|
|
cjrq.chanid = chanid; |
|
|
mcs_io_cjrq(&conn->out, &cjrq); |
|
|
MARK_END(conn->out); |
|
|
iso_send(conn); |
|
|
} |
|
120 |
|
|
121 |
/* Initialise MCS transport data packet */ |
ber_out_header(s, MCS_CONNECT_INITIAL, length); |
122 |
void mcs_init_data(HCONN conn) |
ber_out_header(s, BER_TAG_OCTET_STRING, 0); /* calling domain */ |
123 |
{ |
ber_out_header(s, BER_TAG_OCTET_STRING, 0); /* called domain */ |
|
iso_init(conn); |
|
|
PUSH_LAYER(conn->out, mcs_offset, 8); |
|
|
} |
|
124 |
|
|
125 |
/* Transmit MCS transport data packet */ |
ber_out_header(s, BER_TAG_BOOLEAN, 1); |
126 |
void mcs_send_data(HCONN conn, uint16 chanid, BOOL request) |
out_uint8(s, 0xff); /* upward flag */ |
|
{ |
|
|
MCS_DATA dt; |
|
127 |
|
|
128 |
POP_LAYER(conn->out, mcs_offset); |
mcs_out_domain_params(s, 2, 2, 0, 0xffff); /* target params */ |
129 |
dt.userid = conn->mcs_userid; |
mcs_out_domain_params(s, 1, 1, 1, 0x420); /* min params */ |
130 |
dt.chanid = chanid; |
mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff); /* max params */ |
|
dt.flags = 0x70; |
|
|
dt.length = conn->out.end - conn->out.offset - 8; |
|
|
mcs_io_data(&conn->out, &dt, request); |
|
|
iso_send(conn); |
|
|
} |
|
131 |
|
|
132 |
/* Receive a message on the MCS layer */ |
ber_out_header(s, BER_TAG_OCTET_STRING, datalen); |
133 |
BOOL mcs_recv(HCONN conn, BOOL request) |
out_uint8p(s, mcs_data->data, datalen); |
|
{ |
|
|
MCS_DATA data; |
|
134 |
|
|
135 |
return (iso_recv(conn)) && mcs_io_data(&conn->in, &data, request); |
s_mark_end(s); |
136 |
|
iso_send(s); |
137 |
} |
} |
138 |
|
|
139 |
/* Initialise a DOMAIN_PARAMS structure */ |
/* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */ |
140 |
void mcs_make_domain_params(DOMAIN_PARAMS *dp, uint16 max_channels, |
static BOOL mcs_recv_connect_response(STREAM mcs_data) |
|
uint16 max_users, uint16 max_tokens, uint16 max_pdusize) |
|
141 |
{ |
{ |
142 |
dp->max_channels = max_channels; |
uint8 result; |
143 |
dp->max_users = max_users; |
int length; |
144 |
dp->max_tokens = max_tokens; |
STREAM s; |
|
dp->num_priorities = 1; |
|
|
dp->min_throughput = 0; |
|
|
dp->max_height = 1; |
|
|
dp->max_pdusize = max_pdusize; |
|
|
dp->ver_protocol = 2; |
|
|
} |
|
|
|
|
|
/* RDP-specific 'user data'. Let's just get this right for now - to be |
|
|
decoded later. */ |
|
|
char precanned_connect_userdata[] = { |
|
|
0x00,0x05,0x00,0x14,0x7c,0x00,0x01,0x80,0x9e,0x00,0x08,0x00,0x10,0x00, |
|
|
0x01,0xc0,0x00,0x44,0x75,0x63,0x61,0x80,0x90,0x01,0xc0,0x88,0x00,0x01, |
|
|
0x00,0x08,0x00,0x80,0x02,0xe0,0x01,0x01,0xca,0x03,0xaa,0x09,0x04,0x00, |
|
|
0x00,0xa3,0x01,0x00,0x00,0x52,0x00,0x45,0x00,0x53,0x00,0x31,0x00,0x2d, |
|
|
0x00,0x4e,0x00,0x45,0x00,0x57,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00, |
|
|
0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
|
|
0x00,0x01,0xca,0x00,0x00,0x02,0xc0,0x08,0x00,0x00,0x00,0x00,0x00 }; |
|
145 |
|
|
146 |
/* Initialise a MCS_CONNECT_INITIAL structure */ |
s = iso_recv(); |
147 |
void mcs_make_connect_initial(MCS_CONNECT_INITIAL *mci) |
if (s == NULL) |
148 |
{ |
return False; |
|
mci->calling_domain.length = 0; |
|
|
mci->calling_domain.data = NULL; |
|
|
|
|
|
mci->called_domain.length = 0; |
|
|
mci->called_domain.data = NULL; |
|
|
|
|
|
mci->upward_flag = 0xff; |
|
|
|
|
|
mcs_make_domain_params(&mci->target_params, 2, 2, 0, 0xffff); |
|
|
mcs_make_domain_params(&mci->minimum_params, 1, 1, 1, 0x420); |
|
|
mcs_make_domain_params(&mci->maximum_params, 0xffff, 0xfc17, 0xffff, |
|
|
0xffff); |
|
|
|
|
|
mci->user_data.length = sizeof(precanned_connect_userdata); |
|
|
mci->user_data.data = precanned_connect_userdata; |
|
|
|
|
|
mci->length = 2*2 + 3 + 3*34 + 4 + mci->user_data.length; |
|
|
} |
|
|
|
|
|
/* Marshall/demarshall an ASN.1 BER header */ |
|
|
BOOL ber_io_header(STREAM s, BOOL islong, int tagval, int *length) |
|
|
{ |
|
|
uint16 word_tag; |
|
|
uint8 byte_tag; |
|
|
uint16 word_len; |
|
|
uint8 byte_len; |
|
|
uint8 byte_int; |
|
|
int tag; |
|
|
BOOL res; |
|
149 |
|
|
150 |
/* Read/write tag */ |
ber_parse_header(s, MCS_CONNECT_RESPONSE, &length); |
|
if (islong) { |
|
|
word_tag = tagval; |
|
|
res = msb_io_uint16(s, &word_tag); |
|
|
tag = word_tag; |
|
|
} else { |
|
|
byte_tag = tagval; |
|
|
res = prs_io_uint8(s, &byte_tag); |
|
|
tag = byte_tag; |
|
|
} |
|
151 |
|
|
152 |
if (!res || (tag != tagval)) { |
ber_parse_header(s, BER_TAG_RESULT, &length); |
153 |
fprintf(stderr, "Invalid ASN.1 tag\n"); |
in_uint8(s, result); |
154 |
|
if (result != 0) |
155 |
|
{ |
156 |
|
ERROR("MCS connect: %d\n", result); |
157 |
return False; |
return False; |
158 |
} |
} |
159 |
|
|
160 |
/* Read/write length */ |
ber_parse_header(s, BER_TAG_INTEGER, &length); |
161 |
if (s->marshall) |
in_uint8s(s, length); /* connect id */ |
162 |
|
mcs_parse_domain_params(s); |
163 |
|
|
164 |
|
ber_parse_header(s, BER_TAG_OCTET_STRING, &length); |
165 |
|
if (length > mcs_data->size) |
166 |
{ |
{ |
167 |
if (*length >= 0x80) |
WARN("MCS data length %d\n", length); |
168 |
{ |
length = mcs_data->size; |
|
byte_len = 0x82; |
|
|
word_len = (uint16)*length; |
|
|
res = prs_io_uint8(s, &byte_len); |
|
|
res = res ? msb_io_uint16(s, &word_len) : False; |
|
|
} |
|
|
else |
|
|
{ |
|
|
byte_len = (uint8)*length; |
|
|
res = prs_io_uint8(s, &byte_len); |
|
|
} |
|
169 |
} |
} |
|
else |
|
|
{ |
|
|
if (!prs_io_uint8(s, &byte_len)) |
|
|
return False; |
|
170 |
|
|
171 |
if (byte_len & 0x80) |
in_uint8a(s, mcs_data->data, length); |
172 |
{ |
mcs_data->p = mcs_data->data; |
173 |
byte_len &= ~0x80; |
mcs_data->end = mcs_data->data + length; |
|
*length = 0; |
|
|
while (byte_len--) |
|
|
{ |
|
|
if (!prs_io_uint8(s, &byte_int)) |
|
|
return False; |
|
|
|
|
|
*length <<= 8; |
|
|
*length += byte_int; |
|
|
} |
|
|
} |
|
|
else *length = byte_len; |
|
|
} |
|
174 |
|
|
175 |
return res; |
return s_check_end(s); |
176 |
} |
} |
177 |
|
|
178 |
/* Marshall/demarshall an octet string (ASN.1 BER) */ |
/* Send an EDrq message (ASN.1 PER) */ |
179 |
BOOL ber_io_octet_string(STREAM s, OCTET_STRING *os) |
static void mcs_send_edrq() |
180 |
{ |
{ |
181 |
if (!ber_io_header(s, False, 4, &os->length)) |
STREAM s; |
|
return False; |
|
182 |
|
|
183 |
if (os->length > s->end - s->offset) |
s = iso_init(5); |
|
return False; |
|
184 |
|
|
185 |
if (s->marshall) |
out_uint8(s, (MCS_EDRQ << 2)); |
186 |
{ |
out_uint16_be(s, 1); /* height */ |
187 |
memcpy(s->data + s->offset, os->data, os->length); |
out_uint16_be(s, 1); /* interval */ |
|
} |
|
|
else |
|
|
{ |
|
|
os->data = malloc(os->length); |
|
|
memcpy(os->data, s->data + s->offset, os->length); |
|
|
} |
|
188 |
|
|
189 |
s->offset += os->length; |
s_mark_end(s); |
190 |
return True; |
iso_send(s); |
191 |
} |
} |
192 |
|
|
193 |
/* Marshall/demarshall an integer (ASN.1 BER) */ |
/* Send an AUrq message (ASN.1 PER) */ |
194 |
BOOL ber_io_integer(STREAM s, uint16 *word_int) |
static void mcs_send_aurq() |
195 |
{ |
{ |
196 |
int length = 2; |
STREAM s; |
|
uint8 byte_int; |
|
|
BOOL res; |
|
197 |
|
|
198 |
if (!ber_io_header(s, False, 2, &length)) |
s = iso_init(1); |
|
return False; |
|
199 |
|
|
200 |
if (s->marshall) |
out_uint8(s, (MCS_AURQ << 2)); |
|
{ |
|
|
res = msb_io_uint16(s, word_int); |
|
|
} |
|
|
else |
|
|
{ |
|
|
*word_int = 0; |
|
|
while (length--) |
|
|
{ |
|
|
if (!prs_io_uint8(s, &byte_int)) |
|
|
return False; |
|
201 |
|
|
202 |
*word_int <<= 8; |
s_mark_end(s); |
203 |
*word_int += byte_int; |
iso_send(s); |
|
} |
|
|
} |
|
|
|
|
|
return res; |
|
204 |
} |
} |
205 |
|
|
206 |
/* Marshall/demarshall a simple uint8 type (ASN.1 BER) */ |
/* Expect a AUcf message (ASN.1 PER) */ |
207 |
BOOL ber_io_uint8(STREAM s, uint8 *i, int tagval) |
static BOOL mcs_recv_aucf(uint16 *mcs_userid) |
208 |
{ |
{ |
209 |
int length = 1; |
uint8 opcode, result; |
210 |
|
STREAM s; |
211 |
|
|
212 |
if (!ber_io_header(s, False, tagval, &length)) |
s = iso_recv(); |
213 |
|
if (s == NULL) |
214 |
return False; |
return False; |
215 |
|
|
216 |
if (length != 1) |
in_uint8(s, opcode); |
217 |
|
if ((opcode >> 2) != MCS_AUCF) |
218 |
{ |
{ |
219 |
fprintf(stderr, "Wrong length for simple type\n"); |
ERROR("expected AUcf, got %d\n", opcode); |
220 |
return False; |
return False; |
221 |
} |
} |
222 |
|
|
223 |
return prs_io_uint8(s, i); |
in_uint8(s, result); |
224 |
} |
if (result != 0) |
225 |
|
{ |
226 |
/* Marshall/demarshall a DOMAIN_PARAMS structure (ASN.1 BER) */ |
ERROR("AUrq: %d\n", result); |
227 |
BOOL mcs_io_domain_params(STREAM s, DOMAIN_PARAMS *dp) |
return False; |
228 |
{ |
} |
|
int length = 32; |
|
|
BOOL res; |
|
229 |
|
|
230 |
res = ber_io_header(s, False, 0x30, &length); |
if (opcode & 2) |
231 |
res = res ? ber_io_integer(s, &dp->max_channels ) : False; |
in_uint16_be(s, *mcs_userid); |
|
res = res ? ber_io_integer(s, &dp->max_users ) : False; |
|
|
res = res ? ber_io_integer(s, &dp->max_tokens ) : False; |
|
|
res = res ? ber_io_integer(s, &dp->num_priorities) : False; |
|
|
res = res ? ber_io_integer(s, &dp->min_throughput) : False; |
|
|
res = res ? ber_io_integer(s, &dp->max_height ) : False; |
|
|
res = res ? ber_io_integer(s, &dp->max_pdusize ) : False; |
|
|
res = res ? ber_io_integer(s, &dp->ver_protocol ) : False; |
|
232 |
|
|
233 |
return res; |
return s_check_end(s); |
234 |
} |
} |
235 |
|
|
236 |
/* Marshall/demarshall a MCS_CONNECT_INITIAL structure (ASN.1 BER) */ |
/* Send a CJrq message (ASN.1 PER) */ |
237 |
BOOL mcs_io_connect_initial(STREAM s, MCS_CONNECT_INITIAL *mci) |
static void mcs_send_cjrq(uint16 chanid) |
238 |
{ |
{ |
239 |
BOOL res; |
STREAM s; |
240 |
|
|
241 |
res = ber_io_header(s, True, 0x7f65, &mci->length); |
s = iso_init(5); |
|
res = res ? ber_io_octet_string (s, &mci->calling_domain) : False; |
|
|
res = res ? ber_io_octet_string (s, &mci->called_domain ) : False; |
|
|
res = res ? ber_io_uint8 (s, &mci->upward_flag, 1) : False; |
|
|
res = res ? mcs_io_domain_params(s, &mci->target_params ) : False; |
|
|
res = res ? mcs_io_domain_params(s, &mci->minimum_params) : False; |
|
|
res = res ? mcs_io_domain_params(s, &mci->maximum_params) : False; |
|
|
res = res ? ber_io_octet_string (s, &mci->user_data ) : False; |
|
242 |
|
|
243 |
return res; |
out_uint8(s, (MCS_CJRQ << 2)); |
244 |
|
out_uint16_be(s, mcs_userid); |
245 |
|
out_uint16_be(s, chanid); |
246 |
|
|
247 |
|
s_mark_end(s); |
248 |
|
iso_send(s); |
249 |
} |
} |
250 |
|
|
251 |
/* Marshall/demarshall a MCS_CONNECT_RESPONSE structure (ASN.1 BER) */ |
/* Expect a CJcf message (ASN.1 PER) */ |
252 |
BOOL mcs_io_connect_response(STREAM s, MCS_CONNECT_RESPONSE *mcr) |
static BOOL mcs_recv_cjcf() |
253 |
{ |
{ |
254 |
BOOL res; |
uint8 opcode, result; |
255 |
|
STREAM s; |
|
res = ber_io_header(s, True, 0x7f66, &mcr->length); |
|
|
res = res ? ber_io_uint8 (s, &mcr->result, 10 ) : False; |
|
|
res = res ? ber_io_integer (s, &mcr->connect_id ) : False; |
|
|
res = res ? mcs_io_domain_params(s, &mcr->domain_params) : False; |
|
|
res = res ? ber_io_octet_string (s, &mcr->user_data ) : False; |
|
256 |
|
|
257 |
return res; |
s = iso_recv(); |
258 |
} |
if (s == NULL) |
259 |
|
return False; |
260 |
|
|
261 |
/* Marshall/demarshall an EDrq structure (ASN.1 PER) */ |
in_uint8(s, opcode); |
262 |
BOOL mcs_io_edrq(STREAM s, MCS_EDRQ *edrq) |
if ((opcode >> 2) != MCS_CJCF) |
263 |
{ |
{ |
264 |
uint8 opcode = (1) << 2; |
ERROR("expected CJcf, got %d\n", opcode); |
265 |
uint8 pkt_opcode = opcode; |
return False; |
266 |
BOOL res; |
} |
267 |
|
|
268 |
res = prs_io_uint8(s, &pkt_opcode); |
in_uint8(s, result); |
269 |
if (pkt_opcode != opcode) |
if (result != 0) |
270 |
{ |
{ |
271 |
fprintf(stderr, "Expected EDrq, received %x\n", pkt_opcode); |
ERROR("CJrq: %d\n", result); |
272 |
return False; |
return False; |
273 |
} |
} |
274 |
|
|
275 |
res = res ? msb_io_uint16(s, &edrq->height ) : False; |
in_uint8s(s, 4); /* mcs_userid, req_chanid */ |
276 |
res = res ? msb_io_uint16(s, &edrq->interval) : False; |
if (opcode & 2) |
277 |
|
in_uint8s(s, 2); /* join_chanid */ |
278 |
|
|
279 |
return res; |
return s_check_end(s); |
280 |
} |
} |
281 |
|
|
282 |
/* Marshall/demarshall an AUrq structure (ASN.1 PER) */ |
/* Initialise an MCS transport data packet */ |
283 |
BOOL mcs_io_aurq(STREAM s, MCS_AURQ *aurq) |
STREAM mcs_init(int length) |
284 |
{ |
{ |
285 |
uint8 opcode = (10) << 2; |
STREAM s; |
|
uint8 pkt_opcode = opcode; |
|
|
BOOL res; |
|
286 |
|
|
287 |
res = prs_io_uint8(s, &pkt_opcode); |
s = iso_init(length + 8); |
288 |
if (pkt_opcode != opcode) |
s_push_layer(s, mcs_hdr, 8); |
|
{ |
|
|
fprintf(stderr, "Expected AUrq, received %x\n", pkt_opcode); |
|
|
return False; |
|
|
} |
|
289 |
|
|
290 |
return res; |
return s; |
291 |
} |
} |
292 |
|
|
293 |
/* Marshall/demarshall an AUcf structure (ASN.1 PER) */ |
/* Send an MCS transport data packet */ |
294 |
BOOL mcs_io_aucf(STREAM s, MCS_AUCF *aucf) |
void mcs_send(STREAM s) |
295 |
{ |
{ |
296 |
uint8 opcode = (11) << 2; |
uint16 length; |
|
uint8 pkt_opcode = opcode | 2; |
|
|
BOOL res; |
|
297 |
|
|
298 |
res = prs_io_uint8(s, &pkt_opcode); |
s_pop_layer(s, mcs_hdr); |
299 |
if ((pkt_opcode & 0xfc) != opcode) |
length = s->end - s->p - 8; |
300 |
{ |
length |= 0x8000; |
|
fprintf(stderr, "Expected AUcf, received %x\n", pkt_opcode); |
|
|
return False; |
|
|
} |
|
301 |
|
|
302 |
res = res ? prs_io_uint8 (s, &aucf->result) : False; |
out_uint8(s, (MCS_SDRQ << 2)); |
303 |
if (pkt_opcode & 2) |
out_uint16_be(s, mcs_userid); |
304 |
res = res ? msb_io_uint16(s, &aucf->userid) : False; |
out_uint16_be(s, MCS_GLOBAL_CHANNEL); |
305 |
|
out_uint8(s, 0x70); /* flags */ |
306 |
|
out_uint16_be(s, length); |
307 |
|
|
308 |
return res; |
iso_send(s); |
309 |
} |
} |
310 |
|
|
311 |
/* Marshall/demarshall an CJrq structure (ASN.1 PER) */ |
/* Receive an MCS transport data packet */ |
312 |
BOOL mcs_io_cjrq(STREAM s, MCS_CJRQ *cjrq) |
STREAM mcs_recv() |
313 |
{ |
{ |
314 |
uint8 opcode = (14) << 2; |
uint8 opcode, appid, length; |
315 |
uint8 pkt_opcode = opcode; |
STREAM s; |
316 |
BOOL res; |
|
317 |
|
s = iso_recv(); |
318 |
|
if (s == NULL) |
319 |
|
return NULL; |
320 |
|
|
321 |
res = prs_io_uint8(s, &pkt_opcode); |
in_uint8(s, opcode); |
322 |
if (pkt_opcode != opcode) |
appid = opcode >> 2; |
323 |
|
if (appid != MCS_SDIN) |
324 |
{ |
{ |
325 |
fprintf(stderr, "Expected CJrq, received %x\n", pkt_opcode); |
if (appid != MCS_DPUM) |
326 |
return False; |
{ |
327 |
|
ERROR("expected data, got %d\n", opcode); |
328 |
|
} |
329 |
|
return NULL; |
330 |
} |
} |
331 |
|
|
332 |
res = res ? msb_io_uint16(s, &cjrq->userid) : False; |
in_uint8s(s, 5); /* userid, chanid, flags */ |
333 |
res = res ? msb_io_uint16(s, &cjrq->chanid) : False; |
in_uint8(s, length); |
334 |
|
if (length & 0x80) |
335 |
|
in_uint8s(s, 1); /* second byte of length */ |
336 |
|
|
337 |
return res; |
return s; |
338 |
} |
} |
339 |
|
|
340 |
/* Marshall/demarshall an CJcf structure (ASN.1 PER) */ |
/* Establish a connection up to the MCS layer */ |
341 |
BOOL mcs_io_cjcf(STREAM s, MCS_CJCF *cjcf) |
BOOL mcs_connect(char *server, STREAM mcs_data) |
342 |
{ |
{ |
343 |
uint8 opcode = (15) << 2; |
if (!iso_connect(server)) |
|
uint8 pkt_opcode = opcode | 2; |
|
|
BOOL res; |
|
|
|
|
|
res = prs_io_uint8(s, &pkt_opcode); |
|
|
if ((pkt_opcode & 0xfc) != opcode) |
|
|
{ |
|
|
fprintf(stderr, "Expected CJcf, received %x\n", pkt_opcode); |
|
344 |
return False; |
return False; |
|
} |
|
345 |
|
|
346 |
res = res ? prs_io_uint8 (s, &cjcf->result) : False; |
mcs_send_connect_initial(mcs_data); |
347 |
res = res ? msb_io_uint16(s, &cjcf->userid) : False; |
if (!mcs_recv_connect_response(mcs_data)) |
348 |
res = res ? msb_io_uint16(s, &cjcf->req_chanid) : False; |
goto error; |
|
if (pkt_opcode & 2) |
|
|
res = res ? msb_io_uint16(s, &cjcf->join_chanid) : False; |
|
349 |
|
|
350 |
return res; |
mcs_send_edrq(); |
|
} |
|
351 |
|
|
352 |
/* Marshall/demarshall an SDrq or SDin packet (ASN.1 PER) */ |
mcs_send_aurq(); |
353 |
BOOL mcs_io_data(STREAM s, MCS_DATA *dt, BOOL request) |
if (!mcs_recv_aucf(&mcs_userid)) |
354 |
{ |
goto error; |
|
uint8 opcode = (request ? 25 : 26) << 2; |
|
|
uint8 pkt_opcode = opcode; |
|
|
BOOL res; |
|
355 |
|
|
356 |
res = prs_io_uint8(s, &pkt_opcode); |
mcs_send_cjrq(mcs_userid + 1001); |
357 |
if (pkt_opcode != opcode) |
if (!mcs_recv_cjcf()) |
358 |
{ |
goto error; |
|
fprintf(stderr, "Expected MCS data, received %x\n", pkt_opcode); |
|
|
return False; |
|
|
} |
|
359 |
|
|
360 |
dt->length |= 0x8000; |
mcs_send_cjrq(MCS_GLOBAL_CHANNEL); |
361 |
|
if (!mcs_recv_cjcf()) |
362 |
|
goto error; |
363 |
|
|
364 |
res = res ? msb_io_uint16(s, &dt->userid) : False; |
return True; |
|
res = res ? msb_io_uint16(s, &dt->chanid) : False; |
|
|
res = res ? prs_io_uint8 (s, &dt->flags ) : False; |
|
|
res = res ? msb_io_uint16(s, &dt->length) : False; |
|
365 |
|
|
366 |
return res; |
error: |
367 |
|
iso_disconnect(); |
368 |
|
return False; |
369 |
|
} |
370 |
|
|
371 |
|
/* Disconnect from the MCS layer */ |
372 |
|
void mcs_disconnect() |
373 |
|
{ |
374 |
|
iso_disconnect(); |
375 |
} |
} |