-removing 2nd argument from GNUNET_CLIENT_disconnect as it was virtually always GNUNE...
[oweals/gnunet.git] / src / util / connection.c
index 1ac1b230473fdaab82441f4ed9544ff3243b8e7b..6908144b790ca7f7a5ec7b4db53974534b228663 100644 (file)
 #include "gnunet_scheduler_lib.h"
 #include "gnunet_server_lib.h"
 
-#define DEBUG_CONNECTION GNUNET_NO
+
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
 
 /**
  * Possible functions to call after connect failed or succeeded.
@@ -111,7 +114,7 @@ struct GNUNET_CONNECTION_TransmitHandle
 
 /**
  * During connect, we try multiple possible IP addresses
- * to find out which one might work. 
+ * to find out which one might work.
  */
 struct AddressProbe
 {
@@ -165,13 +168,13 @@ struct GNUNET_CONNECTION_Handle
   const struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
-   * Linked list of sockets we are currently trying out 
+   * Linked list of sockets we are currently trying out
    * (during connect).
    */
   struct AddressProbe *ap_head;
 
   /**
-   * Linked list of sockets we are currently trying out 
+   * Linked list of sockets we are currently trying out
    * (during connect).
    */
   struct AddressProbe *ap_tail;
@@ -320,6 +323,7 @@ GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
   return GNUNET_NETWORK_socket_disable_corking (sock->sock);
 }
 
+
 /**
  * Create a socket handle by boxing an existing OS socket.  The OS
  * socket should henceforth be no longer used directly.
@@ -377,7 +381,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
       GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
   if (NULL == sock)
   {
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept");
     return NULL;
   }
   if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t)))
@@ -425,9 +429,8 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
     /* largely traditional GNU/Linux */
     olen = sizeof (uc);
     if ((0 ==
-         getsockopt (GNUNET_NETWORK_get_fd (sock),
-                     SOL_SOCKET, SO_PEERCRED, &uc, &olen)) &&
-        (olen == sizeof (uc)))
+         getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc,
+                     &olen)) && (olen == sizeof (uc)))
     {
       gc.uid = uc.uid;
       gc.gid = uc.gid;
@@ -455,8 +458,8 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
       (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
   {
     if (aret == GNUNET_NO)
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  _("Access denied to `%s'\n"), GNUNET_a2s (uaddr, addrlen));
+      LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
+           GNUNET_a2s (uaddr, addrlen));
     GNUNET_break (GNUNET_OK ==
                   GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
@@ -469,14 +472,13 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
   ret->addr = uaddr;
   ret->addrlen = addrlen;
   ret->sock = sock;
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Accepting connection from `%s': %p\n"),
-              GNUNET_a2s (uaddr, addrlen), ret);
-#endif
+  LOG (GNUNET_ERROR_TYPE_INFO, 
+       _("Accepting connection from `%s': %p\n"),
+       GNUNET_a2s (uaddr, addrlen), ret);
   return ret;
 }
 
