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"
33 * Number of availability trials we perform per search result.
35 #define AVAILABILITY_TRIALS_MAX 8
38 * Fill in all of the generic fields for a search event and
41 * @param pi structure to fill in
42 * @param h file-sharing handle
43 * @param sc overall search context
44 * @return value returned by the callback
47 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
48 struct GNUNET_FS_Handle *h,
49 struct GNUNET_FS_SearchContext *sc)
53 pi->value.search.sc = sc;
54 pi->value.search.cctx = (NULL != sc) ? sc->client_info : NULL;
55 pi->value.search.pctx =
56 ((NULL == sc) || (NULL == sc->psearch_result))
58 : sc->psearch_result->client_info;
59 pi->value.search.query = (NULL != sc) ? sc->uri : NULL;
60 pi->value.search.duration = (NULL != sc)
61 ? GNUNET_TIME_absolute_get_duration (sc->start_time)
62 : GNUNET_TIME_UNIT_ZERO;
63 pi->value.search.anonymity = (NULL != sc) ? sc->anonymity : 0;
64 ret = h->upcb (h->upcb_cls, pi);
70 * Check if the given result is identical
73 * @param cls points to the URI we check against
75 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
77 * @return GNUNET_SYSERR if the result is present,
81 test_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
83 const struct GNUNET_FS_Uri *uri = cls;
84 struct GNUNET_FS_SearchResult *sr = value;
86 if (GNUNET_FS_uri_test_equal (uri, sr->uri))
93 * We've found a new CHK result. Let the client
96 * @param sc the search context
97 * @param sr the specific result
100 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
101 struct GNUNET_FS_SearchResult *sr)
103 struct GNUNET_FS_ProgressInfo pi;
105 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
106 pi.value.search.specifics.result.meta = sr->meta;
107 pi.value.search.specifics.result.uri = sr->uri;
108 pi.value.search.specifics.result.result = sr;
109 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
110 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
115 * We've found new information about an existing CHK result. Let the
116 * client know about it.
118 * @param sc the search context
119 * @param sr the specific result
122 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
123 struct GNUNET_FS_SearchResult *sr)
125 struct GNUNET_FS_ProgressInfo pi;
127 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
128 pi.value.search.specifics.update.cctx = sr->client_info;
129 pi.value.search.specifics.update.meta = sr->meta;
130 pi.value.search.specifics.update.uri = sr->uri;
131 pi.value.search.specifics.update.availability_rank =
132 2 * sr->availability_success - sr->availability_trials;
133 pi.value.search.specifics.update.availability_certainty =
134 sr->availability_trials;
135 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
136 pi.value.search.specifics.update.current_probe_time
137 = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
138 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
143 * Context for "get_result_present".
145 struct GetResultContext
148 * The URI we're looking for.
150 const struct GNUNET_FS_Uri *uri;
153 * Where to store a pointer to the search
154 * result struct if we found a match.
156 struct GNUNET_FS_SearchResult *sr;
161 * Check if the given result is identical to the given URI and if so
164 * @param cls a "struct GetResultContext"
165 * @param key not used
166 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
167 * should compare with
171 get_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
173 struct GetResultContext *grc = cls;
174 struct GNUNET_FS_SearchResult *sr = value;
176 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
183 * Signal result of last probe to client and then schedule next
187 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
189 struct GNUNET_FS_ProgressInfo pi;
191 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
192 pi.value.search.specifics.update.cctx = sr->client_info;
193 pi.value.search.specifics.update.meta = sr->meta;
194 pi.value.search.specifics.update.uri = sr->uri;
195 pi.value.search.specifics.update.availability_rank
196 = 2 * sr->availability_success - sr->availability_trials;
197 pi.value.search.specifics.update.availability_certainty
198 = sr->availability_trials;
199 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
200 pi.value.search.specifics.update.current_probe_time
201 = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
202 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sr->sc);
203 GNUNET_FS_search_start_probe_ (sr);
208 * Handle the case where we have failed to receive a response for our probe.
210 * @param cls our 'struct GNUNET_FS_SearchResult*'
211 * @param tc scheduler context
214 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 struct GNUNET_FS_SearchResult *sr = cls;
218 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
219 sr->availability_trials++;
220 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
221 sr->probe_ctx = NULL;
222 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
224 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
225 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
227 GNUNET_FS_search_result_sync_ (sr);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Probe #%u for search result %p failed\n",
230 sr->availability_trials,
232 signal_probe_result (sr);
237 * Handle the case where we have gotten a response for our probe.
239 * @param cls our 'struct GNUNET_FS_SearchResult*'
240 * @param tc scheduler context
243 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
245 struct GNUNET_FS_SearchResult *sr = cls;
247 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
248 sr->availability_trials++;
249 sr->availability_success++;
250 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
251 sr->probe_ctx = NULL;
252 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
254 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
255 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
257 GNUNET_FS_search_result_sync_ (sr);
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Probe #%u for search result %p succeeded\n",
260 sr->availability_trials,
262 signal_probe_result (sr);
267 * Notification of FS that a search probe has made progress.
268 * This function is used INSTEAD of the client's event handler
269 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
271 * @param cls closure, always NULL (!), actual closure
272 * is in the client-context of the info struct
273 * @param info details about the event, specifying the event type
274 * and various bits about the event
275 * @return client-context (for the next progress call
276 * for this operation; should be set to NULL for
277 * SUSPEND and STOPPED events). The value returned
278 * will be passed to future callbacks in the respective
279 * field in the GNUNET_FS_ProgressInfo struct.
282 GNUNET_FS_search_probe_progress_ (void *cls,
283 const struct GNUNET_FS_ProgressInfo *info)
285 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
286 struct GNUNET_TIME_Relative dur;
288 switch (info->status)
290 case GNUNET_FS_STATUS_DOWNLOAD_START:
293 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
294 /* probes should never be resumed */
297 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
298 /* probes should never be suspended */
301 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
304 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
305 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
307 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
308 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
310 sr->probe_cancel_task =
311 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
312 &probe_failure_handler, sr);
314 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
315 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
317 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
318 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
320 sr->probe_cancel_task =
321 GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
323 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
324 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
326 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
327 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
331 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
332 if (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task)
334 sr->probe_active_time = GNUNET_TIME_absolute_get ();
335 sr->probe_cancel_task =
336 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
337 &probe_failure_handler, sr);
341 /* should only happen if the cancel task was already
342 created on 'DOWNLOAD_INACTIVE' as we were out of time */
343 GNUNET_break (0 == sr->remaining_probe_time.rel_value);
346 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
347 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
349 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
350 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
352 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
353 sr->remaining_probe_time =
354 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
355 if (0 == sr->remaining_probe_time.rel_value)
356 sr->probe_cancel_task =
357 GNUNET_SCHEDULER_add_now (&probe_failure_handler, sr);
358 GNUNET_FS_search_result_sync_ (sr);
369 * Task run periodically to remind clients that a probe is active.
371 * @param cls the 'struct GNUNET_FS_SearchResult' that we are probing for
372 * @param tc scheduler context
375 probe_ping_task (void *cls,
376 const struct GNUNET_SCHEDULER_TaskContext *tc)
378 struct GNUNET_FS_SearchResult *sr = cls;
380 signal_probe_result (sr);
382 = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
389 * Start download probes for the given search result.
391 * @param sr the search result
394 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
399 if (NULL != sr->probe_ctx)
401 if (NULL != sr->download)
403 if (0 == (sr->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
405 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
407 if ( (GNUNET_FS_URI_CHK != sr->uri->type) && (GNUNET_FS_URI_LOC != sr->uri->type))
409 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
412 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
414 off = len / DBLOCK_SIZE;
416 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
418 if (len - off < DBLOCK_SIZE)
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Starting probe #%u (at offset %llu) for search result %p\n",
424 sr->availability_trials + 1,
425 (unsigned long long) off,
427 sr->remaining_probe_time =
428 GNUNET_TIME_relative_multiply (sr->h->avg_block_latency,
429 2 * (1 + sr->availability_trials));
431 GNUNET_FS_download_start (sr->h, sr->uri, sr->meta, NULL, NULL, off,
433 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
434 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
436 = GNUNET_SCHEDULER_add_now (&probe_ping_task,
442 * Start download probes for the given search result.
444 * @param h file-sharing handle to use for the operation
445 * @param uri URI to probe
446 * @param meta meta data associated with the URI
447 * @param client_info client info pointer to use for associated events
448 * @param anonymity anonymity level to use for the probes
449 * @return the search result handle to access the probe activity
451 struct GNUNET_FS_SearchResult *
452 GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
453 const struct GNUNET_FS_Uri *uri,
454 const struct GNUNET_CONTAINER_MetaData *meta,
458 struct GNUNET_FS_SearchResult *sr;
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 "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, &key,
546 &test_result_present,
548 return; /* duplicate result */
549 /* try to find search result in master map */
552 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
553 &get_result_present, &grc);
555 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
558 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
561 sr->anonymity = sc->anonymity;
562 sr->uri = GNUNET_FS_uri_dup (uri);
563 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
564 sr->mandatory_missing = sc->mandatory_count;
566 sr->keyword_bitmap = GNUNET_malloc ((sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
567 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
568 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
572 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
574 koff = ent - sc->requests;
575 GNUNET_assert ( (ent >= sc->requests) && (koff < sc->uri->data.ksk.keywordCount));
576 sr->keyword_bitmap[koff / 8] |= (1 << (koff % 8));
577 /* check if mandatory satisfied */
579 sr->mandatory_missing--;
581 sr->optional_support++;
582 if (0 != sr->mandatory_missing)
585 notify_client_chk_result (sc, sr);
587 notify_client_chk_update (sc, sr);
588 GNUNET_FS_search_result_sync_ (sr);
589 GNUNET_FS_search_start_probe_ (sr);
594 * Start search for content, internal API.
596 * @param h handle to the file sharing subsystem
597 * @param uri specifies the search parameters; can be
598 * a KSK URI or an SKS URI.
599 * @param anonymity desired level of anonymity
600 * @param options options for the search
601 * @param cctx client context
602 * @param psearch parent search result (for namespace update searches)
603 * @return context that can be used to control the search
605 static struct GNUNET_FS_SearchContext *
606 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
607 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
608 void *cctx, struct GNUNET_FS_SearchResult *psearch);
612 * We have received an SKS result. Start searching for updates and
613 * notify the client if it is a new result.
615 * @param sc context for the overall query
616 * @param id_update identifier for updates, NULL for none
617 * @param uri the URI that was found
618 * @param meta metadata associated with the URI
621 process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
622 const struct GNUNET_FS_Uri *uri,
623 const struct GNUNET_CONTAINER_MetaData *meta)
625 struct GNUNET_FS_Uri uu;
626 struct GNUNET_HashCode key;
627 struct GNUNET_FS_SearchResult *sr;
630 GNUNET_assert (NULL != sc);
631 GNUNET_FS_uri_to_key (uri, &key);
632 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
635 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
636 &test_result_present,
638 return; /* duplicate result */
639 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
642 sr->anonymity = sc->anonymity;
643 sr->uri = GNUNET_FS_uri_dup (uri);
644 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
646 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
647 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
648 GNUNET_FS_search_result_sync_ (sr);
649 GNUNET_FS_search_start_probe_ (sr);
651 notify_client_chk_result (sc, sr);
652 /* search for updates */
653 if (0 == strlen (id_update))
654 return; /* no updates */
655 uu.type = GNUNET_FS_URI_SKS;
656 uu.data.sks.ns = sc->uri->data.sks.ns;
657 uu.data.sks.identifier = GNUNET_strdup (id_update);
658 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
659 GNUNET_free (uu.data.sks.identifier);
664 * Decrypt a ublock using a 'keyword' as the passphrase. Given the
665 * KSK public key derived from the keyword, this function looks up
666 * the original keyword in the search context and decrypts the
667 * given ciphertext block.
669 * @param sc search context with the keywords
670 * @param verification_key public key to use to lookup the keyword
671 * @param edata encrypted data
672 * @param edata_size number of bytes in 'edata' (and 'data')
673 * @param data where to store the plaintext
674 * @return keyword index on success, GNUNET_SYSERR on error (no such
675 * keyword, internal error)
678 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
679 const struct GNUNET_FS_PseudonymIdentifier *verification_key,
684 struct GNUNET_HashCode q;
685 struct GNUNET_CRYPTO_AesSessionKey skey;
686 struct GNUNET_CRYPTO_AesInitializationVector iv;
689 GNUNET_CRYPTO_hash (verification_key,
690 sizeof (struct GNUNET_FS_PseudonymIdentifier),
693 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
694 if (0 == memcmp (&q, &sc->requests[i].uquery, sizeof (struct GNUNET_HashCode)))
696 if (i == sc->uri->data.ksk.keywordCount)
698 /* oops, does not match any of our keywords!? */
700 return GNUNET_SYSERR;
703 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].ukey, &skey, &iv);
705 GNUNET_CRYPTO_aes_decrypt (edata, edata_size, &skey,
709 return GNUNET_SYSERR;
716 * Process a keyword search result. The actual type of block is
717 * a UBlock; we know it is a keyword search result because that's
718 * what we were searching for.
720 * @param sc our search context
721 * @param ub the ublock with the keyword search result
722 * @param size size of nb
725 process_kblock (struct GNUNET_FS_SearchContext *sc, const struct UBlock *ub,
729 char pt[size - sizeof (struct UBlock)];
731 struct GNUNET_CONTAINER_MetaData *meta;
732 struct GNUNET_FS_Uri *uri;
736 if (-1 == (i = decrypt_block_with_keyword (sc,
737 &ub->verification_key,
739 size - sizeof (struct UBlock),
742 /* parse; pt[0] is just '\0', so we skip over that */
743 eos = memchr (&pt[1], '\0', sizeof (pt) - 1);
749 if (NULL == (uri = GNUNET_FS_uri_parse (&pt[1], &emsg)))
751 GNUNET_break_op (0); /* ublock malformed */
752 GNUNET_free_non_null (emsg);
756 if (sizeof (pt) == j)
757 meta = GNUNET_CONTAINER_meta_data_create ();
759 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
762 GNUNET_break_op (0); /* ublock malformed */
763 GNUNET_FS_uri_destroy (uri);
766 process_ksk_result (sc, &sc->requests[i], uri, meta);
769 GNUNET_CONTAINER_meta_data_destroy (meta);
770 GNUNET_FS_uri_destroy (uri);
775 * Process a namespace-search result. The actual type of block is
776 * a UBlock; we know it is a namespace search result because that's
777 * what we were searching for.
779 * @param sc our search context
780 * @param ub the ublock with a namespace result
781 * @param size size of sb
784 process_sblock (struct GNUNET_FS_SearchContext *sc,
785 const struct UBlock *ub,
788 size_t len = size - sizeof (struct UBlock);
790 struct GNUNET_CRYPTO_AesSessionKey skey;
791 struct GNUNET_CRYPTO_AesInitializationVector iv;
792 struct GNUNET_FS_Uri *uri;
793 struct GNUNET_CONTAINER_MetaData *meta;
798 struct GNUNET_HashCode key;
799 struct GNUNET_HashCode id_hash;
800 struct GNUNET_HashCode ns_hash;
804 identifier = sc->uri->data.sks.identifier;
805 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &id_hash);
806 GNUNET_CRYPTO_hash (&sc->uri->data.sks.ns,
807 sizeof (sc->uri->data.sks.ns), &ns_hash);
808 GNUNET_CRYPTO_hash_xor (&id_hash, &ns_hash, &key);
809 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
810 if (-1 == GNUNET_CRYPTO_aes_decrypt (&ub[1], len, &skey, &iv, pt))
816 if (0 == (off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris)))
818 GNUNET_break_op (0); /* ublock malformed */
821 if (NULL == (meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off)))
823 GNUNET_break_op (0); /* ublock malformed */
826 if (NULL == (uri = GNUNET_FS_uri_parse (uris, &emsg)))
828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
829 _("Failed to parse URI `%s': %s\n"),
831 GNUNET_break_op (0); /* ublock malformed */
832 GNUNET_free_non_null (emsg);
833 GNUNET_CONTAINER_meta_data_destroy (meta);
837 process_sks_result (sc, id, uri, meta);
839 GNUNET_FS_uri_destroy (uri);
840 GNUNET_CONTAINER_meta_data_destroy (meta);
845 * Process a search result.
847 * @param sc our search context
848 * @param type type of the result
849 * @param expiration when it will expire
850 * @param data the (encrypted) response
851 * @param size size of data
854 process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
855 struct GNUNET_TIME_Absolute expiration,
859 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Result received has already expired.\n");
863 return; /* result expired */
867 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
868 if (GNUNET_FS_URI_SKS == sc->uri->type)
869 process_sblock (sc, data, size);
871 process_kblock (sc, data, size);
873 case GNUNET_BLOCK_TYPE_ANY:
876 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
879 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
882 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
887 _("Got result with unknown block type `%d', ignoring"), type);
894 * Shutdown any existing connection to the FS
895 * service and try to establish a fresh one
896 * (and then re-transmit our search request).
898 * @param sc the search to reconnec
901 try_reconnect (struct GNUNET_FS_SearchContext *sc);
905 * Type of a function to call when we receive a message
909 * @param msg message received, NULL on timeout or fatal error
912 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
914 struct GNUNET_FS_SearchContext *sc = cls;
915 const struct ClientPutMessage *cm;
918 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
919 (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
924 msize = ntohs (msg->size);
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Receiving %u bytes of result from fs service\n", msize);
927 cm = (const struct ClientPutMessage *) msg;
928 process_result (sc, ntohl (cm->type),
929 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
930 msize - sizeof (struct ClientPutMessage));
931 /* continue receiving */
932 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
933 GNUNET_TIME_UNIT_FOREVER_REL);
938 * Schedule the transmission of the (next) search request
941 * @param sc context for the search
944 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
948 * Closure for 'build_result_set'.
950 struct MessageBuilderContext
953 * How many entries can we store to xoff.
955 unsigned int put_cnt;
958 * How many entries should we skip.
960 unsigned int skip_cnt;
963 * Where to store the keys.
965 struct GNUNET_HashCode *xoff;
968 * Search context we are iterating for.
970 struct GNUNET_FS_SearchContext *sc;
973 * Keyword offset the search result must match (0 for SKS)
975 unsigned int keyword_offset;
980 * Iterating over the known results, pick those matching the given
981 * result range and store their keys at 'xoff'.
983 * @param cls the 'struct MessageBuilderContext'
984 * @param key key for a result
985 * @param value the search result
986 * @return GNUNET_OK to continue iterating
989 build_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
991 struct MessageBuilderContext *mbc = cls;
992 struct GNUNET_FS_SearchResult *sr = value;
994 if ( (NULL != sr->keyword_bitmap) &&
995 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
996 return GNUNET_OK; /* have no match for this keyword yet */
997 if (mbc->skip_cnt > 0)
1002 if (0 == mbc->put_cnt)
1003 return GNUNET_SYSERR;
1004 mbc->sc->search_request_map_offset++;
1005 mbc->xoff[--mbc->put_cnt] = *key;
1012 * Iterating over the known results, count those
1013 * matching the given result range and increment
1014 * put count for each.
1016 * @param cls the 'struct MessageBuilderContext'
1017 * @param key key for a result
1018 * @param value the search result
1019 * @return GNUNET_OK to continue iterating
1022 find_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
1024 struct MessageBuilderContext *mbc = cls;
1025 struct GNUNET_FS_SearchResult *sr = value;
1027 if ( (NULL != sr->keyword_bitmap) &&
1028 (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1029 return GNUNET_OK; /* have no match for this keyword yet */
1036 * We're ready to transmit the search request to the
1037 * file-sharing service. Do it.
1039 * @param cls closure
1040 * @param size number of bytes available in buf
1041 * @param buf where the callee should write the message
1042 * @return number of bytes written to buf
1045 transmit_search_request (void *cls, size_t size, void *buf)
1047 struct GNUNET_FS_SearchContext *sc = cls;
1048 struct MessageBuilderContext mbc;
1050 struct SearchMessage *sm;
1051 const char *identifier;
1052 struct GNUNET_HashCode key;
1053 struct GNUNET_HashCode signing_key;
1054 struct GNUNET_HashCode ns_hash;
1055 struct GNUNET_HashCode id_hash;
1056 struct GNUNET_FS_PseudonymIdentifier verification_key;
1066 mbc.skip_cnt = sc->search_request_map_offset;
1068 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1069 mbc.xoff = (struct GNUNET_HashCode *) & sm[1];
1070 options = SEARCH_MESSAGE_OPTION_NONE;
1071 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1072 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1073 if (GNUNET_FS_uri_test_ksk (sc->uri))
1075 msize = sizeof (struct SearchMessage);
1076 GNUNET_assert (size >= msize);
1077 mbc.keyword_offset = sc->keyword_offset;
1078 /* calculate total number of known results (in put_cnt => sqms) */
1080 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1081 &find_result_set, &mbc);
1083 /* calculate how many results we can send in this message */
1084 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1085 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1086 if (sc->search_request_map_offset < sqms)
1087 GNUNET_assert (mbc.put_cnt > 0);
1089 /* now build message */
1090 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1091 sm->header.size = htons (msize);
1092 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1093 sm->anonymity_level = htonl (sc->anonymity);
1094 memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
1095 sm->query = sc->requests[sc->keyword_offset].uquery;
1096 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1097 &build_result_set, &mbc);
1098 GNUNET_assert (sqms >= sc->search_request_map_offset);
1099 if (sqms != sc->search_request_map_offset)
1101 /* more requesting to be done... */
1102 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1103 schedule_transmit_search_request (sc);
1106 sm->options = htonl (options);
1107 sc->keyword_offset++;
1108 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1110 /* more requesting to be done... */
1111 schedule_transmit_search_request (sc);
1117 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1118 msize = sizeof (struct SearchMessage);
1119 GNUNET_assert (size >= msize);
1120 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1121 sm->anonymity_level = htonl (sc->anonymity);
1122 memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
1124 identifier = sc->uri->data.sks.identifier;
1125 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &id_hash);
1126 GNUNET_CRYPTO_hash (&sc->uri->data.sks.ns,
1127 sizeof (sc->uri->data.sks.ns), &ns_hash);
1128 GNUNET_CRYPTO_hash_xor (&id_hash, &ns_hash, &key);
1129 GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &signing_key);
1130 GNUNET_FS_pseudonym_derive_verification_key (&sc->uri->data.sks.ns,
1133 GNUNET_CRYPTO_hash (&verification_key,
1134 sizeof (verification_key),
1136 mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1137 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1138 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1139 mbc.keyword_offset = 0;
1140 if (sc->search_request_map_offset < sqms)
1141 GNUNET_assert (mbc.put_cnt > 0);
1142 msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1143 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1144 &build_result_set, &mbc);
1145 sm->header.size = htons (msize);
1146 GNUNET_assert (sqms >= sc->search_request_map_offset);
1147 if (sqms != sc->search_request_map_offset)
1149 /* more requesting to be done... */
1150 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1151 schedule_transmit_search_request (sc);
1154 sm->options = htonl (options);
1156 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1157 GNUNET_TIME_UNIT_FOREVER_REL);
1163 * Schedule the transmission of the (next) search request
1166 * @param sc context for the search
1169 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1175 size = sizeof (struct SearchMessage);
1177 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1178 sc->search_request_map_offset;
1179 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
1180 fit = GNUNET_MIN (fit, sqms);
1181 size += sizeof (struct GNUNET_HashCode) * fit;
1182 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1183 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1184 GNUNET_NO, &transmit_search_request, sc);
1190 * Reconnect to the FS service and transmit
1193 * @param cls our search context
1197 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1199 struct GNUNET_FS_SearchContext *sc = cls;
1200 struct GNUNET_CLIENT_Connection *client;
1202 sc->task = GNUNET_SCHEDULER_NO_TASK;
1203 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1209 sc->client = client;
1210 sc->search_request_map_offset = 0;
1211 sc->keyword_offset = 0;
1212 schedule_transmit_search_request (sc);
1217 * Shutdown any existing connection to the FS
1218 * service and try to establish a fresh one
1219 * (and then re-transmit our search request).
1221 * @param sc the search to reconnec
1224 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1226 if (NULL != sc->client)
1228 GNUNET_CLIENT_disconnect (sc->client);
1231 sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
1233 GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff,
1240 * Start search for content, internal API.
1242 * @param h handle to the file sharing subsystem
1243 * @param uri specifies the search parameters; can be
1244 * a KSK URI or an SKS URI.
1245 * @param anonymity desired level of anonymity
1246 * @param options options for the search
1247 * @param cctx initial value for the client context
1248 * @param psearch parent search result (for namespace update searches)
1249 * @return context that can be used to control the search
1251 static struct GNUNET_FS_SearchContext *
1252 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1253 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1254 void *cctx, struct GNUNET_FS_SearchResult *psearch)
1256 struct GNUNET_FS_SearchContext *sc;
1257 struct GNUNET_FS_ProgressInfo pi;
1259 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1261 sc->options = options;
1262 sc->uri = GNUNET_FS_uri_dup (uri);
1263 sc->anonymity = anonymity;
1264 sc->start_time = GNUNET_TIME_absolute_get ();
1265 if (NULL != psearch)
1267 sc->psearch_result = psearch;
1268 psearch->update_search = sc;
1270 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1271 sc->client_info = cctx;
1272 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1274 GNUNET_FS_uri_destroy (sc->uri);
1275 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1279 GNUNET_FS_search_sync_ (sc);
1280 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1281 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1287 * Build the request and actually initiate the search using the
1288 * GNUnet FS service.
1290 * @param sc search context
1291 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1294 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1297 const char *keyword;
1298 struct GNUNET_HashCode signing_key;
1299 struct GNUNET_FS_PseudonymHandle *ph;
1300 struct GNUNET_FS_PseudonymIdentifier anon;
1301 struct GNUNET_FS_PseudonymIdentifier verification_key;
1303 GNUNET_assert (NULL == sc->client);
1304 if (GNUNET_FS_uri_test_ksk (sc->uri))
1306 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1307 ph = GNUNET_FS_pseudonym_get_anonymous_pseudonym_handle ();
1308 GNUNET_FS_pseudonym_get_identifier (ph, &anon);
1309 GNUNET_FS_pseudonym_destroy (ph);
1311 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1312 sc->uri->data.ksk.keywordCount);
1313 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1315 keyword = &sc->uri->data.ksk.keywords[i][1];
1316 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].ukey);
1317 GNUNET_CRYPTO_hash (&sc->requests[i].ukey, sizeof (struct GNUNET_HashCode), &signing_key);
1318 GNUNET_FS_pseudonym_derive_verification_key (&anon,
1321 GNUNET_CRYPTO_hash (&verification_key, sizeof (struct GNUNET_FS_PseudonymIdentifier),
1322 &sc->requests[i].uquery);
1323 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1324 if (sc->requests[i].mandatory)
1325 sc->mandatory_count++;
1326 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
1329 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1330 if (NULL == sc->client)
1331 return GNUNET_SYSERR;
1332 schedule_transmit_search_request (sc);
1338 * Freeze probes for the given search result.
1340 * @param cls the global FS handle
1341 * @param key the key for the search result (unused)
1342 * @param value the search result to free
1346 search_result_freeze_probes (void *cls, const struct GNUNET_HashCode * key,
1349 struct GNUNET_FS_SearchResult *sr = value;
1351 if (NULL != sr->probe_ctx)
1353 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1354 sr->probe_ctx = NULL;
1356 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
1358 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
1359 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
1361 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1363 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1364 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1366 if (NULL != sr->update_search)
1367 GNUNET_FS_search_pause (sr->update_search);
1373 * Resume probes for the given search result.
1375 * @param cls the global FS handle
1376 * @param key the key for the search result (unused)
1377 * @param value the search result to free
1381 search_result_resume_probes (void *cls, const struct GNUNET_HashCode * key,
1384 struct GNUNET_FS_SearchResult *sr = value;
1386 GNUNET_FS_search_start_probe_ (sr);
1387 if (NULL != sr->update_search)
1388 GNUNET_FS_search_continue (sr->update_search);
1394 * Signal suspend and free the given search result.
1396 * @param cls the global FS handle
1397 * @param key the key for the search result (unused)
1398 * @param value the search result to free
1402 search_result_suspend (void *cls, const struct GNUNET_HashCode * key, void *value)
1404 struct GNUNET_FS_SearchContext *sc = cls;
1405 struct GNUNET_FS_SearchResult *sr = value;
1406 struct GNUNET_FS_ProgressInfo pi;
1408 if (NULL != sr->download)
1410 GNUNET_FS_download_signal_suspend_ (sr->download);
1411 sr->download = NULL;
1413 if (NULL != sr->probe_ctx)
1415 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1416 sr->probe_ctx = NULL;
1418 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
1420 GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
1421 sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
1423 if (NULL != sr->update_search)
1425 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1426 sr->update_search = NULL;
1428 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1429 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1430 pi.value.search.specifics.result_suspend.meta = sr->meta;
1431 pi.value.search.specifics.result_suspend.uri = sr->uri;
1432 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1433 GNUNET_break (NULL == sr->client_info);
1434 GNUNET_free_non_null (sr->serialization);
1435 GNUNET_FS_uri_destroy (sr->uri);
1436 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1437 if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1439 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1440 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1442 GNUNET_free_non_null (sr->keyword_bitmap);
1449 * Create SUSPEND event for the given search operation
1450 * and then clean up our state (without stop signal).
1452 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1455 GNUNET_FS_search_signal_suspend_ (void *cls)
1457 struct GNUNET_FS_SearchContext *sc = cls;
1458 struct GNUNET_FS_ProgressInfo pi;
1461 GNUNET_FS_end_top (sc->h, sc->top);
1462 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1463 &search_result_suspend, sc);
1464 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1465 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1466 GNUNET_break (NULL == sc->client_info);
1467 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1468 GNUNET_SCHEDULER_cancel (sc->task);
1469 if (NULL != sc->client)
1470 GNUNET_CLIENT_disconnect (sc->client);
1471 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1472 if (NULL != sc->requests)
1474 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1475 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1476 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1478 GNUNET_free_non_null (sc->requests);
1479 GNUNET_free_non_null (sc->emsg);
1480 GNUNET_FS_uri_destroy (sc->uri);
1481 GNUNET_free_non_null (sc->serialization);
1487 * Start search for content.
1489 * @param h handle to the file sharing subsystem
1490 * @param uri specifies the search parameters; can be
1491 * a KSK URI or an SKS URI.
1492 * @param anonymity desired level of anonymity
1493 * @param options options for the search
1494 * @param cctx initial value for the client context
1495 * @return context that can be used to control the search
1497 struct GNUNET_FS_SearchContext *
1498 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1499 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1500 enum GNUNET_FS_SearchOptions options, void *cctx)
1502 struct GNUNET_FS_SearchContext *ret;
1504 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1507 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1515 * @param sc context for the search that should be paused
1518 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1520 struct GNUNET_FS_ProgressInfo pi;
1522 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1523 GNUNET_SCHEDULER_cancel (sc->task);
1524 sc->task = GNUNET_SCHEDULER_NO_TASK;
1525 if (NULL != sc->client)
1526 GNUNET_CLIENT_disconnect (sc->client);
1528 GNUNET_FS_search_sync_ (sc);
1529 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1530 &search_result_freeze_probes, sc);
1531 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1532 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1537 * Continue paused search.
1539 * @param sc context for the search that should be resumed
1542 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1544 struct GNUNET_FS_ProgressInfo pi;
1546 GNUNET_assert (NULL == sc->client);
1547 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sc->task);
1548 do_reconnect (sc, NULL);
1549 GNUNET_FS_search_sync_ (sc);
1550 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1551 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1552 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1553 &search_result_resume_probes, sc);
1558 * Signal stop for the given search result.
1560 * @param cls the global FS handle
1561 * @param key the key for the search result (unused)
1562 * @param value the search result to free
1566 search_result_stop (void *cls, const struct GNUNET_HashCode * key, void *value)
1568 struct GNUNET_FS_SearchContext *sc = cls;
1569 struct GNUNET_FS_SearchResult *sr = value;
1570 struct GNUNET_FS_ProgressInfo pi;
1572 GNUNET_FS_search_stop_probe_ (sr);
1574 if (NULL != sr->download)
1576 sr->download->search = NULL;
1578 GNUNET_FS_make_top (sr->download->h,
1579 &GNUNET_FS_download_signal_suspend_, sr->download);
1580 if (NULL != sr->download->serialization)
1582 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1583 sr->download->serialization);
1584 GNUNET_free (sr->download->serialization);
1585 sr->download->serialization = NULL;
1587 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1588 GNUNET_FS_download_make_status_ (&pi, sr->download);
1589 GNUNET_FS_download_sync_ (sr->download);
1590 sr->download = NULL;
1592 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1593 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1594 pi.value.search.specifics.result_stopped.meta = sr->meta;
1595 pi.value.search.specifics.result_stopped.uri = sr->uri;
1596 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sc);
1602 * Free the given search result.
1604 * @param cls the global FS handle
1605 * @param key the key for the search result (unused)
1606 * @param value the search result to free
1610 search_result_free (void *cls, const struct GNUNET_HashCode * key, void *value)
1612 struct GNUNET_FS_SearchResult *sr = value;
1614 if (NULL != sr->update_search)
1616 GNUNET_FS_search_stop (sr->update_search);
1617 GNUNET_assert (NULL == sr->update_search);
1619 GNUNET_break (NULL == sr->probe_ctx);
1620 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task);
1621 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_ping_task);
1622 GNUNET_break (NULL == sr->client_info);
1623 GNUNET_free_non_null (sr->serialization);
1624 GNUNET_FS_uri_destroy (sr->uri);
1625 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1626 GNUNET_free_non_null (sr->keyword_bitmap);
1633 * Stop search for content.
1635 * @param sc context for the search that should be stopped
1638 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1640 struct GNUNET_FS_ProgressInfo pi;
1643 if (NULL != sc->top)
1644 GNUNET_FS_end_top (sc->h, sc->top);
1645 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1646 &search_result_stop, sc);
1647 if (NULL != sc->psearch_result)
1648 sc->psearch_result->update_search = NULL;
1649 if (NULL != sc->serialization)
1651 GNUNET_FS_remove_sync_file_ (sc->h,
1652 (sc->psearch_result !=
1653 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1654 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1656 GNUNET_FS_remove_sync_dir_ (sc->h,
1657 (sc->psearch_result !=
1658 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1659 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1661 GNUNET_free (sc->serialization);
1663 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1664 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1665 GNUNET_break (NULL == sc->client_info);
1666 if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1667 GNUNET_SCHEDULER_cancel (sc->task);
1668 if (NULL != sc->client)
1669 GNUNET_CLIENT_disconnect (sc->client);
1670 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1671 &search_result_free, sc);
1672 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1673 if (NULL != sc->requests)
1675 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1676 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1677 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1679 GNUNET_free_non_null (sc->requests);
1680 GNUNET_free_non_null (sc->emsg);
1681 GNUNET_FS_uri_destroy (sc->uri);
1685 /* end of fs_search.c */