VirtualBox

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

Last change on this file since 35757 was 35722, checked in by vboxsync, 14 years ago

Main: reworked listener objects creation, fixes Win problems with events, few cleanups

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 82.1 KB
Line 
1/* $Id: ConsoleVRDPServer.cpp 35722 2011-01-26 16:37:16Z vboxsync $ */
2/** @file
3 * VBox Console VRDP Helper class
4 */
5
6/*
7 * Copyright (C) 2006-2010 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
28#include "Global.h"
29#include "AutoCaller.h"
30#include "Logging.h"
31
32#include <iprt/asm.h>
33#include <iprt/alloca.h>
34#include <iprt/ldr.h>
35#include <iprt/param.h>
36#include <iprt/path.h>
37#include <iprt/cpp/utils.h>
38
39#include <VBox/err.h>
40#include <VBox/RemoteDesktop/VRDEOrders.h>
41#include <VBox/com/listeners.h>
42
43class VRDPConsoleListener
44{
45public:
46 VRDPConsoleListener()
47 {
48 }
49
50 HRESULT init(ConsoleVRDPServer *server)
51 {
52 m_server = server;
53 return S_OK;
54 }
55
56 void uninit()
57 {
58 }
59
60 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
61 {
62 switch (aType)
63 {
64 case VBoxEventType_OnMousePointerShapeChanged:
65 {
66 ComPtr<IMousePointerShapeChangedEvent> mpscev = aEvent;
67 Assert(mpscev);
68 BOOL visible, alpha;
69 ULONG xHot, yHot, width, height;
70 com::SafeArray <BYTE> shape;
71
72 mpscev->COMGETTER(Visible)(&visible);
73 mpscev->COMGETTER(Alpha)(&alpha);
74 mpscev->COMGETTER(Xhot)(&xHot);
75 mpscev->COMGETTER(Yhot)(&yHot);
76 mpscev->COMGETTER(Width)(&width);
77 mpscev->COMGETTER(Height)(&height);
78 mpscev->COMGETTER(Shape)(ComSafeArrayAsOutParam(shape));
79
80 OnMousePointerShapeChange(visible, alpha, xHot, yHot, width, height, ComSafeArrayAsInParam(shape));
81 break;
82 }
83 case VBoxEventType_OnMouseCapabilityChanged:
84 {
85 ComPtr<IMouseCapabilityChangedEvent> mccev = aEvent;
86 Assert(mccev);
87 if (m_server)
88 {
89 BOOL fAbsoluteMouse;
90 mccev->COMGETTER(SupportsAbsolute)(&fAbsoluteMouse);
91 m_server->NotifyAbsoluteMouse(!!fAbsoluteMouse);
92 }
93 break;
94 }
95 case VBoxEventType_OnKeyboardLedsChanged:
96 {
97 ComPtr<IKeyboardLedsChangedEvent> klcev = aEvent;
98 Assert(klcev);
99
100 if (m_server)
101 {
102 BOOL fNumLock, fCapsLock, fScrollLock;
103 klcev->COMGETTER(NumLock)(&fNumLock);
104 klcev->COMGETTER(CapsLock)(&fCapsLock);
105 klcev->COMGETTER(ScrollLock)(&fScrollLock);
106 m_server->NotifyKeyboardLedsChange(fNumLock, fCapsLock, fScrollLock);
107 }
108 break;
109 }
110
111 default:
112 AssertFailed();
113 }
114
115 return S_OK;
116 }
117
118private:
119 STDMETHOD(OnMousePointerShapeChange)(BOOL visible, BOOL alpha, ULONG xHot, ULONG yHot,
120 ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
121 ConsoleVRDPServer *m_server;
122};
123
124typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
125
126VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
127
128#ifdef DEBUG_sunlover
129#define LOGDUMPPTR Log
130void dumpPointer(const uint8_t *pu8Shape, uint32_t width, uint32_t height, bool fXorMaskRGB32)
131{
132 unsigned i;
133
134 const uint8_t *pu8And = pu8Shape;
135
136 for (i = 0; i < height; i++)
137 {
138 unsigned j;
139 LOGDUMPPTR(("%p: ", pu8And));
140 for (j = 0; j < (width + 7) / 8; j++)
141 {
142 unsigned k;
143 for (k = 0; k < 8; k++)
144 {
145 LOGDUMPPTR(("%d", ((*pu8And) & (1 << (7 - k)))? 1: 0));
146 }
147
148 pu8And++;
149 }
150 LOGDUMPPTR(("\n"));
151 }
152
153 if (fXorMaskRGB32)
154 {
155 uint32_t *pu32Xor = (uint32_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
156
157 for (i = 0; i < height; i++)
158 {
159 unsigned j;
160 LOGDUMPPTR(("%p: ", pu32Xor));
161 for (j = 0; j < width; j++)
162 {
163 LOGDUMPPTR(("%08X", *pu32Xor++));
164 }
165 LOGDUMPPTR(("\n"));
166 }
167 }
168 else
169 {
170 /* RDP 24 bit RGB mask. */
171 uint8_t *pu8Xor = (uint8_t*)(pu8Shape + ((((width + 7) / 8) * height + 3) & ~3));
172 for (i = 0; i < height; i++)
173 {
174 unsigned j;
175 LOGDUMPPTR(("%p: ", pu8Xor));
176 for (j = 0; j < width; j++)
177 {
178 LOGDUMPPTR(("%02X%02X%02X", pu8Xor[2], pu8Xor[1], pu8Xor[0]));
179 pu8Xor += 3;
180 }
181 LOGDUMPPTR(("\n"));
182 }
183 }
184}
185#else
186#define dumpPointer(a, b, c, d) do {} while (0)
187#endif /* DEBUG_sunlover */
188
189static void findTopLeftBorder(const uint8_t *pu8AndMask, const uint8_t *pu8XorMask, uint32_t width, uint32_t height, uint32_t *pxSkip, uint32_t *pySkip)
190{
191 /*
192 * Find the top border of the AND mask. First assign to special value.
193 */
194 uint32_t ySkipAnd = ~0;
195
196 const uint8_t *pu8And = pu8AndMask;
197 const uint32_t cbAndRow = (width + 7) / 8;
198 const uint8_t maskLastByte = (uint8_t)( 0xFF << (cbAndRow * 8 - width) );
199
200 Assert(cbAndRow > 0);
201
202 unsigned y;
203 unsigned x;
204
205 for (y = 0; y < height && ySkipAnd == ~(uint32_t)0; y++, pu8And += cbAndRow)
206 {
207 /* For each complete byte in the row. */
208 for (x = 0; x < cbAndRow - 1; x++)
209 {
210 if (pu8And[x] != 0xFF)
211 {
212 ySkipAnd = y;
213 break;
214 }
215 }
216
217 if (ySkipAnd == ~(uint32_t)0)
218 {
219 /* Last byte. */
220 if ((pu8And[cbAndRow - 1] & maskLastByte) != maskLastByte)
221 {
222 ySkipAnd = y;
223 }
224 }
225 }
226
227 if (ySkipAnd == ~(uint32_t)0)
228 {
229 ySkipAnd = 0;
230 }
231
232 /*
233 * Find the left border of the AND mask.
234 */
235 uint32_t xSkipAnd = ~0;
236
237 /* For all bit columns. */
238 for (x = 0; x < width && xSkipAnd == ~(uint32_t)0; x++)
239 {
240 pu8And = pu8AndMask + x/8; /* Currently checking byte. */
241 uint8_t mask = 1 << (7 - x%8); /* Currently checking bit in the byte. */
242
243 for (y = ySkipAnd; y < height; y++, pu8And += cbAndRow)
244 {
245 if ((*pu8And & mask) == 0)
246 {
247 xSkipAnd = x;
248 break;
249 }
250 }
251 }
252
253 if (xSkipAnd == ~(uint32_t)0)
254 {
255 xSkipAnd = 0;
256 }
257
258 /*
259 * Find the XOR mask top border.
260 */
261 uint32_t ySkipXor = ~0;
262
263 uint32_t *pu32XorStart = (uint32_t *)pu8XorMask;
264
265 uint32_t *pu32Xor = pu32XorStart;
266
267 for (y = 0; y < height && ySkipXor == ~(uint32_t)0; y++, pu32Xor += width)
268 {
269 for (x = 0; x < width; x++)
270 {
271 if (pu32Xor[x] != 0)
272 {
273 ySkipXor = y;
274 break;
275 }
276 }
277 }
278
279 if (ySkipXor == ~(uint32_t)0)
280 {
281 ySkipXor = 0;
282 }
283
284 /*
285 * Find the left border of the XOR mask.
286 */
287 uint32_t xSkipXor = ~(uint32_t)0;
288
289 /* For all columns. */
290 for (x = 0; x < width && xSkipXor == ~(uint32_t)0; x++)
291 {
292 pu32Xor = pu32XorStart + x; /* Currently checking dword. */
293
294 for (y = ySkipXor; y < height; y++, pu32Xor += width)
295 {
296 if (*pu32Xor != 0)
297 {
298 xSkipXor = x;
299 break;
300 }
301 }
302 }
303
304 if (xSkipXor == ~(uint32_t)0)
305 {
306 xSkipXor = 0;
307 }
308
309 *pxSkip = RT_MIN(xSkipAnd, xSkipXor);
310 *pySkip = RT_MIN(ySkipAnd, ySkipXor);
311}
312
313/* Generate an AND mask for alpha pointers here, because
314 * guest driver does not do that correctly for Vista pointers.
315 * Similar fix, changing the alpha threshold, could be applied
316 * for the guest driver, but then additions reinstall would be
317 * necessary, which we try to avoid.
318 */
319static void mousePointerGenerateANDMask(uint8_t *pu8DstAndMask, int cbDstAndMask, const uint8_t *pu8SrcAlpha, int w, int h)
320{
321 memset(pu8DstAndMask, 0xFF, cbDstAndMask);
322
323 int y;
324 for (y = 0; y < h; y++)
325 {
326 uint8_t bitmask = 0x80;
327
328 int x;
329 for (x = 0; x < w; x++, bitmask >>= 1)
330 {
331 if (bitmask == 0)
332 {
333 bitmask = 0x80;
334 }
335
336 /* Whether alpha channel value is not transparent enough for the pixel to be seen. */
337 if (pu8SrcAlpha[x * 4 + 3] > 0x7f)
338 {
339 pu8DstAndMask[x / 8] &= ~bitmask;
340 }
341 }
342
343 /* Point to next source and dest scans. */
344 pu8SrcAlpha += w * 4;
345 pu8DstAndMask += (w + 7) / 8;
346 }
347}
348
349STDMETHODIMP VRDPConsoleListener::OnMousePointerShapeChange(BOOL visible,
350 BOOL alpha,
351 ULONG xHot,
352 ULONG yHot,
353 ULONG width,
354 ULONG height,
355 ComSafeArrayIn(BYTE,inShape))
356{
357 LogSunlover(("VRDPConsoleListener::OnMousePointerShapeChange: %d, %d, %lux%lu, @%lu,%lu\n", visible, alpha, width, height, xHot, yHot));
358
359 if (m_server)
360 {
361 com::SafeArray <BYTE> aShape(ComSafeArrayInArg(inShape));
362 if (aShape.size() == 0)
363 {
364 if (!visible)
365 {
366 m_server->MousePointerHide();
367 }
368 }
369 else if (width != 0 && height != 0)
370 {
371 /* Pointer consists of 1 bpp AND and 24 BPP XOR masks.
372 * 'shape' AND mask followed by XOR mask.
373 * XOR mask contains 32 bit (lsb)BGR0(msb) values.
374 *
375 * We convert this to RDP color format which consist of
376 * one bpp AND mask and 24 BPP (BGR) color XOR image.
377 *
378 * RDP clients expect 8 aligned width and height of
379 * pointer (preferably 32x32).
380 *
381 * They even contain bugs which do not appear for
382 * 32x32 pointers but would appear for a 41x32 one.
383 *
384 * So set pointer size to 32x32. This can be done safely
385 * because most pointers are 32x32.
386 */
387 uint8_t* shape = aShape.raw();
388
389 dumpPointer(shape, width, height, true);
390
391 int cbDstAndMask = (((width + 7) / 8) * height + 3) & ~3;
392
393 uint8_t *pu8AndMask = shape;
394 uint8_t *pu8XorMask = shape + cbDstAndMask;
395
396 if (alpha)
397 {
398 pu8AndMask = (uint8_t*)alloca(cbDstAndMask);
399
400 mousePointerGenerateANDMask(pu8AndMask, cbDstAndMask, pu8XorMask, width, height);
401 }
402
403 /* Windows guest alpha pointers are wider than 32 pixels.
404 * Try to find out the top-left border of the pointer and
405 * then copy only meaningful bits. All complete top rows
406 * and all complete left columns where (AND == 1 && XOR == 0)
407 * are skipped. Hot spot is adjusted.
408 */
409 uint32_t ySkip = 0; /* How many rows to skip at the top. */
410 uint32_t xSkip = 0; /* How many columns to skip at the left. */
411
412 findTopLeftBorder(pu8AndMask, pu8XorMask, width, height, &xSkip, &ySkip);
413
414 /* Must not skip the hot spot. */
415 xSkip = RT_MIN(xSkip, xHot);
416 ySkip = RT_MIN(ySkip, yHot);
417
418 /*
419 * Compute size and allocate memory for the pointer.
420 */
421 const uint32_t dstwidth = 32;
422 const uint32_t dstheight = 32;
423
424 VRDECOLORPOINTER *pointer = NULL;
425
426 uint32_t dstmaskwidth = (dstwidth + 7) / 8;
427
428 uint32_t rdpmaskwidth = dstmaskwidth;
429 uint32_t rdpmasklen = dstheight * rdpmaskwidth;
430
431 uint32_t rdpdatawidth = dstwidth * 3;
432 uint32_t rdpdatalen = dstheight * rdpdatawidth;
433
434 pointer = (VRDECOLORPOINTER *)RTMemTmpAlloc(sizeof(VRDECOLORPOINTER) + rdpmasklen + rdpdatalen);
435
436 if (pointer)
437 {
438 uint8_t *maskarray = (uint8_t*)pointer + sizeof(VRDECOLORPOINTER);
439 uint8_t *dataarray = maskarray + rdpmasklen;
440
441 memset(maskarray, 0xFF, rdpmasklen);
442 memset(dataarray, 0x00, rdpdatalen);
443
444 uint32_t srcmaskwidth = (width + 7) / 8;
445 uint32_t srcdatawidth = width * 4;
446
447 /* Copy AND mask. */
448 uint8_t *src = pu8AndMask + ySkip * srcmaskwidth;
449 uint8_t *dst = maskarray + (dstheight - 1) * rdpmaskwidth;
450
451 uint32_t minheight = RT_MIN(height - ySkip, dstheight);
452 uint32_t minwidth = RT_MIN(width - xSkip, dstwidth);
453
454 unsigned x, y;
455
456 for (y = 0; y < minheight; y++)
457 {
458 for (x = 0; x < minwidth; x++)
459 {
460 uint32_t byteIndex = (x + xSkip) / 8;
461 uint32_t bitIndex = (x + xSkip) % 8;
462
463 bool bit = (src[byteIndex] & (1 << (7 - bitIndex))) != 0;
464
465 if (!bit)
466 {
467 byteIndex = x / 8;
468 bitIndex = x % 8;
469
470 dst[byteIndex] &= ~(1 << (7 - bitIndex));
471 }
472 }
473
474 src += srcmaskwidth;
475 dst -= rdpmaskwidth;
476 }
477
478 /* Point src to XOR mask */
479 src = pu8XorMask + ySkip * srcdatawidth;
480 dst = dataarray + (dstheight - 1) * rdpdatawidth;
481
482 for (y = 0; y < minheight ; y++)
483 {
484 for (x = 0; x < minwidth; x++)
485 {
486 memcpy(dst + x * 3, &src[4 * (x + xSkip)], 3);
487 }
488
489 src += srcdatawidth;
490 dst -= rdpdatawidth;
491 }
492
493 pointer->u16HotX = (uint16_t)(xHot - xSkip);
494 pointer->u16HotY = (uint16_t)(yHot - ySkip);
495
496 pointer->u16Width = (uint16_t)dstwidth;
497 pointer->u16Height = (uint16_t)dstheight;
498
499 pointer->u16MaskLen = (uint16_t)rdpmasklen;
500 pointer->u16DataLen = (uint16_t)rdpdatalen;
501
502 dumpPointer((uint8_t*)pointer + sizeof(*pointer), dstwidth, dstheight, false);
503
504 m_server->MousePointerUpdate(pointer);
505
506 RTMemTmpFree(pointer);
507 }
508 }
509 }
510
511 return S_OK;
512}
513
514
515// ConsoleVRDPServer
516////////////////////////////////////////////////////////////////////////////////
517
518RTLDRMOD ConsoleVRDPServer::mVRDPLibrary = NIL_RTLDRMOD;
519
520PFNVRDECREATESERVER ConsoleVRDPServer::mpfnVRDECreateServer = NULL;
521
522VRDEENTRYPOINTS_3 ConsoleVRDPServer::mEntryPoints; /* A copy of the server entry points. */
523VRDEENTRYPOINTS_3 *ConsoleVRDPServer::mpEntryPoints = NULL;
524
525VRDECALLBACKS_3 ConsoleVRDPServer::mCallbacks =
526{
527 { VRDE_INTERFACE_VERSION_3, sizeof(VRDECALLBACKS_3) },
528 ConsoleVRDPServer::VRDPCallbackQueryProperty,
529 ConsoleVRDPServer::VRDPCallbackClientLogon,
530 ConsoleVRDPServer::VRDPCallbackClientConnect,
531 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
532 ConsoleVRDPServer::VRDPCallbackIntercept,
533 ConsoleVRDPServer::VRDPCallbackUSB,
534 ConsoleVRDPServer::VRDPCallbackClipboard,
535 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
536 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
537 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
538 ConsoleVRDPServer::VRDPCallbackInput,
539 ConsoleVRDPServer::VRDPCallbackVideoModeHint,
540 ConsoleVRDPServer::VRDECallbackAudioIn
541};
542
543DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackQueryProperty(void *pvCallback, uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut)
544{
545 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
546
547 int rc = VERR_NOT_SUPPORTED;
548
549 switch (index)
550 {
551 case VRDE_QP_NETWORK_PORT:
552 {
553 /* This is obsolete, the VRDE server uses VRDE_QP_NETWORK_PORT_RANGE instead. */
554 ULONG port = 0;
555
556 if (cbBuffer >= sizeof(uint32_t))
557 {
558 *(uint32_t *)pvBuffer = (uint32_t)port;
559 rc = VINF_SUCCESS;
560 }
561 else
562 {
563 rc = VINF_BUFFER_OVERFLOW;
564 }
565
566 *pcbOut = sizeof(uint32_t);
567 } break;
568
569 case VRDE_QP_NETWORK_ADDRESS:
570 {
571 com::Bstr bstr;
572 server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Address").raw(), bstr.asOutParam());
573
574 /* The server expects UTF8. */
575 com::Utf8Str address = bstr;
576
577 size_t cbAddress = address.length() + 1;
578
579 if (cbAddress >= 0x10000)
580 {
581 /* More than 64K seems to be an invalid address. */
582 rc = VERR_TOO_MUCH_DATA;
583 break;
584 }
585
586 if ((size_t)cbBuffer >= cbAddress)
587 {
588 memcpy(pvBuffer, address.c_str(), cbAddress);
589 rc = VINF_SUCCESS;
590 }
591 else
592 {
593 rc = VINF_BUFFER_OVERFLOW;
594 }
595
596 *pcbOut = (uint32_t)cbAddress;
597 } break;
598
599 case VRDE_QP_NUMBER_MONITORS:
600 {
601 ULONG cMonitors = 1;
602
603 server->mConsole->machine()->COMGETTER(MonitorCount)(&cMonitors);
604
605 if (cbBuffer >= sizeof(uint32_t))
606 {
607 *(uint32_t *)pvBuffer = (uint32_t)cMonitors;
608 rc = VINF_SUCCESS;
609 }
610 else
611 {
612 rc = VINF_BUFFER_OVERFLOW;
613 }
614
615 *pcbOut = sizeof(uint32_t);
616 } break;
617
618 case VRDE_QP_NETWORK_PORT_RANGE:
619 {
620 com::Bstr bstr;
621 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("TCP/Ports").raw(), bstr.asOutParam());
622
623 if (hrc != S_OK)
624 {
625 bstr = "";
626 }
627
628 if (bstr == "0")
629 {
630 bstr = "3389";
631 }
632
633 /* The server expects UTF8. */
634 com::Utf8Str portRange = bstr;
635
636 size_t cbPortRange = portRange.length() + 1;
637
638 if (cbPortRange >= 0x10000)
639 {
640 /* More than 64K seems to be an invalid port range string. */
641 rc = VERR_TOO_MUCH_DATA;
642 break;
643 }
644
645 if ((size_t)cbBuffer >= cbPortRange)
646 {
647 memcpy(pvBuffer, portRange.c_str(), cbPortRange);
648 rc = VINF_SUCCESS;
649 }
650 else
651 {
652 rc = VINF_BUFFER_OVERFLOW;
653 }
654
655 *pcbOut = (uint32_t)cbPortRange;
656 } break;
657
658#ifdef VBOX_WITH_VRDP_VIDEO_CHANNEL
659 case VRDE_QP_VIDEO_CHANNEL:
660 {
661 com::Bstr bstr;
662 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Enabled").raw(), bstr.asOutParam());
663
664 if (hrc != S_OK)
665 {
666 bstr = "";
667 }
668
669 com::Utf8Str value = bstr;
670
671 BOOL fVideoEnabled = RTStrICmp(value.c_str(), "true") == 0
672 || RTStrICmp(value.c_str(), "1") == 0;
673
674 if (cbBuffer >= sizeof(uint32_t))
675 {
676 *(uint32_t *)pvBuffer = (uint32_t)fVideoEnabled;
677 rc = VINF_SUCCESS;
678 }
679 else
680 {
681 rc = VINF_BUFFER_OVERFLOW;
682 }
683
684 *pcbOut = sizeof(uint32_t);
685 } break;
686
687 case VRDE_QP_VIDEO_CHANNEL_QUALITY:
688 {
689 com::Bstr bstr;
690 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr("VideoChannel/Quality").raw(), bstr.asOutParam());
691
692 if (hrc != S_OK)
693 {
694 bstr = "";
695 }
696
697 com::Utf8Str value = bstr;
698
699 ULONG ulQuality = RTStrToUInt32(value.c_str()); /* This returns 0 on invalid string which is ok. */
700
701 if (cbBuffer >= sizeof(uint32_t))
702 {
703 *(uint32_t *)pvBuffer = (uint32_t)ulQuality;
704 rc = VINF_SUCCESS;
705 }
706 else
707 {
708 rc = VINF_BUFFER_OVERFLOW;
709 }
710
711 *pcbOut = sizeof(uint32_t);
712 } break;
713
714 case VRDE_QP_VIDEO_CHANNEL_SUNFLSH:
715 {
716 ULONG ulSunFlsh = 1;
717
718 com::Bstr bstr;
719 HRESULT hrc = server->mConsole->machine()->GetExtraData(Bstr("VRDP/SunFlsh").raw(),
720 bstr.asOutParam());
721 if (hrc == S_OK && !bstr.isEmpty())
722 {
723 com::Utf8Str sunFlsh = bstr;
724 if (!sunFlsh.isEmpty())
725 {
726 ulSunFlsh = sunFlsh.toUInt32();
727 }
728 }
729
730 if (cbBuffer >= sizeof(uint32_t))
731 {
732 *(uint32_t *)pvBuffer = (uint32_t)ulSunFlsh;
733 rc = VINF_SUCCESS;
734 }
735 else
736 {
737 rc = VINF_BUFFER_OVERFLOW;
738 }
739
740 *pcbOut = sizeof(uint32_t);
741 } break;
742#endif /* VBOX_WITH_VRDP_VIDEO_CHANNEL */
743
744 case VRDE_QP_FEATURE:
745 {
746 if (cbBuffer < sizeof(VRDEFEATURE))
747 {
748 rc = VERR_INVALID_PARAMETER;
749 break;
750 }
751
752 size_t cbInfo = cbBuffer - RT_OFFSETOF(VRDEFEATURE, achInfo);
753
754 VRDEFEATURE *pFeature = (VRDEFEATURE *)pvBuffer;
755
756 size_t cchInfo = 0;
757 rc = RTStrNLenEx(pFeature->achInfo, cbInfo, &cchInfo);
758
759 if (RT_FAILURE(rc))
760 {
761 rc = VERR_INVALID_PARAMETER;
762 break;
763 }
764
765 Log(("VRDE_QP_FEATURE [%s]\n", pFeature->achInfo));
766
767 com::Bstr bstrValue;
768
769 if ( RTStrICmp(pFeature->achInfo, "Client/DisableDisplay") == 0
770 || RTStrICmp(pFeature->achInfo, "Client/DisableInput") == 0
771 || RTStrICmp(pFeature->achInfo, "Client/DisableAudio") == 0
772 || RTStrICmp(pFeature->achInfo, "Client/DisableUSB") == 0
773 || RTStrICmp(pFeature->achInfo, "Client/DisableClipboard") == 0
774 )
775 {
776 /* @todo these features should be per client. */
777 NOREF(pFeature->u32ClientId);
778
779 /* These features are mapped to "VRDE/Feature/NAME" extra data. */
780 com::Utf8Str extraData("VRDE/Feature/");
781 extraData += pFeature->achInfo;
782
783 HRESULT hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
784 bstrValue.asOutParam());
785 if (FAILED(hrc) || bstrValue.isEmpty())
786 {
787 /* Also try the old "VRDP/Feature/NAME" */
788 extraData = "VRDP/Feature/";
789 extraData += pFeature->achInfo;
790
791 hrc = server->mConsole->machine()->GetExtraData(com::Bstr(extraData).raw(),
792 bstrValue.asOutParam());
793 if (FAILED(hrc))
794 {
795 rc = VERR_NOT_SUPPORTED;
796 }
797 }
798 }
799 else if (RTStrNCmp(pFeature->achInfo, "Property/", 9) == 0)
800 {
801 /* Generic properties. */
802 const char *pszPropertyName = &pFeature->achInfo[9];
803 HRESULT hrc = server->mConsole->getVRDEServer()->GetVRDEProperty(Bstr(pszPropertyName).raw(), bstrValue.asOutParam());
804 if (FAILED(hrc))
805 {
806 rc = VERR_NOT_SUPPORTED;
807 }
808 }
809 else
810 {
811 rc = VERR_NOT_SUPPORTED;
812 }
813
814 /* Copy the value string to the callers buffer. */
815 if (rc == VINF_SUCCESS)
816 {
817 com::Utf8Str value = bstrValue;
818
819 size_t cb = value.length() + 1;
820
821 if ((size_t)cbInfo >= cb)
822 {
823 memcpy(pFeature->achInfo, value.c_str(), cb);
824 }
825 else
826 {
827 rc = VINF_BUFFER_OVERFLOW;
828 }
829
830 *pcbOut = (uint32_t)cb;
831 }
832 } break;
833
834 case VRDE_SP_NETWORK_BIND_PORT:
835 {
836 if (cbBuffer != sizeof(uint32_t))
837 {
838 rc = VERR_INVALID_PARAMETER;
839 break;
840 }
841
842 ULONG port = *(uint32_t *)pvBuffer;
843
844 server->mVRDPBindPort = port;
845
846 rc = VINF_SUCCESS;
847
848 if (pcbOut)
849 {
850 *pcbOut = sizeof(uint32_t);
851 }
852
853 server->mConsole->onVRDEServerInfoChange();
854 } break;
855
856 default:
857 break;
858 }
859
860 return rc;
861}
862
863DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClientLogon(void *pvCallback, uint32_t u32ClientId, const char *pszUser, const char *pszPassword, const char *pszDomain)
864{
865 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
866
867 return server->mConsole->VRDPClientLogon(u32ClientId, pszUser, pszPassword, pszDomain);
868}
869
870DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientConnect(void *pvCallback, uint32_t u32ClientId)
871{
872 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
873
874 server->mConsole->VRDPClientConnect(u32ClientId);
875}
876
877DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackClientDisconnect(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercepted)
878{
879 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
880
881 server->mConsole->VRDPClientDisconnect(u32ClientId, fu32Intercepted);
882
883 if (ASMAtomicReadU32(&server->mu32AudioInputClientId) == u32ClientId)
884 {
885 Log(("AUDIOIN: disconnected client %u\n", u32ClientId));
886 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
887
888 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
889 if (pPort)
890 {
891 pPort->pfnAudioInputIntercept(pPort, false);
892 }
893 else
894 {
895 AssertFailed();
896 }
897 }
898}
899
900DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackIntercept(void *pvCallback, uint32_t u32ClientId, uint32_t fu32Intercept, void **ppvIntercept)
901{
902 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
903
904 LogFlowFunc(("%x\n", fu32Intercept));
905
906 int rc = VERR_NOT_SUPPORTED;
907
908 switch (fu32Intercept)
909 {
910 case VRDE_CLIENT_INTERCEPT_AUDIO:
911 {
912 server->mConsole->VRDPInterceptAudio(u32ClientId);
913 if (ppvIntercept)
914 {
915 *ppvIntercept = server;
916 }
917 rc = VINF_SUCCESS;
918 } break;
919
920 case VRDE_CLIENT_INTERCEPT_USB:
921 {
922 server->mConsole->VRDPInterceptUSB(u32ClientId, ppvIntercept);
923 rc = VINF_SUCCESS;
924 } break;
925
926 case VRDE_CLIENT_INTERCEPT_CLIPBOARD:
927 {
928 server->mConsole->VRDPInterceptClipboard(u32ClientId);
929 if (ppvIntercept)
930 {
931 *ppvIntercept = server;
932 }
933 rc = VINF_SUCCESS;
934 } break;
935
936 case VRDE_CLIENT_INTERCEPT_AUDIO_INPUT:
937 {
938 /* This request is processed internally by the ConsoleVRDPServer.
939 * Only one client is allowed to intercept audio input.
940 */
941 if (ASMAtomicCmpXchgU32(&server->mu32AudioInputClientId, u32ClientId, 0) == true)
942 {
943 Log(("AUDIOIN: connected client %u\n", u32ClientId));
944
945 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
946 if (pPort)
947 {
948 pPort->pfnAudioInputIntercept(pPort, true);
949 if (ppvIntercept)
950 {
951 *ppvIntercept = server;
952 }
953 }
954 else
955 {
956 AssertFailed();
957 ASMAtomicWriteU32(&server->mu32AudioInputClientId, 0);
958 rc = VERR_NOT_SUPPORTED;
959 }
960 }
961 else
962 {
963 Log(("AUDIOIN: ignored client %u, active client %u\n", u32ClientId, server->mu32AudioInputClientId));
964 rc = VERR_NOT_SUPPORTED;
965 }
966 } break;
967
968 default:
969 break;
970 }
971
972 return rc;
973}
974
975DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackUSB(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint8_t u8Code, const void *pvRet, uint32_t cbRet)
976{
977#ifdef VBOX_WITH_USB
978 return USBClientResponseCallback(pvIntercept, u32ClientId, u8Code, pvRet, cbRet);
979#else
980 return VERR_NOT_SUPPORTED;
981#endif
982}
983
984DECLCALLBACK(int) ConsoleVRDPServer::VRDPCallbackClipboard(void *pvCallback, void *pvIntercept, uint32_t u32ClientId, uint32_t u32Function, uint32_t u32Format, const void *pvData, uint32_t cbData)
985{
986 return ClipboardCallback(pvIntercept, u32ClientId, u32Function, u32Format, pvData, cbData);
987}
988
989DECLCALLBACK(bool) ConsoleVRDPServer::VRDPCallbackFramebufferQuery(void *pvCallback, unsigned uScreenId, VRDEFRAMEBUFFERINFO *pInfo)
990{
991 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
992
993 bool fAvailable = false;
994
995 IFramebuffer *pfb = NULL;
996 LONG xOrigin = 0;
997 LONG yOrigin = 0;
998
999 server->mConsole->getDisplay()->GetFramebuffer(uScreenId, &pfb, &xOrigin, &yOrigin);
1000
1001 if (pfb)
1002 {
1003 pfb->Lock ();
1004
1005 /* Query framebuffer parameters. */
1006 ULONG lineSize = 0;
1007 pfb->COMGETTER(BytesPerLine)(&lineSize);
1008
1009 ULONG bitsPerPixel = 0;
1010 pfb->COMGETTER(BitsPerPixel)(&bitsPerPixel);
1011
1012 BYTE *address = NULL;
1013 pfb->COMGETTER(Address)(&address);
1014
1015 ULONG height = 0;
1016 pfb->COMGETTER(Height)(&height);
1017
1018 ULONG width = 0;
1019 pfb->COMGETTER(Width)(&width);
1020
1021 /* Now fill the information as requested by the caller. */
1022 pInfo->pu8Bits = address;
1023 pInfo->xOrigin = xOrigin;
1024 pInfo->yOrigin = yOrigin;
1025 pInfo->cWidth = width;
1026 pInfo->cHeight = height;
1027 pInfo->cBitsPerPixel = bitsPerPixel;
1028 pInfo->cbLine = lineSize;
1029
1030 pfb->Unlock();
1031
1032 fAvailable = true;
1033 }
1034
1035 if (server->maFramebuffers[uScreenId])
1036 {
1037 server->maFramebuffers[uScreenId]->Release();
1038 }
1039 server->maFramebuffers[uScreenId] = pfb;
1040
1041 return fAvailable;
1042}
1043
1044DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferLock(void *pvCallback, unsigned uScreenId)
1045{
1046 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1047
1048 if (server->maFramebuffers[uScreenId])
1049 {
1050 server->maFramebuffers[uScreenId]->Lock();
1051 }
1052}
1053
1054DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackFramebufferUnlock(void *pvCallback, unsigned uScreenId)
1055{
1056 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1057
1058 if (server->maFramebuffers[uScreenId])
1059 {
1060 server->maFramebuffers[uScreenId]->Unlock();
1061 }
1062}
1063
1064static void fixKbdLockStatus(VRDPInputSynch *pInputSynch, IKeyboard *pKeyboard)
1065{
1066 if ( pInputSynch->cGuestNumLockAdaptions
1067 && (pInputSynch->fGuestNumLock != pInputSynch->fClientNumLock))
1068 {
1069 pInputSynch->cGuestNumLockAdaptions--;
1070 pKeyboard->PutScancode(0x45);
1071 pKeyboard->PutScancode(0x45 | 0x80);
1072 }
1073 if ( pInputSynch->cGuestCapsLockAdaptions
1074 && (pInputSynch->fGuestCapsLock != pInputSynch->fClientCapsLock))
1075 {
1076 pInputSynch->cGuestCapsLockAdaptions--;
1077 pKeyboard->PutScancode(0x3a);
1078 pKeyboard->PutScancode(0x3a | 0x80);
1079 }
1080}
1081
1082DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackInput(void *pvCallback, int type, const void *pvInput, unsigned cbInput)
1083{
1084 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1085 Console *pConsole = server->mConsole;
1086
1087 switch (type)
1088 {
1089 case VRDE_INPUT_SCANCODE:
1090 {
1091 if (cbInput == sizeof(VRDEINPUTSCANCODE))
1092 {
1093 IKeyboard *pKeyboard = pConsole->getKeyboard();
1094
1095 const VRDEINPUTSCANCODE *pInputScancode = (VRDEINPUTSCANCODE *)pvInput;
1096
1097 /* Track lock keys. */
1098 if (pInputScancode->uScancode == 0x45)
1099 {
1100 server->m_InputSynch.fClientNumLock = !server->m_InputSynch.fClientNumLock;
1101 }
1102 else if (pInputScancode->uScancode == 0x3a)
1103 {
1104 server->m_InputSynch.fClientCapsLock = !server->m_InputSynch.fClientCapsLock;
1105 }
1106 else if (pInputScancode->uScancode == 0x46)
1107 {
1108 server->m_InputSynch.fClientScrollLock = !server->m_InputSynch.fClientScrollLock;
1109 }
1110 else if ((pInputScancode->uScancode & 0x80) == 0)
1111 {
1112 /* Key pressed. */
1113 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1114 }
1115
1116 pKeyboard->PutScancode((LONG)pInputScancode->uScancode);
1117 }
1118 } break;
1119
1120 case VRDE_INPUT_POINT:
1121 {
1122 if (cbInput == sizeof(VRDEINPUTPOINT))
1123 {
1124 const VRDEINPUTPOINT *pInputPoint = (VRDEINPUTPOINT *)pvInput;
1125
1126 int mouseButtons = 0;
1127 int iWheel = 0;
1128
1129 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON1)
1130 {
1131 mouseButtons |= MouseButtonState_LeftButton;
1132 }
1133 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON2)
1134 {
1135 mouseButtons |= MouseButtonState_RightButton;
1136 }
1137 if (pInputPoint->uButtons & VRDE_INPUT_POINT_BUTTON3)
1138 {
1139 mouseButtons |= MouseButtonState_MiddleButton;
1140 }
1141 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_UP)
1142 {
1143 mouseButtons |= MouseButtonState_WheelUp;
1144 iWheel = -1;
1145 }
1146 if (pInputPoint->uButtons & VRDE_INPUT_POINT_WHEEL_DOWN)
1147 {
1148 mouseButtons |= MouseButtonState_WheelDown;
1149 iWheel = 1;
1150 }
1151
1152 if (server->m_fGuestWantsAbsolute)
1153 {
1154 pConsole->getMouse()->PutMouseEventAbsolute(pInputPoint->x + 1, pInputPoint->y + 1, iWheel, 0 /* Horizontal wheel */, mouseButtons);
1155 } else
1156 {
1157 pConsole->getMouse()->PutMouseEvent(pInputPoint->x - server->m_mousex,
1158 pInputPoint->y - server->m_mousey,
1159 iWheel, 0 /* Horizontal wheel */, mouseButtons);
1160 server->m_mousex = pInputPoint->x;
1161 server->m_mousey = pInputPoint->y;
1162 }
1163 }
1164 } break;
1165
1166 case VRDE_INPUT_CAD:
1167 {
1168 pConsole->getKeyboard()->PutCAD();
1169 } break;
1170
1171 case VRDE_INPUT_RESET:
1172 {
1173 pConsole->Reset();
1174 } break;
1175
1176 case VRDE_INPUT_SYNCH:
1177 {
1178 if (cbInput == sizeof(VRDEINPUTSYNCH))
1179 {
1180 IKeyboard *pKeyboard = pConsole->getKeyboard();
1181
1182 const VRDEINPUTSYNCH *pInputSynch = (VRDEINPUTSYNCH *)pvInput;
1183
1184 server->m_InputSynch.fClientNumLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_NUMLOCK) != 0;
1185 server->m_InputSynch.fClientCapsLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_CAPITAL) != 0;
1186 server->m_InputSynch.fClientScrollLock = (pInputSynch->uLockStatus & VRDE_INPUT_SYNCH_SCROLL) != 0;
1187
1188 /* The client initiated synchronization. Always make the guest to reflect the client state.
1189 * Than means, when the guest changes the state itself, it is forced to return to the client
1190 * state.
1191 */
1192 if (server->m_InputSynch.fClientNumLock != server->m_InputSynch.fGuestNumLock)
1193 {
1194 server->m_InputSynch.cGuestNumLockAdaptions = 2;
1195 }
1196
1197 if (server->m_InputSynch.fClientCapsLock != server->m_InputSynch.fGuestCapsLock)
1198 {
1199 server->m_InputSynch.cGuestCapsLockAdaptions = 2;
1200 }
1201
1202 fixKbdLockStatus(&server->m_InputSynch, pKeyboard);
1203 }
1204 } break;
1205
1206 default:
1207 break;
1208 }
1209}
1210
1211DECLCALLBACK(void) ConsoleVRDPServer::VRDPCallbackVideoModeHint(void *pvCallback, unsigned cWidth, unsigned cHeight, unsigned cBitsPerPixel, unsigned uScreenId)
1212{
1213 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1214
1215 server->mConsole->getDisplay()->SetVideoModeHint(cWidth, cHeight, cBitsPerPixel, uScreenId);
1216}
1217
1218DECLCALLBACK(void) ConsoleVRDPServer::VRDECallbackAudioIn(void *pvCallback,
1219 void *pvCtx,
1220 uint32_t u32ClientId,
1221 uint32_t u32Event,
1222 const void *pvData,
1223 uint32_t cbData)
1224{
1225 ConsoleVRDPServer *server = static_cast<ConsoleVRDPServer*>(pvCallback);
1226
1227 PPDMIAUDIOSNIFFERPORT pPort = server->mConsole->getAudioSniffer()->getAudioSnifferPort();
1228
1229 switch (u32Event)
1230 {
1231 case VRDE_AUDIOIN_BEGIN:
1232 {
1233 const VRDEAUDIOINBEGIN *pParms = (const VRDEAUDIOINBEGIN *)pvData;
1234
1235 pPort->pfnAudioInputEventBegin (pPort, pvCtx,
1236 VRDE_AUDIO_FMT_SAMPLE_FREQ(pParms->fmt),
1237 VRDE_AUDIO_FMT_CHANNELS(pParms->fmt),
1238 VRDE_AUDIO_FMT_BITS_PER_SAMPLE(pParms->fmt),
1239 VRDE_AUDIO_FMT_SIGNED(pParms->fmt)
1240 );
1241 } break;
1242
1243 case VRDE_AUDIOIN_DATA:
1244 {
1245 pPort->pfnAudioInputEventData (pPort, pvCtx, pvData, cbData);
1246 } break;
1247
1248 case VRDE_AUDIOIN_END:
1249 {
1250 pPort->pfnAudioInputEventEnd (pPort, pvCtx);
1251 } break;
1252
1253 default:
1254 return;
1255 }
1256}
1257
1258
1259ConsoleVRDPServer::ConsoleVRDPServer(Console *console)
1260{
1261 mConsole = console;
1262
1263 int rc = RTCritSectInit(&mCritSect);
1264 AssertRC(rc);
1265
1266 mcClipboardRefs = 0;
1267 mpfnClipboardCallback = NULL;
1268
1269#ifdef VBOX_WITH_USB
1270 mUSBBackends.pHead = NULL;
1271 mUSBBackends.pTail = NULL;
1272
1273 mUSBBackends.thread = NIL_RTTHREAD;
1274 mUSBBackends.fThreadRunning = false;
1275 mUSBBackends.event = 0;
1276#endif
1277
1278 mhServer = 0;
1279 mServerInterfaceVersion = 0;
1280
1281 m_fGuestWantsAbsolute = false;
1282 m_mousex = 0;
1283 m_mousey = 0;
1284
1285 m_InputSynch.cGuestNumLockAdaptions = 2;
1286 m_InputSynch.cGuestCapsLockAdaptions = 2;
1287
1288 m_InputSynch.fGuestNumLock = false;
1289 m_InputSynch.fGuestCapsLock = false;
1290 m_InputSynch.fGuestScrollLock = false;
1291
1292 m_InputSynch.fClientNumLock = false;
1293 m_InputSynch.fClientCapsLock = false;
1294 m_InputSynch.fClientScrollLock = false;
1295
1296 memset(maFramebuffers, 0, sizeof(maFramebuffers));
1297
1298 {
1299 ComPtr<IEventSource> es;
1300 console->COMGETTER(EventSource)(es.asOutParam());
1301 ComObjPtr<VRDPConsoleListenerImpl> aConsoleListener;
1302 aConsoleListener.createObject();
1303 aConsoleListener->init(new VRDPConsoleListener(), this);
1304 mConsoleListener = aConsoleListener;
1305 com::SafeArray <VBoxEventType_T> eventTypes;
1306 eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
1307 eventTypes.push_back(VBoxEventType_OnMouseCapabilityChanged);
1308 eventTypes.push_back(VBoxEventType_OnKeyboardLedsChanged);
1309 es->RegisterListener(mConsoleListener, ComSafeArrayAsInParam(eventTypes), true);
1310 }
1311
1312 mVRDPBindPort = -1;
1313
1314 mAuthLibrary = 0;
1315
1316 mu32AudioInputClientId = 0;
1317}
1318
1319ConsoleVRDPServer::~ConsoleVRDPServer()
1320{
1321 Stop();
1322
1323 if (mConsoleListener)
1324 {
1325 ComPtr<IEventSource> es;
1326 mConsole->COMGETTER(EventSource)(es.asOutParam());
1327 es->UnregisterListener(mConsoleListener);
1328 mConsoleListener.setNull();
1329 }
1330
1331 unsigned i;
1332 for (i = 0; i < RT_ELEMENTS(maFramebuffers); i++)
1333 {
1334 if (maFramebuffers[i])
1335 {
1336 maFramebuffers[i]->Release();
1337 maFramebuffers[i] = NULL;
1338 }
1339 }
1340
1341 if (RTCritSectIsInitialized(&mCritSect))
1342 {
1343 RTCritSectDelete(&mCritSect);
1344 memset(&mCritSect, 0, sizeof(mCritSect));
1345 }
1346}
1347
1348int ConsoleVRDPServer::Launch(void)
1349{
1350 LogFlowThisFunc(("\n"));
1351
1352 IVRDEServer *server = mConsole->getVRDEServer();
1353 AssertReturn(server, VERR_INTERNAL_ERROR_2);
1354
1355 /*
1356 * Check if VRDE is enabled.
1357 */
1358 BOOL fEnabled;
1359 HRESULT hrc = server->COMGETTER(Enabled)(&fEnabled);
1360 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1361 if (!fEnabled)
1362 return VINF_SUCCESS;
1363
1364 /*
1365 * Check that a VRDE extension pack name is set and resolve it into a
1366 * library path.
1367 */
1368 Bstr bstrExtPack;
1369 hrc = server->COMGETTER(VRDEExtPack)(bstrExtPack.asOutParam());
1370 if (FAILED(hrc))
1371 return Global::vboxStatusCodeFromCOM(hrc);
1372 if (bstrExtPack.isEmpty())
1373 return VINF_NOT_SUPPORTED;
1374
1375 Utf8Str strExtPack(bstrExtPack);
1376 Utf8Str strVrdeLibrary;
1377 int vrc = VINF_SUCCESS;
1378 if (strExtPack.equals(VBOXVRDP_KLUDGE_EXTPACK_NAME))
1379 strVrdeLibrary = "VBoxVRDP";
1380 else
1381 {
1382#ifdef VBOX_WITH_EXTPACK
1383 ExtPackManager *pExtPackMgr = mConsole->getExtPackManager();
1384 vrc = pExtPackMgr->getVrdeLibraryPathForExtPack(&strExtPack, &strVrdeLibrary);
1385#else
1386 vrc = VERR_FILE_NOT_FOUND;
1387#endif
1388 }
1389 if (RT_SUCCESS(vrc))
1390 {
1391 /*
1392 * Load the VRDE library and start the server, if it is enabled.
1393 */
1394 vrc = loadVRDPLibrary(strVrdeLibrary.c_str());
1395 if (RT_SUCCESS(vrc))
1396 {
1397 VRDEENTRYPOINTS_3 *pEntryPoints3;
1398 vrc = mpfnVRDECreateServer(&mCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints3, &mhServer);
1399
1400 if (RT_SUCCESS(vrc))
1401 {
1402 mServerInterfaceVersion = 3;
1403 mEntryPoints = *pEntryPoints3;
1404 mpEntryPoints = &mEntryPoints;
1405 }
1406 else if (vrc == VERR_VERSION_MISMATCH)
1407 {
1408 /* An older version of VRDE is installed, try version 1. */
1409 VRDEENTRYPOINTS_1 *pEntryPoints1;
1410
1411 static VRDECALLBACKS_1 sCallbacks =
1412 {
1413 { VRDE_INTERFACE_VERSION_1, sizeof(VRDECALLBACKS_1) },
1414 ConsoleVRDPServer::VRDPCallbackQueryProperty,
1415 ConsoleVRDPServer::VRDPCallbackClientLogon,
1416 ConsoleVRDPServer::VRDPCallbackClientConnect,
1417 ConsoleVRDPServer::VRDPCallbackClientDisconnect,
1418 ConsoleVRDPServer::VRDPCallbackIntercept,
1419 ConsoleVRDPServer::VRDPCallbackUSB,
1420 ConsoleVRDPServer::VRDPCallbackClipboard,
1421 ConsoleVRDPServer::VRDPCallbackFramebufferQuery,
1422 ConsoleVRDPServer::VRDPCallbackFramebufferLock,
1423 ConsoleVRDPServer::VRDPCallbackFramebufferUnlock,
1424 ConsoleVRDPServer::VRDPCallbackInput,
1425 ConsoleVRDPServer::VRDPCallbackVideoModeHint
1426 };
1427
1428 vrc = mpfnVRDECreateServer(&sCallbacks.header, this, (VRDEINTERFACEHDR **)&pEntryPoints1, &mhServer);
1429 if (RT_SUCCESS(vrc))
1430 {
1431 LogRel(("VRDE: loaded an older version of the server.\n"));
1432
1433 mServerInterfaceVersion = 3;
1434 mEntryPoints.header = pEntryPoints1->header;
1435 mEntryPoints.VRDEDestroy = pEntryPoints1->VRDEDestroy;
1436 mEntryPoints.VRDEEnableConnections = pEntryPoints1->VRDEEnableConnections;
1437 mEntryPoints.VRDEDisconnect = pEntryPoints1->VRDEDisconnect;
1438 mEntryPoints.VRDEResize = pEntryPoints1->VRDEResize;
1439 mEntryPoints.VRDEUpdate = pEntryPoints1->VRDEUpdate;
1440 mEntryPoints.VRDEColorPointer = pEntryPoints1->VRDEColorPointer;
1441 mEntryPoints.VRDEHidePointer = pEntryPoints1->VRDEHidePointer;
1442 mEntryPoints.VRDEAudioSamples = pEntryPoints1->VRDEAudioSamples;
1443 mEntryPoints.VRDEAudioVolume = pEntryPoints1->VRDEAudioVolume;
1444 mEntryPoints.VRDEUSBRequest = pEntryPoints1->VRDEUSBRequest;
1445 mEntryPoints.VRDEClipboard = pEntryPoints1->VRDEClipboard;
1446 mEntryPoints.VRDEQueryInfo = pEntryPoints1->VRDEQueryInfo;
1447 mEntryPoints.VRDERedirect = NULL;
1448 mEntryPoints.VRDEAudioInOpen = NULL;
1449 mEntryPoints.VRDEAudioInClose = NULL;
1450 mpEntryPoints = &mEntryPoints;
1451 }
1452 }
1453
1454 if (RT_SUCCESS(vrc))
1455 {
1456#ifdef VBOX_WITH_USB
1457 remoteUSBThreadStart();
1458#endif
1459 }
1460 else
1461 {
1462 if (vrc != VERR_NET_ADDRESS_IN_USE)
1463 LogRel(("VRDE: Could not start the server rc = %Rrc\n", vrc));
1464 /* Don't unload the lib, because it prevents us trying again or
1465 because there may be other users? */
1466 }
1467 }
1468 }
1469
1470 return vrc;
1471}
1472
1473void ConsoleVRDPServer::EnableConnections(void)
1474{
1475 if (mpEntryPoints && mhServer)
1476 {
1477 mpEntryPoints->VRDEEnableConnections(mhServer, true);
1478 }
1479}
1480
1481void ConsoleVRDPServer::DisconnectClient(uint32_t u32ClientId, bool fReconnect)
1482{
1483 if (mpEntryPoints && mhServer)
1484 {
1485 mpEntryPoints->VRDEDisconnect(mhServer, u32ClientId, fReconnect);
1486 }
1487}
1488
1489void ConsoleVRDPServer::MousePointerUpdate(const VRDECOLORPOINTER *pPointer)
1490{
1491 if (mpEntryPoints && mhServer)
1492 {
1493 mpEntryPoints->VRDEColorPointer(mhServer, pPointer);
1494 }
1495}
1496
1497void ConsoleVRDPServer::MousePointerHide(void)
1498{
1499 if (mpEntryPoints && mhServer)
1500 {
1501 mpEntryPoints->VRDEHidePointer(mhServer);
1502 }
1503}
1504
1505void ConsoleVRDPServer::Stop(void)
1506{
1507 Assert(VALID_PTR(this)); /** @todo r=bird: there are(/was) some odd cases where this buster was invalid on
1508 * linux. Just remove this when it's 100% sure that problem has been fixed. */
1509 if (mhServer)
1510 {
1511 HVRDESERVER hServer = mhServer;
1512
1513 /* Reset the handle to avoid further calls to the server. */
1514 mhServer = 0;
1515
1516 if (mpEntryPoints && hServer)
1517 {
1518 mpEntryPoints->VRDEDestroy(hServer);
1519 }
1520 }
1521
1522#ifdef VBOX_WITH_USB
1523 remoteUSBThreadStop();
1524#endif /* VBOX_WITH_USB */
1525
1526 mpfnAuthEntry = NULL;
1527 mpfnAuthEntry2 = NULL;
1528 mpfnAuthEntry3 = NULL;
1529
1530 if (mAuthLibrary)
1531 {
1532 RTLdrClose(mAuthLibrary);
1533 mAuthLibrary = 0;
1534 }
1535}
1536
1537/* Worker thread for Remote USB. The thread polls the clients for
1538 * the list of attached USB devices.
1539 * The thread is also responsible for attaching/detaching devices
1540 * to/from the VM.
1541 *
1542 * It is expected that attaching/detaching is not a frequent operation.
1543 *
1544 * The thread is always running when the VRDP server is active.
1545 *
1546 * The thread scans backends and requests the device list every 2 seconds.
1547 *
1548 * When device list is available, the thread calls the Console to process it.
1549 *
1550 */
1551#define VRDP_DEVICE_LIST_PERIOD_MS (2000)
1552
1553#ifdef VBOX_WITH_USB
1554static DECLCALLBACK(int) threadRemoteUSB(RTTHREAD self, void *pvUser)
1555{
1556 ConsoleVRDPServer *pOwner = (ConsoleVRDPServer *)pvUser;
1557
1558 LogFlow(("Console::threadRemoteUSB: start. owner = %p.\n", pOwner));
1559
1560 pOwner->notifyRemoteUSBThreadRunning(self);
1561
1562 while (pOwner->isRemoteUSBThreadRunning())
1563 {
1564 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1565
1566 while ((pRemoteUSBBackend = pOwner->usbBackendGetNext(pRemoteUSBBackend)) != NULL)
1567 {
1568 pRemoteUSBBackend->PollRemoteDevices();
1569 }
1570
1571 pOwner->waitRemoteUSBThreadEvent(VRDP_DEVICE_LIST_PERIOD_MS);
1572
1573 LogFlow(("Console::threadRemoteUSB: iteration. owner = %p.\n", pOwner));
1574 }
1575
1576 return VINF_SUCCESS;
1577}
1578
1579void ConsoleVRDPServer::notifyRemoteUSBThreadRunning(RTTHREAD thread)
1580{
1581 mUSBBackends.thread = thread;
1582 mUSBBackends.fThreadRunning = true;
1583 int rc = RTThreadUserSignal(thread);
1584 AssertRC(rc);
1585}
1586
1587bool ConsoleVRDPServer::isRemoteUSBThreadRunning(void)
1588{
1589 return mUSBBackends.fThreadRunning;
1590}
1591
1592void ConsoleVRDPServer::waitRemoteUSBThreadEvent(RTMSINTERVAL cMillies)
1593{
1594 int rc = RTSemEventWait(mUSBBackends.event, cMillies);
1595 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1596 NOREF(rc);
1597}
1598
1599void ConsoleVRDPServer::remoteUSBThreadStart(void)
1600{
1601 int rc = RTSemEventCreate(&mUSBBackends.event);
1602
1603 if (RT_FAILURE(rc))
1604 {
1605 AssertFailed();
1606 mUSBBackends.event = 0;
1607 }
1608
1609 if (RT_SUCCESS(rc))
1610 {
1611 rc = RTThreadCreate(&mUSBBackends.thread, threadRemoteUSB, this, 65536,
1612 RTTHREADTYPE_VRDP_IO, RTTHREADFLAGS_WAITABLE, "remote usb");
1613 }
1614
1615 if (RT_FAILURE(rc))
1616 {
1617 LogRel(("Warning: could not start the remote USB thread, rc = %Rrc!!!\n", rc));
1618 mUSBBackends.thread = NIL_RTTHREAD;
1619 }
1620 else
1621 {
1622 /* Wait until the thread is ready. */
1623 rc = RTThreadUserWait(mUSBBackends.thread, 60000);
1624 AssertRC(rc);
1625 Assert (mUSBBackends.fThreadRunning || RT_FAILURE(rc));
1626 }
1627}
1628
1629void ConsoleVRDPServer::remoteUSBThreadStop(void)
1630{
1631 mUSBBackends.fThreadRunning = false;
1632
1633 if (mUSBBackends.thread != NIL_RTTHREAD)
1634 {
1635 Assert (mUSBBackends.event != 0);
1636
1637 RTSemEventSignal(mUSBBackends.event);
1638
1639 int rc = RTThreadWait(mUSBBackends.thread, 60000, NULL);
1640 AssertRC(rc);
1641
1642 mUSBBackends.thread = NIL_RTTHREAD;
1643 }
1644
1645 if (mUSBBackends.event)
1646 {
1647 RTSemEventDestroy(mUSBBackends.event);
1648 mUSBBackends.event = 0;
1649 }
1650}
1651#endif /* VBOX_WITH_USB */
1652
1653AuthResult ConsoleVRDPServer::Authenticate(const Guid &uuid, AuthGuestJudgement guestJudgement,
1654 const char *pszUser, const char *pszPassword, const char *pszDomain,
1655 uint32_t u32ClientId)
1656{
1657 AUTHUUID rawuuid;
1658
1659 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1660
1661 LogFlow(("ConsoleVRDPServer::Authenticate: uuid = %RTuuid, guestJudgement = %d, pszUser = %s, pszPassword = %s, pszDomain = %s, u32ClientId = %d\n",
1662 rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, u32ClientId));
1663
1664 /*
1665 * Called only from VRDP input thread. So thread safety is not required.
1666 */
1667
1668 if (!mAuthLibrary)
1669 {
1670 /* Load the external authentication library. */
1671 Bstr authLibrary;
1672 mConsole->getVRDEServer()->COMGETTER(AuthLibrary)(authLibrary.asOutParam());
1673
1674 Utf8Str filename = authLibrary;
1675
1676 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
1677
1678 int rc;
1679 if (RTPathHavePath(filename.c_str()))
1680 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
1681 else
1682 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
1683
1684 if (RT_FAILURE(rc))
1685 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
1686
1687 if (RT_SUCCESS(rc))
1688 {
1689 typedef struct AuthEntryInfo
1690 {
1691 const char *pszName;
1692 void **ppvAddress;
1693
1694 } AuthEntryInfo;
1695 AuthEntryInfo entries[] =
1696 {
1697 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
1698 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
1699 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
1700 { NULL, NULL }
1701 };
1702
1703 /* Get the entry point. */
1704 AuthEntryInfo *pEntryInfo = &entries[0];
1705 while (pEntryInfo->pszName)
1706 {
1707 *pEntryInfo->ppvAddress = NULL;
1708
1709 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
1710 if (RT_SUCCESS(rc2))
1711 {
1712 /* Found an entry point. */
1713 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
1714 rc = VINF_SUCCESS;
1715 break;
1716 }
1717
1718 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1719 {
1720 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
1721 }
1722 rc = rc2;
1723
1724 pEntryInfo++;
1725 }
1726 }
1727
1728 if (RT_FAILURE(rc))
1729 {
1730 mConsole->setError(E_FAIL,
1731 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
1732 filename.c_str(),
1733 rc);
1734
1735 mpfnAuthEntry = NULL;
1736 mpfnAuthEntry2 = NULL;
1737 mpfnAuthEntry3 = NULL;
1738
1739 if (mAuthLibrary)
1740 {
1741 RTLdrClose(mAuthLibrary);
1742 mAuthLibrary = 0;
1743 }
1744
1745 return AuthResultAccessDenied;
1746 }
1747 }
1748
1749 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
1750
1751 AuthResult result = AuthResultAccessDenied;
1752 if (mpfnAuthEntry3)
1753 {
1754 result = mpfnAuthEntry3("vrde", &rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
1755 }
1756 else if (mpfnAuthEntry2)
1757 {
1758 result = mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
1759 }
1760 else if (mpfnAuthEntry)
1761 {
1762 result = mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
1763 }
1764
1765 switch (result)
1766 {
1767 case AuthResultAccessDenied:
1768 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
1769 break;
1770 case AuthResultAccessGranted:
1771 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
1772 break;
1773 case AuthResultDelegateToGuest:
1774 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
1775 break;
1776 default:
1777 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
1778 result = AuthResultAccessDenied;
1779 }
1780
1781 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
1782
1783 return result;
1784}
1785
1786void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
1787{
1788 AUTHUUID rawuuid;
1789
1790 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1791
1792 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
1793 rawuuid, u32ClientId));
1794
1795 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
1796
1797 if (mpfnAuthEntry3)
1798 mpfnAuthEntry3("vrde", &rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1799 else if (mpfnAuthEntry2)
1800 mpfnAuthEntry2(&rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1801}
1802
1803int ConsoleVRDPServer::lockConsoleVRDPServer(void)
1804{
1805 int rc = RTCritSectEnter(&mCritSect);
1806 AssertRC(rc);
1807 return rc;
1808}
1809
1810void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
1811{
1812 RTCritSectLeave(&mCritSect);
1813}
1814
1815DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
1816 uint32_t u32ClientId,
1817 uint32_t u32Function,
1818 uint32_t u32Format,
1819 const void *pvData,
1820 uint32_t cbData)
1821{
1822 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
1823 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
1824
1825 int rc = VINF_SUCCESS;
1826
1827 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
1828
1829 NOREF(u32ClientId);
1830
1831 switch (u32Function)
1832 {
1833 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
1834 {
1835 if (pServer->mpfnClipboardCallback)
1836 {
1837 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1838 u32Format,
1839 (void *)pvData,
1840 cbData);
1841 }
1842 } break;
1843
1844 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
1845 {
1846 if (pServer->mpfnClipboardCallback)
1847 {
1848 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1849 u32Format,
1850 (void *)pvData,
1851 cbData);
1852 }
1853 } break;
1854
1855 default:
1856 rc = VERR_NOT_SUPPORTED;
1857 }
1858
1859 return rc;
1860}
1861
1862DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
1863 uint32_t u32Function,
1864 void *pvParms,
1865 uint32_t cbParms)
1866{
1867 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
1868 pvExtension, u32Function, pvParms, cbParms));
1869
1870 int rc = VINF_SUCCESS;
1871
1872 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
1873
1874 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
1875
1876 switch (u32Function)
1877 {
1878 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
1879 {
1880 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
1881 } break;
1882
1883 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1884 {
1885 /* The guest announces clipboard formats. This must be delivered to all clients. */
1886 if (mpEntryPoints && pServer->mhServer)
1887 {
1888 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1889 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
1890 pParms->u32Format,
1891 NULL,
1892 0,
1893 NULL);
1894 }
1895 } break;
1896
1897 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1898 {
1899 /* The clipboard service expects that the pvData buffer will be filled
1900 * with clipboard data. The server returns the data from the client that
1901 * announced the requested format most recently.
1902 */
1903 if (mpEntryPoints && pServer->mhServer)
1904 {
1905 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1906 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
1907 pParms->u32Format,
1908 pParms->u.pvData,
1909 pParms->cbData,
1910 &pParms->cbData);
1911 }
1912 } break;
1913
1914 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
1915 {
1916 if (mpEntryPoints && pServer->mhServer)
1917 {
1918 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1919 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
1920 pParms->u32Format,
1921 pParms->u.pvData,
1922 pParms->cbData,
1923 NULL);
1924 }
1925 } break;
1926
1927 default:
1928 rc = VERR_NOT_SUPPORTED;
1929 }
1930
1931 return rc;
1932}
1933
1934void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
1935{
1936 int rc = lockConsoleVRDPServer();
1937
1938 if (RT_SUCCESS(rc))
1939 {
1940 if (mcClipboardRefs == 0)
1941 {
1942 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
1943
1944 if (RT_SUCCESS(rc))
1945 {
1946 mcClipboardRefs++;
1947 }
1948 }
1949
1950 unlockConsoleVRDPServer();
1951 }
1952}
1953
1954void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
1955{
1956 int rc = lockConsoleVRDPServer();
1957
1958 if (RT_SUCCESS(rc))
1959 {
1960 mcClipboardRefs--;
1961
1962 if (mcClipboardRefs == 0)
1963 {
1964 HGCMHostUnregisterServiceExtension(mhClipboard);
1965 }
1966
1967 unlockConsoleVRDPServer();
1968 }
1969}
1970
1971/* That is called on INPUT thread of the VRDP server.
1972 * The ConsoleVRDPServer keeps a list of created backend instances.
1973 */
1974void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
1975{
1976#ifdef VBOX_WITH_USB
1977 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
1978
1979 /* Create a new instance of the USB backend for the new client. */
1980 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
1981
1982 if (pRemoteUSBBackend)
1983 {
1984 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
1985
1986 /* Append the new instance in the list. */
1987 int rc = lockConsoleVRDPServer();
1988
1989 if (RT_SUCCESS(rc))
1990 {
1991 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
1992 if (mUSBBackends.pHead)
1993 {
1994 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
1995 }
1996 else
1997 {
1998 mUSBBackends.pTail = pRemoteUSBBackend;
1999 }
2000
2001 mUSBBackends.pHead = pRemoteUSBBackend;
2002
2003 unlockConsoleVRDPServer();
2004
2005 if (ppvIntercept)
2006 {
2007 *ppvIntercept = pRemoteUSBBackend;
2008 }
2009 }
2010
2011 if (RT_FAILURE(rc))
2012 {
2013 pRemoteUSBBackend->Release();
2014 }
2015 }
2016#endif /* VBOX_WITH_USB */
2017}
2018
2019void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
2020{
2021#ifdef VBOX_WITH_USB
2022 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
2023
2024 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2025
2026 /* Find the instance. */
2027 int rc = lockConsoleVRDPServer();
2028
2029 if (RT_SUCCESS(rc))
2030 {
2031 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2032
2033 if (pRemoteUSBBackend)
2034 {
2035 /* Notify that it will be deleted. */
2036 pRemoteUSBBackend->NotifyDelete();
2037 }
2038
2039 unlockConsoleVRDPServer();
2040 }
2041
2042 if (pRemoteUSBBackend)
2043 {
2044 /* Here the instance has been excluded from the list and can be dereferenced. */
2045 pRemoteUSBBackend->Release();
2046 }
2047#endif
2048}
2049
2050void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
2051{
2052#ifdef VBOX_WITH_USB
2053 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2054
2055 /* Find the instance. */
2056 int rc = lockConsoleVRDPServer();
2057
2058 if (RT_SUCCESS(rc))
2059 {
2060 pRemoteUSBBackend = usbBackendFind(u32ClientId);
2061
2062 if (pRemoteUSBBackend)
2063 {
2064 /* Inform the backend instance that it is referenced by the Guid. */
2065 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
2066
2067 if (fAdded)
2068 {
2069 /* Reference the instance because its pointer is being taken. */
2070 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
2071 }
2072 else
2073 {
2074 pRemoteUSBBackend = NULL;
2075 }
2076 }
2077
2078 unlockConsoleVRDPServer();
2079 }
2080
2081 if (pRemoteUSBBackend)
2082 {
2083 return pRemoteUSBBackend->GetBackendCallbackPointer();
2084 }
2085
2086#endif
2087 return NULL;
2088}
2089
2090void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
2091{
2092#ifdef VBOX_WITH_USB
2093 RemoteUSBBackend *pRemoteUSBBackend = NULL;
2094
2095 /* Find the instance. */
2096 int rc = lockConsoleVRDPServer();
2097
2098 if (RT_SUCCESS(rc))
2099 {
2100 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
2101
2102 if (pRemoteUSBBackend)
2103 {
2104 pRemoteUSBBackend->removeUUID(pGuid);
2105 }
2106
2107 unlockConsoleVRDPServer();
2108
2109 if (pRemoteUSBBackend)
2110 {
2111 pRemoteUSBBackend->Release();
2112 }
2113 }
2114#endif
2115}
2116
2117RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
2118{
2119 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
2120
2121 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
2122#ifdef VBOX_WITH_USB
2123
2124 int rc = lockConsoleVRDPServer();
2125
2126 if (RT_SUCCESS(rc))
2127 {
2128 if (pRemoteUSBBackend == NULL)
2129 {
2130 /* The first backend in the list is requested. */
2131 pNextRemoteUSBBackend = mUSBBackends.pHead;
2132 }
2133 else
2134 {
2135 /* Get pointer to the next backend. */
2136 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2137 }
2138
2139 if (pNextRemoteUSBBackend)
2140 {
2141 pNextRemoteUSBBackend->AddRef();
2142 }
2143
2144 unlockConsoleVRDPServer();
2145
2146 if (pRemoteUSBBackend)
2147 {
2148 pRemoteUSBBackend->Release();
2149 }
2150 }
2151#endif
2152
2153 return pNextRemoteUSBBackend;
2154}
2155
2156#ifdef VBOX_WITH_USB
2157/* Internal method. Called under the ConsoleVRDPServerLock. */
2158RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
2159{
2160 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2161
2162 while (pRemoteUSBBackend)
2163 {
2164 if (pRemoteUSBBackend->ClientId() == u32ClientId)
2165 {
2166 break;
2167 }
2168
2169 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2170 }
2171
2172 return pRemoteUSBBackend;
2173}
2174
2175/* Internal method. Called under the ConsoleVRDPServerLock. */
2176RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
2177{
2178 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2179
2180 while (pRemoteUSBBackend)
2181 {
2182 if (pRemoteUSBBackend->findUUID(pGuid))
2183 {
2184 break;
2185 }
2186
2187 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2188 }
2189
2190 return pRemoteUSBBackend;
2191}
2192#endif
2193
2194/* Internal method. Called by the backend destructor. */
2195void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2196{
2197#ifdef VBOX_WITH_USB
2198 int rc = lockConsoleVRDPServer();
2199 AssertRC(rc);
2200
2201 /* Exclude the found instance from the list. */
2202 if (pRemoteUSBBackend->pNext)
2203 {
2204 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2205 }
2206 else
2207 {
2208 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2209 }
2210
2211 if (pRemoteUSBBackend->pPrev)
2212 {
2213 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2214 }
2215 else
2216 {
2217 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2218 }
2219
2220 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2221
2222 unlockConsoleVRDPServer();
2223#endif
2224}
2225
2226
2227void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2228{
2229 if (mpEntryPoints && mhServer)
2230 {
2231 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2232 }
2233}
2234
2235void ConsoleVRDPServer::SendResize(void) const
2236{
2237 if (mpEntryPoints && mhServer)
2238 {
2239 mpEntryPoints->VRDEResize(mhServer);
2240 }
2241}
2242
2243void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2244{
2245 VRDEORDERHDR update;
2246 update.x = x;
2247 update.y = y;
2248 update.w = w;
2249 update.h = h;
2250 if (mpEntryPoints && mhServer)
2251 {
2252 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
2253 }
2254}
2255
2256void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
2257{
2258 if (mpEntryPoints && mhServer)
2259 {
2260 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
2261 }
2262}
2263
2264void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2265{
2266 if (mpEntryPoints && mhServer)
2267 {
2268 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
2269 }
2270}
2271
2272void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2273{
2274 if (mpEntryPoints && mhServer)
2275 {
2276 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2277 }
2278}
2279
2280/* @todo rc not needed? */
2281int ConsoleVRDPServer::SendAudioInputBegin(void **ppvUserCtx,
2282 void *pvContext,
2283 uint32_t cSamples,
2284 uint32_t iSampleHz,
2285 uint32_t cChannels,
2286 uint32_t cBits)
2287{
2288 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInOpen)
2289 {
2290 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2291 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2292 {
2293 VRDEAUDIOFORMAT audioFormat = VRDE_AUDIO_FMT_MAKE(iSampleHz, cChannels, cBits, 0);
2294 mpEntryPoints->VRDEAudioInOpen (mhServer,
2295 pvContext,
2296 u32ClientId,
2297 audioFormat,
2298 cSamples);
2299 *ppvUserCtx = NULL; /* This is the ConsoleVRDPServer context.
2300 * Currently not used because only one client is allowed to
2301 * do audio input and the client id is saved by the ConsoleVRDPServer.
2302 */
2303
2304 return VINF_SUCCESS;
2305 }
2306 }
2307 return VERR_NOT_SUPPORTED;
2308}
2309
2310void ConsoleVRDPServer::SendAudioInputEnd(void *pvUserCtx)
2311{
2312 if (mpEntryPoints && mhServer && mpEntryPoints->VRDEAudioInClose)
2313 {
2314 uint32_t u32ClientId = ASMAtomicReadU32(&mu32AudioInputClientId);
2315 if (u32ClientId != 0) /* 0 would mean broadcast to all clients. */
2316 {
2317 mpEntryPoints->VRDEAudioInClose(mhServer, u32ClientId);
2318 }
2319 }
2320}
2321
2322
2323
2324void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2325{
2326 if (index == VRDE_QI_PORT)
2327 {
2328 uint32_t cbOut = sizeof(int32_t);
2329
2330 if (cbBuffer >= cbOut)
2331 {
2332 *pcbOut = cbOut;
2333 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2334 }
2335 }
2336 else if (mpEntryPoints && mhServer)
2337 {
2338 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2339 }
2340}
2341
2342/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
2343{
2344 int rc = VINF_SUCCESS;
2345
2346 if (mVRDPLibrary == NIL_RTLDRMOD)
2347 {
2348 RTERRINFOSTATIC ErrInfo;
2349 RTErrInfoInitStatic(&ErrInfo);
2350
2351 if (RTPathHavePath(pszLibraryName))
2352 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, &ErrInfo.Core);
2353 else
2354 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary, RTLDRLOAD_FLAGS_LOCAL, &ErrInfo.Core);
2355 if (RT_SUCCESS(rc))
2356 {
2357 struct SymbolEntry
2358 {
2359 const char *name;
2360 void **ppfn;
2361 };
2362
2363 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2364
2365 static const struct SymbolEntry s_aSymbols[] =
2366 {
2367 DEFSYMENTRY(VRDECreateServer)
2368 };
2369
2370 #undef DEFSYMENTRY
2371
2372 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
2373 {
2374 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
2375
2376 if (RT_FAILURE(rc))
2377 {
2378 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
2379 break;
2380 }
2381 }
2382 }
2383 else
2384 {
2385 if (RTErrInfoIsSet(&ErrInfo.Core))
2386 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, ErrInfo.Core.pszMsg, rc));
2387 else
2388 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
2389
2390 mVRDPLibrary = NIL_RTLDRMOD;
2391 }
2392 }
2393
2394 if (RT_FAILURE(rc))
2395 {
2396 if (mVRDPLibrary != NIL_RTLDRMOD)
2397 {
2398 RTLdrClose(mVRDPLibrary);
2399 mVRDPLibrary = NIL_RTLDRMOD;
2400 }
2401 }
2402
2403 return rc;
2404}
2405
2406/*
2407 * IVRDEServerInfo implementation.
2408 */
2409// constructor / destructor
2410/////////////////////////////////////////////////////////////////////////////
2411
2412VRDEServerInfo::VRDEServerInfo()
2413 : mParent(NULL)
2414{
2415}
2416
2417VRDEServerInfo::~VRDEServerInfo()
2418{
2419}
2420
2421
2422HRESULT VRDEServerInfo::FinalConstruct()
2423{
2424 return BaseFinalConstruct();
2425}
2426
2427void VRDEServerInfo::FinalRelease()
2428{
2429 uninit();
2430 BaseFinalRelease();
2431}
2432
2433// public methods only for internal purposes
2434/////////////////////////////////////////////////////////////////////////////
2435
2436/**
2437 * Initializes the guest object.
2438 */
2439HRESULT VRDEServerInfo::init(Console *aParent)
2440{
2441 LogFlowThisFunc(("aParent=%p\n", aParent));
2442
2443 ComAssertRet(aParent, E_INVALIDARG);
2444
2445 /* Enclose the state transition NotReady->InInit->Ready */
2446 AutoInitSpan autoInitSpan(this);
2447 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2448
2449 unconst(mParent) = aParent;
2450
2451 /* Confirm a successful initialization */
2452 autoInitSpan.setSucceeded();
2453
2454 return S_OK;
2455}
2456
2457/**
2458 * Uninitializes the instance and sets the ready flag to FALSE.
2459 * Called either from FinalRelease() or by the parent when it gets destroyed.
2460 */
2461void VRDEServerInfo::uninit()
2462{
2463 LogFlowThisFunc(("\n"));
2464
2465 /* Enclose the state transition Ready->InUninit->NotReady */
2466 AutoUninitSpan autoUninitSpan(this);
2467 if (autoUninitSpan.uninitDone())
2468 return;
2469
2470 unconst(mParent) = NULL;
2471}
2472
2473// IVRDEServerInfo properties
2474/////////////////////////////////////////////////////////////////////////////
2475
2476#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2477 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2478 { \
2479 if (!a##_aName) \
2480 return E_POINTER; \
2481 \
2482 AutoCaller autoCaller(this); \
2483 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2484 \
2485 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2486 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2487 \
2488 uint32_t value; \
2489 uint32_t cbOut = 0; \
2490 \
2491 mParent->consoleVRDPServer()->QueryInfo \
2492 (_aIndex, &value, sizeof(value), &cbOut); \
2493 \
2494 *a##_aName = cbOut? !!value: FALSE; \
2495 \
2496 return S_OK; \
2497 } \
2498 extern void IMPL_GETTER_BOOL_DUMMY(void)
2499
2500#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
2501 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2502 { \
2503 if (!a##_aName) \
2504 return E_POINTER; \
2505 \
2506 AutoCaller autoCaller(this); \
2507 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2508 \
2509 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2510 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2511 \
2512 _aType value; \
2513 uint32_t cbOut = 0; \
2514 \
2515 mParent->consoleVRDPServer()->QueryInfo \
2516 (_aIndex, &value, sizeof(value), &cbOut); \
2517 \
2518 if (_aValueMask) value &= (_aValueMask); \
2519 *a##_aName = cbOut? value: 0; \
2520 \
2521 return S_OK; \
2522 } \
2523 extern void IMPL_GETTER_SCALAR_DUMMY(void)
2524
2525#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2526 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2527 { \
2528 if (!a##_aName) \
2529 return E_POINTER; \
2530 \
2531 AutoCaller autoCaller(this); \
2532 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2533 \
2534 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2535 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2536 \
2537 uint32_t cbOut = 0; \
2538 \
2539 mParent->consoleVRDPServer()->QueryInfo \
2540 (_aIndex, NULL, 0, &cbOut); \
2541 \
2542 if (cbOut == 0) \
2543 { \
2544 Bstr str(""); \
2545 str.cloneTo(a##_aName); \
2546 return S_OK; \
2547 } \
2548 \
2549 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
2550 \
2551 if (!pchBuffer) \
2552 { \
2553 Log(("VRDEServerInfo::" \
2554 #_aName \
2555 ": Failed to allocate memory %d bytes\n", cbOut)); \
2556 return E_OUTOFMEMORY; \
2557 } \
2558 \
2559 mParent->consoleVRDPServer()->QueryInfo \
2560 (_aIndex, pchBuffer, cbOut, &cbOut); \
2561 \
2562 Bstr str(pchBuffer); \
2563 \
2564 str.cloneTo(a##_aName); \
2565 \
2566 RTMemTmpFree(pchBuffer); \
2567 \
2568 return S_OK; \
2569 } \
2570 extern void IMPL_GETTER_BSTR_DUMMY(void)
2571
2572IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
2573IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
2574IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
2575IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
2576IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
2577IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
2578IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
2579IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
2580IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
2581IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
2582IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
2583IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
2584IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
2585IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
2586IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
2587
2588#undef IMPL_GETTER_BSTR
2589#undef IMPL_GETTER_SCALAR
2590#undef IMPL_GETTER_BOOL
2591/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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