2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
30 * - code-sharing with unindex (can wait)
31 * - persistence support (can wait)
32 * - datastore reservation support (optimization)
33 * - location URIs (publish with anonymity-level zero)
37 #include "gnunet_constants.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_util_lib.h"
40 #include "gnunet_fs_service.h"
43 #define DEBUG_PUBLISH GNUNET_YES
46 * Maximum allowed size for a KBlock.
48 #define MAX_KBLOCK_SIZE 60000
51 * Maximum allowed size for an SBlock.
53 #define MAX_SBLOCK_SIZE 60000
57 * Main function that performs the upload.
58 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
59 * @param tc task context
63 const struct GNUNET_SCHEDULER_TaskContext *tc);
67 * Context for "ds_put_cont".
72 * Current publishing context.
74 struct GNUNET_FS_PublishContext *sc;
77 * Specific file with the block.
79 struct GNUNET_FS_FileInformation *p;
82 * Function to run next, if any (can be NULL).
84 GNUNET_SCHEDULER_Task cont;
94 * Fill in all of the generic fields for
97 * @param pc structure to fill in
98 * @param sc overall publishing context
99 * @param p file information for the file being published
102 make_publish_status (struct GNUNET_FS_ProgressInfo *pi,
103 struct GNUNET_FS_PublishContext *sc,
104 const struct GNUNET_FS_FileInformation *p)
106 pi->value.publish.sc = sc;
107 pi->value.publish.fi = p;
108 pi->value.publish.cctx
110 pi->value.publish.pctx
111 = (NULL == p->dir) ? NULL : p->dir->client_info;
112 pi->value.publish.size
113 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
114 pi->value.publish.eta
115 = GNUNET_TIME_calculate_eta (p->start_time,
117 pi->value.publish.size);
118 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
119 pi->value.publish.completed = p->publish_offset;
120 pi->value.publish.anonymity = p->anonymity;
125 * Cleanup the publish context, we're done
128 * @param pc struct to clean up after
131 publish_cleanup (struct GNUNET_FS_PublishContext *sc)
133 GNUNET_FS_file_information_destroy (sc->fi, NULL, NULL);
134 GNUNET_FS_namespace_delete (sc->namespace, GNUNET_NO);
135 GNUNET_free_non_null (sc->nid);
136 GNUNET_free_non_null (sc->nuid);
137 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
143 * Function called by the datastore API with
144 * the result from the PUT request.
146 * @param cls our closure
147 * @param success GNUNET_OK on success
148 * @param msg error message (or NULL)
151 ds_put_cont (void *cls,
155 struct PutContCtx *pcc = cls;
156 struct GNUNET_FS_ProgressInfo pi;
158 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
160 /* we were aborted in the meantime,
162 publish_cleanup (pcc->sc);
165 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
166 pcc->sc->in_network_wait = GNUNET_NO;
167 if (GNUNET_OK != success)
169 GNUNET_asprintf (&pcc->p->emsg,
170 _("Upload failed: %s"),
172 GNUNET_FS_file_information_sync (pcc->p);
173 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
174 make_publish_status (&pi, pcc->sc, pcc->p);
175 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
176 pi.value.publish.specifics.error.message = pcc->p->emsg;
178 = pcc->sc->h->upcb (pcc->sc->h->upcb_cls,
182 GNUNET_FS_file_information_sync (pcc->p);
183 if (NULL != pcc->cont)
185 = GNUNET_SCHEDULER_add_delayed (pcc->sc->h->sched,
187 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
188 GNUNET_SCHEDULER_NO_TASK,
189 GNUNET_TIME_UNIT_ZERO,
197 * Generate the callback that signals clients
198 * that a file (or directory) has been completely
201 * @param p the completed upload
202 * @param sc context of the publication
205 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
206 struct GNUNET_FS_PublishContext *sc)
208 struct GNUNET_FS_ProgressInfo pi;
210 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
211 make_publish_status (&pi, sc, p);
212 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
213 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
215 = sc->h->upcb (sc->h->upcb_cls,
221 * Generate the callback that signals clients
222 * that a file (or directory) has encountered
223 * a problem during publication.
225 * @param p the upload that had trouble
226 * @param sc context of the publication
227 * @param emsg error message
230 signal_publish_error (struct GNUNET_FS_FileInformation *p,
231 struct GNUNET_FS_PublishContext *sc,
234 struct GNUNET_FS_ProgressInfo pi;
236 p->emsg = GNUNET_strdup (emsg);
237 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
238 make_publish_status (&pi, sc, p);
239 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
240 pi.value.publish.specifics.error.message =emsg;
242 = sc->h->upcb (sc->h->upcb_cls,
248 * We've finished publishing the SBlock as part of a larger upload.
249 * Check the result and complete the larger upload.
251 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
252 * @param uri URI of the published SBlock
253 * @param emsg NULL on success, otherwise error message
256 publish_sblocks_cont (void *cls,
257 const struct GNUNET_FS_Uri *uri,
260 struct GNUNET_FS_PublishContext *sc = cls;
263 signal_publish_error (sc->fi,
268 // FIXME: release the datastore reserve here!
269 signal_publish_completion (sc->fi, sc);
274 * We are almost done publishing the structure,
275 * add SBlocks (if needed).
277 * @param sc overall upload data
280 publish_sblock (struct GNUNET_FS_PublishContext *sc)
282 if (NULL != sc->namespace)
283 GNUNET_FS_publish_sks (sc->h,
289 sc->fi->expirationTime,
293 &publish_sblocks_cont,
296 publish_sblocks_cont (sc, NULL, NULL);
301 * We've finished publishing a KBlock
302 * as part of a larger upload. Check
303 * the result and continue the larger
306 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
307 * @param uri URI of the published blocks
308 * @param emsg NULL on success, otherwise error message
311 publish_kblocks_cont (void *cls,
312 const struct GNUNET_FS_Uri *uri,
315 struct GNUNET_FS_PublishContext *sc = cls;
316 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
320 signal_publish_error (p, sc, emsg);
322 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
324 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
325 GNUNET_SCHEDULER_NO_TASK,
326 GNUNET_TIME_UNIT_ZERO,
331 GNUNET_FS_file_information_sync (p);
333 signal_publish_completion (p, sc);
334 /* move on to next file */
336 sc->fi_pos = p->next;
340 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
342 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
343 GNUNET_SCHEDULER_NO_TASK,
344 GNUNET_TIME_UNIT_ZERO,
351 * Compute the depth of the CHK tree.
353 * @param flen file length for which to compute the depth
354 * @return depth of the tree
357 compute_depth (uint64_t flen)
359 unsigned int treeDepth;
367 if (fl * CHK_PER_INODE < fl)
369 /* integer overflow, this is a HUGE file... */
372 fl = fl * CHK_PER_INODE;
379 * Compute the size of the current IBlock.
381 * @param height height of the IBlock in the tree (aka overall
382 * number of tree levels minus depth); 0 == DBlock
383 * @param offset current offset in the overall file
384 * @return size of the corresponding IBlock
387 compute_iblock_size (unsigned int height,
395 GNUNET_assert (height > 0);
396 bds = DBLOCK_SIZE; /* number of bytes each CHK at level "i"
398 for (i=0;i<height;i++)
399 bds *= CHK_PER_INODE;
403 /* we were triggered at the end of a full block */
408 /* we were triggered at the end of the file */
409 bds /= CHK_PER_INODE;
414 return (uint16_t) (ret * sizeof(struct ContentHashKey));
419 * Compute the offset of the CHK for the
420 * current block in the IBlock above.
422 * @param height height of the IBlock in the tree (aka overall
423 * number of tree levels minus depth); 0 == DBlock
424 * @param offset current offset in the overall file
425 * @return (array of CHKs') offset in the above IBlock
428 compute_chk_offset (unsigned int height,
435 bds = DBLOCK_SIZE; /* number of bytes each CHK at level "i"
437 for (i=0;i<height;i++)
438 bds *= CHK_PER_INODE;
439 GNUNET_assert (0 == (offset % bds));
441 return ret % CHK_PER_INODE;
446 * We are uploading a file or directory; load (if necessary) the next
447 * block into memory, encrypt it and send it to the FS service. Then
448 * continue with the main task.
450 * @param sc overall upload data
451 * @param p specific file or directory for which kblocks
455 publish_content (struct GNUNET_FS_PublishContext *sc,
456 struct GNUNET_FS_FileInformation *p)
458 struct GNUNET_FS_ProgressInfo pi;
459 struct ContentHashKey *mychk;
460 const void *pt_block;
463 char iob[DBLOCK_SIZE];
464 char enc[DBLOCK_SIZE];
465 struct GNUNET_CRYPTO_AesSessionKey sk;
466 struct GNUNET_CRYPTO_AesInitializationVector iv;
469 struct GNUNET_FS_DirectoryBuilder *db;
470 struct GNUNET_FS_FileInformation *dirpos;
473 struct PutContCtx * dpc_cls;
475 // FIXME: figure out how to share this code
477 size = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
478 if (NULL == p->chk_tree)
482 db = GNUNET_FS_directory_builder_create (p->meta);
483 dirpos = p->data.dir.entries;
484 while (NULL != dirpos)
486 if (dirpos->is_directory)
488 raw_data = dirpos->data.dir.dir_data;
489 dirpos->data.dir.dir_data = NULL;
494 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
495 (dirpos->data.file.file_size > 0) )
497 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
499 if (dirpos->data.file.file_size !=
500 dirpos->data.file.reader (dirpos->data.file.reader_cls,
502 dirpos->data.file.file_size,
506 GNUNET_free_non_null (emsg);
507 GNUNET_free (raw_data);
512 GNUNET_FS_directory_builder_add (db,
516 GNUNET_free_non_null (raw_data);
517 dirpos = dirpos->next;
519 GNUNET_FS_directory_builder_finish (db,
520 &p->data.dir.dir_size,
521 &p->data.dir.dir_data);
522 size = p->data.dir.dir_size;
524 p->chk_tree_depth = compute_depth (size);
525 p->chk_tree = GNUNET_malloc (p->chk_tree_depth *
526 sizeof (struct ContentHashKey) *
528 p->current_depth = p->chk_tree_depth;
530 if (p->current_depth == p->chk_tree_depth)
534 pt_size = GNUNET_MIN(DBLOCK_SIZE,
535 p->data.dir.dir_size - p->publish_offset);
536 dd = p->data.dir.dir_data;
537 pt_block = &dd[p->publish_offset];
541 pt_size = GNUNET_MIN(DBLOCK_SIZE,
542 p->data.file.file_size - p->publish_offset);
545 p->data.file.reader (p->data.file.reader_cls,
551 GNUNET_asprintf (&p->emsg,
552 _("Upload failed: %s"),
555 GNUNET_FS_file_information_sync (p);
556 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
557 make_publish_status (&pi, sc, p);
558 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
559 pi.value.publish.specifics.error.message = p->emsg;
561 = sc->h->upcb (sc->h->upcb_cls,
563 /* continue with main (to propagate error up) */
565 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
567 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
568 GNUNET_SCHEDULER_NO_TASK,
569 GNUNET_TIME_UNIT_ZERO,
579 pt_size = compute_iblock_size (p->chk_tree_depth - p->current_depth,
581 pt_block = &p->chk_tree[p->current_depth *
584 off = compute_chk_offset (p->chk_tree_depth - p->current_depth,
586 mychk = &p->chk_tree[(p->current_depth-1)*CHK_PER_INODE+off];
587 GNUNET_CRYPTO_hash (pt_block, pt_size, &mychk->key);
588 GNUNET_CRYPTO_hash_to_aes_key (&mychk->key, &sk, &iv);
589 GNUNET_CRYPTO_aes_encrypt (pt_block,
594 // NOTE: this block below is all that really differs
595 // between publish/unindex! Parameterize & move this code!
596 // FIXME: something around here would need to change
601 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
603 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
604 GNUNET_SCHEDULER_NO_TASK,
605 GNUNET_TIME_UNIT_ZERO,
611 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
612 sc->in_network_wait = GNUNET_YES;
613 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
614 dpc_cls->cont = &do_upload;
615 dpc_cls->cont_cls = sc;
617 GNUNET_DATASTORE_put (sc->dsh,
622 (p->current_depth == p->chk_tree_depth)
623 ? GNUNET_DATASTORE_BLOCKTYPE_DBLOCK
624 : GNUNET_DATASTORE_BLOCKTYPE_IBLOCK,
628 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
632 if (p->current_depth == p->chk_tree_depth)
634 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
635 make_publish_status (&pi, sc, p);
636 pi.value.publish.specifics.progress.data = pt_block;
637 pi.value.publish.specifics.progress.offset = p->publish_offset;
638 pi.value.publish.specifics.progress.data_len = pt_size;
640 = sc->h->upcb (sc->h->upcb_cls,
643 GNUNET_CRYPTO_hash (enc, pt_size, &mychk->query);
644 if (p->current_depth == p->chk_tree_depth)
646 p->publish_offset += pt_size;
647 if ( (p->publish_offset == size) ||
648 (0 == p->publish_offset % (CHK_PER_INODE * DBLOCK_SIZE) ) )
653 if ( (off == CHK_PER_INODE) ||
654 (p->publish_offset == size) )
657 p->current_depth = p->chk_tree_depth;
659 if (0 == p->current_depth)
661 p->chk_uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
662 p->chk_uri->type = chk;
663 p->chk_uri->data.chk.chk = p->chk_tree[0];
664 p->chk_uri->data.chk.file_length = size;
665 GNUNET_free (p->chk_tree);
672 * Main function that performs the upload.
673 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
674 * @param tc task context
677 do_upload (void *cls,
678 const struct GNUNET_SCHEDULER_TaskContext *tc)
680 struct GNUNET_FS_PublishContext *sc = cls;
681 struct GNUNET_FS_ProgressInfo pi;
682 struct GNUNET_FS_FileInformation *p;
685 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
689 /* upload of entire hierarchy complete,
690 publish namespace entries */
694 /* find starting position */
695 while ( (p->is_directory) &&
696 (NULL != p->data.dir.entries) &&
698 (NULL == p->data.dir.entries->chk_uri) )
700 p = p->data.dir.entries;
706 /* error with current file, abort all
707 related files as well! */
708 while (NULL != p->dir)
710 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
713 GNUNET_asprintf (&p->emsg,
714 _("Recursive upload failed at `%s'"),
717 GNUNET_FS_file_information_sync (p);
718 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
719 make_publish_status (&pi, sc, p);
720 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
721 pi.value.publish.specifics.error.message = p->emsg;
723 = sc->h->upcb (sc->h->upcb_cls,
728 /* handle completion */
729 if (NULL != p->chk_uri)
731 /* upload of "p" complete, publish KBlocks! */
732 GNUNET_FS_publish_ksk (sc->h,
740 &publish_kblocks_cont,
744 if ( (!p->is_directory) &&
745 (p->data.file.do_index) )
747 // FIXME: need to pre-compute hash over
748 // the entire file and ask FS to prepare
752 publish_content (sc, p);
757 * Signal the FS's progress function that we are starting
760 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
761 * @param fi the entry in the publish-structure
762 * @param length length of the file or directory
763 * @param meta metadata for the file or directory (can be modified)
764 * @param uri pointer to the keywords that will be used for this entry (can be modified)
765 * @param anonymity pointer to selected anonymity level (can be modified)
766 * @param priority pointer to selected priority (can be modified)
767 * @param expirationTime pointer to selected expiration time (can be modified)
768 * @param client_info pointer to client context set upon creation (can be modified)
769 * @return GNUNET_OK to continue (always)
772 fip_signal_start(void *cls,
773 struct GNUNET_FS_FileInformation *fi,
775 struct GNUNET_CONTAINER_MetaData *meta,
776 struct GNUNET_FS_Uri **uri,
777 unsigned int *anonymity,
778 unsigned int *priority,
779 struct GNUNET_TIME_Absolute *expirationTime,
782 struct GNUNET_FS_PublishContext *sc = cls;
783 struct GNUNET_FS_ProgressInfo pi;
785 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
786 make_publish_status (&pi, sc, fi);
787 *client_info = sc->h->upcb (sc->h->upcb_cls,
794 * Publish a file or directory.
796 * @param h handle to the file sharing subsystem
797 * @param ctx initial value to use for the '*ctx'
798 * in the callback (for the GNUNET_FS_STATUS_PUBLISH_START event).
799 * @param fi information about the file or directory structure to publish
800 * @param namespace namespace to publish the file in, NULL for no namespace
801 * @param nid identifier to use for the publishd content in the namespace
802 * (can be NULL, must be NULL if namespace is NULL)
803 * @param nuid update-identifier that will be used for future updates
804 * (can be NULL, must be NULL if namespace or nid is NULL)
805 * @param options options for the publication
806 * @return context that can be used to control the publish operation
808 struct GNUNET_FS_PublishContext *
809 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
811 struct GNUNET_FS_FileInformation *fi,
812 struct GNUNET_FS_Namespace *namespace,
815 enum GNUNET_FS_PublishOptions options)
817 struct GNUNET_FS_PublishContext *ret;
818 struct GNUNET_DATASTORE_Handle *dsh;
820 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
822 dsh = GNUNET_DATASTORE_connect (h->cfg,
831 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
834 ret->client_ctx = ctx;
836 ret->namespace = namespace;
837 if (namespace != NULL)
840 GNUNET_assert (NULL != nid);
841 ret->nid = GNUNET_strdup (nid);
843 ret->nuid = GNUNET_strdup (nuid);
845 // FIXME: make upload persistent!
848 GNUNET_FS_file_information_inspect (ret->fi,
851 ret->fi_pos = ret->fi;
853 // FIXME: calculate space needed for "fi"
854 // and reserve as first task (then trigger
855 // "do_upload" from that continuation)!
857 = GNUNET_SCHEDULER_add_delayed (h->sched,
859 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
860 GNUNET_SCHEDULER_NO_TASK,
861 GNUNET_TIME_UNIT_ZERO,
869 * Signal the FS's progress function that we are stopping
872 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
873 * @param fi the entry in the publish-structure
874 * @param length length of the file or directory
875 * @param meta metadata for the file or directory (can be modified)
876 * @param uri pointer to the keywords that will be used for this entry (can be modified)
877 * @param anonymity pointer to selected anonymity level (can be modified)
878 * @param priority pointer to selected priority (can be modified)
879 * @param expirationTime pointer to selected expiration time (can be modified)
880 * @param client_info pointer to client context set upon creation (can be modified)
881 * @return GNUNET_OK to continue (always)
884 fip_signal_stop(void *cls,
885 struct GNUNET_FS_FileInformation *fi,
887 struct GNUNET_CONTAINER_MetaData *meta,
888 struct GNUNET_FS_Uri **uri,
889 unsigned int *anonymity,
890 unsigned int *priority,
891 struct GNUNET_TIME_Absolute *expirationTime,
894 struct GNUNET_FS_PublishContext*sc = cls;
895 struct GNUNET_FS_ProgressInfo pi;
897 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
898 make_publish_status (&pi, sc, fi);
899 GNUNET_break (NULL ==
900 sc->h->upcb (sc->h->upcb_cls,
908 * Stop an upload. Will abort incomplete uploads (but
909 * not remove blocks that have already been publishd) or
910 * simply clean up the state for completed uploads.
912 * @param sc context for the upload to stop
915 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *sc)
917 if (GNUNET_SCHEDULER_NO_TASK != sc->upload_task)
918 GNUNET_SCHEDULER_cancel (sc->h->sched, sc->upload_task);
919 // FIXME: remove from persistence DB (?) --- think more about
920 // shutdown / persistent-resume APIs!!!
921 GNUNET_FS_file_information_inspect (sc->fi,
924 if (GNUNET_YES == sc->in_network_wait)
926 sc->in_network_wait = GNUNET_SYSERR;
929 publish_cleanup (sc);
934 * Context for the KSK publication.
936 struct PublishKskContext
942 struct GNUNET_FS_Uri *ksk_uri;
947 struct GNUNET_FS_Handle *h;
950 * The master block that we are sending
951 * (in plaintext), has "mdsize+slen" more
952 * bytes than the struct would suggest.
957 * Buffer of the same size as "kb" for
958 * the encrypted version.
963 * Handle to the datastore, NULL if we are just
966 struct GNUNET_DATASTORE_Handle *dsh;
969 * Function to call once we're done.
971 GNUNET_FS_PublishContinuation cont;
979 * When should the KBlocks expire?
981 struct GNUNET_TIME_Absolute expirationTime;
984 * Size of the serialized metadata.
989 * Size of the (CHK) URI as a string.
994 * Keyword that we are currently processing.
999 * Anonymity level for the KBlocks.
1001 unsigned int anonymity;
1004 * Priority for the KBlocks.
1006 unsigned int priority;
1011 * Continuation of "GNUNET_FS_publish_ksk" that performs
1012 * the actual publishing operation (iterating over all
1015 * @param cls closure of type "struct PublishKskContext*"
1019 publish_ksk_cont (void *cls,
1020 const struct GNUNET_SCHEDULER_TaskContext *tc);
1024 * Function called by the datastore API with
1025 * the result from the PUT request.
1027 * @param cls closure of type "struct PublishKskContext*"
1028 * @param success GNUNET_OK on success
1029 * @param msg error message (or NULL)
1032 kb_put_cont (void *cls,
1036 struct PublishKskContext *pkc = cls;
1038 if (GNUNET_OK != success)
1040 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1041 GNUNET_free (pkc->cpy);
1042 GNUNET_free (pkc->kb);
1043 pkc->cont (pkc->cont_cls,
1046 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1050 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1054 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1059 * Continuation of "GNUNET_FS_publish_ksk" that performs
1060 * the actual publishing operation (iterating over all
1063 * @param cls closure of type "struct PublishKskContext*"
1067 publish_ksk_cont (void *cls,
1068 const struct GNUNET_SCHEDULER_TaskContext *tc)
1070 struct PublishKskContext *pkc = cls;
1071 const char *keyword;
1072 GNUNET_HashCode key;
1073 GNUNET_HashCode query;
1074 struct GNUNET_CRYPTO_AesSessionKey skey;
1075 struct GNUNET_CRYPTO_AesInitializationVector iv;
1076 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1079 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1080 (NULL == pkc->dsh) )
1082 if (NULL != pkc->dsh)
1083 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1084 GNUNET_free (pkc->cpy);
1085 GNUNET_free (pkc->kb);
1086 pkc->cont (pkc->cont_cls,
1089 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1093 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1094 /* first character of keyword indicates if it is
1095 mandatory or not -- ignore for hashing */
1096 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1097 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1098 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1099 pkc->slen + pkc->mdsize,
1103 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1104 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1105 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1106 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1108 GNUNET_assert (GNUNET_OK ==
1109 GNUNET_CRYPTO_rsa_sign (pk,
1111 &pkc->cpy->signature));
1112 GNUNET_CRYPTO_rsa_key_free (pk);
1113 GNUNET_DATASTORE_put (pkc->dsh,
1117 sizeof (struct KBlock) +
1120 GNUNET_DATASTORE_BLOCKTYPE_KBLOCK,
1123 pkc->expirationTime,
1124 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1131 * Publish a CHK under various keywords on GNUnet.
1133 * @param h handle to the file sharing subsystem
1134 * @param ksk_uri keywords to use
1135 * @param meta metadata to use
1136 * @param uri URI to refer to in the KBlock
1137 * @param expirationTime when the KBlock expires
1138 * @param anonymity anonymity level for the KBlock
1139 * @param priority priority for the KBlock
1140 * @param options publication options
1141 * @param cont continuation
1142 * @param cont_cls closure for cont
1145 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1146 struct GNUNET_FS_Uri *ksk_uri,
1147 struct GNUNET_CONTAINER_MetaData *meta,
1148 struct GNUNET_FS_Uri *uri,
1149 struct GNUNET_TIME_Absolute expirationTime,
1150 unsigned int anonymity,
1151 unsigned int priority,
1152 enum GNUNET_FS_PublishOptions options,
1153 GNUNET_FS_PublishContinuation cont,
1156 struct PublishKskContext *pkc;
1161 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1163 pkc->expirationTime = expirationTime;
1164 pkc->anonymity = anonymity;
1165 pkc->priority = priority;
1167 pkc->cont_cls = cont_cls;
1168 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1170 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1172 if (pkc->dsh == NULL)
1174 cont (cont_cls, NULL, _("Could not connect to datastore."));
1179 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1180 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1181 GNUNET_assert (pkc->mdsize >= 0);
1182 uris = GNUNET_FS_uri_to_string (uri);
1183 pkc->slen = strlen (uris) + 1;
1184 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1185 if (size > MAX_KBLOCK_SIZE)
1187 size = MAX_KBLOCK_SIZE;
1188 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1190 pkc->kb = GNUNET_malloc (size);
1191 kbe = (char *) &pkc->kb[1];
1192 memcpy (kbe, uris, pkc->slen);
1194 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1197 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1198 if (pkc->mdsize == -1)
1202 GNUNET_free (pkc->kb);
1203 if (pkc->dsh != NULL)
1204 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1205 cont (cont_cls, NULL, _("Internal error."));
1209 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1211 pkc->cpy = GNUNET_malloc (size);
1212 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1213 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1216 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1217 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1218 GNUNET_SCHEDULER_add_continuation (h->sched,
1222 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1227 * Context for the SKS publication.
1229 struct PublishSksContext
1233 * Global FS context.
1235 struct GNUNET_FS_Uri *uri;
1238 * Handle to the datastore.
1240 struct GNUNET_DATASTORE_Handle *dsh;
1243 * Function to call once we're done.
1245 GNUNET_FS_PublishContinuation cont;
1256 * Function called by the datastore API with
1257 * the result from the PUT (SBlock) request.
1259 * @param cls closure of type "struct PublishSksContext*"
1260 * @param success GNUNET_OK on success
1261 * @param msg error message (or NULL)
1264 sb_put_cont (void *cls,
1268 struct PublishSksContext *psc = cls;
1270 if (NULL != psc->dsh)
1271 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1272 if (GNUNET_OK != success)
1273 psc->cont (psc->cont_cls,
1277 psc->cont (psc->cont_cls,
1280 GNUNET_FS_uri_destroy (psc->uri);
1286 * Publish an SBlock on GNUnet.
1288 * @param h handle to the file sharing subsystem
1289 * @param namespace namespace to publish in
1290 * @param identifier identifier to use
1291 * @param update update identifier to use
1292 * @param meta metadata to use
1293 * @param uri URI to refer to in the SBlock
1294 * @param expirationTime when the SBlock expires
1295 * @param anonymity anonymity level for the SBlock
1296 * @param priority priority for the SBlock
1297 * @param options publication options
1298 * @param cont continuation
1299 * @param cont_cls closure for cont
1302 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1303 struct GNUNET_FS_Namespace *namespace,
1304 const char *identifier,
1306 struct GNUNET_CONTAINER_MetaData *meta,
1307 struct GNUNET_FS_Uri *uri,
1308 struct GNUNET_TIME_Absolute expirationTime,
1309 unsigned int anonymity,
1310 unsigned int priority,
1311 enum GNUNET_FS_PublishOptions options,
1312 GNUNET_FS_PublishContinuation cont,
1315 struct PublishSksContext *psc;
1316 struct GNUNET_CRYPTO_AesSessionKey sk;
1317 struct GNUNET_CRYPTO_AesInitializationVector iv;
1318 struct GNUNET_FS_Uri *sks_uri;
1326 struct SBlock *sb_enc;
1328 GNUNET_HashCode key; /* hash of thisId = key */
1329 GNUNET_HashCode id; /* hash of hc = identifier */
1331 uris = GNUNET_FS_uri_to_string (uri);
1332 slen = strlen (uris) + 1;
1333 idlen = strlen (identifier);
1336 nidlen = strlen (update) + 1;
1337 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1338 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1340 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1341 if (size > MAX_SBLOCK_SIZE)
1343 size = MAX_SBLOCK_SIZE;
1344 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1346 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1347 dest = (char *) &sb[1];
1348 memcpy (dest, update, nidlen);
1350 memcpy (dest, uris, slen);
1352 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1355 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1363 _("Internal error."));
1366 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1367 sb_enc = GNUNET_malloc (sizeof (struct SBlock) + size);
1368 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1369 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1370 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1371 sks_uri->type = sks;
1372 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1373 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1374 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1375 &sks_uri->data.sks.namespace);
1376 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1377 GNUNET_CRYPTO_hash_xor (&id,
1378 &sks_uri->data.sks.namespace,
1379 &sb_enc->identifier);
1380 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1381 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1382 size - sizeof (struct SBlock),
1387 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1388 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1389 + sizeof(struct SBlock)
1390 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1391 GNUNET_assert (GNUNET_OK ==
1392 GNUNET_CRYPTO_rsa_sign (namespace->key,
1394 &sb_enc->signature));
1395 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1398 psc->cont_cls = cont_cls;
1399 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1401 GNUNET_free (sb_enc);
1407 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1408 if (NULL == psc->dsh)
1410 GNUNET_free (sb_enc);
1413 _("Failed to connect to datastore."));
1417 GNUNET_DATASTORE_put (psc->dsh,
1422 GNUNET_DATASTORE_BLOCKTYPE_SBLOCK,
1426 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1429 GNUNET_free (sb_enc);
1433 /* end of fs_publish.c */