Blame channels/encomsp/server/encomsp_main.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Multiparty Virtual Channel
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/print.h>
Packit 1fb8d4
#include <winpr/stream.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/channels/log.h>
Packit 1fb8d4
Packit 1fb8d4
#include "encomsp_main.h"
Packit 1fb8d4
Packit 1fb8d4
#define TAG CHANNELS_TAG("encomsp.server")
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT encomsp_read_header(wStream* s, ENCOMSP_ORDER_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < ENCOMSP_ORDER_HEADER_SIZE)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, header->Type);   /* Type (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT16(s, header->Length); /* Length (2 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#if 0
Packit 1fb8d4
Packit 1fb8d4
static int encomsp_write_header(wStream* s, ENCOMSP_ORDER_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	Stream_Write_UINT16(s, header->Type); /* Type (2 bytes) */
Packit 1fb8d4
	Stream_Write_UINT16(s, header->Length); /* Length (2 bytes) */
Packit 1fb8d4
	return 1;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int encomsp_read_unicode_string(wStream* s, ENCOMSP_UNICODE_STRING* str)
Packit 1fb8d4
{
Packit 1fb8d4
	ZeroMemory(str, sizeof(ENCOMSP_UNICODE_STRING));
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 2)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT16(s, str->cchString); /* cchString (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (str->cchString > 1024)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < (str->cchString * 2))
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read(s, &(str->wString), (str->cchString * 2)); /* String (variable) */
Packit 1fb8d4
	return 1;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT encomsp_recv_change_participant_control_level_pdu(EncomspServerContext* context,
Packit Service 5a9772
                                                              wStream* s,
Packit Service 5a9772
                                                              ENCOMSP_ORDER_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	int beg, end;
Packit 1fb8d4
	ENCOMSP_CHANGE_PARTICIPANT_CONTROL_LEVEL_PDU pdu;
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit Service 5a9772
	beg = ((int)Stream_GetPosition(s)) - ENCOMSP_ORDER_HEADER_SIZE;
Packit 1fb8d4
	CopyMemory(&pdu, header, sizeof(ENCOMSP_ORDER_HEADER));
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 6)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Not enough data!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, pdu.Flags);         /* Flags (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, pdu.ParticipantId); /* ParticipantId (4 bytes) */
Packit Service 5a9772
	end = (int)Stream_GetPosition(s);
Packit 1fb8d4
Packit 1fb8d4
	if ((beg + header->Length) < end)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Not enough data!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((beg + header->Length) > end)
Packit 1fb8d4
	{
Packit Service 5a9772
		if (Stream_GetRemainingLength(s) < (size_t)((beg + header->Length) - end))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "Not enough data!");
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_SetPosition(s, (beg + header->Length));
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	IFCALLRET(context->ChangeParticipantControlLevel, error, context, &pdu);
Packit 1fb8d4
Packit 1fb8d4
	if (error)
