VirtualBox

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

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

compact headers

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette