VirtualBox

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

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

VRDP port range API.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 67.9 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 23643 2009-10-09 12:23:32Z 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->machine ()->GetExtraData(Bstr("VBoxInternal2/VRDPPortRange"), bstr.asOutParam());
710 if (hrc != S_OK || bstr == "")
711 {
712 hrc = server->mConsole->getVRDPServer ()->COMGETTER(Ports) (bstr.asOutParam());
713 }
714 if (hrc != S_OK)
715 {
716 bstr = "";
717 }
718
719 if (bstr == "0")
720 {
721 bstr = "3389";
722 }
723
724 /* The server expects UTF8. */
725 com::Utf8Str portRange = bstr;
726
727 size_t cbPortRange = portRange.length () + 1;
728
729 if (cbPortRange >= 0x10000)
730 {
731 /* More than 64K seems to be an invalid port range string. */
732 rc = VERR_TOO_MUCH_DATA;
733 break;
734 }
735
736 if ((size_t)cbBuffer >= cbPortRange)
737 {
738 if (cbPortRange > 0)
739 {
740 if (portRange.raw())
741 {
742 memcpy (pvBuffer, portRange.raw(), cbPortRange);
743 }
744 else
745 {
746 /* The value is an empty string. */
747 *(uint8_t *)pvBuffer = 0;
748 }
749 }
750
751 rc = VINF_SUCCESS;
752 }
753 else
754 {
755 rc = VINF_BUFFER_OVERFLOW;
756 }
757
758 *pcbOut = (uint32_t)cbPortRange;
759 } break;
760
761 case VRDP_SP_NETWORK_BIND_PORT:
762 {
763 if (cbBuffer != sizeof (uint32_t))
764 {
765 rc = VERR_INVALID_PARAMETER;
766 break;
767 }
768
769 ULONG port = *(uint32_t *)pvBuffer;
770
771 server->mVRDPBindPort = port;
772
773 com::Bstr bstr = Utf8StrFmt("%d", port);
774
775 server->mConsole->machine ()->SetExtraData(Bstr("VBoxInternal2/VRDPBindPort"), bstr);
776
777 rc = VINF_SUCCESS;
778
779 if (pcbOut)
780 {
781 *pcbOut = sizeof (uint32_t);
782 }
783
784 server->mConsole->onRemoteDisplayInfoChange ();
785 } break;
786
787 default:
788 break;
789 }
790
791 return rc;
792}
793
794DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon (void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
795{
796 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
797
798 return server->mConsole->VRDPClientLogon (u32ClientId, pszUser, pszPassword, pszDomain);
799}
800
801DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect (void *pvCallback, uint32_t u32ClientId)
802{
803 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
804
805 server->mConsole->VRDPClientConnect (u32ClientId);
806}
807
808DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect (void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
809{
810 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
811
812 server->mConsole->VRDPClientDisconnect (u32ClientId, fu32Intercepted);
813}
814
815DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept (void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
816{
817 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
818
819 LogFlowFunc(("%x\n", fu32Intercept));
820
821 int rc = VERR_NOT_SUPPORTED;
822
823 switch (fu32Intercept)
824 {
825 case VRDP_CLIENT_INTERCEPT_AUDIO:
826 {
827 server->mConsole->VRDPInterceptAudio (u32ClientId);
828 if (ppvIntercept)
829 {
830 *ppvIntercept = server;
831 }
832 rc = VINF_SUCCESS;
833 } break;
834
835 case VRDP_CLIENT_INTERCEPT_USB:
836 {
837 server->mConsole->VRDPInterceptUSB (u32ClientId, ppvIntercept);
838 rc = VINF_SUCCESS;
839 } break;
840
841 case VRDP_CLIENT_INTERCEPT_CLIPBOARD:
842 {
843 server->mConsole->VRDPInterceptClipboard (u32ClientId);
844 if (ppvIntercept)
845 {
846 *ppvIntercept = server;
847 }
848 rc = VINF_SUCCESS;
849 } break;
850
851 default:
852 break;
853 }
854
855 return rc;
856}
857
858DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB (void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
859{
860#ifdef VBOX_WITH_USB
861 return USBClientResponseCallback (pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
862#else
863 return VERR_NOT_SUPPORTED;
864#endif
865}
866
867DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard (void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
868{
869 return ClipboardCallback (pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
870}
871
872DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery (void *pvCallback, unsigned uScreenId, VRDPFRAMEBUFFERINFO *pInfo)
873{
874 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
875
876 bool fAvailable = false;
877
878 IFramebuffer *pfb = NULL;
879 LONG xOrigin = 0;
880 LONG yOrigin = 0;
881
882 server->mConsole->getDisplay ()->GetFramebuffer (uScreenId, &pfb, &xOrigin, &yOrigin);
883
884 if (pfb)
885 {
886 pfb->Lock ();
887
888 /* Query framebuffer parameters. */
889 ULONG lineSize = 0;
890 pfb->COMGETTER(BytesPerLine) (&lineSize);
891
892 ULONG bitsPerPixel = 0;
893 pfb->COMGETTER(BitsPerPixel) (&bitsPerPixel);
894
895 BYTE *address = NULL;
896 pfb->COMGETTER(Address) (&address);
897
898 ULONG height = 0;
899 pfb->COMGETTER(Height) (&height);
900
901 ULONG width = 0;
902 pfb->COMGETTER(Width) (&width);
903
904 /* Now fill the information as requested by the caller. */
905 pInfo->pu8Bits = address;
906 pInfo->xOrigin = xOrigin;
907 pInfo->yOrigin = yOrigin;
908 pInfo->cWidth = width;
909 pInfo->cHeight = height;
910 pInfo->cBitsPerPixel = bitsPerPixel;
911 pInfo->cbLine = lineSize;
912
913 pfb->Unlock ();
914
915 fAvailable = true;
916 }
917
918 if (server->maFramebuffers[uScreenId])
919 {
920 server->maFramebuffers[uScreenId]->Release ();
921 }
922 server->maFramebuffers[uScreenId] = pfb;
923
924 return fAvailable;
925}
926
927DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock (void *pvCallback, unsigned uScreenId)
928{
929 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
930
931 if (server->maFramebuffers[uScreenId])
932 {
933 server->maFramebuffers[uScreenId]->Lock ();
934 }
935}
936
937DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock (void *pvCallback, unsigned uScreenId)
938{
939 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
940
941 if (server->maFramebuffers[uScreenId])
942 {
943 server->maFramebuffers[uScreenId]->Unlock ();
944 }
945}
946
947static void fixKbdLockStatus (VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
948{
949 if ( pInputSynch->cGuestNumLockAdaptions
950 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
951 {
952 pInputSynch->cGuestNumLockAdaptions--;
953 pKeyboard->PutScancode(0x45);
954 pKeyboard->PutScancode(0x45 | 0x80);
955 }
956 if ( pInputSynch->cGuestCapsLockAdaptions
957 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
958 {
959 pInputSynch->cGuestCapsLockAdaptions--;
960 pKeyboard->PutScancode(0x3a);
961 pKeyboard->PutScancode(0x3a | 0x80);
962 }
963}
964
965DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput (void *pvCallback, int type, const void *pvInput, unsigned cbInput)
966{
967 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
968 Console *pConsole = server->mConsole;
969
970 switch (type)
971 {
972 case VRDP_INPUT_SCANCODE:
973 {
974 if (cbInput == sizeof (VRDPINPUTSCANCODE))
975 {
976 IKeyboard *pKeyboard = pConsole->getKeyboard ();
977
978 const VRDPINPUTSCANCODE *pInputScancode = (VRDPINPUTSCANCODE *)pvInput;
979
980 /* Track lock keys. */
981 if (pInputScancode->uScancode == 0x45)
982 {
983 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
984 }
985 else if (pInputScancode->uScancode == 0x3a)
986 {
987 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
988 }
989 else if (pInputScancode->uScancode == 0x46)
990 {
991 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
992 }
993 else if ((pInputScancode->uScancode & 0x80) == 0)
994 {
995 /* Key pressed. */
996 fixKbdLockStatus (&server->m_InputSynch, pKeyboard);
997 }
998
999 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1000 }
1001 } break;
1002
1003 case VRDP_INPUT_POINT:
1004 {
1005 if (cbInput == sizeof (VRDPINPUTPOINT))
1006 {
1007 const VRDPINPUTPOINT *pInputPoint = (VRDPINPUTPOINT *)pvInput;
1008
1009 int mouseButtons = 0;
1010 int iWheel = 0;
1011
1012 if (pInputPoint->uButtons & VRDP_INPUT_POINT_BUTTON1)
1013 {
1014 mouseButtons |= MouseButtonState_LeftButton;
1015 }
1016 if (pInputPoint->uButtons & VRDP_INPUT_POINT_BUTTON2)
1017 {
1018 mouseButtons |= MouseButtonState_RightButton;
1019 }
1020 if (pInputPoint->uButtons & VRDP_INPUT_POINT_BUTTON3)
1021 {
1022 mouseButtons |= MouseButtonState_MiddleButton;
1023 }
1024 if (pInputPoint->uButtons & VRDP_INPUT_POINT_WHEEL_UP)
1025 {
1026 mouseButtons |= MouseButtonState_WheelUp;
1027 iWheel = -1;
1028 }
1029 if (pInputPoint->uButtons & VRDP_INPUT_POINT_WHEEL_DOWN)
1030 {
1031 mouseButtons |= MouseButtonState_WheelDown;
1032 iWheel = 1;
1033 }
1034
1035 if (server->m_fGuestWantsAbsolute)
1036 {
1037 pConsole->getMouse()->PutMouseEventAbsolute (pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1038 } else
1039 {
1040 pConsole->getMouse()->PutMouseEvent (pInputPoint->x - server->m_mousex,
1041 pInputPoint->y - server->m_mousey,
1042 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1043 server->m_mousex = pInputPoint->x;
1044 server->m_mousey = pInputPoint->y;
1045 }
1046 }
1047 } break;
1048
1049 case VRDP_INPUT_CAD:
1050 {
1051 pConsole->getKeyboard ()->PutCAD();
1052 } break;
1053
1054 case VRDP_INPUT_RESET:
1055 {
1056 pConsole->Reset();
1057 } break;
1058
1059 case VRDP_INPUT_SYNCH:
1060 {
1061 if (cbInput == sizeof (VRDPINPUTSYNCH))
1062 {
1063 IKeyboard *pKeyboard = pConsole->getKeyboard ();
1064
1065 const VRDPINPUTSYNCH *pInputSynch = (VRDPINPUTSYNCH *)pvInput;
1066
1067 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDP_INPUT_SYNCH_NUMLOCK) != 0;
1068 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDP_INPUT_SYNCH_CAPITAL) != 0;
1069 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDP_INPUT_SYNCH_SCROLL) != 0;
1070
1071 /* The client initiated synchronization. Always make the guest to reflect the client state.
1072 * Than means, when the guest changes the state itself, it is forced to return to the client
1073 * state.
1074 */
1075 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1076 {
1077 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1078 }
1079
1080 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1081 {
1082 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1083 }
1084
1085 fixKbdLockStatus (&server->m_InputSynch, pKeyboard);
1086 }
1087 } break;
1088
1089 default:
1090 break;
1091 }
1092}
1093
1094DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint (void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1095{
1096 ConsoleVRDPServer *server = static_cast <ConsoleVRDPServer *> (pvCallback);
1097
1098 server->mConsole->getDisplay ()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1099}
1100#endif /* VBOX_WITH_VRDP */
1101
1102ConsoleVRDPServer::ConsoleVRDPServer (Console *console)
1103{
1104 mConsole = console;
1105
1106 int rc = RTCritSectInit (&mCritSect);
1107 AssertRC (rc);
1108
1109 mcClipboardRefs = 0;
1110 mpfnClipboardCallback = NULL;
1111
1112#ifdef VBOX_WITH_USB
1113 mUSBBackends.pHead = NULL;
1114 mUSBBackends.pTail = NULL;
1115
1116 mUSBBackends.thread = NIL_RTTHREAD;
1117 mUSBBackends.fThreadRunning = false;
1118 mUSBBackends.event = 0;
1119#endif
1120
1121#ifdef VBOX_WITH_VRDP
1122 mhServer = 0;
1123
1124 m_fGuestWantsAbsolute = false;
1125 m_mousex = 0;
1126 m_mousey = 0;
1127
1128 m_InputSynch.cGuestNumLockAdaptions = 2;
1129 m_InputSynch.cGuestCapsLockAdaptions = 2;
1130
1131 m_InputSynch.fGuestNumLock = false;
1132 m_InputSynch.fGuestCapsLock = false;
1133 m_InputSynch.fGuestScrollLock = false;
1134
1135 m_InputSynch.fClientNumLock = false;
1136 m_InputSynch.fClientCapsLock = false;
1137 m_InputSynch.fClientScrollLock = false;
1138
1139 memset (maFramebuffers, 0, sizeof (maFramebuffers));
1140
1141 mConsoleCallback = new VRDPConsoleCallback(this);
1142 mConsoleCallback->AddRef();
1143 console->RegisterCallback(mConsoleCallback);
1144
1145 mConsole->machine ()->SetExtraData(Bstr("VBoxInternal2/VRDPPortRange"), Bstr(""));
1146 mVRDPBindPort = -1;
1147#endif /* VBOX_WITH_VRDP */
1148
1149 mAuthLibrary = 0;
1150}
1151
1152ConsoleVRDPServer::~ConsoleVRDPServer ()
1153{
1154 Stop ();
1155
1156#ifdef VBOX_WITH_VRDP
1157 if (mConsoleCallback)
1158 {
1159 mConsole->UnregisterCallback(mConsoleCallback);
1160 mConsoleCallback->Release();
1161 mConsoleCallback = NULL;
1162 }
1163
1164 unsigned i;
1165 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1166 {
1167 if (maFramebuffers[i])
1168 {
1169 maFramebuffers[i]->Release ();
1170 maFramebuffers[i] = NULL;
1171 }
1172 }
1173#endif /* VBOX_WITH_VRDP */
1174
1175 if (RTCritSectIsInitialized (&mCritSect))
1176 {
1177 RTCritSectDelete (&mCritSect);
1178 memset (&mCritSect, 0, sizeof (mCritSect));
1179 }
1180}
1181
1182int ConsoleVRDPServer::Launch (void)
1183{
1184 LogFlowMember(("ConsoleVRDPServer::Launch\n"));
1185#ifdef VBOX_WITH_VRDP
1186 int rc = VINF_SUCCESS;
1187 IVRDPServer *vrdpserver = mConsole->getVRDPServer ();
1188 Assert(vrdpserver);
1189 BOOL vrdpEnabled = FALSE;
1190
1191 HRESULT rc2 = vrdpserver->COMGETTER(Enabled) (&vrdpEnabled);
1192 AssertComRC(rc2);
1193
1194 if (SUCCEEDED(rc2) && vrdpEnabled)
1195 {
1196 if (loadVRDPLibrary ())
1197 {
1198 rc = mpfnVRDPCreateServer (&mCallbacks.header, this, (VRDPINTERFACEHDR **)&mpEntryPoints, &mhServer);
1199
1200 if (RT_SUCCESS(rc))
1201 {
1202#ifdef VBOX_WITH_USB
1203 remoteUSBThreadStart ();
1204#endif /* VBOX_WITH_USB */
1205 }
1206 else
1207 AssertMsgFailed(("Could not start VRDP server: rc = %Rrc\n", rc));
1208 }
1209 else
1210 {
1211 AssertMsgFailed(("Could not load the VRDP library\n"));
1212 rc = VERR_FILE_NOT_FOUND;
1213 }
1214 }
1215#else
1216 int rc = VERR_NOT_SUPPORTED;
1217 LogRel(("VRDP: this version does not include the VRDP server.\n"));
1218#endif /* VBOX_WITH_VRDP */
1219 return rc;
1220}
1221
1222void ConsoleVRDPServer::EnableConnections (void)
1223{
1224#ifdef VBOX_WITH_VRDP
1225 if (mpEntryPoints && mhServer)
1226 {
1227 mpEntryPoints->VRDPEnableConnections (mhServer, true);
1228 }
1229#endif /* VBOX_WITH_VRDP */
1230}
1231
1232void ConsoleVRDPServer::DisconnectClient (uint32_t u32ClientId, bool fReconnect)
1233{
1234#ifdef VBOX_WITH_VRDP
1235 if (mpEntryPoints && mhServer)
1236 {
1237 mpEntryPoints->VRDPDisconnect (mhServer, u32ClientId, fReconnect);
1238 }
1239#endif /* VBOX_WITH_VRDP */
1240}
1241
1242void ConsoleVRDPServer::MousePointerUpdate (const VRDPCOLORPOINTER *pPointer)
1243{
1244#ifdef VBOX_WITH_VRDP
1245 if (mpEntryPoints && mhServer)
1246 {
1247 mpEntryPoints->VRDPColorPointer (mhServer, pPointer);
1248 }
1249#endif /* VBOX_WITH_VRDP */
1250}
1251
1252void ConsoleVRDPServer::MousePointerHide (void)
1253{
1254#ifdef VBOX_WITH_VRDP
1255 if (mpEntryPoints && mhServer)
1256 {
1257 mpEntryPoints->VRDPHidePointer (mhServer);
1258 }
1259#endif /* VBOX_WITH_VRDP */
1260}
1261
1262void ConsoleVRDPServer::Stop (void)
1263{
1264 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1265 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1266#ifdef VBOX_WITH_VRDP
1267 if (mhServer)
1268 {
1269 HVRDPSERVER hServer = mhServer;
1270
1271 /* Reset the handle to avoid further calls to the server. */
1272 mhServer = 0;
1273
1274 if (mpEntryPoints && hServer)
1275 {
1276 mpEntryPoints->VRDPDestroy (hServer);
1277 }
1278 }
1279#endif /* VBOX_WITH_VRDP */
1280
1281#ifdef VBOX_WITH_USB
1282 remoteUSBThreadStop ();
1283#endif /* VBOX_WITH_USB */
1284
1285 mpfnAuthEntry = NULL;
1286 mpfnAuthEntry2 = NULL;
1287
1288 if (mAuthLibrary)
1289 {
1290 RTLdrClose(mAuthLibrary);
1291 mAuthLibrary = 0;
1292 }
1293}
1294
1295/* Worker thread for Remote USB. The thread polls the clients for
1296 * the list of attached USB devices.
1297 * The thread is also responsible for attaching/detaching devices
1298 * to/from the VM.
1299 *
1300 * It is expected that attaching/detaching is not a frequent operation.
1301 *
1302 * The thread is always running when the VRDP server is active.
1303 *
1304 * The thread scans backends and requests the device list every 2 seconds.
1305 *
1306 * When device list is available, the thread calls the Console to process it.
1307 *
1308 */
1309#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1310
1311#ifdef VBOX_WITH_USB
1312static DECLCALLBACK(int) threadRemoteUSB (RTTHREAD self, void *pvUser)
1313{
1314 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1315
1316 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1317
1318 pOwner->notifyRemoteUSBThreadRunning (self);
1319
1320 while (pOwner->isRemoteUSBThreadRunning ())
1321 {
1322 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1323
1324 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext (pRemoteUSBBackend)) != NULL)
1325 {
1326 pRemoteUSBBackend->PollRemoteDevices ();
1327 }
1328
1329 pOwner->waitRemoteUSBThreadEvent (VRDP_DEVICE_LIST_PERIOD_MS);
1330
1331 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
1332 }
1333
1334 return VINF_SUCCESS;
1335}
1336
1337void ConsoleVRDPServer::notifyRemoteUSBThreadRunning (RTTHREAD thread)
1338{
1339 mUSBBackends.thread = thread;
1340 mUSBBackends.fThreadRunning = true;
1341 int rc = RTThreadUserSignal (thread);
1342 AssertRC (rc);
1343}
1344
1345bool ConsoleVRDPServer::isRemoteUSBThreadRunning (void)
1346{
1347 return mUSBBackends.fThreadRunning;
1348}
1349
1350void ConsoleVRDPServer::waitRemoteUSBThreadEvent (unsigned cMillies)
1351{
1352 int rc = RTSemEventWait (mUSBBackends.event, cMillies);
1353 Assert (RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1354 NOREF(rc);
1355}
1356
1357void ConsoleVRDPServer::remoteUSBThreadStart (void)
1358{
1359 int rc = RTSemEventCreate (&mUSBBackends.event);
1360
1361 if (RT_FAILURE(rc))
1362 {
1363 AssertFailed ();
1364 mUSBBackends.event = 0;
1365 }
1366
1367 if (RT_SUCCESS(rc))
1368 {
1369 rc = RTThreadCreate (&mUSBBackends.thread, threadRemoteUSB, this, 65536,
1370 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
1371 }
1372
1373 if (RT_FAILURE(rc))
1374 {
1375 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
1376 mUSBBackends.thread = NIL_RTTHREAD;
1377 }
1378 else
1379 {
1380 /* Wait until the thread is ready. */
1381 rc = RTThreadUserWait (mUSBBackends.thread, 60000);
1382 AssertRC (rc);
1383 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
1384 }
1385}
1386
1387void ConsoleVRDPServer::remoteUSBThreadStop (void)
1388{
1389 mUSBBackends.fThreadRunning = false;
1390
1391 if (mUSBBackends.thread != NIL_RTTHREAD)
1392 {
1393 Assert (mUSBBackends.event != 0);
1394
1395 RTSemEventSignal (mUSBBackends.event);
1396
1397 int rc = RTThreadWait (mUSBBackends.thread, 60000, NULL);
1398 AssertRC (rc);
1399
1400 mUSBBackends.thread = NIL_RTTHREAD;
1401 }
1402
1403 if (mUSBBackends.event)
1404 {
1405 RTSemEventDestroy (mUSBBackends.event);
1406 mUSBBackends.event = 0;
1407 }
1408}
1409#endif /* VBOX_WITH_USB */
1410
1411VRDPAuthResult ConsoleVRDPServer::Authenticate (const Guid &uuid, VRDPAuthGuestJudgement guestJudgement,
1412 const char *pszUser, const char *pszPassword, const char *pszDomain,
1413 uint32_t u32ClientId)
1414{
1415 VRDPAUTHUUID rawuuid;
1416
1417 memcpy (rawuuid, ((Guid &)uuid).ptr (), sizeof (rawuuid));
1418
1419 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
1420 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
1421
1422 /*
1423 * Called only from VRDP input thread. So thread safety is not required.
1424 */
1425
1426 if (!mAuthLibrary)
1427 {
1428 /* Load the external authentication library. */
1429
1430 ComPtr<IMachine> machine;
1431 mConsole->COMGETTER(Machine)(machine.asOutParam());
1432
1433 ComPtr<IVirtualBox> virtualBox;
1434 machine->COMGETTER(Parent)(virtualBox.asOutParam());
1435
1436 ComPtr<ISystemProperties> systemProperties;
1437 virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
1438
1439 Bstr authLibrary;
1440 systemProperties->COMGETTER(RemoteDisplayAuthLibrary)(authLibrary.asOutParam());
1441
1442 Utf8Str filename = authLibrary;
1443
1444 LogRel(("VRDPAUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
1445
1446 int rc = RTLdrLoad (filename.raw(), &mAuthLibrary);
1447 if (RT_FAILURE(rc))
1448 LogRel(("VRDPAUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
1449
1450 if (RT_SUCCESS(rc))
1451 {
1452 /* Get the entry point. */
1453 mpfnAuthEntry2 = NULL;
1454 int rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth2", (void**)&mpfnAuthEntry2);
1455 if (RT_FAILURE(rc2))
1456 {
1457 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1458 {
1459 LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth2", rc2));
1460 }
1461 rc = rc2;
1462 }
1463
1464 /* Get the entry point. */
1465 mpfnAuthEntry = NULL;
1466 rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth", (void**)&mpfnAuthEntry);
1467 if (RT_FAILURE(rc2))
1468 {
1469 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1470 {
1471 LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth", rc2));
1472 }
1473 rc = rc2;
1474 }
1475
1476 if (mpfnAuthEntry2 || mpfnAuthEntry)
1477 {
1478 LogRel(("VRDPAUTH: Using entry point '%s'.\n", mpfnAuthEntry2? "VRDPAuth2": "VRDPAuth"));
1479 rc = VINF_SUCCESS;
1480 }
1481 }
1482
1483 if (RT_FAILURE(rc))
1484 {
1485 mConsole->reportAuthLibraryError (filename.raw(), rc);
1486
1487 mpfnAuthEntry = NULL;
1488 mpfnAuthEntry2 = NULL;
1489
1490 if (mAuthLibrary)
1491 {
1492 RTLdrClose(mAuthLibrary);
1493 mAuthLibrary = 0;
1494 }
1495
1496 return VRDPAuthAccessDenied;
1497 }
1498 }
1499
1500 Assert (mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1501
1502 VRDPAuthResult result = mpfnAuthEntry2?
1503 mpfnAuthEntry2 (&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId):
1504 mpfnAuthEntry (&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
1505
1506 switch (result)
1507 {
1508 case VRDPAuthAccessDenied:
1509 LogRel(("VRDPAUTH: external authentication module returned 'access denied'\n"));
1510 break;
1511 case VRDPAuthAccessGranted:
1512 LogRel(("VRDPAUTH: external authentication module returned 'access granted'\n"));
1513 break;
1514 case VRDPAuthDelegateToGuest:
1515 LogRel(("VRDPAUTH: external authentication module returned 'delegate request to guest'\n"));
1516 break;
1517 default:
1518 LogRel(("VRDPAUTH: external authentication module returned incorrect return code %d\n", result));
1519 result = VRDPAuthAccessDenied;
1520 }
1521
1522 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
1523
1524 return result;
1525}
1526
1527void ConsoleVRDPServer::AuthDisconnect (const Guid &uuid, uint32_t u32ClientId)
1528{
1529 VRDPAUTHUUID rawuuid;
1530
1531 memcpy (rawuuid, ((Guid &)uuid).ptr (), sizeof (rawuuid));
1532
1533 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
1534 rawuuid, u32ClientId));
1535
1536 Assert (mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1537
1538 if (mpfnAuthEntry2)
1539 mpfnAuthEntry2 (&rawuuid, VRDPAuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1540}
1541
1542int ConsoleVRDPServer::lockConsoleVRDPServer (void)
1543{
1544 int rc = RTCritSectEnter (&mCritSect);
1545 AssertRC (rc);
1546 return rc;
1547}
1548
1549void ConsoleVRDPServer::unlockConsoleVRDPServer (void)
1550{
1551 RTCritSectLeave (&mCritSect);
1552}
1553
1554DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback (void *pvCallback,
1555 uint32_t u32ClientId,
1556 uint32_t u32Function,
1557 uint32_t u32Format,
1558 const void *pvData,
1559 uint32_t cbData)
1560{
1561 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
1562 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
1563
1564 int rc = VINF_SUCCESS;
1565
1566 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
1567
1568 NOREF(u32ClientId);
1569
1570 switch (u32Function)
1571 {
1572 case VRDP_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
1573 {
1574 if (pServer->mpfnClipboardCallback)
1575 {
1576 pServer->mpfnClipboardCallback (VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1577 u32Format,
1578 (void *)pvData,
1579 cbData);
1580 }
1581 } break;
1582
1583 case VRDP_CLIPBOARD_FUNCTION_DATA_READ:
1584 {
1585 if (pServer->mpfnClipboardCallback)
1586 {
1587 pServer->mpfnClipboardCallback (VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1588 u32Format,
1589 (void *)pvData,
1590 cbData);
1591 }
1592 } break;
1593
1594 default:
1595 rc = VERR_NOT_SUPPORTED;
1596 }
1597
1598 return rc;
1599}
1600
1601DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension (void *pvExtension,
1602 uint32_t u32Function,
1603 void *pvParms,
1604 uint32_t cbParms)
1605{
1606 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
1607 pvExtension, u32Function, pvParms, cbParms));
1608
1609 int rc = VINF_SUCCESS;
1610
1611#ifdef VBOX_WITH_VRDP
1612 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
1613
1614 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
1615
1616 switch (u32Function)
1617 {
1618 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
1619 {
1620 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
1621 } break;
1622
1623 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1624 {
1625 /* The guest announces clipboard formats. This must be delivered to all clients. */
1626 if (mpEntryPoints && pServer->mhServer)
1627 {
1628 mpEntryPoints->VRDPClipboard (pServer->mhServer,
1629 VRDP_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
1630 pParms->u32Format,
1631 NULL,
1632 0,
1633 NULL);
1634 }
1635 } break;
1636
1637 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1638 {
1639 /* The clipboard service expects that the pvData buffer will be filled
1640 * with clipboard data. The server returns the data from the client that
1641 * announced the requested format most recently.
1642 */
1643 if (mpEntryPoints && pServer->mhServer)
1644 {
1645 mpEntryPoints->VRDPClipboard (pServer->mhServer,
1646 VRDP_CLIPBOARD_FUNCTION_DATA_READ,
1647 pParms->u32Format,
1648 pParms->u.pvData,
1649 pParms->cbData,
1650 &pParms->cbData);
1651 }
1652 } break;
1653
1654 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
1655 {
1656 if (mpEntryPoints && pServer->mhServer)
1657 {
1658 mpEntryPoints->VRDPClipboard (pServer->mhServer,
1659 VRDP_CLIPBOARD_FUNCTION_DATA_WRITE,
1660 pParms->u32Format,
1661 pParms->u.pvData,
1662 pParms->cbData,
1663 NULL);
1664 }
1665 } break;
1666
1667 default:
1668 rc = VERR_NOT_SUPPORTED;
1669 }
1670#endif /* VBOX_WITH_VRDP */
1671
1672 return rc;
1673}
1674
1675void ConsoleVRDPServer::ClipboardCreate (uint32_t u32ClientId)
1676{
1677 int rc = lockConsoleVRDPServer ();
1678
1679 if (RT_SUCCESS(rc))
1680 {
1681 if (mcClipboardRefs == 0)
1682 {
1683 rc = HGCMHostRegisterServiceExtension (&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
1684
1685 if (RT_SUCCESS(rc))
1686 {
1687 mcClipboardRefs++;
1688 }
1689 }
1690
1691 unlockConsoleVRDPServer ();
1692 }
1693}
1694
1695void ConsoleVRDPServer::ClipboardDelete (uint32_t u32ClientId)
1696{
1697 int rc = lockConsoleVRDPServer ();
1698
1699 if (RT_SUCCESS(rc))
1700 {
1701 mcClipboardRefs--;
1702
1703 if (mcClipboardRefs == 0)
1704 {
1705 HGCMHostUnregisterServiceExtension (mhClipboard);
1706 }
1707
1708 unlockConsoleVRDPServer ();
1709 }
1710}
1711
1712/* That is called on INPUT thread of the VRDP server.
1713 * The ConsoleVRDPServer keeps a list of created backend instances.
1714 */
1715void ConsoleVRDPServer::USBBackendCreate (uint32_t u32ClientId, void **ppvIntercept)
1716{
1717#ifdef VBOX_WITH_USB
1718 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
1719
1720 /* Create a new instance of the USB backend for the new client. */
1721 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend (mConsole, this, u32ClientId);
1722
1723 if (pRemoteUSBBackend)
1724 {
1725 pRemoteUSBBackend->AddRef (); /* 'Release' called in USBBackendDelete. */
1726
1727 /* Append the new instance in the list. */
1728 int rc = lockConsoleVRDPServer ();
1729
1730 if (RT_SUCCESS(rc))
1731 {
1732 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
1733 if (mUSBBackends.pHead)
1734 {
1735 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
1736 }
1737 else
1738 {
1739 mUSBBackends.pTail = pRemoteUSBBackend;
1740 }
1741
1742 mUSBBackends.pHead = pRemoteUSBBackend;
1743
1744 unlockConsoleVRDPServer ();
1745
1746 if (ppvIntercept)
1747 {
1748 *ppvIntercept = pRemoteUSBBackend;
1749 }
1750 }
1751
1752 if (RT_FAILURE(rc))
1753 {
1754 pRemoteUSBBackend->Release ();
1755 }
1756 }
1757#endif /* VBOX_WITH_USB */
1758}
1759
1760void ConsoleVRDPServer::USBBackendDelete (uint32_t u32ClientId)
1761{
1762#ifdef VBOX_WITH_USB
1763 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
1764
1765 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1766
1767 /* Find the instance. */
1768 int rc = lockConsoleVRDPServer ();
1769
1770 if (RT_SUCCESS(rc))
1771 {
1772 pRemoteUSBBackend = usbBackendFind (u32ClientId);
1773
1774 if (pRemoteUSBBackend)
1775 {
1776 /* Notify that it will be deleted. */
1777 pRemoteUSBBackend->NotifyDelete ();
1778 }
1779
1780 unlockConsoleVRDPServer ();
1781 }
1782
1783 if (pRemoteUSBBackend)
1784 {
1785 /* Here the instance has been excluded from the list and can be dereferenced. */
1786 pRemoteUSBBackend->Release ();
1787 }
1788#endif
1789}
1790
1791void *ConsoleVRDPServer::USBBackendRequestPointer (uint32_t u32ClientId, const Guid *pGuid)
1792{
1793#ifdef VBOX_WITH_USB
1794 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1795
1796 /* Find the instance. */
1797 int rc = lockConsoleVRDPServer ();
1798
1799 if (RT_SUCCESS(rc))
1800 {
1801 pRemoteUSBBackend = usbBackendFind (u32ClientId);
1802
1803 if (pRemoteUSBBackend)
1804 {
1805 /* Inform the backend instance that it is referenced by the Guid. */
1806 bool fAdded = pRemoteUSBBackend->addUUID (pGuid);
1807
1808 if (fAdded)
1809 {
1810 /* Reference the instance because its pointer is being taken. */
1811 pRemoteUSBBackend->AddRef (); /* 'Release' is called in USBBackendReleasePointer. */
1812 }
1813 else
1814 {
1815 pRemoteUSBBackend = NULL;
1816 }
1817 }
1818
1819 unlockConsoleVRDPServer ();
1820 }
1821
1822 if (pRemoteUSBBackend)
1823 {
1824 return pRemoteUSBBackend->GetBackendCallbackPointer ();
1825 }
1826
1827#endif
1828 return NULL;
1829}
1830
1831void ConsoleVRDPServer::USBBackendReleasePointer (const Guid *pGuid)
1832{
1833#ifdef VBOX_WITH_USB
1834 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1835
1836 /* Find the instance. */
1837 int rc = lockConsoleVRDPServer ();
1838
1839 if (RT_SUCCESS(rc))
1840 {
1841 pRemoteUSBBackend = usbBackendFindByUUID (pGuid);
1842
1843 if (pRemoteUSBBackend)
1844 {
1845 pRemoteUSBBackend->removeUUID (pGuid);
1846 }
1847
1848 unlockConsoleVRDPServer ();
1849
1850 if (pRemoteUSBBackend)
1851 {
1852 pRemoteUSBBackend->Release ();
1853 }
1854 }
1855#endif
1856}
1857
1858RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext (RemoteUSBBackend *pRemoteUSBBackend)
1859{
1860 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
1861
1862 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
1863#ifdef VBOX_WITH_USB
1864
1865 int rc = lockConsoleVRDPServer ();
1866
1867 if (RT_SUCCESS(rc))
1868 {
1869 if (pRemoteUSBBackend == NULL)
1870 {
1871 /* The first backend in the list is requested. */
1872 pNextRemoteUSBBackend = mUSBBackends.pHead;
1873 }
1874 else
1875 {
1876 /* Get pointer to the next backend. */
1877 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1878 }
1879
1880 if (pNextRemoteUSBBackend)
1881 {
1882 pNextRemoteUSBBackend->AddRef ();
1883 }
1884
1885 unlockConsoleVRDPServer ();
1886
1887 if (pRemoteUSBBackend)
1888 {
1889 pRemoteUSBBackend->Release ();
1890 }
1891 }
1892#endif
1893
1894 return pNextRemoteUSBBackend;
1895}
1896
1897#ifdef VBOX_WITH_USB
1898/* Internal method. Called under the ConsoleVRDPServerLock. */
1899RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind (uint32_t u32ClientId)
1900{
1901 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
1902
1903 while (pRemoteUSBBackend)
1904 {
1905 if (pRemoteUSBBackend->ClientId () == u32ClientId)
1906 {
1907 break;
1908 }
1909
1910 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1911 }
1912
1913 return pRemoteUSBBackend;
1914}
1915
1916/* Internal method. Called under the ConsoleVRDPServerLock. */
1917RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID (const Guid *pGuid)
1918{
1919 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
1920
1921 while (pRemoteUSBBackend)
1922 {
1923 if (pRemoteUSBBackend->findUUID (pGuid))
1924 {
1925 break;
1926 }
1927
1928 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1929 }
1930
1931 return pRemoteUSBBackend;
1932}
1933#endif
1934
1935/* Internal method. Called by the backend destructor. */
1936void ConsoleVRDPServer::usbBackendRemoveFromList (RemoteUSBBackend *pRemoteUSBBackend)
1937{
1938#ifdef VBOX_WITH_USB
1939 int rc = lockConsoleVRDPServer ();
1940 AssertRC (rc);
1941
1942 /* Exclude the found instance from the list. */
1943 if (pRemoteUSBBackend->pNext)
1944 {
1945 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
1946 }
1947 else
1948 {
1949 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
1950 }
1951
1952 if (pRemoteUSBBackend->pPrev)
1953 {
1954 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
1955 }
1956 else
1957 {
1958 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1959 }
1960
1961 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
1962
1963 unlockConsoleVRDPServer ();
1964#endif
1965}
1966
1967
1968void ConsoleVRDPServer::SendUpdate (unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
1969{
1970#ifdef VBOX_WITH_VRDP
1971 if (mpEntryPoints && mhServer)
1972 {
1973 mpEntryPoints->VRDPUpdate (mhServer, uScreenId, pvUpdate, cbUpdate);
1974 }
1975#endif
1976}
1977
1978void ConsoleVRDPServer::SendResize (void) const
1979{
1980#ifdef VBOX_WITH_VRDP
1981 if (mpEntryPoints && mhServer)
1982 {
1983 mpEntryPoints->VRDPResize (mhServer);
1984 }
1985#endif
1986}
1987
1988void ConsoleVRDPServer::SendUpdateBitmap (unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
1989{
1990#ifdef VBOX_WITH_VRDP
1991 VRDPORDERHDR update;
1992 update.x = x;
1993 update.y = y;
1994 update.w = w;
1995 update.h = h;
1996 if (mpEntryPoints && mhServer)
1997 {
1998 mpEntryPoints->VRDPUpdate (mhServer, uScreenId, &update, sizeof (update));
1999 }
2000#endif
2001}
2002
2003void ConsoleVRDPServer::SendAudioSamples (void *pvSamples, uint32_t cSamples, VRDPAUDIOFORMAT format) const
2004{
2005#ifdef VBOX_WITH_VRDP
2006 if (mpEntryPoints && mhServer)
2007 {
2008 mpEntryPoints->VRDPAudioSamples (mhServer, pvSamples, cSamples, format);
2009 }
2010#endif
2011}
2012
2013void ConsoleVRDPServer::SendAudioVolume (uint16_t left, uint16_t right) const
2014{
2015#ifdef VBOX_WITH_VRDP
2016 if (mpEntryPoints && mhServer)
2017 {
2018 mpEntryPoints->VRDPAudioVolume (mhServer, left, right);
2019 }
2020#endif
2021}
2022
2023void ConsoleVRDPServer::SendUSBRequest (uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2024{
2025#ifdef VBOX_WITH_VRDP
2026 if (mpEntryPoints && mhServer)
2027 {
2028 mpEntryPoints->VRDPUSBRequest (mhServer, u32ClientId, pvParms, cbParms);
2029 }
2030#endif
2031}
2032
2033void ConsoleVRDPServer::QueryInfo (uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2034{
2035#ifdef VBOX_WITH_VRDP
2036 if (index == VRDP_QI_PORT)
2037 {
2038 uint32_t cbOut = sizeof (int32_t);
2039
2040 if (cbBuffer >= cbOut)
2041 {
2042 *pcbOut = cbOut;
2043 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2044 }
2045 }
2046 else if (mpEntryPoints && mhServer)
2047 {
2048 mpEntryPoints->VRDPQueryInfo (mhServer, index, pvBuffer, cbBuffer, pcbOut);
2049 }
2050#endif
2051}
2052
2053#ifdef VBOX_WITH_VRDP
2054/* note: static function now! */
2055bool ConsoleVRDPServer::loadVRDPLibrary (void)
2056{
2057 int rc = VINF_SUCCESS;
2058
2059 if (!mVRDPLibrary)
2060 {
2061 rc = SUPR3HardenedLdrLoadAppPriv ("VBoxVRDP", &mVRDPLibrary);
2062
2063 if (RT_SUCCESS(rc))
2064 {
2065 LogFlow(("VRDPServer::loadLibrary(): successfully loaded VRDP library.\n"));
2066
2067 struct SymbolEntry
2068 {
2069 const char *name;
2070 void **ppfn;
2071 };
2072
2073 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2074
2075 static const struct SymbolEntry symbols[] =
2076 {
2077 DEFSYMENTRY(VRDPCreateServer)
2078 };
2079
2080 #undef DEFSYMENTRY
2081
2082 for (unsigned i = 0; i < RT_ELEMENTS(symbols); i++)
2083 {
2084 rc = RTLdrGetSymbol(mVRDPLibrary, symbols[i].name, symbols[i].ppfn);
2085
2086 AssertMsgRC(rc, ("Error resolving VRDP symbol %s\n", symbols[i].name));
2087
2088 if (RT_FAILURE(rc))
2089 {
2090 break;
2091 }
2092 }
2093 }
2094 else
2095 {
2096 LogRel(("VRDPServer::loadLibrary(): failed to load VRDP library! VRDP not available: rc = %Rrc\n", rc));
2097 mVRDPLibrary = NULL;
2098 }
2099 }
2100
2101 // just to be safe
2102 if (RT_FAILURE(rc))
2103 {
2104 if (mVRDPLibrary)
2105 {
2106 RTLdrClose (mVRDPLibrary);
2107 mVRDPLibrary = NULL;
2108 }
2109 }
2110
2111 return (mVRDPLibrary != NULL);
2112}
2113#endif /* VBOX_WITH_VRDP */
2114
2115/*
2116 * IRemoteDisplayInfo implementation.
2117 */
2118// constructor / destructor
2119/////////////////////////////////////////////////////////////////////////////
2120
2121DEFINE_EMPTY_CTOR_DTOR (RemoteDisplayInfo)
2122
2123HRESULT RemoteDisplayInfo::FinalConstruct()
2124{
2125 return S_OK;
2126}
2127
2128void RemoteDisplayInfo::FinalRelease()
2129{
2130 uninit ();
2131}
2132
2133// public methods only for internal purposes
2134/////////////////////////////////////////////////////////////////////////////
2135
2136/**
2137 * Initializes the guest object.
2138 */
2139HRESULT RemoteDisplayInfo::init (Console *aParent)
2140{
2141 LogFlowThisFunc(("aParent=%p\n", aParent));
2142
2143 ComAssertRet (aParent, E_INVALIDARG);
2144
2145 /* Enclose the state transition NotReady->InInit->Ready */
2146 AutoInitSpan autoInitSpan(this);
2147 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2148
2149 unconst(mParent) = aParent;
2150
2151 /* Confirm a successful initialization */
2152 autoInitSpan.setSucceeded();
2153
2154 return S_OK;
2155}
2156
2157/**
2158 * Uninitializes the instance and sets the ready flag to FALSE.
2159 * Called either from FinalRelease() or by the parent when it gets destroyed.
2160 */
2161void RemoteDisplayInfo::uninit()
2162{
2163 LogFlowThisFunc(("\n"));
2164
2165 /* Enclose the state transition Ready->InUninit->NotReady */
2166 AutoUninitSpan autoUninitSpan(this);
2167 if (autoUninitSpan.uninitDone())
2168 return;
2169
2170 unconst(mParent).setNull();
2171}
2172
2173// IRemoteDisplayInfo properties
2174/////////////////////////////////////////////////////////////////////////////
2175
2176#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2177 STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName) (_aType *a##_aName) \
2178 { \
2179 if (!a##_aName) \
2180 return E_POINTER; \
2181 \
2182 AutoCaller autoCaller(this); \
2183 CheckComRCReturnRC(autoCaller.rc()); \
2184 \
2185 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2186 AutoWriteLock alock(this); \
2187 \
2188 uint32_t value; \
2189 uint32_t cbOut = 0; \
2190 \
2191 mParent->consoleVRDPServer ()->QueryInfo \
2192 (_aIndex, &value, sizeof (value), &cbOut); \
2193 \
2194 *a##_aName = cbOut? !!value: FALSE; \
2195 \
2196 return S_OK; \
2197 }
2198
2199#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex) \
2200 STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName) (_aType *a##_aName) \
2201 { \
2202 if (!a##_aName) \
2203 return E_POINTER; \
2204 \
2205 AutoCaller autoCaller(this); \
2206 CheckComRCReturnRC(autoCaller.rc()); \
2207 \
2208 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2209 AutoWriteLock alock(this); \
2210 \
2211 _aType value; \
2212 uint32_t cbOut = 0; \
2213 \
2214 mParent->consoleVRDPServer ()->QueryInfo \
2215 (_aIndex, &value, sizeof (value), &cbOut); \
2216 \
2217 *a##_aName = cbOut? value: 0; \
2218 \
2219 return S_OK; \
2220 }
2221
2222#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2223 STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName) (_aType *a##_aName) \
2224 { \
2225 if (!a##_aName) \
2226 return E_POINTER; \
2227 \
2228 AutoCaller autoCaller(this); \
2229 CheckComRCReturnRC(autoCaller.rc()); \
2230 \
2231 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2232 AutoWriteLock alock(this); \
2233 \
2234 uint32_t cbOut = 0; \
2235 \
2236 mParent->consoleVRDPServer ()->QueryInfo \
2237 (_aIndex, NULL, 0, &cbOut); \
2238 \
2239 if (cbOut == 0) \
2240 { \
2241 Bstr str(""); \
2242 str.cloneTo(a##_aName); \
2243 return S_OK; \
2244 } \
2245 \
2246 char *pchBuffer = (char *)RTMemTmpAlloc (cbOut); \
2247 \
2248 if (!pchBuffer) \
2249 { \
2250 Log(("RemoteDisplayInfo::" \
2251 #_aName \
2252 ": Failed to allocate memory %d bytes\n", cbOut)); \
2253 return E_OUTOFMEMORY; \
2254 } \
2255 \
2256 mParent->consoleVRDPServer ()->QueryInfo \
2257 (_aIndex, pchBuffer, cbOut, &cbOut); \
2258 \
2259 Bstr str(pchBuffer); \
2260 \
2261 str.cloneTo(a##_aName); \
2262 \
2263 RTMemTmpFree (pchBuffer); \
2264 \
2265 return S_OK; \
2266 }
2267
2268IMPL_GETTER_BOOL (BOOL, Active, VRDP_QI_ACTIVE);
2269IMPL_GETTER_SCALAR (LONG, Port, VRDP_QI_PORT);
2270IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDP_QI_NUMBER_OF_CLIENTS);
2271IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDP_QI_BEGIN_TIME);
2272IMPL_GETTER_SCALAR (LONG64, EndTime, VRDP_QI_END_TIME);
2273IMPL_GETTER_SCALAR (ULONG64, BytesSent, VRDP_QI_BYTES_SENT);
2274IMPL_GETTER_SCALAR (ULONG64, BytesSentTotal, VRDP_QI_BYTES_SENT_TOTAL);
2275IMPL_GETTER_SCALAR (ULONG64, BytesReceived, VRDP_QI_BYTES_RECEIVED);
2276IMPL_GETTER_SCALAR (ULONG64, BytesReceivedTotal, VRDP_QI_BYTES_RECEIVED_TOTAL);
2277IMPL_GETTER_BSTR (BSTR, User, VRDP_QI_USER);
2278IMPL_GETTER_BSTR (BSTR, Domain, VRDP_QI_DOMAIN);
2279IMPL_GETTER_BSTR (BSTR, ClientName, VRDP_QI_CLIENT_NAME);
2280IMPL_GETTER_BSTR (BSTR, ClientIP, VRDP_QI_CLIENT_IP);
2281IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDP_QI_CLIENT_VERSION);
2282IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDP_QI_ENCRYPTION_STYLE);
2283
2284#undef IMPL_GETTER_BSTR
2285#undef IMPL_GETTER_SCALAR
2286/* 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