Coverage Report

Created: 2026-10-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/server.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Server Channels
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <stdint.h>
28
29
#include <winpr/atexit.h>
30
#include <winpr/wtypes.h>
31
#include <winpr/crt.h>
32
#include <winpr/synch.h>
33
#include <winpr/stream.h>
34
#include <winpr/assert.h>
35
#include <winpr/cast.h>
36
37
#include <freerdp/log.h>
38
#include <freerdp/constants.h>
39
#include <freerdp/server/channels.h>
40
#include <freerdp/channels/drdynvc.h>
41
#include <freerdp/utils/drdynvc.h>
42
43
#include "rdp.h"
44
45
#include "server.h"
46
47
#define TAG FREERDP_TAG("core.server")
48
#ifdef WITH_DEBUG_DVC
49
#define DEBUG_DVC(...) WLog_DBG(TAG, __VA_ARGS__)
50
#else
51
#define DEBUG_DVC(...) \
52
0
  do                 \
53
0
  {                  \
54
0
  } while (0)
55
#endif
56
57
0
#define DVC_MAX_DATA_PDU_SIZE 1600
58
59
typedef struct
60
{
61
  UINT16 channelId;
62
  UINT16 reserved;
63
  UINT32 length;
64
  UINT32 offset;
65
} wtsChannelMessage;
66
67
static const DWORD g_err_oom = WINPR_CXX_COMPAT_CAST(DWORD, E_OUTOFMEMORY);
68
69
static DWORD g_SessionId = 1;
70
static wHashTable* g_ServerHandles = nullptr;
71
static INIT_ONCE g_HandleInitializer = INIT_ONCE_STATIC_INIT;
72
73
static rdpPeerChannel* wts_get_dvc_channel_by_id(WTSVirtualChannelManager* vcm, UINT32 ChannelId)
74
0
{
75
0
  WINPR_ASSERT(vcm);
76
0
  return HashTable_GetItemValue(vcm->dynamicVirtualChannels, &ChannelId);
77
0
}
78
79
static BOOL wts_queue_receive_data(rdpPeerChannel* channel, const BYTE* Buffer1, size_t Length1,
80
                                   const BYTE* Buffer2, size_t Length2)
81
0
{
82
0
  WINPR_ASSERT(channel);
83
84
0
  if (Length1 > UINT32_MAX - sizeof(wtsChannelMessage))
85
0
    return FALSE;
86
0
  if (Length2 > UINT32_MAX - sizeof(wtsChannelMessage) - Length1)
87
0
    return FALSE;
88
0
  const size_t len = Length1 + Length2;
89
0
  const size_t tlen = sizeof(wtsChannelMessage) + len;
90
0
  wtsChannelMessage* messageCtx = (wtsChannelMessage*)malloc(tlen);
91
0
  if (!messageCtx)
92
0
    return FALSE;
93
94
0
  WINPR_ASSERT(channel->channelId <= UINT16_MAX);
95
0
  messageCtx->channelId = WINPR_ASSERTING_INT_CAST(UINT16, channel->channelId);
96
0
  messageCtx->length = WINPR_ASSERTING_INT_CAST(UINT32, len);
97
0
  messageCtx->offset = 0;
98
0
  BYTE* buffer = (BYTE*)(&messageCtx[1]);
99
0
  CopyMemory(buffer, Buffer1, Length1);
100
101
0
  WINPR_ASSERT(Buffer2 || (Length2 == 0));
102
0
  buffer += Length1;
103
0
  if (Buffer2)
104
0
    CopyMemory(buffer, Buffer2, Length2);
105
106
0
  return MessageQueue_Post(channel->queue, messageCtx, 0, nullptr, nullptr);
107
0
}
108
109
static BOOL wts_queue_send_item(rdpPeerChannel* channel, BYTE* Buffer, UINT32 Length)
110
0
{
111
0
  BYTE* buffer = nullptr;
112
0
  UINT32 length = 0;
113
114
0
  WINPR_ASSERT(channel);
115
0
  WINPR_ASSERT(channel->vcm);
116
0
  buffer = Buffer;
117
0
  length = Length;
118
119
0
  WINPR_ASSERT(channel->channelId <= UINT16_MAX);
120
0
  const UINT16 channelId = (UINT16)channel->channelId;
121
0
  return MessageQueue_Post(channel->vcm->queue, (void*)(UINT_PTR)channelId, 0, (void*)buffer,
122
0
                           (void*)(UINT_PTR)length);
123
0
}
124
125
static unsigned wts_read_variable_uint(wStream* s, int cbLen, UINT32* val)
126
0
{
127
0
  WINPR_ASSERT(s);
128
0
  WINPR_ASSERT(val);
129
0
  switch (cbLen)
130
0
  {
131
0
    case 0:
132
0
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
133
0
        return 0;
134
135
0
      Stream_Read_UINT8(s, *val);
136
0
      return 1;
137
138
0
    case 1:
139
0
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
140
0
        return 0;
141
142
0
      Stream_Read_UINT16(s, *val);
143
0
      return 2;
144
145
0
    case 2:
146
0
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
147
0
        return 0;
148
149
0
      Stream_Read_UINT32(s, *val);
150
0
      return 4;
151
152
0
    default:
153
0
      WLog_ERR(TAG, "invalid wts variable uint len %d", cbLen);
154
0
      return 0;
155
0
  }
156
0
}
157
158
static BOOL wts_read_drdynvc_capabilities_response(rdpPeerChannel* channel, wStream* s)
159
0
{
160
0
  UINT16 Version = 0;
161
162
0
  WINPR_ASSERT(channel);
163
0
  WINPR_ASSERT(channel->vcm);
164
0
  const size_t length = Stream_GetRemainingLength(s);
165
0
  if (length < 3)
166
0
    return FALSE;
167
168
0
  Stream_Seek_UINT8(s); /* Pad (1 byte) */
169
0
  Stream_Read_UINT16(s, Version);
170
0
  DEBUG_DVC("Version: %" PRIu16 "", Version);
171
172
0
  if (Version < 1)
173
0
  {
174
0
    WLog_ERR(TAG, "invalid version %" PRIu16 " for DRDYNVC", Version);
175
0
    return FALSE;
176
0
  }
177
178
0
  WTSVirtualChannelManager* vcm = channel->vcm;
179
0
  vcm->drdynvc_state = DRDYNVC_STATE_READY;
180
181
  /* we only support version 1 for now (no compression yet) */
182
0
  vcm->dvc_spoken_version = MAX(Version, 1);
183
184
0
  return SetEvent(MessageQueue_Event(vcm->queue));
185
0
}
186
187
static BOOL wts_read_drdynvc_create_response(rdpPeerChannel* channel, wStream* s)
188
0
{
189
0
  UINT32 CreationStatus = 0;
190
0
  BOOL status = TRUE;
191
192
0
  WINPR_ASSERT(channel);
193
0
  WINPR_ASSERT(s);
194
0
  const size_t length = Stream_GetRemainingLength(s);
195
0
  if ((length < 4) || (length > UINT32_MAX))
196
0
    return FALSE;
197
198
0
  Stream_Read_UINT32(s, CreationStatus);
199
200
0
  if ((INT32)CreationStatus < 0)
201
0
  {
202
0
    DEBUG_DVC("ChannelId %" PRIu32 " creation failed (%" PRId32 ")", channel->channelId,
203
0
              (INT32)CreationStatus);
204
0
    channel->dvc_open_state = DVC_OPEN_STATE_FAILED;
205
0
  }
206
0
  else
207
0
  {
208
0
    DEBUG_DVC("ChannelId %" PRIu32 " creation succeeded", channel->channelId);
209
0
    channel->dvc_open_state = DVC_OPEN_STATE_SUCCEEDED;
210
0
  }
211
212
0
  channel->creationStatus = (INT32)CreationStatus;
213
0
  IFCALLRET(channel->vcm->dvc_creation_status, status, channel->vcm->dvc_creation_status_userdata,
214
0
            channel->channelId, (INT32)CreationStatus);
215
0
  if (!status)
216
0
    WLog_ERR(TAG, "vcm->dvc_creation_status failed!");
217
218
0
  return status;
219
0
}
220
221
static BOOL wts_read_drdynvc_data_first(rdpPeerChannel* channel, wStream* s, int cbLen)
222
0
{
223
0
  WINPR_ASSERT(channel);
224
0
  WINPR_ASSERT(s);
225
226
0
  const UINT32 value = wts_read_variable_uint(s, cbLen, &channel->dvc_total_length);
227
228
0
  if ((value == 0) || (channel->dvc_total_length == 0))
229
0
    return FALSE;
230
231
0
  const size_t length = Stream_GetRemainingLength(s);
232
0
  if (length > channel->dvc_total_length)
233
0
    return FALSE;
234
235
0
  Stream_ResetPosition(channel->receiveData);
236
237
0
  if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
238
0
    return FALSE;
239
240
0
  Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
241
0
  return TRUE;
242
0
}
243
244
static BOOL wts_read_drdynvc_data(rdpPeerChannel* channel, wStream* s)
245
0
{
246
0
  BOOL ret = FALSE;
247
248
0
  WINPR_ASSERT(channel);
249
0
  WINPR_ASSERT(s);
250
251
0
  const size_t length = Stream_GetRemainingLength(s);
252
0
  if (channel->dvc_total_length > 0)
253
0
  {
254
0
    const size_t pos = Stream_GetPosition(channel->receiveData);
255
0
    if (pos > SIZE_MAX - length)
256
0
      return FALSE;
257
258
0
    if (pos + length > channel->dvc_total_length)
259
0
    {
260
0
      channel->dvc_total_length = 0;
261
0
      WLog_ERR(TAG, "incorrect fragment data, discarded.");
262
0
      return FALSE;
263
0
    }
264
265
0
    if (!Stream_EnsureRemainingCapacity(channel->receiveData, length))
266
0
      return FALSE;
267
268
0
    Stream_Write(channel->receiveData, Stream_ConstPointer(s), length);
269
270
0
    if (Stream_GetPosition(channel->receiveData) >= channel->dvc_total_length)
271
0
    {
272
0
      ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData),
273
0
                                   channel->dvc_total_length, nullptr, 0);
274
0
      channel->dvc_total_length = 0;
275
0
    }
276
0
    else
277
0
      ret = TRUE;
278
0
  }
279
0
  else
280
0
  {
281
0
    ret = wts_queue_receive_data(channel, Stream_ConstPointer(s), length, nullptr, 0);
282
0
  }
283
284
0
  return ret;
285
0
}
286
287
static void wts_read_drdynvc_close_response(rdpPeerChannel* channel)
288
0
{
289
0
  WINPR_ASSERT(channel);
290
0
  DEBUG_DVC("ChannelId %" PRIu32 " close response", channel->channelId);
291
0
  channel->dvc_open_state = DVC_OPEN_STATE_CLOSED;
292
0
  MessageQueue_PostQuit(channel->queue, 0);
293
0
}
294
295
WINPR_ATTR_NODISCARD
296
static BOOL wts_read_drdynvc_pdu_ready_guarded(wStream* s, UINT8 Cmd, UINT8 Sp, UINT32 ChannelId,
297
                                               rdpPeerChannel* dvc)
