summaryrefslogtreecommitdiff
path: root/open-vm-tools/services/plugins/dndcp/dndGuest/rpcV3Util.cpp
blob: f1754feff422c0a88038135461decbfd964e8d69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*********************************************************
 * Copyright (C) 2010-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/**
 * @rpcV3Util.cpp --
 *
 * Implementation of common utility object for DnD/CP version 3 rpc object.
 * It is shared by vmx and guest implementation. Some common utilities
 * including
 * *packet marshalling/un-marshalling
 * *big buffer support
 * are implemented here.
 */


#include "rpcV3Util.hpp"

extern "C" {
#ifdef VMX86_TOOLS
   #include "debug.h"
   #define LOG(level, msg) (Debug msg)
#else
   #define LOGLEVEL_MODULE dnd
   #include "loglevel_user.h"
#endif

   #include "dndClipboard.h"
   #include "util.h"
   #include "dndMsg.h"
   #include "hostinfo.h"
}



/**
 * Constructor.
 */

RpcV3Util::RpcV3Util(void)
   : mVersionMajor(3),
     mVersionMinor(0),
     mSeqNum(1)
{
   mSendBuf.buffer = NULL;
   mRecvBuf.buffer = NULL;
   DnD_TransportBufReset(&mSendBuf);
   DnD_TransportBufReset(&mRecvBuf);
}


/**
 * Destructor.
 */

RpcV3Util::~RpcV3Util(void)
{
   free(mSendBuf.buffer);
   free(mRecvBuf.buffer);
}


/**
 * Initialize the RpcV3Util object. All owner should call this first before
 * calling any other utility function.
 *
 * @param[in] rpc the owner of this utility object
 * @param[in] msgType the type of message (DnD/CP/FT)
 * @param[in] msgSrc source of the message (host/guest/controller)
 */

void
RpcV3Util::Init(RpcBase *rpc)
{
   ASSERT(rpc);
   mRpc = rpc;
}


/**
 * Serialize command, then send the message.
 *
 * @param[in] cmd version 3 command
 *
 * @return true on success, false otherwise.
 */

bool
RpcV3Util::SendMsg(uint32 cmd)
{
   DnDMsg msg;
   bool ret = false;

   DnDMsg_Init(&msg);
   DnDMsg_SetCmd(&msg, cmd);
   ret = SendMsg(&msg);
   DnDMsg_Destroy(&msg);

   return ret;
}


/**
 * Serialize the clipboard item if there is one, then send the message to
 * destId.
 *
 * @param[in] cmd version 3 command
 * @param[in] clip the clipboard item.
 *
 * @return true on success, false otherwise.
 */

bool
RpcV3Util::SendMsg(uint32 cmd,
                   const CPClipboard *clip)
{
   DnDMsg msg;
   DynBuf buf;
   bool ret = false;

   DnDMsg_Init(&msg);
   DynBuf_Init(&buf);

   /* Serialize clip and output into buf. */
   if (!CPClipboard_Serialize(clip, &buf)) {
      LOG(0, ("%s: CPClipboard_Serialize failed.\n", __FUNCTION__));
      goto exit;
   }

   /* Construct msg with both cmd CP_HG_SET_CLIPBOARD and buf. */
   DnDMsg_SetCmd(&msg, cmd);
   if (!DnDMsg_AppendArg(&msg, DynBuf_Get(&buf), DynBuf_GetSize(&buf))) {
      LOG(0, ("%s: DnDMsg_AppendData failed.\n", __FUNCTION__));
      goto exit;
   }

   ret = SendMsg(&msg);

exit:
   DynBuf_Destroy(&buf);
   DnDMsg_Destroy(&msg);
   return ret;
}


/**
 * Serialize command with mouse position, and send the message.
 *
 * @param[in] cmd version 3 command
 * @param[in] x mouse position x.
 * @param[in] y mouse position y.
 *
 * @return true on success, false otherwise.
 */

bool
RpcV3Util::SendMsg(uint32 cmd,
                   int32 x,
                   int32 y)
{
   bool ret = false;

   DnDMsg msg;

   DnDMsg_Init(&msg);

   DnDMsg_SetCmd(&msg, cmd);

   if (!DnDMsg_AppendArg(&msg, &x, sizeof x) ||
       !DnDMsg_AppendArg(&msg, &y, sizeof y)) {
      LOG(0, ("%s: DnDMsg_AppendData failed.\n", __FUNCTION__));
      goto exit;
   }

   ret = SendMsg(&msg);

exit:
   DnDMsg_Destroy(&msg);
   return ret;
}


/**
 * Serialize and send the message.
 *
 * @param[in] msg the message to be serialized and sent.
 *
 * @return true on success, false otherwise.
 */

bool
RpcV3Util::SendMsg(const DnDMsg *msg)
{
   DynBuf buf;
   bool ret = false;

   DynBuf_Init(&buf);

   /* Serialize msg and output to buf. */
   if (!DnDMsg_Serialize((DnDMsg *)msg, &buf)) {
      LOG(0, ("%s: DnDMsg_Serialize failed.\n", __FUNCTION__));
      goto exit;
   }

   ret = SendMsg((uint8 *)DynBuf_Get(&buf), DynBuf_GetSize(&buf));

exit:
   DynBuf_Destroy(&buf);
   return ret;
}


/**
 * Serialize the message and send it to destId.
 *
 * @param[in] binary
 * @param[in] binarySize
 *
 * @return true on success, false otherwise.
 */

