VirtualBox

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

Last change on this file since 39895 was 39493, checked in by vboxsync, 13 years ago

VRDP, Main: forward UTCINFO events as guest properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 99.4 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 39493 2011-12-01 15:42:02Z 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 case VRDE_SP_CLIENT_STATUS:
859 {
860 if (cbBuffer < sizeof(VRDECLIENTSTATUS))
861 {
862 rc = VERR_INVALID_PARAMETER;
863 break;
864 }
865
866 size_t cbStatus = cbBuffer - RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus);
867
868 VRDECLIENTSTATUS *pStatus = (VRDECLIENTSTATUS *)pvBuffer;
869
870 if (cbBuffer < RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus) + pStatus->cbStatus)
871 {
872 rc = VERR_INVALID_PARAMETER;
873 break;
874 }
875
876 size_t cchStatus = 0;
877 rc = RTStrNLenEx(pStatus->achStatus, cbStatus, &cchStatus);
878
879 if (RT_FAILURE(rc))
880 {
881 rc = VERR_INVALID_PARAMETER;
882 break;
883 }
884
885 Log(("VRDE_SP_CLIENT_STATUS [%s]\n", pStatus->achStatus));
886
887 server->mConsole->VRDPClientStatusChange(pStatus->u32ClientId, pStatus->achStatus);
888
889 rc = VINF_SUCCESS;
890
891 if (pcbOut)
892 {
893 *pcbOut = cbBuffer;
894 }
895
896 server->mConsole->onVRDEServerInfoChange();
897 } break;
898
899 default:
900 break;
901 }
902
903 return rc;
904}
905
906DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
907{
908 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
909
910 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
911}
912
913DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
914{
915 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
916
917 server->mConsole->VRDPClientConnect(u32ClientId);
918}
919
920DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
921{
922 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
923
924 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
925
926 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
927 {
928 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
929 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
930
931 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
932 if (pPort)
933 {
934 pPort->pfnAudioInputIntercept(pPort, false);
935 }
936 else
937 {
938 AssertFailed();
939 }
940 }
941}
942
943DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
944{
945 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
946
947 LogFlowFunc(("%x\n", fu32Intercept));
948
949 int rc = VERR_NOT_SUPPORTED;
950
951 switch (fu32Intercept)
952 {
953 case VRDE_CLIENT_INTERCEPT_AUDIO:
954 {
955 server->mConsole->VRDPInterceptAudio(u32ClientId);
956 if (ppvIntercept)
957 {
958 *ppvIntercept = server;
959 }
960 rc = VINF_SUCCESS;
961 } break;
962
963 case VRDE_CLIENT_INTERCEPT_USB:
964 {
965 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
966 rc = VINF_SUCCESS;
967 } break;
968
969 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
970 {
971 server->mConsole->VRDPInterceptClipboard(u32ClientId);
972 if (ppvIntercept)
973 {
974 *ppvIntercept = server;
975 }
976 rc = VINF_SUCCESS;
977 } break;
978
979 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
980 {
981 /* This request is processed internally by the ConsoleVRDPServer.
982 * Only one client is allowed to intercept audio input.
983 */
984 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
985 {
986 Log(("AUDIOIN: connected client %u\n", u32ClientId));
987
988 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
989 if (pPort)
990 {
991 pPort->pfnAudioInputIntercept(pPort, true);
992 if (ppvIntercept)
993 {
994 *ppvIntercept = server;
995 }
996 }
997 else
998 {
999 AssertFailed();
1000 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
1001 rc = VERR_NOT_SUPPORTED;
1002 }
1003 }
1004 else
1005 {
1006 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
1007 rc = VERR_NOT_SUPPORTED;
1008 }
1009 } break;
1010
1011 default:
1012 break;
1013 }
1014
1015 return rc;
1016}
1017
1018DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1019{
1020#ifdef VBOX_WITH_USB
1021 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1022#else
1023 return VERR_NOT_SUPPORTED;
1024#endif
1025}
1026
1027DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
1028{
1029 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1030}
1031
1032DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
1033{
1034 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1035
1036 bool fAvailable = false;
1037
1038 IFramebuffer *pfb = NULL;
1039 LONG xOrigin = 0;
1040 LONG yOrigin = 0;
1041
1042 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1043
1044 if (pfb)
1045 {
1046 pfb->Lock ();
1047
1048 /* Query framebuffer parameters. */
1049 ULONG lineSize = 0;
1050 pfb->COMGETTER(BytesPerLine)(&lineSize);
1051
1052 ULONG bitsPerPixel = 0;
1053 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1054
1055 BYTE *address = NULL;
1056 pfb->COMGETTER(Address)(&address);
1057
1058 ULONG height = 0;
1059 pfb->COMGETTER(Height)(&height);
1060
1061 ULONG width = 0;
1062 pfb->COMGETTER(Width)(&width);
1063
1064 /* Now fill the information as requested by the caller. */
1065 pInfo->pu8Bits = address;
1066 pInfo->xOrigin = xOrigin;
1067 pInfo->yOrigin = yOrigin;
1068 pInfo->cWidth = width;
1069 pInfo->cHeight = height;
1070 pInfo->cBitsPerPixel = bitsPerPixel;
1071 pInfo->cbLine = lineSize;
1072
1073 pfb->Unlock();
1074
1075 fAvailable = true;
1076 }
1077
1078 if (server->maFramebuffers[uScreenId])
1079 {
1080 server->maFramebuffers[uScreenId]->Release();
1081 }
1082 server->maFramebuffers[uScreenId] = pfb;
1083
1084 return fAvailable;
1085}
1086
1087DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1088{
1089 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1090
1091 if (server->maFramebuffers[uScreenId])
1092 {
1093 server->maFramebuffers[uScreenId]->Lock();
1094 }
1095}
1096
1097DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1098{
1099 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1100
1101 if (server->maFramebuffers[uScreenId])
1102 {
1103 server->maFramebuffers[uScreenId]->Unlock();
1104 }
1105}
1106
1107static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1108{
1109 if ( pInputSynch->cGuestNumLockAdaptions
1110 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1111 {
1112 pInputSynch->cGuestNumLockAdaptions--;
1113 pKeyboard->PutScancode(0x45);
1114 pKeyboard->PutScancode(0x45 | 0x80);
1115 }
1116 if ( pInputSynch->cGuestCapsLockAdaptions
1117 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1118 {
1119 pInputSynch->cGuestCapsLockAdaptions--;
1120 pKeyboard->PutScancode(0x3a);
1121 pKeyboard->PutScancode(0x3a | 0x80);
1122 }
1123}
1124
1125DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1126{
1127 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1128 Console *pConsole = server->mConsole;
1129
1130 switch (type)
1131 {
1132 case VRDE_INPUT_SCANCODE:
1133 {
1134 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1135 {
1136 IKeyboard *pKeyboard = pConsole->getKeyboard();
1137
1138 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1139
1140 /* Track lock keys. */
1141 if (pInputScancode->uScancode == 0x45)
1142 {
1143 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1144 }
1145 else if (pInputScancode->uScancode == 0x3a)
1146 {
1147 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1148 }
1149 else if (pInputScancode->uScancode == 0x46)
1150 {
1151 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1152 }
1153 else if ((pInputScancode->uScancode & 0x80) == 0)
1154 {
1155 /* Key pressed. */
1156 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1157 }
1158
1159 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1160 }
1161 } break;
1162
1163 case VRDE_INPUT_POINT:
1164 {
1165 if (cbInput == sizeof(VRDEINPUTPOINT))
1166 {
1167 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1168
1169 int mouseButtons = 0;
1170 int iWheel = 0;
1171
1172 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1173 {
1174 mouseButtons |= MouseButtonState_LeftButton;
1175 }
1176 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1177 {
1178 mouseButtons |= MouseButtonState_RightButton;
1179 }
1180 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1181 {
1182 mouseButtons |= MouseButtonState_MiddleButton;
1183 }
1184 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1185 {
1186 mouseButtons |= MouseButtonState_WheelUp;
1187 iWheel = -1;
1188 }
1189 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1190 {
1191 mouseButtons |= MouseButtonState_WheelDown;
1192 iWheel = 1;
1193 }
1194
1195 if (server->m_fGuestWantsAbsolute)
1196 {
1197 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1198 } else
1199 {
1200 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1201 pInputPoint->y - server->m_mousey,
1202 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1203 server->m_mousex = pInputPoint->x;
1204 server->m_mousey = pInputPoint->y;
1205 }
1206 }
1207 } break;
1208
1209 case VRDE_INPUT_CAD:
1210 {
1211 pConsole->getKeyboard()->PutCAD();
1212 } break;
1213
1214 case VRDE_INPUT_RESET:
1215 {
1216 pConsole->Reset();
1217 } break;
1218
1219 case VRDE_INPUT_SYNCH:
1220 {
1221 if (cbInput == sizeof(VRDEINPUTSYNCH))
1222 {
1223 IKeyboard *pKeyboard = pConsole->getKeyboard();
1224
1225 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1226
1227 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1228 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1229 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1230
1231 /* The client initiated synchronization. Always make the guest to reflect the client state.
1232 * Than means, when the guest changes the state itself, it is forced to return to the client
1233 * state.
1234 */
1235 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1236 {
1237 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1238 }
1239
1240 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1241 {
1242 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1243 }
1244
1245 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1246 }
1247 } break;
1248
1249 default:
1250 break;
1251 }
1252}
1253
1254DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1255{
1256 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1257
1258 server->mConsole->getDisplay()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1259}
1260
1261DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1262 void *pvCtx,
1263 uint32_t u32ClientId,
1264 uint32_t u32Event,
1265 const void *pvData,
1266 uint32_t cbData)
1267{
1268 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1269
1270 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1271
1272 switch (u32Event)
1273 {
1274 case VRDE_AUDIOIN_BEGIN:
1275 {
1276 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1277
1278 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1279 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1280 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1281 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1282 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1283 );
1284 } break;
1285
1286 case VRDE_AUDIOIN_DATA:
1287 {
1288 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1289 } break;
1290
1291 case VRDE_AUDIOIN_END:
1292 {
1293 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1294 } break;
1295
1296 default:
1297 return;
1298 }
1299}
1300
1301
1302ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1303{
1304 mConsole = console;
1305
1306 int rc = RTCritSectInit(&mCritSect);
1307 AssertRC(rc);
1308
1309 mcClipboardRefs = 0;
1310 mpfnClipboardCallback = NULL;
1311
1312#ifdef VBOX_WITH_USB
1313 mUSBBackends.pHead = NULL;
1314 mUSBBackends.pTail = NULL;
1315
1316 mUSBBackends.thread = NIL_RTTHREAD;
1317 mUSBBackends.fThreadRunning = false;
1318 mUSBBackends.event = 0;
1319#endif
1320
1321 mhServer = 0;
1322 mServerInterfaceVersion = 0;
1323
1324 m_fGuestWantsAbsolute = false;
1325 m_mousex = 0;
1326 m_mousey = 0;
1327
1328 m_InputSynch.cGuestNumLockAdaptions = 2;
1329 m_InputSynch.cGuestCapsLockAdaptions = 2;
1330
1331 m_InputSynch.fGuestNumLock = false;
1332 m_InputSynch.fGuestCapsLock = false;
1333 m_InputSynch.fGuestScrollLock = false;
1334
1335 m_InputSynch.fClientNumLock = false;
1336 m_InputSynch.fClientCapsLock = false;
1337 m_InputSynch.fClientScrollLock = false;
1338
1339 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1340
1341 {
1342 ComPtr<IEventSource> es;
1343 console->COMGETTER(EventSource)(es.asOutParam());
1344 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1345 aConsoleListener.createObject();
1346 aConsoleListener->init(new VRDPConsoleListener(), this);
1347 mConsoleListener = aConsoleListener;
1348 com::SafeArray <VBoxEventType_T> eventTypes;
1349 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1350 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1351 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1352 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1353 }
1354
1355 mVRDPBindPort = -1;
1356
1357 mAuthLibrary = 0;
1358
1359 mu32AudioInputClientId = 0;
1360
1361 /*
1362 * Optional interfaces.
1363 */
1364 m_fInterfaceImage = false;
1365 memset(&m_interfaceImage, 0, sizeof (m_interfaceImage));
1366 memset(&m_interfaceCallbacksImage, 0, sizeof (m_interfaceCallbacksImage));
1367}
1368
1369ConsoleVRDPServer::~ConsoleVRDPServer()
1370{
1371 Stop();
1372
1373 if (mConsoleListener)
1374 {
1375 ComPtr<IEventSource> es;
1376 mConsole->COMGETTER(EventSource)(es.asOutParam());
1377 es->UnregisterListener(mConsoleListener);
1378 mConsoleListener.setNull();
1379 }
1380
1381 unsigned i;
1382 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1383 {
1384 if (maFramebuffers[i])
1385 {
1386 maFramebuffers[i]->Release();
1387 maFramebuffers[i] = NULL;
1388 }
1389 }
1390
1391 if (RTCritSectIsInitialized(&mCritSect))
1392 {
1393 RTCritSectDelete(&mCritSect);
1394 memset(&mCritSect, 0, sizeof(mCritSect));
1395 }
1396}
1397
1398int ConsoleVRDPServer::Launch(void)
1399{
1400 LogFlowThisFunc(("\n"));
1401
1402 IVRDEServer *server = mConsole->getVRDEServer();
1403 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1404
1405 /*
1406 * Check if VRDE is enabled.
1407 */
1408 BOOL fEnabled;
1409 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1410 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1411 if (!fEnabled)
1412 return VINF_SUCCESS;
1413
1414 /*
1415 * Check that a VRDE extension pack name is set and resolve it into a
1416 * library path.
1417 */
1418 Bstr bstrExtPack;
1419 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1420 if (FAILED(hrc))
1421 return Global::vboxStatusCodeFromCOM(hrc);
1422 if (bstrExtPack.isEmpty())
1423 return VINF_NOT_SUPPORTED;
1424
1425 Utf8Str strExtPack(bstrExtPack);
1426 Utf8Str strVrdeLibrary;
1427 int vrc = VINF_SUCCESS;
1428 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1429 strVrdeLibrary = "VBoxVRDP";
1430 else
1431 {
1432#ifdef VBOX_WITH_EXTPACK
1433 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1434 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1435#else
1436 vrc = VERR_FILE_NOT_FOUND;
1437#endif
1438 }
1439 if (RT_SUCCESS(vrc))
1440 {
1441 /*
1442 * Load the VRDE library and start the server, if it is enabled.
1443 */
1444 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1445 if (RT_SUCCESS(vrc))
1446 {
1447 VRDEENTRYPOINTS_4 *pEntryPoints4;
1448 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1449
1450 if (RT_SUCCESS(vrc))
1451 {
1452 mServerInterfaceVersion = 4;
1453 mEntryPoints = *pEntryPoints4;
1454 mpEntryPoints = &mEntryPoints;
1455 }
1456 else if (vrc == VERR_VERSION_MISMATCH)
1457 {
1458 /* An older version of VRDE is installed, try version 3. */
1459 VRDEENTRYPOINTS_3 *pEntryPoints3;
1460
1461 static VRDECALLBACKS_3 sCallbacks3 =
1462 {
1463 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1464 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1465 ConsoleVRDPServer::VRDPCallbackClientLogon,
1466 ConsoleVRDPServer::VRDPCallbackClientConnect,
1467 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1468 ConsoleVRDPServer::VRDPCallbackIntercept,
1469 ConsoleVRDPServer::VRDPCallbackUSB,
1470 ConsoleVRDPServer::VRDPCallbackClipboard,
1471 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1472 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1473 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1474 ConsoleVRDPServer::VRDPCallbackInput,
1475 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1476 ConsoleVRDPServer::VRDECallbackAudioIn
1477 };
1478
1479 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1480 if (RT_SUCCESS(vrc))
1481 {
1482 mServerInterfaceVersion = 3;
1483 mEntryPoints.header = pEntryPoints3->header;
1484 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1485 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1486 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1487 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1488 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1489 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1490 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1491 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1492 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1493 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1494 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1495 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1496 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1497 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1498 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1499 mEntryPoints.VRDEGetInterface = NULL;
1500 mpEntryPoints = &mEntryPoints;
1501 }
1502 else if (vrc == VERR_VERSION_MISMATCH)
1503 {
1504 /* An older version of VRDE is installed, try version 1. */
1505 VRDEENTRYPOINTS_1 *pEntryPoints1;
1506
1507 static VRDECALLBACKS_1 sCallbacks1 =
1508 {
1509 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1510 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1511 ConsoleVRDPServer::VRDPCallbackClientLogon,
1512 ConsoleVRDPServer::VRDPCallbackClientConnect,
1513 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1514 ConsoleVRDPServer::VRDPCallbackIntercept,
1515 ConsoleVRDPServer::VRDPCallbackUSB,
1516 ConsoleVRDPServer::VRDPCallbackClipboard,
1517 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1518 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1519 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1520 ConsoleVRDPServer::VRDPCallbackInput,
1521 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1522 };
1523
1524 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1525 if (RT_SUCCESS(vrc))
1526 {
1527 mServerInterfaceVersion = 1;
1528 mEntryPoints.header = pEntryPoints1->header;
1529 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1530 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1531 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1532 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1533 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1534 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1535 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1536 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1537 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1538 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1539 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1540 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1541 mEntryPoints.VRDERedirect = NULL;
1542 mEntryPoints.VRDEAudioInOpen = NULL;
1543 mEntryPoints.VRDEAudioInClose = NULL;
1544 mEntryPoints.VRDEGetInterface = NULL;
1545 mpEntryPoints = &mEntryPoints;
1546 }
1547 }
1548 }
1549
1550 if (RT_SUCCESS(vrc))
1551 {
1552 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1553
1554 if (mServerInterfaceVersion >= 4)
1555 {
1556 /* The server supports optional interfaces. */
1557 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1558
1559 /* Image interface. */
1560 m_interfaceImage.header.u64Version = 1;
1561 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1562
1563 m_interfaceCallbacksImage.header.u64Version = 1;
1564 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1565 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1566
1567 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1568 VRDE_IMAGE_INTERFACE_NAME,
1569 &m_interfaceImage.header,
1570 &m_interfaceCallbacksImage.header,
1571 this);
1572 if (RT_SUCCESS(vrc))
1573 {
1574 m_fInterfaceImage = true;
1575 }
1576
1577 /* Since these interfaces are optional, it is always a success here. */
1578 vrc = VINF_SUCCESS;
1579 }
1580#ifdef VBOX_WITH_USB
1581 remoteUSBThreadStart();
1582#endif
1583 }
1584 else
1585 {
1586 if (vrc != VERR_NET_ADDRESS_IN_USE)
1587 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1588 /* Don't unload the lib, because it prevents us trying again or
1589 because there may be other users? */
1590 }
1591 }
1592 }
1593
1594 return vrc;
1595}
1596
1597typedef struct H3DORInstance
1598{
1599 ConsoleVRDPServer *pThis;
1600 HVRDEIMAGE hImageBitmap;
1601 int32_t x;
1602 int32_t y;
1603 uint32_t w;
1604 uint32_t h;
1605 bool fCreated;
1606} H3DORInstance;
1607
1608/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1609 const char *pszFormat)
1610{
1611 LogFlowFunc(("ctx %p\n", pvContext));
1612
1613 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof (H3DORInstance));
1614
1615 if (p)
1616 {
1617 p->pThis = (ConsoleVRDPServer *)pvContext;
1618 p->hImageBitmap = NULL;
1619 p->x = 0;
1620 p->y = 0;
1621 p->w = 0;
1622 p->h = 0;
1623 p->fCreated = false;
1624
1625 /* Host 3D service passes the actual format of data in this redirect instance.
1626 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1627 */
1628 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1629 {
1630 /* Accept it. */
1631 }
1632 else
1633 {
1634 RTMemFree(p);
1635 p = NULL;
1636 }
1637 }
1638
1639 /* Caller check this for NULL. */
1640 *ppvInstance = p;
1641}
1642
1643/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1644 int32_t x, int32_t y, uint32_t w, uint32_t h)
1645{
1646 LogFlowFunc(("ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1647
1648 H3DORInstance *p = (H3DORInstance *)pvInstance;
1649 Assert(p);
1650 Assert(p->pThis);
1651
1652 /* @todo find out what to do if size changes to 0x0 from non zero */
1653 if (w == 0 || h == 0)
1654 {
1655 /* Do nothing. */
1656 return;
1657 }
1658
1659 RTRECT rect;
1660 rect.xLeft = x;
1661 rect.yTop = y;
1662 rect.xRight = x + w;
1663 rect.yBottom = y + h;
1664
1665 if (p->hImageBitmap)
1666 {
1667 /* An image handle has been already created,
1668 * check if it has the same size as the reported geometry.
1669 */
1670 if ( p->x == x
1671 && p->y == y
1672 && p->w == w
1673 && p->h == h)
1674 {
1675 LogFlowFunc(("geometry not changed\n"));
1676 /* Do nothing. Continue using the existing handle. */
1677 }
1678 else
1679 {
1680 int rc = p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1681 if (RT_SUCCESS(rc))
1682 {
1683 p->x = x;
1684 p->y = y;
1685 p->w = w;
1686 p->h = h;
1687 }
1688 else
1689 {
1690 /* The handle must be recreated. Delete existing handle here. */
1691 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1692 p->hImageBitmap = NULL;
1693 }
1694 }
1695 }
1696
1697 if (!p->hImageBitmap)
1698 {
1699 /* Create a new bitmap handle. */
1700 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1701 * Clipping can be done here or in VRDP server.
1702 * If VRDP does clipping, then uScreenId parameter
1703 * is not necessary and coords must be global.
1704 * (have to check which coords are used in opengl service).
1705 * Since all VRDE API uses a ScreenId,
1706 * the clipping must be done here in ConsoleVRDPServer
1707 */
1708 uint32_t fu32CompletionFlags = 0;
1709 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1710 &p->hImageBitmap,
1711 p,
1712 u32ScreenId,
1713 VRDE_IMAGE_F_CREATE_CONTENT_3D
1714 | VRDE_IMAGE_F_CREATE_WINDOW,
1715 &rect,
1716 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1717 NULL,
1718 0,
1719 &fu32CompletionFlags);
1720 if (RT_SUCCESS(rc))
1721 {
1722 p->x = x;
1723 p->y = y;
1724 p->w = w;
1725 p->h = h;
1726
1727 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1728 {
1729 p->fCreated = true;
1730 }
1731 }
1732 else
1733 {
1734 p->hImageBitmap = NULL;
1735 p->w = 0;
1736 p->h = 0;
1737 }
1738 }
1739}
1740
1741/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1742 uint32_t cRects, RTRECT *paRects)
1743{
1744 LogFlowFunc(("ins %p %d\n", pvInstance, cRects));
1745
1746 H3DORInstance *p = (H3DORInstance *)pvInstance;
1747 Assert(p);
1748 Assert(p->pThis);
1749
1750 if (cRects == 0)
1751 {
1752 /* Complete image is visible. */
1753 RTRECT rect;
1754 rect.xLeft = p->x;
1755 rect.yTop = p->y;
1756 rect.xRight = p->x + p->w;
1757 rect.yBottom = p->y + p->h;
1758 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1759 1,
1760 &rect);
1761 }
1762 else
1763 {
1764 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1765 cRects,
1766 paRects);
1767 }
1768}
1769
1770/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1771 void *pvData, uint32_t cbData)
1772{
1773 LogFlowFunc(("ins %p %p %d\n", pvInstance, pvData, cbData));
1774
1775 H3DORInstance *p = (H3DORInstance *)pvInstance;
1776 Assert(p);
1777 Assert(p->pThis);
1778
1779 /* Currently only a topdown BGR0 bitmap format is supported. */
1780 VRDEIMAGEBITMAP image;
1781
1782 image.cWidth = p->w;
1783 image.cHeight = p->h;
1784 image.pvData = pvData;
1785 image.cbData = cbData;
1786 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
1787 image.iScanDelta = -4 * p->w;
1788
1789 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
1790 p->x,
1791 p->y,
1792 p->w,
1793 p->h,
1794 &image,
1795 sizeof(VRDEIMAGEBITMAP));
1796}
1797
1798/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
1799{
1800 LogFlowFunc(("ins %p\n", pvInstance));
1801
1802 H3DORInstance *p = (H3DORInstance *)pvInstance;
1803 Assert(p);
1804 Assert(p->pThis);
1805
1806 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1807
1808 RTMemFree(p);
1809}
1810
1811/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
1812 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
1813{
1814 int rc = VINF_SUCCESS;
1815
1816 if (index == H3DOR_PROP_FORMATS)
1817 {
1818 /* Return a comma separated list of supported formats. */
1819 static const char *pszSupportedFormats = H3DOR_FMT_RGBA_TOPDOWN;
1820 uint32_t cbOut = (uint32_t)strlen(pszSupportedFormats) + 1;
1821 if (cbOut <= cbBuffer)
1822 {
1823 memcpy(pvBuffer, pszSupportedFormats, cbOut);
1824 }
1825 else
1826 {
1827 rc = VERR_BUFFER_OVERFLOW;
1828 }
1829 *pcbOut = cbOut;
1830 }
1831 else
1832 {
1833 rc = VERR_NOT_SUPPORTED;
1834 }
1835
1836 return rc;
1837}
1838
1839void ConsoleVRDPServer::remote3DRedirect(void)
1840{
1841 if (!m_fInterfaceImage)
1842 {
1843 /* No redirect without corresponding interface. */
1844 return;
1845 }
1846
1847 /* Check if 3D redirection has been enabled. */
1848 com::Bstr bstr;
1849 HRESULT hrc = mConsole->getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
1850
1851 if (hrc != S_OK)
1852 {
1853 bstr = "";
1854 }
1855
1856 com::Utf8Str value = bstr;
1857
1858 bool fEnabled = RTStrICmp(value.c_str(), "true") == 0
1859 || RTStrICmp(value.c_str(), "1") == 0;
1860
1861 if (!fEnabled)
1862 {
1863 return;
1864 }
1865
1866 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
1867 H3DOUTPUTREDIRECT outputRedirect =
1868 {
1869 this,
1870 H3DORBegin,
1871 H3DORGeometry,
1872 H3DORVisibleRegion,
1873 H3DORFrame,
1874 H3DOREnd,
1875 H3DORContextProperty
1876 };
1877
1878 VBOXHGCMSVCPARM parm;
1879
1880 parm.type = VBOX_HGCM_SVC_PARM_PTR;
1881 parm.u.pointer.addr = &outputRedirect;
1882 parm.u.pointer.size = sizeof(outputRedirect);
1883
1884 VMMDev *pVMMDev = mConsole->getVMMDev();
1885
1886 if (!pVMMDev)
1887 {
1888 AssertMsgFailed(("remote3DRedirect no vmmdev\n"));
1889 return;
1890 }
1891
1892 int rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL",
1893 SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT,
1894 SHCRGL_CPARMS_SET_OUTPUT_REDIRECT,
1895 &parm);
1896
1897 if (!RT_SUCCESS(rc))
1898 {
1899 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
1900 return;
1901 }
1902
1903 LogRel(("VRDE: Enabled 3D redirect.\n"));
1904
1905 return;
1906}
1907
1908/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
1909 void *pvUser,
1910 HVRDEIMAGE hVideo,
1911 uint32_t u32Id,
1912 void *pvData,
1913 uint32_t cbData)
1914{
1915 LogFlowFunc(("pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
1916 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
1917
1918 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
1919 H3DORInstance *p = (H3DORInstance *)pvUser;
1920 Assert(p);
1921 Assert(p->pThis);
1922 Assert(p->pThis == pServer);
1923
1924 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
1925 {
1926 if (cbData != sizeof(uint32_t))
1927 {
1928 AssertFailed();
1929 return VERR_INVALID_PARAMETER;
1930 }
1931
1932 uint32_t u32StreamId = *(uint32_t *)pvData;
1933 LogFlowFunc(("VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
1934 u32StreamId));
1935
1936 if (u32StreamId != 0)
1937 {
1938 p->fCreated = true; // @todo not needed?
1939 }
1940 else
1941 {
1942 /* The stream has not been created. */
1943 }
1944 }
1945
1946 return VINF_SUCCESS;
1947}
1948
1949void ConsoleVRDPServer::EnableConnections(void)
1950{
1951 if (mpEntryPoints && mhServer)
1952 {
1953 mpEntryPoints->VRDEEnableConnections(mhServer, true);
1954
1955 /* Redirect 3D output if it is enabled. */
1956 remote3DRedirect();
1957 }
1958}
1959
1960void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
1961{
1962 if (mpEntryPoints && mhServer)
1963 {
1964 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
1965 }
1966}
1967
1968void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
1969{
1970 if (mpEntryPoints && mhServer)
1971 {
1972 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
1973 }
1974}
1975
1976void ConsoleVRDPServer::MousePointerHide(void)
1977{
1978 if (mpEntryPoints && mhServer)
1979 {
1980 mpEntryPoints->VRDEHidePointer(mhServer);
1981 }
1982}
1983
1984void ConsoleVRDPServer::Stop(void)
1985{
1986 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1987 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1988 if (mhServer)
1989 {
1990 HVRDESERVER hServer = mhServer;
1991
1992 /* Reset the handle to avoid further calls to the server. */
1993 mhServer = 0;
1994
1995 if (mpEntryPoints && hServer)
1996 {
1997 mpEntryPoints->VRDEDestroy(hServer);
1998 }
1999 }
2000
2001#ifdef VBOX_WITH_USB
2002 remoteUSBThreadStop();
2003#endif /* VBOX_WITH_USB */
2004
2005 mpfnAuthEntry = NULL;
2006 mpfnAuthEntry2 = NULL;
2007 mpfnAuthEntry3 = NULL;
2008
2009 if (mAuthLibrary)
2010 {
2011 RTLdrClose(mAuthLibrary);
2012 mAuthLibrary = 0;
2013 }
2014}
2015
2016/* Worker thread for Remote USB. The thread polls the clients for
2017 * the list of attached USB devices.
2018 * The thread is also responsible for attaching/detaching devices
2019 * to/from the VM.
2020 *
2021 * It is expected that attaching/detaching is not a frequent operation.
2022 *
2023 * The thread is always running when the VRDP server is active.
2024 *
2025 * The thread scans backends and requests the device list every 2 seconds.
2026 *
2027 * When device list is available, the thread calls the Console to process it.
2028 *
2029 */
2030#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
2031
2032#ifdef VBOX_WITH_USB
2033static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
2034{
2035 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
2036
2037 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
2038
2039 pOwner->notifyRemoteUSBThreadRunning(self);
2040
2041 while (pOwner->isRemoteUSBThreadRunning())
2042 {
2043 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2044
2045 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
2046 {
2047 pRemoteUSBBackend->PollRemoteDevices();
2048 }
2049
2050 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
2051
2052 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
2053 }
2054
2055 return VINF_SUCCESS;
2056}
2057
2058void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
2059{
2060 mUSBBackends.thread = thread;
2061 mUSBBackends.fThreadRunning = true;
2062 int rc = RTThreadUserSignal(thread);
2063 AssertRC(rc);
2064}
2065
2066bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
2067{
2068 return mUSBBackends.fThreadRunning;
2069}
2070
2071void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
2072{
2073 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
2074 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
2075 NOREF(rc);
2076}
2077
2078void ConsoleVRDPServer::remoteUSBThreadStart(void)
2079{
2080 int rc = RTSemEventCreate(&mUSBBackends.event);
2081
2082 if (RT_FAILURE(rc))
2083 {
2084 AssertFailed();
2085 mUSBBackends.event = 0;
2086 }
2087
2088 if (RT_SUCCESS(rc))
2089 {
2090 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
2091 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
2092 }
2093
2094 if (RT_FAILURE(rc))
2095 {
2096 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
2097 mUSBBackends.thread = NIL_RTTHREAD;
2098 }
2099 else
2100 {
2101 /* Wait until the thread is ready. */
2102 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
2103 AssertRC(rc);
2104 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
2105 }
2106}
2107
2108void ConsoleVRDPServer::remoteUSBThreadStop(void)
2109{
2110 mUSBBackends.fThreadRunning = false;
2111
2112 if (mUSBBackends.thread != NIL_RTTHREAD)
2113 {
2114 Assert (mUSBBackends.event != 0);
2115
2116 RTSemEventSignal(mUSBBackends.event);
2117
2118 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
2119 AssertRC(rc);
2120
2121 mUSBBackends.thread = NIL_RTTHREAD;
2122 }
2123
2124 if (mUSBBackends.event)
2125 {
2126 RTSemEventDestroy(mUSBBackends.event);
2127 mUSBBackends.event = 0;
2128 }
2129}
2130#endif /* VBOX_WITH_USB */
2131
2132AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
2133 const char *pszUser, const char *pszPassword, const char *pszDomain,
2134 uint32_t u32ClientId)
2135{
2136 AUTHUUID rawuuid;
2137
2138 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2139
2140 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
2141 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
2142
2143 /*
2144 * Called only from VRDP input thread. So thread safety is not required.
2145 */
2146
2147 if (!mAuthLibrary)
2148 {
2149 /* Load the external authentication library. */
2150 Bstr authLibrary;
2151 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
2152
2153 Utf8Str filename = authLibrary;
2154
2155 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
2156
2157 int rc;
2158 if (RTPathHavePath(filename.c_str()))
2159 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
2160 else
2161 {
2162 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
2163 if (RT_FAILURE(rc))
2164 {
2165 /* Backward compatibility with old default 'VRDPAuth' name.
2166 * Try to load new default 'VBoxAuth' instead.
2167 */
2168 if (filename == "VRDPAuth")
2169 {
2170 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library VBoxAuth\n"));
2171 rc = RTLdrLoadAppPriv("VBoxAuth", &mAuthLibrary);
2172 }
2173 }
2174 }
2175
2176 if (RT_FAILURE(rc))
2177 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
2178
2179 if (RT_SUCCESS(rc))
2180 {
2181 typedef struct AuthEntryInfoStruct
2182 {
2183 const char *pszName;
2184 void **ppvAddress;
2185
2186 } AuthEntryInfo;
2187 AuthEntryInfo entries[] =
2188 {
2189 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
2190 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
2191 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
2192 { NULL, NULL }
2193 };
2194
2195 /* Get the entry point. */
2196 AuthEntryInfo *pEntryInfo = &entries[0];
2197 while (pEntryInfo->pszName)
2198 {
2199 *pEntryInfo->ppvAddress = NULL;
2200
2201 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
2202 if (RT_SUCCESS(rc2))
2203 {
2204 /* Found an entry point. */
2205 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
2206 rc = VINF_SUCCESS;
2207 break;
2208 }
2209
2210 if (rc2 != VERR_SYMBOL_NOT_FOUND)
2211 {
2212 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
2213 }
2214 rc = rc2;
2215
2216 pEntryInfo++;
2217 }
2218 }
2219
2220 if (RT_FAILURE(rc))
2221 {
2222 mConsole->setError(E_FAIL,
2223 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
2224 filename.c_str(),
2225 rc);
2226
2227 mpfnAuthEntry = NULL;
2228 mpfnAuthEntry2 = NULL;
2229 mpfnAuthEntry3 = NULL;
2230
2231 if (mAuthLibrary)
2232 {
2233 RTLdrClose(mAuthLibrary);
2234 mAuthLibrary = 0;
2235 }
2236
2237 return AuthResultAccessDenied;
2238 }
2239 }
2240
2241 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2242
2243 AuthResult result = AuthResultAccessDenied;
2244 if (mpfnAuthEntry3)
2245 {
2246 result = mpfnAuthEntry3("vrde", &rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2247 }
2248 else if (mpfnAuthEntry2)
2249 {
2250 result = mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2251 }
2252 else if (mpfnAuthEntry)
2253 {
2254 result = mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
2255 }
2256
2257 switch (result)
2258 {
2259 case AuthResultAccessDenied:
2260 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
2261 break;
2262 case AuthResultAccessGranted:
2263 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
2264 break;
2265 case AuthResultDelegateToGuest:
2266 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
2267 break;
2268 default:
2269 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
2270 result = AuthResultAccessDenied;
2271 }
2272
2273 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
2274
2275 return result;
2276}
2277
2278void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
2279{
2280 AUTHUUID rawuuid;
2281
2282 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2283
2284 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
2285 rawuuid, u32ClientId));
2286
2287 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2288
2289 if (mpfnAuthEntry3)
2290 mpfnAuthEntry3("vrde", &rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2291 else if (mpfnAuthEntry2)
2292 mpfnAuthEntry2(&rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2293}
2294
2295int ConsoleVRDPServer::lockConsoleVRDPServer(void)
2296{
2297 int rc = RTCritSectEnter(&mCritSect);
2298 AssertRC(rc);
2299 return rc;
2300}
2301
2302void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
2303{
2304 RTCritSectLeave(&mCritSect);
2305}
2306
2307DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
2308 uint32_t u32ClientId,
2309 uint32_t u32Function,
2310 uint32_t u32Format,
2311 const void *pvData,
2312 uint32_t cbData)
2313{
2314 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
2315 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
2316
2317 int rc = VINF_SUCCESS;
2318
2319 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
2320
2321 NOREF(u32ClientId);
2322
2323 switch (u32Function)
2324 {
2325 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
2326 {
2327 if (pServer->mpfnClipboardCallback)
2328 {
2329 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
2330 u32Format,
2331 (void *)pvData,
2332 cbData);
2333 }
2334 } break;
2335
2336 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
2337 {
2338 if (pServer->mpfnClipboardCallback)
2339 {
2340 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
2341 u32Format,
2342 (void *)pvData,
2343 cbData);
2344 }
2345 } break;
2346
2347 default:
2348 rc = VERR_NOT_SUPPORTED;
2349 }
2350
2351 return rc;
2352}
2353
2354DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
2355 uint32_t u32Function,
2356 void *pvParms,
2357 uint32_t cbParms)
2358{
2359 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
2360 pvExtension, u32Function, pvParms, cbParms));
2361
2362 int rc = VINF_SUCCESS;
2363
2364 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
2365
2366 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
2367
2368 switch (u32Function)
2369 {
2370 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
2371 {
2372 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
2373 } break;
2374
2375 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
2376 {
2377 /* The guest announces clipboard formats. This must be delivered to all clients. */
2378 if (mpEntryPoints && pServer->mhServer)
2379 {
2380 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2381 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
2382 pParms->u32Format,
2383 NULL,
2384 0,
2385 NULL);
2386 }
2387 } break;
2388
2389 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
2390 {
2391 /* The clipboard service expects that the pvData buffer will be filled
2392 * with clipboard data. The server returns the data from the client that
2393 * announced the requested format most recently.
2394 */
2395 if (mpEntryPoints && pServer->mhServer)
2396 {
2397 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2398 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
2399 pParms->u32Format,
2400 pParms->u.pvData,
2401 pParms->cbData,
2402 &pParms->cbData);
2403 }
2404 } break;
2405
2406 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
2407 {
2408 if (mpEntryPoints && pServer->mhServer)
2409 {
2410 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2411 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
2412 pParms->u32Format,
2413 pParms->u.pvData,
2414 pParms->cbData,
2415 NULL);
2416 }
2417 } break;
2418
2419 default:
2420 rc = VERR_NOT_SUPPORTED;
2421 }
2422
2423 return rc;
2424}
2425
2426void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
2427{
2428 int rc = lockConsoleVRDPServer();
2429
2430 if (RT_SUCCESS(rc))
2431 {
2432 if (mcClipboardRefs == 0)
2433 {
2434 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
2435
2436 if (RT_SUCCESS(rc))
2437 {
2438 mcClipboardRefs++;
2439 }
2440 }
2441
2442 unlockConsoleVRDPServer();
2443 }
2444}
2445
2446void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
2447{
2448 int rc = lockConsoleVRDPServer();
2449
2450 if (RT_SUCCESS(rc))
2451 {
2452 mcClipboardRefs--;
2453
2454 if (mcClipboardRefs == 0)
2455 {
2456 HGCMHostUnregisterServiceExtension(mhClipboard);
2457 }
2458
2459 unlockConsoleVRDPServer();
2460 }
2461}
2462
2463/* That is called on INPUT thread of the VRDP server.
2464 * The ConsoleVRDPServer keeps a list of created backend instances.
2465 */
2466void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
2467{
2468#ifdef VBOX_WITH_USB
2469 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
2470
2471 /* Create a new instance of the USB backend for the new client. */
2472 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
2473
2474 if (pRemoteUSBBackend)
2475 {
2476 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
2477
2478 /* Append the new instance in the list. */
2479 int rc = lockConsoleVRDPServer();
2480
2481 if (RT_SUCCESS(rc))
2482 {
2483 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
2484 if (mUSBBackends.pHead)
2485 {
2486 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
2487 }
2488 else
2489 {
2490 mUSBBackends.pTail = pRemoteUSBBackend;
2491 }
2492
2493 mUSBBackends.pHead = pRemoteUSBBackend;
2494
2495 unlockConsoleVRDPServer();
2496
2497 if (ppvIntercept)
2498 {
2499 *ppvIntercept = pRemoteUSBBackend;
2500 }
2501 }
2502
2503 if (RT_FAILURE(rc))
2504 {
2505 pRemoteUSBBackend->Release();
2506 }
2507 }
2508#endif /* VBOX_WITH_USB */
2509}
2510
2511void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
2512{
2513#ifdef VBOX_WITH_USB
2514 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
2515
2516 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2517
2518 /* Find the instance. */
2519 int rc = lockConsoleVRDPServer();
2520
2521 if (RT_SUCCESS(rc))
2522 {
2523 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2524
2525 if (pRemoteUSBBackend)
2526 {
2527 /* Notify that it will be deleted. */
2528 pRemoteUSBBackend->NotifyDelete();
2529 }
2530
2531 unlockConsoleVRDPServer();
2532 }
2533
2534 if (pRemoteUSBBackend)
2535 {
2536 /* Here the instance has been excluded from the list and can be dereferenced. */
2537 pRemoteUSBBackend->Release();
2538 }
2539#endif
2540}
2541
2542void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
2543{
2544#ifdef VBOX_WITH_USB
2545 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2546
2547 /* Find the instance. */
2548 int rc = lockConsoleVRDPServer();
2549
2550 if (RT_SUCCESS(rc))
2551 {
2552 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2553
2554 if (pRemoteUSBBackend)
2555 {
2556 /* Inform the backend instance that it is referenced by the Guid. */
2557 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
2558
2559 if (fAdded)
2560 {
2561 /* Reference the instance because its pointer is being taken. */
2562 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
2563 }
2564 else
2565 {
2566 pRemoteUSBBackend = NULL;
2567 }
2568 }
2569
2570 unlockConsoleVRDPServer();
2571 }
2572
2573 if (pRemoteUSBBackend)
2574 {
2575 return pRemoteUSBBackend->GetBackendCallbackPointer();
2576 }
2577
2578#endif
2579 return NULL;
2580}
2581
2582void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
2583{
2584#ifdef VBOX_WITH_USB
2585 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2586
2587 /* Find the instance. */
2588 int rc = lockConsoleVRDPServer();
2589
2590 if (RT_SUCCESS(rc))
2591 {
2592 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
2593
2594 if (pRemoteUSBBackend)
2595 {
2596 pRemoteUSBBackend->removeUUID(pGuid);
2597 }
2598
2599 unlockConsoleVRDPServer();
2600
2601 if (pRemoteUSBBackend)
2602 {
2603 pRemoteUSBBackend->Release();
2604 }
2605 }
2606#endif
2607}
2608
2609RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
2610{
2611 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
2612
2613 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
2614#ifdef VBOX_WITH_USB
2615
2616 int rc = lockConsoleVRDPServer();
2617
2618 if (RT_SUCCESS(rc))
2619 {
2620 if (pRemoteUSBBackend == NULL)
2621 {
2622 /* The first backend in the list is requested. */
2623 pNextRemoteUSBBackend = mUSBBackends.pHead;
2624 }
2625 else
2626 {
2627 /* Get pointer to the next backend. */
2628 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2629 }
2630
2631 if (pNextRemoteUSBBackend)
2632 {
2633 pNextRemoteUSBBackend->AddRef();
2634 }
2635
2636 unlockConsoleVRDPServer();
2637
2638 if (pRemoteUSBBackend)
2639 {
2640 pRemoteUSBBackend->Release();
2641 }
2642 }
2643#endif
2644
2645 return pNextRemoteUSBBackend;
2646}
2647
2648#ifdef VBOX_WITH_USB
2649/* Internal method. Called under the ConsoleVRDPServerLock. */
2650RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
2651{
2652 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2653
2654 while (pRemoteUSBBackend)
2655 {
2656 if (pRemoteUSBBackend->ClientId() == u32ClientId)
2657 {
2658 break;
2659 }
2660
2661 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2662 }
2663
2664 return pRemoteUSBBackend;
2665}
2666
2667/* Internal method. Called under the ConsoleVRDPServerLock. */
2668RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
2669{
2670 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2671
2672 while (pRemoteUSBBackend)
2673 {
2674 if (pRemoteUSBBackend->findUUID(pGuid))
2675 {
2676 break;
2677 }
2678
2679 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2680 }
2681
2682 return pRemoteUSBBackend;
2683}
2684#endif
2685
2686/* Internal method. Called by the backend destructor. */
2687void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2688{
2689#ifdef VBOX_WITH_USB
2690 int rc = lockConsoleVRDPServer();
2691 AssertRC(rc);
2692
2693 /* Exclude the found instance from the list. */
2694 if (pRemoteUSBBackend->pNext)
2695 {
2696 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2697 }
2698 else
2699 {
2700 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2701 }
2702
2703 if (pRemoteUSBBackend->pPrev)
2704 {
2705 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2706 }
2707 else
2708 {
2709 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2710 }
2711
2712 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2713
2714 unlockConsoleVRDPServer();
2715#endif
2716}
2717
2718
2719void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2720{
2721 if (mpEntryPoints && mhServer)
2722 {
2723 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2724 }
2725}
2726
2727void ConsoleVRDPServer::SendResize(void) const
2728{
2729 if (mpEntryPoints && mhServer)
2730 {
2731 mpEntryPoints->VRDEResize(mhServer);
2732 }
2733}
2734
2735void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2736{
2737 VRDEORDERHDR update;
2738 update.x = x;
2739 update.y = y;
2740 update.w = w;
2741 update.h = h;
2742 if (mpEntryPoints && mhServer)
2743 {
2744 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
2745 }
2746}
2747
2748void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
2749{
2750 if (mpEntryPoints && mhServer)
2751 {
2752 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
2753 }
2754}
2755
2756void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2757{
2758 if (mpEntryPoints && mhServer)
2759 {
2760 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
2761 }
2762}
2763
2764void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2765{
2766 if (mpEntryPoints && mhServer)
2767 {
2768 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2769 }
2770}
2771
2772/* @todo rc not needed? */
2773int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
2774 void *pvContext,
2775 uint32_t cSamples,
2776 uint32_t iSampleHz,
2777 uint32_t cChannels,
2778 uint32_t cBits)
2779{
2780 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
2781 {
2782 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2783 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2784 {
2785 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
2786 mpEntryPoints->VRDEAudioInOpen (mhServer,
2787 pvContext,
2788 u32ClientId,
2789 audioFormat,
2790 cSamples);
2791 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
2792 * Currently not used because only one client is allowed to
2793 * do audio input and the client id is saved by the ConsoleVRDPServer.
2794 */
2795
2796 return VINF_SUCCESS;
2797 }
2798 }
2799 return VERR_NOT_SUPPORTED;
2800}
2801
2802void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
2803{
2804 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
2805 {
2806 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2807 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2808 {
2809 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
2810 }
2811 }
2812}
2813
2814#ifdef VBOX_WITH_USB_VIDEO
2815int ConsoleVRDPServer::GetVideoFrameDimensions(uint16_t *pu16Heigh, uint16_t *pu16Width)
2816{
2817 *pu16Heigh = 640;
2818 *pu16Width = 480;
2819 return VINF_SUCCESS;
2820}
2821
2822int ConsoleVRDPServer::SendVideoSreamOn(bool fFetch)
2823{
2824 /* Here we inform server that guest is starting/stopping
2825 * the stream
2826 */
2827 return VINF_SUCCESS;
2828}
2829#endif
2830
2831
2832
2833void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2834{
2835 if (index == VRDE_QI_PORT)
2836 {
2837 uint32_t cbOut = sizeof(int32_t);
2838
2839 if (cbBuffer >= cbOut)
2840 {
2841 *pcbOut = cbOut;
2842 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2843 }
2844 }
2845 else if (mpEntryPoints && mhServer)
2846 {
2847 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2848 }
2849}
2850
2851/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
2852{
2853 int rc = VINF_SUCCESS;
2854
2855 if (mVRDPLibrary == NIL_RTLDRMOD)
2856 {
2857 RTERRINFOSTATIC ErrInfo;
2858 RTErrInfoInitStatic(&ErrInfo);
2859
2860 if (RTPathHavePath(pszLibraryName))
2861 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
2862 else
2863 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
2864 if (RT_SUCCESS(rc))
2865 {
2866 struct SymbolEntry
2867 {
2868 const char *name;
2869 void **ppfn;
2870 };
2871
2872 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2873
2874 static const struct SymbolEntry s_aSymbols[] =
2875 {
2876 DEFSYMENTRY(VRDECreateServer)
2877 };
2878
2879 #undef DEFSYMENTRY
2880
2881 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
2882 {
2883 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
2884
2885 if (RT_FAILURE(rc))
2886 {
2887 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
2888 break;
2889 }
2890 }
2891 }
2892 else
2893 {
2894 if (RTErrInfoIsSet(&ErrInfo.Core))
2895 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
2896 else
2897 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
2898
2899 mVRDPLibrary = NIL_RTLDRMOD;
2900 }
2901 }
2902
2903 if (RT_FAILURE(rc))
2904 {
2905 if (mVRDPLibrary != NIL_RTLDRMOD)
2906 {
2907 RTLdrClose(mVRDPLibrary);
2908 mVRDPLibrary = NIL_RTLDRMOD;
2909 }
2910 }
2911
2912 return rc;
2913}
2914
2915/*
2916 * IVRDEServerInfo implementation.
2917 */
2918// constructor / destructor
2919/////////////////////////////////////////////////////////////////////////////
2920
2921VRDEServerInfo::VRDEServerInfo()
2922 : mParent(NULL)
2923{
2924}
2925
2926VRDEServerInfo::~VRDEServerInfo()
2927{
2928}
2929
2930
2931HRESULT VRDEServerInfo::FinalConstruct()
2932{
2933 return BaseFinalConstruct();
2934}
2935
2936void VRDEServerInfo::FinalRelease()
2937{
2938 uninit();
2939 BaseFinalRelease();
2940}
2941
2942// public methods only for internal purposes
2943/////////////////////////////////////////////////////////////////////////////
2944
2945/**
2946 * Initializes the guest object.
2947 */
2948HRESULT VRDEServerInfo::init(Console *aParent)
2949{
2950 LogFlowThisFunc(("aParent=%p\n", aParent));
2951
2952 ComAssertRet(aParent, E_INVALIDARG);
2953
2954 /* Enclose the state transition NotReady->InInit->Ready */
2955 AutoInitSpan autoInitSpan(this);
2956 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2957
2958 unconst(mParent) = aParent;
2959
2960 /* Confirm a successful initialization */
2961 autoInitSpan.setSucceeded();
2962
2963 return S_OK;
2964}
2965
2966/**
2967 * Uninitializes the instance and sets the ready flag to FALSE.
2968 * Called either from FinalRelease() or by the parent when it gets destroyed.
2969 */
2970void VRDEServerInfo::uninit()
2971{
2972 LogFlowThisFunc(("\n"));
2973
2974 /* Enclose the state transition Ready->InUninit->NotReady */
2975 AutoUninitSpan autoUninitSpan(this);
2976 if (autoUninitSpan.uninitDone())
2977 return;
2978
2979 unconst(mParent) = NULL;
2980}
2981
2982// IVRDEServerInfo properties
2983/////////////////////////////////////////////////////////////////////////////
2984
2985#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2986 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2987 { \
2988 if (!a##_aName) \
2989 return E_POINTER; \
2990 \
2991 AutoCaller autoCaller(this); \
2992 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2993 \
2994 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2995 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2996 \
2997 uint32_t value; \
2998 uint32_t cbOut = 0; \
2999 \
3000 mParent->consoleVRDPServer()->QueryInfo \
3001 (_aIndex, &value, sizeof(value), &cbOut); \
3002 \
3003 *a##_aName = cbOut? !!value: FALSE; \
3004 \
3005 return S_OK; \
3006 } \
3007 extern void IMPL_GETTER_BOOL_DUMMY(void)
3008
3009#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
3010 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3011 { \
3012 if (!a##_aName) \
3013 return E_POINTER; \
3014 \
3015 AutoCaller autoCaller(this); \
3016 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3017 \
3018 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3019 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3020 \
3021 _aType value; \
3022 uint32_t cbOut = 0; \
3023 \
3024 mParent->consoleVRDPServer()->QueryInfo \
3025 (_aIndex, &value, sizeof(value), &cbOut); \
3026 \
3027 if (_aValueMask) value &= (_aValueMask); \
3028 *a##_aName = cbOut? value: 0; \
3029 \
3030 return S_OK; \
3031 } \
3032 extern void IMPL_GETTER_SCALAR_DUMMY(void)
3033
3034#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
3035 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3036 { \
3037 if (!a##_aName) \
3038 return E_POINTER; \
3039 \
3040 AutoCaller autoCaller(this); \
3041 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3042 \
3043 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3044 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3045 \
3046 uint32_t cbOut = 0; \
3047 \
3048 mParent->consoleVRDPServer()->QueryInfo \
3049 (_aIndex, NULL, 0, &cbOut); \
3050 \
3051 if (cbOut == 0) \
3052 { \
3053 Bstr str(""); \
3054 str.cloneTo(a##_aName); \
3055 return S_OK; \
3056 } \
3057 \
3058 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
3059 \
3060 if (!pchBuffer) \
3061 { \
3062 Log(("VRDEServerInfo::" \
3063 #_aName \
3064 ": Failed to allocate memory %d bytes\n", cbOut)); \
3065 return E_OUTOFMEMORY; \
3066 } \
3067 \
3068 mParent->consoleVRDPServer()->QueryInfo \
3069 (_aIndex, pchBuffer, cbOut, &cbOut); \
3070 \
3071 Bstr str(pchBuffer); \
3072 \
3073 str.cloneTo(a##_aName); \
3074 \
3075 RTMemTmpFree(pchBuffer); \
3076 \
3077 return S_OK; \
3078 } \
3079 extern void IMPL_GETTER_BSTR_DUMMY(void)
3080
3081IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
3082IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
3083IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
3084IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
3085IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
3086IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
3087IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
3088IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
3089IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
3090IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
3091IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
3092IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
3093IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
3094IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
3095IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
3096
3097#undef IMPL_GETTER_BSTR
3098#undef IMPL_GETTER_SCALAR
3099#undef IMPL_GETTER_BOOL
3100/* 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