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
75 * @param cls points to the URI we check against
77 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
79 * @return GNUNET_SYSERR if the result is present,
83 test_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
85 const struct GNUNET_FS_Uri *uri = cls;
86 struct GNUNET_FS_SearchResult *sr = value;
88 if (GNUNET_FS_uri_test_equal (uri, sr->uri))
95 * We've found a new CHK result. Let the client
98 * @param sc the search context
99 * @param sr the specific result
102 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
103 struct GNUNET_FS_SearchResult *sr)
105 struct GNUNET_FS_ProgressInfo pi;
107 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
108 pi.value.search.specifics.result.meta = sr->meta;
109 pi.value.search.specifics.result.uri = sr->uri;
110 pi.value.search.specifics.result.result = sr;
111 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
112 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
117 * We've found new information about an existing CHK result. Let the
118 * client know about it.
120 * @param sc the search context
121 * @param sr the specific result
124 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
125 struct GNUNET_FS_SearchResult *sr)
127 struct GNUNET_FS_ProgressInfo pi;
129 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
130 pi.value.search.specifics.update.cctx = sr->client_info;
131 pi.value.search.specifics.update.meta = sr->meta;
132 pi.value.search.specifics.update.uri = sr->uri;
133 pi.value.search.specifics.update.availability_rank =
134 2 * sr->availability_success - sr->availability_trials;
135 pi.value.search.specifics.update.availability_certainty =
136 sr->availability_trials;
137 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
138 pi.value.search.specifics.update.current_probe_time
139 = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
140 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
145 * Context for "get_result_present".
147 struct GetResultContext
150 * The URI we're looking for.
152 const struct GNUNET_FS_Uri *uri;
155 * Where to store a pointer to the search
156 * result struct if we found a match.
158 struct GNUNET_FS_SearchResult *sr;
163 * Check if the given result is identical to the given URI and if so
166 * @param cls a "struct GetResultContext"
167 * @param key not used
168 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
169 * should compare with
173 get_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
175 struct GetResultContext *grc = cls;
176 struct GNUNET_FS_SearchResult *sr = value;
178 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
185 * Signal result of last probe to client and then schedule next
189 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
191 struct GNUNET_FS_ProgressInfo pi;
193 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
194 pi.value.search.specifics.update.cctx = sr->client_info;
195 pi.value.search.specifics.update.meta = sr->meta;
196 pi.value.search.specifics.update.uri = sr->uri;
197 pi.value.search.specifics.update.availability_rank
198 = 2 * sr->availability_success - sr->availability_trials;
199 pi.value.search.specifics.update.availability_certainty
200 = sr->availability_trials;
201 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
202 pi.value.search.specifics.update.current_probe_time
203 = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
204 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sr->sc);
205 GNUNET_FS_search_start_probe_ (sr);
210 * Handle the case where we have failed to receive a response for our probe.
212 * @param cls our 'struct GNUNET_FS_SearchResult*'
213 * @param tc scheduler context
216 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct GNUNET_FS_SearchResult *sr = cls;
220 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
221 sr->availability_trials++;
222 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
223 sr->probe_ctx = NULL;
224 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
226 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
227 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
229 GNUNET_FS_search_result_sync_ (sr);
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "Probe #%u for search result %p failed\n",
232 sr->availability_trials,
234 signal_probe_result (sr);
239 * Handle the case where we have gotten a response for our probe.
241 * @param cls our 'struct GNUNET_FS_SearchResult*'
242 * @param tc scheduler context
245 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
247 struct GNUNET_FS_SearchResult *sr = cls;
249 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
250 sr->availability_trials++;
251 sr->availability_success++;
252 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
253 sr->probe_ctx = NULL;
254 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
256 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
257 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
259 GNUNET_FS_search_result_sync_ (sr);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Probe #%u for search result %p succeeded\n",
262 sr->availability_trials,
264 signal_probe_result (sr);
269 * Notification of FS that a search probe has made progress.
270 * This function is used INSTEAD of the client's event handler
271 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
273 * @param cls closure, always NULL (!), actual closure
274 * is in the client-context of the info struct
275 * @param info details about the event, specifying the event type
276 * and various bits about the event
277 * @return client-context (for the next progress call
278 * for this operation; should be set to NULL for
279 * SUSPEND and STOPPED events). The value returned
280 * will be passed to future callbacks in the respective
281 * field in the GNUNET_FS_ProgressInfo struct.
284 GNUNET_FS_search_probe_progress_ (void *cls,
285 const struct GNUNET_FS_ProgressInfo *info)
287 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
288 struct GNUNET_TIME_Relative dur;
290 switch (info->status)
292 case GNUNET_FS_STATUS_DOWNLOAD_START:
295 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
296 /* probes should never be resumed */
299 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
300 /* probes should never be suspended */
303 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
306 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
307 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
309 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
310 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
312 sr->probe_cancel_task =
313 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
314 &probe_failure_handler, sr);
316 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
317 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
319 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
320 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
322 sr->probe_cancel_task =
323 GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
325 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
326 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
328 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
329 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
333 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
334 if (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task)
336 sr->probe_active_time = GNUNET_TIME_absolute_get ();
337 sr->probe_cancel_task =
338 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
339 &probe_failure_handler, sr);
343 /* should only happen if the cancel task was already
344 created on 'DOWNLOAD_INACTIVE' as we were out of time */
345 GNUNET_break (0 == sr->remaining_probe_time.rel_value);
348 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
349 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
351 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
352 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
354 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
355 sr->remaining_probe_time =
356 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
357 if (0 == sr->remaining_probe_time.rel_value)
358 sr->probe_cancel_task =
359 GNUNET_SCHEDULER_add_now (&probe_failure_handler, sr);
360 GNUNET_FS_search_result_sync_ (sr);
371 * Task run periodically to remind clients that a probe is active.
373 * @param cls the 'struct GNUNET_FS_SearchResult' that we are probing for
374 * @param tc scheduler context
377 probe_ping_task (void *cls,
378 const struct GNUNET_SCHEDULER_TaskContext *tc)
380 struct GNUNET_FS_SearchResult *sr = cls;
382 signal_probe_result (sr);
384 = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
391 * Start download probes for the given search result.
393 * @param sr the search result
396 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
401 if (NULL != sr->probe_ctx)
403 if (NULL != sr->download)
405 if (0 == (sr->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
407 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
409 if ( (GNUNET_FS_URI_CHK != sr->uri->type) && (GNUNET_FS_URI_LOC != sr->uri->type))
411 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
414 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
416 off = len / DBLOCK_SIZE;
418 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
420 if (len - off < DBLOCK_SIZE)
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Starting probe #%u (at offset %llu) for search result %p\n",
426 sr->availability_trials + 1,
427 (unsigned long long) off,
429 sr->remaining_probe_time =
430 GNUNET_TIME_relative_multiply (sr->h->avg_block_latency,
431 2 * (1 + sr->availability_trials));
433 GNUNET_FS_download_start (sr->h, sr->uri, sr->meta, NULL, NULL, off,
435 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
436 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
438 = GNUNET_SCHEDULER_add_now (&probe_ping_task,
444 * Start download probes for the given search result.
446 * @param h file-sharing handle to use for the operation
447 * @param uri URI to probe
448 * @param meta meta data associated with the URI
449 * @param client_info client info pointer to use for associated events
450 * @param anonymity anonymity level to use for the probes
451 * @return the search result handle to access the probe activity
453 struct GNUNET_FS_SearchResult *
454 GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
455 const struct GNUNET_FS_Uri *uri,
456 const struct GNUNET_CONTAINER_MetaData *meta,
460 struct GNUNET_FS_SearchResult *sr;
462 GNUNET_assert (NULL != h);
463 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
465 sr->uri = GNUNET_FS_uri_dup (uri);
466 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
467 sr->client_info = client_info;
468 sr->anonymity = anonymity;
469 GNUNET_FS_search_start_probe_ (sr);
475 * Stop probing activity associated with a search result.
477 * @param sr search result
480 GNUNET_FS_search_stop_probe_ (struct GNUNET_FS_SearchResult *sr)
482 if (NULL != sr->probe_ctx)
484 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
485 sr->probe_ctx = NULL;
487 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
489 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
490 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
492 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
494 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
495 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
501 * Stop probe activity. Must ONLY be used on values
502 * returned from 'GNUNET_FS_probe'.
504 * @param sr search result to stop probing for (freed)
505 * @return the value of the 'client_info' pointer
508 GNUNET_FS_probe_stop (struct GNUNET_FS_SearchResult *sr)
512 GNUNET_assert (NULL == sr->sc);
513 GNUNET_FS_search_stop_probe_ (sr);
514 GNUNET_FS_uri_destroy (sr->uri);
515 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
516 client_info = sr->client_info;
523 * We have received a KSK result. Check how it fits in with the
524 * overall query and notify the client accordingly.
526 * @param sc context for the overall query
527 * @param ent entry for the specific keyword
528 * @param uri the URI that was found
529 * @param meta metadata associated with the URI
530 * under the "ent" keyword
533 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
534 struct SearchRequestEntry *ent,
535 const struct GNUNET_FS_Uri *uri,
536 const struct GNUNET_CONTAINER_MetaData *meta)
538 struct GNUNET_HashCode key;
539 struct GNUNET_FS_SearchResult *sr;
540 struct GetResultContext grc;
545 GNUNET_assert (NULL != sc);
546 GNUNET_FS_uri_to_key (uri, &key);
548 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results, &key,
549 &test_result_present,
551 return; /* duplicate result */
552 /* try to find search result in master map */
555 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
556 &get_result_present, &grc);
558 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
561 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
564 sr->anonymity = sc->anonymity;
565 sr->uri = GNUNET_FS_uri_dup (uri);
566 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
567 sr->mandatory_missing = sc->mandatory_count;
569 sr->keyword_bitmap = GNUNET_malloc ((sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
570 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
571 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
575 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
577 koff = ent - sc->requests;
578 GNUNET_assert ( (ent >= sc->requests) && (koff < sc->uri->data.ksk.keywordCount));
579 sr->keyword_bitmap[koff / 8] |= (1 << (koff % 8));
580 /* check if mandatory satisfied */
582 sr->mandatory_missing--;
584 sr->optional_support++;
585 if (0 != sr->mandatory_missing)
588 notify_client_chk_result (sc, sr);
590 notify_client_chk_update (sc, sr);
591 GNUNET_FS_search_result_sync_ (sr);
592 GNUNET_FS_search_start_probe_ (sr);
597 * Start search for content, internal API.
599 * @param h handle to the file sharing subsystem
600 * @param uri specifies the search parameters; can be
601 * a KSK URI or an SKS URI.
602 * @param anonymity desired level of anonymity
603 * @param options options for the search
604 * @param cctx client context
605 * @param psearch parent search result (for namespace update searches)
606 * @return context that can be used to control the search
608 static struct GNUNET_FS_SearchContext *
609 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
610 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
611 void *cctx, struct GNUNET_FS_SearchResult *psearch);
615 * We have received an SKS result. Start searching for updates and
616 * notify the client if it is a new result.
618 * @param sc context for the overall query
619 * @param id_update identifier for updates, NULL for none
620 * @param uri the URI that was found
621 * @param meta metadata associated with the URI
624 process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
625 const struct GNUNET_FS_Uri *uri,
626 const struct GNUNET_CONTAINER_MetaData *meta)
628 struct GNUNET_FS_Uri uu;
629 struct GNUNET_HashCode key;
630 struct GNUNET_FS_SearchResult *sr;
633 GNUNET_assert (NULL != sc);
634 GNUNET_FS_uri_to_key (uri, &key);
635 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
638 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
639 &test_result_present,
641 return; /* duplicate result */
642 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
645 sr->anonymity = sc->anonymity;
646 sr->uri = GNUNET_FS_uri_dup (uri);
647 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
649 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
651 GNUNET_FS_search_result_sync_ (sr);
652 GNUNET_FS_search_start_probe_ (sr);
654 notify_client_chk_result (sc, sr);
655 /* search for updates */
656 if (0 == strlen (id_update))
657 return; /* no updates */
658 uu.type = GNUNET_FS_URI_SKS;
659 uu.data.sks.ns = sc->uri->data.sks.ns;
660 uu.data.sks.identifier = GNUNET_strdup (id_update);
661 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
662 GNUNET_free (uu.data.sks.identifier);
667 * Decrypt a ublock using a 'keyword' as the passphrase. Given the
668 * KSK public key derived from the keyword, this function looks up
669 * the original keyword in the search context and decrypts the
670 * given ciphertext block.
672 * @param sc search context with the keywords
673 * @param dpub derived public key used for the search
674 * @param edata encrypted data
675 * @param edata_size number of bytes in 'edata' (and 'data')
676 * @param data where to store the plaintext
677 * @return keyword index on success, GNUNET_SYSERR on error (no such
678 * keyword, internal error)
681 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
682 const struct GNUNET_CRYPTO_EccPublicKey *dpub,
687 const struct GNUNET_CRYPTO_EccPrivateKey *anon;
688 struct GNUNET_CRYPTO_EccPublicKey anon_pub;
692 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
693 if (0 == memcmp (dpub,
694 &sc->requests[i].dpub,
695 sizeof (struct GNUNET_CRYPTO_EccPublicKey)))
697 if (i == sc->uri->data.ksk.keywordCount)
699 /* oops, does not match any of our keywords!? */
701 return GNUNET_SYSERR;
704 anon = GNUNET_CRYPTO_ecc_key_get_anonymous ();
705 GNUNET_CRYPTO_ecc_key_get_public (anon, &anon_pub);
706 GNUNET_FS_ublock_decrypt_ (edata, edata_size,
708 sc->requests[i].keyword,
715 * Process a keyword search result. The actual type of block is
716 * a UBlock; we know it is a keyword search result because that's
717 * what we were searching for.
719 * @param sc our search context
720 * @param ub the ublock with the keyword search result
721 * @param size size of nb
724 process_kblock (struct GNUNET_FS_SearchContext *sc, const struct UBlock *ub,
728 char pt[size - sizeof (struct UBlock)];
730 struct GNUNET_CONTAINER_MetaData *meta;
731 struct GNUNET_FS_Uri *uri;
735 if (-1 == (i = decrypt_block_with_keyword (sc,
736 &ub->verification_key,
738 size - sizeof (struct UBlock),
741 /* parse; pt[0] is just '\0', so we skip over that */
742 eos = memchr (&pt[1], '\0', sizeof (pt) - 1);
748 if (NULL == (uri = GNUNET_FS_uri_parse (&pt[1], &emsg)))
750 GNUNET_break_op (0); /* ublock malformed */
751 GNUNET_free_non_null (emsg);
755 if (sizeof (pt) == j)
756 meta = GNUNET_CONTAINER_meta_data_create ();
758 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
761 GNUNET_break_op (0); /* ublock malformed */
762 GNUNET_FS_uri_destroy (uri);
765 process_ksk_result (sc, &sc->requests[i], uri, meta);
768 GNUNET_CONTAINER_meta_data_destroy (meta);
769 GNUNET_FS_uri_destroy (uri);
774 * Process a namespace-search result. The actual type of block is
775 * a UBlock; we know it is a namespace search result because that's
776 * what we were searching for.
778 * @param sc our search context
779 * @param ub the ublock with a namespace result
780 * @param size size of sb
783 process_sblock (struct GNUNET_FS_SearchContext *sc,
784 const struct UBlock *ub,
787 size_t len = size - sizeof (struct UBlock);
789 struct GNUNET_FS_Uri *uri;
790 struct GNUNET_CONTAINER_MetaData *meta;
797 GNUNET_FS_ublock_decrypt_ (&ub[1], len,
798 &sc->uri->data.sks.ns,
799 sc->uri->data.sks.identifier,
802 if (0 == (off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris)))
804 GNUNET_break_op (0); /* ublock malformed */
807 if (NULL == (meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off)))
809 GNUNET_break_op (0); /* ublock malformed */
812 if (NULL == (uri = GNUNET_FS_uri_parse (uris, &emsg)))
814 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
815 _("Failed to parse URI `%s': %s\n"),
817 GNUNET_break_op (0); /* ublock malformed */
818 GNUNET_free_non_null (emsg);
819 GNUNET_CONTAINER_meta_data_destroy (meta);
823 process_sks_result (sc, id, uri, meta);
825 GNUNET_FS_uri_destroy (uri);
826 GNUNET_CONTAINER_meta_data_destroy (meta);
831 * Process a search result.
833 * @param sc our search context
834 * @param type type of the result
835 * @param expiration when it will expire
836 * @param data the (encrypted) response
837 * @param size size of data
840 process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
841 struct GNUNET_TIME_Absolute expiration,
845 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Result received has already expired.\n");
849 return; /* result expired */
853 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
854 if (GNUNET_FS_URI_SKS == sc->uri->type)
855 process_sblock (sc, data, size);
857 process_kblock (sc, data, size);
859 case GNUNET_BLOCK_TYPE_ANY:
862 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
865 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
868 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
873 _("Got result with unknown block type `%d', ignoring"), type);
880 * Shutdown any existing connection to the FS
881 * service and try to establish a fresh one
882 * (and then re-transmit our search request).
884 * @param sc the search to reconnec
887 try_reconnect (struct GNUNET_FS_SearchContext *sc);
891 * Type of a function to call when we receive a message
895 * @param msg message received, NULL on timeout or fatal error
898 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
900 struct GNUNET_FS_SearchContext *sc = cls;
901 const struct ClientPutMessage *cm;
904 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
905 (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
910 msize = ntohs (msg->size);
911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
912 "Receiving %u bytes of result from fs service\n", msize);
913 cm = (const struct ClientPutMessage *) msg;
914 process_result (sc, ntohl (cm->type),
915 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
916 msize - sizeof (struct ClientPutMessage));
917 /* continue receiving */
918 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
919 GNUNET_TIME_UNIT_FOREVER_REL);
924 * Schedule the transmission of the (next) search request
927 * @param sc context for the search
930 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
934 * Closure for 'build_result_set'.
936 struct MessageBuilderContext
939 * How many entries can we store to xoff.
941 unsigned int put_cnt;
944 * How many entries should we skip.
946 unsigned int skip_cnt;
949 * Where to store the keys.
951 struct GNUNET_HashCode *xoff;
954 * Search context we are iterating for.
956 struct GNUNET_FS_SearchContext *sc;
959 * Keyword offset the search result must match (0 for SKS)
961 unsigned int keyword_offset;
966 * Iterating over the known results, pick those matching the given
967 * result range and store their keys at 'xoff'.
969 * @param cls the 'struct MessageBuilderContext'
970 * @param key key for a result
971 * @param value the search result
972 * @return GNUNET_OK to continue iterating
975 build_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
977 struct MessageBuilderContext *mbc = cls;
978 struct GNUNET_FS_SearchResult *sr = value;
980 if ( (NULL != sr->keyword_bitmap) &&
981 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
982 return GNUNET_OK; /* have no match for this keyword yet */
983 if (mbc->skip_cnt > 0)
988 if (0 == mbc->put_cnt)
989 return GNUNET_SYSERR;
990 mbc->sc->search_request_map_offset++;
991 mbc->xoff[--mbc->put_cnt] = *key;
998 * Iterating over the known results, count those
999 * matching the given result range and increment
1000 * put count for each.
1002 * @param cls the 'struct MessageBuilderContext'
1003 * @param key key for a result
1004 * @param value the search result
1005 * @return GNUNET_OK to continue iterating
1008 find_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
1010 struct MessageBuilderContext *mbc = cls;
1011 struct GNUNET_FS_SearchResult *sr = value;
1013 if ( (NULL != sr->keyword_bitmap) &&
1014 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1015 return GNUNET_OK; /* have no match for this keyword yet */
1022 * We're ready to transmit the search request to the
1023 * file-sharing service. Do it.
1025 * @param cls closure
1026 * @param size number of bytes available in buf
1027 * @param buf where the callee should write the message
1028 * @return number of bytes written to buf
1031 transmit_search_request (void *cls, size_t size, void *buf)
1033 struct GNUNET_FS_SearchContext *sc = cls;
1034 struct MessageBuilderContext mbc;
1036 struct SearchMessage *sm;
1037 struct GNUNET_CRYPTO_EccPublicKey dpub;
1047 mbc.skip_cnt = sc->search_request_map_offset;
1049 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1050 mbc.xoff = (struct GNUNET_HashCode *) & sm[1];
1051 options = SEARCH_MESSAGE_OPTION_NONE;
1052 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1053 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1054 if (GNUNET_FS_uri_test_ksk (sc->uri))
1056 msize = sizeof (struct SearchMessage);
1057 GNUNET_assert (size >= msize);
1058 mbc.keyword_offset = sc->keyword_offset;
1059 /* calculate total number of known results (in put_cnt => sqms) */
1061 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1062 &find_result_set, &mbc);
1064 /* calculate how many results we can send in this message */
1065 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1066 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1067 if (sc->search_request_map_offset < sqms)
1068 GNUNET_assert (mbc.put_cnt > 0);
1070 /* now build message */
1071 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1072 sm->header.size = htons (msize);
1073 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1074 sm->anonymity_level = htonl (sc->anonymity);
1075 memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
1076 sm->query = sc->requests[sc->keyword_offset].uquery;
1077 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1078 &build_result_set, &mbc);
1079 GNUNET_assert (sqms >= sc->search_request_map_offset);
1080 if (sqms != sc->search_request_map_offset)
1082 /* more requesting to be done... */
1083 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1084 schedule_transmit_search_request (sc);
1087 sm->options = htonl (options);
1088 sc->keyword_offset++;
1089 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1091 /* more requesting to be done... */
1092 schedule_transmit_search_request (sc);
1098 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1099 msize = sizeof (struct SearchMessage);
1100 GNUNET_assert (size >= msize);
1101 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1102 sm->anonymity_level = htonl (sc->anonymity);
1103 memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
1104 GNUNET_CRYPTO_ecc_public_key_derive (&sc->uri->data.sks.ns,
1105 sc->uri->data.sks.identifier,
1107 GNUNET_CRYPTO_hash (&dpub,
1110 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1111 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1112 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1113 mbc.keyword_offset = 0;
1114 if (sc->search_request_map_offset < sqms)
1115 GNUNET_assert (mbc.put_cnt > 0);
1116 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1117 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1118 &build_result_set, &mbc);
1119 sm->header.size = htons (msize);
1120 GNUNET_assert (sqms >= sc->search_request_map_offset);
1121 if (sqms != sc->search_request_map_offset)
1123 /* more requesting to be done... */
1124 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1125 schedule_transmit_search_request (sc);
1128 sm->options = htonl (options);
1130 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1131 GNUNET_TIME_UNIT_FOREVER_REL);
1137 * Schedule the transmission of the (next) search request
1140 * @param sc context for the search
1143 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1149 size = sizeof (struct SearchMessage);
1151 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1152 sc->search_request_map_offset;
1153 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
1154 fit = GNUNET_MIN (fit, sqms);
1155 size += sizeof (struct GNUNET_HashCode) * fit;
1156 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1157 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1158 GNUNET_NO, &transmit_search_request, sc);
1164 * Reconnect to the FS service and transmit
1167 * @param cls our search context
1171 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1173 struct GNUNET_FS_SearchContext *sc = cls;
1174 struct GNUNET_CLIENT_Connection *client;
1176 sc->task = GNUNET_SCHEDULER_NO_TASK;
1177 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1183 sc->client = client;
1184 sc->search_request_map_offset = 0;
1185 sc->keyword_offset = 0;
1186 schedule_transmit_search_request (sc);
1191 * Shutdown any existing connection to the FS
1192 * service and try to establish a fresh one
1193 * (and then re-transmit our search request).
1195 * @param sc the search to reconnec
1198 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1200 if (NULL != sc->client)
1202 GNUNET_CLIENT_disconnect (sc->client);
1205 sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
1207 GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff,
1214 * Start search for content, internal API.
1216 * @param h handle to the file sharing subsystem
1217 * @param uri specifies the search parameters; can be
1218 * a KSK URI or an SKS URI.
1219 * @param anonymity desired level of anonymity
1220 * @param options options for the search
1221 * @param cctx initial value for the client context
1222 * @param psearch parent search result (for namespace update searches)
1223 * @return context that can be used to control the search
1225 static struct GNUNET_FS_SearchContext *
1226 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1227 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1228 void *cctx, struct GNUNET_FS_SearchResult *psearch)
1230 struct GNUNET_FS_SearchContext *sc;
1231 struct GNUNET_FS_ProgressInfo pi;
1233 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1235 sc->options = options;
1236 sc->uri = GNUNET_FS_uri_dup (uri);
1237 sc->anonymity = anonymity;
1238 sc->start_time = GNUNET_TIME_absolute_get ();
1239 if (NULL != psearch)
1241 sc->psearch_result = psearch;
1242 psearch->update_search = sc;
1244 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1245 sc->client_info = cctx;
1246 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1248 GNUNET_FS_uri_destroy (sc->uri);
1249 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1253 GNUNET_FS_search_sync_ (sc);
1254 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1255 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1261 * Build the request and actually initiate the search using the
1262 * GNUnet FS service.
1264 * @param sc search context
1265 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1268 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1271 const char *keyword;
1272 const struct GNUNET_CRYPTO_EccPrivateKey *anon;
1273 struct GNUNET_CRYPTO_EccPublicKey anon_pub;
1274 struct SearchRequestEntry *sre;
1276 GNUNET_assert (NULL == sc->client);
1277 if (GNUNET_FS_uri_test_ksk (sc->uri))
1279 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1280 anon = GNUNET_CRYPTO_ecc_key_get_anonymous ();
1281 GNUNET_CRYPTO_ecc_key_get_public (anon, &anon_pub);
1283 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1284 sc->uri->data.ksk.keywordCount);
1285 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1287 keyword = &sc->uri->data.ksk.keywords[i][1];
1288 sre = &sc->requests[i];
1289 sre->keyword = GNUNET_strdup (keyword);
1290 GNUNET_CRYPTO_ecc_public_key_derive (&anon_pub,
1293 GNUNET_CRYPTO_hash (&sre->dpub,
1294 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
1296 sre->mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1298 sc->mandatory_count++;
1299 sre->results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
1302 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1303 if (NULL == sc->client)
1304 return GNUNET_SYSERR;
1305 schedule_transmit_search_request (sc);
1311 * Freeze probes for the given search result.
1313 * @param cls the global FS handle
1314 * @param key the key for the search result (unused)
1315 * @param value the search result to free
1319 search_result_freeze_probes (void *cls, const struct GNUNET_HashCode * key,
1322 struct GNUNET_FS_SearchResult *sr = value;
1324 if (NULL != sr->probe_ctx)
1326 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1327 sr->probe_ctx = NULL;
1329 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
1331 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
1332 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
1334 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1336 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1337 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1339 if (NULL != sr->update_search)
1340 GNUNET_FS_search_pause (sr->update_search);
1346 * Resume probes for the given search result.
1348 * @param cls the global FS handle
1349 * @param key the key for the search result (unused)
1350 * @param value the search result to free
1354 search_result_resume_probes (void *cls, const struct GNUNET_HashCode * key,
1357 struct GNUNET_FS_SearchResult *sr = value;
1359 GNUNET_FS_search_start_probe_ (sr);
1360 if (NULL != sr->update_search)
1361 GNUNET_FS_search_continue (sr->update_search);
1367 * Signal suspend and free the given search result.
1369 * @param cls the global FS handle
1370 * @param key the key for the search result (unused)
1371 * @param value the search result to free
1375 search_result_suspend (void *cls, const struct GNUNET_HashCode * key, void *value)
1377 struct GNUNET_FS_SearchContext *sc = cls;
1378 struct GNUNET_FS_SearchResult *sr = value;
1379 struct GNUNET_FS_ProgressInfo pi;
1381 if (NULL != sr->download)
1383 GNUNET_FS_download_signal_suspend_ (sr->download);
1384 sr->download = NULL;
1386 if (NULL != sr->probe_ctx)
1388 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1389 sr->probe_ctx = NULL;
1391 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
1393 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
1394 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
1396 if (NULL != sr->update_search)
1398 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1399 sr->update_search = NULL;
1401 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1402 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1403 pi.value.search.specifics.result_suspend.meta = sr->meta;
1404 pi.value.search.specifics.result_suspend.uri = sr->uri;
1405 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1406 GNUNET_break (NULL == sr->client_info);
1407 GNUNET_free_non_null (sr->serialization);
1408 GNUNET_FS_uri_destroy (sr->uri);
1409 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1410 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1412 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1413 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1415 GNUNET_free_non_null (sr->keyword_bitmap);
1422 * Create SUSPEND event for the given search operation
1423 * and then clean up our state (without stop signal).
1425 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1428 GNUNET_FS_search_signal_suspend_ (void *cls)
1430 struct GNUNET_FS_SearchContext *sc = cls;
1431 struct GNUNET_FS_ProgressInfo pi;
1434 GNUNET_FS_end_top (sc->h, sc->top);
1435 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1436 &search_result_suspend, sc);
1437 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1438 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1439 GNUNET_break (NULL == sc->client_info);
1440 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1441 GNUNET_SCHEDULER_cancel (sc->task);
1442 if (NULL != sc->client)
1443 GNUNET_CLIENT_disconnect (sc->client);
1444 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1445 if (NULL != sc->requests)
1447 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1448 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1450 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1451 GNUNET_free (sc->requests[i].keyword);
1454 GNUNET_free_non_null (sc->requests);
1455 GNUNET_free_non_null (sc->emsg);
1456 GNUNET_FS_uri_destroy (sc->uri);
1457 GNUNET_free_non_null (sc->serialization);
1463 * Start search for content.
1465 * @param h handle to the file sharing subsystem
1466 * @param uri specifies the search parameters; can be
1467 * a KSK URI or an SKS URI.
1468 * @param anonymity desired level of anonymity
1469 * @param options options for the search
1470 * @param cctx initial value for the client context
1471 * @return context that can be used to control the search
1473 struct GNUNET_FS_SearchContext *
1474 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1475 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1476 enum GNUNET_FS_SearchOptions options, void *cctx)
1478 struct GNUNET_FS_SearchContext *ret;
1480 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1483 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1491 * @param sc context for the search that should be paused
1494 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1496 struct GNUNET_FS_ProgressInfo pi;
1498 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1499 GNUNET_SCHEDULER_cancel (sc->task);
1500 sc->task = GNUNET_SCHEDULER_NO_TASK;
1501 if (NULL != sc->client)
1502 GNUNET_CLIENT_disconnect (sc->client);
1504 GNUNET_FS_search_sync_ (sc);
1505 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1506 &search_result_freeze_probes, sc);
1507 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1508 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1513 * Continue paused search.
1515 * @param sc context for the search that should be resumed
1518 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1520 struct GNUNET_FS_ProgressInfo pi;
1522 GNUNET_assert (NULL == sc->client);
1523 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sc->task);
1524 do_reconnect (sc, NULL);
1525 GNUNET_FS_search_sync_ (sc);
1526 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1527 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1528 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1529 &search_result_resume_probes, sc);
1534 * Signal stop for the given search result.
1536 * @param cls the global FS handle
1537 * @param key the key for the search result (unused)
1538 * @param value the search result to free
1542 search_result_stop (void *cls, const struct GNUNET_HashCode * key, void *value)
1544 struct GNUNET_FS_SearchContext *sc = cls;
1545 struct GNUNET_FS_SearchResult *sr = value;
1546 struct GNUNET_FS_ProgressInfo pi;
1548 GNUNET_FS_search_stop_probe_ (sr);
1550 if (NULL != sr->download)
1552 sr->download->search = NULL;
1554 GNUNET_FS_make_top (sr->download->h,
1555 &GNUNET_FS_download_signal_suspend_, sr->download);
1556 if (NULL != sr->download->serialization)
1558 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1559 sr->download->serialization);
1560 GNUNET_free (sr->download->serialization);
1561 sr->download->serialization = NULL;
1563 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1564 GNUNET_FS_download_make_status_ (&pi, sr->download);
1565 GNUNET_FS_download_sync_ (sr->download);
1566 sr->download = NULL;
1568 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1569 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1570 pi.value.search.specifics.result_stopped.meta = sr->meta;
1571 pi.value.search.specifics.result_stopped.uri = sr->uri;
1572 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sc);
1578 * Free the given search result.
1580 * @param cls the global FS handle
1581 * @param key the key for the search result (unused)
1582 * @param value the search result to free
1586 search_result_free (void *cls, const struct GNUNET_HashCode * key, void *value)
1588 struct GNUNET_FS_SearchResult *sr = value;
1590 if (NULL != sr->update_search)
1592 GNUNET_FS_search_stop (sr->update_search);
1593 GNUNET_assert (NULL == sr->update_search);
1595 GNUNET_break (NULL == sr->probe_ctx);
1596 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task);
1597 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_ping_task);
1598 GNUNET_break (NULL == sr->client_info);
1599 GNUNET_free_non_null (sr->serialization);
1600 GNUNET_FS_uri_destroy (sr->uri);
1601 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1602 GNUNET_free_non_null (sr->keyword_bitmap);
1609 * Stop search for content.
1611 * @param sc context for the search that should be stopped
1614 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1616 struct GNUNET_FS_ProgressInfo pi;
1619 if (NULL != sc->top)
1620 GNUNET_FS_end_top (sc->h, sc->top);
1621 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1622 &search_result_stop, sc);
1623 if (NULL != sc->psearch_result)
1624 sc->psearch_result->update_search = NULL;
1625 if (NULL != sc->serialization)
1627 GNUNET_FS_remove_sync_file_ (sc->h,
1628 (sc->psearch_result !=
1629 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1630 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1632 GNUNET_FS_remove_sync_dir_ (sc->h,
1633 (sc->psearch_result !=
1634 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1635 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1637 GNUNET_free (sc->serialization);
1639 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1640 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1641 GNUNET_break (NULL == sc->client_info);
1642 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1643 GNUNET_SCHEDULER_cancel (sc->task);
1644 if (NULL != sc->client)
1645 GNUNET_CLIENT_disconnect (sc->client);
1646 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1647 &search_result_free, sc);
1648 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1649 if (NULL != sc->requests)
1651 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1652 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1653 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1655 GNUNET_free_non_null (sc->requests);
1656 GNUNET_free_non_null (sc->emsg);
1657 GNUNET_FS_uri_destroy (sc->uri);
1661 /* end of fs_search.c */