Coverage Report

Created: 2026-07-30 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/autodetect.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Auto-Detect PDUs
4
 *
5
 * Copyright 2014 Dell Software <Mike.McDonald@software.dell.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <freerdp/config.h>
21
22
#include <winpr/crypto.h>
23
#include <winpr/assert.h>
24
25
#include "autodetect.h"
26
27
5.45k
#define TYPE_ID_AUTODETECT_REQUEST 0x00
28
3.74k
#define TYPE_ID_AUTODETECT_RESPONSE 0x01
29
30
63
#define RDP_RTT_REQUEST_TYPE_CONTINUOUS 0x0001
31
36
#define RDP_RTT_REQUEST_TYPE_CONNECTTIME 0x1001
32
33
1.05k
#define RDP_RTT_RESPONSE_TYPE 0x0000
34
35
41
#define RDP_BW_START_REQUEST_TYPE_CONTINUOUS 0x0014
36
26
#define RDP_BW_START_REQUEST_TYPE_TUNNEL 0x0114
37
18
#define RDP_BW_START_REQUEST_TYPE_CONNECTTIME 0x1014
38
59
#define RDP_BW_PAYLOAD_REQUEST_TYPE 0x0002
39
400
#define RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME 0x002B
40
263
#define RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS 0x0429
41
149
#define RDP_BW_STOP_REQUEST_TYPE_TUNNEL 0x0629
42
43
14
#define RDP_NETCHAR_SYNC_RESPONSE_TYPE 0x0018
44
45
34
#define RDP_NETCHAR_RESULTS_0x0840 0x0840U
46
59
#define RDP_NETCHAR_RESULTS_0x0880 0x0880U
47
113
#define RDP_NETCHAR_RESULTS_0x08C0 0x08C0U
48
49
typedef struct
50
{
51
  UINT8 headerLength;
52
  UINT8 headerTypeId;
53
  UINT16 sequenceNumber;
54
  UINT16 requestType;
55
} AUTODETECT_REQ_PDU;
56
57
typedef struct
58
{
59
  UINT8 headerLength;
60
  UINT8 headerTypeId;
61
  UINT16 sequenceNumber;
62
  UINT16 responseType;
63
} AUTODETECT_RSP_PDU;
64
65
static const char* autodetect_header_type_string(UINT8 headerType, char* buffer, size_t size)
66
5.57k
{
67
5.57k
  const char* str = nullptr;
68
5.57k
  switch (headerType)
69
5.57k
  {
70
1.36k
    case TYPE_ID_AUTODETECT_REQUEST:
71
1.36k
      str = "TYPE_ID_AUTODETECT_REQUEST";
72
1.36k
      break;
73
98
    case TYPE_ID_AUTODETECT_RESPONSE:
74
98
      str = "TYPE_ID_AUTODETECT_RESPONSE";
75
98
      break;
76
4.11k
    default:
77
4.11k
      str = "TYPE_ID_AUTODETECT_UNKNOWN";
78
4.11k
      break;
79
5.57k
  }
80
81
5.57k
  (void)_snprintf(buffer, size, "%s [0x%08" PRIx8 "]", str, headerType);
82
5.57k
  return buffer;
83
5.57k
}
84
85
static const char* autodetect_request_type_to_string(UINT32 requestType)
86
5.57k
{
87
5.57k
  switch (requestType)
88
5.57k
  {
89
1.01k
    case RDP_RTT_RESPONSE_TYPE:
90
1.01k
      return "RDP_RTT_RESPONSE_TYPE";
91
20
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
92
20
      return "RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME";
93
20
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
94
20
      return "RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS";
95
37
    case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
96
37
      return "RDP_RTT_REQUEST_TYPE_CONTINUOUS";
97
7
    case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
98
7
      return "RDP_RTT_REQUEST_TYPE_CONNECTTIME";
99
28
    case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
100
28
      return "RDP_BW_START_REQUEST_TYPE_CONTINUOUS";
101
14
    case RDP_BW_START_REQUEST_TYPE_TUNNEL:
102
14
      return "RDP_BW_START_REQUEST_TYPE_TUNNEL";
103
4
    case RDP_BW_START_REQUEST_TYPE_CONNECTTIME:
104
4
      return "RDP_BW_START_REQUEST_TYPE_CONNECTTIME";
105
19
    case RDP_BW_PAYLOAD_REQUEST_TYPE:
106
19
      return "RDP_BW_PAYLOAD_REQUEST_TYPE";
107
9
    case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
108
9
      return "RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME";
109
6
    case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
110
6
      return "RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS";
111
7
    case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
112
7
      return "RDP_BW_STOP_REQUEST_TYPE_TUNNEL";
113
8
    case RDP_NETCHAR_RESULTS_0x0840:
114
8
      return "RDP_NETCHAR_RESULTS_0x0840";
115
6
    case RDP_NETCHAR_RESULTS_0x0880:
116
6
      return "RDP_NETCHAR_RESULTS_0x0880";
117
8
    case RDP_NETCHAR_RESULTS_0x08C0:
118
8
      return "RDP_NETCHAR_RESULTS_0x08C0";
119
4.36k
    default:
120
4.36k
      return "UNKNOWN";
121
5.57k
  }
122
5.57k
}
123
124
static const char* autodetect_request_type_to_string_buffer(UINT32 requestType, char* buffer,
125
                                                            size_t size)
126
5.57k
{
127
5.57k
  const char* str = autodetect_request_type_to_string(requestType);
128
5.57k
  (void)_snprintf(buffer, size, "%s [0x%08" PRIx32 "]", str, requestType);
129
5.57k
  return buffer;
130
5.57k
}
131
132
static BOOL autodetect_send_rtt_measure_request(rdpAutoDetect* autodetect,
133
                                                WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
134
                                                UINT16 sequenceNumber)
135
0
{
136
0
  UINT16 requestType = 0;
137
0
  UINT16 sec_flags = 0;
138
0
  wStream* s = nullptr;
139
140
0
  WINPR_ASSERT(autodetect);
141
0
  WINPR_ASSERT(autodetect->context);
142
143
0
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
144
0
  if (!s)
145
0
    return FALSE;
146
147
0
  if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
148
0
    requestType = RDP_RTT_REQUEST_TYPE_CONNECTTIME;
149
0
  else
150
0
    requestType = RDP_RTT_REQUEST_TYPE_CONTINUOUS;
151
152
0
  WLog_Print(autodetect->log, WLOG_TRACE, "sending RTT Measure Request PDU");
153
0
  Stream_Write_UINT8(s, 0x06);                       /* headerLength (1 byte) */
154
0
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
155
0
  Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
156
0
  Stream_Write_UINT16(s, requestType);               /* requestType (2 bytes) */
157
0
  autodetect->rttMeasureStartTime = GetTickCount64();
158
0
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
159
0
                                      sec_flags | SEC_AUTODETECT_REQ);
160
0
}
161
162
static BOOL autodetect_send_rtt_measure_response(rdpAutoDetect* autodetect, UINT16 sequenceNumber)
163
2
{
164
2
  UINT16 sec_flags = 0;
165
2
  wStream* s = nullptr;
166
167
2
  WINPR_ASSERT(autodetect);
168
2
  WINPR_ASSERT(autodetect->context);
169
170
  /* Send the response PDU to the server */
171
2
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
172
173
2
  if (!s)
174
0
    return FALSE;
175
176
2
  WLog_Print(autodetect->log, WLOG_TRACE,
177
2
             "sending RTT Measure Response PDU (seqNumber=0x%" PRIx16 ")", sequenceNumber);
178
2
  Stream_Write_UINT8(s, 0x06);                        /* headerLength (1 byte) */
179
2
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE); /* headerTypeId (1 byte) */
180
2
  Stream_Write_UINT16(s, sequenceNumber);             /* sequenceNumber (2 bytes) */
181
2
  Stream_Write_UINT16(s, RDP_RTT_RESPONSE_TYPE);      /* responseType (1 byte) */
182
2
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
183
2
                                      sec_flags | SEC_AUTODETECT_RSP);
184
2
}
185
186
static BOOL autodetect_send_bandwidth_measure_start(rdpAutoDetect* autodetect,
187
                                                    WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
188
                                                    UINT16 sequenceNumber)
189
0
{
190
0
  UINT16 requestType = 0;
191
0
  UINT16 sec_flags = 0;
192
0
  wStream* s = nullptr;
193
194
0
  WINPR_ASSERT(autodetect);
195
0
  WINPR_ASSERT(autodetect->context);
196
197
0
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
198
0
  if (!s)
199
0
    return FALSE;
200
201
0
  if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
202
0
    requestType = RDP_BW_START_REQUEST_TYPE_CONNECTTIME;
203
0
  else
204
0
    requestType = RDP_BW_START_REQUEST_TYPE_CONTINUOUS;
205
206
0
  WLog_Print(autodetect->log, WLOG_TRACE,
207
0
             "sending Bandwidth Measure Start PDU(seqNumber=%" PRIu16 ")", sequenceNumber);
208
0
  Stream_Write_UINT8(s, 0x06);                       /* headerLength (1 byte) */
209
0
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
210
0
  Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
211
0
  Stream_Write_UINT16(s, requestType);               /* requestType (2 bytes) */
212
0
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
213
0
                                      sec_flags | SEC_AUTODETECT_REQ);
214
0
}
215
216
static BOOL
217
autodetect_send_bandwidth_measure_payload(rdpAutoDetect* autodetect,
218
                                          WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
219
                                          UINT16 sequenceNumber, UINT16 payloadLength)
220
0
{
221
0
  UINT16 sec_flags = 0;
222
0
  wStream* s = nullptr;
223
224
0
  WINPR_ASSERT(autodetect);
225
0
  WINPR_ASSERT(autodetect->context);
226
227
0
  WINPR_ASSERT(freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE);
228
229
0
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
230
0
  if (!s)
231
0
    return FALSE;
232
233
0
  WLog_Print(autodetect->log, WLOG_TRACE,
234
0
             "sending Bandwidth Measure Payload PDU -> payloadLength=%" PRIu16 "", payloadLength);
235
  /* 4-bytes aligned */
236
0
  payloadLength &= ~3;
237
238
0
  if (!Stream_EnsureRemainingCapacity(s, 8 + payloadLength))
239
0
  {
240
0
    WLog_Print(autodetect->log, WLOG_ERROR, "Failed to ensure %lu bytes in stream",
241
0
               8ul + payloadLength);
242
0
    Stream_Release(s);
243
0
    return FALSE;
244
0
  }
245
246
0
  Stream_Write_UINT8(s, 0x08);                         /* headerLength (1 byte) */
247
0
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST);   /* headerTypeId (1 byte) */
248
0
  Stream_Write_UINT16(s, sequenceNumber);              /* sequenceNumber (2 bytes) */
249
0
  Stream_Write_UINT16(s, RDP_BW_PAYLOAD_REQUEST_TYPE); /* requestType (2 bytes) */
250
0
  Stream_Write_UINT16(s, payloadLength);               /* payloadLength (2 bytes) */
251
  /* Random data (better measurement in case the line is compressed) */
252
0
  if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
253
0
  {
254
0
    Stream_Release(s);
255
0
    return FALSE;
256
0
  }
257
0
  Stream_Seek(s, payloadLength);
258
0
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
259
0
                                      sec_flags | SEC_AUTODETECT_REQ);
260
0
}
261
262
static BOOL autodetect_send_bandwidth_measure_stop(rdpAutoDetect* autodetect,
263
                                                   WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
264
                                                   UINT16 sequenceNumber, UINT16 payloadLength)