+
 /**
  * Obtain the network address of the other party.
  *
@@ -530,23 +532,19 @@ destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_assert (sock->dns_active == NULL);
   if (0 != (sock->ccs & COCO_TRANSMIT_READY))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Destroy waits for CCS-TR to be done (%p)\n", sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy waits for CCS-TR to be done (%p)\n",
+         sock);
     sock->ccs |= COCO_DESTROY_CONTINUATION;
     return;
   }
   if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Destroy waits for write_task to be done (%p)\n", sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Destroy waits for write_task to be done (%p)\n", sock);
     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
-    sock->destroy_task
-        = GNUNET_SCHEDULER_add_after (sock->write_task,
-                                      &destroy_continuation, sock);
+    sock->destroy_task =
+        GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation,
+                                    sock);
     return;
   }
   if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
@@ -556,27 +554,23 @@ destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   if (sock->sock != NULL)
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
     if (sock->persist != GNUNET_YES)
     {
       if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
           && (errno != ENOTCONN) && (errno != ECONNRESET))
-        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown");
+        LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
     }
   }
   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
   {
     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
-    sock->destroy_task
-        = GNUNET_SCHEDULER_add_after (sock->read_task,
-                                      &destroy_continuation, sock);
+    sock->destroy_task =
+        GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation,
+                                    sock);
     return;
   }
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
   while (NULL != (pos = sock->ap_head))
   {
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
@@ -603,10 +597,7 @@ destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_free_non_null (sock->addr);
   GNUNET_free_non_null (sock->hostname);
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Freeing memory of connection %p.\n", sock);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", sock);
   GNUNET_free (sock->write_buffer);
   GNUNET_free (sock);
 }
@@ -633,16 +624,9 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 static void
 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
 {
-#if DEBUG_CONNECTION
-  GNUNET_log ((0 != strncmp (h->hostname,
-                             "localhost:",
-                             10))
-              ? GNUNET_ERROR_TYPE_INFO
-              : GNUNET_ERROR_TYPE_WARNING,
-              _
-              ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
-              h->hostname, h->port);
-#endif
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
+       h->hostname, h->port);
   /* connect failed / timed out */
   GNUNET_break (h->ap_head == NULL);
   GNUNET_break (h->ap_tail == NULL);
@@ -652,20 +636,16 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
   /* trigger jobs that used to wait on "connect_task" */
   if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "connect_fail_continuation triggers receive_again (%p)\n", h);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "connect_fail_continuation triggers receive_again (%p)\n", h);
     h->ccs -= COCO_RECEIVE_AGAIN;
     h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
   }
   if (0 != (h->ccs & COCO_TRANSMIT_READY))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
-                h);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
+         h);
     GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
     GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
     h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
@@ -676,11 +656,8 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
   }
   if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "connect_fail_continuation runs destroy_continuation (%p)\n",
-                h);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "connect_fail_continuation runs destroy_continuation (%p)\n", h);
     h->ccs -= COCO_DESTROY_CONTINUATION;
     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
     h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
@@ -696,28 +673,21 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
 static void
 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
 {
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Connection to `%s' succeeded! (%p)\n",
-              GNUNET_a2s (h->addr, h->addrlen), h);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
+       GNUNET_a2s (h->addr, h->addrlen), h);
   /* trigger jobs that waited for the connection */
   if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "connect_success_continuation runs receive_again (%p)\n", h);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "connect_success_continuation runs receive_again (%p)\n", h);
     h->ccs -= COCO_RECEIVE_AGAIN;
     h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
   }
   if (0 != (h->ccs & COCO_TRANSMIT_READY))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
-                h);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
+         h);
     GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
     GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
     h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
@@ -731,11 +701,8 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
   }
   if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "connect_success_continuation runs destroy_continuation (%p)\n",
-                h);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "connect_success_continuation runs destroy_continuation (%p)\n", h);
     h->ccs -= COCO_DESTROY_CONTINUATION;
     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
     h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
@@ -767,8 +734,8 @@ connect_probe_continuation (void *cls,
   error = 0;
   if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
       (GNUNET_OK !=
-       GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
-                                         &error, &len)) || (error != 0))
+       GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error,
+                                         &len)) || (error != 0))
   {
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
     GNUNET_free (ap);
@@ -804,8 +771,8 @@ connect_probe_continuation (void *cls,
  * @param addrlen length of addr
  */
 static void
-try_connect_using_address (void *cls,
-                           const struct sockaddr *addr, socklen_t addrlen)
+try_connect_using_address (void *cls, const struct sockaddr *addr,
+                           socklen_t addrlen)
 {
   struct GNUNET_CONNECTION_Handle *h = cls;
   struct AddressProbe *ap;
@@ -814,7 +781,7 @@ try_connect_using_address (void *cls,
   if (addr == NULL)
   {
     h->dns_active = NULL;
-    if (NULL == h->ap_head)
+    if ((NULL == h->ap_head) && (NULL == h->sock))
       connect_fail_continuation (h);
     return;
   }
@@ -822,11 +789,9 @@ try_connect_using_address (void *cls,
     return;                     /* already connected */
   GNUNET_assert (h->addr == NULL);
   /* try to connect */
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Trying to connect using address `%s:%u/%s:%u'\n",
-              h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, h->port,
+       GNUNET_a2s (addr, addrlen), h->port);
   ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
   ap->addr = (const struct sockaddr *) &ap[1];
   memcpy (&ap[1], addr, addrlen);
