~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to lib/dndGuest/dndTransportGuestRpc.cc

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*********************************************************
2
 
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify it
5
 
 * under the terms of the GNU Lesser General Public License as published
6
 
 * by the Free Software Foundation version 2.1 and no later version.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
11
 
 * License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program; if not, write to the Free Software Foundation, Inc.,
15
 
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
16
 
 *
17
 
 *********************************************************/
18
 
 
19
 
/*
20
 
 * dndTransportGuestRpc.cc --
21
 
 *
22
 
 *     GuestRpc implementation of the dndTransport interface.
23
 
 *
24
 
 *     A multiple packet transport protocol is implemented in this layer to
25
 
 *     support big buffer transfer. With the support, message size limit is
26
 
 *     improved from 64K to 4M (theoratically can be 4G).
27
 
 *
28
 
 *     There are 3 different types of packet. If a message is smaller than
29
 
 *     64K, it will be sent with type DND_TRANSPORT_PACKET_TYPE_SINGLE. If
30
 
 *     it is bigger than 64K, initial 64K will be first sent with
31
 
 *     DND_TRANSPORT_PACKET_TYPE_PAYLOAD. After received the packet, the 
32
 
 *     receiver should reply with DND_TRANSPORT_PACKET_TYPE_REQUEST, then
33
 
 *     next 64K will be sent.
34
 
 *
35
 
 *     This is a temporary solution before VICF is available. To simplify
36
 
 *     the implementation, there are some limitations:
37
 
 *     1. Any time for any transport class, there is only one pending
38
 
 *        big buffer for each direction. Any following big size message will
39
 
 *        be dropped before the pending transfer is done.Small message (<64K)
40
 
 *        can be sent at any time.
41
 
 *     2. Caller can not cancel any pending big buffer sending and 
42
 
 *        receiving.
43
 
 *     3. Pending big buffer will be dropped if there is any error.
44
 
 */
45
 
 
46
 
 
47
 
#include <sigc++/hide.h>
48
 
 
49
 
#include "dndTransportGuestRpc.hh"
50
 
 
51
 
extern "C" {
52
 
   #include "util.h"
53
 
   #include "rpcout.h"
54
 
   #include "rpcin.h"
55
 
   #include "debug.h"
56
 
   #include "str.h"
57
 
   #include "hostinfo.h"
58
 
   #include "dndMsg.h"
59
 
}
60
 
 
61
 
/*
62
 
 *-----------------------------------------------------------------------------
63
 
 *
64
 
 * RecvMsgCB --
65
 
 *
66
 
 *      Reads msg from VMX.
67
 
 *
68
 
 * Results:
69
 
 *      TRUE if success, FALSE otherwise.
70
 
 *
71
 
 * Side effects:
72
 
 *      None
73
 
 *
74
 
 *-----------------------------------------------------------------------------
75
 
 */
76
 
 
77
 
static Bool
78
 
RecvMsgCB(char const **result,     // OUT
79
 
          size_t *resultLen,       // OUT
80
 
          const char *name,        // IN
81
 
          const char *args,        // IN
82
 
          size_t argsSize,         // IN: Size of args
83
 
          void *clientData)        // IN
84
 
{
85
 
   DnDTransportGuestRpc *transport = (DnDTransportGuestRpc *)clientData;
86
 
   ASSERT(transport);
87
 
 
88
 
   /* '- 1' is to ignore empty space between command and args. */
89
 
   if ((argsSize - 1) <= 0) {
90
 
      Debug("%s: invalid argsSize\n", __FUNCTION__);
91
 
      return RpcIn_SetRetVals(result, resultLen, "invalid arg size", FALSE);
92
 
   }
93
 
   transport->RecvMsg((DnDTransportPacketHeader *)(args + 1), argsSize - 1);
94
 
   return RpcIn_SetRetVals(result, resultLen, "", TRUE);
95
 
}
96
 
 
97
 
 
98
 
