gpl3
[oweals/gnunet.git] / src / datastore / datastore_api.c
index 65a519e598c38945162ac04a78579f33598eb452..dcafeb839d659ab6461edcddbae7775e538438ab 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
 #include "gnunet_datastore_service.h"
 #include "datastore.h"
 
+
+/**
+ * Context for processing status messages.
+ */
+struct StatusContext
+{
+  /**
+   * Continuation to call with the status.
+   */
+  GNUNET_DATASTORE_ContinuationWithStatus cont;
+
+  /**
+   * Closure for cont.
+   */
+  void *cont_cls;
+
+};
+
+
+/**
+ * Context for processing result messages.
+ */
+struct ResultContext
+{
+  /**
+   * Iterator to call with the result.
+   */
+  GNUNET_DATASTORE_Iterator iter;
+
+  /**
+   * Closure for iter.
+   */
+  void *iter_cls;
+
+};
+
+
+/**
+ *  Context for a queue operation.
+ */
+union QueueContext
+{
+
+  struct StatusContext sc;
+  
+  struct ResultContext rc;
+
+};
+
+
+
 /**
  * Entry in our priority queue.
  */
-struct QueueEntry
+struct GNUNET_DATASTORE_QueueEntry
 {
 
   /**
    * This is a linked list.
    */
-  struct QueueEntry *next;
+  struct GNUNET_DATASTORE_QueueEntry *next;
 
   /**
    * This is a linked list.
    */
-  struct QueueEntry *prev;
+  struct GNUNET_DATASTORE_QueueEntry *prev;
 
   /**
    * Handle to the master context.
@@ -54,25 +105,24 @@ struct QueueEntry
   /**
    * Response processor (NULL if we are not waiting for a response).
    * This struct should be used for the closure, function-specific
-   * arguments can be passed via 'client_ctx'.
+   * arguments can be passed via 'qc'.
    */
   GNUNET_CLIENT_MessageHandler response_proc;
-  
-  /**
-   * Specific context (variable argument that
-   * can be used by the response processor).
-   */
-  void *client_ctx;
 
   /**
    * Function to call after transmission of the request.
    */
-  GNUNET_DATASTORE_ContinuationWithStatus contX;
+  GNUNET_DATASTORE_ContinuationWithStatus cont;
    
   /**
    * Closure for 'cont'.
    */
-  void *cont_clsX;
+  void *cont_cls;
+
+  /**
+   * Context for the operation.
+   */
+  union QueueContext qc;
 
   /**
    * Task for timeout signalling.
@@ -141,12 +191,12 @@ struct GNUNET_DATASTORE_Handle
   /**
    * Current head of priority queue.
    */
-  struct QueueEntry *queue_head;
+  struct GNUNET_DATASTORE_QueueEntry *queue_head;
 
   /**
    * Current tail of priority queue.
    */
-  struct QueueEntry *queue_tail;
+  struct GNUNET_DATASTORE_QueueEntry *queue_tail;
 
   /**
    * Task for trying to reconnect.
@@ -164,6 +214,11 @@ struct GNUNET_DATASTORE_Handle
    */
   unsigned int queue_size;
 
+  /**
+   * Are we currently trying to receive from the service?
+   */
+  int in_receive;
+
 };
 
 
@@ -190,7 +245,7 @@ GNUNET_DATASTORE_connect (const struct
   if (c == NULL)
     return NULL; /* oops */
   h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) + 
-                    GNUNET_SERVER_MAX_MESSAGE_SIZE);
+                    GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
   h->client = c;
   h->cfg = cfg;
   h->sched = sched;
@@ -240,22 +295,23 @@ transmit_drop (void *cls,
 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
                                  int drop)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
 
   if (h->client != NULL)
