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 Tem ple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file fs/fs_search.c
22 * @brief Helper functions for searching.
23 * @author Christian Grothoff
27 #include "gnunet_constants.h"
28 #include "gnunet_fs_service.h"
29 #include "gnunet_protocols.h"
34 * Number of availability trials we perform per search result.
36 #define AVAILABILITY_TRIALS_MAX 8
39 * Fill in all of the generic fields for a search event and
42 * @param pi structure to fill in
43 * @param sc overall search context
44 * @return value returned by the callback
47 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
48 struct GNUNET_FS_SearchContext *sc)
52 pi->value.search.sc = sc;
53 pi->value.search.cctx = sc->client_info;
54 pi->value.search.pctx =
55 (NULL == sc->psearch_result) ? NULL : sc->psearch_result->client_info;
56 pi->value.search.query = sc->uri;
57 pi->value.search.duration =
58 GNUNET_TIME_absolute_get_duration (sc->start_time);
59 pi->value.search.anonymity = sc->anonymity;
60 ret = sc->h->upcb (sc->h->upcb_cls, pi);
66 * Check if the given result is identical
69 * @param cls points to the URI we check against
71 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
73 * @return GNUNET_SYSERR if the result is present,
77 test_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
79 const struct GNUNET_FS_Uri *uri = cls;
80 struct GNUNET_FS_SearchResult *sr = value;
82 if (GNUNET_FS_uri_test_equal (uri, sr->uri))
89 * We've found a new CHK result. Let the client
92 * @param sc the search context
93 * @param sr the specific result
96 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
97 struct GNUNET_FS_SearchResult *sr)
99 struct GNUNET_FS_ProgressInfo pi;
101 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
102 pi.value.search.specifics.result.meta = sr->meta;
103 pi.value.search.specifics.result.uri = sr->uri;
104 pi.value.search.specifics.result.result = sr;
105 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
106 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
111 * We've found new information about an existing CHK result. Let the
112 * client know about it.
114 * @param sc the search context
115 * @param sr the specific result
118 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
119 struct GNUNET_FS_SearchResult *sr)
121 struct GNUNET_FS_ProgressInfo pi;
123 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
124 pi.value.search.specifics.update.cctx = sr->client_info;
125 pi.value.search.specifics.update.meta = sr->meta;
126 pi.value.search.specifics.update.uri = sr->uri;
127 pi.value.search.specifics.update.availability_rank =
128 2 * sr->availability_success - sr->availability_trials;
129 pi.value.search.specifics.update.availability_certainty =
130 sr->availability_trials;
131 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
132 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
137 * Context for "get_result_present".
139 struct GetResultContext
142 * The URI we're looking for.
144 const struct GNUNET_FS_Uri *uri;
147 * Where to store a pointer to the search
148 * result struct if we found a match.
150 struct GNUNET_FS_SearchResult *sr;
155 * Check if the given result is identical to the given URI and if so
158 * @param cls a "struct GetResultContext"
159 * @param key not used
160 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
161 * should compare with
165 get_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
167 struct GetResultContext *grc = cls;
168 struct GNUNET_FS_SearchResult *sr = value;
170 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
177 * Signal result of last probe to client and then schedule next
181 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
183 struct GNUNET_FS_ProgressInfo pi;
185 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
186 pi.value.search.specifics.update.cctx = sr->client_info;
187 pi.value.search.specifics.update.meta = sr->meta;
188 pi.value.search.specifics.update.uri = sr->uri;
189 pi.value.search.specifics.update.availability_rank = sr->availability_success;
190 pi.value.search.specifics.update.availability_certainty =
191 sr->availability_trials;
192 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
193 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
194 GNUNET_FS_search_start_probe_ (sr);
199 * Handle the case where we have failed to receive a response for our probe.
201 * @param cls our 'struct GNUNET_FS_SearchResult*'
202 * @param tc scheduler context
205 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 struct GNUNET_FS_SearchResult *sr = cls;
209 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
210 sr->availability_trials++;
211 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
212 sr->probe_ctx = NULL;
213 GNUNET_FS_search_result_sync_ (sr);
214 signal_probe_result (sr);
219 * Handle the case where we have gotten a response for our probe.
221 * @param cls our 'struct GNUNET_FS_SearchResult*'
222 * @param tc scheduler context
225 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 struct GNUNET_FS_SearchResult *sr = cls;
229 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
230 sr->availability_trials++;
231 sr->availability_success++;
232 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
233 sr->probe_ctx = NULL;
234 GNUNET_FS_search_result_sync_ (sr);
235 signal_probe_result (sr);
240 * Notification of FS that a search probe has made progress.
241 * This function is used INSTEAD of the client's event handler
242 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
244 * @param cls closure, always NULL (!), actual closure
245 * is in the client-context of the info struct
246 * @param info details about the event, specifying the event type
247 * and various bits about the event
248 * @return client-context (for the next progress call
249 * for this operation; should be set to NULL for
250 * SUSPEND and STOPPED events). The value returned
251 * will be passed to future callbacks in the respective
252 * field in the GNUNET_FS_ProgressInfo struct.
255 GNUNET_FS_search_probe_progress_ (void *cls,
256 const struct GNUNET_FS_ProgressInfo *info)
258 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
259 struct GNUNET_TIME_Relative dur;
261 switch (info->status)
263 case GNUNET_FS_STATUS_DOWNLOAD_START:
266 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
267 /* probes should never be resumed */
270 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
271 /* probes should never be suspended */
274 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
277 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
278 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
280 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
281 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
283 sr->probe_cancel_task =
284 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
285 &probe_failure_handler, sr);
287 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
288 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
290 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
291 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
293 sr->probe_cancel_task =
294 GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
296 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
297 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
299 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
300 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
304 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
305 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task);
306 sr->probe_active_time = GNUNET_TIME_absolute_get ();
307 sr->probe_cancel_task =
308 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
309 &probe_failure_handler, sr);
311 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
312 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
314 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
315 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
317 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
318 sr->remaining_probe_time =
319 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
320 GNUNET_FS_search_result_sync_ (sr);
331 * Start download probes for the given search result.
333 * @param sr the search result
336 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
341 if (NULL != sr->probe_ctx)
343 if (NULL != sr->download)
345 if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
347 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
349 if ( (chk != sr->uri->type) && (loc != sr->uri->type))
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)
391 struct GNUNET_HashCode key;
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;
480 struct GNUNET_HashCode key;
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 (0 == strlen (id_update))
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,
536 struct GNUNET_HashCode q;
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 (struct 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;
711 struct GNUNET_HashCode key;
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,
769 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "Result received has already expired.\n");
773 return; /* result expired */
777 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
778 if (!GNUNET_FS_uri_test_ksk (sc->uri))
783 if (sizeof (struct KBlock) > size)
788 process_kblock (sc, data, size);
790 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
791 if (!GNUNET_FS_uri_test_sks (sc->uri))
796 if (sizeof (struct SBlock) > size)
801 process_sblock (sc, data, size);
803 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
804 if (!GNUNET_FS_uri_test_ksk (sc->uri))
809 if (sizeof (struct NBlock) > size)
814 process_nblock (sc, data, size);
816 case GNUNET_BLOCK_TYPE_ANY:
819 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
822 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
825 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
829 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
830 _("Got result with unknown block type `%d', ignoring"), type);
837 * Shutdown any existing connection to the FS
838 * service and try to establish a fresh one
839 * (and then re-transmit our search request).
841 * @param sc the search to reconnec
844 try_reconnect (struct GNUNET_FS_SearchContext *sc);
848 * Type of a function to call when we receive a message
852 * @param msg message received, NULL on timeout or fatal error
855 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
857 struct GNUNET_FS_SearchContext *sc = cls;
858 const struct ClientPutMessage *cm;
861 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
862 (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
867 msize = ntohs (msg->size);
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 "Receiving %u bytes of result from fs service\n", msize);
870 cm = (const struct ClientPutMessage *) msg;
871 process_result (sc, ntohl (cm->type),
872 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
873 msize - sizeof (struct ClientPutMessage));
874 /* continue receiving */
875 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
876 GNUNET_TIME_UNIT_FOREVER_REL);
881 * Schedule the transmission of the (next) search request
884 * @param sc context for the search
887 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
891 * Closure for 'build_result_set'.
893 struct MessageBuilderContext
896 * How many entries can we store to xoff.
898 unsigned int put_cnt;
901 * How many entries should we skip.
903 unsigned int skip_cnt;
906 * Where to store the keys.
908 struct GNUNET_HashCode *xoff;
911 * Search context we are iterating for.
913 struct GNUNET_FS_SearchContext *sc;
916 * Keyword offset the search result must match (0 for SKS)
918 unsigned int keyword_offset;
923 * Iterating over the known results, pick those matching the given
924 * result range and store their keys at 'xoff'.
926 * @param cls the 'struct MessageBuilderContext'
927 * @param key key for a result
928 * @param value the search result
929 * @return GNUNET_OK to continue iterating
932 build_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
934 struct MessageBuilderContext *mbc = cls;
935 struct GNUNET_FS_SearchResult *sr = value;
937 if ( (NULL != sr->keyword_bitmap) &&
938 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
939 return GNUNET_OK; /* have no match for this keyword yet */
940 if (mbc->skip_cnt > 0)
945 if (0 == mbc->put_cnt)
946 return GNUNET_SYSERR;
947 mbc->sc->search_request_map_offset++;
948 mbc->xoff[--mbc->put_cnt] = *key;
954 * Iterating over the known results, count those
955 * matching the given result range and increment
956 * put count for each.
958 * @param cls the 'struct MessageBuilderContext'
959 * @param key key for a result
960 * @param value the search result
961 * @return GNUNET_OK to continue iterating
964 find_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
966 struct MessageBuilderContext *mbc = cls;
967 struct GNUNET_FS_SearchResult *sr = value;
969 if ( (NULL != sr->keyword_bitmap) &&
970 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
971 return GNUNET_OK; /* have no match for this keyword yet */
978 * We're ready to transmit the search request to the
979 * file-sharing service. Do it.
982 * @param size number of bytes available in buf
983 * @param buf where the callee should write the message
984 * @return number of bytes written to buf
987 transmit_search_request (void *cls, size_t size, void *buf)
989 struct GNUNET_FS_SearchContext *sc = cls;
990 struct MessageBuilderContext mbc;
992 struct SearchMessage *sm;
993 const char *identifier;
994 struct GNUNET_HashCode key;
995 struct GNUNET_HashCode idh;
1005 mbc.skip_cnt = sc->search_request_map_offset;
1007 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1008 mbc.xoff = (struct GNUNET_HashCode *) & sm[1];
1009 options = SEARCH_MESSAGE_OPTION_NONE;
1010 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1011 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1012 if (GNUNET_FS_uri_test_ksk (sc->uri))
1014 msize = sizeof (struct SearchMessage);
1015 GNUNET_assert (size >= msize);
1016 mbc.keyword_offset = sc->keyword_offset;
1017 /* calculate total number of known results (in put_cnt => sqms) */
1019 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1020 &find_result_set, &mbc);
1022 /* calculate how many results we can send in this message */
1023 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1024 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1025 if (sc->search_request_map_offset < sqms)
1026 GNUNET_assert (mbc.put_cnt > 0);
1028 /* now build message */
1029 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1030 sm->header.size = htons (msize);
1031 sm->type = htonl (GNUNET_BLOCK_TYPE_ANY);
1032 sm->anonymity_level = htonl (sc->anonymity);
1033 memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
1034 sm->query = sc->requests[sc->keyword_offset].query;
1035 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1036 &build_result_set, &mbc);
1037 GNUNET_assert (sqms >= sc->search_request_map_offset);
1038 if (sqms != sc->search_request_map_offset)
1040 /* more requesting to be done... */
1041 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1042 schedule_transmit_search_request (sc);
1045 sm->options = htonl (options);
1046 sc->keyword_offset++;
1047 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1049 /* more requesting to be done... */
1050 schedule_transmit_search_request (sc);
1056 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1057 msize = sizeof (struct SearchMessage);
1058 GNUNET_assert (size >= msize);
1059 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_SBLOCK);
1060 sm->anonymity_level = htonl (sc->anonymity);
1061 sm->target = sc->uri->data.sks.namespace;
1062 identifier = sc->uri->data.sks.identifier;
1063 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
1064 GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &idh);
1065 GNUNET_CRYPTO_hash_xor (&idh, &sm->target, &sm->query);
1066 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1067 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1068 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1069 mbc.keyword_offset = 0;
1070 if (sc->search_request_map_offset < sqms)
1071 GNUNET_assert (mbc.put_cnt > 0);
1072 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1073 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1074 &build_result_set, &mbc);
1075 sm->header.size = htons (msize);
1076 GNUNET_assert (sqms >= sc->search_request_map_offset);
1077 if (sqms != sc->search_request_map_offset)
1079 /* more requesting to be done... */
1080 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1081 schedule_transmit_search_request (sc);
1084 sm->options = htonl (options);
1086 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1087 GNUNET_TIME_UNIT_FOREVER_REL);
1093 * Schedule the transmission of the (next) search request
1096 * @param sc context for the search
1099 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1105 size = sizeof (struct SearchMessage);
1107 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1108 sc->search_request_map_offset;
1109 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
1110 fit = GNUNET_MIN (fit, sqms);
1111 size += sizeof (struct GNUNET_HashCode) * fit;
1112 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1113 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1114 GNUNET_NO, &transmit_search_request, sc);
1120 * Reconnect to the FS service and transmit
1123 * @param cls our search context
1127 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1129 struct GNUNET_FS_SearchContext *sc = cls;
1130 struct GNUNET_CLIENT_Connection *client;
1132 sc->task = GNUNET_SCHEDULER_NO_TASK;
1133 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1139 sc->client = client;
1140 sc->search_request_map_offset = 0;
1141 sc->keyword_offset = 0;
1142 schedule_transmit_search_request (sc);
1147 * Shutdown any existing connection to the FS
1148 * service and try to establish a fresh one
1149 * (and then re-transmit our search request).
1151 * @param sc the search to reconnec
1154 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1156 if (NULL != sc->client)
1158 GNUNET_CLIENT_disconnect (sc->client);
1162 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1168 * Start search for content, internal API.
1170 * @param h handle to the file sharing subsystem
1171 * @param uri specifies the search parameters; can be
1172 * a KSK URI or an SKS URI.
1173 * @param anonymity desired level of anonymity
1174 * @param options options for the search
1175 * @param cctx initial value for the client context
1176 * @param psearch parent search result (for namespace update searches)
1177 * @return context that can be used to control the search
1179 static struct GNUNET_FS_SearchContext *
1180 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1181 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1182 void *cctx, struct GNUNET_FS_SearchResult *psearch)
1184 struct GNUNET_FS_SearchContext *sc;
1185 struct GNUNET_FS_ProgressInfo pi;
1187 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1189 sc->options = options;
1190 sc->uri = GNUNET_FS_uri_dup (uri);
1191 sc->anonymity = anonymity;
1192 sc->start_time = GNUNET_TIME_absolute_get ();
1193 if (NULL != psearch)
1195 sc->psearch_result = psearch;
1196 psearch->update_search = sc;
1198 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1199 sc->client_info = cctx;
1200 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1202 GNUNET_FS_uri_destroy (sc->uri);
1203 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1207 GNUNET_FS_search_sync_ (sc);
1208 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1209 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1215 * Build the request and actually initiate the search using the
1216 * GNUnet FS service.
1218 * @param sc search context
1219 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1222 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1225 const char *keyword;
1226 struct GNUNET_HashCode hc;
1227 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1228 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1230 GNUNET_assert (NULL == sc->client);
1231 if (GNUNET_FS_uri_test_ksk (sc->uri))
1233 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1235 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1236 sc->uri->data.ksk.keywordCount);
1237 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1239 keyword = &sc->uri->data.ksk.keywords[i][1];
1240 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1241 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1242 GNUNET_assert (NULL != pk);
1243 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1244 GNUNET_CRYPTO_rsa_key_free (pk);
1245 GNUNET_CRYPTO_hash (&pub,
1247 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1248 &sc->requests[i].query);
1249 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1250 if (sc->requests[i].mandatory)
1251 sc->mandatory_count++;
1252 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1253 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].key);
1256 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1257 if (NULL == sc->client)
1258 return GNUNET_SYSERR;
1259 schedule_transmit_search_request (sc);
1265 * Freeze probes for the given search result.
1267 * @param cls the global FS handle
1268 * @param key the key for the search result (unused)
1269 * @param value the search result to free
1273 search_result_freeze_probes (void *cls, const struct GNUNET_HashCode * key,
1276 struct GNUNET_FS_SearchResult *sr = value;
1278 if (NULL != sr->probe_ctx)
1280 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1281 sr->probe_ctx = NULL;
1283 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1285 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1286 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1288 if (NULL != sr->update_search)
1289 GNUNET_FS_search_pause (sr->update_search);
1295 * Resume probes for the given search result.
1297 * @param cls the global FS handle
1298 * @param key the key for the search result (unused)
1299 * @param value the search result to free
1303 search_result_resume_probes (void *cls, const struct GNUNET_HashCode * key,
1306 struct GNUNET_FS_SearchResult *sr = value;
1308 GNUNET_FS_search_start_probe_ (sr);
1309 if (NULL != sr->update_search)
1310 GNUNET_FS_search_continue (sr->update_search);
1316 * Signal suspend and free the given search result.
1318 * @param cls the global FS handle
1319 * @param key the key for the search result (unused)
1320 * @param value the search result to free
1324 search_result_suspend (void *cls, const struct GNUNET_HashCode * key, void *value)
1326 struct GNUNET_FS_SearchContext *sc = cls;
1327 struct GNUNET_FS_SearchResult *sr = value;
1328 struct GNUNET_FS_ProgressInfo pi;
1330 if (NULL != sr->download)
1332 GNUNET_FS_download_signal_suspend_ (sr->download);
1333 sr->download = NULL;
1335 if (NULL != sr->probe_ctx)
1337 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1338 sr->probe_ctx = NULL;
1340 if (NULL != sr->update_search)
1342 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1343 sr->update_search = NULL;
1345 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1346 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1347 pi.value.search.specifics.result_suspend.meta = sr->meta;
1348 pi.value.search.specifics.result_suspend.uri = sr->uri;
1349 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1350 GNUNET_break (NULL == sr->client_info);
1351 GNUNET_free_non_null (sr->serialization);
1352 GNUNET_FS_uri_destroy (sr->uri);
1353 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1354 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1356 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1357 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1359 GNUNET_free_non_null (sr->keyword_bitmap);
1366 * Create SUSPEND event for the given search operation
1367 * and then clean up our state (without stop signal).
1369 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1372 GNUNET_FS_search_signal_suspend_ (void *cls)
1374 struct GNUNET_FS_SearchContext *sc = cls;
1375 struct GNUNET_FS_ProgressInfo pi;
1378 GNUNET_FS_end_top (sc->h, sc->top);
1379 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1380 &search_result_suspend, sc);
1381 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1382 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1383 GNUNET_break (NULL == sc->client_info);
1384 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1385 GNUNET_SCHEDULER_cancel (sc->task);
1386 if (NULL != sc->client)
1387 GNUNET_CLIENT_disconnect (sc->client);
1388 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1389 if (NULL != sc->requests)
1391 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1392 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1393 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1395 GNUNET_free_non_null (sc->requests);
1396 GNUNET_free_non_null (sc->emsg);
1397 GNUNET_FS_uri_destroy (sc->uri);
1398 GNUNET_free_non_null (sc->serialization);
1404 * Start search for content.
1406 * @param h handle to the file sharing subsystem
1407 * @param uri specifies the search parameters; can be
1408 * a KSK URI or an SKS URI.
1409 * @param anonymity desired level of anonymity
1410 * @param options options for the search
1411 * @param cctx initial value for the client context
1412 * @return context that can be used to control the search
1414 struct GNUNET_FS_SearchContext *
1415 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1416 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1417 enum GNUNET_FS_SearchOptions options, void *cctx)
1419 struct GNUNET_FS_SearchContext *ret;
1421 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1424 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1432 * @param sc context for the search that should be paused
1435 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1437 struct GNUNET_FS_ProgressInfo pi;
1439 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1440 GNUNET_SCHEDULER_cancel (sc->task);
1441 sc->task = GNUNET_SCHEDULER_NO_TASK;
1442 if (NULL != sc->client)
1443 GNUNET_CLIENT_disconnect (sc->client);
1445 GNUNET_FS_search_sync_ (sc);
1446 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1447 &search_result_freeze_probes, sc);
1448 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1449 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1454 * Continue paused search.
1456 * @param sc context for the search that should be resumed
1459 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1461 struct GNUNET_FS_ProgressInfo pi;
1463 GNUNET_assert (NULL == sc->client);
1464 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sc->task);
1465 do_reconnect (sc, NULL);
1466 GNUNET_FS_search_sync_ (sc);
1467 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1468 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1469 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1470 &search_result_resume_probes, sc);
1475 * Signal stop for the given search result.
1477 * @param cls the global FS handle
1478 * @param key the key for the search result (unused)
1479 * @param value the search result to free
1483 search_result_stop (void *cls, const struct GNUNET_HashCode * key, void *value)
1485 struct GNUNET_FS_SearchContext *sc = cls;
1486 struct GNUNET_FS_SearchResult *sr = value;
1487 struct GNUNET_FS_ProgressInfo pi;
1489 if (NULL != sr->probe_ctx)
1491 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1492 sr->probe_ctx = NULL;
1494 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1496 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1497 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1500 if (NULL != sr->download)
1502 sr->download->search = NULL;
1504 GNUNET_FS_make_top (sr->download->h,
1505 &GNUNET_FS_download_signal_suspend_, sr->download);
1506 if (NULL != sr->download->serialization)
1508 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1509 sr->download->serialization);
1510 GNUNET_free (sr->download->serialization);
1511 sr->download->serialization = NULL;
1513 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1514 GNUNET_FS_download_make_status_ (&pi, sr->download);
1515 GNUNET_FS_download_sync_ (sr->download);
1516 sr->download = NULL;
1518 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1519 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1520 pi.value.search.specifics.result_stopped.meta = sr->meta;
1521 pi.value.search.specifics.result_stopped.uri = sr->uri;
1522 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1528 * Free the given search result.
1530 * @param cls the global FS handle
1531 * @param key the key for the search result (unused)
1532 * @param value the search result to free
1536 search_result_free (void *cls, const struct GNUNET_HashCode * key, void *value)
1538 struct GNUNET_FS_SearchResult *sr = value;
1540 if (NULL != sr->update_search)
1542 GNUNET_FS_search_stop (sr->update_search);
1543 GNUNET_assert (NULL == sr->update_search);
1545 GNUNET_break (NULL == sr->probe_ctx);
1546 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task);
1547 GNUNET_break (NULL == sr->client_info);
1548 GNUNET_free_non_null (sr->serialization);
1549 GNUNET_FS_uri_destroy (sr->uri);
1550 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1551 GNUNET_free_non_null (sr->keyword_bitmap);
1558 * Stop search for content.
1560 * @param sc context for the search that should be stopped
1563 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1565 struct GNUNET_FS_ProgressInfo pi;
1568 if (NULL != sc->top)
1569 GNUNET_FS_end_top (sc->h, sc->top);
1570 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1571 &search_result_stop, sc);
1572 if (NULL != sc->psearch_result)
1573 sc->psearch_result->update_search = NULL;
1574 if (NULL != sc->serialization)
1576 GNUNET_FS_remove_sync_file_ (sc->h,
1577 (sc->psearch_result !=
1578 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1579 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1581 GNUNET_FS_remove_sync_dir_ (sc->h,
1582 (sc->psearch_result !=
1583 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1584 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1586 GNUNET_free (sc->serialization);
1588 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1589 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1590 GNUNET_break (NULL == sc->client_info);
1591 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1592 GNUNET_SCHEDULER_cancel (sc->task);
1593 if (NULL != sc->client)
1594 GNUNET_CLIENT_disconnect (sc->client);
1595 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1596 &search_result_free, sc);
1597 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1598 if (NULL != sc->requests)
1600 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1601 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1602 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1604 GNUNET_free_non_null (sc->requests);
1605 GNUNET_free_non_null (sc->emsg);
1606 GNUNET_FS_uri_destroy (sc->uri);
1610 /* end of fs_search.c */