h2s_full, useful for testing
[oweals/gnunet.git] / src / include / gnunet_common.h
index afc9b0bcbc82234be1983abfef9efd42a1014739..8703dc3b7870b4b690df43644d76ab07ccc99b0c 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2006 Christian Grothoff (and other contributing authors)
+     (C) 2006, 2009 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 #ifndef GNUNET_COMMON_H
 #define GNUNET_COMMON_H
 
+#if HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#if HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+#ifdef MINGW
+#include "winproc.h"
+#endif
+
 /**
  * Version of the API (for entire gnunetutil.so library).
  */
@@ -71,12 +81,13 @@ struct GNUNET_MessageHeader
 {
 
   /**
-   * The length of the struct (in bytes, including the length field itself)
+   * The length of the struct (in bytes, including the length field itself),
+   * in big-endian format.
    */
   uint16_t size GNUNET_PACKED;
 
   /**
-   * The type of the message (XX_CS_PROTO_XXXX)
+   * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
    */
   uint16_t type GNUNET_PACKED;
 
@@ -106,8 +117,8 @@ struct GNUNET_PeerIdentity
 /**
  * Function called with a filename.
  *
- * @param filename complete filename (absolute path)
  * @param cls closure
+ * @param filename complete filename (absolute path)
  * @return GNUNET_OK to continue to iterate,
  *  GNUNET_SYSERR to abort iteration with error!
  */
@@ -171,22 +182,23 @@ GNUNET_log_from (enum GNUNET_ErrorType kind,
 /**
  * Ignore the next n calls to the log function.
  *
- * @param n number of log calls to ignore, use 0 to
- *  assert that the log skip counter is currently zero.
+ * @param n number of log calls to ignore
+ * @param check_reset GNUNET_YES to assert that the log skip counter is currently zero
  */
-void GNUNET_log_skip (unsigned int n);
+void
+GNUNET_log_skip (unsigned int n, int check_reset);
 
 
 /**
  * Setup logging.
  *
- * @param component default component to use
+ * @param comp default component to use
  * @param loglevel what types of messages should be logged
  * @param logfile change logging to logfile (use NULL to keep stderr)
  * @return GNUNET_OK on success, GNUNET_SYSERR if logfile could not be opened
  */
 int
-GNUNET_log_setup (const char *component,
+GNUNET_log_setup (const char *comp,
                   const char *loglevel, const char *logfile);
 
 /**
@@ -206,6 +218,27 @@ void GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls);
 void GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls);
 
 
+/**
+ * Convert a hash value to a string (for printing debug messages).
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param hc the hash code
+ * @return string 
+ */
+const char *GNUNET_h2s (const GNUNET_HashCode *hc);
+
+/**
+ * Convert a hash value to a string (for printing debug messages).
+ * This prints all 104 characters of a hashcode!
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param hc the hash code
+ * @return string
+ */
+const char *GNUNET_h2s_full (const GNUNET_HashCode *hc);
+
 /**
  * Convert a peer identity to a string (for printing debug messages).
  * This is one of the very few calls in the entire API that is
@@ -301,7 +334,7 @@ unsigned long long GNUNET_htonll (unsigned long long n);
 /**
  * Maximum allocation with GNUNET_malloc macro.
  */
-#define GNUNET_MAX_GNUNET_MALLOC_CHECKED (1024 * 1024 * 40)
+#define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
 
 /**
  * Wrapper around malloc. Allocates size bytes of memory.
@@ -309,7 +342,7 @@ unsigned long long GNUNET_htonll (unsigned long long n);
  *
  * @param size the number of bytes to allocate, must be
  *        smaller than 40 MB.
- * @return pointer to size bytes of memory
+ * @return pointer to size bytes of memory, never NULL (!)
  */
 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
 
@@ -318,7 +351,7 @@ unsigned long long GNUNET_htonll (unsigned long long n);
  * The memory will be zero'ed out.
  *
  * @param size the number of bytes to allocate
- * @return pointer to size bytes of memory
+ * @return pointer to size bytes of memory, NULL if we do not have enough memory
  */
 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
 
@@ -401,11 +434,23 @@ unsigned long long GNUNET_htonll (unsigned long long n);
 
 /**
  * Like snprintf, just aborts if the buffer is of insufficient size.
+ *
+ * @param buf pointer to buffer that is written to
+ * @param size number of bytes in buf
+ * @param format format strings
+ * @param ... data for format string
+ * @return number of bytes written to buf or negative value on error
  */
 int GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
 
+
 /**
  * Like asprintf, just portable.
+ *
+ * @param buf set to a buffer of sufficient size (allocated, caller must free)
+ * @param format format string (see printf, fprintf, etc.)
+ * @param ... data for format string
+ * @return number of bytes in "*buf" excluding 0-termination
  */
 int GNUNET_asprintf (char **buf, const char *format, ...);
 
@@ -417,15 +462,26 @@ int GNUNET_asprintf (char **buf, const char *format, ...);
  * memory is available.  Don't use GNUNET_xmalloc_ directly. Use the
  * GNUNET_malloc macro.
  * The memory will be zero'ed out.
+ *
+ * @param size number of bytes to allocate
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ * @return allocated memory, never NULL
  */
 void *GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
 
+
 /**
- * Allocate memory.  This function does not check if the
- * allocation request is within reasonable bounds, allowing
- * allocations larger than 40 MB.  If you don't expect the
- * possibility of very large allocations, use GNUNET_malloc instead.
- * The memory will be zero'ed out.
+ * Allocate memory.  This function does not check if the allocation
+ * request is within reasonable bounds, allowing allocations larger
+ * than 40 MB.  If you don't expect the possibility of very large
+ * allocations, use GNUNET_malloc instead.  The memory will be zero'ed
+ * out.
+ *
+ * @param size number of bytes to allocate
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ * @return pointer to size bytes of memory, NULL if we do not have enough memory
  */
 void *GNUNET_xmalloc_unchecked_ (size_t size,
                                  const char *filename, int linenumber);
@@ -441,12 +497,20 @@ void *GNUNET_xrealloc_ (void *ptr,
  * Free memory. Merely a wrapper for the case that we
  * want to keep track of allocations.  Don't use GNUNET_xfree_
  * directly. Use the GNUNET_free macro.
+ *
+ * @param ptr pointer to memory to free
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
  */
 void GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
 
 
 /**
  * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the GNUNET_strdup macro.
+ * @param str string to duplicate
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ * @return the duplicated string
  */
 char *GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
 
@@ -462,6 +526,8 @@ char *GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
  * @param elementSize the size of the elements of the array
  * @param oldCount address of the number of elements in the *old array
  * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
  */
 void GNUNET_xgrow_ (void **old,
                     size_t elementSize,