22#include <freerdp/config.h>
25#include <winpr/print.h>
26#include <winpr/stream.h>
28#include <freerdp/freerdp.h>
29#include <freerdp/channels/log.h>
31#include "encomsp_main.h"
33#define TAG CHANNELS_TAG("encomsp.server")
42 if (!Stream_CheckAndLogRequiredLength(TAG, s, ENCOMSP_ORDER_HEADER_SIZE))
43 return ERROR_INVALID_DATA;
45 Stream_Read_UINT16(s, header->Type);
46 Stream_Read_UINT16(s, header->Length);
55static UINT encomsp_recv_change_participant_control_level_pdu(EncomspServerContext* context,
60 UINT error = CHANNEL_RC_OK;
62 const size_t pos = Stream_GetPosition(s);
63 if (pos < ENCOMSP_ORDER_HEADER_SIZE)
64 return ERROR_INVALID_PARAMETER;
66 const size_t beg = pos - ENCOMSP_ORDER_HEADER_SIZE;
69 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
70 return ERROR_INVALID_DATA;
72 Stream_Read_UINT16(s, pdu.Flags);
73 Stream_Read_UINT32(s, pdu.ParticipantId);
74 const size_t end = Stream_GetPosition(s);
76 if ((beg + header->Length) < end)
78 WLog_ERR(TAG,
"Not enough data!");
79 return ERROR_INVALID_DATA;
82 if ((beg + header->Length) > end)
84 if (!Stream_CheckAndLogRequiredLength(TAG, s, (
size_t)((beg + header->Length) - end)))
85 return ERROR_INVALID_DATA;
87 Stream_SetPosition(s, (beg + header->Length));
90 IFCALLRET(context->ChangeParticipantControlLevel, error, context, &pdu);
93 WLog_ERR(TAG,
"context->ChangeParticipantControlLevel failed with error %" PRIu32
"",
104static UINT encomsp_server_receive_pdu(EncomspServerContext* context,
wStream* s)
106 UINT error = CHANNEL_RC_OK;
108 while (Stream_GetRemainingLength(s) > 0)
111 if ((error = encomsp_read_header(s, &header)))
113 WLog_ERR(TAG,
"encomsp_read_header failed with error %" PRIu32
"!", error);
117 WLog_INFO(TAG,
"EncomspReceive: Type: %" PRIu16
" Length: %" PRIu16
"", header.Type,
122 case ODTYPE_PARTICIPANT_CTRL_CHANGED:
124 encomsp_recv_change_participant_control_level_pdu(context, s, &header)))
127 "encomsp_recv_change_participant_control_level_pdu failed with error "
136 WLog_ERR(TAG,
"header.Type unknown %" PRIu16
"!", header.Type);
137 return ERROR_INVALID_DATA;
144static DWORD WINAPI encomsp_server_thread(LPVOID arg)
150 HANDLE ChannelEvent = NULL;
151 DWORD BytesReturned = 0;
153 EncomspServerContext* context = NULL;
154 UINT error = CHANNEL_RC_OK;
156 context = (EncomspServerContext*)arg;
161 s = Stream_New(NULL, 4096);
165 WLog_ERR(TAG,
"Stream_New failed!");
166 error = CHANNEL_RC_NO_MEMORY;
170 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
171 &BytesReturned) == TRUE)
173 if (BytesReturned ==
sizeof(HANDLE))
174 ChannelEvent = *(HANDLE*)buffer;
176 WTSFreeMemory(buffer);
180 events[nCount++] = ChannelEvent;
181 events[nCount++] = context->priv->StopEvent;
185 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
187 if (status == WAIT_FAILED)
189 error = GetLastError();
190 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
194 status = WaitForSingleObject(context->priv->StopEvent, 0);
196 if (status == WAIT_FAILED)
198 error = GetLastError();
199 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
203 if (status == WAIT_OBJECT_0)
208 if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0, NULL, 0, &BytesReturned))
210 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
211 error = ERROR_INTERNAL_ERROR;
215 if (BytesReturned < 1)
218 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
220 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
221 error = CHANNEL_RC_NO_MEMORY;
225 const size_t cap = Stream_Capacity(s);
226 if ((cap > UINT32_MAX) ||
227 !WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s,
char),
228 (ULONG)cap, &BytesReturned))
230 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
231 error = ERROR_INTERNAL_ERROR;
235 if (Stream_GetPosition(s) >= ENCOMSP_ORDER_HEADER_SIZE)
239 if (header->Length >= Stream_GetPosition(s))
241 Stream_SealLength(s);
242 Stream_SetPosition(s, 0);
244 if ((error = encomsp_server_receive_pdu(context, s)))
246 WLog_ERR(TAG,
"encomsp_server_receive_pdu failed with error %" PRIu32
"!",
251 Stream_SetPosition(s, 0);
256 Stream_Free(s, TRUE);
259 if (error && context->rdpcontext)
260 setChannelError(context->rdpcontext, error,
"encomsp_server_thread reported an error");
271static UINT encomsp_server_start(EncomspServerContext* context)
273 context->priv->ChannelHandle =
274 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, ENCOMSP_SVC_CHANNEL_NAME);
276 if (!context->priv->ChannelHandle)
277 return CHANNEL_RC_BAD_CHANNEL;
279 if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
281 WLog_ERR(TAG,
"CreateEvent failed!");
282 return ERROR_INTERNAL_ERROR;
285 if (!(context->priv->Thread =
286 CreateThread(NULL, 0, encomsp_server_thread, (
void*)context, 0, NULL)))
288 WLog_ERR(TAG,
"CreateThread failed!");
289 (void)CloseHandle(context->priv->StopEvent);
290 context->priv->StopEvent = NULL;
291 return ERROR_INTERNAL_ERROR;
294 return CHANNEL_RC_OK;
302static UINT encomsp_server_stop(EncomspServerContext* context)
304 UINT error = CHANNEL_RC_OK;
305 (void)SetEvent(context->priv->StopEvent);
307 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
309 error = GetLastError();
310 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
314 (void)CloseHandle(context->priv->Thread);
315 (void)CloseHandle(context->priv->StopEvent);
319EncomspServerContext* encomsp_server_context_new(HANDLE vcm)
321 EncomspServerContext* context = NULL;
322 context = (EncomspServerContext*)calloc(1,
sizeof(EncomspServerContext));
327 context->Start = encomsp_server_start;
328 context->Stop = encomsp_server_stop;
329 context->priv = (EncomspServerPrivate*)calloc(1,
sizeof(EncomspServerPrivate));
333 WLog_ERR(TAG,
"calloc failed!");
342void encomsp_server_context_free(EncomspServerContext* context)
346 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
347 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);