-    GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+    {
+      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+      h->client = NULL;
+    }
   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (h->sched,
-                            h->reconnect_task);
-  h->client = NULL;
+    {
+      GNUNET_SCHEDULER_cancel (h->sched,
+                              h->reconnect_task);
+      h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    }
   while (NULL != (qe = h->queue_head))
     {
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      if (NULL != qe->response_proc)
-       qe->response_proc (qe, NULL);
-      GNUNET_free (qe);
+      GNUNET_assert (NULL != qe->response_proc);
+      qe->response_proc (qe, NULL);
     }
   if (GNUNET_YES == drop) 
     {
@@ -281,24 +337,18 @@ void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
 /**
  * A request has timed out (before being transmitted to the service).
  *
- * @param cls the 'struct QueueEntry'
+ * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
  * @param tc scheduler context
  */
 static void
 timeout_queue_entry (void *cls,
                     const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct QueueEntry *qe = cls;
-  struct GNUNET_DATASTORE_Handle *h = qe->h;
+  struct GNUNET_DATASTORE_QueueEntry *qe = cls;
 
   qe->task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_assert (qe->was_transmitted == GNUNET_NO);
-  GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                              h->queue_tail,
-                              qe);
-  if (qe->response_proc != NULL)
-    qe->response_proc (qe, NULL);
-  GNUNET_free (qe);
+  qe->response_proc (qe, NULL);
 }
 
 
@@ -313,20 +363,20 @@ timeout_queue_entry (void *cls,
  *        (if other requests of higher priority are in the queue)
  * @param timeout timeout for the operation
  * @param response_proc function to call with replies (can be NULL)
- * @param client_ctx client context (NOT a closure for response_proc)
+ * @param qc client context (NOT a closure for response_proc)
  * @return NULL if the queue is full (and this entry was dropped)
  */
-static struct QueueEntry *
+static struct GNUNET_DATASTORE_QueueEntry *
 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
                  size_t msize,
                  unsigned int queue_priority,
                  unsigned int max_queue_size,
                  struct GNUNET_TIME_Relative timeout,
                  GNUNET_CLIENT_MessageHandler response_proc,            
-                 void *client_ctx)
+                 const union QueueContext *qc)
 {
-  struct QueueEntry *ret;
-  struct QueueEntry *pos;
+  struct GNUNET_DATASTORE_QueueEntry *ret;
+  struct GNUNET_DATASTORE_QueueEntry *pos;
   unsigned int c;
 
   c = 0;
@@ -338,8 +388,15 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
       c++;
       pos = pos->next;
     }
-  if (c >= max_queue_size)
-    return NULL;
+  ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
+  ret->h = h;
+  ret->response_proc = response_proc;
+  ret->qc = *qc;
+  ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
+  ret->priority = queue_priority;
+  ret->max_queue = max_queue_size;
+  ret->message_size = msize;
+  ret->was_transmitted = GNUNET_NO;
   if (pos == NULL)
     {
       /* append at the tail */
@@ -354,39 +411,28 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
           (h->queue_head->was_transmitted) )
        pos = h->queue_head;
     }
-  ret = GNUNET_malloc (sizeof (struct QueueEntry) + msize);
+  c++;
   GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
                                     h->queue_tail,
                                     pos,
                                     ret);
-  ret->h = h;
-  ret->response_proc = response_proc;
-  ret->client_ctx = client_ctx;
+  h->queue_size++;
+  if (c > max_queue_size)
+    {
+      response_proc (ret, NULL);
+      return NULL;
+    }
   ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
                                            timeout,
                                            &timeout_queue_entry,
                                            ret);
-  ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  ret->priority = queue_priority;
-  ret->max_queue = max_queue_size;
-  ret->message_size = msize;
-  ret->was_transmitted = GNUNET_NO;
-  h->queue_size++;
-  c++;
   pos = ret->next;
   while (pos != NULL) 
     {
       if (pos->max_queue < h->queue_size)
        {
-         GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                      h->queue_tail,
-                                      pos);
-         GNUNET_SCHEDULER_cancel (h->sched,
-                                  pos->task);
-         if (pos->response_proc != NULL)
-           pos->response_proc (pos, NULL);
-         GNUNET_free (pos);
-         h->queue_size--;
+         GNUNET_assert (pos->response_proc != NULL);
+         pos->response_proc (pos, NULL);
          break;
        }
       pos = pos->next;
