VirtualBox

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

Last change on this file since 25881 was 25860, checked in by vboxsync, 15 years ago

Main: cleanup: get rid of VirtualBoxBaseProto, move AutoCaller*/*Span* classes out of VirtualBoxBaseProto class scope and into separate header; move CombinedProgress into separate header (it's only used by Console any more)

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