2 This file is part of GNUnet.
3 (C) 2001-2014 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,
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 *`
218 * @param tc scheduler context
221 probe_failure_handler (void *cls,
222 const struct GNUNET_SCHEDULER_TaskContext *tc)
224 struct GNUNET_FS_SearchResult *sr = cls;
226 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
227 sr->availability_trials++;
228 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
229 sr->probe_ctx = NULL;
230 GNUNET_FS_stop_probe_ping_task_ (sr);
231 GNUNET_FS_search_result_sync_ (sr);
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Probe #%u for search result %p failed\n",
234 sr->availability_trials,
236 signal_probe_result (sr);
241 * Handle the case where we have gotten a response for our probe.
243 * @param cls our `struct GNUNET_FS_SearchResult *`
244 * @param tc scheduler context
247 probe_success_handler (void *cls,
248 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 GNUNET_FS_stop_probe_ping_task_ (sr);
258 GNUNET_FS_search_result_sync_ (sr);
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Probe #%u for search result %p succeeded\n",
261 sr->availability_trials,
263 signal_probe_result (sr);
268 * Notification of FS that a search probe has made progress.
269 * This function is used INSTEAD of the client's event handler
270 * for downloads where the #GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
272 * @param cls closure, always NULL (!), actual closure
273 * is in the client-context of the info struct
274 * @param info details about the event, specifying the event type
275 * and various bits about the event
276 * @return client-context (for the next progress call
277 * for this operation; should be set to NULL for
278 * SUSPEND and STOPPED events). The value returned
279 * will be passed to future callbacks in the respective
280 * field in the `struct GNUNET_FS_ProgressInfo`.
283 GNUNET_FS_search_probe_progress_ (void *cls,
284 const struct GNUNET_FS_ProgressInfo *info)
286 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
287 struct GNUNET_TIME_Relative dur;
289 switch (info->status)
291 case GNUNET_FS_STATUS_DOWNLOAD_START:
294 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
295 /* probes should never be resumed */
298 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
299 /* probes should never be suspended */
302 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
305 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
306 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
308 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
309 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
311 sr->probe_cancel_task =
312 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
313 &probe_failure_handler, sr);
315 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
316 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
318 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
319 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
321 sr->probe_cancel_task =
322 GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
324 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
325 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
327 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
328 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
332 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
333 if (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task)
335 sr->probe_active_time = GNUNET_TIME_absolute_get ();
336 sr->probe_cancel_task =
337 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
338 &probe_failure_handler, sr);
341 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
342 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
344 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
345 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
347 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
348 sr->remaining_probe_time =
349 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
350 if (0 == sr->remaining_probe_time.rel_value_us)
351 sr->probe_cancel_task =
352 GNUNET_SCHEDULER_add_now (&probe_failure_handler, sr);
353 GNUNET_FS_search_result_sync_ (sr);
364 * Task run periodically to remind clients that a probe is active.
366 * @param cls the `struct GNUNET_FS_SearchResult` that we are probing for
367 * @param tc scheduler context
370 probe_ping_task_cb (void *cls,
371 const struct GNUNET_SCHEDULER_TaskContext *tc)
373 struct GNUNET_FS_Handle *h = cls;
374 struct GNUNET_FS_SearchResult *sr;
376 for (sr = h->probes_head; NULL != sr; sr = sr->next)
377 if (NULL != sr->probe_ctx->client)
378 signal_probe_result (sr);
380 = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
387 * Start the ping task for this search result.
389 * @param sr result to start pinging for.
392 start_probe_ping_task (struct GNUNET_FS_SearchResult *sr)
394 struct GNUNET_FS_Handle *h = sr->h;
396 GNUNET_CONTAINER_DLL_insert (h->probes_head,
399 if (GNUNET_SCHEDULER_NO_TASK == h->probe_ping_task)
401 = GNUNET_SCHEDULER_add_now (&probe_ping_task_cb,
407 * Stop the ping task for this search result.
409 * @param sr result to start pinging for.
412 GNUNET_FS_stop_probe_ping_task_ (struct GNUNET_FS_SearchResult *sr)
414 struct GNUNET_FS_Handle *h = sr->h;
416 GNUNET_CONTAINER_DLL_remove (h->probes_head,
419 if (NULL == h->probes_head)
421 GNUNET_SCHEDULER_cancel (h->probe_ping_task);
422 h->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
428 * Start download probes for the given search result.
430 * @param sr the search result
433 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
438 if (NULL != sr->probe_ctx)
440 if (NULL != sr->download)
442 if (0 == (sr->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
444 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
446 if ( (GNUNET_FS_URI_CHK != sr->uri->type) && (GNUNET_FS_URI_LOC != sr->uri->type))
448 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
451 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
453 off = len / DBLOCK_SIZE;
455 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
457 if (len - off < DBLOCK_SIZE)
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Starting probe #%u (at offset %llu) for search result %p\n",
463 sr->availability_trials + 1,
464 (unsigned long long) off,
466 sr->remaining_probe_time =
467 GNUNET_TIME_relative_multiply (sr->h->avg_block_latency,
468 2 * (1 + sr->availability_trials));
470 GNUNET_FS_download_start (sr->h, sr->uri, sr->meta, NULL, NULL, off,
472 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
473 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
474 start_probe_ping_task (sr);
479 * Start download probes for the given search result.
481 * @param h file-sharing handle to use for the operation
482 * @param uri URI to probe
483 * @param meta meta data associated with the URI
484 * @param client_info client info pointer to use for associated events
485 * @param anonymity anonymity level to use for the probes
486 * @return the search result handle to access the probe activity
488 struct GNUNET_FS_SearchResult *
489 GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
490 const struct GNUNET_FS_Uri *uri,
491 const struct GNUNET_CONTAINER_MetaData *meta,
495 struct GNUNET_FS_SearchResult *sr;
497 GNUNET_assert (NULL != h);
498 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
500 sr->uri = GNUNET_FS_uri_dup (uri);
501 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
502 sr->client_info = client_info;
503 sr->anonymity = anonymity;
504 GNUNET_FS_search_start_probe_ (sr);
510 * Stop probing activity associated with a search result.
512 * @param sr search result
515 GNUNET_FS_search_stop_probe_ (struct GNUNET_FS_SearchResult *sr)
517 if (NULL != sr->probe_ctx)
519 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
520 sr->probe_ctx = NULL;
521 GNUNET_FS_stop_probe_ping_task_ (sr);
523 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
525 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
526 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
532 * Stop probe activity. Must ONLY be used on values
533 * returned from #GNUNET_FS_probe.
535 * @param sr search result to stop probing for (freed)
536 * @return the value of the 'client_info' pointer
539 GNUNET_FS_probe_stop (struct GNUNET_FS_SearchResult *sr)
543 GNUNET_assert (NULL == sr->sc);
544 GNUNET_FS_search_stop_probe_ (sr);
545 GNUNET_FS_uri_destroy (sr->uri);
546 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
547 client_info = sr->client_info;
554 * We have received a KSK result. Check how it fits in with the
555 * overall query and notify the client accordingly.
557 * @param sc context for the overall query
558 * @param ent entry for the specific keyword
559 * @param uri the URI that was found
560 * @param meta metadata associated with the URI
561 * under the @a ent keyword
564 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
565 struct SearchRequestEntry *ent,
566 const struct GNUNET_FS_Uri *uri,
567 const struct GNUNET_CONTAINER_MetaData *meta)
569 struct GNUNET_HashCode key;
570 struct GNUNET_FS_SearchResult *sr;
571 struct GetResultContext grc;
576 GNUNET_assert (NULL != sc);
577 GNUNET_FS_uri_to_key (uri, &key);
579 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
581 &test_result_present,
583 return; /* duplicate result */
584 /* try to find search result in master map */
587 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
589 &get_result_present, &grc);
591 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
594 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
597 sr->anonymity = sc->anonymity;
598 sr->uri = GNUNET_FS_uri_dup (uri);
599 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
600 sr->mandatory_missing = sc->mandatory_count;
602 sr->keyword_bitmap = GNUNET_malloc ((sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
603 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
608 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
610 GNUNET_break (GNUNET_OK ==
611 GNUNET_CONTAINER_multihashmap_put (ent->results,
614 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
616 koff = ent - sc->requests;
617 GNUNET_assert ( (ent >= sc->requests) &&
618 (koff < sc->uri->data.ksk.keywordCount));
619 sr->keyword_bitmap[koff / 8] |= (1 << (koff % 8));
620 /* check if mandatory satisfied */
621 if (1 <= GNUNET_CONTAINER_multihashmap_size (ent->results))
625 GNUNET_break (sr->mandatory_missing > 0);
626 sr->mandatory_missing--;
630 sr->optional_support++;
633 if (0 != sr->mandatory_missing)
635 GNUNET_break (NULL == sr->client_info);
639 notify_client_chk_result (sc, sr);
641 notify_client_chk_update (sc, sr);
642 GNUNET_FS_search_result_sync_ (sr);
643 GNUNET_FS_search_start_probe_ (sr);
648 * Start search for content, internal API.
650 * @param h handle to the file sharing subsystem
651 * @param uri specifies the search parameters; can be
652 * a KSK URI or an SKS URI.
653 * @param anonymity desired level of anonymity
654 * @param options options for the search
655 * @param cctx client context
656 * @param psearch parent search result (for namespace update searches)
657 * @return context that can be used to control the search
659 static struct GNUNET_FS_SearchContext *
660 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
661 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
662 void *cctx, struct GNUNET_FS_SearchResult *psearch);
666 * We have received an SKS result. Start searching for updates and
667 * notify the client if it is a new result.
669 * @param sc context for the overall query
670 * @param id_update identifier for updates, NULL for none
671 * @param uri the URI that was found
672 * @param meta metadata associated with the URI
675 process_sks_result (struct GNUNET_FS_SearchContext *sc,
676 const char *id_update,
677 const struct GNUNET_FS_Uri *uri,
678 const struct GNUNET_CONTAINER_MetaData *meta)
680 struct GNUNET_FS_Uri uu;
681 struct GNUNET_HashCode key;
682 struct GNUNET_FS_SearchResult *sr;
685 GNUNET_assert (NULL != sc);
686 GNUNET_FS_uri_to_key (uri, &key);
687 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
690 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
691 &test_result_present,
693 return; /* duplicate result */
694 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
697 sr->anonymity = sc->anonymity;
698 sr->uri = GNUNET_FS_uri_dup (uri);
699 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
701 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
702 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
703 GNUNET_FS_search_result_sync_ (sr);
704 GNUNET_FS_search_start_probe_ (sr);
706 if (0 == sr->mandatory_missing)
707 notify_client_chk_result (sc, sr);
709 GNUNET_break (NULL == sr->client_info);
710 /* search for updates */
711 if (0 == strlen (id_update))
712 return; /* no updates */
713 uu.type = GNUNET_FS_URI_SKS;
714 uu.data.sks.ns = sc->uri->data.sks.ns;
715 uu.data.sks.identifier = GNUNET_strdup (id_update);
716 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
717 GNUNET_free (uu.data.sks.identifier);
722 * Decrypt a ublock using a 'keyword' as the passphrase. Given the
723 * KSK public key derived from the keyword, this function looks up
724 * the original keyword in the search context and decrypts the
725 * given ciphertext block.
727 * @param sc search context with the keywords
728 * @param dpub derived public key used for the search
729 * @param edata encrypted data
730 * @param edata_size number of bytes in @a edata (and @a data)
731 * @param data where to store the plaintext
732 * @return keyword index on success, #GNUNET_SYSERR on error (no such
733 * keyword, internal error)
736 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
737 const struct GNUNET_CRYPTO_EcdsaPublicKey *dpub,
742 const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
743 struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
747 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
748 if (0 == memcmp (dpub,
749 &sc->requests[i].dpub,
750 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
752 if (i == sc->uri->data.ksk.keywordCount)
754 /* oops, does not match any of our keywords!? */
756 return GNUNET_SYSERR;
759 anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
760 GNUNET_CRYPTO_ecdsa_key_get_public (anon, &anon_pub);
761 GNUNET_FS_ublock_decrypt_ (edata, edata_size,
763 sc->requests[i].keyword,
770 * Process a keyword search result. The actual type of block is
771 * a UBlock; we know it is a keyword search result because that's
772 * what we were searching for.
774 * @param sc our search context
775 * @param ub the ublock with the keyword search result
776 * @param size size of @a ub
779 process_kblock (struct GNUNET_FS_SearchContext *sc,
780 const struct UBlock *ub,
784 char pt[size - sizeof (struct UBlock)];
786 struct GNUNET_CONTAINER_MetaData *meta;
787 struct GNUNET_FS_Uri *uri;
791 if (-1 == (i = decrypt_block_with_keyword (sc,
792 &ub->verification_key,
794 size - sizeof (struct UBlock),
797 /* parse; pt[0] is just '\0', so we skip over that */
798 eos = memchr (&pt[1], '\0', sizeof (pt) - 1);
804 if (NULL == (uri = GNUNET_FS_uri_parse (&pt[1], &emsg)))
806 if (GNUNET_FS_VERSION > 0x00090400)
808 /* we broke this in 0x00090300, so don't bitch
809 too loudly just one version up... */
810 GNUNET_break_op (0); /* ublock malformed */
811 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
812 _("Failed to parse URI `%s': %s\n"),
816 GNUNET_free_non_null (emsg);
820 if (sizeof (pt) == j)
821 meta = GNUNET_CONTAINER_meta_data_create ();
823 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
826 GNUNET_break_op (0); /* ublock malformed */
827 GNUNET_FS_uri_destroy (uri);
830 process_ksk_result (sc,
836 GNUNET_CONTAINER_meta_data_destroy (meta);
837 GNUNET_FS_uri_destroy (uri);
842 * Process a namespace-search result. The actual type of block is
843 * a UBlock; we know it is a namespace search result because that's
844 * what we were searching for.
846 * @param sc our search context
847 * @param ub the ublock with a namespace result
848 * @param size size of @a ub
851 process_sblock (struct GNUNET_FS_SearchContext *sc,
852 const struct UBlock *ub,
855 size_t len = size - sizeof (struct UBlock);
857 struct GNUNET_FS_Uri *uri;
858 struct GNUNET_CONTAINER_MetaData *meta;
864 GNUNET_FS_ublock_decrypt_ (&ub[1], len,
865 &sc->uri->data.sks.ns,
866 sc->uri->data.sks.identifier,
869 if (0 == (off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris)))
871 GNUNET_break_op (0); /* ublock malformed */
874 if (NULL == (meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off)))
876 GNUNET_break_op (0); /* ublock malformed */
879 if (NULL == (uri = GNUNET_FS_uri_parse (uris, &emsg)))
881 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
882 _("Failed to parse URI `%s': %s\n"),
884 GNUNET_break_op (0); /* ublock malformed */
885 GNUNET_free_non_null (emsg);
886 GNUNET_CONTAINER_meta_data_destroy (meta);
890 process_sks_result (sc, id, uri, meta);
892 GNUNET_FS_uri_destroy (uri);
893 GNUNET_CONTAINER_meta_data_destroy (meta);
898 * Process a search result.
900 * @param sc our search context
901 * @param type type of the result
902 * @param expiration when it will expire
903 * @param data the (encrypted) response
904 * @param size size of @a data
907 process_result (struct GNUNET_FS_SearchContext *sc,
908 enum GNUNET_BLOCK_Type type,
909 struct GNUNET_TIME_Absolute expiration,
913 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value_us > 0)
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Result received has already expired.\n");
917 return; /* result expired */
921 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
922 if (GNUNET_FS_URI_SKS == sc->uri->type)
923 process_sblock (sc, data, size);
925 process_kblock (sc, data, size);
927 case GNUNET_BLOCK_TYPE_ANY:
930 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
933 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
936 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
940 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
941 _("Got result with unknown block type `%d', ignoring"), type);
948 * Shutdown any existing connection to the FS
949 * service and try to establish a fresh one
950 * (and then re-transmit our search request).
952 * @param sc the search to reconnec
955 try_reconnect (struct GNUNET_FS_SearchContext *sc);
959 * Type of a function to call when we receive a message
963 * @param msg message received, NULL on timeout or fatal error
966 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
968 struct GNUNET_FS_SearchContext *sc = cls;
969 const struct ClientPutMessage *cm;
972 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
973 (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
978 msize = ntohs (msg->size);
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Receiving %u bytes of result from fs service\n", msize);
981 cm = (const struct ClientPutMessage *) msg;
982 process_result (sc, ntohl (cm->type),
983 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
984 msize - sizeof (struct ClientPutMessage));
985 /* continue receiving */
986 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
987 GNUNET_TIME_UNIT_FOREVER_REL);
992 * Schedule the transmission of the (next) search request
995 * @param sc context for the search
998 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
1002 * Closure for 'build_result_set'.
1004 struct MessageBuilderContext
1007 * How many entries can we store to xoff.
1009 unsigned int put_cnt;
1012 * How many entries should we skip.
1014 unsigned int skip_cnt;
1017 * Where to store the keys.
1019 struct GNUNET_HashCode *xoff;
1022 * Search context we are iterating for.
1024 struct GNUNET_FS_SearchContext *sc;
1027 * Keyword offset the search result must match (0 for SKS)
1029 unsigned int keyword_offset;
1034 * Iterating over the known results, pick those matching the given
1035 * result range and store their keys at 'xoff'.
1037 * @param cls the `struct MessageBuilderContext`
1038 * @param key key for a result
1039 * @param value the search result
1040 * @return #GNUNET_OK to continue iterating
1043 build_result_set (void *cls,
1044 const struct GNUNET_HashCode *key,
1047 struct MessageBuilderContext *mbc = cls;
1048 struct GNUNET_FS_SearchResult *sr = value;
1050 if ( (NULL != sr->keyword_bitmap) &&
1051 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1052 return GNUNET_OK; /* have no match for this keyword yet */
1053 if (mbc->skip_cnt > 0)
1058 if (0 == mbc->put_cnt)
1059 return GNUNET_SYSERR;
1060 mbc->sc->search_request_map_offset++;
1061 mbc->xoff[--mbc->put_cnt] = *key;
1068 * Iterating over the known results, count those matching the given
1069 * result range and increment put count for each.
1071 * @param cls the `struct MessageBuilderContext`
1072 * @param key key for a result
1073 * @param value the search result
1074 * @return #GNUNET_OK to continue iterating
1077 find_result_set (void *cls,
1078 const struct GNUNET_HashCode *key,
1081 struct MessageBuilderContext *mbc = cls;
1082 struct GNUNET_FS_SearchResult *sr = value;
1084 if ( (NULL != sr->keyword_bitmap) &&
1085 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1086 return GNUNET_OK; /* have no match for this keyword yet */
1093 * We're ready to transmit the search request to the file-sharing
1094 * service. Do it. If the request is too large to fit into a single
1095 * message, transmit in increments.
1097 * @param cls closure
1098 * @param size number of bytes available in @a buf
1099 * @param buf where the callee should write the message
1100 * @return number of bytes written to @a buf
1103 transmit_search_request (void *cls, size_t size, void *buf)
1105 struct GNUNET_FS_SearchContext *sc = cls;
1106 struct MessageBuilderContext mbc;
1108 struct SearchMessage *sm;
1109 struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
1110 unsigned int total_seen_results; /* total number of result hashes to send */
1111 unsigned int message_size_limit;
1120 mbc.skip_cnt = sc->search_request_map_offset;
1122 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1123 mbc.xoff = (struct GNUNET_HashCode *) &sm[1];
1124 options = SEARCH_MESSAGE_OPTION_NONE;
1125 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1126 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1127 if (GNUNET_FS_uri_test_ksk (sc->uri))
1129 msize = sizeof (struct SearchMessage);
1130 GNUNET_assert (size >= msize);
1131 mbc.keyword_offset = sc->keyword_offset;
1132 /* calculate total number of known results (in put_cnt => total_seen_results) */
1134 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1135 &find_result_set, &mbc);
1136 total_seen_results = mbc.put_cnt;
1137 /* calculate how many results we can send in this message */
1138 message_size_limit = (size - msize) / sizeof (struct GNUNET_HashCode);
1139 mbc.put_cnt = GNUNET_MIN (message_size_limit,
1140 total_seen_results - mbc.skip_cnt);
1141 if (sc->search_request_map_offset < total_seen_results)
1142 GNUNET_assert (mbc.put_cnt > 0);
1144 /* now build message */
1145 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1146 sm->header.size = htons (msize);
1147 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1148 sm->anonymity_level = htonl (sc->anonymity);
1149 memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
1150 sm->query = sc->requests[sc->keyword_offset].uquery;
1151 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1152 &build_result_set, &mbc);
1153 GNUNET_assert (0 == mbc.put_cnt);
1154 GNUNET_assert (total_seen_results >= sc->search_request_map_offset);
1155 if (total_seen_results != sc->search_request_map_offset)
1157 /* more requesting to be done... */
1158 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1159 schedule_transmit_search_request (sc);
1162 sm->options = htonl (options);
1163 sc->keyword_offset++;
1164 sc->search_request_map_offset = 0;
1165 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1167 /* more requesting to be done... */
1168 schedule_transmit_search_request (sc);
1174 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1175 msize = sizeof (struct SearchMessage);
1176 GNUNET_assert (size >= msize);
1177 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1178 sm->anonymity_level = htonl (sc->anonymity);
1179 memset (&sm->target, 0, sizeof (struct GNUNET_PeerIdentity));
1180 GNUNET_CRYPTO_ecdsa_public_key_derive (&sc->uri->data.sks.ns,
1181 sc->uri->data.sks.identifier,
1184 GNUNET_CRYPTO_hash (&dpub,
1187 message_size_limit = (size - msize) / sizeof (struct GNUNET_HashCode);
1188 total_seen_results = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1189 mbc.put_cnt = GNUNET_MIN (message_size_limit,
1190 total_seen_results - mbc.skip_cnt);
1191 mbc.keyword_offset = 0;
1192 if (sc->search_request_map_offset < total_seen_results)
1193 GNUNET_assert (mbc.put_cnt > 0);
1194 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1195 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1196 &build_result_set, &mbc);
1197 sm->header.size = htons (msize);
1198 GNUNET_assert (total_seen_results >= sc->search_request_map_offset);
1199 if (total_seen_results != sc->search_request_map_offset)
1201 /* more requesting to be done... */
1202 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1203 schedule_transmit_search_request (sc);
1206 sm->options = htonl (options);
1208 GNUNET_CLIENT_receive (sc->client,
1209 &receive_results, sc,
1210 GNUNET_TIME_UNIT_FOREVER_REL);
1216 * Schedule the transmission of the (next) search request
1219 * @param sc context for the search
1222 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1227 unsigned int request;
1229 size = sizeof (struct SearchMessage);
1231 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1232 sc->search_request_map_offset;
1233 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
1234 request = GNUNET_MIN (fit, left);
1235 size += sizeof (struct GNUNET_HashCode) * request;
1236 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1237 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1239 &transmit_search_request, sc);
1244 * Reconnect to the FS service and transmit
1247 * @param cls our search context
1251 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1253 struct GNUNET_FS_SearchContext *sc = cls;
1254 struct GNUNET_CLIENT_Connection *client;
1256 sc->task = GNUNET_SCHEDULER_NO_TASK;
1257 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1263 sc->client = client;
1264 sc->search_request_map_offset = 0;
1265 sc->keyword_offset = 0;
1266 schedule_transmit_search_request (sc);
1271 * Shutdown any existing connection to the FS
1272 * service and try to establish a fresh one
1273 * (and then re-transmit our search request).
1275 * @param sc the search to reconnec
1278 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1280 if (NULL != sc->client)
1282 GNUNET_CLIENT_disconnect (sc->client);
1285 sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
1287 GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff,
1294 * Start search for content, internal API.
1296 * @param h handle to the file sharing subsystem
1297 * @param uri specifies the search parameters; can be
1298 * a KSK URI or an SKS URI.
1299 * @param anonymity desired level of anonymity
1300 * @param options options for the search
1301 * @param cctx initial value for the client context
1302 * @param psearch parent search result (for namespace update searches)
1303 * @return context that can be used to control the search
1305 static struct GNUNET_FS_SearchContext *
1306 search_start (struct GNUNET_FS_Handle *h,
1307 const struct GNUNET_FS_Uri *uri,
1309 enum GNUNET_FS_SearchOptions options,
1311 struct GNUNET_FS_SearchResult *psearch)
1313 struct GNUNET_FS_SearchContext *sc;
1314 struct GNUNET_FS_ProgressInfo pi;
1316 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
1318 sc->options = options;
1319 sc->uri = GNUNET_FS_uri_dup (uri);
1320 sc->anonymity = anonymity;
1321 sc->start_time = GNUNET_TIME_absolute_get ();
1322 if (NULL != psearch)
1324 sc->psearch_result = psearch;
1325 psearch->update_search = sc;
1327 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1328 sc->client_info = cctx;
1329 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1331 GNUNET_FS_uri_destroy (sc->uri);
1332 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1336 GNUNET_FS_search_sync_ (sc);
1337 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1338 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1344 * Update the 'results' map for the individual keywords with the
1345 * results from the 'global' result set.
1347 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
1348 * @param key current key code
1349 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
1350 * @return #GNUNET_YES (we should continue to iterate)
1353 update_sre_result_maps (void *cls,
1354 const struct GNUNET_HashCode *key,
1357 struct GNUNET_FS_SearchContext *sc = cls;
1358 struct GNUNET_FS_SearchResult *sr = value;
1361 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1362 if (0 != (sr->keyword_bitmap[i / 8] & (1 << (i % 8))))
1363 GNUNET_break (GNUNET_OK ==
1364 GNUNET_CONTAINER_multihashmap_put (sc->requests[i].results,
1367 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1374 * Build the request and actually initiate the search using the
1375 * GNUnet FS service.
1377 * @param sc search context
1378 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1381 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1384 const char *keyword;
1385 const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
1386 struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
1387 struct SearchRequestEntry *sre;
1389 GNUNET_assert (NULL == sc->client);
1390 if (GNUNET_FS_uri_test_ksk (sc->uri))
1392 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1393 anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
1394 GNUNET_CRYPTO_ecdsa_key_get_public (anon, &anon_pub);
1396 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1397 sc->uri->data.ksk.keywordCount);
1398 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1400 keyword = &sc->uri->data.ksk.keywords[i][1];
1401 sre = &sc->requests[i];
1402 sre->keyword = GNUNET_strdup (keyword);
1403 GNUNET_CRYPTO_ecdsa_public_key_derive (&anon_pub,
1407 GNUNET_CRYPTO_hash (&sre->dpub,
1408 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
1410 sre->mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1412 sc->mandatory_count++;
1413 sre->results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
1415 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1416 &update_sre_result_maps,
1419 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1420 if (NULL == sc->client)
1421 return GNUNET_SYSERR;
1422 sc->search_request_map_offset = 0;
1423 schedule_transmit_search_request (sc);
1429 * Freeze probes for the given search result.
1431 * @param cls the global FS handle
1432 * @param key the key for the search result (unused)
1433 * @param value the search result to free
1434 * @return #GNUNET_OK
1437 search_result_freeze_probes (void *cls,
1438 const struct GNUNET_HashCode *key,
1441 struct GNUNET_FS_SearchResult *sr = value;
1443 if (NULL != sr->probe_ctx)
1445 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1446 sr->probe_ctx = NULL;
1447 GNUNET_FS_stop_probe_ping_task_ (sr);
1449 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1451 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1452 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1454 if (NULL != sr->update_search)
1455 GNUNET_FS_search_pause (sr->update_search);
1461 * Resume probes for the given search result.
1463 * @param cls the global FS handle
1464 * @param key the key for the search result (unused)
1465 * @param value the search result to free
1466 * @return #GNUNET_OK
1469 search_result_resume_probes (void *cls,
1470 const struct GNUNET_HashCode * key,
1473 struct GNUNET_FS_SearchResult *sr = value;
1475 GNUNET_FS_search_start_probe_ (sr);
1476 if (NULL != sr->update_search)
1477 GNUNET_FS_search_continue (sr->update_search);
1483 * Signal suspend and free the given search result.
1485 * @param cls the global FS handle
1486 * @param key the key for the search result (unused)
1487 * @param value the search result to free
1488 * @return #GNUNET_OK
1491 search_result_suspend (void *cls,
1492 const struct GNUNET_HashCode *key,
1495 struct GNUNET_FS_SearchContext *sc = cls;
1496 struct GNUNET_FS_SearchResult *sr = value;
1497 struct GNUNET_FS_ProgressInfo pi;
1499 if (NULL != sr->download)
1501 GNUNET_FS_download_signal_suspend_ (sr->download);
1502 sr->download = NULL;
1504 if (NULL != sr->update_search)
1506 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1507 sr->update_search = NULL;
1509 GNUNET_FS_search_stop_probe_ (sr);
1510 if (0 == sr->mandatory_missing)
1512 /* client is aware of search result, notify about suspension event */
1513 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1514 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1515 pi.value.search.specifics.result_suspend.meta = sr->meta;
1516 pi.value.search.specifics.result_suspend.uri = sr->uri;
1517 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1519 GNUNET_break (NULL == sr->client_info);
1520 GNUNET_free_non_null (sr->serialization);
1521 GNUNET_FS_uri_destroy (sr->uri);
1522 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1523 GNUNET_free_non_null (sr->keyword_bitmap);
1530 * Create SUSPEND event for the given search operation
1531 * and then clean up our state (without stop signal).
1533 * @param cls the `struct GNUNET_FS_SearchContext` to signal for
1536 GNUNET_FS_search_signal_suspend_ (void *cls)
1538 struct GNUNET_FS_SearchContext *sc = cls;
1539 struct GNUNET_FS_ProgressInfo pi;
1542 GNUNET_FS_end_top (sc->h, sc->top);
1543 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1544 &search_result_suspend, sc);
1545 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1546 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1547 GNUNET_break (NULL == sc->client_info);
1548 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1550 GNUNET_SCHEDULER_cancel (sc->task);
1551 sc->task = GNUNET_SCHEDULER_NO_TASK;
1553 if (NULL != sc->client)
1555 GNUNET_CLIENT_disconnect (sc->client);
1558 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1559 if (NULL != sc->requests)
1561 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1562 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1564 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1565 GNUNET_free (sc->requests[i].keyword);
1568 GNUNET_free_non_null (sc->requests);
1569 GNUNET_free_non_null (sc->emsg);
1570 GNUNET_FS_uri_destroy (sc->uri);
1571 GNUNET_free_non_null (sc->serialization);
1577 * Start search for content.
1579 * @param h handle to the file sharing subsystem
1580 * @param uri specifies the search parameters; can be
1581 * a KSK URI or an SKS URI.
1582 * @param anonymity desired level of anonymity
1583 * @param options options for the search
1584 * @param cctx initial value for the client context
1585 * @return context that can be used to control the search
1587 struct GNUNET_FS_SearchContext *
1588 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1589 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1590 enum GNUNET_FS_SearchOptions options, void *cctx)
1592 struct GNUNET_FS_SearchContext *ret;
1594 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1597 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1605 * @param sc context for the search that should be paused
1608 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1610 struct GNUNET_FS_ProgressInfo pi;
1612 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1614 GNUNET_SCHEDULER_cancel (sc->task);
1615 sc->task = GNUNET_SCHEDULER_NO_TASK;
1617 if (NULL != sc->client)
1618 GNUNET_CLIENT_disconnect (sc->client);
1620 GNUNET_FS_search_sync_ (sc);
1621 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1622 &search_result_freeze_probes, sc);
1623 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1624 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1629 * Continue paused search.
1631 * @param sc context for the search that should be resumed
1634 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1636 struct GNUNET_FS_ProgressInfo pi;
1638 GNUNET_assert (NULL == sc->client);
1639 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sc->task);
1640 do_reconnect (sc, NULL);
1641 GNUNET_FS_search_sync_ (sc);
1642 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1643 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1644 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1645 &search_result_resume_probes, sc);
1650 * Signal stop for the given search result.
1652 * @param cls the global FS handle
1653 * @param key the key for the search result (unused)
1654 * @param value the search result to free
1655 * @return #GNUNET_OK
1658 search_result_stop (void *cls,
1659 const struct GNUNET_HashCode *key,
1662 struct GNUNET_FS_SearchContext *sc = cls;
1663 struct GNUNET_FS_SearchResult *sr = value;
1664 struct GNUNET_FS_ProgressInfo pi;
1666 GNUNET_FS_search_stop_probe_ (sr);
1667 if (NULL != sr->download)
1669 sr->download->search = NULL;
1671 GNUNET_FS_make_top (sr->download->h,
1672 &GNUNET_FS_download_signal_suspend_,
1674 if (NULL != sr->download->serialization)
1676 GNUNET_FS_remove_sync_file_ (sc->h,
1677 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1678 sr->download->serialization);
1679 GNUNET_free (sr->download->serialization);
1680 sr->download->serialization = NULL;
1682 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1683 GNUNET_FS_download_make_status_ (&pi, sr->download);
1684 GNUNET_FS_download_sync_ (sr->download);
1685 sr->download = NULL;
1687 if (0 != sr->mandatory_missing)
1689 /* client is unaware of search result as
1690 it does not match required keywords */
1691 GNUNET_break (NULL == sr->client_info);
1694 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1695 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1696 pi.value.search.specifics.result_stopped.meta = sr->meta;
1697 pi.value.search.specifics.result_stopped.uri = sr->uri;
1698 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sc);
1704 * Free the given search result.
1706 * @param cls the global FS handle
1707 * @param key the key for the search result (unused)
1708 * @param value the search result to free
1709 * @return #GNUNET_OK
1712 search_result_free (void *cls,
1713 const struct GNUNET_HashCode *key,
1716 struct GNUNET_FS_SearchResult *sr = value;
1718 if (NULL != sr->update_search)
1720 GNUNET_FS_search_stop (sr->update_search);
1721 GNUNET_assert (NULL == sr->update_search);
1723 GNUNET_break (NULL == sr->probe_ctx);
1724 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task);
1725 GNUNET_break (NULL == sr->client_info);
1726 GNUNET_free_non_null (sr->serialization);
1727 GNUNET_FS_uri_destroy (sr->uri);
1728 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1729 GNUNET_free_non_null (sr->keyword_bitmap);
1736 * Stop search for content.
1738 * @param sc context for the search that should be stopped
1741 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1743 struct GNUNET_FS_ProgressInfo pi;
1746 if (NULL != sc->top)
1747 GNUNET_FS_end_top (sc->h, sc->top);
1748 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1749 &search_result_stop, sc);
1750 if (NULL != sc->psearch_result)
1751 sc->psearch_result->update_search = NULL;
1752 if (NULL != sc->serialization)
1754 GNUNET_FS_remove_sync_file_ (sc->h,
1755 (sc->psearch_result !=
1756 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1757 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1759 GNUNET_FS_remove_sync_dir_ (sc->h,
1760 (sc->psearch_result !=
1761 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1762 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1764 GNUNET_free (sc->serialization);
1766 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1767 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1768 GNUNET_break (NULL == sc->client_info);
1769 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1770 GNUNET_SCHEDULER_cancel (sc->task);
1771 if (NULL != sc->client)
1772 GNUNET_CLIENT_disconnect (sc->client);
1773 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1774 &search_result_free, sc);
1775 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1776 if (NULL != sc->requests)
1778 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1779 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1780 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1782 GNUNET_free_non_null (sc->requests);
1783 GNUNET_free_non_null (sc->emsg);
1784 GNUNET_FS_uri_destroy (sc->uri);
1788 /* end of fs_search.c */