VirtualBox

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

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

ConsoleVRDPServer: do not stop VRDP server if a resize is in process

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