aboutsummaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
Diffstat (limited to 'src/include')
-rw-r--r--src/include/gnunet_common.h494
1 files changed, 325 insertions, 169 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h
index 1f5600381..1916024b8 100644
--- a/src/include/gnunet_common.h
+++ b/src/include/gnunet_common.h
@@ -57,9 +57,8 @@
57#endif 57#endif
58 58
59#ifdef __cplusplus 59#ifdef __cplusplus
60extern "C" 60extern "C" {
61{ 61#if 0 /* keep Emacsens' auto-indent happy */
62#if 0 /* keep Emacsens' auto-indent happy */
63} 62}
64#endif 63#endif
65#endif 64#endif
@@ -76,14 +75,14 @@ extern "C"
76 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO != 75 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
77 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`. 76 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
78 */ 77 */
79#define GNUNET_OK 1 78#define GNUNET_OK 1
80#define GNUNET_SYSERR -1 79#define GNUNET_SYSERR -1
81#define GNUNET_YES 1 80#define GNUNET_YES 1
82#define GNUNET_NO 0 81#define GNUNET_NO 0
83 82
84#define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b)) 83#define GNUNET_MIN(a, b) (((a) < (b)) ? (a) : (b))
85 84
86#define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b)) 85#define GNUNET_MAX(a, b) (((a) > (b)) ? (a) : (b))
87 86
88/* some systems use one underscore only, and mingw uses no underscore... */ 87/* some systems use one underscore only, and mingw uses no underscore... */
89#ifndef __BYTE_ORDER 88#ifndef __BYTE_ORDER
@@ -117,7 +116,7 @@ extern "C"
117/** 116/**
118 * wrap va_arg for enums 117 * wrap va_arg for enums
119 */ 118 */
120#define GNUNET_VA_ARG_ENUM(va,X) ((enum X) va_arg (va, int)) 119#define GNUNET_VA_ARG_ENUM(va, X) ((enum X) va_arg (va, int))
121 120
122 121
123/** 122/**
@@ -133,37 +132,37 @@ extern "C"
133 * Endian operations 132 * Endian operations
134 */ 133 */
135 134
136# if __BYTE_ORDER == __LITTLE_ENDIAN 135#if __BYTE_ORDER == __LITTLE_ENDIAN
137# define GNUNET_htobe16(x) __bswap_16 (x) 136#define GNUNET_htobe16(x) __bswap_16 (x)
138# define GNUNET_htole16(x) (x) 137#define GNUNET_htole16(x) (x)
139# define GNUNET_be16toh(x) __bswap_16 (x) 138#define GNUNET_be16toh(x) __bswap_16 (x)
140# define GNUNET_le16toh(x) (x) 139#define GNUNET_le16toh(x) (x)
141 140
142# define GNUNET_htobe32(x) __bswap_32 (x) 141#define GNUNET_htobe32(x) __bswap_32 (x)
143# define GNUNET_htole32(x) (x) 142#define GNUNET_htole32(x) (x)
144# define GNUNET_be32toh(x) __bswap_32 (x) 143#define GNUNET_be32toh(x) __bswap_32 (x)
145# define GNUNET_le32toh(x) (x) 144#define GNUNET_le32toh(x) (x)
146 145
147# define GNUNET_htobe64(x) __bswap_64 (x) 146#define GNUNET_htobe64(x) __bswap_64 (x)
148# define GNUNET_htole64(x) (x) 147#define GNUNET_htole64(x) (x)
149# define GNUNET_be64toh(x) __bswap_64 (x) 148#define GNUNET_be64toh(x) __bswap_64 (x)
150# define GNUNET_le64toh(x) (x) 149#define GNUNET_le64toh(x) (x)
151#endif 150#endif
152# if __BYTE_ORDER == __BIG_ENDIAN 151#if __BYTE_ORDER == __BIG_ENDIAN
153# define GNUNET_htobe16(x) (x) 152#define GNUNET_htobe16(x) (x)
154# define GNUNET_htole16(x) __bswap_16 (x) 153#define GNUNET_htole16(x) __bswap_16 (x)
155# define GNUNET_be16toh(x) (x) 154#define GNUNET_be16toh(x) (x)
156# define GNUNET_le16toh(x) __bswap_16 (x) 155#define GNUNET_le16toh(x) __bswap_16 (x)
157 156
158# define GNUNET_htobe32(x) (x) 157#define GNUNET_htobe32(x) (x)
159# define GNUNET_htole32(x) __bswap_32 (x) 158#define GNUNET_htole32(x) __bswap_32 (x)
160# define GNUNET_be32toh(x) (x) 159#define GNUNET_be32toh(x) (x)
161# define GNUNET_le32toh(x) __bswap_32 (x) 160#define GNUNET_le32toh(x) __bswap_32 (x)
162 161
163# define GNUNET_htobe64(x) (x) 162#define GNUNET_htobe64(x) (x)
164# define GNUNET_htole64(x) __bswap_64 (x) 163#define GNUNET_htole64(x) __bswap_64 (x)
165# define GNUNET_be64toh(x) (x) 164#define GNUNET_be64toh(x) (x)
166# define GNUNET_le64toh(x) __bswap_64 (x) 165#define GNUNET_le64toh(x) __bswap_64 (x)
167#endif 166#endif
168 167
169 168
@@ -179,19 +178,19 @@ extern "C"
179 * on the stack with a variable-length that might be zero, write 178 * on the stack with a variable-length that might be zero, write
180 * "int[GNUNET_NZL(n)] x;" instead of "int[n] x". 179 * "int[GNUNET_NZL(n)] x;" instead of "int[n] x".
181 */ 180 */
182#define GNUNET_NZL(l) GNUNET_MAX(1,l) 181#define GNUNET_NZL(l) GNUNET_MAX (1, l)
183 182
184 183
185/** 184/**
186 * gcc-ism to get packed structs. 185 * gcc-ism to get packed structs.
187 */ 186 */
188#define GNUNET_PACKED __attribute__((packed)) 187#define GNUNET_PACKED __attribute__ ((packed))
189 188
190/** 189/**
191 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields 190 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
192 */ 191 */
193#if MINGW 192#if MINGW
194#define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct)) 193#define GNUNET_GCC_STRUCT_LAYOUT __attribute__ ((gcc_struct))
195#else 194#else
196#define GNUNET_GCC_STRUCT_LAYOUT 195#define GNUNET_GCC_STRUCT_LAYOUT
197#endif 196#endif
@@ -202,20 +201,20 @@ extern "C"
202 * bug #33594. 201 * bug #33594.
203 */ 202 */
204#ifdef __BIGGEST_ALIGNMENT__ 203#ifdef __BIGGEST_ALIGNMENT__
205#define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__))) 204#define GNUNET_ALIGN __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)))
206#else 205#else
207#define GNUNET_ALIGN __attribute__((aligned (8))) 206#define GNUNET_ALIGN __attribute__ ((aligned (8)))
208#endif 207#endif
209 208
210/** 209/**
211 * gcc-ism to document unused arguments 210 * gcc-ism to document unused arguments
212 */ 211 */
213#define GNUNET_UNUSED __attribute__((unused)) 212#define GNUNET_UNUSED __attribute__ ((unused))
214 213
215/** 214/**
216 * gcc-ism to document functions that don't return 215 * gcc-ism to document functions that don't return
217 */ 216 */
218#define GNUNET_NORETURN __attribute__((noreturn)) 217#define GNUNET_NORETURN __attribute__ ((noreturn))
219 218
220#if MINGW 219#if MINGW
221#if __GNUC__ > 3 220#if __GNUC__ > 3
@@ -225,9 +224,7 @@ extern "C"
225 * so we *only* use this on W32 (see #670578 from Debian); fortunately, 224 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
226 * W32 doesn't run on sparc anyway. 225 * W32 doesn't run on sparc anyway.
227 */ 226 */
228#define GNUNET_NETWORK_STRUCT_BEGIN \ 227#define GNUNET_NETWORK_STRUCT_BEGIN _Pragma ("pack(push)") _Pragma ("pack(1)")
229 _Pragma("pack(push)") \
230 _Pragma("pack(1)")
231 228
232/** 229/**
233 * gcc 4.x-ism to pack structures even on W32 (to be used after structs) 230 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
@@ -235,7 +232,7 @@ extern "C"
235 * so we *only* use this on W32 (see #670578 from Debian); fortunately, 232 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
236 * W32 doesn't run on sparc anyway. 233 * W32 doesn't run on sparc anyway.
237 */ 234 */
238#define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)") 235#define GNUNET_NETWORK_STRUCT_END _Pragma ("pack(pop)")
239 236
240#else 237#else
241#error gcc 4.x or higher required on W32 systems 238#error gcc 4.x or higher required on W32 systems
@@ -261,22 +258,20 @@ GNUNET_NETWORK_STRUCT_BEGIN
261 */ 258 */
262struct GNUNET_HashCode 259struct GNUNET_HashCode
263{ 260{
264 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */ 261 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
265}; 262};
266 263
267 264
268
269/** 265/**
270 * @brief A 256-bit hashcode. Used under special conditions, like when space 266 * @brief A 256-bit hashcode. Used under special conditions, like when space
271 * is critical and security is not impacted by it. 267 * is critical and security is not impacted by it.
272 */ 268 */
273struct GNUNET_ShortHashCode 269struct GNUNET_ShortHashCode
274{ 270{
275 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */ 271 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
276}; 272};
277 273
278 274
279
280/** 275/**
281 * Header for all communications. 276 * Header for all communications.
282 */ 277 */
@@ -293,7 +288,6 @@ struct GNUNET_MessageHeader
293 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format. 288 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
294 */ 289 */
295 uint16_t type GNUNET_PACKED; 290 uint16_t type GNUNET_PACKED;
296
297}; 291};
298 292
299 293
@@ -325,7 +319,7 @@ struct GNUNET_OperationResultMessage
325 */ 319 */
326struct GNUNET_AsyncScopeId 320struct GNUNET_AsyncScopeId
327{ 321{
328 uint32_t bits[16 / sizeof (uint32_t)]; /* = 16 bytes */ 322 uint32_t bits[16 / sizeof (uint32_t)]; /* = 16 bytes */
329}; 323};
330 324
331GNUNET_NETWORK_STRUCT_END 325GNUNET_NETWORK_STRUCT_END
@@ -334,7 +328,8 @@ GNUNET_NETWORK_STRUCT_END
334/** 328/**
335 * Saved async scope identifier or root scope. 329 * Saved async scope identifier or root scope.
336 */ 330 */
337struct GNUNET_AsyncScopeSave { 331struct GNUNET_AsyncScopeSave
332{
338 /** 333 /**
339 * Saved scope. Unused if 'have_scope==GNUNET_NO'. 334 * Saved scope. Unused if 'have_scope==GNUNET_NO'.
340 */ 335 */
@@ -356,9 +351,7 @@ struct GNUNET_AsyncScopeSave {
356 * #GNUNET_NO to stop iteration with no error, 351 * #GNUNET_NO to stop iteration with no error,
357 * #GNUNET_SYSERR to abort iteration with error! 352 * #GNUNET_SYSERR to abort iteration with error!
358 */ 353 */
359typedef int 354typedef int (*GNUNET_FileNameCallback) (void *cls, const char *filename);
360(*GNUNET_FileNameCallback) (void *cls,
361 const char *filename);
362 355
363 356
364/** 357/**
@@ -366,8 +359,7 @@ typedef int
366 * 359 *
367 * @param cls Closure. 360 * @param cls Closure.
368 */ 361 */
369typedef void 362typedef void (*GNUNET_ContinuationCallback) (void *cls);
370(*GNUNET_ContinuationCallback) (void *cls);
371 363
372 364
373/** 365/**
@@ -382,9 +374,10 @@ typedef void
382 * @param data_size 374 * @param data_size
383 * Size of @a data. 375 * Size of @a data.
384 */ 376 */
385typedef void 377typedef void (*GNUNET_ResultCallback) (void *cls,
386(*GNUNET_ResultCallback) (void *cls, int64_t result_code, 378 int64_t result_code,
387 const void *data, uint16_t data_size); 379 const void *data,
380 uint16_t data_size);
388 381
389 382
390/* ****************************** logging ***************************** */ 383/* ****************************** logging ***************************** */
@@ -420,12 +413,11 @@ enum GNUNET_ErrorType
420 * @param date when was the message logged? 413 * @param date when was the message logged?
421 * @param message what is the message 414 * @param message what is the message
422 */ 415 */
423typedef void 416typedef void (*GNUNET_Logger) (void *cls,
424(*GNUNET_Logger) (void *cls, 417 enum GNUNET_ErrorType kind,
425 enum GNUNET_ErrorType kind, 418 const char *component,
426 const char *component, 419 const char *date,
427 const char *date, 420 const char *message);
428 const char *message);
429 421
430 422
431/** 423/**
@@ -438,7 +430,7 @@ int
438GNUNET_get_log_skip (void); 430GNUNET_get_log_skip (void);
439 431
440 432
441#if !defined(GNUNET_CULL_LOGGING) 433#if ! defined(GNUNET_CULL_LOGGING)
442int 434int
443GNUNET_get_log_call_status (int caller_level, 435GNUNET_get_log_call_status (int caller_level,
444 const char *comp, 436 const char *comp,
@@ -462,23 +454,23 @@ GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
462 454
463/* from glib */ 455/* from glib */
464#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 456#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
465#define _GNUNET_BOOLEAN_EXPR(expr) \ 457#define _GNUNET_BOOLEAN_EXPR(expr) \
466 __extension__ ({ \ 458 __extension__({ \
467 int _gnunet_boolean_var_; \ 459 int _gnunet_boolean_var_; \
468 if (expr) \ 460 if (expr) \
469 _gnunet_boolean_var_ = 1; \ 461 _gnunet_boolean_var_ = 1; \
470 else \ 462 else \
471 _gnunet_boolean_var_ = 0; \ 463 _gnunet_boolean_var_ = 0; \
472 _gnunet_boolean_var_; \ 464 _gnunet_boolean_var_; \
473}) 465 })
474#define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1)) 466#define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR (expr), 1))
475#define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0)) 467#define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR (expr), 0))
476#else 468#else
477#define GN_LIKELY(expr) (expr) 469#define GN_LIKELY(expr) (expr)
478#define GN_UNLIKELY(expr) (expr) 470#define GN_UNLIKELY(expr) (expr)
479#endif 471#endif
480 472
481#if !defined(GNUNET_LOG_CALL_STATUS) 473#if ! defined(GNUNET_LOG_CALL_STATUS)
482#define GNUNET_LOG_CALL_STATUS -1 474#define GNUNET_LOG_CALL_STATUS -1
483#endif 475#endif
484 476
@@ -494,35 +486,65 @@ GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
494 * @param ... arguments for format string 486 * @param ... arguments for format string
495 */ 487 */
496void 488void
497GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp, 489GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind,
498 const char *message, ...); 490 const char *comp,
499 491 const char *message,
500#if !defined(GNUNET_CULL_LOGGING) 492 ...);
501#define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\ 493
502 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\ 494#if ! defined(GNUNET_CULL_LOGGING)
503 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \ 495#define GNUNET_log_from(kind, comp, ...) \
504 if (GN_UNLIKELY(log_call_enabled == -1))\ 496 do \
505 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \ 497 { \
506 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\ 498 int log_line = __LINE__; \
507 else {\ 499 static int log_call_enabled = GNUNET_LOG_CALL_STATUS; \
508 if (GN_UNLIKELY(log_call_enabled))\ 500 if ((GNUNET_EXTRA_LOGGING > 0) || \
509 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \ 501 ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) \
510 }\ 502 { \
511 }\ 503 if (GN_UNLIKELY (log_call_enabled == -1)) \
512} while (0) 504 log_call_enabled = \
513 505 GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), \
514 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\ 506 (comp), \
515 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\ 507 __FILE__, \
516 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \ 508 __FUNCTION__, \
517 if (GN_UNLIKELY(log_call_enabled == -1))\ 509 log_line); \
518 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\ 510 if (GN_UNLIKELY (GNUNET_get_log_skip () > 0)) \
519 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\ 511 { \
520 else {\ 512 GNUNET_log_skip (-1, GNUNET_NO); \
521 if (GN_UNLIKELY(log_call_enabled))\ 513 } \
522 GNUNET_log_nocheck ((kind), __VA_ARGS__); \ 514 else \
523 }\ 515 { \
524 }\ 516 if (GN_UNLIKELY (log_call_enabled)) \
525} while (0) 517 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
518 } \
519 } \
520 } while (0)
521
522#define GNUNET_log(kind, ...) \
523 do \
524 { \
525 int log_line = __LINE__; \
526 static int log_call_enabled = GNUNET_LOG_CALL_STATUS; \
527 if ((GNUNET_EXTRA_LOGGING > 0) || \
528 ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) \
529 { \
530 if (GN_UNLIKELY (log_call_enabled == -1)) \
531 log_call_enabled = \
532 GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), \
533 NULL, \
534 __FILE__, \
535 __FUNCTION__, \
536 log_line); \
537 if (GN_UNLIKELY (GNUNET_get_log_skip () > 0)) \
538 { \
539 GNUNET_log_skip (-1, GNUNET_NO); \
540 } \
541 else \
542 { \
543 if (GN_UNLIKELY (log_call_enabled)) \
544 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
545 } \
546 } \
547 } while (0)
526#else 548#else
527#define GNUNET_log(...) 549#define GNUNET_log(...)
528#define GNUNET_log_from(...) 550#define GNUNET_log_from(...)
@@ -539,8 +561,8 @@ GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
539 */ 561 */
540void 562void
541GNUNET_log_config_missing (enum GNUNET_ErrorType kind, 563GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
542 const char *section, 564 const char *section,
543 const char *option); 565 const char *option);
544 566
545 567
546/** 568/**
@@ -554,9 +576,9 @@ GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
554 */ 576 */
555void 577void
556GNUNET_log_config_invalid (enum GNUNET_ErrorType kind, 578GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
557 const char *section, 579 const char *section,
558 const char *option, 580 const char *option,
559 const char *required); 581 const char *required);
560 582
561 583
562/** 584/**
@@ -577,8 +599,7 @@ GNUNET_abort_ (void) GNUNET_NORETURN;
577 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero 599 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
578 */ 600 */
579void 601void
580GNUNET_log_skip (int n, 602GNUNET_log_skip (int n, int check_reset);
581 int check_reset);
582 603
583 604
584/** 605/**
@@ -591,9 +612,7 @@ GNUNET_log_skip (int n,
591 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened 612 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
592 */ 613 */
593int 614int
594GNUNET_log_setup (const char *comp, 615GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile);
595 const char *loglevel,
596 const char *logfile);
597 616
598 617
599/** 618/**
@@ -607,8 +626,7 @@ GNUNET_log_setup (const char *comp,
607 * @param logger_cls closure for @a logger 626 * @param logger_cls closure for @a logger
608 */ 627 */
609void 628void
610GNUNET_logger_add (GNUNET_Logger logger, 629GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls);
611 void *logger_cls);
612 630
613 631
614/** 632/**
@@ -619,8 +637,7 @@ GNUNET_logger_add (GNUNET_Logger logger,
619 * @param logger_cls closure for @a logger 637 * @param logger_cls closure for @a logger
620 */ 638 */
621void 639void
622GNUNET_logger_remove (GNUNET_Logger logger, 640GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls);
623 void *logger_cls);
624 641
625 642
626/** 643/**
@@ -798,8 +815,7 @@ GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
798 * will be overwritten by next call to #GNUNET_a2s(). 815 * will be overwritten by next call to #GNUNET_a2s().
799 */ 816 */
800const char * 817const char *
801GNUNET_a2s (const struct sockaddr *addr, 818GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen);
802 socklen_t addrlen);
803 819
804 820
805/** 821/**
@@ -817,14 +833,36 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
817 * @ingroup logging 833 * @ingroup logging
818 * Use this for fatal errors that cannot be handled 834 * Use this for fatal errors that cannot be handled
819 */ 835 */
820#define GNUNET_assert(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d. Aborting.\n"), __FILE__, __LINE__); GNUNET_abort_(); } } while(0) 836#define GNUNET_assert(cond) \
837 do \
838 { \
839 if (! (cond)) \
840 { \
841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
842 _ ("Assertion failed at %s:%d. Aborting.\n"), \
843 __FILE__, \
844 __LINE__); \
845 GNUNET_abort_ (); \
846 } \
847 } while (0)
821 848
822 849
823/** 850/**
824 * @ingroup logging 851 * @ingroup logging
825 * Use this for fatal errors that cannot be handled 852 * Use this for fatal errors that cannot be handled
826 */ 853 */
827#define GNUNET_assert_at(cond, f, l) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d. Aborting.\n"), f, l); GNUNET_abort_(); } } while(0) 854#define GNUNET_assert_at(cond, f, l) \
855 do \
856 { \
857 if (! (cond)) \
858 { \
859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
860 _ ("Assertion failed at %s:%d. Aborting.\n"), \
861 f, \
862 l); \
863 GNUNET_abort_ (); \
864 } \
865 } while (0)
828 866
829 867
830/** 868/**
@@ -834,7 +872,19 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
834 * @param cond Condition to evaluate 872 * @param cond Condition to evaluate
835 * @param comp Component string to use for logging 873 * @param comp Component string to use for logging
836 */ 874 */
837#define GNUNET_assert_from(cond, comp) do { if (! (cond)) { GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, comp, _("Assertion failed at %s:%d. Aborting.\n"), __FILE__, __LINE__); GNUNET_abort_(); } } while(0) 875#define GNUNET_assert_from(cond, comp) \
876 do \
877 { \
878 if (! (cond)) \
879 { \
880 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, \
881 comp, \
882 _ ("Assertion failed at %s:%d. Aborting.\n"), \
883 __FILE__, \
884 __LINE__); \
885 GNUNET_abort_ (); \
886 } \
887 } while (0)
838 888
839 889
840/** 890/**
@@ -842,7 +892,17 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
842 * Use this for internal assertion violations that are 892 * Use this for internal assertion violations that are
843 * not fatal (can be handled) but should not occur. 893 * not fatal (can be handled) but should not occur.
844 */ 894 */
845#define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0) 895#define GNUNET_break(cond) \
896 do \
897 { \
898 if (! (cond)) \
899 { \
900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
901 _ ("Assertion failed at %s:%d.\n"), \
902 __FILE__, \
903 __LINE__); \
904 } \
905 } while (0)
846 906
847 907
848/** 908/**
@@ -854,7 +914,17 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
854 * we still want to see these problems during 914 * we still want to see these problems during
855 * development and testing. "OP == other peer". 915 * development and testing. "OP == other peer".
856 */ 916 */
857#define GNUNET_break_op(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, _("External protocol violation detected at %s:%d.\n"), __FILE__, __LINE__); } } while(0) 917#define GNUNET_break_op(cond) \
918 do \
919 { \
920 if (! (cond)) \
921 { \
922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, \
923 _ ("External protocol violation detected at %s:%d.\n"), \
924 __FILE__, \
925 __LINE__); \
926 } \
927 } while (0)
858 928
859 929
860/** 930/**
@@ -863,7 +933,16 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
863 * a failure of the command 'cmd' with the message given 933 * a failure of the command 'cmd' with the message given
864 * by strerror(errno). 934 * by strerror(errno).
865 */ 935 */
866#define GNUNET_log_strerror(level, cmd) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, STRERROR(errno)); } while(0) 936#define GNUNET_log_strerror(level, cmd) \
937 do \
938 { \
939 GNUNET_log (level, \
940 _ ("`%s' failed at %s:%d with error: %s\n"), \
941 cmd, \
942 __FILE__, \
943 __LINE__, \
944 STRERROR (errno)); \
945 } while (0)
867 946
868 947
869/** 948/**
@@ -872,7 +951,17 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
872 * a failure of the command 'cmd' with the message given 951 * a failure of the command 'cmd' with the message given
873 * by strerror(errno). 952 * by strerror(errno).
874 */ 953 */
875#define GNUNET_log_from_strerror(level, component, cmd) do { GNUNET_log_from (level, component, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, STRERROR(errno)); } while(0) 954#define GNUNET_log_from_strerror(level, component, cmd) \
955 do \
956 { \
957 GNUNET_log_from (level, \
958 component, \
959 _ ("`%s' failed at %s:%d with error: %s\n"), \
960 cmd, \
961 __FILE__, \
962 __LINE__, \
963 STRERROR (errno)); \
964 } while (0)
876 965
877 966
878/** 967/**
@@ -881,7 +970,17 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
881 * a failure of the command 'cmd' with the message given 970 * a failure of the command 'cmd' with the message given
882 * by strerror(errno). 971 * by strerror(errno).
883 */ 972 */
884#define GNUNET_log_strerror_file(level, cmd, filename) do { GNUNET_log(level, _("`%s' failed on file `%s' at %s:%d with error: %s\n"), cmd, filename, __FILE__, __LINE__, STRERROR(errno)); } while(0) 973#define GNUNET_log_strerror_file(level, cmd, filename) \
974 do \
975 { \
976 GNUNET_log (level, \
977 _ ("`%s' failed on file `%s' at %s:%d with error: %s\n"), \
978 cmd, \
979 filename, \
980 __FILE__, \
981 __LINE__, \
982 STRERROR (errno)); \
983 } while (0)
885 984
886 985
887/** 986/**
@@ -890,7 +989,18 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
890 * a failure of the command 'cmd' with the message given 989 * a failure of the command 'cmd' with the message given
891 * by strerror(errno). 990 * by strerror(errno).
892 */ 991 */
893#define GNUNET_log_from_strerror_file(level, component, cmd, filename) do { GNUNET_log_from (level, component, _("`%s' failed on file `%s' at %s:%d with error: %s\n"), cmd, filename,__FILE__, __LINE__, STRERROR(errno)); } while(0) 992#define GNUNET_log_from_strerror_file(level, component, cmd, filename) \
993 do \
994 { \
995 GNUNET_log_from (level, \
996 component, \
997 _ ("`%s' failed on file `%s' at %s:%d with error: %s\n"), \
998 cmd, \
999 filename, \
1000 __FILE__, \
1001 __LINE__, \
1002 STRERROR (errno)); \
1003 } while (0)
894 1004
895/* ************************* endianess conversion ****************** */ 1005/* ************************* endianess conversion ****************** */
896 1006
@@ -965,12 +1075,12 @@ GNUNET_ntoh_double (double d);
965 * Compare memory in @a a and @a b, where both must be of 1075 * Compare memory in @a a and @a b, where both must be of
966 * the same pointer type. 1076 * the same pointer type.
967 */ 1077 */
968#define GNUNET_memcmp(a,b) ({ \ 1078#define GNUNET_memcmp(a, b) \
969 const typeof(*b) * _a = (a); \ 1079 ({ \
970 const typeof(*a) * _b = (b); \ 1080 const typeof (*b) *_a = (a); \
971 memcmp(_a, \ 1081 const typeof (*a) *_b = (b); \
972 _b, \ 1082 memcmp (_a, _b, sizeof (*a)); \
973 sizeof (*a)); }) 1083 })
974 1084
975 1085
976/** 1086/**
@@ -979,11 +1089,11 @@ GNUNET_ntoh_double (double d);
979 * @param a pointer to a struct which should be tested for the 1089 * @param a pointer to a struct which should be tested for the
980 * entire memory being zero'ed out. 1090 * entire memory being zero'ed out.
981 */ 1091 */
982#define GNUNET_is_zero(a) ({ \ 1092#define GNUNET_is_zero(a) \
983 static const typeof(*a) _z; \ 1093 ({ \
984 memcmp((a), \ 1094 static const typeof (*a) _z; \
985 &_z, \ 1095 memcmp ((a), &_z, sizeof (_z)); \
986 sizeof (_z)); }) 1096 })
987 1097
988 1098
989/** 1099/**
@@ -995,7 +1105,14 @@ GNUNET_ntoh_double (double d);
995 * @param src source of the copy, may be NULL if @a n is zero 1105 * @param src source of the copy, may be NULL if @a n is zero
996 * @param n number of bytes to copy 1106 * @param n number of bytes to copy
997 */ 1107 */
998#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) 1108#define GNUNET_memcpy(dst, src, n) \
1109 do \
1110 { \
1111 if (0 != n) \
1112 { \
1113 (void) memcpy (dst, src, n); \
1114 } \
1115 } while (0)
999 1116
1000 1117
1001/** 1118/**
@@ -1018,7 +1135,8 @@ GNUNET_ntoh_double (double d);
1018 * @param m size of the second dimension 1135 * @param m size of the second dimension
1019 * @param type name of the struct or union, i.e. pass 'struct Foo'. 1136 * @param type name of the struct or union, i.e. pass 'struct Foo'.
1020 */ 1137 */
1021#define GNUNET_new_array_2d(n, m, type) (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__) 1138#define GNUNET_new_array_2d(n, m, type) \
1139 (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__)
1022 1140
1023/** 1141/**
1024 * @ingroup memory 1142 * @ingroup memory
@@ -1030,7 +1148,8 @@ GNUNET_ntoh_double (double d);
1030 * @param o size of the third dimension 1148 * @param o size of the third dimension
1031 * @param type name of the struct or union, i.e. pass 'struct Foo'. 1149 * @param type name of the struct or union, i.e. pass 'struct Foo'.
1032 */ 1150 */
1033#define GNUNET_new_array_3d(n, m, o, type) (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__) 1151#define GNUNET_new_array_3d(n, m, o, type) \
1152 (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__)
1034 1153
1035/** 1154/**
1036 * @ingroup memory 1155 * @ingroup memory
@@ -1041,7 +1160,7 @@ GNUNET_ntoh_double (double d);
1041 * smaller than 40 MB. 1160 * smaller than 40 MB.
1042 * @return pointer to size bytes of memory, never NULL (!) 1161 * @return pointer to size bytes of memory, never NULL (!)
1043 */ 1162 */
1044#define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__) 1163#define GNUNET_malloc(size) GNUNET_xmalloc_ (size, __FILE__, __LINE__)
1045 1164
1046/** 1165/**
1047 * @ingroup memory 1166 * @ingroup memory
@@ -1051,7 +1170,7 @@ GNUNET_ntoh_double (double d);
1051 * @param size the number of bytes in buf (and size of the allocation) 1170 * @param size the number of bytes in buf (and size of the allocation)
1052 * @return pointer to size bytes of memory, never NULL (!) 1171 * @return pointer to size bytes of memory, never NULL (!)
1053 */ 1172 */
1054#define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__) 1173#define GNUNET_memdup(buf, size) GNUNET_xmemdup_ (buf, size, __FILE__, __LINE__)
1055 1174
1056/** 1175/**
1057 * @ingroup memory 1176 * @ingroup memory
@@ -1061,7 +1180,8 @@ GNUNET_ntoh_double (double d);
1061 * @param size the number of bytes to allocate 1180 * @param size the number of bytes to allocate
1062 * @return pointer to size bytes of memory, NULL if we do not have enough memory 1181 * @return pointer to size bytes of memory, NULL if we do not have enough memory
1063 */ 1182 */
1064#define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__) 1183#define GNUNET_malloc_large(size) \
1184 GNUNET_xmalloc_unchecked_ (size, __FILE__, __LINE__)
1065 1185
1066/** 1186/**
1067 * @ingroup memory 1187 * @ingroup memory
@@ -1072,7 +1192,8 @@ GNUNET_ntoh_double (double d);
1072 * @param size the number of bytes to reallocate 1192 * @param size the number of bytes to reallocate
1073 * @return pointer to size bytes of memory 1193 * @return pointer to size bytes of memory
1074 */ 1194 */
1075#define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__) 1195#define GNUNET_realloc(ptr, size) \
1196 GNUNET_xrealloc_ (ptr, size, __FILE__, __LINE__)
1076 1197
1077/** 1198/**
1078 * @ingroup memory 1199 * @ingroup memory
@@ -1083,7 +1204,7 @@ GNUNET_ntoh_double (double d);
1083 * @param ptr location where to free the memory. ptr must have 1204 * @param ptr location where to free the memory. ptr must have
1084 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier. 1205 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
1085 */ 1206 */
1086#define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__) 1207#define GNUNET_free(ptr) GNUNET_xfree_ (ptr, __FILE__, __LINE__)
1087 1208
1088/** 1209/**
1089 * @ingroup memory 1210 * @ingroup memory
@@ -1092,7 +1213,15 @@ GNUNET_ntoh_double (double d);
1092 * 1213 *
1093 * @param ptr the location in memory to free 1214 * @param ptr the location in memory to free
1094 */ 1215 */
1095#define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0) 1216#define GNUNET_free_non_null(ptr) \
1217 do \
1218 { \
1219 void *__x__ = ptr; \
1220 if (__x__ != NULL) \
1221 { \
1222 GNUNET_free (__x__); \
1223 } \
1224 } while (0)
1096 1225
1097/** 1226/**
1098 * @ingroup memory 1227 * @ingroup memory
@@ -1102,7 +1231,7 @@ GNUNET_ntoh_double (double d);
1102 * @param a pointer to a zero-terminated string 1231 * @param a pointer to a zero-terminated string
1103 * @return a copy of the string including zero-termination 1232 * @return a copy of the string including zero-termination
1104 */ 1233 */
1105#define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__) 1234#define GNUNET_strdup(a) GNUNET_xstrdup_ (a, __FILE__, __LINE__)
1106 1235
1107/** 1236/**
1108 * @ingroup memory 1237 * @ingroup memory
@@ -1113,7 +1242,8 @@ GNUNET_ntoh_double (double d);
1113 * @param length of the string to duplicate 1242 * @param length of the string to duplicate
1114 * @return a partial copy of the string including zero-termination 1243 * @return a partial copy of the string including zero-termination
1115 */ 1244 */
1116#define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__) 1245#define GNUNET_strndup(a, length) \
1246 GNUNET_xstrndup_ (a, length, __FILE__, __LINE__)
1117 1247
1118/** 1248/**
1119 * @ingroup memory 1249 * @ingroup memory
@@ -1150,7 +1280,13 @@ GNUNET_ntoh_double (double d);
1150 * @param tsize the target size for the resulting vector, use 0 to 1280 * @param tsize the target size for the resulting vector, use 0 to
1151 * free the vector (then, arr will be NULL afterwards). 1281 * free the vector (then, arr will be NULL afterwards).
1152 */ 1282 */
1153#define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&(arr), sizeof((arr)[0]), &size, tsize, __FILE__, __LINE__) 1283#define GNUNET_array_grow(arr, size, tsize) \
1284 GNUNET_xgrow_ ((void **) &(arr), \
1285 sizeof ((arr)[0]), \
1286 &size, \
1287 tsize, \
1288 __FILE__, \
1289 __LINE__)
1154 1290
1155/** 1291/**
1156 * @ingroup memory 1292 * @ingroup memory
@@ -1165,7 +1301,12 @@ GNUNET_ntoh_double (double d);
1165 * array size 1301 * array size
1166 * @param element the element that will be appended to the array 1302 * @param element the element that will be appended to the array
1167 */ 1303 */
1168#define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); (arr)[size-1] = element; } while(0) 1304#define GNUNET_array_append(arr, size, element) \
1305 do \
1306 { \
1307 GNUNET_array_grow (arr, size, size + 1); \
1308 (arr)[size - 1] = element; \
1309 } while (0)
1169 1310
1170/** 1311/**
1171 * @ingroup memory 1312 * @ingroup memory
@@ -1226,8 +1367,11 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
1226 * @return allocated memory, never NULL 1367 * @return allocated memory, never NULL
1227 */ 1368 */
1228void ** 1369void **
1229GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize, 1370GNUNET_xnew_array_2d_ (size_t n,
1230 const char *filename, int linenumber); 1371 size_t m,
1372 size_t elementSize,
1373 const char *filename,
1374 int linenumber);
1231 1375
1232 1376
1233/** 1377/**
@@ -1246,8 +1390,12 @@ GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
1246 * @return allocated memory, never NULL 1390 * @return allocated memory, never NULL
1247 */ 1391 */
1248void *** 1392void ***
1249GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize, 1393GNUNET_xnew_array_3d_ (size_t n,
1250 const char *filename, int linenumber); 1394 size_t m,
1395 size_t o,
1396 size_t elementSize,
1397 const char *filename,
1398 int linenumber);
1251 1399
1252 1400
1253/** 1401/**
@@ -1262,7 +1410,9 @@ GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
1262 * @return allocated memory, never NULL 1410 * @return allocated memory, never NULL
1263 */ 1411 */
1264void * 1412void *
1265GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, 1413GNUNET_xmemdup_ (const void *buf,
1414 size_t size,
1415 const char *filename,
1266 int linenumber); 1416 int linenumber);
1267 1417
1268 1418
@@ -1323,7 +1473,9 @@ GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1323 * @return the duplicated string 1473 * @return the duplicated string
1324 */ 1474 */
1325char * 1475char *
1326GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, 1476GNUNET_xstrndup_ (const char *str,
1477 size_t len,
1478 const char *filename,
1327 int linenumber); 1479 int linenumber);
1328 1480
1329/** 1481/**
@@ -1342,8 +1494,12 @@ GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1342 * @param linenumber line where this call is being made (for debugging) 1494 * @param linenumber line where this call is being made (for debugging)
1343 */ 1495 */
1344void 1496void
1345GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, 1497GNUNET_xgrow_ (void **old,
1346 unsigned int newCount, const char *filename, int linenumber); 1498 size_t elementSize,
1499 unsigned int *oldCount,
1500 unsigned int newCount,
1501 const char *filename,
1502 int linenumber);
1347 1503
1348 1504
1349/** 1505/**
@@ -1475,7 +1631,7 @@ enum GNUNET_SCHEDULER_Priority
1475}; 1631};
1476 1632
1477 1633
1478#if 0 /* keep Emacsens' auto-indent happy */ 1634#if 0 /* keep Emacsens' auto-indent happy */
1479{ 1635{
1480#endif 1636#endif
1481#ifdef __cplusplus 1637#ifdef __cplusplus