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
29 * - code-sharing with unindex (write unindex code)
30 * - indexing cleanup: unindex on failure (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"
44 #define DEBUG_PUBLISH GNUNET_YES
47 * Maximum allowed size for a KBlock.
49 #define MAX_KBLOCK_SIZE 60000
52 * Maximum allowed size for an SBlock.
54 #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
100 * @param offset where in the file are we so far
103 make_publish_status (struct GNUNET_FS_ProgressInfo *pi,
104 struct GNUNET_FS_PublishContext *sc,
105 const struct GNUNET_FS_FileInformation *p,
108 pi->value.publish.sc = sc;
109 pi->value.publish.fi = p;
110 pi->value.publish.cctx
112 pi->value.publish.pctx
113 = (NULL == p->dir) ? NULL : p->dir->client_info;
114 pi->value.publish.filename
115 = (p->is_directory) ? p->data.dir.dirname : p->data.file.filename;
116 pi->value.publish.size
117 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
118 pi->value.publish.eta
119 = GNUNET_TIME_calculate_eta (p->start_time,
121 pi->value.publish.size);
122 pi->value.publish.completed = offset;
123 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
124 pi->value.publish.anonymity = p->anonymity;
129 * Cleanup the publish context, we're done
132 * @param pc struct to clean up after
135 publish_cleanup (struct GNUNET_FS_PublishContext *sc)
137 GNUNET_FS_file_information_destroy (sc->fi, NULL, NULL);
138 GNUNET_FS_namespace_delete (sc->namespace, GNUNET_NO);
139 GNUNET_free_non_null (sc->nid);
140 GNUNET_free_non_null (sc->nuid);
141 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
147 * Function called by the datastore API with
148 * the result from the PUT request.
150 * @param cls our closure
151 * @param success GNUNET_OK on success
152 * @param msg error message (or NULL)
155 ds_put_cont (void *cls,
159 struct PutContCtx *pcc = cls;
160 struct GNUNET_FS_ProgressInfo pi;
162 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
164 /* we were aborted in the meantime,
166 publish_cleanup (pcc->sc);
169 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
170 pcc->sc->in_network_wait = GNUNET_NO;
171 if (GNUNET_OK != success)
173 GNUNET_asprintf (&pcc->p->emsg,
174 _("Upload failed: %s"),
176 GNUNET_FS_file_information_sync (pcc->p);
177 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
178 make_publish_status (&pi, pcc->sc, pcc->p, 0);
179 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
180 pi.value.publish.specifics.error.message = pcc->p->emsg;
182 = pcc->sc->h->upcb (pcc->sc->h->upcb_cls,
186 GNUNET_FS_file_information_sync (pcc->p);
187 if (NULL != pcc->cont)
189 = GNUNET_SCHEDULER_add_delayed (pcc->sc->h->sched,
191 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
192 GNUNET_SCHEDULER_NO_TASK,
193 GNUNET_TIME_UNIT_ZERO,
201 * Generate the callback that signals clients
202 * that a file (or directory) has been completely
205 * @param p the completed upload
206 * @param sc context of the publication
209 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
210 struct GNUNET_FS_PublishContext *sc)
212 struct GNUNET_FS_ProgressInfo pi;
214 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
215 make_publish_status (&pi, sc, p,
216 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
217 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
218 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
220 = sc->h->upcb (sc->h->upcb_cls,
226 * Generate the callback that signals clients
227 * that a file (or directory) has encountered
228 * a problem during publication.
230 * @param p the upload that had trouble
231 * @param sc context of the publication
232 * @param emsg error message
235 signal_publish_error (struct GNUNET_FS_FileInformation *p,
236 struct GNUNET_FS_PublishContext *sc,
239 struct GNUNET_FS_ProgressInfo pi;
241 p->emsg = GNUNET_strdup (emsg);
242 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
243 make_publish_status (&pi, sc, p, 0);
244 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
245 pi.value.publish.specifics.error.message =emsg;
247 = sc->h->upcb (sc->h->upcb_cls,
253 * We've finished publishing the SBlock as part of a larger upload.
254 * Check the result and complete the larger upload.
256 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
257 * @param uri URI of the published SBlock
258 * @param emsg NULL on success, otherwise error message
261 publish_sblocks_cont (void *cls,
262 const struct GNUNET_FS_Uri *uri,
265 struct GNUNET_FS_PublishContext *sc = cls;
268 signal_publish_error (sc->fi,
273 // FIXME: release the datastore reserve here!
274 signal_publish_completion (sc->fi, sc);
279 * We are almost done publishing the structure,
280 * add SBlocks (if needed).
282 * @param sc overall upload data
285 publish_sblock (struct GNUNET_FS_PublishContext *sc)
287 if (NULL != sc->namespace)
288 GNUNET_FS_publish_sks (sc->h,
294 sc->fi->expirationTime,
298 &publish_sblocks_cont,
301 publish_sblocks_cont (sc, NULL, NULL);
306 * We've finished publishing a KBlock
307 * as part of a larger upload. Check
308 * the result and continue the larger
311 * @param cls the "struct GNUNET_FS_PublishContext*"
312 * of the larger upload
313 * @param uri URI of the published blocks
314 * @param emsg NULL on success, otherwise error message
317 publish_kblocks_cont (void *cls,
318 const struct GNUNET_FS_Uri *uri,
321 struct GNUNET_FS_PublishContext *sc = cls;
322 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
326 signal_publish_error (p, sc, emsg);
328 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
330 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
331 GNUNET_SCHEDULER_NO_TASK,
332 GNUNET_TIME_UNIT_ZERO,
337 GNUNET_FS_file_information_sync (p);
339 signal_publish_completion (p, sc);
340 /* move on to next file */
342 sc->fi_pos = p->next;
346 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
348 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
349 GNUNET_SCHEDULER_NO_TASK,
350 GNUNET_TIME_UNIT_ZERO,
357 * Function called by the tree encoder to obtain
358 * a block of plaintext data (for the lowest level
361 * @param cls our publishing context
362 * @param offset identifies which block to get
363 * @param max (maximum) number of bytes to get; returning
364 * fewer will also cause errors
365 * @param buf where to copy the plaintext buffer
366 * @param emsg location to store an error message (on error)
367 * @return number of bytes copied to buf, 0 on error
370 block_reader (void *cls,
376 struct GNUNET_FS_PublishContext *sc = cls;
377 struct GNUNET_FS_FileInformation *p;
384 pt_size = GNUNET_MIN(max,
385 p->data.dir.dir_size - offset);
386 dd = p->data.dir.dir_data;
393 pt_size = GNUNET_MIN(max,
394 p->data.file.file_size - offset);
396 p->data.file.reader (p->data.file.reader_cls,
408 * The tree encoder has finished processing a
409 * file. Call it's finish method and deal with
412 * @param cls our publishing context
413 * @param tc scheduler's task context (not used)
416 encode_cont (void *cls,
417 const struct GNUNET_SCHEDULER_TaskContext *tc)
419 struct GNUNET_FS_PublishContext *sc = cls;
420 struct GNUNET_FS_FileInformation *p;
421 struct GNUNET_FS_ProgressInfo pi;
425 GNUNET_FS_tree_encoder_finish (p->te,
431 GNUNET_asprintf (&p->emsg,
432 _("Upload failed: %s"),
435 GNUNET_FS_file_information_sync (p);
436 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
437 make_publish_status (&pi, sc, p, 0);
438 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
439 pi.value.publish.specifics.error.message = p->emsg;
441 = sc->h->upcb (sc->h->upcb_cls,
444 /* continue with main */
446 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
448 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
449 GNUNET_SCHEDULER_NO_TASK,
450 GNUNET_TIME_UNIT_ZERO,
457 * Function called asking for the current (encoded)
458 * block to be processed. After processing the
459 * client should either call "GNUNET_FS_tree_encode_next"
460 * or (on error) "GNUNET_FS_tree_encode_finish".
463 * @param query the query for the block (key for lookup in the datastore)
464 * @param type type of the block (IBLOCK or DBLOCK)
465 * @param block the (encrypted) block
466 * @param block_size size of block (in bytes)
469 block_proc (void *cls,
470 const GNUNET_HashCode *query,
476 struct GNUNET_FS_PublishContext *sc = cls;
477 struct GNUNET_FS_FileInformation *p;
478 struct PutContCtx * dpc_cls;
479 struct OnDemandBlock odb;
485 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
487 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
488 GNUNET_SCHEDULER_NO_TASK,
489 GNUNET_TIME_UNIT_ZERO,
495 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
496 sc->in_network_wait = GNUNET_YES;
497 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
498 dpc_cls->cont = &do_upload;
499 dpc_cls->cont_cls = sc;
501 if ( (p->is_directory) &&
502 (p->data.file.do_index) &&
503 (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK) )
506 odb.file_id = p->data.file.file_id;
507 GNUNET_DATASTORE_put (sc->dsh,
510 sizeof(struct OnDemandBlock),
512 GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND,
516 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
521 GNUNET_DATASTORE_put (sc->dsh,
530 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
537 * Function called with information about our
538 * progress in computing the tree encoding.
541 * @param offset where are we in the file
542 * @param pt_block plaintext of the currently processed block
543 * @param pt_size size of pt_block
544 * @param depth depth of the block in the tree
547 progress_proc (void *cls,
549 const void *pt_block,
553 struct GNUNET_FS_PublishContext *sc = cls;
554 struct GNUNET_FS_FileInformation *p;
555 struct GNUNET_FS_ProgressInfo pi;
558 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
559 make_publish_status (&pi, sc, p, offset);
560 pi.value.publish.specifics.progress.data = pt_block;
561 pi.value.publish.specifics.progress.offset = offset;
562 pi.value.publish.specifics.progress.data_len = pt_size;
563 pi.value.publish.specifics.progress.depth = depth;
565 = sc->h->upcb (sc->h->upcb_cls,
571 * We are uploading a file or directory; load (if necessary) the next
572 * block into memory, encrypt it and send it to the FS service. Then
573 * continue with the main task.
575 * @param sc overall upload data
578 publish_content (struct GNUNET_FS_PublishContext *sc)
580 struct GNUNET_FS_FileInformation *p;
582 struct GNUNET_FS_DirectoryBuilder *db;
583 struct GNUNET_FS_FileInformation *dirpos;
592 db = GNUNET_FS_directory_builder_create (p->meta);
593 dirpos = p->data.dir.entries;
594 while (NULL != dirpos)
596 if (dirpos->is_directory)
598 raw_data = dirpos->data.dir.dir_data;
599 dirpos->data.dir.dir_data = NULL;
604 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
605 (dirpos->data.file.file_size > 0) )
607 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
609 if (dirpos->data.file.file_size !=
610 dirpos->data.file.reader (dirpos->data.file.reader_cls,
612 dirpos->data.file.file_size,
616 GNUNET_free_non_null (emsg);
617 GNUNET_free (raw_data);
622 GNUNET_FS_directory_builder_add (db,
626 GNUNET_free_non_null (raw_data);
627 dirpos = dirpos->next;
629 GNUNET_FS_directory_builder_finish (db,
630 &p->data.dir.dir_size,
631 &p->data.dir.dir_data);
633 size = (p->is_directory)
634 ? p->data.dir.dir_size
635 : p->data.file.file_size;
636 p->te = GNUNET_FS_tree_encoder_create (sc->h,
645 GNUNET_FS_tree_encoder_next (p->te);
650 * Process the response (or lack thereof) from
651 * the "fs" service to our 'start index' request.
653 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
654 * @param msg the response we got
657 process_index_start_response (void *cls,
658 const struct GNUNET_MessageHeader *msg)
660 struct GNUNET_FS_PublishContext *sc = cls;
661 struct GNUNET_FS_FileInformation *p;
665 GNUNET_CLIENT_disconnect (sc->client);
670 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
671 _("Can not index file `%s': %s. Will try to insert instead.\n"),
672 p->data.file.filename,
673 _("timeout on index-start request to `fs' service"));
674 p->data.file.do_index = GNUNET_NO;
675 publish_content (sc);
678 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
680 msize = ntohs (msg->size);
681 emsg = (const char *) &msg[1];
682 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
683 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
684 emsg = gettext_noop ("unknown error");
685 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
686 _("Can not index file `%s': %s. Will try to insert instead.\n"),
687 p->data.file.filename,
689 p->data.file.do_index = GNUNET_NO;
690 publish_content (sc);
693 /* success! continue with indexing */
694 publish_content (sc);
699 * Function called once the hash computation over an
700 * indexed file has completed.
702 * @param cls closure, our publishing context
703 * @param res resulting hash, NULL on error
706 hash_for_index_cb (void *cls,
707 const GNUNET_HashCode *
710 struct GNUNET_FS_PublishContext *sc = cls;
711 struct GNUNET_FS_FileInformation *p;
712 struct IndexStartMessage *ism;
714 struct GNUNET_CLIENT_Connection *client;
721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
722 _("Can not index file `%s': %s. Will try to insert instead.\n"),
723 p->data.file.filename,
724 _("failed to compute hash"));
725 p->data.file.do_index = GNUNET_NO;
726 publish_content (sc);
729 slen = strlen (p->data.file.filename) + 1;
730 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
733 _("Can not index file `%s': %s. Will try to insert instead.\n"),
734 p->data.file.filename,
735 _("filename too long"));
736 p->data.file.do_index = GNUNET_NO;
737 publish_content (sc);
740 client = GNUNET_CLIENT_connect (sc->h->sched,
745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
746 _("Can not index file `%s': %s. Will try to insert instead.\n"),
747 p->data.file.filename,
748 _("could not connect to `fs' service"));
749 p->data.file.do_index = GNUNET_NO;
750 publish_content (sc);
753 p->data.file.file_id = *res;
754 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
756 ism->header.size = htons(sizeof(struct IndexStartMessage) +
758 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
760 GNUNET_DISK_file_get_identifiers (p->data.file.filename,
764 ism->device = htonl (dev);
765 ism->inode = GNUNET_htonll(ino);
768 p->data.file.filename,
771 GNUNET_CLIENT_transmit_and_get_response (client,
773 GNUNET_TIME_UNIT_FOREVER_REL,
774 &process_index_start_response,
781 * Main function that performs the upload.
782 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
783 * @param tc task context
786 do_upload (void *cls,
787 const struct GNUNET_SCHEDULER_TaskContext *tc)
789 struct GNUNET_FS_PublishContext *sc = cls;
790 struct GNUNET_FS_ProgressInfo pi;
791 struct GNUNET_FS_FileInformation *p;
794 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
798 /* upload of entire hierarchy complete,
799 publish namespace entries */
803 /* find starting position */
804 while ( (p->is_directory) &&
805 (NULL != p->data.dir.entries) &&
807 (NULL == p->data.dir.entries->chk_uri) )
809 p = p->data.dir.entries;
815 /* error with current file, abort all
816 related files as well! */
817 while (NULL != p->dir)
819 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
822 GNUNET_asprintf (&p->emsg,
823 _("Recursive upload failed at `%s'"),
826 GNUNET_FS_file_information_sync (p);
827 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
828 make_publish_status (&pi, sc, p, 0);
829 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
830 pi.value.publish.specifics.error.message = p->emsg;
832 = sc->h->upcb (sc->h->upcb_cls,
837 /* handle completion */
838 if (NULL != p->chk_uri)
840 /* upload of "p" complete, publish KBlocks! */
841 GNUNET_FS_publish_ksk (sc->h,
849 &publish_kblocks_cont,
853 if ( (!p->is_directory) &&
854 (p->data.file.do_index) )
856 if (NULL == p->data.file.filename)
858 p->data.file.do_index = GNUNET_NO;
859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
860 _("Can not index file `%s': %s. Will try to insert instead.\n"),
862 _("needs to be an actual file"));
863 publish_content (sc);
866 GNUNET_CRYPTO_hash_file (sc->h->sched,
867 GNUNET_SCHEDULER_PRIORITY_IDLE,
869 p->data.file.filename,
875 publish_content (sc);
880 * Signal the FS's progress function that we are starting
883 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
884 * @param fi the entry in the publish-structure
885 * @param length length of the file or directory
886 * @param meta metadata for the file or directory (can be modified)
887 * @param uri pointer to the keywords that will be used for this entry (can be modified)
888 * @param anonymity pointer to selected anonymity level (can be modified)
889 * @param priority pointer to selected priority (can be modified)
890 * @param expirationTime pointer to selected expiration time (can be modified)
891 * @param client_info pointer to client context set upon creation (can be modified)
892 * @return GNUNET_OK to continue (always)
895 fip_signal_start(void *cls,
896 struct GNUNET_FS_FileInformation *fi,
898 struct GNUNET_CONTAINER_MetaData *meta,
899 struct GNUNET_FS_Uri **uri,
902 struct GNUNET_TIME_Absolute *expirationTime,
905 struct GNUNET_FS_PublishContext *sc = cls;
906 struct GNUNET_FS_ProgressInfo pi;
908 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
909 make_publish_status (&pi, sc, fi, 0);
910 *client_info = sc->h->upcb (sc->h->upcb_cls,
917 * Publish a file or directory.
919 * @param h handle to the file sharing subsystem
920 * @param ctx initial value to use for the '*ctx'
921 * in the callback (for the GNUNET_FS_STATUS_PUBLISH_START event).
922 * @param fi information about the file or directory structure to publish
923 * @param namespace namespace to publish the file in, NULL for no namespace
924 * @param nid identifier to use for the publishd content in the namespace
925 * (can be NULL, must be NULL if namespace is NULL)
926 * @param nuid update-identifier that will be used for future updates
927 * (can be NULL, must be NULL if namespace or nid is NULL)
928 * @param options options for the publication
929 * @return context that can be used to control the publish operation
931 struct GNUNET_FS_PublishContext *
932 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
934 struct GNUNET_FS_FileInformation *fi,
935 struct GNUNET_FS_Namespace *namespace,
938 enum GNUNET_FS_PublishOptions options)
940 struct GNUNET_FS_PublishContext *ret;
941 struct GNUNET_DATASTORE_Handle *dsh;
943 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
945 dsh = GNUNET_DATASTORE_connect (h->cfg,
954 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
957 ret->client_ctx = ctx;
959 ret->namespace = namespace;
960 if (namespace != NULL)
963 GNUNET_assert (NULL != nid);
964 ret->nid = GNUNET_strdup (nid);
966 ret->nuid = GNUNET_strdup (nuid);
968 // FIXME: make upload persistent!
971 GNUNET_FS_file_information_inspect (ret->fi,
974 ret->fi_pos = ret->fi;
976 // FIXME: calculate space needed for "fi"
977 // and reserve as first task (then trigger
978 // "do_upload" from that continuation)!
980 = GNUNET_SCHEDULER_add_delayed (h->sched,
982 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
983 GNUNET_SCHEDULER_NO_TASK,
984 GNUNET_TIME_UNIT_ZERO,
992 * Signal the FS's progress function that we are stopping
995 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
996 * @param fi the entry in the publish-structure
997 * @param length length of the file or directory
998 * @param meta metadata for the file or directory (can be modified)
999 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1000 * @param anonymity pointer to selected anonymity level (can be modified)
1001 * @param priority pointer to selected priority (can be modified)
1002 * @param expirationTime pointer to selected expiration time (can be modified)
1003 * @param client_info pointer to client context set upon creation (can be modified)
1004 * @return GNUNET_OK to continue (always)
1007 fip_signal_stop(void *cls,
1008 struct GNUNET_FS_FileInformation *fi,
1010 struct GNUNET_CONTAINER_MetaData *meta,
1011 struct GNUNET_FS_Uri **uri,
1012 uint32_t *anonymity,
1014 struct GNUNET_TIME_Absolute *expirationTime,
1017 struct GNUNET_FS_PublishContext*sc = cls;
1018 struct GNUNET_FS_ProgressInfo pi;
1021 off = (fi->chk_uri == NULL) ? 0 : length;
1022 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1023 make_publish_status (&pi, sc, fi, off);
1024 GNUNET_break (NULL ==
1025 sc->h->upcb (sc->h->upcb_cls,
1027 *client_info = NULL;
1033 * Stop an upload. Will abort incomplete uploads (but
1034 * not remove blocks that have already been publishd) or
1035 * simply clean up the state for completed uploads.
1037 * @param sc context for the upload to stop
1040 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *sc)
1042 if (GNUNET_SCHEDULER_NO_TASK != sc->upload_task)
1043 GNUNET_SCHEDULER_cancel (sc->h->sched, sc->upload_task);
1044 // FIXME: remove from persistence DB (?) --- think more about
1045 // shutdown / persistent-resume APIs!!!
1046 GNUNET_FS_file_information_inspect (sc->fi,
1049 if (GNUNET_YES == sc->in_network_wait)
1051 sc->in_network_wait = GNUNET_SYSERR;
1054 publish_cleanup (sc);
1059 * Context for the KSK publication.
1061 struct PublishKskContext
1067 struct GNUNET_FS_Uri *ksk_uri;
1070 * Global FS context.
1072 struct GNUNET_FS_Handle *h;
1075 * The master block that we are sending
1076 * (in plaintext), has "mdsize+slen" more
1077 * bytes than the struct would suggest.
1082 * Buffer of the same size as "kb" for
1083 * the encrypted version.
1088 * Handle to the datastore, NULL if we are just
1091 struct GNUNET_DATASTORE_Handle *dsh;
1094 * Function to call once we're done.
1096 GNUNET_FS_PublishContinuation cont;
1104 * When should the KBlocks expire?
1106 struct GNUNET_TIME_Absolute expirationTime;
1109 * Size of the serialized metadata.
1114 * Size of the (CHK) URI as a string.
1119 * Keyword that we are currently processing.
1124 * Anonymity level for the KBlocks.
1129 * Priority for the KBlocks.
1136 * Continuation of "GNUNET_FS_publish_ksk" that performs
1137 * the actual publishing operation (iterating over all
1140 * @param cls closure of type "struct PublishKskContext*"
1144 publish_ksk_cont (void *cls,
1145 const struct GNUNET_SCHEDULER_TaskContext *tc);
1149 * Function called by the datastore API with
1150 * the result from the PUT request.
1152 * @param cls closure of type "struct PublishKskContext*"
1153 * @param success GNUNET_OK on success
1154 * @param msg error message (or NULL)
1157 kb_put_cont (void *cls,
1161 struct PublishKskContext *pkc = cls;
1163 if (GNUNET_OK != success)
1165 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1166 GNUNET_free (pkc->cpy);
1167 GNUNET_free (pkc->kb);
1168 pkc->cont (pkc->cont_cls,
1171 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1175 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1179 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1184 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1185 * publishing operation (iterating over all of the keywords).
1187 * @param cls closure of type "struct PublishKskContext*"
1191 publish_ksk_cont (void *cls,
1192 const struct GNUNET_SCHEDULER_TaskContext *tc)
1194 struct PublishKskContext *pkc = cls;
1195 const char *keyword;
1196 GNUNET_HashCode key;
1197 GNUNET_HashCode query;
1198 struct GNUNET_CRYPTO_AesSessionKey skey;
1199 struct GNUNET_CRYPTO_AesInitializationVector iv;
1200 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1203 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1204 (NULL == pkc->dsh) )
1206 if (NULL != pkc->dsh)
1207 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1208 GNUNET_free (pkc->cpy);
1209 GNUNET_free (pkc->kb);
1210 pkc->cont (pkc->cont_cls,
1213 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1217 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1218 /* first character of keyword indicates if it is
1219 mandatory or not -- ignore for hashing */
1220 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1221 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1222 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1223 pkc->slen + pkc->mdsize,
1227 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1228 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1229 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1230 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1232 GNUNET_assert (GNUNET_OK ==
1233 GNUNET_CRYPTO_rsa_sign (pk,
1235 &pkc->cpy->signature));
1236 GNUNET_CRYPTO_rsa_key_free (pk);
1237 GNUNET_DATASTORE_put (pkc->dsh,
1241 sizeof (struct KBlock) +
1244 GNUNET_DATASTORE_BLOCKTYPE_KBLOCK,
1247 pkc->expirationTime,
1248 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1255 * Publish a CHK under various keywords on GNUnet.
1257 * @param h handle to the file sharing subsystem
1258 * @param ksk_uri keywords to use
1259 * @param meta metadata to use
1260 * @param uri URI to refer to in the KBlock
1261 * @param expirationTime when the KBlock expires
1262 * @param anonymity anonymity level for the KBlock
1263 * @param priority priority for the KBlock
1264 * @param options publication options
1265 * @param cont continuation
1266 * @param cont_cls closure for cont
1269 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1270 struct GNUNET_FS_Uri *ksk_uri,
1271 struct GNUNET_CONTAINER_MetaData *meta,
1272 struct GNUNET_FS_Uri *uri,
1273 struct GNUNET_TIME_Absolute expirationTime,
1276 enum GNUNET_FS_PublishOptions options,
1277 GNUNET_FS_PublishContinuation cont,
1280 struct PublishKskContext *pkc;
1285 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1287 pkc->expirationTime = expirationTime;
1288 pkc->anonymity = anonymity;
1289 pkc->priority = priority;
1291 pkc->cont_cls = cont_cls;
1292 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1294 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1296 if (pkc->dsh == NULL)
1298 cont (cont_cls, NULL, _("Could not connect to datastore."));
1303 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1304 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1305 GNUNET_assert (pkc->mdsize >= 0);
1306 uris = GNUNET_FS_uri_to_string (uri);
1307 pkc->slen = strlen (uris) + 1;
1308 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1309 if (size > MAX_KBLOCK_SIZE)
1311 size = MAX_KBLOCK_SIZE;
1312 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1314 pkc->kb = GNUNET_malloc (size);
1315 kbe = (char *) &pkc->kb[1];
1316 memcpy (kbe, uris, pkc->slen);
1318 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1321 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1322 if (pkc->mdsize == -1)
1326 GNUNET_free (pkc->kb);
1327 if (pkc->dsh != NULL)
1328 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1329 cont (cont_cls, NULL, _("Internal error."));
1333 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1335 pkc->cpy = GNUNET_malloc (size);
1336 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1337 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1340 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1341 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1342 GNUNET_SCHEDULER_add_continuation (h->sched,
1346 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1351 * Context for the SKS publication.
1353 struct PublishSksContext
1357 * Global FS context.
1359 struct GNUNET_FS_Uri *uri;
1362 * Handle to the datastore.
1364 struct GNUNET_DATASTORE_Handle *dsh;
1367 * Function to call once we're done.
1369 GNUNET_FS_PublishContinuation cont;
1380 * Function called by the datastore API with
1381 * the result from the PUT (SBlock) request.
1383 * @param cls closure of type "struct PublishSksContext*"
1384 * @param success GNUNET_OK on success
1385 * @param msg error message (or NULL)
1388 sb_put_cont (void *cls,
1392 struct PublishSksContext *psc = cls;
1394 if (NULL != psc->dsh)
1395 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1396 if (GNUNET_OK != success)
1397 psc->cont (psc->cont_cls,
1401 psc->cont (psc->cont_cls,
1404 GNUNET_FS_uri_destroy (psc->uri);
1410 * Publish an SBlock on GNUnet.
1412 * @param h handle to the file sharing subsystem
1413 * @param namespace namespace to publish in
1414 * @param identifier identifier to use
1415 * @param update update identifier to use
1416 * @param meta metadata to use
1417 * @param uri URI to refer to in the SBlock
1418 * @param expirationTime when the SBlock expires
1419 * @param anonymity anonymity level for the SBlock
1420 * @param priority priority for the SBlock
1421 * @param options publication options
1422 * @param cont continuation
1423 * @param cont_cls closure for cont
1426 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1427 struct GNUNET_FS_Namespace *namespace,
1428 const char *identifier,
1430 struct GNUNET_CONTAINER_MetaData *meta,
1431 struct GNUNET_FS_Uri *uri,
1432 struct GNUNET_TIME_Absolute expirationTime,
1435 enum GNUNET_FS_PublishOptions options,
1436 GNUNET_FS_PublishContinuation cont,
1439 struct PublishSksContext *psc;
1440 struct GNUNET_CRYPTO_AesSessionKey sk;
1441 struct GNUNET_CRYPTO_AesInitializationVector iv;
1442 struct GNUNET_FS_Uri *sks_uri;
1450 struct SBlock *sb_enc;
1452 GNUNET_HashCode key; /* hash of thisId = key */
1453 GNUNET_HashCode id; /* hash of hc = identifier */
1455 uris = GNUNET_FS_uri_to_string (uri);
1456 slen = strlen (uris) + 1;
1457 idlen = strlen (identifier);
1460 nidlen = strlen (update) + 1;
1461 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1462 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1464 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1465 if (size > MAX_SBLOCK_SIZE)
1467 size = MAX_SBLOCK_SIZE;
1468 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1470 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1471 dest = (char *) &sb[1];
1472 memcpy (dest, update, nidlen);
1474 memcpy (dest, uris, slen);
1476 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1479 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1487 _("Internal error."));
1490 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1491 sb_enc = GNUNET_malloc (sizeof (struct SBlock) + size);
1492 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1493 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1494 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1495 sks_uri->type = sks;
1496 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1497 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1498 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1499 &sks_uri->data.sks.namespace);
1500 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1501 GNUNET_CRYPTO_hash_xor (&id,
1502 &sks_uri->data.sks.namespace,
1503 &sb_enc->identifier);
1504 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1505 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1506 size - sizeof (struct SBlock),
1511 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1512 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1513 + sizeof(struct SBlock)
1514 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1515 GNUNET_assert (GNUNET_OK ==
1516 GNUNET_CRYPTO_rsa_sign (namespace->key,
1518 &sb_enc->signature));
1519 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1522 psc->cont_cls = cont_cls;
1523 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1525 GNUNET_free (sb_enc);
1531 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1532 if (NULL == psc->dsh)
1534 GNUNET_free (sb_enc);
1537 _("Failed to connect to datastore."));
1541 GNUNET_DATASTORE_put (psc->dsh,
1546 GNUNET_DATASTORE_BLOCKTYPE_SBLOCK,
1550 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1553 GNUNET_free (sb_enc);
1557 /* end of fs_publish.c */