gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r3027 - GNUnet/src/include


From: grothoff
Subject: [GNUnet-SVN] r3027 - GNUnet/src/include
Date: Fri, 23 Jun 2006 08:04:41 -0700 (PDT)

Author: grothoff
Date: 2006-06-23 08:04:37 -0700 (Fri, 23 Jun 2006)
New Revision: 3027

Added:
   GNUnet/src/include/gnunet_state_service.h
   GNUnet/src/include/gnunet_util_cron.h
   GNUnet/src/include/gnunet_util_disk.h
   GNUnet/src/include/gnunet_util_getopt.h
   GNUnet/src/include/gnunet_util_network.h
   GNUnet/src/include/gnunet_util_os.h
   GNUnet/src/include/gnunet_util_string.h
   GNUnet/src/include/gnunet_util_threads.h
Log:
headers

Added: GNUnet/src/include/gnunet_state_service.h
===================================================================
--- GNUnet/src/include/gnunet_state_service.h   2006-06-23 15:04:30 UTC (rev 
3026)
+++ GNUnet/src/include/gnunet_state_service.h   2006-06-23 15:04:37 UTC (rev 
3027)
@@ -0,0 +1,98 @@
+/*
+      This file is part of GNUnet
+      (C) 2002, 2003, 2004, 2005, 2006 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
+      by the Free Software Foundation; either version 2, or (at your
+      option) any later version.
+
+      GNUnet is distributed in the hope that it will be useful, but
+      WITHOUT ANY WARRANTY; without even the implied warranty of
+      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+      General Public License for more details.
+
+      You should have received a copy of the GNU General Public License
+      along with GNUnet; see the file COPYING.  If not, write to the
+      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+      Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * @file include/gnunet_state_service.h
+ * @brief module to help keep some small bits of persistent state by name
+ * @author Christian Grothoff
+ */
+
+#ifndef GNUNET_STATE_SERVICE_API_H
+#define GNUNET_STATE_SERVICE_API_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/**
+ * @brief API to the stats service
+ */
+typedef struct {
+
+  /**
+   * Read the contents of a bucket to a buffer.
+   *
+   * @param fn the hashcode representing the entry
+   * @param result the buffer to write the result to
+   *        (*result should be NULL, sufficient space is allocated)
+   * @return the number of bytes read on success, -1 on failure
+   */
+  int (*read)(struct GE_Context * ectx,
+             const char * name,
+             void ** result);
+  
+  /**
+   * Append content to file.
+   *
+   * @param fn the key for the entry
+   * @param len the number of bytes in block
+   * @param block the data to store
+   * @return SYSERR on error, OK if ok.
+   */
+  int (*append)(struct GE_Context * ectx,
+               const char * name,
+               int len,
+               const void * block);
+  
+  /**
+   * Write content to a file.
+   *
+   * @param fn the key for the entry
+   * @param len the number of bytes in block
+   * @param block the data to store
+   * @return SYSERR on error, OK if ok.
+   */
+  int (*write)(struct GE_Context * ectx,
+              const char * name,
+              int len,
+              const void * block);
+  
+  /**
+   * Free space in the database by removing one file
+   * @param name the hashcode representing the name of the file
+   *        (without directory)
+   */
+  int (*unlink)(struct GE_Context * ectx,
+               const char * name);
+
+} State_ServiceAPI;
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* STATE_SERVICE_API_H */

Added: GNUnet/src/include/gnunet_util_cron.h
===================================================================
--- GNUnet/src/include/gnunet_util_cron.h       2006-06-23 15:04:30 UTC (rev 
3026)
+++ GNUnet/src/include/gnunet_util_cron.h       2006-06-23 15:04:37 UTC (rev 
3027)
@@ -0,0 +1,159 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001, 2002, 2003, 2004, 2005, 2006 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
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_util_cron.h
+ * @brief periodic job runner
+ * @author Christian Grothoff
+ * @author Krista Bennett
+ * @author Ioana Patrascu
+ * @author Tzvetan Horozov
+ */
+
+#ifndef GNUNET_UTIL_CRON_H
+#define GNUNET_UTIL_CRON_H
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/**
+ * @brief constants to specify time
+ */
+#define cronMILLIS ((cron_t)1)
+#define cronSECONDS ((cron_t)(1000 * cronMILLIS))
+#define cronMINUTES ((cron_t) (60 * cronSECONDS))
+#define cronHOURS ((cron_t)(60 * cronMINUTES))
+#define cronDAYS ((cron_t)(24 * cronHOURS))
+#define cronWEEKS ((cron_t)(7 * cronDAYS))
+#define cronMONTHS ((cron_t)(30 * cronDAYS))
+#define cronYEARS ((cron_t)(365 * cronDAYS))
+
+/**
+ * Type of a cron-job method.
+ */
+typedef void (*CronJob)(void *);
+
+/**
+ * Time for absolute times used by cron (64 bit)
+ */
+typedef unsigned long long cron_t;
+
+/**
+ * Start the cron jobs.
+ */
+void cron_start_jobs(void);
+
+/**
+ * Stop the cron service.
+ */
+void cron_stop_jobs(void);
+
+/**
+ * Stop running cron-jobs for a short time.  This method may only be
+ * called by a thread that is not holding any locks.  It will cause
+ * a deadlock if this method is called from within a cron-job and
+ * checkself is NO.  If checkself is YES and this method is called
+ * within a cron-job, nothing happens.
+ *
+ * @param checkself, if YES and this thread is the cron thread, do nothing
+ */
+void cron_suspend_jobs(int checkself);
+
+/**
+ * Resume running cron-jobs.  Call must be matched by
+ * previous call to cron_suspend_jobs with identical
+ * arguments.
+ *
+ * @param checkself, if YES and this thread is the cron thread, do nothing
+ */
+void cron_resume_jobs(int checkself);
+
+/**
+ * Is the cron-thread currently running?
+ */
+int cron_test_running(void);
+
+/**
+ * Get the current time (in cron-units).
+ *
+ * @return the current time
+ */
+cron_t cron_get_time(void);
+
+/**
+ * Add a cron-job to the delta list.
+ * @param method which method should we run
+ * @param delta how many milliseconds until we run the method
+ * @param deltaRepeat if this is a periodic, the time between
+ *        the runs, otherwise 0.
+ * @param data argument to pass to the method
+ */
+void cron_add_job(CronJob method,
+                 unsigned int delta,
+                 unsigned int deltaRepeat,
+                 void * data);
+
+/**
+ * If the specified cron-job exists in th delta-list, move it to the
+ * head of the list.  If it is running, do nothing.  If it is does not
+ * exist and is not running, add it to the list to run it next.
+ *
+ * @param method which method should we run
+ * @param deltaRepeat if this is a periodic, the time between
+ *        the runs, otherwise 0.
+ * @param data extra argument to calls to method, freed if
+ *        non-null and cron is shutdown before the job is
+ *        run and/or delCronJob is called
+ */
+void cron_advance_job(CronJob method,
+                     unsigned int deltaRepeat,
+                     void * data);
+
+/**
+ * Remove all matching cron-jobs from the list. This method should
+ * only be called while cron is suspended or stopped, or from a cron
+ * job that deletes another cron job.  If cron is not suspended or
+ * stopped, it may be running the method that is to be deleted, which
+ * could be bad (in this case, the deletion will not affect the
+ * running job and may return before the running job has terminated).
+ *
+ * @param method which method is listed?
+ * @param repeat which repeat factor was chosen?
+ * @param data what was the data given to the method
+ * @return the number of jobs removed
+ */
+int cron_del_job(CronJob method,
+                unsigned int repeat,
+                void * data);
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+/* ifndef GNUNET_UTIL_CRON_H */
+#endif
+/* end of gnunet_util_cron.h */

Added: GNUnet/src/include/gnunet_util_disk.h
===================================================================
--- GNUnet/src/include/gnunet_util_disk.h       2006-06-23 15:04:30 UTC (rev 
3026)
+++ GNUnet/src/include/gnunet_util_disk.h       2006-06-23 15:04:37 UTC (rev 
3027)
@@ -0,0 +1,194 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001, 2002, 2003, 2004, 2005, 2006 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
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_util_disk.h
+ * @brief disk IO apis
+ */
+
+#ifndef GNUNET_UTIL_DISK_H
+#define GNUNET_UTIL_DISK_H
+
+#include "gnunet_util_error.h"
+
+/* we need size_t, and since it can be both unsigned int
+   or unsigned long long, this IS platform dependent;
+   but "stdlib.h" should be portable 'enough' to be
+   unconditionally available... */
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/**
+ * Get the number of blocks that are left on the partition that
+ * contains the given file (for normal users).
+ *
+ * @param part a file on the partition to check
+ * @return -1 on errors, otherwise the number of free blocks
+ */
+long disk_get_blocks_available(struct GE_Context * ectx,
+                              const char * part);
+
+/**
+ * Assert that fil corresponds to a filename
+ * (of a file that exists and that is not a directory).
+ * 
+ * @return YES if exists, NO if not a file, SYSERR
+ *   if file does not exist
+ */
+int disk_file_test(struct GE_Context * ectx,
+                  const char * fil);
+
+/**
+ * Get the size of the file (or directory)
+ * of the given file (in bytes).
+ *
+ * @param includeSymLinks should symbolic links be
+ *        included?
+ *
+ * @return OK on success, SYSERR on error
+ */
+int disk_file_size(struct GE_Context * ectx,
+                  const char * filename,
+                  unsigned long long * size,
+                  int includeSymLinks);
+
+/**
+ * Wrapper around "open()".  Opens a file.
+ *
+ * @return file handle, -1 on error
+ */
+int disk_file_open(struct GE_Context * ectx,
+                  const char * filename, 
+                  int oflag, 
+                  ...);
+
+/**
+ * Wrapper around "close()".  Closes a file.
+ */
+void disk_file_close(struct GE_Context * ectx,
+                    const char * filename,
+                    int fd);
+
+/**
+ * Read the contents of a binary file into a buffer.
+ * @param fileName the name of the file, not freed,
+ *        must already be expanded!
+ * @param len the maximum number of bytes to read
+ * @param result the buffer to write the result to
+ * @return the number of bytes read on success, -1 on failure
+ */
+int disk_file_read(struct GE_Context * ectx,
+                  const char * fileName,
+                  int len,
+                  void * result);
+
+/**
+ * Write a buffer to a file.
+ * @param fileName the name of the file, NOT freed!
+ * @param buffer the data to write
+ * @param n number of bytes to write
+ * @param mode the mode for file permissions
+ * @return OK on success, SYSERR on error
+ */
+int disk_file_write(struct GE_Context * ectx,
+                   const char * fileName,
+                   const void * buffer,
+                   unsigned int n,
+                   const char * mode);
+
+/**
+ * Copy a file.
+ * @return OK on success, SYSERR on error
+ */
+int disk_file_copy(struct GE_Context * ectx,
+                  const char * src,
+                  const char * dst);
+
+/**
+ * Function called on each file in a directory.
+ * @return OK to continue to iterate,
+ *  SYSERR to abort iteration with error!
+ */
+typedef int (*DirectoryEntryCallback)(const char * filename,
+                                     const char * dirName,
+                                     void * data);
+
+/**
+ * Scan a directory for files. The name of the directory
+ * must be expanded first (!).
+ *
+ * @param dirName the name of the directory
+ * @param callback the method to call for each file
+ * @param data argument to pass to callback
+ * @return the number of files found, -1 on error
+ */
+int disk_directory_scan(struct GE_Context * ectx,
+                       const char * dirName,
+                       DirectoryEntryCallback callback,
+                       void * data);
+
+/**
+ * Test if fil is a directory that can be accessed.
+ * Will not print an error message if the directory
+ * does not exist.  Will log errors if SYSERR is
+ * returned.
+ *
+ * @return YES if yes, NO if does not exist, SYSERR
+ *   on any error and if exists but not directory
+ */
+int disk_directory_test(struct GE_Context * ectx,
+                       const char * fil);
+
+/**
+ * Remove all files in a directory (rm -rf). Call with
+ * caution.
+ *
+ * @param fileName the file to remove
+ * @return OK on success, SYSERR on error
+ */
+int disk_directory_remove(struct GE_Context * ectx,
+                         const char * fileName);
+
+/**
+ * Implementation of "mkdir -p"
+ *
+ * @param dir the directory to create
+ * @returns SYSERR on failure, OK otherwise
+ */
+int disk_directory_create(struct GE_Context * ectx,
+                         const char * dir);
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+
+/* ifndef GNUNET_UTIL_DISK_H */
+#endif
+/* end of gnunet_util_disk.h */

