VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ConsoleVRDPServer.cpp@ 48654

Last change on this file since 48654 was 48406, checked in by vboxsync, 11 years ago

Main,VBoxManage: Implemented IConsole::EmulatedUSB. Removed IMachine::emulatedUSBWebcameraEnabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 136.8 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 48406 2013-09-10 12:53:50Z vboxsync $ */
2/** @file
3 * VBox Console VRDP Helper class
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "ConsoleVRDPServer.h"
19#include "ConsoleImpl.h"
20#include "DisplayImpl.h"
21#include "KeyboardImpl.h"
22#include "MouseImpl.h"
23#include "AudioSnifferInterface.h"
24#ifdef VBOX_WITH_EXTPACK
25# include "ExtPackManagerImpl.h"
26#endif
27#include "VMMDev.h"
28#ifdef VBOX_WITH_USB_CARDREADER
29# include "UsbCardReader.h"
30#endif
31#include "UsbWebcamInterface.h"
32
33#include "Global.h"
34#include "AutoCaller.h"
35#include "Logging.h"
36
37#include <iprt/asm.h>
38#include <iprt/alloca.h>
39#include <iprt/ldr.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/cpp/utils.h>
43
44#include <VBox/err.h>
45#include <VBox/RemoteDesktop/VRDEOrders.h>
46#include <VBox/com/listeners.h>
47#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
48
49class VRDPConsoleListener
50{
51public:
52 VRDPConsoleListener()
53 {
54 }
55
56 HRESULT init(ConsoleVRDPServer *server)
57 {
58 m_server = server;
59 return S_OK;
60 }
61
62 void uninit()
63 {
64 }
65
66 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
67 {
68 switch (aType)
69 {
70 case VBoxEventType_OnMousePointerShapeChanged:
71 {
72 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
73 Assert(mpscev);
74 BOOL visible, alpha;
75 ULONG xHot, yHot, width, height;
76 com::SafeArray <BYTE> shape;
77
78 mpscev->COMGETTER(Visible)(&visible);
79 mpscev->COMGETTER(Alpha)(&alpha);
80 mpscev->COMGETTER(Xhot)(&xHot);
81 mpscev->COMGETTER(Yhot)(&yHot);
82 mpscev->COMGETTER(Width)(&width);
83 mpscev->COMGETTER(Height)(&height);
84 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
85
86 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
87 break;
88 }
89 case VBoxEventType_OnMouseCapabilityChanged:
90 {
91 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
92 Assert(mccev);
93 if (m_server)
94 {
95 BOOL fAbsoluteMouse;
96 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
97 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
98 }
99 break;
100 }
101 case VBoxEventType_OnKeyboardLedsChanged:
102 {
103 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
104 Assert(klcev);
105
106 if (m_server)
107 {
108 BOOL fNumLock, fCapsLock, fScrollLock;
109 klcev->COMGETTER(NumLock)(&fNumLock);
110 klcev->COMGETTER(CapsLock)(&fCapsLock);
111 klcev->COMGETTER(ScrollLock)(&fScrollLock);
112 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
113 }
114 break;
115 }
116
117 default:
118 AssertFailed();
119 }
120
121 return S_OK;
122 }
123
124private:
125 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
126 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
127 ConsoleVRDPServer *m_server;
128};
129
130typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
131
132VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
133
134#ifdef DEBUG_sunlover
135#define LOGDUMPPTR Log
136void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
137{
138 unsigned i;
139
140 const uint8_t *pu8And = pu8Shape;
141
142 for (i = 0; i < height; i++)
143 {
144 unsigned j;
145 LOGDUMPPTR(("%p: ", pu8And));
146 for (j = 0; j < (width + 7) / 8; j++)
147 {
148 unsigned k;
149 for (k = 0; k < 8; k++)
150 {
151 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
152 }
153
154 pu8And++;
155 }
156 LOGDUMPPTR(("\n"));
157 }
158
159 if (fXorMaskRGB32)
160 {
161 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
162
163 for (i = 0; i < height; i++)
164 {
165 unsigned j;
166 LOGDUMPPTR(("%p: ", pu32Xor));
167 for (j = 0; j < width; j++)
168 {
169 LOGDUMPPTR(("%08X", *pu32Xor++));
170 }
171 LOGDUMPPTR(("\n"));
172 }
173 }
174 else
175 {
176 /* RDP 24 bit RGB mask. */
177 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
178 for (i = 0; i < height; i++)
179 {
180 unsigned j;
181 LOGDUMPPTR(("%p: ", pu8Xor));
182 for (j = 0; j < width; j++)
183 {
184 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
185 pu8Xor += 3;
186 }
187 LOGDUMPPTR(("\n"));
188 }
189 }
190}
191#else
192#define dumpPointer(a, b, c, d) do {} while (0)
193#endif /* DEBUG_sunlover */
194
195static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
196{
197 /*
198 * Find the top border of the AND mask. First assign to special value.
199 */
200 uint32_t ySkipAnd = ~0;
201
202 const uint8_t *pu8And = pu8AndMask;
203 const uint32_t cbAndRow = (width + 7) / 8;
204 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
205
206 Assert(cbAndRow > 0);
207
208 unsigned y;
209 unsigned x;
210
211 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
212 {
213 /* For each complete byte in the row. */
214 for (x = 0; x < cbAndRow - 1; x++)
215 {
216 if (pu8And[x] != 0xFF)
217 {
218 ySkipAnd = y;
219 break;
220 }
221 }
222
223 if (ySkipAnd == ~(uint32_t)0)
224 {
225 /* Last byte. */
226 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
227 {
228 ySkipAnd = y;
229 }
230 }
231 }
232
233 if (ySkipAnd == ~(uint32_t)0)
234 {
235 ySkipAnd = 0;
236 }
237
238 /*
239 * Find the left border of the AND mask.
240 */
241 uint32_t xSkipAnd = ~0;
242
243 /* For all bit columns. */
244 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
245 {
246 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
247 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
248
249 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
250 {
251 if ((*pu8And & mask) == 0)
252 {
253 xSkipAnd = x;
254 break;
255 }
256 }
257 }
258
259 if (xSkipAnd == ~(uint32_t)0)
260 {
261 xSkipAnd = 0;
262 }
263
264 /*
265 * Find the XOR mask top border.
266 */
267 uint32_t ySkipXor = ~0;
268
269 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
270
271 uint32_t *pu32Xor = pu32XorStart;
272
273 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
274 {
275 for (x = 0; x < width; x++)
276 {
277 if (pu32Xor[x] != 0)
278 {
279 ySkipXor = y;
280 break;
281 }
282 }
283 }
284
285 if (ySkipXor == ~(uint32_t)0)
286 {
287 ySkipXor = 0;
288 }
289
290 /*
291 * Find the left border of the XOR mask.
292 */
293 uint32_t xSkipXor = ~(uint32_t)0;
294
295 /* For all columns. */
296 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
297 {
298 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
299
300 for (y = ySkipXor; y < height; y++, pu32Xor += width)
301 {
302 if (*pu32Xor != 0)
303 {
304 xSkipXor = x;
305 break;
306 }
307 }
308 }
309
310 if (xSkipXor == ~(uint32_t)0)
311 {
312 xSkipXor = 0;
313 }
314
315 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
316 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
317}
318
319/* Generate an AND mask for alpha pointers here, because
320 * guest driver does not do that correctly for Vista pointers.
321 * Similar fix, changing the alpha threshold, could be applied
322 * for the guest driver, but then additions reinstall would be
323 * necessary, which we try to avoid.
324 */
325static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
326{
327 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
328
329 int y;
330 for (y = 0; y < h; y++)
331 {
332 uint8_t bitmask = 0x80;
333
334 int x;
335 for (x = 0; x < w; x++, bitmask >>= 1)
336 {
337 if (bitmask == 0)
338 {
339 bitmask = 0x80;
340 }
341
342 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
343 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
344 {
345 pu8DstAndMask[x / 8] &= ~bitmask;
346 }
347 }
348
349 /* Point to next source and dest scans. */
350 pu8SrcAlpha += w * 4;
351 pu8DstAndMask += (w + 7) / 8;
352 }
353}
354
355STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
356 BOOL alpha,
357 ULONG xHot,
358 ULONG yHot,
359 ULONG width,
360 ULONG height,
361 ComSafeArrayIn(BYTE,inShape))
362{
363 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
364
365 if (m_server)
366 {
367 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
368 if (aShape.size() == 0)
369 {
370 if (!visible)
371 {
372 m_server->MousePointerHide();
373 }
374 }
375 else if (width != 0 && height != 0)
376 {
377 uint8_t* shape = aShape.raw();
378
379 dumpPointer(shape, width, height, true);
380
381 if (m_server->MousePointer(alpha, xHot, yHot, width, height, shape) == VINF_SUCCESS)
382 {
383 return S_OK;
384 }
385
386 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
387 * 'shape' AND mask followed by XOR mask.
388 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
389 *
390 * We convert this to RDP color format which consist of
391 * one bpp AND mask and 24 BPP (BGR) color XOR image.
392 *
393 * RDP clients expect 8 aligned width and height of
394 * pointer (preferably 32x32).
395 *
396 * They even contain bugs which do not appear for
397 * 32x32 pointers but would appear for a 41x32 one.
398 *
399 * So set pointer size to 32x32. This can be done safely
400 * because most pointers are 32x32.
401 */
402
403 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
404
405 uint8_t *pu8AndMask = shape;
406 uint8_t *pu8XorMask = shape + cbDstAndMask;
407
408 if (alpha)
409 {
410 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
411
412 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
413 }
414
415 /* Windows guest alpha pointers are wider than 32 pixels.
416 * Try to find out the top-left border of the pointer and
417 * then copy only meaningful bits. All complete top rows
418 * and all complete left columns where (AND == 1 && XOR == 0)
419 * are skipped. Hot spot is adjusted.
420 */
421 uint32_t ySkip = 0; /* How many rows to skip at the top. */
422 uint32_t xSkip = 0; /* How many columns to skip at the left. */
423
424 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
425
426 /* Must not skip the hot spot. */
427 xSkip = RT_MIN(xSkip, xHot);
428 ySkip = RT_MIN(ySkip, yHot);
429
430 /*
431 * Compute size and allocate memory for the pointer.
432 */
433 const uint32_t dstwidth = 32;
434 const uint32_t dstheight = 32;
435
436 VRDECOLORPOINTER *pointer = NULL;
437
438 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
439
440 uint32_t rdpmaskwidth = dstmaskwidth;
441 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
442
443 uint32_t rdpdatawidth = dstwidth * 3;
444 uint32_t rdpdatalen = dstheight * rdpdatawidth;
445
446 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
447
448 if (pointer)
449 {
450 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
451 uint8_t *dataarray = maskarray + rdpmasklen;
452
453 memset(maskarray, 0xFF, rdpmasklen);
454 memset(dataarray, 0x00, rdpdatalen);
455
456 uint32_t srcmaskwidth = (width + 7) / 8;
457 uint32_t srcdatawidth = width * 4;
458
459 /* Copy AND mask. */
460 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
461 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
462
463 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
464 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
465
466 unsigned x, y;
467
468 for (y = 0; y < minheight; y++)
469 {
470 for (x = 0; x < minwidth; x++)
471 {
472 uint32_t byteIndex = (x + xSkip) / 8;
473 uint32_t bitIndex = (x + xSkip) % 8;
474
475 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
476
477 if (!bit)
478 {
479 byteIndex = x / 8;
480 bitIndex = x % 8;
481
482 dst[byteIndex] &= ~(1 << (7 - bitIndex));
483 }
484 }
485
486 src += srcmaskwidth;
487 dst -= rdpmaskwidth;
488 }
489
490 /* Point src to XOR mask */
491 src = pu8XorMask + ySkip * srcdatawidth;
492 dst = dataarray + (dstheight - 1) * rdpdatawidth;
493
494 for (y = 0; y < minheight ; y++)
495 {
496 for (x = 0; x < minwidth; x++)
497 {
498 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
499 }
500
501 src += srcdatawidth;
502 dst -= rdpdatawidth;
503 }
504
505 pointer->u16HotX = (uint16_t)(xHot - xSkip);
506 pointer->u16HotY = (uint16_t)(yHot - ySkip);
507
508 pointer->u16Width = (uint16_t)dstwidth;
509 pointer->u16Height = (uint16_t)dstheight;
510
511 pointer->u16MaskLen = (uint16_t)rdpmasklen;
512 pointer->u16DataLen = (uint16_t)rdpdatalen;
513
514 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
515
516 m_server->MousePointerUpdate(pointer);
517
518 RTMemTmpFree(pointer);
519 }
520 }
521 }
522
523 return S_OK;
524}
525
526
527// ConsoleVRDPServer
528////////////////////////////////////////////////////////////////////////////////
529
530RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
531
532PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
533
534VRDEENTRYPOINTS_4 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
535VRDEENTRYPOINTS_4 *ConsoleVRDPServer::mpEntryPoints = NULL;
536
537VRDECALLBACKS_4 ConsoleVRDPServer::mCallbacks =
538{
539 { VRDE_INTERFACE_VERSION_4, sizeof(VRDECALLBACKS_4) },
540 ConsoleVRDPServer::VRDPCallbackQueryProperty,
541 ConsoleVRDPServer::VRDPCallbackClientLogon,
542 ConsoleVRDPServer::VRDPCallbackClientConnect,
543 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
544 ConsoleVRDPServer::VRDPCallbackIntercept,
545 ConsoleVRDPServer::VRDPCallbackUSB,
546 ConsoleVRDPServer::VRDPCallbackClipboard,
547 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
548 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
549 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
550 ConsoleVRDPServer::VRDPCallbackInput,
551 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
552 ConsoleVRDPServer::VRDECallbackAudioIn
553};
554
555DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
556{
557 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
558
559 int rc = VERR_NOT_SUPPORTED;
560
561 switch (index)
562 {
563 case VRDE_QP_NETWORK_PORT:
564 {
565 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
566 ULONG port = 0;
567
568 if (cbBuffer >= sizeof(uint32_t))
569 {
570 *(uint32_t *)pvBuffer = (uint32_t)port;
571 rc = VINF_SUCCESS;
572 }
573 else
574 {
575 rc = VINF_BUFFER_OVERFLOW;
576 }
577
578 *pcbOut = sizeof(uint32_t);
579 } break;
580
581 case VRDE_QP_NETWORK_ADDRESS:
582 {
583 com::Bstr bstr;
584 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
585
586 /* The server expects UTF8. */
587 com::Utf8Str address = bstr;
588
589 size_t cbAddress = address.length() + 1;
590
591 if (cbAddress >= 0x10000)
592 {
593 /* More than 64K seems to be an invalid address. */
594 rc = VERR_TOO_MUCH_DATA;
595 break;
596 }
597
598 if ((size_t)cbBuffer >= cbAddress)
599 {
600 memcpy(pvBuffer, address.c_str(), cbAddress);
601 rc = VINF_SUCCESS;
602 }
603 else
604 {
605 rc = VINF_BUFFER_OVERFLOW;
606 }
607
608 *pcbOut = (uint32_t)cbAddress;
609 } break;
610
611 case VRDE_QP_NUMBER_MONITORS:
612 {
613 ULONG cMonitors = 1;
614
615 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
616
617 if (cbBuffer >= sizeof(uint32_t))
618 {
619 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
620 rc = VINF_SUCCESS;
621 }
622 else
623 {
624 rc = VINF_BUFFER_OVERFLOW;
625 }
626
627 *pcbOut = sizeof(uint32_t);
628 } break;
629
630 case VRDE_QP_NETWORK_PORT_RANGE:
631 {
632 com::Bstr bstr;
633 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
634
635 if (hrc != S_OK)
636 {
637 bstr = "";
638 }
639
640 if (bstr == "0")
641 {
642 bstr = "3389";
643 }
644
645 /* The server expects UTF8. */
646 com::Utf8Str portRange = bstr;
647
648 size_t cbPortRange = portRange.length() + 1;
649
650 if (cbPortRange >= 0x10000)
651 {
652 /* More than 64K seems to be an invalid port range string. */
653 rc = VERR_TOO_MUCH_DATA;
654 break;
655 }
656
657 if ((size_t)cbBuffer >= cbPortRange)
658 {
659 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
660 rc = VINF_SUCCESS;
661 }
662 else
663 {
664 rc = VINF_BUFFER_OVERFLOW;
665 }
666
667 *pcbOut = (uint32_t)cbPortRange;
668 } break;
669
670 case VRDE_QP_VIDEO_CHANNEL:
671 {
672 com::Bstr bstr;
673 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(), bstr.asOutParam());
674
675 if (hrc != S_OK)
676 {
677 bstr = "";
678 }
679
680 com::Utf8Str value = bstr;
681
682 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
683 || RTStrICmp(value.c_str(), "1") == 0;
684
685 if (cbBuffer >= sizeof(uint32_t))
686 {
687 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
688 rc = VINF_SUCCESS;
689 }
690 else
691 {
692 rc = VINF_BUFFER_OVERFLOW;
693 }
694
695 *pcbOut = sizeof(uint32_t);
696 } break;
697
698 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
699 {
700 com::Bstr bstr;
701 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(), bstr.asOutParam());
702
703 if (hrc != S_OK)
704 {
705 bstr = "";
706 }
707
708 com::Utf8Str value = bstr;
709
710 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
711
712 if (cbBuffer >= sizeof(uint32_t))
713 {
714 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
715 rc = VINF_SUCCESS;
716 }
717 else
718 {
719 rc = VINF_BUFFER_OVERFLOW;
720 }
721
722 *pcbOut = sizeof(uint32_t);
723 } break;
724
725 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
726 {
727 ULONG ulSunFlsh = 1;
728
729 com::Bstr bstr;
730 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
731 bstr.asOutParam());
732 if (hrc == S_OK && !bstr.isEmpty())
733 {
734 com::Utf8Str sunFlsh = bstr;
735 if (!sunFlsh.isEmpty())
736 {
737 ulSunFlsh = sunFlsh.toUInt32();
738 }
739 }
740
741 if (cbBuffer >= sizeof(uint32_t))
742 {
743 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
744 rc = VINF_SUCCESS;
745 }
746 else
747 {
748 rc = VINF_BUFFER_OVERFLOW;
749 }
750
751 *pcbOut = sizeof(uint32_t);
752 } break;
753
754 case VRDE_QP_FEATURE:
755 {
756 if (cbBuffer < sizeof(VRDEFEATURE))
757 {
758 rc = VERR_INVALID_PARAMETER;
759 break;
760 }
761
762 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
763
764 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
765
766 size_t cchInfo = 0;
767 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
768
769 if (RT_FAILURE(rc))
770 {
771 rc = VERR_INVALID_PARAMETER;
772 break;
773 }
774
775 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
776
777 com::Bstr bstrValue;
778
779 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
780 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
781 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
782 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
783 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
784 )
785 {
786 /* @todo these features should be per client. */
787 NOREF(pFeature->u32ClientId);
788
789 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
790 com::Utf8Str extraData("VRDE/Feature/");
791 extraData += pFeature->achInfo;
792
793 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
794 bstrValue.asOutParam());
795 if (FAILED(hrc) || bstrValue.isEmpty())
796 {
797 /* Also try the old "VRDP/Feature/NAME" */
798 extraData = "VRDP/Feature/";
799 extraData += pFeature->achInfo;
800
801 hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
802 bstrValue.asOutParam());
803 if (FAILED(hrc))
804 {
805 rc = VERR_NOT_SUPPORTED;
806 }
807 }
808 }
809 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
810 {
811 /* Generic properties. */
812 const char *pszPropertyName = &pFeature->achInfo[9];
813 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(), bstrValue.asOutParam());
814 if (FAILED(hrc))
815 {
816 rc = VERR_NOT_SUPPORTED;
817 }
818 }
819 else
820 {
821 rc = VERR_NOT_SUPPORTED;
822 }
823
824 /* Copy the value string to the callers buffer. */
825 if (rc == VINF_SUCCESS)
826 {
827 com::Utf8Str value = bstrValue;
828
829 size_t cb = value.length() + 1;
830
831 if ((size_t)cbInfo >= cb)
832 {
833 memcpy(pFeature->achInfo, value.c_str(), cb);
834 }
835 else
836 {
837 rc = VINF_BUFFER_OVERFLOW;
838 }
839
840 *pcbOut = (uint32_t)cb;
841 }
842 } break;
843
844 case VRDE_SP_NETWORK_BIND_PORT:
845 {
846 if (cbBuffer != sizeof(uint32_t))
847 {
848 rc = VERR_INVALID_PARAMETER;
849 break;
850 }
851
852 ULONG port = *(uint32_t *)pvBuffer;
853
854 server->mVRDPBindPort = port;
855
856 rc = VINF_SUCCESS;
857
858 if (pcbOut)
859 {
860 *pcbOut = sizeof(uint32_t);
861 }
862
863 server->mConsole->onVRDEServerInfoChange();
864 } break;
865
866 case VRDE_SP_CLIENT_STATUS:
867 {
868 if (cbBuffer < sizeof(VRDECLIENTSTATUS))
869 {
870 rc = VERR_INVALID_PARAMETER;
871 break;
872 }
873
874 size_t cbStatus = cbBuffer - RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus);
875
876 VRDECLIENTSTATUS *pStatus = (VRDECLIENTSTATUS *)pvBuffer;
877
878 if (cbBuffer < RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus) + pStatus->cbStatus)
879 {
880 rc = VERR_INVALID_PARAMETER;
881 break;
882 }
883
884 size_t cchStatus = 0;
885 rc = RTStrNLenEx(pStatus->achStatus, cbStatus, &cchStatus);
886
887 if (RT_FAILURE(rc))
888 {
889 rc = VERR_INVALID_PARAMETER;
890 break;
891 }
892
893 Log(("VRDE_SP_CLIENT_STATUS [%s]\n", pStatus->achStatus));
894
895 server->mConsole->VRDPClientStatusChange(pStatus->u32ClientId, pStatus->achStatus);
896
897 rc = VINF_SUCCESS;
898
899 if (pcbOut)
900 {
901 *pcbOut = cbBuffer;
902 }
903
904 server->mConsole->onVRDEServerInfoChange();
905 } break;
906
907 default:
908 break;
909 }
910
911 return rc;
912}
913
914DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
915{
916 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
917
918 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
919}
920
921DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
922{
923 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
924
925 server->mConsole->VRDPClientConnect(u32ClientId);
926
927 /* Should the server report usage of an interface for each client?
928 * Similar to Intercept.
929 */
930 int c = ASMAtomicIncS32(&server->mcClients);
931 if (c == 1)
932 {
933 /* Features which should be enabled only if there is a client. */
934 server->remote3DRedirect(true);
935 }
936}
937
938DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
939{
940 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
941
942 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
943
944 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
945 {
946 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
947 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
948
949 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
950 if (pPort)
951 {
952 pPort->pfnAudioInputIntercept(pPort, false);
953 }
954 else
955 {
956 AssertFailed();
957 }
958 }
959
960 int c = ASMAtomicDecS32(&server->mcClients);
961 if (c == 0)
962 {
963 /* Features which should be enabled only if there is a client. */
964 server->remote3DRedirect(false);
965 }
966}
967
968DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
969{
970 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
971
972 LogFlowFunc(("%x\n", fu32Intercept));
973
974 int rc = VERR_NOT_SUPPORTED;
975
976 switch (fu32Intercept)
977 {
978 case VRDE_CLIENT_INTERCEPT_AUDIO:
979 {
980 server->mConsole->VRDPInterceptAudio(u32ClientId);
981 if (ppvIntercept)
982 {
983 *ppvIntercept = server;
984 }
985 rc = VINF_SUCCESS;
986 } break;
987
988 case VRDE_CLIENT_INTERCEPT_USB:
989 {
990 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
991 rc = VINF_SUCCESS;
992 } break;
993
994 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
995 {
996 server->mConsole->VRDPInterceptClipboard(u32ClientId);
997 if (ppvIntercept)
998 {
999 *ppvIntercept = server;
1000 }
1001 rc = VINF_SUCCESS;
1002 } break;
1003
1004 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
1005 {
1006 /* This request is processed internally by the ConsoleVRDPServer.
1007 * Only one client is allowed to intercept audio input.
1008 */
1009 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
1010 {
1011 Log(("AUDIOIN: connected client %u\n", u32ClientId));
1012
1013 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1014 if (pPort)
1015 {
1016 pPort->pfnAudioInputIntercept(pPort, true);
1017 if (ppvIntercept)
1018 {
1019 *ppvIntercept = server;
1020 }
1021 }
1022 else
1023 {
1024 AssertFailed();
1025 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
1026 rc = VERR_NOT_SUPPORTED;
1027 }
1028 }
1029 else
1030 {
1031 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
1032 rc = VERR_NOT_SUPPORTED;
1033 }
1034 } break;
1035
1036 default:
1037 break;
1038 }
1039
1040 return rc;
1041}
1042
1043DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1044{
1045#ifdef VBOX_WITH_USB
1046 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1047#else
1048 return VERR_NOT_SUPPORTED;
1049#endif
1050}
1051
1052DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
1053{
1054 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1055}
1056
1057DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
1058{
1059 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1060
1061 bool fAvailable = false;
1062
1063 IFramebuffer *pfb = NULL;
1064 LONG xOrigin = 0;
1065 LONG yOrigin = 0;
1066
1067 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1068
1069 if (pfb)
1070 {
1071 pfb->Lock ();
1072
1073 /* Query framebuffer parameters. */
1074 ULONG lineSize = 0;
1075 pfb->COMGETTER(BytesPerLine)(&lineSize);
1076
1077 ULONG bitsPerPixel = 0;
1078 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1079
1080 BYTE *address = NULL;
1081 pfb->COMGETTER(Address)(&address);
1082
1083 ULONG height = 0;
1084 pfb->COMGETTER(Height)(&height);
1085
1086 ULONG width = 0;
1087 pfb->COMGETTER(Width)(&width);
1088
1089 /* Now fill the information as requested by the caller. */
1090 pInfo->pu8Bits = address;
1091 pInfo->xOrigin = xOrigin;
1092 pInfo->yOrigin = yOrigin;
1093 pInfo->cWidth = width;
1094 pInfo->cHeight = height;
1095 pInfo->cBitsPerPixel = bitsPerPixel;
1096 pInfo->cbLine = lineSize;
1097
1098 pfb->Unlock();
1099
1100 fAvailable = true;
1101 }
1102
1103 if (server->maFramebuffers[uScreenId])
1104 {
1105 server->maFramebuffers[uScreenId]->Release();
1106 }
1107 server->maFramebuffers[uScreenId] = pfb;
1108
1109 return fAvailable;
1110}
1111
1112DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1113{
1114 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1115
1116 if (server->maFramebuffers[uScreenId])
1117 {
1118 server->maFramebuffers[uScreenId]->Lock();
1119 }
1120}
1121
1122DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1123{
1124 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1125
1126 if (server->maFramebuffers[uScreenId])
1127 {
1128 server->maFramebuffers[uScreenId]->Unlock();
1129 }
1130}
1131
1132static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1133{
1134 if ( pInputSynch->cGuestNumLockAdaptions
1135 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1136 {
1137 pInputSynch->cGuestNumLockAdaptions--;
1138 pKeyboard->PutScancode(0x45);
1139 pKeyboard->PutScancode(0x45 | 0x80);
1140 }
1141 if ( pInputSynch->cGuestCapsLockAdaptions
1142 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1143 {
1144 pInputSynch->cGuestCapsLockAdaptions--;
1145 pKeyboard->PutScancode(0x3a);
1146 pKeyboard->PutScancode(0x3a | 0x80);
1147 }
1148}
1149
1150DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1151{
1152 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1153 Console *pConsole = server->mConsole;
1154
1155 switch (type)
1156 {
1157 case VRDE_INPUT_SCANCODE:
1158 {
1159 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1160 {
1161 IKeyboard *pKeyboard = pConsole->getKeyboard();
1162
1163 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1164
1165 /* Track lock keys. */
1166 if (pInputScancode->uScancode == 0x45)
1167 {
1168 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1169 }
1170 else if (pInputScancode->uScancode == 0x3a)
1171 {
1172 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1173 }
1174 else if (pInputScancode->uScancode == 0x46)
1175 {
1176 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1177 }
1178 else if ((pInputScancode->uScancode & 0x80) == 0)
1179 {
1180 /* Key pressed. */
1181 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1182 }
1183
1184 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1185 }
1186 } break;
1187
1188 case VRDE_INPUT_POINT:
1189 {
1190 if (cbInput == sizeof(VRDEINPUTPOINT))
1191 {
1192 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1193
1194 int mouseButtons = 0;
1195 int iWheel = 0;
1196
1197 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1198 {
1199 mouseButtons |= MouseButtonState_LeftButton;
1200 }
1201 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1202 {
1203 mouseButtons |= MouseButtonState_RightButton;
1204 }
1205 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1206 {
1207 mouseButtons |= MouseButtonState_MiddleButton;
1208 }
1209 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1210 {
1211 mouseButtons |= MouseButtonState_WheelUp;
1212 iWheel = -1;
1213 }
1214 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1215 {
1216 mouseButtons |= MouseButtonState_WheelDown;
1217 iWheel = 1;
1218 }
1219
1220 if (server->m_fGuestWantsAbsolute)
1221 {
1222 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1223 } else
1224 {
1225 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1226 pInputPoint->y - server->m_mousey,
1227 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1228 server->m_mousex = pInputPoint->x;
1229 server->m_mousey = pInputPoint->y;
1230 }
1231 }
1232 } break;
1233
1234 case VRDE_INPUT_CAD:
1235 {
1236 pConsole->getKeyboard()->PutCAD();
1237 } break;
1238
1239 case VRDE_INPUT_RESET:
1240 {
1241 pConsole->Reset();
1242 } break;
1243
1244 case VRDE_INPUT_SYNCH:
1245 {
1246 if (cbInput == sizeof(VRDEINPUTSYNCH))
1247 {
1248 IKeyboard *pKeyboard = pConsole->getKeyboard();
1249
1250 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1251
1252 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1253 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1254 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1255
1256 /* The client initiated synchronization. Always make the guest to reflect the client state.
1257 * Than means, when the guest changes the state itself, it is forced to return to the client
1258 * state.
1259 */
1260 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1261 {
1262 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1263 }
1264
1265 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1266 {
1267 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1268 }
1269
1270 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1271 }
1272 } break;
1273
1274 default:
1275 break;
1276 }
1277}
1278
1279DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1280{
1281 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1282
1283 server->mConsole->getDisplay()->SetVideoModeHint(uScreenId, TRUE /*=enabled*/,
1284 FALSE /*=changeOrigin*/, 0/*=OriginX*/, 0/*=OriginY*/,
1285 cWidth, cHeight, cBitsPerPixel);
1286}
1287
1288DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1289 void *pvCtx,
1290 uint32_t u32ClientId,
1291 uint32_t u32Event,
1292 const void *pvData,
1293 uint32_t cbData)
1294{
1295 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1296
1297 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1298
1299 switch (u32Event)
1300 {
1301 case VRDE_AUDIOIN_BEGIN:
1302 {
1303 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1304
1305 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1306 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1307 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1308 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1309 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1310 );
1311 } break;
1312
1313 case VRDE_AUDIOIN_DATA:
1314 {
1315 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1316 } break;
1317
1318 case VRDE_AUDIOIN_END:
1319 {
1320 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1321 } break;
1322
1323 default:
1324 return;
1325 }
1326}
1327
1328
1329ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1330{
1331 mConsole = console;
1332
1333 int rc = RTCritSectInit(&mCritSect);
1334 AssertRC(rc);
1335
1336 mcClipboardRefs = 0;
1337 mpfnClipboardCallback = NULL;
1338
1339#ifdef VBOX_WITH_USB
1340 mUSBBackends.pHead = NULL;
1341 mUSBBackends.pTail = NULL;
1342
1343 mUSBBackends.thread = NIL_RTTHREAD;
1344 mUSBBackends.fThreadRunning = false;
1345 mUSBBackends.event = 0;
1346#endif
1347
1348 mhServer = 0;
1349 mServerInterfaceVersion = 0;
1350
1351 mcInResize = 0;
1352
1353 m_fGuestWantsAbsolute = false;
1354 m_mousex = 0;
1355 m_mousey = 0;
1356
1357 m_InputSynch.cGuestNumLockAdaptions = 2;
1358 m_InputSynch.cGuestCapsLockAdaptions = 2;
1359
1360 m_InputSynch.fGuestNumLock = false;
1361 m_InputSynch.fGuestCapsLock = false;
1362 m_InputSynch.fGuestScrollLock = false;
1363
1364 m_InputSynch.fClientNumLock = false;
1365 m_InputSynch.fClientCapsLock = false;
1366 m_InputSynch.fClientScrollLock = false;
1367
1368 RT_ZERO(maFramebuffers);
1369
1370 {
1371 ComPtr<IEventSource> es;
1372 console->COMGETTER(EventSource)(es.asOutParam());
1373 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1374 aConsoleListener.createObject();
1375 aConsoleListener->init(new VRDPConsoleListener(), this);
1376 mConsoleListener = aConsoleListener;
1377 com::SafeArray <VBoxEventType_T> eventTypes;
1378 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1379 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1380 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1381 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1382 }
1383
1384 mVRDPBindPort = -1;
1385
1386 mAuthLibrary = 0;
1387
1388 mu32AudioInputClientId = 0;
1389 mcClients = 0;
1390
1391 /*
1392 * Optional interfaces.
1393 */
1394 m_fInterfaceImage = false;
1395 RT_ZERO(m_interfaceImage);
1396 RT_ZERO(m_interfaceCallbacksImage);
1397 RT_ZERO(m_interfaceMousePtr);
1398 RT_ZERO(m_interfaceSCard);
1399 RT_ZERO(m_interfaceCallbacksSCard);
1400 RT_ZERO(m_interfaceTSMF);
1401 RT_ZERO(m_interfaceCallbacksTSMF);
1402 RT_ZERO(m_interfaceVideoIn);
1403 RT_ZERO(m_interfaceCallbacksVideoIn);
1404 RT_ZERO(m_interfaceInput);
1405 RT_ZERO(m_interfaceCallbacksInput);
1406
1407 rc = RTCritSectInit(&mTSMFLock);
1408 AssertRC(rc);
1409}
1410
1411ConsoleVRDPServer::~ConsoleVRDPServer()
1412{
1413 Stop();
1414
1415 if (mConsoleListener)
1416 {
1417 ComPtr<IEventSource> es;
1418 mConsole->COMGETTER(EventSource)(es.asOutParam());
1419 es->UnregisterListener(mConsoleListener);
1420 mConsoleListener.setNull();
1421 }
1422
1423 unsigned i;
1424 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1425 {
1426 if (maFramebuffers[i])
1427 {
1428 maFramebuffers[i]->Release();
1429 maFramebuffers[i] = NULL;
1430 }
1431 }
1432
1433 if (RTCritSectIsInitialized(&mCritSect))
1434 {
1435 RTCritSectDelete(&mCritSect);
1436 RT_ZERO(mCritSect);
1437 }
1438
1439 if (RTCritSectIsInitialized(&mTSMFLock))
1440 {
1441 RTCritSectDelete(&mTSMFLock);
1442 RT_ZERO(mTSMFLock);
1443 }
1444}
1445
1446int ConsoleVRDPServer::Launch(void)
1447{
1448 LogFlowThisFunc(("\n"));
1449
1450 IVRDEServer *server = mConsole->getVRDEServer();
1451 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1452
1453 /*
1454 * Check if VRDE is enabled.
1455 */
1456 BOOL fEnabled;
1457 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1458 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1459 if (!fEnabled)
1460 return VINF_SUCCESS;
1461
1462 /*
1463 * Check that a VRDE extension pack name is set and resolve it into a
1464 * library path.
1465 */
1466 Bstr bstrExtPack;
1467 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1468 if (FAILED(hrc))
1469 return Global::vboxStatusCodeFromCOM(hrc);
1470 if (bstrExtPack.isEmpty())
1471 return VINF_NOT_SUPPORTED;
1472
1473 Utf8Str strExtPack(bstrExtPack);
1474 Utf8Str strVrdeLibrary;
1475 int vrc = VINF_SUCCESS;
1476 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1477 strVrdeLibrary = "VBoxVRDP";
1478 else
1479 {
1480#ifdef VBOX_WITH_EXTPACK
1481 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1482 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1483#else
1484 vrc = VERR_FILE_NOT_FOUND;
1485#endif
1486 }
1487 if (RT_SUCCESS(vrc))
1488 {
1489 /*
1490 * Load the VRDE library and start the server, if it is enabled.
1491 */
1492 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1493 if (RT_SUCCESS(vrc))
1494 {
1495 VRDEENTRYPOINTS_4 *pEntryPoints4;
1496 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1497
1498 if (RT_SUCCESS(vrc))
1499 {
1500 mServerInterfaceVersion = 4;
1501 mEntryPoints = *pEntryPoints4;
1502 mpEntryPoints = &mEntryPoints;
1503 }
1504 else if (vrc == VERR_VERSION_MISMATCH)
1505 {
1506 /* An older version of VRDE is installed, try version 3. */
1507 VRDEENTRYPOINTS_3 *pEntryPoints3;
1508
1509 static VRDECALLBACKS_3 sCallbacks3 =
1510 {
1511 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1512 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1513 ConsoleVRDPServer::VRDPCallbackClientLogon,
1514 ConsoleVRDPServer::VRDPCallbackClientConnect,
1515 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1516 ConsoleVRDPServer::VRDPCallbackIntercept,
1517 ConsoleVRDPServer::VRDPCallbackUSB,
1518 ConsoleVRDPServer::VRDPCallbackClipboard,
1519 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1520 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1521 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1522 ConsoleVRDPServer::VRDPCallbackInput,
1523 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1524 ConsoleVRDPServer::VRDECallbackAudioIn
1525 };
1526
1527 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1528 if (RT_SUCCESS(vrc))
1529 {
1530 mServerInterfaceVersion = 3;
1531 mEntryPoints.header = pEntryPoints3->header;
1532 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1533 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1534 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1535 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1536 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1537 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1538 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1539 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1540 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1541 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1542 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1543 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1544 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1545 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1546 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1547 mEntryPoints.VRDEGetInterface = NULL;
1548 mpEntryPoints = &mEntryPoints;
1549 }
1550 else if (vrc == VERR_VERSION_MISMATCH)
1551 {
1552 /* An older version of VRDE is installed, try version 1. */
1553 VRDEENTRYPOINTS_1 *pEntryPoints1;
1554
1555 static VRDECALLBACKS_1 sCallbacks1 =
1556 {
1557 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1558 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1559 ConsoleVRDPServer::VRDPCallbackClientLogon,
1560 ConsoleVRDPServer::VRDPCallbackClientConnect,
1561 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1562 ConsoleVRDPServer::VRDPCallbackIntercept,
1563 ConsoleVRDPServer::VRDPCallbackUSB,
1564 ConsoleVRDPServer::VRDPCallbackClipboard,
1565 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1566 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1567 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1568 ConsoleVRDPServer::VRDPCallbackInput,
1569 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1570 };
1571
1572 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1573 if (RT_SUCCESS(vrc))
1574 {
1575 mServerInterfaceVersion = 1;
1576 mEntryPoints.header = pEntryPoints1->header;
1577 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1578 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1579 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1580 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1581 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1582 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1583 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1584 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1585 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1586 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1587 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1588 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1589 mEntryPoints.VRDERedirect = NULL;
1590 mEntryPoints.VRDEAudioInOpen = NULL;
1591 mEntryPoints.VRDEAudioInClose = NULL;
1592 mEntryPoints.VRDEGetInterface = NULL;
1593 mpEntryPoints = &mEntryPoints;
1594 }
1595 }
1596 }
1597
1598 if (RT_SUCCESS(vrc))
1599 {
1600 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1601
1602 if (mServerInterfaceVersion >= 4)
1603 {
1604 /* The server supports optional interfaces. */
1605 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1606
1607 /* Image interface. */
1608 m_interfaceImage.header.u64Version = 1;
1609 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1610
1611 m_interfaceCallbacksImage.header.u64Version = 1;
1612 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1613 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1614
1615 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1616 VRDE_IMAGE_INTERFACE_NAME,
1617 &m_interfaceImage.header,
1618 &m_interfaceCallbacksImage.header,
1619 this);
1620 if (RT_SUCCESS(vrc))
1621 {
1622 LogRel(("VRDE: [%s]\n", VRDE_IMAGE_INTERFACE_NAME));
1623 m_fInterfaceImage = true;
1624 }
1625
1626 /* Mouse pointer interface. */
1627 m_interfaceMousePtr.header.u64Version = 1;
1628 m_interfaceMousePtr.header.u64Size = sizeof(m_interfaceMousePtr);
1629
1630 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1631 VRDE_MOUSEPTR_INTERFACE_NAME,
1632 &m_interfaceMousePtr.header,
1633 NULL,
1634 this);
1635 if (RT_SUCCESS(vrc))
1636 {
1637 LogRel(("VRDE: [%s]\n", VRDE_MOUSEPTR_INTERFACE_NAME));
1638 }
1639 else
1640 {
1641 RT_ZERO(m_interfaceMousePtr);
1642 }
1643
1644 /* Smartcard interface. */
1645 m_interfaceSCard.header.u64Version = 1;
1646 m_interfaceSCard.header.u64Size = sizeof(m_interfaceSCard);
1647
1648 m_interfaceCallbacksSCard.header.u64Version = 1;
1649 m_interfaceCallbacksSCard.header.u64Size = sizeof(m_interfaceCallbacksSCard);
1650 m_interfaceCallbacksSCard.VRDESCardCbNotify = VRDESCardCbNotify;
1651 m_interfaceCallbacksSCard.VRDESCardCbResponse = VRDESCardCbResponse;
1652
1653 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1654 VRDE_SCARD_INTERFACE_NAME,
1655 &m_interfaceSCard.header,
1656 &m_interfaceCallbacksSCard.header,
1657 this);
1658 if (RT_SUCCESS(vrc))
1659 {
1660 LogRel(("VRDE: [%s]\n", VRDE_SCARD_INTERFACE_NAME));
1661 }
1662 else
1663 {
1664 RT_ZERO(m_interfaceSCard);
1665 }
1666
1667 /* Raw TSMF interface. */
1668 m_interfaceTSMF.header.u64Version = 1;
1669 m_interfaceTSMF.header.u64Size = sizeof(m_interfaceTSMF);
1670
1671 m_interfaceCallbacksTSMF.header.u64Version = 1;
1672 m_interfaceCallbacksTSMF.header.u64Size = sizeof(m_interfaceCallbacksTSMF);
1673 m_interfaceCallbacksTSMF.VRDETSMFCbNotify = VRDETSMFCbNotify;
1674
1675 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1676 VRDE_TSMF_INTERFACE_NAME,
1677 &m_interfaceTSMF.header,
1678 &m_interfaceCallbacksTSMF.header,
1679 this);
1680 if (RT_SUCCESS(vrc))
1681 {
1682 LogRel(("VRDE: [%s]\n", VRDE_TSMF_INTERFACE_NAME));
1683 }
1684 else
1685 {
1686 RT_ZERO(m_interfaceTSMF);
1687 }
1688
1689 /* VideoIn interface. */
1690 m_interfaceVideoIn.header.u64Version = 1;
1691 m_interfaceVideoIn.header.u64Size = sizeof(m_interfaceVideoIn);
1692
1693 m_interfaceCallbacksVideoIn.header.u64Version = 1;
1694 m_interfaceCallbacksVideoIn.header.u64Size = sizeof(m_interfaceCallbacksVideoIn);
1695 m_interfaceCallbacksVideoIn.VRDECallbackVideoInNotify = VRDECallbackVideoInNotify;
1696 m_interfaceCallbacksVideoIn.VRDECallbackVideoInDeviceDesc = VRDECallbackVideoInDeviceDesc;
1697 m_interfaceCallbacksVideoIn.VRDECallbackVideoInControl = VRDECallbackVideoInControl;
1698 m_interfaceCallbacksVideoIn.VRDECallbackVideoInFrame = VRDECallbackVideoInFrame;
1699
1700 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1701 VRDE_VIDEOIN_INTERFACE_NAME,
1702 &m_interfaceVideoIn.header,
1703 &m_interfaceCallbacksVideoIn.header,
1704 this);
1705 if (RT_SUCCESS(vrc))
1706 {
1707 LogRel(("VRDE: [%s]\n", VRDE_VIDEOIN_INTERFACE_NAME));
1708 }
1709 else
1710 {
1711 RT_ZERO(m_interfaceVideoIn);
1712 }
1713
1714 /* Input interface. */
1715 m_interfaceInput.header.u64Version = 1;
1716 m_interfaceInput.header.u64Size = sizeof(m_interfaceInput);
1717
1718 m_interfaceCallbacksInput.header.u64Version = 1;
1719 m_interfaceCallbacksInput.header.u64Size = sizeof(m_interfaceCallbacksInput);
1720 m_interfaceCallbacksInput.VRDECallbackInputSetup = VRDECallbackInputSetup;
1721 m_interfaceCallbacksInput.VRDECallbackInputEvent = VRDECallbackInputEvent;
1722
1723 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1724 VRDE_INPUT_INTERFACE_NAME,
1725 &m_interfaceInput.header,
1726 &m_interfaceCallbacksInput.header,
1727 this);
1728 if (RT_SUCCESS(vrc))
1729 {
1730 LogRel(("VRDE: [%s]\n", VRDE_INPUT_INTERFACE_NAME));
1731 }
1732 else
1733 {
1734 RT_ZERO(m_interfaceInput);
1735 }
1736
1737 /* Since these interfaces are optional, it is always a success here. */
1738 vrc = VINF_SUCCESS;
1739 }
1740#ifdef VBOX_WITH_USB
1741 remoteUSBThreadStart();
1742#endif
1743 }
1744 else
1745 {
1746 if (vrc != VERR_NET_ADDRESS_IN_USE)
1747 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1748 /* Don't unload the lib, because it prevents us trying again or
1749 because there may be other users? */
1750 }
1751 }
1752 }
1753
1754 return vrc;
1755}
1756
1757typedef struct H3DORInstance
1758{
1759 ConsoleVRDPServer *pThis;
1760 HVRDEIMAGE hImageBitmap;
1761 int32_t x;
1762 int32_t y;
1763 uint32_t w;
1764 uint32_t h;
1765 bool fCreated;
1766 bool fFallback;
1767} H3DORInstance;
1768
1769#define H3DORLOG Log
1770
1771/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1772 const char *pszFormat)
1773{
1774 H3DORLOG(("H3DORBegin: ctx %p [%s]\n", pvContext, pszFormat));
1775
1776 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof(H3DORInstance));
1777
1778 if (p)
1779 {
1780 p->pThis = (ConsoleVRDPServer *)pvContext;
1781 p->hImageBitmap = NULL;
1782 p->x = 0;
1783 p->y = 0;
1784 p->w = 0;
1785 p->h = 0;
1786 p->fCreated = false;
1787 p->fFallback = false;
1788
1789 /* Host 3D service passes the actual format of data in this redirect instance.
1790 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1791 */
1792 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1793 {
1794 /* Accept it. */
1795 }
1796 else
1797 {
1798 RTMemFree(p);
1799 p = NULL;
1800 }
1801 }
1802
1803 H3DORLOG(("H3DORBegin: ins %p\n", p));
1804
1805 /* Caller checks this for NULL. */
1806 *ppvInstance = p;
1807}
1808
1809/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1810 int32_t x, int32_t y, uint32_t w, uint32_t h)
1811{
1812 H3DORLOG(("H3DORGeometry: ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1813
1814 H3DORInstance *p = (H3DORInstance *)pvInstance;
1815 Assert(p);
1816 Assert(p->pThis);
1817
1818 /* @todo find out what to do if size changes to 0x0 from non zero */
1819 if (w == 0 || h == 0)
1820 {
1821 /* Do nothing. */
1822 return;
1823 }
1824
1825 RTRECT rect;
1826 rect.xLeft = x;
1827 rect.yTop = y;
1828 rect.xRight = x + w;
1829 rect.yBottom = y + h;
1830
1831 if (p->hImageBitmap)
1832 {
1833 /* An image handle has been already created,
1834 * check if it has the same size as the reported geometry.
1835 */
1836 if ( p->x == x
1837 && p->y == y
1838 && p->w == w
1839 && p->h == h)
1840 {
1841 H3DORLOG(("H3DORGeometry: geometry not changed\n"));
1842 /* Do nothing. Continue using the existing handle. */
1843 }
1844 else
1845 {
1846 int rc = p->fFallback?
1847 VERR_NOT_SUPPORTED: /* Try to go out of fallback mode. */
1848 p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1849 if (RT_SUCCESS(rc))
1850 {
1851 p->x = x;
1852 p->y = y;
1853 p->w = w;
1854 p->h = h;
1855 }
1856 else
1857 {
1858 /* The handle must be recreated. Delete existing handle here. */
1859 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1860 p->hImageBitmap = NULL;
1861 }
1862 }
1863 }
1864
1865 if (!p->hImageBitmap)
1866 {
1867 /* Create a new bitmap handle. */
1868 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1869 * Clipping can be done here or in VRDP server.
1870 * If VRDP does clipping, then uScreenId parameter
1871 * is not necessary and coords must be global.
1872 * (have to check which coords are used in opengl service).
1873 * Since all VRDE API uses a ScreenId,
1874 * the clipping must be done here in ConsoleVRDPServer
1875 */
1876 uint32_t fu32CompletionFlags = 0;
1877 p->fFallback = false;
1878 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1879 &p->hImageBitmap,
1880 p,
1881 u32ScreenId,
1882 VRDE_IMAGE_F_CREATE_CONTENT_3D
1883 | VRDE_IMAGE_F_CREATE_WINDOW,
1884 &rect,
1885 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1886 NULL,
1887 0,
1888 &fu32CompletionFlags);
1889 if (RT_FAILURE(rc))
1890 {
1891 /* No support for a 3D + WINDOW. Try bitmap updates. */
1892 H3DORLOG(("H3DORGeometry: Fallback to bitmaps\n"));
1893 fu32CompletionFlags = 0;
1894 p->fFallback = true;
1895 rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1896 &p->hImageBitmap,
1897 p,
1898 u32ScreenId,
1899 0,
1900 &rect,
1901 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1902 NULL,
1903 0,
1904 &fu32CompletionFlags);
1905 }
1906
1907 H3DORLOG(("H3DORGeometry: Image handle create %Rrc, flags 0x%RX32\n", rc, fu32CompletionFlags));
1908
1909 if (RT_SUCCESS(rc))
1910 {
1911 p->x = x;
1912 p->y = y;
1913 p->w = w;
1914 p->h = h;
1915
1916 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1917 {
1918 p->fCreated = true;
1919 }
1920 }
1921 else
1922 {
1923 p->hImageBitmap = NULL;
1924 p->w = 0;
1925 p->h = 0;
1926 }
1927 }
1928
1929 H3DORLOG(("H3DORGeometry: ins %p completed\n", pvInstance));
1930}
1931
1932/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1933 uint32_t cRects, const RTRECT *paRects)
1934{
1935 H3DORLOG(("H3DORVisibleRegion: ins %p %d\n", pvInstance, cRects));
1936
1937 H3DORInstance *p = (H3DORInstance *)pvInstance;
1938 Assert(p);
1939 Assert(p->pThis);
1940
1941 if (cRects == 0)
1942 {
1943 /* Complete image is visible. */
1944 RTRECT rect;
1945 rect.xLeft = p->x;
1946 rect.yTop = p->y;
1947 rect.xRight = p->x + p->w;
1948 rect.yBottom = p->y + p->h;
1949 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1950 1,
1951 &rect);
1952 }
1953 else
1954 {
1955 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1956 cRects,
1957 paRects);
1958 }
1959
1960 H3DORLOG(("H3DORVisibleRegion: ins %p completed\n", pvInstance));
1961}
1962
1963/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1964 void *pvData, uint32_t cbData)
1965{
1966 H3DORLOG(("H3DORFrame: ins %p %p %d\n", pvInstance, pvData, cbData));
1967
1968 H3DORInstance *p = (H3DORInstance *)pvInstance;
1969 Assert(p);
1970 Assert(p->pThis);
1971
1972 /* Currently only a topdown BGR0 bitmap format is supported. */
1973 VRDEIMAGEBITMAP image;
1974
1975 image.cWidth = p->w;
1976 image.cHeight = p->h;
1977 image.pvData = pvData;
1978 image.cbData = cbData;
1979 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
1980 image.iScanDelta = -4 * p->w;
1981
1982 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
1983 p->x,
1984 p->y,
1985 p->w,
1986 p->h,
1987 &image,
1988 sizeof(VRDEIMAGEBITMAP));
1989
1990 H3DORLOG(("H3DORFrame: ins %p completed\n", pvInstance));
1991}
1992
1993/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
1994{
1995 H3DORLOG(("H3DOREnd: ins %p\n", pvInstance));
1996
1997 H3DORInstance *p = (H3DORInstance *)pvInstance;
1998 Assert(p);
1999 Assert(p->pThis);
2000
2001 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
2002
2003 RTMemFree(p);
2004
2005 H3DORLOG(("H3DOREnd: ins %p completed\n", pvInstance));
2006}
2007
2008/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
2009 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
2010{
2011 int rc = VINF_SUCCESS;
2012
2013 H3DORLOG(("H3DORContextProperty: index %d\n", index));
2014
2015 if (index == H3DOR_PROP_FORMATS)
2016 {
2017 /* Return a comma separated list of supported formats. */
2018 static const char *pszSupportedFormats = H3DOR_FMT_RGBA_TOPDOWN;
2019 uint32_t cbOut = (uint32_t)strlen(pszSupportedFormats) + 1;
2020 if (cbOut <= cbBuffer)
2021 {
2022 memcpy(pvBuffer, pszSupportedFormats, cbOut);
2023 }
2024 else
2025 {
2026 rc = VERR_BUFFER_OVERFLOW;
2027 }
2028 *pcbOut = cbOut;
2029 }
2030 else
2031 {
2032 rc = VERR_NOT_SUPPORTED;
2033 }
2034
2035 H3DORLOG(("H3DORContextProperty: %Rrc\n", rc));
2036 return rc;
2037}
2038
2039void ConsoleVRDPServer::remote3DRedirect(bool fEnable)
2040{
2041 if (!m_fInterfaceImage)
2042 {
2043 /* No redirect without corresponding interface. */
2044 return;
2045 }
2046
2047 /* Check if 3D redirection has been enabled. It is enabled by default. */
2048 com::Bstr bstr;
2049 HRESULT hrc = mConsole->getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
2050
2051 com::Utf8Str value = hrc == S_OK? bstr: "";
2052
2053 bool fAllowed = RTStrICmp(value.c_str(), "true") == 0
2054 || RTStrICmp(value.c_str(), "1") == 0
2055 || value.c_str()[0] == 0;
2056
2057 if (!fAllowed && fEnable)
2058 {
2059 return;
2060 }
2061
2062 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
2063 H3DOUTPUTREDIRECT outputRedirect =
2064 {
2065 this,
2066 H3DORBegin,
2067 H3DORGeometry,
2068 H3DORVisibleRegion,
2069 H3DORFrame,
2070 H3DOREnd,
2071 H3DORContextProperty
2072 };
2073
2074 if (!fEnable)
2075 {
2076 /* This will tell the service to disable rediection. */
2077 RT_ZERO(outputRedirect);
2078 }
2079
2080 VBOXHGCMSVCPARM parm;
2081
2082 parm.type = VBOX_HGCM_SVC_PARM_PTR;
2083 parm.u.pointer.addr = &outputRedirect;
2084 parm.u.pointer.size = sizeof(outputRedirect);
2085
2086 VMMDev *pVMMDev = mConsole->getVMMDev();
2087
2088 if (!pVMMDev)
2089 {
2090 AssertMsgFailed(("remote3DRedirect no vmmdev\n"));
2091 return;
2092 }
2093
2094 int rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL",
2095 SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT,
2096 SHCRGL_CPARMS_SET_OUTPUT_REDIRECT,
2097 &parm);
2098
2099 if (!RT_SUCCESS(rc))
2100 {
2101 Log(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2102 return;
2103 }
2104
2105 LogRel(("VRDE: %s 3D redirect.\n", fEnable? "Enabled": "Disabled"));
2106
2107 return;
2108}
2109
2110/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
2111 void *pvUser,
2112 HVRDEIMAGE hVideo,
2113 uint32_t u32Id,
2114 void *pvData,
2115 uint32_t cbData)
2116{
2117 H3DORLOG(("H3DOR: VRDEImageCbNotify: pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
2118 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
2119
2120 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
2121 H3DORInstance *p = (H3DORInstance *)pvUser;
2122 Assert(p);
2123 Assert(p->pThis);
2124 Assert(p->pThis == pServer);
2125
2126 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
2127 {
2128 if (cbData != sizeof(uint32_t))
2129 {
2130 AssertFailed();
2131 return VERR_INVALID_PARAMETER;
2132 }
2133
2134 uint32_t u32StreamId = *(uint32_t *)pvData;
2135 H3DORLOG(("H3DOR: VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
2136 u32StreamId));
2137
2138 if (u32StreamId != 0)
2139 {
2140 p->fCreated = true; // @todo not needed?
2141 }
2142 else
2143 {
2144 /* The stream has not been created. */
2145 }
2146 }
2147
2148 return VINF_SUCCESS;
2149}
2150
2151#undef H3DORLOG
2152
2153/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbNotify(void *pvContext,
2154 uint32_t u32Id,
2155 void *pvData,
2156 uint32_t cbData)
2157{
2158#ifdef VBOX_WITH_USB_CARDREADER
2159 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2160 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2161 return pReader->VRDENotify(u32Id, pvData, cbData);
2162#else
2163 NOREF(pvContext);
2164 NOREF(u32Id);
2165 NOREF(pvData);
2166 NOREF(cbData);
2167 return VERR_NOT_SUPPORTED;
2168#endif
2169}
2170
2171/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbResponse(void *pvContext,
2172 int rcRequest,
2173 void *pvUser,
2174 uint32_t u32Function,
2175 void *pvData,
2176 uint32_t cbData)
2177{
2178#ifdef VBOX_WITH_USB_CARDREADER
2179 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2180 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2181 return pReader->VRDEResponse(rcRequest, pvUser, u32Function, pvData, cbData);
2182#else
2183 NOREF(pvContext);
2184 NOREF(rcRequest);
2185 NOREF(pvUser);
2186 NOREF(u32Function);
2187 NOREF(pvData);
2188 NOREF(cbData);
2189 return VERR_NOT_SUPPORTED;
2190#endif
2191}
2192
2193int ConsoleVRDPServer::SCardRequest(void *pvUser, uint32_t u32Function, const void *pvData, uint32_t cbData)
2194{
2195 int rc = VINF_SUCCESS;
2196
2197 if (mhServer && mpEntryPoints && m_interfaceSCard.VRDESCardRequest)
2198 {
2199 rc = m_interfaceSCard.VRDESCardRequest(mhServer, pvUser, u32Function, pvData, cbData);
2200 }
2201 else
2202 {
2203 rc = VERR_NOT_SUPPORTED;
2204 }
2205
2206 return rc;
2207}
2208
2209
2210struct TSMFHOSTCHCTX;
2211struct TSMFVRDPCTX;
2212
2213typedef struct TSMFHOSTCHCTX
2214{
2215 ConsoleVRDPServer *pThis;
2216
2217 struct TSMFVRDPCTX *pVRDPCtx; /* NULL if no corresponding host channel context. */
2218
2219 void *pvDataReceived;
2220 uint32_t cbDataReceived;
2221 uint32_t cbDataAllocated;
2222} TSMFHOSTCHCTX;
2223
2224typedef struct TSMFVRDPCTX
2225{
2226 ConsoleVRDPServer *pThis;
2227
2228 VBOXHOSTCHANNELCALLBACKS *pCallbacks;
2229 void *pvCallbacks;
2230
2231 TSMFHOSTCHCTX *pHostChCtx; /* NULL if no corresponding host channel context. */
2232
2233 uint32_t u32ChannelHandle;
2234} TSMFVRDPCTX;
2235
2236static int tsmfContextsAlloc(TSMFHOSTCHCTX **ppHostChCtx, TSMFVRDPCTX **ppVRDPCtx)
2237{
2238 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)RTMemAllocZ(sizeof(TSMFHOSTCHCTX));
2239 if (!pHostChCtx)
2240 {
2241 return VERR_NO_MEMORY;
2242 }
2243
2244 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)RTMemAllocZ(sizeof(TSMFVRDPCTX));
2245 if (!pVRDPCtx)
2246 {
2247 RTMemFree(pHostChCtx);
2248 return VERR_NO_MEMORY;
2249 }
2250
2251 *ppHostChCtx = pHostChCtx;
2252 *ppVRDPCtx = pVRDPCtx;
2253 return VINF_SUCCESS;
2254}
2255
2256int ConsoleVRDPServer::tsmfLock(void)
2257{
2258 int rc = RTCritSectEnter(&mTSMFLock);
2259 AssertRC(rc);
2260 return rc;
2261}
2262
2263void ConsoleVRDPServer::tsmfUnlock(void)
2264{
2265 RTCritSectLeave(&mTSMFLock);
2266}
2267
2268/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelAttach(void *pvProvider,
2269 void **ppvChannel,
2270 uint32_t u32Flags,
2271 VBOXHOSTCHANNELCALLBACKS *pCallbacks,
2272 void *pvCallbacks)
2273{
2274 LogFlowFunc(("\n"));
2275
2276 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvProvider);
2277
2278 /* Create 2 context structures: for the VRDP server and for the host service. */
2279 TSMFHOSTCHCTX *pHostChCtx = NULL;
2280 TSMFVRDPCTX *pVRDPCtx = NULL;
2281
2282 int rc = tsmfContextsAlloc(&pHostChCtx, &pVRDPCtx);
2283 if (RT_FAILURE(rc))
2284 {
2285 return rc;
2286 }
2287
2288 pHostChCtx->pThis = pThis;
2289 pHostChCtx->pVRDPCtx = pVRDPCtx;
2290
2291 pVRDPCtx->pThis = pThis;
2292 pVRDPCtx->pCallbacks = pCallbacks;
2293 pVRDPCtx->pvCallbacks = pvCallbacks;
2294 pVRDPCtx->pHostChCtx = pHostChCtx;
2295
2296 rc = pThis->m_interfaceTSMF.VRDETSMFChannelCreate(pThis->mhServer, pVRDPCtx, u32Flags);
2297
2298 if (RT_SUCCESS(rc))
2299 {
2300 /* @todo contexts should be in a list for accounting. */
2301 *ppvChannel = pHostChCtx;
2302 }
2303 else
2304 {
2305 RTMemFree(pHostChCtx);
2306 RTMemFree(pVRDPCtx);
2307 }
2308
2309 return rc;
2310}
2311
2312/* static */ DECLCALLBACK(void) ConsoleVRDPServer::tsmfHostChannelDetach(void *pvChannel)
2313{
2314 LogFlowFunc(("\n"));
2315
2316 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2317 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2318
2319 int rc = pThis->tsmfLock();
2320 if (RT_SUCCESS(rc))
2321 {
2322 bool fClose = false;
2323 uint32_t u32ChannelHandle = 0;
2324
2325 if (pHostChCtx->pVRDPCtx)
2326 {
2327 /* There is still a VRDP context for this channel. */
2328 pHostChCtx->pVRDPCtx->pHostChCtx = NULL;
2329 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2330 fClose = true;
2331 }
2332
2333 pThis->tsmfUnlock();
2334
2335 RTMemFree(pHostChCtx);
2336
2337 if (fClose)
2338 {
2339 LogFlowFunc(("Closing VRDE channel %d.\n", u32ChannelHandle));
2340 pThis->m_interfaceTSMF.VRDETSMFChannelClose(pThis->mhServer, u32ChannelHandle);
2341 }
2342 else
2343 {
2344 LogFlowFunc(("No VRDE channel.\n"));
2345 }
2346 }
2347}
2348
2349/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelSend(void *pvChannel,
2350 const void *pvData,
2351 uint32_t cbData)
2352{
2353 LogFlowFunc(("cbData %d\n", cbData));
2354
2355 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2356 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2357
2358 int rc = pThis->tsmfLock();
2359 if (RT_SUCCESS(rc))
2360 {
2361 bool fSend = false;
2362 uint32_t u32ChannelHandle = 0;
2363
2364 if (pHostChCtx->pVRDPCtx)
2365 {
2366 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2367 fSend = true;
2368 }
2369
2370 pThis->tsmfUnlock();
2371
2372 if (fSend)
2373 {
2374 LogFlowFunc(("Send to VRDE channel %d.\n", u32ChannelHandle));
2375 rc = pThis->m_interfaceTSMF.VRDETSMFChannelSend(pThis->mhServer, u32ChannelHandle,
2376 pvData, cbData);
2377 }
2378 }
2379
2380 return rc;
2381}
2382
2383/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelRecv(void *pvChannel,
2384 void *pvData,
2385 uint32_t cbData,
2386 uint32_t *pcbReceived,
2387 uint32_t *pcbRemaining)
2388{
2389 LogFlowFunc(("cbData %d\n", cbData));
2390
2391 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2392 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2393
2394 int rc = pThis->tsmfLock();
2395 if (RT_SUCCESS(rc))
2396 {
2397 uint32_t cbToCopy = RT_MIN(cbData, pHostChCtx->cbDataReceived);
2398 uint32_t cbRemaining = pHostChCtx->cbDataReceived - cbToCopy;
2399
2400 LogFlowFunc(("cbToCopy %d, cbRemaining %d\n", cbToCopy, cbRemaining));
2401
2402 if (cbToCopy != 0)
2403 {
2404 memcpy(pvData, pHostChCtx->pvDataReceived, cbToCopy);
2405
2406 if (cbRemaining != 0)
2407 {
2408 memmove(pHostChCtx->pvDataReceived, (uint8_t *)pHostChCtx->pvDataReceived + cbToCopy, cbRemaining);
2409 }
2410
2411 pHostChCtx->cbDataReceived = cbRemaining;
2412 }
2413
2414 pThis->tsmfUnlock();
2415
2416 *pcbRemaining = cbRemaining;
2417 *pcbReceived = cbToCopy;
2418 }
2419
2420 return rc;
2421}
2422
2423/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelControl(void *pvChannel,
2424 uint32_t u32Code,
2425 const void *pvParm,
2426 uint32_t cbParm,
2427 const void *pvData,
2428 uint32_t cbData,
2429 uint32_t *pcbDataReturned)
2430{
2431 LogFlowFunc(("u32Code %u\n", u32Code));
2432
2433 if (!pvChannel)
2434 {
2435 /* Special case, the provider must answer rather than a channel instance. */
2436 if (u32Code == VBOX_HOST_CHANNEL_CTRL_EXISTS)
2437 {
2438 *pcbDataReturned = 0;
2439 return VINF_SUCCESS;
2440 }
2441
2442 return VERR_NOT_IMPLEMENTED;
2443 }
2444
2445 /* Channels do not support this. */
2446 return VERR_NOT_IMPLEMENTED;
2447}
2448
2449
2450void ConsoleVRDPServer::setupTSMF(void)
2451{
2452 if (m_interfaceTSMF.header.u64Size == 0)
2453 {
2454 return;
2455 }
2456
2457 /* Register with the host channel service. */
2458 VBOXHOSTCHANNELINTERFACE hostChannelInterface =
2459 {
2460 this,
2461 tsmfHostChannelAttach,
2462 tsmfHostChannelDetach,
2463 tsmfHostChannelSend,
2464 tsmfHostChannelRecv,
2465 tsmfHostChannelControl
2466 };
2467
2468 VBoxHostChannelHostRegister parms;
2469
2470 static char szProviderName[] = "/vrde/tsmf";
2471
2472 parms.name.type = VBOX_HGCM_SVC_PARM_PTR;
2473 parms.name.u.pointer.addr = &szProviderName[0];
2474 parms.name.u.pointer.size = sizeof(szProviderName);
2475
2476 parms.iface.type = VBOX_HGCM_SVC_PARM_PTR;
2477 parms.iface.u.pointer.addr = &hostChannelInterface;
2478 parms.iface.u.pointer.size = sizeof(hostChannelInterface);
2479
2480 VMMDev *pVMMDev = mConsole->getVMMDev();
2481
2482 if (!pVMMDev)
2483 {
2484 AssertMsgFailed(("setupTSMF no vmmdev\n"));
2485 return;
2486 }
2487
2488 int rc = pVMMDev->hgcmHostCall("VBoxHostChannel",
2489 VBOX_HOST_CHANNEL_HOST_FN_REGISTER,
2490 2,
2491 &parms.name);
2492
2493 if (!RT_SUCCESS(rc))
2494 {
2495 Log(("VBOX_HOST_CHANNEL_HOST_FN_REGISTER failed with %Rrc\n", rc));
2496 return;
2497 }
2498
2499 LogRel(("VRDE: Enabled TSMF channel.\n"));
2500
2501 return;
2502}
2503
2504/* @todo these defines must be in a header, which is used by guest component as well. */
2505#define VBOX_TSMF_HCH_CREATE_ACCEPTED (VBOX_HOST_CHANNEL_EVENT_USER + 0)
2506#define VBOX_TSMF_HCH_CREATE_DECLINED (VBOX_HOST_CHANNEL_EVENT_USER + 1)
2507#define VBOX_TSMF_HCH_DISCONNECTED (VBOX_HOST_CHANNEL_EVENT_USER + 2)
2508
2509/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDETSMFCbNotify(void *pvContext,
2510 uint32_t u32Notification,
2511 void *pvChannel,
2512 const void *pvParm,
2513 uint32_t cbParm)
2514{
2515 int rc = VINF_SUCCESS;
2516
2517 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2518
2519 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)pvChannel;
2520
2521 Assert(pVRDPCtx->pThis == pThis);
2522
2523 if (pVRDPCtx->pCallbacks == NULL)
2524 {
2525 LogFlowFunc(("tsmfHostChannel: Channel disconnected. Skipping.\n"));
2526 return;
2527 }
2528
2529 switch (u32Notification)
2530 {
2531 case VRDE_TSMF_N_CREATE_ACCEPTED:
2532 {
2533 VRDETSMFNOTIFYCREATEACCEPTED *p = (VRDETSMFNOTIFYCREATEACCEPTED *)pvParm;
2534 Assert(cbParm == sizeof(VRDETSMFNOTIFYCREATEACCEPTED));
2535
2536 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_ACCEPTED(%p): p->u32ChannelHandle %d\n",
2537 pVRDPCtx, p->u32ChannelHandle));
2538
2539 pVRDPCtx->u32ChannelHandle = p->u32ChannelHandle;
2540
2541 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2542 VBOX_TSMF_HCH_CREATE_ACCEPTED,
2543 NULL, 0);
2544 } break;
2545
2546 case VRDE_TSMF_N_CREATE_DECLINED:
2547 {
2548 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_DECLINED(%p)\n", pVRDPCtx));
2549
2550 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2551 VBOX_TSMF_HCH_CREATE_DECLINED,
2552 NULL, 0);
2553 } break;
2554
2555 case VRDE_TSMF_N_DATA:
2556 {
2557 /* Save the data in the intermediate buffer and send the event. */
2558 VRDETSMFNOTIFYDATA *p = (VRDETSMFNOTIFYDATA *)pvParm;
2559 Assert(cbParm == sizeof(VRDETSMFNOTIFYDATA));
2560
2561 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA(%p): p->cbData %d\n", pVRDPCtx, p->cbData));
2562
2563 VBOXHOSTCHANNELEVENTRECV ev;
2564 ev.u32SizeAvailable = 0;
2565
2566 rc = pThis->tsmfLock();
2567
2568 if (RT_SUCCESS(rc))
2569 {
2570 TSMFHOSTCHCTX *pHostChCtx = pVRDPCtx->pHostChCtx;
2571
2572 if (pHostChCtx)
2573 {
2574 if (pHostChCtx->pvDataReceived)
2575 {
2576 uint32_t cbAlloc = p->cbData + pHostChCtx->cbDataReceived;
2577 pHostChCtx->pvDataReceived = RTMemRealloc(pHostChCtx->pvDataReceived, cbAlloc);
2578 memcpy((uint8_t *)pHostChCtx->pvDataReceived + pHostChCtx->cbDataReceived, p->pvData, p->cbData);
2579
2580 pHostChCtx->cbDataReceived += p->cbData;
2581 pHostChCtx->cbDataAllocated = cbAlloc;
2582 }
2583 else
2584 {
2585 pHostChCtx->pvDataReceived = RTMemAlloc(p->cbData);
2586 memcpy(pHostChCtx->pvDataReceived, p->pvData, p->cbData);
2587
2588 pHostChCtx->cbDataReceived = p->cbData;
2589 pHostChCtx->cbDataAllocated = p->cbData;
2590 }
2591
2592 ev.u32SizeAvailable = p->cbData;
2593 }
2594 else
2595 {
2596 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA: no host channel. Skipping\n"));
2597 }
2598
2599 pThis->tsmfUnlock();
2600 }
2601
2602 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2603 VBOX_HOST_CHANNEL_EVENT_RECV,
2604 &ev, sizeof(ev));
2605 } break;
2606
2607 case VRDE_TSMF_N_DISCONNECTED:
2608 {
2609 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DISCONNECTED(%p)\n", pVRDPCtx));
2610
2611 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2612 VBOX_TSMF_HCH_DISCONNECTED,
2613 NULL, 0);
2614
2615 /* The callback context will not be used anymore. */
2616 pVRDPCtx->pCallbacks->HostChannelCallbackDeleted(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx);
2617 pVRDPCtx->pCallbacks = NULL;
2618 pVRDPCtx->pvCallbacks = NULL;
2619
2620 rc = pThis->tsmfLock();
2621 if (RT_SUCCESS(rc))
2622 {
2623 if (pVRDPCtx->pHostChCtx)
2624 {
2625 /* There is still a host channel context for this channel. */
2626 pVRDPCtx->pHostChCtx->pVRDPCtx = NULL;
2627 }
2628
2629 pThis->tsmfUnlock();
2630
2631 RT_ZERO(*pVRDPCtx);
2632 RTMemFree(pVRDPCtx);
2633 }
2634 } break;
2635
2636 default:
2637 {
2638 AssertFailed();
2639 } break;
2640 }
2641}
2642
2643/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInNotify(void *pvCallback,
2644 uint32_t u32Id,
2645 const void *pvData,
2646 uint32_t cbData)
2647{
2648 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2649 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2650 pWebcam->EmWebcamCbNotify(u32Id, pvData, cbData);
2651}
2652
2653/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInDeviceDesc(void *pvCallback,
2654 int rcRequest,
2655 void *pDeviceCtx,
2656 void *pvUser,
2657 const VRDEVIDEOINDEVICEDESC *pDeviceDesc,
2658 uint32_t cbDevice)
2659{
2660 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2661 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2662 pWebcam->EmWebcamCbDeviceDesc(rcRequest, pDeviceCtx, pvUser, pDeviceDesc, cbDevice);
2663}
2664
2665/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInControl(void *pvCallback,
2666 int rcRequest,
2667 void *pDeviceCtx,
2668 void *pvUser,
2669 const VRDEVIDEOINCTRLHDR *pControl,
2670 uint32_t cbControl)
2671{
2672 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2673 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2674 pWebcam->EmWebcamCbControl(rcRequest, pDeviceCtx, pvUser, pControl, cbControl);
2675}
2676
2677/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInFrame(void *pvCallback,
2678 int rcRequest,
2679 void *pDeviceCtx,
2680 const VRDEVIDEOINPAYLOADHDR *pFrame,
2681 uint32_t cbFrame)
2682{
2683 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2684 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2685 pWebcam->EmWebcamCbFrame(rcRequest, pDeviceCtx, pFrame, cbFrame);
2686}
2687
2688int ConsoleVRDPServer::VideoInDeviceAttach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle, void *pvDeviceCtx)
2689{
2690 int rc;
2691
2692 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceAttach)
2693 {
2694 rc = m_interfaceVideoIn.VRDEVideoInDeviceAttach(mhServer, pDeviceHandle, pvDeviceCtx);
2695 }
2696 else
2697 {
2698 rc = VERR_NOT_SUPPORTED;
2699 }
2700
2701 return rc;
2702}
2703
2704int ConsoleVRDPServer::VideoInDeviceDetach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2705{
2706 int rc;
2707
2708 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceDetach)
2709 {
2710 rc = m_interfaceVideoIn.VRDEVideoInDeviceDetach(mhServer, pDeviceHandle);
2711 }
2712 else
2713 {
2714 rc = VERR_NOT_SUPPORTED;
2715 }
2716
2717 return rc;
2718}
2719
2720int ConsoleVRDPServer::VideoInGetDeviceDesc(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2721{
2722 int rc;
2723
2724 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInGetDeviceDesc)
2725 {
2726 rc = m_interfaceVideoIn.VRDEVideoInGetDeviceDesc(mhServer, pvUser, pDeviceHandle);
2727 }
2728 else
2729 {
2730 rc = VERR_NOT_SUPPORTED;
2731 }
2732
2733 return rc;
2734}
2735
2736int ConsoleVRDPServer::VideoInControl(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle,
2737 const VRDEVIDEOINCTRLHDR *pReq, uint32_t cbReq)
2738{
2739 int rc;
2740
2741 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInControl)
2742 {
2743 rc = m_interfaceVideoIn.VRDEVideoInControl(mhServer, pvUser, pDeviceHandle, pReq, cbReq);
2744 }
2745 else
2746 {
2747 rc = VERR_NOT_SUPPORTED;
2748 }
2749
2750 return rc;
2751}
2752
2753
2754/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputSetup(void *pvCallback,
2755 int rcRequest,
2756 uint32_t u32Method,
2757 const void *pvResult,
2758 uint32_t cbResult)
2759{
2760 NOREF(pvCallback);
2761 NOREF(rcRequest);
2762 NOREF(u32Method);
2763 NOREF(pvResult);
2764 NOREF(cbResult);
2765}
2766
2767/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputEvent(void *pvCallback,
2768 uint32_t u32Method,
2769 const void *pvEvent,
2770 uint32_t cbEvent)
2771{
2772 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2773
2774 if (u32Method == VRDE_INPUT_METHOD_TOUCH)
2775 {
2776 if (cbEvent >= sizeof(VRDEINPUTHEADER))
2777 {
2778 VRDEINPUTHEADER *pHeader = (VRDEINPUTHEADER *)pvEvent;
2779
2780 if (pHeader->u16EventId == VRDEINPUT_EVENTID_TOUCH)
2781 {
2782 IMouse *pMouse = pThis->mConsole->getMouse();
2783
2784 VRDEINPUT_TOUCH_EVENT_PDU *p = (VRDEINPUT_TOUCH_EVENT_PDU *)pHeader;
2785
2786 uint16_t iFrame;
2787 for (iFrame = 0; iFrame < p->u16FrameCount; iFrame++)
2788 {
2789 VRDEINPUT_TOUCH_FRAME *pFrame = &p->aFrames[iFrame];
2790
2791 com::SafeArray<LONG64> aContacts(pFrame->u16ContactCount);
2792
2793 uint16_t iContact;
2794 for (iContact = 0; iContact < pFrame->u16ContactCount; iContact++)
2795 {
2796 VRDEINPUT_CONTACT_DATA *pContact = &pFrame->aContacts[iContact];
2797
2798 int16_t x = (int16_t)(pContact->i32X + 1);
2799 int16_t y = (int16_t)(pContact->i32Y + 1);
2800 uint8_t contactId = pContact->u8ContactId;
2801 uint8_t contactState = TouchContactState_None;
2802
2803 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INRANGE)
2804 {
2805 contactState |= TouchContactState_InRange;
2806 }
2807 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INCONTACT)
2808 {
2809 contactState |= TouchContactState_InContact;
2810 }
2811
2812 aContacts[iContact] = RT_MAKE_U64_FROM_U16((uint16_t)x,
2813 (uint16_t)y,
2814 RT_MAKE_U16(contactId, contactState),
2815 0);
2816 }
2817
2818 if (pFrame->u64FrameOffset == 0)
2819 {
2820 pThis->mu64TouchInputTimestampMCS = 0;
2821 }
2822 else
2823 {
2824 pThis->mu64TouchInputTimestampMCS += pFrame->u64FrameOffset;
2825 }
2826
2827 pMouse->PutEventMultiTouch(pFrame->u16ContactCount,
2828 ComSafeArrayAsInParam(aContacts),
2829 (ULONG)(pThis->mu64TouchInputTimestampMCS / 1000)); /* Micro->milliseconds. */
2830 }
2831 }
2832 else if (pHeader->u16EventId == VRDEINPUT_EVENTID_DISMISS_HOVERING_CONTACT)
2833 {
2834 /* @todo */
2835 }
2836 else
2837 {
2838 AssertMsgFailed(("EventId %d\n", pHeader->u16EventId));
2839 }
2840 }
2841 }
2842}
2843
2844
2845void ConsoleVRDPServer::EnableConnections(void)
2846{
2847 if (mpEntryPoints && mhServer)
2848 {
2849 mpEntryPoints->VRDEEnableConnections(mhServer, true);
2850
2851 /* Setup the generic TSMF channel. */
2852 setupTSMF();
2853 }
2854}
2855
2856void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
2857{
2858 if (mpEntryPoints && mhServer)
2859 {
2860 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
2861 }
2862}
2863
2864int ConsoleVRDPServer::MousePointer(BOOL alpha,
2865 ULONG xHot,
2866 ULONG yHot,
2867 ULONG width,
2868 ULONG height,
2869 const uint8_t *pu8Shape)
2870{
2871 int rc = VINF_SUCCESS;
2872
2873 if (mhServer && mpEntryPoints && m_interfaceMousePtr.VRDEMousePtr)
2874 {
2875 size_t cbMask = (((width + 7) / 8) * height + 3) & ~3;
2876 size_t cbData = width * height * 4;
2877
2878 size_t cbDstMask = alpha? 0: cbMask;
2879
2880 size_t cbPointer = sizeof(VRDEMOUSEPTRDATA) + cbDstMask + cbData;
2881 uint8_t *pu8Pointer = (uint8_t *)RTMemAlloc(cbPointer);
2882 if (pu8Pointer != NULL)
2883 {
2884 VRDEMOUSEPTRDATA *pPointer = (VRDEMOUSEPTRDATA *)pu8Pointer;
2885
2886 pPointer->u16HotX = (uint16_t)xHot;
2887 pPointer->u16HotY = (uint16_t)yHot;
2888 pPointer->u16Width = (uint16_t)width;
2889 pPointer->u16Height = (uint16_t)height;
2890 pPointer->u16MaskLen = (uint16_t)cbDstMask;
2891 pPointer->u32DataLen = (uint32_t)cbData;
2892
2893 /* AND mask. */
2894 uint8_t *pu8Mask = pu8Pointer + sizeof(VRDEMOUSEPTRDATA);
2895 if (cbDstMask)
2896 {
2897 memcpy(pu8Mask, pu8Shape, cbDstMask);
2898 }
2899
2900 /* XOR mask */
2901 uint8_t *pu8Data = pu8Mask + pPointer->u16MaskLen;
2902 memcpy(pu8Data, pu8Shape + cbMask, cbData);
2903
2904 m_interfaceMousePtr.VRDEMousePtr(mhServer, pPointer);
2905
2906 RTMemFree(pu8Pointer);
2907 }
2908 else
2909 {
2910 rc = VERR_NO_MEMORY;
2911 }
2912 }
2913 else
2914 {
2915 rc = VERR_NOT_SUPPORTED;
2916 }
2917
2918 return rc;
2919}
2920
2921void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
2922{
2923 if (mpEntryPoints && mhServer)
2924 {
2925 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
2926 }
2927}
2928
2929void ConsoleVRDPServer::MousePointerHide(void)
2930{
2931 if (mpEntryPoints && mhServer)
2932 {
2933 mpEntryPoints->VRDEHidePointer(mhServer);
2934 }
2935}
2936
2937void ConsoleVRDPServer::Stop(void)
2938{
2939 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
2940 * linux. Just remove this when it's 100% sure that problem has been fixed. */
2941
2942#ifdef VBOX_WITH_USB
2943 remoteUSBThreadStop();
2944#endif /* VBOX_WITH_USB */
2945
2946 if (mhServer)
2947 {
2948 HVRDESERVER hServer = mhServer;
2949
2950 /* Reset the handle to avoid further calls to the server. */
2951 mhServer = 0;
2952
2953 /* Workaround for VM process hangs on termination.
2954 *
2955 * Make sure that the server is not currently processing a resize.
2956 * mhServer 0 will not allow to enter the server again.
2957 * Wait until any current resize returns from the server.
2958 */
2959 if (mcInResize)
2960 {
2961 LogRel(("VRDP: waiting for resize %d\n", mcInResize));
2962
2963 int i = 0;
2964 while (mcInResize && ++i < 100)
2965 {
2966 RTThreadSleep(10);
2967 }
2968 }
2969
2970 if (mpEntryPoints && hServer)
2971 {
2972 mpEntryPoints->VRDEDestroy(hServer);
2973 }
2974 }
2975
2976 mpfnAuthEntry = NULL;
2977 mpfnAuthEntry2 = NULL;
2978 mpfnAuthEntry3 = NULL;
2979
2980 if (mAuthLibrary)
2981 {
2982 RTLdrClose(mAuthLibrary);
2983 mAuthLibrary = 0;
2984 }
2985}
2986
2987/* Worker thread for Remote USB. The thread polls the clients for
2988 * the list of attached USB devices.
2989 * The thread is also responsible for attaching/detaching devices
2990 * to/from the VM.
2991 *
2992 * It is expected that attaching/detaching is not a frequent operation.
2993 *
2994 * The thread is always running when the VRDP server is active.
2995 *
2996 * The thread scans backends and requests the device list every 2 seconds.
2997 *
2998 * When device list is available, the thread calls the Console to process it.
2999 *
3000 */
3001#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
3002
3003#ifdef VBOX_WITH_USB
3004static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
3005{
3006 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
3007
3008 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
3009
3010 pOwner->notifyRemoteUSBThreadRunning(self);
3011
3012 while (pOwner->isRemoteUSBThreadRunning())
3013 {
3014 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3015
3016 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
3017 {
3018 pRemoteUSBBackend->PollRemoteDevices();
3019 }
3020
3021 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
3022
3023 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
3024 }
3025
3026 return VINF_SUCCESS;
3027}
3028
3029void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
3030{
3031 mUSBBackends.thread = thread;
3032 mUSBBackends.fThreadRunning = true;
3033 int rc = RTThreadUserSignal(thread);
3034 AssertRC(rc);
3035}
3036
3037bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
3038{
3039 return mUSBBackends.fThreadRunning;
3040}
3041
3042void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
3043{
3044 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
3045 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
3046 NOREF(rc);
3047}
3048
3049void ConsoleVRDPServer::remoteUSBThreadStart(void)
3050{
3051 int rc = RTSemEventCreate(&mUSBBackends.event);
3052
3053 if (RT_FAILURE(rc))
3054 {
3055 AssertFailed();
3056 mUSBBackends.event = 0;
3057 }
3058
3059 if (RT_SUCCESS(rc))
3060 {
3061 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
3062 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
3063 }
3064
3065 if (RT_FAILURE(rc))
3066 {
3067 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
3068 mUSBBackends.thread = NIL_RTTHREAD;
3069 }
3070 else
3071 {
3072 /* Wait until the thread is ready. */
3073 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
3074 AssertRC(rc);
3075 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
3076 }
3077}
3078
3079void ConsoleVRDPServer::remoteUSBThreadStop(void)
3080{
3081 mUSBBackends.fThreadRunning = false;
3082
3083 if (mUSBBackends.thread != NIL_RTTHREAD)
3084 {
3085 Assert (mUSBBackends.event != 0);
3086
3087 RTSemEventSignal(mUSBBackends.event);
3088
3089 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
3090 AssertRC(rc);
3091
3092 mUSBBackends.thread = NIL_RTTHREAD;
3093 }
3094
3095 if (mUSBBackends.event)
3096 {
3097 RTSemEventDestroy(mUSBBackends.event);
3098 mUSBBackends.event = 0;
3099 }
3100}
3101#endif /* VBOX_WITH_USB */
3102
3103typedef struct AuthCtx
3104{
3105 AuthResult result;
3106
3107 PAUTHENTRY3 pfnAuthEntry3;
3108 PAUTHENTRY2 pfnAuthEntry2;
3109 PAUTHENTRY pfnAuthEntry;
3110
3111 const char *pszCaller;
3112 PAUTHUUID pUuid;
3113 AuthGuestJudgement guestJudgement;
3114 const char *pszUser;
3115 const char *pszPassword;
3116 const char *pszDomain;
3117 int fLogon;
3118 unsigned clientId;
3119} AuthCtx;
3120
3121static DECLCALLBACK(int) authThread(RTTHREAD self, void *pvUser)
3122{
3123 AuthCtx *pCtx = (AuthCtx *)pvUser;
3124
3125 if (pCtx->pfnAuthEntry3)
3126 {
3127 pCtx->result = pCtx->pfnAuthEntry3(pCtx->pszCaller, pCtx->pUuid, pCtx->guestJudgement,
3128 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain,
3129 pCtx->fLogon, pCtx->clientId);
3130 }
3131 else if (pCtx->pfnAuthEntry2)
3132 {
3133 pCtx->result = pCtx->pfnAuthEntry2(pCtx->pUuid, pCtx->guestJudgement,
3134 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain,
3135 pCtx->fLogon, pCtx->clientId);
3136 }
3137 else if (pCtx->pfnAuthEntry)
3138 {
3139 pCtx->result = pCtx->pfnAuthEntry(pCtx->pUuid, pCtx->guestJudgement,
3140 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain);
3141 }
3142 return VINF_SUCCESS;
3143}
3144
3145static AuthResult authCall(AuthCtx *pCtx)
3146{
3147 AuthResult result = AuthResultAccessDenied;
3148
3149 /* Use a separate thread because external modules might need a lot of stack space. */
3150 RTTHREAD thread = NIL_RTTHREAD;
3151 int rc = RTThreadCreate(&thread, authThread, pCtx, 512*_1K,
3152 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "VRDEAuth");
3153 LogFlow(("authCall: RTThreadCreate %Rrc\n", rc));
3154
3155 if (RT_SUCCESS(rc))
3156 {
3157 rc = RTThreadWait(thread, RT_INDEFINITE_WAIT, NULL);
3158 LogFlow(("authCall: RTThreadWait %Rrc\n", rc));
3159 }
3160
3161 if (RT_SUCCESS(rc))
3162 {
3163 /* Only update the result if the thread finished without errors. */
3164 result = pCtx->result;
3165 }
3166 else
3167 {
3168 LogRel(("AUTH: unable to execute the auth thread %Rrc\n", rc));
3169 }
3170
3171 return result;
3172}
3173
3174AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
3175 const char *pszUser, const char *pszPassword, const char *pszDomain,
3176 uint32_t u32ClientId)
3177{
3178 AUTHUUID rawuuid;
3179
3180 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
3181
3182 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
3183 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
3184
3185 /*
3186 * Called only from VRDP input thread. So thread safety is not required.
3187 */
3188
3189 if (!mAuthLibrary)
3190 {
3191 /* Load the external authentication library. */
3192 Bstr authLibrary;
3193 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
3194
3195 Utf8Str filename = authLibrary;
3196
3197 LogRel(("AUTH: loading external authentication library '%ls'\n", authLibrary.raw()));
3198
3199 int rc;
3200 if (RTPathHavePath(filename.c_str()))
3201 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
3202 else
3203 {
3204 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
3205 if (RT_FAILURE(rc))
3206 {
3207 /* Backward compatibility with old default 'VRDPAuth' name.
3208 * Try to load new default 'VBoxAuth' instead.
3209 */
3210 if (filename == "VRDPAuth")
3211 {
3212 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library VBoxAuth\n"));
3213 rc = RTLdrLoadAppPriv("VBoxAuth", &mAuthLibrary);
3214 }
3215 }
3216 }
3217
3218 if (RT_FAILURE(rc))
3219 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
3220
3221 if (RT_SUCCESS(rc))
3222 {
3223 typedef struct AuthEntryInfoStruct
3224 {
3225 const char *pszName;
3226 void **ppvAddress;
3227
3228 } AuthEntryInfo;
3229 AuthEntryInfo entries[] =
3230 {
3231 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
3232 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
3233 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
3234 { NULL, NULL }
3235 };
3236
3237 /* Get the entry point. */
3238 AuthEntryInfo *pEntryInfo = &entries[0];
3239 while (pEntryInfo->pszName)
3240 {
3241 *pEntryInfo->ppvAddress = NULL;
3242
3243 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
3244 if (RT_SUCCESS(rc2))
3245 {
3246 /* Found an entry point. */
3247 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
3248 rc = VINF_SUCCESS;
3249 break;
3250 }
3251
3252 if (rc2 != VERR_SYMBOL_NOT_FOUND)
3253 {
3254 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
3255 }
3256 rc = rc2;
3257
3258 pEntryInfo++;
3259 }
3260 }
3261
3262 if (RT_FAILURE(rc))
3263 {
3264 mConsole->setError(E_FAIL,
3265 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
3266 filename.c_str(),
3267 rc);
3268
3269 mpfnAuthEntry = NULL;
3270 mpfnAuthEntry2 = NULL;
3271 mpfnAuthEntry3 = NULL;
3272
3273 if (mAuthLibrary)
3274 {
3275 RTLdrClose(mAuthLibrary);
3276 mAuthLibrary = 0;
3277 }
3278
3279 return AuthResultAccessDenied;
3280 }
3281 }
3282
3283 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
3284
3285 AuthCtx ctx;
3286 ctx.result = AuthResultAccessDenied; /* Denied by default. */
3287 ctx.pfnAuthEntry3 = mpfnAuthEntry3;
3288 ctx.pfnAuthEntry2 = mpfnAuthEntry2;
3289 ctx.pfnAuthEntry = mpfnAuthEntry;
3290 ctx.pszCaller = "vrde";
3291 ctx.pUuid = &rawuuid;
3292 ctx.guestJudgement = guestJudgement;
3293 ctx.pszUser = pszUser;
3294 ctx.pszPassword = pszPassword;
3295 ctx.pszDomain = pszDomain;
3296 ctx.fLogon = true;
3297 ctx.clientId = u32ClientId;
3298
3299 AuthResult result = authCall(&ctx);
3300
3301 switch (result)
3302 {
3303 case AuthResultAccessDenied:
3304 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
3305 break;
3306 case AuthResultAccessGranted:
3307 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
3308 break;
3309 case AuthResultDelegateToGuest:
3310 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
3311 break;
3312 default:
3313 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
3314 result = AuthResultAccessDenied;
3315 }
3316
3317 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
3318
3319 return result;
3320}
3321
3322void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
3323{
3324 AUTHUUID rawuuid;
3325
3326 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
3327
3328 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
3329 rawuuid, u32ClientId));
3330
3331 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
3332
3333 AuthCtx ctx;
3334 ctx.result = AuthResultAccessDenied; /* Not used. */
3335 ctx.pfnAuthEntry3 = mpfnAuthEntry3;
3336 ctx.pfnAuthEntry2 = mpfnAuthEntry2;
3337 ctx.pfnAuthEntry = NULL; /* Does not use disconnect notification. */
3338 ctx.pszCaller = "vrde";
3339 ctx.pUuid = &rawuuid;
3340 ctx.guestJudgement = AuthGuestNotAsked;
3341 ctx.pszUser = NULL;
3342 ctx.pszPassword = NULL;
3343 ctx.pszDomain = NULL;
3344 ctx.fLogon = false;
3345 ctx.clientId = u32ClientId;
3346
3347 authCall(&ctx);
3348}
3349
3350int ConsoleVRDPServer::lockConsoleVRDPServer(void)
3351{
3352 int rc = RTCritSectEnter(&mCritSect);
3353 AssertRC(rc);
3354 return rc;
3355}
3356
3357void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
3358{
3359 RTCritSectLeave(&mCritSect);
3360}
3361
3362DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
3363 uint32_t u32ClientId,
3364 uint32_t u32Function,
3365 uint32_t u32Format,
3366 const void *pvData,
3367 uint32_t cbData)
3368{
3369 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
3370 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
3371
3372 int rc = VINF_SUCCESS;
3373
3374 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
3375
3376 NOREF(u32ClientId);
3377
3378 switch (u32Function)
3379 {
3380 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
3381 {
3382 if (pServer->mpfnClipboardCallback)
3383 {
3384 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
3385 u32Format,
3386 (void *)pvData,
3387 cbData);
3388 }
3389 } break;
3390
3391 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
3392 {
3393 if (pServer->mpfnClipboardCallback)
3394 {
3395 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
3396 u32Format,
3397 (void *)pvData,
3398 cbData);
3399 }
3400 } break;
3401
3402 default:
3403 rc = VERR_NOT_SUPPORTED;
3404 }
3405
3406 return rc;
3407}
3408
3409DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
3410 uint32_t u32Function,
3411 void *pvParms,
3412 uint32_t cbParms)
3413{
3414 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
3415 pvExtension, u32Function, pvParms, cbParms));
3416
3417 int rc = VINF_SUCCESS;
3418
3419 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
3420
3421 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
3422
3423 switch (u32Function)
3424 {
3425 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
3426 {
3427 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
3428 } break;
3429
3430 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
3431 {
3432 /* The guest announces clipboard formats. This must be delivered to all clients. */
3433 if (mpEntryPoints && pServer->mhServer)
3434 {
3435 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3436 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
3437 pParms->u32Format,
3438 NULL,
3439 0,
3440 NULL);
3441 }
3442 } break;
3443
3444 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
3445 {
3446 /* The clipboard service expects that the pvData buffer will be filled
3447 * with clipboard data. The server returns the data from the client that
3448 * announced the requested format most recently.
3449 */
3450 if (mpEntryPoints && pServer->mhServer)
3451 {
3452 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3453 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
3454 pParms->u32Format,
3455 pParms->u.pvData,
3456 pParms->cbData,
3457 &pParms->cbData);
3458 }
3459 } break;
3460
3461 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
3462 {
3463 if (mpEntryPoints && pServer->mhServer)
3464 {
3465 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3466 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
3467 pParms->u32Format,
3468 pParms->u.pvData,
3469 pParms->cbData,
3470 NULL);
3471 }
3472 } break;
3473
3474 default:
3475 rc = VERR_NOT_SUPPORTED;
3476 }
3477
3478 return rc;
3479}
3480
3481void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
3482{
3483 int rc = lockConsoleVRDPServer();
3484
3485 if (RT_SUCCESS(rc))
3486 {
3487 if (mcClipboardRefs == 0)
3488 {
3489 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
3490
3491 if (RT_SUCCESS(rc))
3492 {
3493 mcClipboardRefs++;
3494 }
3495 }
3496
3497 unlockConsoleVRDPServer();
3498 }
3499}
3500
3501void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
3502{
3503 int rc = lockConsoleVRDPServer();
3504
3505 if (RT_SUCCESS(rc))
3506 {
3507 mcClipboardRefs--;
3508
3509 if (mcClipboardRefs == 0)
3510 {
3511 HGCMHostUnregisterServiceExtension(mhClipboard);
3512 }
3513
3514 unlockConsoleVRDPServer();
3515 }
3516}
3517
3518/* That is called on INPUT thread of the VRDP server.
3519 * The ConsoleVRDPServer keeps a list of created backend instances.
3520 */
3521void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
3522{
3523#ifdef VBOX_WITH_USB
3524 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
3525
3526 /* Create a new instance of the USB backend for the new client. */
3527 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
3528
3529 if (pRemoteUSBBackend)
3530 {
3531 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
3532
3533 /* Append the new instance in the list. */
3534 int rc = lockConsoleVRDPServer();
3535
3536 if (RT_SUCCESS(rc))
3537 {
3538 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
3539 if (mUSBBackends.pHead)
3540 {
3541 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
3542 }
3543 else
3544 {
3545 mUSBBackends.pTail = pRemoteUSBBackend;
3546 }
3547
3548 mUSBBackends.pHead = pRemoteUSBBackend;
3549
3550 unlockConsoleVRDPServer();
3551
3552 if (ppvIntercept)
3553 {
3554 *ppvIntercept = pRemoteUSBBackend;
3555 }
3556 }
3557
3558 if (RT_FAILURE(rc))
3559 {
3560 pRemoteUSBBackend->Release();
3561 }
3562 }
3563#endif /* VBOX_WITH_USB */
3564}
3565
3566void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
3567{
3568#ifdef VBOX_WITH_USB
3569 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
3570
3571 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3572
3573 /* Find the instance. */
3574 int rc = lockConsoleVRDPServer();
3575
3576 if (RT_SUCCESS(rc))
3577 {
3578 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3579
3580 if (pRemoteUSBBackend)
3581 {
3582 /* Notify that it will be deleted. */
3583 pRemoteUSBBackend->NotifyDelete();
3584 }
3585
3586 unlockConsoleVRDPServer();
3587 }
3588
3589 if (pRemoteUSBBackend)
3590 {
3591 /* Here the instance has been excluded from the list and can be dereferenced. */
3592 pRemoteUSBBackend->Release();
3593 }
3594#endif
3595}
3596
3597void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
3598{
3599#ifdef VBOX_WITH_USB
3600 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3601
3602 /* Find the instance. */
3603 int rc = lockConsoleVRDPServer();
3604
3605 if (RT_SUCCESS(rc))
3606 {
3607 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3608
3609 if (pRemoteUSBBackend)
3610 {
3611 /* Inform the backend instance that it is referenced by the Guid. */
3612 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
3613
3614 if (fAdded)
3615 {
3616 /* Reference the instance because its pointer is being taken. */
3617 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
3618 }
3619 else
3620 {
3621 pRemoteUSBBackend = NULL;
3622 }
3623 }
3624
3625 unlockConsoleVRDPServer();
3626 }
3627
3628 if (pRemoteUSBBackend)
3629 {
3630 return pRemoteUSBBackend->GetBackendCallbackPointer();
3631 }
3632
3633#endif
3634 return NULL;
3635}
3636
3637void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
3638{
3639#ifdef VBOX_WITH_USB
3640 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3641
3642 /* Find the instance. */
3643 int rc = lockConsoleVRDPServer();
3644
3645 if (RT_SUCCESS(rc))
3646 {
3647 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
3648
3649 if (pRemoteUSBBackend)
3650 {
3651 pRemoteUSBBackend->removeUUID(pGuid);
3652 }
3653
3654 unlockConsoleVRDPServer();
3655
3656 if (pRemoteUSBBackend)
3657 {
3658 pRemoteUSBBackend->Release();
3659 }
3660 }
3661#endif
3662}
3663
3664RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
3665{
3666 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
3667
3668 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
3669#ifdef VBOX_WITH_USB
3670
3671 int rc = lockConsoleVRDPServer();
3672
3673 if (RT_SUCCESS(rc))
3674 {
3675 if (pRemoteUSBBackend == NULL)
3676 {
3677 /* The first backend in the list is requested. */
3678 pNextRemoteUSBBackend = mUSBBackends.pHead;
3679 }
3680 else
3681 {
3682 /* Get pointer to the next backend. */
3683 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3684 }
3685
3686 if (pNextRemoteUSBBackend)
3687 {
3688 pNextRemoteUSBBackend->AddRef();
3689 }
3690
3691 unlockConsoleVRDPServer();
3692
3693 if (pRemoteUSBBackend)
3694 {
3695 pRemoteUSBBackend->Release();
3696 }
3697 }
3698#endif
3699
3700 return pNextRemoteUSBBackend;
3701}
3702
3703#ifdef VBOX_WITH_USB
3704/* Internal method. Called under the ConsoleVRDPServerLock. */
3705RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
3706{
3707 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3708
3709 while (pRemoteUSBBackend)
3710 {
3711 if (pRemoteUSBBackend->ClientId() == u32ClientId)
3712 {
3713 break;
3714 }
3715
3716 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3717 }
3718
3719 return pRemoteUSBBackend;
3720}
3721
3722/* Internal method. Called under the ConsoleVRDPServerLock. */
3723RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
3724{
3725 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3726
3727 while (pRemoteUSBBackend)
3728 {
3729 if (pRemoteUSBBackend->findUUID(pGuid))
3730 {
3731 break;
3732 }
3733
3734 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3735 }
3736
3737 return pRemoteUSBBackend;
3738}
3739#endif
3740
3741/* Internal method. Called by the backend destructor. */
3742void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
3743{
3744#ifdef VBOX_WITH_USB
3745 int rc = lockConsoleVRDPServer();
3746 AssertRC(rc);
3747
3748 /* Exclude the found instance from the list. */
3749 if (pRemoteUSBBackend->pNext)
3750 {
3751 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
3752 }
3753 else
3754 {
3755 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
3756 }
3757
3758 if (pRemoteUSBBackend->pPrev)
3759 {
3760 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
3761 }
3762 else
3763 {
3764 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3765 }
3766
3767 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
3768
3769 unlockConsoleVRDPServer();
3770#endif
3771}
3772
3773
3774void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
3775{
3776 if (mpEntryPoints && mhServer)
3777 {
3778 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
3779 }
3780}
3781
3782void ConsoleVRDPServer::SendResize(void)
3783{
3784 if (mpEntryPoints && mhServer)
3785 {
3786 ++mcInResize;
3787 mpEntryPoints->VRDEResize(mhServer);
3788 --mcInResize;
3789 }
3790}
3791
3792void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
3793{
3794 VRDEORDERHDR update;
3795 update.x = x;
3796 update.y = y;
3797 update.w = w;
3798 update.h = h;
3799 if (mpEntryPoints && mhServer)
3800 {
3801 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
3802 }
3803}
3804
3805void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
3806{
3807 if (mpEntryPoints && mhServer)
3808 {
3809 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
3810 }
3811}
3812
3813void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
3814{
3815 if (mpEntryPoints && mhServer)
3816 {
3817 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
3818 }
3819}
3820
3821void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
3822{
3823 if (mpEntryPoints && mhServer)
3824 {
3825 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
3826 }
3827}
3828
3829/* @todo rc not needed? */
3830int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
3831 void *pvContext,
3832 uint32_t cSamples,
3833 uint32_t iSampleHz,
3834 uint32_t cChannels,
3835 uint32_t cBits)
3836{
3837 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
3838 {
3839 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3840 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3841 {
3842 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
3843 mpEntryPoints->VRDEAudioInOpen (mhServer,
3844 pvContext,
3845 u32ClientId,
3846 audioFormat,
3847 cSamples);
3848 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
3849 * Currently not used because only one client is allowed to
3850 * do audio input and the client id is saved by the ConsoleVRDPServer.
3851 */
3852
3853 return VINF_SUCCESS;
3854 }
3855 }
3856 return VERR_NOT_SUPPORTED;
3857}
3858
3859void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
3860{
3861 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
3862 {
3863 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3864 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3865 {
3866 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
3867 }
3868 }
3869}
3870
3871
3872void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
3873{
3874 if (index == VRDE_QI_PORT)
3875 {
3876 uint32_t cbOut = sizeof(int32_t);
3877
3878 if (cbBuffer >= cbOut)
3879 {
3880 *pcbOut = cbOut;
3881 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
3882 }
3883 }
3884 else if (mpEntryPoints && mhServer)
3885 {
3886 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
3887 }
3888}
3889
3890/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
3891{
3892 int rc = VINF_SUCCESS;
3893
3894 if (mVRDPLibrary == NIL_RTLDRMOD)
3895 {
3896 RTERRINFOSTATIC ErrInfo;
3897 RTErrInfoInitStatic(&ErrInfo);
3898
3899 if (RTPathHavePath(pszLibraryName))
3900 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
3901 else
3902 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
3903 if (RT_SUCCESS(rc))
3904 {
3905 struct SymbolEntry
3906 {
3907 const char *name;
3908 void **ppfn;
3909 };
3910
3911 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
3912
3913 static const struct SymbolEntry s_aSymbols[] =
3914 {
3915 DEFSYMENTRY(VRDECreateServer)
3916 };
3917
3918 #undef DEFSYMENTRY
3919
3920 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
3921 {
3922 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
3923
3924 if (RT_FAILURE(rc))
3925 {
3926 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
3927 break;
3928 }
3929 }
3930 }
3931 else
3932 {
3933 if (RTErrInfoIsSet(&ErrInfo.Core))
3934 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
3935 else
3936 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
3937
3938 mVRDPLibrary = NIL_RTLDRMOD;
3939 }
3940 }
3941
3942 if (RT_FAILURE(rc))
3943 {
3944 if (mVRDPLibrary != NIL_RTLDRMOD)
3945 {
3946 RTLdrClose(mVRDPLibrary);
3947 mVRDPLibrary = NIL_RTLDRMOD;
3948 }
3949 }
3950
3951 return rc;
3952}
3953
3954/*
3955 * IVRDEServerInfo implementation.
3956 */
3957// constructor / destructor
3958/////////////////////////////////////////////////////////////////////////////
3959
3960VRDEServerInfo::VRDEServerInfo()
3961 : mParent(NULL)
3962{
3963}
3964
3965VRDEServerInfo::~VRDEServerInfo()
3966{
3967}
3968
3969
3970HRESULT VRDEServerInfo::FinalConstruct()
3971{
3972 return BaseFinalConstruct();
3973}
3974
3975void VRDEServerInfo::FinalRelease()
3976{
3977 uninit();
3978 BaseFinalRelease();
3979}
3980
3981// public methods only for internal purposes
3982/////////////////////////////////////////////////////////////////////////////
3983
3984/**
3985 * Initializes the guest object.
3986 */
3987HRESULT VRDEServerInfo::init(Console *aParent)
3988{
3989 LogFlowThisFunc(("aParent=%p\n", aParent));
3990
3991 ComAssertRet(aParent, E_INVALIDARG);
3992
3993 /* Enclose the state transition NotReady->InInit->Ready */
3994 AutoInitSpan autoInitSpan(this);
3995 AssertReturn(autoInitSpan.isOk(), E_FAIL);
3996
3997 unconst(mParent) = aParent;
3998
3999 /* Confirm a successful initialization */
4000 autoInitSpan.setSucceeded();
4001
4002 return S_OK;
4003}
4004
4005/**
4006 * Uninitializes the instance and sets the ready flag to FALSE.
4007 * Called either from FinalRelease() or by the parent when it gets destroyed.
4008 */
4009void VRDEServerInfo::uninit()
4010{
4011 LogFlowThisFunc(("\n"));
4012
4013 /* Enclose the state transition Ready->InUninit->NotReady */
4014 AutoUninitSpan autoUninitSpan(this);
4015 if (autoUninitSpan.uninitDone())
4016 return;
4017
4018 unconst(mParent) = NULL;
4019}
4020
4021// IVRDEServerInfo properties
4022/////////////////////////////////////////////////////////////////////////////
4023
4024#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
4025 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
4026 { \
4027 if (!a##_aName) \
4028 return E_POINTER; \
4029 \
4030 AutoCaller autoCaller(this); \
4031 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
4032 \
4033 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
4034 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
4035 \
4036 uint32_t value; \
4037 uint32_t cbOut = 0; \
4038 \
4039 mParent->consoleVRDPServer()->QueryInfo \
4040 (_aIndex, &value, sizeof(value), &cbOut); \
4041 \
4042 *a##_aName = cbOut? !!value: FALSE; \
4043 \
4044 return S_OK; \
4045 } \
4046 extern void IMPL_GETTER_BOOL_DUMMY(void)
4047
4048#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
4049 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
4050 { \
4051 if (!a##_aName) \
4052 return E_POINTER; \
4053 \
4054 AutoCaller autoCaller(this); \
4055 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
4056 \
4057 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
4058 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
4059 \
4060 _aType value; \
4061 uint32_t cbOut = 0; \
4062 \
4063 mParent->consoleVRDPServer()->QueryInfo \
4064 (_aIndex, &value, sizeof(value), &cbOut); \
4065 \
4066 if (_aValueMask) value &= (_aValueMask); \
4067 *a##_aName = cbOut? value: 0; \
4068 \
4069 return S_OK; \
4070 } \
4071 extern void IMPL_GETTER_SCALAR_DUMMY(void)
4072
4073#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
4074 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
4075 { \
4076 if (!a##_aName) \
4077 return E_POINTER; \
4078 \
4079 AutoCaller autoCaller(this); \
4080 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
4081 \
4082 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
4083 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
4084 \
4085 uint32_t cbOut = 0; \
4086 \
4087 mParent->consoleVRDPServer()->QueryInfo \
4088 (_aIndex, NULL, 0, &cbOut); \
4089 \
4090 if (cbOut == 0) \
4091 { \
4092 Bstr str(""); \
4093 str.cloneTo(a##_aName); \
4094 return S_OK; \
4095 } \
4096 \
4097 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
4098 \
4099 if (!pchBuffer) \
4100 { \
4101 Log(("VRDEServerInfo::" \
4102 #_aName \
4103 ": Failed to allocate memory %d bytes\n", cbOut)); \
4104 return E_OUTOFMEMORY; \
4105 } \
4106 \
4107 mParent->consoleVRDPServer()->QueryInfo \
4108 (_aIndex, pchBuffer, cbOut, &cbOut); \
4109 \
4110 Bstr str(pchBuffer); \
4111 \
4112 str.cloneTo(a##_aName); \
4113 \
4114 RTMemTmpFree(pchBuffer); \
4115 \
4116 return S_OK; \
4117 } \
4118 extern void IMPL_GETTER_BSTR_DUMMY(void)
4119
4120IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
4121IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
4122IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
4123IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
4124IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
4125IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
4126IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
4127IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
4128IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
4129IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
4130IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
4131IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
4132IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
4133IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
4134IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
4135
4136#undef IMPL_GETTER_BSTR
4137#undef IMPL_GETTER_SCALAR
4138#undef IMPL_GETTER_BOOL
4139/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette