VirtualBox

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

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

VRDP: video channel: enable SunFlsh support by default (xTracker 4606).

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