VirtualBox

source: vbox/trunk/include/VBox/HostServices/GuestPropertySvc.h@ 13780

Last change on this file since 13780 was 13780, checked in by vboxsync, 16 years ago

Guest Properties (HostServices and Main): forgotten header

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.9 KB
Line 
1/** @file
2 * Guest property service:
3 * Common header for host service and guest clients.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#ifndef ___VBox_HostService_GuestPropertyService_h
32#define ___VBox_HostService_GuestPropertyService_h
33
34#include <VBox/types.h>
35#include <VBox/VBoxGuest.h>
36#include <VBox/hgcmsvc.h>
37#include <VBox/log.h>
38#include <iprt/assert.h>
39
40#include <string.h>
41
42#ifdef RT_OS_WINDOWS
43# define strncasecmp strnicmp
44#endif
45
46/** Everything defined in this file lives in this namespace. */
47namespace guestProp {
48
49/******************************************************************************
50* Typedefs, constants and inlines *
51******************************************************************************/
52
53/** Maximum length for property names */
54enum { MAX_NAME_LEN = 64 };
55/** Maximum length for property values */
56enum { MAX_VALUE_LEN = 128 };
57/** Maximum number of properties per guest */
58enum { MAX_PROPS = 256 };
59/** Maximum size for enumeration patterns */
60enum { MAX_PATTERN_LEN = 1024 };
61
62/**
63 * The guest property flag values which are currently accepted.
64 */
65enum ePropFlags
66{
67 NILFLAG = 0,
68 TRANSIENT = RT_BIT(1),
69 RDONLYGUEST = RT_BIT(2),
70 RDONLYHOST = RT_BIT(3),
71 READONLY = RDONLYGUEST | RDONLYHOST,
72 ALLFLAGS = TRANSIENT | READONLY
73};
74
75/**
76 * Get the name of a flag as a string.
77 * @returns the name, or NULL if fFlag is invalid.
78 * @param fFlag the flag. Must be a value from the ePropFlags enumeration
79 * list.
80 */
81DECLINLINE(const char *) flagName(uint32_t fFlag)
82{
83 switch(fFlag)
84 {
85 case TRANSIENT:
86 return "TRANSIENT";
87 case RDONLYGUEST:
88 return "RDONLYGUEST";
89 case RDONLYHOST:
90 return "RDONLYHOST";
91 case READONLY:
92 return "READONLY";
93 default:
94 return NULL;
95 }
96}
97
98/**
99 * Get the length of a flag name as returned by flagName.
100 * @returns the length, or 0 if fFlag is invalid.
101 * @param fFlag the flag. Must be a value from the ePropFlags enumeration
102 * list.
103 */
104DECLINLINE(size_t) flagNameLen(uint32_t fFlag)
105{
106 const char *pcszName = flagName(fFlag);
107 return RT_LIKELY(pcszName != NULL) ? strlen(pcszName) : 0;
108}
109
110/**
111 * Maximum length for the property flags field. We only ever return one of
112 * RDONLYGUEST, RDONLYHOST and RDONLY
113 */
114enum { MAX_FLAGS_LEN = sizeof("TRANSIENT, RDONLYGUEST") };
115
116/**
117 * Parse a guest properties flags string for flag names and make sure that
118 * there is no junk text in the string.
119 * @returns IPRT status code
120 * @returns VERR_INVALID_PARAM if the flag string is not valid
121 * @param pcszFlags the flag string to parse
122 * @param pfFlags where to store the parse result. May not be NULL.
123 * @note This function is also inline because it must be accessible from
124 * several modules and it does not seem reasonable to put it into
125 * its own library.
126 */
127DECLINLINE(int) validateFlags(const char *pcszFlags, uint32_t *pfFlags)
128{
129 static uint32_t flagList[] =
130 {
131 TRANSIENT, READONLY, RDONLYGUEST, RDONLYHOST
132 };
133 const char *pcszNext = pcszFlags;
134 int rc = VINF_SUCCESS;
135 uint32_t fFlags = 0;
136 AssertLogRelReturn(VALID_PTR(pfFlags), VERR_INVALID_POINTER);
137 AssertLogRelReturn(VALID_PTR(pcszFlags), VERR_INVALID_POINTER);
138 while (' ' == *pcszNext)
139 ++pcszNext;
140 while ((*pcszNext != '\0') && RT_SUCCESS(rc))
141 {
142 unsigned i = 0;
143 for (; i < RT_ELEMENTS(flagList); ++i)
144 if (strncasecmp(pcszNext, flagName(flagList[i]),
145 flagNameLen(flagList[i])
146 ) == 0
147 )
148 break;
149 if (RT_ELEMENTS(flagList) == i)
150 rc = VERR_PARSE_ERROR;
151 else
152 {
153 fFlags |= flagList[i];
154 pcszNext += flagNameLen(flagList[i]);
155 while (' ' == *pcszNext)
156 ++pcszNext;
157 if (',' == *pcszNext)
158 ++pcszNext;
159 else if (*pcszNext != '\0')
160 rc = VERR_PARSE_ERROR;
161 while (' ' == *pcszNext)
162 ++pcszNext;
163 }
164 }
165 if (RT_SUCCESS(rc))
166 *pfFlags = fFlags;
167 return rc;
168}
169
170/**
171 * Write out flags to a string.
172 * @returns IPRT status code
173 * @param fFlags the flags to write out
174 * @param pszFlags where to write the flags string. This must point to
175 * a buffer of size (at least) MAX_FLAGS_LEN.
176 */
177DECLINLINE(int) writeFlags(uint32_t fFlags, char *pszFlags)
178{
179 static uint32_t flagList[] =
180 {
181 TRANSIENT, READONLY, RDONLYGUEST, RDONLYHOST
182 };
183 char *pszNext = pszFlags;
184 int rc = VINF_SUCCESS;
185 AssertLogRelReturn(VALID_PTR(pszFlags), VERR_INVALID_POINTER);
186 if ((fFlags & ~ALLFLAGS) != NILFLAG)
187 rc = VERR_INVALID_PARAMETER;
188 if (RT_SUCCESS(rc))
189 {
190 unsigned i = 0;
191 for (; i < RT_ELEMENTS(flagList); ++i)
192 {
193 if (flagList[i] == (fFlags & flagList[i]))
194 {
195 strcpy(pszNext, flagName(flagList[i]));
196 pszNext += flagNameLen(flagList[i]);
197 fFlags &= ~flagList[i];
198 if (fFlags != NILFLAG)
199 {
200 strcpy(pszNext, ", ");
201 pszNext += 2;
202 }
203 }
204 }
205 *pszNext = '\0';
206 if (fFlags != NILFLAG)
207 rc = VERR_INVALID_PARAMETER; /* But pszFlags will still be set right. */
208 }
209 return rc;
210}
211
212/*
213 * The service functions which are callable by host.
214 */
215enum eHostFn
216{
217 /**
218 * Set properties in a block. The parameters are pointers to
219 * NULL-terminated arrays containing the paramters. These are, in order,
220 * name, value, timestamp, flags. Strings are stored as pointers to
221 * mutable utf8 data. All parameters must be supplied.
222 */
223 SET_PROPS_HOST = 1,
224 /**
225 * Get the value attached to a guest property
226 * The parameter format matches that of GET_PROP.
227 */
228 GET_PROP_HOST = 2,
229 /**
230 * Set the value attached to a guest property
231 * The parameter format matches that of SET_PROP.
232 */
233 SET_PROP_HOST = 3,
234 /**
235 * Set the value attached to a guest property
236 * The parameter format matches that of SET_PROP_VALUE.
237 */
238 SET_PROP_VALUE_HOST = 4,
239 /**
240 * Remove a guest property.
241 * The parameter format matches that of DEL_PROP.
242 */
243 DEL_PROP_HOST = 5,
244 /**
245 * Enumerate guest properties.
246 * The parameter format matches that of ENUM_PROPS.
247 */
248 ENUM_PROPS_HOST = 6
249};
250
251/**
252 * The service functions which are called by guest. The numbers may not change,
253 * so we hardcode them.
254 */
255enum eGuestFn
256{
257 /** Get a guest property */
258 GET_PROP = 1,
259 /** Set a guest property */
260 SET_PROP = 2,
261 /** Set just the value of a guest property */
262 SET_PROP_VALUE = 3,
263 /** Delete a guest property */
264 DEL_PROP = 4,
265 /** Enumerate guest properties */
266 ENUM_PROPS = 5
267};
268
269/**
270 * Data structure to pass to the service extension callback. We use this to
271 * notify the host of changes to properties.
272 */
273typedef struct _HOSTCALLBACKDATA
274{
275 /** Magic number to identify the structure */
276 uint32_t u32Magic;
277 /** The name of the property that was changed */
278 const char *pcszName;
279 /** The new property value, or NULL if the property was deleted */
280 const char *pcszValue;
281 /** The timestamp of the modification */
282 uint64_t u64Timestamp;
283 /** The flags field of the modified property */
284 const char *pcszFlags;
285} HOSTCALLBACKDATA, *PHOSTCALLBACKDATA;
286
287enum
288{
289 /** Magic number for sanity checking the HOSTCALLBACKDATA structure */
290 HOSTCALLBACKMAGIC = 0x69c87a78
291};
292
293/**
294 * HGCM parameter structures. Packing is explicitly defined as this is a wire format.
295 */
296#pragma pack (1)
297/** The guest is requesting the value of a property */
298typedef struct _GetProperty
299{
300 VBoxGuestHGCMCallInfo hdr;
301
302 /**
303 * The property name (IN pointer)
304 * This must fit to a number of criteria, namely
305 * - Only Utf8 strings are allowed
306 * - Less than or equal to MAX_NAME_LEN bytes in length
307 * - Zero terminated
308 */
309 HGCMFunctionParameter name;
310
311 /**
312 * The returned string data will be placed here. (OUT pointer)
313 * This call returns two null-terminated strings which will be placed one
314 * after another: value and flags.
315 */
316 HGCMFunctionParameter buffer;
317
318 /**
319 * The property timestamp. (OUT uint64_t)
320 */
321
322 HGCMFunctionParameter timestamp;
323
324 /**
325 * If the buffer provided was large enough this will contain the size of
326 * the returned data. Otherwise it will contain the size of the buffer
327 * needed to hold the data and VERR_BUFFER_OVERFLOW will be returned.
328 * (OUT uint32_t)
329 */
330 HGCMFunctionParameter size;
331} GetProperty;
332
333/** The guest is requesting to change a property */
334typedef struct _SetProperty
335{
336 VBoxGuestHGCMCallInfo hdr;
337
338 /**
339 * The property name. (IN pointer)
340 * This must fit to a number of criteria, namely
341 * - Only Utf8 strings are allowed
342 * - Less than or equal to MAX_NAME_LEN bytes in length
343 * - Zero terminated
344 */
345 HGCMFunctionParameter name;
346
347 /**
348 * The value of the property (IN pointer)
349 * Criteria as for the name parameter, but with length less than or equal to
350 * MAX_VALUE_LEN.
351 */
352 HGCMFunctionParameter value;
353
354 /**
355 * The property flags (IN pointer)
356 * This is a comma-separated list of the format flag=value
357 * The length must be less than or equal to MAX_FLAGS_LEN and only
358 * known flag names and values will be accepted.
359 */
360 HGCMFunctionParameter flags;
361} SetProperty;
362
363/** The guest is requesting to change the value of a property */
364typedef struct _SetPropertyValue
365{
366 VBoxGuestHGCMCallInfo hdr;
367
368 /**
369 * The property name. (IN pointer)
370 * This must fit to a number of criteria, namely
371 * - Only Utf8 strings are allowed
372 * - Less than or equal to MAX_NAME_LEN bytes in length
373 * - Zero terminated
374 */
375 HGCMFunctionParameter name;
376
377 /**
378 * The value of the property (IN pointer)
379 * Criteria as for the name parameter, but with length less than or equal to
380 * MAX_VALUE_LEN.
381 */
382 HGCMFunctionParameter value;
383} SetPropertyValue;
384
385/** The guest is requesting to remove a property */
386typedef struct _DelProperty
387{
388 VBoxGuestHGCMCallInfo hdr;
389
390 /**
391 * The property name. This must fit to a number of criteria, namely
392 * - Only Utf8 strings are allowed
393 * - Less than or equal to MAX_NAME_LEN bytes in length
394 * - Zero terminated
395 */
396 HGCMFunctionParameter name;
397} DelProperty;
398
399/** The guest is requesting to enumerate properties */
400typedef struct _EnumProperties
401{
402 VBoxGuestHGCMCallInfo hdr;
403
404 /**
405 * Array of patterns to match the properties against, separated by '|'
406 * characters. For backwards compatibility, '\0' is also accepted
407 * as a separater.
408 * (IN pointer)
409 * If only a single, empty pattern is given then match all.
410 */
411 HGCMFunctionParameter patterns;
412 /**
413 * On success, null-separated array of strings in which the properties are
414 * returned. (OUT pointer)
415 * The number of strings in the array is always a multiple of four,
416 * and in sequences of name, value, timestamp (hexadecimal string) and the
417 * flags as a comma-separated list in the format "name=value". The list
418 * is terminated by an empty string after a "flags" entry (or at the
419 * start).
420 */
421 HGCMFunctionParameter strings;
422 /**
423 * On success, the size of the returned data. If the buffer provided is
424 * too small, the size of buffer needed. (OUT uint32_t)
425 */
426 HGCMFunctionParameter size;
427} EnumProperties;
428#pragma pack ()
429
430} /* namespace guestProp */
431
432#endif /* ___VBox_HostService_GuestPropertySvc_h defined */
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