298
0
{
299
0
  switch (Cmd)
300
0
  {
301
0
    case CREATE_REQUEST_PDU:
302
0
      return wts_read_drdynvc_create_response(dvc, s);
303
304
0
    case DATA_FIRST_PDU:
305
0
      if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
306
0
      {
307
0
        WLog_ERR(TAG,
308
0
                 "ChannelId %" PRIu32 " did not open successfully. "
309
0
                 "Ignoring DYNVC_DATA_FIRST PDU",
310
0
                 ChannelId);
311
0
        return TRUE;
312
0
      }
313
314
0
      return wts_read_drdynvc_data_first(dvc, s, Sp);
315
316
0
    case DATA_PDU:
317
0
      if (dvc->dvc_open_state != DVC_OPEN_STATE_SUCCEEDED)
318
0
      {
319
0
        WLog_ERR(TAG,
320
0
                 "ChannelId %" PRIu32 " did not open successfully. "
321
0
                 "Ignoring DYNVC_DATA PDU",
322
0
                 ChannelId);
323
0
        return TRUE;
324
0
      }
325
326
0
      return wts_read_drdynvc_data(dvc, s);
327
328
0
    case CLOSE_REQUEST_PDU:
329
0
      wts_read_drdynvc_close_response(dvc);
330
0
      break;
331
332
0
    case DATA_FIRST_COMPRESSED_PDU:
333
0
    case DATA_COMPRESSED_PDU:
334
0
      WLog_ERR(TAG, "Compressed data not handled");
335
0
      break;
336
337
0
    case SOFT_SYNC_RESPONSE_PDU:
338
0
      WLog_ERR(TAG, "SoftSync response not handled yet(and rather strange to receive "
339
0
                    "that packet as our code doesn't send SoftSync requests");
340
0
      break;
341
342
0
    case SOFT_SYNC_REQUEST_PDU:
343
0
      WLog_ERR(TAG, "Not expecting a SoftSyncRequest on the server");
344
0
      return FALSE;
345
346
0
    default:
347
0
      WLog_ERR(TAG, "Cmd %d not recognized.", Cmd);
348
0
      break;
349
0
  }
350
0
  return TRUE;
351
0
}
352
353
WINPR_ATTR_NODISCARD
354
static BOOL wts_read_drdynvc_pdu_ready(rdpPeerChannel* channel, wStream* s, UINT8 Cmd, UINT8 Sp,
355
                                       UINT8 cbChId)
356
0
{
357
0
  WINPR_ASSERT(channel);
358
359
0
  BOOL haveChannelId = 0;
360
0
  switch (Cmd)
361
0
  {
362
0
    case SOFT_SYNC_REQUEST_PDU:
363
0
    case SOFT_SYNC_RESPONSE_PDU:
364
0
      haveChannelId = FALSE;
365
0
      break;
366
0
    default:
367
0
      haveChannelId = TRUE;
368
0
      break;
369
0
  }
370
371
0
  UINT32 ChannelId = 0;
372
0
  rdpPeerChannel* dvc = nullptr;
373
0
  wHashTable* table = nullptr;
374
0
  if (haveChannelId)
375
0
  {
376
0
    const unsigned val = wts_read_variable_uint(s, cbChId, &ChannelId);
377
0
    if (val == 0)
378
0
      return FALSE;
379
380
0
    DEBUG_DVC("Cmd %s ChannelId %" PRIu32 " length %" PRIuz "", drdynvc_get_packet_type(Cmd),
381
0
              ChannelId, Stream_GetRemainingLength(s));
382
0
    table = channel->vcm->dynamicVirtualChannels;
383
0
    if (!table)
384
0
      return FALSE;
385
0
    HashTable_Lock(table);
386
0
    dvc = wts_get_dvc_channel_by_id(channel->vcm, ChannelId);
387
0
    if (!dvc)
388
0
    {
389
0
      DEBUG_DVC("ChannelId %" PRIu32 " does not exist.", ChannelId);
390
0
      HashTable_Unlock(table);
391
0
      return TRUE;
392
0
    }
393
0
  }
394
395
0
  const BOOL rc = wts_read_drdynvc_pdu_ready_guarded(s, Cmd, Sp, ChannelId, dvc);
396
0
  if (table)
397
0
    HashTable_Unlock(table);
398
0
  return rc;
399
0
}
400
401
WINPR_ATTR_NODISCARD
402
static BOOL wts_read_drdynvc_pdu(rdpPeerChannel* channel)
403
0
{
404
0
  WINPR_ASSERT(channel);
405
0
  WINPR_ASSERT(channel->vcm);
406
407
0
  const size_t slength = Stream_GetPosition(channel->receiveData);
408
0
  if ((slength < 1) || (slength > UINT32_MAX))
409
0
    return FALSE;
410
411
0
  Stream_ResetPosition(channel->receiveData);
412
0
  wStream buffer = WINPR_C_ARRAY_INIT;
413
0
  wStream* s = Stream_StaticConstInit(&buffer, Stream_Buffer(channel->receiveData), slength);
414
0
  const UINT8 value = Stream_Get_UINT8(s);
415
0
  const UINT8 Cmd = (value & 0xf0) >> 4;
416
0
  const UINT8 Sp = (value & 0x0c) >> 2;
417
0
  const UINT8 cbChId = (value & 0x03) >> 0;
418
419
0
  if (Cmd == CAPABILITY_REQUEST_PDU)
420
0
    return wts_read_drdynvc_capabilities_response(channel, s);
421
422
0
  if (channel->vcm->drdynvc_state == DRDYNVC_STATE_READY)
423
0
    return wts_read_drdynvc_pdu_ready(channel, s, Cmd, Sp, cbChId);
424
0
  else
425
0
    WLog_ERR(TAG, "received Cmd %d but channel is not ready.", Cmd);
426
427
0
  return TRUE;
428
0
}
429
430
static int wts_write_variable_uint(wStream* s, UINT32 val)
431
0
{
432
0
  int cb = 0;
433
434
0
  WINPR_ASSERT(s);
435
0
  if (val <= 0xFF)
436
0
  {
437
0
    cb = 0;
438
0
    Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, val));
439
0
  }
440
0
  else if (val <= 0xFFFF)
441
0
  {
442
0
    cb = 1;
443
0
    Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, val));
444
0
  }
445
0
  else
446
0
  {
447
0
    cb = 2;
448
0
    Stream_Write_UINT32(s, val);
449
0
  }
450
451
0
  return cb;
452
0
}
453
454
static void wts_write_drdynvc_header(wStream* s, BYTE Cmd, UINT32 ChannelId)
455
0
{
456
0
  WINPR_ASSERT(s);
457
458
0
  BYTE* bm = Stream_PointerAs(s, BYTE);
459
0
  Stream_Seek_UINT8(s);
460
0
  const int cbChId = wts_write_variable_uint(s, ChannelId);
461
0
  *bm = (((Cmd & 0x0F) << 4) | cbChId) & 0xFF;
462
0
}
463
464
static BOOL wts_write_drdynvc_create_request(wStream* s, UINT32 ChannelId, const char* ChannelName)
465
0
{
466
0
  size_t len = 0;
467
468
0
  WINPR_ASSERT(s);
469
0
  WINPR_ASSERT(ChannelName);
470
471
0
  wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
472
0
  len = strlen(ChannelName) + 1;
473
474
0
  if (!Stream_EnsureRemainingCapacity(s, len))
475
0
    return FALSE;
476
477
0
  Stream_Write(s, ChannelName, len);
478
0
  return TRUE;
479
0
}
480
481
static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId, const BYTE* data,
482
                                  size_t s, UINT32 flags, size_t t)
483
0
{
484
0
  BOOL ret = TRUE;
485
0
  size_t size = s;
486
0
  const size_t totalSize = t;
487
488
0
  WINPR_ASSERT(channel);
489
0
  WINPR_ASSERT(channel->vcm);
490
0
  WINPR_UNUSED(channelId);
491
492
0
  if (channel->channelFlags & CHANNEL_OPTION_SHOW_PROTOCOL)
493
0
  {
494
0
    BOOL firstPass = TRUE;
495
496
0
    while (size)
497
0
    {
498
0
      const UINT32 payloadLen = (size > CHANNEL_CHUNK_LENGTH)
499
0
                                    ? CHANNEL_CHUNK_LENGTH
500
0
                                    : WINPR_ASSERTING_INT_CAST(UINT32, size);
501
0
      size -= payloadLen;
502
503
      /* here we skip other flags than CHANNEL_FLAG_FIRST and CHANNEL_FLAG_LAST
504
       * as it's the only ones treated by ChannelPduTracker.
505
       */
506
0
      UINT32 newFlags = 0;
507
0
      if (firstPass)
508
0
      {
509
0
        firstPass = FALSE;
510
0
        if (flags & CHANNEL_FLAG_FIRST)
511
0
          newFlags = CHANNEL_FLAG_FIRST;
512
0
      }
513
514
0
      if (!size && (flags & CHANNEL_FLAG_LAST))
515
0
        newFlags |= CHANNEL_FLAG_LAST;
516
517
0
      const CHANNEL_PDU_HEADER header = {
518
0
        .length = payloadLen,
519
0
        .flags = newFlags,
520
0
      };
521
522
0
      if (!wts_queue_receive_data(channel, (const BYTE*)&header, sizeof(header), data,
523
0
                                  payloadLen))
524
0
        return FALSE;
525
526
0
      data += payloadLen;
527
0
    }
528
0
    return TRUE;
529
0
  }
530
531
0
  if ((flags & CHANNEL_FLAG_FIRST) != 0)
532
0
  {
533
0
    Stream_ResetPosition(channel->receiveData);
534
0
  }
535
536
0
  if (!Stream_EnsureRemainingCapacity(channel->receiveData, size))
537
0
    return FALSE;
538
539
0
  Stream_Write(channel->receiveData, data, size);
540
541
0
  if ((flags & CHANNEL_FLAG_LAST) != 0)
542
0
  {
543
0
    if (Stream_GetPosition(channel->receiveData) != totalSize)
544
0
      return FALSE;
545
546
0
    if (channel == channel->vcm->drdynvc_channel)
547
0
    {
548
0
      ret = wts_read_drdynvc_pdu(channel);
549
0
    }
550
0
    else
551
0
    {
552
0
      const size_t pos = Stream_GetPosition(channel->receiveData);
553
0
      if (pos > UINT32_MAX)
554
0
        ret = FALSE;
555
0
      else
556
0
        ret = wts_queue_receive_data(channel, Stream_Buffer(channel->receiveData), pos,
557
0
                                     nullptr, 0);
558
0
    }
559
560
0
    Stream_ResetPosition(channel->receiveData);
561
0
  }
562
0
  else if (Stream_GetPosition(channel->receiveData) >= totalSize)
563
0
    return FALSE;
564
565
0
  return ret;
566
0
}
567
568
static BOOL WTSReceiveChannelData(freerdp_peer* client, UINT16 channelId, const BYTE* data,
569
                                  size_t size, UINT32 flags, size_t totalSize)
