e7ebeac82d7550f21fae452775a90966055208da
[oweals/gnunet.git] / src / fs / fs_publish.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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_publish.c
23  * @brief publish a file or directory in GNUnet
24  * @see http://gnunet.org/encoding
25  * @author Krista Bennett
26  * @author Christian Grothoff
27  *
28  * TODO:
29  * - indexing cleanup: unindex on failure (can wait)
30  * - datastore reservation support (optimization)
31  * - location URIs (publish with anonymity-level zero)
32  */
33
34 #include "platform.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_fs_service.h"
39 #include "fs.h"
40 #include "fs_tree.h"
41
42 #define DEBUG_PUBLISH GNUNET_NO
43
44
45 /**
46  * Context for "ds_put_cont".
47  */
48 struct PutContCtx
49 {
50   /**
51    * Current publishing context.
52    */
53   struct GNUNET_FS_PublishContext *sc;
54
55   /**
56    * Specific file with the block.
57    */
58   struct GNUNET_FS_FileInformation *p;
59
60   /**
61    * Function to run next, if any (can be NULL).
62    */
63   GNUNET_SCHEDULER_Task cont;
64
65   /**
66    * Closure for cont.
67    */
68   void *cont_cls;
69 };
70
71
72 /**
73  * Fill in all of the generic fields for 
74  * a publish event and call the callback.
75  *
76  * @param pi structure to fill in
77  * @param sc overall publishing context
78  * @param p file information for the file being published
79  * @param offset where in the file are we so far
80  * @return value returned from callback
81  */
82 void *
83 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
84                                 struct GNUNET_FS_PublishContext *sc,
85                                 const struct GNUNET_FS_FileInformation *p,
86                                 uint64_t offset)
87 {
88   pi->value.publish.sc = sc;
89   pi->value.publish.fi = p;
90   pi->value.publish.cctx
91     = p->client_info;
92   pi->value.publish.pctx
93     = (NULL == p->dir) ? NULL : p->dir->client_info;
94   pi->value.publish.filename = p->filename;
95   pi->value.publish.size 
96     = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
97   pi->value.publish.eta 
98     = GNUNET_TIME_calculate_eta (p->start_time,
99                                  offset,
100                                  pi->value.publish.size);
101   pi->value.publish.completed = offset;
102   pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
103   pi->value.publish.anonymity = p->anonymity;
104   return sc->h->upcb (sc->h->upcb_cls,
105                       pi);
106 }
107
108
109 /**
110  * Cleanup the publish context, we're done with it.
111  *
112  * @param cls struct to clean up after
113  * @param tc scheduler context
114  */
115 static void
116 publish_cleanup (void *cls,
117                  const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   struct GNUNET_FS_PublishContext *pc = cls;
120
121   GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
122   if (pc->namespace != NULL)
123     GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
124   GNUNET_free_non_null (pc->nid);  
125   GNUNET_free_non_null (pc->nuid);
126   GNUNET_free_non_null (pc->serialization);
127   if (pc->dsh != NULL)
128     {
129       GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
130       pc->dsh = NULL;
131     }
132   if (pc->client != NULL)
133     GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
134   GNUNET_free (pc);
135 }
136
137
138 /**
139  * Function called by the datastore API with
140  * the result from the PUT request.
141  *
142  * @param cls our closure
143  * @param success GNUNET_OK on success
144  * @param msg error message (or NULL)
145  */
146 static void
147 ds_put_cont (void *cls,
148              int success,
149              const char *msg)
150 {
151   struct PutContCtx *pcc = cls;
152   struct GNUNET_FS_ProgressInfo pi;
153
154   if (GNUNET_SYSERR == pcc->sc->in_network_wait)
155     {
156       /* we were aborted in the meantime, finish shutdown! */
157       GNUNET_SCHEDULER_add_continuation (pcc->sc->h->sched,                                      
158                                          &publish_cleanup,
159                                          pcc->sc,
160                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
161       GNUNET_free (pcc);
162       return;
163     }
164   GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
165   pcc->sc->in_network_wait = GNUNET_NO;
166   if (GNUNET_OK != success)
167     {
168       GNUNET_asprintf (&pcc->p->emsg, 
169                        _("Upload failed: %s"),
170                        msg);
171       pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
172       pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
173       pi.value.publish.specifics.error.message = pcc->p->emsg;
174       pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
175     }
176   if (NULL != pcc->cont)
177     pcc->sc->upload_task 
178       = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
179                                             GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
180                                             pcc->cont,
181                                             pcc->cont_cls);
182   GNUNET_free (pcc);
183 }
184
185
186 /**
187  * Generate the callback that signals clients
188  * that a file (or directory) has been completely
189  * published.
190  *
191  * @param p the completed upload
192  * @param sc context of the publication
193  */
194 static void 
195 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
196                            struct GNUNET_FS_PublishContext *sc)
197 {
198   struct GNUNET_FS_ProgressInfo pi;
199   
200   pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
201   pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
202   pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
203   p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
204                                                    GNUNET_ntohll (p->chk_uri->data.chk.file_length));
205 }
206
207
208 /**
209  * Generate the callback that signals clients
210  * that a file (or directory) has encountered
211  * a problem during publication.
212  *
213  * @param p the upload that had trouble
214  * @param sc context of the publication
215  * @param emsg error message
216  */
217 static void 
218 signal_publish_error (struct GNUNET_FS_FileInformation *p,
219                       struct GNUNET_FS_PublishContext *sc,
220                       const char *emsg)
221 {
222   struct GNUNET_FS_ProgressInfo pi;
223   
224   p->emsg = GNUNET_strdup (emsg);
225   pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
226   pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
227   pi.value.publish.specifics.error.message =emsg;
228   p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
229 }
230
231
232 /**
233  * We've finished publishing the SBlock as part of a larger upload.
234  * Check the result and complete the larger upload.
235  *
236  * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
237  * @param uri URI of the published SBlock
238  * @param emsg NULL on success, otherwise error message
239  */
240 static void
241 publish_sblocks_cont (void *cls,
242                       const struct GNUNET_FS_Uri *uri,
243                       const char *emsg)
244 {
245   struct GNUNET_FS_PublishContext *pc = cls;
246   if (NULL != emsg)
247     {
248       signal_publish_error (pc->fi,
249                             pc,
250                             emsg);
251       GNUNET_FS_publish_sync_ (pc);
252       return;
253     }  
254   // FIXME: release the datastore reserve here!
255   signal_publish_completion (pc->fi, pc);
256   pc->all_done = GNUNET_YES;
257   GNUNET_FS_publish_sync_ (pc);
258 }
259
260
261 /**
262  * We are almost done publishing the structure,
263  * add SBlocks (if needed).
264  *
265  * @param sc overall upload data
266  */
267 static void
268 publish_sblock (struct GNUNET_FS_PublishContext *sc)
269 {
270   if (NULL != sc->namespace)
271     GNUNET_FS_publish_sks (sc->h,
272                            sc->namespace,
273                            sc->nid,
274                            sc->nuid,
275                            sc->fi->meta,
276                            sc->fi->chk_uri,
277                            sc->fi->expirationTime,
278                            sc->fi->anonymity,
279                            sc->fi->priority,
280                            sc->options,
281                            &publish_sblocks_cont,
282                            sc);
283   else
284     publish_sblocks_cont (sc, NULL, NULL);
285 }
286
287
288 /**
289  * We've finished publishing a KBlock as part of a larger upload.
290  * Check the result and continue the larger upload.
291  *
292  * @param cls the "struct GNUNET_FS_PublishContext*"
293  *        of the larger upload
294  * @param uri URI of the published blocks
295  * @param emsg NULL on success, otherwise error message
296  */
297 static void
298 publish_kblocks_cont (void *cls,
299                       const struct GNUNET_FS_Uri *uri,
300                       const char *emsg)
301 {
302   struct GNUNET_FS_PublishContext *pc = cls;
303   struct GNUNET_FS_FileInformation *p = pc->fi_pos;
304
305   if (NULL != emsg)
306     {
307       signal_publish_error (p, pc, emsg);
308       GNUNET_FS_file_information_sync_ (p);
309       GNUNET_FS_publish_sync_ (pc);
310       pc->upload_task 
311         = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
312                                               GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
313                                               &GNUNET_FS_publish_main_,
314                                               pc);
315       return;
316     }
317   if (NULL != p->dir)
318     signal_publish_completion (p, pc);    
319   /* move on to next file */
320   if (NULL != p->next)
321     pc->fi_pos = p->next;
322   else
323     pc->fi_pos = p->dir;
324   GNUNET_FS_publish_sync_ (pc);
325   pc->upload_task 
326     = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
327                                           GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
328                                           &GNUNET_FS_publish_main_,
329                                           pc);
330 }
331
332
333 /**
334  * Function called by the tree encoder to obtain
335  * a block of plaintext data (for the lowest level
336  * of the tree).
337  *
338  * @param cls our publishing context
339  * @param offset identifies which block to get
340  * @param max (maximum) number of bytes to get; returning
341  *        fewer will also cause errors
342  * @param buf where to copy the plaintext buffer
343  * @param emsg location to store an error message (on error)
344  * @return number of bytes copied to buf, 0 on error
345  */
346 static size_t
347 block_reader (void *cls,
348               uint64_t offset,
349               size_t max, 
350               void *buf,
351               char **emsg)
352 {
353   struct GNUNET_FS_PublishContext *sc = cls;
354   struct GNUNET_FS_FileInformation *p;
355   size_t pt_size;
356   const char *dd;
357
358   p = sc->fi_pos;
359   if (p->is_directory)
360     {
361       pt_size = GNUNET_MIN(max,
362                            p->data.dir.dir_size - offset);
363       dd = p->data.dir.dir_data;
364       memcpy (buf,
365               &dd[offset],
366               pt_size);
367     }
368   else
369     {
370       pt_size = GNUNET_MIN(max,
371                            p->data.file.file_size - offset);
372       if (pt_size == 0)
373         return 0; /* calling reader with pt_size==0 
374                      might free buf, so don't! */
375       if (pt_size !=
376           p->data.file.reader (p->data.file.reader_cls,
377                                offset,
378                                pt_size,
379                                buf,
380                                emsg))
381         return 0;
382     }
383   return pt_size;
384 }
385
386
387 /**
388  * The tree encoder has finished processing a
389  * file.   Call it's finish method and deal with
390  * the final result.
391  *
392  * @param cls our publishing context
393  * @param tc scheduler's task context (not used)
394  */
395 static void 
396 encode_cont (void *cls,
397              const struct GNUNET_SCHEDULER_TaskContext *tc)
398 {
399   struct GNUNET_FS_PublishContext *sc = cls;
400   struct GNUNET_FS_FileInformation *p;
401   struct GNUNET_FS_ProgressInfo pi;
402   char *emsg;
403   
404   p = sc->fi_pos;
405   GNUNET_FS_tree_encoder_finish (p->te,
406                                  &p->chk_uri,
407                                  &emsg);
408   p->te = NULL;
409   if (NULL != emsg)
410     {
411       GNUNET_asprintf (&p->emsg, 
412                        _("Upload failed: %s"),
413                        emsg);
414       GNUNET_free (emsg);
415       pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
416       pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
417       pi.value.publish.specifics.error.message = p->emsg;
418       p->client_info =  GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
419     }
420   /* continue with main */
421   sc->upload_task 
422     = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
423                                           GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
424                                           &GNUNET_FS_publish_main_,
425                                           sc);
426 }
427
428
429 /**
430  * Function called asking for the current (encoded)
431  * block to be processed.  After processing the
432  * client should either call "GNUNET_FS_tree_encode_next"
433  * or (on error) "GNUNET_FS_tree_encode_finish".
434  *
435  * @param cls closure
436  * @param query the query for the block (key for lookup in the datastore)
437  * @param offset offset of the block in the file
438  * @param type type of the block (IBLOCK or DBLOCK)
439  * @param block the (encrypted) block
440  * @param block_size size of block (in bytes)
441  */
442 static void 
443 block_proc (void *cls,
444             const GNUNET_HashCode *query,
445             uint64_t offset,
446             enum GNUNET_BLOCK_Type type,
447             const void *block,
448             uint16_t block_size)
449 {
450   struct GNUNET_FS_PublishContext *sc = cls;
451   struct GNUNET_FS_FileInformation *p;
452   struct PutContCtx * dpc_cls;
453   struct OnDemandBlock odb;
454
455   p = sc->fi_pos;
456   if (NULL == sc->dsh)
457     {
458       sc->upload_task
459         = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
460                                               GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
461                                               &GNUNET_FS_publish_main_,
462                                               sc);
463       return;
464     }
465   
466   GNUNET_assert (GNUNET_NO == sc->in_network_wait);
467   sc->in_network_wait = GNUNET_YES;
468   dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
469   dpc_cls->cont = &GNUNET_FS_publish_main_;
470   dpc_cls->cont_cls = sc;
471   dpc_cls->sc = sc;
472   dpc_cls->p = p;
473   if ( (! p->is_directory) &&
474        (GNUNET_YES == p->data.file.do_index) &&
475        (type == GNUNET_BLOCK_TYPE_DBLOCK) )
476     {
477 #if DEBUG_PUBLISH
478       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479                   "Indexing block `%s' for offset %llu with index size %u\n",
480                   GNUNET_h2s (query),
481                   (unsigned long long) offset,
482                   sizeof (struct OnDemandBlock));
483 #endif
484       odb.offset = GNUNET_htonll (offset);
485       odb.file_id = p->data.file.file_id;
486       GNUNET_DATASTORE_put (sc->dsh,
487                             sc->rid,
488                             query,
489                             sizeof(struct OnDemandBlock),
490                             &odb,
491                             GNUNET_BLOCK_TYPE_ONDEMAND,
492                             p->priority,
493                             p->anonymity,
494                             p->expirationTime,
495                             -2, 1,
496                             GNUNET_CONSTANTS_SERVICE_TIMEOUT,
497                             &ds_put_cont,
498                             dpc_cls);     
499       return;
500     }
501 #if DEBUG_PUBLISH
502   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503               "Publishing block `%s' for offset %llu with size %u\n",
504               GNUNET_h2s (query),
505               (unsigned long long) offset,
506               (unsigned int) block_size);
507 #endif
508   GNUNET_DATASTORE_put (sc->dsh,
509                         sc->rid,
510                         query,
511                         block_size,
512                         block,
513                         type,
514                         p->priority,
515                         p->anonymity,
516                         p->expirationTime,
517                         -2, 1,
518                         GNUNET_CONSTANTS_SERVICE_TIMEOUT,
519                         &ds_put_cont,
520                         dpc_cls);
521 }
522
523
524 /**
525  * Function called with information about our
526  * progress in computing the tree encoding.
527  *
528  * @param cls closure
529  * @param offset where are we in the file
530  * @param pt_block plaintext of the currently processed block
531  * @param pt_size size of pt_block
532  * @param depth depth of the block in the tree
533  */
534 static void 
535 progress_proc (void *cls,
536                uint64_t offset,
537                const void *pt_block,
538                size_t pt_size,
539                unsigned int depth)
540 {                      
541   struct GNUNET_FS_PublishContext *sc = cls;
542   struct GNUNET_FS_FileInformation *p;
543   struct GNUNET_FS_ProgressInfo pi;
544
545   p = sc->fi_pos;
546   pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
547   pi.value.publish.specifics.progress.data = pt_block;
548   pi.value.publish.specifics.progress.offset = offset;
549   pi.value.publish.specifics.progress.data_len = pt_size;
550   pi.value.publish.specifics.progress.depth = depth;
551   p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
552 }
553
554
555 /**
556  * We are uploading a file or directory; load (if necessary) the next
557  * block into memory, encrypt it and send it to the FS service.  Then
558  * continue with the main task.
559  *
560  * @param sc overall upload data
561  */
562 static void
563 publish_content (struct GNUNET_FS_PublishContext *sc) 
564 {
565   struct GNUNET_FS_FileInformation *p;
566   char *emsg;
567   struct GNUNET_FS_DirectoryBuilder *db;
568   struct GNUNET_FS_FileInformation *dirpos;
569   void *raw_data;
570   uint64_t size;
571
572   p = sc->fi_pos;
573   GNUNET_assert (p != NULL);
574   if (NULL == p->te)
575     {
576       if (p->is_directory)
577         {
578           db = GNUNET_FS_directory_builder_create (p->meta);
579           dirpos = p->data.dir.entries;
580           while (NULL != dirpos)
581             {
582               if (dirpos->is_directory)
583                 {
584                   raw_data = dirpos->data.dir.dir_data;
585                   dirpos->data.dir.dir_data = NULL;
586                 }
587               else
588                 {
589                   raw_data = NULL;
590                   if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
591                        (dirpos->data.file.file_size > 0) )
592                     {
593                       raw_data = GNUNET_malloc (dirpos->data.file.file_size);
594                       emsg = NULL;
595                       if (dirpos->data.file.file_size !=
596                           dirpos->data.file.reader (dirpos->data.file.reader_cls,
597                                                     0,
598                                                     dirpos->data.file.file_size,
599                                                     raw_data,
600                                                     &emsg))
601                         {
602                           GNUNET_free_non_null (emsg);
603                           GNUNET_free (raw_data);
604                           raw_data = NULL;
605                         } 
606                     }
607                 }
608               GNUNET_FS_directory_builder_add (db,
609                                                dirpos->chk_uri,
610                                                dirpos->meta,
611                                                raw_data);
612               GNUNET_free_non_null (raw_data);
613               dirpos = dirpos->next;
614             }
615           GNUNET_FS_directory_builder_finish (db,
616                                               &p->data.dir.dir_size,
617                                               &p->data.dir.dir_data);
618           GNUNET_FS_file_information_sync_ (p);
619         }
620       size = (p->is_directory) 
621         ? p->data.dir.dir_size 
622         : p->data.file.file_size;
623       p->te = GNUNET_FS_tree_encoder_create (sc->h,
624                                              size,
625                                              sc,
626                                              &block_reader,
627                                              &block_proc,
628                                              &progress_proc,
629                                              &encode_cont);
630
631     }
632   GNUNET_FS_tree_encoder_next (p->te);
633 }
634
635
636 /**
637  * Process the response (or lack thereof) from
638  * the "fs" service to our 'start index' request.
639  *
640  * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
641  * @param msg the response we got
642  */
643 static void
644 process_index_start_response (void *cls,
645                               const struct GNUNET_MessageHeader *msg)
646 {
647   struct GNUNET_FS_PublishContext *sc = cls;
648   struct GNUNET_FS_FileInformation *p;
649   const char *emsg;
650   uint16_t msize;
651
652   GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
653   sc->client = NULL;
654   p = sc->fi_pos;
655   if (msg == NULL)
656     {
657       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
659                   p->filename,
660                   _("timeout on index-start request to `fs' service"));
661       p->data.file.do_index = GNUNET_NO;
662       GNUNET_FS_file_information_sync_ (p);
663       publish_content (sc);
664       return;
665     }
666   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
667     {
668       msize = ntohs (msg->size);
669       emsg = (const char *) &msg[1];
670       if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
671            (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
672         emsg = gettext_noop ("unknown error");
673       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
674                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
675                   p->filename,
676                   gettext (emsg));
677       p->data.file.do_index = GNUNET_NO;
678       GNUNET_FS_file_information_sync_ (p);
679       publish_content (sc);
680       return;
681     }
682   p->data.file.index_start_confirmed = GNUNET_YES;
683   /* success! continue with indexing */
684   GNUNET_FS_file_information_sync_ (p);
685   publish_content (sc);
686 }
687
688
689 /**
690  * Function called once the hash computation over an
691  * indexed file has completed.
692  *
693  * @param cls closure, our publishing context
694  * @param res resulting hash, NULL on error
695  */
696 static void 
697 hash_for_index_cb (void *cls,
698                    const GNUNET_HashCode *
699                    res)
700 {
701   struct GNUNET_FS_PublishContext *sc = cls;
702   struct GNUNET_FS_FileInformation *p;
703   struct IndexStartMessage *ism;
704   size_t slen;
705   struct GNUNET_CLIENT_Connection *client;
706   uint32_t dev;
707   uint64_t ino;
708   char *fn;
709
710   p = sc->fi_pos;
711   if (NULL == res) 
712     {
713       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
714                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
715                   p->filename,
716                   _("failed to compute hash"));
717       p->data.file.do_index = GNUNET_NO;
718       GNUNET_FS_file_information_sync_ (p);
719       publish_content (sc);
720       return;
721     }
722   if (GNUNET_YES == p->data.file.index_start_confirmed)
723     {
724       publish_content (sc);
725       return;
726     }
727   fn = GNUNET_STRINGS_filename_expand (p->filename);
728   GNUNET_assert (fn != NULL);
729   slen = strlen (fn) + 1;
730   if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
731     {
732       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
733                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
734                   fn,
735                   _("filename too long"));
736       GNUNET_free (fn);
737       p->data.file.do_index = GNUNET_NO;
738       GNUNET_FS_file_information_sync_ (p);
739       publish_content (sc);
740       return;
741     }
742 #if DEBUG_PUBLISH
743   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744               "Hash of indexed file `%s' is `%s'\n",
745               p->data.file.filename,
746               GNUNET_h2s (res));
747 #endif
748   client = GNUNET_CLIENT_connect (sc->h->sched,
749                                   "fs",
750                                   sc->h->cfg);
751   if (NULL == client)
752     {
753       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
754                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
755                   p->filename,
756                   _("could not connect to `fs' service"));
757       p->data.file.do_index = GNUNET_NO;
758       publish_content (sc);
759       GNUNET_free (fn);
760       return;
761     }
762   if (p->data.file.have_hash != GNUNET_YES)
763     {
764       p->data.file.file_id = *res;
765       p->data.file.have_hash = GNUNET_YES;
766       GNUNET_FS_file_information_sync_ (p);
767     }
768   ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
769                        slen);
770   ism->header.size = htons(sizeof(struct IndexStartMessage) +
771                            slen);
772   ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
773   if (GNUNET_OK ==
774       GNUNET_DISK_file_get_identifiers (p->filename,
775                                         &dev,
776                                         &ino))
777     {
778       ism->device = htonl (dev);
779       ism->inode = GNUNET_htonll(ino);
780     }
781   else
782     {
783       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784                   _("Failed to get file identifiers for `%s'\n"),
785                   p->filename);
786     }
787   ism->file_id = *res;
788   memcpy (&ism[1],
789           fn,
790           slen);
791   GNUNET_free (fn);
792   sc->client = client;
793   GNUNET_break (GNUNET_YES ==
794                 GNUNET_CLIENT_transmit_and_get_response (client,
795                                                          &ism->header,
796                                                          GNUNET_TIME_UNIT_FOREVER_REL,
797                                                          GNUNET_YES,
798                                                          &process_index_start_response,
799                                                          sc));
800   GNUNET_free (ism);
801 }
802
803
804 /**
805  * Main function that performs the upload.
806  *
807  * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
808  * @param tc task context
809  */
810 void
811 GNUNET_FS_publish_main_ (void *cls,
812                          const struct GNUNET_SCHEDULER_TaskContext *tc)
813 {
814   struct GNUNET_FS_PublishContext *pc = cls;
815   struct GNUNET_FS_ProgressInfo pi;
816   struct GNUNET_FS_FileInformation *p;
817   char *fn;
818
819   pc->upload_task = GNUNET_SCHEDULER_NO_TASK;  
820   p = pc->fi_pos;
821   if (NULL == p)
822     {
823       /* upload of entire hierarchy complete,
824          publish namespace entries */
825       GNUNET_FS_publish_sync_ (pc);
826       publish_sblock (pc);
827       return;
828     }
829   /* find starting position */
830   while ( (p->is_directory) &&
831           (NULL != p->data.dir.entries) &&
832           (NULL == p->emsg) &&
833           (NULL == p->data.dir.entries->chk_uri) )
834     {
835       p = p->data.dir.entries;
836       pc->fi_pos = p;
837       GNUNET_FS_publish_sync_ (pc);
838     }
839   /* abort on error */
840   if (NULL != p->emsg)
841     {
842       /* error with current file, abort all
843          related files as well! */
844       while (NULL != p->dir)
845         {
846           fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
847                                                        EXTRACTOR_METATYPE_FILENAME);
848           p = p->dir;
849           if (fn != NULL)
850             {
851               GNUNET_asprintf (&p->emsg, 
852                                _("Recursive upload failed at `%s': %s"),
853                                fn,
854                                p->emsg);
855               GNUNET_free (fn);
856             }
857           else
858             {
859               GNUNET_asprintf (&p->emsg, 
860                                _("Recursive upload failed: %s"),
861                                p->emsg);              
862             }
863           pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
864           pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
865           pi.value.publish.specifics.error.message = p->emsg;
866           p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
867         }
868       pc->all_done = GNUNET_YES;
869       GNUNET_FS_publish_sync_ (pc);
870       return;
871     }
872   /* handle completion */
873   if (NULL != p->chk_uri)
874     {
875       GNUNET_FS_publish_sync_ (pc);
876       /* upload of "p" complete, publish KBlocks! */
877       if (p->keywords != NULL)
878         {
879           GNUNET_FS_publish_ksk (pc->h,
880                                  p->keywords,
881                                  p->meta,
882                                  p->chk_uri,
883                                  p->expirationTime,
884                                  p->anonymity,
885                                  p->priority,
886                                  pc->options,
887                                  &publish_kblocks_cont,
888                                  pc);
889         }
890       else
891         {
892           publish_kblocks_cont (pc,
893                                 p->chk_uri,
894                                 NULL);
895         }
896       return;
897     }
898   if ( (!p->is_directory) &&
899        (p->data.file.do_index) )
900     {
901       if (NULL == p->filename)
902         {
903           p->data.file.do_index = GNUNET_NO;
904           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
905                       _("Can not index file `%s': %s.  Will try to insert instead.\n"),
906                       "<no-name>",
907                       _("needs to be an actual file"));
908           GNUNET_FS_file_information_sync_ (p);
909           publish_content (pc);
910           return;
911         }      
912       if (p->data.file.have_hash)
913         {
914           hash_for_index_cb (pc,
915                              &p->data.file.file_id);
916         }
917       else
918         {
919           p->start_time = GNUNET_TIME_absolute_get ();
920           GNUNET_CRYPTO_hash_file (pc->h->sched,
921                                    GNUNET_SCHEDULER_PRIORITY_IDLE,
922                                    p->filename,
923                                    HASHING_BLOCKSIZE,
924                                    &hash_for_index_cb,
925                                    pc);
926         }
927       return;
928     }
929   publish_content (pc);
930 }
931
932
933 /**
934  * Signal the FS's progress function that we are starting
935  * an upload.
936  *
937  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
938  * @param fi the entry in the publish-structure
939  * @param length length of the file or directory
940  * @param meta metadata for the file or directory (can be modified)
941  * @param uri pointer to the keywords that will be used for this entry (can be modified)
942  * @param anonymity pointer to selected anonymity level (can be modified)
943  * @param priority pointer to selected priority (can be modified)
944  * @param do_index should we index?
945  * @param expirationTime pointer to selected expiration time (can be modified)
946  * @param client_info pointer to client context set upon creation (can be modified)
947  * @return GNUNET_OK to continue (always)
948  */
949 static int
950 fip_signal_start(void *cls,
951                  struct GNUNET_FS_FileInformation *fi,
952                  uint64_t length,
953                  struct GNUNET_CONTAINER_MetaData *meta,
954                  struct GNUNET_FS_Uri **uri,
955                  uint32_t *anonymity,
956                  uint32_t *priority,
957                  int *do_index,
958                  struct GNUNET_TIME_Absolute *expirationTime,
959                  void **client_info)
960 {
961   struct GNUNET_FS_PublishContext *sc = cls;
962   struct GNUNET_FS_ProgressInfo pi;
963
964   pi.status = GNUNET_FS_STATUS_PUBLISH_START;
965   *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
966   GNUNET_FS_file_information_sync_ (fi);
967   return GNUNET_OK;
968 }
969
970
971 /**
972  * Signal the FS's progress function that we are suspending
973  * an upload.
974  *
975  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
976  * @param fi the entry in the publish-structure
977  * @param length length of the file or directory
978  * @param meta metadata for the file or directory (can be modified)
979  * @param uri pointer to the keywords that will be used for this entry (can be modified)
980  * @param anonymity pointer to selected anonymity level (can be modified)
981  * @param priority pointer to selected priority (can be modified)
982  * @param do_index should we index?
983  * @param expirationTime pointer to selected expiration time (can be modified)
984  * @param client_info pointer to client context set upon creation (can be modified)
985  * @return GNUNET_OK to continue (always)
986  */
987 static int
988 fip_signal_suspend(void *cls,
989                    struct GNUNET_FS_FileInformation *fi,
990                    uint64_t length,
991                    struct GNUNET_CONTAINER_MetaData *meta,
992                    struct GNUNET_FS_Uri **uri,
993                    uint32_t *anonymity,
994                    uint32_t *priority,
995                    int *do_index,
996                    struct GNUNET_TIME_Absolute *expirationTime,
997                    void **client_info)
998 {
999   struct GNUNET_FS_PublishContext*sc = cls;
1000   struct GNUNET_FS_ProgressInfo pi;
1001   uint64_t off;
1002
1003   GNUNET_free_non_null (fi->serialization);
1004   fi->serialization = NULL;    
1005   off = (fi->chk_uri == NULL) ? 0 : length;
1006   pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1007   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1008   *client_info = NULL;
1009   if (NULL != sc->dsh)
1010     {
1011       GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
1012       sc->dsh = NULL;
1013     }
1014   return GNUNET_OK;
1015 }
1016
1017
1018 /**
1019  * Create SUSPEND event for the given publish operation
1020  * and then clean up our state (without stop signal).
1021  *
1022  * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1023  */
1024 void
1025 GNUNET_FS_publish_signal_suspend_ (void *cls)
1026 {
1027   struct GNUNET_FS_PublishContext *pc = cls;
1028
1029   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1030     {
1031       GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1032       pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1033     }
1034   GNUNET_FS_file_information_inspect (pc->fi,
1035                                       &fip_signal_suspend,
1036                                       pc);
1037   GNUNET_FS_end_top (pc->h, pc->top);
1038   publish_cleanup (pc, NULL);
1039 }
1040
1041
1042 /**
1043  * Publish a file or directory.
1044  *
1045  * @param h handle to the file sharing subsystem
1046  * @param fi information about the file or directory structure to publish
1047  * @param namespace namespace to publish the file in, NULL for no namespace
1048  * @param nid identifier to use for the publishd content in the namespace
1049  *        (can be NULL, must be NULL if namespace is NULL)
1050  * @param nuid update-identifier that will be used for future updates 
1051  *        (can be NULL, must be NULL if namespace or nid is NULL)
1052  * @param options options for the publication 
1053  * @return context that can be used to control the publish operation
1054  */
1055 struct GNUNET_FS_PublishContext *
1056 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1057                          struct GNUNET_FS_FileInformation *fi,
1058                          struct GNUNET_FS_Namespace *namespace,
1059                          const char *nid,
1060                          const char *nuid,
1061                          enum GNUNET_FS_PublishOptions options)
1062 {
1063   struct GNUNET_FS_PublishContext *ret;
1064   struct GNUNET_DATASTORE_Handle *dsh;
1065
1066   if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1067     {
1068       dsh = GNUNET_DATASTORE_connect (h->cfg,
1069                                       h->sched);
1070       if (NULL == dsh)
1071         return NULL;
1072     }
1073   else
1074     {
1075       dsh = NULL;
1076     }
1077   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1078   ret->dsh = dsh;
1079   ret->h = h;
1080   ret->fi = fi;
1081   ret->namespace = namespace;
1082   if (namespace != NULL)
1083     {
1084       namespace->rc++;
1085       GNUNET_assert (NULL != nid);
1086       ret->nid = GNUNET_strdup (nid);
1087       if (NULL != nuid)
1088         ret->nuid = GNUNET_strdup (nuid);
1089     }
1090   /* signal start */
1091   GNUNET_FS_file_information_inspect (ret->fi,
1092                                       &fip_signal_start,
1093                                       ret);
1094   ret->fi_pos = ret->fi;
1095   ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1096   GNUNET_FS_publish_sync_ (ret);
1097   // FIXME: calculate space needed for "fi"
1098   // and reserve as first task (then trigger
1099   // "publish_main" from that continuation)!
1100   ret->upload_task 
1101     = GNUNET_SCHEDULER_add_with_priority (h->sched,
1102                                           GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1103                                           &GNUNET_FS_publish_main_,
1104                                           ret);
1105   return ret;
1106 }
1107
1108
1109 /**
1110  * Signal the FS's progress function that we are stopping
1111  * an upload.
1112  *
1113  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1114  * @param fi the entry in the publish-structure
1115  * @param length length of the file or directory
1116  * @param meta metadata for the file or directory (can be modified)
1117  * @param uri pointer to the keywords that will be used for this entry (can be modified)
1118  * @param anonymity pointer to selected anonymity level (can be modified)
1119  * @param priority pointer to selected priority (can be modified)
1120  * @param do_index should we index?
1121  * @param expirationTime pointer to selected expiration time (can be modified)
1122  * @param client_info pointer to client context set upon creation (can be modified)
1123  * @return GNUNET_OK to continue (always)
1124  */
1125 static int
1126 fip_signal_stop(void *cls,
1127                 struct GNUNET_FS_FileInformation *fi,
1128                 uint64_t length,
1129                 struct GNUNET_CONTAINER_MetaData *meta,
1130                 struct GNUNET_FS_Uri **uri,
1131                 uint32_t *anonymity,
1132                 uint32_t *priority,
1133                 int *do_index,
1134                 struct GNUNET_TIME_Absolute *expirationTime,
1135                 void **client_info)
1136 {
1137   struct GNUNET_FS_PublishContext*sc = cls;
1138   struct GNUNET_FS_ProgressInfo pi;
1139   uint64_t off;
1140
1141   if (fi->serialization != NULL) 
1142     {
1143       GNUNET_FS_remove_sync_file_ (sc->h,
1144                                    GNUNET_FS_SYNC_PATH_FILE_INFO,
1145                                    fi->serialization);
1146       GNUNET_free (fi->serialization);
1147       fi->serialization = NULL;
1148     }
1149   off = (fi->chk_uri == NULL) ? 0 : length;
1150   pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1151   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1152   *client_info = NULL;
1153   return GNUNET_OK;
1154 }
1155
1156
1157 /**
1158  * Stop an upload.  Will abort incomplete uploads (but 
1159  * not remove blocks that have already been publishd) or
1160  * simply clean up the state for completed uploads.
1161  * Must NOT be called from within the event callback!
1162  *
1163  * @param pc context for the upload to stop
1164  */
1165 void 
1166 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1167 {
1168   GNUNET_FS_end_top (pc->h, pc->top);
1169   if (NULL != pc->dsh)
1170     {
1171       GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1172       pc->dsh = NULL;
1173     }
1174   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1175     {
1176       GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1177       pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1178     }
1179   if (pc->serialization != NULL) 
1180     {
1181       GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1182       GNUNET_free (pc->serialization);
1183       pc->serialization = NULL;
1184     }
1185   GNUNET_FS_file_information_inspect (pc->fi,
1186                                       &fip_signal_stop,
1187                                       pc);
1188   if (GNUNET_YES == pc->in_network_wait)
1189     {
1190       pc->in_network_wait = GNUNET_SYSERR;
1191       return;
1192     }
1193   publish_cleanup (pc, NULL);
1194 }
1195
1196
1197 /**
1198  * Context for the KSK publication.
1199  */
1200 struct PublishKskContext
1201 {
1202
1203   /**
1204    * Keywords to use.
1205    */
1206   struct GNUNET_FS_Uri *ksk_uri;
1207
1208   /**
1209    * Global FS context.
1210    */
1211   struct GNUNET_FS_Handle *h;
1212
1213   /**
1214    * The master block that we are sending
1215    * (in plaintext), has "mdsize+slen" more
1216    * bytes than the struct would suggest.
1217    */
1218   struct KBlock *kb;
1219
1220   /**
1221    * Buffer of the same size as "kb" for
1222    * the encrypted version.
1223    */ 
1224   struct KBlock *cpy;
1225
1226   /**
1227    * Handle to the datastore, NULL if we are just
1228    * simulating.
1229    */
1230   struct GNUNET_DATASTORE_Handle *dsh;
1231
1232   /**
1233    * Function to call once we're done.
1234    */
1235   GNUNET_FS_PublishContinuation cont;
1236
1237   /**
1238    * Closure for cont.
1239    */ 
1240   void *cont_cls;
1241
1242   /**
1243    * When should the KBlocks expire?
1244    */
1245   struct GNUNET_TIME_Absolute expirationTime;
1246
1247   /**
1248    * Size of the serialized metadata.
1249    */
1250   ssize_t mdsize;
1251
1252   /**
1253    * Size of the (CHK) URI as a string.
1254    */
1255   size_t slen;
1256
1257   /**
1258    * Keyword that we are currently processing.
1259    */
1260   unsigned int i;
1261
1262   /**
1263    * Anonymity level for the KBlocks.
1264    */
1265   uint32_t anonymity;
1266
1267   /**
1268    * Priority for the KBlocks.
1269    */
1270   uint32_t priority;
1271 };
1272
1273
1274 /**
1275  * Continuation of "GNUNET_FS_publish_ksk" that performs
1276  * the actual publishing operation (iterating over all
1277  * of the keywords).
1278  *
1279  * @param cls closure of type "struct PublishKskContext*"
1280  * @param tc unused
1281  */
1282 static void
1283 publish_ksk_cont (void *cls,
1284                   const struct GNUNET_SCHEDULER_TaskContext *tc);
1285
1286
1287 /**
1288  * Function called by the datastore API with
1289  * the result from the PUT request.
1290  *
1291  * @param cls closure of type "struct PublishKskContext*"
1292  * @param success GNUNET_OK on success
1293  * @param msg error message (or NULL)
1294  */
1295 static void
1296 kb_put_cont (void *cls,
1297              int success,
1298              const char *msg)
1299 {
1300   struct PublishKskContext *pkc = cls;
1301
1302   if (GNUNET_OK != success)
1303     {
1304       if (NULL != pkc->dsh)
1305         {
1306           GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1307           pkc->dsh = NULL;
1308         }
1309       GNUNET_free (pkc->cpy);
1310       GNUNET_free (pkc->kb);
1311       pkc->cont (pkc->cont_cls,
1312                  NULL,
1313                  msg);
1314       GNUNET_FS_uri_destroy (pkc->ksk_uri);
1315       GNUNET_free (pkc);
1316       return;
1317     }
1318   GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1319                                      &publish_ksk_cont,
1320                                      pkc,
1321                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1322 }
1323
1324
1325 /**
1326  * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1327  * publishing operation (iterating over all of the keywords).
1328  *
1329  * @param cls closure of type "struct PublishKskContext*"
1330  * @param tc unused
1331  */
1332 static void
1333 publish_ksk_cont (void *cls,
1334                   const struct GNUNET_SCHEDULER_TaskContext *tc)
1335 {
1336   struct PublishKskContext *pkc = cls;
1337   const char *keyword;
1338   GNUNET_HashCode key;
1339   GNUNET_HashCode query;
1340   struct GNUNET_CRYPTO_AesSessionKey skey;
1341   struct GNUNET_CRYPTO_AesInitializationVector iv;
1342   struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1343
1344
1345   if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1346        (NULL == pkc->dsh) )
1347     {
1348       if (NULL != pkc->dsh)
1349         {
1350           GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1351           pkc->dsh = NULL;
1352         }
1353       GNUNET_free (pkc->cpy);
1354       GNUNET_free (pkc->kb);
1355       pkc->cont (pkc->cont_cls,
1356                  pkc->ksk_uri,
1357                  NULL);
1358       GNUNET_FS_uri_destroy (pkc->ksk_uri);
1359       GNUNET_free (pkc);
1360       return;
1361     }
1362   keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1363   /* first character of keyword indicates if it is
1364      mandatory or not -- ignore for hashing */
1365   GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1366   GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1367   GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1368                              pkc->slen + pkc->mdsize,
1369                              &skey,
1370                              &iv,
1371                              &pkc->cpy[1]);
1372   pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1373   GNUNET_assert (NULL != pk);
1374   GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1375   GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1376                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1377                       &query);
1378   GNUNET_assert (GNUNET_OK == 
1379                  GNUNET_CRYPTO_rsa_sign (pk,
1380                                          &pkc->cpy->purpose,
1381                                          &pkc->cpy->signature));
1382   GNUNET_CRYPTO_rsa_key_free (pk);
1383   GNUNET_DATASTORE_put (pkc->dsh,
1384                         0,
1385                         &query,
1386                         pkc->mdsize + 
1387                         sizeof (struct KBlock) + 
1388                         pkc->slen,
1389                         pkc->cpy,
1390                         GNUNET_BLOCK_TYPE_KBLOCK, 
1391                         pkc->priority,
1392                         pkc->anonymity,
1393                         pkc->expirationTime,
1394                         -2, 1,
1395                         GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1396                         &kb_put_cont,
1397                         pkc);
1398 }
1399
1400
1401 /**
1402  * Publish a CHK under various keywords on GNUnet.
1403  *
1404  * @param h handle to the file sharing subsystem
1405  * @param ksk_uri keywords to use
1406  * @param meta metadata to use
1407  * @param uri URI to refer to in the KBlock
1408  * @param expirationTime when the KBlock expires
1409  * @param anonymity anonymity level for the KBlock
1410  * @param priority priority for the KBlock
1411  * @param options publication options
1412  * @param cont continuation
1413  * @param cont_cls closure for cont
1414  */
1415 void
1416 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1417                        const struct GNUNET_FS_Uri *ksk_uri,
1418                        const struct GNUNET_CONTAINER_MetaData *meta,
1419                        const struct GNUNET_FS_Uri *uri,
1420                        struct GNUNET_TIME_Absolute expirationTime,
1421                        uint32_t anonymity,
1422                        uint32_t priority,
1423                        enum GNUNET_FS_PublishOptions options,
1424                        GNUNET_FS_PublishContinuation cont,
1425                        void *cont_cls)
1426 {
1427   struct PublishKskContext *pkc;
1428   char *uris;
1429   size_t size;
1430   char *kbe;
1431   char *sptr;
1432
1433   pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1434   pkc->h = h;
1435   pkc->expirationTime = expirationTime;
1436   pkc->anonymity = anonymity;
1437   pkc->priority = priority;
1438   pkc->cont = cont;
1439   pkc->cont_cls = cont_cls;
1440   if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1441     {
1442       pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1443                                            h->sched);
1444       if (pkc->dsh == NULL)
1445         {
1446           cont (cont_cls, NULL, _("Could not connect to datastore."));
1447           GNUNET_free (pkc);
1448           return;
1449         }
1450     }
1451   if (meta == NULL)
1452     pkc->mdsize = 0;
1453   else
1454     pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1455   GNUNET_assert (pkc->mdsize >= 0);
1456   uris = GNUNET_FS_uri_to_string (uri);
1457   pkc->slen = strlen (uris) + 1;
1458   size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1459   if (size > MAX_KBLOCK_SIZE)
1460     {
1461       size = MAX_KBLOCK_SIZE;
1462       pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1463     }
1464   pkc->kb = GNUNET_malloc (size);
1465   kbe = (char *) &pkc->kb[1];
1466   memcpy (kbe, uris, pkc->slen);
1467   GNUNET_free (uris);
1468   sptr = &kbe[pkc->slen];
1469   if (meta != NULL)
1470     pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1471                                                         &sptr,
1472                                                         pkc->mdsize,
1473                                                         GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1474   if (pkc->mdsize == -1)
1475     {
1476       GNUNET_break (0);
1477       GNUNET_free (pkc->kb);
1478       if (pkc->dsh != NULL)
1479         {
1480           GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1481           pkc->dsh = NULL;
1482         }
1483       cont (cont_cls, NULL, _("Internal error."));
1484       GNUNET_free (pkc);
1485       return;
1486     }
1487   size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1488
1489   pkc->cpy = GNUNET_malloc (size);
1490   pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + 
1491                                   sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1492                                   pkc->mdsize + 
1493                                   pkc->slen);
1494   pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1495   pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1496   GNUNET_SCHEDULER_add_continuation (h->sched,
1497                                      &publish_ksk_cont,
1498                                      pkc,
1499                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1500 }
1501
1502
1503 /**
1504  * Context for the SKS publication.
1505  */
1506 struct PublishSksContext
1507 {
1508
1509   /**
1510    * Global FS context.
1511    */
1512   struct GNUNET_FS_Uri *uri;
1513
1514   /**
1515    * Handle to the datastore.
1516    */
1517   struct GNUNET_DATASTORE_Handle *dsh;
1518
1519   /**
1520    * Function to call once we're done.
1521    */
1522   GNUNET_FS_PublishContinuation cont;
1523
1524   /**
1525    * Closure for cont.
1526    */ 
1527   void *cont_cls;
1528
1529 };
1530
1531
1532 /**
1533  * Function called by the datastore API with
1534  * the result from the PUT (SBlock) request.
1535  *
1536  * @param cls closure of type "struct PublishSksContext*"
1537  * @param success GNUNET_OK on success
1538  * @param msg error message (or NULL)
1539  */
1540 static void
1541 sb_put_cont (void *cls,
1542              int success,
1543              const char *msg)
1544 {
1545   struct PublishSksContext *psc = cls;
1546
1547   if (NULL != psc->dsh)
1548     {
1549       GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1550       psc->dsh = NULL;
1551     }
1552   if (GNUNET_OK != success)
1553     psc->cont (psc->cont_cls,
1554                NULL,
1555                msg);
1556   else
1557     psc->cont (psc->cont_cls,
1558                psc->uri,
1559                NULL);
1560   GNUNET_FS_uri_destroy (psc->uri);
1561   GNUNET_free (psc);
1562 }
1563
1564
1565 /**
1566  * Publish an SBlock on GNUnet.
1567  *
1568  * @param h handle to the file sharing subsystem
1569  * @param namespace namespace to publish in
1570  * @param identifier identifier to use
1571  * @param update update identifier to use
1572  * @param meta metadata to use
1573  * @param uri URI to refer to in the SBlock
1574  * @param expirationTime when the SBlock expires
1575  * @param anonymity anonymity level for the SBlock
1576  * @param priority priority for the SBlock
1577  * @param options publication options
1578  * @param cont continuation
1579  * @param cont_cls closure for cont
1580  */
1581 void
1582 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1583                        struct GNUNET_FS_Namespace *namespace,
1584                        const char *identifier,
1585                        const char *update,
1586                        const struct GNUNET_CONTAINER_MetaData *meta,
1587                        const struct GNUNET_FS_Uri *uri,
1588                        struct GNUNET_TIME_Absolute expirationTime,
1589                        uint32_t anonymity,
1590                        uint32_t priority,
1591                        enum GNUNET_FS_PublishOptions options,
1592                        GNUNET_FS_PublishContinuation cont,
1593                        void *cont_cls)
1594 {
1595   struct PublishSksContext *psc;
1596   struct GNUNET_CRYPTO_AesSessionKey sk;
1597   struct GNUNET_CRYPTO_AesInitializationVector iv;
1598   struct GNUNET_FS_Uri *sks_uri;
1599   char *uris;
1600   size_t size;
1601   size_t slen;
1602   size_t nidlen;
1603   size_t idlen;
1604   ssize_t mdsize;
1605   struct SBlock *sb;
1606   struct SBlock *sb_enc;
1607   char *dest;
1608   struct GNUNET_CONTAINER_MetaData *mmeta;
1609   GNUNET_HashCode key;         /* hash of thisId = key */
1610   GNUNET_HashCode id;          /* hash of hc = identifier */
1611   GNUNET_HashCode query;       /* id ^ nsid = DB query */
1612
1613   if (NULL == meta)
1614     mmeta = GNUNET_CONTAINER_meta_data_create ();
1615   else
1616     mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1617   uris = GNUNET_FS_uri_to_string (uri);
1618   slen = strlen (uris) + 1;
1619   idlen = strlen (identifier);
1620   if (update == NULL)
1621     update = "";
1622   nidlen = strlen (update) + 1;
1623   mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1624   size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1625   if (size > MAX_SBLOCK_SIZE)
1626     {
1627       size = MAX_SBLOCK_SIZE;
1628       mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1629     }
1630   sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1631   dest = (char *) &sb[1];
1632   memcpy (dest, update, nidlen);
1633   dest += nidlen;
1634   memcpy (dest, uris, slen);
1635   GNUNET_free (uris);
1636   dest += slen;
1637   mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1638                                                  &dest,
1639                                                  mdsize, 
1640                                                  GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1641   GNUNET_CONTAINER_meta_data_destroy (mmeta);
1642   if (mdsize == -1)
1643     {
1644       GNUNET_break (0);
1645       GNUNET_free (sb);
1646       cont (cont_cls,
1647             NULL,
1648             _("Internal error."));
1649       return;
1650     }
1651   size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1652   sb_enc = GNUNET_malloc (size);
1653   GNUNET_CRYPTO_hash (identifier, idlen, &key);
1654   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1655   sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1656   sks_uri->type = sks;
1657   GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1658   GNUNET_CRYPTO_hash (&sb_enc->subspace,
1659                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1660                       &sks_uri->data.sks.namespace);
1661   sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1662   GNUNET_CRYPTO_hash_xor (&id, 
1663                           &sks_uri->data.sks.namespace, 
1664                           &sb_enc->identifier);
1665   GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1666   GNUNET_CRYPTO_aes_encrypt (&sb[1],
1667                              size - sizeof (struct SBlock),
1668                              &sk,
1669                              &iv,
1670                              &sb_enc[1]);
1671   sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1672   sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1673                                + sizeof(struct SBlock)
1674                                - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1675   GNUNET_assert (GNUNET_OK == 
1676                  GNUNET_CRYPTO_rsa_sign (namespace->key,
1677                                          &sb_enc->purpose,
1678                                          &sb_enc->signature));
1679   psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1680   psc->uri = sks_uri;
1681   psc->cont = cont;
1682   psc->cont_cls = cont_cls;
1683   if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1684     {
1685       GNUNET_free (sb_enc);
1686       GNUNET_free (sb);
1687       sb_put_cont (psc,
1688                    GNUNET_OK,
1689                    NULL);
1690       return;
1691     }
1692   psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1693   if (NULL == psc->dsh)
1694     {
1695       GNUNET_free (sb_enc);
1696       GNUNET_free (sb);
1697       sb_put_cont (psc,
1698                    GNUNET_NO,
1699                    _("Failed to connect to datastore."));
1700       return;
1701     }
1702   GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1703                           &id,
1704                           &query);  
1705   GNUNET_DATASTORE_put (psc->dsh,
1706                         0,
1707                         &sb_enc->identifier,
1708                         size,
1709                         sb_enc,
1710                         GNUNET_BLOCK_TYPE_SBLOCK, 
1711                         priority,
1712                         anonymity,
1713                         expirationTime,
1714                         -2, 1,
1715                         GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1716                         &sb_put_cont,
1717                         psc);
1718   GNUNET_free (sb);
1719   GNUNET_free (sb_enc);
1720 }
1721
1722 /* end of fs_publish.c */