-ecc sign/verify only
[oweals/gnunet.git] / src / util / connection.c
index 8d0042b7bdc34fa14bc99f62d4220beab0cda51e..d7ae12fb3e09778e3ac0cbbafe2ccfa8b1e7a525 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 
 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
 
-/**
- * Possible functions to call after connect failed or succeeded.
- */
-enum ConnectContinuations
-{
-    /**
-     * Call nothing.
-     */
-  COCO_NONE = 0,
-
-    /**
-     * Call "receive_again".
-     */
-  COCO_RECEIVE_AGAIN = 1,
-
-    /**
-     * Call "transmit_ready".
-     */
-  COCO_TRANSMIT_READY = 2,
-
-    /**
-     * Call "destroy_continuation".
-     */
-  COCO_DESTROY_CONTINUATION = 4
-};
-
 
 /**
  * Transmission handle.  There can only be one for each connection.
@@ -242,11 +216,6 @@ struct GNUNET_CONNECTION_Handle
    */
   GNUNET_SCHEDULER_TaskIdentifier write_task;
 
-  /**
-   * Destroy task (if already scheduled).
-   */
-  GNUNET_SCHEDULER_TaskIdentifier destroy_task;
-
   /**
    * Handle to a pending DNS lookup request.
    */
@@ -262,21 +231,11 @@ struct GNUNET_CONNECTION_Handle
    */
   struct GNUNET_TIME_Absolute receive_timeout;
 
-  /**
-   * Functions to call after connect failed or succeeded.
-   */
-  enum ConnectContinuations ccs;
-
   /**
    * Maximum number of bytes to read (for receiving).
    */
   size_t max;
 
-  /**
-   * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this connection.
-   */
-  int ignore_shutdown;
-
   /**
    * Port to connect to.
    */
@@ -288,10 +247,18 @@ struct GNUNET_CONNECTION_Handle
    * termination as a signal (because only then will the leaked
    * socket be freed!)
    */
-  int16_t persist;
+  int8_t persist;
+
+  /**
+   * Usually 0.  Set to 1 if this handle is in used and should
+   * 'GNUNET_CONNECTION_destroy' be called right now, the action needs
+   * to be deferred by setting it to -1.
+   */
+  int8_t destroy_later;
 
 };
 
+
 /**
  * Set the persist option on this connection handle.  Indicates
  * that the underlying socket or fd should never really be closed.
@@ -370,7 +337,6 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
   void *uaddr;
   struct GNUNET_CONNECTION_Credentials *gcp;
   struct GNUNET_CONNECTION_Credentials gc;
-
 #ifdef SO_PEERCRED
   struct ucred uc;
   socklen_t olen;
@@ -393,7 +359,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
 
   sa = (struct sockaddr *) addr;
   v6 = (struct sockaddr_in6 *) addr;
-  if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
+  if ((AF_INET6 == sa->sa_family) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
   {
     /* convert to V4 address */
     v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
@@ -418,7 +384,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
   gcp = NULL;
   gc.uid = 0;
   gc.gid = 0;
-  if (sa->sa_family == AF_UNIX)
+  if (AF_UNIX == sa->sa_family)
   {
 #if HAVE_GETPEEREID
     /* most BSDs */
@@ -454,10 +420,10 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
 #endif
   }
 
