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>
|
2016-01-28 13:12:20 +03:00
|
|
|
* Copyright 2015-2016 Thincast Technologies GmbH
|
2015-06-03 14:38:47 +03:00
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2016-01-28 13:12:20 +03:00
|
|
|
* Copyright 2016 Armin Novak <armin.novak@thincast.com>
|
2016-06-29 00:39:14 +03:00
|
|
|
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-08-05 06:24:46 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
|
|
|
|
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-10-08 06:53:24 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-10-17 23:42:51 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2011-08-04 19:22:58 +04:00
|
|
|
#include <freerdp/types.h>
|
2012-10-07 18:21:54 +04:00
|
|
|
#include <freerdp/constants.h>
|
2014-08-11 11:12:01 +04:00
|
|
|
#include <freerdp/channels/log.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <dbt.h>
|
2014-02-20 13:42:02 +04:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2014-02-14 11:09:55 +04:00
|
|
|
#endif
|
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
#ifdef __MACOSX__
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-05 06:24:46 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-05 11:43:48 +04:00
|
|
|
#include "rdpdr_capabilities.h"
|
2012-10-09 05:00:07 +04:00
|
|
|
|
2011-08-05 21:54:40 +04:00
|
|
|
#include "devman.h"
|
|
|
|
#include "irp.h"
|
2012-10-09 04:26:11 +04:00
|
|
|
|
2011-08-05 11:43:48 +04:00
|
|
|
#include "rdpdr_main.h"
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2014-02-14 12:35:44 +04:00
|
|
|
typedef struct _DEVICE_DRIVE_EXT DEVICE_DRIVE_EXT;
|
|
|
|
|
|
|
|
struct _DEVICE_DRIVE_EXT
|
|
|
|
{
|
|
|
|
DEVICE device;
|
|
|
|
char* path;
|
|
|
|
};
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-07-21 17:51:42 +03:00
|
|
|
static UINT rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr,
|
|
|
|
BOOL userLoggedOn);
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-07-21 17:51:42 +03:00
|
|
|
static UINT rdpdr_send_device_list_remove_request(rdpdrPlugin* rdpdr,
|
|
|
|
UINT32 count, UINT32 ids[])
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2014-05-10 17:50:36 +04:00
|
|
|
UINT32 i;
|
2014-02-14 11:09:55 +04:00
|
|
|
wStream* s;
|
2016-01-27 21:26:52 +03:00
|
|
|
s = Stream_New(NULL, count * sizeof(UINT32) + 8);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
|
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_REMOVE);
|
|
|
|
Stream_Write_UINT32(s, count);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
Stream_Write_UINT32(s, ids[i]);
|
|
|
|
|
|
|
|
Stream_SealLength(s);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
2016-03-29 23:03:15 +03:00
|
|
|
#ifdef _UWP
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
void first_hotplug(rdpdrPlugin* rdpdr)
|
2016-03-29 23:03:15 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* drive_hotplug_thread_func(void* arg)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif _WIN32
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
BOOL check_path(char* path)
|
|
|
|
{
|
|
|
|
UINT type = GetDriveTypeA(path);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
if (!(type == DRIVE_REMOVABLE || type == DRIVE_CDROM || type == DRIVE_REMOTE))
|
|
|
|
return FALSE;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
return GetVolumeInformationA(path, NULL, 0, NULL, NULL, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
void first_hotplug(rdpdrPlugin* rdpdr)
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char drive_path[5] = { 'c', ':', '\\', '\0' };
|
|
|
|
DWORD unitmask = GetLogicalDrives();
|
|
|
|
|
|
|
|
for (i = 0; i < 26; i++)
|
|
|
|
{
|
|
|
|
if (unitmask & 0x01)
|
|
|
|
{
|
|
|
|
RDPDR_DRIVE* drive;
|
|
|
|
drive_path[0] = 'A' + i;
|
|
|
|
drive_path[1] = ':';
|
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
if (check_path(drive_path))
|
|
|
|
{
|
|
|
|
drive = (RDPDR_DRIVE*)malloc(sizeof(RDPDR_DRIVE));
|
|
|
|
ZeroMemory(drive, sizeof(RDPDR_DRIVE));
|
|
|
|
drive->Type = RDPDR_DTYP_FILESYSTEM;
|
|
|
|
drive->Path = _strdup(drive_path);
|
|
|
|
drive_path[1] = '\0';
|
|
|
|
drive->Name = _strdup(drive_path);
|
2016-07-21 17:51:42 +03:00
|
|
|
devman_load_device_service(rdpdr->devman, (RDPDR_DEVICE*)drive,
|
|
|
|
rdpdr->rdpcontext);
|
2016-02-26 21:32:54 +03:00
|
|
|
}
|
2015-09-07 13:16:03 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
unitmask = unitmask >> 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
LRESULT CALLBACK hotplug_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdrPlugin* rdpdr;
|
2014-02-14 11:09:55 +04:00
|
|
|
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
switch (Msg)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
|
|
|
case WM_DEVICECHANGE:
|
|
|
|
switch (wParam)
|
|
|
|
{
|
|
|
|
case DBT_DEVICEARRIVAL:
|
|
|
|
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
|
|
|
|
{
|
|
|
|
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
|
|
|
|
DWORD unitmask = lpdbv->dbcv_unitmask;
|
|
|
|
int i;
|
|
|
|
char drive_path[4] = { 'c', ':', '/', '\0'};
|
|
|
|
|
|
|
|
for (i = 0; i < 26; i++)
|
|
|
|
{
|
|
|
|
if (unitmask & 0x01)
|
|
|
|
{
|
|
|
|
RDPDR_DRIVE* drive;
|
|
|
|
drive_path[0] = 'A' + i;
|
2015-09-07 13:16:03 +03:00
|
|
|
drive_path[1] = ':';
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2016-02-26 21:32:54 +03:00
|
|
|
if (check_path(drive_path))
|
|
|
|
{
|
|
|
|
drive = (RDPDR_DRIVE*) malloc(sizeof(RDPDR_DRIVE));
|
|
|
|
ZeroMemory(drive, sizeof(RDPDR_DRIVE));
|
|
|
|
drive->Type = RDPDR_DTYP_FILESYSTEM;
|
|
|
|
drive->Path = _strdup(drive_path);
|
|
|
|
drive_path[1] = '\0';
|
|
|
|
drive->Name = _strdup(drive_path);
|
2016-07-21 17:51:42 +03:00
|
|
|
devman_load_device_service(rdpdr->devman, (RDPDR_DEVICE*)drive,
|
|
|
|
rdpdr->rdpcontext);
|
2016-02-26 21:32:54 +03:00
|
|
|
rdpdr_send_device_list_announce_request(rdpdr, TRUE);
|
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
unitmask = unitmask >> 1;
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DBT_DEVICEREMOVECOMPLETE:
|
|
|
|
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
|
|
|
|
{
|
|
|
|
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
|
|
|
|
DWORD unitmask = lpdbv->dbcv_unitmask;
|
|
|
|
int i, j, count;
|
|
|
|
char drive_name_upper, drive_name_lower;
|
2016-07-21 17:51:42 +03:00
|
|
|
ULONG_PTR* keys;
|
|
|
|
DEVICE_DRIVE_EXT* device_ext;
|
2014-02-14 11:09:55 +04:00
|
|
|
UINT32 ids[1];
|
|
|
|
|
|
|
|
for (i = 0; i < 26; i++)
|
|
|
|
{
|
|
|
|
if (unitmask & 0x01)
|
|
|
|
{
|
|
|
|
drive_name_upper = 'A' + i;
|
|
|
|
drive_name_lower = 'a' + i;
|
|
|
|
count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
|
|
|
|
|
|
|
|
for (j = 0; j < count; j++)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
device_ext = (DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(
|
|
|
|
rdpdr->devman->devices, (void*)keys[j]);
|
|
|
|
|
|
|
|
if (device_ext->path[0] == drive_name_upper
|
|
|
|
|| device_ext->path[0] == drive_name_lower)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
devman_unregister_device(rdpdr->devman, (void*)keys[j]);
|
2014-02-14 11:09:55 +04:00
|
|
|
ids[0] = keys[j];
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if ((error = rdpdr_send_device_list_remove_request(rdpdr, 1, ids)))
|
|
|
|
{
|
|
|
|
// dont end on error, just report ?
|
2016-07-21 17:51:42 +03:00
|
|
|
WLog_ERR(TAG, "rdpdr_send_device_list_remove_request failed with error %lu!",
|
|
|
|
error);
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
unitmask = unitmask >> 1;
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DefWindowProc(hWnd, Msg, wParam, lParam);
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
return DefWindowProc(hWnd, Msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2014-02-14 12:35:44 +04:00
|
|
|
static void* drive_hotplug_thread_func(void* arg)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdrPlugin* rdpdr;
|
2014-02-14 11:09:55 +04:00
|
|
|
WNDCLASSEX wnd_cls;
|
|
|
|
HWND hwnd;
|
|
|
|
MSG msg;
|
|
|
|
BOOL bRet;
|
|
|
|
DEV_BROADCAST_HANDLE NotificationFilter;
|
|
|
|
HDEVNOTIFY hDevNotify;
|
2016-07-21 17:51:42 +03:00
|
|
|
rdpdr = (rdpdrPlugin*)arg;
|
2014-02-14 11:09:55 +04:00
|
|
|
/* init windows class */
|
|
|
|
wnd_cls.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wnd_cls.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
wnd_cls.lpfnWndProc = hotplug_proc;
|
|
|
|
wnd_cls.cbClsExtra = 0;
|
|
|
|
wnd_cls.cbWndExtra = 0;
|
|
|
|
wnd_cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wnd_cls.hCursor = NULL;
|
|
|
|
wnd_cls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
|
|
|
wnd_cls.lpszMenuName = NULL;
|
|
|
|
wnd_cls.lpszClassName = L"DRIVE_HOTPLUG";
|
|
|
|
wnd_cls.hInstance = NULL;
|
|
|
|
wnd_cls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
RegisterClassEx(&wnd_cls);
|
|
|
|
/* create window */
|
|
|
|
hwnd = CreateWindowEx(0, L"DRIVE_HOTPLUG", NULL,
|
2016-07-21 17:51:42 +03:00
|
|
|
0, 0, 0, 0, 0,
|
|
|
|
NULL, NULL, NULL, NULL);
|
2014-02-14 11:09:55 +04:00
|
|
|
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)rdpdr);
|
|
|
|
rdpdr->hotplug_wnd = hwnd;
|
|
|
|
/* register device interface to hwnd */
|
|
|
|
NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HANDLE);
|
|
|
|
NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
|
2016-07-21 17:51:42 +03:00
|
|
|
hDevNotify = RegisterDeviceNotification(hwnd, &NotificationFilter,
|
|
|
|
DEVICE_NOTIFY_WINDOW_HANDLE);
|
2014-02-14 11:09:55 +04:00
|
|
|
|
|
|
|
/* message loop */
|
|
|
|
while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0)
|
|
|
|
{
|
|
|
|
if (bRet == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnregisterDeviceNotification(hDevNotify);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-30 17:50:10 +03:00
|
|
|
if (rdpdr->hotplug_wnd && !PostMessage(rdpdr->hotplug_wnd, WM_QUIT, 0, 0))
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "PostMessage failed with error %lu", error);
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-30 17:50:10 +03:00
|
|
|
return error;
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
#elif __MACOSX__
|
|
|
|
|
|
|
|
#define MAX_USB_DEVICES 100
|
|
|
|
|
|
|
|
typedef struct _hotplug_dev
|
|
|
|
{
|
|
|
|
char* path;
|
|
|
|
BOOL to_add;
|
|
|
|
} hotplug_dev;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT handle_hotplug(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
struct dirent* pDirent;
|
|
|
|
DIR* pDir;
|
2015-09-07 13:16:03 +03:00
|
|
|
char fullpath[PATH_MAX];
|
|
|
|
char* szdir = (char*)"/Volumes";
|
|
|
|
struct stat buf;
|
|
|
|
hotplug_dev dev_array[MAX_USB_DEVICES];
|
|
|
|
int count;
|
2016-07-21 17:51:42 +03:00
|
|
|
DEVICE_DRIVE_EXT* device_ext;
|
|
|
|
ULONG_PTR* keys;
|
2015-09-07 13:16:03 +03:00
|
|
|
int i, j;
|
|
|
|
int size = 0;
|
|
|
|
UINT error;
|
|
|
|
UINT32 ids[1];
|
2016-07-21 17:51:42 +03:00
|
|
|
pDir = opendir(szdir);
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
if (pDir == NULL)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
printf("Cannot open directory\n");
|
2015-09-07 13:16:03 +03:00
|
|
|
return ERROR_OPEN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pDirent = readdir(pDir)) != NULL)
|
|
|
|
{
|
|
|
|
if (pDirent->d_name[0] != '.')
|
|
|
|
{
|
|
|
|
sprintf(fullpath, "%s/%s", szdir, pDirent->d_name);
|
|
|
|
lstat(fullpath, &buf);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
|
|
|
if (S_ISDIR(buf.st_mode))
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
dev_array[size].path = _strdup(fullpath);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if (!dev_array[size].path)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
closedir(pDir);
|
2015-09-07 13:16:03 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
dev_array[size++].to_add = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
closedir(pDir);
|
2015-09-07 13:16:03 +03:00
|
|
|
/* delete removed devices */
|
|
|
|
count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
|
|
|
|
|
|
|
|
for (j = 0; j < count; j++)
|
|
|
|
{
|
|
|
|
BOOL dev_found = FALSE;
|
2016-07-21 17:51:42 +03:00
|
|
|
device_ext = (DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(
|
|
|
|
rdpdr->devman->devices, (void*)keys[j]);
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
if (!device_ext)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (device_ext->path == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* not plugable device */
|
|
|
|
if (strstr(device_ext->path, "/Volumes/") == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (strstr(device_ext->path, dev_array[i].path) != NULL)
|
|
|
|
{
|
|
|
|
dev_found = TRUE;
|
|
|
|
dev_array[i].to_add = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev_found)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
devman_unregister_device(rdpdr->devman, (void*)keys[j]);
|
2015-09-07 13:16:03 +03:00
|
|
|
ids[0] = keys[j];
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if ((error = rdpdr_send_device_list_remove_request(rdpdr, 1, ids)))
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
WLog_ERR(TAG, "rdpdr_send_device_list_remove_request failed with error %lu!",
|
|
|
|
error);
|
2015-09-07 13:16:03 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add new devices */
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
RDPDR_DRIVE* drive;
|
|
|
|
|
|
|
|
if (dev_array[i].to_add)
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
drive = (RDPDR_DRIVE*) calloc(1, sizeof(RDPDR_DRIVE));
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if (!drive)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
drive->Type = RDPDR_DTYP_FILESYSTEM;
|
|
|
|
drive->Path = dev_array[i].path;
|
|
|
|
dev_array[i].path = NULL;
|
|
|
|
name = strrchr(drive->Path, '/') + 1;
|
|
|
|
drive->Name = _strdup(name);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if (!drive->Name)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "_strdup failed!");
|
|
|
|
free(drive->Path);
|
|
|
|
free(drive);
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
|
|
|
if ((error = devman_load_device_service(rdpdr->devman, (RDPDR_DEVICE*)drive,
|
|
|
|
rdpdr->rdpcontext)))
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "devman_load_device_service failed!");
|
|
|
|
free(drive->Path);
|
|
|
|
free(drive->Name);
|
|
|
|
free(drive);
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
for (i = 0; i < size; i++)
|
2016-07-21 17:51:42 +03:00
|
|
|
free(dev_array[i].path);
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
return error;
|
2016-07-21 17:51:42 +03:00
|
|
|
}
|
2015-09-07 13:16:03 +03:00
|
|
|
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
static void drive_hotplug_fsevent_callback(ConstFSEventStreamRef streamRef,
|
|
|
|
void* clientCallBackInfo,
|
|
|
|
size_t numEvents, void* eventPaths, const FSEventStreamEventFlags eventFlags[],
|
|
|
|
const FSEventStreamEventId eventIds[])
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
rdpdrPlugin* rdpdr;
|
|
|
|
int i;
|
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
char** paths = (char**)eventPaths;
|
2015-09-07 13:16:03 +03:00
|
|
|
rdpdr = (rdpdrPlugin*) clientCallBackInfo;
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
for (i = 0; i < numEvents; i++)
|
2015-09-07 13:16:03 +03:00
|
|
|
{
|
|
|
|
if (strcmp(paths[i], "/Volumes/") == 0)
|
|
|
|
{
|
|
|
|
if ((error = handle_hotplug(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "handle_hotplug failed with error %lu!", error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rdpdr_send_device_list_announce_request(rdpdr, TRUE);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
void first_hotplug(rdpdrPlugin* rdpdr)
|
2016-02-26 21:31:14 +03:00
|
|
|
{
|
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-02-26 21:31:14 +03:00
|
|
|
if ((error = handle_hotplug(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "handle_hotplug failed with error %lu!", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
static void* drive_hotplug_thread_func(void* arg)
|
|
|
|
{
|
|
|
|
rdpdrPlugin* rdpdr;
|
|
|
|
FSEventStreamRef fsev;
|
|
|
|
rdpdr = (rdpdrPlugin*) arg;
|
|
|
|
CFStringRef path = CFSTR("/Volumes/");
|
2016-07-21 17:51:42 +03:00
|
|
|
CFArrayRef pathsToWatch = CFArrayCreate(kCFAllocatorMalloc, (const void**)&path,
|
|
|
|
1, NULL);
|
2015-09-07 13:16:03 +03:00
|
|
|
FSEventStreamContext ctx;
|
|
|
|
ZeroMemory(&ctx, sizeof(ctx));
|
|
|
|
ctx.info = arg;
|
2016-07-21 17:51:42 +03:00
|
|
|
fsev = FSEventStreamCreate(kCFAllocatorMalloc, drive_hotplug_fsevent_callback,
|
|
|
|
&ctx, pathsToWatch, kFSEventStreamEventIdSinceNow, 1,
|
|
|
|
kFSEventStreamCreateFlagNone);
|
2015-09-07 13:16:03 +03:00
|
|
|
rdpdr->runLoop = CFRunLoopGetCurrent();
|
|
|
|
FSEventStreamScheduleWithRunLoop(fsev, rdpdr->runLoop, kCFRunLoopDefaultMode);
|
|
|
|
FSEventStreamStart(fsev);
|
|
|
|
CFRunLoopRun();
|
|
|
|
FSEventStreamStop(fsev);
|
|
|
|
FSEventStreamRelease(fsev);
|
|
|
|
ExitThread(CHANNEL_RC_OK);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
if (rdpdr->hotplugThread)
|
|
|
|
{
|
|
|
|
CFRunLoopStop(rdpdr->runLoop);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(rdpdr->hotplugThread, INFINITE) == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
rdpdr->hotplugThread = NULL;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
#else
|
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
#define MAX_USB_DEVICES 100
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
typedef struct _hotplug_dev
|
|
|
|
{
|
2014-02-20 13:42:02 +04:00
|
|
|
char* path;
|
|
|
|
BOOL to_add;
|
|
|
|
} hotplug_dev;
|
|
|
|
|
|
|
|
static char* next_line(FILE* fd, size_t* len)
|
|
|
|
{
|
|
|
|
size_t newsiz;
|
|
|
|
int c;
|
|
|
|
char* newbuf;
|
|
|
|
char* lrbuf;
|
|
|
|
int lrsiz;
|
|
|
|
*len = 0;
|
|
|
|
lrsiz = 0;
|
|
|
|
lrbuf = NULL;
|
|
|
|
newbuf = NULL;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c = fgetc(fd);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
if (ferror(fd))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (c == EOF)
|
|
|
|
{
|
|
|
|
if (*len == 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lrbuf[(*len)] = '\0';
|
|
|
|
return lrbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*len == lrsiz)
|
|
|
|
{
|
|
|
|
newsiz = lrsiz + 4096;
|
|
|
|
newbuf = realloc(lrbuf, newsiz);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
if (newbuf == NULL)
|
|
|
|
return NULL;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
lrbuf = newbuf;
|
|
|
|
lrsiz = newsiz;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
lrbuf[(*len)] = c;
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
lrbuf[(*len)] = '\0';
|
|
|
|
return lrbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*len)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* get_word(char* str, unsigned int* offset)
|
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
char* tmp;
|
2014-05-09 07:45:40 +04:00
|
|
|
char* word;
|
2014-02-20 13:42:02 +04:00
|
|
|
int wlen;
|
|
|
|
|
|
|
|
if (*offset >= strlen(str))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
p = str + *offset;
|
|
|
|
tmp = p;
|
|
|
|
|
|
|
|
while (*tmp != ' ' && *tmp != '\0')
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
wlen = tmp - p;
|
|
|
|
*offset += wlen;
|
|
|
|
|
|
|
|
/* in case there are more than one space between words */
|
|
|
|
while (*(str + *offset) == ' ')
|
|
|
|
(*offset)++;
|
|
|
|
|
2014-05-09 07:45:40 +04:00
|
|
|
word = malloc(wlen + 1);
|
2016-03-07 14:54:49 +03:00
|
|
|
|
2014-05-09 07:45:40 +04:00
|
|
|
if (word != NULL)
|
|
|
|
{
|
|
|
|
CopyMemory(word, p, wlen);
|
|
|
|
word[wlen] = '\0';
|
|
|
|
}
|
2016-03-07 14:54:49 +03:00
|
|
|
|
2014-05-09 07:45:40 +04:00
|
|
|
return word;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT handle_hotplug(rdpdrPlugin* rdpdr)
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
FILE* f;
|
2014-02-20 13:42:02 +04:00
|
|
|
size_t len;
|
2016-07-21 17:51:42 +03:00
|
|
|
char* line;
|
|
|
|
char* word;
|
2014-02-20 13:42:02 +04:00
|
|
|
unsigned int wlen;
|
|
|
|
hotplug_dev dev_array[MAX_USB_DEVICES];
|
|
|
|
int i, j;
|
|
|
|
int size = 0;
|
|
|
|
int count;
|
2016-07-21 17:51:42 +03:00
|
|
|
DEVICE_DRIVE_EXT* device_ext;
|
|
|
|
ULONG_PTR* keys;
|
2014-02-20 13:42:02 +04:00
|
|
|
UINT32 ids[1];
|
2015-09-01 13:21:25 +03:00
|
|
|
UINT error = 0;
|
|
|
|
memset(dev_array, 0, sizeof(dev_array));
|
2014-02-20 13:42:02 +04:00
|
|
|
f = fopen("/proc/mounts", "r");
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
if (f == NULL)
|
|
|
|
{
|
2015-06-03 14:38:47 +03:00
|
|
|
WLog_ERR(TAG, "fopen failed!");
|
|
|
|
return ERROR_OPEN_FAILED;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((line = next_line(f, &len)))
|
|
|
|
{
|
|
|
|
wlen = 0;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
while ((word = get_word(line, &wlen)))
|
|
|
|
{
|
|
|
|
/* copy hotpluged device mount point to the dev_array */
|
|
|
|
if (strstr(word, "/mnt/") != NULL || strstr(word, "/media/") != NULL)
|
|
|
|
{
|
2015-09-01 13:34:40 +03:00
|
|
|
dev_array[size].path = word;
|
2014-02-20 13:42:02 +04:00
|
|
|
dev_array[size++].to_add = TRUE;
|
|
|
|
}
|
2016-01-14 17:55:25 +03:00
|
|
|
else
|
2016-07-21 17:51:42 +03:00
|
|
|
free(word);
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
/* delete removed devices */
|
|
|
|
count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
|
|
|
|
|
|
|
|
for (j = 0; j < count; j++)
|
|
|
|
{
|
|
|
|
BOOL dev_found = FALSE;
|
2016-07-21 17:51:42 +03:00
|
|
|
device_ext = (DEVICE_DRIVE_EXT*)ListDictionary_GetItemValue(
|
|
|
|
rdpdr->devman->devices, (void*)keys[j]);
|
2014-02-20 13:42:02 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!device_ext || !device_ext->path)
|
2015-06-03 14:38:47 +03:00
|
|
|
continue;
|
2014-02-20 13:42:02 +04:00
|
|
|
|
|
|
|
/* not plugable device */
|
2016-07-21 17:51:42 +03:00
|
|
|
if (strstr(device_ext->path, "/mnt/") == NULL
|
|
|
|
&& strstr(device_ext->path, "/media/") == NULL)
|
2014-02-20 13:42:02 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (strstr(device_ext->path, dev_array[i].path) != NULL)
|
|
|
|
{
|
|
|
|
dev_found = TRUE;
|
|
|
|
dev_array[i].to_add = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev_found)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
devman_unregister_device(rdpdr->devman, (void*)keys[j]);
|
2014-02-20 13:42:02 +04:00
|
|
|
ids[0] = keys[j];
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if ((error = rdpdr_send_device_list_remove_request(rdpdr, 1, ids)))
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
WLog_ERR(TAG, "rdpdr_send_device_list_remove_request failed with error %lu!",
|
|
|
|
error);
|
2015-08-28 12:17:20 +03:00
|
|
|
goto cleanup;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add new devices */
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
RDPDR_DRIVE* drive;
|
|
|
|
|
|
|
|
if (dev_array[i].to_add)
|
|
|
|
{
|
|
|
|
char* name;
|
2015-06-03 14:38:47 +03:00
|
|
|
drive = (RDPDR_DRIVE*) calloc(1, sizeof(RDPDR_DRIVE));
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!drive)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
2015-08-28 12:17:20 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto cleanup;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2014-02-20 13:42:02 +04:00
|
|
|
|
|
|
|
drive->Type = RDPDR_DTYP_FILESYSTEM;
|
2015-08-28 12:05:44 +03:00
|
|
|
drive->Path = dev_array[i].path;
|
|
|
|
dev_array[i].path = NULL;
|
2014-02-20 13:42:02 +04:00
|
|
|
name = strrchr(drive->Path, '/') + 1;
|
|
|
|
drive->Name = _strdup(name);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!drive->Name)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "_strdup failed!");
|
|
|
|
free(drive->Path);
|
|
|
|
free(drive);
|
2015-08-28 12:17:20 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto cleanup;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
|
|
|
if ((error = devman_load_device_service(rdpdr->devman, (RDPDR_DEVICE*)drive,
|
|
|
|
rdpdr->rdpcontext)))
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "devman_load_device_service failed!");
|
|
|
|
free(drive->Path);
|
|
|
|
free(drive->Name);
|
|
|
|
free(drive);
|
2015-08-28 12:17:20 +03:00
|
|
|
goto cleanup;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 12:17:20 +03:00
|
|
|
|
|
|
|
cleanup:
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-08-28 12:17:20 +03:00
|
|
|
for (i = 0; i < size; i++)
|
2016-07-21 17:51:42 +03:00
|
|
|
free(dev_array[i].path);
|
2015-08-28 12:17:20 +03:00
|
|
|
|
2015-09-07 13:16:03 +03:00
|
|
|
return error;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
void first_hotplug(rdpdrPlugin* rdpdr)
|
2016-02-26 21:31:14 +03:00
|
|
|
{
|
|
|
|
UINT error;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-02-26 21:31:14 +03:00
|
|
|
if ((error = handle_hotplug(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "handle_hotplug failed with error %lu!", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 12:35:44 +04:00
|
|
|
static void* drive_hotplug_thread_func(void* arg)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2014-02-20 13:42:02 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
|
|
|
int mfd;
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
int rv;
|
2016-03-02 04:10:22 +03:00
|
|
|
UINT error = 0;
|
2016-01-27 21:26:52 +03:00
|
|
|
DWORD status;
|
2014-03-26 00:37:47 +04:00
|
|
|
rdpdr = (rdpdrPlugin*) arg;
|
2014-02-20 13:42:02 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!(rdpdr->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
2016-01-27 21:26:52 +03:00
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2014-02-20 13:42:02 +04:00
|
|
|
|
|
|
|
mfd = open("/proc/mounts", O_RDONLY, 0);
|
2014-03-26 00:37:47 +04:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
if (mfd < 0)
|
|
|
|
{
|
2014-12-28 22:56:13 +03:00
|
|
|
WLog_ERR(TAG, "ERROR: Unable to open /proc/mounts.");
|
2016-01-27 21:26:52 +03:00
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2014-02-20 13:42:02 +04:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(mfd, &rfds);
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
while ((rv = select(mfd + 1, NULL, NULL, &rfds, &tv)) >= 0)
|
2014-02-20 13:42:02 +04:00
|
|
|
{
|
2016-01-27 21:26:52 +03:00
|
|
|
status = WaitForSingleObject(rdpdr->stopEvent, 0);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error);
|
|
|
|
goto out;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (status == WAIT_OBJECT_0)
|
2014-02-20 13:42:02 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (FD_ISSET(mfd, &rfds))
|
|
|
|
{
|
|
|
|
/* file /proc/mounts changed, handle this */
|
2015-06-03 14:38:47 +03:00
|
|
|
if ((error = handle_hotplug(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "handle_hotplug failed with error %lu!", error);
|
2016-01-27 21:26:52 +03:00
|
|
|
goto out;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2015-09-07 13:16:03 +03:00
|
|
|
else
|
|
|
|
rdpdr_send_device_list_announce_request(rdpdr, TRUE);
|
2014-02-20 13:42:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(mfd, &rfds);
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
out:
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
if (error && rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
|
|
|
"drive_hotplug_thread_func reported an error");
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-06-29 00:39:14 +03:00
|
|
|
CloseHandle(rdpdr->stopEvent);
|
2016-01-27 21:26:52 +03:00
|
|
|
ExitThread((DWORD)error);
|
|
|
|
return NULL;
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2016-01-27 21:26:52 +03:00
|
|
|
UINT error;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2014-03-26 00:37:47 +04:00
|
|
|
if (rdpdr->hotplugThread)
|
|
|
|
{
|
|
|
|
if (rdpdr->stopEvent)
|
|
|
|
SetEvent(rdpdr->stopEvent);
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(rdpdr->hotplugThread, INFINITE) == WAIT_FAILED)
|
2016-01-27 21:26:52 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-03-26 00:37:47 +04:00
|
|
|
rdpdr->hotplugThread = NULL;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-02-14 11:09:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_process_connect(rdpdrPlugin* rdpdr)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2014-02-10 10:06:11 +04:00
|
|
|
UINT32 index;
|
2012-11-09 04:01:52 +04:00
|
|
|
RDPDR_DEVICE* device;
|
|
|
|
rdpSettings* settings;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-10-17 23:42:51 +04:00
|
|
|
rdpdr->devman = devman_new(rdpdr);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!rdpdr->devman)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "devman_new failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-10-18 01:39:48 +04:00
|
|
|
settings = (rdpSettings*) rdpdr->channelEntryPoints.pExtendedData;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2014-12-30 15:34:11 +03:00
|
|
|
if (settings->ClientHostname)
|
2016-07-21 17:51:42 +03:00
|
|
|
strncpy(rdpdr->computerName, settings->ClientHostname,
|
|
|
|
sizeof(rdpdr->computerName) - 1);
|
2014-12-30 15:34:11 +03:00
|
|
|
else
|
2016-07-21 17:51:42 +03:00
|
|
|
strncpy(rdpdr->computerName, settings->ComputerName,
|
|
|
|
sizeof(rdpdr->computerName) - 1);
|
2012-10-07 18:21:54 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
for (index = 0; index < settings->DeviceCount; index++)
|
|
|
|
{
|
|
|
|
device = settings->DeviceArray[index];
|
2014-03-26 00:37:47 +04:00
|
|
|
|
|
|
|
if (device->Name && (strcmp(device->Name, "*") == 0))
|
2014-02-14 11:09:55 +04:00
|
|
|
{
|
2016-02-26 21:31:14 +03:00
|
|
|
first_hotplug(rdpdr);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!(rdpdr->hotplugThread = CreateThread(NULL, 0,
|
2016-07-21 17:51:42 +03:00
|
|
|
(LPTHREAD_START_ROUTINE) drive_hotplug_thread_func, rdpdr, 0, NULL)))
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-14 11:09:55 +04:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-26 00:37:47 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
if ((error = devman_load_device_service(rdpdr->devman, device,
|
|
|
|
rdpdr->rdpcontext)))
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "devman_load_device_service failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return error;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
static UINT rdpdr_process_server_announce_request(rdpdrPlugin* rdpdr,
|
|
|
|
wStream* s)
|
2011-08-05 06:24:46 +04:00
|
|
|
{
|
2016-01-28 14:25:44 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT16(s, rdpdr->versionMajor);
|
|
|
|
Stream_Read_UINT16(s, rdpdr->versionMinor);
|
|
|
|
Stream_Read_UINT32(s, rdpdr->clientID);
|
2014-05-07 02:19:54 +04:00
|
|
|
rdpdr->sequenceId++;
|
2016-01-24 17:21:06 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_send_client_announce_reply(rdpdrPlugin* rdpdr)
|
2011-08-05 06:24:46 +04:00
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
|
|
|
s = Stream_New(NULL, 12);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_CLIENTID_CONFIRM); /* PacketId (2 bytes) */
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT16(s, rdpdr->versionMajor);
|
|
|
|
Stream_Write_UINT16(s, rdpdr->versionMinor);
|
|
|
|
Stream_Write_UINT32(s, (UINT32) rdpdr->clientID);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
|
2011-08-05 06:24:46 +04:00
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2013-01-11 00:30:32 +04:00
|
|
|
WCHAR* computerNameW = NULL;
|
2012-02-10 05:04:27 +04:00
|
|
|
size_t computerNameLenW;
|
2011-08-05 06:24:46 +04:00
|
|
|
|
|
|
|
if (!rdpdr->computerName[0])
|
|
|
|
gethostname(rdpdr->computerName, sizeof(rdpdr->computerName) - 1);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
computerNameLenW = ConvertToUnicode(CP_UTF8, 0, rdpdr->computerName, -1,
|
|
|
|
&computerNameW, 0) * 2;
|
2013-10-17 22:19:33 +04:00
|
|
|
s = Stream_New(NULL, 16 + computerNameLenW + 2);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-08-05 06:24:46 +04:00
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_CLIENT_NAME); /* PacketId (2 bytes) */
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT32(s, 1); /* unicodeFlag, 0 for ASCII and 1 for Unicode */
|
|
|
|
Stream_Write_UINT32(s, 0); /* codePage, must be set to zero */
|
2016-07-21 17:51:42 +03:00
|
|
|
Stream_Write_UINT32(s, computerNameLenW +
|
|
|
|
2); /* computerNameLen, including null terminator */
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write(s, computerNameW, computerNameLenW);
|
|
|
|
Stream_Write_UINT16(s, 0); /* null terminator */
|
2012-10-09 07:21:26 +04:00
|
|
|
free(computerNameW);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2011-08-05 06:24:46 +04:00
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
static UINT rdpdr_process_server_clientid_confirm(rdpdrPlugin* rdpdr,
|
|
|
|
wStream* s)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 versionMajor;
|
|
|
|
UINT16 versionMinor;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 clientID;
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2016-01-28 14:25:44 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Read_UINT16(s, versionMajor);
|
|
|
|
Stream_Read_UINT16(s, versionMinor);
|
|
|
|
Stream_Read_UINT32(s, clientID);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
if (versionMajor != rdpdr->versionMajor || versionMinor != rdpdr->versionMinor)
|
|
|
|
{
|
|
|
|
rdpdr->versionMajor = versionMajor;
|
|
|
|
rdpdr->versionMinor = versionMinor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientID != rdpdr->clientID)
|
|
|
|
rdpdr->clientID = clientID;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
|
|
|
return CHANNEL_RC_OK;
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-07-21 17:51:42 +03:00
|
|
|
static UINT rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr,
|
|
|
|
BOOL userLoggedOn)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2012-02-10 05:04:27 +04:00
|
|
|
int i;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE c;
|
2013-10-18 00:30:36 +04:00
|
|
|
int pos;
|
|
|
|
int index;
|
2013-10-17 22:19:33 +04:00
|
|
|
wStream* s;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 count;
|
2011-08-05 18:44:06 +04:00
|
|
|
int data_len;
|
|
|
|
int count_pos;
|
2012-02-10 05:04:27 +04:00
|
|
|
DEVICE* device;
|
2013-10-18 00:30:36 +04:00
|
|
|
int keyCount;
|
|
|
|
ULONG_PTR* pKeys;
|
2013-10-17 22:19:33 +04:00
|
|
|
s = Stream_New(NULL, 256);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_ANNOUNCE); /* PacketId (2 bytes) */
|
2014-02-10 10:06:11 +04:00
|
|
|
count_pos = (int) Stream_GetPosition(s);
|
2011-08-05 18:44:06 +04:00
|
|
|
count = 0;
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Seek_UINT32(s); /* deviceCount */
|
2013-10-18 00:30:36 +04:00
|
|
|
pKeys = NULL;
|
|
|
|
keyCount = ListDictionary_GetKeys(rdpdr->devman->devices, &pKeys);
|
|
|
|
|
|
|
|
for (index = 0; index < keyCount; index++)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
device = (DEVICE*) ListDictionary_GetItemValue(rdpdr->devman->devices,
|
|
|
|
(void*) pKeys[index]);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. versionMinor 0x0005 doesn't send PAKID_CORE_USER_LOGGEDON
|
|
|
|
* so all devices should be sent regardless of user_loggedon
|
|
|
|
* 2. smartcard devices should be always sent
|
|
|
|
* 3. other devices are sent only after user_loggedon
|
|
|
|
*/
|
2012-11-21 18:30:06 +04:00
|
|
|
|
|
|
|
if ((rdpdr->versionMinor == 0x0005) ||
|
2016-07-21 17:51:42 +03:00
|
|
|
(device->type == RDPDR_DTYP_SMARTCARD) || userLoggedOn)
|
2011-08-05 18:44:06 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
data_len = (int)(device->data == NULL ? 0 : Stream_GetPosition(device->data));
|
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 20 + data_len))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2011-08-05 18:44:06 +04:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT32(s, device->type); /* deviceType */
|
|
|
|
Stream_Write_UINT32(s, device->id); /* deviceID */
|
|
|
|
strncpy((char*) Stream_Pointer(s), device->name, 8);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Peek_UINT8(s, c);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
if (c > 0x7F)
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT8(s, '_');
|
2011-08-05 18:44:06 +04:00
|
|
|
else
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Seek_UINT8(s);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write_UINT32(s, data_len);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2011-08-05 18:44:06 +04:00
|
|
|
if (data_len > 0)
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Write(s, Stream_Buffer(device->data), data_len);
|
2011-08-05 18:44:06 +04:00
|
|
|
|
|
|
|
count++;
|
2014-12-28 22:56:13 +03:00
|
|
|
WLog_INFO(TAG, "registered device #%d: %s (type=%d id=%d)",
|
2016-07-21 17:51:42 +03:00
|
|
|
count, device->name, device->type, device->id);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(pKeys);
|
2014-02-10 10:06:11 +04:00
|
|
|
pos = (int) Stream_GetPosition(s);
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_SetPosition(s, count_pos);
|
|
|
|
Stream_Write_UINT32(s, count);
|
|
|
|
Stream_SetPosition(s, pos);
|
|
|
|
Stream_SealLength(s);
|
2015-06-03 14:38:47 +03:00
|
|
|
return rdpdr_send(rdpdr, s);
|
2011-08-05 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_process_irp(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-05 21:54:40 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-01-28 14:05:14 +03:00
|
|
|
irp = irp_new(rdpdr->devman, s, &error);
|
2012-02-10 05:04:27 +04:00
|
|
|
|
2013-10-17 23:42:51 +04:00
|
|
|
if (!irp)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
2016-01-28 14:05:14 +03:00
|
|
|
WLog_ERR(TAG, "irp_new failed with %lu!", error);
|
|
|
|
return error;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2011-08-05 21:54:40 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
IFCALLRET(irp->device->IRPRequest, error, irp->device, irp);
|
2011-08-05 21:54:40 +04:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
if (error)
|
|
|
|
WLog_ERR(TAG, "device->IRPRequest failed with error %lu", error);
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
return error;
|
2011-08-05 21:54:40 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_process_init(rdpdrPlugin* rdpdr)
|
2014-04-06 00:57:31 +04:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int keyCount;
|
|
|
|
DEVICE* device;
|
|
|
|
ULONG_PTR* pKeys;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2014-04-06 00:57:31 +04:00
|
|
|
pKeys = NULL;
|
|
|
|
keyCount = ListDictionary_GetKeys(rdpdr->devman->devices, &pKeys);
|
|
|
|
|
|
|
|
for (index = 0; index < keyCount; index++)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
device = (DEVICE*) ListDictionary_GetItemValue(rdpdr->devman->devices,
|
|
|
|
(void*) pKeys[index]);
|
2015-06-03 14:38:47 +03:00
|
|
|
IFCALLRET(device->Init, error, device);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (error != CHANNEL_RC_OK)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Init failed!");
|
|
|
|
free(pKeys);
|
|
|
|
return error;
|
|
|
|
}
|
2014-04-06 00:57:31 +04:00
|
|
|
}
|
2014-04-06 01:15:17 +04:00
|
|
|
|
|
|
|
free(pKeys);
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-04-06 00:57:31 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_process_receive(rdpdrPlugin* rdpdr, wStream* s)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 component;
|
2014-12-28 22:56:13 +03:00
|
|
|
UINT16 packetId;
|
|
|
|
UINT32 deviceId;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 status;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!rdpdr || !s)
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_NULL_DATA;
|
|
|
|
|
2016-01-28 14:25:44 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
Stream_Read_UINT16(s, component); /* Component (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, packetId); /* PacketId (2 bytes) */
|
2011-08-04 19:22:58 +04:00
|
|
|
|
|
|
|
if (component == RDPDR_CTYP_CORE)
|
|
|
|
{
|
2014-12-28 22:56:13 +03:00
|
|
|
switch (packetId)
|
2011-08-04 19:22:58 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
case PAKID_CORE_SERVER_ANNOUNCE:
|
|
|
|
if ((error = rdpdr_process_server_announce_request(rdpdr, s)))
|
|
|
|
return error;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
if ((error = rdpdr_send_client_announce_reply(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_send_client_announce_reply failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
if ((error = rdpdr_send_client_name_request(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_send_client_name_request failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
if ((error = rdpdr_process_init(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_process_init failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
break;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case PAKID_CORE_SERVER_CAPABILITY:
|
|
|
|
if ((error = rdpdr_process_capability_request(rdpdr, s)))
|
|
|
|
return error;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
if ((error = rdpdr_send_capability_response(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_send_capability_response failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
break;
|
2016-01-27 21:26:52 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case PAKID_CORE_CLIENTID_CONFIRM:
|
|
|
|
if ((error = rdpdr_process_server_clientid_confirm(rdpdr, s)))
|
|
|
|
return error;
|
|
|
|
|
|
|
|
if ((error = rdpdr_send_device_list_announce_request(rdpdr, FALSE)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_send_device_list_announce_request failed with error %lu",
|
|
|
|
error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_USER_LOGGEDON:
|
|
|
|
if ((error = rdpdr_send_device_list_announce_request(rdpdr, TRUE)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_send_device_list_announce_request failed with error %lu",
|
|
|
|
error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAKID_CORE_DEVICE_REPLY:
|
|
|
|
|
|
|
|
/* connect to a specific resource */
|
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, deviceId);
|
|
|
|
Stream_Read_UINT32(s, status);
|
|
|
|
break;
|
2011-08-04 19:22:58 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case PAKID_CORE_DEVICE_IOREQUEST:
|
|
|
|
if ((error = rdpdr_process_irp(rdpdr, s)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_process_irp failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "RDPDR_CTYP_CORE unknown PacketId: 0x%04X", packetId);
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
break;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (component == RDPDR_CTYP_PRN)
|
|
|
|
{
|
2015-07-28 15:05:57 +03:00
|
|
|
switch (packetId)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
case PAKID_PRN_CACHE_DATA:
|
|
|
|
{
|
|
|
|
UINT32 eventID;
|
2015-07-28 15:05:57 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return ERROR_INVALID_DATA;
|
2015-07-28 15:05:57 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
Stream_Read_UINT32(s, eventID);
|
|
|
|
WLog_ERR(TAG,
|
|
|
|
"Ignoring unhandled message PAKID_PRN_CACHE_DATA (EventID: 0x%04X)", eventID);
|
|
|
|
}
|
|
|
|
break;
|
2016-01-27 21:26:52 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case PAKID_PRN_USING_XPS:
|
|
|
|
WLog_ERR(TAG, "Ignoring unhandled message PAKID_PRN_USING_XPS");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "Unknown printing component packetID: 0x%04X", packetId);
|
|
|
|
return ERROR_INVALID_DATA;
|
2015-07-28 15:05:57 +03:00
|
|
|
}
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
WLog_ERR(TAG, "Unknown message: Component: 0x%04X PacketId: 0x%04X", component,
|
|
|
|
packetId);
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-04 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 23:20:04 +04:00
|
|
|
|
|
|
|
/****************************************************************************************/
|
|
|
|
|
|
|
|
|
2015-01-20 14:31:26 +03:00
|
|
|
static wListDictionary* g_InitHandles = NULL;
|
|
|
|
static wListDictionary* g_OpenHandles = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT rdpdr_add_init_handle_data(void* pInitHandle, void* pUserData)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
if (!g_InitHandles)
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2013-10-17 23:20:04 +04:00
|
|
|
g_InitHandles = ListDictionary_New(TRUE);
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!g_InitHandles)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "ListDictionary_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ListDictionary_Add(g_InitHandles, pInitHandle, pUserData))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "ListDictionary_Add failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void* rdpdr_get_init_handle_data(void* pInitHandle)
|
|
|
|
{
|
|
|
|
void* pUserData = NULL;
|
|
|
|
pUserData = ListDictionary_GetItemValue(g_InitHandles, pInitHandle);
|
|
|
|
return pUserData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdpdr_remove_init_handle_data(void* pInitHandle)
|
|
|
|
{
|
|
|
|
ListDictionary_Remove(g_InitHandles, pInitHandle);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-01-20 14:31:26 +03:00
|
|
|
if (ListDictionary_Count(g_InitHandles) < 1)
|
|
|
|
{
|
|
|
|
ListDictionary_Free(g_InitHandles);
|
|
|
|
g_InitHandles = NULL;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT rdpdr_add_open_handle_data(DWORD openHandle, void* pUserData)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
void* pOpenHandle = (void*)(size_t) openHandle;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
|
|
|
if (!g_OpenHandles)
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2013-10-17 23:20:04 +04:00
|
|
|
g_OpenHandles = ListDictionary_New(TRUE);
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!g_OpenHandles)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "ListDictionary_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ListDictionary_Add(g_OpenHandles, pOpenHandle, pUserData))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "ListDictionary_Add failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void* rdpdr_get_open_handle_data(DWORD openHandle)
|
|
|
|
{
|
|
|
|
void* pUserData = NULL;
|
2016-07-21 17:51:42 +03:00
|
|
|
void* pOpenHandle = (void*)(size_t) openHandle;
|
2013-10-17 23:20:04 +04:00
|
|
|
pUserData = ListDictionary_GetItemValue(g_OpenHandles, pOpenHandle);
|
|
|
|
return pUserData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdpdr_remove_open_handle_data(DWORD openHandle)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
void* pOpenHandle = (void*)(size_t) openHandle;
|
2013-10-17 23:20:04 +04:00
|
|
|
ListDictionary_Remove(g_OpenHandles, pOpenHandle);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-01-20 14:31:26 +03:00
|
|
|
if (ListDictionary_Count(g_OpenHandles) < 1)
|
|
|
|
{
|
|
|
|
ListDictionary_Free(g_OpenHandles);
|
|
|
|
g_OpenHandles = NULL;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT rdpdr_send(rdpdrPlugin* rdpdr, wStream* s)
|
2013-10-17 22:19:33 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT status;
|
2013-10-17 23:42:51 +04:00
|
|
|
rdpdrPlugin* plugin = (rdpdrPlugin*) rdpdr;
|
2013-10-17 22:19:33 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!rdpdr || !s)
|
2016-01-27 21:26:52 +03:00
|
|
|
return CHANNEL_RC_NULL_DATA;
|
2016-01-24 17:21:06 +03:00
|
|
|
|
2013-10-17 22:19:33 +04:00
|
|
|
if (!plugin)
|
|
|
|
status = CHANNEL_RC_BAD_INIT_HANDLE;
|
|
|
|
else
|
2014-02-10 10:06:11 +04:00
|
|
|
{
|
2013-10-18 01:39:48 +04:00
|
|
|
status = plugin->channelEntryPoints.pVirtualChannelWrite(plugin->OpenHandle,
|
2016-07-21 17:51:42 +03:00
|
|
|
Stream_Buffer(s), (UINT32) Stream_GetPosition(s), s);
|
2014-02-10 10:06:11 +04:00
|
|
|
}
|
2013-10-17 22:19:33 +04:00
|
|
|
|
|
|
|
if (status != CHANNEL_RC_OK)
|
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2015-01-20 13:55:22 +03:00
|
|
|
WLog_ERR(TAG, "VirtualChannelWrite failed with %s [%08X]",
|
2016-07-21 17:51:42 +03:00
|
|
|
WTSErrorToString(status), status);
|
2013-10-17 22:19:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_virtual_channel_event_data_received(rdpdrPlugin* rdpdr,
|
2016-07-21 17:51:42 +03:00
|
|
|
void* pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
wStream* data_in;
|
|
|
|
|
|
|
|
if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
|
|
|
|
* This flag is only valid in server-to-client virtual channel traffic. It MUST be
|
|
|
|
* ignored in client-to-server data." Thus it would be best practice to cease data
|
|
|
|
* transmission. However, simply returning here avoids a crash.
|
|
|
|
*/
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dataFlags & CHANNEL_FLAG_FIRST)
|
|
|
|
{
|
2015-01-20 13:55:22 +03:00
|
|
|
if (rdpdr->data_in != NULL)
|
2013-11-04 04:10:33 +04:00
|
|
|
Stream_Free(rdpdr->data_in, TRUE);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->data_in = Stream_New(NULL, totalLength);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!rdpdr->data_in)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
data_in = rdpdr->data_in;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(data_in, (int) dataLength))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
|
2016-02-03 13:04:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2015-06-03 14:38:47 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2013-10-17 23:20:04 +04:00
|
|
|
Stream_Write(data_in, pData, dataLength);
|
|
|
|
|
|
|
|
if (dataFlags & CHANNEL_FLAG_LAST)
|
|
|
|
{
|
|
|
|
if (Stream_Capacity(data_in) != Stream_GetPosition(data_in))
|
|
|
|
{
|
2015-01-20 13:55:22 +03:00
|
|
|
WLog_ERR(TAG, "rdpdr_virtual_channel_event_data_received: read error");
|
2015-06-03 14:38:47 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->data_in = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
Stream_SealLength(data_in);
|
|
|
|
Stream_SetPosition(data_in, 0);
|
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (!MessageQueue_Post(rdpdr->queue, NULL, 0, (void*) data_in, NULL))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "MessageQueue_Post failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
static VOID VCAPITYPE rdpdr_virtual_channel_open_event(DWORD openHandle,
|
|
|
|
UINT event,
|
|
|
|
LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr = (rdpdrPlugin*) rdpdr_get_open_handle_data(openHandle);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
if (!rdpdr || !pData)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2015-01-20 13:55:22 +03:00
|
|
|
WLog_ERR(TAG, "rdpdr_virtual_channel_open_event: error no match");
|
2013-10-17 23:20:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_DATA_RECEIVED:
|
|
|
|
if ((error = rdpdr_virtual_channel_event_data_received(rdpdr, pData,
|
|
|
|
dataLength, totalLength, dataFlags)))
|
|
|
|
WLog_ERR(TAG,
|
|
|
|
"rdpdr_virtual_channel_event_data_received failed with error %lu!", error);
|
|
|
|
|
|
|
|
break;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_WRITE_COMPLETE:
|
|
|
|
Stream_Free((wStream*) pData, TRUE);
|
|
|
|
break;
|
2015-01-20 13:55:22 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_USER:
|
|
|
|
break;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
|
|
|
"rdpdr_virtual_channel_open_event reported an error");
|
2015-07-15 10:50:35 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* rdpdr_virtual_channel_client_thread(void* arg)
|
|
|
|
{
|
|
|
|
wStream* data;
|
|
|
|
wMessage message;
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr = (rdpdrPlugin*) arg;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-01-24 17:21:06 +03:00
|
|
|
if (!rdpdr)
|
|
|
|
{
|
2016-01-27 21:26:52 +03:00
|
|
|
ExitThread((DWORD) CHANNEL_RC_NULL_DATA);
|
2016-01-24 17:21:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if ((error = rdpdr_process_connect(rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_process_connect failed with error %lu!", error);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
|
|
|
"rdpdr_virtual_channel_client_thread reported an error");
|
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
ExitThread((DWORD) error);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2014-12-28 22:56:13 +03:00
|
|
|
if (!MessageQueue_Wait(rdpdr->queue))
|
2013-10-17 23:20:04 +04:00
|
|
|
break;
|
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
if (MessageQueue_Peek(rdpdr->queue, &message, TRUE))
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
if (message.id == WMQ_QUIT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (message.id == 0)
|
|
|
|
{
|
|
|
|
data = (wStream*) message.wParam;
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if ((error = rdpdr_process_receive(rdpdr, data)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_process_receive failed with error %lu!", error);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
|
|
|
"rdpdr_virtual_channel_client_thread reported an error");
|
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
ExitThread((DWORD) error);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-07-21 17:51:42 +03:00
|
|
|
static UINT rdpdr_virtual_channel_event_connected(rdpdrPlugin* rdpdr,
|
|
|
|
LPVOID pData, UINT32 dataLength)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
|
|
|
UINT32 status;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2013-11-04 04:10:33 +04:00
|
|
|
status = rdpdr->channelEntryPoints.pVirtualChannelOpen(rdpdr->InitHandle,
|
2016-07-21 17:51:42 +03:00
|
|
|
&rdpdr->OpenHandle, rdpdr->channelDef.name, rdpdr_virtual_channel_open_event);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
if (status != CHANNEL_RC_OK)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "pVirtualChannelOpen failed with %s [%08X]",
|
2016-07-21 17:51:42 +03:00
|
|
|
WTSErrorToString(status), status);
|
2016-01-27 21:26:52 +03:00
|
|
|
return status;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-01-27 21:26:52 +03:00
|
|
|
if ((error = rdpdr_add_open_handle_data(rdpdr->OpenHandle, rdpdr)))
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2015-06-03 14:38:47 +03:00
|
|
|
WLog_ERR(TAG, "rdpdr_add_open_handle_data failed with error %lu!", error);
|
|
|
|
return error;
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
rdpdr->queue = MessageQueue_New(NULL);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!rdpdr->queue)
|
|
|
|
{
|
2015-06-03 14:38:47 +03:00
|
|
|
WLog_ERR(TAG, "MessageQueue_New failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (!(rdpdr->thread = CreateThread(NULL, 0,
|
2016-07-21 17:51:42 +03:00
|
|
|
(LPTHREAD_START_ROUTINE) rdpdr_virtual_channel_client_thread, (void*) rdpdr, 0,
|
|
|
|
NULL)))
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2015-06-03 14:38:47 +03:00
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
2015-05-18 12:28:00 +03:00
|
|
|
}
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpdr_virtual_channel_event_disconnected(rdpdrPlugin* rdpdr)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
if (MessageQueue_PostQuit(rdpdr->queue, 0)
|
|
|
|
&& (WaitForSingleObject(rdpdr->thread, INFINITE) == WAIT_FAILED))
|
2016-01-27 21:26:52 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2014-12-27 23:20:29 +03:00
|
|
|
|
2015-01-20 13:55:22 +03:00
|
|
|
MessageQueue_Free(rdpdr->queue);
|
|
|
|
CloseHandle(rdpdr->thread);
|
|
|
|
rdpdr->queue = NULL;
|
|
|
|
rdpdr->thread = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if ((error = drive_hotplug_thread_terminate(rdpdr)))
|
2016-01-27 21:26:52 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "drive_hotplug_thread_terminate failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2014-02-14 11:09:55 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
error = rdpdr->channelEntryPoints.pVirtualChannelClose(rdpdr->OpenHandle);
|
2016-07-21 17:51:42 +03:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if (CHANNEL_RC_OK != error)
|
2015-01-20 13:55:22 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "pVirtualChannelClose failed with %s [%08X]",
|
2016-07-21 17:51:42 +03:00
|
|
|
WTSErrorToString(error), error);
|
2015-01-20 13:55:22 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (rdpdr->data_in)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
Stream_Free(rdpdr->data_in, TRUE);
|
|
|
|
rdpdr->data_in = NULL;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (rdpdr->devman)
|
|
|
|
{
|
|
|
|
devman_free(rdpdr->devman);
|
|
|
|
rdpdr->devman = NULL;
|
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_remove_open_handle_data(rdpdr->OpenHandle);
|
2015-06-03 14:38:47 +03:00
|
|
|
return error;
|
2015-01-20 13:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpdr_virtual_channel_event_terminated(rdpdrPlugin* rdpdr)
|
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr_remove_init_handle_data(rdpdr->InitHandle);
|
2014-12-03 22:17:27 +03:00
|
|
|
free(rdpdr);
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
static VOID VCAPITYPE rdpdr_virtual_channel_init_event(LPVOID pInitHandle,
|
|
|
|
UINT event,
|
|
|
|
LPVOID pData, UINT dataLength)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr = (rdpdrPlugin*) rdpdr_get_init_handle_data(pInitHandle);
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
if (!rdpdr)
|
2013-10-17 23:20:04 +04:00
|
|
|
{
|
2015-07-15 10:50:35 +03:00
|
|
|
WLog_ERR(TAG, "error no match");
|
2013-10-17 23:20:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_INITIALIZED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_CONNECTED:
|
|
|
|
if ((error = rdpdr_virtual_channel_event_connected(rdpdr, pData, dataLength)))
|
|
|
|
WLog_ERR(TAG, "rdpdr_virtual_channel_event_connected failed with error %lu!",
|
|
|
|
error);
|
2016-01-27 21:26:52 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
break;
|
2016-01-27 21:26:52 +03:00
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
case CHANNEL_EVENT_DISCONNECTED:
|
|
|
|
if ((error = rdpdr_virtual_channel_event_disconnected(rdpdr)))
|
|
|
|
WLog_ERR(TAG, "rdpdr_virtual_channel_event_disconnected failed with error %lu!",
|
|
|
|
error);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHANNEL_EVENT_TERMINATED:
|
|
|
|
rdpdr_virtual_channel_event_terminated(rdpdr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "unknown event %d!", event);
|
|
|
|
error = ERROR_INVALID_DATA;
|
|
|
|
break;
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
2015-07-15 10:50:35 +03:00
|
|
|
|
|
|
|
if (error && rdpdr->rdpcontext)
|
2016-07-21 17:51:42 +03:00
|
|
|
setChannelError(rdpdr->rdpcontext, error,
|
|
|
|
"rdpdr_virtual_channel_init_event reported an error");
|
2013-10-17 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 06:48:17 +04:00
|
|
|
/* rdpdr is always built-in */
|
2012-10-08 06:53:24 +04:00
|
|
|
#define VirtualChannelEntry rdpdr_VirtualChannelEntry
|
|
|
|
|
2014-03-04 03:28:31 +04:00
|
|
|
BOOL VCAPITYPE VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
|
2012-10-08 06:53:24 +04:00
|
|
|
{
|
2015-01-20 13:55:22 +03:00
|
|
|
UINT rc;
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdrPlugin* rdpdr;
|
2015-07-15 10:50:35 +03:00
|
|
|
CHANNEL_ENTRY_POINTS_FREERDP* pEntryPointsEx;
|
2014-02-17 02:27:36 +04:00
|
|
|
rdpdr = (rdpdrPlugin*) calloc(1, sizeof(rdpdrPlugin));
|
|
|
|
|
|
|
|
if (!rdpdr)
|
2015-06-03 14:38:47 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-10-08 06:53:24 +04:00
|
|
|
|
2013-11-04 04:10:33 +04:00
|
|
|
rdpdr->channelDef.options =
|
2016-07-21 17:51:42 +03:00
|
|
|
CHANNEL_OPTION_INITIALIZED |
|
|
|
|
CHANNEL_OPTION_ENCRYPT_RDP |
|
|
|
|
CHANNEL_OPTION_COMPRESS_RDP;
|
2013-11-04 04:10:33 +04:00
|
|
|
strcpy(rdpdr->channelDef.name, "rdpdr");
|
2014-05-07 02:19:54 +04:00
|
|
|
rdpdr->sequenceId = 0;
|
2015-07-15 10:50:35 +03:00
|
|
|
pEntryPointsEx = (CHANNEL_ENTRY_POINTS_FREERDP*) pEntryPoints;
|
|
|
|
|
|
|
|
if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP)) &&
|
2016-07-21 17:51:42 +03:00
|
|
|
(pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
rdpdr->rdpcontext = pEntryPointsEx->context;
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:51:42 +03:00
|
|
|
CopyMemory(&(rdpdr->channelEntryPoints), pEntryPoints,
|
|
|
|
sizeof(CHANNEL_ENTRY_POINTS_FREERDP));
|
2015-01-20 13:55:22 +03:00
|
|
|
rc = rdpdr->channelEntryPoints.pVirtualChannelInit(&rdpdr->InitHandle,
|
2016-07-21 17:51:42 +03:00
|
|
|
&rdpdr->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
|
|
|
|
rdpdr_virtual_channel_init_event);
|
2015-06-03 14:38:47 +03:00
|
|
|
|
2015-01-20 13:55:22 +03:00
|
|
|
if (CHANNEL_RC_OK != rc)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "pVirtualChannelInit failed with %s [%08X]",
|
2016-07-21 17:51:42 +03:00
|
|
|
WTSErrorToString(rc), rc);
|
2015-01-20 13:55:22 +03:00
|
|
|
free(rdpdr);
|
2015-06-03 14:38:47 +03:00
|
|
|
return FALSE;
|
2015-01-20 13:55:22 +03:00
|
|
|
}
|
2013-10-17 23:20:04 +04:00
|
|
|
|
2015-06-03 14:38:47 +03:00
|
|
|
if ((rc = rdpdr_add_init_handle_data(rdpdr->InitHandle, (void*) rdpdr)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "rdpdr_add_init_handle_data failed with error %lu!", rc);
|
|
|
|
free(rdpdr);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2012-10-08 06:53:24 +04:00
|
|
|
}
|