Tell core that we want to have this packet delivered
[oweals/gnunet.git] / src / util / connection.c
index f88a7b5f2cf60f6364728a5f963088a5a732d8b3..aa5db91bf41e1a14976a4c1ce21f4584635ce5a5 100644 (file)
@@ -37,6 +37,7 @@
 #include "gnunet_container_lib.h"
 #include "gnunet_resolver_service.h"
 #include "gnunet_scheduler_lib.h"
+#include "gnunet_server_lib.h"
 
 #define DEBUG_CONNECTION GNUNET_NO
 
@@ -159,11 +160,6 @@ struct AddressProbe
 struct GNUNET_CONNECTION_Handle
 {
 
-  /**
-   * Scheduler that was used for the connect task.
-   */
-  struct GNUNET_SCHEDULER_Handle *sched;
-
   /**
    * Configuration to use.
    */
@@ -213,7 +209,7 @@ struct GNUNET_CONNECTION_Handle
   char *write_buffer;
 
   /**
-   * Size of our write buffer.
+   * Current size of our write buffer.
    */
   size_t write_buffer_size;
 
@@ -225,7 +221,7 @@ struct GNUNET_CONNECTION_Handle
 
   /**
    * Current read-offset in write buffer (how many
-   * bytes have already been send).
+   * bytes have already been sent).
    */
   size_t write_buffer_pos;
 
@@ -244,6 +240,11 @@ 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.
    */
@@ -269,37 +270,55 @@ struct GNUNET_CONNECTION_Handle
    */
   size_t max;
 
+  /**
+   * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
+   */
+  int ignore_shutdown;
+
   /**
    * Port to connect to.
    */
   uint16_t port;
 
+  /**
+   * When shutdown, do not ever actually close the socket, but
+   * free resources.  Only should ever be set if using program
+   * termination as a signal (because only then will the leaked
+   * socket be freed!)
+   */
+  int16_t persist;
+
 };
 
