2 This file is part of GNUnet.
3 (C) 2001-2006, 2008-2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_search.c
23 * @brief Helper functions for searching.
24 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_protocols.h"
33 #define DEBUG_SEARCH GNUNET_EXTRA_LOGGING
36 * Number of availability trials we perform per search result.
38 #define AVAILABILITY_TRIALS_MAX 8
41 * Fill in all of the generic fields for a search event and
44 * @param pi structure to fill in
45 * @param sc overall search context
46 * @return value returned by the callback
49 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
50 struct GNUNET_FS_SearchContext *sc)
54 pi->value.search.sc = sc;
55 pi->value.search.cctx = sc->client_info;
56 pi->value.search.pctx =
57 (sc->psearch_result == NULL) ? NULL : sc->psearch_result->client_info;
58 pi->value.search.query = sc->uri;
59 pi->value.search.duration =
60 GNUNET_TIME_absolute_get_duration (sc->start_time);
61 pi->value.search.anonymity = sc->anonymity;
62 ret = sc->h->upcb (sc->h->upcb_cls, pi);
68 * Check if the given result is identical
71 * @param cls points to the URI we check against
73 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
75 * @return GNUNET_SYSERR if the result is present,
79 test_result_present (void *cls, const GNUNET_HashCode * key, void *value)
81 const struct GNUNET_FS_Uri *uri = cls;
82 struct GNUNET_FS_SearchResult *sr = value;
84 if (GNUNET_FS_uri_test_equal (uri, sr->uri))
91 * We've found a new CHK result. Let the client
94 * @param sc the search context
95 * @param sr the specific result
98 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
99 struct GNUNET_FS_SearchResult *sr)
101 struct GNUNET_FS_ProgressInfo pi;
103 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
104 pi.value.search.specifics.result.meta = sr->meta;
105 pi.value.search.specifics.result.uri = sr->uri;
106 pi.value.search.specifics.result.result = sr;
107 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
108 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
113 * We've found new information about an existing CHK result. Let the
114 * client know about it.
116 * @param sc the search context
117 * @param sr the specific result
120 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
121 struct GNUNET_FS_SearchResult *sr)
123 struct GNUNET_FS_ProgressInfo pi;
125 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
126 pi.value.search.specifics.update.cctx = sr->client_info;
127 pi.value.search.specifics.update.meta = sr->meta;
128 pi.value.search.specifics.update.uri = sr->uri;
129 pi.value.search.specifics.update.availability_rank =
130 2 * sr->availability_success - sr->availability_trials;
131 pi.value.search.specifics.update.availability_certainty =
132 sr->availability_trials;
133 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
134 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
139 * Context for "get_result_present".
141 struct GetResultContext
144 * The URI we're looking for.
146 const struct GNUNET_FS_Uri *uri;
149 * Where to store a pointer to the search
150 * result struct if we found a match.
152 struct GNUNET_FS_SearchResult *sr;
157 * Check if the given result is identical to the given URI and if so
160 * @param cls a "struct GetResultContext"
161 * @param key not used
162 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
163 * should compare with
167 get_result_present (void *cls, const GNUNET_HashCode * key, void *value)
169 struct GetResultContext *grc = cls;
170 struct GNUNET_FS_SearchResult *sr = value;
172 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
179 * Signal result of last probe to client and then schedule next
183 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
185 struct GNUNET_FS_ProgressInfo pi;
187 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
188 pi.value.search.specifics.update.cctx = sr->client_info;
189 pi.value.search.specifics.update.meta = sr->meta;
190 pi.value.search.specifics.update.uri = sr->uri;
191 pi.value.search.specifics.update.availability_rank = sr->availability_success;
192 pi.value.search.specifics.update.availability_certainty =
193 sr->availability_trials;
194 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
195 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
196 GNUNET_FS_search_start_probe_ (sr);
201 * Handle the case where we have failed to receive a response for our probe.
203 * @param cls our 'struct GNUNET_FS_SearchResult*'
204 * @param tc scheduler context
207 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 struct GNUNET_FS_SearchResult *sr = cls;
211 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
212 sr->availability_trials++;
213 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
214 sr->probe_ctx = NULL;
215 GNUNET_FS_search_result_sync_ (sr);
216 signal_probe_result (sr);
221 * Handle the case where we have gotten a response for our probe.
223 * @param cls our 'struct GNUNET_FS_SearchResult*'
224 * @param tc scheduler context
227 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct GNUNET_FS_SearchResult *sr = cls;
231 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
232 sr->availability_trials++;
233 sr->availability_success++;
234 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
235 sr->probe_ctx = NULL;
236 GNUNET_FS_search_result_sync_ (sr);
237 signal_probe_result (sr);
242 * Notification of FS that a search probe has made progress.
243 * This function is used INSTEAD of the client's event handler
244 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
246 * @param cls closure, always NULL (!), actual closure
247 * is in the client-context of the info struct
248 * @param info details about the event, specifying the event type
249 * and various bits about the event
250 * @return client-context (for the next progress call
251 * for this operation; should be set to NULL for
252 * SUSPEND and STOPPED events). The value returned
253 * will be passed to future callbacks in the respective
254 * field in the GNUNET_FS_ProgressInfo struct.
257 GNUNET_FS_search_probe_progress_ (void *cls,
258 const struct GNUNET_FS_ProgressInfo *info)
260 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
261 struct GNUNET_TIME_Relative dur;
263 switch (info->status)
265 case GNUNET_FS_STATUS_DOWNLOAD_START:
268 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
269 /* probes should never be resumed */
272 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
273 /* probes should never be suspended */
276 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
279 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
280 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
282 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
283 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
285 sr->probe_cancel_task =
286 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
287 &probe_failure_handler, sr);
289 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
290 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
292 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
293 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
295 sr->probe_cancel_task =
296 GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
298 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
299 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
301 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
302 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
306 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
307 GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
308 sr->probe_active_time = GNUNET_TIME_absolute_get ();
309 sr->probe_cancel_task =
310 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
311 &probe_failure_handler, sr);
313 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
314 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
316 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
317 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
319 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
320 sr->remaining_probe_time =
321 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
322 GNUNET_FS_search_result_sync_ (sr);
333 * Start download probes for the given search result.
335 * @param sr the search result
338 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
343 if (sr->probe_ctx != NULL)
345 if (sr->download != NULL)
347 if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
349 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
351 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
354 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
356 off = len / DBLOCK_SIZE;
358 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
360 if (len - off < DBLOCK_SIZE)
364 sr->remaining_probe_time =
365 GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
366 2 * (1 + sr->availability_trials));
368 GNUNET_FS_download_start (sr->sc->h, sr->uri, sr->meta, NULL, NULL, off,
369 len, sr->sc->anonymity,
370 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
371 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
376 * We have received a KSK result. Check how it fits in with the
377 * overall query and notify the client accordingly.
379 * @param sc context for the overall query
380 * @param ent entry for the specific keyword
381 * @param uri the URI that was found
382 * @param meta metadata associated with the URI
383 * under the "ent" keyword
386 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
387 struct SearchRequestEntry *ent,
388 const struct GNUNET_FS_Uri *uri,
389 const struct GNUNET_CONTAINER_MetaData *meta)
392 struct GNUNET_FS_SearchResult *sr;
393 struct GetResultContext grc;
398 GNUNET_assert (NULL != sc);
399 GNUNET_FS_uri_to_key (uri, &key);
401 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results, &key,
402 &test_result_present,
404 return; /* duplicate result */
405 /* try to find search result in master map */
408 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
409 &get_result_present, &grc);
411 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
414 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
416 sr->uri = GNUNET_FS_uri_dup (uri);
417 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
418 sr->mandatory_missing = sc->mandatory_count;
420 sr->keyword_bitmap = GNUNET_malloc ((sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
421 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
422 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
426 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
428 koff = ent - sc->requests;
429 GNUNET_assert ( (ent >= sc->requests) && (koff < sc->uri->data.ksk.keywordCount));
430 sr->keyword_bitmap[koff / 8] |= (1 << (koff % 8));
431 /* check if mandatory satisfied */
433 sr->mandatory_missing--;
435 sr->optional_support++;
436 if (0 != sr->mandatory_missing)
439 notify_client_chk_result (sc, sr);
441 notify_client_chk_update (sc, sr);
442 GNUNET_FS_search_result_sync_ (sr);
443 GNUNET_FS_search_start_probe_ (sr);
448 * Start search for content, internal API.
450 * @param h handle to the file sharing subsystem
451 * @param uri specifies the search parameters; can be
452 * a KSK URI or an SKS URI.
453 * @param anonymity desired level of anonymity
454 * @param options options for the search
455 * @param cctx client context
456 * @param psearch parent search result (for namespace update searches)
457 * @return context that can be used to control the search
459 static struct GNUNET_FS_SearchContext *
460 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
461 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
462 void *cctx, struct GNUNET_FS_SearchResult *psearch);
466 * We have received an SKS result. Start searching for updates and
467 * notify the client if it is a new result.
469 * @param sc context for the overall query
470 * @param id_update identifier for updates, NULL for none
471 * @param uri the URI that was found
472 * @param meta metadata associated with the URI
475 process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
476 const struct GNUNET_FS_Uri *uri,
477 const struct GNUNET_CONTAINER_MetaData *meta)
479 struct GNUNET_FS_Uri uu;
481 struct GNUNET_FS_SearchResult *sr;
484 GNUNET_assert (NULL != sc);
485 GNUNET_FS_uri_to_key (uri, &key);
486 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
489 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
490 &test_result_present,
492 return; /* duplicate result */
493 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
495 sr->uri = GNUNET_FS_uri_dup (uri);
496 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
498 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
500 GNUNET_FS_search_result_sync_ (sr);
501 GNUNET_FS_search_start_probe_ (sr);
503 notify_client_chk_result (sc, sr);
504 /* search for updates */
505 if (strlen (id_update) == 0)
506 return; /* no updates */
508 uu.data.sks.namespace = sc->uri->data.sks.namespace;
509 uu.data.sks.identifier = GNUNET_strdup (id_update);
510 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
511 GNUNET_free (uu.data.sks.identifier);
516 * Decrypt a block using a 'keyword' as the passphrase. Given the
517 * KSK public key derived from the keyword, this function looks up
518 * the original keyword in the search context and decrypts the
519 * given ciphertext block.
521 * @param sc search context with the keywords
522 * @param public_key public key to use to lookup the keyword
523 * @param edata encrypted data
524 * @param edata_size number of bytes in 'edata' (and 'data')
525 * @param data where to store the plaintext
526 * @return keyword index on success, GNUNET_SYSERR on error (no such
527 * keyword, internal error)
530 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
531 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
537 struct GNUNET_CRYPTO_AesSessionKey skey;
538 struct GNUNET_CRYPTO_AesInitializationVector iv;
541 GNUNET_CRYPTO_hash (public_key,
542 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
545 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
546 if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
548 if (i == sc->uri->data.ksk.keywordCount)
550 /* oops, does not match any of our keywords!? */
552 return GNUNET_SYSERR;
555 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
557 GNUNET_CRYPTO_aes_decrypt (edata, edata_size, &skey,
561 return GNUNET_SYSERR;
568 * Process a keyword-search result.
570 * @param sc our search context
571 * @param kb the kblock
572 * @param size size of kb
575 process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
579 char pt[size - sizeof (struct KBlock)];
581 struct GNUNET_CONTAINER_MetaData *meta;
582 struct GNUNET_FS_Uri *uri;
586 if (-1 == (i = decrypt_block_with_keyword (sc,
589 size - sizeof (struct KBlock),
593 eos = memchr (pt, 0, sizeof (pt));
600 if (sizeof (pt) == j)
601 meta = GNUNET_CONTAINER_meta_data_create ();
603 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
606 GNUNET_break_op (0); /* kblock malformed */
609 uri = GNUNET_FS_uri_parse (pt, &emsg);
612 GNUNET_break_op (0); /* kblock malformed */
613 GNUNET_free_non_null (emsg);
614 GNUNET_CONTAINER_meta_data_destroy (meta);
618 process_ksk_result (sc, &sc->requests[i], uri, meta);
621 GNUNET_CONTAINER_meta_data_destroy (meta);
622 GNUNET_FS_uri_destroy (uri);
627 * Process a keyword-search result with a namespace advertisment.
629 * @param sc our search context
630 * @param nb the nblock
631 * @param size size of nb
634 process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb,
638 char pt[size - sizeof (struct NBlock)];
640 struct GNUNET_CONTAINER_MetaData *meta;
641 struct GNUNET_FS_Uri *uri;
645 if (-1 == (i = decrypt_block_with_keyword (sc,
648 size - sizeof (struct NBlock),
652 eos = memchr (pt, 0, sizeof (pt));
659 if (sizeof (pt) == j)
660 meta = GNUNET_CONTAINER_meta_data_create ();
662 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
665 GNUNET_break_op (0); /* nblock malformed */
669 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
671 uri->data.sks.identifier = GNUNET_strdup (pt);
672 GNUNET_CRYPTO_hash (&nb->subspace,
673 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
674 &uri->data.sks.namespace);
675 uris = GNUNET_FS_uri_to_string (uri);
676 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_URI,
677 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
678 uris, strlen (uris) + 1);
680 GNUNET_PSEUDONYM_add (sc->h->cfg, &uri->data.sks.namespace, meta);
682 process_ksk_result (sc, &sc->requests[i], uri, meta);
685 GNUNET_CONTAINER_meta_data_destroy (meta);
686 GNUNET_FS_uri_destroy (uri);
691 * Process a namespace-search result.
693 * @param sc our search context
694 * @param sb the sblock
695 * @param size size of sb
698 process_sblock (struct GNUNET_FS_SearchContext *sc, const struct SBlock *sb,
701 size_t len = size - sizeof (struct SBlock);
703 struct GNUNET_CRYPTO_AesSessionKey skey;
704 struct GNUNET_CRYPTO_AesInitializationVector iv;
705 struct GNUNET_FS_Uri *uri;
706 struct GNUNET_CONTAINER_MetaData *meta;
715 identifier = sc->uri->data.sks.identifier;
716 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
717 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
718 if (-1 == GNUNET_CRYPTO_aes_decrypt (&sb[1], len, &skey, &iv, pt))
724 off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris);
727 GNUNET_break_op (0); /* sblock malformed */
730 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off);
733 GNUNET_break_op (0); /* sblock malformed */
736 uri = GNUNET_FS_uri_parse (uris, &emsg);
739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse URI `%s': %s\n", uris,
741 GNUNET_break_op (0); /* sblock malformed */
742 GNUNET_free_non_null (emsg);
743 GNUNET_CONTAINER_meta_data_destroy (meta);
747 process_sks_result (sc, id, uri, meta);
749 GNUNET_FS_uri_destroy (uri);
750 GNUNET_CONTAINER_meta_data_destroy (meta);
755 * Process a search result.
757 * @param sc our search context
758 * @param type type of the result
759 * @param expiration when it will expire
760 * @param data the (encrypted) response
761 * @param size size of data
764 process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
765 struct GNUNET_TIME_Absolute expiration, const void *data,
768 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "Result received has already expired.\n");
772 return; /* result expired */
776 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
777 if (!GNUNET_FS_uri_test_ksk (sc->uri))
782 if (sizeof (struct KBlock) > size)
787 process_kblock (sc, data, size);
789 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
790 if (!GNUNET_FS_uri_test_sks (sc->uri))
795 if (sizeof (struct SBlock) > size)
800 process_sblock (sc, data, size);
802 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
803 if (!GNUNET_FS_uri_test_ksk (sc->uri))
808 if (sizeof (struct NBlock) > size)
813 process_nblock (sc, data, size);
815 case GNUNET_BLOCK_TYPE_ANY:
818 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
821 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
824 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
828 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
829 _("Got result with unknown block type `%d', ignoring"), type);
836 * Shutdown any existing connection to the FS
837 * service and try to establish a fresh one
838 * (and then re-transmit our search request).
840 * @param sc the search to reconnec
843 try_reconnect (struct GNUNET_FS_SearchContext *sc);
847 * Type of a function to call when we receive a message
851 * @param msg message received, NULL on timeout or fatal error
854 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
856 struct GNUNET_FS_SearchContext *sc = cls;
857 const struct ClientPutMessage *cm;
860 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
861 (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
866 msize = ntohs (msg->size);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Receiving %u bytes of result from fs service\n", msize);
869 cm = (const struct ClientPutMessage *) msg;
870 process_result (sc, ntohl (cm->type),
871 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
872 msize - sizeof (struct ClientPutMessage));
873 /* continue receiving */
874 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
875 GNUNET_TIME_UNIT_FOREVER_REL);
880 * Schedule the transmission of the (next) search request
883 * @param sc context for the search
886 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
890 * Closure for 'build_result_set'.
892 struct MessageBuilderContext
895 * How many entries can we store to xoff.
897 unsigned int put_cnt;
900 * How many entries should we skip.
902 unsigned int skip_cnt;
905 * Where to store the keys.
907 GNUNET_HashCode *xoff;
910 * Search context we are iterating for.
912 struct GNUNET_FS_SearchContext *sc;
915 * Keyword offset the search result must match (0 for SKS)
917 unsigned int keyword_offset;
922 * Iterating over the known results, pick those matching the given
923 * result range and store their keys at 'xoff'.
925 * @param cls the 'struct MessageBuilderContext'
926 * @param key key for a result
927 * @param value the search result
928 * @return GNUNET_OK to continue iterating
931 build_result_set (void *cls, const GNUNET_HashCode * key, void *value)
933 struct MessageBuilderContext *mbc = cls;
934 struct GNUNET_FS_SearchResult *sr = value;
936 if ( (sr->keyword_bitmap != NULL) &&
937 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
938 return GNUNET_OK; /* have no match for this keyword yet */
939 if (mbc->skip_cnt > 0)
944 if (mbc->put_cnt == 0)
945 return GNUNET_SYSERR;
946 mbc->sc->search_request_map_offset++;
947 mbc->xoff[--mbc->put_cnt] = *key;
953 * Iterating over the known results, count those
954 * matching the given result range and increment
955 * put count for each.
957 * @param cls the 'struct MessageBuilderContext'
958 * @param key key for a result
959 * @param value the search result
960 * @return GNUNET_OK to continue iterating
963 find_result_set (void *cls, const GNUNET_HashCode * key, void *value)
965 struct MessageBuilderContext *mbc = cls;
966 struct GNUNET_FS_SearchResult *sr = value;
968 if ( (sr->keyword_bitmap != NULL) &&
969 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
970 return GNUNET_OK; /* have no match for this keyword yet */
977 * We're ready to transmit the search request to the
978 * file-sharing service. Do it.
981 * @param size number of bytes available in buf
982 * @param buf where the callee should write the message
983 * @return number of bytes written to buf
986 transmit_search_request (void *cls, size_t size, void *buf)
988 struct GNUNET_FS_SearchContext *sc = cls;
989 struct MessageBuilderContext mbc;
991 struct SearchMessage *sm;
992 const char *identifier;
1004 mbc.skip_cnt = sc->search_request_map_offset;
1006 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1007 mbc.xoff = (GNUNET_HashCode *) & sm[1];
1008 options = SEARCH_MESSAGE_OPTION_NONE;
1009 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1010 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1011 if (GNUNET_FS_uri_test_ksk (sc->uri))
1013 msize = sizeof (struct SearchMessage);
1014 GNUNET_assert (size >= msize);
1015 mbc.keyword_offset = sc->keyword_offset;
1017 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1018 &find_result_set, &mbc);
1020 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
1021 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1022 if (sc->search_request_map_offset < sqms)
1023 GNUNET_assert (mbc.put_cnt > 0);
1025 sm->header.size = htons (msize);
1026 sm->type = htonl (GNUNET_BLOCK_TYPE_ANY);
1027 sm->anonymity_level = htonl (sc->anonymity);
1028 memset (&sm->target, 0, sizeof (GNUNET_HashCode));
1029 sm->query = sc->requests[sc->keyword_offset].query;
1030 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1031 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1032 &build_result_set, &mbc);
1033 sm->header.size = htons (msize);
1034 GNUNET_assert (sqms >= sc->search_request_map_offset);
1035 if (sqms != sc->search_request_map_offset)
1037 /* more requesting to be done... */
1038 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1039 schedule_transmit_search_request (sc);
1042 sm->options = htonl (options);
1043 sc->keyword_offset++;
1044 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1046 /* more requesting to be done... */
1047 schedule_transmit_search_request (sc);
1053 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1054 msize = sizeof (struct SearchMessage);
1055 GNUNET_assert (size >= msize);
1056 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_SBLOCK);
1057 sm->anonymity_level = htonl (sc->anonymity);
1058 sm->target = sc->uri->data.sks.namespace;
1059 identifier = sc->uri->data.sks.identifier;
1060 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
1061 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &idh);
1062 GNUNET_CRYPTO_hash_xor (&idh, &sm->target, &sm->query);
1063 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
1064 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1065 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1066 mbc.keyword_offset = 0;
1067 if (sc->search_request_map_offset < sqms)
1068 GNUNET_assert (mbc.put_cnt > 0);
1069 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1070 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1071 &build_result_set, &mbc);
1072 sm->header.size = htons (msize);
1073 GNUNET_assert (sqms >= sc->search_request_map_offset);
1074 if (sqms != sc->search_request_map_offset)
1076 /* more requesting to be done... */
1077 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1078 schedule_transmit_search_request (sc);
1081 sm->options = htonl (options);
1083 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1084 GNUNET_TIME_UNIT_FOREVER_REL);
1090 * Schedule the transmission of the (next) search request
1093 * @param sc context for the search
1096 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1102 size = sizeof (struct SearchMessage);
1104 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1105 sc->search_request_map_offset;
1106 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (GNUNET_HashCode);
1107 fit = GNUNET_MIN (fit, sqms);
1108 size += sizeof (GNUNET_HashCode) * fit;
1109 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1110 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1111 GNUNET_NO, &transmit_search_request, sc);
1117 * Reconnect to the FS service and transmit
1120 * @param cls our search context
1124 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1126 struct GNUNET_FS_SearchContext *sc = cls;
1127 struct GNUNET_CLIENT_Connection *client;
1129 sc->task = GNUNET_SCHEDULER_NO_TASK;
1130 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1136 sc->client = client;
1137 sc->search_request_map_offset = 0;
1138 sc->keyword_offset = 0;
1139 schedule_transmit_search_request (sc);
1144 * Shutdown any existing connection to the FS
1145 * service and try to establish a fresh one
1146 * (and then re-transmit our search request).
1148 * @param sc the search to reconnec
1151 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1153 if (NULL != sc->client)
1155 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1159 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1165 * Start search for content, internal API.
1167 * @param h handle to the file sharing subsystem
1168 * @param uri specifies the search parameters; can be
1169 * a KSK URI or an SKS URI.
1170 * @param anonymity desired level of anonymity
1171 * @param options options for the search
1172 * @param cctx initial value for the client context
1173 * @param psearch parent search result (for namespace update searches)
1174 * @return context that can be used to control the search
1176 static struct GNUNET_FS_SearchContext *
1177 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1178 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1179 void *cctx, struct GNUNET_FS_SearchResult *psearch)
1181 struct GNUNET_FS_SearchContext *sc;
1182 struct GNUNET_FS_ProgressInfo pi;
1184 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1186 sc->options = options;
1187 sc->uri = GNUNET_FS_uri_dup (uri);
1188 sc->anonymity = anonymity;
1189 sc->start_time = GNUNET_TIME_absolute_get ();
1190 if (psearch != NULL)
1192 sc->psearch_result = psearch;
1193 psearch->update_search = sc;
1195 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1196 sc->client_info = cctx;
1197 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1199 GNUNET_FS_uri_destroy (sc->uri);
1200 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1204 GNUNET_FS_search_sync_ (sc);
1205 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1206 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1212 * Build the request and actually initiate the search using the
1213 * GNUnet FS service.
1215 * @param sc search context
1216 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1219 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1222 const char *keyword;
1224 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1225 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1227 GNUNET_assert (NULL == sc->client);
1228 if (GNUNET_FS_uri_test_ksk (sc->uri))
1230 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1232 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1233 sc->uri->data.ksk.keywordCount);
1234 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1236 keyword = &sc->uri->data.ksk.keywords[i][1];
1237 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1238 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1239 GNUNET_assert (pk != NULL);
1240 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1241 GNUNET_CRYPTO_rsa_key_free (pk);
1242 GNUNET_CRYPTO_hash (&pub,
1244 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1245 &sc->requests[i].query);
1246 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1247 if (sc->requests[i].mandatory)
1248 sc->mandatory_count++;
1249 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1250 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].key);
1253 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1254 if (NULL == sc->client)
1255 return GNUNET_SYSERR;
1256 schedule_transmit_search_request (sc);
1262 * Freeze probes for the given search result.
1264 * @param cls the global FS handle
1265 * @param key the key for the search result (unused)
1266 * @param value the search result to free
1270 search_result_freeze_probes (void *cls, const GNUNET_HashCode * key,
1273 struct GNUNET_FS_SearchResult *sr = value;
1275 if (sr->probe_ctx != NULL)
1277 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1278 sr->probe_ctx = NULL;
1280 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1282 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1283 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1285 if (sr->update_search != NULL)
1286 GNUNET_FS_search_pause (sr->update_search);
1292 * Resume probes for the given search result.
1294 * @param cls the global FS handle
1295 * @param key the key for the search result (unused)
1296 * @param value the search result to free
1300 search_result_resume_probes (void *cls, const GNUNET_HashCode * key,
1303 struct GNUNET_FS_SearchResult *sr = value;
1305 GNUNET_FS_search_start_probe_ (sr);
1306 if (sr->update_search != NULL)
1307 GNUNET_FS_search_continue (sr->update_search);
1313 * Signal suspend and free the given search result.
1315 * @param cls the global FS handle
1316 * @param key the key for the search result (unused)
1317 * @param value the search result to free
1321 search_result_suspend (void *cls, const GNUNET_HashCode * key, void *value)
1323 struct GNUNET_FS_SearchContext *sc = cls;
1324 struct GNUNET_FS_SearchResult *sr = value;
1325 struct GNUNET_FS_ProgressInfo pi;
1327 if (sr->download != NULL)
1328 GNUNET_FS_download_signal_suspend_ (sr->download);
1329 if (sr->update_search != NULL)
1330 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1331 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1332 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1333 pi.value.search.specifics.result_suspend.meta = sr->meta;
1334 pi.value.search.specifics.result_suspend.uri = sr->uri;
1335 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1336 GNUNET_break (NULL == sr->client_info);
1337 GNUNET_free_non_null (sr->serialization);
1338 GNUNET_FS_uri_destroy (sr->uri);
1339 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1340 if (sr->probe_ctx != NULL)
1341 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1342 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1343 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1344 GNUNET_free_non_null (sr->keyword_bitmap);
1351 * Create SUSPEND event for the given search operation
1352 * and then clean up our state (without stop signal).
1354 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1357 GNUNET_FS_search_signal_suspend_ (void *cls)
1359 struct GNUNET_FS_SearchContext *sc = cls;
1360 struct GNUNET_FS_ProgressInfo pi;
1363 GNUNET_FS_end_top (sc->h, sc->top);
1364 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1365 &search_result_suspend, sc);
1366 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1367 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1368 GNUNET_break (NULL == sc->client_info);
1369 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1370 GNUNET_SCHEDULER_cancel (sc->task);
1371 if (NULL != sc->client)
1372 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1373 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1374 if (sc->requests != NULL)
1376 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1377 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1378 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1380 GNUNET_free_non_null (sc->requests);
1381 GNUNET_free_non_null (sc->emsg);
1382 GNUNET_FS_uri_destroy (sc->uri);
1383 GNUNET_free_non_null (sc->serialization);
1389 * Start search for content.
1391 * @param h handle to the file sharing subsystem
1392 * @param uri specifies the search parameters; can be
1393 * a KSK URI or an SKS URI.
1394 * @param anonymity desired level of anonymity
1395 * @param options options for the search
1396 * @param cctx initial value for the client context
1397 * @return context that can be used to control the search
1399 struct GNUNET_FS_SearchContext *
1400 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1401 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1402 enum GNUNET_FS_SearchOptions options, void *cctx)
1404 struct GNUNET_FS_SearchContext *ret;
1406 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1409 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1417 * @param sc context for the search that should be paused
1420 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1422 struct GNUNET_FS_ProgressInfo pi;
1424 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1425 GNUNET_SCHEDULER_cancel (sc->task);
1426 sc->task = GNUNET_SCHEDULER_NO_TASK;
1427 if (NULL != sc->client)
1428 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1430 GNUNET_FS_search_sync_ (sc);
1431 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1432 &search_result_freeze_probes, sc);
1433 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1434 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1439 * Continue paused search.
1441 * @param sc context for the search that should be resumed
1444 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1446 struct GNUNET_FS_ProgressInfo pi;
1448 GNUNET_assert (sc->client == NULL);
1449 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1450 do_reconnect (sc, NULL);
1451 GNUNET_FS_search_sync_ (sc);
1452 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1453 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1454 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1455 &search_result_resume_probes, sc);
1460 * Signal stop for the given search result.
1462 * @param cls the global FS handle
1463 * @param key the key for the search result (unused)
1464 * @param value the search result to free
1468 search_result_stop (void *cls, const GNUNET_HashCode * key, void *value)
1470 struct GNUNET_FS_SearchContext *sc = cls;
1471 struct GNUNET_FS_SearchResult *sr = value;
1472 struct GNUNET_FS_ProgressInfo pi;
1474 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1475 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1476 pi.value.search.specifics.result_stopped.meta = sr->meta;
1477 pi.value.search.specifics.result_stopped.uri = sr->uri;
1478 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1484 * Free the given search result.
1486 * @param cls the global FS handle
1487 * @param key the key for the search result (unused)
1488 * @param value the search result to free
1492 search_result_free (void *cls, const GNUNET_HashCode * key, void *value)
1494 struct GNUNET_FS_SearchContext *sc = cls;
1495 struct GNUNET_FS_SearchResult *sr = value;
1496 struct GNUNET_FS_ProgressInfo pi;
1498 if (NULL != sr->download)
1500 sr->download->search = NULL;
1502 GNUNET_FS_make_top (sr->download->h,
1503 &GNUNET_FS_download_signal_suspend_, sr->download);
1504 if (NULL != sr->download->serialization)
1506 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1507 sr->download->serialization);
1508 GNUNET_free (sr->download->serialization);
1509 sr->download->serialization = NULL;
1511 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1512 GNUNET_FS_download_make_status_ (&pi, sr->download);
1513 GNUNET_FS_download_sync_ (sr->download);
1514 sr->download = NULL;
1516 if (NULL != sr->update_search)
1518 GNUNET_FS_search_stop (sr->update_search);
1519 GNUNET_assert (sr->update_search == NULL);
1521 GNUNET_break (NULL == sr->client_info);
1522 GNUNET_free_non_null (sr->serialization);
1523 GNUNET_FS_uri_destroy (sr->uri);
1524 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1525 if (sr->probe_ctx != NULL)
1526 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1527 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1528 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1529 GNUNET_free_non_null (sr->keyword_bitmap);
1536 * Stop search for content.
1538 * @param sc context for the search that should be stopped
1541 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1543 struct GNUNET_FS_ProgressInfo pi;
1546 if (sc->top != NULL)
1547 GNUNET_FS_end_top (sc->h, sc->top);
1548 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1549 &search_result_stop, sc);
1550 if (sc->psearch_result != NULL)
1551 sc->psearch_result->update_search = NULL;
1552 if (sc->serialization != NULL)
1554 GNUNET_FS_remove_sync_file_ (sc->h,
1555 (sc->psearch_result !=
1556 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1557 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1559 GNUNET_FS_remove_sync_dir_ (sc->h,
1560 (sc->psearch_result !=
1561 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1562 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1564 GNUNET_free (sc->serialization);
1566 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1567 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1568 GNUNET_break (NULL == sc->client_info);
1569 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1570 GNUNET_SCHEDULER_cancel (sc->task);
1571 if (NULL != sc->client)
1572 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1573 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1574 &search_result_free, sc);
1575 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1576 if (sc->requests != NULL)
1578 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1579 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1580 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1582 GNUNET_free_non_null (sc->requests);
1583 GNUNET_free_non_null (sc->emsg);
1584 GNUNET_FS_uri_destroy (sc->uri);
1588 /* end of fs_search.c */