VirtualBox

Changeset 80468 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Aug 28, 2019 9:57:23 AM (5 years ago)
Author:
vboxsync
Message:

Shared Clipboard/URI: Made the event source and event IDs more non-deterministic.

Location:
trunk/src/VBox
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/GuestHost/SharedClipboard/clipboard-common.cpp

    r80459 r80468  
    2424#include <iprt/semaphore.h>
    2525#include <iprt/path.h>
     26#include <iprt/rand.h>
    2627
    2728#include <iprt/errcore.h>
     
    3536 *
    3637 * @returns VBox status code.
    37  * @param   uID                 Event ID to associate payload to.
     38 * @param   uID                 Payload ID to set for this payload. Useful for consequtive payloads.
    3839 * @param   pvData              Data block to associate to this payload.
    3940 * @param   cbData              Size (in bytes) of data block to associate.
     
    9798 * @returns VBox status code.
    9899 * @param   pEvent              Event to initialize.
    99  * @param   uID                 ID to use for the event.
    100  */
    101 int SharedClipboardEventCreate(PSHAREDCLIPBOARDEVENT pEvent, uint16_t uID)
     100 * @param   uID                 Event ID to use.
     101 */
     102int SharedClipboardEventCreate(PSHAREDCLIPBOARDEVENT pEvent, VBOXCLIPBOARDEVENTID uID)
    102103{
    103104    AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
     
    145146 * @param   uID                 ID to use for event source.
    146147 */
    147 int SharedClipboardEventSourceCreate(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
     148int SharedClipboardEventSourceCreate(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTSOURCEID uID)
    148149{
    149150    AssertPtrReturn(pSource, VERR_INVALID_POINTER);
     
    156157
    157158    pSource->uID          = uID;
    158     pSource->uEventIDNext = 1; /* Event ID 0 always is reserved (marks "unused"). */
     159    /* Choose a random event ID starting point. */
     160    pSource->uEventIDNext = RTRandU32() % VBOX_SHARED_CLIPBOARD_MAX_EVENTS;
    159161
    160162    LogFlowFuncLeaveRC(rc);
     
    189191 * @param   pSource             Event source to generate event for.
    190192 */
    191 uint16_t SharedClipboardEventIDGenerate(PSHAREDCLIPBOARDEVENTSOURCE pSource)
     193VBOXCLIPBOARDEVENTID SharedClipboardEventIDGenerate(PSHAREDCLIPBOARDEVENTSOURCE pSource)
    192194{
    193195    AssertPtrReturn(pSource, 0);
    194196
    195197    LogFlowFunc(("uSource=%RU16: New event: %RU16\n", pSource->uID, pSource->uEventIDNext));
    196     return pSource->uEventIDNext++; /** @todo Improve this. */
     198    return pSource->uEventIDNext++; /** @todo Handle rollovers? */
    197199}
    198200
     
    204206 * @param   uID                 Event ID to get.
    205207 */
    206 inline PSHAREDCLIPBOARDEVENT sharedClipboardEventGet(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
     208inline PSHAREDCLIPBOARDEVENT sharedClipboardEventGet(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
    207209{
    208210    PSHAREDCLIPBOARDEVENT pEvIt;
     
    223225 * @param   uID                 Event ID to register.
    224226 */
    225 int SharedClipboardEventRegister(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
     227int SharedClipboardEventRegister(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
    226228{
    227229    AssertPtrReturn(pSource, VERR_INVALID_POINTER);
     
    266268 * @param   uID                 Event ID to unregister.
    267269 */
    268 int SharedClipboardEventUnregister(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
     270int SharedClipboardEventUnregister(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
    269271{
    270272    AssertPtrReturn(pSource, VERR_INVALID_POINTER);
     
    305307 *                              SharedClipboardPayloadFree().
    306308 */
    307 int SharedClipboardEventWait(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID, RTMSINTERVAL uTimeoutMs,
     309int SharedClipboardEventWait(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID, RTMSINTERVAL uTimeoutMs,
    308310                             PSHAREDCLIPBOARDEVENTPAYLOAD* ppPayload)
    309311{
     
    340342 * @param   pPayload            Event payload to associate. Takes ownership. Optional.
    341343 */
    342 int SharedClipboardEventSignal(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID,
     344int SharedClipboardEventSignal(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID,
    343345                               PSHAREDCLIPBOARDEVENTPAYLOAD pPayload)
    344346{
     
    376378 * @param   uID                 Event ID to detach payload for.
    377379 */
    378 void SharedClipboardEventPayloadDetach(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
     380void SharedClipboardEventPayloadDetach(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
    379381{
    380382    AssertPtrReturnVoid(pSource);
  • trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-internal.h

    r80444 r80468  
    181181} VBOXCLIPBOARDCLIENT, *PVBOXCLIPBOARDCLIENT;
    182182
     183/**
     184 * Structure for keeping a single (HGCM) client map entry.
     185 * Currently empty.
     186 */
    183187typedef struct _VBOXCLIPBOARDCLIENTMAPENTRY
    184188{
    185189} VBOXCLIPBOARDCLIENTMAPENTRY;
    186190
    187 /** Map holding pointers to drag and drop clients. Key is the (unique) HGCM client ID. */
     191/**
     192 * Structure for keeping a single event source map entry.
     193 * Currently empty.
     194 */
     195typedef struct _VBOXCLIPBOARDEVENTSOURCEMAPENTRY
     196{
     197} VBOXCLIPBOARDEVENTSOURCEMAPENTRY;
     198
     199/** Map holding information about connected HGCM clients. Key is the (unique) HGCM client ID. */
    188200typedef std::map<uint32_t, VBOXCLIPBOARDCLIENTMAPENTRY> ClipboardClientMap;
     201
     202/** Map holding information about event sources. Key is the (unique) event source ID. */
     203typedef std::map<VBOXCLIPBOARDEVENTSOURCEID, VBOXCLIPBOARDEVENTSOURCEMAPENTRY> ClipboardEventSourceMap;
    189204
    190205/** Simple queue (list) which holds deferred (waiting) clients. */
  • trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-uri.cpp

    r80444 r80468  
    8989    if (pMsgHdr)
    9090    {
    91         uint16_t uEventHdrRead = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    92 
    93         HGCMSvcSetU32(&pMsgHdr->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEventHdrRead));
     91        VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     92
     93        HGCMSvcSetU32(&pMsgHdr->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
    9494        HGCMSvcSetU32(&pMsgHdr->m_paParms[1], 0 /* fRoots */);
    9595
     
    9797        if (RT_SUCCESS(rc))
    9898        {
    99             int rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEventHdrRead);
     99            int rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEvent);
    100100            AssertRC(rc2);
    101101
     
    104104            {
    105105                PSHAREDCLIPBOARDEVENTPAYLOAD pPayloadHdr;
    106                 rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEventHdrRead,
     106                rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEvent,
    107107                                              pCtx->pTransfer->uTimeoutMs, &pPayloadHdr);
    108108                if (RT_SUCCESS(rc))
     
    128128                                                                                                 VBOX_SHARED_CLIPBOARD_CPARMS_ROOT_LIST_ENTRY_READ_REQ);
    129129
    130                                     uint16_t uEventEntryRead = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     130                                    uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    131131
    132132                                    HGCMSvcSetU32(&pMsgEntry->m_paParms[0],
    133                                                   VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEventEntryRead));
     133                                                  VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
    134134                                    HGCMSvcSetU32(&pMsgEntry->m_paParms[1], 0 /* fRoots */);
    135135                                    HGCMSvcSetU32(&pMsgEntry->m_paParms[2], i /* uIndex */);
    136136
    137                                     rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEventEntryRead);
     137                                    rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEvent);
    138138                                    AssertRC(rc2);
    139139
     
    143143
    144144                                    PSHAREDCLIPBOARDEVENTPAYLOAD pPayloadEntry;
    145                                     rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEventEntryRead,
     145                                    rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEvent,
    146146                                                                  pCtx->pTransfer->uTimeoutMs, &pPayloadEntry);
    147147                                    if (RT_FAILURE(rc))
     
    155155                                    SharedClipboardPayloadFree(pPayloadEntry);
    156156
    157                                     SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEventEntryRead);
     157                                    SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEvent);
    158158
    159159                                    if (RT_FAILURE(rc))
     
    182182            }
    183183
    184             SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEventHdrRead);
     184            SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEvent);
    185185        }
    186186    }
     
    206206    if (pMsg)
    207207    {
    208         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     208        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    209209
    210210        pMsg->m_Ctx.uContextID = VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent);
     
    263263    if (pMsg)
    264264    {
    265         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     265        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    266266
    267267        pMsg->m_Ctx.uContextID = VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent);
     
    310310    if (pMsg)
    311311    {
    312         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     312        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    313313
    314314        HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
     
    370370    if (pMsg)
    371371    {
    372         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     372        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    373373
    374374        HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
     
    429429    if (pMsg)
    430430    {
    431         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     431        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    432432
    433433        LogFlowFunc(("pszPath=%s, fCreate=0x%x\n", pCreateParms->pszPath, pCreateParms->fCreate));
     
    488488    if (pMsg)
    489489    {
    490         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     490        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    491491
    492492        HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
     
    541541    if (pMsg)
    542542    {
    543         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     543        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    544544
    545545        HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
     
    599599    if (pMsg)
    600600    {
    601         const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
     601        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
    602602
    603603        HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
     
    11811181                        if (RT_SUCCESS(rc))
    11821182                        {
    1183                             const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
     1183                            const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
    11841184
    11851185                            LogFlowFunc(("uCID=%RU32 -> uEvent=%RU32\n", uCID, uEvent));
     
    14221422                if (RT_SUCCESS(rc))
    14231423                {
    1424                     const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
     1424                    const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
    14251425
    14261426                    PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
     
    14711471                if (RT_SUCCESS(rc))
    14721472                {
    1473                     const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
     1473                    const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
    14741474
    14751475                    PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
     
    15511551                    if (RT_SUCCESS(rc))
    15521552                    {
    1553                         const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
     1553                        const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
    15541554
    15551555                        PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
     
    15991599                    if (RT_SUCCESS(rc))
    16001600                    {
    1601                         const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
     1601                        const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
    16021602
    16031603                        PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
     
    16451645                if (RT_SUCCESS(rc))
    16461646                {
    1647                     const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
     1647                    const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
    16481648
    16491649                    PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
  • trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc.cpp

    r80444 r80468  
    186186#include <iprt/assert.h>
    187187#include <iprt/critsect.h>
     188#include <iprt/rand.h>
    188189
    189190#include <VBox/err.h>
     
    228229ClipboardClientMap g_mapClients;
    229230
     231/** Global map of all registered event sources. */
     232ClipboardEventSourceMap g_mapEventSources;
     233
    230234/** Global list of all clients which are queued up (deferred return) and ready
    231235 *  to process new commands. The key is the (unique) client ID. */
    232236ClipboardClientQueue g_listClientsDeferred;
    233237
     238
     239/**
     240 * Creates a (unique) event source ID.
     241 *
     242 * @returns VBox status code, or VERR_NOT_FOUND on error.
     243 * @param   puID                Where to store the created event source ID on success.
     244 */
     245int vboxSvcClipboardEventSourceCreateID(PVBOXCLIPBOARDEVENTSOURCEID puID)
     246{
     247    AssertPtrReturn(puID, VERR_INVALID_POINTER);
     248
     249    for (uint32_t i = 0; i < 32; i++) /* Don't try too hard. */
     250    {
     251        VBOXCLIPBOARDEVENTSOURCEID uID = RTRandU32() % VBOX_SHARED_CLIPBOARD_MAX_EVENT_SOURCES;
     252        if (g_mapEventSources.find(uID) == g_mapEventSources.end())
     253        {
     254            *puID = uID;
     255            return VINF_SUCCESS;
     256        }
     257    }
     258
     259    return VERR_NOT_FOUND;
     260}
    234261
    235262uint32_t vboxSvcClipboardGetMode(void)
     
    738765    if (pMsg)
    739766    {
    740         uint16_t uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
     767        VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
    741768
    742769        HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pClient->Events.uID, uEvent));
     
    10351062    RT_NOREF(fRequestor, fRestoring);
    10361063
    1037     int rc = VINF_SUCCESS;
    1038 
    10391064    PVBOXCLIPBOARDCLIENT pClient = (PVBOXCLIPBOARDCLIENT)pvClient;
    10401065    AssertPtr(pvClient);
     
    10441069
    10451070    /* Create the client's own event source. */
    1046     SharedClipboardEventSourceCreate(&pClient->Events, (uint16_t)g_mapClients.size());
    1047 
    1048     LogFlowFunc(("[Client %RU32] Using event source %RU32\n", u32ClientID, pClient->Events.uID));
    1049 
    1050     /* Reset the client state. */
    1051     vboxSvcClipboardClientStateReset(&pClient->State);
    1052 
    1053     /* (Re-)initialize the client state. */
    1054     vboxSvcClipboardClientStateInit(&pClient->State, u32ClientID);
    1055 
    1056     rc = VBoxClipboardSvcImplConnect(pClient, VBoxSvcClipboardGetHeadless());
     1071    VBOXCLIPBOARDEVENTSOURCEID uEventSourceID;
     1072    int rc = vboxSvcClipboardEventSourceCreateID(&uEventSourceID);
     1073    if (RT_SUCCESS(rc))
     1074        rc = SharedClipboardEventSourceCreate(&pClient->Events, uEventSourceID);
     1075    if (RT_SUCCESS(rc))
     1076    {
     1077        LogFlowFunc(("[Client %RU32] Using event source %RU32\n", u32ClientID, pClient->Events.uID));
     1078
     1079        /* Reset the client state. */
     1080        vboxSvcClipboardClientStateReset(&pClient->State);
     1081
     1082        /* (Re-)initialize the client state. */
     1083        rc = vboxSvcClipboardClientStateInit(&pClient->State, u32ClientID);
     1084        if (RT_SUCCESS(rc))
     1085        {
     1086            rc = VBoxClipboardSvcImplConnect(pClient, VBoxSvcClipboardGetHeadless());
    10571087#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
    1058     if (RT_SUCCESS(rc))
    1059         rc = SharedClipboardURICtxInit(&pClient->URI);
     1088            if (RT_SUCCESS(rc))
     1089                rc = SharedClipboardURICtxInit(&pClient->URI);
    10601090#endif
    1061 
    1062     if (RT_SUCCESS(rc))
    1063     {
    1064         VBOXCLIPBOARDCLIENTMAPENTRY ClientEntry;
    1065         RT_ZERO(ClientEntry);
    1066 
    1067         g_mapClients[u32ClientID] = ClientEntry; /** @todo Handle OOM / collisions? */
     1091            if (RT_SUCCESS(rc))
     1092            {
     1093                VBOXCLIPBOARDCLIENTMAPENTRY ClientEntry;
     1094                RT_ZERO(ClientEntry);
     1095
     1096                g_mapClients[u32ClientID] = ClientEntry; /** @todo Handle OOM / collisions? */
     1097            }
     1098        }
    10681099    }
    10691100
Note: See TracChangeset for help on using the changeset viewer.

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