+/**
+ * Set the persist option on this connection handle.  Indicates
+ * that the underlying socket or fd should never really be closed.
+ * Used for indicating process death.
+ *
+ * @param sock the connection to set persistent
+ */
+void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
+{
+  sock->persist = GNUNET_YES;
+}
 
 /**
  * Create a socket handle by boxing an existing OS socket.  The OS
  * socket should henceforth be no longer used directly.
  * GNUNET_socket_destroy will close it.
  *
- * @param sched scheduler to use
  * @param osSocket existing socket to box
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the boxed socket handle
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
-                                        *sched,
-                                        struct GNUNET_NETWORK_Handle
-                                        *osSocket, size_t maxbuf)
+GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
+                                        *osSocket)
 {
   struct GNUNET_CONNECTION_Handle *ret;
-  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
-  ret->write_buffer = (char *) &ret[1];
-  ret->write_buffer_size = maxbuf;
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
   ret->sock = osSocket;
-  ret->sched = sched;
   return ret;
 }
 
@@ -308,24 +327,18 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
  * Create a socket handle by accepting on a listen socket.  This
  * function may block if the listen socket has no connection ready.
  *
- * @param sched scheduler to use
  * @param access function to use to check if access is allowed
  * @param access_cls closure for access
  * @param lsock listen socket
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the socket handle, NULL on error
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
-                                      *sched,
-                                      GNUNET_CONNECTION_AccessCheck access,
+GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
                                       void *access_cls,
-                                      struct GNUNET_NETWORK_Handle *lsock,
-                                      size_t maxbuf)
+                                      struct GNUNET_NETWORK_Handle *lsock)
 {
   struct GNUNET_CONNECTION_Handle *ret;
-  char addr[32];
+  char addr[128];
   socklen_t addrlen;
   struct GNUNET_NETWORK_Handle *sock;
   int aret;
@@ -342,7 +355,8 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
       return NULL;
     }
-  if (addrlen > sizeof (addr))
+  if ( (addrlen > sizeof (addr)) ||
+       (addrlen < sizeof (sa_family_t)) )
     {
       GNUNET_break (0);
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
@@ -384,13 +398,12 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
       GNUNET_free (uaddr);
       return NULL;
     }
-  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
-  ret->write_buffer = (char *) &ret[1];
-  ret->write_buffer_size = maxbuf;
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
   ret->addr = uaddr;
   ret->addrlen = addrlen;
   ret->sock = sock;
-  ret->sched = sched;
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Accepting connection from `%s': %p\n"),
@@ -420,17 +433,6 @@ GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
 }
 
 
-/**
- * It is time to re-try connecting.
- * 
- * @param cls the handle for the connection that should be re-tried
- * @param tc unused scheduler taks context
- */
-static void
-retry_connect_continuation (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
 /**
  * This function is called after establishing a connection either has
  * succeeded or timed out.  Note that it is possible that the attempt
@@ -458,7 +460,9 @@ destroy_continuation (void *cls,
 {
   struct GNUNET_CONNECTION_Handle *sock = cls;
   GNUNET_CONNECTION_TransmitReadyNotify notify;
+  struct AddressProbe *pos;
 
+  sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_assert (sock->dns_active == NULL);
   if (0 != (sock->ccs & COCO_TRANSMIT_READY))
     {
@@ -475,9 +479,10 @@ destroy_continuation (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Destroy waits for write_task to be done (%p)\n", sock);
 #endif
-      GNUNET_SCHEDULER_add_after (sock->sched,
-                                  sock->write_task,
-                                  &destroy_continuation, sock);
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
+      sock->destroy_task 
+       = GNUNET_SCHEDULER_add_after (sock->write_task,
+                                     &destroy_continuation, sock);
       return;
     }
   if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
@@ -491,18 +496,33 @@ destroy_continuation (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Shutting down socket (%p)\n", sock);
 #endif
-      GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
+      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");
+      }
     }
   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
     {
-      GNUNET_SCHEDULER_add_after (sock->sched,
-                                  sock->read_task,
-                                  &destroy_continuation, sock);
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
+      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);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+             "Destroy actually runs (%p)!\n", sock);
 #endif
+  while (NULL != (pos = sock->ap_head))
+    {
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
+      GNUNET_SCHEDULER_cancel (pos->task);
+      GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
+      GNUNET_free (pos);
+    }
   GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
   GNUNET_assert (sock->ccs == COCO_NONE);
   if (NULL != (notify = sock->nth.notify_ready))
@@ -510,14 +530,23 @@ destroy_continuation (void *cls,
       sock->nth.notify_ready = NULL;
       notify (sock->nth.notify_ready_cls, 0, NULL);
     }
-  if (sock->sock != NULL)
-    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
+
+  if (sock->sock != NULL) 
+    {
+      if (sock->persist != GNUNET_YES)
+       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
+      else
+       GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
+                                    leak the socket in this special case) ... */
+    }
   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
+  GNUNET_free (sock->write_buffer);
   GNUNET_free (sock);
 }
 
@@ -544,9 +573,13 @@ static void
 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
 {
 #if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Failed to establish TCP connection to `%s:%u', no further addresses to try (%p).\n",
-              h->hostname, h->port, h);
+  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
   /* connect failed / timed out */
   GNUNET_break (h->ap_head == NULL);
@@ -554,46 +587,45 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
   GNUNET_break (h->dns_active == GNUNET_NO);
   GNUNET_break (h->sock == NULL);
 