@@ -852,18 +817,18 @@ try_connect_using_address (void *cls,
     GNUNET_free (ap);
     return;                     /* not supported by OS */
   }
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Trying to connect to `%s' (%p)\n"),
-              GNUNET_a2s (ap->addr, ap->addrlen), h);
-#endif
-  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
-                                                   ap->addr,
-                                                   ap->addrlen)) &&
+  LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
+       GNUNET_a2s (ap->addr, ap->addrlen), h);
+  if ((GNUNET_OK !=
+       GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
       (errno != EINPROGRESS))
   {
     /* maybe refused / unsupported address, try next */
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
+#if 0
+    LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to `%s' (%p)\n"),
+         GNUNET_a2s (ap->addr, ap->addrlen), h);
+#endif
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
     GNUNET_free (ap);
     return;
@@ -871,13 +836,15 @@ try_connect_using_address (void *cls,
   GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
   delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
   if (h->nth.notify_ready != NULL)
-    delay = GNUNET_TIME_relative_min (delay,
-                                      GNUNET_TIME_absolute_get_remaining
-                                      (h->nth.transmit_timeout));
+    delay =
+        GNUNET_TIME_relative_min (delay,
+                                  GNUNET_TIME_absolute_get_remaining (h->
+                                                                      nth.transmit_timeout));
   if (h->receiver != NULL)
-    delay = GNUNET_TIME_relative_min (delay,
-                                      GNUNET_TIME_absolute_get_remaining
-                                      (h->receive_timeout));
+    delay =
+        GNUNET_TIME_relative_min (delay,
+                                  GNUNET_TIME_absolute_get_remaining
+                                  (h->receive_timeout));
   ap->task =
       GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
                                       &connect_probe_continuation, ap);
@@ -895,9 +862,9 @@ try_connect_using_address (void *cls,
  * @return the socket handle
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_connect (const struct
-                                       GNUNET_CONFIGURATION_Handle *cfg,
-                                       const char *hostname, uint16_t port)
+GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
+                                       *cfg, const char *hostname,
+                                       uint16_t port)
 {
   struct GNUNET_CONNECTION_Handle *ret;
 
@@ -908,10 +875,10 @@ GNUNET_CONNECTION_create_from_connect (const struct
   ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
   ret->port = port;
   ret->hostname = GNUNET_strdup (hostname);
-  ret->dns_active = GNUNET_RESOLVER_ip_get (ret->hostname,
-                                            AF_UNSPEC,
-                                            GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
-                                            &try_connect_using_address, ret);
+  ret->dns_active =
+      GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
+                              GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
+                              &try_connect_using_address, ret);
   return ret;
 }
 
@@ -966,8 +933,8 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
     GNUNET_free (ret);
     return NULL;
   }
-  if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
-                                                  ret->addr, ret->addrlen))
+  if (GNUNET_OK !=
+      GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
   {
     /* Just return; we expect everything to work eventually so don't fail HARD */
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
@@ -1004,18 +971,16 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
   s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
   if (s == NULL)
   {
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
-                         GNUNET_ERROR_TYPE_BULK, "socket");
+    LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket");
     return NULL;
   }
-  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
-      && (errno != EINPROGRESS))
+  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
+      (errno != EINPROGRESS))
   {
     /* maybe refused / unsupported address, try next */
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                _("Attempt to connect to `%s' failed\n"),
-                GNUNET_a2s (serv_addr, addrlen));
+    LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
+    LOG (GNUNET_ERROR_TYPE_INFO, _("Attempt to connect to `%s' failed\n"),
+         GNUNET_a2s (serv_addr, addrlen));
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
     return NULL;
   }
