gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r22170 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r22170 - gnunet/src/util
Date: Wed, 20 Jun 2012 20:45:30 +0200

Author: grothoff
Date: 2012-06-20 20:45:30 +0200 (Wed, 20 Jun 2012)
New Revision: 22170

Modified:
   gnunet/src/util/client.c
   gnunet/src/util/network.c
   gnunet/src/util/service.c
Log:
-fixing #2439

Modified: gnunet/src/util/client.c
===================================================================
--- gnunet/src/util/client.c    2012-06-20 18:38:27 UTC (rev 22169)
+++ gnunet/src/util/client.c    2012-06-20 18:45:30 UTC (rev 22170)
@@ -738,40 +738,41 @@
       {
         LOG (GNUNET_ERROR_TYPE_WARNING,
              _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
-             sizeof (s_un.sun_path));
+             (unsigned long long) sizeof (s_un.sun_path));
+       unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
       }
-      else
+    }
+    if (NULL != unixpath)
+    {
+      sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
+      if (NULL != sock)
       {
-        sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
-        if (NULL != sock)
-        {
-          memset (&s_un, 0, sizeof (s_un));
-          s_un.sun_family = AF_UNIX;
-          slen = strlen (unixpath) + 1;
-          if (slen >= sizeof (s_un.sun_path))
-            slen = sizeof (s_un.sun_path) - 1;
-          memcpy (s_un.sun_path, unixpath, slen);
-          s_un.sun_path[slen] = '\0';
-          slen = sizeof (struct sockaddr_un);
+       memset (&s_un, 0, sizeof (s_un));
+       s_un.sun_family = AF_UNIX;
+       slen = strlen (unixpath) + 1;
+       if (slen >= sizeof (s_un.sun_path))
+         slen = sizeof (s_un.sun_path) - 1;
+       memcpy (s_un.sun_path, unixpath, slen);
+       s_un.sun_path[slen] = '\0';
+       slen = sizeof (struct sockaddr_un);
 #if LINUX
-          s_un.sun_path[0] = '\0';
+       s_un.sun_path[0] = '\0';
 #endif
 #if HAVE_SOCKADDR_IN_SIN_LEN
-          s_un.sun_len = (u_char) slen;
+       s_un.sun_len = (u_char) slen;
 #endif
-          if (GNUNET_OK !=
-              GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) 
&s_un,
-                                          slen))
-          {
-            /* failed to bind => service must be running */
-            GNUNET_free (unixpath);
-            (void) GNUNET_NETWORK_socket_close (sock);
-            GNUNET_SCHEDULER_add_continuation (task, task_cls,
-                                               
GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-            return;
-          }
-          (void) GNUNET_NETWORK_socket_close (sock);
-        }
+       if (GNUNET_OK !=
+           GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
+                                       slen))
+        {
+         /* failed to bind => service must be running */
+         GNUNET_free (unixpath);
+         (void) GNUNET_NETWORK_socket_close (sock);
+         GNUNET_SCHEDULER_add_continuation (task, task_cls,
+                                            
GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+         return;
+       }
+       (void) GNUNET_NETWORK_socket_close (sock);        
         /* let's try IP */
       }
     }

Modified: gnunet/src/util/network.c
===================================================================
--- gnunet/src/util/network.c   2012-06-20 18:38:27 UTC (rev 22169)
+++ gnunet/src/util/network.c   2012-06-20 18:45:30 UTC (rev 22170)
@@ -22,12 +22,11 @@
  * @file util/network.c
  * @brief basic, low-level networking interface
  * @author Nils Durner
+ * @author Christian Grothoff
  */
-
 #include "platform.h"
-#include "gnunet_disk_lib.h"
 #include "disk.h"
-#include "gnunet_container_lib.h"
+#include "gnunet_util_lib.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file 
(kind, "util", syscall, filename)
@@ -67,6 +66,49 @@
 };
 
 
+/**
+ * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
+ * shorten it to an acceptable length while keeping it unique
+ * and making sure it remains a valid filename (if possible).
+ *
+ * @param unixpath long path, will be freed (or same pointer returned
+ *        with moved 0-termination).
+ * @return shortened unixpath, NULL on error
+ */
+char *
+GNUNET_NETWORK_shorten_unixpath (char *unixpath)
+{
+  struct sockaddr_un dummy;
+  size_t slen;
+  char *end;
+  struct GNUNET_CRYPTO_ShortHashCode sh;
+  struct GNUNET_CRYPTO_ShortHashAsciiEncoded ae;
+  size_t upm;
+
+  upm = sizeof (dummy.sun_path);   
+  slen = strlen (unixpath);
+  if (slen < upm)
+    return unixpath; /* no shortening required */
+  GNUNET_CRYPTO_short_hash (unixpath, slen, &sh);
+  while (sizeof (struct GNUNET_CRYPTO_ShortHashAsciiEncoded) + 
+        strlen (unixpath) >= upm)
+  {
+    if (NULL == (end = strrchr (unixpath, '/')))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 _("Unable to shorten unix path `%s' while keeping name 
unique\n"),
+                 unixpath);
+      GNUNET_free (unixpath);
+      return NULL;
+    }
+    *end = '\0';
+  }
+  GNUNET_CRYPTO_short_hash_to_enc (&sh, &ae);
+  strcat (unixpath, (char*) ae.short_encoding);
+  return unixpath;
+}
+
+
 #ifndef FD_COPY
 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
 #endif

Modified: gnunet/src/util/service.c
===================================================================
--- gnunet/src/util/service.c   2012-06-20 18:38:27 UTC (rev 22169)
+++ gnunet/src/util/service.c   2012-06-20 18:45:30 UTC (rev 22170)
@@ -973,12 +973,12 @@
     {
       LOG (GNUNET_ERROR_TYPE_WARNING,
            _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
-           sizeof (s_un.sun_path));
-      GNUNET_free_non_null (hostname);
-      GNUNET_free (unixpath);
-      return GNUNET_SYSERR;
+           (unsigned long long) sizeof (s_un.sun_path));
+      unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
     }
-
+  }
+  if (NULL != unixpath)
+  {
     desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
     if (NULL == desc)
     {




reply via email to

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