VirtualBox

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

Last change on this file since 99974 was 99974, checked in by vboxsync, 2 years ago

Shared Clipboard/HostService: Fixes for (not) reporting (and starting) transfers with older Guest Additions. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 101.9 KB
Line 
1/* $Id: VBoxSharedClipboardSvc.cpp 99974 2023-05-25 11:10:14Z 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);
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 RTCritSectEnter(&pClient->CritSect);
617 if (pClient->Pending.uType)
618 {
619 if (pClient->Pending.cParms > 1)
620 HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_QUIT);
621 if (pClient->Pending.cParms > 2)
622 HGCMSvcSetU32(&pClient->Pending.paParms[1], 0);
623 g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
624 pClient->Pending.uType = 0;
625 pClient->Pending.cParms = 0;
626 pClient->Pending.hHandle = NULL;
627 pClient->Pending.paParms = NULL;
628 }
629 RTCritSectLeave(&pClient->CritSect);
630
631 ShClEventSourceDestroy(&pClient->EventSrc);
632
633 shClSvcClientStateDestroy(&pClient->State);
634
635 PSHCLCLIENTLEGACYCID pCidIter, pCidIterNext;
636 RTListForEachSafe(&pClient->Legacy.lstCID, pCidIter, pCidIterNext, SHCLCLIENTLEGACYCID, Node)
637 {
638 RTMemFree(pCidIter);
639 }
640
641 int rc2 = RTCritSectDelete(&pClient->CritSect);
642 AssertRC(rc2);
643
644 ClipboardClientMap::iterator itClient = g_mapClients.find(pClient->State.uClientID);
645 if (itClient != g_mapClients.end())
646 g_mapClients.erase(itClient);
647 else
648 AssertFailed();
649
650 LogFlowFuncLeave();
651}
652
653void shClSvcClientLock(PSHCLCLIENT pClient)
654{
655 int rc2 = RTCritSectEnter(&pClient->CritSect);
656 AssertRC(rc2);
657}
658
659void shClSvcClientUnlock(PSHCLCLIENT pClient)
660{
661 int rc2 = RTCritSectLeave(&pClient->CritSect);
662 AssertRC(rc2);
663}
664
665/**
666 * Resets a Shared Clipboard client.
667 *
668 * @param pClient Client to reset.
669 */
670void shClSvcClientReset(PSHCLCLIENT pClient)
671{
672 if (!pClient)
673 return;
674
675 LogFlowFunc(("[Client %RU32]\n", pClient->State.uClientID));
676 RTCritSectEnter(&pClient->CritSect);
677
678 /* Reset message queue. */
679 shClSvcMsgQueueReset(pClient);
680
681 /* Reset event source. */
682 ShClEventSourceReset(&pClient->EventSrc);
683
684 /* Reset pending state. */
685 RT_ZERO(pClient->Pending);
686
687#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
688 shClSvcClientTransfersReset(pClient);
689#endif
690
691 shclSvcClientStateReset(&pClient->State);
692
693 RTCritSectLeave(&pClient->CritSect);
694}
695
696static int shClSvcClientNegogiateChunkSize(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall,
697 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
698{
699 /*
700 * Validate the request.
701 */
702 ASSERT_GUEST_RETURN(cParms == VBOX_SHCL_CPARMS_NEGOTIATE_CHUNK_SIZE, VERR_WRONG_PARAMETER_COUNT);
703 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
704 uint32_t const cbClientMaxChunkSize = paParms[0].u.uint32;
705 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
706 uint32_t const cbClientChunkSize = paParms[1].u.uint32;
707
708 uint32_t const cbHostMaxChunkSize = VBOX_SHCL_MAX_CHUNK_SIZE; /** @todo Make this configurable. */
709
710 /*
711 * Do the work.
712 */
713 if (cbClientChunkSize == 0) /* Does the client want us to choose? */
714 {
715 paParms[0].u.uint32 = cbHostMaxChunkSize; /* Maximum */
716 paParms[1].u.uint32 = RT_MIN(pClient->State.cbChunkSize, cbHostMaxChunkSize); /* Preferred */
717
718 }
719 else /* The client told us what it supports, so update and report back. */
720 {
721 paParms[0].u.uint32 = RT_MIN(cbClientMaxChunkSize, cbHostMaxChunkSize); /* Maximum */
722 paParms[1].u.uint32 = RT_MIN(cbClientMaxChunkSize, pClient->State.cbChunkSize); /* Preferred */
723 }
724
725 int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
726 if (RT_SUCCESS(rc))
727 {
728 Log(("[Client %RU32] chunk size: %#RU32, max: %#RU32\n",
729 pClient->State.uClientID, paParms[1].u.uint32, paParms[0].u.uint32));
730 }
731 else
732 LogFunc(("pfnCallComplete -> %Rrc\n", rc));
733
734 return VINF_HGCM_ASYNC_EXECUTE;
735}
736
737/**
738 * Implements VBOX_SHCL_GUEST_FN_REPORT_FEATURES.
739 *
740 * @returns VBox status code.
741 * @retval VINF_HGCM_ASYNC_EXECUTE on success (we complete the message here).
742 * @retval VERR_ACCESS_DENIED if not master
743 * @retval VERR_INVALID_PARAMETER if bit 63 in the 2nd parameter isn't set.
744 * @retval VERR_WRONG_PARAMETER_COUNT
745 *
746 * @param pClient The client state.
747 * @param hCall The client's call handle.
748 * @param cParms Number of parameters.
749 * @param paParms Array of parameters.
750 */
751static int shClSvcClientReportFeatures(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall,
752 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
753{
754 /*
755 * Validate the request.
756 */
757 ASSERT_GUEST_RETURN(cParms == 2, VERR_WRONG_PARAMETER_COUNT);
758 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
759 uint64_t const fFeatures0 = paParms[0].u.uint64;
760 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
761 uint64_t const fFeatures1 = paParms[1].u.uint64;
762 ASSERT_GUEST_RETURN(fFeatures1 & VBOX_SHCL_GF_1_MUST_BE_ONE, VERR_INVALID_PARAMETER);
763
764 /*
765 * Do the work.
766 */
767 paParms[0].u.uint64 = g_fHostFeatures0;
768 paParms[1].u.uint64 = 0;
769
770 int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
771 if (RT_SUCCESS(rc))
772 {
773 pClient->State.fGuestFeatures0 = fFeatures0;
774 pClient->State.fGuestFeatures1 = fFeatures1;
775 LogRel2(("Shared Clipboard: Guest reported the following features: %#RX64\n",
776 pClient->State.fGuestFeatures0)); /* Note: fFeatures1 not used yet. */
777 if (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_TRANSFERS)
778 LogRel2(("Shared Clipboard: Guest supports file transfers\n"));
779 }
780 else
781 LogFunc(("pfnCallComplete -> %Rrc\n", rc));
782
783 return VINF_HGCM_ASYNC_EXECUTE;
784}
785
786/**
787 * Implements VBOX_SHCL_GUEST_FN_QUERY_FEATURES.
788 *
789 * @returns VBox status code.
790 * @retval VINF_HGCM_ASYNC_EXECUTE on success (we complete the message here).
791 * @retval VERR_WRONG_PARAMETER_COUNT
792 *
793 * @param hCall The client's call handle.
794 * @param cParms Number of parameters.
795 * @param paParms Array of parameters.
796 */
797static int shClSvcClientQueryFeatures(VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
798{
799 /*
800 * Validate the request.
801 */
802 ASSERT_GUEST_RETURN(cParms == 2, VERR_WRONG_PARAMETER_COUNT);
803 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
804 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
805 ASSERT_GUEST(paParms[1].u.uint64 & RT_BIT_64(63));
806
807 /*
808 * Do the work.
809 */
810 paParms[0].u.uint64 = g_fHostFeatures0;
811 paParms[1].u.uint64 = 0;
812 int rc = g_pHelpers->pfnCallComplete(hCall, VINF_SUCCESS);
813 if (RT_FAILURE(rc))
814 LogFunc(("pfnCallComplete -> %Rrc\n", rc));
815
816 return VINF_HGCM_ASYNC_EXECUTE;
817}
818
819/**
820 * Implements VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT.
821 *
822 * @returns VBox status code.
823 * @retval VINF_SUCCESS if a message was pending and is being returned.
824 * @retval VERR_TRY_AGAIN if no message pending and not blocking.
825 * @retval VERR_RESOURCE_BUSY if another read already made a waiting call.
826 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
827 *
828 * @param pClient The client state.
829 * @param hCall The client's call handle.
830 * @param cParms Number of parameters.
831 * @param paParms Array of parameters.
832 * @param fWait Set if we should wait for a message, clear if to return
833 * immediately.
834 *
835 * @note Caller takes and leave the client's critical section.
836 */
837static int shClSvcClientMsgPeek(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool fWait)
838{
839 /*
840 * Validate the request.
841 */
842 ASSERT_GUEST_MSG_RETURN(cParms >= 2, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
843
844 uint64_t idRestoreCheck = 0;
845 uint32_t i = 0;
846 if (paParms[i].type == VBOX_HGCM_SVC_PARM_64BIT)
847 {
848 idRestoreCheck = paParms[0].u.uint64;
849 paParms[0].u.uint64 = 0;
850 i++;
851 }
852 for (; i < cParms; i++)
853 {
854 ASSERT_GUEST_MSG_RETURN(paParms[i].type == VBOX_HGCM_SVC_PARM_32BIT, ("#%u type=%u\n", i, paParms[i].type),
855 VERR_WRONG_PARAMETER_TYPE);
856 paParms[i].u.uint32 = 0;
857 }
858
859 /*
860 * Check restore session ID.
861 */
862 if (idRestoreCheck != 0)
863 {
864 uint64_t idRestore = g_pHelpers->pfnGetVMMDevSessionId(g_pHelpers);
865 if (idRestoreCheck != idRestore)
866 {
867 paParms[0].u.uint64 = idRestore;
868 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VERR_VM_RESTORED (%#RX64 -> %#RX64)\n",
869 pClient->State.uClientID, idRestoreCheck, idRestore));
870 return VERR_VM_RESTORED;
871 }
872 Assert(!g_pHelpers->pfnIsCallRestored(hCall));
873 }
874
875 /*
876 * Return information about the first message if one is pending in the list.
877 */
878 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
879 if (pFirstMsg)
880 {
881 shClSvcMsgSetPeekReturn(pFirstMsg, paParms, cParms);
882 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VINF_SUCCESS (idMsg=%s (%u), cParms=%u)\n",
883 pClient->State.uClientID, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
884 return VINF_SUCCESS;
885 }
886
887 /*
888 * If we cannot wait, fail the call.
889 */
890 if (!fWait)
891 {
892 LogFlowFunc(("[Client %RU32] GUEST_MSG_PEEK_NOWAIT -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
893 return VERR_TRY_AGAIN;
894 }
895
896 /*
897 * Wait for the host to queue a message for this client.
898 */
899 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n",
900 pClient->State.uClientID), VERR_RESOURCE_BUSY);
901 pClient->Pending.hHandle = hCall;
902 pClient->Pending.cParms = cParms;
903 pClient->Pending.paParms = paParms;
904 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT;
905 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
906 return VINF_HGCM_ASYNC_EXECUTE;
907}
908
909/**
910 * Implements VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT.
911 *
912 * @returns VBox status code.
913 * @retval VINF_SUCCESS if a message was pending and is being returned.
914 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
915 *
916 * @param pClient The client state.
917 * @param hCall The client's call handle.
918 * @param cParms Number of parameters.
919 * @param paParms Array of parameters.
920 *
921 * @note Caller takes and leave the client's critical section.
922 */
923static int shClSvcClientMsgOldGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
924{
925 /*
926 * Validate input.
927 */
928 ASSERT_GUEST_RETURN(cParms == VBOX_SHCL_CPARMS_GET_HOST_MSG_OLD, VERR_WRONG_PARAMETER_COUNT);
929 ASSERT_GUEST_RETURN(paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* id32Msg */
930 ASSERT_GUEST_RETURN(paParms[1].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* f32Formats */
931
932 paParms[0].u.uint32 = 0;
933 paParms[1].u.uint32 = 0;
934
935 /*
936 * If there is a message pending we can return immediately.
937 */
938 int rc;
939 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
940 if (pFirstMsg)
941 {
942 LogFlowFunc(("[Client %RU32] uMsg=%s (%RU32), cParms=%RU32\n", pClient->State.uClientID,
943 ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
944
945 rc = shClSvcMsgSetOldWaitReturn(pFirstMsg, paParms, cParms);
946 AssertPtr(g_pHelpers);
947 rc = g_pHelpers->pfnCallComplete(hCall, rc);
948 if (rc != VERR_CANCELLED)
949 {
950 RTListNodeRemove(&pFirstMsg->ListEntry);
951 shClSvcMsgFree(pClient, pFirstMsg);
952
953 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
954 }
955 }
956 /*
957 * Otherwise we must wait.
958 */
959 else
960 {
961 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n", pClient->State.uClientID),
962 VERR_RESOURCE_BUSY);
963
964 pClient->Pending.hHandle = hCall;
965 pClient->Pending.cParms = cParms;
966 pClient->Pending.paParms = paParms;
967 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT;
968
969 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
970
971 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
972 }
973
974 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
975 return rc;
976}
977
978/**
979 * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
980 *
981 * @returns VBox status code.
982 * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
983 * @retval VERR_TRY_AGAIN if no message pending.
984 * @retval VERR_BUFFER_OVERFLOW if a parmeter buffer is too small. The buffer
985 * size was updated to reflect the required size, though this isn't yet
986 * forwarded to the guest. (The guest is better of using peek with
987 * parameter count + 2 parameters to get the sizes.)
988 * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
989 * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
990 *
991 * @param pClient The client state.
992 * @param hCall The client's call handle.
993 * @param cParms Number of parameters.
994 * @param paParms Array of parameters.
995 *
996 * @note Called from within pClient->CritSect.
997 */
998static int shClSvcClientMsgGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
999{
1000 /*
1001 * Validate the request.
1002 */
1003 uint32_t const idMsgExpected = cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT ? paParms[0].u.uint32
1004 : cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT ? paParms[0].u.uint64
1005 : UINT32_MAX;
1006
1007 /*
1008 * Return information about the first message if one is pending in the list.
1009 */
1010 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
1011 if (pFirstMsg)
1012 {
1013 LogFlowFunc(("First message is: %s (%u), cParms=%RU32\n", ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
1014
1015 ASSERT_GUEST_MSG_RETURN(pFirstMsg->idMsg == idMsgExpected || idMsgExpected == UINT32_MAX,
1016 ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
1017 pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->cParms,
1018 idMsgExpected, ShClHostMsgToStr(idMsgExpected), cParms),
1019 VERR_MISMATCH);
1020 ASSERT_GUEST_MSG_RETURN(pFirstMsg->cParms == cParms,
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_WRONG_PARAMETER_COUNT);
1025
1026 /* Check the parameter types. */
1027 for (uint32_t i = 0; i < cParms; i++)
1028 ASSERT_GUEST_MSG_RETURN(pFirstMsg->aParms[i].type == paParms[i].type,
1029 ("param #%u: type %u, caller expected %u (idMsg=%u %s)\n", i, pFirstMsg->aParms[i].type,
1030 paParms[i].type, pFirstMsg->idMsg, ShClHostMsgToStr(pFirstMsg->idMsg)),
1031 VERR_WRONG_PARAMETER_TYPE);
1032 /*
1033 * Copy out the parameters.
1034 *
1035 * No assertions on buffer overflows, and keep going till the end so we can
1036 * communicate all the required buffer sizes.
1037 */
1038 int rc = VINF_SUCCESS;
1039 for (uint32_t i = 0; i < cParms; i++)
1040 switch (pFirstMsg->aParms[i].type)
1041 {
1042 case VBOX_HGCM_SVC_PARM_32BIT:
1043 paParms[i].u.uint32 = pFirstMsg->aParms[i].u.uint32;
1044 break;
1045
1046 case VBOX_HGCM_SVC_PARM_64BIT:
1047 paParms[i].u.uint64 = pFirstMsg->aParms[i].u.uint64;
1048 break;
1049
1050 case VBOX_HGCM_SVC_PARM_PTR:
1051 {
1052 uint32_t const cbSrc = pFirstMsg->aParms[i].u.pointer.size;
1053 uint32_t const cbDst = paParms[i].u.pointer.size;
1054 paParms[i].u.pointer.size = cbSrc; /** @todo Check if this is safe in other layers...
1055 * Update: Safe, yes, but VMMDevHGCM doesn't pass it along. */
1056 if (cbSrc <= cbDst)
1057 memcpy(paParms[i].u.pointer.addr, pFirstMsg->aParms[i].u.pointer.addr, cbSrc);
1058 else
1059 {
1060 AssertMsgFailed(("#%u: cbSrc=%RU32 is bigger than cbDst=%RU32\n", i, cbSrc, cbDst));
1061 rc = VERR_BUFFER_OVERFLOW;
1062 }
1063 break;
1064 }
1065
1066 default:
1067 AssertMsgFailed(("#%u: %u\n", i, pFirstMsg->aParms[i].type));
1068 rc = VERR_INTERNAL_ERROR;
1069 break;
1070 }
1071 if (RT_SUCCESS(rc))
1072 {
1073 /*
1074 * Complete the message and remove the pending message unless the
1075 * guest raced us and cancelled this call in the meantime.
1076 */
1077 AssertPtr(g_pHelpers);
1078 rc = g_pHelpers->pfnCallComplete(hCall, rc);
1079
1080 LogFlowFunc(("[Client %RU32] pfnCallComplete -> %Rrc\n", pClient->State.uClientID, rc));
1081
1082 if (rc != VERR_CANCELLED)
1083 {
1084 RTListNodeRemove(&pFirstMsg->ListEntry);
1085 shClSvcMsgFree(pClient, pFirstMsg);
1086 }
1087
1088 return VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
1089 }
1090
1091 LogFlowFunc(("[Client %RU32] Returning %Rrc\n", pClient->State.uClientID, rc));
1092 return rc;
1093 }
1094
1095 paParms[0].u.uint32 = 0;
1096 paParms[1].u.uint32 = 0;
1097 LogFlowFunc(("[Client %RU32] -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
1098 return VERR_TRY_AGAIN;
1099}
1100
1101/**
1102 * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
1103 *
1104 * @returns VBox status code.
1105 * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
1106 * @retval VERR_TRY_AGAIN if no message pending.
1107 * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
1108 * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
1109 *
1110 * @param pClient The client state.
1111 * @param cParms Number of parameters.
1112 *
1113 * @note Called from within pClient->CritSect.
1114 */
1115static int shClSvcClientMsgCancel(PSHCLCLIENT pClient, uint32_t cParms)
1116{
1117 /*
1118 * Validate the request.
1119 */
1120 ASSERT_GUEST_MSG_RETURN(cParms == 0, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
1121
1122 /*
1123 * Execute.
1124 */
1125 if (pClient->Pending.uType != 0)
1126 {
1127 LogFlowFunc(("[Client %RU32] Cancelling waiting thread, isPending=%d, pendingNumParms=%RU32, m_idSession=%x\n",
1128 pClient->State.uClientID, pClient->Pending.uType, pClient->Pending.cParms, pClient->State.uSessionID));
1129
1130 /*
1131 * The PEEK call is simple: At least two parameters, all set to zero before sleeping.
1132 */
1133 int rcComplete;
1134 if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
1135 {
1136 Assert(pClient->Pending.cParms >= 2);
1137 if (pClient->Pending.paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT)
1138 HGCMSvcSetU64(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
1139 else
1140 HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
1141 rcComplete = VINF_TRY_AGAIN;
1142 }
1143 /*
1144 * The MSG_OLD call is complicated, though we're
1145 * generally here to wake up someone who is peeking and have two parameters.
1146 * If there aren't two parameters, fail the call.
1147 */
1148 else
1149 {
1150 Assert(pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
1151 if (pClient->Pending.cParms > 0)
1152 HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_CANCELED);
1153 if (pClient->Pending.cParms > 1)
1154 HGCMSvcSetU32(&pClient->Pending.paParms[1], 0);
1155 rcComplete = pClient->Pending.cParms == 2 ? VINF_SUCCESS : VERR_TRY_AGAIN;
1156 }
1157
1158 g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, rcComplete);
1159
1160 pClient->Pending.hHandle = NULL;
1161 pClient->Pending.paParms = NULL;
1162 pClient->Pending.cParms = 0;
1163 pClient->Pending.uType = 0;
1164 return VINF_SUCCESS;
1165 }
1166 return VWRN_NOT_FOUND;
1167}
1168
1169
1170/**
1171 * Wakes up a pending client (i.e. waiting for new messages).
1172 *
1173 * @returns VBox status code.
1174 * @retval VINF_NO_CHANGE if the client is not in pending mode.
1175 *
1176 * @param pClient Client to wake up.
1177 * @note Caller must enter pClient->CritSect.
1178 */
1179int shClSvcClientWakeup(PSHCLCLIENT pClient)
1180{
1181 Assert(RTCritSectIsOwner(&pClient->CritSect));
1182 int rc = VINF_NO_CHANGE;
1183
1184 if (pClient->Pending.uType != 0)
1185 {
1186 LogFunc(("[Client %RU32] Waking up ...\n", pClient->State.uClientID));
1187
1188 PSHCLCLIENTMSG pFirstMsg = RTListGetFirst(&pClient->MsgQueue, SHCLCLIENTMSG, ListEntry);
1189 AssertReturn(pFirstMsg, VERR_INTERNAL_ERROR);
1190
1191 LogFunc(("[Client %RU32] Current host message is %s (%RU32), cParms=%RU32\n",
1192 pClient->State.uClientID, ShClHostMsgToStr(pFirstMsg->idMsg), pFirstMsg->idMsg, pFirstMsg->cParms));
1193
1194 if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
1195 shClSvcMsgSetPeekReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
1196 else if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT) /* Legacy, Guest Additions < 6.1. */
1197 shClSvcMsgSetOldWaitReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
1198 else
1199 AssertMsgFailedReturn(("pClient->Pending.uType=%u\n", pClient->Pending.uType), VERR_INTERNAL_ERROR_3);
1200
1201 rc = g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
1202
1203 if ( rc != VERR_CANCELLED
1204 && pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT)
1205 {
1206 RTListNodeRemove(&pFirstMsg->ListEntry);
1207 shClSvcMsgFree(pClient, pFirstMsg);
1208 }
1209
1210 pClient->Pending.hHandle = NULL;
1211 pClient->Pending.paParms = NULL;
1212 pClient->Pending.cParms = 0;
1213 pClient->Pending.uType = 0;
1214 }
1215 else
1216 LogFunc(("[Client %RU32] Not in pending state, skipping wakeup\n", pClient->State.uClientID));
1217
1218 return rc;
1219}
1220
1221/**
1222 * Requests to read clipboard data from the guest.
1223 *
1224 * @returns VBox status code.
1225 * @param pClient Client to request to read data form.
1226 * @param fFormats The formats being requested, OR'ed together (VBOX_SHCL_FMT_XXX).
1227 * @param ppEvent Where to return the event for waiting for new data on success. Optional.
1228 * Must be released by the caller with ShClEventRelease().
1229 */
1230int ShClSvcGuestDataRequest(PSHCLCLIENT pClient, SHCLFORMATS fFormats, PSHCLEVENT *ppEvent)
1231{
1232 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
1233
1234 LogFlowFunc(("fFormats=%#x\n", fFormats));
1235
1236 int rc = VERR_NOT_SUPPORTED;
1237
1238 /* Generate a separate message for every (valid) format we support. */
1239 while (fFormats)
1240 {
1241 /* Pick the next format to get from the mask: */
1242 /** @todo Make format reporting precedence configurable? */
1243 SHCLFORMAT fFormat;
1244 if (fFormats & VBOX_SHCL_FMT_UNICODETEXT)
1245 fFormat = VBOX_SHCL_FMT_UNICODETEXT;
1246 else if (fFormats & VBOX_SHCL_FMT_BITMAP)
1247 fFormat = VBOX_SHCL_FMT_BITMAP;
1248 else if (fFormats & VBOX_SHCL_FMT_HTML)
1249 fFormat = VBOX_SHCL_FMT_HTML;
1250 else
1251 AssertMsgFailedBreak(("%#x\n", fFormats));
1252
1253 /* Remove it from the mask. */
1254 fFormats &= ~fFormat;
1255
1256#ifdef LOG_ENABLED
1257 char *pszFmt = ShClFormatsToStrA(fFormat);
1258 AssertPtrReturn(pszFmt, VERR_NO_MEMORY);
1259 LogRel2(("Shared Clipboard: Requesting guest clipboard data in format '%s'\n", pszFmt));
1260 RTStrFree(pszFmt);
1261#endif
1262 /*
1263 * Allocate messages, one for each format.
1264 */
1265 PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient,
1266 pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID
1267 ? VBOX_SHCL_HOST_MSG_READ_DATA_CID : VBOX_SHCL_HOST_MSG_READ_DATA,
1268 2);
1269 if (pMsg)
1270 {
1271 /*
1272 * Enter the critical section and generate an event.
1273 */
1274 RTCritSectEnter(&pClient->CritSect);
1275
1276 PSHCLEVENT pEvent;
1277 rc = ShClEventSourceGenerateAndRegisterEvent(&pClient->EventSrc, &pEvent);
1278 if (RT_SUCCESS(rc))
1279 {
1280 LogFlowFunc(("fFormats=%#x -> fFormat=%#x, idEvent=%#x\n", fFormats, fFormat, pEvent->idEvent));
1281
1282 const uint64_t uCID = VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID, pClient->EventSrc.uID, pEvent->idEvent);
1283
1284 rc = VINF_SUCCESS;
1285
1286 /* Save the context ID in our legacy cruft if we have to deal with old(er) Guest Additions (< 6.1). */
1287 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1288 {
1289 AssertStmt(pClient->Legacy.cCID < 4096, rc = VERR_TOO_MUCH_DATA);
1290 if (RT_SUCCESS(rc))
1291 {
1292 PSHCLCLIENTLEGACYCID pCID = (PSHCLCLIENTLEGACYCID)RTMemAlloc(sizeof(SHCLCLIENTLEGACYCID));
1293 if (pCID)
1294 {
1295 pCID->uCID = uCID;
1296 pCID->enmType = 0; /* Not used yet. */
1297 pCID->uFormat = fFormat;
1298 RTListAppend(&pClient->Legacy.lstCID, &pCID->Node);
1299 pClient->Legacy.cCID++;
1300 }
1301 else
1302 rc = VERR_NO_MEMORY;
1303 }
1304 }
1305
1306 if (RT_SUCCESS(rc))
1307 {
1308 /*
1309 * Format the message.
1310 */
1311 if (pMsg->idMsg == VBOX_SHCL_HOST_MSG_READ_DATA_CID)
1312 HGCMSvcSetU64(&pMsg->aParms[0], uCID);
1313 else
1314 HGCMSvcSetU32(&pMsg->aParms[0], VBOX_SHCL_HOST_MSG_READ_DATA);
1315 HGCMSvcSetU32(&pMsg->aParms[1], fFormat);
1316
1317 shClSvcMsgAdd(pClient, pMsg, true /* fAppend */);
1318
1319 /* Return event handle to the caller if requested. */
1320 if (ppEvent)
1321 {
1322 *ppEvent = pEvent;
1323 }
1324
1325 shClSvcClientWakeup(pClient);
1326 }
1327
1328 /* Remove event from list if caller did not request event handle or in case
1329 * of failure (in this case caller should not release event). */
1330 if ( RT_FAILURE(rc)
1331 || !ppEvent)
1332 {
1333 ShClEventRelease(pEvent);
1334 }
1335 }
1336 else
1337 rc = VERR_SHCLPB_MAX_EVENTS_REACHED;
1338
1339 RTCritSectLeave(&pClient->CritSect);
1340
1341 if (RT_FAILURE(rc))
1342 shClSvcMsgFree(pClient, pMsg);
1343 }
1344 else
1345 rc = VERR_NO_MEMORY;
1346
1347 if (RT_FAILURE(rc))
1348 break;
1349 }
1350
1351 if (RT_FAILURE(rc))
1352 LogRel(("Shared Clipboard: Requesting data in formats %#x from guest failed with %Rrc\n", fFormats, rc));
1353
1354 LogFlowFuncLeaveRC(rc);
1355 return rc;
1356}
1357
1358/**
1359 * Signals the host that clipboard data from the guest has been received.
1360 *
1361 * @returns VBox status code. Returns VERR_NOT_FOUND when related event ID was not found.
1362 * @param pClient Client the guest clipboard data was received from.
1363 * @param pCmdCtx Client command context.
1364 * @param uFormat Clipboard format of data received.
1365 * @param pvData Pointer to clipboard data received. This can be
1366 * NULL if @a cbData is zero.
1367 * @param cbData Size (in bytes) of clipboard data received.
1368 * This can be zero.
1369 */
1370int ShClSvcGuestDataSignal(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
1371{
1372 LogFlowFuncEnter();
1373 RT_NOREF(uFormat);
1374
1375 /*
1376 * Validate input.
1377 */
1378 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
1379 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
1380 if (cbData > 0)
1381 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
1382
1383 const SHCLEVENTID idEvent = VBOX_SHCL_CONTEXTID_GET_EVENT(pCmdCtx->uContextID);
1384 AssertMsgReturn(idEvent != NIL_SHCLEVENTID, ("NIL event in context ID %#RX64\n", pCmdCtx->uContextID), VERR_WRONG_ORDER);
1385
1386 PSHCLEVENT pEvent = ShClEventSourceGetFromId(&pClient->EventSrc, idEvent);
1387 AssertMsgReturn(pEvent != NULL, ("Event %#x not found\n", idEvent), VERR_NOT_FOUND);
1388
1389 /*
1390 * Make a copy of the data so we can attach it to the signal.
1391 *
1392 * Note! We still signal the waiter should we run out of memory,
1393 * because otherwise it will be stuck waiting.
1394 */
1395 int rc = VINF_SUCCESS;
1396 PSHCLEVENTPAYLOAD pPayload = NULL;
1397 if (cbData > 0)
1398 rc = ShClPayloadAlloc(idEvent, pvData, cbData, &pPayload);
1399
1400 /*
1401 * Signal the event.
1402 */
1403 int rc2 = ShClEventSignal(pEvent, pPayload);
1404 if (RT_FAILURE(rc2))
1405 {
1406 rc = rc2;
1407 ShClPayloadFree(pPayload);
1408 LogRel(("Shared Clipboard: Signalling of guest clipboard data to the host failed: %Rrc\n", rc));
1409 }
1410
1411 LogFlowFuncLeaveRC(rc);
1412 return rc;
1413}
1414
1415/**
1416 * Reports available VBox clipboard formats to the guest.
1417 *
1418 * @note Host backend callers must check if it's active (use
1419 * ShClSvcIsBackendActive) before calling to prevent mixing up the
1420 * VRDE clipboard.
1421 *
1422 * @returns VBox status code.
1423 * @param pClient Client to report clipboard formats to.
1424 * @param fFormats The formats to report (VBOX_SHCL_FMT_XXX), zero
1425 * is okay (empty the clipboard).
1426 */
1427int ShClSvcHostReportFormats(PSHCLCLIENT pClient, SHCLFORMATS fFormats)
1428{
1429 /*
1430 * Check if the service mode allows this operation and whether the guest is
1431 * supposed to be reading from the host. Otherwise, silently ignore reporting
1432 * formats and return VINF_SUCCESS in order to do not trigger client
1433 * termination in svcConnect().
1434 */
1435 uint32_t uMode = ShClSvcGetMode();
1436 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1437 || uMode == VBOX_SHCL_MODE_HOST_TO_GUEST)
1438 { /* likely */ }
1439 else
1440 return VINF_SUCCESS;
1441
1442 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
1443
1444 LogFlowFunc(("fFormats=%#x\n", fFormats));
1445
1446#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1447 bool fSkipTransfers = false;
1448 if (!(g_fTransferMode & VBOX_SHCL_TRANSFER_MODE_F_ENABLED))
1449 {
1450 LogRel2(("Shared Clipboard: File transfers are disabled, skipping reporting those to the guest\n"));
1451 fSkipTransfers = true;
1452 }
1453 else if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_TRANSFERS))
1454 {
1455 LogRel2(("Shared Clipboard: File transfers not supported by installed Guest Addtions, skipping reporting those to the guest\n"));
1456 fSkipTransfers = true;
1457 }
1458
1459 if (fSkipTransfers)
1460 fFormats &= ~VBOX_SHCL_FMT_URI_LIST;
1461#endif
1462
1463#ifdef LOG_ENABLED
1464 char *pszFmts = ShClFormatsToStrA(fFormats);
1465 AssertPtrReturn(pszFmts, VERR_NO_MEMORY);
1466 LogRel2(("Shared Clipboard: Reporting formats '%s' to guest\n", pszFmts));
1467 RTStrFree(pszFmts);
1468#endif
1469
1470 /*
1471 * Allocate a message, populate parameters and post it to the client.
1472 */
1473 int rc;
1474 PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient, VBOX_SHCL_HOST_MSG_FORMATS_REPORT, 2);
1475 if (pMsg)
1476 {
1477 HGCMSvcSetU32(&pMsg->aParms[0], VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
1478 HGCMSvcSetU32(&pMsg->aParms[1], fFormats);
1479
1480 RTCritSectEnter(&pClient->CritSect);
1481 shClSvcMsgAddAndWakeupClient(pClient, pMsg);
1482 RTCritSectLeave(&pClient->CritSect);
1483
1484#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1485 /* Create a transfer locally and also tell the guest to create a transfer on the guest side. */
1486 if (!fSkipTransfers)
1487 {
1488 rc = shClSvcTransferStart(pClient, SHCLTRANSFERDIR_TO_REMOTE, SHCLSOURCE_LOCAL,
1489 NULL /* pTransfer */);
1490 if (RT_SUCCESS(rc))
1491 rc = shClSvcSetSource(pClient, SHCLSOURCE_LOCAL);
1492
1493 if (RT_FAILURE(rc))
1494 LogRel(("Shared Clipboard: Initializing host write transfer failed with %Rrc\n", rc));
1495 }
1496 else
1497#endif
1498 rc = VINF_SUCCESS;
1499 }
1500 else
1501 rc = VERR_NO_MEMORY;
1502
1503 if (RT_FAILURE(rc))
1504 LogRel(("Shared Clipboard: Reporting formats %#x to guest failed with %Rrc\n", fFormats, rc));
1505
1506 LogFlowFuncLeaveRC(rc);
1507 return rc;
1508}
1509
1510
1511/**
1512 * Handles the VBOX_SHCL_GUEST_FN_REPORT_FORMATS message from the guest.
1513 */
1514static int shClSvcClientReportFormats(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1515{
1516 /*
1517 * Check if the service mode allows this operation and whether the guest is
1518 * supposed to be reading from the host.
1519 */
1520 uint32_t uMode = ShClSvcGetMode();
1521 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1522 || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
1523 { /* likely */ }
1524 else
1525 return VERR_ACCESS_DENIED;
1526
1527 /*
1528 * Digest parameters.
1529 */
1530 ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS
1531 || ( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B
1532 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
1533 VERR_WRONG_PARAMETER_COUNT);
1534
1535 uintptr_t iParm = 0;
1536 if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
1537 {
1538 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1539 /* no defined value, so just ignore it */
1540 iParm++;
1541 }
1542 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1543 uint32_t const fFormats = paParms[iParm].u.uint32;
1544 iParm++;
1545 if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
1546 {
1547 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1548 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1549 iParm++;
1550 }
1551 Assert(iParm == cParms);
1552
1553 /*
1554 * Report the formats.
1555 *
1556 * We ignore empty reports if the guest isn't the clipboard owner, this
1557 * prevents a freshly booted guest with an empty clibpoard from clearing
1558 * the host clipboard on startup. Likewise, when a guest shutdown it will
1559 * typically issue an empty report in case it's the owner, we don't want
1560 * that to clear host content either.
1561 */
1562 int rc;
1563 if (!fFormats && pClient->State.enmSource != SHCLSOURCE_REMOTE)
1564 rc = VINF_SUCCESS;
1565 else
1566 {
1567 rc = shClSvcSetSource(pClient, SHCLSOURCE_REMOTE);
1568 if (RT_SUCCESS(rc))
1569 {
1570 rc = RTCritSectEnter(&g_CritSect);
1571 if (RT_SUCCESS(rc))
1572 {
1573 if (g_ExtState.pfnExtension)
1574 {
1575 SHCLEXTPARMS parms;
1576 RT_ZERO(parms);
1577 parms.uFormat = fFormats;
1578
1579 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof(parms));
1580 }
1581 else
1582 {
1583#ifdef LOG_ENABLED
1584 char *pszFmts = ShClFormatsToStrA(fFormats);
1585 if (pszFmts)
1586 {
1587 LogRel2(("Shared Clipboard: Guest reported formats '%s' to host\n", pszFmts));
1588 RTStrFree(pszFmts);
1589 }
1590#endif
1591 rc = ShClBackendReportFormats(&g_ShClBackend, pClient, fFormats);
1592 if (RT_FAILURE(rc))
1593 LogRel(("Shared Clipboard: Reporting guest clipboard formats to the host failed with %Rrc\n", rc));
1594 }
1595
1596 RTCritSectLeave(&g_CritSect);
1597 }
1598 else
1599 LogRel2(("Shared Clipboard: Unable to take internal lock while receiving guest clipboard announcement: %Rrc\n", rc));
1600 }
1601 }
1602
1603 return rc;
1604}
1605
1606/**
1607 * Called when the guest wants to read host clipboard data.
1608 * Handles the VBOX_SHCL_GUEST_FN_DATA_READ message.
1609 *
1610 * @returns VBox status code.
1611 * @retval VINF_BUFFER_OVERFLOW if the guest supplied a smaller buffer than needed in order to read the host clipboard data.
1612 * @param pClient Client that wants to read host clipboard data.
1613 * @param cParms Number of HGCM parameters supplied in \a paParms.
1614 * @param paParms Array of HGCM parameters.
1615 */
1616static int shClSvcClientReadData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1617{
1618 LogFlowFuncEnter();
1619
1620 /*
1621 * Check if the service mode allows this operation and whether the guest is
1622 * supposed to be reading from the host.
1623 */
1624 uint32_t uMode = ShClSvcGetMode();
1625 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1626 || uMode == VBOX_SHCL_MODE_HOST_TO_GUEST)
1627 { /* likely */ }
1628 else
1629 return VERR_ACCESS_DENIED;
1630
1631 /*
1632 * Digest parameters.
1633 *
1634 * We are dragging some legacy here from the 6.1 dev cycle, a 5 parameter
1635 * variant which prepends a 64-bit context ID (RAZ as meaning not defined),
1636 * a 32-bit flag (MBZ, no defined meaning) and switches the last two parameters.
1637 */
1638 ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_DATA_READ
1639 || ( cParms == VBOX_SHCL_CPARMS_DATA_READ_61B
1640 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
1641 VERR_WRONG_PARAMETER_COUNT);
1642
1643 uintptr_t iParm = 0;
1644 SHCLCLIENTCMDCTX cmdCtx;
1645 RT_ZERO(cmdCtx);
1646 if (cParms == VBOX_SHCL_CPARMS_DATA_READ_61B)
1647 {
1648 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1649 /* This has no defined meaning and was never used, however the guest passed stuff, so ignore it and leave idContext=0. */
1650 iParm++;
1651 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1652 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1653 iParm++;
1654 }
1655
1656 SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
1657 uint32_t cbData = 0;
1658 void *pvData = NULL;
1659
1660 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1661 uFormat = paParms[iParm].u.uint32;
1662 iParm++;
1663 if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
1664 {
1665 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1666 pvData = paParms[iParm].u.pointer.addr;
1667 cbData = paParms[iParm].u.pointer.size;
1668 iParm++;
1669 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
1670 iParm++;
1671 }
1672 else
1673 {
1674 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
1675 iParm++;
1676 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1677 pvData = paParms[iParm].u.pointer.addr;
1678 cbData = paParms[iParm].u.pointer.size;
1679 iParm++;
1680 }
1681 Assert(iParm == cParms);
1682
1683 /*
1684 * For some reason we need to do this (makes absolutely no sense to bird).
1685 */
1686 /** @todo r=bird: I really don't get why you need the State.POD.uFormat
1687 * member. I'm sure there is a reason. Incomplete code? */
1688 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1689 {
1690 if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
1691 pClient->State.POD.uFormat = uFormat;
1692 }
1693
1694#ifdef LOG_ENABLED
1695 char *pszFmt = ShClFormatsToStrA(uFormat);
1696 AssertPtrReturn(pszFmt, VERR_NO_MEMORY);
1697 LogRel2(("Shared Clipboard: Guest wants to read %RU32 bytes host clipboard data in format '%s'\n", cbData, pszFmt));
1698 RTStrFree(pszFmt);
1699#endif
1700
1701 /*
1702 * Do the reading.
1703 */
1704 uint32_t cbActual = 0;
1705
1706 int rc = RTCritSectEnter(&g_CritSect);
1707 AssertRCReturn(rc, rc);
1708
1709 /* If there is a service extension active, try reading data from it first. */
1710 if (g_ExtState.pfnExtension)
1711 {
1712 SHCLEXTPARMS parms;
1713 RT_ZERO(parms);
1714
1715 parms.uFormat = uFormat;
1716 parms.u.pvData = pvData;
1717 parms.cbData = cbData;
1718
1719 g_ExtState.fReadingData = true;
1720
1721 /* Read clipboard data from the extension. */
1722 rc = g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ, &parms, sizeof(parms));
1723
1724 LogRel2(("Shared Clipboard: Read extension clipboard data (fDelayedAnnouncement=%RTbool, fDelayedFormats=%#x, max %RU32 bytes), got %RU32 bytes: rc=%Rrc\n",
1725 g_ExtState.fDelayedAnnouncement, g_ExtState.fDelayedFormats, cbData, parms.cbData, rc));
1726
1727 /* Did the extension send the clipboard formats yet?
1728 * Otherwise, do this now. */
1729 if (g_ExtState.fDelayedAnnouncement)
1730 {
1731 int rc2 = ShClSvcHostReportFormats(pClient, g_ExtState.fDelayedFormats);
1732 AssertRC(rc2);
1733
1734 g_ExtState.fDelayedAnnouncement = false;
1735 g_ExtState.fDelayedFormats = 0;
1736 }
1737
1738 g_ExtState.fReadingData = false;
1739
1740 if (RT_SUCCESS(rc))
1741 cbActual = parms.cbData;
1742 }
1743 else
1744 {
1745 rc = ShClBackendReadData(&g_ShClBackend, pClient, &cmdCtx, uFormat, pvData, cbData, &cbActual);
1746 if (RT_SUCCESS(rc))
1747 LogRel2(("Shared Clipboard: Read host clipboard data (max %RU32 bytes), got %RU32 bytes\n", cbData, cbActual));
1748 else
1749 LogRel(("Shared Clipboard: Reading host clipboard data failed with %Rrc\n", rc));
1750 }
1751
1752 if (RT_SUCCESS(rc))
1753 {
1754 /* Return the actual size required to fullfil the request. */
1755 if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
1756 HGCMSvcSetU32(&paParms[2], cbActual);
1757 else
1758 HGCMSvcSetU32(&paParms[3], cbActual);
1759
1760 /* If the data to return exceeds the buffer the guest supplies, tell it (and let it try again). */
1761 if (cbActual >= cbData)
1762 rc = VINF_BUFFER_OVERFLOW;
1763 }
1764
1765 RTCritSectLeave(&g_CritSect);
1766
1767 LogFlowFuncLeaveRC(rc);
1768 return rc;
1769}
1770
1771/**
1772 * Called when the guest writes clipboard data to the host.
1773 * Handles the VBOX_SHCL_GUEST_FN_DATA_WRITE message.
1774 *
1775 * @returns VBox status code.
1776 * @param pClient Client that wants to read host clipboard data.
1777 * @param cParms Number of HGCM parameters supplied in \a paParms.
1778 * @param paParms Array of HGCM parameters.
1779 */
1780static int shClSvcClientWriteData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1781{
1782 LogFlowFuncEnter();
1783
1784 /*
1785 * Check if the service mode allows this operation and whether the guest is
1786 * supposed to be reading from the host.
1787 */
1788 uint32_t uMode = ShClSvcGetMode();
1789 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1790 || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
1791 { /* likely */ }
1792 else
1793 return VERR_ACCESS_DENIED;
1794
1795 const bool fReportsContextID = RT_BOOL(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID);
1796
1797 /*
1798 * Digest parameters.
1799 *
1800 * There are 3 different format here, formatunately no parameters have been
1801 * switch around so it's plain sailing compared to the DATA_READ message.
1802 */
1803 ASSERT_GUEST_RETURN(fReportsContextID
1804 ? cParms == VBOX_SHCL_CPARMS_DATA_WRITE || cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B
1805 : cParms == VBOX_SHCL_CPARMS_DATA_WRITE_OLD,
1806 VERR_WRONG_PARAMETER_COUNT);
1807
1808 uintptr_t iParm = 0;
1809 SHCLCLIENTCMDCTX cmdCtx;
1810 RT_ZERO(cmdCtx);
1811 if (cParms > VBOX_SHCL_CPARMS_DATA_WRITE_OLD)
1812 {
1813 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1814 cmdCtx.uContextID = paParms[iParm].u.uint64;
1815 iParm++;
1816 }
1817 else
1818 {
1819 /* Older Guest Additions (< 6.1) did not supply a context ID.
1820 * We dig it out from our saved context ID list then a bit down below. */
1821 }
1822
1823 if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
1824 {
1825 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1826 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1827 iParm++;
1828 }
1829
1830 SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
1831 uint32_t cbData = 0;
1832 void *pvData = NULL;
1833
1834 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* Format bit. */
1835 uFormat = paParms[iParm].u.uint32;
1836 iParm++;
1837 if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
1838 {
1839 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* "cbData" - duplicates buffer size. */
1840 iParm++;
1841 }
1842 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1843 pvData = paParms[iParm].u.pointer.addr;
1844 cbData = paParms[iParm].u.pointer.size;
1845 iParm++;
1846 Assert(iParm == cParms);
1847
1848 /*
1849 * Handle / check context ID.
1850 */
1851 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. */
1852 {
1853 PSHCLCLIENTLEGACYCID pCID = NULL;
1854 PSHCLCLIENTLEGACYCID pCIDIter;
1855 RTListForEach(&pClient->Legacy.lstCID, pCIDIter, SHCLCLIENTLEGACYCID, Node) /* Slow, but does the job for now. */
1856 {
1857 if (pCIDIter->uFormat == uFormat)
1858 {
1859 pCID = pCIDIter;
1860 break;
1861 }
1862 }
1863
1864 ASSERT_GUEST_MSG_RETURN(pCID != NULL, ("Context ID for format %#x not found\n", uFormat), VERR_INVALID_CONTEXT);
1865 cmdCtx.uContextID = pCID->uCID;
1866
1867 /* Not needed anymore; clean up. */
1868 Assert(pClient->Legacy.cCID);
1869 pClient->Legacy.cCID--;
1870 RTListNodeRemove(&pCID->Node);
1871 RTMemFree(pCID);
1872 }
1873
1874 uint64_t const idCtxExpected = VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID, pClient->EventSrc.uID,
1875 VBOX_SHCL_CONTEXTID_GET_EVENT(cmdCtx.uContextID));
1876 ASSERT_GUEST_MSG_RETURN(cmdCtx.uContextID == idCtxExpected,
1877 ("Wrong context ID: %#RX64, expected %#RX64\n", cmdCtx.uContextID, idCtxExpected),
1878 VERR_INVALID_CONTEXT);
1879
1880 /*
1881 * For some reason we need to do this (makes absolutely no sense to bird).
1882 */
1883 /** @todo r=bird: I really don't get why you need the State.POD.uFormat
1884 * member. I'm sure there is a reason. Incomplete code? */
1885 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1886 {
1887 if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
1888 pClient->State.POD.uFormat = uFormat;
1889 }
1890
1891#ifdef LOG_ENABLED
1892 char *pszFmt = ShClFormatsToStrA(uFormat);
1893 if (pszFmt)
1894 {
1895 LogRel2(("Shared Clipboard: Guest writes %RU32 bytes clipboard data in format '%s' to host\n", cbData, pszFmt));
1896 RTStrFree(pszFmt);
1897 }
1898#endif
1899
1900 /*
1901 * Write the data to the active host side clipboard.
1902 */
1903 int rc = RTCritSectEnter(&g_CritSect);
1904 AssertRCReturn(rc, rc);
1905
1906 if (g_ExtState.pfnExtension)
1907 {
1908 SHCLEXTPARMS parms;
1909 RT_ZERO(parms);
1910 parms.uFormat = uFormat;
1911 parms.u.pvData = pvData;
1912 parms.cbData = cbData;
1913
1914 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof(parms));
1915 rc = VINF_SUCCESS;
1916 }
1917 else
1918 {
1919 /* Let the backend implementation know. */
1920 rc = ShClBackendWriteData(&g_ShClBackend, pClient, &cmdCtx, uFormat, pvData, cbData);
1921 if (RT_FAILURE(rc))
1922 LogRel(("Shared Clipboard: Writing guest clipboard data to the host failed with %Rrc\n", rc));
1923
1924 int rc2; /* Don't return internals back to the guest. */
1925 rc2 = ShClSvcGuestDataSignal(pClient, &cmdCtx, uFormat, pvData, cbData); /* To complete pending events, if any. */
1926 if (RT_FAILURE(rc2))
1927 LogRel(("Shared Clipboard: Signalling host about guest clipboard data failed with %Rrc\n", rc2));
1928 AssertRC(rc2);
1929 }
1930
1931 RTCritSectLeave(&g_CritSect);
1932
1933 LogFlowFuncLeaveRC(rc);
1934 return rc;
1935}
1936
1937/**
1938 * Gets an error from HGCM service parameters.
1939 *
1940 * @returns VBox status code.
1941 * @param cParms Number of HGCM parameters supplied in \a paParms.
1942 * @param paParms Array of HGCM parameters.
1943 * @param pRc Where to store the received error code.
1944 */
1945static int shClSvcClientError(uint32_t cParms, VBOXHGCMSVCPARM paParms[], int *pRc)
1946{
1947 AssertPtrReturn(paParms, VERR_INVALID_PARAMETER);
1948 AssertPtrReturn(pRc, VERR_INVALID_PARAMETER);
1949
1950 int rc;
1951
1952 if (cParms == VBOX_SHCL_CPARMS_ERROR)
1953 {
1954 rc = HGCMSvcGetU32(&paParms[1], (uint32_t *)pRc); /** @todo int vs. uint32_t !!! */
1955 }
1956 else
1957 rc = VERR_INVALID_PARAMETER;
1958
1959 LogFlowFuncLeaveRC(rc);
1960 return rc;
1961}
1962
1963/**
1964 * Sets the transfer source type of a Shared Clipboard client.
1965 *
1966 * @returns VBox status code.
1967 * @param pClient Client to set transfer source type for.
1968 * @param enmSource Source type to set.
1969 */
1970int shClSvcSetSource(PSHCLCLIENT pClient, SHCLSOURCE enmSource)
1971{
1972 if (!pClient) /* If no client connected (anymore), bail out. */
1973 return VINF_SUCCESS;
1974
1975 int rc = VINF_SUCCESS;
1976
1977 if (ShClSvcLock())
1978 {
1979 pClient->State.enmSource = enmSource;
1980
1981 LogFlowFunc(("Source of client %RU32 is now %RU32\n", pClient->State.uClientID, pClient->State.enmSource));
1982
1983 ShClSvcUnlock();
1984 }
1985
1986 LogFlowFuncLeaveRC(rc);
1987 return rc;
1988}
1989
1990static int svcInit(VBOXHGCMSVCFNTABLE *pTable)
1991{
1992 int rc = RTCritSectInit(&g_CritSect);
1993
1994 if (RT_SUCCESS(rc))
1995 {
1996 shClSvcModeSet(VBOX_SHCL_MODE_OFF);
1997
1998 rc = ShClBackendInit(ShClSvcGetBackend(), pTable);
1999
2000 /* Clean up on failure, because 'svnUnload' will not be called
2001 * if the 'svcInit' returns an error.
2002 */
2003 if (RT_FAILURE(rc))
2004 {
2005 RTCritSectDelete(&g_CritSect);
2006 }
2007 }
2008
2009 return rc;
2010}
2011
2012static DECLCALLBACK(int) svcUnload(void *)
2013{
2014 LogFlowFuncEnter();
2015
2016 ShClBackendDestroy(ShClSvcGetBackend());
2017
2018 RTCritSectDelete(&g_CritSect);
2019
2020 return VINF_SUCCESS;
2021}
2022
2023static DECLCALLBACK(int) svcDisconnect(void *, uint32_t u32ClientID, void *pvClient)
2024{
2025 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
2026
2027 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2028 AssertPtr(pClient);
2029
2030 /* In order to communicate with guest service, HGCM VRDP clipboard extension
2031 * needs to know its connection client ID. Currently, in svcConnect() we always
2032 * cache ID of the first ever connected client. When client disconnects,
2033 * we need to forget its ID and let svcConnect() to pick up the next ID when a new
2034 * connection will be requested by guest service (see #10115). */
2035 if (g_ExtState.uClientID == u32ClientID)
2036 {
2037 g_ExtState.uClientID = 0;
2038 }
2039
2040#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2041 shClSvcClientTransfersReset(pClient);
2042#endif
2043
2044 ShClBackendDisconnect(&g_ShClBackend, pClient);
2045
2046 shClSvcClientDestroy(pClient);
2047
2048 return VINF_SUCCESS;
2049}
2050
2051static DECLCALLBACK(int) svcConnect(void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
2052{
2053 RT_NOREF(fRequestor, fRestoring);
2054
2055 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2056 AssertPtr(pvClient);
2057
2058 int rc = shClSvcClientInit(pClient, u32ClientID);
2059 if (RT_SUCCESS(rc))
2060 {
2061 /* Assign weak pointer to client map. */
2062 /** @todo r=bird: The g_mapClients is only there for looking up
2063 * g_ExtState.uClientID (unserialized btw), so why not use store the
2064 * pClient value directly in g_ExtState instead of the ID? It cannot
2065 * crash any worse that racing map insertion/removal. */
2066 g_mapClients[u32ClientID] = pClient; /** @todo Handle OOM / collisions? */
2067 rc = ShClBackendConnect(&g_ShClBackend, pClient, ShClSvcGetHeadless());
2068 if (RT_SUCCESS(rc))
2069 {
2070 /* Sync the host clipboard content with the client. */
2071 rc = ShClBackendSync(&g_ShClBackend, pClient);
2072 if (RT_SUCCESS(rc))
2073 {
2074 /* For now we ASSUME that the first client that connects is in charge for
2075 communicating with the service extension. */
2076 /** @todo This isn't optimal, but only the guest really knows which client is in
2077 * focus on the console. See @bugref{10115} for details. */
2078 if (g_ExtState.uClientID == 0)
2079 g_ExtState.uClientID = u32ClientID;
2080
2081 /* The sync could return VINF_NO_CHANGE if nothing has changed on the host, but
2082 older Guest Additions didn't use RT_SUCCESS to but == VINF_SUCCESS to check for
2083 success. So just return VINF_SUCCESS here to not break older Guest Additions. */
2084 LogFunc(("Successfully connected client %#x%s\n",
2085 u32ClientID, g_ExtState.uClientID == u32ClientID ? " - Use by ExtState too" : ""));
2086 return VINF_SUCCESS;
2087 }
2088
2089 LogFunc(("ShClBackendSync failed: %Rrc\n", rc));
2090 ShClBackendDisconnect(&g_ShClBackend, pClient);
2091 }
2092 else
2093 LogFunc(("ShClBackendConnect failed: %Rrc\n", rc));
2094 shClSvcClientDestroy(pClient);
2095 }
2096 else
2097 LogFunc(("shClSvcClientInit failed: %Rrc\n", rc));
2098 LogFlowFuncLeaveRC(rc);
2099 return rc;
2100}
2101
2102static DECLCALLBACK(void) svcCall(void *,
2103 VBOXHGCMCALLHANDLE callHandle,
2104 uint32_t u32ClientID,
2105 void *pvClient,
2106 uint32_t u32Function,
2107 uint32_t cParms,
2108 VBOXHGCMSVCPARM paParms[],
2109 uint64_t tsArrival)
2110{
2111 RT_NOREF(u32ClientID, pvClient, tsArrival);
2112 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2113 AssertPtr(pClient);
2114
2115#ifdef LOG_ENABLED
2116 Log2Func(("u32ClientID=%RU32, fn=%RU32 (%s), cParms=%RU32, paParms=%p\n",
2117 u32ClientID, u32Function, ShClGuestMsgToStr(u32Function), cParms, paParms));
2118 for (uint32_t i = 0; i < cParms; i++)
2119 {
2120 switch (paParms[i].type)
2121 {
2122 case VBOX_HGCM_SVC_PARM_32BIT:
2123 Log3Func((" paParms[%RU32]: type uint32_t - value %RU32\n", i, paParms[i].u.uint32));
2124 break;
2125 case VBOX_HGCM_SVC_PARM_64BIT:
2126 Log3Func((" paParms[%RU32]: type uint64_t - value %RU64\n", i, paParms[i].u.uint64));
2127 break;
2128 case VBOX_HGCM_SVC_PARM_PTR:
2129 Log3Func((" paParms[%RU32]: type ptr - value 0x%p (%RU32 bytes)\n",
2130 i, paParms[i].u.pointer.addr, paParms[i].u.pointer.size));
2131 break;
2132 case VBOX_HGCM_SVC_PARM_PAGES:
2133 Log3Func((" paParms[%RU32]: type pages - cb=%RU32, cPages=%RU16\n",
2134 i, paParms[i].u.Pages.cb, paParms[i].u.Pages.cPages));
2135 break;
2136 default:
2137 AssertFailed();
2138 }
2139 }
2140 Log2Func(("Client state: fFlags=0x%x, fGuestFeatures0=0x%x, fGuestFeatures1=0x%x\n",
2141 pClient->State.fFlags, pClient->State.fGuestFeatures0, pClient->State.fGuestFeatures1));
2142#endif
2143
2144 int rc;
2145 switch (u32Function)
2146 {
2147 case VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT:
2148 RTCritSectEnter(&pClient->CritSect);
2149 rc = shClSvcClientMsgOldGet(pClient, callHandle, cParms, paParms);
2150 RTCritSectLeave(&pClient->CritSect);
2151 break;
2152
2153 case VBOX_SHCL_GUEST_FN_CONNECT:
2154 LogRel(("Shared Clipboard: 6.1.0 beta or rc Guest Additions detected. Please upgrade!\n"));
2155 rc = VERR_NOT_IMPLEMENTED;
2156 break;
2157
2158 case VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE:
2159 rc = shClSvcClientNegogiateChunkSize(pClient, callHandle, cParms, paParms);
2160 break;
2161
2162 case VBOX_SHCL_GUEST_FN_REPORT_FEATURES:
2163 rc = shClSvcClientReportFeatures(pClient, callHandle, cParms, paParms);
2164 break;
2165
2166 case VBOX_SHCL_GUEST_FN_QUERY_FEATURES:
2167 rc = shClSvcClientQueryFeatures(callHandle, cParms, paParms);
2168 break;
2169
2170 case VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT:
2171 RTCritSectEnter(&pClient->CritSect);
2172 rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, false /*fWait*/);
2173 RTCritSectLeave(&pClient->CritSect);
2174 break;
2175
2176 case VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT:
2177 RTCritSectEnter(&pClient->CritSect);
2178 rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, true /*fWait*/);
2179 RTCritSectLeave(&pClient->CritSect);
2180 break;
2181
2182 case VBOX_SHCL_GUEST_FN_MSG_GET:
2183 RTCritSectEnter(&pClient->CritSect);
2184 rc = shClSvcClientMsgGet(pClient, callHandle, cParms, paParms);
2185 RTCritSectLeave(&pClient->CritSect);
2186 break;
2187
2188 case VBOX_SHCL_GUEST_FN_MSG_CANCEL:
2189 RTCritSectEnter(&pClient->CritSect);
2190 rc = shClSvcClientMsgCancel(pClient, cParms);
2191 RTCritSectLeave(&pClient->CritSect);
2192 break;
2193
2194 case VBOX_SHCL_GUEST_FN_REPORT_FORMATS:
2195 rc = shClSvcClientReportFormats(pClient, cParms, paParms);
2196 break;
2197
2198 case VBOX_SHCL_GUEST_FN_DATA_READ:
2199 rc = shClSvcClientReadData(pClient, cParms, paParms);
2200 break;
2201
2202 case VBOX_SHCL_GUEST_FN_DATA_WRITE:
2203 rc = shClSvcClientWriteData(pClient, cParms, paParms);
2204 break;
2205
2206 case VBOX_SHCL_GUEST_FN_ERROR:
2207 {
2208 int rcGuest;
2209 rc = shClSvcClientError(cParms,paParms, &rcGuest);
2210 if (RT_SUCCESS(rc))
2211 {
2212 LogRel(("Shared Clipboard: Error reported from guest side: %Rrc\n", rcGuest));
2213
2214 shClSvcClientLock(pClient);
2215
2216 /* Reset message queue. */
2217 shClSvcMsgQueueReset(pClient);
2218
2219#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2220 shClSvcClientTransfersReset(pClient);
2221#endif
2222 shClSvcClientUnlock(pClient);
2223 }
2224 break;
2225 }
2226
2227 default:
2228 {
2229#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2230 if ( u32Function <= VBOX_SHCL_GUEST_FN_LAST
2231 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID) )
2232 {
2233 if (g_fTransferMode & VBOX_SHCL_TRANSFER_MODE_F_ENABLED)
2234 rc = shClSvcTransferHandler(pClient, callHandle, u32Function, cParms, paParms, tsArrival);
2235 else
2236 {
2237 LogRel2(("Shared Clipboard: File transfers are disabled for this VM\n"));
2238 rc = VERR_ACCESS_DENIED;
2239 }
2240 }
2241 else
2242#endif
2243 {
2244 LogRel2(("Shared Clipboard: Unknown guest function: %u (%#x)\n", u32Function, u32Function));
2245 rc = VERR_NOT_IMPLEMENTED;
2246 }
2247 break;
2248 }
2249 }
2250
2251 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
2252
2253 if (rc != VINF_HGCM_ASYNC_EXECUTE)
2254 g_pHelpers->pfnCallComplete(callHandle, rc);
2255}
2256
2257/**
2258 * Initializes a Shared Clipboard service's client state.
2259 *
2260 * @returns VBox status code.
2261 * @param pClientState Client state to initialize.
2262 * @param uClientID Client ID (HGCM) to use for this client state.
2263 */
2264int shClSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID)
2265{
2266 LogFlowFuncEnter();
2267
2268 shclSvcClientStateReset(pClientState);
2269
2270 /* Register the client. */
2271 pClientState->uClientID = uClientID;
2272
2273 return VINF_SUCCESS;
2274}
2275
2276/**
2277 * Destroys a Shared Clipboard service's client state.
2278 *
2279 * @returns VBox status code.
2280 * @param pClientState Client state to destroy.
2281 */
2282int shClSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState)
2283{
2284 RT_NOREF(pClientState);
2285
2286 LogFlowFuncEnter();
2287
2288 return VINF_SUCCESS;
2289}
2290
2291/**
2292 * Resets a Shared Clipboard service's client state.
2293 *
2294 * @param pClientState Client state to reset.
2295 */
2296void shclSvcClientStateReset(PSHCLCLIENTSTATE pClientState)
2297{
2298 LogFlowFuncEnter();
2299
2300 pClientState->fGuestFeatures0 = VBOX_SHCL_GF_NONE;
2301 pClientState->fGuestFeatures1 = VBOX_SHCL_GF_NONE;
2302
2303 pClientState->cbChunkSize = VBOX_SHCL_DEFAULT_CHUNK_SIZE; /** @todo Make this configurable. */
2304 pClientState->enmSource = SHCLSOURCE_INVALID;
2305 pClientState->fFlags = SHCLCLIENTSTATE_FLAGS_NONE;
2306
2307 pClientState->POD.enmDir = SHCLTRANSFERDIR_UNKNOWN;
2308 pClientState->POD.uFormat = VBOX_SHCL_FMT_NONE;
2309 pClientState->POD.cbToReadWriteTotal = 0;
2310 pClientState->POD.cbReadWritten = 0;
2311
2312#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2313 pClientState->Transfers.enmTransferDir = SHCLTRANSFERDIR_UNKNOWN;
2314#endif
2315}
2316
2317/*
2318 * We differentiate between a function handler for the guest and one for the host.
2319 */
2320static DECLCALLBACK(int) svcHostCall(void *,
2321 uint32_t u32Function,
2322 uint32_t cParms,
2323 VBOXHGCMSVCPARM paParms[])
2324{
2325 int rc = VINF_SUCCESS;
2326
2327 LogFlowFunc(("u32Function=%RU32 (%s), cParms=%RU32, paParms=%p\n",
2328 u32Function, ShClHostFunctionToStr(u32Function), cParms, paParms));
2329
2330 switch (u32Function)
2331 {
2332 case VBOX_SHCL_HOST_FN_SET_MODE:
2333 {
2334 if (cParms != 1)
2335 {
2336 rc = VERR_INVALID_PARAMETER;
2337 }
2338 else
2339 {
2340 uint32_t u32Mode = VBOX_SHCL_MODE_OFF;
2341
2342 rc = HGCMSvcGetU32(&paParms[0], &u32Mode);
2343 if (RT_SUCCESS(rc))
2344 rc = shClSvcModeSet(u32Mode);
2345 }
2346
2347 break;
2348 }
2349
2350#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2351 case VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE:
2352 {
2353 if (cParms != 1)
2354 {
2355 rc = VERR_INVALID_PARAMETER;
2356 }
2357 else
2358 {
2359 uint32_t fTransferMode;
2360 rc = HGCMSvcGetU32(&paParms[0], &fTransferMode);
2361 if (RT_SUCCESS(rc))
2362 rc = shClSvcTransferModeSet(fTransferMode);
2363 }
2364 break;
2365 }
2366#endif
2367 case VBOX_SHCL_HOST_FN_SET_HEADLESS:
2368 {
2369 if (cParms != 1)
2370 {
2371 rc = VERR_INVALID_PARAMETER;
2372 }
2373 else
2374 {
2375 uint32_t uHeadless;
2376 rc = HGCMSvcGetU32(&paParms[0], &uHeadless);
2377 if (RT_SUCCESS(rc))
2378 {
2379 g_fHeadless = RT_BOOL(uHeadless);
2380 LogRel(("Shared Clipboard: Service running in %s mode\n", g_fHeadless ? "headless" : "normal"));
2381 }
2382 }
2383 break;
2384 }
2385
2386 default:
2387 {
2388#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2389 rc = shClSvcTransferHostHandler(u32Function, cParms, paParms);
2390#else
2391 rc = VERR_NOT_IMPLEMENTED;
2392#endif
2393 break;
2394 }
2395 }
2396
2397 LogFlowFuncLeaveRC(rc);
2398 return rc;
2399}
2400
2401#ifndef UNIT_TEST
2402
2403/**
2404 * SSM descriptor table for the SHCLCLIENTLEGACYCID structure.
2405 *
2406 * @note Saving the ListEntry attribute is not necessary, as this gets used on runtime only.
2407 */
2408static SSMFIELD const s_aShClSSMClientLegacyCID[] =
2409{
2410 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uCID),
2411 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, enmType),
2412 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uFormat),
2413 SSMFIELD_ENTRY_TERM()
2414};
2415
2416/**
2417 * SSM descriptor table for the SHCLCLIENTSTATE structure.
2418 *
2419 * @note Saving the session ID not necessary, as they're not persistent across
2420 * state save/restore.
2421 */
2422static SSMFIELD const s_aShClSSMClientState[] =
2423{
2424 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
2425 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
2426 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
2427 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
2428 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fFlags),
2429 SSMFIELD_ENTRY_TERM()
2430};
2431
2432/**
2433 * VBox 6.1 Beta 1 version of s_aShClSSMClientState (no flags).
2434 */
2435static SSMFIELD const s_aShClSSMClientState61B1[] =
2436{
2437 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
2438 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
2439 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
2440 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
2441 SSMFIELD_ENTRY_TERM()
2442};
2443
2444/**
2445 * SSM descriptor table for the SHCLCLIENTPODSTATE structure.
2446 */
2447static SSMFIELD const s_aShClSSMClientPODState[] =
2448{
2449 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, enmDir),
2450 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, uFormat),
2451 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbToReadWriteTotal),
2452 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbReadWritten),
2453 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, tsLastReadWrittenMs),
2454 SSMFIELD_ENTRY_TERM()
2455};
2456
2457/**
2458 * SSM descriptor table for the SHCLCLIENTURISTATE structure.
2459 */
2460static SSMFIELD const s_aShClSSMClientTransferState[] =
2461{
2462 SSMFIELD_ENTRY(SHCLCLIENTTRANSFERSTATE, enmTransferDir),
2463 SSMFIELD_ENTRY_TERM()
2464};
2465
2466/**
2467 * SSM descriptor table for the header of the SHCLCLIENTMSG structure.
2468 * The actual message parameters will be serialized separately.
2469 */
2470static SSMFIELD const s_aShClSSMClientMsgHdr[] =
2471{
2472 SSMFIELD_ENTRY(SHCLCLIENTMSG, idMsg),
2473 SSMFIELD_ENTRY(SHCLCLIENTMSG, cParms),
2474 SSMFIELD_ENTRY_TERM()
2475};
2476
2477/**
2478 * SSM descriptor table for what used to be the VBOXSHCLMSGCTX structure but is
2479 * now part of SHCLCLIENTMSG.
2480 */
2481static SSMFIELD const s_aShClSSMClientMsgCtx[] =
2482{
2483 SSMFIELD_ENTRY(SHCLCLIENTMSG, idCtx),
2484 SSMFIELD_ENTRY_TERM()
2485};
2486#endif /* !UNIT_TEST */
2487
2488static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM)
2489{
2490 LogFlowFuncEnter();
2491
2492#ifndef UNIT_TEST
2493 /*
2494 * When the state will be restored, pending requests will be reissued
2495 * by VMMDev. The service therefore must save state as if there were no
2496 * pending request.
2497 * Pending requests, if any, will be completed in svcDisconnect.
2498 */
2499 RT_NOREF(u32ClientID);
2500 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
2501
2502 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2503 AssertPtr(pClient);
2504
2505 /* Write Shared Clipboard saved state version. */
2506 pVMM->pfnSSMR3PutU32(pSSM, VBOX_SHCL_SAVED_STATE_VER_CURRENT);
2507
2508 int rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
2509 AssertRCReturn(rc, rc);
2510
2511 rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /*fFlags*/, &s_aShClSSMClientPODState[0], NULL);
2512 AssertRCReturn(rc, rc);
2513
2514 rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientTransferState[0], NULL);
2515 AssertRCReturn(rc, rc);
2516
2517 /* Serialize the client's internal message queue. */
2518 rc = pVMM->pfnSSMR3PutU64(pSSM, pClient->cMsgAllocated);
2519 AssertRCReturn(rc, rc);
2520
2521 PSHCLCLIENTMSG pMsg;
2522 RTListForEach(&pClient->MsgQueue, pMsg, SHCLCLIENTMSG, ListEntry)
2523 {
2524 pVMM->pfnSSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
2525 pVMM->pfnSSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
2526
2527 for (uint32_t iParm = 0; iParm < pMsg->cParms; iParm++)
2528 HGCMSvcSSMR3Put(&pMsg->aParms[iParm], pSSM, pVMM);
2529 }
2530
2531 rc = pVMM->pfnSSMR3PutU64(pSSM, pClient->Legacy.cCID);
2532 AssertRCReturn(rc, rc);
2533
2534 PSHCLCLIENTLEGACYCID pCID;
2535 RTListForEach(&pClient->Legacy.lstCID, pCID, SHCLCLIENTLEGACYCID, Node)
2536 {
2537 rc = pVMM->pfnSSMR3PutStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /*fFlags*/, &s_aShClSSMClientLegacyCID[0], NULL);
2538 AssertRCReturn(rc, rc);
2539 }
2540#else /* UNIT_TEST */
2541 RT_NOREF(u32ClientID, pvClient, pSSM, pVMM);
2542#endif /* UNIT_TEST */
2543 return VINF_SUCCESS;
2544}
2545
2546#ifndef UNIT_TEST
2547static int svcLoadStateV0(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM, uint32_t uVersion)
2548{
2549 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
2550
2551 uint32_t uMarker;
2552 int rc = pVMM->pfnSSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
2553 AssertRC(rc);
2554 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
2555
2556 rc = pVMM->pfnSSMR3Skip(pSSM, sizeof(uint32_t)); /* Client ID */
2557 AssertRCReturn(rc, rc);
2558
2559 bool fValue;
2560 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgQuit */
2561 AssertRCReturn(rc, rc);
2562
2563 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgReadData */
2564 AssertRCReturn(rc, rc);
2565
2566 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgFormats */
2567 AssertRCReturn(rc, rc);
2568
2569 uint32_t fFormats;
2570 rc = pVMM->pfnSSMR3GetU32(pSSM, &fFormats); /* u32RequestedFormat */
2571 AssertRCReturn(rc, rc);
2572
2573 rc = pVMM->pfnSSMR3GetU32(pSSM, &uMarker); /* End marker. */
2574 AssertRCReturn(rc, rc);
2575 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
2576
2577 return VINF_SUCCESS;
2578}
2579#endif /* UNIT_TEST */
2580
2581static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient,
2582 PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM, uint32_t uVersion)
2583{
2584 LogFlowFuncEnter();
2585
2586#ifndef UNIT_TEST
2587
2588 RT_NOREF(u32ClientID, uVersion);
2589
2590 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2591 AssertPtr(pClient);
2592
2593 /* Restore the client data. */
2594 uint32_t lenOrVer;
2595 int rc = pVMM->pfnSSMR3GetU32(pSSM, &lenOrVer);
2596 AssertRCReturn(rc, rc);
2597
2598 LogFunc(("u32ClientID=%RU32, lenOrVer=%#RX64\n", u32ClientID, lenOrVer));
2599
2600 if (lenOrVer == VBOX_SHCL_SAVED_STATE_VER_3_1)
2601 return svcLoadStateV0(u32ClientID, pvClient, pSSM, pVMM, uVersion);
2602
2603 if ( lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1B2
2604 && lenOrVer <= VBOX_SHCL_SAVED_STATE_VER_CURRENT)
2605 {
2606 if (lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1RC1)
2607 {
2608 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */,
2609 &s_aShClSSMClientState[0], NULL);
2610 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /* fFlags */,
2611 &s_aShClSSMClientPODState[0], NULL);
2612 }
2613 else
2614 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */,
2615 &s_aShClSSMClientState61B1[0], NULL);
2616 rc = pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /* fFlags */,
2617 &s_aShClSSMClientTransferState[0], NULL);
2618 AssertRCReturn(rc, rc);
2619
2620 /* Load the client's internal message queue. */
2621 uint64_t cMsgs;
2622 rc = pVMM->pfnSSMR3GetU64(pSSM, &cMsgs);
2623 AssertRCReturn(rc, rc);
2624 AssertLogRelMsgReturn(cMsgs < _16K, ("Too many messages: %u (%x)\n", cMsgs, cMsgs), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2625
2626 for (uint64_t i = 0; i < cMsgs; i++)
2627 {
2628 union
2629 {
2630 SHCLCLIENTMSG Msg;
2631 uint8_t abPadding[RT_UOFFSETOF(SHCLCLIENTMSG, aParms) + sizeof(VBOXHGCMSVCPARM) * 2];
2632 } u;
2633
2634 pVMM->pfnSSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/,
2635 &s_aShClSSMClientMsgHdr[0], NULL);
2636 rc = pVMM->pfnSSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/,
2637 &s_aShClSSMClientMsgCtx[0], NULL);
2638 AssertRCReturn(rc, rc);
2639
2640 AssertLogRelMsgReturn(u.Msg.cParms <= VMMDEV_MAX_HGCM_PARMS,
2641 ("Too many HGCM message parameters: %u (%#x)\n", u.Msg.cParms, u.Msg.cParms),
2642 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2643
2644 PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient, u.Msg.idMsg, u.Msg.cParms);
2645 AssertReturn(pMsg, VERR_NO_MEMORY);
2646 pMsg->idCtx = u.Msg.idCtx;
2647
2648 for (uint32_t p = 0; p < pMsg->cParms; p++)
2649 {
2650 rc = HGCMSvcSSMR3Get(&pMsg->aParms[p], pSSM, pVMM);
2651 AssertRCReturnStmt(rc, shClSvcMsgFree(pClient, pMsg), rc);
2652 }
2653
2654 RTCritSectEnter(&pClient->CritSect);
2655 shClSvcMsgAdd(pClient, pMsg, true /* fAppend */);
2656 RTCritSectLeave(&pClient->CritSect);
2657 }
2658
2659 if (lenOrVer >= VBOX_SHCL_SAVED_STATE_LEGACY_CID)
2660 {
2661 uint64_t cCID;
2662 rc = pVMM->pfnSSMR3GetU64(pSSM, &cCID);
2663 AssertRCReturn(rc, rc);
2664 AssertLogRelMsgReturn(cCID < _16K, ("Too many context IDs: %u (%x)\n", cCID, cCID), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2665
2666 for (uint64_t i = 0; i < cCID; i++)
2667 {
2668 PSHCLCLIENTLEGACYCID pCID = (PSHCLCLIENTLEGACYCID)RTMemAlloc(sizeof(SHCLCLIENTLEGACYCID));
2669 AssertPtrReturn(pCID, VERR_NO_MEMORY);
2670
2671 pVMM->pfnSSMR3GetStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /* fFlags */,
2672 &s_aShClSSMClientLegacyCID[0], NULL);
2673 RTListAppend(&pClient->Legacy.lstCID, &pCID->Node);
2674 }
2675 }
2676 }
2677 else
2678 {
2679 LogRel(("Shared Clipboard: Unsupported saved state version (%#x)\n", lenOrVer));
2680 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
2681 }
2682
2683 /* Actual host data are to be reported to guest (SYNC). */
2684 ShClBackendSync(&g_ShClBackend, pClient);
2685
2686#else /* UNIT_TEST */
2687 RT_NOREF(u32ClientID, pvClient, pSSM, pVMM, uVersion);
2688#endif /* UNIT_TEST */
2689 return VINF_SUCCESS;
2690}
2691
2692static DECLCALLBACK(int) extCallback(uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
2693{
2694 RT_NOREF(pvData, cbData);
2695
2696 LogFlowFunc(("u32Function=%RU32\n", u32Function));
2697
2698 int rc = VINF_SUCCESS;
2699
2700 /* Figure out if the client in charge for the service extension still is connected. */
2701 ClipboardClientMap::const_iterator itClient = g_mapClients.find(g_ExtState.uClientID);
2702 if (itClient != g_mapClients.end())
2703 {
2704 PSHCLCLIENT pClient = itClient->second;
2705 AssertPtr(pClient);
2706
2707 switch (u32Function)
2708 {
2709 /* The service extension announces formats to the guest. */
2710 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
2711 {
2712 LogFlowFunc(("VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE: g_ExtState.fReadingData=%RTbool\n", g_ExtState.fReadingData));
2713 if (!g_ExtState.fReadingData)
2714 rc = ShClSvcHostReportFormats(pClient, u32Format);
2715 else
2716 {
2717 g_ExtState.fDelayedAnnouncement = true;
2718 g_ExtState.fDelayedFormats = u32Format;
2719 rc = VINF_SUCCESS;
2720 }
2721 break;
2722 }
2723
2724 /* The service extension wants read data from the guest. */
2725 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
2726 rc = ShClSvcGuestDataRequest(pClient, u32Format, NULL /* pidEvent */);
2727 break;
2728
2729 default:
2730 /* Just skip other messages. */
2731 break;
2732 }
2733 }
2734 else
2735 rc = VERR_NOT_FOUND;
2736
2737 LogFlowFuncLeaveRC(rc);
2738 return rc;
2739}
2740
2741static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
2742{
2743 LogFlowFunc(("pfnExtension=%p\n", pfnExtension));
2744
2745 SHCLEXTPARMS parms;
2746 RT_ZERO(parms);
2747
2748 /*
2749 * Reference counting for service extension registration is done a few
2750 * layers up (in ConsoleVRDPServer::ClipboardCreate()).
2751 */
2752
2753 int rc = RTCritSectEnter(&g_CritSect);
2754 AssertLogRelRCReturn(rc, rc);
2755
2756 if (pfnExtension)
2757 {
2758 /* Install extension. */
2759 g_ExtState.pfnExtension = pfnExtension;
2760 g_ExtState.pvExtension = pvExtension;
2761
2762 parms.u.pfnCallback = extCallback;
2763 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
2764
2765 LogRel2(("Shared Clipboard: registered service extension\n"));
2766 }
2767 else
2768 {
2769 if (g_ExtState.pfnExtension)
2770 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
2771
2772 /* Uninstall extension. */
2773 g_ExtState.pvExtension = NULL;
2774 g_ExtState.pfnExtension = NULL;
2775
2776 LogRel2(("Shared Clipboard: de-registered service extension\n"));
2777 }
2778
2779 RTCritSectLeave(&g_CritSect);
2780
2781 return VINF_SUCCESS;
2782}
2783
2784extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
2785{
2786 int rc = VINF_SUCCESS;
2787
2788 LogFlowFunc(("pTable=%p\n", pTable));
2789
2790 if (!RT_VALID_PTR(pTable))
2791 {
2792 rc = VERR_INVALID_PARAMETER;
2793 }
2794 else
2795 {
2796 LogFunc(("pTable->cbSize = %d, ptable->u32Version = 0x%08X\n", pTable->cbSize, pTable->u32Version));
2797
2798 if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
2799 || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
2800 {
2801 rc = VERR_VERSION_MISMATCH;
2802 }
2803 else
2804 {
2805 g_pHelpers = pTable->pHelpers;
2806
2807 pTable->cbClient = sizeof(SHCLCLIENT);
2808
2809 /* Map legacy clients to root. */
2810 pTable->idxLegacyClientCategory = HGCM_CLIENT_CATEGORY_ROOT;
2811
2812 /* Limit the number of clients to 128 in each category (should be enough),
2813 but set kernel clients to 1. */
2814 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
2815 pTable->acMaxClients[i] = 128;
2816 pTable->acMaxClients[HGCM_CLIENT_CATEGORY_KERNEL] = 1;
2817
2818 /* Only 16 pending calls per client (1 should be enough). */
2819 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
2820 pTable->acMaxCallsPerClient[i] = 16;
2821
2822 pTable->pfnUnload = svcUnload;
2823 pTable->pfnConnect = svcConnect;
2824 pTable->pfnDisconnect = svcDisconnect;
2825 pTable->pfnCall = svcCall;
2826 pTable->pfnHostCall = svcHostCall;
2827 pTable->pfnSaveState = svcSaveState;
2828 pTable->pfnLoadState = svcLoadState;
2829 pTable->pfnRegisterExtension = svcRegisterExtension;
2830 pTable->pfnNotify = NULL;
2831 pTable->pvService = NULL;
2832
2833 /* Service specific initialization. */
2834 rc = svcInit(pTable);
2835 }
2836 }
2837
2838 LogFlowFunc(("Returning %Rrc\n", rc));
2839 return rc;
2840}
Note: See TracBrowser for help on using the repository browser.

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