gnunet_common.h 53 KB

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