aboutsummaryrefslogtreecommitdiff
path: root/src/include/gnunet_common.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/gnunet_common.h')
-rw-r--r--src/include/gnunet_common.h97
1 files changed, 66 insertions, 31 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h
index ca3ddceaa..9fa3de37d 100644
--- a/src/include/gnunet_common.h
+++ b/src/include/gnunet_common.h
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2006-2021 GNUnet e.V. 3 Copyright (C) 2006-2022 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -19,6 +19,10 @@
19 */ 19 */
20 20
21/** 21/**
22 * @addtogroup libgnunetutil
23 * Multi-function utilities library for GNUnet programs
24 * @{
25 *
22 * @file include/gnunet_common.h 26 * @file include/gnunet_common.h
23 * @brief commonly used definitions; globals in this file 27 * @brief commonly used definitions; globals in this file
24 * are exempt from the rule that the module name ("common") 28 * are exempt from the rule that the module name ("common")
@@ -36,28 +40,38 @@
36#ifndef GNUNET_COMMON_H 40#ifndef GNUNET_COMMON_H
37#define GNUNET_COMMON_H 41#define GNUNET_COMMON_H
38 42
43#include "gnunet_config.h"
44
45#include <stdbool.h>
39#include <stdlib.h> 46#include <stdlib.h>
40#if HAVE_SYS_SOCKET_H
41#include <sys/socket.h> 47#include <sys/socket.h>
42#endif 48#include <sys/un.h>
43#if HAVE_NETINET_IN_H
44#include <netinet/in.h> 49#include <netinet/in.h>
45#endif 50#include <arpa/inet.h>
46#ifdef HAVE_STDINT_H
47#include <stdint.h> 51#include <stdint.h>
48#endif
49#ifdef HAVE_STDARG_H
50#include <stdarg.h> 52#include <stdarg.h>
51#endif
52#ifdef HAVE_SYS_TYPES_H
53#include <sys/types.h> 53#include <sys/types.h>
54#endif
55#ifdef HAVE_SYS_SOCKET_H
56#include <sys/socket.h>
57#endif
58 54
55#if defined(__FreeBSD__)
59 56
60#ifdef HAVE_BYTESWAP_H 57#include <sys/endian.h>
58#define bswap_32(x) bswap32(x)
59#define bswap_64(x) bswap64(x)
60
61#elif defined(__OpenBSD__)
62
63#define bswap_32(x) swap32(x)
64#define bswap_64(x) swap64(x)
65
66#elif defined(__NetBSD__)
67
68#include <machine/bswap.h>
69#if defined(__BSWAP_RENAME) && !defined(__bswap_32)
70#define bswap_32(x) bswap32(x)
71#define bswap_64(x) bswap64(x)
72#endif
73
74#elif defined(__linux__) || defined(GNU)
61#include <byteswap.h> 75#include <byteswap.h>
62#endif 76#endif
63 77
@@ -132,12 +146,6 @@ enum GNUNET_GenericReturnValue
132#endif 146#endif
133 147
134/** 148/**
135 * wrap va_arg for enums
136 */
137#define GNUNET_VA_ARG_ENUM(va, X) ((enum X) va_arg (va, int))
138
139
140/**
141 * @ingroup logging 149 * @ingroup logging
142 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source 150 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
143 * tree where gnunet_config.h is unavailable 151 * tree where gnunet_config.h is unavailable
@@ -150,11 +158,10 @@ enum GNUNET_GenericReturnValue
150 * Endian operations 158 * Endian operations
151 */ 159 */
152 160
153#if __BYTE_ORDER == __LITTLE_ENDIAN 161#if defined(bswap_16) || defined(bswap_32) || defined(bswap_64)
154#if defined(__linux__) 162#define BYTE_SWAP_16(x) bswap_16 (x)
155#define BYTE_SWAP_16(x) __bswap_16 (x) 163#define BYTE_SWAP_32(x) bswap_32 (x)
156#define BYTE_SWAP_32(x) __bswap_32 (x) 164#define BYTE_SWAP_64(x) bswap_64 (x)
157#define BYTE_SWAP_64(x) __bswap_64 (x)
158#else 165#else
159#define BYTE_SWAP_16(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8)) 166#define BYTE_SWAP_16(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
160 167
@@ -170,6 +177,7 @@ enum GNUNET_GenericReturnValue
170 56)) 177 56))
171#endif 178#endif
172 179
180#if __BYTE_ORDER == __LITTLE_ENDIAN
173#define GNUNET_htobe16(x) BYTE_SWAP_16 (x) 181#define GNUNET_htobe16(x) BYTE_SWAP_16 (x)
174#define GNUNET_htole16(x) (x) 182#define GNUNET_htole16(x) (x)
175#define GNUNET_be16toh(x) BYTE_SWAP_16 (x) 183#define GNUNET_be16toh(x) BYTE_SWAP_16 (x)
@@ -623,6 +631,31 @@ GNUNET_abort_ (void) GNUNET_NORETURN;
623 631
624 632
625/** 633/**
634 * Convert a buffer to an 8-character string
635 * representative of the contents. This is used
636 * for logging binary data when debugging.
637 *
638 * @param buf buffer to log
639 * @param buf_size number of bytes in @a buf
640 * @return text representation of buf, valid until next
641 * call to this function
642 */
643const char *
644GNUNET_b2s (const void *buf,
645 size_t buf_size);
646
647
648/**
649 * Convert a fixed-sized object to a string using
650 * #GNUNET_b2s().
651 *
652 * @param obj address of object to convert
653 * @return string representing the binary obj buffer
654 */
655#define GNUNET_B2S(obj) GNUNET_b2s ((obj), sizeof (*(obj)))
656
657
658/**
626 * @ingroup logging 659 * @ingroup logging
627 * Ignore the next @a n calls to the log function. 660 * Ignore the next @a n calls to the log function.
628 * 661 *
@@ -642,7 +675,7 @@ GNUNET_log_skip (int n, int check_reset);
642 * @param logfile change logging to logfile (use NULL to keep stderr) 675 * @param logfile change logging to logfile (use NULL to keep stderr)
643 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened 676 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
644 */ 677 */
645int 678enum GNUNET_GenericReturnValue
646GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile); 679GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile);
647 680
648 681
@@ -955,7 +988,7 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
955 } while (0) 988 } while (0)
956 989
957 990
958#if HAVE_STATIC_ASSERT 991#ifdef _Static_assert
959/** 992/**
960 * Assertion to be checked (if supported by C compiler) at 993 * Assertion to be checked (if supported by C compiler) at
961 * compile time, otherwise checked at runtime and resulting 994 * compile time, otherwise checked at runtime and resulting
@@ -1240,7 +1273,7 @@ GNUNET_is_zero_ (const void *a,
1240 * @return GNUNET_YES if a is zero, GNUNET_NO otherwise 1273 * @return GNUNET_YES if a is zero, GNUNET_NO otherwise
1241 */ 1274 */
1242#define GNUNET_is_zero(a) \ 1275#define GNUNET_is_zero(a) \
1243 GNUNET_is_zero_ (a, sizeof (*a)) 1276 GNUNET_is_zero_ ((a), sizeof (*(a)))
1244 1277
1245 1278
1246/** 1279/**
@@ -1481,7 +1514,7 @@ GNUNET_is_zero_ (const void *a,
1481 * will be updated to reflect the new address. The TYPE of 1514 * will be updated to reflect the new address. The TYPE of
1482 * arr is important since size is the number of elements and 1515 * arr is important since size is the number of elements and
1483 * not the size in bytes 1516 * not the size in bytes
1484 * @param len the number of elements in the existing vector (number 1517 * @param len2 the number of elements in the existing vector (number
1485 * of elements to copy over), will be updated with the new 1518 * of elements to copy over), will be updated with the new
1486 * array size 1519 * array size
1487 1520
@@ -1714,7 +1747,7 @@ GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1714 * Set the async scope for the current thread. 1747 * Set the async scope for the current thread.
1715 * 1748 *
1716 * @param aid the async scope identifier 1749 * @param aid the async scope identifier
1717 * @param old_scope[out] location to save the old scope 1750 * @param[out] old_scope location to save the old scope
1718 */ 1751 */
1719void 1752void
1720GNUNET_async_scope_enter (const struct GNUNET_AsyncScopeId *aid, 1753GNUNET_async_scope_enter (const struct GNUNET_AsyncScopeId *aid,
@@ -1836,3 +1869,5 @@ enum GNUNET_SCHEDULER_Priority
1836#endif 1869#endif
1837 1870
1838#endif /* GNUNET_COMMON_H */ 1871#endif /* GNUNET_COMMON_H */
1872
1873/** @} */ /* end of group addition */