VirtualBox

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

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

Shared Clipboard/HostService: Only start a transfer on the host side if we supply an URI list. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 102.0 KB
Line 
1/* $Id: VBoxSharedClipboardSvc.cpp 100017 2023-05-30 16:06:53Z 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 && fFormats & VBOX_SHCL_FMT_URI_LIST) /* Only start a transfer if we supply an URI list. */
1488 {
1489 rc = shClSvcTransferStart(pClient, SHCLTRANSFERDIR_TO_REMOTE, SHCLSOURCE_LOCAL,
1490 NULL /* pTransfer */);
1491 if (RT_SUCCESS(rc))
1492 rc = shClSvcSetSource(pClient, SHCLSOURCE_LOCAL);
1493
1494 if (RT_FAILURE(rc))
1495 LogRel(("Shared Clipboard: Initializing host write transfer failed with %Rrc\n", rc));
1496 }
1497 else
1498#endif
1499 rc = VINF_SUCCESS;
1500 }
1501 else
1502 rc = VERR_NO_MEMORY;
1503
1504 if (RT_FAILURE(rc))
1505 LogRel(("Shared Clipboard: Reporting formats %#x to guest failed with %Rrc\n", fFormats, rc));
1506
1507 LogFlowFuncLeaveRC(rc);
1508 return rc;
1509}
1510
1511
1512/**
1513 * Handles the VBOX_SHCL_GUEST_FN_REPORT_FORMATS message from the guest.
1514 */
1515static int shClSvcClientReportFormats(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1516{
1517 /*
1518 * Check if the service mode allows this operation and whether the guest is
1519 * supposed to be reading from the host.
1520 */
1521 uint32_t uMode = ShClSvcGetMode();
1522 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1523 || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
1524 { /* likely */ }
1525 else
1526 return VERR_ACCESS_DENIED;
1527
1528 /*
1529 * Digest parameters.
1530 */
1531 ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS
1532 || ( cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B
1533 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
1534 VERR_WRONG_PARAMETER_COUNT);
1535
1536 uintptr_t iParm = 0;
1537 if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
1538 {
1539 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1540 /* no defined value, so just ignore it */
1541 iParm++;
1542 }
1543 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1544 uint32_t const fFormats = paParms[iParm].u.uint32;
1545 iParm++;
1546 if (cParms == VBOX_SHCL_CPARMS_REPORT_FORMATS_61B)
1547 {
1548 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1549 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1550 iParm++;
1551 }
1552 Assert(iParm == cParms);
1553
1554 /*
1555 * Report the formats.
1556 *
1557 * We ignore empty reports if the guest isn't the clipboard owner, this
1558 * prevents a freshly booted guest with an empty clibpoard from clearing
1559 * the host clipboard on startup. Likewise, when a guest shutdown it will
1560 * typically issue an empty report in case it's the owner, we don't want
1561 * that to clear host content either.
1562 */
1563 int rc;
1564 if (!fFormats && pClient->State.enmSource != SHCLSOURCE_REMOTE)
1565 rc = VINF_SUCCESS;
1566 else
1567 {
1568 rc = shClSvcSetSource(pClient, SHCLSOURCE_REMOTE);
1569 if (RT_SUCCESS(rc))
1570 {
1571 rc = RTCritSectEnter(&g_CritSect);
1572 if (RT_SUCCESS(rc))
1573 {
1574 if (g_ExtState.pfnExtension)
1575 {
1576 SHCLEXTPARMS parms;
1577 RT_ZERO(parms);
1578 parms.uFormat = fFormats;
1579
1580 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof(parms));
1581 }
1582 else
1583 {
1584#ifdef LOG_ENABLED
1585 char *pszFmts = ShClFormatsToStrA(fFormats);
1586 if (pszFmts)
1587 {
1588 LogRel2(("Shared Clipboard: Guest reported formats '%s' to host\n", pszFmts));
1589 RTStrFree(pszFmts);
1590 }
1591#endif
1592 rc = ShClBackendReportFormats(&g_ShClBackend, pClient, fFormats);
1593 if (RT_FAILURE(rc))
1594 LogRel(("Shared Clipboard: Reporting guest clipboard formats to the host failed with %Rrc\n", rc));
1595 }
1596
1597 RTCritSectLeave(&g_CritSect);
1598 }
1599 else
1600 LogRel2(("Shared Clipboard: Unable to take internal lock while receiving guest clipboard announcement: %Rrc\n", rc));
1601 }
1602 }
1603
1604 return rc;
1605}
1606
1607/**
1608 * Called when the guest wants to read host clipboard data.
1609 * Handles the VBOX_SHCL_GUEST_FN_DATA_READ message.
1610 *
1611 * @returns VBox status code.
1612 * @retval VINF_BUFFER_OVERFLOW if the guest supplied a smaller buffer than needed in order to read the host clipboard data.
1613 * @param pClient Client that wants to read host clipboard data.
1614 * @param cParms Number of HGCM parameters supplied in \a paParms.
1615 * @param paParms Array of HGCM parameters.
1616 */
1617static int shClSvcClientReadData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1618{
1619 LogFlowFuncEnter();
1620
1621 /*
1622 * Check if the service mode allows this operation and whether the guest is
1623 * supposed to be reading from the host.
1624 */
1625 uint32_t uMode = ShClSvcGetMode();
1626 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1627 || uMode == VBOX_SHCL_MODE_HOST_TO_GUEST)
1628 { /* likely */ }
1629 else
1630 return VERR_ACCESS_DENIED;
1631
1632 /*
1633 * Digest parameters.
1634 *
1635 * We are dragging some legacy here from the 6.1 dev cycle, a 5 parameter
1636 * variant which prepends a 64-bit context ID (RAZ as meaning not defined),
1637 * a 32-bit flag (MBZ, no defined meaning) and switches the last two parameters.
1638 */
1639 ASSERT_GUEST_RETURN( cParms == VBOX_SHCL_CPARMS_DATA_READ
1640 || ( cParms == VBOX_SHCL_CPARMS_DATA_READ_61B
1641 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID)),
1642 VERR_WRONG_PARAMETER_COUNT);
1643
1644 uintptr_t iParm = 0;
1645 SHCLCLIENTCMDCTX cmdCtx;
1646 RT_ZERO(cmdCtx);
1647 if (cParms == VBOX_SHCL_CPARMS_DATA_READ_61B)
1648 {
1649 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1650 /* This has no defined meaning and was never used, however the guest passed stuff, so ignore it and leave idContext=0. */
1651 iParm++;
1652 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1653 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1654 iParm++;
1655 }
1656
1657 SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
1658 uint32_t cbData = 0;
1659 void *pvData = NULL;
1660
1661 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1662 uFormat = paParms[iParm].u.uint32;
1663 iParm++;
1664 if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
1665 {
1666 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1667 pvData = paParms[iParm].u.pointer.addr;
1668 cbData = paParms[iParm].u.pointer.size;
1669 iParm++;
1670 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
1671 iParm++;
1672 }
1673 else
1674 {
1675 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /*cbDataReturned*/
1676 iParm++;
1677 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1678 pvData = paParms[iParm].u.pointer.addr;
1679 cbData = paParms[iParm].u.pointer.size;
1680 iParm++;
1681 }
1682 Assert(iParm == cParms);
1683
1684 /*
1685 * For some reason we need to do this (makes absolutely no sense to bird).
1686 */
1687 /** @todo r=bird: I really don't get why you need the State.POD.uFormat
1688 * member. I'm sure there is a reason. Incomplete code? */
1689 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1690 {
1691 if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
1692 pClient->State.POD.uFormat = uFormat;
1693 }
1694
1695#ifdef LOG_ENABLED
1696 char *pszFmt = ShClFormatsToStrA(uFormat);
1697 AssertPtrReturn(pszFmt, VERR_NO_MEMORY);
1698 LogRel2(("Shared Clipboard: Guest wants to read %RU32 bytes host clipboard data in format '%s'\n", cbData, pszFmt));
1699 RTStrFree(pszFmt);
1700#endif
1701
1702 /*
1703 * Do the reading.
1704 */
1705 uint32_t cbActual = 0;
1706
1707 int rc = RTCritSectEnter(&g_CritSect);
1708 AssertRCReturn(rc, rc);
1709
1710 /* If there is a service extension active, try reading data from it first. */
1711 if (g_ExtState.pfnExtension)
1712 {
1713 SHCLEXTPARMS parms;
1714 RT_ZERO(parms);
1715
1716 parms.uFormat = uFormat;
1717 parms.u.pvData = pvData;
1718 parms.cbData = cbData;
1719
1720 g_ExtState.fReadingData = true;
1721
1722 /* Read clipboard data from the extension. */
1723 rc = g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ, &parms, sizeof(parms));
1724
1725 LogRel2(("Shared Clipboard: Read extension clipboard data (fDelayedAnnouncement=%RTbool, fDelayedFormats=%#x, max %RU32 bytes), got %RU32 bytes: rc=%Rrc\n",
1726 g_ExtState.fDelayedAnnouncement, g_ExtState.fDelayedFormats, cbData, parms.cbData, rc));
1727
1728 /* Did the extension send the clipboard formats yet?
1729 * Otherwise, do this now. */
1730 if (g_ExtState.fDelayedAnnouncement)
1731 {
1732 int rc2 = ShClSvcHostReportFormats(pClient, g_ExtState.fDelayedFormats);
1733 AssertRC(rc2);
1734
1735 g_ExtState.fDelayedAnnouncement = false;
1736 g_ExtState.fDelayedFormats = 0;
1737 }
1738
1739 g_ExtState.fReadingData = false;
1740
1741 if (RT_SUCCESS(rc))
1742 cbActual = parms.cbData;
1743 }
1744 else
1745 {
1746 rc = ShClBackendReadData(&g_ShClBackend, pClient, &cmdCtx, uFormat, pvData, cbData, &cbActual);
1747 if (RT_SUCCESS(rc))
1748 LogRel2(("Shared Clipboard: Read host clipboard data (max %RU32 bytes), got %RU32 bytes\n", cbData, cbActual));
1749 else
1750 LogRel(("Shared Clipboard: Reading host clipboard data failed with %Rrc\n", rc));
1751 }
1752
1753 if (RT_SUCCESS(rc))
1754 {
1755 /* Return the actual size required to fullfil the request. */
1756 if (cParms != VBOX_SHCL_CPARMS_DATA_READ_61B)
1757 HGCMSvcSetU32(&paParms[2], cbActual);
1758 else
1759 HGCMSvcSetU32(&paParms[3], cbActual);
1760
1761 /* If the data to return exceeds the buffer the guest supplies, tell it (and let it try again). */
1762 if (cbActual >= cbData)
1763 rc = VINF_BUFFER_OVERFLOW;
1764 }
1765
1766 RTCritSectLeave(&g_CritSect);
1767
1768 LogFlowFuncLeaveRC(rc);
1769 return rc;
1770}
1771
1772/**
1773 * Called when the guest writes clipboard data to the host.
1774 * Handles the VBOX_SHCL_GUEST_FN_DATA_WRITE message.
1775 *
1776 * @returns VBox status code.
1777 * @param pClient Client that wants to read host clipboard data.
1778 * @param cParms Number of HGCM parameters supplied in \a paParms.
1779 * @param paParms Array of HGCM parameters.
1780 */
1781static int shClSvcClientWriteData(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1782{
1783 LogFlowFuncEnter();
1784
1785 /*
1786 * Check if the service mode allows this operation and whether the guest is
1787 * supposed to be reading from the host.
1788 */
1789 uint32_t uMode = ShClSvcGetMode();
1790 if ( uMode == VBOX_SHCL_MODE_BIDIRECTIONAL
1791 || uMode == VBOX_SHCL_MODE_GUEST_TO_HOST)
1792 { /* likely */ }
1793 else
1794 return VERR_ACCESS_DENIED;
1795
1796 const bool fReportsContextID = RT_BOOL(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID);
1797
1798 /*
1799 * Digest parameters.
1800 *
1801 * There are 3 different format here, formatunately no parameters have been
1802 * switch around so it's plain sailing compared to the DATA_READ message.
1803 */
1804 ASSERT_GUEST_RETURN(fReportsContextID
1805 ? cParms == VBOX_SHCL_CPARMS_DATA_WRITE || cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B
1806 : cParms == VBOX_SHCL_CPARMS_DATA_WRITE_OLD,
1807 VERR_WRONG_PARAMETER_COUNT);
1808
1809 uintptr_t iParm = 0;
1810 SHCLCLIENTCMDCTX cmdCtx;
1811 RT_ZERO(cmdCtx);
1812 if (cParms > VBOX_SHCL_CPARMS_DATA_WRITE_OLD)
1813 {
1814 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_WRONG_PARAMETER_TYPE);
1815 cmdCtx.uContextID = paParms[iParm].u.uint64;
1816 iParm++;
1817 }
1818 else
1819 {
1820 /* Older Guest Additions (< 6.1) did not supply a context ID.
1821 * We dig it out from our saved context ID list then a bit down below. */
1822 }
1823
1824 if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
1825 {
1826 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE);
1827 ASSERT_GUEST_RETURN(paParms[iParm].u.uint32 == 0, VERR_INVALID_FLAGS);
1828 iParm++;
1829 }
1830
1831 SHCLFORMAT uFormat = VBOX_SHCL_FMT_NONE;
1832 uint32_t cbData = 0;
1833 void *pvData = NULL;
1834
1835 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* Format bit. */
1836 uFormat = paParms[iParm].u.uint32;
1837 iParm++;
1838 if (cParms == VBOX_SHCL_CPARMS_DATA_WRITE_61B)
1839 {
1840 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_WRONG_PARAMETER_TYPE); /* "cbData" - duplicates buffer size. */
1841 iParm++;
1842 }
1843 ASSERT_GUEST_RETURN(paParms[iParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_WRONG_PARAMETER_TYPE); /* Data buffer */
1844 pvData = paParms[iParm].u.pointer.addr;
1845 cbData = paParms[iParm].u.pointer.size;
1846 iParm++;
1847 Assert(iParm == cParms);
1848
1849 /*
1850 * Handle / check context ID.
1851 */
1852 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. */
1853 {
1854 PSHCLCLIENTLEGACYCID pCID = NULL;
1855 PSHCLCLIENTLEGACYCID pCIDIter;
1856 RTListForEach(&pClient->Legacy.lstCID, pCIDIter, SHCLCLIENTLEGACYCID, Node) /* Slow, but does the job for now. */
1857 {
1858 if (pCIDIter->uFormat == uFormat)
1859 {
1860 pCID = pCIDIter;
1861 break;
1862 }
1863 }
1864
1865 ASSERT_GUEST_MSG_RETURN(pCID != NULL, ("Context ID for format %#x not found\n", uFormat), VERR_INVALID_CONTEXT);
1866 cmdCtx.uContextID = pCID->uCID;
1867
1868 /* Not needed anymore; clean up. */
1869 Assert(pClient->Legacy.cCID);
1870 pClient->Legacy.cCID--;
1871 RTListNodeRemove(&pCID->Node);
1872 RTMemFree(pCID);
1873 }
1874
1875 uint64_t const idCtxExpected = VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID, pClient->EventSrc.uID,
1876 VBOX_SHCL_CONTEXTID_GET_EVENT(cmdCtx.uContextID));
1877 ASSERT_GUEST_MSG_RETURN(cmdCtx.uContextID == idCtxExpected,
1878 ("Wrong context ID: %#RX64, expected %#RX64\n", cmdCtx.uContextID, idCtxExpected),
1879 VERR_INVALID_CONTEXT);
1880
1881 /*
1882 * For some reason we need to do this (makes absolutely no sense to bird).
1883 */
1884 /** @todo r=bird: I really don't get why you need the State.POD.uFormat
1885 * member. I'm sure there is a reason. Incomplete code? */
1886 if (!(pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID))
1887 {
1888 if (pClient->State.POD.uFormat == VBOX_SHCL_FMT_NONE)
1889 pClient->State.POD.uFormat = uFormat;
1890 }
1891
1892#ifdef LOG_ENABLED
1893 char *pszFmt = ShClFormatsToStrA(uFormat);
1894 if (pszFmt)
1895 {
1896 LogRel2(("Shared Clipboard: Guest writes %RU32 bytes clipboard data in format '%s' to host\n", cbData, pszFmt));
1897 RTStrFree(pszFmt);
1898 }
1899#endif
1900
1901 /*
1902 * Write the data to the active host side clipboard.
1903 */
1904 int rc = RTCritSectEnter(&g_CritSect);
1905 AssertRCReturn(rc, rc);
1906
1907 if (g_ExtState.pfnExtension)
1908 {
1909 SHCLEXTPARMS parms;
1910 RT_ZERO(parms);
1911 parms.uFormat = uFormat;
1912 parms.u.pvData = pvData;
1913 parms.cbData = cbData;
1914
1915 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof(parms));
1916 rc = VINF_SUCCESS;
1917 }
1918 else
1919 {
1920 /* Let the backend implementation know. */
1921 rc = ShClBackendWriteData(&g_ShClBackend, pClient, &cmdCtx, uFormat, pvData, cbData);
1922 if (RT_FAILURE(rc))
1923 LogRel(("Shared Clipboard: Writing guest clipboard data to the host failed with %Rrc\n", rc));
1924
1925 int rc2; /* Don't return internals back to the guest. */
1926 rc2 = ShClSvcGuestDataSignal(pClient, &cmdCtx, uFormat, pvData, cbData); /* To complete pending events, if any. */
1927 if (RT_FAILURE(rc2))
1928 LogRel(("Shared Clipboard: Signalling host about guest clipboard data failed with %Rrc\n", rc2));
1929 AssertRC(rc2);
1930 }
1931
1932 RTCritSectLeave(&g_CritSect);
1933
1934 LogFlowFuncLeaveRC(rc);
1935 return rc;
1936}
1937
1938/**
1939 * Gets an error from HGCM service parameters.
1940 *
1941 * @returns VBox status code.
1942 * @param cParms Number of HGCM parameters supplied in \a paParms.
1943 * @param paParms Array of HGCM parameters.
1944 * @param pRc Where to store the received error code.
1945 */
1946static int shClSvcClientError(uint32_t cParms, VBOXHGCMSVCPARM paParms[], int *pRc)
1947{
1948 AssertPtrReturn(paParms, VERR_INVALID_PARAMETER);
1949 AssertPtrReturn(pRc, VERR_INVALID_PARAMETER);
1950
1951 int rc;
1952
1953 if (cParms == VBOX_SHCL_CPARMS_ERROR)
1954 {
1955 rc = HGCMSvcGetU32(&paParms[1], (uint32_t *)pRc); /** @todo int vs. uint32_t !!! */
1956 }
1957 else
1958 rc = VERR_INVALID_PARAMETER;
1959
1960 LogFlowFuncLeaveRC(rc);
1961 return rc;
1962}
1963
1964/**
1965 * Sets the transfer source type of a Shared Clipboard client.
1966 *
1967 * @returns VBox status code.
1968 * @param pClient Client to set transfer source type for.
1969 * @param enmSource Source type to set.
1970 */
1971int shClSvcSetSource(PSHCLCLIENT pClient, SHCLSOURCE enmSource)
1972{
1973 if (!pClient) /* If no client connected (anymore), bail out. */
1974 return VINF_SUCCESS;
1975
1976 int rc = VINF_SUCCESS;
1977
1978 if (ShClSvcLock())
1979 {
1980 pClient->State.enmSource = enmSource;
1981
1982 LogFlowFunc(("Source of client %RU32 is now %RU32\n", pClient->State.uClientID, pClient->State.enmSource));
1983
1984 ShClSvcUnlock();
1985 }
1986
1987 LogFlowFuncLeaveRC(rc);
1988 return rc;
1989}
1990
1991static int svcInit(VBOXHGCMSVCFNTABLE *pTable)
1992{
1993 int rc = RTCritSectInit(&g_CritSect);
1994
1995 if (RT_SUCCESS(rc))
1996 {
1997 shClSvcModeSet(VBOX_SHCL_MODE_OFF);
1998
1999 rc = ShClBackendInit(ShClSvcGetBackend(), pTable);
2000
2001 /* Clean up on failure, because 'svnUnload' will not be called
2002 * if the 'svcInit' returns an error.
2003 */
2004 if (RT_FAILURE(rc))
2005 {
2006 RTCritSectDelete(&g_CritSect);
2007 }
2008 }
2009
2010 return rc;
2011}
2012
2013static DECLCALLBACK(int) svcUnload(void *)
2014{
2015 LogFlowFuncEnter();
2016
2017 ShClBackendDestroy(ShClSvcGetBackend());
2018
2019 RTCritSectDelete(&g_CritSect);
2020
2021 return VINF_SUCCESS;
2022}
2023
2024static DECLCALLBACK(int) svcDisconnect(void *, uint32_t u32ClientID, void *pvClient)
2025{
2026 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
2027
2028 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2029 AssertPtr(pClient);
2030
2031 /* In order to communicate with guest service, HGCM VRDP clipboard extension
2032 * needs to know its connection client ID. Currently, in svcConnect() we always
2033 * cache ID of the first ever connected client. When client disconnects,
2034 * we need to forget its ID and let svcConnect() to pick up the next ID when a new
2035 * connection will be requested by guest service (see #10115). */
2036 if (g_ExtState.uClientID == u32ClientID)
2037 {
2038 g_ExtState.uClientID = 0;
2039 }
2040
2041#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2042 shClSvcClientTransfersReset(pClient);
2043#endif
2044
2045 ShClBackendDisconnect(&g_ShClBackend, pClient);
2046
2047 shClSvcClientDestroy(pClient);
2048
2049 return VINF_SUCCESS;
2050}
2051
2052static DECLCALLBACK(int) svcConnect(void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
2053{
2054 RT_NOREF(fRequestor, fRestoring);
2055
2056 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2057 AssertPtr(pvClient);
2058
2059 int rc = shClSvcClientInit(pClient, u32ClientID);
2060 if (RT_SUCCESS(rc))
2061 {
2062 /* Assign weak pointer to client map. */
2063 /** @todo r=bird: The g_mapClients is only there for looking up
2064 * g_ExtState.uClientID (unserialized btw), so why not use store the
2065 * pClient value directly in g_ExtState instead of the ID? It cannot
2066 * crash any worse that racing map insertion/removal. */
2067 g_mapClients[u32ClientID] = pClient; /** @todo Handle OOM / collisions? */
2068 rc = ShClBackendConnect(&g_ShClBackend, pClient, ShClSvcGetHeadless());
2069 if (RT_SUCCESS(rc))
2070 {
2071 /* Sync the host clipboard content with the client. */
2072 rc = ShClBackendSync(&g_ShClBackend, pClient);
2073 if (RT_SUCCESS(rc))
2074 {
2075 /* For now we ASSUME that the first client that connects is in charge for
2076 communicating with the service extension. */
2077 /** @todo This isn't optimal, but only the guest really knows which client is in
2078 * focus on the console. See @bugref{10115} for details. */
2079 if (g_ExtState.uClientID == 0)
2080 g_ExtState.uClientID = u32ClientID;
2081
2082 /* The sync could return VINF_NO_CHANGE if nothing has changed on the host, but
2083 older Guest Additions didn't use RT_SUCCESS to but == VINF_SUCCESS to check for
2084 success. So just return VINF_SUCCESS here to not break older Guest Additions. */
2085 LogFunc(("Successfully connected client %#x%s\n",
2086 u32ClientID, g_ExtState.uClientID == u32ClientID ? " - Use by ExtState too" : ""));
2087 return VINF_SUCCESS;
2088 }
2089
2090 LogFunc(("ShClBackendSync failed: %Rrc\n", rc));
2091 ShClBackendDisconnect(&g_ShClBackend, pClient);
2092 }
2093 else
2094 LogFunc(("ShClBackendConnect failed: %Rrc\n", rc));
2095 shClSvcClientDestroy(pClient);
2096 }
2097 else
2098 LogFunc(("shClSvcClientInit failed: %Rrc\n", rc));
2099 LogFlowFuncLeaveRC(rc);
2100 return rc;
2101}
2102
2103static DECLCALLBACK(void) svcCall(void *,
2104 VBOXHGCMCALLHANDLE callHandle,
2105 uint32_t u32ClientID,
2106 void *pvClient,
2107 uint32_t u32Function,
2108 uint32_t cParms,
2109 VBOXHGCMSVCPARM paParms[],
2110 uint64_t tsArrival)
2111{
2112 RT_NOREF(u32ClientID, pvClient, tsArrival);
2113 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2114 AssertPtr(pClient);
2115
2116#ifdef LOG_ENABLED
2117 Log2Func(("u32ClientID=%RU32, fn=%RU32 (%s), cParms=%RU32, paParms=%p\n",
2118 u32ClientID, u32Function, ShClGuestMsgToStr(u32Function), cParms, paParms));
2119 for (uint32_t i = 0; i < cParms; i++)
2120 {
2121 switch (paParms[i].type)
2122 {
2123 case VBOX_HGCM_SVC_PARM_32BIT:
2124 Log3Func((" paParms[%RU32]: type uint32_t - value %RU32\n", i, paParms[i].u.uint32));
2125 break;
2126 case VBOX_HGCM_SVC_PARM_64BIT:
2127 Log3Func((" paParms[%RU32]: type uint64_t - value %RU64\n", i, paParms[i].u.uint64));
2128 break;
2129 case VBOX_HGCM_SVC_PARM_PTR:
2130 Log3Func((" paParms[%RU32]: type ptr - value 0x%p (%RU32 bytes)\n",
2131 i, paParms[i].u.pointer.addr, paParms[i].u.pointer.size));
2132 break;
2133 case VBOX_HGCM_SVC_PARM_PAGES:
2134 Log3Func((" paParms[%RU32]: type pages - cb=%RU32, cPages=%RU16\n",
2135 i, paParms[i].u.Pages.cb, paParms[i].u.Pages.cPages));
2136 break;
2137 default:
2138 AssertFailed();
2139 }
2140 }
2141 Log2Func(("Client state: fFlags=0x%x, fGuestFeatures0=0x%x, fGuestFeatures1=0x%x\n",
2142 pClient->State.fFlags, pClient->State.fGuestFeatures0, pClient->State.fGuestFeatures1));
2143#endif
2144
2145 int rc;
2146 switch (u32Function)
2147 {
2148 case VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT:
2149 RTCritSectEnter(&pClient->CritSect);
2150 rc = shClSvcClientMsgOldGet(pClient, callHandle, cParms, paParms);
2151 RTCritSectLeave(&pClient->CritSect);
2152 break;
2153
2154 case VBOX_SHCL_GUEST_FN_CONNECT:
2155 LogRel(("Shared Clipboard: 6.1.0 beta or rc Guest Additions detected. Please upgrade!\n"));
2156 rc = VERR_NOT_IMPLEMENTED;
2157 break;
2158
2159 case VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE:
2160 rc = shClSvcClientNegogiateChunkSize(pClient, callHandle, cParms, paParms);
2161 break;
2162
2163 case VBOX_SHCL_GUEST_FN_REPORT_FEATURES:
2164 rc = shClSvcClientReportFeatures(pClient, callHandle, cParms, paParms);
2165 break;
2166
2167 case VBOX_SHCL_GUEST_FN_QUERY_FEATURES:
2168 rc = shClSvcClientQueryFeatures(callHandle, cParms, paParms);
2169 break;
2170
2171 case VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT:
2172 RTCritSectEnter(&pClient->CritSect);
2173 rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, false /*fWait*/);
2174 RTCritSectLeave(&pClient->CritSect);
2175 break;
2176
2177 case VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT:
2178 RTCritSectEnter(&pClient->CritSect);
2179 rc = shClSvcClientMsgPeek(pClient, callHandle, cParms, paParms, true /*fWait*/);
2180 RTCritSectLeave(&pClient->CritSect);
2181 break;
2182
2183 case VBOX_SHCL_GUEST_FN_MSG_GET:
2184 RTCritSectEnter(&pClient->CritSect);
2185 rc = shClSvcClientMsgGet(pClient, callHandle, cParms, paParms);
2186 RTCritSectLeave(&pClient->CritSect);
2187 break;
2188
2189 case VBOX_SHCL_GUEST_FN_MSG_CANCEL:
2190 RTCritSectEnter(&pClient->CritSect);
2191 rc = shClSvcClientMsgCancel(pClient, cParms);
2192 RTCritSectLeave(&pClient->CritSect);
2193 break;
2194
2195 case VBOX_SHCL_GUEST_FN_REPORT_FORMATS:
2196 rc = shClSvcClientReportFormats(pClient, cParms, paParms);
2197 break;
2198
2199 case VBOX_SHCL_GUEST_FN_DATA_READ:
2200 rc = shClSvcClientReadData(pClient, cParms, paParms);
2201 break;
2202
2203 case VBOX_SHCL_GUEST_FN_DATA_WRITE:
2204 rc = shClSvcClientWriteData(pClient, cParms, paParms);
2205 break;
2206
2207 case VBOX_SHCL_GUEST_FN_ERROR:
2208 {
2209 int rcGuest;
2210 rc = shClSvcClientError(cParms,paParms, &rcGuest);
2211 if (RT_SUCCESS(rc))
2212 {
2213 LogRel(("Shared Clipboard: Error reported from guest side: %Rrc\n", rcGuest));
2214
2215 shClSvcClientLock(pClient);
2216
2217 /* Reset message queue. */
2218 shClSvcMsgQueueReset(pClient);
2219
2220#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2221 shClSvcClientTransfersReset(pClient);
2222#endif
2223 shClSvcClientUnlock(pClient);
2224 }
2225 break;
2226 }
2227
2228 default:
2229 {
2230#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2231 if ( u32Function <= VBOX_SHCL_GUEST_FN_LAST
2232 && (pClient->State.fGuestFeatures0 & VBOX_SHCL_GF_0_CONTEXT_ID) )
2233 {
2234 if (g_fTransferMode & VBOX_SHCL_TRANSFER_MODE_F_ENABLED)
2235 rc = shClSvcTransferHandler(pClient, callHandle, u32Function, cParms, paParms, tsArrival);
2236 else
2237 {
2238 LogRel2(("Shared Clipboard: File transfers are disabled for this VM\n"));
2239 rc = VERR_ACCESS_DENIED;
2240 }
2241 }
2242 else
2243#endif
2244 {
2245 LogRel2(("Shared Clipboard: Unknown guest function: %u (%#x)\n", u32Function, u32Function));
2246 rc = VERR_NOT_IMPLEMENTED;
2247 }
2248 break;
2249 }
2250 }
2251
2252 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
2253
2254 if (rc != VINF_HGCM_ASYNC_EXECUTE)
2255 g_pHelpers->pfnCallComplete(callHandle, rc);
2256}
2257
2258/**
2259 * Initializes a Shared Clipboard service's client state.
2260 *
2261 * @returns VBox status code.
2262 * @param pClientState Client state to initialize.
2263 * @param uClientID Client ID (HGCM) to use for this client state.
2264 */
2265int shClSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID)
2266{
2267 LogFlowFuncEnter();
2268
2269 shclSvcClientStateReset(pClientState);
2270
2271 /* Register the client. */
2272 pClientState->uClientID = uClientID;
2273
2274 return VINF_SUCCESS;
2275}
2276
2277/**
2278 * Destroys a Shared Clipboard service's client state.
2279 *
2280 * @returns VBox status code.
2281 * @param pClientState Client state to destroy.
2282 */
2283int shClSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState)
2284{
2285 RT_NOREF(pClientState);
2286
2287 LogFlowFuncEnter();
2288
2289 return VINF_SUCCESS;
2290}
2291
2292/**
2293 * Resets a Shared Clipboard service's client state.
2294 *
2295 * @param pClientState Client state to reset.
2296 */
2297void shclSvcClientStateReset(PSHCLCLIENTSTATE pClientState)
2298{
2299 LogFlowFuncEnter();
2300
2301 pClientState->fGuestFeatures0 = VBOX_SHCL_GF_NONE;
2302 pClientState->fGuestFeatures1 = VBOX_SHCL_GF_NONE;
2303
2304 pClientState->cbChunkSize = VBOX_SHCL_DEFAULT_CHUNK_SIZE; /** @todo Make this configurable. */
2305 pClientState->enmSource = SHCLSOURCE_INVALID;
2306 pClientState->fFlags = SHCLCLIENTSTATE_FLAGS_NONE;
2307
2308 pClientState->POD.enmDir = SHCLTRANSFERDIR_UNKNOWN;
2309 pClientState->POD.uFormat = VBOX_SHCL_FMT_NONE;
2310 pClientState->POD.cbToReadWriteTotal = 0;
2311 pClientState->POD.cbReadWritten = 0;
2312
2313#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2314 pClientState->Transfers.enmTransferDir = SHCLTRANSFERDIR_UNKNOWN;
2315#endif
2316}
2317
2318/*
2319 * We differentiate between a function handler for the guest and one for the host.
2320 */
2321static DECLCALLBACK(int) svcHostCall(void *,
2322 uint32_t u32Function,
2323 uint32_t cParms,
2324 VBOXHGCMSVCPARM paParms[])
2325{
2326 int rc = VINF_SUCCESS;
2327
2328 LogFlowFunc(("u32Function=%RU32 (%s), cParms=%RU32, paParms=%p\n",
2329 u32Function, ShClHostFunctionToStr(u32Function), cParms, paParms));
2330
2331 switch (u32Function)
2332 {
2333 case VBOX_SHCL_HOST_FN_SET_MODE:
2334 {
2335 if (cParms != 1)
2336 {
2337 rc = VERR_INVALID_PARAMETER;
2338 }
2339 else
2340 {
2341 uint32_t u32Mode = VBOX_SHCL_MODE_OFF;
2342
2343 rc = HGCMSvcGetU32(&paParms[0], &u32Mode);
2344 if (RT_SUCCESS(rc))
2345 rc = shClSvcModeSet(u32Mode);
2346 }
2347
2348 break;
2349 }
2350
2351#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2352 case VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE:
2353 {
2354 if (cParms != 1)
2355 {
2356 rc = VERR_INVALID_PARAMETER;
2357 }
2358 else
2359 {
2360 uint32_t fTransferMode;
2361 rc = HGCMSvcGetU32(&paParms[0], &fTransferMode);
2362 if (RT_SUCCESS(rc))
2363 rc = shClSvcTransferModeSet(fTransferMode);
2364 }
2365 break;
2366 }
2367#endif
2368 case VBOX_SHCL_HOST_FN_SET_HEADLESS:
2369 {
2370 if (cParms != 1)
2371 {
2372 rc = VERR_INVALID_PARAMETER;
2373 }
2374 else
2375 {
2376 uint32_t uHeadless;
2377 rc = HGCMSvcGetU32(&paParms[0], &uHeadless);
2378 if (RT_SUCCESS(rc))
2379 {
2380 g_fHeadless = RT_BOOL(uHeadless);
2381 LogRel(("Shared Clipboard: Service running in %s mode\n", g_fHeadless ? "headless" : "normal"));
2382 }
2383 }
2384 break;
2385 }
2386
2387 default:
2388 {
2389#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
2390 rc = shClSvcTransferHostHandler(u32Function, cParms, paParms);
2391#else
2392 rc = VERR_NOT_IMPLEMENTED;
2393#endif
2394 break;
2395 }
2396 }
2397
2398 LogFlowFuncLeaveRC(rc);
2399 return rc;
2400}
2401
2402#ifndef UNIT_TEST
2403
2404/**
2405 * SSM descriptor table for the SHCLCLIENTLEGACYCID structure.
2406 *
2407 * @note Saving the ListEntry attribute is not necessary, as this gets used on runtime only.
2408 */
2409static SSMFIELD const s_aShClSSMClientLegacyCID[] =
2410{
2411 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uCID),
2412 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, enmType),
2413 SSMFIELD_ENTRY(SHCLCLIENTLEGACYCID, uFormat),
2414 SSMFIELD_ENTRY_TERM()
2415};
2416
2417/**
2418 * SSM descriptor table for the SHCLCLIENTSTATE structure.
2419 *
2420 * @note Saving the session ID not necessary, as they're not persistent across
2421 * state save/restore.
2422 */
2423static SSMFIELD const s_aShClSSMClientState[] =
2424{
2425 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
2426 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
2427 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
2428 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
2429 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fFlags),
2430 SSMFIELD_ENTRY_TERM()
2431};
2432
2433/**
2434 * VBox 6.1 Beta 1 version of s_aShClSSMClientState (no flags).
2435 */
2436static SSMFIELD const s_aShClSSMClientState61B1[] =
2437{
2438 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures0),
2439 SSMFIELD_ENTRY(SHCLCLIENTSTATE, fGuestFeatures1),
2440 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
2441 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
2442 SSMFIELD_ENTRY_TERM()
2443};
2444
2445/**
2446 * SSM descriptor table for the SHCLCLIENTPODSTATE structure.
2447 */
2448static SSMFIELD const s_aShClSSMClientPODState[] =
2449{
2450 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, enmDir),
2451 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, uFormat),
2452 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbToReadWriteTotal),
2453 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, cbReadWritten),
2454 SSMFIELD_ENTRY(SHCLCLIENTPODSTATE, tsLastReadWrittenMs),
2455 SSMFIELD_ENTRY_TERM()
2456};
2457
2458/**
2459 * SSM descriptor table for the SHCLCLIENTURISTATE structure.
2460 */
2461static SSMFIELD const s_aShClSSMClientTransferState[] =
2462{
2463 SSMFIELD_ENTRY(SHCLCLIENTTRANSFERSTATE, enmTransferDir),
2464 SSMFIELD_ENTRY_TERM()
2465};
2466
2467/**
2468 * SSM descriptor table for the header of the SHCLCLIENTMSG structure.
2469 * The actual message parameters will be serialized separately.
2470 */
2471static SSMFIELD const s_aShClSSMClientMsgHdr[] =
2472{
2473 SSMFIELD_ENTRY(SHCLCLIENTMSG, idMsg),
2474 SSMFIELD_ENTRY(SHCLCLIENTMSG, cParms),
2475 SSMFIELD_ENTRY_TERM()
2476};
2477
2478/**
2479 * SSM descriptor table for what used to be the VBOXSHCLMSGCTX structure but is
2480 * now part of SHCLCLIENTMSG.
2481 */
2482static SSMFIELD const s_aShClSSMClientMsgCtx[] =
2483{
2484 SSMFIELD_ENTRY(SHCLCLIENTMSG, idCtx),
2485 SSMFIELD_ENTRY_TERM()
2486};
2487#endif /* !UNIT_TEST */
2488
2489static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM)
2490{
2491 LogFlowFuncEnter();
2492
2493#ifndef UNIT_TEST
2494 /*
2495 * When the state will be restored, pending requests will be reissued
2496 * by VMMDev. The service therefore must save state as if there were no
2497 * pending request.
2498 * Pending requests, if any, will be completed in svcDisconnect.
2499 */
2500 RT_NOREF(u32ClientID);
2501 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
2502
2503 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2504 AssertPtr(pClient);
2505
2506 /* Write Shared Clipboard saved state version. */
2507 pVMM->pfnSSMR3PutU32(pSSM, VBOX_SHCL_SAVED_STATE_VER_CURRENT);
2508
2509 int rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
2510 AssertRCReturn(rc, rc);
2511
2512 rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /*fFlags*/, &s_aShClSSMClientPODState[0], NULL);
2513 AssertRCReturn(rc, rc);
2514
2515 rc = pVMM->pfnSSMR3PutStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientTransferState[0], NULL);
2516 AssertRCReturn(rc, rc);
2517
2518 /* Serialize the client's internal message queue. */
2519 rc = pVMM->pfnSSMR3PutU64(pSSM, pClient->cMsgAllocated);
2520 AssertRCReturn(rc, rc);
2521
2522 PSHCLCLIENTMSG pMsg;
2523 RTListForEach(&pClient->MsgQueue, pMsg, SHCLCLIENTMSG, ListEntry)
2524 {
2525 pVMM->pfnSSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
2526 pVMM->pfnSSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
2527
2528 for (uint32_t iParm = 0; iParm < pMsg->cParms; iParm++)
2529 HGCMSvcSSMR3Put(&pMsg->aParms[iParm], pSSM, pVMM);
2530 }
2531
2532 rc = pVMM->pfnSSMR3PutU64(pSSM, pClient->Legacy.cCID);
2533 AssertRCReturn(rc, rc);
2534
2535 PSHCLCLIENTLEGACYCID pCID;
2536 RTListForEach(&pClient->Legacy.lstCID, pCID, SHCLCLIENTLEGACYCID, Node)
2537 {
2538 rc = pVMM->pfnSSMR3PutStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /*fFlags*/, &s_aShClSSMClientLegacyCID[0], NULL);
2539 AssertRCReturn(rc, rc);
2540 }
2541#else /* UNIT_TEST */
2542 RT_NOREF(u32ClientID, pvClient, pSSM, pVMM);
2543#endif /* UNIT_TEST */
2544 return VINF_SUCCESS;
2545}
2546
2547#ifndef UNIT_TEST
2548static int svcLoadStateV0(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM, uint32_t uVersion)
2549{
2550 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
2551
2552 uint32_t uMarker;
2553 int rc = pVMM->pfnSSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
2554 AssertRC(rc);
2555 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
2556
2557 rc = pVMM->pfnSSMR3Skip(pSSM, sizeof(uint32_t)); /* Client ID */
2558 AssertRCReturn(rc, rc);
2559
2560 bool fValue;
2561 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgQuit */
2562 AssertRCReturn(rc, rc);
2563
2564 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgReadData */
2565 AssertRCReturn(rc, rc);
2566
2567 rc = pVMM->pfnSSMR3GetBool(pSSM, &fValue); /* fHostMsgFormats */
2568 AssertRCReturn(rc, rc);
2569
2570 uint32_t fFormats;
2571 rc = pVMM->pfnSSMR3GetU32(pSSM, &fFormats); /* u32RequestedFormat */
2572 AssertRCReturn(rc, rc);
2573
2574 rc = pVMM->pfnSSMR3GetU32(pSSM, &uMarker); /* End marker. */
2575 AssertRCReturn(rc, rc);
2576 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
2577
2578 return VINF_SUCCESS;
2579}
2580#endif /* UNIT_TEST */
2581
2582static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient,
2583 PSSMHANDLE pSSM, PCVMMR3VTABLE pVMM, uint32_t uVersion)
2584{
2585 LogFlowFuncEnter();
2586
2587#ifndef UNIT_TEST
2588
2589 RT_NOREF(u32ClientID, uVersion);
2590
2591 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
2592 AssertPtr(pClient);
2593
2594 /* Restore the client data. */
2595 uint32_t lenOrVer;
2596 int rc = pVMM->pfnSSMR3GetU32(pSSM, &lenOrVer);
2597 AssertRCReturn(rc, rc);
2598
2599 LogFunc(("u32ClientID=%RU32, lenOrVer=%#RX64\n", u32ClientID, lenOrVer));
2600
2601 if (lenOrVer == VBOX_SHCL_SAVED_STATE_VER_3_1)
2602 return svcLoadStateV0(u32ClientID, pvClient, pSSM, pVMM, uVersion);
2603
2604 if ( lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1B2
2605 && lenOrVer <= VBOX_SHCL_SAVED_STATE_VER_CURRENT)
2606 {
2607 if (lenOrVer >= VBOX_SHCL_SAVED_STATE_VER_6_1RC1)
2608 {
2609 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */,
2610 &s_aShClSSMClientState[0], NULL);
2611 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State.POD, sizeof(pClient->State.POD), 0 /* fFlags */,
2612 &s_aShClSSMClientPODState[0], NULL);
2613 }
2614 else
2615 pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /* fFlags */,
2616 &s_aShClSSMClientState61B1[0], NULL);
2617 rc = pVMM->pfnSSMR3GetStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /* fFlags */,
2618 &s_aShClSSMClientTransferState[0], NULL);
2619 AssertRCReturn(rc, rc);
2620
2621 /* Load the client's internal message queue. */
2622 uint64_t cMsgs;
2623 rc = pVMM->pfnSSMR3GetU64(pSSM, &cMsgs);
2624 AssertRCReturn(rc, rc);
2625 AssertLogRelMsgReturn(cMsgs < _16K, ("Too many messages: %u (%x)\n", cMsgs, cMsgs), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2626
2627 for (uint64_t i = 0; i < cMsgs; i++)
2628 {
2629 union
2630 {
2631 SHCLCLIENTMSG Msg;
2632 uint8_t abPadding[RT_UOFFSETOF(SHCLCLIENTMSG, aParms) + sizeof(VBOXHGCMSVCPARM) * 2];
2633 } u;
2634
2635 pVMM->pfnSSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/,
2636 &s_aShClSSMClientMsgHdr[0], NULL);
2637 rc = pVMM->pfnSSMR3GetStructEx(pSSM, &u.Msg, RT_UOFFSETOF(SHCLCLIENTMSG, aParms), 0 /*fFlags*/,
2638 &s_aShClSSMClientMsgCtx[0], NULL);
2639 AssertRCReturn(rc, rc);
2640
2641 AssertLogRelMsgReturn(u.Msg.cParms <= VMMDEV_MAX_HGCM_PARMS,
2642 ("Too many HGCM message parameters: %u (%#x)\n", u.Msg.cParms, u.Msg.cParms),
2643 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2644
2645 PSHCLCLIENTMSG pMsg = shClSvcMsgAlloc(pClient, u.Msg.idMsg, u.Msg.cParms);
2646 AssertReturn(pMsg, VERR_NO_MEMORY);
2647 pMsg->idCtx = u.Msg.idCtx;
2648
2649 for (uint32_t p = 0; p < pMsg->cParms; p++)
2650 {
2651 rc = HGCMSvcSSMR3Get(&pMsg->aParms[p], pSSM, pVMM);
2652 AssertRCReturnStmt(rc, shClSvcMsgFree(pClient, pMsg), rc);
2653 }
2654
2655 RTCritSectEnter(&pClient->CritSect);
2656 shClSvcMsgAdd(pClient, pMsg, true /* fAppend */);
2657 RTCritSectLeave(&pClient->CritSect);
2658 }
2659
2660 if (lenOrVer >= VBOX_SHCL_SAVED_STATE_LEGACY_CID)
2661 {
2662 uint64_t cCID;
2663 rc = pVMM->pfnSSMR3GetU64(pSSM, &cCID);
2664 AssertRCReturn(rc, rc);
2665 AssertLogRelMsgReturn(cCID < _16K, ("Too many context IDs: %u (%x)\n", cCID, cCID), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2666
2667 for (uint64_t i = 0; i < cCID; i++)
2668 {
2669 PSHCLCLIENTLEGACYCID pCID = (PSHCLCLIENTLEGACYCID)RTMemAlloc(sizeof(SHCLCLIENTLEGACYCID));
2670 AssertPtrReturn(pCID, VERR_NO_MEMORY);
2671
2672 pVMM->pfnSSMR3GetStructEx(pSSM, pCID, sizeof(SHCLCLIENTLEGACYCID), 0 /* fFlags */,
2673 &s_aShClSSMClientLegacyCID[0], NULL);
2674 RTListAppend(&pClient->Legacy.lstCID, &pCID->Node);
2675 }
2676 }
2677 }
2678 else
2679 {
2680 LogRel(("Shared Clipboard: Unsupported saved state version (%#x)\n", lenOrVer));
2681 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
2682 }
2683
2684 /* Actual host data are to be reported to guest (SYNC). */
2685 ShClBackendSync(&g_ShClBackend, pClient);
2686
2687#else /* UNIT_TEST */
2688 RT_NOREF(u32ClientID, pvClient, pSSM, pVMM, uVersion);
2689#endif /* UNIT_TEST */
2690 return VINF_SUCCESS;
2691}
2692
2693static DECLCALLBACK(int) extCallback(uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
2694{
2695 RT_NOREF(pvData, cbData);
2696
2697 LogFlowFunc(("u32Function=%RU32\n", u32Function));
2698
2699 int rc = VINF_SUCCESS;
2700
2701 /* Figure out if the client in charge for the service extension still is connected. */
2702 ClipboardClientMap::const_iterator itClient = g_mapClients.find(g_ExtState.uClientID);
2703 if (itClient != g_mapClients.end())
2704 {
2705 PSHCLCLIENT pClient = itClient->second;
2706 AssertPtr(pClient);
2707
2708 switch (u32Function)
2709 {
2710 /* The service extension announces formats to the guest. */
2711 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
2712 {
2713 LogFlowFunc(("VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE: g_ExtState.fReadingData=%RTbool\n", g_ExtState.fReadingData));
2714 if (!g_ExtState.fReadingData)
2715 rc = ShClSvcHostReportFormats(pClient, u32Format);
2716 else
2717 {
2718 g_ExtState.fDelayedAnnouncement = true;
2719 g_ExtState.fDelayedFormats = u32Format;
2720 rc = VINF_SUCCESS;
2721 }
2722 break;
2723 }
2724
2725 /* The service extension wants read data from the guest. */
2726 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
2727 rc = ShClSvcGuestDataRequest(pClient, u32Format, NULL /* pidEvent */);
2728 break;
2729
2730 default:
2731 /* Just skip other messages. */
2732 break;
2733 }
2734 }
2735 else
2736 rc = VERR_NOT_FOUND;
2737
2738 LogFlowFuncLeaveRC(rc);
2739 return rc;
2740}
2741
2742static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
2743{
2744 LogFlowFunc(("pfnExtension=%p\n", pfnExtension));
2745
2746 SHCLEXTPARMS parms;
2747 RT_ZERO(parms);
2748
2749 /*
2750 * Reference counting for service extension registration is done a few
2751 * layers up (in ConsoleVRDPServer::ClipboardCreate()).
2752 */
2753
2754 int rc = RTCritSectEnter(&g_CritSect);
2755 AssertLogRelRCReturn(rc, rc);
2756
2757 if (pfnExtension)
2758 {
2759 /* Install extension. */
2760 g_ExtState.pfnExtension = pfnExtension;
2761 g_ExtState.pvExtension = pvExtension;
2762
2763 parms.u.pfnCallback = extCallback;
2764 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
2765
2766 LogRel2(("Shared Clipboard: registered service extension\n"));
2767 }
2768 else
2769 {
2770 if (g_ExtState.pfnExtension)
2771 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
2772
2773 /* Uninstall extension. */
2774 g_ExtState.pvExtension = NULL;
2775 g_ExtState.pfnExtension = NULL;
2776
2777 LogRel2(("Shared Clipboard: de-registered service extension\n"));
2778 }
2779
2780 RTCritSectLeave(&g_CritSect);
2781
2782 return VINF_SUCCESS;
2783}
2784
2785extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
2786{
2787 int rc = VINF_SUCCESS;
2788
2789 LogFlowFunc(("pTable=%p\n", pTable));
2790
2791 if (!RT_VALID_PTR(pTable))
2792 {
2793 rc = VERR_INVALID_PARAMETER;
2794 }
2795 else
2796 {
2797 LogFunc(("pTable->cbSize = %d, ptable->u32Version = 0x%08X\n", pTable->cbSize, pTable->u32Version));
2798
2799 if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
2800 || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
2801 {
2802 rc = VERR_VERSION_MISMATCH;
2803 }
2804 else
2805 {
2806 g_pHelpers = pTable->pHelpers;
2807
2808 pTable->cbClient = sizeof(SHCLCLIENT);
2809
2810 /* Map legacy clients to root. */
2811 pTable->idxLegacyClientCategory = HGCM_CLIENT_CATEGORY_ROOT;
2812
2813 /* Limit the number of clients to 128 in each category (should be enough),
2814 but set kernel clients to 1. */
2815 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
2816 pTable->acMaxClients[i] = 128;
2817 pTable->acMaxClients[HGCM_CLIENT_CATEGORY_KERNEL] = 1;
2818
2819 /* Only 16 pending calls per client (1 should be enough). */
2820 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
2821 pTable->acMaxCallsPerClient[i] = 16;
2822
2823 pTable->pfnUnload = svcUnload;
2824 pTable->pfnConnect = svcConnect;
2825 pTable->pfnDisconnect = svcDisconnect;
2826 pTable->pfnCall = svcCall;
2827 pTable->pfnHostCall = svcHostCall;
2828 pTable->pfnSaveState = svcSaveState;
2829 pTable->pfnLoadState = svcLoadState;
2830 pTable->pfnRegisterExtension = svcRegisterExtension;
2831 pTable->pfnNotify = NULL;
2832 pTable->pvService = NULL;
2833
2834 /* Service specific initialization. */
2835 rc = svcInit(pTable);
2836 }
2837 }
2838
2839 LogFlowFunc(("Returning %Rrc\n", rc));
2840 return rc;
2841}
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