VirtualBox

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

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

VRDP video channel configuration API.

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