gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r3026 - in GNUnet/src/util: . network os


From: grothoff
Subject: [GNUnet-SVN] r3026 - in GNUnet/src/util: . network os
Date: Fri, 23 Jun 2006 08:04:33 -0700 (PDT)

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

Modified:
   GNUnet/src/util/README
   GNUnet/src/util/network/tcpio.c
   GNUnet/src/util/os/dso.c
Log:
more refactoring

Modified: GNUnet/src/util/README
===================================================================
--- GNUnet/src/util/README      2006-06-22 18:45:40 UTC (rev 3025)
+++ GNUnet/src/util/README      2006-06-23 15:04:30 UTC (rev 3026)
@@ -3,14 +3,14 @@
 util/error: basic error handling functions (lowest layer)
 util/win: win32 portability (lowest layer)
  util/string: string and memory abstraction (depends on error)
-  util/disk  : disk IO abstractions (depends on string)
+  util/disk: disk IO abstractions (depends on string)
  util/config: configuration handling (depends on error)
   util/getopt: command line parsing (depends on config)
  util/threads: pthread abstractions (depends on error)
 
 -------- TODO ----------
-  util/network: network IO abstractions (depends on threads, config)
-  util/os    : process and system abstractions (depends on threads, config)
+  util/os: process and system abstractions (depends on threads, config)
+   util/network: network IO abstractions (depends on threads, config, os)
 
 util: main utility library (depends on all of the above)
       => these are all statically linked into gnunetutil.so

Modified: GNUnet/src/util/network/tcpio.c
===================================================================
--- GNUnet/src/util/network/tcpio.c     2006-06-22 18:45:40 UTC (rev 3025)
+++ GNUnet/src/util/network/tcpio.c     2006-06-23 15:04:30 UTC (rev 3026)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002 Christian Grothoff (and other contributing authors)
+     (C) 2001, 2002, 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
@@ -43,6 +43,66 @@
 #define DEBUG_TCPIO NO
 
 /**
+ * Struct to refer to a GNUnet TCP connection.
+ * This is more than just a socket because if the server
+ * drops the connection, the client automatically tries
+ * to reconnect (and for that needs connection information).
+ */
+typedef struct GNUNET_TCP_SOCKET {
+
+  /**
+   * the socket handle, -1 if invalid / not life
+   */
+  int socket;
+
+  /**
+   * the following is the IP for the remote host for client-sockets,
+   * as returned by gethostbyname("hostname"); server sockets should
+   * use 0.
+   */
+  IPaddr ip;
+
+  /**
+   * the port number, in host byte order
+   */
+  unsigned short port;
+
+  /**
+   * Write buffer length for non-blocking writes.
+   */
+  unsigned int outBufLen;
+
+  /**
+   * Write buffer for non-blocking writes.
+   */
+  void * outBufPending;
+
+  struct Mutex * readlock;
+
+  struct Mutex * writelock;
+
+} GNUNET_TCP_SOCKET;
+
+
+/**
+ * CS communication: simple return value
+ */
+typedef struct {
+
+  /**
+   * The CS header (values: sizeof(CS_returnvalue_MESSAGE) + error-size, 
CS_PROTO_RETURN_VALUE)
+   */
+  MESSAGE_HEADER header;
+
+  /**
+   * The return value (network byte order)
+   */
+  int return_value;
+} RETURN_VALUE_MESSAGE;
+
+
+
+/**
  * Initialize a GNUnet client socket.
  * @param port the portnumber in host byte order
  * @param ip IP of the host to connect to, in network byte order

Modified: GNUnet/src/util/os/dso.c
===================================================================
--- GNUnet/src/util/os/dso.c    2006-06-22 18:45:40 UTC (rev 3025)
+++ GNUnet/src/util/os/dso.c    2006-06-23 15:04:30 UTC (rev 3026)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2002, 2003, 2004, 2005 Christian Grothoff (and other contributing 
authors)
+     (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
@@ -19,142 +19,165 @@
 */
 
 /**
- * @file util/dso.c
- * @brief Methods to access dynamic shared objects (DSOs).
+ * @file util/os/dso.c
+ * @brief Methods to access plugins (or dynamic shared objects (DSOs)).
  * @author Christian Grothoff
  */
 
 #include "platform.h"
-#include "gnunet_util.h"
+#include "gnunet_util_os.h"
 
-static int using_valgrind;
+typedef struct PluginHandle {
+  struct GE_Context * ectx;
+  char * libprefix;
+  char * dsoname;
+  void * handle;
+} Plugin;
 
-static char * old_dlsearchpath = NULL;
 
+static char * old_dlsearchpath;
+
+/* NILS: this method will need to be
+   ported for Win32 and other non-linux
+   systems */
+#if LINUX
+static char * getPluginPath() {
+  char * fn;
+  char * lnk;
+  size_t size;
+
+  fn = MALLOC(64);
+  SNPRINTF(fn, 
+          64,
+          "/proc/%u/exe",
+          getpid());
+  lnk = MALLOC(1024);
+  size = readlink(fn, lnk, 1023);
+  if ( (size == 0) || (size >= 1024) ) {
+    GE_LOG_STRERROR_FILE(NULL,
+                        GE_ERROR | GE_USER | GE_ADMIN | GE_IMMEDIATE,
+                        "readlink",
+                        fn);
+    FREE(fn);
+    FREE(lnk);
+    return NULL;
+  }
+  FREE(fn);
+  lnk[size] = '\0';
+  while ( (lnk[size] != '/') &&
+         (size > 0) )
+    size--;
+  if ( (size < 4) ||
+       (lnk[size-4] != '/') ) {
+    GE_LOG(NULL,
+          GE_ERROR | GE_USER | GE_ADMIN | GE_IMMEDIATE,
+          _("Cannot determine plugin path, application must be installed in 
directory ending with `%s'.\n"),
+          "bin/");
+    FREE(lnk);
+    return NULL;
+  }
+  lnk[  size] = '\0';
+  lnk[--size] = 'b';
+  lnk[--size] = 'i';
+  lnk[--size] = 'l';
+  return lnk;
+}
+#endif
+
 /* using libtool, needs init! */
 void __attribute__ ((constructor)) gnc_ltdl_init(void) {
   int err;
+  const char * opath;
+  char * path;
 
-  err = lt_dlinit ();
-  if (err > 0)
-    {
-#if DEBUG
-      fprintf(stderr,
-             _("Initialization of plugin mechanism failed: %s!\n"),
-             lt_dlerror());
-#endif
-      return;
-    }
-  if (lt_dlgetsearchpath() != NULL)
-    old_dlsearchpath = strdup(lt_dlgetsearchpath());
-  if (lt_dlgetsearchpath () == NULL)
-    lt_dladdsearchdir ("/usr/lib/GNUnet");
-  else if (strstr (lt_dlgetsearchpath (), "/usr/lib/GNUnet") == NULL)
-    lt_dladdsearchdir ("/usr/lib/GNUnet");
-  if (strstr (lt_dlgetsearchpath (), "/usr/local/lib/GNUnet") == NULL)
-    lt_dladdsearchdir ("/usr/local/lib/GNUnet");
-#ifdef PLUGIN_PATH
-  if (strstr (lt_dlgetsearchpath (), PLUGIN_PATH) == NULL)
-    lt_dladdsearchdir (PLUGIN_PATH);
-#endif
+  err = lt_dlinit();
+  if (err > 0) {
+    fprintf(stderr,
+           _("Initialization of plugin mechanism failed: %s!\n"),
+           lt_dlerror());
+    return;
+  }
+  opath = lt_dlgetsearchpath();
+  if (opath != NULL)
+    old_dlsearchpath = STRDUP(opath);
+  path = getPluginPath();
+  lt_dlsetsearchpath(path);
+  FREE(path);
 }
 
 void __attribute__ ((destructor)) gnc_ltdl_fini(void) {
   lt_dlsetsearchpath(old_dlsearchpath);
   if (old_dlsearchpath != NULL) {
-    free(old_dlsearchpath);
+    FREE(old_dlsearchpath);
     old_dlsearchpath = NULL;
   }
-  if (0 != using_valgrind)
-    lt_dlexit ();
+  lt_dlexit ();
 }
 
-
-static char * buildLibName(const char * prefix,
-                          const char * dso) {
-  char * libname;
-
-  libname = MALLOC(strlen(dso) +
-                  strlen(prefix) + 1);
-  libname[0] = '\0';
-  strcat(libname, prefix);
-  strcat(libname, dso);
-  return libname;
-}
-
-void * loadDynamicLibrary(const char * libprefix,
-                         const char * dsoname) {
+struct PluginHandle * 
+os_plugin_load(struct GE_Context * ectx,
+              const char * libprefix,
+              const char * dsoname) {
   void * libhandle;
   char * libname;
+  Plugin * plug;
 
-  if (0 != lt_dlinit())
-    DIE_STRERROR("lt_dlinit");
-  /* finally, load the library */
-  libname = buildLibName(libprefix,
-                        dsoname);
+  libname = MALLOC(strlen(dsoname) +
+                  strlen(libprefix) + 1);
+  strcpy(libname, libprefix);
+  strcat(libname, dsoname);
   libhandle = lt_dlopenext(libname);
   if (libhandle == NULL) {
-    LOG(LOG_ERROR,
-       _("`%s' failed for library `%s' at %s:%d with error: %s\n"),
-       "lt_dlopenext",
-       libname,
-       __FILE__, __LINE__,
-       lt_dlerror());
+    GE_LOG(ectx,
+          GE_ERROR | GE_USER | GE_ADMIN | GE_IMMEDIATE,
+          _("`%s' failed for library `%s' with error: %s\n"),
+          "lt_dlopenext",
+          libname,
+          lt_dlerror());
+    FREE(libname);
+    return NULL;
   }
   FREE(libname);
-  return libhandle;
+  plug = MALLOC(sizeof(Plugin));
+  plug->handle = libhandle;
+  plug->libprefix = STRDUP(libprefix);
+  plug->dsoname = STRDUP(dsoname);
+  plug->ectx = ectx;
+  return plug;
 }
 
-void unloadDynamicLibrary(void * libhandle) {
-  /* when valgrinding, comment out these lines
-     to get decent traces for memory leaks on exit */
-  if (0 != getConfigurationInt("GNUNETD",
-                              "VALGRIND")) {
-    lt_dlclose(libhandle);
-    if (0 != lt_dlexit())
-      LOG_STRERROR(LOG_WARNING, "lt_dlexit");
-  } else
-    using_valgrind = 1;
+void os_plugin_unload(struct PluginHandle * libhandle) {
+  lt_dlclose(plugin->handle);
+  FREE(plugin->libprefix);
+  FREE(plugin->dsoname);
+  FREE(plugin);
 }
 
-void * trybindDynamicMethod(void * libhandle,
-                           const char * methodprefix,
-                           const char * dsoname) {
+void * 
+os_plugin_resolve_function(struct PluginHandle * plugin,
+                          const char * methodprefix,
+                          int logError) {
   char * initName;
   void * mptr;
 
-  initName = MALLOC(strlen(dsoname) +
+  initName = MALLOC(strlen(plug->dsoname) +
                    strlen(methodprefix) + 2);
-  initName[0] = '\0';
-  strcat(initName, "_");
+  strcpy(initName, "_");
   strcat(initName, methodprefix);
   strcat(initName, dsoname);
   mptr = lt_dlsym(libhandle, &initName[1]);
-  if (mptr == NULL) {
-    /* try again with "_" prefix; some systems use that
-       variant. */
+  if (mptr == NULL) 
     mptr = lt_dlsym(libhandle, initName);
-  }
+  if ( (mptr == NULL) &&
+       (logError) )
+    GE_LOG(plug->ectx,
+          GE_ERROR | GE_USER | GE_DEVELOPER | GE_IMMEDIATE,
+          _("`%s' failed to resolve method '%s' with error: %s\n"),
+          "lt_dlsym",
+          &initName[1],
+          lt_dlerror());
   FREE(initName);
   return mptr;
 }
 
-void * bindDynamicMethod(void * libhandle,
-                        const char * methodprefix,
-                        const char * dsoname) {
-  void * mptr;
-
-  mptr = trybindDynamicMethod(libhandle,
-                             methodprefix,
-                             dsoname);
-  if (mptr == NULL)
-    LOG(LOG_ERROR,
-       _("`%s' failed to resolve method '%s%s' at %s:%d with error: %s\n"),
-       "lt_dlsym",
-       methodprefix, dsoname,
-       __FILE__, __LINE__,
-       lt_dlerror());
-  return mptr;
-}
-
 /* end of dso.c */                     





reply via email to

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