VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc.cpp@ 93444

Last change on this file since 93444 was 93444, checked in by vboxsync, 3 years ago

VMM,Main,HostServices: Use a function table for accessing the VBoxVMM.dll/so/dylib functionality, and load it dynamically when the Console object is initialized. Also converted a few drivers in Main to use device helpers to get config values and such. bugref:10074

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 100.9 KB
Line 
1/* $Id: VBoxSharedClipboardSvc.cpp 93444 2022-01-26 18:01:15Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Host service entry points.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_hostclip The Shared Clipboard Host Service
20 *
21 * The shared clipboard host service is the host half of the clibpoard proxying
22 * between the host and the guest. The guest parts live in VBoxClient, VBoxTray
23 * and VBoxService depending on the OS, with code shared between host and guest
24 * under src/VBox/GuestHost/SharedClipboard/.
25 *
26 * The service is split into a platform-independent core and platform-specific
27 * backends. The service defines two communication protocols - one to
28 * communicate with the clipboard service running on the guest, and one to
29 * communicate with the backend. These will be described in a very skeletal
30 * fashion here.
31 *
32 * r=bird: The "two communication protocols" does not seems to be factual, there
33 * is only one protocol, the first one mentioned. It cannot be backend
34 * specific, because the guest/host protocol is platform and backend agnostic in
35 * nature. You may call it versions, but I take a great dislike to "protocol
36 * versions" here, as you've just extended the existing protocol with a feature
37 * that allows to transfer files and directories too. See @bugref{9437#c39}.
38 *
39 *
40 * @section sec_hostclip_guest_proto The guest communication protocol
41 *
42 * The guest clipboard service communicates with the host service over HGCM
43 * (the host is a HGCM service). HGCM is connection based, so the guest side
44 * has to connect before anything else can be done. (Windows hosts currently
45 * only support one simultaneous connection.) Once it has connected, it can
46 * send messages to the host services, some of which will receive immediate
47 * replies from the host, others which will block till a reply becomes
48 * available. The latter is because HGCM does't allow the host to initiate
49 * communication, it must be guest triggered. The HGCM service is single
50 * threaded, so it doesn't matter if the guest tries to send lots of requests in
51 * parallel, the service will process them one at the time.
52 *
53 * There are currently four messages defined. The first is
54 * VBOX_SHCL_GUEST_FN_MSG_GET / VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT, which waits
55 * for a message from the host. If a host message is sent while the guest is
56 * not waiting, it will be queued until the guest requests it. The host code
57 * only supports a single simultaneous GET call from one client guest.
58 *
59 * The second guest message is VBOX_SHCL_GUEST_FN_REPORT_FORMATS, which tells
60 * the host that the guest has new clipboard data available. The third is
61 * VBOX_SHCL_GUEST_FN_DATA_READ, which asks the host to send its clipboard data
62 * and waits until it arrives. The host supports at most one simultaneous
63 * VBOX_SHCL_GUEST_FN_DATA_READ call from a guest - if a second call is made
64 * before the first has returned, the first will be aborted.
65 *
66 * The last guest message is VBOX_SHCL_GUEST_FN_DATA_WRITE, which is used to
67 * send the contents of the guest clipboard to the host. This call should be
68 * used after the host has requested data from the guest.
69 *
70 *
71 * @section sec_hostclip_backend_proto The communication protocol with the
72 * platform-specific backend
73 *
74 * The initial protocol implementation (called protocol v0) was very simple,
75 * and could only handle simple data (like copied text and so on). It also
76 * was limited to two (2) fixed parameters at all times.
77 *
78 * Since VBox 6.1 a newer protocol (v1) has been established to also support
79 * file transfers. This protocol uses a (per-client) message queue instead
80 * (see VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT vs. VBOX_SHCL_GUEST_FN_GET_HOST_MSG).
81 *
82 * To distinguish the old (legacy) or new(er) protocol, the VBOX_SHCL_GUEST_FN_CONNECT
83 * message has been introduced. If an older guest does not send this message,
84 * an appropriate translation will be done to serve older Guest Additions (< 6.1).
85 *
86 * The protocol also support out-of-order messages by using so-called "context IDs",
87 * which are generated by the host. A context ID consists of a so-called "source event ID"
88 * and a so-called "event ID". Each HGCM client has an own, random, source event ID and
89 * generates non-deterministic event IDs so that the guest side does not known what
90 * comes next; the guest side has to reply with the same conext ID which was sent by
91 * the host request.
92 *
93 * Also see the protocol changelog at VBoxShClSvc.h.
94 *
95 *
96 * @section sec_uri_intro Transferring files
97 *
98 * Since VBox x.x.x transferring files via Shared Clipboard is supported.
99 * See the VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS define for supported / enabled
100 * platforms. This is called "Shared Clipboard transfers".
101 *
102 * Copying files / directories from guest A to guest B requires the host
103 * service to act as a proxy and cache, as we don't allow direct VM-to-VM
104 * communication. Copying from / to the host also is taken into account.
105 *
106 * At the moment a transfer is a all-or-nothing operation, e.g. it either
107 * completes or fails completely. There might be callbacks in the future
108 * to e.g. skip failing entries.
109 *
110 * Known limitations:
111 *
112 * - Support for VRDE (VRDP) is not implemented yet (see #9498).
113 * - Unicode support on Windows hosts / guests is not enabled (yet).
114 * - Symbolic links / Windows junctions are not allowed.
115 * - Windows alternate data streams (ADS) are not allowed.
116 * - No support for ACLs yet.
117 * - No (maybe never) support for NT4.
118
119 * @section sec_transfer_structure Transfer handling structure
120 *
121 * All structures / classes are designed for running on both, on the guest
122 * (via VBoxTray / VBoxClient) or on the host (host service) to avoid code
123 * duplication where applicable.
124 *
125 * Per HGCM client there is a so-called "transfer context", which in turn can
126 * have one or mulitple so-called "Shared Clipboard transfer" objects. At the
127 * moment we only support on concurrent Shared Clipboard transfer per transfer
128 * context. It's being used for reading from a source or writing to destination,
129 * depening on its direction. An Shared Clipboard transfer can have optional
130 * callbacks which might be needed by various implementations. Also, transfers
131 * optionally can run in an asynchronous thread to prevent blocking the UI while
132 * running.
133 *
134 * @section sec_transfer_providers Transfer providers
135 *
136 * For certain implementations (for example on Windows guests / hosts, using
137 * IDataObject and IStream objects) a more flexible approach reqarding reading /
138 * writing is needed. For this so-called transfer providers abstract the way of how
139 * data is being read / written in the current context (host / guest), while
140 * the rest of the code stays the same.
141 *
142 * @section sec_transfer_protocol Transfer protocol
143 *
144 * The host service issues commands which the guest has to respond with an own
145 * message to. The protocol itself is designed so that it has primitives to list
146 * directories and open/close/read/write file system objects.
147 *
148 * Note that this is different from the DnD approach, as Shared Clipboard transfers
149 * need to be deeper integrated within the host / guest OS (i.e. for progress UI),
150 * and this might require non-monolithic / random access APIs to achieve.
151 *
152 * As there can be multiple file system objects (fs objects) selected for transfer,
153 * a transfer can be queried for its root entries, which then contains the top-level
154 * elements. Based on these elements, (a) (recursive) listing(s) can be performed
155 * to (partially) walk down into directories and query fs object information. The
156 * provider provides appropriate interface for this, even if not all implementations
157 * might need this mechanism.
158 *
159 * An Shared Clipboard transfer has three stages:
160 * - 1. Announcement: An Shared Clipboard transfer-compatible format (currently only one format available)
161 * has been announced, the destination side creates a transfer object, which then,
162 * depending on the actual implementation, can be used to tell the OS that
163 * there is transfer (file) data available.
164 * At this point this just acts as a (kind-of) promise to the OS that we
165 * can provide (file) data at some later point in time.
166 *
167 * - 2. Initialization: As soon as the OS requests the (file) data, mostly triggered
168 * by the user starting a paste operation (CTRL + V), the transfer get initialized
169 * on the destination side, which in turn lets the source know that a transfer
170 * is going to happen.
171 *
172 * - 3. Transfer: At this stage the actual transfer from source to the destination takes
173 * place. How the actual transfer is structurized (e.g. which files / directories
174 * are transferred in which order) depends on the destination implementation. This
175 * is necessary in order to fulfill requirements on the destination side with
176 * regards to ETA calculation or other dependencies.
177 * Both sides can abort or cancel the transfer at any time.
178 */
179
180
181/*********************************************************************************************************************************
182* Header Files *
183*********************************************************************************************************************************/
184#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
185#include <VBox/log.h>
186#include <VBox/vmm/vmmr3vtable.h> /* must be included before hgcmsvc.h */
187
188#include <VBox/GuestHost/clipboard-helper.h>
189#include <VBox/HostServices/Service.h>
190#include <VBox/HostServices/VBoxClipboardSvc.h>
191#include <VBox/HostServices/VBoxClipboardExt.h>
192
193#include <VBox/AssertGuest.h>
194#include <VBox/err.h>
195#include <VBox/VMMDev.h>
196#include <VBox/vmm/ssm.h>
197
198#include <iprt/mem.h>
199#include <iprt/string.h>
200#include <iprt/assert.h>
201#include <iprt/critsect.h>
202#include <iprt/rand.h>
203
204#include "VBoxSharedClipboardSvc-internal.h"
205#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
206# include "VBoxSharedClipboardSvc-transfers.h"
207#endif
208
209using namespace HGCM;
210
211
212/*********************************************************************************************************************************
213* Defined Constants And Macros *
214*********************************************************************************************************************************/
215/** @name The saved state versions for the shared clipboard service.
216 *
217 * @note We set bit 31 because prior to version 0x80000002 there would be a
218 * structure size rather than a version number. Setting bit 31 dispells
219 * any possible ambiguity.
220 *
221 * @{ */
222/** The current saved state version. */
223#define VBOX_SHCL_SAVED_STATE_VER_CURRENT VBOX_SHCL_SAVED_STATE_LEGACY_CID
224/** Adds the legacy context ID list. */
225#define VBOX_SHCL_SAVED_STATE_LEGACY_CID UINT32_C(0x80000005)
226/** Adds the client's POD state and client state flags.
227 * @since 6.1 RC1 */
228#define VBOX_SHCL_SAVED_STATE_VER_6_1RC1 UINT32_C(0x80000004)
229/** First attempt saving state during @bugref{9437} development.
230 * @since 6.1 BETA 2 */
231#define VBOX_SHCL_SAVED_STATE_VER_6_1B2 UINT32_C(0x80000003)
232/** First structured version.
233 * @since 3.1 / r53668 */
234#define VBOX_SHCL_SAVED_STATE_VER_3_1 UINT32_C(0x80000002)
235/** This was just a state memory dump, including pointers and everything.
236 * @note This is not supported any more. Sorry. */
237#define VBOX_SHCL_SAVED_STATE_VER_NOT_SUPP (ARCH_BITS == 64 ? UINT32_C(72) : UINT32_C(48))
238/** @} */
239
240
241/*********************************************************************************************************************************
242* Global Variables *
243*********************************************************************************************************************************/
244PVBOXHGCMSVCHELPERS g_pHelpers;
245
246static RTCRITSECT g_CritSect; /** @todo r=andy Put this into some instance struct, avoid globals. */
247/** Global Shared Clipboard mode. */
248static uint32_t g_uMode = VBOX_SHCL_MODE_OFF;
249#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
250/** Global Shared Clipboard (file) transfer mode. */
251uint32_t g_fTransferMode = VBOX_SHCL_TRANSFER_MODE_DISABLED;
252#endif
253
254/** Is the clipboard running in headless mode? */
255static bool g_fHeadless = false;
256
257/** Holds the service extension state. */
258SHCLEXTSTATE g_ExtState = { 0 };
259
260/** Global map of all connected clients. */
261ClipboardClientMap g_mapClients;
262
263/** Global list of all clients which are queued up (deferred return) and ready
264 * to process new commands. The key is the (unique) client ID. */
265ClipboardClientQueue g_listClientsDeferred;
266
267/** Host feature mask (VBOX_SHCL_HF_0_XXX) for VBOX_SHCL_GUEST_FN_REPORT_FEATURES
268 * and VBOX_SHCL_GUEST_FN_QUERY_FEATURES. */
269static uint64_t const g_fHostFeatures0 = VBOX_SHCL_HF_0_CONTEXT_ID
270#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
271 | VBOX_SHCL_HF_0_TRANSFERS
272#endif
273 ;
274
275
276/**
277 * Returns the current Shared Clipboard service mode.
278 *
279 * @returns Current Shared Clipboard service mode.
280 */
281uint32_t ShClSvcGetMode(void)
282{
283 return g_uMode;
284}
285
286/**
287 * Getter for headless setting. Also needed by testcase.
288 *
289 * @returns Whether service currently running in headless mode or not.
290 */
291bool ShClSvcGetHeadless(void)
292{
293 return g_fHeadless;
294}
295
296static int shClSvcModeSet(uint32_t uMode)
297{
298 int rc = VERR_NOT_SUPPORTED;
299
300 switch (uMode)
301 {
302 case VBOX_SHCL_MODE_OFF:
303 RT_FALL_THROUGH();
304 case VBOX_SHCL_MODE_HOST_TO_GUEST:
305 RT_FALL_THROUGH();
306 case VBOX_SHCL_MODE_GUEST_TO_HOST:
307 RT_FALL_THROUGH();
308 case VBOX_SHCL_MODE_BIDIRECTIONAL:
309 {
310 g_uMode = uMode;
311
312 rc = VINF_SUCCESS;
313 break;
314 }
315
316 default:
317 {
318 g_uMode = VBOX_SHCL_MODE_OFF;
319 break;
320 }
321 }
322
323 LogFlowFuncLeaveRC(rc);
324 return rc;
325}
326
327/**
328 * Takes the global Shared Clipboard service lock.
329 *
330 * @returns \c true if locking was successful, or \c false if not.
331 */
332bool ShClSvcLock(void)
333{
334 return RT_SUCCESS(RTCritSectEnter(&g_CritSect));
335}
336
337/**
338 * Unlocks the formerly locked global Shared Clipboard service lock.
339 */
340void ShClSvcUnlock(void)
341{
342 int rc2 = RTCritSectLeave(&g_CritSect);
343 AssertRC(rc2);
344}
345
346/**
347 * Resets a client's state message queue.
348 *
349 * @param pClient Pointer to the client data structure to reset message queue for.
350 * @note Caller enters pClient->CritSect.
351 */
352void shClSvcMsgQueueReset(PSHCLCLIENT pClient)
353{
354 Assert(RTCritSectIsOwner(&pClient->CritSect));
355 LogFlowFuncEnter();
356
357 while (!RTListIsEmpty(&pClient->MsgQueue))
358 {
359 PSHCLCLIENTMSG pMsg = RTListRemoveFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
360 shClSvcMsgFree(pClient, pMsg);
361 }
362 pClient->cMsgAllocated = 0;
363
364 while (!RTListIsEmpty(&pClient->Legacy.lstCID))
365 {
366 PSHCLCLIENTLEGACYCID pCID = RTListRemoveFirst(&pClient->Legacy.lstCID, SHCLCLIENTLEGACYCID, Node);
367 RTMemFree(pCID);
368 }
369 pClient->Legacy.cCID = 0;
370}
371
372/**
373 * Allocates a new clipboard message.
374 *
375 * @returns Allocated clipboard message, or NULL on failure.
376 * @param pClient The client which is target of this message.
377 * @param idMsg The message ID (VBOX_SHCL_HOST_MSG_XXX) to use
378 * @param cParms The number of parameters the message takes.
379 */
380PSHCLCLIENTMSG shClSvcMsgAlloc(PSHCLCLIENT pClient, uint32_t idMsg, uint32_t cParms)
381{
382 RT_NOREF(pClient);
383 PSHCLCLIENTMSG pMsg = (PSHCLCLIENTMSG)RTMemAllocZ(RT_UOFFSETOF_DYN(SHCLCLIENTMSG, aParms[cParms]));
384 if (pMsg)
385 {
386 uint32_t cAllocated = ASMAtomicIncU32(&pClient->cMsgAllocated);
387 if (cAllocated <= 4096)
388 {
389 RTListInit(&pMsg->ListEntry);
390 pMsg->cParms = cParms;
391 pMsg->idMsg = idMsg;
392 return pMsg;
393 }
394 AssertMsgFailed(("Too many messages allocated for client %u! (%u)\n", pClient->State.uClientID, cAllocated));
395 ASMAtomicDecU32(&pClient->cMsgAllocated);
396 RTMemFree(pMsg);
397 }
398 return NULL;
399}
400
401/**
402 * Frees a formerly allocated clipboard message.
403 *
404 * @param pClient The client which was the target of this message.
405 * @param pMsg Clipboard message to free.
406 */
407void shClSvcMsgFree(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg)
408{
409 RT_NOREF(pClient);
410 /** @todo r=bird: Do accounting. */
411 if (pMsg)
412 {
413 pMsg->idMsg = UINT32_C(0xdeadface);
414 RTMemFree(pMsg);
415
416 uint32_t cAllocated = ASMAtomicDecU32(&pClient->cMsgAllocated);
417 Assert(cAllocated < UINT32_MAX / 2);
418 RT_NOREF(cAllocated);
419 }
420}
421
422/**
423 * Sets the VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT
424 * return parameters.
425 *
426 * @param pMsg Message to set return parameters to.
427 * @param paDstParms The peek parameter vector.
428 * @param cDstParms The number of peek parameters (at least two).
429 * @remarks ASSUMES the parameters has been cleared by clientMsgPeek.
430 */
431static void shClSvcMsgSetPeekReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
432{
433 Assert(cDstParms >= 2);
434 if (paDstParms[0].type == VBOX_HGCM_SVC_PARM_32BIT)
435 paDstParms[0].u.uint32 = pMsg->idMsg;
436 else
437 paDstParms[0].u.uint64 = pMsg->idMsg;
438 paDstParms[1].u.uint32 = pMsg->cParms;
439
440 uint32_t i = RT_MIN(cDstParms, pMsg->cParms + 2);
441 while (i-- > 2)
442 switch (pMsg->aParms[i - 2].type)
443 {
444 case VBOX_HGCM_SVC_PARM_32BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint32_t); break;
445 case VBOX_HGCM_SVC_PARM_64BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint64_t); break;
446 case VBOX_HGCM_SVC_PARM_PTR: paDstParms[i].u.uint32 = pMsg->aParms[i - 2].u.pointer.size; break;
447 }
448}
449
450/**
451 * Sets the VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT return parameters.
452 *
453 * @returns VBox status code.
454 * @param pMsg The message which parameters to return to the guest.
455 * @param paDstParms The peek parameter vector.
456 * @param cDstParms The number of peek parameters should be exactly two
457 */
458static int shClSvcMsgSetOldWaitReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
459{
460 /*
461 * Assert sanity.
462 */
463 AssertPtr(pMsg);
464 AssertPtrReturn(paDstParms, VERR_INVALID_POINTER);
465 AssertReturn(cDstParms >= 2, VERR_INVALID_PARAMETER);
466
467 Assert(pMsg->cParms == 2);
468 Assert(pMsg->aParms[0].u.uint32 == pMsg->idMsg);
469 switch (pMsg->idMsg)
470 {
471 case VBOX_SHCL_HOST_MSG_READ_DATA:
472 case VBOX_SHCL_HOST_MSG_FORMATS_REPORT:
473 break;
474 default:
475 AssertFailed();
476 }
477
478 /*
479 * Set the parameters.
480 */
481 if (pMsg->cParms > 0)
482 paDstParms[0] = pMsg->aParms[0];
483 if (pMsg->cParms > 1)
484 paDstParms[1] = pMsg->aParms[1];
485 return VINF_SUCCESS;
486}
487
488/**
489 * Adds a new message to a client'S message queue.
490 *
491 * @param pClient Pointer to the client data structure to add new message to.
492 * @param pMsg Pointer to message to add. The queue then owns the pointer.
493 * @param fAppend Whether to append or prepend the message to the queue.
494 *
495 * @note Caller must enter critical section.
496 */
497void shClSvcMsgAdd(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg, bool fAppend)
498{
499 Assert(RTCritSectIsOwner(&pClient->CritSect));
500 AssertPtr(pMsg);
501
502 LogFlowFunc(("idMsg=%s (%RU32) cParms=%RU32 fAppend=%RTbool\n",
503 ShClHostMsgToStr(pMsg->idMsg), pMsg->idMsg, pMsg->cParms, fAppend));
504
505 if (fAppend)
506 RTListAppend(&pClient->MsgQueue, &pMsg->ListEntry);
507 else
508 RTListPrepend(&pClient->MsgQueue, &pMsg->ListEntry);
509}
510
511
512/**
513 * Appends a message to the client's queue and wake it up.
514 *
515 * @returns VBox status code, though the message is consumed regardless of what
516 * is returned.
517 * @param pClient The client to queue the message on.
518 * @param pMsg The message to queue. Ownership is always
519 * transfered to the queue.
520 *
521 * @note Caller must enter critical section.
522 */
523int shClSvcMsgAddAndWakeupClient(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg)
524{
525 Assert(RTCritSectIsOwner(&pClient->CritSect));
526 AssertPtr(pMsg);
527 AssertPtr(pClient);
528 LogFlowFunc(("idMsg=%s (%u) cParms=%u\n", ShClHostMsgToStr(pMsg->idMsg), pMsg->idMsg, pMsg->cParms));
529
530 RTListAppend(&pClient->MsgQueue, &pMsg->ListEntry);
531 return shClSvcClientWakeup(pClient);
532}
533
534/**
535 * Initializes a Shared Clipboard client.
536 *
537 * @param pClient Client to initialize.
538 * @param uClientID HGCM client ID to assign client to.
539 */
540int shClSvcClientInit(PSHCLCLIENT pClient, uint32_t uClientID)
541{
542 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
543
544 /* Assign the client ID. */
545 pClient->State.uClientID = uClientID;
546
547 RTListInit(&pClient->MsgQueue);
548 pClient->cMsgAllocated = 0;
549
550 RTListInit(&pClient->Legacy.lstCID);
551 pClient->Legacy.cCID = 0;
552
553 LogFlowFunc(("[Client %RU32]\n", pClient->State.uClientID));
554
555 int rc = RTCritSectInit(&pClient->CritSect);
556 if (RT_SUCCESS(rc))
557 {
558 /* Create the client's own event source. */
559 rc = ShClEventSourceCreate(&pClient->EventSrc, 0 /* ID, ignored */);
560 if (RT_SUCCESS(rc))
561 {
562 LogFlowFunc(("[Client %RU32] Using event source %RU32\n", uClientID, pClient->EventSrc.uID));
563
564 /* Reset the client state. */
565 shclSvcClientStateReset(&pClient->State);
566
567 /* (Re-)initialize the client state. */
568 rc = shClSvcClientStateInit(&pClient->State, uClientID);
569
570#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
571 if (RT_SUCCESS(rc))
572 rc = ShClTransferCtxInit(&pClient->Transfers.Ctx);
573#endif
574 }
575 }
576
577 LogFlowFuncLeaveRC(rc);
578 return rc;
579}
580
581/**
582 * Destroys a Shared Clipboard client.
583 *
584 * @param pClient Client to destroy.
585 */
586void shClSvcClientDestroy(PSHCLCLIENT pClient)
587{
588 AssertPtrReturnVoid(pClient);
589
590 LogFlowFunc(("[Client %RU32]\n", pClient->State.uClientID));
591
592 /* Make sure to send a quit message to the guest so that it can terminate gracefully. */
593 RTCritSectEnter(&pClient->CritSect);
594 if (pClient->Pending.uType)
595 {
596 if (pClient->Pending.cParms > 1)
597 HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_QUIT);
598 if (pClient->Pending.cParms > 2)
599 HGCMSvcSetU32(&pClient->Pending.paParms[1], 0);
600 g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
601 pClient->Pending.uType = 0;
602 pClient->Pending.cParms = 0;
603 pClient->Pending.hHandle = NULL;
604 pClient->Pending.paParms = NULL;
605 }
606 RTCritSectLeave(&pClient->CritSect);
607
608 ShClEventSourceDestroy(&pClient->EventSrc);
609
610 shClSvcClientStateDestroy(&pClient->State);
611
612 PSHCLCLIENTLEGACYCID pCidIter, pCidIterNext;
613 RTListForEachSafe(&pClient->Legacy.lstCID, pCidIter, pCidIterNext, SHCLCLIENTLEGACYCID, Node)
614 {
615 RTMemFree(pCidIter);
616 }
617
618 int rc2 = RTCritSectDelete(&pClient->CritSect);
619 AssertRC(rc2);
620
621 ClipboardClientMap::iterator itClient = g_mapClients.find(pClient->State.uClientID);
622 if (itClient != g_mapClients.end())
623 g_mapClients.erase(itClient);
624 else
625 AssertFailed();
626
627 LogFlowFuncLeave();
628}
629
630void shClSvcClientLock(PSHCLCLIENT pClient)
631{
632 int rc2 = RTCritSectEnter(&pClient->CritSect);
633 AssertRC(rc2);
634}
635
636void shClSvcClientUnlock(PSHCLCLIENT pClient)
637{
638 int rc2 = RTCritSectLeave(&pClient->CritSect);
639 AssertRC(rc2);
640}
641
642/**
643 * Resets a Shared Clipboard client.
644 *
645 * @param pClient Client to reset.
646 */
647void shClSvcClientReset(PSHCLCLIENT pClient)
648{
649 if (!pClient)
650 return;
651
652 LogFlowFunc(("[Client %RU32]\n", pClient->State.uClientID));
653 RTCritSectEnter(&pClient->CritSect);
654
655 /* Reset message queue. */
656 shClSvcMsgQueueReset(pClient);
657
658 /* Reset event source. */
659 ShClEventSourceReset(&pClient->EventSrc);
660
661 /* Reset pending state. */
662 RT_ZERO(pClient->Pending);
663
664#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
665 shClSvcClientTransfersReset(pClient);
666#endif
667
668 shclSvcClientStateReset(&pClient->State);
669
670 RTCritSectLeave(&pClient->CritSect);
671}
672
673static int shClSvcClientNegogiateChunkSize(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall,
674 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
675{
676 /*
677 * Validate the request.
678 */
679 ASSERT_GUEST_RETURN(cParms == VBOX_SHCL_CPARMS_NEGOTIATE_CHUNK_SIZE, VERR_WRONG_PARAMETER_COUNT);
680 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
681 uint32_t const cbClientMaxChunkSize = paParms[0].u.uint32;
682 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
683 uint32_t const cbClientChunkSize = paParms[1].u.uint32;
684
685 uint32_t const cbHostMaxChunkSize = VBOX_SHCL_MAX_CHUNK_SIZE; /** @todo Make this configurable. */
686
687 /*
688 * Do the work.
689 */
690 if (cbClientChunkSize == 0) /* Does the client want us to choose? */
691 {
692 paParms[0].u.uint32 = cbHostMaxChunkSize; /* Maximum */
693 paParms[1].u.uint32 = RT_MIN(pClient->State.cbChunkSize, cbHostMaxChunkSize); /* Preferred */
694
695 }
696 else /* The client told us what it supports, so update and report back. */
697 {
698 paParms[0].u.uint32 = RT_MIN(cbClientMaxChunkSize, cbHostMaxChunkSize); /* Maximum */
699 paParms[1].u.uint32 = RT_MIN(cbClientMaxChunkSize, pClient->State.cbChunkSize); /* Preferred */
700 }
701
702 int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
703 if (RT_SUCCESS(rc))
704 {
705 Log(("[Client %RU32] chunk size: %#RU32, max: %#RU32\n",
706 pClient->State.uClientID, paParms[1].u.uint32, paParms[0].u.uint32));
707 }
708 else
709 LogFunc(("pfnCallComplete -> %Rrc\n", rc));
710
711 return VINF_HGCM_ASYNC_EXECUTE;
712}
713
714/**
715 * Implements VBOX_SHCL_GUEST_FN_REPORT_FEATURES.
716 *
717 * @returns VBox status code.
718 * @retval VINF_HGCM_ASYNC_EXECUTE on success (we complete the message here).
719 * @retval VERR_ACCESS_DENIED if not master
720 * @retval VERR_INVALID_PARAMETER if bit 63 in the 2nd parameter isn't set.
721 * @retval VERR_WRONG_PARAMETER_COUNT
722 *
723 * @param pClient The client state.
724 * @param hCall The client's call handle.
725 * @param cParms Number of parameters.
726 * @param paParms Array of parameters.
727 */
728static int shClSvcClientReportFeatures(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall,
729 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
730{
731 /*
732 * Validate the request.
733 */
734 ASSERT_GUEST_RETURN(cParms == 2, VERR_WRONG_PARAMETER_COUNT);
735 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
736 uint64_t const fFeatures0 = paParms[0].u.uint64;
737 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
738 uint64_t const fFeatures1 = paParms[1].u.uint64;
739 ASSERT_GUEST_RETURN(fFeatures1 & VBOX_SHCL_GF_1_MUST_BE_ONE, VERR_INVALID_PARAMETER);
740
741 /*
742 * Do the work.
743 */
744 paParms[0].u.uint64 = g_fHostFeatures0;
745 paParms[1].u.uint64 = 0;
746
747 int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
748 if (RT_SUCCESS(rc))
749 {
750 pClient->State.fGuestFeatures0 = fFeatures0;
751 pClient->State.fGuestFeatures1 = fFeatures1;
752 LogRel2(("Shared Clipboard: Guest reported the following features: %#RX64\n",
753 pClient->State.fGuestFeatures0)); /* Note: fFeatures1 not used yet. */
754 if (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_TRANSFERS)
755 LogRel2(("Shared Clipboard: Guest supports file transfers\n"));
756 }
757 else
758 LogFunc(("pfnCallComplete -> %Rrc\n", rc));
759
760 return VINF_HGCM_ASYNC_EXECUTE;
761}
762
763/**
764 * Implements VBOX_SHCL_GUEST_FN_QUERY_FEATURES.
765 *
766 * @returns VBox status code.
767 * @retval VINF_HGCM_ASYNC_EXECUTE on success (we complete the message here).
768 * @retval VERR_WRONG_PARAMETER_COUNT
769 *
770 * @param hCall The client's call handle.
771 * @param cParms Number of parameters.
772 * @param paParms Array of parameters.
773 */
774static int shClSvcClientQueryFeatures(VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
775{
776 /*
777 * Validate the request.
778 */
779 ASSERT_GUEST_RETURN(cParms == 2, VERR_WRONG_PARAMETER_COUNT);
780 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
781 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
782 ASSERT_GUEST(paParms[1].u.uint64 & RT_BIT_64(63));
783
784 /*
785 * Do the work.
786 */
787 paParms[0].u.uint64 = g_fHostFeatures0;
788 paParms[1].u.uint64 = 0;
789 int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
790 if (RT_FAILURE(rc))
791 LogFunc(("pfnCallComplete -> %Rrc\n", rc));
792
793 return VINF_HGCM_ASYNC_EXECUTE;
794}
795
796/**
797 * Implements VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT.
798 *
799 * @returns VBox status code.
800 * @retval VINF_SUCCESS if a message was pending and is being returned.
801 * @retval VERR_TRY_AGAIN if no message pending and not blocking.
802 * @retval VERR_RESOURCE_BUSY if another read already made a waiting call.
803 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
804 *
805 * @param pClient The client state.
806 * @param hCall The client's call handle.
807 * @param cParms Number of parameters.
808 * @param paParms Array of parameters.
809 * @param fWait Set if we should wait for a message, clear if to return
810 * immediately.
811 *
812 * @note Caller takes and leave the client's critical section.
813 */
814static int shClSvcClientMsgPeek(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool fWait)
815{
816 /*
817 * Validate the request.
818 */
819 ASSERT_GUEST_MSG_RETURN(cParms >= 2, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
820
821 uint64_t idRestoreCheck = 0;
822 uint32_t i = 0;
823 if (paParms[i].type == VBOX_HGCM_SVC_PARM_64BIT)
824 {
825 idRestoreCheck = paParms[0].u.uint64;
826 paParms[0].u.uint64 = 0;
827 i++;
828 }
829 for (; i < cParms; i++)
830 {
831 ASSERT_GUEST_MSG_RETURN(paParms[i].type == VBOX_HGCM_SVC_PARM_32BIT, ("#%u type=%u\n", i, paParms[i].type),
832 VERR_WRONG_PARAMETER_TYPE);
833 paParms[i].u.uint32 = 0;
834 }
835
836 /*
837 * Check restore session ID.
838 */
839 if (idRestoreCheck != 0)
840 {
841 uint64_t idRestore = g_pHelpers->pfnGetVMMDevSessionId(g_pHelpers);
842 if (idRestoreCheck != idRestore)
843 {
844 paParms[0].u.uint64 = idRestore;
845 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VERR_VM_RESTORED (%#RX64 -> %#RX64)\n",
846 pClient->State.uClientID, idRestoreCheck, idRestore));
847 return VERR_VM_RESTORED;
848 }
849 Assert(!g_pHelpers->pfnIsCallRestored(hCall));
850 }
851
852 /*
853 * Return information about the first message if one is pending in the list.
854 */
855 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
856 if (pFirstMsg)
857 {
858 shClSvcMsgSetPeekReturn(pFirstMsg, paParms, cParms);
859 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VINF_SUCCESS (idMsg=%s (%u), cParms=%u)\n",
860 pClient->State.uClientID, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
861 return VINF_SUCCESS;
862 }
863
864 /*
865 * If we cannot wait, fail the call.
866 */
867 if (!fWait)
868 {
869 LogFlowFunc(("[Client %RU32] GUEST_MSG_PEEK_NOWAIT -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
870 return VERR_TRY_AGAIN;
871 }
872
873 /*
874 * Wait for the host to queue a message for this client.
875 */
876 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n",
877 pClient->State.uClientID), VERR_RESOURCE_BUSY);
878 pClient->Pending.hHandle = hCall;
879 pClient->Pending.cParms = cParms;
880 pClient->Pending.paParms = paParms;
881 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT;
882 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
883 return VINF_HGCM_ASYNC_EXECUTE;
884}
885
886/**
887 * Implements VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT.
888 *
889 * @returns VBox status code.
890 * @retval VINF_SUCCESS if a message was pending and is being returned.
891 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
892 *
893 * @param pClient The client state.
894 * @param hCall The client's call handle.
895 * @param cParms Number of parameters.
896 * @param paParms Array of parameters.
897 *
898 * @note Caller takes and leave the client's critical section.
899 */
900static int shClSvcClientMsgOldGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
901{
902 /*
903 * Validate input.
904 */
905 ASSERT_GUEST_RETURN(cParms == VBOX_SHCL_CPARMS_GET_HOST_MSG_OLD, VERR_WRONG_PARAMETER_COUNT);
906 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* id32Msg */
907 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* f32Formats */
908
909 paParms[0].u.uint32 = 0;
910 paParms[1].u.uint32 = 0;
911
912 /*
913 * If there is a message pending we can return immediately.
914 */
915 int rc;
916 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
917 if (pFirstMsg)
918 {
919 LogFlowFunc(("[Client %RU32] uMsg=%s (%RU32), cParms=%RU32\n", pClient->State.uClientID,
920 ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
921
922 rc = shClSvcMsgSetOldWaitReturn(pFirstMsg, paParms, cParms);
923 AssertPtr(g_pHelpers);
924 rc = g_pHelpers->pfnCallComplete(hCall, rc);
925 if (rc != VERR_CANCELLED)
926 {
927 RTListNodeRemove(&pFirstMsg->ListEntry);
928 shClSvcMsgFree(pClient, pFirstMsg);
929
930 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
931 }
932 }
933 /*
934 * Otherwise we must wait.
935 */
936 else
937 {
938 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n", pClient->State.uClientID),
939 VERR_RESOURCE_BUSY);
940
941 pClient->Pending.hHandle = hCall;
942 pClient->Pending.cParms = cParms;
943 pClient->Pending.paParms = paParms;
944 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT;
945
946 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
947
948 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
949 }
950
951 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
952 return rc;
953}
954
955/**
956 * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
957 *
958 * @returns VBox status code.
959 * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
960 * @retval VERR_TRY_AGAIN if no message pending.
961 * @retval VERR_BUFFER_OVERFLOW if a parmeter buffer is too small. The buffer
962 * size was updated to reflect the required size, though this isn't yet
963 * forwarded to the guest. (The guest is better of using peek with
964 * parameter count + 2 parameters to get the sizes.)
965 * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
966 * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
967 *
968 * @param pClient The client state.
969 * @param hCall The client's call handle.
970 * @param cParms Number of parameters.
971 * @param paParms Array of parameters.
972 *
973 * @note Called from within pClient->CritSect.
974 */
975static int shClSvcClientMsgGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
976{
977 /*
978 * Validate the request.
979 */
980 uint32_t const idMsgExpected = cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT ? paParms[0].u.uint32
981 : cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT ? paParms[0].u.uint64
982 : UINT32_MAX;
983
984 /*
985 * Return information about the first message if one is pending in the list.
986 */
987 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
988 if (pFirstMsg)
989 {
990 LogFlowFunc(("First message is: %s (%u), cParms=%RU32\n", ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
991
992 ASSERT_GUEST_MSG_RETURN(pFirstMsg->idMsg == idMsgExpected || idMsgExpected == UINT32_MAX,
993 ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
994 pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->cParms,
995 idMsgExpected, ShClHostMsgToStr(idMsgExpected), cParms),
996 VERR_MISMATCH);
997 ASSERT_GUEST_MSG_RETURN(pFirstMsg->cParms == cParms,
998 ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
999 pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->cParms,
1000 idMsgExpected, ShClHostMsgToStr(idMsgExpected), cParms),
1001 VERR_WRONG_PARAMETER_COUNT);
1002
1003 /* Check the parameter types. */
1004 for (uint32_t i = 0; i < cParms; i++)
1005 ASSERT_GUEST_MSG_RETURN(pFirstMsg->aParms[i].type == paParms[i].type,
1006 ("param #%u: type %u, caller expected %u (idMsg=%u %s)\n", i, pFirstMsg->aParms[i].type,
1007 paParms[i].type, pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg)),
1008 VERR_WRONG_PARAMETER_TYPE);
1009 /*
1010 * Copy out the parameters.
1011 *
1012 * No assertions on buffer overflows, and keep going till the end so we can
1013 * communicate all the required buffer sizes.
1014 */
1015 int rc = VINF_SUCCESS;
1016 for (uint32_t i = 0; i < cParms; i++)
1017 switch (pFirstMsg->aParms[i].type)
1018 {
1019 case VBOX_HGCM_SVC_PARM_32BIT:
1020 paParms[i].u.uint32 = pFirstMsg->aParms[i].u.uint32;
1021 break;
1022
1023 case VBOX_HGCM_SVC_PARM_64BIT:
1024 paParms[i].u.uint64 = pFirstMsg->aParms[i].u.uint64;
1025 break;
1026
1027 case VBOX_HGCM_SVC_PARM_PTR:
1028 {
1029 uint32_t const cbSrc = pFirstMsg->aParms[i].u.pointer.size;
1030 uint32_t const cbDst = paParms[i].u.pointer.size;
1031 paParms[i].u.pointer.size = cbSrc; /** @todo Check if this is safe in other layers...
1032 * Update: Safe, yes, but VMMDevHGCM doesn't pass it along. */
1033 if (cbSrc <= cbDst)
1034 memcpy(paParms[i].u.pointer.addr, pFirstMsg->aParms[i].u.pointer.addr, cbSrc);
1035 else
1036 {
1037 AssertMsgFailed(("#%u: cbSrc=%RU32 is bigger than cbDst=%RU32\n", i, cbSrc, cbDst));
1038 rc = VERR_BUFFER_OVERFLOW;
1039 }
1040 break;
1041 }
1042
1043 default:
1044 AssertMsgFailed(("#%u: %u\n", i, pFirstMsg->aParms[i].type));
1045 rc = VERR_INTERNAL_ERROR;
1046 break;
1047 }
1048 if (RT_SUCCESS(rc))
1049 {
1050 /*
1051 * Complete the message and remove the pending message unless the
1052 * guest raced us and cancelled this call in the meantime.
1053 */
1054 AssertPtr(g_pHelpers);
1055 rc = g_pHelpers->pfnCallComplete(hCall, rc);
1056
1057 LogFlowFunc(("[Client %RU32] pfnCallComplete -> %Rrc\n", pClient->State.uClientID, rc));
1058
1059 if (rc != VERR_CANCELLED)
1060 {
1061 RTListNodeRemove(&pFirstMsg->ListEntry);
1062 shClSvcMsgFree(pClient, pFirstMsg);
1063 }
1064
1065 return VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
1066 }
1067
1068 LogFlowFunc(("[Client %RU32] Returning %Rrc\n", pClient->State.uClientID, rc));
1069 return rc;
1070 }
1071
1072 paParms[0].u.uint32 = 0;
1073 paParms[1].u.uint32 = 0;
1074 LogFlowFunc(("[Client %RU32] -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
1075 return VERR_TRY_AGAIN;
1076}
1077
1078/**
1079 * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
1080 *
1081 * @returns VBox status code.
1082 * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
1083 * @retval VERR_TRY_AGAIN if no message pending.
1084 * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
1085 * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
1086 *
1087 * @param pClient The client state.
1088 * @param cParms Number of parameters.
1089 *
1090 * @note Called from within pClient->CritSect.
1091 */
1092static int shClSvcClientMsgCancel(PSHCLCLIENT pClient, uint32_t cParms)
1093{
1094 /*
1095 * Validate the request.
1096 */
1097 ASSERT_GUEST_MSG_RETURN(cParms == 0, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
1098
1099 /*
1100 * Execute.
1101 */
1102 if (pClient->Pending.uType != 0)
1103 {
1104 LogFlowFunc(("[Client %RU32] Cancelling waiting thread, isPending=%d, pendingNumParms=%RU32, m_idSession=%x\n",
1105 pClient->State.uClientID, pClient->Pending.uType, pClient->Pending.cParms, pClient->State.uSessionID));
1106
1107 /*
1108 * The PEEK call is simple: At least two parameters, all set to zero before sleeping.
1109 */
1110 int rcComplete;
1111 if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
1112 {
1113 Assert(pClient->Pending.cParms >= 2);
1114 if (pClient->Pending.paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT)
1115 HGCMSvcSetU64(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
1116 else
1117 HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
1118 rcComplete = VINF_TRY_AGAIN;
1119 }
1120 /*
1121 * The MSG_OLD call is complicated, though we're
1122 * generally here to wake up someone who is peeking and have two parameters.
1123 * If there aren't two parameters, fail the call.
1124 */
1125 else
1126 {
1127 Assert(pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
1128 if (pClient->Pending.cParms > 0)
1129 HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
1130 if (pClient->Pending.cParms > 1)
1131 HGCMSvcSetU32(&pClient->Pending.paParms[1], 0);
1132 rcComplete = pClient->Pending.cParms == 2 ? VINF_SUCCESS : VERR_TRY_AGAIN;
1133 }
1134
1135 g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, rcComplete);
1136
1137 pClient->Pending.hHandle = NULL;
1138 pClient->Pending.paParms = NULL;
1139 pClient->Pending.cParms = 0;
1140 pClient->Pending.uType = 0;
1141 return VINF_SUCCESS;
1142 }
1143 return VWRN_NOT_FOUND;
1144}
1145
1146
1147/**
1148 * Wakes up a pending client (i.e. waiting for new messages).
1149 *
1150 * @returns VBox status code.
1151 * @retval VINF_NO_CHANGE if the client is not in pending mode.
1152 *
1153 * @param pClient Client to wake up.
1154 * @note Caller must enter pClient->CritSect.
1155 */
1156int shClSvcClientWakeup(PSHCLCLIENT pClient)
1157{
1158 Assert(RTCritSectIsOwner(&pClient->CritSect));
1159 int rc = VINF_NO_CHANGE;
1160
1161 if (pClient->Pending.uType != 0)
1162 {
1163 LogFunc(("[Client %RU32] Waking up ...\n", pClient->State.uClientID));
1164
1165 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
1166 AssertReturn(pFirstMsg, VERR_INTERNAL_ERROR);
1167
1168 LogFunc(("[Client %RU32] Current host message is %s (%RU32), cParms=%RU32\n",
1169 pClient->State.uClientID, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
1170
1171 if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
1172 shClSvcMsgSetPeekReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
1173 else if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT) /* Legacy, Guest Additions < 6.1. */
1174 shClSvcMsgSetOldWaitReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
1175 else
1176 AssertMsgFailedReturn(("pClient->Pending.uType=%u\n", pClient->Pending.uType), VERR_INTERNAL_ERROR_3);
1177
1178 rc = g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
1179
1180 if ( rc != VERR_CANCELLED
1181 && pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT)
1182 {
1183 RTListNodeRemove(&pFirstMsg->ListEntry);
1184 shClSvcMsgFree(pClient, pFirstMsg);
1185 }
1186
1187 pClient->Pending.hHandle = NULL;
1188 pClient->Pending.paParms = NULL;
1189 pClient->Pending.cParms = 0;
1190 pClient->Pending.uType = 0;
1191 }
1192 else
1193 LogFunc(("[Client %RU32] Not in pending state, skipping wakeup\n", pClient->State.uClientID));
1194
1195 return rc;
1196}
1197
1198/**
1199 * Requests to read clipboard data from the guest.
1200 *
1201 * @returns VBox status code.
1202 * @param pClient Client to request to read data form.
1203 * @param fFormats The formats being requested, OR'ed together (VBOX_SHCL_FMT_XXX).
1204 * @param ppEvent Where to return the event for waiting for new data on success. Optional.
1205 * Must be released by the caller with ShClEventRelease().
1206 */
1207int ShClSvcGuestDataRequest(PSHCLCLIENT pClient, SHCLFORMATS fFormats, PSHCLEVENT *ppEvent)
1208{
1209 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
1210
1211 LogFlowFunc(("fFormats=%#x\n", fFormats));
1212
1213 int rc = VERR_NOT_SUPPORTED;
1214
1215 /* Generate a separate message for every (valid) format we support. */
1216 while (fFormats)
1217 {
1218 /* Pick the next format to get from the mask: */
1219 /** @todo Make format reporting precedence configurable? */
1220 SHCLFORMAT fFormat;
1221 if (fFormats & VBOX_SHCL_FMT_UNICODETEXT)
1222 fFormat = VBOX_SHCL_FMT_UNICODETEXT;
1223 else if (fFormats & VBOX_SHCL_FMT_BITMAP)
1224 fFormat = VBOX_SHCL_FMT_BITMAP;
1225 else if (fFormats & VBOX_SHCL_FMT_HTML)
1226 fFormat = VBOX_SHCL_FMT_HTML;
1227 else
1228 AssertMsgFailedBreak(("%#x\n", fFormats));
1229
1230 /* Remove it from the mask. */
1231 fFormats &= ~fFormat;
1232
1233#ifdef LOG_ENABLED
1234 char *pszFmt = ShClFormatsToStrA(fFormat);
1235 AssertPtrReturn(pszFmt, VERR_NO_MEMORY);
1236 LogRel2(("Shared Clipboard: Requesting guest clipboard data in format '%s'\n", pszFmt));
1237 RTStrFree(pszFmt);
1238#endif
1239 /*
1240 * Allocate messages, one for each format.
1241 */
1242 PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient,
1243 pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID
1244 ? VBOX_SHCL_HOST_MSG_READ_DATA_CID : VBOX_SHCL_HOST_MSG_READ_DATA,
1245 2);
1246 if (pMsg)
1247 {
1248 /*
1249 * Enter the critical section and generate an event.
1250 */
1251 RTCritSectEnter(&pClient->CritSect);
1252
1253 PSHCLEVENT pEvent;
1254 rc = ShClEventSourceGenerateAndRegisterEvent(&pClient->EventSrc, &pEvent);
1255 if (RT_SUCCESS(rc))
1256 {
1257 LogFlowFunc(("fFormats=%#x -> fFormat=%#x, idEvent=%#x\n", fFormats, fFormat, pEvent->idEvent));
1258
1259 const uint64_t uCID = VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID, pClient->EventSrc.uID, pEvent->idEvent);
1260
1261 rc = VINF_SUCCESS;
1262
1263 /* Save the context ID in our legacy cruft if we have to deal with old(er) Guest Additions (< 6.1). */
1264 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1265 {
1266 AssertStmt(pClient->Legacy.cCID < 4096, rc = VERR_TOO_MUCH_DATA);
1267 if (RT_SUCCESS(rc))
1268 {
1269 PSHCLCLIENTLEGACYCID pCID = (PSHCLCLIENTLEGACYCID)RTMemAlloc(sizeof(SHCLCLIENTLEGACYCID));
1270 if (pCID)
1271 {
1272 pCID->uCID = uCID;
1273 pCID->enmType = 0; /* Not used yet. */
1274 pCID->uFormat = fFormat;
1275 RTListAppend(&pClient->Legacy.lstCID, &pCID->Node);
1276 pClient->Legacy.cCID++;
1277 }
1278 else
1279 rc = VERR_NO_MEMORY;
1280 }
1281 }
1282
1283 if (RT_SUCCESS(rc))
1284 {
1285 /*
1286 * Format the message.
1287 */
1288 if (pMsg->idMsg == VBOX_SHCL_HOST_MSG_READ_DATA_CID)
1289 HGCMSvcSetU64(&pMsg->aParms[0], uCID);
1290 else
1291 HGCMSvcSetU32(&pMsg->aParms[0], VBOX_SHCL_HOST_MSG_READ_DATA);
1292 HGCMSvcSetU32(&pMsg->aParms[1], fFormat);
1293
1294 shClSvcMsgAdd(pClient, pMsg, true /* fAppend */);
1295
1296 /* Return event handle to the caller if requested. */
1297 if (ppEvent)
1298 {
1299 *ppEvent = pEvent;
1300 }
1301
1302 shClSvcClientWakeup(pClient);
1303 }
1304
1305 /* Remove event from list if caller did not request event handle or in case
1306 * of failure (in this case caller should not release event). */
1307 if ( RT_FAILURE(rc)
1308 || !ppEvent)
1309 {
1310 ShClEventRelease(pEvent);
1311 }
1312 }
1313 else
1314 rc = VERR_SHCLPB_MAX_EVENTS_REACHED;
1315
1316 RTCritSectLeave(&pClient->CritSect);
1317
1318 if (RT_FAILURE(rc))
1319 shClSvcMsgFree(pClient, pMsg);
1320 }
1321 else
1322 rc = VERR_NO_MEMORY;
1323
1324 if (RT_FAILURE(rc))
1325 break;
1326 }
1327
1328 if (RT_FAILURE(rc))
1329 LogRel(("Shared Clipboard: Requesting data in formats %#x from guest failed with %Rrc\n", fFormats, rc));
1330
1331 LogFlowFuncLeaveRC(rc);
1332 return rc;
1333}
1334
1335/**
1336 * Signals the host that clipboard data from the guest has been received.
1337 *
1338 * @returns VBox status code. Returns VERR_NOT_FOUND when related event ID was not found.
1339 * @param pClient Client the guest clipboard data was received from.
1340 * @param pCmdCtx Client command context.
1341 * @param uFormat Clipboard format of data received.
1342 * @param pvData Pointer to clipboard data received. This can be
1343 * NULL if @a cbData is zero.
1344 * @param cbData Size (in bytes) of clipboard data received.
1345 * This can be zero.
1346 */
1347int ShClSvcGuestDataSignal(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
1348{
1349 LogFlowFuncEnter();
1350 RT_NOREF(uFormat);
1351
1352 /*
1353 * Validate input.
1354 */
1355 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
1356 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
1357 if (cbData > 0)
1358 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1359
1360 const SHCLEVENTID idEvent = VBOX_SHCL_CONTEXTID_GET_EVENT(pCmdCtx->uContextID);
1361 AssertMsgReturn(idEvent != NIL_SHCLEVENTID, ("NIL event in context ID %#RX64\n", pCmdCtx->uContextID), VERR_WRONG_ORDER);
1362
1363 PSHCLEVENT pEvent = ShClEventSourceGetFromId(&pClient->EventSrc, idEvent);
1364 AssertMsgReturn(pEvent != NULL, ("Event %#x not found\n", idEvent), VERR_NOT_FOUND);
1365
1366 /*
1367 * Make a copy of the data so we can attach it to the signal.
1368 *
1369 * Note! We still signal the waiter should we run out of memory,
1370 * because otherwise it will be stuck waiting.
1371 */
1372 int rc = VINF_SUCCESS;
1373 PSHCLEVENTPAYLOAD pPayload = NULL;
1374 if (cbData > 0)
1375 rc = ShClPayloadAlloc(idEvent, pvData, cbData, &pPayload);
1376
1377 /*
1378 * Signal the event.
1379 */
1380 int rc2 = ShClEventSignal(pEvent, pPayload);
1381 if (RT_FAILURE(rc2))
1382 {
1383 rc = rc2;
1384 ShClPayloadFree(pPayload);
1385 LogRel(("Shared Clipboard: Signalling of guest clipboard data to the host failed: %Rrc\n", rc));
1386 }
1387
1388 LogFlowFuncLeaveRC(rc);
1389 return rc;
1390}
1391
1392/**
1393 * Reports available VBox clipboard formats to the guest.
1394 *
1395 * @note Host backend callers must check if it's active (use
1396 * ShClSvcIsBackendActive) before calling to prevent mixing up the
1397 * VRDE clipboard.
1398 *
1399 * @returns VBox status code.
1400 * @param pClient Client to report clipboard formats to.
1401 * @param fFormats The formats to report (VBOX_SHCL_FMT_XXX), zero
1402 * is okay (empty the clipboard).
1403 */
1404int ShClSvcHostReportFormats(PSHCLCLIENT pClient, SHCLFORMATS fFormats)
1405{
1406 /*
1407 * Check if the service mode allows this operation and whether the guest is
1408 * supposed to be reading from the host. Otherwise, silently ignore reporting
1409 * formats and return VINF_SUCCESS in order to do not trigger client
1410 * termination in svcConnect().
1411 */
1412 uint32_t uMode = ShClSvcGetMode();
1413 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1414 || uMode == VBOX_SHCL_MODE_HOST_TO_GUEST)
1415 { /* likely */ }
1416 else
1417 return VINF_SUCCESS;
1418
1419 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
1420
1421 LogFlowFunc(("fFormats=%#x\n", fFormats));
1422
1423#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1424 /*
1425 * If transfer mode is set to disabled, don't report the URI list format to the guest.
1426 */
1427 if (!(g_fTransferMode & VBOX_SHCL_TRANSFER_MODE_ENABLED))
1428 {
1429 fFormats &= ~VBOX_SHCL_FMT_URI_LIST;
1430 LogRel2(("Shared Clipboard: File transfers are disabled, skipping reporting those to the guest\n"));
1431 }
1432#endif
1433
1434#ifdef LOG_ENABLED
1435 char *pszFmts = ShClFormatsToStrA(fFormats);
1436 AssertPtrReturn(pszFmts, VERR_NO_MEMORY);
1437 LogRel2(("Shared Clipboard: Reporting formats '%s' to guest\n", pszFmts));
1438 RTStrFree(pszFmts);
1439#endif
1440
1441 /*
1442 * Allocate a message, populate parameters and post it to the client.
1443 */
1444 int rc;
1445 PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient, VBOX_SHCL_HOST_MSG_FORMATS_REPORT, 2);
1446 if (pMsg)
1447 {
1448 HGCMSvcSetU32(&pMsg->aParms[0], VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
1449 HGCMSvcSetU32(&pMsg->aParms[1], fFormats);
1450
1451 RTCritSectEnter(&pClient->CritSect);
1452 shClSvcMsgAddAndWakeupClient(pClient, pMsg);
1453 RTCritSectLeave(&pClient->CritSect);
1454
1455#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1456 /* If we announce an URI list, create a transfer locally and also tell the guest to create
1457 * a transfer on the guest side. */
1458 if (fFormats & VBOX_SHCL_FMT_URI_LIST)
1459 {
1460 rc = shClSvcTransferStart(pClient, SHCLTRANSFERDIR_TO_REMOTE, SHCLSOURCE_LOCAL,
1461 NULL /* pTransfer */);
1462 if (RT_SUCCESS(rc))
1463 rc = shClSvcSetSource(pClient, SHCLSOURCE_LOCAL);
1464
1465 if (RT_FAILURE(rc))
1466 LogRel(("Shared Clipboard: Initializing host write transfer failed with %Rrc\n", rc));
1467 }
1468 else
1469#endif
1470 {
1471 rc = VINF_SUCCESS;
1472 }
1473 }
1474 else
1475 rc = VERR_NO_MEMORY;
1476
1477 if (RT_FAILURE(rc))
1478 LogRel(("Shared Clipboard: Reporting formats %#x to guest failed with %Rrc\n", fFormats, rc));
1479
1480 LogFlowFuncLeaveRC(rc);
1481 return rc;
1482}
1483
1484
1485/**
1486 * Handles the VBOX_SHCL_GUEST_FN_REPORT_FORMATS message from the guest.
1487 */
1488static int shClSvcClientReportFormats(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1489{
1490 /*
1491 * Check if the service mode allows this operation and whether the guest is
1492 * supposed to be reading from the host.
1493 */
1494 uint32_t uMode = ShClSvcGetMode();
1495 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1496 || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
1497 { /* likely */ }
1498 else
1499 return VERR_ACCESS_DENIED;
1500
1501 /*
1502 * Digest parameters.
1503 */
1504 ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS
1505 || ( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B
1506 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
1507 VERR_WRONG_PARAMETER_COUNT);
1508
1509 uintptr_t iParm = 0;
1510 if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
1511 {
1512 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1513 /* no defined value, so just ignore it */
1514 iParm++;
1515 }
1516 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1517 uint32_t const fFormats = paParms[iParm].u.uint32;
1518 iParm++;
1519 if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
1520 {
1521 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1522 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1523 iParm++;
1524 }
1525 Assert(iParm == cParms);
1526
1527 /*
1528 * Report the formats.
1529 *
1530 * We ignore empty reports if the guest isn't the clipboard owner, this
1531 * prevents a freshly booted guest with an empty clibpoard from clearing
1532 * the host clipboard on startup. Likewise, when a guest shutdown it will
1533 * typically issue an empty report in case it's the owner, we don't want
1534 * that to clear host content either.
1535 */
1536 int rc;
1537 if (!fFormats && pClient->State.enmSource != SHCLSOURCE_REMOTE)
1538 rc = VINF_SUCCESS;
1539 else
1540 {
1541 rc = shClSvcSetSource(pClient, SHCLSOURCE_REMOTE);
1542 if (RT_SUCCESS(rc))
1543 {
1544 rc = RTCritSectEnter(&g_CritSect);
1545 if (RT_SUCCESS(rc))
1546 {
1547 if (g_ExtState.pfnExtension)
1548 {
1549 SHCLEXTPARMS parms;
1550 RT_ZERO(parms);
1551 parms.uFormat = fFormats;
1552
1553 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof(parms));
1554 }
1555 else
1556 {
1557#ifdef LOG_ENABLED
1558 char *pszFmts = ShClFormatsToStrA(fFormats);
1559 if (pszFmts)
1560 {
1561 LogRel2(("Shared Clipboard: Guest reported formats '%s' to host\n", pszFmts));
1562 RTStrFree(pszFmts);
1563 }
1564#endif
1565 rc = ShClBackendFormatAnnounce(pClient, fFormats);
1566 if (RT_FAILURE(rc))
1567 LogRel(("Shared Clipboard: Reporting guest clipboard formats to the host failed with %Rrc\n", rc));
1568 }
1569
1570 RTCritSectLeave(&g_CritSect);
1571 }
1572 else
1573 LogRel2(("Shared Clipboard: Unable to take internal lock while receiving guest clipboard announcement: %Rrc\n", rc));
1574 }
1575 }
1576
1577 return rc;
1578}
1579
1580/**
1581 * Called when the guest wants to read host clipboard data.
1582 * Handles the VBOX_SHCL_GUEST_FN_DATA_READ message.
1583 *
1584 * @returns VBox status code.
1585 * @retval VINF_BUFFER_OVERFLOW if the guest supplied a smaller buffer than needed in order to read the host clipboard data.
1586 * @param pClient Client that wants to read host clipboard data.
1587 * @param cParms Number of HGCM parameters supplied in \a paParms.
1588 * @param paParms Array of HGCM parameters.
1589 */
1590static int shClSvcClientReadData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1591{
1592 LogFlowFuncEnter();
1593
1594 /*
1595 * Check if the service mode allows this operation and whether the guest is
1596 * supposed to be reading from the host.
1597 */
1598 uint32_t uMode = ShClSvcGetMode();
1599 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1600 || uMode == VBOX_SHCL_MODE_HOST_TO_GUEST)
1601 { /* likely */ }
1602 else
1603 return VERR_ACCESS_DENIED;
1604
1605 /*
1606 * Digest parameters.
1607 *
1608 * We are dragging some legacy here from the 6.1 dev cycle, a 5 parameter
1609 * variant which prepends a 64-bit context ID (RAZ as meaning not defined),
1610 * a 32-bit flag (MBZ, no defined meaning) and switches the last two parameters.
1611 */
1612 ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_DATA_READ
1613 || ( cParms == VBOX_SHCL_CPARMS_DATA_READ_61B
1614 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
1615 VERR_WRONG_PARAMETER_COUNT);
1616
1617 uintptr_t iParm = 0;
1618 SHCLCLIENTCMDCTX cmdCtx;
1619 RT_ZERO(cmdCtx);
1620 if (cParms == VBOX_SHCL_CPARMS_DATA_READ_61B)
1621 {
1622 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1623 /* This has no defined meaning and was never used, however the guest passed stuff, so ignore it and leave idContext=0. */
1624 iParm++;
1625 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1626 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1627 iParm++;
1628 }
1629
1630 SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
1631 uint32_t cbData = 0;
1632 void *pvData = NULL;
1633
1634 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1635 uFormat = paParms[iParm].u.uint32;
1636 iParm++;
1637 if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
1638 {
1639 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1640 pvData = paParms[iParm].u.pointer.addr;
1641 cbData = paParms[iParm].u.pointer.size;
1642 iParm++;
1643 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
1644 iParm++;
1645 }
1646 else
1647 {
1648 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
1649 iParm++;
1650 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1651 pvData = paParms[iParm].u.pointer.addr;
1652 cbData = paParms[iParm].u.pointer.size;
1653 iParm++;
1654 }
1655 Assert(iParm == cParms);
1656
1657 /*
1658 * For some reason we need to do this (makes absolutely no sense to bird).
1659 */
1660 /** @todo r=bird: I really don't get why you need the State.POD.uFormat
1661 * member. I'm sure there is a reason. Incomplete code? */
1662 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1663 {
1664 if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
1665 pClient->State.POD.uFormat = uFormat;
1666 }
1667
1668#ifdef LOG_ENABLED
1669 char *pszFmt = ShClFormatsToStrA(uFormat);
1670 AssertPtrReturn(pszFmt, VERR_NO_MEMORY);
1671 LogRel2(("Shared Clipboard: Guest wants to read %RU32 bytes host clipboard data in format '%s'\n", cbData, pszFmt));
1672 RTStrFree(pszFmt);
1673#endif
1674
1675 /*
1676 * Do the reading.
1677 */
1678 uint32_t cbActual = 0;
1679
1680 int rc = RTCritSectEnter(&g_CritSect);
1681 AssertRCReturn(rc, rc);
1682
1683 /* If there is a service extension active, try reading data from it first. */
1684 if (g_ExtState.pfnExtension)
1685 {
1686 SHCLEXTPARMS parms;
1687 RT_ZERO(parms);
1688
1689 parms.uFormat = uFormat;
1690 parms.u.pvData = pvData;
1691 parms.cbData = cbData;
1692
1693 g_ExtState.fReadingData = true;
1694
1695 /* Read clipboard data from the extension. */
1696 rc = g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ, &parms, sizeof(parms));
1697
1698 LogRel2(("Shared Clipboard: Read extension clipboard data (fDelayedAnnouncement=%RTbool, fDelayedFormats=%#x, max %RU32 bytes), got %RU32 bytes: rc=%Rrc\n",
1699 g_ExtState.fDelayedAnnouncement, g_ExtState.fDelayedFormats, cbData, parms.cbData, rc));
1700
1701 /* Did the extension send the clipboard formats yet?
1702 * Otherwise, do this now. */
1703 if (g_ExtState.fDelayedAnnouncement)
1704 {
1705 int rc2 = ShClSvcHostReportFormats(pClient, g_ExtState.fDelayedFormats);
1706 AssertRC(rc2);
1707
1708 g_ExtState.fDelayedAnnouncement = false;
1709 g_ExtState.fDelayedFormats = 0;
1710 }
1711
1712 g_ExtState.fReadingData = false;
1713
1714 if (RT_SUCCESS(rc))
1715 cbActual = parms.cbData;
1716 }
1717 else
1718 {
1719 rc = ShClBackendReadData(pClient, &cmdCtx, uFormat, pvData, cbData, &cbActual);
1720 if (RT_SUCCESS(rc))
1721 LogRel2(("Shared Clipboard: Read host clipboard data (max %RU32 bytes), got %RU32 bytes\n", cbData, cbActual));
1722 else
1723 LogRel(("Shared Clipboard: Reading host clipboard data failed with %Rrc\n", rc));
1724 }
1725
1726 if (RT_SUCCESS(rc))
1727 {
1728 /* Return the actual size required to fullfil the request. */
1729 if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
1730 HGCMSvcSetU32(&paParms[2], cbActual);
1731 else
1732 HGCMSvcSetU32(&paParms[3], cbActual);
1733
1734 /* If the data to return exceeds the buffer the guest supplies, tell it (and let it try again). */
1735 if (cbActual >= cbData)
1736 rc = VINF_BUFFER_OVERFLOW;
1737 }
1738
1739 RTCritSectLeave(&g_CritSect);
1740
1741 LogFlowFuncLeaveRC(rc);
1742 return rc;
1743}
1744
1745/**
1746 * Called when the guest writes clipboard data to the host.
1747 * Handles the VBOX_SHCL_GUEST_FN_DATA_WRITE message.
1748 *
1749 * @returns VBox status code.
1750 * @param pClient Client that wants to read host clipboard data.
1751 * @param cParms Number of HGCM parameters supplied in \a paParms.
1752 * @param paParms Array of HGCM parameters.
1753 */
1754int shClSvcClientWriteData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1755{
1756 LogFlowFuncEnter();
1757
1758 /*
1759 * Check if the service mode allows this operation and whether the guest is
1760 * supposed to be reading from the host.
1761 */
1762 uint32_t uMode = ShClSvcGetMode();
1763 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1764 || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
1765 { /* likely */ }
1766 else
1767 return VERR_ACCESS_DENIED;
1768
1769 const bool fReportsContextID = RT_BOOL(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID);
1770
1771 /*
1772 * Digest parameters.
1773 *
1774 * There are 3 different format here, formatunately no parameters have been
1775 * switch around so it's plain sailing compared to the DATA_READ message.
1776 */
1777 ASSERT_GUEST_RETURN(fReportsContextID
1778 ? cParms == VBOX_SHCL_CPARMS_DATA_WRITE || cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B
1779 : cParms == VBOX_SHCL_CPARMS_DATA_WRITE_OLD,
1780 VERR_WRONG_PARAMETER_COUNT);
1781
1782 uintptr_t iParm = 0;
1783 SHCLCLIENTCMDCTX cmdCtx;
1784 RT_ZERO(cmdCtx);
1785 if (cParms > VBOX_SHCL_CPARMS_DATA_WRITE_OLD)
1786 {
1787 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1788 cmdCtx.uContextID = paParms[iParm].u.uint64;
1789 iParm++;
1790 }
1791 else
1792 {
1793 /* Older Guest Additions (< 6.1) did not supply a context ID.
1794 * We dig it out from our saved context ID list then a bit down below. */
1795 }
1796
1797 if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
1798 {
1799 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1800 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1801 iParm++;
1802 }
1803
1804 SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
1805 uint32_t cbData = 0;
1806 void *pvData = NULL;
1807
1808 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* Format bit. */
1809 uFormat = paParms[iParm].u.uint32;
1810 iParm++;
1811 if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
1812 {
1813 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* "cbData" - duplicates buffer size. */
1814 iParm++;
1815 }
1816 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1817 pvData = paParms[iParm].u.pointer.addr;
1818 cbData = paParms[iParm].u.pointer.size;
1819 iParm++;
1820 Assert(iParm == cParms);
1821
1822 /*
1823 * Handle / check context ID.
1824 */
1825 if (!fReportsContextID) /* Do we have to deal with old(er) GAs (< 6.1) which don't support context IDs? Dig out the context ID then. */
1826 {
1827 PSHCLCLIENTLEGACYCID pCID = NULL;
1828 PSHCLCLIENTLEGACYCID pCIDIter;
1829 RTListForEach(&pClient->Legacy.lstCID, pCIDIter, SHCLCLIENTLEGACYCID, Node) /* Slow, but does the job for now. */
1830 {
1831 if (pCIDIter->uFormat == uFormat)
1832 {
1833 pCID = pCIDIter;
1834 break;
1835 }
1836 }
1837
1838 ASSERT_GUEST_MSG_RETURN(pCID != NULL, ("Context ID for format %#x not found\n", uFormat), VERR_INVALID_CONTEXT);
1839 cmdCtx.uContextID = pCID->uCID;
1840
1841 /* Not needed anymore; clean up. */
1842 Assert(pClient->Legacy.cCID);
1843 pClient->Legacy.cCID--;
1844 RTListNodeRemove(&pCID->Node);
1845 RTMemFree(pCID);
1846 }
1847
1848 uint64_t const idCtxExpected = VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID, pClient->EventSrc.uID,
1849 VBOX_SHCL_CONTEXTID_GET_EVENT(cmdCtx.uContextID));
1850 ASSERT_GUEST_MSG_RETURN(cmdCtx.uContextID == idCtxExpected,
1851 ("Wrong context ID: %#RX64, expected %#RX64\n", cmdCtx.uContextID, idCtxExpected),
1852 VERR_INVALID_CONTEXT);
1853
1854 /*
1855 * For some reason we need to do this (makes absolutely no sense to bird).
1856 */
1857 /** @todo r=bird: I really don't get why you need the State.POD.uFormat
1858 * member. I'm sure there is a reason. Incomplete code? */
1859 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1860 {
1861 if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
1862 pClient->State.POD.uFormat = uFormat;
1863 }
1864
1865#ifdef LOG_ENABLED
1866 char *pszFmt = ShClFormatsToStrA(uFormat);
1867 if (pszFmt)
1868 {
1869 LogRel2(("Shared Clipboard: Guest writes %RU32 bytes clipboard data in format '%s' to host\n", cbData, pszFmt));
1870 RTStrFree(pszFmt);
1871 }
1872#endif
1873
1874 /*
1875 * Write the data to the active host side clipboard.
1876 */
1877 int rc = RTCritSectEnter(&g_CritSect);
1878 AssertRCReturn(rc, rc);
1879
1880 if (g_ExtState.pfnExtension)
1881 {
1882 SHCLEXTPARMS parms;
1883 RT_ZERO(parms);
1884 parms.uFormat = uFormat;
1885 parms.u.pvData = pvData;
1886 parms.cbData = cbData;
1887
1888 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof(parms));
1889 rc = VINF_SUCCESS;
1890 }
1891 else
1892 {
1893 /* Let the backend implementation know. */
1894 rc = ShClBackendWriteData(pClient, &cmdCtx, uFormat, pvData, cbData);
1895 if (RT_FAILURE(rc))
1896 LogRel(("Shared Clipboard: Writing guest clipboard data to the host failed with %Rrc\n", rc));
1897
1898 int rc2; /* Don't return internals back to the guest. */
1899 rc2 = ShClSvcGuestDataSignal(pClient, &cmdCtx, uFormat, pvData, cbData); /* To complete pending events, if any. */
1900 if (RT_FAILURE(rc2))
1901 LogRel(("Shared Clipboard: Signalling host about guest clipboard data failed with %Rrc\n", rc2));
1902 AssertRC(rc2);
1903 }
1904
1905 RTCritSectLeave(&g_CritSect);
1906
1907 LogFlowFuncLeaveRC(rc);
1908 return rc;
1909}
1910
1911/**
1912 * Gets an error from HGCM service parameters.
1913 *
1914 * @returns VBox status code.
1915 * @param cParms Number of HGCM parameters supplied in \a paParms.
1916 * @param paParms Array of HGCM parameters.
1917 * @param pRc Where to store the received error code.
1918 */
1919static int shClSvcClientError(uint32_t cParms, VBOXHGCMSVCPARM paParms[], int *pRc)
1920{
1921 AssertPtrReturn(paParms, VERR_INVALID_PARAMETER);
1922 AssertPtrReturn(pRc, VERR_INVALID_PARAMETER);
1923
1924 int rc;
1925
1926 if (cParms == VBOX_SHCL_CPARMS_ERROR)
1927 {
1928 rc = HGCMSvcGetU32(&paParms[1], (uint32_t *)pRc); /** @todo int vs. uint32_t !!! */
1929 }
1930 else
1931 rc = VERR_INVALID_PARAMETER;
1932
1933 LogFlowFuncLeaveRC(rc);
1934 return rc;
1935}
1936
1937/**
1938 * Sets the transfer source type of a Shared Clipboard client.
1939 *
1940 * @returns VBox status code.
1941 * @param pClient Client to set transfer source type for.
1942 * @param enmSource Source type to set.
1943 */
1944int shClSvcSetSource(PSHCLCLIENT pClient, SHCLSOURCE enmSource)
1945{
1946 if (!pClient) /* If no client connected (anymore), bail out. */
1947 return VINF_SUCCESS;
1948
1949 int rc = VINF_SUCCESS;
1950
1951 if (ShClSvcLock())
1952 {
1953 pClient->State.enmSource = enmSource;
1954
1955 LogFlowFunc(("Source of client %RU32 is now %RU32\n", pClient->State.uClientID, pClient->State.enmSource));
1956
1957 ShClSvcUnlock();
1958 }
1959
1960 LogFlowFuncLeaveRC(rc);
1961 return rc;
1962}
1963
1964static int svcInit(VBOXHGCMSVCFNTABLE *pTable)
1965{
1966 int rc = RTCritSectInit(&g_CritSect);
1967
1968 if (RT_SUCCESS(rc))
1969 {
1970 shClSvcModeSet(VBOX_SHCL_MODE_OFF);
1971
1972 rc = ShClBackendInit(pTable);
1973
1974 /* Clean up on failure, because 'svnUnload' will not be called
1975 * if the 'svcInit' returns an error.
1976 */
1977 if (RT_FAILURE(rc))
1978 {
1979 RTCritSectDelete(&g_CritSect);
1980 }
1981 }
1982
1983 return rc;
1984}
1985
1986static DECLCALLBACK(int) svcUnload(void *)
1987{
1988 LogFlowFuncEnter();
1989
1990 ShClBackendDestroy();
1991
1992 RTCritSectDelete(&g_CritSect);
1993
1994 return VINF_SUCCESS;
1995}
1996
1997static DECLCALLBACK(int) svcDisconnect(void *, uint32_t u32ClientID, void *pvClient)
1998{
1999 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
2000
2001 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2002 AssertPtr(pClient);
2003
2004 /* In order to communicate with guest service, HGCM VRDP clipboard extension
2005 * needs to know its connection client ID. Currently, in svcConnect() we always
2006 * cache ID of the first ever connected client. When client disconnects,
2007 * we need to forget its ID and let svcConnect() to pick up the next ID when a new
2008 * connection will be requested by guest service (see #10115). */
2009 if (g_ExtState.uClientID == u32ClientID)
2010 {
2011 g_ExtState.uClientID = 0;
2012 }
2013
2014#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2015 shClSvcClientTransfersReset(pClient);
2016#endif
2017
2018 ShClBackendDisconnect(pClient);
2019
2020 shClSvcClientDestroy(pClient);
2021
2022 return VINF_SUCCESS;
2023}
2024
2025static DECLCALLBACK(int) svcConnect(void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
2026{
2027 RT_NOREF(fRequestor, fRestoring);
2028
2029 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2030 AssertPtr(pvClient);
2031
2032 int rc = shClSvcClientInit(pClient, u32ClientID);
2033 if (RT_SUCCESS(rc))
2034 {
2035 /* Assign weak pointer to client map. */
2036 /** @todo r=bird: The g_mapClients is only there for looking up
2037 * g_ExtState.uClientID (unserialized btw), so why not use store the
2038 * pClient value directly in g_ExtState instead of the ID? It cannot
2039 * crash any worse that racing map insertion/removal. */
2040 g_mapClients[u32ClientID] = pClient; /** @todo Handle OOM / collisions? */
2041
2042 rc = ShClBackendConnect(pClient, ShClSvcGetHeadless());
2043 if (RT_SUCCESS(rc))
2044 {
2045 /* Sync the host clipboard content with the client. */
2046 rc = ShClBackendSync(pClient);
2047 if (RT_SUCCESS(rc))
2048 {
2049 /* For now we ASSUME that the first client that connects is in charge for
2050 communicating with the service extension. */
2051 /** @todo This isn't optimal, but only the guest really knows which client is in
2052 * focus on the console. See @bugref{10115} for details. */
2053 if (g_ExtState.uClientID == 0)
2054 g_ExtState.uClientID = u32ClientID;
2055
2056 /* The sync could return VINF_NO_CHANGE if nothing has changed on the host, but
2057 older Guest Additions didn't use RT_SUCCESS to but == VINF_SUCCESS to check for
2058 success. So just return VINF_SUCCESS here to not break older Guest Additions. */
2059 LogFunc(("Successfully connected client %#x%s\n",
2060 u32ClientID, g_ExtState.uClientID == u32ClientID ? " - Use by ExtState too" : ""));
2061 return VINF_SUCCESS;
2062 }
2063
2064 LogFunc(("ShClBackendSync failed: %Rrc\n", rc));
2065 ShClBackendDisconnect(pClient);
2066 }
2067 else
2068 LogFunc(("ShClBackendConnect failed: %Rrc\n", rc));
2069 shClSvcClientDestroy(pClient);
2070 }
2071 else
2072 LogFunc(("shClSvcClientInit failed: %Rrc\n", rc));
2073 LogFlowFuncLeaveRC(rc);
2074 return rc;
2075}
2076
2077static DECLCALLBACK(void) svcCall(void *,
2078 VBOXHGCMCALLHANDLE callHandle,
2079 uint32_t u32ClientID,
2080 void *pvClient,
2081 uint32_t u32Function,
2082 uint32_t cParms,
2083 VBOXHGCMSVCPARM paParms[],
2084 uint64_t tsArrival)
2085{
2086 RT_NOREF(u32ClientID, pvClient, tsArrival);
2087 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2088 AssertPtr(pClient);
2089
2090#ifdef LOG_ENABLED
2091 Log2Func(("u32ClientID=%RU32, fn=%RU32 (%s), cParms=%RU32, paParms=%p\n",
2092 u32ClientID, u32Function, ShClGuestMsgToStr(u32Function), cParms, paParms));
2093 for (uint32_t i = 0; i < cParms; i++)
2094 {
2095 switch (paParms[i].type)
2096 {
2097 case VBOX_HGCM_SVC_PARM_32BIT:
2098 Log3Func((" paParms[%RU32]: type uint32_t - value %RU32\n", i, paParms[i].u.uint32));
2099 break;
2100 case VBOX_HGCM_SVC_PARM_64BIT:
2101 Log3Func((" paParms[%RU32]: type uint64_t - value %RU64\n", i, paParms[i].u.uint64));
2102 break;
2103 case VBOX_HGCM_SVC_PARM_PTR:
2104 Log3Func((" paParms[%RU32]: type ptr - value 0x%p (%RU32 bytes)\n",
2105 i, paParms[i].u.pointer.addr, paParms[i].u.pointer.size));
2106 break;
2107 case VBOX_HGCM_SVC_PARM_PAGES:
2108 Log3Func((" paParms[%RU32]: type pages - cb=%RU32, cPages=%RU16\n",
2109 i, paParms[i].u.Pages.cb, paParms[i].u.Pages.cPages));
2110 break;
2111 default:
2112 AssertFailed();
2113 }
2114 }
2115 Log2Func(("Client state: fFlags=0x%x, fGuestFeatures0=0x%x, fGuestFeatures1=0x%x\n",
2116 pClient->State.fFlags, pClient->State.fGuestFeatures0, pClient->State.fGuestFeatures1));
2117#endif
2118
2119 int rc;
2120 switch (u32Function)
2121 {
2122 case VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT:
2123 RTCritSectEnter(&pClient->CritSect);
2124 rc = shClSvcClientMsgOldGet(pClient, callHandle, cParms, paParms);
2125 RTCritSectLeave(&pClient->CritSect);
2126 break;
2127
2128 case VBOX_SHCL_GUEST_FN_CONNECT:
2129 LogRel(("Shared Clipboard: 6.1.0 beta or rc Guest Additions detected. Please upgrade!\n"));
2130 rc = VERR_NOT_IMPLEMENTED;
2131 break;
2132
2133 case VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE:
2134 rc = shClSvcClientNegogiateChunkSize(pClient, callHandle, cParms, paParms);
2135 break;
2136
2137 case VBOX_SHCL_GUEST_FN_REPORT_FEATURES:
2138 rc = shClSvcClientReportFeatures(pClient, callHandle, cParms, paParms);
2139 break;
2140
2141 case VBOX_SHCL_GUEST_FN_QUERY_FEATURES:
2142 rc = shClSvcClientQueryFeatures(callHandle, cParms, paParms);
2143 break;
2144
2145 case VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT:
2146 RTCritSectEnter(&pClient->CritSect);
2147 rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, false /*fWait*/);
2148 RTCritSectLeave(&pClient->CritSect);
2149 break;
2150
2151 case VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT:
2152 RTCritSectEnter(&pClient->CritSect);
2153 rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, true /*fWait*/);
2154 RTCritSectLeave(&pClient->CritSect);
2155 break;
2156
2157 case VBOX_SHCL_GUEST_FN_MSG_GET:
2158 RTCritSectEnter(&pClient->CritSect);
2159 rc = shClSvcClientMsgGet(pClient, callHandle, cParms, paParms);
2160 RTCritSectLeave(&pClient->CritSect);
2161 break;
2162
2163 case VBOX_SHCL_GUEST_FN_MSG_CANCEL:
2164 RTCritSectEnter(&pClient->CritSect);
2165 rc = shClSvcClientMsgCancel(pClient, cParms);
2166 RTCritSectLeave(&pClient->CritSect);
2167 break;
2168
2169 case VBOX_SHCL_GUEST_FN_REPORT_FORMATS:
2170 rc = shClSvcClientReportFormats(pClient, cParms, paParms);
2171 break;
2172
2173 case VBOX_SHCL_GUEST_FN_DATA_READ:
2174 rc = shClSvcClientReadData(pClient, cParms, paParms);
2175 break;
2176
2177 case VBOX_SHCL_GUEST_FN_DATA_WRITE:
2178 rc = shClSvcClientWriteData(pClient, cParms, paParms);
2179 break;
2180
2181 case VBOX_SHCL_GUEST_FN_ERROR:
2182 {
2183 int rcGuest;
2184 rc = shClSvcClientError(cParms,paParms, &rcGuest);
2185 if (RT_SUCCESS(rc))
2186 {
2187 LogRel(("Shared Clipboard: Error reported from guest side: %Rrc\n", rcGuest));
2188
2189 shClSvcClientLock(pClient);
2190
2191#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2192 shClSvcClientTransfersReset(pClient);
2193#endif
2194 shClSvcClientUnlock(pClient);
2195 }
2196 break;
2197 }
2198
2199 default:
2200 {
2201#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2202 if ( u32Function <= VBOX_SHCL_GUEST_FN_LAST
2203 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID) )
2204 {
2205 if (g_fTransferMode & VBOX_SHCL_TRANSFER_MODE_ENABLED)
2206 rc = shClSvcTransferHandler(pClient, callHandle, u32Function, cParms, paParms, tsArrival);
2207 else
2208 {
2209 LogRel2(("Shared Clipboard: File transfers are disabled for this VM\n"));
2210 rc = VERR_ACCESS_DENIED;
2211 }
2212 }
2213 else
2214#endif
2215 {
2216 LogRel2(("Shared Clipboard: Unknown guest function: %u (%#x)\n", u32Function, u32Function));
2217 rc = VERR_NOT_IMPLEMENTED;
2218 }
2219 break;
2220 }
2221 }
2222
2223 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
2224
2225 if (rc != VINF_HGCM_ASYNC_EXECUTE)
2226 g_pHelpers->pfnCallComplete(callHandle, rc);
2227}
2228
2229/**
2230 * Initializes a Shared Clipboard service's client state.
2231 *
2232 * @returns VBox status code.
2233 * @param pClientState Client state to initialize.
2234 * @param uClientID Client ID (HGCM) to use for this client state.
2235 */
2236int shClSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID)
2237{
2238 LogFlowFuncEnter();
2239
2240 shclSvcClientStateReset(pClientState);
2241
2242 /* Register the client. */
2243 pClientState->uClientID = uClientID;
2244
2245 return VINF_SUCCESS;
2246}
2247
2248/**
2249 * Destroys a Shared Clipboard service's client state.
2250 *
2251 * @returns VBox status code.
2252 * @param pClientState Client state to destroy.
2253 */
2254int shClSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState)
2255{
2256 RT_NOREF(pClientState);
2257
2258 LogFlowFuncEnter();
2259
2260 return VINF_SUCCESS;
2261}
2262
2263/**
2264 * Resets a Shared Clipboard service's client state.
2265 *
2266 * @param pClientState Client state to reset.
2267 */
2268void shclSvcClientStateReset(PSHCLCLIENTSTATE pClientState)
2269{
2270 LogFlowFuncEnter();
2271
2272 pClientState->fGuestFeatures0 = VBOX_SHCL_GF_NONE;
2273 pClientState->fGuestFeatures1 = VBOX_SHCL_GF_NONE;
2274
2275 pClientState->cbChunkSize = VBOX_SHCL_DEFAULT_CHUNK_SIZE; /** @todo Make this configurable. */
2276 pClientState->enmSource = SHCLSOURCE_INVALID;
2277 pClientState->fFlags = SHCLCLIENTSTATE_FLAGS_NONE;
2278
2279 pClientState->POD.enmDir = SHCLTRANSFERDIR_UNKNOWN;
2280 pClientState->POD.uFormat = VBOX_SHCL_FMT_NONE;
2281 pClientState->POD.cbToReadWriteTotal = 0;
2282 pClientState->POD.cbReadWritten = 0;
2283
2284#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2285 pClientState->Transfers.enmTransferDir = SHCLTRANSFERDIR_UNKNOWN;
2286#endif
2287}
2288
2289/*
2290 * We differentiate between a function handler for the guest and one for the host.
2291 */
2292static DECLCALLBACK(int) svcHostCall(void *,
2293 uint32_t u32Function,
2294 uint32_t cParms,
2295 VBOXHGCMSVCPARM paParms[])
2296{
2297 int rc = VINF_SUCCESS;
2298
2299 LogFlowFunc(("u32Function=%RU32 (%s), cParms=%RU32, paParms=%p\n",
2300 u32Function, ShClHostFunctionToStr(u32Function), cParms, paParms));
2301
2302 switch (u32Function)
2303 {
2304 case VBOX_SHCL_HOST_FN_SET_MODE:
2305 {
2306 if (cParms != 1)
2307 {
2308 rc = VERR_INVALID_PARAMETER;
2309 }
2310 else
2311 {
2312 uint32_t u32Mode = VBOX_SHCL_MODE_OFF;
2313
2314 rc = HGCMSvcGetU32(&paParms[0], &u32Mode);
2315 if (RT_SUCCESS(rc))
2316 rc = shClSvcModeSet(u32Mode);
2317 }
2318
2319 break;
2320 }
2321
2322#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2323 case VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE:
2324 {
2325 if (cParms != 1)
2326 {
2327 rc = VERR_INVALID_PARAMETER;
2328 }
2329 else
2330 {
2331 uint32_t fTransferMode;
2332 rc = HGCMSvcGetU32(&paParms[0], &fTransferMode);
2333 if (RT_SUCCESS(rc))
2334 rc = shClSvcTransferModeSet(fTransferMode);
2335 }
2336 break;
2337 }
2338#endif
2339 case VBOX_SHCL_HOST_FN_SET_HEADLESS:
2340 {
2341 if (cParms != 1)
2342 {
2343 rc = VERR_INVALID_PARAMETER;
2344 }
2345 else
2346 {
2347 uint32_t uHeadless;
2348 rc = HGCMSvcGetU32(&paParms[0], &uHeadless);
2349 if (RT_SUCCESS(rc))
2350 {
2351 g_fHeadless = RT_BOOL(uHeadless);
2352 LogRel(("Shared Clipboard: Service running in %s mode\n", g_fHeadless ? "headless" : "normal"));
2353 }
2354 }
2355 break;
2356 }
2357
2358 default:
2359 {
2360#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2361 rc = shClSvcTransferHostHandler(u32Function, cParms, paParms);
2362#else
2363 rc = VERR_NOT_IMPLEMENTED;
2364#endif
2365 break;
2366 }
2367 }
2368
2369 LogFlowFuncLeaveRC(rc);
2370 return rc;
2371}
2372
2373#ifndef UNIT_TEST
2374
2375/**
2376 * SSM descriptor table for the SHCLCLIENTLEGACYCID structure.
2377 *
2378 * @note Saving the ListEntry attribute is not necessary, as this gets used on runtime only.
2379 */
2380static SSMFIELD const s_aShClSSMClientLegacyCID[] =
2381{
2382 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uCID),
2383 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, enmType),
2384 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uFormat),
2385 SSMFIELD_ENTRY_TERM()
2386};
2387
2388/**
2389 * SSM descriptor table for the SHCLCLIENTSTATE structure.
2390 *
2391 * @note Saving the session ID not necessary, as they're not persistent across
2392 * state save/restore.
2393 */
2394static SSMFIELD const s_aShClSSMClientState[] =
2395{
2396 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
2397 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
2398 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
2399 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
2400 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fFlags),
2401 SSMFIELD_ENTRY_TERM()
2402};
2403
2404/**
2405 * VBox 6.1 Beta 1 version of s_aShClSSMClientState (no flags).
2406 */
2407static SSMFIELD const s_aShClSSMClientState61B1[] =
2408{
2409 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
2410 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
2411 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
2412 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
2413 SSMFIELD_ENTRY_TERM()
2414};
2415
2416/**
2417 * SSM descriptor table for the SHCLCLIENTPODSTATE structure.
2418 */
2419static SSMFIELD const s_aShClSSMClientPODState[] =
2420{
2421 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, enmDir),
2422 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, uFormat),
2423 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbToReadWriteTotal),
2424 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbReadWritten),
2425 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, tsLastReadWrittenMs),
2426 SSMFIELD_ENTRY_TERM()
2427};
2428
2429/**
2430 * SSM descriptor table for the SHCLCLIENTURISTATE structure.
2431 */
2432static SSMFIELD const s_aShClSSMClientTransferState[] =
2433{
2434 SSMFIELD_ENTRY(SHCLCLIENTTRANSFERSTATE, enmTransferDir),
2435 SSMFIELD_ENTRY_TERM()
2436};
2437
2438/**
2439 * SSM descriptor table for the header of the SHCLCLIENTMSG structure.
2440 * The actual message parameters will be serialized separately.
2441 */
2442static SSMFIELD const s_aShClSSMClientMsgHdr[] =
2443{
2444 SSMFIELD_ENTRY(SHCLCLIENTMSG, idMsg),
2445 SSMFIELD_ENTRY(SHCLCLIENTMSG, cParms),
2446 SSMFIELD_ENTRY_TERM()
2447};
2448
2449/**
2450 * SSM descriptor table for what used to be the VBOXSHCLMSGCTX structure but is
2451 * now part of SHCLCLIENTMSG.
2452 */
2453static SSMFIELD const s_aShClSSMClientMsgCtx[] =
2454{
2455 SSMFIELD_ENTRY(SHCLCLIENTMSG, idCtx),
2456 SSMFIELD_ENTRY_TERM()
2457};
2458#endif /* !UNIT_TEST */
2459
2460static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM)
2461{
2462 LogFlowFuncEnter();
2463
2464#ifndef UNIT_TEST
2465 /*
2466 * When the state will be restored, pending requests will be reissued
2467 * by VMMDev. The service therefore must save state as if there were no
2468 * pending request.
2469 * Pending requests, if any, will be completed in svcDisconnect.
2470 */
2471 RT_NOREF(u32ClientID);
2472 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
2473
2474 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2475 AssertPtr(pClient);
2476
2477 /* Write Shared Clipboard saved state version. */
2478 pVMM->pfnSSMR3PutU32(pSSM, VBOX_SHCL_SAVED_STATE_VER_CURRENT);
2479
2480 int rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
2481 AssertRCReturn(rc, rc);
2482
2483 rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /*fFlags*/, &s_aShClSSMClientPODState[0], NULL);
2484 AssertRCReturn(rc, rc);
2485
2486 rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientTransferState[0], NULL);
2487 AssertRCReturn(rc, rc);
2488
2489 /* Serialize the client's internal message queue. */
2490 rc = pVMM->pfnSSMR3PutU64(pSSM, pClient->cMsgAllocated);
2491 AssertRCReturn(rc, rc);
2492
2493 PSHCLCLIENTMSG pMsg;
2494 RTListForEach(&pClient->MsgQueue, pMsg, SHCLCLIENTMSG, ListEntry)
2495 {
2496 pVMM->pfnSSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
2497 pVMM->pfnSSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
2498
2499 for (uint32_t iParm = 0; iParm < pMsg->cParms; iParm++)
2500 HGCMSvcSSMR3Put(&pMsg->aParms[iParm], pSSM, pVMM);
2501 }
2502
2503 rc = pVMM->pfnSSMR3PutU64(pSSM, pClient->Legacy.cCID);
2504 AssertRCReturn(rc, rc);
2505
2506 PSHCLCLIENTLEGACYCID pCID;
2507 RTListForEach(&pClient->Legacy.lstCID, pCID, SHCLCLIENTLEGACYCID, Node)
2508 {
2509 rc = pVMM->pfnSSMR3PutStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /*fFlags*/, &s_aShClSSMClientLegacyCID[0], NULL);
2510 AssertRCReturn(rc, rc);
2511 }
2512#else /* UNIT_TEST */
2513 RT_NOREF(u32ClientID, pvClient, pSSM, pVMM);
2514#endif /* UNIT_TEST */
2515 return VINF_SUCCESS;
2516}
2517
2518#ifndef UNIT_TEST
2519static int svcLoadStateV0(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM, uint32_t uVersion)
2520{
2521 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
2522
2523 uint32_t uMarker;
2524 int rc = pVMM->pfnSSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
2525 AssertRC(rc);
2526 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
2527
2528 rc = pVMM->pfnSSMR3Skip(pSSM, sizeof(uint32_t)); /* Client ID */
2529 AssertRCReturn(rc, rc);
2530
2531 bool fValue;
2532 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgQuit */
2533 AssertRCReturn(rc, rc);
2534
2535 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgReadData */
2536 AssertRCReturn(rc, rc);
2537
2538 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgFormats */
2539 AssertRCReturn(rc, rc);
2540
2541 uint32_t fFormats;
2542 rc = pVMM->pfnSSMR3GetU32(pSSM, &fFormats); /* u32RequestedFormat */
2543 AssertRCReturn(rc, rc);
2544
2545 rc = pVMM->pfnSSMR3GetU32(pSSM, &uMarker); /* End marker. */
2546 AssertRCReturn(rc, rc);
2547 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
2548
2549 return VINF_SUCCESS;
2550}
2551#endif /* UNIT_TEST */
2552
2553static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient,
2554 PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM, uint32_t uVersion)
2555{
2556 LogFlowFuncEnter();
2557
2558#ifndef UNIT_TEST
2559
2560 RT_NOREF(u32ClientID, uVersion);
2561
2562 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2563 AssertPtr(pClient);
2564
2565 /* Restore the client data. */
2566 uint32_t lenOrVer;
2567 int rc = pVMM->pfnSSMR3GetU32(pSSM, &lenOrVer);
2568 AssertRCReturn(rc, rc);
2569
2570 LogFunc(("u32ClientID=%RU32, lenOrVer=%#RX64\n", u32ClientID, lenOrVer));
2571
2572 if (lenOrVer == VBOX_SHCL_SAVED_STATE_VER_3_1)
2573 return svcLoadStateV0(u32ClientID, pvClient, pSSM, pVMM, uVersion);
2574
2575 if ( lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1B2
2576 && lenOrVer <= VBOX_SHCL_SAVED_STATE_VER_CURRENT)
2577 {
2578 if (lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1RC1)
2579 {
2580 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */,
2581 &s_aShClSSMClientState[0], NULL);
2582 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /* fFlags */,
2583 &s_aShClSSMClientPODState[0], NULL);
2584 }
2585 else
2586 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */,
2587 &s_aShClSSMClientState61B1[0], NULL);
2588 rc = pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /* fFlags */,
2589 &s_aShClSSMClientTransferState[0], NULL);
2590 AssertRCReturn(rc, rc);
2591
2592 /* Load the client's internal message queue. */
2593 uint64_t cMsgs;
2594 rc = pVMM->pfnSSMR3GetU64(pSSM, &cMsgs);
2595 AssertRCReturn(rc, rc);
2596 AssertLogRelMsgReturn(cMsgs < _16K, ("Too many messages: %u (%x)\n", cMsgs, cMsgs), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2597
2598 for (uint64_t i = 0; i < cMsgs; i++)
2599 {
2600 union
2601 {
2602 SHCLCLIENTMSG Msg;
2603 uint8_t abPadding[RT_UOFFSETOF(SHCLCLIENTMSG, aParms) + sizeof(VBOXHGCMSVCPARM) * 2];
2604 } u;
2605
2606 pVMM->pfnSSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/,
2607 &s_aShClSSMClientMsgHdr[0], NULL);
2608 rc = pVMM->pfnSSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/,
2609 &s_aShClSSMClientMsgCtx[0], NULL);
2610 AssertRCReturn(rc, rc);
2611
2612 AssertLogRelMsgReturn(u.Msg.cParms <= VMMDEV_MAX_HGCM_PARMS,
2613 ("Too many HGCM message parameters: %u (%#x)\n", u.Msg.cParms, u.Msg.cParms),
2614 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2615
2616 PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient, u.Msg.idMsg, u.Msg.cParms);
2617 AssertReturn(pMsg, VERR_NO_MEMORY);
2618 pMsg->idCtx = u.Msg.idCtx;
2619
2620 for (uint32_t p = 0; p < pMsg->cParms; p++)
2621 {
2622 rc = HGCMSvcSSMR3Get(&pMsg->aParms[p], pSSM, pVMM);
2623 AssertRCReturnStmt(rc, shClSvcMsgFree(pClient, pMsg), rc);
2624 }
2625
2626 RTCritSectEnter(&pClient->CritSect);
2627 shClSvcMsgAdd(pClient, pMsg, true /* fAppend */);
2628 RTCritSectLeave(&pClient->CritSect);
2629 }
2630
2631 if (lenOrVer >= VBOX_SHCL_SAVED_STATE_LEGACY_CID)
2632 {
2633 uint64_t cCID;
2634 rc = pVMM->pfnSSMR3GetU64(pSSM, &cCID);
2635 AssertRCReturn(rc, rc);
2636 AssertLogRelMsgReturn(cCID < _16K, ("Too many context IDs: %u (%x)\n", cCID, cCID), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2637
2638 for (uint64_t i = 0; i < cCID; i++)
2639 {
2640 PSHCLCLIENTLEGACYCID pCID = (PSHCLCLIENTLEGACYCID)RTMemAlloc(sizeof(SHCLCLIENTLEGACYCID));
2641 AssertPtrReturn(pCID, VERR_NO_MEMORY);
2642
2643 pVMM->pfnSSMR3GetStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /* fFlags */,
2644 &s_aShClSSMClientLegacyCID[0], NULL);
2645 RTListAppend(&pClient->Legacy.lstCID, &pCID->Node);
2646 }
2647 }
2648 }
2649 else
2650 {
2651 LogRel(("Shared Clipboard: Unsupported saved state version (%#x)\n", lenOrVer));
2652 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
2653 }
2654
2655 /* Actual host data are to be reported to guest (SYNC). */
2656 ShClBackendSync(pClient);
2657
2658#else /* UNIT_TEST */
2659 RT_NOREF(u32ClientID, pvClient, pSSM, pVMM, uVersion);
2660#endif /* UNIT_TEST */
2661 return VINF_SUCCESS;
2662}
2663
2664static DECLCALLBACK(int) extCallback(uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
2665{
2666 RT_NOREF(pvData, cbData);
2667
2668 LogFlowFunc(("u32Function=%RU32\n", u32Function));
2669
2670 int rc = VINF_SUCCESS;
2671
2672 /* Figure out if the client in charge for the service extension still is connected. */
2673 ClipboardClientMap::const_iterator itClient = g_mapClients.find(g_ExtState.uClientID);
2674 if (itClient != g_mapClients.end())
2675 {
2676 PSHCLCLIENT pClient = itClient->second;
2677 AssertPtr(pClient);
2678
2679 switch (u32Function)
2680 {
2681 /* The service extension announces formats to the guest. */
2682 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
2683 {
2684 LogFlowFunc(("VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE: g_ExtState.fReadingData=%RTbool\n", g_ExtState.fReadingData));
2685 if (!g_ExtState.fReadingData)
2686 rc = ShClSvcHostReportFormats(pClient, u32Format);
2687 else
2688 {
2689 g_ExtState.fDelayedAnnouncement = true;
2690 g_ExtState.fDelayedFormats = u32Format;
2691 rc = VINF_SUCCESS;
2692 }
2693 break;
2694 }
2695
2696 /* The service extension wants read data from the guest. */
2697 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
2698 rc = ShClSvcGuestDataRequest(pClient, u32Format, NULL /* pidEvent */);
2699 break;
2700
2701 default:
2702 /* Just skip other messages. */
2703 break;
2704 }
2705 }
2706 else
2707 rc = VERR_NOT_FOUND;
2708
2709 LogFlowFuncLeaveRC(rc);
2710 return rc;
2711}
2712
2713static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
2714{
2715 LogFlowFunc(("pfnExtension=%p\n", pfnExtension));
2716
2717 SHCLEXTPARMS parms;
2718 RT_ZERO(parms);
2719
2720 /*
2721 * Reference counting for service extension registration is done a few
2722 * layers up (in ConsoleVRDPServer::ClipboardCreate()).
2723 */
2724
2725 int rc = RTCritSectEnter(&g_CritSect);
2726 AssertLogRelRCReturn(rc, rc);
2727
2728 if (pfnExtension)
2729 {
2730 /* Install extension. */
2731 g_ExtState.pfnExtension = pfnExtension;
2732 g_ExtState.pvExtension = pvExtension;
2733
2734 parms.u.pfnCallback = extCallback;
2735 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
2736
2737 LogRel2(("Shared Clipboard: registered service extension\n"));
2738 }
2739 else
2740 {
2741 if (g_ExtState.pfnExtension)
2742 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
2743
2744 /* Uninstall extension. */
2745 g_ExtState.pvExtension = NULL;
2746 g_ExtState.pfnExtension = NULL;
2747
2748 LogRel2(("Shared Clipboard: de-registered service extension\n"));
2749 }
2750
2751 RTCritSectLeave(&g_CritSect);
2752
2753 return VINF_SUCCESS;
2754}
2755
2756extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
2757{
2758 int rc = VINF_SUCCESS;
2759
2760 LogFlowFunc(("pTable=%p\n", pTable));
2761
2762 if (!RT_VALID_PTR(pTable))
2763 {
2764 rc = VERR_INVALID_PARAMETER;
2765 }
2766 else
2767 {
2768 LogFunc(("pTable->cbSize = %d, ptable->u32Version = 0x%08X\n", pTable->cbSize, pTable->u32Version));
2769
2770 if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
2771 || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
2772 {
2773 rc = VERR_VERSION_MISMATCH;
2774 }
2775 else
2776 {
2777 g_pHelpers = pTable->pHelpers;
2778
2779 pTable->cbClient = sizeof(SHCLCLIENT);
2780
2781 /* Map legacy clients to root. */
2782 pTable->idxLegacyClientCategory = HGCM_CLIENT_CATEGORY_ROOT;
2783
2784 /* Limit the number of clients to 128 in each category (should be enough),
2785 but set kernel clients to 1. */
2786 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
2787 pTable->acMaxClients[i] = 128;
2788 pTable->acMaxClients[HGCM_CLIENT_CATEGORY_KERNEL] = 1;
2789
2790 /* Only 16 pending calls per client (1 should be enough). */
2791 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
2792 pTable->acMaxCallsPerClient[i] = 16;
2793
2794 pTable->pfnUnload = svcUnload;
2795 pTable->pfnConnect = svcConnect;
2796 pTable->pfnDisconnect = svcDisconnect;
2797 pTable->pfnCall = svcCall;
2798 pTable->pfnHostCall = svcHostCall;
2799 pTable->pfnSaveState = svcSaveState;
2800 pTable->pfnLoadState = svcLoadState;
2801 pTable->pfnRegisterExtension = svcRegisterExtension;
2802 pTable->pfnNotify = NULL;
2803 pTable->pvService = NULL;
2804
2805 /* Service specific initialization. */
2806 rc = svcInit(pTable);
2807 }
2808 }
2809
2810 LogFlowFunc(("Returning %Rrc\n", rc));
2811 return rc;
2812}
Note: See TracBrowser for help on using the repository browser.

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