@@ -1023,11 +988,8 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
   ret->addr = GNUNET_malloc (addrlen);
   memcpy (ret->addr, serv_addr, addrlen);
   ret->addrlen = addrlen;
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Trying to connect to `%s' (%p)\n"),
-              GNUNET_a2s (serv_addr, addrlen), ret);
-#endif
+  LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
+       GNUNET_a2s (serv_addr, addrlen), ret);
   return ret;
 }
 
@@ -1052,7 +1014,7 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
 /**
  * Close the socket and free associated resources. Pending
  * transmissions may be completed or dropped depending on the
- * arguments.   If a receive call is pending and should 
+ * arguments.   If a receive call is pending and should
  * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
  * should be called explicitly first.
  *
@@ -1081,7 +1043,6 @@ GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
     GNUNET_RESOLVER_request_cancel (sock->dns_active);
     sock->dns_active = NULL;
   }
-
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
   sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
 }
@@ -1095,10 +1056,8 @@ signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
 {
   GNUNET_CONNECTION_Receiver receiver;
 
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Network signals time out to receiver (%p)!\n", sh);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Network signals time out to receiver (%p)!\n",
+       sh);
   GNUNET_assert (NULL != (receiver = sh->receiver));
   sh->receiver = NULL;
   receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
@@ -1137,10 +1096,8 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
   {
     /* ignore shutdown request, go again immediately */
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Ignoring shutdown signal per configuration\n");
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Ignoring shutdown signal per configuration\n");
     sh->read_task =
         GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
                                        (sh->receive_timeout), sh->sock,
@@ -1152,24 +1109,20 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
   {
-#if DEBUG_CONNECTION
     if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Receive from `%s' encounters error: time out by %llums... (%p)\n",
-                  GNUNET_a2s (sh->addr, sh->addrlen),
-                  GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
-                  rel_value, sh);
-#endif
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Receive from `%s' encounters error: time out by %llums... (%p)\n",
+           GNUNET_a2s (sh->addr, sh->addrlen),
+           GNUNET_TIME_absolute_get_duration (sh->receive_timeout).rel_value,
+           sh);
     signal_timeout (sh);
     return;
   }
   if (sh->sock == NULL)
   {
     /* connect failed for good */
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Receive encounters error, socket closed... (%p)\n", sh);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Receive encounters error, socket closed... (%p)\n", sh);
     signal_error (sh, ECONNREFUSED);
     return;
   }
@@ -1180,19 +1133,13 @@ RETRY:
   {
     if (errno == EINTR)
       goto RETRY;
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Error receiving: %s\n", STRERROR (errno));
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno));
     signal_error (sh, errno);
     return;
   }
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "receive_ready read %u/%u bytes from `%s' (%p)!\n",
-              (unsigned int) ret,
-              sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "receive_ready read %u/%u bytes from `%s' (%p)!\n", (unsigned int) ret,
+       sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
   GNUNET_assert (NULL != (receiver = sh->receiver));
   sh->receiver = NULL;
   receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
@@ -1219,10 +1166,8 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (sh->sock == NULL)
   {
     /* not connected and no longer trying */
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Receive encounters error, socket closed (%p)...\n", sh);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Receive encounters error, socket closed (%p)...\n", sh);
     signal_error (sh, ECONNREFUSED);
     return;
   }
@@ -1230,10 +1175,8 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if ((now.abs_value > sh->receive_timeout.abs_value) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Receive encounters error: time out (%p)...\n", sh);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Receive encounters error: time out (%p)...\n", sh);
     signal_timeout (sh);
     return;
   }
@@ -1260,8 +1203,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param receiver_cls closure for receiver
  */
 void
-GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
-                           size_t max,
+GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
                            struct GNUNET_TIME_Relative timeout,
                            GNUNET_CONNECTION_Receiver receiver,
                            void *receiver_cls)
