VirtualBox

source: vbox/trunk/include/VBox/hgcmsvc.h@ 55326

Last change on this file since 55326 was 55326, checked in by vboxsync, 9 years ago

VBox/hgcmsvc.h: Added optional arguments for setPointer() and setString() to make deep copies.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/** @file
2 * Host-Guest Communication Manager (HGCM) - Service library definitions.
3 */
4
5/*
6 * Copyright (C) 2006-2015 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_hgcm_h
27#define ___VBox_hgcm_h
28
29#include <iprt/assert.h>
30#include <iprt/mem.h>
31#include <iprt/string.h>
32#include <VBox/cdefs.h>
33#include <VBox/types.h>
34#include <VBox/err.h>
35#ifdef VBOX_TEST_HGCM_PARMS
36# include <iprt/test.h>
37#endif
38
39/** @todo proper comments. */
40
41/**
42 * Service interface version.
43 *
44 * Includes layout of both VBOXHGCMSVCFNTABLE and VBOXHGCMSVCHELPERS.
45 *
46 * A service can work with these structures if major version
47 * is equal and minor version of service is <= version of the
48 * structures.
49 *
50 * For example when a new helper is added at the end of helpers
51 * structure, then the minor version will be increased. All older
52 * services still can work because they have their old helpers
53 * unchanged.
54 *
55 * Revision history.
56 * 1.1->2.1 Because the pfnConnect now also has the pvClient parameter.
57 * 2.1->2.2 Because pfnSaveState and pfnLoadState were added
58 * 2.2->3.1 Because pfnHostCall is now synchronous, returns rc, and parameters were changed
59 * 3.1->3.2 Because pfnRegisterExtension was added
60 * 3.2->3.3 Because pfnDisconnectClient helper was added
61 * 3.3->4.1 Because the pvService entry and parameter was added
62 * 4.1->4.2 Because the VBOX_HGCM_SVC_PARM_CALLBACK parameter type was added
63 * 4.2->5.1 Removed the VBOX_HGCM_SVC_PARM_CALLBACK parameter type, as
64 * this problem is already solved by service extension callbacks
65 */
66#define VBOX_HGCM_SVC_VERSION_MAJOR (0x0005)
67#define VBOX_HGCM_SVC_VERSION_MINOR (0x0001)
68#define VBOX_HGCM_SVC_VERSION ((VBOX_HGCM_SVC_VERSION_MAJOR << 16) + VBOX_HGCM_SVC_VERSION_MINOR)
69
70
71/** Typed pointer to distinguish a call to service. */
72struct VBOXHGCMCALLHANDLE_TYPEDEF;
73typedef struct VBOXHGCMCALLHANDLE_TYPEDEF *VBOXHGCMCALLHANDLE;
74
75/** Service helpers pointers table. */
76typedef struct _VBOXHGCMSVCHELPERS
77{
78 /** The service has processed the Call request. */
79 DECLR3CALLBACKMEMBER(void, pfnCallComplete, (VBOXHGCMCALLHANDLE callHandle, int32_t rc));
80
81 void *pvInstance;
82
83 /** The service disconnects the client. */
84 DECLR3CALLBACKMEMBER(void, pfnDisconnectClient, (void *pvInstance, uint32_t u32ClientID));
85} VBOXHGCMSVCHELPERS;
86
87typedef VBOXHGCMSVCHELPERS *PVBOXHGCMSVCHELPERS;
88
89
90#define VBOX_HGCM_SVC_PARM_INVALID (0U)
91#define VBOX_HGCM_SVC_PARM_32BIT (1U)
92#define VBOX_HGCM_SVC_PARM_64BIT (2U)
93#define VBOX_HGCM_SVC_PARM_PTR (3U)
94
95typedef struct VBOXHGCMSVCPARM
96{
97 /** VBOX_HGCM_SVC_PARM_* values. */
98 uint32_t type;
99
100 union
101 {
102 uint32_t uint32;
103 uint64_t uint64;
104 struct
105 {
106 uint32_t size;
107 void *addr;
108 } pointer;
109 } u;
110#ifdef __cplusplus
111 /** Extract a uint32_t value from an HGCM parameter structure */
112 int getUInt32(uint32_t *u32)
113 {
114 AssertPtrReturn(u32, VERR_INVALID_POINTER);
115 int rc = VINF_SUCCESS;
116 if (type != VBOX_HGCM_SVC_PARM_32BIT)
117 rc = VERR_INVALID_PARAMETER;
118 if (RT_SUCCESS(rc))
119 *u32 = u.uint32;
120 return rc;
121 }
122
123 /** Extract a uint64_t value from an HGCM parameter structure */
124 int getUInt64(uint64_t *u64)
125 {
126 AssertPtrReturn(u64, VERR_INVALID_POINTER);
127 int rc = VINF_SUCCESS;
128 if (type != VBOX_HGCM_SVC_PARM_64BIT)
129 rc = VERR_INVALID_PARAMETER;
130 if (RT_SUCCESS(rc))
131 *u64 = u.uint64;
132 return rc;
133 }
134
135 /** Extract a pointer value from an HGCM parameter structure */
136 int getPointer(void **ppv, uint32_t *pcb)
137 {
138 AssertPtrReturn(ppv, VERR_INVALID_POINTER);
139 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
140 if (type == VBOX_HGCM_SVC_PARM_PTR)
141 {
142 *ppv = u.pointer.addr;
143 *pcb = u.pointer.size;
144 return VINF_SUCCESS;
145 }
146
147 return VERR_INVALID_PARAMETER;
148 }
149
150 /** Extract a constant pointer value from an HGCM parameter structure */
151 int getPointer(const void **ppcv, uint32_t *pcb)
152 {
153 AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
154 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
155 void *pv;
156 int rc = getPointer(&pv, pcb);
157 *ppcv = pv;
158 return rc;
159 }
160
161 /** Extract a pointer value to a non-empty buffer from an HGCM parameter
162 * structure */
163 int getBuffer(void **ppv, uint32_t *pcb)
164 {
165 AssertPtrReturn(ppv, VERR_INVALID_POINTER);
166 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
167 void *pv = NULL;
168 uint32_t cb = 0;
169 int rc = getPointer(&pv, &cb);
170 if ( RT_SUCCESS(rc)
171 && VALID_PTR(pv)
172 && cb > 0)
173 {
174 *ppv = pv;
175 *pcb = cb;
176 return VINF_SUCCESS;
177 }
178
179 return VERR_INVALID_PARAMETER;
180 }
181
182 /** Extract a pointer value to a non-empty constant buffer from an HGCM
183 * parameter structure */
184 int getBuffer(const void **ppcv, uint32_t *pcb)
185 {
186 AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
187 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
188 void *pcv = NULL;
189 int rc = getBuffer(&pcv, pcb);
190 *ppcv = pcv;
191 return rc;
192 }
193
194 /** Extract a string value from an HGCM parameter structure */
195 int getString(char **ppch, uint32_t *pcb)
196 {
197 uint32_t cb = 0;
198 char *pch = NULL;
199 int rc = getBuffer((void **)&pch, &cb);
200 if (RT_FAILURE(rc))
201 {
202 *ppch = NULL;
203 *pcb = 0;
204 return rc;
205 }
206 rc = RTStrValidateEncodingEx(pch, cb,
207 RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
208 *ppch = pch;
209 *pcb = cb;
210 return rc;
211 }
212
213 /** Extract a constant string value from an HGCM parameter structure */
214 int getString(const char **ppch, uint32_t *pcb)
215 {
216 char *pch = NULL;
217 int rc = getString(&pch, pcb);
218 *ppch = pch;
219 return rc;
220 }
221
222 /** Set a uint32_t value to an HGCM parameter structure */
223 void setUInt32(uint32_t u32)
224 {
225 type = VBOX_HGCM_SVC_PARM_32BIT;
226 u.uint32 = u32;
227 }
228
229 /** Set a uint64_t value to an HGCM parameter structure */
230 void setUInt64(uint64_t u64)
231 {
232 type = VBOX_HGCM_SVC_PARM_64BIT;
233 u.uint64 = u64;
234 }
235
236 /** Set a pointer value to an HGCM parameter structure */
237 int setPointer(void *pv, uint32_t cb, bool fDeepCopy = false)
238 {
239 type = VBOX_HGCM_SVC_PARM_PTR;
240 void *addr = fDeepCopy ? RTMemDup(pv, cb) : pv;
241 if (fDeepCopy && !addr)
242 return VERR_NO_MEMORY;
243 u.pointer.addr = addr;
244 u.pointer.size = cb;
245 }
246
247 /** Set a const string value to an HGCM parameter structure */
248 int setString(const char *psz, bool fDeepCopy = false)
249 {
250 type = VBOX_HGCM_SVC_PARM_PTR;
251 void *pszaddr = fDeepCopy ? (void *)RTStrDup(psz) : (void *)psz;
252 if (fDeepCopy && !pszaddr)
253 return VERR_NO_MEMORY;
254 u.pointer.addr = pszaddr;
255 u.pointer.size = (uint32_t)strlen((char *)pszaddr) + 1;
256 }
257
258#ifdef VBOX_TEST_HGCM_PARMS
259 /** Test the getString member function. Indirectly tests the getPointer
260 * and getBuffer APIs.
261 * @param hTest an running IPRT test
262 * @param aType the type that the parameter should be set to before
263 * calling getString
264 * @param apcc the value that the parameter should be set to before
265 * calling getString, and also the address (!) which we
266 * expect getString to return. Stricter than needed of
267 * course, but I was feeling lazy.
268 * @param acb the size that the parameter should be set to before
269 * calling getString, and also the size which we expect
270 * getString to return.
271 * @param rcExp the expected return value of the call to getString.
272 */
273 void doTestGetString(RTTEST hTest, uint32_t aType, const char *apcc,
274 uint32_t acb, int rcExp)
275 {
276 /* An RTTest API like this, which would print out an additional line
277 * of context if a test failed, would be nice. This is because the
278 * line number alone doesn't help much here, given that this is a
279 * subroutine called many times. */
280 /*
281 RTTestContextF(hTest,
282 ("doTestGetString, aType=%u, apcc=%p, acp=%u, rcExp=%Rrc",
283 aType, apcc, acp, rcExp));
284 */
285 setPointer((void *)apcc, acb);
286 type = aType; /* in case we don't want VBOX_HGCM_SVC_PARM_PTR */
287 const char *pcc = NULL;
288 uint32_t cb = 0;
289 int rc = getString(&pcc, &cb);
290 RTTEST_CHECK_RC(hTest, rc, rcExp);
291 if (RT_SUCCESS(rcExp))
292 {
293 RTTEST_CHECK_MSG_RETV(hTest, (pcc == apcc),
294 (hTest, "expected %p, got %p", apcc, pcc));
295 RTTEST_CHECK_MSG_RETV(hTest, (cb == acb),
296 (hTest, "expected %u, got %u", acb, cb));
297 }
298 }
299
300 /** Run some unit tests on the getString method and indirectly test
301 * getPointer and getBuffer as well. */
302 void testGetString(RTTEST hTest)
303 {
304 RTTestSub(hTest, "HGCM string parameter handling");
305 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_32BIT, "test", 3,
306 VERR_INVALID_PARAMETER);
307 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 5,
308 VINF_SUCCESS);
309 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 3,
310 VERR_BUFFER_OVERFLOW);
311 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test\xf0", 6,
312 VERR_INVALID_UTF8_ENCODING);
313 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 0,
314 VERR_INVALID_PARAMETER);
315 doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, (const char *)0x1, 5,
316 VERR_INVALID_PARAMETER);
317 RTTestSubDone(hTest);
318 }
319#endif
320
321 VBOXHGCMSVCPARM() : type(VBOX_HGCM_SVC_PARM_INVALID) {}
322#endif /* __cplusplus */
323} VBOXHGCMSVCPARM;
324
325typedef VBOXHGCMSVCPARM *PVBOXHGCMSVCPARM;
326
327#ifdef VBOX_WITH_CRHGSMI
328typedef void * HGCMCVSHANDLE;
329
330typedef DECLCALLBACK(void) HGCMHOSTFASTCALLCB (int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext);
331typedef HGCMHOSTFASTCALLCB *PHGCMHOSTFASTCALLCB;
332#endif
333
334
335/** Service specific extension callback.
336 * This callback is called by the service to perform service specific operation.
337 *
338 * @param pvExtension The extension pointer.
339 * @param u32Function What the callback is supposed to do.
340 * @param pvParm The function parameters.
341 * @param cbParm The size of the function parameters.
342 */
343typedef DECLCALLBACK(int) FNHGCMSVCEXT(void *pvExtension, uint32_t u32Function, void *pvParm, uint32_t cbParms);
344typedef FNHGCMSVCEXT *PFNHGCMSVCEXT;
345
346/** The Service DLL entry points.
347 *
348 * HGCM will call the DLL "VBoxHGCMSvcLoad"
349 * function and the DLL must fill in the VBOXHGCMSVCFNTABLE
350 * with function pointers.
351 */
352
353/* The structure is used in separately compiled binaries so an explicit packing is required. */
354#pragma pack(1)
355typedef struct _VBOXHGCMSVCFNTABLE
356{
357 /** Filled by HGCM */
358
359 /** Size of the structure. */
360 uint32_t cbSize;
361
362 /** Version of the structure, including the helpers. */
363 uint32_t u32Version;
364
365 PVBOXHGCMSVCHELPERS pHelpers;
366
367 /** Filled by the service. */
368
369 /** Size of client information the service want to have. */
370 uint32_t cbClient;
371#if ARCH_BITS == 64
372 /** Ensure that the following pointers are properly aligned on 64-bit system. */
373 uint32_t u32Alignment0;
374#endif
375
376 /** Uninitialize service */
377 DECLR3CALLBACKMEMBER(int, pfnUnload, (void *pvService));
378
379 /** Inform the service about a client connection. */
380 DECLR3CALLBACKMEMBER(int, pfnConnect, (void *pvService, uint32_t u32ClientID, void *pvClient));
381
382 /** Inform the service that the client wants to disconnect. */
383 DECLR3CALLBACKMEMBER(int, pfnDisconnect, (void *pvService, uint32_t u32ClientID, void *pvClient));
384
385 /** Service entry point.
386 * Return code is passed to pfnCallComplete callback.
387 */
388 DECLR3CALLBACKMEMBER(void, pfnCall, (void *pvService, VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]));
389
390 /** Host Service entry point meant for privileged features invisible to the guest.
391 * Return code is passed to pfnCallComplete callback.
392 */
393 DECLR3CALLBACKMEMBER(int, pfnHostCall, (void *pvService, uint32_t function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]));
394
395 /** Inform the service about a VM save operation. */
396 DECLR3CALLBACKMEMBER(int, pfnSaveState, (void *pvService, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM));
397
398 /** Inform the service about a VM load operation. */
399 DECLR3CALLBACKMEMBER(int, pfnLoadState, (void *pvService, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM));
400
401 /** Register a service extension callback. */
402 DECLR3CALLBACKMEMBER(int, pfnRegisterExtension, (void *pvService, PFNHGCMSVCEXT pfnExtension, void *pvExtension));
403
404 /** User/instance data pointer for the service. */
405 void *pvService;
406
407} VBOXHGCMSVCFNTABLE;
408#pragma pack()
409
410
411/** Service initialization entry point. */
412typedef DECLCALLBACK(int) VBOXHGCMSVCLOAD(VBOXHGCMSVCFNTABLE *ptable);
413typedef VBOXHGCMSVCLOAD *PFNVBOXHGCMSVCLOAD;
414#define VBOX_HGCM_SVCLOAD_NAME "VBoxHGCMSvcLoad"
415
416#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette