VirtualBox

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

最後變更 在這個檔案從34559是 34559,由 vboxsync 提交於 14 年 前

Updated VBox authentication library interface, removed references to VRDP (update).

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 73.5 KB
 
1/* $Id: ConsoleVRDPServer.cpp 34559 2010-12-01 11:04:48Z 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.alldomusa.eu.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
1493 ComPtr<IMachine> machine;
1494 mConsole->COMGETTER(Machine)(machine.asOutParam());
1495
1496 ComPtr<IVirtualBox> virtualBox;
1497 machine->COMGETTER(Parent)(virtualBox.asOutParam());
1498
1499 ComPtr<ISystemProperties> systemProperties;
1500 virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
1501
1502 Bstr authLibrary;
1503 systemProperties->COMGETTER(VRDEAuthLibrary)(authLibrary.asOutParam());
1504
1505 Utf8Str filename = authLibrary;
1506
1507 LogRel(("AUTH: ConsoleVRDPServer::Authenticate: loading external authentication library '%ls'\n", authLibrary.raw()));
1508
1509 int rc;
1510 if (RTPathHavePath(filename.c_str()))
1511 rc = RTLdrLoad(filename.c_str(), &mAuthLibrary);
1512 else
1513 rc = RTLdrLoadAppPriv(filename.c_str(), &mAuthLibrary);
1514
1515 if (RT_FAILURE(rc))
1516 LogRel(("AUTH: Failed to load external authentication library. Error code: %Rrc\n", rc));
1517
1518 if (RT_SUCCESS(rc))
1519 {
1520 typedef struct AuthEntryInfo
1521 {
1522 const char *pszName;
1523 void **ppvAddress;
1524
1525 } AuthEntryInfo;
1526 AuthEntryInfo entries[] =
1527 {
1528 { AUTHENTRY3_NAME, (void **)&mpfnAuthEntry3 },
1529 { AUTHENTRY2_NAME, (void **)&mpfnAuthEntry2 },
1530 { AUTHENTRY_NAME, (void **)&mpfnAuthEntry },
1531 { NULL, NULL }
1532 };
1533
1534 /* Get the entry point. */
1535 AuthEntryInfo *pEntryInfo = &entries[0];
1536 while (pEntryInfo->pszName)
1537 {
1538 *pEntryInfo->ppvAddress = NULL;
1539
1540 int rc2 = RTLdrGetSymbol(mAuthLibrary, pEntryInfo->pszName, pEntryInfo->ppvAddress);
1541 if (RT_SUCCESS(rc2))
1542 {
1543 /* Found an entry point. */
1544 LogRel(("AUTH: Using entry point '%s'.\n", pEntryInfo->pszName));
1545 rc = VINF_SUCCESS;
1546 break;
1547 }
1548
1549 if (rc2 != VERR_SYMBOL_NOT_FOUND)
1550 {
1551 LogRel(("AUTH: Could not resolve import '%s'. Error code: %Rrc\n", pEntryInfo->pszName, rc2));
1552 }
1553 rc = rc2;
1554
1555 pEntryInfo++;
1556 }
1557 }
1558
1559 if (RT_FAILURE(rc))
1560 {
1561 mConsole->setError(E_FAIL,
1562 mConsole->tr("Could not load the external authentication library '%s' (%Rrc)"),
1563 filename.c_str(),
1564 rc);
1565
1566 mpfnAuthEntry = NULL;
1567 mpfnAuthEntry2 = NULL;
1568 mpfnAuthEntry3 = NULL;
1569
1570 if (mAuthLibrary)
1571 {
1572 RTLdrClose(mAuthLibrary);
1573 mAuthLibrary = 0;
1574 }
1575
1576 return AuthResultAccessDenied;
1577 }
1578 }
1579
1580 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
1581
1582 AuthResult result = AuthResultAccessDenied;
1583 if (mpfnAuthEntry3)
1584 {
1585 result = mpfnAuthEntry3("vrde", &rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
1586 }
1587 else if (mpfnAuthEntry2)
1588 {
1589 result = mpfnAuthEntry2(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain, true, u32ClientId);
1590 }
1591 else if (mpfnAuthEntry)
1592 {
1593 result = mpfnAuthEntry(&rawuuid, guestJudgement, pszUser, pszPassword, pszDomain);
1594 }
1595
1596 switch (result)
1597 {
1598 case AuthResultAccessDenied:
1599 LogRel(("AUTH: external authentication module returned 'access denied'\n"));
1600 break;
1601 case AuthResultAccessGranted:
1602 LogRel(("AUTH: external authentication module returned 'access granted'\n"));
1603 break;
1604 case AuthResultDelegateToGuest:
1605 LogRel(("AUTH: external authentication module returned 'delegate request to guest'\n"));
1606 break;
1607 default:
1608 LogRel(("AUTH: external authentication module returned incorrect return code %d\n", result));
1609 result = AuthResultAccessDenied;
1610 }
1611
1612 LogFlow(("ConsoleVRDPServer::Authenticate: result = %d\n", result));
1613
1614 return result;
1615}
1616
1617void ConsoleVRDPServer::AuthDisconnect(const Guid &uuid, uint32_t u32ClientId)
1618{
1619 AUTHUUID rawuuid;
1620
1621 memcpy(rawuuid, uuid.raw(), sizeof(rawuuid));
1622
1623 LogFlow(("ConsoleVRDPServer::AuthDisconnect: uuid = %RTuuid, u32ClientId = %d\n",
1624 rawuuid, u32ClientId));
1625
1626 Assert(mAuthLibrary && (mpfnAuthEntry || mpfnAuthEntry2 || mpfnAuthEntry3));
1627
1628 if (mpfnAuthEntry3)
1629 mpfnAuthEntry3("vrde", &rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1630 else if (mpfnAuthEntry2)
1631 mpfnAuthEntry2(&rawuuid, AuthGuestNotAsked, NULL, NULL, NULL, false, u32ClientId);
1632}
1633
1634int ConsoleVRDPServer::lockConsoleVRDPServer(void)
1635{
1636 int rc = RTCritSectEnter(&mCritSect);
1637 AssertRC(rc);
1638 return rc;
1639}
1640
1641void ConsoleVRDPServer::unlockConsoleVRDPServer(void)
1642{
1643 RTCritSectLeave(&mCritSect);
1644}
1645
1646DECLCALLBACK(int) ConsoleVRDPServer::ClipboardCallback(void *pvCallback,
1647 uint32_t u32ClientId,
1648 uint32_t u32Function,
1649 uint32_t u32Format,
1650 const void *pvData,
1651 uint32_t cbData)
1652{
1653 LogFlowFunc(("pvCallback = %p, u32ClientId = %d, u32Function = %d, u32Format = 0x%08X, pvData = %p, cbData = %d\n",
1654 pvCallback, u32ClientId, u32Function, u32Format, pvData, cbData));
1655
1656 int rc = VINF_SUCCESS;
1657
1658 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvCallback);
1659
1660 NOREF(u32ClientId);
1661
1662 switch (u32Function)
1663 {
1664 case VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE:
1665 {
1666 if (pServer->mpfnClipboardCallback)
1667 {
1668 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1669 u32Format,
1670 (void *)pvData,
1671 cbData);
1672 }
1673 } break;
1674
1675 case VRDE_CLIPBOARD_FUNCTION_DATA_READ:
1676 {
1677 if (pServer->mpfnClipboardCallback)
1678 {
1679 pServer->mpfnClipboardCallback(VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1680 u32Format,
1681 (void *)pvData,
1682 cbData);
1683 }
1684 } break;
1685
1686 default:
1687 rc = VERR_NOT_SUPPORTED;
1688 }
1689
1690 return rc;
1691}
1692
1693DECLCALLBACK(int) ConsoleVRDPServer::ClipboardServiceExtension(void *pvExtension,
1694 uint32_t u32Function,
1695 void *pvParms,
1696 uint32_t cbParms)
1697{
1698 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
1699 pvExtension, u32Function, pvParms, cbParms));
1700
1701 int rc = VINF_SUCCESS;
1702
1703 ConsoleVRDPServer *pServer = static_cast <ConsoleVRDPServer *>(pvExtension);
1704
1705 VBOXCLIPBOARDEXTPARMS *pParms = (VBOXCLIPBOARDEXTPARMS *)pvParms;
1706
1707 switch (u32Function)
1708 {
1709 case VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK:
1710 {
1711 pServer->mpfnClipboardCallback = pParms->u.pfnCallback;
1712 } break;
1713
1714 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1715 {
1716 /* The guest announces clipboard formats. This must be delivered to all clients. */
1717 if (mpEntryPoints && pServer->mhServer)
1718 {
1719 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1720 VRDE_CLIPBOARD_FUNCTION_FORMAT_ANNOUNCE,
1721 pParms->u32Format,
1722 NULL,
1723 0,
1724 NULL);
1725 }
1726 } break;
1727
1728 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1729 {
1730 /* The clipboard service expects that the pvData buffer will be filled
1731 * with clipboard data. The server returns the data from the client that
1732 * announced the requested format most recently.
1733 */
1734 if (mpEntryPoints && pServer->mhServer)
1735 {
1736 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1737 VRDE_CLIPBOARD_FUNCTION_DATA_READ,
1738 pParms->u32Format,
1739 pParms->u.pvData,
1740 pParms->cbData,
1741 &pParms->cbData);
1742 }
1743 } break;
1744
1745 case VBOX_CLIPBOARD_EXT_FN_DATA_WRITE:
1746 {
1747 if (mpEntryPoints && pServer->mhServer)
1748 {
1749 mpEntryPoints->VRDEClipboard(pServer->mhServer,
1750 VRDE_CLIPBOARD_FUNCTION_DATA_WRITE,
1751 pParms->u32Format,
1752 pParms->u.pvData,
1753 pParms->cbData,
1754 NULL);
1755 }
1756 } break;
1757
1758 default:
1759 rc = VERR_NOT_SUPPORTED;
1760 }
1761
1762 return rc;
1763}
1764
1765void ConsoleVRDPServer::ClipboardCreate(uint32_t u32ClientId)
1766{
1767 int rc = lockConsoleVRDPServer();
1768
1769 if (RT_SUCCESS(rc))
1770 {
1771 if (mcClipboardRefs == 0)
1772 {
1773 rc = HGCMHostRegisterServiceExtension(&mhClipboard, "VBoxSharedClipboard", ClipboardServiceExtension, this);
1774
1775 if (RT_SUCCESS(rc))
1776 {
1777 mcClipboardRefs++;
1778 }
1779 }
1780
1781 unlockConsoleVRDPServer();
1782 }
1783}
1784
1785void ConsoleVRDPServer::ClipboardDelete(uint32_t u32ClientId)
1786{
1787 int rc = lockConsoleVRDPServer();
1788
1789 if (RT_SUCCESS(rc))
1790 {
1791 mcClipboardRefs--;
1792
1793 if (mcClipboardRefs == 0)
1794 {
1795 HGCMHostUnregisterServiceExtension(mhClipboard);
1796 }
1797
1798 unlockConsoleVRDPServer();
1799 }
1800}
1801
1802/* That is called on INPUT thread of the VRDP server.
1803 * The ConsoleVRDPServer keeps a list of created backend instances.
1804 */
1805void ConsoleVRDPServer::USBBackendCreate(uint32_t u32ClientId, void **ppvIntercept)
1806{
1807#ifdef VBOX_WITH_USB
1808 LogFlow(("ConsoleVRDPServer::USBBackendCreate: u32ClientId = %d\n", u32ClientId));
1809
1810 /* Create a new instance of the USB backend for the new client. */
1811 RemoteUSBBackend *pRemoteUSBBackend = new RemoteUSBBackend(mConsole, this, u32ClientId);
1812
1813 if (pRemoteUSBBackend)
1814 {
1815 pRemoteUSBBackend->AddRef(); /* 'Release' called in USBBackendDelete. */
1816
1817 /* Append the new instance in the list. */
1818 int rc = lockConsoleVRDPServer();
1819
1820 if (RT_SUCCESS(rc))
1821 {
1822 pRemoteUSBBackend->pNext = mUSBBackends.pHead;
1823 if (mUSBBackends.pHead)
1824 {
1825 mUSBBackends.pHead->pPrev = pRemoteUSBBackend;
1826 }
1827 else
1828 {
1829 mUSBBackends.pTail = pRemoteUSBBackend;
1830 }
1831
1832 mUSBBackends.pHead = pRemoteUSBBackend;
1833
1834 unlockConsoleVRDPServer();
1835
1836 if (ppvIntercept)
1837 {
1838 *ppvIntercept = pRemoteUSBBackend;
1839 }
1840 }
1841
1842 if (RT_FAILURE(rc))
1843 {
1844 pRemoteUSBBackend->Release();
1845 }
1846 }
1847#endif /* VBOX_WITH_USB */
1848}
1849
1850void ConsoleVRDPServer::USBBackendDelete(uint32_t u32ClientId)
1851{
1852#ifdef VBOX_WITH_USB
1853 LogFlow(("ConsoleVRDPServer::USBBackendDelete: u32ClientId = %d\n", u32ClientId));
1854
1855 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1856
1857 /* Find the instance. */
1858 int rc = lockConsoleVRDPServer();
1859
1860 if (RT_SUCCESS(rc))
1861 {
1862 pRemoteUSBBackend = usbBackendFind(u32ClientId);
1863
1864 if (pRemoteUSBBackend)
1865 {
1866 /* Notify that it will be deleted. */
1867 pRemoteUSBBackend->NotifyDelete();
1868 }
1869
1870 unlockConsoleVRDPServer();
1871 }
1872
1873 if (pRemoteUSBBackend)
1874 {
1875 /* Here the instance has been excluded from the list and can be dereferenced. */
1876 pRemoteUSBBackend->Release();
1877 }
1878#endif
1879}
1880
1881void *ConsoleVRDPServer::USBBackendRequestPointer(uint32_t u32ClientId, const Guid *pGuid)
1882{
1883#ifdef VBOX_WITH_USB
1884 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1885
1886 /* Find the instance. */
1887 int rc = lockConsoleVRDPServer();
1888
1889 if (RT_SUCCESS(rc))
1890 {
1891 pRemoteUSBBackend = usbBackendFind(u32ClientId);
1892
1893 if (pRemoteUSBBackend)
1894 {
1895 /* Inform the backend instance that it is referenced by the Guid. */
1896 bool fAdded = pRemoteUSBBackend->addUUID(pGuid);
1897
1898 if (fAdded)
1899 {
1900 /* Reference the instance because its pointer is being taken. */
1901 pRemoteUSBBackend->AddRef(); /* 'Release' is called in USBBackendReleasePointer. */
1902 }
1903 else
1904 {
1905 pRemoteUSBBackend = NULL;
1906 }
1907 }
1908
1909 unlockConsoleVRDPServer();
1910 }
1911
1912 if (pRemoteUSBBackend)
1913 {
1914 return pRemoteUSBBackend->GetBackendCallbackPointer();
1915 }
1916
1917#endif
1918 return NULL;
1919}
1920
1921void ConsoleVRDPServer::USBBackendReleasePointer(const Guid *pGuid)
1922{
1923#ifdef VBOX_WITH_USB
1924 RemoteUSBBackend *pRemoteUSBBackend = NULL;
1925
1926 /* Find the instance. */
1927 int rc = lockConsoleVRDPServer();
1928
1929 if (RT_SUCCESS(rc))
1930 {
1931 pRemoteUSBBackend = usbBackendFindByUUID(pGuid);
1932
1933 if (pRemoteUSBBackend)
1934 {
1935 pRemoteUSBBackend->removeUUID(pGuid);
1936 }
1937
1938 unlockConsoleVRDPServer();
1939
1940 if (pRemoteUSBBackend)
1941 {
1942 pRemoteUSBBackend->Release();
1943 }
1944 }
1945#endif
1946}
1947
1948RemoteUSBBackend *ConsoleVRDPServer::usbBackendGetNext(RemoteUSBBackend *pRemoteUSBBackend)
1949{
1950 LogFlow(("ConsoleVRDPServer::usbBackendGetNext: pBackend = %p\n", pRemoteUSBBackend));
1951
1952 RemoteUSBBackend *pNextRemoteUSBBackend = NULL;
1953#ifdef VBOX_WITH_USB
1954
1955 int rc = lockConsoleVRDPServer();
1956
1957 if (RT_SUCCESS(rc))
1958 {
1959 if (pRemoteUSBBackend == NULL)
1960 {
1961 /* The first backend in the list is requested. */
1962 pNextRemoteUSBBackend = mUSBBackends.pHead;
1963 }
1964 else
1965 {
1966 /* Get pointer to the next backend. */
1967 pNextRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
1968 }
1969
1970 if (pNextRemoteUSBBackend)
1971 {
1972 pNextRemoteUSBBackend->AddRef();
1973 }
1974
1975 unlockConsoleVRDPServer();
1976
1977 if (pRemoteUSBBackend)
1978 {
1979 pRemoteUSBBackend->Release();
1980 }
1981 }
1982#endif
1983
1984 return pNextRemoteUSBBackend;
1985}
1986
1987#ifdef VBOX_WITH_USB
1988/* Internal method. Called under the ConsoleVRDPServerLock. */
1989RemoteUSBBackend *ConsoleVRDPServer::usbBackendFind(uint32_t u32ClientId)
1990{
1991 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
1992
1993 while (pRemoteUSBBackend)
1994 {
1995 if (pRemoteUSBBackend->ClientId() == u32ClientId)
1996 {
1997 break;
1998 }
1999
2000 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2001 }
2002
2003 return pRemoteUSBBackend;
2004}
2005
2006/* Internal method. Called under the ConsoleVRDPServerLock. */
2007RemoteUSBBackend *ConsoleVRDPServer::usbBackendFindByUUID(const Guid *pGuid)
2008{
2009 RemoteUSBBackend *pRemoteUSBBackend = mUSBBackends.pHead;
2010
2011 while (pRemoteUSBBackend)
2012 {
2013 if (pRemoteUSBBackend->findUUID(pGuid))
2014 {
2015 break;
2016 }
2017
2018 pRemoteUSBBackend = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2019 }
2020
2021 return pRemoteUSBBackend;
2022}
2023#endif
2024
2025/* Internal method. Called by the backend destructor. */
2026void ConsoleVRDPServer::usbBackendRemoveFromList(RemoteUSBBackend *pRemoteUSBBackend)
2027{
2028#ifdef VBOX_WITH_USB
2029 int rc = lockConsoleVRDPServer();
2030 AssertRC(rc);
2031
2032 /* Exclude the found instance from the list. */
2033 if (pRemoteUSBBackend->pNext)
2034 {
2035 pRemoteUSBBackend->pNext->pPrev = pRemoteUSBBackend->pPrev;
2036 }
2037 else
2038 {
2039 mUSBBackends.pTail = (RemoteUSBBackend *)pRemoteUSBBackend->pPrev;
2040 }
2041
2042 if (pRemoteUSBBackend->pPrev)
2043 {
2044 pRemoteUSBBackend->pPrev->pNext = pRemoteUSBBackend->pNext;
2045 }
2046 else
2047 {
2048 mUSBBackends.pHead = (RemoteUSBBackend *)pRemoteUSBBackend->pNext;
2049 }
2050
2051 pRemoteUSBBackend->pNext = pRemoteUSBBackend->pPrev = NULL;
2052
2053 unlockConsoleVRDPServer();
2054#endif
2055}
2056
2057
2058void ConsoleVRDPServer::SendUpdate(unsigned uScreenId, void *pvUpdate, uint32_t cbUpdate) const
2059{
2060 if (mpEntryPoints && mhServer)
2061 {
2062 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, pvUpdate, cbUpdate);
2063 }
2064}
2065
2066void ConsoleVRDPServer::SendResize(void) const
2067{
2068 if (mpEntryPoints && mhServer)
2069 {
2070 mpEntryPoints->VRDEResize(mhServer);
2071 }
2072}
2073
2074void ConsoleVRDPServer::SendUpdateBitmap(unsigned uScreenId, uint32_t x, uint32_t y, uint32_t w, uint32_t h) const
2075{
2076 VRDEORDERHDR update;
2077 update.x = x;
2078 update.y = y;
2079 update.w = w;
2080 update.h = h;
2081 if (mpEntryPoints && mhServer)
2082 {
2083 mpEntryPoints->VRDEUpdate(mhServer, uScreenId, &update, sizeof(update));
2084 }
2085}
2086
2087void ConsoleVRDPServer::SendAudioSamples(void *pvSamples, uint32_t cSamples, VRDEAUDIOFORMAT format) const
2088{
2089 if (mpEntryPoints && mhServer)
2090 {
2091 mpEntryPoints->VRDEAudioSamples(mhServer, pvSamples, cSamples, format);
2092 }
2093}
2094
2095void ConsoleVRDPServer::SendAudioVolume(uint16_t left, uint16_t right) const
2096{
2097 if (mpEntryPoints && mhServer)
2098 {
2099 mpEntryPoints->VRDEAudioVolume(mhServer, left, right);
2100 }
2101}
2102
2103void ConsoleVRDPServer::SendUSBRequest(uint32_t u32ClientId, void *pvParms, uint32_t cbParms) const
2104{
2105 if (mpEntryPoints && mhServer)
2106 {
2107 mpEntryPoints->VRDEUSBRequest(mhServer, u32ClientId, pvParms, cbParms);
2108 }
2109}
2110
2111void ConsoleVRDPServer::QueryInfo(uint32_t index, void *pvBuffer, uint32_t cbBuffer, uint32_t *pcbOut) const
2112{
2113 if (index == VRDE_QI_PORT)
2114 {
2115 uint32_t cbOut = sizeof(int32_t);
2116
2117 if (cbBuffer >= cbOut)
2118 {
2119 *pcbOut = cbOut;
2120 *(int32_t *)pvBuffer = (int32_t)mVRDPBindPort;
2121 }
2122 }
2123 else if (mpEntryPoints && mhServer)
2124 {
2125 mpEntryPoints->VRDEQueryInfo(mhServer, index, pvBuffer, cbBuffer, pcbOut);
2126 }
2127}
2128
2129/* static */ int ConsoleVRDPServer::loadVRDPLibrary(const char *pszLibraryName)
2130{
2131 int rc = VINF_SUCCESS;
2132
2133 if (mVRDPLibrary == NIL_RTLDRMOD)
2134 {
2135 char szErr[4096 + 512];
2136 szErr[0] = '\0';
2137 if (RTPathHavePath(pszLibraryName))
2138 rc = SUPR3HardenedLdrLoadPlugIn(pszLibraryName, &mVRDPLibrary, szErr, sizeof(szErr));
2139 else
2140 rc = SUPR3HardenedLdrLoadAppPriv(pszLibraryName, &mVRDPLibrary);
2141 if (RT_SUCCESS(rc))
2142 {
2143 struct SymbolEntry
2144 {
2145 const char *name;
2146 void **ppfn;
2147 };
2148
2149 #define DEFSYMENTRY(a) { #a, (void**)&mpfn##a }
2150
2151 static const struct SymbolEntry s_aSymbols[] =
2152 {
2153 DEFSYMENTRY(VRDECreateServer)
2154 };
2155
2156 #undef DEFSYMENTRY
2157
2158 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
2159 {
2160 rc = RTLdrGetSymbol(mVRDPLibrary, s_aSymbols[i].name, s_aSymbols[i].ppfn);
2161
2162 if (RT_FAILURE(rc))
2163 {
2164 LogRel(("VRDE: Error resolving symbol '%s', rc %Rrc.\n", s_aSymbols[i].name, rc));
2165 break;
2166 }
2167 }
2168 }
2169 else
2170 {
2171 if (szErr[0])
2172 LogRel(("VRDE: Error loading the library '%s': %s (%Rrc)\n", pszLibraryName, szErr, rc));
2173 else
2174 LogRel(("VRDE: Error loading the library '%s' rc = %Rrc.\n", pszLibraryName, rc));
2175
2176 mVRDPLibrary = NIL_RTLDRMOD;
2177 }
2178 }
2179
2180 if (RT_FAILURE(rc))
2181 {
2182 if (mVRDPLibrary != NIL_RTLDRMOD)
2183 {
2184 RTLdrClose(mVRDPLibrary);
2185 mVRDPLibrary = NIL_RTLDRMOD;
2186 }
2187 }
2188
2189 return rc;
2190}
2191
2192/*
2193 * IVRDEServerInfo implementation.
2194 */
2195// constructor / destructor
2196/////////////////////////////////////////////////////////////////////////////
2197
2198VRDEServerInfo::VRDEServerInfo()
2199 : mParent(NULL)
2200{
2201}
2202
2203VRDEServerInfo::~VRDEServerInfo()
2204{
2205}
2206
2207
2208HRESULT VRDEServerInfo::FinalConstruct()
2209{
2210 return S_OK;
2211}
2212
2213void VRDEServerInfo::FinalRelease()
2214{
2215 uninit();
2216}
2217
2218// public methods only for internal purposes
2219/////////////////////////////////////////////////////////////////////////////
2220
2221/**
2222 * Initializes the guest object.
2223 */
2224HRESULT VRDEServerInfo::init(Console *aParent)
2225{
2226 LogFlowThisFunc(("aParent=%p\n", aParent));
2227
2228 ComAssertRet(aParent, E_INVALIDARG);
2229
2230 /* Enclose the state transition NotReady->InInit->Ready */
2231 AutoInitSpan autoInitSpan(this);
2232 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2233
2234 unconst(mParent) = aParent;
2235
2236 /* Confirm a successful initialization */
2237 autoInitSpan.setSucceeded();
2238
2239 return S_OK;
2240}
2241
2242/**
2243 * Uninitializes the instance and sets the ready flag to FALSE.
2244 * Called either from FinalRelease() or by the parent when it gets destroyed.
2245 */
2246void VRDEServerInfo::uninit()
2247{
2248 LogFlowThisFunc(("\n"));
2249
2250 /* Enclose the state transition Ready->InUninit->NotReady */
2251 AutoUninitSpan autoUninitSpan(this);
2252 if (autoUninitSpan.uninitDone())
2253 return;
2254
2255 unconst(mParent) = NULL;
2256}
2257
2258// IVRDEServerInfo properties
2259/////////////////////////////////////////////////////////////////////////////
2260
2261#define IMPL_GETTER_BOOL(_aType, _aName, _aIndex) \
2262 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2263 { \
2264 if (!a##_aName) \
2265 return E_POINTER; \
2266 \
2267 AutoCaller autoCaller(this); \
2268 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2269 \
2270 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2271 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2272 \
2273 uint32_t value; \
2274 uint32_t cbOut = 0; \
2275 \
2276 mParent->consoleVRDPServer()->QueryInfo \
2277 (_aIndex, &value, sizeof(value), &cbOut); \
2278 \
2279 *a##_aName = cbOut? !!value: FALSE; \
2280 \
2281 return S_OK; \
2282 } \
2283 extern void IMPL_GETTER_BOOL_DUMMY(void)
2284
2285#define IMPL_GETTER_SCALAR(_aType, _aName, _aIndex, _aValueMask) \
2286 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2287 { \
2288 if (!a##_aName) \
2289 return E_POINTER; \
2290 \
2291 AutoCaller autoCaller(this); \
2292 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2293 \
2294 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2295 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2296 \
2297 _aType value; \
2298 uint32_t cbOut = 0; \
2299 \
2300 mParent->consoleVRDPServer()->QueryInfo \
2301 (_aIndex, &value, sizeof(value), &cbOut); \
2302 \
2303 if (_aValueMask) value &= (_aValueMask); \
2304 *a##_aName = cbOut? value: 0; \
2305 \
2306 return S_OK; \
2307 } \
2308 extern void IMPL_GETTER_SCALAR_DUMMY(void)
2309
2310#define IMPL_GETTER_BSTR(_aType, _aName, _aIndex) \
2311 STDMETHODIMP VRDEServerInfo::COMGETTER(_aName)(_aType *a##_aName) \
2312 { \
2313 if (!a##_aName) \
2314 return E_POINTER; \
2315 \
2316 AutoCaller autoCaller(this); \
2317 if (FAILED(autoCaller.rc())) return autoCaller.rc(); \
2318 \
2319 /* todo: Not sure if a AutoReadLock would be sufficient. */ \
2320 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); \
2321 \
2322 uint32_t cbOut = 0; \
2323 \
2324 mParent->consoleVRDPServer()->QueryInfo \
2325 (_aIndex, NULL, 0, &cbOut); \
2326 \
2327 if (cbOut == 0) \
2328 { \
2329 Bstr str(""); \
2330 str.cloneTo(a##_aName); \
2331 return S_OK; \
2332 } \
2333 \
2334 char *pchBuffer = (char *)RTMemTmpAlloc(cbOut); \
2335 \
2336 if (!pchBuffer) \
2337 { \
2338 Log(("VRDEServerInfo::" \
2339 #_aName \
2340 ": Failed to allocate memory %d bytes\n", cbOut)); \
2341 return E_OUTOFMEMORY; \
2342 } \
2343 \
2344 mParent->consoleVRDPServer()->QueryInfo \
2345 (_aIndex, pchBuffer, cbOut, &cbOut); \
2346 \
2347 Bstr str(pchBuffer); \
2348 \
2349 str.cloneTo(a##_aName); \
2350 \
2351 RTMemTmpFree(pchBuffer); \
2352 \
2353 return S_OK; \
2354 } \
2355 extern void IMPL_GETTER_BSTR_DUMMY(void)
2356
2357IMPL_GETTER_BOOL (BOOL, Active, VRDE_QI_ACTIVE);
2358IMPL_GETTER_SCALAR (LONG, Port, VRDE_QI_PORT, 0);
2359IMPL_GETTER_SCALAR (ULONG, NumberOfClients, VRDE_QI_NUMBER_OF_CLIENTS, 0);
2360IMPL_GETTER_SCALAR (LONG64, BeginTime, VRDE_QI_BEGIN_TIME, 0);
2361IMPL_GETTER_SCALAR (LONG64, EndTime, VRDE_QI_END_TIME, 0);
2362IMPL_GETTER_SCALAR (LONG64, BytesSent, VRDE_QI_BYTES_SENT, INT64_MAX);
2363IMPL_GETTER_SCALAR (LONG64, BytesSentTotal, VRDE_QI_BYTES_SENT_TOTAL, INT64_MAX);
2364IMPL_GETTER_SCALAR (LONG64, BytesReceived, VRDE_QI_BYTES_RECEIVED, INT64_MAX);
2365IMPL_GETTER_SCALAR (LONG64, BytesReceivedTotal, VRDE_QI_BYTES_RECEIVED_TOTAL, INT64_MAX);
2366IMPL_GETTER_BSTR (BSTR, User, VRDE_QI_USER);
2367IMPL_GETTER_BSTR (BSTR, Domain, VRDE_QI_DOMAIN);
2368IMPL_GETTER_BSTR (BSTR, ClientName, VRDE_QI_CLIENT_NAME);
2369IMPL_GETTER_BSTR (BSTR, ClientIP, VRDE_QI_CLIENT_IP);
2370IMPL_GETTER_SCALAR (ULONG, ClientVersion, VRDE_QI_CLIENT_VERSION, 0);
2371IMPL_GETTER_SCALAR (ULONG, EncryptionStyle, VRDE_QI_ENCRYPTION_STYLE, 0);
2372
2373#undef IMPL_GETTER_BSTR
2374#undef IMPL_GETTER_SCALAR
2375#undef IMPL_GETTER_BOOL
2376/* vi: set tabstop=4 shiftwidth=4 expandtab: */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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