2011-08-04 19:22:58 +04:00
|
|
|
/**
|
2012-10-09 05:00:07 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Device Redirection Virtual Channel
|
2011-08-04 19:22:58 +04:00
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
2012-10-09 05:00:07 +04:00
|
|
|
* Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2015-06-03 14:38:47 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2016-01-24 17:21:06 +03:00
|
|
|
* Copyright 2016 Armin Novak <armin.novak@gmail.com>
|
2011-08-04 19:22:58 +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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-10-17 23:42:51 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <freerdp/types.h>
|
2012-11-18 07:03:04 +04:00
|
|
|
#include <freerdp/addin.h>
|
2012-10-09 06:48:17 +04:00
|
|
|
#include <freerdp/client/channels.h>
|
2014-08-11 11:12:01 +04:00
|
|
|
#include <freerdp/channels/log.h>
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2012-10-09 05:00:07 +04:00
|
|
|
#include "rdpdr_main.h"
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include "devman.h"
|
|
|
|
|
2022-12-22 02:32:17 +03:00
|
|
|
#define TAG CHANNELS_TAG("rdpdr.client")
|
|
|
|
|
2018-10-19 12:30:01 +03:00
|
|
|
static void devman_device_free(void* obj)
|
2013-10-18 00:30:36 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DEVICE* device = (DEVICE*)obj;
|
2018-10-19 12:30:01 +03:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!device)
|
|
|
|
return;
|
|
|
|
|
2013-10-18 00:30:36 +04:00
|
|
|
IFCALL(device->Free, device);
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
DEVMAN* devman_new(rdpdrPlugin* rdpdr)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
|
|
|
DEVMAN* devman;
|
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!rdpdr)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
devman = (DEVMAN*)calloc(1, sizeof(DEVMAN));
|
2015-06-03 14:38:47 +03:00
|
|
|
|
2015-05-20 20:19:50 +03:00
|
|
|
if (!devman)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_INFO, "calloc failed!");
|
2015-05-20 20:19:50 +03:00
|
|
|
return NULL;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
devman->plugin = (void*)rdpdr;
|
2011-08-04 19:22:58 +04:00
|
|
|
devman->id_sequence = 1;
|
2013-10-18 00:30:36 +04:00
|
|
|
devman->devices = ListDictionary_New(TRUE);
|
2018-12-12 12:06:41 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!devman->devices)
|
|
|
|
{
|
2022-12-22 02:32:17 +03:00
|
|
|
WLog_Print(rdpdr->log, WLOG_INFO, "ListDictionary_New failed!");
|
2015-08-28 12:06:26 +03:00
|
|
|
free(devman);
|
2015-05-18 12:28:00 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-03 14:38:47 +03:00
|
|
|
|
2018-10-19 12:30:01 +03:00
|
|
|
ListDictionary_ValueObject(devman->devices)->fnObjectFree = devman_device_free;
|
2011-08-05 19:19:43 +04:00
|
|
|
return devman;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void devman_free(DEVMAN* devman)
|
|
|
|
{
|
2013-10-18 00:30:36 +04:00
|
|
|
ListDictionary_Free(devman->devices);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(devman);
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
void devman_unregister_device(DEVMAN* devman, void* key)
|
|
|
|
{
|
|
|
|
DEVICE* device;
|
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!devman || !key)
|
|
|
|
return;
|
|
|
|
|
2023-06-27 12:37:00 +03:00
|
|
|
device = (DEVICE*)ListDictionary_Take(devman->devices, key);
|
2014-04-06 00:57:31 +04:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
if (device)
|
|
|
|
devman_device_free(device);
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT devman_register_device(DEVMAN* devman, DEVICE* device)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2013-10-18 00:30:36 +04:00
|
|
|
void* key = NULL;
|
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!devman || !device)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
device->id = devman->id_sequence++;
|
2019-11-06 17:24:51 +03:00
|
|
|
key = (void*)(size_t)device->id;
|
2013-10-18 00:30:36 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!ListDictionary_Add(devman->devices, key, device))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_INFO(TAG, "ListDictionary_Add failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2018-12-12 12:06:41 +03:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-18 00:30:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DEVICE* devman_get_device_by_id(DEVMAN* devman, UINT32 id)
|
|
|
|
{
|
|
|
|
DEVICE* device = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
void* key = (void*)(size_t)id;
|
2013-10-18 00:30:36 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!devman)
|
2022-12-14 15:38:34 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "device manager=%p", devman);
|
2016-01-24 17:21:06 +03:00
|
|
|
return NULL;
|
2022-12-14 15:38:34 +03:00
|
|
|
}
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
device = (DEVICE*)ListDictionary_GetItemValue(devman->devices, key);
|
2022-12-14 15:38:34 +03:00
|
|
|
if (!device)
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_WARN(TAG, "could not find device ID 0x%08" PRIx32, id);
|
2018-12-12 12:06:41 +03:00
|
|
|
return device;
|
|
|
|
}
|
2013-10-18 00:30:36 +04:00
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
DEVICE* devman_get_device_by_type(DEVMAN* devman, UINT32 type)
|
|
|
|
{
|
|
|
|
DEVICE* device = NULL;
|
2023-06-27 10:32:00 +03:00
|
|
|
ULONG_PTR* keys = NULL;
|
2018-12-12 12:06:41 +03:00
|
|
|
|
|
|
|
if (!devman)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ListDictionary_Lock(devman->devices);
|
2023-06-27 10:32:00 +03:00
|
|
|
const size_t count = ListDictionary_GetKeys(devman->devices, &keys);
|
2018-12-12 12:06:41 +03:00
|
|
|
|
2023-06-27 10:32:00 +03:00
|
|
|
for (size_t x = 0; x < count; x++)
|
2018-12-12 12:06:41 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
DEVICE* cur = (DEVICE*)ListDictionary_GetItemValue(devman->devices, (void*)keys[x]);
|
2018-12-12 12:06:41 +03:00
|
|
|
|
|
|
|
if (!cur)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cur->type != type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
device = cur;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(keys);
|
|
|
|
ListDictionary_Unlock(devman->devices);
|
2013-10-18 00:30:36 +04:00
|
|
|
return device;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2019-09-04 16:48:34 +03:00
|
|
|
static const char DRIVE_SERVICE_NAME[] = "drive";
|
|
|
|
static const char PRINTER_SERVICE_NAME[] = "printer";
|
|
|
|
static const char SMARTCARD_SERVICE_NAME[] = "smartcard";
|
|
|
|
static const char SERIAL_SERVICE_NAME[] = "serial";
|
|
|
|
static const char PARALLEL_SERVICE_NAME[] = "parallel";
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-10-24 15:34:26 +03:00
|
|
|
UINT devman_load_device_service(DEVMAN* devman, const RDPDR_DEVICE* device, rdpContext* rdpcontext)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2019-09-04 16:48:34 +03:00
|
|
|
const char* ServiceName = NULL;
|
2011-08-04 19:22:58 +04:00
|
|
|
DEVICE_SERVICE_ENTRY_POINTS ep;
|
2012-10-09 06:48:17 +04:00
|
|
|
PDEVICE_SERVICE_ENTRY entry = NULL;
|
2022-06-23 08:57:38 +03:00
|
|
|
union
|
|
|
|
{
|
2020-02-19 12:09:32 +03:00
|
|
|
const RDPDR_DEVICE* cdp;
|
|
|
|
RDPDR_DEVICE* dp;
|
|
|
|
} devconv;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2020-02-19 12:09:32 +03:00
|
|
|
devconv.cdp = device;
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!devman || !device || !rdpcontext)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
if (device->Type == RDPDR_DTYP_FILESYSTEM)
|
|
|
|
ServiceName = DRIVE_SERVICE_NAME;
|
|
|
|
else if (device->Type == RDPDR_DTYP_PRINT)
|
|
|
|
ServiceName = PRINTER_SERVICE_NAME;
|
|
|
|
else if (device->Type == RDPDR_DTYP_SMARTCARD)
|
|
|
|
ServiceName = SMARTCARD_SERVICE_NAME;
|
|
|
|
else if (device->Type == RDPDR_DTYP_SERIAL)
|
|
|
|
ServiceName = SERIAL_SERVICE_NAME;
|
|
|
|
else if (device->Type == RDPDR_DTYP_PARALLEL)
|
|
|
|
ServiceName = PARALLEL_SERVICE_NAME;
|
|
|
|
|
|
|
|
if (!ServiceName)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_INFO(TAG, "ServiceName %s did not match!", ServiceName);
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_INVALID_NAME;
|
|
|
|
}
|
2012-11-09 04:01:52 +04:00
|
|
|
|
2016-03-17 11:33:39 +03:00
|
|
|
if (device->Name)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_INFO(TAG, "Loading device service %s [%s] (static)", ServiceName, device->Name);
|
2016-03-17 11:33:39 +03:00
|
|
|
else
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_INFO(TAG, "Loading device service %s (static)", ServiceName);
|
2018-12-12 12:06:41 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
entry = (PDEVICE_SERVICE_ENTRY)freerdp_load_channel_addin_entry(ServiceName, NULL,
|
|
|
|
"DeviceServiceEntry", 0);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2013-10-18 00:30:36 +04:00
|
|
|
if (!entry)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_INFO(TAG, "freerdp_load_channel_addin_entry failed!");
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
|
|
|
|
ep.devman = devman;
|
|
|
|
ep.RegisterDevice = devman_register_device;
|
2020-02-19 12:09:32 +03:00
|
|
|
ep.device = devconv.dp;
|
2015-07-15 10:50:35 +03:00
|
|
|
ep.rdpcontext = rdpcontext;
|
|
|
|
return entry(&ep);
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|