~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to lib/include/vmci_sockets.h

  • Committer: Evan Broder
  • Date: 2010-03-21 23:26:53 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: broder@mit.edu-20100321232653-5a57r7v7ch4o6byv
Merging shared upstream rev into target branch.

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
 
 * vmci_sockets.h --
21
 
 *
22
 
 *    VMCI sockets public constants and types.
23
 
 */
24
 
 
25
 
#ifndef _VMCI_SOCKETS_H_
26
 
#define _VMCI_SOCKETS_H_
27
 
 
28
 
 
29
 
#if defined(_WIN32)
30
 
#  if !defined(_DDK_DRIVER_)
31
 
#     include <winsock2.h>
32
 
#  endif // _DDK_DRIVER_
33
 
#else // _WIN32
34
 
#if defined(linux) && !defined(VMKERNEL)
35
 
#  if !defined(__KERNEL__)
36
 
#    include <sys/socket.h>
37
 
#  endif // __KERNEL__
38
 
#else // linux && !VMKERNEL
39
 
#  if defined(__APPLE__)
40
 
#    include <sys/socket.h>
41
 
#    include <string.h>
42
 
#  endif // __APPLE__
43
 
#endif // linux && !VMKERNEL
44
 
#endif
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.
50
 
 */
51
 
#define SO_VMCI_BUFFER_SIZE                 0
52
 
#define SO_VMCI_BUFFER_MIN_SIZE             1
53
 
#define SO_VMCI_BUFFER_MAX_SIZE             2
54
 
#define SO_VMCI_PEER_HOST_VM_ID             3
55
 
 
56
 
/*
57
 
 * The VMCI sockets address equivalents of INADDR_ANY.  The first works for
58
 
 * the svm_cid (context id) field of the address structure below and indicates
59
 
 * the current guest (or the host, if running outside a guest), while the
60
 
 * second indicates any available port.
61
 
 */
62
 
#define VMADDR_CID_ANY  ((unsigned int) -1)
63
 
#define VMADDR_PORT_ANY ((unsigned int) -1)
64
 
 
65
 
 
66
 
#if defined(_WIN32) || defined(VMKERNEL)
67
 
   typedef unsigned short sa_family_t;
68
 
#endif // _WIN32
69
 
 
70
 
#if defined(VMKERNEL)
71
 
   struct sockaddr {
72
 
      sa_family_t sa_family;
73
 
      char sa_data[14];
74
 
   };
75
 
#endif
76
 
 
77
 
/*
78
 
 * Address structure for VSockets VMCI sockets. The address family should be
79
 
 * set to AF_VMCI. The structure members should all align on their natural
80
 
 * boundaries without resorting to compiler packing directives.
81
 
 */
82
 
 
83
 
struct sockaddr_vm {
84
 
#if defined(__APPLE__)
85
 
   unsigned char svm_len;                           // Mac OS has the length first.
86
 
#endif // __APPLE__
87
 
   sa_family_t svm_family;                          // AF_VMCI.
88
 
   unsigned short svm_reserved1;                    // Reserved.
89
 
   unsigned int svm_port;                           // Port.
90
 
   unsigned int svm_cid;                            // Context id.
91
 
   unsigned char svm_zero[sizeof(struct sockaddr) - // Same size as sockaddr.
92
 
#if defined(__APPLE__)
93
 
                             sizeof(unsigned char) -
94
 
#endif // __APPLE__
95
 
                             sizeof(sa_family_t) -
96
 
                             sizeof(unsigned short) -
97
 
                             sizeof(unsigned int) -
98
 
                             sizeof(unsigned int)];
99
 
};
100
 
 
101
 
 
102
 
#if defined(_WIN32)
103
 
#  if !defined(_DDK_DRIVER_)
104
 
#     include <winioctl.h>
105
 
#     define VMCI_SOCKETS_DEVICE          L"\\\\.\\VMCI"
106
 
#     define VMCI_SOCKETS_GET_AF_VALUE    0x81032068
107
 
#     define VMCI_SOCKETS_GET_LOCAL_CID   0x8103206c
108
 
      static __inline int VMCISock_GetAFValue(void)