265
0
{
266
0
  UINT16 requestType = 0;
267
0
  UINT16 sec_flags = 0;
268
0
  wStream* s = nullptr;
269
270
0
  WINPR_ASSERT(autodetect);
271
0
  WINPR_ASSERT(autodetect->context);
272
273
0
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
274
0
  if (!s)
275
0
    return FALSE;
276
277
0
  if (freerdp_get_state(autodetect->context) < CONNECTION_STATE_ACTIVE)
278
0
    requestType = RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME;
279
0
  else
280
0
    requestType = RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS;
281
282
0
  if (requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
283
0
    payloadLength = 0;
284
285
0
  WLog_Print(autodetect->log, WLOG_TRACE,
286
0
             "sending Bandwidth Measure Stop PDU -> payloadLength=%" PRIu16 "", payloadLength);
287
  /* 4-bytes aligned */
288
0
  payloadLength &= ~3;
289
0
  Stream_Write_UINT8(s, requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME
290
0
                            ? 0x08
291
0
                            : 0x06);                 /* headerLength (1 byte) */
292
0
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
293
0
  Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
294
0
  Stream_Write_UINT16(s, requestType);               /* requestType (2 bytes) */
295
296
0
  if (requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
297
0
  {
298
0
    Stream_Write_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
299
300
0
    if (payloadLength > 0)
301
0
    {
302
0
      if (!Stream_EnsureRemainingCapacity(s, payloadLength))
303
0
      {
304
0
        WLog_Print(autodetect->log, WLOG_ERROR,
305
0
                   "Failed to ensure %" PRIu16 " bytes in stream", payloadLength);
306
0
        Stream_Release(s);
307
0
        return FALSE;
308
0
      }
309
310
      /* Random data (better measurement in case the line is compressed) */
311
0
      if (winpr_RAND(Stream_Pointer(s), payloadLength) < 0)
312
0
      {
313
0
        Stream_Release(s);
314
0
        return FALSE;
315
0
      }
316
0
      Stream_Seek(s, payloadLength);
317
0
    }
318
0
  }
319
320
0
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
321
0
                                      sec_flags | SEC_AUTODETECT_REQ);
322
0
}
323
324
static BOOL autodetect_send_bandwidth_measure_results(rdpAutoDetect* autodetect,
325
                                                      RDP_TRANSPORT_TYPE transport,
326
                                                      UINT16 responseType, UINT16 sequenceNumber)
327
126
{
328
126
  BOOL success = TRUE;
329
126
  UINT16 sec_flags = 0;
330
126
  UINT64 timeDelta = GetTickCount64();
331
332
126
  WINPR_ASSERT(autodetect);
333
126
  WINPR_ASSERT(autodetect->context);
334
335
  /* Compute the total time */
336
126
  if (autodetect->bandwidthMeasureStartTime > timeDelta)
337
0
  {
338
0
    WLog_Print(autodetect->log, WLOG_WARN,
339
0
               "Invalid bandwidthMeasureStartTime %" PRIu64 " > current %" PRIu64
340
0
               ", trimming to 0",
341
0
               autodetect->bandwidthMeasureStartTime, timeDelta);
342
0
    timeDelta = 0;
343
0
  }
344
126
  else
345
126
    timeDelta -= autodetect->bandwidthMeasureStartTime;
346
347
  /* Send the result PDU to the server */
348
126
  wStream* s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
349
350
126
  if (!s)
351
0
    return FALSE;
352
353
126
  WLog_Print(autodetect->log, WLOG_TRACE,
354
126
             "sending Bandwidth Measure Results PDU -> timeDelta=%" PRIu64 ", byteCount=%" PRIu32
355
126
             "",
356
126
             timeDelta, autodetect->bandwidthMeasureByteCount);
357
358
126
  Stream_Write_UINT8(s, 0x0E);                                   /* headerLength (1 byte) */
359
126
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE);            /* headerTypeId (1 byte) */
360
126
  Stream_Write_UINT16(s, sequenceNumber);                        /* sequenceNumber (2 bytes) */
361
126
  Stream_Write_UINT16(s, responseType);                          /* responseType (1 byte) */
362
126
  Stream_Write_UINT32(s, (UINT32)MIN(timeDelta, UINT32_MAX));    /* timeDelta (4 bytes) */
363
126
  Stream_Write_UINT32(s, autodetect->bandwidthMeasureByteCount); /* byteCount (4 bytes) */
364
126
  IFCALLRET(autodetect->ClientBandwidthMeasureResult, success, autodetect, transport,
365
126
            responseType, sequenceNumber, (UINT32)MIN(timeDelta, UINT32_MAX),
366
126
            autodetect->bandwidthMeasureByteCount);
367
368
126
  if (!success)
369
0
  {
370
0
    WLog_Print(autodetect->log, WLOG_ERROR, "ClientBandwidthMeasureResult failed");
371
0
    Stream_Release(s);
372
0
    return FALSE;
373
0
  }
374
375
126
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
376
126
                                      sec_flags | SEC_AUTODETECT_RSP);
377
126
}
378
379
static BOOL autodetect_send_netchar_result(rdpAutoDetect* autodetect,
380
                                           WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
381
                                           UINT16 sequenceNumber,
382
                                           const rdpNetworkCharacteristicsResult* result)
383
32
{
384
32
  UINT16 sec_flags = 0;
385
32
  wStream* s = nullptr;
386
387
32
  WINPR_ASSERT(autodetect);
388
32
  WINPR_ASSERT(autodetect->context);
389
390
32
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
391
392
32
  if (!s)
393
0
    return FALSE;
394
395
32
  WLog_Print(autodetect->log, WLOG_TRACE, "sending Network Characteristics Result PDU");
396
397
32
  switch (result->type)
398
32
  {
399
2
    case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT:
400
2
      Stream_Write_UINT8(s, 0x0E);                       /* headerLength (1 byte) */
401
2
      Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
402
2
      Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
403
2
      WINPR_ASSERT((result->type <= UINT16_MAX));
404
2
      WINPR_ASSERT((result->type >= 0));
405
2
      Stream_Write_UINT16(s, (UINT16)result->type);      /* requestType (2 bytes) */
406
2
      Stream_Write_UINT32(s, result->baseRTT);           /* baseRTT (4 bytes) */
407
2
      Stream_Write_UINT32(s, result->averageRTT);        /* averageRTT (4 bytes) */
408
2
      break;
409
1
    case RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT:
410
1
      Stream_Write_UINT8(s, 0x0E);                       /* headerLength (1 byte) */
411
1
      Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
412
1
      Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
413
1
      WINPR_ASSERT((result->type <= UINT16_MAX));
414
1
      WINPR_ASSERT((result->type >= 0));
415
1
      Stream_Write_UINT16(s, (UINT16)result->type);      /* requestType (2 bytes) */
416
1
      Stream_Write_UINT32(s, result->bandwidth);         /* bandwidth (4 bytes) */
417
1
      Stream_Write_UINT32(s, result->averageRTT);        /* averageRTT (4 bytes) */
418
1
      break;
419
29
    case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT:
420
29
      Stream_Write_UINT8(s, 0x12);                       /* headerLength (1 byte) */
421
29
      Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
422
29
      Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
423
29
      WINPR_ASSERT((result->type <= UINT16_MAX));
424
29
      WINPR_ASSERT((result->type >= 0));
425
29
      Stream_Write_UINT16(s, (UINT16)result->type);      /* requestType (2 bytes) */
426
29
      Stream_Write_UINT32(s, result->baseRTT);           /* baseRTT (4 bytes) */
427
29
      Stream_Write_UINT32(s, result->bandwidth);         /* bandwidth (4 bytes) */
428
29
      Stream_Write_UINT32(s, result->averageRTT);        /* averageRTT (4 bytes) */
429
29
      break;
430
0
    default:
431
0
      WINPR_ASSERT(FALSE);
432
0
      break;
433
32
  }
434
435
32
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
436
32
                                      sec_flags | SEC_AUTODETECT_REQ);
