VirtualBox

source: vbox/trunk/src/VBox/Main/ConsoleVRDPServer.cpp@ 33451

Last change on this file since 33451 was 33386, checked in by vboxsync, 14 years ago

VRDE: API changes for the VRDP server separation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 73.3 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 33386 2010-10-24 15:57:55Z 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
24#include "AutoCaller.h"
25#include "Logging.h"
26
27#include <iprt/asm.h>
28#include <iprt/ldr.h>
29#include <iprt/param.h>
30#include <iprt/path.h>
31#include <iprt/alloca.h>
32#include <iprt/cpp/utils.h>
33
34#include <VBox/err.h>
35#ifdef VBOX_WITH_VRDP
36#include <VBox/RemoteDesktop/VRDEOrders.h>
37#endif /* VBOX_WITH_VRDP */
38
39class VRDPConsoleListener :
40 VBOX_SCRIPTABLE_IMPL(IEventListener)
41{
42public:
43 VRDPConsoleListener(ConsoleVRDPServer *server)
44 : m_server(server)
45 {
46#ifndef VBOX_WITH_XPCOM
47 refcnt = 0;
48#endif /* !VBOX_WITH_XPCOM */
49 }
50
51 virtual ~VRDPConsoleListener() {}
52
53 NS_DECL_ISUPPORTS
54
55#ifndef VBOX_WITH_XPCOM
56 STDMETHOD_(ULONG, AddRef)() {
57 return ::InterlockedIncrement(&refcnt);
58 }
59 STDMETHOD_(ULONG, Release)()
60 {
61 long cnt = ::InterlockedDecrement(&refcnt);
62 if (cnt == 0)
63 delete this;
64 return cnt;
65 }
66 STDMETHOD(QueryInterface)(REFIID riid , void **ppObj)
67 {
68 if (riid == IID_IUnknown) {
69 *ppObj = (IUnknown*)this;
70 AddRef();
71 return S_OK;
72 }
73 if (riid == IID_IEventListener) {
74 *ppObj = (IEventListener*)this;
75 AddRef();
76 return S_OK;
77 }
78 *ppObj = NULL;
79 return E_NOINTERFACE;
80 }
81#endif /* !VBOX_WITH_XPCOM */
82
83
84 STDMETHOD(HandleEvent)(IEvent * aEvent)
85 {
86 VBoxEventType_T aType = VBoxEventType_Invalid;
87
88 aEvent->COMGETTER(Type)(&aType);
89 switch (aType)
90 {
91 case VBoxEventType_OnMousePointerShapeChanged:
92 {
93 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
94 Assert(mpscev);
95 BOOL visible, alpha;
96 ULONG xHot, yHot, width, height;
97 com::SafeArray <BYTE> shape;
98
99 mpscev->COMGETTER(Visible)(&visible);
100 mpscev->COMGETTER(Alpha)(&alpha);
101 mpscev->COMGETTER(Xhot)(&xHot);
102 mpscev->COMGETTER(Yhot)(&yHot);
103 mpscev->COMGETTER(Width)(&width);
104 mpscev->COMGETTER(Height)(&height);
105 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
106
107 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
108 break;
109 }
110 case VBoxEventType_OnMouseCapabilityChanged:
111 {
112 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
113 Assert(mccev);
114 if (m_server)
115 {
116 BOOL fAbsoluteMouse;
117 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
118 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
119 }
120 break;
121 }
122 case VBoxEventType_OnKeyboardLedsChanged:
123 {
124 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
125 Assert(klcev);
126
127 if (m_server)
128 {
129 BOOL fNumLock, fCapsLock, fScrollLock;
130 klcev->COMGETTER(NumLock)(&fNumLock);
131 klcev->COMGETTER(CapsLock)(&fCapsLock);
132 klcev->COMGETTER(ScrollLock)(&fScrollLock);
133 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
134 }
135 break;
136 }
137
138 default:
139 AssertFailed();
140 }
141
142 return S_OK;
143 }
144
145private:
146 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
147 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
148 ConsoleVRDPServer *m_server;
149#ifndef VBOX_WITH_XPCOM
150 long refcnt;
151#endif /* !VBOX_WITH_XPCOM */
152};
153
154#ifdef VBOX_WITH_XPCOM
155#include <nsMemory.h>
156NS_DECL_CLASSINFO(VRDPConsoleListener)
157NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VRDPConsoleListener, IEventListener)
158#endif /* VBOX_WITH_XPCOM */
159
160#ifdef DEBUG_sunlover
161#define LOGDUMPPTR Log
162void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
163{
164 unsigned i;
165
166 const uint8_t *pu8And = pu8Shape;
167
168 for (i = 0; i < height; i++)
169 {
170 unsigned j;
171 LOGDUMPPTR(("%p: ", pu8And));
172 for (j = 0; j < (width + 7) / 8; j++)
173 {
174 unsigned k;
175 for (k = 0; k < 8; k++)
176 {
177 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
178 }
179
180 pu8And++;
181 }
182 LOGDUMPPTR(("\n"));
183 }
184
185 if (fXorMaskRGB32)
186 {
187 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
188
189 for (i = 0; i < height; i++)
190 {
191 unsigned j;
192 LOGDUMPPTR(("%p: ", pu32Xor));
193 for (j = 0; j < width; j++)
194 {
195 LOGDUMPPTR(("%08X", *pu32Xor++));
196 }
197 LOGDUMPPTR(("\n"));
198 }
199 }
200 else
201 {
202 /* RDP 24 bit RGB mask. */
203 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
204 for (i = 0; i < height; i++)
205 {
206 unsigned j;
207 LOGDUMPPTR(("%p: ", pu8Xor));
208 for (j = 0; j < width; j++)
209 {
210 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
211 pu8Xor += 3;
212 }
213 LOGDUMPPTR(("\n"));
214 }
215 }
216}
217#else
218#define dumpPointer(a, b, c, d) do {} while (0)
219#endif /* DEBUG_sunlover */
220
221static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
222{
223 /*
224 * Find the top border of the AND mask. First assign to special value.
225 */
226 uint32_t ySkipAnd = ~0;
227
228 const uint8_t *pu8And = pu8AndMask;
229 const uint32_t cbAndRow = (width + 7) / 8;
230 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
231
232 Assert(cbAndRow > 0);
233
234 unsigned y;
235 unsigned x;
236
237 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
238 {
239 /* For each complete byte in the row. */
240 for (x = 0; x < cbAndRow - 1; x++)
241 {
242 if (pu8And[x] != 0xFF)
243 {
244 ySkipAnd = y;
245 break;
246 }
247 }
248
249 if (ySkipAnd == ~(uint32_t)0)
250 {
251 /* Last byte. */
252 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
253 {
254 ySkipAnd = y;
255 }
256 }
257 }
258
259 if (ySkipAnd == ~(uint32_t)0)
260 {
261 ySkipAnd = 0;
262 }
263
264 /*
265 * Find the left border of the AND mask.
266 */
267 uint32_t xSkipAnd = ~0;
268
269 /* For all bit columns. */
270 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
271 {
272 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
273 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
274
275 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
276 {
277 if ((*pu8And & mask) == 0)
278 {
279 xSkipAnd = x;
280 break;
281 }
282 }
283 }
284
285 if (xSkipAnd == ~(uint32_t)0)
286 {
287 xSkipAnd = 0;
288 }
289
290 /*
291 * Find the XOR mask top border.
292 */
293 uint32_t ySkipXor = ~0;
294
295 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
296
297 uint32_t *pu32Xor = pu32XorStart;
298
299 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
300 {
301 for (x = 0; x < width; x++)
302 {
303 if (pu32Xor[x] != 0)
304 {
305 ySkipXor = y;
306 break;
307 }
308 }
309 }
310
311 if (ySkipXor == ~(uint32_t)0)
312 {
313 ySkipXor = 0;
314 }
315
316 /*
317 * Find the left border of the XOR mask.
318 */
319 uint32_t xSkipXor = ~(uint32_t)0;
320
321 /* For all columns. */
322 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
323 {
324 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
325
326 for (y = ySkipXor; y < height; y++, pu32Xor += width)
327 {
328 if (*pu32Xor != 0)
329 {
330 xSkipXor = x;
331 break;
332 }
333 }
334 }
335
336 if (xSkipXor == ~(uint32_t)0)
337 {
338 xSkipXor = 0;
339 }
340
341 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
342 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
343}
344
345/* Generate an AND mask for alpha pointers here, because
346 * guest driver does not do that correctly for Vista pointers.
347 * Similar fix, changing the alpha threshold, could be applied
348 * for the guest driver, but then additions reinstall would be
349 * necessary, which we try to avoid.
350 */
351static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
352{
353 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
354
355 int y;
356 for (y = 0; y < h; y++)
357 {
358 uint8_t bitmask = 0x80;
359
360 int x;
361 for (x = 0; x < w; x++, bitmask >>= 1)
362 {
363 if (bitmask == 0)
364 {
365 bitmask = 0x80;
366 }
367
368 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
369 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
370 {
371 pu8DstAndMask[x / 8] &= ~bitmask;
372 }
373 }
374
375 /* Point to next source and dest scans. */
376 pu8SrcAlpha += w * 4;
377 pu8DstAndMask += (w + 7) / 8;
378 }
379}
380
381STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
382 BOOL alpha,
383 ULONG xHot,
384 ULONG yHot,
385 ULONG width,
386 ULONG height,
387 ComSafeArrayIn(BYTE,inShape))
388{
389 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
390
391 if (m_server)
392 {
393 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
394 if (aShape.size() == 0)
395 {
396 if (!visible)
397 {
398 m_server->MousePointerHide();
399 }
400 }
401 else if (width != 0 && height != 0)
402 {
403 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
404 * 'shape' AND mask followed by XOR mask.
405 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
406 *
407 * We convert this to RDP color format which consist of
408 * one bpp AND mask and 24 BPP (BGR) color XOR image.
409 *
410 * RDP clients expect 8 aligned width and height of
411 * pointer (preferably 32x32).
412 *
413 * They even contain bugs which do not appear for
414 * 32x32 pointers but would appear for a 41x32 one.
415 *
416 * So set pointer size to 32x32. This can be done safely
417 * because most pointers are 32x32.
418 */
419 uint8_t* shape = aShape.raw();
420
421 dumpPointer(shape, width, height, true);
422
423 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
424
425 uint8_t *pu8AndMask = shape;
426 uint8_t *pu8XorMask = shape + cbDstAndMask;
427
428 if (alpha)
429 {
430 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
431
432 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
433 }
434
435 /* Windows guest alpha pointers are wider than 32 pixels.
436 * Try to find out the top-left border of the pointer and
437 * then copy only meaningful bits. All complete top rows
438 * and all complete left columns where (AND == 1 && XOR == 0)
439 * are skipped. Hot spot is adjusted.
440 */
441 uint32_t ySkip = 0; /* How many rows to skip at the top. */
442 uint32_t xSkip = 0; /* How many columns to skip at the left. */
443
444 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
445
446 /* Must not skip the hot spot. */
447 xSkip = RT_MIN(xSkip, xHot);
448 ySkip = RT_MIN(ySkip, yHot);
449
450 /*
451 * Compute size and allocate memory for the pointer.
452 */
453 const uint32_t dstwidth = 32;
454 const uint32_t dstheight = 32;
455
456 VRDECOLORPOINTER *pointer = NULL;
457
458 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
459
460 uint32_t rdpmaskwidth = dstmaskwidth;
461 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
462
463 uint32_t rdpdatawidth = dstwidth * 3;
464 uint32_t rdpdatalen = dstheight * rdpdatawidth;
465
466 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
467
468 if (pointer)
469 {
470 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
471 uint8_t *dataarray = maskarray + rdpmasklen;
472
473 memset(maskarray, 0xFF, rdpmasklen);
474 memset(dataarray, 0x00, rdpdatalen);
475
476 uint32_t srcmaskwidth = (width + 7) / 8;
477 uint32_t srcdatawidth = width * 4;
478
479 /* Copy AND mask. */
480 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
481 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
482
483 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
484 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
485
486 unsigned x, y;
487
488 for (y = 0; y < minheight; y++)
489 {
490 for (x = 0; x < minwidth; x++)
491 {
492 uint32_t byteIndex = (x + xSkip) / 8;
493 uint32_t bitIndex = (x + xSkip) % 8;
494
495 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
496
497 if (!bit)
498 {
499 byteIndex = x / 8;
500 bitIndex = x % 8;
501
502 dst[byteIndex] &= ~(1 << (7 - bitIndex));
503 }
504 }
505
506 src += srcmaskwidth;
507 dst -= rdpmaskwidth;
508 }
509
510 /* Point src to XOR mask */
511 src = pu8XorMask + ySkip * srcdatawidth;
512 dst = dataarray + (dstheight - 1) * rdpdatawidth;
513
514 for (y = 0; y < minheight ; y++)
515 {
516 for (x = 0; x < minwidth; x++)
517 {
518 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
519 }
520
521 src += srcdatawidth;
522 dst -= rdpdatawidth;
523 }
524
525 pointer->u16HotX = (uint16_t)(xHot - xSkip);
526 pointer->u16HotY = (uint16_t)(yHot - ySkip);
527
528 pointer->u16Width = (uint16_t)dstwidth;
529 pointer->u16Height = (uint16_t)dstheight;
530
531 pointer->u16MaskLen = (uint16_t)rdpmasklen;
532 pointer->u16DataLen = (uint16_t)rdpdatalen;
533
534 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
535
536 m_server->MousePointerUpdate(pointer);
537
538 RTMemTmpFree(pointer);
539 }
540 }
541 }
542
543 return S_OK;
544}
545
546
547// ConsoleVRDPServer
548////////////////////////////////////////////////////////////////////////////////
549
550#ifdef VBOX_WITH_VRDP
551RTLDRMOD ConsoleVRDPServer::mVRDPLibrary;
552
553PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
554
555VRDEENTRYPOINTS_1 *ConsoleVRDPServer::mpEntryPoints = NULL;
556
557VRDECALLBACKS_1 ConsoleVRDPServer::mCallbacks =
558{
559 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
560 ConsoleVRDPServer::VRDPCallbackQueryProperty,
561 ConsoleVRDPServer::VRDPCallbackClientLogon,
562 ConsoleVRDPServer::VRDPCallbackClientConnect,
563 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
564 ConsoleVRDPServer::VRDPCallbackIntercept,
565 ConsoleVRDPServer::VRDPCallbackUSB,
566 ConsoleVRDPServer::VRDPCallbackClipboard,
567 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
568 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
569 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
570 ConsoleVRDPServer::VRDPCallbackInput,
571 ConsoleVRDPServer::VRDPCallbackVideoModeHint
572};
573
574DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
575{
576 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
577
578 int rc = VERR_NOT_SUPPORTED;
579
580 switch (index)
581 {
582 case VRDE_QP_NETWORK_PORT:
583 {
584 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
585 ULONG port = 0;
586
587 if (cbBuffer >= sizeof(uint32_t))
588 {
589 *(uint32_t *)pvBuffer = (uint32_t)port;
590 rc = VINF_SUCCESS;
591 }
592 else
593 {
594 rc = VINF_BUFFER_OVERFLOW;
595 }
596
597 *pcbOut = sizeof(uint32_t);
598 } break;
599
600 case VRDE_QP_NETWORK_ADDRESS:
601 {
602 com::Bstr bstr;
603 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
604
605 /* The server expects UTF8. */
606 com::Utf8Str address = bstr;
607
608 size_t cbAddress = address.length() + 1;
609
610 if (cbAddress >= 0x10000)
611 {
612 /* More than 64K seems to be an invalid address. */
613 rc = VERR_TOO_MUCH_DATA;
614 break;
615 }
616
617 if ((size_t)cbBuffer >= cbAddress)
618 {
619 memcpy(pvBuffer, address.c_str(), cbAddress);
620 rc = VINF_SUCCESS;
621 }
622 else
623 {
624 rc = VINF_BUFFER_OVERFLOW;
625 }
626
627 *pcbOut = (uint32_t)cbAddress;
628 } break;
629
630 case VRDE_QP_NUMBER_MONITORS:
631 {
632 ULONG cMonitors = 1;
633
634 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
635
636 if (cbBuffer >= sizeof(uint32_t))
637 {
638 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
639 rc = VINF_SUCCESS;
640 }
641 else
642 {
643 rc = VINF_BUFFER_OVERFLOW;
644 }
645
646 *pcbOut = sizeof(uint32_t);
647 } break;
648
649 case VRDE_QP_NETWORK_PORT_RANGE:
650 {
651 com::Bstr bstr;
652 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
653
654 if (hrc != S_OK)
655 {
656 bstr = "";
657 }
658
659 if (bstr == "0")
660 {
661 bstr = "3389";
662 }
663
664 /* The server expects UTF8. */
665 com::Utf8Str portRange = bstr;
666
667 size_t cbPortRange = portRange.length() + 1;
668
669 if (cbPortRange >= 0x10000)
670 {
671 /* More than 64K seems to be an invalid port range string. */
672 rc = VERR_TOO_MUCH_DATA;
673 break;
674 }
675
676 if ((size_t)cbBuffer >= cbPortRange)
677 {
678 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
679 rc = VINF_SUCCESS;
680 }
681 else
682 {
683 rc = VINF_BUFFER_OVERFLOW;
684 }
685
686 *pcbOut = (uint32_t)cbPortRange;
687 } break;
688
689#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
690 case VRDE_QP_VIDEO_CHANNEL:
691 {
692 BOOL fVideoEnabled = FALSE;
693
694 server->mConsole->getVRDEServer()->COMGETTER(VideoChannel)(&fVideoEnabled);
695
696 if (cbBuffer >= sizeof(uint32_t))
697 {
698 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
699 rc = VINF_SUCCESS;
700 }
701 else
702 {
703 rc = VINF_BUFFER_OVERFLOW;
704 }
705
706 *pcbOut = sizeof(uint32_t);
707 } break;
708
709 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
710 {
711 ULONG ulQuality = 0;
712
713 server->mConsole->getVRDEServer()->COMGETTER(VideoChannelQuality)(&ulQuality);
714
715 if (cbBuffer >= sizeof(uint32_t))
716 {
717 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
718 rc = VINF_SUCCESS;
719 }
720 else
721 {
722 rc = VINF_BUFFER_OVERFLOW;
723 }
724
725 *pcbOut = sizeof(uint32_t);
726 } break;
727
728 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
729 {
730 ULONG ulSunFlsh = 1;
731
732 com::Bstr bstr;
733 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
734 bstr.asOutParam());
735 if (hrc == S_OK && !bstr.isEmpty())
736 {
737 com::Utf8Str sunFlsh = bstr;
738 if (!sunFlsh.isEmpty())
739 {
740 ulSunFlsh = sunFlsh.toUInt32();
741 }
742 }
743
744 if (cbBuffer >= sizeof(uint32_t))
745 {
746 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
747 rc = VINF_SUCCESS;
748 }
749 else
750 {
751 rc = VINF_BUFFER_OVERFLOW;
752 }
753
754 *pcbOut = sizeof(uint32_t);
755 } break;
756#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
757
758 case VRDE_QP_FEATURE:
759 {
760 if (cbBuffer < sizeof(VRDEFEATURE))
761 {
762 rc = VERR_INVALID_PARAMETER;
763 break;
764 }
765
766 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
767
768 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
769
770 size_t cchInfo = 0;
771 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
772
773 if (RT_FAILURE(rc))
774 {
775 rc = VERR_INVALID_PARAMETER;
776 break;
777 }
778
779 /* features are mapped to "VRDP/Feature/NAME" extra data. */
780 com::Utf8Str extraData("VRDP/Feature/");
781 extraData += pFeature->achInfo;
782
783 com::Bstr bstrValue;
784
785 /* @todo these features should be per client. */
786 NOREF(pFeature->u32ClientId);
787
788 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
789 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
790 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
791 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
792 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
793 )
794 {
795 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
796 bstrValue.asOutParam());
797 if (hrc == S_OK && !bstrValue.isEmpty())
798 {
799 rc = VINF_SUCCESS;
800 }
801 }
802 else
803 {
804 rc = VERR_NOT_SUPPORTED;
805 }
806
807 /* Copy the value string to the callers buffer. */
808 if (rc == VINF_SUCCESS)
809 {
810 com::Utf8Str value = bstrValue;
811
812 size_t cb = value.length() + 1;
813
814 if ((size_t)cbInfo >= cb)
815 {
816 memcpy(pFeature->achInfo, value.c_str(), cb);
817 }
818 else
819 {
820 rc = VINF_BUFFER_OVERFLOW;
821 }
822
823 *pcbOut = (uint32_t)cb;
824 }
825 } break;
826
827 case VRDE_SP_NETWORK_BIND_PORT:
828 {
829 if (cbBuffer != sizeof(uint32_t))
830 {
831 rc = VERR_INVALID_PARAMETER;
832 break;
833 }
834
835 ULONG port = *(uint32_t *)pvBuffer;
836
837 server->mVRDPBindPort = port;
838
839 rc = VINF_SUCCESS;
840
841 if (pcbOut)
842 {
843 *pcbOut = sizeof(uint32_t);
844 }
845
846 server->mConsole->onVRDEServerInfoChange();
847 } break;
848
849 default:
850 break;
851 }
852
853 return rc;
854}
855
856DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
857{
858 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
859
860 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
861}
862
863DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
864{
865 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
866
867 server->mConsole->VRDPClientConnect(u32ClientId);
868}
869
870DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
871{
872 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
873
874 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
875}
876
877DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
878{
879 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
880
881 LogFlowFunc(("%x\n", fu32Intercept));
882
883 int rc = VERR_NOT_SUPPORTED;
884
885 switch (fu32Intercept)
886 {
887 case VRDE_CLIENT_INTERCEPT_AUDIO:
888 {
889 server->mConsole->VRDPInterceptAudio(u32ClientId);
890 if (ppvIntercept)
891 {
892 *ppvIntercept = server;
893 }
894 rc = VINF_SUCCESS;
895 } break;
896
897 case VRDE_CLIENT_INTERCEPT_USB:
898 {
899 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
900 rc = VINF_SUCCESS;
901 } break;
902
903 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
904 {
905 server->mConsole->VRDPInterceptClipboard(u32ClientId);
906 if (ppvIntercept)
907 {
908 *ppvIntercept = server;
909 }
910 rc = VINF_SUCCESS;
911 } break;
912
913 default:
914 break;
915 }
916
917 return rc;
918}
919
920DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
921{
922#ifdef VBOX_WITH_USB
923 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
924#else
925 return VERR_NOT_SUPPORTED;
926#endif
927}
928
929DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
930{
931 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
932}
933
934DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
935{
936 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
937
938 bool fAvailable = false;
939
940 IFramebuffer *pfb = NULL;
941 LONG xOrigin = 0;
942 LONG yOrigin = 0;
943
944 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
945
946 if (pfb)
947 {
948 pfb->Lock ();
949
950 /* Query framebuffer parameters. */
951 ULONG lineSize = 0;
952 pfb->COMGETTER(BytesPerLine)(&lineSize);
953
954 ULONG bitsPerPixel = 0;
955 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
956
957 BYTE *address = NULL;
958 pfb->COMGETTER(Address)(&address);
959
960 ULONG height = 0;
961 pfb->COMGETTER(Height)(&height);
962
963 ULONG width = 0;
964 pfb->COMGETTER(Width)(&width);
965
966 /* Now fill the information as requested by the caller. */
967 pInfo->pu8Bits = address;
968 pInfo->xOrigin = xOrigin;
969 pInfo->yOrigin = yOrigin;
970 pInfo->cWidth = width;
971 pInfo->cHeight = height;
972 pInfo->cBitsPerPixel = bitsPerPixel;
973 pInfo->cbLine = lineSize;
974
975 pfb->Unlock();
976
977 fAvailable = true;
978 }
979
980 if (server->maFramebuffers[uScreenId])
981 {
982 server->maFramebuffers[uScreenId]->Release();
983 }
984 server->maFramebuffers[uScreenId] = pfb;
985
986 return fAvailable;
987}
988
989DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
990{
991 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
992
993 if (server->maFramebuffers[uScreenId])
994 {
995 server->maFramebuffers[uScreenId]->Lock();
996 }
997}
998
999DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1000{
1001 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1002
1003 if (server->maFramebuffers[uScreenId])
1004 {
1005 server->maFramebuffers[uScreenId]->Unlock();
1006 }
1007}
1008
1009static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1010{
1011 if ( pInputSynch->cGuestNumLockAdaptions
1012 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1013 {
1014 pInputSynch->cGuestNumLockAdaptions--;
1015 pKeyboard->PutScancode(0x45);
1016 pKeyboard->PutScancode(0x45 | 0x80);
1017 }
1018 if ( pInputSynch->cGuestCapsLockAdaptions
1019 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1020 {
1021 pInputSynch->cGuestCapsLockAdaptions--;
1022 pKeyboard->PutScancode(0x3a);
1023 pKeyboard->PutScancode(0x3a | 0x80);
1024 }
1025}
1026
1027DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1028{
1029 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1030 Console *pConsole = server->mConsole;
1031
1032 switch (type)
1033 {
1034 case VRDE_INPUT_SCANCODE:
1035 {
1036 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1037 {
1038 IKeyboard *pKeyboard = pConsole->getKeyboard();
1039
1040 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1041
1042 /* Track lock keys. */
1043 if (pInputScancode->uScancode == 0x45)
1044 {
1045 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1046 }
1047 else if (pInputScancode->uScancode == 0x3a)
1048 {
1049 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1050 }
1051 else if (pInputScancode->uScancode == 0x46)
1052 {
1053 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1054 }
1055 else if ((pInputScancode->uScancode & 0x80) == 0)
1056 {
1057 /* Key pressed. */
1058 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1059 }
1060
1061 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1062 }
1063 } break;
1064
1065 case VRDE_INPUT_POINT:
1066 {
1067 if (cbInput == sizeof(VRDEINPUTPOINT))
1068 {
1069 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1070
1071 int mouseButtons = 0;
1072 int iWheel = 0;
1073
1074 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1075 {
1076 mouseButtons |= MouseButtonState_LeftButton;
1077 }
1078 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1079 {
1080 mouseButtons |= MouseButtonState_RightButton;
1081 }
1082 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1083 {
1084 mouseButtons |= MouseButtonState_MiddleButton;
1085 }
1086 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1087 {
1088 mouseButtons |= MouseButtonState_WheelUp;
1089 iWheel = -1;
1090 }
1091 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1092 {
1093 mouseButtons |= MouseButtonState_WheelDown;
1094 iWheel = 1;
1095 }
1096
1097 if (server->m_fGuestWantsAbsolute)
1098 {
1099 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1100 } else
1101 {
1102 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1103 pInputPoint->y - server->m_mousey,
1104 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1105 server->m_mousex = pInputPoint->x;
1106 server->m_mousey = pInputPoint->y;
1107 }
1108 }
1109 } break;
1110
1111 case VRDE_INPUT_CAD:
1112 {
1113 pConsole->getKeyboard()->PutCAD();
1114 } break;
1115
1116 case VRDE_INPUT_RESET:
1117 {
1118 pConsole->Reset();
1119 } break;
1120
1121 case VRDE_INPUT_SYNCH:
1122 {
1123 if (cbInput == sizeof(VRDEINPUTSYNCH))
1124 {
1125 IKeyboard *pKeyboard = pConsole->getKeyboard();
1126
1127 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1128
1129 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1130 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1131 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1132
1133 /* The client initiated synchronization. Always make the guest to reflect the client state.
1134 * Than means, when the guest changes the state itself, it is forced to return to the client
1135 * state.
1136 */
1137 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1138 {
1139 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1140 }
1141
1142 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1143 {
1144 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1145 }
1146
1147 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1148 }
1149 } break;
1150
1151 default:
1152 break;
1153 }
1154}
1155
1156DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1157{
1158 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1159
1160 server->mConsole->getDisplay()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1161}
1162#endif /* VBOX_WITH_VRDP */
1163
1164ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1165{
1166 mConsole = console;
1167
1168 int rc = RTCritSectInit(&mCritSect);
1169 AssertRC(rc);
1170
1171 mcClipboardRefs = 0;
1172 mpfnClipboardCallback = NULL;
1173
1174#ifdef VBOX_WITH_USB
1175 mUSBBackends.pHead = NULL;
1176 mUSBBackends.pTail = NULL;
1177
1178 mUSBBackends.thread = NIL_RTTHREAD;
1179 mUSBBackends.fThreadRunning = false;
1180 mUSBBackends.event = 0;
1181#endif
1182
1183#ifdef VBOX_WITH_VRDP
1184 mhServer = 0;
1185
1186 m_fGuestWantsAbsolute = false;
1187 m_mousex = 0;
1188 m_mousey = 0;
1189
1190 m_InputSynch.cGuestNumLockAdaptions = 2;
1191 m_InputSynch.cGuestCapsLockAdaptions = 2;
1192
1193 m_InputSynch.fGuestNumLock = false;
1194 m_InputSynch.fGuestCapsLock = false;
1195 m_InputSynch.fGuestScrollLock = false;
1196
1197 m_InputSynch.fClientNumLock = false;
1198 m_InputSynch.fClientCapsLock = false;
1199 m_InputSynch.fClientScrollLock = false;
1200
1201 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1202
1203 {
1204 ComPtr<IEventSource> es;
1205 console->COMGETTER(EventSource)(es.asOutParam());
1206 mConsoleListener = new VRDPConsoleListener(this);
1207 mConsoleListener->AddRef();
1208 com::SafeArray <VBoxEventType_T> eventTypes;
1209 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1210 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1211 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1212 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1213 }
1214
1215 mVRDPBindPort = -1;
1216#endif /* VBOX_WITH_VRDP */
1217
1218 mAuthLibrary = 0;
1219}
1220
1221ConsoleVRDPServer::~ConsoleVRDPServer()
1222{
1223 Stop();
1224
1225#ifdef VBOX_WITH_VRDP
1226 if (mConsoleListener)
1227 {
1228 ComPtr<IEventSource> es;
1229 mConsole->COMGETTER(EventSource)(es.asOutParam());
1230 es->UnregisterListener(mConsoleListener);
1231 mConsoleListener->Release();
1232 mConsoleListener = NULL;
1233 }
1234
1235 unsigned i;
1236 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1237 {
1238 if (maFramebuffers[i])
1239 {
1240 maFramebuffers[i]->Release();
1241 maFramebuffers[i] = NULL;
1242 }
1243 }
1244#endif /* VBOX_WITH_VRDP */
1245
1246 if (RTCritSectIsInitialized(&mCritSect))
1247 {
1248 RTCritSectDelete(&mCritSect);
1249 memset(&mCritSect, 0, sizeof(mCritSect));
1250 }
1251}
1252
1253int ConsoleVRDPServer::Launch(void)
1254{
1255 LogFlowThisFunc(("\n"));
1256#ifdef VBOX_WITH_VRDP
1257 int rc = VINF_SUCCESS;
1258
1259 /*
1260 * Check that a VRDE library name is set.
1261 */
1262 Utf8Str filename;
1263
1264 ComPtr<IMachine> machine;
1265 HRESULT hrc = mConsole->COMGETTER(Machine)(machine.asOutParam());
1266
1267 if (SUCCEEDED(hrc))
1268 {
1269 ComPtr<IVirtualBox> virtualBox;
1270 hrc = machine->COMGETTER(Parent)(virtualBox.asOutParam());
1271
1272 if (SUCCEEDED(hrc))
1273 {
1274 ComPtr<ISystemProperties> systemProperties;
1275 hrc = virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
1276
1277 if (SUCCEEDED(hrc))
1278 {
1279 Bstr library;
1280 hrc = systemProperties->COMGETTER(DefaultVRDELibrary)(library.asOutParam());
1281
1282 if (SUCCEEDED(hrc))
1283 {
1284 filename = library;
1285 }
1286 }
1287 }
1288 }
1289
1290 if (FAILED(hrc) || filename.isEmpty())
1291 {
1292 return VINF_NOT_SUPPORTED;
1293 }
1294
1295 /*
1296 * Load the VRDE library and start the server, if it is enabled.
1297 */
1298 IVRDEServer *server = mConsole->getVRDEServer();
1299 Assert(server);
1300 BOOL fEnabled = FALSE;
1301
1302 hrc = server->COMGETTER(Enabled)(&fEnabled);
1303 AssertComRC(hrc);
1304
1305 if (SUCCEEDED(hrc) && fEnabled)
1306 {
1307 rc = loadVRDPLibrary(filename.c_str());
1308
1309 if (RT_SUCCESS(rc))
1310 {
1311 rc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&mpEntryPoints, &mhServer);
1312
1313 if (RT_SUCCESS(rc))
1314 {
1315#ifdef VBOX_WITH_USB
1316 remoteUSBThreadStart();
1317#endif /* VBOX_WITH_USB */
1318 }
1319 else if (rc != VERR_NET_ADDRESS_IN_USE)
1320 {
1321 LogRel(("VRDE: Could not start VRDP server rc = %Rrc\n", rc));
1322 }
1323 }
1324 }
1325#else
1326 int rc = VERR_NOT_SUPPORTED;
1327 LogRel(("VRDP: this version does not include the VRDP server.\n"));
1328#endif /* VBOX_WITH_VRDP */
1329 return rc;
1330}
1331
1332void ConsoleVRDPServer::EnableConnections(void)
1333{
1334#ifdef VBOX_WITH_VRDP
1335 if (mpEntryPoints && mhServer)
1336 {
1337 mpEntryPoints->VRDEEnableConnections(mhServer, true);
1338 }
1339#endif /* VBOX_WITH_VRDP */
1340}
1341
1342void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
1343{
1344#ifdef VBOX_WITH_VRDP
1345 if (mpEntryPoints && mhServer)
1346 {
1347 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
1348 }
1349#endif /* VBOX_WITH_VRDP */
1350}
1351
1352void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
1353{
1354#ifdef VBOX_WITH_VRDP
1355 if (mpEntryPoints && mhServer)
1356 {
1357 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
1358 }
1359#endif /* VBOX_WITH_VRDP */
1360}
1361
1362void ConsoleVRDPServer::MousePointerHide(void)
1363{
1364#ifdef VBOX_WITH_VRDP
1365 if (mpEntryPoints && mhServer)
1366 {
1367 mpEntryPoints->VRDEHidePointer(mhServer);
1368 }
1369#endif /* VBOX_WITH_VRDP */
1370}
1371
1372void ConsoleVRDPServer::Stop(void)
1373{
1374 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1375 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1376#ifdef VBOX_WITH_VRDP
1377 if (mhServer)
1378 {
1379 HVRDESERVER hServer = mhServer;
1380
1381 /* Reset the handle to avoid further calls to the server. */
1382 mhServer = 0;
1383
1384 if (mpEntryPoints && hServer)
1385 {
1386 mpEntryPoints->VRDEDestroy(hServer);
1387 }
1388 }
1389#endif /* VBOX_WITH_VRDP */
1390
1391#ifdef VBOX_WITH_USB
1392 remoteUSBThreadStop();
1393#endif /* VBOX_WITH_USB */
1394
1395 mpfnAuthEntry = NULL;
1396 mpfnAuthEntry2 = NULL;
1397
1398 if (mAuthLibrary)
1399 {
1400 RTLdrClose(mAuthLibrary);
1401 mAuthLibrary = 0;
1402 }
1403}
1404
1405/* Worker thread for Remote USB. The thread polls the clients for
1406 * the list of attached USB devices.
1407 * The thread is also responsible for attaching/detaching devices
1408 * to/from the VM.
1409 *
1410 * It is expected that attaching/detaching is not a frequent operation.
1411 *
1412 * The thread is always running when the VRDP server is active.
1413 *
1414 * The thread scans backends and requests the device list every 2 seconds.
1415 *
1416 * When device list is available, the thread calls the Console to process it.
1417 *
1418 */
1419#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1420
1421#ifdef VBOX_WITH_USB
1422static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
1423{
1424 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1425
1426 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1427
1428 pOwner->notifyRemoteUSBThreadRunning(self);
1429
1430 while (pOwner->isRemoteUSBThreadRunning())
1431 {
1432 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1433
1434 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
1435 {
1436 pRemoteUSBBackend->PollRemoteDevices();
1437 }
1438
1439 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
1440
1441 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
1442 }
1443
1444 return VINF_SUCCESS;
1445}
1446
1447void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
1448{
1449 mUSBBackends.thread = thread;
1450 mUSBBackends.fThreadRunning = true;
1451 int rc = RTThreadUserSignal(thread);
1452 AssertRC(rc);
1453}
1454
1455bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
1456{
1457 return mUSBBackends.fThreadRunning;
1458}
1459
1460void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
1461{
1462 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
1463 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1464 NOREF(rc);
1465}
1466
1467void ConsoleVRDPServer::remoteUSBThreadStart(void)
1468{
1469 int rc = RTSemEventCreate(&mUSBBackends.event);
1470
1471 if (RT_FAILURE(rc))
1472 {
1473 AssertFailed();
1474 mUSBBackends.event = 0;
1475 }
1476
1477 if (RT_SUCCESS(rc))
1478 {
1479 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
1480 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
1481 }
1482
1483 if (RT_FAILURE(rc))
1484 {
1485 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
1486 mUSBBackends.thread = NIL_RTTHREAD;
1487 }
1488 else
1489 {
1490 /* Wait until the thread is ready. */
1491 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
1492 AssertRC(rc);
1493 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
1494 }
1495}
1496
1497void ConsoleVRDPServer::remoteUSBThreadStop(void)
1498{
1499 mUSBBackends.fThreadRunning = false;
1500
1501 if (mUSBBackends.thread != NIL_RTTHREAD)
1502 {
1503 Assert (mUSBBackends.event != 0);
1504
1505 RTSemEventSignal(mUSBBackends.event);
1506
1507 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
1508 AssertRC(rc);
1509
1510 mUSBBackends.thread = NIL_RTTHREAD;
1511 }
1512
1513 if (mUSBBackends.event)
1514 {
1515 RTSemEventDestroy(mUSBBackends.event);
1516 mUSBBackends.event = 0;
1517 }
1518}
1519#endif /* VBOX_WITH_USB */
1520
1521VRDPAuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, VRDPAuthGuestJudgement guestJudgement,
1522 const char *pszUser, const char *pszPassword, const char *pszDomain,
1523 uint32_t u32ClientId)
1524{
1525 VRDPAUTHUUID rawuuid;
1526
1527 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1528
1529 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
1530 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
1531
1532 /*
1533 * Called only from VRDP input thread. So thread safety is not required.
1534 */
1535
1536 if (!mAuthLibrary)
1537 {
1538 /* Load the external authentication library. */
1539
1540 ComPtr<IMachine> machine;
1541 mConsole->COMGETTER(Machine)(machine.asOutParam());
1542
1543 ComPtr<IVirtualBox> virtualBox;
1544 machine->COMGETTER(Parent)(virtualBox.asOutParam());
1545
1546 ComPtr<ISystemProperties> systemProperties;
1547 virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
1548
1549 Bstr authLibrary;
1550 systemProperties->COMGETTER(VRDEAuthLibrary)(authLibrary.asOutParam());
1551
1552 Utf8Str filename = authLibrary;
1553
1554 LogRel(("VRDPAUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
1555
1556 int rc;
1557 if (RTPathHavePath(filename.c_str()))
1558 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
1559 else
1560 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
1561
1562 if (RT_FAILURE(rc))
1563 LogRel(("VRDPAUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
1564
1565 if (RT_SUCCESS(rc))
1566 {
1567 /* Get the entry point. */
1568 mpfnAuthEntry2 = NULL;
1569 int rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth2", (void**)&mpfnAuthEntry2);
1570 if (RT_FAILURE(rc2))
1571 {
1572 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1573 {
1574 LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth2", rc2));
1575 }
1576 rc = rc2;
1577 }
1578
1579 /* Get the entry point. */
1580 mpfnAuthEntry = NULL;
1581 rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth", (void**)&mpfnAuthEntry);
1582 if (RT_FAILURE(rc2))
1583 {
1584 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1585 {
1586 LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth", rc2));
1587 }
1588 rc = rc2;
1589 }
1590
1591 if (mpfnAuthEntry2 || mpfnAuthEntry)
1592 {
1593 LogRel(("VRDPAUTH: Using entry point '%s'.\n", mpfnAuthEntry2? "VRDPAuth2": "VRDPAuth"));
1594 rc = VINF_SUCCESS;
1595 }
1596 }
1597
1598 if (RT_FAILURE(rc))
1599 {
1600 mConsole->setError(E_FAIL,
1601 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
1602 filename.c_str(),
1603 rc);
1604
1605 mpfnAuthEntry = NULL;
1606 mpfnAuthEntry2 = NULL;
1607
1608 if (mAuthLibrary)
1609 {
1610 RTLdrClose(mAuthLibrary);
1611 mAuthLibrary = 0;
1612 }
1613
1614 return VRDPAuthAccessDenied;
1615 }
1616 }
1617
1618 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1619
1620 VRDPAuthResult result = mpfnAuthEntry2?
1621 mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId):
1622 mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
1623
1624 switch (result)
1625 {
1626 case VRDPAuthAccessDenied:
1627 LogRel(("VRDPAUTH: external authentication module returned 'access denied'\n"));
1628 break;
1629 case VRDPAuthAccessGranted:
1630 LogRel(("VRDPAUTH: external authentication module returned 'access granted'\n"));
1631 break;
1632 case VRDPAuthDelegateToGuest:
1633 LogRel(("VRDPAUTH: external authentication module returned 'delegate request to guest'\n"));
1634 break;
1635 default:
1636 LogRel(("VRDPAUTH: external authentication module returned incorrect return code %d\n", result));
1637 result = VRDPAuthAccessDenied;
1638 }
1639
1640 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
1641
1642 return result;
1643}
1644
1645void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
1646{
1647 VRDPAUTHUUID rawuuid;
1648
1649 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1650
1651 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
1652 rawuuid, u32ClientId));
1653
1654 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1655
1656 if (mpfnAuthEntry2)
1657 mpfnAuthEntry2(&rawuuid, VRDPAuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1658}
1659
1660int ConsoleVRDPServer::lockConsoleVRDPServer(void)
1661{
1662 int rc = RTCritSectEnter(&mCritSect);
1663 AssertRC(rc);
1664 return rc;
1665}
1666
1667void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
1668{
1669 RTCritSectLeave(&mCritSect);
1670}
1671
1672DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
1673 uint32_t u32ClientId,
1674 uint32_t u32Function,
1675 uint32_t u32Format,
1676 const void *pvData,
1677 uint32_t cbData)
1678{
1679 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
1680 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
1681
1682 int rc = VINF_SUCCESS;
1683
1684 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
1685
1686 NOREF(u32ClientId);
1687
1688 switch (u32Function)
1689 {
1690 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
1691 {
1692 if (pServer->mpfnClipboardCallback)
1693 {
1694 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1695 u32Format,
1696 (void *)pvData,
1697 cbData);
1698 }
1699 } break;
1700
1701 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
1702 {
1703 if (pServer->mpfnClipboardCallback)
1704 {
1705 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1706 u32Format,
1707 (void *)pvData,
1708 cbData);
1709 }
1710 } break;
1711
1712 default:
1713 rc = VERR_NOT_SUPPORTED;
1714 }
1715
1716 return rc;
1717}
1718
1719DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
1720 uint32_t u32Function,
1721 void *pvParms,
1722 uint32_t cbParms)
1723{
1724 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
1725 pvExtension, u32Function, pvParms, cbParms));
1726
1727 int rc = VINF_SUCCESS;
1728
1729#ifdef VBOX_WITH_VRDP
1730 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
1731
1732 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
1733
1734 switch (u32Function)
1735 {
1736 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
1737 {
1738 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
1739 } break;
1740
1741 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1742 {
1743 /* The guest announces clipboard formats. This must be delivered to all clients. */
1744 if (mpEntryPoints && pServer->mhServer)
1745 {
1746 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1747 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
1748 pParms->u32Format,
1749 NULL,
1750 0,
1751 NULL);
1752 }
1753 } break;
1754
1755 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1756 {
1757 /* The clipboard service expects that the pvData buffer will be filled
1758 * with clipboard data. The server returns the data from the client that
1759 * announced the requested format most recently.
1760 */
1761 if (mpEntryPoints && pServer->mhServer)
1762 {
1763 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1764 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
1765 pParms->u32Format,
1766 pParms->u.pvData,
1767 pParms->cbData,
1768 &pParms->cbData);
1769 }
1770 } break;
1771
1772 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
1773 {
1774 if (mpEntryPoints && pServer->mhServer)
1775 {
1776 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1777 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
1778 pParms->u32Format,
1779 pParms->u.pvData,
1780 pParms->cbData,
1781 NULL);
1782 }
1783 } break;
1784
1785 default:
1786 rc = VERR_NOT_SUPPORTED;
1787 }
1788#endif /* VBOX_WITH_VRDP */
1789
1790 return rc;
1791}
1792
1793void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
1794{
1795 int rc = lockConsoleVRDPServer();
1796
1797 if (RT_SUCCESS(rc))
1798 {
1799 if (mcClipboardRefs == 0)
1800 {
1801 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
1802
1803 if (RT_SUCCESS(rc))
1804 {
1805 mcClipboardRefs++;
1806 }
1807 }
1808
1809 unlockConsoleVRDPServer();
1810 }
1811}
1812
1813void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
1814{
1815 int rc = lockConsoleVRDPServer();
1816
1817 if (RT_SUCCESS(rc))
1818 {
1819 mcClipboardRefs--;
1820
1821 if (mcClipboardRefs == 0)
1822 {
1823 HGCMHostUnregisterServiceExtension(mhClipboard);
1824 }
1825
1826 unlockConsoleVRDPServer();
1827 }
1828}
1829
1830/* That is called on INPUT thread of the VRDP server.
1831 * The ConsoleVRDPServer keeps a list of created backend instances.
1832 */
1833void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
1834{
1835#ifdef VBOX_WITH_USB
1836 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
1837
1838 /* Create a new instance of the USB backend for the new client. */
1839 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
1840
1841 if (pRemoteUSBBackend)
1842 {
1843 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
1844
1845 /* Append the new instance in the list. */
1846 int rc = lockConsoleVRDPServer();
1847
1848 if (RT_SUCCESS(rc))
1849 {
1850 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
1851 if (mUSBBackends.pHead)
1852 {
1853 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
1854 }
1855 else
1856 {
1857 mUSBBackends.pTail = pRemoteUSBBackend;
1858 }
1859
1860 mUSBBackends.pHead = pRemoteUSBBackend;
1861
1862 unlockConsoleVRDPServer();
1863
1864 if (ppvIntercept)
1865 {
1866 *ppvIntercept = pRemoteUSBBackend;
1867 }
1868 }
1869
1870 if (RT_FAILURE(rc))
1871 {
1872 pRemoteUSBBackend->Release();
1873 }
1874 }
1875#endif /* VBOX_WITH_USB */
1876}
1877
1878void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
1879{
1880#ifdef VBOX_WITH_USB
1881 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
1882
1883 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1884
1885 /* Find the instance. */
1886 int rc = lockConsoleVRDPServer();
1887
1888 if (RT_SUCCESS(rc))
1889 {
1890 pRemoteUSBBackend = usbBackendFind(u32ClientId);
1891
1892 if (pRemoteUSBBackend)
1893 {
1894 /* Notify that it will be deleted. */
1895 pRemoteUSBBackend->NotifyDelete();
1896 }
1897
1898 unlockConsoleVRDPServer();
1899 }
1900
1901 if (pRemoteUSBBackend)
1902 {
1903 /* Here the instance has been excluded from the list and can be dereferenced. */
1904 pRemoteUSBBackend->Release();
1905 }
1906#endif
1907}
1908
1909void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
1910{
1911#ifdef VBOX_WITH_USB
1912 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1913
1914 /* Find the instance. */
1915 int rc = lockConsoleVRDPServer();
1916
1917 if (RT_SUCCESS(rc))
1918 {
1919 pRemoteUSBBackend = usbBackendFind(u32ClientId);
1920
1921 if (pRemoteUSBBackend)
1922 {
1923 /* Inform the backend instance that it is referenced by the Guid. */
1924 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
1925
1926 if (fAdded)
1927 {
1928 /* Reference the instance because its pointer is being taken. */
1929 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
1930 }
1931 else
1932 {
1933 pRemoteUSBBackend = NULL;
1934 }
1935 }
1936
1937 unlockConsoleVRDPServer();
1938 }
1939
1940 if (pRemoteUSBBackend)
1941 {
1942 return pRemoteUSBBackend->GetBackendCallbackPointer();
1943 }
1944
1945#endif
1946 return NULL;
1947}
1948
1949void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
1950{
1951#ifdef VBOX_WITH_USB
1952 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1953
1954 /* Find the instance. */
1955 int rc = lockConsoleVRDPServer();
1956
1957 if (RT_SUCCESS(rc))
1958 {
1959 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
1960
1961 if (pRemoteUSBBackend)
1962 {
1963 pRemoteUSBBackend->removeUUID(pGuid);
1964 }
1965
1966 unlockConsoleVRDPServer();
1967
1968 if (pRemoteUSBBackend)
1969 {
1970 pRemoteUSBBackend->Release();
1971 }
1972 }
1973#endif
1974}
1975
1976RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
1977{
1978 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
1979
1980 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
1981#ifdef VBOX_WITH_USB
1982
1983 int rc = lockConsoleVRDPServer();
1984
1985 if (RT_SUCCESS(rc))
1986 {
1987 if (pRemoteUSBBackend == NULL)
1988 {
1989 /* The first backend in the list is requested. */
1990 pNextRemoteUSBBackend = mUSBBackends.pHead;
1991 }
1992 else
1993 {
1994 /* Get pointer to the next backend. */
1995 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1996 }
1997
1998 if (pNextRemoteUSBBackend)
1999 {
2000 pNextRemoteUSBBackend->AddRef();
2001 }
2002
2003 unlockConsoleVRDPServer();
2004
2005 if (pRemoteUSBBackend)
2006 {
2007 pRemoteUSBBackend->Release();
2008 }
2009 }
2010#endif
2011
2012 return pNextRemoteUSBBackend;
2013}
2014
2015#ifdef VBOX_WITH_USB
2016/* Internal method. Called under the ConsoleVRDPServerLock. */
2017RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
2018{
2019 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2020
2021 while (pRemoteUSBBackend)
2022 {
2023 if (pRemoteUSBBackend->ClientId() == u32ClientId)
2024 {
2025 break;
2026 }
2027
2028 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2029 }
2030
2031 return pRemoteUSBBackend;
2032}
2033
2034/* Internal method. Called under the ConsoleVRDPServerLock. */
2035RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
2036{
2037 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2038
2039 while (pRemoteUSBBackend)
2040 {
2041 if (pRemoteUSBBackend->findUUID(pGuid))
2042 {
2043 break;
2044 }
2045
2046 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2047 }
2048
2049 return pRemoteUSBBackend;
2050}
2051#endif
2052
2053/* Internal method. Called by the backend destructor. */
2054void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2055{
2056#ifdef VBOX_WITH_USB
2057 int rc = lockConsoleVRDPServer();
2058 AssertRC(rc);
2059
2060 /* Exclude the found instance from the list. */
2061 if (pRemoteUSBBackend->pNext)
2062 {
2063 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2064 }
2065 else
2066 {
2067 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2068 }
2069
2070 if (pRemoteUSBBackend->pPrev)
2071 {
2072 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2073 }
2074 else
2075 {
2076 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2077 }
2078
2079 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2080
2081 unlockConsoleVRDPServer();
2082#endif
2083}
2084
2085
2086void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2087{
2088#ifdef VBOX_WITH_VRDP
2089 if (mpEntryPoints && mhServer)
2090 {
2091 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2092 }
2093#endif
2094}
2095
2096void ConsoleVRDPServer::SendResize(void) const
2097{
2098#ifdef VBOX_WITH_VRDP
2099 if (mpEntryPoints && mhServer)
2100 {
2101 mpEntryPoints->VRDEResize(mhServer);
2102 }
2103#endif
2104}
2105
2106void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2107{
2108#ifdef VBOX_WITH_VRDP
2109 VRDEORDERHDR update;
2110 update.x = x;
2111 update.y = y;
2112 update.w = w;
2113 update.h = h;
2114 if (mpEntryPoints && mhServer)
2115 {
2116 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
2117 }
2118#endif
2119}
2120
2121void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
2122{
2123#ifdef VBOX_WITH_VRDP
2124 if (mpEntryPoints && mhServer)
2125 {
2126 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
2127 }
2128#endif
2129}
2130
2131void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2132{
2133#ifdef VBOX_WITH_VRDP
2134 if (mpEntryPoints && mhServer)
2135 {
2136 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
2137 }
2138#endif
2139}
2140
2141void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2142{
2143#ifdef VBOX_WITH_VRDP
2144 if (mpEntryPoints && mhServer)
2145 {
2146 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2147 }
2148#endif
2149}
2150
2151void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2152{
2153#ifdef VBOX_WITH_VRDP
2154 if (index == VRDE_QI_PORT)
2155 {
2156 uint32_t cbOut = sizeof(int32_t);
2157
2158 if (cbBuffer >= cbOut)
2159 {
2160 *pcbOut = cbOut;
2161 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2162 }
2163 }
2164 else if (mpEntryPoints && mhServer)
2165 {
2166 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2167 }
2168#endif
2169}
2170
2171#ifdef VBOX_WITH_VRDP
2172/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
2173{
2174 int rc = VINF_SUCCESS;
2175
2176 if (!mVRDPLibrary)
2177 {
2178 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary);
2179
2180 if (RT_SUCCESS(rc))
2181 {
2182 struct SymbolEntry
2183 {
2184 const char *name;
2185 void **ppfn;
2186 };
2187
2188 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2189
2190 static const struct SymbolEntry symbols[] =
2191 {
2192 DEFSYMENTRY(VRDECreateServer)
2193 };
2194
2195 #undef DEFSYMENTRY
2196
2197 for (unsigned i = 0; i < RT_ELEMENTS(symbols); i++)
2198 {
2199 rc = RTLdrGetSymbol(mVRDPLibrary, symbols[i].name, symbols[i].ppfn);
2200
2201 if (RT_FAILURE(rc))
2202 {
2203 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", symbols[i].name, rc));
2204 break;
2205 }
2206 }
2207 }
2208 else
2209 {
2210 if (rc != VERR_FILE_NOT_FOUND)
2211 {
2212 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
2213 }
2214
2215 mVRDPLibrary = NULL;
2216 }
2217 }
2218
2219 if (RT_FAILURE(rc))
2220 {
2221 if (mVRDPLibrary)
2222 {
2223 RTLdrClose(mVRDPLibrary);
2224 mVRDPLibrary = NULL;
2225 }
2226 }
2227
2228 return rc;
2229}
2230#endif /* VBOX_WITH_VRDP */
2231
2232/*
2233 * IVRDEServerInfo implementation.
2234 */
2235// constructor / destructor
2236/////////////////////////////////////////////////////////////////////////////
2237
2238VRDEServerInfo::VRDEServerInfo()
2239 : mParent(NULL)
2240{
2241}
2242
2243VRDEServerInfo::~VRDEServerInfo()
2244{
2245}
2246
2247
2248HRESULT VRDEServerInfo::FinalConstruct()
2249{
2250 return S_OK;
2251}
2252
2253void VRDEServerInfo::FinalRelease()
2254{
2255 uninit();
2256}
2257
2258// public methods only for internal purposes
2259/////////////////////////////////////////////////////////////////////////////
2260
2261/**
2262 * Initializes the guest object.
2263 */
2264HRESULT VRDEServerInfo::init(Console *aParent)
2265{
2266 LogFlowThisFunc(("aParent=%p\n", aParent));
2267
2268 ComAssertRet(aParent, E_INVALIDARG);
2269
2270 /* Enclose the state transition NotReady->InInit->Ready */
2271 AutoInitSpan autoInitSpan(this);
2272 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2273
2274 unconst(mParent) = aParent;
2275
2276 /* Confirm a successful initialization */
2277 autoInitSpan.setSucceeded();
2278
2279 return S_OK;
2280}
2281
2282/**
2283 * Uninitializes the instance and sets the ready flag to FALSE.
2284 * Called either from FinalRelease() or by the parent when it gets destroyed.
2285 */
2286void VRDEServerInfo::uninit()
2287{
2288 LogFlowThisFunc(("\n"));
2289
2290 /* Enclose the state transition Ready->InUninit->NotReady */
2291 AutoUninitSpan autoUninitSpan(this);
2292 if (autoUninitSpan.uninitDone())
2293 return;
2294
2295 unconst(mParent) = NULL;
2296}
2297
2298// IVRDEServerInfo properties
2299/////////////////////////////////////////////////////////////////////////////
2300
2301#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2302 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2303 { \
2304 if (!a##_aName) \
2305 return E_POINTER; \
2306 \
2307 AutoCaller autoCaller(this); \
2308 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2309 \
2310 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2311 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2312 \
2313 uint32_t value; \
2314 uint32_t cbOut = 0; \
2315 \
2316 mParent->consoleVRDPServer()->QueryInfo \
2317 (_aIndex, &value, sizeof(value), &cbOut); \
2318 \
2319 *a##_aName = cbOut? !!value: FALSE; \
2320 \
2321 return S_OK; \
2322 } \
2323 extern void IMPL_GETTER_BOOL_DUMMY(void)
2324
2325#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
2326 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2327 { \
2328 if (!a##_aName) \
2329 return E_POINTER; \
2330 \
2331 AutoCaller autoCaller(this); \
2332 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2333 \
2334 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2335 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2336 \
2337 _aType value; \
2338 uint32_t cbOut = 0; \
2339 \
2340 mParent->consoleVRDPServer()->QueryInfo \
2341 (_aIndex, &value, sizeof(value), &cbOut); \
2342 \
2343 if (_aValueMask) value &= (_aValueMask); \
2344 *a##_aName = cbOut? value: 0; \
2345 \
2346 return S_OK; \
2347 } \
2348 extern void IMPL_GETTER_SCALAR_DUMMY(void)
2349
2350#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2351 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2352 { \
2353 if (!a##_aName) \
2354 return E_POINTER; \
2355 \
2356 AutoCaller autoCaller(this); \
2357 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2358 \
2359 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2360 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2361 \
2362 uint32_t cbOut = 0; \
2363 \
2364 mParent->consoleVRDPServer()->QueryInfo \
2365 (_aIndex, NULL, 0, &cbOut); \
2366 \
2367 if (cbOut == 0) \
2368 { \
2369 Bstr str(""); \
2370 str.cloneTo(a##_aName); \
2371 return S_OK; \
2372 } \
2373 \
2374 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
2375 \
2376 if (!pchBuffer) \
2377 { \
2378 Log(("VRDEServerInfo::" \
2379 #_aName \
2380 ": Failed to allocate memory %d bytes\n", cbOut)); \
2381 return E_OUTOFMEMORY; \
2382 } \
2383 \
2384 mParent->consoleVRDPServer()->QueryInfo \
2385 (_aIndex, pchBuffer, cbOut, &cbOut); \
2386 \
2387 Bstr str(pchBuffer); \
2388 \
2389 str.cloneTo(a##_aName); \
2390 \
2391 RTMemTmpFree(pchBuffer); \
2392 \
2393 return S_OK; \
2394 } \
2395 extern void IMPL_GETTER_BSTR_DUMMY(void)
2396
2397IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
2398IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
2399IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
2400IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
2401IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
2402IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
2403IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
2404IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
2405IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
2406IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
2407IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
2408IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
2409IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
2410IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
2411IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
2412
2413#undef IMPL_GETTER_BSTR
2414#undef IMPL_GETTER_SCALAR
2415#undef IMPL_GETTER_BOOL
2416/* 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