VirtualBox

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

Last change on this file since 43388 was 43388, checked in by vboxsync, 13 years ago

ConsoleVRDPServer: raw TSMF close channel.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 115.8 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 43388 2012-09-21 09:51:44Z vboxsync $ */
2/** @file
3 * VBox Console VRDP Helper class
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#include "VMMDev.h"
28#ifdef VBOX_WITH_USB_CARDREADER
29# include "UsbCardReader.h"
30#endif
31
32#include "Global.h"
33#include "AutoCaller.h"
34#include "Logging.h"
35
36#include <iprt/asm.h>
37#include <iprt/alloca.h>
38#include <iprt/ldr.h>
39#include <iprt/param.h>
40#include <iprt/path.h>
41#include <iprt/cpp/utils.h>
42
43#include <VBox/err.h>
44#include <VBox/RemoteDesktop/VRDEOrders.h>
45#include <VBox/com/listeners.h>
46#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
47
48class VRDPConsoleListener
49{
50public:
51 VRDPConsoleListener()
52 {
53 }
54
55 HRESULT init(ConsoleVRDPServer *server)
56 {
57 m_server = server;
58 return S_OK;
59 }
60
61 void uninit()
62 {
63 }
64
65 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
66 {
67 switch (aType)
68 {
69 case VBoxEventType_OnMousePointerShapeChanged:
70 {
71 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
72 Assert(mpscev);
73 BOOL visible, alpha;
74 ULONG xHot, yHot, width, height;
75 com::SafeArray <BYTE> shape;
76
77 mpscev->COMGETTER(Visible)(&visible);
78 mpscev->COMGETTER(Alpha)(&alpha);
79 mpscev->COMGETTER(Xhot)(&xHot);
80 mpscev->COMGETTER(Yhot)(&yHot);
81 mpscev->COMGETTER(Width)(&width);
82 mpscev->COMGETTER(Height)(&height);
83 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
84
85 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
86 break;
87 }
88 case VBoxEventType_OnMouseCapabilityChanged:
89 {
90 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
91 Assert(mccev);
92 if (m_server)
93 {
94 BOOL fAbsoluteMouse;
95 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
96 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
97 }
98 break;
99 }
100 case VBoxEventType_OnKeyboardLedsChanged:
101 {
102 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
103 Assert(klcev);
104
105 if (m_server)
106 {
107 BOOL fNumLock, fCapsLock, fScrollLock;
108 klcev->COMGETTER(NumLock)(&fNumLock);
109 klcev->COMGETTER(CapsLock)(&fCapsLock);
110 klcev->COMGETTER(ScrollLock)(&fScrollLock);
111 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
112 }
113 break;
114 }
115
116 default:
117 AssertFailed();
118 }
119
120 return S_OK;
121 }
122
123private:
124 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
125 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
126 ConsoleVRDPServer *m_server;
127};
128
129typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
130
131VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
132
133#ifdef DEBUG_sunlover
134#define LOGDUMPPTR Log
135void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
136{
137 unsigned i;
138
139 const uint8_t *pu8And = pu8Shape;
140
141 for (i = 0; i < height; i++)
142 {
143 unsigned j;
144 LOGDUMPPTR(("%p: ", pu8And));
145 for (j = 0; j < (width + 7) / 8; j++)
146 {
147 unsigned k;
148 for (k = 0; k < 8; k++)
149 {
150 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
151 }
152
153 pu8And++;
154 }
155 LOGDUMPPTR(("\n"));
156 }
157
158 if (fXorMaskRGB32)
159 {
160 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
161
162 for (i = 0; i < height; i++)
163 {
164 unsigned j;
165 LOGDUMPPTR(("%p: ", pu32Xor));
166 for (j = 0; j < width; j++)
167 {
168 LOGDUMPPTR(("%08X", *pu32Xor++));
169 }
170 LOGDUMPPTR(("\n"));
171 }
172 }
173 else
174 {
175 /* RDP 24 bit RGB mask. */
176 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
177 for (i = 0; i < height; i++)
178 {
179 unsigned j;
180 LOGDUMPPTR(("%p: ", pu8Xor));
181 for (j = 0; j < width; j++)
182 {
183 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
184 pu8Xor += 3;
185 }
186 LOGDUMPPTR(("\n"));
187 }
188 }
189}
190#else
191#define dumpPointer(a, b, c, d) do {} while (0)
192#endif /* DEBUG_sunlover */
193
194static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
195{
196 /*
197 * Find the top border of the AND mask. First assign to special value.
198 */
199 uint32_t ySkipAnd = ~0;
200
201 const uint8_t *pu8And = pu8AndMask;
202 const uint32_t cbAndRow = (width + 7) / 8;
203 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
204
205 Assert(cbAndRow > 0);
206
207 unsigned y;
208 unsigned x;
209
210 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
211 {
212 /* For each complete byte in the row. */
213 for (x = 0; x < cbAndRow - 1; x++)
214 {
215 if (pu8And[x] != 0xFF)
216 {
217 ySkipAnd = y;
218 break;
219 }
220 }
221
222 if (ySkipAnd == ~(uint32_t)0)
223 {
224 /* Last byte. */
225 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
226 {
227 ySkipAnd = y;
228 }
229 }
230 }
231
232 if (ySkipAnd == ~(uint32_t)0)
233 {
234 ySkipAnd = 0;
235 }
236
237 /*
238 * Find the left border of the AND mask.
239 */
240 uint32_t xSkipAnd = ~0;
241
242 /* For all bit columns. */
243 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
244 {
245 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
246 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
247
248 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
249 {
250 if ((*pu8And & mask) == 0)
251 {
252 xSkipAnd = x;
253 break;
254 }
255 }
256 }
257
258 if (xSkipAnd == ~(uint32_t)0)
259 {
260 xSkipAnd = 0;
261 }
262
263 /*
264 * Find the XOR mask top border.
265 */
266 uint32_t ySkipXor = ~0;
267
268 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
269
270 uint32_t *pu32Xor = pu32XorStart;
271
272 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
273 {
274 for (x = 0; x < width; x++)
275 {
276 if (pu32Xor[x] != 0)
277 {
278 ySkipXor = y;
279 break;
280 }
281 }
282 }
283
284 if (ySkipXor == ~(uint32_t)0)
285 {
286 ySkipXor = 0;
287 }
288
289 /*
290 * Find the left border of the XOR mask.
291 */
292 uint32_t xSkipXor = ~(uint32_t)0;
293
294 /* For all columns. */
295 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
296 {
297 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
298
299 for (y = ySkipXor; y < height; y++, pu32Xor += width)
300 {
301 if (*pu32Xor != 0)
302 {
303 xSkipXor = x;
304 break;
305 }
306 }
307 }
308
309 if (xSkipXor == ~(uint32_t)0)
310 {
311 xSkipXor = 0;
312 }
313
314 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
315 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
316}
317
318/* Generate an AND mask for alpha pointers here, because
319 * guest driver does not do that correctly for Vista pointers.
320 * Similar fix, changing the alpha threshold, could be applied
321 * for the guest driver, but then additions reinstall would be
322 * necessary, which we try to avoid.
323 */
324static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
325{
326 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
327
328 int y;
329 for (y = 0; y < h; y++)
330 {
331 uint8_t bitmask = 0x80;
332
333 int x;
334 for (x = 0; x < w; x++, bitmask >>= 1)
335 {
336 if (bitmask == 0)
337 {
338 bitmask = 0x80;
339 }
340
341 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
342 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
343 {
344 pu8DstAndMask[x / 8] &= ~bitmask;
345 }
346 }
347
348 /* Point to next source and dest scans. */
349 pu8SrcAlpha += w * 4;
350 pu8DstAndMask += (w + 7) / 8;
351 }
352}
353
354STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
355 BOOL alpha,
356 ULONG xHot,
357 ULONG yHot,
358 ULONG width,
359 ULONG height,
360 ComSafeArrayIn(BYTE,inShape))
361{
362 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
363
364 if (m_server)
365 {
366 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
367 if (aShape.size() == 0)
368 {
369 if (!visible)
370 {
371 m_server->MousePointerHide();
372 }
373 }
374 else if (width != 0 && height != 0)
375 {
376 uint8_t* shape = aShape.raw();
377
378 dumpPointer(shape, width, height, true);
379
380 if (m_server->MousePointer(alpha, xHot, yHot, width, height, shape) == VINF_SUCCESS)
381 {
382 return S_OK;
383 }
384
385 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
386 * 'shape' AND mask followed by XOR mask.
387 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
388 *
389 * We convert this to RDP color format which consist of
390 * one bpp AND mask and 24 BPP (BGR) color XOR image.
391 *
392 * RDP clients expect 8 aligned width and height of
393 * pointer (preferably 32x32).
394 *
395 * They even contain bugs which do not appear for
396 * 32x32 pointers but would appear for a 41x32 one.
397 *
398 * So set pointer size to 32x32. This can be done safely
399 * because most pointers are 32x32.
400 */
401
402 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
403
404 uint8_t *pu8AndMask = shape;
405 uint8_t *pu8XorMask = shape + cbDstAndMask;
406
407 if (alpha)
408 {
409 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
410
411 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
412 }
413
414 /* Windows guest alpha pointers are wider than 32 pixels.
415 * Try to find out the top-left border of the pointer and
416 * then copy only meaningful bits. All complete top rows
417 * and all complete left columns where (AND == 1 && XOR == 0)
418 * are skipped. Hot spot is adjusted.
419 */
420 uint32_t ySkip = 0; /* How many rows to skip at the top. */
421 uint32_t xSkip = 0; /* How many columns to skip at the left. */
422
423 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
424
425 /* Must not skip the hot spot. */
426 xSkip = RT_MIN(xSkip, xHot);
427 ySkip = RT_MIN(ySkip, yHot);
428
429 /*
430 * Compute size and allocate memory for the pointer.
431 */
432 const uint32_t dstwidth = 32;
433 const uint32_t dstheight = 32;
434
435 VRDECOLORPOINTER *pointer = NULL;
436
437 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
438
439 uint32_t rdpmaskwidth = dstmaskwidth;
440 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
441
442 uint32_t rdpdatawidth = dstwidth * 3;
443 uint32_t rdpdatalen = dstheight * rdpdatawidth;
444
445 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
446
447 if (pointer)
448 {
449 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
450 uint8_t *dataarray = maskarray + rdpmasklen;
451
452 memset(maskarray, 0xFF, rdpmasklen);
453 memset(dataarray, 0x00, rdpdatalen);
454
455 uint32_t srcmaskwidth = (width + 7) / 8;
456 uint32_t srcdatawidth = width * 4;
457
458 /* Copy AND mask. */
459 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
460 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
461
462 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
463 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
464
465 unsigned x, y;
466
467 for (y = 0; y < minheight; y++)
468 {
469 for (x = 0; x < minwidth; x++)
470 {
471 uint32_t byteIndex = (x + xSkip) / 8;
472 uint32_t bitIndex = (x + xSkip) % 8;
473
474 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
475
476 if (!bit)
477 {
478 byteIndex = x / 8;
479 bitIndex = x % 8;
480
481 dst[byteIndex] &= ~(1 << (7 - bitIndex));
482 }
483 }
484
485 src += srcmaskwidth;
486 dst -= rdpmaskwidth;
487 }
488
489 /* Point src to XOR mask */
490 src = pu8XorMask + ySkip * srcdatawidth;
491 dst = dataarray + (dstheight - 1) * rdpdatawidth;
492
493 for (y = 0; y < minheight ; y++)
494 {
495 for (x = 0; x < minwidth; x++)
496 {
497 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
498 }
499
500 src += srcdatawidth;
501 dst -= rdpdatawidth;
502 }
503
504 pointer->u16HotX = (uint16_t)(xHot - xSkip);
505 pointer->u16HotY = (uint16_t)(yHot - ySkip);
506
507 pointer->u16Width = (uint16_t)dstwidth;
508 pointer->u16Height = (uint16_t)dstheight;
509
510 pointer->u16MaskLen = (uint16_t)rdpmasklen;
511 pointer->u16DataLen = (uint16_t)rdpdatalen;
512
513 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
514
515 m_server->MousePointerUpdate(pointer);
516
517 RTMemTmpFree(pointer);
518 }
519 }
520 }
521
522 return S_OK;
523}
524
525
526// ConsoleVRDPServer
527////////////////////////////////////////////////////////////////////////////////
528
529RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
530
531PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
532
533VRDEENTRYPOINTS_4 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
534VRDEENTRYPOINTS_4 *ConsoleVRDPServer::mpEntryPoints = NULL;
535
536VRDECALLBACKS_4 ConsoleVRDPServer::mCallbacks =
537{
538 { VRDE_INTERFACE_VERSION_4, sizeof(VRDECALLBACKS_4) },
539 ConsoleVRDPServer::VRDPCallbackQueryProperty,
540 ConsoleVRDPServer::VRDPCallbackClientLogon,
541 ConsoleVRDPServer::VRDPCallbackClientConnect,
542 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
543 ConsoleVRDPServer::VRDPCallbackIntercept,
544 ConsoleVRDPServer::VRDPCallbackUSB,
545 ConsoleVRDPServer::VRDPCallbackClipboard,
546 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
547 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
548 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
549 ConsoleVRDPServer::VRDPCallbackInput,
550 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
551 ConsoleVRDPServer::VRDECallbackAudioIn
552};
553
554DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
555{
556 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
557
558 int rc = VERR_NOT_SUPPORTED;
559
560 switch (index)
561 {
562 case VRDE_QP_NETWORK_PORT:
563 {
564 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
565 ULONG port = 0;
566
567 if (cbBuffer >= sizeof(uint32_t))
568 {
569 *(uint32_t *)pvBuffer = (uint32_t)port;
570 rc = VINF_SUCCESS;
571 }
572 else
573 {
574 rc = VINF_BUFFER_OVERFLOW;
575 }
576
577 *pcbOut = sizeof(uint32_t);
578 } break;
579
580 case VRDE_QP_NETWORK_ADDRESS:
581 {
582 com::Bstr bstr;
583 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
584
585 /* The server expects UTF8. */
586 com::Utf8Str address = bstr;
587
588 size_t cbAddress = address.length() + 1;
589
590 if (cbAddress >= 0x10000)
591 {
592 /* More than 64K seems to be an invalid address. */
593 rc = VERR_TOO_MUCH_DATA;
594 break;
595 }
596
597 if ((size_t)cbBuffer >= cbAddress)
598 {
599 memcpy(pvBuffer, address.c_str(), cbAddress);
600 rc = VINF_SUCCESS;
601 }
602 else
603 {
604 rc = VINF_BUFFER_OVERFLOW;
605 }
606
607 *pcbOut = (uint32_t)cbAddress;
608 } break;
609
610 case VRDE_QP_NUMBER_MONITORS:
611 {
612 ULONG cMonitors = 1;
613
614 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
615
616 if (cbBuffer >= sizeof(uint32_t))
617 {
618 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
619 rc = VINF_SUCCESS;
620 }
621 else
622 {
623 rc = VINF_BUFFER_OVERFLOW;
624 }
625
626 *pcbOut = sizeof(uint32_t);
627 } break;
628
629 case VRDE_QP_NETWORK_PORT_RANGE:
630 {
631 com::Bstr bstr;
632 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
633
634 if (hrc != S_OK)
635 {
636 bstr = "";
637 }
638
639 if (bstr == "0")
640 {
641 bstr = "3389";
642 }
643
644 /* The server expects UTF8. */
645 com::Utf8Str portRange = bstr;
646
647 size_t cbPortRange = portRange.length() + 1;
648
649 if (cbPortRange >= 0x10000)
650 {
651 /* More than 64K seems to be an invalid port range string. */
652 rc = VERR_TOO_MUCH_DATA;
653 break;
654 }
655
656 if ((size_t)cbBuffer >= cbPortRange)
657 {
658 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
659 rc = VINF_SUCCESS;
660 }
661 else
662 {
663 rc = VINF_BUFFER_OVERFLOW;
664 }
665
666 *pcbOut = (uint32_t)cbPortRange;
667 } break;
668
669#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
670 case VRDE_QP_VIDEO_CHANNEL:
671 {
672 com::Bstr bstr;
673 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(), bstr.asOutParam());
674
675 if (hrc != S_OK)
676 {
677 bstr = "";
678 }
679
680 com::Utf8Str value = bstr;
681
682 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
683 || RTStrICmp(value.c_str(), "1") == 0;
684
685 if (cbBuffer >= sizeof(uint32_t))
686 {
687 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
688 rc = VINF_SUCCESS;
689 }
690 else
691 {
692 rc = VINF_BUFFER_OVERFLOW;
693 }
694
695 *pcbOut = sizeof(uint32_t);
696 } break;
697
698 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
699 {
700 com::Bstr bstr;
701 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(), bstr.asOutParam());
702
703 if (hrc != S_OK)
704 {
705 bstr = "";
706 }
707
708 com::Utf8Str value = bstr;
709
710 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
711
712 if (cbBuffer >= sizeof(uint32_t))
713 {
714 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
715 rc = VINF_SUCCESS;
716 }
717 else
718 {
719 rc = VINF_BUFFER_OVERFLOW;
720 }
721
722 *pcbOut = sizeof(uint32_t);
723 } break;
724
725 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
726 {
727 ULONG ulSunFlsh = 1;
728
729 com::Bstr bstr;
730 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
731 bstr.asOutParam());
732 if (hrc == S_OK && !bstr.isEmpty())
733 {
734 com::Utf8Str sunFlsh = bstr;
735 if (!sunFlsh.isEmpty())
736 {
737 ulSunFlsh = sunFlsh.toUInt32();
738 }
739 }
740
741 if (cbBuffer >= sizeof(uint32_t))
742 {
743 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
744 rc = VINF_SUCCESS;
745 }
746 else
747 {
748 rc = VINF_BUFFER_OVERFLOW;
749 }
750
751 *pcbOut = sizeof(uint32_t);
752 } break;
753#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
754
755 case VRDE_QP_FEATURE:
756 {
757 if (cbBuffer < sizeof(VRDEFEATURE))
758 {
759 rc = VERR_INVALID_PARAMETER;
760 break;
761 }
762
763 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
764
765 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
766
767 size_t cchInfo = 0;
768 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
769
770 if (RT_FAILURE(rc))
771 {
772 rc = VERR_INVALID_PARAMETER;
773 break;
774 }
775
776 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
777
778 com::Bstr bstrValue;
779
780 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
781 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
782 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
783 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
784 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
785 )
786 {
787 /* @todo these features should be per client. */
788 NOREF(pFeature->u32ClientId);
789
790 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
791 com::Utf8Str extraData("VRDE/Feature/");
792 extraData += pFeature->achInfo;
793
794 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
795 bstrValue.asOutParam());
796 if (FAILED(hrc) || bstrValue.isEmpty())
797 {
798 /* Also try the old "VRDP/Feature/NAME" */
799 extraData = "VRDP/Feature/";
800 extraData += pFeature->achInfo;
801
802 hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
803 bstrValue.asOutParam());
804 if (FAILED(hrc))
805 {
806 rc = VERR_NOT_SUPPORTED;
807 }
808 }
809 }
810 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
811 {
812 /* Generic properties. */
813 const char *pszPropertyName = &pFeature->achInfo[9];
814 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(), bstrValue.asOutParam());
815 if (FAILED(hrc))
816 {
817 rc = VERR_NOT_SUPPORTED;
818 }
819 }
820 else
821 {
822 rc = VERR_NOT_SUPPORTED;
823 }
824
825 /* Copy the value string to the callers buffer. */
826 if (rc == VINF_SUCCESS)
827 {
828 com::Utf8Str value = bstrValue;
829
830 size_t cb = value.length() + 1;
831
832 if ((size_t)cbInfo >= cb)
833 {
834 memcpy(pFeature->achInfo, value.c_str(), cb);
835 }
836 else
837 {
838 rc = VINF_BUFFER_OVERFLOW;
839 }
840
841 *pcbOut = (uint32_t)cb;
842 }
843 } break;
844
845 case VRDE_SP_NETWORK_BIND_PORT:
846 {
847 if (cbBuffer != sizeof(uint32_t))
848 {
849 rc = VERR_INVALID_PARAMETER;
850 break;
851 }
852
853 ULONG port = *(uint32_t *)pvBuffer;
854
855 server->mVRDPBindPort = port;
856
857 rc = VINF_SUCCESS;
858
859 if (pcbOut)
860 {
861 *pcbOut = sizeof(uint32_t);
862 }
863
864 server->mConsole->onVRDEServerInfoChange();
865 } break;
866
867 case VRDE_SP_CLIENT_STATUS:
868 {
869 if (cbBuffer < sizeof(VRDECLIENTSTATUS))
870 {
871 rc = VERR_INVALID_PARAMETER;
872 break;
873 }
874
875 size_t cbStatus = cbBuffer - RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus);
876
877 VRDECLIENTSTATUS *pStatus = (VRDECLIENTSTATUS *)pvBuffer;
878
879 if (cbBuffer < RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus) + pStatus->cbStatus)
880 {
881 rc = VERR_INVALID_PARAMETER;
882 break;
883 }
884
885 size_t cchStatus = 0;
886 rc = RTStrNLenEx(pStatus->achStatus, cbStatus, &cchStatus);
887
888 if (RT_FAILURE(rc))
889 {
890 rc = VERR_INVALID_PARAMETER;
891 break;
892 }
893
894 Log(("VRDE_SP_CLIENT_STATUS [%s]\n", pStatus->achStatus));
895
896 server->mConsole->VRDPClientStatusChange(pStatus->u32ClientId, pStatus->achStatus);
897
898 rc = VINF_SUCCESS;
899
900 if (pcbOut)
901 {
902 *pcbOut = cbBuffer;
903 }
904
905 server->mConsole->onVRDEServerInfoChange();
906 } break;
907
908 default:
909 break;
910 }
911
912 return rc;
913}
914
915DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
916{
917 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
918
919 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
920}
921
922DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
923{
924 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
925
926 server->mConsole->VRDPClientConnect(u32ClientId);
927}
928
929DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
930{
931 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
932
933 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
934
935 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
936 {
937 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
938 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
939
940 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
941 if (pPort)
942 {
943 pPort->pfnAudioInputIntercept(pPort, false);
944 }
945 else
946 {
947 AssertFailed();
948 }
949 }
950}
951
952DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
953{
954 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
955
956 LogFlowFunc(("%x\n", fu32Intercept));
957
958 int rc = VERR_NOT_SUPPORTED;
959
960 switch (fu32Intercept)
961 {
962 case VRDE_CLIENT_INTERCEPT_AUDIO:
963 {
964 server->mConsole->VRDPInterceptAudio(u32ClientId);
965 if (ppvIntercept)
966 {
967 *ppvIntercept = server;
968 }
969 rc = VINF_SUCCESS;
970 } break;
971
972 case VRDE_CLIENT_INTERCEPT_USB:
973 {
974 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
975 rc = VINF_SUCCESS;
976 } break;
977
978 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
979 {
980 server->mConsole->VRDPInterceptClipboard(u32ClientId);
981 if (ppvIntercept)
982 {
983 *ppvIntercept = server;
984 }
985 rc = VINF_SUCCESS;
986 } break;
987
988 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
989 {
990 /* This request is processed internally by the ConsoleVRDPServer.
991 * Only one client is allowed to intercept audio input.
992 */
993 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
994 {
995 Log(("AUDIOIN: connected client %u\n", u32ClientId));
996
997 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
998 if (pPort)
999 {
1000 pPort->pfnAudioInputIntercept(pPort, true);
1001 if (ppvIntercept)
1002 {
1003 *ppvIntercept = server;
1004 }
1005 }
1006 else
1007 {
1008 AssertFailed();
1009 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
1010 rc = VERR_NOT_SUPPORTED;
1011 }
1012 }
1013 else
1014 {
1015 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
1016 rc = VERR_NOT_SUPPORTED;
1017 }
1018 } break;
1019
1020 default:
1021 break;
1022 }
1023
1024 return rc;
1025}
1026
1027DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1028{
1029#ifdef VBOX_WITH_USB
1030 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1031#else
1032 return VERR_NOT_SUPPORTED;
1033#endif
1034}
1035
1036DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
1037{
1038 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1039}
1040
1041DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
1042{
1043 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1044
1045 bool fAvailable = false;
1046
1047 IFramebuffer *pfb = NULL;
1048 LONG xOrigin = 0;
1049 LONG yOrigin = 0;
1050
1051 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1052
1053 if (pfb)
1054 {
1055 pfb->Lock ();
1056
1057 /* Query framebuffer parameters. */
1058 ULONG lineSize = 0;
1059 pfb->COMGETTER(BytesPerLine)(&lineSize);
1060
1061 ULONG bitsPerPixel = 0;
1062 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1063
1064 BYTE *address = NULL;
1065 pfb->COMGETTER(Address)(&address);
1066
1067 ULONG height = 0;
1068 pfb->COMGETTER(Height)(&height);
1069
1070 ULONG width = 0;
1071 pfb->COMGETTER(Width)(&width);
1072
1073 /* Now fill the information as requested by the caller. */
1074 pInfo->pu8Bits = address;
1075 pInfo->xOrigin = xOrigin;
1076 pInfo->yOrigin = yOrigin;
1077 pInfo->cWidth = width;
1078 pInfo->cHeight = height;
1079 pInfo->cBitsPerPixel = bitsPerPixel;
1080 pInfo->cbLine = lineSize;
1081
1082 pfb->Unlock();
1083
1084 fAvailable = true;
1085 }
1086
1087 if (server->maFramebuffers[uScreenId])
1088 {
1089 server->maFramebuffers[uScreenId]->Release();
1090 }
1091 server->maFramebuffers[uScreenId] = pfb;
1092
1093 return fAvailable;
1094}
1095
1096DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1097{
1098 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1099
1100 if (server->maFramebuffers[uScreenId])
1101 {
1102 server->maFramebuffers[uScreenId]->Lock();
1103 }
1104}
1105
1106DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1107{
1108 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1109
1110 if (server->maFramebuffers[uScreenId])
1111 {
1112 server->maFramebuffers[uScreenId]->Unlock();
1113 }
1114}
1115
1116static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1117{
1118 if ( pInputSynch->cGuestNumLockAdaptions
1119 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1120 {
1121 pInputSynch->cGuestNumLockAdaptions--;
1122 pKeyboard->PutScancode(0x45);
1123 pKeyboard->PutScancode(0x45 | 0x80);
1124 }
1125 if ( pInputSynch->cGuestCapsLockAdaptions
1126 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1127 {
1128 pInputSynch->cGuestCapsLockAdaptions--;
1129 pKeyboard->PutScancode(0x3a);
1130 pKeyboard->PutScancode(0x3a | 0x80);
1131 }
1132}
1133
1134DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1135{
1136 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1137 Console *pConsole = server->mConsole;
1138
1139 switch (type)
1140 {
1141 case VRDE_INPUT_SCANCODE:
1142 {
1143 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1144 {
1145 IKeyboard *pKeyboard = pConsole->getKeyboard();
1146
1147 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1148
1149 /* Track lock keys. */
1150 if (pInputScancode->uScancode == 0x45)
1151 {
1152 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1153 }
1154 else if (pInputScancode->uScancode == 0x3a)
1155 {
1156 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1157 }
1158 else if (pInputScancode->uScancode == 0x46)
1159 {
1160 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1161 }
1162 else if ((pInputScancode->uScancode & 0x80) == 0)
1163 {
1164 /* Key pressed. */
1165 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1166 }
1167
1168 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1169 }
1170 } break;
1171
1172 case VRDE_INPUT_POINT:
1173 {
1174 if (cbInput == sizeof(VRDEINPUTPOINT))
1175 {
1176 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1177
1178 int mouseButtons = 0;
1179 int iWheel = 0;
1180
1181 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1182 {
1183 mouseButtons |= MouseButtonState_LeftButton;
1184 }
1185 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1186 {
1187 mouseButtons |= MouseButtonState_RightButton;
1188 }
1189 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1190 {
1191 mouseButtons |= MouseButtonState_MiddleButton;
1192 }
1193 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1194 {
1195 mouseButtons |= MouseButtonState_WheelUp;
1196 iWheel = -1;
1197 }
1198 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1199 {
1200 mouseButtons |= MouseButtonState_WheelDown;
1201 iWheel = 1;
1202 }
1203
1204 if (server->m_fGuestWantsAbsolute)
1205 {
1206 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1207 } else
1208 {
1209 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1210 pInputPoint->y - server->m_mousey,
1211 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1212 server->m_mousex = pInputPoint->x;
1213 server->m_mousey = pInputPoint->y;
1214 }
1215 }
1216 } break;
1217
1218 case VRDE_INPUT_CAD:
1219 {
1220 pConsole->getKeyboard()->PutCAD();
1221 } break;
1222
1223 case VRDE_INPUT_RESET:
1224 {
1225 pConsole->Reset();
1226 } break;
1227
1228 case VRDE_INPUT_SYNCH:
1229 {
1230 if (cbInput == sizeof(VRDEINPUTSYNCH))
1231 {
1232 IKeyboard *pKeyboard = pConsole->getKeyboard();
1233
1234 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1235
1236 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1237 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1238 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1239
1240 /* The client initiated synchronization. Always make the guest to reflect the client state.
1241 * Than means, when the guest changes the state itself, it is forced to return to the client
1242 * state.
1243 */
1244 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1245 {
1246 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1247 }
1248
1249 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1250 {
1251 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1252 }
1253
1254 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1255 }
1256 } break;
1257
1258 default:
1259 break;
1260 }
1261}
1262
1263DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1264{
1265 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1266
1267 server->mConsole->getDisplay()->SetVideoModeHint(uScreenId, TRUE /*=enabled*/,
1268 FALSE /*=changeOrigin*/, 0/*=OriginX*/, 0/*=OriginY*/,
1269 cWidth, cHeight, cBitsPerPixel);
1270}
1271
1272DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1273 void *pvCtx,
1274 uint32_t u32ClientId,
1275 uint32_t u32Event,
1276 const void *pvData,
1277 uint32_t cbData)
1278{
1279 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1280
1281 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1282
1283 switch (u32Event)
1284 {
1285 case VRDE_AUDIOIN_BEGIN:
1286 {
1287 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1288
1289 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1290 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1291 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1292 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1293 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1294 );
1295 } break;
1296
1297 case VRDE_AUDIOIN_DATA:
1298 {
1299 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1300 } break;
1301
1302 case VRDE_AUDIOIN_END:
1303 {
1304 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1305 } break;
1306
1307 default:
1308 return;
1309 }
1310}
1311
1312
1313ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1314{
1315 mConsole = console;
1316
1317 int rc = RTCritSectInit(&mCritSect);
1318 AssertRC(rc);
1319
1320 mcClipboardRefs = 0;
1321 mpfnClipboardCallback = NULL;
1322
1323#ifdef VBOX_WITH_USB
1324 mUSBBackends.pHead = NULL;
1325 mUSBBackends.pTail = NULL;
1326
1327 mUSBBackends.thread = NIL_RTTHREAD;
1328 mUSBBackends.fThreadRunning = false;
1329 mUSBBackends.event = 0;
1330#endif
1331
1332 mhServer = 0;
1333 mServerInterfaceVersion = 0;
1334
1335 m_fGuestWantsAbsolute = false;
1336 m_mousex = 0;
1337 m_mousey = 0;
1338
1339 m_InputSynch.cGuestNumLockAdaptions = 2;
1340 m_InputSynch.cGuestCapsLockAdaptions = 2;
1341
1342 m_InputSynch.fGuestNumLock = false;
1343 m_InputSynch.fGuestCapsLock = false;
1344 m_InputSynch.fGuestScrollLock = false;
1345
1346 m_InputSynch.fClientNumLock = false;
1347 m_InputSynch.fClientCapsLock = false;
1348 m_InputSynch.fClientScrollLock = false;
1349
1350 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1351
1352 {
1353 ComPtr<IEventSource> es;
1354 console->COMGETTER(EventSource)(es.asOutParam());
1355 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1356 aConsoleListener.createObject();
1357 aConsoleListener->init(new VRDPConsoleListener(), this);
1358 mConsoleListener = aConsoleListener;
1359 com::SafeArray <VBoxEventType_T> eventTypes;
1360 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1361 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1362 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1363 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1364 }
1365
1366 mVRDPBindPort = -1;
1367
1368 mAuthLibrary = 0;
1369
1370 mu32AudioInputClientId = 0;
1371
1372 /*
1373 * Optional interfaces.
1374 */
1375 m_fInterfaceImage = false;
1376 memset(&m_interfaceImage, 0, sizeof (m_interfaceImage));
1377 memset(&m_interfaceCallbacksImage, 0, sizeof (m_interfaceCallbacksImage));
1378 RT_ZERO(m_interfaceMousePtr);
1379 RT_ZERO(m_interfaceSCard);
1380 RT_ZERO(m_interfaceCallbacksSCard);
1381 RT_ZERO(m_interfaceTSMF);
1382 RT_ZERO(m_interfaceCallbacksTSMF);
1383}
1384
1385ConsoleVRDPServer::~ConsoleVRDPServer()
1386{
1387 Stop();
1388
1389 if (mConsoleListener)
1390 {
1391 ComPtr<IEventSource> es;
1392 mConsole->COMGETTER(EventSource)(es.asOutParam());
1393 es->UnregisterListener(mConsoleListener);
1394 mConsoleListener.setNull();
1395 }
1396
1397 unsigned i;
1398 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1399 {
1400 if (maFramebuffers[i])
1401 {
1402 maFramebuffers[i]->Release();
1403 maFramebuffers[i] = NULL;
1404 }
1405 }
1406
1407 if (RTCritSectIsInitialized(&mCritSect))
1408 {
1409 RTCritSectDelete(&mCritSect);
1410 memset(&mCritSect, 0, sizeof(mCritSect));
1411 }
1412}
1413
1414int ConsoleVRDPServer::Launch(void)
1415{
1416 LogFlowThisFunc(("\n"));
1417
1418 IVRDEServer *server = mConsole->getVRDEServer();
1419 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1420
1421 /*
1422 * Check if VRDE is enabled.
1423 */
1424 BOOL fEnabled;
1425 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1426 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1427 if (!fEnabled)
1428 return VINF_SUCCESS;
1429
1430 /*
1431 * Check that a VRDE extension pack name is set and resolve it into a
1432 * library path.
1433 */
1434 Bstr bstrExtPack;
1435 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1436 if (FAILED(hrc))
1437 return Global::vboxStatusCodeFromCOM(hrc);
1438 if (bstrExtPack.isEmpty())
1439 return VINF_NOT_SUPPORTED;
1440
1441 Utf8Str strExtPack(bstrExtPack);
1442 Utf8Str strVrdeLibrary;
1443 int vrc = VINF_SUCCESS;
1444 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1445 strVrdeLibrary = "VBoxVRDP";
1446 else
1447 {
1448#ifdef VBOX_WITH_EXTPACK
1449 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1450 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1451#else
1452 vrc = VERR_FILE_NOT_FOUND;
1453#endif
1454 }
1455 if (RT_SUCCESS(vrc))
1456 {
1457 /*
1458 * Load the VRDE library and start the server, if it is enabled.
1459 */
1460 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1461 if (RT_SUCCESS(vrc))
1462 {
1463 VRDEENTRYPOINTS_4 *pEntryPoints4;
1464 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1465
1466 if (RT_SUCCESS(vrc))
1467 {
1468 mServerInterfaceVersion = 4;
1469 mEntryPoints = *pEntryPoints4;
1470 mpEntryPoints = &mEntryPoints;
1471 }
1472 else if (vrc == VERR_VERSION_MISMATCH)
1473 {
1474 /* An older version of VRDE is installed, try version 3. */
1475 VRDEENTRYPOINTS_3 *pEntryPoints3;
1476
1477 static VRDECALLBACKS_3 sCallbacks3 =
1478 {
1479 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1480 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1481 ConsoleVRDPServer::VRDPCallbackClientLogon,
1482 ConsoleVRDPServer::VRDPCallbackClientConnect,
1483 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1484 ConsoleVRDPServer::VRDPCallbackIntercept,
1485 ConsoleVRDPServer::VRDPCallbackUSB,
1486 ConsoleVRDPServer::VRDPCallbackClipboard,
1487 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1488 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1489 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1490 ConsoleVRDPServer::VRDPCallbackInput,
1491 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1492 ConsoleVRDPServer::VRDECallbackAudioIn
1493 };
1494
1495 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1496 if (RT_SUCCESS(vrc))
1497 {
1498 mServerInterfaceVersion = 3;
1499 mEntryPoints.header = pEntryPoints3->header;
1500 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1501 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1502 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1503 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1504 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1505 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1506 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1507 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1508 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1509 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1510 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1511 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1512 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1513 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1514 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1515 mEntryPoints.VRDEGetInterface = NULL;
1516 mpEntryPoints = &mEntryPoints;
1517 }
1518 else if (vrc == VERR_VERSION_MISMATCH)
1519 {
1520 /* An older version of VRDE is installed, try version 1. */
1521 VRDEENTRYPOINTS_1 *pEntryPoints1;
1522
1523 static VRDECALLBACKS_1 sCallbacks1 =
1524 {
1525 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1526 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1527 ConsoleVRDPServer::VRDPCallbackClientLogon,
1528 ConsoleVRDPServer::VRDPCallbackClientConnect,
1529 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1530 ConsoleVRDPServer::VRDPCallbackIntercept,
1531 ConsoleVRDPServer::VRDPCallbackUSB,
1532 ConsoleVRDPServer::VRDPCallbackClipboard,
1533 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1534 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1535 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1536 ConsoleVRDPServer::VRDPCallbackInput,
1537 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1538 };
1539
1540 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1541 if (RT_SUCCESS(vrc))
1542 {
1543 mServerInterfaceVersion = 1;
1544 mEntryPoints.header = pEntryPoints1->header;
1545 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1546 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1547 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1548 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1549 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1550 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1551 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1552 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1553 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1554 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1555 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1556 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1557 mEntryPoints.VRDERedirect = NULL;
1558 mEntryPoints.VRDEAudioInOpen = NULL;
1559 mEntryPoints.VRDEAudioInClose = NULL;
1560 mEntryPoints.VRDEGetInterface = NULL;
1561 mpEntryPoints = &mEntryPoints;
1562 }
1563 }
1564 }
1565
1566 if (RT_SUCCESS(vrc))
1567 {
1568 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1569
1570 if (mServerInterfaceVersion >= 4)
1571 {
1572 /* The server supports optional interfaces. */
1573 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1574
1575 /* Image interface. */
1576 m_interfaceImage.header.u64Version = 1;
1577 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1578
1579 m_interfaceCallbacksImage.header.u64Version = 1;
1580 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1581 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1582
1583 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1584 VRDE_IMAGE_INTERFACE_NAME,
1585 &m_interfaceImage.header,
1586 &m_interfaceCallbacksImage.header,
1587 this);
1588 if (RT_SUCCESS(vrc))
1589 {
1590 LogRel(("VRDE: [%s]\n", VRDE_IMAGE_INTERFACE_NAME));
1591 m_fInterfaceImage = true;
1592 }
1593
1594 /* Mouse pointer interface. */
1595 m_interfaceMousePtr.header.u64Version = 1;
1596 m_interfaceMousePtr.header.u64Size = sizeof(m_interfaceMousePtr);
1597
1598 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1599 VRDE_MOUSEPTR_INTERFACE_NAME,
1600 &m_interfaceMousePtr.header,
1601 NULL,
1602 this);
1603 if (RT_SUCCESS(vrc))
1604 {
1605 LogRel(("VRDE: [%s]\n", VRDE_MOUSEPTR_INTERFACE_NAME));
1606 }
1607 else
1608 {
1609 RT_ZERO(m_interfaceMousePtr);
1610 }
1611
1612 /* Smartcard interface. */
1613 m_interfaceSCard.header.u64Version = 1;
1614 m_interfaceSCard.header.u64Size = sizeof(m_interfaceSCard);
1615
1616 m_interfaceCallbacksSCard.header.u64Version = 1;
1617 m_interfaceCallbacksSCard.header.u64Size = sizeof(m_interfaceCallbacksSCard);
1618 m_interfaceCallbacksSCard.VRDESCardCbNotify = VRDESCardCbNotify;
1619 m_interfaceCallbacksSCard.VRDESCardCbResponse = VRDESCardCbResponse;
1620
1621 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1622 VRDE_SCARD_INTERFACE_NAME,
1623 &m_interfaceSCard.header,
1624 &m_interfaceCallbacksSCard.header,
1625 this);
1626 if (RT_SUCCESS(vrc))
1627 {
1628 LogRel(("VRDE: [%s]\n", VRDE_SCARD_INTERFACE_NAME));
1629 }
1630 else
1631 {
1632 RT_ZERO(m_interfaceSCard);
1633 }
1634
1635 /* Raw TSMF interface. */
1636 m_interfaceTSMF.header.u64Version = 1;
1637 m_interfaceTSMF.header.u64Size = sizeof(m_interfaceTSMF);
1638
1639 m_interfaceCallbacksTSMF.header.u64Version = 1;
1640 m_interfaceCallbacksTSMF.header.u64Size = sizeof(m_interfaceCallbacksTSMF);
1641 m_interfaceCallbacksTSMF.VRDETSMFCbNotify = VRDETSMFCbNotify;
1642
1643 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1644 VRDE_TSMF_INTERFACE_NAME,
1645 &m_interfaceTSMF.header,
1646 &m_interfaceCallbacksTSMF.header,
1647 this);
1648 if (RT_SUCCESS(vrc))
1649 {
1650 LogRel(("VRDE: [%s]\n", VRDE_TSMF_INTERFACE_NAME));
1651 }
1652 else
1653 {
1654 RT_ZERO(m_interfaceTSMF);
1655 }
1656
1657 /* Since these interfaces are optional, it is always a success here. */
1658 vrc = VINF_SUCCESS;
1659 }
1660#ifdef VBOX_WITH_USB
1661 remoteUSBThreadStart();
1662#endif
1663 }
1664 else
1665 {
1666 if (vrc != VERR_NET_ADDRESS_IN_USE)
1667 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1668 /* Don't unload the lib, because it prevents us trying again or
1669 because there may be other users? */
1670 }
1671 }
1672 }
1673
1674 return vrc;
1675}
1676
1677typedef struct H3DORInstance
1678{
1679 ConsoleVRDPServer *pThis;
1680 HVRDEIMAGE hImageBitmap;
1681 int32_t x;
1682 int32_t y;
1683 uint32_t w;
1684 uint32_t h;
1685 bool fCreated;
1686} H3DORInstance;
1687
1688/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1689 const char *pszFormat)
1690{
1691 LogFlowFunc(("ctx %p\n", pvContext));
1692
1693 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof (H3DORInstance));
1694
1695 if (p)
1696 {
1697 p->pThis = (ConsoleVRDPServer *)pvContext;
1698 p->hImageBitmap = NULL;
1699 p->x = 0;
1700 p->y = 0;
1701 p->w = 0;
1702 p->h = 0;
1703 p->fCreated = false;
1704
1705 /* Host 3D service passes the actual format of data in this redirect instance.
1706 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1707 */
1708 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1709 {
1710 /* Accept it. */
1711 }
1712 else
1713 {
1714 RTMemFree(p);
1715 p = NULL;
1716 }
1717 }
1718
1719 /* Caller check this for NULL. */
1720 *ppvInstance = p;
1721}
1722
1723/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1724 int32_t x, int32_t y, uint32_t w, uint32_t h)
1725{
1726 LogFlowFunc(("ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1727
1728 H3DORInstance *p = (H3DORInstance *)pvInstance;
1729 Assert(p);
1730 Assert(p->pThis);
1731
1732 /* @todo find out what to do if size changes to 0x0 from non zero */
1733 if (w == 0 || h == 0)
1734 {
1735 /* Do nothing. */
1736 return;
1737 }
1738
1739 RTRECT rect;
1740 rect.xLeft = x;
1741 rect.yTop = y;
1742 rect.xRight = x + w;
1743 rect.yBottom = y + h;
1744
1745 if (p->hImageBitmap)
1746 {
1747 /* An image handle has been already created,
1748 * check if it has the same size as the reported geometry.
1749 */
1750 if ( p->x == x
1751 && p->y == y
1752 && p->w == w
1753 && p->h == h)
1754 {
1755 LogFlowFunc(("geometry not changed\n"));
1756 /* Do nothing. Continue using the existing handle. */
1757 }
1758 else
1759 {
1760 int rc = p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1761 if (RT_SUCCESS(rc))
1762 {
1763 p->x = x;
1764 p->y = y;
1765 p->w = w;
1766 p->h = h;
1767 }
1768 else
1769 {
1770 /* The handle must be recreated. Delete existing handle here. */
1771 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1772 p->hImageBitmap = NULL;
1773 }
1774 }
1775 }
1776
1777 if (!p->hImageBitmap)
1778 {
1779 /* Create a new bitmap handle. */
1780 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1781 * Clipping can be done here or in VRDP server.
1782 * If VRDP does clipping, then uScreenId parameter
1783 * is not necessary and coords must be global.
1784 * (have to check which coords are used in opengl service).
1785 * Since all VRDE API uses a ScreenId,
1786 * the clipping must be done here in ConsoleVRDPServer
1787 */
1788 uint32_t fu32CompletionFlags = 0;
1789 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1790 &p->hImageBitmap,
1791 p,
1792 u32ScreenId,
1793 VRDE_IMAGE_F_CREATE_CONTENT_3D
1794 | VRDE_IMAGE_F_CREATE_WINDOW,
1795 &rect,
1796 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1797 NULL,
1798 0,
1799 &fu32CompletionFlags);
1800 if (RT_SUCCESS(rc))
1801 {
1802 p->x = x;
1803 p->y = y;
1804 p->w = w;
1805 p->h = h;
1806
1807 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1808 {
1809 p->fCreated = true;
1810 }
1811 }
1812 else
1813 {
1814 p->hImageBitmap = NULL;
1815 p->w = 0;
1816 p->h = 0;
1817 }
1818 }
1819}
1820
1821/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1822 uint32_t cRects, RTRECT *paRects)
1823{
1824 LogFlowFunc(("ins %p %d\n", pvInstance, cRects));
1825
1826 H3DORInstance *p = (H3DORInstance *)pvInstance;
1827 Assert(p);
1828 Assert(p->pThis);
1829
1830 if (cRects == 0)
1831 {
1832 /* Complete image is visible. */
1833 RTRECT rect;
1834 rect.xLeft = p->x;
1835 rect.yTop = p->y;
1836 rect.xRight = p->x + p->w;
1837 rect.yBottom = p->y + p->h;
1838 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1839 1,
1840 &rect);
1841 }
1842 else
1843 {
1844 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1845 cRects,
1846 paRects);
1847 }
1848}
1849
1850/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1851 void *pvData, uint32_t cbData)
1852{
1853 LogFlowFunc(("ins %p %p %d\n", pvInstance, pvData, cbData));
1854
1855 H3DORInstance *p = (H3DORInstance *)pvInstance;
1856 Assert(p);
1857 Assert(p->pThis);
1858
1859 /* Currently only a topdown BGR0 bitmap format is supported. */
1860 VRDEIMAGEBITMAP image;
1861
1862 image.cWidth = p->w;
1863 image.cHeight = p->h;
1864 image.pvData = pvData;
1865 image.cbData = cbData;
1866 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
1867 image.iScanDelta = -4 * p->w;
1868
1869 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
1870 p->x,
1871 p->y,
1872 p->w,
1873 p->h,
1874 &image,
1875 sizeof(VRDEIMAGEBITMAP));
1876}
1877
1878/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
1879{
1880 LogFlowFunc(("ins %p\n", pvInstance));
1881
1882 H3DORInstance *p = (H3DORInstance *)pvInstance;
1883 Assert(p);
1884 Assert(p->pThis);
1885
1886 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1887
1888 RTMemFree(p);
1889}
1890
1891/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
1892 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
1893{
1894 int rc = VINF_SUCCESS;
1895
1896 if (index == H3DOR_PROP_FORMATS)
1897 {
1898 /* Return a comma separated list of supported formats. */
1899 static const char *pszSupportedFormats = H3DOR_FMT_RGBA_TOPDOWN;
1900 uint32_t cbOut = (uint32_t)strlen(pszSupportedFormats) + 1;
1901 if (cbOut <= cbBuffer)
1902 {
1903 memcpy(pvBuffer, pszSupportedFormats, cbOut);
1904 }
1905 else
1906 {
1907 rc = VERR_BUFFER_OVERFLOW;
1908 }
1909 *pcbOut = cbOut;
1910 }
1911 else
1912 {
1913 rc = VERR_NOT_SUPPORTED;
1914 }
1915
1916 return rc;
1917}
1918
1919void ConsoleVRDPServer::remote3DRedirect(void)
1920{
1921 if (!m_fInterfaceImage)
1922 {
1923 /* No redirect without corresponding interface. */
1924 return;
1925 }
1926
1927 /* Check if 3D redirection has been enabled. */
1928 com::Bstr bstr;
1929 HRESULT hrc = mConsole->getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
1930
1931 if (hrc != S_OK)
1932 {
1933 bstr = "";
1934 }
1935
1936 com::Utf8Str value = bstr;
1937
1938 bool fEnabled = RTStrICmp(value.c_str(), "true") == 0
1939 || RTStrICmp(value.c_str(), "1") == 0;
1940
1941 if (!fEnabled)
1942 {
1943 return;
1944 }
1945
1946 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
1947 H3DOUTPUTREDIRECT outputRedirect =
1948 {
1949 this,
1950 H3DORBegin,
1951 H3DORGeometry,
1952 H3DORVisibleRegion,
1953 H3DORFrame,
1954 H3DOREnd,
1955 H3DORContextProperty
1956 };
1957
1958 VBOXHGCMSVCPARM parm;
1959
1960 parm.type = VBOX_HGCM_SVC_PARM_PTR;
1961 parm.u.pointer.addr = &outputRedirect;
1962 parm.u.pointer.size = sizeof(outputRedirect);
1963
1964 VMMDev *pVMMDev = mConsole->getVMMDev();
1965
1966 if (!pVMMDev)
1967 {
1968 AssertMsgFailed(("remote3DRedirect no vmmdev\n"));
1969 return;
1970 }
1971
1972 int rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL",
1973 SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT,
1974 SHCRGL_CPARMS_SET_OUTPUT_REDIRECT,
1975 &parm);
1976
1977 if (!RT_SUCCESS(rc))
1978 {
1979 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
1980 return;
1981 }
1982
1983 LogRel(("VRDE: Enabled 3D redirect.\n"));
1984
1985 return;
1986}
1987
1988/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
1989 void *pvUser,
1990 HVRDEIMAGE hVideo,
1991 uint32_t u32Id,
1992 void *pvData,
1993 uint32_t cbData)
1994{
1995 LogFlowFunc(("pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
1996 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
1997
1998 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
1999 H3DORInstance *p = (H3DORInstance *)pvUser;
2000 Assert(p);
2001 Assert(p->pThis);
2002 Assert(p->pThis == pServer);
2003
2004 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
2005 {
2006 if (cbData != sizeof(uint32_t))
2007 {
2008 AssertFailed();
2009 return VERR_INVALID_PARAMETER;
2010 }
2011
2012 uint32_t u32StreamId = *(uint32_t *)pvData;
2013 LogFlowFunc(("VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
2014 u32StreamId));
2015
2016 if (u32StreamId != 0)
2017 {
2018 p->fCreated = true; // @todo not needed?
2019 }
2020 else
2021 {
2022 /* The stream has not been created. */
2023 }
2024 }
2025
2026 return VINF_SUCCESS;
2027}
2028
2029/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbNotify(void *pvContext,
2030 uint32_t u32Id,
2031 void *pvData,
2032 uint32_t cbData)
2033{
2034#ifdef VBOX_WITH_USB_CARDREADER
2035 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2036 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2037 return pReader->VRDENotify(u32Id, pvData, cbData);
2038#else
2039 NOREF(pvContext);
2040 NOREF(u32Id);
2041 NOREF(pvData);
2042 NOREF(cbData);
2043 return VERR_NOT_SUPPORTED;
2044#endif
2045}
2046
2047/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbResponse(void *pvContext,
2048 int rcRequest,
2049 void *pvUser,
2050 uint32_t u32Function,
2051 void *pvData,
2052 uint32_t cbData)
2053{
2054#ifdef VBOX_WITH_USB_CARDREADER
2055 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2056 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2057 return pReader->VRDEResponse(rcRequest, pvUser, u32Function, pvData, cbData);
2058#else
2059 NOREF(pvContext);
2060 NOREF(rcRequest);
2061 NOREF(pvUser);
2062 NOREF(u32Function);
2063 NOREF(pvData);
2064 NOREF(cbData);
2065 return VERR_NOT_SUPPORTED;
2066#endif
2067}
2068
2069int ConsoleVRDPServer::SCardRequest(void *pvUser, uint32_t u32Function, const void *pvData, uint32_t cbData)
2070{
2071 int rc = VINF_SUCCESS;
2072
2073 if (mhServer && mpEntryPoints && m_interfaceSCard.VRDESCardRequest)
2074 {
2075 rc = m_interfaceSCard.VRDESCardRequest(mhServer, pvUser, u32Function, pvData, cbData);
2076 }
2077 else
2078 {
2079 rc = VERR_NOT_SUPPORTED;
2080 }
2081
2082 return rc;
2083}
2084
2085typedef struct TSMFHOSTCHANNELCTX
2086{
2087 ConsoleVRDPServer *pThis;
2088
2089 VBOXHOSTCHANNELCALLBACKS *pCallbacks;
2090 void *pvCallbacks;
2091
2092 uint32_t u32ChannelHandle;
2093
2094 void *pvDataReceived;
2095 uint32_t cbDataReceived;
2096 uint32_t cbDataAllocated;
2097} TSMFHOSTCHANNELCTX;
2098
2099/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelAttach(void *pvProvider,
2100 void **ppvChannel,
2101 uint32_t u32Flags,
2102 VBOXHOSTCHANNELCALLBACKS *pCallbacks,
2103 void *pvCallbacks)
2104{
2105 LogFlowFunc(("\n"));
2106
2107 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvProvider);
2108
2109 TSMFHOSTCHANNELCTX *pCtx = (TSMFHOSTCHANNELCTX *)RTMemAllocZ(sizeof(TSMFHOSTCHANNELCTX));
2110 if (!pCtx)
2111 {
2112 return VERR_NO_MEMORY;
2113 }
2114
2115 pCtx->pThis = pThis;
2116 pCtx->pCallbacks = pCallbacks;
2117 pCtx->pvCallbacks = pvCallbacks;
2118
2119 int rc = pThis->m_interfaceTSMF.VRDETSMFChannelCreate(pThis->mhServer, pCtx, u32Flags);
2120
2121 if (RT_SUCCESS(rc))
2122 {
2123 /* @todo contexts should be in a list for accounting. */
2124 *ppvChannel = pCtx;
2125 }
2126 else
2127 {
2128 RTMemFree(pCtx);
2129 }
2130
2131 return rc;
2132}
2133
2134/* static */ DECLCALLBACK(void) ConsoleVRDPServer::tsmfHostChannelDetach(void *pvChannel)
2135{
2136 LogFlowFunc(("\n"));
2137
2138 TSMFHOSTCHANNELCTX *pCtx = (TSMFHOSTCHANNELCTX *)pvChannel;
2139
2140 pCtx->pThis->m_interfaceTSMF.VRDETSMFChannelClose(pCtx->pThis->mhServer, pCtx->u32ChannelHandle);
2141 /* @todo */
2142}
2143
2144/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelSend(void *pvChannel,
2145 const void *pvData,
2146 uint32_t cbData)
2147{
2148 LogFlowFunc(("\n"));
2149 TSMFHOSTCHANNELCTX *pCtx = (TSMFHOSTCHANNELCTX *)pvChannel;
2150
2151 int rc = pCtx->pThis->m_interfaceTSMF.VRDETSMFChannelSend(pCtx->pThis->mhServer, pCtx->u32ChannelHandle,
2152 pvData, cbData);
2153
2154 return rc;
2155}
2156
2157/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelRecv(void *pvChannel,
2158 void *pvData,
2159 uint32_t cbData,
2160 uint32_t *pcbReceived,
2161 uint32_t *pcbRemaining)
2162{
2163 LogFlowFunc(("\n"));
2164
2165 TSMFHOSTCHANNELCTX *pCtx = (TSMFHOSTCHANNELCTX *)pvChannel;
2166 int rc = VINF_SUCCESS;
2167
2168 uint32_t cbToCopy = RT_MIN(cbData, pCtx->cbDataReceived);
2169 uint32_t cbRemaining = pCtx->cbDataReceived - cbToCopy;
2170
2171 LogFlowFunc(("cbToCopy %d, cbRemaining %d\n", cbToCopy, cbRemaining));
2172
2173 if (cbToCopy != 0)
2174 {
2175 memcpy(pvData, pCtx->pvDataReceived, cbToCopy);
2176
2177 if (cbRemaining != 0)
2178 {
2179 memmove(pCtx->pvDataReceived, (uint8_t *)pCtx->pvDataReceived + cbToCopy, cbRemaining);
2180 }
2181
2182 pCtx->cbDataReceived = cbRemaining;
2183 }
2184
2185 *pcbRemaining = cbRemaining;
2186 *pcbReceived = cbToCopy;
2187
2188 return VINF_SUCCESS;
2189}
2190
2191/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelControl(void *pvChannel,
2192 uint32_t u32Code,
2193 const void *pvParm,
2194 uint32_t cbParm,
2195 const void *pvData,
2196 uint32_t cbData,
2197 uint32_t *pcbDataReturned)
2198{
2199 LogFlowFunc(("\n"));
2200 return VERR_NOT_IMPLEMENTED;
2201}
2202
2203
2204void ConsoleVRDPServer::setupTSMF(void)
2205{
2206 if (m_interfaceTSMF.header.u64Size == 0)
2207 {
2208 return;
2209 }
2210
2211 /* Register with the host channel service. */
2212 VBOXHOSTCHANNELINTERFACE hostChannelInterface =
2213 {
2214 this,
2215 tsmfHostChannelAttach,
2216 tsmfHostChannelDetach,
2217 tsmfHostChannelSend,
2218 tsmfHostChannelRecv,
2219 tsmfHostChannelControl
2220 };
2221
2222 VBoxHostChannelHostRegister parms;
2223
2224 static char szProviderName[] = "/vrde/tsmf";
2225
2226 parms.name.type = VBOX_HGCM_SVC_PARM_PTR;
2227 parms.name.u.pointer.addr = &szProviderName[0];
2228 parms.name.u.pointer.size = sizeof(szProviderName);
2229
2230 parms.iface.type = VBOX_HGCM_SVC_PARM_PTR;
2231 parms.iface.u.pointer.addr = &hostChannelInterface;
2232 parms.iface.u.pointer.size = sizeof(hostChannelInterface);
2233
2234 VMMDev *pVMMDev = mConsole->getVMMDev();
2235
2236 if (!pVMMDev)
2237 {
2238 AssertMsgFailed(("setupTSMF no vmmdev\n"));
2239 return;
2240 }
2241
2242 int rc = pVMMDev->hgcmHostCall("VBoxHostChannel",
2243 VBOX_HOST_CHANNEL_HOST_FN_REGISTER,
2244 2,
2245 &parms.name);
2246
2247 if (!RT_SUCCESS(rc))
2248 {
2249 Log(("VBOX_HOST_CHANNEL_HOST_FN_REGISTER failed with %Rrc\n", rc));
2250 return;
2251 }
2252
2253 LogRel(("VRDE: Enabled TSMF channel.\n"));
2254
2255 return;
2256}
2257
2258/* @todo these defines must be in a header, which is used by guest component as well. */
2259#define VBOX_TSMF_HCH_CREATE_ACCEPTED (VBOX_HOST_CHANNEL_EVENT_USER + 0)
2260#define VBOX_TSMF_HCH_CREATE_DECLINED (VBOX_HOST_CHANNEL_EVENT_USER + 1)
2261#define VBOX_TSMF_HCH_DISCONNECTED (VBOX_HOST_CHANNEL_EVENT_USER + 2)
2262
2263/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDETSMFCbNotify(void *pvContext,
2264 uint32_t u32Notification,
2265 void *pvChannel,
2266 const void *pvParm,
2267 uint32_t cbParm)
2268{
2269 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2270
2271 TSMFHOSTCHANNELCTX *pCtx = (TSMFHOSTCHANNELCTX *)pvChannel;
2272
2273 Assert(pCtx->pThis == pThis);
2274
2275 switch(u32Notification)
2276 {
2277 case VRDE_TSMF_N_CREATE_ACCEPTED:
2278 {
2279 VRDETSMFNOTIFYCREATEACCEPTED *p = (VRDETSMFNOTIFYCREATEACCEPTED *)pvParm;
2280 Assert(cbParm == sizeof(VRDETSMFNOTIFYCREATEACCEPTED));
2281
2282 LogFlowFunc(("VRDE_TSMF_N_CREATE_ACCEPTED: p->u32ChannelHandle %d\n", p->u32ChannelHandle));
2283
2284 pCtx->u32ChannelHandle = p->u32ChannelHandle;
2285
2286 pCtx->pCallbacks->HostChannelCallbackEvent(pCtx->pvCallbacks, pCtx,
2287 VBOX_TSMF_HCH_CREATE_ACCEPTED,
2288 NULL, 0);
2289 } break;
2290
2291 case VRDE_TSMF_N_CREATE_DECLINED:
2292 {
2293 pCtx->pCallbacks->HostChannelCallbackEvent(pCtx->pvCallbacks, pCtx,
2294 VBOX_TSMF_HCH_CREATE_DECLINED,
2295 NULL, 0);
2296 } break;
2297
2298 case VRDE_TSMF_N_DATA:
2299 {
2300 /* Save the data in the intermediate buffer and send the event. */
2301 VRDETSMFNOTIFYDATA *p = (VRDETSMFNOTIFYDATA *)pvParm;
2302 Assert(cbParm == sizeof(VRDETSMFNOTIFYDATA));
2303
2304 LogFlowFunc(("VRDE_TSMF_N_DATA: p->cbData %d\n", p->cbData));
2305
2306 if (pCtx->pvDataReceived)
2307 {
2308 uint32_t cbAlloc = p->cbData + pCtx->cbDataReceived;
2309 pCtx->pvDataReceived = RTMemRealloc(pCtx->pvDataReceived, cbAlloc);
2310 memcpy((uint8_t *)pCtx->pvDataReceived + pCtx->cbDataReceived, p->pvData, p->cbData);
2311
2312 pCtx->cbDataReceived += p->cbData;
2313 pCtx->cbDataAllocated = cbAlloc;
2314 }
2315 else
2316 {
2317 pCtx->pvDataReceived = RTMemAlloc(p->cbData);
2318 memcpy(pCtx->pvDataReceived, p->pvData, p->cbData);
2319
2320 pCtx->cbDataReceived = p->cbData;
2321 pCtx->cbDataAllocated = p->cbData;
2322 }
2323
2324 VBOXHOSTCHANNELEVENTRECV ev;
2325 ev.u32SizeAvailable = p->cbData;
2326
2327 pCtx->pCallbacks->HostChannelCallbackEvent(pCtx->pvCallbacks, pCtx,
2328 VBOX_HOST_CHANNEL_EVENT_RECV,
2329 &ev, sizeof(ev));
2330 } break;
2331
2332 case VRDE_TSMF_N_DISCONNECTED:
2333 {
2334 pCtx->pCallbacks->HostChannelCallbackEvent(pCtx->pvCallbacks, pCtx,
2335 VBOX_TSMF_HCH_DISCONNECTED,
2336 NULL, 0);
2337 } break;
2338
2339 default:
2340 {
2341 AssertFailed();
2342 } break;
2343 }
2344}
2345
2346void ConsoleVRDPServer::EnableConnections(void)
2347{
2348 if (mpEntryPoints && mhServer)
2349 {
2350 mpEntryPoints->VRDEEnableConnections(mhServer, true);
2351
2352 /* Redirect 3D output if it is enabled. */
2353 remote3DRedirect();
2354
2355 /* Setup the generic TSMF channel. */
2356 setupTSMF();
2357 }
2358}
2359
2360void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
2361{
2362 if (mpEntryPoints && mhServer)
2363 {
2364 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
2365 }
2366}
2367
2368int ConsoleVRDPServer::MousePointer(BOOL alpha,
2369 ULONG xHot,
2370 ULONG yHot,
2371 ULONG width,
2372 ULONG height,
2373 const uint8_t *pu8Shape)
2374{
2375 int rc = VINF_SUCCESS;
2376
2377 if (mhServer && mpEntryPoints && m_interfaceMousePtr.VRDEMousePtr)
2378 {
2379 size_t cbMask = (((width + 7) / 8) * height + 3) & ~3;
2380 size_t cbData = width * height * 4;
2381
2382 size_t cbDstMask = alpha? 0: cbMask;
2383
2384 size_t cbPointer = sizeof(VRDEMOUSEPTRDATA) + cbDstMask + cbData;
2385 uint8_t *pu8Pointer = (uint8_t *)RTMemAlloc(cbPointer);
2386 if (pu8Pointer != NULL)
2387 {
2388 VRDEMOUSEPTRDATA *pPointer = (VRDEMOUSEPTRDATA *)pu8Pointer;
2389
2390 pPointer->u16HotX = (uint16_t)xHot;
2391 pPointer->u16HotY = (uint16_t)yHot;
2392 pPointer->u16Width = (uint16_t)width;
2393 pPointer->u16Height = (uint16_t)height;
2394 pPointer->u16MaskLen = (uint16_t)cbDstMask;
2395 pPointer->u32DataLen = (uint32_t)cbData;
2396
2397 /* AND mask. */
2398 uint8_t *pu8Mask = pu8Pointer + sizeof(VRDEMOUSEPTRDATA);
2399 if (cbDstMask)
2400 {
2401 memcpy(pu8Mask, pu8Shape, cbDstMask);
2402 }
2403
2404 /* XOR mask */
2405 uint8_t *pu8Data = pu8Mask + pPointer->u16MaskLen;
2406 memcpy(pu8Data, pu8Shape + cbMask, cbData);
2407
2408 m_interfaceMousePtr.VRDEMousePtr(mhServer, pPointer);
2409
2410 RTMemFree(pu8Pointer);
2411 }
2412 else
2413 {
2414 rc = VERR_NO_MEMORY;
2415 }
2416 }
2417 else
2418 {
2419 rc = VERR_NOT_SUPPORTED;
2420 }
2421
2422 return rc;
2423}
2424
2425void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
2426{
2427 if (mpEntryPoints && mhServer)
2428 {
2429 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
2430 }
2431}
2432
2433void ConsoleVRDPServer::MousePointerHide(void)
2434{
2435 if (mpEntryPoints && mhServer)
2436 {
2437 mpEntryPoints->VRDEHidePointer(mhServer);
2438 }
2439}
2440
2441void ConsoleVRDPServer::Stop(void)
2442{
2443 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
2444 * linux. Just remove this when it's 100% sure that problem has been fixed. */
2445 if (mhServer)
2446 {
2447 HVRDESERVER hServer = mhServer;
2448
2449 /* Reset the handle to avoid further calls to the server. */
2450 mhServer = 0;
2451
2452 if (mpEntryPoints && hServer)
2453 {
2454 mpEntryPoints->VRDEDestroy(hServer);
2455 }
2456 }
2457
2458#ifdef VBOX_WITH_USB
2459 remoteUSBThreadStop();
2460#endif /* VBOX_WITH_USB */
2461
2462 mpfnAuthEntry = NULL;
2463 mpfnAuthEntry2 = NULL;
2464 mpfnAuthEntry3 = NULL;
2465
2466 if (mAuthLibrary)
2467 {
2468 RTLdrClose(mAuthLibrary);
2469 mAuthLibrary = 0;
2470 }
2471}
2472
2473/* Worker thread for Remote USB. The thread polls the clients for
2474 * the list of attached USB devices.
2475 * The thread is also responsible for attaching/detaching devices
2476 * to/from the VM.
2477 *
2478 * It is expected that attaching/detaching is not a frequent operation.
2479 *
2480 * The thread is always running when the VRDP server is active.
2481 *
2482 * The thread scans backends and requests the device list every 2 seconds.
2483 *
2484 * When device list is available, the thread calls the Console to process it.
2485 *
2486 */
2487#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
2488
2489#ifdef VBOX_WITH_USB
2490static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
2491{
2492 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
2493
2494 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
2495
2496 pOwner->notifyRemoteUSBThreadRunning(self);
2497
2498 while (pOwner->isRemoteUSBThreadRunning())
2499 {
2500 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2501
2502 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
2503 {
2504 pRemoteUSBBackend->PollRemoteDevices();
2505 }
2506
2507 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
2508
2509 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
2510 }
2511
2512 return VINF_SUCCESS;
2513}
2514
2515void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
2516{
2517 mUSBBackends.thread = thread;
2518 mUSBBackends.fThreadRunning = true;
2519 int rc = RTThreadUserSignal(thread);
2520 AssertRC(rc);
2521}
2522
2523bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
2524{
2525 return mUSBBackends.fThreadRunning;
2526}
2527
2528void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
2529{
2530 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
2531 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
2532 NOREF(rc);
2533}
2534
2535void ConsoleVRDPServer::remoteUSBThreadStart(void)
2536{
2537 int rc = RTSemEventCreate(&mUSBBackends.event);
2538
2539 if (RT_FAILURE(rc))
2540 {
2541 AssertFailed();
2542 mUSBBackends.event = 0;
2543 }
2544
2545 if (RT_SUCCESS(rc))
2546 {
2547 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
2548 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
2549 }
2550
2551 if (RT_FAILURE(rc))
2552 {
2553 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
2554 mUSBBackends.thread = NIL_RTTHREAD;
2555 }
2556 else
2557 {
2558 /* Wait until the thread is ready. */
2559 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
2560 AssertRC(rc);
2561 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
2562 }
2563}
2564
2565void ConsoleVRDPServer::remoteUSBThreadStop(void)
2566{
2567 mUSBBackends.fThreadRunning = false;
2568
2569 if (mUSBBackends.thread != NIL_RTTHREAD)
2570 {
2571 Assert (mUSBBackends.event != 0);
2572
2573 RTSemEventSignal(mUSBBackends.event);
2574
2575 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
2576 AssertRC(rc);
2577
2578 mUSBBackends.thread = NIL_RTTHREAD;
2579 }
2580
2581 if (mUSBBackends.event)
2582 {
2583 RTSemEventDestroy(mUSBBackends.event);
2584 mUSBBackends.event = 0;
2585 }
2586}
2587#endif /* VBOX_WITH_USB */
2588
2589AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
2590 const char *pszUser, const char *pszPassword, const char *pszDomain,
2591 uint32_t u32ClientId)
2592{
2593 AUTHUUID rawuuid;
2594
2595 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2596
2597 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
2598 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
2599
2600 /*
2601 * Called only from VRDP input thread. So thread safety is not required.
2602 */
2603
2604 if (!mAuthLibrary)
2605 {
2606 /* Load the external authentication library. */
2607 Bstr authLibrary;
2608 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
2609
2610 Utf8Str filename = authLibrary;
2611
2612 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
2613
2614 int rc;
2615 if (RTPathHavePath(filename.c_str()))
2616 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
2617 else
2618 {
2619 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
2620 if (RT_FAILURE(rc))
2621 {
2622 /* Backward compatibility with old default 'VRDPAuth' name.
2623 * Try to load new default 'VBoxAuth' instead.
2624 */
2625 if (filename == "VRDPAuth")
2626 {
2627 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library VBoxAuth\n"));
2628 rc = RTLdrLoadAppPriv("VBoxAuth", &mAuthLibrary);
2629 }
2630 }
2631 }
2632
2633 if (RT_FAILURE(rc))
2634 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
2635
2636 if (RT_SUCCESS(rc))
2637 {
2638 typedef struct AuthEntryInfoStruct
2639 {
2640 const char *pszName;
2641 void **ppvAddress;
2642
2643 } AuthEntryInfo;
2644 AuthEntryInfo entries[] =
2645 {
2646 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
2647 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
2648 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
2649 { NULL, NULL }
2650 };
2651
2652 /* Get the entry point. */
2653 AuthEntryInfo *pEntryInfo = &entries[0];
2654 while (pEntryInfo->pszName)
2655 {
2656 *pEntryInfo->ppvAddress = NULL;
2657
2658 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
2659 if (RT_SUCCESS(rc2))
2660 {
2661 /* Found an entry point. */
2662 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
2663 rc = VINF_SUCCESS;
2664 break;
2665 }
2666
2667 if (rc2 != VERR_SYMBOL_NOT_FOUND)
2668 {
2669 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
2670 }
2671 rc = rc2;
2672
2673 pEntryInfo++;
2674 }
2675 }
2676
2677 if (RT_FAILURE(rc))
2678 {
2679 mConsole->setError(E_FAIL,
2680 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
2681 filename.c_str(),
2682 rc);
2683
2684 mpfnAuthEntry = NULL;
2685 mpfnAuthEntry2 = NULL;
2686 mpfnAuthEntry3 = NULL;
2687
2688 if (mAuthLibrary)
2689 {
2690 RTLdrClose(mAuthLibrary);
2691 mAuthLibrary = 0;
2692 }
2693
2694 return AuthResultAccessDenied;
2695 }
2696 }
2697
2698 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2699
2700 AuthResult result = AuthResultAccessDenied;
2701 if (mpfnAuthEntry3)
2702 {
2703 result = mpfnAuthEntry3("vrde", &rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2704 }
2705 else if (mpfnAuthEntry2)
2706 {
2707 result = mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
2708 }
2709 else if (mpfnAuthEntry)
2710 {
2711 result = mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
2712 }
2713
2714 switch (result)
2715 {
2716 case AuthResultAccessDenied:
2717 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
2718 break;
2719 case AuthResultAccessGranted:
2720 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
2721 break;
2722 case AuthResultDelegateToGuest:
2723 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
2724 break;
2725 default:
2726 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
2727 result = AuthResultAccessDenied;
2728 }
2729
2730 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
2731
2732 return result;
2733}
2734
2735void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
2736{
2737 AUTHUUID rawuuid;
2738
2739 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
2740
2741 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
2742 rawuuid, u32ClientId));
2743
2744 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
2745
2746 if (mpfnAuthEntry3)
2747 mpfnAuthEntry3("vrde", &rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2748 else if (mpfnAuthEntry2)
2749 mpfnAuthEntry2(&rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
2750}
2751
2752int ConsoleVRDPServer::lockConsoleVRDPServer(void)
2753{
2754 int rc = RTCritSectEnter(&mCritSect);
2755 AssertRC(rc);
2756 return rc;
2757}
2758
2759void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
2760{
2761 RTCritSectLeave(&mCritSect);
2762}
2763
2764DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
2765 uint32_t u32ClientId,
2766 uint32_t u32Function,
2767 uint32_t u32Format,
2768 const void *pvData,
2769 uint32_t cbData)
2770{
2771 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
2772 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
2773
2774 int rc = VINF_SUCCESS;
2775
2776 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
2777
2778 NOREF(u32ClientId);
2779
2780 switch (u32Function)
2781 {
2782 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
2783 {
2784 if (pServer->mpfnClipboardCallback)
2785 {
2786 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
2787 u32Format,
2788 (void *)pvData,
2789 cbData);
2790 }
2791 } break;
2792
2793 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
2794 {
2795 if (pServer->mpfnClipboardCallback)
2796 {
2797 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
2798 u32Format,
2799 (void *)pvData,
2800 cbData);
2801 }
2802 } break;
2803
2804 default:
2805 rc = VERR_NOT_SUPPORTED;
2806 }
2807
2808 return rc;
2809}
2810
2811DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
2812 uint32_t u32Function,
2813 void *pvParms,
2814 uint32_t cbParms)
2815{
2816 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
2817 pvExtension, u32Function, pvParms, cbParms));
2818
2819 int rc = VINF_SUCCESS;
2820
2821 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
2822
2823 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
2824
2825 switch (u32Function)
2826 {
2827 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
2828 {
2829 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
2830 } break;
2831
2832 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
2833 {
2834 /* The guest announces clipboard formats. This must be delivered to all clients. */
2835 if (mpEntryPoints && pServer->mhServer)
2836 {
2837 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2838 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
2839 pParms->u32Format,
2840 NULL,
2841 0,
2842 NULL);
2843 }
2844 } break;
2845
2846 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
2847 {
2848 /* The clipboard service expects that the pvData buffer will be filled
2849 * with clipboard data. The server returns the data from the client that
2850 * announced the requested format most recently.
2851 */
2852 if (mpEntryPoints && pServer->mhServer)
2853 {
2854 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2855 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
2856 pParms->u32Format,
2857 pParms->u.pvData,
2858 pParms->cbData,
2859 &pParms->cbData);
2860 }
2861 } break;
2862
2863 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
2864 {
2865 if (mpEntryPoints && pServer->mhServer)
2866 {
2867 mpEntryPoints->VRDEClipboard(pServer->mhServer,
2868 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
2869 pParms->u32Format,
2870 pParms->u.pvData,
2871 pParms->cbData,
2872 NULL);
2873 }
2874 } break;
2875
2876 default:
2877 rc = VERR_NOT_SUPPORTED;
2878 }
2879
2880 return rc;
2881}
2882
2883void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
2884{
2885 int rc = lockConsoleVRDPServer();
2886
2887 if (RT_SUCCESS(rc))
2888 {
2889 if (mcClipboardRefs == 0)
2890 {
2891 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
2892
2893 if (RT_SUCCESS(rc))
2894 {
2895 mcClipboardRefs++;
2896 }
2897 }
2898
2899 unlockConsoleVRDPServer();
2900 }
2901}
2902
2903void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
2904{
2905 int rc = lockConsoleVRDPServer();
2906
2907 if (RT_SUCCESS(rc))
2908 {
2909 mcClipboardRefs--;
2910
2911 if (mcClipboardRefs == 0)
2912 {
2913 HGCMHostUnregisterServiceExtension(mhClipboard);
2914 }
2915
2916 unlockConsoleVRDPServer();
2917 }
2918}
2919
2920/* That is called on INPUT thread of the VRDP server.
2921 * The ConsoleVRDPServer keeps a list of created backend instances.
2922 */
2923void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
2924{
2925#ifdef VBOX_WITH_USB
2926 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
2927
2928 /* Create a new instance of the USB backend for the new client. */
2929 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
2930
2931 if (pRemoteUSBBackend)
2932 {
2933 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
2934
2935 /* Append the new instance in the list. */
2936 int rc = lockConsoleVRDPServer();
2937
2938 if (RT_SUCCESS(rc))
2939 {
2940 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
2941 if (mUSBBackends.pHead)
2942 {
2943 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
2944 }
2945 else
2946 {
2947 mUSBBackends.pTail = pRemoteUSBBackend;
2948 }
2949
2950 mUSBBackends.pHead = pRemoteUSBBackend;
2951
2952 unlockConsoleVRDPServer();
2953
2954 if (ppvIntercept)
2955 {
2956 *ppvIntercept = pRemoteUSBBackend;
2957 }
2958 }
2959
2960 if (RT_FAILURE(rc))
2961 {
2962 pRemoteUSBBackend->Release();
2963 }
2964 }
2965#endif /* VBOX_WITH_USB */
2966}
2967
2968void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
2969{
2970#ifdef VBOX_WITH_USB
2971 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
2972
2973 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2974
2975 /* Find the instance. */
2976 int rc = lockConsoleVRDPServer();
2977
2978 if (RT_SUCCESS(rc))
2979 {
2980 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2981
2982 if (pRemoteUSBBackend)
2983 {
2984 /* Notify that it will be deleted. */
2985 pRemoteUSBBackend->NotifyDelete();
2986 }
2987
2988 unlockConsoleVRDPServer();
2989 }
2990
2991 if (pRemoteUSBBackend)
2992 {
2993 /* Here the instance has been excluded from the list and can be dereferenced. */
2994 pRemoteUSBBackend->Release();
2995 }
2996#endif
2997}
2998
2999void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
3000{
3001#ifdef VBOX_WITH_USB
3002 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3003
3004 /* Find the instance. */
3005 int rc = lockConsoleVRDPServer();
3006
3007 if (RT_SUCCESS(rc))
3008 {
3009 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3010
3011 if (pRemoteUSBBackend)
3012 {
3013 /* Inform the backend instance that it is referenced by the Guid. */
3014 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
3015
3016 if (fAdded)
3017 {
3018 /* Reference the instance because its pointer is being taken. */
3019 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
3020 }
3021 else
3022 {
3023 pRemoteUSBBackend = NULL;
3024 }
3025 }
3026
3027 unlockConsoleVRDPServer();
3028 }
3029
3030 if (pRemoteUSBBackend)
3031 {
3032 return pRemoteUSBBackend->GetBackendCallbackPointer();
3033 }
3034
3035#endif
3036 return NULL;
3037}
3038
3039void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
3040{
3041#ifdef VBOX_WITH_USB
3042 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3043
3044 /* Find the instance. */
3045 int rc = lockConsoleVRDPServer();
3046
3047 if (RT_SUCCESS(rc))
3048 {
3049 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
3050
3051 if (pRemoteUSBBackend)
3052 {
3053 pRemoteUSBBackend->removeUUID(pGuid);
3054 }
3055
3056 unlockConsoleVRDPServer();
3057
3058 if (pRemoteUSBBackend)
3059 {
3060 pRemoteUSBBackend->Release();
3061 }
3062 }
3063#endif
3064}
3065
3066RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
3067{
3068 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
3069
3070 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
3071#ifdef VBOX_WITH_USB
3072
3073 int rc = lockConsoleVRDPServer();
3074
3075 if (RT_SUCCESS(rc))
3076 {
3077 if (pRemoteUSBBackend == NULL)
3078 {
3079 /* The first backend in the list is requested. */
3080 pNextRemoteUSBBackend = mUSBBackends.pHead;
3081 }
3082 else
3083 {
3084 /* Get pointer to the next backend. */
3085 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3086 }
3087
3088 if (pNextRemoteUSBBackend)
3089 {
3090 pNextRemoteUSBBackend->AddRef();
3091 }
3092
3093 unlockConsoleVRDPServer();
3094
3095 if (pRemoteUSBBackend)
3096 {
3097 pRemoteUSBBackend->Release();
3098 }
3099 }
3100#endif
3101
3102 return pNextRemoteUSBBackend;
3103}
3104
3105#ifdef VBOX_WITH_USB
3106/* Internal method. Called under the ConsoleVRDPServerLock. */
3107RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
3108{
3109 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3110
3111 while (pRemoteUSBBackend)
3112 {
3113 if (pRemoteUSBBackend->ClientId() == u32ClientId)
3114 {
3115 break;
3116 }
3117
3118 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3119 }
3120
3121 return pRemoteUSBBackend;
3122}
3123
3124/* Internal method. Called under the ConsoleVRDPServerLock. */
3125RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
3126{
3127 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3128
3129 while (pRemoteUSBBackend)
3130 {
3131 if (pRemoteUSBBackend->findUUID(pGuid))
3132 {
3133 break;
3134 }
3135
3136 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3137 }
3138
3139 return pRemoteUSBBackend;
3140}
3141#endif
3142
3143/* Internal method. Called by the backend destructor. */
3144void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
3145{
3146#ifdef VBOX_WITH_USB
3147 int rc = lockConsoleVRDPServer();
3148 AssertRC(rc);
3149
3150 /* Exclude the found instance from the list. */
3151 if (pRemoteUSBBackend->pNext)
3152 {
3153 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
3154 }
3155 else
3156 {
3157 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
3158 }
3159
3160 if (pRemoteUSBBackend->pPrev)
3161 {
3162 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
3163 }
3164 else
3165 {
3166 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3167 }
3168
3169 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
3170
3171 unlockConsoleVRDPServer();
3172#endif
3173}
3174
3175
3176void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
3177{
3178 if (mpEntryPoints && mhServer)
3179 {
3180 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
3181 }
3182}
3183
3184void ConsoleVRDPServer::SendResize(void) const
3185{
3186 if (mpEntryPoints && mhServer)
3187 {
3188 mpEntryPoints->VRDEResize(mhServer);
3189 }
3190}
3191
3192void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
3193{
3194 VRDEORDERHDR update;
3195 update.x = x;
3196 update.y = y;
3197 update.w = w;
3198 update.h = h;
3199 if (mpEntryPoints && mhServer)
3200 {
3201 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
3202 }
3203}
3204
3205void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
3206{
3207 if (mpEntryPoints && mhServer)
3208 {
3209 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
3210 }
3211}
3212
3213void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
3214{
3215 if (mpEntryPoints && mhServer)
3216 {
3217 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
3218 }
3219}
3220
3221void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
3222{
3223 if (mpEntryPoints && mhServer)
3224 {
3225 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
3226 }
3227}
3228
3229/* @todo rc not needed? */
3230int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
3231 void *pvContext,
3232 uint32_t cSamples,
3233 uint32_t iSampleHz,
3234 uint32_t cChannels,
3235 uint32_t cBits)
3236{
3237 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
3238 {
3239 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3240 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3241 {
3242 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
3243 mpEntryPoints->VRDEAudioInOpen (mhServer,
3244 pvContext,
3245 u32ClientId,
3246 audioFormat,
3247 cSamples);
3248 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
3249 * Currently not used because only one client is allowed to
3250 * do audio input and the client id is saved by the ConsoleVRDPServer.
3251 */
3252
3253 return VINF_SUCCESS;
3254 }
3255 }
3256 return VERR_NOT_SUPPORTED;
3257}
3258
3259void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
3260{
3261 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
3262 {
3263 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3264 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3265 {
3266 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
3267 }
3268 }
3269}
3270
3271#ifdef VBOX_WITH_USB_VIDEO
3272int ConsoleVRDPServer::GetVideoFrameDimensions(uint16_t *pu16Heigh, uint16_t *pu16Width)
3273{
3274 *pu16Heigh = 640;
3275 *pu16Width = 480;
3276 return VINF_SUCCESS;
3277}
3278
3279int ConsoleVRDPServer::SendVideoSreamOn(bool fFetch)
3280{
3281 /* Here we inform server that guest is starting/stopping
3282 * the stream
3283 */
3284 return VINF_SUCCESS;
3285}
3286#endif
3287
3288
3289
3290void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
3291{
3292 if (index == VRDE_QI_PORT)
3293 {
3294 uint32_t cbOut = sizeof(int32_t);
3295
3296 if (cbBuffer >= cbOut)
3297 {
3298 *pcbOut = cbOut;
3299 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
3300 }
3301 }
3302 else if (mpEntryPoints && mhServer)
3303 {
3304 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
3305 }
3306}
3307
3308/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
3309{
3310 int rc = VINF_SUCCESS;
3311
3312 if (mVRDPLibrary == NIL_RTLDRMOD)
3313 {
3314 RTERRINFOSTATIC ErrInfo;
3315 RTErrInfoInitStatic(&ErrInfo);
3316
3317 if (RTPathHavePath(pszLibraryName))
3318 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
3319 else
3320 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
3321 if (RT_SUCCESS(rc))
3322 {
3323 struct SymbolEntry
3324 {
3325 const char *name;
3326 void **ppfn;
3327 };
3328
3329 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
3330
3331 static const struct SymbolEntry s_aSymbols[] =
3332 {
3333 DEFSYMENTRY(VRDECreateServer)
3334 };
3335
3336 #undef DEFSYMENTRY
3337
3338 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
3339 {
3340 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
3341
3342 if (RT_FAILURE(rc))
3343 {
3344 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
3345 break;
3346 }
3347 }
3348 }
3349 else
3350 {
3351 if (RTErrInfoIsSet(&ErrInfo.Core))
3352 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
3353 else
3354 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
3355
3356 mVRDPLibrary = NIL_RTLDRMOD;
3357 }
3358 }
3359
3360 if (RT_FAILURE(rc))
3361 {
3362 if (mVRDPLibrary != NIL_RTLDRMOD)
3363 {
3364 RTLdrClose(mVRDPLibrary);
3365 mVRDPLibrary = NIL_RTLDRMOD;
3366 }
3367 }
3368
3369 return rc;
3370}
3371
3372/*
3373 * IVRDEServerInfo implementation.
3374 */
3375// constructor / destructor
3376/////////////////////////////////////////////////////////////////////////////
3377
3378VRDEServerInfo::VRDEServerInfo()
3379 : mParent(NULL)
3380{
3381}
3382
3383VRDEServerInfo::~VRDEServerInfo()
3384{
3385}
3386
3387
3388HRESULT VRDEServerInfo::FinalConstruct()
3389{
3390 return BaseFinalConstruct();
3391}
3392
3393void VRDEServerInfo::FinalRelease()
3394{
3395 uninit();
3396 BaseFinalRelease();
3397}
3398
3399// public methods only for internal purposes
3400/////////////////////////////////////////////////////////////////////////////
3401
3402/**
3403 * Initializes the guest object.
3404 */
3405HRESULT VRDEServerInfo::init(Console *aParent)
3406{
3407 LogFlowThisFunc(("aParent=%p\n", aParent));
3408
3409 ComAssertRet(aParent, E_INVALIDARG);
3410
3411 /* Enclose the state transition NotReady->InInit->Ready */
3412 AutoInitSpan autoInitSpan(this);
3413 AssertReturn(autoInitSpan.isOk(), E_FAIL);
3414
3415 unconst(mParent) = aParent;
3416
3417 /* Confirm a successful initialization */
3418 autoInitSpan.setSucceeded();
3419
3420 return S_OK;
3421}
3422
3423/**
3424 * Uninitializes the instance and sets the ready flag to FALSE.
3425 * Called either from FinalRelease() or by the parent when it gets destroyed.
3426 */
3427void VRDEServerInfo::uninit()
3428{
3429 LogFlowThisFunc(("\n"));
3430
3431 /* Enclose the state transition Ready->InUninit->NotReady */
3432 AutoUninitSpan autoUninitSpan(this);
3433 if (autoUninitSpan.uninitDone())
3434 return;
3435
3436 unconst(mParent) = NULL;
3437}
3438
3439// IVRDEServerInfo properties
3440/////////////////////////////////////////////////////////////////////////////
3441
3442#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
3443 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3444 { \
3445 if (!a##_aName) \
3446 return E_POINTER; \
3447 \
3448 AutoCaller autoCaller(this); \
3449 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3450 \
3451 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3452 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3453 \
3454 uint32_t value; \
3455 uint32_t cbOut = 0; \
3456 \
3457 mParent->consoleVRDPServer()->QueryInfo \
3458 (_aIndex, &value, sizeof(value), &cbOut); \
3459 \
3460 *a##_aName = cbOut? !!value: FALSE; \
3461 \
3462 return S_OK; \
3463 } \
3464 extern void IMPL_GETTER_BOOL_DUMMY(void)
3465
3466#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
3467 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3468 { \
3469 if (!a##_aName) \
3470 return E_POINTER; \
3471 \
3472 AutoCaller autoCaller(this); \
3473 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3474 \
3475 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3476 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3477 \
3478 _aType value; \
3479 uint32_t cbOut = 0; \
3480 \
3481 mParent->consoleVRDPServer()->QueryInfo \
3482 (_aIndex, &value, sizeof(value), &cbOut); \
3483 \
3484 if (_aValueMask) value &= (_aValueMask); \
3485 *a##_aName = cbOut? value: 0; \
3486 \
3487 return S_OK; \
3488 } \
3489 extern void IMPL_GETTER_SCALAR_DUMMY(void)
3490
3491#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
3492 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3493 { \
3494 if (!a##_aName) \
3495 return E_POINTER; \
3496 \
3497 AutoCaller autoCaller(this); \
3498 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3499 \
3500 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3501 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3502 \
3503 uint32_t cbOut = 0; \
3504 \
3505 mParent->consoleVRDPServer()->QueryInfo \
3506 (_aIndex, NULL, 0, &cbOut); \
3507 \
3508 if (cbOut == 0) \
3509 { \
3510 Bstr str(""); \
3511 str.cloneTo(a##_aName); \
3512 return S_OK; \
3513 } \
3514 \
3515 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
3516 \
3517 if (!pchBuffer) \
3518 { \
3519 Log(("VRDEServerInfo::" \
3520 #_aName \
3521 ": Failed to allocate memory %d bytes\n", cbOut)); \
3522 return E_OUTOFMEMORY; \
3523 } \
3524 \
3525 mParent->consoleVRDPServer()->QueryInfo \
3526 (_aIndex, pchBuffer, cbOut, &cbOut); \
3527 \
3528 Bstr str(pchBuffer); \
3529 \
3530 str.cloneTo(a##_aName); \
3531 \
3532 RTMemTmpFree(pchBuffer); \
3533 \
3534 return S_OK; \
3535 } \
3536 extern void IMPL_GETTER_BSTR_DUMMY(void)
3537
3538IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
3539IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
3540IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
3541IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
3542IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
3543IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
3544IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
3545IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
3546IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
3547IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
3548IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
3549IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
3550IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
3551IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
3552IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
3553
3554#undef IMPL_GETTER_BSTR
3555#undef IMPL_GETTER_SCALAR
3556#undef IMPL_GETTER_BOOL
3557/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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