tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / fs / fs_search.c
index cabe2d8ef4a196f63a34b69779899417fc46015b..0571a2b3f3aed706bd71ac902afe5616aecf1351 100644 (file)
@@ -1,21 +1,21 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2001-2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2001-2014 GNUnet e.V.
 
-     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 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Tem ple Place - Suite 330,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file fs/fs_search.c
@@ -215,11 +215,9 @@ signal_probe_result (struct GNUNET_FS_SearchResult *sr)
  * Handle the case where we have failed to receive a response for our probe.
  *
  * @param cls our `struct GNUNET_FS_SearchResult *`
- * @param tc scheduler context
  */
 static void
-probe_failure_handler (void *cls,
-                       const struct GNUNET_SCHEDULER_TaskContext *tc)
+probe_failure_handler (void *cls)
 {
   struct GNUNET_FS_SearchResult *sr = cls;
 
@@ -241,11 +239,9 @@ probe_failure_handler (void *cls,
  * Handle the case where we have gotten a response for our probe.
  *
  * @param cls our `struct GNUNET_FS_SearchResult *`
- * @param tc scheduler context
  */
 static void
-probe_success_handler (void *cls,
-                       const struct GNUNET_SCHEDULER_TaskContext *tc)
+probe_success_handler (void *cls)
 {
   struct GNUNET_FS_SearchResult *sr = cls;
 
@@ -364,17 +360,15 @@ GNUNET_FS_search_probe_progress_ (void *cls,
  * Task run periodically to remind clients that a probe is active.
  *
  * @param cls the `struct GNUNET_FS_SearchResult` that we are probing for
- * @param tc scheduler context
  */
 static void
-probe_ping_task_cb (void *cls,
-                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+probe_ping_task_cb (void *cls)
 {
   struct GNUNET_FS_Handle *h = cls;
   struct GNUNET_FS_SearchResult *sr;
 
   for (sr = h->probes_head; NULL != sr; sr = sr->next)
-    if (NULL != sr->probe_ctx->client)
+    if (NULL != sr->probe_ctx->mq)
       signal_probe_result (sr);
   h->probe_ping_task
     = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
@@ -464,8 +458,8 @@ GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
              (unsigned long long) off,
              sr);
   sr->remaining_probe_time =
-      GNUNET_TIME_relative_multiply (sr->h->avg_block_latency,
-                                     2 * (1 + sr->availability_trials));
+      GNUNET_TIME_relative_saturating_multiply (sr->h->avg_block_latency,
+                                                2 * (1 + sr->availability_trials));
   sr->probe_ctx =
       GNUNET_FS_download_start (sr->h, sr->uri, sr->meta, NULL, NULL, off,
                                 len, sr->anonymity,
@@ -574,7 +568,13 @@ process_ksk_result (struct GNUNET_FS_SearchContext *sc,
 
   /* check if new */
   GNUNET_assert (NULL != sc);
-  GNUNET_FS_uri_to_key (uri, &key);
+  if (GNUNET_OK !=
+      GNUNET_FS_uri_to_key (uri,
+                           &key))
+  {
+    GNUNET_break_op (0);
+    return;
+  }
   if (GNUNET_SYSERR ==
       GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
                                                   &key,
@@ -657,9 +657,12 @@ process_ksk_result (struct GNUNET_FS_SearchContext *sc,
  * @return context that can be used to control the search
  */
 static struct GNUNET_FS_SearchContext *
-search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
-              uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
-              void *cctx, struct GNUNET_FS_SearchResult *psearch);
+search_start (struct GNUNET_FS_Handle *h,
+              const struct GNUNET_FS_Uri *uri,
+              uint32_t anonymity,
+              enum GNUNET_FS_SearchOptions options,
+              void *cctx,
+              struct GNUNET_FS_SearchResult *psearch);
 
 
 /**
@@ -683,8 +686,15 @@ process_sks_result (struct GNUNET_FS_SearchContext *sc,
 
   /* check if new */
   GNUNET_assert (NULL != sc);
-  GNUNET_FS_uri_to_key (uri, &key);
-  GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
+  if (GNUNET_OK !=
+      GNUNET_FS_uri_to_key (uri,
+                           &key))
+  {
+    GNUNET_break (0);
+    return;
+  }
+  GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key,
+                         &uri->data.chk.chk.query,
                           &key);
   if (GNUNET_SYSERR ==
       GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
@@ -895,22 +905,46 @@ process_sblock (struct GNUNET_FS_SearchContext *sc,
 
 
 /**
- * Process a search result.
+ * Shutdown any existing connection to the FS
+ * service and try to establish a fresh one
+ * (and then re-transmit our search request).
  *
- * @param sc our search context
- * @param type type of the result
- * @param expiration when it will expire
- * @param data the (encrypted) response
- * @param size size of @a data
+ * @param sc the search to reconnec
  */
 static void
-process_result (struct GNUNET_FS_SearchContext *sc,
-               enum GNUNET_BLOCK_Type type,
-                struct GNUNET_TIME_Absolute expiration,
-               const void *data,
-                size_t size)
+try_reconnect (struct GNUNET_FS_SearchContext *sc);
+
+
+/**
+ * We check a result message from the service.
+ *
+ * @param cls closure
+ * @param msg result message received
+ */
+static int
+check_result (void *cls,
+              const struct ClientPutMessage *cm)
 {
-  if (GNUNET_TIME_absolute_get_duration (expiration).rel_value_us > 0)
+  /* payload of any variable size is OK */
+  return GNUNET_OK;
+}
+
+
+/**
+ * We process a search result from the service.
+ *
+ * @param cls closure
+ * @param msg result message received
+ */
+static void
+handle_result (void *cls,
+               const struct ClientPutMessage *cm)
+{
+  struct GNUNET_FS_SearchContext *sc = cls;
+  uint16_t msize = ntohs (cm->header.size) - sizeof (*cm);
+  enum GNUNET_BLOCK_Type type = ntohl (cm->type);
+
+  if (GNUNET_TIME_absolute_get_duration (GNUNET_TIME_absolute_ntoh (cm->expiration)).rel_value_us > 0)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Result received has already expired.\n");
@@ -920,9 +954,13 @@ process_result (struct GNUNET_FS_SearchContext *sc,
   {
   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
     if (GNUNET_FS_URI_SKS == sc->uri->type)
-      process_sblock (sc, data, size);
+      process_sblock (sc,
+                      (const struct UBlock *) &cm[1],
+                      msize);
     else
-      process_kblock (sc, data, size);
+      process_kblock (sc,
+                      (const struct UBlock *) &cm[1],
+                      msize);
     break;
   case GNUNET_BLOCK_TYPE_ANY:
     GNUNET_break (0);
@@ -938,56 +976,13 @@ process_result (struct GNUNET_FS_SearchContext *sc,
     break;
   default:
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Got result with unknown block type `%d', ignoring"), type);
+                _("Got result with unknown block type `%d', ignoring"),
+                type);
     break;
   }
 }
 
 
-/**
- * Shutdown any existing connection to the FS
- * service and try to establish a fresh one
- * (and then re-transmit our search request).
- *
- * @param sc the search to reconnec
- */
-static void
-try_reconnect (struct GNUNET_FS_SearchContext *sc);
-
-
-/**
- * Type of a function to call when we receive a message
- * from the service.
- *
- * @param cls closure
- * @param msg message received, NULL on timeout or fatal error
- */
-static void
-receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
-{
-  struct GNUNET_FS_SearchContext *sc = cls;
-  const struct ClientPutMessage *cm;
-  uint16_t msize;
-
-  if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
-      (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
-  {
-    try_reconnect (sc);
-    return;
-  }
-  msize = ntohs (msg->size);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Receiving %u bytes of result from fs service\n", msize);
-  cm = (const struct ClientPutMessage *) msg;
-  process_result (sc, ntohl (cm->type),
-                  GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
-                  msize - sizeof (struct ClientPutMessage));
-  /* continue receiving */
-  GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
-                         GNUNET_TIME_UNIT_FOREVER_REL);
-}
-
-
 /**
  * Schedule the transmission of the (next) search request
  * to the service.
@@ -999,7 +994,7 @@ schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
 
 
 /**
- * Closure for 'build_result_set'.
+ * Closure for #build_result_set().
  */
 struct MessageBuilderContext
 {
@@ -1057,7 +1052,6 @@ build_result_set (void *cls,
   }
   if (0 == mbc->put_cnt)
     return GNUNET_SYSERR;
-  mbc->sc->search_request_map_offset++;
   mbc->xoff[--mbc->put_cnt] = *key;
 
   return GNUNET_OK;
@@ -1090,153 +1084,159 @@ find_result_set (void *cls,
 
 
 /**
- * We're ready to transmit the search request to the file-sharing
- * service.  Do it.  If the request is too large to fit into a single
- * message, transmit in increments.
+ * Schedule the transmission of the (next) search request
+ * to the service.
  *
- * @param cls closure
- * @param size number of bytes available in @a buf
- * @param buf where the callee should write the message
- * @return number of bytes written to @a buf
+ * @param sc context for the search
  */
-static size_t
-transmit_search_request (void *cls, size_t size, void *buf)
+static void
+schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
 {
-  struct GNUNET_FS_SearchContext *sc = cls;
   struct MessageBuilderContext mbc;
-  size_t msize;
+  struct GNUNET_MQ_Envelope *env;
   struct SearchMessage *sm;
   struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
   unsigned int total_seen_results; /* total number of result hashes to send */
-  unsigned int message_size_limit;
   uint32_t options;
+  unsigned int left;
+  unsigned int todo;
+  unsigned int fit;
+  unsigned int search_request_map_offset;
+  unsigned int keyword_offset;
+  int first_call;
 
-  if (NULL == buf)
-  {
-    try_reconnect (sc);
-    return 0;
-  }
+  memset (&mbc, 0, sizeof (mbc));
   mbc.sc = sc;
-  mbc.skip_cnt = sc->search_request_map_offset;
-  sm = buf;
-  sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
-  mbc.xoff = (struct GNUNET_HashCode *) &sm[1];
-  options = SEARCH_MESSAGE_OPTION_NONE;
-  if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
-    options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
   if (GNUNET_FS_uri_test_ksk (sc->uri))
   {
-    msize = sizeof (struct SearchMessage);
-    GNUNET_assert (size >= msize);
-    mbc.keyword_offset = sc->keyword_offset;
-    /* calculate total number of known results (in put_cnt => total_seen_results) */
-    mbc.put_cnt = 0;
+    /* This will calculate the result set size ONLY for
+       "keyword_offset == 0", so we will have to recalculate
+       it for the other keywords later! */
     GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
-                                           &find_result_set, &mbc);
+                                           &find_result_set,
+                                           &mbc);
     total_seen_results = mbc.put_cnt;
-    /* calculate how many results we can send in this message */
-    message_size_limit = (size - msize) / sizeof (struct GNUNET_HashCode);
-    mbc.put_cnt = GNUNET_MIN (message_size_limit,
-                              total_seen_results - mbc.skip_cnt);
-    if (sc->search_request_map_offset < total_seen_results)
-      GNUNET_assert (mbc.put_cnt > 0);
-
-    /* now build message */
-    msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
-    sm->header.size = htons (msize);
-    sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
-    sm->anonymity_level = htonl (sc->anonymity);
-    memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
-    sm->query = sc->requests[sc->keyword_offset].uquery;
-    GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
-                                           &build_result_set, &mbc);
-    GNUNET_assert (0 == mbc.put_cnt);
-    GNUNET_assert (total_seen_results >= sc->search_request_map_offset);
-    if (total_seen_results != sc->search_request_map_offset)
-    {
-      /* more requesting to be done... */
-      sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
-      schedule_transmit_search_request (sc);
-      return msize;
-    }
-    sm->options = htonl (options);
-    sc->keyword_offset++;
-    sc->search_request_map_offset = 0;
-    if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
-    {
-      /* more requesting to be done... */
-      schedule_transmit_search_request (sc);
-      return msize;
-    }
   }
   else
   {
-    GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
-    msize = sizeof (struct SearchMessage);
-    GNUNET_assert (size >= msize);
+    total_seen_results
+      = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
+  }
+  search_request_map_offset = 0;
+  keyword_offset = 0;
+  first_call = GNUNET_YES;
+  while ( (0 != (left =
+                 (total_seen_results - search_request_map_offset))) ||
+          (GNUNET_YES == first_call) )
+  {
+    first_call = GNUNET_NO;
+    options = SEARCH_MESSAGE_OPTION_NONE;
+    if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
+      options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
+
+    fit = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof (*sm)) / sizeof (struct GNUNET_HashCode);
+    todo = GNUNET_MIN (fit,
+                       left);
+    env = GNUNET_MQ_msg_extra (sm,
+                               sizeof (struct GNUNET_HashCode) * todo,
+                               GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
+    mbc.skip_cnt = search_request_map_offset;
+    mbc.xoff = (struct GNUNET_HashCode *) &sm[1];
     sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
     sm->anonymity_level = htonl (sc->anonymity);
-    memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
-    GNUNET_CRYPTO_ecdsa_public_key_derive (&sc->uri->data.sks.ns,
-                                        sc->uri->data.sks.identifier,
-                                        "fs-ublock",
-                                        &dpub);
-    GNUNET_CRYPTO_hash (&dpub,
-                       sizeof (dpub),
-                       &sm->query);
-    message_size_limit = (size - msize) / sizeof (struct GNUNET_HashCode);
-    total_seen_results = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
-    mbc.put_cnt = GNUNET_MIN (message_size_limit,
-                              total_seen_results - mbc.skip_cnt);
-    mbc.keyword_offset = 0;
-    if (sc->search_request_map_offset < total_seen_results)
-      GNUNET_assert (mbc.put_cnt > 0);
-    msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
-    GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
-                                           &build_result_set, &mbc);
-    sm->header.size = htons (msize);
-    GNUNET_assert (total_seen_results >= sc->search_request_map_offset);
-    if (total_seen_results != sc->search_request_map_offset)
+    memset (&sm->target,
+            0,
+            sizeof (struct GNUNET_PeerIdentity));
+
+    if (GNUNET_FS_uri_test_ksk (sc->uri))
+    {
+      mbc.keyword_offset = keyword_offset;
+      /* calculate how many results we can send in this message */
+      mbc.put_cnt = todo;
+      /* now build message */
+      sm->query = sc->requests[keyword_offset].uquery;
+      GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
+                                             &build_result_set,
+                                             &mbc);
+      search_request_map_offset += todo;
+      GNUNET_assert (0 == mbc.put_cnt);
+      GNUNET_assert (total_seen_results >= search_request_map_offset);
+      if (total_seen_results != search_request_map_offset)
+      {
+        /* more requesting to be done... */
+        sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
+      }
+      else
+      {
+        sm->options = htonl (options);
+        keyword_offset++;
+        if (sc->uri->data.ksk.keywordCount != keyword_offset)
+        {
+          /* more keywords => more requesting to be done... */
+          first_call = GNUNET_YES;
+          search_request_map_offset = 0;
+          mbc.put_cnt = 0;
+          mbc.keyword_offset = keyword_offset;
+          GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
+                                                 &find_result_set,
+                                                 &mbc);
+          total_seen_results = mbc.put_cnt;
+        }
+      }
+    }
+    else
     {
-      /* more requesting to be done... */
-      sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
-      schedule_transmit_search_request (sc);
-      return msize;
+      GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
+
+      GNUNET_CRYPTO_ecdsa_public_key_derive (&sc->uri->data.sks.ns,
+                                             sc->uri->data.sks.identifier,
+                                             "fs-ublock",
+                                             &dpub);
+      GNUNET_CRYPTO_hash (&dpub,
+                          sizeof (dpub),
+                          &sm->query);
+      mbc.put_cnt = todo;
+      mbc.keyword_offset = 0;
+      GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
+                                             &build_result_set,
+                                             &mbc);
+      GNUNET_assert (total_seen_results >= search_request_map_offset);
+      if (total_seen_results != search_request_map_offset)
+      {
+        /* more requesting to be done... */
+        sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
+      }
+      else
+      {
+        sm->options = htonl (options);
+      }
     }
-    sm->options = htonl (options);
+    GNUNET_MQ_send (sc->mq,
+                    env);
   }
-  GNUNET_CLIENT_receive (sc->client,
-                         &receive_results, sc,
-                         GNUNET_TIME_UNIT_FOREVER_REL);
-  return msize;
 }
 
 
 /**
- * Schedule the transmission of the (next) search request
- * to the service.
+ * Generic error handler, called with the appropriate error code and
+ * the same closure specified at the creation of the message queue.
+ * Not every message queue implementation supports an error handler.
  *
- * @param sc context for the search
+ * @param cls closure with the `struct GNUNET_FS_SearchContext *`
+ * @param error error code
  */
 static void
-schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
+search_mq_error_handler (void *cls,
+                         enum GNUNET_MQ_Error error)
 {
-  size_t size;
-  unsigned int left;
-  unsigned int fit;
-  unsigned int request;
-
-  size = sizeof (struct SearchMessage);
-  left =
-      GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
-      sc->search_request_map_offset;
-  fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
-  request = GNUNET_MIN (fit, left);
-  size += sizeof (struct GNUNET_HashCode) * request;
-  GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
-                                       GNUNET_CONSTANTS_SERVICE_TIMEOUT,
-                                       GNUNET_NO,
-                                       &transmit_search_request, sc);
+  struct GNUNET_FS_SearchContext *sc = cls;
+
+  if (NULL != sc->mq)
+  {
+    GNUNET_MQ_destroy (sc->mq);
+    sc->mq = NULL;
+  }
+  try_reconnect (sc);
 }
 
 
@@ -1245,24 +1245,30 @@ schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
  * our queries NOW.
  *
  * @param cls our search context
- * @param tc unused
  */
 static void
-do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_reconnect (void *cls)
 {
   struct GNUNET_FS_SearchContext *sc = cls;
-  struct GNUNET_CLIENT_Connection *client;
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_hd_var_size (result,
+                           GNUNET_MESSAGE_TYPE_FS_PUT,
+                           struct ClientPutMessage,
+                           sc),
+    GNUNET_MQ_handler_end ()
+  };
 
   sc->task = NULL;
-  client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
-  if (NULL == client)
+  sc->mq = GNUNET_CLIENT_connect (sc->h->cfg,
+                                  "fs",
+                                  handlers,
+                                  &search_mq_error_handler,
+                                  sc);
+  if (NULL == sc->mq)
   {
     try_reconnect (sc);
     return;
   }
-  sc->client = client;
-  sc->search_request_map_offset = 0;
-  sc->keyword_offset = 0;
   schedule_transmit_search_request (sc);
 }
 
@@ -1277,10 +1283,10 @@ do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 try_reconnect (struct GNUNET_FS_SearchContext *sc)
 {
-  if (NULL != sc->client)
+  if (NULL != sc->mq)
   {
-    GNUNET_CLIENT_disconnect (sc->client);
-    sc->client = NULL;
+    GNUNET_MQ_destroy (sc->mq);
+    sc->mq = NULL;
   }
   sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
   sc->task =
@@ -1386,15 +1392,16 @@ GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
   struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
   struct SearchRequestEntry *sre;
 
-  GNUNET_assert (NULL == sc->client);
+  GNUNET_assert (NULL == sc->mq);
   if (GNUNET_FS_uri_test_ksk (sc->uri))
   {
     GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
     anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
     GNUNET_CRYPTO_ecdsa_key_get_public (anon, &anon_pub);
-    sc->requests =
-        GNUNET_malloc (sizeof (struct SearchRequestEntry) *
-                       sc->uri->data.ksk.keywordCount);
+    sc->requests
+      = GNUNET_new_array (sc->uri->data.ksk.keywordCount,
+                          struct SearchRequestEntry);
+
     for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
     {
       keyword = &sc->uri->data.ksk.keywords[i][1];
@@ -1416,11 +1423,14 @@ GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
                                            &update_sre_result_maps,
                                            sc);
   }
-  sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
-  if (NULL == sc->client)
+  GNUNET_assert (NULL == sc->task);
+  do_reconnect (sc);
+  if (NULL == sc->mq)
+  {
+    GNUNET_SCHEDULER_cancel (sc->task);
+    sc->task = NULL;
     return GNUNET_SYSERR;
-  sc->search_request_map_offset = 0;
-  schedule_transmit_search_request (sc);
+  }
   return GNUNET_OK;
 }
 
@@ -1550,10 +1560,10 @@ GNUNET_FS_search_signal_suspend_ (void *cls)
     GNUNET_SCHEDULER_cancel (sc->task);
     sc->task = NULL;
   }
-  if (NULL != sc->client)
+  if (NULL != sc->mq)
   {
-    GNUNET_CLIENT_disconnect (sc->client);
-    sc->client = NULL;
+    GNUNET_MQ_destroy (sc->mq);
+    sc->mq = NULL;
   }
   GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
   if (NULL != sc->requests)
@@ -1614,14 +1624,19 @@ GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
     GNUNET_SCHEDULER_cancel (sc->task);
     sc->task = NULL;
   }
-  if (NULL != sc->client)
-    GNUNET_CLIENT_disconnect (sc->client);
-  sc->client = NULL;
+  if (NULL != sc->mq)
+  {
+    GNUNET_MQ_destroy (sc->mq);
+    sc->mq = NULL;
+  }
   GNUNET_FS_search_sync_ (sc);
   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
-                                         &search_result_freeze_probes, sc);
+                                         &search_result_freeze_probes,
+                                         sc);
   pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