Added: GNUnet/src/include/gnunet_util_getopt.h
===================================================================
--- GNUnet/src/include/gnunet_util_getopt.h     2006-06-23 15:04:30 UTC (rev 
3026)
+++ GNUnet/src/include/gnunet_util_getopt.h     2006-06-23 15:04:37 UTC (rev 
3027)
@@ -0,0 +1,237 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001, 2002, 2003, 2004, 2005, 2006 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
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_util_getopt.h
+ * @brief command line parsing and --help formatting
+ *
+ * @author Christian Grothoff
+ */
+
+#ifndef GNUNET_UTIL_GETOPT_H
+#define GNUNET_UTIL_GETOPT_H
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+#include "gnunet_util_config.h"
+
+/**
+ * @brief General context for command line processors.
+ */
+typedef struct {
+
+  /**
+   * Name of the application
+   */
+  const char * binaryName;
+  
+  /**
+   * Array with all command line options.
+   */
+  const struct CommandLineOption * allOptions;
+
+  /**
+   * Original command line
+   */
+  const char ** argv;
+
+  /**
+   * Total number of argv's.
+   */
+  unsigned int argc;
+
+  /**
+   * Current argument.
+   */
+  unsigned int currentArgument;
+
+  /**
+   * For logging errors
+   */
+  struct GE_Context * ectx;
+
+  /**
+   * For configuration
+   */
+  struct GC_Configuration * cfg;
+
+} CommandLineProcessorContext;
+
+/**
+ * @brief Process a command line option
+ *
+ * @param ctx context for all options
+ * @param scls specific closure (for this processor)
+ * @param option long name of the option (i.e. "config" for --config)
+ * @param value argument, NULL if none was given 
+ * @return OK to continue processing other options, SYSERR to abort
+ */ 
+typedef int (*CommandLineOptionProcessor)(CommandLineProcessorContext * ctx,
+                                         void * scls,
+                                         const char * option,
+                                         const char * value);
+       
+/**
+ * @brief Definition of a command line option.
+ */
+typedef struct CommandLineOption {
+
+  /**
+   * Short name of the option (use '\0' for none).
+   */
+  const char shortName;
+
+  /**
+   * Long name of the option (may not be NULL)
+   */
+  const char * name;
+
+  /**
+   * Name of the argument for the user in help text
+   */
+  const char * argumentHelp;
+
+  /**
+   * Help text for the option (description)
+   */
+  const char * description;
+
+  /**
+   * Is an argument required?  0: NO (includes optional), 1: YES.
+   */
+  int require_argument;
+
+  /**
+   * Handler for the option.
+   */ 
+  CommandLineOptionProcessor processor;
+  
+  /**
+   * Specific closure to pass to the processor.
+   */
+  void * scls;
+
+} CommandLineOption;
+
+/**
+ * Macro defining the option to print the command line
+ * help text.  
+ *
+ * @param about string with brief description of the application
+ */
+#define COMMAND_LINE_OPTION_HELP(about) \
+  { 'h', "help", NULL, gettext_noop("print this help"), 0, 
&gnunet_getopt_format_help, about }
+
+/**
+ * Macro defining the option to print the version of
+ * the application
+ *
+ * @param version string with the version number
+ */
+#define COMMAND_LINE_OPTION_VERSION(version) \
+  { 'v', "version", NULL, gettext_noop("print the version number"), 0, 
&gnunet_getopt_print_version, version }
+  
+/**
+ * Set the configuration option for logging.
+ */
+#define COMMAND_LINE_OPTION_LOGGING \
+  { 'L', "log", "LOGRULES", gettext_noop("configure logging to use LOGRULES"), 
1, &gnunet_getopt_configure_set_option, "GNUNET:LOGGING" }
+
+/**
+ * Set the configuration option for increasing verbosity.
+ */
+#define COMMAND_LINE_OPTION_VERBOSE \
+  { 'V', "verbose", NULL, gettext_noop("be verbose"), 1, 
&gnunet_getopt_configure_increment_value, "1" }
+
+/**
+ * Set the configuration option for the configuration file.
+ */
+#define COMMAND_LINE_OPTION_CFG_FILE \
+  { 'c', "config", "FILENAME", gettext_noop("use configuration file 
FILENAME"), 1, &gnunet_getopt_configure_set_option, "GNUNET:CONFIGFILE" }
+
+/**
+ * Set the configuration option for the configuration file.
+ */
+#define COMMAND_LINE_OPTION_HOSTNAME \
+  { 'H', "host", "HOSTNAME", gettext_noop("specify host on which gnunetd is 
running"), 1, &gnunet_getopt_configure_set_option, "GNUNET:HOSTNAME" }
+
+/**
+ * Marker to end the list of options.
+ */
+#define COMMAND_LINE_OPTION_END \
+  { '\0', NULL, NULL, 0, NULL, NULL }
+
+/**
+ * Parse the command line.
+ *
+ * @param binaryName name of the binary / application
+ * @param ectx for reporting errors
+ * @param cfg for storing/accessing configuration data
+ * @param allOptions defined options and handlers
+ * @param argc number of arguments 
+ * @param argv actual arguments
+ * @return index into argv with first non-option
+ *   argument, or -1 on error
+ */
+int gnunet_parse_options(const char * binaryName,
+                        struct GE_Context * ectx,
+                        struct GC_Configuration * cfg,
+                        const CommandLineOption * allOptions,
+                        unsigned int argc,
+                        const char ** argv);
+
+int gnunet_getopt_configure_set_option(CommandLineProcessorContext * ctx,
+                                      void * scls,
+                                      const char * option,
+                                      const char * value);
+
+int gnunet_getopt_configure_increment_value(CommandLineProcessorContext * ctx,
+                                           void * scls,
+                                           const char * option,
+                                           const char * value);
+
+/* *************** internal prototypes - use macros above! ************* */
+
+int gnunet_getopt_format_help(CommandLineProcessorContext * ctx,
+                             void * scls,
+                             const char * option,
+                             const char * value);
+
+int gnunet_getopt_print_version(CommandLineProcessorContext * ctx,
+                               void * scls,
+                               const char * option,
+                               const char * value);
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+
+/* ifndef GNUNET_UTIL_GETOPT_H */
+#endif
+/* end of gnunet_util_getopt.h */

Added: GNUnet/src/include/gnunet_util_network.h
===================================================================
--- GNUnet/src/include/gnunet_util_network.h    2006-06-23 15:04:30 UTC (rev 
3026)
+++ GNUnet/src/include/gnunet_util_network.h    2006-06-23 15:04:37 UTC (rev 
3027)
@@ -0,0 +1,482 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001, 2002, 2003, 2004, 2005, 2006 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
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_util_network.h
+ * @brief networking interface to libgnunetutil
+ *
+ * @author Christian Grothoff
+ * @author Krista Bennett
+ * @author Gerd Knorr <address@hidden>
+ * @author Ioana Patrascu
+ * @author Tzvetan Horozov
+ *
+ *
+ * TODO:
+ * - add configuration argument to 
+ *   methods that need it
+ * - abstract sockets that are
+ *   represented as "int" 
+ *   (typedef int GNUNET_SOCKET or so) 
+ * - 
+ */
+
+#ifndef GNUNET_UTIL_NETWORK_H
+#define GNUNET_UTIL_NETWORK_H
+
+/* we need size_t, and since it can be both unsigned int
+   or unsigned long long, this IS platform dependent;
+   but "stdlib.h" should be portable 'enough' to be
+   unconditionally available... */
+#include <stdlib.h>
+
+/* add error and config prototypes */
+#include "gnunet_util_config.h"
+#include "gnunet_util_string.h"
+#include "gnunet_util_os.h"
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/**
+ * We use an unsigned short in the protocol header, thus:
+ */
+#define MAX_BUFFER_SIZE 65536
+
+/**
+ * @brief Specify low-level network IO behavior
+ */
+typedef enum {
+  /**
+   * Do not block.
+   */
+  NC_Nonblocking = 0x000,
+  /**
+   * Call may block.
+   */
+  NC_Blocking    = 0x001,
+  /**
+   * Ignore interrupts (re-try if operation
+   * was aborted due to interrupt)
+   */
+  NC_IgnoreInt   = 0x010,
+  /**
+   * Always try to read/write the maximum
+   * amount of data (using possibly multiple
+   * calls).  Only return on non-interrupt
+   * error or if completely done.
+   */
+  NC_Complete    = 0x111,
+} NC_KIND;
+
+/**
+ * @brief 512-bit hashcode
+ */
+typedef struct {
+  unsigned int bits[512 / 8 / sizeof(unsigned int)]; /* = 16 */
+} HashCode512;
+
+/**
+ * The identity of the host (basically the SHA-512 hashcode of
+ * it's public key).
+ */
+typedef struct {
+  HashCode512 hashPubKey;
+} PeerIdentity;
+
+/**
+ * Header for all Client-Server communications.
+ */
+typedef struct {
+
+  /**
+   * The length of the struct (in bytes, including the length field itself)
+   */
+  unsigned short size;
+
+  /**
+   * The type of the message (XX_CS_PROTO_XXXX)
+   */
+  unsigned short type;
+
+} MESSAGE_HEADER;
+
+/**
+ * @brief an IPv4 address
+ */
+typedef struct {
+  unsigned int addr; /* struct in_addr */
+} IPaddr;
+
+/**
+ * @brief IPV4 network in CIDR notation.
+ */
+struct CIDRNetwork;
+
+/**
+ * @brief an IPV6 address.
+ */
+typedef struct {
+  unsigned int addr[4]; /* struct in6_addr addr; */
+} IP6addr;
+
+/**
+ * @brief IPV6 network in CIDR notation.
+ */
+struct CIDR6Network;
+
+struct ClientServerConnection;
+
+struct SocketHandle;
+
+/* *********************** endianess conversion ************* */
+
+/**
+ * Convert a long-long to host-byte-order.
+ * @param n the value in network byte order
+ * @return the same value in host byte order
+ */
+unsigned long long ntohll(unsigned long long n);
+
+/**
+ * Convert a long long to network-byte-order.
+ * @param n the value in host byte order
+ * @return the same value in network byte order
+ */
+unsigned long long htonll(unsigned long long n);
+
+/* ***************** basic parsing **************** */
+
+/**
+ * Parse a network specification. The argument specifies
+ * a list of networks. The format is
+ * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
+ * with a semicolon). The network must be given in dotted-decimal
+ * notation. The netmask can be given in CIDR notation (/16) or
+ * in dotted-decimal (/255.255.0.0).
+ * <p>
+ * @param routeList a string specifying the forbidden networks
+ * @return the converted list, NULL if the synatx is flawed
+ */
+struct CIDRNetwork * 
+parse_ipv4_network_specification(struct GE_Context * ectx,
+                                const char * routeList);
+
+/**
+ * Parse a network specification. The argument specifies
+ * a list of networks. The format is
+ * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
+ * with a semicolon). The network must be given in dotted-decimal
+ * notation. The netmask can be given in CIDR notation (/16) or
+ * in dotted-decimal (/255.255.0.0).
+ * <p>
+ * @param routeList a string specifying the forbidden networks
+ * @return the converted list, NULL if the synatx is flawed
+ */
+struct CIDR6Network * 
+parse_ipv6_network_specification(struct GE_Context * ectx,
+                                const char * routeList);
+
+/**
+ * Check if the given IP address is in the list of
+ * IP addresses.
+ * @param list a list of networks
+ * @param ip the IP to check (in network byte order)
+ * @return NO if the IP is not in the list, YES if it it is
+ */
+int check_ipv4_listed(const struct CIDRNetwork * list,
+                     IPaddr ip);
+
+/**
+ * Check if the given IP address is in the list of
+ * IP addresses.
+ * @param list a list of networks
+ * @param ip the IP to check (in network byte order)
+ * @return NO if the IP is not in the list, YES if it it is
+ */
+int check_ipv6_listed(const struct CIDR6Network * list,
+                     IP6addr ip);
+
+#define PRIP(ip) (unsigned int)(((unsigned int)(ip))>>24), (unsigned 
int)((((unsigned)(ip)))>>16 & 255), (unsigned int)((((unsigned int)(ip)))>>8 & 
255), (unsigned int)((((unsigned int)(ip))) & 255)
+
+/**
+ * Get the IP address of the given host.
+ *
+ * @return OK on success, SYSERR on error
+ */
+int get_host_by_name(struct GE_Context * ectx,
+                    const char * hostname,
+                    IPaddr * ip);
+
+/* ***************** high-level GNUnet client-server connections *********** */
+
+/**
+ * Get a connection with gnunetd.
+ */
+struct ClientServerConnection * 
+daemon_connection_create(struct GE_Context * ectx,
+                        struct GC_Configuration * cfg);
+
+/**
+ * Initialize a GNUnet server socket.
+ * @param sock the open socket
+ * @param result the SOCKET (filled in)
+ * @return OK (always successful)
+ */
+struct ClientServerConnection * 
+client_connection_create(struct GE_Context * ectx,
+                        struct GC_Configuration * cfg,
+                        SocketHandle sock);
+
+/**
+ * Close a GNUnet TCP socket for now (use to temporarily close
+ * a TCP connection that will probably not be used for a long
+ * time; the socket will still be auto-reopened by the
+ * readFromSocket/writeToSocket methods if it is a client-socket).
+ *
+ * Also, you must still call connection_destroy to free all
+ * resources associated with the connection.
+ */
+void connection_close_temporarily(struct ClientServerConnection * sock);
+
+/**
+ * Destroy connection between gnunetd and clients.
+ * Also closes the connection if it is still active.
+ */
+void connection_destroy(struct ClientServerConnection * con);
+
+/**
+ * Check if a socket is open. Will ALWAYS return 'true' for a valid
+ * client socket (even if the connection is closed), but will return
+ * false for a closed server socket.
+ *
+ * @return 1 if open, 0 if closed
+ */
+int connection_test_open(struct ClientServerConnection * sock);
+
+/**
+ * Check a socket, open and connect if it is closed and it is a
+ * client-socket.
+ *
+ * @return OK if the socket is now open, SYSERR if not
+ */
+int connection_ensure_connected(struct ClientServerConnection * sock);
+
+/**
+ * Read from a GNUnet client-server connection.
+ *
+ * @param sock the socket
+ * @param buffer the buffer to write data to
+ *        if NULL == *buffer, *buffer is allocated (caller frees)
+ * @return OK if the read was successful, SYSERR if the socket
+ *         was closed by the other side (if the socket is a
+ *         client socket and is used again, the next
+ *         read/write call will automatically attempt
+ *         to re-establish the connection).
+ */
+int connection_read(struct ClientServerConnection * sock,
+                   MESSAGE_HEADER ** buffer);
+
+/**
+ * Write to a GNUnet TCP socket.
+ *
+ * @param sock the socket to write to
+ * @param buffer the buffer to write
+ * @param isBlocking is this call allowed to block
+ * @return OK if the write was sucessful, 
+ *         NO if it would block and isBlocking was NO,
+ *         SYSERR if the write failed (error will be logged)
+ */
+int connection_write(struct ClientServerConnection * sock,
+                    const CS_MESSAGE_HEADER * buffer,
+                    int isBlocking);
+
+
+/**
+ * Obtain a simple return value from the connection.
+ * Note that the protocol will automatically communicate
+ * errors and pass those to the error context used when
+ * the socket was created.  In that case, read_result
+ * will return SYSERR for the corresponding communication.
+ * 
+ * @param sock the TCP socket
+ * @param ret the return value from TCP
+ * @return SYSERR on error, OK if the return value was
+ *         read successfully
+ */
+int connection_read_result(struct ClientServerConnection * sock,
+                          int * ret);
+
+/**
+ * Send a simple return value to the other side.
+ *
+ * @param sock the TCP socket
+ * @param ret the return value to send via TCP
+ * @return SYSERR on error, OK if the return value was
+ *         send successfully
+ */
+int connection_write_result(struct ClientServerConnection * sock,
+                           int ret);
+
+/**
+ * Stop gnunetd
+ *
+ * Note that returning an error does NOT mean that
+ * gnunetd will continue to run (it may have been
+ * shutdown by something else in the meantime or
+ * crashed).  Call connection_test_running() frequently
+ * to check the status of gnunetd.
+ *
+ * Furthermore, note that this WILL potentially kill
+ * gnunetd processes on remote machines that cannot
+ * be restarted with startGNUnetDaemon!
+ *
+ * This function does NOT need the PID and will also
+ * kill daemonized gnunetd's.
+ *
+ * @return OK successfully stopped, SYSERR: error
+ */
+int connection_request_shutdown(struct ClientServerConnection * sock);
+
+/**
+ * Checks if gnunetd is running
+ *
+ * Uses CS_PROTO_traffic_COUNT query to determine if gnunetd is
+ * running.
+ *
+ * @return OK if gnunetd is running, SYSERR if not
+ */
+int connection_test_running(struct GE_Context * ectx);
+
+/**
+ * Wait until the gnunet daemon is
+ * running.
+ *
+ * @param timeout how long to wait at most in ms
+ * @return OK if gnunetd is now running
+ */
+int connection_wait_for_running(struct GE_Context * ectx,
+                               unsigned long long_t timeout);
+
+/**
+ * Send a return value that indicates
+ * a serious error to the other side.
+ *
+ * @param sock the TCP socket
+ * @param mask GE_MASK 
+ * @param date date string
+ * @param msg message string
+ * @return SYSERR on error, OK if the error code was send
+ *         successfully
+ */
+int connection_write_error(struct ClientServerConnection * sock,
+                          GE_KIND mask,
+                          const char * date,
+                          const char * msg);
+
+/* ********************* low-level socket operations **************** */
+
+/**
+ * Create a socket handle by boxing an OS socket.
+ * The OS socket should henceforth be no longer used
+ * directly.  socket_destroy will close it.
+ */
+struct SocketHandle * 
+socket_create(struct GE_Context * ectx,
+             struct LoadMonitor * mon,
+             int osSocket);
+
+void socket_destroy(struct SocketHandle * s);
+
+/**
+ * Depending on doBlock, enable or disable the nonblocking mode
+ * of socket s.
+ *
+ * @return Upon successful completion, it returns zero.
+ * @return Otherwise -1 is returned.
+ */
+int socket_set_blocking(struct SocketHandle * s, 
+                       int doBlock);
+
+/**
+ * Check whether the socket is blocking
+ * @param s the socket
+ * @return YES if blocking, NO non-blocking
+ */
+int socket_test_blocking(struct SocketHandle * s);
+
+/**
+ * Do a read on the given socket.  
+ *
+ * @brief reads at most max bytes to buf. Interrupts are IGNORED.
+ * @param s socket
+ * @param nc
+ * @param buf buffer
+ * @param max maximum number of bytes to read
+ * @param read number of bytes actually read.
+ *             0 is returned if no more bytes can be read
+ * @return SYSERR on error, YES on success or NO if the operation
+ *         would have blocked
+ */
+int socket_recv(struct SocketHandle * s,
+               NC_KIND nc,
+               void * buf,
+               size_t max,
+               size_t * read);
+
+/**
+ * Do a write on the given socket.
+ * Write at most max bytes from buf.
+ *
+ * @param s socket
+ * @param buf buffer to send
+ * @param max maximum number of bytes to send
+ * @param sent number of bytes actually sent
+ * @return SYSERR on error, YES on success or
+ *         NO if the operation would have blocked.
+ */
+int socket_send(struct SocketHandle * s,
+               NC_KIND nc,
+               const void * buf,
+               size_t max,
+               size_t * sent);
+
+/**
+ * Check if socket is valid
+ * @return YES if valid, NO otherwise
+ */
+int socket_test_valid(struct SocketHandle * s);
+
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+
+/* ifndef GNUNET_UTIL_NETWORK_H */
+#endif
+/* end of gnunet_util_network.h */

Added: GNUnet/src/include/gnunet_util_os.h
===================================================================
--- GNUnet/src/include/gnunet_util_os.h 2006-06-23 15:04:30 UTC (rev 3026)
+++ GNUnet/src/include/gnunet_util_os.h 2006-06-23 15:04:37 UTC (rev 3027)
@@ -0,0 +1,231 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001, 2002, 2003, 2004, 2005, 2006 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
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_util_os.h
+ * @brief low level process routines (fork, IPC, 
+ *        OS statistics, OS properties)
+ * @author Christian Grothoff
+ * @author Krista Bennett
+ * @author Gerd Knorr <address@hidden>
+ * @author Ioana Patrascu
+ * @author Tzvetan Horozov
+ */
+
+#ifndef GNUNET_UTIL_OS_H
+#define GNUNET_UTIL_OS_H
+
+/* add error and config prototypes */
+#include "gnunet_util_config.h"
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/**
+ * @brief Inter-process semaphore.
+ */
+struct IPC_SEMAPHORE;
+
+/**
+ * @brief plugin (shared library) handle
+ */ 
+struct PluginHandle;
+
+/**
+ * @param isDefault is this presumably the default interface
+ * @return OK to continue iteration, SYSERR to abort
+ */
+typedef int (*NetworkIfcProcessor)(const char * name,
+                                  int isDefault,
+                                  void * cls);
+
+typedef enum {
+  Download,
+  Upload,
+} NetworkDirection;
+
+struct LoadMonitor;
+
+struct IPC_SEMAPHORE * 
+IPC_SEMAPHORE_CREATE(struct GE_Context * ectx,
+                    const char * basename,
+                    unsigned int initialValue);
+
+void IPC_SEMAPHORE_DESTROY(struct IPC_Semaphore * sem);
+
+void IPC_SEMAPHORE_UP(struct IPC_SEMAPHORE * sem);
+
+void IPC_SEMAPHORE_DOWN(struct IPC_SEMAPHORE * sem,
+                       int mayBlock);
+
+/**
+ * Load plugin
+ */
+struct PluginHandle * 
+os_plugin_load(struct GE_Context * ectx,
+              const char * libprefix,
+              const char * dsoname);
+
+/**
+ * Try resolving a function provided by the plugin
+ * @param logError YES if failure to find the function
+ *        is an error that should be logged
+ * @param methodprefix prefix for the method; the
+ *        method name will be automatically extended
+ *        with the respective dsoname of the plugin
+ * @return NULL on error, otherwise pointer to the function
+ */
+void * 
+os_plugin_resolve_function(struct PluginHandle * plugin,                       
+                          const char * methodprefix,
+                          int logError);
+
+void os_plugin_unload(struct PluginHandle * plugin);
+
+struct LoadMonitor * 
+os_network_monitor_create(struct GE_Context * ectx,
+                      struct GC_Configuration * cfg);
+
+void os_network_monitor_destroy(struct LoadMonitor * mon);
+
+/**
+ * Get the load of the network relative to what is allowed.
+ *
+ * @return the network load as a percentage of allowed
+ *        (100 is equivalent to full load)
+ */
+int os_network_monitor_get_load(struct LoadMonitor * monitor,
+                            NetworkDirection dir);
+
+/**
+ * Tell monitor to increment the number of bytes sent/received
+ */
+void os_network_monitor_notify_transmission(NetworkDirection dir,
+                                           unsigned long long delta);
+
+/**
+ * @brief Enumerate all network interfaces
+ * @param callback the callback function
+ */
+void os_list_network_interfaces(struct GE_Context * ectx,
+                               NetworkIfcProcessor proc,
+                               void * cls);
+
+/**
+ * @brief Make "application" start automatically
+ *
+ * @param testCapability YES to merely probe if the OS has this
+ *        functionality (in that case, no actual operation is
+ *        performed).  SYSERR is returned if
+ *        a) autostart is not supported,
+ *        b) the application does not seem to exist
+ *        c) the user or group do not exist
+ *        d) the user has insufficient permissions for 
+ *           changing autostart
+ *        e) doAutoStart is NO, but autostart is already
+ *           disabled
+ *        f) doAutoStart is YES, but autostart is already
+ *           enabled
+ * @param doAutoStart YES to enable autostart of the
+ *        application, NO to disable it
+ * @param username name of the user account to use
+ * @param groupname name of the group to use
+ * @return OK on success, SYSERR on error
+ */
+int os_modify_autostart(struct GE_Context * ectx,
+                       int testCapability,
+                       int doAutoStart,
+                       const char * application
+                       const char * username, 
+                       const char * groupname);
+
+/**
+ * @brief Add or remove a service account for GNUnet
+ *
+ * @param testCapability YES to merely probe if the OS has this
+ *        functionality (in that case, no actual operation is
+ *        performed).  SYSERR is returned if
+ *        a) adding users is not supported,
+ *        b) the user has insufficient permissions for 
+ *           adding/removing users
+ *        c) doAdd is NO, but user does not exist
+ *        d) doAdd is YES, and user already exists
+ * @param name the name of the new user or group
+ * @param doAdd YES to add, NO to remove user, SYSERR to
+ *        purge (removes user AND group)
+ * @param name the name of the user
+ * @param group name of the group
+ * @return OK on success, SYSERR on error
+ */
+int os_modify_user(struct GE_Context * ectx,
+                  int doAdd,
+                  const char * name,
+                  const char * group);
+
+/**
+ * Get the current CPU load.
+ * @param ectx for error reporting
+ * @param cfg to determine acceptable load level (LOAD::MAXCPULOAD)
+ * @return -1 on error, otherwise load value 
+ */
+int os_cpu_get_load(struct GE_Context * ectx,
+                   struct GC_Configuration * cfg);
+
+/**
+ * Start gnunetd process
+ *
+ * @param cfgFile configuration file to use, NULL for default
+ * @param daemonize YES if gnunetd should be daemonized
+ * @return pid_t of gnunetd if NOT daemonized, 0 if
+ *  daemonized sucessfully, -1 on error
+ */
+int os_daemon_start(struct GE_Context * ectx,
+                   const char * cfgFile,
+                   int daemonize);
+
+/**
+ * Wait until the gnunet daemon (or any other CHILD process for that
+ * matter) with the given PID has terminated.  Assumes that
+ * the daemon was started with daemon_start in no-daemonize mode.
+ * On arbitrary PIDs, this function may fail unexpectedly.
+ *
+ * @return YES if gnunetd shutdown with
+ *  return value 0, SYSERR if waitpid
+ *  failed, NO if gnunetd shutdown with
+ *  some error
+ */
+int os_daemon_stop(struct GE_Context * ectx,
+                  int pid);
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+
+/* ifndef GNUNET_UTIL_OS_H */
+#endif
+/* end of gnunet_util_os.h */

Added: GNUnet/src/include/gnunet_util_string.h
===================================================================
--- GNUnet/src/include/gnunet_util_string.h     2006-06-23 15:04:30 UTC (rev 
3026)
+++ GNUnet/src/include/gnunet_util_string.h     2006-06-23 15:04:37 UTC (rev 
3027)
@@ -0,0 +1,311 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001, 2002, 2003, 2004, 2005, 2006 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
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_util_string.h
+ * @brief string handling functions (including malloc,
+ *        command line parsing and --help formatting)
+ *
+ * @author Christian Grothoff
+ * @author Krista Bennett
+ * @author Gerd Knorr <address@hidden>
+ * @author Ioana Patrascu
+ * @author Tzvetan Horozov
+ */
+
+#ifndef GNUNET_UTIL_STRING_H
+#define GNUNET_UTIL_STRING_H
+
+/* we need size_t, and since it can be both unsigned int
+   or unsigned long long, this IS platform dependent;
+   but "stdlib.h" should be portable 'enough' to be
+   unconditionally available... */
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+#include "gnunet_util_error.h"
+#include "gnunet_util_config.h"
+
+/**
+ * Wrapper around malloc. Allocates size bytes of memory.
+ *
+ * @param size the number of bytes to allocate, must be
+ *        smaller than 40 MB.
+ * @return pointer to size bytes of memory
+ */
+#define MALLOC(size) xmalloc_(size, __FILE__,__LINE__,__FUNCTION__)
+
+/**
+ * Wrapper around malloc. Allocates size bytes of memory.
+ *
+ * @param size the number of bytes to allocate
+ * @return pointer to size bytes of memory
+ */
+#define MALLOC_LARGE(size) xmalloc_unchecked_(size, 
__FILE__,__LINE__,__FUNCTION__)
+
+/**
+ * Wrapper around realloc. Rellocates size bytes of memory.
+ *
+ * @param ptr the pointer to reallocate
+ * @param size the number of bytes to reallocate
+ * @return pointer to size bytes of memory
+ */
+#define REALLOC(ptr, size) xrealloc_(ptr, size, __FILE__,__LINE__,__FUNCTION__)
+
+/**
+ * Wrapper around free. Frees the memory referred to by ptr.
+ * Note that is is generally better to free memory that was
+ * allocated with GROW using GROW(mem, size, 0) instead of FREE.
+ *
+ * @param ptr location where to free the memory. ptr must have
+ *     been returned by STRDUP, MALLOC or GROW earlier.
+ */
+#define FREE(ptr) xfree_(ptr, __FILE__, __LINE__,__FUNCTION__)
+
+/**
+ * Free the memory pointed to by ptr if ptr is not NULL.
+ * Equivalent to if (ptr!=null)FREE(ptr).
+ *
+ * @param ptr the location in memory to free
+ */
+#define FREENONNULL(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { 
FREE(__x__); } } while(0)
+
+/**
+ * Wrapper around STRDUP.  Makes a copy of the zero-terminated string
+ * pointed to by a.
+ *
+ * @param a pointer to a zero-terminated string
+ * @return a copy of the string including zero-termination
+ */
+#define STRDUP(a) xstrdup_(a,__FILE__,__LINE__, __FUNCTION__)
+
+/**
+ * Wrapper around STRNDUP.  Makes a copy of the zero-terminated string
+ * pointed to by a.
+ *
+ * @param a pointer to a zero-terminated string
+ * @param n the maximum number of characters to copy (+1 for 0-termination)
+ * @return a copy of the string including zero-termination
+ */
+#define STRNDUP(a,n) xstrndup_(a,n,__FILE__,__LINE__,__FUNCTION__)
+
+/**
+ * Grow a well-typed (!) array.  This is a convenience
+ * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
+ * to the new (target) size <tt>tsize</tt>.
+ * <p>
+ *
+ * Example (simple, well-typed stack):
+ *
+ * <pre>
+ * static struct foo * myVector = NULL;
+ * static int myVecLen = 0;
+ *
+ * static void push(struct foo * elem) {
+ *   GROW(myVector, myVecLen, myVecLen+1);
+ *   memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
+ * }
+ *
+ * static void pop(struct foo * elem) {
+ *   if (myVecLen == 0) die();
+ *   memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
+ *   GROW(myVector, myVecLen, myVecLen-1);
+ * }
+ * </pre>
+ *
+ * @param arr base-pointer of the vector, may be NULL if size is 0;
+ *        will be updated to reflect the new address. The TYPE of
+ *        arr is important since size is the number of elements and
+ *        not the size in bytes
+ * @param size the number of elements in the existing vector (number
+ *        of elements to copy over)
+ * @param tsize the target size for the resulting vector, use 0 to
+ *        free the vector (then, arr will be NULL afterwards).
+ */
+#define GROW(arr,size,tsize) xgrow_((void**)&arr, sizeof(arr[0]), &size, 
tsize, __FILE__, __LINE__, __FUNCTION__)
+
+/**
+ * Append an element to a list (growing the 
+ * list by one).
+ */
+#define APPEND(arr,size,element) GROW(arr,size,size+1); arr[size-1] = (element)
+
+/**
+ * Like snprintf, just aborts if the buffer is of insufficient size.
+ */
+int SNPRINTF(char * buf,
+            size_t size,
+            const char * format,
+            ...);
+
+/**
+ * Give relative time in human-readable fancy format.
+ */
+char * string_get_fancy_time_interval(unsigned long long delta);
+
+/**
+ * Convert a given filesize into a fancy human-readable format.
+ */
+char * strng_get_fancy_byte_size(unsigned long long size);
+
+/**
+ * Convert the len characters long character sequence
+ * given in input that is in the given charset
+ * to UTF-8.
+ *
+ * @return the converted string (0-terminated)
+ */
+char * string_convertToUtf8(struct GE_Context * ectx,
+                           const char * input,
+                           size_t len,
+                           const char * charset);
+
+/**
+ * Complete filename (a la shell) from abbrevition.
+ *
+ * @param fil the name of the file, may contain ~/ or
+ *        be relative to the current directory
+ * @returns the full file name,
+ *          NULL is returned on error
+ */
+char * string_expandFileName(struct GE_Context * ectx,
+                            const char * fil);
+
+/**
+ * String functions
+ */
+#if !HAVE_STRLCPY
+size_t strlcpy(char *dest, 
+              const char *src, 
+              size_t size);
+#endif
+
+#if !HAVE_STRLCAT
+size_t strlcat(char *dest,
+              const char *src,
+              size_t count);
+#endif
+
+
+/* ************** internal implementations, use macros above! ************** */
+
+/**
+ * Allocate memory. Checks the return value, aborts if no more
+ * memory is available.  Don't use xmalloc_ directly. Use the
+ * MALLOC macro.
+ */
+void * xmalloc_(size_t size,
+               const char * filename,
+               int linenumber,
+               const char * function);
+
+/**
+ * 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 MALLOC instead.
+ */
+void * xmalloc_unchecked_(size_t size,
+                         const char * filename,
+                         int linenumber,
+                         const char * function);
+
+/**
+ * Reallocate memory. Checks the return value, aborts if no more
+ * memory is available.
+ */
+void * xrealloc_(void * ptr,
+                const size_t n,
+                const char * filename,
+                int linenumber,
+                const char * function);
+
+/**
+ * Free memory. Merely a wrapper for the case that we
+ * want to keep track of allocations.  Don't use xfree_
+ * directly. Use the FREE macro.
+ */
+void xfree_(void * ptr,
+           const char * filename,
+           int linenumber,
+           const char * function);
+
+
+/**
+ * Dup a string. Don't call xstrdup_ directly. Use the STRDUP macro.
+ */
+char * xstrdup_(const char * str,
+               const char * filename,
+               int linenumber,
+               const char * function);
+
+/**
+ * Dup a string. Don't call xstrdup_ directly. Use the STRDUP macro.
+ *
+ * @param str the string to dup
+ * @param n the maximum number of characters to copy (+1 for 0-termination)
+ * @param filename where in the code was the call to GROW
+ * @param linenumber where in the code was the call to GROW
+ * @return strdup(str)
+ */
+char * xstrndup_(const char * str,
+                const size_t n,
+                const char * filename,
+                int linenumber,
+                const char * function);
+
+/**
+ * Grow an array, the new elements are zeroed out.
+ * Grows old by (*oldCount-newCount)*elementSize
+ * bytes and sets *oldCount to newCount.
+ *
+ * Don't call xgrow_ directly. Use the GROW macro.
+ *
+ * @param old address of the pointer to the array
+ *        *old may be NULL
+ * @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)
+ */
+void xgrow_(void ** old,
+           size_t elementSize,
+           unsigned int * oldCount,
+           unsigned int newCount,
+           const char * filename,
+           int linenumber,
+           const char * function);
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+
+/* ifndef GNUNET_UTIL_STRING_H */
+#endif
+/* end of gnunet_util_string.h */

