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,
536 * Process a keyword-search result.
538 * @param sc our search context
539 * @param kb the kblock
540 * @param size size of kb
543 process_kblock (struct GNUNET_FS_SearchContext *sc,
544 const struct KBlock *kb,
550 char pt[size - sizeof (struct KBlock)];
551 struct GNUNET_CRYPTO_AesSessionKey skey;
552 struct GNUNET_CRYPTO_AesInitializationVector iv;
554 struct GNUNET_CONTAINER_MetaData *meta;
555 struct GNUNET_FS_Uri *uri;
558 GNUNET_CRYPTO_hash (&kb->keyspace,
559 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
562 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
564 &sc->requests[i].query,
565 sizeof (GNUNET_HashCode)))
567 if (i == sc->uri->data.ksk.keywordCount)
569 /* oops, does not match any of our keywords!? */
574 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
575 GNUNET_CRYPTO_aes_decrypt (&kb[1],
576 size - sizeof (struct KBlock),
581 eos = memchr (pt, 0, sizeof (pt));
588 if (sizeof (pt) == j)
589 meta = GNUNET_CONTAINER_meta_data_create ();
591 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
595 GNUNET_break_op (0); /* kblock malformed */
598 uri = GNUNET_FS_uri_parse (pt, &emsg);
601 GNUNET_break_op (0); /* kblock malformed */
602 GNUNET_free_non_null (emsg);
603 GNUNET_CONTAINER_meta_data_destroy (meta);
607 process_ksk_result (sc, &sc->requests[i], uri, meta);
610 GNUNET_CONTAINER_meta_data_destroy (meta);
611 GNUNET_FS_uri_destroy (uri);
616 * Process a keyword-search result with a namespace advertisment.
618 * @param sc our search context
619 * @param nb the nblock
620 * @param size size of nb
623 process_nblock (struct GNUNET_FS_SearchContext *sc,
624 const struct NBlock *nb,
630 char pt[size - sizeof (struct NBlock)];
631 struct GNUNET_CRYPTO_AesSessionKey skey;
632 struct GNUNET_CRYPTO_AesInitializationVector iv;
634 struct GNUNET_CONTAINER_MetaData *meta;
635 struct GNUNET_FS_Uri *uri;
638 GNUNET_CRYPTO_hash (&nb->keyspace,
639 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
642 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
644 &sc->requests[i].query,
645 sizeof (GNUNET_HashCode)))
647 if (i == sc->uri->data.ksk.keywordCount)
649 /* oops, does not match any of our keywords!? */
654 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
655 GNUNET_CRYPTO_aes_decrypt (&nb[1],
656 size - sizeof (struct NBlock),
661 eos = memchr (pt, 0, sizeof (pt));
668 if (sizeof (pt) == j)
669 meta = GNUNET_CONTAINER_meta_data_create ();
671 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
675 GNUNET_break_op (0); /* nblock malformed */
679 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
681 uri->data.sks.identifier = GNUNET_strdup (pt);
682 GNUNET_CRYPTO_hash (&nb->subspace,
683 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
684 &uri->data.sks.namespace);
685 uris = GNUNET_FS_uri_to_string (uri);
686 GNUNET_CONTAINER_meta_data_insert (meta,
688 EXTRACTOR_METATYPE_URI,
689 EXTRACTOR_METAFORMAT_UTF8,
694 GNUNET_PSEUDONYM_add (sc->h->cfg,
695 &uri->data.sks.namespace,
698 process_ksk_result (sc, &sc->requests[i], uri, meta);
701 GNUNET_CONTAINER_meta_data_destroy (meta);
702 GNUNET_FS_uri_destroy (uri);
707 * Process a namespace-search result.
709 * @param sc our search context
710 * @param sb the sblock
711 * @param size size of sb
714 process_sblock (struct GNUNET_FS_SearchContext *sc,
715 const struct SBlock *sb,
718 size_t len = size - sizeof (struct SBlock);
720 struct GNUNET_CRYPTO_AesSessionKey skey;
721 struct GNUNET_CRYPTO_AesInitializationVector iv;
722 struct GNUNET_FS_Uri *uri;
723 struct GNUNET_CONTAINER_MetaData *meta;
732 identifier = sc->uri->data.sks.identifier;
733 GNUNET_CRYPTO_hash (identifier,
736 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
737 GNUNET_CRYPTO_aes_decrypt (&sb[1],
743 off = GNUNET_STRINGS_buffer_tokenize (pt,
750 GNUNET_break_op (0); /* sblock malformed */
753 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off],
757 GNUNET_break_op (0); /* sblock malformed */
760 uri = GNUNET_FS_uri_parse (uris, &emsg);
763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
764 "Failed to parse URI `%s': %s\n",
766 GNUNET_break_op (0); /* sblock malformed */
767 GNUNET_free_non_null (emsg);
768 GNUNET_CONTAINER_meta_data_destroy (meta);
772 process_sks_result (sc, id, uri, meta);
774 GNUNET_FS_uri_destroy (uri);
775 GNUNET_CONTAINER_meta_data_destroy (meta);
780 * Process a search result.
782 * @param sc our search context
783 * @param type type of the result
784 * @param expiration when it will expire
785 * @param data the (encrypted) response
786 * @param size size of data
789 process_result (struct GNUNET_FS_SearchContext *sc,
790 enum GNUNET_BLOCK_Type type,
791 struct GNUNET_TIME_Absolute expiration,
795 if (GNUNET_TIME_absolute_get_duration (expiration).value > 0)
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Result received has already expired.\n");
799 return; /* result expired */
803 case GNUNET_BLOCK_TYPE_KBLOCK:
804 if (! GNUNET_FS_uri_test_ksk (sc->uri))
809 if (sizeof (struct KBlock) > size)
814 process_kblock (sc, data, size);
816 case GNUNET_BLOCK_TYPE_SBLOCK:
817 if (! GNUNET_FS_uri_test_sks (sc->uri))
822 if (sizeof (struct SBlock) > size)
827 process_sblock (sc, data, size);
829 case GNUNET_BLOCK_TYPE_NBLOCK:
830 if (! GNUNET_FS_uri_test_ksk (sc->uri))
835 if (sizeof (struct NBlock) > size)
840 process_nblock (sc, data, size);
842 case GNUNET_BLOCK_TYPE_ANY:
843 case GNUNET_BLOCK_TYPE_DBLOCK:
844 case GNUNET_BLOCK_TYPE_ONDEMAND:
845 case GNUNET_BLOCK_TYPE_IBLOCK:
849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
850 _("Got result with unknown block type `%d', ignoring"),
858 * Shutdown any existing connection to the FS
859 * service and try to establish a fresh one
860 * (and then re-transmit our search request).
862 * @param sc the search to reconnec
865 try_reconnect (struct GNUNET_FS_SearchContext *sc);
869 * Type of a function to call when we receive a message
873 * @param msg message received, NULL on timeout or fatal error
876 receive_results (void *cls,
877 const struct GNUNET_MessageHeader * msg)
879 struct GNUNET_FS_SearchContext *sc = cls;
880 const struct PutMessage *cm;
883 if ( (NULL == msg) ||
884 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
885 (ntohs (msg->size) <= sizeof (struct PutMessage)) )
890 msize = ntohs (msg->size);
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Receiving %u bytes of result from fs service\n",
894 cm = (const struct PutMessage*) msg;
897 GNUNET_TIME_absolute_ntoh (cm->expiration),
899 msize - sizeof (struct PutMessage));
900 /* continue receiving */
901 GNUNET_CLIENT_receive (sc->client,
904 GNUNET_TIME_UNIT_FOREVER_REL);
909 * We're ready to transmit the search request to the
910 * file-sharing service. Do it.
913 * @param size number of bytes available in buf
914 * @param buf where the callee should write the message
915 * @return number of bytes written to buf
918 transmit_search_request (void *cls,
922 struct GNUNET_FS_SearchContext *sc = cls;
924 struct SearchMessage *sm;
926 const char *identifier;
935 if (GNUNET_FS_uri_test_ksk (sc->uri))
937 msize = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
938 GNUNET_assert (size >= msize);
940 memset (sm, 0, msize);
941 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
943 sm[i].header.size = htons (sizeof (struct SearchMessage));
944 sm[i].header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
945 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
946 sm[i].options = htonl (1);
948 sm[i].options = htonl (0);
949 sm[i].type = htonl (GNUNET_BLOCK_TYPE_ANY);
950 sm[i].anonymity_level = htonl (sc->anonymity);
951 sm[i].query = sc->requests[i].query;
952 /* FIXME: should transmit hash codes of all already-known results here!
953 (and if they do not fit, add another message with the same
954 header and additional already-seen results!) */
959 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
960 msize = sizeof (struct SearchMessage);
961 GNUNET_assert (size >= msize);
963 memset (sm, 0, msize);
964 sm->header.size = htons (sizeof (struct SearchMessage));
965 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
966 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
967 sm->options = htonl (1);
969 sm->options = htonl (0);
970 sm->type = htonl (GNUNET_BLOCK_TYPE_SBLOCK);
971 sm->anonymity_level = htonl (sc->anonymity);
972 sm->target = sc->uri->data.sks.namespace;
973 identifier = sc->uri->data.sks.identifier;
974 GNUNET_CRYPTO_hash (identifier,
977 GNUNET_CRYPTO_hash (&key,
978 sizeof (GNUNET_HashCode),
980 GNUNET_CRYPTO_hash_xor (&idh,
983 /* FIXME: should transmit hash codes of all already-known results here!
984 (and if they do not fit, add another message with the same
985 header and additional already-seen results!) */
987 GNUNET_CLIENT_receive (sc->client,
990 GNUNET_TIME_UNIT_FOREVER_REL);
996 * Reconnect to the FS service and transmit
999 * @param cls our search context
1003 do_reconnect (void *cls,
1004 const struct GNUNET_SCHEDULER_TaskContext *tc)
1006 struct GNUNET_FS_SearchContext *sc = cls;
1007 struct GNUNET_CLIENT_Connection *client;
1010 sc->task = GNUNET_SCHEDULER_NO_TASK;
1011 client = GNUNET_CLIENT_connect (sc->h->sched,
1019 sc->client = client;
1020 if (GNUNET_FS_uri_test_ksk (sc->uri))
1021 size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1023 size = sizeof (struct SearchMessage);
1024 GNUNET_CLIENT_notify_transmit_ready (client,
1026 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1028 &transmit_search_request,
1034 * Shutdown any existing connection to the FS
1035 * service and try to establish a fresh one
1036 * (and then re-transmit our search request).
1038 * @param sc the search to reconnec
1041 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1043 if (NULL != sc->client)
1045 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1049 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
1050 GNUNET_TIME_UNIT_SECONDS,
1057 * Start search for content, internal API.
1059 * @param h handle to the file sharing subsystem
1060 * @param uri specifies the search parameters; can be
1061 * a KSK URI or an SKS URI.
1062 * @param anonymity desired level of anonymity
1063 * @param options options for the search
1064 * @param cctx initial value for the client context
1065 * @param psearch parent search result (for namespace update searches)
1066 * @return context that can be used to control the search
1068 static struct GNUNET_FS_SearchContext *
1069 search_start (struct GNUNET_FS_Handle *h,
1070 const struct GNUNET_FS_Uri *uri,
1072 enum GNUNET_FS_SearchOptions options,
1074 struct GNUNET_FS_SearchResult *psearch)
1076 struct GNUNET_FS_SearchContext *sc;
1077 struct GNUNET_FS_ProgressInfo pi;
1079 sc = GNUNET_malloc (sizeof(struct GNUNET_FS_SearchContext));
1081 sc->options = options;
1082 sc->uri = GNUNET_FS_uri_dup (uri);
1083 sc->anonymity = anonymity;
1084 sc->start_time = GNUNET_TIME_absolute_get ();
1085 if (psearch != NULL)
1087 sc->psearch_result = psearch;
1088 psearch->update_search = sc;
1090 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1091 sc->client_info = cctx;
1093 GNUNET_FS_search_start_searching_ (sc))
1095 GNUNET_FS_uri_destroy (sc->uri);
1096 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1100 GNUNET_FS_search_sync_ (sc);
1101 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1102 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1108 * Build the request and actually initiate the search using the
1109 * GNUnet FS service.
1111 * @param sc search context
1112 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1115 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1118 const char *keyword;
1120 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1121 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1124 GNUNET_assert (NULL == sc->client);
1125 if (GNUNET_FS_uri_test_ksk (sc->uri))
1127 size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1131 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1132 size = sizeof (struct SearchMessage);
1134 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1137 _("Too many keywords specified for a single search."));
1138 return GNUNET_SYSERR;
1140 if (GNUNET_FS_uri_test_ksk (sc->uri))
1142 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1143 sc->requests = GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1144 sc->uri->data.ksk.keywordCount);
1145 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1147 keyword = &sc->uri->data.ksk.keywords[i][1];
1148 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1149 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1150 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1151 GNUNET_CRYPTO_rsa_key_free (pk);
1152 GNUNET_CRYPTO_hash (&pub,
1153 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1154 &sc->requests[i].query);
1155 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1156 if (sc->requests[i].mandatory)
1157 sc->mandatory_count++;
1158 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1159 GNUNET_CRYPTO_hash (keyword,
1161 &sc->requests[i].key);
1164 sc->client = GNUNET_CLIENT_connect (sc->h->sched,
1167 if (NULL == sc->client)
1168 return GNUNET_SYSERR;
1169 GNUNET_CLIENT_notify_transmit_ready (sc->client,
1171 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1173 &transmit_search_request,
1180 * Freeze probes for the given search result.
1182 * @param cls the global FS handle
1183 * @param key the key for the search result (unused)
1184 * @param value the search result to free
1188 search_result_freeze_probes (void *cls,
1189 const GNUNET_HashCode * key,
1192 struct GNUNET_FS_SearchContext *sc = cls;
1193 struct GNUNET_FS_Handle *h = sc->h;
1194 struct GNUNET_FS_SearchResult *sr = value;
1196 if (sr->probe_ctx != NULL)
1198 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1199 sr->probe_ctx = NULL;
1201 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1203 GNUNET_SCHEDULER_cancel (h->sched,
1204 sr->probe_cancel_task);
1205 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1207 if (sr->update_search != NULL)
1208 GNUNET_FS_search_pause (sr->update_search);
1214 * Resume probes for the given search result.
1216 * @param cls the global FS handle
1217 * @param key the key for the search result (unused)
1218 * @param value the search result to free
1222 search_result_resume_probes (void *cls,
1223 const GNUNET_HashCode * key,
1226 struct GNUNET_FS_SearchResult *sr = value;
1228 GNUNET_FS_search_start_probe_ (sr);
1229 if (sr->update_search != NULL)
1230 GNUNET_FS_search_continue (sr->update_search);
1236 * Create SUSPEND event for the given search operation
1237 * and then clean up our state (without stop signal).
1239 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1242 search_signal_suspend (void *cls);
1246 * Signal suspend and free the given search result.
1248 * @param cls the global FS handle
1249 * @param key the key for the search result (unused)
1250 * @param value the search result to free
1254 search_result_suspend (void *cls,
1255 const GNUNET_HashCode * key,
1258 struct GNUNET_FS_SearchContext *sc = cls;
1259 struct GNUNET_FS_Handle *h = sc->h;
1260 struct GNUNET_FS_SearchResult *sr = value;
1261 struct GNUNET_FS_ProgressInfo pi;
1263 if (sr->download != NULL)
1264 GNUNET_FS_download_signal_suspend_ (sr->download);
1265 if (sr->update_search != NULL)
1266 search_signal_suspend (sr->update_search);
1267 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1268 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1269 pi.value.search.specifics.result_suspend.meta = sr->meta;
1270 pi.value.search.specifics.result_suspend.uri = sr->uri;
1271 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1272 GNUNET_break (NULL == sr->client_info);
1273 GNUNET_free_non_null (sr->serialization);
1274 GNUNET_FS_uri_destroy (sr->uri);
1275 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1276 if (sr->probe_ctx != NULL)
1277 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1278 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1279 GNUNET_SCHEDULER_cancel (h->sched,
1280 sr->probe_cancel_task);
1287 * Create SUSPEND event for the given search operation
1288 * and then clean up our state (without stop signal).
1290 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1293 search_signal_suspend (void *cls)
1295 struct GNUNET_FS_SearchContext *sc = cls;
1296 struct GNUNET_FS_ProgressInfo pi;
1299 GNUNET_FS_end_top (sc->h, sc->top);
1300 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1301 &search_result_suspend,
1303 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1304 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1305 GNUNET_break (NULL == sc->client_info);
1306 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1307 GNUNET_SCHEDULER_cancel (sc->h->sched,
1309 if (NULL != sc->client)
1310 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1311 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1312 if (sc->requests != NULL)
1314 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1315 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1316 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1318 GNUNET_free_non_null (sc->requests);
1319 GNUNET_free_non_null (sc->emsg);
1320 GNUNET_FS_uri_destroy (sc->uri);
1326 * Start search for content.
1328 * @param h handle to the file sharing subsystem
1329 * @param uri specifies the search parameters; can be
1330 * a KSK URI or an SKS URI.
1331 * @param anonymity desired level of anonymity
1332 * @param options options for the search
1333 * @param cctx initial value for the client context
1334 * @return context that can be used to control the search
1336 struct GNUNET_FS_SearchContext *
1337 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1338 const struct GNUNET_FS_Uri *uri,
1340 enum GNUNET_FS_SearchOptions options,
1343 struct GNUNET_FS_SearchContext *ret;
1344 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1345 ret->top = GNUNET_FS_make_top (h, &search_signal_suspend, ret);
1353 * @param sc context for the search that should be paused
1356 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1358 struct GNUNET_FS_ProgressInfo pi;
1360 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1361 GNUNET_SCHEDULER_cancel (sc->h->sched,
1363 sc->task = GNUNET_SCHEDULER_NO_TASK;
1364 if (NULL != sc->client)
1365 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1367 GNUNET_FS_search_sync_ (sc);
1368 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1369 &search_result_freeze_probes,
1371 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1372 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1377 * Continue paused search.
1379 * @param sc context for the search that should be resumed
1382 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1384 struct GNUNET_FS_ProgressInfo pi;
1386 GNUNET_assert (sc->client == NULL);
1387 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1388 do_reconnect (sc, NULL);
1389 GNUNET_FS_search_sync_ (sc);
1390 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1391 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1392 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1393 &search_result_resume_probes,
1399 * Free the given search result.
1401 * @param cls the global FS handle
1402 * @param key the key for the search result (unused)
1403 * @param value the search result to free
1407 search_result_free (void *cls,
1408 const GNUNET_HashCode * key,
1411 struct GNUNET_FS_SearchContext *sc = cls;
1412 struct GNUNET_FS_Handle *h = sc->h;
1413 struct GNUNET_FS_SearchResult *sr = value;
1414 struct GNUNET_FS_ProgressInfo pi;
1416 if (NULL != sr->download)
1418 sr->download->search = NULL;
1419 if (NULL != sr->download->serialization)
1421 GNUNET_FS_remove_sync_file_ (sc->h,
1422 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1423 sr->download->serialization);
1424 GNUNET_free (sr->download->serialization);
1425 sr->download->serialization = NULL;
1427 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1428 GNUNET_FS_download_make_status_ (&pi,
1430 GNUNET_FS_download_sync_ (sr->download);
1431 sr->download = NULL;
1433 if (NULL != sr->update_search)
1435 GNUNET_FS_search_stop (sr->update_search);
1436 GNUNET_assert (sr->update_search == NULL);
1438 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1439 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1440 pi.value.search.specifics.result_stopped.meta = sr->meta;
1441 pi.value.search.specifics.result_stopped.uri = sr->uri;
1442 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1443 GNUNET_break (NULL == sr->client_info);
1444 GNUNET_free_non_null (sr->serialization);
1445 GNUNET_FS_uri_destroy (sr->uri);
1446 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1447 if (sr->probe_ctx != NULL)
1448 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1449 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1450 GNUNET_SCHEDULER_cancel (h->sched,
1451 sr->probe_cancel_task);
1458 * Stop search for content.
1460 * @param sc context for the search that should be stopped
1463 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1465 struct GNUNET_FS_ProgressInfo pi;
1468 if (sc->top != NULL)
1469 GNUNET_FS_end_top (sc->h, sc->top);
1470 if (sc->psearch_result != NULL)
1471 sc->psearch_result->update_search = NULL;
1472 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1473 &search_result_free,
1475 if (sc->serialization != NULL)
1477 GNUNET_FS_remove_sync_file_ (sc->h,
1478 (sc->psearch_result != NULL)
1479 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1480 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1482 GNUNET_FS_remove_sync_dir_ (sc->h,
1483 (sc->psearch_result != NULL)
1484 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1485 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1488 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1489 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1490 GNUNET_break (NULL == sc->client_info);
1491 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1492 GNUNET_SCHEDULER_cancel (sc->h->sched,
1494 if (NULL != sc->client)
1495 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1496 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1497 if (sc->requests != NULL)
1499 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1500 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1501 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1503 GNUNET_free_non_null (sc->requests);
1504 GNUNET_free_non_null (sc->emsg);
1505 GNUNET_FS_uri_destroy (sc->uri);
1509 /* end of fs_search.c */