@@ -426,7 +472,15 @@ try_reconnect (void *cls,
   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
   h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
   if (h->client == NULL)
-    return;
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 "DATASTORE reconnect failed (fatally)\n");
+      return;
+    }
+#if DEBUG_DATASTORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Reconnected to DATASTORE\n");
+#endif
   process_queue (h);
 }
 
@@ -435,12 +489,25 @@ try_reconnect (void *cls,
  * Disconnect from the service and then try reconnecting to the datastore service
  * after some delay.
  *
- * @param cls the 'struct GNUNET_DATASTORE_Handle'
- * @param tc scheduler context
+ * @param h handle to datastore to disconnect and reconnect
  */
 static void
 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
 {
+  if (h->client == NULL)
+    {
+#if DEBUG_DATASTORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "client NULL in disconnect, will not try to reconnect\n");
+#endif
+      return;
+    }
+#if 0
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# reconnected to DATASTORE"),
+                           1,
+                           GNUNET_NO);
+#endif
   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
   h->client = NULL;
   h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
@@ -464,7 +531,7 @@ transmit_request (void *cls,
                  void *buf)
 {
   struct GNUNET_DATASTORE_Handle *h = cls;
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   size_t msize;
 
   h->th = NULL;
@@ -473,7 +540,7 @@ transmit_request (void *cls,
   if (buf == NULL)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to transmit request to database.\n"));
+                 _("Failed to transmit request to DATASTORE.\n"));
       do_disconnect (h);
       return 0;
     }
@@ -482,11 +549,17 @@ transmit_request (void *cls,
       process_queue (h);
       return 0;
     }
+ #if DEBUG_DATASTORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Transmitting %u byte request to DATASTORE\n",
+             msize);
+#endif
   memcpy (buf, &qe[1], msize);
   qe->was_transmitted = GNUNET_YES;
   GNUNET_SCHEDULER_cancel (h->sched,
                           qe->task);
   qe->task = GNUNET_SCHEDULER_NO_TASK;
+  h->in_receive = GNUNET_YES;
   GNUNET_CLIENT_receive (h->client,
                         qe->response_proc,
                         qe,
@@ -504,16 +577,45 @@ transmit_request (void *cls,
 static void
 process_queue (struct GNUNET_DATASTORE_Handle *h)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
 
   if (NULL == (qe = h->queue_head))
-    return; /* no entry in queue */
+    {
+#if DEBUG_DATASTORE > 1
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Queue empty\n");
+#endif
+      return; /* no entry in queue */
+    }
   if (qe->was_transmitted == GNUNET_YES)
-    return; /* waiting for replies */
+    {
+#if DEBUG_DATASTORE > 1
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Head request already transmitted\n");
+#endif
+      return; /* waiting for replies */
+    }
   if (h->th != NULL)
-    return; /* request pending */
+    {
+#if DEBUG_DATASTORE > 1
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Pending transmission request\n");
+#endif
+      return; /* request pending */
+    }
   if (h->client == NULL)
-    return; /* waiting for reconnect */
+    {
+#if DEBUG_DATASTORE > 1
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Not connected\n");
+#endif
+      return; /* waiting for reconnect */
+    }
+#if DEBUG_DATASTORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Queueing %u byte request to DATASTORE\n",
+             qe->message_size);
+#endif
   h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
                                               qe->message_size,
                                               GNUNET_TIME_absolute_get_remaining (qe->timeout),
@@ -523,26 +625,6 @@ process_queue (struct GNUNET_DATASTORE_Handle *h)
 }
 
 
