VirtualBox

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

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

TCP ports range for VRDP server: delete VRDPPortRange extra data on VM startup.

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