-  if ((access != NULL) &&
+  if ((NULL != access) &&
       (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
   {
-    if (aret == GNUNET_NO)
+    if (GNUNET_NO == aret)
       LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
            GNUNET_a2s (uaddr, addrlen));
     GNUNET_break (GNUNET_OK ==
@@ -491,7 +457,7 @@ int
 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
                                void **addr, size_t * addrlen)
 {
-  if ((connection->addr == NULL) || (connection->addrlen == 0))
+  if ((NULL == connection->addr) || (0 == connection->addrlen))
     return GNUNET_NO;
   *addr = GNUNET_malloc (connection->addrlen);
   memcpy (*addr, connection->addr, connection->addrlen);
@@ -501,113 +467,125 @@ GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
 
 
 /**
- * This function is called after establishing a connection either has
- * succeeded or timed out.  Note that it is possible that the attempt
- * timed out and that we're immediately retrying.  If we are retrying,
- * we need to wait again (or timeout); if we succeeded, we need to
- * wait for data (or timeout).
+ * Tell the receiver callback that we had an IO error.
  *
- * @param cls our connection handle
- * @param tc task context describing why we are here
+ * @param connection connection to signal error
+ * @param errcode error code to send
  */
 static void
-receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+signal_receive_error (struct GNUNET_CONNECTION_Handle *connection, int errcode)
+{
+  GNUNET_CONNECTION_Receiver receiver;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Receive encounters error (%s), connection closed (%p)\n", 
+       STRERROR (errcode),
+       connection);
+  GNUNET_assert (NULL != (receiver = connection->receiver));
+  connection->receiver = NULL;
+  receiver (connection->receiver_cls, NULL, 0, connection->addr, connection->addrlen, errcode);
+}
 
 
 /**
- * Scheduler let us know that the connect task is finished (or was
- * cancelled due to shutdown).  Now really clean up.
+ * Tell the receiver callback that a timeout was reached.
  *
- * @param cls our "struct GNUNET_CONNECTION_Handle *"
- * @param tc unused
+ * @param connection connection to signal for
  */
 static void
-destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+signal_receive_timeout (struct GNUNET_CONNECTION_Handle *connection)
+{
+  GNUNET_CONNECTION_Receiver receiver;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection signals timeout to receiver (%p)!\n",
+       connection);
+  GNUNET_assert (NULL != (receiver = connection->receiver));
+  connection->receiver = NULL;
+  receiver (connection->receiver_cls, NULL, 0, NULL, 0, 0);
+}
+
+
+/**
+ * We failed to transmit data to the service, signal the error.
+ *
+ * @param connection handle that had trouble
+ * @param ecode error code (errno)
+ */
+static void
+signal_transmit_error (struct GNUNET_CONNECTION_Handle *connection,
+                      int ecode)
 {
-  struct GNUNET_CONNECTION_Handle *connection = cls;
   GNUNET_CONNECTION_TransmitReadyNotify notify;
-  struct AddressProbe *pos;
 
-  connection->destroy_task = GNUNET_SCHEDULER_NO_TASK;
-  GNUNET_assert (connection->dns_active == NULL);
-  if (0 != (connection->ccs & COCO_TRANSMIT_READY))
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy waits for CCS-TR to be done (%p)\n",
-         connection);
-    connection->ccs |= COCO_DESTROY_CONTINUATION;
-    return;
-  }
-  if (connection->write_task != GNUNET_SCHEDULER_NO_TASK)
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Transmission encounterd error (%s), connection closed (%p)\n",
+       STRERROR (ecode),
+       connection);
+  if (NULL != connection->sock)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Destroy waits for write_task to be done (%p)\n", connection);
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
-    connection->destroy_task =
-        GNUNET_SCHEDULER_add_after (connection->write_task, &destroy_continuation,
-                                    connection);
-    return;
+    GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR);
+    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
+    connection->sock = NULL;
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
   }
-  if (0 != (connection->ccs & COCO_RECEIVE_AGAIN))
+  if (GNUNET_SCHEDULER_NO_TASK != connection->read_task)
   {
-    connection->ccs |= COCO_DESTROY_CONTINUATION;
+    /* send errors trigger read errors... */
+    GNUNET_SCHEDULER_cancel (connection->read_task);
+    connection->read_task = GNUNET_SCHEDULER_NO_TASK;
+    signal_receive_timeout (connection);
     return;
   }
-  if (connection->sock != NULL)
+  if (NULL == connection->nth.notify_ready)
+    return;                     /* nobody to tell about it */
+  notify = connection->nth.notify_ready;
+  connection->nth.notify_ready = NULL;
+  notify (connection->nth.notify_ready_cls, 0, NULL);
+}
+
+
+/**
+ * We've failed for good to establish a connection (timeout or
+ * no more addresses to try).
+ *
+ * @param connection the connection we tried to establish
+ */
+static void
+connect_fail_continuation (struct GNUNET_CONNECTION_Handle *connection)
+{
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
+       connection->hostname, connection->port);
+  GNUNET_break (NULL == connection->ap_head);
+  GNUNET_break (NULL == connection->ap_tail);
+  GNUNET_break (GNUNET_NO == connection->dns_active);
+  GNUNET_break (NULL == connection->sock);
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
+
+  /* signal errors for jobs that used to wait on the connection */
+  connection->destroy_later = 1;
+  if (NULL != connection->receiver)
+    signal_receive_error (connection, ECONNREFUSED);
+  if (NULL != connection->nth.notify_ready)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection);
-    if (connection->persist != GNUNET_YES)
-    {
-      if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR))
-          && (errno != ENOTCONN) && (errno != ECONNRESET))
-        LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
-    }
+    GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
+    GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
+    connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    signal_transmit_error (connection, ECONNREFUSED);
   }
