VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ConsoleVRDPServer.cpp@ 35650

Last change on this file since 35650 was 35638, checked in by vboxsync, 14 years ago

Main. QT/FE: fix long standing COM issue

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 81.9 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 35638 2011-01-19 19:10:49Z 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#include "AudioSnifferInterface.h"
24#ifdef VBOX_WITH_EXTPACK
25# include "ExtPackManagerImpl.h"
26#endif
27
28#include "Global.h"
29#include "AutoCaller.h"
30#include "Logging.h"
31
32#include <iprt/asm.h>
33#include <iprt/alloca.h>
34#include <iprt/ldr.h>
35#include <iprt/param.h>
36#include <iprt/path.h>
37#include <iprt/cpp/utils.h>
38
39#include <VBox/err.h>
40#include <VBox/RemoteDesktop/VRDEOrders.h>
41#include <VBox/com/listeners.h>
42
43class VRDPConsoleListener
44{
45public:
46 VRDPConsoleListener(ConsoleVRDPServer *server)
47 : m_server(server)
48 {
49 }
50
51 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
52 {
53 switch (aType)
54 {
55 case VBoxEventType_OnMousePointerShapeChanged:
56 {
57 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
58 Assert(mpscev);
59 BOOL visible, alpha;
60 ULONG xHot, yHot, width, height;
61 com::SafeArray <BYTE> shape;
62
63 mpscev->COMGETTER(Visible)(&visible);
64 mpscev->COMGETTER(Alpha)(&alpha);
65 mpscev->COMGETTER(Xhot)(&xHot);
66 mpscev->COMGETTER(Yhot)(&yHot);
67 mpscev->COMGETTER(Width)(&width);
68 mpscev->COMGETTER(Height)(&height);
69 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
70
71 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
72 break;
73 }
74 case VBoxEventType_OnMouseCapabilityChanged:
75 {
76 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
77 Assert(mccev);
78 if (m_server)
79 {
80 BOOL fAbsoluteMouse;
81 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
82 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
83 }
84 break;
85 }
86 case VBoxEventType_OnKeyboardLedsChanged:
87 {
88 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
89 Assert(klcev);
90
91 if (m_server)
92 {
93 BOOL fNumLock, fCapsLock, fScrollLock;
94 klcev->COMGETTER(NumLock)(&fNumLock);
95 klcev->COMGETTER(CapsLock)(&fCapsLock);
96 klcev->COMGETTER(ScrollLock)(&fScrollLock);
97 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
98 }
99 break;
100 }
101
102 default:
103 AssertFailed();
104 }
105
106 return S_OK;
107 }
108
109private:
110 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
111 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
112 ConsoleVRDPServer *m_server;
113};
114
115typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
116
117VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
118
119#ifdef DEBUG_sunlover
120#define LOGDUMPPTR Log
121void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
122{
123 unsigned i;
124
125 const uint8_t *pu8And = pu8Shape;
126
127 for (i = 0; i < height; i++)
128 {
129 unsigned j;
130 LOGDUMPPTR(("%p: ", pu8And));
131 for (j = 0; j < (width + 7) / 8; j++)
132 {
133 unsigned k;
134 for (k = 0; k < 8; k++)
135 {
136 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
137 }
138
139 pu8And++;
140 }
141 LOGDUMPPTR(("\n"));
142 }
143
144 if (fXorMaskRGB32)
145 {
146 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
147
148 for (i = 0; i < height; i++)
149 {
150 unsigned j;
151 LOGDUMPPTR(("%p: ", pu32Xor));
152 for (j = 0; j < width; j++)
153 {
154 LOGDUMPPTR(("%08X", *pu32Xor++));
155 }
156 LOGDUMPPTR(("\n"));
157 }
158 }
159 else
160 {
161 /* RDP 24 bit RGB mask. */
162 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
163 for (i = 0; i < height; i++)
164 {
165 unsigned j;
166 LOGDUMPPTR(("%p: ", pu8Xor));
167 for (j = 0; j < width; j++)
168 {
169 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
170 pu8Xor += 3;
171 }
172 LOGDUMPPTR(("\n"));
173 }
174 }
175}
176#else
177#define dumpPointer(a, b, c, d) do {} while (0)
178#endif /* DEBUG_sunlover */
179
180static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
181{
182 /*
183 * Find the top border of the AND mask. First assign to special value.
184 */
185 uint32_t ySkipAnd = ~0;
186
187 const uint8_t *pu8And = pu8AndMask;
188 const uint32_t cbAndRow = (width + 7) / 8;
189 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
190
191 Assert(cbAndRow > 0);
192
193 unsigned y;
194 unsigned x;
195
196 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
197 {
198 /* For each complete byte in the row. */
199 for (x = 0; x < cbAndRow - 1; x++)
200 {
201 if (pu8And[x] != 0xFF)
202 {
203 ySkipAnd = y;
204 break;
205 }
206 }
207
208 if (ySkipAnd == ~(uint32_t)0)
209 {
210 /* Last byte. */
211 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
212 {
213 ySkipAnd = y;
214 }
215 }
216 }
217
218 if (ySkipAnd == ~(uint32_t)0)
219 {
220 ySkipAnd = 0;
221 }
222
223 /*
224 * Find the left border of the AND mask.
225 */
226 uint32_t xSkipAnd = ~0;
227
228 /* For all bit columns. */
229 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
230 {
231 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
232 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
233
234 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
235 {
236 if ((*pu8And & mask) == 0)
237 {
238 xSkipAnd = x;
239 break;
240 }
241 }
242 }
243
244 if (xSkipAnd == ~(uint32_t)0)
245 {
246 xSkipAnd = 0;
247 }
248
249 /*
250 * Find the XOR mask top border.
251 */
252 uint32_t ySkipXor = ~0;
253
254 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
255
256 uint32_t *pu32Xor = pu32XorStart;
257
258 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
259 {
260 for (x = 0; x < width; x++)
261 {
262 if (pu32Xor[x] != 0)
263 {
264 ySkipXor = y;
265 break;
266 }
267 }
268 }
269
270 if (ySkipXor == ~(uint32_t)0)
271 {
272 ySkipXor = 0;
273 }
274
275 /*
276 * Find the left border of the XOR mask.
277 */
278 uint32_t xSkipXor = ~(uint32_t)0;
279
280 /* For all columns. */
281 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
282 {
283 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
284
285 for (y = ySkipXor; y < height; y++, pu32Xor += width)
286 {
287 if (*pu32Xor != 0)
288 {
289 xSkipXor = x;
290 break;
291 }
292 }
293 }
294
295 if (xSkipXor == ~(uint32_t)0)
296 {
297 xSkipXor = 0;
298 }
299
300 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
301 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
302}
303
304/* Generate an AND mask for alpha pointers here, because
305 * guest driver does not do that correctly for Vista pointers.
306 * Similar fix, changing the alpha threshold, could be applied
307 * for the guest driver, but then additions reinstall would be
308 * necessary, which we try to avoid.
309 */
310static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
311{
312 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
313
314 int y;
315 for (y = 0; y < h; y++)
316 {
317 uint8_t bitmask = 0x80;
318
319 int x;
320 for (x = 0; x < w; x++, bitmask >>= 1)
321 {
322 if (bitmask == 0)
323 {
324 bitmask = 0x80;
325 }
326
327 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
328 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
329 {
330 pu8DstAndMask[x / 8] &= ~bitmask;
331 }
332 }
333
334 /* Point to next source and dest scans. */
335 pu8SrcAlpha += w * 4;
336 pu8DstAndMask += (w + 7) / 8;
337 }
338}
339
340STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
341 BOOL alpha,
342 ULONG xHot,
343 ULONG yHot,
344 ULONG width,
345 ULONG height,
346 ComSafeArrayIn(BYTE,inShape))
347{
348 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
349
350 if (m_server)
351 {
352 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
353 if (aShape.size() == 0)
354 {
355 if (!visible)
356 {
357 m_server->MousePointerHide();
358 }
359 }
360 else if (width != 0 && height != 0)
361 {
362 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
363 * 'shape' AND mask followed by XOR mask.
364 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
365 *
366 * We convert this to RDP color format which consist of
367 * one bpp AND mask and 24 BPP (BGR) color XOR image.
368 *
369 * RDP clients expect 8 aligned width and height of
370 * pointer (preferably 32x32).
371 *
372 * They even contain bugs which do not appear for
373 * 32x32 pointers but would appear for a 41x32 one.
374 *
375 * So set pointer size to 32x32. This can be done safely
376 * because most pointers are 32x32.
377 */
378 uint8_t* shape = aShape.raw();
379
380 dumpPointer(shape, width, height, true);
381
382 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
383
384 uint8_t *pu8AndMask = shape;
385 uint8_t *pu8XorMask = shape + cbDstAndMask;
386
387 if (alpha)
388 {
389 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
390
391 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
392 }
393
394 /* Windows guest alpha pointers are wider than 32 pixels.
395 * Try to find out the top-left border of the pointer and
396 * then copy only meaningful bits. All complete top rows
397 * and all complete left columns where (AND == 1 && XOR == 0)
398 * are skipped. Hot spot is adjusted.
399 */
400 uint32_t ySkip = 0; /* How many rows to skip at the top. */
401 uint32_t xSkip = 0; /* How many columns to skip at the left. */
402
403 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
404
405 /* Must not skip the hot spot. */
406 xSkip = RT_MIN(xSkip, xHot);
407 ySkip = RT_MIN(ySkip, yHot);
408
409 /*
410 * Compute size and allocate memory for the pointer.
411 */
412 const uint32_t dstwidth = 32;
413 const uint32_t dstheight = 32;
414
415 VRDECOLORPOINTER *pointer = NULL;
416
417 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
418
419 uint32_t rdpmaskwidth = dstmaskwidth;
420 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
421
422 uint32_t rdpdatawidth = dstwidth * 3;
423 uint32_t rdpdatalen = dstheight * rdpdatawidth;
424
425 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
426
427 if (pointer)
428 {
429 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
430 uint8_t *dataarray = maskarray + rdpmasklen;
431
432 memset(maskarray, 0xFF, rdpmasklen);
433 memset(dataarray, 0x00, rdpdatalen);
434
435 uint32_t srcmaskwidth = (width + 7) / 8;
436 uint32_t srcdatawidth = width * 4;
437
438 /* Copy AND mask. */
439 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
440 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
441
442 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
443 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
444
445 unsigned x, y;
446
447 for (y = 0; y < minheight; y++)
448 {
449 for (x = 0; x < minwidth; x++)
450 {
451 uint32_t byteIndex = (x + xSkip) / 8;
452 uint32_t bitIndex = (x + xSkip) % 8;
453
454 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
455
456 if (!bit)
457 {
458 byteIndex = x / 8;
459 bitIndex = x % 8;
460
461 dst[byteIndex] &= ~(1 << (7 - bitIndex));
462 }
463 }
464
465 src += srcmaskwidth;
466 dst -= rdpmaskwidth;
467 }
468
469 /* Point src to XOR mask */
470 src = pu8XorMask + ySkip * srcdatawidth;
471 dst = dataarray + (dstheight - 1) * rdpdatawidth;
472
473 for (y = 0; y < minheight ; y++)
474 {
475 for (x = 0; x < minwidth; x++)
476 {
477 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
478 }
479
480 src += srcdatawidth;
481 dst -= rdpdatawidth;
482 }
483
484 pointer->u16HotX = (uint16_t)(xHot - xSkip);
485 pointer->u16HotY = (uint16_t)(yHot - ySkip);
486
487 pointer->u16Width = (uint16_t)dstwidth;
488 pointer->u16Height = (uint16_t)dstheight;
489
490 pointer->u16MaskLen = (uint16_t)rdpmasklen;
491 pointer->u16DataLen = (uint16_t)rdpdatalen;
492
493 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
494
495 m_server->MousePointerUpdate(pointer);
496
497 RTMemTmpFree(pointer);
498 }
499 }
500 }
501
502 return S_OK;
503}
504
505
506// ConsoleVRDPServer
507////////////////////////////////////////////////////////////////////////////////
508
509RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
510
511PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
512
513VRDEENTRYPOINTS_3 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
514VRDEENTRYPOINTS_3 *ConsoleVRDPServer::mpEntryPoints = NULL;
515
516VRDECALLBACKS_3 ConsoleVRDPServer::mCallbacks =
517{
518 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
519 ConsoleVRDPServer::VRDPCallbackQueryProperty,
520 ConsoleVRDPServer::VRDPCallbackClientLogon,
521 ConsoleVRDPServer::VRDPCallbackClientConnect,
522 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
523 ConsoleVRDPServer::VRDPCallbackIntercept,
524 ConsoleVRDPServer::VRDPCallbackUSB,
525 ConsoleVRDPServer::VRDPCallbackClipboard,
526 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
527 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
528 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
529 ConsoleVRDPServer::VRDPCallbackInput,
530 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
531 ConsoleVRDPServer::VRDECallbackAudioIn
532};
533
534DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
535{
536 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
537
538 int rc = VERR_NOT_SUPPORTED;
539
540 switch (index)
541 {
542 case VRDE_QP_NETWORK_PORT:
543 {
544 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
545 ULONG port = 0;
546
547 if (cbBuffer >= sizeof(uint32_t))
548 {
549 *(uint32_t *)pvBuffer = (uint32_t)port;
550 rc = VINF_SUCCESS;
551 }
552 else
553 {
554 rc = VINF_BUFFER_OVERFLOW;
555 }
556
557 *pcbOut = sizeof(uint32_t);
558 } break;
559
560 case VRDE_QP_NETWORK_ADDRESS:
561 {
562 com::Bstr bstr;
563 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
564
565 /* The server expects UTF8. */
566 com::Utf8Str address = bstr;
567
568 size_t cbAddress = address.length() + 1;
569
570 if (cbAddress >= 0x10000)
571 {
572 /* More than 64K seems to be an invalid address. */
573 rc = VERR_TOO_MUCH_DATA;
574 break;
575 }
576
577 if ((size_t)cbBuffer >= cbAddress)
578 {
579 memcpy(pvBuffer, address.c_str(), cbAddress);
580 rc = VINF_SUCCESS;
581 }
582 else
583 {
584 rc = VINF_BUFFER_OVERFLOW;
585 }
586
587 *pcbOut = (uint32_t)cbAddress;
588 } break;
589
590 case VRDE_QP_NUMBER_MONITORS:
591 {
592 ULONG cMonitors = 1;
593
594 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
595
596 if (cbBuffer >= sizeof(uint32_t))
597 {
598 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
599 rc = VINF_SUCCESS;
600 }
601 else
602 {
603 rc = VINF_BUFFER_OVERFLOW;
604 }
605
606 *pcbOut = sizeof(uint32_t);
607 } break;
608
609 case VRDE_QP_NETWORK_PORT_RANGE:
610 {
611 com::Bstr bstr;
612 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
613
614 if (hrc != S_OK)
615 {
616 bstr = "";
617 }
618
619 if (bstr == "0")
620 {
621 bstr = "3389";
622 }
623
624 /* The server expects UTF8. */
625 com::Utf8Str portRange = bstr;
626
627 size_t cbPortRange = portRange.length() + 1;
628
629 if (cbPortRange >= 0x10000)
630 {
631 /* More than 64K seems to be an invalid port range string. */
632 rc = VERR_TOO_MUCH_DATA;
633 break;
634 }
635
636 if ((size_t)cbBuffer >= cbPortRange)
637 {
638 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
639 rc = VINF_SUCCESS;
640 }
641 else
642 {
643 rc = VINF_BUFFER_OVERFLOW;
644 }
645
646 *pcbOut = (uint32_t)cbPortRange;
647 } break;
648
649#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
650 case VRDE_QP_VIDEO_CHANNEL:
651 {
652 com::Bstr bstr;
653 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(), bstr.asOutParam());
654
655 if (hrc != S_OK)
656 {
657 bstr = "";
658 }
659
660 com::Utf8Str value = bstr;
661
662 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
663 || RTStrICmp(value.c_str(), "1") == 0;
664
665 if (cbBuffer >= sizeof(uint32_t))
666 {
667 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
668 rc = VINF_SUCCESS;
669 }
670 else
671 {
672 rc = VINF_BUFFER_OVERFLOW;
673 }
674
675 *pcbOut = sizeof(uint32_t);
676 } break;
677
678 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
679 {
680 com::Bstr bstr;
681 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(), bstr.asOutParam());
682
683 if (hrc != S_OK)
684 {
685 bstr = "";
686 }
687
688 com::Utf8Str value = bstr;
689
690 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
691
692 if (cbBuffer >= sizeof(uint32_t))
693 {
694 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
695 rc = VINF_SUCCESS;
696 }
697 else
698 {
699 rc = VINF_BUFFER_OVERFLOW;
700 }
701
702 *pcbOut = sizeof(uint32_t);
703 } break;
704
705 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
706 {
707 ULONG ulSunFlsh = 1;
708
709 com::Bstr bstr;
710 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
711 bstr.asOutParam());
712 if (hrc == S_OK && !bstr.isEmpty())
713 {
714 com::Utf8Str sunFlsh = bstr;
715 if (!sunFlsh.isEmpty())
716 {
717 ulSunFlsh = sunFlsh.toUInt32();
718 }
719 }
720
721 if (cbBuffer >= sizeof(uint32_t))
722 {
723 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
724 rc = VINF_SUCCESS;
725 }
726 else
727 {
728 rc = VINF_BUFFER_OVERFLOW;
729 }
730
731 *pcbOut = sizeof(uint32_t);
732 } break;
733#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
734
735 case VRDE_QP_FEATURE:
736 {
737 if (cbBuffer < sizeof(VRDEFEATURE))
738 {
739 rc = VERR_INVALID_PARAMETER;
740 break;
741 }
742
743 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
744
745 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
746
747 size_t cchInfo = 0;
748 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
749
750 if (RT_FAILURE(rc))
751 {
752 rc = VERR_INVALID_PARAMETER;
753 break;
754 }
755
756 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
757
758 com::Bstr bstrValue;
759
760 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
761 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
762 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
763 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
764 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
765 )
766 {
767 /* @todo these features should be per client. */
768 NOREF(pFeature->u32ClientId);
769
770 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
771 com::Utf8Str extraData("VRDE/Feature/");
772 extraData += pFeature->achInfo;
773
774 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
775 bstrValue.asOutParam());
776 if (FAILED(hrc) || bstrValue.isEmpty())
777 {
778 /* Also try the old "VRDP/Feature/NAME" */
779 extraData = "VRDP/Feature/";
780 extraData += pFeature->achInfo;
781
782 hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
783 bstrValue.asOutParam());
784 if (FAILED(hrc))
785 {
786 rc = VERR_NOT_SUPPORTED;
787 }
788 }
789 }
790 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
791 {
792 /* Generic properties. */
793 const char *pszPropertyName = &pFeature->achInfo[9];
794 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(), bstrValue.asOutParam());
795 if (FAILED(hrc))
796 {
797 rc = VERR_NOT_SUPPORTED;
798 }
799 }
800 else
801 {
802 rc = VERR_NOT_SUPPORTED;
803 }
804
805 /* Copy the value string to the callers buffer. */
806 if (rc == VINF_SUCCESS)
807 {
808 com::Utf8Str value = bstrValue;
809
810 size_t cb = value.length() + 1;
811
812 if ((size_t)cbInfo >= cb)
813 {
814 memcpy(pFeature->achInfo, value.c_str(), cb);
815 }
816 else
817 {
818 rc = VINF_BUFFER_OVERFLOW;
819 }
820
821 *pcbOut = (uint32_t)cb;
822 }
823 } break;
824
825 case VRDE_SP_NETWORK_BIND_PORT:
826 {
827 if (cbBuffer != sizeof(uint32_t))
828 {
829 rc = VERR_INVALID_PARAMETER;
830 break;
831 }
832
833 ULONG port = *(uint32_t *)pvBuffer;
834
835 server->mVRDPBindPort = port;
836
837 rc = VINF_SUCCESS;
838
839 if (pcbOut)
840 {
841 *pcbOut = sizeof(uint32_t);
842 }
843
844 server->mConsole->onVRDEServerInfoChange();
845 } break;
846
847 default:
848 break;
849 }
850
851 return rc;
852}
853
854DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
855{
856 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
857
858 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
859}
860
861DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
862{
863 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
864
865 server->mConsole->VRDPClientConnect(u32ClientId);
866}
867
868DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
869{
870 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
871
872 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
873
874 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
875 {
876 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
877 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
878
879 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
880 if (pPort)
881 {
882 pPort->pfnAudioInputIntercept(pPort, false);
883 }
884 else
885 {
886 AssertFailed();
887 }
888 }
889}
890
891DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
892{
893 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
894
895 LogFlowFunc(("%x\n", fu32Intercept));
896
897 int rc = VERR_NOT_SUPPORTED;
898
899 switch (fu32Intercept)
900 {
901 case VRDE_CLIENT_INTERCEPT_AUDIO:
902 {
903 server->mConsole->VRDPInterceptAudio(u32ClientId);
904 if (ppvIntercept)
905 {
906 *ppvIntercept = server;
907 }
908 rc = VINF_SUCCESS;
909 } break;
910
911 case VRDE_CLIENT_INTERCEPT_USB:
912 {
913 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
914 rc = VINF_SUCCESS;
915 } break;
916
917 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
918 {
919 server->mConsole->VRDPInterceptClipboard(u32ClientId);
920 if (ppvIntercept)
921 {
922 *ppvIntercept = server;
923 }
924 rc = VINF_SUCCESS;
925 } break;
926
927 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
928 {
929 /* This request is processed internally by the ConsoleVRDPServer.
930 * Only one client is allowed to intercept audio input.
931 */
932 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
933 {
934 Log(("AUDIOIN: connected client %u\n", u32ClientId));
935
936 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
937 if (pPort)
938 {
939 pPort->pfnAudioInputIntercept(pPort, true);
940 if (ppvIntercept)
941 {
942 *ppvIntercept = server;
943 }
944 }
945 else
946 {
947 AssertFailed();
948 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
949 rc = VERR_NOT_SUPPORTED;
950 }
951 }
952 else
953 {
954 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
955 rc = VERR_NOT_SUPPORTED;
956 }
957 } break;
958
959 default:
960 break;
961 }
962
963 return rc;
964}
965
966DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
967{
968#ifdef VBOX_WITH_USB
969 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
970#else
971 return VERR_NOT_SUPPORTED;
972#endif
973}
974
975DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
976{
977 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
978}
979
980DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
981{
982 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
983
984 bool fAvailable = false;
985
986 IFramebuffer *pfb = NULL;
987 LONG xOrigin = 0;
988 LONG yOrigin = 0;
989
990 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
991
992 if (pfb)
993 {
994 pfb->Lock ();
995
996 /* Query framebuffer parameters. */
997 ULONG lineSize = 0;
998 pfb->COMGETTER(BytesPerLine)(&lineSize);
999
1000 ULONG bitsPerPixel = 0;
1001 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1002
1003 BYTE *address = NULL;
1004 pfb->COMGETTER(Address)(&address);
1005
1006 ULONG height = 0;
1007 pfb->COMGETTER(Height)(&height);
1008
1009 ULONG width = 0;
1010 pfb->COMGETTER(Width)(&width);
1011
1012 /* Now fill the information as requested by the caller. */
1013 pInfo->pu8Bits = address;
1014 pInfo->xOrigin = xOrigin;
1015 pInfo->yOrigin = yOrigin;
1016 pInfo->cWidth = width;
1017 pInfo->cHeight = height;
1018 pInfo->cBitsPerPixel = bitsPerPixel;
1019 pInfo->cbLine = lineSize;
1020
1021 pfb->Unlock();
1022
1023 fAvailable = true;
1024 }
1025
1026 if (server->maFramebuffers[uScreenId])
1027 {
1028 server->maFramebuffers[uScreenId]->Release();
1029 }
1030 server->maFramebuffers[uScreenId] = pfb;
1031
1032 return fAvailable;
1033}
1034
1035DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1036{
1037 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1038
1039 if (server->maFramebuffers[uScreenId])
1040 {
1041 server->maFramebuffers[uScreenId]->Lock();
1042 }
1043}
1044
1045DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1046{
1047 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1048
1049 if (server->maFramebuffers[uScreenId])
1050 {
1051 server->maFramebuffers[uScreenId]->Unlock();
1052 }
1053}
1054
1055static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1056{
1057 if ( pInputSynch->cGuestNumLockAdaptions
1058 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1059 {
1060 pInputSynch->cGuestNumLockAdaptions--;
1061 pKeyboard->PutScancode(0x45);
1062 pKeyboard->PutScancode(0x45 | 0x80);
1063 }
1064 if ( pInputSynch->cGuestCapsLockAdaptions
1065 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1066 {
1067 pInputSynch->cGuestCapsLockAdaptions--;
1068 pKeyboard->PutScancode(0x3a);
1069 pKeyboard->PutScancode(0x3a | 0x80);
1070 }
1071}
1072
1073DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1074{
1075 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1076 Console *pConsole = server->mConsole;
1077
1078 switch (type)
1079 {
1080 case VRDE_INPUT_SCANCODE:
1081 {
1082 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1083 {
1084 IKeyboard *pKeyboard = pConsole->getKeyboard();
1085
1086 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1087
1088 /* Track lock keys. */
1089 if (pInputScancode->uScancode == 0x45)
1090 {
1091 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1092 }
1093 else if (pInputScancode->uScancode == 0x3a)
1094 {
1095 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1096 }
1097 else if (pInputScancode->uScancode == 0x46)
1098 {
1099 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1100 }
1101 else if ((pInputScancode->uScancode & 0x80) == 0)
1102 {
1103 /* Key pressed. */
1104 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1105 }
1106
1107 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1108 }
1109 } break;
1110
1111 case VRDE_INPUT_POINT:
1112 {
1113 if (cbInput == sizeof(VRDEINPUTPOINT))
1114 {
1115 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1116
1117 int mouseButtons = 0;
1118 int iWheel = 0;
1119
1120 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1121 {
1122 mouseButtons |= MouseButtonState_LeftButton;
1123 }
1124 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1125 {
1126 mouseButtons |= MouseButtonState_RightButton;
1127 }
1128 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1129 {
1130 mouseButtons |= MouseButtonState_MiddleButton;
1131 }
1132 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1133 {
1134 mouseButtons |= MouseButtonState_WheelUp;
1135 iWheel = -1;
1136 }
1137 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1138 {
1139 mouseButtons |= MouseButtonState_WheelDown;
1140 iWheel = 1;
1141 }
1142
1143 if (server->m_fGuestWantsAbsolute)
1144 {
1145 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1146 } else
1147 {
1148 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1149 pInputPoint->y - server->m_mousey,
1150 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1151 server->m_mousex = pInputPoint->x;
1152 server->m_mousey = pInputPoint->y;
1153 }
1154 }
1155 } break;
1156
1157 case VRDE_INPUT_CAD:
1158 {
1159 pConsole->getKeyboard()->PutCAD();
1160 } break;
1161
1162 case VRDE_INPUT_RESET:
1163 {
1164 pConsole->Reset();
1165 } break;
1166
1167 case VRDE_INPUT_SYNCH:
1168 {
1169 if (cbInput == sizeof(VRDEINPUTSYNCH))
1170 {
1171 IKeyboard *pKeyboard = pConsole->getKeyboard();
1172
1173 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1174
1175 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1176 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1177 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1178
1179 /* The client initiated synchronization. Always make the guest to reflect the client state.
1180 * Than means, when the guest changes the state itself, it is forced to return to the client
1181 * state.
1182 */
1183 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1184 {
1185 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1186 }
1187
1188 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1189 {
1190 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1191 }
1192
1193 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1194 }
1195 } break;
1196
1197 default:
1198 break;
1199 }
1200}
1201
1202DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1203{
1204 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1205
1206 server->mConsole->getDisplay()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1207}
1208
1209DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1210 void *pvCtx,
1211 uint32_t u32ClientId,
1212 uint32_t u32Event,
1213 const void *pvData,
1214 uint32_t cbData)
1215{
1216 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1217
1218 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1219
1220 switch (u32Event)
1221 {
1222 case VRDE_AUDIOIN_BEGIN:
1223 {
1224 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1225
1226 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1227 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1228 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1229 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1230 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1231 );
1232 } break;
1233
1234 case VRDE_AUDIOIN_DATA:
1235 {
1236 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1237 } break;
1238
1239 case VRDE_AUDIOIN_END:
1240 {
1241 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1242 } break;
1243
1244 default:
1245 return;
1246 }
1247}
1248
1249
1250ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1251{
1252 mConsole = console;
1253
1254 int rc = RTCritSectInit(&mCritSect);
1255 AssertRC(rc);
1256
1257 mcClipboardRefs = 0;
1258 mpfnClipboardCallback = NULL;
1259
1260#ifdef VBOX_WITH_USB
1261 mUSBBackends.pHead = NULL;
1262 mUSBBackends.pTail = NULL;
1263
1264 mUSBBackends.thread = NIL_RTTHREAD;
1265 mUSBBackends.fThreadRunning = false;
1266 mUSBBackends.event = 0;
1267#endif
1268
1269 mhServer = 0;
1270 mServerInterfaceVersion = 0;
1271
1272 m_fGuestWantsAbsolute = false;
1273 m_mousex = 0;
1274 m_mousey = 0;
1275
1276 m_InputSynch.cGuestNumLockAdaptions = 2;
1277 m_InputSynch.cGuestCapsLockAdaptions = 2;
1278
1279 m_InputSynch.fGuestNumLock = false;
1280 m_InputSynch.fGuestCapsLock = false;
1281 m_InputSynch.fGuestScrollLock = false;
1282
1283 m_InputSynch.fClientNumLock = false;
1284 m_InputSynch.fClientCapsLock = false;
1285 m_InputSynch.fClientScrollLock = false;
1286
1287 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1288
1289 {
1290 ComPtr<IEventSource> es;
1291 console->COMGETTER(EventSource)(es.asOutParam());
1292 mConsoleListener = new VRDPConsoleListenerImpl(this);
1293 com::SafeArray <VBoxEventType_T> eventTypes;
1294 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1295 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1296 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1297 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1298 }
1299
1300 mVRDPBindPort = -1;
1301
1302 mAuthLibrary = 0;
1303
1304 mu32AudioInputClientId = 0;
1305}
1306
1307ConsoleVRDPServer::~ConsoleVRDPServer()
1308{
1309 Stop();
1310
1311 if (mConsoleListener)
1312 {
1313 ComPtr<IEventSource> es;
1314 mConsole->COMGETTER(EventSource)(es.asOutParam());
1315 es->UnregisterListener(mConsoleListener);
1316 mConsoleListener->Release();
1317 mConsoleListener = NULL;
1318 }
1319
1320 unsigned i;
1321 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1322 {
1323 if (maFramebuffers[i])
1324 {
1325 maFramebuffers[i]->Release();
1326 maFramebuffers[i] = NULL;
1327 }
1328 }
1329
1330 if (RTCritSectIsInitialized(&mCritSect))
1331 {
1332 RTCritSectDelete(&mCritSect);
1333 memset(&mCritSect, 0, sizeof(mCritSect));
1334 }
1335}
1336
1337int ConsoleVRDPServer::Launch(void)
1338{
1339 LogFlowThisFunc(("\n"));
1340
1341 IVRDEServer *server = mConsole->getVRDEServer();
1342 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1343
1344 /*
1345 * Check if VRDE is enabled.
1346 */
1347 BOOL fEnabled;
1348 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1349 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1350 if (!fEnabled)
1351 return VINF_SUCCESS;
1352
1353 /*
1354 * Check that a VRDE extension pack name is set and resolve it into a
1355 * library path.
1356 */
1357 Bstr bstrExtPack;
1358 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1359 if (FAILED(hrc))
1360 return Global::vboxStatusCodeFromCOM(hrc);
1361 if (bstrExtPack.isEmpty())
1362 return VINF_NOT_SUPPORTED;
1363
1364 Utf8Str strExtPack(bstrExtPack);
1365 Utf8Str strVrdeLibrary;
1366 int vrc = VINF_SUCCESS;
1367 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1368 strVrdeLibrary = "VBoxVRDP";
1369 else
1370 {
1371#ifdef VBOX_WITH_EXTPACK
1372 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1373 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1374#else
1375 vrc = VERR_FILE_NOT_FOUND;
1376#endif
1377 }
1378 if (RT_SUCCESS(vrc))
1379 {
1380 /*
1381 * Load the VRDE library and start the server, if it is enabled.
1382 */
1383 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1384 if (RT_SUCCESS(vrc))
1385 {
1386 VRDEENTRYPOINTS_3 *pEntryPoints3;
1387 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1388
1389 if (RT_SUCCESS(vrc))
1390 {
1391 mServerInterfaceVersion = 3;
1392 mEntryPoints = *pEntryPoints3;
1393 mpEntryPoints = &mEntryPoints;
1394 }
1395 else if (vrc == VERR_VERSION_MISMATCH)
1396 {
1397 /* An older version of VRDE is installed, try version 1. */
1398 VRDEENTRYPOINTS_1 *pEntryPoints1;
1399
1400 static VRDECALLBACKS_1 sCallbacks =
1401 {
1402 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1403 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1404 ConsoleVRDPServer::VRDPCallbackClientLogon,
1405 ConsoleVRDPServer::VRDPCallbackClientConnect,
1406 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1407 ConsoleVRDPServer::VRDPCallbackIntercept,
1408 ConsoleVRDPServer::VRDPCallbackUSB,
1409 ConsoleVRDPServer::VRDPCallbackClipboard,
1410 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1411 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1412 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1413 ConsoleVRDPServer::VRDPCallbackInput,
1414 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1415 };
1416
1417 vrc = mpfnVRDECreateServer(&sCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1418 if (RT_SUCCESS(vrc))
1419 {
1420 LogRel(("VRDE: loaded an older version of the server.\n"));
1421
1422 mServerInterfaceVersion = 3;
1423 mEntryPoints.header = pEntryPoints1->header;
1424 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1425 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1426 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1427 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1428 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1429 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1430 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1431 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1432 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1433 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1434 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1435 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1436 mEntryPoints.VRDERedirect = NULL;
1437 mEntryPoints.VRDEAudioInOpen = NULL;
1438 mEntryPoints.VRDEAudioInClose = NULL;
1439 mpEntryPoints = &mEntryPoints;
1440 }
1441 }
1442
1443 if (RT_SUCCESS(vrc))
1444 {
1445#ifdef VBOX_WITH_USB
1446 remoteUSBThreadStart();
1447#endif
1448 }
1449 else
1450 {
1451 if (vrc != VERR_NET_ADDRESS_IN_USE)
1452 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1453 /* Don't unload the lib, because it prevents us trying again or
1454 because there may be other users? */
1455 }
1456 }
1457 }
1458
1459 return vrc;
1460}
1461
1462void ConsoleVRDPServer::EnableConnections(void)
1463{
1464 if (mpEntryPoints && mhServer)
1465 {
1466 mpEntryPoints->VRDEEnableConnections(mhServer, true);
1467 }
1468}
1469
1470void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
1471{
1472 if (mpEntryPoints && mhServer)
1473 {
1474 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
1475 }
1476}
1477
1478void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
1479{
1480 if (mpEntryPoints && mhServer)
1481 {
1482 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
1483 }
1484}
1485
1486void ConsoleVRDPServer::MousePointerHide(void)
1487{
1488 if (mpEntryPoints && mhServer)
1489 {
1490 mpEntryPoints->VRDEHidePointer(mhServer);
1491 }
1492}
1493
1494void ConsoleVRDPServer::Stop(void)
1495{
1496 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1497 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1498 if (mhServer)
1499 {
1500 HVRDESERVER hServer = mhServer;
1501
1502 /* Reset the handle to avoid further calls to the server. */
1503 mhServer = 0;
1504
1505 if (mpEntryPoints && hServer)
1506 {
1507 mpEntryPoints->VRDEDestroy(hServer);
1508 }
1509 }
1510
1511#ifdef VBOX_WITH_USB
1512 remoteUSBThreadStop();
1513#endif /* VBOX_WITH_USB */
1514
1515 mpfnAuthEntry = NULL;
1516 mpfnAuthEntry2 = NULL;
1517 mpfnAuthEntry3 = NULL;
1518
1519 if (mAuthLibrary)
1520 {
1521 RTLdrClose(mAuthLibrary);
1522 mAuthLibrary = 0;
1523 }
1524}
1525
1526/* Worker thread for Remote USB. The thread polls the clients for
1527 * the list of attached USB devices.
1528 * The thread is also responsible for attaching/detaching devices
1529 * to/from the VM.
1530 *
1531 * It is expected that attaching/detaching is not a frequent operation.
1532 *
1533 * The thread is always running when the VRDP server is active.
1534 *
1535 * The thread scans backends and requests the device list every 2 seconds.
1536 *
1537 * When device list is available, the thread calls the Console to process it.
1538 *
1539 */
1540#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1541
1542#ifdef VBOX_WITH_USB
1543static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
1544{
1545 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1546
1547 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1548
1549 pOwner->notifyRemoteUSBThreadRunning(self);
1550
1551 while (pOwner->isRemoteUSBThreadRunning())
1552 {
1553 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1554
1555 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
1556 {
1557 pRemoteUSBBackend->PollRemoteDevices();
1558 }
1559
1560 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
1561
1562 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
1563 }
1564
1565 return VINF_SUCCESS;
1566}
1567
1568void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
1569{
1570 mUSBBackends.thread = thread;
1571 mUSBBackends.fThreadRunning = true;
1572 int rc = RTThreadUserSignal(thread);
1573 AssertRC(rc);
1574}
1575
1576bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
1577{
1578 return mUSBBackends.fThreadRunning;
1579}
1580
1581void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
1582{
1583 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
1584 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1585 NOREF(rc);
1586}
1587
1588void ConsoleVRDPServer::remoteUSBThreadStart(void)
1589{
1590 int rc = RTSemEventCreate(&mUSBBackends.event);
1591
1592 if (RT_FAILURE(rc))
1593 {
1594 AssertFailed();
1595 mUSBBackends.event = 0;
1596 }
1597
1598 if (RT_SUCCESS(rc))
1599 {
1600 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
1601 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
1602 }
1603
1604 if (RT_FAILURE(rc))
1605 {
1606 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
1607 mUSBBackends.thread = NIL_RTTHREAD;
1608 }
1609 else
1610 {
1611 /* Wait until the thread is ready. */
1612 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
1613 AssertRC(rc);
1614 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
1615 }
1616}
1617
1618void ConsoleVRDPServer::remoteUSBThreadStop(void)
1619{
1620 mUSBBackends.fThreadRunning = false;
1621
1622 if (mUSBBackends.thread != NIL_RTTHREAD)
1623 {
1624 Assert (mUSBBackends.event != 0);
1625
1626 RTSemEventSignal(mUSBBackends.event);
1627
1628 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
1629 AssertRC(rc);
1630
1631 mUSBBackends.thread = NIL_RTTHREAD;
1632 }
1633
1634 if (mUSBBackends.event)
1635 {
1636 RTSemEventDestroy(mUSBBackends.event);
1637 mUSBBackends.event = 0;
1638 }
1639}
1640#endif /* VBOX_WITH_USB */
1641
1642AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
1643 const char *pszUser, const char *pszPassword, const char *pszDomain,
1644 uint32_t u32ClientId)
1645{
1646 AUTHUUID rawuuid;
1647
1648 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1649
1650 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
1651 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
1652
1653 /*
1654 * Called only from VRDP input thread. So thread safety is not required.
1655 */
1656
1657 if (!mAuthLibrary)
1658 {
1659 /* Load the external authentication library. */
1660 Bstr authLibrary;
1661 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
1662
1663 Utf8Str filename = authLibrary;
1664
1665 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
1666
1667 int rc;
1668 if (RTPathHavePath(filename.c_str()))
1669 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
1670 else
1671 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
1672
1673 if (RT_FAILURE(rc))
1674 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
1675
1676 if (RT_SUCCESS(rc))
1677 {
1678 typedef struct AuthEntryInfo
1679 {
1680 const char *pszName;
1681 void **ppvAddress;
1682
1683 } AuthEntryInfo;
1684 AuthEntryInfo entries[] =
1685 {
1686 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
1687 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
1688 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
1689 { NULL, NULL }
1690 };
1691
1692 /* Get the entry point. */
1693 AuthEntryInfo *pEntryInfo = &entries[0];
1694 while (pEntryInfo->pszName)
1695 {
1696 *pEntryInfo->ppvAddress = NULL;
1697
1698 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
1699 if (RT_SUCCESS(rc2))
1700 {
1701 /* Found an entry point. */
1702 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
1703 rc = VINF_SUCCESS;
1704 break;
1705 }
1706
1707 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1708 {
1709 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
1710 }
1711 rc = rc2;
1712
1713 pEntryInfo++;
1714 }
1715 }
1716
1717 if (RT_FAILURE(rc))
1718 {
1719 mConsole->setError(E_FAIL,
1720 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
1721 filename.c_str(),
1722 rc);
1723
1724 mpfnAuthEntry = NULL;
1725 mpfnAuthEntry2 = NULL;
1726 mpfnAuthEntry3 = NULL;
1727
1728 if (mAuthLibrary)
1729 {
1730 RTLdrClose(mAuthLibrary);
1731 mAuthLibrary = 0;
1732 }
1733
1734 return AuthResultAccessDenied;
1735 }
1736 }
1737
1738 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
1739
1740 AuthResult result = AuthResultAccessDenied;
1741 if (mpfnAuthEntry3)
1742 {
1743 result = mpfnAuthEntry3("vrde", &rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
1744 }
1745 else if (mpfnAuthEntry2)
1746 {
1747 result = mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
1748 }
1749 else if (mpfnAuthEntry)
1750 {
1751 result = mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
1752 }
1753
1754 switch (result)
1755 {
1756 case AuthResultAccessDenied:
1757 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
1758 break;
1759 case AuthResultAccessGranted:
1760 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
1761 break;
1762 case AuthResultDelegateToGuest:
1763 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
1764 break;
1765 default:
1766 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
1767 result = AuthResultAccessDenied;
1768 }
1769
1770 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
1771
1772 return result;
1773}
1774
1775void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
1776{
1777 AUTHUUID rawuuid;
1778
1779 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1780
1781 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
1782 rawuuid, u32ClientId));
1783
1784 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
1785
1786 if (mpfnAuthEntry3)
1787 mpfnAuthEntry3("vrde", &rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1788 else if (mpfnAuthEntry2)
1789 mpfnAuthEntry2(&rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1790}
1791
1792int ConsoleVRDPServer::lockConsoleVRDPServer(void)
1793{
1794 int rc = RTCritSectEnter(&mCritSect);
1795 AssertRC(rc);
1796 return rc;
1797}
1798
1799void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
1800{
1801 RTCritSectLeave(&mCritSect);
1802}
1803
1804DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
1805 uint32_t u32ClientId,
1806 uint32_t u32Function,
1807 uint32_t u32Format,
1808 const void *pvData,
1809 uint32_t cbData)
1810{
1811 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
1812 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
1813
1814 int rc = VINF_SUCCESS;
1815
1816 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
1817
1818 NOREF(u32ClientId);
1819
1820 switch (u32Function)
1821 {
1822 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
1823 {
1824 if (pServer->mpfnClipboardCallback)
1825 {
1826 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1827 u32Format,
1828 (void *)pvData,
1829 cbData);
1830 }
1831 } break;
1832
1833 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
1834 {
1835 if (pServer->mpfnClipboardCallback)
1836 {
1837 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1838 u32Format,
1839 (void *)pvData,
1840 cbData);
1841 }
1842 } break;
1843
1844 default:
1845 rc = VERR_NOT_SUPPORTED;
1846 }
1847
1848 return rc;
1849}
1850
1851DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
1852 uint32_t u32Function,
1853 void *pvParms,
1854 uint32_t cbParms)
1855{
1856 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
1857 pvExtension, u32Function, pvParms, cbParms));
1858
1859 int rc = VINF_SUCCESS;
1860
1861 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
1862
1863 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
1864
1865 switch (u32Function)
1866 {
1867 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
1868 {
1869 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
1870 } break;
1871
1872 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1873 {
1874 /* The guest announces clipboard formats. This must be delivered to all clients. */
1875 if (mpEntryPoints && pServer->mhServer)
1876 {
1877 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1878 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
1879 pParms->u32Format,
1880 NULL,
1881 0,
1882 NULL);
1883 }
1884 } break;
1885
1886 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1887 {
1888 /* The clipboard service expects that the pvData buffer will be filled
1889 * with clipboard data. The server returns the data from the client that
1890 * announced the requested format most recently.
1891 */
1892 if (mpEntryPoints && pServer->mhServer)
1893 {
1894 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1895 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
1896 pParms->u32Format,
1897 pParms->u.pvData,
1898 pParms->cbData,
1899 &pParms->cbData);
1900 }
1901 } break;
1902
1903 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
1904 {
1905 if (mpEntryPoints && pServer->mhServer)
1906 {
1907 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1908 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
1909 pParms->u32Format,
1910 pParms->u.pvData,
1911 pParms->cbData,
1912 NULL);
1913 }
1914 } break;
1915
1916 default:
1917 rc = VERR_NOT_SUPPORTED;
1918 }
1919
1920 return rc;
1921}
1922
1923void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
1924{
1925 int rc = lockConsoleVRDPServer();
1926
1927 if (RT_SUCCESS(rc))
1928 {
1929 if (mcClipboardRefs == 0)
1930 {
1931 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
1932
1933 if (RT_SUCCESS(rc))
1934 {
1935 mcClipboardRefs++;
1936 }
1937 }
1938
1939 unlockConsoleVRDPServer();
1940 }
1941}
1942
1943void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
1944{
1945 int rc = lockConsoleVRDPServer();
1946
1947 if (RT_SUCCESS(rc))
1948 {
1949 mcClipboardRefs--;
1950
1951 if (mcClipboardRefs == 0)
1952 {
1953 HGCMHostUnregisterServiceExtension(mhClipboard);
1954 }
1955
1956 unlockConsoleVRDPServer();
1957 }
1958}
1959
1960/* That is called on INPUT thread of the VRDP server.
1961 * The ConsoleVRDPServer keeps a list of created backend instances.
1962 */
1963void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
1964{
1965#ifdef VBOX_WITH_USB
1966 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
1967
1968 /* Create a new instance of the USB backend for the new client. */
1969 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
1970
1971 if (pRemoteUSBBackend)
1972 {
1973 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
1974
1975 /* Append the new instance in the list. */
1976 int rc = lockConsoleVRDPServer();
1977
1978 if (RT_SUCCESS(rc))
1979 {
1980 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
1981 if (mUSBBackends.pHead)
1982 {
1983 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
1984 }
1985 else
1986 {
1987 mUSBBackends.pTail = pRemoteUSBBackend;
1988 }
1989
1990 mUSBBackends.pHead = pRemoteUSBBackend;
1991
1992 unlockConsoleVRDPServer();
1993
1994 if (ppvIntercept)
1995 {
1996 *ppvIntercept = pRemoteUSBBackend;
1997 }
1998 }
1999
2000 if (RT_FAILURE(rc))
2001 {
2002 pRemoteUSBBackend->Release();
2003 }
2004 }
2005#endif /* VBOX_WITH_USB */
2006}
2007
2008void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
2009{
2010#ifdef VBOX_WITH_USB
2011 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
2012
2013 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2014
2015 /* Find the instance. */
2016 int rc = lockConsoleVRDPServer();
2017
2018 if (RT_SUCCESS(rc))
2019 {
2020 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2021
2022 if (pRemoteUSBBackend)
2023 {
2024 /* Notify that it will be deleted. */
2025 pRemoteUSBBackend->NotifyDelete();
2026 }
2027
2028 unlockConsoleVRDPServer();
2029 }
2030
2031 if (pRemoteUSBBackend)
2032 {
2033 /* Here the instance has been excluded from the list and can be dereferenced. */
2034 pRemoteUSBBackend->Release();
2035 }
2036#endif
2037}
2038
2039void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
2040{
2041#ifdef VBOX_WITH_USB
2042 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2043
2044 /* Find the instance. */
2045 int rc = lockConsoleVRDPServer();
2046
2047 if (RT_SUCCESS(rc))
2048 {
2049 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2050
2051 if (pRemoteUSBBackend)
2052 {
2053 /* Inform the backend instance that it is referenced by the Guid. */
2054 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
2055
2056 if (fAdded)
2057 {
2058 /* Reference the instance because its pointer is being taken. */
2059 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
2060 }
2061 else
2062 {
2063 pRemoteUSBBackend = NULL;
2064 }
2065 }
2066
2067 unlockConsoleVRDPServer();
2068 }
2069
2070 if (pRemoteUSBBackend)
2071 {
2072 return pRemoteUSBBackend->GetBackendCallbackPointer();
2073 }
2074
2075#endif
2076 return NULL;
2077}
2078
2079void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
2080{
2081#ifdef VBOX_WITH_USB
2082 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2083
2084 /* Find the instance. */
2085 int rc = lockConsoleVRDPServer();
2086
2087 if (RT_SUCCESS(rc))
2088 {
2089 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
2090
2091 if (pRemoteUSBBackend)
2092 {
2093 pRemoteUSBBackend->removeUUID(pGuid);
2094 }
2095
2096 unlockConsoleVRDPServer();
2097
2098 if (pRemoteUSBBackend)
2099 {
2100 pRemoteUSBBackend->Release();
2101 }
2102 }
2103#endif
2104}
2105
2106RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
2107{
2108 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
2109
2110 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
2111#ifdef VBOX_WITH_USB
2112
2113 int rc = lockConsoleVRDPServer();
2114
2115 if (RT_SUCCESS(rc))
2116 {
2117 if (pRemoteUSBBackend == NULL)
2118 {
2119 /* The first backend in the list is requested. */
2120 pNextRemoteUSBBackend = mUSBBackends.pHead;
2121 }
2122 else
2123 {
2124 /* Get pointer to the next backend. */
2125 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2126 }
2127
2128 if (pNextRemoteUSBBackend)
2129 {
2130 pNextRemoteUSBBackend->AddRef();
2131 }
2132
2133 unlockConsoleVRDPServer();
2134
2135 if (pRemoteUSBBackend)
2136 {
2137 pRemoteUSBBackend->Release();
2138 }
2139 }
2140#endif
2141
2142 return pNextRemoteUSBBackend;
2143}
2144
2145#ifdef VBOX_WITH_USB
2146/* Internal method. Called under the ConsoleVRDPServerLock. */
2147RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
2148{
2149 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2150
2151 while (pRemoteUSBBackend)
2152 {
2153 if (pRemoteUSBBackend->ClientId() == u32ClientId)
2154 {
2155 break;
2156 }
2157
2158 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2159 }
2160
2161 return pRemoteUSBBackend;
2162}
2163
2164/* Internal method. Called under the ConsoleVRDPServerLock. */
2165RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
2166{
2167 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2168
2169 while (pRemoteUSBBackend)
2170 {
2171 if (pRemoteUSBBackend->findUUID(pGuid))
2172 {
2173 break;
2174 }
2175
2176 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2177 }
2178
2179 return pRemoteUSBBackend;
2180}
2181#endif
2182
2183/* Internal method. Called by the backend destructor. */
2184void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2185{
2186#ifdef VBOX_WITH_USB
2187 int rc = lockConsoleVRDPServer();
2188 AssertRC(rc);
2189
2190 /* Exclude the found instance from the list. */
2191 if (pRemoteUSBBackend->pNext)
2192 {
2193 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2194 }
2195 else
2196 {
2197 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2198 }
2199
2200 if (pRemoteUSBBackend->pPrev)
2201 {
2202 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2203 }
2204 else
2205 {
2206 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2207 }
2208
2209 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2210
2211 unlockConsoleVRDPServer();
2212#endif
2213}
2214
2215
2216void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2217{
2218 if (mpEntryPoints && mhServer)
2219 {
2220 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2221 }
2222}
2223
2224void ConsoleVRDPServer::SendResize(void) const
2225{
2226 if (mpEntryPoints && mhServer)
2227 {
2228 mpEntryPoints->VRDEResize(mhServer);
2229 }
2230}
2231
2232void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2233{
2234 VRDEORDERHDR update;
2235 update.x = x;
2236 update.y = y;
2237 update.w = w;
2238 update.h = h;
2239 if (mpEntryPoints && mhServer)
2240 {
2241 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
2242 }
2243}
2244
2245void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
2246{
2247 if (mpEntryPoints && mhServer)
2248 {
2249 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
2250 }
2251}
2252
2253void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2254{
2255 if (mpEntryPoints && mhServer)
2256 {
2257 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
2258 }
2259}
2260
2261void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2262{
2263 if (mpEntryPoints && mhServer)
2264 {
2265 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2266 }
2267}
2268
2269/* @todo rc not needed? */
2270int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
2271 void *pvContext,
2272 uint32_t cSamples,
2273 uint32_t iSampleHz,
2274 uint32_t cChannels,
2275 uint32_t cBits)
2276{
2277 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
2278 {
2279 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2280 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2281 {
2282 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
2283 mpEntryPoints->VRDEAudioInOpen (mhServer,
2284 pvContext,
2285 u32ClientId,
2286 audioFormat,
2287 cSamples);
2288 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
2289 * Currently not used because only one client is allowed to
2290 * do audio input and the client id is saved by the ConsoleVRDPServer.
2291 */
2292
2293 return VINF_SUCCESS;
2294 }
2295 }
2296 return VERR_NOT_SUPPORTED;
2297}
2298
2299void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
2300{
2301 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
2302 {
2303 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2304 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2305 {
2306 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
2307 }
2308 }
2309}
2310
2311
2312
2313void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2314{
2315 if (index == VRDE_QI_PORT)
2316 {
2317 uint32_t cbOut = sizeof(int32_t);
2318
2319 if (cbBuffer >= cbOut)
2320 {
2321 *pcbOut = cbOut;
2322 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2323 }
2324 }
2325 else if (mpEntryPoints && mhServer)
2326 {
2327 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2328 }
2329}
2330
2331/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
2332{
2333 int rc = VINF_SUCCESS;
2334
2335 if (mVRDPLibrary == NIL_RTLDRMOD)
2336 {
2337 RTERRINFOSTATIC ErrInfo;
2338 RTErrInfoInitStatic(&ErrInfo);
2339
2340 if (RTPathHavePath(pszLibraryName))
2341 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
2342 else
2343 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
2344 if (RT_SUCCESS(rc))
2345 {
2346 struct SymbolEntry
2347 {
2348 const char *name;
2349 void **ppfn;
2350 };
2351
2352 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2353
2354 static const struct SymbolEntry s_aSymbols[] =
2355 {
2356 DEFSYMENTRY(VRDECreateServer)
2357 };
2358
2359 #undef DEFSYMENTRY
2360
2361 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
2362 {
2363 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
2364
2365 if (RT_FAILURE(rc))
2366 {
2367 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
2368 break;
2369 }
2370 }
2371 }
2372 else
2373 {
2374 if (RTErrInfoIsSet(&ErrInfo.Core))
2375 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
2376 else
2377 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
2378
2379 mVRDPLibrary = NIL_RTLDRMOD;
2380 }
2381 }
2382
2383 if (RT_FAILURE(rc))
2384 {
2385 if (mVRDPLibrary != NIL_RTLDRMOD)
2386 {
2387 RTLdrClose(mVRDPLibrary);
2388 mVRDPLibrary = NIL_RTLDRMOD;
2389 }
2390 }
2391
2392 return rc;
2393}
2394
2395/*
2396 * IVRDEServerInfo implementation.
2397 */
2398// constructor / destructor
2399/////////////////////////////////////////////////////////////////////////////
2400
2401VRDEServerInfo::VRDEServerInfo()
2402 : mParent(NULL)
2403{
2404}
2405
2406VRDEServerInfo::~VRDEServerInfo()
2407{
2408}
2409
2410
2411HRESULT VRDEServerInfo::FinalConstruct()
2412{
2413 return BaseFinalConstruct();
2414}
2415
2416void VRDEServerInfo::FinalRelease()
2417{
2418 uninit();
2419 BaseFinalRelease();
2420}
2421
2422// public methods only for internal purposes
2423/////////////////////////////////////////////////////////////////////////////
2424
2425/**
2426 * Initializes the guest object.
2427 */
2428HRESULT VRDEServerInfo::init(Console *aParent)
2429{
2430 LogFlowThisFunc(("aParent=%p\n", aParent));
2431
2432 ComAssertRet(aParent, E_INVALIDARG);
2433
2434 /* Enclose the state transition NotReady->InInit->Ready */
2435 AutoInitSpan autoInitSpan(this);
2436 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2437
2438 unconst(mParent) = aParent;
2439
2440 /* Confirm a successful initialization */
2441 autoInitSpan.setSucceeded();
2442
2443 return S_OK;
2444}
2445
2446/**
2447 * Uninitializes the instance and sets the ready flag to FALSE.
2448 * Called either from FinalRelease() or by the parent when it gets destroyed.
2449 */
2450void VRDEServerInfo::uninit()
2451{
2452 LogFlowThisFunc(("\n"));
2453
2454 /* Enclose the state transition Ready->InUninit->NotReady */
2455 AutoUninitSpan autoUninitSpan(this);
2456 if (autoUninitSpan.uninitDone())
2457 return;
2458
2459 unconst(mParent) = NULL;
2460}
2461
2462// IVRDEServerInfo properties
2463/////////////////////////////////////////////////////////////////////////////
2464
2465#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2466 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2467 { \
2468 if (!a##_aName) \
2469 return E_POINTER; \
2470 \
2471 AutoCaller autoCaller(this); \
2472 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2473 \
2474 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2475 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2476 \
2477 uint32_t value; \
2478 uint32_t cbOut = 0; \
2479 \
2480 mParent->consoleVRDPServer()->QueryInfo \
2481 (_aIndex, &value, sizeof(value), &cbOut); \
2482 \
2483 *a##_aName = cbOut? !!value: FALSE; \
2484 \
2485 return S_OK; \
2486 } \
2487 extern void IMPL_GETTER_BOOL_DUMMY(void)
2488
2489#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
2490 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2491 { \
2492 if (!a##_aName) \
2493 return E_POINTER; \
2494 \
2495 AutoCaller autoCaller(this); \
2496 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2497 \
2498 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2499 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2500 \
2501 _aType value; \
2502 uint32_t cbOut = 0; \
2503 \
2504 mParent->consoleVRDPServer()->QueryInfo \
2505 (_aIndex, &value, sizeof(value), &cbOut); \
2506 \
2507 if (_aValueMask) value &= (_aValueMask); \
2508 *a##_aName = cbOut? value: 0; \
2509 \
2510 return S_OK; \
2511 } \
2512 extern void IMPL_GETTER_SCALAR_DUMMY(void)
2513
2514#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2515 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2516 { \
2517 if (!a##_aName) \
2518 return E_POINTER; \
2519 \
2520 AutoCaller autoCaller(this); \
2521 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2522 \
2523 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2524 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2525 \
2526 uint32_t cbOut = 0; \
2527 \
2528 mParent->consoleVRDPServer()->QueryInfo \
2529 (_aIndex, NULL, 0, &cbOut); \
2530 \
2531 if (cbOut == 0) \
2532 { \
2533 Bstr str(""); \
2534 str.cloneTo(a##_aName); \
2535 return S_OK; \
2536 } \
2537 \
2538 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
2539 \
2540 if (!pchBuffer) \
2541 { \
2542 Log(("VRDEServerInfo::" \
2543 #_aName \
2544 ": Failed to allocate memory %d bytes\n", cbOut)); \
2545 return E_OUTOFMEMORY; \
2546 } \
2547 \
2548 mParent->consoleVRDPServer()->QueryInfo \
2549 (_aIndex, pchBuffer, cbOut, &cbOut); \
2550 \
2551 Bstr str(pchBuffer); \
2552 \
2553 str.cloneTo(a##_aName); \
2554 \
2555 RTMemTmpFree(pchBuffer); \
2556 \
2557 return S_OK; \
2558 } \
2559 extern void IMPL_GETTER_BSTR_DUMMY(void)
2560
2561IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
2562IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
2563IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
2564IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
2565IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
2566IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
2567IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
2568IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
2569IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
2570IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
2571IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
2572IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
2573IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
2574IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
2575IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
2576
2577#undef IMPL_GETTER_BSTR
2578#undef IMPL_GETTER_SCALAR
2579#undef IMPL_GETTER_BOOL
2580/* 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