add missing files
[oweals/gnunet.git] / src / include / gnunet_common.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006-2013 GNUnet e.V.
4
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
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file include/gnunet_common.h
23  * @brief commonly used definitions; globals in this file
24  *        are exempt from the rule that the module name ("common")
25  *        must be part of the symbol name.
26  *
27  * @author Christian Grothoff
28  * @author Nils Durner
29  * @author Martin Schanzenbach
30  *
31  * @defgroup logging Logging
32  * @see [Documentation](https://gnunet.org/logging)
33  *
34  * @defgroup memory Memory management
35  */
36 #ifndef GNUNET_COMMON_H
37 #define GNUNET_COMMON_H
38
39 #if HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #if HAVE_NETINET_IN_H
43 #include <netinet/in.h>
44 #endif
45 #ifdef HAVE_STDINT_H
46 #include <stdint.h>
47 #endif
48 #ifdef HAVE_STDARG_H
49 #include <stdarg.h>
50 #endif
51
52 #ifdef HAVE_BYTESWAP_H
53 #include <byteswap.h>
54 #endif
55
56 #ifdef __cplusplus
57 extern "C" {
58 #if 0 /* keep Emacsens' auto-indent happy */
59 }
60 #endif
61 #endif
62
63 /**
64  * Version of the API (for entire gnunetutil.so library).
65  */
66 #define GNUNET_UTIL_VERSION 0x000A0103
67
68
69 /**
70  * Named constants for return values.  The following invariants hold:
71  * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
72  * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
73  * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
74  */
75 #define GNUNET_OK 1
76 #define GNUNET_SYSERR -1
77 #define GNUNET_YES 1
78 #define GNUNET_NO 0
79
80 #define GNUNET_MIN(a, b) (((a) < (b)) ? (a) : (b))
81
82 #define GNUNET_MAX(a, b) (((a) > (b)) ? (a) : (b))
83
84 /* some systems use one underscore only, and mingw uses no underscore... */
85 #ifndef __BYTE_ORDER
86 #ifdef _BYTE_ORDER
87 #define __BYTE_ORDER _BYTE_ORDER
88 #else
89 #ifdef BYTE_ORDER
90 #define __BYTE_ORDER BYTE_ORDER
91 #endif
92 #endif
93 #endif
94 #ifndef __BIG_ENDIAN
95 #ifdef _BIG_ENDIAN
96 #define __BIG_ENDIAN _BIG_ENDIAN
97 #else
98 #ifdef BIG_ENDIAN
99 #define __BIG_ENDIAN BIG_ENDIAN
100 #endif
101 #endif
102 #endif
103 #ifndef __LITTLE_ENDIAN
104 #ifdef _LITTLE_ENDIAN
105 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
106 #else
107 #ifdef LITTLE_ENDIAN
108 #define __LITTLE_ENDIAN LITTLE_ENDIAN
109 #endif
110 #endif
111 #endif
112
113 /**
114  * wrap va_arg for enums
115  */
116 #define GNUNET_VA_ARG_ENUM(va, X) ((enum X) va_arg (va, int))
117
118
119 /**
120  * @ingroup logging
121  * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
122  * tree where gnunet_config.h is unavailable
123  */
124 #ifndef GNUNET_EXTRA_LOGGING
125 #define GNUNET_EXTRA_LOGGING 0
126 #endif
127
128 /**
129  * Endian operations
130  */
131
132 #if __BYTE_ORDER == __LITTLE_ENDIAN
133 #define GNUNET_htobe16(x) __bswap_16 (x)
134 #define GNUNET_htole16(x) (x)
135 #define GNUNET_be16toh(x) __bswap_16 (x)
136 #define GNUNET_le16toh(x) (x)
137
138 #define GNUNET_htobe32(x) __bswap_32 (x)
139 #define GNUNET_htole32(x) (x)
140 #define GNUNET_be32toh(x) __bswap_32 (x)
141 #define GNUNET_le32toh(x) (x)
142
143 #define GNUNET_htobe64(x) __bswap_64 (x)
144 #define GNUNET_htole64(x) (x)
145 #define GNUNET_be64toh(x) __bswap_64 (x)
146 #define GNUNET_le64toh(x) (x)
147 #endif
148 #if __BYTE_ORDER == __BIG_ENDIAN
149 #define GNUNET_htobe16(x) (x)
150 #define GNUNET_htole16(x) __bswap_16 (x)
151 #define GNUNET_be16toh(x) (x)
152 #define GNUNET_le16toh(x) __bswap_16 (x)
153
154 #define GNUNET_htobe32(x) (x)
155 #define GNUNET_htole32(x) __bswap_32 (x)
156 #define GNUNET_be32toh(x) (x)
157 #define GNUNET_le32toh(x) __bswap_32 (x)
158
159 #define GNUNET_htobe64(x) (x)
160 #define GNUNET_htole64(x) __bswap_64 (x)
161 #define GNUNET_be64toh(x) (x)
162 #define GNUNET_le64toh(x) __bswap_64 (x)
163 #endif
164
165
166 /**
167  * Macro used to avoid using 0 for the length of a variable-size
168  * array (Non-Zero-Length).
169  *
170  * Basically, C standard says that "int[n] x;" is undefined if n=0.
171  * This was supposed to prevent issues with pointer aliasing.
172  * However, C compilers may conclude that n!=0 as n=0 would be
173  * undefined, and then optimize under the assumption n!=0, which
174  * could cause actual issues.  Hence, when initializing an array
175  * on the stack with a variable-length that might be zero, write
176  * "int[GNUNET_NZL(n)] x;" instead of "int[n] x".
177  */
178 #define GNUNET_NZL(l) GNUNET_MAX (1, l)
179
180
181 /**
182  * gcc-ism to get packed structs.
183  */
184 #define GNUNET_PACKED __attribute__ ((packed))
185
186 /**
187  * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
188  */
189 #define GNUNET_GCC_STRUCT_LAYOUT
190
191 /**
192  * gcc-ism to force alignment; we use this to align char-arrays
193  * that may then be cast to 'struct's.  See also gcc
194  * bug #33594.
195  */
196 #ifdef __BIGGEST_ALIGNMENT__
197 #define GNUNET_ALIGN __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)))
198 #else
199 #define GNUNET_ALIGN __attribute__ ((aligned (8)))
200 #endif
201
202 /**
203  * gcc-ism to document unused arguments
204  */
205 #define GNUNET_UNUSED __attribute__ ((unused))
206
207 /**
208  * gcc-ism to document functions that don't return
209  */
210 #define GNUNET_NORETURN __attribute__ ((noreturn))
211
212 /**
213  * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
214  */
215 #define GNUNET_NETWORK_STRUCT_BEGIN
216
217 /**
218  * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
219  */
220 #define GNUNET_NETWORK_STRUCT_END
221
222 /* ************************ super-general types *********************** */
223
224 GNUNET_NETWORK_STRUCT_BEGIN
225
226 /**
227  * @brief A 512-bit hashcode.  These are the default length for GNUnet, using SHA-512.
228  */
229 struct GNUNET_HashCode
230 {
231   uint32_t bits[512 / 8 / sizeof(uint32_t)];  /* = 16 */
232 };
233
234
235 /**
236  * @brief A 256-bit hashcode.  Used under special conditions, like when space
237  * is critical and security is not impacted by it.
238  */
239 struct GNUNET_ShortHashCode
240 {
241   uint32_t bits[256 / 8 / sizeof(uint32_t)];  /* = 8 */
242 };
243
244
245 /**
246  * A UUID, a 128 bit random value.
247  */
248 struct GNUNET_Uuid
249 {
250   /**
251    * 128 random bits.
252    */
253   uint32_t value[4];
254 };
255
256
257 /**
258  * Header for all communications.
259  */
260 struct GNUNET_MessageHeader
261 {
262   /**
263    * The length of the struct (in bytes, including the length field itself),
264    * in big-endian format.
265    */
266   uint16_t size GNUNET_PACKED;
267
268   /**
269    * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
270    */
271   uint16_t type GNUNET_PACKED;
272 };
273
274
275 /**
276  * Answer from service to client about last operation.
277  */
278 struct GNUNET_OperationResultMessage
279 {
280   struct GNUNET_MessageHeader header;
281
282   uint32_t reserved GNUNET_PACKED;
283
284   /**
285    * Operation ID.
286    */
287   uint64_t op_id GNUNET_PACKED;
288
289   /**
290    * Status code for the operation.
291    */
292   uint64_t result_code GNUNET_PACKED;
293
294   /* Followed by data. */
295 };
296
297
298 /**
299  * Identifier for an asynchronous execution context.
300  */
301 struct GNUNET_AsyncScopeId
302 {
303   uint32_t bits[16 / sizeof(uint32_t)];  /* = 16 bytes */
304 };
305
306 GNUNET_NETWORK_STRUCT_END
307
308
309 /**
310  * Saved async scope identifier or root scope.
311  */
312 struct GNUNET_AsyncScopeSave
313 {
314   /**
315    * Saved scope.  Unused if 'have_scope==GNUNET_NO'.
316    */
317   struct GNUNET_AsyncScopeId scope_id;
318
319   /**
320    * GNUNET_YES unless this saved scope is the unnamed root scope.
321    */
322   int have_scope;
323 };
324
325
326 /**
327  * Function called with a filename.
328  *
329  * @param cls closure
330  * @param filename complete filename (absolute path)
331  * @return #GNUNET_OK to continue to iterate,
332  *  #GNUNET_NO to stop iteration with no error,
333  *  #GNUNET_SYSERR to abort iteration with error!
334  */
335 typedef int (*GNUNET_FileNameCallback) (void *cls, const char *filename);
336
337
338 /**
339  * Generic continuation callback.
340  *
341  * @param cls  Closure.
342  */
343 typedef void (*GNUNET_ContinuationCallback) (void *cls);
344
345
346 /**
347  * Function called with the result of an asynchronous operation.
348  *
349  * @param cls
350  *        Closure.
351  * @param result_code
352  *        Result code for the operation.
353  * @param data
354  *        Data result for the operation.
355  * @param data_size
356  *        Size of @a data.
357  */
358 typedef void (*GNUNET_ResultCallback) (void *cls,
359                                        int64_t result_code,
360                                        const void *data,
361                                        uint16_t data_size);
362
363
364 /* ****************************** logging ***************************** */
365
366 /**
367  * @ingroup logging
368  * Types of errors.
369  */
370 enum GNUNET_ErrorType
371 {
372   GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
373   GNUNET_ERROR_TYPE_NONE = 0,
374   GNUNET_ERROR_TYPE_ERROR = 1,
375   GNUNET_ERROR_TYPE_WARNING = 2,
376   /* UX: We need a message type that is output by
377    * default without looking like there is a problem.
378    */
379   GNUNET_ERROR_TYPE_MESSAGE = 4,
380   GNUNET_ERROR_TYPE_INFO = 8,
381   GNUNET_ERROR_TYPE_DEBUG = 16,
382   GNUNET_ERROR_TYPE_INVALID = 32,
383   GNUNET_ERROR_TYPE_BULK = 64
384 };
385
386
387 /**
388  * @ingroup logging
389  * User-defined handler for log messages.
390  *
391  * @param cls closure
392  * @param kind severeity
393  * @param component what component is issuing the message?
394  * @param date when was the message logged?
395  * @param message what is the message
396  */
397 typedef void (*GNUNET_Logger) (void *cls,
398                                enum GNUNET_ErrorType kind,
399                                const char *component,
400                                const char *date,
401                                const char *message);
402
403
404 /**
405  * @ingroup logging
406  * Get the number of log calls that are going to be skipped
407  *
408  * @return number of log calls to be ignored
409  */
410 int
411 GNUNET_get_log_skip (void);
412
413
414 #if ! defined(GNUNET_CULL_LOGGING)
415 int
416 GNUNET_get_log_call_status (int caller_level,
417                             const char *comp,
418                             const char *file,
419                             const char *function,
420                             int line);
421 #endif
422
423
424 /**
425  * @ingroup logging
426  * Main log function.
427  *
428  * @param kind how serious is the error?
429  * @param message what is the message (format string)
430  * @param ... arguments for format string
431  */
432 void
433 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
434 __attribute__ ((format (printf, 2, 3)));
435
436 /* from glib */
437 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
438 #define _GNUNET_BOOLEAN_EXPR(expr) \
439   __extension__ ({                  \
440     int _gnunet_boolean_var_;      \
441     if (expr)                      \
442       _gnunet_boolean_var_ = 1;    \
443     else                           \
444       _gnunet_boolean_var_ = 0;    \
445     _gnunet_boolean_var_;          \
446   })
447 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR (expr), 1))
448 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR (expr), 0))
449 #else
450 #define GN_LIKELY(expr) (expr)
451 #define GN_UNLIKELY(expr) (expr)
452 #endif
453
454 #if ! defined(GNUNET_LOG_CALL_STATUS)
455 #define GNUNET_LOG_CALL_STATUS -1
456 #endif
457
458
459 /**
460  * @ingroup logging
461  * Log function that specifies an alternative component.
462  * This function should be used by plugins.
463  *
464  * @param kind how serious is the error?
465  * @param comp component responsible for generating the message
466  * @param message what is the message (format string)
467  * @param ... arguments for format string
468  */
469 void
470 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind,
471                          const char *comp,
472                          const char *message,
473                          ...);
474
475 #if ! defined(GNUNET_CULL_LOGGING)
476 #define GNUNET_log_from(kind, comp, ...)                                  \
477   do                                                                      \
478   {                                                                       \
479     static int log_call_enabled = GNUNET_LOG_CALL_STATUS;                 \
480     if ((GNUNET_EXTRA_LOGGING > 0) ||                                     \
481         ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0))                        \
482     {                                                                     \
483       if (GN_UNLIKELY (log_call_enabled == -1))                           \
484         log_call_enabled =                                                \
485           GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), \
486                                       (comp),                             \
487                                       __FILE__,                           \
488                                       __FUNCTION__,                       \
489                                       __LINE__);                          \
490       if (GN_UNLIKELY (GNUNET_get_log_skip () > 0))                       \
491       {                                                                   \
492         GNUNET_log_skip (-1, GNUNET_NO);                                  \
493       }                                                                   \
494       else                                                                \
495       {                                                                   \
496         if (GN_UNLIKELY (log_call_enabled))                               \
497           GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__);            \
498       }                                                                   \
499     }                                                                     \
500   } while (0)
501
502 #define GNUNET_log(kind, ...)                                             \
503   do                                                                      \
504   {                                                                       \
505     static int log_call_enabled = GNUNET_LOG_CALL_STATUS;                 \
506     if ((GNUNET_EXTRA_LOGGING > 0) ||                                     \
507         ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0))                        \
508     {                                                                     \
509       if (GN_UNLIKELY (log_call_enabled == -1))                           \
510         log_call_enabled =                                                \
511           GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), \
512                                       NULL,                               \
513                                       __FILE__,                           \
514                                       __FUNCTION__,                       \
515                                       __LINE__);                          \
516       if (GN_UNLIKELY (GNUNET_get_log_skip () > 0))                       \
517       {                                                                   \
518         GNUNET_log_skip (-1, GNUNET_NO);                                  \
519       }                                                                   \
520       else                                                                \
521       {                                                                   \
522         if (GN_UNLIKELY (log_call_enabled))                               \
523           GNUNET_log_nocheck ((kind), __VA_ARGS__);                       \
524       }                                                                   \
525     }                                                                     \
526   } while (0)
527 #else
528 #define GNUNET_log(...)
529 #define GNUNET_log_from(...)
530 #endif
531
532
533 /**
534  * @ingroup logging
535  * Log error message about missing configuration option.
536  *
537  * @param kind log level
538  * @param section section with missing option
539  * @param option name of missing option
540  */
541 void
542 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
543                            const char *section,
544                            const char *option);
545
546
547 /**
548  * @ingroup logging
549  * Log error message about invalid configuration option value.
550  *
551  * @param kind log level
552  * @param section section with invalid option
553  * @param option name of invalid option
554  * @param required what is required that is invalid about the option
555  */
556 void
557 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
558                            const char *section,
559                            const char *option,
560                            const char *required);
561
562
563 /**
564  * @ingroup logging
565  * Abort the process, generate a core dump if possible.
566  * Most code should use `GNUNET_assert (0)` instead to
567  * first log the location of the failure.
568  */
569 void
570 GNUNET_abort_ (void) GNUNET_NORETURN;
571
572
573 /**
574  * @ingroup logging
575  * Ignore the next @a n calls to the log function.
576  *
577  * @param n number of log calls to ignore (could be negative)
578  * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
579  */
580 void
581 GNUNET_log_skip (int n, int check_reset);
582
583
584 /**
585  * @ingroup logging
586  * Setup logging.
587  *
588  * @param comp default component to use
589  * @param loglevel what types of messages should be logged
590  * @param logfile change logging to logfile (use NULL to keep stderr)
591  * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
592  */
593 int
594 GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile);
595
596
597 /**
598  * @ingroup logging
599  * Add a custom logger.  Note that installing any custom logger
600  * will disable the standard logger.  When multiple custom loggers
601  * are installed, all will be called.  The standard logger will
602  * only be used if no custom loggers are present.
603  *
604  * @param logger log function
605  * @param logger_cls closure for @a logger
606  */
607 void
608 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls);
609
610
611 /**
612  * @ingroup logging
613  * Remove a custom logger.
614  *
615  * @param logger log function
616  * @param logger_cls closure for @a logger
617  */
618 void
619 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls);
620
621
622 /**
623  * @ingroup logging
624  * Convert a short hash value to a string (for printing debug messages).
625  * This is one of the very few calls in the entire API that is
626  * NOT reentrant!
627  *
628  * @param shc the hash code
629  * @return string
630  */
631 const char *
632 GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc);
633
634
635 /**
636  * @ingroup logging
637  * Convert a UUID to a string (for printing debug messages).
638  * This is one of the very few calls in the entire API that is
639  * NOT reentrant!
640  *
641  * @param uuid the UUID
642  * @return string
643  */
644 const char *
645 GNUNET_uuid2s (const struct GNUNET_Uuid *uuid);
646
647
648 /**
649  * @ingroup logging
650  * Convert a hash value to a string (for printing debug messages).
651  * This is one of the very few calls in the entire API that is
652  * NOT reentrant!
653  *
654  * @param hc the hash code
655  * @return string
656  */
657 const char *
658 GNUNET_h2s (const struct GNUNET_HashCode *hc);
659
660
661 /**
662  * @ingroup logging
663  * Convert a hash value to a string (for printing debug messages).
664  * This is one of the very few calls in the entire API that is
665  * NOT reentrant! Identical to #GNUNET_h2s(), except that another
666  * buffer is used so both #GNUNET_h2s() and #GNUNET_h2s2() can be
667  * used within the same log statement.
668  *
669  * @param hc the hash code
670  * @return string
671  */
672 const char *
673 GNUNET_h2s2 (const struct GNUNET_HashCode *hc);
674
675
676 /**
677  * @ingroup logging
678  * Convert a hash value to a string (for printing debug messages).
679  * This prints all 104 characters of a hashcode!
680  * This is one of the very few calls in the entire API that is
681  * NOT reentrant!
682  *
683  * @param hc the hash code
684  * @return string
685  */
686 const char *
687 GNUNET_h2s_full (const struct GNUNET_HashCode *hc);
688
689
690 /**
691  * Public key. Details in gnunet_util_crypto.h.
692  */
693 struct GNUNET_CRYPTO_EddsaPublicKey;
694
695
696 /**
697  * Public key. Details in gnunet_util_crypto.h.
698  */
699 struct GNUNET_CRYPTO_EcdhePublicKey;
700
701
702 /**
703  * @ingroup logging
704  * Convert a public key value to a string (for printing debug messages).
705  * This is one of the very few calls in the entire API that is
706  * NOT reentrant!
707  *
708  * @param hc the hash code
709  * @return string
710  */
711 const char *
712 GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
713
714
715 /**
716  * @ingroup logging
717  * Convert a public key value to a string (for printing debug messages).
718  * This is one of the very few calls in the entire API that is
719  * NOT reentrant!
720  *
721  * @param hc the hash code
722  * @return string
723  */
724 const char *
725 GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
726
727
728 /**
729  * @ingroup logging
730  * Convert a public key value to a string (for printing debug messages).
731  * This is one of the very few calls in the entire API that is
732  * NOT reentrant!
733  *
734  * @param hc the hash code
735  * @return string
736  */
737 const char *
738 GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
739
740
741 /**
742  * @ingroup logging
743  * Convert a public key value to a string (for printing debug messages).
744  * This is one of the very few calls in the entire API that is
745  * NOT reentrant!
746  *
747  * @param hc the hash code
748  * @return string
749  */
750 const char *
751 GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
752
753
754 /**
755  * @ingroup logging
756  * Convert a peer identity to a string (for printing debug messages).
757  * This is one of the very few calls in the entire API that is
758  * NOT reentrant!
759  *
760  * @param pid the peer identity
761  * @return string form of the pid; will be overwritten by next
762  *         call to #GNUNET_i2s().
763  */
764 const char *
765 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
766
767
768 /**
769  * @ingroup logging
770  * Convert a peer identity to a string (for printing debug messages).
771  * This is one of the very few calls in the entire API that is
772  * NOT reentrant!  Identical to #GNUNET_i2s(), except that another
773  * buffer is used so both #GNUNET_i2s() and #GNUNET_i2s2() can be
774  * used within the same log statement.
775  *
776  * @param pid the peer identity
777  * @return string form of the pid; will be overwritten by next
778  *         call to #GNUNET_i2s().
779  */
780 const char *
781 GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid);
782
783
784 /**
785  * @ingroup logging
786  * Convert a peer identity to a string (for printing debug messages).
787  * This is one of the very few calls in the entire API that is
788  * NOT reentrant!
789  *
790  * @param pid the peer identity
791  * @return string form of the pid; will be overwritten by next
792  *         call to #GNUNET_i2s_full().
793  */
794 const char *
795 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
796
797
798 /**
799  * @ingroup logging
800  * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
801  * (for printing debug messages).  This is one of the very few calls
802  * in the entire API that is NOT reentrant!
803  *
804  * @param addr the address
805  * @param addrlen the length of the @a addr
806  * @return nicely formatted string for the address
807  *  will be overwritten by next call to #GNUNET_a2s().
808  */
809 const char *
810 GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen);
811
812
813 /**
814  * @ingroup logging
815  * Convert error type to string.
816  *
817  * @param kind type to convert
818  * @return string corresponding to the type
819  */
820 const char *
821 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
822
823
824 /**
825  * @ingroup logging
826  * Use this for fatal errors that cannot be handled
827  */
828 #define GNUNET_assert(cond)                                     \
829   do                                                            \
830   {                                                             \
831     if (! (cond))                                               \
832     {                                                           \
833       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,                      \
834                   _ ("Assertion failed at %s:%d. Aborting.\n"), \
835                   __FILE__,                                     \
836                   __LINE__);                                    \
837       GNUNET_abort_ ();                                         \
838     }                                                           \
839   } while (0)
840
841
842 /**
843  * @ingroup logging
844  * Use this for fatal errors that cannot be handled
845  */
846 #define GNUNET_assert_at(cond, f, l)                            \
847   do                                                            \
848   {                                                             \
849     if (! (cond))                                               \
850     {                                                           \
851       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,                      \
852                   _ ("Assertion failed at %s:%d. Aborting.\n"), \
853                   f,                                            \
854                   l);                                           \
855       GNUNET_abort_ ();                                         \
856     }                                                           \
857   } while (0)
858
859
860 /**
861  * @ingroup logging
862  * Use this for fatal errors that cannot be handled
863  *
864  * @param cond Condition to evaluate
865  * @param comp Component string to use for logging
866  */
867 #define GNUNET_assert_from(cond, comp)                               \
868   do                                                                 \
869   {                                                                  \
870     if (! (cond))                                                    \
871     {                                                                \
872       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,                      \
873                        comp,                                         \
874                        _ ("Assertion failed at %s:%d. Aborting.\n"), \
875                        __FILE__,                                     \
876                        __LINE__);                                    \
877       GNUNET_abort_ ();                                              \
878     }                                                                \
879   } while (0)
880
881
882 /**
883  * @ingroup logging
884  * Use this for internal assertion violations that are
885  * not fatal (can be handled) but should not occur.
886  */
887 #define GNUNET_break(cond)                            \
888   do                                                  \
889   {                                                   \
890     if (! (cond))                                     \
891     {                                                 \
892       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,            \
893                   _ ("Assertion failed at %s:%d.\n"), \
894                   __FILE__,                           \
895                   __LINE__);                          \
896     }                                                 \
897   } while (0)
898
899
900 /**
901  * @ingroup logging
902  * Use this for assertion violations caused by other
903  * peers (i.e. protocol violations).  We do not want to
904  * confuse end-users (say, some other peer runs an
905  * older, broken or incompatible GNUnet version), but
906  * we still want to see these problems during
907  * development and testing.  "OP == other peer".
908  */
909 #define GNUNET_break_op(cond)                                             \
910   do                                                                      \
911   {                                                                       \
912     if (! (cond))                                                         \
913     {                                                                     \
914       GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,     \
915                   _ ("External protocol violation detected at %s:%d.\n"), \
916                   __FILE__,                                               \
917                   __LINE__);                                              \
918     }                                                                     \
919   } while (0)
920
921
922 /**
923  * @ingroup logging
924  * Log an error message at log-level 'level' that indicates
925  * a failure of the command 'cmd' with the message given
926  * by strerror(errno).
927  */
928 #define GNUNET_log_strerror(level, cmd)                      \
929   do                                                         \
930   {                                                          \
931     GNUNET_log (level,                                       \
932                 _ ("`%s' failed at %s:%d with error: %s\n"), \
933                 cmd,                                         \
934                 __FILE__,                                    \
935                 __LINE__,                                    \
936                 strerror (errno));                           \
937   } while (0)
938
939
940 /**
941  * @ingroup logging
942  * Log an error message at log-level 'level' that indicates
943  * a failure of the command 'cmd' with the message given
944  * by strerror(errno).
945  */
946 #define GNUNET_log_from_strerror(level, component, cmd)           \
947   do                                                              \
948   {                                                               \
949     GNUNET_log_from (level,                                       \
950                      component,                                   \
951                      _ ("`%s' failed at %s:%d with error: %s\n"), \
952                      cmd,                                         \
953                      __FILE__,                                    \
954                      __LINE__,                                    \
955                      strerror (errno));                           \
956   } while (0)
957
958
959 /**
960  * @ingroup logging
961  * Log an error message at log-level 'level' that indicates
962  * a failure of the command 'cmd' with the message given
963  * by strerror(errno).
964  */
965 #define GNUNET_log_strerror_file(level, cmd, filename)                    \
966   do                                                                      \
967   {                                                                       \
968     GNUNET_log (level,                                                    \
969                 _ ("`%s' failed on file `%s' at %s:%d with error: %s\n"), \
970                 cmd,                                                      \
971                 filename,                                                 \
972                 __FILE__,                                                 \
973                 __LINE__,                                                 \
974                 strerror (errno));                                        \
975   } while (0)
976
977
978 /**
979  * @ingroup logging
980  * Log an error message at log-level 'level' that indicates
981  * a failure of the command 'cmd' with the message given
982  * by strerror(errno).
983  */
984 #define GNUNET_log_from_strerror_file(level, component, cmd, filename)         \
985   do                                                                           \
986   {                                                                            \
987     GNUNET_log_from (level,                                                    \
988                      component,                                                \
989                      _ ("`%s' failed on file `%s' at %s:%d with error: %s\n"), \
990                      cmd,                                                      \
991                      filename,                                                 \
992                      __FILE__,                                                 \
993                      __LINE__,                                                 \
994                      strerror (errno));                                        \
995   } while (0)
996
997 /* ************************* endianess conversion ****************** */
998
999 /**
1000  * Convert unsigned 64-bit integer to network byte order.
1001  *
1002  * @param n
1003  *        The value in host byte order.
1004  *
1005  * @return The same value in network byte order.
1006  */
1007 uint64_t
1008 GNUNET_htonll (uint64_t n);
1009
1010
1011 /**
1012  * Convert unsigned 64-bit integer to host byte order.
1013  *
1014  * @param n
1015  *        The value in network byte order.
1016  *
1017  * @return The same value in host byte order.
1018  */
1019 uint64_t
1020 GNUNET_ntohll (uint64_t n);
1021
1022
1023 /**
1024  * Convert double to network byte order.
1025  *
1026  * @param d
1027  *        The value in host byte order.
1028  *
1029  * @return The same value in network byte order.
1030  */
1031 double
1032 GNUNET_hton_double (double d);
1033
1034
1035 /**
1036  * Convert double to host byte order
1037  *
1038  * @param d
1039  *        The value in network byte order.
1040  *
1041  * @return The same value in host byte order.
1042  */
1043 double
1044 GNUNET_ntoh_double (double d);
1045
1046
1047 /* ************************* allocation functions ****************** */
1048
1049 /**
1050  * @ingroup memory
1051  * Maximum allocation with GNUNET_malloc macro.
1052  */
1053 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
1054
1055 /**
1056  * @ingroup memory
1057  * Allocate a struct or union of the given @a type.
1058  * Wrapper around #GNUNET_malloc that returns a pointer
1059  * to the newly created object of the correct type.
1060  *
1061  * @param type name of the struct or union, i.e. pass 'struct Foo'.
1062  */
1063 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof(type))
1064
1065
1066 /**
1067  * Compare memory in @a a and @a b, where both must be of
1068  * the same pointer type.
1069  */
1070 #define GNUNET_memcmp(a, b)       \
1071   ({                              \
1072     const typeof (*b) * _a = (a);  \
1073     const typeof (*a) * _b = (b);  \
1074     memcmp (_a, _b, sizeof(*a)); \
1075   })
1076
1077
1078 /**
1079  * Check that memory in @a a is all zeros. @a a must be a pointer.
1080  *
1081  * @param a pointer to a struct which should be tested for the
1082  *          entire memory being zero'ed out.
1083  */
1084 #define GNUNET_is_zero(a)           \
1085   ({                                \
1086     static const typeof (*a) _z;    \
1087     memcmp ((a), &_z, sizeof(_z)); \
1088   })
1089
1090
1091 /**
1092  * Call memcpy() but check for @a n being 0 first. In the latter
1093  * case, it is now safe to pass NULL for @a src or @a dst.
1094  * Unlike traditional memcpy(), returns nothing.
1095  *
1096  * @param dst destination of the copy, may be NULL if @a n is zero
1097  * @param src source of the copy, may be NULL if @a n is zero
1098  * @param n number of bytes to copy
1099  */
1100 #define GNUNET_memcpy(dst, src, n) \
1101   do                               \
1102   {                                \
1103     if (0 != n)                    \
1104     {                              \
1105       (void) memcpy (dst, src, n); \
1106     }                              \
1107   } while (0)
1108
1109
1110 /**
1111  * @ingroup memory
1112  * Allocate a size @a n array with structs or unions of the given @a type.
1113  * Wrapper around #GNUNET_malloc that returns a pointer
1114  * to the newly created objects of the correct type.
1115  *
1116  * @param n number of elements in the array
1117  * @param type name of the struct or union, i.e. pass 'struct Foo'.
1118  */
1119 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof(type))
1120
1121 /**
1122  * @ingroup memory
1123  * Allocate a size @a n times @a m array
1124  * with structs or unions of the given @a type.
1125  *
1126  * @param n size of the first dimension
1127  * @param m size of the second dimension
1128  * @param type name of the struct or union, i.e. pass 'struct Foo'.
1129  */
1130 #define GNUNET_new_array_2d(n, m, type) \
1131   (type **) GNUNET_xnew_array_2d_ (n, m, sizeof(type), __FILE__, __LINE__)
1132
1133 /**
1134  * @ingroup memory
1135  * Allocate a size @a n times @a m times @a o array
1136  * with structs or unions of the given @a type.
1137  *
1138  * @param n size of the first dimension
1139  * @param m size of the second dimension
1140  * @param o size of the third dimension
1141  * @param type name of the struct or union, i.e. pass 'struct Foo'.
1142  */
1143 #define GNUNET_new_array_3d(n, m, o, type) \
1144   (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof(type), __FILE__, __LINE__)
1145
1146 /**
1147  * @ingroup memory
1148  * Wrapper around malloc. Allocates size bytes of memory.
1149  * The memory will be zero'ed out.
1150  *
1151  * @param size the number of bytes to allocate, must be
1152  *        smaller than 40 MB.
1153  * @return pointer to size bytes of memory, never NULL (!)
1154  */
1155 #define GNUNET_malloc(size) GNUNET_xmalloc_ (size, __FILE__, __LINE__)
1156
1157 /**
1158  * @ingroup memory
1159  * Allocate and initialize a block of memory.
1160  *
1161  * @param buf data to initalize the block with
1162  * @param size the number of bytes in buf (and size of the allocation)
1163  * @return pointer to size bytes of memory, never NULL (!)
1164  */
1165 #define GNUNET_memdup(buf, size) GNUNET_xmemdup_ (buf, size, __FILE__, __LINE__)
1166
1167 /**
1168  * @ingroup memory
1169  * Wrapper around malloc. Allocates size bytes of memory.
1170  * The memory will be zero'ed out.
1171  *
1172  * @param size the number of bytes to allocate
1173  * @return pointer to size bytes of memory, NULL if we do not have enough memory
1174  */
1175 #define GNUNET_malloc_large(size) \
1176   GNUNET_xmalloc_unchecked_ (size, __FILE__, __LINE__)
1177
1178 /**
1179  * @ingroup memory
1180  * Wrapper around realloc. Reallocates size bytes of memory.
1181  * The content of the intersection of the new and old size will be unchanged.
1182  *
1183  * @param ptr the pointer to reallocate
1184  * @param size the number of bytes to reallocate
1185  * @return pointer to size bytes of memory
1186  */
1187 #define GNUNET_realloc(ptr, size) \
1188   GNUNET_xrealloc_ (ptr, size, __FILE__, __LINE__)
1189
1190 /**
1191  * @ingroup memory
1192  * Wrapper around free. Frees the memory referred to by ptr.
1193  * Note that it is generally better to free memory that was
1194  * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
1195  *
1196  * @param ptr location where to free the memory. ptr must have
1197  *     been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
1198  */
1199 #define GNUNET_free(ptr) GNUNET_xfree_ (ptr, __FILE__, __LINE__)
1200
1201 /**
1202  * @ingroup memory
1203  * Free the memory pointed to by ptr if ptr is not NULL.
1204  * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
1205  *
1206  * @param ptr the location in memory to free
1207  */
1208 #define GNUNET_free_non_null(ptr) \
1209   do                              \
1210   {                               \
1211     void *__x__ = ptr;            \
1212     if (__x__ != NULL)            \
1213     {                             \
1214       GNUNET_free (__x__);        \
1215     }                             \
1216   } while (0)
1217
1218 /**
1219  * @ingroup memory
1220  * Wrapper around #GNUNET_xstrdup_.  Makes a copy of the zero-terminated string
1221  * pointed to by a.
1222  *
1223  * @param a pointer to a zero-terminated string
1224  * @return a copy of the string including zero-termination
1225  */
1226 #define GNUNET_strdup(a) GNUNET_xstrdup_ (a, __FILE__, __LINE__)
1227
1228 /**
1229  * @ingroup memory
1230  * Wrapper around #GNUNET_xstrndup_.  Makes a partial copy of the string
1231  * pointed to by a.
1232  *
1233  * @param a pointer to a string
1234  * @param length of the string to duplicate
1235  * @return a partial copy of the string including zero-termination
1236  */
1237 #define GNUNET_strndup(a, length) \
1238   GNUNET_xstrndup_ (a, length, __FILE__, __LINE__)
1239
1240 /**
1241  * @ingroup memory
1242  * Grow a well-typed (!) array.  This is a convenience
1243  * method to grow a vector @a arr of size @a size
1244  * to the new (target) size @a tsize.
1245  * <p>
1246  *
1247  * Example (simple, well-typed stack):
1248  *
1249  * <pre>
1250  * static struct foo * myVector = NULL;
1251  * static int myVecLen = 0;
1252  *
1253  * static void push(struct foo * elem) {
1254  *   GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
1255  *   GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
1256  * }
1257  *
1258  * static void pop(struct foo * elem) {
1259  *   if (myVecLen == 0) die();
1260  *   GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
1261  *   GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
1262  * }
1263  * </pre>
1264  *
1265  * @param arr base-pointer of the vector, may be NULL if size is 0;
1266  *        will be updated to reflect the new address. The TYPE of
1267  *        arr is important since size is the number of elements and
1268  *        not the size in bytes
1269  * @param size the number of elements in the existing vector (number
1270  *        of elements to copy over), will be updated with the new
1271  *        array size
1272  * @param tsize the target size for the resulting vector, use 0 to
1273  *        free the vector (then, arr will be NULL afterwards).
1274  */
1275 #define GNUNET_array_grow(arr, size, tsize) \
1276   GNUNET_xgrow_ ((void **) &(arr),          \
1277                  sizeof((arr)[0]),         \
1278                  &size,                     \
1279                  tsize,                     \
1280                  __FILE__,                  \
1281                  __LINE__)
1282
1283 /**
1284  * @ingroup memory
1285  * Append an element to a list (growing the list by one).
1286  *
1287  * @param arr base-pointer of the vector, may be NULL if size is 0;
1288  *        will be updated to reflect the new address. The TYPE of
1289  *        arr is important since size is the number of elements and
1290  *        not the size in bytes
1291  * @param size the number of elements in the existing vector (number
1292  *        of elements to copy over), will be updated with the new
1293  *        array size
1294  * @param element the element that will be appended to the array
1295  */
1296 #define GNUNET_array_append(arr, size, element) \
1297   do                                            \
1298   {                                             \
1299     GNUNET_array_grow (arr, size, size + 1);    \
1300     (arr) [size - 1] = element;                  \
1301   } while (0)
1302
1303 /**
1304  * @ingroup memory
1305  * Like snprintf, just aborts if the buffer is of insufficient size.
1306  *
1307  * @param buf pointer to buffer that is written to
1308  * @param size number of bytes in @a buf
1309  * @param format format strings
1310  * @param ... data for format string
1311  * @return number of bytes written to buf or negative value on error
1312  */
1313 int
1314 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
1315
1316
1317 /**
1318  * @ingroup memory
1319  * Like asprintf, just portable.
1320  *
1321  * @param buf set to a buffer of sufficient size (allocated, caller must free)
1322  * @param format format string (see printf, fprintf, etc.)
1323  * @param ... data for format string
1324  * @return number of bytes in "*buf" excluding 0-termination
1325  */
1326 int
1327 GNUNET_asprintf (char **buf, const char *format, ...);
1328
1329
1330 /* ************** internal implementations, use macros above! ************** */
1331
1332 /**
1333  * Allocate memory. Checks the return value, aborts if no more
1334  * memory is available.  Don't use GNUNET_xmalloc_ directly. Use the
1335  * #GNUNET_malloc macro.
1336  * The memory will be zero'ed out.
1337  *
1338  * @param size number of bytes to allocate
1339  * @param filename where is this call being made (for debugging)
1340  * @param linenumber line where this call is being made (for debugging)
1341  * @return allocated memory, never NULL
1342  */
1343 void *
1344 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
1345
1346
1347 /**
1348  * Allocate memory for a two dimensional array in one block
1349  * and set up pointers. Aborts if no more memory is available.
1350  * Don't use GNUNET_xnew_array_2d_ directly. Use the
1351  * #GNUNET_new_array_2d macro.
1352  * The memory of the elements will be zero'ed out.
1353  *
1354  * @param n size of the first dimension
1355  * @param m size of the second dimension
1356  * @param elementSize size of a single element in bytes
1357  * @param filename where is this call being made (for debugging)
1358  * @param linenumber line where this call is being made (for debugging)
1359  * @return allocated memory, never NULL
1360  */
1361 void **
1362 GNUNET_xnew_array_2d_ (size_t n,
1363                        size_t m,
1364                        size_t elementSize,
1365                        const char *filename,
1366                        int linenumber);
1367
1368
1369 /**
1370  * Allocate memory for a three dimensional array in one block
1371  * and set up pointers. Aborts if no more memory is available.
1372  * Don't use GNUNET_xnew_array_3d_ directly. Use the
1373  * #GNUNET_new_array_3d macro.
1374  * The memory of the elements will be zero'ed out.
1375  *
1376  * @param n size of the first dimension
1377  * @param m size of the second dimension
1378  * @param o size of the third dimension
1379  * @param elementSize size of a single element in bytes
1380  * @param filename where is this call being made (for debugging)
1381  * @param linenumber line where this call is being made (for debugging)
1382  * @return allocated memory, never NULL
1383  */
1384 void ***
1385 GNUNET_xnew_array_3d_ (size_t n,
1386                        size_t m,
1387                        size_t o,
1388                        size_t elementSize,
1389                        const char *filename,
1390                        int linenumber);
1391
1392
1393 /**
1394  * Allocate and initialize memory. Checks the return value, aborts if no more
1395  * memory is available.  Don't use GNUNET_xmemdup_ directly. Use the
1396  * #GNUNET_memdup macro.
1397  *
1398  * @param buf buffer to initialize from (must contain size bytes)
1399  * @param size number of bytes to allocate
1400  * @param filename where is this call being made (for debugging)
1401  * @param linenumber line where this call is being made (for debugging)
1402  * @return allocated memory, never NULL
1403  */
1404 void *
1405 GNUNET_xmemdup_ (const void *buf,
1406                  size_t size,
1407                  const char *filename,
1408                  int linenumber);
1409
1410
1411 /**
1412  * Allocate memory.  This function does not check if the allocation
1413  * request is within reasonable bounds, allowing allocations larger
1414  * than 40 MB.  If you don't expect the possibility of very large
1415  * allocations, use #GNUNET_malloc instead.  The memory will be zero'ed
1416  * out.
1417  *
1418  * @param size number of bytes to allocate
1419  * @param filename where is this call being made (for debugging)
1420  * @param linenumber line where this call is being made (for debugging)
1421  * @return pointer to size bytes of memory, NULL if we do not have enough memory
1422  */
1423 void *
1424 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
1425
1426
1427 /**
1428  * Reallocate memory. Checks the return value, aborts if no more
1429  * memory is available.
1430  */
1431 void *
1432 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1433
1434
1435 /**
1436  * Free memory. Merely a wrapper for the case that we
1437  * want to keep track of allocations.  Don't use GNUNET_xfree_
1438  * directly. Use the #GNUNET_free macro.
1439  *
1440  * @param ptr pointer to memory to free
1441  * @param filename where is this call being made (for debugging)
1442  * @param linenumber line where this call is being made (for debugging)
1443  */
1444 void
1445 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1446
1447
1448 /**
1449  * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1450  * @param str string to duplicate
1451  * @param filename where is this call being made (for debugging)
1452  * @param linenumber line where this call is being made (for debugging)
1453  * @return the duplicated string
1454  */
1455 char *
1456 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1457
1458 /**
1459  * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1460  *
1461  * @param str string to duplicate
1462  * @param len length of the string to duplicate
1463  * @param filename where is this call being made (for debugging)
1464  * @param linenumber line where this call is being made (for debugging)
1465  * @return the duplicated string
1466  */
1467 char *
1468 GNUNET_xstrndup_ (const char *str,
1469                   size_t len,
1470                   const char *filename,
1471                   int linenumber);
1472
1473 /**
1474  * Grow an array, the new elements are zeroed out.
1475  * Grows old by (*oldCount-newCount)*elementSize
1476  * bytes and sets *oldCount to newCount.
1477  *
1478  * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1479  *
1480  * @param old address of the pointer to the array
1481  *        *old may be NULL
1482  * @param elementSize the size of the elements of the array
1483  * @param oldCount address of the number of elements in the *old array
1484  * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1485  * @param filename where is this call being made (for debugging)
1486  * @param linenumber line where this call is being made (for debugging)
1487  */
1488 void
1489 GNUNET_xgrow_ (void **old,
1490                size_t elementSize,
1491                unsigned int *oldCount,
1492                unsigned int newCount,
1493                const char *filename,
1494                int linenumber);
1495
1496
1497 /**
1498  * @ingroup memory
1499  * Create a copy of the given message.
1500  *
1501  * @param msg message to copy
1502  * @return duplicate of the message
1503  */
1504 struct GNUNET_MessageHeader *
1505 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1506
1507
1508 /**
1509  * Set the async scope for the current thread.
1510  *
1511  * @param aid the async scope identifier
1512  * @param old_scope[out] location to save the old scope
1513  */
1514 void
1515 GNUNET_async_scope_enter (const struct GNUNET_AsyncScopeId *aid,
1516                           struct GNUNET_AsyncScopeSave *old_scope);
1517
1518
1519 /**
1520  * Clear the current thread's async scope.
1521  *
1522  * @param old_scope scope to restore
1523  */
1524 void
1525 GNUNET_async_scope_restore (struct GNUNET_AsyncScopeSave *old_scope);
1526
1527
1528 /**
1529  * Get the current async scope.
1530  *
1531  * @param[out] scope_ret pointer to where the result is stored
1532  */
1533 void
1534 GNUNET_async_scope_get (struct GNUNET_AsyncScopeSave *scope_ret);
1535
1536
1537 /**
1538  * Generate a fresh async scope identifier.
1539  *
1540  * @param[out] aid_ret pointer to where the result is stored
1541  */
1542 void
1543 GNUNET_async_scope_fresh (struct GNUNET_AsyncScopeId *aid_ret);
1544
1545
1546 #if __STDC_VERSION__ < 199901L
1547 #if __GNUC__ >= 2
1548 #define __func__ __FUNCTION__
1549 #else
1550 #define __func__ "<unknown>"
1551 #endif
1552 #endif
1553
1554
1555 /**
1556  * Valid task priorities.  Use these, do not pass random integers!
1557  * For various reasons (#3862 -- building with QT Creator, and
1558  * our restricted cross-compilation with emscripten) this cannot
1559  * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1560  * Naturally, logically this is part of the scheduler.
1561  */
1562 enum GNUNET_SCHEDULER_Priority
1563 {
1564   /**
1565    * Run with the same priority as the current job.
1566    */
1567   GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1568
1569   /**
1570    * Run when otherwise idle.
1571    */
1572   GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1573
1574   /**
1575    * Run as background job (higher than idle,
1576    * lower than default).
1577    */
1578   GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1579
1580   /**
1581    * Run with the default priority (normal
1582    * P2P operations).  Any task that is scheduled
1583    * without an explicit priority being specified
1584    * will run with this priority.
1585    */
1586   GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1587
1588   /**
1589    * Run with high priority (important requests).
1590    * Higher than DEFAULT.
1591    */
1592   GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1593
1594   /**
1595    * Run with priority for interactive tasks.
1596    * Higher than "HIGH".
1597    */
1598   GNUNET_SCHEDULER_PRIORITY_UI = 5,
1599
1600   /**
1601    * Run with priority for urgent tasks.  Use
1602    * for things like aborts and shutdowns that
1603    * need to preempt "UI"-level tasks.
1604    * Higher than "UI".
1605    */
1606   GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1607
1608   /**
1609    * This is an internal priority level that is only used for tasks
1610    * that are being triggered due to shutdown (they have automatically
1611    * highest priority).  User code must not use this priority level
1612    * directly.  Tasks run with this priority level that internally
1613    * schedule other tasks will see their original priority level
1614    * be inherited (unless otherwise specified).
1615    */
1616   GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1617
1618   /**
1619    * Number of priorities (must be the last priority).
1620    * This priority must not be used by clients.
1621    */
1622   GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1623 };
1624
1625
1626 #if 0 /* keep Emacsens' auto-indent happy */
1627 {
1628 #endif
1629 #ifdef __cplusplus
1630 }
1631 #endif
1632
1633 #endif /* GNUNET_COMMON_H */