2 This file is part of GNUnet.
3 (C) 2001-2013 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
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, const struct GNUNET_HashCode * key, void *value)
176 struct GetResultContext *grc = cls;
177 struct GNUNET_FS_SearchResult *sr = value;
179 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
186 * Signal result of last probe to client and then schedule next
189 * @param sr search result to signal for
192 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
194 struct GNUNET_FS_ProgressInfo pi;
196 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
197 pi.value.search.specifics.update.cctx = sr->client_info;
198 pi.value.search.specifics.update.meta = sr->meta;
199 pi.value.search.specifics.update.uri = sr->uri;
200 pi.value.search.specifics.update.availability_rank
201 = 2 * sr->availability_success - sr->availability_trials;
202 pi.value.search.specifics.update.availability_certainty
203 = sr->availability_trials;
204 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
205 pi.value.search.specifics.update.current_probe_time
206 = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
207 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sr->sc);
208 GNUNET_FS_search_start_probe_ (sr);
213 * Handle the case where we have failed to receive a response for our probe.
215 * @param cls our `struct GNUNET_FS_SearchResult *`
216 * @param tc scheduler context
219 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 struct GNUNET_FS_SearchResult *sr = cls;
223 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
224 sr->availability_trials++;
225 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
226 sr->probe_ctx = NULL;
227 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
229 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
230 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
232 GNUNET_FS_search_result_sync_ (sr);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Probe #%u for search result %p failed\n",
235 sr->availability_trials,
237 signal_probe_result (sr);
242 * Handle the case where we have gotten a response for our probe.
244 * @param cls our `struct GNUNET_FS_SearchResult *`
245 * @param tc scheduler context
248 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 struct GNUNET_FS_SearchResult *sr = cls;
252 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
253 sr->availability_trials++;
254 sr->availability_success++;
255 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
256 sr->probe_ctx = NULL;
257 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
259 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
260 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
262 GNUNET_FS_search_result_sync_ (sr);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Probe #%u for search result %p succeeded\n",
265 sr->availability_trials,
267 signal_probe_result (sr);
272 * Notification of FS that a search probe has made progress.
273 * This function is used INSTEAD of the client's event handler
274 * for downloads where the #GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
276 * @param cls closure, always NULL (!), actual closure
277 * is in the client-context of the info struct
278 * @param info details about the event, specifying the event type
279 * and various bits about the event
280 * @return client-context (for the next progress call
281 * for this operation; should be set to NULL for
282 * SUSPEND and STOPPED events). The value returned
283 * will be passed to future callbacks in the respective
284 * field in the `struct GNUNET_FS_ProgressInfo`.
287 GNUNET_FS_search_probe_progress_ (void *cls,
288 const struct GNUNET_FS_ProgressInfo *info)
290 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
291 struct GNUNET_TIME_Relative dur;
293 switch (info->status)
295 case GNUNET_FS_STATUS_DOWNLOAD_START:
298 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
299 /* probes should never be resumed */
302 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
303 /* probes should never be suspended */
306 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
309 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
310 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
312 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
313 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
315 sr->probe_cancel_task =
316 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
317 &probe_failure_handler, sr);
319 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
320 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
322 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
323 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
325 sr->probe_cancel_task =
326 GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
328 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
329 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
331 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
332 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
336 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
337 if (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task)
339 sr->probe_active_time = GNUNET_TIME_absolute_get ();
340 sr->probe_cancel_task =
341 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
342 &probe_failure_handler, sr);
345 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
346 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
348 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
349 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
351 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
352 sr->remaining_probe_time =
353 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
354 if (0 == sr->remaining_probe_time.rel_value_us)
355 sr->probe_cancel_task =
356 GNUNET_SCHEDULER_add_now (&probe_failure_handler, sr);
357 GNUNET_FS_search_result_sync_ (sr);
368 * Task run periodically to remind clients that a probe is active.
370 * @param cls the 'struct GNUNET_FS_SearchResult' that we are probing for
371 * @param tc scheduler context
374 probe_ping_task (void *cls,
375 const struct GNUNET_SCHEDULER_TaskContext *tc)
377 struct GNUNET_FS_SearchResult *sr = cls;
379 signal_probe_result (sr);
381 = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
388 * Start download probes for the given search result.
390 * @param sr the search result
393 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
398 if (NULL != sr->probe_ctx)
400 if (NULL != sr->download)
402 if (0 == (sr->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
404 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
406 if ( (GNUNET_FS_URI_CHK != sr->uri->type) && (GNUNET_FS_URI_LOC != sr->uri->type))
408 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
411 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
413 off = len / DBLOCK_SIZE;
415 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
417 if (len - off < DBLOCK_SIZE)
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Starting probe #%u (at offset %llu) for search result %p\n",
423 sr->availability_trials + 1,
424 (unsigned long long) off,
426 sr->remaining_probe_time =
427 GNUNET_TIME_relative_multiply (sr->h->avg_block_latency,
428 2 * (1 + sr->availability_trials));
430 GNUNET_FS_download_start (sr->h, sr->uri, sr->meta, NULL, NULL, off,
432 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
433 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
435 = GNUNET_SCHEDULER_add_now (&probe_ping_task,
441 * Start download probes for the given search result.
443 * @param h file-sharing handle to use for the operation
444 * @param uri URI to probe
445 * @param meta meta data associated with the URI
446 * @param client_info client info pointer to use for associated events
447 * @param anonymity anonymity level to use for the probes
448 * @return the search result handle to access the probe activity
450 struct GNUNET_FS_SearchResult *
451 GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
452 const struct GNUNET_FS_Uri *uri,
453 const struct GNUNET_CONTAINER_MetaData *meta,
457 struct GNUNET_FS_SearchResult *sr;
459 GNUNET_assert (NULL != h);
460 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
462 sr->uri = GNUNET_FS_uri_dup (uri);
463 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
464 sr->client_info = client_info;
465 sr->anonymity = anonymity;
466 GNUNET_FS_search_start_probe_ (sr);
472 * Stop probing activity associated with a search result.
474 * @param sr search result
477 GNUNET_FS_search_stop_probe_ (struct GNUNET_FS_SearchResult *sr)
479 if (NULL != sr->probe_ctx)
481 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
482 sr->probe_ctx = NULL;
484 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
486 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
487 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
489 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
491 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
492 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
498 * Stop probe activity. Must ONLY be used on values
499 * returned from #GNUNET_FS_probe.
501 * @param sr search result to stop probing for (freed)
502 * @return the value of the 'client_info' pointer
505 GNUNET_FS_probe_stop (struct GNUNET_FS_SearchResult *sr)
509 GNUNET_assert (NULL == sr->sc);
510 GNUNET_FS_search_stop_probe_ (sr);
511 GNUNET_FS_uri_destroy (sr->uri);
512 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
513 client_info = sr->client_info;
520 * We have received a KSK result. Check how it fits in with the
521 * overall query and notify the client accordingly.
523 * @param sc context for the overall query
524 * @param ent entry for the specific keyword
525 * @param uri the URI that was found
526 * @param meta metadata associated with the URI
527 * under the @a ent keyword
530 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
531 struct SearchRequestEntry *ent,
532 const struct GNUNET_FS_Uri *uri,
533 const struct GNUNET_CONTAINER_MetaData *meta)
535 struct GNUNET_HashCode key;
536 struct GNUNET_FS_SearchResult *sr;
537 struct GetResultContext grc;
542 GNUNET_assert (NULL != sc);
543 GNUNET_FS_uri_to_key (uri, &key);
545 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
547 &test_result_present,
549 return; /* duplicate result */
550 /* try to find search result in master map */
553 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
555 &get_result_present, &grc);
557 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
560 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
563 sr->anonymity = sc->anonymity;
564 sr->uri = GNUNET_FS_uri_dup (uri);
565 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
566 sr->mandatory_missing = sc->mandatory_count;
568 sr->keyword_bitmap = GNUNET_malloc ((sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
569 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
570 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
574 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
576 GNUNET_break (GNUNET_OK ==
577 GNUNET_CONTAINER_multihashmap_put (ent->results,
580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
582 koff = ent - sc->requests;
583 GNUNET_assert ( (ent >= sc->requests) &&
584 (koff < sc->uri->data.ksk.keywordCount));
585 sr->keyword_bitmap[koff / 8] |= (1 << (koff % 8));
586 /* check if mandatory satisfied */
587 if (1 == GNUNET_CONTAINER_multihashmap_size (ent->results))
591 GNUNET_break (sr->mandatory_missing > 0);
592 sr->mandatory_missing--;
596 sr->optional_support++;
599 if (0 != sr->mandatory_missing)
601 GNUNET_break (NULL == sr->client_info);
605 notify_client_chk_result (sc, sr);
607 notify_client_chk_update (sc, sr);
608 GNUNET_FS_search_result_sync_ (sr);
609 GNUNET_FS_search_start_probe_ (sr);
614 * Start search for content, internal API.
616 * @param h handle to the file sharing subsystem
617 * @param uri specifies the search parameters; can be
618 * a KSK URI or an SKS URI.
619 * @param anonymity desired level of anonymity
620 * @param options options for the search
621 * @param cctx client context
622 * @param psearch parent search result (for namespace update searches)
623 * @return context that can be used to control the search
625 static struct GNUNET_FS_SearchContext *
626 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
627 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
628 void *cctx, struct GNUNET_FS_SearchResult *psearch);
632 * We have received an SKS result. Start searching for updates and
633 * notify the client if it is a new result.
635 * @param sc context for the overall query
636 * @param id_update identifier for updates, NULL for none
637 * @param uri the URI that was found
638 * @param meta metadata associated with the URI
641 process_sks_result (struct GNUNET_FS_SearchContext *sc,
642 const char *id_update,
643 const struct GNUNET_FS_Uri *uri,
644 const struct GNUNET_CONTAINER_MetaData *meta)
646 struct GNUNET_FS_Uri uu;
647 struct GNUNET_HashCode key;
648 struct GNUNET_FS_SearchResult *sr;
651 GNUNET_assert (NULL != sc);
652 GNUNET_FS_uri_to_key (uri, &key);
653 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
656 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
657 &test_result_present,
659 return; /* duplicate result */
660 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
663 sr->anonymity = sc->anonymity;
664 sr->uri = GNUNET_FS_uri_dup (uri);
665 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
667 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
669 GNUNET_FS_search_result_sync_ (sr);
670 GNUNET_FS_search_start_probe_ (sr);
672 if (0 == sr->mandatory_missing)
673 notify_client_chk_result (sc, sr);
675 GNUNET_break (NULL == sr->client_info);
676 /* search for updates */
677 if (0 == strlen (id_update))
678 return; /* no updates */
679 uu.type = GNUNET_FS_URI_SKS;
680 uu.data.sks.ns = sc->uri->data.sks.ns;
681 uu.data.sks.identifier = GNUNET_strdup (id_update);
682 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
683 GNUNET_free (uu.data.sks.identifier);
688 * Decrypt a ublock using a 'keyword' as the passphrase. Given the
689 * KSK public key derived from the keyword, this function looks up
690 * the original keyword in the search context and decrypts the
691 * given ciphertext block.
693 * @param sc search context with the keywords
694 * @param dpub derived public key used for the search
695 * @param edata encrypted data
696 * @param edata_size number of bytes in @a edata (and @a data)
697 * @param data where to store the plaintext
698 * @return keyword index on success, #GNUNET_SYSERR on error (no such
699 * keyword, internal error)
702 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
703 const struct GNUNET_CRYPTO_EcdsaPublicKey *dpub,
708 const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
709 struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
713 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
714 if (0 == memcmp (dpub,
715 &sc->requests[i].dpub,
716 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
718 if (i == sc->uri->data.ksk.keywordCount)
720 /* oops, does not match any of our keywords!? */
722 return GNUNET_SYSERR;
725 anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
726 GNUNET_CRYPTO_ecdsa_key_get_public (anon, &anon_pub);
727 GNUNET_FS_ublock_decrypt_ (edata, edata_size,
729 sc->requests[i].keyword,
736 * Process a keyword search result. The actual type of block is
737 * a UBlock; we know it is a keyword search result because that's
738 * what we were searching for.
740 * @param sc our search context
741 * @param ub the ublock with the keyword search result
742 * @param size size of @a ub
745 process_kblock (struct GNUNET_FS_SearchContext *sc,
746 const struct UBlock *ub,
750 char pt[size - sizeof (struct UBlock)];
752 struct GNUNET_CONTAINER_MetaData *meta;
753 struct GNUNET_FS_Uri *uri;
757 if (-1 == (i = decrypt_block_with_keyword (sc,
758 &ub->verification_key,
760 size - sizeof (struct UBlock),
763 /* parse; pt[0] is just '\0', so we skip over that */
764 eos = memchr (&pt[1], '\0', sizeof (pt) - 1);
770 if (NULL == (uri = GNUNET_FS_uri_parse (&pt[1], &emsg)))
772 if (GNUNET_FS_VERSION > 0x00090400)
774 /* we broke this in 0x00090300, so don't bitch
775 too loudly just one version up... */
776 GNUNET_break_op (0); /* ublock malformed */
777 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
778 _("Failed to parse URI `%s': %s\n"),
782 GNUNET_free_non_null (emsg);
786 if (sizeof (pt) == j)
787 meta = GNUNET_CONTAINER_meta_data_create ();
789 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
792 GNUNET_break_op (0); /* ublock malformed */
793 GNUNET_FS_uri_destroy (uri);
796 process_ksk_result (sc,
802 GNUNET_CONTAINER_meta_data_destroy (meta);
803 GNUNET_FS_uri_destroy (uri);
808 * Process a namespace-search result. The actual type of block is
809 * a UBlock; we know it is a namespace search result because that's
810 * what we were searching for.
812 * @param sc our search context
813 * @param ub the ublock with a namespace result
814 * @param size size of @a ub
817 process_sblock (struct GNUNET_FS_SearchContext *sc,
818 const struct UBlock *ub,
821 size_t len = size - sizeof (struct UBlock);
823 struct GNUNET_FS_Uri *uri;
824 struct GNUNET_CONTAINER_MetaData *meta;
830 GNUNET_FS_ublock_decrypt_ (&ub[1], len,
831 &sc->uri->data.sks.ns,
832 sc->uri->data.sks.identifier,
835 if (0 == (off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris)))
837 GNUNET_break_op (0); /* ublock malformed */
840 if (NULL == (meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off)))
842 GNUNET_break_op (0); /* ublock malformed */
845 if (NULL == (uri = GNUNET_FS_uri_parse (uris, &emsg)))
847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
848 _("Failed to parse URI `%s': %s\n"),
850 GNUNET_break_op (0); /* ublock malformed */
851 GNUNET_free_non_null (emsg);
852 GNUNET_CONTAINER_meta_data_destroy (meta);
856 process_sks_result (sc, id, uri, meta);
858 GNUNET_FS_uri_destroy (uri);
859 GNUNET_CONTAINER_meta_data_destroy (meta);
864 * Process a search result.
866 * @param sc our search context
867 * @param type type of the result
868 * @param expiration when it will expire
869 * @param data the (encrypted) response
870 * @param size size of @a data
873 process_result (struct GNUNET_FS_SearchContext *sc,
874 enum GNUNET_BLOCK_Type type,
875 struct GNUNET_TIME_Absolute expiration,
879 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value_us > 0)
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Result received has already expired.\n");
883 return; /* result expired */
887 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
888 if (GNUNET_FS_URI_SKS == sc->uri->type)
889 process_sblock (sc, data, size);
891 process_kblock (sc, data, size);
893 case GNUNET_BLOCK_TYPE_ANY:
896 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
899 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
902 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
906 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
907 _("Got result with unknown block type `%d', ignoring"), type);
914 * Shutdown any existing connection to the FS
915 * service and try to establish a fresh one
916 * (and then re-transmit our search request).
918 * @param sc the search to reconnec
921 try_reconnect (struct GNUNET_FS_SearchContext *sc);
925 * Type of a function to call when we receive a message
929 * @param msg message received, NULL on timeout or fatal error
932 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
934 struct GNUNET_FS_SearchContext *sc = cls;
935 const struct ClientPutMessage *cm;
938 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
939 (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
944 msize = ntohs (msg->size);
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Receiving %u bytes of result from fs service\n", msize);
947 cm = (const struct ClientPutMessage *) msg;
948 process_result (sc, ntohl (cm->type),
949 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
950 msize - sizeof (struct ClientPutMessage));
951 /* continue receiving */
952 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
953 GNUNET_TIME_UNIT_FOREVER_REL);
958 * Schedule the transmission of the (next) search request
961 * @param sc context for the search
964 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
968 * Closure for 'build_result_set'.
970 struct MessageBuilderContext
973 * How many entries can we store to xoff.
975 unsigned int put_cnt;
978 * How many entries should we skip.
980 unsigned int skip_cnt;
983 * Where to store the keys.
985 struct GNUNET_HashCode *xoff;
988 * Search context we are iterating for.
990 struct GNUNET_FS_SearchContext *sc;
993 * Keyword offset the search result must match (0 for SKS)
995 unsigned int keyword_offset;
1000 * Iterating over the known results, pick those matching the given
1001 * result range and store their keys at 'xoff'.
1003 * @param cls the `struct MessageBuilderContext`
1004 * @param key key for a result
1005 * @param value the search result
1006 * @return #GNUNET_OK to continue iterating
1009 build_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
1011 struct MessageBuilderContext *mbc = cls;
1012 struct GNUNET_FS_SearchResult *sr = value;
1014 if ( (NULL != sr->keyword_bitmap) &&
1015 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1016 return GNUNET_OK; /* have no match for this keyword yet */
1017 if (mbc->skip_cnt > 0)
1022 if (0 == mbc->put_cnt)
1023 return GNUNET_SYSERR;
1024 mbc->sc->search_request_map_offset++;
1025 mbc->xoff[--mbc->put_cnt] = *key;
1032 * Iterating over the known results, count those
1033 * matching the given result range and increment
1034 * put count for each.
1036 * @param cls the `struct MessageBuilderContext`
1037 * @param key key for a result
1038 * @param value the search result
1039 * @return #GNUNET_OK to continue iterating
1042 find_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
1044 struct MessageBuilderContext *mbc = cls;
1045 struct GNUNET_FS_SearchResult *sr = value;
1047 if ( (NULL != sr->keyword_bitmap) &&
1048 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1049 return GNUNET_OK; /* have no match for this keyword yet */
1056 * We're ready to transmit the search request to the
1057 * file-sharing service. Do it.
1059 * @param cls closure
1060 * @param size number of bytes available in @a buf
1061 * @param buf where the callee should write the message
1062 * @return number of bytes written to @a buf
1065 transmit_search_request (void *cls, size_t size, void *buf)
1067 struct GNUNET_FS_SearchContext *sc = cls;
1068 struct MessageBuilderContext mbc;
1070 struct SearchMessage *sm;
1071 struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
1081 mbc.skip_cnt = sc->search_request_map_offset;
1083 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1084 mbc.xoff = (struct GNUNET_HashCode *) & sm[1];
1085 options = SEARCH_MESSAGE_OPTION_NONE;
1086 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1087 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1088 if (GNUNET_FS_uri_test_ksk (sc->uri))
1090 msize = sizeof (struct SearchMessage);
1091 GNUNET_assert (size >= msize);
1092 mbc.keyword_offset = sc->keyword_offset;
1093 /* calculate total number of known results (in put_cnt => sqms) */
1095 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1096 &find_result_set, &mbc);
1098 /* calculate how many results we can send in this message */
1099 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1100 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1101 if (sc->search_request_map_offset < sqms)
1102 GNUNET_assert (mbc.put_cnt > 0);
1104 /* now build message */
1105 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1106 sm->header.size = htons (msize);
1107 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1108 sm->anonymity_level = htonl (sc->anonymity);
1109 memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
1110 sm->query = sc->requests[sc->keyword_offset].uquery;
1111 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1112 &build_result_set, &mbc);
1113 GNUNET_assert (sqms >= sc->search_request_map_offset);
1114 if (sqms != sc->search_request_map_offset)
1116 /* more requesting to be done... */
1117 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1118 schedule_transmit_search_request (sc);
1121 sm->options = htonl (options);
1122 sc->keyword_offset++;
1123 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1125 /* more requesting to be done... */
1126 schedule_transmit_search_request (sc);
1132 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1133 msize = sizeof (struct SearchMessage);
1134 GNUNET_assert (size >= msize);
1135 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1136 sm->anonymity_level = htonl (sc->anonymity);
1137 memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
1138 GNUNET_CRYPTO_ecdsa_public_key_derive (&sc->uri->data.sks.ns,
1139 sc->uri->data.sks.identifier,
1142 GNUNET_CRYPTO_hash (&dpub,
1145 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1146 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1147 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1148 mbc.keyword_offset = 0;
1149 if (sc->search_request_map_offset < sqms)
1150 GNUNET_assert (mbc.put_cnt > 0);
1151 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1152 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1153 &build_result_set, &mbc);
1154 sm->header.size = htons (msize);
1155 GNUNET_assert (sqms >= sc->search_request_map_offset);
1156 if (sqms != sc->search_request_map_offset)
1158 /* more requesting to be done... */
1159 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1160 schedule_transmit_search_request (sc);
1163 sm->options = htonl (options);
1165 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1166 GNUNET_TIME_UNIT_FOREVER_REL);
1172 * Schedule the transmission of the (next) search request
1175 * @param sc context for the search
1178 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1184 size = sizeof (struct SearchMessage);
1186 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1187 sc->search_request_map_offset;
1188 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
1189 fit = GNUNET_MIN (fit, sqms);
1190 size += sizeof (struct GNUNET_HashCode) * fit;
1191 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1192 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1193 GNUNET_NO, &transmit_search_request, sc);
1199 * Reconnect to the FS service and transmit
1202 * @param cls our search context
1206 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1208 struct GNUNET_FS_SearchContext *sc = cls;
1209 struct GNUNET_CLIENT_Connection *client;
1211 sc->task = GNUNET_SCHEDULER_NO_TASK;
1212 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1218 sc->client = client;
1219 sc->search_request_map_offset = 0;
1220 sc->keyword_offset = 0;
1221 schedule_transmit_search_request (sc);
1226 * Shutdown any existing connection to the FS
1227 * service and try to establish a fresh one
1228 * (and then re-transmit our search request).
1230 * @param sc the search to reconnec
1233 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1235 if (NULL != sc->client)
1237 GNUNET_CLIENT_disconnect (sc->client);
1240 sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
1242 GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff,
1249 * Start search for content, internal API.
1251 * @param h handle to the file sharing subsystem
1252 * @param uri specifies the search parameters; can be
1253 * a KSK URI or an SKS URI.
1254 * @param anonymity desired level of anonymity
1255 * @param options options for the search
1256 * @param cctx initial value for the client context
1257 * @param psearch parent search result (for namespace update searches)
1258 * @return context that can be used to control the search
1260 static struct GNUNET_FS_SearchContext *
1261 search_start (struct GNUNET_FS_Handle *h,
1262 const struct GNUNET_FS_Uri *uri,
1264 enum GNUNET_FS_SearchOptions options,
1266 struct GNUNET_FS_SearchResult *psearch)
1268 struct GNUNET_FS_SearchContext *sc;
1269 struct GNUNET_FS_ProgressInfo pi;
1271 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
1273 sc->options = options;
1274 sc->uri = GNUNET_FS_uri_dup (uri);
1275 sc->anonymity = anonymity;
1276 sc->start_time = GNUNET_TIME_absolute_get ();
1277 if (NULL != psearch)
1279 sc->psearch_result = psearch;
1280 psearch->update_search = sc;
1282 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1283 sc->client_info = cctx;
1284 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1286 GNUNET_FS_uri_destroy (sc->uri);
1287 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1291 GNUNET_FS_search_sync_ (sc);
1292 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1293 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1299 * Update the 'results' map for the individual keywords with the
1300 * results from the 'global' result set.
1302 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
1303 * @param key current key code
1304 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
1305 * @return #GNUNET_YES (we should continue to iterate)
1308 update_sre_result_maps (void *cls,
1309 const struct GNUNET_HashCode *key,
1312 struct GNUNET_FS_SearchContext *sc = cls;
1313 struct GNUNET_FS_SearchResult *sr = value;
1316 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1317 if (0 != (sr->keyword_bitmap[i / 8] & (1 << (i % 8))))
1318 GNUNET_break (GNUNET_OK ==
1319 GNUNET_CONTAINER_multihashmap_put (sc->requests[i].results,
1322 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1329 * Build the request and actually initiate the search using the
1330 * GNUnet FS service.
1332 * @param sc search context
1333 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1336 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1339 const char *keyword;
1340 const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
1341 struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
1342 struct SearchRequestEntry *sre;
1344 GNUNET_assert (NULL == sc->client);
1345 if (GNUNET_FS_uri_test_ksk (sc->uri))
1347 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1348 anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
1349 GNUNET_CRYPTO_ecdsa_key_get_public (anon, &anon_pub);
1351 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1352 sc->uri->data.ksk.keywordCount);
1353 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1355 keyword = &sc->uri->data.ksk.keywords[i][1];
1356 sre = &sc->requests[i];
1357 sre->keyword = GNUNET_strdup (keyword);
1358 GNUNET_CRYPTO_ecdsa_public_key_derive (&anon_pub,
1362 GNUNET_CRYPTO_hash (&sre->dpub,
1363 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
1365 sre->mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1367 sc->mandatory_count++;
1368 sre->results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
1370 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1371 &update_sre_result_maps,
1374 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1375 if (NULL == sc->client)
1376 return GNUNET_SYSERR;
1377 schedule_transmit_search_request (sc);
1383 * Freeze probes for the given search result.
1385 * @param cls the global FS handle
1386 * @param key the key for the search result (unused)
1387 * @param value the search result to free
1388 * @return #GNUNET_OK
1391 search_result_freeze_probes (void *cls,
1392 const struct GNUNET_HashCode *key,
1395 struct GNUNET_FS_SearchResult *sr = value;
1397 if (NULL != sr->probe_ctx)
1399 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1400 sr->probe_ctx = NULL;
1402 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
1404 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
1405 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
1407 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1409 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1410 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1412 if (NULL != sr->update_search)
1413 GNUNET_FS_search_pause (sr->update_search);
1419 * Resume probes for the given search result.
1421 * @param cls the global FS handle
1422 * @param key the key for the search result (unused)
1423 * @param value the search result to free
1424 * @return #GNUNET_OK
1427 search_result_resume_probes (void *cls,
1428 const struct GNUNET_HashCode * key,
1431 struct GNUNET_FS_SearchResult *sr = value;
1433 GNUNET_FS_search_start_probe_ (sr);
1434 if (NULL != sr->update_search)
1435 GNUNET_FS_search_continue (sr->update_search);
1441 * Signal suspend and free the given search result.
1443 * @param cls the global FS handle
1444 * @param key the key for the search result (unused)
1445 * @param value the search result to free
1446 * @return #GNUNET_OK
1449 search_result_suspend (void *cls,
1450 const struct GNUNET_HashCode *key,
1453 struct GNUNET_FS_SearchContext *sc = cls;
1454 struct GNUNET_FS_SearchResult *sr = value;
1455 struct GNUNET_FS_ProgressInfo pi;
1457 if (NULL != sr->download)
1459 GNUNET_FS_download_signal_suspend_ (sr->download);
1460 sr->download = NULL;
1462 if (NULL != sr->update_search)
1464 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1465 sr->update_search = NULL;
1467 GNUNET_FS_search_stop_probe_ (sr);
1468 if (0 == sr->mandatory_missing)
1470 /* client is aware of search result, notify about suspension event */
1471 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1472 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1473 pi.value.search.specifics.result_suspend.meta = sr->meta;
1474 pi.value.search.specifics.result_suspend.uri = sr->uri;
1475 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1477 GNUNET_break (NULL == sr->client_info);
1478 GNUNET_free_non_null (sr->serialization);
1479 GNUNET_FS_uri_destroy (sr->uri);
1480 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1481 GNUNET_free_non_null (sr->keyword_bitmap);
1488 * Create SUSPEND event for the given search operation
1489 * and then clean up our state (without stop signal).
1491 * @param cls the `struct GNUNET_FS_SearchContext` to signal for
1494 GNUNET_FS_search_signal_suspend_ (void *cls)
1496 struct GNUNET_FS_SearchContext *sc = cls;
1497 struct GNUNET_FS_ProgressInfo pi;
1500 GNUNET_FS_end_top (sc->h, sc->top);
1501 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1502 &search_result_suspend, sc);
1503 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1504 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1505 GNUNET_break (NULL == sc->client_info);
1506 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1508 GNUNET_SCHEDULER_cancel (sc->task);
1509 sc->task = GNUNET_SCHEDULER_NO_TASK;
1511 if (NULL != sc->client)
1513 GNUNET_CLIENT_disconnect (sc->client);
1516 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1517 if (NULL != sc->requests)
1519 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1520 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1522 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1523 GNUNET_free (sc->requests[i].keyword);
1526 GNUNET_free_non_null (sc->requests);
1527 GNUNET_free_non_null (sc->emsg);
1528 GNUNET_FS_uri_destroy (sc->uri);
1529 GNUNET_free_non_null (sc->serialization);
1535 * Start search for content.
1537 * @param h handle to the file sharing subsystem
1538 * @param uri specifies the search parameters; can be
1539 * a KSK URI or an SKS URI.
1540 * @param anonymity desired level of anonymity
1541 * @param options options for the search
1542 * @param cctx initial value for the client context
1543 * @return context that can be used to control the search
1545 struct GNUNET_FS_SearchContext *
1546 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1547 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1548 enum GNUNET_FS_SearchOptions options, void *cctx)
1550 struct GNUNET_FS_SearchContext *ret;
1552 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1555 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1563 * @param sc context for the search that should be paused
1566 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1568 struct GNUNET_FS_ProgressInfo pi;
1570 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1572 GNUNET_SCHEDULER_cancel (sc->task);
1573 sc->task = GNUNET_SCHEDULER_NO_TASK;
1575 if (NULL != sc->client)
1576 GNUNET_CLIENT_disconnect (sc->client);
1578 GNUNET_FS_search_sync_ (sc);
1579 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1580 &search_result_freeze_probes, sc);
1581 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1582 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1587 * Continue paused search.
1589 * @param sc context for the search that should be resumed
1592 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1594 struct GNUNET_FS_ProgressInfo pi;
1596 GNUNET_assert (NULL == sc->client);
1597 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sc->task);
1598 do_reconnect (sc, NULL);
1599 GNUNET_FS_search_sync_ (sc);
1600 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1601 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1602 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1603 &search_result_resume_probes, sc);
1608 * Signal stop for the given search result.
1610 * @param cls the global FS handle
1611 * @param key the key for the search result (unused)
1612 * @param value the search result to free
1613 * @return #GNUNET_OK
1616 search_result_stop (void *cls,
1617 const struct GNUNET_HashCode *key,
1620 struct GNUNET_FS_SearchContext *sc = cls;
1621 struct GNUNET_FS_SearchResult *sr = value;
1622 struct GNUNET_FS_ProgressInfo pi;
1624 GNUNET_FS_search_stop_probe_ (sr);
1625 if (NULL != sr->download)
1627 sr->download->search = NULL;
1629 GNUNET_FS_make_top (sr->download->h,
1630 &GNUNET_FS_download_signal_suspend_,
1632 if (NULL != sr->download->serialization)
1634 GNUNET_FS_remove_sync_file_ (sc->h,
1635 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1636 sr->download->serialization);
1637 GNUNET_free (sr->download->serialization);
1638 sr->download->serialization = NULL;
1640 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1641 GNUNET_FS_download_make_status_ (&pi, sr->download);
1642 GNUNET_FS_download_sync_ (sr->download);
1643 sr->download = NULL;
1645 if (0 != sr->mandatory_missing)
1647 /* client is unaware of search result as
1648 it does not match required keywords */
1649 GNUNET_break (NULL == sr->client_info);
1652 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1653 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1654 pi.value.search.specifics.result_stopped.meta = sr->meta;
1655 pi.value.search.specifics.result_stopped.uri = sr->uri;
1656 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sc);
1662 * Free the given search result.
1664 * @param cls the global FS handle
1665 * @param key the key for the search result (unused)
1666 * @param value the search result to free
1667 * @return #GNUNET_OK
1670 search_result_free (void *cls,
1671 const struct GNUNET_HashCode *key,
1674 struct GNUNET_FS_SearchResult *sr = value;
1676 if (NULL != sr->update_search)
1678 GNUNET_FS_search_stop (sr->update_search);
1679 GNUNET_assert (NULL == sr->update_search);
1681 GNUNET_break (NULL == sr->probe_ctx);
1682 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task);
1683 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_ping_task);
1684 GNUNET_break (NULL == sr->client_info);
1685 GNUNET_free_non_null (sr->serialization);
1686 GNUNET_FS_uri_destroy (sr->uri);
1687 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1688 GNUNET_free_non_null (sr->keyword_bitmap);
1695 * Stop search for content.
1697 * @param sc context for the search that should be stopped
1700 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1702 struct GNUNET_FS_ProgressInfo pi;
1705 if (NULL != sc->top)
1706 GNUNET_FS_end_top (sc->h, sc->top);
1707 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1708 &search_result_stop, sc);
1709 if (NULL != sc->psearch_result)
1710 sc->psearch_result->update_search = NULL;
1711 if (NULL != sc->serialization)
1713 GNUNET_FS_remove_sync_file_ (sc->h,
1714 (sc->psearch_result !=
1715 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1716 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1718 GNUNET_FS_remove_sync_dir_ (sc->h,
1719 (sc->psearch_result !=
1720 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1721 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1723 GNUNET_free (sc->serialization);
1725 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1726 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1727 GNUNET_break (NULL == sc->client_info);
1728 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1729 GNUNET_SCHEDULER_cancel (sc->task);
1730 if (NULL != sc->client)
1731 GNUNET_CLIENT_disconnect (sc->client);
1732 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1733 &search_result_free, sc);
1734 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1735 if (NULL != sc->requests)
1737 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1738 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1739 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1741 GNUNET_free_non_null (sc->requests);
1742 GNUNET_free_non_null (sc->emsg);
1743 GNUNET_FS_uri_destroy (sc->uri);
1747 /* end of fs_search.c */