Coverage Report

Created: 2026-03-04 06:17

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.00k
#define TYPE_ID_AUTODETECT_REQUEST 0x00
28
3.48k
#define TYPE_ID_AUTODETECT_RESPONSE 0x01
29
30
61
#define RDP_RTT_REQUEST_TYPE_CONTINUOUS 0x0001
31
30
#define RDP_RTT_REQUEST_TYPE_CONNECTTIME 0x1001
32
33
1.05k
#define RDP_RTT_RESPONSE_TYPE 0x0000
34
35
37
#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
101
#define RDP_BW_PAYLOAD_REQUEST_TYPE 0x0002
39
267
#define RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME 0x002B
40
183
#define RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS 0x0429
41
100
#define RDP_BW_STOP_REQUEST_TYPE_TUNNEL 0x0629
42
43
16
#define RDP_NETCHAR_SYNC_RESPONSE_TYPE 0x0018
44
45
31
#define RDP_NETCHAR_RESULTS_0x0840 0x0840U
46
58
#define RDP_NETCHAR_RESULTS_0x0880 0x0880U
47
73
#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.30k
{
67
5.30k
  const char* str = nullptr;
68
5.30k
  switch (headerType)
69
5.30k
  {
70
1.25k
    case TYPE_ID_AUTODETECT_REQUEST:
71
1.25k
      str = "TYPE_ID_AUTODETECT_REQUEST";
72
1.25k
      break;
73
119
    case TYPE_ID_AUTODETECT_RESPONSE:
74
119
      str = "TYPE_ID_AUTODETECT_RESPONSE";
75
119
      break;
76
3.93k
    default:
77
3.93k
      str = "TYPE_ID_AUTODETECT_UNKNOWN";
78
3.93k
      break;
79
5.30k
  }
80
81
5.30k
  (void)_snprintf(buffer, size, "%s [0x%08" PRIx8 "]", str, headerType);
82
5.30k
  return buffer;
83
5.30k
}
84
85
static const char* autodetect_request_type_to_string(UINT32 requestType)
86
5.30k
{
87
5.30k
  switch (requestType)
88
5.30k
  {
89
1.00k
    case RDP_RTT_RESPONSE_TYPE:
90
1.00k
      return "RDP_RTT_RESPONSE_TYPE";
91
27
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
92
27
      return "RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME";
93
8
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
94
8
      return "RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS";
95
41
    case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
96
41
      return "RDP_RTT_REQUEST_TYPE_CONTINUOUS";
97
6
    case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
98
6
      return "RDP_RTT_REQUEST_TYPE_CONNECTTIME";
99
25
    case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
100
25
      return "RDP_BW_START_REQUEST_TYPE_CONTINUOUS";
101
15
    case RDP_BW_START_REQUEST_TYPE_TUNNEL:
102
15
      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
22
    case RDP_BW_PAYLOAD_REQUEST_TYPE:
106
22
      return "RDP_BW_PAYLOAD_REQUEST_TYPE";
107
8
    case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
108
8
      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
2
    case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
112
2
      return "RDP_BW_STOP_REQUEST_TYPE_TUNNEL";
113
9
    case RDP_NETCHAR_RESULTS_0x0840:
114
9
      return "RDP_NETCHAR_RESULTS_0x0840";
115
7
    case RDP_NETCHAR_RESULTS_0x0880:
116
7
      return "RDP_NETCHAR_RESULTS_0x0880";
117
8
    case RDP_NETCHAR_RESULTS_0x08C0:
118
8
      return "RDP_NETCHAR_RESULTS_0x08C0";
119
4.11k
    default:
120
4.11k
      return "UNKNOWN";
121
5.30k
  }
122
5.30k
}
123
124
static const char* autodetect_request_type_to_string_buffer(UINT32 requestType, char* buffer,
125
                                                            size_t size)