-
-
-/**
- * Context for processing status messages.
- */
-struct StatusContext
-{
-  /**
-   * Continuation to call with the status.
-   */
-  GNUNET_DATASTORE_ContinuationWithStatus cont;
-
-  /**
-   * Closure for cont.
-   */
-  void *cont_cls;
-
-};
-
-
 /**
  * Dummy continuation used to do nothing (but be non-zero).
  *
@@ -557,6 +639,24 @@ drop_status_cont (void *cls, int result, const char *emsg)
 }
 
 
+static void
+free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
+{
+  struct GNUNET_DATASTORE_Handle *h = qe->h;
+
+  GNUNET_CONTAINER_DLL_remove (h->queue_head,
+                              h->queue_tail,
+                              qe);
+  if (qe->task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_cancel (h->sched,
+                              qe->task);
+      qe->task = GNUNET_SCHEDULER_NO_TASK;
+    }
+  h->queue_size--;
+  GNUNET_free (qe);
+}
+
 /**
  * Type of a function to call when we receive a message
  * from the service.
@@ -569,35 +669,41 @@ process_status_message (void *cls,
                        const struct
                        GNUNET_MessageHeader * msg)
 {
-  struct QueueEntry *qe = cls;
+  struct GNUNET_DATASTORE_QueueEntry *qe = cls;
   struct GNUNET_DATASTORE_Handle *h = qe->h;
-  struct StatusContext *rc = qe->client_ctx;
+  struct StatusContext rc = qe->qc.sc;
   const struct StatusMessage *sm;
   const char *emsg;
   int32_t status;
+  int was_transmitted;
 
-  GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                              h->queue_tail,
-                              qe);
-  GNUNET_free (qe);
+  h->in_receive = GNUNET_NO;
+  was_transmitted = qe->was_transmitted;
   if (msg == NULL)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to receive response from database.\n"));
-      do_disconnect (h);
+    {      
+      free_queue_entry (qe);
+      if (NULL == h->client)
+       return; /* forced disconnect */
+      if (was_transmitted == GNUNET_YES)
+       {
+         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                     _("Failed to receive response from database.\n"));
+         do_disconnect (h);
+       }
       return;
     }
-
+  GNUNET_assert (GNUNET_YES == qe->was_transmitted);
+  GNUNET_assert (h->queue_head == qe);
+  free_queue_entry (qe);
   if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
        (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) ) 
     {
       GNUNET_break (0);
       h->retry_time = GNUNET_TIME_UNIT_ZERO;
       do_disconnect (h);
-      rc->cont (rc->cont_cls, 
-               GNUNET_SYSERR,
-               _("Error reading response from datastore service"));
-      GNUNET_free (rc);
+      rc.cont (rc.cont_cls, 
+              GNUNET_SYSERR,
+              _("Error reading response from datastore service"));
       return;
     }
   sm = (const struct StatusMessage*) msg;
@@ -624,10 +730,9 @@ process_status_message (void *cls,
              (int) status,
              emsg);
 #endif
-  rc->cont (rc->cont_cls, 
-           status,
-           emsg);
-  GNUNET_free (rc);  
+  rc.cont (rc.cont_cls, 
+          status,
+          emsg);
   process_queue (h);
 }
 
@@ -653,8 +758,11 @@ process_status_message (void *cls,
  * @param timeout timeout for the operation
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
                      int rid,
                       const GNUNET_HashCode * key,
@@ -670,10 +778,10 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
                      GNUNET_DATASTORE_ContinuationWithStatus cont,
                      void *cont_cls)
 {
-  struct StatusContext *scont;
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct DataMessage *dm;
   size_t msize;
+  union QueueContext qc;
 
 #if DEBUG_DATASTORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -682,15 +790,14 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
              GNUNET_h2s (key));
 #endif
   msize = sizeof(struct DataMessage) + size;
-  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
-  scont = GNUNET_malloc (sizeof (struct StatusContext));
-  scont->cont = cont;
-  scont->cont_cls = cont_cls;
+  GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
+  qc.sc.cont = cont;
+  qc.sc.cont_cls = cont_cls;
   qe = make_queue_entry (h, msize,
                         queue_priority, max_queue_size, timeout,
-                        &process_status_message, scont);
+                        &process_status_message, &qc);
   if (qe == NULL)
