-typo
[oweals/gnunet.git] / src / util / client.c
index e098f218331288617dcd4bdd07626d62a3e76cdb..73cca2383c197a1843f09b7a3cccea91bd39f81b 100644 (file)
@@ -247,6 +247,89 @@ struct GNUNET_CLIENT_Connection
 };
 
 
+/**
+ * Try connecting to the server using UNIX domain sockets.
+ *
+ * @param service_name name of service to connect to
+ * @param cfg configuration to use
+ * @return NULL on error, connection to UNIX otherwise
+ */
+static struct GNUNET_CONNECTION_Handle *
+try_unixpath (const char *service_name,
+             const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+#if AF_UNIX
+  struct GNUNET_CONNECTION_Handle *connection;
+  char *unixpath;
+  struct sockaddr_un s_un;
+
+  unixpath = NULL;
+  if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && 
+      (0 < strlen (unixpath)))     
+  {
+    /* We have a non-NULL unixpath, need to validate it */
+    if (strlen (unixpath) >= sizeof (s_un.sun_path))
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+          _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
+          (unsigned long long) sizeof (s_un.sun_path));
+      unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
+      LOG (GNUNET_ERROR_TYPE_INFO,
+          _("Using `%s' instead\n"), unixpath);
+    }
+    connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
+    if (NULL != connection)
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
+          unixpath);
+      GNUNET_free (unixpath);
+      return connection;
+    }
+  }
+  GNUNET_free_non_null (unixpath);
+#endif
+  return NULL;
+}
+
+
+/**
+ * Try connecting to the server using UNIX domain sockets.
+ *
+ * @param service_name name of service to connect to
+ * @param cfg configuration to use
+ * @return GNUNET_OK if the configuration is valid, GNUNET_SYSERR if not
+ */
+static int
+test_service_configuration (const char *service_name,
+                           const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  int ret = GNUNET_SYSERR;
+  char *hostname = NULL;
+  unsigned long long port;
+#if AF_UNIX
+  char *unixpath = NULL;
+
+  if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && 
+      (0 < strlen (unixpath)))     
+    ret = GNUNET_OK;
+  GNUNET_free_non_null (unixpath);
+#endif
+
+  if ( (GNUNET_YES ==
+       GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
+       (GNUNET_OK ==
+       GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) && 
+       (port <= 65535) && (0 != port) &&
+       (GNUNET_OK ==
+       GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
+                                              &hostname)) &&
+       (0 != strlen (hostname)) )
+    ret = GNUNET_OK;
+  GNUNET_free_non_null (hostname);
+  return ret;
+}
+
+
 /**
  * Try to connect to the service.
  *
@@ -261,32 +344,16 @@ do_connect (const char *service_name,
 {
   struct GNUNET_CONNECTION_Handle *connection;
   char *hostname;
-  char *unixpath;
   unsigned long long port;
 
   connection = NULL;
-#if AF_UNIX
   if (0 == (attempt % 2))
   {
-    /* on even rounds, try UNIX */
-    unixpath = NULL;
-    if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && 
-       (0 < strlen (unixpath)))     
-    {
-      /* We have a non-NULL unixpath, need to validate it */
-      connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
-      if (NULL != connection)
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
-             unixpath);
-        GNUNET_free (unixpath);
-        return connection;
-      }
-    }
-    GNUNET_free_non_null (unixpath);
+    /* on even rounds, try UNIX first */
+    connection = try_unixpath (service_name, cfg);
+    if (NULL != connection)
+      return connection;
   }
-#endif
-
   if (GNUNET_YES ==
       GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
   {
@@ -319,28 +386,10 @@ do_connect (const char *service_name,
   }
   if (0 == port)
   {
-#if AF_UNIX
-    if (0 != (attempt % 2))
-    {
-      /* try UNIX */
-      unixpath = NULL;
-      if ((GNUNET_OK ==
-           GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
-                                                  &unixpath)) &&
-          (0 < strlen (unixpath)))
-      {
-        connection =
-            GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
-        if (NULL != connection)
-        {
-          GNUNET_free (unixpath);
-          GNUNET_free_non_null (hostname);
-          return connection;
-        }
-      }
-      GNUNET_free_non_null (unixpath);
-    }
-#endif
+    /* if port is 0, try UNIX */
+    connection = try_unixpath (service_name, cfg);
+    if (NULL != connection)
+      return connection;
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
          service_name);
@@ -367,6 +416,10 @@ GNUNET_CLIENT_connect (const char *service_name,
   struct GNUNET_CLIENT_Connection *client;
   struct GNUNET_CONNECTION_Handle *connection;
 
+  if (GNUNET_OK != 
+      test_service_configuration (service_name,
+                                 cfg))
+    return NULL;
   connection = do_connect (service_name, cfg, 0);
   client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
   client->attempts = 1;
@@ -695,40 +748,43 @@ GNUNET_CLIENT_service_test (const char *service,
       {
         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);
+        LOG (GNUNET_ERROR_TYPE_INFO,
+             _("Using `%s' instead\n"), 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 */
       }
     }