126
5.30k
{
127
5.30k
  const char* str = autodetect_request_type_to_string(requestType);
128
5.30k
  (void)_snprintf(buffer, size, "%s [0x%08" PRIx32 "]", str, requestType);
129
5.30k
  return buffer;
130
5.30k
}
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
3
{
164
3
  UINT16 sec_flags = 0;
165
3
  wStream* s = nullptr;
166
167
3
  WINPR_ASSERT(autodetect);
168
3
  WINPR_ASSERT(autodetect->context);
169
170
  /* Send the response PDU to the server */
171
3
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
172
173
3
  if (!s)
174
0
    return FALSE;
175
176
3
  WLog_Print(autodetect->log, WLOG_TRACE,
177
3
             "sending RTT Measure Response PDU (seqNumber=0x%" PRIx16 ")", sequenceNumber);
178
3
  Stream_Write_UINT8(s, 0x06);                        /* headerLength (1 byte) */
179
3
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE); /* headerTypeId (1 byte) */
180
3
  Stream_Write_UINT16(s, sequenceNumber);             /* sequenceNumber (2 bytes) */
181
3
  Stream_Write_UINT16(s, RDP_RTT_RESPONSE_TYPE);      /* responseType (1 byte) */
182
3
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
183
3
                                      sec_flags | SEC_AUTODETECT_RSP);
184
3
}
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
83
{
328
83
  BOOL success = TRUE;
329
83
  UINT16 sec_flags = 0;
330
83
  UINT64 timeDelta = GetTickCount64();
331
332
83
  WINPR_ASSERT(autodetect);
333
83
  WINPR_ASSERT(autodetect->context);
334
335
  /* Compute the total time */
336
83
  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
83
  else
345
83
    timeDelta -= autodetect->bandwidthMeasureStartTime;
346
347
  /* Send the result PDU to the server */
348
83
  wStream* s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
349
350
83
  if (!s)
351
0
    return FALSE;
352
353
83
  WLog_Print(autodetect->log, WLOG_TRACE,
354
83
             "sending Bandwidth Measure Results PDU -> timeDelta=%" PRIu64 ", byteCount=%" PRIu32
355
83
             "",
356
83
             timeDelta, autodetect->bandwidthMeasureByteCount);
357
358
83
  Stream_Write_UINT8(s, 0x0E);                                   /* headerLength (1 byte) */
359
83
  Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_RESPONSE);            /* headerTypeId (1 byte) */
360
83
  Stream_Write_UINT16(s, sequenceNumber);                        /* sequenceNumber (2 bytes) */
361
83
  Stream_Write_UINT16(s, responseType);                          /* responseType (1 byte) */
362
83
  Stream_Write_UINT32(s, (UINT32)MIN(timeDelta, UINT32_MAX));    /* timeDelta (4 bytes) */
363
83
  Stream_Write_UINT32(s, autodetect->bandwidthMeasureByteCount); /* byteCount (4 bytes) */
364
83
  IFCALLRET(autodetect->ClientBandwidthMeasureResult, success, autodetect, transport,
365
83
            responseType, sequenceNumber, (UINT32)MIN(timeDelta, UINT32_MAX),
366
83
            autodetect->bandwidthMeasureByteCount);
367
368
83
  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
83
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
376
83
                                      sec_flags | SEC_AUTODETECT_RSP);