570
0
{
571
0
  rdpMcs* mcs = nullptr;
572
573
0
  WINPR_ASSERT(client);
574
0
  WINPR_ASSERT(client->context);
575
0
  WINPR_ASSERT(client->context->rdp);
576
577
0
  mcs = client->context->rdp->mcs;
578
0
  WINPR_ASSERT(mcs);
579
580
0
  for (UINT32 i = 0; i < mcs->channelCount; i++)
581
0
  {
582
0
    rdpMcsChannel* cur = &mcs->channels[i];
583
0
    if (cur->ChannelId == channelId)
584
0
    {
585
0
      rdpPeerChannel* channel = (rdpPeerChannel*)cur->handle;
586
587
0
      if (channel)
588
0
        return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize);
589
0
    }
590
0
  }
591
592
0
  WLog_WARN(TAG, "unknown channelId %" PRIu16 " ignored", channelId);
593
594
0
  return TRUE;
595
0
}
596
597
#if defined(WITH_FREERDP_DEPRECATED)
598
void WTSVirtualChannelManagerGetFileDescriptor(HANDLE hServer, void** fds, int* fds_count)
599
{
600
  void* fd = nullptr;
601
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
602
  WINPR_ASSERT(vcm);
603
  WINPR_ASSERT(fds);
604
  WINPR_ASSERT(fds_count);
605
606
  fd = GetEventWaitObject(MessageQueue_Event(vcm->queue));
607
608
  if (fd)
609
  {
610
    fds[*fds_count] = fd;
611
    (*fds_count)++;
612
  }
613
614
#if 0
615
616
  if (vcm->drdynvc_channel)
617
  {
618
    fd = GetEventWaitObject(vcm->drdynvc_channel->receiveEvent);
619
620
    if (fd)
621
    {
622
      fds[*fds_count] = fd;
623
      (*fds_count)++;
624
    }
625
  }
626
627
#endif
628
}
629
#endif
630
631
BOOL WTSVirtualChannelManagerOpen(HANDLE hServer)
632
0
{
633
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
634
635
0
  if (!vcm)
636
0
    return FALSE;
637
638
0
  if (vcm->drdynvc_state == DRDYNVC_STATE_NONE)
639
0
  {
640
0
    rdpPeerChannel* channel = nullptr;
641
642
    /* Initialize drdynvc channel once and only once. */
643
0
    vcm->drdynvc_state = DRDYNVC_STATE_INITIALIZED;
644
0
    channel = (rdpPeerChannel*)WTSVirtualChannelOpen((HANDLE)vcm, WTS_CURRENT_SESSION,
645
0
                                                     DRDYNVC_SVC_CHANNEL_NAME);
646
647
0
    if (channel)
648
0
    {
649
0
      BYTE capaBuffer[12] = WINPR_C_ARRAY_INIT;
650
0
      wStream staticS = WINPR_C_ARRAY_INIT;
651
0
      wStream* s = Stream_StaticInit(&staticS, capaBuffer, sizeof(capaBuffer));
652
653
0
      vcm->drdynvc_channel = channel;
654
0
      vcm->dvc_spoken_version = 1;
655
0
      Stream_Write_UINT8(s, 0x50);    /* Cmd=5 sp=0 cbId=0 */
656
0
      Stream_Write_UINT8(s, 0x00);    /* Pad */
657
0
      Stream_Write_UINT16(s, 0x0001); /* Version */
658
659
      /* TODO: shall implement version 2 and 3 */
660
661
0
      const size_t pos = Stream_GetPosition(s);
662
0
      WINPR_ASSERT(pos <= UINT32_MAX);
663
0
      ULONG written = 0;
664
0
      if (!WTSVirtualChannelWrite(channel, (PCHAR)capaBuffer, (UINT32)pos, &written))
665
0
        return FALSE;
666
0
    }
667
0
  }
668
669
0
  return TRUE;
670
0
}
671
672
BOOL WTSVirtualChannelManagerCheckFileDescriptorEx(HANDLE hServer, BOOL autoOpen)
673
0
{
674
0
  wMessage message = WINPR_C_ARRAY_INIT;
675
0
  BOOL status = TRUE;
676
0
  WTSVirtualChannelManager* vcm = nullptr;
677
678
0
  if (!hServer || hServer == INVALID_HANDLE_VALUE)
679
0
    return FALSE;
680
681
0
  vcm = (WTSVirtualChannelManager*)hServer;
682
683
0
  if (autoOpen)
684
0
  {
685
0
    if (!WTSVirtualChannelManagerOpen(hServer))
686
0
      return FALSE;
687
0
  }
688
689
0
  while (MessageQueue_Peek(vcm->queue, &message, TRUE))
690
0
  {
691
0
    BYTE* buffer = nullptr;
692
0
    UINT32 length = 0;
693
0
    UINT16 channelId = 0;
694
0
    channelId = (UINT16)(UINT_PTR)message.context;
695
0
    buffer = (BYTE*)message.wParam;
696
0
    length = (UINT32)(UINT_PTR)message.lParam;
697
698
0
    WINPR_ASSERT(vcm->client);
699
0
    WINPR_ASSERT(vcm->client->SendChannelData);
700
0
    if (!vcm->client->SendChannelData(vcm->client, channelId, buffer, length))
701
0
    {
702
0
      status = FALSE;
703
0
    }
704
705
0
    free(buffer);
706
707
0
    if (!status)
708
0
      break;
709
0
  }
710
711
0
  return status;
712
0
}
713
714
BOOL WTSVirtualChannelManagerCheckFileDescriptor(HANDLE hServer)
715
0
{
716
0
  return WTSVirtualChannelManagerCheckFileDescriptorEx(hServer, TRUE);
717
0
}
718
719
HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer)
720
0
{
721
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
722
0
  WINPR_ASSERT(vcm);
723
0
  return MessageQueue_Event(vcm->queue);
724
0
}
725
726
static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs, const char* channel_name)
727
0
{
728
0
  if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN + 1))
729
0
    return nullptr;
730
731
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
732
0
  {
733
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
734
0
    if (mchannel->joined)
735
0
    {
736
0
      if (_strnicmp(mchannel->Name, channel_name, CHANNEL_NAME_LEN + 1) == 0)
737
0
        return mchannel;
738
0
    }
739
0
  }
740
741
0
  return nullptr;
742
0
}
743
744
static rdpMcsChannel* wts_get_joined_channel_by_id(rdpMcs* mcs, const UINT16 channel_id)
745
0
{
746
0
  if (!mcs || !channel_id)
747
0
    return nullptr;
748
749
0
  WINPR_ASSERT(mcs->channels);
750
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
751
0
  {
752
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
753
0
    if (mchannel->joined)
754
0
    {
755
0
      if (mchannel->ChannelId == channel_id)
756
0
        return &mcs->channels[index];
757
0
    }
758
0
  }
759
760
0
  return nullptr;
761
0
}
762
763
BOOL WTSIsChannelJoinedByName(freerdp_peer* client, const char* channel_name)
764
0
{
765
0
  if (!client || !client->context || !client->context->rdp)
766
0
    return FALSE;
767
768
0
  return (wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name) != nullptr);
769
0
}
770
771
BOOL WTSIsChannelJoinedById(freerdp_peer* client, UINT16 channel_id)
772
0
{
773
0
  if (!client || !client->context || !client->context->rdp)
774
0
    return FALSE;
775
776
0
  return (wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id) != nullptr);
777
0
}
778
779
BOOL WTSVirtualChannelManagerIsChannelJoined(HANDLE hServer, const char* name)
780
0
{
781
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
782
783
0
  if (!vcm || !vcm->rdp)
784
0
    return FALSE;
785
786
0
  return (wts_get_joined_channel_by_name(vcm->rdp->mcs, name) != nullptr);
787
0
}
788
789
BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer)
790
0
{
791
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
792
0
  WINPR_ASSERT(vcm);
793
0
  return vcm->drdynvc_state;
794
0
}
795
796
void WTSVirtualChannelManagerSetDVCCreationCallback(HANDLE hServer, psDVCCreationStatusCallback cb,
797
                                                    void* userdata)
798
0
{
799
0
  WTSVirtualChannelManager* vcm = hServer;
800
801
0
  WINPR_ASSERT(vcm);
802
803
0
  vcm->dvc_creation_status = cb;
804
0
  vcm->dvc_creation_status_userdata = userdata;
805
0
}
806
807
UINT16 WTSChannelGetId(freerdp_peer* client, const char* channel_name)
808
0
{
809
0
  rdpMcsChannel* channel = nullptr;
810
811
0
  WINPR_ASSERT(channel_name);
812
0
  if (!client || !client->context || !client->context->rdp)
813
0
    return 0;
814
815
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
816
817
0
  if (!channel)
818
0
    return 0;
819
820
0
  return channel->ChannelId;
821
0
}
822
823
UINT32 WTSChannelGetIdByHandle(HANDLE hChannelHandle)
824
0
{
825
0
  rdpPeerChannel* channel = hChannelHandle;
826
827
0
  WINPR_ASSERT(channel);
828
829
0
  return channel->channelId;
830
0
}
831
832
BOOL WTSChannelSetHandleByName(freerdp_peer* client, const char* channel_name, void* handle)
833
0
{
834
0
  rdpMcsChannel* channel = nullptr;
835
836
0
  WINPR_ASSERT(channel_name);
837
0
  if (!client || !client->context || !client->context->rdp)
838
0
    return FALSE;
839
840
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
841
842
0
  if (!channel)
843
0
    return FALSE;
844
845
0
  channel->handle = handle;
846
0
  return TRUE;
847
0
}
848
849
BOOL WTSChannelSetHandleById(freerdp_peer* client, UINT16 channel_id, void* handle)
850
0
{
851
0
  rdpMcsChannel* channel = nullptr;
852
853
0
  if (!client || !client->context || !client->context->rdp)
854
0
    return FALSE;
855
856
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
857
858
0
  if (!channel)
859
0
    return FALSE;
860
861
0
  channel->handle = handle;
862
0
  return TRUE;
863
0
}
864
865
void* WTSChannelGetHandleByName(freerdp_peer* client, const char* channel_name)
866
0
{
867
0
  rdpMcsChannel* channel = nullptr;
868
869
0
  WINPR_ASSERT(channel_name);
870
0
  if (!client || !client->context || !client->context->rdp)
871
0
    return nullptr;
872
873
0
  channel = wts_get_joined_channel_by_name(client->context->rdp->mcs, channel_name);
874
875
0
  if (!channel)
876
0
    return nullptr;
877
878
0
  return channel->handle;
879
0
}
880
881
void* WTSChannelGetHandleById(freerdp_peer* client, UINT16 channel_id)
882
0
{
883
0
  rdpMcsChannel* channel = nullptr;
884
885
0
  if (!client || !client->context || !client->context->rdp)
886
0
    return nullptr;
887
888
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
889
890
0
  if (!channel)
891
0
    return nullptr;
892
893
0
  return channel->handle;
894
0
}
895
896
const char* WTSChannelGetName(freerdp_peer* client, UINT16 channel_id)
897
0
{
898
0
  rdpMcsChannel* channel = nullptr;
899
900
0
  if (!client || !client->context || !client->context->rdp)
901
0
    return nullptr;
902
903
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
904
905
0
  if (!channel)
906
0
    return nullptr;
907
908
0
  return (const char*)channel->Name;
909
0
}
910
911
char** WTSGetAcceptedChannelNames(freerdp_peer* client, size_t* count)
912
0
{
913
0
  rdpMcs* mcs = nullptr;
914
0
  char** names = nullptr;
915
916
0
  if (!client || !client->context || !count)
917
0
    return nullptr;
918
919
0
  WINPR_ASSERT(client->context->rdp);
920
0
  mcs = client->context->rdp->mcs;
921
0
  WINPR_ASSERT(mcs);
922
0
  *count = mcs->channelCount;
923
924
0
  names = (char**)calloc(mcs->channelCount, sizeof(char*));
925
0
  if (!names)
926
0
    return nullptr;
927
928
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
929
0
  {
930
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
931
0
    names[index] = mchannel->Name;
932
0
  }
933
934
0
  return names;
935
0
}
936
937
INT64 WTSChannelGetOptions(freerdp_peer* client, UINT16 channel_id)
938
0
{
939
0
  rdpMcsChannel* channel = nullptr;
940
941
0
  if (!client || !client->context || !client->context->rdp)
942
0
    return -1;
943
944
0
  channel = wts_get_joined_channel_by_id(client->context->rdp->mcs, channel_id);
945
946
0
  if (!channel)
947
0
    return -1;
948
949
0
  return (INT64)channel->options;
950
0
}
951
952
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
953
                                                  WINPR_ATTR_UNUSED ULONG TargetLogonId,
