/src/FreeRDP/libfreerdp/core/mcs.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * T.125 Multipoint Communication Service (MCS) Protocol |
4 | | * |
5 | | * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
8 | | * Copyright 2017 Armin Novak <armin.novak@thincast.com> |
9 | | * Copyright 2017 Thincast Technologies GmbH |
10 | | * |
11 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
12 | | * you may not use this file except in compliance with the License. |
13 | | * You may obtain a copy of the License at |
14 | | * |
15 | | * http://www.apache.org/licenses/LICENSE-2.0 |
16 | | * |
17 | | * Unless required by applicable law or agreed to in writing, software |
18 | | * distributed under the License is distributed on an "AS IS" BASIS, |
19 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | * See the License for the specific language governing permissions and |
21 | | * limitations under the License. |
22 | | */ |
23 | | |
24 | | #include <freerdp/config.h> |
25 | | |
26 | | #include <winpr/crt.h> |
27 | | #include <winpr/assert.h> |
28 | | #include <freerdp/log.h> |
29 | | |
30 | | #include "gcc.h" |
31 | | |
32 | | #include "mcs.h" |
33 | | #include "tpdu.h" |
34 | | #include "tpkt.h" |
35 | | #include "client.h" |
36 | | #include "connection.h" |
37 | | |
38 | 52.0k | #define MCS_TAG FREERDP_TAG("core") |
39 | | |
40 | | /** |
41 | | * T.125 MCS is defined in: |
42 | | * |
43 | | * http://www.itu.int/rec/T-REC-T.125-199802-I/ |
44 | | * ITU-T T.125 Multipoint Communication Service Protocol Specification |
45 | | */ |
46 | | |
47 | | /** |
48 | | * Connect-Initial ::= [APPLICATION 101] IMPLICIT SEQUENCE |
49 | | * { |
50 | | * callingDomainSelector OCTET_STRING, |
51 | | * calledDomainSelector OCTET_STRING, |
52 | | * upwardFlag BOOLEAN, |
53 | | * targetParameters DomainParameters, |
54 | | * minimumParameters DomainParameters, |
55 | | * maximumParameters DomainParameters, |
56 | | * userData OCTET_STRING |
57 | | * } |
58 | | * |
59 | | * DomainParameters ::= SEQUENCE |
60 | | * { |
61 | | * maxChannelIds INTEGER (0..MAX), |
62 | | * maxUserIds INTEGER (0..MAX), |
63 | | * maxTokenIds INTEGER (0..MAX), |
64 | | * numPriorities INTEGER (0..MAX), |
65 | | * minThroughput INTEGER (0..MAX), |
66 | | * maxHeight INTEGER (0..MAX), |
67 | | * maxMCSPDUsize INTEGER (0..MAX), |
68 | | * protocolVersion INTEGER (0..MAX) |
69 | | * } |
70 | | * |
71 | | * Connect-Response ::= [APPLICATION 102] IMPLICIT SEQUENCE |
72 | | * { |
73 | | * result Result, |
74 | | * calledConnectId INTEGER (0..MAX), |
75 | | * domainParameters DomainParameters, |
76 | | * userData OCTET_STRING |
77 | | * } |
78 | | * |
79 | | * Result ::= ENUMERATED |
80 | | * { |
81 | | * rt-successful (0), |
82 | | * rt-domain-merging (1), |
83 | | * rt-domain-not-hierarchical (2), |
84 | | * rt-no-such-channel (3), |
85 | | * rt-no-such-domain (4), |
86 | | * rt-no-such-user (5), |
87 | | * rt-not-admitted (6), |
88 | | * rt-other-user-id (7), |
89 | | * rt-parameters-unacceptable (8), |
90 | | * rt-token-not-available (9), |
91 | | * rt-token-not-possessed (10), |
92 | | * rt-too-many-channels (11), |
93 | | * rt-too-many-tokens (12), |
94 | | * rt-too-many-users (13), |
95 | | * rt-unspecified-failure (14), |
96 | | * rt-user-rejected (15) |
97 | | * } |
98 | | * |
99 | | * ErectDomainRequest ::= [APPLICATION 1] IMPLICIT SEQUENCE |
100 | | * { |
101 | | * subHeight INTEGER (0..MAX), |
102 | | * subInterval INTEGER (0..MAX) |
103 | | * } |
104 | | * |
105 | | * AttachUserRequest ::= [APPLICATION 10] IMPLICIT SEQUENCE |
106 | | * { |
107 | | * } |
108 | | * |
109 | | * AttachUserConfirm ::= [APPLICATION 11] IMPLICIT SEQUENCE |
110 | | * { |
111 | | * result Result, |
112 | | * initiator UserId OPTIONAL |
113 | | * } |
114 | | * |
115 | | * ChannelJoinRequest ::= [APPLICATION 14] IMPLICIT SEQUENCE |
116 | | * { |
117 | | * initiator UserId, |
118 | | * channelId ChannelId |
119 | | * } |
120 | | * |
121 | | * ChannelJoinConfirm ::= [APPLICATION 15] IMPLICIT SEQUENCE |
122 | | * { |
123 | | * result Result, |
124 | | * initiator UserId, |
125 | | * requested ChannelId, |
126 | | * channelId ChannelId OPTIONAL |
127 | | * } |
128 | | * |
129 | | * SendDataRequest ::= [APPLICATION 25] IMPLICIT SEQUENCE |
130 | | * { |
131 | | * initiator UserId, |
132 | | * channelId ChannelId, |
133 | | * dataPriority DataPriority, |
134 | | * segmentation Segmentation, |
135 | | * userData OCTET_STRING |
136 | | * } |
137 | | * |
138 | | * DataPriority ::= CHOICE |
139 | | * { |
140 | | * top NULL, |
141 | | * high NULL, |
142 | | * medium NULL, |
143 | | * low NULL, |
144 | | * ... |
145 | | * } |
146 | | * |
147 | | * Segmentation ::= BIT_STRING |
148 | | * { |
149 | | * begin (0), |
150 | | * end (1) |
151 | | * } (SIZE(2)) |
152 | | * |
153 | | * SendDataIndication ::= SEQUENCE |
154 | | * { |
155 | | * initiator UserId, |
156 | | * channelId ChannelId, |
157 | | * reliability BOOLEAN, |
158 | | * domainReferenceID INTEGER (0..65535) OPTIONAL, |
159 | | * dataPriority DataPriority, |
160 | | * segmentation Segmentation, |
161 | | * userData OCTET_STRING, |
162 | | * totalDataSize INTEGER OPTIONAL, |
163 | | * nonStandard SEQUENCE OF NonStandardParameter OPTIONAL, |
164 | | * ... |
165 | | * } |
166 | | * |
167 | | */ |
168 | | |
169 | | static const BYTE callingDomainSelector[1] = { 0x01 }; |
170 | | static const BYTE calledDomainSelector[1] = { 0x01 }; |
171 | | |
172 | | /* |
173 | | static const char* const mcs_result_enumerated[] = |
174 | | { |
175 | | "rt-successful", |
176 | | "rt-domain-merging", |
177 | | "rt-domain-not-hierarchical", |
178 | | "rt-no-such-channel", |
179 | | "rt-no-such-domain", |
180 | | "rt-no-such-user", |
181 | | "rt-not-admitted", |
182 | | "rt-other-user-id", |
183 | | "rt-parameters-unacceptable", |
184 | | "rt-token-not-available", |
185 | | "rt-token-not-possessed", |
186 | | "rt-too-many-channels", |
187 | | "rt-too-many-tokens", |
188 | | "rt-too-many-users", |
189 | | "rt-unspecified-failure", |
190 | | "rt-user-rejected" |
191 | | }; |
192 | | */ |
193 | | |
194 | | const char* mcs_domain_pdu_string(DomainMCSPDU pdu) |
195 | 104 | { |
196 | 104 | switch (pdu) |
197 | 104 | { |
198 | 9 | case DomainMCSPDU_PlumbDomainIndication: |
199 | 9 | return "DomainMCSPDU_PlumbDomainIndication"; |
200 | 1 | case DomainMCSPDU_ErectDomainRequest: |
201 | 1 | return "DomainMCSPDU_ErectDomainRequest"; |
202 | 0 | case DomainMCSPDU_MergeChannelsRequest: |
203 | 0 | return "DomainMCSPDU_MergeChannelsRequest"; |
204 | 3 | case DomainMCSPDU_MergeChannelsConfirm: |
205 | 3 | return "DomainMCSPDU_MergeChannelsConfirm"; |
206 | 0 | case DomainMCSPDU_PurgeChannelsIndication: |
207 | 0 | return "DomainMCSPDU_PurgeChannelsIndication"; |
208 | 0 | case DomainMCSPDU_MergeTokensRequest: |
209 | 0 | return "DomainMCSPDU_MergeTokensRequest"; |
210 | 1 | case DomainMCSPDU_MergeTokensConfirm: |
211 | 1 | return "DomainMCSPDU_MergeTokensConfirm"; |
212 | 0 | case DomainMCSPDU_PurgeTokensIndication: |
213 | 0 | return "DomainMCSPDU_PurgeTokensIndication"; |
214 | 0 | case DomainMCSPDU_DisconnectProviderUltimatum: |
215 | 0 | return "DomainMCSPDU_DisconnectProviderUltimatum"; |
216 | 0 | case DomainMCSPDU_RejectMCSPDUUltimatum: |
217 | 0 | return "DomainMCSPDU_RejectMCSPDUUltimatum"; |
218 | 0 | case DomainMCSPDU_AttachUserRequest: |
219 | 0 | return "DomainMCSPDU_AttachUserRequest"; |
220 | 0 | case DomainMCSPDU_AttachUserConfirm: |
221 | 0 | return "DomainMCSPDU_AttachUserConfirm"; |
222 | 0 | case DomainMCSPDU_DetachUserRequest: |
223 | 0 | return "DomainMCSPDU_DetachUserRequest"; |
224 | 1 | case DomainMCSPDU_DetachUserIndication: |
225 | 1 | return "DomainMCSPDU_DetachUserIndication"; |
226 | 0 | case DomainMCSPDU_ChannelJoinRequest: |
227 | 0 | return "DomainMCSPDU_ChannelJoinRequest"; |
228 | 1 | case DomainMCSPDU_ChannelJoinConfirm: |
229 | 1 | return "DomainMCSPDU_ChannelJoinConfirm"; |
230 | 1 | case DomainMCSPDU_ChannelLeaveRequest: |
231 | 1 | return "DomainMCSPDU_ChannelLeaveRequest"; |
232 | 0 | case DomainMCSPDU_ChannelConveneRequest: |
233 | 0 | return "DomainMCSPDU_ChannelConveneRequest"; |
234 | 0 | case DomainMCSPDU_ChannelConveneConfirm: |
235 | 0 | return "DomainMCSPDU_ChannelConveneConfirm"; |
236 | 1 | case DomainMCSPDU_ChannelDisbandRequest: |
237 | 1 | return "DomainMCSPDU_ChannelDisbandRequest"; |
238 | 0 | case DomainMCSPDU_ChannelDisbandIndication: |
239 | 0 | return "DomainMCSPDU_ChannelDisbandIndication"; |
240 | 0 | case DomainMCSPDU_ChannelAdmitRequest: |
241 | 0 | return "DomainMCSPDU_ChannelAdmitRequest"; |
242 | 0 | case DomainMCSPDU_ChannelAdmitIndication: |
243 | 0 | return "DomainMCSPDU_ChannelAdmitIndication"; |
244 | 2 | case DomainMCSPDU_ChannelExpelRequest: |
245 | 2 | return "DomainMCSPDU_ChannelExpelRequest"; |
246 | 0 | case DomainMCSPDU_ChannelExpelIndication: |
247 | 0 | return "DomainMCSPDU_ChannelExpelIndication"; |
248 | 24 | case DomainMCSPDU_SendDataRequest: |
249 | 24 | return "DomainMCSPDU_SendDataRequest"; |
250 | 28 | case DomainMCSPDU_SendDataIndication: |
251 | 28 | return "DomainMCSPDU_SendDataIndication"; |
252 | 1 | case DomainMCSPDU_UniformSendDataRequest: |
253 | 1 | return "DomainMCSPDU_UniformSendDataRequest"; |
254 | 0 | case DomainMCSPDU_UniformSendDataIndication: |
255 | 0 | return "DomainMCSPDU_UniformSendDataIndication"; |
256 | 0 | case DomainMCSPDU_TokenGrabRequest: |
257 | 0 | return "DomainMCSPDU_TokenGrabRequest"; |
258 | 0 | case DomainMCSPDU_TokenGrabConfirm: |
259 | 0 | return "DomainMCSPDU_TokenGrabConfirm"; |
260 | 10 | case DomainMCSPDU_TokenInhibitRequest: |
261 | 10 | return "DomainMCSPDU_TokenInhibitRequest"; |
262 | 0 | case DomainMCSPDU_TokenInhibitConfirm: |
263 | 0 | return "DomainMCSPDU_TokenInhibitConfirm"; |
264 | 0 | case DomainMCSPDU_TokenGiveRequest: |
265 | 0 | return "DomainMCSPDU_TokenGiveRequest"; |
266 | 0 | case DomainMCSPDU_TokenGiveIndication: |
267 | 0 | return "DomainMCSPDU_TokenGiveIndication"; |
268 | 0 | case DomainMCSPDU_TokenGiveResponse: |
269 | 0 | return "DomainMCSPDU_TokenGiveResponse"; |
270 | 0 | case DomainMCSPDU_TokenGiveConfirm: |
271 | 0 | return "DomainMCSPDU_TokenGiveConfirm"; |
272 | 2 | case DomainMCSPDU_TokenPleaseRequest: |
273 | 2 | return "DomainMCSPDU_TokenPleaseRequest"; |
274 | 1 | case DomainMCSPDU_TokenPleaseConfirm: |
275 | 1 | return "DomainMCSPDU_TokenPleaseConfirm"; |
276 | 0 | case DomainMCSPDU_TokenReleaseRequest: |
277 | 0 | return "DomainMCSPDU_TokenReleaseRequest"; |
278 | 0 | case DomainMCSPDU_TokenReleaseConfirm: |
279 | 0 | return "DomainMCSPDU_TokenReleaseConfirm"; |
280 | 0 | case DomainMCSPDU_TokenTestRequest: |
281 | 0 | return "DomainMCSPDU_TokenTestRequest"; |
282 | 1 | case DomainMCSPDU_TokenTestConfirm: |
283 | 1 | return "DomainMCSPDU_TokenTestConfirm"; |
284 | 0 | case DomainMCSPDU_enum_length: |
285 | 0 | return "DomainMCSPDU_enum_length"; |
286 | 17 | default: |
287 | 17 | return "DomainMCSPDU_UNKNOWN"; |
288 | 104 | } |
289 | 104 | } |
290 | | |
291 | | static BOOL mcs_merge_domain_parameters(wLog* log, DomainParameters* targetParameters, |
292 | | DomainParameters* minimumParameters, |
293 | | DomainParameters* maximumParameters, |
294 | | DomainParameters* pOutParameters); |
295 | | |
296 | | static BOOL mcs_write_connect_initial(wStream* s, rdpMcs* mcs, wStream* userData); |
297 | | static BOOL mcs_write_connect_response(wStream* s, rdpMcs* mcs, wStream* userData); |
298 | | static BOOL mcs_read_domain_mcspdu_header(wLog* log, wStream* s, DomainMCSPDU domainMCSPDU, |
299 | | UINT16* length, DomainMCSPDU* actual); |
300 | | |
301 | | static int mcs_initialize_client_channels(rdpMcs* mcs, const rdpSettings* settings) |
302 | 0 | { |
303 | 0 | if (!mcs || !settings) |
304 | 0 | return -1; |
305 | | |
306 | 0 | mcs->channelCount = freerdp_settings_get_uint32(settings, FreeRDP_ChannelCount); |
307 | |
|
308 | 0 | if (mcs->channelCount > mcs->channelMaxCount) |
309 | 0 | mcs->channelCount = mcs->channelMaxCount; |
310 | |
|
311 | 0 | ZeroMemory(mcs->channels, sizeof(rdpMcsChannel) * mcs->channelMaxCount); |
312 | |
|
313 | 0 | for (UINT32 index = 0; index < mcs->channelCount; index++) |
314 | 0 | { |
315 | 0 | const CHANNEL_DEF* defchannel = |
316 | 0 | freerdp_settings_get_pointer_array(settings, FreeRDP_ChannelDefArray, index); |
317 | 0 | rdpMcsChannel* cur = &mcs->channels[index]; |
318 | 0 | WINPR_ASSERT(defchannel); |
319 | 0 | CopyMemory(cur->Name, defchannel->name, CHANNEL_NAME_LEN); |
320 | 0 | cur->options = defchannel->options; |
321 | 0 | } |
322 | | |
323 | 0 | return 0; |
324 | 0 | } |
325 | | |
326 | | /** |
327 | | * Read a DomainMCSPDU header. |
328 | | * @param s stream |
329 | | * @param domainMCSPDU DomainMCSPDU type |
330 | | * @param length TPKT length |
331 | | * |
332 | | * @return \b TRUE for success, \b FALSE otherwise |
333 | | */ |
334 | | |
335 | | BOOL mcs_read_domain_mcspdu_header(wLog* log, wStream* s, DomainMCSPDU domainMCSPDU, UINT16* length, |
336 | | DomainMCSPDU* actual) |
337 | 0 | { |
338 | 0 | UINT16 li = 0; |
339 | 0 | BYTE choice = 0; |
340 | |
|
341 | 0 | if (actual) |
342 | 0 | *actual = DomainMCSPDU_invalid; |
343 | |
|
344 | 0 | WINPR_ASSERT(s); |
345 | 0 | WINPR_ASSERT(domainMCSPDU); |
346 | 0 | WINPR_ASSERT(length); |
347 | | |
348 | 0 | if (!tpkt_read_header(s, length)) |
349 | 0 | return FALSE; |
350 | | |
351 | 0 | if (!tpdu_read_data(s, &li, *length)) |
352 | 0 | return FALSE; |
353 | | |
354 | 0 | if (!per_read_choice(s, &choice)) |
355 | 0 | return FALSE; |
356 | | |
357 | 0 | const BYTE val = choice >> 2; |
358 | 0 | const DomainMCSPDU MCSPDU = |
359 | 0 | (val > DomainMCSPDU_enum_length) ? DomainMCSPDU_invalid : (DomainMCSPDU)(val); |
360 | 0 | if (actual) |
361 | 0 | *actual = MCSPDU; |
362 | |
|
363 | 0 | if (domainMCSPDU != MCSPDU) |
364 | 0 | { |
365 | 0 | WLog_Print(log, WLOG_ERROR, "Expected MCS %s, got %s", mcs_domain_pdu_string(domainMCSPDU), |
366 | 0 | mcs_domain_pdu_string(MCSPDU)); |
367 | 0 | return FALSE; |
368 | 0 | } |
369 | | |
370 | 0 | return TRUE; |
371 | 0 | } |
372 | | |
373 | | /** |
374 | | * Write a DomainMCSPDU header. |
375 | | * @param s stream |
376 | | * @param domainMCSPDU DomainMCSPDU type |
377 | | * @param length TPKT length |
378 | | */ |
379 | | |
380 | | BOOL mcs_write_domain_mcspdu_header(wStream* s, DomainMCSPDU domainMCSPDU, UINT16 length, |
381 | | BYTE options) |
382 | 2.65k | { |
383 | 2.65k | WINPR_ASSERT(s); |
384 | 2.65k | WINPR_ASSERT((options & ~0x03) == 0); |
385 | 2.65k | WINPR_ASSERT((domainMCSPDU & ~0x3F) == 0); |
386 | | |
387 | 2.65k | if (!tpkt_write_header(s, length)) |
388 | 0 | return FALSE; |
389 | 2.65k | if (!tpdu_write_data(s)) |
390 | 0 | return FALSE; |
391 | 2.65k | return per_write_choice(s, (BYTE)((domainMCSPDU << 2) | options)); |
392 | 2.65k | } |
393 | | |
394 | | /** |
395 | | * Initialize MCS Domain Parameters. |
396 | | * @param domainParameters domain parameters |
397 | | * @param maxChannelIds max channel ids |
398 | | * @param maxUserIds max user ids |
399 | | * @param maxTokenIds max token ids |
400 | | * @param maxMCSPDUsize max MCS PDU size |
401 | | */ |
402 | | |
403 | | static BOOL mcs_init_domain_parameters(DomainParameters* domainParameters, UINT32 maxChannelIds, |
404 | | UINT32 maxUserIds, UINT32 maxTokenIds, UINT32 maxMCSPDUsize) |
405 | 208k | { |
406 | 208k | if (!domainParameters) |
407 | 0 | return FALSE; |
408 | | |
409 | 208k | domainParameters->maxChannelIds = maxChannelIds; |
410 | 208k | domainParameters->maxUserIds = maxUserIds; |
411 | 208k | domainParameters->maxTokenIds = maxTokenIds; |
412 | 208k | domainParameters->maxMCSPDUsize = maxMCSPDUsize; |
413 | 208k | domainParameters->numPriorities = 1; |
414 | 208k | domainParameters->minThroughput = 0; |
415 | 208k | domainParameters->maxHeight = 1; |
416 | 208k | domainParameters->protocolVersion = 2; |
417 | 208k | return TRUE; |
418 | 208k | } |
419 | | |
420 | | /** |
421 | | * Read MCS Domain Parameters. |
422 | | * @param s stream |
423 | | * @param domainParameters domain parameters |
424 | | */ |
425 | | |
426 | | static BOOL mcs_read_domain_parameters(wStream* s, DomainParameters* domainParameters) |
427 | 5.82k | { |
428 | 5.82k | size_t length = 0; |
429 | | |
430 | 5.82k | if (!s || !domainParameters) |
431 | 0 | return FALSE; |
432 | | |
433 | 5.82k | return ber_read_sequence_tag(s, &length) && |
434 | 5.74k | ber_read_integer(s, &(domainParameters->maxChannelIds)) && |
435 | 5.71k | ber_read_integer(s, &(domainParameters->maxUserIds)) && |
436 | 5.69k | ber_read_integer(s, &(domainParameters->maxTokenIds)) && |
437 | 5.67k | ber_read_integer(s, &(domainParameters->numPriorities)) && |
438 | 5.62k | ber_read_integer(s, &(domainParameters->minThroughput)) && |
439 | 5.60k | ber_read_integer(s, &(domainParameters->maxHeight)) && |
440 | 5.58k | ber_read_integer(s, &(domainParameters->maxMCSPDUsize)) && |
441 | 5.56k | ber_read_integer(s, &(domainParameters->protocolVersion)); |
442 | 5.82k | } |
443 | | |
444 | | /** |
445 | | * Write MCS Domain Parameters. |
446 | | * @param s stream |
447 | | * @param domainParameters domain parameters |
448 | | */ |
449 | | |
450 | | static BOOL mcs_write_domain_parameters(wLog* log, wStream* s, DomainParameters* domainParameters) |
451 | 0 | { |
452 | 0 | size_t length = 0; |
453 | 0 | wStream* tmps = NULL; |
454 | |
|
455 | 0 | if (!s || !domainParameters) |
456 | 0 | return FALSE; |
457 | | |
458 | 0 | tmps = Stream_New(NULL, Stream_Capacity(s)); |
459 | |
|
460 | 0 | if (!tmps) |
461 | 0 | { |
462 | 0 | WLog_Print(log, WLOG_ERROR, "Stream_New failed!"); |
463 | 0 | return FALSE; |
464 | 0 | } |
465 | | |
466 | 0 | ber_write_integer(tmps, domainParameters->maxChannelIds); |
467 | 0 | ber_write_integer(tmps, domainParameters->maxUserIds); |
468 | 0 | ber_write_integer(tmps, domainParameters->maxTokenIds); |
469 | 0 | ber_write_integer(tmps, domainParameters->numPriorities); |
470 | 0 | ber_write_integer(tmps, domainParameters->minThroughput); |
471 | 0 | ber_write_integer(tmps, domainParameters->maxHeight); |
472 | 0 | ber_write_integer(tmps, domainParameters->maxMCSPDUsize); |
473 | 0 | ber_write_integer(tmps, domainParameters->protocolVersion); |
474 | 0 | length = Stream_GetPosition(tmps); |
475 | 0 | ber_write_sequence_tag(s, length); |
476 | 0 | Stream_Write(s, Stream_Buffer(tmps), length); |
477 | 0 | Stream_Free(tmps, TRUE); |
478 | 0 | return TRUE; |
479 | 0 | } |
480 | | |
481 | | #ifdef DEBUG_MCS |
482 | | /** |
483 | | * Print MCS Domain Parameters. |
484 | | * @param domainParameters domain parameters |
485 | | */ |
486 | | |
487 | | static void mcs_print_domain_parameters(DomainParameters* domainParameters) |
488 | | { |
489 | | WLog_INFO(TAG, "DomainParameters {"); |
490 | | |
491 | | if (domainParameters) |
492 | | { |
493 | | WLog_INFO(TAG, "\tmaxChannelIds:%" PRIu32 "", domainParameters->maxChannelIds); |
494 | | WLog_INFO(TAG, "\tmaxUserIds:%" PRIu32 "", domainParameters->maxUserIds); |
495 | | WLog_INFO(TAG, "\tmaxTokenIds:%" PRIu32 "", domainParameters->maxTokenIds); |
496 | | WLog_INFO(TAG, "\tnumPriorities:%" PRIu32 "", domainParameters->numPriorities); |
497 | | WLog_INFO(TAG, "\tminThroughput:%" PRIu32 "", domainParameters->minThroughput); |
498 | | WLog_INFO(TAG, "\tmaxHeight:%" PRIu32 "", domainParameters->maxHeight); |
499 | | WLog_INFO(TAG, "\tmaxMCSPDUsize:%" PRIu32 "", domainParameters->maxMCSPDUsize); |
500 | | WLog_INFO(TAG, "\tprotocolVersion:%" PRIu32 "", domainParameters->protocolVersion); |
501 | | } |
502 | | else |
503 | | WLog_INFO(TAG, "\tdomainParameters=%p", domainParameters); |
504 | | |
505 | | WLog_INFO(TAG, "}"); |
506 | | } |
507 | | #endif |
508 | | |
509 | | /** |
510 | | * Merge MCS Domain Parameters. |
511 | | * @param targetParameters target parameters |
512 | | * @param minimumParameters minimum parameters |
513 | | * @param maximumParameters maximum parameters |
514 | | * @param pOutParameters output parameters |
515 | | * |
516 | | * @return \b TRUE for success, \b FALSE otherwise |
517 | | */ |
518 | | |
519 | | BOOL mcs_merge_domain_parameters(wLog* log, DomainParameters* targetParameters, |
520 | | DomainParameters* minimumParameters, |
521 | | DomainParameters* maximumParameters, |
522 | | DomainParameters* pOutParameters) |
523 | 469 | { |
524 | | /* maxChannelIds */ |
525 | 469 | if (!targetParameters || !minimumParameters || !maximumParameters || !pOutParameters) |
526 | 0 | return FALSE; |
527 | | |
528 | 469 | if (targetParameters->maxChannelIds >= 4) |
529 | 415 | { |
530 | 415 | pOutParameters->maxChannelIds = targetParameters->maxChannelIds; |
531 | 415 | } |
532 | 54 | else if (maximumParameters->maxChannelIds >= 4) |
533 | 52 | { |
534 | 52 | pOutParameters->maxChannelIds = 4; |
535 | 52 | } |
536 | 2 | else |
537 | 2 | { |
538 | 2 | WLog_Print(log, WLOG_ERROR, "invalid maxChannelIds [%" PRIu32 ", %" PRIu32 "]", |
539 | 2 | targetParameters->maxChannelIds, maximumParameters->maxChannelIds); |
540 | 2 | return FALSE; |
541 | 2 | } |
542 | | |
543 | | /* maxUserIds */ |
544 | | |
545 | 467 | if (targetParameters->maxUserIds >= 3) |
546 | 442 | { |
547 | 442 | pOutParameters->maxUserIds = targetParameters->maxUserIds; |
548 | 442 | } |
549 | 25 | else if (maximumParameters->maxUserIds >= 3) |
550 | 22 | { |
551 | 22 | pOutParameters->maxUserIds = 3; |
552 | 22 | } |
553 | 3 | else |
554 | 3 | { |
555 | 3 | WLog_Print(log, WLOG_ERROR, "invalid maxUserIds [%" PRIu32 ", %" PRIu32 "]", |
556 | 3 | targetParameters->maxUserIds, maximumParameters->maxUserIds); |
557 | 3 | return FALSE; |
558 | 3 | } |
559 | | |
560 | | /* maxTokenIds */ |
561 | 464 | pOutParameters->maxTokenIds = targetParameters->maxTokenIds; |
562 | | |
563 | | /* numPriorities */ |
564 | | |
565 | 464 | if (minimumParameters->numPriorities <= 1) |
566 | 93 | { |
567 | 93 | pOutParameters->numPriorities = 1; |
568 | 93 | } |
569 | 371 | else |
570 | 371 | { |
571 | 371 | WLog_Print(log, WLOG_ERROR, "invalid numPriorities [%" PRIu32 "]", |
572 | 371 | maximumParameters->numPriorities); |
573 | 371 | return FALSE; |
574 | 371 | } |
575 | | |
576 | | /* minThroughput */ |
577 | 93 | pOutParameters->minThroughput = targetParameters->minThroughput; |
578 | | |
579 | | /* maxHeight */ |
580 | | |
581 | 93 | if ((targetParameters->maxHeight == 1) || (minimumParameters->maxHeight <= 1)) |
582 | 79 | { |
583 | 79 | pOutParameters->maxHeight = 1; |
584 | 79 | } |
585 | 14 | else |
586 | 14 | { |
587 | 14 | WLog_Print(log, WLOG_ERROR, "invalid maxHeight [%" PRIu32 ", %" PRIu32 "]", |
588 | 14 | targetParameters->maxHeight, minimumParameters->maxHeight); |
589 | 14 | return FALSE; |
590 | 14 | } |
591 | | |
592 | | /* maxMCSPDUsize */ |
593 | | |
594 | 79 | if (targetParameters->maxMCSPDUsize >= 1024) |
595 | 64 | { |
596 | 64 | if (targetParameters->maxMCSPDUsize <= 65528) |
597 | 17 | { |
598 | 17 | pOutParameters->maxMCSPDUsize = targetParameters->maxMCSPDUsize; |
599 | 17 | } |
600 | 47 | else if ((minimumParameters->maxMCSPDUsize >= 124) && |
601 | 46 | (minimumParameters->maxMCSPDUsize <= 65528)) |
602 | 1 | { |
603 | 1 | pOutParameters->maxMCSPDUsize = 65528; |
604 | 1 | } |
605 | 46 | else |
606 | 46 | { |
607 | 46 | WLog_Print(log, WLOG_ERROR, "invalid maxMCSPDUsize [%" PRIu32 ", %" PRIu32 "]", |
608 | 46 | targetParameters->maxMCSPDUsize, minimumParameters->maxMCSPDUsize); |
609 | 46 | return FALSE; |
610 | 46 | } |
611 | 64 | } |
612 | 15 | else |
613 | 15 | { |
614 | 15 | if (maximumParameters->maxMCSPDUsize >= 124) |
615 | 14 | { |
616 | 14 | pOutParameters->maxMCSPDUsize = maximumParameters->maxMCSPDUsize; |
617 | 14 | } |
618 | 1 | else |
619 | 1 | { |
620 | 1 | WLog_Print(log, WLOG_ERROR, "invalid maxMCSPDUsize [%" PRIu32 "]", |
621 | 1 | maximumParameters->maxMCSPDUsize); |
622 | 1 | return FALSE; |
623 | 1 | } |
624 | 15 | } |
625 | | |
626 | | /* protocolVersion */ |
627 | | |
628 | 32 | if ((targetParameters->protocolVersion == 2) || |
629 | 30 | ((minimumParameters->protocolVersion <= 2) && (maximumParameters->protocolVersion >= 2))) |
630 | 3 | { |
631 | 3 | pOutParameters->protocolVersion = 2; |
632 | 3 | } |
633 | 29 | else |
634 | 29 | { |
635 | 29 | WLog_Print(log, WLOG_ERROR, |
636 | 29 | "invalid protocolVersion [%" PRIu32 ", %" PRIu32 ", %" PRIu32 "]", |
637 | 29 | targetParameters->protocolVersion, minimumParameters->protocolVersion, |
638 | 29 | maximumParameters->protocolVersion); |
639 | 29 | return FALSE; |
640 | 29 | } |
641 | | |
642 | 3 | return TRUE; |
643 | 32 | } |
644 | | |
645 | | /** |
646 | | * Read an MCS Connect Initial PDU. |
647 | | * msdn{cc240508} |
648 | | * @param mcs MCS module |
649 | | * @param s stream |
650 | | */ |
651 | | |
652 | | BOOL mcs_recv_connect_initial(rdpMcs* mcs, wStream* s) |
653 | 17.3k | { |
654 | 17.3k | UINT16 li = 0; |
655 | 17.3k | size_t length = 0; |
656 | 17.3k | BOOL upwardFlag = FALSE; |
657 | 17.3k | UINT16 tlength = 0; |
658 | | |
659 | 17.3k | WINPR_ASSERT(mcs); |
660 | 17.3k | WINPR_ASSERT(s); |
661 | | |
662 | 17.3k | if (!tpkt_read_header(s, &tlength)) |
663 | 425 | return FALSE; |
664 | | |
665 | 16.9k | if (!tpdu_read_data(s, &li, tlength)) |
666 | 13.5k | return FALSE; |
667 | | |
668 | 3.37k | if (!ber_read_application_tag(s, MCS_TYPE_CONNECT_INITIAL, &length)) |
669 | 1.86k | return FALSE; |
670 | | |
671 | | /* callingDomainSelector (OCTET_STRING) */ |
672 | 1.50k | if (!ber_read_octet_string_tag(s, &length) || |
673 | 1.48k | (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, length))) |
674 | 26 | return FALSE; |
675 | | |
676 | 1.48k | Stream_Seek(s, length); |
677 | | |
678 | | /* calledDomainSelector (OCTET_STRING) */ |
679 | 1.48k | if (!ber_read_octet_string_tag(s, &length) || |
680 | 1.46k | (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, length))) |
681 | 21 | return FALSE; |
682 | | |
683 | 1.46k | Stream_Seek(s, length); |
684 | | |
685 | | /* upwardFlag (BOOLEAN) */ |
686 | 1.46k | if (!ber_read_BOOL(s, &upwardFlag)) |
687 | 29 | return FALSE; |
688 | | |
689 | | /* targetParameters (DomainParameters) */ |
690 | 1.43k | if (!mcs_read_domain_parameters(s, &mcs->targetParameters)) |
691 | 80 | return FALSE; |
692 | | |
693 | | /* minimumParameters (DomainParameters) */ |
694 | 1.35k | if (!mcs_read_domain_parameters(s, &mcs->minimumParameters)) |
695 | 43 | return FALSE; |
696 | | |
697 | | /* maximumParameters (DomainParameters) */ |
698 | 1.31k | if (!mcs_read_domain_parameters(s, &mcs->maximumParameters)) |
699 | 24 | return FALSE; |
700 | | |
701 | 1.28k | if (!ber_read_octet_string_tag(s, &length) || |
702 | 1.28k | (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, length))) |
703 | 5 | return FALSE; |
704 | | |
705 | 1.28k | if (!gcc_read_conference_create_request(s, mcs)) |
706 | 812 | return FALSE; |
707 | | |
708 | 469 | if (!mcs_merge_domain_parameters(mcs->log, &mcs->targetParameters, &mcs->minimumParameters, |
709 | 469 | &mcs->maximumParameters, &mcs->domainParameters)) |
710 | 466 | return FALSE; |
711 | | |
712 | 3 | return tpkt_ensure_stream_consumed(mcs->log, s, tlength); |
713 | 469 | } |
714 | | |
715 | | /** |
716 | | * Write an MCS Connect Initial PDU. |
717 | | * msdn{cc240508} |
718 | | * @param s stream |
719 | | * @param mcs MCS module |
720 | | * @param userData GCC Conference Create Request |
721 | | */ |
722 | | |
723 | | BOOL mcs_write_connect_initial(wStream* s, rdpMcs* mcs, wStream* userData) |
724 | 0 | { |
725 | 0 | size_t length = 0; |
726 | 0 | wStream* tmps = NULL; |
727 | 0 | BOOL ret = FALSE; |
728 | |
|
729 | 0 | if (!s || !mcs || !userData) |
730 | 0 | return FALSE; |
731 | | |
732 | 0 | tmps = Stream_New(NULL, Stream_Capacity(s)); |
733 | |
|
734 | 0 | if (!tmps) |
735 | 0 | { |
736 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
737 | 0 | return FALSE; |
738 | 0 | } |
739 | | |
740 | | /* callingDomainSelector (OCTET_STRING) */ |
741 | 0 | ber_write_octet_string(tmps, callingDomainSelector, sizeof(callingDomainSelector)); |
742 | | /* calledDomainSelector (OCTET_STRING) */ |
743 | 0 | ber_write_octet_string(tmps, calledDomainSelector, sizeof(calledDomainSelector)); |
744 | | /* upwardFlag (BOOLEAN) */ |
745 | 0 | ber_write_BOOL(tmps, TRUE); |
746 | | |
747 | | /* targetParameters (DomainParameters) */ |
748 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &mcs->targetParameters)) |
749 | 0 | goto out; |
750 | | |
751 | | /* minimumParameters (DomainParameters) */ |
752 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &mcs->minimumParameters)) |
753 | 0 | goto out; |
754 | | |
755 | | /* maximumParameters (DomainParameters) */ |
756 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &mcs->maximumParameters)) |
757 | 0 | goto out; |
758 | | |
759 | | /* userData (OCTET_STRING) */ |
760 | 0 | ber_write_octet_string(tmps, Stream_Buffer(userData), Stream_GetPosition(userData)); |
761 | 0 | length = Stream_GetPosition(tmps); |
762 | | /* Connect-Initial (APPLICATION 101, IMPLICIT SEQUENCE) */ |
763 | 0 | ber_write_application_tag(s, MCS_TYPE_CONNECT_INITIAL, length); |
764 | 0 | Stream_Write(s, Stream_Buffer(tmps), length); |
765 | 0 | ret = TRUE; |
766 | 0 | out: |
767 | 0 | Stream_Free(tmps, TRUE); |
768 | 0 | return ret; |
769 | 0 | } |
770 | | |
771 | | /** |
772 | | * Write an MCS Connect Response PDU. |
773 | | * msdn{cc240508} |
774 | | * @param s stream |
775 | | * @param mcs MCS module |
776 | | * @param userData GCC Conference Create Response |
777 | | * |
778 | | * @return \b TRUE for success, \b FALSE otherwise |
779 | | */ |
780 | | |
781 | | BOOL mcs_write_connect_response(wStream* s, rdpMcs* mcs, wStream* userData) |
782 | 0 | { |
783 | 0 | size_t length = 0; |
784 | 0 | wStream* tmps = NULL; |
785 | 0 | BOOL ret = FALSE; |
786 | |
|
787 | 0 | if (!s || !mcs || !userData) |
788 | 0 | return FALSE; |
789 | | |
790 | 0 | tmps = Stream_New(NULL, Stream_Capacity(s)); |
791 | |
|
792 | 0 | if (!tmps) |
793 | 0 | { |
794 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
795 | 0 | return FALSE; |
796 | 0 | } |
797 | | |
798 | 0 | ber_write_enumerated(tmps, 0, MCS_Result_enum_length); |
799 | 0 | ber_write_integer(tmps, 0); /* calledConnectId */ |
800 | |
|
801 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &(mcs->domainParameters))) |
802 | 0 | goto out; |
803 | | |
804 | | /* userData (OCTET_STRING) */ |
805 | 0 | ber_write_octet_string(tmps, Stream_Buffer(userData), Stream_GetPosition(userData)); |
806 | 0 | length = Stream_GetPosition(tmps); |
807 | 0 | ber_write_application_tag(s, MCS_TYPE_CONNECT_RESPONSE, length); |
808 | 0 | Stream_Write(s, Stream_Buffer(tmps), length); |
809 | 0 | ret = TRUE; |
810 | 0 | out: |
811 | 0 | Stream_Free(tmps, TRUE); |
812 | 0 | return ret; |
813 | 0 | } |
814 | | |
815 | | /** |
816 | | * Send MCS Connect Initial. |
817 | | * msdn{cc240508} |
818 | | * @param mcs mcs module |
819 | | */ |
820 | | |
821 | | static BOOL mcs_send_connect_initial(rdpMcs* mcs) |
822 | 0 | { |
823 | 0 | int status = -1; |
824 | 0 | size_t length = 0; |
825 | 0 | wStream* s = NULL; |
826 | 0 | size_t bm = 0; |
827 | 0 | size_t em = 0; |
828 | 0 | wStream* gcc_CCrq = NULL; |
829 | |
|
830 | 0 | if (!mcs || !mcs->context) |
831 | 0 | return FALSE; |
832 | | |
833 | 0 | mcs_initialize_client_channels(mcs, mcs->context->settings); |
834 | 0 | wStream* client_data = Stream_New(NULL, 512); |
835 | |
|
836 | 0 | if (!client_data) |
837 | 0 | { |
838 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
839 | 0 | return FALSE; |
840 | 0 | } |
841 | | |
842 | 0 | if (!gcc_write_client_data_blocks(client_data, mcs)) |
843 | 0 | goto out; |
844 | 0 | gcc_CCrq = Stream_New(NULL, 1024); |
845 | |
|
846 | 0 | if (!gcc_CCrq) |
847 | 0 | { |
848 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
849 | 0 | goto out; |
850 | 0 | } |
851 | | |
852 | 0 | if (!gcc_write_conference_create_request(gcc_CCrq, client_data)) |
853 | 0 | goto out; |
854 | 0 | length = Stream_GetPosition(gcc_CCrq) + 7; |
855 | 0 | s = Stream_New(NULL, 1024 + length); |
856 | |
|
857 | 0 | if (!s) |
858 | 0 | { |
859 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
860 | 0 | goto out; |
861 | 0 | } |
862 | | |
863 | 0 | bm = Stream_GetPosition(s); |
864 | 0 | Stream_Seek(s, 7); |
865 | |
|
866 | 0 | if (!mcs_write_connect_initial(s, mcs, gcc_CCrq)) |
867 | 0 | { |
868 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "mcs_write_connect_initial failed!"); |
869 | 0 | goto out; |
870 | 0 | } |
871 | | |
872 | 0 | em = Stream_GetPosition(s); |
873 | 0 | length = (em - bm); |
874 | 0 | if (length > UINT16_MAX) |
875 | 0 | goto out; |
876 | 0 | Stream_SetPosition(s, bm); |
877 | 0 | if (!tpkt_write_header(s, (UINT16)length)) |
878 | 0 | goto out; |
879 | 0 | if (!tpdu_write_data(s)) |
880 | 0 | goto out; |
881 | 0 | Stream_SetPosition(s, em); |
882 | 0 | Stream_SealLength(s); |
883 | |
|
884 | 0 | { |
885 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
886 | 0 | status = transport_write(transport, s); |
887 | 0 | } |
888 | |
|
889 | 0 | out: |
890 | 0 | Stream_Free(s, TRUE); |
891 | 0 | Stream_Free(gcc_CCrq, TRUE); |
892 | 0 | Stream_Free(client_data, TRUE); |
893 | 0 | return (status < 0 ? FALSE : TRUE); |
894 | 0 | } |
895 | | |
896 | | /** |
897 | | * Read MCS Connect Response. |
898 | | * msdn{cc240501} |
899 | | * @param mcs mcs module |
900 | | */ |
901 | | |
902 | | BOOL mcs_recv_connect_response(rdpMcs* mcs, wStream* s) |
903 | 17.3k | { |
904 | 17.3k | size_t length = 0; |
905 | 17.3k | UINT16 tlength = 0; |
906 | 17.3k | BYTE result = 0; |
907 | 17.3k | UINT16 li = 0; |
908 | 17.3k | UINT32 calledConnectId = 0; |
909 | | |
910 | 17.3k | if (!mcs || !s) |
911 | 0 | return FALSE; |
912 | | |
913 | 17.3k | if (!tpkt_read_header(s, &tlength)) |
914 | 425 | return FALSE; |
915 | | |
916 | 16.9k | if (!tpdu_read_data(s, &li, tlength)) |
917 | 13.5k | return FALSE; |
918 | | |
919 | 3.37k | if (!ber_read_application_tag(s, MCS_TYPE_CONNECT_RESPONSE, &length) || |
920 | 1.78k | !ber_read_enumerated(s, &result, MCS_Result_enum_length) || |
921 | 1.74k | !ber_read_integer(s, &calledConnectId) || |
922 | 1.72k | !mcs_read_domain_parameters(s, &(mcs->domainParameters)) || |
923 | 1.58k | !ber_read_octet_string_tag(s, &length)) |
924 | 1.79k | { |
925 | 1.79k | return FALSE; |
926 | 1.79k | } |
927 | | |
928 | 1.58k | if (!gcc_read_conference_create_response(s, mcs)) |
929 | 1.45k | { |
930 | 1.45k | WLog_Print(mcs->log, WLOG_ERROR, "gcc_read_conference_create_response failed"); |
931 | 1.45k | return FALSE; |
932 | 1.45k | } |
933 | | |
934 | 132 | return tpkt_ensure_stream_consumed(mcs->log, s, tlength); |
935 | 1.58k | } |
936 | | |
937 | | /** |
938 | | * Send MCS Connect Response. |
939 | | * msdn{cc240501} |
940 | | * @param mcs mcs module |
941 | | */ |
942 | | |
943 | | BOOL mcs_send_connect_response(rdpMcs* mcs) |
944 | 0 | { |
945 | 0 | size_t length = 0; |
946 | 0 | int status = -1; |
947 | 0 | wStream* s = NULL; |
948 | 0 | size_t bm = 0; |
949 | 0 | size_t em = 0; |
950 | 0 | wStream* gcc_CCrsp = NULL; |
951 | |
|
952 | 0 | if (!mcs) |
953 | 0 | return FALSE; |
954 | | |
955 | 0 | wStream* server_data = Stream_New(NULL, 512); |
956 | |
|
957 | 0 | if (!server_data) |
958 | 0 | { |
959 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
960 | 0 | return FALSE; |
961 | 0 | } |
962 | | |
963 | 0 | if (!gcc_write_server_data_blocks(server_data, mcs)) |
964 | 0 | goto out; |
965 | | |
966 | 0 | gcc_CCrsp = Stream_New(NULL, 512 + Stream_Capacity(server_data)); |
967 | |
|
968 | 0 | if (!gcc_CCrsp) |
969 | 0 | { |
970 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
971 | 0 | goto out; |
972 | 0 | } |
973 | | |
974 | 0 | if (!gcc_write_conference_create_response(gcc_CCrsp, server_data)) |
975 | 0 | goto out; |
976 | 0 | length = Stream_GetPosition(gcc_CCrsp) + 7; |
977 | 0 | s = Stream_New(NULL, length + 1024); |
978 | |
|
979 | 0 | if (!s) |
980 | 0 | { |
981 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
982 | 0 | goto out; |
983 | 0 | } |
984 | | |
985 | 0 | bm = Stream_GetPosition(s); |
986 | 0 | Stream_Seek(s, 7); |
987 | |
|
988 | 0 | if (!mcs_write_connect_response(s, mcs, gcc_CCrsp)) |
989 | 0 | goto out; |
990 | | |
991 | 0 | em = Stream_GetPosition(s); |
992 | 0 | length = (em - bm); |
993 | 0 | if (length > UINT16_MAX) |
994 | 0 | goto out; |
995 | 0 | Stream_SetPosition(s, bm); |
996 | 0 | if (!tpkt_write_header(s, (UINT16)length)) |
997 | 0 | goto out; |
998 | 0 | if (!tpdu_write_data(s)) |
999 | 0 | goto out; |
1000 | 0 | Stream_SetPosition(s, em); |
1001 | 0 | Stream_SealLength(s); |
1002 | |
|
1003 | 0 | { |
1004 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
1005 | 0 | status = transport_write(transport, s); |
1006 | 0 | } |
1007 | |
|
1008 | 0 | out: |
1009 | 0 | Stream_Free(s, TRUE); |
1010 | 0 | Stream_Free(gcc_CCrsp, TRUE); |
1011 | 0 | Stream_Free(server_data, TRUE); |
1012 | 0 | return (status < 0) ? FALSE : TRUE; |
1013 | 0 | } |
1014 | | |
1015 | | /** |
1016 | | * Read MCS Erect Domain Request. |
1017 | | * msdn{cc240523} |
1018 | | * @param mcs MCS module to use |
1019 | | * @param s stream |
1020 | | */ |
1021 | | |
1022 | | BOOL mcs_recv_erect_domain_request(rdpMcs* mcs, wStream* s) |
1023 | 0 | { |
1024 | 0 | UINT16 length = 0; |
1025 | 0 | UINT32 subHeight = 0; |
1026 | 0 | UINT32 subInterval = 0; |
1027 | |
|
1028 | 0 | WINPR_ASSERT(mcs); |
1029 | 0 | WINPR_ASSERT(s); |
1030 | | |
1031 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_ErectDomainRequest, &length, NULL)) |
1032 | 0 | return FALSE; |
1033 | | |
1034 | 0 | if (!per_read_integer(s, &subHeight)) /* subHeight (INTEGER) */ |
1035 | 0 | return FALSE; |
1036 | | |
1037 | 0 | if (!per_read_integer(s, &subInterval)) /* subInterval (INTEGER) */ |
1038 | 0 | return FALSE; |
1039 | | |
1040 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1041 | 0 | } |
1042 | | |
1043 | | /** |
1044 | | * Send MCS Erect Domain Request. |
1045 | | * msdn{cc240523} |
1046 | | * @param mcs MCS module to use |
1047 | | */ |
1048 | | |
1049 | | BOOL mcs_send_erect_domain_request(rdpMcs* mcs) |
1050 | 0 | { |
1051 | 0 | UINT16 length = 12; |
1052 | |
|
1053 | 0 | if (!mcs) |
1054 | 0 | return FALSE; |
1055 | | |
1056 | 0 | wStream* s = Stream_New(NULL, length); |
1057 | |
|
1058 | 0 | if (!s) |
1059 | 0 | { |
1060 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1061 | 0 | return FALSE; |
1062 | 0 | } |
1063 | | |
1064 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_ErectDomainRequest, length, 0); |
1065 | 0 | per_write_integer(s, 0); /* subHeight (INTEGER) */ |
1066 | 0 | per_write_integer(s, 0); /* subInterval (INTEGER) */ |
1067 | 0 | Stream_SealLength(s); |
1068 | |
|
1069 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
1070 | 0 | const int status = transport_write(transport, s); |
1071 | 0 | Stream_Free(s, TRUE); |
1072 | 0 | return (status < 0) ? FALSE : TRUE; |
1073 | 0 | } |
1074 | | |
1075 | | /** |
1076 | | * Read MCS Attach User Request. |
1077 | | * msdn{cc240524} |
1078 | | * @param mcs mcs module |
1079 | | * @param s stream |
1080 | | */ |
1081 | | |
1082 | | BOOL mcs_recv_attach_user_request(rdpMcs* mcs, wStream* s) |
1083 | 0 | { |
1084 | 0 | UINT16 length = 0; |
1085 | |
|
1086 | 0 | if (!mcs || !s) |
1087 | 0 | return FALSE; |
1088 | | |
1089 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_AttachUserRequest, &length, NULL)) |
1090 | 0 | return FALSE; |
1091 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1092 | 0 | } |
1093 | | |
1094 | | /** |
1095 | | * Send MCS Attach User Request. |
1096 | | * msdn{cc240524} |
1097 | | * @param mcs mcs module |
1098 | | */ |
1099 | | |
1100 | | BOOL mcs_send_attach_user_request(rdpMcs* mcs) |
1101 | 0 | { |
1102 | 0 | UINT16 length = 8; |
1103 | |
|
1104 | 0 | if (!mcs) |
1105 | 0 | return FALSE; |
1106 | | |
1107 | 0 | wStream* s = Stream_New(NULL, length); |
1108 | |
|
1109 | 0 | if (!s) |
1110 | 0 | { |
1111 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1112 | 0 | return FALSE; |
1113 | 0 | } |
1114 | | |
1115 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_AttachUserRequest, length, 0); |
1116 | 0 | Stream_SealLength(s); |
1117 | |
|
1118 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
1119 | 0 | const int status = transport_write(transport, s); |
1120 | 0 | Stream_Free(s, TRUE); |
1121 | 0 | return (status < 0) ? FALSE : TRUE; |
1122 | 0 | } |
1123 | | |
1124 | | /** |
1125 | | * Read MCS Attach User Confirm. |
1126 | | * msdn{cc240525} |
1127 | | * @param mcs mcs module |
1128 | | */ |
1129 | | |
1130 | | BOOL mcs_recv_attach_user_confirm(rdpMcs* mcs, wStream* s) |
1131 | 0 | { |
1132 | 0 | BYTE result = 0; |
1133 | 0 | UINT16 length = 0; |
1134 | |
|
1135 | 0 | if (!mcs || !s) |
1136 | 0 | return FALSE; |
1137 | | |
1138 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_AttachUserConfirm, &length, NULL)) |
1139 | 0 | return FALSE; |
1140 | 0 | if (!per_read_enumerated(s, &result, MCS_Result_enum_length)) /* result */ |
1141 | 0 | return FALSE; |
1142 | 0 | if (!per_read_integer16(s, &(mcs->userId), MCS_BASE_CHANNEL_ID)) /* initiator (UserId) */ |
1143 | 0 | return FALSE; |
1144 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1145 | 0 | } |
1146 | | |
1147 | | /** |
1148 | | * Send MCS Attach User Confirm. |
1149 | | * msdn{cc240525} |
1150 | | * @param mcs mcs module |
1151 | | */ |
1152 | | |
1153 | | BOOL mcs_send_attach_user_confirm(rdpMcs* mcs) |
1154 | 0 | { |
1155 | 0 | UINT16 length = 11; |
1156 | |
|
1157 | 0 | if (!mcs) |
1158 | 0 | return FALSE; |
1159 | | |
1160 | 0 | wStream* s = Stream_New(NULL, length); |
1161 | |
|
1162 | 0 | if (!s) |
1163 | 0 | { |
1164 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1165 | 0 | return FALSE; |
1166 | 0 | } |
1167 | | |
1168 | 0 | mcs->userId = mcs->baseChannelId++; |
1169 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_AttachUserConfirm, length, 2); |
1170 | 0 | per_write_enumerated(s, 0, MCS_Result_enum_length); /* result */ |
1171 | 0 | per_write_integer16(s, mcs->userId, MCS_BASE_CHANNEL_ID); /* initiator (UserId) */ |
1172 | 0 | Stream_SealLength(s); |
1173 | |
|
1174 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
1175 | 0 | const int status = transport_write(transport, s); |
1176 | 0 | Stream_Free(s, TRUE); |
1177 | 0 | return (status < 0) ? FALSE : TRUE; |
1178 | 0 | } |
1179 | | |
1180 | | /** |
1181 | | * Read MCS Channel Join Request. |
1182 | | * msdn{cc240526} |
1183 | | * @param mcs mcs module |
1184 | | * @param s stream |
1185 | | */ |
1186 | | |
1187 | | BOOL mcs_recv_channel_join_request(rdpMcs* mcs, const rdpSettings* settings, wStream* s, |
1188 | | UINT16* channelId) |
1189 | 0 | { |
1190 | 0 | UINT16 length = 0; |
1191 | 0 | UINT16 userId = 0; |
1192 | |
|
1193 | 0 | if (!mcs || !s || !channelId) |
1194 | 0 | return FALSE; |
1195 | | |
1196 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_ChannelJoinRequest, &length, NULL)) |
1197 | 0 | return FALSE; |
1198 | | |
1199 | 0 | if (!per_read_integer16(s, &userId, MCS_BASE_CHANNEL_ID)) |
1200 | 0 | return FALSE; |
1201 | 0 | if (userId != mcs->userId) |
1202 | 0 | { |
1203 | 0 | if (freerdp_settings_get_bool(settings, FreeRDP_TransportDumpReplay)) |
1204 | 0 | mcs->userId = userId; |
1205 | 0 | else |
1206 | 0 | return FALSE; |
1207 | 0 | } |
1208 | 0 | if (!per_read_integer16(s, channelId, 0)) |
1209 | 0 | return FALSE; |
1210 | | |
1211 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1212 | 0 | } |
1213 | | |
1214 | | /** |
1215 | | * Send MCS Channel Join Request. |
1216 | | * msdn{cc240526} |
1217 | | * |
1218 | | * @param mcs mcs module |
1219 | | * @param channelId channel id |
1220 | | * |
1221 | | * @return \b TRUE for success, \b FALSE otherwise |
1222 | | */ |
1223 | | |
1224 | | BOOL mcs_send_channel_join_request(rdpMcs* mcs, UINT16 channelId) |
1225 | 0 | { |
1226 | 0 | UINT16 length = 12; |
1227 | |
|
1228 | 0 | WINPR_ASSERT(mcs); |
1229 | | |
1230 | 0 | wStream* s = Stream_New(NULL, length); |
1231 | |
|
1232 | 0 | if (!s) |
1233 | 0 | { |
1234 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1235 | 0 | return FALSE; |
1236 | 0 | } |
1237 | | |
1238 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_ChannelJoinRequest, length, 0); |
1239 | 0 | per_write_integer16(s, mcs->userId, MCS_BASE_CHANNEL_ID); |
1240 | 0 | per_write_integer16(s, channelId, 0); |
1241 | 0 | Stream_SealLength(s); |
1242 | |
|
1243 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
1244 | 0 | const int status = transport_write(transport, s); |
1245 | 0 | Stream_Free(s, TRUE); |
1246 | 0 | return (status < 0) ? FALSE : TRUE; |
1247 | 0 | } |
1248 | | |
1249 | | /** |
1250 | | * Read MCS Channel Join Confirm. |
1251 | | * msdn{cc240527} |
1252 | | * @param mcs mcs module |
1253 | | */ |
1254 | | |
1255 | | BOOL mcs_recv_channel_join_confirm(rdpMcs* mcs, wStream* s, UINT16* channelId) |
1256 | 0 | { |
1257 | 0 | UINT16 length = 0; |
1258 | 0 | BYTE result = 0; |
1259 | 0 | UINT16 initiator = 0; |
1260 | 0 | UINT16 requested = 0; |
1261 | |
|
1262 | 0 | WINPR_ASSERT(mcs); |
1263 | 0 | WINPR_ASSERT(channelId); |
1264 | | |
1265 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_ChannelJoinConfirm, &length, NULL)) |
1266 | 0 | return FALSE; |
1267 | | |
1268 | 0 | if (!per_read_enumerated(s, &result, MCS_Result_enum_length)) /* result */ |
1269 | 0 | return FALSE; |
1270 | 0 | if (!per_read_integer16(s, &initiator, MCS_BASE_CHANNEL_ID)) /* initiator (UserId) */ |
1271 | 0 | return FALSE; |
1272 | 0 | if (!per_read_integer16(s, &requested, 0)) /* requested (ChannelId) */ |
1273 | 0 | return FALSE; |
1274 | 0 | if (!per_read_integer16(s, channelId, 0)) /* channelId */ |
1275 | 0 | return FALSE; |
1276 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1277 | 0 | } |
1278 | | |
1279 | | /** |
1280 | | * Send MCS Channel Join Confirm. |
1281 | | * msdn{cc240527} |
1282 | | * @param mcs mcs module |
1283 | | */ |
1284 | | |
1285 | | BOOL mcs_send_channel_join_confirm(rdpMcs* mcs, UINT16 channelId) |
1286 | 0 | { |
1287 | 0 | int status = -1; |
1288 | 0 | UINT16 length = 15; |
1289 | |
|
1290 | 0 | if (!mcs) |
1291 | 0 | return FALSE; |
1292 | | |
1293 | 0 | wStream* s = Stream_New(NULL, length); |
1294 | |
|
1295 | 0 | if (!s) |
1296 | 0 | { |
1297 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1298 | 0 | return FALSE; |
1299 | 0 | } |
1300 | | |
1301 | 0 | if (!mcs_write_domain_mcspdu_header(s, DomainMCSPDU_ChannelJoinConfirm, length, 2)) |
1302 | 0 | goto fail; |
1303 | 0 | if (!per_write_enumerated(s, 0, MCS_Result_enum_length)) /* result */ |
1304 | 0 | goto fail; |
1305 | 0 | if (!per_write_integer16(s, mcs->userId, MCS_BASE_CHANNEL_ID)) /* initiator (UserId) */ |
1306 | 0 | goto fail; |
1307 | 0 | if (!per_write_integer16(s, channelId, 0)) /* requested (ChannelId) */ |
1308 | 0 | goto fail; |
1309 | 0 | if (!per_write_integer16(s, channelId, 0)) /* channelId */ |
1310 | 0 | goto fail; |
1311 | 0 | Stream_SealLength(s); |
1312 | |
|
1313 | 0 | { |
1314 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
1315 | 0 | status = transport_write(transport, s); |
1316 | 0 | } |
1317 | |
|
1318 | 0 | fail: |
1319 | 0 | Stream_Free(s, TRUE); |
1320 | 0 | return (status < 0) ? FALSE : TRUE; |
1321 | 0 | } |
1322 | | |
1323 | | /** |
1324 | | * Receive MCS Disconnect Provider Ultimatum PDU. |
1325 | | * @param mcs mcs module |
1326 | | */ |
1327 | | |
1328 | | BOOL mcs_recv_disconnect_provider_ultimatum(WINPR_ATTR_UNUSED rdpMcs* mcs, wStream* s, int* reason) |
1329 | 802 | { |
1330 | 802 | BYTE b1 = 0; |
1331 | 802 | BYTE b2 = 0; |
1332 | | |
1333 | 802 | WINPR_ASSERT(mcs); |
1334 | 802 | WINPR_ASSERT(s); |
1335 | 802 | WINPR_ASSERT(reason); |
1336 | | |
1337 | | /* |
1338 | | * http://msdn.microsoft.com/en-us/library/cc240872.aspx: |
1339 | | * |
1340 | | * PER encoded (ALIGNED variant of BASIC-PER) PDU contents: |
1341 | | * 21 80 |
1342 | | * |
1343 | | * 0x21: |
1344 | | * 0 - --\ |
1345 | | * 0 - | |
1346 | | * 1 - | CHOICE: From DomainMCSPDU select disconnectProviderUltimatum (8) |
1347 | | * 0 - | of type DisconnectProviderUltimatum |
1348 | | * 0 - | |
1349 | | * 0 - --/ |
1350 | | * 0 - --\ |
1351 | | * 1 - | |
1352 | | * | DisconnectProviderUltimatum::reason = rn-user-requested (3) |
1353 | | * 0x80: | |
1354 | | * 1 - --/ |
1355 | | * 0 - padding |
1356 | | * 0 - padding |
1357 | | * 0 - padding |
1358 | | * 0 - padding |
1359 | | * 0 - padding |
1360 | | * 0 - padding |
1361 | | * 0 - padding |
1362 | | */ |
1363 | | |
1364 | 802 | if (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, 1)) |
1365 | 2 | return FALSE; |
1366 | | |
1367 | 800 | Stream_Rewind_UINT8(s); |
1368 | 800 | Stream_Read_UINT8(s, b1); |
1369 | 800 | Stream_Read_UINT8(s, b2); |
1370 | 800 | *reason = ((b1 & 0x01) << 1) | (b2 >> 7); |
1371 | 800 | return TRUE; |
1372 | 802 | } |
1373 | | |
1374 | | /** |
1375 | | * Send MCS Disconnect Provider Ultimatum PDU. |
1376 | | * @param mcs mcs module |
1377 | | */ |
1378 | | |
1379 | | BOOL mcs_send_disconnect_provider_ultimatum(rdpMcs* mcs, enum Disconnect_Ultimatum reason) |
1380 | 0 | { |
1381 | 0 | int status = -1; |
1382 | 0 | UINT16 length = 9; |
1383 | |
|
1384 | 0 | WINPR_ASSERT(mcs); |
1385 | | |
1386 | 0 | wStream* s = Stream_New(NULL, length); |
1387 | |
|
1388 | 0 | if (!s) |
1389 | 0 | goto fail; |
1390 | | |
1391 | 0 | if (!mcs_write_domain_mcspdu_header(s, DomainMCSPDU_DisconnectProviderUltimatum, length, 1)) |
1392 | 0 | goto fail; |
1393 | | |
1394 | 0 | if (!per_write_enumerated(s, 0x80, WINPR_ASSERTING_INT_CAST(BYTE, reason))) |
1395 | 0 | goto fail; |
1396 | | |
1397 | 0 | { |
1398 | 0 | rdpTransport* transport = freerdp_get_transport(mcs->context); |
1399 | 0 | status = transport_write(transport, s); |
1400 | 0 | } |
1401 | |
|
1402 | 0 | fail: |
1403 | 0 | WLog_Print(mcs->log, WLOG_DEBUG, "sending DisconnectProviderUltimatum(%s)", |
1404 | 0 | freerdp_disconnect_reason_string((int)reason)); |
1405 | 0 | Stream_Free(s, TRUE); |
1406 | 0 | return (status < 0) ? FALSE : TRUE; |
1407 | 0 | } |
1408 | | |
1409 | | BOOL mcs_client_begin(rdpMcs* mcs) |
1410 | 0 | { |
1411 | 0 | if (!mcs || !mcs->context) |
1412 | 0 | return FALSE; |
1413 | | |
1414 | | /* First transition state, we need this to trigger session recording */ |
1415 | 0 | if (!mcs_send_connect_initial(mcs)) |
1416 | 0 | { |
1417 | 0 | freerdp_set_last_error_if_not(mcs->context, FREERDP_ERROR_MCS_CONNECT_INITIAL_ERROR); |
1418 | |
|
1419 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Error: unable to send MCS Connect Initial"); |
1420 | 0 | return FALSE; |
1421 | 0 | } |
1422 | | |
1423 | 0 | return TRUE; |
1424 | 0 | } |
1425 | | |
1426 | | /** |
1427 | | * Instantiate new MCS module. |
1428 | | * @param context rdpContext to use |
1429 | | * @return new MCS module |
1430 | | */ |
1431 | | |
1432 | | rdpMcs* mcs_new(rdpContext* context) |
1433 | 52.0k | { |
1434 | 52.0k | rdpMcs* mcs = (rdpMcs*)calloc(1, sizeof(rdpMcs)); |
1435 | | |
1436 | 52.0k | if (!mcs) |
1437 | 0 | return NULL; |
1438 | 52.0k | mcs->log = WLog_Get(MCS_TAG); |
1439 | 52.0k | WINPR_ASSERT(mcs->log); |
1440 | | |
1441 | 52.0k | mcs->context = context; |
1442 | 52.0k | mcs_init_domain_parameters(&mcs->targetParameters, 34, 2, 0, 0xFFFF); |
1443 | 52.0k | mcs_init_domain_parameters(&mcs->minimumParameters, 1, 1, 1, 0x420); |
1444 | 52.0k | mcs_init_domain_parameters(&mcs->maximumParameters, 0xFFFF, 0xFC17, 0xFFFF, 0xFFFF); |
1445 | 52.0k | mcs_init_domain_parameters(&mcs->domainParameters, 0, 0, 0, 0xFFFF); |
1446 | 52.0k | mcs->channelCount = 0; |
1447 | 52.0k | mcs->channelMaxCount = CHANNEL_MAX_COUNT; |
1448 | 52.0k | mcs->baseChannelId = MCS_GLOBAL_CHANNEL_ID + 1; |
1449 | 52.0k | mcs->channels = (rdpMcsChannel*)calloc(mcs->channelMaxCount, sizeof(rdpMcsChannel)); |
1450 | | |
1451 | 52.0k | if (!mcs->channels) |
1452 | 0 | goto out_free; |
1453 | | |
1454 | 52.0k | return mcs; |
1455 | 0 | out_free: |
1456 | 0 | free(mcs); |
1457 | 0 | return NULL; |
1458 | 52.0k | } |
1459 | | |
1460 | | /** |
1461 | | * Free MCS module. |
1462 | | * @param mcs MCS module to be freed |
1463 | | */ |
1464 | | |
1465 | | void mcs_free(rdpMcs* mcs) |
1466 | 52.0k | { |
1467 | 52.0k | if (mcs) |
1468 | 52.0k | { |
1469 | 52.0k | free(mcs->channels); |
1470 | 52.0k | free(mcs); |
1471 | 52.0k | } |
1472 | 52.0k | } |
1473 | | |
1474 | | BOOL mcs_server_apply_to_settings(const rdpMcs* mcs, rdpSettings* settings) |
1475 | 0 | { |
1476 | 0 | BOOL rc = FALSE; |
1477 | |
|
1478 | 0 | WINPR_ASSERT(mcs); |
1479 | 0 | WINPR_ASSERT(settings); |
1480 | | |
1481 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_ChannelCount, mcs->channelCount)) |
1482 | 0 | goto fail; |
1483 | | |
1484 | 0 | for (UINT32 x = 0; x < mcs->channelCount; x++) |
1485 | 0 | { |
1486 | 0 | const rdpMcsChannel* current = &mcs->channels[x]; |
1487 | 0 | CHANNEL_DEF def = { 0 }; |
1488 | 0 | def.options = current->options; |
1489 | 0 | memcpy(def.name, current->Name, sizeof(def.name)); |
1490 | 0 | if (!freerdp_settings_set_pointer_array(settings, FreeRDP_ChannelDefArray, x, &def)) |
1491 | 0 | goto fail; |
1492 | 0 | } |
1493 | | |
1494 | 0 | rc = TRUE; |
1495 | 0 | fail: |
1496 | 0 | if (!rc) |
1497 | 0 | WLog_Print(mcs->log, WLOG_WARN, "failed to apply settings"); |
1498 | |
|
1499 | 0 | return rc; |
1500 | 0 | } |