/src/FreeRDP/libfreerdp/core/mcs.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | #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] = "\x01"; |
170 | | static const BYTE calledDomainSelector[1] = "\x01"; |
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 | 0 | { |
196 | 0 | switch (pdu) |
197 | 0 | { |
198 | 0 | case DomainMCSPDU_PlumbDomainIndication: |
199 | 0 | return "DomainMCSPDU_PlumbDomainIndication"; |
200 | 0 | case DomainMCSPDU_ErectDomainRequest: |
201 | 0 | return "DomainMCSPDU_ErectDomainRequest"; |
202 | 0 | case DomainMCSPDU_MergeChannelsRequest: |
203 | 0 | return "DomainMCSPDU_MergeChannelsRequest"; |
204 | 0 | case DomainMCSPDU_MergeChannelsConfirm: |
205 | 0 | return "DomainMCSPDU_MergeChannelsConfirm"; |
206 | 0 | case DomainMCSPDU_PurgeChannelsIndication: |
207 | 0 | return "DomainMCSPDU_PurgeChannelsIndication"; |
208 | 0 | case DomainMCSPDU_MergeTokensRequest: |
209 | 0 | return "DomainMCSPDU_MergeTokensRequest"; |
210 | 0 | case DomainMCSPDU_MergeTokensConfirm: |
211 | 0 | 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 | 0 | case DomainMCSPDU_DetachUserIndication: |
225 | 0 | return "DomainMCSPDU_DetachUserIndication"; |
226 | 0 | case DomainMCSPDU_ChannelJoinRequest: |
227 | 0 | return "DomainMCSPDU_ChannelJoinRequest"; |
228 | 0 | case DomainMCSPDU_ChannelJoinConfirm: |
229 | 0 | return "DomainMCSPDU_ChannelJoinConfirm"; |
230 | 0 | case DomainMCSPDU_ChannelLeaveRequest: |
231 | 0 | return "DomainMCSPDU_ChannelLeaveRequest"; |
232 | 0 | case DomainMCSPDU_ChannelConveneRequest: |
233 | 0 | return "DomainMCSPDU_ChannelConveneRequest"; |
234 | 0 | case DomainMCSPDU_ChannelConveneConfirm: |
235 | 0 | return "DomainMCSPDU_ChannelConveneConfirm"; |
236 | 0 | case DomainMCSPDU_ChannelDisbandRequest: |
237 | 0 | 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 | 0 | case DomainMCSPDU_ChannelExpelRequest: |
245 | 0 | return "DomainMCSPDU_ChannelExpelRequest"; |
246 | 0 | case DomainMCSPDU_ChannelExpelIndication: |
247 | 0 | return "DomainMCSPDU_ChannelExpelIndication"; |
248 | 0 | case DomainMCSPDU_SendDataRequest: |
249 | 0 | return "DomainMCSPDU_SendDataRequest"; |
250 | 0 | case DomainMCSPDU_SendDataIndication: |
251 | 0 | return "DomainMCSPDU_SendDataIndication"; |
252 | 0 | case DomainMCSPDU_UniformSendDataRequest: |
253 | 0 | 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 | 0 | case DomainMCSPDU_TokenInhibitRequest: |
261 | 0 | 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 | 0 | case DomainMCSPDU_TokenPleaseRequest: |
273 | 0 | return "DomainMCSPDU_TokenPleaseRequest"; |
274 | 0 | case DomainMCSPDU_TokenPleaseConfirm: |
275 | 0 | 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 | 0 | case DomainMCSPDU_TokenTestConfirm: |
283 | 0 | return "DomainMCSPDU_TokenTestConfirm"; |
284 | 0 | case DomainMCSPDU_enum_length: |
285 | 0 | return "DomainMCSPDU_enum_length"; |
286 | 0 | default: |
287 | 0 | return "DomainMCSPDU_UNKNOWN"; |
288 | 0 | } |
289 | 0 | } |
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 DomainMCSPDU MCSPDU = (choice >> 2); |
358 | 0 | if (actual) |
359 | 0 | *actual = MCSPDU; |
360 | |
|
361 | 0 | if (domainMCSPDU != MCSPDU) |
362 | 0 | { |
363 | 0 | WLog_Print(log, WLOG_ERROR, "Expected MCS %s, got %s", mcs_domain_pdu_string(domainMCSPDU), |
364 | 0 | mcs_domain_pdu_string(MCSPDU)); |
365 | 0 | return FALSE; |
366 | 0 | } |
367 | | |
368 | 0 | return TRUE; |
369 | 0 | } |
370 | | |
371 | | /** |
372 | | * Write a DomainMCSPDU header. |
373 | | * @param s stream |
374 | | * @param domainMCSPDU DomainMCSPDU type |
375 | | * @param length TPKT length |
376 | | */ |
377 | | |
378 | | BOOL mcs_write_domain_mcspdu_header(wStream* s, DomainMCSPDU domainMCSPDU, UINT16 length, |
379 | | BYTE options) |
380 | 0 | { |
381 | 0 | WINPR_ASSERT(s); |
382 | 0 | WINPR_ASSERT((options & ~0x03) == 0); |
383 | 0 | WINPR_ASSERT((domainMCSPDU & ~0x3F) == 0); |
384 | | |
385 | 0 | if (!tpkt_write_header(s, length)) |
386 | 0 | return FALSE; |
387 | 0 | if (!tpdu_write_data(s)) |
388 | 0 | return FALSE; |
389 | 0 | return per_write_choice(s, (BYTE)((domainMCSPDU << 2) | options)); |
390 | 0 | } |
391 | | |
392 | | /** |
393 | | * Initialize MCS Domain Parameters. |
394 | | * @param domainParameters domain parameters |
395 | | * @param maxChannelIds max channel ids |
396 | | * @param maxUserIds max user ids |
397 | | * @param maxTokenIds max token ids |
398 | | * @param maxMCSPDUsize max MCS PDU size |
399 | | */ |
400 | | |
401 | | static BOOL mcs_init_domain_parameters(DomainParameters* domainParameters, UINT32 maxChannelIds, |
402 | | UINT32 maxUserIds, UINT32 maxTokenIds, UINT32 maxMCSPDUsize) |
403 | 0 | { |
404 | 0 | if (!domainParameters) |
405 | 0 | return FALSE; |
406 | | |
407 | 0 | domainParameters->maxChannelIds = maxChannelIds; |
408 | 0 | domainParameters->maxUserIds = maxUserIds; |
409 | 0 | domainParameters->maxTokenIds = maxTokenIds; |
410 | 0 | domainParameters->maxMCSPDUsize = maxMCSPDUsize; |
411 | 0 | domainParameters->numPriorities = 1; |
412 | 0 | domainParameters->minThroughput = 0; |
413 | 0 | domainParameters->maxHeight = 1; |
414 | 0 | domainParameters->protocolVersion = 2; |
415 | 0 | return TRUE; |
416 | 0 | } |
417 | | |
418 | | /** |
419 | | * Read MCS Domain Parameters. |
420 | | * @param s stream |
421 | | * @param domainParameters domain parameters |
422 | | */ |
423 | | |
424 | | static BOOL mcs_read_domain_parameters(wStream* s, DomainParameters* domainParameters) |
425 | 0 | { |
426 | 0 | size_t length = 0; |
427 | |
|
428 | 0 | if (!s || !domainParameters) |
429 | 0 | return FALSE; |
430 | | |
431 | 0 | return ber_read_sequence_tag(s, &length) && |
432 | 0 | ber_read_integer(s, &(domainParameters->maxChannelIds)) && |
433 | 0 | ber_read_integer(s, &(domainParameters->maxUserIds)) && |
434 | 0 | ber_read_integer(s, &(domainParameters->maxTokenIds)) && |
435 | 0 | ber_read_integer(s, &(domainParameters->numPriorities)) && |
436 | 0 | ber_read_integer(s, &(domainParameters->minThroughput)) && |
437 | 0 | ber_read_integer(s, &(domainParameters->maxHeight)) && |
438 | 0 | ber_read_integer(s, &(domainParameters->maxMCSPDUsize)) && |
439 | 0 | ber_read_integer(s, &(domainParameters->protocolVersion)); |
440 | 0 | } |
441 | | |
442 | | /** |
443 | | * Write MCS Domain Parameters. |
444 | | * @param s stream |
445 | | * @param domainParameters domain parameters |
446 | | */ |
447 | | |
448 | | static BOOL mcs_write_domain_parameters(wLog* log, wStream* s, DomainParameters* domainParameters) |
449 | 0 | { |
450 | 0 | size_t length = 0; |
451 | 0 | wStream* tmps = NULL; |
452 | |
|
453 | 0 | if (!s || !domainParameters) |
454 | 0 | return FALSE; |
455 | | |
456 | 0 | tmps = Stream_New(NULL, Stream_Capacity(s)); |
457 | |
|
458 | 0 | if (!tmps) |
459 | 0 | { |
460 | 0 | WLog_Print(log, WLOG_ERROR, "Stream_New failed!"); |
461 | 0 | return FALSE; |
462 | 0 | } |
463 | | |
464 | 0 | ber_write_integer(tmps, domainParameters->maxChannelIds); |
465 | 0 | ber_write_integer(tmps, domainParameters->maxUserIds); |
466 | 0 | ber_write_integer(tmps, domainParameters->maxTokenIds); |
467 | 0 | ber_write_integer(tmps, domainParameters->numPriorities); |
468 | 0 | ber_write_integer(tmps, domainParameters->minThroughput); |
469 | 0 | ber_write_integer(tmps, domainParameters->maxHeight); |
470 | 0 | ber_write_integer(tmps, domainParameters->maxMCSPDUsize); |
471 | 0 | ber_write_integer(tmps, domainParameters->protocolVersion); |
472 | 0 | length = Stream_GetPosition(tmps); |
473 | 0 | ber_write_sequence_tag(s, length); |
474 | 0 | Stream_Write(s, Stream_Buffer(tmps), length); |
475 | 0 | Stream_Free(tmps, TRUE); |
476 | 0 | return TRUE; |
477 | 0 | } |
478 | | |
479 | | #ifdef DEBUG_MCS |
480 | | /** |
481 | | * Print MCS Domain Parameters. |
482 | | * @param domainParameters domain parameters |
483 | | */ |
484 | | |
485 | | static void mcs_print_domain_parameters(DomainParameters* domainParameters) |
486 | | { |
487 | | WLog_INFO(TAG, "DomainParameters {"); |
488 | | |
489 | | if (domainParameters) |
490 | | { |
491 | | WLog_INFO(TAG, "\tmaxChannelIds:%" PRIu32 "", domainParameters->maxChannelIds); |
492 | | WLog_INFO(TAG, "\tmaxUserIds:%" PRIu32 "", domainParameters->maxUserIds); |
493 | | WLog_INFO(TAG, "\tmaxTokenIds:%" PRIu32 "", domainParameters->maxTokenIds); |
494 | | WLog_INFO(TAG, "\tnumPriorities:%" PRIu32 "", domainParameters->numPriorities); |
495 | | WLog_INFO(TAG, "\tminThroughput:%" PRIu32 "", domainParameters->minThroughput); |
496 | | WLog_INFO(TAG, "\tmaxHeight:%" PRIu32 "", domainParameters->maxHeight); |
497 | | WLog_INFO(TAG, "\tmaxMCSPDUsize:%" PRIu32 "", domainParameters->maxMCSPDUsize); |
498 | | WLog_INFO(TAG, "\tprotocolVersion:%" PRIu32 "", domainParameters->protocolVersion); |
499 | | } |
500 | | else |
501 | | WLog_INFO(TAG, "\tdomainParameters=%p", domainParameters); |
502 | | |
503 | | WLog_INFO(TAG, "}"); |
504 | | } |
505 | | #endif |
506 | | |
507 | | /** |
508 | | * Merge MCS Domain Parameters. |
509 | | * @param targetParameters target parameters |
510 | | * @param minimumParameters minimum parameters |
511 | | * @param maximumParameters maximum parameters |
512 | | * @param pOutParameters output parameters |
513 | | * |
514 | | * @return \b TRUE for success, \b FALSE otherwise |
515 | | */ |
516 | | |
517 | | BOOL mcs_merge_domain_parameters(wLog* log, DomainParameters* targetParameters, |
518 | | DomainParameters* minimumParameters, |
519 | | DomainParameters* maximumParameters, |
520 | | DomainParameters* pOutParameters) |
521 | 0 | { |
522 | | /* maxChannelIds */ |
523 | 0 | if (!targetParameters || !minimumParameters || !maximumParameters || !pOutParameters) |
524 | 0 | return FALSE; |
525 | | |
526 | 0 | if (targetParameters->maxChannelIds >= 4) |
527 | 0 | { |
528 | 0 | pOutParameters->maxChannelIds = targetParameters->maxChannelIds; |
529 | 0 | } |
530 | 0 | else if (maximumParameters->maxChannelIds >= 4) |
531 | 0 | { |
532 | 0 | pOutParameters->maxChannelIds = 4; |
533 | 0 | } |
534 | 0 | else |
535 | 0 | { |
536 | 0 | WLog_Print(log, WLOG_ERROR, "invalid maxChannelIds [%" PRIu32 ", %" PRIu32 "]", |
537 | 0 | targetParameters->maxChannelIds, maximumParameters->maxChannelIds); |
538 | 0 | return FALSE; |
539 | 0 | } |
540 | | |
541 | | /* maxUserIds */ |
542 | | |
543 | 0 | if (targetParameters->maxUserIds >= 3) |
544 | 0 | { |
545 | 0 | pOutParameters->maxUserIds = targetParameters->maxUserIds; |
546 | 0 | } |
547 | 0 | else if (maximumParameters->maxUserIds >= 3) |
548 | 0 | { |
549 | 0 | pOutParameters->maxUserIds = 3; |
550 | 0 | } |
551 | 0 | else |
552 | 0 | { |
553 | 0 | WLog_Print(log, WLOG_ERROR, "invalid maxUserIds [%" PRIu32 ", %" PRIu32 "]", |
554 | 0 | targetParameters->maxUserIds, maximumParameters->maxUserIds); |
555 | 0 | return FALSE; |
556 | 0 | } |
557 | | |
558 | | /* maxTokenIds */ |
559 | 0 | pOutParameters->maxTokenIds = targetParameters->maxTokenIds; |
560 | | |
561 | | /* numPriorities */ |
562 | |
|
563 | 0 | if (minimumParameters->numPriorities <= 1) |
564 | 0 | { |
565 | 0 | pOutParameters->numPriorities = 1; |
566 | 0 | } |
567 | 0 | else |
568 | 0 | { |
569 | 0 | WLog_Print(log, WLOG_ERROR, "invalid numPriorities [%" PRIu32 "]", |
570 | 0 | maximumParameters->numPriorities); |
571 | 0 | return FALSE; |
572 | 0 | } |
573 | | |
574 | | /* minThroughput */ |
575 | 0 | pOutParameters->minThroughput = targetParameters->minThroughput; |
576 | | |
577 | | /* maxHeight */ |
578 | |
|
579 | 0 | if ((targetParameters->maxHeight == 1) || (minimumParameters->maxHeight <= 1)) |
580 | 0 | { |
581 | 0 | pOutParameters->maxHeight = 1; |
582 | 0 | } |
583 | 0 | else |
584 | 0 | { |
585 | 0 | WLog_Print(log, WLOG_ERROR, "invalid maxHeight [%" PRIu32 ", %" PRIu32 "]", |
586 | 0 | targetParameters->maxHeight, minimumParameters->maxHeight); |
587 | 0 | return FALSE; |
588 | 0 | } |
589 | | |
590 | | /* maxMCSPDUsize */ |
591 | | |
592 | 0 | if (targetParameters->maxMCSPDUsize >= 1024) |
593 | 0 | { |
594 | 0 | if (targetParameters->maxMCSPDUsize <= 65528) |
595 | 0 | { |
596 | 0 | pOutParameters->maxMCSPDUsize = targetParameters->maxMCSPDUsize; |
597 | 0 | } |
598 | 0 | else if ((minimumParameters->maxMCSPDUsize >= 124) && |
599 | 0 | (minimumParameters->maxMCSPDUsize <= 65528)) |
600 | 0 | { |
601 | 0 | pOutParameters->maxMCSPDUsize = 65528; |
602 | 0 | } |
603 | 0 | else |
604 | 0 | { |
605 | 0 | WLog_Print(log, WLOG_ERROR, "invalid maxMCSPDUsize [%" PRIu32 ", %" PRIu32 "]", |
606 | 0 | targetParameters->maxMCSPDUsize, minimumParameters->maxMCSPDUsize); |
607 | 0 | return FALSE; |
608 | 0 | } |
609 | 0 | } |
610 | 0 | else |
611 | 0 | { |
612 | 0 | if (maximumParameters->maxMCSPDUsize >= 124) |
613 | 0 | { |
614 | 0 | pOutParameters->maxMCSPDUsize = maximumParameters->maxMCSPDUsize; |
615 | 0 | } |
616 | 0 | else |
617 | 0 | { |
618 | 0 | WLog_Print(log, WLOG_ERROR, "invalid maxMCSPDUsize [%" PRIu32 "]", |
619 | 0 | maximumParameters->maxMCSPDUsize); |
620 | 0 | return FALSE; |
621 | 0 | } |
622 | 0 | } |
623 | | |
624 | | /* protocolVersion */ |
625 | | |
626 | 0 | if ((targetParameters->protocolVersion == 2) || |
627 | 0 | ((minimumParameters->protocolVersion <= 2) && (maximumParameters->protocolVersion >= 2))) |
628 | 0 | { |
629 | 0 | pOutParameters->protocolVersion = 2; |
630 | 0 | } |
631 | 0 | else |
632 | 0 | { |
633 | 0 | WLog_Print(log, WLOG_ERROR, |
634 | 0 | "invalid protocolVersion [%" PRIu32 ", %" PRIu32 ", %" PRIu32 "]", |
635 | 0 | targetParameters->protocolVersion, minimumParameters->protocolVersion, |
636 | 0 | maximumParameters->protocolVersion); |
637 | 0 | return FALSE; |
638 | 0 | } |
639 | | |
640 | 0 | return TRUE; |
641 | 0 | } |
642 | | |
643 | | /** |
644 | | * Read an MCS Connect Initial PDU. |
645 | | * msdn{cc240508} |
646 | | * @param mcs MCS module |
647 | | * @param s stream |
648 | | */ |
649 | | |
650 | | BOOL mcs_recv_connect_initial(rdpMcs* mcs, wStream* s) |
651 | 0 | { |
652 | 0 | UINT16 li = 0; |
653 | 0 | size_t length = 0; |
654 | 0 | BOOL upwardFlag = FALSE; |
655 | 0 | UINT16 tlength = 0; |
656 | |
|
657 | 0 | WINPR_ASSERT(mcs); |
658 | 0 | WINPR_ASSERT(s); |
659 | | |
660 | 0 | if (!tpkt_read_header(s, &tlength)) |
661 | 0 | return FALSE; |
662 | | |
663 | 0 | if (!tpdu_read_data(s, &li, tlength)) |
664 | 0 | return FALSE; |
665 | | |
666 | 0 | if (!ber_read_application_tag(s, MCS_TYPE_CONNECT_INITIAL, &length)) |
667 | 0 | return FALSE; |
668 | | |
669 | | /* callingDomainSelector (OCTET_STRING) */ |
670 | 0 | if (!ber_read_octet_string_tag(s, &length) || |
671 | 0 | (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, length))) |
672 | 0 | return FALSE; |
673 | | |
674 | 0 | Stream_Seek(s, length); |
675 | | |
676 | | /* calledDomainSelector (OCTET_STRING) */ |
677 | 0 | if (!ber_read_octet_string_tag(s, &length) || |
678 | 0 | (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, length))) |
679 | 0 | return FALSE; |
680 | | |
681 | 0 | Stream_Seek(s, length); |
682 | | |
683 | | /* upwardFlag (BOOLEAN) */ |
684 | 0 | if (!ber_read_BOOL(s, &upwardFlag)) |
685 | 0 | return FALSE; |
686 | | |
687 | | /* targetParameters (DomainParameters) */ |
688 | 0 | if (!mcs_read_domain_parameters(s, &mcs->targetParameters)) |
689 | 0 | return FALSE; |
690 | | |
691 | | /* minimumParameters (DomainParameters) */ |
692 | 0 | if (!mcs_read_domain_parameters(s, &mcs->minimumParameters)) |
693 | 0 | return FALSE; |
694 | | |
695 | | /* maximumParameters (DomainParameters) */ |
696 | 0 | if (!mcs_read_domain_parameters(s, &mcs->maximumParameters)) |
697 | 0 | return FALSE; |
698 | | |
699 | 0 | if (!ber_read_octet_string_tag(s, &length) || |
700 | 0 | (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, length))) |
701 | 0 | return FALSE; |
702 | | |
703 | 0 | if (!gcc_read_conference_create_request(s, mcs)) |
704 | 0 | return FALSE; |
705 | | |
706 | 0 | if (!mcs_merge_domain_parameters(mcs->log, &mcs->targetParameters, &mcs->minimumParameters, |
707 | 0 | &mcs->maximumParameters, &mcs->domainParameters)) |
708 | 0 | return FALSE; |
709 | | |
710 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, tlength); |
711 | 0 | } |
712 | | |
713 | | /** |
714 | | * Write an MCS Connect Initial PDU. |
715 | | * msdn{cc240508} |
716 | | * @param s stream |
717 | | * @param mcs MCS module |
718 | | * @param userData GCC Conference Create Request |
719 | | */ |
720 | | |
721 | | BOOL mcs_write_connect_initial(wStream* s, rdpMcs* mcs, wStream* userData) |
722 | 0 | { |
723 | 0 | size_t length = 0; |
724 | 0 | wStream* tmps = NULL; |
725 | 0 | BOOL ret = FALSE; |
726 | |
|
727 | 0 | if (!s || !mcs || !userData) |
728 | 0 | return FALSE; |
729 | | |
730 | 0 | tmps = Stream_New(NULL, Stream_Capacity(s)); |
731 | |
|
732 | 0 | if (!tmps) |
733 | 0 | { |
734 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
735 | 0 | return FALSE; |
736 | 0 | } |
737 | | |
738 | | /* callingDomainSelector (OCTET_STRING) */ |
739 | 0 | ber_write_octet_string(tmps, callingDomainSelector, sizeof(callingDomainSelector)); |
740 | | /* calledDomainSelector (OCTET_STRING) */ |
741 | 0 | ber_write_octet_string(tmps, calledDomainSelector, sizeof(calledDomainSelector)); |
742 | | /* upwardFlag (BOOLEAN) */ |
743 | 0 | ber_write_BOOL(tmps, TRUE); |
744 | | |
745 | | /* targetParameters (DomainParameters) */ |
746 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &mcs->targetParameters)) |
747 | 0 | goto out; |
748 | | |
749 | | /* minimumParameters (DomainParameters) */ |
750 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &mcs->minimumParameters)) |
751 | 0 | goto out; |
752 | | |
753 | | /* maximumParameters (DomainParameters) */ |
754 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &mcs->maximumParameters)) |
755 | 0 | goto out; |
756 | | |
757 | | /* userData (OCTET_STRING) */ |
758 | 0 | ber_write_octet_string(tmps, Stream_Buffer(userData), Stream_GetPosition(userData)); |
759 | 0 | length = Stream_GetPosition(tmps); |
760 | | /* Connect-Initial (APPLICATION 101, IMPLICIT SEQUENCE) */ |
761 | 0 | ber_write_application_tag(s, MCS_TYPE_CONNECT_INITIAL, length); |
762 | 0 | Stream_Write(s, Stream_Buffer(tmps), length); |
763 | 0 | ret = TRUE; |
764 | 0 | out: |
765 | 0 | Stream_Free(tmps, TRUE); |
766 | 0 | return ret; |
767 | 0 | } |
768 | | |
769 | | /** |
770 | | * Write an MCS Connect Response PDU. |
771 | | * msdn{cc240508} |
772 | | * @param s stream |
773 | | * @param mcs MCS module |
774 | | * @param userData GCC Conference Create Response |
775 | | * |
776 | | * @return \b TRUE for success, \b FALSE otherwise |
777 | | */ |
778 | | |
779 | | BOOL mcs_write_connect_response(wStream* s, rdpMcs* mcs, wStream* userData) |
780 | 0 | { |
781 | 0 | size_t length = 0; |
782 | 0 | wStream* tmps = NULL; |
783 | 0 | BOOL ret = FALSE; |
784 | |
|
785 | 0 | if (!s || !mcs || !userData) |
786 | 0 | return FALSE; |
787 | | |
788 | 0 | tmps = Stream_New(NULL, Stream_Capacity(s)); |
789 | |
|
790 | 0 | if (!tmps) |
791 | 0 | { |
792 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
793 | 0 | return FALSE; |
794 | 0 | } |
795 | | |
796 | 0 | ber_write_enumerated(tmps, 0, MCS_Result_enum_length); |
797 | 0 | ber_write_integer(tmps, 0); /* calledConnectId */ |
798 | |
|
799 | 0 | if (!mcs_write_domain_parameters(mcs->log, tmps, &(mcs->domainParameters))) |
800 | 0 | goto out; |
801 | | |
802 | | /* userData (OCTET_STRING) */ |
803 | 0 | ber_write_octet_string(tmps, Stream_Buffer(userData), Stream_GetPosition(userData)); |
804 | 0 | length = Stream_GetPosition(tmps); |
805 | 0 | ber_write_application_tag(s, MCS_TYPE_CONNECT_RESPONSE, length); |
806 | 0 | Stream_Write(s, Stream_Buffer(tmps), length); |
807 | 0 | ret = TRUE; |
808 | 0 | out: |
809 | 0 | Stream_Free(tmps, TRUE); |
810 | 0 | return ret; |
811 | 0 | } |
812 | | |
813 | | /** |
814 | | * Send MCS Connect Initial. |
815 | | * msdn{cc240508} |
816 | | * @param mcs mcs module |
817 | | */ |
818 | | |
819 | | static BOOL mcs_send_connect_initial(rdpMcs* mcs) |
820 | 0 | { |
821 | 0 | int status = -1; |
822 | 0 | size_t length = 0; |
823 | 0 | wStream* s = NULL; |
824 | 0 | size_t bm = 0; |
825 | 0 | size_t em = 0; |
826 | 0 | wStream* gcc_CCrq = NULL; |
827 | 0 | wStream* client_data = NULL; |
828 | 0 | rdpContext* context = NULL; |
829 | |
|
830 | 0 | if (!mcs) |
831 | 0 | return FALSE; |
832 | | |
833 | 0 | context = transport_get_context(mcs->transport); |
834 | 0 | WINPR_ASSERT(context); |
835 | | |
836 | 0 | mcs_initialize_client_channels(mcs, context->settings); |
837 | 0 | client_data = Stream_New(NULL, 512); |
838 | |
|
839 | 0 | if (!client_data) |
840 | 0 | { |
841 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
842 | 0 | return FALSE; |
843 | 0 | } |
844 | | |
845 | 0 | if (!gcc_write_client_data_blocks(client_data, mcs)) |
846 | 0 | goto out; |
847 | 0 | gcc_CCrq = Stream_New(NULL, 1024); |
848 | |
|
849 | 0 | if (!gcc_CCrq) |
850 | 0 | { |
851 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
852 | 0 | goto out; |
853 | 0 | } |
854 | | |
855 | 0 | if (!gcc_write_conference_create_request(gcc_CCrq, client_data)) |
856 | 0 | goto out; |
857 | 0 | length = Stream_GetPosition(gcc_CCrq) + 7; |
858 | 0 | s = Stream_New(NULL, 1024 + length); |
859 | |
|
860 | 0 | if (!s) |
861 | 0 | { |
862 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
863 | 0 | goto out; |
864 | 0 | } |
865 | | |
866 | 0 | bm = Stream_GetPosition(s); |
867 | 0 | Stream_Seek(s, 7); |
868 | |
|
869 | 0 | if (!mcs_write_connect_initial(s, mcs, gcc_CCrq)) |
870 | 0 | { |
871 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "mcs_write_connect_initial failed!"); |
872 | 0 | goto out; |
873 | 0 | } |
874 | | |
875 | 0 | em = Stream_GetPosition(s); |
876 | 0 | length = (em - bm); |
877 | 0 | if (length > UINT16_MAX) |
878 | 0 | goto out; |
879 | 0 | Stream_SetPosition(s, bm); |
880 | 0 | if (!tpkt_write_header(s, (UINT16)length)) |
881 | 0 | goto out; |
882 | 0 | if (!tpdu_write_data(s)) |
883 | 0 | goto out; |
884 | 0 | Stream_SetPosition(s, em); |
885 | 0 | Stream_SealLength(s); |
886 | 0 | status = transport_write(mcs->transport, s); |
887 | 0 | out: |
888 | 0 | Stream_Free(s, TRUE); |
889 | 0 | Stream_Free(gcc_CCrq, TRUE); |
890 | 0 | Stream_Free(client_data, TRUE); |
891 | 0 | return (status < 0 ? FALSE : TRUE); |
892 | 0 | } |
893 | | |
894 | | /** |
895 | | * Read MCS Connect Response. |
896 | | * msdn{cc240501} |
897 | | * @param mcs mcs module |
898 | | */ |
899 | | |
900 | | BOOL mcs_recv_connect_response(rdpMcs* mcs, wStream* s) |
901 | 0 | { |
902 | 0 | size_t length = 0; |
903 | 0 | UINT16 tlength = 0; |
904 | 0 | BYTE result = 0; |
905 | 0 | UINT16 li = 0; |
906 | 0 | UINT32 calledConnectId = 0; |
907 | |
|
908 | 0 | if (!mcs || !s) |
909 | 0 | return FALSE; |
910 | | |
911 | 0 | if (!tpkt_read_header(s, &tlength)) |
912 | 0 | return FALSE; |
913 | | |
914 | 0 | if (!tpdu_read_data(s, &li, tlength)) |
915 | 0 | return FALSE; |
916 | | |
917 | 0 | if (!ber_read_application_tag(s, MCS_TYPE_CONNECT_RESPONSE, &length) || |
918 | 0 | !ber_read_enumerated(s, &result, MCS_Result_enum_length) || |
919 | 0 | !ber_read_integer(s, &calledConnectId) || |
920 | 0 | !mcs_read_domain_parameters(s, &(mcs->domainParameters)) || |
921 | 0 | !ber_read_octet_string_tag(s, &length)) |
922 | 0 | { |
923 | 0 | return FALSE; |
924 | 0 | } |
925 | | |
926 | 0 | if (!gcc_read_conference_create_response(s, mcs)) |
927 | 0 | { |
928 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "gcc_read_conference_create_response failed"); |
929 | 0 | return FALSE; |
930 | 0 | } |
931 | | |
932 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, tlength); |
933 | 0 | } |
934 | | |
935 | | /** |
936 | | * Send MCS Connect Response. |
937 | | * msdn{cc240501} |
938 | | * @param mcs mcs module |
939 | | */ |
940 | | |
941 | | BOOL mcs_send_connect_response(rdpMcs* mcs) |
942 | 0 | { |
943 | 0 | size_t length = 0; |
944 | 0 | int status = -1; |
945 | 0 | wStream* s = NULL; |
946 | 0 | size_t bm = 0; |
947 | 0 | size_t em = 0; |
948 | 0 | wStream* gcc_CCrsp = NULL; |
949 | 0 | wStream* server_data = NULL; |
950 | |
|
951 | 0 | if (!mcs) |
952 | 0 | return FALSE; |
953 | | |
954 | 0 | server_data = Stream_New(NULL, 512); |
955 | |
|
956 | 0 | if (!server_data) |
957 | 0 | { |
958 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
959 | 0 | return FALSE; |
960 | 0 | } |
961 | | |
962 | 0 | if (!gcc_write_server_data_blocks(server_data, mcs)) |
963 | 0 | goto out; |
964 | | |
965 | 0 | gcc_CCrsp = Stream_New(NULL, 512 + Stream_Capacity(server_data)); |
966 | |
|
967 | 0 | if (!gcc_CCrsp) |
968 | 0 | { |
969 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
970 | 0 | goto out; |
971 | 0 | } |
972 | | |
973 | 0 | if (!gcc_write_conference_create_response(gcc_CCrsp, server_data)) |
974 | 0 | goto out; |
975 | 0 | length = Stream_GetPosition(gcc_CCrsp) + 7; |
976 | 0 | s = Stream_New(NULL, length + 1024); |
977 | |
|
978 | 0 | if (!s) |
979 | 0 | { |
980 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
981 | 0 | goto out; |
982 | 0 | } |
983 | | |
984 | 0 | bm = Stream_GetPosition(s); |
985 | 0 | Stream_Seek(s, 7); |
986 | |
|
987 | 0 | if (!mcs_write_connect_response(s, mcs, gcc_CCrsp)) |
988 | 0 | goto out; |
989 | | |
990 | 0 | em = Stream_GetPosition(s); |
991 | 0 | length = (em - bm); |
992 | 0 | if (length > UINT16_MAX) |
993 | 0 | goto out; |
994 | 0 | Stream_SetPosition(s, bm); |
995 | 0 | if (!tpkt_write_header(s, (UINT16)length)) |
996 | 0 | goto out; |
997 | 0 | if (!tpdu_write_data(s)) |
998 | 0 | goto out; |
999 | 0 | Stream_SetPosition(s, em); |
1000 | 0 | Stream_SealLength(s); |
1001 | 0 | status = transport_write(mcs->transport, s); |
1002 | 0 | out: |
1003 | 0 | Stream_Free(s, TRUE); |
1004 | 0 | Stream_Free(gcc_CCrsp, TRUE); |
1005 | 0 | Stream_Free(server_data, TRUE); |
1006 | 0 | return (status < 0) ? FALSE : TRUE; |
1007 | 0 | } |
1008 | | |
1009 | | /** |
1010 | | * Read MCS Erect Domain Request. |
1011 | | * msdn{cc240523} |
1012 | | * @param mcs MCS module to use |
1013 | | * @param s stream |
1014 | | */ |
1015 | | |
1016 | | BOOL mcs_recv_erect_domain_request(rdpMcs* mcs, wStream* s) |
1017 | 0 | { |
1018 | 0 | UINT16 length = 0; |
1019 | 0 | UINT32 subHeight = 0; |
1020 | 0 | UINT32 subInterval = 0; |
1021 | |
|
1022 | 0 | WINPR_ASSERT(mcs); |
1023 | 0 | WINPR_ASSERT(s); |
1024 | | |
1025 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_ErectDomainRequest, &length, NULL)) |
1026 | 0 | return FALSE; |
1027 | | |
1028 | 0 | if (!per_read_integer(s, &subHeight)) /* subHeight (INTEGER) */ |
1029 | 0 | return FALSE; |
1030 | | |
1031 | 0 | if (!per_read_integer(s, &subInterval)) /* subInterval (INTEGER) */ |
1032 | 0 | return FALSE; |
1033 | | |
1034 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1035 | 0 | } |
1036 | | |
1037 | | /** |
1038 | | * Send MCS Erect Domain Request. |
1039 | | * msdn{cc240523} |
1040 | | * @param mcs MCS module to use |
1041 | | */ |
1042 | | |
1043 | | BOOL mcs_send_erect_domain_request(rdpMcs* mcs) |
1044 | 0 | { |
1045 | 0 | wStream* s = NULL; |
1046 | 0 | int status = 0; |
1047 | 0 | UINT16 length = 12; |
1048 | |
|
1049 | 0 | if (!mcs) |
1050 | 0 | return FALSE; |
1051 | | |
1052 | 0 | s = Stream_New(NULL, length); |
1053 | |
|
1054 | 0 | if (!s) |
1055 | 0 | { |
1056 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1057 | 0 | return FALSE; |
1058 | 0 | } |
1059 | | |
1060 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_ErectDomainRequest, length, 0); |
1061 | 0 | per_write_integer(s, 0); /* subHeight (INTEGER) */ |
1062 | 0 | per_write_integer(s, 0); /* subInterval (INTEGER) */ |
1063 | 0 | Stream_SealLength(s); |
1064 | 0 | status = transport_write(mcs->transport, s); |
1065 | 0 | Stream_Free(s, TRUE); |
1066 | 0 | return (status < 0) ? FALSE : TRUE; |
1067 | 0 | } |
1068 | | |
1069 | | /** |
1070 | | * Read MCS Attach User Request. |
1071 | | * msdn{cc240524} |
1072 | | * @param mcs mcs module |
1073 | | * @param s stream |
1074 | | */ |
1075 | | |
1076 | | BOOL mcs_recv_attach_user_request(rdpMcs* mcs, wStream* s) |
1077 | 0 | { |
1078 | 0 | UINT16 length = 0; |
1079 | |
|
1080 | 0 | if (!mcs || !s) |
1081 | 0 | return FALSE; |
1082 | | |
1083 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_AttachUserRequest, &length, NULL)) |
1084 | 0 | return FALSE; |
1085 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1086 | 0 | } |
1087 | | |
1088 | | /** |
1089 | | * Send MCS Attach User Request. |
1090 | | * msdn{cc240524} |
1091 | | * @param mcs mcs module |
1092 | | */ |
1093 | | |
1094 | | BOOL mcs_send_attach_user_request(rdpMcs* mcs) |
1095 | 0 | { |
1096 | 0 | wStream* s = NULL; |
1097 | 0 | int status = 0; |
1098 | 0 | UINT16 length = 8; |
1099 | |
|
1100 | 0 | if (!mcs) |
1101 | 0 | return FALSE; |
1102 | | |
1103 | 0 | s = Stream_New(NULL, length); |
1104 | |
|
1105 | 0 | if (!s) |
1106 | 0 | { |
1107 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1108 | 0 | return FALSE; |
1109 | 0 | } |
1110 | | |
1111 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_AttachUserRequest, length, 0); |
1112 | 0 | Stream_SealLength(s); |
1113 | 0 | status = transport_write(mcs->transport, s); |
1114 | 0 | Stream_Free(s, TRUE); |
1115 | 0 | return (status < 0) ? FALSE : TRUE; |
1116 | 0 | } |
1117 | | |
1118 | | /** |
1119 | | * Read MCS Attach User Confirm. |
1120 | | * msdn{cc240525} |
1121 | | * @param mcs mcs module |
1122 | | */ |
1123 | | |
1124 | | BOOL mcs_recv_attach_user_confirm(rdpMcs* mcs, wStream* s) |
1125 | 0 | { |
1126 | 0 | BYTE result = 0; |
1127 | 0 | UINT16 length = 0; |
1128 | |
|
1129 | 0 | if (!mcs || !s) |
1130 | 0 | return FALSE; |
1131 | | |
1132 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_AttachUserConfirm, &length, NULL)) |
1133 | 0 | return FALSE; |
1134 | 0 | if (!per_read_enumerated(s, &result, MCS_Result_enum_length)) /* result */ |
1135 | 0 | return FALSE; |
1136 | 0 | if (!per_read_integer16(s, &(mcs->userId), MCS_BASE_CHANNEL_ID)) /* initiator (UserId) */ |
1137 | 0 | return FALSE; |
1138 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1139 | 0 | } |
1140 | | |
1141 | | /** |
1142 | | * Send MCS Attach User Confirm. |
1143 | | * msdn{cc240525} |
1144 | | * @param mcs mcs module |
1145 | | */ |
1146 | | |
1147 | | BOOL mcs_send_attach_user_confirm(rdpMcs* mcs) |
1148 | 0 | { |
1149 | 0 | wStream* s = NULL; |
1150 | 0 | int status = 0; |
1151 | 0 | UINT16 length = 11; |
1152 | |
|
1153 | 0 | if (!mcs) |
1154 | 0 | return FALSE; |
1155 | | |
1156 | 0 | s = Stream_New(NULL, length); |
1157 | |
|
1158 | 0 | if (!s) |
1159 | 0 | { |
1160 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1161 | 0 | return FALSE; |
1162 | 0 | } |
1163 | | |
1164 | 0 | mcs->userId = mcs->baseChannelId++; |
1165 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_AttachUserConfirm, length, 2); |
1166 | 0 | per_write_enumerated(s, 0, MCS_Result_enum_length); /* result */ |
1167 | 0 | per_write_integer16(s, mcs->userId, MCS_BASE_CHANNEL_ID); /* initiator (UserId) */ |
1168 | 0 | Stream_SealLength(s); |
1169 | 0 | status = transport_write(mcs->transport, s); |
1170 | 0 | Stream_Free(s, TRUE); |
1171 | 0 | return (status < 0) ? FALSE : TRUE; |
1172 | 0 | } |
1173 | | |
1174 | | /** |
1175 | | * Read MCS Channel Join Request. |
1176 | | * msdn{cc240526} |
1177 | | * @param mcs mcs module |
1178 | | * @param s stream |
1179 | | */ |
1180 | | |
1181 | | BOOL mcs_recv_channel_join_request(rdpMcs* mcs, const rdpSettings* settings, wStream* s, |
1182 | | UINT16* channelId) |
1183 | 0 | { |
1184 | 0 | UINT16 length = 0; |
1185 | 0 | UINT16 userId = 0; |
1186 | |
|
1187 | 0 | if (!mcs || !s || !channelId) |
1188 | 0 | return FALSE; |
1189 | | |
1190 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_ChannelJoinRequest, &length, NULL)) |
1191 | 0 | return FALSE; |
1192 | | |
1193 | 0 | if (!per_read_integer16(s, &userId, MCS_BASE_CHANNEL_ID)) |
1194 | 0 | return FALSE; |
1195 | 0 | if (userId != mcs->userId) |
1196 | 0 | { |
1197 | 0 | if (freerdp_settings_get_bool(settings, FreeRDP_TransportDumpReplay)) |
1198 | 0 | mcs->userId = userId; |
1199 | 0 | else |
1200 | 0 | return FALSE; |
1201 | 0 | } |
1202 | 0 | if (!per_read_integer16(s, channelId, 0)) |
1203 | 0 | return FALSE; |
1204 | | |
1205 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1206 | 0 | } |
1207 | | |
1208 | | /** |
1209 | | * Send MCS Channel Join Request. |
1210 | | * msdn{cc240526} |
1211 | | * |
1212 | | * @param mcs mcs module |
1213 | | * @param channelId channel id |
1214 | | * |
1215 | | * @return \b TRUE for success, \b FALSE otherwise |
1216 | | */ |
1217 | | |
1218 | | BOOL mcs_send_channel_join_request(rdpMcs* mcs, UINT16 channelId) |
1219 | 0 | { |
1220 | 0 | wStream* s = NULL; |
1221 | 0 | int status = 0; |
1222 | 0 | UINT16 length = 12; |
1223 | |
|
1224 | 0 | WINPR_ASSERT(mcs); |
1225 | | |
1226 | 0 | s = Stream_New(NULL, length); |
1227 | |
|
1228 | 0 | if (!s) |
1229 | 0 | { |
1230 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1231 | 0 | return FALSE; |
1232 | 0 | } |
1233 | | |
1234 | 0 | mcs_write_domain_mcspdu_header(s, DomainMCSPDU_ChannelJoinRequest, length, 0); |
1235 | 0 | per_write_integer16(s, mcs->userId, MCS_BASE_CHANNEL_ID); |
1236 | 0 | per_write_integer16(s, channelId, 0); |
1237 | 0 | Stream_SealLength(s); |
1238 | 0 | status = transport_write(mcs->transport, s); |
1239 | 0 | Stream_Free(s, TRUE); |
1240 | 0 | return (status < 0) ? FALSE : TRUE; |
1241 | 0 | } |
1242 | | |
1243 | | /** |
1244 | | * Read MCS Channel Join Confirm. |
1245 | | * msdn{cc240527} |
1246 | | * @param mcs mcs module |
1247 | | */ |
1248 | | |
1249 | | BOOL mcs_recv_channel_join_confirm(rdpMcs* mcs, wStream* s, UINT16* channelId) |
1250 | 0 | { |
1251 | 0 | UINT16 length = 0; |
1252 | 0 | BYTE result = 0; |
1253 | 0 | UINT16 initiator = 0; |
1254 | 0 | UINT16 requested = 0; |
1255 | |
|
1256 | 0 | WINPR_ASSERT(mcs); |
1257 | 0 | WINPR_ASSERT(channelId); |
1258 | | |
1259 | 0 | if (!mcs_read_domain_mcspdu_header(mcs->log, s, DomainMCSPDU_ChannelJoinConfirm, &length, NULL)) |
1260 | 0 | return FALSE; |
1261 | | |
1262 | 0 | if (!per_read_enumerated(s, &result, MCS_Result_enum_length)) /* result */ |
1263 | 0 | return FALSE; |
1264 | 0 | if (!per_read_integer16(s, &initiator, MCS_BASE_CHANNEL_ID)) /* initiator (UserId) */ |
1265 | 0 | return FALSE; |
1266 | 0 | if (!per_read_integer16(s, &requested, 0)) /* requested (ChannelId) */ |
1267 | 0 | return FALSE; |
1268 | 0 | if (!per_read_integer16(s, channelId, 0)) /* channelId */ |
1269 | 0 | return FALSE; |
1270 | 0 | return tpkt_ensure_stream_consumed(mcs->log, s, length); |
1271 | 0 | } |
1272 | | |
1273 | | /** |
1274 | | * Send MCS Channel Join Confirm. |
1275 | | * msdn{cc240527} |
1276 | | * @param mcs mcs module |
1277 | | */ |
1278 | | |
1279 | | BOOL mcs_send_channel_join_confirm(rdpMcs* mcs, UINT16 channelId) |
1280 | 0 | { |
1281 | 0 | wStream* s = NULL; |
1282 | 0 | int status = -1; |
1283 | 0 | UINT16 length = 15; |
1284 | |
|
1285 | 0 | if (!mcs) |
1286 | 0 | return FALSE; |
1287 | | |
1288 | 0 | s = Stream_New(NULL, length); |
1289 | |
|
1290 | 0 | if (!s) |
1291 | 0 | { |
1292 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Stream_New failed!"); |
1293 | 0 | return FALSE; |
1294 | 0 | } |
1295 | | |
1296 | 0 | if (!mcs_write_domain_mcspdu_header(s, DomainMCSPDU_ChannelJoinConfirm, length, 2)) |
1297 | 0 | goto fail; |
1298 | 0 | if (!per_write_enumerated(s, 0, MCS_Result_enum_length)) /* result */ |
1299 | 0 | goto fail; |
1300 | 0 | if (!per_write_integer16(s, mcs->userId, MCS_BASE_CHANNEL_ID)) /* initiator (UserId) */ |
1301 | 0 | goto fail; |
1302 | 0 | if (!per_write_integer16(s, channelId, 0)) /* requested (ChannelId) */ |
1303 | 0 | goto fail; |
1304 | 0 | if (!per_write_integer16(s, channelId, 0)) /* channelId */ |
1305 | 0 | goto fail; |
1306 | 0 | Stream_SealLength(s); |
1307 | 0 | status = transport_write(mcs->transport, s); |
1308 | 0 | fail: |
1309 | 0 | Stream_Free(s, TRUE); |
1310 | 0 | return (status < 0) ? FALSE : TRUE; |
1311 | 0 | } |
1312 | | |
1313 | | /** |
1314 | | * Receive MCS Disconnect Provider Ultimatum PDU. |
1315 | | * @param mcs mcs module |
1316 | | */ |
1317 | | |
1318 | | BOOL mcs_recv_disconnect_provider_ultimatum(WINPR_ATTR_UNUSED rdpMcs* mcs, wStream* s, int* reason) |
1319 | 0 | { |
1320 | 0 | BYTE b1 = 0; |
1321 | 0 | BYTE b2 = 0; |
1322 | |
|
1323 | 0 | WINPR_ASSERT(mcs); |
1324 | 0 | WINPR_ASSERT(s); |
1325 | 0 | WINPR_ASSERT(reason); |
1326 | | |
1327 | | /* |
1328 | | * http://msdn.microsoft.com/en-us/library/cc240872.aspx: |
1329 | | * |
1330 | | * PER encoded (ALIGNED variant of BASIC-PER) PDU contents: |
1331 | | * 21 80 |
1332 | | * |
1333 | | * 0x21: |
1334 | | * 0 - --\ |
1335 | | * 0 - | |
1336 | | * 1 - | CHOICE: From DomainMCSPDU select disconnectProviderUltimatum (8) |
1337 | | * 0 - | of type DisconnectProviderUltimatum |
1338 | | * 0 - | |
1339 | | * 0 - --/ |
1340 | | * 0 - --\ |
1341 | | * 1 - | |
1342 | | * | DisconnectProviderUltimatum::reason = rn-user-requested (3) |
1343 | | * 0x80: | |
1344 | | * 1 - --/ |
1345 | | * 0 - padding |
1346 | | * 0 - padding |
1347 | | * 0 - padding |
1348 | | * 0 - padding |
1349 | | * 0 - padding |
1350 | | * 0 - padding |
1351 | | * 0 - padding |
1352 | | */ |
1353 | | |
1354 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(mcs->log, s, 1)) |
1355 | 0 | return FALSE; |
1356 | | |
1357 | 0 | Stream_Rewind_UINT8(s); |
1358 | 0 | Stream_Read_UINT8(s, b1); |
1359 | 0 | Stream_Read_UINT8(s, b2); |
1360 | 0 | *reason = ((b1 & 0x01) << 1) | (b2 >> 7); |
1361 | 0 | return TRUE; |
1362 | 0 | } |
1363 | | |
1364 | | /** |
1365 | | * Send MCS Disconnect Provider Ultimatum PDU. |
1366 | | * @param mcs mcs module |
1367 | | */ |
1368 | | |
1369 | | BOOL mcs_send_disconnect_provider_ultimatum(rdpMcs* mcs, enum Disconnect_Ultimatum reason) |
1370 | 0 | { |
1371 | 0 | wStream* s = NULL; |
1372 | 0 | int status = -1; |
1373 | 0 | UINT16 length = 9; |
1374 | |
|
1375 | 0 | WINPR_ASSERT(mcs); |
1376 | | |
1377 | 0 | s = Stream_New(NULL, length); |
1378 | |
|
1379 | 0 | if (!s) |
1380 | 0 | goto fail; |
1381 | | |
1382 | 0 | if (!mcs_write_domain_mcspdu_header(s, DomainMCSPDU_DisconnectProviderUltimatum, length, 1)) |
1383 | 0 | goto fail; |
1384 | | |
1385 | 0 | if (!per_write_enumerated(s, 0x80, WINPR_ASSERTING_INT_CAST(BYTE, reason))) |
1386 | 0 | goto fail; |
1387 | 0 | status = transport_write(mcs->transport, s); |
1388 | 0 | fail: |
1389 | 0 | WLog_Print(mcs->log, WLOG_DEBUG, "sending DisconnectProviderUltimatum(%s)", |
1390 | 0 | freerdp_disconnect_reason_string((int)reason)); |
1391 | 0 | Stream_Free(s, TRUE); |
1392 | 0 | return (status < 0) ? FALSE : TRUE; |
1393 | 0 | } |
1394 | | |
1395 | | BOOL mcs_client_begin(rdpMcs* mcs) |
1396 | 0 | { |
1397 | 0 | rdpContext* context = NULL; |
1398 | |
|
1399 | 0 | if (!mcs || !mcs->transport) |
1400 | 0 | return FALSE; |
1401 | | |
1402 | 0 | context = transport_get_context(mcs->transport); |
1403 | |
|
1404 | 0 | if (!context) |
1405 | 0 | return FALSE; |
1406 | | |
1407 | | /* First transition state, we need this to trigger session recording */ |
1408 | 0 | if (!mcs_send_connect_initial(mcs)) |
1409 | 0 | { |
1410 | 0 | freerdp_set_last_error_if_not(context, FREERDP_ERROR_MCS_CONNECT_INITIAL_ERROR); |
1411 | |
|
1412 | 0 | WLog_Print(mcs->log, WLOG_ERROR, "Error: unable to send MCS Connect Initial"); |
1413 | 0 | return FALSE; |
1414 | 0 | } |
1415 | | |
1416 | 0 | return TRUE; |
1417 | 0 | } |
1418 | | |
1419 | | /** |
1420 | | * Instantiate new MCS module. |
1421 | | * @param transport transport |
1422 | | * @return new MCS module |
1423 | | */ |
1424 | | |
1425 | | rdpMcs* mcs_new(rdpTransport* transport) |
1426 | 0 | { |
1427 | 0 | rdpMcs* mcs = NULL; |
1428 | |
|
1429 | 0 | mcs = (rdpMcs*)calloc(1, sizeof(rdpMcs)); |
1430 | |
|
1431 | 0 | if (!mcs) |
1432 | 0 | return NULL; |
1433 | 0 | mcs->log = WLog_Get(MCS_TAG); |
1434 | 0 | WINPR_ASSERT(mcs->log); |
1435 | | |
1436 | 0 | mcs->transport = transport; |
1437 | 0 | mcs_init_domain_parameters(&mcs->targetParameters, 34, 2, 0, 0xFFFF); |
1438 | 0 | mcs_init_domain_parameters(&mcs->minimumParameters, 1, 1, 1, 0x420); |
1439 | 0 | mcs_init_domain_parameters(&mcs->maximumParameters, 0xFFFF, 0xFC17, 0xFFFF, 0xFFFF); |
1440 | 0 | mcs_init_domain_parameters(&mcs->domainParameters, 0, 0, 0, 0xFFFF); |
1441 | 0 | mcs->channelCount = 0; |
1442 | 0 | mcs->channelMaxCount = CHANNEL_MAX_COUNT; |
1443 | 0 | mcs->baseChannelId = MCS_GLOBAL_CHANNEL_ID + 1; |
1444 | 0 | mcs->channels = (rdpMcsChannel*)calloc(mcs->channelMaxCount, sizeof(rdpMcsChannel)); |
1445 | |
|
1446 | 0 | if (!mcs->channels) |
1447 | 0 | goto out_free; |
1448 | | |
1449 | 0 | return mcs; |
1450 | 0 | out_free: |
1451 | 0 | free(mcs); |
1452 | 0 | return NULL; |
1453 | 0 | } |
1454 | | |
1455 | | /** |
1456 | | * Free MCS module. |
1457 | | * @param mcs MCS module to be freed |
1458 | | */ |
1459 | | |
1460 | | void mcs_free(rdpMcs* mcs) |
1461 | 0 | { |
1462 | 0 | if (mcs) |
1463 | 0 | { |
1464 | 0 | free(mcs->channels); |
1465 | 0 | free(mcs); |
1466 | 0 | } |
1467 | 0 | } |
1468 | | |
1469 | | BOOL mcs_server_apply_to_settings(const rdpMcs* mcs, rdpSettings* settings) |
1470 | 0 | { |
1471 | 0 | BOOL rc = FALSE; |
1472 | |
|
1473 | 0 | WINPR_ASSERT(mcs); |
1474 | 0 | WINPR_ASSERT(settings); |
1475 | | |
1476 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_ChannelCount, mcs->channelCount)) |
1477 | 0 | goto fail; |
1478 | | |
1479 | 0 | for (UINT32 x = 0; x < mcs->channelCount; x++) |
1480 | 0 | { |
1481 | 0 | const rdpMcsChannel* current = &mcs->channels[x]; |
1482 | 0 | CHANNEL_DEF def = { 0 }; |
1483 | 0 | def.options = current->options; |
1484 | 0 | memcpy(def.name, current->Name, sizeof(def.name)); |
1485 | 0 | if (!freerdp_settings_set_pointer_array(settings, FreeRDP_ChannelDefArray, x, &def)) |
1486 | 0 | goto fail; |
1487 | 0 | } |
1488 | | |
1489 | 0 | rc = TRUE; |
1490 | 0 | fail: |
1491 | 0 | if (!rc) |
1492 | 0 | WLog_Print(mcs->log, WLOG_WARN, "failed to apply settings"); |
1493 | |
|
1494 | 0 | return rc; |
1495 | 0 | } |