bool
RpcV3Util::SendMsg(const uint8 *binary,
                   uint32 binarySize)
{
   DnDTransportPacketHeader *packet = NULL;
   size_t packetSize;
   bool ret = FALSE;

   if (binarySize > DNDMSG_MAX_ARGSZ) {
      LOG(1, ("%s: message is too big, quit.\n", __FUNCTION__));
      return false;
   }

   LOG(4, ("%s: got message, size %d.\n", __FUNCTION__, binarySize));

   if (binarySize <= DND_MAX_TRANSPORT_PACKET_PAYLOAD_SIZE) {
      /*
       * It is a small size message, so it is not needed to buffer it. Just
       * put message into a packet and send it.
       */
      packetSize = DnD_TransportMsgToPacket((uint8 *)binary, binarySize,
                                            mSeqNum, &packet);
   } else {
      /*
       * It is a big size message. First buffer it and send it with multiple
       * packets.
       */
      if (mSendBuf.buffer) {
         /*
          * There is a pending big size message. If there is no update time
          * more than DND_MAX_TRANSPORT_LATENCY_TIME, remove old message and
          * send new message. Otherwise ignore this message.
          */

         if ((Hostinfo_SystemTimerUS() - mSendBuf.lastUpdateTime) <
             DND_MAX_TRANSPORT_LATENCY_TIME) {
            LOG(1, ("%s: got a big buffer, but there is another pending one, drop it\n",
                    __FUNCTION__));
            return false;
         }
      }
      DnD_TransportBufInit(&mSendBuf, (uint8 *)binary, binarySize, mSeqNum);
      packetSize = DnD_TransportBufGetPacket(&mSendBuf, &packet);
   }

   /* Increase sequence number for next message. */
   mSeqNum++;
   if (packetSize) {
      ret = mRpc->SendPacket(0, (const uint8 *)packet, packetSize);
   }
   free(packet);
   return ret;
}



/**
 * Callback from transport layer after received a packet from srcId.
 *
 * @param[in] srcId addressId where the packet is from
 * @param[in] packet
 * @param[in] packetSize
 */

void
RpcV3Util::OnRecvPacket(uint32 srcId,
                        const uint8 *packet,
                        size_t packetSize)
{
   DnDTransportPacketHeader *packetV3 = (DnDTransportPacketHeader *)packet;
   ASSERT(packetV3);
   if (packetSize <= 0 ||
       packetSize != (packetV3->payloadSize + DND_TRANSPORT_PACKET_HEADER_SIZE) ||
       packetSize > DND_MAX_TRANSPORT_PACKET_SIZE) {
      LOG(0, ("%s: Received invalid data.\n", __FUNCTION__));
      return;
   }

   switch (packetV3->type) {
   case DND_TRANSPORT_PACKET_TYPE_SINGLE:
      if (packetV3->payloadSize != packetV3->totalSize) {
         LOG(0, ("%s: received invalid packet.\n", __FUNCTION__));
         return;
      }
      /* This is a single packet. Forward to rpc layer for further processing. */
      mRpc->HandleMsg(NULL, packetV3->payload, packetV3->payloadSize);
      break;
   case DND_TRANSPORT_PACKET_TYPE_REQUEST:
      {
         /* Peer is asking for next packet. */
         DnDTransportPacketHeader *replyPacket = NULL;
         size_t replyPacketSize;

         if (packetV3->payloadSize ||
             packetV3->seqNum != mSendBuf.seqNum ||
             packetV3->offset != mSendBuf.offset) {
            LOG(0, ("%s: received packet does not match local buffer.\n", __FUNCTION__));
            return;
         }

         replyPacketSize = DnD_TransportBufGetPacket(&mSendBuf, &replyPacket);

         if (!replyPacketSize) {
            /*
             * Not needed to reset mSendBuf because DnD_TransportBufGetPacket already
             * did that.
             */
            LOG(0, ("%s: DnD_TransportBufGetPacket failed.\n", __FUNCTION__));
            return;
         }

         if (!mRpc->SendPacket(0, (const uint8 *)replyPacket, replyPacketSize) ||
             mSendBuf.offset == mSendBuf.totalSize) {
            /* Reset mSendBuf if whole buffer is sent or there is any error. */
            DnD_TransportBufReset(&mSendBuf);
         }

         free(replyPacket);

         break;
      }
   case DND_TRANSPORT_PACKET_TYPE_PAYLOAD:
      /* Received next packet for big binary buffer. */
      if (!DnD_TransportBufAppendPacket(&mRecvBuf, packetV3, packetSize)) {
         LOG(0, ("%s: DnD_TransportBufAppendPacket failed.\n", __FUNCTION__));
         return;
      }

      if (mRecvBuf.offset == mRecvBuf.totalSize) {
         /*
          * Received all packets for the messge, forward it to rpc layer for
          * further processing.
          */
         mRpc->HandleMsg(NULL, mRecvBuf.buffer, mRecvBuf.totalSize);
         DnD_TransportBufReset(&mRecvBuf);
      } else {
         /* Send request for next packet. */
         DnDTransportPacketHeader *replyPacket = NULL;
         size_t replyPacketSize;

         replyPacketSize = DnD_TransportReqPacket(&mRecvBuf, &replyPacket);

         if (!replyPacketSize) {
            LOG(0, ("%s: DnD_TransportReqPacket failed.\n", __FUNCTION__));
            return;
         }

         if (!mRpc->SendPacket(0, (const uint8 *)replyPacket, replyPacketSize)) {
            DnD_TransportBufReset(&mRecvBuf);
         }
         free(replyPacket);
      }
      break;
   default:
      LOG(0, ("%s: unknown packet.\n", __FUNCTION__));
      break;
   }
}