Packit Service 5a9772
		WLog_ERR(TAG, "context->ChangeParticipantControlLevel failed with error %" PRIu32 "",
Packit 1fb8d4
		         error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT encomsp_server_receive_pdu(EncomspServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	ENCOMSP_ORDER_HEADER header;
Packit 1fb8d4
Packit 1fb8d4
	while (Stream_GetRemainingLength(s) > 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((error = encomsp_read_header(s, &header)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "encomsp_read_header failed with error %" PRIu32 "!", error);
Packit 1fb8d4
			return error;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		WLog_INFO(TAG, "EncomspReceive: Type: %" PRIu16 " Length: %" PRIu16 "", header.Type,
Packit 1fb8d4
		          header.Length);
Packit 1fb8d4
Packit 1fb8d4
		switch (header.Type)
Packit 1fb8d4
		{
Packit 1fb8d4
			case ODTYPE_PARTICIPANT_CTRL_CHANGED:
Packit Service 5a9772
				if ((error =
Packit Service 5a9772
				         encomsp_recv_change_participant_control_level_pdu(context, s, &header)))
Packit 1fb8d4
				{
Packit 1fb8d4
					WLog_ERR(TAG,
Packit Service 5a9772
					         "encomsp_recv_change_participant_control_level_pdu failed with error "
Packit Service 5a9772
					         "%" PRIu32 "!",
Packit 1fb8d4
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit Service 5a9772
				WLog_ERR(TAG, "header.Type unknown %" PRIu16 "!", header.Type);
Packit 1fb8d4
				return ERROR_INVALID_DATA;
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static DWORD WINAPI encomsp_server_thread(LPVOID arg)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	DWORD nCount;
Packit 1fb8d4
	void* buffer;
Packit 1fb8d4
	HANDLE events[8];
Packit 1fb8d4
	HANDLE ChannelEvent;
Packit 1fb8d4
	DWORD BytesReturned;
Packit 1fb8d4
	ENCOMSP_ORDER_HEADER* header;
Packit 1fb8d4
	EncomspServerContext* context;
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	DWORD status;
Packit Service 5a9772
	context = (EncomspServerContext*)arg;
Packit 1fb8d4
Packit 1fb8d4
	buffer = NULL;
Packit 1fb8d4
	BytesReturned = 0;
Packit 1fb8d4
	ChannelEvent = NULL;
Packit 1fb8d4
	s = Stream_New(NULL, 4096);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
Packit Service 5a9772
	                           &BytesReturned) == TRUE)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (BytesReturned == sizeof(HANDLE))
Packit 1fb8d4
			CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
Packit 1fb8d4
Packit 1fb8d4
		WTSFreeMemory(buffer);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	nCount = 0;
Packit 1fb8d4
	events[nCount++] = ChannelEvent;
Packit 1fb8d4
	events[nCount++] = context->priv->StopEvent;
Packit 1fb8d4
Packit 1fb8d4
	while (1)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			error = GetLastError();
Packit Service 5a9772
			WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		status = WaitForSingleObject(context->priv->StopEvent, 0);
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			error = GetLastError();
Packit Service 5a9772
			WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_OBJECT_0)
Packit 1fb8d4
		{
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		WTSVirtualChannelRead(context->priv->ChannelHandle, 0, NULL, 0, &BytesReturned);
Packit 1fb8d4
Packit 1fb8d4
		if (BytesReturned < 1)
Packit 1fb8d4
			continue;
Packit 1fb8d4
Packit 1fb8d4
		if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
			error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
		                           Stream_Capacity(s), &BytesReturned))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
Packit 1fb8d4
			error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (Stream_GetPosition(s) >= ENCOMSP_ORDER_HEADER_SIZE)
Packit 1fb8d4
		{
Packit Service 5a9772
			header = (ENCOMSP_ORDER_HEADER*)Stream_Buffer(s);
Packit 1fb8d4
Packit 1fb8d4
			if (header->Length >= Stream_GetPosition(s))
Packit 1fb8d4
			{
Packit 1fb8d4
				Stream_SealLength(s);
Packit 1fb8d4
				Stream_SetPosition(s, 0);
Packit 1fb8d4
Packit 1fb8d4
				if ((error = encomsp_server_receive_pdu(context, s)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG, "encomsp_server_receive_pdu failed with error %" PRIu32 "!",
Packit Service 5a9772
					         error);
Packit 1fb8d4
					break;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				Stream_SetPosition(s, 0);
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
out:
Packit 1fb8d4
Packit 1fb8d4
	if (error && context->rdpcontext)
Packit Service 5a9772
		setChannelError(context->rdpcontext, error, "encomsp_server_thread reported an error");
Packit 1fb8d4
Packit 1fb8d4
	ExitThread(error);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT encomsp_server_start(EncomspServerContext* context)
Packit 1fb8d4
{
Packit Service 5a9772
	context->priv->ChannelHandle =
Packit Service 5a9772
	    WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "encomsp");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->priv->ChannelHandle)
Packit 1fb8d4
		return CHANNEL_RC_BAD_CHANNEL;
Packit 1fb8d4
Packit 1fb8d4
	if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CreateEvent failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!(context->priv->Thread =
Packit Service 5a9772
	          CreateThread(NULL, 0, encomsp_server_thread, (void*)context, 0, NULL)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CreateThread failed!");
Packit 1fb8d4
		CloseHandle(context->priv->StopEvent);
Packit 1fb8d4
		context->priv->StopEvent = NULL;
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT encomsp_server_stop(EncomspServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	SetEvent(context->priv->StopEvent);
Packit 1fb8d4
Packit 1fb8d4
	if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
Packit 1fb8d4
	{
Packit 1fb8d4
		error = GetLastError();
Packit Service 5a9772
		WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
Packit 1fb8d4
		return error;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	CloseHandle(context->priv->Thread);
Packit 1fb8d4
	CloseHandle(context->priv->StopEvent);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
EncomspServerContext* encomsp_server_context_new(HANDLE vcm)
Packit 1fb8d4
{
Packit 1fb8d4
	EncomspServerContext* context;
Packit Service 5a9772
	context = (EncomspServerContext*)calloc(1, sizeof(EncomspServerContext));
Packit 1fb8d4
Packit 1fb8d4
	if (context)
Packit 1fb8d4
	{
Packit 1fb8d4
		context->vcm = vcm;
Packit 1fb8d4
		context->Start = encomsp_server_start;
Packit 1fb8d4
		context->Stop = encomsp_server_stop;
Packit Service 5a9772
		context->priv = (EncomspServerPrivate*)calloc(1, sizeof(EncomspServerPrivate));
Packit 1fb8d4
Packit 1fb8d4
		if (!context->priv)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
			free(context);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return context;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void encomsp_server_context_free(EncomspServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	if (context)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
Packit 1fb8d4
			WTSVirtualChannelClose(context->priv->ChannelHandle);
Packit Service 5a9772
Packit 1fb8d4
		free(context->priv);
Packit 1fb8d4
		free(context);
Packit 1fb8d4
	}
Packit 1fb8d4
}