-    return;
+    return NULL;
   dm = (struct DataMessage* ) &qe[1];
   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
   dm->header.size = htons(msize);
@@ -704,6 +811,7 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
   dm->key = *key;
   memcpy (&dm[1], data, size);
   process_queue (h);
+  return qe;
 }
 
 
@@ -722,8 +830,11 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
  * @param cont continuation to call when done; "success" will be set to
  *             a positive reservation value if space could be reserved.
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
                          uint64_t amount,
                          uint32_t entries,
@@ -733,9 +844,9 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
                          GNUNET_DATASTORE_ContinuationWithStatus cont,
                          void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct ReserveMessage *rm;
-  struct StatusContext *scont;
+  union QueueContext qc;
 
   if (cont == NULL)
     cont = &drop_status_cont;
@@ -745,20 +856,20 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
              (unsigned long long) amount,
              (unsigned int) entries);
 #endif
-  scont = GNUNET_malloc (sizeof (struct StatusContext));
-  scont->cont = cont;
-  scont->cont_cls = cont_cls;
+  qc.sc.cont = cont;
+  qc.sc.cont_cls = cont_cls;
   qe = make_queue_entry (h, sizeof(struct ReserveMessage),
                         queue_priority, max_queue_size, timeout,
-                        &process_status_message, scont);
+                        &process_status_message, &qc);
   if (qe == NULL)
-    return;
+    return NULL;
   rm = (struct ReserveMessage*) &qe[1];
   rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
   rm->header.size = htons(sizeof (struct ReserveMessage));
   rm->entries = htonl(entries);
   rm->amount = GNUNET_htonll(amount);
   process_queue (h);
+  return qe;
 }
 
 
@@ -779,8 +890,11 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
  * @param timeout how long to wait at most for a response
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
                                  int rid,
                                  unsigned int queue_priority,
@@ -789,9 +903,9 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
                                  GNUNET_DATASTORE_ContinuationWithStatus cont,
                                  void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct ReleaseReserveMessage *rrm;
-  struct StatusContext *scont;
+  union QueueContext qc;
 
   if (cont == NULL)
     cont = &drop_status_cont;
@@ -800,19 +914,19 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
              "Asked to release reserve %d\n",
              rid);
 #endif
-  scont = GNUNET_malloc (sizeof (struct StatusContext));
-  scont->cont = cont;
-  scont->cont_cls = cont_cls;
+  qc.sc.cont = cont;
+  qc.sc.cont_cls = cont_cls;
   qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
                         queue_priority, max_queue_size, timeout,
-                        &process_status_message, scont);
+                        &process_status_message, &qc);
   if (qe == NULL)
-    return;
+    return NULL;
   rrm = (struct ReleaseReserveMessage*) &qe[1];
   rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
   rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
   rrm->rid = htonl(rid);
   process_queue (h);
+  return qe;
 }
 
 
@@ -829,8 +943,11 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
  * @param timeout how long to wait at most for a response
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
                         unsigned long long uid,
                         uint32_t priority,
@@ -841,9 +958,9 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
                         GNUNET_DATASTORE_ContinuationWithStatus cont,
                         void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct UpdateMessage *um;
-  struct StatusContext *scont;
+  union QueueContext qc;
 
   if (cont == NULL)
     cont = &drop_status_cont;
@@ -854,14 +971,13 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
              (unsigned int) priority,
              (unsigned long long) expiration.value);
 #endif
-  scont = GNUNET_malloc (sizeof (struct StatusContext));
-  scont->cont = cont;
-  scont->cont_cls = cont_cls;
+  qc.sc.cont = cont;
+  qc.sc.cont_cls = cont_cls;
   qe = make_queue_entry (h, sizeof(struct UpdateMessage),
                         queue_priority, max_queue_size, timeout,
-                        &process_status_message, scont);
+                        &process_status_message, &qc);
   if (qe == NULL)
