VirtualBox

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

Last change on this file since 45674 was 45674, checked in by vboxsync, 12 years ago

Main/Machine+Console+Display+VRDEServer,VBoxManage: allow VMs without graphics controller, eliminate annoying spurious error messages about Console not yet powered up when taking screenshots, getting/setting guest properties and updating metrics, make as many parameters to modifyvm as possible case insensitive

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 131.0 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 45674 2013-04-23 08:45:54Z 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
930DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
931{
932 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
933
934 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
935
936 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
937 {
938 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
939 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
940
941 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
942 if (pPort)
943 {
944 pPort->pfnAudioInputIntercept(pPort, false);
945 }
946 else
947 {
948 AssertFailed();
949 }
950 }
951}
952
953DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
954{
955 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
956
957 LogFlowFunc(("%x\n", fu32Intercept));
958
959 int rc = VERR_NOT_SUPPORTED;
960
961 switch (fu32Intercept)
962 {
963 case VRDE_CLIENT_INTERCEPT_AUDIO:
964 {
965 server->mConsole->VRDPInterceptAudio(u32ClientId);
966 if (ppvIntercept)
967 {
968 *ppvIntercept = server;
969 }
970 rc = VINF_SUCCESS;
971 } break;
972
973 case VRDE_CLIENT_INTERCEPT_USB:
974 {
975 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
976 rc = VINF_SUCCESS;
977 } break;
978
979 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
980 {
981 server->mConsole->VRDPInterceptClipboard(u32ClientId);
982 if (ppvIntercept)
983 {
984 *ppvIntercept = server;
985 }
986 rc = VINF_SUCCESS;
987 } break;
988
989 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
990 {
991 /* This request is processed internally by the ConsoleVRDPServer.
992 * Only one client is allowed to intercept audio input.
993 */
994 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
995 {
996 Log(("AUDIOIN: connected client %u\n", u32ClientId));
997
998 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
999 if (pPort)
1000 {
1001 pPort->pfnAudioInputIntercept(pPort, true);
1002 if (ppvIntercept)
1003 {
1004 *ppvIntercept = server;
1005 }
1006 }
1007 else
1008 {
1009 AssertFailed();
1010 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
1011 rc = VERR_NOT_SUPPORTED;
1012 }
1013 }
1014 else
1015 {
1016 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
1017 rc = VERR_NOT_SUPPORTED;
1018 }
1019 } break;
1020
1021 default:
1022 break;
1023 }
1024
1025 return rc;
1026}
1027
1028DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
1029{
1030#ifdef VBOX_WITH_USB
1031 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
1032#else
1033 return VERR_NOT_SUPPORTED;
1034#endif
1035}
1036
1037DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
1038{
1039 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
1040}
1041
1042DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
1043{
1044 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1045
1046 bool fAvailable = false;
1047
1048 IFramebuffer *pfb = NULL;
1049 LONG xOrigin = 0;
1050 LONG yOrigin = 0;
1051
1052 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1053
1054 if (pfb)
1055 {
1056 pfb->Lock ();
1057
1058 /* Query framebuffer parameters. */
1059 ULONG lineSize = 0;
1060 pfb->COMGETTER(BytesPerLine)(&lineSize);
1061
1062 ULONG bitsPerPixel = 0;
1063 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1064
1065 BYTE *address = NULL;
1066 pfb->COMGETTER(Address)(&address);
1067
1068 ULONG height = 0;
1069 pfb->COMGETTER(Height)(&height);
1070
1071 ULONG width = 0;
1072 pfb->COMGETTER(Width)(&width);
1073
1074 /* Now fill the information as requested by the caller. */
1075 pInfo->pu8Bits = address;
1076 pInfo->xOrigin = xOrigin;
1077 pInfo->yOrigin = yOrigin;
1078 pInfo->cWidth = width;
1079 pInfo->cHeight = height;
1080 pInfo->cBitsPerPixel = bitsPerPixel;
1081 pInfo->cbLine = lineSize;
1082
1083 pfb->Unlock();
1084
1085 fAvailable = true;
1086 }
1087
1088 if (server->maFramebuffers[uScreenId])
1089 {
1090 server->maFramebuffers[uScreenId]->Release();
1091 }
1092 server->maFramebuffers[uScreenId] = pfb;
1093
1094 return fAvailable;
1095}
1096
1097DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1098{
1099 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1100
1101 if (server->maFramebuffers[uScreenId])
1102 {
1103 server->maFramebuffers[uScreenId]->Lock();
1104 }
1105}
1106
1107DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1108{
1109 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1110
1111 if (server->maFramebuffers[uScreenId])
1112 {
1113 server->maFramebuffers[uScreenId]->Unlock();
1114 }
1115}
1116
1117static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1118{
1119 if ( pInputSynch->cGuestNumLockAdaptions
1120 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1121 {
1122 pInputSynch->cGuestNumLockAdaptions--;
1123 pKeyboard->PutScancode(0x45);
1124 pKeyboard->PutScancode(0x45 | 0x80);
1125 }
1126 if ( pInputSynch->cGuestCapsLockAdaptions
1127 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1128 {
1129 pInputSynch->cGuestCapsLockAdaptions--;
1130 pKeyboard->PutScancode(0x3a);
1131 pKeyboard->PutScancode(0x3a | 0x80);
1132 }
1133}
1134
1135DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1136{
1137 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1138 Console *pConsole = server->mConsole;
1139
1140 switch (type)
1141 {
1142 case VRDE_INPUT_SCANCODE:
1143 {
1144 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1145 {
1146 IKeyboard *pKeyboard = pConsole->getKeyboard();
1147
1148 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1149
1150 /* Track lock keys. */
1151 if (pInputScancode->uScancode == 0x45)
1152 {
1153 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1154 }
1155 else if (pInputScancode->uScancode == 0x3a)
1156 {
1157 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1158 }
1159 else if (pInputScancode->uScancode == 0x46)
1160 {
1161 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1162 }
1163 else if ((pInputScancode->uScancode & 0x80) == 0)
1164 {
1165 /* Key pressed. */
1166 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1167 }
1168
1169 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1170 }
1171 } break;
1172
1173 case VRDE_INPUT_POINT:
1174 {
1175 if (cbInput == sizeof(VRDEINPUTPOINT))
1176 {
1177 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1178
1179 int mouseButtons = 0;
1180 int iWheel = 0;
1181
1182 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1183 {
1184 mouseButtons |= MouseButtonState_LeftButton;
1185 }
1186 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1187 {
1188 mouseButtons |= MouseButtonState_RightButton;
1189 }
1190 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1191 {
1192 mouseButtons |= MouseButtonState_MiddleButton;
1193 }
1194 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1195 {
1196 mouseButtons |= MouseButtonState_WheelUp;
1197 iWheel = -1;
1198 }
1199 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1200 {
1201 mouseButtons |= MouseButtonState_WheelDown;
1202 iWheel = 1;
1203 }
1204
1205 if (server->m_fGuestWantsAbsolute)
1206 {
1207 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1208 } else
1209 {
1210 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1211 pInputPoint->y - server->m_mousey,
1212 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1213 server->m_mousex = pInputPoint->x;
1214 server->m_mousey = pInputPoint->y;
1215 }
1216 }
1217 } break;
1218
1219 case VRDE_INPUT_CAD:
1220 {
1221 pConsole->getKeyboard()->PutCAD();
1222 } break;
1223
1224 case VRDE_INPUT_RESET:
1225 {
1226 pConsole->Reset();
1227 } break;
1228
1229 case VRDE_INPUT_SYNCH:
1230 {
1231 if (cbInput == sizeof(VRDEINPUTSYNCH))
1232 {
1233 IKeyboard *pKeyboard = pConsole->getKeyboard();
1234
1235 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1236
1237 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1238 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1239 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1240
1241 /* The client initiated synchronization. Always make the guest to reflect the client state.
1242 * Than means, when the guest changes the state itself, it is forced to return to the client
1243 * state.
1244 */
1245 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1246 {
1247 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1248 }
1249
1250 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1251 {
1252 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1253 }
1254
1255 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1256 }
1257 } break;
1258
1259 default:
1260 break;
1261 }
1262}
1263
1264DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1265{
1266 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1267
1268 server->mConsole->getDisplay()->SetVideoModeHint(uScreenId, TRUE /*=enabled*/,
1269 FALSE /*=changeOrigin*/, 0/*=OriginX*/, 0/*=OriginY*/,
1270 cWidth, cHeight, cBitsPerPixel);
1271}
1272
1273DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1274 void *pvCtx,
1275 uint32_t u32ClientId,
1276 uint32_t u32Event,
1277 const void *pvData,
1278 uint32_t cbData)
1279{
1280 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1281
1282 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1283
1284 switch (u32Event)
1285 {
1286 case VRDE_AUDIOIN_BEGIN:
1287 {
1288 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1289
1290 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1291 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1292 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1293 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1294 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1295 );
1296 } break;
1297
1298 case VRDE_AUDIOIN_DATA:
1299 {
1300 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1301 } break;
1302
1303 case VRDE_AUDIOIN_END:
1304 {
1305 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1306 } break;
1307
1308 default:
1309 return;
1310 }
1311}
1312
1313
1314ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1315{
1316 mConsole = console;
1317
1318 int rc = RTCritSectInit(&mCritSect);
1319 AssertRC(rc);
1320
1321 mcClipboardRefs = 0;
1322 mpfnClipboardCallback = NULL;
1323
1324#ifdef VBOX_WITH_USB
1325 mUSBBackends.pHead = NULL;
1326 mUSBBackends.pTail = NULL;
1327
1328 mUSBBackends.thread = NIL_RTTHREAD;
1329 mUSBBackends.fThreadRunning = false;
1330 mUSBBackends.event = 0;
1331#endif
1332
1333 mhServer = 0;
1334 mServerInterfaceVersion = 0;
1335
1336 m_fGuestWantsAbsolute = false;
1337 m_mousex = 0;
1338 m_mousey = 0;
1339
1340 m_InputSynch.cGuestNumLockAdaptions = 2;
1341 m_InputSynch.cGuestCapsLockAdaptions = 2;
1342
1343 m_InputSynch.fGuestNumLock = false;
1344 m_InputSynch.fGuestCapsLock = false;
1345 m_InputSynch.fGuestScrollLock = false;
1346
1347 m_InputSynch.fClientNumLock = false;
1348 m_InputSynch.fClientCapsLock = false;
1349 m_InputSynch.fClientScrollLock = false;
1350
1351 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1352
1353 {
1354 ComPtr<IEventSource> es;
1355 console->COMGETTER(EventSource)(es.asOutParam());
1356 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1357 aConsoleListener.createObject();
1358 aConsoleListener->init(new VRDPConsoleListener(), this);
1359 mConsoleListener = aConsoleListener;
1360 com::SafeArray <VBoxEventType_T> eventTypes;
1361 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1362 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1363 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1364 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1365 }
1366
1367 mVRDPBindPort = -1;
1368
1369 mAuthLibrary = 0;
1370
1371 mu32AudioInputClientId = 0;
1372
1373 /*
1374 * Optional interfaces.
1375 */
1376 m_fInterfaceImage = false;
1377 memset(&m_interfaceImage, 0, sizeof (m_interfaceImage));
1378 memset(&m_interfaceCallbacksImage, 0, sizeof (m_interfaceCallbacksImage));
1379 RT_ZERO(m_interfaceMousePtr);
1380 RT_ZERO(m_interfaceSCard);
1381 RT_ZERO(m_interfaceCallbacksSCard);
1382 RT_ZERO(m_interfaceTSMF);
1383 RT_ZERO(m_interfaceCallbacksTSMF);
1384 RT_ZERO(m_interfaceVideoIn);
1385 RT_ZERO(m_interfaceCallbacksVideoIn);
1386
1387 rc = RTCritSectInit(&mTSMFLock);
1388 AssertRC(rc);
1389}
1390
1391ConsoleVRDPServer::~ConsoleVRDPServer()
1392{
1393 Stop();
1394
1395 if (mConsoleListener)
1396 {
1397 ComPtr<IEventSource> es;
1398 mConsole->COMGETTER(EventSource)(es.asOutParam());
1399 es->UnregisterListener(mConsoleListener);
1400 mConsoleListener.setNull();
1401 }
1402
1403 unsigned i;
1404 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1405 {
1406 if (maFramebuffers[i])
1407 {
1408 maFramebuffers[i]->Release();
1409 maFramebuffers[i] = NULL;
1410 }
1411 }
1412
1413 if (RTCritSectIsInitialized(&mCritSect))
1414 {
1415 RTCritSectDelete(&mCritSect);
1416 memset(&mCritSect, 0, sizeof(mCritSect));
1417 }
1418
1419 if (RTCritSectIsInitialized(&mTSMFLock))
1420 {
1421 RTCritSectDelete(&mTSMFLock);
1422 memset(&mTSMFLock, 0, sizeof(mTSMFLock));
1423 }
1424}
1425
1426int ConsoleVRDPServer::Launch(void)
1427{
1428 LogFlowThisFunc(("\n"));
1429
1430 IVRDEServer *server = mConsole->getVRDEServer();
1431 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1432
1433 /*
1434 * Check if the prerequisites for VRDE are present.
1435 */
1436 BOOL fPrerequisites;
1437 HRESULT hrc = server->COMGETTER(CheckPrerequisites)(&fPrerequisites);
1438 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1439 if (!fPrerequisites)
1440 {
1441 LogRel(("VRDE: prerequisite missing, skipped start\n"));
1442 return VINF_SUCCESS;
1443 }
1444
1445 /*
1446 * Check if VRDE is enabled.
1447 */
1448 BOOL fEnabled;
1449 hrc = server->COMGETTER(Enabled)(&fEnabled);
1450 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1451 if (!fEnabled)
1452 return VINF_SUCCESS;
1453
1454 /*
1455 * Check that a VRDE extension pack name is set and resolve it into a
1456 * library path.
1457 */
1458 Bstr bstrExtPack;
1459 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1460 if (FAILED(hrc))
1461 return Global::vboxStatusCodeFromCOM(hrc);
1462 if (bstrExtPack.isEmpty())
1463 return VINF_NOT_SUPPORTED;
1464
1465 Utf8Str strExtPack(bstrExtPack);
1466 Utf8Str strVrdeLibrary;
1467 int vrc = VINF_SUCCESS;
1468 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1469 strVrdeLibrary = "VBoxVRDP";
1470 else
1471 {
1472#ifdef VBOX_WITH_EXTPACK
1473 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1474 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1475#else
1476 vrc = VERR_FILE_NOT_FOUND;
1477#endif
1478 }
1479 if (RT_SUCCESS(vrc))
1480 {
1481 /*
1482 * Load the VRDE library and start the server, if it is enabled.
1483 */
1484 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1485 if (RT_SUCCESS(vrc))
1486 {
1487 VRDEENTRYPOINTS_4 *pEntryPoints4;
1488 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints4, &mhServer);
1489
1490 if (RT_SUCCESS(vrc))
1491 {
1492 mServerInterfaceVersion = 4;
1493 mEntryPoints = *pEntryPoints4;
1494 mpEntryPoints = &mEntryPoints;
1495 }
1496 else if (vrc == VERR_VERSION_MISMATCH)
1497 {
1498 /* An older version of VRDE is installed, try version 3. */
1499 VRDEENTRYPOINTS_3 *pEntryPoints3;
1500
1501 static VRDECALLBACKS_3 sCallbacks3 =
1502 {
1503 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
1504 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1505 ConsoleVRDPServer::VRDPCallbackClientLogon,
1506 ConsoleVRDPServer::VRDPCallbackClientConnect,
1507 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1508 ConsoleVRDPServer::VRDPCallbackIntercept,
1509 ConsoleVRDPServer::VRDPCallbackUSB,
1510 ConsoleVRDPServer::VRDPCallbackClipboard,
1511 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1512 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1513 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1514 ConsoleVRDPServer::VRDPCallbackInput,
1515 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
1516 ConsoleVRDPServer::VRDECallbackAudioIn
1517 };
1518
1519 vrc = mpfnVRDECreateServer(&sCallbacks3.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1520 if (RT_SUCCESS(vrc))
1521 {
1522 mServerInterfaceVersion = 3;
1523 mEntryPoints.header = pEntryPoints3->header;
1524 mEntryPoints.VRDEDestroy = pEntryPoints3->VRDEDestroy;
1525 mEntryPoints.VRDEEnableConnections = pEntryPoints3->VRDEEnableConnections;
1526 mEntryPoints.VRDEDisconnect = pEntryPoints3->VRDEDisconnect;
1527 mEntryPoints.VRDEResize = pEntryPoints3->VRDEResize;
1528 mEntryPoints.VRDEUpdate = pEntryPoints3->VRDEUpdate;
1529 mEntryPoints.VRDEColorPointer = pEntryPoints3->VRDEColorPointer;
1530 mEntryPoints.VRDEHidePointer = pEntryPoints3->VRDEHidePointer;
1531 mEntryPoints.VRDEAudioSamples = pEntryPoints3->VRDEAudioSamples;
1532 mEntryPoints.VRDEAudioVolume = pEntryPoints3->VRDEAudioVolume;
1533 mEntryPoints.VRDEUSBRequest = pEntryPoints3->VRDEUSBRequest;
1534 mEntryPoints.VRDEClipboard = pEntryPoints3->VRDEClipboard;
1535 mEntryPoints.VRDEQueryInfo = pEntryPoints3->VRDEQueryInfo;
1536 mEntryPoints.VRDERedirect = pEntryPoints3->VRDERedirect;
1537 mEntryPoints.VRDEAudioInOpen = pEntryPoints3->VRDEAudioInOpen;
1538 mEntryPoints.VRDEAudioInClose = pEntryPoints3->VRDEAudioInClose;
1539 mEntryPoints.VRDEGetInterface = NULL;
1540 mpEntryPoints = &mEntryPoints;
1541 }
1542 else if (vrc == VERR_VERSION_MISMATCH)
1543 {
1544 /* An older version of VRDE is installed, try version 1. */
1545 VRDEENTRYPOINTS_1 *pEntryPoints1;
1546
1547 static VRDECALLBACKS_1 sCallbacks1 =
1548 {
1549 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1550 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1551 ConsoleVRDPServer::VRDPCallbackClientLogon,
1552 ConsoleVRDPServer::VRDPCallbackClientConnect,
1553 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1554 ConsoleVRDPServer::VRDPCallbackIntercept,
1555 ConsoleVRDPServer::VRDPCallbackUSB,
1556 ConsoleVRDPServer::VRDPCallbackClipboard,
1557 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1558 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1559 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1560 ConsoleVRDPServer::VRDPCallbackInput,
1561 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1562 };
1563
1564 vrc = mpfnVRDECreateServer(&sCallbacks1.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1565 if (RT_SUCCESS(vrc))
1566 {
1567 mServerInterfaceVersion = 1;
1568 mEntryPoints.header = pEntryPoints1->header;
1569 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1570 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1571 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1572 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1573 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1574 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1575 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1576 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1577 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1578 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1579 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1580 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1581 mEntryPoints.VRDERedirect = NULL;
1582 mEntryPoints.VRDEAudioInOpen = NULL;
1583 mEntryPoints.VRDEAudioInClose = NULL;
1584 mEntryPoints.VRDEGetInterface = NULL;
1585 mpEntryPoints = &mEntryPoints;
1586 }
1587 }
1588 }
1589
1590 if (RT_SUCCESS(vrc))
1591 {
1592 LogRel(("VRDE: loaded version %d of the server.\n", mServerInterfaceVersion));
1593
1594 if (mServerInterfaceVersion >= 4)
1595 {
1596 /* The server supports optional interfaces. */
1597 Assert(mpEntryPoints->VRDEGetInterface != NULL);
1598
1599 /* Image interface. */
1600 m_interfaceImage.header.u64Version = 1;
1601 m_interfaceImage.header.u64Size = sizeof(m_interfaceImage);
1602
1603 m_interfaceCallbacksImage.header.u64Version = 1;
1604 m_interfaceCallbacksImage.header.u64Size = sizeof(m_interfaceCallbacksImage);
1605 m_interfaceCallbacksImage.VRDEImageCbNotify = VRDEImageCbNotify;
1606
1607 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1608 VRDE_IMAGE_INTERFACE_NAME,
1609 &m_interfaceImage.header,
1610 &m_interfaceCallbacksImage.header,
1611 this);
1612 if (RT_SUCCESS(vrc))
1613 {
1614 LogRel(("VRDE: [%s]\n", VRDE_IMAGE_INTERFACE_NAME));
1615 m_fInterfaceImage = true;
1616 }
1617
1618 /* Mouse pointer interface. */
1619 m_interfaceMousePtr.header.u64Version = 1;
1620 m_interfaceMousePtr.header.u64Size = sizeof(m_interfaceMousePtr);
1621
1622 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1623 VRDE_MOUSEPTR_INTERFACE_NAME,
1624 &m_interfaceMousePtr.header,
1625 NULL,
1626 this);
1627 if (RT_SUCCESS(vrc))
1628 {
1629 LogRel(("VRDE: [%s]\n", VRDE_MOUSEPTR_INTERFACE_NAME));
1630 }
1631 else
1632 {
1633 RT_ZERO(m_interfaceMousePtr);
1634 }
1635
1636 /* Smartcard interface. */
1637 m_interfaceSCard.header.u64Version = 1;
1638 m_interfaceSCard.header.u64Size = sizeof(m_interfaceSCard);
1639
1640 m_interfaceCallbacksSCard.header.u64Version = 1;
1641 m_interfaceCallbacksSCard.header.u64Size = sizeof(m_interfaceCallbacksSCard);
1642 m_interfaceCallbacksSCard.VRDESCardCbNotify = VRDESCardCbNotify;
1643 m_interfaceCallbacksSCard.VRDESCardCbResponse = VRDESCardCbResponse;
1644
1645 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1646 VRDE_SCARD_INTERFACE_NAME,
1647 &m_interfaceSCard.header,
1648 &m_interfaceCallbacksSCard.header,
1649 this);
1650 if (RT_SUCCESS(vrc))
1651 {
1652 LogRel(("VRDE: [%s]\n", VRDE_SCARD_INTERFACE_NAME));
1653 }
1654 else
1655 {
1656 RT_ZERO(m_interfaceSCard);
1657 }
1658
1659 /* Raw TSMF interface. */
1660 m_interfaceTSMF.header.u64Version = 1;
1661 m_interfaceTSMF.header.u64Size = sizeof(m_interfaceTSMF);
1662
1663 m_interfaceCallbacksTSMF.header.u64Version = 1;
1664 m_interfaceCallbacksTSMF.header.u64Size = sizeof(m_interfaceCallbacksTSMF);
1665 m_interfaceCallbacksTSMF.VRDETSMFCbNotify = VRDETSMFCbNotify;
1666
1667 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1668 VRDE_TSMF_INTERFACE_NAME,
1669 &m_interfaceTSMF.header,
1670 &m_interfaceCallbacksTSMF.header,
1671 this);
1672 if (RT_SUCCESS(vrc))
1673 {
1674 LogRel(("VRDE: [%s]\n", VRDE_TSMF_INTERFACE_NAME));
1675 }
1676 else
1677 {
1678 RT_ZERO(m_interfaceTSMF);
1679 }
1680
1681 /* VideoIn interface. */
1682 m_interfaceVideoIn.header.u64Version = 1;
1683 m_interfaceVideoIn.header.u64Size = sizeof(m_interfaceVideoIn);
1684
1685 m_interfaceCallbacksVideoIn.header.u64Version = 1;
1686 m_interfaceCallbacksVideoIn.header.u64Size = sizeof(m_interfaceCallbacksVideoIn);
1687 m_interfaceCallbacksVideoIn.VRDECallbackVideoInNotify = VRDECallbackVideoInNotify;
1688 m_interfaceCallbacksVideoIn.VRDECallbackVideoInDeviceDesc = VRDECallbackVideoInDeviceDesc;
1689 m_interfaceCallbacksVideoIn.VRDECallbackVideoInControl = VRDECallbackVideoInControl;
1690 m_interfaceCallbacksVideoIn.VRDECallbackVideoInFrame = VRDECallbackVideoInFrame;
1691
1692 vrc = mpEntryPoints->VRDEGetInterface(mhServer,
1693 VRDE_VIDEOIN_INTERFACE_NAME,
1694 &m_interfaceVideoIn.header,
1695 &m_interfaceCallbacksVideoIn.header,
1696 this);
1697 if (RT_SUCCESS(vrc))
1698 {
1699 LogRel(("VRDE: [%s]\n", VRDE_VIDEOIN_INTERFACE_NAME));
1700 }
1701 else
1702 {
1703 RT_ZERO(m_interfaceVideoIn);
1704 }
1705
1706 /* Since these interfaces are optional, it is always a success here. */
1707 vrc = VINF_SUCCESS;
1708 }
1709#ifdef VBOX_WITH_USB
1710 remoteUSBThreadStart();
1711#endif
1712 }
1713 else
1714 {
1715 if (vrc != VERR_NET_ADDRESS_IN_USE)
1716 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1717 /* Don't unload the lib, because it prevents us trying again or
1718 because there may be other users? */
1719 }
1720 }
1721 }
1722
1723 return vrc;
1724}
1725
1726typedef struct H3DORInstance
1727{
1728 ConsoleVRDPServer *pThis;
1729 HVRDEIMAGE hImageBitmap;
1730 int32_t x;
1731 int32_t y;
1732 uint32_t w;
1733 uint32_t h;
1734 bool fCreated;
1735} H3DORInstance;
1736
1737/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORBegin(const void *pvContext, void **ppvInstance,
1738 const char *pszFormat)
1739{
1740 LogFlowFunc(("ctx %p\n", pvContext));
1741
1742 H3DORInstance *p = (H3DORInstance *)RTMemAlloc(sizeof (H3DORInstance));
1743
1744 if (p)
1745 {
1746 p->pThis = (ConsoleVRDPServer *)pvContext;
1747 p->hImageBitmap = NULL;
1748 p->x = 0;
1749 p->y = 0;
1750 p->w = 0;
1751 p->h = 0;
1752 p->fCreated = false;
1753
1754 /* Host 3D service passes the actual format of data in this redirect instance.
1755 * That is what will be in the H3DORFrame's parameters pvData and cbData.
1756 */
1757 if (RTStrICmp(pszFormat, H3DOR_FMT_RGBA_TOPDOWN) == 0)
1758 {
1759 /* Accept it. */
1760 }
1761 else
1762 {
1763 RTMemFree(p);
1764 p = NULL;
1765 }
1766 }
1767
1768 /* Caller check this for NULL. */
1769 *ppvInstance = p;
1770}
1771
1772/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORGeometry(void *pvInstance,
1773 int32_t x, int32_t y, uint32_t w, uint32_t h)
1774{
1775 LogFlowFunc(("ins %p %d,%d %dx%d\n", pvInstance, x, y, w, h));
1776
1777 H3DORInstance *p = (H3DORInstance *)pvInstance;
1778 Assert(p);
1779 Assert(p->pThis);
1780
1781 /* @todo find out what to do if size changes to 0x0 from non zero */
1782 if (w == 0 || h == 0)
1783 {
1784 /* Do nothing. */
1785 return;
1786 }
1787
1788 RTRECT rect;
1789 rect.xLeft = x;
1790 rect.yTop = y;
1791 rect.xRight = x + w;
1792 rect.yBottom = y + h;
1793
1794 if (p->hImageBitmap)
1795 {
1796 /* An image handle has been already created,
1797 * check if it has the same size as the reported geometry.
1798 */
1799 if ( p->x == x
1800 && p->y == y
1801 && p->w == w
1802 && p->h == h)
1803 {
1804 LogFlowFunc(("geometry not changed\n"));
1805 /* Do nothing. Continue using the existing handle. */
1806 }
1807 else
1808 {
1809 int rc = p->pThis->m_interfaceImage.VRDEImageGeometrySet(p->hImageBitmap, &rect);
1810 if (RT_SUCCESS(rc))
1811 {
1812 p->x = x;
1813 p->y = y;
1814 p->w = w;
1815 p->h = h;
1816 }
1817 else
1818 {
1819 /* The handle must be recreated. Delete existing handle here. */
1820 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1821 p->hImageBitmap = NULL;
1822 }
1823 }
1824 }
1825
1826 if (!p->hImageBitmap)
1827 {
1828 /* Create a new bitmap handle. */
1829 uint32_t u32ScreenId = 0; /* @todo clip to corresponding screens.
1830 * Clipping can be done here or in VRDP server.
1831 * If VRDP does clipping, then uScreenId parameter
1832 * is not necessary and coords must be global.
1833 * (have to check which coords are used in opengl service).
1834 * Since all VRDE API uses a ScreenId,
1835 * the clipping must be done here in ConsoleVRDPServer
1836 */
1837 uint32_t fu32CompletionFlags = 0;
1838 int rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1839 &p->hImageBitmap,
1840 p,
1841 u32ScreenId,
1842 VRDE_IMAGE_F_CREATE_CONTENT_3D
1843 | VRDE_IMAGE_F_CREATE_WINDOW,
1844 &rect,
1845 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1846 NULL,
1847 0,
1848 &fu32CompletionFlags);
1849 if (RT_FAILURE(rc))
1850 {
1851 /* No support for a 3D + WINDOW. Try bitmap updates. */
1852 fu32CompletionFlags = 0;
1853 rc = p->pThis->m_interfaceImage.VRDEImageHandleCreate(p->pThis->mhServer,
1854 &p->hImageBitmap,
1855 p,
1856 u32ScreenId,
1857 0,
1858 &rect,
1859 VRDE_IMAGE_FMT_ID_BITMAP_BGRA8,
1860 NULL,
1861 0,
1862 &fu32CompletionFlags);
1863 }
1864
1865 if (RT_SUCCESS(rc))
1866 {
1867 p->x = x;
1868 p->y = y;
1869 p->w = w;
1870 p->h = h;
1871
1872 if ((fu32CompletionFlags & VRDE_IMAGE_F_COMPLETE_ASYNC) == 0)
1873 {
1874 p->fCreated = true;
1875 }
1876 }
1877 else
1878 {
1879 p->hImageBitmap = NULL;
1880 p->w = 0;
1881 p->h = 0;
1882 }
1883 }
1884}
1885
1886/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORVisibleRegion(void *pvInstance,
1887 uint32_t cRects, const RTRECT *paRects)
1888{
1889 LogFlowFunc(("ins %p %d\n", pvInstance, cRects));
1890
1891 H3DORInstance *p = (H3DORInstance *)pvInstance;
1892 Assert(p);
1893 Assert(p->pThis);
1894
1895 if (cRects == 0)
1896 {
1897 /* Complete image is visible. */
1898 RTRECT rect;
1899 rect.xLeft = p->x;
1900 rect.yTop = p->y;
1901 rect.xRight = p->x + p->w;
1902 rect.yBottom = p->y + p->h;
1903 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1904 1,
1905 &rect);
1906 }
1907 else
1908 {
1909 p->pThis->m_interfaceImage.VRDEImageRegionSet (p->hImageBitmap,
1910 cRects,
1911 paRects);
1912 }
1913}
1914
1915/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DORFrame(void *pvInstance,
1916 void *pvData, uint32_t cbData)
1917{
1918 LogFlowFunc(("ins %p %p %d\n", pvInstance, pvData, cbData));
1919
1920 H3DORInstance *p = (H3DORInstance *)pvInstance;
1921 Assert(p);
1922 Assert(p->pThis);
1923
1924 /* Currently only a topdown BGR0 bitmap format is supported. */
1925 VRDEIMAGEBITMAP image;
1926
1927 image.cWidth = p->w;
1928 image.cHeight = p->h;
1929 image.pvData = pvData;
1930 image.cbData = cbData;
1931 image.pvScanLine0 = (uint8_t *)pvData + (p->h - 1) * p->w * 4;
1932 image.iScanDelta = -4 * p->w;
1933
1934 p->pThis->m_interfaceImage.VRDEImageUpdate (p->hImageBitmap,
1935 p->x,
1936 p->y,
1937 p->w,
1938 p->h,
1939 &image,
1940 sizeof(VRDEIMAGEBITMAP));
1941}
1942
1943/* static */ DECLCALLBACK(void) ConsoleVRDPServer::H3DOREnd(void *pvInstance)
1944{
1945 LogFlowFunc(("ins %p\n", pvInstance));
1946
1947 H3DORInstance *p = (H3DORInstance *)pvInstance;
1948 Assert(p);
1949 Assert(p->pThis);
1950
1951 p->pThis->m_interfaceImage.VRDEImageHandleClose(p->hImageBitmap);
1952
1953 RTMemFree(p);
1954}
1955
1956/* static */ DECLCALLBACK(int) ConsoleVRDPServer::H3DORContextProperty(const void *pvContext, uint32_t index,
1957 void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
1958{
1959 int rc = VINF_SUCCESS;
1960
1961 if (index == H3DOR_PROP_FORMATS)
1962 {
1963 /* Return a comma separated list of supported formats. */
1964 static const char *pszSupportedFormats = H3DOR_FMT_RGBA_TOPDOWN;
1965 uint32_t cbOut = (uint32_t)strlen(pszSupportedFormats) + 1;
1966 if (cbOut <= cbBuffer)
1967 {
1968 memcpy(pvBuffer, pszSupportedFormats, cbOut);
1969 }
1970 else
1971 {
1972 rc = VERR_BUFFER_OVERFLOW;
1973 }
1974 *pcbOut = cbOut;
1975 }
1976 else
1977 {
1978 rc = VERR_NOT_SUPPORTED;
1979 }
1980
1981 return rc;
1982}
1983
1984void ConsoleVRDPServer::remote3DRedirect(void)
1985{
1986 if (!m_fInterfaceImage)
1987 {
1988 /* No redirect without corresponding interface. */
1989 return;
1990 }
1991
1992 /* Check if 3D redirection has been enabled. */
1993 com::Bstr bstr;
1994 HRESULT hrc = mConsole->getVRDEServer()->GetVRDEProperty(Bstr("H3DRedirect/Enabled").raw(), bstr.asOutParam());
1995
1996 if (hrc != S_OK)
1997 {
1998 bstr = "";
1999 }
2000
2001 com::Utf8Str value = bstr;
2002
2003 bool fEnabled = RTStrICmp(value.c_str(), "true") == 0
2004 || RTStrICmp(value.c_str(), "1") == 0;
2005
2006 if (!fEnabled)
2007 {
2008 return;
2009 }
2010
2011 /* Tell the host 3D service to redirect output using the ConsoleVRDPServer callbacks. */
2012 H3DOUTPUTREDIRECT outputRedirect =
2013 {
2014 this,
2015 H3DORBegin,
2016 H3DORGeometry,
2017 H3DORVisibleRegion,
2018 H3DORFrame,
2019 H3DOREnd,
2020 H3DORContextProperty
2021 };
2022
2023 VBOXHGCMSVCPARM parm;
2024
2025 parm.type = VBOX_HGCM_SVC_PARM_PTR;
2026 parm.u.pointer.addr = &outputRedirect;
2027 parm.u.pointer.size = sizeof(outputRedirect);
2028
2029 VMMDev *pVMMDev = mConsole->getVMMDev();
2030
2031 if (!pVMMDev)
2032 {
2033 AssertMsgFailed(("remote3DRedirect no vmmdev\n"));
2034 return;
2035 }
2036
2037 int rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL",
2038 SHCRGL_HOST_FN_SET_OUTPUT_REDIRECT,
2039 SHCRGL_CPARMS_SET_OUTPUT_REDIRECT,
2040 &parm);
2041
2042 if (!RT_SUCCESS(rc))
2043 {
2044 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2045 return;
2046 }
2047
2048 LogRel(("VRDE: Enabled 3D redirect.\n"));
2049
2050 return;
2051}
2052
2053/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDEImageCbNotify (void *pvContext,
2054 void *pvUser,
2055 HVRDEIMAGE hVideo,
2056 uint32_t u32Id,
2057 void *pvData,
2058 uint32_t cbData)
2059{
2060 LogFlowFunc(("pvContext %p, pvUser %p, hVideo %p, u32Id %u, pvData %p, cbData %d\n",
2061 pvContext, pvUser, hVideo, u32Id, pvData, cbData));
2062
2063 ConsoleVRDPServer *pServer = static_cast<ConsoleVRDPServer*>(pvContext);
2064 H3DORInstance *p = (H3DORInstance *)pvUser;
2065 Assert(p);
2066 Assert(p->pThis);
2067 Assert(p->pThis == pServer);
2068
2069 if (u32Id == VRDE_IMAGE_NOTIFY_HANDLE_CREATE)
2070 {
2071 if (cbData != sizeof(uint32_t))
2072 {
2073 AssertFailed();
2074 return VERR_INVALID_PARAMETER;
2075 }
2076
2077 uint32_t u32StreamId = *(uint32_t *)pvData;
2078 LogFlowFunc(("VRDE_IMAGE_NOTIFY_HANDLE_CREATE u32StreamId %d\n",
2079 u32StreamId));
2080
2081 if (u32StreamId != 0)
2082 {
2083 p->fCreated = true; // @todo not needed?
2084 }
2085 else
2086 {
2087 /* The stream has not been created. */
2088 }
2089 }
2090
2091 return VINF_SUCCESS;
2092}
2093
2094/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbNotify(void *pvContext,
2095 uint32_t u32Id,
2096 void *pvData,
2097 uint32_t cbData)
2098{
2099#ifdef VBOX_WITH_USB_CARDREADER
2100 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2101 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2102 return pReader->VRDENotify(u32Id, pvData, cbData);
2103#else
2104 NOREF(pvContext);
2105 NOREF(u32Id);
2106 NOREF(pvData);
2107 NOREF(cbData);
2108 return VERR_NOT_SUPPORTED;
2109#endif
2110}
2111
2112/* static */ DECLCALLBACK(int) ConsoleVRDPServer::VRDESCardCbResponse(void *pvContext,
2113 int rcRequest,
2114 void *pvUser,
2115 uint32_t u32Function,
2116 void *pvData,
2117 uint32_t cbData)
2118{
2119#ifdef VBOX_WITH_USB_CARDREADER
2120 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2121 UsbCardReader *pReader = pThis->mConsole->getUsbCardReader();
2122 return pReader->VRDEResponse(rcRequest, pvUser, u32Function, pvData, cbData);
2123#else
2124 NOREF(pvContext);
2125 NOREF(rcRequest);
2126 NOREF(pvUser);
2127 NOREF(u32Function);
2128 NOREF(pvData);
2129 NOREF(cbData);
2130 return VERR_NOT_SUPPORTED;
2131#endif
2132}
2133
2134int ConsoleVRDPServer::SCardRequest(void *pvUser, uint32_t u32Function, const void *pvData, uint32_t cbData)
2135{
2136 int rc = VINF_SUCCESS;
2137
2138 if (mhServer && mpEntryPoints && m_interfaceSCard.VRDESCardRequest)
2139 {
2140 rc = m_interfaceSCard.VRDESCardRequest(mhServer, pvUser, u32Function, pvData, cbData);
2141 }
2142 else
2143 {
2144 rc = VERR_NOT_SUPPORTED;
2145 }
2146
2147 return rc;
2148}
2149
2150
2151struct TSMFHOSTCHCTX;
2152struct TSMFVRDPCTX;
2153
2154typedef struct TSMFHOSTCHCTX
2155{
2156 ConsoleVRDPServer *pThis;
2157
2158 struct TSMFVRDPCTX *pVRDPCtx; /* NULL if no corresponding host channel context. */
2159
2160 void *pvDataReceived;
2161 uint32_t cbDataReceived;
2162 uint32_t cbDataAllocated;
2163} TSMFHOSTCHCTX;
2164
2165typedef struct TSMFVRDPCTX
2166{
2167 ConsoleVRDPServer *pThis;
2168
2169 VBOXHOSTCHANNELCALLBACKS *pCallbacks;
2170 void *pvCallbacks;
2171
2172 TSMFHOSTCHCTX *pHostChCtx; /* NULL if no corresponding host channel context. */
2173
2174 uint32_t u32ChannelHandle;
2175} TSMFVRDPCTX;
2176
2177static int tsmfContextsAlloc(TSMFHOSTCHCTX **ppHostChCtx, TSMFVRDPCTX **ppVRDPCtx)
2178{
2179 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)RTMemAllocZ(sizeof(TSMFHOSTCHCTX));
2180 if (!pHostChCtx)
2181 {
2182 return VERR_NO_MEMORY;
2183 }
2184
2185 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)RTMemAllocZ(sizeof(TSMFVRDPCTX));
2186 if (!pVRDPCtx)
2187 {
2188 RTMemFree(pHostChCtx);
2189 return VERR_NO_MEMORY;
2190 }
2191
2192 *ppHostChCtx = pHostChCtx;
2193 *ppVRDPCtx = pVRDPCtx;
2194 return VINF_SUCCESS;
2195}
2196
2197int ConsoleVRDPServer::tsmfLock(void)
2198{
2199 int rc = RTCritSectEnter(&mTSMFLock);
2200 AssertRC(rc);
2201 return rc;
2202}
2203
2204void ConsoleVRDPServer::tsmfUnlock(void)
2205{
2206 RTCritSectLeave(&mTSMFLock);
2207}
2208
2209/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelAttach(void *pvProvider,
2210 void **ppvChannel,
2211 uint32_t u32Flags,
2212 VBOXHOSTCHANNELCALLBACKS *pCallbacks,
2213 void *pvCallbacks)
2214{
2215 LogFlowFunc(("\n"));
2216
2217 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvProvider);
2218
2219 /* Create 2 context structures: for the VRDP server and for the host service. */
2220 TSMFHOSTCHCTX *pHostChCtx = NULL;
2221 TSMFVRDPCTX *pVRDPCtx = NULL;
2222
2223 int rc = tsmfContextsAlloc(&pHostChCtx, &pVRDPCtx);
2224 if (RT_FAILURE(rc))
2225 {
2226 return rc;
2227 }
2228
2229 pHostChCtx->pThis = pThis;
2230 pHostChCtx->pVRDPCtx = pVRDPCtx;
2231
2232 pVRDPCtx->pThis = pThis;
2233 pVRDPCtx->pCallbacks = pCallbacks;
2234 pVRDPCtx->pvCallbacks = pvCallbacks;
2235 pVRDPCtx->pHostChCtx = pHostChCtx;
2236
2237 rc = pThis->m_interfaceTSMF.VRDETSMFChannelCreate(pThis->mhServer, pVRDPCtx, u32Flags);
2238
2239 if (RT_SUCCESS(rc))
2240 {
2241 /* @todo contexts should be in a list for accounting. */
2242 *ppvChannel = pHostChCtx;
2243 }
2244 else
2245 {
2246 RTMemFree(pHostChCtx);
2247 RTMemFree(pVRDPCtx);
2248 }
2249
2250 return rc;
2251}
2252
2253/* static */ DECLCALLBACK(void) ConsoleVRDPServer::tsmfHostChannelDetach(void *pvChannel)
2254{
2255 LogFlowFunc(("\n"));
2256
2257 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2258 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2259
2260 int rc = pThis->tsmfLock();
2261 if (RT_SUCCESS(rc))
2262 {
2263 bool fClose = false;
2264 uint32_t u32ChannelHandle = 0;
2265
2266 if (pHostChCtx->pVRDPCtx)
2267 {
2268 /* There is still a VRDP context for this channel. */
2269 pHostChCtx->pVRDPCtx->pHostChCtx = NULL;
2270 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2271 fClose = true;
2272 }
2273
2274 pThis->tsmfUnlock();
2275
2276 RTMemFree(pHostChCtx);
2277
2278 if (fClose)
2279 {
2280 LogFlowFunc(("Closing VRDE channel %d.\n", u32ChannelHandle));
2281 pThis->m_interfaceTSMF.VRDETSMFChannelClose(pThis->mhServer, u32ChannelHandle);
2282 }
2283 else
2284 {
2285 LogFlowFunc(("No VRDE channel.\n"));
2286 }
2287 }
2288}
2289
2290/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelSend(void *pvChannel,
2291 const void *pvData,
2292 uint32_t cbData)
2293{
2294 LogFlowFunc(("cbData %d\n", cbData));
2295
2296 TSMFHOSTCHCTX *pHostChCtx = (TSMFHOSTCHCTX *)pvChannel;
2297 ConsoleVRDPServer *pThis = pHostChCtx->pThis;
2298
2299 int rc = pThis->tsmfLock();
2300 if (RT_SUCCESS(rc))
2301 {
2302 bool fSend = false;
2303 uint32_t u32ChannelHandle = 0;
2304
2305 if (pHostChCtx->pVRDPCtx)
2306 {
2307 u32ChannelHandle = pHostChCtx->pVRDPCtx->u32ChannelHandle;
2308 fSend = true;
2309 }
2310
2311 pThis->tsmfUnlock();
2312
2313 if (fSend)
2314 {
2315 LogFlowFunc(("Send to VRDE channel %d.\n", u32ChannelHandle));
2316 rc = pThis->m_interfaceTSMF.VRDETSMFChannelSend(pThis->mhServer, u32ChannelHandle,
2317 pvData, cbData);
2318 }
2319 }
2320
2321 return rc;
2322}
2323
2324/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelRecv(void *pvChannel,
2325 void *pvData,
2326 uint32_t cbData,
2327 uint32_t *pcbReceived,
2328 uint32_t *pcbRemaining)
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 uint32_t cbToCopy = RT_MIN(cbData, pHostChCtx->cbDataReceived);
2339 uint32_t cbRemaining = pHostChCtx->cbDataReceived - cbToCopy;
2340
2341 LogFlowFunc(("cbToCopy %d, cbRemaining %d\n", cbToCopy, cbRemaining));
2342
2343 if (cbToCopy != 0)
2344 {
2345 memcpy(pvData, pHostChCtx->pvDataReceived, cbToCopy);
2346
2347 if (cbRemaining != 0)
2348 {
2349 memmove(pHostChCtx->pvDataReceived, (uint8_t *)pHostChCtx->pvDataReceived + cbToCopy, cbRemaining);
2350 }
2351
2352 pHostChCtx->cbDataReceived = cbRemaining;
2353 }
2354
2355 pThis->tsmfUnlock();
2356
2357 *pcbRemaining = cbRemaining;
2358 *pcbReceived = cbToCopy;
2359 }
2360
2361 return rc;
2362}
2363
2364/* static */ DECLCALLBACK(int) ConsoleVRDPServer::tsmfHostChannelControl(void *pvChannel,
2365 uint32_t u32Code,
2366 const void *pvParm,
2367 uint32_t cbParm,
2368 const void *pvData,
2369 uint32_t cbData,
2370 uint32_t *pcbDataReturned)
2371{
2372 LogFlowFunc(("u32Code %u\n", u32Code));
2373
2374 if (!pvChannel)
2375 {
2376 /* Special case, the provider must answer rather than a channel instance. */
2377 if (u32Code == VBOX_HOST_CHANNEL_CTRL_EXISTS)
2378 {
2379 *pcbDataReturned = 0;
2380 return VINF_SUCCESS;
2381 }
2382
2383 return VERR_NOT_IMPLEMENTED;
2384 }
2385
2386 /* Channels do not support this. */
2387 return VERR_NOT_IMPLEMENTED;
2388}
2389
2390
2391void ConsoleVRDPServer::setupTSMF(void)
2392{
2393 if (m_interfaceTSMF.header.u64Size == 0)
2394 {
2395 return;
2396 }
2397
2398 /* Register with the host channel service. */
2399 VBOXHOSTCHANNELINTERFACE hostChannelInterface =
2400 {
2401 this,
2402 tsmfHostChannelAttach,
2403 tsmfHostChannelDetach,
2404 tsmfHostChannelSend,
2405 tsmfHostChannelRecv,
2406 tsmfHostChannelControl
2407 };
2408
2409 VBoxHostChannelHostRegister parms;
2410
2411 static char szProviderName[] = "/vrde/tsmf";
2412
2413 parms.name.type = VBOX_HGCM_SVC_PARM_PTR;
2414 parms.name.u.pointer.addr = &szProviderName[0];
2415 parms.name.u.pointer.size = sizeof(szProviderName);
2416
2417 parms.iface.type = VBOX_HGCM_SVC_PARM_PTR;
2418 parms.iface.u.pointer.addr = &hostChannelInterface;
2419 parms.iface.u.pointer.size = sizeof(hostChannelInterface);
2420
2421 VMMDev *pVMMDev = mConsole->getVMMDev();
2422
2423 if (!pVMMDev)
2424 {
2425 AssertMsgFailed(("setupTSMF no vmmdev\n"));
2426 return;
2427 }
2428
2429 int rc = pVMMDev->hgcmHostCall("VBoxHostChannel",
2430 VBOX_HOST_CHANNEL_HOST_FN_REGISTER,
2431 2,
2432 &parms.name);
2433
2434 if (!RT_SUCCESS(rc))
2435 {
2436 Log(("VBOX_HOST_CHANNEL_HOST_FN_REGISTER failed with %Rrc\n", rc));
2437 return;
2438 }
2439
2440 LogRel(("VRDE: Enabled TSMF channel.\n"));
2441
2442 return;
2443}
2444
2445/* @todo these defines must be in a header, which is used by guest component as well. */
2446#define VBOX_TSMF_HCH_CREATE_ACCEPTED (VBOX_HOST_CHANNEL_EVENT_USER + 0)
2447#define VBOX_TSMF_HCH_CREATE_DECLINED (VBOX_HOST_CHANNEL_EVENT_USER + 1)
2448#define VBOX_TSMF_HCH_DISCONNECTED (VBOX_HOST_CHANNEL_EVENT_USER + 2)
2449
2450/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDETSMFCbNotify(void *pvContext,
2451 uint32_t u32Notification,
2452 void *pvChannel,
2453 const void *pvParm,
2454 uint32_t cbParm)
2455{
2456 int rc = VINF_SUCCESS;
2457
2458 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvContext);
2459
2460 TSMFVRDPCTX *pVRDPCtx = (TSMFVRDPCTX *)pvChannel;
2461
2462 Assert(pVRDPCtx->pThis == pThis);
2463
2464 if (pVRDPCtx->pCallbacks == NULL)
2465 {
2466 LogFlowFunc(("tsmfHostChannel: Channel disconnected. Skipping.\n"));
2467 return;
2468 }
2469
2470 switch (u32Notification)
2471 {
2472 case VRDE_TSMF_N_CREATE_ACCEPTED:
2473 {
2474 VRDETSMFNOTIFYCREATEACCEPTED *p = (VRDETSMFNOTIFYCREATEACCEPTED *)pvParm;
2475 Assert(cbParm == sizeof(VRDETSMFNOTIFYCREATEACCEPTED));
2476
2477 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_ACCEPTED(%p): p->u32ChannelHandle %d\n",
2478 pVRDPCtx, p->u32ChannelHandle));
2479
2480 pVRDPCtx->u32ChannelHandle = p->u32ChannelHandle;
2481
2482 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2483 VBOX_TSMF_HCH_CREATE_ACCEPTED,
2484 NULL, 0);
2485 } break;
2486
2487 case VRDE_TSMF_N_CREATE_DECLINED:
2488 {
2489 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_CREATE_DECLINED(%p)\n", pVRDPCtx));
2490
2491 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2492 VBOX_TSMF_HCH_CREATE_DECLINED,
2493 NULL, 0);
2494 } break;
2495
2496 case VRDE_TSMF_N_DATA:
2497 {
2498 /* Save the data in the intermediate buffer and send the event. */
2499 VRDETSMFNOTIFYDATA *p = (VRDETSMFNOTIFYDATA *)pvParm;
2500 Assert(cbParm == sizeof(VRDETSMFNOTIFYDATA));
2501
2502 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA(%p): p->cbData %d\n", pVRDPCtx, p->cbData));
2503
2504 VBOXHOSTCHANNELEVENTRECV ev;
2505 ev.u32SizeAvailable = 0;
2506
2507 rc = pThis->tsmfLock();
2508
2509 if (RT_SUCCESS(rc))
2510 {
2511 TSMFHOSTCHCTX *pHostChCtx = pVRDPCtx->pHostChCtx;
2512
2513 if (pHostChCtx)
2514 {
2515 if (pHostChCtx->pvDataReceived)
2516 {
2517 uint32_t cbAlloc = p->cbData + pHostChCtx->cbDataReceived;
2518 pHostChCtx->pvDataReceived = RTMemRealloc(pHostChCtx->pvDataReceived, cbAlloc);
2519 memcpy((uint8_t *)pHostChCtx->pvDataReceived + pHostChCtx->cbDataReceived, p->pvData, p->cbData);
2520
2521 pHostChCtx->cbDataReceived += p->cbData;
2522 pHostChCtx->cbDataAllocated = cbAlloc;
2523 }
2524 else
2525 {
2526 pHostChCtx->pvDataReceived = RTMemAlloc(p->cbData);
2527 memcpy(pHostChCtx->pvDataReceived, p->pvData, p->cbData);
2528
2529 pHostChCtx->cbDataReceived = p->cbData;
2530 pHostChCtx->cbDataAllocated = p->cbData;
2531 }
2532
2533 ev.u32SizeAvailable = p->cbData;
2534 }
2535 else
2536 {
2537 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DATA: no host channel. Skipping\n"));
2538 }
2539
2540 pThis->tsmfUnlock();
2541 }
2542
2543 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2544 VBOX_HOST_CHANNEL_EVENT_RECV,
2545 &ev, sizeof(ev));
2546 } break;
2547
2548 case VRDE_TSMF_N_DISCONNECTED:
2549 {
2550 LogFlowFunc(("tsmfHostChannel: VRDE_TSMF_N_DISCONNECTED(%p)\n", pVRDPCtx));
2551
2552 pVRDPCtx->pCallbacks->HostChannelCallbackEvent(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx,
2553 VBOX_TSMF_HCH_DISCONNECTED,
2554 NULL, 0);
2555
2556 /* The callback context will not be used anymore. */
2557 pVRDPCtx->pCallbacks->HostChannelCallbackDeleted(pVRDPCtx->pvCallbacks, pVRDPCtx->pHostChCtx);
2558 pVRDPCtx->pCallbacks = NULL;
2559 pVRDPCtx->pvCallbacks = NULL;
2560
2561 rc = pThis->tsmfLock();
2562 if (RT_SUCCESS(rc))
2563 {
2564 if (pVRDPCtx->pHostChCtx)
2565 {
2566 /* There is still a host channel context for this channel. */
2567 pVRDPCtx->pHostChCtx->pVRDPCtx = NULL;
2568 }
2569
2570 pThis->tsmfUnlock();
2571
2572 memset(pVRDPCtx, 0, sizeof(*pVRDPCtx));
2573 RTMemFree(pVRDPCtx);
2574 }
2575 } break;
2576
2577 default:
2578 {
2579 AssertFailed();
2580 } break;
2581 }
2582}
2583
2584/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInNotify(void *pvCallback,
2585 uint32_t u32Id,
2586 const void *pvData,
2587 uint32_t cbData)
2588{
2589#ifdef VBOX_WITH_USB_VIDEO
2590 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2591 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2592 pWebcam->EmWebcamCbNotify(u32Id, pvData, cbData);
2593#else
2594 NOREF(pvCallback);
2595 NOREF(u32Id);
2596 NOREF(pvData);
2597 NOREF(cbData);
2598#endif
2599}
2600
2601/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInDeviceDesc(void *pvCallback,
2602 int rcRequest,
2603 void *pDeviceCtx,
2604 void *pvUser,
2605 const VRDEVIDEOINDEVICEDESC *pDeviceDesc,
2606 uint32_t cbDevice)
2607{
2608#ifdef VBOX_WITH_USB_VIDEO
2609 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2610 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2611 pWebcam->EmWebcamCbDeviceDesc(rcRequest, pDeviceCtx, pvUser, pDeviceDesc, cbDevice);
2612#else
2613 NOREF(pvCallback);
2614 NOREF(rcRequest);
2615 NOREF(pDeviceCtx);
2616 NOREF(pvUser);
2617 NOREF(pDeviceDesc);
2618 NOREF(cbDevice);
2619#endif
2620}
2621
2622/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInControl(void *pvCallback,
2623 int rcRequest,
2624 void *pDeviceCtx,
2625 void *pvUser,
2626 const VRDEVIDEOINCTRLHDR *pControl,
2627 uint32_t cbControl)
2628{
2629#ifdef VBOX_WITH_USB_VIDEO
2630 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2631 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2632 pWebcam->EmWebcamCbControl(rcRequest, pDeviceCtx, pvUser, pControl, cbControl);
2633#else
2634 NOREF(pvCallback);
2635 NOREF(rcRequest);
2636 NOREF(pDeviceCtx);
2637 NOREF(pvUser);
2638 NOREF(pControl);
2639 NOREF(cbControl);
2640#endif
2641}
2642
2643/* static */ DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackVideoInFrame(void *pvCallback,
2644 int rcRequest,
2645 void *pDeviceCtx,
2646 const VRDEVIDEOINPAYLOADHDR *pFrame,
2647 uint32_t cbFrame)
2648{
2649#ifdef VBOX_WITH_USB_VIDEO
2650 ConsoleVRDPServer *pThis = static_cast<ConsoleVRDPServer*>(pvCallback);
2651 EmWebcam *pWebcam = pThis->mConsole->getEmWebcam();
2652 pWebcam->EmWebcamCbFrame(rcRequest, pDeviceCtx, pFrame, cbFrame);
2653#else
2654 NOREF(pvCallback);
2655 NOREF(rcRequest);
2656 NOREF(pDeviceCtx);
2657 NOREF(pFrame);
2658 NOREF(cbFrame);
2659#endif
2660}
2661
2662int ConsoleVRDPServer::VideoInDeviceAttach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle, void *pvDeviceCtx)
2663{
2664 int rc;
2665
2666 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceAttach)
2667 {
2668 rc = m_interfaceVideoIn.VRDEVideoInDeviceAttach(mhServer, pDeviceHandle, pvDeviceCtx);
2669 }
2670 else
2671 {
2672 rc = VERR_NOT_SUPPORTED;
2673 }
2674
2675 return rc;
2676}
2677
2678int ConsoleVRDPServer::VideoInDeviceDetach(const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2679{
2680 int rc;
2681
2682 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInDeviceDetach)
2683 {
2684 rc = m_interfaceVideoIn.VRDEVideoInDeviceDetach(mhServer, pDeviceHandle);
2685 }
2686 else
2687 {
2688 rc = VERR_NOT_SUPPORTED;
2689 }
2690
2691 return rc;
2692}
2693
2694int ConsoleVRDPServer::VideoInGetDeviceDesc(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle)
2695{
2696 int rc;
2697
2698 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInGetDeviceDesc)
2699 {
2700 rc = m_interfaceVideoIn.VRDEVideoInGetDeviceDesc(mhServer, pvUser, pDeviceHandle);
2701 }
2702 else
2703 {
2704 rc = VERR_NOT_SUPPORTED;
2705 }
2706
2707 return rc;
2708}
2709
2710int ConsoleVRDPServer::VideoInControl(void *pvUser, const VRDEVIDEOINDEVICEHANDLE *pDeviceHandle,
2711 const VRDEVIDEOINCTRLHDR *pReq, uint32_t cbReq)
2712{
2713 int rc;
2714
2715 if (mhServer && mpEntryPoints && m_interfaceVideoIn.VRDEVideoInControl)
2716 {
2717 rc = m_interfaceVideoIn.VRDEVideoInControl(mhServer, pvUser, pDeviceHandle, pReq, cbReq);
2718 }
2719 else
2720 {
2721 rc = VERR_NOT_SUPPORTED;
2722 }
2723
2724 return rc;
2725}
2726
2727void ConsoleVRDPServer::EnableConnections(void)
2728{
2729 if (mpEntryPoints && mhServer)
2730 {
2731 mpEntryPoints->VRDEEnableConnections(mhServer, true);
2732
2733 /* Redirect 3D output if it is enabled. */
2734 remote3DRedirect();
2735
2736 /* Setup the generic TSMF channel. */
2737 setupTSMF();
2738 }
2739}
2740
2741void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
2742{
2743 if (mpEntryPoints && mhServer)
2744 {
2745 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
2746 }
2747}
2748
2749int ConsoleVRDPServer::MousePointer(BOOL alpha,
2750 ULONG xHot,
2751 ULONG yHot,
2752 ULONG width,
2753 ULONG height,
2754 const uint8_t *pu8Shape)
2755{
2756 int rc = VINF_SUCCESS;
2757
2758 if (mhServer && mpEntryPoints && m_interfaceMousePtr.VRDEMousePtr)
2759 {
2760 size_t cbMask = (((width + 7) / 8) * height + 3) & ~3;
2761 size_t cbData = width * height * 4;
2762
2763 size_t cbDstMask = alpha? 0: cbMask;
2764
2765 size_t cbPointer = sizeof(VRDEMOUSEPTRDATA) + cbDstMask + cbData;
2766 uint8_t *pu8Pointer = (uint8_t *)RTMemAlloc(cbPointer);
2767 if (pu8Pointer != NULL)
2768 {
2769 VRDEMOUSEPTRDATA *pPointer = (VRDEMOUSEPTRDATA *)pu8Pointer;
2770
2771 pPointer->u16HotX = (uint16_t)xHot;
2772 pPointer->u16HotY = (uint16_t)yHot;
2773 pPointer->u16Width = (uint16_t)width;
2774 pPointer->u16Height = (uint16_t)height;
2775 pPointer->u16MaskLen = (uint16_t)cbDstMask;
2776 pPointer->u32DataLen = (uint32_t)cbData;
2777
2778 /* AND mask. */
2779 uint8_t *pu8Mask = pu8Pointer + sizeof(VRDEMOUSEPTRDATA);
2780 if (cbDstMask)
2781 {
2782 memcpy(pu8Mask, pu8Shape, cbDstMask);
2783 }
2784
2785 /* XOR mask */
2786 uint8_t *pu8Data = pu8Mask + pPointer->u16MaskLen;
2787 memcpy(pu8Data, pu8Shape + cbMask, cbData);
2788
2789 m_interfaceMousePtr.VRDEMousePtr(mhServer, pPointer);
2790
2791 RTMemFree(pu8Pointer);
2792 }
2793 else
2794 {
2795 rc = VERR_NO_MEMORY;
2796 }
2797 }
2798 else
2799 {
2800 rc = VERR_NOT_SUPPORTED;
2801 }
2802
2803 return rc;
2804}
2805
2806void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
2807{
2808 if (mpEntryPoints && mhServer)
2809 {
2810 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
2811 }
2812}
2813
2814void ConsoleVRDPServer::MousePointerHide(void)
2815{
2816 if (mpEntryPoints && mhServer)
2817 {
2818 mpEntryPoints->VRDEHidePointer(mhServer);
2819 }
2820}
2821
2822void ConsoleVRDPServer::Stop(void)
2823{
2824 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
2825 * linux. Just remove this when it's 100% sure that problem has been fixed. */
2826 if (mhServer)
2827 {
2828 HVRDESERVER hServer = mhServer;
2829
2830 /* Reset the handle to avoid further calls to the server. */
2831 mhServer = 0;
2832
2833 if (mpEntryPoints && hServer)
2834 {
2835 mpEntryPoints->VRDEDestroy(hServer);
2836 }
2837 }
2838
2839#ifdef VBOX_WITH_USB
2840 remoteUSBThreadStop();
2841#endif /* VBOX_WITH_USB */
2842
2843 mpfnAuthEntry = NULL;
2844 mpfnAuthEntry2 = NULL;
2845 mpfnAuthEntry3 = NULL;
2846
2847 if (mAuthLibrary)
2848 {
2849 RTLdrClose(mAuthLibrary);
2850 mAuthLibrary = 0;
2851 }
2852}
2853
2854/* Worker thread for Remote USB. The thread polls the clients for
2855 * the list of attached USB devices.
2856 * The thread is also responsible for attaching/detaching devices
2857 * to/from the VM.
2858 *
2859 * It is expected that attaching/detaching is not a frequent operation.
2860 *
2861 * The thread is always running when the VRDP server is active.
2862 *
2863 * The thread scans backends and requests the device list every 2 seconds.
2864 *
2865 * When device list is available, the thread calls the Console to process it.
2866 *
2867 */
2868#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
2869
2870#ifdef VBOX_WITH_USB
2871static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
2872{
2873 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
2874
2875 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
2876
2877 pOwner->notifyRemoteUSBThreadRunning(self);
2878
2879 while (pOwner->isRemoteUSBThreadRunning())
2880 {
2881 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2882
2883 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
2884 {
2885 pRemoteUSBBackend->PollRemoteDevices();
2886 }
2887
2888 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
2889
2890 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
2891 }
2892
2893 return VINF_SUCCESS;
2894}
2895
2896void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
2897{
2898 mUSBBackends.thread = thread;
2899 mUSBBackends.fThreadRunning = true;
2900 int rc = RTThreadUserSignal(thread);
2901 AssertRC(rc);
2902}
2903
2904bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
2905{
2906 return mUSBBackends.fThreadRunning;
2907}
2908
2909void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
2910{
2911 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
2912 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
2913 NOREF(rc);
2914}
2915
2916void ConsoleVRDPServer::remoteUSBThreadStart(void)
2917{
2918 int rc = RTSemEventCreate(&mUSBBackends.event);
2919
2920 if (RT_FAILURE(rc))
2921 {
2922 AssertFailed();
2923 mUSBBackends.event = 0;
2924 }
2925
2926 if (RT_SUCCESS(rc))
2927 {
2928 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
2929 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
2930 }
2931
2932 if (RT_FAILURE(rc))
2933 {
2934 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
2935 mUSBBackends.thread = NIL_RTTHREAD;
2936 }
2937 else
2938 {
2939 /* Wait until the thread is ready. */
2940 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
2941 AssertRC(rc);
2942 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
2943 }
2944}
2945
2946void ConsoleVRDPServer::remoteUSBThreadStop(void)
2947{
2948 mUSBBackends.fThreadRunning = false;
2949
2950 if (mUSBBackends.thread != NIL_RTTHREAD)
2951 {
2952 Assert (mUSBBackends.event != 0);
2953
2954 RTSemEventSignal(mUSBBackends.event);
2955
2956 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
2957 AssertRC(rc);
2958
2959 mUSBBackends.thread = NIL_RTTHREAD;
2960 }
2961
2962 if (mUSBBackends.event)
2963 {
2964 RTSemEventDestroy(mUSBBackends.event);
2965 mUSBBackends.event = 0;
2966 }
2967}
2968#endif /* VBOX_WITH_USB */
2969
2970typedef struct AuthCtx
2971{
2972 AuthResult result;
2973
2974 PAUTHENTRY3 pfnAuthEntry3;
2975 PAUTHENTRY2 pfnAuthEntry2;
2976 PAUTHENTRY pfnAuthEntry;
2977
2978 const char *pszCaller;
2979 PAUTHUUID pUuid;
2980 AuthGuestJudgement guestJudgement;
2981 const char *pszUser;
2982 const char *pszPassword;
2983 const char *pszDomain;
2984 int fLogon;
2985 unsigned clientId;
2986} AuthCtx;
2987
2988static DECLCALLBACK(int) authThread(RTTHREAD self, void *pvUser)
2989{
2990 AuthCtx *pCtx = (AuthCtx *)pvUser;
2991
2992 if (pCtx->pfnAuthEntry3)
2993 {
2994 pCtx->result = pCtx->pfnAuthEntry3(pCtx->pszCaller, pCtx->pUuid, pCtx->guestJudgement,
2995 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain,
2996 pCtx->fLogon, pCtx->clientId);
2997 }
2998 else if (pCtx->pfnAuthEntry2)
2999 {
3000 pCtx->result = pCtx->pfnAuthEntry2(pCtx->pUuid, pCtx->guestJudgement,
3001 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain,
3002 pCtx->fLogon, pCtx->clientId);
3003 }
3004 else if (pCtx->pfnAuthEntry)
3005 {
3006 pCtx->result = pCtx->pfnAuthEntry(pCtx->pUuid, pCtx->guestJudgement,
3007 pCtx->pszUser, pCtx->pszPassword, pCtx->pszDomain);
3008 }
3009 return VINF_SUCCESS;
3010}
3011
3012static AuthResult authCall(AuthCtx *pCtx)
3013{
3014 AuthResult result = AuthResultAccessDenied;
3015
3016 /* Use a separate thread because external modules might need a lot of stack space. */
3017 RTTHREAD thread = NIL_RTTHREAD;
3018 int rc = RTThreadCreate(&thread, authThread, pCtx, 512*_1K,
3019 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "VRDEAuth");
3020 LogFlow(("authCall: RTThreadCreate %Rrc\n", rc));
3021
3022 if (RT_SUCCESS(rc))
3023 {
3024 rc = RTThreadWait(thread, RT_INDEFINITE_WAIT, NULL);
3025 LogFlow(("authCall: RTThreadWait %Rrc\n", rc));
3026 }
3027
3028 if (RT_SUCCESS(rc))
3029 {
3030 /* Only update the result if the thread finished without errors. */
3031 result = pCtx->result;
3032 }
3033 else
3034 {
3035 LogRel(("AUTH: unable to execute the auth thread %Rrc\n", rc));
3036 }
3037
3038 return result;
3039}
3040
3041AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
3042 const char *pszUser, const char *pszPassword, const char *pszDomain,
3043 uint32_t u32ClientId)
3044{
3045 AUTHUUID rawuuid;
3046
3047 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
3048
3049 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
3050 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
3051
3052 /*
3053 * Called only from VRDP input thread. So thread safety is not required.
3054 */
3055
3056 if (!mAuthLibrary)
3057 {
3058 /* Load the external authentication library. */
3059 Bstr authLibrary;
3060 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
3061
3062 Utf8Str filename = authLibrary;
3063
3064 LogRel(("AUTH: loading external authentication library '%ls'\n", authLibrary.raw()));
3065
3066 int rc;
3067 if (RTPathHavePath(filename.c_str()))
3068 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
3069 else
3070 {
3071 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
3072 if (RT_FAILURE(rc))
3073 {
3074 /* Backward compatibility with old default 'VRDPAuth' name.
3075 * Try to load new default 'VBoxAuth' instead.
3076 */
3077 if (filename == "VRDPAuth")
3078 {
3079 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library VBoxAuth\n"));
3080 rc = RTLdrLoadAppPriv("VBoxAuth", &mAuthLibrary);
3081 }
3082 }
3083 }
3084
3085 if (RT_FAILURE(rc))
3086 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
3087
3088 if (RT_SUCCESS(rc))
3089 {
3090 typedef struct AuthEntryInfoStruct
3091 {
3092 const char *pszName;
3093 void **ppvAddress;
3094
3095 } AuthEntryInfo;
3096 AuthEntryInfo entries[] =
3097 {
3098 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
3099 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
3100 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
3101 { NULL, NULL }
3102 };
3103
3104 /* Get the entry point. */
3105 AuthEntryInfo *pEntryInfo = &entries[0];
3106 while (pEntryInfo->pszName)
3107 {
3108 *pEntryInfo->ppvAddress = NULL;
3109
3110 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
3111 if (RT_SUCCESS(rc2))
3112 {
3113 /* Found an entry point. */
3114 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
3115 rc = VINF_SUCCESS;
3116 break;
3117 }
3118
3119 if (rc2 != VERR_SYMBOL_NOT_FOUND)
3120 {
3121 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
3122 }
3123 rc = rc2;
3124
3125 pEntryInfo++;
3126 }
3127 }
3128
3129 if (RT_FAILURE(rc))
3130 {
3131 mConsole->setError(E_FAIL,
3132 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
3133 filename.c_str(),
3134 rc);
3135
3136 mpfnAuthEntry = NULL;
3137 mpfnAuthEntry2 = NULL;
3138 mpfnAuthEntry3 = NULL;
3139
3140 if (mAuthLibrary)
3141 {
3142 RTLdrClose(mAuthLibrary);
3143 mAuthLibrary = 0;
3144 }
3145
3146 return AuthResultAccessDenied;
3147 }
3148 }
3149
3150 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
3151
3152 AuthCtx ctx;
3153 ctx.result = AuthResultAccessDenied; /* Denied by default. */
3154 ctx.pfnAuthEntry3 = mpfnAuthEntry3;
3155 ctx.pfnAuthEntry2 = mpfnAuthEntry2;
3156 ctx.pfnAuthEntry = mpfnAuthEntry;
3157 ctx.pszCaller = "vrde";
3158 ctx.pUuid = &rawuuid;
3159 ctx.guestJudgement = guestJudgement;
3160 ctx.pszUser = pszUser;
3161 ctx.pszPassword = pszPassword;
3162 ctx.pszDomain = pszDomain;
3163 ctx.fLogon = true;
3164 ctx.clientId = u32ClientId;
3165
3166 AuthResult result = authCall(&ctx);
3167
3168 switch (result)
3169 {
3170 case AuthResultAccessDenied:
3171 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
3172 break;
3173 case AuthResultAccessGranted:
3174 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
3175 break;
3176 case AuthResultDelegateToGuest:
3177 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
3178 break;
3179 default:
3180 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
3181 result = AuthResultAccessDenied;
3182 }
3183
3184 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
3185
3186 return result;
3187}
3188
3189void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
3190{
3191 AUTHUUID rawuuid;
3192
3193 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
3194
3195 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
3196 rawuuid, u32ClientId));
3197
3198 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
3199
3200 AuthCtx ctx;
3201 ctx.result = AuthResultAccessDenied; /* Not used. */
3202 ctx.pfnAuthEntry3 = mpfnAuthEntry3;
3203 ctx.pfnAuthEntry2 = mpfnAuthEntry2;
3204 ctx.pfnAuthEntry = NULL; /* Does not use disconnect notification. */
3205 ctx.pszCaller = "vrde";
3206 ctx.pUuid = &rawuuid;
3207 ctx.guestJudgement = AuthGuestNotAsked;
3208 ctx.pszUser = NULL;
3209 ctx.pszPassword = NULL;
3210 ctx.pszDomain = NULL;
3211 ctx.fLogon = false;
3212 ctx.clientId = u32ClientId;
3213
3214 authCall(&ctx);
3215}
3216
3217int ConsoleVRDPServer::lockConsoleVRDPServer(void)
3218{
3219 int rc = RTCritSectEnter(&mCritSect);
3220 AssertRC(rc);
3221 return rc;
3222}
3223
3224void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
3225{
3226 RTCritSectLeave(&mCritSect);
3227}
3228
3229DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
3230 uint32_t u32ClientId,
3231 uint32_t u32Function,
3232 uint32_t u32Format,
3233 const void *pvData,
3234 uint32_t cbData)
3235{
3236 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
3237 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
3238
3239 int rc = VINF_SUCCESS;
3240
3241 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
3242
3243 NOREF(u32ClientId);
3244
3245 switch (u32Function)
3246 {
3247 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
3248 {
3249 if (pServer->mpfnClipboardCallback)
3250 {
3251 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
3252 u32Format,
3253 (void *)pvData,
3254 cbData);
3255 }
3256 } break;
3257
3258 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
3259 {
3260 if (pServer->mpfnClipboardCallback)
3261 {
3262 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
3263 u32Format,
3264 (void *)pvData,
3265 cbData);
3266 }
3267 } break;
3268
3269 default:
3270 rc = VERR_NOT_SUPPORTED;
3271 }
3272
3273 return rc;
3274}
3275
3276DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
3277 uint32_t u32Function,
3278 void *pvParms,
3279 uint32_t cbParms)
3280{
3281 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
3282 pvExtension, u32Function, pvParms, cbParms));
3283
3284 int rc = VINF_SUCCESS;
3285
3286 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
3287
3288 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
3289
3290 switch (u32Function)
3291 {
3292 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
3293 {
3294 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
3295 } break;
3296
3297 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
3298 {
3299 /* The guest announces clipboard formats. This must be delivered to all clients. */
3300 if (mpEntryPoints && pServer->mhServer)
3301 {
3302 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3303 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
3304 pParms->u32Format,
3305 NULL,
3306 0,
3307 NULL);
3308 }
3309 } break;
3310
3311 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
3312 {
3313 /* The clipboard service expects that the pvData buffer will be filled
3314 * with clipboard data. The server returns the data from the client that
3315 * announced the requested format most recently.
3316 */
3317 if (mpEntryPoints && pServer->mhServer)
3318 {
3319 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3320 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
3321 pParms->u32Format,
3322 pParms->u.pvData,
3323 pParms->cbData,
3324 &pParms->cbData);
3325 }
3326 } break;
3327
3328 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
3329 {
3330 if (mpEntryPoints && pServer->mhServer)
3331 {
3332 mpEntryPoints->VRDEClipboard(pServer->mhServer,
3333 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
3334 pParms->u32Format,
3335 pParms->u.pvData,
3336 pParms->cbData,
3337 NULL);
3338 }
3339 } break;
3340
3341 default:
3342 rc = VERR_NOT_SUPPORTED;
3343 }
3344
3345 return rc;
3346}
3347
3348void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
3349{
3350 int rc = lockConsoleVRDPServer();
3351
3352 if (RT_SUCCESS(rc))
3353 {
3354 if (mcClipboardRefs == 0)
3355 {
3356 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
3357
3358 if (RT_SUCCESS(rc))
3359 {
3360 mcClipboardRefs++;
3361 }
3362 }
3363
3364 unlockConsoleVRDPServer();
3365 }
3366}
3367
3368void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
3369{
3370 int rc = lockConsoleVRDPServer();
3371
3372 if (RT_SUCCESS(rc))
3373 {
3374 mcClipboardRefs--;
3375
3376 if (mcClipboardRefs == 0)
3377 {
3378 HGCMHostUnregisterServiceExtension(mhClipboard);
3379 }
3380
3381 unlockConsoleVRDPServer();
3382 }
3383}
3384
3385/* That is called on INPUT thread of the VRDP server.
3386 * The ConsoleVRDPServer keeps a list of created backend instances.
3387 */
3388void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
3389{
3390#ifdef VBOX_WITH_USB
3391 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
3392
3393 /* Create a new instance of the USB backend for the new client. */
3394 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
3395
3396 if (pRemoteUSBBackend)
3397 {
3398 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
3399
3400 /* Append the new instance in the list. */
3401 int rc = lockConsoleVRDPServer();
3402
3403 if (RT_SUCCESS(rc))
3404 {
3405 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
3406 if (mUSBBackends.pHead)
3407 {
3408 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
3409 }
3410 else
3411 {
3412 mUSBBackends.pTail = pRemoteUSBBackend;
3413 }
3414
3415 mUSBBackends.pHead = pRemoteUSBBackend;
3416
3417 unlockConsoleVRDPServer();
3418
3419 if (ppvIntercept)
3420 {
3421 *ppvIntercept = pRemoteUSBBackend;
3422 }
3423 }
3424
3425 if (RT_FAILURE(rc))
3426 {
3427 pRemoteUSBBackend->Release();
3428 }
3429 }
3430#endif /* VBOX_WITH_USB */
3431}
3432
3433void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
3434{
3435#ifdef VBOX_WITH_USB
3436 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
3437
3438 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3439
3440 /* Find the instance. */
3441 int rc = lockConsoleVRDPServer();
3442
3443 if (RT_SUCCESS(rc))
3444 {
3445 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3446
3447 if (pRemoteUSBBackend)
3448 {
3449 /* Notify that it will be deleted. */
3450 pRemoteUSBBackend->NotifyDelete();
3451 }
3452
3453 unlockConsoleVRDPServer();
3454 }
3455
3456 if (pRemoteUSBBackend)
3457 {
3458 /* Here the instance has been excluded from the list and can be dereferenced. */
3459 pRemoteUSBBackend->Release();
3460 }
3461#endif
3462}
3463
3464void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
3465{
3466#ifdef VBOX_WITH_USB
3467 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3468
3469 /* Find the instance. */
3470 int rc = lockConsoleVRDPServer();
3471
3472 if (RT_SUCCESS(rc))
3473 {
3474 pRemoteUSBBackend = usbBackendFind(u32ClientId);
3475
3476 if (pRemoteUSBBackend)
3477 {
3478 /* Inform the backend instance that it is referenced by the Guid. */
3479 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
3480
3481 if (fAdded)
3482 {
3483 /* Reference the instance because its pointer is being taken. */
3484 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
3485 }
3486 else
3487 {
3488 pRemoteUSBBackend = NULL;
3489 }
3490 }
3491
3492 unlockConsoleVRDPServer();
3493 }
3494
3495 if (pRemoteUSBBackend)
3496 {
3497 return pRemoteUSBBackend->GetBackendCallbackPointer();
3498 }
3499
3500#endif
3501 return NULL;
3502}
3503
3504void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
3505{
3506#ifdef VBOX_WITH_USB
3507 RemoteUSBBackend *pRemoteUSBBackend = NULL;
3508
3509 /* Find the instance. */
3510 int rc = lockConsoleVRDPServer();
3511
3512 if (RT_SUCCESS(rc))
3513 {
3514 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
3515
3516 if (pRemoteUSBBackend)
3517 {
3518 pRemoteUSBBackend->removeUUID(pGuid);
3519 }
3520
3521 unlockConsoleVRDPServer();
3522
3523 if (pRemoteUSBBackend)
3524 {
3525 pRemoteUSBBackend->Release();
3526 }
3527 }
3528#endif
3529}
3530
3531RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
3532{
3533 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
3534
3535 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
3536#ifdef VBOX_WITH_USB
3537
3538 int rc = lockConsoleVRDPServer();
3539
3540 if (RT_SUCCESS(rc))
3541 {
3542 if (pRemoteUSBBackend == NULL)
3543 {
3544 /* The first backend in the list is requested. */
3545 pNextRemoteUSBBackend = mUSBBackends.pHead;
3546 }
3547 else
3548 {
3549 /* Get pointer to the next backend. */
3550 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3551 }
3552
3553 if (pNextRemoteUSBBackend)
3554 {
3555 pNextRemoteUSBBackend->AddRef();
3556 }
3557
3558 unlockConsoleVRDPServer();
3559
3560 if (pRemoteUSBBackend)
3561 {
3562 pRemoteUSBBackend->Release();
3563 }
3564 }
3565#endif
3566
3567 return pNextRemoteUSBBackend;
3568}
3569
3570#ifdef VBOX_WITH_USB
3571/* Internal method. Called under the ConsoleVRDPServerLock. */
3572RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
3573{
3574 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3575
3576 while (pRemoteUSBBackend)
3577 {
3578 if (pRemoteUSBBackend->ClientId() == u32ClientId)
3579 {
3580 break;
3581 }
3582
3583 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3584 }
3585
3586 return pRemoteUSBBackend;
3587}
3588
3589/* Internal method. Called under the ConsoleVRDPServerLock. */
3590RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
3591{
3592 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
3593
3594 while (pRemoteUSBBackend)
3595 {
3596 if (pRemoteUSBBackend->findUUID(pGuid))
3597 {
3598 break;
3599 }
3600
3601 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3602 }
3603
3604 return pRemoteUSBBackend;
3605}
3606#endif
3607
3608/* Internal method. Called by the backend destructor. */
3609void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
3610{
3611#ifdef VBOX_WITH_USB
3612 int rc = lockConsoleVRDPServer();
3613 AssertRC(rc);
3614
3615 /* Exclude the found instance from the list. */
3616 if (pRemoteUSBBackend->pNext)
3617 {
3618 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
3619 }
3620 else
3621 {
3622 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
3623 }
3624
3625 if (pRemoteUSBBackend->pPrev)
3626 {
3627 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
3628 }
3629 else
3630 {
3631 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
3632 }
3633
3634 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
3635
3636 unlockConsoleVRDPServer();
3637#endif
3638}
3639
3640
3641void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
3642{
3643 if (mpEntryPoints && mhServer)
3644 {
3645 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
3646 }
3647}
3648
3649void ConsoleVRDPServer::SendResize(void) const
3650{
3651 if (mpEntryPoints && mhServer)
3652 {
3653 mpEntryPoints->VRDEResize(mhServer);
3654 }
3655}
3656
3657void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
3658{
3659 VRDEORDERHDR update;
3660 update.x = x;
3661 update.y = y;
3662 update.w = w;
3663 update.h = h;
3664 if (mpEntryPoints && mhServer)
3665 {
3666 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
3667 }
3668}
3669
3670void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
3671{
3672 if (mpEntryPoints && mhServer)
3673 {
3674 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
3675 }
3676}
3677
3678void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
3679{
3680 if (mpEntryPoints && mhServer)
3681 {
3682 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
3683 }
3684}
3685
3686void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
3687{
3688 if (mpEntryPoints && mhServer)
3689 {
3690 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
3691 }
3692}
3693
3694/* @todo rc not needed? */
3695int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
3696 void *pvContext,
3697 uint32_t cSamples,
3698 uint32_t iSampleHz,
3699 uint32_t cChannels,
3700 uint32_t cBits)
3701{
3702 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
3703 {
3704 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3705 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3706 {
3707 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
3708 mpEntryPoints->VRDEAudioInOpen (mhServer,
3709 pvContext,
3710 u32ClientId,
3711 audioFormat,
3712 cSamples);
3713 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
3714 * Currently not used because only one client is allowed to
3715 * do audio input and the client id is saved by the ConsoleVRDPServer.
3716 */
3717
3718 return VINF_SUCCESS;
3719 }
3720 }
3721 return VERR_NOT_SUPPORTED;
3722}
3723
3724void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
3725{
3726 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
3727 {
3728 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
3729 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
3730 {
3731 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
3732 }
3733 }
3734}
3735
3736#ifdef VBOX_WITH_USB_VIDEO
3737int ConsoleVRDPServer::GetVideoFrameDimensions(uint16_t *pu16Heigh, uint16_t *pu16Width)
3738{
3739 *pu16Heigh = 640;
3740 *pu16Width = 480;
3741 return VINF_SUCCESS;
3742}
3743
3744int ConsoleVRDPServer::SendVideoSreamOn(bool fFetch)
3745{
3746 /* Here we inform server that guest is starting/stopping
3747 * the stream
3748 */
3749 return VINF_SUCCESS;
3750}
3751#endif
3752
3753
3754
3755void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
3756{
3757 if (index == VRDE_QI_PORT)
3758 {
3759 uint32_t cbOut = sizeof(int32_t);
3760
3761 if (cbBuffer >= cbOut)
3762 {
3763 *pcbOut = cbOut;
3764 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
3765 }
3766 }
3767 else if (mpEntryPoints && mhServer)
3768 {
3769 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
3770 }
3771}
3772
3773/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
3774{
3775 int rc = VINF_SUCCESS;
3776
3777 if (mVRDPLibrary == NIL_RTLDRMOD)
3778 {
3779 RTERRINFOSTATIC ErrInfo;
3780 RTErrInfoInitStatic(&ErrInfo);
3781
3782 if (RTPathHavePath(pszLibraryName))
3783 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
3784 else
3785 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
3786 if (RT_SUCCESS(rc))
3787 {
3788 struct SymbolEntry
3789 {
3790 const char *name;
3791 void **ppfn;
3792 };
3793
3794 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
3795
3796 static const struct SymbolEntry s_aSymbols[] =
3797 {
3798 DEFSYMENTRY(VRDECreateServer)
3799 };
3800
3801 #undef DEFSYMENTRY
3802
3803 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
3804 {
3805 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
3806
3807 if (RT_FAILURE(rc))
3808 {
3809 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
3810 break;
3811 }
3812 }
3813 }
3814 else
3815 {
3816 if (RTErrInfoIsSet(&ErrInfo.Core))
3817 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
3818 else
3819 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
3820
3821 mVRDPLibrary = NIL_RTLDRMOD;
3822 }
3823 }
3824
3825 if (RT_FAILURE(rc))
3826 {
3827 if (mVRDPLibrary != NIL_RTLDRMOD)
3828 {
3829 RTLdrClose(mVRDPLibrary);
3830 mVRDPLibrary = NIL_RTLDRMOD;
3831 }
3832 }
3833
3834 return rc;
3835}
3836
3837/*
3838 * IVRDEServerInfo implementation.
3839 */
3840// constructor / destructor
3841/////////////////////////////////////////////////////////////////////////////
3842
3843VRDEServerInfo::VRDEServerInfo()
3844 : mParent(NULL)
3845{
3846}
3847
3848VRDEServerInfo::~VRDEServerInfo()
3849{
3850}
3851
3852
3853HRESULT VRDEServerInfo::FinalConstruct()
3854{
3855 return BaseFinalConstruct();
3856}
3857
3858void VRDEServerInfo::FinalRelease()
3859{
3860 uninit();
3861 BaseFinalRelease();
3862}
3863
3864// public methods only for internal purposes
3865/////////////////////////////////////////////////////////////////////////////
3866
3867/**
3868 * Initializes the guest object.
3869 */
3870HRESULT VRDEServerInfo::init(Console *aParent)
3871{
3872 LogFlowThisFunc(("aParent=%p\n", aParent));
3873
3874 ComAssertRet(aParent, E_INVALIDARG);
3875
3876 /* Enclose the state transition NotReady->InInit->Ready */
3877 AutoInitSpan autoInitSpan(this);
3878 AssertReturn(autoInitSpan.isOk(), E_FAIL);
3879
3880 unconst(mParent) = aParent;
3881
3882 /* Confirm a successful initialization */
3883 autoInitSpan.setSucceeded();
3884
3885 return S_OK;
3886}
3887
3888/**
3889 * Uninitializes the instance and sets the ready flag to FALSE.
3890 * Called either from FinalRelease() or by the parent when it gets destroyed.
3891 */
3892void VRDEServerInfo::uninit()
3893{
3894 LogFlowThisFunc(("\n"));
3895
3896 /* Enclose the state transition Ready->InUninit->NotReady */
3897 AutoUninitSpan autoUninitSpan(this);
3898 if (autoUninitSpan.uninitDone())
3899 return;
3900
3901 unconst(mParent) = NULL;
3902}
3903
3904// IVRDEServerInfo properties
3905/////////////////////////////////////////////////////////////////////////////
3906
3907#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
3908 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3909 { \
3910 if (!a##_aName) \
3911 return E_POINTER; \
3912 \
3913 AutoCaller autoCaller(this); \
3914 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3915 \
3916 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3917 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3918 \
3919 uint32_t value; \
3920 uint32_t cbOut = 0; \
3921 \
3922 mParent->consoleVRDPServer()->QueryInfo \
3923 (_aIndex, &value, sizeof(value), &cbOut); \
3924 \
3925 *a##_aName = cbOut? !!value: FALSE; \
3926 \
3927 return S_OK; \
3928 } \
3929 extern void IMPL_GETTER_BOOL_DUMMY(void)
3930
3931#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
3932 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3933 { \
3934 if (!a##_aName) \
3935 return E_POINTER; \
3936 \
3937 AutoCaller autoCaller(this); \
3938 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3939 \
3940 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3941 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3942 \
3943 _aType value; \
3944 uint32_t cbOut = 0; \
3945 \
3946 mParent->consoleVRDPServer()->QueryInfo \
3947 (_aIndex, &value, sizeof(value), &cbOut); \
3948 \
3949 if (_aValueMask) value &= (_aValueMask); \
3950 *a##_aName = cbOut? value: 0; \
3951 \
3952 return S_OK; \
3953 } \
3954 extern void IMPL_GETTER_SCALAR_DUMMY(void)
3955
3956#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
3957 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
3958 { \
3959 if (!a##_aName) \
3960 return E_POINTER; \
3961 \
3962 AutoCaller autoCaller(this); \
3963 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
3964 \
3965 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
3966 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
3967 \
3968 uint32_t cbOut = 0; \
3969 \
3970 mParent->consoleVRDPServer()->QueryInfo \
3971 (_aIndex, NULL, 0, &cbOut); \
3972 \
3973 if (cbOut == 0) \
3974 { \
3975 Bstr str(""); \
3976 str.cloneTo(a##_aName); \
3977 return S_OK; \
3978 } \
3979 \
3980 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
3981 \
3982 if (!pchBuffer) \
3983 { \
3984 Log(("VRDEServerInfo::" \
3985 #_aName \
3986 ": Failed to allocate memory %d bytes\n", cbOut)); \
3987 return E_OUTOFMEMORY; \
3988 } \
3989 \
3990 mParent->consoleVRDPServer()->QueryInfo \
3991 (_aIndex, pchBuffer, cbOut, &cbOut); \
3992 \
3993 Bstr str(pchBuffer); \
3994 \
3995 str.cloneTo(a##_aName); \
3996 \
3997 RTMemTmpFree(pchBuffer); \
3998 \
3999 return S_OK; \
4000 } \
4001 extern void IMPL_GETTER_BSTR_DUMMY(void)
4002
4003IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
4004IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
4005IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
4006IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
4007IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
4008IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
4009IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
4010IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
4011IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
4012IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
4013IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
4014IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
4015IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
4016IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
4017IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
4018
4019#undef IMPL_GETTER_BSTR
4020#undef IMPL_GETTER_SCALAR
4021#undef IMPL_GETTER_BOOL
4022/* 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