- Allocate buffer large enough to contain UNIX_PATH_MAX size pathnames in case of...
[oweals/gnunet.git] / src / util / client.c
index 88a3b392cc5799f2f92d47333f3db87f0a4eb297..02bec5a8f57e6c797b9f6ca5b5f2c9bceef11862 100644 (file)
@@ -168,7 +168,7 @@ struct GNUNET_CLIENT_Connection
   GNUNET_CLIENT_MessageHandler receiver_handler;
 
   /**
-   * Closure for receiver_handler.
+   * Closure for @e receiver_handler.
    */
   void *receiver_handler_cls;
 
@@ -330,7 +330,7 @@ test_service_configuration (const char *service_name,
  */
 static struct GNUNET_CONNECTION_Handle *
 do_connect (const char *service_name,
-            const struct GNUNET_CONFIGURATION_Handle *cfg, 
+            const struct GNUNET_CONFIGURATION_Handle *cfg,
            unsigned int attempt)
 {
   struct GNUNET_CONNECTION_Handle *connection;
@@ -545,8 +545,11 @@ receive_helper (void *cls, const void *buf, size_t available,
   if (0 == remaining.rel_value_us)
   {
     /* signal timeout! */
-    if (NULL != client->receiver_handler)
-      client->receiver_handler (client->receiver_handler_cls, NULL);
+    if (NULL != (receive_handler = client->receiver_handler))
+    {
+      client->receiver_handler = NULL;
+      receive_handler (client->receiver_handler_cls, NULL);
+    }
     return;
   }
   /* back to receive -- either for more data or to call callback! */
@@ -573,8 +576,11 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   char mbuf[msize] GNUNET_ALIGN;
   struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u from %s service.\n",
-       ntohs (cmsg->type), msize, client->service_name);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received message of type %u and size %u from %s service.\n",
+       ntohs (cmsg->type),
+       msize,
+       client->service_name);
   client->receive_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_assert (GNUNET_YES == client->msg_complete);
   GNUNET_assert (client->received_pos >= msize);
@@ -600,7 +606,7 @@ receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  */
 void
 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
-                       GNUNET_CLIENT_MessageHandler handler, 
+                       GNUNET_CLIENT_MessageHandler handler,
                       void *handler_cls,
                        struct GNUNET_TIME_Relative timeout)
 {
@@ -816,7 +822,7 @@ struct GNUNET_CLIENT_TestHandle *
 GNUNET_CLIENT_service_test (const char *service,
                             const struct GNUNET_CONFIGURATION_Handle *cfg,
                             struct GNUNET_TIME_Relative timeout,
-                            GNUNET_CLIENT_TestResultCallback cb, 
+                            GNUNET_CLIENT_TestResultCallback cb,
                            void *cb_cls)
 {
   struct GNUNET_CLIENT_TestHandle *th;
@@ -836,11 +842,12 @@ GNUNET_CLIENT_service_test (const char *service,
     /* probe UNIX support */
     struct sockaddr_un s_un;
     char *unixpath;
+    int abstract;
 
     unixpath = NULL;
     if ((GNUNET_OK ==
         GNUNET_CONFIGURATION_get_value_filename (cfg,
-                                                 service, 
+                                                 service,
                                                  "UNIXPATH",
                                                  &unixpath)) &&
        (0 < strlen (unixpath)))  /* We have a non-NULL unixpath, does that mean it's valid? */
@@ -856,19 +863,29 @@ GNUNET_CLIENT_service_test (const char *service,
              _("Using `%s' instead\n"), unixpath);
       }
     }
+#ifdef LINUX
+    abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
+                                                     "TESTING",
+                                                     "USE_ABSTRACT_SOCKETS");
+#else
+    abstract = GNUNET_NO;
+#endif
+    if ((NULL != unixpath) && (GNUNET_YES != abstract))
+    {
+      if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (unixpath))
+        GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                  "mkdir", unixpath);
+    }
     if (NULL != unixpath)
     {
-      if (GNUNET_OK !=
-         GNUNET_DISK_directory_create_for_file (unixpath))
-       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                                 "mkdir",
-                                 unixpath);
       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;
         strncpy (s_un.sun_path, unixpath, sizeof (s_un.sun_path) - 1);
+        if (GNUNET_YES == abstract)
+          s_un.sun_path[0] = '\0';
 #if HAVE_SOCKADDR_IN_SIN_LEN
         s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
 #endif
@@ -1030,7 +1047,7 @@ client_notify (void *cls, size_t size, void *buf);
  * @param tc unused
  */
 static void
-client_delayed_retry (void *cls, 
+client_delayed_retry (void *cls,
                      const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CLIENT_TransmitHandle *th = cls;
@@ -1064,6 +1081,7 @@ client_delayed_retry (void *cls,
          "Transmission failed %u times, trying again in %s.\n",
          MAX_ATTEMPTS - th->attempts_left,
          GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->reconnect_task);
     th->reconnect_task =
         GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
     return;
@@ -1141,6 +1159,7 @@ client_notify (void *cls, size_t size, void *buf)
          MAX_ATTEMPTS - th->attempts_left,
          GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
     client->th = th;
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->reconnect_task);
     th->reconnect_task =
         GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
     return 0;
@@ -1184,7 +1203,7 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
                                      size_t size,
                                      struct GNUNET_TIME_Relative timeout,
                                      int auto_retry,
-                                     GNUNET_CONNECTION_TransmitReadyNotify notify, 
+                                     GNUNET_CONNECTION_TransmitReadyNotify notify,
                                     void *notify_cls)
 {
   struct GNUNET_CLIENT_TransmitHandle *th;
@@ -1196,7 +1215,7 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
     GNUNET_break (0);
     return NULL;
   }
-  th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
+  th = GNUNET_new (struct GNUNET_CLIENT_TransmitHandle);
   th->client = client;
   th->size = size;
   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
@@ -1209,8 +1228,10 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
   client->th = th;
   if (NULL == client->connection)
   {
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->reconnect_task);
     th->reconnect_task =
-        GNUNET_SCHEDULER_add_delayed (client->back_off, &client_delayed_retry,
+        GNUNET_SCHEDULER_add_delayed (client->back_off,
+                                      &client_delayed_retry,
                                       th);
 
   }