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