/*
99
 
 *-----------------------------------------------------------------------------
100
 
 *
101
 
 * DnDTransportGuestRpc::DnDTransportGuestRpc --
102
 
 *
103
 
 *      Constructor of DnDTransportGuestRpc class.
104
 
 *
105
 
 * Results:
106
 
 *      None
107
 
 *
108
 
 * Side effects:
109
 
 *      None
110
 
 *
111
 
 *-----------------------------------------------------------------------------
112
 
 */
113
 
 
114
 
DnDTransportGuestRpc::DnDTransportGuestRpc(struct RpcIn *rpcIn, // IN
115
 
                                           const char *rpcCmd)  // IN
116
 
   : mRpcIn(rpcIn)
117
 
{
118
 
   ASSERT(rpcIn);
119
 
   ASSERT(rpcCmd);
120
 
 
121
 
   RpcIn_RegisterCallback(rpcIn, rpcCmd, RecvMsgCB, this);
122
 
   mRpcCmd = Util_SafeStrdup(rpcCmd);
123
 
 
124
 
   mSendBuf.buffer = NULL;
125
 
   mRecvBuf.buffer = NULL;
126
 
   DnD_TransportBufReset(&mSendBuf);
127
 
   DnD_TransportBufReset(&mRecvBuf);
128
 
   mSeqNum= 0;
129
 
}
130
 
 
131
 
 
132
 
/*
133
 
 *----------------------------------------------------------------------
134
 
 *
135
 
 * DnDTransportGuestRpc::~DnDTransportGuestRpc --
136
 
 *
137
 
 *      Destructor of DnDTransportGuestRpc.
138
 
 *
139
 
 * Results:
140
 
 *      None.
141
 
 *
142
 
 * Side effects:
143
 
 *      None.
144
 
 *
145
 
 *----------------------------------------------------------------------
146
 
 */
147
 
 
148
 
DnDTransportGuestRpc::~DnDTransportGuestRpc(void)
149
 
{
150
 
   RpcIn_UnregisterCallback(mRpcIn, mRpcCmd);
151
 
   free(mRpcCmd);
152
 
   free(mSendBuf.buffer);
153
 
   free(mRecvBuf.buffer);
154
 
}
155
 
 
156
 
 
157
 
/*
158
 
 *-----------------------------------------------------------------------------
159
 
 *
160
 
 * DnDTransportGuestRpc::SendMsg --
161
 
 *
162
 
 *      Sends msg to the VMX.
163
 
 *
164
 
 * Results:
165
 
 *      TRUE if success, FALSE otherwise.
166
 
 *
167
 
 * Side effects:
168
 
 *      None
169
 
 *
170
 
 *-----------------------------------------------------------------------------
171
 
 */
172
 
 
173
 
bool
174
 
DnDTransportGuestRpc::SendMsg(uint8 *msg,    // IN
175
 
                              size_t length) // IN
176
 
