VirtualBox

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

Last change on this file since 25720 was 25634, checked in by vboxsync, 15 years ago

VRDP: Main: only use AppPriv if the library name doesn't contain a path component

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