File: dndMsg.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1%2Bbuild1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze-lts
  • size: 20,376 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (499 lines) | stat: -rw-r--r-- 12,471 bytes parent folder | download | duplicates (4)
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*********************************************************
 * Copyright (C) 2007 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.
 *
 *********************************************************/

/*
 * dndMsg.c --
 *
 *      DnDMsg represents an rpc message which is sent across the
 *      wire. Any args that it holds will be written out exactly as stored.
 *
 *      To protect itself there are many checks to ensure the data which is
 *      serialized and unserialized is sane. Defines and asserts are used to
 *      ensure the message stays under these limits when serializing out and
 *      checks are enforced to ensure that the data to be unserialized remains
 *      under these limits.
 */

#include <stdlib.h>
#include <string.h>
#include "vm_assert.h"

#include "dndMsg.h"
#include "dndInt.h"

#define LOGLEVEL_MODULE dnd
#include "loglevel_user.h"


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_Init --
 *
 *      DnDMsg constructor.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None
 *
 *----------------------------------------------------------------------------
 */

void
DnDMsg_Init(DnDMsg *msg)   // IN/OUT: the message
{
   ASSERT(msg);

   msg->ver = 3;
   msg->cmd = 0;
   msg->nargs = 0;
   DynBufArray_Init(&msg->args, 0);
   msg->expectedArgsSz = 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_Destroy --
 *
 *      Destroys a message by clearing any of the data that is contained in it.
 *
 * Results:
 *      None
 *
 * Side effects:
 *      Frees the arguments' memory.
 *
 *----------------------------------------------------------------------------
 */

void
DnDMsg_Destroy(DnDMsg *msg)  // IN/OUT: the message
{
   uint32 i;
   uint32 count;

   ASSERT(msg);

   count = DynArray_Count(&msg->args);

   msg->ver = 0;
   msg->cmd = 0;
   msg->nargs = 0;
   msg->expectedArgsSz = 0;

   for (i = 0; i < count; ++i) {
      DynBuf *b = DynArray_AddressOf(&msg->args, i);
      DynBuf_Destroy(b);
   }
   DynArray_SetCount(&msg->args, 0);
   DynBufArray_Destroy(&msg->args);
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_Cmd --
 *
 *      Gets the dnd/copy paste command from the header.
 *
 * Results:
 *      An uint32 representing the command.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

uint32
DnDMsg_GetCmd(DnDMsg *msg)      // IN/OUT: the message
{
   ASSERT(msg);

   return msg->cmd;
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_SetCmd --
 *
 *      Sets the command for the message.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

void
DnDMsg_SetCmd(DnDMsg *msg,      // IN/OUT: the message
              uint32 cmd)       // IN: the command
{
   ASSERT(msg);
   ASSERT((DND_INVALID < cmd && cmd < DND_MAX) ||
          (CP_INVALID < cmd && cmd < CP_MAX));

   msg->cmd = cmd;
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_NumArgs --
 *
 *      Determines the number of arguments currently in the DnDMsg.
 *
 * Results:
 *      The number of arguments.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

uint32
DnDMsg_NumArgs(DnDMsg *msg)     // IN/OUT: the message
{
   ASSERT(msg);

   return DynBufArray_Count(&msg->args);
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_GetArg --
 *
 *      Gets an argument stored in DnDMsg.
 *
 * Results:
 *      Null if the argument is out of bounds, otherwise a pointer to a dynbuf
 *      containing the argument.
 *       This dynbuf is still
 *      managed by the DnDMsg and should NOT be destroyed.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

DynBuf *
DnDMsg_GetArg(DnDMsg *msg,      // IN/OUT: the message
              uint32 idx)       // IN: the argument to return
{
   ASSERT(msg);
   ASSERT(0 <= idx && idx < DynBufArray_Count(&msg->args));

   return DynArray_AddressOf(&msg->args, idx);
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_AppendArg --
 *
 *      Adds the data to the end of the argument list in the message. It will
 *      create a copy of the data to be mananged by DnDMsg until the message is
 *      destroyed.
 *
 * Results:
 *      TRUE on success, FALSE on failure.
 *
 * Side effects:
 *      Increases the internal arg size counter.
 *
 *----------------------------------------------------------------------------
 */

Bool
DnDMsg_AppendArg(DnDMsg *msg,   // IN/OUT: the message
                 void *buf,     // IN: the input buffer
                 size_t len)    // IN: the length of the input buffer
{
   DynBuf clonebuf;

   ASSERT(msg);
   ASSERT(buf);

   if (DynBufArray_Count(&msg->args) >= DNDMSG_MAX_ARGS) {
      return FALSE;
   }

   DynBuf_Init(&clonebuf);
   if (!DynBuf_Append(&clonebuf, buf, len)) {
      goto err;
   }

   /* The dynbufarray now owns the clonebuf data. */
   if (!DynBufArray_Push(&msg->args, clonebuf)) {
      goto err;
   }
   return TRUE;

err:
   DynBuf_Destroy(&clonebuf);
   return FALSE;
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_Serialize --
 *
 *      Serialize the contents of the DnDMsg out to the provided dynbuf. It
 *      will ASSERT if any invariants are broken.
 *
 * Results:
 *      TRUE on success.
 *      FALSE on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

Bool
DnDMsg_Serialize(DnDMsg *msg,   // IN/OUT: the message
                 DynBuf* buf)   // OUT: the output buffer
{
   DynBuf *curArg;
   uint32 nargs;
   uint32 i;
   uint32 serializeArgsSz = 0;

   ASSERT(msg);
   ASSERT(buf);
   ASSERT((DND_INVALID < msg->cmd && msg->cmd < DND_MAX) ||
          (CP_INVALID < msg->cmd && msg->cmd < CP_MAX));

   nargs = DynBufArray_Count(&msg->args);

   for (i = 0; i < nargs; ++i) {
      DynBuf *b = DynArray_AddressOf(&msg->args, i);
      serializeArgsSz += sizeof(uint32) + DynBuf_GetSize(b);
   }

   if (DynBuf_Append(buf, &msg->ver, sizeof msg->ver) &&
       DynBuf_Append(buf, &msg->cmd, sizeof msg->cmd) &&
       DynBuf_Append(buf, &nargs, sizeof nargs) &&
       DynBuf_Append(buf, &serializeArgsSz, sizeof serializeArgsSz)) {
      int i;
      uint32 curArgsSz;

      for (i = 0; i < nargs; i++) {
         curArg = DynBufArray_AddressOf(&msg->args, i);
         curArgsSz = DynBuf_GetSize(curArg);

         if (!DynBuf_Append(buf, &curArgsSz, sizeof curArgsSz) ||
             !DynBuf_Append(buf, DynBuf_Get(curArg), curArgsSz)) {
            return FALSE;
         }
      }
   } else {
      return FALSE;
   }

   return TRUE;
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_UnserializeHeader --
 *
 *      Read the header from the buffer into a DnDMsg. Any contents in the
 *      DnDMsg will be destroyed. This allows you to retrieve header
 *      information. These functions are specified in the dndMsg.h. Most
 *      notably, you can retrieve the size of the arguments so that you can
 *      pass a properly sized buffer to DnDMsg_UnserializeArgs.
 *
 *      This is the one of the two places that nargs is set. The other is
 *      implicitly set by DnDMsg_AppendArg with the push and only ever
 *      realized through the DnDMsg_Serialize function. expectedArgsSz,
 *      curArgSz follows the same idea.
 *
 * Results:
 *      DNDMSG_SUCCESS on success.
 *      DNDMSG_INPUT_TOO_SMALL when provided buffer is too small.
 *      DNDMSG_INPUT_ERR when the provided buffer is inconsistant.
 *      DNDMSG_NOMEM when we run out of memory.
 *      DNDMSG_ERR on any other error.
 *
 * Side effects:
 *      On success the msg's header will be filled. On failure the msg will be
 *      destroyed.
 *
 *----------------------------------------------------------------------------
 */

DnDMsgErr
DnDMsg_UnserializeHeader(DnDMsg *msg,   // IN/OUT: the message
                         void *buf,     // IN: the input buffer
                         size_t len)    // IN: the buffer length
{
   BufRead r;

   ASSERT(msg);
   ASSERT(buf);

   r.pos = buf;
   r.unreadLen = len;

   if (len < DNDMSG_HEADERSIZE_V3) {
      return DNDMSG_INPUT_TOO_SMALL;
   }

   /* Read buffer into msg. */
   if (DnDReadBuffer(&r, &msg->ver, sizeof msg->ver) &&
       DnDReadBuffer(&r, &msg->cmd, sizeof msg->cmd) &&
       DnDReadBuffer(&r, &msg->nargs, sizeof msg->nargs) &&
       DnDReadBuffer(&r, &msg->expectedArgsSz, sizeof msg->expectedArgsSz)) {
      /* Sanity checks. */
      if (msg->expectedArgsSz < DNDMSG_MAX_ARGSZ &&
          (msg->cmd < DND_MAX || msg->cmd < CP_MAX) &&
          0 < msg->cmd &&
          msg->ver >= 3 &&
          msg->nargs < DNDMSG_MAX_ARGS) {
         return DNDMSG_SUCCESS;
      } else {
         return DNDMSG_INPUT_ERR;
      }
   } else {
      return DNDMSG_INPUT_TOO_SMALL;
   }
}


/*
 *----------------------------------------------------------------------------
 *
 * DnDMsg_UnserializeArgs --
 *
 *      Unserialize the arguments of the message provided by the buffer.
 *      Each argument is a uint32 of the size followed by the buffer. On
 *      failure the message will revert to the state which was passed into the
 *      function.
 *
 * Results:
 *      DNDMSG_SUCCESS on success.
 *      DNDMSG_INPUT_TOO_SMALL when provided buffer is too small.
 *      DNDMSG_INPUT_ERR when the provided buffer is inconsistant.
 *      DNDMSG_NOMEM when we run out of memory.
 *      DNDMSG_ERR on any other error.
 *
 * Side effects:
 *      On success, arguments found in buf are unserialized into msg.
 *
 *----------------------------------------------------------------------------
 */

DnDMsgErr
DnDMsg_UnserializeArgs(DnDMsg *msg,     // IN/OUT: the message
                       void *buf,       // IN: input buffer
                       size_t len)      // IN: buffer length
{
   uint32 i;
   uint32 count;
   BufRead r;
   uint32 readArgsSz = 0;

   void *data = NULL;
   DnDMsgErr ret = DNDMSG_SUCCESS;

   ASSERT(msg);
   ASSERT(DynBufArray_Count(&msg->args) == 0);
   ASSERT(buf);

   r.pos = buf;
   r.unreadLen = len;

   if (len < msg->expectedArgsSz) {
      return DNDMSG_INPUT_TOO_SMALL;
   }

   for (i = 0; i < msg->nargs; ++i) {
      uint32 argSz;
      if (!DnDReadBuffer(&r, &argSz, sizeof argSz)) {
         ret = DNDMSG_INPUT_TOO_SMALL;
         goto err;
      }

      if (argSz > DNDMSG_MAX_ARGSZ ||
          readArgsSz + sizeof (uint32) + argSz > msg->expectedArgsSz) {
         ret = DNDMSG_INPUT_ERR;
         goto err;
      }

      data = malloc(argSz);
      if (!data) {
         ret = DNDMSG_NOMEM;
         goto err;
      }

      if (!DnDReadBuffer(&r, data, argSz)) {
         ret = DNDMSG_ERR;
         goto err;
      }

      if (!DnDMsg_AppendArg(msg, data, argSz)) {
         ret = DNDMSG_NOMEM;
         goto err;
      }
      readArgsSz += argSz + sizeof (uint32);
      free(data);
   }

   ASSERT(ret == DNDMSG_SUCCESS);
   return ret;

err:
   if (data) {
      free(data);
   }

   count = DynBufArray_Count(&msg->args);
   for (i = 0; i < count; ++i) {
      DynBuf *b = DynArray_AddressOf(&msg->args, i);
      DynBuf_Destroy(b);
   }
   /*
    * DnDMsg_AppendArg relies on DynBufArray_Push, hence the count needs to be
    * reset.
    */
   DynBufArray_SetCount(&msg->args, 0);

   return ret;
}