deserialize search link to download
[oweals/gnunet.git] / src / fs / fs_search.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 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 2, 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 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file fs/fs_search.c
23  * @brief Helper functions for searching.
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - insert code for serialization where needed
28  * - remove *directory* with search results upon completion
29  * - centralize code that sprintf's the 'pbuf[32]' strings
30  * - add support for pushing "already seen" information
31  *   to FS service for bloomfilter (can wait)
32  */
33
34 #include "platform.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_fs_service.h"
37 #include "gnunet_protocols.h"
38 #include "fs.h"
39
40 #define DEBUG_SEARCH GNUNET_NO
41
42
43
44 /**
45  * Fill in all of the generic fields for a search event and
46  * call the callback.
47  *
48  * @param pi structure to fill in
49  * @param sc overall search context
50  * @return value returned by the callback
51  */
52 void *
53 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
54                                struct GNUNET_FS_SearchContext *sc)
55 {
56   pi->value.search.sc = sc;
57   pi->value.search.cctx
58     = sc->client_info;
59   pi->value.search.pctx
60     = (sc->parent == NULL) ? NULL : sc->parent->client_info;
61   pi->value.search.query 
62     = sc->uri;
63   pi->value.search.duration = GNUNET_TIME_absolute_get_duration (sc->start_time);
64   pi->value.search.anonymity = sc->anonymity;
65   return sc->h->upcb (sc->h->upcb_cls,
66                       pi);
67 }
68
69
70 /**
71  * Check if the given result is identical
72  * to the given URI.
73  * 
74  * @param cls points to the URI we check against
75  * @param key not used
76  * @param value a "struct GNUNET_FS_SearchResult" who's URI we
77  *        should compare with
78  * @return GNUNET_SYSERR if the result is present,
79  *         GNUNET_OK otherwise
80  */
81 static int
82 test_result_present (void *cls,
83                      const GNUNET_HashCode * key,
84                      void *value)
85 {
86   const struct GNUNET_FS_Uri *uri = cls;
87   struct GNUNET_FS_SearchResult *sr = value;
88
89   if (GNUNET_FS_uri_test_equal (uri,
90                                 sr->uri))
91     return GNUNET_SYSERR;
92   return GNUNET_OK;
93 }
94
95
96 /**
97  * We've found a new CHK result.  Let the client
98  * know about it.
99  * 
100  * @param sc the search context
101  * @param sr the specific result
102  */
103 static void
104 notify_client_chk_result (struct GNUNET_FS_SearchContext *sc, 
105                           struct GNUNET_FS_SearchResult *sr)
106 {                         
107   struct GNUNET_FS_ProgressInfo pi;
108
109   pi.status = GNUNET_FS_STATUS_SEARCH_RESULT;
110   pi.value.search.specifics.result.meta = sr->meta;
111   pi.value.search.specifics.result.uri = sr->uri;
112   pi.value.search.specifics.result.result = sr;
113   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
114 }
115
116
117 /**
118  * We've found new information about an existing CHK result.  Let the
119  * client know about it.
120  * 
121  * @param sc the search context
122  * @param sr the specific result
123  */
124 static void
125 notify_client_chk_update (struct GNUNET_FS_SearchContext *sc, 
126                           struct GNUNET_FS_SearchResult *sr)
127 {                         
128   struct GNUNET_FS_ProgressInfo pi;
129
130   pi.status = GNUNET_FS_STATUS_SEARCH_UPDATE;
131   pi.value.search.specifics.update.cctx = sr->client_info;
132   pi.value.search.specifics.update.meta = sr->meta;
133   pi.value.search.specifics.update.uri = sr->uri;
134   pi.value.search.specifics.update.availability_rank
135     = 2*sr->availability_success - sr->availability_trials;
136   pi.value.search.specifics.update.availability_certainty 
137     = sr->availability_trials;
138   pi.value.search.specifics.update.applicability_rank 
139     = sr->optional_support;
140   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
141 }
142
143
144 /**
145  * Context for "get_result_present".
146  */
147 struct GetResultContext 
148 {
149   /**
150    * The URI we're looking for.
151    */
152   const struct GNUNET_FS_Uri *uri;
153
154   /**
155    * Where to store a pointer to the search
156    * result struct if we found a match.
157    */
158   struct GNUNET_FS_SearchResult *sr;
159 };
160
161
162 /**
163  * Check if the given result is identical to the given URI and if so
164  * return it.
165  * 
166  * @param cls a "struct GetResultContext"
167  * @param key not used
168  * @param value a "struct GNUNET_FS_SearchResult" who's URI we
169  *        should compare with
170  * @return GNUNET_OK
171  */
172 static int
173 get_result_present (void *cls,
174                      const GNUNET_HashCode * key,
175                      void *value)
176 {
177   struct GetResultContext *grc = cls;
178   struct GNUNET_FS_SearchResult *sr = value;
179
180   if (GNUNET_FS_uri_test_equal (grc->uri,
181                                 sr->uri))
182     grc->sr = sr;
183   return GNUNET_OK;
184 }
185
186
187 /**
188  * Signal result of last probe to client and then schedule next
189  * probe.
190  */
191 static void
192 signal_probe_result (struct GNUNET_FS_SearchResult *sr)
193 {
194   struct GNUNET_FS_ProgressInfo pi;
195
196   pi.status = GNUNET_FS_STATUS_SEARCH_START;
197   pi.value.search.specifics.update.cctx = sr->client_info;
198   pi.value.search.specifics.update.meta = sr->meta;
199   pi.value.search.specifics.update.uri = sr->uri;
200   pi.value.search.specifics.update.availability_rank = sr->availability_success;
201   pi.value.search.specifics.update.availability_certainty = sr->availability_trials;
202   pi.value.search.specifics.update.applicability_rank = sr->optional_support;
203   sr->sc->client_info = GNUNET_FS_search_make_status_ (&pi, sr->sc);
204   GNUNET_FS_search_start_probe_ (sr);
205 }
206
207
208 /**
209  * Handle the case where we have failed to receive a response for our probe.
210  *
211  * @param cls our 'struct GNUNET_FS_SearchResult*'
212  * @param tc scheduler context
213  */
214 static void
215 probe_failure_handler (void *cls,
216                        const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   struct GNUNET_FS_SearchResult *sr = cls;
219   sr->availability_trials++;
220   signal_probe_result (sr);
221 }
222
223
224 /**
225  * Handle the case where we have gotten a response for our probe.
226  *
227  * @param cls our 'struct GNUNET_FS_SearchResult*'
228  * @param tc scheduler context
229  */
230 static void
231 probe_success_handler (void *cls,
232                        const struct GNUNET_SCHEDULER_TaskContext *tc)
233 {
234   struct GNUNET_FS_SearchResult *sr = cls;
235   sr->availability_trials++;
236   sr->availability_success++;
237   signal_probe_result (sr);
238 }
239
240
241 /**
242  * Notification of FS that a search probe has made progress.
243  * This function is used INSTEAD of the client's event handler
244  * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
245  *
246  * @param cls closure, always NULL (!), actual closure
247  *        is in the client-context of the info struct
248  * @param info details about the event, specifying the event type
249  *        and various bits about the event
250  * @return client-context (for the next progress call
251  *         for this operation; should be set to NULL for
252  *         SUSPEND and STOPPED events).  The value returned
253  *         will be passed to future callbacks in the respective
254  *         field in the GNUNET_FS_ProgressInfo struct.
255  */
256 void*
257 GNUNET_FS_search_probe_progress_ (void *cls,
258                                   const struct GNUNET_FS_ProgressInfo *info)
259 {
260   struct GNUNET_FS_SearchResult *sr = info->value.download.cctx;
261   struct GNUNET_TIME_Relative dur;
262
263   switch (info->status)
264     {
265     case GNUNET_FS_STATUS_DOWNLOAD_START:
266       /* ignore */
267       break;
268     case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
269       /* probes should never be resumed */
270       GNUNET_assert (0);
271       break;
272     case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
273       /* probes should never be suspended */
274       GNUNET_break (0);
275       break;
276     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
277       /* ignore */
278       break;
279     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
280       if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
281         {
282           GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
283                                    sr->probe_cancel_task);
284           sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
285         }     
286       sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
287                                                             sr->remaining_probe_time,
288                                                             &probe_failure_handler,
289                                                             sr);
290       break;
291     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
292       if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
293         {
294           GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
295                                    sr->probe_cancel_task);
296           sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
297         }     
298       sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
299                                                             sr->remaining_probe_time,
300                                                             &probe_success_handler,
301                                                             sr);
302       break;
303     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
304       if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
305         {
306           GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
307                                    sr->probe_cancel_task);
308           sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
309         }     
310       sr = NULL;
311       break;
312     case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
313       GNUNET_assert (sr->probe_cancel_task == GNUNET_SCHEDULER_NO_TASK);
314       sr->probe_active_time = GNUNET_TIME_absolute_get ();
315       sr->probe_cancel_task = GNUNET_SCHEDULER_add_delayed (sr->sc->h->sched,
316                                                             sr->remaining_probe_time,
317                                                             &probe_failure_handler,
318                                                             sr);
319       break;
320     case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
321       if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
322         {
323           GNUNET_SCHEDULER_cancel (sr->sc->h->sched,
324                                    sr->probe_cancel_task);
325           sr->probe_cancel_task = GNUNET_SCHEDULER_NO_TASK;
326         }
327       dur = GNUNET_TIME_absolute_get_duration (sr->probe_active_time);
328       sr->remaining_probe_time = GNUNET_TIME_relative_subtract (sr->remaining_probe_time,
329                                                                 dur);
330       break;
331     default:
332       GNUNET_break (0);
333       return NULL;
334     }
335   return sr;
336 }
337
338
339 /**
340  * Start download probes for the given search result.
341  *
342  * @param sr the search result
343  */
344 void
345 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr)
346 {
347   uint64_t off;
348   uint64_t len;
349   
350   if (sr->probe_ctx != NULL)
351     return;
352   if (sr->download != NULL)
353     return;
354   if (0 == (sr->sc->h->flags & GNUNET_FS_FLAGS_DO_PROBES))
355     return;
356   if (sr->availability_trials > AVAILABILITY_TRIALS_MAX)
357     return;
358   len = GNUNET_FS_uri_chk_get_file_size (sr->uri);
359   if (len == 0)
360     return;
361   if ( (len <= DBLOCK_SIZE) && (sr->availability_success > 0))
362     return;
363   off = len / DBLOCK_SIZE;
364   if (off > 0)
365     off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, off);
366   off *= DBLOCK_SIZE;
367   if (len - off < DBLOCK_SIZE)
368     len = len - off;
369   else
370     len = DBLOCK_SIZE;
371   sr->remaining_probe_time = GNUNET_TIME_relative_multiply (sr->sc->h->avg_block_latency,
372                                                             2 * (1 + sr->availability_trials));
373   sr->probe_ctx = GNUNET_FS_download_start (sr->sc->h,
374                                             sr->uri,
375                                             sr->meta,
376                                             NULL, NULL,
377                                             off, len, 
378                                             sr->sc->anonymity,
379                                             GNUNET_FS_DOWNLOAD_NO_TEMPORARIES |
380                                             GNUNET_FS_DOWNLOAD_IS_PROBE,
381                                             sr, NULL);    
382 }
383
384
385 /**
386  * We have received a KSK result.  Check how it fits in with the
387  * overall query and notify the client accordingly.
388  *
389  * @param sc context for the overall query
390  * @param ent entry for the specific keyword
391  * @param uri the URI that was found
392  * @param meta metadata associated with the URI
393  *        under the "ent" keyword
394  */
395 static void
396 process_ksk_result (struct GNUNET_FS_SearchContext *sc, 
397                     struct SearchRequestEntry *ent,
398                     const struct GNUNET_FS_Uri *uri,
399                     const struct GNUNET_CONTAINER_MetaData *meta)
400 {
401   GNUNET_HashCode key;
402   struct GNUNET_FS_SearchResult *sr;
403   struct GetResultContext grc;
404   int is_new;
405
406   /* check if new */
407   GNUNET_FS_uri_to_key (uri, &key);
408   if (GNUNET_SYSERR ==
409       GNUNET_CONTAINER_multihashmap_get_multiple (ent->results,
410                                                   &key,
411                                                   &test_result_present,
412                                                   (void*) uri))
413     return; /* duplicate result */
414   /* try to find search result in master map */
415   grc.sr = NULL;
416   grc.uri = uri;
417   GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
418                                               &key,
419                                               &get_result_present,
420                                               &grc);
421   sr = grc.sr;
422   is_new = (NULL == sr) || (sr->mandatory_missing > 0);
423   if (NULL == sr)
424     {
425       sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
426       sr->sc = sc;
427       sr->uri = GNUNET_FS_uri_dup (uri);
428       sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
429       sr->mandatory_missing = sc->mandatory_count;
430       GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
431                                          &key,
432                                          sr,
433                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
434     }
435   else
436     {
437       GNUNET_CONTAINER_meta_data_merge (sr->meta, meta);
438     }
439   /* check if mandatory satisfied */
440   if (ent->mandatory)
441     sr->mandatory_missing--;
442   else
443     sr->optional_support++;
444   if (0 != sr->mandatory_missing)
445     return;
446   if (is_new)
447     notify_client_chk_result (sc, sr);
448   else
449     notify_client_chk_update (sc, sr);
450   GNUNET_FS_search_start_probe_ (sr);
451 }
452
453
454 /**
455  * Start search for content, internal API.
456  *
457  * @param h handle to the file sharing subsystem
458  * @param uri specifies the search parameters; can be
459  *        a KSK URI or an SKS URI.
460  * @param anonymity desired level of anonymity
461  * @param options options for the search
462  * @param cctx client context
463  * @param parent parent search (for namespace update searches)
464  * @return context that can be used to control the search
465  */
466 static struct GNUNET_FS_SearchContext *
467 search_start (struct GNUNET_FS_Handle *h,
468               const struct GNUNET_FS_Uri *uri,
469               uint32_t anonymity,
470               enum GNUNET_FS_SearchOptions options,
471               void *cctx,
472               struct GNUNET_FS_SearchContext *parent);
473
474
475 /**
476  * We have received an SKS result.  Start searching for updates and
477  * notify the client if it is a new result.
478  *
479  * @param sc context for the overall query
480  * @param id_update identifier for updates, NULL for none
481  * @param uri the URI that was found
482  * @param meta metadata associated with the URI
483   */
484 static void
485 process_sks_result (struct GNUNET_FS_SearchContext *sc, 
486                     const char *id_update,
487                     const struct GNUNET_FS_Uri *uri,
488                     const struct GNUNET_CONTAINER_MetaData *meta)
489 {
490   struct GNUNET_FS_Uri uu;
491   GNUNET_HashCode key;
492   struct GNUNET_FS_SearchResult *sr;
493
494   /* check if new */
495   GNUNET_FS_uri_to_key (uri, &key);
496   GNUNET_CRYPTO_hash_xor (&uri->data.chk.chk.key,
497                           &uri->data.chk.chk.query,
498                           &key);
499   if (GNUNET_SYSERR ==
500       GNUNET_CONTAINER_multihashmap_get_multiple (sc->master_result_map,
501                                                   &key,
502                                                   &test_result_present,
503                                                   (void*) uri))
504     return; /* duplicate result */
505   sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
506   sr->sc = sc;
507   sr->uri = GNUNET_FS_uri_dup (uri);
508   sr->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
509   GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
510                                      &key,
511                                      sr,
512                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
513   GNUNET_FS_search_start_probe_ (sr);
514   /* notify client */
515   notify_client_chk_result (sc, sr);
516   /* search for updates */
517   if (strlen (id_update) == 0)
518     return; /* no updates */
519   uu.type = sks;
520   uu.data.sks.namespace = sc->uri->data.sks.namespace;
521   uu.data.sks.identifier = GNUNET_strdup (id_update);
522   /* FIXME: should attach update search
523      to the individual result, not
524      the entire SKS search! */
525   search_start (sc->h,
526                 &uu,
527                 sc->anonymity,
528                 sc->options,
529                 NULL,
530                 sc);
531 }
532
533
534 /**
535  * Process a keyword-search result.
536  *
537  * @param sc our search context
538  * @param kb the kblock
539  * @param size size of kb
540  */
541 static void
542 process_kblock (struct GNUNET_FS_SearchContext *sc,
543                 const struct KBlock *kb,
544                 size_t size)
545 {
546   unsigned int i;
547   size_t j;
548   GNUNET_HashCode q;
549   char pt[size - sizeof (struct KBlock)];
550   struct GNUNET_CRYPTO_AesSessionKey skey;
551   struct GNUNET_CRYPTO_AesInitializationVector iv;
552   const char *eos;
553   struct GNUNET_CONTAINER_MetaData *meta;
554   struct GNUNET_FS_Uri *uri;
555   char *emsg;
556   
557   GNUNET_CRYPTO_hash (&kb->keyspace,
558                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
559                       &q);
560   /* find key */
561   for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
562     if (0 == memcmp (&q,
563                      &sc->requests[i].query,
564                      sizeof (GNUNET_HashCode)))
565       break;
566   if (i == sc->uri->data.ksk.keywordCount)
567     {
568       /* oops, does not match any of our keywords!? */
569       GNUNET_break (0);
570       return;
571     }
572   /* decrypt */
573   GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
574   GNUNET_CRYPTO_aes_decrypt (&kb[1],
575                              size - sizeof (struct KBlock),
576                              &skey,
577                              &iv,
578                              pt);
579   /* parse */
580   eos = memchr (pt, 0, sizeof (pt));
581   if (NULL == eos)
582     {
583       GNUNET_break_op (0);
584       return;
585     }
586   j = eos - pt + 1;
587   if (sizeof (pt) == j)
588     meta = GNUNET_CONTAINER_meta_data_create ();
589   else
590     meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
591                                                    sizeof (pt) - j);
592   if (meta == NULL)
593     {
594       GNUNET_break_op (0);       /* kblock malformed */
595       return;
596     }
597   uri = GNUNET_FS_uri_parse (pt, &emsg);
598   if (uri == NULL)
599     {
600       GNUNET_break_op (0);       /* kblock malformed */
601       GNUNET_free_non_null (emsg);
602       GNUNET_CONTAINER_meta_data_destroy (meta);
603       return;
604     }
605   /* process */
606   process_ksk_result (sc, &sc->requests[i], uri, meta);
607
608   /* clean up */
609   GNUNET_CONTAINER_meta_data_destroy (meta);
610   GNUNET_FS_uri_destroy (uri);
611 }
612
613
614 /**
615  * Process a keyword-search result with a namespace advertisment.
616  *
617  * @param sc our search context
618  * @param nb the nblock
619  * @param size size of nb
620  */
621 static void
622 process_nblock (struct GNUNET_FS_SearchContext *sc,
623                 const struct NBlock *nb,
624                 size_t size)
625 {
626   unsigned int i;
627   size_t j;
628   GNUNET_HashCode q;
629   char pt[size - sizeof (struct NBlock)];
630   struct GNUNET_CRYPTO_AesSessionKey skey;
631   struct GNUNET_CRYPTO_AesInitializationVector iv;
632   const char *eos;
633   struct GNUNET_CONTAINER_MetaData *meta;
634   struct GNUNET_FS_Uri *uri;
635   char *uris;
636   
637   GNUNET_CRYPTO_hash (&nb->keyspace,
638                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
639                       &q);
640   /* find key */
641   for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
642     if (0 == memcmp (&q,
643                      &sc->requests[i].query,
644                      sizeof (GNUNET_HashCode)))
645       break;
646   if (i == sc->uri->data.ksk.keywordCount)
647     {
648       /* oops, does not match any of our keywords!? */
649       GNUNET_break (0);
650       return;
651     }
652   /* decrypt */
653   GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv);
654   GNUNET_CRYPTO_aes_decrypt (&nb[1],
655                              size - sizeof (struct NBlock),
656                              &skey,
657                              &iv,
658                              pt);
659   /* parse */
660   eos = memchr (pt, 0, sizeof (pt));
661   if (NULL == eos)
662     {
663       GNUNET_break_op (0);
664       return;
665     }
666   j = eos - pt + 1;
667   if (sizeof (pt) == j)
668     meta = GNUNET_CONTAINER_meta_data_create ();
669   else
670     meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[j],
671                                                    sizeof (pt) - j);
672   if (meta == NULL)
673     {
674       GNUNET_break_op (0);       /* nblock malformed */
675       return;
676     }
677
678   uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
679   uri->type = sks;
680   uri->data.sks.identifier = GNUNET_strdup (pt);
681   GNUNET_CRYPTO_hash (&nb->subspace,
682                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
683                       &uri->data.sks.namespace);
684   uris = GNUNET_FS_uri_to_string (uri);
685   GNUNET_CONTAINER_meta_data_insert (meta,
686                                      "<gnunet>",
687                                      EXTRACTOR_METATYPE_URI,
688                                      EXTRACTOR_METAFORMAT_UTF8,
689                                      "text/plain",
690                                      uris,
691                                      strlen (uris)+1);
692   GNUNET_free (uris);
693   GNUNET_PSEUDONYM_add (sc->h->cfg,
694                         &uri->data.sks.namespace,
695                         meta);
696   /* process */
697   process_ksk_result (sc, &sc->requests[i], uri, meta);
698
699   /* clean up */
700   GNUNET_CONTAINER_meta_data_destroy (meta);
701   GNUNET_FS_uri_destroy (uri);
702 }
703
704
705 /**
706  * Process a namespace-search result.
707  *
708  * @param sc our search context
709  * @param sb the sblock
710  * @param size size of sb
711  */
712 static void
713 process_sblock (struct GNUNET_FS_SearchContext *sc,
714                 const struct SBlock *sb,
715                 size_t size)
716 {
717   size_t len = size - sizeof (struct SBlock);
718   char pt[len];
719   struct GNUNET_CRYPTO_AesSessionKey skey;
720   struct GNUNET_CRYPTO_AesInitializationVector iv;
721   struct GNUNET_FS_Uri *uri;
722   struct GNUNET_CONTAINER_MetaData *meta;
723   const char *id;
724   const char *uris;
725   size_t off;
726   char *emsg;
727   GNUNET_HashCode key;
728   char *identifier;
729
730   /* decrypt */
731   identifier = sc->uri->data.sks.identifier;
732   GNUNET_CRYPTO_hash (identifier, 
733                       strlen (identifier), 
734                       &key);
735   GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
736   GNUNET_CRYPTO_aes_decrypt (&sb[1],
737                              len,
738                              &skey,
739                              &iv,
740                              pt);
741   /* parse */
742   off = GNUNET_STRINGS_buffer_tokenize (pt,
743                                         len, 
744                                         2, 
745                                         &id, 
746                                         &uris);
747   if (off == 0)
748     {
749       GNUNET_break_op (0);     /* sblock malformed */
750       return;
751     }
752   meta = GNUNET_CONTAINER_meta_data_deserialize (&pt[off], 
753                                                  len - off);
754   if (meta == NULL)
755     {
756       GNUNET_break_op (0);     /* sblock malformed */
757       return;
758     }
759   uri = GNUNET_FS_uri_parse (uris, &emsg);
760   if (uri == NULL)
761     {
762       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
763                   "Failed to parse URI `%s': %s\n",
764                   uris, emsg);
765       GNUNET_break_op (0);     /* sblock malformed */
766       GNUNET_free_non_null (emsg);
767       GNUNET_CONTAINER_meta_data_destroy (meta);
768       return;
769     }
770   /* process */
771   process_sks_result (sc, id, uri, meta);
772   /* clean up */
773   GNUNET_FS_uri_destroy (uri);
774   GNUNET_CONTAINER_meta_data_destroy (meta);
775 }
776
777
778 /**
779  * Process a search result.
780  *
781  * @param sc our search context
782  * @param type type of the result
783  * @param expiration when it will expire
784  * @param data the (encrypted) response
785  * @param size size of data
786  */
787 static void
788 process_result (struct GNUNET_FS_SearchContext *sc,
789                 enum GNUNET_BLOCK_Type type,
790                 struct GNUNET_TIME_Absolute expiration,
791                 const void *data,
792                 size_t size)
793 {
794   if (GNUNET_TIME_absolute_get_duration (expiration).value > 0)
795     {
796       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797                   "Result received has already expired.\n");
798       return; /* result expired */
799     }
800   switch (type)
801     {
802     case GNUNET_BLOCK_TYPE_KBLOCK:
803       if (! GNUNET_FS_uri_test_ksk (sc->uri))
804         {
805           GNUNET_break (0);
806           return;
807         }
808       if (sizeof (struct KBlock) > size)
809         {
810           GNUNET_break_op (0);
811           return;
812         }
813       process_kblock (sc, data, size);
814       break;
815     case GNUNET_BLOCK_TYPE_SBLOCK:
816       if (! GNUNET_FS_uri_test_sks (sc->uri))
817         {
818           GNUNET_break (0);
819           return;
820         }
821       if (sizeof (struct SBlock) > size)
822         {
823           GNUNET_break_op (0);
824           return;
825         }
826       process_sblock (sc, data, size);
827       break;
828     case GNUNET_BLOCK_TYPE_NBLOCK:
829       if (! GNUNET_FS_uri_test_ksk (sc->uri))
830         {
831           GNUNET_break (0);
832           return;
833         }
834       if (sizeof (struct NBlock) > size)
835         {
836           GNUNET_break_op (0);
837           return;
838         }
839       process_nblock (sc, data, size);
840       break;
841     case GNUNET_BLOCK_TYPE_ANY:
842     case GNUNET_BLOCK_TYPE_DBLOCK:
843     case GNUNET_BLOCK_TYPE_ONDEMAND:
844     case GNUNET_BLOCK_TYPE_IBLOCK:
845       GNUNET_break (0);
846       break;
847     default:
848       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
849                   _("Got result with unknown block type `%d', ignoring"),
850                   type);
851       break;
852     }
853 }
854
855
856 /**
857  * Shutdown any existing connection to the FS
858  * service and try to establish a fresh one
859  * (and then re-transmit our search request).
860  *
861  * @param sc the search to reconnec
862  */
863 static void 
864 try_reconnect (struct GNUNET_FS_SearchContext *sc);
865
866
867 /**
868  * Type of a function to call when we receive a message
869  * from the service.
870  *
871  * @param cls closure
872  * @param msg message received, NULL on timeout or fatal error
873  */
874 static void 
875 receive_results (void *cls,
876                  const struct GNUNET_MessageHeader * msg)
877 {
878   struct GNUNET_FS_SearchContext *sc = cls;
879   const struct PutMessage *cm;
880   uint16_t msize;
881
882   if ( (NULL == msg) ||
883        (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
884        (ntohs (msg->size) <= sizeof (struct PutMessage)) )
885     {
886       try_reconnect (sc);
887       return;
888     }
889   msize = ntohs (msg->size);
890   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891               "Receiving %u bytes of result from fs service\n",
892               msize);
893   cm = (const struct PutMessage*) msg;
894   process_result (sc, 
895                   ntohl (cm->type),
896                   GNUNET_TIME_absolute_ntoh (cm->expiration),
897                   &cm[1],
898                   msize - sizeof (struct PutMessage));
899   /* continue receiving */
900   GNUNET_CLIENT_receive (sc->client,
901                          &receive_results,
902                          sc,
903                          GNUNET_TIME_UNIT_FOREVER_REL);
904 }
905
906
907 /**
908  * We're ready to transmit the search request to the
909  * file-sharing service.  Do it.
910  *
911  * @param cls closure
912  * @param size number of bytes available in buf
913  * @param buf where the callee should write the message
914  * @return number of bytes written to buf
915  */
916 static size_t
917 transmit_search_request (void *cls,
918                          size_t size, 
919                          void *buf)
920 {
921   struct GNUNET_FS_SearchContext *sc = cls;
922   size_t msize;
923   struct SearchMessage *sm;
924   unsigned int i;
925   const char *identifier;
926   GNUNET_HashCode key;
927   GNUNET_HashCode idh;
928
929   if (NULL == buf)
930     {
931       try_reconnect (sc);
932       return 0;
933     }
934   if (GNUNET_FS_uri_test_ksk (sc->uri))
935     {
936       msize = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
937       GNUNET_assert (size >= msize);
938       sm = buf;
939       memset (sm, 0, msize);
940       for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
941         {
942           sm[i].header.size = htons (sizeof (struct SearchMessage));
943           sm[i].header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
944           if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
945             sm[i].options = htonl (1);
946           else
947             sm[i].options = htonl (0);            
948           sm[i].type = htonl (GNUNET_BLOCK_TYPE_ANY);
949           sm[i].anonymity_level = htonl (sc->anonymity);
950           sm[i].query = sc->requests[i].query;
951         }
952     }
953   else
954     {
955       GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
956       msize = sizeof (struct SearchMessage);
957       GNUNET_assert (size >= msize);
958       sm = buf;
959       memset (sm, 0, msize);
960       sm->header.size = htons (sizeof (struct SearchMessage));
961       sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
962       if (0 != (sc->options & GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY))
963         sm->options = htonl (1);
964       else
965         sm->options = htonl (0);      
966       sm->type = htonl (GNUNET_BLOCK_TYPE_SBLOCK);
967       sm->anonymity_level = htonl (sc->anonymity);
968       sm->target = sc->uri->data.sks.namespace;
969       identifier = sc->uri->data.sks.identifier;
970       GNUNET_CRYPTO_hash (identifier,
971                           strlen (identifier),
972                           &key);
973       GNUNET_CRYPTO_hash (&key,
974                           sizeof (GNUNET_HashCode),
975                           &idh);
976       GNUNET_CRYPTO_hash_xor (&idh,
977                               &sm->target,
978                               &sm->query);
979    }
980   GNUNET_CLIENT_receive (sc->client,
981                          &receive_results,
982                          sc,
983                          GNUNET_TIME_UNIT_FOREVER_REL);
984   return msize;
985 }
986
987
988 /**
989  * Reconnect to the FS service and transmit
990  * our queries NOW.
991  *
992  * @param cls our search context
993  * @param tc unused
994  */
995 static void
996 do_reconnect (void *cls,
997               const struct GNUNET_SCHEDULER_TaskContext *tc)
998 {
999   struct GNUNET_FS_SearchContext *sc = cls;
1000   struct GNUNET_CLIENT_Connection *client;
1001   size_t size;
1002   
1003   sc->task = GNUNET_SCHEDULER_NO_TASK;
1004   client = GNUNET_CLIENT_connect (sc->h->sched,
1005                                   "fs",
1006                                   sc->h->cfg);
1007   if (NULL == client)
1008     {
1009       try_reconnect (sc);
1010       return;
1011     }
1012   sc->client = client;
1013   if (GNUNET_FS_uri_test_ksk (sc->uri))
1014     size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1015   else
1016     size = sizeof (struct SearchMessage);
1017   GNUNET_CLIENT_notify_transmit_ready (client,
1018                                        size,
1019                                        GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1020                                        GNUNET_NO,
1021                                        &transmit_search_request,
1022                                        sc);  
1023 }
1024
1025
1026 /**
1027  * Shutdown any existing connection to the FS
1028  * service and try to establish a fresh one
1029  * (and then re-transmit our search request).
1030  *
1031  * @param sc the search to reconnec
1032  */
1033 static void 
1034 try_reconnect (struct GNUNET_FS_SearchContext *sc)
1035 {
1036   if (NULL != sc->client)
1037     {
1038       GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1039       sc->client = NULL;
1040     }
1041   sc->task
1042     = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
1043                                     GNUNET_TIME_UNIT_SECONDS,
1044                                     &do_reconnect,
1045                                     sc);
1046 }
1047
1048
1049 /**
1050  * Start search for content, internal API.
1051  *
1052  * @param h handle to the file sharing subsystem
1053  * @param uri specifies the search parameters; can be
1054  *        a KSK URI or an SKS URI.
1055  * @param anonymity desired level of anonymity
1056  * @param options options for the search
1057  * @param cctx initial value for the client context
1058  * @param parent parent search (for namespace update searches)
1059  * @return context that can be used to control the search
1060  */
1061 static struct GNUNET_FS_SearchContext *
1062 search_start (struct GNUNET_FS_Handle *h,
1063               const struct GNUNET_FS_Uri *uri,
1064               uint32_t anonymity,
1065               enum GNUNET_FS_SearchOptions options,
1066               void *cctx,
1067               struct GNUNET_FS_SearchContext *parent)
1068 {
1069   struct GNUNET_FS_SearchContext *sc;
1070   struct GNUNET_FS_ProgressInfo pi;
1071
1072   sc = GNUNET_malloc (sizeof(struct GNUNET_FS_SearchContext));
1073   sc->h = h;
1074   sc->options = options;
1075   sc->uri = GNUNET_FS_uri_dup (uri);
1076   sc->anonymity = anonymity;
1077   sc->start_time = GNUNET_TIME_absolute_get ();
1078   sc->parent = parent;
1079   sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1080   sc->client_info = cctx;
1081   if (NULL != parent)
1082     GNUNET_CONTAINER_DLL_insert (parent->child_head,
1083                                  parent->child_tail,
1084                                  sc);
1085   if (GNUNET_OK !=
1086       GNUNET_FS_search_start_searching_ (sc))
1087     {
1088       GNUNET_FS_uri_destroy (sc->uri);
1089       GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1090       GNUNET_free (sc);      
1091       return NULL;
1092     }
1093   pi.status = GNUNET_FS_STATUS_SEARCH_START;
1094   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1095   return sc;
1096 }
1097
1098
1099 /**
1100  * Build the request and actually initiate the search using the
1101  * GNUnet FS service.
1102  *
1103  * @param sc search context
1104  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1105  */
1106 int
1107 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc)
1108 {
1109   unsigned int i;
1110   const char *keyword;
1111   GNUNET_HashCode hc;
1112   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;  
1113   struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1114   size_t size;
1115
1116   GNUNET_assert (NULL == sc->client);
1117   if (GNUNET_FS_uri_test_ksk (sc->uri))
1118     {
1119       size = sizeof (struct SearchMessage) * sc->uri->data.ksk.keywordCount;
1120     }
1121   else
1122     {
1123       GNUNET_assert (GNUNET_FS_uri_test_sks (sc->uri));
1124       size = sizeof (struct SearchMessage);
1125     }
1126   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1127     {
1128       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1129                   _("Too many keywords specified for a single search."));
1130       return GNUNET_SYSERR;
1131     }
1132   if (GNUNET_FS_uri_test_ksk (sc->uri))
1133     {
1134       GNUNET_assert (0 != sc->uri->data.ksk.keywordCount);
1135       sc->requests = GNUNET_malloc (sizeof (struct SearchRequestEntry) *
1136                                     sc->uri->data.ksk.keywordCount);
1137       for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1138         {
1139           keyword = &sc->uri->data.ksk.keywords[i][1];
1140           GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
1141           pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
1142           GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
1143           GNUNET_CRYPTO_rsa_key_free (pk);
1144           GNUNET_CRYPTO_hash (&pub,
1145                               sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 
1146                               &sc->requests[i].query);
1147           sc->requests[i].mandatory = (sc->uri->data.ksk.keywords[i][0] == '+');
1148           if (sc->requests[i].mandatory)
1149             sc->mandatory_count++;
1150           sc->requests[i].results = GNUNET_CONTAINER_multihashmap_create (4);
1151           GNUNET_CRYPTO_hash (keyword,
1152                               strlen (keyword),
1153                               &sc->requests[i].key);
1154         }
1155     }
1156   sc->client = GNUNET_CLIENT_connect (sc->h->sched,
1157                                       "fs",
1158                                       sc->h->cfg);
1159   if (NULL == sc->client)
1160     return GNUNET_SYSERR;
1161   GNUNET_CLIENT_notify_transmit_ready (sc->client,
1162                                        size,
1163                                        GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1164                                        GNUNET_NO,
1165                                        &transmit_search_request,
1166                                        sc);
1167   return GNUNET_OK;
1168 }
1169
1170
1171 /**
1172  * Create SUSPEND event for the given search operation
1173  * and then clean up our state (without stop signal).
1174  *
1175  * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
1176  */
1177 static void
1178 search_signal_suspend (void *cls)
1179 {
1180   struct GNUNET_FS_SearchContext *sc = cls;
1181
1182   GNUNET_FS_end_top (sc->h, sc->top);
1183   /* FIXME: signal! */
1184   GNUNET_free (sc);
1185 }
1186
1187
1188 /**
1189  * Start search for content.
1190  *
1191  * @param h handle to the file sharing subsystem
1192  * @param uri specifies the search parameters; can be
1193  *        a KSK URI or an SKS URI.
1194  * @param anonymity desired level of anonymity
1195  * @param options options for the search
1196  * @param cctx initial value for the client context
1197  * @return context that can be used to control the search
1198  */
1199 struct GNUNET_FS_SearchContext *
1200 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1201                         const struct GNUNET_FS_Uri *uri,
1202                         uint32_t anonymity,
1203                         enum GNUNET_FS_SearchOptions options,
1204                         void *cctx)
1205 {
1206   struct GNUNET_FS_SearchContext *ret;
1207   ret = search_start (h, uri, anonymity, options, cctx, NULL);
1208   ret->top = GNUNET_FS_make_top (h, &search_signal_suspend, ret);
1209   return ret;
1210 }
1211
1212
1213 /**
1214  * Pause search.  
1215  *
1216  * @param sc context for the search that should be paused
1217  */
1218 void 
1219 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc)
1220 {
1221   struct GNUNET_FS_ProgressInfo pi;
1222
1223   if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1224     GNUNET_SCHEDULER_cancel (sc->h->sched,
1225                              sc->task);
1226   sc->task = GNUNET_SCHEDULER_NO_TASK;
1227   if (NULL != sc->client)
1228     GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1229   sc->client = NULL;
1230   // FIXME: make persistent!
1231   // FIXME: should this freeze all active probes?
1232   pi.status = GNUNET_FS_STATUS_SEARCH_PAUSED;
1233   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1234 }
1235
1236
1237 /**
1238  * Continue paused search.
1239  *
1240  * @param sc context for the search that should be resumed
1241  */
1242 void 
1243 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc)
1244 {
1245   struct GNUNET_FS_ProgressInfo pi;
1246
1247   GNUNET_assert (sc->client == NULL);
1248   GNUNET_assert (sc->task == GNUNET_SCHEDULER_NO_TASK);
1249   do_reconnect (sc, NULL);
1250   // FIXME: make persistent!
1251   pi.status = GNUNET_FS_STATUS_SEARCH_CONTINUED;
1252   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1253 }
1254
1255
1256 /**
1257  * Free the given search result.
1258  *
1259  * @param cls the global FS handle
1260  * @param key the key for the search result (unused)
1261  * @param value the search result to free
1262  * @return GNUNET_OK
1263  */
1264 static int
1265 search_result_free (void *cls,
1266                     const GNUNET_HashCode * key,
1267                     void *value)
1268 {
1269   struct GNUNET_FS_SearchContext *sc = cls;
1270   struct GNUNET_FS_Handle *h = sc->h;
1271   char pbuf[32];
1272   struct GNUNET_FS_SearchResult *sr = value;
1273   struct GNUNET_FS_ProgressInfo pi;
1274
1275   if (NULL != sr->download)
1276     {
1277       sr->download->search = NULL;
1278       pi.status = GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT;
1279       GNUNET_FS_download_make_status_ (&pi,
1280                                        sr->download);
1281       /* FIXME: promote download to top-level! */
1282       sr->download = NULL;     
1283     }
1284   pi.status = GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED;
1285   pi.value.search.specifics.result_stopped.cctx = sr->client_info;
1286   pi.value.search.specifics.result_stopped.meta = sr->meta;
1287   pi.value.search.specifics.result_stopped.uri = sr->uri;
1288   sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1289   GNUNET_break (NULL == sr->client_info);
1290   if (sr->serialization != NULL)
1291     {
1292       GNUNET_snprintf (pbuf,
1293                        sizeof (pbuf),
1294                        "%s%s%s",
1295                        "search-results",
1296                        DIR_SEPARATOR_STR,
1297                        sc->serialization);
1298       GNUNET_FS_remove_sync_file_ (sc->h,
1299                                    pbuf,
1300                                    sr->serialization);
1301       GNUNET_free (sr->serialization);
1302     }
1303   GNUNET_FS_uri_destroy (sr->uri);
1304   GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1305   if (sr->probe_ctx != NULL)
1306     GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);    
1307   if (sr->probe_cancel_task != GNUNET_SCHEDULER_NO_TASK)
1308     GNUNET_SCHEDULER_cancel (h->sched,
1309                              sr->probe_cancel_task);    
1310   GNUNET_free (sr);
1311   return GNUNET_OK;
1312 }
1313
1314
1315 /**
1316  * Stop search for content.
1317  *
1318  * @param sc context for the search that should be stopped
1319  */
1320 void 
1321 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc)
1322 {
1323   struct GNUNET_FS_ProgressInfo pi;
1324   unsigned int i;
1325   struct GNUNET_FS_SearchContext *parent;
1326
1327   if (sc->top != NULL)
1328     GNUNET_FS_end_top (sc->h, sc->top);
1329   // FIXME: make un-persistent!
1330   if (NULL != (parent = sc->parent))
1331     {
1332       GNUNET_CONTAINER_DLL_remove (parent->child_head,
1333                                    parent->child_tail,
1334                                    sc);
1335       sc->parent = NULL;
1336     }
1337   while (NULL != sc->child_head)
1338     GNUNET_FS_search_stop (sc->child_head);
1339   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1340                                          &search_result_free,
1341                                          sc);
1342   pi.status = GNUNET_FS_STATUS_SEARCH_STOPPED;
1343   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
1344   GNUNET_break (NULL == sc->client_info);
1345   if (sc->task != GNUNET_SCHEDULER_NO_TASK)
1346     GNUNET_SCHEDULER_cancel (sc->h->sched,
1347                              sc->task);
1348   if (NULL != sc->client)
1349     GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
1350   GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1351   if (sc->requests != NULL)
1352     {
1353       GNUNET_assert (GNUNET_FS_uri_test_ksk (sc->uri));
1354       for (i=0;i<sc->uri->data.ksk.keywordCount;i++)
1355         GNUNET_CONTAINER_multihashmap_destroy (sc->requests[i].results);
1356     }
1357   GNUNET_free_non_null (sc->requests);
1358   GNUNET_free_non_null (sc->emsg);
1359   GNUNET_FS_uri_destroy (sc->uri);
1360   GNUNET_free (sc);
1361 }
1362
1363 /* end of fs_search.c */