2 This file is part of GNUnet.
3 Copyright (C) 2001-2014 GNUnet e.V.
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 02110-1301, USA.
21 * @file fs/fs_search.c
22 * @brief Helper functions for searching.
23 * @author Christian Grothoff
26 #include "gnunet_constants.h"
27 #include "gnunet_fs_service.h"
28 #include "gnunet_protocols.h"
30 #include "fs_publish_ublock.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 h file-sharing handle
44 * @param sc overall search context
45 * @return value returned by the callback
48 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
49 struct GNUNET_FS_Handle *h,
50 struct GNUNET_FS_SearchContext *sc)
54 pi->value.search.sc = sc;
55 pi->value.search.cctx = (NULL != sc) ? sc->client_info : NULL;
56 pi->value.search.pctx =
57 ((NULL == sc) || (NULL == sc->psearch_result))
59 : sc->psearch_result->client_info;
60 pi->value.search.query = (NULL != sc) ? sc->uri : NULL;
61 pi->value.search.duration = (NULL != sc)
62 ? GNUNET_TIME_absolute_get_duration (sc->start_time)
63 : GNUNET_TIME_UNIT_ZERO;
64 pi->value.search.anonymity = (NULL != sc) ? sc->anonymity : 0;
66 ret = h->upcb (h->upcb_cls, pi);
72 * Check if the given result is identical to the given URI.
74 * @param cls points to the URI we check against
76 * @param value a `struct GNUNET_FS_SearchResult` who's URI we
78 * @return #GNUNET_SYSERR if the result is present,
79 * #GNUNET_OK otherwise
82 test_result_present (void *cls,
83 const struct GNUNET_HashCode * key,
86 const struct GNUNET_FS_Uri *uri = cls;
87 struct GNUNET_FS_SearchResult *sr = value;
89 if (GNUNET_FS_uri_test_equal (uri, sr->uri))
96 * We've found a new CHK result. Let the client
99 * @param sc the search context
100 * @param sr the specific result
103 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
104 struct GNUNET_FS_SearchResult *sr)
106 struct GNUNET_FS_ProgressInfo pi;
108 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
109 pi.value.search.specifics.result.meta = sr->meta;
110 pi.value.search.specifics.result.uri = sr->uri;
111 pi.value.search.specifics.result.result = sr;
112 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
113 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
118 * We've found new information about an existing CHK result. Let the
119 * client know about it.
121 * @param sc the search context
122 * @param sr the specific result
125 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
126 struct GNUNET_FS_SearchResult *sr)
128 struct GNUNET_FS_ProgressInfo pi;
130 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
131 pi.value.search.specifics.update.cctx = sr->client_info;
132 pi.value.search.specifics.update.meta = sr->meta;
133 pi.value.search.specifics.update.uri = sr->uri;
134 pi.value.search.specifics.update.availability_rank =
135 2 * sr->availability_success - sr->availability_trials;
136 pi.value.search.specifics.update.availability_certainty =
137 sr->availability_trials;
138 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
139 pi.value.search.specifics.update.current_probe_time
140 = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
141 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
146 * Context for "get_result_present".
148 struct GetResultContext
151 * The URI we're looking for.
153 const struct GNUNET_FS_Uri *uri;
156 * Where to store a pointer to the search
157 * result struct if we found a match.
159 struct GNUNET_FS_SearchResult *sr;
164 * Check if the given result is identical to the given URI and if so
167 * @param cls a `struct GetResultContext`
168 * @param key not used
169 * @param value a `struct GNUNET_FS_SearchResult` who's URI we
170 * should compare with
174 get_result_present (void *cls,
175 const struct GNUNET_HashCode *key,
178 struct GetResultContext *grc = cls;
179 struct GNUNET_FS_SearchResult *sr = value;
181 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
188 * Signal result of last probe to client and then schedule next
191 * @param sr search result to signal for
194 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
196 struct GNUNET_FS_ProgressInfo pi;
198 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
199 pi.value.search.specifics.update.cctx = sr->client_info;
200 pi.value.search.specifics.update.meta = sr->meta;
201 pi.value.search.specifics.update.uri = sr->uri;
202 pi.value.search.specifics.update.availability_rank
203 = 2 * sr->availability_success - sr->availability_trials;
204 pi.value.search.specifics.update.availability_certainty
205 = sr->availability_trials;
206 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
207 pi.value.search.specifics.update.current_probe_time
208 = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
209 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sr->sc);
210 GNUNET_FS_search_start_probe_ (sr);
215 * Handle the case where we have failed to receive a response for our probe.
217 * @param cls our `struct GNUNET_FS_SearchResult *`
220 probe_failure_handler (void *cls)
222 struct GNUNET_FS_SearchResult *sr = cls;
224 sr->probe_cancel_task = NULL;
225 sr->availability_trials++;
226 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
227 sr->probe_ctx = NULL;
228 GNUNET_FS_stop_probe_ping_task_ (sr);
229 GNUNET_FS_search_result_sync_ (sr);
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "Probe #%u for search result %p failed\n",
232 sr->availability_trials,
234 signal_probe_result (sr);
239 * Handle the case where we have gotten a response for our probe.
241 * @param cls our `struct GNUNET_FS_SearchResult *`
244 probe_success_handler (void *cls)
246 struct GNUNET_FS_SearchResult *sr = cls;
248 sr->probe_cancel_task = NULL;
249 sr->availability_trials++;
250 sr->availability_success++;
251 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
252 sr->probe_ctx = NULL;
253 GNUNET_FS_stop_probe_ping_task_ (sr);
254 GNUNET_FS_search_result_sync_ (sr);
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Probe #%u for search result %p succeeded\n",
257 sr->availability_trials,
259 signal_probe_result (sr);
264 * Notification of FS that a search probe has made progress.
265 * This function is used INSTEAD of the client's event handler
266 * for downloads where the #GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
268 * @param cls closure, always NULL (!), actual closure
269 * is in the client-context of the info struct
270 * @param info details about the event, specifying the event type
271 * and various bits about the event
272 * @return client-context (for the next progress call
273 * for this operation; should be set to NULL for
274 * SUSPEND and STOPPED events). The value returned
275 * will be passed to future callbacks in the respective
276 * field in the `struct GNUNET_FS_ProgressInfo`.
279 GNUNET_FS_search_probe_progress_ (void *cls,
280 const struct GNUNET_FS_ProgressInfo *info)
282 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
283 struct GNUNET_TIME_Relative dur;
285 switch (info->status)
287 case GNUNET_FS_STATUS_DOWNLOAD_START:
290 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
291 /* probes should never be resumed */
294 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
295 /* probes should never be suspended */
298 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
301 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
302 if (NULL != sr->probe_cancel_task)
304 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
305 sr->probe_cancel_task = NULL;
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_COMPLETED:
312 if (NULL != sr->probe_cancel_task)
314 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
315 sr->probe_cancel_task = NULL;
317 sr->probe_cancel_task =
318 GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
320 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
321 if (NULL != sr->probe_cancel_task)
323 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
324 sr->probe_cancel_task = NULL;
328 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
329 if (NULL == sr->probe_cancel_task)
331 sr->probe_active_time = GNUNET_TIME_absolute_get ();
332 sr->probe_cancel_task =
333 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
334 &probe_failure_handler, sr);
337 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
338 if (NULL != sr->probe_cancel_task)
340 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
341 sr->probe_cancel_task = NULL;
343 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
344 sr->remaining_probe_time =
345 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
346 if (0 == sr->remaining_probe_time.rel_value_us)
347 sr->probe_cancel_task =
348 GNUNET_SCHEDULER_add_now (&probe_failure_handler, sr);
349 GNUNET_FS_search_result_sync_ (sr);
360 * Task run periodically to remind clients that a probe is active.
362 * @param cls the `struct GNUNET_FS_SearchResult` that we are probing for
365 probe_ping_task_cb (void *cls)
367 struct GNUNET_FS_Handle *h = cls;
368 struct GNUNET_FS_SearchResult *sr;
370 for (sr = h->probes_head; NULL != sr; sr = sr->next)
371 if (NULL != sr->probe_ctx->client)
372 signal_probe_result (sr);
374 = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
381 * Start the ping task for this search result.
383 * @param sr result to start pinging for.
386 start_probe_ping_task (struct GNUNET_FS_SearchResult *sr)
388 struct GNUNET_FS_Handle *h = sr->h;
390 GNUNET_CONTAINER_DLL_insert (h->probes_head,
393 if (NULL == h->probe_ping_task)
395 = GNUNET_SCHEDULER_add_now (&probe_ping_task_cb,
401 * Stop the ping task for this search result.
403 * @param sr result to start pinging for.
406 GNUNET_FS_stop_probe_ping_task_ (struct GNUNET_FS_SearchResult *sr)
408 struct GNUNET_FS_Handle *h = sr->h;
410 GNUNET_CONTAINER_DLL_remove (h->probes_head,
413 if (NULL == h->probes_head)
415 GNUNET_SCHEDULER_cancel (h->probe_ping_task);
416 h->probe_ping_task = NULL;
422 * Start download probes for the given search result.
424 * @param sr the search result
427 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
432 if (NULL != sr->probe_ctx)
434 if (NULL != sr->download)
436 if (0 == (sr->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
438 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
440 if ( (GNUNET_FS_URI_CHK != sr->uri->type) && (GNUNET_FS_URI_LOC != sr->uri->type))
442 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
445 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
447 off = len / DBLOCK_SIZE;
449 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
451 if (len - off < DBLOCK_SIZE)
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
456 "Starting probe #%u (at offset %llu) for search result %p\n",
457 sr->availability_trials + 1,
458 (unsigned long long) off,
460 sr->remaining_probe_time =
461 GNUNET_TIME_relative_multiply (sr->h->avg_block_latency,
462 2 * (1 + sr->availability_trials));
464 GNUNET_FS_download_start (sr->h, sr->uri, sr->meta, NULL, NULL, off,
466 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
467 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
468 start_probe_ping_task (sr);
473 * Start download probes for the given search result.
475 * @param h file-sharing handle to use for the operation
476 * @param uri URI to probe
477 * @param meta meta data associated with the URI
478 * @param client_info client info pointer to use for associated events
479 * @param anonymity anonymity level to use for the probes
480 * @return the search result handle to access the probe activity
482 struct GNUNET_FS_SearchResult *
483 GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
484 const struct GNUNET_FS_Uri *uri,
485 const struct GNUNET_CONTAINER_MetaData *meta,
489 struct GNUNET_FS_SearchResult *sr;
491 GNUNET_assert (NULL != h);
492 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
494 sr->uri = GNUNET_FS_uri_dup (uri);
495 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
496 sr->client_info = client_info;
497 sr->anonymity = anonymity;
498 GNUNET_FS_search_start_probe_ (sr);
504 * Stop probing activity associated with a search result.
506 * @param sr search result
509 GNUNET_FS_search_stop_probe_ (struct GNUNET_FS_SearchResult *sr)
511 if (NULL != sr->probe_ctx)
513 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
514 sr->probe_ctx = NULL;
515 GNUNET_FS_stop_probe_ping_task_ (sr);
517 if (NULL != sr->probe_cancel_task)
519 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
520 sr->probe_cancel_task = NULL;
526 * Stop probe activity. Must ONLY be used on values
527 * returned from #GNUNET_FS_probe.
529 * @param sr search result to stop probing for (freed)
530 * @return the value of the 'client_info' pointer
533 GNUNET_FS_probe_stop (struct GNUNET_FS_SearchResult *sr)
537 GNUNET_assert (NULL == sr->sc);
538 GNUNET_FS_search_stop_probe_ (sr);
539 GNUNET_FS_uri_destroy (sr->uri);
540 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
541 client_info = sr->client_info;
548 * We have received a KSK result. Check how it fits in with the
549 * overall query and notify the client accordingly.
551 * @param sc context for the overall query
552 * @param ent entry for the specific keyword
553 * @param uri the URI that was found
554 * @param meta metadata associated with the URI
555 * under the @a ent keyword
558 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
559 struct SearchRequestEntry *ent,
560 const struct GNUNET_FS_Uri *uri,
561 const struct GNUNET_CONTAINER_MetaData *meta)
563 struct GNUNET_HashCode key;
564 struct GNUNET_FS_SearchResult *sr;
565 struct GetResultContext grc;
570 GNUNET_assert (NULL != sc);
571 GNUNET_FS_uri_to_key (uri, &key);
573 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
575 &test_result_present,
577 return; /* duplicate result */
578 /* try to find search result in master map */
581 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
583 &get_result_present, &grc);
585 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
588 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
591 sr->anonymity = sc->anonymity;
592 sr->uri = GNUNET_FS_uri_dup (uri);
593 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
594 sr->mandatory_missing = sc->mandatory_count;
596 sr->keyword_bitmap = GNUNET_malloc ((sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
597 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
602 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
604 GNUNET_break (GNUNET_OK ==
605 GNUNET_CONTAINER_multihashmap_put (ent->results,
608 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
610 koff = ent - sc->requests;
611 GNUNET_assert ( (ent >= sc->requests) &&
612 (koff < sc->uri->data.ksk.keywordCount));
613 sr->keyword_bitmap[koff / 8] |= (1 << (koff % 8));
614 /* check if mandatory satisfied */
615 if (1 <= GNUNET_CONTAINER_multihashmap_size (ent->results))
619 GNUNET_break (sr->mandatory_missing > 0);
620 sr->mandatory_missing--;
624 sr->optional_support++;
627 if (0 != sr->mandatory_missing)
629 GNUNET_break (NULL == sr->client_info);
633 notify_client_chk_result (sc, sr);
635 notify_client_chk_update (sc, sr);
636 GNUNET_FS_search_result_sync_ (sr);
637 GNUNET_FS_search_start_probe_ (sr);
642 * Start search for content, internal API.
644 * @param h handle to the file sharing subsystem
645 * @param uri specifies the search parameters; can be
646 * a KSK URI or an SKS URI.
647 * @param anonymity desired level of anonymity
648 * @param options options for the search
649 * @param cctx client context
650 * @param psearch parent search result (for namespace update searches)
651 * @return context that can be used to control the search
653 static struct GNUNET_FS_SearchContext *
654 search_start (struct GNUNET_FS_Handle *h,
655 const struct GNUNET_FS_Uri *uri,
657 enum GNUNET_FS_SearchOptions options,
659 struct GNUNET_FS_SearchResult *psearch);
663 * We have received an SKS result. Start searching for updates and
664 * notify the client if it is a new result.
666 * @param sc context for the overall query
667 * @param id_update identifier for updates, NULL for none
668 * @param uri the URI that was found
669 * @param meta metadata associated with the URI
672 process_sks_result (struct GNUNET_FS_SearchContext *sc,
673 const char *id_update,
674 const struct GNUNET_FS_Uri *uri,
675 const struct GNUNET_CONTAINER_MetaData *meta)
677 struct GNUNET_FS_Uri uu;
678 struct GNUNET_HashCode key;
679 struct GNUNET_FS_SearchResult *sr;
682 GNUNET_assert (NULL != sc);
683 GNUNET_FS_uri_to_key (uri, &key);
684 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
687 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
688 &test_result_present,
690 return; /* duplicate result */
691 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
694 sr->anonymity = sc->anonymity;
695 sr->uri = GNUNET_FS_uri_dup (uri);
696 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
698 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
700 GNUNET_FS_search_result_sync_ (sr);
701 GNUNET_FS_search_start_probe_ (sr);
703 if (0 == sr->mandatory_missing)
704 notify_client_chk_result (sc, sr);
706 GNUNET_break (NULL == sr->client_info);
707 /* search for updates */
708 if (0 == strlen (id_update))
709 return; /* no updates */
710 uu.type = GNUNET_FS_URI_SKS;
711 uu.data.sks.ns = sc->uri->data.sks.ns;
712 uu.data.sks.identifier = GNUNET_strdup (id_update);
713 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
714 GNUNET_free (uu.data.sks.identifier);
719 * Decrypt a ublock using a 'keyword' as the passphrase. Given the
720 * KSK public key derived from the keyword, this function looks up
721 * the original keyword in the search context and decrypts the
722 * given ciphertext block.
724 * @param sc search context with the keywords
725 * @param dpub derived public key used for the search
726 * @param edata encrypted data
727 * @param edata_size number of bytes in @a edata (and @a data)
728 * @param data where to store the plaintext
729 * @return keyword index on success, #GNUNET_SYSERR on error (no such
730 * keyword, internal error)
733 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
734 const struct GNUNET_CRYPTO_EcdsaPublicKey *dpub,
739 const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
740 struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
744 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
745 if (0 == memcmp (dpub,
746 &sc->requests[i].dpub,
747 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
749 if (i == sc->uri->data.ksk.keywordCount)
751 /* oops, does not match any of our keywords!? */
753 return GNUNET_SYSERR;
756 anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
757 GNUNET_CRYPTO_ecdsa_key_get_public (anon, &anon_pub);
758 GNUNET_FS_ublock_decrypt_ (edata, edata_size,
760 sc->requests[i].keyword,
767 * Process a keyword search result. The actual type of block is
768 * a UBlock; we know it is a keyword search result because that's
769 * what we were searching for.
771 * @param sc our search context
772 * @param ub the ublock with the keyword search result
773 * @param size size of @a ub
776 process_kblock (struct GNUNET_FS_SearchContext *sc,
777 const struct UBlock *ub,
781 char pt[size - sizeof (struct UBlock)];
783 struct GNUNET_CONTAINER_MetaData *meta;
784 struct GNUNET_FS_Uri *uri;
788 if (-1 == (i = decrypt_block_with_keyword (sc,
789 &ub->verification_key,
791 size - sizeof (struct UBlock),
794 /* parse; pt[0] is just '\0', so we skip over that */
795 eos = memchr (&pt[1], '\0', sizeof (pt) - 1);
801 if (NULL == (uri = GNUNET_FS_uri_parse (&pt[1], &emsg)))
803 if (GNUNET_FS_VERSION > 0x00090400)
805 /* we broke this in 0x00090300, so don't bitch
806 too loudly just one version up... */
807 GNUNET_break_op (0); /* ublock malformed */
808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
809 _("Failed to parse URI `%s': %s\n"),
813 GNUNET_free_non_null (emsg);
817 if (sizeof (pt) == j)
818 meta = GNUNET_CONTAINER_meta_data_create ();
820 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
823 GNUNET_break_op (0); /* ublock malformed */
824 GNUNET_FS_uri_destroy (uri);
827 process_ksk_result (sc,
833 GNUNET_CONTAINER_meta_data_destroy (meta);
834 GNUNET_FS_uri_destroy (uri);
839 * Process a namespace-search result. The actual type of block is
840 * a UBlock; we know it is a namespace search result because that's
841 * what we were searching for.
843 * @param sc our search context
844 * @param ub the ublock with a namespace result
845 * @param size size of @a ub
848 process_sblock (struct GNUNET_FS_SearchContext *sc,
849 const struct UBlock *ub,
852 size_t len = size - sizeof (struct UBlock);
854 struct GNUNET_FS_Uri *uri;
855 struct GNUNET_CONTAINER_MetaData *meta;
861 GNUNET_FS_ublock_decrypt_ (&ub[1], len,
862 &sc->uri->data.sks.ns,
863 sc->uri->data.sks.identifier,
866 if (0 == (off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris)))
868 GNUNET_break_op (0); /* ublock malformed */
871 if (NULL == (meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off)))
873 GNUNET_break_op (0); /* ublock malformed */
876 if (NULL == (uri = GNUNET_FS_uri_parse (uris, &emsg)))
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
879 _("Failed to parse URI `%s': %s\n"),
881 GNUNET_break_op (0); /* ublock malformed */
882 GNUNET_free_non_null (emsg);
883 GNUNET_CONTAINER_meta_data_destroy (meta);
887 process_sks_result (sc, id, uri, meta);
889 GNUNET_FS_uri_destroy (uri);
890 GNUNET_CONTAINER_meta_data_destroy (meta);
895 * Process a search result.
897 * @param sc our search context
898 * @param type type of the result
899 * @param expiration when it will expire
900 * @param data the (encrypted) response
901 * @param size size of @a data
904 process_result (struct GNUNET_FS_SearchContext *sc,
905 enum GNUNET_BLOCK_Type type,
906 struct GNUNET_TIME_Absolute expiration,
910 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value_us > 0)
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Result received has already expired.\n");
914 return; /* result expired */
918 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
919 if (GNUNET_FS_URI_SKS == sc->uri->type)
920 process_sblock (sc, data, size);
922 process_kblock (sc, data, size);
924 case GNUNET_BLOCK_TYPE_ANY:
927 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
930 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
933 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
937 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
938 _("Got result with unknown block type `%d', ignoring"), type);
945 * Shutdown any existing connection to the FS
946 * service and try to establish a fresh one
947 * (and then re-transmit our search request).
949 * @param sc the search to reconnec
952 try_reconnect (struct GNUNET_FS_SearchContext *sc);
956 * Type of a function to call when we receive a message
960 * @param msg message received, NULL on timeout or fatal error
963 receive_results (void *cls,
964 const struct GNUNET_MessageHeader *msg)
966 struct GNUNET_FS_SearchContext *sc = cls;
967 const struct ClientPutMessage *cm;
970 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
971 (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
976 msize = ntohs (msg->size);
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Receiving %u bytes of result from fs service\n",
980 cm = (const struct ClientPutMessage *) msg;
981 process_result (sc, ntohl (cm->type),
982 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
983 msize - sizeof (struct ClientPutMessage));
984 /* continue receiving */
985 GNUNET_CLIENT_receive (sc->client,
988 GNUNET_TIME_UNIT_FOREVER_REL);
993 * Schedule the transmission of the (next) search request
996 * @param sc context for the search
999 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
1003 * Closure for #build_result_set().
1005 struct MessageBuilderContext
1008 * How many entries can we store to xoff.
1010 unsigned int put_cnt;
1013 * How many entries should we skip.
1015 unsigned int skip_cnt;
1018 * Where to store the keys.
1020 struct GNUNET_HashCode *xoff;
1023 * Search context we are iterating for.
1025 struct GNUNET_FS_SearchContext *sc;
1028 * Keyword offset the search result must match (0 for SKS)
1030 unsigned int keyword_offset;
1035 * Iterating over the known results, pick those matching the given
1036 * result range and store their keys at 'xoff'.
1038 * @param cls the `struct MessageBuilderContext`
1039 * @param key key for a result
1040 * @param value the search result
1041 * @return #GNUNET_OK to continue iterating
1044 build_result_set (void *cls,
1045 const struct GNUNET_HashCode *key,
1048 struct MessageBuilderContext *mbc = cls;
1049 struct GNUNET_FS_SearchResult *sr = value;
1051 if ( (NULL != sr->keyword_bitmap) &&
1052 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1053 return GNUNET_OK; /* have no match for this keyword yet */
1054 if (mbc->skip_cnt > 0)
1059 if (0 == mbc->put_cnt)
1060 return GNUNET_SYSERR;
1061 mbc->sc->search_request_map_offset++;
1062 mbc->xoff[--mbc->put_cnt] = *key;
1069 * Iterating over the known results, count those matching the given
1070 * result range and increment put count for each.
1072 * @param cls the `struct MessageBuilderContext`
1073 * @param key key for a result
1074 * @param value the search result
1075 * @return #GNUNET_OK to continue iterating
1078 find_result_set (void *cls,
1079 const struct GNUNET_HashCode *key,
1082 struct MessageBuilderContext *mbc = cls;
1083 struct GNUNET_FS_SearchResult *sr = value;
1085 if ( (NULL != sr->keyword_bitmap) &&
1086 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1087 return GNUNET_OK; /* have no match for this keyword yet */
1094 * We're ready to transmit the search request to the file-sharing
1095 * service. Do it. If the request is too large to fit into a single
1096 * message, transmit in increments.
1098 * @param cls closure
1099 * @param size number of bytes available in @a buf
1100 * @param buf where the callee should write the message
1101 * @return number of bytes written to @a buf
1104 transmit_search_request (void *cls,
1108 struct GNUNET_FS_SearchContext *sc = cls;
1109 struct MessageBuilderContext mbc;
1111 struct SearchMessage *sm;
1112 struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
1113 unsigned int total_seen_results; /* total number of result hashes to send */
1114 unsigned int message_size_limit;
1123 mbc.skip_cnt = sc->search_request_map_offset;
1125 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1126 mbc.xoff = (struct GNUNET_HashCode *) &sm[1];
1127 options = SEARCH_MESSAGE_OPTION_NONE;
1128 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1129 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1130 if (GNUNET_FS_uri_test_ksk (sc->uri))
1132 msize = sizeof (struct SearchMessage);
1133 GNUNET_assert (size >= msize);
1134 mbc.keyword_offset = sc->keyword_offset;
1135 /* calculate total number of known results (in put_cnt => total_seen_results) */
1137 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1138 &find_result_set, &mbc);
1139 total_seen_results = mbc.put_cnt;
1140 /* calculate how many results we can send in this message */
1141 message_size_limit = (size - msize) / sizeof (struct GNUNET_HashCode);
1142 mbc.put_cnt = GNUNET_MIN (message_size_limit,
1143 total_seen_results - mbc.skip_cnt);
1144 if (sc->search_request_map_offset < total_seen_results)
1145 GNUNET_assert (mbc.put_cnt > 0);
1147 /* now build message */
1148 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1149 sm->header.size = htons (msize);
1150 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1151 sm->anonymity_level = htonl (sc->anonymity);
1152 memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
1153 sm->query = sc->requests[sc->keyword_offset].uquery;
1154 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1155 &build_result_set, &mbc);
1156 GNUNET_assert (0 == mbc.put_cnt);
1157 GNUNET_assert (total_seen_results >= sc->search_request_map_offset);
1158 if (total_seen_results != sc->search_request_map_offset)
1160 /* more requesting to be done... */
1161 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1162 schedule_transmit_search_request (sc);
1165 sm->options = htonl (options);
1166 sc->keyword_offset++;
1167 sc->search_request_map_offset = 0;
1168 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1170 /* more requesting to be done... */
1171 schedule_transmit_search_request (sc);
1177 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1178 msize = sizeof (struct SearchMessage);
1179 GNUNET_assert (size >= msize);
1180 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1181 sm->anonymity_level = htonl (sc->anonymity);
1182 memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
1183 GNUNET_CRYPTO_ecdsa_public_key_derive (&sc->uri->data.sks.ns,
1184 sc->uri->data.sks.identifier,
1187 GNUNET_CRYPTO_hash (&dpub,
1190 message_size_limit = (size - msize) / sizeof (struct GNUNET_HashCode);
1191 total_seen_results = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1192 mbc.put_cnt = GNUNET_MIN (message_size_limit,
1193 total_seen_results - mbc.skip_cnt);
1194 mbc.keyword_offset = 0;
1195 if (sc->search_request_map_offset < total_seen_results)
1196 GNUNET_assert (mbc.put_cnt > 0);
1197 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1198 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1199 &build_result_set, &mbc);
1200 sm->header.size = htons (msize);
1201 GNUNET_assert (total_seen_results >= sc->search_request_map_offset);
1202 if (total_seen_results != sc->search_request_map_offset)
1204 /* more requesting to be done... */
1205 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1206 schedule_transmit_search_request (sc);
1209 sm->options = htonl (options);
1211 GNUNET_CLIENT_receive (sc->client,
1212 &receive_results, sc,
1213 GNUNET_TIME_UNIT_FOREVER_REL);
1219 * Schedule the transmission of the (next) search request
1222 * @param sc context for the search
1225 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1230 unsigned int request;
1232 size = sizeof (struct SearchMessage);
1234 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1235 sc->search_request_map_offset;
1236 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
1237 request = GNUNET_MIN (fit, left);
1238 size += sizeof (struct GNUNET_HashCode) * request;
1239 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1240 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1242 &transmit_search_request, sc);
1247 * Reconnect to the FS service and transmit
1250 * @param cls our search context
1253 do_reconnect (void *cls)
1255 struct GNUNET_FS_SearchContext *sc = cls;
1256 struct GNUNET_CLIENT_Connection *client;
1259 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1265 sc->client = client;
1266 sc->search_request_map_offset = 0;
1267 sc->keyword_offset = 0;
1268 schedule_transmit_search_request (sc);
1273 * Shutdown any existing connection to the FS
1274 * service and try to establish a fresh one
1275 * (and then re-transmit our search request).
1277 * @param sc the search to reconnec
1280 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1282 if (NULL != sc->client)
1284 GNUNET_CLIENT_disconnect (sc->client);
1287 sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
1289 GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff,
1296 * Start search for content, internal API.
1298 * @param h handle to the file sharing subsystem
1299 * @param uri specifies the search parameters; can be
1300 * a KSK URI or an SKS URI.
1301 * @param anonymity desired level of anonymity
1302 * @param options options for the search
1303 * @param cctx initial value for the client context
1304 * @param psearch parent search result (for namespace update searches)
1305 * @return context that can be used to control the search
1307 static struct GNUNET_FS_SearchContext *
1308 search_start (struct GNUNET_FS_Handle *h,
1309 const struct GNUNET_FS_Uri *uri,
1311 enum GNUNET_FS_SearchOptions options,
1313 struct GNUNET_FS_SearchResult *psearch)
1315 struct GNUNET_FS_SearchContext *sc;
1316 struct GNUNET_FS_ProgressInfo pi;
1318 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
1320 sc->options = options;
1321 sc->uri = GNUNET_FS_uri_dup (uri);
1322 sc->anonymity = anonymity;
1323 sc->start_time = GNUNET_TIME_absolute_get ();
1324 if (NULL != psearch)
1326 sc->psearch_result = psearch;
1327 psearch->update_search = sc;
1329 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1330 sc->client_info = cctx;
1331 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1333 GNUNET_FS_uri_destroy (sc->uri);
1334 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1338 GNUNET_FS_search_sync_ (sc);
1339 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1340 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1346 * Update the 'results' map for the individual keywords with the
1347 * results from the 'global' result set.
1349 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
1350 * @param key current key code
1351 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
1352 * @return #GNUNET_YES (we should continue to iterate)
1355 update_sre_result_maps (void *cls,
1356 const struct GNUNET_HashCode *key,
1359 struct GNUNET_FS_SearchContext *sc = cls;
1360 struct GNUNET_FS_SearchResult *sr = value;
1363 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1364 if (0 != (sr->keyword_bitmap[i / 8] & (1 << (i % 8))))
1365 GNUNET_break (GNUNET_OK ==
1366 GNUNET_CONTAINER_multihashmap_put (sc->requests[i].results,
1369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1376 * Build the request and actually initiate the search using the
1377 * GNUnet FS service.
1379 * @param sc search context
1380 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1383 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1386 const char *keyword;
1387 const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
1388 struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
1389 struct SearchRequestEntry *sre;
1391 GNUNET_assert (NULL == sc->client);
1392 if (GNUNET_FS_uri_test_ksk (sc->uri))
1394 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1395 anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
1396 GNUNET_CRYPTO_ecdsa_key_get_public (anon, &anon_pub);
1398 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1399 sc->uri->data.ksk.keywordCount);
1400 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1402 keyword = &sc->uri->data.ksk.keywords[i][1];
1403 sre = &sc->requests[i];
1404 sre->keyword = GNUNET_strdup (keyword);
1405 GNUNET_CRYPTO_ecdsa_public_key_derive (&anon_pub,
1409 GNUNET_CRYPTO_hash (&sre->dpub,
1410 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
1412 sre->mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1414 sc->mandatory_count++;
1415 sre->results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
1417 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1418 &update_sre_result_maps,
1421 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1422 if (NULL == sc->client)
1423 return GNUNET_SYSERR;
1424 sc->search_request_map_offset = 0;
1425 schedule_transmit_search_request (sc);
1431 * Freeze probes for the given search result.
1433 * @param cls the global FS handle
1434 * @param key the key for the search result (unused)
1435 * @param value the search result to free
1436 * @return #GNUNET_OK
1439 search_result_freeze_probes (void *cls,
1440 const struct GNUNET_HashCode *key,
1443 struct GNUNET_FS_SearchResult *sr = value;
1445 if (NULL != sr->probe_ctx)
1447 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1448 sr->probe_ctx = NULL;
1449 GNUNET_FS_stop_probe_ping_task_ (sr);
1451 if (NULL != sr->probe_cancel_task)
1453 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1454 sr->probe_cancel_task = NULL;
1456 if (NULL != sr->update_search)
1457 GNUNET_FS_search_pause (sr->update_search);
1463 * Resume probes for the given search result.
1465 * @param cls the global FS handle
1466 * @param key the key for the search result (unused)
1467 * @param value the search result to free
1468 * @return #GNUNET_OK
1471 search_result_resume_probes (void *cls,
1472 const struct GNUNET_HashCode * key,
1475 struct GNUNET_FS_SearchResult *sr = value;
1477 GNUNET_FS_search_start_probe_ (sr);
1478 if (NULL != sr->update_search)
1479 GNUNET_FS_search_continue (sr->update_search);
1485 * Signal suspend and free the given search result.
1487 * @param cls the global FS handle
1488 * @param key the key for the search result (unused)
1489 * @param value the search result to free
1490 * @return #GNUNET_OK
1493 search_result_suspend (void *cls,
1494 const struct GNUNET_HashCode *key,
1497 struct GNUNET_FS_SearchContext *sc = cls;
1498 struct GNUNET_FS_SearchResult *sr = value;
1499 struct GNUNET_FS_ProgressInfo pi;
1501 if (NULL != sr->download)
1503 GNUNET_FS_download_signal_suspend_ (sr->download);
1504 sr->download = NULL;
1506 if (NULL != sr->update_search)
1508 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1509 sr->update_search = NULL;
1511 GNUNET_FS_search_stop_probe_ (sr);
1512 if (0 == sr->mandatory_missing)
1514 /* client is aware of search result, notify about suspension event */
1515 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1516 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1517 pi.value.search.specifics.result_suspend.meta = sr->meta;
1518 pi.value.search.specifics.result_suspend.uri = sr->uri;
1519 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
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 GNUNET_free_non_null (sr->keyword_bitmap);
1532 * Create SUSPEND event for the given search operation
1533 * and then clean up our state (without stop signal).
1535 * @param cls the `struct GNUNET_FS_SearchContext` to signal for
1538 GNUNET_FS_search_signal_suspend_ (void *cls)
1540 struct GNUNET_FS_SearchContext *sc = cls;
1541 struct GNUNET_FS_ProgressInfo pi;
1544 GNUNET_FS_end_top (sc->h, sc->top);
1545 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1546 &search_result_suspend, sc);
1547 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1548 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1549 GNUNET_break (NULL == sc->client_info);
1550 if (sc->task != NULL)
1552 GNUNET_SCHEDULER_cancel (sc->task);
1555 if (NULL != sc->client)
1557 GNUNET_CLIENT_disconnect (sc->client);
1560 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1561 if (NULL != sc->requests)
1563 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1564 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1566 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1567 GNUNET_free (sc->requests[i].keyword);
1570 GNUNET_free_non_null (sc->requests);
1571 GNUNET_free_non_null (sc->emsg);
1572 GNUNET_FS_uri_destroy (sc->uri);
1573 GNUNET_free_non_null (sc->serialization);
1579 * Start search for content.
1581 * @param h handle to the file sharing subsystem
1582 * @param uri specifies the search parameters; can be
1583 * a KSK URI or an SKS URI.
1584 * @param anonymity desired level of anonymity
1585 * @param options options for the search
1586 * @param cctx initial value for the client context
1587 * @return context that can be used to control the search
1589 struct GNUNET_FS_SearchContext *
1590 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1591 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1592 enum GNUNET_FS_SearchOptions options, void *cctx)
1594 struct GNUNET_FS_SearchContext *ret;
1596 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1599 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1607 * @param sc context for the search that should be paused
1610 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1612 struct GNUNET_FS_ProgressInfo pi;
1614 if (NULL != sc->task)
1616 GNUNET_SCHEDULER_cancel (sc->task);
1619 if (NULL != sc->client)
1620 GNUNET_CLIENT_disconnect (sc->client);
1622 GNUNET_FS_search_sync_ (sc);
1623 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1624 &search_result_freeze_probes, sc);
1625 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1626 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1631 * Continue paused search.
1633 * @param sc context for the search that should be resumed
1636 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1638 struct GNUNET_FS_ProgressInfo pi;
1640 GNUNET_assert (NULL == sc->client);
1641 GNUNET_assert (NULL == sc->task);
1643 GNUNET_FS_search_sync_ (sc);
1644 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1645 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1646 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1647 &search_result_resume_probes, sc);
1652 * Signal stop for the given search result.
1654 * @param cls the global FS handle
1655 * @param key the key for the search result (unused)
1656 * @param value the search result to free
1657 * @return #GNUNET_OK
1660 search_result_stop (void *cls,
1661 const struct GNUNET_HashCode *key,
1664 struct GNUNET_FS_SearchContext *sc = cls;
1665 struct GNUNET_FS_SearchResult *sr = value;
1666 struct GNUNET_FS_ProgressInfo pi;
1668 GNUNET_FS_search_stop_probe_ (sr);
1669 if (NULL != sr->download)
1671 sr->download->search = NULL;
1673 GNUNET_FS_make_top (sr->download->h,
1674 &GNUNET_FS_download_signal_suspend_,
1676 if (NULL != sr->download->serialization)
1678 GNUNET_FS_remove_sync_file_ (sc->h,
1679 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1680 sr->download->serialization);
1681 GNUNET_free (sr->download->serialization);
1682 sr->download->serialization = NULL;
1684 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1685 GNUNET_FS_download_make_status_ (&pi, sr->download);
1686 GNUNET_FS_download_sync_ (sr->download);
1687 sr->download = NULL;
1689 if (0 != sr->mandatory_missing)
1691 /* client is unaware of search result as
1692 it does not match required keywords */
1693 GNUNET_break (NULL == sr->client_info);
1696 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1697 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1698 pi.value.search.specifics.result_stopped.meta = sr->meta;
1699 pi.value.search.specifics.result_stopped.uri = sr->uri;
1700 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sc);
1706 * Free the given search result.
1708 * @param cls the global FS handle
1709 * @param key the key for the search result (unused)
1710 * @param value the search result to free
1711 * @return #GNUNET_OK
1714 search_result_free (void *cls,
1715 const struct GNUNET_HashCode *key,
1718 struct GNUNET_FS_SearchResult *sr = value;
1720 if (NULL != sr->update_search)
1722 GNUNET_FS_search_stop (sr->update_search);
1723 GNUNET_assert (NULL == sr->update_search);
1725 GNUNET_break (NULL == sr->probe_ctx);
1726 GNUNET_break (NULL == sr->probe_cancel_task);
1727 GNUNET_break (NULL == sr->client_info);
1728 GNUNET_free_non_null (sr->serialization);
1729 GNUNET_FS_uri_destroy (sr->uri);
1730 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1731 GNUNET_free_non_null (sr->keyword_bitmap);
1738 * Stop search for content.
1740 * @param sc context for the search that should be stopped
1743 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1745 struct GNUNET_FS_ProgressInfo pi;
1748 if (NULL != sc->top)
1749 GNUNET_FS_end_top (sc->h, sc->top);
1750 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1751 &search_result_stop, sc);
1752 if (NULL != sc->psearch_result)
1753 sc->psearch_result->update_search = NULL;
1754 if (NULL != sc->serialization)
1756 GNUNET_FS_remove_sync_file_ (sc->h,
1757 (sc->psearch_result !=
1758 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1759 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1761 GNUNET_FS_remove_sync_dir_ (sc->h,
1762 (sc->psearch_result !=
1763 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1764 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1766 GNUNET_free (sc->serialization);
1768 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1769 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1770 GNUNET_break (NULL == sc->client_info);
1771 if (NULL != sc->task)
1772 GNUNET_SCHEDULER_cancel (sc->task);
1773 if (NULL != sc->client)
1774 GNUNET_CLIENT_disconnect (sc->client);
1775 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1776 &search_result_free, sc);
1777 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1778 if (NULL != sc->requests)
1780 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1781 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1782 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1784 GNUNET_free_non_null (sc->requests);
1785 GNUNET_free_non_null (sc->emsg);
1786 GNUNET_FS_uri_destroy (sc->uri);
1790 /* end of fs_search.c */