2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_search.c
23 * @brief Helper functions for searching.
24 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_protocols.h"
33 #define DEBUG_SEARCH GNUNET_EXTRA_LOGGING
36 * Number of availability trials we perform per search result.
38 #define AVAILABILITY_TRIALS_MAX 8
41 * Fill in all of the generic fields for a search event and
44 * @param pi structure to fill in
45 * @param sc overall search context
46 * @return value returned by the callback
49 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
50 struct GNUNET_FS_SearchContext *sc)
54 pi->value.search.sc = sc;
55 pi->value.search.cctx = sc->client_info;
56 pi->value.search.pctx =
57 (sc->psearch_result == NULL) ? NULL : sc->psearch_result->client_info;
58 pi->value.search.query = sc->uri;
59 pi->value.search.duration =
60 GNUNET_TIME_absolute_get_duration (sc->start_time);
61 pi->value.search.anonymity = sc->anonymity;
62 ret = sc->h->upcb (sc->h->upcb_cls, pi);
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, const GNUNET_HashCode * key, void *value)
81 const struct GNUNET_FS_Uri *uri = cls;
82 struct GNUNET_FS_SearchResult *sr = value;
84 if (GNUNET_FS_uri_test_equal (uri, sr->uri))
91 * We've found a new CHK result. Let the client
94 * @param sc the search context
95 * @param sr the specific result
98 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
99 struct GNUNET_FS_SearchResult *sr)
101 struct GNUNET_FS_ProgressInfo pi;
103 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
104 pi.value.search.specifics.result.meta = sr->meta;
105 pi.value.search.specifics.result.uri = sr->uri;
106 pi.value.search.specifics.result.result = sr;
107 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
108 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
113 * We've found new information about an existing CHK result. Let the
114 * client know about it.
116 * @param sc the search context
117 * @param sr the specific result
120 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
121 struct GNUNET_FS_SearchResult *sr)
123 struct GNUNET_FS_ProgressInfo pi;
125 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
126 pi.value.search.specifics.update.cctx = sr->client_info;
127 pi.value.search.specifics.update.meta = sr->meta;
128 pi.value.search.specifics.update.uri = sr->uri;
129 pi.value.search.specifics.update.availability_rank =
130 2 * sr->availability_success - sr->availability_trials;
131 pi.value.search.specifics.update.availability_certainty =
132 sr->availability_trials;
133 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
134 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
139 * Context for "get_result_present".
141 struct GetResultContext
144 * The URI we're looking for.
146 const struct GNUNET_FS_Uri *uri;
149 * Where to store a pointer to the search
150 * result struct if we found a match.
152 struct GNUNET_FS_SearchResult *sr;
157 * Check if the given result is identical to the given URI and if so
160 * @param cls a "struct GetResultContext"
161 * @param key not used
162 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
163 * should compare with
167 get_result_present (void *cls, const GNUNET_HashCode * key, void *value)
169 struct GetResultContext *grc = cls;
170 struct GNUNET_FS_SearchResult *sr = value;
172 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
179 * Signal result of last probe to client and then schedule next
183 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
185 struct GNUNET_FS_ProgressInfo pi;
187 pi.status = GNUNET_FS_STATUS_SEARCH_START;
188 pi.value.search.specifics.update.cctx = sr->client_info;
189 pi.value.search.specifics.update.meta = sr->meta;
190 pi.value.search.specifics.update.uri = sr->uri;
191 pi.value.search.specifics.update.availability_rank = sr->availability_success;
192 pi.value.search.specifics.update.availability_certainty =
193 sr->availability_trials;
194 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
195 sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
196 GNUNET_FS_search_start_probe_ (sr);
201 * Handle the case where we have failed to receive a response for our probe.
203 * @param cls our 'struct GNUNET_FS_SearchResult*'
204 * @param tc scheduler context
207 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 struct GNUNET_FS_SearchResult *sr = cls;
211 sr->availability_trials++;
212 GNUNET_FS_search_result_sync_ (sr);
213 signal_probe_result (sr);
218 * Handle the case where we have gotten a response for our probe.
220 * @param cls our 'struct GNUNET_FS_SearchResult*'
221 * @param tc scheduler context
224 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 struct GNUNET_FS_SearchResult *sr = cls;
228 sr->availability_trials++;
229 sr->availability_success++;
230 GNUNET_FS_search_result_sync_ (sr);
231 signal_probe_result (sr);
236 * Notification of FS that a search probe has made progress.
237 * This function is used INSTEAD of the client's event handler
238 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
240 * @param cls closure, always NULL (!), actual closure
241 * is in the client-context of the info struct
242 * @param info details about the event, specifying the event type
243 * and various bits about the event
244 * @return client-context (for the next progress call
245 * for this operation; should be set to NULL for
246 * SUSPEND and STOPPED events). The value returned
247 * will be passed to future callbacks in the respective
248 * field in the GNUNET_FS_ProgressInfo struct.
251 GNUNET_FS_search_probe_progress_ (void *cls,
252 const struct GNUNET_FS_ProgressInfo *info)
254 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
255 struct GNUNET_TIME_Relative dur;
257 switch (info->status)
259 case GNUNET_FS_STATUS_DOWNLOAD_START:
262 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
263 /* probes should never be resumed */
266 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
267 /* probes should never be suspended */
270 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
273 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
274 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
276 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
277 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
279 sr->probe_cancel_task =
280 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
281 &probe_failure_handler, sr);
283 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
284 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
286 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
287 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
289 sr->probe_cancel_task =
290 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
291 &probe_success_handler, sr);
293 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
294 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
296 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
297 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
301 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
302 GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
303 sr->probe_active_time = GNUNET_TIME_absolute_get ();
304 sr->probe_cancel_task =
305 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
306 &probe_failure_handler, sr);
308 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
309 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
311 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
312 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
314 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
315 sr->remaining_probe_time =
316 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
317 GNUNET_FS_search_result_sync_ (sr);
328 * Start download probes for the given search result.
330 * @param sr the search result
333 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
338 if (sr->probe_ctx != NULL)
340 if (sr->download != NULL)
342 if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
344 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
346 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
349 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
351 off = len / DBLOCK_SIZE;
353 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
355 if (len - off < DBLOCK_SIZE)
359 sr->remaining_probe_time =
360 GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
361 2 * (1 + sr->availability_trials));
363 GNUNET_FS_download_start (sr->sc->h, sr->uri, sr->meta, NULL, NULL, off,
364 len, sr->sc->anonymity,
365 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
366 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
371 * We have received a KSK result. Check how it fits in with the
372 * overall query and notify the client accordingly.
374 * @param sc context for the overall query
375 * @param ent entry for the specific keyword
376 * @param uri the URI that was found
377 * @param meta metadata associated with the URI
378 * under the "ent" keyword
381 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
382 struct SearchRequestEntry *ent,
383 const struct GNUNET_FS_Uri *uri,
384 const struct GNUNET_CONTAINER_MetaData *meta)
387 struct GNUNET_FS_SearchResult *sr;
388 struct GetResultContext grc;
392 GNUNET_assert (NULL != sc);
393 GNUNET_FS_uri_to_key (uri, &key);
395 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results, &key,
396 &test_result_present,
398 return; /* duplicate result */
399 /* try to find search result in master map */
402 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
403 &get_result_present, &grc);
405 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
408 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
410 sr->uri = GNUNET_FS_uri_dup (uri);
411 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
412 sr->mandatory_missing = sc->mandatory_count;
414 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
419 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
421 /* check if mandatory satisfied */
423 sr->mandatory_missing--;
425 sr->optional_support++;
426 if (0 != sr->mandatory_missing)
429 notify_client_chk_result (sc, sr);
431 notify_client_chk_update (sc, sr);
432 GNUNET_FS_search_result_sync_ (sr);
433 GNUNET_FS_search_start_probe_ (sr);
438 * Start search for content, internal API.
440 * @param h handle to the file sharing subsystem
441 * @param uri specifies the search parameters; can be
442 * a KSK URI or an SKS URI.
443 * @param anonymity desired level of anonymity
444 * @param options options for the search
445 * @param cctx client context
446 * @param psearch parent search result (for namespace update searches)
447 * @return context that can be used to control the search
449 static struct GNUNET_FS_SearchContext *
450 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
451 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
452 void *cctx, struct GNUNET_FS_SearchResult *psearch);
456 * We have received an SKS result. Start searching for updates and
457 * notify the client if it is a new result.
459 * @param sc context for the overall query
460 * @param id_update identifier for updates, NULL for none
461 * @param uri the URI that was found
462 * @param meta metadata associated with the URI
465 process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
466 const struct GNUNET_FS_Uri *uri,
467 const struct GNUNET_CONTAINER_MetaData *meta)
469 struct GNUNET_FS_Uri uu;
471 struct GNUNET_FS_SearchResult *sr;
474 GNUNET_assert (NULL != sc);
475 GNUNET_FS_uri_to_key (uri, &key);
476 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
479 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
480 &test_result_present,
482 return; /* duplicate result */
483 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
485 sr->uri = GNUNET_FS_uri_dup (uri);
486 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
488 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
490 GNUNET_FS_search_result_sync_ (sr);
491 GNUNET_FS_search_start_probe_ (sr);
493 notify_client_chk_result (sc, sr);
494 /* search for updates */
495 if (strlen (id_update) == 0)
496 return; /* no updates */
498 uu.data.sks.namespace = sc->uri->data.sks.namespace;
499 uu.data.sks.identifier = GNUNET_strdup (id_update);
500 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
501 GNUNET_free (uu.data.sks.identifier);
506 * Process a keyword-search result.
508 * @param sc our search context
509 * @param kb the kblock
510 * @param size size of kb
513 process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
519 char pt[size - sizeof (struct KBlock)];
520 struct GNUNET_CRYPTO_AesSessionKey skey;
521 struct GNUNET_CRYPTO_AesInitializationVector iv;
523 struct GNUNET_CONTAINER_MetaData *meta;
524 struct GNUNET_FS_Uri *uri;
527 GNUNET_CRYPTO_hash (&kb->keyspace,
528 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
531 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
532 if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
534 if (i == sc->uri->data.ksk.keywordCount)
536 /* oops, does not match any of our keywords!? */
541 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
543 GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey,
550 eos = memchr (pt, 0, sizeof (pt));
557 if (sizeof (pt) == j)
558 meta = GNUNET_CONTAINER_meta_data_create ();
560 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
563 GNUNET_break_op (0); /* kblock malformed */
566 uri = GNUNET_FS_uri_parse (pt, &emsg);
569 GNUNET_break_op (0); /* kblock malformed */
570 GNUNET_free_non_null (emsg);
571 GNUNET_CONTAINER_meta_data_destroy (meta);
575 process_ksk_result (sc, &sc->requests[i], uri, meta);
578 GNUNET_CONTAINER_meta_data_destroy (meta);
579 GNUNET_FS_uri_destroy (uri);
584 * Process a keyword-search result with a namespace advertisment.
586 * @param sc our search context
587 * @param nb the nblock
588 * @param size size of nb
591 process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb,
597 char pt[size - sizeof (struct NBlock)];
598 struct GNUNET_CRYPTO_AesSessionKey skey;
599 struct GNUNET_CRYPTO_AesInitializationVector iv;
601 struct GNUNET_CONTAINER_MetaData *meta;
602 struct GNUNET_FS_Uri *uri;
605 GNUNET_CRYPTO_hash (&nb->keyspace,
606 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
609 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
610 if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
612 if (i == sc->uri->data.ksk.keywordCount)
614 /* oops, does not match any of our keywords!? */
619 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
621 GNUNET_CRYPTO_aes_decrypt (&nb[1], size - sizeof (struct NBlock), &skey,
628 eos = memchr (pt, 0, sizeof (pt));
635 if (sizeof (pt) == j)
636 meta = GNUNET_CONTAINER_meta_data_create ();
638 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
641 GNUNET_break_op (0); /* nblock malformed */
645 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
647 uri->data.sks.identifier = GNUNET_strdup (pt);
648 GNUNET_CRYPTO_hash (&nb->subspace,
649 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
650 &uri->data.sks.namespace);
651 uris = GNUNET_FS_uri_to_string (uri);
652 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_URI,
653 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
654 uris, strlen (uris) + 1);
656 GNUNET_PSEUDONYM_add (sc->h->cfg, &uri->data.sks.namespace, meta);
658 process_ksk_result (sc, &sc->requests[i], uri, meta);
661 GNUNET_CONTAINER_meta_data_destroy (meta);
662 GNUNET_FS_uri_destroy (uri);
667 * Process a namespace-search result.
669 * @param sc our search context
670 * @param sb the sblock
671 * @param size size of sb
674 process_sblock (struct GNUNET_FS_SearchContext *sc, const struct SBlock *sb,
677 size_t len = size - sizeof (struct SBlock);
679 struct GNUNET_CRYPTO_AesSessionKey skey;
680 struct GNUNET_CRYPTO_AesInitializationVector iv;
681 struct GNUNET_FS_Uri *uri;
682 struct GNUNET_CONTAINER_MetaData *meta;
691 identifier = sc->uri->data.sks.identifier;
692 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
693 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
694 if (-1 == GNUNET_CRYPTO_aes_decrypt (&sb[1], len, &skey, &iv, pt))
700 off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris);
703 GNUNET_break_op (0); /* sblock malformed */
706 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off);
709 GNUNET_break_op (0); /* sblock malformed */
712 uri = GNUNET_FS_uri_parse (uris, &emsg);
715 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse URI `%s': %s\n", uris,
717 GNUNET_break_op (0); /* sblock malformed */
718 GNUNET_free_non_null (emsg);
719 GNUNET_CONTAINER_meta_data_destroy (meta);
723 process_sks_result (sc, id, uri, meta);
725 GNUNET_FS_uri_destroy (uri);
726 GNUNET_CONTAINER_meta_data_destroy (meta);
731 * Process a search result.
733 * @param sc our search context
734 * @param type type of the result
735 * @param expiration when it will expire
736 * @param data the (encrypted) response
737 * @param size size of data
740 process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
741 struct GNUNET_TIME_Absolute expiration, const void *data,
744 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "Result received has already expired.\n");
748 return; /* result expired */
752 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
753 if (!GNUNET_FS_uri_test_ksk (sc->uri))
758 if (sizeof (struct KBlock) > size)
763 process_kblock (sc, data, size);
765 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
766 if (!GNUNET_FS_uri_test_sks (sc->uri))
771 if (sizeof (struct SBlock) > size)
776 process_sblock (sc, data, size);
778 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
779 if (!GNUNET_FS_uri_test_ksk (sc->uri))
784 if (sizeof (struct NBlock) > size)
789 process_nblock (sc, data, size);
791 case GNUNET_BLOCK_TYPE_ANY:
794 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
797 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
800 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
804 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
805 _("Got result with unknown block type `%d', ignoring"), type);
812 * Shutdown any existing connection to the FS
813 * service and try to establish a fresh one
814 * (and then re-transmit our search request).
816 * @param sc the search to reconnec
819 try_reconnect (struct GNUNET_FS_SearchContext *sc);
823 * Type of a function to call when we receive a message
827 * @param msg message received, NULL on timeout or fatal error
830 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
832 struct GNUNET_FS_SearchContext *sc = cls;
833 const struct PutMessage *cm;
836 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
837 (ntohs (msg->size) <= sizeof (struct PutMessage)))
842 msize = ntohs (msg->size);
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Receiving %u bytes of result from fs service\n", msize);
845 cm = (const struct PutMessage *) msg;
846 process_result (sc, ntohl (cm->type),
847 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
848 msize - sizeof (struct PutMessage));
849 /* continue receiving */
850 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
851 GNUNET_TIME_UNIT_FOREVER_REL);
856 * Schedule the transmission of the (next) search request
859 * @param sc context for the search
862 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
866 * Closure for 'build_result_set'.
868 struct MessageBuilderContext
871 * How many entries can we store to xoff.
873 unsigned int put_cnt;
876 * How many entries should we skip.
878 unsigned int skip_cnt;
881 * Where to store the keys.
883 GNUNET_HashCode *xoff;
886 * Search context we are iterating for.
888 struct GNUNET_FS_SearchContext *sc;
891 * URI the search result must match, NULL for any
893 struct GNUNET_FS_Uri *uri;
898 * Iterating over the known results, pick those matching the given
899 * result range and store their keys at 'xoff'.
901 * @param cls the 'struct MessageBuilderContext'
902 * @param key key for a result
903 * @param value the search result
904 * @return GNUNET_OK to continue iterating
907 build_result_set (void *cls, const GNUNET_HashCode * key, void *value)
909 struct MessageBuilderContext *mbc = cls;
910 struct GNUNET_FS_SearchResult *sr = value;
912 if ((mbc->uri != NULL) &&
913 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
915 if (mbc->skip_cnt > 0)
920 if (mbc->put_cnt == 0)
921 return GNUNET_SYSERR;
922 mbc->sc->search_request_map_offset++;
923 mbc->xoff[--mbc->put_cnt] = *key;
929 * Iterating over the known results, count those
930 * matching the given result range and increment
931 * put count for each.
933 * @param cls the 'struct MessageBuilderContext'
934 * @param key key for a result
935 * @param value the search result
936 * @return GNUNET_OK to continue iterating
939 find_result_set (void *cls, const GNUNET_HashCode * key, void *value)
941 struct MessageBuilderContext *mbc = cls;
942 struct GNUNET_FS_SearchResult *sr = value;
944 if ((mbc->uri != NULL) &&
945 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
953 * We're ready to transmit the search request to the
954 * file-sharing service. Do it.
957 * @param size number of bytes available in buf
958 * @param buf where the callee should write the message
959 * @return number of bytes written to buf
962 transmit_search_request (void *cls, size_t size, void *buf)
964 struct GNUNET_FS_SearchContext *sc = cls;
965 struct MessageBuilderContext mbc;
967 struct SearchMessage *sm;
968 const char *identifier;
980 mbc.skip_cnt = sc->search_request_map_offset;
982 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
983 mbc.xoff = (GNUNET_HashCode *) & sm[1];
984 options = SEARCH_MESSAGE_OPTION_NONE;
985 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
986 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
987 if (GNUNET_FS_uri_test_ksk (sc->uri))
989 msize = sizeof (struct SearchMessage);
990 GNUNET_assert (size >= msize);
993 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
994 &find_result_set, &mbc);
996 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
997 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
998 if (sc->search_request_map_offset < sqms)
999 GNUNET_assert (mbc.put_cnt > 0);
1001 sm->header.size = htons (msize);
1002 sm->type = htonl (GNUNET_BLOCK_TYPE_ANY);
1003 sm->anonymity_level = htonl (sc->anonymity);
1004 memset (&sm->target, 0, sizeof (GNUNET_HashCode));
1005 sm->query = sc->requests[sc->keyword_offset].query;
1006 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1007 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1008 &build_result_set, &mbc);
1009 sm->header.size = htons (msize);
1010 if (sqms != sc->search_request_map_offset)
1012 /* more requesting to be done... */
1013 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1014 schedule_transmit_search_request (sc);
1017 sm->options = htonl (options);
1018 sc->keyword_offset++;
1019 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1021 /* more requesting to be done... */
1022 schedule_transmit_search_request (sc);
1028 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1029 msize = sizeof (struct SearchMessage);
1030 GNUNET_assert (size >= msize);
1031 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_SBLOCK);
1032 sm->anonymity_level = htonl (sc->anonymity);
1033 sm->target = sc->uri->data.sks.namespace;
1034 identifier = sc->uri->data.sks.identifier;
1035 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
1036 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &idh);
1037 GNUNET_CRYPTO_hash_xor (&idh, &sm->target, &sm->query);
1038 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
1039 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1040 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1042 if (sc->search_request_map_offset < sqms)
1043 GNUNET_assert (mbc.put_cnt > 0);
1044 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1045 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1046 &build_result_set, &mbc);
1047 sm->header.size = htons (msize);
1048 if (sqms != sc->search_request_map_offset)
1050 /* more requesting to be done... */
1051 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1052 schedule_transmit_search_request (sc);
1055 sm->options = htonl (options);
1057 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1058 GNUNET_TIME_UNIT_FOREVER_REL);
1064 * Schedule the transmission of the (next) search request
1067 * @param sc context for the search
1070 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1076 size = sizeof (struct SearchMessage);
1078 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1079 sc->search_request_map_offset;
1080 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (GNUNET_HashCode);
1081 fit = GNUNET_MIN (fit, sqms);
1082 size += sizeof (GNUNET_HashCode) * fit;
1083 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1084 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1085 GNUNET_NO, &transmit_search_request, sc);
1091 * Reconnect to the FS service and transmit
1094 * @param cls our search context
1098 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 struct GNUNET_FS_SearchContext *sc = cls;
1101 struct GNUNET_CLIENT_Connection *client;
1103 sc->task = GNUNET_SCHEDULER_NO_TASK;
1104 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1110 sc->client = client;
1111 sc->search_request_map_offset = 0;
1112 sc->keyword_offset = 0;
1113 schedule_transmit_search_request (sc);
1118 * Shutdown any existing connection to the FS
1119 * service and try to establish a fresh one
1120 * (and then re-transmit our search request).
1122 * @param sc the search to reconnec
1125 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1127 if (NULL != sc->client)
1129 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1133 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1139 * Start search for content, internal API.
1141 * @param h handle to the file sharing subsystem
1142 * @param uri specifies the search parameters; can be
1143 * a KSK URI or an SKS URI.
1144 * @param anonymity desired level of anonymity
1145 * @param options options for the search
1146 * @param cctx initial value for the client context
1147 * @param psearch parent search result (for namespace update searches)
1148 * @return context that can be used to control the search
1150 static struct GNUNET_FS_SearchContext *
1151 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1152 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1153 void *cctx, struct GNUNET_FS_SearchResult *psearch)
1155 struct GNUNET_FS_SearchContext *sc;
1156 struct GNUNET_FS_ProgressInfo pi;
1158 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1160 sc->options = options;
1161 sc->uri = GNUNET_FS_uri_dup (uri);
1162 sc->anonymity = anonymity;
1163 sc->start_time = GNUNET_TIME_absolute_get ();
1164 if (psearch != NULL)
1166 sc->psearch_result = psearch;
1167 psearch->update_search = sc;
1169 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1170 sc->client_info = cctx;
1171 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1173 GNUNET_FS_uri_destroy (sc->uri);
1174 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1178 GNUNET_FS_search_sync_ (sc);
1179 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1180 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1186 * Build the request and actually initiate the search using the
1187 * GNUnet FS service.
1189 * @param sc search context
1190 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1193 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1196 const char *keyword;
1198 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1199 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1201 GNUNET_assert (NULL == sc->client);
1202 if (GNUNET_FS_uri_test_ksk (sc->uri))
1204 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1206 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1207 sc->uri->data.ksk.keywordCount);
1208 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1210 keyword = &sc->uri->data.ksk.keywords[i][1];
1211 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1212 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1213 GNUNET_assert (pk != NULL);
1214 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1215 GNUNET_CRYPTO_rsa_key_free (pk);
1216 GNUNET_CRYPTO_hash (&pub,
1218 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1219 &sc->requests[i].query);
1220 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1221 if (sc->requests[i].mandatory)
1222 sc->mandatory_count++;
1223 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1224 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].key);
1227 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1228 if (NULL == sc->client)
1229 return GNUNET_SYSERR;
1230 schedule_transmit_search_request (sc);
1236 * Freeze probes for the given search result.
1238 * @param cls the global FS handle
1239 * @param key the key for the search result (unused)
1240 * @param value the search result to free
1244 search_result_freeze_probes (void *cls, const GNUNET_HashCode * key,
1247 struct GNUNET_FS_SearchResult *sr = value;
1249 if (sr->probe_ctx != NULL)
1251 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1252 sr->probe_ctx = NULL;
1254 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1256 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1257 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1259 if (sr->update_search != NULL)
1260 GNUNET_FS_search_pause (sr->update_search);
1266 * Resume probes for the given search result.
1268 * @param cls the global FS handle
1269 * @param key the key for the search result (unused)
1270 * @param value the search result to free
1274 search_result_resume_probes (void *cls, const GNUNET_HashCode * key,
1277 struct GNUNET_FS_SearchResult *sr = value;
1279 GNUNET_FS_search_start_probe_ (sr);
1280 if (sr->update_search != NULL)
1281 GNUNET_FS_search_continue (sr->update_search);
1287 * Signal suspend and free the given search result.
1289 * @param cls the global FS handle
1290 * @param key the key for the search result (unused)
1291 * @param value the search result to free
1295 search_result_suspend (void *cls, const GNUNET_HashCode * key, void *value)
1297 struct GNUNET_FS_SearchContext *sc = cls;
1298 struct GNUNET_FS_SearchResult *sr = value;
1299 struct GNUNET_FS_ProgressInfo pi;
1301 if (sr->download != NULL)
1302 GNUNET_FS_download_signal_suspend_ (sr->download);
1303 if (sr->update_search != NULL)
1304 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1305 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1306 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1307 pi.value.search.specifics.result_suspend.meta = sr->meta;
1308 pi.value.search.specifics.result_suspend.uri = sr->uri;
1309 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1310 GNUNET_break (NULL == sr->client_info);
1311 GNUNET_free_non_null (sr->serialization);
1312 GNUNET_FS_uri_destroy (sr->uri);
1313 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1314 if (sr->probe_ctx != NULL)
1315 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1316 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1317 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1324 * Create SUSPEND event for the given search operation
1325 * and then clean up our state (without stop signal).
1327 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1330 GNUNET_FS_search_signal_suspend_ (void *cls)
1332 struct GNUNET_FS_SearchContext *sc = cls;
1333 struct GNUNET_FS_ProgressInfo pi;
1336 GNUNET_FS_end_top (sc->h, sc->top);
1337 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1338 &search_result_suspend, sc);
1339 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1340 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1341 GNUNET_break (NULL == sc->client_info);
1342 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1343 GNUNET_SCHEDULER_cancel (sc->task);
1344 if (NULL != sc->client)
1345 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1346 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1347 if (sc->requests != NULL)
1349 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1350 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1351 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1353 GNUNET_free_non_null (sc->requests);
1354 GNUNET_free_non_null (sc->emsg);
1355 GNUNET_FS_uri_destroy (sc->uri);
1356 GNUNET_free_non_null (sc->serialization);
1362 * Start search for content.
1364 * @param h handle to the file sharing subsystem
1365 * @param uri specifies the search parameters; can be
1366 * a KSK URI or an SKS URI.
1367 * @param anonymity desired level of anonymity
1368 * @param options options for the search
1369 * @param cctx initial value for the client context
1370 * @return context that can be used to control the search
1372 struct GNUNET_FS_SearchContext *
1373 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1374 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1375 enum GNUNET_FS_SearchOptions options, void *cctx)
1377 struct GNUNET_FS_SearchContext *ret;
1379 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1382 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1390 * @param sc context for the search that should be paused
1393 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1395 struct GNUNET_FS_ProgressInfo pi;
1397 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1398 GNUNET_SCHEDULER_cancel (sc->task);
1399 sc->task = GNUNET_SCHEDULER_NO_TASK;
1400 if (NULL != sc->client)
1401 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1403 GNUNET_FS_search_sync_ (sc);
1404 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1405 &search_result_freeze_probes, sc);
1406 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1407 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1412 * Continue paused search.
1414 * @param sc context for the search that should be resumed
1417 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1419 struct GNUNET_FS_ProgressInfo pi;
1421 GNUNET_assert (sc->client == NULL);
1422 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1423 do_reconnect (sc, NULL);
1424 GNUNET_FS_search_sync_ (sc);
1425 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1426 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1427 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1428 &search_result_resume_probes, sc);
1433 * Free the given search result.
1435 * @param cls the global FS handle
1436 * @param key the key for the search result (unused)
1437 * @param value the search result to free
1441 search_result_free (void *cls, const GNUNET_HashCode * key, void *value)
1443 struct GNUNET_FS_SearchContext *sc = cls;
1444 struct GNUNET_FS_SearchResult *sr = value;
1445 struct GNUNET_FS_ProgressInfo pi;
1447 if (NULL != sr->download)
1449 sr->download->search = NULL;
1451 GNUNET_FS_make_top (sr->download->h,
1452 &GNUNET_FS_download_signal_suspend_, sr->download);
1453 if (NULL != sr->download->serialization)
1455 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1456 sr->download->serialization);
1457 GNUNET_free (sr->download->serialization);
1458 sr->download->serialization = NULL;
1460 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1461 GNUNET_FS_download_make_status_ (&pi, sr->download);
1462 GNUNET_FS_download_sync_ (sr->download);
1463 sr->download = NULL;
1465 if (NULL != sr->update_search)
1467 GNUNET_FS_search_stop (sr->update_search);
1468 GNUNET_assert (sr->update_search == NULL);
1470 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1471 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1472 pi.value.search.specifics.result_stopped.meta = sr->meta;
1473 pi.value.search.specifics.result_stopped.uri = sr->uri;
1474 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1475 GNUNET_break (NULL == sr->client_info);
1476 GNUNET_free_non_null (sr->serialization);
1477 GNUNET_FS_uri_destroy (sr->uri);
1478 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1479 if (sr->probe_ctx != NULL)
1480 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1481 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1482 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1489 * Stop search for content.
1491 * @param sc context for the search that should be stopped
1494 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1496 struct GNUNET_FS_ProgressInfo pi;
1499 if (sc->top != NULL)
1500 GNUNET_FS_end_top (sc->h, sc->top);
1501 if (sc->psearch_result != NULL)
1502 sc->psearch_result->update_search = NULL;
1503 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1504 &search_result_free, sc);
1505 if (sc->serialization != NULL)
1507 GNUNET_FS_remove_sync_file_ (sc->h,
1508 (sc->psearch_result !=
1509 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1510 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1512 GNUNET_FS_remove_sync_dir_ (sc->h,
1513 (sc->psearch_result !=
1514 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1515 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1517 GNUNET_free (sc->serialization);
1519 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1520 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1521 GNUNET_break (NULL == sc->client_info);
1522 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1523 GNUNET_SCHEDULER_cancel (sc->task);
1524 if (NULL != sc->client)
1525 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1526 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1527 if (sc->requests != NULL)
1529 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1530 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1531 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1533 GNUNET_free_non_null (sc->requests);
1534 GNUNET_free_non_null (sc->emsg);
1535 GNUNET_FS_uri_destroy (sc->uri);
1539 /* end of fs_search.c */