VirtualBox

source: vbox/trunk/src/libs/libslirp-4.8.0/include/glib.h@ 107548

Last change on this file since 107548 was 107548, checked in by vboxsync, 6 weeks ago

Devices/Network: Fixed and added configuration for packets broadcast to NAT adapter's network. Propagated additional parameters from CFGM. Simplified port forwarding a bit and added additional release logging. bugref:10268

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/** @file
2 * libslirp: glib replacement header
3 */
4
5/*
6 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef INCLUDED_glib_h
37#define INCLUDED_glib_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#ifndef LOG_GROUP
43# define LOG_GROUP LOG_GROUP_DRV_NAT
44#endif
45#include <VBox/log.h>
46
47#include <iprt/asm.h> /* for RT_BE2H_XXX and RT_H2BE_XXX */
48#include <iprt/assert.h>
49#include <iprt/getopt.h>
50#include <iprt/mem.h>
51#include <iprt/err.h>
52#include <iprt/stdarg.h>
53#include <iprt/string.h>
54#include <iprt/rand.h>
55#include <iprt/handletable.h>
56
57#if !defined(RT_OS_WINDOWS)
58# include <signal.h>
59# define G_OS_UNIX
60#else
61# define G_OS_WIN32
62#endif
63
64#define G_LITTLE_ENDIAN 1234
65#define G_BIG_ENDIAN 4321
66#define GLIB_SIZEOF_VOID_P 8
67
68#ifndef RT_BIG_ENDIAN
69# define G_BYTE_ORDER G_LITTLE_ENDIAN
70#else
71# define G_BYTE_ORDER G_BIG_ENDIAN
72#endif
73
74/** @name Typedefs
75 * @{ */
76typedef char gchar;
77typedef bool gboolean;
78typedef int gint;
79typedef int GPid;
80typedef void *gpointer;
81typedef int GRand;
82typedef void *GSpawnChildSetupFunc;
83typedef int GSpawnFlags;
84typedef char **GStrv;
85/** @} */
86
87/** @name Constants
88 * @{ */
89#define STR_LEN_MAX 4096
90#define G_SPAWN_SEARCH_PATH 4
91#undef FALSE /* windows.h */
92#define FALSE false
93#undef TRUE /* windows.h */
94#define TRUE true
95/* #} */
96
97/** @name Data Structures
98 * @{ */
99
100typedef struct GError
101{
102 uint32_t domain;
103 int code;
104 char *message;
105} GError;
106
107typedef struct GString
108{
109 char *str;
110 size_t len;
111 size_t cbAlloc;
112} GString;
113
114/** @} */
115
116#define g_assert(x) Assert(x)
117#define g_assert_not_reached() AssertFatalFailed()
118
119DECLINLINE(void) g_critical(const char *pszFmt, ...)
120{
121 va_list args;
122 va_start(args, pszFmt);
123 RTLogRelPrintf("%N", pszFmt, &args);
124 va_end(args);
125#ifdef LOG_ENABLED
126 va_start(args, pszFmt);
127 Log(("%N", pszFmt, &args));
128 va_end(args);
129#endif
130}
131
132#define g_error(...) LogRel((__VA_ARGS__))
133#define g_warning(...) LogRelWarn((__VA_ARGS__))
134
135#define g_warn_if_fail(x) do { \
136 if (RT_LIKELY(!x)) { /* likely */ } \
137 else g_warning("WARNING\n"); \
138 } while (0)
139
140#define g_warn_if_reached() g_warning("WARNING: function %s line %s", __PRETTY_FUNCTION__, __LINE__)
141
142#define g_return_if_fail(a_Expr) do { if (RT_LIKELY((a_Expr))) {/* likely */} else return; } while (0)
143
144#define g_return_val_if_fail(a_Expr, a_rc) do { if (RT_LIKELY((a_Expr))) {/* likely */} else return (a_rc); } while (0)
145
146#define G_STATIC_ASSERT(x) AssertCompile(x)
147
148
149/** @name Memory Allocation
150 * @{ */
151
152#define g_free(x) RTMemFree(x)
153#define g_malloc(x) RTMemAlloc(x)
154#define g_new(x, y) RTMemAlloc(sizeof(x) * (y))
155#define g_new0(x, y) RTMemAllocZ(sizeof(x) * (y))
156#define g_malloc0(x) RTMemAllocZ(x)
157#define g_realloc(x, y) RTMemRealloc(x, y)
158
159DECLINLINE(GError *) g_error_new(uint32_t uDomain, int iErr, const char *pszMsg)
160{
161 size_t cbMsg = pszMsg ? strlen(pszMsg) + 1 : 0;
162 GError *pErr = (GError *)g_malloc(sizeof(*pErr) + cbMsg);
163 if (pErr)
164 {
165 pErr->domain = uDomain;
166 pErr->code = iErr;
167 pErr->message = pszMsg ? (char *)memcpy(pErr + 1, pszMsg, cbMsg) : NULL;
168 }
169 return pErr;
170}
171
172DECLINLINE(void) g_error_free(GError *pErr)
173{
174 g_free(pErr);
175}
176
177DECLINLINE(char *) g_strdup(const char *pcszStr)
178{
179 if (pcszStr != NULL)
180 return RTStrDup(pcszStr);
181 return NULL;
182}
183
184/** @} */
185
186/** @name String Functions
187 * @{ */
188#ifdef __GNUC__
189# define G_GNUC_PRINTF(x, y) __attribute__((format (printf, x, y)))
190#else
191# define G_GNUC_PRINTF(x, y)
192#endif
193
194DECLINLINE(int) g_vsnprintf(char *pszDst, size_t cbDst, const char *pszFmt, va_list va)
195{
196 ssize_t cchRet = RTStrPrintf2V(pszDst, cbDst, pszFmt, va);
197 return (int)(cchRet >= 0 ? cchRet : -cchRet);
198}
199
200DECLINLINE(int) g_snprintf(char *pszDst, size_t cbDst, const char *pszFmt, ...)
201{
202 va_list va;
203 va_start(va, pszFmt);
204 int cchRet = g_vsnprintf(pszDst, cbDst, pszFmt, va);
205 va_end(va);
206 return cchRet;
207}
208
209DECLINLINE(GString *) g_string_new(const char *pszInitial)
210{
211 GString *pGStr = (GString *)RTMemAlloc(sizeof(GString));
212 if (pGStr)
213 {
214 size_t const cchSrc = pszInitial ? strlen(pszInitial) : 0;
215 size_t const cbAlloc = RT_ALIGN_Z(cchSrc + 1, 64);
216 pGStr->str = RTStrAlloc(cbAlloc);
217 AssertReturnStmt(pGStr->str, RTMemFree(pGStr), NULL);
218
219 if (pszInitial)
220 memcpy(pGStr->str, pszInitial, cchSrc);
221 pGStr->str[cchSrc] = '\0';
222 pGStr->len = cchSrc;
223 pGStr->cbAlloc = cbAlloc;
224 }
225
226 return pGStr;
227}
228
229DECLINLINE(char *) g_string_free(GString *pGStr, bool fFreeCString)
230{
231 char *pszCString = pGStr->str;
232 pGStr->str = NULL;
233 pGStr->len = 0;
234 pGStr->cbAlloc = 0;
235 RTMemFree(pGStr);
236 if (fFreeCString)
237 {
238 RTStrFree(pszCString);
239 pszCString = NULL;
240 }
241 return pszCString;
242}
243
244DECLINLINE(GString *) g_string_append_len(GString *pGStr, const char *pszSrc, size_t cchSrc)
245{
246 size_t cchDst = pGStr->len;
247 if (cchDst + cchSrc >= pGStr->cbAlloc)
248 {
249 size_t const cbAlloc = RT_ALIGN_Z(cchDst + cchSrc + 16, 64);
250 int rc = RTStrRealloc(&pGStr->str, cbAlloc);
251 AssertRCReturn(RT_SUCCESS(rc), NULL);
252 pGStr->cbAlloc = cbAlloc;
253 }
254 memcpy(&pGStr->str[cchDst], pszSrc, cchSrc);
255 cchDst += cchSrc;
256 pGStr->str[cchDst] = '\0';
257 pGStr->len = cchDst;
258 return pGStr;
259}
260
261
262DECLINLINE(GString *) g_string_append_printf(GString *pGStr, const char *pszFmt, ...)
263{
264 va_list args;
265 va_start(args, pszFmt);
266 char *pszTmp = NULL;
267 ssize_t cchRet = RTStrAPrintfV(&pszTmp, pszFmt, args);
268 va_end(args);
269 AssertReturn(cchRet >= 0, NULL);
270
271 pGStr = g_string_append_len(pGStr, pszTmp, (size_t)cchRet);
272 RTStrFree(pszTmp);
273 return pGStr;
274}
275
276DECLINLINE(char *) g_strstr_len(const char *pszHaystack, ssize_t cchHaystack, const char *pszNeedle)
277{
278 if (cchHaystack != -1)
279 {
280 size_t const cchNeedle = strlen(pszNeedle);
281 if (cchHaystack >= (ssize_t)cchNeedle)
282 {
283 char const ch0Needle = *pszNeedle;
284 cchHaystack -= cchNeedle - 1;
285 while (cchHaystack > 0)
286 {
287 const char *pszHit = (const char *)memchr(pszHaystack, ch0Needle, cchHaystack);
288 if (!pszHit)
289 return NULL;
290 if (memcmp(pszHit, pszNeedle, cchNeedle) == 0)
291 return (char *)pszHit;
292 cchHaystack -= (ssize_t)(&pszHit[1] - pszHaystack);
293 pszHaystack = &pszHit[1];
294 }
295 }
296 return NULL;
297 }
298 return RTStrStr(pszHaystack, pszNeedle);
299}
300
301DECLINLINE(bool) g_str_has_prefix(const char *pcszStr, const char *pcszPrefix)
302{
303 return RTStrStartsWith(pcszStr, pcszPrefix);
304}
305
306DECLINLINE(void) g_strfreev(char **papszStrings)
307{
308 if (papszStrings)
309 {
310 size_t i = 0;
311 char *pszEntry;
312 while ((pszEntry = papszStrings[i]) != NULL)
313 {
314 RTStrFree(pszEntry);
315 papszStrings[i] = NULL;
316 i++;
317 }
318 RTMemFree(papszStrings);
319 }
320}
321
322DECLINLINE(size_t) g_strlcpy(char *pszDst, const char *pszSrc, size_t cchSrc)
323{
324 return RTStrCopy(pszDst, cchSrc, pszSrc);
325}
326
327DECLINLINE(unsigned) g_strv_length(char **papszStrings)
328{
329 unsigned cStrings = 0;
330 AssertPtr(papszStrings);
331 if (papszStrings)
332 while (*papszStrings++ != NULL)
333 cStrings++;
334 return cStrings;
335}
336
337/** This is only used once to report a g_vsnprintf error for which errno (our x)
338 * isn't even set. */
339#define g_strerror(x) ("whatever")
340
341/** @} */
342
343/** @name Misc. Functions
344 * @{ */
345#define GLIB_CHECK_VERSION(x, y, z) true
346
347#define GINT16_FROM_BE(x) RT_BE2H_S16(x)
348#define GINT16_TO_BE(x) RT_H2BE_S16(x)
349#define GINT32_FROM_BE(x) RT_BE2H_S32(x)
350#define GINT32_TO_BE(x) RT_H2BE_S32(x)
351
352#define GUINT16_FROM_BE(x) RT_BE2H_U16(x)
353#define GUINT16_TO_BE(x) RT_H2BE_U16(x)
354#define GUINT32_FROM_BE(x) RT_BE2H_U32(x)
355#define GUINT32_TO_BE(x) RT_H2BE_U32(x)
356
357#define MAX(x, y) RT_MAX(x, y)
358#define MIN(x, y) RT_MIN(x, y)
359#define G_N_ELEMENTS(x) RT_ELEMENTS(x)
360#define G_LIKELY(x) RT_LIKELY(x)
361#define G_UNLIKELY(x) RT_UNLIKELY(x)
362
363#define g_rand_int_range(a_hRand, a_uFirst, a_uEnd) RTRandS32Ex(a_uFirst, (a_uEnd) - 1)
364#define g_rand_new() NULL
365#define g_rand_free(a_hRand) ((void)0)
366
367DECLINLINE(bool) g_shell_parse_argv(const char *pszCmdLine, int *pcArgs, char ***ppapszArgs, GError **ppErr)
368{
369 char **papszTmpArgs = NULL;
370 int cTmpArgs = 0;
371 int rc = RTGetOptArgvFromString(&papszTmpArgs, &cTmpArgs, pszCmdLine, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL /*pszIfs*/);
372 if (RT_SUCCESS(rc))
373 {
374 /* Must create a regular string array compatible with g_strfreev. */
375 char **papszArgs = (char **)RTMemAllocZ(sizeof(char *) * (cTmpArgs + 1));
376 if (papszArgs)
377 {
378 int iArg = 0;
379 while (iArg < cTmpArgs)
380 {
381 papszArgs[iArg] = RTStrDup(papszTmpArgs[iArg]);
382 AssertBreak(papszArgs[iArg]);
383 iArg++;
384 }
385 if (iArg == cTmpArgs)
386 {
387 papszArgs[iArg] = NULL;
388
389 *ppapszArgs = papszArgs;
390 *pcArgs = iArg;
391 if (ppErr)
392 *ppErr = NULL;
393 RTGetOptArgvFree(papszTmpArgs);
394 return true;
395 }
396
397 while (iArg-- > 0)
398 RTStrFree(papszArgs[iArg]);
399 RTMemFree(papszArgs);
400 }
401 RTGetOptArgvFree(papszTmpArgs);
402 if (ppErr)
403 *ppErr = g_error_new(0, VERR_NO_STR_MEMORY, "VERR_NO_STR_MEMORY");
404 }
405 else if (ppErr)
406 *ppErr = g_error_new(0, rc, "RTGetOptArgvFromString failed");
407 return false;
408}
409
410DECLINLINE(bool) g_spawn_async_with_fds(const char *working_directory, char **argv,
411 char **envp, int flags,
412 GSpawnChildSetupFunc child_setup,
413 void * user_data, GPid *child_pid, int stdin_fd,
414 int stdout_fd, int stderr_fd, GError **error)
415{
416 /** @todo r=bird: This is best replaced directly in the src/misc.c file as
417 * there we know exactly what stdin_fd, stdout_fd and stderr_fd are
418 * can can do a better mapping to RTProcCreateEx. */
419 AssertFailed();
420 return false;
421}
422
423
424/** @} */
425
426#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