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_FS_uri_to_key (uri, &key);
394 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results, &key,
395 &test_result_present,
397 return; /* duplicate result */
398 /* try to find search result in master map */
401 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
402 &get_result_present, &grc);
404 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
407 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
409 sr->uri = GNUNET_FS_uri_dup (uri);
410 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
411 sr->mandatory_missing = sc->mandatory_count;
413 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
414 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
418 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
420 /* check if mandatory satisfied */
422 sr->mandatory_missing--;
424 sr->optional_support++;
425 if (0 != sr->mandatory_missing)
428 notify_client_chk_result (sc, sr);
430 notify_client_chk_update (sc, sr);
431 GNUNET_FS_search_result_sync_ (sr);
432 GNUNET_FS_search_start_probe_ (sr);
437 * Start search for content, internal API.
439 * @param h handle to the file sharing subsystem
440 * @param uri specifies the search parameters; can be
441 * a KSK URI or an SKS URI.
442 * @param anonymity desired level of anonymity
443 * @param options options for the search
444 * @param cctx client context
445 * @param psearch parent search result (for namespace update searches)
446 * @return context that can be used to control the search
448 static struct GNUNET_FS_SearchContext *
449 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
450 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
451 void *cctx, struct GNUNET_FS_SearchResult *psearch);
455 * We have received an SKS result. Start searching for updates and
456 * notify the client if it is a new result.
458 * @param sc context for the overall query
459 * @param id_update identifier for updates, NULL for none
460 * @param uri the URI that was found
461 * @param meta metadata associated with the URI
464 process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
465 const struct GNUNET_FS_Uri *uri,
466 const struct GNUNET_CONTAINER_MetaData *meta)
468 struct GNUNET_FS_Uri uu;
470 struct GNUNET_FS_SearchResult *sr;
473 GNUNET_FS_uri_to_key (uri, &key);
474 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
477 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
478 &test_result_present,
480 return; /* duplicate result */
481 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
483 sr->uri = GNUNET_FS_uri_dup (uri);
484 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
486 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
488 GNUNET_FS_search_result_sync_ (sr);
489 GNUNET_FS_search_start_probe_ (sr);
491 notify_client_chk_result (sc, sr);
492 /* search for updates */
493 if (strlen (id_update) == 0)
494 return; /* no updates */
496 uu.data.sks.namespace = sc->uri->data.sks.namespace;
497 uu.data.sks.identifier = GNUNET_strdup (id_update);
498 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
499 GNUNET_free (uu.data.sks.identifier);
504 * Process a keyword-search result.
506 * @param sc our search context
507 * @param kb the kblock
508 * @param size size of kb
511 process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
517 char pt[size - sizeof (struct KBlock)];
518 struct GNUNET_CRYPTO_AesSessionKey skey;
519 struct GNUNET_CRYPTO_AesInitializationVector iv;
521 struct GNUNET_CONTAINER_MetaData *meta;
522 struct GNUNET_FS_Uri *uri;
525 GNUNET_CRYPTO_hash (&kb->keyspace,
526 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
529 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
530 if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
532 if (i == sc->uri->data.ksk.keywordCount)
534 /* oops, does not match any of our keywords!? */
539 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
541 GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey,
548 eos = memchr (pt, 0, sizeof (pt));
555 if (sizeof (pt) == j)
556 meta = GNUNET_CONTAINER_meta_data_create ();
558 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
561 GNUNET_break_op (0); /* kblock malformed */
564 uri = GNUNET_FS_uri_parse (pt, &emsg);
567 GNUNET_break_op (0); /* kblock malformed */
568 GNUNET_free_non_null (emsg);
569 GNUNET_CONTAINER_meta_data_destroy (meta);
573 process_ksk_result (sc, &sc->requests[i], uri, meta);
576 GNUNET_CONTAINER_meta_data_destroy (meta);
577 GNUNET_FS_uri_destroy (uri);
582 * Process a keyword-search result with a namespace advertisment.
584 * @param sc our search context
585 * @param nb the nblock
586 * @param size size of nb
589 process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb,
595 char pt[size - sizeof (struct NBlock)];
596 struct GNUNET_CRYPTO_AesSessionKey skey;
597 struct GNUNET_CRYPTO_AesInitializationVector iv;
599 struct GNUNET_CONTAINER_MetaData *meta;
600 struct GNUNET_FS_Uri *uri;
603 GNUNET_CRYPTO_hash (&nb->keyspace,
604 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
607 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
608 if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
610 if (i == sc->uri->data.ksk.keywordCount)
612 /* oops, does not match any of our keywords!? */
617 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
619 GNUNET_CRYPTO_aes_decrypt (&nb[1], size - sizeof (struct NBlock), &skey,
626 eos = memchr (pt, 0, sizeof (pt));
633 if (sizeof (pt) == j)
634 meta = GNUNET_CONTAINER_meta_data_create ();
636 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
639 GNUNET_break_op (0); /* nblock malformed */
643 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
645 uri->data.sks.identifier = GNUNET_strdup (pt);
646 GNUNET_CRYPTO_hash (&nb->subspace,
647 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
648 &uri->data.sks.namespace);
649 uris = GNUNET_FS_uri_to_string (uri);
650 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_URI,
651 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
652 uris, strlen (uris) + 1);
654 GNUNET_PSEUDONYM_add (sc->h->cfg, &uri->data.sks.namespace, meta);
656 process_ksk_result (sc, &sc->requests[i], uri, meta);
659 GNUNET_CONTAINER_meta_data_destroy (meta);
660 GNUNET_FS_uri_destroy (uri);
665 * Process a namespace-search result.
667 * @param sc our search context
668 * @param sb the sblock
669 * @param size size of sb
672 process_sblock (struct GNUNET_FS_SearchContext *sc, const struct SBlock *sb,
675 size_t len = size - sizeof (struct SBlock);
677 struct GNUNET_CRYPTO_AesSessionKey skey;
678 struct GNUNET_CRYPTO_AesInitializationVector iv;
679 struct GNUNET_FS_Uri *uri;
680 struct GNUNET_CONTAINER_MetaData *meta;
689 identifier = sc->uri->data.sks.identifier;
690 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
691 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
692 if (-1 == GNUNET_CRYPTO_aes_decrypt (&sb[1], len, &skey, &iv, pt))
698 off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris);
701 GNUNET_break_op (0); /* sblock malformed */
704 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off);
707 GNUNET_break_op (0); /* sblock malformed */
710 uri = GNUNET_FS_uri_parse (uris, &emsg);
713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse URI `%s': %s\n", uris,
715 GNUNET_break_op (0); /* sblock malformed */
716 GNUNET_free_non_null (emsg);
717 GNUNET_CONTAINER_meta_data_destroy (meta);
721 process_sks_result (sc, id, uri, meta);
723 GNUNET_FS_uri_destroy (uri);
724 GNUNET_CONTAINER_meta_data_destroy (meta);
729 * Process a search result.
731 * @param sc our search context
732 * @param type type of the result
733 * @param expiration when it will expire
734 * @param data the (encrypted) response
735 * @param size size of data
738 process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
739 struct GNUNET_TIME_Absolute expiration, const void *data,
742 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Result received has already expired.\n");
746 return; /* result expired */
750 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
751 if (!GNUNET_FS_uri_test_ksk (sc->uri))
756 if (sizeof (struct KBlock) > size)
761 process_kblock (sc, data, size);
763 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
764 if (!GNUNET_FS_uri_test_sks (sc->uri))
769 if (sizeof (struct SBlock) > size)
774 process_sblock (sc, data, size);
776 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
777 if (!GNUNET_FS_uri_test_ksk (sc->uri))
782 if (sizeof (struct NBlock) > size)
787 process_nblock (sc, data, size);
789 case GNUNET_BLOCK_TYPE_ANY:
792 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
795 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
798 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
802 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
803 _("Got result with unknown block type `%d', ignoring"), type);
810 * Shutdown any existing connection to the FS
811 * service and try to establish a fresh one
812 * (and then re-transmit our search request).
814 * @param sc the search to reconnec
817 try_reconnect (struct GNUNET_FS_SearchContext *sc);
821 * Type of a function to call when we receive a message
825 * @param msg message received, NULL on timeout or fatal error
828 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
830 struct GNUNET_FS_SearchContext *sc = cls;
831 const struct PutMessage *cm;
834 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
835 (ntohs (msg->size) <= sizeof (struct PutMessage)))
840 msize = ntohs (msg->size);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Receiving %u bytes of result from fs service\n", msize);
843 cm = (const struct PutMessage *) msg;
844 process_result (sc, ntohl (cm->type),
845 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
846 msize - sizeof (struct PutMessage));
847 /* continue receiving */
848 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
849 GNUNET_TIME_UNIT_FOREVER_REL);
854 * Schedule the transmission of the (next) search request
857 * @param sc context for the search
860 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
864 * Closure for 'build_result_set'.
866 struct MessageBuilderContext
869 * How many entries can we store to xoff.
871 unsigned int put_cnt;
874 * How many entries should we skip.
876 unsigned int skip_cnt;
879 * Where to store the keys.
881 GNUNET_HashCode *xoff;
884 * Search context we are iterating for.
886 struct GNUNET_FS_SearchContext *sc;
889 * URI the search result must match, NULL for any
891 struct GNUNET_FS_Uri *uri;
896 * Iterating over the known results, pick those matching the given
897 * result range and store their keys at 'xoff'.
899 * @param cls the 'struct MessageBuilderContext'
900 * @param key key for a result
901 * @param value the search result
902 * @return GNUNET_OK to continue iterating
905 build_result_set (void *cls, const GNUNET_HashCode * key, void *value)
907 struct MessageBuilderContext *mbc = cls;
908 struct GNUNET_FS_SearchResult *sr = value;
910 if ((mbc->uri != NULL) &&
911 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
913 if (mbc->skip_cnt > 0)
918 if (mbc->put_cnt == 0)
919 return GNUNET_SYSERR;
920 mbc->sc->search_request_map_offset++;
921 mbc->xoff[--mbc->put_cnt] = *key;
927 * Iterating over the known results, count those
928 * matching the given result range and increment
929 * put count for each.
931 * @param cls the 'struct MessageBuilderContext'
932 * @param key key for a result
933 * @param value the search result
934 * @return GNUNET_OK to continue iterating
937 find_result_set (void *cls, const GNUNET_HashCode * key, void *value)
939 struct MessageBuilderContext *mbc = cls;
940 struct GNUNET_FS_SearchResult *sr = value;
942 if ((mbc->uri != NULL) &&
943 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
951 * We're ready to transmit the search request to the
952 * file-sharing service. Do it.
955 * @param size number of bytes available in buf
956 * @param buf where the callee should write the message
957 * @return number of bytes written to buf
960 transmit_search_request (void *cls, size_t size, void *buf)
962 struct GNUNET_FS_SearchContext *sc = cls;
963 struct MessageBuilderContext mbc;
965 struct SearchMessage *sm;
966 const char *identifier;
978 mbc.skip_cnt = sc->search_request_map_offset;
980 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
981 mbc.xoff = (GNUNET_HashCode *) & sm[1];
982 options = SEARCH_MESSAGE_OPTION_NONE;
983 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
984 options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
985 if (GNUNET_FS_uri_test_ksk (sc->uri))
987 msize = sizeof (struct SearchMessage);
988 GNUNET_assert (size >= msize);
991 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
992 &find_result_set, &mbc);
994 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
995 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
996 if (sc->search_request_map_offset < sqms)
997 GNUNET_assert (mbc.put_cnt > 0);
999 sm->header.size = htons (msize);
1000 sm->type = htonl (GNUNET_BLOCK_TYPE_ANY);
1001 sm->anonymity_level = htonl (sc->anonymity);
1002 memset (&sm->target, 0, sizeof (GNUNET_HashCode));
1003 sm->query = sc->requests[sc->keyword_offset].query;
1004 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1005 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1006 &build_result_set, &mbc);
1007 sm->header.size = htons (msize);
1008 if (sqms != sc->search_request_map_offset)
1010 /* more requesting to be done... */
1011 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1012 schedule_transmit_search_request (sc);
1015 sm->options = htonl (options);
1016 sc->keyword_offset++;
1017 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1019 /* more requesting to be done... */
1020 schedule_transmit_search_request (sc);
1026 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1027 msize = sizeof (struct SearchMessage);
1028 GNUNET_assert (size >= msize);
1029 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_SBLOCK);
1030 sm->anonymity_level = htonl (sc->anonymity);
1031 sm->target = sc->uri->data.sks.namespace;
1032 identifier = sc->uri->data.sks.identifier;
1033 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
1034 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &idh);
1035 GNUNET_CRYPTO_hash_xor (&idh, &sm->target, &sm->query);
1036 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
1037 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1038 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1040 if (sc->search_request_map_offset < sqms)
1041 GNUNET_assert (mbc.put_cnt > 0);
1042 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1043 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1044 &build_result_set, &mbc);
1045 sm->header.size = htons (msize);
1046 if (sqms != sc->search_request_map_offset)
1048 /* more requesting to be done... */
1049 sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1050 schedule_transmit_search_request (sc);
1053 sm->options = htonl (options);
1055 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1056 GNUNET_TIME_UNIT_FOREVER_REL);
1062 * Schedule the transmission of the (next) search request
1065 * @param sc context for the search
1068 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1074 size = sizeof (struct SearchMessage);
1076 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1077 sc->search_request_map_offset;
1078 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (GNUNET_HashCode);
1079 fit = GNUNET_MIN (fit, sqms);
1080 size += sizeof (GNUNET_HashCode) * fit;
1081 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1082 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1083 GNUNET_NO, &transmit_search_request, sc);
1089 * Reconnect to the FS service and transmit
1092 * @param cls our search context
1096 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1098 struct GNUNET_FS_SearchContext *sc = cls;
1099 struct GNUNET_CLIENT_Connection *client;
1101 sc->task = GNUNET_SCHEDULER_NO_TASK;
1102 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1108 sc->client = client;
1109 sc->search_request_map_offset = 0;
1110 sc->keyword_offset = 0;
1111 schedule_transmit_search_request (sc);
1116 * Shutdown any existing connection to the FS
1117 * service and try to establish a fresh one
1118 * (and then re-transmit our search request).
1120 * @param sc the search to reconnec
1123 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1125 if (NULL != sc->client)
1127 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1131 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1137 * Start search for content, internal API.
1139 * @param h handle to the file sharing subsystem
1140 * @param uri specifies the search parameters; can be
1141 * a KSK URI or an SKS URI.
1142 * @param anonymity desired level of anonymity
1143 * @param options options for the search
1144 * @param cctx initial value for the client context
1145 * @param psearch parent search result (for namespace update searches)
1146 * @return context that can be used to control the search
1148 static struct GNUNET_FS_SearchContext *
1149 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1150 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1151 void *cctx, struct GNUNET_FS_SearchResult *psearch)
1153 struct GNUNET_FS_SearchContext *sc;
1154 struct GNUNET_FS_ProgressInfo pi;
1156 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1158 sc->options = options;
1159 sc->uri = GNUNET_FS_uri_dup (uri);
1160 sc->anonymity = anonymity;
1161 sc->start_time = GNUNET_TIME_absolute_get ();
1162 if (psearch != NULL)
1164 sc->psearch_result = psearch;
1165 psearch->update_search = sc;
1167 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1168 sc->client_info = cctx;
1169 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1171 GNUNET_FS_uri_destroy (sc->uri);
1172 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1176 GNUNET_FS_search_sync_ (sc);
1177 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1178 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1184 * Build the request and actually initiate the search using the
1185 * GNUnet FS service.
1187 * @param sc search context
1188 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1191 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1194 const char *keyword;
1196 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1197 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1199 GNUNET_assert (NULL == sc->client);
1200 if (GNUNET_FS_uri_test_ksk (sc->uri))
1202 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1204 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1205 sc->uri->data.ksk.keywordCount);
1206 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1208 keyword = &sc->uri->data.ksk.keywords[i][1];
1209 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1210 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1211 GNUNET_assert (pk != NULL);
1212 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1213 GNUNET_CRYPTO_rsa_key_free (pk);
1214 GNUNET_CRYPTO_hash (&pub,
1216 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1217 &sc->requests[i].query);
1218 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1219 if (sc->requests[i].mandatory)
1220 sc->mandatory_count++;
1221 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1222 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].key);
1225 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1226 if (NULL == sc->client)
1227 return GNUNET_SYSERR;
1228 schedule_transmit_search_request (sc);
1234 * Freeze probes for the given search result.
1236 * @param cls the global FS handle
1237 * @param key the key for the search result (unused)
1238 * @param value the search result to free
1242 search_result_freeze_probes (void *cls, const GNUNET_HashCode * key,
1245 struct GNUNET_FS_SearchResult *sr = value;
1247 if (sr->probe_ctx != NULL)
1249 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1250 sr->probe_ctx = NULL;
1252 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1254 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1255 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1257 if (sr->update_search != NULL)
1258 GNUNET_FS_search_pause (sr->update_search);
1264 * Resume probes for the given search result.
1266 * @param cls the global FS handle
1267 * @param key the key for the search result (unused)
1268 * @param value the search result to free
1272 search_result_resume_probes (void *cls, const GNUNET_HashCode * key,
1275 struct GNUNET_FS_SearchResult *sr = value;
1277 GNUNET_FS_search_start_probe_ (sr);
1278 if (sr->update_search != NULL)
1279 GNUNET_FS_search_continue (sr->update_search);
1285 * Signal suspend and free the given search result.
1287 * @param cls the global FS handle
1288 * @param key the key for the search result (unused)
1289 * @param value the search result to free
1293 search_result_suspend (void *cls, const GNUNET_HashCode * key, void *value)
1295 struct GNUNET_FS_SearchContext *sc = cls;
1296 struct GNUNET_FS_SearchResult *sr = value;
1297 struct GNUNET_FS_ProgressInfo pi;
1299 if (sr->download != NULL)
1300 GNUNET_FS_download_signal_suspend_ (sr->download);
1301 if (sr->update_search != NULL)
1302 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1303 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1304 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1305 pi.value.search.specifics.result_suspend.meta = sr->meta;
1306 pi.value.search.specifics.result_suspend.uri = sr->uri;
1307 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1308 GNUNET_break (NULL == sr->client_info);
1309 GNUNET_free_non_null (sr->serialization);
1310 GNUNET_FS_uri_destroy (sr->uri);
1311 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1312 if (sr->probe_ctx != NULL)
1313 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1314 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1315 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1322 * Create SUSPEND event for the given search operation
1323 * and then clean up our state (without stop signal).
1325 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1328 GNUNET_FS_search_signal_suspend_ (void *cls)
1330 struct GNUNET_FS_SearchContext *sc = cls;
1331 struct GNUNET_FS_ProgressInfo pi;
1334 GNUNET_FS_end_top (sc->h, sc->top);
1335 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1336 &search_result_suspend, sc);
1337 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1338 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1339 GNUNET_break (NULL == sc->client_info);
1340 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1341 GNUNET_SCHEDULER_cancel (sc->task);
1342 if (NULL != sc->client)
1343 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1344 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1345 if (sc->requests != NULL)
1347 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1348 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1349 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1351 GNUNET_free_non_null (sc->requests);
1352 GNUNET_free_non_null (sc->emsg);
1353 GNUNET_FS_uri_destroy (sc->uri);
1354 GNUNET_free_non_null (sc->serialization);
1360 * Start search for content.
1362 * @param h handle to the file sharing subsystem
1363 * @param uri specifies the search parameters; can be
1364 * a KSK URI or an SKS URI.
1365 * @param anonymity desired level of anonymity
1366 * @param options options for the search
1367 * @param cctx initial value for the client context
1368 * @return context that can be used to control the search
1370 struct GNUNET_FS_SearchContext *
1371 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1372 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1373 enum GNUNET_FS_SearchOptions options, void *cctx)
1375 struct GNUNET_FS_SearchContext *ret;
1377 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1380 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1388 * @param sc context for the search that should be paused
1391 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1393 struct GNUNET_FS_ProgressInfo pi;
1395 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1396 GNUNET_SCHEDULER_cancel (sc->task);
1397 sc->task = GNUNET_SCHEDULER_NO_TASK;
1398 if (NULL != sc->client)
1399 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1401 GNUNET_FS_search_sync_ (sc);
1402 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1403 &search_result_freeze_probes, sc);
1404 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1405 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1410 * Continue paused search.
1412 * @param sc context for the search that should be resumed
1415 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1417 struct GNUNET_FS_ProgressInfo pi;
1419 GNUNET_assert (sc->client == NULL);
1420 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1421 do_reconnect (sc, NULL);
1422 GNUNET_FS_search_sync_ (sc);
1423 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1424 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1425 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1426 &search_result_resume_probes, sc);
1431 * Free the given search result.
1433 * @param cls the global FS handle
1434 * @param key the key for the search result (unused)
1435 * @param value the search result to free
1439 search_result_free (void *cls, const GNUNET_HashCode * key, void *value)
1441 struct GNUNET_FS_SearchContext *sc = cls;
1442 struct GNUNET_FS_SearchResult *sr = value;
1443 struct GNUNET_FS_ProgressInfo pi;
1445 if (NULL != sr->download)
1447 sr->download->search = NULL;
1449 GNUNET_FS_make_top (sr->download->h,
1450 &GNUNET_FS_download_signal_suspend_, sr->download);
1451 if (NULL != sr->download->serialization)
1453 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1454 sr->download->serialization);
1455 GNUNET_free (sr->download->serialization);
1456 sr->download->serialization = NULL;
1458 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1459 GNUNET_FS_download_make_status_ (&pi, sr->download);
1460 GNUNET_FS_download_sync_ (sr->download);
1461 sr->download = NULL;
1463 if (NULL != sr->update_search)
1465 GNUNET_FS_search_stop (sr->update_search);
1466 GNUNET_assert (sr->update_search == NULL);
1468 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1469 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1470 pi.value.search.specifics.result_stopped.meta = sr->meta;
1471 pi.value.search.specifics.result_stopped.uri = sr->uri;
1472 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1473 GNUNET_break (NULL == sr->client_info);
1474 GNUNET_free_non_null (sr->serialization);
1475 GNUNET_FS_uri_destroy (sr->uri);
1476 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1477 if (sr->probe_ctx != NULL)
1478 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1479 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1480 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1487 * Stop search for content.
1489 * @param sc context for the search that should be stopped
1492 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1494 struct GNUNET_FS_ProgressInfo pi;
1497 if (sc->top != NULL)
1498 GNUNET_FS_end_top (sc->h, sc->top);
1499 if (sc->psearch_result != NULL)
1500 sc->psearch_result->update_search = NULL;
1501 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1502 &search_result_free, sc);
1503 if (sc->serialization != NULL)
1505 GNUNET_FS_remove_sync_file_ (sc->h,
1506 (sc->psearch_result !=
1507 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1508 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1510 GNUNET_FS_remove_sync_dir_ (sc->h,
1511 (sc->psearch_result !=
1512 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1513 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1515 GNUNET_free (sc->serialization);
1517 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1518 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1519 GNUNET_break (NULL == sc->client_info);
1520 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1521 GNUNET_SCHEDULER_cancel (sc->task);
1522 if (NULL != sc->client)
1523 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1524 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1525 if (sc->requests != NULL)
1527 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1528 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1529 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1531 GNUNET_free_non_null (sc->requests);
1532 GNUNET_free_non_null (sc->emsg);
1533 GNUNET_FS_uri_destroy (sc->uri);
1537 /* end of fs_search.c */