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