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