Files

238 lines
5.5 KiB
C
Raw Permalink Normal View History

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