954
                                                  WINPR_ATTR_UNUSED BYTE HotkeyVk,
955
                                                  WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
956
0
{
957
0
  WLog_ERR("TODO", "TODO: implement");
958
0
  return FALSE;
959
0
}
960
961
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
962
                                                  WINPR_ATTR_UNUSED ULONG TargetLogonId,
963
                                                  WINPR_ATTR_UNUSED BYTE HotkeyVk,
964
                                                  WINPR_ATTR_UNUSED USHORT HotkeyModifiers)
965
0
{
966
0
  WLog_ERR("TODO", "TODO: implement");
967
0
  return FALSE;
968
0
}
969
970
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExW(WINPR_ATTR_UNUSED LPWSTR pTargetServerName,
971
                                                    WINPR_ATTR_UNUSED ULONG TargetLogonId,
972
                                                    WINPR_ATTR_UNUSED BYTE HotkeyVk,
973
                                                    WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
974
                                                    WINPR_ATTR_UNUSED DWORD flags)
975
0
{
976
0
  WLog_ERR("TODO", "TODO: implement");
977
0
  return FALSE;
978
0
}
979
980
BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExA(WINPR_ATTR_UNUSED LPSTR pTargetServerName,
981
                                                    WINPR_ATTR_UNUSED ULONG TargetLogonId,
982
                                                    WINPR_ATTR_UNUSED BYTE HotkeyVk,
983
                                                    WINPR_ATTR_UNUSED USHORT HotkeyModifiers,
984
                                                    WINPR_ATTR_UNUSED DWORD flags)
985
0
{
986
0
  WLog_ERR("TODO", "TODO: implement");
987
0
  return FALSE;
988
0
}
989
990
BOOL WINAPI FreeRDP_WTSStopRemoteControlSession(WINPR_ATTR_UNUSED ULONG LogonId)
991
0
{
992
0
  WLog_ERR("TODO", "TODO: implement");
993
0
  return FALSE;
994
0
}
995
996
BOOL WINAPI FreeRDP_WTSConnectSessionW(WINPR_ATTR_UNUSED ULONG LogonId,
997
                                       WINPR_ATTR_UNUSED ULONG TargetLogonId,
998
                                       WINPR_ATTR_UNUSED PWSTR pPassword,
999
                                       WINPR_ATTR_UNUSED BOOL bWait)
1000
0
{
1001
0
  WLog_ERR("TODO", "TODO: implement");
1002
0
  return FALSE;
1003
0
}
1004
1005
BOOL WINAPI FreeRDP_WTSConnectSessionA(WINPR_ATTR_UNUSED ULONG LogonId,
1006
                                       WINPR_ATTR_UNUSED ULONG TargetLogonId,
1007
                                       WINPR_ATTR_UNUSED PSTR pPassword,
1008
                                       WINPR_ATTR_UNUSED BOOL bWait)
1009
0
{
1010
0
  WLog_ERR("TODO", "TODO: implement");
1011
0
  return FALSE;
1012
0
}
1013
1014
BOOL WINAPI FreeRDP_WTSEnumerateServersW(WINPR_ATTR_UNUSED LPWSTR pDomainName,
1015
                                         WINPR_ATTR_UNUSED DWORD Reserved,
1016
                                         WINPR_ATTR_UNUSED DWORD Version,
1017
                                         WINPR_ATTR_UNUSED PWTS_SERVER_INFOW* ppServerInfo,
1018
                                         WINPR_ATTR_UNUSED DWORD* pCount)
1019
0
{
1020
0
  WLog_ERR("TODO", "TODO: implement");
1021
0
  return FALSE;
1022
0
}
1023
1024
BOOL WINAPI FreeRDP_WTSEnumerateServersA(WINPR_ATTR_UNUSED LPSTR pDomainName,
1025
                                         WINPR_ATTR_UNUSED DWORD Reserved,
1026
                                         WINPR_ATTR_UNUSED DWORD Version,
1027
                                         WINPR_ATTR_UNUSED PWTS_SERVER_INFOA* ppServerInfo,
1028
                                         WINPR_ATTR_UNUSED DWORD* pCount)
1029
0
{
1030
0
  WLog_ERR("TODO", "TODO: implement");
1031
0
  return FALSE;
1032
0
}
1033
1034
HANDLE WINAPI FreeRDP_WTSOpenServerW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1035
0
{
1036
0
  WLog_ERR("TODO", "TODO: implement");
1037
0
  return INVALID_HANDLE_VALUE;
1038
0
}
1039
1040
static void wts_virtual_channel_manager_free_message(void* obj)
1041
0
{
1042
0
  wMessage* msg = (wMessage*)obj;
1043
1044
0
  if (msg)
1045
0
  {
1046
0
    BYTE* buffer = (BYTE*)msg->wParam;
1047
1048
0
    if (buffer)
1049
0
      free(buffer);
1050
0
  }
1051
0
}
1052
1053
static void channel_free(rdpPeerChannel* channel)
1054
0
{
1055
0
  server_channel_common_free(channel);
1056
0
}
1057
1058
static void array_channel_free(void* ptr)
1059
0
{
1060
0
  rdpPeerChannel* channel = ptr;
1061
0
  channel_free(channel);
1062
0
}
1063
1064
static BOOL dynChannelMatch(const void* v1, const void* v2)
1065
0
{
1066
0
  const UINT32* p1 = (const UINT32*)v1;
1067
0
  const UINT32* p2 = (const UINT32*)v2;
1068
0
  return *p1 == *p2;
1069
0
}
1070
1071
static UINT32 channelId_Hash(const void* key)
1072
0
{
1073
0
  const UINT32* v = (const UINT32*)key;
1074
0
  return *v;
1075
0
}
1076
1077
static void clearHandles(void)
1078
1
{
1079
1
  HashTable_Free(g_ServerHandles);
1080
1
  g_ServerHandles = nullptr;
1081
1
}
1082
1083
static BOOL CALLBACK initializeHandles(WINPR_ATTR_UNUSED PINIT_ONCE once,
1084
                                       WINPR_ATTR_UNUSED PVOID param,
1085
                                       WINPR_ATTR_UNUSED PVOID* context)