{
177
 
   DnDTransportPacketHeader *packet = NULL;
178
 
   size_t packetSize;
179
 
   bool ret = FALSE;
180
 
 
181
 
   if (length > DNDMSG_MAX_ARGSZ) {
182
 
      Debug("%s: message is too big, quit.\n", __FUNCTION__);
183
 
      return FALSE;
184
 
   }
185
 
 
186
 
   Debug("%s: got message, size %"FMTSZ"u\n", __FUNCTION__, length);
187
 
 
188
 
   if (length <= DND_MAX_TRANSPORT_PACKET_PAYLOAD_SIZE) {
189
 
      /*
190
 
       * It is a small size message, so it is not needed to buffer it in the transport
191
 
       * layer. Just put message into a packet and send it.
192
 
       */
193
 
      packetSize = DnD_TransportMsgToPacket(msg, length, mSeqNum, &packet);
194
 
   } else {
195
 
      /*
196
 
       * It is a big size message. First buffer it in the transport layer and send
197
 
       * it with multiple packets.
198
 
       */
199
 
      if (mSendBuf.buffer) {
200
 
         /*
201
 
          * There is a pending big size message. If there is no update time more than
202
 
          * DND_MAX_TRANSPORT_LATENCY_TIME, remove old message and send new message.
203
 
          * Otherwise ignore this message.
204
 
          *
205
 
          * XXX This transport implementation is just a temporary solution before VICF
206
 
          * is available. So any time there is only one pending big buffer. Any other
207
 
          * big message during this time will be dropped. Current pending buffer will
208
 
          * also be dropped if there is any error. Later on VICF should provide a
209
 
          * better transportation.
210
 
          */
211
 
         VmTimeType curTime;
212
 
 
213
 
         Hostinfo_GetTimeOfDay(&curTime);
214
 
 
215
 
         if ((curTime - mSendBuf.lastUpdateTime) < DND_MAX_TRANSPORT_LATENCY_TIME) {
216
 
            Debug("%s: got a big buffer, but there is already a pending one, quitting\n",
217
 
                  __FUNCTION__);
218
 
            return FALSE;
219
 
         }
220
 
      }
221
 
      DnD_TransportBufInit(&mSendBuf, msg, length, mSeqNum);
222
 
      packetSize = DnD_TransportBufGetPacket(&mSendBuf, &packet);
223
 
   }
224
 
 
225
 
   mSeqNum++;
226
 
   if (packetSize) {
227
 
      ret = SendPacket((uint8 *)packet, packetSize);
228
 
   }
229
 
   free(packet);
230
 
   return ret;
231
 
}
232
 
 
233
 
 
234
 
/*
235
 
 *-----------------------------------------------------------------------------
236
 
 *
237
 
 * DnDTransportGuestRpc::SendPacket --
238
 
 *
239
 
 *      Sends packet to the VMX.
240
 
 *
241
 
 * Results:
242
 
 *      TRUE if success, FALSE otherwise.
243
 
 *
244
 
 * Side effects:
245
 
 *      None
246
 
 *
247
 
 *-----------------------------------------------------------------------------
248
 
 */
249
 
 
250
 
bool
251
 
DnDTransportGuestRpc::SendPacket(uint8 *packet,     // IN
252
 
                                 size_t packetSize) // IN
253
 
{
254
 
   char *rpc = NULL;
255
 
   size_t rpcSize = 0;
256
 
   size_t nrWritten = 0;
257
 
   bool ret = FALSE;
258
 
 
259
 
   if (packetSize == 0 || packetSize > DND_MAX_TRANSPORT_PACKET_SIZE) {
260
 
      Debug("%s: invalid packet\n", __FUNCTION__);
261
 
      return FALSE;
262
 
   }
263
 
 
264
 
   rpcSize = strlen(mRpcCmd) + 1 + packetSize;
265
 
   rpc = (char *)Util_SafeMalloc(rpcSize);
266
 
   nrWritten = Str_Sprintf(rpc, rpcSize, "%s ", mRpcCmd);
267
 
   ASSERT(nrWritten + packetSize <= rpcSize);
268
 
   memcpy(rpc + nrWritten, packet, packetSize);
269
 
 
270
 
   ret = (TRUE == RpcOut_SendOneRaw(rpc, rpcSize, NULL, NULL));
271
 
   
272
 
   if (!ret) {
273
 
      Debug("%s: failed to send msg to host\n", __FUNCTION__);
274
 
   }
275
 
 
276
 
   free(rpc);
277
 
   return ret;
278
 
}
279
 
 
280
 
 
281
 
/*
282
 
 *-----------------------------------------------------------------------------
283
 
 *
284
 
 * DnDTransportGuestRpc::RecvMsg --
285
 
 *
286
 
 *      Receives packet from VMX.
287
 
 *
288
 
 * Results:
289
 
 *      None
290
 
 *
291
 
 * Side effects:
292
 
 *      None
293
 
 *
294
 
 *-----------------------------------------------------------------------------
295
 
 */
296
 
 
297
 
void
298
 
DnDTransportGuestRpc::RecvMsg(DnDTransportPacketHeader *packet, // IN
299
 
                              size_t packetSize)                // IN
300
 