@@ -1358,15 +1300,16 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
   if (sock->write_buffer_size - sock->write_buffer_off < size)
   {
     /* need to compact */
-    memmove (sock->write_buffer,
-             &sock->write_buffer[sock->write_buffer_pos], used);
+    memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos],
+             used);
     sock->write_buffer_off -= sock->write_buffer_pos;
     sock->write_buffer_pos = 0;
   }
   avail = sock->write_buffer_size - sock->write_buffer_off;
   GNUNET_assert (avail >= size);
-  size = notify (sock->nth.notify_ready_cls,
-                 avail, &sock->write_buffer[sock->write_buffer_off]);
+  size =
+      notify (sock->nth.notify_ready_cls, avail,
+              &sock->write_buffer[sock->write_buffer_off]);
   GNUNET_assert (size <= avail);
   sock->write_buffer_off += size;
   return GNUNET_YES;
@@ -1390,16 +1333,11 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_CONNECTION_Handle *sock = cls;
   GNUNET_CONNECTION_TransmitReadyNotify notify;
 
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock);
-#endif
   sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
-              sock->hostname,
-              sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
+       sock->hostname,
+       sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
   GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
   sock->ccs -= COCO_TRANSMIT_READY;     /* remove request */
   notify = sock->nth.notify_ready;
@@ -1423,11 +1361,9 @@ connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_CONNECTION_Handle *sock = cls;
   GNUNET_CONNECTION_TransmitReadyNotify notify;
 
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
-              sock->nth.notify_size, sock->hostname, sock->port, sock);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
+       sock->nth.notify_size, sock->hostname, sock->port, sock);
   sock->write_task = GNUNET_SCHEDULER_NO_TASK;
   notify = sock->nth.notify_ready;
   sock->nth.notify_ready = NULL;
@@ -1450,6 +1386,7 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock)
     GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
     sock->sock = NULL;
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task);
   }
   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
   {
@@ -1482,21 +1419,17 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   ssize_t ret;
   size_t have;
 
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
   GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
   sock->write_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
-    if (sock->ignore_shutdown == GNUNET_YES)
+    if ((sock->ignore_shutdown == GNUNET_YES) && (NULL != sock->sock))
       goto SCHEDULE_WRITE;      /* ignore shutdown, go again immediately */
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Transmit to `%s' fails, shutdown happened (%p).\n",
-                GNUNET_a2s (sock->addr, sock->addrlen), sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Transmit to `%s' fails, shutdown happened (%p).\n",
+         GNUNET_a2s (sock->addr, sock->addrlen), sock);
     notify = sock->nth.notify_ready;
     if (NULL != notify)
     {
@@ -1507,11 +1440,9 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Transmit to `%s' fails, time out reached (%p).\n",
-                GNUNET_a2s (sock->addr, sock->addrlen), sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Transmit to `%s' fails, time out reached (%p).\n",
+         GNUNET_a2s (sock->addr, sock->addrlen), sock);
     notify = sock->nth.notify_ready;
     GNUNET_assert (NULL != notify);
     sock->nth.notify_ready = NULL;
@@ -1528,12 +1459,11 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                _
-                ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
-                sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         _
+         ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
+         sock);
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task);
     transmit_error (sock);
     return;                     /* connect failed for good, we're finished */
   }
@@ -1541,8 +1471,8 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if ((sock->nth.notify_ready != NULL) &&
       (sock->write_buffer_size < sock->nth.notify_size))
   {
-    sock->write_buffer = GNUNET_realloc (sock->write_buffer,
-                                         sock->nth.notify_size);
+    sock->write_buffer =
+        GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
     sock->write_buffer_size = sock->nth.notify_size;
   }
   process_notify (sock);
@@ -1556,32 +1486,26 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
 RETRY:
-  ret = GNUNET_NETWORK_socket_send (sock->sock,
-                                    &sock->write_buffer[sock->write_buffer_pos],
-                                    have);
+  ret =
+      GNUNET_NETWORK_socket_send (sock->sock,
+                                  &sock->write_buffer[sock->write_buffer_pos],
+                                  have);
   if (ret == -1)
   {
     if (errno == EINTR)
       goto RETRY;
-#if 0
-    int en = errno;
-
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Failed to send to `%s': %s\n"),
-                GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en));
-#endif
-#if DEBUG_CONNECTION
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
-#endif
+    LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
+    if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
+    {
+      GNUNET_SCHEDULER_cancel (sock->write_task);
+      sock->write_task = GNUNET_SCHEDULER_NO_TASK;
+    }
     transmit_error (sock);
     return;
   }
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
-              (unsigned int) ret,
-              have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
+       (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
   sock->write_buffer_pos += ret;
   if (sock->write_buffer_pos == sock->write_buffer_off)
   {
@@ -1593,17 +1517,14 @@ RETRY:
     return;                     /* all data sent! */
   /* not done writing, schedule more */
 SCHEDULE_WRITE:
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
-#endif
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
   have = sock->write_buffer_off - sock->write_buffer_pos;
   GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
   if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
     sock->write_task =
-        GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == NULL)
-                                        ? GNUNET_TIME_UNIT_FOREVER_REL
-                                        :
+        GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
+                                         NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
                                         GNUNET_TIME_absolute_get_remaining
                                         (sock->nth.transmit_timeout),
                                         sock->sock, &transmit_ready, sock);
@@ -1625,8 +1546,8 @@ SCHEDULE_WRITE:
  *         NULL if we are already going to notify someone else (busy)
  */
 struct GNUNET_CONNECTION_TransmitHandle *
-GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
-                                         *sock, size_t size,
+GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
+                                         size_t size,
                                          struct GNUNET_TIME_Relative timeout,
                                          GNUNET_CONNECTION_TransmitReadyNotify
                                          notify, void *notify_cls)
@@ -1647,8 +1568,8 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
   sock->nth.notify_size = size;
   sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
-  if ((sock->sock == NULL) &&
-      (sock->ap_head == NULL) && (sock->dns_active == NULL))
+  if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
+      (sock->dns_active == NULL))
   {
     if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
       GNUNET_SCHEDULER_cancel (sock->write_task);
@@ -1659,10 +1580,7 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
     return &sock->nth;
   if (sock->sock != NULL)
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Scheduling transmit_ready (%p).\n", sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", sock);
     sock->write_task =
         GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
                                         (sock->nth.transmit_timeout),
@@ -1670,15 +1588,11 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
   }
   else
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
-                sock);
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", sock);
     sock->ccs |= COCO_TRANSMIT_READY;
-    sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
-                                                           &transmit_timeout,
-                                                           sock);
+    sock->nth.timeout_task =
+        GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
   }
   return &sock->nth;
 }
@@ -1692,28 +1606,26 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
 void
 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
                                                 GNUNET_CONNECTION_TransmitHandle
-                                                *h)
+                                                *th)
 {
-  GNUNET_assert (h->notify_ready != NULL);
-  if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
+  GNUNET_assert (th->notify_ready != NULL);
+  if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
   {
-#if DEBUG_CONNECTION
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "notify_transmit_ready_cancel cancels timeout_task (%p)\n", h);
-#endif
-    GNUNET_SCHEDULER_cancel (h->timeout_task);
-    h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-    h->sh->ccs -= COCO_TRANSMIT_READY;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
+    GNUNET_SCHEDULER_cancel (th->timeout_task);
+    th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    th->sh->ccs -= COCO_TRANSMIT_READY;
   }
   else
   {
-    if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
+    if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
     {
-      GNUNET_SCHEDULER_cancel (h->sh->write_task);
-      h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
+      GNUNET_SCHEDULER_cancel (th->sh->write_task);
+      th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
     }
   }
-  h->notify_ready = NULL;
+  th->notify_ready = NULL;
 }
 
 /* end of connection.c */