437
32
}
438
439
static FREERDP_AUTODETECT_STATE
440
autodetect_on_connect_time_auto_detect_begin_default(rdpAutoDetect* autodetect)
441
0
{
442
0
  WINPR_ASSERT(autodetect);
443
0
  WINPR_ASSERT(autodetect->RTTMeasureRequest);
444
445
0
  if (!autodetect->RTTMeasureRequest(autodetect, RDP_TRANSPORT_TCP, 0x23))
446
0
    return FREERDP_AUTODETECT_STATE_FAIL;
447
448
0
  return FREERDP_AUTODETECT_STATE_REQUEST;
449
0
}
450
451
static FREERDP_AUTODETECT_STATE
452
autodetect_on_connect_time_auto_detect_progress_default(rdpAutoDetect* autodetect)
453
0
{
454
0
  WINPR_ASSERT(autodetect);
455
456
0
  if (autodetect->state == FREERDP_AUTODETECT_STATE_RESPONSE ||
457
0
      autodetect->state == FREERDP_AUTODETECT_STATE_COMPLETE)
458
0
    return FREERDP_AUTODETECT_STATE_COMPLETE;
459
460
0
  return autodetect->state;
461
0
}
462
463
static BOOL autodetect_recv_rtt_measure_request(rdpAutoDetect* autodetect,
464
                                                WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
465
                                                WINPR_ATTR_UNUSED wStream* s,
466
                                                const AUTODETECT_REQ_PDU* autodetectReqPdu)
467
29
{
468
29
  WINPR_ASSERT(autodetect);
469
29
  WINPR_ASSERT(s);
470
29
  WINPR_ASSERT(autodetectReqPdu);
471
472
29
  if (autodetectReqPdu->headerLength != 0x06)
473
27
  {
474
27
    WLog_Print(autodetect->log, WLOG_ERROR,
475
27
               "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
476
27
               autodetectReqPdu->headerLength);
477
27
    return FALSE;
478
27
  }
479
480
2
  WLog_Print(autodetect->log, WLOG_TRACE, "received RTT Measure Request PDU");
481
  /* Send a response to the server */
482
2
  return autodetect_send_rtt_measure_response(autodetect, autodetectReqPdu->sequenceNumber);
483
29
}
484
485
static BOOL autodetect_recv_rtt_measure_response(rdpAutoDetect* autodetect,
486
                                                 RDP_TRANSPORT_TYPE transport,
487
                                                 WINPR_ATTR_UNUSED wStream* s,
488
                                                 const AUTODETECT_RSP_PDU* autodetectRspPdu)
489
47
{
490
47
  BOOL success = TRUE;
491
492
47
  WINPR_ASSERT(autodetect);
493
47
  WINPR_ASSERT(autodetectRspPdu);
494
495
47
  if (autodetectRspPdu->headerLength != 0x06)
496
8
  {
497
8
    WLog_Print(autodetect->log, WLOG_ERROR,
498
8
               "autodetectRspPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
499
8
               autodetectRspPdu->headerLength);
500
8
    return FALSE;
501
8
  }
502
503
39
  WLog_Print(autodetect->log, WLOG_TRACE, "received RTT Measure Response PDU");
504
39
  autodetect->netCharAverageRTT =
505
39
      (UINT32)MIN(GetTickCount64() - autodetect->rttMeasureStartTime, UINT32_MAX);
506
507
39
  if (autodetect->netCharBaseRTT == 0 ||
508
0
      autodetect->netCharBaseRTT > autodetect->netCharAverageRTT)
509
39
    autodetect->netCharBaseRTT = autodetect->netCharAverageRTT;
510
511
39
  IFCALLRET(autodetect->RTTMeasureResponse, success, autodetect, transport,
512
39
            autodetectRspPdu->sequenceNumber);
513
39
  if (!success)
514
0
    WLog_Print(autodetect->log, WLOG_WARN, "RTTMeasureResponse failed");
515
39
  return success;
516
47
}
517
518
static BOOL autodetect_recv_bandwidth_measure_start(rdpAutoDetect* autodetect,
519
                                                    WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
520
                                                    WINPR_ATTR_UNUSED wStream* s,
521
                                                    const AUTODETECT_REQ_PDU* autodetectReqPdu)
522
14
{
523
14
  WINPR_ASSERT(autodetect);
524
14
  WINPR_ASSERT(s);
525
14
  WINPR_ASSERT(autodetectReqPdu);
526
527
14
  if (autodetectReqPdu->headerLength != 0x06)
528
10
  {
529
10
    WLog_Print(autodetect->log, WLOG_ERROR,
530
10
               "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
531
10
               autodetectReqPdu->headerLength);
532
10
    return FALSE;
533
10
  }
534
535
4
  WLog_Print(autodetect->log, WLOG_TRACE,
536
4
             "received Bandwidth Measure Start PDU - time=%" PRIu64 "", GetTickCount64());
537
  /* Initialize bandwidth measurement parameters */
538
4
  autodetect->bandwidthMeasureStartTime = GetTickCount64();
539
4
  autodetect->bandwidthMeasureByteCount = 0;
540
541
  /* Continuous Auto-Detection: mark the start of the measurement */
542
4
  if (autodetectReqPdu->requestType == RDP_BW_START_REQUEST_TYPE_CONTINUOUS)
543
2
  {
544
2
    autodetect->bandwidthMeasureStarted = TRUE;
545
2
  }
546
547
4
  return TRUE;
548
14
}
549
550
static BOOL
551
autodetect_recv_bandwidth_measure_payload(rdpAutoDetect* autodetect,
552
                                          WINPR_ATTR_UNUSED RDP_TRANSPORT_TYPE transport,
553
                                          wStream* s, const AUTODETECT_REQ_PDU* autodetectReqPdu)
554
40
{
555
40
  UINT16 payloadLength = 0;
556
557
40
  WINPR_ASSERT(autodetect);
558
40
  WINPR_ASSERT(s);
559
40
  WINPR_ASSERT(autodetectReqPdu);
560
561
40
  if (autodetectReqPdu->headerLength != 0x08)
562
8
  {
563
8
    WLog_Print(autodetect->log, WLOG_ERROR,
564
8
               "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8 "]",
565
8
               autodetectReqPdu->headerLength);
566
8
    return FALSE;
567
8
  }
568
569
32
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
570
2
    return FALSE;
571
572
30
  Stream_Read_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
573
30
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
574
2
    return FALSE;
