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)
29 * - handle availability probes (can wait)
30 * - make operations persistent (can wait)
34 #include "gnunet_constants.h"
35 #include "gnunet_fs_service.h"
36 #include "gnunet_protocols.h"
39 #define DEBUG_SEARCH GNUNET_NO
44 * Fill in all of the generic fields for
47 * @param pi structure to fill in
48 * @param sc overall search context
49 * @return value returned by the callback
52 make_search_status (struct GNUNET_FS_ProgressInfo *pi,
53 struct GNUNET_FS_SearchContext *sc)
55 pi->value.search.sc = sc;
59 = (sc->parent == NULL) ? NULL : sc->parent->client_info;
60 pi->value.search.query
62 pi->value.search.duration = GNUNET_TIME_absolute_get_duration (sc->start_time);
63 pi->value.search.anonymity = sc->anonymity;
64 return sc->h->upcb (sc->h->upcb_cls,
70 * Check if the given result is identical
73 * @param cls points to the URI we check against
75 * @param value a "struct SearchResult" who's URI we
77 * @return GNUNET_SYSERR if the result is present,
81 test_result_present (void *cls,
82 const GNUNET_HashCode * key,
85 const struct GNUNET_FS_Uri *uri = cls;
86 struct SearchResult *sr = value;
88 if (GNUNET_FS_uri_test_equal (uri,
96 * We've found a new CHK result. Let the client
99 * @param sc the search context
100 * @param sr the specific result
103 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
104 struct SearchResult *sr)
106 struct GNUNET_FS_ProgressInfo pi;
108 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
109 pi.value.search.specifics.result.meta = sr->meta;
110 pi.value.search.specifics.result.uri = sr->uri;
111 sr->client_info = make_search_status (&pi, sc);
116 * We've found new information about an existing CHK result. Let the
117 * client know about it.
119 * @param sc the search context
120 * @param sr the specific result
123 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
124 struct SearchResult *sr)
126 struct GNUNET_FS_ProgressInfo pi;
128 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
129 pi.value.search.specifics.update.cctx = sr->client_info;
130 pi.value.search.specifics.update.meta = sr->meta;
131 pi.value.search.specifics.update.uri = sr->uri;
132 pi.value.search.specifics.update.availability_rank
133 = 2*sr->availability_success - sr->availability_trials;
134 pi.value.search.specifics.update.availability_certainty
135 = sr->availability_trials;
136 pi.value.search.specifics.update.applicability_rank
137 = sr->optional_support;
138 sr->client_info = make_search_status (&pi, sc);
143 * Context for "get_result_present".
145 struct GetResultContext
148 * The URI we're looking for.
150 const struct GNUNET_FS_Uri *uri;
153 * Where to store a pointer to the search
154 * result struct if we found a match.
156 struct SearchResult *sr;
161 * Check if the given result is identical to the given URI and if so
164 * @param cls a "struct GetResultContext"
165 * @param key not used
166 * @param value a "struct SearchResult" who's URI we
167 * should compare with
171 get_result_present (void *cls,
172 const GNUNET_HashCode * key,
175 struct GetResultContext *grc = cls;
176 struct SearchResult *sr = value;
178 if (GNUNET_FS_uri_test_equal (grc->uri,
186 * Start download probes for the given search result.
188 * @param sr the search result
191 start_probe (struct SearchResult *sr);
195 * Signal result of last probe to client and then schedule next
199 signal_probe_result (struct SearchResult *sr)
201 struct GNUNET_FS_ProgressInfo pi;
203 pi.status = GNUNET_FS_STATUS_SEARCH_START;
204 pi.value.search.specifics.update.cctx = sr->client_info;
205 pi.value.search.specifics.update.meta = sr->meta;
206 pi.value.search.specifics.update.uri = sr->uri;
207 pi.value.search.specifics.update.availability_rank = sr->availability_success;
208 pi.value.search.specifics.update.availability_certainty = sr->availability_trials;
209 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
210 sr->sc->client_info = make_search_status (&pi, sr->sc);
216 * Handle the case where we have failed to receive a response for our probe.
218 * @param cls our 'struct SearchResult*'
219 * @param tc scheduler context
222 probe_failure_handler (void *cls,
223 const struct GNUNET_SCHEDULER_TaskContext *tc)
225 struct SearchResult *sr = cls;
226 sr->availability_trials++;
227 signal_probe_result (sr);
232 * Handle the case where we have gotten a response for our probe.
234 * @param cls our 'struct SearchResult*'
235 * @param tc scheduler context
238 probe_success_handler (void *cls,
239 const struct GNUNET_SCHEDULER_TaskContext *tc)
241 struct SearchResult *sr = cls;
242 sr->availability_trials++;
243 sr->availability_success++;
244 signal_probe_result (sr);
249 * Notification of FS that a search probe has made progress.
250 * This function is used INSTEAD of the client's event handler
251 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
253 * @param cls closure, always NULL (!), actual closure
254 * is in the client-context of the info struct
255 * @param info details about the event, specifying the event type
256 * and various bits about the event
257 * @return client-context (for the next progress call
258 * for this operation; should be set to NULL for
259 * SUSPEND and STOPPED events). The value returned
260 * will be passed to future callbacks in the respective
261 * field in the GNUNET_FS_ProgressInfo struct.
264 GNUNET_FS_search_probe_progress_ (void *cls,
265 const struct GNUNET_FS_ProgressInfo *info)
267 struct SearchResult *sr = info->value.download.cctx;
268 struct GNUNET_TIME_Relative dur;
270 switch (info->status)
272 case GNUNET_FS_STATUS_DOWNLOAD_START:
275 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
276 /* probes should never be resumed */
279 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
280 /* probes should never be suspended */
283 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
286 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
287 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
289 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
290 sr->probe_cancel_task);
291 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
293 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
294 sr->remaining_probe_time,
295 &probe_failure_handler,
298 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
299 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
301 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
302 sr->probe_cancel_task);
303 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
305 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
306 sr->remaining_probe_time,
307 &probe_success_handler,
310 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
311 /* FIXME: clean up? schedule next probe? or already done? */
314 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
315 GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
316 sr->probe_active_time = GNUNET_TIME_absolute_get ();
317 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
318 sr->remaining_probe_time,
319 &probe_failure_handler,
322 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
323 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
325 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
326 sr->probe_cancel_task);
327 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
329 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
330 sr->remaining_probe_time = GNUNET_TIME_relative_subtract (sr->remaining_probe_time,
342 * Start download probes for the given search result.
344 * @param sr the search result
347 start_probe (struct SearchResult *sr)
352 if (sr->probe_ctx != 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 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 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;
430 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
433 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
437 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
439 /* check if mandatory satisfied */
441 sr->mandatory_missing--;
443 sr->optional_support++;
444 if (0 != sr->mandatory_missing)
447 notify_client_chk_result (sc, sr);
449 notify_client_chk_update (sc, sr);
455 * Start search for content, internal API.
457 * @param h handle to the file sharing subsystem
458 * @param uri specifies the search parameters; can be
459 * a KSK URI or an SKS URI.
460 * @param anonymity desired level of anonymity
461 * @param options options for the search
462 * @param cctx client context
463 * @param parent parent search (for namespace update searches)
464 * @return context that can be used to control the search
466 static struct GNUNET_FS_SearchContext *
467 search_start (struct GNUNET_FS_Handle *h,
468 const struct GNUNET_FS_Uri *uri,
470 enum GNUNET_FS_SearchOptions options,
472 struct GNUNET_FS_SearchContext *parent);
476 * We have received an SKS result. Start searching for updates and
477 * notify the client if it is a new result.
479 * @param sc context for the overall query
480 * @param id_update identifier for updates, NULL for none
481 * @param uri the URI that was found
482 * @param meta metadata associated with the URI
485 process_sks_result (struct GNUNET_FS_SearchContext *sc,
486 const char *id_update,
487 const struct GNUNET_FS_Uri *uri,
488 const struct GNUNET_CONTAINER_MetaData *meta)
490 struct GNUNET_FS_Uri uu;
492 struct SearchResult *sr;
495 GNUNET_FS_uri_to_key (uri, &key);
496 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key,
497 &uri->data.chk.chk.query,
500 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
502 &test_result_present,
504 return; /* duplicate result */
505 sr = GNUNET_malloc (sizeof (struct SearchResult));
507 sr->uri = GNUNET_FS_uri_dup (uri);
508 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
509 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
512 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
515 notify_client_chk_result (sc, sr);
516 /* search for updates */
517 if (strlen (id_update) == 0)
518 return; /* no updates */
520 uu.data.sks.namespace = sc->uri->data.sks.namespace;
521 uu.data.sks.identifier = GNUNET_strdup (id_update);
522 /* FIXME: should attach update search
523 to the individual result, not
524 the entire SKS search! */
535 * Process a keyword-search result.
537 * @param sc our search context
538 * @param kb the kblock
539 * @param size size of kb
542 process_kblock (struct GNUNET_FS_SearchContext *sc,
543 const struct KBlock *kb,
549 char pt[size - sizeof (struct KBlock)];
550 struct GNUNET_CRYPTO_AesSessionKey skey;
551 struct GNUNET_CRYPTO_AesInitializationVector iv;
553 struct GNUNET_CONTAINER_MetaData *meta;
554 struct GNUNET_FS_Uri *uri;
557 GNUNET_CRYPTO_hash (&kb->keyspace,
558 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
561 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
563 &sc->requests[i].query,
564 sizeof (GNUNET_HashCode)))
566 if (i == sc->uri->data.ksk.keywordCount)
568 /* oops, does not match any of our keywords!? */
573 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
574 GNUNET_CRYPTO_aes_decrypt (&kb[1],
575 size - sizeof (struct KBlock),
580 eos = memchr (pt, 0, sizeof (pt));
587 if (sizeof (pt) == j)
588 meta = GNUNET_CONTAINER_meta_data_create ();
590 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
594 GNUNET_break_op (0); /* kblock malformed */
597 uri = GNUNET_FS_uri_parse (pt, &emsg);
600 GNUNET_break_op (0); /* kblock malformed */
601 GNUNET_free_non_null (emsg);
602 GNUNET_CONTAINER_meta_data_destroy (meta);
606 process_ksk_result (sc, &sc->requests[i], uri, meta);
609 GNUNET_CONTAINER_meta_data_destroy (meta);
610 GNUNET_FS_uri_destroy (uri);
615 * Process a keyword-search result with a namespace advertisment.
617 * @param sc our search context
618 * @param nb the nblock
619 * @param size size of nb
622 process_nblock (struct GNUNET_FS_SearchContext *sc,
623 const struct NBlock *nb,
629 char pt[size - sizeof (struct NBlock)];
630 struct GNUNET_CRYPTO_AesSessionKey skey;
631 struct GNUNET_CRYPTO_AesInitializationVector iv;
633 struct GNUNET_CONTAINER_MetaData *meta;
634 struct GNUNET_FS_Uri *uri;
637 GNUNET_CRYPTO_hash (&nb->keyspace,
638 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
641 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
643 &sc->requests[i].query,
644 sizeof (GNUNET_HashCode)))
646 if (i == sc->uri->data.ksk.keywordCount)
648 /* oops, does not match any of our keywords!? */
653 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
654 GNUNET_CRYPTO_aes_decrypt (&nb[1],
655 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 GNUNET_CRYPTO_aes_decrypt (&sb[1],
742 off = GNUNET_STRINGS_buffer_tokenize (pt,
749 GNUNET_break_op (0); /* sblock malformed */
752 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off],
756 GNUNET_break_op (0); /* sblock malformed */
759 uri = GNUNET_FS_uri_parse (uris, &emsg);
762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
763 "Failed to parse URI `%s': %s\n",
765 GNUNET_break_op (0); /* sblock malformed */
766 GNUNET_free_non_null (emsg);
767 GNUNET_CONTAINER_meta_data_destroy (meta);
771 process_sks_result (sc, id, uri, meta);
773 GNUNET_FS_uri_destroy (uri);
774 GNUNET_CONTAINER_meta_data_destroy (meta);
779 * Process a search result.
781 * @param sc our search context
782 * @param type type of the result
783 * @param expiration when it will expire
784 * @param data the (encrypted) response
785 * @param size size of data
788 process_result (struct GNUNET_FS_SearchContext *sc,
789 enum GNUNET_BLOCK_Type type,
790 struct GNUNET_TIME_Absolute expiration,
794 if (GNUNET_TIME_absolute_get_duration (expiration).value > 0)
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Result received has already expired.\n");
798 return; /* result expired */
802 case GNUNET_BLOCK_TYPE_KBLOCK:
803 if (! GNUNET_FS_uri_test_ksk (sc->uri))
808 if (sizeof (struct KBlock) > size)
813 process_kblock (sc, data, size);
815 case GNUNET_BLOCK_TYPE_SBLOCK:
816 if (! GNUNET_FS_uri_test_sks (sc->uri))
821 if (sizeof (struct SBlock) > size)
826 process_sblock (sc, data, size);
828 case GNUNET_BLOCK_TYPE_NBLOCK:
829 if (! GNUNET_FS_uri_test_ksk (sc->uri))
834 if (sizeof (struct NBlock) > size)
839 process_nblock (sc, data, size);
841 case GNUNET_BLOCK_TYPE_ANY:
842 case GNUNET_BLOCK_TYPE_DBLOCK:
843 case GNUNET_BLOCK_TYPE_ONDEMAND:
844 case GNUNET_BLOCK_TYPE_IBLOCK:
848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
849 _("Got result with unknown block type `%d', ignoring"),
857 * Shutdown any existing connection to the FS
858 * service and try to establish a fresh one
859 * (and then re-transmit our search request).
861 * @param sc the search to reconnec
864 try_reconnect (struct GNUNET_FS_SearchContext *sc);
868 * Type of a function to call when we receive a message
872 * @param msg message received, NULL on timeout or fatal error
875 receive_results (void *cls,
876 const struct GNUNET_MessageHeader * msg)
878 struct GNUNET_FS_SearchContext *sc = cls;
879 const struct PutMessage *cm;
882 if ( (NULL == msg) ||
883 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
884 (ntohs (msg->size) <= sizeof (struct PutMessage)) )
889 msize = ntohs (msg->size);
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Receiving %u bytes of result from fs service\n",
893 cm = (const struct PutMessage*) msg;
896 GNUNET_TIME_absolute_ntoh (cm->expiration),
898 msize - sizeof (struct PutMessage));
899 /* continue receiving */
900 GNUNET_CLIENT_receive (sc->client,
903 GNUNET_TIME_UNIT_FOREVER_REL);
908 * We're ready to transmit the search request to the
909 * file-sharing service. Do it.
912 * @param size number of bytes available in buf
913 * @param buf where the callee should write the message
914 * @return number of bytes written to buf
917 transmit_search_request (void *cls,
921 struct GNUNET_FS_SearchContext *sc = cls;
923 struct SearchMessage *sm;
925 const char *identifier;
934 if (GNUNET_FS_uri_test_ksk (sc->uri))
936 msize = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
937 GNUNET_assert (size >= msize);
939 memset (sm, 0, msize);
940 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
942 sm[i].header.size = htons (sizeof (struct SearchMessage));
943 sm[i].header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
944 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
945 sm[i].options = htonl (1);
947 sm[i].options = htonl (0);
948 sm[i].type = htonl (GNUNET_BLOCK_TYPE_ANY);
949 sm[i].anonymity_level = htonl (sc->anonymity);
950 sm[i].query = sc->requests[i].query;
955 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
956 msize = sizeof (struct SearchMessage);
957 GNUNET_assert (size >= msize);
959 memset (sm, 0, msize);
960 sm->header.size = htons (sizeof (struct SearchMessage));
961 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
962 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
963 sm->options = htonl (1);
965 sm->options = htonl (0);
966 sm->type = htonl (GNUNET_BLOCK_TYPE_SBLOCK);
967 sm->anonymity_level = htonl (sc->anonymity);
968 sm->target = sc->uri->data.sks.namespace;
969 identifier = sc->uri->data.sks.identifier;
970 GNUNET_CRYPTO_hash (identifier,
973 GNUNET_CRYPTO_hash (&key,
974 sizeof (GNUNET_HashCode),
976 GNUNET_CRYPTO_hash_xor (&idh,
980 GNUNET_CLIENT_receive (sc->client,
983 GNUNET_TIME_UNIT_FOREVER_REL);
989 * Reconnect to the FS service and transmit
992 * @param cls our search context
996 do_reconnect (void *cls,
997 const struct GNUNET_SCHEDULER_TaskContext *tc)
999 struct GNUNET_FS_SearchContext *sc = cls;
1000 struct GNUNET_CLIENT_Connection *client;
1003 sc->task = GNUNET_SCHEDULER_NO_TASK;
1004 client = GNUNET_CLIENT_connect (sc->h->sched,
1012 sc->client = client;
1013 if (GNUNET_FS_uri_test_ksk (sc->uri))
1014 size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1016 size = sizeof (struct SearchMessage);
1017 GNUNET_CLIENT_notify_transmit_ready (client,
1019 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1021 &transmit_search_request,
1027 * Shutdown any existing connection to the FS
1028 * service and try to establish a fresh one
1029 * (and then re-transmit our search request).
1031 * @param sc the search to reconnec
1034 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1036 if (NULL != sc->client)
1038 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1042 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
1043 GNUNET_TIME_UNIT_SECONDS,
1050 * Start search for content, internal API.
1052 * @param h handle to the file sharing subsystem
1053 * @param uri specifies the search parameters; can be
1054 * a KSK URI or an SKS URI.
1055 * @param anonymity desired level of anonymity
1056 * @param options options for the search
1057 * @param cctx initial value for the client context
1058 * @param parent parent search (for namespace update searches)
1059 * @return context that can be used to control the search
1061 static struct GNUNET_FS_SearchContext *
1062 search_start (struct GNUNET_FS_Handle *h,
1063 const struct GNUNET_FS_Uri *uri,
1065 enum GNUNET_FS_SearchOptions options,
1067 struct GNUNET_FS_SearchContext *parent)
1069 struct GNUNET_FS_SearchContext *sc;
1070 struct GNUNET_CLIENT_Connection *client;
1071 struct GNUNET_FS_ProgressInfo pi;
1074 const char *keyword;
1076 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1077 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1079 if (GNUNET_FS_uri_test_ksk (uri))
1081 size = sizeof (struct SearchMessage) * uri->data.ksk.keywordCount;
1085 GNUNET_assert (GNUNET_FS_uri_test_sks (uri));
1086 size = sizeof (struct SearchMessage);
1088 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1091 _("Too many keywords specified for a single search."));
1094 client = GNUNET_CLIENT_connect (h->sched,
1099 sc = GNUNET_malloc (sizeof(struct GNUNET_FS_SearchContext));
1101 sc->options = options;
1102 sc->uri = GNUNET_FS_uri_dup (uri);
1103 sc->anonymity = anonymity;
1104 sc->start_time = GNUNET_TIME_absolute_get ();
1105 sc->client = client;
1106 sc->parent = parent;
1107 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1108 sc->client_info = cctx;
1109 if (GNUNET_FS_uri_test_ksk (uri))
1111 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1112 sc->requests = GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1113 sc->uri->data.ksk.keywordCount);
1114 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1116 keyword = &sc->uri->data.ksk.keywords[i][1];
1117 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1118 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1119 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1120 GNUNET_CRYPTO_rsa_key_free (pk);
1121 GNUNET_CRYPTO_hash (&pub,
1122 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1123 &sc->requests[i].query);
1124 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1125 if (sc->requests[i].mandatory)
1126 sc->mandatory_count++;
1127 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1128 GNUNET_CRYPTO_hash (keyword,
1130 &sc->requests[i].key);
1134 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1137 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1138 sc->client_info = make_search_status (&pi, sc);
1139 GNUNET_CLIENT_notify_transmit_ready (client,
1141 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1143 &transmit_search_request,
1150 * Start search for content.
1152 * @param h handle to the file sharing subsystem
1153 * @param uri specifies the search parameters; can be
1154 * a KSK URI or an SKS URI.
1155 * @param anonymity desired level of anonymity
1156 * @param options options for the search
1157 * @param cctx initial value for the client context
1158 * @return context that can be used to control the search
1160 struct GNUNET_FS_SearchContext *
1161 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1162 const struct GNUNET_FS_Uri *uri,
1164 enum GNUNET_FS_SearchOptions options,
1167 return search_start (h, uri, anonymity, options, cctx, NULL);
1174 * @param sc context for the search that should be paused
1177 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1179 struct GNUNET_FS_ProgressInfo pi;
1181 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1182 GNUNET_SCHEDULER_cancel (sc->h->sched,
1184 sc->task = GNUNET_SCHEDULER_NO_TASK;
1185 if (NULL != sc->client)
1186 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1188 // FIXME: make persistent!
1189 // FIXME: should this freeze all active probes?
1190 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1191 sc->client_info = make_search_status (&pi, sc);
1196 * Continue paused search.
1198 * @param sc context for the search that should be resumed
1201 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1203 struct GNUNET_FS_ProgressInfo pi;
1205 GNUNET_assert (sc->client == NULL);
1206 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1207 do_reconnect (sc, NULL);
1208 // FIXME: make persistent!
1209 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1210 sc->client_info = make_search_status (&pi, sc);
1215 * Free 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_free (void *cls,
1224 const GNUNET_HashCode * key,
1227 struct GNUNET_FS_SearchContext *sc = cls;
1228 struct GNUNET_FS_Handle *h = sc->h;
1229 struct SearchResult *sr = value;
1230 struct GNUNET_FS_ProgressInfo pi;
1232 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1233 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1234 pi.value.search.specifics.result_stopped.meta = sr->meta;
1235 pi.value.search.specifics.result_stopped.uri = sr->uri;
1236 sr->client_info = make_search_status (&pi, sc);
1237 GNUNET_break (NULL == sr->client_info);
1239 GNUNET_FS_uri_destroy (sr->uri);
1240 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1241 if (sr->probe_ctx != NULL)
1242 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1243 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1244 GNUNET_SCHEDULER_cancel (h->sched,
1245 sr->probe_cancel_task);
1252 * Stop search for content.
1254 * @param sc context for the search that should be stopped
1257 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1259 struct GNUNET_FS_ProgressInfo pi;
1261 struct GNUNET_FS_SearchContext *parent;
1263 // FIXME: make un-persistent!
1264 if (NULL != (parent = sc->parent))
1266 GNUNET_CONTAINER_DLL_remove (parent->child_head,
1271 while (NULL != sc->child_head)
1272 GNUNET_FS_search_stop (sc->child_head);
1273 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1274 &search_result_free,
1276 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1277 sc->client_info = make_search_status (&pi, sc);
1278 GNUNET_break (NULL == sc->client_info);
1279 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1280 GNUNET_SCHEDULER_cancel (sc->h->sched,
1282 if (NULL != sc->client)
1283 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1284 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1285 if (sc->requests != NULL)
1287 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1288 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1289 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1291 GNUNET_free_non_null (sc->requests);
1292 GNUNET_FS_uri_destroy (sc->uri);
1296 /* end of fs_search.c */