377
83
}
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
17
{
384
17
  UINT16 sec_flags = 0;
385
17
  wStream* s = nullptr;
386
387
17
  WINPR_ASSERT(autodetect);
388
17
  WINPR_ASSERT(autodetect->context);
389
390
17
  s = rdp_message_channel_pdu_init(autodetect->context->rdp, &sec_flags);
391
392
17
  if (!s)
393
0
    return FALSE;
394
395
17
  WLog_Print(autodetect->log, WLOG_TRACE, "sending Network Characteristics Result PDU");
396
397
17
  switch (result->type)
398
17
  {
399
3
    case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT:
400
3
      Stream_Write_UINT8(s, 0x0E);                       /* headerLength (1 byte) */
401
3
      Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
402
3
      Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
403
3
      WINPR_ASSERT((result->type <= UINT16_MAX));
404
3
      WINPR_ASSERT((result->type >= 0));
405
3
      Stream_Write_UINT16(s, (UINT16)result->type);      /* requestType (2 bytes) */
406
3
      Stream_Write_UINT32(s, result->baseRTT);           /* baseRTT (4 bytes) */
407
3
      Stream_Write_UINT32(s, result->averageRTT);        /* averageRTT (4 bytes) */
408
3
      break;
409
5
    case RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT:
410
5
      Stream_Write_UINT8(s, 0x0E);                       /* headerLength (1 byte) */
411
5
      Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
412
5
      Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
413
5
      WINPR_ASSERT((result->type <= UINT16_MAX));
414
5
      WINPR_ASSERT((result->type >= 0));
415
5
      Stream_Write_UINT16(s, (UINT16)result->type);      /* requestType (2 bytes) */
416
5
      Stream_Write_UINT32(s, result->bandwidth);         /* bandwidth (4 bytes) */
417
5
      Stream_Write_UINT32(s, result->averageRTT);        /* averageRTT (4 bytes) */
418
5
      break;
419
9
    case RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT:
420
9
      Stream_Write_UINT8(s, 0x12);                       /* headerLength (1 byte) */
421
9
      Stream_Write_UINT8(s, TYPE_ID_AUTODETECT_REQUEST); /* headerTypeId (1 byte) */
422
9
      Stream_Write_UINT16(s, sequenceNumber);            /* sequenceNumber (2 bytes) */
423
9
      WINPR_ASSERT((result->type <= UINT16_MAX));
424
9
      WINPR_ASSERT((result->type >= 0));
425
9
      Stream_Write_UINT16(s, (UINT16)result->type);      /* requestType (2 bytes) */
426
9
      Stream_Write_UINT32(s, result->baseRTT);           /* baseRTT (4 bytes) */
427
9
      Stream_Write_UINT32(s, result->bandwidth);         /* bandwidth (4 bytes) */
428
9
      Stream_Write_UINT32(s, result->averageRTT);        /* averageRTT (4 bytes) */
429
9
      break;
430
0
    default:
431
0
      WINPR_ASSERT(FALSE);
432
0
      break;
433
17
  }
434
435
17
  return rdp_send_message_channel_pdu(autodetect->context->rdp, s,
436
17
                                      sec_flags | SEC_AUTODETECT_REQ);
437
17
}
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
24
{
468
24
  WINPR_ASSERT(autodetect);
469
24
  WINPR_ASSERT(s);
470
24
  WINPR_ASSERT(autodetectReqPdu);
471
472
24
  if (autodetectReqPdu->headerLength != 0x06)
473
21
  {
474
21
    WLog_Print(autodetect->log, WLOG_ERROR,
475
21
               "autodetectReqPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
476
21
               autodetectReqPdu->headerLength);
477
21
    return FALSE;
478
21
  }
479
480
3
  WLog_Print(autodetect->log, WLOG_TRACE, "received RTT Measure Request PDU");
481
  /* Send a response to the server */
482
3
  return autodetect_send_rtt_measure_response(autodetect, autodetectReqPdu->sequenceNumber);
483
24
}
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
46
{
490
46
  BOOL success = TRUE;
491
492
46
  WINPR_ASSERT(autodetect);
493
46
  WINPR_ASSERT(autodetectRspPdu);
494
495
46
  if (autodetectRspPdu->headerLength != 0x06)
496
14
  {
497
14
    WLog_Print(autodetect->log, WLOG_ERROR,
498
14
               "autodetectRspPdu->headerLength != 0x06 [0x%02" PRIx8 "]",
499
14
               autodetectRspPdu->headerLength);
500
14
    return FALSE;
501
14
  }
502
503
32
  WLog_Print(autodetect->log, WLOG_TRACE, "received RTT Measure Response PDU");
504
32
  autodetect->netCharAverageRTT =
505
32
      (UINT32)MIN(GetTickCount64() - autodetect->rttMeasureStartTime, UINT32_MAX);
506
507
32
  if (autodetect->netCharBaseRTT == 0 ||
508
0
      autodetect->netCharBaseRTT > autodetect->netCharAverageRTT)
509
32
    autodetect->netCharBaseRTT = autodetect->netCharAverageRTT;
510
511
32
  IFCALLRET(autodetect->RTTMeasureResponse, success, autodetect, transport,
512
32
            autodetectRspPdu->sequenceNumber);
513
32
  if (!success)
514
0
    WLog_Print(autodetect->log, WLOG_WARN, "RTTMeasureResponse failed");
515
32
  return success;
516
46
}
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
79
{
555
79
  UINT16 payloadLength = 0;
556
557
79
  WINPR_ASSERT(autodetect);
558
79
  WINPR_ASSERT(s);
559
79
  WINPR_ASSERT(autodetectReqPdu);
560
561
79
  if (autodetectReqPdu->headerLength != 0x08)
562
11
  {
563
11
    WLog_Print(autodetect->log, WLOG_ERROR,
564
11
               "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8 "]",
565
11
               autodetectReqPdu->headerLength);
566
11
    return FALSE;
567
11
  }