1086
1
{
1087
1
  WINPR_ASSERT(g_ServerHandles == nullptr);
1088
1
  g_ServerHandles = HashTable_New(TRUE);
1089
1
  (void)winpr_atexit(clearHandles);
1090
1
  return g_ServerHandles != nullptr;
1091
1
}
1092
1093
static bool setup(void)
1094
20
{
1095
20
  return InitOnceExecuteOnce(&g_HandleInitializer, initializeHandles, nullptr, nullptr);
1096
20
}
1097
1098
static void wtsCloseVCM(WTSVirtualChannelManager* vcm, bool closeDrdynvc)
1099
20
{
1100
20
  WINPR_ASSERT(vcm);
1101
1102
20
  HashTable_Lock(g_ServerHandles);
1103
1104
/* clang analyzer does not like the check for INVALID_HANDLE_VALUE and considers the path not taken,
1105
 * leading to false positives on memory leaks. */
1106
#ifdef __clang_analyzer__
1107
  const BOOL valid = vcm != nullptr;
1108
#else
1109
20
  const BOOL valid = (vcm != nullptr) && (vcm != INVALID_HANDLE_VALUE);
1110
20
#endif
1111
20
  if (valid)
1112
20
  {
1113
20
    HashTable_Remove(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId);
1114
1115
20
    HashTable_Free(vcm->dynamicVirtualChannels);
1116
1117
20
    if (vcm->drdynvc_channel)
1118
0
    {
1119
0
      if (closeDrdynvc)
1120
0
        (void)WTSVirtualChannelClose(vcm->drdynvc_channel);
1121
0
      vcm->drdynvc_channel = nullptr;
1122
0
    }
1123
1124
20
    MessageQueue_Free(vcm->queue);
1125
20
    free(vcm);
1126
20
  }
1127
20
  HashTable_Unlock(g_ServerHandles);
1128
20
}
1129
1130
HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName)
1131
20
{
1132
20
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1133
1134
20
  rdpContext* context = WINPR_PACKED_ALIGN_CAST(rdpContext*, pServerName);
1135
1136
20
  if (!setup() || !context)
1137
0
    return INVALID_HANDLE_VALUE;
1138
1139
20
  freerdp_peer* client = context->peer;
1140
1141
20
  if (!client)
1142
0
  {
1143
0
    SetLastError(ERROR_INVALID_DATA);
1144
0
    return INVALID_HANDLE_VALUE;
1145
0
  }
1146
1147
20
  WTSVirtualChannelManager* vcm = calloc(1, sizeof(WTSVirtualChannelManager));
1148
1149
20
  if (!vcm)
1150
0
    goto fail;
1151
1152
20
  vcm->client = client;
1153
20
  vcm->rdp = context->rdp;
1154
1155
20
  queueCallbacks.fnObjectFree = wts_virtual_channel_manager_free_message;
1156
20
  vcm->queue = MessageQueue_New(&queueCallbacks);
1157
1158
20
  if (!vcm->queue)
1159
0
    goto fail;
1160
1161
20
  vcm->dvc_channel_id_seq = 0;
1162
20
  vcm->dynamicVirtualChannels = HashTable_New(TRUE);
1163
1164
20
  if (!vcm->dynamicVirtualChannels)
1165
0
    goto fail;
1166
1167
20
  if (!HashTable_SetHashFunction(vcm->dynamicVirtualChannels, channelId_Hash))
1168
0
    goto fail;
1169
1170
20
  {
1171
20
    wObject* obj = HashTable_ValueObject(vcm->dynamicVirtualChannels);
1172
20
    WINPR_ASSERT(obj);
1173
20
    obj->fnObjectFree = array_channel_free;
1174
1175
20
    obj = HashTable_KeyObject(vcm->dynamicVirtualChannels);
1176
20
    obj->fnObjectEquals = dynChannelMatch;
1177
20
  }
1178
20
  client->ReceiveChannelData = WTSReceiveChannelData;
1179
20
  {
1180
20
    HashTable_Lock(g_ServerHandles);
1181
20
    vcm->SessionId = g_SessionId++;
1182
20
    const BOOL rc =
1183
20
        HashTable_Insert(g_ServerHandles, (void*)(UINT_PTR)vcm->SessionId, (void*)vcm);
1184
20
    HashTable_Unlock(g_ServerHandles);
1185
20
    if (!rc)
1186
0
      goto fail;
1187
20
  }
1188
1189
20
  HANDLE hServer = (HANDLE)vcm;
1190
20
  return hServer;
1191
1192
0
fail:
1193
0
  wtsCloseVCM(vcm, false);
1194
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1195
0
  return INVALID_HANDLE_VALUE;
1196
20
}
1197
1198
HANDLE WINAPI FreeRDP_WTSOpenServerExW(WINPR_ATTR_UNUSED LPWSTR pServerName)
1199
0
{
1200
0
  WLog_ERR("TODO", "TODO: implement");
1201
0
  return INVALID_HANDLE_VALUE;
1202
0
}
1203
1204
HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName)
1205
0
{
1206
0
  return FreeRDP_WTSOpenServerA(pServerName);
1207
0
}
1208
1209
VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer)
1210
20
{
1211
20
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
1212
20
  wtsCloseVCM(vcm, true);
1213
20
}
1214
1215
BOOL WINAPI FreeRDP_WTSEnumerateSessionsW(WINPR_ATTR_UNUSED HANDLE hServer,
1216
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1217
                                          WINPR_ATTR_UNUSED DWORD Version,
1218
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOW* ppSessionInfo,
1219
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1220
0
{
1221
0
  WLog_ERR("TODO", "TODO: implement");
1222
0
  return FALSE;
1223
0
}
1224
1225
BOOL WINAPI FreeRDP_WTSEnumerateSessionsA(WINPR_ATTR_UNUSED HANDLE hServer,
1226
                                          WINPR_ATTR_UNUSED DWORD Reserved,
1227
                                          WINPR_ATTR_UNUSED DWORD Version,
1228
                                          WINPR_ATTR_UNUSED PWTS_SESSION_INFOA* ppSessionInfo,
1229
                                          WINPR_ATTR_UNUSED DWORD* pCount)
1230
0
{
1231
0
  WLog_ERR("TODO", "TODO: implement");
1232
0
  return FALSE;
1233
0
}
1234
1235
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExW(WINPR_ATTR_UNUSED HANDLE hServer,
1236
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1237
                                            WINPR_ATTR_UNUSED DWORD Filter,
1238
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1W* ppSessionInfo,
1239
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1240
0
{
1241
0
  WLog_ERR("TODO", "TODO: implement");
1242
0
  return FALSE;
1243
0
}
1244
1245
BOOL WINAPI FreeRDP_WTSEnumerateSessionsExA(WINPR_ATTR_UNUSED HANDLE hServer,
1246
                                            WINPR_ATTR_UNUSED DWORD* pLevel,
1247
                                            WINPR_ATTR_UNUSED DWORD Filter,
1248
                                            WINPR_ATTR_UNUSED PWTS_SESSION_INFO_1A* ppSessionInfo,
1249
                                            WINPR_ATTR_UNUSED DWORD* pCount)
1250
0
{
1251
0
  WLog_ERR("TODO", "TODO: implement");
1252
0
  return FALSE;
1253
0
}
1254
1255
BOOL WINAPI FreeRDP_WTSEnumerateProcessesW(WINPR_ATTR_UNUSED HANDLE hServer,
1256
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1257
                                           WINPR_ATTR_UNUSED DWORD Version,
1258
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOW* ppProcessInfo,
1259
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1260
0
{
1261
0
  WLog_ERR("TODO", "TODO: implement");
1262
0
  return FALSE;
1263
0
}
1264
1265
BOOL WINAPI FreeRDP_WTSEnumerateProcessesA(WINPR_ATTR_UNUSED HANDLE hServer,
1266
                                           WINPR_ATTR_UNUSED DWORD Reserved,
1267
                                           WINPR_ATTR_UNUSED DWORD Version,
1268
                                           WINPR_ATTR_UNUSED PWTS_PROCESS_INFOA* ppProcessInfo,
1269
                                           WINPR_ATTR_UNUSED DWORD* pCount)
1270
0
{
1271
0
  WLog_ERR("TODO", "TODO: implement");
1272
0
  return FALSE;
1273
0
}
1274
1275
BOOL WINAPI FreeRDP_WTSTerminateProcess(WINPR_ATTR_UNUSED HANDLE hServer,
1276
                                        WINPR_ATTR_UNUSED DWORD ProcessId,
1277
                                        WINPR_ATTR_UNUSED DWORD ExitCode)
1278
0
{
1279
0
  WLog_ERR("TODO", "TODO: implement");
1280
0
  return FALSE;
1281
0
}
1282
1283
BOOL WINAPI FreeRDP_WTSQuerySessionInformationW(WINPR_ATTR_UNUSED HANDLE hServer,
1284
                                                WINPR_ATTR_UNUSED DWORD SessionId,
1285
                                                WINPR_ATTR_UNUSED WTS_INFO_CLASS WTSInfoClass,
1286
                                                WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1287
                                                WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1288
0
{
1289
0
  WLog_ERR("TODO", "TODO: implement");
1290
0
  return FALSE;
1291
0
}
1292
1293
BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1294
                                                WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer,
1295
                                                DWORD* pBytesReturned)
1296
0
{
1297
0
  DWORD BytesReturned = 0;
1298
0
  WTSVirtualChannelManager* vcm = nullptr;
1299
0
  vcm = (WTSVirtualChannelManager*)hServer;
1300
1301
0
  if (!vcm)
1302
0
    return FALSE;
1303
1304
0
  if (WTSInfoClass == WTSSessionId)
1305
0
  {
1306
0
    ULONG* pBuffer = nullptr;
1307
0
    BytesReturned = sizeof(ULONG);
1308
0
    pBuffer = (ULONG*)malloc(sizeof(BytesReturned));
1309
1310
0
    if (!pBuffer)
1311
0
    {
1312
0
      SetLastError(g_err_oom);
1313
0
      return FALSE;
1314
0
    }
1315
1316
0
    *pBuffer = vcm->SessionId;
1317
0
    *ppBuffer = (LPSTR)pBuffer;
1318
0
    *pBytesReturned = BytesReturned;
1319
0
    return TRUE;
1320
0
  }
1321
1322
0
  return FALSE;
1323
0
}
1324
1325
BOOL WINAPI FreeRDP_WTSQueryUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1326
                                        WINPR_ATTR_UNUSED LPWSTR pUserName,
1327
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1328
                                        WINPR_ATTR_UNUSED LPWSTR* ppBuffer,
1329
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1330
0
{
1331
0
  WLog_ERR("TODO", "TODO: implement");
1332
0
  return FALSE;
1333
0
}
1334
1335
BOOL WINAPI FreeRDP_WTSQueryUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1336
                                        WINPR_ATTR_UNUSED LPSTR pUserName,
1337
                                        WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1338
                                        WINPR_ATTR_UNUSED LPSTR* ppBuffer,
1339
                                        WINPR_ATTR_UNUSED DWORD* pBytesReturned)
1340
0
{
1341
0
  WLog_ERR("TODO", "TODO: implement");
1342
0
  return FALSE;
1343
0
}
1344
1345
BOOL WINAPI FreeRDP_WTSSetUserConfigW(WINPR_ATTR_UNUSED LPWSTR pServerName,
1346
                                      WINPR_ATTR_UNUSED LPWSTR pUserName,
1347
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1348
                                      WINPR_ATTR_UNUSED LPWSTR pBuffer,
1349
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1350
0
{
1351
0
  WLog_ERR("TODO", "TODO: implement");
1352
0
  return FALSE;
1353
0
}
1354
1355
BOOL WINAPI FreeRDP_WTSSetUserConfigA(WINPR_ATTR_UNUSED LPSTR pServerName,
1356
                                      WINPR_ATTR_UNUSED LPSTR pUserName,
1357
                                      WINPR_ATTR_UNUSED WTS_CONFIG_CLASS WTSConfigClass,
1358
                                      WINPR_ATTR_UNUSED LPSTR pBuffer,
1359
                                      WINPR_ATTR_UNUSED DWORD DataLength)
1360
0
{
1361
0
  WLog_ERR("TODO", "TODO: implement");
1362
0
  return FALSE;
1363
0
}
1364
1365
BOOL WINAPI
1366
FreeRDP_WTSSendMessageW(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1367
                        WINPR_ATTR_UNUSED LPWSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1368
                        WINPR_ATTR_UNUSED LPWSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1369
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1370
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1371
0
{
1372
0
  WLog_ERR("TODO", "TODO: implement");
1373
0
  return FALSE;
1374
0
}
1375
1376
BOOL WINAPI
1377
FreeRDP_WTSSendMessageA(WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED DWORD SessionId,
1378
                        WINPR_ATTR_UNUSED LPSTR pTitle, WINPR_ATTR_UNUSED DWORD TitleLength,
1379
                        WINPR_ATTR_UNUSED LPSTR pMessage, WINPR_ATTR_UNUSED DWORD MessageLength,
1380
                        WINPR_ATTR_UNUSED DWORD Style, WINPR_ATTR_UNUSED DWORD Timeout,
1381
                        WINPR_ATTR_UNUSED DWORD* pResponse, WINPR_ATTR_UNUSED BOOL bWait)
1382
0
{
1383
0
  WLog_ERR("TODO", "TODO: implement");
1384
0
  return FALSE;
1385
0
}
1386
1387
BOOL WINAPI FreeRDP_WTSDisconnectSession(WINPR_ATTR_UNUSED HANDLE hServer,
1388
                                         WINPR_ATTR_UNUSED DWORD SessionId,
1389
                                         WINPR_ATTR_UNUSED BOOL bWait)
1390
0
{
1391
0
  WLog_ERR("TODO", "TODO: implement");
1392
0
  return FALSE;
1393
0
}
1394
1395
BOOL WINAPI FreeRDP_WTSLogoffSession(WINPR_ATTR_UNUSED HANDLE hServer,
1396
                                     WINPR_ATTR_UNUSED DWORD SessionId,
1397
                                     WINPR_ATTR_UNUSED BOOL bWait)
1398
0
{
1399
0
  WLog_ERR("TODO", "TODO: implement");
1400
0
  return FALSE;
1401
0
}
1402
1403
BOOL WINAPI FreeRDP_WTSShutdownSystem(WINPR_ATTR_UNUSED HANDLE hServer,
1404
                                      WINPR_ATTR_UNUSED DWORD ShutdownFlag)
1405
0
{
1406
0
  WLog_ERR("TODO", "TODO: implement");
1407
0
  return FALSE;
1408
0
}
1409
1410
BOOL WINAPI FreeRDP_WTSWaitSystemEvent(WINPR_ATTR_UNUSED HANDLE hServer,
1411
                                       WINPR_ATTR_UNUSED DWORD EventMask,
1412
                                       WINPR_ATTR_UNUSED DWORD* pEventFlags)
1413
0
{
1414
0
  WLog_ERR("TODO", "TODO: implement");
1415
0
  return FALSE;
1416
0
}
1417
1418
static void peer_channel_queue_free_message(void* obj)
1419
0
{
1420
0
  wMessage* msg = (wMessage*)obj;
1421
0
  if (!msg)
1422
0
    return;
1423
1424
0
  free(msg->context);
1425
0
  msg->context = nullptr;
1426
0
}
1427
1428
static rdpPeerChannel* channel_new(WTSVirtualChannelManager* vcm, freerdp_peer* client,
1429
                                   UINT32 ChannelId, UINT16 index, UINT16 type, size_t chunkSize,
1430
                                   const char* name, UINT32 flags)
1431
0
{
1432
0
  wObject queueCallbacks = WINPR_C_ARRAY_INIT;
1433
0
  queueCallbacks.fnObjectFree = peer_channel_queue_free_message;
1434
1435
0
  rdpPeerChannel* channel =
1436
0
      server_channel_common_new(client, index, ChannelId, chunkSize, &queueCallbacks, name);
1437
1438
0
  WINPR_ASSERT(vcm);
1439
0
  WINPR_ASSERT(client);
1440
1441
0
  if (!channel)
1442
0
    goto fail;
1443
1444
0
  channel->vcm = vcm;
1445
0
  channel->channelType = type;
1446
0
  channel->creationStatus =
1447
0
      (type == RDP_PEER_CHANNEL_TYPE_SVC) ? ERROR_SUCCESS : ERROR_OPERATION_IN_PROGRESS;
1448
0
  channel->channelFlags = flags;
1449
1450
0
  return channel;
1451
0
fail:
1452
0
  channel_free(channel);
1453
0
  return nullptr;
1454
0
}
1455
1456
static HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenStatic(HANDLE hServer,
1457
                                                         WINPR_ATTR_UNUSED DWORD SessionId,
1458
                                                         LPSTR pVirtualName, UINT32 Flags)
1459
0
{
1460
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
1461
0
  if (!vcm)
1462
0
  {
1463
0
    SetLastError(ERROR_INVALID_DATA);
1464
0
    return nullptr;
1465
0
  }
1466
1467
0
  freerdp_peer* client = vcm->client;
1468
0
  WINPR_ASSERT(client);
1469
1470
0
  rdpContext* context = client->context;
1471
0
  WINPR_ASSERT(context);
1472
0
  WINPR_ASSERT(context->rdp);
1473
0
  WINPR_ASSERT(context->settings);
1474
1475
0
  rdpMcs* mcs = context->rdp->mcs;
1476
0
  WINPR_ASSERT(mcs);
1477
1478
0
  size_t length = strnlen(pVirtualName, CHANNEL_NAME_LEN + 1);
1479
0
  if (length > CHANNEL_NAME_LEN)
1480
0
  {
1481
0
    SetLastError(ERROR_NOT_FOUND);
1482
0
    return nullptr;
1483
0
  }
1484
1485
0
  rdpMcsChannel* joined_channel = nullptr;
1486
0
  UINT32 index = 0;
1487
0
  for (; index < mcs->channelCount; index++)
1488
0
  {
1489
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1490
0
    if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
1491
0
    {
1492
0
      joined_channel = mchannel;
1493
0
      break;
1494
0
    }
1495
0
  }
1496
1497
0
  if (!joined_channel)
1498
0
  {
1499
0
    SetLastError(ERROR_NOT_FOUND);
1500
0
    return nullptr;
1501
0
  }
1502
1503
0
  rdpPeerChannel* channel = (rdpPeerChannel*)joined_channel->handle;
1504
0
  if (!channel)
1505
0
  {
1506
0
    const UINT32 VCChunkSize =
1507
0
        freerdp_settings_get_uint32(context->settings, FreeRDP_VCChunkSize);
1508
1509
0
    WINPR_ASSERT(index <= UINT16_MAX);
1510
0
    channel = channel_new(vcm, client, joined_channel->ChannelId, (UINT16)index,
1511
0
                          RDP_PEER_CHANNEL_TYPE_SVC, VCChunkSize, pVirtualName, Flags);
1512
1513
0
    if (!channel)
1514
0
      goto fail;
1515
1516
0
    joined_channel->handle = channel;
1517
0
  }
1518
1519
0
  HANDLE hChannelHandle = (HANDLE)channel;
1520
0
  return hChannelHandle;
1521
0
fail:
1522
0
  channel_free(channel);
1523
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1524
0
  return nullptr;
1525
0
}
1526
1527
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId, LPSTR pVirtualName)
1528
0
{
1529
0
  return FreeRDP_WTSVirtualChannelOpenStatic(hServer, SessionId, pVirtualName, 0);
1530
0
}
1531
1532
HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName, DWORD flags)
1533
0
{
1534
0
  wStream* s = nullptr;
1535
0
  rdpPeerChannel* channel = nullptr;
1536
0
  BOOL joined = FALSE;
1537
0
  ULONG written = 0;
1538
1539
0
  if (!setup())
1540
0
    return nullptr;
1541
1542
0
  if (SessionId == WTS_CURRENT_SESSION)
1543
0
    return nullptr;
1544
1545
0
  HashTable_Lock(g_ServerHandles);
1546
0
  WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)HashTable_GetItemValue(
1547
0
      g_ServerHandles, (void*)(UINT_PTR)SessionId);
1548
1549
0
  if (!vcm)
1550
0
    goto end;
1551
1552
0
  if (!(flags & WTS_CHANNEL_OPTION_DYNAMIC))
1553
0
  {
1554
0
    HashTable_Unlock(g_ServerHandles);
1555
0
    return FreeRDP_WTSVirtualChannelOpenStatic((HANDLE)vcm, SessionId, pVirtualName, flags);
1556
0
  }
1557
1558
0
  freerdp_peer* client = vcm->client;
1559
0
  WINPR_ASSERT(client);
1560
0
  WINPR_ASSERT(client->context);
1561
0
  WINPR_ASSERT(client->context->rdp);
1562
1563
0
  rdpMcs* mcs = client->context->rdp->mcs;
1564
0
  WINPR_ASSERT(mcs);
1565
1566
0
  for (UINT32 index = 0; index < mcs->channelCount; index++)
1567
0
  {
1568
0
    rdpMcsChannel* mchannel = &mcs->channels[index];
1569
0
    if (mchannel->joined &&
1570
0
        (strncmp(mchannel->Name, DRDYNVC_SVC_CHANNEL_NAME, CHANNEL_NAME_LEN + 1) == 0))
1571
0
    {
1572
0
      joined = TRUE;
1573
0
      break;
1574
0
    }
1575
0
  }
1576
1577
0
  if (!joined)
1578
0
  {
1579
0
    SetLastError(ERROR_NOT_FOUND);
1580
0
    goto end;
1581
0
  }
1582
1583
0
  if (!vcm->drdynvc_channel || (vcm->drdynvc_state != DRDYNVC_STATE_READY))
1584
0
  {
1585
0
    SetLastError(ERROR_NOT_READY);
1586
0
    goto end;
1587
0
  }
1588
1589
0
  WINPR_ASSERT(client);
1590
0
  WINPR_ASSERT(client->context);
1591
0
  WINPR_ASSERT(client->context->settings);
1592
1593
0
  const UINT32 VCChunkSize =
1594
0
      freerdp_settings_get_uint32(client->context->settings, FreeRDP_VCChunkSize);
1595
0
  channel =
1596
0
      channel_new(vcm, client, 0, 0, RDP_PEER_CHANNEL_TYPE_DVC, VCChunkSize, pVirtualName, flags);
1597
1598
0
  if (!channel)
1599
0
  {
1600
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1601
0
    goto end;
1602
0
  }
1603
1604
0
  const LONG hdl = InterlockedIncrement(&vcm->dvc_channel_id_seq);
1605
0
  channel->channelId = WINPR_ASSERTING_INT_CAST(uint32_t, hdl);
1606
1607
0
  if (!HashTable_Insert(vcm->dynamicVirtualChannels, &channel->channelId, channel))
1608
0
  {
1609
0
    channel_free(channel);
1610
0
    channel = nullptr;
1611
0
    goto fail;
1612
0
  }
1613
0
  s = Stream_New(nullptr, 64);
1614
1615
0
  if (!s)
1616
0
    goto fail;
1617
1618
0
  if (!wts_write_drdynvc_create_request(s, channel->channelId, pVirtualName))
1619
0
    goto fail;
1620
1621
0
  {
1622
0
    const size_t pos = Stream_GetPosition(s);
1623
0
    WINPR_ASSERT(pos <= UINT32_MAX);
1624
0
    if (!WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char), (UINT32)pos,
1625
0
                                &written))
1626
0
      goto fail;
1627
0
  }