-  if (connection->read_task != GNUNET_SCHEDULER_NO_TASK)
+  if (-1 == connection->destroy_later)
   {
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
-    connection->destroy_task =
-        GNUNET_SCHEDULER_add_after (connection->read_task, &destroy_continuation,
-                                    connection);
+    /* do it now */
+    connection->destroy_later = 0;
+    GNUNET_CONNECTION_destroy (connection);
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", connection);
-  while (NULL != (pos = connection->ap_head))
-  {
-    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
-    GNUNET_SCHEDULER_cancel (pos->task);
-    GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, pos);
-    GNUNET_free (pos);
-  }
-  GNUNET_assert (connection->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
-  GNUNET_assert (connection->ccs == COCO_NONE);
-  if (NULL != (notify = connection->nth.notify_ready))
-  {
-    connection->nth.notify_ready = NULL;
-    notify (connection->nth.notify_ready_cls, 0, NULL);
-  }
-
-  if (connection->sock != NULL)
-  {
-    if (connection->persist != GNUNET_YES)
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
-    else
-      GNUNET_free (connection->sock); /* at least no memory leak (we deliberately
-                                 * leak the socket in this special case) ... */
-  }
-  GNUNET_free_non_null (connection->addr);
-  GNUNET_free_non_null (connection->hostname);
-  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", connection);
-  GNUNET_free (connection->write_buffer);
-  GNUNET_free (connection);
+  connection->destroy_later = 0;
 }
 
 
-
 /**
- * See if we are now connected.  If not, wait longer for
- * connect to succeed.  If connected, we should be able
- * to write now as well, unless we timed out.
+ * We are ready to transmit (or got a timeout).
  *
  * @param cls our connection handle
  * @param tc task context describing why we are here
@@ -617,52 +595,14 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
 /**
- * We've failed for good to establish a connection.
+ * This function is called once we either timeout or have data ready
+ * to read.
  *
- * @param h the connection we tried to establish
+ * @param cls connection to read from
+ * @param tc scheduler context
  */
 static void
-connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
-{
-  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);
-  GNUNET_break (h->dns_active == GNUNET_NO);
-  GNUNET_break (h->sock == NULL);
-
-  /* trigger jobs that used to wait on "connect_task" */
-  if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
-  {
-    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))
-  {
-    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;
-    h->ccs -= COCO_TRANSMIT_READY;
-    GNUNET_assert (h->nth.notify_ready != NULL);
-    GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
-    h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
-  }
-  if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
-  {
-    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);
-  }
-}
+receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
 /**
@@ -673,40 +613,35 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
 static void
 connect_success_continuation (struct GNUNET_CONNECTION_Handle *connection)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+       "Connection to `%s' succeeded! (%p)\n",
        GNUNET_a2s (connection->addr, connection->addrlen), connection);
   /* trigger jobs that waited for the connection */
-  if (0 != (connection->ccs & COCO_RECEIVE_AGAIN))
+  if (NULL != connection->receiver)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "connect_success_continuation runs receive_again (%p)\n", connection);
-    connection->ccs -= COCO_RECEIVE_AGAIN;
-    connection->read_task = GNUNET_SCHEDULER_add_now (&receive_again, connection);
+         "Connection succeeded, starting with receiving data (%p)\n", 
+        connection);
+    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->read_task);
+    connection->read_task =
+      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
+                                     (connection->receive_timeout), connection->sock,
+                                     &receive_ready, connection);
   }
-  if (0 != (connection->ccs & COCO_TRANSMIT_READY))
+  if (NULL != connection->nth.notify_ready)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
+         "Connection succeeded, starting with sending data (%p)\n",
          connection);
     GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
     GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
     connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
-    connection->ccs -= COCO_TRANSMIT_READY;
     GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK);
-    GNUNET_assert (connection->nth.notify_ready != NULL);
     connection->write_task =
         GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
                                         (connection->nth.transmit_timeout), connection->sock,
                                         &transmit_ready, connection);
   }
-  if (0 != (connection->ccs & COCO_DESTROY_CONTINUATION))
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "connect_success_continuation runs destroy_continuation (%p)\n", connection);
-    connection->ccs -= COCO_DESTROY_CONTINUATION;
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
-    connection->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, connection);
-  }
 }
 
 
@@ -727,7 +662,7 @@ connect_probe_continuation (void *cls,
   int error;
   socklen_t len;
 
-  GNUNET_assert (ap->sock != NULL);
+  GNUNET_assert (NULL != ap->sock);
   GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, ap);
   len = sizeof (error);
   errno = 0;
