VirtualBox

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

Last change on this file since 61572 was 58383, checked in by vboxsync, 9 years ago

Main: an option to use VRDE auth library from VBoxSVC (not enabled)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 132.7 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 58383 2015-10-23 09:24:16Z vboxsync $ */
2/** @file
3 * VBox Console VRDP helper class.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "ConsoleVRDPServer.h"
19#include "ConsoleImpl.h"
20#include "DisplayImpl.h"
21#include "KeyboardImpl.h"
22#include "MouseImpl.h"
23#include "DrvAudioVRDE.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#include "UsbWebcamInterface.h"
32
33#include "Global.h"
34#include "AutoCaller.h"
35#include "Logging.h"
36
37#include <iprt/asm.h>
38#include <iprt/alloca.h>
39#include <iprt/ldr.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/cpp/utils.h>
43
44#include <VBox/err.h>
45#include <VBox/RemoteDesktop/VRDEOrders.h>
46#include <VBox/com/listeners.h>
47#include <VBox/HostServices/VBoxCrOpenGLSvc.h>
48
49class VRDPConsoleListener
50{
51public:
52 VRDPConsoleListener()
53 {
54 }
55
56 HRESULT init(ConsoleVRDPServer *server)
57 {
58 m_server = server;
59 return S_OK;
60 }
61
62 void uninit()
63 {
64 }
65
66 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
67 {
68 switch (aType)
69 {
70 case VBoxEventType_OnMousePointerShapeChanged:
71 {
72 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
73 Assert(mpscev);
74 BOOL visible, alpha;
75 ULONG xHot, yHot, width, height;
76 com::SafeArray <BYTE> shape;
77
78 mpscev->COMGETTER(Visible)(&visible);
79 mpscev->COMGETTER(Alpha)(&alpha);
80 mpscev->COMGETTER(Xhot)(&xHot);
81 mpscev->COMGETTER(Yhot)(&yHot);
82 mpscev->COMGETTER(Width)(&width);
83 mpscev->COMGETTER(Height)(&height);
84 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
85
86 m_server->onMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
87 break;
88 }
89 case VBoxEventType_OnMouseCapabilityChanged:
90 {
91 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
92 Assert(mccev);
93 if (m_server)
94 {
95 BOOL fAbsoluteMouse;
96 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
97 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
98 }
99 break;
100 }
101 case VBoxEventType_OnKeyboardLedsChanged:
102 {
103 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
104 Assert(klcev);
105
106 if (m_server)
107 {
108 BOOL fNumLock, fCapsLock, fScrollLock;
109 klcev->COMGETTER(NumLock)(&fNumLock);
110 klcev->COMGETTER(CapsLock)(&fCapsLock);
111 klcev->COMGETTER(ScrollLock)(&fScrollLock);
112 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
113 }
114 break;
115 }
116
117 default:
118 AssertFailed();
119 }
120
121 return S_OK;
122 }
123
124private:
125 ConsoleVRDPServer *m_server;
126};
127
128typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
129
130VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
131
132#ifdef DEBUG_sunlover
133#define LOGDUMPPTR Log
134void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
135{
136 unsigned i;
137
138 const uint8_t *pu8And = pu8Shape;
139
140 for (i = 0; i < height; i++)
141 {
142 unsigned j;
143 LOGDUMPPTR(("%p: ", pu8And));
144 for (j = 0; j < (width + 7) / 8; j++)
145 {
146 unsigned k;
147 for (k = 0; k < 8; k++)
148 {
149 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
150 }
151
152 pu8And++;
153 }
154 LOGDUMPPTR(("\n"));
155 }
156
157 if (fXorMaskRGB32)
158 {
159 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
160
161 for (i = 0; i < height; i++)
162 {
163 unsigned j;
164 LOGDUMPPTR(("%p: ", pu32Xor));
165 for (j = 0; j < width; j++)
166 {
167 LOGDUMPPTR(("%08X", *pu32Xor++));
168 }
169 LOGDUMPPTR(("\n"));
170 }
171 }
172 else
173 {
174 /* RDP 24 bit RGB mask. */
175 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
176 for (i = 0; i < height; i++)
177 {
178 unsigned j;
179 LOGDUMPPTR(("%p: ", pu8Xor));
180 for (j = 0; j < width; j++)
181 {
182 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
183 pu8Xor += 3;
184 }
185 LOGDUMPPTR(("\n"));
186 }
187 }
188}
189#else
190#define dumpPointer(a, b, c, d) do {} while (0)
191#endif /* DEBUG_sunlover */
192
193static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width,
194 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
354void ConsoleVRDPServer::onMousePointerShapeChange(BOOL visible,
355 BOOL alpha,
356 ULONG xHot,
357 ULONG yHot,
358 ULONG width,
359 ULONG height,
360 ComSafeArrayIn(BYTE,inShape))
361{
362 Log9(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n",
363 visible, alpha, width, height, xHot, yHot));
364
365 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
366 if (aShape.size() == 0)
367 {
368 if (!visible)
369 {
370 MousePointerHide();
371 }
372 }
373 else if (width != 0 && height != 0)
374 {
375 uint8_t* shape = aShape.raw();
376
377 dumpPointer(shape, width, height, true);
378
379 /* Try the new interface. */
380 if (MousePointer(alpha, xHot, yHot, width, height, shape) == VINF_SUCCESS)
381 {
382 return;
383 }
384
385 /* Continue with the old interface. */
386
387 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
388 * 'shape' AND mask followed by XOR mask.
389 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
390 *
391 * We convert this to RDP color format which consist of
392 * one bpp AND mask and 24 BPP (BGR) color XOR image.
393 *
394 * RDP clients expect 8 aligned width and height of
395 * pointer (preferably 32x32).
396 *
397 * They even contain bugs which do not appear for
398 * 32x32 pointers but would appear for a 41x32 one.
399 *
400 * So set pointer size to 32x32. This can be done safely
401 * because most pointers are 32x32.
402 */
403
404 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
405
406 uint8_t *pu8AndMask = shape;
407 uint8_t *pu8XorMask = shape + cbDstAndMask;
408
409 if (alpha)
410 {
411 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
412
413 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
414 }
415
416 /* Windows guest alpha pointers are wider than 32 pixels.
417 * Try to find out the top-left border of the pointer and
418 * then copy only meaningful bits. All complete top rows
419 * and all complete left columns where (AND == 1 && XOR == 0)
420 * are skipped. Hot spot is adjusted.
421 */
422 uint32_t ySkip = 0; /* How many rows to skip at the top. */
423 uint32_t xSkip = 0; /* How many columns to skip at the left. */
424
425 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
426
427 /* Must not skip the hot spot. */
428 xSkip = RT_MIN(xSkip, xHot);
429 ySkip = RT_MIN(ySkip, yHot);
430
431 /*
432 * Compute size and allocate memory for the pointer.
433 */
434 const uint32_t dstwidth = 32;
435 const uint32_t dstheight = 32;
436
437 VRDECOLORPOINTER *pointer = NULL;
438
439 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
440
441 uint32_t rdpmaskwidth = dstmaskwidth;
442 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
443
444 uint32_t rdpdatawidth = dstwidth * 3;
445 uint32_t rdpdatalen = dstheight * rdpdatawidth;
446
447 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
448
449 if (pointer)
450 {
451 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
452 uint8_t *dataarray = maskarray + rdpmasklen;
453
454 memset(maskarray, 0xFF, rdpmasklen);
455 memset(dataarray, 0x00, rdpdatalen);
456
457 uint32_t srcmaskwidth = (width + 7) / 8;
458 uint32_t srcdatawidth = width * 4;
459
460 /* Copy AND mask. */
461 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
462 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
463
464 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
465 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
466
467 unsigned x, y;
468
469 for (y = 0; y < minheight; y++)
470 {
471 for (x = 0; x < minwidth; x++)
472 {
473 uint32_t byteIndex = (x + xSkip) / 8;
474 uint32_t bitIndex = (x + xSkip) % 8;
475
476 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
477
478 if (!bit)
479 {
480 byteIndex = x / 8;
481 bitIndex = x % 8;
482
483 dst[byteIndex] &= ~(1 << (7 - bitIndex));
484 }
485 }
486
487 src += srcmaskwidth;
488 dst -= rdpmaskwidth;
489 }
490
491 /* Point src to XOR mask */
492 src = pu8XorMask + ySkip * srcdatawidth;
493 dst = dataarray + (dstheight - 1) * rdpdatawidth;
494
495 for (y = 0; y < minheight ; y++)
496 {
497 for (x = 0; x < minwidth; x++)
498 {
499 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
500 }
501
502 src += srcdatawidth;
503 dst -= rdpdatawidth;
504 }
505
506 pointer->u16HotX = (uint16_t)(xHot - xSkip);
507 pointer->u16HotY = (uint16_t)(yHot - ySkip);
508
509 pointer->u16Width = (uint16_t)dstwidth;
510 pointer->u16Height = (uint16_t)dstheight;
511
512 pointer->u16MaskLen = (uint16_t)rdpmasklen;
513 pointer->u16DataLen = (uint16_t)rdpdatalen;
514
515 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
516
517 MousePointerUpdate(pointer);
518
519 RTMemTmpFree(pointer);
520 }
521 }
522}
523
524
525// ConsoleVRDPServer
526////////////////////////////////////////////////////////////////////////////////
527
528RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
529
530PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
531
532VRDEENTRYPOINTS_4 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
533VRDEENTRYPOINTS_4 *ConsoleVRDPServer::mpEntryPoints = NULL;
534
535VRDECALLBACKS_4 ConsoleVRDPServer::mCallbacks =
536{
537 { VRDE_INTERFACE_VERSION_4, sizeof(VRDECALLBACKS_4) },
538 ConsoleVRDPServer::VRDPCallbackQueryProperty,
539 ConsoleVRDPServer::VRDPCallbackClientLogon,
540 ConsoleVRDPServer::VRDPCallbackClientConnect,
541 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
542 ConsoleVRDPServer::VRDPCallbackIntercept,
543 ConsoleVRDPServer::VRDPCallbackUSB,
544 ConsoleVRDPServer::VRDPCallbackClipboard,
545 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
546 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
547 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
548 ConsoleVRDPServer::VRDPCallbackInput,
549 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
550 ConsoleVRDPServer::VRDECallbackAudioIn
551};
552
553DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer,
554 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->i_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->i_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->i_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 >= _64K)
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 case VRDE_QP_VIDEO_CHANNEL:
670 {
671 com::Bstr bstr;
672 HRESULT hrc = server->mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(),
673 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->i_getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(),
702 bstr.asOutParam());
703
704 if (hrc != S_OK)
705 {
706 bstr = "";
707 }
708
709 com::Utf8Str value = bstr;
710
711 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
712
713 if (cbBuffer >= sizeof(uint32_t))
714 {
715 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
716 rc = VINF_SUCCESS;
717 }
718 else
719 {
720 rc = VINF_BUFFER_OVERFLOW;
721 }
722
723 *pcbOut = sizeof(uint32_t);
724 } break;
725
726 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
727 {
728 ULONG ulSunFlsh = 1;
729
730 com::Bstr bstr;
731 HRESULT hrc = server->mConsole->i_machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
732 bstr.asOutParam());
733 if (hrc == S_OK && !bstr.isEmpty())
734 {
735 com::Utf8Str sunFlsh = bstr;
736 if (!sunFlsh.isEmpty())
737 {
738 ulSunFlsh = sunFlsh.toUInt32();
739 }
740 }
741
742 if (cbBuffer >= sizeof(uint32_t))
743 {
744 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
745 rc = VINF_SUCCESS;
746 }
747 else
748 {
749 rc = VINF_BUFFER_OVERFLOW;
750 }
751
752 *pcbOut = sizeof(uint32_t);
753 } break;
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->i_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->i_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->i_getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(),
815 bstrValue.asOutParam());
816 if (FAILED(hrc))
817 {
818 rc = VERR_NOT_SUPPORTED;
819 }
820 }
821 else
822 {
823 rc = VERR_NOT_SUPPORTED;
824 }
825
826 /* Copy the value string to the callers buffer. */
827 if (rc == VINF_SUCCESS)
828 {
829 com::Utf8Str value = bstrValue;
830
831 size_t cb = value.length() + 1;
832
833 if ((size_t)cbInfo >= cb)
834 {
835 memcpy(pFeature->achInfo, value.c_str(), cb);
836 }
837 else
838 {
839 rc = VINF_BUFFER_OVERFLOW;
840 }
841
842 *pcbOut = (uint32_t)cb;
843 }
844 } break;
845
846 case VRDE_SP_NETWORK_BIND_PORT:
847 {
848 if (cbBuffer != sizeof(uint32_t))
849 {
850 rc = VERR_INVALID_PARAMETER;
851 break;
852 }
853
854 ULONG port = *(uint32_t *)pvBuffer;
855
856 server->mVRDPBindPort = port;
857
858 rc = VINF_SUCCESS;
859
860 if (pcbOut)
861 {
862 *pcbOut = sizeof(uint32_t);
863 }
864
865 server->mConsole->i_onVRDEServerInfoChange();
866 } break;
867
868 case VRDE_SP_CLIENT_STATUS:
869 {
870 if (cbBuffer < sizeof(VRDECLIENTSTATUS))
871 {
872 rc = VERR_INVALID_PARAMETER;
873 break;
874 }
875
876 size_t cbStatus = cbBuffer - RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus);
877
878 VRDECLIENTSTATUS *pStatus = (VRDECLIENTSTATUS *)pvBuffer;
879
880 if (cbBuffer < RT_UOFFSETOF(VRDECLIENTSTATUS, achStatus) + pStatus->cbStatus)
881 {
882 rc = VERR_INVALID_PARAMETER;
883 break;
884 }
885
886 size_t cchStatus = 0;
887 rc = RTStrNLenEx(pStatus->achStatus, cbStatus, &cchStatus);
888
889 if (RT_FAILURE(rc))
890 {
891 rc = VERR_INVALID_PARAMETER;
892 break;
893 }
894
895 Log(("VRDE_SP_CLIENT_STATUS [%s]\n", pStatus->achStatus));
896
897 server->mConsole->i_VRDPClientStatusChange(pStatus->u32ClientId, pStatus->achStatus);
898
899 rc = VINF_SUCCESS;
900
901 if (pcbOut)
902 {
903 *pcbOut = cbBuffer;
904 }
905
906 server->mConsole->i_onVRDEServerInfoChange();
907 } break;
908
909 default:
910 break;
911 }
912
913 return rc;
914}
915
916DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser,
917 const char *pszPassword, const char *pszDomain)
918{
919 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
920
921 return server->mConsole->i_VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
922}
923
924DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
925{
926 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
927
928 server->mConsole->i_VRDPClientConnect(u32ClientId);
929
930 /* Should the server report usage of an interface for each client?
931 * Similar to Intercept.
932 */
933 int c = ASMAtomicIncS32(&server->mcClients);
934 if (c == 1)
935 {
936 /* Features which should be enabled only if there is a client. */
937 server->remote3DRedirect(true);
938 }
939}
940
941DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId,
942 uint32_t fu32Intercepted)
943{
944 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
945 AssertPtrReturnVoid(pServer);
946
947 pServer->mConsole->i_VRDPClientDisconnect(u32ClientId, fu32Intercepted);
948
949 if (ASMAtomicReadU32(&pServer->mu32AudioInputClientId) == u32ClientId)
950 {
951 LogFunc(("Disconnected client %u\n", u32ClientId));
952 ASMAtomicWriteU32(&pServer->mu32AudioInputClientId, 0);
953
954 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
955 if (pVRDE)
956 pVRDE->onVRDEInputIntercept(false /* fIntercept */);
957 }
958
959 int32_t cClients = ASMAtomicDecS32(&pServer->mcClients);
960 if (cClients == 0)
961 {
962 /* Features which should be enabled only if there is a client. */
963 pServer->remote3DRedirect(false);
964 }
965}
966
967DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept,
968 void **ppvIntercept)
969{
970 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
971 AssertPtrReturn(pServer, VERR_INVALID_POINTER);
972
973 LogFlowFunc(("%x\n", fu32Intercept));
974
975 int rc = VERR_NOT_SUPPORTED;
976
977 switch (fu32Intercept)
978 {
979 case VRDE_CLIENT_INTERCEPT_AUDIO:
980 {
981 pServer->mConsole->i_VRDPInterceptAudio(u32ClientId);
982 if (ppvIntercept)
983 {
984 *ppvIntercept = pServer;
985 }
986 rc = VINF_SUCCESS;
987 } break;
988
989 case VRDE_CLIENT_INTERCEPT_USB:
990 {
991 pServer->mConsole->i_VRDPInterceptUSB(u32ClientId, ppvIntercept);
992 rc = VINF_SUCCESS;
993 } break;
994
995 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
996 {
997 pServer->mConsole->i_VRDPInterceptClipboard(u32ClientId);
998 if (ppvIntercept)
999 {
1000 *ppvIntercept = pServer;
1001 }
1002 rc = VINF_SUCCESS;
1003 } break;
1004
1005 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
1006 {
1007 /*
1008 * This request is processed internally by the ConsoleVRDPServer.
1009 * Only one client is allowed to intercept audio input.
1010 */
1011 if (ASMAtomicCmpXchgU32(&pServer->mu32AudioInputClientId, u32ClientId, 0) == true)
1012 {
1013 LogFunc(("Intercepting audio input by client %RU32\n", u32ClientId));
1014
1015 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
1016 if (pVRDE)
1017 pVRDE->onVRDEInputIntercept(true /* fIntercept */);
1018 }
1019 else
1020 {
1021 Log(("AUDIOIN: ignored client %RU32, active client %RU32\n", u32ClientId, pServer->mu32AudioInputClientId));
1022 rc = VERR_NOT_SUPPORTED;
1023 }
1024 } break;
1025
1026 default:
1027 break;
1028 }
1029
1030 return rc;
1031}
1032
1033DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId,
1034 uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1035{
1036#ifdef VBOX_WITH_USB
1037 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1038#else
1039 return VERR_NOT_SUPPORTED;
1040#endif
1041}
1042
1043DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId,
1044 uint32_t u32Function, uint32_t u32Format,
1045 const void *pvData, uint32_t cbData)
1046{
1047 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1048}
1049
1050DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId,
1051 VRDEFRAMEBUFFERINFO *pInfo)
1052{
1053 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1054
1055 bool fAvailable = false;
1056
1057 /* Obtain the new screen bitmap. */
1058 HRESULT hr = server->mConsole->i_getDisplay()->QuerySourceBitmap(uScreenId, server->maSourceBitmaps[uScreenId].asOutParam());
1059 if (SUCCEEDED(hr))
1060 {
1061 LONG xOrigin = 0;
1062 LONG yOrigin = 0;
1063 BYTE *pAddress = NULL;
1064 ULONG ulWidth = 0;
1065 ULONG ulHeight = 0;
1066 ULONG ulBitsPerPixel = 0;
1067 ULONG ulBytesPerLine = 0;
1068 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
1069
1070 hr = server->maSourceBitmaps[uScreenId]->QueryBitmapInfo(&pAddress,
1071 &ulWidth,
1072 &ulHeight,
1073 &ulBitsPerPixel,
1074 &ulBytesPerLine,
1075 &bitmapFormat);
1076
1077 if (SUCCEEDED(hr))
1078 {
1079 ULONG dummy;
1080 GuestMonitorStatus_T monitorStatus;
1081 hr = server->mConsole->i_getDisplay()->GetScreenResolution(uScreenId, &dummy, &dummy, &dummy,
1082 &xOrigin, &yOrigin, &monitorStatus);
1083
1084 if (SUCCEEDED(hr))
1085 {
1086 /* Now fill the information as requested by the caller. */
1087 pInfo->pu8Bits = pAddress;
1088 pInfo->xOrigin = xOrigin;
1089 pInfo->yOrigin = yOrigin;
1090 pInfo->cWidth = ulWidth;
1091 pInfo->cHeight = ulHeight;
1092 pInfo->cBitsPerPixel = ulBitsPerPixel;
1093 pInfo->cbLine = ulBytesPerLine;
1094
1095 fAvailable = true;
1096 }
1097 }
1098 }
1099
1100 return fAvailable;
1101}
1102
1103DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1104{
1105 NOREF(pvCallback);
1106 NOREF(uScreenId);
1107 /* Do nothing */
1108}
1109
1110DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1111{
1112 NOREF(pvCallback);
1113 NOREF(uScreenId);
1114 /* Do nothing */
1115}
1116
1117static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1118{
1119 if ( pInputSynch->cGuestNumLockAdaptions
1120 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1121 {
1122 pInputSynch->cGuestNumLockAdaptions--;
1123 pKeyboard->PutScancode(0x45);
1124 pKeyboard->PutScancode(0x45 | 0x80);
1125 }
1126 if ( pInputSynch->cGuestCapsLockAdaptions
1127 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1128 {
1129 pInputSynch->cGuestCapsLockAdaptions--;
1130 pKeyboard->PutScancode(0x3a);
1131 pKeyboard->PutScancode(0x3a | 0x80);
1132 }
1133}
1134
1135DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1136{
1137 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1138 Console *pConsole = server->mConsole;
1139
1140 switch (type)
1141 {
1142 case VRDE_INPUT_SCANCODE:
1143 {
1144 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1145 {
1146 IKeyboard *pKeyboard = pConsole->i_getKeyboard();
1147
1148 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1149
1150 /* Track lock keys. */
1151 if (pInputScancode->uScancode == 0x45)
1152 {
1153 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1154 }
1155 else if (pInputScancode->uScancode == 0x3a)
1156 {
1157 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1158 }
1159 else if (pInputScancode->uScancode == 0x46)
1160 {
1161 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1162 }
1163 else if ((pInputScancode->uScancode & 0x80) == 0)
1164 {
1165 /* Key pressed. */
1166 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1167 }
1168
1169 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1170 }
1171 } break;
1172
1173 case VRDE_INPUT_POINT:
1174 {
1175 if (cbInput == sizeof(VRDEINPUTPOINT))
1176 {
1177 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1178
1179 int mouseButtons = 0;
1180 int iWheel = 0;
1181
1182 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1183 {
1184 mouseButtons |= MouseButtonState_LeftButton;
1185 }
1186 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1187 {
1188 mouseButtons |= MouseButtonState_RightButton;
1189 }
1190 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1191 {
1192 mouseButtons |= MouseButtonState_MiddleButton;
1193 }
1194 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1195 {
1196 mouseButtons |= MouseButtonState_WheelUp;
1197 iWheel = -1;
1198 }
1199 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1200 {
1201 mouseButtons |= MouseButtonState_WheelDown;
1202 iWheel = 1;
1203 }
1204
1205 if (server->m_fGuestWantsAbsolute)
1206 {
1207 pConsole->i_getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel,
1208 0 /* Horizontal wheel */, mouseButtons);
1209 } else
1210 {
1211 pConsole->i_getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1212 pInputPoint->y - server->m_mousey,
1213 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1214 server->m_mousex = pInputPoint->x;
1215 server->m_mousey = pInputPoint->y;
1216 }
1217 }
1218 } break;
1219
1220 case VRDE_INPUT_CAD:
1221 {
1222 pConsole->i_getKeyboard()->PutCAD();
1223 } break;
1224
1225 case VRDE_INPUT_RESET:
1226 {
1227 pConsole->Reset();
1228 } break;
1229
1230 case VRDE_INPUT_SYNCH:
1231 {
1232 if (cbInput == sizeof(VRDEINPUTSYNCH))
1233 {
1234 IKeyboard *pKeyboard = pConsole->i_getKeyboard();
1235
1236 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1237
1238 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1239 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1240 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1241
1242 /* The client initiated synchronization. Always make the guest to reflect the client state.
1243 * Than means, when the guest changes the state itself, it is forced to return to the client
1244 * state.
1245 */
1246 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1247 {
1248 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1249 }
1250
1251 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1252 {
1253 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1254 }
1255
1256 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1257 }
1258 } break;
1259
1260 default:
1261 break;
1262 }
1263}
1264
1265DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight,
1266 unsigned cBitsPerPixel, unsigned uScreenId)
1267{
1268 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1269
1270 server->mConsole->i_getDisplay()->SetVideoModeHint(uScreenId, TRUE /*=enabled*/,
1271 FALSE /*=changeOrigin*/, 0/*=OriginX*/, 0/*=OriginY*/,
1272 cWidth, cHeight, cBitsPerPixel);
1273}
1274
1275DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1276 void *pvCtx,
1277 uint32_t u32ClientId,
1278 uint32_t u32Event,
1279 const void *pvData,
1280 uint32_t cbData)
1281{
1282 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvCallback);
1283 AssertPtrReturnVoid(pServer);
1284
1285 AudioVRDE *pVRDE = pServer->mConsole->i_getAudioVRDE();
1286 if (!pVRDE) /* Nothing to do, bail out early. */
1287 return;
1288
1289 switch (u32Event)
1290 {
1291 case VRDE_AUDIOIN_BEGIN:
1292 {
1293 pVRDE->onVRDEInputBegin(pvCtx, (PVRDEAUDIOINBEGIN)pvData);
1294 break;
1295 }
1296
1297 case VRDE_AUDIOIN_DATA:
1298 pVRDE->onVRDEInputData(pvCtx, pvData, cbData);
1299 break;
1300
1301 case VRDE_AUDIOIN_END:
1302 pVRDE->onVRDEInputEnd(pvCtx);
1303 break;
1304
1305 default:
1306 break;
1307 }
1308}
1309
1310ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1311{
1312 mConsole = console;
1313
1314 int rc = RTCritSectInit(&mCritSect);
1315 AssertRC(rc);
1316
1317 mcClipboardRefs = 0;
1318 mpfnClipboardCallback = NULL;
1319#ifdef VBOX_WITH_USB
1320 mUSBBackends.pHead = NULL;
1321 mUSBBackends.pTail = NULL;
1322
1323 mUSBBackends.thread = NIL_RTTHREAD;
1324 mUSBBackends.fThreadRunning = false;
1325 mUSBBackends.event = 0;
1326#endif
1327
1328 mhServer = 0;
1329 mServerInterfaceVersion = 0;
1330
1331 mcInResize = 0;
1332
1333 m_fGuestWantsAbsolute = false;
1334 m_mousex = 0;
1335 m_mousey = 0;
1336
1337 m_InputSynch.cGuestNumLockAdaptions = 2;
1338 m_InputSynch.cGuestCapsLockAdaptions = 2;
1339
1340 m_InputSynch.fGuestNumLock = false;
1341 m_InputSynch.fGuestCapsLock = false;
1342 m_InputSynch.fGuestScrollLock = false;
1343
1344 m_InputSynch.fClientNumLock = false;
1345 m_InputSynch.fClientCapsLock = false;
1346 m_InputSynch.fClientScrollLock = false;
1347
1348 {
1349 ComPtr<IEventSource> es;
1350 console->COMGETTER(EventSource)(es.asOutParam());
1351 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1352 aConsoleListener.createObject();
1353 aConsoleListener->init(new VRDPConsoleListener(), this);
1354 mConsoleListener = aConsoleListener;
1355 com::SafeArray <VBoxEventType_T> eventTypes;
1356 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1357 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1358 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1359 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1360 }
1361
1362 mVRDPBindPort = -1;
1363
1364#ifndef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
1365 RT_ZERO(mAuthLibCtx);
1366#endif
1367
1368 mu32AudioInputClientId = 0;
1369 mcClients = 0;
1370
1371 /*
1372 * Optional interfaces.
1373 */
1374 m_fInterfaceImage = false;
1375 RT_ZERO(m_interfaceImage);
1376 RT_ZERO(m_interfaceCallbacksImage);
1377 RT_ZERO(m_interfaceMousePtr);
1378 RT_ZERO(m_interfaceSCard);
1379 RT_ZERO(m_interfaceCallbacksSCard);
1380 RT_ZERO(m_interfaceTSMF);
1381 RT_ZERO(m_interfaceCallbacksTSMF);
1382 RT_ZERO(m_interfaceVideoIn);
1383 RT_ZERO(m_interfaceCallbacksVideoIn);
1384 RT_ZERO(m_interfaceInput);
1385 RT_ZERO(m_interfaceCallbacksInput);
1386
1387 rc = RTCritSectInit(&mTSMFLock);
1388 AssertRC(rc);
1389
1390 mEmWebcam = new EmWebcam(this);
1391 AssertPtr(mEmWebcam);
1392}
1393
1394ConsoleVRDPServer::~ConsoleVRDPServer()
1395{
1396 Stop();
1397
1398 if (mConsoleListener)
1399 {
1400 ComPtr<IEventSource> es;
1401 mConsole->COMGETTER(EventSource)(es.asOutParam());
1402 es->UnregisterListener(mConsoleListener);
1403 mConsoleListener.setNull();
1404 }
1405
1406 unsigned i;
1407 for (i = 0; i < RT_ELEMENTS(maSourceBitmaps); i++)
1408 {
1409 maSourceBitmaps[i].setNull();
1410 }
1411
1412 if (mEmWebcam)
1413 {
1414 delete mEmWebcam;
1415 mEmWebcam = NULL;
1416 }
1417
1418 if (RTCritSectIsInitialized(&mCritSect))
1419 {
1420 RTCritSectDelete(&mCritSect);
1421 RT_ZERO(mCritSect);
1422 }
1423
1424 if (RTCritSectIsInitialized(&mTSMFLock))
1425 {
1426 RTCritSectDelete(&mTSMFLock);
1427 RT_ZERO(mTSMFLock);
1428 }
1429}
1430
1431int ConsoleVRDPServer::Launch(void)
1432{
1433 LogFlowThisFunc(("\n"));
1434
1435 IVRDEServer *server = mConsole->i_getVRDEServer();
1436 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1437
1438 /*
1439 * Check if VRDE is enabled.
1440 */
1441 BOOL fEnabled;
1442 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1443 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1444 if (!fEnabled)
1445 return VINF_SUCCESS;
1446
1447 /*
1448 * Check that a VRDE extension pack name is set and resolve it into a
1449 * library path.
1450 */
1451 Bstr bstrExtPack;
1452 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1453 if (FAILED(hrc))
1454 return Global::vboxStatusCodeFromCOM(hrc);
1455 if (bstrExtPack.isEmpty())
1456 return VINF_NOT_SUPPORTED;
1457
1458 Utf8Str strExtPack(bstrExtPack);
1459 Utf8Str strVrdeLibrary;
1460 int vrc = VINF_SUCCESS;
1461 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1462 strVrdeLibrary = "VBoxVRDP";
1463 else
1464 {
1465#ifdef VBOX_WITH_EXTPACK
1466 ExtPackManager *pExtPackMgr = mConsole->i_getExtPackManager();
1467 vrc = pExtPackMgr->i_getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1468#else
1469 vrc = VERR_FILE_NOT_FOUND;
1470#endif
1471 }
1472 if (RT_SUCCESS(vrc))
1473 {
1474 /*
1475 * Load the VRDE library and start the server, if it is enabled.
1476 */
1477 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1478 if (RT_SUCCESS(vrc))
1479 {
1480 VRDEENTRYPOINTS_4 *pEntryPoints4;
1481 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1482
1483 if (RT_SUCCESS(vrc))
1484 {
1485 mServerInterfaceVersion = 4;
1486 mEntryPoints = *pEntryPoints4;
1487 mpEntryPoints = &mEntryPoints;
1488 }
1489 else if (vrc == VERR_VERSION_MISMATCH)
1490 {
1491 /* An older version of VRDE is installed, try version 3. */
1492 VRDEENTRYPOINTS_3 *pEntryPoints3;
1493
1494 static VRDECALLBACKS_3 sCallbacks3 =
1495 {
1496 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1497 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1498 ConsoleVRDPServer::VRDPCallbackClientLogon,
1499 ConsoleVRDPServer::VRDPCallbackClientConnect,
1500 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1501 ConsoleVRDPServer::VRDPCallbackIntercept,
1502 ConsoleVRDPServer::VRDPCallbackUSB,
1503 ConsoleVRDPServer::VRDPCallbackClipboard,
1504 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1505 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1506 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1507 ConsoleVRDPServer::VRDPCallbackInput,
1508 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1509 ConsoleVRDPServer::VRDECallbackAudioIn
1510 };
1511
1512 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1513 if (RT_SUCCESS(vrc))
1514 {
1515 mServerInterfaceVersion = 3;
1516 mEntryPoints.header = pEntryPoints3->header;
1517 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1518 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1519 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1520 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1521 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1522 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1523 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1524 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1525 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1526 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1527 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1528 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1529 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1530 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1531 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1532 mEntryPoints.VRDEGetInterface = NULL;
1533 mpEntryPoints = &mEntryPoints;
1534 }
1535 else if (vrc == VERR_VERSION_MISMATCH)
1536 {
1537 /* An older version of VRDE is installed, try version 1. */
1538 VRDEENTRYPOINTS_1 *pEntryPoints1;
1539
1540 static VRDECALLBACKS_1 sCallbacks1 =
1541 {
1542 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1543 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1544 ConsoleVRDPServer::VRDPCallbackClientLogon,
1545 ConsoleVRDPServer::VRDPCallbackClientConnect,
1546 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1547 ConsoleVRDPServer::VRDPCallbackIntercept,
1548 ConsoleVRDPServer::VRDPCallbackUSB,
1549 ConsoleVRDPServer::VRDPCallbackClipboard,
1550 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1551 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1552 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1553 ConsoleVRDPServer::VRDPCallbackInput,
1554 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1555 };
1556
1557 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1558 if (RT_SUCCESS(vrc))
1559 {
1560 mServerInterfaceVersion = 1;
1561 mEntryPoints.header = pEntryPoints1->header;
1562 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1563 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1564 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1565 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1566 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1567 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1568 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1569 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1570 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1571 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1572 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1573 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1574 mEntryPoints.VRDERedirect = NULL;
1575 mEntryPoints.VRDEAudioInOpen = NULL;
1576 mEntryPoints.VRDEAudioInClose = NULL;
1577 mEntryPoints.VRDEGetInterface = NULL;
1578 mpEntryPoints = &mEntryPoints;
1579 }
1580 }
1581 }
1582
1583 if (RT_SUCCESS(vrc))
1584 {
1585 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1586
1587 if (mServerInterfaceVersion >= 4)
1588 {
1589 /* The server supports optional interfaces. */
1590 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1591
1592 /* Image interface. */
1593 m_interfaceImage.header.u64Version = 1;
1594 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1595
1596 m_interfaceCallbacksImage.header.u64Version = 1;
1597 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1598 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1599
1600 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1601 VRDE_IMAGE_INTERFACE_NAME,
1602 &m_interfaceImage.header,
1603 &m_interfaceCallbacksImage.header,
1604 this);
1605 if (RT_SUCCESS(vrc))
1606 {
1607 LogRel(("VRDE: [%s]\n", VRDE_IMAGE_INTERFACE_NAME));
1608 m_fInterfaceImage = true;
1609 }
1610
1611 /* Mouse pointer interface. */
1612 m_interfaceMousePtr.header.u64Version = 1;
1613 m_interfaceMousePtr.header.u64Size = sizeof(m_interfaceMousePtr);
1614
1615 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1616 VRDE_MOUSEPTR_INTERFACE_NAME,
1617 &m_interfaceMousePtr.header,
1618 NULL,
1619 this);
1620 if (RT_SUCCESS(vrc))
1621 {
1622 LogRel(("VRDE: [%s]\n", VRDE_MOUSEPTR_INTERFACE_NAME));
1623 }
1624 else
1625 {
1626 RT_ZERO(m_interfaceMousePtr);
1627 }
1628
1629 /* Smartcard interface. */
1630 m_interfaceSCard.header.u64Version = 1;
1631 m_interfaceSCard.header.u64Size = sizeof(m_interfaceSCard);
1632
1633 m_interfaceCallbacksSCard.header.u64Version = 1;
1634 m_interfaceCallbacksSCard.header.u64Size = sizeof(m_interfaceCallbacksSCard);
1635 m_interfaceCallbacksSCard.VRDESCardCbNotify = VRDESCardCbNotify;
1636 m_interfaceCallbacksSCard.VRDESCardCbResponse = VRDESCardCbResponse;
1637
1638 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1639 VRDE_SCARD_INTERFACE_NAME,
1640 &m_interfaceSCard.header,
1641 &m_interfaceCallbacksSCard.header,
1642 this);
1643 if (RT_SUCCESS(vrc))
1644 {
1645 LogRel(("VRDE: [%s]\n", VRDE_SCARD_INTERFACE_NAME));
1646 }
1647 else
1648 {
1649 RT_ZERO(m_interfaceSCard);
1650 }
1651
1652 /* Raw TSMF interface. */
1653 m_interfaceTSMF.header.u64Version = 1;
1654 m_interfaceTSMF.header.u64Size = sizeof(m_interfaceTSMF);
1655
1656 m_interfaceCallbacksTSMF.header.u64Version = 1;
1657 m_interfaceCallbacksTSMF.header.u64Size = sizeof(m_interfaceCallbacksTSMF);
1658 m_interfaceCallbacksTSMF.VRDETSMFCbNotify = VRDETSMFCbNotify;
1659
1660 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1661 VRDE_TSMF_INTERFACE_NAME,
1662 &m_interfaceTSMF.header,
1663 &m_interfaceCallbacksTSMF.header,
1664 this);
1665 if (RT_SUCCESS(vrc))
1666 {
1667 LogRel(("VRDE: [%s]\n", VRDE_TSMF_INTERFACE_NAME));
1668 }
1669 else
1670 {
1671 RT_ZERO(m_interfaceTSMF);
1672 }
1673
1674 /* VideoIn interface. */
1675 m_interfaceVideoIn.header.u64Version = 1;
1676 m_interfaceVideoIn.header.u64Size = sizeof(m_interfaceVideoIn);
1677
1678 m_interfaceCallbacksVideoIn.header.u64Version = 1;
1679 m_interfaceCallbacksVideoIn.header.u64Size = sizeof(m_interfaceCallbacksVideoIn);
1680 m_interfaceCallbacksVideoIn.VRDECallbackVideoInNotify = VRDECallbackVideoInNotify;
1681 m_interfaceCallbacksVideoIn.VRDECallbackVideoInDeviceDesc = VRDECallbackVideoInDeviceDesc;
1682 m_interfaceCallbacksVideoIn.VRDECallbackVideoInControl = VRDECallbackVideoInControl;
1683 m_interfaceCallbacksVideoIn.VRDECallbackVideoInFrame = VRDECallbackVideoInFrame;
1684
1685 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1686 VRDE_VIDEOIN_INTERFACE_NAME,
1687 &m_interfaceVideoIn.header,
1688 &m_interfaceCallbacksVideoIn.header,
1689 this);
1690 if (RT_SUCCESS(vrc))
1691 {
1692 LogRel(("VRDE: [%s]\n", VRDE_VIDEOIN_INTERFACE_NAME));
1693 }
1694 else
1695 {
1696 RT_ZERO(m_interfaceVideoIn);
1697 }
1698
1699 /* Input interface. */
1700 m_interfaceInput.header.u64Version = 1;
1701 m_interfaceInput.header.u64Size = sizeof(m_interfaceInput);
1702
1703 m_interfaceCallbacksInput.header.u64Version = 1;
1704 m_interfaceCallbacksInput.header.u64Size = sizeof(m_interfaceCallbacksInput);
1705 m_interfaceCallbacksInput.VRDECallbackInputSetup = VRDECallbackInputSetup;
1706 m_interfaceCallbacksInput.VRDECallbackInputEvent = VRDECallbackInputEvent;
1707
1708 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1709 VRDE_INPUT_INTERFACE_NAME,
1710 &m_interfaceInput.header,
1711 &m_interfaceCallbacksInput.header,
1712 this);
1713 if (RT_SUCCESS(vrc))
1714 {
1715 LogRel(("VRDE: [%s]\n", VRDE_INPUT_INTERFACE_NAME));
1716 }
1717 else
1718 {
1719 RT_ZERO(m_interfaceInput);
1720 }
1721
1722 /* Since these interfaces are optional, it is always a success here. */
1723 vrc = VINF_SUCCESS;
1724 }
1725#ifdef VBOX_WITH_USB
1726 remoteUSBThreadStart();
1727#endif
1728
1729 /*
1730 * Re-init the server current state, which is usually obtained from events.
1731 */
1732 fetchCurrentState();
1733 }
1734 else
1735 {
1736 if (vrc != VERR_NET_ADDRESS_IN_USE)
1737 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1738 /* Don't unload the lib, because it prevents us trying again or
1739 because there may be other users? */
1740 }
1741 }
1742 }
1743
1744 return vrc;
1745}
1746
1747void ConsoleVRDPServer::fetchCurrentState(void)
1748{
1749 ComPtr<IMousePointerShape> mps;
1750 mConsole->i_getMouse()->COMGETTER(PointerShape)(mps.asOutParam());
1751 if (!mps.isNull())
1752 {
1753 BOOL visible, alpha;
1754 ULONG hotX, hotY, width, height;
1755 com::SafeArray <BYTE> shape;
1756
1757 mps->COMGETTER(Visible)(&visible);
1758 mps->COMGETTER(Alpha)(&alpha);
1759 mps->COMGETTER(HotX)(&hotX);
1760 mps->COMGETTER(HotY)(&hotY);
1761 mps->COMGETTER(Width)(&width);
1762 mps->COMGETTER(Height)(&height);
1763 mps->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
1764
1765 onMousePointerShapeChange(visible, alpha, hotX, hotY, width, height, ComSafeArrayAsInParam(shape));
1766 }
1767}
1768
1769typedef struct H3DORInstance
1770{
1771 ConsoleVRDPServer *pThis;
1772 HVRDEIMAGE hImageBitmap;
1773 int32_t x;
1774 int32_t y;
1775 uint32_t w;
1776 uint32_t h;
1777 bool fCreated;
1778 bool fFallback;
1779 bool fTopDown;
1780} H3DORInstance;
1781
1782#define H3DORLOG Log
1783
1784/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1785 const char *pszFormat)
1786{
1787 H3DORLOG(("H3DORBegin: ctx %p [%s]\n", pvContext, pszFormat));
1788
1789 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof(H3DORInstance));
1790
1791 if (p)
1792 {
1793 p->pThis = (ConsoleVRDPServer *)pvContext;
1794 p->hImageBitmap = NULL;
1795 p->x = 0;
1796 p->y = 0;
1797 p->w = 0;
1798 p->h = 0;
1799 p->fCreated = false;
1800 p->fFallback = false;
1801
1802 /* Host 3D service passes the actual format of data in this redirect instance.
1803 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1804 */
1805 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1806 {
1807 /* Accept it. */
1808 p->fTopDown = true;
1809 }
1810 else if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA) == 0)
1811 {
1812 /* Accept it. */
1813 p->fTopDown = false;
1814 }
1815 else
1816 {
1817 RTMemFree(p);
1818 p = NULL;
1819 }
1820 }
1821
1822 H3DORLOG(("H3DORBegin: ins %p\n", p));
1823
1824 /* Caller checks this for NULL. */
1825 *ppvInstance = p;
1826}
1827
1828/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1829 int32_t x, int32_t y, uint32_t w, uint32_t h)
1830{
1831 H3DORLOG(("H3DORGeometry: ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1832
1833 H3DORInstance *p = (H3DORInstance *)pvInstance;
1834 Assert(p);
1835 Assert(p->pThis);
1836
1837 /* @todo find out what to do if size changes to 0x0 from non zero */
1838 if (w == 0 || h == 0)
1839 {
1840 /* Do nothing. */
1841 return;
1842 }
1843
1844 RTRECT rect;
1845 rect.xLeft = x;
1846 rect.yTop = y;
1847 rect.xRight = x + w;
1848 rect.yBottom = y + h;
1849
1850 if (p->hImageBitmap)
1851 {
1852 /* An image handle has been already created,
1853 * check if it has the same size as the reported geometry.
1854 */
1855 if ( p->x == x
1856 && p->y == y
1857 && p->w == w
1858 && p->h == h)
1859 {
1860 H3DORLOG(("H3DORGeometry: geometry not changed\n"));
1861 /* Do nothing. Continue using the existing handle. */
1862 }
1863 else
1864 {
1865 int rc = p->fFallback?
1866 VERR_NOT_SUPPORTED: /* Try to go out of fallback mode. */
1867 p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1868 if (RT_SUCCESS(rc))
1869 {
1870 p->x = x;
1871 p->y = y;
1872 p->w = w;
1873 p->h = h;
1874 }
1875 else
1876 {
1877 /* The handle must be recreated. Delete existing handle here. */
1878 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1879 p->hImageBitmap = NULL;
1880 }
1881 }
1882 }
1883
1884 if (!p->hImageBitmap)
1885 {
1886 /* Create a new bitmap handle. */
1887 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1888 * Clipping can be done here or in VRDP server.
1889 * If VRDP does clipping, then uScreenId parameter
1890 * is not necessary and coords must be global.
1891 * (have to check which coords are used in opengl service).
1892 * Since all VRDE API uses a ScreenId,
1893 * the clipping must be done here in ConsoleVRDPServer
1894 */
1895 uint32_t fu32CompletionFlags = 0;
1896 p->fFallback = false;
1897 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1898 &p->hImageBitmap,
1899 p,
1900 u32ScreenId,
1901 VRDE_IMAGE_F_CREATE_CONTENT_3D
1902 | VRDE_IMAGE_F_CREATE_WINDOW,
1903 &rect,
1904 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1905 NULL,
1906 0,
1907 &fu32CompletionFlags);
1908 if (RT_FAILURE(rc))
1909 {
1910 /* No support for a 3D + WINDOW. Try bitmap updates. */
1911 H3DORLOG(("H3DORGeometry: Fallback to bitmaps\n"));
1912 fu32CompletionFlags = 0;
1913 p->fFallback = true;
1914 rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1915 &p->hImageBitmap,
1916 p,
1917 u32ScreenId,
1918 0,
1919 &rect,
1920 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1921 NULL,
1922 0,
1923 &fu32CompletionFlags);
1924 }
1925
1926 H3DORLOG(("H3DORGeometry: Image handle create %Rrc, flags 0x%RX32\n", rc, fu32CompletionFlags));
1927
1928 if (RT_SUCCESS(rc))
1929 {
1930 p->x = x;
1931 p->y = y;
1932 p->w = w;
1933 p->h = h;
1934
1935 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1936 {
1937 p->fCreated = true;
1938 }
1939 }
1940 else
1941 {
1942 p->hImageBitmap = NULL;
1943 p->w = 0;
1944 p->h = 0;
1945 }
1946 }
1947
1948 H3DORLOG(("H3DORGeometry: ins %p completed\n", pvInstance));
1949}
1950
1951/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1952 uint32_t cRects, const RTRECT *paRects)
1953{
1954 H3DORLOG(("H3DORVisibleRegion: ins %p %d\n", pvInstance, cRects));
1955
1956 H3DORInstance *p = (H3DORInstance *)pvInstance;
1957 Assert(p);
1958 Assert(p->pThis);
1959
1960 if (cRects == 0)
1961 {
1962 /* Complete image is visible. */
1963 RTRECT rect;
1964 rect.xLeft = p->x;
1965 rect.yTop = p->y;
1966 rect.xRight = p->x + p->w;
1967 rect.yBottom = p->y + p->h;
1968 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1969 1,
1970 &rect);
1971 }
1972 else
1973 {
1974 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1975 cRects,
1976 paRects);
1977 }
1978
1979 H3DORLOG(("H3DORVisibleRegion: ins %p completed\n", pvInstance));
1980}
1981
1982/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1983 void *pvData, uint32_t cbData)
1984{
1985 H3DORLOG(("H3DORFrame: ins %p %p %d\n", pvInstance, pvData, cbData));
1986
1987 H3DORInstance *p = (H3DORInstance *)pvInstance;
1988 Assert(p);
1989 Assert(p->pThis);
1990
1991 /* Currently only a topdown BGR0 bitmap format is supported. */
1992 VRDEIMAGEBITMAP image;
1993
1994 image.cWidth = p->w;
1995 image.cHeight = p->h;
1996 image.pvData = pvData;
1997 image.cbData = cbData;
1998 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
1999 image.iScanDelta = 4 * p->w;
2000 if (p->fTopDown)
2001 {
2002 image.iScanDelta = -image.iScanDelta;
2003 }
2004
2005 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
2006 p->x,
2007 p->y,
2008 p->w,
2009 p->h,
2010 &image,
2011 sizeof(VRDEIMAGEBITMAP));
2012
2013 H3DORLOG(("H3DORFrame: ins %p completed\n", pvInstance));
2014}
2015
2016/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
2017{
2018 H3DORLOG(("H3DOREnd: ins %p\n", pvInstance));
2019
2020 H3DORInstance *p = (H3DORInstance *)pvInstance;
2021 Assert(p);
2022 Assert(p->pThis);
2023
2024 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
2025
2026 RTMemFree(p);
2027
2028 H3DORLOG(("H3DOREnd: ins %p completed\n", pvInstance));
2029}
2030
2031/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
2032 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
2033{
2034 int rc = VINF_SUCCESS;
2035
2036 H3DORLOG(("H3DORContextProperty: index %d\n", index));
2037
2038 if (index == H3DOR_PROP_FORMATS)
2039 {
2040 /* Return a comma separated list of supported formats. */
2041 uint32_t cbOut = (uint32_t)strlen(H3DOR_FMT_RGBA_TOPDOWN) + 1
2042 + (uint32_t)strlen(H3DOR_FMT_RGBA) + 1;
2043 if (cbOut <= cbBuffer)
2044 {
2045 char *pch = (char *)pvBuffer;
2046 memcpy(pch, H3DOR_FMT_RGBA_TOPDOWN, strlen(H3DOR_FMT_RGBA_TOPDOWN));
2047 pch += strlen(H3DOR_FMT_RGBA_TOPDOWN);
2048 *pch++ = ',';
2049 memcpy(pch, H3DOR_FMT_RGBA, strlen(H3DOR_FMT_RGBA));
2050 pch += strlen(H3DOR_FMT_RGBA);
2051 *pch++ = '\0';
2052 }
2053 else
2054 {
2055 rc = VERR_BUFFER_OVERFLOW;
2056 }
2057 *pcbOut = cbOut;
2058 }
2059 else
2060 {
2061 rc = VERR_NOT_SUPPORTED;
2062 }
2063
2064 H3DORLOG(("H3DORContextProperty: %Rrc\n", rc));
2065 return rc;
2066}
2067
2068void ConsoleVRDPServer::remote3DRedirect(bool fEnable)
2069{
2070 if (!m_fInterfaceImage)
2071 {
2072 /* No redirect without corresponding interface. */
2073 return;
2074 }
2075
2076 /* Check if 3D redirection has been enabled. It is enabled by default. */
2077 com::Bstr bstr;
2078 HRESULT hrc = mConsole->i_getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
2079
2080 com::Utf8Str value = hrc == S_OK? bstr: "";
2081
2082 bool fAllowed = RTStrICmp(value.c_str(), "true") == 0
2083 || RTStrICmp(value.c_str(), "1") == 0
2084 || value.c_str()[0] == 0;
2085
2086 if (!fAllowed && fEnable)
2087 {
2088 return;
2089 }
2090
2091 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
2092 H3DOUTPUTREDIRECT outputRedirect =
2093 {
2094 this,
2095 H3DORBegin,
2096 H3DORGeometry,
2097 H3DORVisibleRegion,
2098 H3DORFrame,
2099 H3DOREnd,
2100 H3DORContextProperty
2101 };
2102
2103 if (!fEnable)
2104 {
2105 /* This will tell the service to disable rediection. */
2106 RT_ZERO(outputRedirect);
2107 }
2108
2109#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2110 VBOXCRCMDCTL_HGCM data;
2111 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2112 data.Hdr.u32Function = SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT;
2113
2114 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2115 data.aParms[0].u.pointer.addr = &outputRedirect;
2116 data.aParms[0].u.pointer.size = sizeof(outputRedirect);
2117
2118 int rc = mConsole->i_getDisplay()->i_crCtlSubmitSync(&data.Hdr, sizeof (data));
2119 if (!RT_SUCCESS(rc))
2120 {
2121 Log(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2122 return;
2123 }
2124
2125 LogRel(("VRDE: %s 3D redirect.\n", fEnable? "Enabled": "Disabled"));
2126# ifdef DEBUG_misha
2127 AssertFailed();
2128# endif
2129#endif
2130
2131 return;
2132}
2133
2134/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
2135 void *pvUser,
2136 HVRDEIMAGE hVideo,
2137 uint32_t u32Id,
2138 void *pvData,
2139 uint32_t cbData)
2140{
2141 H3DORLOG(("H3DOR: VRDEImageCbNotify: pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
2142 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
2143
2144 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
2145 H3DORInstance *p = (H3DORInstance *)pvUser;
2146 Assert(p);
2147 Assert(p->pThis);
2148 Assert(p->pThis == pServer);
2149
2150 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
2151 {
2152 if (cbData != sizeof(uint32_t))
2153 {
2154 AssertFailed();
2155 return VERR_INVALID_PARAMETER;
2156 }
2157
2158 uint32_t u32StreamId = *(uint32_t *)pvData;
2159 H3DORLOG(("H3DOR: VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
2160 u32StreamId));
2161
2162 if (u32StreamId != 0)
2163 {
2164 p->fCreated = true; // @todo not needed?
2165 }
2166 else
2167 {
2168 /* The stream has not been created. */
2169 }
2170 }
2171
2172 return VINF_SUCCESS;
2173}
2174
2175#undef H3DORLOG
2176
2177/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbNotify(void *pvContext,
2178 uint32_t u32Id,
2179 void *pvData,
2180 uint32_t cbData)
2181{
2182#ifdef VBOX_WITH_USB_CARDREADER
2183 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2184 UsbCardReader *pReader = pThis->mConsole->i_getUsbCardReader();
2185 return pReader->VRDENotify(u32Id, pvData, cbData);
2186#else
2187 NOREF(pvContext);
2188 NOREF(u32Id);
2189 NOREF(pvData);
2190 NOREF(cbData);
2191 return VERR_NOT_SUPPORTED;
2192#endif
2193}
2194
2195/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbResponse(void *pvContext,
2196 int rcRequest,
2197 void *pvUser,
2198 uint32_t u32Function,
2199 void *pvData,
2200 uint32_t cbData)
2201{
2202#ifdef VBOX_WITH_USB_CARDREADER
2203 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2204 UsbCardReader *pReader = pThis->mConsole->i_getUsbCardReader();
2205 return pReader->VRDEResponse(rcRequest, pvUser, u32Function, pvData, cbData);
2206#else
2207 NOREF(pvContext);
2208 NOREF(rcRequest);
2209 NOREF(pvUser);
2210 NOREF(u32Function);
2211 NOREF(pvData);
2212 NOREF(cbData);
2213 return VERR_NOT_SUPPORTED;
2214#endif
2215}
2216
2217int ConsoleVRDPServer::SCardRequest(void *pvUser, uint32_t u32Function, const void *pvData, uint32_t cbData)
2218{
2219 int rc = VINF_SUCCESS;
2220
2221 if (mhServer && mpEntryPoints && m_interfaceSCard.VRDESCardRequest)
2222 {
2223 rc = m_interfaceSCard.VRDESCardRequest(mhServer, pvUser, u32Function, pvData, cbData);
2224 }
2225 else
2226 {
2227 rc = VERR_NOT_SUPPORTED;
2228 }
2229
2230 return rc;
2231}
2232
2233
2234struct TSMFHOSTCHCTX;
2235struct TSMFVRDPCTX;
2236
2237typedef struct TSMFHOSTCHCTX
2238{
2239 ConsoleVRDPServer *pThis;
2240
2241 struct TSMFVRDPCTX *pVRDPCtx; /* NULL if no corresponding host channel context. */
2242
2243 void *pvDataReceived;
2244 uint32_t cbDataReceived;
2245 uint32_t cbDataAllocated;
2246} TSMFHOSTCHCTX;
2247
2248typedef struct TSMFVRDPCTX
2249{
2250 ConsoleVRDPServer *pThis;
2251
2252 VBOXHOSTCHANNELCALLBACKS *pCallbacks;
2253 void *pvCallbacks;
2254
2255 TSMFHOSTCHCTX *pHostChCtx; /* NULL if no corresponding host channel context. */
2256
2257 uint32_t u32ChannelHandle;
2258} TSMFVRDPCTX;
2259
2260static int tsmfContextsAlloc(TSMFHOSTCHCTX **ppHostChCtx, TSMFVRDPCTX **ppVRDPCtx)
2261{
2262 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)RTMemAllocZ(sizeof(TSMFHOSTCHCTX));
2263 if (!pHostChCtx)
2264 {
2265 return VERR_NO_MEMORY;
2266 }
2267
2268 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)RTMemAllocZ(sizeof(TSMFVRDPCTX));
2269 if (!pVRDPCtx)
2270 {
2271 RTMemFree(pHostChCtx);
2272 return VERR_NO_MEMORY;
2273 }
2274
2275 *ppHostChCtx = pHostChCtx;
2276 *ppVRDPCtx = pVRDPCtx;
2277 return VINF_SUCCESS;
2278}
2279
2280int ConsoleVRDPServer::tsmfLock(void)
2281{
2282 int rc = RTCritSectEnter(&mTSMFLock);
2283 AssertRC(rc);
2284 return rc;
2285}
2286
2287void ConsoleVRDPServer::tsmfUnlock(void)
2288{
2289 RTCritSectLeave(&mTSMFLock);
2290}
2291
2292/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelAttach(void *pvProvider,
2293 void **ppvChannel,
2294 uint32_t u32Flags,
2295 VBOXHOSTCHANNELCALLBACKS *pCallbacks,
2296 void *pvCallbacks)
2297{
2298 LogFlowFunc(("\n"));
2299
2300 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvProvider);
2301
2302 /* Create 2 context structures: for the VRDP server and for the host service. */
2303 TSMFHOSTCHCTX *pHostChCtx = NULL;
2304 TSMFVRDPCTX *pVRDPCtx = NULL;
2305
2306 int rc = tsmfContextsAlloc(&pHostChCtx, &pVRDPCtx);
2307 if (RT_FAILURE(rc))
2308 {
2309 return rc;
2310 }
2311
2312 pHostChCtx->pThis = pThis;
2313 pHostChCtx->pVRDPCtx = pVRDPCtx;
2314
2315 pVRDPCtx->pThis = pThis;
2316 pVRDPCtx->pCallbacks = pCallbacks;
2317 pVRDPCtx->pvCallbacks = pvCallbacks;
2318 pVRDPCtx->pHostChCtx = pHostChCtx;
2319
2320 rc = pThis->m_interfaceTSMF.VRDETSMFChannelCreate(pThis->mhServer, pVRDPCtx, u32Flags);
2321
2322 if (RT_SUCCESS(rc))
2323 {
2324 /* @todo contexts should be in a list for accounting. */
2325 *ppvChannel = pHostChCtx;
2326 }
2327 else
2328 {
2329 RTMemFree(pHostChCtx);
2330 RTMemFree(pVRDPCtx);
2331 }
2332
2333 return rc;
2334}
2335
2336/* static */ DECLCALLBACK(void) ConsoleVRDPServer::tsmfHostChannelDetach(void *pvChannel)
2337{
2338 LogFlowFunc(("\n"));
2339
2340 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2341 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2342
2343 int rc = pThis->tsmfLock();
2344 if (RT_SUCCESS(rc))
2345 {
2346 bool fClose = false;
2347 uint32_t u32ChannelHandle = 0;
2348
2349 if (pHostChCtx->pVRDPCtx)
2350 {
2351 /* There is still a VRDP context for this channel. */
2352 pHostChCtx->pVRDPCtx->pHostChCtx = NULL;
2353 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2354 fClose = true;
2355 }
2356
2357 pThis->tsmfUnlock();
2358
2359 RTMemFree(pHostChCtx);
2360
2361 if (fClose)
2362 {
2363 LogFlowFunc(("Closing VRDE channel %d.\n", u32ChannelHandle));
2364 pThis->m_interfaceTSMF.VRDETSMFChannelClose(pThis->mhServer, u32ChannelHandle);
2365 }
2366 else
2367 {
2368 LogFlowFunc(("No VRDE channel.\n"));
2369 }
2370 }
2371}
2372
2373/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelSend(void *pvChannel,
2374 const void *pvData,
2375 uint32_t cbData)
2376{
2377 LogFlowFunc(("cbData %d\n", cbData));
2378
2379 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2380 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2381
2382 int rc = pThis->tsmfLock();
2383 if (RT_SUCCESS(rc))
2384 {
2385 bool fSend = false;
2386 uint32_t u32ChannelHandle = 0;
2387
2388 if (pHostChCtx->pVRDPCtx)
2389 {
2390 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2391 fSend = true;
2392 }
2393
2394 pThis->tsmfUnlock();
2395
2396 if (fSend)
2397 {
2398 LogFlowFunc(("Send to VRDE channel %d.\n", u32ChannelHandle));
2399 rc = pThis->m_interfaceTSMF.VRDETSMFChannelSend(pThis->mhServer, u32ChannelHandle,
2400 pvData, cbData);
2401 }
2402 }
2403
2404 return rc;
2405}
2406
2407/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelRecv(void *pvChannel,
2408 void *pvData,
2409 uint32_t cbData,
2410 uint32_t *pcbReceived,
2411 uint32_t *pcbRemaining)
2412{
2413 LogFlowFunc(("cbData %d\n", cbData));
2414
2415 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2416 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2417
2418 int rc = pThis->tsmfLock();
2419 if (RT_SUCCESS(rc))
2420 {
2421 uint32_t cbToCopy = RT_MIN(cbData, pHostChCtx->cbDataReceived);
2422 uint32_t cbRemaining = pHostChCtx->cbDataReceived - cbToCopy;
2423
2424 LogFlowFunc(("cbToCopy %d, cbRemaining %d\n", cbToCopy, cbRemaining));
2425
2426 if (cbToCopy != 0)
2427 {
2428 memcpy(pvData, pHostChCtx->pvDataReceived, cbToCopy);
2429
2430 if (cbRemaining != 0)
2431 {
2432 memmove(pHostChCtx->pvDataReceived, (uint8_t *)pHostChCtx->pvDataReceived + cbToCopy, cbRemaining);
2433 }
2434
2435 pHostChCtx->cbDataReceived = cbRemaining;
2436 }
2437
2438 pThis->tsmfUnlock();
2439
2440 *pcbRemaining = cbRemaining;
2441 *pcbReceived = cbToCopy;
2442 }
2443
2444 return rc;
2445}
2446
2447/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelControl(void *pvChannel,
2448 uint32_t u32Code,
2449 const void *pvParm,
2450 uint32_t cbParm,
2451 const void *pvData,
2452 uint32_t cbData,
2453 uint32_t *pcbDataReturned)
2454{
2455 LogFlowFunc(("u32Code %u\n", u32Code));
2456
2457 if (!pvChannel)
2458 {
2459 /* Special case, the provider must answer rather than a channel instance. */
2460 if (u32Code == VBOX_HOST_CHANNEL_CTRL_EXISTS)
2461 {
2462 *pcbDataReturned = 0;
2463 return VINF_SUCCESS;
2464 }
2465
2466 return VERR_NOT_IMPLEMENTED;
2467 }
2468
2469 /* Channels do not support this. */
2470 return VERR_NOT_IMPLEMENTED;
2471}
2472
2473
2474void ConsoleVRDPServer::setupTSMF(void)
2475{
2476 if (m_interfaceTSMF.header.u64Size == 0)
2477 {
2478 return;
2479 }
2480
2481 /* Register with the host channel service. */
2482 VBOXHOSTCHANNELINTERFACE hostChannelInterface =
2483 {
2484 this,
2485 tsmfHostChannelAttach,
2486 tsmfHostChannelDetach,
2487 tsmfHostChannelSend,
2488 tsmfHostChannelRecv,
2489 tsmfHostChannelControl
2490 };
2491
2492 VBoxHostChannelHostRegister parms;
2493
2494 static char szProviderName[] = "/vrde/tsmf";
2495
2496 parms.name.type = VBOX_HGCM_SVC_PARM_PTR;
2497 parms.name.u.pointer.addr = &szProviderName[0];
2498 parms.name.u.pointer.size = sizeof(szProviderName);
2499
2500 parms.iface.type = VBOX_HGCM_SVC_PARM_PTR;
2501 parms.iface.u.pointer.addr = &hostChannelInterface;
2502 parms.iface.u.pointer.size = sizeof(hostChannelInterface);
2503
2504 VMMDev *pVMMDev = mConsole->i_getVMMDev();
2505
2506 if (!pVMMDev)
2507 {
2508 AssertMsgFailed(("setupTSMF no vmmdev\n"));
2509 return;
2510 }
2511
2512 int rc = pVMMDev->hgcmHostCall("VBoxHostChannel",
2513 VBOX_HOST_CHANNEL_HOST_FN_REGISTER,
2514 2,
2515 &parms.name);
2516
2517 if (!RT_SUCCESS(rc))
2518 {
2519 Log(("VBOX_HOST_CHANNEL_HOST_FN_REGISTER failed with %Rrc\n", rc));
2520 return;
2521 }
2522
2523 LogRel(("VRDE: Enabled TSMF channel.\n"));
2524
2525 return;
2526}
2527
2528/* @todo these defines must be in a header, which is used by guest component as well. */
2529#define VBOX_TSMF_HCH_CREATE_ACCEPTED (VBOX_HOST_CHANNEL_EVENT_USER + 0)
2530#define VBOX_TSMF_HCH_CREATE_DECLINED (VBOX_HOST_CHANNEL_EVENT_USER + 1)
2531#define VBOX_TSMF_HCH_DISCONNECTED (VBOX_HOST_CHANNEL_EVENT_USER + 2)
2532
2533/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDETSMFCbNotify(void *pvContext,
2534 uint32_t u32Notification,
2535 void *pvChannel,
2536 const void *pvParm,
2537 uint32_t cbParm)
2538{
2539 int rc = VINF_SUCCESS;
2540
2541 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2542
2543 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)pvChannel;
2544
2545 Assert(pVRDPCtx->pThis == pThis);
2546
2547 if (pVRDPCtx->pCallbacks == NULL)
2548 {
2549 LogFlowFunc(("tsmfHostChannel: Channel disconnected. Skipping.\n"));
2550 return;
2551 }
2552
2553 switch (u32Notification)
2554 {
2555 case VRDE_TSMF_N_CREATE_ACCEPTED:
2556 {
2557 VRDETSMFNOTIFYCREATEACCEPTED *p = (VRDETSMFNOTIFYCREATEACCEPTED *)pvParm;
2558 Assert(cbParm == sizeof(VRDETSMFNOTIFYCREATEACCEPTED));
2559
2560 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_ACCEPTED(%p): p->u32ChannelHandle %d\n",
2561 pVRDPCtx, p->u32ChannelHandle));
2562
2563 pVRDPCtx->u32ChannelHandle = p->u32ChannelHandle;
2564
2565 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2566 VBOX_TSMF_HCH_CREATE_ACCEPTED,
2567 NULL, 0);
2568 } break;
2569
2570 case VRDE_TSMF_N_CREATE_DECLINED:
2571 {
2572 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_DECLINED(%p)\n", pVRDPCtx));
2573
2574 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2575 VBOX_TSMF_HCH_CREATE_DECLINED,
2576 NULL, 0);
2577 } break;
2578
2579 case VRDE_TSMF_N_DATA:
2580 {
2581 /* Save the data in the intermediate buffer and send the event. */
2582 VRDETSMFNOTIFYDATA *p = (VRDETSMFNOTIFYDATA *)pvParm;
2583 Assert(cbParm == sizeof(VRDETSMFNOTIFYDATA));
2584
2585 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA(%p): p->cbData %d\n", pVRDPCtx, p->cbData));
2586
2587 VBOXHOSTCHANNELEVENTRECV ev;
2588 ev.u32SizeAvailable = 0;
2589
2590 rc = pThis->tsmfLock();
2591
2592 if (RT_SUCCESS(rc))
2593 {
2594 TSMFHOSTCHCTX *pHostChCtx = pVRDPCtx->pHostChCtx;
2595
2596 if (pHostChCtx)
2597 {
2598 if (pHostChCtx->pvDataReceived)
2599 {
2600 uint32_t cbAlloc = p->cbData + pHostChCtx->cbDataReceived;
2601 pHostChCtx->pvDataReceived = RTMemRealloc(pHostChCtx->pvDataReceived, cbAlloc);
2602 memcpy((uint8_t *)pHostChCtx->pvDataReceived + pHostChCtx->cbDataReceived, p->pvData, p->cbData);
2603
2604 pHostChCtx->cbDataReceived += p->cbData;
2605 pHostChCtx->cbDataAllocated = cbAlloc;
2606 }
2607 else
2608 {
2609 pHostChCtx->pvDataReceived = RTMemAlloc(p->cbData);
2610 memcpy(pHostChCtx->pvDataReceived, p->pvData, p->cbData);
2611
2612 pHostChCtx->cbDataReceived = p->cbData;
2613 pHostChCtx->cbDataAllocated = p->cbData;
2614 }
2615
2616 ev.u32SizeAvailable = p->cbData;
2617 }
2618 else
2619 {
2620 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA: no host channel. Skipping\n"));
2621 }
2622
2623 pThis->tsmfUnlock();
2624 }
2625
2626 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2627 VBOX_HOST_CHANNEL_EVENT_RECV,
2628 &ev, sizeof(ev));
2629 } break;
2630
2631 case VRDE_TSMF_N_DISCONNECTED:
2632 {
2633 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DISCONNECTED(%p)\n", pVRDPCtx));
2634
2635 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2636 VBOX_TSMF_HCH_DISCONNECTED,
2637 NULL, 0);
2638
2639 /* The callback context will not be used anymore. */
2640 pVRDPCtx->pCallbacks->HostChannelCallbackDeleted(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx);
2641 pVRDPCtx->pCallbacks = NULL;
2642 pVRDPCtx->pvCallbacks = NULL;
2643
2644 rc = pThis->tsmfLock();
2645 if (RT_SUCCESS(rc))
2646 {
2647 if (pVRDPCtx->pHostChCtx)
2648 {
2649 /* There is still a host channel context for this channel. */
2650 pVRDPCtx->pHostChCtx->pVRDPCtx = NULL;
2651 }
2652
2653 pThis->tsmfUnlock();
2654
2655 RT_ZERO(*pVRDPCtx);
2656 RTMemFree(pVRDPCtx);
2657 }
2658 } break;
2659
2660 default:
2661 {
2662 AssertFailed();
2663 } break;
2664 }
2665}
2666
2667/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInNotify(void *pvCallback,
2668 uint32_t u32Id,
2669 const void *pvData,
2670 uint32_t cbData)
2671{
2672 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2673 if (pThis->mEmWebcam)
2674 {
2675 pThis->mEmWebcam->EmWebcamCbNotify(u32Id, pvData, cbData);
2676 }
2677}
2678
2679/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInDeviceDesc(void *pvCallback,
2680 int rcRequest,
2681 void *pDeviceCtx,
2682 void *pvUser,
2683 const VRDEVIDEOINDEVICEDESC *pDeviceDesc,
2684 uint32_t cbDevice)
2685{
2686 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2687 if (pThis->mEmWebcam)
2688 {
2689 pThis->mEmWebcam->EmWebcamCbDeviceDesc(rcRequest, pDeviceCtx, pvUser, pDeviceDesc, cbDevice);
2690 }
2691}
2692
2693/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInControl(void *pvCallback,
2694 int rcRequest,
2695 void *pDeviceCtx,
2696 void *pvUser,
2697 const VRDEVIDEOINCTRLHDR *pControl,
2698 uint32_t cbControl)
2699{
2700 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2701 if (pThis->mEmWebcam)
2702 {
2703 pThis->mEmWebcam->EmWebcamCbControl(rcRequest, pDeviceCtx, pvUser, pControl, cbControl);
2704 }
2705}
2706
2707/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInFrame(void *pvCallback,
2708 int rcRequest,
2709 void *pDeviceCtx,
2710 const VRDEVIDEOINPAYLOADHDR *pFrame,
2711 uint32_t cbFrame)
2712{
2713 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2714 if (pThis->mEmWebcam)
2715 {
2716 pThis->mEmWebcam->EmWebcamCbFrame(rcRequest, pDeviceCtx, pFrame, cbFrame);
2717 }
2718}
2719
2720int ConsoleVRDPServer::VideoInDeviceAttach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle, void *pvDeviceCtx)
2721{
2722 int rc;
2723
2724 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceAttach)
2725 {
2726 rc = m_interfaceVideoIn.VRDEVideoInDeviceAttach(mhServer, pDeviceHandle, pvDeviceCtx);
2727 }
2728 else
2729 {
2730 rc = VERR_NOT_SUPPORTED;
2731 }
2732
2733 return rc;
2734}
2735
2736int ConsoleVRDPServer::VideoInDeviceDetach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2737{
2738 int rc;
2739
2740 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceDetach)
2741 {
2742 rc = m_interfaceVideoIn.VRDEVideoInDeviceDetach(mhServer, pDeviceHandle);
2743 }
2744 else
2745 {
2746 rc = VERR_NOT_SUPPORTED;
2747 }
2748
2749 return rc;
2750}
2751
2752int ConsoleVRDPServer::VideoInGetDeviceDesc(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2753{
2754 int rc;
2755
2756 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInGetDeviceDesc)
2757 {
2758 rc = m_interfaceVideoIn.VRDEVideoInGetDeviceDesc(mhServer, pvUser, pDeviceHandle);
2759 }
2760 else
2761 {
2762 rc = VERR_NOT_SUPPORTED;
2763 }
2764
2765 return rc;
2766}
2767
2768int ConsoleVRDPServer::VideoInControl(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle,
2769 const VRDEVIDEOINCTRLHDR *pReq, uint32_t cbReq)
2770{
2771 int rc;
2772
2773 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInControl)
2774 {
2775 rc = m_interfaceVideoIn.VRDEVideoInControl(mhServer, pvUser, pDeviceHandle, pReq, cbReq);
2776 }
2777 else
2778 {
2779 rc = VERR_NOT_SUPPORTED;
2780 }
2781
2782 return rc;
2783}
2784
2785
2786/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputSetup(void *pvCallback,
2787 int rcRequest,
2788 uint32_t u32Method,
2789 const void *pvResult,
2790 uint32_t cbResult)
2791{
2792 NOREF(pvCallback);
2793 NOREF(rcRequest);
2794 NOREF(u32Method);
2795 NOREF(pvResult);
2796 NOREF(cbResult);
2797}
2798
2799/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackInputEvent(void *pvCallback,
2800 uint32_t u32Method,
2801 const void *pvEvent,
2802 uint32_t cbEvent)
2803{
2804 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2805
2806 if (u32Method == VRDE_INPUT_METHOD_TOUCH)
2807 {
2808 if (cbEvent >= sizeof(VRDEINPUTHEADER))
2809 {
2810 VRDEINPUTHEADER *pHeader = (VRDEINPUTHEADER *)pvEvent;
2811
2812 if (pHeader->u16EventId == VRDEINPUT_EVENTID_TOUCH)
2813 {
2814 IMouse *pMouse = pThis->mConsole->i_getMouse();
2815
2816 VRDEINPUT_TOUCH_EVENT_PDU *p = (VRDEINPUT_TOUCH_EVENT_PDU *)pHeader;
2817
2818 uint16_t iFrame;
2819 for (iFrame = 0; iFrame < p->u16FrameCount; iFrame++)
2820 {
2821 VRDEINPUT_TOUCH_FRAME *pFrame = &p->aFrames[iFrame];
2822
2823 com::SafeArray<LONG64> aContacts(pFrame->u16ContactCount);
2824
2825 uint16_t iContact;
2826 for (iContact = 0; iContact < pFrame->u16ContactCount; iContact++)
2827 {
2828 VRDEINPUT_CONTACT_DATA *pContact = &pFrame->aContacts[iContact];
2829
2830 int16_t x = (int16_t)(pContact->i32X + 1);
2831 int16_t y = (int16_t)(pContact->i32Y + 1);
2832 uint8_t contactId = pContact->u8ContactId;
2833 uint8_t contactState = TouchContactState_None;
2834
2835 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INRANGE)
2836 {
2837 contactState |= TouchContactState_InRange;
2838 }
2839 if (pContact->u32ContactFlags & VRDEINPUT_CONTACT_FLAG_INCONTACT)
2840 {
2841 contactState |= TouchContactState_InContact;
2842 }
2843
2844 aContacts[iContact] = RT_MAKE_U64_FROM_U16((uint16_t)x,
2845 (uint16_t)y,
2846 RT_MAKE_U16(contactId, contactState),
2847 0);
2848 }
2849
2850 if (pFrame->u64FrameOffset == 0)
2851 {
2852 pThis->mu64TouchInputTimestampMCS = 0;
2853 }
2854 else
2855 {
2856 pThis->mu64TouchInputTimestampMCS += pFrame->u64FrameOffset;
2857 }
2858
2859 pMouse->PutEventMultiTouch(pFrame->u16ContactCount,
2860 ComSafeArrayAsInParam(aContacts),
2861 (ULONG)(pThis->mu64TouchInputTimestampMCS / 1000)); /* Micro->milliseconds. */
2862 }
2863 }
2864 else if (pHeader->u16EventId == VRDEINPUT_EVENTID_DISMISS_HOVERING_CONTACT)
2865 {
2866 /* @todo */
2867 }
2868 else
2869 {
2870 AssertMsgFailed(("EventId %d\n", pHeader->u16EventId));
2871 }
2872 }
2873 }
2874}
2875
2876
2877void ConsoleVRDPServer::EnableConnections(void)
2878{
2879 if (mpEntryPoints && mhServer)
2880 {
2881 mpEntryPoints->VRDEEnableConnections(mhServer, true);
2882
2883 /* Setup the generic TSMF channel. */
2884 setupTSMF();
2885 }
2886}
2887
2888void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
2889{
2890 if (mpEntryPoints && mhServer)
2891 {
2892 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
2893 }
2894}
2895
2896int ConsoleVRDPServer::MousePointer(BOOL alpha,
2897 ULONG xHot,
2898 ULONG yHot,
2899 ULONG width,
2900 ULONG height,
2901 const uint8_t *pu8Shape)
2902{
2903 int rc = VINF_SUCCESS;
2904
2905 if (mhServer && mpEntryPoints && m_interfaceMousePtr.VRDEMousePtr)
2906 {
2907 size_t cbMask = (((width + 7) / 8) * height + 3) & ~3;
2908 size_t cbData = width * height * 4;
2909
2910 size_t cbDstMask = alpha? 0: cbMask;
2911
2912 size_t cbPointer = sizeof(VRDEMOUSEPTRDATA) + cbDstMask + cbData;
2913 uint8_t *pu8Pointer = (uint8_t *)RTMemAlloc(cbPointer);
2914 if (pu8Pointer != NULL)
2915 {
2916 VRDEMOUSEPTRDATA *pPointer = (VRDEMOUSEPTRDATA *)pu8Pointer;
2917
2918 pPointer->u16HotX = (uint16_t)xHot;
2919 pPointer->u16HotY = (uint16_t)yHot;
2920 pPointer->u16Width = (uint16_t)width;
2921 pPointer->u16Height = (uint16_t)height;
2922 pPointer->u16MaskLen = (uint16_t)cbDstMask;
2923 pPointer->u32DataLen = (uint32_t)cbData;
2924
2925 /* AND mask. */
2926 uint8_t *pu8Mask = pu8Pointer + sizeof(VRDEMOUSEPTRDATA);
2927 if (cbDstMask)
2928 {
2929 memcpy(pu8Mask, pu8Shape, cbDstMask);
2930 }
2931
2932 /* XOR mask */
2933 uint8_t *pu8Data = pu8Mask + pPointer->u16MaskLen;
2934 memcpy(pu8Data, pu8Shape + cbMask, cbData);
2935
2936 m_interfaceMousePtr.VRDEMousePtr(mhServer, pPointer);
2937
2938 RTMemFree(pu8Pointer);
2939 }
2940 else
2941 {
2942 rc = VERR_NO_MEMORY;
2943 }
2944 }
2945 else
2946 {
2947 rc = VERR_NOT_SUPPORTED;
2948 }
2949
2950 return rc;
2951}
2952
2953void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
2954{
2955 if (mpEntryPoints && mhServer)
2956 {
2957 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
2958 }
2959}
2960
2961void ConsoleVRDPServer::MousePointerHide(void)
2962{
2963 if (mpEntryPoints && mhServer)
2964 {
2965 mpEntryPoints->VRDEHidePointer(mhServer);
2966 }
2967}
2968
2969void ConsoleVRDPServer::Stop(void)
2970{
2971 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
2972 * linux. Just remove this when it's 100% sure that problem has been fixed. */
2973
2974#ifdef VBOX_WITH_USB
2975 remoteUSBThreadStop();
2976#endif /* VBOX_WITH_USB */
2977
2978 if (mhServer)
2979 {
2980 HVRDESERVER hServer = mhServer;
2981
2982 /* Reset the handle to avoid further calls to the server. */
2983 mhServer = 0;
2984
2985 /* Workaround for VM process hangs on termination.
2986 *
2987 * Make sure that the server is not currently processing a resize.
2988 * mhServer 0 will not allow to enter the server again.
2989 * Wait until any current resize returns from the server.
2990 */
2991 if (mcInResize)
2992 {
2993 LogRel(("VRDP: waiting for resize %d\n", mcInResize));
2994
2995 int i = 0;
2996 while (mcInResize && ++i < 100)
2997 {
2998 RTThreadSleep(10);
2999 }
3000 }
3001
3002 if (mpEntryPoints && hServer)
3003 {
3004 mpEntryPoints->VRDEDestroy(hServer);
3005 }
3006 }
3007
3008#ifndef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3009 AuthLibUnload(&mAuthLibCtx);
3010#endif
3011}
3012
3013/* Worker thread for Remote USB. The thread polls the clients for
3014 * the list of attached USB devices.
3015 * The thread is also responsible for attaching/detaching devices
3016 * to/from the VM.
3017 *
3018 * It is expected that attaching/detaching is not a frequent operation.
3019 *
3020 * The thread is always running when the VRDP server is active.
3021 *
3022 * The thread scans backends and requests the device list every 2 seconds.
3023 *
3024 * When device list is available, the thread calls the Console to process it.
3025 *
3026 */
3027#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
3028
3029#ifdef VBOX_WITH_USB
3030static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
3031{
3032 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
3033
3034 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
3035
3036 pOwner->notifyRemoteUSBThreadRunning(self);
3037
3038 while (pOwner->isRemoteUSBThreadRunning())
3039 {
3040 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3041
3042 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
3043 {
3044 pRemoteUSBBackend->PollRemoteDevices();
3045 }
3046
3047 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
3048
3049 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
3050 }
3051
3052 return VINF_SUCCESS;
3053}
3054
3055void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
3056{
3057 mUSBBackends.thread = thread;
3058 mUSBBackends.fThreadRunning = true;
3059 int rc = RTThreadUserSignal(thread);
3060 AssertRC(rc);
3061}
3062
3063bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
3064{
3065 return mUSBBackends.fThreadRunning;
3066}
3067
3068void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
3069{
3070 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
3071 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
3072 NOREF(rc);
3073}
3074
3075void ConsoleVRDPServer::remoteUSBThreadStart(void)
3076{
3077 int rc = RTSemEventCreate(&mUSBBackends.event);
3078
3079 if (RT_FAILURE(rc))
3080 {
3081 AssertFailed();
3082 mUSBBackends.event = 0;
3083 }
3084
3085 if (RT_SUCCESS(rc))
3086 {
3087 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
3088 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
3089 }
3090
3091 if (RT_FAILURE(rc))
3092 {
3093 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
3094 mUSBBackends.thread = NIL_RTTHREAD;
3095 }
3096 else
3097 {
3098 /* Wait until the thread is ready. */
3099 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
3100 AssertRC(rc);
3101 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
3102 }
3103}
3104
3105void ConsoleVRDPServer::remoteUSBThreadStop(void)
3106{
3107 mUSBBackends.fThreadRunning = false;
3108
3109 if (mUSBBackends.thread != NIL_RTTHREAD)
3110 {
3111 Assert (mUSBBackends.event != 0);
3112
3113 RTSemEventSignal(mUSBBackends.event);
3114
3115 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
3116 AssertRC(rc);
3117
3118 mUSBBackends.thread = NIL_RTTHREAD;
3119 }
3120
3121 if (mUSBBackends.event)
3122 {
3123 RTSemEventDestroy(mUSBBackends.event);
3124 mUSBBackends.event = 0;
3125 }
3126}
3127#endif /* VBOX_WITH_USB */
3128
3129AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
3130 const char *pszUser, const char *pszPassword, const char *pszDomain,
3131 uint32_t u32ClientId)
3132{
3133 LogFlowFunc(("uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
3134 uuid.raw(), guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
3135
3136 AuthResult result = AuthResultAccessDenied;
3137
3138#ifdef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3139 try
3140 {
3141 /* Init auth parameters. Order is important. */
3142 SafeArray<BSTR> authParams;
3143 Bstr("VRDEAUTH" ).detachTo(authParams.appendedRaw());
3144 Bstr(uuid.toUtf16() ).detachTo(authParams.appendedRaw());
3145 BstrFmt("%u", guestJudgement).detachTo(authParams.appendedRaw());
3146 Bstr(pszUser ).detachTo(authParams.appendedRaw());
3147 Bstr(pszPassword ).detachTo(authParams.appendedRaw());
3148 Bstr(pszDomain ).detachTo(authParams.appendedRaw());
3149 BstrFmt("%u", u32ClientId).detachTo(authParams.appendedRaw());
3150
3151 Bstr authResult;
3152 HRESULT hr = mConsole->mControl->AuthenticateExternal(ComSafeArrayAsInParam(authParams),
3153 authResult.asOutParam());
3154 LogFlowFunc(("%Rhrc [%ls]\n", hr, authResult.raw()));
3155
3156 size_t cbPassword = RTUtf16Len((PRTUTF16)authParams[4]) * sizeof(RTUTF16);
3157 if (cbPassword)
3158 RTMemWipeThoroughly(authParams[4], cbPassword, 10 /* cPasses */);
3159
3160 if (SUCCEEDED(hr) && authResult == "granted")
3161 result = AuthResultAccessGranted;
3162 }
3163 catch (std::bad_alloc)
3164 {
3165 }
3166#else
3167 /*
3168 * Called only from VRDP input thread. So thread safety is not required.
3169 */
3170
3171 if (!mAuthLibCtx.hAuthLibrary)
3172 {
3173 /* Load the external authentication library. */
3174 Bstr authLibrary;
3175 mConsole->i_getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
3176
3177 Utf8Str filename = authLibrary;
3178
3179 int rc = AuthLibLoad(&mAuthLibCtx, filename.c_str());
3180
3181 if (RT_FAILURE(rc))
3182 {
3183 mConsole->setError(E_FAIL,
3184 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
3185 filename.c_str(),
3186 rc);
3187
3188 return AuthResultAccessDenied;
3189 }
3190 }
3191
3192 result = AuthLibAuthenticate(&mAuthLibCtx,
3193 uuid.raw(), guestJudgement,
3194 pszUser, pszPassword, pszDomain,
3195 u32ClientId);
3196#endif /* !VBOX_WITH_VRDEAUTH_IN_VBOXSVC */
3197
3198 switch (result)
3199 {
3200 case AuthResultAccessDenied:
3201 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
3202 break;
3203 case AuthResultAccessGranted:
3204 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
3205 break;
3206 case AuthResultDelegateToGuest:
3207 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
3208 break;
3209 default:
3210 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
3211 result = AuthResultAccessDenied;
3212 }
3213
3214 LogFlowFunc(("result = %d\n", result));
3215
3216 return result;
3217}
3218
3219void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
3220{
3221 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
3222 uuid.raw(), u32ClientId));
3223
3224#ifdef VBOX_WITH_VRDEAUTH_IN_VBOXSVC
3225 try
3226 {
3227 /* Init auth parameters. Order is important. */
3228 SafeArray<BSTR> authParams;
3229 Bstr("VRDEAUTHDISCONNECT").detachTo(authParams.appendedRaw());
3230 Bstr(uuid.toUtf16() ).detachTo(authParams.appendedRaw());
3231 BstrFmt("%u", u32ClientId).detachTo(authParams.appendedRaw());
3232
3233 Bstr authResult;
3234 HRESULT hr = mConsole->mControl->AuthenticateExternal(ComSafeArrayAsInParam(authParams),
3235 authResult.asOutParam());
3236 LogFlowFunc(("%Rhrc [%ls]\n", hr, authResult.raw())); NOREF(hr);
3237 }
3238 catch (std::bad_alloc)
3239 {
3240 }
3241#else
3242 AuthLibDisconnect(&mAuthLibCtx, uuid.raw(), u32ClientId);
3243#endif /* !VBOX_WITH_VRDEAUTH_IN_VBOXSVC */
3244}
3245
3246int ConsoleVRDPServer::lockConsoleVRDPServer(void)
3247{
3248 int rc = RTCritSectEnter(&mCritSect);
3249 AssertRC(rc);
3250 return rc;
3251}
3252
3253void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
3254{
3255 RTCritSectLeave(&mCritSect);
3256}
3257
3258DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
3259 uint32_t u32ClientId,
3260 uint32_t u32Function,
3261 uint32_t u32Format,
3262 const void *pvData,
3263 uint32_t cbData)
3264{
3265 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
3266 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
3267
3268 int rc = VINF_SUCCESS;
3269
3270 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
3271
3272 NOREF(u32ClientId);
3273
3274 switch (u32Function)
3275 {
3276 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
3277 {
3278 if (pServer->mpfnClipboardCallback)
3279 {
3280 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
3281 u32Format,
3282 (void *)pvData,
3283 cbData);
3284 }
3285 } break;
3286
3287 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
3288 {
3289 if (pServer->mpfnClipboardCallback)
3290 {
3291 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
3292 u32Format,
3293 (void *)pvData,
3294 cbData);
3295 }
3296 } break;
3297
3298 default:
3299 rc = VERR_NOT_SUPPORTED;
3300 }
3301
3302 return rc;
3303}
3304
3305DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
3306 uint32_t u32Function,
3307 void *pvParms,
3308 uint32_t cbParms)
3309{
3310 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
3311 pvExtension, u32Function, pvParms, cbParms));
3312
3313 int rc = VINF_SUCCESS;
3314
3315 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
3316
3317 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
3318
3319 switch (u32Function)
3320 {
3321 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
3322 {
3323 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
3324 } break;
3325
3326 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
3327 {
3328 /* The guest announces clipboard formats. This must be delivered to all clients. */
3329 if (mpEntryPoints && pServer->mhServer)
3330 {
3331 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3332 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
3333 pParms->u32Format,
3334 NULL,
3335 0,
3336 NULL);
3337 }
3338 } break;
3339
3340 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
3341 {
3342 /* The clipboard service expects that the pvData buffer will be filled
3343 * with clipboard data. The server returns the data from the client that
3344 * announced the requested format most recently.
3345 */
3346 if (mpEntryPoints && pServer->mhServer)
3347 {
3348 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3349 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
3350 pParms->u32Format,
3351 pParms->u.pvData,
3352 pParms->cbData,
3353 &pParms->cbData);
3354 }
3355 } break;
3356
3357 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
3358 {
3359 if (mpEntryPoints && pServer->mhServer)
3360 {
3361 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3362 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
3363 pParms->u32Format,
3364 pParms->u.pvData,
3365 pParms->cbData,
3366 NULL);
3367 }
3368 } break;
3369
3370 default:
3371 rc = VERR_NOT_SUPPORTED;
3372 }
3373
3374 return rc;
3375}
3376
3377void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
3378{
3379 int rc = lockConsoleVRDPServer();
3380
3381 if (RT_SUCCESS(rc))
3382 {
3383 if (mcClipboardRefs == 0)
3384 {
3385 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
3386
3387 if (RT_SUCCESS(rc))
3388 {
3389 mcClipboardRefs++;
3390 }
3391 }
3392
3393 unlockConsoleVRDPServer();
3394 }
3395}
3396
3397void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
3398{
3399 int rc = lockConsoleVRDPServer();
3400
3401 if (RT_SUCCESS(rc))
3402 {
3403 mcClipboardRefs--;
3404
3405 if (mcClipboardRefs == 0)
3406 {
3407 HGCMHostUnregisterServiceExtension(mhClipboard);
3408 }
3409
3410 unlockConsoleVRDPServer();
3411 }
3412}
3413
3414/* That is called on INPUT thread of the VRDP server.
3415 * The ConsoleVRDPServer keeps a list of created backend instances.
3416 */
3417void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
3418{
3419#ifdef VBOX_WITH_USB
3420 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
3421
3422 /* Create a new instance of the USB backend for the new client. */
3423 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
3424
3425 if (pRemoteUSBBackend)
3426 {
3427 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
3428
3429 /* Append the new instance in the list. */
3430 int rc = lockConsoleVRDPServer();
3431
3432 if (RT_SUCCESS(rc))
3433 {
3434 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
3435 if (mUSBBackends.pHead)
3436 {
3437 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
3438 }
3439 else
3440 {
3441 mUSBBackends.pTail = pRemoteUSBBackend;
3442 }
3443
3444 mUSBBackends.pHead = pRemoteUSBBackend;
3445
3446 unlockConsoleVRDPServer();
3447
3448 if (ppvIntercept)
3449 {
3450 *ppvIntercept = pRemoteUSBBackend;
3451 }
3452 }
3453
3454 if (RT_FAILURE(rc))
3455 {
3456 pRemoteUSBBackend->Release();
3457 }
3458 }
3459#endif /* VBOX_WITH_USB */
3460}
3461
3462void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
3463{
3464#ifdef VBOX_WITH_USB
3465 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
3466
3467 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3468
3469 /* Find the instance. */
3470 int rc = lockConsoleVRDPServer();
3471
3472 if (RT_SUCCESS(rc))
3473 {
3474 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3475
3476 if (pRemoteUSBBackend)
3477 {
3478 /* Notify that it will be deleted. */
3479 pRemoteUSBBackend->NotifyDelete();
3480 }
3481
3482 unlockConsoleVRDPServer();
3483 }
3484
3485 if (pRemoteUSBBackend)
3486 {
3487 /* Here the instance has been excluded from the list and can be dereferenced. */
3488 pRemoteUSBBackend->Release();
3489 }
3490#endif
3491}
3492
3493void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
3494{
3495#ifdef VBOX_WITH_USB
3496 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3497
3498 /* Find the instance. */
3499 int rc = lockConsoleVRDPServer();
3500
3501 if (RT_SUCCESS(rc))
3502 {
3503 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3504
3505 if (pRemoteUSBBackend)
3506 {
3507 /* Inform the backend instance that it is referenced by the Guid. */
3508 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
3509
3510 if (fAdded)
3511 {
3512 /* Reference the instance because its pointer is being taken. */
3513 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
3514 }
3515 else
3516 {
3517 pRemoteUSBBackend = NULL;
3518 }
3519 }
3520
3521 unlockConsoleVRDPServer();
3522 }
3523
3524 if (pRemoteUSBBackend)
3525 {
3526 return pRemoteUSBBackend->GetBackendCallbackPointer();
3527 }
3528
3529#endif
3530 return NULL;
3531}
3532
3533void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
3534{
3535#ifdef VBOX_WITH_USB
3536 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3537
3538 /* Find the instance. */
3539 int rc = lockConsoleVRDPServer();
3540
3541 if (RT_SUCCESS(rc))
3542 {
3543 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
3544
3545 if (pRemoteUSBBackend)
3546 {
3547 pRemoteUSBBackend->removeUUID(pGuid);
3548 }
3549
3550 unlockConsoleVRDPServer();
3551
3552 if (pRemoteUSBBackend)
3553 {
3554 pRemoteUSBBackend->Release();
3555 }
3556 }
3557#endif
3558}
3559
3560RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
3561{
3562 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
3563
3564 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
3565#ifdef VBOX_WITH_USB
3566
3567 int rc = lockConsoleVRDPServer();
3568
3569 if (RT_SUCCESS(rc))
3570 {
3571 if (pRemoteUSBBackend == NULL)
3572 {
3573 /* The first backend in the list is requested. */
3574 pNextRemoteUSBBackend = mUSBBackends.pHead;
3575 }
3576 else
3577 {
3578 /* Get pointer to the next backend. */
3579 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3580 }
3581
3582 if (pNextRemoteUSBBackend)
3583 {
3584 pNextRemoteUSBBackend->AddRef();
3585 }
3586
3587 unlockConsoleVRDPServer();
3588
3589 if (pRemoteUSBBackend)
3590 {
3591 pRemoteUSBBackend->Release();
3592 }
3593 }
3594#endif
3595
3596 return pNextRemoteUSBBackend;
3597}
3598
3599#ifdef VBOX_WITH_USB
3600/* Internal method. Called under the ConsoleVRDPServerLock. */
3601RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
3602{
3603 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3604
3605 while (pRemoteUSBBackend)
3606 {
3607 if (pRemoteUSBBackend->ClientId() == u32ClientId)
3608 {
3609 break;
3610 }
3611
3612 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3613 }
3614
3615 return pRemoteUSBBackend;
3616}
3617
3618/* Internal method. Called under the ConsoleVRDPServerLock. */
3619RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
3620{
3621 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3622
3623 while (pRemoteUSBBackend)
3624 {
3625 if (pRemoteUSBBackend->findUUID(pGuid))
3626 {
3627 break;
3628 }
3629
3630 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3631 }
3632
3633 return pRemoteUSBBackend;
3634}
3635#endif
3636
3637/* Internal method. Called by the backend destructor. */
3638void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
3639{
3640#ifdef VBOX_WITH_USB
3641 int rc = lockConsoleVRDPServer();
3642 AssertRC(rc);
3643
3644 /* Exclude the found instance from the list. */
3645 if (pRemoteUSBBackend->pNext)
3646 {
3647 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
3648 }
3649 else
3650 {
3651 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
3652 }
3653
3654 if (pRemoteUSBBackend->pPrev)
3655 {
3656 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
3657 }
3658 else
3659 {
3660 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3661 }
3662
3663 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
3664
3665 unlockConsoleVRDPServer();
3666#endif
3667}
3668
3669
3670void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
3671{
3672 if (mpEntryPoints && mhServer)
3673 {
3674 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
3675 }
3676}
3677
3678void ConsoleVRDPServer::SendResize(void)
3679{
3680 if (mpEntryPoints && mhServer)
3681 {
3682 ++mcInResize;
3683 mpEntryPoints->VRDEResize(mhServer);
3684 --mcInResize;
3685 }
3686}
3687
3688void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
3689{
3690 VRDEORDERHDR update;
3691 update.x = x;
3692 update.y = y;
3693 update.w = w;
3694 update.h = h;
3695 if (mpEntryPoints && mhServer)
3696 {
3697 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
3698 }
3699}
3700
3701void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
3702{
3703 if (mpEntryPoints && mhServer)
3704 {
3705 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
3706 }
3707}
3708
3709void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
3710{
3711 if (mpEntryPoints && mhServer)
3712 {
3713 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
3714 }
3715}
3716
3717void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
3718{
3719 if (mpEntryPoints && mhServer)
3720 {
3721 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
3722 }
3723}
3724
3725int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
3726 void *pvContext,
3727 uint32_t cSamples,
3728 uint32_t iSampleHz,
3729 uint32_t cChannels,
3730 uint32_t cBits)
3731{
3732 if ( mhServer
3733 && mpEntryPoints && mpEntryPoints->VRDEAudioInOpen)
3734 {
3735 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3736 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3737 {
3738 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
3739 mpEntryPoints->VRDEAudioInOpen(mhServer,
3740 pvContext,
3741 u32ClientId,
3742 audioFormat,
3743 cSamples);
3744 if (ppvUserCtx)
3745 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
3746 * Currently not used because only one client is allowed to
3747 * do audio input and the client ID is saved by the ConsoleVRDPServer.
3748 */
3749 return VINF_SUCCESS;
3750 }
3751 }
3752
3753 /*
3754 * Not supported or no client connected.
3755 */
3756 return VERR_NOT_SUPPORTED;
3757}
3758
3759void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
3760{
3761 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
3762 {
3763 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3764 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3765 {
3766 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
3767 }
3768 }
3769}
3770
3771void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
3772{
3773 if (index == VRDE_QI_PORT)
3774 {
3775 uint32_t cbOut = sizeof(int32_t);
3776
3777 if (cbBuffer >= cbOut)
3778 {
3779 *pcbOut = cbOut;
3780 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
3781 }
3782 }
3783 else if (mpEntryPoints && mhServer)
3784 {
3785 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
3786 }
3787}
3788
3789/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
3790{
3791 int rc = VINF_SUCCESS;
3792
3793 if (mVRDPLibrary == NIL_RTLDRMOD)
3794 {
3795 RTERRINFOSTATIC ErrInfo;
3796 RTErrInfoInitStatic(&ErrInfo);
3797
3798 if (RTPathHavePath(pszLibraryName))
3799 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
3800 else
3801 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
3802 if (RT_SUCCESS(rc))
3803 {
3804 struct SymbolEntry
3805 {
3806 const char *name;
3807 void **ppfn;
3808 };
3809
3810 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
3811
3812 static const struct SymbolEntry s_aSymbols[] =
3813 {
3814 DEFSYMENTRY(VRDECreateServer)
3815 };
3816
3817 #undef DEFSYMENTRY
3818
3819 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
3820 {
3821 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
3822
3823 if (RT_FAILURE(rc))
3824 {
3825 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
3826 break;
3827 }
3828 }
3829 }
3830 else
3831 {
3832 if (RTErrInfoIsSet(&ErrInfo.Core))
3833 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
3834 else
3835 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
3836
3837 mVRDPLibrary = NIL_RTLDRMOD;
3838 }
3839 }
3840
3841 if (RT_FAILURE(rc))
3842 {
3843 if (mVRDPLibrary != NIL_RTLDRMOD)
3844 {
3845 RTLdrClose(mVRDPLibrary);
3846 mVRDPLibrary = NIL_RTLDRMOD;
3847 }
3848 }
3849
3850 return rc;
3851}
3852
3853/*
3854 * IVRDEServerInfo implementation.
3855 */
3856// constructor / destructor
3857/////////////////////////////////////////////////////////////////////////////
3858
3859VRDEServerInfo::VRDEServerInfo()
3860 : mParent(NULL)
3861{
3862}
3863
3864VRDEServerInfo::~VRDEServerInfo()
3865{
3866}
3867
3868
3869HRESULT VRDEServerInfo::FinalConstruct()
3870{
3871 return BaseFinalConstruct();
3872}
3873
3874void VRDEServerInfo::FinalRelease()
3875{
3876 uninit();
3877 BaseFinalRelease();
3878}
3879
3880// public methods only for internal purposes
3881/////////////////////////////////////////////////////////////////////////////
3882
3883/**
3884 * Initializes the guest object.
3885 */
3886HRESULT VRDEServerInfo::init(Console *aParent)
3887{
3888 LogFlowThisFunc(("aParent=%p\n", aParent));
3889
3890 ComAssertRet(aParent, E_INVALIDARG);
3891
3892 /* Enclose the state transition NotReady->InInit->Ready */
3893 AutoInitSpan autoInitSpan(this);
3894 AssertReturn(autoInitSpan.isOk(), E_FAIL);
3895
3896 unconst(mParent) = aParent;
3897
3898 /* Confirm a successful initialization */
3899 autoInitSpan.setSucceeded();
3900
3901 return S_OK;
3902}
3903
3904/**
3905 * Uninitializes the instance and sets the ready flag to FALSE.
3906 * Called either from FinalRelease() or by the parent when it gets destroyed.
3907 */
3908void VRDEServerInfo::uninit()
3909{
3910 LogFlowThisFunc(("\n"));
3911
3912 /* Enclose the state transition Ready->InUninit->NotReady */
3913 AutoUninitSpan autoUninitSpan(this);
3914 if (autoUninitSpan.uninitDone())
3915 return;
3916
3917 unconst(mParent) = NULL;
3918}
3919
3920// IVRDEServerInfo properties
3921/////////////////////////////////////////////////////////////////////////////
3922
3923#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
3924 HRESULT VRDEServerInfo::get##_aName(_aType *a##_aName) \
3925 { \
3926 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3927 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3928 \
3929 uint32_t value; \
3930 uint32_t cbOut = 0; \
3931 \
3932 mParent->i_consoleVRDPServer()->QueryInfo \
3933 (_aIndex, &value, sizeof(value), &cbOut); \
3934 \
3935 *a##_aName = cbOut? !!value: FALSE; \
3936 \
3937 return S_OK; \
3938 } \
3939 extern void IMPL_GETTER_BOOL_DUMMY(void)
3940
3941#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
3942 HRESULT VRDEServerInfo::get##_aName(_aType *a##_aName) \
3943 { \
3944 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3945 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3946 \
3947 _aType value; \
3948 uint32_t cbOut = 0; \
3949 \
3950 mParent->i_consoleVRDPServer()->QueryInfo \
3951 (_aIndex, &value, sizeof(value), &cbOut); \
3952 \
3953 if (_aValueMask) value &= (_aValueMask); \
3954 *a##_aName = cbOut? value: 0; \
3955 \
3956 return S_OK; \
3957 } \
3958 extern void IMPL_GETTER_SCALAR_DUMMY(void)
3959
3960#define IMPL_GETTER_UTF8STR(_aType, _aName, _aIndex) \
3961 HRESULT VRDEServerInfo::get##_aName(_aType &a##_aName) \
3962 { \
3963 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3964 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3965 \
3966 uint32_t cbOut = 0; \
3967 \
3968 mParent->i_consoleVRDPServer()->QueryInfo \
3969 (_aIndex, NULL, 0, &cbOut); \
3970 \
3971 if (cbOut == 0) \
3972 { \
3973 a##_aName = Utf8Str::Empty; \
3974 return S_OK; \
3975 } \
3976 \
3977 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
3978 \
3979 if (!pchBuffer) \
3980 { \
3981 Log(("VRDEServerInfo::" \
3982 #_aName \
3983 ": Failed to allocate memory %d bytes\n", cbOut)); \
3984 return E_OUTOFMEMORY; \
3985 } \
3986 \
3987 mParent->i_consoleVRDPServer()->QueryInfo \
3988 (_aIndex, pchBuffer, cbOut, &cbOut); \
3989 \
3990 a##_aName = pchBuffer; \
3991 \
3992 RTMemTmpFree(pchBuffer); \
3993 \
3994 return S_OK; \
3995 } \
3996 extern void IMPL_GETTER_BSTR_DUMMY(void)
3997
3998IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
3999IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
4000IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
4001IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
4002IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
4003IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
4004IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
4005IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
4006IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
4007IMPL_GETTER_UTF8STR(Utf8Str, User, VRDE_QI_USER);
4008IMPL_GETTER_UTF8STR(Utf8Str, Domain, VRDE_QI_DOMAIN);
4009IMPL_GETTER_UTF8STR(Utf8Str, ClientName, VRDE_QI_CLIENT_NAME);
4010IMPL_GETTER_UTF8STR(Utf8Str, ClientIP, VRDE_QI_CLIENT_IP);
4011IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
4012IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
4013
4014#undef IMPL_GETTER_UTF8STR
4015#undef IMPL_GETTER_SCALAR
4016#undef IMPL_GETTER_BOOL
4017/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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