22 #include <freerdp/config.h>
24 #include <winpr/crt.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);
54 Stream_Write_UINT16(s, header->Type);
55 Stream_Write_UINT16(s, header->Length);
63 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
66 Stream_Read_UINT16(s, str->cchString);
68 if (str->cchString > 1024)
71 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, str->cchString,
sizeof(WCHAR)))
74 Stream_Read(s, &(str->wString), (str->cchString * 2));
85 static UINT encomsp_recv_change_participant_control_level_pdu(EncomspServerContext* context,
92 UINT error = CHANNEL_RC_OK;
93 beg = ((int)Stream_GetPosition(s)) - ENCOMSP_ORDER_HEADER_SIZE;
96 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
97 return ERROR_INVALID_DATA;
99 Stream_Read_UINT16(s, pdu.Flags);
100 Stream_Read_UINT32(s, pdu.ParticipantId);
101 end = (int)Stream_GetPosition(s);
103 if ((beg + header->Length) < end)
105 WLog_ERR(TAG,
"Not enough data!");
106 return ERROR_INVALID_DATA;
109 if ((beg + header->Length) > end)
111 if (!Stream_CheckAndLogRequiredLength(TAG, s, (
size_t)((beg + header->Length) - end)))
112 return ERROR_INVALID_DATA;
114 Stream_SetPosition(s, (beg + header->Length));
117 IFCALLRET(context->ChangeParticipantControlLevel, error, context, &pdu);
120 WLog_ERR(TAG,
"context->ChangeParticipantControlLevel failed with error %" PRIu32
"",
131 static UINT encomsp_server_receive_pdu(EncomspServerContext* context,
wStream* s)
133 UINT error = CHANNEL_RC_OK;
136 while (Stream_GetRemainingLength(s) > 0)
138 if ((error = encomsp_read_header(s, &header)))
140 WLog_ERR(TAG,
"encomsp_read_header failed with error %" PRIu32
"!", error);
144 WLog_INFO(TAG,
"EncomspReceive: Type: %" PRIu16
" Length: %" PRIu16
"", header.Type,
149 case ODTYPE_PARTICIPANT_CTRL_CHANGED:
151 encomsp_recv_change_participant_control_level_pdu(context, s, &header)))
154 "encomsp_recv_change_participant_control_level_pdu failed with error "
163 WLog_ERR(TAG,
"header.Type unknown %" PRIu16
"!", header.Type);
164 return ERROR_INVALID_DATA;
171 static DWORD WINAPI encomsp_server_thread(LPVOID arg)
177 HANDLE ChannelEvent = NULL;
178 DWORD BytesReturned = 0;
180 EncomspServerContext* context = NULL;
181 UINT error = CHANNEL_RC_OK;
183 context = (EncomspServerContext*)arg;
188 s = Stream_New(NULL, 4096);
192 WLog_ERR(TAG,
"Stream_New failed!");
193 error = CHANNEL_RC_NO_MEMORY;
197 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
198 &BytesReturned) == TRUE)
200 if (BytesReturned ==
sizeof(HANDLE))
201 CopyMemory(&ChannelEvent, buffer,
sizeof(HANDLE));
203 WTSFreeMemory(buffer);
207 events[nCount++] = ChannelEvent;
208 events[nCount++] = context->priv->StopEvent;
212 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
214 if (status == WAIT_FAILED)
216 error = GetLastError();
217 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
221 status = WaitForSingleObject(context->priv->StopEvent, 0);
223 if (status == WAIT_FAILED)
225 error = GetLastError();
226 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
230 if (status == WAIT_OBJECT_0)
235 if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0, NULL, 0, &BytesReturned))
237 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
238 error = ERROR_INTERNAL_ERROR;
242 if (BytesReturned < 1)
245 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
247 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
248 error = CHANNEL_RC_NO_MEMORY;
252 const size_t cap = Stream_Capacity(s);
253 if ((cap > UINT32_MAX) ||
254 !WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s,
char),
255 (ULONG)cap, &BytesReturned))
257 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
258 error = ERROR_INTERNAL_ERROR;
262 if (Stream_GetPosition(s) >= ENCOMSP_ORDER_HEADER_SIZE)
266 if (header->Length >= Stream_GetPosition(s))
268 Stream_SealLength(s);
269 Stream_SetPosition(s, 0);
271 if ((error = encomsp_server_receive_pdu(context, s)))
273 WLog_ERR(TAG,
"encomsp_server_receive_pdu failed with error %" PRIu32
"!",
278 Stream_SetPosition(s, 0);
283 Stream_Free(s, TRUE);
286 if (error && context->rdpcontext)
287 setChannelError(context->rdpcontext, error,
"encomsp_server_thread reported an error");
298 static UINT encomsp_server_start(EncomspServerContext* context)
300 context->priv->ChannelHandle =
301 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, ENCOMSP_SVC_CHANNEL_NAME);
303 if (!context->priv->ChannelHandle)
304 return CHANNEL_RC_BAD_CHANNEL;
306 if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
308 WLog_ERR(TAG,
"CreateEvent failed!");
309 return ERROR_INTERNAL_ERROR;
312 if (!(context->priv->Thread =
313 CreateThread(NULL, 0, encomsp_server_thread, (
void*)context, 0, NULL)))
315 WLog_ERR(TAG,
"CreateThread failed!");
316 (void)CloseHandle(context->priv->StopEvent);
317 context->priv->StopEvent = NULL;
318 return ERROR_INTERNAL_ERROR;
321 return CHANNEL_RC_OK;
329 static UINT encomsp_server_stop(EncomspServerContext* context)
331 UINT error = CHANNEL_RC_OK;
332 (void)SetEvent(context->priv->StopEvent);
334 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
336 error = GetLastError();
337 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
341 (void)CloseHandle(context->priv->Thread);
342 (void)CloseHandle(context->priv->StopEvent);
346 EncomspServerContext* encomsp_server_context_new(HANDLE vcm)
348 EncomspServerContext* context = NULL;
349 context = (EncomspServerContext*)calloc(1,
sizeof(EncomspServerContext));
354 context->Start = encomsp_server_start;
355 context->Stop = encomsp_server_stop;
356 context->priv = (EncomspServerPrivate*)calloc(1,
sizeof(EncomspServerPrivate));
360 WLog_ERR(TAG,
"calloc failed!");
369 void encomsp_server_context_free(EncomspServerContext* context)
373 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
374 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);