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 * - 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)
36 #include "gnunet_constants.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet_fs_service.h"
43 #define DEBUG_PUBLISH GNUNET_NO
46 * Main function that performs the upload.
47 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
48 * @param tc task context
52 const struct GNUNET_SCHEDULER_TaskContext *tc);
56 * Context for "ds_put_cont".
61 * Current publishing context.
63 struct GNUNET_FS_PublishContext *sc;
66 * Specific file with the block.
68 struct GNUNET_FS_FileInformation *p;
71 * Function to run next, if any (can be NULL).
73 GNUNET_SCHEDULER_Task cont;
83 * Fill in all of the generic fields for
86 * @param pi structure to fill in
87 * @param sc overall publishing context
88 * @param p file information for the file being published
89 * @param offset where in the file are we so far
92 make_publish_status (struct GNUNET_FS_ProgressInfo *pi,
93 struct GNUNET_FS_PublishContext *sc,
94 const struct GNUNET_FS_FileInformation *p,
97 pi->value.publish.sc = sc;
98 pi->value.publish.fi = p;
99 pi->value.publish.cctx
101 pi->value.publish.pctx
102 = (NULL == p->dir) ? NULL : p->dir->client_info;
103 pi->value.publish.filename
104 = (p->is_directory) ? p->data.dir.dirname : p->data.file.filename;
105 pi->value.publish.size
106 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
107 pi->value.publish.eta
108 = GNUNET_TIME_calculate_eta (p->start_time,
110 pi->value.publish.size);
111 pi->value.publish.completed = offset;
112 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
113 pi->value.publish.anonymity = p->anonymity;
118 * Cleanup the publish context, we're done
121 * @param sc struct to clean up after
124 publish_cleanup (struct GNUNET_FS_PublishContext *sc)
126 GNUNET_FS_file_information_destroy (sc->fi, NULL, NULL);
127 if (sc->namespace != NULL)
128 GNUNET_FS_namespace_delete (sc->namespace, GNUNET_NO);
129 GNUNET_free_non_null (sc->nid);
130 GNUNET_free_non_null (sc->nuid);
131 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
137 * Function called by the datastore API with
138 * the result from the PUT request.
140 * @param cls our closure
141 * @param success GNUNET_OK on success
142 * @param msg error message (or NULL)
145 ds_put_cont (void *cls,
149 struct PutContCtx *pcc = cls;
150 struct GNUNET_FS_ProgressInfo pi;
152 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
154 /* we were aborted in the meantime,
156 publish_cleanup (pcc->sc);
159 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
160 pcc->sc->in_network_wait = GNUNET_NO;
161 if (GNUNET_OK != success)
163 GNUNET_asprintf (&pcc->p->emsg,
164 _("Upload failed: %s"),
166 GNUNET_FS_file_information_sync (pcc->p);
167 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
168 make_publish_status (&pi, pcc->sc, pcc->p, 0);
169 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
170 pi.value.publish.specifics.error.message = pcc->p->emsg;
172 = pcc->sc->h->upcb (pcc->sc->h->upcb_cls,
175 GNUNET_FS_file_information_sync (pcc->p);
176 if (NULL != pcc->cont)
178 = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
179 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
187 * Generate the callback that signals clients
188 * that a file (or directory) has been completely
191 * @param p the completed upload
192 * @param sc context of the publication
195 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
196 struct GNUNET_FS_PublishContext *sc)
198 struct GNUNET_FS_ProgressInfo pi;
200 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
201 make_publish_status (&pi, sc, p,
202 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
203 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
204 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
206 = sc->h->upcb (sc->h->upcb_cls,
212 * Generate the callback that signals clients
213 * that a file (or directory) has encountered
214 * a problem during publication.
216 * @param p the upload that had trouble
217 * @param sc context of the publication
218 * @param emsg error message
221 signal_publish_error (struct GNUNET_FS_FileInformation *p,
222 struct GNUNET_FS_PublishContext *sc,
225 struct GNUNET_FS_ProgressInfo pi;
227 p->emsg = GNUNET_strdup (emsg);
228 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
229 make_publish_status (&pi, sc, p, 0);
230 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
231 pi.value.publish.specifics.error.message =emsg;
233 = sc->h->upcb (sc->h->upcb_cls,
239 * We've finished publishing the SBlock as part of a larger upload.
240 * Check the result and complete the larger upload.
242 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
243 * @param uri URI of the published SBlock
244 * @param emsg NULL on success, otherwise error message
247 publish_sblocks_cont (void *cls,
248 const struct GNUNET_FS_Uri *uri,
251 struct GNUNET_FS_PublishContext *sc = cls;
254 signal_publish_error (sc->fi,
259 // FIXME: release the datastore reserve here!
260 signal_publish_completion (sc->fi, sc);
261 sc->all_done = GNUNET_YES;
266 * We are almost done publishing the structure,
267 * add SBlocks (if needed).
269 * @param sc overall upload data
272 publish_sblock (struct GNUNET_FS_PublishContext *sc)
274 if (NULL != sc->namespace)
275 GNUNET_FS_publish_sks (sc->h,
281 sc->fi->expirationTime,
285 &publish_sblocks_cont,
288 publish_sblocks_cont (sc, NULL, NULL);
293 * We've finished publishing a KBlock as part of a larger upload.
294 * Check the result and continue the larger upload.
296 * @param cls the "struct GNUNET_FS_PublishContext*"
297 * of the larger upload
298 * @param uri URI of the published blocks
299 * @param emsg NULL on success, otherwise error message
302 publish_kblocks_cont (void *cls,
303 const struct GNUNET_FS_Uri *uri,
306 struct GNUNET_FS_PublishContext *sc = cls;
307 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
311 signal_publish_error (p, sc, emsg);
313 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
314 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
319 GNUNET_FS_file_information_sync (p);
321 signal_publish_completion (p, sc);
322 /* move on to next file */
324 sc->fi_pos = p->next;
328 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
329 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
336 * Function called by the tree encoder to obtain
337 * a block of plaintext data (for the lowest level
340 * @param cls our publishing context
341 * @param offset identifies which block to get
342 * @param max (maximum) number of bytes to get; returning
343 * fewer will also cause errors
344 * @param buf where to copy the plaintext buffer
345 * @param emsg location to store an error message (on error)
346 * @return number of bytes copied to buf, 0 on error
349 block_reader (void *cls,
355 struct GNUNET_FS_PublishContext *sc = cls;
356 struct GNUNET_FS_FileInformation *p;
363 pt_size = GNUNET_MIN(max,
364 p->data.dir.dir_size - offset);
365 dd = p->data.dir.dir_data;
372 pt_size = GNUNET_MIN(max,
373 p->data.file.file_size - offset);
375 return 0; /* calling reader with pt_size==0
376 might free buf, so don't! */
378 p->data.file.reader (p->data.file.reader_cls,
390 * The tree encoder has finished processing a
391 * file. Call it's finish method and deal with
394 * @param cls our publishing context
395 * @param tc scheduler's task context (not used)
398 encode_cont (void *cls,
399 const struct GNUNET_SCHEDULER_TaskContext *tc)
401 struct GNUNET_FS_PublishContext *sc = cls;
402 struct GNUNET_FS_FileInformation *p;
403 struct GNUNET_FS_ProgressInfo pi;
407 GNUNET_FS_tree_encoder_finish (p->te,
413 GNUNET_asprintf (&p->emsg,
414 _("Upload failed: %s"),
417 GNUNET_FS_file_information_sync (p);
418 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
419 make_publish_status (&pi, sc, p, 0);
420 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
421 pi.value.publish.specifics.error.message = p->emsg;
423 = sc->h->upcb (sc->h->upcb_cls,
426 /* continue with main */
428 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
429 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
436 * Function called asking for the current (encoded)
437 * block to be processed. After processing the
438 * client should either call "GNUNET_FS_tree_encode_next"
439 * or (on error) "GNUNET_FS_tree_encode_finish".
442 * @param query the query for the block (key for lookup in the datastore)
443 * @param offset offset of the block in the file
444 * @param type type of the block (IBLOCK or DBLOCK)
445 * @param block the (encrypted) block
446 * @param block_size size of block (in bytes)
449 block_proc (void *cls,
450 const GNUNET_HashCode *query,
456 struct GNUNET_FS_PublishContext *sc = cls;
457 struct GNUNET_FS_FileInformation *p;
458 struct PutContCtx * dpc_cls;
459 struct OnDemandBlock odb;
465 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
466 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
472 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
473 sc->in_network_wait = GNUNET_YES;
474 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
475 dpc_cls->cont = &do_upload;
476 dpc_cls->cont_cls = sc;
479 if ( (! p->is_directory) &&
480 (GNUNET_YES == p->data.file.do_index) &&
481 (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK) )
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Indexing block `%s' for offset %llu with index size %u\n",
487 (unsigned long long) offset,
488 sizeof (struct OnDemandBlock));
490 odb.offset = GNUNET_htonll (offset);
491 odb.file_id = p->data.file.file_id;
492 GNUNET_DATASTORE_put (sc->dsh,
495 sizeof(struct OnDemandBlock),
497 GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND,
501 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Publishing block `%s' for offset %llu with size %u\n",
510 (unsigned long long) offset,
511 (unsigned int) block_size);
513 GNUNET_DATASTORE_put (sc->dsh,
522 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
529 * Function called with information about our
530 * progress in computing the tree encoding.
533 * @param offset where are we in the file
534 * @param pt_block plaintext of the currently processed block
535 * @param pt_size size of pt_block
536 * @param depth depth of the block in the tree
539 progress_proc (void *cls,
541 const void *pt_block,
545 struct GNUNET_FS_PublishContext *sc = cls;
546 struct GNUNET_FS_FileInformation *p;
547 struct GNUNET_FS_ProgressInfo pi;
550 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
551 make_publish_status (&pi, sc, p, offset);
552 pi.value.publish.specifics.progress.data = pt_block;
553 pi.value.publish.specifics.progress.offset = offset;
554 pi.value.publish.specifics.progress.data_len = pt_size;
555 pi.value.publish.specifics.progress.depth = depth;
557 = sc->h->upcb (sc->h->upcb_cls,
563 * We are uploading a file or directory; load (if necessary) the next
564 * block into memory, encrypt it and send it to the FS service. Then
565 * continue with the main task.
567 * @param sc overall upload data
570 publish_content (struct GNUNET_FS_PublishContext *sc)
572 struct GNUNET_FS_FileInformation *p;
574 struct GNUNET_FS_DirectoryBuilder *db;
575 struct GNUNET_FS_FileInformation *dirpos;
584 db = GNUNET_FS_directory_builder_create (p->meta);
585 dirpos = p->data.dir.entries;
586 while (NULL != dirpos)
588 if (dirpos->is_directory)
590 raw_data = dirpos->data.dir.dir_data;
591 dirpos->data.dir.dir_data = NULL;
596 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
597 (dirpos->data.file.file_size > 0) )
599 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
601 if (dirpos->data.file.file_size !=
602 dirpos->data.file.reader (dirpos->data.file.reader_cls,
604 dirpos->data.file.file_size,
608 GNUNET_free_non_null (emsg);
609 GNUNET_free (raw_data);
614 GNUNET_FS_directory_builder_add (db,
618 GNUNET_free_non_null (raw_data);
619 dirpos = dirpos->next;
621 GNUNET_FS_directory_builder_finish (db,
622 &p->data.dir.dir_size,
623 &p->data.dir.dir_data);
625 size = (p->is_directory)
626 ? p->data.dir.dir_size
627 : p->data.file.file_size;
628 p->te = GNUNET_FS_tree_encoder_create (sc->h,
637 GNUNET_FS_tree_encoder_next (p->te);
642 * Process the response (or lack thereof) from
643 * the "fs" service to our 'start index' request.
645 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
646 * @param msg the response we got
649 process_index_start_response (void *cls,
650 const struct GNUNET_MessageHeader *msg)
652 struct GNUNET_FS_PublishContext *sc = cls;
653 struct GNUNET_FS_FileInformation *p;
657 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
662 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
663 _("Can not index file `%s': %s. Will try to insert instead.\n"),
664 p->data.file.filename,
665 _("timeout on index-start request to `fs' service"));
666 p->data.file.do_index = GNUNET_NO;
667 publish_content (sc);
670 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
672 msize = ntohs (msg->size);
673 emsg = (const char *) &msg[1];
674 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
675 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
676 emsg = gettext_noop ("unknown error");
677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
678 _("Can not index file `%s': %s. Will try to insert instead.\n"),
679 p->data.file.filename,
681 p->data.file.do_index = GNUNET_NO;
682 publish_content (sc);
685 p->data.file.index_start_confirmed = GNUNET_YES;
686 /* success! continue with indexing */
687 publish_content (sc);
692 * Function called once the hash computation over an
693 * indexed file has completed.
695 * @param cls closure, our publishing context
696 * @param res resulting hash, NULL on error
699 hash_for_index_cb (void *cls,
700 const GNUNET_HashCode *
703 struct GNUNET_FS_PublishContext *sc = cls;
704 struct GNUNET_FS_FileInformation *p;
705 struct IndexStartMessage *ism;
707 struct GNUNET_CLIENT_Connection *client;
714 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
715 _("Can not index file `%s': %s. Will try to insert instead.\n"),
716 p->data.file.filename,
717 _("failed to compute hash"));
718 p->data.file.do_index = GNUNET_NO;
719 publish_content (sc);
722 if (GNUNET_YES == p->data.file.index_start_confirmed)
724 publish_content (sc);
727 slen = strlen (p->data.file.filename) + 1;
728 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
731 _("Can not index file `%s': %s. Will try to insert instead.\n"),
732 p->data.file.filename,
733 _("filename too long"));
734 p->data.file.do_index = GNUNET_NO;
735 publish_content (sc);
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Hash of indexed file `%s' is `%s'\n",
741 p->data.file.filename,
744 client = GNUNET_CLIENT_connect (sc->h->sched,
749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
750 _("Can not index file `%s': %s. Will try to insert instead.\n"),
751 p->data.file.filename,
752 _("could not connect to `fs' service"));
753 p->data.file.do_index = GNUNET_NO;
754 publish_content (sc);
757 p->data.file.file_id = *res;
758 p->data.file.have_hash = GNUNET_YES;
759 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
761 ism->header.size = htons(sizeof(struct IndexStartMessage) +
763 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
765 GNUNET_DISK_file_get_identifiers (p->data.file.filename,
769 ism->device = htonl (dev);
770 ism->inode = GNUNET_htonll(ino);
774 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
775 _("Failed to get file identifiers for `%s'\n"),
776 p->data.file.filename);
780 p->data.file.filename,
783 GNUNET_break (GNUNET_YES ==
784 GNUNET_CLIENT_transmit_and_get_response (client,
786 GNUNET_TIME_UNIT_FOREVER_REL,
788 &process_index_start_response,
795 * Main function that performs the upload.
796 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
797 * @param tc task context
800 do_upload (void *cls,
801 const struct GNUNET_SCHEDULER_TaskContext *tc)
803 struct GNUNET_FS_PublishContext *sc = cls;
804 struct GNUNET_FS_ProgressInfo pi;
805 struct GNUNET_FS_FileInformation *p;
808 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
812 /* upload of entire hierarchy complete,
813 publish namespace entries */
817 /* find starting position */
818 while ( (p->is_directory) &&
819 (NULL != p->data.dir.entries) &&
821 (NULL == p->data.dir.entries->chk_uri) )
823 p = p->data.dir.entries;
829 /* error with current file, abort all
830 related files as well! */
831 while (NULL != p->dir)
833 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
834 EXTRACTOR_METATYPE_FILENAME);
836 GNUNET_asprintf (&p->emsg,
837 _("Recursive upload failed at `%s'"),
840 GNUNET_FS_file_information_sync (p);
841 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
842 make_publish_status (&pi, sc, p, 0);
843 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
844 pi.value.publish.specifics.error.message = p->emsg;
846 = sc->h->upcb (sc->h->upcb_cls,
849 sc->all_done = GNUNET_YES;
852 /* handle completion */
853 if (NULL != p->chk_uri)
855 /* upload of "p" complete, publish KBlocks! */
856 if (p->keywords != NULL)
858 GNUNET_FS_publish_ksk (sc->h,
866 &publish_kblocks_cont,
871 publish_kblocks_cont (sc,
877 if ( (!p->is_directory) &&
878 (p->data.file.do_index) )
880 if (NULL == p->data.file.filename)
882 p->data.file.do_index = GNUNET_NO;
883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
884 _("Can not index file `%s': %s. Will try to insert instead.\n"),
886 _("needs to be an actual file"));
887 publish_content (sc);
890 if (p->data.file.have_hash)
891 hash_for_index_cb (sc,
892 &p->data.file.file_id);
895 p->start_time = GNUNET_TIME_absolute_get ();
896 GNUNET_CRYPTO_hash_file (sc->h->sched,
897 GNUNET_SCHEDULER_PRIORITY_IDLE,
898 p->data.file.filename,
905 publish_content (sc);
910 * Signal the FS's progress function that we are starting
913 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
914 * @param fi the entry in the publish-structure
915 * @param length length of the file or directory
916 * @param meta metadata for the file or directory (can be modified)
917 * @param uri pointer to the keywords that will be used for this entry (can be modified)
918 * @param anonymity pointer to selected anonymity level (can be modified)
919 * @param priority pointer to selected priority (can be modified)
920 * @param expirationTime pointer to selected expiration time (can be modified)
921 * @param client_info pointer to client context set upon creation (can be modified)
922 * @return GNUNET_OK to continue (always)
925 fip_signal_start(void *cls,
926 struct GNUNET_FS_FileInformation *fi,
928 struct GNUNET_CONTAINER_MetaData *meta,
929 struct GNUNET_FS_Uri **uri,
932 struct GNUNET_TIME_Absolute *expirationTime,
935 struct GNUNET_FS_PublishContext *sc = cls;
936 struct GNUNET_FS_ProgressInfo pi;
938 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
939 make_publish_status (&pi, sc, fi, 0);
940 *client_info = sc->h->upcb (sc->h->upcb_cls,
947 * Publish a file or directory.
949 * @param h handle to the file sharing subsystem
950 * @param fi information about the file or directory structure to publish
951 * @param namespace namespace to publish the file in, NULL for no namespace
952 * @param nid identifier to use for the publishd content in the namespace
953 * (can be NULL, must be NULL if namespace is NULL)
954 * @param nuid update-identifier that will be used for future updates
955 * (can be NULL, must be NULL if namespace or nid is NULL)
956 * @param options options for the publication
957 * @return context that can be used to control the publish operation
959 struct GNUNET_FS_PublishContext *
960 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
961 struct GNUNET_FS_FileInformation *fi,
962 struct GNUNET_FS_Namespace *namespace,
965 enum GNUNET_FS_PublishOptions options)
967 struct GNUNET_FS_PublishContext *ret;
968 struct GNUNET_DATASTORE_Handle *dsh;
970 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
972 dsh = GNUNET_DATASTORE_connect (h->cfg,
981 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
985 ret->namespace = namespace;
986 if (namespace != NULL)
989 GNUNET_assert (NULL != nid);
990 ret->nid = GNUNET_strdup (nid);
992 ret->nuid = GNUNET_strdup (nuid);
994 // FIXME: make upload persistent!
997 GNUNET_FS_file_information_inspect (ret->fi,
1000 ret->fi_pos = ret->fi;
1002 // FIXME: calculate space needed for "fi"
1003 // and reserve as first task (then trigger
1004 // "do_upload" from that continuation)!
1006 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1007 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1015 * Signal the FS's progress function that we are stopping
1018 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1019 * @param fi the entry in the publish-structure
1020 * @param length length of the file or directory
1021 * @param meta metadata for the file or directory (can be modified)
1022 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1023 * @param anonymity pointer to selected anonymity level (can be modified)
1024 * @param priority pointer to selected priority (can be modified)
1025 * @param expirationTime pointer to selected expiration time (can be modified)
1026 * @param client_info pointer to client context set upon creation (can be modified)
1027 * @return GNUNET_OK to continue (always)
1030 fip_signal_stop(void *cls,
1031 struct GNUNET_FS_FileInformation *fi,
1033 struct GNUNET_CONTAINER_MetaData *meta,
1034 struct GNUNET_FS_Uri **uri,
1035 uint32_t *anonymity,
1037 struct GNUNET_TIME_Absolute *expirationTime,
1040 struct GNUNET_FS_PublishContext*sc = cls;
1041 struct GNUNET_FS_ProgressInfo pi;
1044 off = (fi->chk_uri == NULL) ? 0 : length;
1045 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1046 make_publish_status (&pi, sc, fi, off);
1047 GNUNET_break (NULL ==
1048 sc->h->upcb (sc->h->upcb_cls,
1050 *client_info = NULL;
1056 * Stop an upload. Will abort incomplete uploads (but
1057 * not remove blocks that have already been publishd) or
1058 * simply clean up the state for completed uploads.
1059 * Must NOT be called from within the event callback!
1061 * @param sc context for the upload to stop
1064 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *sc)
1066 if (GNUNET_SCHEDULER_NO_TASK != sc->upload_task)
1067 GNUNET_SCHEDULER_cancel (sc->h->sched, sc->upload_task);
1069 GNUNET_assert (sc->all_done == GNUNET_YES);
1070 // FIXME: remove from persistence DB (?) --- think more about
1071 // shutdown / persistent-resume APIs!!!
1072 GNUNET_FS_file_information_inspect (sc->fi,
1075 if (GNUNET_YES == sc->in_network_wait)
1077 sc->in_network_wait = GNUNET_SYSERR;
1080 publish_cleanup (sc);
1085 * Context for the KSK publication.
1087 struct PublishKskContext
1093 struct GNUNET_FS_Uri *ksk_uri;
1096 * Global FS context.
1098 struct GNUNET_FS_Handle *h;
1101 * The master block that we are sending
1102 * (in plaintext), has "mdsize+slen" more
1103 * bytes than the struct would suggest.
1108 * Buffer of the same size as "kb" for
1109 * the encrypted version.
1114 * Handle to the datastore, NULL if we are just
1117 struct GNUNET_DATASTORE_Handle *dsh;
1120 * Function to call once we're done.
1122 GNUNET_FS_PublishContinuation cont;
1130 * When should the KBlocks expire?
1132 struct GNUNET_TIME_Absolute expirationTime;
1135 * Size of the serialized metadata.
1140 * Size of the (CHK) URI as a string.
1145 * Keyword that we are currently processing.
1150 * Anonymity level for the KBlocks.
1155 * Priority for the KBlocks.
1162 * Continuation of "GNUNET_FS_publish_ksk" that performs
1163 * the actual publishing operation (iterating over all
1166 * @param cls closure of type "struct PublishKskContext*"
1170 publish_ksk_cont (void *cls,
1171 const struct GNUNET_SCHEDULER_TaskContext *tc);
1175 * Function called by the datastore API with
1176 * the result from the PUT request.
1178 * @param cls closure of type "struct PublishKskContext*"
1179 * @param success GNUNET_OK on success
1180 * @param msg error message (or NULL)
1183 kb_put_cont (void *cls,
1187 struct PublishKskContext *pkc = cls;
1189 if (GNUNET_OK != success)
1191 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1192 GNUNET_free (pkc->cpy);
1193 GNUNET_free (pkc->kb);
1194 pkc->cont (pkc->cont_cls,
1197 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1201 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1204 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1209 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1210 * publishing operation (iterating over all of the keywords).
1212 * @param cls closure of type "struct PublishKskContext*"
1216 publish_ksk_cont (void *cls,
1217 const struct GNUNET_SCHEDULER_TaskContext *tc)
1219 struct PublishKskContext *pkc = cls;
1220 const char *keyword;
1221 GNUNET_HashCode key;
1222 GNUNET_HashCode query;
1223 struct GNUNET_CRYPTO_AesSessionKey skey;
1224 struct GNUNET_CRYPTO_AesInitializationVector iv;
1225 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1228 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1229 (NULL == pkc->dsh) )
1231 if (NULL != pkc->dsh)
1232 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1233 GNUNET_free (pkc->cpy);
1234 GNUNET_free (pkc->kb);
1235 pkc->cont (pkc->cont_cls,
1238 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1242 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1243 /* first character of keyword indicates if it is
1244 mandatory or not -- ignore for hashing */
1245 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1246 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1247 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1248 pkc->slen + pkc->mdsize,
1252 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1253 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1254 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1255 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1257 GNUNET_assert (GNUNET_OK ==
1258 GNUNET_CRYPTO_rsa_sign (pk,
1260 &pkc->cpy->signature));
1261 GNUNET_CRYPTO_rsa_key_free (pk);
1262 GNUNET_DATASTORE_put (pkc->dsh,
1266 sizeof (struct KBlock) +
1269 GNUNET_DATASTORE_BLOCKTYPE_KBLOCK,
1272 pkc->expirationTime,
1273 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1280 * Publish a CHK under various keywords on GNUnet.
1282 * @param h handle to the file sharing subsystem
1283 * @param ksk_uri keywords to use
1284 * @param meta metadata to use
1285 * @param uri URI to refer to in the KBlock
1286 * @param expirationTime when the KBlock expires
1287 * @param anonymity anonymity level for the KBlock
1288 * @param priority priority for the KBlock
1289 * @param options publication options
1290 * @param cont continuation
1291 * @param cont_cls closure for cont
1294 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1295 struct GNUNET_FS_Uri *ksk_uri,
1296 struct GNUNET_CONTAINER_MetaData *meta,
1297 struct GNUNET_FS_Uri *uri,
1298 struct GNUNET_TIME_Absolute expirationTime,
1301 enum GNUNET_FS_PublishOptions options,
1302 GNUNET_FS_PublishContinuation cont,
1305 struct PublishKskContext *pkc;
1311 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1313 pkc->expirationTime = expirationTime;
1314 pkc->anonymity = anonymity;
1315 pkc->priority = priority;
1317 pkc->cont_cls = cont_cls;
1318 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1320 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1322 if (pkc->dsh == NULL)
1324 cont (cont_cls, NULL, _("Could not connect to datastore."));
1332 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1333 GNUNET_assert (pkc->mdsize >= 0);
1334 uris = GNUNET_FS_uri_to_string (uri);
1335 pkc->slen = strlen (uris) + 1;
1336 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1337 if (size > MAX_KBLOCK_SIZE)
1339 size = MAX_KBLOCK_SIZE;
1340 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1342 pkc->kb = GNUNET_malloc (size);
1343 kbe = (char *) &pkc->kb[1];
1344 memcpy (kbe, uris, pkc->slen);
1346 sptr = &kbe[pkc->slen];
1348 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1351 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1352 if (pkc->mdsize == -1)
1355 GNUNET_free (pkc->kb);
1356 if (pkc->dsh != NULL)
1357 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1358 cont (cont_cls, NULL, _("Internal error."));
1362 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1364 pkc->cpy = GNUNET_malloc (size);
1365 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1366 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1369 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1370 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1371 GNUNET_SCHEDULER_add_continuation (h->sched,
1374 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1379 * Context for the SKS publication.
1381 struct PublishSksContext
1385 * Global FS context.
1387 struct GNUNET_FS_Uri *uri;
1390 * Handle to the datastore.
1392 struct GNUNET_DATASTORE_Handle *dsh;
1395 * Function to call once we're done.
1397 GNUNET_FS_PublishContinuation cont;
1408 * Function called by the datastore API with
1409 * the result from the PUT (SBlock) request.
1411 * @param cls closure of type "struct PublishSksContext*"
1412 * @param success GNUNET_OK on success
1413 * @param msg error message (or NULL)
1416 sb_put_cont (void *cls,
1420 struct PublishSksContext *psc = cls;
1422 if (NULL != psc->dsh)
1423 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1424 if (GNUNET_OK != success)
1425 psc->cont (psc->cont_cls,
1429 psc->cont (psc->cont_cls,
1432 GNUNET_FS_uri_destroy (psc->uri);
1438 * Publish an SBlock on GNUnet.
1440 * @param h handle to the file sharing subsystem
1441 * @param namespace namespace to publish in
1442 * @param identifier identifier to use
1443 * @param update update identifier to use
1444 * @param meta metadata to use
1445 * @param uri URI to refer to in the SBlock
1446 * @param expirationTime when the SBlock expires
1447 * @param anonymity anonymity level for the SBlock
1448 * @param priority priority for the SBlock
1449 * @param options publication options
1450 * @param cont continuation
1451 * @param cont_cls closure for cont
1454 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1455 struct GNUNET_FS_Namespace *namespace,
1456 const char *identifier,
1458 struct GNUNET_CONTAINER_MetaData *meta,
1459 const struct GNUNET_FS_Uri *uri,
1460 struct GNUNET_TIME_Absolute expirationTime,
1463 enum GNUNET_FS_PublishOptions options,
1464 GNUNET_FS_PublishContinuation cont,
1467 struct PublishSksContext *psc;
1468 struct GNUNET_CRYPTO_AesSessionKey sk;
1469 struct GNUNET_CRYPTO_AesInitializationVector iv;
1470 struct GNUNET_FS_Uri *sks_uri;
1478 struct SBlock *sb_enc;
1480 GNUNET_HashCode key; /* hash of thisId = key */
1481 GNUNET_HashCode id; /* hash of hc = identifier */
1483 uris = GNUNET_FS_uri_to_string (uri);
1484 slen = strlen (uris) + 1;
1485 idlen = strlen (identifier);
1488 nidlen = strlen (update) + 1;
1489 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1491 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1492 if (size > MAX_SBLOCK_SIZE)
1494 size = MAX_SBLOCK_SIZE;
1495 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1497 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1498 dest = (char *) &sb[1];
1499 memcpy (dest, update, nidlen);
1501 memcpy (dest, uris, slen);
1503 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1506 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1514 _("Internal error."));
1517 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1518 sb_enc = GNUNET_malloc (sizeof (struct SBlock) + size);
1519 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1520 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1521 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1522 sks_uri->type = sks;
1523 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1524 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1525 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1526 &sks_uri->data.sks.namespace);
1527 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1528 GNUNET_CRYPTO_hash_xor (&id,
1529 &sks_uri->data.sks.namespace,
1530 &sb_enc->identifier);
1531 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1532 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1533 size - sizeof (struct SBlock),
1537 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1538 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1539 + sizeof(struct SBlock)
1540 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1541 GNUNET_assert (GNUNET_OK ==
1542 GNUNET_CRYPTO_rsa_sign (namespace->key,
1544 &sb_enc->signature));
1545 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1548 psc->cont_cls = cont_cls;
1549 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1551 GNUNET_free (sb_enc);
1558 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1559 if (NULL == psc->dsh)
1561 GNUNET_free (sb_enc);
1565 _("Failed to connect to datastore."));
1569 GNUNET_DATASTORE_put (psc->dsh,
1574 GNUNET_DATASTORE_BLOCKTYPE_SBLOCK,
1578 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1582 GNUNET_free (sb_enc);
1589 * Add an entry into a namespace.
1591 * @param dstU to which URI should the namespace entry refer?
1592 * @param md what meta-data should be associated with the
1594 * @param thisId name of this entry in the namespace (keyword/identifier)
1595 * @param nextId name of the update for this entry (to be published in
1596 * the future; maybe NULL)
1597 * @param pid unique identifier of the namespace/pseudonym
1598 * @return URI on success, NULL on error
1600 struct GNUNET_ECRS_URI *
1601 GNUNET_ECRS_namespace_add_content (struct GNUNET_GE_Context *ectx,
1602 struct GNUNET_GC_Configuration *cfg,
1603 const GNUNET_HashCode * pid,
1604 uint32_t anonymityLevel,
1606 GNUNET_CronTime expiration,
1609 const struct GNUNET_ECRS_URI *dstU,
1610 const struct GNUNET_MetaData *md)
1612 struct GNUNET_ECRS_URI *uri;
1613 struct GNUNET_ClientServerConnection *sock;
1614 GNUNET_DatastoreValue *value;
1616 unsigned int mdsize;
1617 struct GNUNET_RSA_PrivateKey *hk;
1618 GNUNET_EC_SBlock *sb;
1621 GNUNET_HashCode hc; /* hash of thisId = key */
1622 GNUNET_HashCode hc2; /* hash of hc = identifier */
1624 unsigned int nidlen;
1626 hk = read_namespace_key (cfg, pid);
1630 /* THEN: construct GNUNET_EC_SBlock */
1631 dstURI = GNUNET_ECRS_uri_to_string (dstU);
1632 mdsize = GNUNET_meta_data_get_serialized_size (md, GNUNET_SERIALIZE_PART);
1635 nidlen = strlen (nextId) + 1;
1636 size = mdsize + sizeof (GNUNET_EC_SBlock) + strlen (dstURI) + 1 + nidlen;
1637 if (size > MAX_SBLOCK_SIZE)
1639 size = MAX_SBLOCK_SIZE;
1641 size - (sizeof (GNUNET_EC_SBlock) + strlen (dstURI) + 1 + nidlen);
1643 value = GNUNET_malloc (sizeof (GNUNET_DatastoreValue) + size);
1644 sb = (GNUNET_EC_SBlock *) & value[1];
1645 sb->type = htonl (GNUNET_ECRS_BLOCKTYPE_SIGNED);
1646 destPos = (char *) &sb[1];
1647 memcpy (destPos, nextId, nidlen);
1649 memcpy (destPos, dstURI, strlen (dstURI) + 1);
1650 destPos += strlen (dstURI) + 1;
1651 mdsize = GNUNET_meta_data_serialize (ectx,
1654 mdsize, GNUNET_SERIALIZE_PART);
1657 GNUNET_GE_BREAK (ectx, 0);
1658 GNUNET_free (dstURI);
1659 GNUNET_RSA_free_key (hk);
1660 GNUNET_free (value);
1663 size = sizeof (GNUNET_EC_SBlock) + mdsize + strlen (dstURI) + 1 + nidlen;
1664 value->size = htonl (sizeof (GNUNET_DatastoreValue) + size);
1665 value->type = htonl (GNUNET_ECRS_BLOCKTYPE_SIGNED);
1666 value->priority = htonl (priority);
1667 value->anonymity_level = htonl (anonymityLevel);
1668 value->expiration_time = GNUNET_htonll (expiration);
1669 GNUNET_hash (thisId, strlen (thisId), &hc);
1670 GNUNET_hash (&hc, sizeof (GNUNET_HashCode), &hc2);
1671 uri = GNUNET_malloc (sizeof (URI));
1673 GNUNET_RSA_get_public_key (hk, &sb->subspace);
1674 GNUNET_hash (&sb->subspace,
1675 sizeof (GNUNET_RSA_PublicKey), &uri->data.sks.namespace);
1676 GNUNET_GE_BREAK (ectx, 0 == memcmp (&uri->data.sks.namespace,
1677 pid, sizeof (GNUNET_HashCode)));
1678 uri->data.sks.identifier = GNUNET_strdup (thisId);
1679 GNUNET_hash_xor (&hc2, &uri->data.sks.namespace, &sb->identifier);
1680 GNUNET_ECRS_encryptInPlace (&hc, &sb[1], size - sizeof (GNUNET_EC_SBlock));
1681 GNUNET_GE_ASSERT (ectx,
1682 GNUNET_OK == GNUNET_RSA_sign (hk,
1686 (GNUNET_RSA_Signature) -
1688 (GNUNET_RSA_PublicKey) -
1689 sizeof (unsigned int),
1692 GNUNET_RSA_free_key (hk);
1693 sock = GNUNET_client_connection_create (ectx, cfg);
1694 ret = GNUNET_FS_insert (sock, value);
1695 if (ret != GNUNET_OK)
1700 GNUNET_client_connection_destroy (sock);
1701 GNUNET_free (value);
1702 GNUNET_free (dstURI);
1710 /* end of fs_publish.c */