575
28
  Stream_Seek(s, payloadLength);
576
577
28
  WLog_Print(autodetect->log, WLOG_DEBUG,
578
28
             "received Bandwidth Measure Payload PDU -> payloadLength=%" PRIu16 "",
579
28
             payloadLength);
580
  /* Add the payload length to the bandwidth measurement parameters */
581
28
  autodetect->bandwidthMeasureByteCount += payloadLength;
582
28
  return TRUE;
583
30
}
584
585
static BOOL autodetect_recv_bandwidth_measure_stop(rdpAutoDetect* autodetect,
586
                                                   RDP_TRANSPORT_TYPE transport, wStream* s,
587
                                                   const AUTODETECT_REQ_PDU* autodetectReqPdu)
588
142
{
589
142
  UINT16 payloadLength = 0;
590
142
  UINT16 responseType = 0;
591
592
142
  WINPR_ASSERT(autodetect);
593
142
  WINPR_ASSERT(s);
594
142
  WINPR_ASSERT(autodetectReqPdu);
595
596
142
  if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
597
123
  {
598
123
    if (autodetectReqPdu->headerLength != 0x08)
599
8
    {
600
8
      WLog_Print(autodetect->log, WLOG_ERROR,
601
8
                 "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8 "]",
602
8
                 autodetectReqPdu->headerLength);
603
8
      return FALSE;
604
8
    }
605
606
115
    if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
607
2
      return FALSE;
608
609
113
    Stream_Read_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
610
113
  }
611
19
  else
612
19
  {
613
19
    if (autodetectReqPdu->headerLength != 0x06)
614
4
    {
615
4
      WLog_Print(autodetect->log, WLOG_ERROR,
616
4
                 "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
617
4
                 autodetectReqPdu->headerLength);
618
4
      return FALSE;
619
4
    }
620
621
15
    payloadLength = 0;
622
15
  }
623
624
128
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
625
2
    return FALSE;
626
126
  Stream_Seek(s, payloadLength);
627
628
126
  WLog_Print(autodetect->log, WLOG_TRACE,
629
126
             "received Bandwidth Measure Stop PDU -> payloadLength=%" PRIu16 "", payloadLength);
630
  /* Add the payload length to the bandwidth measurement parameters */
631
126
  autodetect->bandwidthMeasureByteCount += payloadLength;
632
633
  /* Continuous Auto-Detection: mark the stop of the measurement */
634
126
  if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
635
6
  {
636
6
    autodetect->bandwidthMeasureStarted = FALSE;
637
6
  }
638
639
  /* Send a response the server */
640
126
  responseType = autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME
641
126
                     ? RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME
642
126
                     : RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS;
643
126
  return autodetect_send_bandwidth_measure_results(autodetect, transport, responseType,
644
126
                                                   autodetectReqPdu->sequenceNumber);
645
128
}
646
647
static BOOL autodetect_recv_bandwidth_measure_results(rdpAutoDetect* autodetect,
648
                                                      RDP_TRANSPORT_TYPE transport, wStream* s,
649
                                                      const AUTODETECT_RSP_PDU* autodetectRspPdu)
650
31
{
651
31
  UINT32 timeDelta = 0;
652
31
  UINT32 byteCount = 0;
653
31
  BOOL success = TRUE;
654
655
31
  WINPR_ASSERT(autodetect);
656
31
  WINPR_ASSERT(s);
657
31
  WINPR_ASSERT(autodetectRspPdu);
658
659
31
  if (autodetectRspPdu->headerLength != 0x0E)
660
10
  {
661
10
    WLog_Print(autodetect->log, WLOG_ERROR,
662
10
               "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
663
10
               autodetectRspPdu->headerLength);
664
10
    return FALSE;
665
10
  }
666
667
21
  WLog_Print(autodetect->log, WLOG_TRACE, "received Bandwidth Measure Results PDU");
668
21
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
669
2
    return FALSE;
670
19
  Stream_Read_UINT32(s, timeDelta); /* timeDelta (4 bytes) */
671
19
  Stream_Read_UINT32(s, byteCount); /* byteCount (4 bytes) */
672
673
19
  IFCALLRET(autodetect->BandwidthMeasureResults, success, autodetect, transport,
674
19
            autodetectRspPdu->sequenceNumber, autodetectRspPdu->responseType, timeDelta,
675
19
            byteCount);
676
19
  if (!success)
677
0
    WLog_Print(autodetect->log, WLOG_WARN, "BandwidthMeasureResults failed");
678
19
  return success;
679
21
}
680
681
static BOOL autodetect_recv_netchar_sync(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
682
                                         wStream* s, const AUTODETECT_RSP_PDU* autodetectRspPdu)
683
14
{
684
14
  UINT32 bandwidth = 0;
685
14
  UINT32 rtt = 0;
686
14
  BOOL success = TRUE;
687
688
14
  WINPR_ASSERT(autodetect);
689
14
  WINPR_ASSERT(s);
690
14
  WINPR_ASSERT(autodetectRspPdu);
691
692
14
  if (autodetectRspPdu->headerLength != 0x0E)
693
5
  {
694
5
    WLog_Print(autodetect->log, WLOG_ERROR,
695
5
               "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
696
5
               autodetectRspPdu->headerLength);
697
5
    return FALSE;
698
5
  }
699
9
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
700
2
    return FALSE;
701
702
  /* bandwidth and averageRTT fields are present (baseRTT field is not) */
703
7
  Stream_Read_UINT32(s, bandwidth); /* bandwidth (4 bytes) */
704
7
  Stream_Read_UINT32(s, rtt);       /* rtt (4 bytes) */
705
706
7
  WLog_Print(autodetect->log, WLOG_TRACE,
707
7
             "received Network Characteristics Sync PDU -> bandwidth=%" PRIu32 ", rtt=%" PRIu32
708
7
             "",
709
7
             bandwidth, rtt);
710
711
7
  IFCALLRET(autodetect->NetworkCharacteristicsSync, success, autodetect, transport,
712
7
            autodetectRspPdu->sequenceNumber, bandwidth, rtt);
713
7
  if (!success)
714
0
    WLog_Print(autodetect->log, WLOG_WARN, "NetworkCharacteristicsSync failed");
715
7
  return success;
716
9
}
717
718
static BOOL autodetect_recv_netchar_request(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
719
                                            wStream* s, const AUTODETECT_REQ_PDU* autodetectReqPdu)
