FreeRDP
Loading...
Searching...
No Matches
autodetect.c
1
19
20#include <freerdp/config.h>
21
22#include <winpr/crypto.h>
23#include <winpr/assert.h>
24
25#include "autodetect.h"
26#include "multitransport.h"
27
28#define TYPE_ID_AUTODETECT_REQUEST 0x00
29#define TYPE_ID_AUTODETECT_RESPONSE 0x01
30
31#define RDP_RTT_REQUEST_TYPE_CONTINUOUS 0x0001
32#define RDP_RTT_REQUEST_TYPE_CONNECTTIME 0x1001
33
34#define RDP_RTT_RESPONSE_TYPE 0x0000
35
36#define RDP_BW_START_REQUEST_TYPE_CONTINUOUS 0x0014
37#define RDP_BW_START_REQUEST_TYPE_TUNNEL 0x0114
38#define RDP_BW_START_REQUEST_TYPE_CONNECTTIME 0x1014
39#define RDP_BW_PAYLOAD_REQUEST_TYPE 0x0002
40#define RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME 0x002B
41#define RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS 0x0429
42#define RDP_BW_STOP_REQUEST_TYPE_TUNNEL 0x0629
43
44#define RDP_NETCHAR_SYNC_RESPONSE_TYPE 0x0018
45
46#define RDP_NETCHAR_RESULTS_0x0840 0x0840U
47#define RDP_NETCHAR_RESULTS_0x0880 0x0880U
48#define RDP_NETCHAR_RESULTS_0x08C0 0x08C0U
49
50typedef struct
51{
52 UINT8 headerLength;
53 UINT8 headerTypeId;
54 UINT16 sequenceNumber;
55 UINT16 requestType;
56} AUTODETECT_REQ_PDU;
57
58typedef struct
59{
60 UINT8 headerLength;
61 UINT8 headerTypeId;
62 UINT16 sequenceNumber;
63 UINT16 responseType;
64} AUTODETECT_RSP_PDU;
65
66static const char* autodetect_header_type_string(UINT8 headerType, char* buffer, size_t size)
67{
68 const char* str = nullptr;
69 switch (headerType)
70 {
71 case TYPE_ID_AUTODETECT_REQUEST:
72 str = "TYPE_ID_AUTODETECT_REQUEST";
73 break;
74 case TYPE_ID_AUTODETECT_RESPONSE:
75 str = "TYPE_ID_AUTODETECT_RESPONSE";
76 break;
77 default:
78 str = "TYPE_ID_AUTODETECT_UNKNOWN";
79 break;
80 }
81
82 (void)_snprintf(buffer, size, "%s [0x%08" PRIx8 "]", str, headerType);
83 return buffer;
84}
85
86static const char* autodetect_request_type_to_string(UINT32 requestType)
87{
88 switch (requestType)
89 {
90 case RDP_RTT_RESPONSE_TYPE:
91 return "RDP_RTT_RESPONSE_TYPE";
92 case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
93 return "RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME";
94 case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
95 return "RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS";
96 case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
97 return "RDP_RTT_REQUEST_TYPE_CONTINUOUS";
98 case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
99 return "RDP_RTT_REQUEST_TYPE_CONNECTTIME";
100 case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
101 return "RDP_BW_START_REQUEST_TYPE_CONTINUOUS";
102 case RDP_BW_START_REQUEST_TYPE_TUNNEL:
103 return "RDP_BW_START_REQUEST_TYPE_TUNNEL";
104 case RDP_BW_START_REQUEST_TYPE_CONNECTTIME:
105 return "RDP_BW_START_REQUEST_TYPE_CONNECTTIME";
106 case RDP_BW_PAYLOAD_REQUEST_TYPE:
107 return "RDP_BW_PAYLOAD_REQUEST_TYPE";
108 case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
109 return "RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME";
110 case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
111 return "RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS";
112 case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
113 return "RDP_BW_STOP_REQUEST_TYPE_TUNNEL";
114 case RDP_NETCHAR_RESULTS_0x0840:
115 return "RDP_NETCHAR_RESULTS_0x0840";
116 case RDP_NETCHAR_RESULTS_0x0880:
117 return "RDP_NETCHAR_RESULTS_0x0880";
118 case RDP_NETCHAR_RESULTS_0x08C0:
119 return "RDP_NETCHAR_RESULTS_0x08C0";
120 default:
121 return "UNKNOWN";
122 }
123}
124
125static const char* autodetect_request_type_to_string_buffer(UINT32 requestType, char* buffer,
126 size_t size)
127{
128 const char* str = autodetect_request_type_to_string(requestType);
129 (void)_snprintf(buffer, size, "%s [0x%08" PRIx32 "]", str, requestType);
130 return buffer;
131}
132
133static BOOL autodetect_send_rtt_measure_request(rdpAutoDetect* autodetect,
134 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
135 UINT16 sequenceNumber)
136{
137 UINT16 requestType = 0;
138 UINT16 sec_flags = 0;
139 wStream* s = nullptr;
140
141 WINPR_ASSERT(autodetect);
142 WINPR_ASSERT(autodetect->context);
143
144 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
145 if (!s)
146 return FALSE;
147
148 if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
149 requestType = RDP_RTT_REQUEST_TYPE_CONNECTTIME;
150 else
151 requestType = RDP_RTT_REQUEST_TYPE_CONTINUOUS;
152
153 WLog_Print(autodetect->log, WLOG_TRACE, "sending RTT Measure Request PDU");
154 Stream_Write_UINT8(s, 0x06); /* headerLength (1 byte) */
155 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
156 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
157 Stream_Write_UINT16(s, requestType); /* requestType (2 bytes) */
158 autodetect->rttMeasureStartTime = GetTickCount64();
159 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
160 sec_flags | SEC_AUTODETECT_REQ);
161}
162
163static BOOL autodetect_send_rtt_measure_response(rdpAutoDetect* autodetect, UINT16 sequenceNumber)
164{
165 UINT16 sec_flags = 0;
166 wStream* s = nullptr;
167
168 WINPR_ASSERT(autodetect);
169 WINPR_ASSERT(autodetect->context);
170
171 /* Send the response PDU to the server */
172 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
173
174 if (!s)
175 return FALSE;
176
177 WLog_Print(autodetect->log, WLOG_TRACE,
178 "sending RTT Measure Response PDU (seqNumber=0x%" PRIx16 ")", sequenceNumber);
179 Stream_Write_UINT8(s, 0x06); /* headerLength (1 byte) */
180 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE); /* headerTypeId (1 byte) */
181 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
182 Stream_Write_UINT16(s, RDP_RTT_RESPONSE_TYPE); /* responseType (1 byte) */
183 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
184 sec_flags | SEC_AUTODETECT_RSP);
185}
186
187static BOOL autodetect_send_netchar_sync(rdpAutoDetect* autodetect, UINT16 sequenceNumber)
188{
189 UINT16 sec_flags = 0;
190
191 WINPR_ASSERT(autodetect);
192 WINPR_ASSERT(autodetect->context);
193
194 wStream* s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
195 if (!s)
196 return FALSE;
197
198 WLog_Print(autodetect->log, WLOG_TRACE,
199 "sending Network Characteristics Sync PDU -> bandwidth=%" PRIu32 ", rtt=%" PRIu32,
200 autodetect->netCharBandwidth, autodetect->netCharAverageRTT);
201 Stream_Write_UINT8(s, 0x0E); /* headerLength (1 byte) */
202 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE); /* headerTypeId (1 byte) */
203 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
204 Stream_Write_UINT16(s, RDP_NETCHAR_SYNC_RESPONSE_TYPE); /* responseType (2 bytes) */
205 Stream_Write_UINT32(s, autodetect->netCharBandwidth); /* bandwidth (4 bytes) */
206 Stream_Write_UINT32(s, autodetect->netCharAverageRTT); /* rtt (4 bytes) */
207 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
208 sec_flags | SEC_AUTODETECT_RSP);
209}
210
211static BOOL autodetect_send_bandwidth_measure_start(rdpAutoDetect* autodetect,
212 RDP_TRANSPORT_TYPE transport,
213 UINT16 sequenceNumber)
214{
215 UINT16 requestType = 0;
216 UINT16 sec_flags = 0;
217 wStream* s = nullptr;
218
219 WINPR_ASSERT(autodetect);
220 WINPR_ASSERT(autodetect->context);
221
222#if defined(WITH_RDPEUDP_SERVER)
223 BOOL sent = FALSE;
224 if (multitransport_server_send_autodetect(autodetect->context->rdp->multitransport, transport,
225 sequenceNumber, RDP_BW_START_REQUEST_TYPE_CONTINUOUS,
226 &sent))
227 return sent;
228#else
229 if (transport != RDP_TRANSPORT_TCP)
230 {
231 WLog_Print(autodetect->log, WLOG_ERROR,
232 "sending Bandwidth Measure Start PDU -> unsupported transport %s, abort",
233 rdp_transport_type_string(transport));
234 return FALSE;
235 }
236#endif
237
238 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
239 if (!s)
240 return FALSE;
241
242 if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
243 requestType = RDP_BW_START_REQUEST_TYPE_CONNECTTIME;
244 else
245 requestType = RDP_BW_START_REQUEST_TYPE_CONTINUOUS;
246
247 WLog_Print(autodetect->log, WLOG_TRACE,
248 "sending Bandwidth Measure Start PDU(seqNumber=%" PRIu16 ")", sequenceNumber);
249 Stream_Write_UINT8(s, 0x06); /* headerLength (1 byte) */
250 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
251 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
252 Stream_Write_UINT16(s, requestType); /* requestType (2 bytes) */
253 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
254 sec_flags | SEC_AUTODETECT_REQ);
255}
256
257static BOOL
258autodetect_send_bandwidth_measure_payload(rdpAutoDetect* autodetect,
259 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
260 UINT16 sequenceNumber, UINT16 payloadLength)
261{
262 UINT16 sec_flags = 0;
263 wStream* s = nullptr;
264
265 WINPR_ASSERT(autodetect);
266 WINPR_ASSERT(autodetect->context);
267
268 WINPR_ASSERT(freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE);
269
270 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
271 if (!s)
272 return FALSE;
273
274 WLog_Print(autodetect->log, WLOG_TRACE,
275 "sending Bandwidth Measure Payload PDU -> payloadLength=%" PRIu16 "", payloadLength);
276 /* 4-bytes aligned */
277 payloadLength &= ~3;
278
279 if (!Stream_EnsureRemainingCapacity(s, 8ull + payloadLength))
280 {
281 WLog_Print(autodetect->log, WLOG_ERROR, "Failed to ensure %lu bytes in stream",
282 8ul + payloadLength);
283 Stream_Release(s);
284 return FALSE;
285 }
286
287 Stream_Write_UINT8(s, 0x08); /* headerLength (1 byte) */
288 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
289 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
290 Stream_Write_UINT16(s, RDP_BW_PAYLOAD_REQUEST_TYPE); /* requestType (2 bytes) */
291 Stream_Write_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
292 /* Random data (better measurement in case the line is compressed) */
293 if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
294 {
295 Stream_Release(s);
296 return FALSE;
297 }
298 Stream_Seek(s, payloadLength);
299 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
300 sec_flags | SEC_AUTODETECT_REQ);
301}
302
303static BOOL autodetect_send_bandwidth_measure_stop(rdpAutoDetect* autodetect,
304 RDP_TRANSPORT_TYPE transport,
305 UINT16 sequenceNumber, UINT16 payloadLength)
306{
307 UINT16 requestType = 0;
308 UINT16 sec_flags = 0;
309 wStream* s = nullptr;
310
311 WINPR_ASSERT(autodetect);
312 WINPR_ASSERT(autodetect->context);
313
314#if defined(WITH_RDPEUDP_SERVER)
315 BOOL sent = FALSE;
316 if (multitransport_server_send_autodetect(autodetect->context->rdp->multitransport, transport,
317 sequenceNumber, RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS,
318 &sent))
319 return sent;
320#else
321 if (transport != RDP_TRANSPORT_TCP)
322 {
323 WLog_Print(autodetect->log, WLOG_ERROR,
324 "sending Bandwidth Measure Stop PDU -> unsupported transport %s, abort",
325 rdp_transport_type_string(transport));
326 return FALSE;
327 }
328#endif
329
330 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
331 if (!s)
332 return FALSE;
333
334 if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
335 requestType = RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME;
336 else
337 requestType = RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS;
338
339 if (requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
340 payloadLength = 0;
341
342 WLog_Print(autodetect->log, WLOG_TRACE,
343 "sending Bandwidth Measure Stop PDU -> payloadLength=%" PRIu16 "", payloadLength);
344 /* 4-bytes aligned */
345 payloadLength &= ~3;
346 Stream_Write_UINT8(s, requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME
347 ? 0x08
348 : 0x06); /* headerLength (1 byte) */
349 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
350 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
351 Stream_Write_UINT16(s, requestType); /* requestType (2 bytes) */
352
353 if (requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
354 {
355 Stream_Write_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
356
357 if (payloadLength > 0)
358 {
359 if (!Stream_EnsureRemainingCapacity(s, payloadLength))
360 {
361 WLog_Print(autodetect->log, WLOG_ERROR,
362 "Failed to ensure %" PRIu16 " bytes in stream", payloadLength);
363 Stream_Release(s);
364 return FALSE;
365 }
366
367 /* Random data (better measurement in case the line is compressed) */
368 if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
369 {
370 Stream_Release(s);
371 return FALSE;
372 }
373 Stream_Seek(s, payloadLength);
374 }
375 }
376
377 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
378 sec_flags | SEC_AUTODETECT_REQ);
379}
380
381/* [MS-RDPBCGR] 2.2.14.2.2 Bandwidth Measure Results (RDP_BW_RESULTS), 14 bytes */
382static void autodetect_write_bandwidth_measure_results(const rdpAutoDetect* autodetect, wStream* s,
383 UINT16 responseType, UINT16 sequenceNumber,
384 UINT32 timeDelta)
385{
386 WINPR_ASSERT(autodetect);
387 WINPR_ASSERT(s);
388
389 Stream_Write_UINT8(s, 0x0E); /* headerLength (1 byte) */
390 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE); /* headerTypeId (1 byte) */
391 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
392 Stream_Write_UINT16(s, responseType); /* responseType (2 bytes) */
393 Stream_Write_UINT32(s, timeDelta); /* timeDelta (4 bytes) */
394 Stream_Write_UINT32(s, autodetect->bandwidthMeasureByteCount); /* byteCount (4 bytes) */
395}
396
397static BOOL autodetect_send_bandwidth_measure_results(rdpAutoDetect* autodetect,
398 RDP_TRANSPORT_TYPE transport,
399 UINT16 responseType, UINT16 sequenceNumber)
400{
401 BOOL success = TRUE;
402 UINT16 sec_flags = 0;
403 UINT64 timeDelta = GetTickCount64();
404
405 WINPR_ASSERT(autodetect);
406 WINPR_ASSERT(autodetect->context);
407
408 /* Compute the total time */
409 if (autodetect->bandwidthMeasureStartTime > timeDelta)
410 {
411 WLog_Print(autodetect->log, WLOG_WARN,
412 "Invalid bandwidthMeasureStartTime %" PRIu64 " > current %" PRIu64
413 ", trimming to 0",
414 autodetect->bandwidthMeasureStartTime, timeDelta);
415 timeDelta = 0;
416 }
417 else
418 timeDelta -= autodetect->bandwidthMeasureStartTime;
419
420 const UINT32 delta = (UINT32)MIN(timeDelta, UINT32_MAX);
421 WLog_Print(autodetect->log, WLOG_TRACE,
422 "sending Bandwidth Measure Results PDU -> timeDelta=%" PRIu32 ", byteCount=%" PRIu32
423 "%s",
424 delta, autodetect->bandwidthMeasureByteCount,
425 (transport == RDP_TRANSPORT_UDP_R) ? " in the UDP tunnel" : "");
426
427 IFCALLRET(autodetect->ClientBandwidthMeasureResult, success, autodetect, transport,
428 responseType, sequenceNumber, delta, autodetect->bandwidthMeasureByteCount);
429 if (!success)
430 {
431 WLog_Print(autodetect->log, WLOG_ERROR, "ClientBandwidthMeasureResult failed");
432 return FALSE;
433 }
434
435 /* [MS-RDPBCGR] 3.2.5.17: a measurement stopped in a tunnel subheader is answered in one on
436 * the same tunnel ([MS-RDPEMT] 2.2.1.1.1), all others on the main channel */
437 if (transport == RDP_TRANSPORT_UDP_R)
438 {
439 BYTE buffer[0x0E] = WINPR_C_ARRAY_INIT;
440 wStream sbuffer = WINPR_C_ARRAY_INIT;
441 wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
442 autodetect_write_bandwidth_measure_results(autodetect, s, responseType, sequenceNumber,
443 delta);
444 return multitransport_send_autodetect_response(autodetect->context->rdp->multitransport,
445 buffer, sizeof(buffer));
446 }
447
448 wStream* s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
449 if (!s)
450 return FALSE;
451 autodetect_write_bandwidth_measure_results(autodetect, s, responseType, sequenceNumber, delta);
452 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
453 sec_flags | SEC_AUTODETECT_RSP);
454}
455
456static BOOL autodetect_send_netchar_result(rdpAutoDetect* autodetect,
457 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
458 UINT16 sequenceNumber,
459 const rdpNetworkCharacteristicsResult* result)
460{
461 UINT16 sec_flags = 0;
462 wStream* s = nullptr;
463
464 WINPR_ASSERT(autodetect);
465 WINPR_ASSERT(autodetect->context);
466
467 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
468
469 if (!s)
470 return FALSE;
471
472 WLog_Print(autodetect->log, WLOG_TRACE, "sending Network Characteristics Result PDU");
473
474 switch (result->type)
475 {
476 case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT:
477 Stream_Write_UINT8(s, 0x0E); /* headerLength (1 byte) */
478 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
479 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
480 WINPR_ASSERT((result->type <= UINT16_MAX));
481 WINPR_ASSERT((result->type >= 0));
482 Stream_Write_UINT16(s, (UINT16)result->type); /* requestType (2 bytes) */
483 Stream_Write_UINT32(s, result->baseRTT); /* baseRTT (4 bytes) */
484 Stream_Write_UINT32(s, result->averageRTT); /* averageRTT (4 bytes) */
485 break;
486 case RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT:
487 Stream_Write_UINT8(s, 0x0E); /* headerLength (1 byte) */
488 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
489 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
490 WINPR_ASSERT((result->type <= UINT16_MAX));
491 WINPR_ASSERT((result->type >= 0));
492 Stream_Write_UINT16(s, (UINT16)result->type); /* requestType (2 bytes) */
493 Stream_Write_UINT32(s, result->bandwidth); /* bandwidth (4 bytes) */
494 Stream_Write_UINT32(s, result->averageRTT); /* averageRTT (4 bytes) */
495 break;
496 case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT:
497 Stream_Write_UINT8(s, 0x12); /* headerLength (1 byte) */
498 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
499 Stream_Write_UINT16(s, sequenceNumber); /* sequenceNumber (2 bytes) */
500 WINPR_ASSERT((result->type <= UINT16_MAX));
501 WINPR_ASSERT((result->type >= 0));
502 Stream_Write_UINT16(s, (UINT16)result->type); /* requestType (2 bytes) */
503 Stream_Write_UINT32(s, result->baseRTT); /* baseRTT (4 bytes) */
504 Stream_Write_UINT32(s, result->bandwidth); /* bandwidth (4 bytes) */
505 Stream_Write_UINT32(s, result->averageRTT); /* averageRTT (4 bytes) */
506 break;
507 default:
508 WINPR_ASSERT(FALSE);
509 break;
510 }
511
512 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
513 sec_flags | SEC_AUTODETECT_REQ);
514}
515
516static FREERDP_AUTODETECT_STATE
517autodetect_on_connect_time_auto_detect_begin_default(rdpAutoDetect* autodetect)
518{
519 WINPR_ASSERT(autodetect);
520 WINPR_ASSERT(autodetect->RTTMeasureRequest);
521
522 if (!autodetect->RTTMeasureRequest(autodetect, RDP_TRANSPORT_TCP, 0x23))
523 return FREERDP_AUTODETECT_STATE_FAIL;
524
525 return FREERDP_AUTODETECT_STATE_REQUEST;
526}
527
528static FREERDP_AUTODETECT_STATE
529autodetect_on_connect_time_auto_detect_progress_default(rdpAutoDetect* autodetect)
530{
531 WINPR_ASSERT(autodetect);
532
533 if (autodetect->state == FREERDP_AUTODETECT_STATE_RESPONSE ||
534 autodetect->state == FREERDP_AUTODETECT_STATE_COMPLETE)
535 return FREERDP_AUTODETECT_STATE_COMPLETE;
536
537 return autodetect->state;
538}
539
540static BOOL autodetect_recv_rtt_measure_request(rdpAutoDetect* autodetect,
541 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
542 WINPR_ATTR_UNUSED wStream* s,
543 const AUTODETECT_REQ_PDU* autodetectReqPdu)
544{
545 WINPR_ASSERT(autodetect);
546 WINPR_ASSERT(s);
547 WINPR_ASSERT(autodetectReqPdu);
548
549 if (autodetectReqPdu->headerLength != 0x06)
550 {
551 WLog_Print(autodetect->log, WLOG_ERROR,
552 "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
553 autodetectReqPdu->headerLength);
554 return FALSE;
555 }
556
557 WLog_Print(autodetect->log, WLOG_TRACE, "received RTT Measure Request PDU");
558 /* Send a response to the server */
559 return autodetect_send_rtt_measure_response(autodetect, autodetectReqPdu->sequenceNumber);
560}
561
562static BOOL autodetect_recv_rtt_measure_response(rdpAutoDetect* autodetect,
563 RDP_TRANSPORT_TYPE transport,
564 WINPR_ATTR_UNUSED wStream* s,
565 const AUTODETECT_RSP_PDU* autodetectRspPdu)
566{
567 BOOL success = TRUE;
568
569 WINPR_ASSERT(autodetect);
570 WINPR_ASSERT(autodetectRspPdu);
571
572 if (autodetectRspPdu->headerLength != 0x06)
573 {
574 WLog_Print(autodetect->log, WLOG_ERROR,
575 "autodetectRspPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
576 autodetectRspPdu->headerLength);
577 return FALSE;
578 }
579
580 WLog_Print(autodetect->log, WLOG_TRACE, "received RTT Measure Response PDU");
581 autodetect->netCharAverageRTT =
582 (UINT32)MIN(GetTickCount64() - autodetect->rttMeasureStartTime, UINT32_MAX);
583
584 if (autodetect->netCharBaseRTT == 0 ||
585 autodetect->netCharBaseRTT > autodetect->netCharAverageRTT)
586 autodetect->netCharBaseRTT = autodetect->netCharAverageRTT;
587
588 IFCALLRET(autodetect->RTTMeasureResponse, success, autodetect, transport,
589 autodetectRspPdu->sequenceNumber);
590 if (!success)
591 WLog_Print(autodetect->log, WLOG_WARN, "RTTMeasureResponse failed");
592 return success;
593}
594
595static BOOL autodetect_recv_bandwidth_measure_start(rdpAutoDetect* autodetect,
596 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
597 WINPR_ATTR_UNUSED wStream* s,
598 const AUTODETECT_REQ_PDU* autodetectReqPdu)
599{
600 WINPR_ASSERT(autodetect);
601 WINPR_ASSERT(s);
602 WINPR_ASSERT(autodetectReqPdu);
603
604 if (autodetectReqPdu->headerLength != 0x06)
605 {
606 WLog_Print(autodetect->log, WLOG_ERROR,
607 "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
608 autodetectReqPdu->headerLength);
609 return FALSE;
610 }
611
612 WLog_Print(autodetect->log, WLOG_TRACE,
613 "received Bandwidth Measure Start PDU - time=%" PRIu64 "", GetTickCount64());
614 /* Initialize bandwidth measurement parameters */
615 autodetect->bandwidthMeasureStartTime = GetTickCount64();
616 autodetect->bandwidthMeasureByteCount = 0;
617
618 /* [MS-RDPBCGR] 3.2.5.17: an auto-reconnect can answer the connect-time measurement with
619 * the bandwidth and RTT the server reported before, which ends the measurement
620 * ([MS-RDPBCGR] 2.2.14.2.3). */
621 if ((autodetectReqPdu->requestType == RDP_BW_START_REQUEST_TYPE_CONNECTTIME) &&
622 (autodetect->netCharBandwidth > 0))
623 {
625 autodetect->context->settings, FreeRDP_ServerAutoReconnectCookie);
626 if (cookie && (cookie->cbLen > 0))
627 return autodetect_send_netchar_sync(autodetect, autodetectReqPdu->sequenceNumber);
628 }
629
630 /* Continuous Auto-Detection: mark the start of the measurement */
631 if (autodetectReqPdu->requestType == RDP_BW_START_REQUEST_TYPE_CONTINUOUS)
632 {
633 autodetect->bandwidthMeasureStarted = TRUE;
634 }
635
636 return TRUE;
637}
638
639static BOOL
640autodetect_recv_bandwidth_measure_payload(rdpAutoDetect* autodetect,
641 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
642 wStream* s, const AUTODETECT_REQ_PDU* autodetectReqPdu)
643{
644 UINT16 payloadLength = 0;
645
646 WINPR_ASSERT(autodetect);
647 WINPR_ASSERT(s);
648 WINPR_ASSERT(autodetectReqPdu);
649
650 if (autodetectReqPdu->headerLength != 0x08)
651 {
652 WLog_Print(autodetect->log, WLOG_ERROR,
653 "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8 "]",
654 autodetectReqPdu->headerLength);
655 return FALSE;
656 }
657
658 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
659 return FALSE;
660
661 Stream_Read_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
662 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
663 return FALSE;
664 Stream_Seek(s, payloadLength);
665
666 WLog_Print(autodetect->log, WLOG_DEBUG,
667 "received Bandwidth Measure Payload PDU -> payloadLength=%" PRIu16 "",
668 payloadLength);
669 /* Add the message to the bandwidth measurement parameters: Windows counts the whole
670 * Bandwidth Measure Payload message, header included (mstsc reports 4000 bytes for a
671 * 3992 byte payload), and ends the connect-time detection with a 5 s delay otherwise */
672 autodetect->bandwidthMeasureByteCount += autodetectReqPdu->headerLength;
673 autodetect->bandwidthMeasureByteCount += payloadLength;
674 return TRUE;
675}
676
677static BOOL autodetect_recv_bandwidth_measure_stop(rdpAutoDetect* autodetect,
678 RDP_TRANSPORT_TYPE transport, wStream* s,
679 const AUTODETECT_REQ_PDU* autodetectReqPdu)
680{
681 UINT16 payloadLength = 0;
682 UINT16 responseType = 0;
683
684 WINPR_ASSERT(autodetect);
685 WINPR_ASSERT(s);
686 WINPR_ASSERT(autodetectReqPdu);
687
688 if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
689 {
690 if (autodetectReqPdu->headerLength != 0x08)
691 {
692 WLog_Print(autodetect->log, WLOG_ERROR,
693 "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8 "]",
694 autodetectReqPdu->headerLength);
695 return FALSE;
696 }
697
698 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
699 return FALSE;
700
701 Stream_Read_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
702 }
703 else
704 {
705 if (autodetectReqPdu->headerLength != 0x06)
706 {
707 WLog_Print(autodetect->log, WLOG_ERROR,
708 "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
709 autodetectReqPdu->headerLength);
710 return FALSE;
711 }
712
713 payloadLength = 0;
714 }
715
716 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
717 return FALSE;
718 Stream_Seek(s, payloadLength);
719
720 WLog_Print(autodetect->log, WLOG_TRACE,
721 "received Bandwidth Measure Stop PDU -> payloadLength=%" PRIu16 "", payloadLength);
722 /* Add the payload to the bandwidth measurement parameters, with its header as for a
723 * Bandwidth Measure Payload message (see autodetect_recv_bandwidth_measure_payload) */
724 if (payloadLength > 0)
725 {
726 autodetect->bandwidthMeasureByteCount += autodetectReqPdu->headerLength;
727 autodetect->bandwidthMeasureByteCount += payloadLength;
728 }
729
730 /* Continuous Auto-Detection: mark the stop of the measurement */
731 if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
732 {
733 autodetect->bandwidthMeasureStarted = FALSE;
734 }
735
736 /* Send a response the server */
737 responseType = autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME
738 ? RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME
739 : RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS;
740 return autodetect_send_bandwidth_measure_results(autodetect, transport, responseType,
741 autodetectReqPdu->sequenceNumber);
742}
743
744static BOOL autodetect_recv_bandwidth_measure_results(rdpAutoDetect* autodetect,
745 RDP_TRANSPORT_TYPE transport, wStream* s,
746 const AUTODETECT_RSP_PDU* autodetectRspPdu)
747{
748 UINT32 timeDelta = 0;
749 UINT32 byteCount = 0;
750 BOOL success = TRUE;
751
752 WINPR_ASSERT(autodetect);
753 WINPR_ASSERT(s);
754 WINPR_ASSERT(autodetectRspPdu);
755
756 if (autodetectRspPdu->headerLength != 0x0E)
757 {
758 WLog_Print(autodetect->log, WLOG_ERROR,
759 "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
760 autodetectRspPdu->headerLength);
761 return FALSE;
762 }
763
764 WLog_Print(autodetect->log, WLOG_TRACE, "received Bandwidth Measure Results PDU");
765 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
766 return FALSE;
767 Stream_Read_UINT32(s, timeDelta); /* timeDelta (4 bytes) */
768 Stream_Read_UINT32(s, byteCount); /* byteCount (4 bytes) */
769
770 IFCALLRET(autodetect->BandwidthMeasureResults, success, autodetect, transport,
771 autodetectRspPdu->sequenceNumber, autodetectRspPdu->responseType, timeDelta,
772 byteCount);
773 if (!success)
774 WLog_Print(autodetect->log, WLOG_WARN, "BandwidthMeasureResults failed");
775 return success;
776}
777
778static BOOL autodetect_recv_netchar_sync(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
779 wStream* s, const AUTODETECT_RSP_PDU* autodetectRspPdu)
780{
781 UINT32 bandwidth = 0;
782 UINT32 rtt = 0;
783 BOOL success = TRUE;
784
785 WINPR_ASSERT(autodetect);
786 WINPR_ASSERT(s);
787 WINPR_ASSERT(autodetectRspPdu);
788
789 if (autodetectRspPdu->headerLength != 0x0E)
790 {
791 WLog_Print(autodetect->log, WLOG_ERROR,
792 "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
793 autodetectRspPdu->headerLength);
794 return FALSE;
795 }
796 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
797 return FALSE;
798
799 /* bandwidth and averageRTT fields are present (baseRTT field is not) */
800 Stream_Read_UINT32(s, bandwidth); /* bandwidth (4 bytes) */
801 Stream_Read_UINT32(s, rtt); /* rtt (4 bytes) */
802
803 WLog_Print(autodetect->log, WLOG_TRACE,
804 "received Network Characteristics Sync PDU -> bandwidth=%" PRIu32 ", rtt=%" PRIu32
805 "",
806 bandwidth, rtt);
807
808 IFCALLRET(autodetect->NetworkCharacteristicsSync, success, autodetect, transport,
809 autodetectRspPdu->sequenceNumber, bandwidth, rtt);
810 if (!success)
811 WLog_Print(autodetect->log, WLOG_WARN, "NetworkCharacteristicsSync failed");
812 return success;
813}
814
815static BOOL autodetect_recv_netchar_request(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
816 wStream* s, const AUTODETECT_REQ_PDU* autodetectReqPdu)
817{
818 rdpNetworkCharacteristicsResult result = {
819 .type = RDP_NETCHAR_RESERVED, .baseRTT = 0, .averageRTT = 0, .bandwidth = 0
820 };
821 BOOL success = TRUE;
822
823 WINPR_ASSERT(autodetect);
824 WINPR_ASSERT(s);
825 WINPR_ASSERT(autodetectReqPdu);
826
827 switch (autodetectReqPdu->requestType)
828 {
829 case RDP_NETCHAR_RESULTS_0x0840:
830
831 /* baseRTT and averageRTT fields are present (bandwidth field is not) */
832 if (autodetectReqPdu->headerLength != 0x0E)
833 {
834 WLog_Print(autodetect->log, WLOG_ERROR,
835 "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
836 autodetectReqPdu->headerLength);
837 return FALSE;
838 }
839 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
840 return FALSE;
841
842 result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT;
843 Stream_Read_UINT32(s, result.baseRTT); /* baseRTT (4 bytes) */
844 Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
845 autodetect->netCharBaseRTT = result.baseRTT;
846 break;
847
848 case RDP_NETCHAR_RESULTS_0x0880:
849
850 /* bandwidth and averageRTT fields are present (baseRTT field is not) */
851 if (autodetectReqPdu->headerLength != 0x0E)
852 {
853 WLog_Print(autodetect->log, WLOG_ERROR,
854 "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
855 autodetectReqPdu->headerLength);
856 return FALSE;
857 }
858 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
859 return FALSE;
860
861 result.type = RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT;
862 Stream_Read_UINT32(s, result.bandwidth); /* bandwidth (4 bytes) */
863 Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
864 autodetect->netCharBandwidth = result.bandwidth;
865 break;
866
867 case RDP_NETCHAR_RESULTS_0x08C0:
868
869 /* baseRTT, bandwidth, and averageRTT fields are present */
870 if (autodetectReqPdu->headerLength != 0x12)
871 {
872 WLog_Print(autodetect->log, WLOG_ERROR,
873 "autodetectReqPdu->headerLength != 0x012 [0x%02" PRIx8 "]",
874 autodetectReqPdu->headerLength);
875 return FALSE;
876 }
877 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 12))
878 return FALSE;
879
880 result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT;
881 Stream_Read_UINT32(s, result.baseRTT); /* baseRTT (4 bytes) */
882 Stream_Read_UINT32(s, result.bandwidth); /* bandwidth (4 bytes) */
883 Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
884 autodetect->netCharBaseRTT = result.baseRTT;
885 autodetect->netCharBandwidth = result.bandwidth;
886 break;
887
888 default:
889 WINPR_ASSERT(FALSE);
890 break;
891 }
892 /* [MS-RDPBCGR] 3.2.5.17: the metrics are kept for a Network Characteristics Sync */
893 autodetect->netCharAverageRTT = result.averageRTT;
894
895 WLog_Print(autodetect->log, WLOG_TRACE,
896 "received Network Characteristics Result PDU -> baseRTT=%" PRIu32
897 ", bandwidth=%" PRIu32 ", averageRTT=%" PRIu32 "",
898 result.baseRTT, result.bandwidth, result.averageRTT);
899
900 IFCALLRET(autodetect->NetworkCharacteristicsResult, success, autodetect, transport,
901 autodetectReqPdu->sequenceNumber, &result);
902 if (!success)
903 WLog_Print(autodetect->log, WLOG_WARN, "NetworkCharacteristicsResult failed");
904 return success;
905}
906
907state_run_t autodetect_recv_request_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
908 wStream* s)
909{
910 AUTODETECT_REQ_PDU autodetectReqPdu = WINPR_C_ARRAY_INIT;
911 const rdpSettings* settings = nullptr;
912 BOOL success = FALSE;
913
914 WINPR_ASSERT(autodetect);
915 WINPR_ASSERT(autodetect->context);
916
917 settings = autodetect->context->settings;
918 WINPR_ASSERT(settings);
919
920 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
921 return STATE_RUN_FAILED;
922
923 Stream_Read_UINT8(s, autodetectReqPdu.headerLength); /* headerLength (1 byte) */
924 Stream_Read_UINT8(s, autodetectReqPdu.headerTypeId); /* headerTypeId (1 byte) */
925 Stream_Read_UINT16(s, autodetectReqPdu.sequenceNumber); /* sequenceNumber (2 bytes) */
926 Stream_Read_UINT16(s, autodetectReqPdu.requestType); /* requestType (2 bytes) */
927
928 if (WLog_IsLevelActive(autodetect->log, WLOG_TRACE))
929 {
930 char rbuffer[128] = WINPR_C_ARRAY_INIT;
931 const char* requestTypeStr = autodetect_request_type_to_string_buffer(
932 autodetectReqPdu.requestType, rbuffer, sizeof(rbuffer));
933
934 char hbuffer[128] = WINPR_C_ARRAY_INIT;
935 const char* headerStr =
936 autodetect_header_type_string(autodetectReqPdu.headerTypeId, hbuffer, sizeof(hbuffer));
937
938 WLog_Print(autodetect->log, WLOG_TRACE,
939 "rdp_recv_autodetect_request_packet: headerLength=%" PRIu8
940 ", headerTypeId=%s, sequenceNumber=%" PRIu16 ", requestType=%s",
941 autodetectReqPdu.headerLength, headerStr, autodetectReqPdu.sequenceNumber,
942 requestTypeStr);
943 }
944
945 if (!freerdp_settings_get_bool(settings, FreeRDP_NetworkAutoDetect))
946 {
947 char rbuffer[128] = WINPR_C_ARRAY_INIT;
948 const char* requestTypeStr = autodetect_request_type_to_string_buffer(
949 autodetectReqPdu.requestType, rbuffer, sizeof(rbuffer));
950
951 WLog_Print(autodetect->log, WLOG_WARN,
952 "Received a [MS-RDPBCGR] 2.2.14.1.1 RTT Measure Request [%s] "
953 "message but support was not enabled",
954 requestTypeStr);
955 goto fail;
956 }
957
958 if (autodetectReqPdu.headerTypeId != TYPE_ID_AUTODETECT_REQUEST)
959 {
960 char rbuffer[128] = WINPR_C_ARRAY_INIT;
961 const char* requestTypeStr = autodetect_request_type_to_string_buffer(
962 autodetectReqPdu.requestType, rbuffer, sizeof(rbuffer));
963 char hbuffer[128] = WINPR_C_ARRAY_INIT;
964 const char* headerStr =
965 autodetect_header_type_string(autodetectReqPdu.headerTypeId, hbuffer, sizeof(hbuffer));
966
967 WLog_Print(autodetect->log, WLOG_ERROR,
968 "Received a [MS-RDPBCGR] 2.2.14.1.1 RTT Measure Request [%s] "
969 "message with invalid headerTypeId=%s",
970 requestTypeStr, headerStr);
971 goto fail;
972 }
973
974 if (!IFCALLRESULT(TRUE, autodetect->RequestReceived, autodetect, transport,
975 autodetectReqPdu.requestType, autodetectReqPdu.sequenceNumber))
976 goto fail;
977
978 switch (autodetectReqPdu.requestType)
979 {
980 case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
981 case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
982 /* RTT Measure Request (RDP_RTT_REQUEST) - MS-RDPBCGR 2.2.14.1.1 */
983 success =
984 autodetect_recv_rtt_measure_request(autodetect, transport, s, &autodetectReqPdu);
985 break;
986
987 case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
988 case RDP_BW_START_REQUEST_TYPE_TUNNEL:
989 case RDP_BW_START_REQUEST_TYPE_CONNECTTIME:
990 /* Bandwidth Measure Start (RDP_BW_START) - MS-RDPBCGR 2.2.14.1.2 */
991 success = autodetect_recv_bandwidth_measure_start(autodetect, transport, s,
992 &autodetectReqPdu);
993 break;
994
995 case RDP_BW_PAYLOAD_REQUEST_TYPE:
996 /* Bandwidth Measure Payload (RDP_BW_PAYLOAD) - MS-RDPBCGR 2.2.14.1.3 */
997 success = autodetect_recv_bandwidth_measure_payload(autodetect, transport, s,
998 &autodetectReqPdu);
999 break;
1000
1001 case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
1002 case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
1003 case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
1004 /* Bandwidth Measure Stop (RDP_BW_STOP) - MS-RDPBCGR 2.2.14.1.4 */
1005 success =
1006 autodetect_recv_bandwidth_measure_stop(autodetect, transport, s, &autodetectReqPdu);
1007 break;
1008
1009 case RDP_NETCHAR_RESULTS_0x0840:
1010 case RDP_NETCHAR_RESULTS_0x0880:
1011 case RDP_NETCHAR_RESULTS_0x08C0:
1012 /* Network Characteristics Result (RDP_NETCHAR_RESULT) - MS-RDPBCGR 2.2.14.1.5 */
1013 success = autodetect_recv_netchar_request(autodetect, transport, s, &autodetectReqPdu);
1014 break;
1015
1016 default:
1017 WLog_Print(autodetect->log, WLOG_ERROR, "Unknown requestType=0x%04" PRIx16,
1018 autodetectReqPdu.requestType);
1019 break;
1020 }
1021
1022fail:
1023 if (success)
1024 autodetect->state = FREERDP_AUTODETECT_STATE_REQUEST;
1025 else
1026 autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
1027 return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
1028}
1029
1030state_run_t autodetect_recv_tunnel_request(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
1031 wStream* s)
1032{
1033 WINPR_ASSERT(autodetect);
1034 WINPR_ASSERT(s);
1035
1036 /* [MS-RDPBCGR] 1.3.9: Continuous Auto-Detection over a sideband channel sends Bandwidth
1037 * Measure Start, Bandwidth Measure Stop and Network Characteristics Result. The result needs
1038 * no answer, a Stop on the reliable tunnel is answered on it ([MS-RDPBCGR] 3.2.5.17). Start
1039 * and Stop of a lossy tunnel (0x0114, 0x0629) are not, there is none. */
1040 UINT16 requestType = 0;
1041 if (Stream_GetRemainingLength(s) >= 6)
1042 {
1043 const BYTE* header = Stream_ConstPointer(s);
1044 requestType = winpr_Data_Get_UINT16(&header[4]);
1045 }
1046
1047 switch (requestType)
1048 {
1049 case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
1050 case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
1051 case RDP_NETCHAR_RESULTS_0x0840:
1052 case RDP_NETCHAR_RESULTS_0x0880:
1053 case RDP_NETCHAR_RESULTS_0x08C0:
1054 return autodetect_recv_request_packet(autodetect, transport, s);
1055
1056 default:
1057 WLog_Print(autodetect->log, WLOG_DEBUG,
1058 "ignoring auto-detect request 0x%04" PRIx16 " in a tunnel subheader",
1059 requestType);
1060 return STATE_RUN_SUCCESS;
1061 }
1062}
1063
1064state_run_t autodetect_recv_tunnel_response(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
1065 wStream* s)
1066{
1067 WINPR_ASSERT(autodetect);
1068 WINPR_ASSERT(s);
1069
1070 /* The server only sends Bandwidth Measure Start/Stop over the tunnel, so only results
1071 * are expected. Do not pass responses to the application on a transport not used for
1072 * the request. */
1073 UINT16 responseType = 0;
1074 if (Stream_GetRemainingLength(s) >= 6)
1075 {
1076 const BYTE* header = Stream_ConstPointer(s);
1077 responseType = winpr_Data_Get_UINT16(&header[4]);
1078 }
1079
1080 if (responseType != RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS)
1081 {
1082 WLog_Print(autodetect->log, WLOG_DEBUG,
1083 "discarding auto-detect response 0x%04" PRIx16 " in a tunnel subheader",
1084 responseType);
1085 return STATE_RUN_SUCCESS;
1086 }
1087 return autodetect_recv_response_packet(autodetect, transport, s);
1088}
1089
1090state_run_t autodetect_recv_response_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
1091 wStream* s)
1092{
1093 AUTODETECT_RSP_PDU autodetectRspPdu = WINPR_C_ARRAY_INIT;
1094 const rdpSettings* settings = nullptr;
1095 BOOL success = FALSE;
1096
1097 WINPR_ASSERT(autodetect);
1098 WINPR_ASSERT(autodetect->context);
1099 WINPR_ASSERT(s);
1100
1101 settings = autodetect->context->settings;
1102 WINPR_ASSERT(settings);
1103
1104 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
1105 goto fail;
1106
1107 Stream_Read_UINT8(s, autodetectRspPdu.headerLength); /* headerLength (1 byte) */
1108 Stream_Read_UINT8(s, autodetectRspPdu.headerTypeId); /* headerTypeId (1 byte) */
1109 Stream_Read_UINT16(s, autodetectRspPdu.sequenceNumber); /* sequenceNumber (2 bytes) */
1110 Stream_Read_UINT16(s, autodetectRspPdu.responseType); /* responseType (2 bytes) */
1111
1112 if (WLog_IsLevelActive(autodetect->log, WLOG_TRACE))
1113 {
1114 char rbuffer[128] = WINPR_C_ARRAY_INIT;
1115 const char* requestStr = autodetect_request_type_to_string_buffer(
1116 autodetectRspPdu.responseType, rbuffer, sizeof(rbuffer));
1117 char hbuffer[128] = WINPR_C_ARRAY_INIT;
1118 const char* headerStr =
1119 autodetect_header_type_string(autodetectRspPdu.headerTypeId, hbuffer, sizeof(hbuffer));
1120
1121 WLog_Print(autodetect->log, WLOG_TRACE,
1122 "rdp_recv_autodetect_response_packet: headerLength=%" PRIu8 ", headerTypeId=%s"
1123 ", sequenceNumber=%" PRIu16 ", requestType=%s",
1124 autodetectRspPdu.headerLength, headerStr, autodetectRspPdu.sequenceNumber,
1125 requestStr);
1126 }
1127
1128 if (!freerdp_settings_get_bool(settings, FreeRDP_NetworkAutoDetect))
1129 {
1130 char rbuffer[128] = WINPR_C_ARRAY_INIT;
1131
1132 const char* requestStr = autodetect_request_type_to_string_buffer(
1133 autodetectRspPdu.responseType, rbuffer, sizeof(rbuffer));
1134
1135 WLog_Print(autodetect->log, WLOG_WARN,
1136 "Received a [MS-RDPBCGR] 2.2.14.2.1 RTT Measure Response [%s] "
1137 "message but support was not enabled",
1138 requestStr);
1139 return STATE_RUN_FAILED;
1140 }
1141
1142 if (autodetectRspPdu.headerTypeId != TYPE_ID_AUTODETECT_RESPONSE)
1143 {
1144 char rbuffer[128] = WINPR_C_ARRAY_INIT;
1145 const char* requestStr = autodetect_request_type_to_string_buffer(
1146 autodetectRspPdu.responseType, rbuffer, sizeof(rbuffer));
1147 char hbuffer[128] = WINPR_C_ARRAY_INIT;
1148 const char* headerStr =
1149 autodetect_header_type_string(autodetectRspPdu.headerTypeId, hbuffer, sizeof(hbuffer));
1150 WLog_Print(autodetect->log, WLOG_ERROR,
1151 "Received a [MS-RDPBCGR] 2.2.14.2.1 RTT Measure Response [%s] "
1152 "message with invalid headerTypeId=%s",
1153 requestStr, headerStr);
1154 goto fail;
1155 }
1156
1157 if (!IFCALLRESULT(TRUE, autodetect->ResponseReceived, autodetect, transport,
1158 autodetectRspPdu.responseType, autodetectRspPdu.sequenceNumber))
1159 goto fail;
1160
1161 switch (autodetectRspPdu.responseType)
1162 {
1163 case RDP_RTT_RESPONSE_TYPE:
1164 /* RTT Measure Response (RDP_RTT_RESPONSE) - MS-RDPBCGR 2.2.14.2.1 */
1165 success =
1166 autodetect_recv_rtt_measure_response(autodetect, transport, s, &autodetectRspPdu);
1167 break;
1168
1169 case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
1170 case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
1171 /* Bandwidth Measure Results (RDP_BW_RESULTS) - MS-RDPBCGR 2.2.14.2.2 */
1172 success = autodetect_recv_bandwidth_measure_results(autodetect, transport, s,
1173 &autodetectRspPdu);
1174 break;
1175
1176 case RDP_NETCHAR_SYNC_RESPONSE_TYPE:
1177 /* Network Characteristics Sync (RDP_NETCHAR_SYNC) - MS-RDPBCGR 2.2.14.2.3 */
1178 success = autodetect_recv_netchar_sync(autodetect, transport, s, &autodetectRspPdu);
1179 break;
1180
1181 default:
1182 WLog_Print(autodetect->log, WLOG_ERROR, "Unknown responseType=0x%04" PRIx16,
1183 autodetectRspPdu.responseType);
1184 break;
1185 }
1186
1187fail:
1188 if (success)
1189 {
1190 if (autodetectRspPdu.responseType == RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME)
1191 autodetect->state = FREERDP_AUTODETECT_STATE_COMPLETE;
1192 else
1193 autodetect->state = FREERDP_AUTODETECT_STATE_RESPONSE;
1194 }
1195 else
1196 autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
1197
1198 return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
1199}
1200
1201void autodetect_on_connect_time_auto_detect_begin(rdpAutoDetect* autodetect)
1202{
1203 WINPR_ASSERT(autodetect);
1204 WINPR_ASSERT(autodetect->OnConnectTimeAutoDetectBegin);
1205
1206 autodetect->state = autodetect->OnConnectTimeAutoDetectBegin(autodetect);
1207}
1208
1209void autodetect_on_connect_time_auto_detect_progress(rdpAutoDetect* autodetect)
1210{
1211 WINPR_ASSERT(autodetect);
1212 WINPR_ASSERT(autodetect->OnConnectTimeAutoDetectProgress);
1213
1214 autodetect->state = autodetect->OnConnectTimeAutoDetectProgress(autodetect);
1215}
1216
1217rdpAutoDetect* autodetect_new(rdpContext* context)
1218{
1219 rdpAutoDetect* autoDetect = (rdpAutoDetect*)calloc(1, sizeof(rdpAutoDetect));
1220 if (!autoDetect)
1221 return nullptr;
1222 autoDetect->context = context;
1223 autoDetect->log = WLog_Get(AUTODETECT_TAG);
1224
1225 return autoDetect;
1226}
1227
1228void autodetect_free(rdpAutoDetect* autoDetect)
1229{
1230 free(autoDetect);
1231}
1232
1233void autodetect_register_server_callbacks(rdpAutoDetect* autodetect)
1234{
1235 WINPR_ASSERT(autodetect);
1236
1237 autodetect->RTTMeasureRequest = autodetect_send_rtt_measure_request;
1238 autodetect->BandwidthMeasureStart = autodetect_send_bandwidth_measure_start;
1239 autodetect->BandwidthMeasurePayload = autodetect_send_bandwidth_measure_payload;
1240 autodetect->BandwidthMeasureStop = autodetect_send_bandwidth_measure_stop;
1241 autodetect->NetworkCharacteristicsResult = autodetect_send_netchar_result;
1242
1243 /*
1244 * Default handlers for Connect-Time Auto-Detection
1245 * (MAY be overridden by the API user)
1246 */
1247 autodetect->OnConnectTimeAutoDetectBegin = autodetect_on_connect_time_auto_detect_begin_default;
1248 autodetect->OnConnectTimeAutoDetectProgress =
1249 autodetect_on_connect_time_auto_detect_progress_default;
1250}
1251
1252FREERDP_AUTODETECT_STATE autodetect_get_state(rdpAutoDetect* autodetect)
1253{
1254 WINPR_ASSERT(autodetect);
1255 return autodetect->state;
1256}
1257
1258rdpAutoDetect* autodetect_get(rdpContext* context)
1259{
1260 WINPR_ASSERT(context);
1261 WINPR_ASSERT(context->rdp);
1262 return context->rdp->autodetect;
1263}
WINPR_ATTR_NODISCARD FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.