109
 
      {
110
 
         int afvalue = -1;
111
 
         HANDLE device = CreateFileW(VMCI_SOCKETS_DEVICE, GENERIC_READ, 0, NULL,
112
 
                                     OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
113
 
         if (INVALID_HANDLE_VALUE != device) {
114
 
            DWORD ioReturn;
115
 
            DeviceIoControl(device, VMCI_SOCKETS_GET_AF_VALUE, &afvalue,
116
 
                            sizeof afvalue, &afvalue, sizeof afvalue,
117
 
                            &ioReturn, NULL);
118
 
            CloseHandle(device);
119
 
            device = INVALID_HANDLE_VALUE;
120
 
         }
121
 
         return afvalue;
122
 
      }
123
 
   
124
 
      static __inline unsigned int VMCISock_GetLocalCID(void)
125
 
      {
126
 
         unsigned int cid = VMADDR_CID_ANY;
127
 
         HANDLE device = CreateFileW(VMCI_SOCKETS_DEVICE, GENERIC_READ, 0, NULL,
128
 
                                     OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
129
 
         if (INVALID_HANDLE_VALUE != device) {
130
 
            DWORD ioReturn;
131
 
            DeviceIoControl(device, VMCI_SOCKETS_GET_LOCAL_CID, &cid,
132
 
                            sizeof cid, &cid, sizeof cid, &ioReturn,
133
 
                            NULL);
134
 
            CloseHandle(device);
135
 
            device = INVALID_HANDLE_VALUE;
136
 
         }
137
 
         return cid;
138
 
      }
139
 
#  endif // _DDK_DRIVER_
140
 
#else // _WIN32
141
 
#if defined(linux) && !defined(VMKERNEL)
142
 
#  ifdef __KERNEL__
143
 
   void VMCISock_KernelRegister(void);
144
 
   void VMCISock_KernelDeregister(void);
145
 
   int VMCISock_GetAFValue(void);
146
 
   int VMCISock_GetLocalCID(void);
147
 
#  else // __KERNEL__
148
 
#  include <sys/types.h>
149
 
#  include <sys/stat.h>
150
 
#  include <fcntl.h>
151
 
#  include <sys/ioctl.h>
152
 
#  include <unistd.h>
153
 
 
154
 
#  include <stdio.h>
155
 
 
156
 
#  define VMCI_SOCKETS_DEFAULT_DEVICE      "/dev/vsock"
157
 
#  define VMCI_SOCKETS_CLASSIC_ESX_DEVICE  "/vmfs/devices/char/vsock/vsock"
158
 
#  define IOCTL_VMCI_SOCKETS_GET_AF_VALUE  1976
159
 
#  define IOCTL_VMCI_SOCKETS_GET_LOCAL_CID 1977
160
 
 
161
 
   /*
162
 
    *----------------------------------------------------------------------------
163
 
    *
164
 
    * VMCISock_GetAFValue and VMCISock_GetAFValueFd --
165
 
    *
166
 
    *      Returns the value to be used for the VMCI Sockets address family.
167
 
    *      This value should be used as the domain argument to socket(2) (when
168
 
    *      you might otherwise use AF_INET).  For VMCI Socket-specific options,
169
 
    *      this value should also be used for the level argument to
170
 
    *      setsockopt(2) (when you might otherwise use SOL_TCP).
171
 
    *
172
 
    *      This function leaves its descriptor to the vsock device open so that
173
 
    *      the socket implementation knows that the socket family is still in
174
 
    *      use.  We do this because we register our address family with the
175
 
    *      kernel on-demand and need a notification to unregister the address
176
 
    *      family.
177
 
    *
178
 
    *      For many programs this behavior is sufficient as is, but some may
179
 
    *      wish to close this descriptor once they are done with VMCI Sockets.
180
 
    *      For these programs, we provide a VMCISock_GetAFValueFd() that takes
181
 
    *      an optional outFd argument.  This value can be provided to
182
 
    *      VMCISock_ReleaseAFValueFd() only after the program no longer will
183
 
    *      use VMCI Sockets.  Note that outFd is only valid in cases where
184
 
    *      VMCISock_GetAFValueFd() returns a non-negative value.
185
 
    *
186
 
    * Results:
187
 
    *      The address family value to use on success, negative error code on
188
 
    *      failure.
189
 
    *
190
 
    *----------------------------------------------------------------------------
191
 
    */
192
 
 
193
 
   static inline int VMCISock_GetAFValueFd(int *outFd)
194
 
   {
195
 
      int fd;
196
 
      int family;
197
 
 
198
 
      fd = open(VMCI_SOCKETS_DEFAULT_DEVICE, O_RDWR);
199
 
      if (fd < 0) {
200
 
         fd = open(VMCI_SOCKETS_CLASSIC_ESX_DEVICE, O_RDWR);
201
 
         if (fd < 0) {
202
 
            return -1;
203
 
         }
204
 
      }
205
 
 
206
 
      if (ioctl(fd, IOCTL_VMCI_SOCKETS_GET_AF_VALUE, &family) < 0) {
207
 
         family = -1;
208
 
      }
209
 
 
210
 
      if (family < 0) {
211
 
         close(fd);
212
 
      } else if (outFd) {
213
 
         *outFd = fd;
214
 
      }
215
 
 
216
 
      return family;
217
 
   }
218
 
 
219
 
   static inline int VMCISock_GetAFValue(void)
220
 
   {
221
 
      return VMCISock_GetAFValueFd(NULL);
222
 
   }
223
 
 
224
 
 
225
 
   static inline void VMCISock_ReleaseAFValueFd(int fd)
226
 
   {
227
 
      if (fd >= 0) {
228
 
         close(fd);
229
 
      }
230
 
   }
231
 
 
232
 
   static inline unsigned int VMCISock_GetLocalCID(void)
233
 
   {
234
 
      int fd;
235
 
      unsigned int contextId;
236
 
 
237
 
      fd = open(VMCI_SOCKETS_DEFAULT_DEVICE, O_RDWR);
238
 
      if (fd < 0) {
239
 
         fd = open(VMCI_SOCKETS_CLASSIC_ESX_DEVICE, O_RDWR);
240
 
         if (fd < 0) {
241
 
            return VMADDR_CID_ANY;
242
 
         }
243
 
      }
244
 
 
245
 
      if (ioctl(fd, IOCTL_VMCI_SOCKETS_GET_LOCAL_CID, &contextId) < 0) {
246
 
         contextId = VMADDR_CID_ANY;
247
 
      }
248
 
 
249
 
      close(fd);
250
 
      return contextId;
251
 
   }
252
 
#  endif // __KERNEL__
253
 
#else
254
 
#if defined(__APPLE__) && !defined(KERNEL)
255
 
extern int VMCISock_GetAFValue(void);
256
 
extern unsigned int VMCISock_GetLocalCID(void);
257
 
#endif // __APPLE__ && !KERNEL
258
 
#endif // linux && !VMKERNEL
259
 
#endif // _WIN32
260
 
 
261
 
 
262
 
#endif // _VMCI_SOCKETS_H_
263