720
69
{
721
69
  rdpNetworkCharacteristicsResult result = {
722
69
    .type = RDP_NETCHAR_RESERVED, .baseRTT = 0, .averageRTT = 0, .bandwidth = 0
723
69
  };
724
69
  BOOL success = TRUE;
725
726
69
  WINPR_ASSERT(autodetect);
727
69
  WINPR_ASSERT(s);
728
69
  WINPR_ASSERT(autodetectReqPdu);
729
730
69
  switch (autodetectReqPdu->requestType)
731
69
  {
732
13
    case RDP_NETCHAR_RESULTS_0x0840:
733
734
      /* baseRTT and averageRTT fields are present (bandwidth field is not) */
735
13
      if (autodetectReqPdu->headerLength != 0x0E)
736
4
      {
737
4
        WLog_Print(autodetect->log, WLOG_ERROR,
738
4
                   "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
739
4
                   autodetectReqPdu->headerLength);
740
4
        return FALSE;
741
4
      }
742
9
      if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
743
2
        return FALSE;
744
745
7
      result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT;
746
7
      Stream_Read_UINT32(s, result.baseRTT);    /* baseRTT (4 bytes) */
747
7
      Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
748
7
      break;
749
750
20
    case RDP_NETCHAR_RESULTS_0x0880:
751
752
      /* bandwidth and averageRTT fields are present (baseRTT field is not) */
753
20
      if (autodetectReqPdu->headerLength != 0x0E)
754
5
      {
755
5
        WLog_Print(autodetect->log, WLOG_ERROR,
756
5
                   "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
757
5
                   autodetectReqPdu->headerLength);
758
5
        return FALSE;
759
5
      }
760
15
      if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
761
2
        return FALSE;
762
763
13
      result.type = RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT;
764
13
      Stream_Read_UINT32(s, result.bandwidth);  /* bandwidth (4 bytes) */
765
13
      Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
766
13
      break;
767
768
36
    case RDP_NETCHAR_RESULTS_0x08C0:
769
770
      /* baseRTT, bandwidth, and averageRTT fields are present */
771
36
      if (autodetectReqPdu->headerLength != 0x12)
772
4
      {
773
4
        WLog_Print(autodetect->log, WLOG_ERROR,
774
4
                   "autodetectReqPdu->headerLength != 0x012 [0x%02" PRIx8 "]",
775
4
                   autodetectReqPdu->headerLength);
776
4
        return FALSE;
777
4
      }
778
32
      if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 12))
779
2
        return FALSE;
780
781
30
      result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT;
782
30
      Stream_Read_UINT32(s, result.baseRTT);    /* baseRTT (4 bytes) */
783
30
      Stream_Read_UINT32(s, result.bandwidth);  /* bandwidth (4 bytes) */
784
30
      Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
785
30
      break;
786
787
0
    default:
788
0
      WINPR_ASSERT(FALSE);
789
0
      break;
790
69
  }
791
792
50
  WLog_Print(autodetect->log, WLOG_TRACE,
793
50
             "received Network Characteristics Result PDU -> baseRTT=%" PRIu32
794
50
             ", bandwidth=%" PRIu32 ", averageRTT=%" PRIu32 "",
795
50
             result.baseRTT, result.bandwidth, result.averageRTT);
796
797
50
  IFCALLRET(autodetect->NetworkCharacteristicsResult, success, autodetect, transport,
798
50
            autodetectReqPdu->sequenceNumber, &result);
799
50
  if (!success)
800
32
    WLog_Print(autodetect->log, WLOG_WARN, "NetworkCharacteristicsResult failed");
801
50
  return success;
802
69
}
803
804
state_run_t autodetect_recv_request_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
805
                                           wStream* s)
