Milestone 5: deliver embedded RDP sessions and lifecycle hardening
This commit is contained in:
26
third_party/FreeRDP/channels/location/CMakeLists.txt
vendored
Normal file
26
third_party/FreeRDP/channels/location/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
# FreeRDP: A Remote Desktop Protocol Implementation
|
||||
# FreeRDP cmake build script
|
||||
#
|
||||
# Copyright 2023 Pascal Nowack <Pascal.Nowack@gmx.de>
|
||||
#
|
||||
# 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.
|
||||
|
||||
define_channel("location")
|
||||
|
||||
if(WITH_CLIENT_CHANNELS)
|
||||
add_channel_client(${MODULE_PREFIX} ${CHANNEL_NAME})
|
||||
endif()
|
||||
|
||||
if(WITH_SERVER_CHANNELS)
|
||||
add_channel_server(${MODULE_PREFIX} ${CHANNEL_NAME})
|
||||
endif()
|
||||
20
third_party/FreeRDP/channels/location/ChannelOptions.cmake
vendored
Normal file
20
third_party/FreeRDP/channels/location/ChannelOptions.cmake
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
set(OPTION_DEFAULT ON)
|
||||
set(OPTION_CLIENT_DEFAULT ON)
|
||||
set(OPTION_SERVER_DEFAULT ON)
|
||||
|
||||
define_channel_options(
|
||||
NAME
|
||||
"location"
|
||||
TYPE
|
||||
"dynamic"
|
||||
DESCRIPTION
|
||||
"Location Virtual Channel Extension"
|
||||
SPECIFICATIONS
|
||||
"[MS-RDPEL]"
|
||||
DEFAULT
|
||||
${OPTION_DEFAULT}
|
||||
CLIENT_DEFAULT
|
||||
${OPTION_CLIENT_DEFAULT}
|
||||
SERVER_DEFAULT
|
||||
${OPTION_SERVER_DEFAULT}
|
||||
)
|
||||
26
third_party/FreeRDP/channels/location/client/CMakeLists.txt
vendored
Normal file
26
third_party/FreeRDP/channels/location/client/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
# FreeRDP: A Remote Desktop Protocol Implementation
|
||||
# FreeRDP cmake build script
|
||||
#
|
||||
# Copyright 2024 Armin Novak <anovak@thincast.com>
|
||||
# Copyright 2024 Thincast Technologies GmbH
|
||||
#
|
||||
# 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.
|
||||
|
||||
define_channel_client("location")
|
||||
|
||||
set(${MODULE_PREFIX}_SRCS location_main.c)
|
||||
|
||||
set(${MODULE_PREFIX}_LIBS winpr)
|
||||
include_directories(..)
|
||||
|
||||
add_channel_client_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} TRUE "DVCPluginEntry")
|
||||
492
third_party/FreeRDP/channels/location/client/location_main.c
vendored
Normal file
492
third_party/FreeRDP/channels/location/client/location_main.c
vendored
Normal file
@@ -0,0 +1,492 @@
|
||||
/**
|
||||
* FreeRDP: A Remote Desktop Protocol Implementation
|
||||
* Location Virtual Channel Extension
|
||||
*
|
||||
* Copyright 2024 Armin Novak <anovak@thincast.com>
|
||||
* Copyright 2024 Thincast Technologies GmbH
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <freerdp/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <winpr/crt.h>
|
||||
#include <winpr/assert.h>
|
||||
#include <winpr/cast.h>
|
||||
#include <winpr/stream.h>
|
||||
|
||||
#include <freerdp/client/channels.h>
|
||||
#include <freerdp/channels/log.h>
|
||||
#include <freerdp/channels/location.h>
|
||||
#include <freerdp/client/location.h>
|
||||
#include <freerdp/utils/encoded_types.h>
|
||||
|
||||
#define TAG CHANNELS_TAG("location.client")
|
||||
|
||||
/* implement [MS-RDPEL]
|
||||
* https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpel/4397a0af-c821-4b75-9068-476fb579c327
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
GENERIC_DYNVC_PLUGIN baseDynPlugin;
|
||||
LocationClientContext context;
|
||||
} LOCATION_PLUGIN;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GENERIC_CHANNEL_CALLBACK baseCb;
|
||||
UINT32 serverVersion;
|
||||
UINT32 clientVersion;
|
||||
UINT32 serverFlags;
|
||||
UINT32 clientFlags;
|
||||
} LOCATION_CALLBACK;
|
||||
|
||||
static BOOL location_read_header(wLog* log, wStream* s, UINT16* ppduType, UINT32* ppduLength)
|
||||
{
|
||||
WINPR_ASSERT(log);
|
||||
WINPR_ASSERT(s);
|
||||
WINPR_ASSERT(ppduType);
|
||||
WINPR_ASSERT(ppduLength);
|
||||
|
||||
if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 6))
|
||||
return FALSE;
|
||||
Stream_Read_UINT16(s, *ppduType);
|
||||
Stream_Read_UINT32(s, *ppduLength);
|
||||
if (*ppduLength < 6)
|
||||
{
|
||||
WLog_Print(log, WLOG_ERROR,
|
||||
"RDPLOCATION_HEADER::pduLengh=%" PRIu16 " < sizeof(RDPLOCATION_HEADER)[6]",
|
||||
*ppduLength);
|
||||
return FALSE;
|
||||
}
|
||||
return Stream_CheckAndLogRequiredLengthWLog(log, s, *ppduLength - 6ull);
|
||||
}
|
||||
|
||||
static BOOL location_write_header(wStream* s, UINT16 pduType, UINT32 pduLength)
|
||||
{
|
||||
if (!Stream_EnsureRemainingCapacity(s, 6))
|
||||
return FALSE;
|
||||
Stream_Write_UINT16(s, pduType);
|
||||
Stream_Write_UINT32(s, pduLength + 6);
|
||||
return Stream_EnsureRemainingCapacity(s, pduLength);
|
||||
}
|
||||
|
||||
static BOOL location_read_server_ready_pdu(LOCATION_CALLBACK* callback, wStream* s, UINT32 pduSize)
|
||||
{
|
||||
if (pduSize < 6 + 4)
|
||||
return FALSE; // Short message
|
||||
|
||||
Stream_Read_UINT32(s, callback->serverVersion);
|
||||
if (pduSize >= 6 + 4 + 4)
|
||||
Stream_Read_UINT32(s, callback->serverFlags);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static UINT location_channel_send(IWTSVirtualChannel* channel, wStream* s)
|
||||
{
|
||||
const size_t len = Stream_GetPosition(s);
|
||||
if (len > UINT32_MAX)
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
|
||||
Stream_SetPosition(s, 2);
|
||||
Stream_Write_UINT32(s, (UINT32)len);
|
||||
|
||||
WINPR_ASSERT(channel);
|
||||
WINPR_ASSERT(channel->Write);
|
||||
return channel->Write(channel, (UINT32)len, Stream_Buffer(s), nullptr);
|
||||
}
|
||||
|
||||
static UINT location_send_client_ready_pdu(const LOCATION_CALLBACK* callback)
|
||||
{
|
||||
wStream sbuffer = WINPR_C_ARRAY_INIT;
|
||||
BYTE buffer[32] = WINPR_C_ARRAY_INIT;
|
||||
wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
|
||||
WINPR_ASSERT(s);
|
||||
|
||||
if (!location_write_header(s, PDUTYPE_CLIENT_READY, 8))
|
||||
return ERROR_OUTOFMEMORY;
|
||||
|
||||
Stream_Write_UINT32(s, callback->clientVersion);
|
||||
Stream_Write_UINT32(s, callback->clientFlags);
|
||||
return location_channel_send(callback->baseCb.channel, s);
|
||||
}
|
||||
|
||||
static const char* location_version_str(UINT32 version, char* buffer, size_t size)
|
||||
{
|
||||
const char* str = nullptr;
|
||||
switch (version)
|
||||
{
|
||||
case RDPLOCATION_PROTOCOL_VERSION_100:
|
||||
str = "RDPLOCATION_PROTOCOL_VERSION_100";
|
||||
break;
|
||||
case RDPLOCATION_PROTOCOL_VERSION_200:
|
||||
str = "RDPLOCATION_PROTOCOL_VERSION_200";
|
||||
break;
|
||||
default:
|
||||
str = "RDPLOCATION_PROTOCOL_VERSION_UNKNOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
(void)_snprintf(buffer, size, "%s [0x%08" PRIx32 "]", str, version);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function description
|
||||
*
|
||||
* @return 0 on success, otherwise a Win32 error code
|
||||
*/
|
||||
static UINT location_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
|
||||
{
|
||||
LOCATION_CALLBACK* callback = (LOCATION_CALLBACK*)pChannelCallback;
|
||||
|
||||
WINPR_ASSERT(callback);
|
||||
|
||||
LOCATION_PLUGIN* plugin = (LOCATION_PLUGIN*)callback->baseCb.plugin;
|
||||
WINPR_ASSERT(plugin);
|
||||
|
||||
UINT16 pduType = 0;
|
||||
UINT32 pduLength = 0;
|
||||
if (!location_read_header(plugin->baseDynPlugin.log, data, &pduType, &pduLength))
|
||||
return ERROR_INVALID_DATA;
|
||||
|
||||
switch (pduType)
|
||||
{
|
||||
case PDUTYPE_SERVER_READY:
|
||||
if (!location_read_server_ready_pdu(callback, data, pduLength))
|
||||
return ERROR_INVALID_DATA;
|
||||
|
||||
switch (callback->serverVersion)
|
||||
{
|
||||
case RDPLOCATION_PROTOCOL_VERSION_200:
|
||||
callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_200;
|
||||
break;
|
||||
case RDPLOCATION_PROTOCOL_VERSION_100:
|
||||
callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_100;
|
||||
break;
|
||||
default:
|
||||
callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_100;
|
||||
if (callback->serverVersion > RDPLOCATION_PROTOCOL_VERSION_200)
|
||||
callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_200;
|
||||
break;
|
||||
}
|
||||
|
||||
{
|
||||
char cbuffer[64] = WINPR_C_ARRAY_INIT;
|
||||
char sbuffer[64] = WINPR_C_ARRAY_INIT;
|
||||
WLog_Print(plugin->baseDynPlugin.log, WLOG_DEBUG,
|
||||
"Server version %s, client version %s",
|
||||
location_version_str(callback->serverVersion, sbuffer, sizeof(sbuffer)),
|
||||
location_version_str(callback->clientVersion, cbuffer, sizeof(cbuffer)));
|
||||
}
|
||||
|
||||
if (!plugin->context.LocationStart)
|
||||
{
|
||||
WLog_Print(plugin->baseDynPlugin.log, WLOG_WARN,
|
||||
"LocationStart=nullptr, no location data will be sent");
|
||||
return CHANNEL_RC_OK;
|
||||
}
|
||||
|
||||
{
|
||||
const UINT res =
|
||||
plugin->context.LocationStart(&plugin->context, callback->clientVersion, 0);
|
||||
if (res != CHANNEL_RC_OK)
|
||||
return res;
|
||||
}
|
||||
return location_send_client_ready_pdu(callback);
|
||||
default:
|
||||
WLog_WARN(TAG, "invalid pduType=%" PRIu16, pduType);
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
static UINT location_send_base_location3d(IWTSVirtualChannel* channel,
|
||||
const RDPLOCATION_BASE_LOCATION3D_PDU* pdu)
|
||||
{
|
||||
wStream sbuffer = WINPR_C_ARRAY_INIT;
|
||||
BYTE buffer[32] = WINPR_C_ARRAY_INIT;
|
||||
wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
|
||||
WINPR_ASSERT(s);
|
||||
WINPR_ASSERT(channel);
|
||||
WINPR_ASSERT(pdu);
|
||||
|
||||
if (pdu->source)
|
||||
WLog_DBG(TAG,
|
||||
"latitude=%lf, longitude=%lf, altitude=%" PRId32
|
||||
", speed=%lf, heading=%lf, haccuracy=%lf, source=%" PRIu8,
|
||||
pdu->latitude, pdu->longitude, pdu->altitude, pdu->speed ? *pdu->speed : FP_NAN,
|
||||
pdu->heading ? *pdu->heading : FP_NAN,
|
||||
pdu->horizontalAccuracy ? *pdu->horizontalAccuracy : FP_NAN, *pdu->source);
|
||||
else
|
||||
WLog_DBG(TAG, "latitude=%lf, longitude=%lf, altitude=%" PRId32, pdu->latitude,
|
||||
pdu->longitude, pdu->altitude);
|
||||
|
||||
if (!location_write_header(s, PDUTYPE_BASE_LOCATION3D, pdu->source ? 25 : 12))
|
||||
return ERROR_OUTOFMEMORY;
|
||||
|
||||
if (!freerdp_write_four_byte_float(s, pdu->latitude) ||
|
||||
!freerdp_write_four_byte_float(s, pdu->longitude) ||
|
||||
!freerdp_write_four_byte_signed_integer(s, pdu->altitude))
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
|
||||
if (pdu->source)
|
||||
{
|
||||
if (!freerdp_write_four_byte_float(s, *pdu->speed) ||
|
||||
!freerdp_write_four_byte_float(s, *pdu->heading) ||
|
||||
!freerdp_write_four_byte_float(s, *pdu->horizontalAccuracy))
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
|
||||
Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, *pdu->source));
|
||||
}
|
||||
|
||||
return location_channel_send(channel, s);
|
||||
}
|
||||
|
||||
static UINT location_send_location2d_delta(IWTSVirtualChannel* channel,
|
||||
const RDPLOCATION_LOCATION2D_DELTA_PDU* pdu)
|
||||
{
|
||||
wStream sbuffer = WINPR_C_ARRAY_INIT;
|
||||
BYTE buffer[32] = WINPR_C_ARRAY_INIT;
|
||||
wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
|
||||
WINPR_ASSERT(s);
|
||||
|
||||
WINPR_ASSERT(channel);
|
||||
WINPR_ASSERT(pdu);
|
||||
|
||||
const BOOL ext = pdu->speedDelta && pdu->headingDelta;
|
||||
|
||||
if (ext)
|
||||
WLog_DBG(TAG, "latitude=%lf, longitude=%lf, speed=%lf, heading=%lf", pdu->latitudeDelta,
|
||||
pdu->longitudeDelta, pdu->speedDelta ? *pdu->speedDelta : FP_NAN,
|
||||
pdu->headingDelta ? *pdu->headingDelta : FP_NAN);
|
||||
else
|
||||
WLog_DBG(TAG, "latitude=%lf, longitude=%lf", pdu->latitudeDelta, pdu->longitudeDelta);
|
||||
|
||||
if (!location_write_header(s, PDUTYPE_LOCATION2D_DELTA, ext ? 16 : 8))
|
||||
return ERROR_OUTOFMEMORY;
|
||||
|
||||
if (!freerdp_write_four_byte_float(s, pdu->latitudeDelta) ||
|
||||
!freerdp_write_four_byte_float(s, pdu->longitudeDelta))
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
|
||||
if (ext)
|
||||
{
|
||||
if (!freerdp_write_four_byte_float(s, *pdu->speedDelta) ||
|
||||
!freerdp_write_four_byte_float(s, *pdu->headingDelta))
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
return location_channel_send(channel, s);
|
||||
}
|
||||
|
||||
static UINT location_send_location3d_delta(IWTSVirtualChannel* channel,
|
||||
const RDPLOCATION_LOCATION3D_DELTA_PDU* pdu)
|
||||
{
|
||||
wStream sbuffer = WINPR_C_ARRAY_INIT;
|
||||
BYTE buffer[32] = WINPR_C_ARRAY_INIT;
|
||||
wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
|
||||
WINPR_ASSERT(s);
|
||||
|
||||
WINPR_ASSERT(channel);
|
||||
WINPR_ASSERT(pdu);
|
||||
|
||||
const BOOL ext = pdu->speedDelta && pdu->headingDelta;
|
||||
|
||||
if (ext)
|
||||
WLog_DBG(TAG, "latitude=%lf, longitude=%lf, altitude=%" PRId32 ", speed=%lf, heading=%lf",
|
||||
pdu->latitudeDelta, pdu->longitudeDelta, pdu->altitudeDelta,
|
||||
pdu->speedDelta ? *pdu->speedDelta : FP_NAN,
|
||||
pdu->headingDelta ? *pdu->headingDelta : FP_NAN);
|
||||
else
|
||||
WLog_DBG(TAG, "latitude=%lf, longitude=%lf, altitude=%" PRId32, pdu->latitudeDelta,
|
||||
pdu->longitudeDelta, pdu->altitudeDelta);
|
||||
|
||||
if (!location_write_header(s, PDUTYPE_LOCATION3D_DELTA, ext ? 20 : 12))
|
||||
return ERROR_OUTOFMEMORY;
|
||||
|
||||
if (!freerdp_write_four_byte_float(s, pdu->latitudeDelta) ||
|
||||
!freerdp_write_four_byte_float(s, pdu->longitudeDelta) ||
|
||||
!freerdp_write_four_byte_signed_integer(s, pdu->altitudeDelta))
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
|
||||
if (ext)
|
||||
{
|
||||
if (!freerdp_write_four_byte_float(s, *pdu->speedDelta) ||
|
||||
!freerdp_write_four_byte_float(s, *pdu->headingDelta))
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
return location_channel_send(channel, s);
|
||||
}
|
||||
|
||||
static UINT location_send(LocationClientContext* context, LOCATION_PDUTYPE type, size_t count, ...)
|
||||
{
|
||||
WINPR_ASSERT(context);
|
||||
|
||||
LOCATION_PLUGIN* loc = context->handle;
|
||||
WINPR_ASSERT(loc);
|
||||
|
||||
GENERIC_LISTENER_CALLBACK* cb = loc->baseDynPlugin.listener_callback;
|
||||
WINPR_ASSERT(cb);
|
||||
|
||||
IWTSVirtualChannel* channel = cb->channel;
|
||||
WINPR_ASSERT(channel);
|
||||
|
||||
const LOCATION_CALLBACK* callback =
|
||||
(const LOCATION_CALLBACK*)loc->baseDynPlugin.channel_callbacks;
|
||||
WINPR_ASSERT(callback);
|
||||
|
||||
UINT32 res = ERROR_INTERNAL_ERROR;
|
||||
va_list ap = WINPR_C_ARRAY_INIT;
|
||||
va_start(ap, count);
|
||||
switch (type)
|
||||
{
|
||||
case PDUTYPE_BASE_LOCATION3D:
|
||||
if ((count != 3) && (count != 7))
|
||||
res = ERROR_INVALID_PARAMETER;
|
||||
else
|
||||
{
|
||||
LOCATIONSOURCE source = LOCATIONSOURCE_IP;
|
||||
double speed = FP_NAN;
|
||||
double heading = FP_NAN;
|
||||
double horizontalAccuracy = FP_NAN;
|
||||
RDPLOCATION_BASE_LOCATION3D_PDU pdu = { .latitude = va_arg(ap, double),
|
||||
.longitude = va_arg(ap, double),
|
||||
.altitude = va_arg(ap, INT32),
|
||||
.speed = nullptr,
|
||||
.heading = nullptr,
|
||||
.horizontalAccuracy = nullptr,
|
||||
.source = nullptr };
|
||||
|
||||
if ((count > 3) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
|
||||
{
|
||||
speed = va_arg(ap, double);
|
||||
heading = va_arg(ap, double);
|
||||
horizontalAccuracy = va_arg(ap, double);
|
||||
source = WINPR_ASSERTING_INT_CAST(LOCATIONSOURCE, va_arg(ap, int));
|
||||
pdu.speed = &speed;
|
||||
pdu.heading = &heading;
|
||||
pdu.horizontalAccuracy = &horizontalAccuracy;
|
||||
pdu.source = &source;
|
||||
}
|
||||
res = location_send_base_location3d(channel, &pdu);
|
||||
}
|
||||
break;
|
||||
case PDUTYPE_LOCATION2D_DELTA:
|
||||
if ((count != 2) && (count != 4))
|
||||
res = ERROR_INVALID_PARAMETER;
|
||||
else
|
||||
{
|
||||
RDPLOCATION_LOCATION2D_DELTA_PDU pdu = { .latitudeDelta = va_arg(ap, double),
|
||||
.longitudeDelta = va_arg(ap, double),
|
||||
.speedDelta = nullptr,
|
||||
.headingDelta = nullptr };
|
||||
|
||||
double speedDelta = FP_NAN;
|
||||
double headingDelta = FP_NAN;
|
||||
if ((count > 2) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
|
||||
{
|
||||
speedDelta = va_arg(ap, double);
|
||||
headingDelta = va_arg(ap, double);
|
||||
pdu.speedDelta = &speedDelta;
|
||||
pdu.headingDelta = &headingDelta;
|
||||
}
|
||||
res = location_send_location2d_delta(channel, &pdu);
|
||||
}
|
||||
break;
|
||||
case PDUTYPE_LOCATION3D_DELTA:
|
||||
if ((count != 3) && (count != 5))
|
||||
res = ERROR_INVALID_PARAMETER;
|
||||
else
|
||||
{
|
||||
double speedDelta = FP_NAN;
|
||||
double headingDelta = FP_NAN;
|
||||
|
||||
RDPLOCATION_LOCATION3D_DELTA_PDU pdu = { .latitudeDelta = va_arg(ap, double),
|
||||
.longitudeDelta = va_arg(ap, double),
|
||||
.altitudeDelta = va_arg(ap, INT32),
|
||||
.speedDelta = nullptr,
|
||||
.headingDelta = nullptr };
|
||||
if ((count > 3) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
|
||||
{
|
||||
speedDelta = va_arg(ap, double);
|
||||
headingDelta = va_arg(ap, double);
|
||||
pdu.speedDelta = &speedDelta;
|
||||
pdu.headingDelta = &headingDelta;
|
||||
}
|
||||
res = location_send_location3d_delta(channel, &pdu);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
res = ERROR_INVALID_PARAMETER;
|
||||
break;
|
||||
}
|
||||
va_end(ap);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function description
|
||||
*
|
||||
* @return 0 on success, otherwise a Win32 error code
|
||||
*/
|
||||
static UINT location_on_close(IWTSVirtualChannelCallback* pChannelCallback)
|
||||
{
|
||||
UINT res = CHANNEL_RC_OK;
|
||||
GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
|
||||
|
||||
if (callback)
|
||||
{
|
||||
LOCATION_PLUGIN* plugin = (LOCATION_PLUGIN*)callback->plugin;
|
||||
WINPR_ASSERT(plugin);
|
||||
|
||||
res = IFCALLRESULT(CHANNEL_RC_OK, plugin->context.LocationStop, &plugin->context);
|
||||
}
|
||||
free(callback);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static UINT location_init(GENERIC_DYNVC_PLUGIN* plugin, WINPR_ATTR_UNUSED rdpContext* context,
|
||||
WINPR_ATTR_UNUSED rdpSettings* settings)
|
||||
{
|
||||
LOCATION_PLUGIN* loc = (LOCATION_PLUGIN*)plugin;
|
||||
|
||||
WINPR_ASSERT(loc);
|
||||
|
||||
loc->context.LocationSend = location_send;
|
||||
loc->context.handle = loc;
|
||||
plugin->iface.pInterface = &loc->context;
|
||||
return CHANNEL_RC_OK;
|
||||
}
|
||||
|
||||
static const IWTSVirtualChannelCallback location_callbacks = { location_on_data_received,
|
||||
nullptr, /* Open */
|
||||
location_on_close, nullptr };
|
||||
|
||||
/**
|
||||
* Function description
|
||||
*
|
||||
* @return 0 on success, otherwise a Win32 error code
|
||||
*/
|
||||
FREERDP_ENTRY_POINT(UINT VCAPITYPE location_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
|
||||
{
|
||||
return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, LOCATION_DVC_CHANNEL_NAME,
|
||||
sizeof(LOCATION_PLUGIN), sizeof(LOCATION_CALLBACK),
|
||||
&location_callbacks, location_init, nullptr);
|
||||
}
|
||||
24
third_party/FreeRDP/channels/location/server/CMakeLists.txt
vendored
Normal file
24
third_party/FreeRDP/channels/location/server/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# FreeRDP: A Remote Desktop Protocol Implementation
|
||||
# FreeRDP cmake build script
|
||||
#
|
||||
# Copyright 2023 Pascal Nowack <Pascal.Nowack@gmx.de>
|
||||
#
|
||||
# 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.
|
||||
|
||||
define_channel_server("location")
|
||||
|
||||
set(${MODULE_PREFIX}_SRCS location_main.c)
|
||||
|
||||
set(${MODULE_PREFIX}_LIBS freerdp)
|
||||
|
||||
add_channel_server_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} FALSE "DVCPluginEntry")
|
||||
689
third_party/FreeRDP/channels/location/server/location_main.c
vendored
Normal file
689
third_party/FreeRDP/channels/location/server/location_main.c
vendored
Normal file
@@ -0,0 +1,689 @@
|
||||
/**
|
||||
* FreeRDP: A Remote Desktop Protocol Implementation
|
||||
* Location Virtual Channel Extension
|
||||
*
|
||||
* Copyright 2023 Pascal Nowack <Pascal.Nowack@gmx.de>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <freerdp/config.h>
|
||||
|
||||
#include <freerdp/freerdp.h>
|
||||
#include <freerdp/channels/log.h>
|
||||
#include <freerdp/server/location.h>
|
||||
#include <freerdp/utils/encoded_types.h>
|
||||
|
||||
#define TAG CHANNELS_TAG("location.server")
|
||||
|
||||
typedef enum
|
||||
{
|
||||
LOCATION_INITIAL,
|
||||
LOCATION_OPENED,
|
||||
} eLocationChannelState;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
LocationServerContext context;
|
||||
|
||||
HANDLE stopEvent;
|
||||
|
||||
HANDLE thread;
|
||||
void* location_channel;
|
||||
|
||||
DWORD SessionId;
|
||||
|
||||
BOOL isOpened;
|
||||
BOOL externalThread;
|
||||
|
||||
/* Channel state */
|
||||
eLocationChannelState state;
|
||||
|
||||
wStream* buffer;
|
||||
} location_server;
|
||||
|
||||
static UINT location_server_initialize(LocationServerContext* context, BOOL externalThread)
|
||||
{
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
location_server* location = (location_server*)context;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
if (location->isOpened)
|
||||
{
|
||||
WLog_WARN(TAG, "Application error: Location channel already initialized, "
|
||||
"calling in this state is not possible!");
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
location->externalThread = externalThread;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_open_channel(location_server* location)
|
||||
{
|
||||
LocationServerContext* context = &location->context;
|
||||
DWORD Error = ERROR_SUCCESS;
|
||||
HANDLE hEvent = nullptr;
|
||||
DWORD BytesReturned = 0;
|
||||
PULONG pSessionId = nullptr;
|
||||
UINT32 channelId = 0;
|
||||
BOOL status = TRUE;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
if (WTSQuerySessionInformationA(location->context.vcm, WTS_CURRENT_SESSION, WTSSessionId,
|
||||
(LPSTR*)&pSessionId, &BytesReturned) == FALSE)
|
||||
{
|
||||
WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
location->SessionId = (DWORD)*pSessionId;
|
||||
WTSFreeMemory(pSessionId);
|
||||
hEvent = WTSVirtualChannelManagerGetEventHandle(location->context.vcm);
|
||||
|
||||
if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED)
|
||||
{
|
||||
Error = GetLastError();
|
||||
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", Error);
|
||||
return Error;
|
||||
}
|
||||
|
||||
location->location_channel = WTSVirtualChannelOpenEx(
|
||||
location->SessionId, LOCATION_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC);
|
||||
if (!location->location_channel)
|
||||
{
|
||||
Error = GetLastError();
|
||||
WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed with error %" PRIu32 "!", Error);
|
||||
return Error;
|
||||
}
|
||||
|
||||
channelId = WTSChannelGetIdByHandle(location->location_channel);
|
||||
|
||||
IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
|
||||
if (!status)
|
||||
{
|
||||
WLog_ERR(TAG, "context->ChannelIdAssigned failed!");
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
return Error;
|
||||
}
|
||||
|
||||
static UINT location_server_recv_client_ready(LocationServerContext* context, wStream* s,
|
||||
const RDPLOCATION_HEADER* header)
|
||||
{
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
|
||||
WINPR_ASSERT(context);
|
||||
WINPR_ASSERT(s);
|
||||
WINPR_ASSERT(header);
|
||||
|
||||
RDPLOCATION_CLIENT_READY_PDU pdu = { .header = *header,
|
||||
.protocolVersion = RDPLOCATION_PROTOCOL_VERSION_100,
|
||||
.flags = 0 };
|
||||
|
||||
if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
|
||||
return ERROR_NO_DATA;
|
||||
|
||||
{
|
||||
const UINT32 version = Stream_Get_UINT32(s);
|
||||
switch (version)
|
||||
{
|
||||
case RDPLOCATION_PROTOCOL_VERSION_100:
|
||||
pdu.protocolVersion = RDPLOCATION_PROTOCOL_VERSION_100;
|
||||
break;
|
||||
case RDPLOCATION_PROTOCOL_VERSION_200:
|
||||
pdu.protocolVersion = RDPLOCATION_PROTOCOL_VERSION_200;
|
||||
break;
|
||||
default:
|
||||
pdu.protocolVersion = RDPLOCATION_PROTOCOL_VERSION_200;
|
||||
WLog_WARN(TAG,
|
||||
"Received unsupported protocol version %" PRIu32
|
||||
", setting to highest supported %u",
|
||||
version, pdu.protocolVersion);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (Stream_GetRemainingLength(s) >= 4)
|
||||
Stream_Read_UINT32(s, pdu.flags);
|
||||
|
||||
IFCALLRET(context->ClientReady, error, context, &pdu);
|
||||
if (error)
|
||||
WLog_ERR(TAG, "context->ClientReady failed with error %" PRIu32 "", error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_recv_base_location3d(LocationServerContext* context, wStream* s,
|
||||
const RDPLOCATION_HEADER* header)
|
||||
{
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
double speed = 0.0;
|
||||
double heading = 0.0;
|
||||
double horizontalAccuracy = 0.0;
|
||||
LOCATIONSOURCE source = LOCATIONSOURCE_IP;
|
||||
|
||||
WINPR_ASSERT(context);
|
||||
WINPR_ASSERT(s);
|
||||
WINPR_ASSERT(header);
|
||||
|
||||
RDPLOCATION_BASE_LOCATION3D_PDU pdu = { .header = *header,
|
||||
.latitude = FP_NAN,
|
||||
.longitude = FP_NAN,
|
||||
.altitude = 0,
|
||||
.speed = nullptr,
|
||||
.heading = nullptr,
|
||||
.horizontalAccuracy = nullptr,
|
||||
.source = nullptr };
|
||||
|
||||
if (!freerdp_read_four_byte_float(s, &pdu.latitude) ||
|
||||
!freerdp_read_four_byte_float(s, &pdu.longitude) ||
|
||||
!freerdp_read_four_byte_signed_integer(s, &pdu.altitude))
|
||||
return FALSE;
|
||||
|
||||
if (Stream_GetRemainingLength(s) >= 1)
|
||||
{
|
||||
if (!freerdp_read_four_byte_float(s, &speed) ||
|
||||
!freerdp_read_four_byte_float(s, &heading) ||
|
||||
!freerdp_read_four_byte_float(s, &horizontalAccuracy) ||
|
||||
!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
||||
return FALSE;
|
||||
|
||||
{
|
||||
const UINT8 src = Stream_Get_UINT8(s);
|
||||
switch (src)
|
||||
{
|
||||
case LOCATIONSOURCE_IP:
|
||||
case LOCATIONSOURCE_WIFI:
|
||||
case LOCATIONSOURCE_CELL:
|
||||
case LOCATIONSOURCE_GNSS:
|
||||
break;
|
||||
default:
|
||||
WLog_ERR(TAG, "Invalid LOCATIONSOURCE value %" PRIu8 "", src);
|
||||
return FALSE;
|
||||
}
|
||||
source = (LOCATIONSOURCE)src;
|
||||
}
|
||||
|
||||
pdu.speed = &speed;
|
||||
pdu.heading = &heading;
|
||||
pdu.horizontalAccuracy = &horizontalAccuracy;
|
||||
pdu.source = &source;
|
||||
}
|
||||
|
||||
IFCALLRET(context->BaseLocation3D, error, context, &pdu);
|
||||
if (error)
|
||||
WLog_ERR(TAG, "context->BaseLocation3D failed with error %" PRIu32 "", error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_recv_location2d_delta(LocationServerContext* context, wStream* s,
|
||||
const RDPLOCATION_HEADER* header)
|
||||
{
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
double speedDelta = 0.0;
|
||||
double headingDelta = 0.0;
|
||||
|
||||
WINPR_ASSERT(context);
|
||||
WINPR_ASSERT(s);
|
||||
WINPR_ASSERT(header);
|
||||
|
||||
RDPLOCATION_LOCATION2D_DELTA_PDU pdu = { .header = *header,
|
||||
.latitudeDelta = FP_NAN,
|
||||
.longitudeDelta = FP_NAN,
|
||||
.speedDelta = nullptr,
|
||||
.headingDelta = nullptr };
|
||||
|
||||
if (!freerdp_read_four_byte_float(s, &pdu.latitudeDelta) ||
|
||||
!freerdp_read_four_byte_float(s, &pdu.longitudeDelta))
|
||||
return FALSE;
|
||||
|
||||
if (Stream_GetRemainingLength(s) >= 1)
|
||||
{
|
||||
if (!freerdp_read_four_byte_float(s, &speedDelta) ||
|
||||
!freerdp_read_four_byte_float(s, &headingDelta))
|
||||
return FALSE;
|
||||
|
||||
pdu.speedDelta = &speedDelta;
|
||||
pdu.headingDelta = &headingDelta;
|
||||
}
|
||||
|
||||
IFCALLRET(context->Location2DDelta, error, context, &pdu);
|
||||
if (error)
|
||||
WLog_ERR(TAG, "context->Location2DDelta failed with error %" PRIu32 "", error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_recv_location3d_delta(LocationServerContext* context, wStream* s,
|
||||
const RDPLOCATION_HEADER* header)
|
||||
{
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
double speedDelta = 0.0;
|
||||
double headingDelta = 0.0;
|
||||
|
||||
WINPR_ASSERT(context);
|
||||
WINPR_ASSERT(s);
|
||||
WINPR_ASSERT(header);
|
||||
|
||||
RDPLOCATION_LOCATION3D_DELTA_PDU pdu = { .header = *header,
|
||||
.latitudeDelta = FP_NAN,
|
||||
.longitudeDelta = FP_NAN,
|
||||
.speedDelta = nullptr,
|
||||
.headingDelta = nullptr };
|
||||
|
||||
if (!freerdp_read_four_byte_float(s, &pdu.latitudeDelta) ||
|
||||
!freerdp_read_four_byte_float(s, &pdu.longitudeDelta) ||
|
||||
!freerdp_read_four_byte_signed_integer(s, &pdu.altitudeDelta))
|
||||
return FALSE;
|
||||
|
||||
if (Stream_GetRemainingLength(s) >= 1)
|
||||
{
|
||||
if (!freerdp_read_four_byte_float(s, &speedDelta) ||
|
||||
!freerdp_read_four_byte_float(s, &headingDelta))
|
||||
return FALSE;
|
||||
|
||||
pdu.speedDelta = &speedDelta;
|
||||
pdu.headingDelta = &headingDelta;
|
||||
}
|
||||
|
||||
IFCALLRET(context->Location3DDelta, error, context, &pdu);
|
||||
if (error)
|
||||
WLog_ERR(TAG, "context->Location3DDelta failed with error %" PRIu32 "", error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_process_message(location_server* location)
|
||||
{
|
||||
UINT error = ERROR_INTERNAL_ERROR;
|
||||
ULONG BytesReturned = 0;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
WINPR_ASSERT(location->location_channel);
|
||||
|
||||
wStream* s = location->buffer;
|
||||
WINPR_ASSERT(s);
|
||||
|
||||
Stream_ResetPosition(s);
|
||||
const BOOL rc =
|
||||
WTSVirtualChannelRead(location->location_channel, 0, nullptr, 0, &BytesReturned);
|
||||
if (!rc)
|
||||
goto out;
|
||||
|
||||
if (BytesReturned < 1)
|
||||
{
|
||||
error = CHANNEL_RC_OK;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
|
||||
{
|
||||
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
||||
error = CHANNEL_RC_NO_MEMORY;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (WTSVirtualChannelRead(location->location_channel, 0, Stream_BufferAs(s, char),
|
||||
(ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
|
||||
{
|
||||
WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
|
||||
goto out;
|
||||
}
|
||||
|
||||
Stream_SetLength(s, BytesReturned);
|
||||
if (!Stream_CheckAndLogRequiredLength(TAG, s, LOCATION_HEADER_SIZE))
|
||||
return ERROR_NO_DATA;
|
||||
|
||||
{
|
||||
const UINT16 pduType = Stream_Get_UINT16(s);
|
||||
const RDPLOCATION_HEADER header = { .pduType = (LOCATION_PDUTYPE)pduType,
|
||||
.pduLength = Stream_Get_UINT32(s) };
|
||||
|
||||
switch (pduType)
|
||||
{
|
||||
case PDUTYPE_CLIENT_READY:
|
||||
error = location_server_recv_client_ready(&location->context, s, &header);
|
||||
break;
|
||||
case PDUTYPE_BASE_LOCATION3D:
|
||||
error = location_server_recv_base_location3d(&location->context, s, &header);
|
||||
break;
|
||||
case PDUTYPE_LOCATION2D_DELTA:
|
||||
error = location_server_recv_location2d_delta(&location->context, s, &header);
|
||||
break;
|
||||
case PDUTYPE_LOCATION3D_DELTA:
|
||||
error = location_server_recv_location3d_delta(&location->context, s, &header);
|
||||
break;
|
||||
default:
|
||||
WLog_ERR(TAG, "location_process_message: unknown or invalid pduType %" PRIu16 "",
|
||||
pduType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
if (error)
|
||||
WLog_ERR(TAG, "Response failed with error %" PRIu32 "!", error);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_context_poll_int(LocationServerContext* context)
|
||||
{
|
||||
location_server* location = (location_server*)context;
|
||||
UINT error = ERROR_INTERNAL_ERROR;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
switch (location->state)
|
||||
{
|
||||
case LOCATION_INITIAL:
|
||||
error = location_server_open_channel(location);
|
||||
if (error)
|
||||
WLog_ERR(TAG, "location_server_open_channel failed with error %" PRIu32 "!", error);
|
||||
else
|
||||
location->state = LOCATION_OPENED;
|
||||
break;
|
||||
case LOCATION_OPENED:
|
||||
error = location_process_message(location);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static HANDLE location_server_get_channel_handle(location_server* location)
|
||||
{
|
||||
void* buffer = nullptr;
|
||||
DWORD BytesReturned = 0;
|
||||
HANDLE ChannelEvent = nullptr;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
if (WTSVirtualChannelQuery(location->location_channel, WTSVirtualEventHandle, &buffer,
|
||||
&BytesReturned) == TRUE)
|
||||
{
|
||||
if (BytesReturned == sizeof(HANDLE))
|
||||
ChannelEvent = *(HANDLE*)buffer;
|
||||
|
||||
WTSFreeMemory(buffer);
|
||||
}
|
||||
|
||||
return ChannelEvent;
|
||||
}
|
||||
|
||||
static DWORD WINAPI location_server_thread_func(LPVOID arg)
|
||||
{
|
||||
DWORD nCount = 0;
|
||||
HANDLE events[2] = WINPR_C_ARRAY_INIT;
|
||||
location_server* location = (location_server*)arg;
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
DWORD status = 0;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
nCount = 0;
|
||||
events[nCount++] = location->stopEvent;
|
||||
|
||||
while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
|
||||
{
|
||||
switch (location->state)
|
||||
{
|
||||
case LOCATION_INITIAL:
|
||||
error = location_server_context_poll_int(&location->context);
|
||||
if (error == CHANNEL_RC_OK)
|
||||
{
|
||||
events[1] = location_server_get_channel_handle(location);
|
||||
nCount = 2;
|
||||
}
|
||||
break;
|
||||
case LOCATION_OPENED:
|
||||
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
||||
switch (status)
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
break;
|
||||
case WAIT_OBJECT_0 + 1:
|
||||
case WAIT_TIMEOUT:
|
||||
error = location_server_context_poll_int(&location->context);
|
||||
break;
|
||||
|
||||
case WAIT_FAILED:
|
||||
default:
|
||||
error = ERROR_INTERNAL_ERROR;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
(void)WTSVirtualChannelClose(location->location_channel);
|
||||
location->location_channel = nullptr;
|
||||
|
||||
if (error && location->context.rdpcontext)
|
||||
setChannelError(location->context.rdpcontext, error,
|
||||
"location_server_thread_func reported an error");
|
||||
|
||||
ExitThread(error);
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_open(LocationServerContext* context)
|
||||
{
|
||||
location_server* location = (location_server*)context;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
if (!location->externalThread && (location->thread == nullptr))
|
||||
{
|
||||
location->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!location->stopEvent)
|
||||
{
|
||||
WLog_ERR(TAG, "CreateEvent failed!");
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
location->thread =
|
||||
CreateThread(nullptr, 0, location_server_thread_func, location, 0, nullptr);
|
||||
if (!location->thread)
|
||||
{
|
||||
WLog_ERR(TAG, "CreateThread failed!");
|
||||
(void)CloseHandle(location->stopEvent);
|
||||
location->stopEvent = nullptr;
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
}
|
||||
}
|
||||
location->isOpened = TRUE;
|
||||
|
||||
return CHANNEL_RC_OK;
|
||||
}
|
||||
|
||||
static UINT location_server_close(LocationServerContext* context)
|
||||
{
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
location_server* location = (location_server*)context;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
if (!location->externalThread && location->thread)
|
||||
{
|
||||
(void)SetEvent(location->stopEvent);
|
||||
|
||||
if (WaitForSingleObject(location->thread, INFINITE) == WAIT_FAILED)
|
||||
{
|
||||
error = GetLastError();
|
||||
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
|
||||
return error;
|
||||
}
|
||||
|
||||
(void)CloseHandle(location->thread);
|
||||
(void)CloseHandle(location->stopEvent);
|
||||
location->thread = nullptr;
|
||||
location->stopEvent = nullptr;
|
||||
}
|
||||
if (location->externalThread)
|
||||
{
|
||||
if (location->state != LOCATION_INITIAL)
|
||||
{
|
||||
(void)WTSVirtualChannelClose(location->location_channel);
|
||||
location->location_channel = nullptr;
|
||||
location->state = LOCATION_INITIAL;
|
||||
}
|
||||
}
|
||||
location->isOpened = FALSE;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_context_poll(LocationServerContext* context)
|
||||
{
|
||||
location_server* location = (location_server*)context;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
|
||||
if (!location->externalThread)
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
|
||||
return location_server_context_poll_int(context);
|
||||
}
|
||||
|
||||
static BOOL location_server_context_handle(LocationServerContext* context, HANDLE* handle)
|
||||
{
|
||||
location_server* location = (location_server*)context;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
WINPR_ASSERT(handle);
|
||||
|
||||
if (!location->externalThread)
|
||||
return FALSE;
|
||||
if (location->state == LOCATION_INITIAL)
|
||||
return FALSE;
|
||||
|
||||
*handle = location_server_get_channel_handle(location);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static UINT location_server_packet_send(LocationServerContext* context, wStream* s)
|
||||
{
|
||||
location_server* location = (location_server*)context;
|
||||
UINT error = CHANNEL_RC_OK;
|
||||
ULONG written = 0;
|
||||
|
||||
WINPR_ASSERT(location);
|
||||
WINPR_ASSERT(s);
|
||||
|
||||
const size_t pos = Stream_GetPosition(s);
|
||||
WINPR_ASSERT(pos <= UINT32_MAX);
|
||||
if (!WTSVirtualChannelWrite(location->location_channel, Stream_BufferAs(s, char), (ULONG)pos,
|
||||
&written))
|
||||
{
|
||||
WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
|
||||
error = ERROR_INTERNAL_ERROR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (written < Stream_GetPosition(s))
|
||||
{
|
||||
WLog_WARN(TAG, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
|
||||
Stream_GetPosition(s));
|
||||
}
|
||||
|
||||
out:
|
||||
Stream_Free(s, TRUE);
|
||||
return error;
|
||||
}
|
||||
|
||||
static UINT location_server_send_server_ready(LocationServerContext* context,
|
||||
const RDPLOCATION_SERVER_READY_PDU* serverReady)
|
||||
{
|
||||
wStream* s = nullptr;
|
||||
UINT32 pduLength = 0;
|
||||
UINT32 protocolVersion = 0;
|
||||
|
||||
WINPR_ASSERT(context);
|
||||
WINPR_ASSERT(serverReady);
|
||||
|
||||
protocolVersion = serverReady->protocolVersion;
|
||||
|
||||
pduLength = LOCATION_HEADER_SIZE + 4 + 4;
|
||||
|
||||
s = Stream_New(nullptr, pduLength);
|
||||
if (!s)
|
||||
{
|
||||
WLog_ERR(TAG, "Stream_New failed!");
|
||||
return ERROR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
|
||||
/* RDPLOCATION_HEADER */
|
||||
Stream_Write_UINT16(s, PDUTYPE_SERVER_READY);
|
||||
Stream_Write_UINT32(s, pduLength);
|
||||
|
||||
Stream_Write_UINT32(s, protocolVersion);
|
||||
Stream_Write_UINT32(s, serverReady->flags);
|
||||
|
||||
return location_server_packet_send(context, s);
|
||||
}
|
||||
|
||||
LocationServerContext* location_server_context_new(HANDLE vcm)
|
||||
{
|
||||
location_server* location = (location_server*)calloc(1, sizeof(location_server));
|
||||
|
||||
if (!location)
|
||||
return nullptr;
|
||||
|
||||
location->context.vcm = vcm;
|
||||
location->context.Initialize = location_server_initialize;
|
||||
location->context.Open = location_server_open;
|
||||
location->context.Close = location_server_close;
|
||||
location->context.Poll = location_server_context_poll;
|
||||
location->context.ChannelHandle = location_server_context_handle;
|
||||
|
||||
location->context.ServerReady = location_server_send_server_ready;
|
||||
|
||||
location->buffer = Stream_New(nullptr, 4096);
|
||||
if (!location->buffer)
|
||||
goto fail;
|
||||
|
||||
return &location->context;
|
||||
fail:
|
||||
WINPR_PRAGMA_DIAG_PUSH
|
||||
WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
|
||||
location_server_context_free(&location->context);
|
||||
WINPR_PRAGMA_DIAG_POP
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void location_server_context_free(LocationServerContext* context)
|
||||
{
|
||||
location_server* location = (location_server*)context;
|
||||
|
||||
if (location)
|
||||
{
|
||||
location_server_close(context);
|
||||
Stream_Free(location->buffer, TRUE);
|
||||
}
|
||||
|
||||
free(location);
|
||||
}
|
||||
Reference in New Issue
Block a user