-fix
[oweals/gnunet.git] / src / fs / fs_search.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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 Tem ple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file fs/fs_search.c
22  * @brief Helper functions for searching.
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_fs_service.h"
28 #include "gnunet_protocols.h"
29 #include "fs_api.h"
30
31
32 /**
33  * Number of availability trials we perform per search result.
34  */
35 #define AVAILABILITY_TRIALS_MAX 8
36
37 /**
38  * Fill in all of the generic fields for a search event and
39  * call the callback.
40  *
41  * @param pi structure to fill in
42  * @param h file-sharing handle
43  * @param sc overall search context
44  * @return value returned by the callback
45  */
46 void *
47 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
48                                struct GNUNET_FS_Handle *h,
49                                struct GNUNET_FS_SearchContext *sc)
50 {
51   void *ret;
52
53   pi->value.search.sc = sc;
54   pi->value.search.cctx = (NULL != sc) ? sc->client_info : NULL;
55   pi->value.search.pctx =
56     ((NULL == sc) || (NULL == sc->psearch_result)) 
57     ? NULL 
58     : sc->psearch_result->client_info;
59   pi->value.search.query = (NULL != sc) ? sc->uri : NULL;
60   pi->value.search.duration = (NULL != sc) 
61     ? GNUNET_TIME_absolute_get_duration (sc->start_time)
62     : GNUNET_TIME_UNIT_ZERO;
63   pi->value.search.anonymity = (NULL != sc) ? sc->anonymity : 0;
64   ret = h->upcb (h->upcb_cls, pi);
65   return ret;
66 }
67
68
69 /**
70  * Check if the given result is identical
71  * to the given URI.
72  *
73  * @param cls points to the URI we check against
74  * @param key not used
75  * @param value a "struct GNUNET_FS_SearchResult" who's URI we
76  *        should compare with
77  * @return GNUNET_SYSERR if the result is present,
78  *         GNUNET_OK otherwise
79  */
80 static int
81 test_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
82 {
83   const struct GNUNET_FS_Uri *uri = cls;
84   struct GNUNET_FS_SearchResult *sr = value;
85
86   if (GNUNET_FS_uri_test_equal (uri, sr->uri))
87     return GNUNET_SYSERR;
88   return GNUNET_OK;
89 }
90
91
92 /**
93  * We've found a new CHK result.  Let the client
94  * know about it.
95  *
96  * @param sc the search context
97  * @param sr the specific result
98  */
99 static void
100 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc,
101                           struct GNUNET_FS_SearchResult *sr)
102 {
103   struct GNUNET_FS_ProgressInfo pi;
104
105   pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
106   pi.value.search.specifics.result.meta = sr->meta;
107   pi.value.search.specifics.result.uri = sr->uri;
108   pi.value.search.specifics.result.result = sr;
109   pi.value.search.specifics.result.applicability_rank = sr->optional_support;
110   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
111 }
112
113
114 /**
115  * We've found new information about an existing CHK result.  Let the
116  * client know about it.
117  *
118  * @param sc the search context
119  * @param sr the specific result
120  */
121 static void
122 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc,
123                           struct GNUNET_FS_SearchResult *sr)
124 {
125   struct GNUNET_FS_ProgressInfo pi;
126  
127   pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
128   pi.value.search.specifics.update.cctx = sr->client_info;
129   pi.value.search.specifics.update.meta = sr->meta;
130   pi.value.search.specifics.update.uri = sr->uri;
131   pi.value.search.specifics.update.availability_rank =
132       2 * sr->availability_success - sr->availability_trials;
133   pi.value.search.specifics.update.availability_certainty =
134       sr->availability_trials;
135   pi.value.search.specifics.update.applicability_rank = sr->optional_support;
136   pi.value.search.specifics.update.current_probe_time 
137     = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
138   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
139 }
140
141
142 /**
143  * Context for "get_result_present".
144  */
145 struct GetResultContext
146 {
147   /**
148    * The URI we're looking for.
149    */
150   const struct GNUNET_FS_Uri *uri;
151
152   /**
153    * Where to store a pointer to the search
154    * result struct if we found a match.
155    */
156   struct GNUNET_FS_SearchResult *sr;
157 };
158
159
160 /**
161  * Check if the given result is identical to the given URI and if so
162  * return it.
163  *
164  * @param cls a "struct GetResultContext"
165  * @param key not used
166  * @param value a "struct GNUNET_FS_SearchResult" who's URI we
167  *        should compare with
168  * @return GNUNET_OK
169  */
170 static int
171 get_result_present (void *cls, const struct GNUNET_HashCode * key, void *value)
172 {
173   struct GetResultContext *grc = cls;
174   struct GNUNET_FS_SearchResult *sr = value;
175
176   if (GNUNET_FS_uri_test_equal (grc->uri, sr->uri))
177     grc->sr = sr;
178   return GNUNET_OK;
179 }
180
181
182 /**
183  * Signal result of last probe to client and then schedule next
184  * probe.
185  */
186 static void
187 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
188 {
189   struct GNUNET_FS_ProgressInfo pi;
190
191   pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
192   pi.value.search.specifics.update.cctx = sr->client_info;
193   pi.value.search.specifics.update.meta = sr->meta;
194   pi.value.search.specifics.update.uri = sr->uri;
195   pi.value.search.specifics.update.availability_rank 
196     = 2 * sr->availability_success - sr->availability_trials;
197   pi.value.search.specifics.update.availability_certainty 
198     = sr->availability_trials;
199   pi.value.search.specifics.update.applicability_rank = sr->optional_support;
200   pi.value.search.specifics.update.current_probe_time 
201     = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
202   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sr->sc);
203   GNUNET_FS_search_start_probe_ (sr);
204 }
205
206
207 /**
208  * Handle the case where we have failed to receive a response for our probe.
209  *
210  * @param cls our 'struct GNUNET_FS_SearchResult*'
211  * @param tc scheduler context
212  */
213 static void
214 probe_failure_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215 {
216   struct GNUNET_FS_SearchResult *sr = cls;
217
218   sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
219   sr->availability_trials++;
220   GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
221   sr->probe_ctx = NULL;
222   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
223   {
224     GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
225     sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
226   }
227   GNUNET_FS_search_result_sync_ (sr);
228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229               "Probe #%u for search result %p failed\n",
230               sr->availability_trials,
231               sr);
232   signal_probe_result (sr);
233 }
234
235
236 /**
237  * Handle the case where we have gotten a response for our probe.
238  *
239  * @param cls our 'struct GNUNET_FS_SearchResult*'
240  * @param tc scheduler context
241  */
242 static void
243 probe_success_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 {
245   struct GNUNET_FS_SearchResult *sr = cls;
246
247   sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
248   sr->availability_trials++;
249   sr->availability_success++;
250   GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
251   sr->probe_ctx = NULL;
252   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
253   {
254     GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
255     sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
256   }
257   GNUNET_FS_search_result_sync_ (sr);
258   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259               "Probe #%u for search result %p succeeded\n",
260               sr->availability_trials,
261               sr);
262   signal_probe_result (sr);
263 }
264
265
266 /**
267  * Notification of FS that a search probe has made progress.
268  * This function is used INSTEAD of the client's event handler
269  * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
270  *
271  * @param cls closure, always NULL (!), actual closure
272  *        is in the client-context of the info struct
273  * @param info details about the event, specifying the event type
274  *        and various bits about the event
275  * @return client-context (for the next progress call
276  *         for this operation; should be set to NULL for
277  *         SUSPEND and STOPPED events).  The value returned
278  *         will be passed to future callbacks in the respective
279  *         field in the GNUNET_FS_ProgressInfo struct.
280  */
281 void *
282 GNUNET_FS_search_probe_progress_ (void *cls,
283                                   const struct GNUNET_FS_ProgressInfo *info)
284 {
285   struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
286   struct GNUNET_TIME_Relative dur;
287
288   switch (info->status)
289   {
290   case GNUNET_FS_STATUS_DOWNLOAD_START:
291     /* ignore */
292     break;
293   case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
294     /* probes should never be resumed */
295     GNUNET_assert (0);
296     break;
297   case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
298     /* probes should never be suspended */
299     GNUNET_break (0);
300     break;
301   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
302     /* ignore */
303     break;
304   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
305     if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
306     {
307       GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
308       sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
309     }
310     sr->probe_cancel_task =
311         GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
312                                       &probe_failure_handler, sr);
313     break;
314   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
315     if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
316     {
317       GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
318       sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
319     }
320     sr->probe_cancel_task =
321         GNUNET_SCHEDULER_add_now (&probe_success_handler, sr);
322     break;
323   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
324     if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
325     {
326       GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
327       sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
328     }
329     sr = NULL;
330     break;
331   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
332     if (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task)
333     {
334       sr->probe_active_time = GNUNET_TIME_absolute_get ();
335       sr->probe_cancel_task =
336         GNUNET_SCHEDULER_add_delayed (sr->remaining_probe_time,
337                                       &probe_failure_handler, sr);
338     }
339     else
340     {
341       /* should only happen if the cancel task was already
342          created on 'DOWNLOAD_INACTIVE' as we were out of time */
343       GNUNET_break (0 == sr->remaining_probe_time.rel_value);
344     }
345     break;
346   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
347     if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
348     {
349       GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
350       sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
351     }
352     dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
353     sr->remaining_probe_time =
354         GNUNET_TIME_relative_subtract (sr->remaining_probe_time, dur);
355     if (0 == sr->remaining_probe_time.rel_value)
356       sr->probe_cancel_task =
357         GNUNET_SCHEDULER_add_now (&probe_failure_handler, sr);
358     GNUNET_FS_search_result_sync_ (sr);
359     break;
360   default:
361     GNUNET_break (0);
362     return NULL;
363   }
364   return sr;
365 }
366
367
368 /**
369  * Task run periodically to remind clients that a probe is active.
370  *
371  * @param cls the 'struct GNUNET_FS_SearchResult' that we are probing for
372  * @param tc scheduler context
373  */
374 static void
375 probe_ping_task (void *cls,
376                  const struct GNUNET_SCHEDULER_TaskContext *tc)
377 {
378   struct GNUNET_FS_SearchResult *sr = cls;
379
380   signal_probe_result (sr);
381   sr->probe_ping_task 
382     = GNUNET_SCHEDULER_add_delayed (GNUNET_FS_PROBE_UPDATE_FREQUENCY,
383                                     &probe_ping_task,
384                                     sr);
385 }
386
387
388 /**
389  * Start download probes for the given search result.
390  *
391  * @param sr the search result
392  */
393 void
394 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
395 {
396   uint64_t off;
397   uint64_t len;
398
399   if (NULL != sr->probe_ctx)
400     return;
401   if (NULL != sr->download)
402     return;
403   if (0 == (sr->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
404     return;
405   if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
406     return;
407   if ( (GNUNET_FS_URI_CHK != sr->uri->type) && (GNUNET_FS_URI_LOC != sr->uri->type))
408     return;
409   len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
410   if (0 == len)
411     return;
412   if ((len <= DBLOCK_SIZE) && (sr->availability_success > 0))
413     return;
414   off = len / DBLOCK_SIZE;
415   if (off > 0)
416     off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
417   off *= DBLOCK_SIZE;
418   if (len - off < DBLOCK_SIZE)
419     len = len - off;
420   else
421     len = DBLOCK_SIZE;
422   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423               "Starting probe #%u (at offset %llu) for search result %p\n",
424               sr->availability_trials + 1,
425               (unsigned long long) off,
426               sr);
427   sr->remaining_probe_time =
428       GNUNET_TIME_relative_multiply (sr->h->avg_block_latency,
429                                      2 * (1 + sr->availability_trials));
430   sr->probe_ctx =
431       GNUNET_FS_download_start (sr->h, sr->uri, sr->meta, NULL, NULL, off,
432                                 len, sr->anonymity,
433                                 GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
434                                 GNUNET_FS_DOWNLOAD_IS_PROBE, sr, NULL);
435   sr->probe_ping_task 
436     = GNUNET_SCHEDULER_add_now (&probe_ping_task,
437                                 sr);
438 }
439
440
441 /**
442  * Start download probes for the given search result.
443  *
444  * @param h file-sharing handle to use for the operation
445  * @param uri URI to probe
446  * @param meta meta data associated with the URI
447  * @param client_info client info pointer to use for associated events
448  * @param anonymity anonymity level to use for the probes
449  * @return the search result handle to access the probe activity
450  */
451 struct GNUNET_FS_SearchResult *
452 GNUNET_FS_probe (struct GNUNET_FS_Handle *h,
453                  const struct GNUNET_FS_Uri *uri,
454                  const struct GNUNET_CONTAINER_MetaData *meta,
455                  void *client_info,
456                  uint32_t anonymity)
457 {
458   struct GNUNET_FS_SearchResult *sr;
459
460   sr = GNUNET_new (struct GNUNET_FS_SearchResult);
461   sr->h = h;
462   sr->uri = GNUNET_FS_uri_dup (uri);
463   sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
464   sr->client_info = client_info;
465   sr->anonymity = anonymity; 
466   GNUNET_FS_search_start_probe_ (sr);
467   return sr;
468 }
469
470
471 /**
472  * Stop probing activity associated with a search result.
473  * 
474  * @param sr search result 
475  */
476 static void
477 GNUNET_FS_search_stop_probe_ (struct GNUNET_FS_SearchResult *sr)
478 {
479   if (NULL != sr->probe_ctx)
480   {
481     GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
482     sr->probe_ctx = NULL;
483   }
484   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
485   {
486     GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
487     sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
488   }
489   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
490   {
491     GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
492     sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
493   }
494 }
495
496
497 /**
498  * Stop probe activity.  Must ONLY be used on values
499  * returned from 'GNUNET_FS_probe'.
500  *
501  * @param sr search result to stop probing for (freed)
502  * @return the value of the 'client_info' pointer
503  */
504 void *
505 GNUNET_FS_probe_stop (struct GNUNET_FS_SearchResult *sr)
506 {
507   void *client_info;
508
509   GNUNET_assert (NULL == sr->sc);
510   GNUNET_FS_search_stop_probe_ (sr);
511   GNUNET_FS_uri_destroy (sr->uri);
512   GNUNET_CONTAINER_meta_data_destroy (sr->meta);
513   client_info = sr->client_info;
514   GNUNET_free (sr);
515   return client_info;
516 }
517
518
519 /**
520  * We have received a KSK result.  Check how it fits in with the
521  * overall query and notify the client accordingly.
522  *
523  * @param sc context for the overall query
524  * @param ent entry for the specific keyword
525  * @param uri the URI that was found
526  * @param meta metadata associated with the URI
527  *        under the "ent" keyword
528  */
529 static void
530 process_ksk_result (struct GNUNET_FS_SearchContext *sc,
531                     struct SearchRequestEntry *ent,
532                     const struct GNUNET_FS_Uri *uri,
533                     const struct GNUNET_CONTAINER_MetaData *meta)
534 {
535   struct GNUNET_HashCode key;
536   struct GNUNET_FS_SearchResult *sr;
537   struct GetResultContext grc;
538   int is_new;
539   unsigned int koff;
540
541   /* check if new */
542   GNUNET_assert (NULL != sc);
543   GNUNET_FS_uri_to_key (uri, &key);
544   if (GNUNET_SYSERR ==
545       GNUNET_CONTAINER_multihashmap_get_multiple (ent->results, &key,
546                                                   &test_result_present,
547                                                   (void *) uri))
548     return;                     /* duplicate result */
549   /* try to find search result in master map */
550   grc.sr = NULL;
551   grc.uri = uri;
552   GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
553                                               &get_result_present, &grc);
554   sr = grc.sr;
555   is_new = (NULL == sr) || (sr->mandatory_missing > 0);
556   if (NULL == sr)
557   {
558     sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
559     sr->h = sc->h;
560     sr->sc = sc;
561     sr->anonymity = sc->anonymity;
562     sr->uri = GNUNET_FS_uri_dup (uri);
563     sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
564     sr->mandatory_missing = sc->mandatory_count;
565     sr->key = key;
566     sr->keyword_bitmap = GNUNET_malloc ((sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
567     GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
568                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
569   }
570   else
571   {
572     GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
573   }
574   koff = ent - sc->requests;
575   GNUNET_assert ( (ent >= sc->requests) && (koff < sc->uri->data.ksk.keywordCount));
576   sr->keyword_bitmap[koff / 8] |= (1 << (koff % 8));
577   /* check if mandatory satisfied */
578   if (ent->mandatory)
579     sr->mandatory_missing--;
580   else
581     sr->optional_support++;
582   if (0 != sr->mandatory_missing)
583     return;
584   if (is_new)
585     notify_client_chk_result (sc, sr);
586   else
587     notify_client_chk_update (sc, sr);
588   GNUNET_FS_search_result_sync_ (sr);
589   GNUNET_FS_search_start_probe_ (sr);
590 }
591
592
593 /**
594  * Start search for content, internal API.
595  *
596  * @param h handle to the file sharing subsystem
597  * @param uri specifies the search parameters; can be
598  *        a KSK URI or an SKS URI.
599  * @param anonymity desired level of anonymity
600  * @param options options for the search
601  * @param cctx client context
602  * @param psearch parent search result (for namespace update searches)
603  * @return context that can be used to control the search
604  */
605 static struct GNUNET_FS_SearchContext *
606 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
607               uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
608               void *cctx, struct GNUNET_FS_SearchResult *psearch);
609
610
611 /**
612  * We have received an SKS result.  Start searching for updates and
613  * notify the client if it is a new result.
614  *
615  * @param sc context for the overall query
616  * @param id_update identifier for updates, NULL for none
617  * @param uri the URI that was found
618  * @param meta metadata associated with the URI
619   */
620 static void
621 process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update,
622                     const struct GNUNET_FS_Uri *uri,
623                     const struct GNUNET_CONTAINER_MetaData *meta)
624 {
625   struct GNUNET_FS_Uri uu;
626   struct GNUNET_HashCode key;
627   struct GNUNET_FS_SearchResult *sr;
628
629   /* check if new */
630   GNUNET_assert (NULL != sc);
631   GNUNET_FS_uri_to_key (uri, &key);
632   GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key, &uri->data.chk.chk.query,
633                           &key);
634   if (GNUNET_SYSERR ==
635       GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map, &key,
636                                                   &test_result_present,
637                                                   (void *) uri))
638     return;                     /* duplicate result */
639   sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
640   sr->h = sc->h;
641   sr->sc = sc;
642   sr->anonymity = sc->anonymity;
643   sr->uri = GNUNET_FS_uri_dup (uri);
644   sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
645   sr->key = key;
646   GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &key, sr,
647                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
648   GNUNET_FS_search_result_sync_ (sr);
649   GNUNET_FS_search_start_probe_ (sr);
650   /* notify client */
651   notify_client_chk_result (sc, sr);
652   /* search for updates */
653   if (0 == strlen (id_update))
654     return;                     /* no updates */
655   uu.type = GNUNET_FS_URI_SKS;
656   uu.data.sks.ns = sc->uri->data.sks.ns;
657   uu.data.sks.identifier = GNUNET_strdup (id_update);
658   (void) search_start (sc->h, &uu, sc->anonymity, sc->options, NULL, sr);
659   GNUNET_free (uu.data.sks.identifier);
660 }
661
662
663 /**
664  * Decrypt a ublock using a 'keyword' as the passphrase.  Given the
665  * KSK public key derived from the keyword, this function looks up
666  * the original keyword in the search context and decrypts the
667  * given ciphertext block.
668  *
669  * @param sc search context with the keywords
670  * @param verification_key public key to use to lookup the keyword
671  * @param edata encrypted data
672  * @param edata_size number of bytes in 'edata' (and 'data')
673  * @param data where to store the plaintext
674  * @return keyword index on success, GNUNET_SYSERR on error (no such 
675  *         keyword, internal error)
676  */
677 static int
678 decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc,
679                             const struct GNUNET_FS_PseudonymIdentifier *verification_key,
680                             const void *edata,
681                             size_t edata_size,
682                             char *data)
683
684   struct GNUNET_HashCode q;
685   struct GNUNET_CRYPTO_AesSessionKey skey;
686   struct GNUNET_CRYPTO_AesInitializationVector iv;
687   int i;
688
689   GNUNET_CRYPTO_hash (verification_key,
690                       sizeof (struct GNUNET_FS_PseudonymIdentifier),
691                       &q);
692   /* find key */
693   for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
694     if (0 == memcmp (&q, &sc->requests[i].uquery, sizeof (struct GNUNET_HashCode)))
695       break;
696   if (i == sc->uri->data.ksk.keywordCount)
697   {
698     /* oops, does not match any of our keywords!? */
699     GNUNET_break (0);
700     return GNUNET_SYSERR;
701   }
702   /* decrypt */
703   GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].ukey, &skey, &iv);
704   if (-1 ==
705       GNUNET_CRYPTO_aes_decrypt (edata, edata_size, &skey,
706                                  &iv, data))
707   {
708     GNUNET_break (0);
709     return GNUNET_SYSERR;
710   }
711   return i;
712 }
713
714
715 /**
716  * Process a keyword search result.  The actual type of block is
717  * a UBlock; we know it is a keyword search result because that's
718  * what we were searching for.
719  *
720  * @param sc our search context
721  * @param ub the ublock with the keyword search result
722  * @param size size of nb
723  */
724 static void
725 process_kblock (struct GNUNET_FS_SearchContext *sc, const struct UBlock *ub,
726                 size_t size)
727 {
728   size_t j;
729   char pt[size - sizeof (struct UBlock)];
730   const char *eos;
731   struct GNUNET_CONTAINER_MetaData *meta;
732   struct GNUNET_FS_Uri *uri;
733   char *emsg;
734   int i;
735
736   if (-1 == (i = decrypt_block_with_keyword (sc,
737                                              &ub->verification_key,
738                                              &ub[1],
739                                              size - sizeof (struct UBlock),
740                                              pt)))
741     return;
742   /* parse; pt[0] is just '\0', so we skip over that */
743   eos = memchr (&pt[1], '\0', sizeof (pt) - 1);
744   if (NULL == eos)
745   {
746     GNUNET_break_op (0);
747     return;
748   }
749   if (NULL == (uri = GNUNET_FS_uri_parse (&pt[1], &emsg)))
750   {
751     GNUNET_break_op (0);        /* ublock malformed */
752     GNUNET_free_non_null (emsg);   
753     return;
754   }
755   j = eos - pt + 1;
756   if (sizeof (pt) == j)
757     meta = GNUNET_CONTAINER_meta_data_create ();
758   else
759     meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j], sizeof (pt) - j);
760   if (NULL == meta)
761   {
762     GNUNET_break_op (0);        /* ublock malformed */
763     GNUNET_FS_uri_destroy (uri);
764     return;
765   }
766   process_ksk_result (sc, &sc->requests[i], uri, meta);
767
768   /* clean up */
769   GNUNET_CONTAINER_meta_data_destroy (meta);
770   GNUNET_FS_uri_destroy (uri);
771 }
772
773
774 /**
775  * Process a namespace-search result.  The actual type of block is
776  * a UBlock; we know it is a namespace search result because that's
777  * what we were searching for.
778  *
779  * @param sc our search context
780  * @param ub the ublock with a namespace result
781  * @param size size of sb
782  */
783 static void
784 process_sblock (struct GNUNET_FS_SearchContext *sc, 
785                 const struct UBlock *ub,
786                 size_t size)
787 {
788   size_t len = size - sizeof (struct UBlock);
789   char pt[len];
790   struct GNUNET_CRYPTO_AesSessionKey skey;
791   struct GNUNET_CRYPTO_AesInitializationVector iv;
792   struct GNUNET_FS_Uri *uri;
793   struct GNUNET_CONTAINER_MetaData *meta;
794   const char *id;
795   const char *uris;
796   size_t off;
797   char *emsg;
798   struct GNUNET_HashCode key;
799   struct GNUNET_HashCode id_hash;
800   struct GNUNET_HashCode ns_hash;
801   char *identifier;
802
803   /* decrypt */
804   identifier = sc->uri->data.sks.identifier;
805   GNUNET_CRYPTO_hash (identifier, strlen (identifier), &id_hash);
806   GNUNET_CRYPTO_hash (&sc->uri->data.sks.ns, 
807                       sizeof (sc->uri->data.sks.ns), &ns_hash);
808   GNUNET_CRYPTO_hash_xor (&id_hash, &ns_hash, &key);
809   GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
810   if (-1 == GNUNET_CRYPTO_aes_decrypt (&ub[1], len, &skey, &iv, pt))
811   {
812     GNUNET_break (0);
813     return;
814   }
815   /* parse */
816   if (0 == (off = GNUNET_STRINGS_buffer_tokenize (pt, len, 2, &id, &uris)))
817   {
818     GNUNET_break_op (0);        /* ublock malformed */
819     return;
820   }
821   if (NULL == (meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], len - off)))
822   {
823     GNUNET_break_op (0);        /* ublock malformed */
824     return;
825   }
826   if (NULL == (uri = GNUNET_FS_uri_parse (uris, &emsg)))
827   {
828     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
829                 _("Failed to parse URI `%s': %s\n"), 
830                 uris, emsg);
831     GNUNET_break_op (0);        /* ublock malformed */
832     GNUNET_free_non_null (emsg);
833     GNUNET_CONTAINER_meta_data_destroy (meta);
834     return;
835   }
836   /* process */
837   process_sks_result (sc, id, uri, meta);
838   /* clean up */
839   GNUNET_FS_uri_destroy (uri);
840   GNUNET_CONTAINER_meta_data_destroy (meta);
841 }
842
843
844 /**
845  * Process a search result.
846  *
847  * @param sc our search context
848  * @param type type of the result
849  * @param expiration when it will expire
850  * @param data the (encrypted) response
851  * @param size size of data
852  */
853 static void
854 process_result (struct GNUNET_FS_SearchContext *sc, enum GNUNET_BLOCK_Type type,
855                 struct GNUNET_TIME_Absolute expiration, 
856                 const void *data,
857                 size_t size)
858 {
859   if (GNUNET_TIME_absolute_get_duration (expiration).rel_value > 0)
860   {
861     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862                 "Result received has already expired.\n");
863     return;                     /* result expired */
864   }
865   switch (type)
866   {
867   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
868     if (GNUNET_FS_URI_SKS == sc->uri->type)
869       process_sblock (sc, data, size);
870     else
871       process_kblock (sc, data, size);
872     break;
873   case GNUNET_BLOCK_TYPE_ANY:
874     GNUNET_break (0);
875     break;
876   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
877     GNUNET_break (0);
878     break;
879   case GNUNET_BLOCK_TYPE_FS_ONDEMAND:
880     GNUNET_break (0);
881     break;
882   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
883     GNUNET_break (0);
884     break;
885   default:
886     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
887                 _("Got result with unknown block type `%d', ignoring"), type);
888     break;
889   }
890 }
891
892
893 /**
894  * Shutdown any existing connection to the FS
895  * service and try to establish a fresh one
896  * (and then re-transmit our search request).
897  *
898  * @param sc the search to reconnec
899  */
900 static void
901 try_reconnect (struct GNUNET_FS_SearchContext *sc);
902
903
904 /**
905  * Type of a function to call when we receive a message
906  * from the service.
907  *
908  * @param cls closure
909  * @param msg message received, NULL on timeout or fatal error
910  */
911 static void
912 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
913 {
914   struct GNUNET_FS_SearchContext *sc = cls;
915   const struct ClientPutMessage *cm;
916   uint16_t msize;
917
918   if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
919       (ntohs (msg->size) <= sizeof (struct ClientPutMessage)))
920   {
921     try_reconnect (sc);
922     return;
923   }
924   msize = ntohs (msg->size);
925   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926               "Receiving %u bytes of result from fs service\n", msize);
927   cm = (const struct ClientPutMessage *) msg;
928   process_result (sc, ntohl (cm->type),
929                   GNUNET_TIME_absolute_ntoh (cm->expiration), &cm[1],
930                   msize - sizeof (struct ClientPutMessage));
931   /* continue receiving */
932   GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
933                          GNUNET_TIME_UNIT_FOREVER_REL);
934 }
935
936
937 /**
938  * Schedule the transmission of the (next) search request
939  * to the service.
940  *
941  * @param sc context for the search
942  */
943 static void
944 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc);
945
946
947 /**
948  * Closure for 'build_result_set'.
949  */
950 struct MessageBuilderContext
951 {
952   /**
953    * How many entries can we store to xoff.
954    */
955   unsigned int put_cnt;
956
957   /**
958    * How many entries should we skip.
959    */
960   unsigned int skip_cnt;
961
962   /**
963    * Where to store the keys.
964    */
965   struct GNUNET_HashCode *xoff;
966
967   /**
968    * Search context we are iterating for.
969    */
970   struct GNUNET_FS_SearchContext *sc;
971
972   /**
973    * Keyword offset the search result must match (0 for SKS)
974    */
975   unsigned int keyword_offset;
976 };
977
978
979 /**
980  * Iterating over the known results, pick those matching the given
981  * result range and store their keys at 'xoff'.
982  *
983  * @param cls the 'struct MessageBuilderContext'
984  * @param key key for a result
985  * @param value the search result
986  * @return GNUNET_OK to continue iterating
987  */
988 static int
989 build_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
990 {
991   struct MessageBuilderContext *mbc = cls;
992   struct GNUNET_FS_SearchResult *sr = value;
993
994   if ( (NULL != sr->keyword_bitmap) &&
995        (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
996     return GNUNET_OK; /* have no match for this keyword yet */
997   if (mbc->skip_cnt > 0)
998   {
999     mbc->skip_cnt--;
1000     return GNUNET_OK;
1001   }
1002   if (0 == mbc->put_cnt)
1003     return GNUNET_SYSERR;
1004   mbc->sc->search_request_map_offset++;
1005   mbc->xoff[--mbc->put_cnt] = *key;
1006
1007   return GNUNET_OK;
1008 }
1009
1010
1011 /**
1012  * Iterating over the known results, count those
1013  * matching the given result range and increment
1014  * put count for each.
1015  *
1016  * @param cls the 'struct MessageBuilderContext'
1017  * @param key key for a result
1018  * @param value the search result
1019  * @return GNUNET_OK to continue iterating
1020  */
1021 static int
1022 find_result_set (void *cls, const struct GNUNET_HashCode * key, void *value)
1023 {
1024   struct MessageBuilderContext *mbc = cls;
1025   struct GNUNET_FS_SearchResult *sr = value;
1026
1027   if ( (NULL != sr->keyword_bitmap) &&
1028        (0 == (sr->keyword_bitmap[mbc->keyword_offset / 8] & (1 << (mbc->keyword_offset % 8)))) )
1029     return GNUNET_OK; /* have no match for this keyword yet */
1030   mbc->put_cnt++;
1031   return GNUNET_OK;
1032 }
1033
1034
1035 /**
1036  * We're ready to transmit the search request to the
1037  * file-sharing service.  Do it.
1038  *
1039  * @param cls closure
1040  * @param size number of bytes available in buf
1041  * @param buf where the callee should write the message
1042  * @return number of bytes written to buf
1043  */
1044 static size_t
1045 transmit_search_request (void *cls, size_t size, void *buf)
1046 {
1047   struct GNUNET_FS_SearchContext *sc = cls;
1048   struct MessageBuilderContext mbc;
1049   size_t msize;
1050   struct SearchMessage *sm;
1051   const char *identifier;
1052   struct GNUNET_HashCode key;
1053   struct GNUNET_HashCode signing_key;
1054   struct GNUNET_HashCode ns_hash;
1055   struct GNUNET_HashCode id_hash;
1056   struct GNUNET_FS_PseudonymIdentifier verification_key;
1057   unsigned int sqms;
1058   uint32_t options;
1059
1060   if (NULL == buf)
1061   {
1062     try_reconnect (sc);
1063     return 0;
1064   }
1065   mbc.sc = sc;
1066   mbc.skip_cnt = sc->search_request_map_offset;
1067   sm = buf;
1068   sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1069   mbc.xoff = (struct GNUNET_HashCode *) & sm[1];
1070   options = SEARCH_MESSAGE_OPTION_NONE;
1071   if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
1072     options |= SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY;
1073   if (GNUNET_FS_uri_test_ksk (sc->uri))
1074   {
1075     msize = sizeof (struct SearchMessage);
1076     GNUNET_assert (size >= msize);
1077     mbc.keyword_offset = sc->keyword_offset;
1078     /* calculate total number of known results (in put_cnt => sqms) */
1079     mbc.put_cnt = 0;
1080     GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1081                                            &find_result_set, &mbc);
1082     sqms = mbc.put_cnt;
1083     /* calculate how many results we can send in this message */
1084     mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1085     mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1086     if (sc->search_request_map_offset < sqms)
1087       GNUNET_assert (mbc.put_cnt > 0);
1088
1089     /* now build message */
1090     msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1091     sm->header.size = htons (msize);
1092     sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1093     sm->anonymity_level = htonl (sc->anonymity);
1094     memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
1095     sm->query = sc->requests[sc->keyword_offset].uquery;
1096     GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1097                                            &build_result_set, &mbc);
1098     GNUNET_assert (sqms >= sc->search_request_map_offset);
1099     if (sqms != sc->search_request_map_offset)
1100     {
1101       /* more requesting to be done... */
1102       sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1103       schedule_transmit_search_request (sc);
1104       return msize;
1105     }
1106     sm->options = htonl (options);
1107     sc->keyword_offset++;
1108     if (sc->uri->data.ksk.keywordCount != sc->keyword_offset)
1109     {
1110       /* more requesting to be done... */
1111       schedule_transmit_search_request (sc);
1112       return msize;
1113     }
1114   }
1115   else
1116   {
1117     GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1118     msize = sizeof (struct SearchMessage);
1119     GNUNET_assert (size >= msize);
1120     sm->type = htonl (GNUNET_BLOCK_TYPE_FS_UBLOCK);
1121     sm->anonymity_level = htonl (sc->anonymity);
1122     memset (&sm->target, 0, sizeof (struct GNUNET_HashCode));
1123
1124     identifier = sc->uri->data.sks.identifier;
1125     GNUNET_CRYPTO_hash (identifier, strlen (identifier), &id_hash);
1126     GNUNET_CRYPTO_hash (&sc->uri->data.sks.ns,
1127                         sizeof (sc->uri->data.sks.ns), &ns_hash);
1128     GNUNET_CRYPTO_hash_xor (&id_hash, &ns_hash, &key);
1129     GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &signing_key);
1130     GNUNET_FS_pseudonym_derive_verification_key (&sc->uri->data.sks.ns,
1131                                                  &signing_key,
1132                                                  &verification_key);
1133     GNUNET_CRYPTO_hash (&verification_key,
1134                         sizeof (verification_key),
1135                         &sm->query);
1136     mbc.put_cnt = (size - msize) / sizeof (struct GNUNET_HashCode);
1137     sqms = GNUNET_CONTAINER_multihashmap_size (sc->master_result_map);
1138     mbc.put_cnt = GNUNET_MIN (mbc.put_cnt, sqms - mbc.skip_cnt);
1139     mbc.keyword_offset = 0;
1140     if (sc->search_request_map_offset < sqms)
1141       GNUNET_assert (mbc.put_cnt > 0);
1142     msize += sizeof (struct GNUNET_HashCode) * mbc.put_cnt;
1143     GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1144                                            &build_result_set, &mbc);
1145     sm->header.size = htons (msize);
1146     GNUNET_assert (sqms >= sc->search_request_map_offset);
1147     if (sqms != sc->search_request_map_offset)
1148     {
1149       /* more requesting to be done... */
1150       sm->options = htonl (options | SEARCH_MESSAGE_OPTION_CONTINUED);
1151       schedule_transmit_search_request (sc);
1152       return msize;
1153     }
1154     sm->options = htonl (options);
1155   }
1156   GNUNET_CLIENT_receive (sc->client, &receive_results, sc,
1157                          GNUNET_TIME_UNIT_FOREVER_REL);
1158   return msize;
1159 }
1160
1161
1162 /**
1163  * Schedule the transmission of the (next) search request
1164  * to the service.
1165  *
1166  * @param sc context for the search
1167  */
1168 static void
1169 schedule_transmit_search_request (struct GNUNET_FS_SearchContext *sc)
1170 {
1171   size_t size;
1172   unsigned int sqms;
1173   unsigned int fit;
1174
1175   size = sizeof (struct SearchMessage);
1176   sqms =
1177       GNUNET_CONTAINER_multihashmap_size (sc->master_result_map) -
1178       sc->search_request_map_offset;
1179   fit = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - size) / sizeof (struct GNUNET_HashCode);
1180   fit = GNUNET_MIN (fit, sqms);
1181   size += sizeof (struct GNUNET_HashCode) * fit;
1182   GNUNET_CLIENT_notify_transmit_ready (sc->client, size,
1183                                        GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1184                                        GNUNET_NO, &transmit_search_request, sc);
1185
1186 }
1187
1188
1189 /**
1190  * Reconnect to the FS service and transmit
1191  * our queries NOW.
1192  *
1193  * @param cls our search context
1194  * @param tc unused
1195  */
1196 static void
1197 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1198 {
1199   struct GNUNET_FS_SearchContext *sc = cls;
1200   struct GNUNET_CLIENT_Connection *client;
1201
1202   sc->task = GNUNET_SCHEDULER_NO_TASK;
1203   client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1204   if (NULL == client)
1205   {
1206     try_reconnect (sc);
1207     return;
1208   }
1209   sc->client = client;
1210   sc->search_request_map_offset = 0;
1211   sc->keyword_offset = 0;
1212   schedule_transmit_search_request (sc);
1213 }
1214
1215
1216 /**
1217  * Shutdown any existing connection to the FS
1218  * service and try to establish a fresh one
1219  * (and then re-transmit our search request).
1220  *
1221  * @param sc the search to reconnec
1222  */
1223 static void
1224 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1225 {
1226   if (NULL != sc->client)
1227   {
1228     GNUNET_CLIENT_disconnect (sc->client);
1229     sc->client = NULL;
1230   }
1231   sc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (sc->reconnect_backoff);
1232   sc->task =
1233       GNUNET_SCHEDULER_add_delayed (sc->reconnect_backoff, 
1234                                     &do_reconnect,
1235                                     sc);
1236 }
1237
1238
1239 /**
1240  * Start search for content, internal API.
1241  *
1242  * @param h handle to the file sharing subsystem
1243  * @param uri specifies the search parameters; can be
1244  *        a KSK URI or an SKS URI.
1245  * @param anonymity desired level of anonymity
1246  * @param options options for the search
1247  * @param cctx initial value for the client context
1248  * @param psearch parent search result (for namespace update searches)
1249  * @return context that can be used to control the search
1250  */
1251 static struct GNUNET_FS_SearchContext *
1252 search_start (struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri,
1253               uint32_t anonymity, enum GNUNET_FS_SearchOptions options,
1254               void *cctx, struct GNUNET_FS_SearchResult *psearch)
1255 {
1256   struct GNUNET_FS_SearchContext *sc;
1257   struct GNUNET_FS_ProgressInfo pi;
1258
1259   sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1260   sc->h = h;
1261   sc->options = options;
1262   sc->uri = GNUNET_FS_uri_dup (uri);
1263   sc->anonymity = anonymity;
1264   sc->start_time = GNUNET_TIME_absolute_get ();
1265   if (NULL != psearch)
1266   {
1267     sc->psearch_result = psearch;
1268     psearch->update_search = sc;
1269   }
1270   sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1271   sc->client_info = cctx;
1272   if (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc))
1273   {
1274     GNUNET_FS_uri_destroy (sc->uri);
1275     GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1276     GNUNET_free (sc);
1277     return NULL;
1278   }
1279   GNUNET_FS_search_sync_ (sc);
1280   pi.status = GNUNET_FS_STATUS_SEARCH_START;
1281   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1282   return sc;
1283 }
1284
1285
1286 /**
1287  * Build the request and actually initiate the search using the
1288  * GNUnet FS service.
1289  *
1290  * @param sc search context
1291  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1292  */
1293 int
1294 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1295 {
1296   unsigned int i;
1297   const char *keyword;
1298   struct GNUNET_HashCode signing_key;
1299   struct GNUNET_FS_PseudonymHandle *ph;
1300   struct GNUNET_FS_PseudonymIdentifier anon;
1301   struct GNUNET_FS_PseudonymIdentifier verification_key;
1302
1303   GNUNET_assert (NULL == sc->client);
1304   if (GNUNET_FS_uri_test_ksk (sc->uri))
1305   {
1306     GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1307     ph = GNUNET_FS_pseudonym_get_anonymous_pseudonym_handle ();
1308     GNUNET_FS_pseudonym_get_identifier (ph, &anon);
1309     GNUNET_FS_pseudonym_destroy (ph);
1310     sc->requests =
1311         GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1312                        sc->uri->data.ksk.keywordCount);
1313     for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1314     {
1315       keyword = &sc->uri->data.ksk.keywords[i][1];
1316       GNUNET_CRYPTO_hash (keyword, strlen (keyword), &sc->requests[i].ukey);
1317       GNUNET_CRYPTO_hash (&sc->requests[i].ukey, sizeof (struct GNUNET_HashCode), &signing_key);
1318       GNUNET_FS_pseudonym_derive_verification_key (&anon, 
1319                                                    &signing_key,
1320                                                    &verification_key);
1321       GNUNET_CRYPTO_hash (&verification_key, sizeof (struct GNUNET_FS_PseudonymIdentifier),
1322                           &sc->requests[i].uquery);
1323       sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1324       if (sc->requests[i].mandatory)
1325         sc->mandatory_count++;
1326       sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
1327     }
1328   }
1329   sc->client = GNUNET_CLIENT_connect ("fs", sc->h->cfg);
1330   if (NULL == sc->client)
1331     return GNUNET_SYSERR;
1332   schedule_transmit_search_request (sc);
1333   return GNUNET_OK;
1334 }
1335
1336
1337 /**
1338  * Freeze probes for the given search result.
1339  *
1340  * @param cls the global FS handle
1341  * @param key the key for the search result (unused)
1342  * @param value the search result to free
1343  * @return GNUNET_OK
1344  */
1345 static int
1346 search_result_freeze_probes (void *cls, const struct GNUNET_HashCode * key,
1347                              void *value)
1348 {
1349   struct GNUNET_FS_SearchResult *sr = value;
1350
1351   if (NULL != sr->probe_ctx)
1352   {
1353     GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1354     sr->probe_ctx = NULL;
1355   }
1356   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
1357   {
1358     GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
1359     sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
1360   }
1361   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1362   {
1363     GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1364     sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1365   }
1366   if (NULL != sr->update_search)
1367     GNUNET_FS_search_pause (sr->update_search);
1368   return GNUNET_OK;
1369 }
1370
1371
1372 /**
1373  * Resume probes for the given search result.
1374  *
1375  * @param cls the global FS handle
1376  * @param key the key for the search result (unused)
1377  * @param value the search result to free
1378  * @return GNUNET_OK
1379  */
1380 static int
1381 search_result_resume_probes (void *cls, const struct GNUNET_HashCode * key,
1382                              void *value)
1383 {
1384   struct GNUNET_FS_SearchResult *sr = value;
1385
1386   GNUNET_FS_search_start_probe_ (sr);
1387   if (NULL != sr->update_search)
1388     GNUNET_FS_search_continue (sr->update_search);
1389   return GNUNET_OK;
1390 }
1391
1392
1393 /**
1394  * Signal suspend and free the given search result.
1395  *
1396  * @param cls the global FS handle
1397  * @param key the key for the search result (unused)
1398  * @param value the search result to free
1399  * @return GNUNET_OK
1400  */
1401 static int
1402 search_result_suspend (void *cls, const struct GNUNET_HashCode * key, void *value)
1403 {
1404   struct GNUNET_FS_SearchContext *sc = cls;
1405   struct GNUNET_FS_SearchResult *sr = value;
1406   struct GNUNET_FS_ProgressInfo pi;
1407
1408   if (NULL != sr->download)
1409   {
1410     GNUNET_FS_download_signal_suspend_ (sr->download);
1411     sr->download = NULL;
1412   }
1413   if (NULL != sr->probe_ctx)
1414   {
1415     GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1416     sr->probe_ctx = NULL;
1417   }
1418   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_ping_task)
1419   {
1420     GNUNET_SCHEDULER_cancel (sr->probe_ping_task);
1421     sr->probe_ping_task = GNUNET_SCHEDULER_NO_TASK;
1422   }
1423   if (NULL != sr->update_search)
1424   {
1425     GNUNET_FS_search_signal_suspend_ (sr->update_search);
1426     sr->update_search = NULL;
1427   }
1428   pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND;
1429   pi.value.search.specifics.result_suspend.cctx = sr->client_info;
1430   pi.value.search.specifics.result_suspend.meta = sr->meta;
1431   pi.value.search.specifics.result_suspend.uri = sr->uri;
1432   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1433   GNUNET_break (NULL == sr->client_info);
1434   GNUNET_free_non_null (sr->serialization);
1435   GNUNET_FS_uri_destroy (sr->uri);
1436   GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1437   if (GNUNET_SCHEDULER_NO_TASK != sr->probe_cancel_task)
1438   {
1439     GNUNET_SCHEDULER_cancel (sr->probe_cancel_task);
1440     sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
1441   }
1442   GNUNET_free_non_null (sr->keyword_bitmap);
1443   GNUNET_free (sr);
1444   return GNUNET_OK;
1445 }
1446
1447
1448 /**
1449  * Create SUSPEND event for the given search operation
1450  * and then clean up our state (without stop signal).
1451  *
1452  * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1453  */
1454 void
1455 GNUNET_FS_search_signal_suspend_ (void *cls)
1456 {
1457   struct GNUNET_FS_SearchContext *sc = cls;
1458   struct GNUNET_FS_ProgressInfo pi;
1459   unsigned int i;
1460
1461   GNUNET_FS_end_top (sc->h, sc->top);
1462   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1463                                          &search_result_suspend, sc);
1464   pi.status = GNUNET_FS_STATUS_SEARCH_SUSPEND;
1465   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1466   GNUNET_break (NULL == sc->client_info);
1467   if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1468     GNUNET_SCHEDULER_cancel (sc->task);
1469   if (NULL != sc->client)
1470     GNUNET_CLIENT_disconnect (sc->client);
1471   GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1472   if (NULL != sc->requests)
1473   {
1474     GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1475     for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1476       GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1477   }
1478   GNUNET_free_non_null (sc->requests);
1479   GNUNET_free_non_null (sc->emsg);
1480   GNUNET_FS_uri_destroy (sc->uri);
1481   GNUNET_free_non_null (sc->serialization);
1482   GNUNET_free (sc);
1483 }
1484
1485
1486 /**
1487  * Start search for content.
1488  *
1489  * @param h handle to the file sharing subsystem
1490  * @param uri specifies the search parameters; can be
1491  *        a KSK URI or an SKS URI.
1492  * @param anonymity desired level of anonymity
1493  * @param options options for the search
1494  * @param cctx initial value for the client context
1495  * @return context that can be used to control the search
1496  */
1497 struct GNUNET_FS_SearchContext *
1498 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1499                         const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
1500                         enum GNUNET_FS_SearchOptions options, void *cctx)
1501 {
1502   struct GNUNET_FS_SearchContext *ret;
1503
1504   ret = search_start (h, uri, anonymity, options, cctx, NULL);
1505   if (NULL == ret)
1506     return NULL;
1507   ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, ret);
1508   return ret;
1509 }
1510
1511
1512 /**
1513  * Pause search.
1514  *
1515  * @param sc context for the search that should be paused
1516  */
1517 void
1518 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1519 {
1520   struct GNUNET_FS_ProgressInfo pi;
1521
1522   if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1523     GNUNET_SCHEDULER_cancel (sc->task);
1524   sc->task = GNUNET_SCHEDULER_NO_TASK;
1525   if (NULL != sc->client)
1526     GNUNET_CLIENT_disconnect (sc->client);
1527   sc->client = NULL;
1528   GNUNET_FS_search_sync_ (sc);
1529   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1530                                          &search_result_freeze_probes, sc);
1531   pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1532   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1533 }
1534
1535
1536 /**
1537  * Continue paused search.
1538  *
1539  * @param sc context for the search that should be resumed
1540  */
1541 void
1542 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1543 {
1544   struct GNUNET_FS_ProgressInfo pi;
1545
1546   GNUNET_assert (NULL == sc->client);
1547   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sc->task);
1548   do_reconnect (sc, NULL);
1549   GNUNET_FS_search_sync_ (sc);
1550   pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1551   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1552   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1553                                          &search_result_resume_probes, sc);
1554 }
1555
1556
1557 /**
1558  * Signal stop for the given search result.
1559  *
1560  * @param cls the global FS handle
1561  * @param key the key for the search result (unused)
1562  * @param value the search result to free
1563  * @return GNUNET_OK
1564  */
1565 static int
1566 search_result_stop (void *cls, const struct GNUNET_HashCode * key, void *value)
1567 {
1568   struct GNUNET_FS_SearchContext *sc = cls;
1569   struct GNUNET_FS_SearchResult *sr = value;
1570   struct GNUNET_FS_ProgressInfo pi;
1571
1572   GNUNET_FS_search_stop_probe_ (sr);
1573
1574   if (NULL != sr->download)
1575   {
1576     sr->download->search = NULL;
1577     sr->download->top =
1578         GNUNET_FS_make_top (sr->download->h,
1579                             &GNUNET_FS_download_signal_suspend_, sr->download);
1580     if (NULL != sr->download->serialization)
1581     {
1582       GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1583                                    sr->download->serialization);
1584       GNUNET_free (sr->download->serialization);
1585       sr->download->serialization = NULL;
1586     }
1587     pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1588     GNUNET_FS_download_make_status_ (&pi, sr->download);
1589     GNUNET_FS_download_sync_ (sr->download);
1590     sr->download = NULL;
1591   }
1592   pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1593   pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1594   pi.value.search.specifics.result_stopped.meta = sr->meta;
1595   pi.value.search.specifics.result_stopped.uri = sr->uri;
1596   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sr->h, sc);
1597   return GNUNET_OK;
1598 }
1599
1600
1601 /**
1602  * Free the given search result.
1603  *
1604  * @param cls the global FS handle
1605  * @param key the key for the search result (unused)
1606  * @param value the search result to free
1607  * @return GNUNET_OK
1608  */
1609 static int
1610 search_result_free (void *cls, const struct GNUNET_HashCode * key, void *value)
1611 {
1612   struct GNUNET_FS_SearchResult *sr = value;
1613
1614   if (NULL != sr->update_search)
1615   {
1616     GNUNET_FS_search_stop (sr->update_search);
1617     GNUNET_assert (NULL == sr->update_search);
1618   }
1619   GNUNET_break (NULL == sr->probe_ctx);
1620   GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_cancel_task);
1621   GNUNET_break (GNUNET_SCHEDULER_NO_TASK == sr->probe_ping_task);
1622   GNUNET_break (NULL == sr->client_info);
1623   GNUNET_free_non_null (sr->serialization);
1624   GNUNET_FS_uri_destroy (sr->uri);
1625   GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1626   GNUNET_free_non_null (sr->keyword_bitmap);
1627   GNUNET_free (sr);
1628   return GNUNET_OK;
1629 }
1630
1631
1632 /**
1633  * Stop search for content.
1634  *
1635  * @param sc context for the search that should be stopped
1636  */
1637 void
1638 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1639 {
1640   struct GNUNET_FS_ProgressInfo pi;
1641   unsigned int i;
1642
1643   if (NULL != sc->top)
1644     GNUNET_FS_end_top (sc->h, sc->top);
1645   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1646                                          &search_result_stop, sc);
1647   if (NULL != sc->psearch_result)
1648     sc->psearch_result->update_search = NULL;
1649   if (NULL != sc->serialization)
1650   {
1651     GNUNET_FS_remove_sync_file_ (sc->h,
1652                                  (sc->psearch_result !=
1653                                   NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1654                                  GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1655                                  sc->serialization);
1656     GNUNET_FS_remove_sync_dir_ (sc->h,
1657                                 (sc->psearch_result !=
1658                                  NULL) ? GNUNET_FS_SYNC_PATH_CHILD_SEARCH :
1659                                 GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
1660                                 sc->serialization);
1661     GNUNET_free (sc->serialization);
1662   }
1663   pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1664   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
1665   GNUNET_break (NULL == sc->client_info);
1666   if (GNUNET_SCHEDULER_NO_TASK != sc->task)
1667     GNUNET_SCHEDULER_cancel (sc->task);
1668   if (NULL != sc->client)
1669     GNUNET_CLIENT_disconnect (sc->client);
1670   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1671                                          &search_result_free, sc);
1672   GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1673   if (NULL != sc->requests)
1674   {
1675     GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1676     for (i = 0; i < sc->uri->data.ksk.keywordCount; i++)
1677       GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1678   }
1679   GNUNET_free_non_null (sc->requests);
1680   GNUNET_free_non_null (sc->emsg);
1681   GNUNET_FS_uri_destroy (sc->uri);
1682   GNUNET_free (sc);
1683 }
1684
1685 /* end of fs_search.c */