806
17.9k
{
807
17.9k
  AUTODETECT_REQ_PDU autodetectReqPdu = WINPR_C_ARRAY_INIT;
808
17.9k
  const rdpSettings* settings = nullptr;
809
17.9k
  BOOL success = FALSE;
810
811
17.9k
  WINPR_ASSERT(autodetect);
812
17.9k
  WINPR_ASSERT(autodetect->context);
813
814
17.9k
  settings = autodetect->context->settings;
815
17.9k
  WINPR_ASSERT(settings);
816
817
17.9k
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
818
13.8k
    return STATE_RUN_FAILED;
819
820
4.09k
  Stream_Read_UINT8(s, autodetectReqPdu.headerLength);    /* headerLength (1 byte) */
821
4.09k
  Stream_Read_UINT8(s, autodetectReqPdu.headerTypeId);    /* headerTypeId (1 byte) */
822
4.09k
  Stream_Read_UINT16(s, autodetectReqPdu.sequenceNumber); /* sequenceNumber (2 bytes) */
823
4.09k
  Stream_Read_UINT16(s, autodetectReqPdu.requestType);    /* requestType (2 bytes) */
824
825
4.09k
  if (WLog_IsLevelActive(autodetect->log, WLOG_TRACE))
826
0
  {
827
0
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
828
0
    const char* requestTypeStr = autodetect_request_type_to_string_buffer(
829
0
        autodetectReqPdu.requestType, rbuffer, sizeof(rbuffer));
830
831
0
    char hbuffer[128] = WINPR_C_ARRAY_INIT;
832
0
    const char* headerStr =
833
0
        autodetect_header_type_string(autodetectReqPdu.headerTypeId, hbuffer, sizeof(hbuffer));
834
835
0
    WLog_Print(autodetect->log, WLOG_TRACE,
836
0
               "rdp_recv_autodetect_request_packet: headerLength=%" PRIu8
837
0
               ", headerTypeId=%s, sequenceNumber=%" PRIu16 ", requestType=%s",
838
0
               autodetectReqPdu.headerLength, headerStr, autodetectReqPdu.sequenceNumber,
839
0
               requestTypeStr);
840
0
  }
841
842
4.09k
  if (!freerdp_settings_get_bool(settings, FreeRDP_NetworkAutoDetect))
843
0
  {
844
0
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
845
0
    const char* requestTypeStr = autodetect_request_type_to_string_buffer(
846
0
        autodetectReqPdu.requestType, rbuffer, sizeof(rbuffer));
847
848
0
    WLog_Print(autodetect->log, WLOG_WARN,
849
0
               "Received a [MS-RDPBCGR] 2.2.14.1.1 RTT Measure Request [%s] "
850
0
               "message but support was not enabled",
851
0
               requestTypeStr);
852
0
    goto fail;
853
0
  }
854
855
4.09k
  if (autodetectReqPdu.headerTypeId != TYPE_ID_AUTODETECT_REQUEST)
856
2.10k
  {
857
2.10k
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
858
2.10k
    const char* requestTypeStr = autodetect_request_type_to_string_buffer(
859
2.10k
        autodetectReqPdu.requestType, rbuffer, sizeof(rbuffer));
860
2.10k
    char hbuffer[128] = WINPR_C_ARRAY_INIT;
861
2.10k
    const char* headerStr =
862
2.10k
        autodetect_header_type_string(autodetectReqPdu.headerTypeId, hbuffer, sizeof(hbuffer));
863
864
2.10k
    WLog_Print(autodetect->log, WLOG_ERROR,
865
2.10k
               "Received a [MS-RDPBCGR] 2.2.14.1.1 RTT Measure Request [%s] "
866
2.10k
               "message with invalid headerTypeId=%s",
867
2.10k
               requestTypeStr, headerStr);
868
2.10k
    goto fail;
869
2.10k
  }
870
871
1.99k
  if (!IFCALLRESULT(TRUE, autodetect->RequestReceived, autodetect, transport,
872
1.99k
                    autodetectReqPdu.requestType, autodetectReqPdu.sequenceNumber))
873
0
    goto fail;
874
875
1.99k
  switch (autodetectReqPdu.requestType)
876
1.99k
  {
877
26
    case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
878
29
    case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
879
      /* RTT Measure Request (RDP_RTT_REQUEST) - MS-RDPBCGR 2.2.14.1.1 */
880
29
      success =
881
29
          autodetect_recv_rtt_measure_request(autodetect, transport, s, &autodetectReqPdu);
882
29
      break;
883
884
9
    case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
885
12
    case RDP_BW_START_REQUEST_TYPE_TUNNEL:
886
14
    case RDP_BW_START_REQUEST_TYPE_CONNECTTIME:
887
      /* Bandwidth Measure Start (RDP_BW_START) - MS-RDPBCGR 2.2.14.1.2 */
888
14
      success = autodetect_recv_bandwidth_measure_start(autodetect, transport, s,
889
14
                                                        &autodetectReqPdu);
890
14
      break;
891
892
40
    case RDP_BW_PAYLOAD_REQUEST_TYPE:
893
      /* Bandwidth Measure Payload (RDP_BW_PAYLOAD) - MS-RDPBCGR 2.2.14.1.3 */
894
40
      success = autodetect_recv_bandwidth_measure_payload(autodetect, transport, s,
895
40
                                                          &autodetectReqPdu);
896
40
      break;
897
898
123
    case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
899
131
    case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
900
142
    case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
901
      /* Bandwidth Measure Stop (RDP_BW_STOP) - MS-RDPBCGR 2.2.14.1.4 */
902
142
      success =
903
142
          autodetect_recv_bandwidth_measure_stop(autodetect, transport, s, &autodetectReqPdu);
904
142
      break;
905
906
13
    case RDP_NETCHAR_RESULTS_0x0840:
907
33
    case RDP_NETCHAR_RESULTS_0x0880:
908
69
    case RDP_NETCHAR_RESULTS_0x08C0:
909
      /* Network Characteristics Result (RDP_NETCHAR_RESULT) - MS-RDPBCGR 2.2.14.1.5 */
910
69
      success = autodetect_recv_netchar_request(autodetect, transport, s, &autodetectReqPdu);
911
69
      break;
912
913
1.69k
    default:
914
1.69k
      WLog_Print(autodetect->log, WLOG_ERROR, "Unknown requestType=0x%04" PRIx16,
915
1.69k
                 autodetectReqPdu.requestType);
916
1.69k
      break;
917
1.99k
  }
918
919
4.09k
fail:
920
4.09k
  if (success)
921
50
    autodetect->state = FREERDP_AUTODETECT_STATE_REQUEST;
922
4.04k
  else
923
4.04k
    autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
924
4.09k
  return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
925
1.99k
}
926
927
state_run_t autodetect_recv_response_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
928
                                            wStream* s)
929
17.9k
{
930
17.9k
  AUTODETECT_RSP_PDU autodetectRspPdu = WINPR_C_ARRAY_INIT;
931
17.9k
  const rdpSettings* settings = nullptr;
932
17.9k
  BOOL success = FALSE;
933
934
17.9k
  WINPR_ASSERT(autodetect);
935
17.9k
  WINPR_ASSERT(autodetect->context);
936
17.9k
  WINPR_ASSERT(s);
937
938
17.9k
  settings = autodetect->context->settings;
939
17.9k
  WINPR_ASSERT(settings);
940
941
17.9k
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
942
14.3k
    goto fail;
943
944
3.65k
  Stream_Read_UINT8(s, autodetectRspPdu.headerLength);    /* headerLength (1 byte) */
945
3.65k
  Stream_Read_UINT8(s, autodetectRspPdu.headerTypeId);    /* headerTypeId (1 byte) */
946
3.65k
  Stream_Read_UINT16(s, autodetectRspPdu.sequenceNumber); /* sequenceNumber (2 bytes) */
947
3.65k
  Stream_Read_UINT16(s, autodetectRspPdu.responseType);   /* responseType (2 bytes) */
948
949
3.65k
  if (WLog_IsLevelActive(autodetect->log, WLOG_TRACE))
950
0
  {
951
0
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
952
0
    const char* requestStr = autodetect_request_type_to_string_buffer(
953
0
        autodetectRspPdu.responseType, rbuffer, sizeof(rbuffer));
954
0
    char hbuffer[128] = WINPR_C_ARRAY_INIT;
955
0
    const char* headerStr =
956
0
        autodetect_header_type_string(autodetectRspPdu.headerTypeId, hbuffer, sizeof(hbuffer));
957
958
0
    WLog_Print(autodetect->log, WLOG_TRACE,
959
0
               "rdp_recv_autodetect_response_packet: headerLength=%" PRIu8 ", headerTypeId=%s"
960
0
               ", sequenceNumber=%" PRIu16 ", requestType=%s",
961
0
               autodetectRspPdu.headerLength, headerStr, autodetectRspPdu.sequenceNumber,
962
0
               requestStr);
963
0
  }
964
965
3.65k
  if (!freerdp_settings_get_bool(settings, FreeRDP_NetworkAutoDetect))
966
0
  {
967
0
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
968
969
0
    const char* requestStr = autodetect_request_type_to_string_buffer(
970
0
        autodetectRspPdu.responseType, rbuffer, sizeof(rbuffer));
971
972
0
    WLog_Print(autodetect->log, WLOG_WARN,
973
0
               "Received a [MS-RDPBCGR] 2.2.14.2.1 RTT Measure Response [%s] "
974
0
               "message but support was not enabled",
975
0
               requestStr);
976
0
    return STATE_RUN_FAILED;
977
0
  }
978
979
3.65k
  if (autodetectRspPdu.headerTypeId != TYPE_ID_AUTODETECT_RESPONSE)
980
3.47k
  {
981
3.47k
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
982
3.47k
    const char* requestStr = autodetect_request_type_to_string_buffer(
983
3.47k
        autodetectRspPdu.responseType, rbuffer, sizeof(rbuffer));
984
3.47k
    char hbuffer[128] = WINPR_C_ARRAY_INIT;
985
3.47k
    const char* headerStr =
986
3.47k
        autodetect_header_type_string(autodetectRspPdu.headerTypeId, hbuffer, sizeof(hbuffer));
987
3.47k
    WLog_Print(autodetect->log, WLOG_ERROR,
988
3.47k
               "Received a [MS-RDPBCGR] 2.2.14.2.1 RTT Measure Response [%s] "
989
3.47k
               "message with invalid headerTypeId=%s",
990
3.47k
               requestStr, headerStr);
991
3.47k
    goto fail;
992
3.47k
  }
993
994
180
  if (!IFCALLRESULT(TRUE, autodetect->ResponseReceived, autodetect, transport,
995
180
                    autodetectRspPdu.responseType, autodetectRspPdu.sequenceNumber))
996
0
    goto fail;
997
998
180
  switch (autodetectRspPdu.responseType)
999
180
  {
1000
47
    case RDP_RTT_RESPONSE_TYPE:
1001
      /* RTT Measure Response (RDP_RTT_RESPONSE) - MS-RDPBCGR 2.2.14.2.1 */
1002
47
      success =
1003
47
          autodetect_recv_rtt_measure_response(autodetect, transport, s, &autodetectRspPdu);
1004
47
      break;
1005
1006
9
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
1007
31
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
1008
      /* Bandwidth Measure Results (RDP_BW_RESULTS) - MS-RDPBCGR 2.2.14.2.2 */
1009
31
      success = autodetect_recv_bandwidth_measure_results(autodetect, transport, s,
1010
31
                                                          &autodetectRspPdu);
1011
31
      break;
1012
1013
14
    case RDP_NETCHAR_SYNC_RESPONSE_TYPE:
1014
      /* Network Characteristics Sync (RDP_NETCHAR_SYNC) - MS-RDPBCGR 2.2.14.2.3 */
1015
14
      success = autodetect_recv_netchar_sync(autodetect, transport, s, &autodetectRspPdu);
1016
14
      break;
1017
1018
88
    default:
1019
88
      WLog_Print(autodetect->log, WLOG_ERROR, "Unknown responseType=0x%04" PRIx16,
1020
88
                 autodetectRspPdu.responseType);
1021
88
      break;
1022
180
  }
1023
1024
17.9k
fail:
1025
17.9k
  if (success)
1026
65
  {
1027
65
    if (autodetectRspPdu.responseType == RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME)
1028
2
      autodetect->state = FREERDP_AUTODETECT_STATE_COMPLETE;
1029
63
    else
1030
63
      autodetect->state = FREERDP_AUTODETECT_STATE_RESPONSE;
1031
65
  }
1032
17.9k
  else
1033
17.9k
    autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
1034
1035
17.9k
  return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
1036
180
}
1037
1038
void autodetect_on_connect_time_auto_detect_begin(rdpAutoDetect* autodetect)
1039
0
{
1040
0
  WINPR_ASSERT(autodetect);
1041
0
  WINPR_ASSERT(autodetect->OnConnectTimeAutoDetectBegin);
1042
1043
0
  autodetect->state = autodetect->OnConnectTimeAutoDetectBegin(autodetect);
1044
0
}
1045
1046
void autodetect_on_connect_time_auto_detect_progress(rdpAutoDetect* autodetect)
1047
0
{
1048
0
  WINPR_ASSERT(autodetect);
1049
0
  WINPR_ASSERT(autodetect->OnConnectTimeAutoDetectProgress);
1050
1051
0
  autodetect->state = autodetect->OnConnectTimeAutoDetectProgress(autodetect);
1052
0
}
1053
1054
rdpAutoDetect* autodetect_new(rdpContext* context)
1055
17.9k
{
1056
17.9k
  rdpAutoDetect* autoDetect = (rdpAutoDetect*)calloc(1, sizeof(rdpAutoDetect));
1057
17.9k
  if (!autoDetect)
1058
0
    return nullptr;
1059
17.9k
  autoDetect->context = context;
1060
17.9k
  autoDetect->log = WLog_Get(AUTODETECT_TAG);
1061
1062
17.9k
  return autoDetect;
1063
17.9k
}
1064
1065
void autodetect_free(rdpAutoDetect* autoDetect)
1066
17.9k
{
1067
17.9k
  free(autoDetect);
1068
17.9k
}
1069
1070
void autodetect_register_server_callbacks(rdpAutoDetect* autodetect)
1071
8.58k
{
1072
8.58k
  WINPR_ASSERT(autodetect);
1073
1074
8.58k
  autodetect->RTTMeasureRequest = autodetect_send_rtt_measure_request;
1075
8.58k
  autodetect->BandwidthMeasureStart = autodetect_send_bandwidth_measure_start;
1076
8.58k
  autodetect->BandwidthMeasurePayload = autodetect_send_bandwidth_measure_payload;
1077
8.58k
  autodetect->BandwidthMeasureStop = autodetect_send_bandwidth_measure_stop;
1078
8.58k
  autodetect->NetworkCharacteristicsResult = autodetect_send_netchar_result;
1079
1080
  /*
1081
   * Default handlers for Connect-Time Auto-Detection
1082
   * (MAY be overridden by the API user)
1083
   */
1084
8.58k
  autodetect->OnConnectTimeAutoDetectBegin = autodetect_on_connect_time_auto_detect_begin_default;
1085
8.58k
  autodetect->OnConnectTimeAutoDetectProgress =
1086
8.58k
      autodetect_on_connect_time_auto_detect_progress_default;
1087
8.58k
}
1088
1089
FREERDP_AUTODETECT_STATE autodetect_get_state(rdpAutoDetect* autodetect)
1090
0
{
1091
0
  WINPR_ASSERT(autodetect);
1092
0
  return autodetect->state;
1093
0
}
1094
1095
rdpAutoDetect* autodetect_get(rdpContext* context)
1096
0
{
1097
0
  WINPR_ASSERT(context);
1098
0
  WINPR_ASSERT(context->rdp);
1099
0
  return context->rdp->autodetect;
1100
0
}