Coverage Report

Created: 2026-03-07 07:07

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