2013-08-19 05:52:55 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Dynamic Virtual Channel Extension
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2015-05-29 15:02:31 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2013-08-19 05:52:55 +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 <winpr/crt.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/stream.h>
|
2015-05-29 15:02:31 +03:00
|
|
|
#include <freerdp/channels/log.h>
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
#include "drdynvc_main.h"
|
|
|
|
|
2015-05-29 15:02:31 +03:00
|
|
|
#define TAG CHANNELS_TAG("drdynvc.server")
|
|
|
|
|
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
static void* drdynvc_server_thread(void* arg)
|
|
|
|
{
|
2015-05-29 15:02:31 +03:00
|
|
|
#if 0
|
2013-08-19 05:52:55 +04:00
|
|
|
wStream* s;
|
|
|
|
DWORD status;
|
|
|
|
DWORD nCount;
|
|
|
|
void* buffer;
|
|
|
|
HANDLE events[8];
|
|
|
|
HANDLE ChannelEvent;
|
2013-08-21 02:06:19 +04:00
|
|
|
DWORD BytesReturned;
|
2013-08-19 05:52:55 +04:00
|
|
|
DrdynvcServerContext* context;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = ERROR_INTERNAL_ERROR;
|
2013-08-19 05:52:55 +04:00
|
|
|
context = (DrdynvcServerContext*) arg;
|
|
|
|
buffer = NULL;
|
|
|
|
BytesReturned = 0;
|
|
|
|
ChannelEvent = NULL;
|
2016-11-15 19:41:01 +03:00
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
s = Stream_New(NULL, 4096);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-05-29 15:02:31 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
ExitThread((DWORD) CHANNEL_RC_NO_MEMORY);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle,
|
|
|
|
&buffer, &BytesReturned) == TRUE)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
if (BytesReturned == sizeof(HANDLE))
|
|
|
|
CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
|
|
|
|
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nCount = 0;
|
|
|
|
events[nCount++] = ChannelEvent;
|
|
|
|
events[nCount++] = context->priv->StopEvent;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
2015-05-29 15:02:31 +03:00
|
|
|
error = CHANNEL_RC_OK;
|
2013-08-19 05:52:55 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0, NULL, 0,
|
|
|
|
&BytesReturned))
|
2015-05-29 15:02:31 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
|
|
|
|
break;
|
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
if (BytesReturned < 1)
|
|
|
|
continue;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-03-26 19:09:47 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
|
2015-05-29 15:02:31 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
2015-03-26 19:09:47 +03:00
|
|
|
break;
|
2015-05-29 15:02:31 +03:00
|
|
|
}
|
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0,
|
2016-09-26 13:12:37 +03:00
|
|
|
(PCHAR) Stream_Buffer(s), Stream_Capacity(s), &BytesReturned))
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2015-05-29 15:02:31 +03:00
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
|
2014-07-14 16:00:38 +04:00
|
|
|
break;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
2015-05-29 15:02:31 +03:00
|
|
|
ExitThread((DWORD) error);
|
|
|
|
#endif
|
2015-07-15 10:50:35 +03:00
|
|
|
// WTF ... this code only reads data into the stream until there is no more memory
|
2015-05-29 15:02:31 +03:00
|
|
|
ExitThread(0);
|
2013-08-19 05:52:55 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT drdynvc_server_start(DrdynvcServerContext* context)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
context->priv->ChannelHandle = WTSVirtualChannelOpen(context->vcm,
|
|
|
|
WTS_CURRENT_SESSION, "drdynvc");
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
if (!context->priv->ChannelHandle)
|
2015-05-29 15:02:31 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelOpen failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2015-05-29 15:02:31 +03:00
|
|
|
if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-05-29 15:02:31 +03:00
|
|
|
if (!(context->priv->Thread = CreateThread(NULL, 0,
|
2016-09-26 13:12:37 +03:00
|
|
|
(LPTHREAD_START_ROUTINE) drdynvc_server_thread, (void*) context, 0, NULL)))
|
2015-05-29 15:02:31 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
CloseHandle(context->priv->StopEvent);
|
|
|
|
context->priv->StopEvent = NULL;
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2015-05-29 15:02:31 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT drdynvc_server_stop(DrdynvcServerContext* context)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
UINT error;
|
2013-08-19 05:52:55 +04:00
|
|
|
SetEvent(context->priv->StopEvent);
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
|
2016-09-26 13:12:37 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
CloseHandle(context->priv->Thread);
|
2015-05-29 15:02:31 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 22:30:04 +04:00
|
|
|
DrdynvcServerContext* drdynvc_server_context_new(HANDLE vcm)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
DrdynvcServerContext* context;
|
2014-02-27 22:30:04 +04:00
|
|
|
context = (DrdynvcServerContext*) calloc(1, sizeof(DrdynvcServerContext));
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
context->vcm = vcm;
|
|
|
|
context->Start = drdynvc_server_start;
|
|
|
|
context->Stop = drdynvc_server_stop;
|
2014-02-27 22:30:04 +04:00
|
|
|
context->priv = (DrdynvcServerPrivate*) calloc(1, sizeof(DrdynvcServerPrivate));
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-05-29 15:02:31 +03:00
|
|
|
if (!context->priv)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drdynvc_server_context_free(DrdynvcServerContext* context)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(context->priv);
|
2013-08-19 05:52:55 +04:00
|
|
|
free(context);
|
|
|
|
}
|
|
|
|
}
|