~n-muench/ubuntu/precise/open-vm-tools/open-vm-tools.raring-precise.backport

« back to all changes in this revision

Viewing changes to lib/include/vmci_sockets.h

  • Committer: Nate Muench
  • Date: 2013-06-07 19:36:13 UTC
  • mfrom: (1.4.7)
  • Revision ID: nowiwilldestroyabydos@gmail.com-20130607193613-kctd63r2cs35g2ms
Tags: 2012.12.26-958366-0ubuntu1
* Merging upstream version 2012.12.26-958366.
  - Adds support for 3.5 Kernels (LP: #1083719)
* Adding patch from Mathias Krause <minipli@googlemail.com> to fix
  kernel stack memory leack in vsock module [CVE-2013-3237].

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*********************************************************
2
 
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 
2
 * Copyright (C) 2007-2012 VMware, Inc. All rights reserved.
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify it
5
5
 * under the terms of the GNU Lesser General Public License as published
43
43
#endif // linux && !VMKERNEL
44
44
#endif
45
45
 
46
 
/*
47
 
 * We use the same value for the AF family and the socket option
48
 
 * level. To set options, use the value of VMCISock_GetAFValue for
49
 
 * 'level' and these constants for the optname.
 
46
/**
 
47
 * \brief Option name for STREAM socket buffer size.
 
48
 *
 
49
 * Use as the option name in \c setsockopt(3) or \c getsockopt(3) to set
 
50
 * or get an \c unsigned \c long \c long that specifies the size of the
 
51
 * buffer underlying a vSockets STREAM socket.
 
52
 *
 
53
 * \note Value is clamped to the MIN and MAX.
 
54
 *
 
55
 * \see VMCISock_GetAFValueFd()
 
56
 * \see SO_VMCI_BUFFER_MIN_SIZE
 
57
 * \see SO_VMCI_BUFFER_MAX_SIZE
 
58
 *
 
59
 * An example is given below.
 
60
 *
 
61
 * \code
 
62
 * int vmciFd;
 
63
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
64
 * unsigned long long val = 0x1000;
 
65
 * int fd = socket(af, SOCK_STREAM, 0);
 
66
 * setsockopt(fd, af, SO_VMCI_BUFFER_SIZE, &val, sizeof val);
 
67
 * ...
 
68
 * close(fd);
 
69
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
70
 * \endcode
50
71
 */
51
72
 
52
73
#define SO_VMCI_BUFFER_SIZE                 0
 
74
 
 
75
/**
 
76
 * \brief Option name for STREAM socket minimum buffer size.
 
77
 *
 
78
 * Use as the option name in \c setsockopt(3) or \c getsockopt(3) to set
 
79
 * or get an \c unsigned \c long \c long that specifies the minimum size
 
80
 * allowed for the buffer underlying a vSockets STREAM socket.
 
81
 *
 
82
 * \see VMCISock_GetAFValueFd()
 
83
 * \see SO_VMCI_BUFFER_SIZE
 
84
 * \see SO_VMCI_BUFFER_MAX_SIZE
 
85
 *
 
86
 * An example is given below.
 
87
 *
 
88
 * \code
 
89
 * int vmciFd;
 
90
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
91
 * unsigned long long val = 0x500;
 
92
 * int fd = socket(af, SOCK_STREAM, 0);
 
93
 * setsockopt(fd, af, SO_VMCI_BUFFER_MIN_SIZE, &val, sizeof val);
 
94
 * ...
 
95
 * close(fd);
 
96
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
97
 * \endcode
 
98
 */
 
99
 
53
100
#define SO_VMCI_BUFFER_MIN_SIZE             1
 
101
 
 
102
/**
 
103
 * \brief Option name for STREAM socket maximum buffer size.
 
104
 *
 
105
 * Use as the option name in \c setsockopt(3) or \c getsockopt(3) to set or
 
106
 * get an unsigned long long that specifies the maximum size allowed for the
 
107
 * buffer underlying a vSockets STREAM socket.
 
108
 *
 
109
 * \see VMCISock_GetAFValueFd()
 
110
 * \see SO_VMCI_BUFFER_SIZE
 
111
 * \see SO_VMCI_BUFFER_MIN_SIZE
 
112
 *
 
113
 * An example is given below.
 
114
 *
 
115
 * \code
 
116
 * int vmciFd;
 
117
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
118
 * unsigned long long val = 0x4000;
 
119
 * int fd = socket(af, SOCK_STREAM, 0);
 
120
 * setsockopt(fd, af, SO_VMCI_BUFFER_MAX_SIZE, &val, sizeof val);
 
121
 * ...
 
122
 * close(fd);
 
123
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
124
 * \endcode
 
125
 */
 
126
 
54
127
#define SO_VMCI_BUFFER_MAX_SIZE             2
 
128
 
 
129
/**
 
130
 * \brief Option name for socket peer's host-specific VM ID.
 
131
 *
 
132
 * Use as the option name in \c getsockopt(3) to get a host-specific identifier
 
133
 * for the peer endpoint's VM.  The identifier is a signed integer.
 
134
 *
 
135
 * \note Only available for ESX (VMKernel/userworld) endpoints.
 
136
 *
 
137
 * An example is given below.
 
138
 *
 
139
 * \code
 
140
 * int vmciFd;
 
141
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
142
 * int id;
 
143
 * socklen_t len = sizeof id;
 
144
 * int fd = socket(af, SOCK_DGRAM, 0);
 
145
 * getsockopt(fd, af, SO_VMCI_PEER_HOST_VM_ID, &id, &len);
 
146
 * ...
 
147
 * close(fd);
 
148
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
149
 * \endcode
 
150
 */
 
151
 
55
152
#define SO_VMCI_PEER_HOST_VM_ID             3
 
153
 
 
154
/**
 
155
 * \brief Option name for socket's service label.
 
156
 *
 
157
 * Use as the option name in \c setsockopt(3) or \c getsockopt(3) to set or
 
158
 * get the service label for a socket.  The service label is a C-style
 
159
 * NUL-terminated string.
 
160
 *
 
161
 * \note Only available for ESX (VMkernel/userworld) endpoints.
 
162
 */
 
163
 
56
164
#define SO_VMCI_SERVICE_LABEL               4
 
165
 
 
166
/**
 
167
 * \brief Option name for determining if a socket is trusted.
 
168
 *
 
169
 * Use as the option name in \c getsockopt(3) to determine if a socket is
 
170
 * trusted.  The value is a signed integer.
 
171
 *
 
172
 * An example is given below.
 
173
 *
 
174
 * \code
 
175
 * int vmciFd;
 
176
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
177
 * int trusted;
 
178
 * socklen_t len = sizeof trusted;
 
179
 * int fd = socket(af, SOCK_DGRAM, 0);
 
180
 * getsockopt(fd, af, SO_VMCI_TRUSTED, &trusted, &len);
 
181
 * ...
 
182
 * close(fd);
 
183
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
184
 * \endcode
 
185
 */
 
186
 
57
187
#define SO_VMCI_TRUSTED                     5
 
188
 
 
189
/**
 
190
 * \brief Option name for STREAM socket connection timeout.
 
191
 *
 
192
 * Use as the option name in \c setsockopt(3) or \c getsockopt(3) to set or
 
193
 * get the connection timeout for a STREAM socket.  The value is platform
 
194
 * dependent.  On ESX, Linux and Mac OS, it is a \c struct \c timeval.
 
195
 * On Windows, it is a \c DWORD.
 
196
 *
 
197
 * An example is given below.
 
198
 *
 
199
 * \code
 
200
 * int vmciFd;
 
201
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
202
 * struct timeval t = { 5, 100000 }; // 5.1 seconds
 
203
 * int fd = socket(af, SOCK_STREAM, 0);
 
204
 * setsockopt(fd, af, SO_VMCI_CONNECT_TIMEOUT, &t, sizeof t);
 
205
 * ...
 
206
 * close(fd);
 
207
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
208
 * \endcode
 
209
 */
 
210
 
58
211
#define SO_VMCI_CONNECT_TIMEOUT             6
 
212
 
 
213
/**
 
214
 * \brief Option name for using non-blocking send/receive.
 
215
 *
 
216
 * Use as the option name for \c setsockopt(3) or \c getsockopt(3) to set or
 
217
 * get the non-blocking transmit/receive flag for a STREAM socket.  This flag
 
218
 * determines whether \c send() and \c recv() can be called in non-blocking
 
219
 * contexts for the given socket.  The value is a signed integer.
 
220
 *
 
221
 * This option is only relevant to kernel endpoints, where descheduling
 
222
 * the thread of execution is not allowed, for example, while holding a
 
223
 * spinlock.  It is not to be confused with conventional non-blocking socket
 
224
 * operations.
 
225
 *
 
226
 * \note Only available for VMKernel endpoints.
 
227
 *
 
228
 * An example is given below.
 
229
 *
 
230
 * \code
 
231
 * int vmciFd;
 
232
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
233
 * int nonblock;
 
234
 * socklen_t len = sizeof nonblock;
 
235
 * int fd = socket(af, SOCK_STREAM, 0);
 
236
 * getsockopt(fd, af, SO_VMCI_NONBLOCK_TXRX, &nonblock, &len);
 
237
 * ...
 
238
 * close(fd);
 
239
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
240
 * \endcode
 
241
 */
 
242
 
59
243
#define SO_VMCI_NONBLOCK_TXRX               7
60
244
 
61
 
/*
62
 
 * The VMCI sockets address equivalents of INADDR_ANY.  The first works for
63
 
 * the svm_cid (context id) field of the address structure below and indicates
64
 
 * the current guest (or the host, if running outside a guest), while the
65
 
 * second indicates any available port.
 
245
/**
 
246
 * \brief The vSocket equivalent of INADDR_ANY.
 
247
 *
 
248
 * This works for the \c svm_cid field of sockaddr_vm and indicates the
 
249
 * context ID of the current endpoint.
 
250
 *
 
251
 * \see sockaddr_vm
 
252
 *
 
253
 * An example is given below.
 
254
 *
 
255
 * \code
 
256
 * int vmciFd;
 
257
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
258
 * struct sockaddr_vm addr;
 
259
 * int fd = socket(af, SOCK_DGRAM, 0);
 
260
 * addr.svm_family = af;
 
261
 * addr.svm_cid = VMADDR_CID_ANY;
 
262
 * addr.svm_port = 2000;
 
263
 * bind(fd, &addr, sizeof addr);
 
264
 * ...
 
265
 * close(fd);
 
266
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
267
 * \endcode
66
268
 */
67
269
 
68
270
#define VMADDR_CID_ANY  ((unsigned int)-1)
 
271
 
 
272
/**
 
273
 * \brief Bind to any available port.
 
274
 *
 
275
 * Works for the \c svm_port field of sockaddr_vm.
 
276
 *
 
277
 * \see sockaddr_vm
 
278
 *
 
279
 * An example is given below.
 
280
 *
 
281
 * \code
 
282
 * int vmciFd;
 
283
 * int af = VMCISock_GetAFValueFd(&vmciFd);
 
284
 * struct sockaddr_vm addr;
 
285
 * int fd = socket(af, SOCK_DGRAM, 0);
 
286
 * addr.svm_family = af;
 
287
 * addr.svm_cid = VMADDR_CID_ANY;
 
288
 * addr.svm_port = VMADDR_PORT_ANY;
 
289
 * bind(fd, &addr, sizeof addr);
 
290
 * ...
 
291
 * close(fd);
 
292
 * VMCISock_ReleaseAFValueFd(vmciFd);
 
293
 * \endcode
 
294
 */
 
295
 
69
296
#define VMADDR_PORT_ANY ((unsigned int)-1)
70
297
 
71
 
 
72
 
/* Invalid VMCI sockets version. */
 
298
/**
 
299
 * \brief Invalid vSockets version.
 
300
 *
 
301
 * \see VMCISock_Version()
 
302
 */
73
303
 
74
304
#define VMCI_SOCKETS_INVALID_VERSION ((unsigned int)-1)
75
305
 
76
 
/*
77
 
 * Use these macros with the result of VMCISock_Version() to get the
78
 
 * component parts of a valid version.
 
306
/**
 
307
 * \brief The epoch (first) component of the vSockets version.
 
308
 *
 
309
 * A single byte representing the epoch component of the vSockets version.
 
310
 *
 
311
 * \see VMCISock_Version()
 
312
 *
 
313
 * An example is given below.
 
314
 *
 
315
 * \code
 
316
 * unsigned int ver = VMCISock_Version();
 
317
 * unsigned char epoch = VMCI_SOCKETS_VERSION_EPOCH(ver);
 
318
 * \endcode
79
319
 */
80
320
 
81
321
#define VMCI_SOCKETS_VERSION_EPOCH(_v) (((_v) & 0xFF000000) >> 24)
 
322
 
 
323
/**
 
324
 * \brief The major (second) component of the vSockets version.
 
325
 *
 
326
 * A single byte representing the major component of the vSockets version.
 
327
 * Typically changes for every major release of a product.
 
328
 *
 
329
 * \see VMCISock_Version()
 
330
 *
 
331
 * An example is given below.
 
332
 *
 
333
 * \code
 
334
 * unsigned int ver = VMCISock_Version();
 
335
 * unsigned char major = VMCI_SOCKETS_VERSION_MAJOR(ver);
 
336
 * \endcode
 
337
 */
 
338
 
82
339
#define VMCI_SOCKETS_VERSION_MAJOR(_v) (((_v) & 0x00FF0000) >> 16)
 
340
 
 
341
/**
 
342
 * \brief The minor (third) component of the vSockets version.
 
343
 *
 
344
 * Two bytes representing the minor component of the vSockets version.
 
345
 *
 
346
 * \see VMCISock_Version()
 
347
 *
 
348
 * An example is given below.
 
349
 *
 
350
 * \code
 
351
 * unsigned int ver = VMCISock_Version();
 
352
 * unsigned short minor = VMCI_SOCKETS_VERSION_MINOR(ver);
 
353
 * \endcode
 
354
 */
 
355
 
83
356
#define VMCI_SOCKETS_VERSION_MINOR(_v) (((_v) & 0x0000FFFF))
84
357
 
85
 
/* Missing types for some platforms. */
86
 
 
 
358
/** \cond PRIVATE */
87
359
#if defined(_WIN32) || defined(VMKERNEL)
88
360
   typedef unsigned short sa_family_t;
89
361
#endif // _WIN32
94
366
      char sa_data[14];
95
367
   };
