VirtualBox

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

Last change on this file since 47251 was 47251, checked in by vboxsync, 11 years ago

ConsoleVRDPServer: obsolete assert

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