proper shutdown
[oweals/gnunet.git] / src / util / client.c
index d957dd00a5d4f39c5ef05fe1fe4e0018f2dba1aa..7e688059044211cebf40dcea045f51534252b4a0 100644 (file)
@@ -43,7 +43,6 @@
  */
 #define MAX_ATTEMPTS 50
 
-
 /**
  * Handle for a transmission request.
  */
@@ -253,6 +252,7 @@ struct GNUNET_CLIENT_Connection
 
 };
 
+
 /**
  * Try to connect to the service.
  *
@@ -271,21 +271,28 @@ do_connect (const char *service_name,
   char *unixpath;
   unsigned long long port;
 
+  sock = NULL;
 #if AF_UNIX
-  if (0 == attempt % 2)
+  if (0 == (attempt % 2))
     {
       /* on even rounds, try UNIX */
-      if (GNUNET_OK ==
+      if ((GNUNET_OK ==
          GNUNET_CONFIGURATION_get_value_string (cfg,
                                                 service_name,
-                                                "UNIXPATH", &unixpath))
+                                                "UNIXPATH", &unixpath)) &&
+          (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
        {
-         sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
-                                                                   unixpath);
-         GNUNET_free (unixpath);
+          sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
          if (sock != NULL)
-           return sock;
+           {
+#if DEBUG_CLIENT
+              GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath);
+#endif
+              GNUNET_free(unixpath);
+              return sock;
+           }
        }
+      GNUNET_free_non_null (unixpath);
     }
 #endif
 
@@ -301,8 +308,7 @@ do_connect (const char *service_name,
                                               "HOSTNAME", &hostname)))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  _
-                  ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
+                  _("Could not determine valid hostname and port for service `%s' from configuration.\n"),
                   service_name);
       return NULL;
     }
@@ -314,6 +320,35 @@ do_connect (const char *service_name,
                   service_name);
       return NULL;
     }
+  if (port == 0)
+    {
+#if AF_UNIX
+      if (0 != (attempt % 2))
+       {
+         /* try UNIX */
+         if ((GNUNET_OK ==
+             GNUNET_CONFIGURATION_get_value_string (cfg,
+                                                    service_name,
+                                                    "UNIXPATH", &unixpath)) &&
+              (0 < strlen (unixpath)))
+           {
+             sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
+                                                                       unixpath);
+             GNUNET_free (unixpath);
+             if (sock != NULL)
+               return sock;            
+           }
+       }
+#endif
+#if DEBUG_CLIENT
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+                 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
+                 service_name);
+#endif
+      GNUNET_free (hostname);
+      return NULL;
+    }
+
   sock = GNUNET_CONNECTION_create_from_connect (cfg,
                                                 hostname,
                                                 port);
@@ -456,6 +491,9 @@ receive_helper (void *cls,
   if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
     {
       /* signal timeout! */
+#if DEBUG_CLIENT
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "timeout in receive_helper, available %d, conn->sock %s, errCode %d\n", available, conn->sock == NULL ? "NULL" : "non-NULL", errCode);
+#endif
       if (NULL != (receive_handler = conn->receiver_handler))
         {
           receive_handler_cls = conn->receiver_handler_cls;
@@ -562,6 +600,9 @@ GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
     {
       GNUNET_assert (sock->in_receive == GNUNET_NO);
       sock->in_receive = GNUNET_YES;
+#if DEBUG_CLIENT
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
+#endif
       GNUNET_CONNECTION_receive (sock->sock,
                                  GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
                                  timeout, &receive_helper, sock);