-  /* FIXME: trigger delayed reconnect attempt... */
   /* 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_timeout_continuation triggers receive_again (%p)\n",
+                  "connect_fail_continuation triggers receive_again (%p)\n",
                   h);
 #endif
       h->ccs -= COCO_RECEIVE_AGAIN;
-      h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
-                                                 GNUNET_SCHEDULER_NO_TASK,
+      h->read_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
                                                  &receive_again, h);
     }
   if (0 != (h->ccs & COCO_TRANSMIT_READY))
     {
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "connect_timeout_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
+                  "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
                   h);
 #endif
       GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
-      GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
+      GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
       h->ccs -= COCO_TRANSMIT_READY;
-      h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
-                                                  GNUNET_SCHEDULER_NO_TASK,
+      GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
+      h->write_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
                                                   &transmit_ready, h);
     }
   if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
     {
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "connect_timeout_continuation runs destroy_continuation (%p)\n",
+                  "connect_fail_continuation runs destroy_continuation (%p)\n",
                   h);
 #endif
       h->ccs -= COCO_DESTROY_CONTINUATION;
-      GNUNET_SCHEDULER_add_continuation (h->sched,
-                                         &destroy_continuation,
-                                         h, GNUNET_SCHEDULER_REASON_TIMEOUT);
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
+      h->destroy_task
+       = GNUNET_SCHEDULER_add_now (&destroy_continuation,
+                                   h);
     }
 }
 
@@ -620,8 +652,7 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
                   h);
 #endif
       h->ccs -= COCO_RECEIVE_AGAIN;
-      h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
-                                                 GNUNET_SCHEDULER_NO_TASK,
+      h->read_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
                                                  &receive_again, h);
     }
   if (0 != (h->ccs & COCO_TRANSMIT_READY))
@@ -632,12 +663,12 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
                   h);
 #endif
       GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
-      GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
+      GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
       h->ccs -= COCO_TRANSMIT_READY;
+      GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
       h->write_task =
-        GNUNET_SCHEDULER_add_write_net (h->sched,
-                                        GNUNET_TIME_absolute_get_remaining
+        GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
                                         (h->nth.transmit_timeout), h->sock,
                                         &transmit_ready, h);
     }
@@ -649,10 +680,10 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
                   h);
 #endif
       h->ccs -= COCO_DESTROY_CONTINUATION;
-      GNUNET_SCHEDULER_add_continuation (h->sched,
-                                         &destroy_continuation,
-                                         h,
-                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
+      h->destroy_task
+       = GNUNET_SCHEDULER_add_now (&destroy_continuation,
+                                               h);
     }
 }
 
@@ -672,17 +703,18 @@ connect_probe_continuation (void *cls,
   struct GNUNET_CONNECTION_Handle *h = ap->h;
   struct AddressProbe *pos;
   int error;
-  unsigned int len;
+  socklen_t len;
 
+  GNUNET_assert (ap->sock != NULL);
   GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
   len = sizeof (error);
   errno = 0;
   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)
-      || (errno != 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_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
       GNUNET_free (ap);
@@ -690,6 +722,7 @@ connect_probe_continuation (void *cls,
         connect_fail_continuation (h);
       return;
     }
+  GNUNET_assert (h->sock == NULL);
   h->sock = ap->sock;
   GNUNET_assert (h->addr == NULL);
   h->addr = GNUNET_malloc (ap->addrlen);
@@ -700,7 +733,7 @@ connect_probe_continuation (void *cls,
   while (NULL != (pos = h->ap_head))
     {
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
-      GNUNET_SCHEDULER_cancel (h->sched, pos->task);
+      GNUNET_SCHEDULER_cancel (pos->task);
       GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
       GNUNET_free (pos);
     }
@@ -733,6 +766,7 @@ try_connect_using_address (void *cls,
     }
   if (h->sock != NULL)
     return;                     /* already connected */
+  GNUNET_assert (h->addr == NULL);
   /* try to connect */
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -785,74 +819,116 @@ try_connect_using_address (void *cls,
   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));
+                                      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));
+                                      GNUNET_TIME_absolute_get_remaining
+                                      (h->receive_timeout));
   ap->task =
-    GNUNET_SCHEDULER_add_write_net (h->sched, 
-                                    delay, ap->sock,
+    GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
                                     &connect_probe_continuation, ap);
 }
 
 
-/**
- * It is time to re-try connecting.
- * 
- * @param cls the handle for the connection that should be re-tried
- * @param tc unused scheduler taks context
- */
-static void
-retry_connect_continuation (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_CONNECTION_Handle *sock = cls;
-
-  GNUNET_assert (sock->dns_active == NULL);
-  sock->dns_active = GNUNET_RESOLVER_ip_get (sock->sched,
-                                            sock->cfg,
-                                            sock->hostname,
-                                            AF_UNSPEC,
-                                            GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
-                                            &try_connect_using_address, sock);
-}
-
-
 /**
  * Create a socket handle by (asynchronously) connecting to a host.
  * This function returns immediately, even if the connection has not
  * yet been established.  This function only creates TCP connections.
  *
- * @param sched scheduler to use
  * @param cfg configuration to use
  * @param hostname name of the host to connect to
  * @param port port to connect to
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the socket handle
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
-                                       const struct
+GNUNET_CONNECTION_create_from_connect (const struct
                                        GNUNET_CONFIGURATION_Handle *cfg,
-                                       const char *hostname, uint16_t port,
-                                       size_t maxbuf)
+                                       const char *hostname, uint16_t port)
 {
   struct GNUNET_CONNECTION_Handle *ret;
 
   GNUNET_assert (0 < strlen (hostname));        /* sanity check */
