Coverage Report

Created: 2026-08-31 06:29

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