VirtualBox

source: vbox/trunk/src/VBox/Main/ConsoleVRDPServer.cpp@ 34575

Last change on this file since 34575 was 34574, checked in by vboxsync, 14 years ago

Make vrde auth library configurable per VM.

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