568
569
68
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
570
2
    return FALSE;
571
572
66
  Stream_Read_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
573
66
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
574
3
    return FALSE;
575
63
  Stream_Seek(s, payloadLength);
576
577
63
  WLog_Print(autodetect->log, WLOG_DEBUG,
578
63
             "received Bandwidth Measure Payload PDU -> payloadLength=%" PRIu16 "",
579
63
             payloadLength);
580
  /* Add the payload length to the bandwidth measurement parameters */
581
63
  autodetect->bandwidthMeasureByteCount += payloadLength;
582
63
  return TRUE;
583
66
}
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
98
{
589
98
  UINT16 payloadLength = 0;
590
98
  UINT16 responseType = 0;
591
592
98
  WINPR_ASSERT(autodetect);
593
98
  WINPR_ASSERT(s);
594
98
  WINPR_ASSERT(autodetectReqPdu);
595
596
98
  if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME)
597
78
  {
598
78
    if (autodetectReqPdu->headerLength != 0x08)
599
7
    {
600
7
      WLog_Print(autodetect->log, WLOG_ERROR,
601
7
                 "autodetectReqPdu->headerLength != 0x08 [0x%02" PRIx8 "]",
602
7
                 autodetectReqPdu->headerLength);
603
7
      return FALSE;
604
7
    }
605
606
71
    if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 2))
607
2
      return FALSE;
608
609
69
    Stream_Read_UINT16(s, payloadLength); /* payloadLength (2 bytes) */
610
69
  }
611
20
  else
612
20
  {
613
20
    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
16
    payloadLength = 0;
622
16
  }
623
624
85
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, payloadLength))
625
2
    return FALSE;
626
83
  Stream_Seek(s, payloadLength);
627
628
83
  WLog_Print(autodetect->log, WLOG_TRACE,
629
83
             "received Bandwidth Measure Stop PDU -> payloadLength=%" PRIu16 "", payloadLength);
630
  /* Add the payload length to the bandwidth measurement parameters */
631
83
  autodetect->bandwidthMeasureByteCount += payloadLength;
632
633
  /* Continuous Auto-Detection: mark the stop of the measurement */
634
83
  if (autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS)
635
13
  {
636
13
    autodetect->bandwidthMeasureStarted = FALSE;
637
13
  }
638
639
  /* Send a response the server */
640
83
  responseType = autodetectReqPdu->requestType == RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME
641
83
                     ? RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME
642
83
                     : RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS;
643
83
  return autodetect_send_bandwidth_measure_results(autodetect, transport, responseType,
644
83
                                                   autodetectReqPdu->sequenceNumber);
645
85
}
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
18
{
651
18
  UINT32 timeDelta = 0;
652
18
  UINT32 byteCount = 0;
653
18
  BOOL success = TRUE;
654
655
18
  WINPR_ASSERT(autodetect);
656
18
  WINPR_ASSERT(s);
657
18
  WINPR_ASSERT(autodetectRspPdu);
658
659
18
  if (autodetectRspPdu->headerLength != 0x0E)
660
5
  {
661
5
    WLog_Print(autodetect->log, WLOG_ERROR,
662
5
               "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
663
5
               autodetectRspPdu->headerLength);
664
5
    return FALSE;
665
5
  }
666
667
13
  WLog_Print(autodetect->log, WLOG_TRACE, "received Bandwidth Measure Results PDU");
668
13
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
669
2
    return FALSE;
670
11
  Stream_Read_UINT32(s, timeDelta); /* timeDelta (4 bytes) */
671
11
  Stream_Read_UINT32(s, byteCount); /* byteCount (4 bytes) */
672
673
11
  IFCALLRET(autodetect->BandwidthMeasureResults, success, autodetect, transport,
674
11
            autodetectRspPdu->sequenceNumber, autodetectRspPdu->responseType, timeDelta,
675
11
            byteCount);
676
11
  if (!success)
677
0
    WLog_Print(autodetect->log, WLOG_WARN, "BandwidthMeasureResults failed");
678
11
  return success;
679
13
}
680
681
static BOOL autodetect_recv_netchar_sync(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
682
                                         wStream* s, const AUTODETECT_RSP_PDU* autodetectRspPdu)
