VirtualBox

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

Last change on this file since 87062 was 86959, checked in by vboxsync, 4 years ago

Shared Clipboard: Convert VBox clipboard formats to strings to improve logging.

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

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