-  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
   ret->cfg = cfg;
-  ret->sched = sched;
-  ret->write_buffer = (char *) &ret[1];
-  ret->write_buffer_size = maxbuf;
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
   ret->port = port;
   ret->hostname = GNUNET_strdup (hostname);
-  retry_connect_continuation (ret, NULL);
+  ret->dns_active = GNUNET_RESOLVER_ip_get (cfg,
+                                            ret->hostname,
+                                            AF_UNSPEC,
+                                            GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
+                                            &try_connect_using_address, ret);
+  return ret;
+}
+
+
+/**
+ * Create a socket handle by connecting to a UNIX domain service.
+ * This function returns immediately, even if the connection has not
+ * yet been established.  This function only creates UNIX connections.
+ *
+ * @param cfg configuration to use
+ * @param unixpath path to connect to
+ * @return the socket handle, NULL on systems without UNIX support
+ */
+struct GNUNET_CONNECTION_Handle *
+GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
+                                                  GNUNET_CONFIGURATION_Handle *cfg,
+                                                  const char *unixpath)
+{
+#ifdef AF_UNIX
+  struct GNUNET_CONNECTION_Handle *ret;
+  struct sockaddr_un *un;
+  size_t slen;
+
+  GNUNET_assert (0 < strlen (unixpath));        /* sanity check */
+  un = GNUNET_malloc (sizeof (struct sockaddr_un));
+  un->sun_family = AF_UNIX;
+  slen = strlen (unixpath) + 1;
+  if (slen >= sizeof (un->sun_path))
+    slen = sizeof (un->sun_path) - 1;
+  memcpy (un->sun_path,
+         unixpath,
+         slen);
+  un->sun_path[slen] = '\0';
+  slen += sizeof (sa_family_t);
+#if LINUX
+  un->sun_path[0] = '\0';
+  slen = sizeof (struct sockaddr_un);
+#endif
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
+  ret->cfg = cfg;
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
+  ret->port = 0;
+  ret->hostname = NULL;
+  ret->addr = (struct sockaddr*) un;
+  ret->addrlen = slen;
+  ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
+  if (NULL == ret->sock)
+    {
+      GNUNET_free (ret->addr);
+      GNUNET_free (ret->write_buffer);
+      GNUNET_free (ret);
+      return NULL;
+    }
+  if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
+                                                 ret->addr,
+                                                 ret->addrlen)) 
+    {
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
+      GNUNET_free (ret->addr);
+      GNUNET_free (ret->write_buffer);
+      GNUNET_free (ret);
+      return NULL;
+    }
+  connect_success_continuation (ret);
   return ret;
+#else
+  return NULL;
+#endif
 }
 
 
@@ -861,23 +937,20 @@ GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
  * This function returns immediately, even if the connection has not
  * yet been established.  This function only creates TCP connections.
  *