683
16
{
684
16
  UINT32 bandwidth = 0;
685
16
  UINT32 rtt = 0;
686
16
  BOOL success = TRUE;
687
688
16
  WINPR_ASSERT(autodetect);
689
16
  WINPR_ASSERT(s);
690
16
  WINPR_ASSERT(autodetectRspPdu);
691
692
16
  if (autodetectRspPdu->headerLength != 0x0E)
693
4
  {
694
4
    WLog_Print(autodetect->log, WLOG_ERROR,
695
4
               "autodetectRspPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
696
4
               autodetectRspPdu->headerLength);
697
4
    return FALSE;
698
4
  }
699
12
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
700
2
    return FALSE;
701
702
  /* bandwidth and averageRTT fields are present (baseRTT field is not) */
703
10
  Stream_Read_UINT32(s, bandwidth); /* bandwidth (4 bytes) */
704
10
  Stream_Read_UINT32(s, rtt);       /* rtt (4 bytes) */
705
706
10
  WLog_Print(autodetect->log, WLOG_TRACE,
707
10
             "received Network Characteristics Sync PDU -> bandwidth=%" PRIu32 ", rtt=%" PRIu32
708
10
             "",
709
10
             bandwidth, rtt);
710
711
10
  IFCALLRET(autodetect->NetworkCharacteristicsSync, success, autodetect, transport,
712
10
            autodetectRspPdu->sequenceNumber, bandwidth, rtt);
713
10
  if (!success)
714
0
    WLog_Print(autodetect->log, WLOG_WARN, "NetworkCharacteristicsSync failed");
715
10
  return success;
716
12
}
717
718
static BOOL autodetect_recv_netchar_request(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
719
                                            wStream* s, const AUTODETECT_REQ_PDU* autodetectReqPdu)
720
48
{
721
48
  rdpNetworkCharacteristicsResult result = {
722
48
    .type = RDP_NETCHAR_RESERVED, .baseRTT = 0, .averageRTT = 0, .bandwidth = 0
723
48
  };
724
48
  BOOL success = TRUE;
725
726
48
  WINPR_ASSERT(autodetect);
727
48
  WINPR_ASSERT(s);
728
48
  WINPR_ASSERT(autodetectReqPdu);
729
730
48
  switch (autodetectReqPdu->requestType)
731
48
  {
732
11
    case RDP_NETCHAR_RESULTS_0x0840:
733
734
      /* baseRTT and averageRTT fields are present (bandwidth field is not) */
735
11
      if (autodetectReqPdu->headerLength != 0x0E)
736
3
      {
737
3
        WLog_Print(autodetect->log, WLOG_ERROR,
738
3
                   "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
739
3
                   autodetectReqPdu->headerLength);
740
3
        return FALSE;
741
3
      }
742
8
      if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
743
2
        return FALSE;
744
745
6
      result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_AVG_RTT;
746
6
      Stream_Read_UINT32(s, result.baseRTT);    /* baseRTT (4 bytes) */
747
6
      Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
748
6
      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
2
      {
755
2
        WLog_Print(autodetect->log, WLOG_ERROR,
756
2
                   "autodetectReqPdu->headerLength != 0x0E [0x%02" PRIx8 "]",
757
2
                   autodetectReqPdu->headerLength);
758
2
        return FALSE;
759
2
      }
760
18
      if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 8))
761
2
        return FALSE;
762
763
16
      result.type = RDP_NETCHAR_RESULT_TYPE_BW_AVG_RTT;
764
16
      Stream_Read_UINT32(s, result.bandwidth);  /* bandwidth (4 bytes) */
765
16
      Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
766
16
      break;
767
768
17
    case RDP_NETCHAR_RESULTS_0x08C0:
769
770
      /* baseRTT, bandwidth, and averageRTT fields are present */
771
17
      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
13
      if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 12))
779
2
        return FALSE;
780
781
11
      result.type = RDP_NETCHAR_RESULT_TYPE_BASE_RTT_BW_AVG_RTT;
782
11
      Stream_Read_UINT32(s, result.baseRTT);    /* baseRTT (4 bytes) */
783
11
      Stream_Read_UINT32(s, result.bandwidth);  /* bandwidth (4 bytes) */