-    return;
+    return NULL;
   um = (struct UpdateMessage*) &qe[1];
   um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
   um->header.size = htons(sizeof (struct UpdateMessage));
@@ -869,6 +985,7 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
   um->expiration = GNUNET_TIME_absolute_hton(expiration);
   um->uid = GNUNET_htonll(uid);
   process_queue (h);
+  return qe;
 }
 
 
@@ -889,8 +1006,11 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
  * @param timeout how long to wait at most for a response
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
                          const GNUNET_HashCode *key,
                          uint32_t size, 
@@ -901,10 +1021,10 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
                         GNUNET_DATASTORE_ContinuationWithStatus cont,
                         void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct DataMessage *dm;
   size_t msize;
-  struct StatusContext *scont;
+  union QueueContext qc;
 
   if (cont == NULL)
     cont = &drop_status_cont;
@@ -914,16 +1034,15 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
              size,
              GNUNET_h2s (key));
 #endif
-  scont = GNUNET_malloc (sizeof (struct StatusContext));
-  scont->cont = cont;
-  scont->cont_cls = cont_cls;
+  qc.sc.cont = cont;
+  qc.sc.cont_cls = cont_cls;
   msize = sizeof(struct DataMessage) + size;
-  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
+  GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
   qe = make_queue_entry (h, msize,
                         queue_priority, max_queue_size, timeout,
-                        &process_status_message, scont);
+                        &process_status_message, &qc);
   if (qe == NULL)
-    return;
+    return NULL;
   dm = (struct DataMessage*) &qe[1];
   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
   dm->header.size = htons(msize);
@@ -937,28 +1056,10 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
   dm->key = *key;
   memcpy (&dm[1], data, size);
   process_queue (h);
+  return qe;
 }
 
 
-
-/**
- * Context for processing result messages.
- */
-struct ResultContext
-{
-  /**
-   * Iterator to call with the result.
-   */
-  GNUNET_DATASTORE_Iterator iter;
-
-  /**
-   * Closure for iter.
-   */
-  void *iter_cls;
-
-};
-
-
 /**
  * Type of a function to call when we receive a message
  * from the service.
@@ -970,28 +1071,31 @@ static void
 process_result_message (void *cls,
                        const struct GNUNET_MessageHeader * msg)
 {
-  struct QueueEntry *qe = cls;
+  struct GNUNET_DATASTORE_QueueEntry *qe = cls;
   struct GNUNET_DATASTORE_Handle *h = qe->h;
-  struct ResultContext *rc = qe->client_ctx;
+  struct ResultContext rc = qe->qc.rc;
   const struct DataMessage *dm;
+  int was_transmitted;
 
+  h->in_receive = GNUNET_NO;
   if (msg == NULL)
     {
-#if DEBUG_DATASTORE
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to receive response from datastore\n"));
-#endif
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      GNUNET_free (qe);
-      do_disconnect (h);
-      rc->iter (rc->iter_cls,
-               NULL, 0, NULL, 0, 0, 0, 
-               GNUNET_TIME_UNIT_ZERO_ABS, 0);  
-      GNUNET_free (rc);
+      was_transmitted = qe->was_transmitted;
+      free_queue_entry (qe);
+      if (was_transmitted == GNUNET_YES)
+       {
+         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                     _("Failed to receive response from database.\n"));
+         do_disconnect (h);
+       }
+      if (rc.iter != NULL)
+       rc.iter (rc.iter_cls,
+                NULL, 0, NULL, 0, 0, 0, 
+                GNUNET_TIME_UNIT_ZERO_ABS, 0); 
       return;
     }
+  GNUNET_assert (GNUNET_YES == qe->was_transmitted);
+  GNUNET_assert (h->queue_head == qe);
   if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END) 
     {
       GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
@@ -999,14 +1103,11 @@ process_result_message (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Received end of result set\n");
 #endif
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      GNUNET_free (qe);
-      rc->iter (rc->iter_cls,
-               NULL, 0, NULL, 0, 0, 0, 
-               GNUNET_TIME_UNIT_ZERO_ABS, 0);  
-      GNUNET_free (rc);
+      free_queue_entry (qe);
+      if (rc.iter != NULL)
+       rc.iter (rc.iter_cls,
+                NULL, 0, NULL, 0, 0, 0, 
+                GNUNET_TIME_UNIT_ZERO_ABS, 0); 
       process_queue (h);
       return;
     }
@@ -1015,16 +1116,25 @@ process_result_message (void *cls,
        (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
     {
       GNUNET_break (0);
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      GNUNET_free (qe);
+      free_queue_entry (qe);
+      h->retry_time = GNUNET_TIME_UNIT_ZERO;
+      do_disconnect (h);
+      if (rc.iter != NULL)
+       rc.iter (rc.iter_cls,
+                NULL, 0, NULL, 0, 0, 0, 
+                GNUNET_TIME_UNIT_ZERO_ABS, 0); 
+      return;
+    }
+  if (rc.iter == NULL)
+    {
+      /* abort iteration */
+#if DEBUG_DATASTORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Aborting iteration via disconnect (client has cancelled)\n");
+#endif
+      free_queue_entry (qe);
       h->retry_time = GNUNET_TIME_UNIT_ZERO;
       do_disconnect (h);