@@ -735,17 +670,17 @@ connect_probe_continuation (void *cls,
   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))
+                                         &len)) || (0 != error))
   {
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
     GNUNET_free (ap);
-    if ((NULL == connection->ap_head) && (connection->dns_active == GNUNET_NO))
+    if ((NULL == connection->ap_head) && (GNUNET_NO == connection->dns_active))
       connect_fail_continuation (connection);
     return;
   }
-  GNUNET_assert (connection->sock == NULL);
+  GNUNET_assert (NULL == connection->sock);
   connection->sock = ap->sock;
-  GNUNET_assert (connection->addr == NULL);
+  GNUNET_assert (NULL == connection->addr);
   connection->addr = GNUNET_malloc (ap->addrlen);
   memcpy (connection->addr, ap->addr, ap->addrlen);
   connection->addrlen = ap->addrlen;
@@ -778,16 +713,16 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
   struct AddressProbe *ap;
   struct GNUNET_TIME_Relative delay;
 
-  if (addr == NULL)
+  if (NULL == addr)
   {
     connection->dns_active = NULL;
     if ((NULL == connection->ap_head) && (NULL == connection->sock))
       connect_fail_continuation (connection);
     return;
   }
-  if (connection->sock != NULL)
+  if (NULL != connection->sock)
     return;                     /* already connected */
