2014-07-15 07:42:12 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Echo Virtual Channel Extension
|
|
|
|
*
|
|
|
|
* Copyright 2014 Vic Lee
|
2015-06-08 15:35:27 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2014-07-15 07:42:12 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
#include <winpr/stream.h>
|
2014-07-23 21:08:06 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2014-07-15 07:42:12 +04:00
|
|
|
|
|
|
|
#include <freerdp/server/echo.h>
|
2015-06-08 15:35:27 +03:00
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
|
|
|
#define TAG CHANNELS_TAG("echo.server")
|
2014-07-15 07:42:12 +04:00
|
|
|
|
|
|
|
typedef struct _echo_server
|
|
|
|
{
|
|
|
|
echo_server_context context;
|
|
|
|
|
|
|
|
BOOL opened;
|
|
|
|
|
|
|
|
HANDLE stopEvent;
|
|
|
|
|
|
|
|
HANDLE thread;
|
|
|
|
void* echo_channel;
|
|
|
|
|
|
|
|
DWORD SessionId;
|
|
|
|
|
|
|
|
} echo_server;
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT echo_server_open_channel(echo_server* echo)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
|
|
|
DWORD Error;
|
|
|
|
HANDLE hEvent;
|
|
|
|
DWORD StartTick;
|
|
|
|
DWORD BytesReturned = 0;
|
|
|
|
PULONG pSessionId = NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (WTSQuerySessionInformationA(echo->context.vcm, WTS_CURRENT_SESSION, WTSSessionId,
|
|
|
|
(LPSTR*)&pSessionId, &BytesReturned) == FALSE)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2015-06-08 15:35:27 +03:00
|
|
|
WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
2015-06-08 15:35:27 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
echo->SessionId = (DWORD)*pSessionId;
|
2014-07-15 07:42:12 +04:00
|
|
|
WTSFreeMemory(pSessionId);
|
|
|
|
hEvent = WTSVirtualChannelManagerGetEventHandle(echo->context.vcm);
|
|
|
|
StartTick = GetTickCount();
|
|
|
|
|
|
|
|
while (echo->echo_channel == NULL)
|
|
|
|
{
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED)
|
2016-09-26 13:12:37 +03:00
|
|
|
{
|
|
|
|
Error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", Error);
|
2016-09-26 13:12:37 +03:00
|
|
|
return Error;
|
|
|
|
}
|
2014-07-15 07:42:12 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
echo->echo_channel =
|
|
|
|
WTSVirtualChannelOpenEx(echo->SessionId, "ECHO", WTS_CHANNEL_OPTION_DYNAMIC);
|
2014-07-15 07:42:12 +04:00
|
|
|
|
|
|
|
if (echo->echo_channel)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Error = GetLastError();
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-15 07:42:12 +04:00
|
|
|
if (Error == ERROR_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (GetTickCount() - StartTick > 5000)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
return echo->echo_channel ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI echo_server_thread_func(LPVOID arg)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
void* buffer;
|
|
|
|
DWORD nCount;
|
|
|
|
HANDLE events[8];
|
|
|
|
BOOL ready = FALSE;
|
|
|
|
HANDLE ChannelEvent;
|
|
|
|
DWORD BytesReturned = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
echo_server* echo = (echo_server*)arg;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2016-03-03 18:21:12 +03:00
|
|
|
DWORD status;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if ((error = echo_server_open_channel(echo)))
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2016-03-03 18:21:12 +03:00
|
|
|
UINT error2 = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "echo_server_open_channel failed with error %" PRIu32 "!", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
IFCALLRET(echo->context.OpenResult, error2, &echo->context,
|
|
|
|
ECHO_SERVER_OPEN_RESULT_NOTSUPPORTED);
|
|
|
|
|
2016-03-03 18:21:12 +03:00
|
|
|
if (error2)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "echo server's OpenResult callback failed with error %" PRIu32 "",
|
2016-09-26 13:12:37 +03:00
|
|
|
error2);
|
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
goto out;
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = NULL;
|
|
|
|
BytesReturned = 0;
|
|
|
|
ChannelEvent = NULL;
|
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (WTSVirtualChannelQuery(echo->echo_channel, WTSVirtualEventHandle, &buffer,
|
|
|
|
&BytesReturned) == TRUE)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
|
|
|
if (BytesReturned == sizeof(HANDLE))
|
|
|
|
CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
|
|
|
|
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nCount = 0;
|
|
|
|
events[nCount++] = echo->stopEvent;
|
|
|
|
events[nCount++] = ChannelEvent;
|
|
|
|
|
|
|
|
/* Wait for the client to confirm that the Graphics Pipeline dynamic channel is ready */
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2016-03-03 18:21:12 +03:00
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, 100);
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-03-03 18:21:12 +03:00
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
|
2016-03-03 18:21:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
|
|
|
if (status == WAIT_OBJECT_0)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
IFCALLRET(echo->context.OpenResult, error, &echo->context,
|
|
|
|
ECHO_SERVER_OPEN_RESULT_CLOSED);
|
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if (error)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "OpenResult failed with error %" PRIu32 "!", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-15 07:42:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (WTSVirtualChannelQuery(echo->echo_channel, WTSVirtualChannelReady, &buffer,
|
|
|
|
&BytesReturned) == FALSE)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
IFCALLRET(echo->context.OpenResult, error, &echo->context,
|
|
|
|
ECHO_SERVER_OPEN_RESULT_ERROR);
|
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if (error)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "OpenResult failed with error %" PRIu32 "!", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-15 07:42:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
ready = *((BOOL*)buffer);
|
2014-07-15 07:42:12 +04:00
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
|
|
|
|
if (ready)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
IFCALLRET(echo->context.OpenResult, error, &echo->context, ECHO_SERVER_OPEN_RESULT_OK);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if (error)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "OpenResult failed with error %" PRIu32 "!", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-15 07:42:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s = Stream_New(NULL, 4096);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
WTSVirtualChannelClose(echo->echo_channel);
|
2018-03-07 16:47:06 +03:00
|
|
|
ExitThread(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
2015-06-08 15:35:27 +03:00
|
|
|
}
|
2014-07-15 07:42:12 +04:00
|
|
|
|
|
|
|
while (ready)
|
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (status == WAIT_OBJECT_0)
|
2014-07-15 07:42:12 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
WTSVirtualChannelRead(echo->echo_channel, 0, NULL, 0, &BytesReturned);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-15 07:42:12 +04:00
|
|
|
if (BytesReturned < 1)
|
|
|
|
continue;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (WTSVirtualChannelRead(echo->echo_channel, 0, (PCHAR)Stream_Buffer(s),
|
|
|
|
(ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2015-06-08 15:35:27 +03:00
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2014-07-15 07:42:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
IFCALLRET(echo->context.Response, error, &echo->context, (BYTE*)Stream_Buffer(s),
|
|
|
|
BytesReturned);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if (error)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "Response failed with error %" PRIu32 "!", error);
|
2015-06-08 15:35:27 +03:00
|
|
|
break;
|
|
|
|
}
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-15 07:42:12 +04:00
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
WTSVirtualChannelClose(echo->echo_channel);
|
|
|
|
echo->echo_channel = NULL;
|
2015-07-15 10:50:35 +03:00
|
|
|
out:
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && echo->context.rdpcontext)
|
2016-09-26 13:12:37 +03:00
|
|
|
setChannelError(echo->context.rdpcontext, error,
|
|
|
|
"echo_server_thread_func reported an error");
|
2015-07-15 10:50:35 +03:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
ExitThread(error);
|
|
|
|
return error;
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT echo_server_open(echo_server_context* context)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
echo_server* echo = (echo_server*)context;
|
2014-07-15 07:42:12 +04:00
|
|
|
|
|
|
|
if (echo->thread == NULL)
|
|
|
|
{
|
2015-06-08 15:35:27 +03:00
|
|
|
if (!(echo->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(echo->thread = CreateThread(NULL, 0, echo_server_thread_func, (void*)echo, 0, NULL)))
|
2015-06-08 15:35:27 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
CloseHandle(echo->stopEvent);
|
|
|
|
echo->stopEvent = NULL;
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT echo_server_close(echo_server_context* context)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2019-11-06 17:24:51 +03:00
|
|
|
echo_server* echo = (echo_server*)context;
|
2014-07-15 07:42:12 +04:00
|
|
|
|
|
|
|
if (echo->thread)
|
|
|
|
{
|
|
|
|
SetEvent(echo->stopEvent);
|
2015-07-30 16:49:21 +03:00
|
|
|
|
|
|
|
if (WaitForSingleObject(echo->thread, INFINITE) == WAIT_FAILED)
|
2016-09-26 13:12:37 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
return error;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2014-07-15 07:42:12 +04:00
|
|
|
CloseHandle(echo->thread);
|
|
|
|
CloseHandle(echo->stopEvent);
|
|
|
|
echo->thread = NULL;
|
|
|
|
echo->stopEvent = NULL;
|
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
return error;
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL echo_server_request(echo_server_context* context, const BYTE* buffer, UINT32 length)
|
2014-07-15 07:42:12 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
echo_server* echo = (echo_server*)context;
|
|
|
|
return WTSVirtualChannelWrite(echo->echo_channel, (PCHAR)buffer, length, NULL);
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
echo_server_context* echo_server_context_new(HANDLE vcm)
|
|
|
|
{
|
|
|
|
echo_server* echo;
|
2019-11-06 17:24:51 +03:00
|
|
|
echo = (echo_server*)calloc(1, sizeof(echo_server));
|
2014-07-15 07:42:12 +04:00
|
|
|
|
2015-06-08 15:35:27 +03:00
|
|
|
if (echo)
|
|
|
|
{
|
|
|
|
echo->context.vcm = vcm;
|
|
|
|
echo->context.Open = echo_server_open;
|
|
|
|
echo->context.Close = echo_server_close;
|
|
|
|
echo->context.Request = echo_server_request;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
2014-07-15 07:42:12 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return (echo_server_context*)echo;
|
2014-07-15 07:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void echo_server_context_free(echo_server_context* context)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
echo_server* echo = (echo_server*)context;
|
2014-07-15 07:42:12 +04:00
|
|
|
echo_server_close(context);
|
|
|
|
free(echo);
|
|
|
|
}
|