1628
1629
0
end:
1630
0
  Stream_Free(s, TRUE);
1631
0
  HashTable_Unlock(g_ServerHandles);
1632
0
  return channel;
1633
1634
0
fail:
1635
0
  Stream_Free(s, TRUE);
1636
0
  if (channel)
1637
0
    HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1638
0
  HashTable_Unlock(g_ServerHandles);
1639
1640
0
  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1641
0
  return nullptr;
1642
0
}
1643
1644
BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
1645
0
{
1646
0
  wStream* s = nullptr;
1647
0
  rdpMcs* mcs = nullptr;
1648
1649
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1650
0
  BOOL ret = TRUE;
1651
1652
0
  if (channel)
1653
0
  {
1654
0
    WTSVirtualChannelManager* vcm = channel->vcm;
1655
1656
0
    WINPR_ASSERT(vcm);
1657
0
    WINPR_ASSERT(vcm->client);
1658
0
    WINPR_ASSERT(vcm->client->context);
1659
0
    WINPR_ASSERT(vcm->client->context->rdp);
1660
0
    mcs = vcm->client->context->rdp->mcs;
1661
1662
0
    if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1663
0
    {
1664
0
      if (channel->index < mcs->channelCount)
1665
0
      {
1666
0
        rdpMcsChannel* cur = &mcs->channels[channel->index];
1667
0
        rdpPeerChannel* peerChannel = (rdpPeerChannel*)cur->handle;
1668
0
        channel_free(peerChannel);
1669
0
        cur->handle = nullptr;
1670
0
      }
1671
0
    }
1672
0
    else
1673
0
    {
1674
0
      if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
1675
0
      {
1676
0
        ULONG written = 0;
1677
0
        s = Stream_New(nullptr, 8);
1678
1679
0
        if (!s)
1680
0
        {
1681
0
          WLog_ERR(TAG, "Stream_New failed!");
1682
0
          ret = FALSE;
1683
0
        }
1684
0
        else
1685
0
        {
1686
0
          wts_write_drdynvc_header(s, CLOSE_REQUEST_PDU, channel->channelId);
1687
1688
0
          const size_t pos = Stream_GetPosition(s);
1689
0
          WINPR_ASSERT(pos <= UINT32_MAX);
1690
0
          ret = WTSVirtualChannelWrite(vcm->drdynvc_channel, Stream_BufferAs(s, char),
1691
0
                                       (UINT32)pos, &written);
1692
0
          Stream_Free(s, TRUE);
1693
0
        }
1694
0
      }
1695
0
      HashTable_Remove(vcm->dynamicVirtualChannels, &channel->channelId);
1696
0
    }
1697
0
  }
1698
1699
0
  return ret;
1700
0
}
1701
1702
BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, WINPR_ATTR_UNUSED ULONG TimeOut,
1703
                                          PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead)