-  GNUNET_assert (connection->addr == NULL);
+  GNUNET_assert (NULL == connection->addr);
   /* try to connect */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Trying to connect using address `%s:%u/%s:%u'\n", connection->hostname, connection->port,
@@ -812,7 +747,7 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
     return;                     /* not supported by us */
   }
   ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
-  if (ap->sock == NULL)
+  if (NULL == ap->sock)
   {
     GNUNET_free (ap);
     return;                     /* not supported by OS */
@@ -821,26 +756,22 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
        GNUNET_a2s (ap->addr, ap->addrlen), connection);
   if ((GNUNET_OK !=
        GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
-      (errno != EINPROGRESS))
+      (EINPROGRESS != errno))
   {
     /* maybe refused / unsupported address, try next */
     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), connection);
-#endif
     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
     GNUNET_free (ap);
     return;
   }
   GNUNET_CONTAINER_DLL_insert (connection->ap_head, connection->ap_tail, ap);
   delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
-  if (connection->nth.notify_ready != NULL)
+  if (NULL != connection->nth.notify_ready)
     delay =
         GNUNET_TIME_relative_min (delay,
                                   GNUNET_TIME_absolute_get_remaining (connection->
                                                                       nth.transmit_timeout));
-  if (connection->receiver != NULL)
+  if (NULL != connection->receiver)
     delay =
         GNUNET_TIME_relative_min (delay,
                                   GNUNET_TIME_absolute_get_remaining
@@ -967,15 +898,14 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
   struct GNUNET_NETWORK_Handle *s;
   struct GNUNET_CONNECTION_Handle *connection;
 
-
   s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
-  if (s == NULL)
+  if (NULL == s)
   {
     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))
+      (EINPROGRESS != errno))
   {
     /* maybe refused / unsupported address, try next */
     LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
@@ -999,135 +929,139 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
  * Note that a connection that is still trying to connect is considered
  * valid.
  *
- * @param sock connection to check
+ * @param connection connection to check
  * @return GNUNET_YES if valid, GNUNET_NO otherwise
  */
 int
-GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
+GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection)
 {
-  if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
+  if ((NULL != connection->ap_head) || (NULL != connection->dns_active))
     return GNUNET_YES;          /* still trying to connect */
-  return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
+  return (NULL == connection->sock) ? GNUNET_NO : GNUNET_YES;
 }
 
 
 /**
- * Close the connection and free associated resources.  A pending
- * request for transmission is automatically cancelled (we might
- * want to change this in the future).  We require that there
- * are no active pending requests for reading from the connection.
+ * Close the connection and free associated resources.  There must
+ * not be any pending requests for reading or writing to the
+ * connection at this time.
  *
  * @param connection connection to destroy
  */
 void
 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection)
 {
+  struct AddressProbe *pos;
+
+  if (0 != connection->destroy_later)
+  {
+    connection->destroy_later = -1;
+    return;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection);
+  GNUNET_assert (NULL == connection->nth.notify_ready);
   GNUNET_assert (NULL == connection->receiver);
-  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->read_task);
-  if (connection->write_task != GNUNET_SCHEDULER_NO_TASK)
+  if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
   {
     GNUNET_SCHEDULER_cancel (connection->write_task);
     connection->write_task = GNUNET_SCHEDULER_NO_TASK;
     connection->write_buffer_off = 0;
   }
+  if (GNUNET_SCHEDULER_NO_TASK != connection->read_task)
+  {
+    GNUNET_SCHEDULER_cancel (connection->read_task);
+    connection->read_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != connection->nth.timeout_task)
+  {
+    GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
+    connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  }
   connection->nth.notify_ready = NULL;
-  if (connection->dns_active != NULL)
+  if (NULL != connection->dns_active)
   {
     GNUNET_RESOLVER_request_cancel (connection->dns_active);
     connection->dns_active = NULL;
   }
-  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
-  connection->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, connection);
-}
-
-
-/**
- * Tell the receiver callback that a timeout was reached.
- */
-static void
-signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
-{
-  GNUNET_CONNECTION_Receiver receiver;
-
-  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);
-}
-
-
-/**
- * Tell the receiver callback that we had an IO error.
- */
-static void
-signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
-{
-  GNUNET_CONNECTION_Receiver receiver;
-
-  GNUNET_assert (NULL != (receiver = sh->receiver));
-  sh->receiver = NULL;
-  receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
+  while (NULL != (pos = connection->ap_head))
+  {
+    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
+    GNUNET_SCHEDULER_cancel (pos->task);
+    GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, pos);
+    GNUNET_free (pos);
+  }
+  if ( (NULL != connection->sock) &&
+       (GNUNET_YES != connection->persist) )
+  {
+    if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR)) && 
+       (ENOTCONN != errno) && 
+       (ECONNRESET != errno) )
+      LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");    
+  }
+  if (NULL != connection->sock)
+  {
+    if (GNUNET_YES != connection->persist)
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
+    else
+      GNUNET_free (connection->sock); /* at least no memory leak (we deliberately
+                                      * leak the socket in this special case) ... */
+  }
+  GNUNET_free_non_null (connection->addr);
+  GNUNET_free_non_null (connection->hostname);
+  GNUNET_free (connection->write_buffer);
+  GNUNET_free (connection);
 }
 
 
 /**
  * This function is called once we either timeout
  * or have data ready to read.
+ *
+ * @param cls connection to read from
+ * @param tc scheduler context
  */
 static void
 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CONNECTION_Handle *connection = cls;
-  struct GNUNET_TIME_Absolute now;
   char buffer[connection->max];
   ssize_t ret;
   GNUNET_CONNECTION_Receiver receiver;
 
   connection->read_task = GNUNET_SCHEDULER_NO_TASK;
-  if ((GNUNET_YES == connection->ignore_shutdown) &&
-      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
     /* ignore shutdown request, go again immediately */
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Ignoring shutdown signal per configuration\n");
     connection->read_task =
         GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
                                        (connection->receive_timeout), connection->sock,
                                        &receive_ready, connection);
     return;
   }
-  now = GNUNET_TIME_absolute_get ();
-  if ((now.abs_value > connection->receive_timeout.abs_value) ||
-      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
-      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
   {
-    if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "Receive from `%s' encounters error: time out by %llums... (%p)\n",
-           GNUNET_a2s (connection->addr, connection->addrlen),
-           GNUNET_TIME_absolute_get_duration (connection->receive_timeout).rel_value,
-           connection);
-    signal_timeout (connection);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Receive from `%s' encounters error: timeout (%p)\n",
+        GNUNET_a2s (connection->addr, connection->addrlen),
+        GNUNET_TIME_absolute_get_duration (connection->receive_timeout).rel_value,
+        connection);
+    signal_receive_timeout (connection);
     return;
   }
-  if (connection->sock == NULL)
+  if (NULL == connection->sock)
   {
     /* connect failed for good */
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receive encounters error, connection closed... (%p)\n", connection);
-    signal_error (connection, ECONNREFUSED);
+    signal_receive_error (connection, ECONNREFUSED);
     return;
   }
   GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, connection->sock));
 RETRY:
   ret = GNUNET_NETWORK_socket_recv (connection->sock, buffer, connection->max);
-  if (ret == -1)
+  if (-1 == ret)
   {
-    if (errno == EINTR)
+    if (EINTR == errno)
       goto RETRY;
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno));
-    signal_error (connection, errno);
+    signal_receive_error (connection, errno);
     return;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1139,49 +1073,6 @@ RETRY:
 }
 
 
-/**
- * This function is called after establishing a connection either has
- * succeeded or timed out.  Note that it is possible that the attempt
- * timed out and that we're immediately retrying.  If we are retrying,
- * we need to wait again (or timeout); if we succeeded, we need to
- * wait for data (or timeout).
- *
- * @param cls our connection handle
- * @param tc task context describing why we are here
- */
-static void
-receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_CONNECTION_Handle *connection = cls;
-  struct GNUNET_TIME_Absolute now;
-
-  connection->read_task = GNUNET_SCHEDULER_NO_TASK;
-  if (connection->sock == NULL)
-  {
-    /* not connected and no longer trying */
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receive encounters error, connection closed (%p)...\n", connection);
-    signal_error (connection, ECONNREFUSED);
-    return;
-  }
-  now = GNUNET_TIME_absolute_get ();
-  if ((now.abs_value > connection->receive_timeout.abs_value) ||
-      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receive encounters error: time out (%p)...\n", connection);
-    signal_timeout (connection);
-    return;
-  }
-  GNUNET_assert (connection->sock != NULL);
-  /* connect succeeded, wait for data! */
-  connection->read_task =
-      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
-                                     (connection->receive_timeout), connection->sock,
-                                     &receive_ready, connection);
-}
-
-
 /**
  * Receive data from the given connection.  Note that this function will
  * call "receiver" asynchronously using the scheduler.  It will
@@ -1191,7 +1082,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *
  * @param connection connection handle
  * @param max maximum number of bytes to read
- * @param timeout maximum amount of time to wait (use -1 for "forever")
+ * @param timeout maximum amount of time to wait
  * @param receiver function to call with received data
  * @param receiver_cls closure for receiver
  */
@@ -1201,42 +1092,27 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection, size_t m
                            GNUNET_CONNECTION_Receiver receiver,
                            void *receiver_cls)
 {
-  struct GNUNET_SCHEDULER_TaskContext tc;
-
-  GNUNET_assert ((connection->read_task == GNUNET_SCHEDULER_NO_TASK) &&
-                 (0 == (connection->ccs & COCO_RECEIVE_AGAIN)) &&
-                 (connection->receiver == NULL));
+  GNUNET_assert ((GNUNET_SCHEDULER_NO_TASK == connection->read_task) &&
+                 (NULL == connection->receiver));
+  GNUNET_assert (NULL != receiver);
   connection->receiver = receiver;
   connection->receiver_cls = receiver_cls;
   connection->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
   connection->max = max;
-  if (connection->sock != NULL)
+  if (NULL != connection->sock)
   {
-    memset (&tc, 0, sizeof (tc));
-    tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
-    receive_again (connection, &tc);
+    connection->read_task =
+      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
+                                     (connection->receive_timeout), connection->sock,
+                                     &receive_ready, connection);
     return;
   }
-  if ((connection->dns_active == NULL) && (connection->ap_head == NULL))
+  if ((NULL == connection->dns_active) && (NULL == connection->ap_head))
   {
+    connection->receiver = NULL;
     receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
     return;
   }
-  connection->ccs += COCO_RECEIVE_AGAIN;
-}
-
-
-/**
- * Configure this connection to ignore shutdown signals.
- *
- * @param connection connection handle
- * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
- */
-void
-GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection,
-                                   int do_ignore)
-{
-  connection->ignore_shutdown = do_ignore;
 }
 
 
@@ -1251,16 +1127,11 @@ GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection,
 void *
 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection)
 {
-  if (connection->read_task != GNUNET_SCHEDULER_NO_TASK)
+  if (GNUNET_SCHEDULER_NO_TASK != connection->read_task)
   {
     GNUNET_assert (connection == GNUNET_SCHEDULER_cancel (connection->read_task));
     connection->read_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  else
-  {
-    GNUNET_assert (0 != (connection->ccs & COCO_RECEIVE_AGAIN));
-    connection->ccs -= COCO_RECEIVE_AGAIN;
-  }
   connection->receiver = NULL;
   return connection->receiver_cls;
 }
@@ -1281,7 +1152,7 @@ process_notify (struct GNUNET_CONNECTION_Handle *connection)
   size_t size;
   GNUNET_CONNECTION_TransmitReadyNotify notify;
 
-  GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK);
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
   if (NULL == (notify = connection->nth.notify_ready))
     return GNUNET_NO;
   used = connection->write_buffer_off - connection->write_buffer_pos;
@@ -1304,7 +1175,8 @@ process_notify (struct GNUNET_CONNECTION_Handle *connection)
       notify (connection->nth.notify_ready_cls, avail,
               &connection->write_buffer[connection->write_buffer_off]);
   GNUNET_assert (size <= avail);
-  connection->write_buffer_off += size;
+  if (0 != size)
+    connection->write_buffer_off += size;
   return GNUNET_YES;
 }
 
@@ -1331,9 +1203,8 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
        "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
        connection->hostname,
        connection->port, GNUNET_a2s (connection->addr, connection->addrlen), connection);
-  GNUNET_assert (0 != (connection->ccs & COCO_TRANSMIT_READY));
-  connection->ccs -= COCO_TRANSMIT_READY;     /* remove request */
   notify = connection->nth.notify_ready;
+  GNUNET_assert (NULL != notify);
   connection->nth.notify_ready = NULL;
   notify (connection->nth.notify_ready_cls, 0, NULL);
 }
@@ -1365,41 +1236,7 @@ connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 
 /**
- * FIXME
- *
- * @param connection FIXME
- */
-static void
-transmit_error (struct GNUNET_CONNECTION_Handle *connection)
-{
-  GNUNET_CONNECTION_TransmitReadyNotify notify;
-
-  if (NULL != connection->sock)
-  {
-    GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR);
-    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
-    connection->sock = NULL;
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
-  }
-  if (connection->read_task != GNUNET_SCHEDULER_NO_TASK)
-  {
-    GNUNET_SCHEDULER_cancel (connection->read_task);
-    connection->read_task = GNUNET_SCHEDULER_NO_TASK;
-    signal_timeout (connection);
-    return;
-  }
-  if (connection->nth.notify_ready == NULL)
-    return;                     /* nobody to tell about it */
-  notify = connection->nth.notify_ready;
-  connection->nth.notify_ready = NULL;
-  notify (connection->nth.notify_ready_cls, 0, NULL);
-}
-
-
-/**
- * See if we are now connected.  If not, wait longer for
- * connect to succeed.  If connected, we should be able
- * to write now as well, unless we timed out.
+ * We are ready to transmit (or got a timeout).
  *
  * @param cls our connection handle
  * @param tc task context describing why we are here
@@ -1413,12 +1250,12 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   size_t have;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", connection);
-  GNUNET_assert (connection->write_task != GNUNET_SCHEDULER_NO_TASK);
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != connection->write_task);
   connection->write_task = GNUNET_SCHEDULER_NO_TASK;
-  GNUNET_assert (connection->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task);
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
-    if ((connection->ignore_shutdown == GNUNET_YES) && (NULL != connection->sock))
+    if (NULL != connection->sock)
       goto SCHEDULE_WRITE;      /* ignore shutdown, go again immediately */
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Transmit to `%s' fails, shutdown happened (%p).\n",
@@ -1443,25 +1280,22 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     return;
   }
   GNUNET_assert (NULL != connection->sock);
