VirtualBox

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

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

com/Guid: remove conversion operators to eliminate any compiler surprises. Caught one totally unexpected issue which was indirectly caused by the similar Bstr cleanup, as there still is a Bstr->Guid conversion and thus the Guid magic was triggered

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 72.5 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 32780 2010-09-27 19:00:22Z 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#include <iprt/cpp/utils.h>
33
34#include <VBox/err.h>
35#ifdef VBOX_WITH_VRDP
36#include <VBox/VRDPOrders.h>
37#endif /* VBOX_WITH_VRDP */
38
39class VRDPConsoleListener :
40 VBOX_SCRIPTABLE_IMPL(IEventListener)
41{
42public:
43 VRDPConsoleListener(ConsoleVRDPServer *server)
44 : m_server(server)
45 {
46#ifndef VBOX_WITH_XPCOM
47 refcnt = 0;
48#endif /* !VBOX_WITH_XPCOM */
49 }
50
51 virtual ~VRDPConsoleListener() {}
52
53 NS_DECL_ISUPPORTS
54
55#ifndef VBOX_WITH_XPCOM
56 STDMETHOD_(ULONG, AddRef)() {
57 return ::InterlockedIncrement(&refcnt);
58 }
59 STDMETHOD_(ULONG, Release)()
60 {
61 long cnt = ::InterlockedDecrement(&refcnt);
62 if (cnt == 0)
63 delete this;
64 return cnt;
65 }
66 STDMETHOD(QueryInterface)(REFIID riid , void **ppObj)
67 {
68 if (riid == IID_IUnknown) {
69 *ppObj = (IUnknown*)this;
70 AddRef();
71 return S_OK;
72 }
73 if (riid == IID_IEventListener) {
74 *ppObj = (IEventListener*)this;
75 AddRef();
76 return S_OK;
77 }
78 *ppObj = NULL;
79 return E_NOINTERFACE;
80 }
81#endif /* !VBOX_WITH_XPCOM */
82
83
84 STDMETHOD(HandleEvent)(IEvent * aEvent)
85 {
86 VBoxEventType_T aType = VBoxEventType_Invalid;
87
88 aEvent->COMGETTER(Type)(&aType);
89 switch (aType)
90 {
91 case VBoxEventType_OnMousePointerShapeChanged:
92 {
93 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
94 Assert(mpscev);
95 BOOL visible, alpha;
96 ULONG xHot, yHot, width, height;
97 com::SafeArray <BYTE> shape;
98
99 mpscev->COMGETTER(Visible)(&visible);
100 mpscev->COMGETTER(Alpha)(&alpha);
101 mpscev->COMGETTER(Xhot)(&xHot);
102 mpscev->COMGETTER(Yhot)(&yHot);
103 mpscev->COMGETTER(Width)(&width);
104 mpscev->COMGETTER(Height)(&height);
105 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
106
107 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
108 break;
109 }
110 case VBoxEventType_OnMouseCapabilityChanged:
111 {
112 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
113 Assert(mccev);
114 if (m_server)
115 {
116 BOOL fAbsoluteMouse;
117 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
118 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
119 }
120 break;
121 }
122 case VBoxEventType_OnKeyboardLedsChanged:
123 {
124 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
125 Assert(klcev);
126
127 if (m_server)
128 {
129 BOOL fNumLock, fCapsLock, fScrollLock;
130 klcev->COMGETTER(NumLock)(&fNumLock);
131 klcev->COMGETTER(CapsLock)(&fCapsLock);
132 klcev->COMGETTER(ScrollLock)(&fScrollLock);
133 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
134 }
135 break;
136 }
137
138 default:
139 AssertFailed();
140 }
141
142 return S_OK;
143 }
144
145private:
146 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
147 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
148 ConsoleVRDPServer *m_server;
149#ifndef VBOX_WITH_XPCOM
150 long refcnt;
151#endif /* !VBOX_WITH_XPCOM */
152};
153
154#ifdef VBOX_WITH_XPCOM
155#include <nsMemory.h>
156NS_DECL_CLASSINFO(VRDPConsoleListener)
157NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VRDPConsoleListener, IEventListener)
158#endif /* VBOX_WITH_XPCOM */
159
160#ifdef DEBUG_sunlover
161#define LOGDUMPPTR Log
162void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
163{
164 unsigned i;
165
166 const uint8_t *pu8And = pu8Shape;
167
168 for (i = 0; i < height; i++)
169 {
170 unsigned j;
171 LOGDUMPPTR(("%p: ", pu8And));
172 for (j = 0; j < (width + 7) / 8; j++)
173 {
174 unsigned k;
175 for (k = 0; k < 8; k++)
176 {
177 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
178 }
179
180 pu8And++;
181 }
182 LOGDUMPPTR(("\n"));
183 }
184
185 if (fXorMaskRGB32)
186 {
187 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
188
189 for (i = 0; i < height; i++)
190 {
191 unsigned j;
192 LOGDUMPPTR(("%p: ", pu32Xor));
193 for (j = 0; j < width; j++)
194 {
195 LOGDUMPPTR(("%08X", *pu32Xor++));
196 }
197 LOGDUMPPTR(("\n"));
198 }
199 }
200 else
201 {
202 /* RDP 24 bit RGB mask. */
203 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
204 for (i = 0; i < height; i++)
205 {
206 unsigned j;
207 LOGDUMPPTR(("%p: ", pu8Xor));
208 for (j = 0; j < width; j++)
209 {
210 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
211 pu8Xor += 3;
212 }
213 LOGDUMPPTR(("\n"));
214 }
215 }
216}
217#else
218#define dumpPointer(a, b, c, d) do {} while (0)
219#endif /* DEBUG_sunlover */
220
221static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
222{
223 /*
224 * Find the top border of the AND mask. First assign to special value.
225 */
226 uint32_t ySkipAnd = ~0;
227
228 const uint8_t *pu8And = pu8AndMask;
229 const uint32_t cbAndRow = (width + 7) / 8;
230 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
231
232 Assert(cbAndRow > 0);
233
234 unsigned y;
235 unsigned x;
236
237 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
238 {
239 /* For each complete byte in the row. */
240 for (x = 0; x < cbAndRow - 1; x++)
241 {
242 if (pu8And[x] != 0xFF)
243 {
244 ySkipAnd = y;
245 break;
246 }
247 }
248
249 if (ySkipAnd == ~(uint32_t)0)
250 {
251 /* Last byte. */
252 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
253 {
254 ySkipAnd = y;
255 }
256 }
257 }
258
259 if (ySkipAnd == ~(uint32_t)0)
260 {
261 ySkipAnd = 0;
262 }
263
264 /*
265 * Find the left border of the AND mask.
266 */
267 uint32_t xSkipAnd = ~0;
268
269 /* For all bit columns. */
270 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
271 {
272 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
273 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
274
275 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
276 {
277 if ((*pu8And & mask) == 0)
278 {
279 xSkipAnd = x;
280 break;
281 }
282 }
283 }
284
285 if (xSkipAnd == ~(uint32_t)0)
286 {
287 xSkipAnd = 0;
288 }
289
290 /*
291 * Find the XOR mask top border.
292 */
293 uint32_t ySkipXor = ~0;
294
295 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
296
297 uint32_t *pu32Xor = pu32XorStart;
298
299 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
300 {
301 for (x = 0; x < width; x++)
302 {
303 if (pu32Xor[x] != 0)
304 {
305 ySkipXor = y;
306 break;
307 }
308 }
309 }
310
311 if (ySkipXor == ~(uint32_t)0)
312 {
313 ySkipXor = 0;
314 }
315
316 /*
317 * Find the left border of the XOR mask.
318 */
319 uint32_t xSkipXor = ~(uint32_t)0;
320
321 /* For all columns. */
322 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
323 {
324 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
325
326 for (y = ySkipXor; y < height; y++, pu32Xor += width)
327 {
328 if (*pu32Xor != 0)
329 {
330 xSkipXor = x;
331 break;
332 }
333 }
334 }
335
336 if (xSkipXor == ~(uint32_t)0)
337 {
338 xSkipXor = 0;
339 }
340
341 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
342 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
343}
344
345/* Generate an AND mask for alpha pointers here, because
346 * guest driver does not do that correctly for Vista pointers.
347 * Similar fix, changing the alpha threshold, could be applied
348 * for the guest driver, but then additions reinstall would be
349 * necessary, which we try to avoid.
350 */
351static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
352{
353 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
354
355 int y;
356 for (y = 0; y < h; y++)
357 {
358 uint8_t bitmask = 0x80;
359
360 int x;
361 for (x = 0; x < w; x++, bitmask >>= 1)
362 {
363 if (bitmask == 0)
364 {
365 bitmask = 0x80;
366 }
367
368 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
369 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
370 {
371 pu8DstAndMask[x / 8] &= ~bitmask;
372 }
373 }
374
375 /* Point to next source and dest scans. */
376 pu8SrcAlpha += w * 4;
377 pu8DstAndMask += (w + 7) / 8;
378 }
379}
380
381STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
382 BOOL alpha,
383 ULONG xHot,
384 ULONG yHot,
385 ULONG width,
386 ULONG height,
387 ComSafeArrayIn(BYTE,inShape))
388{
389 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
390
391 if (m_server)
392 {
393 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
394 if (aShape.size() == 0)
395 {
396 if (!visible)
397 {
398 m_server->MousePointerHide();
399 }
400 }
401 else if (width != 0 && height != 0)
402 {
403 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
404 * 'shape' AND mask followed by XOR mask.
405 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
406 *
407 * We convert this to RDP color format which consist of
408 * one bpp AND mask and 24 BPP (BGR) color XOR image.
409 *
410 * RDP clients expect 8 aligned width and height of
411 * pointer (preferably 32x32).
412 *
413 * They even contain bugs which do not appear for
414 * 32x32 pointers but would appear for a 41x32 one.
415 *
416 * So set pointer size to 32x32. This can be done safely
417 * because most pointers are 32x32.
418 */
419 uint8_t* shape = aShape.raw();
420
421 dumpPointer(shape, width, height, true);
422
423 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
424
425 uint8_t *pu8AndMask = shape;
426 uint8_t *pu8XorMask = shape + cbDstAndMask;
427
428 if (alpha)
429 {
430 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
431
432 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
433 }
434
435 /* Windows guest alpha pointers are wider than 32 pixels.
436 * Try to find out the top-left border of the pointer and
437 * then copy only meaningful bits. All complete top rows
438 * and all complete left columns where (AND == 1 && XOR == 0)
439 * are skipped. Hot spot is adjusted.
440 */
441 uint32_t ySkip = 0; /* How many rows to skip at the top. */
442 uint32_t xSkip = 0; /* How many columns to skip at the left. */
443
444 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
445
446 /* Must not skip the hot spot. */
447 xSkip = RT_MIN(xSkip, xHot);
448 ySkip = RT_MIN(ySkip, yHot);
449
450 /*
451 * Compute size and allocate memory for the pointer.
452 */
453 const uint32_t dstwidth = 32;
454 const uint32_t dstheight = 32;
455
456 VRDPCOLORPOINTER *pointer = NULL;
457
458 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
459
460 uint32_t rdpmaskwidth = dstmaskwidth;
461 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
462
463 uint32_t rdpdatawidth = dstwidth * 3;
464 uint32_t rdpdatalen = dstheight * rdpdatawidth;
465
466 pointer = (VRDPCOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDPCOLORPOINTER) + rdpmasklen + rdpdatalen);
467
468 if (pointer)
469 {
470 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDPCOLORPOINTER);
471 uint8_t *dataarray = maskarray + rdpmasklen;
472
473 memset(maskarray, 0xFF, rdpmasklen);
474 memset(dataarray, 0x00, rdpdatalen);
475
476 uint32_t srcmaskwidth = (width + 7) / 8;
477 uint32_t srcdatawidth = width * 4;
478
479 /* Copy AND mask. */
480 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
481 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
482
483 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
484 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
485
486 unsigned x, y;
487
488 for (y = 0; y < minheight; y++)
489 {
490 for (x = 0; x < minwidth; x++)
491 {
492 uint32_t byteIndex = (x + xSkip) / 8;
493 uint32_t bitIndex = (x + xSkip) % 8;
494
495 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
496
497 if (!bit)
498 {
499 byteIndex = x / 8;
500 bitIndex = x % 8;
501
502 dst[byteIndex] &= ~(1 << (7 - bitIndex));
503 }
504 }
505
506 src += srcmaskwidth;
507 dst -= rdpmaskwidth;
508 }
509
510 /* Point src to XOR mask */
511 src = pu8XorMask + ySkip * srcdatawidth;
512 dst = dataarray + (dstheight - 1) * rdpdatawidth;
513
514 for (y = 0; y < minheight ; y++)
515 {
516 for (x = 0; x < minwidth; x++)
517 {
518 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
519 }
520
521 src += srcdatawidth;
522 dst -= rdpdatawidth;
523 }
524
525 pointer->u16HotX = (uint16_t)(xHot - xSkip);
526 pointer->u16HotY = (uint16_t)(yHot - ySkip);
527
528 pointer->u16Width = (uint16_t)dstwidth;
529 pointer->u16Height = (uint16_t)dstheight;
530
531 pointer->u16MaskLen = (uint16_t)rdpmasklen;
532 pointer->u16DataLen = (uint16_t)rdpdatalen;
533
534 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
535
536 m_server->MousePointerUpdate(pointer);
537
538 RTMemTmpFree(pointer);
539 }
540 }
541 }
542
543 return S_OK;
544}
545
546
547// ConsoleVRDPServer
548////////////////////////////////////////////////////////////////////////////////
549
550#ifdef VBOX_WITH_VRDP
551RTLDRMOD ConsoleVRDPServer::mVRDPLibrary;
552
553PFNVRDPCREATESERVER ConsoleVRDPServer::mpfnVRDPCreateServer = NULL;
554
555VRDPENTRYPOINTS_1 *ConsoleVRDPServer::mpEntryPoints = NULL;
556
557VRDPCALLBACKS_1 ConsoleVRDPServer::mCallbacks =
558{
559 { VRDP_INTERFACE_VERSION_1, sizeof(VRDPCALLBACKS_1) },
560 ConsoleVRDPServer::VRDPCallbackQueryProperty,
561 ConsoleVRDPServer::VRDPCallbackClientLogon,
562 ConsoleVRDPServer::VRDPCallbackClientConnect,
563 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
564 ConsoleVRDPServer::VRDPCallbackIntercept,
565 ConsoleVRDPServer::VRDPCallbackUSB,
566 ConsoleVRDPServer::VRDPCallbackClipboard,
567 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
568 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
569 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
570 ConsoleVRDPServer::VRDPCallbackInput,
571 ConsoleVRDPServer::VRDPCallbackVideoModeHint
572};
573
574DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
575{
576 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
577
578 int rc = VERR_NOT_SUPPORTED;
579
580 switch (index)
581 {
582 case VRDP_QP_NETWORK_PORT:
583 {
584 /* This is obsolete, the VRDP server uses VRDP_QP_NETWORK_PORT_RANGE instead. */
585 ULONG port = 0;
586
587 if (cbBuffer >= sizeof(uint32_t))
588 {
589 *(uint32_t *)pvBuffer = (uint32_t)port;
590 rc = VINF_SUCCESS;
591 }
592 else
593 {
594 rc = VINF_BUFFER_OVERFLOW;
595 }
596
597 *pcbOut = sizeof(uint32_t);
598 } break;
599
600 case VRDP_QP_NETWORK_ADDRESS:
601 {
602 com::Bstr bstr;
603 server->mConsole->getVRDPServer()->COMGETTER(NetAddress)(bstr.asOutParam());
604
605 /* The server expects UTF8. */
606 com::Utf8Str address = bstr;
607
608 size_t cbAddress = address.length() + 1;
609
610 if (cbAddress >= 0x10000)
611 {
612 /* More than 64K seems to be an invalid address. */
613 rc = VERR_TOO_MUCH_DATA;
614 break;
615 }
616
617 if ((size_t)cbBuffer >= cbAddress)
618 {
619 memcpy(pvBuffer, address.c_str(), cbAddress);
620 rc = VINF_SUCCESS;
621 }
622 else
623 {
624 rc = VINF_BUFFER_OVERFLOW;
625 }
626
627 *pcbOut = (uint32_t)cbAddress;
628 } break;
629
630 case VRDP_QP_NUMBER_MONITORS:
631 {
632 ULONG cMonitors = 1;
633
634 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
635
636 if (cbBuffer >= sizeof(uint32_t))
637 {
638 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
639 rc = VINF_SUCCESS;
640 }
641 else
642 {
643 rc = VINF_BUFFER_OVERFLOW;
644 }
645
646 *pcbOut = sizeof(uint32_t);
647 } break;
648
649 case VRDP_QP_NETWORK_PORT_RANGE:
650 {
651 com::Bstr bstr;
652 HRESULT hrc = server->mConsole->getVRDPServer()->COMGETTER(Ports)(bstr.asOutParam());
653
654 if (hrc != S_OK)
655 {
656 bstr = "";
657 }
658
659 if (bstr == "0")
660 {
661 bstr = "3389";
662 }
663
664 /* The server expects UTF8. */
665 com::Utf8Str portRange = bstr;
666
667 size_t cbPortRange = portRange.length() + 1;
668
669 if (cbPortRange >= 0x10000)
670 {
671 /* More than 64K seems to be an invalid port range string. */
672 rc = VERR_TOO_MUCH_DATA;
673 break;
674 }
675
676 if ((size_t)cbBuffer >= cbPortRange)
677 {
678 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
679 rc = VINF_SUCCESS;
680 }
681 else
682 {
683 rc = VINF_BUFFER_OVERFLOW;
684 }
685
686 *pcbOut = (uint32_t)cbPortRange;
687 } break;
688
689#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
690 case VRDP_QP_VIDEO_CHANNEL:
691 {
692 BOOL fVideoEnabled = FALSE;
693
694 server->mConsole->getVRDPServer()->COMGETTER(VideoChannel)(&fVideoEnabled);
695
696 if (cbBuffer >= sizeof(uint32_t))
697 {
698 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
699 rc = VINF_SUCCESS;
700 }
701 else
702 {
703 rc = VINF_BUFFER_OVERFLOW;
704 }
705
706 *pcbOut = sizeof(uint32_t);
707 } break;
708
709 case VRDP_QP_VIDEO_CHANNEL_QUALITY:
710 {
711 ULONG ulQuality = 0;
712
713 server->mConsole->getVRDPServer()->COMGETTER(VideoChannelQuality)(&ulQuality);
714
715 if (cbBuffer >= sizeof(uint32_t))
716 {
717 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
718 rc = VINF_SUCCESS;
719 }
720 else
721 {
722 rc = VINF_BUFFER_OVERFLOW;
723 }
724
725 *pcbOut = sizeof(uint32_t);
726 } break;
727
728 case VRDP_QP_VIDEO_CHANNEL_SUNFLSH:
729 {
730 ULONG ulSunFlsh = 1;
731
732 com::Bstr bstr;
733 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
734 bstr.asOutParam());
735 if (hrc == S_OK && !bstr.isEmpty())
736 {
737 com::Utf8Str sunFlsh = bstr;
738 if (!sunFlsh.isEmpty())
739 {
740 ulSunFlsh = sunFlsh.toUInt32();
741 }
742 }
743
744 if (cbBuffer >= sizeof(uint32_t))
745 {
746 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
747 rc = VINF_SUCCESS;
748 }
749 else
750 {
751 rc = VINF_BUFFER_OVERFLOW;
752 }
753
754 *pcbOut = sizeof(uint32_t);
755 } break;
756#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
757
758 case VRDP_QP_FEATURE:
759 {
760 if (cbBuffer < sizeof(VRDPFEATURE))
761 {
762 rc = VERR_INVALID_PARAMETER;
763 break;
764 }
765
766 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDPFEATURE, achInfo);
767
768 VRDPFEATURE *pFeature = (VRDPFEATURE *)pvBuffer;
769
770 size_t cchInfo = 0;
771 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
772
773 if (RT_FAILURE(rc))
774 {
775 rc = VERR_INVALID_PARAMETER;
776 break;
777 }
778
779 /* features are mapped to "VRDP/Feature/NAME" extra data. */
780 com::Utf8Str extraData("VRDP/Feature/");
781 extraData += pFeature->achInfo;
782
783 com::Bstr bstrValue;
784
785 /* @todo these features should be per client. */
786 NOREF(pFeature->u32ClientId);
787
788 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
789 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
790 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
791 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
792 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
793 )
794 {
795 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
796 bstrValue.asOutParam());
797 if (hrc == S_OK && !bstrValue.isEmpty())
798 {
799 rc = VINF_SUCCESS;
800 }
801 }
802 else
803 {
804 rc = VERR_NOT_SUPPORTED;
805 }
806
807 /* Copy the value string to the callers buffer. */
808 if (rc == VINF_SUCCESS)
809 {
810 com::Utf8Str value = bstrValue;
811
812 size_t cb = value.length() + 1;
813
814 if ((size_t)cbInfo >= cb)
815 {
816 memcpy(pFeature->achInfo, value.c_str(), cb);
817 }
818 else
819 {
820 rc = VINF_BUFFER_OVERFLOW;
821 }
822
823 *pcbOut = (uint32_t)cb;
824 }
825 } break;
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 {
1204 ComPtr<IEventSource> es;
1205 console->COMGETTER(EventSource)(es.asOutParam());
1206 mConsoleListener = new VRDPConsoleListener(this);
1207 mConsoleListener->AddRef();
1208 com::SafeArray <VBoxEventType_T> eventTypes;
1209 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1210 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1211 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1212 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1213 }
1214
1215 mVRDPBindPort = -1;
1216#endif /* VBOX_WITH_VRDP */
1217
1218 mAuthLibrary = 0;
1219}
1220
1221ConsoleVRDPServer::~ConsoleVRDPServer()
1222{
1223 Stop();
1224
1225#ifdef VBOX_WITH_VRDP
1226 if (mConsoleListener)
1227 {
1228 ComPtr<IEventSource> es;
1229 mConsole->COMGETTER(EventSource)(es.asOutParam());
1230 es->UnregisterListener(mConsoleListener);
1231 mConsoleListener->Release();
1232 mConsoleListener = NULL;
1233 }
1234
1235 unsigned i;
1236 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1237 {
1238 if (maFramebuffers[i])
1239 {
1240 maFramebuffers[i]->Release();
1241 maFramebuffers[i] = NULL;
1242 }
1243 }
1244#endif /* VBOX_WITH_VRDP */
1245
1246 if (RTCritSectIsInitialized(&mCritSect))
1247 {
1248 RTCritSectDelete(&mCritSect);
1249 memset(&mCritSect, 0, sizeof(mCritSect));
1250 }
1251}
1252
1253int ConsoleVRDPServer::Launch(void)
1254{
1255 LogFlowThisFunc(("\n"));
1256#ifdef VBOX_WITH_VRDP
1257 int rc = VINF_SUCCESS;
1258 IVRDPServer *vrdpserver = mConsole->getVRDPServer();
1259 Assert(vrdpserver);
1260 BOOL vrdpEnabled = FALSE;
1261
1262 HRESULT rc2 = vrdpserver->COMGETTER(Enabled)(&vrdpEnabled);
1263 AssertComRC(rc2);
1264
1265 if (SUCCEEDED(rc2) && vrdpEnabled)
1266 {
1267 if (loadVRDPLibrary())
1268 {
1269 rc = mpfnVRDPCreateServer(&mCallbacks.header, this, (VRDPINTERFACEHDR **)&mpEntryPoints, &mhServer);
1270
1271 if (RT_SUCCESS(rc))
1272 {
1273#ifdef VBOX_WITH_USB
1274 remoteUSBThreadStart();
1275#endif /* VBOX_WITH_USB */
1276 }
1277 else if (rc != VERR_NET_ADDRESS_IN_USE)
1278 AssertMsgFailed(("Could not start VRDP server: rc = %Rrc\n", rc));
1279 }
1280 else
1281 {
1282 AssertMsgFailed(("Could not load the VRDP library\n"));
1283 rc = VERR_FILE_NOT_FOUND;
1284 }
1285 }
1286#else
1287 int rc = VERR_NOT_SUPPORTED;
1288 LogRel(("VRDP: this version does not include the VRDP server.\n"));
1289#endif /* VBOX_WITH_VRDP */
1290 return rc;
1291}
1292
1293void ConsoleVRDPServer::EnableConnections(void)
1294{
1295#ifdef VBOX_WITH_VRDP
1296 if (mpEntryPoints && mhServer)
1297 {
1298 mpEntryPoints->VRDPEnableConnections(mhServer, true);
1299 }
1300#endif /* VBOX_WITH_VRDP */
1301}
1302
1303void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
1304{
1305#ifdef VBOX_WITH_VRDP
1306 if (mpEntryPoints && mhServer)
1307 {
1308 mpEntryPoints->VRDPDisconnect(mhServer, u32ClientId, fReconnect);
1309 }
1310#endif /* VBOX_WITH_VRDP */
1311}
1312
1313void ConsoleVRDPServer::MousePointerUpdate(const VRDPCOLORPOINTER *pPointer)
1314{
1315#ifdef VBOX_WITH_VRDP
1316 if (mpEntryPoints && mhServer)
1317 {
1318 mpEntryPoints->VRDPColorPointer(mhServer, pPointer);
1319 }
1320#endif /* VBOX_WITH_VRDP */
1321}
1322
1323void ConsoleVRDPServer::MousePointerHide(void)
1324{
1325#ifdef VBOX_WITH_VRDP
1326 if (mpEntryPoints && mhServer)
1327 {
1328 mpEntryPoints->VRDPHidePointer(mhServer);
1329 }
1330#endif /* VBOX_WITH_VRDP */
1331}
1332
1333void ConsoleVRDPServer::Stop(void)
1334{
1335 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1336 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1337#ifdef VBOX_WITH_VRDP
1338 if (mhServer)
1339 {
1340 HVRDPSERVER hServer = mhServer;
1341
1342 /* Reset the handle to avoid further calls to the server. */
1343 mhServer = 0;
1344
1345 if (mpEntryPoints && hServer)
1346 {
1347 mpEntryPoints->VRDPDestroy(hServer);
1348 }
1349 }
1350#endif /* VBOX_WITH_VRDP */
1351
1352#ifdef VBOX_WITH_USB
1353 remoteUSBThreadStop();
1354#endif /* VBOX_WITH_USB */
1355
1356 mpfnAuthEntry = NULL;
1357 mpfnAuthEntry2 = NULL;
1358
1359 if (mAuthLibrary)
1360 {
1361 RTLdrClose(mAuthLibrary);
1362 mAuthLibrary = 0;
1363 }
1364}
1365
1366/* Worker thread for Remote USB. The thread polls the clients for
1367 * the list of attached USB devices.
1368 * The thread is also responsible for attaching/detaching devices
1369 * to/from the VM.
1370 *
1371 * It is expected that attaching/detaching is not a frequent operation.
1372 *
1373 * The thread is always running when the VRDP server is active.
1374 *
1375 * The thread scans backends and requests the device list every 2 seconds.
1376 *
1377 * When device list is available, the thread calls the Console to process it.
1378 *
1379 */
1380#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1381
1382#ifdef VBOX_WITH_USB
1383static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
1384{
1385 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1386
1387 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1388
1389 pOwner->notifyRemoteUSBThreadRunning(self);
1390
1391 while (pOwner->isRemoteUSBThreadRunning())
1392 {
1393 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1394
1395 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
1396 {
1397 pRemoteUSBBackend->PollRemoteDevices();
1398 }
1399
1400 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
1401
1402 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
1403 }
1404
1405 return VINF_SUCCESS;
1406}
1407
1408void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
1409{
1410 mUSBBackends.thread = thread;
1411 mUSBBackends.fThreadRunning = true;
1412 int rc = RTThreadUserSignal(thread);
1413 AssertRC(rc);
1414}
1415
1416bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
1417{
1418 return mUSBBackends.fThreadRunning;
1419}
1420
1421void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
1422{
1423 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
1424 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1425 NOREF(rc);
1426}
1427
1428void ConsoleVRDPServer::remoteUSBThreadStart(void)
1429{
1430 int rc = RTSemEventCreate(&mUSBBackends.event);
1431
1432 if (RT_FAILURE(rc))
1433 {
1434 AssertFailed();
1435 mUSBBackends.event = 0;
1436 }
1437
1438 if (RT_SUCCESS(rc))
1439 {
1440 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
1441 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
1442 }
1443
1444 if (RT_FAILURE(rc))
1445 {
1446 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
1447 mUSBBackends.thread = NIL_RTTHREAD;
1448 }
1449 else
1450 {
1451 /* Wait until the thread is ready. */
1452 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
1453 AssertRC(rc);
1454 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
1455 }
1456}
1457
1458void ConsoleVRDPServer::remoteUSBThreadStop(void)
1459{
1460 mUSBBackends.fThreadRunning = false;
1461
1462 if (mUSBBackends.thread != NIL_RTTHREAD)
1463 {
1464 Assert (mUSBBackends.event != 0);
1465
1466 RTSemEventSignal(mUSBBackends.event);
1467
1468 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
1469 AssertRC(rc);
1470
1471 mUSBBackends.thread = NIL_RTTHREAD;
1472 }
1473
1474 if (mUSBBackends.event)
1475 {
1476 RTSemEventDestroy(mUSBBackends.event);
1477 mUSBBackends.event = 0;
1478 }
1479}
1480#endif /* VBOX_WITH_USB */
1481
1482VRDPAuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, VRDPAuthGuestJudgement guestJudgement,
1483 const char *pszUser, const char *pszPassword, const char *pszDomain,
1484 uint32_t u32ClientId)
1485{
1486 VRDPAUTHUUID rawuuid;
1487
1488 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1489
1490 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
1491 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
1492
1493 /*
1494 * Called only from VRDP input thread. So thread safety is not required.
1495 */
1496
1497 if (!mAuthLibrary)
1498 {
1499 /* Load the external authentication library. */
1500
1501 ComPtr<IMachine> machine;
1502 mConsole->COMGETTER(Machine)(machine.asOutParam());
1503
1504 ComPtr<IVirtualBox> virtualBox;
1505 machine->COMGETTER(Parent)(virtualBox.asOutParam());
1506
1507 ComPtr<ISystemProperties> systemProperties;
1508 virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
1509
1510 Bstr authLibrary;
1511 systemProperties->COMGETTER(RemoteDisplayAuthLibrary)(authLibrary.asOutParam());
1512
1513 Utf8Str filename = authLibrary;
1514
1515 LogRel(("VRDPAUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
1516
1517 int rc;
1518 if (RTPathHavePath(filename.c_str()))
1519 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
1520 else
1521 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
1522
1523 if (RT_FAILURE(rc))
1524 LogRel(("VRDPAUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
1525
1526 if (RT_SUCCESS(rc))
1527 {
1528 /* Get the entry point. */
1529 mpfnAuthEntry2 = NULL;
1530 int rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth2", (void**)&mpfnAuthEntry2);
1531 if (RT_FAILURE(rc2))
1532 {
1533 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1534 {
1535 LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth2", rc2));
1536 }
1537 rc = rc2;
1538 }
1539
1540 /* Get the entry point. */
1541 mpfnAuthEntry = NULL;
1542 rc2 = RTLdrGetSymbol(mAuthLibrary, "VRDPAuth", (void**)&mpfnAuthEntry);
1543 if (RT_FAILURE(rc2))
1544 {
1545 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1546 {
1547 LogRel(("VRDPAUTH: Could not resolve import '%s'. Error code: %Rrc\n", "VRDPAuth", rc2));
1548 }
1549 rc = rc2;
1550 }
1551
1552 if (mpfnAuthEntry2 || mpfnAuthEntry)
1553 {
1554 LogRel(("VRDPAUTH: Using entry point '%s'.\n", mpfnAuthEntry2? "VRDPAuth2": "VRDPAuth"));
1555 rc = VINF_SUCCESS;
1556 }
1557 }
1558
1559 if (RT_FAILURE(rc))
1560 {
1561 mConsole->setError(E_FAIL,
1562 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
1563 filename.c_str(),
1564 rc);
1565
1566 mpfnAuthEntry = NULL;
1567 mpfnAuthEntry2 = NULL;
1568
1569 if (mAuthLibrary)
1570 {
1571 RTLdrClose(mAuthLibrary);
1572 mAuthLibrary = 0;
1573 }
1574
1575 return VRDPAuthAccessDenied;
1576 }
1577 }
1578
1579 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1580
1581 VRDPAuthResult result = mpfnAuthEntry2?
1582 mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId):
1583 mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
1584
1585 switch (result)
1586 {
1587 case VRDPAuthAccessDenied:
1588 LogRel(("VRDPAUTH: external authentication module returned 'access denied'\n"));
1589 break;
1590 case VRDPAuthAccessGranted:
1591 LogRel(("VRDPAUTH: external authentication module returned 'access granted'\n"));
1592 break;
1593 case VRDPAuthDelegateToGuest:
1594 LogRel(("VRDPAUTH: external authentication module returned 'delegate request to guest'\n"));
1595 break;
1596 default:
1597 LogRel(("VRDPAUTH: external authentication module returned incorrect return code %d\n", result));
1598 result = VRDPAuthAccessDenied;
1599 }
1600
1601 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
1602
1603 return result;
1604}
1605
1606void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
1607{
1608 VRDPAUTHUUID rawuuid;
1609
1610 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1611
1612 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
1613 rawuuid, u32ClientId));
1614
1615 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2));
1616
1617 if (mpfnAuthEntry2)
1618 mpfnAuthEntry2(&rawuuid, VRDPAuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1619}
1620
1621int ConsoleVRDPServer::lockConsoleVRDPServer(void)
1622{
1623 int rc = RTCritSectEnter(&mCritSect);
1624 AssertRC(rc);
1625 return rc;
1626}
1627
1628void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
1629{
1630 RTCritSectLeave(&mCritSect);
1631}
1632
1633DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
1634 uint32_t u32ClientId,
1635 uint32_t u32Function,
1636 uint32_t u32Format,
1637 const void *pvData,
1638 uint32_t cbData)
1639{
1640 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
1641 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
1642
1643 int rc = VINF_SUCCESS;
1644
1645 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
1646
1647 NOREF(u32ClientId);
1648
1649 switch (u32Function)
1650 {
1651 case VRDP_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
1652 {
1653 if (pServer->mpfnClipboardCallback)
1654 {
1655 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1656 u32Format,
1657 (void *)pvData,
1658 cbData);
1659 }
1660 } break;
1661
1662 case VRDP_CLIPBOARD_FUNCTION_DATA_READ:
1663 {
1664 if (pServer->mpfnClipboardCallback)
1665 {
1666 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1667 u32Format,
1668 (void *)pvData,
1669 cbData);
1670 }
1671 } break;
1672
1673 default:
1674 rc = VERR_NOT_SUPPORTED;
1675 }
1676
1677 return rc;
1678}
1679
1680DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
1681 uint32_t u32Function,
1682 void *pvParms,
1683 uint32_t cbParms)
1684{
1685 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
1686 pvExtension, u32Function, pvParms, cbParms));
1687
1688 int rc = VINF_SUCCESS;
1689
1690#ifdef VBOX_WITH_VRDP
1691 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
1692
1693 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
1694
1695 switch (u32Function)
1696 {
1697 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
1698 {
1699 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
1700 } break;
1701
1702 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1703 {
1704 /* The guest announces clipboard formats. This must be delivered to all clients. */
1705 if (mpEntryPoints && pServer->mhServer)
1706 {
1707 mpEntryPoints->VRDPClipboard(pServer->mhServer,
1708 VRDP_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
1709 pParms->u32Format,
1710 NULL,
1711 0,
1712 NULL);
1713 }
1714 } break;
1715
1716 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1717 {
1718 /* The clipboard service expects that the pvData buffer will be filled
1719 * with clipboard data. The server returns the data from the client that
1720 * announced the requested format most recently.
1721 */
1722 if (mpEntryPoints && pServer->mhServer)
1723 {
1724 mpEntryPoints->VRDPClipboard(pServer->mhServer,
1725 VRDP_CLIPBOARD_FUNCTION_DATA_READ,
1726 pParms->u32Format,
1727 pParms->u.pvData,
1728 pParms->cbData,
1729 &pParms->cbData);
1730 }
1731 } break;
1732
1733 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
1734 {
1735 if (mpEntryPoints && pServer->mhServer)
1736 {
1737 mpEntryPoints->VRDPClipboard(pServer->mhServer,
1738 VRDP_CLIPBOARD_FUNCTION_DATA_WRITE,
1739 pParms->u32Format,
1740 pParms->u.pvData,
1741 pParms->cbData,
1742 NULL);
1743 }
1744 } break;
1745
1746 default:
1747 rc = VERR_NOT_SUPPORTED;
1748 }
1749#endif /* VBOX_WITH_VRDP */
1750
1751 return rc;
1752}
1753
1754void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
1755{
1756 int rc = lockConsoleVRDPServer();
1757
1758 if (RT_SUCCESS(rc))
1759 {
1760 if (mcClipboardRefs == 0)
1761 {
1762 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
1763
1764 if (RT_SUCCESS(rc))
1765 {
1766 mcClipboardRefs++;
1767 }
1768 }
1769
1770 unlockConsoleVRDPServer();
1771 }
1772}
1773
1774void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
1775{
1776 int rc = lockConsoleVRDPServer();
1777
1778 if (RT_SUCCESS(rc))
1779 {
1780 mcClipboardRefs--;
1781
1782 if (mcClipboardRefs == 0)
1783 {
1784 HGCMHostUnregisterServiceExtension(mhClipboard);
1785 }
1786
1787 unlockConsoleVRDPServer();
1788 }
1789}
1790
1791/* That is called on INPUT thread of the VRDP server.
1792 * The ConsoleVRDPServer keeps a list of created backend instances.
1793 */
1794void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
1795{
1796#ifdef VBOX_WITH_USB
1797 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
1798
1799 /* Create a new instance of the USB backend for the new client. */
1800 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
1801
1802 if (pRemoteUSBBackend)
1803 {
1804 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
1805
1806 /* Append the new instance in the list. */
1807 int rc = lockConsoleVRDPServer();
1808
1809 if (RT_SUCCESS(rc))
1810 {
1811 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
1812 if (mUSBBackends.pHead)
1813 {
1814 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
1815 }
1816 else
1817 {
1818 mUSBBackends.pTail = pRemoteUSBBackend;
1819 }
1820
1821 mUSBBackends.pHead = pRemoteUSBBackend;
1822
1823 unlockConsoleVRDPServer();
1824
1825 if (ppvIntercept)
1826 {
1827 *ppvIntercept = pRemoteUSBBackend;
1828 }
1829 }
1830
1831 if (RT_FAILURE(rc))
1832 {
1833 pRemoteUSBBackend->Release();
1834 }
1835 }
1836#endif /* VBOX_WITH_USB */
1837}
1838
1839void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
1840{
1841#ifdef VBOX_WITH_USB
1842 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
1843
1844 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1845
1846 /* Find the instance. */
1847 int rc = lockConsoleVRDPServer();
1848
1849 if (RT_SUCCESS(rc))
1850 {
1851 pRemoteUSBBackend = usbBackendFind(u32ClientId);
1852
1853 if (pRemoteUSBBackend)
1854 {
1855 /* Notify that it will be deleted. */
1856 pRemoteUSBBackend->NotifyDelete();
1857 }
1858
1859 unlockConsoleVRDPServer();
1860 }
1861
1862 if (pRemoteUSBBackend)
1863 {
1864 /* Here the instance has been excluded from the list and can be dereferenced. */
1865 pRemoteUSBBackend->Release();
1866 }
1867#endif
1868}
1869
1870void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
1871{
1872#ifdef VBOX_WITH_USB
1873 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1874
1875 /* Find the instance. */
1876 int rc = lockConsoleVRDPServer();
1877
1878 if (RT_SUCCESS(rc))
1879 {
1880 pRemoteUSBBackend = usbBackendFind(u32ClientId);
1881
1882 if (pRemoteUSBBackend)
1883 {
1884 /* Inform the backend instance that it is referenced by the Guid. */
1885 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
1886
1887 if (fAdded)
1888 {
1889 /* Reference the instance because its pointer is being taken. */
1890 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
1891 }
1892 else
1893 {
1894 pRemoteUSBBackend = NULL;
1895 }
1896 }
1897
1898 unlockConsoleVRDPServer();
1899 }
1900
1901 if (pRemoteUSBBackend)
1902 {
1903 return pRemoteUSBBackend->GetBackendCallbackPointer();
1904 }
1905
1906#endif
1907 return NULL;
1908}
1909
1910void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
1911{
1912#ifdef VBOX_WITH_USB
1913 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1914
1915 /* Find the instance. */
1916 int rc = lockConsoleVRDPServer();
1917
1918 if (RT_SUCCESS(rc))
1919 {
1920 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
1921
1922 if (pRemoteUSBBackend)
1923 {
1924 pRemoteUSBBackend->removeUUID(pGuid);
1925 }
1926
1927 unlockConsoleVRDPServer();
1928
1929 if (pRemoteUSBBackend)
1930 {
1931 pRemoteUSBBackend->Release();
1932 }
1933 }
1934#endif
1935}
1936
1937RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
1938{
1939 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
1940
1941 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
1942#ifdef VBOX_WITH_USB
1943
1944 int rc = lockConsoleVRDPServer();
1945
1946 if (RT_SUCCESS(rc))
1947 {
1948 if (pRemoteUSBBackend == NULL)
1949 {
1950 /* The first backend in the list is requested. */
1951 pNextRemoteUSBBackend = mUSBBackends.pHead;
1952 }
1953 else
1954 {
1955 /* Get pointer to the next backend. */
1956 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1957 }
1958
1959 if (pNextRemoteUSBBackend)
1960 {
1961 pNextRemoteUSBBackend->AddRef();
1962 }
1963
1964 unlockConsoleVRDPServer();
1965
1966 if (pRemoteUSBBackend)
1967 {
1968 pRemoteUSBBackend->Release();
1969 }
1970 }
1971#endif
1972
1973 return pNextRemoteUSBBackend;
1974}
1975
1976#ifdef VBOX_WITH_USB
1977/* Internal method. Called under the ConsoleVRDPServerLock. */
1978RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
1979{
1980 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
1981
1982 while (pRemoteUSBBackend)
1983 {
1984 if (pRemoteUSBBackend->ClientId() == u32ClientId)
1985 {
1986 break;
1987 }
1988
1989 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1990 }
1991
1992 return pRemoteUSBBackend;
1993}
1994
1995/* Internal method. Called under the ConsoleVRDPServerLock. */
1996RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
1997{
1998 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
1999
2000 while (pRemoteUSBBackend)
2001 {
2002 if (pRemoteUSBBackend->findUUID(pGuid))
2003 {
2004 break;
2005 }
2006
2007 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2008 }
2009
2010 return pRemoteUSBBackend;
2011}
2012#endif
2013
2014/* Internal method. Called by the backend destructor. */
2015void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2016{
2017#ifdef VBOX_WITH_USB
2018 int rc = lockConsoleVRDPServer();
2019 AssertRC(rc);
2020
2021 /* Exclude the found instance from the list. */
2022 if (pRemoteUSBBackend->pNext)
2023 {
2024 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2025 }
2026 else
2027 {
2028 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2029 }
2030
2031 if (pRemoteUSBBackend->pPrev)
2032 {
2033 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2034 }
2035 else
2036 {
2037 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2038 }
2039
2040 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2041
2042 unlockConsoleVRDPServer();
2043#endif
2044}
2045
2046
2047void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2048{
2049#ifdef VBOX_WITH_VRDP
2050 if (mpEntryPoints && mhServer)
2051 {
2052 mpEntryPoints->VRDPUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2053 }
2054#endif
2055}
2056
2057void ConsoleVRDPServer::SendResize(void) const
2058{
2059#ifdef VBOX_WITH_VRDP
2060 if (mpEntryPoints && mhServer)
2061 {
2062 mpEntryPoints->VRDPResize(mhServer);
2063 }
2064#endif
2065}
2066
2067void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2068{
2069#ifdef VBOX_WITH_VRDP
2070 VRDPORDERHDR update;
2071 update.x = x;
2072 update.y = y;
2073 update.w = w;
2074 update.h = h;
2075 if (mpEntryPoints && mhServer)
2076 {
2077 mpEntryPoints->VRDPUpdate(mhServer, uScreenId, &update, sizeof(update));
2078 }
2079#endif
2080}
2081
2082void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDPAUDIOFORMAT format) const
2083{
2084#ifdef VBOX_WITH_VRDP
2085 if (mpEntryPoints && mhServer)
2086 {
2087 mpEntryPoints->VRDPAudioSamples(mhServer, pvSamples, cSamples, format);
2088 }
2089#endif
2090}
2091
2092void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2093{
2094#ifdef VBOX_WITH_VRDP
2095 if (mpEntryPoints && mhServer)
2096 {
2097 mpEntryPoints->VRDPAudioVolume(mhServer, left, right);
2098 }
2099#endif
2100}
2101
2102void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2103{
2104#ifdef VBOX_WITH_VRDP
2105 if (mpEntryPoints && mhServer)
2106 {
2107 mpEntryPoints->VRDPUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2108 }
2109#endif
2110}
2111
2112void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2113{
2114#ifdef VBOX_WITH_VRDP
2115 if (index == VRDP_QI_PORT)
2116 {
2117 uint32_t cbOut = sizeof(int32_t);
2118
2119 if (cbBuffer >= cbOut)
2120 {
2121 *pcbOut = cbOut;
2122 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2123 }
2124 }
2125 else if (mpEntryPoints && mhServer)
2126 {
2127 mpEntryPoints->VRDPQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2128 }
2129#endif
2130}
2131
2132#ifdef VBOX_WITH_VRDP
2133/* note: static function now! */
2134bool ConsoleVRDPServer::loadVRDPLibrary(void)
2135{
2136 int rc = VINF_SUCCESS;
2137
2138 if (!mVRDPLibrary)
2139 {
2140 rc = SUPR3HardenedLdrLoadAppPriv("VBoxVRDP", &mVRDPLibrary);
2141
2142 if (RT_SUCCESS(rc))
2143 {
2144 LogFlow(("VRDPServer::loadLibrary(): successfully loaded VRDP library.\n"));
2145
2146 struct SymbolEntry
2147 {
2148 const char *name;
2149 void **ppfn;
2150 };
2151
2152 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2153
2154 static const struct SymbolEntry symbols[] =
2155 {
2156 DEFSYMENTRY(VRDPCreateServer)
2157 };
2158
2159 #undef DEFSYMENTRY
2160
2161 for (unsigned i = 0; i < RT_ELEMENTS(symbols); i++)
2162 {
2163 rc = RTLdrGetSymbol(mVRDPLibrary, symbols[i].name, symbols[i].ppfn);
2164
2165 AssertMsgRC(rc, ("Error resolving VRDP symbol %s\n", symbols[i].name));
2166
2167 if (RT_FAILURE(rc))
2168 {
2169 break;
2170 }
2171 }
2172 }
2173 else
2174 {
2175 LogRel(("VRDPServer::loadLibrary(): failed to load VRDP library! VRDP not available: rc = %Rrc\n", rc));
2176 mVRDPLibrary = NULL;
2177 }
2178 }
2179
2180 // just to be safe
2181 if (RT_FAILURE(rc))
2182 {
2183 if (mVRDPLibrary)
2184 {
2185 RTLdrClose(mVRDPLibrary);
2186 mVRDPLibrary = NULL;
2187 }
2188 }
2189
2190 return (mVRDPLibrary != NULL);
2191}
2192#endif /* VBOX_WITH_VRDP */
2193
2194/*
2195 * IRemoteDisplayInfo implementation.
2196 */
2197// constructor / destructor
2198/////////////////////////////////////////////////////////////////////////////
2199
2200RemoteDisplayInfo::RemoteDisplayInfo()
2201 : mParent(NULL)
2202{
2203}
2204
2205RemoteDisplayInfo::~RemoteDisplayInfo()
2206{
2207}
2208
2209
2210HRESULT RemoteDisplayInfo::FinalConstruct()
2211{
2212 return S_OK;
2213}
2214
2215void RemoteDisplayInfo::FinalRelease()
2216{
2217 uninit();
2218}
2219
2220// public methods only for internal purposes
2221/////////////////////////////////////////////////////////////////////////////
2222
2223/**
2224 * Initializes the guest object.
2225 */
2226HRESULT RemoteDisplayInfo::init(Console *aParent)
2227{
2228 LogFlowThisFunc(("aParent=%p\n", aParent));
2229
2230 ComAssertRet(aParent, E_INVALIDARG);
2231
2232 /* Enclose the state transition NotReady->InInit->Ready */
2233 AutoInitSpan autoInitSpan(this);
2234 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2235
2236 unconst(mParent) = aParent;
2237
2238 /* Confirm a successful initialization */
2239 autoInitSpan.setSucceeded();
2240
2241 return S_OK;
2242}
2243
2244/**
2245 * Uninitializes the instance and sets the ready flag to FALSE.
2246 * Called either from FinalRelease() or by the parent when it gets destroyed.
2247 */
2248void RemoteDisplayInfo::uninit()
2249{
2250 LogFlowThisFunc(("\n"));
2251
2252 /* Enclose the state transition Ready->InUninit->NotReady */
2253 AutoUninitSpan autoUninitSpan(this);
2254 if (autoUninitSpan.uninitDone())
2255 return;
2256
2257 unconst(mParent) = NULL;
2258}
2259
2260// IRemoteDisplayInfo properties
2261/////////////////////////////////////////////////////////////////////////////
2262
2263#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2264 STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName)(_aType *a##_aName) \
2265 { \
2266 if (!a##_aName) \
2267 return E_POINTER; \
2268 \
2269 AutoCaller autoCaller(this); \
2270 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2271 \
2272 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2273 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2274 \
2275 uint32_t value; \
2276 uint32_t cbOut = 0; \
2277 \
2278 mParent->consoleVRDPServer()->QueryInfo \
2279 (_aIndex, &value, sizeof(value), &cbOut); \
2280 \
2281 *a##_aName = cbOut? !!value: FALSE; \
2282 \
2283 return S_OK; \
2284 } \
2285 extern void IMPL_GETTER_BOOL_DUMMY(void)
2286
2287#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
2288 STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName)(_aType *a##_aName) \
2289 { \
2290 if (!a##_aName) \
2291 return E_POINTER; \
2292 \
2293 AutoCaller autoCaller(this); \
2294 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2295 \
2296 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2297 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2298 \
2299 _aType value; \
2300 uint32_t cbOut = 0; \
2301 \
2302 mParent->consoleVRDPServer()->QueryInfo \
2303 (_aIndex, &value, sizeof(value), &cbOut); \
2304 \
2305 if (_aValueMask) value &= (_aValueMask); \
2306 *a##_aName = cbOut? value: 0; \
2307 \
2308 return S_OK; \
2309 } \
2310 extern void IMPL_GETTER_SCALAR_DUMMY(void)
2311
2312#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2313 STDMETHODIMP RemoteDisplayInfo::COMGETTER(_aName)(_aType *a##_aName) \
2314 { \
2315 if (!a##_aName) \
2316 return E_POINTER; \
2317 \
2318 AutoCaller autoCaller(this); \
2319 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2320 \
2321 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2322 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2323 \
2324 uint32_t cbOut = 0; \
2325 \
2326 mParent->consoleVRDPServer()->QueryInfo \
2327 (_aIndex, NULL, 0, &cbOut); \
2328 \
2329 if (cbOut == 0) \
2330 { \
2331 Bstr str(""); \
2332 str.cloneTo(a##_aName); \
2333 return S_OK; \
2334 } \
2335 \
2336 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
2337 \
2338 if (!pchBuffer) \
2339 { \
2340 Log(("RemoteDisplayInfo::" \
2341 #_aName \
2342 ": Failed to allocate memory %d bytes\n", cbOut)); \
2343 return E_OUTOFMEMORY; \
2344 } \
2345 \
2346 mParent->consoleVRDPServer()->QueryInfo \
2347 (_aIndex, pchBuffer, cbOut, &cbOut); \
2348 \
2349 Bstr str(pchBuffer); \
2350 \
2351 str.cloneTo(a##_aName); \
2352 \
2353 RTMemTmpFree(pchBuffer); \
2354 \
2355 return S_OK; \
2356 } \
2357 extern void IMPL_GETTER_BSTR_DUMMY(void)
2358
2359IMPL_GETTER_BOOL (BOOL, Active, VRDP_QI_ACTIVE);
2360IMPL_GETTER_SCALAR (LONG, Port, VRDP_QI_PORT, 0);
2361IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDP_QI_NUMBER_OF_CLIENTS, 0);
2362IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDP_QI_BEGIN_TIME, 0);
2363IMPL_GETTER_SCALAR (LONG64, EndTime, VRDP_QI_END_TIME, 0);
2364IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDP_QI_BYTES_SENT, INT64_MAX);
2365IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDP_QI_BYTES_SENT_TOTAL, INT64_MAX);
2366IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDP_QI_BYTES_RECEIVED, INT64_MAX);
2367IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDP_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
2368IMPL_GETTER_BSTR (BSTR, User, VRDP_QI_USER);
2369IMPL_GETTER_BSTR (BSTR, Domain, VRDP_QI_DOMAIN);
2370IMPL_GETTER_BSTR (BSTR, ClientName, VRDP_QI_CLIENT_NAME);
2371IMPL_GETTER_BSTR (BSTR, ClientIP, VRDP_QI_CLIENT_IP);
2372IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDP_QI_CLIENT_VERSION, 0);
2373IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDP_QI_ENCRYPTION_STYLE, 0);
2374
2375#undef IMPL_GETTER_BSTR
2376#undef IMPL_GETTER_SCALAR
2377#undef IMPL_GETTER_BOOL
2378/* 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