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 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
111 sr->client_info = GNUNET_FS_search_make_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 GNUNET_FS_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 = GNUNET_FS_search_make_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 GNUNET_FS_SearchResult *sr;
161 * Check if the given result is identical to the given URI and if so
164 * @param cls a "struct GetResultContext"
165 * @param key not used
166 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
167 * should compare with
171 get_result_present (void *cls,
172 const GNUNET_HashCode * key,
175 struct GetResultContext *grc = cls;
176 struct GNUNET_FS_SearchResult *sr = value;
178 if (GNUNET_FS_uri_test_equal (grc->uri,
186 * Signal result of last probe to client and then schedule next
190 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
192 struct GNUNET_FS_ProgressInfo pi;
194 pi.status = GNUNET_FS_STATUS_SEARCH_START;
195 pi.value.search.specifics.update.cctx = sr->client_info;
196 pi.value.search.specifics.update.meta = sr->meta;
197 pi.value.search.specifics.update.uri = sr->uri;
198 pi.value.search.specifics.update.availability_rank = sr->availability_success;
199 pi.value.search.specifics.update.availability_certainty = sr->availability_trials;
200 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
201 sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
202 GNUNET_FS_search_start_probe_ (sr);
207 * Handle the case where we have failed to receive a response for our probe.
209 * @param cls our 'struct GNUNET_FS_SearchResult*'
210 * @param tc scheduler context
213 probe_failure_handler (void *cls,
214 const struct GNUNET_SCHEDULER_TaskContext *tc)
216 struct GNUNET_FS_SearchResult *sr = cls;
217 sr->availability_trials++;
218 GNUNET_FS_search_result_sync_ (sr);
219 signal_probe_result (sr);
224 * Handle the case where we have gotten a response for our probe.
226 * @param cls our 'struct GNUNET_FS_SearchResult*'
227 * @param tc scheduler context
230 probe_success_handler (void *cls,
231 const struct GNUNET_SCHEDULER_TaskContext *tc)
233 struct GNUNET_FS_SearchResult *sr = cls;
234 sr->availability_trials++;
235 sr->availability_success++;
236 GNUNET_FS_search_result_sync_ (sr);
237 signal_probe_result (sr);
242 * Notification of FS that a search probe has made progress.
243 * This function is used INSTEAD of the client's event handler
244 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
246 * @param cls closure, always NULL (!), actual closure
247 * is in the client-context of the info struct
248 * @param info details about the event, specifying the event type
249 * and various bits about the event
250 * @return client-context (for the next progress call
251 * for this operation; should be set to NULL for
252 * SUSPEND and STOPPED events). The value returned
253 * will be passed to future callbacks in the respective
254 * field in the GNUNET_FS_ProgressInfo struct.
257 GNUNET_FS_search_probe_progress_ (void *cls,
258 const struct GNUNET_FS_ProgressInfo *info)
260 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
261 struct GNUNET_TIME_Relative dur;
263 switch (info->status)
265 case GNUNET_FS_STATUS_DOWNLOAD_START:
268 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
269 /* probes should never be resumed */
272 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
273 /* probes should never be suspended */
276 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
279 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
280 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
282 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
283 sr->probe_cancel_task);
284 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
286 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
287 sr->remaining_probe_time,
288 &probe_failure_handler,
291 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
292 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
294 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
295 sr->probe_cancel_task);
296 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
298 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
299 sr->remaining_probe_time,
300 &probe_success_handler,
303 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
304 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
306 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
307 sr->probe_cancel_task);
308 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
312 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
313 GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
314 sr->probe_active_time = GNUNET_TIME_absolute_get ();
315 sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
316 sr->remaining_probe_time,
317 &probe_failure_handler,
320 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
321 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
323 GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
324 sr->probe_cancel_task);
325 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
327 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
328 sr->remaining_probe_time = GNUNET_TIME_relative_subtract (sr->remaining_probe_time,
330 GNUNET_FS_search_result_sync_ (sr);
341 * Start download probes for the given search result.
343 * @param sr the search result
346 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
351 if (sr->probe_ctx != NULL)
353 if (sr->download != NULL)
355 if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
357 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
359 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
362 if ( (len <= DBLOCK_SIZE) && (sr->availability_success > 0))
364 off = len / DBLOCK_SIZE;
366 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
368 if (len - off < DBLOCK_SIZE)
372 sr->remaining_probe_time = GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
373 2 * (1 + sr->availability_trials));
374 sr->probe_ctx = GNUNET_FS_download_start (sr->sc->h,
380 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
381 GNUNET_FS_DOWNLOAD_IS_PROBE,
387 * We have received a KSK result. Check how it fits in with the
388 * overall query and notify the client accordingly.
390 * @param sc context for the overall query
391 * @param ent entry for the specific keyword
392 * @param uri the URI that was found
393 * @param meta metadata associated with the URI
394 * under the "ent" keyword
397 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
398 struct SearchRequestEntry *ent,
399 const struct GNUNET_FS_Uri *uri,
400 const struct GNUNET_CONTAINER_MetaData *meta)
403 struct GNUNET_FS_SearchResult *sr;
404 struct GetResultContext grc;
408 GNUNET_FS_uri_to_key (uri, &key);
410 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
412 &test_result_present,
414 return; /* duplicate result */
415 /* try to find search result in master map */
418 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
423 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
426 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
428 sr->uri = GNUNET_FS_uri_dup (uri);
429 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
430 sr->mandatory_missing = sc->mandatory_count;
432 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
435 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
439 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
441 /* check if mandatory satisfied */
443 sr->mandatory_missing--;
445 sr->optional_support++;
446 if (0 != sr->mandatory_missing)
449 notify_client_chk_result (sc, sr);
451 notify_client_chk_update (sc, sr);
452 GNUNET_FS_search_result_sync_ (sr);
453 GNUNET_FS_search_start_probe_ (sr);
458 * Start search for content, internal API.
460 * @param h handle to the file sharing subsystem
461 * @param uri specifies the search parameters; can be
462 * a KSK URI or an SKS URI.
463 * @param anonymity desired level of anonymity
464 * @param options options for the search
465 * @param cctx client context
466 * @param psearch parent search result (for namespace update searches)
467 * @return context that can be used to control the search
469 static struct GNUNET_FS_SearchContext *
470 search_start (struct GNUNET_FS_Handle *h,
471 const struct GNUNET_FS_Uri *uri,
473 enum GNUNET_FS_SearchOptions options,
475 struct GNUNET_FS_SearchResult *psearch);
479 * We have received an SKS result. Start searching for updates and
480 * notify the client if it is a new result.
482 * @param sc context for the overall query
483 * @param id_update identifier for updates, NULL for none
484 * @param uri the URI that was found
485 * @param meta metadata associated with the URI
488 process_sks_result (struct GNUNET_FS_SearchContext *sc,
489 const char *id_update,
490 const struct GNUNET_FS_Uri *uri,
491 const struct GNUNET_CONTAINER_MetaData *meta)
493 struct GNUNET_FS_Uri uu;
495 struct GNUNET_FS_SearchResult *sr;
498 GNUNET_FS_uri_to_key (uri, &key);
499 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key,
500 &uri->data.chk.chk.query,
503 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
505 &test_result_present,
507 return; /* duplicate result */
508 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
510 sr->uri = GNUNET_FS_uri_dup (uri);
511 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
513 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
517 GNUNET_FS_search_result_sync_ (sr);
518 GNUNET_FS_search_start_probe_ (sr);
520 notify_client_chk_result (sc, sr);
521 /* search for updates */
522 if (strlen (id_update) == 0)
523 return; /* no updates */
525 uu.data.sks.namespace = sc->uri->data.sks.namespace;
526 uu.data.sks.identifier = GNUNET_strdup (id_update);
527 (void) search_start (sc->h,
533 GNUNET_free (uu.data.sks.identifier);
538 * Process a keyword-search result.
540 * @param sc our search context
541 * @param kb the kblock
542 * @param size size of kb
545 process_kblock (struct GNUNET_FS_SearchContext *sc,
546 const struct KBlock *kb,
552 char pt[size - sizeof (struct KBlock)];
553 struct GNUNET_CRYPTO_AesSessionKey skey;
554 struct GNUNET_CRYPTO_AesInitializationVector iv;
556 struct GNUNET_CONTAINER_MetaData *meta;
557 struct GNUNET_FS_Uri *uri;
560 GNUNET_CRYPTO_hash (&kb->keyspace,
561 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
564 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
566 &sc->requests[i].query,
567 sizeof (GNUNET_HashCode)))
569 if (i == sc->uri->data.ksk.keywordCount)
571 /* oops, does not match any of our keywords!? */
576 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
577 GNUNET_CRYPTO_aes_decrypt (&kb[1],
578 size - sizeof (struct KBlock),
583 eos = memchr (pt, 0, sizeof (pt));
590 if (sizeof (pt) == j)
591 meta = GNUNET_CONTAINER_meta_data_create ();
593 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
597 GNUNET_break_op (0); /* kblock malformed */
600 uri = GNUNET_FS_uri_parse (pt, &emsg);
603 GNUNET_break_op (0); /* kblock malformed */
604 GNUNET_free_non_null (emsg);
605 GNUNET_CONTAINER_meta_data_destroy (meta);
609 process_ksk_result (sc, &sc->requests[i], uri, meta);
612 GNUNET_CONTAINER_meta_data_destroy (meta);
613 GNUNET_FS_uri_destroy (uri);
618 * Process a keyword-search result with a namespace advertisment.
620 * @param sc our search context
621 * @param nb the nblock
622 * @param size size of nb
625 process_nblock (struct GNUNET_FS_SearchContext *sc,
626 const struct NBlock *nb,
632 char pt[size - sizeof (struct NBlock)];
633 struct GNUNET_CRYPTO_AesSessionKey skey;
634 struct GNUNET_CRYPTO_AesInitializationVector iv;
636 struct GNUNET_CONTAINER_MetaData *meta;
637 struct GNUNET_FS_Uri *uri;
640 GNUNET_CRYPTO_hash (&nb->keyspace,
641 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
644 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
646 &sc->requests[i].query,
647 sizeof (GNUNET_HashCode)))
649 if (i == sc->uri->data.ksk.keywordCount)
651 /* oops, does not match any of our keywords!? */
656 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
657 GNUNET_CRYPTO_aes_decrypt (&nb[1],
658 size - sizeof (struct NBlock),
663 eos = memchr (pt, 0, sizeof (pt));
670 if (sizeof (pt) == j)
671 meta = GNUNET_CONTAINER_meta_data_create ();
673 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
677 GNUNET_break_op (0); /* nblock malformed */
681 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
683 uri->data.sks.identifier = GNUNET_strdup (pt);
684 GNUNET_CRYPTO_hash (&nb->subspace,
685 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
686 &uri->data.sks.namespace);
687 uris = GNUNET_FS_uri_to_string (uri);
688 GNUNET_CONTAINER_meta_data_insert (meta,
690 EXTRACTOR_METATYPE_URI,
691 EXTRACTOR_METAFORMAT_UTF8,
696 GNUNET_PSEUDONYM_add (sc->h->cfg,
697 &uri->data.sks.namespace,
700 process_ksk_result (sc, &sc->requests[i], uri, meta);
703 GNUNET_CONTAINER_meta_data_destroy (meta);
704 GNUNET_FS_uri_destroy (uri);
709 * Process a namespace-search result.
711 * @param sc our search context
712 * @param sb the sblock
713 * @param size size of sb
716 process_sblock (struct GNUNET_FS_SearchContext *sc,
717 const struct SBlock *sb,
720 size_t len = size - sizeof (struct SBlock);
722 struct GNUNET_CRYPTO_AesSessionKey skey;
723 struct GNUNET_CRYPTO_AesInitializationVector iv;
724 struct GNUNET_FS_Uri *uri;
725 struct GNUNET_CONTAINER_MetaData *meta;
734 identifier = sc->uri->data.sks.identifier;
735 GNUNET_CRYPTO_hash (identifier,
738 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
739 GNUNET_CRYPTO_aes_decrypt (&sb[1],
745 off = GNUNET_STRINGS_buffer_tokenize (pt,
752 GNUNET_break_op (0); /* sblock malformed */
755 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off],
759 GNUNET_break_op (0); /* sblock malformed */
762 uri = GNUNET_FS_uri_parse (uris, &emsg);
765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
766 "Failed to parse URI `%s': %s\n",
768 GNUNET_break_op (0); /* sblock malformed */
769 GNUNET_free_non_null (emsg);
770 GNUNET_CONTAINER_meta_data_destroy (meta);
774 process_sks_result (sc, id, uri, meta);
776 GNUNET_FS_uri_destroy (uri);
777 GNUNET_CONTAINER_meta_data_destroy (meta);
782 * Process a search result.
784 * @param sc our search context
785 * @param type type of the result
786 * @param expiration when it will expire
787 * @param data the (encrypted) response
788 * @param size size of data
791 process_result (struct GNUNET_FS_SearchContext *sc,
792 enum GNUNET_BLOCK_Type type,
793 struct GNUNET_TIME_Absolute expiration,
797 if (GNUNET_TIME_absolute_get_duration (expiration).value > 0)
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Result received has already expired.\n");
801 return; /* result expired */
805 case GNUNET_BLOCK_TYPE_KBLOCK:
806 if (! GNUNET_FS_uri_test_ksk (sc->uri))
811 if (sizeof (struct KBlock) > size)
816 process_kblock (sc, data, size);
818 case GNUNET_BLOCK_TYPE_SBLOCK:
819 if (! GNUNET_FS_uri_test_sks (sc->uri))
824 if (sizeof (struct SBlock) > size)
829 process_sblock (sc, data, size);
831 case GNUNET_BLOCK_TYPE_NBLOCK:
832 if (! GNUNET_FS_uri_test_ksk (sc->uri))
837 if (sizeof (struct NBlock) > size)
842 process_nblock (sc, data, size);
844 case GNUNET_BLOCK_TYPE_ANY:
845 case GNUNET_BLOCK_TYPE_DBLOCK:
846 case GNUNET_BLOCK_TYPE_ONDEMAND:
847 case GNUNET_BLOCK_TYPE_IBLOCK:
851 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
852 _("Got result with unknown block type `%d', ignoring"),
860 * Shutdown any existing connection to the FS
861 * service and try to establish a fresh one
862 * (and then re-transmit our search request).
864 * @param sc the search to reconnec
867 try_reconnect (struct GNUNET_FS_SearchContext *sc);
871 * Type of a function to call when we receive a message
875 * @param msg message received, NULL on timeout or fatal error
878 receive_results (void *cls,
879 const struct GNUNET_MessageHeader * msg)
881 struct GNUNET_FS_SearchContext *sc = cls;
882 const struct PutMessage *cm;
885 if ( (NULL == msg) ||
886 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
887 (ntohs (msg->size) <= sizeof (struct PutMessage)) )
892 msize = ntohs (msg->size);
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Receiving %u bytes of result from fs service\n",
896 cm = (const struct PutMessage*) msg;
899 GNUNET_TIME_absolute_ntoh (cm->expiration),
901 msize - sizeof (struct PutMessage));
902 /* continue receiving */
903 GNUNET_CLIENT_receive (sc->client,
906 GNUNET_TIME_UNIT_FOREVER_REL);
911 * We're ready to transmit the search request to the
912 * file-sharing service. Do it.
915 * @param size number of bytes available in buf
916 * @param buf where the callee should write the message
917 * @return number of bytes written to buf
920 transmit_search_request (void *cls,
924 struct GNUNET_FS_SearchContext *sc = cls;
926 struct SearchMessage *sm;
928 const char *identifier;
937 if (GNUNET_FS_uri_test_ksk (sc->uri))
939 msize = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
940 GNUNET_assert (size >= msize);
942 memset (sm, 0, msize);
943 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
945 sm[i].header.size = htons (sizeof (struct SearchMessage));
946 sm[i].header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
947 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
948 sm[i].options = htonl (1);
950 sm[i].options = htonl (0);
951 sm[i].type = htonl (GNUNET_BLOCK_TYPE_ANY);
952 sm[i].anonymity_level = htonl (sc->anonymity);
953 sm[i].query = sc->requests[i].query;
954 /* FIXME: should transmit hash codes of all already-known results here!
955 (and if they do not fit, add another message with the same
956 header and additional already-seen results!) */
961 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
962 msize = sizeof (struct SearchMessage);
963 GNUNET_assert (size >= msize);
965 memset (sm, 0, msize);
966 sm->header.size = htons (sizeof (struct SearchMessage));
967 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
968 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
969 sm->options = htonl (1);
971 sm->options = htonl (0);
972 sm->type = htonl (GNUNET_BLOCK_TYPE_SBLOCK);
973 sm->anonymity_level = htonl (sc->anonymity);
974 sm->target = sc->uri->data.sks.namespace;
975 identifier = sc->uri->data.sks.identifier;
976 GNUNET_CRYPTO_hash (identifier,
979 GNUNET_CRYPTO_hash (&key,
980 sizeof (GNUNET_HashCode),
982 GNUNET_CRYPTO_hash_xor (&idh,
985 /* FIXME: should transmit hash codes of all already-known results here!
986 (and if they do not fit, add another message with the same
987 header and additional already-seen results!) */
989 GNUNET_CLIENT_receive (sc->client,
992 GNUNET_TIME_UNIT_FOREVER_REL);
998 * Reconnect to the FS service and transmit
1001 * @param cls our search context
1005 do_reconnect (void *cls,
1006 const struct GNUNET_SCHEDULER_TaskContext *tc)
1008 struct GNUNET_FS_SearchContext *sc = cls;
1009 struct GNUNET_CLIENT_Connection *client;
1012 sc->task = GNUNET_SCHEDULER_NO_TASK;
1013 client = GNUNET_CLIENT_connect (sc->h->sched,
1021 sc->client = client;
1022 if (GNUNET_FS_uri_test_ksk (sc->uri))
1023 size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1025 size = sizeof (struct SearchMessage);
1026 GNUNET_CLIENT_notify_transmit_ready (client,
1028 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1030 &transmit_search_request,
1036 * Shutdown any existing connection to the FS
1037 * service and try to establish a fresh one
1038 * (and then re-transmit our search request).
1040 * @param sc the search to reconnec
1043 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1045 if (NULL != sc->client)
1047 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1051 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
1052 GNUNET_TIME_UNIT_SECONDS,
1059 * Start search for content, internal API.
1061 * @param h handle to the file sharing subsystem
1062 * @param uri specifies the search parameters; can be
1063 * a KSK URI or an SKS URI.
1064 * @param anonymity desired level of anonymity
1065 * @param options options for the search
1066 * @param cctx initial value for the client context
1067 * @param psearch parent search result (for namespace update searches)
1068 * @return context that can be used to control the search
1070 static struct GNUNET_FS_SearchContext *
1071 search_start (struct GNUNET_FS_Handle *h,
1072 const struct GNUNET_FS_Uri *uri,
1074 enum GNUNET_FS_SearchOptions options,
1076 struct GNUNET_FS_SearchResult *psearch)
1078 struct GNUNET_FS_SearchContext *sc;
1079 struct GNUNET_FS_ProgressInfo pi;
1081 sc = GNUNET_malloc (sizeof(struct GNUNET_FS_SearchContext));
1083 sc->options = options;
1084 sc->uri = GNUNET_FS_uri_dup (uri);
1085 sc->anonymity = anonymity;
1086 sc->start_time = GNUNET_TIME_absolute_get ();
1087 if (psearch != NULL)
1089 sc->psearch_result = psearch;
1090 psearch->update_search = sc;
1092 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1093 sc->client_info = cctx;
1095 GNUNET_FS_search_start_searching_ (sc))
1097 GNUNET_FS_uri_destroy (sc->uri);
1098 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1102 GNUNET_FS_search_sync_ (sc);
1103 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1104 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1110 * Build the request and actually initiate the search using the
1111 * GNUnet FS service.
1113 * @param sc search context
1114 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1117 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1120 const char *keyword;
1122 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1123 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1126 GNUNET_assert (NULL == sc->client);
1127 if (GNUNET_FS_uri_test_ksk (sc->uri))
1129 size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1133 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1134 size = sizeof (struct SearchMessage);
1136 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1139 _("Too many keywords specified for a single search."));
1140 return GNUNET_SYSERR;
1142 if (GNUNET_FS_uri_test_ksk (sc->uri))
1144 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1145 sc->requests = GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1146 sc->uri->data.ksk.keywordCount);
1147 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1149 keyword = &sc->uri->data.ksk.keywords[i][1];
1150 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1151 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1152 GNUNET_assert (pk != NULL);
1153 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1154 GNUNET_CRYPTO_rsa_key_free (pk);
1155 GNUNET_CRYPTO_hash (&pub,
1156 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1157 &sc->requests[i].query);
1158 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1159 if (sc->requests[i].mandatory)
1160 sc->mandatory_count++;
1161 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1162 GNUNET_CRYPTO_hash (keyword,
1164 &sc->requests[i].key);
1167 sc->client = GNUNET_CLIENT_connect (sc->h->sched,
1170 if (NULL == sc->client)
1171 return GNUNET_SYSERR;
1172 GNUNET_CLIENT_notify_transmit_ready (sc->client,
1174 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1176 &transmit_search_request,
1183 * Freeze probes for the given search result.
1185 * @param cls the global FS handle
1186 * @param key the key for the search result (unused)
1187 * @param value the search result to free
1191 search_result_freeze_probes (void *cls,
1192 const GNUNET_HashCode * key,
1195 struct GNUNET_FS_SearchContext *sc = cls;
1196 struct GNUNET_FS_Handle *h = sc->h;
1197 struct GNUNET_FS_SearchResult *sr = value;
1199 if (sr->probe_ctx != NULL)
1201 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1202 sr->probe_ctx = NULL;
1204 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1206 GNUNET_SCHEDULER_cancel (h->sched,
1207 sr->probe_cancel_task);
1208 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1210 if (sr->update_search != NULL)
1211 GNUNET_FS_search_pause (sr->update_search);
1217 * Resume probes for the given search result.
1219 * @param cls the global FS handle
1220 * @param key the key for the search result (unused)
1221 * @param value the search result to free
1225 search_result_resume_probes (void *cls,
1226 const GNUNET_HashCode * key,
1229 struct GNUNET_FS_SearchResult *sr = value;
1231 GNUNET_FS_search_start_probe_ (sr);
1232 if (sr->update_search != NULL)
1233 GNUNET_FS_search_continue (sr->update_search);
1239 * Signal suspend and free the given search result.
1241 * @param cls the global FS handle
1242 * @param key the key for the search result (unused)
1243 * @param value the search result to free
1247 search_result_suspend (void *cls,
1248 const GNUNET_HashCode * key,
1251 struct GNUNET_FS_SearchContext *sc = cls;
1252 struct GNUNET_FS_Handle *h = sc->h;
1253 struct GNUNET_FS_SearchResult *sr = value;
1254 struct GNUNET_FS_ProgressInfo pi;
1256 if (sr->download != NULL)
1257 GNUNET_FS_download_signal_suspend_ (sr->download);
1258 if (sr->update_search != NULL)
1259 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1260 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1261 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1262 pi.value.search.specifics.result_suspend.meta = sr->meta;
1263 pi.value.search.specifics.result_suspend.uri = sr->uri;
1264 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1265 GNUNET_break (NULL == sr->client_info);
1266 GNUNET_free_non_null (sr->serialization);
1267 GNUNET_FS_uri_destroy (sr->uri);
1268 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1269 if (sr->probe_ctx != NULL)
1270 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1271 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1272 GNUNET_SCHEDULER_cancel (h->sched,
1273 sr->probe_cancel_task);
1280 * Create SUSPEND event for the given search operation
1281 * and then clean up our state (without stop signal).
1283 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1286 GNUNET_FS_search_signal_suspend_ (void *cls)
1288 struct GNUNET_FS_SearchContext *sc = cls;
1289 struct GNUNET_FS_ProgressInfo pi;
1292 GNUNET_FS_end_top (sc->h, sc->top);
1293 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1294 &search_result_suspend,
1296 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1297 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1298 GNUNET_break (NULL == sc->client_info);
1299 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1300 GNUNET_SCHEDULER_cancel (sc->h->sched,
1302 if (NULL != sc->client)
1303 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1304 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1305 if (sc->requests != NULL)
1307 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1308 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1309 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1311 GNUNET_free_non_null (sc->requests);
1312 GNUNET_free_non_null (sc->emsg);
1313 GNUNET_FS_uri_destroy (sc->uri);
1314 GNUNET_free_non_null (sc->serialization);
1320 * Start search for content.
1322 * @param h handle to the file sharing subsystem
1323 * @param uri specifies the search parameters; can be
1324 * a KSK URI or an SKS URI.
1325 * @param anonymity desired level of anonymity
1326 * @param options options for the search
1327 * @param cctx initial value for the client context
1328 * @return context that can be used to control the search
1330 struct GNUNET_FS_SearchContext *
1331 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1332 const struct GNUNET_FS_Uri *uri,
1334 enum GNUNET_FS_SearchOptions options,
1337 struct GNUNET_FS_SearchContext *ret;
1338 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1341 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1349 * @param sc context for the search that should be paused
1352 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1354 struct GNUNET_FS_ProgressInfo pi;
1356 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1357 GNUNET_SCHEDULER_cancel (sc->h->sched,
1359 sc->task = GNUNET_SCHEDULER_NO_TASK;
1360 if (NULL != sc->client)
1361 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1363 GNUNET_FS_search_sync_ (sc);
1364 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1365 &search_result_freeze_probes,
1367 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1368 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1373 * Continue paused search.
1375 * @param sc context for the search that should be resumed
1378 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1380 struct GNUNET_FS_ProgressInfo pi;
1382 GNUNET_assert (sc->client == NULL);
1383 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1384 do_reconnect (sc, NULL);
1385 GNUNET_FS_search_sync_ (sc);
1386 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1387 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1388 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1389 &search_result_resume_probes,
1395 * Free the given search result.
1397 * @param cls the global FS handle
1398 * @param key the key for the search result (unused)
1399 * @param value the search result to free
1403 search_result_free (void *cls,
1404 const GNUNET_HashCode * key,
1407 struct GNUNET_FS_SearchContext *sc = cls;
1408 struct GNUNET_FS_Handle *h = sc->h;
1409 struct GNUNET_FS_SearchResult *sr = value;
1410 struct GNUNET_FS_ProgressInfo pi;
1412 if (NULL != sr->download)
1414 sr->download->search = NULL;
1415 if (NULL != sr->download->serialization)
1417 GNUNET_FS_remove_sync_file_ (sc->h,
1418 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1419 sr->download->serialization);
1420 GNUNET_free (sr->download->serialization);
1421 sr->download->serialization = NULL;
1423 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1424 GNUNET_FS_download_make_status_ (&pi,
1426 GNUNET_FS_download_sync_ (sr->download);
1427 sr->download = NULL;
1429 if (NULL != sr->update_search)
1431 GNUNET_FS_search_stop (sr->update_search);
1432 GNUNET_assert (sr->update_search == NULL);
1434 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1435 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1436 pi.value.search.specifics.result_stopped.meta = sr->meta;
1437 pi.value.search.specifics.result_stopped.uri = sr->uri;
1438 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1439 GNUNET_break (NULL == sr->client_info);
1440 GNUNET_free_non_null (sr->serialization);
1441 GNUNET_FS_uri_destroy (sr->uri);
1442 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1443 if (sr->probe_ctx != NULL)
1444 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1445 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1446 GNUNET_SCHEDULER_cancel (h->sched,
1447 sr->probe_cancel_task);
1454 * Stop search for content.
1456 * @param sc context for the search that should be stopped
1459 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1461 struct GNUNET_FS_ProgressInfo pi;
1464 if (sc->top != NULL)
1465 GNUNET_FS_end_top (sc->h, sc->top);
1466 if (sc->psearch_result != NULL)
1467 sc->psearch_result->update_search = NULL;
1468 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1469 &search_result_free,
1471 if (sc->serialization != NULL)
1473 GNUNET_FS_remove_sync_file_ (sc->h,
1474 (sc->psearch_result != NULL)
1475 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1476 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1478 GNUNET_FS_remove_sync_dir_ (sc->h,
1479 (sc->psearch_result != NULL)
1480 ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH
1481 : GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1483 GNUNET_free (sc->serialization);
1485 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1486 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1487 GNUNET_break (NULL == sc->client_info);
1488 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1489 GNUNET_SCHEDULER_cancel (sc->h->sched,
1491 if (NULL != sc->client)
1492 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1493 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1494 if (sc->requests != NULL)
1496 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1497 for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1498 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1500 GNUNET_free_non_null (sc->requests);
1501 GNUNET_free_non_null (sc->emsg);
1502 GNUNET_FS_uri_destroy (sc->uri);
1506 /* end of fs_search.c */