-  if (tc->write_ready == NULL)
+  if (NULL == tc->write_ready) 
   {
-    /* special circumstances (in particular,
-     * PREREQ_DONE after connect): not yet ready to write,
-     * but no "fatal" error either.  Hence retry.  */
+    /* special circumstances (in particular, PREREQ_DONE after
+     * connect): not yet ready to write, but no "fatal" error either.
+     * Hence retry.  */
     goto SCHEDULE_WRITE;
   }
   if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, connection->sock))
   {
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         _
-         ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
-         connection);
     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
-    transmit_error (connection);
-    return;                     /* connect failed for good, we're finished */
+    /* special circumstances (in particular, shutdown): not yet ready
+     * to write, but no "fatal" error either.  Hence retry.  */
+    goto SCHEDULE_WRITE;
   }
   GNUNET_assert (connection->write_buffer_off >= connection->write_buffer_pos);
-  if ((connection->nth.notify_ready != NULL) &&
+  if ((NULL != connection->nth.notify_ready) &&
       (connection->write_buffer_size < connection->nth.notify_size))
   {
     connection->write_buffer =
@@ -1470,7 +1304,7 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   process_notify (connection);
   have = connection->write_buffer_off - connection->write_buffer_pos;
-  if (have == 0)
+  if (0 == have)
   {
     /* no data ready for writing, terminate write loop */
     return;
@@ -1483,21 +1317,20 @@ RETRY:
       GNUNET_NETWORK_socket_send (connection->sock,
                                  &connection->write_buffer[connection->write_buffer_pos],
                                  have);
-  if (ret == -1)
+  if (-1 == ret)
   {
-    if (errno == EINTR)
+    if (EINTR == errno)
       goto RETRY;
-    LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
     if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
     {
       GNUNET_SCHEDULER_cancel (connection->write_task);
       connection->write_task = GNUNET_SCHEDULER_NO_TASK;
     }
-    transmit_error (connection);
+    signal_transmit_error (connection, errno);
     return;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
+       "Connection transmitted %u/%u bytes to `%s' (%p)\n",
        (unsigned int) ret, have, GNUNET_a2s (connection->addr, connection->addrlen), connection);
   connection->write_buffer_pos += ret;
   if (connection->write_buffer_pos == connection->write_buffer_off)
@@ -1506,15 +1339,15 @@ RETRY:
     connection->write_buffer_pos = 0;
     connection->write_buffer_off = 0;
   }
-  if ((connection->write_buffer_off == 0) && (NULL == connection->nth.notify_ready))
+  if ((0 == connection->write_buffer_off) && (NULL == connection->nth.notify_ready))
     return;                     /* all data sent! */
   /* not done writing, schedule more */
 SCHEDULE_WRITE:
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Re-scheduling transmit_ready (more to do) (%p).\n", connection);
   have = connection->write_buffer_off - connection->write_buffer_pos;
-  GNUNET_assert ((connection->nth.notify_ready != NULL) || (have > 0));
-  if (connection->write_task == GNUNET_SCHEDULER_NO_TASK)
+  GNUNET_assert ((NULL != connection->nth.notify_ready) || (have > 0));
+  if (GNUNET_SCHEDULER_NO_TASK == connection->write_task)
     connection->write_task =
         GNUNET_SCHEDULER_add_write_net ((connection->nth.notify_ready ==
                                          NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
@@ -1545,12 +1378,12 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connec
                                          GNUNET_CONNECTION_TransmitReadyNotify
                                          notify, void *notify_cls)
 {
-  if (connection->nth.notify_ready != NULL)
+  if (NULL != connection->nth.notify_ready)
   {
     GNUNET_assert (0);
     return NULL;
   }
-  GNUNET_assert (notify != NULL);
+  GNUNET_assert (NULL != notify);
   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
   GNUNET_assert (connection->write_buffer_off <= connection->write_buffer_size);
   GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size);
@@ -1561,32 +1394,32 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connec
   connection->nth.notify_size = size;
   connection->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task);
-  if ((connection->sock == NULL) && (connection->ap_head == NULL) &&
-      (connection->dns_active == NULL))
+  if ((NULL == connection->sock) && 
+      (NULL == connection->ap_head) &&
+      (NULL == connection->dns_active))
   {
-    if (connection->write_task != GNUNET_SCHEDULER_NO_TASK)
+    if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
       GNUNET_SCHEDULER_cancel (connection->write_task);
     connection->write_task = GNUNET_SCHEDULER_add_now (&connect_error, connection);
     return &connection->nth;
   }
   if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
-    return &connection->nth;
-  if (connection->sock != NULL)
+    return &connection->nth; /* previous transmission still in progress */
+  if (NULL != connection->sock)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", connection);
+    /* connected, try to transmit now */
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmission (%p).\n", connection);
     connection->write_task =
         GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
                                         (connection->nth.transmit_timeout),
                                         connection->sock, &transmit_ready, connection);
+    return &connection->nth;
   }