784
11
      Stream_Read_UINT32(s, result.averageRTT); /* averageRTT (4 bytes) */
785
11
      break;
786
787
0
    default:
788
0
      WINPR_ASSERT(FALSE);
789
0
      break;
790
48
  }
791
792
33
  WLog_Print(autodetect->log, WLOG_TRACE,
793
33
             "received Network Characteristics Result PDU -> baseRTT=%" PRIu32
794
33
             ", bandwidth=%" PRIu32 ", averageRTT=%" PRIu32 "",
795
33
             result.baseRTT, result.bandwidth, result.averageRTT);
796
797
33
  IFCALLRET(autodetect->NetworkCharacteristicsResult, success, autodetect, transport,
798
33
            autodetectReqPdu->sequenceNumber, &result);
799
33
  if (!success)
800
17
    WLog_Print(autodetect->log, WLOG_WARN, "NetworkCharacteristicsResult failed");
801
33
  return success;
802
48
}
803
804
state_run_t autodetect_recv_request_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
805
                                           wStream* s)
806
15.4k
{
807
15.4k
  AUTODETECT_REQ_PDU autodetectReqPdu = WINPR_C_ARRAY_INIT;
808
15.4k
  const rdpSettings* settings = nullptr;
809
15.4k
  BOOL success = FALSE;
810
811
15.4k
  WINPR_ASSERT(autodetect);
812
15.4k
  WINPR_ASSERT(autodetect->context);
813
814
15.4k
  settings = autodetect->context->settings;
815
15.4k
  WINPR_ASSERT(settings);
816
817
15.4k
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
818
11.6k
    return STATE_RUN_FAILED;
819
820
3.75k
  Stream_Read_UINT8(s, autodetectReqPdu.headerLength);    /* headerLength (1 byte) */
821
3.75k
  Stream_Read_UINT8(s, autodetectReqPdu.headerTypeId);    /* headerTypeId (1 byte) */
822
3.75k
  Stream_Read_UINT16(s, autodetectReqPdu.sequenceNumber); /* sequenceNumber (2 bytes) */
823
3.75k
  Stream_Read_UINT16(s, autodetectReqPdu.requestType);    /* requestType (2 bytes) */
824
825
3.75k
  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
3.75k
  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
3.75k
  if (autodetectReqPdu.headerTypeId != TYPE_ID_AUTODETECT_REQUEST)
856
2.08k
  {
857
2.08k
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
858
2.08k
    const char* requestTypeStr = autodetect_request_type_to_string_buffer(
859
2.08k
        autodetectReqPdu.requestType, rbuffer, sizeof(rbuffer));
860
2.08k
    char hbuffer[128] = WINPR_C_ARRAY_INIT;
861
2.08k
    const char* headerStr =
862
2.08k
        autodetect_header_type_string(autodetectReqPdu.headerTypeId, hbuffer, sizeof(hbuffer));
863
864
2.08k
    WLog_Print(autodetect->log, WLOG_ERROR,
865
2.08k
               "Received a [MS-RDPBCGR] 2.2.14.1.1 RTT Measure Request [%s] "
866
2.08k
               "message with invalid headerTypeId=%s",
867
2.08k
               requestTypeStr, headerStr);
868
2.08k
    goto fail;
869
2.08k
  }
870
871
1.67k
  if (!IFCALLRESULT(TRUE, autodetect->RequestReceived, autodetect, transport,
872
1.67k
                    autodetectReqPdu.requestType, autodetectReqPdu.sequenceNumber))
873
0
    goto fail;
874
875
1.67k
  switch (autodetectReqPdu.requestType)
876
1.67k
  {
877
20
    case RDP_RTT_REQUEST_TYPE_CONTINUOUS:
878
24
    case RDP_RTT_REQUEST_TYPE_CONNECTTIME:
879
      /* RTT Measure Request (RDP_RTT_REQUEST) - MS-RDPBCGR 2.2.14.1.1 */
880
24
      success =
881
24
          autodetect_recv_rtt_measure_request(autodetect, transport, s, &autodetectReqPdu);
882
24
      break;
883
884
8
    case RDP_BW_START_REQUEST_TYPE_CONTINUOUS:
885
11
    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
79
    case RDP_BW_PAYLOAD_REQUEST_TYPE:
893
      /* Bandwidth Measure Payload (RDP_BW_PAYLOAD) - MS-RDPBCGR 2.2.14.1.3 */
894
79
      success = autodetect_recv_bandwidth_measure_payload(autodetect, transport, s,
895
79
                                                          &autodetectReqPdu);
896
79
      break;
897
898
78
    case RDP_BW_STOP_REQUEST_TYPE_CONNECTTIME:
899
94
    case RDP_BW_STOP_REQUEST_TYPE_CONTINUOUS:
900
98
    case RDP_BW_STOP_REQUEST_TYPE_TUNNEL:
901
      /* Bandwidth Measure Stop (RDP_BW_STOP) - MS-RDPBCGR 2.2.14.1.4 */
902
98
      success =
903
98
          autodetect_recv_bandwidth_measure_stop(autodetect, transport, s, &autodetectReqPdu);
904
98
      break;
905
906
11
    case RDP_NETCHAR_RESULTS_0x0840:
907
31
    case RDP_NETCHAR_RESULTS_0x0880:
908
48
    case RDP_NETCHAR_RESULTS_0x08C0:
909
      /* Network Characteristics Result (RDP_NETCHAR_RESULT) - MS-RDPBCGR 2.2.14.1.5 */
910
48
      success = autodetect_recv_netchar_request(autodetect, transport, s, &autodetectReqPdu);
911
48
      break;
912
913
1.41k
    default:
914
1.41k
      WLog_Print(autodetect->log, WLOG_ERROR, "Unknown requestType=0x%04" PRIx16,
915
1.41k
                 autodetectReqPdu.requestType);
916
1.41k
      break;
917
1.67k
  }
918
919
3.75k
fail:
920
3.75k
  if (success)
921
83
    autodetect->state = FREERDP_AUTODETECT_STATE_REQUEST;
922
3.67k
  else
923
3.67k
    autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
924
3.75k
  return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
925
1.67k
}
926
927
state_run_t autodetect_recv_response_packet(rdpAutoDetect* autodetect, RDP_TRANSPORT_TYPE transport,
928
                                            wStream* s)