96
368
#endif
 
369
/** \endcond */
97
370
 
98
 
/*
99
 
 * Address structure for VSockets VMCI sockets. The address family should be
100
 
 * set to AF_VMCI. The structure members should all align on their natural
101
 
 * boundaries without resorting to compiler packing directives.
 
371
/**
 
372
 * \brief Address structure for vSockets.
 
373
 *
 
374
 * The address family should be set to whatever VMCISock_GetAFValueFd()
 
375
 * returns.  The structure members should all align on their natural
 
376
 * boundaries without resorting to compiler packing directives.  The total
 
377
 * size of this structure should be exactly the same as that of \c struct
 
378
 * \c sockaddr.
 
379
 *
 
380
 * \see VMCISock_GetAFValueFd()
102
381
 */
103
382
 
104
383
struct sockaddr_vm {
105
384
#if defined(__APPLE__)
106
 
   unsigned char svm_len;                           // Mac OS has the length first.
 
385
   unsigned char svm_len;
107
386
#endif // __APPLE__
108
 
   sa_family_t svm_family;                          // AF_VMCI.
109
 
   unsigned short svm_reserved1;                    // Reserved.
110
 
   unsigned int svm_port;                           // Port.
111
 
   unsigned int svm_cid;                            // Context id.
112
 