-  else
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", connection);
-    connection->ccs |= COCO_TRANSMIT_READY;
-    connection->nth.timeout_task =
-        GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection);
-  }
+  /* not yet connected, wait for connection */
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Need to wait to schedule transmission for connection, adding timeout task (%p).\n", connection);
+  connection->nth.timeout_task =
+    GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection);
   return &connection->nth;
 }
 
@@ -1601,24 +1434,18 @@ GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
                                                 GNUNET_CONNECTION_TransmitHandle
                                                 *th)
 {
-  GNUNET_assert (th->notify_ready != NULL);
-  if (0 != (th->connection->ccs & COCO_TRANSMIT_READY))
+  GNUNET_assert (NULL != th->notify_ready);
+  th->notify_ready = NULL;
+  if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
   {
-    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->connection->ccs -= COCO_TRANSMIT_READY;
   }
-  else
+  if (GNUNET_SCHEDULER_NO_TASK != th->connection->write_task)
   {
-    if (th->connection->write_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      GNUNET_SCHEDULER_cancel (th->connection->write_task);
-      th->connection->write_task = GNUNET_SCHEDULER_NO_TASK;
-    }
+    GNUNET_SCHEDULER_cancel (th->connection->write_task);
+    th->connection->write_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  th->notify_ready = NULL;
 }
 
 /* end of connection.c */