1704
0
{
1705
0
  BYTE* buffer = nullptr;
1706
0
  wMessage message = WINPR_C_ARRAY_INIT;
1707
0
  wtsChannelMessage* messageCtx = nullptr;
1708
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1709
1710
0
  WINPR_ASSERT(channel);
1711
1712
0
  if (!MessageQueue_Peek(channel->queue, &message, FALSE))
1713
0
  {
1714
0
    SetLastError(ERROR_NO_DATA);
1715
0
    *pBytesRead = 0;
1716
0
    return FALSE;
1717
0
  }
1718
1719
0
  messageCtx = message.context;
1720
1721
0
  if (messageCtx == nullptr)
1722
0
    return FALSE;
1723
1724
0
  buffer = (BYTE*)(messageCtx + 1);
1725
0
  *pBytesRead = messageCtx->length - messageCtx->offset;
1726
1727
0
  if (Buffer == nullptr || BufferSize == 0)
1728
0
  {
1729
0
    return TRUE;
1730
0
  }
1731
1732
0
  if (*pBytesRead > BufferSize)
1733
0
    *pBytesRead = BufferSize;
1734
1735
0
  CopyMemory(Buffer, buffer + messageCtx->offset, *pBytesRead);
1736
0
  messageCtx->offset += *pBytesRead;
1737
1738
0
  if (messageCtx->offset >= messageCtx->length)
1739
0
  {
1740
0
    const int rc = MessageQueue_Peek(channel->queue, &message, TRUE);
1741
0
    peer_channel_queue_free_message(&message);
1742
0
    if (rc < 0)
1743
0
      return FALSE;
1744
0
  }
1745
1746
0
  return TRUE;
1747
0
}
1748
1749
BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG uLength,
1750
                                           PULONG pBytesWritten)
1751
0
{
1752
0
  wStream* s = nullptr;
1753
0
  int cbLen = 0;
1754
0
  int cbChId = 0;
1755
0
  int first = 0;
1756
0
  BYTE* buffer = nullptr;
1757
0
  size_t totalWritten = 0;
1758
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1759
0
  BOOL ret = FALSE;
1760
1761
0
  if (!channel)
1762
0
    return FALSE;
1763
1764
0
  EnterCriticalSection(&channel->writeLock);
1765
0
  WINPR_ASSERT(channel->vcm);
1766
0
  if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1767
0
  {
1768
0
    buffer = (BYTE*)malloc(uLength);
1769
1770
0
    if (!buffer)
1771
0
    {
1772
0
      SetLastError(g_err_oom);
1773
0
      goto fail;
1774
0
    }
1775
1776
0
    CopyMemory(buffer, Buffer, uLength);
1777
0
    totalWritten = uLength;
1778
0
    if (!wts_queue_send_item(channel, buffer, uLength))
1779
0
      goto fail;
1780
0
  }
1781
0
  else if (!channel->vcm->drdynvc_channel || (channel->vcm->drdynvc_state != DRDYNVC_STATE_READY))
1782
0
  {
1783
0
    DEBUG_DVC("drdynvc not ready");
1784
0
    goto fail;
1785
0
  }
1786
0
  else
1787
0
  {
1788
0
    first = TRUE;
1789
1790
0
    size_t Length = uLength;
1791
0
    while (Length > 0)
1792
0
    {
1793
0
      s = Stream_New(nullptr, DVC_MAX_DATA_PDU_SIZE);
1794
1795
0
      if (!s)
1796
0
      {
1797
0
        WLog_ERR(TAG, "Stream_New failed!");
1798
0
        SetLastError(g_err_oom);
1799
0
        goto fail;
1800
0
      }
1801
1802
0
      buffer = Stream_Buffer(s);
1803
0
      Stream_Seek_UINT8(s);
1804
0
      cbChId = wts_write_variable_uint(s, channel->channelId);
1805
1806
0
      if (first && (Length > Stream_GetRemainingLength(s)))
1807
0
      {
1808
0
        cbLen = wts_write_variable_uint(s, WINPR_ASSERTING_INT_CAST(uint32_t, Length));
1809
0
        buffer[0] = ((DATA_FIRST_PDU << 4) | (cbLen << 2) | cbChId) & 0xFF;
1810
0
      }
1811
0
      else
1812
0
      {
1813
0
        buffer[0] = ((DATA_PDU << 4) | cbChId) & 0xFF;
1814
0
      }
1815
1816
0
      first = FALSE;
1817
0
      size_t written = Stream_GetRemainingLength(s);
1818
1819
0
      if (written > Length)
1820
0
        written = Length;
1821
1822
0
      Stream_Write(s, Buffer, written);
1823
0
      const size_t length = Stream_GetPosition(s);
1824
0
      Stream_Free(s, FALSE);
1825
0
      if (length > UINT32_MAX)
1826
0
        goto fail;
1827
0
      Length -= written;
1828
0
      Buffer += written;
1829
0
      totalWritten += written;
1830
0
      if (!wts_queue_send_item(channel->vcm->drdynvc_channel, buffer, (UINT32)length))
1831
0
        goto fail;
1832
0
    }
1833
0
  }
1834
1835
0
  if (pBytesWritten)
1836
0
    *pBytesWritten = WINPR_ASSERTING_INT_CAST(uint32_t, totalWritten);
1837
1838
0
  ret = TRUE;
1839
0
fail:
1840
0
  LeaveCriticalSection(&channel->writeLock);
1841
0
  return ret;
1842
0
}
1843
1844
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1845
0
{
1846
0
  WLog_ERR("TODO", "TODO: implement");
1847
0
  return TRUE;
1848
0
}
1849
1850
BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(WINPR_ATTR_UNUSED HANDLE hChannelHandle)
1851
0
{
1852
0
  WLog_ERR("TODO", "TODO: implement");
1853
0
  return TRUE;
1854
0
}
1855
1856
BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS WtsVirtualClass,
1857
                                           PVOID* ppBuffer, DWORD* pBytesReturned)
1858
0
{
1859
0
  void* pfd = nullptr;
1860
0
  BOOL bval = 0;
1861
0
  void* fds[10] = WINPR_C_ARRAY_INIT;
1862
0
  HANDLE hEvent = nullptr;
1863
0
  int fds_count = 0;
1864
0
  BOOL status = FALSE;
1865
0
  rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
1866
1867
0
  WINPR_ASSERT(channel);
1868
1869
0
  switch ((UINT32)WtsVirtualClass)
1870
0
  {
1871
0
    case WTSVirtualFileHandle:
1872
0
      hEvent = MessageQueue_Event(channel->queue);
1873
0
      pfd = GetEventWaitObject(hEvent);
1874
1875
0
      if (pfd)
1876
0
      {
1877
0
        fds[fds_count] = pfd;
1878
0
        (fds_count)++;
1879
0
      }
1880
1881
0
      *ppBuffer = malloc(sizeof(void*));
1882
1883
0
      if (!*ppBuffer)
1884
0
      {
1885
0
        SetLastError(g_err_oom);
1886
0
      }
1887
0
      else
1888
0
      {
1889
0
        CopyMemory(*ppBuffer, (void*)&fds[0], sizeof(void*));
1890
0
        *pBytesReturned = sizeof(void*);
1891
0
        status = TRUE;
1892
0
      }
1893
1894
0
      break;
1895
1896
0
    case WTSVirtualEventHandle:
1897
0
      hEvent = MessageQueue_Event(channel->queue);
1898
1899
0
      *ppBuffer = malloc(sizeof(HANDLE));
1900
1901
0
      if (!*ppBuffer)
1902
0
      {
1903
0
        SetLastError(g_err_oom);
1904
0
      }
1905
0
      else
1906
0
      {
1907
0
        CopyMemory(*ppBuffer, (void*)&hEvent, sizeof(HANDLE));
1908
0
        *pBytesReturned = sizeof(void*);
1909
0
        status = TRUE;
1910
0
      }
1911
1912
0
      break;
1913
1914
0
    case WTSVirtualChannelReady:
1915
0
      if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
1916
0
      {
1917
0
        bval = TRUE;
1918
0
        status = TRUE;
1919
0
      }
1920
0
      else
1921
0
      {
1922
0
        switch (channel->dvc_open_state)
1923
0
        {
1924
0
          case DVC_OPEN_STATE_NONE:
1925
0
            bval = FALSE;
1926
0
            status = TRUE;
1927
0
            break;
1928
1929
0
          case DVC_OPEN_STATE_SUCCEEDED:
1930
0
            bval = TRUE;
1931
0
            status = TRUE;
1932
0
            break;
1933
1934
0
          default:
1935
0
            *ppBuffer = nullptr;
1936
0
            *pBytesReturned = 0;
1937
0
            return FALSE;
1938
0
        }
1939
0
      }
1940
1941
0
      *ppBuffer = malloc(sizeof(BOOL));
1942
1943
0
      if (!*ppBuffer)
1944
0
      {
1945
0
        SetLastError(g_err_oom);
1946
0
        status = FALSE;
1947
0
      }
1948
0
      else
1949
0
      {
1950
0
        CopyMemory(*ppBuffer, &bval, sizeof(BOOL));
1951
0
        *pBytesReturned = sizeof(BOOL);
1952
0
      }
1953
1954
0
      break;
1955
0
    case WTSVirtualChannelOpenStatus:
1956
0
    {
1957
0
      INT32 value = channel->creationStatus;
1958
0
      status = TRUE;
1959
1960
0
      *ppBuffer = malloc(sizeof(value));
1961
0
      if (!*ppBuffer)
1962
0
      {
1963
0
        SetLastError(g_err_oom);
1964
0
        status = FALSE;
1965
0
      }
1966
0
      else
1967
0
      {
1968
0
        CopyMemory(*ppBuffer, &value, sizeof(value));
1969
0
        *pBytesReturned = sizeof(value);
1970
0
      }
1971
0
      break;
1972
0
    }
1973
0
    default:
1974
0
      break;
1975
0
  }
1976
1977
0
  return status;
1978
0
}
1979
1980
VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory)
1981
0
{
1982
0
  free(pMemory);
1983
0
}
1984
1985
BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1986
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1987
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1988
0
{
1989
0
  WLog_ERR("TODO", "TODO: implement");
1990
0
  return FALSE;
1991
0
}
1992
1993
BOOL WINAPI FreeRDP_WTSFreeMemoryExA(WINPR_ATTR_UNUSED WTS_TYPE_CLASS WTSTypeClass,
1994
                                     WINPR_ATTR_UNUSED PVOID pMemory,
1995
                                     WINPR_ATTR_UNUSED ULONG NumberOfEntries)
