Coverage Report

Created: 2026-04-12 07:03

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