gnunet_common.h 41 KB

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