summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/rpcChannel/simpleSocket.c
blob: b99822331e9a1eac86d75835e30bceb64f085db2 (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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*********************************************************
 * Copyright (C) 2013-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.
 *
 *********************************************************/

/*
 * simpleSocket.c --
 *
 *    Simple wrappers for socket.
 *
 */

#include <stdlib.h>
#if defined(linux)
#include <arpa/inet.h>
#endif

#include "simpleSocket.h"
#include "vmci_defs.h"
#include "vmci_sockets.h"
#include "dataMap.h"
#include "err.h"
#include "debug.h"

#define LGPFX "SimpleSock: "


static int
SocketGetLastError(void);

/*
 *-----------------------------------------------------------------------------
 *
 * SocketStartup --
 *
 *      Win32 special socket init.
 *
 * Results:
 *      TRUE on success
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static gboolean
SocketStartup(void)
{
#if defined(_WIN32)
   int err;
   WSADATA wsaData;

   err = WSAStartup(MAKEWORD(2, 0), &wsaData);
   if (err) {
      Warning(LGPFX "Error in WSAStartup: %d[%s]\n", err,
              Err_Errno2String(err));
      return FALSE;
   }

   if (2 != LOBYTE(wsaData.wVersion) || 0 != HIBYTE(wsaData.wVersion)) {
      Warning(LGPFX "Unsupported Winsock version %d.%d\n",
              LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
      return FALSE;
   }
#endif

   return TRUE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * SocketCleanup --
 *
 *      Win32 special socket cleanup.
 *
 * Results:
 *      TRUE on success, FALSE on failure.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static gboolean
SocketCleanup(void)
{
#if defined(_WIN32)
   int err = WSACleanup();
   if (err) {
      Warning(LGPFX "Error in WSACleanup: %d[%s]\n", err,
              Err_Errno2String(err));
      return FALSE;
   }
#endif
   return TRUE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Socket_Close --
 *
 *      wrapper for socket close.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

void
Socket_Close(SOCKET sock)
{
   int res;

#if defined(_WIN32)
   res = closesocket(sock);
#else
   res = close(sock);
#endif

   if (res == SOCKET_ERROR) {
      int err = SocketGetLastError();
      Warning(LGPFX "Error in closing socket %d: %d[%s]\n",
              sock, err, Err_Errno2String(err));
   }

   SocketCleanup();
}


/*
 *-----------------------------------------------------------------------------
 *
 * SocketGetLastError --
 *
 *      Get the last error code.
 *
 * Results:
 *      error code.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static int
SocketGetLastError(void)
{
#if defined(_WIN32)
   return WSAGetLastError();
#else
   return errno;
#endif
}


/*
 *-----------------------------------------------------------------------------
 *
 * Socket_Recv --
 *
 *      Block until given number of bytes of data is received or error occurs.
 *
 * Results:
 *      TRUE on success, FALSE on failure.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

gboolean
Socket_Recv(SOCKET fd,      // IN
            char *buf,      // OUT
            int len)        // IN
{
   int remaining = len;
   int rv;
   int sysErr;

   while (remaining > 0) {
      rv = recv(fd, buf , remaining, 0);
      if (rv == 0) {
         Warning(LGPFX "Socket %d closed by peer.", fd);
         return FALSE;
      }
      if (rv == SOCKET_ERROR) {
         sysErr = SocketGetLastError();
         if (sysErr == SYSERR_EINTR) {
            continue;
         }
         Warning(LGPFX "Recv error for socket %d: %d[%s]", fd, sysErr,
                 Err_Errno2String(sysErr));
         return FALSE;
      }
      remaining -= rv;
      buf += rv;
   }

   Debug(LGPFX "Recved %d bytes from socket %d\n", len, fd);
   return TRUE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Socket_Send --
 *
 *      Block until the given number of bytes of data is sent or error occurs.
 *
 * Results:
 *      TRUE on success, FALSE on failure.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

gboolean
Socket_Send(SOCKET fd,      // IN
            char *buf,      // IN
            int len)        // IN
{
   int left = len;
   int sent = 0;
   int rv;
   int sysErr;

   while (left > 0) {
      rv = send(fd, buf + sent, left, 0);
      if (rv == SOCKET_ERROR) {
         sysErr = SocketGetLastError();
         if (sysErr == SYSERR_EINTR) {
            continue;
         }
         Warning(LGPFX "Send error for socket %d: %d[%s]", fd, sysErr,
                 Err_Errno2String(sysErr));
         return FALSE;
      }
      left -= rv;
      sent += rv;
   }

   Debug(LGPFX "Sent %d bytes from socket %d\n", len, fd);
   return TRUE;
}


/*
 *----------------------------------------------------------------------------
 *
 * Socket_ConnectVMCI --
 *
 *      Connect to VMCI port in blocking mode.
 *      If isPriv is true, we will try to bind the local port to a port that
 *      is less than 1024.
 *
 * Results:
 *      returns the raw socket on sucess, otherwise INVALID_SOCKET;
 *
 * Side effects:
 *      None
 *
 *----------------------------------------------------------------------------
 */

SOCKET
Socket_ConnectVMCI(unsigned int cid,                  // IN
                   unsigned int port,                 // IN
                   gboolean isPriv,                   // IN
                   SockConnError *outError)           // OUT
{
   struct sockaddr_vm addr;
   SOCKET fd;
   SockConnError error = SOCKERR_GENERIC;
   int sysErr;
   socklen_t addrLen = sizeof addr;
   int vsockDev = -1;
   int family = VMCISock_GetAFValueFd(&vsockDev);

   if (outError) {
      *outError = SOCKERR_SUCCESS;
   }

   if (!SocketStartup()) {
      goto error;
   }

   if (family == -1) {
      Warning(LGPFX "Couldn't get VMCI socket family info.");
      goto error;
   }

   memset((char *)&addr, 0, sizeof addr);
   addr.svm_family = family;
   addr.svm_cid = cid;
   addr.svm_port = port;

   Debug(LGPFX "creating new socket, connecting to %u:%u\n", cid, port);

   fd = socket(addr.svm_family, SOCK_STREAM, 0);
   if (fd == INVALID_SOCKET) {
      sysErr = SocketGetLastError();
      Warning(LGPFX "failed to create socket, error %d: %s\n",
              sysErr, Err_Errno2String(sysErr));
      goto error;
   }

   if (isPriv) {
      struct sockaddr_vm localAddr;
      gboolean bindOk = FALSE;
      int localPort;

      memset(&localAddr, 0, sizeof localAddr);
      localAddr.svm_family = addr.svm_family;
      localAddr.svm_cid = VMCISock_GetLocalCID();

      /* Try to bind to port 1~1023 for a privileged user. */
      for (localPort = PRIVILEGED_PORT_MAX;
           localPort >= PRIVILEGED_PORT_MIN; localPort--) {

         localAddr.svm_port = localPort;

         if (bind(fd, (struct sockaddr *)&localAddr, sizeof localAddr) != 0) {
            sysErr = SocketGetLastError();
            if (sysErr == SYSERR_EACCESS) {
               Debug(LGPFX "Couldn't bind to privileged port for "
                     "socket %d\n", fd);
               error = SOCKERR_EACCESS;
               Socket_Close(fd);
               goto error;
            }
            if (sysErr == SYSERR_EADDRINUSE) {
               continue;
            }
            Warning(LGPFX "could not bind socket, error %d: %s\n", sysErr,
                    Err_Errno2String(sysErr));
            Socket_Close(fd);
            error = SOCKERR_BIND;
            goto error;
         } else {
            bindOk = TRUE;
            break;
         }
      }

      if (!bindOk) {
         Debug(LGPFX "Failed to bind to privileged port for socket %d, "
               "no port available\n", fd);
         error = SOCKERR_BIND;
         Socket_Close(fd);
         goto error;
      } else {
         Debug(LGPFX "Successfully bound to port %d for socket %d\n",
               localAddr.svm_port, fd);
      }
   }

   if (connect(fd, (struct sockaddr *)&addr, addrLen) != 0) {
      sysErr = SocketGetLastError();
      Debug(LGPFX "socket connect failed, error %d: %s\n",
            sysErr, Err_Errno2String(sysErr));
      Socket_Close(fd);
      error = SOCKERR_CONNECT;
      goto error;
   }

   VMCISock_ReleaseAFValueFd(vsockDev);
   Debug(LGPFX "socket %d connected\n", fd);
   return fd;

error:
   if (outError) {
      *outError = error;
   }
   VMCISock_ReleaseAFValueFd(vsockDev);

   return INVALID_SOCKET;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Socket_DecodePacket --
 *
 *    Helper function to decode received packet in DataMap encoding format.
 *
 * Result:
 *    None
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

static gboolean
Socket_DecodePacket(const char *recvBuf,       // IN
                    int fullPktLen,            // IN
                    char **payload,            // OUT
                    int32 *payloadLen)         // OUT
{
   ErrorCode res;
   DataMap map;
   char *buf;
   int32 len;

   *payload = NULL;
   *payloadLen = 0;

   /* decoding the packet */
   res = DataMap_Deserialize(recvBuf, fullPktLen, &map);
   if (res != DMERR_SUCCESS) {
      Debug(LGPFX "Error in dataMap decoding, error=%d\n", res);
      return FALSE;
   }

   res = DataMap_GetString(&map, GUESTRPCPKT_FIELD_PAYLOAD, &buf, &len);
   if (res == DMERR_SUCCESS) {
      char *tmpPtr = malloc(len + 1);
      if (tmpPtr == NULL) {
         Debug(LGPFX "Error in allocating memory\n");
         goto error;
      }
      memcpy(tmpPtr, buf, len);
      /* add a trailing 0 for backward compatibility */
      tmpPtr[len] = '\0';

      *payload = tmpPtr;
      *payloadLen = len;
   } else {
      Debug(LGPFX "Error in decoding payload, error=%d\n", res);
      goto error;
   }

   DataMap_Destroy(&map);
   return TRUE;

error:
   DataMap_Destroy(&map);
   return FALSE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Socket_PackSendData --
 *
 *    Helper function for building send packet and serialize it.
 *
 * Result:
 *    TRUE on sucess, FALSE otherwise.
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

static gboolean
Socket_PackSendData(const char *buf,             // IN
                    int len,                     // IN
                    char **serBuf,               // OUT
                    int32 *serBufLen)            // OUT
{
   DataMap map;
   ErrorCode res;
   char *newBuf;
   gboolean mapCreated = FALSE;
   int64 pktType = GUESTRPCPKT_TYPE_DATA;

   res = DataMap_Create(&map);
   if (res != DMERR_SUCCESS) {
      goto error;
   }

   mapCreated = TRUE;
   res = DataMap_SetInt64(&map, GUESTRPCPKT_FIELD_TYPE,
                          pktType, TRUE);
   if (res != DMERR_SUCCESS) {
      goto error;
   }

   newBuf = malloc(len);
   if (newBuf == NULL) {
      Debug(LGPFX "Error in allocating memory.\n");
      goto error;
   }
   memcpy(newBuf, buf, len);
   res = DataMap_SetString(&map, GUESTRPCPKT_FIELD_PAYLOAD, newBuf,
                           len, TRUE);
   if (res != DMERR_SUCCESS) {
      free(newBuf);
      goto error;
   }

   res = DataMap_Serialize(&map, serBuf, serBufLen);
   if (res != DMERR_SUCCESS) {
      goto error;
   }

   DataMap_Destroy(&map);
   return TRUE;

error:
   if (mapCreated) {
      DataMap_Destroy(&map);
   }
   Debug(LGPFX "Error in dataMap encoding\n");
   return FALSE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Socket_RecvPacket --
 *
 *    Helper function to recv a dataMap packet over the socket.
 *    The caller has to *free* the payload to avoid memory leak.
 *
 * Result:
 *    TRUE on sucess, FALSE otherwise.
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

gboolean
Socket_RecvPacket(SOCKET sock,               // IN
                  char **payload,            // OUT
                  int *payloadLen)           // OUT
{
   gboolean ok;
   int32 packetLen;
   int packetLenSize = sizeof packetLen;
   int fullPktLen;
   char *recvBuf;
   int recvBufLen;

   ok = Socket_Recv(sock, (char *)&packetLen, packetLenSize);
   if (!ok) {
      Debug(LGPFX "error in recving packet header, err=%d\n",
            SocketGetLastError());
      return FALSE;
   }

   fullPktLen = ntohl(packetLen) + packetLenSize;
   recvBufLen = fullPktLen;
   recvBuf = malloc(recvBufLen);
   if (recvBuf == NULL) {
      Debug(LGPFX "Could not allocate recv buffer.\n");
      return FALSE;
   }

   memcpy(recvBuf, &packetLen, packetLenSize);
   ok = Socket_Recv(sock, recvBuf + packetLenSize,
                     fullPktLen - packetLenSize);
   if (!ok) {
      Debug(LGPFX "error in recving packet, err=%d\n",
            SocketGetLastError());
      free(recvBuf);
      return FALSE;
   }

   ok = Socket_DecodePacket(recvBuf, fullPktLen, payload, payloadLen);
   free(recvBuf);
   return ok;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Socket_SendPacket --
 *
 *    Helper function to send a dataMap packet over the socket.
 *
 * Result:
 *    TRUE on sucess, FALSE otherwise.
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

gboolean
Socket_SendPacket(SOCKET sock,               // IN
                  const char *payload,       // IN
                  int payloadLen)            // IN
{
   gboolean ok;
   char *sendBuf;
   int sendBufLen;

   if (!Socket_PackSendData(payload, payloadLen, &sendBuf, &sendBufLen)) {
      return FALSE;
   }

   ok = Socket_Send(sock, sendBuf, sendBufLen);
   free(sendBuf);

   return ok;
}