FreeRDP
Loading...
Searching...
No Matches
urbdrc_main.c
1
21#include <winpr/assert.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26
27#include <winpr/pool.h>
28#include <winpr/print.h>
29
30#include <winpr/crt.h>
31#include <winpr/synch.h>
32#include <winpr/string.h>
33#include <winpr/cmdline.h>
34
35#include <freerdp/dvc.h>
36#include <freerdp/addin.h>
37#include <freerdp/channels/log.h>
38#include <freerdp/channels/urbdrc.h>
39
40#include "urbdrc_types.h"
41#include "urbdrc_main.h"
42#include "data_transfer.h"
43
44#include <urbdrc_helpers.h>
45
46static IWTSVirtualChannel* get_channel(IUDEVMAN* idevman)
47{
48 IWTSVirtualChannelManager* channel_mgr = nullptr;
49 URBDRC_PLUGIN* urbdrc = nullptr;
50
51 if (!idevman)
52 return nullptr;
53
54 urbdrc = (URBDRC_PLUGIN*)idevman->plugin;
55
56 if (!urbdrc || !urbdrc->listener_callback)
57 return nullptr;
58
59 channel_mgr = urbdrc->listener_callback->channel_mgr;
60
61 if (!channel_mgr)
62 return nullptr;
63
64 return channel_mgr->FindChannelById(channel_mgr, idevman->controlChannelId);
65}
66
67static int func_container_id_generate(IUDEVICE* pdev, char* strContainerId)
68{
69 char* p = nullptr;
70 char* path = nullptr;
71 UINT8 containerId[17] = WINPR_C_ARRAY_INIT;
72 UINT16 idVendor = 0;
73 UINT16 idProduct = 0;
74 idVendor = (UINT16)pdev->query_device_descriptor(pdev, ID_VENDOR);
75 idProduct = (UINT16)pdev->query_device_descriptor(pdev, ID_PRODUCT);
76 path = pdev->getPath(pdev);
77
78 if (strlen(path) > 8)
79 p = (path + strlen(path)) - 8;
80 else
81 p = path;
82
83 (void)sprintf_s((char*)containerId, sizeof(containerId), "%04" PRIX16 "%04" PRIX16 "%s",
84 idVendor, idProduct, p);
85 /* format */
86 (void)sprintf_s(strContainerId, DEVICE_CONTAINER_STR_SIZE,
87 "{%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
88 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
89 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "}",
90 containerId[0], containerId[1], containerId[2], containerId[3], containerId[4],
91 containerId[5], containerId[6], containerId[7], containerId[8], containerId[9],
92 containerId[10], containerId[11], containerId[12], containerId[13],
93 containerId[14], containerId[15]);
94 return 0;
95}
96
97static int func_instance_id_generate(IUDEVICE* pdev, char* strInstanceId, size_t len)
98{
99 char instanceId[17] = WINPR_C_ARRAY_INIT;
100 (void)sprintf_s(instanceId, sizeof(instanceId), "\\%s", pdev->getPath(pdev));
101 /* format */
102 (void)sprintf_s(strInstanceId, len,
103 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
104 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
105 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "",
106 instanceId[0], instanceId[1], instanceId[2], instanceId[3], instanceId[4],
107 instanceId[5], instanceId[6], instanceId[7], instanceId[8], instanceId[9],
108 instanceId[10], instanceId[11], instanceId[12], instanceId[13], instanceId[14],
109 instanceId[15]);
110 return 0;
111}
112
113/* [MS-RDPEUSB] 2.2.3.2 Interface Manipulation Exchange Capabilities Response
114 * (RIM_EXCHANGE_CAPABILITY_RESPONSE) */
115static UINT urbdrc_send_capability_response(GENERIC_CHANNEL_CALLBACK* callback, UINT32 MessageId,
116 UINT32 Version)
117{
118 const UINT32 InterfaceId = ((STREAM_ID_NONE << 30) | CAPABILITIES_NEGOTIATOR);
119 wStream* out = create_shared_message_header_with_functionid(InterfaceId, MessageId, Version, 4);
120
121 if (!out)
122 return ERROR_OUTOFMEMORY;
123
124 Stream_Write_UINT32(out, 0x00000000); /* HRESULT */
125 return stream_write_and_free(callback->plugin, callback->channel, out);
126}
127
133static UINT urbdrc_process_capability_request(GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
134 UINT32 MessageId)
135{
136 WINPR_ASSERT(callback);
137 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
138 WINPR_ASSERT(urbdrc);
139
140 if (!callback || !s)
141 return ERROR_INVALID_PARAMETER;
142
143 if (!Stream_CheckAndLogRequiredLengthWLog(urbdrc->log, s, 4))
144 return ERROR_INVALID_DATA;
145
146 UINT32 Version = Stream_Get_UINT32(s);
147
148 if (Version > RIM_CAPABILITY_VERSION_01)
149 {
150 WLog_Print(urbdrc->log, WLOG_WARN, "Unknown capability version %" PRIu32 ", expected %d",
151 Version, RIM_CAPABILITY_VERSION_01);
152 Version = RIM_CAPABILITY_VERSION_01;
153 }
154
155 return urbdrc_send_capability_response(callback, MessageId, Version);
156}
157
158/* [MS-RDPEUSB] 2.2.5.1 Channel Created Message (CHANNEL_CREATED) */
159static UINT urbdrc_send_channel_created(GENERIC_CHANNEL_CALLBACK* callback, UINT32 MessageId,
160 UINT32 MajorVersion, UINT32 MinorVersion,
161 UINT32 Capabilities)
162{
163 WINPR_ASSERT(callback);
164
165 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CLIENT_CHANNEL_NOTIFICATION);
166 wStream* out =
167 create_shared_message_header_with_functionid(InterfaceId, MessageId, CHANNEL_CREATED, 12);
168
169 if (!out)
170 return ERROR_OUTOFMEMORY;
171
172 Stream_Write_UINT32(out, MajorVersion);
173 Stream_Write_UINT32(out, MinorVersion);
174 Stream_Write_UINT32(out, Capabilities); /* capabilities version */
175 return stream_write_and_free(callback->plugin, callback->channel, out);
176}
177
183static UINT urbdrc_process_channel_create(GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
184 UINT32 MessageId)
185{
186 UINT32 MajorVersion = 0;
187 UINT32 MinorVersion = 0;
188 UINT32 Capabilities = 0;
189 URBDRC_PLUGIN* urbdrc = nullptr;
190
191 if (!callback || !s || !callback->plugin)
192 return ERROR_INVALID_PARAMETER;
193
194 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
195
196 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
197 return ERROR_INVALID_DATA;
198
199 Stream_Read_UINT32(s, MajorVersion);
200 Stream_Read_UINT32(s, MinorVersion);
201 Stream_Read_UINT32(s, Capabilities);
202
203 /* Version check, we only support version 1.0 */
204 if ((MajorVersion != 1) || (MinorVersion != 0))
205 {
206 WLog_Print(urbdrc->log, WLOG_WARN,
207 "server supports USB channel version %" PRIu32 ".%" PRIu32, MajorVersion,
208 MinorVersion);
209 WLog_Print(urbdrc->log, WLOG_WARN, "we only support channel version 1.0");
210 MajorVersion = 1;
211 MinorVersion = 0;
212 }
213 if (Capabilities != 0)
214 {
215 WLog_Print(urbdrc->log, WLOG_WARN,
216 "[MS-RDPEUSB] 2.2.5.1 Channel Created Message (CHANNEL_CREATED) states "
217 "Capabilities must be 0, got %" PRIu32,
218 Capabilities);
219 Capabilities = 0;
220 }
221
222 return urbdrc_send_channel_created(callback, MessageId, MajorVersion, MinorVersion,
223 Capabilities);
224}
225
226static UINT urdbrc_send_virtual_channel_add(IWTSPlugin* plugin, IWTSVirtualChannel* channel,
227 UINT32 MessageId)
228{
229 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CLIENT_DEVICE_SINK);
230 wStream* out = create_shared_message_header_with_functionid(InterfaceId, MessageId,
231 ADD_VIRTUAL_CHANNEL, 0);
232
233 if (!out)
234 return ERROR_OUTOFMEMORY;
235
236 return stream_write_and_free(plugin, channel, out);
237}
238
239static BOOL write_string_block(wStream* s, size_t count, const char** strings, const size_t* length,
240 BOOL isMultiSZ)
241{
242 size_t len = 0;
243 for (size_t x = 0; x < count; x++)
244 {
245 const SSIZE_T wlen = ConvertUtf8NToWChar(strings[x], length[x], nullptr, 0);
246 if (wlen < 0)
247 return FALSE;
248 len += WINPR_ASSERTING_INT_CAST(size_t, wlen) + 1ULL;
249 }
250
251 if (isMultiSZ)
252 len++;
253
254 if (!Stream_EnsureRemainingCapacity(s, len * sizeof(WCHAR) + sizeof(UINT32)))
255 return FALSE;
256
257 /* Write number of characters (including '\0') of all strings */
258 Stream_Write_UINT32(s, WINPR_ASSERTING_INT_CAST(UINT32, len)); /* cchHwIds */
259 /* HardwareIds 1 */
260
261 for (size_t x = 0; x < count; x++)
262 {
263 size_t clength = length[x];
264 const char* str = strings[x];
265 const SSIZE_T wlen = ConvertUtf8NToWChar(str, clength, nullptr, 0);
266 if (wlen < 0)
267 return FALSE;
268 const size_t wlength = WINPR_ASSERTING_INT_CAST(size_t, wlen);
269
270 const SSIZE_T w = Stream_Write_UTF16_String_From_UTF8(s, wlength, str, clength, TRUE);
271 if ((w < 0) || ((size_t)w != wlength))
272 return FALSE;
273 Stream_Write_UINT16(s, 0);
274 }
275
276 if (isMultiSZ)
277 Stream_Write_UINT16(s, 0);
278 return TRUE;
279}
280
281/* [MS-RDPEUSB] 2.2.4.2 Add Device Message (ADD_DEVICE) */
282static UINT urbdrc_send_add_device(GENERIC_CHANNEL_CALLBACK* callback, UINT32 UsbDevice,
283 UINT32 bcdUSB, const char* strInstanceId, size_t InstanceIdLen,
284 size_t nrHwIds, const char* HardwareIds[],
285 const size_t HardwareIdsLen[], size_t nrCompatIds,
286 const char* CompatibilityIds[],
287 const size_t CompatibilityIdsLen[], const char* strContainerId,
288 size_t ContainerIdLen)
289{
290 WINPR_ASSERT(callback);
291 WINPR_ASSERT(HardwareIds);
292 WINPR_ASSERT(HardwareIdsLen);
293 WINPR_ASSERT(CompatibilityIds);
294 WINPR_ASSERT(CompatibilityIdsLen);
295
296 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CLIENT_DEVICE_SINK);
297 wStream* out = create_shared_message_header_with_functionid(InterfaceId, 0, ADD_DEVICE, 8);
298 if (!out)
299 return ERROR_OUTOFMEMORY;
300
301 Stream_Write_UINT32(out, 0x00000001); /* NumUsbDevice */
302 Stream_Write_UINT32(out, UsbDevice); /* UsbDevice */
303
304 if (!write_string_block(out, 1, &strInstanceId, &InstanceIdLen, FALSE))
305 goto fail;
306
307 if (!write_string_block(out, nrHwIds, HardwareIds, HardwareIdsLen, TRUE))
308 goto fail;
309
310 if (!write_string_block(out, nrCompatIds, CompatibilityIds, CompatibilityIdsLen, TRUE))
311 goto fail;
312
313 if (!write_string_block(out, 1, &strContainerId, &ContainerIdLen, FALSE))
314 goto fail;
315
316 /* USB_DEVICE_CAPABILITIES 28 bytes */
317 if (!Stream_EnsureRemainingCapacity(out, 28))
318 goto fail;
319
320 Stream_Write_UINT32(out, 0x0000001c); /* CbSize */
321 Stream_Write_UINT32(out, 2); /* UsbBusInterfaceVersion, 0 ,1 or 2 */ // TODO: Get from libusb
322 Stream_Write_UINT32(out, 0x600); /* USBDI_Version, 0x500 or 0x600 */ // TODO: Get from libusb
323 /* Supported_USB_Version, 0x110,0x110 or 0x200(usb2.0) */
324 Stream_Write_UINT32(out, bcdUSB);
325 Stream_Write_UINT32(out, 0x00000000); /* HcdCapabilities, MUST always be zero */
326
327 if (bcdUSB < 0x200)
328 Stream_Write_UINT32(out, 0x00000000); /* DeviceIsHighSpeed */
329 else
330 Stream_Write_UINT32(out, 0x00000001); /* DeviceIsHighSpeed */
331
332 Stream_Write_UINT32(out, 0x50); /* NoAckIsochWriteJitterBufferSizeInMs, >=10 or <=512 */
333 return stream_write_and_free(callback->plugin, callback->channel, out);
334
335fail:
336 Stream_Free(out, TRUE);
337 return ERROR_INTERNAL_ERROR;
338}
339
345static UINT urdbrc_send_usb_device_add(GENERIC_CHANNEL_CALLBACK* callback, IUDEVICE* pdev)
346{
347 char HardwareIds[2][DEVICE_HARDWARE_ID_SIZE] = { WINPR_C_ARRAY_INIT };
348 const char* CHardwareIds[2] = { HardwareIds[0], HardwareIds[1] };
349 char CompatibilityIds[4][DEVICE_COMPATIBILITY_ID_SIZE] = { WINPR_C_ARRAY_INIT };
350 const char* CCompatibilityIds[4] = { CompatibilityIds[0], CompatibilityIds[1],
351 CompatibilityIds[2], CompatibilityIds[3] };
352 char strContainerId[DEVICE_CONTAINER_STR_SIZE] = WINPR_C_ARRAY_INIT;
353 char strInstanceId[DEVICE_INSTANCE_STR_SIZE] = WINPR_C_ARRAY_INIT;
354 size_t CompatibilityIdLen[4] = WINPR_C_ARRAY_INIT;
355 size_t HardwareIdsLen[2] = WINPR_C_ARRAY_INIT;
356 const size_t nrHwIds = ARRAYSIZE(HardwareIds);
357 size_t nrCompatIds = 3;
358
359 /* USB kernel driver detach!! */
360 if (!pdev->detach_kernel_driver(pdev))
361 return ERROR_INTERNAL_ERROR;
362
363 {
364 const UINT16 idVendor = (UINT16)pdev->query_device_descriptor(pdev, ID_VENDOR);
365 const UINT16 idProduct = (UINT16)pdev->query_device_descriptor(pdev, ID_PRODUCT);
366 const UINT16 bcdDevice = (UINT16)pdev->query_device_descriptor(pdev, BCD_DEVICE);
367 (void)sprintf_s(HardwareIds[1], DEVICE_HARDWARE_ID_SIZE,
368 "USB\\VID_%04" PRIX16 "&PID_%04" PRIX16 "", idVendor, idProduct);
369 (void)sprintf_s(HardwareIds[0], DEVICE_HARDWARE_ID_SIZE,
370 "USB\\VID_%04" PRIX16 "&PID_%04" PRIX16 "&REV_%04" PRIX16 "", idVendor,
371 idProduct, bcdDevice);
372 }
373 {
374 const UINT8 bDeviceClass = (UINT8)pdev->query_device_descriptor(pdev, B_DEVICE_CLASS);
375 const UINT8 bDeviceSubClass = (UINT8)pdev->query_device_descriptor(pdev, B_DEVICE_SUBCLASS);
376 const UINT8 bDeviceProtocol = (UINT8)pdev->query_device_descriptor(pdev, B_DEVICE_PROTOCOL);
377
378 if (!(pdev->isCompositeDevice(pdev)))
379 {
380 (void)sprintf_s(CompatibilityIds[2], DEVICE_COMPATIBILITY_ID_SIZE,
381 "USB\\Class_%02" PRIX8 "", bDeviceClass);
382 (void)sprintf_s(CompatibilityIds[1], DEVICE_COMPATIBILITY_ID_SIZE,
383 "USB\\Class_%02" PRIX8 "&SubClass_%02" PRIX8 "", bDeviceClass,
384 bDeviceSubClass);
385 (void)sprintf_s(CompatibilityIds[0], DEVICE_COMPATIBILITY_ID_SIZE,
386 "USB\\Class_%02" PRIX8 "&SubClass_%02" PRIX8 "&Prot_%02" PRIX8 "",
387 bDeviceClass, bDeviceSubClass, bDeviceProtocol);
388 }
389 else
390 {
391 (void)sprintf_s(CompatibilityIds[3], DEVICE_COMPATIBILITY_ID_SIZE, "USB\\COMPOSITE");
392 (void)sprintf_s(CompatibilityIds[2], DEVICE_COMPATIBILITY_ID_SIZE, "USB\\DevClass_00");
393 (void)sprintf_s(CompatibilityIds[1], DEVICE_COMPATIBILITY_ID_SIZE,
394 "USB\\DevClass_00&SubClass_00");
395 (void)sprintf_s(CompatibilityIds[0], DEVICE_COMPATIBILITY_ID_SIZE,
396 "USB\\DevClass_00&SubClass_00&Prot_00");
397 nrCompatIds = 4;
398 }
399 }
400 func_instance_id_generate(pdev, strInstanceId, DEVICE_INSTANCE_STR_SIZE);
401 func_container_id_generate(pdev, strContainerId);
402
403 for (size_t x = 0; x < nrHwIds; x++)
404 {
405 HardwareIdsLen[x] = strnlen(HardwareIds[x], DEVICE_HARDWARE_ID_SIZE);
406 }
407
408 for (size_t x = 0; x < nrCompatIds; x++)
409 {
410 CompatibilityIdLen[x] = strnlen(CompatibilityIds[x], DEVICE_COMPATIBILITY_ID_SIZE);
411 }
412
413 const size_t InstanceIdLen = strnlen(strInstanceId, sizeof(strInstanceId));
414 const size_t ContainerIdLen = strnlen(strContainerId, sizeof(strContainerId));
415
416 const UINT32 UsbDevice = pdev->get_UsbDevice(pdev);
417 const UINT32 bcdUSB =
418 WINPR_ASSERTING_INT_CAST(uint32_t, pdev->query_device_descriptor(pdev, BCD_USB));
419 return urbdrc_send_add_device(callback, UsbDevice, bcdUSB, strInstanceId, InstanceIdLen,
420 nrHwIds, CHardwareIds, HardwareIdsLen, nrCompatIds,
421 CCompatibilityIds, CompatibilityIdLen, strContainerId,
422 ContainerIdLen);
423}
424
430static UINT urbdrc_exchange_capabilities(GENERIC_CHANNEL_CALLBACK* callback, wStream* data)
431{
432 UINT32 MessageId = 0;
433 UINT32 FunctionId = 0;
434 UINT32 InterfaceId = 0;
435 UINT error = CHANNEL_RC_OK;
436
437 if (!data)
438 return ERROR_INVALID_PARAMETER;
439
440 if (!Stream_CheckAndLogRequiredLength(TAG, data, 8))
441 return ERROR_INVALID_DATA;
442
443 Stream_Rewind_UINT32(data);
444 Stream_Read_UINT32(data, InterfaceId);
445 Stream_Read_UINT32(data, MessageId);
446 Stream_Read_UINT32(data, FunctionId);
447
448 switch (FunctionId)
449 {
450 case RIM_EXCHANGE_CAPABILITY_REQUEST:
451 if (InterfaceId != 0)
452 {
453 WLog_ERR(
454 TAG,
455 "[MS-RDPEUSB] 2.2.3.1 Interface Manipulation Exchange Capabilities Request "
456 "(RIM_EXCHANGE_CAPABILITY_REQUEST))::InterfaceId expected 0, got %" PRIu32,
457 InterfaceId);
458 return ERROR_INVALID_DATA;
459 }
460 error = urbdrc_process_capability_request(callback, data, MessageId);
461 break;
462
463 case RIMCALL_RELEASE:
464 break;
465
466 default:
467 error = ERROR_NOT_FOUND;
468 break;
469 }
470
471 return error;
472}
473
474static BOOL urbdrc_announce_devices(IUDEVMAN* udevman)
475{
476 UINT error = ERROR_SUCCESS;
477
478 udevman->loading_lock(udevman);
479 udevman->rewind(udevman);
480
481 while (udevman->has_next(udevman))
482 {
483 IUDEVICE* pdev = udevman->get_next(udevman);
484
485 if (!pdev->isAlreadySend(pdev))
486 {
487 const UINT32 deviceId = pdev->get_UsbDevice(pdev);
488 UINT cerror =
489 urdbrc_send_virtual_channel_add(udevman->plugin, get_channel(udevman), deviceId);
490
491 if (cerror != ERROR_SUCCESS)
492 break;
493 }
494 }
495
496 udevman->loading_unlock(udevman);
497
498 return error == ERROR_SUCCESS;
499}
500
501static UINT urbdrc_device_control_channel(GENERIC_CHANNEL_CALLBACK* callback,
502 WINPR_ATTR_UNUSED wStream* s)
503{
504 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
505 IUDEVMAN* udevman = urbdrc->udevman;
506 IWTSVirtualChannel* channel = callback->channel;
507 IUDEVICE* pdev = nullptr;
508 BOOL found = FALSE;
509 UINT error = ERROR_INTERNAL_ERROR;
510 UINT32 channelId = callback->channel_mgr->GetChannelId(channel);
511
512 switch (urbdrc->vchannel_status)
513 {
514 case INIT_CHANNEL_IN:
515 /* Control channel was established */
516 error = ERROR_SUCCESS;
517 if (!udevman->initialize(udevman, channelId))
518 goto fail;
519
520 if (!urbdrc_announce_devices(udevman))
521 goto fail;
522
523 urbdrc->vchannel_status = INIT_CHANNEL_OUT;
524 break;
525
526 case INIT_CHANNEL_OUT:
527 /* A new device channel was created, add the channel
528 * to the device */
529 udevman->loading_lock(udevman);
530 udevman->rewind(udevman);
531
532 while (udevman->has_next(udevman))
533 {
534 pdev = udevman->get_next(udevman);
535
536 if (!pdev->isAlreadySend(pdev))
537 {
538 const UINT32 channelID = callback->channel_mgr->GetChannelId(channel);
539 found = TRUE;
540 pdev->setAlreadySend(pdev);
541 pdev->set_channelManager(pdev, callback->channel_mgr);
542 pdev->set_channelID(pdev, channelID);
543 break;
544 }
545 }
546
547 udevman->loading_unlock(udevman);
548 error = ERROR_SUCCESS;
549
550 if (found && pdev->isAlreadySend(pdev))
551 error = urdbrc_send_usb_device_add(callback, pdev);
552
553 break;
554
555 default:
556 WLog_Print(urbdrc->log, WLOG_ERROR, "vchannel_status unknown value %" PRIu32 "",
557 urbdrc->vchannel_status);
558 break;
559 }
560
561fail:
562 return error;
563}
564
570static UINT urbdrc_process_channel_notification(GENERIC_CHANNEL_CALLBACK* callback, wStream* data)
571{
572 UINT32 MessageId = 0;
573 UINT32 FunctionId = 0;
574 UINT32 InterfaceId = 0;
575 UINT error = CHANNEL_RC_OK;
576 URBDRC_PLUGIN* urbdrc = nullptr;
577
578 if (!callback || !data)
579 return ERROR_INVALID_PARAMETER;
580
581 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
582
583 if (!urbdrc)
584 return ERROR_INVALID_PARAMETER;
585
586 if (!Stream_CheckAndLogRequiredLength(TAG, data, 8))
587 return ERROR_INVALID_DATA;
588
589 Stream_Rewind(data, 4);
590 Stream_Read_UINT32(data, InterfaceId);
591 Stream_Read_UINT32(data, MessageId);
592 Stream_Read_UINT32(data, FunctionId);
593 WLog_Print(urbdrc->log, WLOG_TRACE, "%s [%" PRIu32 "]",
594 call_to_string(FALSE, InterfaceId, FunctionId), FunctionId);
595
596 switch (FunctionId)
597 {
598 case CHANNEL_CREATED:
599 error = urbdrc_process_channel_create(callback, data, MessageId);
600 break;
601
602 case RIMCALL_RELEASE:
603 error = urbdrc_device_control_channel(callback, data);
604 break;
605
606 default:
607 WLog_Print(urbdrc->log, WLOG_TRACE, "unknown FunctionId 0x%" PRIX32 "", FunctionId);
608 error = 1;
609 break;
610 }
611
612 return error;
613}
614
620static UINT urbdrc_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
621{
622 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
623 URBDRC_PLUGIN* urbdrc = nullptr;
624 IUDEVMAN* udevman = nullptr;
625 UINT32 InterfaceId = 0;
626 UINT error = ERROR_INTERNAL_ERROR;
627
628 if (callback == nullptr)
629 return ERROR_INVALID_PARAMETER;
630
631 if (callback->plugin == nullptr)
632 return error;
633
634 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
635
636 if (urbdrc->udevman == nullptr)
637 return error;
638
639 udevman = urbdrc->udevman;
640
641 if (!Stream_CheckAndLogRequiredLength(TAG, data, 12))
642 return ERROR_INVALID_DATA;
643
644 urbdrc_dump_message(urbdrc->log, FALSE, FALSE, data);
645 Stream_Read_UINT32(data, InterfaceId);
646
647 /* Need to check InterfaceId and mask values */
648 switch (InterfaceId)
649 {
650 case CAPABILITIES_NEGOTIATOR | (STREAM_ID_NONE << 30):
651 error = urbdrc_exchange_capabilities(callback, data);
652 break;
653
654 case SERVER_CHANNEL_NOTIFICATION | (STREAM_ID_PROXY << 30):
655 error = urbdrc_process_channel_notification(callback, data);
656 break;
657
658 default:
659 error = urbdrc_process_udev_data_transfer(callback, urbdrc, udevman, data);
660 WLog_DBG(TAG, "urbdrc_process_udev_data_transfer returned 0x%08" PRIx32, error);
661 error = ERROR_SUCCESS; /* Ignore errors, the device may have been unplugged. */
662 break;
663 }
664
665 return error;
666}
667
673static UINT urbdrc_on_close(IWTSVirtualChannelCallback* pChannelCallback)
674{
675 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
676 if (callback)
677 {
678 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
679 if (urbdrc)
680 {
681 IUDEVMAN* udevman = urbdrc->udevman;
682 if (udevman && callback->channel_mgr)
683 {
684 UINT32 control = callback->channel_mgr->GetChannelId(callback->channel);
685 if (udevman->controlChannelId == control)
686 udevman->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
687 else
688 { /* Need to notify the local backend the device is gone */
689 IUDEVICE* pdev = udevman->get_udevice_by_ChannelID(udevman, control);
690 if (pdev)
691 pdev->markChannelClosed(pdev);
692 }
693 }
694 }
695 }
696 free(callback);
697 return CHANNEL_RC_OK;
698}
699
705static UINT urbdrc_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
706 IWTSVirtualChannel* pChannel,
707 WINPR_ATTR_UNUSED BYTE* pData,
708 WINPR_ATTR_UNUSED BOOL* pbAccept,
709 IWTSVirtualChannelCallback** ppCallback)
710{
711 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
712 GENERIC_CHANNEL_CALLBACK* callback = nullptr;
713
714 if (!ppCallback)
715 return ERROR_INVALID_PARAMETER;
716
717 callback = (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK));
718
719 if (!callback)
720 return ERROR_OUTOFMEMORY;
721
722 callback->iface.OnDataReceived = urbdrc_on_data_received;
723 callback->iface.OnClose = urbdrc_on_close;
724 callback->plugin = listener_callback->plugin;
725 callback->channel_mgr = listener_callback->channel_mgr;
726 callback->channel = pChannel;
727 *ppCallback = (IWTSVirtualChannelCallback*)callback;
728 return CHANNEL_RC_OK;
729}
730
736static UINT urbdrc_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
737{
738 UINT status = 0;
739 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
740 IUDEVMAN* udevman = nullptr;
741 char channelName[sizeof(URBDRC_CHANNEL_NAME)] = { URBDRC_CHANNEL_NAME };
742
743 if (!urbdrc || !urbdrc->udevman)
744 return ERROR_INVALID_PARAMETER;
745
746 if (urbdrc->initialized)
747 {
748 WLog_ERR(TAG, "[%s] channel initialized twice, aborting", URBDRC_CHANNEL_NAME);
749 return ERROR_INVALID_DATA;
750 }
751 udevman = urbdrc->udevman;
752 urbdrc->listener_callback =
754
755 if (!urbdrc->listener_callback)
756 return CHANNEL_RC_NO_MEMORY;
757
758 urbdrc->listener_callback->iface.OnNewChannelConnection = urbdrc_on_new_channel_connection;
759 urbdrc->listener_callback->plugin = pPlugin;
760 urbdrc->listener_callback->channel_mgr = pChannelMgr;
761
762 /* [MS-RDPEUSB] 2.1 Transport defines the channel name in uppercase letters */
763 CharUpperA(channelName);
764 status = pChannelMgr->CreateListener(pChannelMgr, channelName, 0,
765 &urbdrc->listener_callback->iface, &urbdrc->listener);
766 if (status != CHANNEL_RC_OK)
767 return status;
768
769 status = CHANNEL_RC_OK;
770 if (udevman->listener_created_callback)
771 status = udevman->listener_created_callback(udevman);
772
773 urbdrc->initialized = status == CHANNEL_RC_OK;
774 return status;
775}
776
782static UINT urbdrc_plugin_terminated(IWTSPlugin* pPlugin)
783{
784 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
785 IUDEVMAN* udevman = nullptr;
786
787 if (!urbdrc)
788 return ERROR_INVALID_DATA;
789 if (urbdrc->listener_callback)
790 {
791 IWTSVirtualChannelManager* mgr = urbdrc->listener_callback->channel_mgr;
792 if (mgr)
793 IFCALL(mgr->DestroyListener, mgr, urbdrc->listener);
794 }
795 udevman = urbdrc->udevman;
796
797 if (udevman)
798 {
799 udevman->free(udevman);
800 udevman = nullptr;
801 }
802
803 free(urbdrc->subsystem);
804 free(urbdrc->listener_callback);
805 free(urbdrc);
806 return CHANNEL_RC_OK;
807}
808
809static BOOL urbdrc_register_udevman_addin(IWTSPlugin* pPlugin, IUDEVMAN* udevman)
810{
811 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
812
813 if (urbdrc->udevman)
814 {
815 WLog_Print(urbdrc->log, WLOG_ERROR, "existing device, abort.");
816 return FALSE;
817 }
818
819 DEBUG_DVC("device registered.");
820 urbdrc->udevman = udevman;
821 return TRUE;
822}
823
829static UINT urbdrc_load_udevman_addin(IWTSPlugin* pPlugin, LPCSTR name, const ADDIN_ARGV* args)
830{
831 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
832 FREERDP_URBDRC_SERVICE_ENTRY_POINTS entryPoints = WINPR_C_ARRAY_INIT;
833
834 PVIRTUALCHANNELENTRY pvce =
835 freerdp_load_channel_addin_entry(URBDRC_CHANNEL_NAME, name, nullptr, 0);
836 PFREERDP_URBDRC_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_URBDRC_DEVICE_ENTRY);
837
838 if (!entry)
839 return ERROR_INVALID_OPERATION;
840
841 entryPoints.plugin = pPlugin;
842 entryPoints.pRegisterUDEVMAN = urbdrc_register_udevman_addin;
843 entryPoints.args = args;
844
845 const UINT error = entry(&entryPoints);
846 if (error)
847 {
848 WLog_Print(urbdrc->log, WLOG_ERROR, "%s entry returns error.", name);
849 return error;
850 }
851
852 return CHANNEL_RC_OK;
853}
854
855static BOOL urbdrc_set_subsystem(URBDRC_PLUGIN* urbdrc, const char* subsystem)
856{
857 free(urbdrc->subsystem);
858 urbdrc->subsystem = _strdup(subsystem);
859 return (urbdrc->subsystem != nullptr);
860}
861
867static UINT urbdrc_process_addin_args(URBDRC_PLUGIN* urbdrc, const ADDIN_ARGV* args)
868{
869 int status = 0;
870 COMMAND_LINE_ARGUMENT_A urbdrc_args[] = {
871 { "dbg", COMMAND_LINE_VALUE_FLAG, "", nullptr, BoolValueFalse, -1, nullptr, "debug" },
872 { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", nullptr, nullptr, -1, nullptr,
873 "subsystem" },
874 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device list>", nullptr, nullptr, -1, nullptr,
875 "devices" },
876 { "encode", COMMAND_LINE_VALUE_FLAG, "", nullptr, nullptr, -1, nullptr, "encode" },
877 { "quality", COMMAND_LINE_VALUE_REQUIRED, "<[0-2] -> [high-medium-low]>", nullptr, nullptr,
878 -1, nullptr, "quality" },
879 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
880 };
881
882 const DWORD flags =
883 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
884 const COMMAND_LINE_ARGUMENT_A* arg = nullptr;
885 status = CommandLineParseArgumentsA(args->argc, args->argv, urbdrc_args, flags, urbdrc, nullptr,
886 nullptr);
887
888 if (status < 0)
889 return ERROR_INVALID_DATA;
890
891 arg = urbdrc_args;
892
893 do
894 {
895 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
896 continue;
897
898 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dbg")
899 {
900 if (!WLog_SetLogLevel(urbdrc->log, WLOG_TRACE))
901 return ERROR_INTERNAL_ERROR;
902 }
903 CommandLineSwitchCase(arg, "sys")
904 {
905 if (!urbdrc_set_subsystem(urbdrc, arg->Value))
906 return ERROR_OUTOFMEMORY;
907 }
908 CommandLineSwitchDefault(arg)
909 {
910 }
911 CommandLineSwitchEnd(arg)
912 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
913
914 return CHANNEL_RC_OK;
915}
916
917BOOL add_device(IUDEVMAN* idevman, UINT32 flags, BYTE busnum, BYTE devnum, UINT16 idVendor,
918 UINT16 idProduct)
919{
920 UINT32 regflags = 0;
921
922 if (!idevman)
923 return FALSE;
924
925 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)idevman->plugin;
926
927 if (!urbdrc || !urbdrc->listener_callback)
928 return FALSE;
929
930 UINT32 mask = (DEVICE_ADD_FLAG_VENDOR | DEVICE_ADD_FLAG_PRODUCT);
931 if ((flags & mask) == mask)
932 regflags |= UDEVMAN_FLAG_ADD_BY_VID_PID;
933 mask = (DEVICE_ADD_FLAG_BUS | DEVICE_ADD_FLAG_DEV);
934 if ((flags & mask) == mask)
935 regflags |= UDEVMAN_FLAG_ADD_BY_ADDR;
936
937 const size_t success =
938 idevman->register_udevice(idevman, busnum, devnum, idVendor, idProduct, regflags);
939
940 if ((success > 0) && (flags & DEVICE_ADD_FLAG_REGISTER))
941 {
942 if (!urbdrc_announce_devices(idevman))
943 return FALSE;
944 }
945
946 return TRUE;
947}
948
949BOOL del_device(IUDEVMAN* idevman, UINT32 flags, BYTE busnum, BYTE devnum, UINT16 idVendor,
950 UINT16 idProduct)
951{
952 IUDEVICE* pdev = nullptr;
953 URBDRC_PLUGIN* urbdrc = nullptr;
954
955 if (!idevman)
956 return FALSE;
957
958 urbdrc = (URBDRC_PLUGIN*)idevman->plugin;
959
960 if (!urbdrc || !urbdrc->listener_callback)
961 return FALSE;
962
963 idevman->loading_lock(idevman);
964 idevman->rewind(idevman);
965
966 while (idevman->has_next(idevman))
967 {
968 BOOL match = TRUE;
969 IUDEVICE* dev = idevman->get_next(idevman);
970
971 if ((flags & (DEVICE_ADD_FLAG_BUS | DEVICE_ADD_FLAG_DEV | DEVICE_ADD_FLAG_VENDOR |
972 DEVICE_ADD_FLAG_PRODUCT)) == 0)
973 match = FALSE;
974 if (flags & DEVICE_ADD_FLAG_BUS)
975 {
976 if (dev->get_bus_number(dev) != busnum)
977 match = FALSE;
978 }
979 if (flags & DEVICE_ADD_FLAG_DEV)
980 {
981 if (dev->get_dev_number(dev) != devnum)
982 match = FALSE;
983 }
984 if (flags & DEVICE_ADD_FLAG_VENDOR)
985 {
986 int vid = dev->query_device_descriptor(dev, ID_VENDOR);
987 if (vid != idVendor)
988 match = FALSE;
989 }
990 if (flags & DEVICE_ADD_FLAG_PRODUCT)
991 {
992 int pid = dev->query_device_descriptor(dev, ID_PRODUCT);
993 if (pid != idProduct)
994 match = FALSE;
995 }
996
997 if (match)
998 {
999 pdev = dev;
1000 break;
1001 }
1002 }
1003
1004 if (pdev)
1005 pdev->setChannelClosed(pdev);
1006
1007 idevman->loading_unlock(idevman);
1008 return TRUE;
1009}
1010
1016FREERDP_ENTRY_POINT(UINT VCAPITYPE urbdrc_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1017{
1018 UINT status = 0;
1019 const ADDIN_ARGV* args = nullptr;
1020 URBDRC_PLUGIN* urbdrc = nullptr;
1021 urbdrc = (URBDRC_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, URBDRC_CHANNEL_NAME);
1022 args = pEntryPoints->GetPluginData(pEntryPoints);
1023
1024 if (urbdrc == nullptr)
1025 {
1026 urbdrc = (URBDRC_PLUGIN*)calloc(1, sizeof(URBDRC_PLUGIN));
1027
1028 if (!urbdrc)
1029 return CHANNEL_RC_NO_MEMORY;
1030
1031 urbdrc->iface.Initialize = urbdrc_plugin_initialize;
1032 urbdrc->iface.Terminated = urbdrc_plugin_terminated;
1033 urbdrc->vchannel_status = INIT_CHANNEL_IN;
1034 status = pEntryPoints->RegisterPlugin(pEntryPoints, URBDRC_CHANNEL_NAME, &urbdrc->iface);
1035
1036 /* After we register the plugin free will be taken care of by dynamic channel */
1037 if (status != CHANNEL_RC_OK)
1038 {
1039 free(urbdrc);
1040 goto fail;
1041 }
1042
1043 urbdrc->log = WLog_Get(TAG);
1044
1045 if (!urbdrc->log)
1046 goto fail;
1047 }
1048
1049 status = urbdrc_process_addin_args(urbdrc, args);
1050
1051 if (status != CHANNEL_RC_OK)
1052 goto fail;
1053
1054 if (!urbdrc->subsystem && !urbdrc_set_subsystem(urbdrc, "libusb"))
1055 goto fail;
1056
1057 return urbdrc_load_udevman_addin(&urbdrc->iface, urbdrc->subsystem, args);
1058fail:
1059 return status;
1060}
1061
1062UINT stream_write_and_free(IWTSPlugin* plugin, IWTSVirtualChannel* channel, wStream* out)
1063{
1064 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)plugin;
1065
1066 if (!out)
1067 return ERROR_INVALID_PARAMETER;
1068
1069 if (!channel || !out || !urbdrc)
1070 {
1071 Stream_Free(out, TRUE);
1072 return ERROR_INVALID_PARAMETER;
1073 }
1074
1075 if (!channel->Write)
1076 {
1077 Stream_Free(out, TRUE);
1078 return ERROR_INTERNAL_ERROR;
1079 }
1080
1081 urbdrc_dump_message(urbdrc->log, TRUE, TRUE, out);
1082 const size_t len = Stream_GetPosition(out);
1083 UINT rc = ERROR_INTERNAL_ERROR;
1084 if (len <= UINT32_MAX)
1085 rc = channel->Write(channel, (UINT32)len, Stream_Buffer(out), nullptr);
1086 Stream_Free(out, TRUE);
1087 return rc;
1088}
Definition urbdrc_main.h:73