2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 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 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_search.c
23 * @brief Helper functions for searching.
24 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_protocols.h"
33 #define DEBUG_SEARCH GNUNET_NO
36 * Fill in all of the generic fields for a search event and
39 * @param pi structure to fill in
40 * @param sc overall search context
41 * @return value returned by the callback
44 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
45 struct GNUNET_FS_SearchContext *sc)
48 pi->value.search.sc = sc;
52 = (sc->psearch_result == NULL) ? NULL : sc->psearch_result->client_info;
53 pi->value.search.query
55 pi->value.search.duration = GNUNET_TIME_absolute_get_duration (sc->start_time);
56 pi->value.search.anonymity = sc->anonymity;
57 ret = sc->h->upcb (sc->h->upcb_cls,
64 * Check if the given result is identical
67 * @param cls points to the URI we check against
69 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
71 * @return GNUNET_SYSERR if the result is present,
75 test_result_present (void *cls,
76 const GNUNET_HashCode * key,
79 const struct GNUNET_FS_Uri *uri = cls;
80 struct GNUNET_FS_SearchResult *sr = value;
82 if (GNUNET_FS_uri_test_equal (uri,
90 * We've found a new CHK result. Let the client
93 * @param sc the search context
94 * @param sr the specific result
97 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
98 struct GNUNET_FS_SearchResult *sr)
100 struct GNUNET_FS_ProgressInfo pi;
102 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
103 pi.value.search.specifics.result.meta = sr->meta;
104 pi.value.search.specifics.result.uri = sr->uri;
105 pi.value.search.specifics.result.result = sr;
106 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
107 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
112 * We've found new information about an existing CHK result. Let the
113 * client know about it.
115 * @param sc the search context
116 * @param sr the specific result
119 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
120 struct GNUNET_FS_SearchResult *sr)
122 struct GNUNET_FS_ProgressInfo pi;
124 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
125 pi.value.search.specifics.update.cctx = sr->client_info;
126 pi.value.search.specifics.update.meta = sr->meta;
127 pi.value.search.specifics.update.uri = sr->uri;
128 pi.value.search.specifics.update.availability_rank
129 = 2*sr->availability_success - sr->availability_trials;
130 pi.value.search.specifics.update.availability_certainty
131 = sr->availability_trials;
132 pi.value.search.specifics.update.applicability_rank
133 = sr->optional_support;
134 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
139 * Context for "get_result_present".
141 struct GetResultContext
144 * The URI we're looking for.
146 const struct GNUNET_FS_Uri *uri;
149 * Where to store a pointer to the search
150 * result struct if we found a match.
152 struct GNUNET_FS_SearchResult *sr;
157 * Check if the given result is identical to the given URI and if so
160 * @param cls a "struct GetResultContext"
161 * @param key not used
162 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
163 * should compare with
167 get_result_present (void *cls,
168 const GNUNET_HashCode * key,
171 struct GetResultContext *grc = cls;
172 struct GNUNET_FS_SearchResult *sr = value;
174 if (GNUNET_FS_uri_test_equal (grc->uri,
182 * Signal result of last probe to client and then schedule next
186 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
188 struct GNUNET_FS_ProgressInfo pi;
190 pi.status = GNUNET_FS_STATUS_SEARCH_START;
191 pi.value.search.specifics.update.cctx = sr->client_info;
192 pi.value.search.specifics.update.meta = sr->meta;
193 pi.value.search.specifics.update.uri = sr->uri;
194 pi.value.search.specifics.update.availability_rank = sr->availability_success;
195 pi.value.search.specifics.update.availability_certainty = sr->availability_trials;
196 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
197 sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
198 GNUNET_FS_search_start_probe_ (sr);
203 * Handle the case where we have failed to receive a response for our probe.
205 * @param cls our 'struct GNUNET_FS_SearchResult*'
206 * @param tc scheduler context
209 probe_failure_handler (void *cls,
210 const struct GNUNET_SCHEDULER_TaskContext *tc)
212 struct GNUNET_FS_SearchResult *sr = cls;
213 sr->availability_trials++;
214 GNUNET_FS_search_result_sync_ (sr);
215 signal_probe_result (sr);
220 * Handle the case where we have gotten a response for our probe.
222 * @param cls our 'struct GNUNET_FS_SearchResult*'
223 * @param tc scheduler context
226 probe_success_handler (void *cls,
227 const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct GNUNET_FS_SearchResult *sr = cls;
230 sr->availability_trials++;
231 sr->availability_success++;
232 GNUNET_FS_search_result_sync_ (sr);
233 signal_probe_result (sr);
238 * Notification of FS that a search probe has made progress.
239 * This function is used INSTEAD of the client's event handler
240 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
242 * @param cls closure, always NULL (!), actual closure
243 * is in the client-context of the info struct
244 * @param info details about the event, specifying the event type
245 * and various bits about the event
246 * @return client-context (for the next progress call
247 * for this operation; should be set to NULL for
248 * SUSPEND and STOPPED events). The value returned
249 * will be passed to future callbacks in the respective
250 * field in the GNUNET_FS_ProgressInfo struct.
253 GNUNET_FS_search_probe_progress_ (void *cls,
254 const struct GNUNET_FS_ProgressInfo *info)
256 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
257 struct GNUNET_TIME_Relative dur;
259 switch (info->status)
261 case GNUNET_FS_STATUS_DOWNLOAD_START:
264 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
265 /* probes should never be resumed */
268 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
269 /* probes should never be suspended */
272 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
275 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
276 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
278 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
279 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
281 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
282 &probe_failure_handler,
285 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
286 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
288 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
289 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
291 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
292 &probe_success_handler,
295 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
296 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
298 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
299 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
303 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
304 GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
305 sr->probe_active_time = GNUNET_TIME_absolute_get ();
306 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
307 &probe_failure_handler,
310 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
311 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
313 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
314 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
316 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
317 sr->remaining_probe_time = GNUNET_TIME_relative_subtract (sr->remaining_probe_time,
319 GNUNET_FS_search_result_sync_ (sr);
330 * Start download probes for the given search result.
332 * @param sr the search result
335 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
340 if (sr->probe_ctx != NULL)
342 if (sr->download != NULL)
344 if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
346 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
348 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
351 if ( (len <= DBLOCK_SIZE) && (sr->availability_success > 0))
353 off = len / DBLOCK_SIZE;
355 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
357 if (len - off < DBLOCK_SIZE)
361 sr->remaining_probe_time = GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
362 2 * (1 + sr->availability_trials));
363 sr->probe_ctx = GNUNET_FS_download_start (sr->sc->h,
369 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
370 GNUNET_FS_DOWNLOAD_IS_PROBE,
376 * We have received a KSK result. Check how it fits in with the
377 * overall query and notify the client accordingly.
379 * @param sc context for the overall query
380 * @param ent entry for the specific keyword
381 * @param uri the URI that was found
382 * @param meta metadata associated with the URI
383 * under the "ent" keyword
386 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
387 struct SearchRequestEntry *ent,
388 const struct GNUNET_FS_Uri *uri,
389 const struct GNUNET_CONTAINER_MetaData *meta)
392 struct GNUNET_FS_SearchResult *sr;
393 struct GetResultContext grc;
397 GNUNET_FS_uri_to_key (uri, &key);
399 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
401 &test_result_present,
403 return; /* duplicate result */
404 /* try to find search result in master map */
407 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
412 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
415 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
417 sr->uri = GNUNET_FS_uri_dup (uri);
418 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
419 sr->mandatory_missing = sc->mandatory_count;
421 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
428 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
430 /* check if mandatory satisfied */
432 sr->mandatory_missing--;
434 sr->optional_support++;
435 if (0 != sr->mandatory_missing)
438 notify_client_chk_result (sc, sr);
440 notify_client_chk_update (sc, sr);
441 GNUNET_FS_search_result_sync_ (sr);
442 GNUNET_FS_search_start_probe_ (sr);
447 * Start search for content, internal API.
449 * @param h handle to the file sharing subsystem
450 * @param uri specifies the search parameters; can be
451 * a KSK URI or an SKS URI.
452 * @param anonymity desired level of anonymity
453 * @param options options for the search
454 * @param cctx client context
455 * @param psearch parent search result (for namespace update searches)
456 * @return context that can be used to control the search
458 static struct GNUNET_FS_SearchContext *
459 search_start (struct GNUNET_FS_Handle *h,
460 const struct GNUNET_FS_Uri *uri,
462 enum GNUNET_FS_SearchOptions options,
464 struct GNUNET_FS_SearchResult *psearch);
468 * We have received an SKS result. Start searching for updates and
469 * notify the client if it is a new result.
471 * @param sc context for the overall query
472 * @param id_update identifier for updates, NULL for none
473 * @param uri the URI that was found
474 * @param meta metadata associated with the URI
477 process_sks_result (struct GNUNET_FS_SearchContext *sc,
478 const char *id_update,
479 const struct GNUNET_FS_Uri *uri,
480 const struct GNUNET_CONTAINER_MetaData *meta)
482 struct GNUNET_FS_Uri uu;
484 struct GNUNET_FS_SearchResult *sr;
487 GNUNET_FS_uri_to_key (uri, &key);
488 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key,
489 &uri->data.chk.chk.query,
492 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
494 &test_result_present,
496 return; /* duplicate result */
497 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
499 sr->uri = GNUNET_FS_uri_dup (uri);
500 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
502 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
506 GNUNET_FS_search_result_sync_ (sr);
507 GNUNET_FS_search_start_probe_ (sr);
509 notify_client_chk_result (sc, sr);
510 /* search for updates */
511 if (strlen (id_update) == 0)
512 return; /* no updates */
514 uu.data.sks.namespace = sc->uri->data.sks.namespace;
515 uu.data.sks.identifier = GNUNET_strdup (id_update);
516 (void) search_start (sc->h,
522 GNUNET_free (uu.data.sks.identifier);
527 * Process a keyword-search result.
529 * @param sc our search context
530 * @param kb the kblock
531 * @param size size of kb
534 process_kblock (struct GNUNET_FS_SearchContext *sc,
535 const struct KBlock *kb,
541 char pt[size - sizeof (struct KBlock)];
542 struct GNUNET_CRYPTO_AesSessionKey skey;
543 struct GNUNET_CRYPTO_AesInitializationVector iv;
545 struct GNUNET_CONTAINER_MetaData *meta;
546 struct GNUNET_FS_Uri *uri;
549 GNUNET_CRYPTO_hash (&kb->keyspace,
550 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
553 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
555 &sc->requests[i].query,
556 sizeof (GNUNET_HashCode)))
558 if (i == sc->uri->data.ksk.keywordCount)
560 /* oops, does not match any of our keywords!? */
565 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
566 if (-1 == GNUNET_CRYPTO_aes_decrypt (&kb[1],
567 size - sizeof (struct KBlock),
576 eos = memchr (pt, 0, sizeof (pt));
583 if (sizeof (pt) == j)
584 meta = GNUNET_CONTAINER_meta_data_create ();
586 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
590 GNUNET_break_op (0); /* kblock malformed */
593 uri = GNUNET_FS_uri_parse (pt, &emsg);
596 GNUNET_break_op (0); /* kblock malformed */
597 GNUNET_free_non_null (emsg);
598 GNUNET_CONTAINER_meta_data_destroy (meta);
602 process_ksk_result (sc, &sc->requests[i], uri, meta);
605 GNUNET_CONTAINER_meta_data_destroy (meta);
606 GNUNET_FS_uri_destroy (uri);
611 * Process a keyword-search result with a namespace advertisment.
613 * @param sc our search context
614 * @param nb the nblock
615 * @param size size of nb
618 process_nblock (struct GNUNET_FS_SearchContext *sc,
619 const struct NBlock *nb,
625 char pt[size - sizeof (struct NBlock)];
626 struct GNUNET_CRYPTO_AesSessionKey skey;
627 struct GNUNET_CRYPTO_AesInitializationVector iv;
629 struct GNUNET_CONTAINER_MetaData *meta;
630 struct GNUNET_FS_Uri *uri;
633 GNUNET_CRYPTO_hash (&nb->keyspace,
634 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
637 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
639 &sc->requests[i].query,
640 sizeof (GNUNET_HashCode)))
642 if (i == sc->uri->data.ksk.keywordCount)
644 /* oops, does not match any of our keywords!? */
649 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
650 if (-1 == GNUNET_CRYPTO_aes_decrypt (&nb[1],
651 size - sizeof (struct NBlock),
660 eos = memchr (pt, 0, sizeof (pt));
667 if (sizeof (pt) == j)
668 meta = GNUNET_CONTAINER_meta_data_create ();
670 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
674 GNUNET_break_op (0); /* nblock malformed */
678 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
680 uri->data.sks.identifier = GNUNET_strdup (pt);
681 GNUNET_CRYPTO_hash (&nb->subspace,
682 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
683 &uri->data.sks.namespace);
684 uris = GNUNET_FS_uri_to_string (uri);
685 GNUNET_CONTAINER_meta_data_insert (meta,
687 EXTRACTOR_METATYPE_URI,
688 EXTRACTOR_METAFORMAT_UTF8,
693 GNUNET_PSEUDONYM_add (sc->h->cfg,
694 &uri->data.sks.namespace,
697 process_ksk_result (sc, &sc->requests[i], uri, meta);
700 GNUNET_CONTAINER_meta_data_destroy (meta);
701 GNUNET_FS_uri_destroy (uri);
706 * Process a namespace-search result.
708 * @param sc our search context
709 * @param sb the sblock
710 * @param size size of sb
713 process_sblock (struct GNUNET_FS_SearchContext *sc,
714 const struct SBlock *sb,
717 size_t len = size - sizeof (struct SBlock);
719 struct GNUNET_CRYPTO_AesSessionKey skey;
720 struct GNUNET_CRYPTO_AesInitializationVector iv;
721 struct GNUNET_FS_Uri *uri;
722 struct GNUNET_CONTAINER_MetaData *meta;
731 identifier = sc->uri->data.sks.identifier;
732 GNUNET_CRYPTO_hash (identifier,
735 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
736 if (-1 == GNUNET_CRYPTO_aes_decrypt (&sb[1],
746 off = GNUNET_STRINGS_buffer_tokenize (pt,
753 GNUNET_break_op (0); /* sblock malformed */
756 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off],
760 GNUNET_break_op (0); /* sblock malformed */
763 uri = GNUNET_FS_uri_parse (uris, &emsg);
766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
767 "Failed to parse URI `%s': %s\n",
769 GNUNET_break_op (0); /* sblock malformed */
770 GNUNET_free_non_null (emsg);
771 GNUNET_CONTAINER_meta_data_destroy (meta);
775 process_sks_result (sc, id, uri, meta);
777 GNUNET_FS_uri_destroy (uri);
778 GNUNET_CONTAINER_meta_data_destroy (meta);
783 * Process a search result.
785 * @param sc our search context
786 * @param type type of the result
787 * @param expiration when it will expire
788 * @param data the (encrypted) response
789 * @param size size of data
792 process_result (struct GNUNET_FS_SearchContext *sc,
793 enum GNUNET_BLOCK_Type type,
794 struct GNUNET_TIME_Absolute expiration,
798 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Result received has already expired.\n");
802 return; /* result expired */
806 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
807 if (! GNUNET_FS_uri_test_ksk (sc->uri))
812 if (sizeof (struct KBlock) > size)
817 process_kblock (sc, data, size);
819 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
820 if (! GNUNET_FS_uri_test_sks (sc->uri))
825 if (sizeof (struct SBlock) > size)
830 process_sblock (sc, data, size);
832 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
833 if (! GNUNET_FS_uri_test_ksk (sc->uri))
838 if (sizeof (struct NBlock) > size)
843 process_nblock (sc, data, size);
845 case GNUNET_BLOCK_TYPE_ANY:
848 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
851 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
854 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
859 _("Got result with unknown block type `%d', ignoring"),
867 * Shutdown any existing connection to the FS
868 * service and try to establish a fresh one
869 * (and then re-transmit our search request).
871 * @param sc the search to reconnec
874 try_reconnect (struct GNUNET_FS_SearchContext *sc);
878 * Type of a function to call when we receive a message
882 * @param msg message received, NULL on timeout or fatal error
885 receive_results (void *cls,
886 const struct GNUNET_MessageHeader * msg)
888 struct GNUNET_FS_SearchContext *sc = cls;
889 const struct PutMessage *cm;
892 if ( (NULL == msg) ||
893 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
894 (ntohs (msg->size) <= sizeof (struct PutMessage)) )
899 msize = ntohs (msg->size);
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Receiving %u bytes of result from fs service\n",
903 cm = (const struct PutMessage*) msg;
906 GNUNET_TIME_absolute_ntoh (cm->expiration),
908 msize - sizeof (struct PutMessage));
909 /* continue receiving */
910 GNUNET_CLIENT_receive (sc->client,
913 GNUNET_TIME_UNIT_FOREVER_REL);
918 * Schedule the transmission of the (next) search request
921 * @param sc context for the search
924 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
928 * Closure for 'build_result_set'.
930 struct MessageBuilderContext
933 * How many entries can we store to xoff.
935 unsigned int put_cnt;
938 * How many entries should we skip.
940 unsigned int skip_cnt;
943 * Where to store the keys.
945 GNUNET_HashCode *xoff;
948 * Search context we are iterating for.
950 struct GNUNET_FS_SearchContext *sc;
953 * URI the search result must match, NULL for any
955 struct GNUNET_FS_Uri *uri;
960 * Iterating over the known results, pick those
961 * matching the given result range and store
962 * their keys at 'xoff'.
964 * @param cls the 'struct MessageBuilderContext'
965 * @param key key for a result
966 * @param value the search result
967 * @return GNUNET_OK to continue iterating
970 build_result_set (void *cls,
971 const GNUNET_HashCode *key,
974 struct MessageBuilderContext *mbc = cls;
975 struct GNUNET_FS_SearchResult *sr = value;
977 if ( (mbc->uri != NULL) &&
978 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri,
981 if (mbc->skip_cnt > 0)
986 if (mbc->put_cnt == 0)
987 return GNUNET_SYSERR;
988 mbc->sc->search_request_map_offset++;
989 mbc->xoff[--mbc->put_cnt] = *key;
995 * Iterating over the known results, count those
996 * matching the given result range and increment
997 * put count for each.
999 * @param cls the 'struct MessageBuilderContext'
1000 * @param key key for a result
1001 * @param value the search result
1002 * @return GNUNET_OK to continue iterating
1005 find_result_set (void *cls,
1006 const GNUNET_HashCode *key,
1009 struct MessageBuilderContext *mbc = cls;
1010 struct GNUNET_FS_SearchResult *sr = value;
1012 if ( (mbc->uri != NULL) &&
1013 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri,
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,
1035 struct GNUNET_FS_SearchContext *sc = cls;
1036 struct MessageBuilderContext mbc;
1038 struct SearchMessage *sm;
1039 const char *identifier;
1040 GNUNET_HashCode key;
1041 GNUNET_HashCode idh;
1050 mbc.skip_cnt = sc->search_request_map_offset;
1052 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1053 mbc.xoff = (GNUNET_HashCode* ) &sm[1];
1054 if (GNUNET_FS_uri_test_ksk (sc->uri))
1056 msize = sizeof (struct SearchMessage);
1057 GNUNET_assert (size >= msize);
1060 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1064 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
1065 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt,
1066 sqms - mbc.skip_cnt);
1067 if (sc->search_request_map_offset < sqms)
1068 GNUNET_assert (mbc.put_cnt > 0);
1070 sm->header.size = htons (msize);
1071 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1072 sm->options = htonl (1);
1074 sm->options = htonl (0);
1075 sm->type = htonl (GNUNET_BLOCK_TYPE_ANY);
1076 sm->anonymity_level = htonl (sc->anonymity);
1077 memset (&sm->target, 0, sizeof (GNUNET_HashCode));
1078 sm->query = sc->requests[sc->keyword_offset].query;
1079 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1080 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1083 sm->header.size = htons (msize);
1084 if (sqms != sc->search_request_map_offset)
1086 /* more requesting to be done... */
1087 schedule_transmit_search_request (sc);
1090 sc->keyword_offset++;
1091 if (sc->uri->data.ksk.keywordCount !=
1094 /* more requesting to be done... */
1095 schedule_transmit_search_request (sc);
1101 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1102 msize = sizeof (struct SearchMessage);
1103 GNUNET_assert (size >= msize);
1104 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1105 sm->options = htonl (1);
1107 sm->options = htonl (0);
1108 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_SBLOCK);
1109 sm->anonymity_level = htonl (sc->anonymity);
1110 sm->target = sc->uri->data.sks.namespace;
1111 identifier = sc->uri->data.sks.identifier;
1112 GNUNET_CRYPTO_hash (identifier,
1113 strlen (identifier),
1115 GNUNET_CRYPTO_hash (&key,
1116 sizeof (GNUNET_HashCode),
1118 GNUNET_CRYPTO_hash_xor (&idh,
1121 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
1122 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1123 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt,
1124 sqms - mbc.skip_cnt);
1126 if (sc->search_request_map_offset < sqms)
1127 GNUNET_assert (mbc.put_cnt > 0);
1128 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1129 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1132 sm->header.size = htons (msize);
1133 if (sqms != sc->search_request_map_offset)
1135 /* more requesting to be done... */
1136 schedule_transmit_search_request (sc);
1140 GNUNET_CLIENT_receive (sc->client,
1143 GNUNET_TIME_UNIT_FOREVER_REL);
1149 * Schedule the transmission of the (next) search request
1152 * @param sc context for the search
1155 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1161 size = sizeof (struct SearchMessage);
1162 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) - sc->search_request_map_offset;
1163 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (GNUNET_HashCode);
1164 fit = GNUNET_MIN (fit, sqms);
1165 size += sizeof (GNUNET_HashCode) * fit;
1166 GNUNET_CLIENT_notify_transmit_ready (sc->client,
1168 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1170 &transmit_search_request,
1177 * Reconnect to the FS service and transmit
1180 * @param cls our search context
1184 do_reconnect (void *cls,
1185 const struct GNUNET_SCHEDULER_TaskContext *tc)
1187 struct GNUNET_FS_SearchContext *sc = cls;
1188 struct GNUNET_CLIENT_Connection *client;
1190 sc->task = GNUNET_SCHEDULER_NO_TASK;
1191 client = GNUNET_CLIENT_connect ("fs",
1198 sc->client = client;
1199 sc->search_request_map_offset = 0;
1200 sc->keyword_offset = 0;
1201 schedule_transmit_search_request (sc);
1206 * Shutdown any existing connection to the FS
1207 * service and try to establish a fresh one
1208 * (and then re-transmit our search request).
1210 * @param sc the search to reconnec
1213 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1215 if (NULL != sc->client)
1217 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1221 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1228 * Start search for content, internal API.
1230 * @param h handle to the file sharing subsystem
1231 * @param uri specifies the search parameters; can be
1232 * a KSK URI or an SKS URI.
1233 * @param anonymity desired level of anonymity
1234 * @param options options for the search
1235 * @param cctx initial value for the client context
1236 * @param psearch parent search result (for namespace update searches)
1237 * @return context that can be used to control the search
1239 static struct GNUNET_FS_SearchContext *
1240 search_start (struct GNUNET_FS_Handle *h,
1241 const struct GNUNET_FS_Uri *uri,
1243 enum GNUNET_FS_SearchOptions options,
1245 struct GNUNET_FS_SearchResult *psearch)
1247 struct GNUNET_FS_SearchContext *sc;
1248 struct GNUNET_FS_ProgressInfo pi;
1250 sc = GNUNET_malloc (sizeof(struct GNUNET_FS_SearchContext));
1252 sc->options = options;
1253 sc->uri = GNUNET_FS_uri_dup (uri);
1254 sc->anonymity = anonymity;
1255 sc->start_time = GNUNET_TIME_absolute_get ();
1256 if (psearch != NULL)
1258 sc->psearch_result = psearch;
1259 psearch->update_search = sc;
1261 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1262 sc->client_info = cctx;
1264 GNUNET_FS_search_start_searching_ (sc))
1266 GNUNET_FS_uri_destroy (sc->uri);
1267 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1271 GNUNET_FS_search_sync_ (sc);
1272 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1273 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1279 * Build the request and actually initiate the search using the
1280 * GNUnet FS service.
1282 * @param sc search context
1283 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1286 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1289 const char *keyword;
1291 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1292 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1294 GNUNET_assert (NULL == sc->client);
1295 if (GNUNET_FS_uri_test_ksk (sc->uri))
1297 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1298 sc->requests = GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1299 sc->uri->data.ksk.keywordCount);
1300 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1302 keyword = &sc->uri->data.ksk.keywords[i][1];
1303 GNUNET_CRYPTO_hash (keyword,
1306 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1307 GNUNET_assert (pk != NULL);
1308 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1309 GNUNET_CRYPTO_rsa_key_free (pk);
1310 GNUNET_CRYPTO_hash (&pub,
1311 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1312 &sc->requests[i].query);
1313 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1314 if (sc->requests[i].mandatory)
1315 sc->mandatory_count++;
1316 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1317 GNUNET_CRYPTO_hash (keyword,
1319 &sc->requests[i].key);
1322 sc->client = GNUNET_CLIENT_connect ("fs",
1324 if (NULL == sc->client)
1325 return GNUNET_SYSERR;
1326 schedule_transmit_search_request (sc);
1332 * Freeze probes for the given search result.
1334 * @param cls the global FS handle
1335 * @param key the key for the search result (unused)
1336 * @param value the search result to free
1340 search_result_freeze_probes (void *cls,
1341 const GNUNET_HashCode * key,
1344 struct GNUNET_FS_SearchResult *sr = value;
1346 if (sr->probe_ctx != NULL)
1348 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1349 sr->probe_ctx = NULL;
1351 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1353 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1354 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1356 if (sr->update_search != NULL)
1357 GNUNET_FS_search_pause (sr->update_search);
1363 * Resume probes for the given search result.
1365 * @param cls the global FS handle
1366 * @param key the key for the search result (unused)
1367 * @param value the search result to free
1371 search_result_resume_probes (void *cls,
1372 const GNUNET_HashCode * key,
1375 struct GNUNET_FS_SearchResult *sr = value;
1377 GNUNET_FS_search_start_probe_ (sr);
1378 if (sr->update_search != NULL)
1379 GNUNET_FS_search_continue (sr->update_search);
1385 * Signal suspend and free the given search result.
1387 * @param cls the global FS handle
1388 * @param key the key for the search result (unused)
1389 * @param value the search result to free
1393 search_result_suspend (void *cls,
1394 const GNUNET_HashCode * key,
1397 struct GNUNET_FS_SearchContext *sc = cls;
1398 struct GNUNET_FS_SearchResult *sr = value;
1399 struct GNUNET_FS_ProgressInfo pi;
1401 if (sr->download != NULL)
1402 GNUNET_FS_download_signal_suspend_ (sr->download);
1403 if (sr->update_search != NULL)
1404 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1405 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1406 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1407 pi.value.search.specifics.result_suspend.meta = sr->meta;
1408 pi.value.search.specifics.result_suspend.uri = sr->uri;
1409 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1410 GNUNET_break (NULL == sr->client_info);
1411 GNUNET_free_non_null (sr->serialization);
1412 GNUNET_FS_uri_destroy (sr->uri);
1413 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1414 if (sr->probe_ctx != NULL)
1415 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1416 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1417 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1424 * Create SUSPEND event for the given search operation
1425 * and then clean up our state (without stop signal).
1427 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1430 GNUNET_FS_search_signal_suspend_ (void *cls)
1432 struct GNUNET_FS_SearchContext *sc = cls;
1433 struct GNUNET_FS_ProgressInfo pi;
1436 GNUNET_FS_end_top (sc->h, sc->top);
1437 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1438 &search_result_suspend,
1440 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1441 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1442 GNUNET_break (NULL == sc->client_info);
1443 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1444 GNUNET_SCHEDULER_cancel (sc->task);
1445 if (NULL != sc->client)
1446 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1447 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1448 if (sc->requests != NULL)
1450 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1451 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1452 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
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,
1477 enum GNUNET_FS_SearchOptions options,
1480 struct GNUNET_FS_SearchContext *ret;
1481 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1484 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1492 * @param sc context for the search that should be paused
1495 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1497 struct GNUNET_FS_ProgressInfo pi;
1499 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1500 GNUNET_SCHEDULER_cancel (sc->task);
1501 sc->task = GNUNET_SCHEDULER_NO_TASK;
1502 if (NULL != sc->client)
1503 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1505 GNUNET_FS_search_sync_ (sc);
1506 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1507 &search_result_freeze_probes,
1509 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1510 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1515 * Continue paused search.
1517 * @param sc context for the search that should be resumed
1520 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1522 struct GNUNET_FS_ProgressInfo pi;
1524 GNUNET_assert (sc->client == NULL);
1525 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1526 do_reconnect (sc, NULL);
1527 GNUNET_FS_search_sync_ (sc);
1528 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1529 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1530 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1531 &search_result_resume_probes,
1537 * Free the given search result.
1539 * @param cls the global FS handle
1540 * @param key the key for the search result (unused)
1541 * @param value the search result to free
1545 search_result_free (void *cls,
1546 const GNUNET_HashCode * key,
1549 struct GNUNET_FS_SearchContext *sc = cls;
1550 struct GNUNET_FS_SearchResult *sr = value;
1551 struct GNUNET_FS_ProgressInfo pi;
1553 if (NULL != sr->download)
1555 sr->download->search = NULL;
1556 sr->download->top = GNUNET_FS_make_top (sr->download->h,
1557 &GNUNET_FS_download_signal_suspend_,
1559 if (NULL != sr->download->serialization)
1561 GNUNET_FS_remove_sync_file_ (sc->h,
1562 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1563 sr->download->serialization);
1564 GNUNET_free (sr->download->serialization);
1565 sr->download->serialization = NULL;
1567 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1568 GNUNET_FS_download_make_status_ (&pi,
1570 GNUNET_FS_download_sync_ (sr->download);
1571 sr->download = NULL;
1573 if (NULL != sr->update_search)
1575 GNUNET_FS_search_stop (sr->update_search);
1576 GNUNET_assert (sr->update_search == NULL);
1578 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1579 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1580 pi.value.search.specifics.result_stopped.meta = sr->meta;
1581 pi.value.search.specifics.result_stopped.uri = sr->uri;
1582 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1583 GNUNET_break (NULL == sr->client_info);
1584 GNUNET_free_non_null (sr->serialization);
1585 GNUNET_FS_uri_destroy (sr->uri);
1586 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1587 if (sr->probe_ctx != NULL)
1588 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1589 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1590 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1597 * Stop search for content.
1599 * @param sc context for the search that should be stopped
1602 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1604 struct GNUNET_FS_ProgressInfo pi;
1607 if (sc->top != NULL)
1608 GNUNET_FS_end_top (sc->h, sc->top);
1609 if (sc->psearch_result != NULL)
1610 sc->psearch_result->update_search = NULL;
1611 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1612 &search_result_free,
1614 if (sc->serialization != NULL)
1616 GNUNET_FS_remove_sync_file_ (sc->h,
1617 (sc->psearch_result != NULL)
1618 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1619 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1621 GNUNET_FS_remove_sync_dir_ (sc->h,
1622 (sc->psearch_result != NULL)
1623 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1624 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1626 GNUNET_free (sc->serialization);
1628 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1629 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1630 GNUNET_break (NULL == sc->client_info);
1631 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1632 GNUNET_SCHEDULER_cancel (sc->task);
1633 if (NULL != sc->client)
1634 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1635 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1636 if (sc->requests != NULL)
1638 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1639 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1640 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1642 GNUNET_free_non_null (sc->requests);
1643 GNUNET_free_non_null (sc->emsg);
1644 GNUNET_FS_uri_destroy (sc->uri);
1648 /* end of fs_search.c */