VirtualBox

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

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

VRDE: More API changes for the VRDP server separation.

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