929
15.4k
{
930
15.4k
  AUTODETECT_RSP_PDU autodetectRspPdu = WINPR_C_ARRAY_INIT;
931
15.4k
  const rdpSettings* settings = nullptr;
932
15.4k
  BOOL success = FALSE;
933
934
15.4k
  WINPR_ASSERT(autodetect);
935
15.4k
  WINPR_ASSERT(autodetect->context);
936
15.4k
  WINPR_ASSERT(s);
937
938
15.4k
  settings = autodetect->context->settings;
939
15.4k
  WINPR_ASSERT(settings);
940
941
15.4k
  if (!Stream_CheckAndLogRequiredLengthWLog(autodetect->log, s, 6))
942
12.0k
    goto fail;
943
944
3.36k
  Stream_Read_UINT8(s, autodetectRspPdu.headerLength);    /* headerLength (1 byte) */
945
3.36k
  Stream_Read_UINT8(s, autodetectRspPdu.headerTypeId);    /* headerTypeId (1 byte) */
946
3.36k
  Stream_Read_UINT16(s, autodetectRspPdu.sequenceNumber); /* sequenceNumber (2 bytes) */
947
3.36k
  Stream_Read_UINT16(s, autodetectRspPdu.responseType);   /* responseType (2 bytes) */
948
949
3.36k
  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.36k
  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.36k
  if (autodetectRspPdu.headerTypeId != TYPE_ID_AUTODETECT_RESPONSE)
980
3.22k
  {
981
3.22k
    char rbuffer[128] = WINPR_C_ARRAY_INIT;
982
3.22k
    const char* requestStr = autodetect_request_type_to_string_buffer(
983
3.22k
        autodetectRspPdu.responseType, rbuffer, sizeof(rbuffer));
984
3.22k
    char hbuffer[128] = WINPR_C_ARRAY_INIT;
985
3.22k
    const char* headerStr =
986
3.22k
        autodetect_header_type_string(autodetectRspPdu.headerTypeId, hbuffer, sizeof(hbuffer));
987
3.22k
    WLog_Print(autodetect->log, WLOG_ERROR,
988
3.22k
               "Received a [MS-RDPBCGR] 2.2.14.2.1 RTT Measure Response [%s] "
989
3.22k
               "message with invalid headerTypeId=%s",
990
3.22k
               requestStr, headerStr);
991
3.22k
    goto fail;
992
3.22k
  }
993
994
140
  if (!IFCALLRESULT(TRUE, autodetect->ResponseReceived, autodetect, transport,
995
140
                    autodetectRspPdu.responseType, autodetectRspPdu.sequenceNumber))
996
0
    goto fail;
997
998
140
  switch (autodetectRspPdu.responseType)
999
140
  {
1000
46
    case RDP_RTT_RESPONSE_TYPE:
1001
      /* RTT Measure Response (RDP_RTT_RESPONSE) - MS-RDPBCGR 2.2.14.2.1 */
1002
46
      success =
1003
46
          autodetect_recv_rtt_measure_response(autodetect, transport, s, &autodetectRspPdu);
1004
46
      break;
1005
1006
6
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME:
1007
18
    case RDP_BW_RESULTS_RESPONSE_TYPE_CONTINUOUS:
1008
      /* Bandwidth Measure Results (RDP_BW_RESULTS) - MS-RDPBCGR 2.2.14.2.2 */
1009
18
      success = autodetect_recv_bandwidth_measure_results(autodetect, transport, s,
1010
18
                                                          &autodetectRspPdu);
1011
18
      break;
1012
1013
16
    case RDP_NETCHAR_SYNC_RESPONSE_TYPE:
1014
      /* Network Characteristics Sync (RDP_NETCHAR_SYNC) - MS-RDPBCGR 2.2.14.2.3 */
1015
16
      success = autodetect_recv_netchar_sync(autodetect, transport, s, &autodetectRspPdu);
1016
16
      break;
1017
1018
60
    default:
1019
60
      WLog_Print(autodetect->log, WLOG_ERROR, "Unknown responseType=0x%04" PRIx16,
1020
60
                 autodetectRspPdu.responseType);
1021
60
      break;
1022
140
  }
1023
1024
15.4k
fail:
1025
15.4k
  if (success)
1026
53
  {
1027
53
    if (autodetectRspPdu.responseType == RDP_BW_RESULTS_RESPONSE_TYPE_CONNECTTIME)
1028
3
      autodetect->state = FREERDP_AUTODETECT_STATE_COMPLETE;
1029
50
    else
1030
50
      autodetect->state = FREERDP_AUTODETECT_STATE_RESPONSE;
1031
53
  }
1032
15.3k
  else
1033
15.3k
    autodetect->state = FREERDP_AUTODETECT_STATE_FAIL;
1034
1035
15.4k
  return success ? STATE_RUN_SUCCESS : STATE_RUN_FAILED;
1036
140
}
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
15.4k
{
1056
15.4k
  rdpAutoDetect* autoDetect = (rdpAutoDetect*)calloc(1, sizeof(rdpAutoDetect));
1057
15.4k
  if (!autoDetect)
1058
0
    return nullptr;
1059
15.4k
  autoDetect->context = context;
1060
15.4k
  autoDetect->log = WLog_Get(AUTODETECT_TAG);
1061
1062
15.4k
  return autoDetect;
1063
15.4k
}
1064
1065
void autodetect_free(rdpAutoDetect* autoDetect)
1066
15.4k
{
1067
15.4k
  free(autoDetect);
1068
15.4k
}
1069
1070
void autodetect_register_server_callbacks(rdpAutoDetect* autodetect)
1071
7.31k
{
1072
7.31k
  WINPR_ASSERT(autodetect);
1073
1074
7.31k
  autodetect->RTTMeasureRequest = autodetect_send_rtt_measure_request;
1075
7.31k
  autodetect->BandwidthMeasureStart = autodetect_send_bandwidth_measure_start;
1076
7.31k
  autodetect->BandwidthMeasurePayload = autodetect_send_bandwidth_measure_payload;
1077
7.31k
  autodetect->BandwidthMeasureStop = autodetect_send_bandwidth_measure_stop;
1078
7.31k
  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
7.31k
  autodetect->OnConnectTimeAutoDetectBegin = autodetect_on_connect_time_auto_detect_begin_default;
1085
7.31k
  autodetect->OnConnectTimeAutoDetectProgress =
1086
7.31k
      autodetect_on_connect_time_auto_detect_progress_default;
1087
7.31k
}
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
}