VirtualBox

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

Last change on this file since 81152 was 81152, checked in by vboxsync, 5 years ago

Shared Clipboard/HostService: Crash fix on X11 hosts.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 68.4 KB
Line 
1/* $Id: VBoxSharedClipboardSvc.cpp 81152 2019-10-08 13:30:43Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Host service entry points.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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 provides a proxy between the host's
22 * clipboard and a similar proxy running on a guest. The service is split
23 * into a platform-independent core and platform-specific backends. The
24 * service defines two communication protocols - one to communicate with the
25 * clipboard service running on the guest, and one to communicate with the
26 * backend. These will be described in a very skeletal fashion here.
27 *
28 * @section sec_hostclip_guest_proto The guest communication protocol
29 *
30 * The guest clipboard service communicates with the host service via HGCM
31 * (the host service runs as an HGCM service). The guest clipboard must
32 * connect to the host service before all else (Windows hosts currently only
33 * support one simultaneous connection). Once it has connected, it can send
34 * HGCM messages to the host services, some of which will receive replies from
35 * the host. The host can only reply to a guest message, it cannot initiate
36 * any communication. The guest can in theory send any number of messages in
37 * parallel (see the descriptions of the messages for the practice), and the
38 * host will receive these in sequence, and may reply to them at once
39 * (releasing the caller in the guest) or defer the reply until later.
40 *
41 * There are currently four messages defined. The first is
42 * VBOX_SHCL_FN_GET_HOST_MSG, which waits for a message from the
43 * host. If a host message is sent while the guest is not waiting, it will be
44 * queued until the guest requests it. The host code only supports a single
45 * simultaneous VBOX_SHCL_FN_GET_HOST_MSG call from one guest.
46 *
47 * The second guest message is VBOX_SHCL_FN_FORMATS, which tells
48 * the host that the guest has new clipboard data available. The third is
49 * VBOX_SHCL_FN_READ_DATA, which asks the host to send its
50 * clipboard data and waits until it arrives. The host supports at most one
51 * simultaneous VBOX_SHCL_FN_READ_DATA call from a guest - if a
52 * second call is made before the first has returned, the first will be
53 * aborted.
54 *
55 * The last guest message is VBOX_SHCL_FN_WRITE_DATA, which is
56 * used to send the contents of the guest clipboard to the host. This call
57 * should be used after the host has requested data from the guest.
58 *
59 * @section sec_hostclip_backend_proto The communication protocol with the
60 * platform-specific backend
61 *
62 * The initial protocol implementation (called protocol v0) was very simple,
63 * and could only handle simple data (like copied text and so on). It also
64 * was limited to two (2) fixed parameters at all times.
65 *
66 * Since VBox 6.1 a newer protocol (v1) has been established to also support
67 * file transfers. This protocol uses a (per-client) message queue instead
68 * (see VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD vs. VBOX_SHCL_GUEST_FN_GET_HOST_MSG).
69 *
70 * To distinguish the old (legacy) or new(er) protocol, the VBOX_SHCL_GUEST_FN_CONNECT
71 * message has been introduced. If an older guest does not send this message,
72 * an appropriate translation will be done to serve older Guest Additions (< 6.1).
73 *
74 * The protocol also support out-of-order messages by using so-called "context IDs",
75 * which are generated by the host. A context ID consists of a so-called "source event ID"
76 * and a so-called "event ID". Each HGCM client has an own, random, source event ID and
77 * generates non-deterministic event IDs so that the guest side does not known what
78 * comes next; the guest side has to reply with the same conext ID which was sent by
79 * the host request.
80 *
81 * Also see the protocol changelog at VBoxShClSvc.h.
82 *
83 * @section sec_uri_intro Transferring files
84 *
85 * Since VBox x.x.x transferring files via Shared Clipboard is supported.
86 * See the VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS define for supported / enabled
87 * platforms. This is called "Shared Clipboard transfers".
88 *
89 * Copying files / directories from guest A to guest B requires the host
90 * service to act as a proxy and cache, as we don't allow direct VM-to-VM
91 * communication. Copying from / to the host also is taken into account.
92 *
93 * At the moment a transfer is a all-or-nothing operation, e.g. it either
94 * completes or fails completely. There might be callbacks in the future
95 * to e.g. skip failing entries.
96 *
97 * Known limitations:
98 *
99 * - Support for VRDE (VRDP) is not implemented yet (see #9498).
100 * - Unicode support on Windows hosts / guests is not enabled (yet).
101 * - Symbolic links are not yet handled.
102 * - No support for ACLs yet.
103 * - No (maybe never) support for NT4.
104 *
105 * @section sec_transfers_areas Clipboard areas.
106 *
107 * For larger / longer transfers there might be file data
108 * temporarily cached on the host, which has not been transferred to the
109 * destination yet. Such a cache is called a "Shared Clipboard Area", which
110 * in turn is identified by a unique ID across all VMs running on the same
111 * host. To control the access (and needed cleanup) of such clipboard areas,
112 * VBoxSVC (Main) is used for this task. A Shared Clipboard client can register,
113 * unregister, attach to and detach from a clipboard area. If all references
114 * to a clipboard area are released, a clipboard area gets detroyed automatically
115 * by VBoxSVC.
116 *
117 * By default a clipboard area lives in the user's temporary directory in the
118 * sub folder "VirtualBox Shared Clipboards/clipboard-<ID>". VBoxSVC does not
119 * do any file locking in a clipboard area, but keeps the clipboard areas's
120 * directory open to prevent deletion by third party processes.
121 *
122 * @todo We might use some VFS / container (IPRT?) for this instead of the
123 * host's file system directly?
124 *
125 * @section sec_transfer_structure Transfer handling structure
126 *
127 * All structures / classes are designed for running on both, on the guest
128 * (via VBoxTray / VBoxClient) or on the host (host service) to avoid code
129 * duplication where applicable.
130 *
131 * Per HGCM client there is a so-called "transfer context", which in turn can have
132 * one or mulitple so-called "Shared Clipboard transfer" objects. At the moment we only support
133 * on concurrent Shared Clipboard transfer per transfer context. It's being used for reading from a
134 * source or writing to destination, depening on its direction. An Shared Clipboard transfer
135 * can have optional callbacks which might be needed by various implementations.
136 * Also, transfers optionally can run in an asynchronous thread to prevent
137 * blocking the UI while running.
138 *
139 * An Shared Clipboard transfer can maintain its own clipboard area; for the host service such
140 * a clipboard area is coupled to a clipboard area registered or attached with
141 * VBoxSVC. This is needed because multiple transfers from multiple VMs (n:n) can
142 * rely on the same clipboard area, so there needs a master keeping tracking of
143 * a clipboard area. To minimize IPC traffic only the minimum de/attaching is done
144 * at the moment. A clipboard area gets cleaned up (i.e. physically deleted) if
145 * no references are held to it anymore, or if VBoxSVC goes down.
146 *
147 * @section sec_transfer_providers Transfer providers
148 *
149 * For certain implementations (for example on Windows guests / hosts, using
150 * IDataObject and IStream objects) a more flexible approach reqarding reading /
151 * writing is needed. For this so-called transfer providers abstract the way of how
152 * data is being read / written in the current context (host / guest), while
153 * the rest of the code stays the same.
154 *
155 * @section sec_transfer_protocol Transfer protocol
156 *
157 * The host service issues commands which the guest has to respond with an own
158 * message to. The protocol itself is designed so that it has primitives to list
159 * directories and open/close/read/write file system objects.
160 *
161 * Note that this is different from the DnD approach, as Shared Clipboard transfers
162 * need to be deeper integrated within the host / guest OS (i.e. for progress UI),
163 * and this might require non-monolithic / random access APIs to achieve.
164 *
165 * As there can be multiple file system objects (fs objects) selected for transfer,
166 * a transfer can be queried for its root entries, which then contains the top-level
167 * elements. Based on these elements, (a) (recursive) listing(s) can be performed
168 * to (partially) walk down into directories and query fs object information. The
169 * provider provides appropriate interface for this, even if not all implementations
170 * might need this mechanism.
171 *
172 * An Shared Clipboard transfer has three stages:
173 * - 1. Announcement: An Shared Clipboard transfer-compatible format (currently only one format available)
174 * has been announced, the destination side creates a transfer object, which then,
175 * depending on the actual implementation, can be used to tell the OS that
176 * there is transfer (file) data available.
177 * At this point this just acts as a (kind-of) promise to the OS that we
178 * can provide (file) data at some later point in time.
179 *
180 * - 2. Initialization: As soon as the OS requests the (file) data, mostly triggered
181 * by the user starting a paste operation (CTRL + V), the transfer get initialized
182 * on the destination side, which in turn lets the source know that a transfer
183 * is going to happen.
184 *
185 * - 3. Transfer: At this stage the actual transfer from source to the destination takes
186 * place. How the actual transfer is structurized (e.g. which files / directories
187 * are transferred in which order) depends on the destination implementation. This
188 * is necessary in order to fulfill requirements on the destination side with
189 * regards to ETA calculation or other dependencies.
190 * Both sides can abort or cancel the transfer at any time.
191 */
192
193
194/*********************************************************************************************************************************
195* Header Files *
196*********************************************************************************************************************************/
197#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
198#include <VBox/log.h>
199
200#include <VBox/AssertGuest.h>
201#include <VBox/GuestHost/clipboard-helper.h>
202#include <VBox/HostServices/Service.h>
203#include <VBox/HostServices/VBoxClipboardSvc.h>
204#include <VBox/HostServices/VBoxClipboardExt.h>
205
206#include <iprt/alloc.h>
207#include <iprt/string.h>
208#include <iprt/assert.h>
209#include <iprt/critsect.h>
210#include <iprt/rand.h>
211
212#include <VBox/err.h>
213#include <VBox/vmm/ssm.h>
214
215#include "VBoxSharedClipboardSvc-internal.h"
216#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
217# include "VBoxSharedClipboardSvc-transfers.h"
218#endif
219
220using namespace HGCM;
221
222
223/*********************************************************************************************************************************
224* Prototypes *
225*********************************************************************************************************************************/
226static int shclSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID);
227static int shclSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState);
228static void shclSvcClientStateReset(PSHCLCLIENTSTATE pClientState);
229
230
231/*********************************************************************************************************************************
232* Global Variables *
233*********************************************************************************************************************************/
234PVBOXHGCMSVCHELPERS g_pHelpers;
235
236static RTCRITSECT g_CritSect;
237static uint32_t g_uMode;
238
239/** Is the clipboard running in headless mode? */
240static bool g_fHeadless = false;
241
242/** Holds the service extension state. */
243SHCLEXTSTATE g_ExtState = { 0 };
244
245/** Global map of all connected clients. */
246ClipboardClientMap g_mapClients;
247
248/** Global list of all clients which are queued up (deferred return) and ready
249 * to process new commands. The key is the (unique) client ID. */
250ClipboardClientQueue g_listClientsDeferred;
251
252
253uint32_t shclSvcGetMode(void)
254{
255 return g_uMode;
256}
257
258#ifdef UNIT_TEST
259/** Testing interface, getter for clipboard mode */
260uint32_t TestClipSvcGetMode(void)
261{
262 return shclSvcGetMode();
263}
264#endif
265
266/** Getter for headless setting. Also needed by testcase. */
267bool ShClSvcGetHeadless(void)
268{
269 return g_fHeadless;
270}
271
272static int shclSvcModeSet(uint32_t uMode)
273{
274 int rc = VERR_NOT_SUPPORTED;
275
276 switch (uMode)
277 {
278 case VBOX_SHCL_MODE_OFF:
279 RT_FALL_THROUGH();
280 case VBOX_SHCL_MODE_HOST_TO_GUEST:
281 RT_FALL_THROUGH();
282 case VBOX_SHCL_MODE_GUEST_TO_HOST:
283 RT_FALL_THROUGH();
284 case VBOX_SHCL_MODE_BIDIRECTIONAL:
285 {
286 g_uMode = uMode;
287
288 rc = VINF_SUCCESS;
289 break;
290 }
291
292 default:
293 {
294 g_uMode = VBOX_SHCL_MODE_OFF;
295 break;
296 }
297 }
298
299 LogFlowFuncLeaveRC(rc);
300 return rc;
301}
302
303bool ShClSvcLock(void)
304{
305 return RT_SUCCESS(RTCritSectEnter(&g_CritSect));
306}
307
308void ShClSvcUnlock(void)
309{
310 int rc2 = RTCritSectLeave(&g_CritSect);
311 AssertRC(rc2);
312}
313
314/**
315 * Resets a client's state message queue.
316 *
317 * @param pClient Pointer to the client data structure to reset message queue for.
318 */
319void shclSvcMsgQueueReset(PSHCLCLIENT pClient)
320{
321 LogFlowFuncEnter();
322
323 while (!pClient->queueMsg.isEmpty())
324 {
325 RTMemFree(pClient->queueMsg.last());
326 pClient->queueMsg.removeLast();
327 }
328}
329
330/**
331 * Allocates a new clipboard message.
332 *
333 * @returns Allocated clipboard message, or NULL on failure.
334 * @param uMsg Message type of message to allocate.
335 * @param cParms Number of HGCM parameters to allocate.
336 */
337PSHCLCLIENTMSG shclSvcMsgAlloc(uint32_t uMsg, uint32_t cParms)
338{
339 PSHCLCLIENTMSG pMsg = (PSHCLCLIENTMSG)RTMemAlloc(sizeof(SHCLCLIENTMSG));
340 if (pMsg)
341 {
342 pMsg->paParms = (PVBOXHGCMSVCPARM)RTMemAllocZ(sizeof(VBOXHGCMSVCPARM) * cParms);
343 if (pMsg->paParms)
344 {
345 pMsg->cParms = cParms;
346 pMsg->uMsg = uMsg;
347
348 return pMsg;
349 }
350 }
351
352 RTMemFree(pMsg);
353 return NULL;
354}
355
356/**
357 * Frees a formerly allocated clipboard message.
358 *
359 * @param pMsg Clipboard message to free.
360 * The pointer will be invalid after calling this function.
361 */
362void shclSvcMsgFree(PSHCLCLIENTMSG pMsg)
363{
364 if (!pMsg)
365 return;
366
367 if (pMsg->paParms)
368 RTMemFree(pMsg->paParms);
369
370 RTMemFree(pMsg);
371 pMsg = NULL;
372}
373
374/**
375 * Sets the VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT
376 * return parameters.
377 *
378 * @param pMsg Message to set return parameters to.
379 * @param paDstParms The peek parameter vector.
380 * @param cDstParms The number of peek parameters (at least two).
381 * @remarks ASSUMES the parameters has been cleared by clientMsgPeek.
382 */
383void shclSvcMsgSetPeekReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
384{
385 Assert(cDstParms >= 2);
386 if (paDstParms[0].type == VBOX_HGCM_SVC_PARM_32BIT)
387 paDstParms[0].u.uint32 = pMsg->uMsg;
388 else
389 paDstParms[0].u.uint64 = pMsg->uMsg;
390 paDstParms[1].u.uint32 = pMsg->cParms;
391
392 uint32_t i = RT_MIN(cDstParms, pMsg->cParms + 2);
393 while (i-- > 2)
394 switch (pMsg->paParms[i - 2].type)
395 {
396 case VBOX_HGCM_SVC_PARM_32BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint32_t); break;
397 case VBOX_HGCM_SVC_PARM_64BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint64_t); break;
398 case VBOX_HGCM_SVC_PARM_PTR: paDstParms[i].u.uint32 = pMsg->paParms[i - 2].u.pointer.size; break;
399 }
400}
401
402/**
403 * Sets the VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD return parameters.
404 *
405 * This function does the necessary translation between the legacy protocol (v0) and the new protocols (>= v1),
406 * as messages are always stored as >= v1 messages in the message queue.
407 *
408 * @returns VBox status code.
409 * @param pMsg Message to set return parameters to.
410 * @param paDstParms The peek parameter vector.
411 * @param cDstParms The number of peek parameters (at least two).
412 */
413int shclSvcMsgSetGetHostMsgOldReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
414{
415 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
416 AssertPtrReturn(paDstParms, VERR_INVALID_POINTER);
417 AssertReturn (cDstParms >= 2, VERR_INVALID_PARAMETER);
418
419 int rc = VINF_SUCCESS;
420
421 switch (pMsg->uMsg)
422 {
423 case VBOX_SHCL_HOST_MSG_QUIT:
424 {
425 HGCMSvcSetU32(&paDstParms[0], VBOX_SHCL_HOST_MSG_QUIT);
426 HGCMSvcSetU32(&paDstParms[1], 0 /* Not used */);
427 break;
428 }
429
430 case VBOX_SHCL_HOST_MSG_READ_DATA:
431 {
432 HGCMSvcSetU32(&paDstParms[0], VBOX_SHCL_HOST_MSG_READ_DATA);
433 AssertBreakStmt(pMsg->cParms >= 2, rc = VERR_INVALID_PARAMETER); /* Paranoia. */
434 uint32_t uFmt;
435 rc = HGCMSvcGetU32(&pMsg->paParms[1] /* uFormat */, &uFmt);
436 if (RT_SUCCESS(rc))
437 HGCMSvcSetU32(&paDstParms[1], uFmt);
438 break;
439 }
440
441 case VBOX_SHCL_HOST_MSG_FORMATS_REPORT:
442 {
443 HGCMSvcSetU32(&paDstParms[0], VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
444 AssertBreakStmt(pMsg->cParms >= 2, rc = VERR_INVALID_PARAMETER); /* Paranoia. */
445 uint32_t uFmts;
446 rc = HGCMSvcGetU32(&pMsg->paParms[1] /* uFormats */, &uFmts);
447 if (RT_SUCCESS(rc))
448 HGCMSvcSetU32(&paDstParms[1], uFmts);
449 break;
450 }
451
452 default:
453 AssertFailed(); /* Not supported by legacy protocol. */
454 rc = VERR_NOT_SUPPORTED;
455 break;
456 }
457
458 LogFlowFuncLeaveRC(rc);
459 return rc;
460}
461
462/**
463 * Adds a new message to a client'S message queue.
464 *
465 * @returns IPRT status code.
466 * @param pClient Pointer to the client data structure to add new message to.
467 * @param pMsg Pointer to message to add. The queue then owns the pointer.
468 * @param fAppend Whether to append or prepend the message to the queue.
469 */
470int shclSvcMsgAdd(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg, bool fAppend)
471{
472 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
473
474 LogFlowFunc(("uMsg=%RU32 (%s), cParms=%RU32, fAppend=%RTbool\n",
475 pMsg->uMsg, VBoxShClHostMsgToStr(pMsg->uMsg), pMsg->cParms, fAppend));
476
477 if (fAppend)
478 pClient->queueMsg.append(pMsg);
479 else
480 pClient->queueMsg.prepend(pMsg);
481
482 /** @todo Catch / handle OOM? */
483
484 return VINF_SUCCESS;
485}
486
487/**
488 * Implements VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT.
489 *
490 * @returns VBox status code.
491 * @retval VINF_SUCCESS if a message was pending and is being returned.
492 * @retval VERR_TRY_AGAIN if no message pending and not blocking.
493 * @retval VERR_RESOURCE_BUSY if another read already made a waiting call.
494 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
495 *
496 * @param pClient The client state.
497 * @param hCall The client's call handle.
498 * @param cParms Number of parameters.
499 * @param paParms Array of parameters.
500 * @param fWait Set if we should wait for a message, clear if to return
501 * immediately.
502 */
503int shclSvcMsgPeek(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[],
504 bool fWait)
505{
506 /*
507 * Validate the request.
508 */
509 ASSERT_GUEST_MSG_RETURN(cParms >= 2, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
510
511 uint64_t idRestoreCheck = 0;
512 uint32_t i = 0;
513 if (paParms[i].type == VBOX_HGCM_SVC_PARM_64BIT)
514 {
515 idRestoreCheck = paParms[0].u.uint64;
516 paParms[0].u.uint64 = 0;
517 i++;
518 }
519 for (; i < cParms; i++)
520 {
521 ASSERT_GUEST_MSG_RETURN(paParms[i].type == VBOX_HGCM_SVC_PARM_32BIT, ("#%u type=%u\n", i, paParms[i].type),
522 VERR_WRONG_PARAMETER_TYPE);
523 paParms[i].u.uint32 = 0;
524 }
525
526 /*
527 * Check restore session ID.
528 */
529 if (idRestoreCheck != 0)
530 {
531 uint64_t idRestore = g_pHelpers->pfnGetVMMDevSessionId(g_pHelpers);
532 if (idRestoreCheck != idRestore)
533 {
534 paParms[0].u.uint64 = idRestore;
535 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VERR_VM_RESTORED (%#RX64 -> %#RX64)\n",
536 pClient->State.uClientID, idRestoreCheck, idRestore));
537 return VERR_VM_RESTORED;
538 }
539 Assert(!g_pHelpers->pfnIsCallRestored(hCall));
540 }
541
542 /*
543 * Return information about the first message if one is pending in the list.
544 */
545 if (!pClient->queueMsg.isEmpty())
546 {
547 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
548 if (pFirstMsg)
549 {
550 shclSvcMsgSetPeekReturn(pFirstMsg, paParms, cParms);
551 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VINF_SUCCESS (idMsg=%u (%s), cParms=%u)\n",
552 pClient->State.uClientID, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg),
553 pFirstMsg->cParms));
554 return VINF_SUCCESS;
555 }
556 }
557
558 /*
559 * If we cannot wait, fail the call.
560 */
561 if (!fWait)
562 {
563 LogFlowFunc(("[Client %RU32] GUEST_MSG_PEEK_NOWAIT -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
564 return VERR_TRY_AGAIN;
565 }
566
567 /*
568 * Wait for the host to queue a message for this client.
569 */
570 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n",
571 pClient->State.uClientID), VERR_RESOURCE_BUSY);
572 pClient->Pending.hHandle = hCall;
573 pClient->Pending.cParms = cParms;
574 pClient->Pending.paParms = paParms;
575 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT;
576 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
577 return VINF_HGCM_ASYNC_EXECUTE;
578}
579
580/**
581 * Implements VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD.
582 *
583 * @returns VBox status code.
584 * @retval VINF_SUCCESS if a message was pending and is being returned.
585 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
586 *
587 * @param pClient The client state.
588 * @param hCall The client's call handle.
589 * @param cParms Number of parameters.
590 * @param paParms Array of parameters.
591 */
592int shclSvcMsgGetOld(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
593{
594 int rc;
595
596 if (cParms != VBOX_SHCL_CPARMS_GET_HOST_MSG_OLD)
597 {
598 rc = VERR_INVALID_PARAMETER;
599 }
600 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* msg */
601 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT) /* formats */
602 {
603 rc = VERR_INVALID_PARAMETER;
604 }
605 else
606 {
607 if (!pClient->queueMsg.isEmpty())
608 {
609 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
610 AssertPtr(pFirstMsg);
611
612 LogFlowFunc(("[Client %RU32] uMsg=%RU32 (%s), cParms=%RU32\n",
613 pClient->State.uClientID, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg),
614 pFirstMsg->cParms));
615
616 rc = shclSvcMsgSetGetHostMsgOldReturn(pFirstMsg, paParms, cParms);
617 if (RT_SUCCESS(rc))
618 {
619 AssertPtr(g_pHelpers);
620 rc = g_pHelpers->pfnCallComplete(hCall, rc);
621 if (rc != VERR_CANCELLED)
622 {
623 pClient->queueMsg.removeFirst();
624 shclSvcMsgFree(pFirstMsg);
625
626 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
627 }
628 }
629 }
630 else
631 {
632 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n",
633 pClient->State.uClientID), VERR_RESOURCE_BUSY);
634
635 pClient->Pending.hHandle = hCall;
636 pClient->Pending.cParms = cParms;
637 pClient->Pending.paParms = paParms;
638 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD;
639
640 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
641
642 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
643 }
644 }
645
646 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
647 return rc;
648}
649
650/**
651 * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
652 *
653 * @returns VBox status code.
654 * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
655 * @retval VERR_TRY_AGAIN if no message pending.
656 * @retval VERR_BUFFER_OVERFLOW if a parmeter buffer is too small. The buffer
657 * size was updated to reflect the required size, though this isn't yet
658 * forwarded to the guest. (The guest is better of using peek with
659 * parameter count + 2 parameters to get the sizes.)
660 * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
661 * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
662 *
663 * @param pClient The client state.
664 * @param hCall The client's call handle.
665 * @param cParms Number of parameters.
666 * @param paParms Array of parameters.
667 */
668int shclSvcMsgGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
669{
670 /*
671 * Validate the request.
672 */
673 uint32_t const idMsgExpected = cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT ? paParms[0].u.uint32
674 : cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT ? paParms[0].u.uint64
675 : UINT32_MAX;
676
677 /*
678 * Return information about the first message if one is pending in the list.
679 */
680 if (!pClient->queueMsg.isEmpty())
681 {
682 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
683 if (pFirstMsg)
684 {
685 LogFlowFunc(("First message is: %RU32 (%s), cParms=%RU32\n",
686 pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg), pFirstMsg->cParms));
687
688 ASSERT_GUEST_MSG_RETURN(pFirstMsg->uMsg == idMsgExpected || idMsgExpected == UINT32_MAX,
689 ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
690 pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg), pFirstMsg->cParms,
691 idMsgExpected, VBoxShClHostMsgToStr(idMsgExpected), cParms),
692 VERR_MISMATCH);
693 ASSERT_GUEST_MSG_RETURN(pFirstMsg->cParms == cParms,
694 ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
695 pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg), pFirstMsg->cParms,
696 idMsgExpected, VBoxShClHostMsgToStr(idMsgExpected), cParms),
697 VERR_WRONG_PARAMETER_COUNT);
698
699 /* Check the parameter types. */
700 for (uint32_t i = 0; i < cParms; i++)
701 ASSERT_GUEST_MSG_RETURN(pFirstMsg->paParms[i].type == paParms[i].type,
702 ("param #%u: type %u, caller expected %u (idMsg=%u %s)\n", i, pFirstMsg->paParms[i].type,
703 paParms[i].type, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg)),
704 VERR_WRONG_PARAMETER_TYPE);
705 /*
706 * Copy out the parameters.
707 *
708 * No assertions on buffer overflows, and keep going till the end so we can
709 * communicate all the required buffer sizes.
710 */
711 int rc = VINF_SUCCESS;
712 for (uint32_t i = 0; i < cParms; i++)
713 switch (pFirstMsg->paParms[i].type)
714 {
715 case VBOX_HGCM_SVC_PARM_32BIT:
716 paParms[i].u.uint32 = pFirstMsg->paParms[i].u.uint32;
717 break;
718
719 case VBOX_HGCM_SVC_PARM_64BIT:
720 paParms[i].u.uint64 = pFirstMsg->paParms[i].u.uint64;
721 break;
722
723 case VBOX_HGCM_SVC_PARM_PTR:
724 {
725 uint32_t const cbSrc = pFirstMsg->paParms[i].u.pointer.size;
726 uint32_t const cbDst = paParms[i].u.pointer.size;
727 paParms[i].u.pointer.size = cbSrc; /** @todo Check if this is safe in other layers...
728 * Update: Safe, yes, but VMMDevHGCM doesn't pass it along. */
729 if (cbSrc <= cbDst)
730 memcpy(paParms[i].u.pointer.addr, pFirstMsg->paParms[i].u.pointer.addr, cbSrc);
731 else
732 {
733 AssertMsgFailed(("#%u: cbSrc=%RU32 is bigger than cbDst=%RU32\n", i, cbSrc, cbDst));
734 rc = VERR_BUFFER_OVERFLOW;
735 }
736 break;
737 }
738
739 default:
740 AssertMsgFailed(("#%u: %u\n", i, pFirstMsg->paParms[i].type));
741 rc = VERR_INTERNAL_ERROR;
742 break;
743 }
744 if (RT_SUCCESS(rc))
745 {
746 /*
747 * Complete the message and remove the pending message unless the
748 * guest raced us and cancelled this call in the meantime.
749 */
750 AssertPtr(g_pHelpers);
751 rc = g_pHelpers->pfnCallComplete(hCall, rc);
752
753 LogFlowFunc(("[Client %RU32] pfnCallComplete -> %Rrc\n", pClient->State.uClientID, rc));
754
755 if (rc != VERR_CANCELLED)
756 {
757 pClient->queueMsg.removeFirst();
758 shclSvcMsgFree(pFirstMsg);
759 }
760
761 return VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
762 }
763
764 LogFlowFunc(("[Client %RU32] Returning %Rrc\n", pClient->State.uClientID, rc));
765 return rc;
766 }
767 }
768
769 paParms[0].u.uint32 = 0;
770 paParms[1].u.uint32 = 0;
771 LogFlowFunc(("[Client %RU32] -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
772 return VERR_TRY_AGAIN;
773}
774
775/**
776 * Wakes up a pending client (i.e. waiting for new messages).
777 *
778 * @returns VBox status code.
779 * @retval VINF_NO_CHANGE if the client is not in pending mode.
780 *
781 * @param pClient Client to wake up.
782 */
783int shclSvcClientWakeup(PSHCLCLIENT pClient)
784{
785 int rc = VINF_NO_CHANGE;
786
787 if (pClient->Pending.uType)
788 {
789 LogFunc(("[Client %RU32] Waking up ...\n", pClient->State.uClientID));
790
791 rc = VINF_SUCCESS;
792
793 if (!pClient->queueMsg.isEmpty())
794 {
795 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
796 if (pFirstMsg)
797 {
798 LogFunc(("[Client %RU32] Current host message is %RU32 (%s), cParms=%RU32\n",
799 pClient->State.uClientID, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg),
800 pFirstMsg->cParms));
801
802 bool fDonePending = false;
803
804 if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
805 {
806 shclSvcMsgSetPeekReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
807 fDonePending = true;
808 }
809 else if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD) /* Legacy */
810 {
811 rc = shclSvcMsgSetGetHostMsgOldReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
812 if (RT_SUCCESS(rc))
813 {
814 /* The old (legacy) protocol gets the message right when returning from peeking, so
815 * remove the actual message from our queue right now. */
816 pClient->queueMsg.removeFirst();
817 shclSvcMsgFree(pFirstMsg);
818
819 fDonePending = true;
820 }
821 }
822
823 if (fDonePending)
824 {
825 rc = g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
826
827 pClient->Pending.hHandle = NULL;
828 pClient->Pending.paParms = NULL;
829 pClient->Pending.cParms = 0;
830 pClient->Pending.uType = 0;
831 }
832 }
833 else
834 AssertFailed();
835 }
836 else
837 AssertMsgFailed(("Waking up client ID=%RU32 with no host message in queue is a bad idea\n", pClient->State.uClientID));
838
839 return rc;
840 }
841 else
842 LogFunc(("[Client %RU32] Not in pending state, skipping wakeup\n", pClient->State.uClientID));
843
844 return VINF_NO_CHANGE;
845}
846
847/**
848 * Requests to read clipboard data from the guest.
849 *
850 * @returns VBox status code.
851 * @param pClient Client to request to read data form.
852 * @param pDataReq Data request to send to the guest.
853 * @param puEvent Event ID for waiting for new data. Optional.
854 */
855int shclSvcDataReadRequest(PSHCLCLIENT pClient, PSHCLDATAREQ pDataReq,
856 PSHCLEVENTID puEvent)
857{
858 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
859 AssertPtrReturn(pDataReq, VERR_INVALID_POINTER);
860 /* puEvent is optional. */
861
862 int rc;
863
864 PSHCLCLIENTMSG pMsgReadData = shclSvcMsgAlloc(VBOX_SHCL_HOST_MSG_READ_DATA,
865 VBOX_SHCL_CPARMS_READ_DATA);
866 if (pMsgReadData)
867 {
868 const SHCLEVENTID uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
869
870 HGCMSvcSetU32(&pMsgReadData->paParms[0], VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID,
871 pClient->Events.uID, uEvent));
872 HGCMSvcSetU32(&pMsgReadData->paParms[1], pDataReq->uFmt);
873 HGCMSvcSetU32(&pMsgReadData->paParms[2], pClient->State.cbChunkSize);
874
875 rc = shclSvcMsgAdd(pClient, pMsgReadData, true /* fAppend */);
876 if (RT_SUCCESS(rc))
877 {
878 rc = SharedClipboardEventRegister(&pClient->Events, uEvent);
879 if (RT_SUCCESS(rc))
880 {
881 rc = shclSvcClientWakeup(pClient);
882 if (RT_SUCCESS(rc))
883 {
884 if (puEvent)
885 *puEvent = uEvent;
886 }
887 else
888 SharedClipboardEventUnregister(&pClient->Events, uEvent);
889 }
890 }
891 }
892 else
893 rc = VERR_NO_MEMORY;
894
895 LogFlowFuncLeaveRC(rc);
896 return rc;
897}
898
899int shclSvcDataReadSignal(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
900 PSHCLDATABLOCK pData)
901{
902 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
903 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
904 AssertPtrReturn(pData, VERR_INVALID_POINTER);
905
906 SHCLEVENTID uEvent;
907 if (pClient->State.uProtocolVer == 0)
908 {
909 /* Protocol v0 did not have any context ID handling, so we ASSUME that the last event registered
910 * is the one we want to handle (as this all was a synchronous protocol anyway). */
911 uEvent = SharedClipboardEventGetLast(&pClient->Events);
912 }
913 else
914 uEvent = VBOX_SHCL_CONTEXTID_GET_EVENT(pCmdCtx->uContextID);
915
916 int rc = VINF_SUCCESS;
917
918 PSHCLEVENTPAYLOAD pPayload = NULL;
919 if (pData->cbData)
920 rc = SharedClipboardPayloadAlloc(uEvent, pData->pvData, pData->cbData, &pPayload);
921
922 if (RT_SUCCESS(rc))
923 {
924 rc = SharedClipboardEventSignal(&pClient->Events, uEvent, pPayload);
925 if (RT_FAILURE(rc))
926 SharedClipboardPayloadFree(pPayload);
927 }
928
929 LogFlowFuncLeaveRC(rc);
930 return rc;
931}
932
933int shclSvcFormatsReport(PSHCLCLIENT pClient, PSHCLFORMATDATA pFormats)
934{
935 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
936 AssertPtrReturn(pFormats, VERR_INVALID_POINTER);
937
938 int rc;
939
940 PSHCLCLIENTMSG pMsg = shclSvcMsgAlloc(VBOX_SHCL_HOST_MSG_FORMATS_REPORT, 3);
941 if (pMsg)
942 {
943 const SHCLEVENTID uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
944
945 HGCMSvcSetU32(&pMsg->paParms[0], VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID,
946 pClient->Events.uID, uEvent));
947 HGCMSvcSetU32(&pMsg->paParms[1], pFormats->uFormats);
948 HGCMSvcSetU32(&pMsg->paParms[2], 0 /* fFlags */);
949
950 rc = shclSvcMsgAdd(pClient, pMsg, true /* fAppend */);
951 if (RT_SUCCESS(rc))
952 {
953#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
954 /* If this is an URI list, create a transfer locally and also tell the guest to create
955 * a transfer on the guest side. */
956 if (pFormats->uFormats & VBOX_SHCL_FMT_URI_LIST)
957 {
958 rc = shclSvcTransferStart(pClient, SHCLTRANSFERDIR_WRITE, SHCLSOURCE_LOCAL,
959 NULL /* pTransfer */);
960 if (RT_FAILURE(rc))
961 LogRel(("Shared Clipboard: Initializing host write transfer failed with %Rrc\n", rc));
962 }
963 else
964 {
965#endif
966 rc = shclSvcClientWakeup(pClient);
967
968#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
969 }
970#endif
971 }
972 }
973 else
974 rc = VERR_NO_MEMORY;
975
976 LogFlowFuncLeaveRC(rc);
977 return rc;
978}
979
980int shclSvcGetDataWrite(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
981{
982 LogFlowFuncEnter();
983
984 if ( shclSvcGetMode() != VBOX_SHCL_MODE_GUEST_TO_HOST
985 && shclSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
986 {
987 return VERR_NOT_SUPPORTED;
988 }
989
990 int rc;
991
992 SHCLDATABLOCK dataBlock;
993 RT_ZERO(dataBlock);
994
995 SHCLCLIENTCMDCTX cmdCtx;
996 RT_ZERO(cmdCtx);
997
998 if (pClient->State.uProtocolVer == 0) /* Legacy protocol */
999 {
1000 if (cParms < 2)
1001 {
1002 rc = VERR_INVALID_PARAMETER;
1003 }
1004 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
1005 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* ptr */
1006 {
1007 rc = VERR_INVALID_PARAMETER;
1008 }
1009 else
1010 {
1011 rc = HGCMSvcGetU32(&paParms[0], &dataBlock.uFormat);
1012 if (RT_SUCCESS(rc))
1013 rc = HGCMSvcGetBuf(&paParms[1], &dataBlock.pvData, &dataBlock.cbData);
1014 }
1015 }
1016 else
1017 {
1018 if (cParms < VBOX_SHCL_CPARMS_WRITE_DATA)
1019 {
1020 rc = VERR_INVALID_PARAMETER;
1021 }
1022 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uContext */
1023 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uFormat */
1024 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* cbData */
1025 || paParms[3].type != VBOX_HGCM_SVC_PARM_PTR) /* pvData */
1026 {
1027 rc = VERR_INVALID_PARAMETER;
1028 }
1029 else
1030 {
1031 rc = HGCMSvcGetU32(&paParms[0], &cmdCtx.uContextID);
1032 if (RT_SUCCESS(rc))
1033 rc = HGCMSvcGetU32(&paParms[1], &dataBlock.uFormat);
1034 if (RT_SUCCESS(rc))
1035 rc = HGCMSvcGetBuf(&paParms[3], &dataBlock.pvData, &dataBlock.cbData);
1036
1037 /** @todo Handle the rest. */
1038 }
1039 }
1040
1041 if (RT_SUCCESS(rc))
1042 {
1043 if (g_ExtState.pfnExtension)
1044 {
1045 SHCLEXTPARMS parms;
1046 RT_ZERO(parms);
1047
1048 parms.uFormat = dataBlock.uFormat;
1049 parms.u.pvData = dataBlock.pvData;
1050 parms.cbData = dataBlock.cbData;
1051
1052 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof(parms));
1053 }
1054
1055 rc = ShClSvcImplWriteData(pClient, &cmdCtx, &dataBlock);
1056 }
1057
1058 LogFlowFuncLeaveRC(rc);
1059 return rc;
1060}
1061
1062int shclSvcSetSource(PSHCLCLIENT pClient, SHCLSOURCE enmSource)
1063{
1064 if (!pClient) /* If no client connected (anymore), bail out. */
1065 return VINF_SUCCESS;
1066
1067 int rc = VINF_SUCCESS;
1068
1069 if (ShClSvcLock())
1070 {
1071 pClient->State.enmSource = enmSource;
1072
1073 LogFlowFunc(("Source of client %RU32 is now %RU32\n", pClient->State.uClientID, pClient->State.enmSource));
1074
1075 ShClSvcUnlock();
1076 }
1077
1078 LogFlowFuncLeaveRC(rc);
1079 return rc;
1080}
1081
1082static int svcInit(void)
1083{
1084 int rc = RTCritSectInit(&g_CritSect);
1085
1086 if (RT_SUCCESS(rc))
1087 {
1088 shclSvcModeSet(VBOX_SHCL_MODE_OFF);
1089
1090 rc = ShClSvcImplInit();
1091
1092 /* Clean up on failure, because 'svnUnload' will not be called
1093 * if the 'svcInit' returns an error.
1094 */
1095 if (RT_FAILURE(rc))
1096 {
1097 RTCritSectDelete(&g_CritSect);
1098 }
1099 }
1100
1101 return rc;
1102}
1103
1104static DECLCALLBACK(int) svcUnload(void *)
1105{
1106 LogFlowFuncEnter();
1107
1108 ShClSvcImplDestroy();
1109
1110 RTCritSectDelete(&g_CritSect);
1111
1112 return VINF_SUCCESS;
1113}
1114
1115static DECLCALLBACK(int) svcDisconnect(void *, uint32_t u32ClientID, void *pvClient)
1116{
1117 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1118
1119 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1120 AssertPtr(pClient);
1121
1122#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1123 PSHCLTRANSFER pTransfer = SharedClipboardTransferCtxGetTransfer(&pClient->TransferCtx, 0 /* Index*/);
1124 if (pTransfer)
1125 shclSvcTransferAreaDetach(&pClient->State, pTransfer);
1126
1127 SharedClipboardTransferCtxDestroy(&pClient->TransferCtx);
1128#endif
1129
1130 ShClSvcImplDisconnect(pClient);
1131
1132 /* Make sure to send a quit message to the guest so that it can terminate gracefully. */
1133 if (pClient->Pending.uType)
1134 {
1135 if (pClient->Pending.cParms >= 2)
1136 {
1137 HGCMSvcSetU32(&pClient->Pending.paParms[0], VBOX_SHCL_HOST_MSG_QUIT);
1138 HGCMSvcSetU32(&pClient->Pending.paParms[1], 0);
1139 }
1140 g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
1141 pClient->Pending.uType = 0;
1142 }
1143
1144 shclSvcClientStateReset(&pClient->State);
1145 shclSvcClientStateDestroy(&pClient->State);
1146
1147 SharedClipboardEventSourceDestroy(&pClient->Events);
1148
1149 ClipboardClientMap::iterator itClient = g_mapClients.find(u32ClientID);
1150 if (itClient != g_mapClients.end())
1151 {
1152 g_mapClients.erase(itClient);
1153 }
1154 else
1155 AssertFailed();
1156
1157 return VINF_SUCCESS;
1158}
1159
1160static DECLCALLBACK(int) svcConnect(void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
1161{
1162 RT_NOREF(fRequestor, fRestoring);
1163
1164 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1165 AssertPtr(pvClient);
1166
1167 /* Assign the client ID. */
1168 pClient->State.uClientID = u32ClientID;
1169
1170 /* Create the client's own event source. */
1171 int rc = SharedClipboardEventSourceCreate(&pClient->Events, 0 /* ID, ignored */);
1172 if (RT_SUCCESS(rc))
1173 {
1174 LogFlowFunc(("[Client %RU32] Using event source %RU32\n", u32ClientID, pClient->Events.uID));
1175
1176 /* Reset the client state. */
1177 shclSvcClientStateReset(&pClient->State);
1178
1179 /* (Re-)initialize the client state. */
1180 rc = shclSvcClientStateInit(&pClient->State, u32ClientID);
1181 if (RT_SUCCESS(rc))
1182 {
1183 rc = ShClSvcImplConnect(pClient, ShClSvcGetHeadless());
1184#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1185 if (RT_SUCCESS(rc))
1186 rc = SharedClipboardTransferCtxInit(&pClient->TransferCtx);
1187#endif
1188 if (RT_SUCCESS(rc))
1189 {
1190 /* Assign weak pointer to client map .*/
1191 g_mapClients[u32ClientID] = pClient; /** @todo Handle OOM / collisions? */
1192
1193 /* For now we ASSUME that the first client ever connected is in charge for
1194 * communicating withe the service extension.
1195 *
1196 ** @todo This needs to be fixed ASAP w/o breaking older guest / host combos. */
1197 if (g_ExtState.uClientID == 0)
1198 g_ExtState.uClientID = u32ClientID;
1199 }
1200 }
1201 }
1202
1203 LogFlowFuncLeaveRC(rc);
1204 return rc;
1205}
1206
1207static DECLCALLBACK(void) svcCall(void *,
1208 VBOXHGCMCALLHANDLE callHandle,
1209 uint32_t u32ClientID,
1210 void *pvClient,
1211 uint32_t u32Function,
1212 uint32_t cParms,
1213 VBOXHGCMSVCPARM paParms[],
1214 uint64_t tsArrival)
1215{
1216 RT_NOREF(u32ClientID, pvClient, tsArrival);
1217
1218 int rc = VINF_SUCCESS;
1219
1220 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1221 AssertPtr(pClient);
1222
1223 LogFunc(("u32ClientID=%RU32 (proto %RU32), fn=%RU32 (%s), cParms=%RU32, paParms=%p\n",
1224 u32ClientID, pClient->State.uProtocolVer, u32Function, VBoxShClGuestMsgToStr(u32Function), cParms, paParms));
1225
1226#ifdef DEBUG
1227 uint32_t i;
1228
1229 for (i = 0; i < cParms; i++)
1230 {
1231 /** @todo parameters other than 32 bit */
1232 LogFunc((" paParms[%d]: type %RU32 - value %RU32\n", i, paParms[i].type, paParms[i].u.uint32));
1233 }
1234#endif
1235
1236 switch (u32Function)
1237 {
1238 case VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD:
1239 {
1240 rc = shclSvcMsgGetOld(pClient, callHandle, cParms, paParms);
1241 break;
1242 }
1243
1244 case VBOX_SHCL_GUEST_FN_CONNECT:
1245 {
1246 if (cParms != VBOX_SHCL_CPARMS_CONNECT)
1247 {
1248 rc = VERR_INVALID_PARAMETER;
1249 }
1250 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uProtocolVer */
1251 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uProtocolFlags */
1252 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* cbChunkSize */
1253 || paParms[3].type != VBOX_HGCM_SVC_PARM_32BIT /* enmCompression */
1254 || paParms[4].type != VBOX_HGCM_SVC_PARM_32BIT) /* enmChecksumType */
1255 {
1256 rc = VERR_INVALID_PARAMETER;
1257 }
1258 else if (shclSvcGetMode() == VBOX_SHCL_MODE_OFF)
1259 {
1260 rc = VERR_ACCESS_DENIED;
1261 }
1262 else
1263 {
1264 /* Update the protocol version and tell the guest. */
1265 pClient->State.uProtocolVer = 1;
1266
1267 LogFlowFunc(("Now using protocol v%RU32\n", pClient->State.uProtocolVer));
1268
1269 HGCMSvcSetU32(&paParms[0], pClient->State.uProtocolVer);
1270 HGCMSvcSetU32(&paParms[1], 0 /* Procotol flags, not used yet */);
1271 HGCMSvcSetU32(&paParms[2], pClient->State.cbChunkSize);
1272 HGCMSvcSetU32(&paParms[3], 0 /* Compression type, not used yet */);
1273 HGCMSvcSetU32(&paParms[4], 0 /* Checksum type, not used yet */);
1274
1275 rc = VINF_SUCCESS;
1276 }
1277
1278 break;
1279 }
1280
1281 case VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT:
1282 {
1283 rc = shclSvcMsgPeek(pClient, callHandle, cParms, paParms, false /*fWait*/);
1284 break;
1285 }
1286
1287 case VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT:
1288 {
1289 rc = shclSvcMsgPeek(pClient, callHandle, cParms, paParms, true /*fWait*/);
1290 break;
1291 }
1292
1293 case VBOX_SHCL_GUEST_FN_MSG_GET:
1294 {
1295 rc = shclSvcMsgGet(pClient, callHandle, cParms, paParms);
1296 break;
1297 }
1298
1299 case VBOX_SHCL_GUEST_FN_FORMATS_REPORT:
1300 {
1301 uint32_t uFormats = 0;
1302
1303 if (pClient->State.uProtocolVer == 0)
1304 {
1305 if (cParms != 1)
1306 {
1307 rc = VERR_INVALID_PARAMETER;
1308 }
1309 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT) /* uFormats */
1310 {
1311 rc = VERR_INVALID_PARAMETER;
1312 }
1313 else
1314 {
1315 rc = HGCMSvcGetU32(&paParms[0], &uFormats);
1316 }
1317 }
1318 else
1319 {
1320 if (cParms != 3)
1321 {
1322 rc = VERR_INVALID_PARAMETER;
1323 }
1324 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uContextID */
1325 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uFormats */
1326 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT) /* fFlags */
1327 {
1328 rc = VERR_INVALID_PARAMETER;
1329 }
1330 else
1331 {
1332 rc = HGCMSvcGetU32(&paParms[1], &uFormats);
1333
1334 /** @todo Handle rest. */
1335 }
1336 }
1337
1338 if (RT_SUCCESS(rc))
1339 {
1340 if ( shclSvcGetMode() != VBOX_SHCL_MODE_GUEST_TO_HOST
1341 && shclSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
1342 {
1343 rc = VERR_ACCESS_DENIED;
1344 }
1345 else if (uFormats != VBOX_SHCL_FMT_NONE) /* Only announce formats if we actually *have* formats to announce! */
1346 {
1347 rc = shclSvcSetSource(pClient, SHCLSOURCE_REMOTE);
1348 if (RT_SUCCESS(rc))
1349 {
1350 if (g_ExtState.pfnExtension)
1351 {
1352 SHCLEXTPARMS parms;
1353 RT_ZERO(parms);
1354
1355 parms.uFormat = uFormats;
1356
1357 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1358 &parms, sizeof(parms));
1359 }
1360
1361 SHCLCLIENTCMDCTX cmdCtx;
1362 RT_ZERO(cmdCtx);
1363
1364 SHCLFORMATDATA formatData;
1365 RT_ZERO(formatData);
1366
1367 formatData.uFormats = uFormats;
1368 Assert(formatData.uFormats != VBOX_SHCL_FMT_NONE); /* Sanity. */
1369
1370 rc = ShClSvcImplFormatAnnounce(pClient, &cmdCtx, &formatData);
1371 }
1372 }
1373 }
1374
1375 break;
1376 }
1377
1378 case VBOX_SHCL_GUEST_FN_DATA_READ:
1379 {
1380 if (cParms != VBOX_SHCL_CPARMS_READ_DATA)
1381 {
1382 rc = VERR_INVALID_PARAMETER;
1383 }
1384 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
1385 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
1386 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* size */
1387 )
1388 {
1389 rc = VERR_INVALID_PARAMETER;
1390 }
1391 else
1392 {
1393 if ( shclSvcGetMode() != VBOX_SHCL_MODE_HOST_TO_GUEST
1394 && shclSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
1395 {
1396 rc = VERR_ACCESS_DENIED;
1397 break;
1398 }
1399
1400 uint32_t uFormat;
1401 rc = HGCMSvcGetU32(&paParms[0], &uFormat);
1402 if (RT_SUCCESS(rc))
1403 {
1404 void *pv;
1405 uint32_t cb;
1406 rc = HGCMSvcGetBuf(&paParms[1], &pv, &cb);
1407 if (RT_SUCCESS(rc))
1408 {
1409 uint32_t cbActual = 0;
1410
1411 /* If there is a service extension active, try reading data from it first. */
1412 if (g_ExtState.pfnExtension)
1413 {
1414 SHCLEXTPARMS parms;
1415 RT_ZERO(parms);
1416
1417 parms.uFormat = uFormat;
1418 parms.u.pvData = pv;
1419 parms.cbData = cb;
1420
1421 g_ExtState.fReadingData = true;
1422
1423 /* Read clipboard data from the extension. */
1424 rc = g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1425 &parms, sizeof(parms));
1426
1427 LogFlowFunc(("g_ExtState.fDelayedAnnouncement=%RTbool, g_ExtState.uDelayedFormats=0x%x\n",
1428 g_ExtState.fDelayedAnnouncement, g_ExtState.uDelayedFormats));
1429
1430 /* Did the extension send the clipboard formats yet?
1431 * Otherwise, do this now. */
1432 if (g_ExtState.fDelayedAnnouncement)
1433 {
1434 SHCLFORMATDATA formatData;
1435 RT_ZERO(formatData);
1436
1437 formatData.uFormats = g_ExtState.uDelayedFormats;
1438 Assert(formatData.uFormats != VBOX_SHCL_FMT_NONE); /* There better is *any* format here now. */
1439
1440 int rc2 = shclSvcFormatsReport(pClient, &formatData);
1441 AssertRC(rc2);
1442
1443 g_ExtState.fDelayedAnnouncement = false;
1444 g_ExtState.uDelayedFormats = 0;
1445 }
1446
1447 g_ExtState.fReadingData = false;
1448
1449 if (RT_SUCCESS(rc))
1450 {
1451 cbActual = parms.cbData;
1452 }
1453 }
1454
1455 /* Note: The host clipboard *always* has precedence over the service extension above,
1456 * so data which has been read above might get overridden by the host clipboard eventually. */
1457
1458 SHCLCLIENTCMDCTX cmdCtx;
1459 RT_ZERO(cmdCtx);
1460
1461 /* Release any other pending read, as we only
1462 * support one pending read at one time. */
1463 if (RT_SUCCESS(rc))
1464 {
1465 SHCLDATABLOCK dataBlock;
1466 RT_ZERO(dataBlock);
1467
1468 dataBlock.pvData = pv;
1469 dataBlock.cbData = cb;
1470 dataBlock.uFormat = uFormat;
1471
1472 rc = ShClSvcImplReadData(pClient, &cmdCtx, &dataBlock, &cbActual);
1473 if (RT_SUCCESS(rc))
1474 HGCMSvcSetU32(&paParms[2], cbActual);
1475 }
1476 }
1477 }
1478 }
1479
1480 break;
1481 }
1482
1483 case VBOX_SHCL_GUEST_FN_DATA_WRITE:
1484 {
1485 rc = shclSvcGetDataWrite(pClient, cParms, paParms);
1486 break;
1487 }
1488
1489 default:
1490 {
1491#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1492 rc = shclSvcTransferHandler(pClient, callHandle, u32Function, cParms, paParms, tsArrival);
1493#else
1494 rc = VERR_NOT_IMPLEMENTED;
1495#endif
1496 break;
1497 }
1498 }
1499
1500 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
1501
1502 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1503 g_pHelpers->pfnCallComplete(callHandle, rc);
1504}
1505
1506/**
1507 * Initializes a Shared Clipboard service's client state.
1508 *
1509 * @returns VBox status code.
1510 * @param pClientState Client state to initialize.
1511 * @param uClientID Client ID (HGCM) to use for this client state.
1512 */
1513static int shclSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID)
1514{
1515 LogFlowFuncEnter();
1516
1517 shclSvcClientStateReset(pClientState);
1518
1519 /* Register the client. */
1520 pClientState->uClientID = uClientID;
1521
1522 return VINF_SUCCESS;
1523}
1524
1525/**
1526 * Destroys a Shared Clipboard service's client state.
1527 *
1528 * @returns VBox status code.
1529 * @param pClientState Client state to destroy.
1530 */
1531static int shclSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState)
1532{
1533 RT_NOREF(pClientState);
1534
1535 LogFlowFuncEnter();
1536
1537 return VINF_SUCCESS;
1538}
1539
1540/**
1541 * Resets a Shared Clipboard service's client state.
1542 *
1543 * @param pClientState Client state to reset.
1544 */
1545static void shclSvcClientStateReset(PSHCLCLIENTSTATE pClientState)
1546{
1547 LogFlowFuncEnter();
1548
1549 pClientState->uProtocolVer = 0;
1550 pClientState->cbChunkSize = _64K; /** Make this configurable. */
1551
1552#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1553 pClientState->Transfers.enmTransferDir = SHCLTRANSFERDIR_UNKNOWN;
1554#else
1555 RT_NOREF(pClientState);
1556#endif
1557}
1558
1559/*
1560 * We differentiate between a function handler for the guest and one for the host.
1561 */
1562static DECLCALLBACK(int) svcHostCall(void *,
1563 uint32_t u32Function,
1564 uint32_t cParms,
1565 VBOXHGCMSVCPARM paParms[])
1566{
1567 int rc = VINF_SUCCESS;
1568
1569 LogFlowFunc(("u32Function=%RU32 (%s), cParms=%RU32, paParms=%p\n",
1570 u32Function, VBoxShClHostFunctionToStr(u32Function), cParms, paParms));
1571
1572 switch (u32Function)
1573 {
1574 case VBOX_SHCL_HOST_FN_SET_MODE:
1575 {
1576 if (cParms != 1)
1577 {
1578 rc = VERR_INVALID_PARAMETER;
1579 }
1580 else
1581 {
1582 uint32_t u32Mode = VBOX_SHCL_MODE_OFF;
1583
1584 rc = HGCMSvcGetU32(&paParms[0], &u32Mode);
1585 if (RT_SUCCESS(rc))
1586 rc = shclSvcModeSet(u32Mode);
1587 }
1588
1589 break;
1590 }
1591
1592 case VBOX_SHCL_HOST_FN_SET_HEADLESS:
1593 {
1594 if (cParms != 1)
1595 {
1596 rc = VERR_INVALID_PARAMETER;
1597 }
1598 else
1599 {
1600 uint32_t uHeadless;
1601 rc = HGCMSvcGetU32(&paParms[0], &uHeadless);
1602 if (RT_SUCCESS(rc))
1603 {
1604 g_fHeadless = RT_BOOL(uHeadless);
1605 LogRel(("Shared Clipboard: Service running in %s mode\n", g_fHeadless ? "headless" : "normal"));
1606 }
1607 }
1608 break;
1609 }
1610
1611 default:
1612 {
1613#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1614 rc = shclSvcTransferHostHandler(u32Function, cParms, paParms);
1615#else
1616 rc = VERR_NOT_IMPLEMENTED;
1617#endif
1618 break;
1619 }
1620 }
1621
1622 LogFlowFuncLeaveRC(rc);
1623 return rc;
1624}
1625
1626#ifndef UNIT_TEST
1627/**
1628 * SSM descriptor table for the SHCLCLIENTSTATE structure.
1629 */
1630static SSMFIELD const s_aShClSSMClientState[] =
1631{
1632 SSMFIELD_ENTRY(SHCLCLIENTSTATE, uProtocolVer),
1633 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
1634 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
1635 SSMFIELD_ENTRY_TERM()
1636};
1637
1638/**
1639 * SSM descriptor table for the SHCLCLIENTURISTATE structure.
1640 */
1641static SSMFIELD const s_aShClSSMClientURIState[] =
1642{
1643 SSMFIELD_ENTRY(SHCLCLIENTTRANSFERSTATE, enmTransferDir),
1644 SSMFIELD_ENTRY_TERM()
1645};
1646
1647/**
1648 * SSM descriptor table for the header of the SHCLCLIENTMSG structure.
1649 * The actual message parameters will be serialized separately.
1650 */
1651static SSMFIELD const s_aShClSSMClientMsgHdr[] =
1652{
1653 SSMFIELD_ENTRY(SHCLCLIENTMSG, uMsg),
1654 SSMFIELD_ENTRY(SHCLCLIENTMSG, cParms),
1655 SSMFIELD_ENTRY_TERM()
1656};
1657
1658/**
1659 * SSM descriptor table for the VBOXSHCLMSGCTX structure.
1660 */
1661static SSMFIELD const s_aShClSSMClientMsgCtx[] =
1662{
1663 SSMFIELD_ENTRY(SHCLMSGCTX, uContextID),
1664 SSMFIELD_ENTRY_TERM()
1665};
1666#endif /* !UNIT_TEST */
1667
1668static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
1669{
1670 RT_NOREF(u32ClientID);
1671
1672#ifndef UNIT_TEST
1673 /*
1674 * When the state will be restored, pending requests will be reissued
1675 * by VMMDev. The service therefore must save state as if there were no
1676 * pending request.
1677 * Pending requests, if any, will be completed in svcDisconnect.
1678 */
1679 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1680
1681 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1682 AssertPtr(pClient);
1683
1684 /* Write Shared Clipboard saved state version. */
1685 SSMR3PutU32(pSSM, VBOX_SHCL_SSM_VER_1);
1686
1687 int rc = SSMR3PutStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
1688 AssertRCReturn(rc, rc);
1689
1690 rc = SSMR3PutStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientURIState[0], NULL);
1691 AssertRCReturn(rc, rc);
1692
1693 /* Serialize the client's internal message queue. */
1694 rc = SSMR3PutU64(pSSM, (uint64_t)pClient->queueMsg.size());
1695 AssertRCReturn(rc, rc);
1696
1697 for (size_t i = 0; i < pClient->queueMsg.size(); i++)
1698 {
1699 PSHCLCLIENTMSG pMsg = pClient->queueMsg.at(i);
1700 AssertPtr(pMsg);
1701
1702 rc = SSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
1703 AssertRCReturn(rc, rc);
1704
1705 rc = SSMR3PutStructEx(pSSM, &pMsg->Ctx, sizeof(SHCLMSGCTX), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
1706 AssertRCReturn(rc, rc);
1707
1708 for (uint32_t p = 0; p < pMsg->cParms; p++)
1709 {
1710 rc = HGCMSvcSSMR3Put(&pMsg->paParms[p], pSSM);
1711 AssertRCReturn(rc, rc);
1712 }
1713 }
1714
1715#else /* UNIT_TEST */
1716 RT_NOREF3(u32ClientID, pvClient, pSSM);
1717#endif /* UNIT_TEST */
1718 return VINF_SUCCESS;
1719}
1720
1721#ifndef UNIT_TEST
1722static int svcLoadStateV0(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
1723{
1724 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
1725
1726 uint32_t uMarker;
1727 int rc = SSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
1728 AssertRC(rc);
1729 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
1730
1731 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Client ID */
1732 AssertRCReturn(rc, rc);
1733
1734 bool fValue;
1735 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgQuit */
1736 AssertRCReturn(rc, rc);
1737
1738 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgReadData */
1739 AssertRCReturn(rc, rc);
1740
1741 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgFormats */
1742 AssertRCReturn(rc, rc);
1743
1744 uint32_t fFormats;
1745 rc = SSMR3GetU32(pSSM, &fFormats); /* u32RequestedFormat */
1746 AssertRCReturn(rc, rc);
1747
1748 rc = SSMR3GetU32(pSSM, &uMarker); /* End marker. */
1749 AssertRCReturn(rc, rc);
1750 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
1751
1752 return VINF_SUCCESS;
1753}
1754#endif /* UNIT_TEST */
1755
1756static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
1757{
1758#ifndef UNIT_TEST
1759 RT_NOREF(u32ClientID, uVersion);
1760
1761 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1762
1763 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1764 AssertPtr(pClient);
1765
1766 /* Restore the client data. */
1767 uint32_t lenOrVer;
1768 int rc = SSMR3GetU32(pSSM, &lenOrVer);
1769 AssertRCReturn(rc, rc);
1770 if (lenOrVer == VBOX_SHCL_SSM_VER_0)
1771 {
1772 return svcLoadStateV0(u32ClientID, pvClient, pSSM, uVersion);
1773 }
1774 else if (lenOrVer == VBOX_SHCL_SSM_VER_1)
1775 {
1776 rc = SSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
1777 AssertRCReturn(rc, rc);
1778
1779 rc = SSMR3GetStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientURIState[0], NULL);
1780 AssertRCReturn(rc, rc);
1781
1782 /* Load the client's internal message queue. */
1783 uint64_t cMsg;
1784 rc = SSMR3GetU64(pSSM, &cMsg);
1785 AssertRCReturn(rc, rc);
1786
1787 for (uint64_t i = 0; i < cMsg; i++)
1788 {
1789 PSHCLCLIENTMSG pMsg = (PSHCLCLIENTMSG)RTMemAlloc(sizeof(SHCLCLIENTMSG));
1790 AssertPtrReturn(pMsg, VERR_NO_MEMORY);
1791
1792 rc = SSMR3GetStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
1793 AssertRCReturn(rc, rc);
1794
1795 rc = SSMR3GetStructEx(pSSM, &pMsg->Ctx, sizeof(SHCLMSGCTX), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
1796 AssertRCReturn(rc, rc);
1797
1798 pMsg->paParms = (PVBOXHGCMSVCPARM)RTMemAllocZ(sizeof(VBOXHGCMSVCPARM) * pMsg->cParms);
1799 AssertPtrReturn(pMsg->paParms, VERR_NO_MEMORY);
1800
1801 for (uint32_t p = 0; p < pMsg->cParms; p++)
1802 {
1803 rc = HGCMSvcSSMR3Get(&pMsg->paParms[p], pSSM);
1804 AssertRCReturn(rc, rc);
1805 }
1806
1807 rc = shclSvcMsgAdd(pClient, pMsg, true /* fAppend */);
1808 AssertRCReturn(rc, rc);
1809 }
1810 }
1811 else
1812 {
1813 LogRel(("Shared Clipboard: Unknown saved state version (%#x)\n", lenOrVer));
1814 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1815 }
1816
1817 /* Actual host data are to be reported to guest (SYNC). */
1818 ShClSvcImplSync(pClient);
1819
1820#else /* UNIT_TEST */
1821 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
1822#endif /* UNIT_TEST */
1823 return VINF_SUCCESS;
1824}
1825
1826static DECLCALLBACK(int) extCallback(uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
1827{
1828 RT_NOREF(pvData, cbData);
1829
1830 LogFlowFunc(("u32Function=%RU32\n", u32Function));
1831
1832 int rc = VINF_SUCCESS;
1833
1834 /* Figure out if the client in charge for the service extension still is connected. */
1835 ClipboardClientMap::const_iterator itClient = g_mapClients.find(g_ExtState.uClientID);
1836 if (itClient != g_mapClients.end())
1837 {
1838 PSHCLCLIENT pClient = itClient->second;
1839 AssertPtr(pClient);
1840
1841 switch (u32Function)
1842 {
1843 /* The service extension announces formats to the guest. */
1844 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1845 {
1846 LogFlowFunc(("VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE: g_ExtState.fReadingData=%RTbool\n", g_ExtState.fReadingData));
1847 if (g_ExtState.fReadingData)
1848 {
1849 g_ExtState.fDelayedAnnouncement = true;
1850 g_ExtState.uDelayedFormats = u32Format;
1851 }
1852 else if (u32Format != VBOX_SHCL_FMT_NONE)
1853 {
1854 SHCLFORMATDATA formatData;
1855 RT_ZERO(formatData);
1856
1857 formatData.uFormats = u32Format;
1858
1859 rc = shclSvcFormatsReport(pClient, &formatData);
1860 }
1861
1862 break;
1863 }
1864
1865 /* The service extension wants read data from the guest. */
1866 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1867 {
1868 SHCLDATAREQ dataReq;
1869 RT_ZERO(dataReq);
1870
1871 dataReq.uFmt = u32Format;
1872 dataReq.cbSize = _64K; /** @todo Make this more dynamic. */
1873
1874 rc = shclSvcDataReadRequest(pClient, &dataReq, NULL /* puEvent */);
1875 break;
1876 }
1877
1878 default:
1879 /* Just skip other messages. */
1880 break;
1881 }
1882 }
1883 else
1884 rc = VERR_NOT_FOUND;
1885
1886 LogFlowFuncLeaveRC(rc);
1887 return rc;
1888}
1889
1890static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
1891{
1892 LogFlowFunc(("pfnExtension=%p\n", pfnExtension));
1893
1894 SHCLEXTPARMS parms;
1895 RT_ZERO(parms);
1896
1897 if (pfnExtension)
1898 {
1899 /* Install extension. */
1900 g_ExtState.pfnExtension = pfnExtension;
1901 g_ExtState.pvExtension = pvExtension;
1902
1903 parms.u.pfnCallback = extCallback;
1904
1905 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
1906 }
1907 else
1908 {
1909 if (g_ExtState.pfnExtension)
1910 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
1911
1912 /* Uninstall extension. */
1913 g_ExtState.pfnExtension = NULL;
1914 g_ExtState.pvExtension = NULL;
1915 }
1916
1917 return VINF_SUCCESS;
1918}
1919
1920extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
1921{
1922 int rc = VINF_SUCCESS;
1923
1924 LogFlowFunc(("ptable=%p\n", pTable));
1925
1926 if (!pTable)
1927 {
1928 rc = VERR_INVALID_PARAMETER;
1929 }
1930 else
1931 {
1932 LogFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", pTable->cbSize, pTable->u32Version));
1933
1934 if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1935 || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
1936 {
1937 rc = VERR_INVALID_PARAMETER;
1938 }
1939 else
1940 {
1941 g_pHelpers = pTable->pHelpers;
1942
1943 pTable->cbClient = sizeof(SHCLCLIENT);
1944
1945 pTable->pfnUnload = svcUnload;
1946 pTable->pfnConnect = svcConnect;
1947 pTable->pfnDisconnect = svcDisconnect;
1948 pTable->pfnCall = svcCall;
1949 pTable->pfnHostCall = svcHostCall;
1950 pTable->pfnSaveState = svcSaveState;
1951 pTable->pfnLoadState = svcLoadState;
1952 pTable->pfnRegisterExtension = svcRegisterExtension;
1953 pTable->pfnNotify = NULL;
1954 pTable->pvService = NULL;
1955
1956 /* Service specific initialization. */
1957 rc = svcInit();
1958 }
1959 }
1960
1961 return rc;
1962}
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