Added: GNUnet/src/include/gnunet_util_threads.h
===================================================================
--- GNUnet/src/include/gnunet_util_threads.h    2006-06-23 15:04:30 UTC (rev 
3026)
+++ GNUnet/src/include/gnunet_util_threads.h    2006-06-23 15:04:37 UTC (rev 
3027)
@@ -0,0 +1,179 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001, 2002, 2003, 2004, 2005, 2006 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
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_util_threads.h
+ * @brief pthreads wapper and thread related services
+ *
+ * @author Christian Grothoff
+ * @author Krista Bennett
+ * @author Gerd Knorr <address@hidden>
+ * @author Ioana Patrascu
+ * @author Tzvetan Horozov
+ *
+ * TODO:
+ * - consider moving SHUTDOWN into
+ *   seperate shared library (not part of
+ *   libgnunetutil binary at all!)
+ */
+
+#ifndef GNUNET_UTIL_THREADS_H
+#define GNUNET_UTIL_THREADS_H
+
+#ifdef __cplusplus
+extern "C" {
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/**
+ * Main method of a thread.
+ */
+typedef void * (*PThreadMain)(void*);
+
+/**
+ * @brief Encapsulation of a pthread handle.
+ */
+struct PTHREAD;
+
+/**
+ * @brief Structure for MUTual EXclusion (Mutex).
+ */
+struct MUTEX;
+
+/**
+ * @brief semaphore abstraction (for pthreads)
+ */
+struct SEMAPHORE;
+
+/**
+ * Returns YES if pt is the handle for THIS thread.
+ */
+int PTHREAD_TEST_SELF(struct PTHREAD * pt);
+
+/**
+ * Get the handle for THIS thread.
+ */
+struct PTHREAD * PTHREAD_GET_SELF();
+
+/**
+ * Release handle for a thread (should have been
+ * obtained using PTHREAD_GET_SELF).
+ */
+void PTHREAD_REL_SELF(struct PTHREAD * pt);
+
+/**
+ * Create a thread. Use this method instead of pthread_create since
+ * BSD may only give a 1k stack otherwise.
+ *
+ * @param main the main method of the thread
+ * @param arg the argument to main
+ * @param stackSize the size of the stack of the thread in bytes.
+ *        Note that if the stack overflows, some OSes (seen under BSD)
+ *        will just segfault and gdb will give a messed-up stacktrace.
+ * @return the handle
+ */
+struct PTHREAD * PTHREAD_CREATE(PThreadMain main,
+                               void * arg,
+                               unsigned int stackSize);
+
+/**
+ * Wait for the other thread to terminate.  May only be called
+ * once per created thread, the handle is afterwards invalid.
+ *
+ * @param ret set to the return value of the other thread.
+ */
+void PTHREAD_JOIN(struct PTHREAD * handle,
+                 void ** ret);
+
+/**
+ * Sleep for the specified time interval.  PTHREAD_STOP_SLEEP can be
+ * used to interrupt the sleep.  Caller is responsible to check that
+ * the sleep was long enough.
+ *
+ * @param time how long to sleep (in milli seconds)
+ */
+void PTHREAD_SLEEP(unsigned long long time);
+
+/**
+ * Stop the sleep of anothe thread.
+ */
+void PTHREAD_STOP_SLEEP(struct PTHREAD * handle);
+
+struct MUTEX * MUTEX_CREATE(int isRecursive);
+
+void MUTEX_DESTROY(struct MUTEX * mutex);
+
+void MUTEX_LOCK(struct MUTEX * mutex);
+
+void MUTEX_UNLOCK(struct MUTEX * mutex);
+
+struct SEMAPHORE * SEMAPHORE_CREATE(int value);
+
+void SEMAPHORE_DESTROY(struct SEMAPHORE * sem);
+
+/**
+ * @param block set to NO to never block (and
+ *        thus fail if semaphore counter is 0)
+ * @return SYSERR if would block, otherwise
+ *  new count value after change 
+ */
+int SEMAPHORE_DOWN(struct SEMAPHORE * sem,
+                  int mayblock);
+
+/**
+ * function increments the semaphore and signals any threads that
+ * are blocked waiting a change in the semaphore.
+ *
+ * @return new count value of the semaphore after increment
+ */
+int SEMAPHORE_UP(struct SEMAPHORE * sem);
+
+/**
+ * Programatically shutdown the application.
+ */
+void GNUNET_SHUTDOWN_INIITATE(void);
+
+/**
+ * Test if the shutdown has been initiated.
+ *
+ * @return YES if we are shutting down, NO otherwise
+ */
+int GNUNET_SHUTDOWN_TEST(void);
+
+/**
+ * Wait until the shutdown has been initiated.  This
+ * should be called by the main thread (if it has 
+ * nothing better to do) to wait for a user signal
+ * (or other thread) to initiate the shutdown.
+ */
+void GNUNET_SHUTDOWN_WAITFOR(void);
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+/* ifndef GNUNET_UTIL_THREADS_H */
+#endif
+/* end of gnunet_util_threads.h */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]