-      rc->iter (rc->iter_cls,
-               NULL, 0, NULL, 0, 0, 0, 
-               GNUNET_TIME_UNIT_ZERO_ABS, 0);  
-      GNUNET_free (rc);
       return;
     }
   dm = (const struct DataMessage*) msg;
@@ -1036,15 +1146,15 @@ process_result_message (void *cls,
              ntohl(dm->size),
              GNUNET_h2s(&dm->key));
 #endif
-  rc->iter (rc->iter_cls,
-           &dm->key,
-           ntohl(dm->size),
-           &dm[1],
-           ntohl(dm->type),
-           ntohl(dm->priority),
-           ntohl(dm->anonymity),
-           GNUNET_TIME_absolute_ntoh(dm->expiration),  
-           GNUNET_ntohll(dm->uid));
+  rc.iter (rc.iter_cls,
+          &dm->key,
+          ntohl(dm->size),
+          &dm[1],
+          ntohl(dm->type),
+          ntohl(dm->priority),
+          ntohl(dm->anonymity),
+          GNUNET_TIME_absolute_ntoh(dm->expiration),   
+          GNUNET_ntohll(dm->uid));
 }
 
 
@@ -1060,8 +1170,11 @@ process_result_message (void *cls,
  *        will be called once with a value (if available)
  *        and always once with a value of NULL.
  * @param iter_cls closure for iter
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
                             unsigned int queue_priority,
                             unsigned int max_queue_size,
@@ -1069,27 +1182,27 @@ GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
                              GNUNET_DATASTORE_Iterator iter, 
                             void *iter_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct GNUNET_MessageHeader *m;
-  struct ResultContext *rcont;
+  union QueueContext qc;
 
 #if DEBUG_DATASTORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Asked to get random entry in %llu ms\n",
              (unsigned long long) timeout.value);
 #endif
-  rcont = GNUNET_malloc (sizeof (struct ResultContext));
-  rcont->iter = iter;
-  rcont->iter_cls = iter_cls;
+  qc.rc.iter = iter;
+  qc.rc.iter_cls = iter_cls;
   qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
                         queue_priority, max_queue_size, timeout,
-                        &process_result_message, rcont);
+                        &process_result_message, &qc);
   if (qe == NULL)
-    return;
+    return NULL;    
   m = (struct GNUNET_MessageHeader*) &qe[1];
   m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
   m->size = htons(sizeof (struct GNUNET_MessageHeader));
   process_queue (h);
+  return qe;
 }
 
 