- * @param sched scheduler to use
  * @param af_family address family to use
  * @param serv_addr server address
  * @param addrlen length of server address
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the socket handle
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
-                                        *sched, int af_family,
+GNUNET_CONNECTION_create_from_sockaddr (int af_family,
                                         const struct sockaddr *serv_addr,
-                                        socklen_t addrlen, size_t maxbuf)
+                                        socklen_t addrlen)
 {
   struct GNUNET_NETWORK_Handle *s;
   struct GNUNET_CONNECTION_Handle *ret;
 
+
   s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
   if (s == NULL)
     {
@@ -893,7 +966,7 @@ GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
       return NULL;
     }
-  ret = GNUNET_CONNECTION_create_from_existing (sched, s, maxbuf);
+  ret = GNUNET_CONNECTION_create_from_existing (s);
   ret->addr = GNUNET_malloc (addrlen);
   memcpy (ret->addr, serv_addr, addrlen);
   ret->addrlen = addrlen;
@@ -925,23 +998,39 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
 
 /**
  * Close the socket and free associated resources. Pending
- * transmissions are simply dropped.  A pending receive call will be
- * called with an error code of "EPIPE".
+ * transmissions may be completed or dropped depending on the
+ * arguments.   If a receive call is pending and should 
+ * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
+ * should be called explicitly first.
  *
  * @param sock socket to destroy
+ * @param finish_pending_write should pending writes be completed or aborted?
+ *        (this applies to transmissions where the data has already been
+ *        read from the application; all other transmissions should be
+ *        aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
  */
 void
-GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock)
+GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
+                          int finish_pending_write)
 {
+  if (GNUNET_NO == finish_pending_write)
+    {
+      if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
+       {
+         GNUNET_SCHEDULER_cancel (sock->write_task);
+         sock->write_task = GNUNET_SCHEDULER_NO_TASK;
+         sock->write_buffer_off = 0;
+       }
+    }
   if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
     {
       GNUNET_RESOLVER_request_cancel (sock->dns_active);
       sock->dns_active = NULL;
     }
-  GNUNET_assert (sock->sched != NULL);
-  GNUNET_SCHEDULER_add_after (sock->sched,
-                              GNUNET_SCHEDULER_NO_TASK,
-                              &destroy_continuation, sock);
+
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
+  sock->destroy_task 
+    = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
 }
 
 
@@ -990,8 +1079,22 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_CONNECTION_Receiver receiver;
 
   sh->read_task = GNUNET_SCHEDULER_NO_TASK;
+  if ( (GNUNET_YES == sh->ignore_shutdown) &&
+       (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
+      sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
+                                                    (sh->receive_timeout),
+                                                    sh->sock,
+                                                    &receive_ready, sh);
+      return;
+    }
   now = GNUNET_TIME_absolute_get ();
-  if ((now.value > sh->receive_timeout.value) ||
+  if ((now.abs_value > sh->receive_timeout.abs_value) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
@@ -1001,7 +1104,7 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                     "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).
-                    value, sh);
+                    rel_value, sh);
 #endif
       signal_timeout (sh);
       return;
@@ -1070,7 +1173,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       return;
     }
   now = GNUNET_TIME_absolute_get ();
-  if ((now.value > sh->receive_timeout.value) ||
+  if ((now.abs_value > sh->receive_timeout.abs_value) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
 #if DEBUG_CONNECTION
@@ -1082,8 +1185,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     }
   GNUNET_assert (sh->sock != NULL);
   /* connect succeeded, wait for data! */
-  sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
-                                                 GNUNET_TIME_absolute_get_remaining
+  sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
                                                  (sh->receive_timeout),
                                                  sh->sock,
                                                  &receive_ready, sh);
@@ -1122,7 +1224,6 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
   if (sock->sock != NULL)
     {
       memset (&tc, 0, sizeof (tc));
-      tc.sched = sock->sched;
       tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
       receive_again (sock, &tc);
       return;
@@ -1136,6 +1237,20 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
 }
 
 
+/**
+ * Configure this connection to ignore shutdown signals.
+ *
+ * @param sock socket handle
+ * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
+ */
+void
+GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
+                                  int do_ignore)
+{
+  sock->ignore_shutdown = do_ignore;
+}
+
+
 /**
  * Cancel receive job on the given socket.  Note that the
  * receiver callback must not have been called yet in order
@@ -1149,8 +1264,7 @@ GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
 {
   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
     {
-      GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
-                                                      sock->read_task));
+      GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
       sock->read_task = GNUNET_SCHEDULER_NO_TASK;
     }
   else
@@ -1184,7 +1298,7 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
   used = sock->write_buffer_off - sock->write_buffer_pos;
   avail = sock->write_buffer_size - used;
   size = sock->nth.notify_size;
-  if (sock->nth.notify_size > avail)
+  if (size > avail)
     return GNUNET_NO;
   sock->nth.notify_ready = NULL;
   if (sock->write_buffer_size - sock->write_buffer_off < size)
@@ -1195,10 +1309,12 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
       sock->write_buffer_off -= sock->write_buffer_pos;
       sock->write_buffer_pos = 0;
     }
-  GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
+  avail = sock->write_buffer_size - sock->write_buffer_off;
+  GNUNET_assert (avail >= size);
   size = notify (sock->nth.notify_ready_cls,
-                 sock->write_buffer_size - sock->write_buffer_off,
+                 avail,
                  &sock->write_buffer[sock->write_buffer_off]);
+  GNUNET_assert (size <= avail);
   sock->write_buffer_off += size;
   return GNUNET_YES;
 }
@@ -1266,6 +1382,20 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock)
 {
   GNUNET_CONNECTION_TransmitReadyNotify notify;
 
+  if (NULL != sock->sock)
+    {
+      GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
+      GNUNET_break (GNUNET_OK ==
+                   GNUNET_NETWORK_socket_close (sock->sock));
+      sock->sock = NULL;
+    }
+  if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_cancel (sock->read_task);
+      sock->read_task = GNUNET_SCHEDULER_NO_TASK;
+      signal_timeout (sock);
+      return;
+    }
   if (sock->nth.notify_ready == NULL)
     return;                     /* nobody to tell about it */
   notify = sock->nth.notify_ready;