1996
0
{
1997
0
  WLog_ERR("TODO", "TODO: implement");
1998
0
  return FALSE;
1999
0
}
2000
2001
BOOL WINAPI FreeRDP_WTSRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd,
2002
                                                   WINPR_ATTR_UNUSED DWORD dwFlags)
2003
0
{
2004
0
  WLog_ERR("TODO", "TODO: implement");
2005
0
  return FALSE;
2006
0
}
2007
2008
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(WINPR_ATTR_UNUSED HWND hWnd)
2009
0
{
2010
0
  WLog_ERR("TODO", "TODO: implement");
2011
0
  return FALSE;
2012
0
}
2013
2014
BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2015
                                                     WINPR_ATTR_UNUSED HWND hWnd,
2016
                                                     WINPR_ATTR_UNUSED DWORD dwFlags)
2017
0
{
2018
0
  WLog_ERR("TODO", "TODO: implement");
2019
0
  return FALSE;
2020
0
}
2021
2022
BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(WINPR_ATTR_UNUSED HANDLE hServer,
2023
                                                       WINPR_ATTR_UNUSED HWND hWnd)
2024
0
{
2025
0
  WLog_ERR("TODO", "TODO: implement");
2026
0
  return FALSE;
2027
0
}
2028
2029
BOOL WINAPI FreeRDP_WTSQueryUserToken(WINPR_ATTR_UNUSED ULONG SessionId,
2030
                                      WINPR_ATTR_UNUSED PHANDLE phToken)
2031
0
{
2032
0
  WLog_ERR("TODO", "TODO: implement");
2033
0
  return FALSE;
2034
0
}
2035
2036
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExW(WINPR_ATTR_UNUSED HANDLE hServer,
2037
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
2038
                                             WINPR_ATTR_UNUSED DWORD SessionId,
2039
                                             WINPR_ATTR_UNUSED LPWSTR* ppProcessInfo,
2040
                                             WINPR_ATTR_UNUSED DWORD* pCount)
2041
0
{
2042
0
  WLog_ERR("TODO", "TODO: implement");
2043
0
  return FALSE;
2044
0
}
2045
2046
BOOL WINAPI FreeRDP_WTSEnumerateProcessesExA(WINPR_ATTR_UNUSED HANDLE hServer,
2047
                                             WINPR_ATTR_UNUSED DWORD* pLevel,
2048
                                             WINPR_ATTR_UNUSED DWORD SessionId,
2049
                                             WINPR_ATTR_UNUSED LPSTR* ppProcessInfo,
2050
                                             WINPR_ATTR_UNUSED DWORD* pCount)
2051
0
{
2052
0
  WLog_ERR("TODO", "TODO: implement");
2053
0
  return FALSE;
2054
0
}
2055
2056
BOOL WINAPI FreeRDP_WTSEnumerateListenersW(WINPR_ATTR_UNUSED HANDLE hServer,
2057
                                           WINPR_ATTR_UNUSED PVOID pReserved,
2058
                                           WINPR_ATTR_UNUSED DWORD Reserved,
2059
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEW pListeners,
2060
                                           WINPR_ATTR_UNUSED DWORD* pCount)
2061
0
{
2062
0
  WLog_ERR("TODO", "TODO: implement");
2063
0
  return FALSE;
2064
0
}
2065
2066
BOOL WINAPI FreeRDP_WTSEnumerateListenersA(WINPR_ATTR_UNUSED HANDLE hServer,
2067
                                           WINPR_ATTR_UNUSED PVOID pReserved,
2068
                                           WINPR_ATTR_UNUSED DWORD Reserved,
2069
                                           WINPR_ATTR_UNUSED PWTSLISTENERNAMEA pListeners,
2070
                                           WINPR_ATTR_UNUSED DWORD* pCount)
2071
0
{
2072
0
  WLog_ERR("TODO", "TODO: implement");
2073
0
  return FALSE;
2074
0
}
2075
2076
BOOL WINAPI FreeRDP_WTSQueryListenerConfigW(WINPR_ATTR_UNUSED HANDLE hServer,
2077
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2078
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2079
                                            WINPR_ATTR_UNUSED LPWSTR pListenerName,
2080
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer)
2081
0
{
2082
0
  WLog_ERR("TODO", "TODO: implement");
2083
0
  return FALSE;
2084
0
}
2085
2086
BOOL WINAPI FreeRDP_WTSQueryListenerConfigA(WINPR_ATTR_UNUSED HANDLE hServer,
2087
                                            WINPR_ATTR_UNUSED PVOID pReserved,
2088
                                            WINPR_ATTR_UNUSED DWORD Reserved,
2089
                                            WINPR_ATTR_UNUSED LPSTR pListenerName,
2090
                                            WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer)
2091
0
{
2092
0
  WLog_ERR("TODO", "TODO: implement");
2093
0
  return FALSE;
2094
0
}
2095
2096
BOOL WINAPI FreeRDP_WTSCreateListenerW(WINPR_ATTR_UNUSED HANDLE hServer,
2097
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2098
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2099
                                       WINPR_ATTR_UNUSED LPWSTR pListenerName,
2100
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGW pBuffer,
2101
                                       WINPR_ATTR_UNUSED DWORD flag)
2102
0
{
2103
0
  WLog_ERR("TODO", "TODO: implement");
2104
0
  return FALSE;
2105
0
}
2106
2107
BOOL WINAPI FreeRDP_WTSCreateListenerA(WINPR_ATTR_UNUSED HANDLE hServer,
2108
                                       WINPR_ATTR_UNUSED PVOID pReserved,
2109
                                       WINPR_ATTR_UNUSED DWORD Reserved,
2110
                                       WINPR_ATTR_UNUSED LPSTR pListenerName,
2111
                                       WINPR_ATTR_UNUSED PWTSLISTENERCONFIGA pBuffer,
2112
                                       WINPR_ATTR_UNUSED DWORD flag)
2113
0
{
2114
0
  WLog_ERR("TODO", "TODO: implement");
2115
0
  return FALSE;
2116
0
}
2117
2118
BOOL WINAPI FreeRDP_WTSSetListenerSecurityW(
2119
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2120
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2121
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2122
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2123
0
{
2124
0
  WLog_ERR("TODO", "TODO: implement");
2125
0
  return FALSE;
2126
0
}
2127
2128
BOOL WINAPI FreeRDP_WTSSetListenerSecurityA(
2129
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2130
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2131
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2132
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor)
2133
0
{
2134
0
  WLog_ERR("TODO", "TODO: implement");
2135
0
  return FALSE;
2136
0
}
2137
2138
BOOL WINAPI FreeRDP_WTSGetListenerSecurityW(
2139
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2140
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPWSTR pListenerName,
2141
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2142
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2143
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2144
0
{
2145
0
  WLog_ERR("TODO", "TODO: implement");
2146
0
  return FALSE;
2147
0
}
2148
2149
BOOL WINAPI FreeRDP_WTSGetListenerSecurityA(
2150
    WINPR_ATTR_UNUSED HANDLE hServer, WINPR_ATTR_UNUSED PVOID pReserved,
2151
    WINPR_ATTR_UNUSED DWORD Reserved, WINPR_ATTR_UNUSED LPSTR pListenerName,
2152
    WINPR_ATTR_UNUSED SECURITY_INFORMATION SecurityInformation,
2153
    WINPR_ATTR_UNUSED PSECURITY_DESCRIPTOR pSecurityDescriptor, WINPR_ATTR_UNUSED DWORD nLength,
2154
    WINPR_ATTR_UNUSED LPDWORD lpnLengthNeeded)
2155
0
{
2156
0
  WLog_ERR("TODO", "TODO: implement");
2157
0
  return FALSE;
2158
0
}
2159
2160
BOOL CDECL FreeRDP_WTSEnableChildSessions(WINPR_ATTR_UNUSED BOOL bEnable)
2161
0
{
2162
0
  WLog_ERR("TODO", "TODO: implement");
2163
0
  return FALSE;
2164
0
}
2165
2166
BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(WINPR_ATTR_UNUSED PBOOL pbEnabled)
2167
0
{
2168
0
  WLog_ERR("TODO", "TODO: implement");
2169
0
  return FALSE;
2170
0
}
2171
2172
BOOL CDECL FreeRDP_WTSGetChildSessionId(WINPR_ATTR_UNUSED PULONG pSessionId)
2173
0
{
2174
0
  WLog_ERR("TODO", "TODO: implement");
2175
0
  return FALSE;
2176
0
}
2177
2178
DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(void)
2179
0
{
2180
0
  WLog_ERR("TODO", "TODO: implement");
2181
0
  return 0xFFFFFFFF;
2182
0
}
2183
BOOL WINAPI FreeRDP_WTSLogoffUser(WINPR_ATTR_UNUSED HANDLE hServer)
2184
0
{
2185
0
  WLog_ERR("TODO", "TODO: implement");
2186
0
  return FALSE;
2187
0
}
2188
2189
BOOL WINAPI FreeRDP_WTSLogonUser(WINPR_ATTR_UNUSED HANDLE hServer,
2190
                                 WINPR_ATTR_UNUSED LPCSTR username,
2191
                                 WINPR_ATTR_UNUSED LPCSTR password, WINPR_ATTR_UNUSED LPCSTR domain)
2192
0
{
2193
0
  WLog_ERR("TODO", "TODO: implement");
2194
0
  return FALSE;
2195
0
}
2196
2197
void server_channel_common_free(rdpPeerChannel* channel)
2198
0
{
2199
0
  if (!channel)
2200
0
    return;
2201
0
  MessageQueue_Free(channel->queue);
2202
0
  Stream_Free(channel->receiveData, TRUE);
2203
0
  DeleteCriticalSection(&channel->writeLock);
2204
0
  free(channel);
2205
0
}
2206
2207
rdpPeerChannel* server_channel_common_new(freerdp_peer* client, UINT16 index, UINT32 channelId,
2208
                                          size_t chunkSize, const wObject* callback,
2209
                                          const char* name)
2210
0
{
2211
0
  rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1, sizeof(rdpPeerChannel));
2212
0
  if (!channel)
2213
0
    return nullptr;
2214
2215
0
  InitializeCriticalSection(&channel->writeLock);
2216
2217
0
  channel->receiveData = Stream_New(nullptr, chunkSize);
2218
0
  if (!channel->receiveData)
2219
0
    goto fail;
2220
2221
0
  channel->queue = MessageQueue_New(callback);
2222
0
  if (!channel->queue)
2223
0
    goto fail;
2224
2225
0
  channel->index = index;
2226
0
  channel->client = client;
2227
0
  channel->channelId = channelId;
2228
0
  strncpy(channel->channelName, name, ARRAYSIZE(channel->channelName) - 1);
2229
0
  return channel;
2230
0
fail:
2231
0
  WINPR_PRAGMA_DIAG_PUSH
2232
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2233
0
  server_channel_common_free(channel);
2234
0
  WINPR_PRAGMA_DIAG_POP
2235
0
  return nullptr;
2236
0
}