VirtualBox

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

Last change on this file since 36920 was 36856, checked in by vboxsync, 14 years ago

warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 97.5 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 36856 2011-04-27 14:39:42Z vboxsync $ */
2/** @file
3 * VBox Console VRDP Helper class
4 */
5
6/*
7 * Copyright (C) 2006-2010 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
29#include "Global.h"
30#include "AutoCaller.h"
31#include "Logging.h"
32
33#include <iprt/asm.h>
34#include <iprt/alloca.h>
35#include <iprt/ldr.h>
36#include <iprt/param.h>
37#include <iprt/path.h>
38#include <iprt/cpp/utils.h>
39
40#include <VBox/err.h>
41#include <VBox/RemoteDesktop/VRDEOrders.h>
42#include <VBox/com/listeners.h>
43#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
44
45class VRDPConsoleListener
46{
47public:
48 VRDPConsoleListener()
49 {
50 }
51
52 HRESULT init(ConsoleVRDPServer *server)
53 {
54 m_server = server;
55 return S_OK;
56 }
57
58 void uninit()
59 {
60 }
61
62 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
63 {
64 switch (aType)
65 {
66 case VBoxEventType_OnMousePointerShapeChanged:
67 {
68 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
69 Assert(mpscev);
70 BOOL visible, alpha;
71 ULONG xHot, yHot, width, height;
72 com::SafeArray <BYTE> shape;
73
74 mpscev->COMGETTER(Visible)(&visible);
75 mpscev->COMGETTER(Alpha)(&alpha);
76 mpscev->COMGETTER(Xhot)(&xHot);
77 mpscev->COMGETTER(Yhot)(&yHot);
78 mpscev->COMGETTER(Width)(&width);
79 mpscev->COMGETTER(Height)(&height);
80 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
81
82 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
83 break;
84 }
85 case VBoxEventType_OnMouseCapabilityChanged:
86 {
87 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
88 Assert(mccev);
89 if (m_server)
90 {
91 BOOL fAbsoluteMouse;
92 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
93 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
94 }
95 break;
96 }
97 case VBoxEventType_OnKeyboardLedsChanged:
98 {
99 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
100 Assert(klcev);
101
102 if (m_server)
103 {
104 BOOL fNumLock, fCapsLock, fScrollLock;
105 klcev->COMGETTER(NumLock)(&fNumLock);
106 klcev->COMGETTER(CapsLock)(&fCapsLock);
107 klcev->COMGETTER(ScrollLock)(&fScrollLock);
108 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
109 }
110 break;
111 }
112
113 default:
114 AssertFailed();
115 }
116
117 return S_OK;
118 }
119
120private:
121 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
122 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
123 ConsoleVRDPServer *m_server;
124};
125
126typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
127
128VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
129
130#ifdef DEBUG_sunlover
131#define LOGDUMPPTR Log
132void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
133{
134 unsigned i;
135
136 const uint8_t *pu8And = pu8Shape;
137
138 for (i = 0; i < height; i++)
139 {
140 unsigned j;
141 LOGDUMPPTR(("%p: ", pu8And));
142 for (j = 0; j < (width + 7) / 8; j++)
143 {
144 unsigned k;
145 for (k = 0; k < 8; k++)
146 {
147 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
148 }
149
150 pu8And++;
151 }
152 LOGDUMPPTR(("\n"));
153 }
154
155 if (fXorMaskRGB32)
156 {
157 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
158
159 for (i = 0; i < height; i++)
160 {
161 unsigned j;
162 LOGDUMPPTR(("%p: ", pu32Xor));
163 for (j = 0; j < width; j++)
164 {
165 LOGDUMPPTR(("%08X", *pu32Xor++));
166 }
167 LOGDUMPPTR(("\n"));
168 }
169 }
170 else
171 {
172 /* RDP 24 bit RGB mask. */
173 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
174 for (i = 0; i < height; i++)
175 {
176 unsigned j;
177 LOGDUMPPTR(("%p: ", pu8Xor));
178 for (j = 0; j < width; j++)
179 {
180 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
181 pu8Xor += 3;
182 }
183 LOGDUMPPTR(("\n"));
184 }
185 }
186}
187#else
188#define dumpPointer(a, b, c, d) do {} while (0)
189#endif /* DEBUG_sunlover */
190
191static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
192{
193 /*
194 * Find the top border of the AND mask. First assign to special value.
195 */
196 uint32_t ySkipAnd = ~0;
197
198 const uint8_t *pu8And = pu8AndMask;
199 const uint32_t cbAndRow = (width + 7) / 8;
200 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
201
202 Assert(cbAndRow > 0);
203
204 unsigned y;
205 unsigned x;
206
207 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
208 {
209 /* For each complete byte in the row. */
210 for (x = 0; x < cbAndRow - 1; x++)
211 {
212 if (pu8And[x] != 0xFF)
213 {
214 ySkipAnd = y;
215 break;
216 }
217 }
218
219 if (ySkipAnd == ~(uint32_t)0)
220 {
221 /* Last byte. */
222 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
223 {
224 ySkipAnd = y;
225 }
226 }
227 }
228
229 if (ySkipAnd == ~(uint32_t)0)
230 {
231 ySkipAnd = 0;
232 }
233
234 /*
235 * Find the left border of the AND mask.
236 */
237 uint32_t xSkipAnd = ~0;
238
239 /* For all bit columns. */
240 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
241 {
242 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
243 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
244
245 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
246 {
247 if ((*pu8And & mask) == 0)
248 {
249 xSkipAnd = x;
250 break;
251 }
252 }
253 }
254
255 if (xSkipAnd == ~(uint32_t)0)
256 {
257 xSkipAnd = 0;
258 }
259
260 /*
261 * Find the XOR mask top border.
262 */
263 uint32_t ySkipXor = ~0;
264
265 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
266
267 uint32_t *pu32Xor = pu32XorStart;
268
269 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
270 {
271 for (x = 0; x < width; x++)
272 {
273 if (pu32Xor[x] != 0)
274 {
275 ySkipXor = y;
276 break;
277 }
278 }
279 }
280
281 if (ySkipXor == ~(uint32_t)0)
282 {
283 ySkipXor = 0;
284 }
285
286 /*
287 * Find the left border of the XOR mask.
288 */
289 uint32_t xSkipXor = ~(uint32_t)0;
290
291 /* For all columns. */
292 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
293 {
294 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
295
296 for (y = ySkipXor; y < height; y++, pu32Xor += width)
297 {
298 if (*pu32Xor != 0)
299 {
300 xSkipXor = x;
301 break;
302 }
303 }
304 }
305
306 if (xSkipXor == ~(uint32_t)0)
307 {
308 xSkipXor = 0;
309 }
310
311 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
312 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
313}
314
315/* Generate an AND mask for alpha pointers here, because
316 * guest driver does not do that correctly for Vista pointers.
317 * Similar fix, changing the alpha threshold, could be applied
318 * for the guest driver, but then additions reinstall would be
319 * necessary, which we try to avoid.
320 */
321static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
322{
323 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
324
325 int y;
326 for (y = 0; y < h; y++)
327 {
328 uint8_t bitmask = 0x80;
329
330 int x;
331 for (x = 0; x < w; x++, bitmask >>= 1)
332 {
333 if (bitmask == 0)
334 {
335 bitmask = 0x80;
336 }
337
338 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
339 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
340 {
341 pu8DstAndMask[x / 8] &= ~bitmask;
342 }
343 }
344
345 /* Point to next source and dest scans. */
346 pu8SrcAlpha += w * 4;
347 pu8DstAndMask += (w + 7) / 8;
348 }
349}
350
351STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
352 BOOL alpha,
353 ULONG xHot,
354 ULONG yHot,
355 ULONG width,
356 ULONG height,
357 ComSafeArrayIn(BYTE,inShape))
358{
359 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
360
361 if (m_server)
362 {
363 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
364 if (aShape.size() == 0)
365 {
366 if (!visible)
367 {
368 m_server->MousePointerHide();
369 }
370 }
371 else if (width != 0 && height != 0)
372 {
373 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
374 * 'shape' AND mask followed by XOR mask.
375 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
376 *
377 * We convert this to RDP color format which consist of
378 * one bpp AND mask and 24 BPP (BGR) color XOR image.
379 *
380 * RDP clients expect 8 aligned width and height of
381 * pointer (preferably 32x32).
382 *
383 * They even contain bugs which do not appear for
384 * 32x32 pointers but would appear for a 41x32 one.
385 *
386 * So set pointer size to 32x32. This can be done safely
387 * because most pointers are 32x32.
388 */
389 uint8_t* shape = aShape.raw();
390
391 dumpPointer(shape, width, height, true);
392
393 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
394
395 uint8_t *pu8AndMask = shape;
396 uint8_t *pu8XorMask = shape + cbDstAndMask;
397
398 if (alpha)
399 {
400 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
401
402 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
403 }
404
405 /* Windows guest alpha pointers are wider than 32 pixels.
406 * Try to find out the top-left border of the pointer and
407 * then copy only meaningful bits. All complete top rows
408 * and all complete left columns where (AND == 1 && XOR == 0)
409 * are skipped. Hot spot is adjusted.
410 */
411 uint32_t ySkip = 0; /* How many rows to skip at the top. */
412 uint32_t xSkip = 0; /* How many columns to skip at the left. */
413
414 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
415
416 /* Must not skip the hot spot. */
417 xSkip = RT_MIN(xSkip, xHot);
418 ySkip = RT_MIN(ySkip, yHot);
419
420 /*
421 * Compute size and allocate memory for the pointer.
422 */
423 const uint32_t dstwidth = 32;
424 const uint32_t dstheight = 32;
425
426 VRDECOLORPOINTER *pointer = NULL;
427
428 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
429
430 uint32_t rdpmaskwidth = dstmaskwidth;
431 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
432
433 uint32_t rdpdatawidth = dstwidth * 3;
434 uint32_t rdpdatalen = dstheight * rdpdatawidth;
435
436 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
437
438 if (pointer)
439 {
440 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
441 uint8_t *dataarray = maskarray + rdpmasklen;
442
443 memset(maskarray, 0xFF, rdpmasklen);
444 memset(dataarray, 0x00, rdpdatalen);
445
446 uint32_t srcmaskwidth = (width + 7) / 8;
447 uint32_t srcdatawidth = width * 4;
448
449 /* Copy AND mask. */
450 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
451 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
452
453 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
454 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
455
456 unsigned x, y;
457
458 for (y = 0; y < minheight; y++)
459 {
460 for (x = 0; x < minwidth; x++)
461 {
462 uint32_t byteIndex = (x + xSkip) / 8;
463 uint32_t bitIndex = (x + xSkip) % 8;
464
465 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
466
467 if (!bit)
468 {
469 byteIndex = x / 8;
470 bitIndex = x % 8;
471
472 dst[byteIndex] &= ~(1 << (7 - bitIndex));
473 }
474 }
475
476 src += srcmaskwidth;
477 dst -= rdpmaskwidth;
478 }
479
480 /* Point src to XOR mask */
481 src = pu8XorMask + ySkip * srcdatawidth;
482 dst = dataarray + (dstheight - 1) * rdpdatawidth;
483
484 for (y = 0; y < minheight ; y++)
485 {
486 for (x = 0; x < minwidth; x++)
487 {
488 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
489 }
490
491 src += srcdatawidth;
492 dst -= rdpdatawidth;
493 }
494
495 pointer->u16HotX = (uint16_t)(xHot - xSkip);
496 pointer->u16HotY = (uint16_t)(yHot - ySkip);
497
498 pointer->u16Width = (uint16_t)dstwidth;
499 pointer->u16Height = (uint16_t)dstheight;
500
501 pointer->u16MaskLen = (uint16_t)rdpmasklen;
502 pointer->u16DataLen = (uint16_t)rdpdatalen;
503
504 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
505
506 m_server->MousePointerUpdate(pointer);
507
508 RTMemTmpFree(pointer);
509 }
510 }
511 }
512
513 return S_OK;
514}
515
516
517// ConsoleVRDPServer
518////////////////////////////////////////////////////////////////////////////////
519
520RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
521
522PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
523
524VRDEENTRYPOINTS_4 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
525VRDEENTRYPOINTS_4 *ConsoleVRDPServer::mpEntryPoints = NULL;
526
527VRDECALLBACKS_4 ConsoleVRDPServer::mCallbacks =
528{
529 { VRDE_INTERFACE_VERSION_4, sizeof(VRDECALLBACKS_4) },
530 ConsoleVRDPServer::VRDPCallbackQueryProperty,
531 ConsoleVRDPServer::VRDPCallbackClientLogon,
532 ConsoleVRDPServer::VRDPCallbackClientConnect,
533 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
534 ConsoleVRDPServer::VRDPCallbackIntercept,
535 ConsoleVRDPServer::VRDPCallbackUSB,
536 ConsoleVRDPServer::VRDPCallbackClipboard,
537 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
538 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
539 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
540 ConsoleVRDPServer::VRDPCallbackInput,
541 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
542 ConsoleVRDPServer::VRDECallbackAudioIn
543};
544
545DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
546{
547 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
548
549 int rc = VERR_NOT_SUPPORTED;
550
551 switch (index)
552 {
553 case VRDE_QP_NETWORK_PORT:
554 {
555 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
556 ULONG port = 0;
557
558 if (cbBuffer >= sizeof(uint32_t))
559 {
560 *(uint32_t *)pvBuffer = (uint32_t)port;
561 rc = VINF_SUCCESS;
562 }
563 else
564 {
565 rc = VINF_BUFFER_OVERFLOW;
566 }
567
568 *pcbOut = sizeof(uint32_t);
569 } break;
570
571 case VRDE_QP_NETWORK_ADDRESS:
572 {
573 com::Bstr bstr;
574 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
575
576 /* The server expects UTF8. */
577 com::Utf8Str address = bstr;
578
579 size_t cbAddress = address.length() + 1;
580
581 if (cbAddress >= 0x10000)
582 {
583 /* More than 64K seems to be an invalid address. */
584 rc = VERR_TOO_MUCH_DATA;
585 break;
586 }
587
588 if ((size_t)cbBuffer >= cbAddress)
589 {
590 memcpy(pvBuffer, address.c_str(), cbAddress);
591 rc = VINF_SUCCESS;
592 }
593 else
594 {
595 rc = VINF_BUFFER_OVERFLOW;
596 }
597
598 *pcbOut = (uint32_t)cbAddress;
599 } break;
600
601 case VRDE_QP_NUMBER_MONITORS:
602 {
603 ULONG cMonitors = 1;
604
605 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
606
607 if (cbBuffer >= sizeof(uint32_t))
608 {
609 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
610 rc = VINF_SUCCESS;
611 }
612 else
613 {
614 rc = VINF_BUFFER_OVERFLOW;
615 }
616
617 *pcbOut = sizeof(uint32_t);
618 } break;
619
620 case VRDE_QP_NETWORK_PORT_RANGE:
621 {
622 com::Bstr bstr;
623 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
624
625 if (hrc != S_OK)
626 {
627 bstr = "";
628 }
629
630 if (bstr == "0")
631 {
632 bstr = "3389";
633 }
634
635 /* The server expects UTF8. */
636 com::Utf8Str portRange = bstr;
637
638 size_t cbPortRange = portRange.length() + 1;
639
640 if (cbPortRange >= 0x10000)
641 {
642 /* More than 64K seems to be an invalid port range string. */
643 rc = VERR_TOO_MUCH_DATA;
644 break;
645 }
646
647 if ((size_t)cbBuffer >= cbPortRange)
648 {
649 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
650 rc = VINF_SUCCESS;
651 }
652 else
653 {
654 rc = VINF_BUFFER_OVERFLOW;
655 }
656
657 *pcbOut = (uint32_t)cbPortRange;
658 } break;
659
660#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
661 case VRDE_QP_VIDEO_CHANNEL:
662 {
663 com::Bstr bstr;
664 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(), bstr.asOutParam());
665
666 if (hrc != S_OK)
667 {
668 bstr = "";
669 }
670
671 com::Utf8Str value = bstr;
672
673 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
674 || RTStrICmp(value.c_str(), "1") == 0;
675
676 if (cbBuffer >= sizeof(uint32_t))
677 {
678 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
679 rc = VINF_SUCCESS;
680 }
681 else
682 {
683 rc = VINF_BUFFER_OVERFLOW;
684 }
685
686 *pcbOut = sizeof(uint32_t);
687 } break;
688
689 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
690 {
691 com::Bstr bstr;
692 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(), bstr.asOutParam());
693
694 if (hrc != S_OK)
695 {
696 bstr = "";
697 }
698
699 com::Utf8Str value = bstr;
700
701 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
702
703 if (cbBuffer >= sizeof(uint32_t))
704 {
705 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
706 rc = VINF_SUCCESS;
707 }
708 else
709 {
710 rc = VINF_BUFFER_OVERFLOW;
711 }
712
713 *pcbOut = sizeof(uint32_t);
714 } break;
715
716 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
717 {
718 ULONG ulSunFlsh = 1;
719
720 com::Bstr bstr;
721 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
722 bstr.asOutParam());
723 if (hrc == S_OK && !bstr.isEmpty())
724 {
725 com::Utf8Str sunFlsh = bstr;
726 if (!sunFlsh.isEmpty())
727 {
728 ulSunFlsh = sunFlsh.toUInt32();
729 }
730 }
731
732 if (cbBuffer >= sizeof(uint32_t))
733 {
734 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
735 rc = VINF_SUCCESS;
736 }
737 else
738 {
739 rc = VINF_BUFFER_OVERFLOW;
740 }
741
742 *pcbOut = sizeof(uint32_t);
743 } break;
744#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
745
746 case VRDE_QP_FEATURE:
747 {
748 if (cbBuffer < sizeof(VRDEFEATURE))
749 {
750 rc = VERR_INVALID_PARAMETER;
751 break;
752 }
753
754 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
755
756 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
757
758 size_t cchInfo = 0;
759 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
760
761 if (RT_FAILURE(rc))
762 {
763 rc = VERR_INVALID_PARAMETER;
764 break;
765 }
766
767 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
768
769 com::Bstr bstrValue;
770
771 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
772 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
773 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
774 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
775 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
776 )
777 {
778 /* @todo these features should be per client. */
779 NOREF(pFeature->u32ClientId);
780
781 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
782 com::Utf8Str extraData("VRDE/Feature/");
783 extraData += pFeature->achInfo;
784
785 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
786 bstrValue.asOutParam());
787 if (FAILED(hrc) || bstrValue.isEmpty())
788 {
789 /* Also try the old "VRDP/Feature/NAME" */
790 extraData = "VRDP/Feature/";
791 extraData += pFeature->achInfo;
792
793 hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
794 bstrValue.asOutParam());
795 if (FAILED(hrc))
796 {
797 rc = VERR_NOT_SUPPORTED;
798 }
799 }
800 }
801 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
802 {
803 /* Generic properties. */
804 const char *pszPropertyName = &pFeature->achInfo[9];
805 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(), bstrValue.asOutParam());
806 if (FAILED(hrc))
807 {
808 rc = VERR_NOT_SUPPORTED;
809 }
810 }
811 else
812 {
813 rc = VERR_NOT_SUPPORTED;
814 }
815
816 /* Copy the value string to the callers buffer. */
817 if (rc == VINF_SUCCESS)
818 {
819 com::Utf8Str value = bstrValue;
820
821 size_t cb = value.length() + 1;
822
823 if ((size_t)cbInfo >= cb)
824 {
825 memcpy(pFeature->achInfo, value.c_str(), cb);
826 }
827 else
828 {
829 rc = VINF_BUFFER_OVERFLOW;
830 }
831
832 *pcbOut = (uint32_t)cb;
833 }
834 } break;
835
836 case VRDE_SP_NETWORK_BIND_PORT:
837 {
838 if (cbBuffer != sizeof(uint32_t))
839 {
840 rc = VERR_INVALID_PARAMETER;
841 break;
842 }
843
844 ULONG port = *(uint32_t *)pvBuffer;
845
846 server->mVRDPBindPort = port;
847
848 rc = VINF_SUCCESS;
849
850 if (pcbOut)
851 {
852 *pcbOut = sizeof(uint32_t);
853 }
854
855 server->mConsole->onVRDEServerInfoChange();
856 } break;
857
858 default:
859 break;
860 }
861
862 return rc;
863}
864
865DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
866{
867 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
868
869 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
870}
871
872DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
873{
874 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
875
876 server->mConsole->VRDPClientConnect(u32ClientId);
877}
878
879DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
880{
881 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
882
883 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
884
885 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
886 {
887 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
888 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
889
890 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
891 if (pPort)
892 {
893 pPort->pfnAudioInputIntercept(pPort, false);
894 }
895 else
896 {
897 AssertFailed();
898 }
899 }
900}
901
902DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
903{
904 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
905
906 LogFlowFunc(("%x\n", fu32Intercept));
907
908 int rc = VERR_NOT_SUPPORTED;
909
910 switch (fu32Intercept)
911 {
912 case VRDE_CLIENT_INTERCEPT_AUDIO:
913 {
914 server->mConsole->VRDPInterceptAudio(u32ClientId);
915 if (ppvIntercept)
916 {
917 *ppvIntercept = server;
918 }
919 rc = VINF_SUCCESS;
920 } break;
921
922 case VRDE_CLIENT_INTERCEPT_USB:
923 {
924 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
925 rc = VINF_SUCCESS;
926 } break;
927
928 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
929 {
930 server->mConsole->VRDPInterceptClipboard(u32ClientId);
931 if (ppvIntercept)
932 {
933 *ppvIntercept = server;
934 }
935 rc = VINF_SUCCESS;
936 } break;
937
938 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
939 {
940 /* This request is processed internally by the ConsoleVRDPServer.
941 * Only one client is allowed to intercept audio input.
942 */
943 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
944 {
945 Log(("AUDIOIN: connected client %u\n", u32ClientId));
946
947 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
948 if (pPort)
949 {
950 pPort->pfnAudioInputIntercept(pPort, true);
951 if (ppvIntercept)
952 {
953 *ppvIntercept = server;
954 }
955 }
956 else
957 {
958 AssertFailed();
959 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
960 rc = VERR_NOT_SUPPORTED;
961 }
962 }
963 else
964 {
965 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
966 rc = VERR_NOT_SUPPORTED;
967 }
968 } break;
969
970 default:
971 break;
972 }
973
974 return rc;
975}
976
977DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
978{
979#ifdef VBOX_WITH_USB
980 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
981#else
982 return VERR_NOT_SUPPORTED;
983#endif
984}
985
986DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
987{
988 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
989}
990
991DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
992{
993 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
994
995 bool fAvailable = false;
996
997 IFramebuffer *pfb = NULL;
998 LONG xOrigin = 0;
999 LONG yOrigin = 0;
1000
1001 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1002
1003 if (pfb)
1004 {
1005 pfb->Lock ();
1006
1007 /* Query framebuffer parameters. */
1008 ULONG lineSize = 0;
1009 pfb->COMGETTER(BytesPerLine)(&lineSize);
1010
1011 ULONG bitsPerPixel = 0;
1012 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1013
1014 BYTE *address = NULL;
1015 pfb->COMGETTER(Address)(&address);
1016
1017 ULONG height = 0;
1018 pfb->COMGETTER(Height)(&height);
1019
1020 ULONG width = 0;
1021 pfb->COMGETTER(Width)(&width);
1022
1023 /* Now fill the information as requested by the caller. */
1024 pInfo->pu8Bits = address;
1025 pInfo->xOrigin = xOrigin;
1026 pInfo->yOrigin = yOrigin;
1027 pInfo->cWidth = width;
1028 pInfo->cHeight = height;
1029 pInfo->cBitsPerPixel = bitsPerPixel;
1030 pInfo->cbLine = lineSize;
1031
1032 pfb->Unlock();
1033
1034 fAvailable = true;
1035 }
1036
1037 if (server->maFramebuffers[uScreenId])
1038 {
1039 server->maFramebuffers[uScreenId]->Release();
1040 }
1041 server->maFramebuffers[uScreenId] = pfb;
1042
1043 return fAvailable;
1044}
1045
1046DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1047{
1048 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1049
1050 if (server->maFramebuffers[uScreenId])
1051 {
1052 server->maFramebuffers[uScreenId]->Lock();
1053 }
1054}
1055
1056DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1057{
1058 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1059
1060 if (server->maFramebuffers[uScreenId])
1061 {
1062 server->maFramebuffers[uScreenId]->Unlock();
1063 }
1064}
1065
1066static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1067{
1068 if ( pInputSynch->cGuestNumLockAdaptions
1069 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1070 {
1071 pInputSynch->cGuestNumLockAdaptions--;
1072 pKeyboard->PutScancode(0x45);
1073 pKeyboard->PutScancode(0x45 | 0x80);
1074 }
1075 if ( pInputSynch->cGuestCapsLockAdaptions
1076 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1077 {
1078 pInputSynch->cGuestCapsLockAdaptions--;
1079 pKeyboard->PutScancode(0x3a);
1080 pKeyboard->PutScancode(0x3a | 0x80);
1081 }
1082}
1083
1084DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1085{
1086 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1087 Console *pConsole = server->mConsole;
1088
1089 switch (type)
1090 {
1091 case VRDE_INPUT_SCANCODE:
1092 {
1093 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1094 {
1095 IKeyboard *pKeyboard = pConsole->getKeyboard();
1096
1097 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1098
1099 /* Track lock keys. */
1100 if (pInputScancode->uScancode == 0x45)
1101 {
1102 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1103 }
1104 else if (pInputScancode->uScancode == 0x3a)
1105 {
1106 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1107 }
1108 else if (pInputScancode->uScancode == 0x46)
1109 {
1110 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1111 }
1112 else if ((pInputScancode->uScancode & 0x80) == 0)
1113 {
1114 /* Key pressed. */
1115 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1116 }
1117
1118 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1119 }
1120 } break;
1121
1122 case VRDE_INPUT_POINT:
1123 {
1124 if (cbInput == sizeof(VRDEINPUTPOINT))
1125 {
1126 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1127
1128 int mouseButtons = 0;
1129 int iWheel = 0;
1130
1131 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1132 {
1133 mouseButtons |= MouseButtonState_LeftButton;
1134 }
1135 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1136 {
1137 mouseButtons |= MouseButtonState_RightButton;
1138 }
1139 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1140 {
1141 mouseButtons |= MouseButtonState_MiddleButton;
1142 }
1143 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1144 {
1145 mouseButtons |= MouseButtonState_WheelUp;
1146 iWheel = -1;
1147 }
1148 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1149 {
1150 mouseButtons |= MouseButtonState_WheelDown;
1151 iWheel = 1;
1152 }
1153
1154 if (server->m_fGuestWantsAbsolute)
1155 {
1156 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1157 } else
1158 {
1159 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1160 pInputPoint->y - server->m_mousey,
1161 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1162 server->m_mousex = pInputPoint->x;
1163 server->m_mousey = pInputPoint->y;
1164 }
1165 }
1166 } break;
1167
1168 case VRDE_INPUT_CAD:
1169 {
1170 pConsole->getKeyboard()->PutCAD();
1171 } break;
1172
1173 case VRDE_INPUT_RESET:
1174 {
1175 pConsole->Reset();
1176 } break;
1177
1178 case VRDE_INPUT_SYNCH:
1179 {
1180 if (cbInput == sizeof(VRDEINPUTSYNCH))
1181 {
1182 IKeyboard *pKeyboard = pConsole->getKeyboard();
1183
1184 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1185
1186 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1187 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1188 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1189
1190 /* The client initiated synchronization. Always make the guest to reflect the client state.
1191 * Than means, when the guest changes the state itself, it is forced to return to the client
1192 * state.
1193 */
1194 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1195 {
1196 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1197 }
1198
1199 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1200 {
1201 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1202 }
1203
1204 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1205 }
1206 } break;
1207
1208 default:
1209 break;
1210 }
1211}
1212
1213DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1214{
1215 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1216
1217 server->mConsole->getDisplay()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1218}
1219
1220DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1221 void *pvCtx,
1222 uint32_t u32ClientId,
1223 uint32_t u32Event,
1224 const void *pvData,
1225 uint32_t cbData)
1226{
1227 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1228
1229 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1230
1231 switch (u32Event)
1232 {
1233 case VRDE_AUDIOIN_BEGIN:
1234 {
1235 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1236
1237 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1238 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1239 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1240 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1241 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1242 );
1243 } break;
1244
1245 case VRDE_AUDIOIN_DATA:
1246 {
1247 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1248 } break;
1249
1250 case VRDE_AUDIOIN_END:
1251 {
1252 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1253 } break;
1254
1255 default:
1256 return;
1257 }
1258}
1259
1260
1261ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1262{
1263 mConsole = console;
1264
1265 int rc = RTCritSectInit(&mCritSect);
1266 AssertRC(rc);
1267
1268 mcClipboardRefs = 0;
1269 mpfnClipboardCallback = NULL;
1270
1271#ifdef VBOX_WITH_USB
1272 mUSBBackends.pHead = NULL;
1273 mUSBBackends.pTail = NULL;
1274
1275 mUSBBackends.thread = NIL_RTTHREAD;
1276 mUSBBackends.fThreadRunning = false;
1277 mUSBBackends.event = 0;
1278#endif
1279
1280 mhServer = 0;
1281 mServerInterfaceVersion = 0;
1282
1283 m_fGuestWantsAbsolute = false;
1284 m_mousex = 0;
1285 m_mousey = 0;
1286
1287 m_InputSynch.cGuestNumLockAdaptions = 2;
1288 m_InputSynch.cGuestCapsLockAdaptions = 2;
1289
1290 m_InputSynch.fGuestNumLock = false;
1291 m_InputSynch.fGuestCapsLock = false;
1292 m_InputSynch.fGuestScrollLock = false;
1293
1294 m_InputSynch.fClientNumLock = false;
1295 m_InputSynch.fClientCapsLock = false;
1296 m_InputSynch.fClientScrollLock = false;
1297
1298 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1299
1300 {
1301 ComPtr<IEventSource> es;
1302 console->COMGETTER(EventSource)(es.asOutParam());
1303 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1304 aConsoleListener.createObject();
1305 aConsoleListener->init(new VRDPConsoleListener(), this);
1306 mConsoleListener = aConsoleListener;
1307 com::SafeArray <VBoxEventType_T> eventTypes;
1308 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1309 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1310 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1311 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1312 }
1313
1314 mVRDPBindPort = -1;
1315
1316 mAuthLibrary = 0;
1317
1318 mu32AudioInputClientId = 0;
1319
1320 /*
1321 * Optional interfaces.
1322 */
1323 m_fInterfaceImage = false;
1324 memset(&m_interfaceImage, 0, sizeof (m_interfaceImage));
1325 memset(&m_interfaceCallbacksImage, 0, sizeof (m_interfaceCallbacksImage));
1326}
1327
1328ConsoleVRDPServer::~ConsoleVRDPServer()
1329{
1330 Stop();
1331
1332 if (mConsoleListener)
1333 {
1334 ComPtr<IEventSource> es;
1335 mConsole->COMGETTER(EventSource)(es.asOutParam());
1336 es->UnregisterListener(mConsoleListener);
1337 mConsoleListener.setNull();
1338 }
1339
1340 unsigned i;
1341 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1342 {
1343 if (maFramebuffers[i])
1344 {
1345 maFramebuffers[i]->Release();
1346 maFramebuffers[i] = NULL;
1347 }
1348 }
1349
1350 if (RTCritSectIsInitialized(&mCritSect))
1351 {
1352 RTCritSectDelete(&mCritSect);
1353 memset(&mCritSect, 0, sizeof(mCritSect));
1354 }
1355}
1356
1357int ConsoleVRDPServer::Launch(void)
1358{
1359 LogFlowThisFunc(("\n"));
1360
1361 IVRDEServer *server = mConsole->getVRDEServer();
1362 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1363
1364 /*
1365 * Check if VRDE is enabled.
1366 */
1367 BOOL fEnabled;
1368 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1369 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1370 if (!fEnabled)
1371 return VINF_SUCCESS;
1372
1373 /*
1374 * Check that a VRDE extension pack name is set and resolve it into a
1375 * library path.
1376 */
1377 Bstr bstrExtPack;
1378 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1379 if (FAILED(hrc))
1380 return Global::vboxStatusCodeFromCOM(hrc);
1381 if (bstrExtPack.isEmpty())
1382 return VINF_NOT_SUPPORTED;
1383
1384 Utf8Str strExtPack(bstrExtPack);
1385 Utf8Str strVrdeLibrary;
1386 int vrc = VINF_SUCCESS;
1387 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1388 strVrdeLibrary = "VBoxVRDP";
1389 else
1390 {
1391#ifdef VBOX_WITH_EXTPACK
1392 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1393 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1394#else
1395 vrc = VERR_FILE_NOT_FOUND;
1396#endif
1397 }
1398 if (RT_SUCCESS(vrc))
1399 {
1400 /*
1401 * Load the VRDE library and start the server, if it is enabled.
1402 */
1403 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1404 if (RT_SUCCESS(vrc))
1405 {
1406 VRDEENTRYPOINTS_4 *pEntryPoints4;
1407 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1408
1409 if (RT_SUCCESS(vrc))
1410 {
1411 mServerInterfaceVersion = 4;
1412 mEntryPoints = *pEntryPoints4;
1413 mpEntryPoints = &mEntryPoints;
1414 }
1415 else if (vrc == VERR_VERSION_MISMATCH)
1416 {
1417 /* An older version of VRDE is installed, try version 3. */
1418 VRDEENTRYPOINTS_3 *pEntryPoints3;
1419
1420 static VRDECALLBACKS_3 sCallbacks3 =
1421 {
1422 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1423 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1424 ConsoleVRDPServer::VRDPCallbackClientLogon,
1425 ConsoleVRDPServer::VRDPCallbackClientConnect,
1426 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1427 ConsoleVRDPServer::VRDPCallbackIntercept,
1428 ConsoleVRDPServer::VRDPCallbackUSB,
1429 ConsoleVRDPServer::VRDPCallbackClipboard,
1430 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1431 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1432 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1433 ConsoleVRDPServer::VRDPCallbackInput,
1434 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1435 ConsoleVRDPServer::VRDECallbackAudioIn
1436 };
1437
1438 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1439 if (RT_SUCCESS(vrc))
1440 {
1441 mServerInterfaceVersion = 3;
1442 mEntryPoints.header = pEntryPoints3->header;
1443 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1444 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1445 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1446 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1447 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1448 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1449 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1450 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1451 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1452 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1453 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1454 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1455 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1456 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1457 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1458 mEntryPoints.VRDEGetInterface = NULL;
1459 mpEntryPoints = &mEntryPoints;
1460 }
1461 else if (vrc == VERR_VERSION_MISMATCH)
1462 {
1463 /* An older version of VRDE is installed, try version 1. */
1464 VRDEENTRYPOINTS_1 *pEntryPoints1;
1465
1466 static VRDECALLBACKS_1 sCallbacks1 =
1467 {
1468 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1469 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1470 ConsoleVRDPServer::VRDPCallbackClientLogon,
1471 ConsoleVRDPServer::VRDPCallbackClientConnect,
1472 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1473 ConsoleVRDPServer::VRDPCallbackIntercept,
1474 ConsoleVRDPServer::VRDPCallbackUSB,
1475 ConsoleVRDPServer::VRDPCallbackClipboard,
1476 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1477 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1478 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1479 ConsoleVRDPServer::VRDPCallbackInput,
1480 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1481 };
1482
1483 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1484 if (RT_SUCCESS(vrc))
1485 {
1486 mServerInterfaceVersion = 1;
1487 mEntryPoints.header = pEntryPoints1->header;
1488 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1489 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1490 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1491 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1492 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1493 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1494 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1495 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1496 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1497 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1498 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1499 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1500 mEntryPoints.VRDERedirect = NULL;
1501 mEntryPoints.VRDEAudioInOpen = NULL;
1502 mEntryPoints.VRDEAudioInClose = NULL;
1503 mEntryPoints.VRDEGetInterface = NULL;
1504 mpEntryPoints = &mEntryPoints;
1505 }
1506 }
1507 }
1508
1509 if (RT_SUCCESS(vrc))
1510 {
1511 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1512
1513 if (mServerInterfaceVersion >= 4)
1514 {
1515 /* The server supports optional interfaces. */
1516 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1517
1518 /* Image interface. */
1519 m_interfaceImage.header.u64Version = 1;
1520 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1521
1522 m_interfaceCallbacksImage.header.u64Version = 1;
1523 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1524 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1525
1526 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1527 VRDE_IMAGE_INTERFACE_NAME,
1528 &m_interfaceImage.header,
1529 &m_interfaceCallbacksImage.header,
1530 this);
1531 if (RT_SUCCESS(vrc))
1532 {
1533 m_fInterfaceImage = true;
1534 }
1535
1536 /* Since these interfaces are optional, it is always a success here. */
1537 vrc = VINF_SUCCESS;
1538 }
1539#ifdef VBOX_WITH_USB
1540 remoteUSBThreadStart();
1541#endif
1542 }
1543 else
1544 {
1545 if (vrc != VERR_NET_ADDRESS_IN_USE)
1546 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1547 /* Don't unload the lib, because it prevents us trying again or
1548 because there may be other users? */
1549 }
1550 }
1551 }
1552
1553 return vrc;
1554}
1555
1556typedef struct H3DORInstance
1557{
1558 ConsoleVRDPServer *pThis;
1559 HVRDEIMAGE hImageBitmap;
1560 int32_t x;
1561 int32_t y;
1562 uint32_t w;
1563 uint32_t h;
1564 bool fCreated;
1565} H3DORInstance;
1566
1567/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1568 const char *pszFormat)
1569{
1570 LogFlowFunc(("ctx %p\n", pvContext));
1571
1572 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof (H3DORInstance));
1573
1574 if (p)
1575 {
1576 p->pThis = (ConsoleVRDPServer *)pvContext;
1577 p->hImageBitmap = NULL;
1578 p->x = 0;
1579 p->y = 0;
1580 p->w = 0;
1581 p->h = 0;
1582 p->fCreated = false;
1583
1584 /* Host 3D service passes the actual format of data in this redirect instance.
1585 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1586 */
1587 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1588 {
1589 /* Accept it. */
1590 }
1591 else
1592 {
1593 RTMemFree(p);
1594 p = NULL;
1595 }
1596 }
1597
1598 /* Caller check this for NULL. */
1599 *ppvInstance = p;
1600}
1601
1602/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1603 int32_t x, int32_t y, uint32_t w, uint32_t h)
1604{
1605 LogFlowFunc(("ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1606
1607 H3DORInstance *p = (H3DORInstance *)pvInstance;
1608 Assert(p);
1609 Assert(p->pThis);
1610
1611 /* @todo find out what to do if size changes to 0x0 from non zero */
1612 if (w == 0 || h == 0)
1613 {
1614 /* Do nothing. */
1615 return;
1616 }
1617
1618 RTRECT rect;
1619 rect.xLeft = x;
1620 rect.yTop = y;
1621 rect.xRight = x + w;
1622 rect.yBottom = y + h;
1623
1624 if (p->hImageBitmap)
1625 {
1626 /* An image handle has been already created,
1627 * check if it has the same size as the reported geometry.
1628 */
1629 if ( p->x == x
1630 && p->y == y
1631 && p->w == w
1632 && p->h == h)
1633 {
1634 LogFlowFunc(("geometry not changed\n"));
1635 /* Do nothing. Continue using the existing handle. */
1636 }
1637 else
1638 {
1639 int rc = p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1640 if (RT_SUCCESS(rc))
1641 {
1642 p->x = x;
1643 p->y = y;
1644 p->w = w;
1645 p->h = h;
1646 }
1647 else
1648 {
1649 /* The handle must be recreated. Delete existing handle here. */
1650 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1651 p->hImageBitmap = NULL;
1652 }
1653 }
1654 }
1655
1656 if (!p->hImageBitmap)
1657 {
1658 /* Create a new bitmap handle. */
1659 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1660 * Clipping can be done here or in VRDP server.
1661 * If VRDP does clipping, then uScreenId parameter
1662 * is not necessary and coords must be global.
1663 * (have to check which coords are used in opengl service).
1664 * Since all VRDE API uses a ScreenId,
1665 * the clipping must be done here in ConsoleVRDPServer
1666 */
1667 uint32_t fu32CompletionFlags = 0;
1668 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1669 &p->hImageBitmap,
1670 p,
1671 u32ScreenId,
1672 VRDE_IMAGE_F_CREATE_CONTENT_3D
1673 | VRDE_IMAGE_F_CREATE_WINDOW,
1674 &rect,
1675 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1676 NULL,
1677 0,
1678 &fu32CompletionFlags);
1679 if (RT_SUCCESS(rc))
1680 {
1681 p->x = x;
1682 p->y = y;
1683 p->w = w;
1684 p->h = h;
1685
1686 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1687 {
1688 p->fCreated = true;
1689 }
1690 }
1691 else
1692 {
1693 p->hImageBitmap = NULL;
1694 p->w = 0;
1695 p->h = 0;
1696 }
1697 }
1698}
1699
1700/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1701 uint32_t cRects, RTRECT *paRects)
1702{
1703 LogFlowFunc(("ins %p %d\n", pvInstance, cRects));
1704
1705 H3DORInstance *p = (H3DORInstance *)pvInstance;
1706 Assert(p);
1707 Assert(p->pThis);
1708
1709 if (cRects == 0)
1710 {
1711 /* Complete image is visible. */
1712 RTRECT rect;
1713 rect.xLeft = p->x;
1714 rect.yTop = p->y;
1715 rect.xRight = p->x + p->w;
1716 rect.yBottom = p->y + p->h;
1717 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1718 1,
1719 &rect);
1720 }
1721 else
1722 {
1723 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1724 cRects,
1725 paRects);
1726 }
1727}
1728
1729/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1730 void *pvData, uint32_t cbData)
1731{
1732 LogFlowFunc(("ins %p %p %d\n", pvInstance, pvData, cbData));
1733
1734 H3DORInstance *p = (H3DORInstance *)pvInstance;
1735 Assert(p);
1736 Assert(p->pThis);
1737
1738 /* Currently only a topdown BGR0 bitmap format is supported. */
1739 VRDEIMAGEBITMAP image;
1740
1741 image.cWidth = p->w;
1742 image.cHeight = p->h;
1743 image.pvData = pvData;
1744 image.cbData = cbData;
1745 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
1746 image.iScanDelta = -4 * p->w;
1747
1748 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
1749 p->x,
1750 p->y,
1751 p->w,
1752 p->h,
1753 &image,
1754 sizeof(VRDEIMAGEBITMAP));
1755}
1756
1757/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
1758{
1759 LogFlowFunc(("ins %p\n", pvInstance));
1760
1761 H3DORInstance *p = (H3DORInstance *)pvInstance;
1762 Assert(p);
1763 Assert(p->pThis);
1764
1765 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1766
1767 RTMemFree(p);
1768}
1769
1770/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
1771 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
1772{
1773 int rc = VINF_SUCCESS;
1774
1775 if (index == H3DOR_PROP_FORMATS)
1776 {
1777 /* Return a comma separated list of supported formats. */
1778 static const char *pszSupportedFormats = H3DOR_FMT_RGBA_TOPDOWN;
1779 uint32_t cbOut = (uint32_t)strlen(pszSupportedFormats) + 1;
1780 if (cbOut <= cbBuffer)
1781 {
1782 memcpy(pvBuffer, pszSupportedFormats, cbOut);
1783 }
1784 else
1785 {
1786 rc = VERR_BUFFER_OVERFLOW;
1787 }
1788 *pcbOut = cbOut;
1789 }
1790 else
1791 {
1792 rc = VERR_NOT_SUPPORTED;
1793 }
1794
1795 return rc;
1796}
1797
1798void ConsoleVRDPServer::remote3DRedirect(void)
1799{
1800 if (!m_fInterfaceImage)
1801 {
1802 /* No redirect without corresponding interface. */
1803 return;
1804 }
1805
1806 /* Check if 3D redirection has been enabled. */
1807 com::Bstr bstr;
1808 HRESULT hrc = mConsole->getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
1809
1810 if (hrc != S_OK)
1811 {
1812 bstr = "";
1813 }
1814
1815 com::Utf8Str value = bstr;
1816
1817 bool fEnabled = RTStrICmp(value.c_str(), "true") == 0
1818 || RTStrICmp(value.c_str(), "1") == 0;
1819
1820 if (!fEnabled)
1821 {
1822 return;
1823 }
1824
1825 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
1826 H3DOUTPUTREDIRECT outputRedirect =
1827 {
1828 this,
1829 H3DORBegin,
1830 H3DORGeometry,
1831 H3DORVisibleRegion,
1832 H3DORFrame,
1833 H3DOREnd,
1834 H3DORContextProperty
1835 };
1836
1837 VBOXHGCMSVCPARM parm;
1838
1839 parm.type = VBOX_HGCM_SVC_PARM_PTR;
1840 parm.u.pointer.addr = &outputRedirect;
1841 parm.u.pointer.size = sizeof(outputRedirect);
1842
1843 VMMDev *pVMMDev = mConsole->getVMMDev();
1844
1845 if (!pVMMDev)
1846 {
1847 AssertMsgFailed(("remote3DRedirect no vmmdev\n"));
1848 return;
1849 }
1850
1851 int rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL",
1852 SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT,
1853 SHCRGL_CPARMS_SET_OUTPUT_REDIRECT,
1854 &parm);
1855
1856 if (!RT_SUCCESS(rc))
1857 {
1858 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
1859 return;
1860 }
1861
1862 LogRel(("VRDE: Enabled 3D redirect.\n"));
1863
1864 return;
1865}
1866
1867/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
1868 void *pvUser,
1869 HVRDEIMAGE hVideo,
1870 uint32_t u32Id,
1871 void *pvData,
1872 uint32_t cbData)
1873{
1874 LogFlowFunc(("pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
1875 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
1876
1877 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
1878 H3DORInstance *p = (H3DORInstance *)pvUser;
1879 Assert(p);
1880 Assert(p->pThis);
1881 Assert(p->pThis == pServer);
1882
1883 // @todo Process u32Id
1884
1885 p->fCreated = true;
1886
1887 return VINF_SUCCESS;
1888}
1889
1890void ConsoleVRDPServer::EnableConnections(void)
1891{
1892 if (mpEntryPoints && mhServer)
1893 {
1894 mpEntryPoints->VRDEEnableConnections(mhServer, true);
1895
1896 /* Redirect 3D output if it is enabled. */
1897 remote3DRedirect();
1898 }
1899}
1900
1901void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
1902{
1903 if (mpEntryPoints && mhServer)
1904 {
1905 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
1906 }
1907}
1908
1909void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
1910{
1911 if (mpEntryPoints && mhServer)
1912 {
1913 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
1914 }
1915}
1916
1917void ConsoleVRDPServer::MousePointerHide(void)
1918{
1919 if (mpEntryPoints && mhServer)
1920 {
1921 mpEntryPoints->VRDEHidePointer(mhServer);
1922 }
1923}
1924
1925void ConsoleVRDPServer::Stop(void)
1926{
1927 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1928 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1929 if (mhServer)
1930 {
1931 HVRDESERVER hServer = mhServer;
1932
1933 /* Reset the handle to avoid further calls to the server. */
1934 mhServer = 0;
1935
1936 if (mpEntryPoints && hServer)
1937 {
1938 mpEntryPoints->VRDEDestroy(hServer);
1939 }
1940 }
1941
1942#ifdef VBOX_WITH_USB
1943 remoteUSBThreadStop();
1944#endif /* VBOX_WITH_USB */
1945
1946 mpfnAuthEntry = NULL;
1947 mpfnAuthEntry2 = NULL;
1948 mpfnAuthEntry3 = NULL;
1949
1950 if (mAuthLibrary)
1951 {
1952 RTLdrClose(mAuthLibrary);
1953 mAuthLibrary = 0;
1954 }
1955}
1956
1957/* Worker thread for Remote USB. The thread polls the clients for
1958 * the list of attached USB devices.
1959 * The thread is also responsible for attaching/detaching devices
1960 * to/from the VM.
1961 *
1962 * It is expected that attaching/detaching is not a frequent operation.
1963 *
1964 * The thread is always running when the VRDP server is active.
1965 *
1966 * The thread scans backends and requests the device list every 2 seconds.
1967 *
1968 * When device list is available, the thread calls the Console to process it.
1969 *
1970 */
1971#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1972
1973#ifdef VBOX_WITH_USB
1974static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
1975{
1976 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1977
1978 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1979
1980 pOwner->notifyRemoteUSBThreadRunning(self);
1981
1982 while (pOwner->isRemoteUSBThreadRunning())
1983 {
1984 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1985
1986 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
1987 {
1988 pRemoteUSBBackend->PollRemoteDevices();
1989 }
1990
1991 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
1992
1993 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
1994 }
1995
1996 return VINF_SUCCESS;
1997}
1998
1999void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
2000{
2001 mUSBBackends.thread = thread;
2002 mUSBBackends.fThreadRunning = true;
2003 int rc = RTThreadUserSignal(thread);
2004 AssertRC(rc);
2005}
2006
2007bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
2008{
2009 return mUSBBackends.fThreadRunning;
2010}
2011
2012void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
2013{
2014 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
2015 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
2016 NOREF(rc);
2017}
2018
2019void ConsoleVRDPServer::remoteUSBThreadStart(void)
2020{
2021 int rc = RTSemEventCreate(&mUSBBackends.event);
2022
2023 if (RT_FAILURE(rc))
2024 {
2025 AssertFailed();
2026 mUSBBackends.event = 0;
2027 }
2028
2029 if (RT_SUCCESS(rc))
2030 {
2031 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
2032 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
2033 }
2034
2035 if (RT_FAILURE(rc))
2036 {
2037 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
2038 mUSBBackends.thread = NIL_RTTHREAD;
2039 }
2040 else
2041 {
2042 /* Wait until the thread is ready. */
2043 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
2044 AssertRC(rc);
2045 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
2046 }
2047}
2048
2049void ConsoleVRDPServer::remoteUSBThreadStop(void)
2050{
2051 mUSBBackends.fThreadRunning = false;
2052
2053 if (mUSBBackends.thread != NIL_RTTHREAD)
2054 {
2055 Assert (mUSBBackends.event != 0);
2056
2057 RTSemEventSignal(mUSBBackends.event);
2058
2059 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
2060 AssertRC(rc);
2061
2062 mUSBBackends.thread = NIL_RTTHREAD;
2063 }
2064
2065 if (mUSBBackends.event)
2066 {
2067 RTSemEventDestroy(mUSBBackends.event);
2068 mUSBBackends.event = 0;
2069 }
2070}
2071#endif /* VBOX_WITH_USB */
2072
2073AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
2074 const char *pszUser, const char *pszPassword, const char *pszDomain,
2075 uint32_t u32ClientId)
2076{
2077 AUTHUUID rawuuid;
2078
2079 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2080
2081 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
2082 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
2083
2084 /*
2085 * Called only from VRDP input thread. So thread safety is not required.
2086 */
2087
2088 if (!mAuthLibrary)
2089 {
2090 /* Load the external authentication library. */
2091 Bstr authLibrary;
2092 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
2093
2094 Utf8Str filename = authLibrary;
2095
2096 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
2097
2098 int rc;
2099 if (RTPathHavePath(filename.c_str()))
2100 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
2101 else
2102 {
2103 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
2104 if (RT_FAILURE(rc))
2105 {
2106 /* Backward compatibility with old default 'VRDPAuth' name.
2107 * Try to load new default 'VBoxAuth' instead.
2108 */
2109 if (filename == "VRDPAuth")
2110 {
2111 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library VBoxAuth\n"));
2112 rc = RTLdrLoadAppPriv("VBoxAuth", &mAuthLibrary);
2113 }
2114 }
2115 }
2116
2117 if (RT_FAILURE(rc))
2118 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
2119
2120 if (RT_SUCCESS(rc))
2121 {
2122 typedef struct AuthEntryInfo
2123 {
2124 const char *pszName;
2125 void **ppvAddress;
2126
2127 } AuthEntryInfo;
2128 AuthEntryInfo entries[] =
2129 {
2130 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
2131 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
2132 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
2133 { NULL, NULL }
2134 };
2135
2136 /* Get the entry point. */
2137 AuthEntryInfo *pEntryInfo = &entries[0];
2138 while (pEntryInfo->pszName)
2139 {
2140 *pEntryInfo->ppvAddress = NULL;
2141
2142 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
2143 if (RT_SUCCESS(rc2))
2144 {
2145 /* Found an entry point. */
2146 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
2147 rc = VINF_SUCCESS;
2148 break;
2149 }
2150
2151 if (rc2 != VERR_SYMBOL_NOT_FOUND)
2152 {
2153 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
2154 }
2155 rc = rc2;
2156
2157 pEntryInfo++;
2158 }
2159 }
2160
2161 if (RT_FAILURE(rc))
2162 {
2163 mConsole->setError(E_FAIL,
2164 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
2165 filename.c_str(),
2166 rc);
2167
2168 mpfnAuthEntry = NULL;
2169 mpfnAuthEntry2 = NULL;
2170 mpfnAuthEntry3 = NULL;
2171
2172 if (mAuthLibrary)
2173 {
2174 RTLdrClose(mAuthLibrary);
2175 mAuthLibrary = 0;
2176 }
2177
2178 return AuthResultAccessDenied;
2179 }
2180 }
2181
2182 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2183
2184 AuthResult result = AuthResultAccessDenied;
2185 if (mpfnAuthEntry3)
2186 {
2187 result = mpfnAuthEntry3("vrde", &rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2188 }
2189 else if (mpfnAuthEntry2)
2190 {
2191 result = mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2192 }
2193 else if (mpfnAuthEntry)
2194 {
2195 result = mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
2196 }
2197
2198 switch (result)
2199 {
2200 case AuthResultAccessDenied:
2201 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
2202 break;
2203 case AuthResultAccessGranted:
2204 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
2205 break;
2206 case AuthResultDelegateToGuest:
2207 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
2208 break;
2209 default:
2210 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
2211 result = AuthResultAccessDenied;
2212 }
2213
2214 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
2215
2216 return result;
2217}
2218
2219void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
2220{
2221 AUTHUUID rawuuid;
2222
2223 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2224
2225 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
2226 rawuuid, u32ClientId));
2227
2228 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2229
2230 if (mpfnAuthEntry3)
2231 mpfnAuthEntry3("vrde", &rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2232 else if (mpfnAuthEntry2)
2233 mpfnAuthEntry2(&rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2234}
2235
2236int ConsoleVRDPServer::lockConsoleVRDPServer(void)
2237{
2238 int rc = RTCritSectEnter(&mCritSect);
2239 AssertRC(rc);
2240 return rc;
2241}
2242
2243void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
2244{
2245 RTCritSectLeave(&mCritSect);
2246}
2247
2248DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
2249 uint32_t u32ClientId,
2250 uint32_t u32Function,
2251 uint32_t u32Format,
2252 const void *pvData,
2253 uint32_t cbData)
2254{
2255 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
2256 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
2257
2258 int rc = VINF_SUCCESS;
2259
2260 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
2261
2262 NOREF(u32ClientId);
2263
2264 switch (u32Function)
2265 {
2266 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
2267 {
2268 if (pServer->mpfnClipboardCallback)
2269 {
2270 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
2271 u32Format,
2272 (void *)pvData,
2273 cbData);
2274 }
2275 } break;
2276
2277 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
2278 {
2279 if (pServer->mpfnClipboardCallback)
2280 {
2281 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
2282 u32Format,
2283 (void *)pvData,
2284 cbData);
2285 }
2286 } break;
2287
2288 default:
2289 rc = VERR_NOT_SUPPORTED;
2290 }
2291
2292 return rc;
2293}
2294
2295DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
2296 uint32_t u32Function,
2297 void *pvParms,
2298 uint32_t cbParms)
2299{
2300 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
2301 pvExtension, u32Function, pvParms, cbParms));
2302
2303 int rc = VINF_SUCCESS;
2304
2305 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
2306
2307 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
2308
2309 switch (u32Function)
2310 {
2311 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
2312 {
2313 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
2314 } break;
2315
2316 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
2317 {
2318 /* The guest announces clipboard formats. This must be delivered to all clients. */
2319 if (mpEntryPoints && pServer->mhServer)
2320 {
2321 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2322 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
2323 pParms->u32Format,
2324 NULL,
2325 0,
2326 NULL);
2327 }
2328 } break;
2329
2330 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
2331 {
2332 /* The clipboard service expects that the pvData buffer will be filled
2333 * with clipboard data. The server returns the data from the client that
2334 * announced the requested format most recently.
2335 */
2336 if (mpEntryPoints && pServer->mhServer)
2337 {
2338 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2339 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
2340 pParms->u32Format,
2341 pParms->u.pvData,
2342 pParms->cbData,
2343 &pParms->cbData);
2344 }
2345 } break;
2346
2347 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
2348 {
2349 if (mpEntryPoints && pServer->mhServer)
2350 {
2351 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2352 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
2353 pParms->u32Format,
2354 pParms->u.pvData,
2355 pParms->cbData,
2356 NULL);
2357 }
2358 } break;
2359
2360 default:
2361 rc = VERR_NOT_SUPPORTED;
2362 }
2363
2364 return rc;
2365}
2366
2367void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
2368{
2369 int rc = lockConsoleVRDPServer();
2370
2371 if (RT_SUCCESS(rc))
2372 {
2373 if (mcClipboardRefs == 0)
2374 {
2375 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
2376
2377 if (RT_SUCCESS(rc))
2378 {
2379 mcClipboardRefs++;
2380 }
2381 }
2382
2383 unlockConsoleVRDPServer();
2384 }
2385}
2386
2387void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
2388{
2389 int rc = lockConsoleVRDPServer();
2390
2391 if (RT_SUCCESS(rc))
2392 {
2393 mcClipboardRefs--;
2394
2395 if (mcClipboardRefs == 0)
2396 {
2397 HGCMHostUnregisterServiceExtension(mhClipboard);
2398 }
2399
2400 unlockConsoleVRDPServer();
2401 }
2402}
2403
2404/* That is called on INPUT thread of the VRDP server.
2405 * The ConsoleVRDPServer keeps a list of created backend instances.
2406 */
2407void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
2408{
2409#ifdef VBOX_WITH_USB
2410 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
2411
2412 /* Create a new instance of the USB backend for the new client. */
2413 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
2414
2415 if (pRemoteUSBBackend)
2416 {
2417 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
2418
2419 /* Append the new instance in the list. */
2420 int rc = lockConsoleVRDPServer();
2421
2422 if (RT_SUCCESS(rc))
2423 {
2424 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
2425 if (mUSBBackends.pHead)
2426 {
2427 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
2428 }
2429 else
2430 {
2431 mUSBBackends.pTail = pRemoteUSBBackend;
2432 }
2433
2434 mUSBBackends.pHead = pRemoteUSBBackend;
2435
2436 unlockConsoleVRDPServer();
2437
2438 if (ppvIntercept)
2439 {
2440 *ppvIntercept = pRemoteUSBBackend;
2441 }
2442 }
2443
2444 if (RT_FAILURE(rc))
2445 {
2446 pRemoteUSBBackend->Release();
2447 }
2448 }
2449#endif /* VBOX_WITH_USB */
2450}
2451
2452void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
2453{
2454#ifdef VBOX_WITH_USB
2455 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
2456
2457 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2458
2459 /* Find the instance. */
2460 int rc = lockConsoleVRDPServer();
2461
2462 if (RT_SUCCESS(rc))
2463 {
2464 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2465
2466 if (pRemoteUSBBackend)
2467 {
2468 /* Notify that it will be deleted. */
2469 pRemoteUSBBackend->NotifyDelete();
2470 }
2471
2472 unlockConsoleVRDPServer();
2473 }
2474
2475 if (pRemoteUSBBackend)
2476 {
2477 /* Here the instance has been excluded from the list and can be dereferenced. */
2478 pRemoteUSBBackend->Release();
2479 }
2480#endif
2481}
2482
2483void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
2484{
2485#ifdef VBOX_WITH_USB
2486 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2487
2488 /* Find the instance. */
2489 int rc = lockConsoleVRDPServer();
2490
2491 if (RT_SUCCESS(rc))
2492 {
2493 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2494
2495 if (pRemoteUSBBackend)
2496 {
2497 /* Inform the backend instance that it is referenced by the Guid. */
2498 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
2499
2500 if (fAdded)
2501 {
2502 /* Reference the instance because its pointer is being taken. */
2503 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
2504 }
2505 else
2506 {
2507 pRemoteUSBBackend = NULL;
2508 }
2509 }
2510
2511 unlockConsoleVRDPServer();
2512 }
2513
2514 if (pRemoteUSBBackend)
2515 {
2516 return pRemoteUSBBackend->GetBackendCallbackPointer();
2517 }
2518
2519#endif
2520 return NULL;
2521}
2522
2523void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
2524{
2525#ifdef VBOX_WITH_USB
2526 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2527
2528 /* Find the instance. */
2529 int rc = lockConsoleVRDPServer();
2530
2531 if (RT_SUCCESS(rc))
2532 {
2533 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
2534
2535 if (pRemoteUSBBackend)
2536 {
2537 pRemoteUSBBackend->removeUUID(pGuid);
2538 }
2539
2540 unlockConsoleVRDPServer();
2541
2542 if (pRemoteUSBBackend)
2543 {
2544 pRemoteUSBBackend->Release();
2545 }
2546 }
2547#endif
2548}
2549
2550RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
2551{
2552 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
2553
2554 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
2555#ifdef VBOX_WITH_USB
2556
2557 int rc = lockConsoleVRDPServer();
2558
2559 if (RT_SUCCESS(rc))
2560 {
2561 if (pRemoteUSBBackend == NULL)
2562 {
2563 /* The first backend in the list is requested. */
2564 pNextRemoteUSBBackend = mUSBBackends.pHead;
2565 }
2566 else
2567 {
2568 /* Get pointer to the next backend. */
2569 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2570 }
2571
2572 if (pNextRemoteUSBBackend)
2573 {
2574 pNextRemoteUSBBackend->AddRef();
2575 }
2576
2577 unlockConsoleVRDPServer();
2578
2579 if (pRemoteUSBBackend)
2580 {
2581 pRemoteUSBBackend->Release();
2582 }
2583 }
2584#endif
2585
2586 return pNextRemoteUSBBackend;
2587}
2588
2589#ifdef VBOX_WITH_USB
2590/* Internal method. Called under the ConsoleVRDPServerLock. */
2591RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
2592{
2593 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2594
2595 while (pRemoteUSBBackend)
2596 {
2597 if (pRemoteUSBBackend->ClientId() == u32ClientId)
2598 {
2599 break;
2600 }
2601
2602 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2603 }
2604
2605 return pRemoteUSBBackend;
2606}
2607
2608/* Internal method. Called under the ConsoleVRDPServerLock. */
2609RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
2610{
2611 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2612
2613 while (pRemoteUSBBackend)
2614 {
2615 if (pRemoteUSBBackend->findUUID(pGuid))
2616 {
2617 break;
2618 }
2619
2620 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2621 }
2622
2623 return pRemoteUSBBackend;
2624}
2625#endif
2626
2627/* Internal method. Called by the backend destructor. */
2628void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2629{
2630#ifdef VBOX_WITH_USB
2631 int rc = lockConsoleVRDPServer();
2632 AssertRC(rc);
2633
2634 /* Exclude the found instance from the list. */
2635 if (pRemoteUSBBackend->pNext)
2636 {
2637 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2638 }
2639 else
2640 {
2641 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2642 }
2643
2644 if (pRemoteUSBBackend->pPrev)
2645 {
2646 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2647 }
2648 else
2649 {
2650 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2651 }
2652
2653 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2654
2655 unlockConsoleVRDPServer();
2656#endif
2657}
2658
2659
2660void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2661{
2662 if (mpEntryPoints && mhServer)
2663 {
2664 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2665 }
2666}
2667
2668void ConsoleVRDPServer::SendResize(void) const
2669{
2670 if (mpEntryPoints && mhServer)
2671 {
2672 mpEntryPoints->VRDEResize(mhServer);
2673 }
2674}
2675
2676void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2677{
2678 VRDEORDERHDR update;
2679 update.x = x;
2680 update.y = y;
2681 update.w = w;
2682 update.h = h;
2683 if (mpEntryPoints && mhServer)
2684 {
2685 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
2686 }
2687}
2688
2689void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
2690{
2691 if (mpEntryPoints && mhServer)
2692 {
2693 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
2694 }
2695}
2696
2697void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2698{
2699 if (mpEntryPoints && mhServer)
2700 {
2701 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
2702 }
2703}
2704
2705void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2706{
2707 if (mpEntryPoints && mhServer)
2708 {
2709 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2710 }
2711}
2712
2713/* @todo rc not needed? */
2714int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
2715 void *pvContext,
2716 uint32_t cSamples,
2717 uint32_t iSampleHz,
2718 uint32_t cChannels,
2719 uint32_t cBits)
2720{
2721 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
2722 {
2723 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2724 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2725 {
2726 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
2727 mpEntryPoints->VRDEAudioInOpen (mhServer,
2728 pvContext,
2729 u32ClientId,
2730 audioFormat,
2731 cSamples);
2732 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
2733 * Currently not used because only one client is allowed to
2734 * do audio input and the client id is saved by the ConsoleVRDPServer.
2735 */
2736
2737 return VINF_SUCCESS;
2738 }
2739 }
2740 return VERR_NOT_SUPPORTED;
2741}
2742
2743void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
2744{
2745 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
2746 {
2747 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2748 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2749 {
2750 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
2751 }
2752 }
2753}
2754
2755
2756
2757void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2758{
2759 if (index == VRDE_QI_PORT)
2760 {
2761 uint32_t cbOut = sizeof(int32_t);
2762
2763 if (cbBuffer >= cbOut)
2764 {
2765 *pcbOut = cbOut;
2766 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2767 }
2768 }
2769 else if (mpEntryPoints && mhServer)
2770 {
2771 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2772 }
2773}
2774
2775/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
2776{
2777 int rc = VINF_SUCCESS;
2778
2779 if (mVRDPLibrary == NIL_RTLDRMOD)
2780 {
2781 RTERRINFOSTATIC ErrInfo;
2782 RTErrInfoInitStatic(&ErrInfo);
2783
2784 if (RTPathHavePath(pszLibraryName))
2785 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
2786 else
2787 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
2788 if (RT_SUCCESS(rc))
2789 {
2790 struct SymbolEntry
2791 {
2792 const char *name;
2793 void **ppfn;
2794 };
2795
2796 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2797
2798 static const struct SymbolEntry s_aSymbols[] =
2799 {
2800 DEFSYMENTRY(VRDECreateServer)
2801 };
2802
2803 #undef DEFSYMENTRY
2804
2805 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
2806 {
2807 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
2808
2809 if (RT_FAILURE(rc))
2810 {
2811 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
2812 break;
2813 }
2814 }
2815 }
2816 else
2817 {
2818 if (RTErrInfoIsSet(&ErrInfo.Core))
2819 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
2820 else
2821 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
2822
2823 mVRDPLibrary = NIL_RTLDRMOD;
2824 }
2825 }
2826
2827 if (RT_FAILURE(rc))
2828 {
2829 if (mVRDPLibrary != NIL_RTLDRMOD)
2830 {
2831 RTLdrClose(mVRDPLibrary);
2832 mVRDPLibrary = NIL_RTLDRMOD;
2833 }
2834 }
2835
2836 return rc;
2837}
2838
2839/*
2840 * IVRDEServerInfo implementation.
2841 */
2842// constructor / destructor
2843/////////////////////////////////////////////////////////////////////////////
2844
2845VRDEServerInfo::VRDEServerInfo()
2846 : mParent(NULL)
2847{
2848}
2849
2850VRDEServerInfo::~VRDEServerInfo()
2851{
2852}
2853
2854
2855HRESULT VRDEServerInfo::FinalConstruct()
2856{
2857 return BaseFinalConstruct();
2858}
2859
2860void VRDEServerInfo::FinalRelease()
2861{
2862 uninit();
2863 BaseFinalRelease();
2864}
2865
2866// public methods only for internal purposes
2867/////////////////////////////////////////////////////////////////////////////
2868
2869/**
2870 * Initializes the guest object.
2871 */
2872HRESULT VRDEServerInfo::init(Console *aParent)
2873{
2874 LogFlowThisFunc(("aParent=%p\n", aParent));
2875
2876 ComAssertRet(aParent, E_INVALIDARG);
2877
2878 /* Enclose the state transition NotReady->InInit->Ready */
2879 AutoInitSpan autoInitSpan(this);
2880 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2881
2882 unconst(mParent) = aParent;
2883
2884 /* Confirm a successful initialization */
2885 autoInitSpan.setSucceeded();
2886
2887 return S_OK;
2888}
2889
2890/**
2891 * Uninitializes the instance and sets the ready flag to FALSE.
2892 * Called either from FinalRelease() or by the parent when it gets destroyed.
2893 */
2894void VRDEServerInfo::uninit()
2895{
2896 LogFlowThisFunc(("\n"));
2897
2898 /* Enclose the state transition Ready->InUninit->NotReady */
2899 AutoUninitSpan autoUninitSpan(this);
2900 if (autoUninitSpan.uninitDone())
2901 return;
2902
2903 unconst(mParent) = NULL;
2904}
2905
2906// IVRDEServerInfo properties
2907/////////////////////////////////////////////////////////////////////////////
2908
2909#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2910 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2911 { \
2912 if (!a##_aName) \
2913 return E_POINTER; \
2914 \
2915 AutoCaller autoCaller(this); \
2916 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2917 \
2918 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2919 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2920 \
2921 uint32_t value; \
2922 uint32_t cbOut = 0; \
2923 \
2924 mParent->consoleVRDPServer()->QueryInfo \
2925 (_aIndex, &value, sizeof(value), &cbOut); \
2926 \
2927 *a##_aName = cbOut? !!value: FALSE; \
2928 \
2929 return S_OK; \
2930 } \
2931 extern void IMPL_GETTER_BOOL_DUMMY(void)
2932
2933#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
2934 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2935 { \
2936 if (!a##_aName) \
2937 return E_POINTER; \
2938 \
2939 AutoCaller autoCaller(this); \
2940 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2941 \
2942 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2943 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2944 \
2945 _aType value; \
2946 uint32_t cbOut = 0; \
2947 \
2948 mParent->consoleVRDPServer()->QueryInfo \
2949 (_aIndex, &value, sizeof(value), &cbOut); \
2950 \
2951 if (_aValueMask) value &= (_aValueMask); \
2952 *a##_aName = cbOut? value: 0; \
2953 \
2954 return S_OK; \
2955 } \
2956 extern void IMPL_GETTER_SCALAR_DUMMY(void)
2957
2958#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2959 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2960 { \
2961 if (!a##_aName) \
2962 return E_POINTER; \
2963 \
2964 AutoCaller autoCaller(this); \
2965 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2966 \
2967 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2968 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2969 \
2970 uint32_t cbOut = 0; \
2971 \
2972 mParent->consoleVRDPServer()->QueryInfo \
2973 (_aIndex, NULL, 0, &cbOut); \
2974 \
2975 if (cbOut == 0) \
2976 { \
2977 Bstr str(""); \
2978 str.cloneTo(a##_aName); \
2979 return S_OK; \
2980 } \
2981 \
2982 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
2983 \
2984 if (!pchBuffer) \
2985 { \
2986 Log(("VRDEServerInfo::" \
2987 #_aName \
2988 ": Failed to allocate memory %d bytes\n", cbOut)); \
2989 return E_OUTOFMEMORY; \
2990 } \
2991 \
2992 mParent->consoleVRDPServer()->QueryInfo \
2993 (_aIndex, pchBuffer, cbOut, &cbOut); \
2994 \
2995 Bstr str(pchBuffer); \
2996 \
2997 str.cloneTo(a##_aName); \
2998 \
2999 RTMemTmpFree(pchBuffer); \
3000 \
3001 return S_OK; \
3002 } \
3003 extern void IMPL_GETTER_BSTR_DUMMY(void)
3004
3005IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
3006IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
3007IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
3008IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
3009IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
3010IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
3011IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
3012IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
3013IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
3014IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
3015IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
3016IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
3017IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
3018IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
3019IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
3020
3021#undef IMPL_GETTER_BSTR
3022#undef IMPL_GETTER_SCALAR
3023#undef IMPL_GETTER_BOOL
3024/* 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