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_NO
36 * Fill in all of the generic fields for a search event and
39 * @param pi structure to fill in
40 * @param sc overall search context
41 * @return value returned by the callback
44 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
45 struct GNUNET_FS_SearchContext *sc)
49 pi->value.search.sc = sc;
50 pi->value.search.cctx = sc->client_info;
51 pi->value.search.pctx =
52 (sc->psearch_result == NULL) ? NULL : sc->psearch_result->client_info;
53 pi->value.search.query = sc->uri;
54 pi->value.search.duration =
55 GNUNET_TIME_absolute_get_duration (sc->start_time);
56 pi->value.search.anonymity = sc->anonymity;
57 ret = sc->h->upcb (sc->h->upcb_cls, pi);
63 * Check if the given result is identical
66 * @param cls points to the URI we check against
68 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
70 * @return GNUNET_SYSERR if the result is present,
74 test_result_present (void *cls, const GNUNET_HashCode * key, void *value)
76 const struct GNUNET_FS_Uri *uri = cls;
77 struct GNUNET_FS_SearchResult *sr = value;
79 if (GNUNET_FS_uri_test_equal (uri, sr->uri))
86 * We've found a new CHK result. Let the client
89 * @param sc the search context
90 * @param sr the specific result
93 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
94 struct GNUNET_FS_SearchResult *sr)
96 struct GNUNET_FS_ProgressInfo pi;
98 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
99 pi.value.search.specifics.result.meta = sr->meta;
100 pi.value.search.specifics.result.uri = sr->uri;
101 pi.value.search.specifics.result.result = sr;
102 pi.value.search.specifics.result.applicability_rank = sr->optional_support;
103 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
108 * We've found new information about an existing CHK result. Let the
109 * client know about it.
111 * @param sc the search context
112 * @param sr the specific result
115 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
116 struct GNUNET_FS_SearchResult *sr)
118 struct GNUNET_FS_ProgressInfo pi;
120 pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
121 pi.value.search.specifics.update.cctx = sr->client_info;
122 pi.value.search.specifics.update.meta = sr->meta;
123 pi.value.search.specifics.update.uri = sr->uri;
124 pi.value.search.specifics.update.availability_rank =
125 2 * sr->availability_success - sr->availability_trials;
126 pi.value.search.specifics.update.availability_certainty =
127 sr->availability_trials;
128 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
129 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
134 * Context for "get_result_present".
136 struct GetResultContext
139 * The URI we're looking for.
141 const struct GNUNET_FS_Uri *uri;
144 * Where to store a pointer to the search
145 * result struct if we found a match.
147 struct GNUNET_FS_SearchResult *sr;
152 * Check if the given result is identical to the given URI and if so
155 * @param cls a "struct GetResultContext"
156 * @param key not used
157 * @param value a "struct GNUNET_FS_SearchResult" who's URI we
158 * should compare with
162 get_result_present (void *cls, const GNUNET_HashCode * key, void *value)
164 struct GetResultContext *grc = cls;
165 struct GNUNET_FS_SearchResult *sr = value;
167 if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
174 * Signal result of last probe to client and then schedule next
178 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
180 struct GNUNET_FS_ProgressInfo pi;
182 pi.status = GNUNET_FS_STATUS_SEARCH_START;
183 pi.value.search.specifics.update.cctx = sr->client_info;
184 pi.value.search.specifics.update.meta = sr->meta;
185 pi.value.search.specifics.update.uri = sr->uri;
186 pi.value.search.specifics.update.availability_rank = sr->availability_success;
187 pi.value.search.specifics.update.availability_certainty =
188 sr->availability_trials;
189 pi.value.search.specifics.update.applicability_rank = sr->optional_support;
190 sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
191 GNUNET_FS_search_start_probe_ (sr);
196 * Handle the case where we have failed to receive a response for our probe.
198 * @param cls our 'struct GNUNET_FS_SearchResult*'
199 * @param tc scheduler context
202 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 struct GNUNET_FS_SearchResult *sr = cls;
206 sr->availability_trials++;
207 GNUNET_FS_search_result_sync_ (sr);
208 signal_probe_result (sr);
213 * Handle the case where we have gotten a response for our probe.
215 * @param cls our 'struct GNUNET_FS_SearchResult*'
216 * @param tc scheduler context
219 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 struct GNUNET_FS_SearchResult *sr = cls;
223 sr->availability_trials++;
224 sr->availability_success++;
225 GNUNET_FS_search_result_sync_ (sr);
226 signal_probe_result (sr);
231 * Notification of FS that a search probe has made progress.
232 * This function is used INSTEAD of the client's event handler
233 * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
235 * @param cls closure, always NULL (!), actual closure
236 * is in the client-context of the info struct
237 * @param info details about the event, specifying the event type
238 * and various bits about the event
239 * @return client-context (for the next progress call
240 * for this operation; should be set to NULL for
241 * SUSPEND and STOPPED events). The value returned
242 * will be passed to future callbacks in the respective
243 * field in the GNUNET_FS_ProgressInfo struct.
246 GNUNET_FS_search_probe_progress_ (void *cls,
247 const struct GNUNET_FS_ProgressInfo *info)
249 struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
250 struct GNUNET_TIME_Relative dur;
252 switch (info->status)
254 case GNUNET_FS_STATUS_DOWNLOAD_START:
257 case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
258 /* probes should never be resumed */
261 case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
262 /* probes should never be suspended */
265 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
268 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
269 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
271 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
272 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
274 sr->probe_cancel_task =
275 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
276 &probe_failure_handler, sr);
278 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
279 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
281 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
282 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
284 sr->probe_cancel_task =
285 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
286 &probe_success_handler, sr);
288 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
289 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
291 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
292 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
296 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
297 GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
298 sr->probe_active_time = GNUNET_TIME_absolute_get ();
299 sr->probe_cancel_task =
300 GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
301 &probe_failure_handler, sr);
303 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
304 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
306 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
307 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
309 dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
310 sr->remaining_probe_time =
311 GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
312 GNUNET_FS_search_result_sync_ (sr);
323 * Start download probes for the given search result.
325 * @param sr the search result
328 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
333 if (sr->probe_ctx != NULL)
335 if (sr->download != NULL)
337 if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
339 if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
341 len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
344 if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
346 off = len / DBLOCK_SIZE;
348 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
350 if (len - off < DBLOCK_SIZE)
354 sr->remaining_probe_time =
355 GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
356 2 * (1 + sr->availability_trials));
358 GNUNET_FS_download_start (sr->sc->h, sr->uri, sr->meta, NULL, NULL, off,
359 len, sr->sc->anonymity,
360 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
361 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
366 * We have received a KSK result. Check how it fits in with the
367 * overall query and notify the client accordingly.
369 * @param sc context for the overall query
370 * @param ent entry for the specific keyword
371 * @param uri the URI that was found
372 * @param meta metadata associated with the URI
373 * under the "ent" keyword
376 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
377 struct SearchRequestEntry *ent,
378 const struct GNUNET_FS_Uri *uri,
379 const struct GNUNET_CONTAINER_MetaData *meta)
382 struct GNUNET_FS_SearchResult *sr;
383 struct GetResultContext grc;
387 GNUNET_FS_uri_to_key (uri, &key);
389 GNUNET_CONTAINER_multihashmap_get_multiple (ent->results, &key,
390 &test_result_present,
392 return; /* duplicate result */
393 /* try to find search result in master map */
396 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
397 &get_result_present, &grc);
399 is_new = (NULL == sr) || (sr->mandatory_missing > 0);
402 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
404 sr->uri = GNUNET_FS_uri_dup (uri);
405 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
406 sr->mandatory_missing = sc->mandatory_count;
408 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
413 GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
415 /* check if mandatory satisfied */
417 sr->mandatory_missing--;
419 sr->optional_support++;
420 if (0 != sr->mandatory_missing)
423 notify_client_chk_result (sc, sr);
425 notify_client_chk_update (sc, sr);
426 GNUNET_FS_search_result_sync_ (sr);
427 GNUNET_FS_search_start_probe_ (sr);
432 * Start search for content, internal API.
434 * @param h handle to the file sharing subsystem
435 * @param uri specifies the search parameters; can be
436 * a KSK URI or an SKS URI.
437 * @param anonymity desired level of anonymity
438 * @param options options for the search
439 * @param cctx client context
440 * @param psearch parent search result (for namespace update searches)
441 * @return context that can be used to control the search
443 static struct GNUNET_FS_SearchContext *
444 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
445 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
446 void *cctx, struct GNUNET_FS_SearchResult *psearch);
450 * We have received an SKS result. Start searching for updates and
451 * notify the client if it is a new result.
453 * @param sc context for the overall query
454 * @param id_update identifier for updates, NULL for none
455 * @param uri the URI that was found
456 * @param meta metadata associated with the URI
459 process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
460 const struct GNUNET_FS_Uri *uri,
461 const struct GNUNET_CONTAINER_MetaData *meta)
463 struct GNUNET_FS_Uri uu;
465 struct GNUNET_FS_SearchResult *sr;
468 GNUNET_FS_uri_to_key (uri, &key);
469 GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
472 GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
473 &test_result_present,
475 return; /* duplicate result */
476 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
478 sr->uri = GNUNET_FS_uri_dup (uri);
479 sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
481 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
482 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
483 GNUNET_FS_search_result_sync_ (sr);
484 GNUNET_FS_search_start_probe_ (sr);
486 notify_client_chk_result (sc, sr);
487 /* search for updates */
488 if (strlen (id_update) == 0)
489 return; /* no updates */
491 uu.data.sks.namespace = sc->uri->data.sks.namespace;
492 uu.data.sks.identifier = GNUNET_strdup (id_update);
493 (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
494 GNUNET_free (uu.data.sks.identifier);
499 * Process a keyword-search result.
501 * @param sc our search context
502 * @param kb the kblock
503 * @param size size of kb
506 process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb,
512 char pt[size - sizeof (struct KBlock)];
513 struct GNUNET_CRYPTO_AesSessionKey skey;
514 struct GNUNET_CRYPTO_AesInitializationVector iv;
516 struct GNUNET_CONTAINER_MetaData *meta;
517 struct GNUNET_FS_Uri *uri;
520 GNUNET_CRYPTO_hash (&kb->keyspace,
521 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
524 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
525 if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
527 if (i == sc->uri->data.ksk.keywordCount)
529 /* oops, does not match any of our keywords!? */
534 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
536 GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey,
543 eos = memchr (pt, 0, sizeof (pt));
550 if (sizeof (pt) == j)
551 meta = GNUNET_CONTAINER_meta_data_create ();
553 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
556 GNUNET_break_op (0); /* kblock malformed */
559 uri = GNUNET_FS_uri_parse (pt, &emsg);
562 GNUNET_break_op (0); /* kblock malformed */
563 GNUNET_free_non_null (emsg);
564 GNUNET_CONTAINER_meta_data_destroy (meta);
568 process_ksk_result (sc, &sc->requests[i], uri, meta);
571 GNUNET_CONTAINER_meta_data_destroy (meta);
572 GNUNET_FS_uri_destroy (uri);
577 * Process a keyword-search result with a namespace advertisment.
579 * @param sc our search context
580 * @param nb the nblock
581 * @param size size of nb
584 process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb,
590 char pt[size - sizeof (struct NBlock)];
591 struct GNUNET_CRYPTO_AesSessionKey skey;
592 struct GNUNET_CRYPTO_AesInitializationVector iv;
594 struct GNUNET_CONTAINER_MetaData *meta;
595 struct GNUNET_FS_Uri *uri;
598 GNUNET_CRYPTO_hash (&nb->keyspace,
599 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
602 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
603 if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode)))
605 if (i == sc->uri->data.ksk.keywordCount)
607 /* oops, does not match any of our keywords!? */
612 GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
614 GNUNET_CRYPTO_aes_decrypt (&nb[1], size - sizeof (struct NBlock), &skey,
621 eos = memchr (pt, 0, sizeof (pt));
628 if (sizeof (pt) == j)
629 meta = GNUNET_CONTAINER_meta_data_create ();
631 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
634 GNUNET_break_op (0); /* nblock malformed */
638 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
640 uri->data.sks.identifier = GNUNET_strdup (pt);
641 GNUNET_CRYPTO_hash (&nb->subspace,
642 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
643 &uri->data.sks.namespace);
644 uris = GNUNET_FS_uri_to_string (uri);
645 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_URI,
646 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
647 uris, strlen (uris) + 1);
649 GNUNET_PSEUDONYM_add (sc->h->cfg, &uri->data.sks.namespace, meta);
651 process_ksk_result (sc, &sc->requests[i], uri, meta);
654 GNUNET_CONTAINER_meta_data_destroy (meta);
655 GNUNET_FS_uri_destroy (uri);
660 * Process a namespace-search result.
662 * @param sc our search context
663 * @param sb the sblock
664 * @param size size of sb
667 process_sblock (struct GNUNET_FS_SearchContext *sc, const struct SBlock *sb,
670 size_t len = size - sizeof (struct SBlock);
672 struct GNUNET_CRYPTO_AesSessionKey skey;
673 struct GNUNET_CRYPTO_AesInitializationVector iv;
674 struct GNUNET_FS_Uri *uri;
675 struct GNUNET_CONTAINER_MetaData *meta;
684 identifier = sc->uri->data.sks.identifier;
685 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
686 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
687 if (-1 == GNUNET_CRYPTO_aes_decrypt (&sb[1], len, &skey, &iv, pt))
693 off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris);
696 GNUNET_break_op (0); /* sblock malformed */
699 meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off);
702 GNUNET_break_op (0); /* sblock malformed */
705 uri = GNUNET_FS_uri_parse (uris, &emsg);
708 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to parse URI `%s': %s\n", uris,
710 GNUNET_break_op (0); /* sblock malformed */
711 GNUNET_free_non_null (emsg);
712 GNUNET_CONTAINER_meta_data_destroy (meta);
716 process_sks_result (sc, id, uri, meta);
718 GNUNET_FS_uri_destroy (uri);
719 GNUNET_CONTAINER_meta_data_destroy (meta);
724 * Process a search result.
726 * @param sc our search context
727 * @param type type of the result
728 * @param expiration when it will expire
729 * @param data the (encrypted) response
730 * @param size size of data
733 process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
734 struct GNUNET_TIME_Absolute expiration, const void *data,
737 if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Result received has already expired.\n");
741 return; /* result expired */
745 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
746 if (!GNUNET_FS_uri_test_ksk (sc->uri))
751 if (sizeof (struct KBlock) > size)
756 process_kblock (sc, data, size);
758 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
759 if (!GNUNET_FS_uri_test_sks (sc->uri))
764 if (sizeof (struct SBlock) > size)
769 process_sblock (sc, data, size);
771 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
772 if (!GNUNET_FS_uri_test_ksk (sc->uri))
777 if (sizeof (struct NBlock) > size)
782 process_nblock (sc, data, size);
784 case GNUNET_BLOCK_TYPE_ANY:
787 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
790 case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
793 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
797 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
798 _("Got result with unknown block type `%d', ignoring"), type);
805 * Shutdown any existing connection to the FS
806 * service and try to establish a fresh one
807 * (and then re-transmit our search request).
809 * @param sc the search to reconnec
812 try_reconnect (struct GNUNET_FS_SearchContext *sc);
816 * Type of a function to call when we receive a message
820 * @param msg message received, NULL on timeout or fatal error
823 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
825 struct GNUNET_FS_SearchContext *sc = cls;
826 const struct PutMessage *cm;
829 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
830 (ntohs (msg->size) <= sizeof (struct PutMessage)))
835 msize = ntohs (msg->size);
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Receiving %u bytes of result from fs service\n", msize);
838 cm = (const struct PutMessage *) msg;
839 process_result (sc, ntohl (cm->type),
840 GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
841 msize - sizeof (struct PutMessage));
842 /* continue receiving */
843 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
844 GNUNET_TIME_UNIT_FOREVER_REL);
849 * Schedule the transmission of the (next) search request
852 * @param sc context for the search
855 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
859 * Closure for 'build_result_set'.
861 struct MessageBuilderContext
864 * How many entries can we store to xoff.
866 unsigned int put_cnt;
869 * How many entries should we skip.
871 unsigned int skip_cnt;
874 * Where to store the keys.
876 GNUNET_HashCode *xoff;
879 * Search context we are iterating for.
881 struct GNUNET_FS_SearchContext *sc;
884 * URI the search result must match, NULL for any
886 struct GNUNET_FS_Uri *uri;
891 * Iterating over the known results, pick those
892 * matching the given result range and store
893 * their keys at 'xoff'.
895 * @param cls the 'struct MessageBuilderContext'
896 * @param key key for a result
897 * @param value the search result
898 * @return GNUNET_OK to continue iterating
901 build_result_set (void *cls, const GNUNET_HashCode * key, void *value)
903 struct MessageBuilderContext *mbc = cls;
904 struct GNUNET_FS_SearchResult *sr = value;
906 if ((mbc->uri != NULL) &&
907 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
909 if (mbc->skip_cnt > 0)
914 if (mbc->put_cnt == 0)
915 return GNUNET_SYSERR;
916 mbc->sc->search_request_map_offset++;
917 mbc->xoff[--mbc->put_cnt] = *key;
923 * Iterating over the known results, count those
924 * matching the given result range and increment
925 * put count for each.
927 * @param cls the 'struct MessageBuilderContext'
928 * @param key key for a result
929 * @param value the search result
930 * @return GNUNET_OK to continue iterating
933 find_result_set (void *cls, const GNUNET_HashCode * key, void *value)
935 struct MessageBuilderContext *mbc = cls;
936 struct GNUNET_FS_SearchResult *sr = value;
938 if ((mbc->uri != NULL) &&
939 (GNUNET_YES != GNUNET_FS_uri_test_equal (mbc->uri, sr->uri)))
947 * We're ready to transmit the search request to the
948 * file-sharing service. Do it.
951 * @param size number of bytes available in buf
952 * @param buf where the callee should write the message
953 * @return number of bytes written to buf
956 transmit_search_request (void *cls, size_t size, void *buf)
958 struct GNUNET_FS_SearchContext *sc = cls;
959 struct MessageBuilderContext mbc;
961 struct SearchMessage *sm;
962 const char *identifier;
973 mbc.skip_cnt = sc->search_request_map_offset;
975 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
976 mbc.xoff = (GNUNET_HashCode *) & sm[1];
977 if (GNUNET_FS_uri_test_ksk (sc->uri))
979 msize = sizeof (struct SearchMessage);
980 GNUNET_assert (size >= msize);
983 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
984 &find_result_set, &mbc);
986 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
987 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
988 if (sc->search_request_map_offset < sqms)
989 GNUNET_assert (mbc.put_cnt > 0);
991 sm->header.size = htons (msize);
992 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
993 sm->options = htonl (1);
995 sm->options = htonl (0);
996 sm->type = htonl (GNUNET_BLOCK_TYPE_ANY);
997 sm->anonymity_level = htonl (sc->anonymity);
998 memset (&sm->target, 0, sizeof (GNUNET_HashCode));
999 sm->query = sc->requests[sc->keyword_offset].query;
1000 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1001 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1002 &build_result_set, &mbc);
1003 sm->header.size = htons (msize);
1004 if (sqms != sc->search_request_map_offset)
1006 /* more requesting to be done... */
1007 schedule_transmit_search_request (sc);
1010 sc->keyword_offset++;
1011 if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1013 /* more requesting to be done... */
1014 schedule_transmit_search_request (sc);
1020 GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1021 msize = sizeof (struct SearchMessage);
1022 GNUNET_assert (size >= msize);
1023 if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1024 sm->options = htonl (1);
1026 sm->options = htonl (0);
1027 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_SBLOCK);
1028 sm->anonymity_level = htonl (sc->anonymity);
1029 sm->target = sc->uri->data.sks.namespace;
1030 identifier = sc->uri->data.sks.identifier;
1031 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &key);
1032 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &idh);
1033 GNUNET_CRYPTO_hash_xor (&idh, &sm->target, &sm->query);
1034 mbc.put_cnt = (size - msize) / sizeof (GNUNET_HashCode);
1035 sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1036 mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1038 if (sc->search_request_map_offset < sqms)
1039 GNUNET_assert (mbc.put_cnt > 0);
1040 msize += sizeof (GNUNET_HashCode) * mbc.put_cnt;
1041 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1042 &build_result_set, &mbc);
1043 sm->header.size = htons (msize);
1044 if (sqms != sc->search_request_map_offset)
1046 /* more requesting to be done... */
1047 schedule_transmit_search_request (sc);
1051 GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1052 GNUNET_TIME_UNIT_FOREVER_REL);
1058 * Schedule the transmission of the (next) search request
1061 * @param sc context for the search
1064 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1070 size = sizeof (struct SearchMessage);
1072 GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1073 sc->search_request_map_offset;
1074 fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (GNUNET_HashCode);
1075 fit = GNUNET_MIN (fit, sqms);
1076 size += sizeof (GNUNET_HashCode) * fit;
1077 GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1078 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1079 GNUNET_NO, &transmit_search_request, sc);
1085 * Reconnect to the FS service and transmit
1088 * @param cls our search context
1092 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1094 struct GNUNET_FS_SearchContext *sc = cls;
1095 struct GNUNET_CLIENT_Connection *client;
1097 sc->task = GNUNET_SCHEDULER_NO_TASK;
1098 client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1104 sc->client = client;
1105 sc->search_request_map_offset = 0;
1106 sc->keyword_offset = 0;
1107 schedule_transmit_search_request (sc);
1112 * Shutdown any existing connection to the FS
1113 * service and try to establish a fresh one
1114 * (and then re-transmit our search request).
1116 * @param sc the search to reconnec
1119 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1121 if (NULL != sc->client)
1123 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1127 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1133 * Start search for content, internal API.
1135 * @param h handle to the file sharing subsystem
1136 * @param uri specifies the search parameters; can be
1137 * a KSK URI or an SKS URI.
1138 * @param anonymity desired level of anonymity
1139 * @param options options for the search
1140 * @param cctx initial value for the client context
1141 * @param psearch parent search result (for namespace update searches)
1142 * @return context that can be used to control the search
1144 static struct GNUNET_FS_SearchContext *
1145 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1146 uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1147 void *cctx, struct GNUNET_FS_SearchResult *psearch)
1149 struct GNUNET_FS_SearchContext *sc;
1150 struct GNUNET_FS_ProgressInfo pi;
1152 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1154 sc->options = options;
1155 sc->uri = GNUNET_FS_uri_dup (uri);
1156 sc->anonymity = anonymity;
1157 sc->start_time = GNUNET_TIME_absolute_get ();
1158 if (psearch != NULL)
1160 sc->psearch_result = psearch;
1161 psearch->update_search = sc;
1163 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1164 sc->client_info = cctx;
1165 if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1167 GNUNET_FS_uri_destroy (sc->uri);
1168 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1172 GNUNET_FS_search_sync_ (sc);
1173 pi.status = GNUNET_FS_STATUS_SEARCH_START;
1174 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1180 * Build the request and actually initiate the search using the
1181 * GNUnet FS service.
1183 * @param sc search context
1184 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1187 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1190 const char *keyword;
1192 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
1193 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1195 GNUNET_assert (NULL == sc->client);
1196 if (GNUNET_FS_uri_test_ksk (sc->uri))
1198 GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1200 GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1201 sc->uri->data.ksk.keywordCount);
1202 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1204 keyword = &sc->uri->data.ksk.keywords[i][1];
1205 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1206 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1207 GNUNET_assert (pk != NULL);
1208 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1209 GNUNET_CRYPTO_rsa_key_free (pk);
1210 GNUNET_CRYPTO_hash (&pub,
1212 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1213 &sc->requests[i].query);
1214 sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1215 if (sc->requests[i].mandatory)
1216 sc->mandatory_count++;
1217 sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1218 GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].key);
1221 sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1222 if (NULL == sc->client)
1223 return GNUNET_SYSERR;
1224 schedule_transmit_search_request (sc);
1230 * Freeze probes for the given search result.
1232 * @param cls the global FS handle
1233 * @param key the key for the search result (unused)
1234 * @param value the search result to free
1238 search_result_freeze_probes (void *cls, const GNUNET_HashCode * key,
1241 struct GNUNET_FS_SearchResult *sr = value;
1243 if (sr->probe_ctx != NULL)
1245 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1246 sr->probe_ctx = NULL;
1248 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1250 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1251 sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1253 if (sr->update_search != NULL)
1254 GNUNET_FS_search_pause (sr->update_search);
1260 * Resume probes for the given search result.
1262 * @param cls the global FS handle
1263 * @param key the key for the search result (unused)
1264 * @param value the search result to free
1268 search_result_resume_probes (void *cls, const GNUNET_HashCode * key,
1271 struct GNUNET_FS_SearchResult *sr = value;
1273 GNUNET_FS_search_start_probe_ (sr);
1274 if (sr->update_search != NULL)
1275 GNUNET_FS_search_continue (sr->update_search);
1281 * Signal suspend and free the given search result.
1283 * @param cls the global FS handle
1284 * @param key the key for the search result (unused)
1285 * @param value the search result to free
1289 search_result_suspend (void *cls, const GNUNET_HashCode * key, void *value)
1291 struct GNUNET_FS_SearchContext *sc = cls;
1292 struct GNUNET_FS_SearchResult *sr = value;
1293 struct GNUNET_FS_ProgressInfo pi;
1295 if (sr->download != NULL)
1296 GNUNET_FS_download_signal_suspend_ (sr->download);
1297 if (sr->update_search != NULL)
1298 GNUNET_FS_search_signal_suspend_ (sr->update_search);
1299 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1300 pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1301 pi.value.search.specifics.result_suspend.meta = sr->meta;
1302 pi.value.search.specifics.result_suspend.uri = sr->uri;
1303 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1304 GNUNET_break (NULL == sr->client_info);
1305 GNUNET_free_non_null (sr->serialization);
1306 GNUNET_FS_uri_destroy (sr->uri);
1307 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1308 if (sr->probe_ctx != NULL)
1309 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1310 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1311 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1318 * Create SUSPEND event for the given search operation
1319 * and then clean up our state (without stop signal).
1321 * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1324 GNUNET_FS_search_signal_suspend_ (void *cls)
1326 struct GNUNET_FS_SearchContext *sc = cls;
1327 struct GNUNET_FS_ProgressInfo pi;
1330 GNUNET_FS_end_top (sc->h, sc->top);
1331 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1332 &search_result_suspend, sc);
1333 pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1334 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1335 GNUNET_break (NULL == sc->client_info);
1336 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1337 GNUNET_SCHEDULER_cancel (sc->task);
1338 if (NULL != sc->client)
1339 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1340 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1341 if (sc->requests != NULL)
1343 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1344 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1345 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1347 GNUNET_free_non_null (sc->requests);
1348 GNUNET_free_non_null (sc->emsg);
1349 GNUNET_FS_uri_destroy (sc->uri);
1350 GNUNET_free_non_null (sc->serialization);
1356 * Start search for content.
1358 * @param h handle to the file sharing subsystem
1359 * @param uri specifies the search parameters; can be
1360 * a KSK URI or an SKS URI.
1361 * @param anonymity desired level of anonymity
1362 * @param options options for the search
1363 * @param cctx initial value for the client context
1364 * @return context that can be used to control the search
1366 struct GNUNET_FS_SearchContext *
1367 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1368 const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1369 enum GNUNET_FS_SearchOptions options, void *cctx)
1371 struct GNUNET_FS_SearchContext *ret;
1373 ret = search_start (h, uri, anonymity, options, cctx, NULL);
1376 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1384 * @param sc context for the search that should be paused
1387 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1389 struct GNUNET_FS_ProgressInfo pi;
1391 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1392 GNUNET_SCHEDULER_cancel (sc->task);
1393 sc->task = GNUNET_SCHEDULER_NO_TASK;
1394 if (NULL != sc->client)
1395 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1397 GNUNET_FS_search_sync_ (sc);
1398 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1399 &search_result_freeze_probes, sc);
1400 pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1401 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1406 * Continue paused search.
1408 * @param sc context for the search that should be resumed
1411 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1413 struct GNUNET_FS_ProgressInfo pi;
1415 GNUNET_assert (sc->client == NULL);
1416 GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1417 do_reconnect (sc, NULL);
1418 GNUNET_FS_search_sync_ (sc);
1419 pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1420 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1421 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1422 &search_result_resume_probes, sc);
1427 * Free the given search result.
1429 * @param cls the global FS handle
1430 * @param key the key for the search result (unused)
1431 * @param value the search result to free
1435 search_result_free (void *cls, const GNUNET_HashCode * key, void *value)
1437 struct GNUNET_FS_SearchContext *sc = cls;
1438 struct GNUNET_FS_SearchResult *sr = value;
1439 struct GNUNET_FS_ProgressInfo pi;
1441 if (NULL != sr->download)
1443 sr->download->search = NULL;
1445 GNUNET_FS_make_top (sr->download->h,
1446 &GNUNET_FS_download_signal_suspend_, sr->download);
1447 if (NULL != sr->download->serialization)
1449 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1450 sr->download->serialization);
1451 GNUNET_free (sr->download->serialization);
1452 sr->download->serialization = NULL;
1454 pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1455 GNUNET_FS_download_make_status_ (&pi, sr->download);
1456 GNUNET_FS_download_sync_ (sr->download);
1457 sr->download = NULL;
1459 if (NULL != sr->update_search)
1461 GNUNET_FS_search_stop (sr->update_search);
1462 GNUNET_assert (sr->update_search == NULL);
1464 pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1465 pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1466 pi.value.search.specifics.result_stopped.meta = sr->meta;
1467 pi.value.search.specifics.result_stopped.uri = sr->uri;
1468 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1469 GNUNET_break (NULL == sr->client_info);
1470 GNUNET_free_non_null (sr->serialization);
1471 GNUNET_FS_uri_destroy (sr->uri);
1472 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1473 if (sr->probe_ctx != NULL)
1474 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1475 if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1476 GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1483 * Stop search for content.
1485 * @param sc context for the search that should be stopped
1488 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1490 struct GNUNET_FS_ProgressInfo pi;
1493 if (sc->top != NULL)
1494 GNUNET_FS_end_top (sc->h, sc->top);
1495 if (sc->psearch_result != NULL)
1496 sc->psearch_result->update_search = NULL;
1497 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1498 &search_result_free, sc);
1499 if (sc->serialization != NULL)
1501 GNUNET_FS_remove_sync_file_ (sc->h,
1502 (sc->psearch_result !=
1503 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1504 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1506 GNUNET_FS_remove_sync_dir_ (sc->h,
1507 (sc->psearch_result !=
1508 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1509 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1511 GNUNET_free (sc->serialization);
1513 pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1514 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1515 GNUNET_break (NULL == sc->client_info);
1516 if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1517 GNUNET_SCHEDULER_cancel (sc->task);
1518 if (NULL != sc->client)
1519 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1520 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1521 if (sc->requests != NULL)
1523 GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1524 for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1525 GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1527 GNUNET_free_non_null (sc->requests);
1528 GNUNET_free_non_null (sc->emsg);
1529 GNUNET_FS_uri_destroy (sc->uri);
1533 /* end of fs_search.c */