VirtualBox

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

Last change on this file since 100524 was 100413, checked in by vboxsync, 18 months ago

Shared Clipboard: Removed dead code. ​​​bugref:9437

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