check
[oweals/gnunet.git] / src / include / gnunet_common.h
index 402674f48922c0fc97b70828b31ac95d7b78c3e6..73945a809b1a9c30569ffa1e6915ff67dc1831ca 100644 (file)
 #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
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+#ifdef HAVE_STDARG_H
+#include <stdarg.h>
+#endif
 /**
  * Version of the API (for entire gnunetutil.so library).
  */
@@ -71,12 +87,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 +123,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!
  */
@@ -121,6 +138,7 @@ typedef int (*GNUNET_FileNameCallback) (void *cls, const char *filename);
  */
 enum GNUNET_ErrorType
 {
+  GNUNET_ERROR_TYPE_NONE = 0,
   GNUNET_ERROR_TYPE_ERROR = 1,
   GNUNET_ERROR_TYPE_WARNING = 2,
   GNUNET_ERROR_TYPE_INFO = 4,
@@ -129,6 +147,7 @@ enum GNUNET_ErrorType
   GNUNET_ERROR_TYPE_BULK = 32
 };
 
+
 /**
  * User-defined handler for log messages.
  *
@@ -181,15 +200,16 @@ 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);
 
+
 /**
  * Add a custom logger.
  *
@@ -198,6 +218,7 @@ GNUNET_log_setup (const char *component,
  */
 void GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls);
 
+
 /**
  * Remove a custom logger.
  *
@@ -218,6 +239,18 @@ void GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls);
 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
@@ -251,6 +284,7 @@ const char *GNUNET_a2s (const struct sockaddr *addr,
  */
 const char *GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
 
+
 /**
  * Use this for fatal errors that cannot be handled
  */
@@ -321,16 +355,25 @@ 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__)
 
+/**
+ * Allocate and initialize a block of memory.
+ * 
+ * @param buf data to initalize the block with
+ * @param size the number of bytes in buf (and size of the allocation)
+ * @return pointer to size bytes of memory, never NULL (!)
+ */
+#define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
+
 /**
  * Wrapper around malloc. Allocates size bytes of memory.
  * 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__)
 
@@ -450,18 +493,33 @@ int GNUNET_asprintf (char **buf, const char *format, ...);
 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 and initialize memory. Checks the return value, aborts if no more
+ * memory is available.  Don't use GNUNET_xmemdup_ directly. Use the
+ * GNUNET_memdup macro.
  *
+ * @param buf buffer to initialize from (must contain size bytes)
  * @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_xmemdup_ (const void *buf, 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.
+ *
+ * @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);
 
@@ -470,7 +528,7 @@ void *GNUNET_xmalloc_unchecked_ (size_t size,
  * memory is available.
  */
 void *GNUNET_xrealloc_ (void *ptr,
-                        const size_t n, const char *filename, int linenumber);
+                        size_t n, const char *filename, int linenumber);
 
 /**
  * Free memory. Merely a wrapper for the case that we