VirtualBox

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

Last change on this file since 25275 was 25149, checked in by vboxsync, 15 years ago

Main: cleanup: remove all CheckComRC* macros (no functional change)

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

© 2023 Oracle
ContactPrivacy policyTerms of Use