@@ -1297,6 +1427,20 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   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)
+       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
+      notify = sock->nth.notify_ready;
+      sock->nth.notify_ready = NULL;
+      notify (sock->nth.notify_ready_cls, 0, NULL);
+      return;
+    }
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
     {
 #if DEBUG_CONNECTION
@@ -1325,17 +1469,17 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                   ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
                   sock);
 #endif
-      if (NULL != sock->sock)
-        {
-          GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
-          GNUNET_break (GNUNET_OK ==
-                        GNUNET_NETWORK_socket_close (sock->sock));
-          sock->sock = NULL;
-        }
       transmit_error (sock);
       return;                   /* connect failed for good, we're finished */
     }
   GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
+  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_size = sock->nth.notify_size;
+    }    
   process_notify (sock);
   have = sock->write_buffer_off - sock->write_buffer_pos;
   if (have == 0)
@@ -1355,12 +1499,17 @@ RETRY:
     {
       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
-      GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
-      sock->sock = NULL;
       transmit_error (sock);
       return;
     }
@@ -1387,8 +1536,7 @@ SCHEDULE_WRITE:
 #endif
   if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
     sock->write_task =
-      GNUNET_SCHEDULER_add_write_net (tc->sched,
-                                      GNUNET_TIME_absolute_get_remaining
+      GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
                                       (sock->nth.transmit_timeout),
                                       sock->sock, &transmit_ready, sock);
 }
@@ -1418,7 +1566,7 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
   if (sock->nth.notify_ready != NULL)
     return NULL;
   GNUNET_assert (notify != NULL);
-  GNUNET_assert (sock->write_buffer_size >= size);
+  GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
   GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
@@ -1431,9 +1579,9 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
   if ((sock->sock == NULL) &&
       (sock->ap_head == NULL) && (sock->dns_active == NULL))
     {
-      sock->write_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
-                                                       GNUNET_TIME_UNIT_ZERO,
-                                                       &connect_error, sock);
+      if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
+       GNUNET_SCHEDULER_cancel (sock->write_task);
+      sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
       return &sock->nth;
     }
   if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
@@ -1444,8 +1592,7 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Scheduling transmit_ready (%p).\n", sock);
 #endif
-      sock->write_task = GNUNET_SCHEDULER_add_write_net (sock->sched,
-                                                         GNUNET_TIME_absolute_get_remaining
+      sock->write_task = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
                                                          (sock->nth.
                                                           transmit_timeout),
                                                          sock->sock,
@@ -1460,8 +1607,7 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
                   sock);
 #endif
       sock->ccs |= COCO_TRANSMIT_READY;
-      sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
-                                                             timeout,
+      sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
                                                              &transmit_timeout,
                                                              sock);
     }
@@ -1486,14 +1632,17 @@ GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
                   "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
                   h);
 #endif
-      GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
+      GNUNET_SCHEDULER_cancel (h->timeout_task);
       h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
       h->sh->ccs -= COCO_TRANSMIT_READY;
     }
   else
     {
-      GNUNET_SCHEDULER_cancel (h->sh->sched, h->sh->write_task);
-      h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
+      if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
+       {
+         GNUNET_SCHEDULER_cancel (h->sh->write_task);
+         h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
+       }
     }
   h->notify_ready = NULL;
 }