-  sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
+  sc->client_info = GNUNET_FS_search_make_status_ (&pi,
+                                                   sc->h,
+                                                   sc);
 }
 
 
@@ -1635,9 +1650,9 @@ GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
 {
   struct GNUNET_FS_ProgressInfo pi;
 
-  GNUNET_assert (NULL == sc->client);
+  GNUNET_assert (NULL == sc->mq);
   GNUNET_assert (NULL == sc->task);
-  do_reconnect (sc, NULL);
+  do_reconnect (sc);
   GNUNET_FS_search_sync_ (sc);
   pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
@@ -1667,8 +1682,8 @@ search_result_stop (void *cls,
   if (NULL != sr->download)
   {
     sr->download->search = NULL;
-    sr->download->top =
-        GNUNET_FS_make_top (sr->download->h,
+    sr->download->top
+      = GNUNET_FS_make_top (sr->download->h,
                             &GNUNET_FS_download_signal_suspend_,
                             sr->download);
     if (NULL != sr->download->serialization)
@@ -1680,7 +1695,8 @@ search_result_stop (void *cls,
       sr->download->serialization = NULL;
     }
     pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
-    GNUNET_FS_download_make_status_ (&pi, sr->download);
+    GNUNET_FS_download_make_status_ (&pi,
+                                     sr->download);
     GNUNET_FS_download_sync_ (sr->download);
     sr->download = NULL;
   }
@@ -1746,30 +1762,39 @@ GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
   if (NULL != sc->top)
     GNUNET_FS_end_top (sc->h, sc->top);
   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
-                                         &search_result_stop, sc);
+                                         &search_result_stop,
+                                         sc);
   if (NULL != sc->psearch_result)
     sc->psearch_result->update_search = NULL;
   if (NULL != sc->serialization)
   {
     GNUNET_FS_remove_sync_file_ (sc->h,
-                                 (sc->psearch_result !=
-                                  NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
-                                 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
+                                 (NULL != sc->psearch_result)
+                                 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
+                                 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
                                  sc->serialization);
     GNUNET_FS_remove_sync_dir_ (sc->h,
-                                (sc->psearch_result !=
-                                 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
-                                GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
+                                (NULL != sc->psearch_result)
+                                ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
+                                GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
                                 sc->serialization);
     GNUNET_free (sc->serialization);
   }
   pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
-  sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
+  sc->client_info = GNUNET_FS_search_make_status_ (&pi,
+                                                   sc->h,
+                                                   sc);
   GNUNET_break (NULL == sc->client_info);
   if (NULL != sc->task)
+  {
     GNUNET_SCHEDULER_cancel (sc->task);
-  if (NULL != sc->client)
-    GNUNET_CLIENT_disconnect (sc->client);
+    sc->task = NULL;
+  }
+  if (NULL != sc->mq)
+  {
+    GNUNET_MQ_destroy (sc->mq);
+    sc->mq = NULL;
+  }
   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
                                          &search_result_free, sc);
   GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);