2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * Device Redirection Virtual Channel
5 * Copyright 2010-2011 Vic Lee
6 * Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
29 #include <winpr/crt.h>
30 #include <winpr/stream.h>
32 #include <freerdp/types.h>
33 #include <freerdp/constants.h>
34 #include <freerdp/channels/log.h>
35 #include <freerdp/channels/rdpdr.h>
41 #include <sys/types.h>
50 #include "rdpdr_capabilities.h"
55 #include "rdpdr_main.h"
57 typedef struct _DEVICE_DRIVE_EXT DEVICE_DRIVE_EXT;
59 struct _DEVICE_DRIVE_EXT
65 static void rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL userLoggedOn);
67 static void rdpdr_send_device_list_remove_request(rdpdrPlugin* rdpdr, UINT32 count, UINT32 ids[])
72 s = Stream_New(NULL, 256);
74 Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
75 Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_REMOVE);
76 Stream_Write_UINT32(s, count);
78 for (i = 0; i < count; i++)
79 Stream_Write_UINT32(s, ids[i]);
88 LRESULT CALLBACK hotplug_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
91 PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
93 rdpdr = (rdpdrPlugin *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
100 case DBT_DEVICEARRIVAL:
101 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
103 PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
104 DWORD unitmask = lpdbv->dbcv_unitmask;
106 char drive_path[4] = { 'c', ':', '/', '\0'};
108 for (i = 0; i < 26; i++)
114 drive_path[0] = 'A' + i;
116 drive = (RDPDR_DRIVE*) malloc(sizeof(RDPDR_DRIVE));
117 ZeroMemory(drive, sizeof(RDPDR_DRIVE));
119 drive->Type = RDPDR_DTYP_FILESYSTEM;
121 drive->Path = _strdup(drive_path);
122 drive_path[1] = '\0';
123 drive->Name = _strdup(drive_path);
124 devman_load_device_service(rdpdr->devman, (RDPDR_DEVICE *)drive);
125 rdpdr_send_device_list_announce_request(rdpdr, TRUE);
127 unitmask = unitmask >> 1;
132 case DBT_DEVICEREMOVECOMPLETE:
133 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
135 PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
136 DWORD unitmask = lpdbv->dbcv_unitmask;
138 char drive_name_upper, drive_name_lower;
141 DEVICE_DRIVE_EXT *device_ext;
144 for (i = 0; i < 26; i++)
148 drive_name_upper = 'A' + i;
149 drive_name_lower = 'a' + i;
151 count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
153 for (j = 0; j < count; j++)
155 device_ext = (DEVICE_DRIVE_EXT *)ListDictionary_GetItemValue(rdpdr->devman->devices, (void *)keys[j]);
156 if (device_ext->path[0] == drive_name_upper || device_ext->path[0] == drive_name_lower)
158 devman_unregister_device(rdpdr->devman, (void *)keys[j]);
160 rdpdr_send_device_list_remove_request(rdpdr, 1, ids);
165 unitmask = unitmask >> 1;
176 return DefWindowProc(hWnd, Msg, wParam, lParam);
178 return DefWindowProc(hWnd, Msg, wParam, lParam);
181 static void* drive_hotplug_thread_func(void* arg)
188 DEV_BROADCAST_HANDLE NotificationFilter;
189 HDEVNOTIFY hDevNotify;
191 rdpdr = (rdpdrPlugin *)arg;
193 /* init windows class */
194 wnd_cls.cbSize = sizeof(WNDCLASSEX);
195 wnd_cls.style = CS_HREDRAW | CS_VREDRAW;
196 wnd_cls.lpfnWndProc = hotplug_proc;
197 wnd_cls.cbClsExtra = 0;
198 wnd_cls.cbWndExtra = 0;
199 wnd_cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
200 wnd_cls.hCursor = NULL;
201 wnd_cls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
202 wnd_cls.lpszMenuName = NULL;
203 wnd_cls.lpszClassName = L"DRIVE_HOTPLUG";
204 wnd_cls.hInstance = NULL;
205 wnd_cls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
206 RegisterClassEx(&wnd_cls);
209 hwnd = CreateWindowEx(0, L"DRIVE_HOTPLUG", NULL,
211 NULL, NULL, NULL, NULL);
212 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)rdpdr);
214 rdpdr->hotplug_wnd = hwnd;
215 /* register device interface to hwnd */
216 NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HANDLE);
217 NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
218 hDevNotify = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
221 while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0)
229 TranslateMessage(&msg);
230 DispatchMessage(&msg);
234 UnregisterDeviceNotification(hDevNotify);
239 static void drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
241 if (rdpdr->hotplug_wnd)
242 PostMessage(rdpdr->hotplug_wnd, WM_QUIT, 0, 0);
247 #define MAX_USB_DEVICES 100
249 typedef struct _hotplug_dev {
254 static char* next_line(FILE* fd, size_t* len)
279 lrbuf[(*len)] = '\0';
287 newsiz = lrsiz + 4096;
288 newbuf = realloc(lrbuf, newsiz);
298 lrbuf[(*len)] = '\0';
307 static char* get_word(char* str, unsigned int* offset)
314 if (*offset >= strlen(str))
320 while (*tmp != ' ' && *tmp != '\0')
326 /* in case there are more than one space between words */
327 while (*(str + *offset) == ' ')
330 word = malloc(wlen + 1);
334 CopyMemory(word, p, wlen);
341 static void handle_hotplug(rdpdrPlugin* rdpdr)
349 hotplug_dev dev_array[MAX_USB_DEVICES];
354 DEVICE_DRIVE_EXT *device_ext;
358 f = fopen("/proc/mounts", "r");
364 while ((line = next_line(f, &len)))
367 while ((word = get_word(line, &wlen)))
369 /* copy hotpluged device mount point to the dev_array */
370 if (strstr(word, "/mnt/") != NULL || strstr(word, "/media/") != NULL)
372 dev_array[size].path = strdup(word);
373 dev_array[size++].to_add = TRUE;
382 /* delete removed devices */
383 count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
385 for (j = 0; j < count; j++)
387 BOOL dev_found = FALSE;
389 device_ext = (DEVICE_DRIVE_EXT *)ListDictionary_GetItemValue(rdpdr->devman->devices, (void *)keys[j]);
391 /* not plugable device */
392 if (strstr(device_ext->path, "/mnt/") == NULL && strstr(device_ext->path, "/media/") == NULL)
395 for (i = 0; i < size; i++)
397 if (strstr(device_ext->path, dev_array[i].path) != NULL)
400 dev_array[i].to_add = FALSE;
407 devman_unregister_device(rdpdr->devman, (void *)keys[j]);
409 rdpdr_send_device_list_remove_request(rdpdr, 1, ids);
413 /* add new devices */
414 for (i = 0; i < size; i++)
418 if (dev_array[i].to_add)
422 drive = (RDPDR_DRIVE*) malloc(sizeof(RDPDR_DRIVE));
423 ZeroMemory(drive, sizeof(RDPDR_DRIVE));
425 drive->Type = RDPDR_DTYP_FILESYSTEM;
427 drive->Path = _strdup(dev_array[i].path);
428 name = strrchr(drive->Path, '/') + 1;
429 drive->Name = _strdup(name);
430 devman_load_device_service(rdpdr->devman, (RDPDR_DEVICE *)drive);
433 free(dev_array[i].path);
434 dev_array[i].path = NULL;
436 rdpdr_send_device_list_announce_request(rdpdr, TRUE);
439 static void* drive_hotplug_thread_func(void* arg)
447 rdpdr = (rdpdrPlugin*) arg;
449 rdpdr->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
451 mfd = open("/proc/mounts", O_RDONLY, 0);
455 WLog_ERR(TAG, "ERROR: Unable to open /proc/mounts.");
464 handle_hotplug(rdpdr);
466 while ((rv = select(mfd+1, NULL, NULL, &rfds, &tv)) >= 0)
468 if (WaitForSingleObject(rdpdr->stopEvent, 0) == WAIT_OBJECT_0)
471 if (FD_ISSET(mfd, &rfds))
473 /* file /proc/mounts changed, handle this */
474 handle_hotplug(rdpdr);
486 static void drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
488 if (rdpdr->hotplugThread)
490 if (rdpdr->stopEvent)
491 SetEvent(rdpdr->stopEvent);
493 WaitForSingleObject(rdpdr->hotplugThread, INFINITE);
494 rdpdr->hotplugThread = NULL;
501 static void rdpdr_process_connect(rdpdrPlugin* rdpdr)
504 RDPDR_DEVICE* device;
505 rdpSettings* settings;
507 rdpdr->devman = devman_new(rdpdr);
508 settings = (rdpSettings*) rdpdr->channelEntryPoints.pExtendedData;
510 strncpy(rdpdr->computerName, settings->ComputerName, sizeof(rdpdr->computerName) - 1);
512 for (index = 0; index < settings->DeviceCount; index++)
514 device = settings->DeviceArray[index];
516 if (device->Name && (strcmp(device->Name, "*") == 0))
518 rdpdr->hotplugThread = CreateThread(NULL, 0,
519 (LPTHREAD_START_ROUTINE) drive_hotplug_thread_func, rdpdr, 0, NULL);
523 devman_load_device_service(rdpdr->devman, device);
527 static void rdpdr_process_server_announce_request(rdpdrPlugin* rdpdr, wStream* s)
529 Stream_Read_UINT16(s, rdpdr->versionMajor);
530 Stream_Read_UINT16(s, rdpdr->versionMinor);
531 Stream_Read_UINT32(s, rdpdr->clientID);
536 static void rdpdr_send_client_announce_reply(rdpdrPlugin* rdpdr)
540 s = Stream_New(NULL, 12);
542 Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
543 Stream_Write_UINT16(s, PAKID_CORE_CLIENTID_CONFIRM); /* PacketId (2 bytes) */
545 Stream_Write_UINT16(s, rdpdr->versionMajor);
546 Stream_Write_UINT16(s, rdpdr->versionMinor);
547 Stream_Write_UINT32(s, (UINT32) rdpdr->clientID);
549 rdpdr_send(rdpdr, s);
552 static void rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
555 WCHAR* computerNameW = NULL;
556 size_t computerNameLenW;
558 if (!rdpdr->computerName[0])
559 gethostname(rdpdr->computerName, sizeof(rdpdr->computerName) - 1);
561 computerNameLenW = ConvertToUnicode(CP_UTF8, 0, rdpdr->computerName, -1, &computerNameW, 0) * 2;
563 s = Stream_New(NULL, 16 + computerNameLenW + 2);
565 Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
566 Stream_Write_UINT16(s, PAKID_CORE_CLIENT_NAME); /* PacketId (2 bytes) */
568 Stream_Write_UINT32(s, 1); /* unicodeFlag, 0 for ASCII and 1 for Unicode */
569 Stream_Write_UINT32(s, 0); /* codePage, must be set to zero */
570 Stream_Write_UINT32(s, computerNameLenW + 2); /* computerNameLen, including null terminator */
571 Stream_Write(s, computerNameW, computerNameLenW);
572 Stream_Write_UINT16(s, 0); /* null terminator */
576 rdpdr_send(rdpdr, s);
579 static void rdpdr_process_server_clientid_confirm(rdpdrPlugin* rdpdr, wStream* s)
585 Stream_Read_UINT16(s, versionMajor);
586 Stream_Read_UINT16(s, versionMinor);
587 Stream_Read_UINT32(s, clientID);
589 if (versionMajor != rdpdr->versionMajor || versionMinor != rdpdr->versionMinor)
591 rdpdr->versionMajor = versionMajor;
592 rdpdr->versionMinor = versionMinor;
595 if (clientID != rdpdr->clientID)
597 rdpdr->clientID = clientID;
601 static void rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL userLoggedOn)
615 s = Stream_New(NULL, 256);
617 Stream_Write_UINT16(s, RDPDR_CTYP_CORE); /* Component (2 bytes) */
618 Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_ANNOUNCE); /* PacketId (2 bytes) */
620 count_pos = (int) Stream_GetPosition(s);
623 Stream_Seek_UINT32(s); /* deviceCount */
626 keyCount = ListDictionary_GetKeys(rdpdr->devman->devices, &pKeys);
628 for (index = 0; index < keyCount; index++)
630 device = (DEVICE*) ListDictionary_GetItemValue(rdpdr->devman->devices, (void*) pKeys[index]);
633 * 1. versionMinor 0x0005 doesn't send PAKID_CORE_USER_LOGGEDON
634 * so all devices should be sent regardless of user_loggedon
635 * 2. smartcard devices should be always sent
636 * 3. other devices are sent only after user_loggedon
639 if ((rdpdr->versionMinor == 0x0005) ||
640 (device->type == RDPDR_DTYP_SMARTCARD) || userLoggedOn)
642 data_len = (int) (device->data == NULL ? 0 : Stream_GetPosition(device->data));
643 Stream_EnsureRemainingCapacity(s, 20 + data_len);
645 Stream_Write_UINT32(s, device->type); /* deviceType */
646 Stream_Write_UINT32(s, device->id); /* deviceID */
647 strncpy((char*) Stream_Pointer(s), device->name, 8);
649 for (i = 0; i < 8; i++)
651 Stream_Peek_UINT8(s, c);
654 Stream_Write_UINT8(s, '_');
656 Stream_Seek_UINT8(s);
659 Stream_Write_UINT32(s, data_len);
662 Stream_Write(s, Stream_Buffer(device->data), data_len);
665 WLog_INFO(TAG, "registered device #%d: %s (type=%d id=%d)",
666 count, device->name, device->type, device->id);
673 pos = (int) Stream_GetPosition(s);
674 Stream_SetPosition(s, count_pos);
675 Stream_Write_UINT32(s, count);
676 Stream_SetPosition(s, pos);
677 Stream_SealLength(s);
679 rdpdr_send(rdpdr, s);
682 static BOOL rdpdr_process_irp(rdpdrPlugin* rdpdr, wStream* s)
686 irp = irp_new(rdpdr->devman, s);
691 IFCALL(irp->device->IRPRequest, irp->device, irp);
696 static void rdpdr_process_init(rdpdrPlugin* rdpdr)
704 keyCount = ListDictionary_GetKeys(rdpdr->devman->devices, &pKeys);
706 for (index = 0; index < keyCount; index++)
708 device = (DEVICE*) ListDictionary_GetItemValue(rdpdr->devman->devices, (void*) pKeys[index]);
710 IFCALL(device->Init, device);
716 static void rdpdr_process_receive(rdpdrPlugin* rdpdr, wStream* s)
723 Stream_Read_UINT16(s, component); /* Component (2 bytes) */
724 Stream_Read_UINT16(s, packetId); /* PacketId (2 bytes) */
726 if (component == RDPDR_CTYP_CORE)
730 case PAKID_CORE_SERVER_ANNOUNCE:
731 rdpdr_process_server_announce_request(rdpdr, s);
732 rdpdr_send_client_announce_reply(rdpdr);
733 rdpdr_send_client_name_request(rdpdr);
734 rdpdr_process_init(rdpdr);
737 case PAKID_CORE_SERVER_CAPABILITY:
738 rdpdr_process_capability_request(rdpdr, s);
739 rdpdr_send_capability_response(rdpdr);
742 case PAKID_CORE_CLIENTID_CONFIRM:
743 rdpdr_process_server_clientid_confirm(rdpdr, s);
744 rdpdr_send_device_list_announce_request(rdpdr, FALSE);
747 case PAKID_CORE_USER_LOGGEDON:
748 rdpdr_send_device_list_announce_request(rdpdr, TRUE);
751 case PAKID_CORE_DEVICE_REPLY:
752 /* connect to a specific resource */
753 Stream_Read_UINT32(s, deviceId);
754 Stream_Read_UINT32(s, status);
757 case PAKID_CORE_DEVICE_IOREQUEST:
758 if (rdpdr_process_irp(rdpdr, s))
763 WLog_ERR(TAG, "rdpdr_process_receive: RDPDR_CTYP_CORE unknown PacketId: 0x%04X", packetId);
768 else if (component == RDPDR_CTYP_PRN)
770 WLog_ERR(TAG, "rdpdr_process_receive: RDPDR_CTYP_PRN unknown PacketId: 0x%04X", packetId);
774 WLog_ERR(TAG, "rdpdr_process_receive: unknown message: Component: 0x%04X PacketId: 0x%04X",
775 component, packetId);
778 Stream_Free(s, TRUE);
782 /****************************************************************************************/
785 static wListDictionary* g_InitHandles;
786 static wListDictionary* g_OpenHandles;
788 void rdpdr_add_init_handle_data(void* pInitHandle, void* pUserData)
791 g_InitHandles = ListDictionary_New(TRUE);
793 ListDictionary_Add(g_InitHandles, pInitHandle, pUserData);
796 void* rdpdr_get_init_handle_data(void* pInitHandle)
798 void* pUserData = NULL;
799 pUserData = ListDictionary_GetItemValue(g_InitHandles, pInitHandle);
803 void rdpdr_remove_init_handle_data(void* pInitHandle)
805 ListDictionary_Remove(g_InitHandles, pInitHandle);
808 void rdpdr_add_open_handle_data(DWORD openHandle, void* pUserData)
810 void* pOpenHandle = (void*) (size_t) openHandle;
813 g_OpenHandles = ListDictionary_New(TRUE);
815 ListDictionary_Add(g_OpenHandles, pOpenHandle, pUserData);
818 void* rdpdr_get_open_handle_data(DWORD openHandle)
820 void* pUserData = NULL;
821 void* pOpenHandle = (void*) (size_t) openHandle;
822 pUserData = ListDictionary_GetItemValue(g_OpenHandles, pOpenHandle);
826 void rdpdr_remove_open_handle_data(DWORD openHandle)
828 void* pOpenHandle = (void*) (size_t) openHandle;
829 ListDictionary_Remove(g_OpenHandles, pOpenHandle);
832 int rdpdr_send(rdpdrPlugin* rdpdr, wStream* s)
835 rdpdrPlugin* plugin = (rdpdrPlugin*) rdpdr;
839 status = CHANNEL_RC_BAD_INIT_HANDLE;
843 status = plugin->channelEntryPoints.pVirtualChannelWrite(plugin->OpenHandle,
844 Stream_Buffer(s), (UINT32) Stream_GetPosition(s), s);
847 if (status != CHANNEL_RC_OK)
849 Stream_Free(s, TRUE);
850 WLog_ERR(TAG, "rdpdr_send: VirtualChannelWrite failed %d", status);
856 static void rdpdr_virtual_channel_event_data_received(rdpdrPlugin* rdpdr,
857 void* pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
861 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
864 * According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
865 * This flag is only valid in server-to-client virtual channel traffic. It MUST be
866 * ignored in client-to-server data." Thus it would be best practice to cease data
867 * transmission. However, simply returning here avoids a crash.
872 if (dataFlags & CHANNEL_FLAG_FIRST)
875 Stream_Free(rdpdr->data_in, TRUE);
877 rdpdr->data_in = Stream_New(NULL, totalLength);
880 data_in = rdpdr->data_in;
881 Stream_EnsureRemainingCapacity(data_in, (int) dataLength);
882 Stream_Write(data_in, pData, dataLength);
884 if (dataFlags & CHANNEL_FLAG_LAST)
886 if (Stream_Capacity(data_in) != Stream_GetPosition(data_in))
888 WLog_ERR(TAG, "rdpdr_virtual_channel_event_data_received: read error\n");
891 rdpdr->data_in = NULL;
892 Stream_SealLength(data_in);
893 Stream_SetPosition(data_in, 0);
895 MessageQueue_Post(rdpdr->queue, NULL, 0, (void*) data_in, NULL);
899 static VOID VCAPITYPE rdpdr_virtual_channel_open_event(DWORD openHandle, UINT event,
900 LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
904 rdpdr = (rdpdrPlugin*) rdpdr_get_open_handle_data(openHandle);
908 WLog_ERR(TAG, "rdpdr_virtual_channel_open_event: error no match\n");
914 case CHANNEL_EVENT_DATA_RECEIVED:
915 rdpdr_virtual_channel_event_data_received(rdpdr, pData, dataLength, totalLength, dataFlags);
918 case CHANNEL_EVENT_WRITE_COMPLETE:
919 Stream_Free((wStream*) pData, TRUE);
924 static void* rdpdr_virtual_channel_client_thread(void* arg)
928 rdpdrPlugin* rdpdr = (rdpdrPlugin*) arg;
930 rdpdr_process_connect(rdpdr);
934 if (!MessageQueue_Wait(rdpdr->queue))
937 if (MessageQueue_Peek(rdpdr->queue, &message, TRUE))
939 if (message.id == WMQ_QUIT)
944 data = (wStream*) message.wParam;
945 rdpdr_process_receive(rdpdr, data);
954 static void rdpdr_virtual_channel_event_connected(rdpdrPlugin* rdpdr, LPVOID pData, UINT32 dataLength)
958 status = rdpdr->channelEntryPoints.pVirtualChannelOpen(rdpdr->InitHandle,
959 &rdpdr->OpenHandle, rdpdr->channelDef.name, rdpdr_virtual_channel_open_event);
961 rdpdr_add_open_handle_data(rdpdr->OpenHandle, rdpdr);
963 if (status != CHANNEL_RC_OK)
965 WLog_ERR(TAG, "rdpdr_virtual_channel_event_connected: open failed: status: %d\n", status);
969 rdpdr->queue = MessageQueue_New(NULL);
971 rdpdr->thread = CreateThread(NULL, 0,
972 (LPTHREAD_START_ROUTINE) rdpdr_virtual_channel_client_thread, (void*) rdpdr, 0, NULL);
975 static void rdpdr_virtual_channel_event_terminated(rdpdrPlugin* rdpdr)
979 MessageQueue_PostQuit(rdpdr->queue, 0);
980 WaitForSingleObject(rdpdr->thread, INFINITE);
982 MessageQueue_Free(rdpdr->queue);
985 CloseHandle(rdpdr->thread);
986 rdpdr->thread = NULL;
989 drive_hotplug_thread_terminate(rdpdr);
991 rdpdr->channelEntryPoints.pVirtualChannelClose(rdpdr->OpenHandle);
995 Stream_Free(rdpdr->data_in, TRUE);
996 rdpdr->data_in = NULL;
1001 devman_free(rdpdr->devman);
1002 rdpdr->devman = NULL;
1005 rdpdr_remove_open_handle_data(rdpdr->OpenHandle);
1006 rdpdr_remove_init_handle_data(rdpdr->InitHandle);
1011 static VOID VCAPITYPE rdpdr_virtual_channel_init_event(LPVOID pInitHandle, UINT event, LPVOID pData, UINT dataLength)
1015 rdpdr = (rdpdrPlugin*) rdpdr_get_init_handle_data(pInitHandle);
1019 WLog_ERR(TAG, "rdpdr_virtual_channel_init_event: error no match");
1025 case CHANNEL_EVENT_CONNECTED:
1026 rdpdr_virtual_channel_event_connected(rdpdr, pData, dataLength);
1029 case CHANNEL_EVENT_DISCONNECTED:
1032 case CHANNEL_EVENT_TERMINATED:
1033 rdpdr_virtual_channel_event_terminated(rdpdr);
1038 /* rdpdr is always built-in */
1039 #define VirtualChannelEntry rdpdr_VirtualChannelEntry
1041 BOOL VCAPITYPE VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
1045 rdpdr = (rdpdrPlugin*) calloc(1, sizeof(rdpdrPlugin));
1050 rdpdr->channelDef.options =
1051 CHANNEL_OPTION_INITIALIZED |
1052 CHANNEL_OPTION_ENCRYPT_RDP |
1053 CHANNEL_OPTION_COMPRESS_RDP;
1055 strcpy(rdpdr->channelDef.name, "rdpdr");
1057 rdpdr->sequenceId = 0;
1059 CopyMemory(&(rdpdr->channelEntryPoints), pEntryPoints, sizeof(CHANNEL_ENTRY_POINTS_FREERDP));
1061 rdpdr->channelEntryPoints.pVirtualChannelInit(&rdpdr->InitHandle,
1062 &rdpdr->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000, rdpdr_virtual_channel_init_event);
1064 rdpdr_add_init_handle_data(rdpdr->InitHandle, (void*) rdpdr);