   unsigned char svm_zero[sizeof(struct sockaddr) - // Same size as sockaddr.
 
387
 
 
388
   /** \brief Address family. \see VMCISock_GetAFValueFd() */
 
389
   sa_family_t svm_family;
 
390
 
 
391
   /** \cond PRIVATE */
 
392
   unsigned short svm_reserved1;
 
393
   /** \endcond */
 
394
 
 
395
   /** \brief Port.  \see VMADDR_PORT_ANY */
 
396
   unsigned int svm_port;
 
397
 
 
398
   /** \brief Context ID.  \see VMADDR_CID_ANY */
 
399
   unsigned int svm_cid;
 
400
 
 
401
   /** \cond PRIVATE */
 
402
   unsigned char svm_zero[sizeof(struct sockaddr) -
113
403
#if defined(__APPLE__)
114
404
                             sizeof(unsigned char) -
115
405
#endif // __APPLE__
117
407
                             sizeof(unsigned short) -
118
408
                             sizeof(unsigned int) -
119
409
                             sizeof(unsigned int)];
 
410
   /** \endcond */
120
411
};
121
412
 
 
413
/** \cond PRIVATE */
 
414
struct uuid_2_cid {
 
415
   unsigned int u2c_context_id;
 
416
   unsigned int u2c_pad;
 
417
   char u2c_uuid_string[128];
 
418
};
 
419
/** \endcond */
122
420
 
123
421
#if defined(_WIN32)
124
422
#  if !defined(NT_INCLUDED)
127
425
#     define VMCI_SOCKETS_VERSION         0x81032058
128
426
#     define VMCI_SOCKETS_GET_AF_VALUE    0x81032068
129
427
#     define VMCI_SOCKETS_GET_LOCAL_CID   0x8103206c
 
428
#     define VMCI_SOCKETS_UUID_2_CID      0x810320a4
130
429
 
131
430
      static __inline unsigned int __VMCISock_DeviceIoControl(DWORD cmd)
132
431
      {
168
467
      {
169
468
         return __VMCISock_DeviceIoControl(VMCI_SOCKETS_GET_LOCAL_CID);
170
469
      }
 
470
 
 
471
      static __inline unsigned int VMCISock_Uuid2ContextId(const char *uuidString)
 
472
      {
 
473
         struct uuid_2_cid io;
 
474
         HANDLE device = CreateFileW(VMCI_SOCKETS_DEVICE, GENERIC_READ, 0, NULL,
 
475
                                     OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
 
476
         io.u2c_context_id = VMADDR_CID_ANY;
 
477
         if (INVALID_HANDLE_VALUE != device) {
 
478
            DWORD ioReturn;
 
479
            strncpy_s(io.u2c_uuid_string, sizeof io.u2c_uuid_string,
 
480
                      uuidString, _TRUNCATE);
 
481
            DeviceIoControl(device, VMCI_SOCKETS_UUID_2_CID, &io, sizeof io,
 
482
                            &io, sizeof io, &ioReturn, NULL);
 
483
            CloseHandle(device);
 
484
            device = INVALID_HANDLE_VALUE;
 
485
         }
 
486
         return io.u2c_context_id;
 
487
      }
171
488
#  endif // !NT_INCLUDED
172
489
#else // _WIN32
173
490
#if (defined(linux) && !defined(VMKERNEL)) || (defined(__APPLE__))
179
496
#  elif defined(__APPLE__) && (KERNEL)
180
497
   /* Nothing to define here. */
181
498
#  else // __KERNEL__
182
 
#  include <sys/types.h>
183
 
#  include <sys/stat.h>
184
499
#  include <fcntl.h>
 
500
#  include <stdio.h>
 
501
#  include <string.h>
185
502
#  include <sys/ioctl.h>
 
503
#  include <sys/stat.h>
 
504
#  include <sys/types.h>
186
505
#  include <unistd.h>
187
506
 
188
 
#  include <stdio.h>
189
 
 
 
507
/** \cond PRIVATE */
190
508
#  define VMCI_SOCKETS_DEFAULT_DEVICE      "/dev/vsock"
191
509
#  define VMCI_SOCKETS_CLASSIC_ESX_DEVICE  "/vmfs/devices/char/vsock/vsock"
192
 
 
193
 
# if defined(linux)
194
 
#  define VMCI_SOCKETS_VERSION       1972
195
 
#  define VMCI_SOCKETS_GET_AF_VALUE  1976
196
 
#  define VMCI_SOCKETS_GET_LOCAL_CID 1977
197
 
# elif defined(__APPLE__)
198
 
#  include <sys/ioccom.h>
199
 
#  define VMCI_SOCKETS_VERSION       _IOR('V', 21,  unsigned)
200
 
#  define VMCI_SOCKETS_GET_AF_VALUE  _IOR('V', 25 , int)
201
 
#  define VMCI_SOCKETS_GET_LOCAL_CID _IOR('V', 26 , unsigned)
 
510
#  if defined(linux)
 
511
#     define VMCI_SOCKETS_VERSION       1972
 
512
#     define VMCI_SOCKETS_GET_AF_VALUE  1976
 
513
#     define VMCI_SOCKETS_GET_LOCAL_CID 1977
 
514
#     define VMCI_SOCKETS_UUID_2_CID    1991
 
515
#  elif defined(__APPLE__)
 
516
#     include <sys/ioccom.h>
 
517
#     define VMCI_SOCKETS_VERSION       _IOR( 'V', 21, unsigned)
 
518
#     define VMCI_SOCKETS_GET_AF_VALUE  _IOR( 'V', 25, int)
 
519
#     define VMCI_SOCKETS_GET_LOCAL_CID _IOR( 'V', 26, unsigned)
 
520
#     define VMCI_SOCKETS_UUID_2_CID    _IOWR('V', 40, struct uuid_2_cid)
202
521
#endif
 
522
/** \endcond */
 
523
 
 
524
   /*
 
525
    ***********************************************************************
 
526
    * VMCISock_Version                                               */ /**
 
527
    *
 
528
    * \brief Retrieve the vSockets version.
 
529
    *
 
530
    * Returns the current version of vSockets.  The version is a 32-bit
 
531
    * unsigned integer that consist of three components: the epoch, the
 
532
    * major version, and the minor version.  Use the \c VMCI_SOCKETS_VERSION
 
533
    * macros to extract the components.
 
534
    *
 
535
    * \see VMCI_SOCKETS_VERSION_EPOCH()
 
536
    * \see VMCI_SOCKETS_VERSION_MAJOR()
 
537
    * \see VMCI_SOCKETS_VERSION_MINOR()
 
538
    *
 
539
    * \retval  VMCI_SOCKETS_INVALID_VERSION  Not available.
 
540
    * \retval  other                         The current version.
 
541
    *
 
542
    * An example is given below.
 
543
    *
 
544
    * \code
 
545
    * unsigned int ver = VMCISock_Version();
 
546
    * if (ver != VMCI_SOCKETS_INVALID_VERSION) {
 
547
    *    printf("vSockets version=%d.%d.%d\n",
 
548
    *           VMCI_SOCKETS_VERSION_EPOCH(ver),
 
549
    *           VMCI_SOCKETS_VERSION_MAJOR(ver),
 
550
    *           VMCI_SOCKETS_VERSION_MINOR(ver));
 
551
    * }
 
552
    * \endcode
 
553
    *
 
554
    ***********************************************************************
 
555
    */
203
556
 
204
557
   static inline unsigned int VMCISock_Version(void)
205
558
   {
223
576
   }
224
577
 
225
578
   /*
226
 
    *----------------------------------------------------------------------------
227
 
    *
228
 
    * VMCISock_GetAFValue and VMCISock_GetAFValueFd --
229
 
    *
230
 
    *      Returns the value to be used for the VMCI Sockets address family.
231
 
    *      This value should be used as the domain argument to socket(2) (when
232
 
    *      you might otherwise use AF_INET).  For VMCI Socket-specific options,
233
 
    *      this value should also be used for the level argument to
234
 
    *      setsockopt(2) (when you might otherwise use SOL_TCP).
235
 
    *
236
 
    *      This function leaves its descriptor to the vsock device open so that
237
 
    *      the socket implementation knows that the socket family is still in
238
 
    *      use.  We do this because we register our address family with the
239
 
    *      kernel on-demand and need a notification to unregister the address
240
 
    *      family.
241
 
    *
242
 
    *      For many programs this behavior is sufficient as is, but some may
243
 
    *      wish to close this descriptor once they are done with VMCI Sockets.
244
 
    *      For these programs, we provide a VMCISock_GetAFValueFd() that takes
245
 
    *      an optional outFd argument.  This value can be provided to
246
 
    *      VMCISock_ReleaseAFValueFd() only after the program no longer will
247
 
    *      use VMCI Sockets.  Note that outFd is only valid in cases where
248
 
    *      VMCISock_GetAFValueFd() returns a non-negative value.
249
 
    *
250
 
    * Results:
251
 
    *      The address family value to use on success, negative error code on
252
 
    *      failure.
253
 
    *
254
 
    *----------------------------------------------------------------------------
 
579
    ***********************************************************************
 
580
    * VMCISock_GetAFValueFd                                          */ /**
 
581
    *
 
582
    * \brief Retrieve the address family value for vSockets.
 
583
    *
 
584
    * Returns the value to be used for the VMCI Sockets address family.
 
585
    * This value should be used as the domain argument to \c socket(2) (when
 
586
    * you might otherwise use \c AF_INET).  For VMCI Socket-specific options,
 
587
    * this value should also be used for the level argument to
 
588
    * \c setsockopt(2) (when you might otherwise use \c SOL_TCP).
 
589
    *
 
590
    * \see VMCISock_ReleaseAFValueFd()
 
591
    * \see sockaddr_vm
 
592
    *
 
593
    * \param[out]    outFd    File descriptor to the VMCI device.  The
 
594
    *                         address family value is valid until this
 
595
    *                         descriptor is closed.  This parameter is
 
596
    *                         only valid if the return value is not -1.
 
597
    *                         Call VMCISock_ReleaseAFValueFd() to  close
 
598
    *                         this descriptor.
 
599
    *
 
600
    * \retval  -1       Not available.
 
601
    * \retval  other    The address family value.
 
602
    *
 
603
    * An example is given below.
 
604
    *
 
605
    * \code
 
606
    * int vmciFd;
 
607
    * int af = VMCISock_GetAFValueFd(&vmciFd);
 
608
    * if (af != -1) {
 
609
    *    int fd = socket(af, SOCK_STREAM, 0);
 
610
    *    ...
 
611
    *    close(fd);
 
612
    *    close(vmciFd);
 
613
    * }
 
614
    * \endcode
 
615
    *
 
616
    ***********************************************************************
255
617
    */
256
618
 
257
619
   static inline int VMCISock_GetAFValueFd(int *outFd)
280
642
      return family;
281
643
   }
282
644
 
 
645
   /** \cond PRIVATE */
 
646
   /*
 
647
    ***********************************************************************
 
648
    * VMCISock_GetAFValue                                            */ /**
 
649
    *
 
650
    * \brief Retrieve the address family value for vSockets.
 
651
    *
 
652
    * Returns the value to be used for the VMCI Sockets address family.
 
653
    * This value should be used as the domain argument to \c socket(2) (when
 
654
    * you might otherwise use \c AF_INET).  For VMCI Socket-specific options,
 
655
    * this value should also be used for the level argument to
 
656
    * \c setsockopt(2) (when you might otherwise use \c SOL_TCP).
 
657
    *
 
658
    * \note This function leaves its descriptor to the vsock device open so
 
659
    * that the socket implementation knows that the socket family is still in
 
660
    * use.  This is done because the address family is registered with the
 
661
    * kernel on-demand and a notification is needed to unregister the address
 
662
    * family.  Use of this function is thus discouraged; please use
 
663
    * VMCISock_GetAFValueFd() instead.
 
664
    *
 
665
    * \see VMCISock_GetAFValueFd()
 
666
    * \see sockaddr_vm
 
667
    *
 
668
    * \retval  -1       Not available.
 
669
    * \retval  other    The address family value.
 
670
    *
 
671
    * An example is given below.
 
672
    *
 
673
    * \code
 
674
    * int af = VMCISock_GetAFValue();
 
675
    * if (af != -1) {
 
676
    *    int fd = socket(af, SOCK_STREAM, 0);
 
677
    *    ...
 
678
    *    close(fd);
 
679
    * }
 
680
    * \endcode
 
681
    *
 
682
    ***********************************************************************
 
683
    */
 
684
 
283
685
   static inline int VMCISock_GetAFValue(void)
284
686
   {
285
687
      return VMCISock_GetAFValueFd(NULL);
286
688
   }
 
689
   /** \endcond PRIVATE */
287
690
 
 
691
   /*
 
692
    ***********************************************************************
 
693
    * VMCISock_ReleaseAFValueFd                                      */ /**
 
694
    *
 
695
    * \brief Release the file descriptor obtained when retrieving the
 
696
    *        address family value.
 
697
    *
 
698
    * Use this to release the file descriptor obtained by calling
 
699
    * VMCISock_GetAFValueFd().
 
700
    *
 
701
    * \see VMCISock_GetAFValueFd()
 
702
    *
 
703
    * \param[in]  fd    File descriptor to the VMCI device.
 
704
    *
 
705
    ***********************************************************************
 
706
    */
288
707
 
289
708
   static inline void VMCISock_ReleaseAFValueFd(int fd)
290
709
   {
293
712
      }
294
713
   }
295
714
 
 
715
   /*
 
716
    ***********************************************************************
 
717
    * VMCISock_GetLocalCID                                           */ /**
 
718
    *
 
719
    * \brief Retrieve the current context ID.
 
720
    *
 
721
    * \see VMADDR_CID_ANY
 
722
    *
 
723
    * \retval  VMADDR_CID_ANY    Not available.
 
724
    * \retval  other             The current context ID.
 
725
    *
 
726
    * An example is given below.
 
727
    *
 
728
    * \code
 
729
    * int vmciFd;
 
730
    * int af = VMCISock_GetAFValueFd(&vmciFd);
 
731
    * struct sockaddr_vm addr;
 
732
    * addr.svm_family = af;
 
733
    * addr.svm_cid = VMCISock_GetLocalCID();
 
734
    * VMCISock_ReleaseAFValueFd(vmciFd);
 
735
    * \endcode
 
736
    *
 
737
    ***********************************************************************
 
738
    */
 
739
 
296
740
   static inline unsigned int VMCISock_GetLocalCID(void)
297
741
   {
298
742
      int fd;
313
757
      close(fd);
314
758
      return contextId;
315
759
   }
 
760
 
 
761
   /*
 
762
    ***********************************************************************
 
763
    * VMCISock_Uuid2ContextId                                        */ /**
 
764
    *
 
765
    * \brief Retrieve the context ID of a running VM, given a VM's UUID.
 
766
    *
 
767
    * Retrieves the context ID of a running virtual machine given that virtual
 
768
    * machines's unique identifier.  The identifier is local to the host and
 
769
    * its meaning is platform-specific.  On ESX, which is currently the only
 
770
    * supported platform, it is the "bios.uuid" field as specified in the VM's
 
771
    * VMX file.
 
772
    *
 
773
    * \see VMADDR_CID_ANY
 
774
    *
 
775
    * \retval  VMADDR_CID_ANY    Not available.
 
776
    * \retval  other             The VM's context ID.
 
777
    *
 
778
    * \note Only available for ESX (userworld) endpoints.
 
779
    *
 
780
    * An example is given below.
 
781
    *
 
782
    * \code
 
783
    * int vmciFd;
 
784
    * int af = VMCISock_GetAFValueFd(&vmciFd);
 
785
    * unsigned int cid = VMCISock_Uuid2ContextId(
 
786
    *    "56 4d 07 d8 cc d5 c4 0d-98 44 dc 1e 8f e0 da f3");
 
787
    * VMCISock_ReleaseAFValueFd(vmciFd);
 
788
    * \endcode
 
789
    *
 
790
    ***********************************************************************
 
791
    */
 
792
 
 
793
   static inline unsigned int VMCISock_Uuid2ContextId(const char *uuidString)
 
794
   {
 
795
      int fd;
 
796
      struct uuid_2_cid io;
 
797
 
 
798
      fd = open(VMCI_SOCKETS_DEFAULT_DEVICE, O_RDWR);
 
799
      if (fd < 0) {
 
800
         fd = open(VMCI_SOCKETS_CLASSIC_ESX_DEVICE, O_RDWR);
 
801
         if (fd < 0) {
 
802
            return VMADDR_CID_ANY;
 
803
         }
 
804
      }
 
805
 
 
806
      strncpy(io.u2c_uuid_string, uuidString, sizeof io.u2c_uuid_string);
 
807
      if (ioctl(fd, VMCI_SOCKETS_UUID_2_CID, &io) < 0) {
 
808
         io.u2c_context_id = VMADDR_CID_ANY;
 
809
      }
 
810
 
 
811
      close(fd);
 
812
      return io.u2c_context_id;
 
813
   }
316
814
#  endif // __KERNEL__
317
815
#endif // linux && !VMKERNEL
318
816
#endif // _WIN32