@@ -1111,8 +1224,11 @@ GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
  * @param iter function to call on each matching value;
  *        will be called once with a NULL value at the end
  * @param iter_cls closure for iter
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
                       const GNUNET_HashCode * key,
                      enum GNUNET_BLOCK_Type type,
@@ -1122,9 +1238,9 @@ GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
                       GNUNET_DATASTORE_Iterator iter, 
                      void *iter_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct GetMessage *gm;
-  struct ResultContext *rcont;
+  union QueueContext qc;
 
 #if DEBUG_DATASTORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1132,14 +1248,13 @@ GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
              (unsigned int) type,
              GNUNET_h2s (key));
 #endif
-  rcont = GNUNET_malloc (sizeof (struct ResultContext));
-  rcont->iter = iter;
-  rcont->iter_cls = iter_cls;
+  qc.rc.iter = iter;
+  qc.rc.iter_cls = iter_cls;
   qe = make_queue_entry (h, sizeof(struct GetMessage),
                         queue_priority, max_queue_size, timeout,
-                        &process_result_message, rcont);
+                        &process_result_message, &qc);
   if (qe == NULL)
-    return;
+    return NULL;
   gm = (struct GetMessage*) &qe[1];
   gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
   gm->type = htonl(type);
@@ -1153,6 +1268,7 @@ GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
       gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
     }
   process_queue (h);
+  return qe;
 }
 
 
@@ -1168,29 +1284,70 @@ void
 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
                           int more)
 {
-  struct QueueEntry *qe = h->queue_head;
-  struct ResultContext *rc = qe->client_ctx;
+  struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
+  struct ResultContext rc = qe->qc.rc;
 
-  GNUNET_assert (NULL != qe);
   GNUNET_assert (&process_result_message == qe->response_proc);
   if (GNUNET_YES == more)
     {     
+      h->in_receive = GNUNET_YES;
       GNUNET_CLIENT_receive (h->client,
                             qe->response_proc,
                             qe,
                             GNUNET_TIME_absolute_get_remaining (qe->timeout));
       return;
     }
-  GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                              h->queue_tail,
-                              qe);
-  GNUNET_free (qe);
+  free_queue_entry (qe);
   h->retry_time = GNUNET_TIME_UNIT_ZERO;
   do_disconnect (h);
-  rc->iter (rc->iter_cls,
-           NULL, 0, NULL, 0, 0, 0, 
-           GNUNET_TIME_UNIT_ZERO_ABS, 0);      
-  GNUNET_free (rc);
+  rc.iter (rc.iter_cls,
+          NULL, 0, NULL, 0, 0, 0, 
+          GNUNET_TIME_UNIT_ZERO_ABS, 0);       
+}
+
+
+/**
+ * Cancel a datastore operation.  The final callback from the
+ * operation must not have been done yet.
+ * 
+ * @param qe operation to cancel
+ */
+void
+GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
+{
+  struct GNUNET_DATASTORE_Handle *h;
+  int reconnect;
+
+  h = qe->h;
+#if DEBUG_DATASTORE
+  GNUNET_log  (GNUNET_ERROR_TYPE_DEBUG,
+              "Pending DATASTORE request %p cancelled (%d, %d)\n",
+              qe,
+              qe->was_transmitted,
+              h->queue_head == qe);
+#endif
+  reconnect = GNUNET_NO;
+  if (GNUNET_YES == qe->was_transmitted) 
+    {
+      if (qe->response_proc == &process_result_message)        
+       {
+         qe->qc.rc.iter = NULL;    
+         if (GNUNET_YES != h->in_receive)
+           GNUNET_DATASTORE_get_next (h, GNUNET_YES);
+         return;
+       }
+      reconnect = GNUNET_YES;
+    }
+  free_queue_entry (qe);
+  if (reconnect)
+    {
+      h->retry_time = GNUNET_TIME_UNIT_ZERO;
+      do_disconnect (h);
+    }
+  else
+    {
+      process_queue (h);
+    }
 }