{
301
 
   if (packetSize <= 0 ||
302
 
       packetSize != (packet->payloadSize + DND_TRANSPORT_PACKET_HEADER_SIZE) ||
303
 
       packetSize > DND_MAX_TRANSPORT_PACKET_SIZE) {
304
 
      Debug("%s: Received invalid data.\n", __FUNCTION__);
305
 
      return;
306
 
   }
307
 
 
308
 
   Debug("%s: received data, size %"FMTSZ"u.\n", __FUNCTION__, packetSize);
309
 
 
310
 
   switch (packet->type) {
311
 
   case DND_TRANSPORT_PACKET_TYPE_SINGLE:
312
 
      if (packet->payloadSize != packet->totalSize) {
313
 
         Debug("%s: received invalid packet.\n", __FUNCTION__);
314
 
         return;
315
 
      }
316
 
      recvMsgChanged.emit(packet->payload,
317
 
                          packet->payloadSize);
318
 
      break;
319
 
   case DND_TRANSPORT_PACKET_TYPE_REQUEST:
320
 
      {
321
 
         DnDTransportPacketHeader *replyPacket = NULL;
322
 
         size_t replyPacketSize;
323
 
 
324
 
         /* Validate received packet. */
325
 
         if (packet->payloadSize ||
326
 
             packet->seqNum != mSendBuf.seqNum ||
327
 
             packet->offset != mSendBuf.offset) {
328
 
            Debug("%s: received packet does not match local buffer.\n", __FUNCTION__);
329
 
            return;
330
 
         }
331
 
 
332
 
         replyPacketSize = DnD_TransportBufGetPacket(&mSendBuf, &replyPacket);
333
 
 
334
 
         if (!replyPacketSize) {
335
 
            /*
336
 
             * Not needed to reset mSendBuf because DnD_TransportBufGetPacket already
337
 
             * did that.
338
 
             */
339
 
            Debug("%s: DnD_TransportBufGetPacket failed.\n", __FUNCTION__);
340
 
            return;
341
 
         }
342
 
  
343
 
         if (!SendPacket((uint8 *)replyPacket, replyPacketSize) ||
344
 
             mSendBuf.offset == mSendBuf.totalSize) {
345
 
            /* Reset mSendBuf if whole buffer is sent or there is any error. */
346
 
            DnD_TransportBufReset(&mSendBuf);
347
 
         }
348
 
 
349
 
         free(replyPacket);
350
 
 
351
 
         break;
352
 
      }
353
 
   case DND_TRANSPORT_PACKET_TYPE_PAYLOAD:
354
 
      if (!DnD_TransportBufAppendPacket(&mRecvBuf, packet, packetSize)) {
355
 
         Debug("%s: DnD_TransportBufAppendPacket failed.\n", __FUNCTION__);
356
 
         return;
357
 
      }
358
 
 
359
 
      if (mRecvBuf.offset == mRecvBuf.totalSize) {
360
 
         /* Received all packets for the messge. */
361
 
         recvMsgChanged.emit(mRecvBuf.buffer, mRecvBuf.totalSize);
362
 
         DnD_TransportBufReset(&mRecvBuf);
363
 
      } else {
364
 
         /* Send request for next packet. */
365
 
         DnDTransportPacketHeader *replyPacket = NULL;
366
 
         size_t replyPacketSize;
367
 
 
368
 
         replyPacketSize = DnD_TransportReqPacket(&mRecvBuf, &replyPacket);
369
 
 
370
 
         if (!replyPacketSize) {
371
 
            Debug("%s: DnD_TransportReqPacket failed.\n", __FUNCTION__);
372
 
            return;
373
 
         }
374
 
         
375
 
         if (!SendPacket((uint8 *)replyPacket, replyPacketSize)) {
376
 
            DnD_TransportBufReset(&mRecvBuf);
377
 
         }
378
 
         free(replyPacket);      
379
 
      }
380
 
      break;
381
 
   default:
382
 
      Debug("%s: unknown packet.\n", __FUNCTION__);
383
 
      break;
384
 
   }
385
 
}