20#include <freerdp/config.h>
22#include <winpr/crypto.h>
23#include <winpr/assert.h>
25#include "autodetect.h"
26#include "multitransport.h"
28#define TYPE_ID_AUTODETECT_REQUEST 0x00
29#define TYPE_ID_AUTODETECT_RESPONSE 0x01
31#define RDP_RTT_REQUEST_TYPE_CONTINUOUS 0x0001
32#define RDP_RTT_REQUEST_TYPE_CONNECTTIME 0x1001
34#define RDP_RTT_RESPONSE_TYPE 0x0000
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
44#define RDP_NETCHAR_SYNC_RESPONSE_TYPE 0x0018
46#define RDP_NETCHAR_RESULTS_0x0840 0x0840U
47#define RDP_NETCHAR_RESULTS_0x0880 0x0880U
48#define RDP_NETCHAR_RESULTS_0x08C0 0x08C0U
54 UINT16 sequenceNumber;
62 UINT16 sequenceNumber;
66static const char* autodetect_header_type_string(UINT8 headerType,
char* buffer,
size_t size)
68 const char* str =
nullptr;
71 case TYPE_ID_AUTODETECT_REQUEST:
72 str =
"TYPE_ID_AUTODETECT_REQUEST";
74 case TYPE_ID_AUTODETECT_RESPONSE:
75 str =
"TYPE_ID_AUTODETECT_RESPONSE";
78 str =
"TYPE_ID_AUTODETECT_UNKNOWN";
82 (void)_snprintf(buffer, size,
"%s [0x%08" PRIx8
"]", str, headerType);
86static const char* autodetect_request_type_to_string(UINT32 requestType)
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";
125static const char* autodetect_request_type_to_string_buffer(UINT32 requestType,
char* buffer,
128 const char* str = autodetect_request_type_to_string(requestType);
129 (void)_snprintf(buffer, size,
"%s [0x%08" PRIx32
"]", str, requestType);
133static BOOL autodetect_send_rtt_measure_request(rdpAutoDetect* autodetect,
134 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
135 UINT16 sequenceNumber)
137 UINT16 requestType = 0;
138 UINT16 sec_flags = 0;
141 WINPR_ASSERT(autodetect);
142 WINPR_ASSERT(autodetect->context);
144 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
148 if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
149 requestType = RDP_RTT_REQUEST_TYPE_CONNECTTIME;
151 requestType = RDP_RTT_REQUEST_TYPE_CONTINUOUS;
153 WLog_Print(autodetect->log, WLOG_TRACE,
"sending RTT Measure Request PDU");
154 Stream_Write_UINT8(s, 0x06);
155 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);
156 Stream_Write_UINT16(s, sequenceNumber);
157 Stream_Write_UINT16(s, requestType);
158 autodetect->rttMeasureStartTime = GetTickCount64();
159 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
160 sec_flags | SEC_AUTODETECT_REQ);
163static BOOL autodetect_send_rtt_measure_response(rdpAutoDetect* autodetect, UINT16 sequenceNumber)
165 UINT16 sec_flags = 0;
168 WINPR_ASSERT(autodetect);
169 WINPR_ASSERT(autodetect->context);
172 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
177 WLog_Print(autodetect->log, WLOG_TRACE,
178 "sending RTT Measure Response PDU (seqNumber=0x%" PRIx16
")", sequenceNumber);
179 Stream_Write_UINT8(s, 0x06);
180 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE);
181 Stream_Write_UINT16(s, sequenceNumber);
182 Stream_Write_UINT16(s, RDP_RTT_RESPONSE_TYPE);
183 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
184 sec_flags | SEC_AUTODETECT_RSP);
187static BOOL autodetect_send_netchar_sync(rdpAutoDetect* autodetect, UINT16 sequenceNumber)
189 UINT16 sec_flags = 0;
191 WINPR_ASSERT(autodetect);
192 WINPR_ASSERT(autodetect->context);
194 wStream* s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
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);
202 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE);
203 Stream_Write_UINT16(s, sequenceNumber);
204 Stream_Write_UINT16(s, RDP_NETCHAR_SYNC_RESPONSE_TYPE);
205 Stream_Write_UINT32(s, autodetect->netCharBandwidth);
206 Stream_Write_UINT32(s, autodetect->netCharAverageRTT);
207 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
208 sec_flags | SEC_AUTODETECT_RSP);
211static BOOL autodetect_send_bandwidth_measure_start(rdpAutoDetect* autodetect,
212 RDP_TRANSPORT_TYPE transport,
213 UINT16 sequenceNumber)
215 UINT16 requestType = 0;
216 UINT16 sec_flags = 0;
219 WINPR_ASSERT(autodetect);
220 WINPR_ASSERT(autodetect->context);
222#if defined(WITH_RDPEUDP_SERVER)
224 if (multitransport_server_send_autodetect(autodetect->context->rdp->multitransport, transport,
225 sequenceNumber, RDP_BW_START_REQUEST_TYPE_CONTINUOUS,
229 if (transport != RDP_TRANSPORT_TCP)
231 WLog_Print(autodetect->log, WLOG_ERROR,
232 "sending Bandwidth Measure Start PDU -> unsupported transport %s, abort",
233 rdp_transport_type_string(transport));
238 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
242 if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
243 requestType = RDP_BW_START_REQUEST_TYPE_CONNECTTIME;
245 requestType = RDP_BW_START_REQUEST_TYPE_CONTINUOUS;
247 WLog_Print(autodetect->log, WLOG_TRACE,
248 "sending Bandwidth Measure Start PDU(seqNumber=%" PRIu16
")", sequenceNumber);
249 Stream_Write_UINT8(s, 0x06);
250 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);
251 Stream_Write_UINT16(s, sequenceNumber);
252 Stream_Write_UINT16(s, requestType);
253 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
254 sec_flags | SEC_AUTODETECT_REQ);
258autodetect_send_bandwidth_measure_payload(rdpAutoDetect* autodetect,
259 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
260 UINT16 sequenceNumber, UINT16 payloadLength)
262 UINT16 sec_flags = 0;
265 WINPR_ASSERT(autodetect);
266 WINPR_ASSERT(autodetect->context);
268 WINPR_ASSERT(freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE);
270 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
274 WLog_Print(autodetect->log, WLOG_TRACE,
275 "sending Bandwidth Measure Payload PDU -> payloadLength=%" PRIu16
"", payloadLength);
279 if (!Stream_EnsureRemainingCapacity(s, 8ull + payloadLength))
281 WLog_Print(autodetect->log, WLOG_ERROR,
"Failed to ensure %lu bytes in stream",
282 8ul + payloadLength);
287 Stream_Write_UINT8(s, 0x08);
288 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);
289 Stream_Write_UINT16(s, sequenceNumber);
290 Stream_Write_UINT16(s, RDP_BW_PAYLOAD_REQUEST_TYPE);
291 Stream_Write_UINT16(s, payloadLength);
293 if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
298 Stream_Seek(s, payloadLength);
299 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
300 sec_flags | SEC_AUTODETECT_REQ);
303static BOOL autodetect_send_bandwidth_measure_stop(rdpAutoDetect* autodetect,
304 RDP_TRANSPORT_TYPE transport,
305 UINT16 sequenceNumber, UINT16 payloadLength)
307 UINT16 requestType = 0;
308 UINT16 sec_flags = 0;
311 WINPR_ASSERT(autodetect);
312 WINPR_ASSERT(autodetect->context);
314#if defined(WITH_RDPEUDP_SERVER)
316 if (multitransport_server_send_autodetect(autodetect->context->rdp->multitransport, transport,
317 sequenceNumber, RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS,
321 if (transport != RDP_TRANSPORT_TCP)
323 WLog_Print(autodetect->log, WLOG_ERROR,
324 "sending Bandwidth Measure Stop PDU -> unsupported transport %s, abort",
325 rdp_transport_type_string(transport));
330 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
334 if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
335 requestType = RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME;
337 requestType = RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS;
339 if (requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
342 WLog_Print(autodetect->log, WLOG_TRACE,
343 "sending Bandwidth Measure Stop PDU -> payloadLength=%" PRIu16
"", payloadLength);
346 Stream_Write_UINT8(s, requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME
349 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);
350 Stream_Write_UINT16(s, sequenceNumber);
351 Stream_Write_UINT16(s, requestType);
353 if (requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
355 Stream_Write_UINT16(s, payloadLength);
357 if (payloadLength > 0)
359 if (!Stream_EnsureRemainingCapacity(s, payloadLength))
361 WLog_Print(autodetect->log, WLOG_ERROR,
362 "Failed to ensure %" PRIu16
" bytes in stream", payloadLength);
368 if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
373 Stream_Seek(s, payloadLength);
377 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
378 sec_flags | SEC_AUTODETECT_REQ);
382static void autodetect_write_bandwidth_measure_results(
const rdpAutoDetect* autodetect,
wStream* s,
383 UINT16 responseType, UINT16 sequenceNumber,
386 WINPR_ASSERT(autodetect);
389 Stream_Write_UINT8(s, 0x0E);
390 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE);
391 Stream_Write_UINT16(s, sequenceNumber);
392 Stream_Write_UINT16(s, responseType);
393 Stream_Write_UINT32(s, timeDelta);
394 Stream_Write_UINT32(s, autodetect->bandwidthMeasureByteCount);
397static BOOL autodetect_send_bandwidth_measure_results(rdpAutoDetect* autodetect,
398 RDP_TRANSPORT_TYPE transport,
399 UINT16 responseType, UINT16 sequenceNumber)
402 UINT16 sec_flags = 0;
403 UINT64 timeDelta = GetTickCount64();
405 WINPR_ASSERT(autodetect);
406 WINPR_ASSERT(autodetect->context);
409 if (autodetect->bandwidthMeasureStartTime > timeDelta)
411 WLog_Print(autodetect->log, WLOG_WARN,
412 "Invalid bandwidthMeasureStartTime %" PRIu64
" > current %" PRIu64
414 autodetect->bandwidthMeasureStartTime, timeDelta);
418 timeDelta -= autodetect->bandwidthMeasureStartTime;
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
424 delta, autodetect->bandwidthMeasureByteCount,
425 (transport == RDP_TRANSPORT_UDP_R) ?
" in the UDP tunnel" :
"");
427 IFCALLRET(autodetect->ClientBandwidthMeasureResult, success, autodetect, transport,
428 responseType, sequenceNumber, delta, autodetect->bandwidthMeasureByteCount);
431 WLog_Print(autodetect->log, WLOG_ERROR,
"ClientBandwidthMeasureResult failed");
437 if (transport == RDP_TRANSPORT_UDP_R)
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,
444 return multitransport_send_autodetect_response(autodetect->context->rdp->multitransport,
445 buffer,
sizeof(buffer));
448 wStream* s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
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);
456static BOOL autodetect_send_netchar_result(rdpAutoDetect* autodetect,
457 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
458 UINT16 sequenceNumber,
459 const rdpNetworkCharacteristicsResult* result)
461 UINT16 sec_flags = 0;
464 WINPR_ASSERT(autodetect);
465 WINPR_ASSERT(autodetect->context);
467 s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
472 WLog_Print(autodetect->log, WLOG_TRACE,
"sending Network Characteristics Result PDU");
474 switch (result->type)
476 case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT:
477 Stream_Write_UINT8(s, 0x0E);
478 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);
479 Stream_Write_UINT16(s, sequenceNumber);
480 WINPR_ASSERT((result->type <= UINT16_MAX));
481 WINPR_ASSERT((result->type >= 0));
482 Stream_Write_UINT16(s, (UINT16)result->type);
483 Stream_Write_UINT32(s, result->baseRTT);
484 Stream_Write_UINT32(s, result->averageRTT);
486 case RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT:
487 Stream_Write_UINT8(s, 0x0E);
488 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);
489 Stream_Write_UINT16(s, sequenceNumber);
490 WINPR_ASSERT((result->type <= UINT16_MAX));
491 WINPR_ASSERT((result->type >= 0));
492 Stream_Write_UINT16(s, (UINT16)result->type);
493 Stream_Write_UINT32(s, result->bandwidth);
494 Stream_Write_UINT32(s, result->averageRTT);
496 case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT:
497 Stream_Write_UINT8(s, 0x12);
498 Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);
499 Stream_Write_UINT16(s, sequenceNumber);
500 WINPR_ASSERT((result->type <= UINT16_MAX));
501 WINPR_ASSERT((result->type >= 0));
502 Stream_Write_UINT16(s, (UINT16)result->type);
503 Stream_Write_UINT32(s, result->baseRTT);
504 Stream_Write_UINT32(s, result->bandwidth);
505 Stream_Write_UINT32(s, result->averageRTT);
512 return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
513 sec_flags | SEC_AUTODETECT_REQ);
516static FREERDP_AUTODETECT_STATE
517autodetect_on_connect_time_auto_detect_begin_default(rdpAutoDetect* autodetect)
519 WINPR_ASSERT(autodetect);
520 WINPR_ASSERT(autodetect->RTTMeasureRequest);
522 if (!autodetect->RTTMeasureRequest(autodetect, RDP_TRANSPORT_TCP, 0x23))
523 return FREERDP_AUTODETECT_STATE_FAIL;
525 return FREERDP_AUTODETECT_STATE_REQUEST;
528static FREERDP_AUTODETECT_STATE
529autodetect_on_connect_time_auto_detect_progress_default(rdpAutoDetect* autodetect)
531 WINPR_ASSERT(autodetect);
533 if (autodetect->state == FREERDP_AUTODETECT_STATE_RESPONSE ||
534 autodetect->state == FREERDP_AUTODETECT_STATE_COMPLETE)
535 return FREERDP_AUTODETECT_STATE_COMPLETE;
537 return autodetect->state;
540static BOOL autodetect_recv_rtt_measure_request(rdpAutoDetect* autodetect,
541 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
543 const AUTODETECT_REQ_PDU* autodetectReqPdu)
545 WINPR_ASSERT(autodetect);
547 WINPR_ASSERT(autodetectReqPdu);
549 if (autodetectReqPdu->headerLength != 0x06)
551 WLog_Print(autodetect->log, WLOG_ERROR,
552 "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8
"]",
553 autodetectReqPdu->headerLength);
557 WLog_Print(autodetect->log, WLOG_TRACE,
"received RTT Measure Request PDU");
559 return autodetect_send_rtt_measure_response(autodetect, autodetectReqPdu->sequenceNumber);
562static BOOL autodetect_recv_rtt_measure_response(rdpAutoDetect* autodetect,
563 RDP_TRANSPORT_TYPE transport,
565 const AUTODETECT_RSP_PDU* autodetectRspPdu)
569 WINPR_ASSERT(autodetect);
570 WINPR_ASSERT(autodetectRspPdu);
572 if (autodetectRspPdu->headerLength != 0x06)
574 WLog_Print(autodetect->log, WLOG_ERROR,
575 "autodetectRspPdu->headerLength != 0x06 [0x%02" PRIx8
"]",
576 autodetectRspPdu->headerLength);
580 WLog_Print(autodetect->log, WLOG_TRACE,
"received RTT Measure Response PDU");
581 autodetect->netCharAverageRTT =
582 (UINT32)MIN(GetTickCount64() - autodetect->rttMeasureStartTime, UINT32_MAX);
584 if (autodetect->netCharBaseRTT == 0 ||
585 autodetect->netCharBaseRTT > autodetect->netCharAverageRTT)
586 autodetect->netCharBaseRTT = autodetect->netCharAverageRTT;
588 IFCALLRET(autodetect->RTTMeasureResponse, success, autodetect, transport,
589 autodetectRspPdu->sequenceNumber);
591 WLog_Print(autodetect->log, WLOG_WARN,
"RTTMeasureResponse failed");
595static BOOL autodetect_recv_bandwidth_measure_start(rdpAutoDetect* autodetect,
596 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
598 const AUTODETECT_REQ_PDU* autodetectReqPdu)
600 WINPR_ASSERT(autodetect);
602 WINPR_ASSERT(autodetectReqPdu);
604 if (autodetectReqPdu->headerLength != 0x06)
606 WLog_Print(autodetect->log, WLOG_ERROR,
607 "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8
"]",
608 autodetectReqPdu->headerLength);
612 WLog_Print(autodetect->log, WLOG_TRACE,
613 "received Bandwidth Measure Start PDU - time=%" PRIu64
"", GetTickCount64());
615 autodetect->bandwidthMeasureStartTime = GetTickCount64();
616 autodetect->bandwidthMeasureByteCount = 0;
621 if ((autodetectReqPdu->requestType == RDP_BW_START_REQUEST_TYPE_CONNECTTIME) &&
622 (autodetect->netCharBandwidth > 0))
625 autodetect->context->settings, FreeRDP_ServerAutoReconnectCookie);
626 if (cookie && (cookie->cbLen > 0))
627 return autodetect_send_netchar_sync(autodetect, autodetectReqPdu->sequenceNumber);
631 if (autodetectReqPdu->requestType == RDP_BW_START_REQUEST_TYPE_CONTINUOUS)
633 autodetect->bandwidthMeasureStarted = TRUE;
640autodetect_recv_bandwidth_measure_payload(rdpAutoDetect* autodetect,
641 WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
642 wStream* s,
const AUTODETECT_REQ_PDU* autodetectReqPdu)
644 UINT16 payloadLength = 0;
646 WINPR_ASSERT(autodetect);
648 WINPR_ASSERT(autodetectReqPdu);
650 if (autodetectReqPdu->headerLength != 0x08)
652 WLog_Print(autodetect->log, WLOG_ERROR,
653 "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8
"]",
654 autodetectReqPdu->headerLength);
658 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
661 Stream_Read_UINT16(s, payloadLength);
662 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
664 Stream_Seek(s, payloadLength);
666 WLog_Print(autodetect->log, WLOG_DEBUG,
667 "received Bandwidth Measure Payload PDU -> payloadLength=%" PRIu16
"",
672 autodetect->bandwidthMeasureByteCount += autodetectReqPdu->headerLength;
673 autodetect->bandwidthMeasureByteCount += payloadLength;
677static BOOL autodetect_recv_bandwidth_measure_stop(rdpAutoDetect* autodetect,
678 RDP_TRANSPORT_TYPE transport,
wStream* s,
679 const AUTODETECT_REQ_PDU* autodetectReqPdu)
681 UINT16 payloadLength = 0;
682 UINT16 responseType = 0;
684 WINPR_ASSERT(autodetect);
686 WINPR_ASSERT(autodetectReqPdu);
688 if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
690 if (autodetectReqPdu->headerLength != 0x08)
692 WLog_Print(autodetect->log, WLOG_ERROR,
693 "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8
"]",
694 autodetectReqPdu->headerLength);
698 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
701 Stream_Read_UINT16(s, payloadLength);
705 if (autodetectReqPdu->headerLength != 0x06)
707 WLog_Print(autodetect->log, WLOG_ERROR,
708 "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8
"]",
709 autodetectReqPdu->headerLength);
716 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
718 Stream_Seek(s, payloadLength);
720 WLog_Print(autodetect->log, WLOG_TRACE,
721 "received Bandwidth Measure Stop PDU -> payloadLength=%" PRIu16
"", payloadLength);
724 if (payloadLength > 0)
726 autodetect->bandwidthMeasureByteCount += autodetectReqPdu->headerLength;
727 autodetect->bandwidthMeasureByteCount += payloadLength;
731 if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
733 autodetect->bandwidthMeasureStarted = FALSE;
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);
744static BOOL autodetect_recv_bandwidth_measure_results(rdpAutoDetect* autodetect,
745 RDP_TRANSPORT_TYPE transport,
wStream* s,
746 const AUTODETECT_RSP_PDU* autodetectRspPdu)
748 UINT32 timeDelta = 0;
749 UINT32 byteCount = 0;
752 WINPR_ASSERT(autodetect);
754 WINPR_ASSERT(autodetectRspPdu);
756 if (autodetectRspPdu->headerLength != 0x0E)
758 WLog_Print(autodetect->log, WLOG_ERROR,
759 "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8
"]",
760 autodetectRspPdu->headerLength);
764 WLog_Print(autodetect->log, WLOG_TRACE,
"received Bandwidth Measure Results PDU");
765 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
767 Stream_Read_UINT32(s, timeDelta);
768 Stream_Read_UINT32(s, byteCount);
770 IFCALLRET(autodetect->BandwidthMeasureResults, success, autodetect, transport,
771 autodetectRspPdu->sequenceNumber, autodetectRspPdu->responseType, timeDelta,
774 WLog_Print(autodetect->log, WLOG_WARN,
"BandwidthMeasureResults failed");
778static BOOL autodetect_recv_netchar_sync(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
779 wStream* s,
const AUTODETECT_RSP_PDU* autodetectRspPdu)
781 UINT32 bandwidth = 0;
785 WINPR_ASSERT(autodetect);
787 WINPR_ASSERT(autodetectRspPdu);
789 if (autodetectRspPdu->headerLength != 0x0E)
791 WLog_Print(autodetect->log, WLOG_ERROR,
792 "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8
"]",
793 autodetectRspPdu->headerLength);
796 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
800 Stream_Read_UINT32(s, bandwidth);
801 Stream_Read_UINT32(s, rtt);
803 WLog_Print(autodetect->log, WLOG_TRACE,
804 "received Network Characteristics Sync PDU -> bandwidth=%" PRIu32
", rtt=%" PRIu32
808 IFCALLRET(autodetect->NetworkCharacteristicsSync, success, autodetect, transport,
809 autodetectRspPdu->sequenceNumber, bandwidth, rtt);
811 WLog_Print(autodetect->log, WLOG_WARN,
"NetworkCharacteristicsSync failed");
815static BOOL autodetect_recv_netchar_request(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
816 wStream* s,
const AUTODETECT_REQ_PDU* autodetectReqPdu)
818 rdpNetworkCharacteristicsResult result = {
819 .type = RDP_NETCHAR_RESERVED, .baseRTT = 0, .averageRTT = 0, .bandwidth = 0
823 WINPR_ASSERT(autodetect);
825 WINPR_ASSERT(autodetectReqPdu);
827 switch (autodetectReqPdu->requestType)
829 case RDP_NETCHAR_RESULTS_0x0840:
832 if (autodetectReqPdu->headerLength != 0x0E)
834 WLog_Print(autodetect->log, WLOG_ERROR,
835 "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8
"]",
836 autodetectReqPdu->headerLength);
839 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
842 result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT;
843 Stream_Read_UINT32(s, result.baseRTT);
844 Stream_Read_UINT32(s, result.averageRTT);
845 autodetect->netCharBaseRTT = result.baseRTT;
848 case RDP_NETCHAR_RESULTS_0x0880:
851 if (autodetectReqPdu->headerLength != 0x0E)
853 WLog_Print(autodetect->log, WLOG_ERROR,
854 "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8
"]",
855 autodetectReqPdu->headerLength);
858 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
861 result.type = RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT;
862 Stream_Read_UINT32(s, result.bandwidth);
863 Stream_Read_UINT32(s, result.averageRTT);
864 autodetect->netCharBandwidth = result.bandwidth;
867 case RDP_NETCHAR_RESULTS_0x08C0:
870 if (autodetectReqPdu->headerLength != 0x12)
872 WLog_Print(autodetect->log, WLOG_ERROR,
873 "autodetectReqPdu->headerLength != 0x012 [0x%02" PRIx8
"]",
874 autodetectReqPdu->headerLength);
877 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 12))
880 result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT;
881 Stream_Read_UINT32(s, result.baseRTT);
882 Stream_Read_UINT32(s, result.bandwidth);
883 Stream_Read_UINT32(s, result.averageRTT);
884 autodetect->netCharBaseRTT = result.baseRTT;
885 autodetect->netCharBandwidth = result.bandwidth;
893 autodetect->netCharAverageRTT = result.averageRTT;
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);
900 IFCALLRET(autodetect->NetworkCharacteristicsResult, success, autodetect, transport,
901 autodetectReqPdu->sequenceNumber, &result);
903 WLog_Print(autodetect->log, WLOG_WARN,
"NetworkCharacteristicsResult failed");
907state_run_t autodetect_recv_request_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
910 AUTODETECT_REQ_PDU autodetectReqPdu = WINPR_C_ARRAY_INIT;
911 const rdpSettings* settings =
nullptr;
912 BOOL success = FALSE;
914 WINPR_ASSERT(autodetect);
915 WINPR_ASSERT(autodetect->context);
917 settings = autodetect->context->settings;
918 WINPR_ASSERT(settings);
920 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
921 return STATE_RUN_FAILED;
923 Stream_Read_UINT8(s, autodetectReqPdu.headerLength);
924 Stream_Read_UINT8(s, autodetectReqPdu.headerTypeId);
925 Stream_Read_UINT16(s, autodetectReqPdu.sequenceNumber);
926 Stream_Read_UINT16(s, autodetectReqPdu.requestType);
928 if (WLog_IsLevelActive(autodetect->log, WLOG_TRACE))
930 char rbuffer[128] = WINPR_C_ARRAY_INIT;
931 const char* requestTypeStr = autodetect_request_type_to_string_buffer(
932 autodetectReqPdu.requestType, rbuffer,
sizeof(rbuffer));
934 char hbuffer[128] = WINPR_C_ARRAY_INIT;
935 const char* headerStr =
936 autodetect_header_type_string(autodetectReqPdu.headerTypeId, hbuffer,
sizeof(hbuffer));
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,
947 char rbuffer[128] = WINPR_C_ARRAY_INIT;
948 const char* requestTypeStr = autodetect_request_type_to_string_buffer(
949 autodetectReqPdu.requestType, rbuffer,
sizeof(rbuffer));
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",
958 if (autodetectReqPdu.headerTypeId != TYPE_ID_AUTODETECT_REQUEST)
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));
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);
974 if (!IFCALLRESULT(TRUE, autodetect->RequestReceived, autodetect, transport,
975 autodetectReqPdu.requestType, autodetectReqPdu.sequenceNumber))
978 switch (autodetectReqPdu.requestType)
980 case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
981 case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
984 autodetect_recv_rtt_measure_request(autodetect, transport, s, &autodetectReqPdu);
987 case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
988 case RDP_BW_START_REQUEST_TYPE_TUNNEL:
989 case RDP_BW_START_REQUEST_TYPE_CONNECTTIME:
991 success = autodetect_recv_bandwidth_measure_start(autodetect, transport, s,
995 case RDP_BW_PAYLOAD_REQUEST_TYPE:
997 success = autodetect_recv_bandwidth_measure_payload(autodetect, transport, s,
1001 case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
1002 case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
1003 case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
1006 autodetect_recv_bandwidth_measure_stop(autodetect, transport, s, &autodetectReqPdu);
1009 case RDP_NETCHAR_RESULTS_0x0840:
1010 case RDP_NETCHAR_RESULTS_0x0880:
1011 case RDP_NETCHAR_RESULTS_0x08C0:
1013 success = autodetect_recv_netchar_request(autodetect, transport, s, &autodetectReqPdu);
1017 WLog_Print(autodetect->log, WLOG_ERROR,
"Unknown requestType=0x%04" PRIx16,
1018 autodetectReqPdu.requestType);
1024 autodetect->state = FREERDP_AUTODETECT_STATE_REQUEST;
1026 autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
1027 return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
1030state_run_t autodetect_recv_tunnel_request(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
1033 WINPR_ASSERT(autodetect);
1040 UINT16 requestType = 0;
1041 if (Stream_GetRemainingLength(s) >= 6)
1043 const BYTE* header = Stream_ConstPointer(s);
1044 requestType = winpr_Data_Get_UINT16(&header[4]);
1047 switch (requestType)
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);
1057 WLog_Print(autodetect->log, WLOG_DEBUG,
1058 "ignoring auto-detect request 0x%04" PRIx16
" in a tunnel subheader",
1060 return STATE_RUN_SUCCESS;
1064state_run_t autodetect_recv_tunnel_response(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
1067 WINPR_ASSERT(autodetect);
1073 UINT16 responseType = 0;
1074 if (Stream_GetRemainingLength(s) >= 6)
1076 const BYTE* header = Stream_ConstPointer(s);
1077 responseType = winpr_Data_Get_UINT16(&header[4]);
1080 if (responseType != RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS)
1082 WLog_Print(autodetect->log, WLOG_DEBUG,
1083 "discarding auto-detect response 0x%04" PRIx16
" in a tunnel subheader",
1085 return STATE_RUN_SUCCESS;
1087 return autodetect_recv_response_packet(autodetect, transport, s);
1090state_run_t autodetect_recv_response_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
1093 AUTODETECT_RSP_PDU autodetectRspPdu = WINPR_C_ARRAY_INIT;
1094 const rdpSettings* settings =
nullptr;
1095 BOOL success = FALSE;
1097 WINPR_ASSERT(autodetect);
1098 WINPR_ASSERT(autodetect->context);
1101 settings = autodetect->context->settings;
1102 WINPR_ASSERT(settings);
1104 if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
1107 Stream_Read_UINT8(s, autodetectRspPdu.headerLength);
1108 Stream_Read_UINT8(s, autodetectRspPdu.headerTypeId);
1109 Stream_Read_UINT16(s, autodetectRspPdu.sequenceNumber);
1110 Stream_Read_UINT16(s, autodetectRspPdu.responseType);
1112 if (WLog_IsLevelActive(autodetect->log, WLOG_TRACE))
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));
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,
1130 char rbuffer[128] = WINPR_C_ARRAY_INIT;
1132 const char* requestStr = autodetect_request_type_to_string_buffer(
1133 autodetectRspPdu.responseType, rbuffer,
sizeof(rbuffer));
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",
1139 return STATE_RUN_FAILED;
1142 if (autodetectRspPdu.headerTypeId != TYPE_ID_AUTODETECT_RESPONSE)
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);
1157 if (!IFCALLRESULT(TRUE, autodetect->ResponseReceived, autodetect, transport,
1158 autodetectRspPdu.responseType, autodetectRspPdu.sequenceNumber))
1161 switch (autodetectRspPdu.responseType)
1163 case RDP_RTT_RESPONSE_TYPE:
1166 autodetect_recv_rtt_measure_response(autodetect, transport, s, &autodetectRspPdu);
1169 case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
1170 case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
1172 success = autodetect_recv_bandwidth_measure_results(autodetect, transport, s,
1176 case RDP_NETCHAR_SYNC_RESPONSE_TYPE:
1178 success = autodetect_recv_netchar_sync(autodetect, transport, s, &autodetectRspPdu);
1182 WLog_Print(autodetect->log, WLOG_ERROR,
"Unknown responseType=0x%04" PRIx16,
1183 autodetectRspPdu.responseType);
1190 if (autodetectRspPdu.responseType == RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME)
1191 autodetect->state = FREERDP_AUTODETECT_STATE_COMPLETE;
1193 autodetect->state = FREERDP_AUTODETECT_STATE_RESPONSE;
1196 autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
1198 return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
1201void autodetect_on_connect_time_auto_detect_begin(rdpAutoDetect* autodetect)
1203 WINPR_ASSERT(autodetect);
1204 WINPR_ASSERT(autodetect->OnConnectTimeAutoDetectBegin);
1206 autodetect->state = autodetect->OnConnectTimeAutoDetectBegin(autodetect);
1209void autodetect_on_connect_time_auto_detect_progress(rdpAutoDetect* autodetect)
1211 WINPR_ASSERT(autodetect);
1212 WINPR_ASSERT(autodetect->OnConnectTimeAutoDetectProgress);
1214 autodetect->state = autodetect->OnConnectTimeAutoDetectProgress(autodetect);
1217rdpAutoDetect* autodetect_new(rdpContext* context)
1219 rdpAutoDetect* autoDetect = (rdpAutoDetect*)calloc(1,
sizeof(rdpAutoDetect));
1222 autoDetect->context = context;
1223 autoDetect->log = WLog_Get(AUTODETECT_TAG);
1228void autodetect_free(rdpAutoDetect* autoDetect)
1233void autodetect_register_server_callbacks(rdpAutoDetect* autodetect)
1235 WINPR_ASSERT(autodetect);
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;
1247 autodetect->OnConnectTimeAutoDetectBegin = autodetect_on_connect_time_auto_detect_begin_default;
1248 autodetect->OnConnectTimeAutoDetectProgress =
1249 autodetect_on_connect_time_auto_detect_progress_default;
1252FREERDP_AUTODETECT_STATE autodetect_get_state(rdpAutoDetect* autodetect)
1254 WINPR_ASSERT(autodetect);
1255 return autodetect->state;
1258rdpAutoDetect* autodetect_get(rdpContext* context)
1260 WINPR_ASSERT(context);
1261 WINPR_ASSERT(context->rdp);
1262 return context->rdp->autodetect;
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.