Coverage Report

Created: 2025-07-01 06:46

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