VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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

© 2023 Oracle
ContactPrivacy policyTerms of Use