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 2, 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
27 * - add support for pushing "already seen" information
28 * to FS service for bloomfilter (can wait)
32 #include "gnunet_constants.h"
33 #include "gnunet_fs_service.h"
34 #include "gnunet_protocols.h"
37 #define DEBUG_SEARCH GNUNET_NO
40 * Fill in all of the generic fields for a search event and
43 * @param pi structure to fill in
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_SearchContext *sc)
52 pi->value.search.sc = sc;
56 = (sc->psearch_result == NULL) ? NULL : sc->psearch_result->client_info;
57 pi->value.search.query
59 pi->value.search.duration = GNUNET_TIME_absolute_get_duration (sc->start_time);
60 pi->value.search.anonymity = sc->anonymity;
61 ret = sc->h->upcb (sc->h->upcb_cls,
68 * Check if the given result is identical
71 * @param cls points to the URI we check against
73 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
75 * @return GNUNET_SYSERR if the result is present,
79 test_result_present (void *cls,
80 const GNUNET_HashCode * key,
83 const struct GNUNET_FS_Uri *uri = cls;
84 struct GNUNET_FS_SearchResult *sr = value;
86 if (GNUNET_FS_uri_test_equal (uri,
94 * We've found a new CHK result. Let the client
97 * @param sc the search context
98 * @param sr the specific result
101 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
102 struct GNUNET_FS_SearchResult *sr)
104 struct GNUNET_FS_ProgressInfo pi;
106 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
107 pi.value.search.specifics.result.meta = sr->meta;
108 pi.value.search.specifics.result.uri = sr->uri;
109 pi.value.search.specifics.result.result = sr;
110 sr->client_info = GNUNET_FS_search_make_status_ (&pi, 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
136 = sr->optional_support;
137 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
142 * Context for "get_result_present".
144 struct GetResultContext
147 * The URI we're looking for.
149 const struct GNUNET_FS_Uri *uri;
152 * Where to store a pointer to the search
153 * result struct if we found a match.
155 struct GNUNET_FS_SearchResult *sr;
160 * Check if the given result is identical to the given URI and if so
163 * @param cls a "struct GetResultContext"
164 * @param key not used
165 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
166 * should compare with
170 get_result_present (void *cls,
171 const GNUNET_HashCode * key,
174 struct GetResultContext *grc = cls;
175 struct GNUNET_FS_SearchResult *sr = value;
177 if (GNUNET_FS_uri_test_equal (grc->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_START;
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 = sr->availability_success;
198 pi.value.search.specifics.update.availability_certainty = sr->availability_trials;
199 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
200 sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
201 GNUNET_FS_search_start_probe_ (sr);
206 * Handle the case where we have failed to receive a response for our probe.
208 * @param cls our 'struct GNUNET_FS_SearchResult*'
209 * @param tc scheduler context
212 probe_failure_handler (void *cls,
213 const struct GNUNET_SCHEDULER_TaskContext *tc)
215 struct GNUNET_FS_SearchResult *sr = cls;
216 sr->availability_trials++;
217 GNUNET_FS_search_result_sync_ (sr);
218 signal_probe_result (sr);
223 * Handle the case where we have gotten a response for our probe.
225 * @param cls our 'struct GNUNET_FS_SearchResult*'
226 * @param tc scheduler context
229 probe_success_handler (void *cls,
230 const struct GNUNET_SCHEDULER_TaskContext *tc)
232 struct GNUNET_FS_SearchResult *sr = cls;
233 sr->availability_trials++;
234 sr->availability_success++;
235 GNUNET_FS_search_result_sync_ (sr);
236 signal_probe_result (sr);
241 * Notification of FS that a search probe has made progress.
242 * This function is used INSTEAD of the client's event handler
243 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
245 * @param cls closure, always NULL (!), actual closure
246 * is in the client-context of the info struct
247 * @param info details about the event, specifying the event type
248 * and various bits about the event
249 * @return client-context (for the next progress call
250 * for this operation; should be set to NULL for
251 * SUSPEND and STOPPED events). The value returned
252 * will be passed to future callbacks in the respective
253 * field in the GNUNET_FS_ProgressInfo struct.
256 GNUNET_FS_search_probe_progress_ (void *cls,
257 const struct GNUNET_FS_ProgressInfo *info)
259 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
260 struct GNUNET_TIME_Relative dur;
262 switch (info->status)
264 case GNUNET_FS_STATUS_DOWNLOAD_START:
267 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
268 /* probes should never be resumed */
271 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
272 /* probes should never be suspended */
275 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
278 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
279 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
281 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
282 sr->probe_cancel_task);
283 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
285 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
286 sr->remaining_probe_time,
287 &probe_failure_handler,
290 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
291 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
293 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
294 sr->probe_cancel_task);
295 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
297 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
298 sr->remaining_probe_time,
299 &probe_success_handler,
302 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
303 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
305 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
306 sr->probe_cancel_task);
307 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
311 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
312 GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
313 sr->probe_active_time = GNUNET_TIME_absolute_get ();
314 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
315 sr->remaining_probe_time,
316 &probe_failure_handler,
319 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
320 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
322 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
323 sr->probe_cancel_task);
324 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
326 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
327 sr->remaining_probe_time = GNUNET_TIME_relative_subtract (sr->remaining_probe_time,
329 GNUNET_FS_search_result_sync_ (sr);
340 * Start download probes for the given search result.
342 * @param sr the search result
345 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
350 if (sr->probe_ctx != NULL)
352 if (sr->download != NULL)
354 if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
356 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
358 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
361 if ( (len <= DBLOCK_SIZE) && (sr->availability_success > 0))
363 off = len / DBLOCK_SIZE;
365 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
367 if (len - off < DBLOCK_SIZE)
371 sr->remaining_probe_time = GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
372 2 * (1 + sr->availability_trials));
373 sr->probe_ctx = GNUNET_FS_download_start (sr->sc->h,
379 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
380 GNUNET_FS_DOWNLOAD_IS_PROBE,
386 * We have received a KSK result. Check how it fits in with the
387 * overall query and notify the client accordingly.
389 * @param sc context for the overall query
390 * @param ent entry for the specific keyword
391 * @param uri the URI that was found
392 * @param meta metadata associated with the URI
393 * under the "ent" keyword
396 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
397 struct SearchRequestEntry *ent,
398 const struct GNUNET_FS_Uri *uri,
399 const struct GNUNET_CONTAINER_MetaData *meta)
402 struct GNUNET_FS_SearchResult *sr;
403 struct GetResultContext grc;
407 GNUNET_FS_uri_to_key (uri, &key);
409 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
411 &test_result_present,
413 return; /* duplicate result */
414 /* try to find search result in master map */
417 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
422 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
425 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
427 sr->uri = GNUNET_FS_uri_dup (uri);
428 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
429 sr->mandatory_missing = sc->mandatory_count;
431 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
438 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
440 /* check if mandatory satisfied */
442 sr->mandatory_missing--;
444 sr->optional_support++;
445 if (0 != sr->mandatory_missing)
448 notify_client_chk_result (sc, sr);
450 notify_client_chk_update (sc, sr);
451 GNUNET_FS_search_result_sync_ (sr);
452 GNUNET_FS_search_start_probe_ (sr);
457 * Start search for content, internal API.
459 * @param h handle to the file sharing subsystem
460 * @param uri specifies the search parameters; can be
461 * a KSK URI or an SKS URI.
462 * @param anonymity desired level of anonymity
463 * @param options options for the search
464 * @param cctx client context
465 * @param psearch parent search result (for namespace update searches)
466 * @return context that can be used to control the search
468 static struct GNUNET_FS_SearchContext *
469 search_start (struct GNUNET_FS_Handle *h,
470 const struct GNUNET_FS_Uri *uri,
472 enum GNUNET_FS_SearchOptions options,
474 struct GNUNET_FS_SearchResult *psearch);
478 * We have received an SKS result. Start searching for updates and
479 * notify the client if it is a new result.
481 * @param sc context for the overall query
482 * @param id_update identifier for updates, NULL for none
483 * @param uri the URI that was found
484 * @param meta metadata associated with the URI
487 process_sks_result (struct GNUNET_FS_SearchContext *sc,
488 const char *id_update,
489 const struct GNUNET_FS_Uri *uri,
490 const struct GNUNET_CONTAINER_MetaData *meta)
492 struct GNUNET_FS_Uri uu;
494 struct GNUNET_FS_SearchResult *sr;
497 GNUNET_FS_uri_to_key (uri, &key);
498 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key,
499 &uri->data.chk.chk.query,
502 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
504 &test_result_present,
506 return; /* duplicate result */
507 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
509 sr->uri = GNUNET_FS_uri_dup (uri);
510 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
512 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
515 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
516 GNUNET_FS_search_result_sync_ (sr);
517 GNUNET_FS_search_start_probe_ (sr);
519 notify_client_chk_result (sc, sr);
520 /* search for updates */
521 if (strlen (id_update) == 0)
522 return; /* no updates */
524 uu.data.sks.namespace = sc->uri->data.sks.namespace;
525 uu.data.sks.identifier = GNUNET_strdup (id_update);
526 (void) search_start (sc->h,
532 GNUNET_free (uu.data.sks.identifier);
537 * Process a keyword-search result.
539 * @param sc our search context
540 * @param kb the kblock
541 * @param size size of kb
544 process_kblock (struct GNUNET_FS_SearchContext *sc,
545 const struct KBlock *kb,
551 char pt[size - sizeof (struct KBlock)];
552 struct GNUNET_CRYPTO_AesSessionKey skey;
553 struct GNUNET_CRYPTO_AesInitializationVector iv;
555 struct GNUNET_CONTAINER_MetaData *meta;
556 struct GNUNET_FS_Uri *uri;
559 GNUNET_CRYPTO_hash (&kb->keyspace,
560 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
563 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
565 &sc->requests[i].query,
566 sizeof (GNUNET_HashCode)))
568 if (i == sc->uri->data.ksk.keywordCount)
570 /* oops, does not match any of our keywords!? */
575 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
576 GNUNET_CRYPTO_aes_decrypt (&kb[1],
577 size - sizeof (struct KBlock),
582 eos = memchr (pt, 0, sizeof (pt));
589 if (sizeof (pt) == j)
590 meta = GNUNET_CONTAINER_meta_data_create ();
592 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
596 GNUNET_break_op (0); /* kblock malformed */
599 uri = GNUNET_FS_uri_parse (pt, &emsg);
602 GNUNET_break_op (0); /* kblock malformed */
603 GNUNET_free_non_null (emsg);
604 GNUNET_CONTAINER_meta_data_destroy (meta);
608 process_ksk_result (sc, &sc->requests[i], uri, meta);
611 GNUNET_CONTAINER_meta_data_destroy (meta);
612 GNUNET_FS_uri_destroy (uri);
617 * Process a keyword-search result with a namespace advertisment.
619 * @param sc our search context
620 * @param nb the nblock
621 * @param size size of nb
624 process_nblock (struct GNUNET_FS_SearchContext *sc,
625 const struct NBlock *nb,
631 char pt[size - sizeof (struct NBlock)];
632 struct GNUNET_CRYPTO_AesSessionKey skey;
633 struct GNUNET_CRYPTO_AesInitializationVector iv;
635 struct GNUNET_CONTAINER_MetaData *meta;
636 struct GNUNET_FS_Uri *uri;
639 GNUNET_CRYPTO_hash (&nb->keyspace,
640 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
643 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
645 &sc->requests[i].query,
646 sizeof (GNUNET_HashCode)))
648 if (i == sc->uri->data.ksk.keywordCount)
650 /* oops, does not match any of our keywords!? */
655 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
656 GNUNET_CRYPTO_aes_decrypt (&nb[1],
657 size - sizeof (struct NBlock),
662 eos = memchr (pt, 0, sizeof (pt));
669 if (sizeof (pt) == j)
670 meta = GNUNET_CONTAINER_meta_data_create ();
672 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
676 GNUNET_break_op (0); /* nblock malformed */
680 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
682 uri->data.sks.identifier = GNUNET_strdup (pt);
683 GNUNET_CRYPTO_hash (&nb->subspace,
684 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
685 &uri->data.sks.namespace);
686 uris = GNUNET_FS_uri_to_string (uri);
687 GNUNET_CONTAINER_meta_data_insert (meta,
689 EXTRACTOR_METATYPE_URI,
690 EXTRACTOR_METAFORMAT_UTF8,
695 GNUNET_PSEUDONYM_add (sc->h->cfg,
696 &uri->data.sks.namespace,
699 process_ksk_result (sc, &sc->requests[i], uri, meta);
702 GNUNET_CONTAINER_meta_data_destroy (meta);
703 GNUNET_FS_uri_destroy (uri);
708 * Process a namespace-search result.
710 * @param sc our search context
711 * @param sb the sblock
712 * @param size size of sb
715 process_sblock (struct GNUNET_FS_SearchContext *sc,
716 const struct SBlock *sb,
719 size_t len = size - sizeof (struct SBlock);
721 struct GNUNET_CRYPTO_AesSessionKey skey;
722 struct GNUNET_CRYPTO_AesInitializationVector iv;
723 struct GNUNET_FS_Uri *uri;
724 struct GNUNET_CONTAINER_MetaData *meta;
733 identifier = sc->uri->data.sks.identifier;
734 GNUNET_CRYPTO_hash (identifier,
737 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
738 GNUNET_CRYPTO_aes_decrypt (&sb[1],
744 off = GNUNET_STRINGS_buffer_tokenize (pt,
751 GNUNET_break_op (0); /* sblock malformed */
754 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off],
758 GNUNET_break_op (0); /* sblock malformed */
761 uri = GNUNET_FS_uri_parse (uris, &emsg);
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 "Failed to parse URI `%s': %s\n",
767 GNUNET_break_op (0); /* sblock malformed */
768 GNUNET_free_non_null (emsg);
769 GNUNET_CONTAINER_meta_data_destroy (meta);
773 process_sks_result (sc, id, uri, meta);
775 GNUNET_FS_uri_destroy (uri);
776 GNUNET_CONTAINER_meta_data_destroy (meta);
781 * Process a search result.
783 * @param sc our search context
784 * @param type type of the result
785 * @param expiration when it will expire
786 * @param data the (encrypted) response
787 * @param size size of data
790 process_result (struct GNUNET_FS_SearchContext *sc,
791 enum GNUNET_BLOCK_Type type,
792 struct GNUNET_TIME_Absolute expiration,
796 if (GNUNET_TIME_absolute_get_duration (expiration).value > 0)
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Result received has already expired.\n");
800 return; /* result expired */
804 case GNUNET_BLOCK_TYPE_KBLOCK:
805 if (! GNUNET_FS_uri_test_ksk (sc->uri))
810 if (sizeof (struct KBlock) > size)
815 process_kblock (sc, data, size);
817 case GNUNET_BLOCK_TYPE_SBLOCK:
818 if (! GNUNET_FS_uri_test_sks (sc->uri))
823 if (sizeof (struct SBlock) > size)
828 process_sblock (sc, data, size);
830 case GNUNET_BLOCK_TYPE_NBLOCK:
831 if (! GNUNET_FS_uri_test_ksk (sc->uri))
836 if (sizeof (struct NBlock) > size)
841 process_nblock (sc, data, size);
843 case GNUNET_BLOCK_TYPE_ANY:
844 case GNUNET_BLOCK_TYPE_DBLOCK:
845 case GNUNET_BLOCK_TYPE_ONDEMAND:
846 case GNUNET_BLOCK_TYPE_IBLOCK:
850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
851 _("Got result with unknown block type `%d', ignoring"),
859 * Shutdown any existing connection to the FS
860 * service and try to establish a fresh one
861 * (and then re-transmit our search request).
863 * @param sc the search to reconnec
866 try_reconnect (struct GNUNET_FS_SearchContext *sc);
870 * Type of a function to call when we receive a message
874 * @param msg message received, NULL on timeout or fatal error
877 receive_results (void *cls,
878 const struct GNUNET_MessageHeader * msg)
880 struct GNUNET_FS_SearchContext *sc = cls;
881 const struct PutMessage *cm;
884 if ( (NULL == msg) ||
885 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
886 (ntohs (msg->size) <= sizeof (struct PutMessage)) )
891 msize = ntohs (msg->size);
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893 "Receiving %u bytes of result from fs service\n",
895 cm = (const struct PutMessage*) msg;
898 GNUNET_TIME_absolute_ntoh (cm->expiration),
900 msize - sizeof (struct PutMessage));
901 /* continue receiving */
902 GNUNET_CLIENT_receive (sc->client,
905 GNUNET_TIME_UNIT_FOREVER_REL);
910 * We're ready to transmit the search request to the
911 * file-sharing service. Do it.
914 * @param size number of bytes available in buf
915 * @param buf where the callee should write the message
916 * @return number of bytes written to buf
919 transmit_search_request (void *cls,
923 struct GNUNET_FS_SearchContext *sc = cls;
925 struct SearchMessage *sm;
927 const char *identifier;
936 if (GNUNET_FS_uri_test_ksk (sc->uri))
938 msize = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
939 GNUNET_assert (size >= msize);
941 memset (sm, 0, msize);
942 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
944 sm[i].header.size = htons (sizeof (struct SearchMessage));
945 sm[i].header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
946 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
947 sm[i].options = htonl (1);
949 sm[i].options = htonl (0);
950 sm[i].type = htonl (GNUNET_BLOCK_TYPE_ANY);
951 sm[i].anonymity_level = htonl (sc->anonymity);
952 sm[i].query = sc->requests[i].query;
953 /* FIXME: should transmit hash codes of all already-known results here!
954 (and if they do not fit, add another message with the same
955 header and additional already-seen results!) */
960 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
961 msize = sizeof (struct SearchMessage);
962 GNUNET_assert (size >= msize);
964 memset (sm, 0, msize);
965 sm->header.size = htons (sizeof (struct SearchMessage));
966 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
967 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
968 sm->options = htonl (1);
970 sm->options = htonl (0);
971 sm->type = htonl (GNUNET_BLOCK_TYPE_SBLOCK);
972 sm->anonymity_level = htonl (sc->anonymity);
973 sm->target = sc->uri->data.sks.namespace;
974 identifier = sc->uri->data.sks.identifier;
975 GNUNET_CRYPTO_hash (identifier,
978 GNUNET_CRYPTO_hash (&key,
979 sizeof (GNUNET_HashCode),
981 GNUNET_CRYPTO_hash_xor (&idh,
984 /* FIXME: should transmit hash codes of all already-known results here!
985 (and if they do not fit, add another message with the same
986 header and additional already-seen results!) */
988 GNUNET_CLIENT_receive (sc->client,
991 GNUNET_TIME_UNIT_FOREVER_REL);
997 * Reconnect to the FS service and transmit
1000 * @param cls our search context
1004 do_reconnect (void *cls,
1005 const struct GNUNET_SCHEDULER_TaskContext *tc)
1007 struct GNUNET_FS_SearchContext *sc = cls;
1008 struct GNUNET_CLIENT_Connection *client;
1011 sc->task = GNUNET_SCHEDULER_NO_TASK;
1012 client = GNUNET_CLIENT_connect (sc->h->sched,
1020 sc->client = client;
1021 if (GNUNET_FS_uri_test_ksk (sc->uri))
1022 size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1024 size = sizeof (struct SearchMessage);
1025 GNUNET_CLIENT_notify_transmit_ready (client,
1027 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1029 &transmit_search_request,
1035 * Shutdown any existing connection to the FS
1036 * service and try to establish a fresh one
1037 * (and then re-transmit our search request).
1039 * @param sc the search to reconnec
1042 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1044 if (NULL != sc->client)
1046 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1050 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
1051 GNUNET_TIME_UNIT_SECONDS,
1058 * Start search for content, internal API.
1060 * @param h handle to the file sharing subsystem
1061 * @param uri specifies the search parameters; can be
1062 * a KSK URI or an SKS URI.
1063 * @param anonymity desired level of anonymity
1064 * @param options options for the search
1065 * @param cctx initial value for the client context
1066 * @param psearch parent search result (for namespace update searches)
1067 * @return context that can be used to control the search
1069 static struct GNUNET_FS_SearchContext *
1070 search_start (struct GNUNET_FS_Handle *h,
1071 const struct GNUNET_FS_Uri *uri,
1073 enum GNUNET_FS_SearchOptions options,
1075 struct GNUNET_FS_SearchResult *psearch)
1077 struct GNUNET_FS_SearchContext *sc;
1078 struct GNUNET_FS_ProgressInfo pi;
1080 sc = GNUNET_malloc (sizeof(struct GNUNET_FS_SearchContext));
1082 sc->options = options;
1083 sc->uri = GNUNET_FS_uri_dup (uri);
1084 sc->anonymity = anonymity;
1085 sc->start_time = GNUNET_TIME_absolute_get ();
1086 if (psearch != NULL)
1088 sc->psearch_result = psearch;
1089 psearch->update_search = sc;
1091 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1092 sc->client_info = cctx;
1094 GNUNET_FS_search_start_searching_ (sc))
1096 GNUNET_FS_uri_destroy (sc->uri);
1097 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1101 GNUNET_FS_search_sync_ (sc);
1102 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1103 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1109 * Build the request and actually initiate the search using the
1110 * GNUnet FS service.
1112 * @param sc search context
1113 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1116 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1119 const char *keyword;
1121 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1122 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1125 GNUNET_assert (NULL == sc->client);
1126 if (GNUNET_FS_uri_test_ksk (sc->uri))
1128 size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1132 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1133 size = sizeof (struct SearchMessage);
1135 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1138 _("Too many keywords specified for a single search."));
1139 return GNUNET_SYSERR;
1141 if (GNUNET_FS_uri_test_ksk (sc->uri))
1143 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1144 sc->requests = GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1145 sc->uri->data.ksk.keywordCount);
1146 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1148 keyword = &sc->uri->data.ksk.keywords[i][1];
1149 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1150 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1151 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1152 GNUNET_CRYPTO_rsa_key_free (pk);
1153 GNUNET_CRYPTO_hash (&pub,
1154 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1155 &sc->requests[i].query);
1156 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1157 if (sc->requests[i].mandatory)
1158 sc->mandatory_count++;
1159 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1160 GNUNET_CRYPTO_hash (keyword,
1162 &sc->requests[i].key);
1165 sc->client = GNUNET_CLIENT_connect (sc->h->sched,
1168 if (NULL == sc->client)
1169 return GNUNET_SYSERR;
1170 GNUNET_CLIENT_notify_transmit_ready (sc->client,
1172 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1174 &transmit_search_request,
1181 * Freeze probes for the given search result.
1183 * @param cls the global FS handle
1184 * @param key the key for the search result (unused)
1185 * @param value the search result to free
1189 search_result_freeze_probes (void *cls,
1190 const GNUNET_HashCode * key,
1193 struct GNUNET_FS_SearchContext *sc = cls;
1194 struct GNUNET_FS_Handle *h = sc->h;
1195 struct GNUNET_FS_SearchResult *sr = value;
1197 if (sr->probe_ctx != NULL)
1199 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1200 sr->probe_ctx = NULL;
1202 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1204 GNUNET_SCHEDULER_cancel (h->sched,
1205 sr->probe_cancel_task);
1206 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1208 if (sr->update_search != NULL)
1209 GNUNET_FS_search_pause (sr->update_search);
1215 * Resume probes for the given search result.
1217 * @param cls the global FS handle
1218 * @param key the key for the search result (unused)
1219 * @param value the search result to free
1223 search_result_resume_probes (void *cls,
1224 const GNUNET_HashCode * key,
1227 struct GNUNET_FS_SearchResult *sr = value;
1229 GNUNET_FS_search_start_probe_ (sr);
1230 if (sr->update_search != NULL)
1231 GNUNET_FS_search_continue (sr->update_search);
1237 * Signal suspend and free the given search result.
1239 * @param cls the global FS handle
1240 * @param key the key for the search result (unused)
1241 * @param value the search result to free
1245 search_result_suspend (void *cls,
1246 const GNUNET_HashCode * key,
1249 struct GNUNET_FS_SearchContext *sc = cls;
1250 struct GNUNET_FS_Handle *h = sc->h;
1251 struct GNUNET_FS_SearchResult *sr = value;
1252 struct GNUNET_FS_ProgressInfo pi;
1254 if (sr->download != NULL)
1255 GNUNET_FS_download_signal_suspend_ (sr->download);
1256 if (sr->update_search != NULL)
1257 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1258 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1259 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1260 pi.value.search.specifics.result_suspend.meta = sr->meta;
1261 pi.value.search.specifics.result_suspend.uri = sr->uri;
1262 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1263 GNUNET_break (NULL == sr->client_info);
1264 GNUNET_free_non_null (sr->serialization);
1265 GNUNET_FS_uri_destroy (sr->uri);
1266 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1267 if (sr->probe_ctx != NULL)
1268 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1269 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1270 GNUNET_SCHEDULER_cancel (h->sched,
1271 sr->probe_cancel_task);
1278 * Create SUSPEND event for the given search operation
1279 * and then clean up our state (without stop signal).
1281 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1284 GNUNET_FS_search_signal_suspend_ (void *cls)
1286 struct GNUNET_FS_SearchContext *sc = cls;
1287 struct GNUNET_FS_ProgressInfo pi;
1290 GNUNET_FS_end_top (sc->h, sc->top);
1291 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1292 &search_result_suspend,
1294 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1295 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1296 GNUNET_break (NULL == sc->client_info);
1297 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1298 GNUNET_SCHEDULER_cancel (sc->h->sched,
1300 if (NULL != sc->client)
1301 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1302 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1303 if (sc->requests != NULL)
1305 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1306 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1307 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1309 GNUNET_free_non_null (sc->requests);
1310 GNUNET_free_non_null (sc->emsg);
1311 GNUNET_FS_uri_destroy (sc->uri);
1312 GNUNET_free_non_null (sc->serialization);
1318 * Start search for content.
1320 * @param h handle to the file sharing subsystem
1321 * @param uri specifies the search parameters; can be
1322 * a KSK URI or an SKS URI.
1323 * @param anonymity desired level of anonymity
1324 * @param options options for the search
1325 * @param cctx initial value for the client context
1326 * @return context that can be used to control the search
1328 struct GNUNET_FS_SearchContext *
1329 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1330 const struct GNUNET_FS_Uri *uri,
1332 enum GNUNET_FS_SearchOptions options,
1335 struct GNUNET_FS_SearchContext *ret;
1336 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1337 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1345 * @param sc context for the search that should be paused
1348 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1350 struct GNUNET_FS_ProgressInfo pi;
1352 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1353 GNUNET_SCHEDULER_cancel (sc->h->sched,
1355 sc->task = GNUNET_SCHEDULER_NO_TASK;
1356 if (NULL != sc->client)
1357 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1359 GNUNET_FS_search_sync_ (sc);
1360 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1361 &search_result_freeze_probes,
1363 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1364 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1369 * Continue paused search.
1371 * @param sc context for the search that should be resumed
1374 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1376 struct GNUNET_FS_ProgressInfo pi;
1378 GNUNET_assert (sc->client == NULL);
1379 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1380 do_reconnect (sc, NULL);
1381 GNUNET_FS_search_sync_ (sc);
1382 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1383 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1384 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1385 &search_result_resume_probes,
1391 * Free the given search result.
1393 * @param cls the global FS handle
1394 * @param key the key for the search result (unused)
1395 * @param value the search result to free
1399 search_result_free (void *cls,
1400 const GNUNET_HashCode * key,
1403 struct GNUNET_FS_SearchContext *sc = cls;
1404 struct GNUNET_FS_Handle *h = sc->h;
1405 struct GNUNET_FS_SearchResult *sr = value;
1406 struct GNUNET_FS_ProgressInfo pi;
1408 if (NULL != sr->download)
1410 sr->download->search = NULL;
1411 if (NULL != sr->download->serialization)
1413 GNUNET_FS_remove_sync_file_ (sc->h,
1414 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1415 sr->download->serialization);
1416 GNUNET_free (sr->download->serialization);
1417 sr->download->serialization = NULL;
1419 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1420 GNUNET_FS_download_make_status_ (&pi,
1422 GNUNET_FS_download_sync_ (sr->download);
1423 sr->download = NULL;
1425 if (NULL != sr->update_search)
1427 GNUNET_FS_search_stop (sr->update_search);
1428 GNUNET_assert (sr->update_search == NULL);
1430 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1431 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1432 pi.value.search.specifics.result_stopped.meta = sr->meta;
1433 pi.value.search.specifics.result_stopped.uri = sr->uri;
1434 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1435 GNUNET_break (NULL == sr->client_info);
1436 GNUNET_free_non_null (sr->serialization);
1437 GNUNET_FS_uri_destroy (sr->uri);
1438 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1439 if (sr->probe_ctx != NULL)
1440 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1441 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1442 GNUNET_SCHEDULER_cancel (h->sched,
1443 sr->probe_cancel_task);
1450 * Stop search for content.
1452 * @param sc context for the search that should be stopped
1455 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1457 struct GNUNET_FS_ProgressInfo pi;
1460 if (sc->top != NULL)
1461 GNUNET_FS_end_top (sc->h, sc->top);
1462 if (sc->psearch_result != NULL)
1463 sc->psearch_result->update_search = NULL;
1464 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1465 &search_result_free,
1467 if (sc->serialization != NULL)
1469 GNUNET_FS_remove_sync_file_ (sc->h,
1470 (sc->psearch_result != NULL)
1471 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1472 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1474 GNUNET_FS_remove_sync_dir_ (sc->h,
1475 (sc->psearch_result != NULL)
1476 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1477 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1479 GNUNET_free (sc->serialization);
1481 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1482 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1483 GNUNET_break (NULL == sc->client_info);
1484 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1485 GNUNET_SCHEDULER_cancel (sc->h->sched,
1487 if (NULL != sc->client)
1488 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1489 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1490 if (sc->requests != NULL)
1492 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1493 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1494 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1496 GNUNET_free_non_null (sc->requests);
1497 GNUNET_free_non_null (sc->emsg);
1498 GNUNET_FS_uri_destroy (sc->uri);
1502 /* end of fs_search.c */