2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 3, 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_namespace.c
23 * @brief create and destroy namespaces
24 * @author Christian Grothoff
27 #include "gnunet_constants.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fs_service.h"
33 #define DEBUG_NAMESPACE GNUNET_EXTRA_LOGGING
37 * Maximum legal size for an sblock.
39 #define MAX_SBLOCK_SIZE (60 * 1024)
42 * Maximum legal size for an nblock.
44 #define MAX_NBLOCK_SIZE (60 * 1024)
48 * Return the name of the directory in which we store
49 * our local namespaces (or rather, their public keys).
51 * @param h global fs handle
52 * @return NULL on error, otherwise the name of the directory
55 get_namespace_directory (struct GNUNET_FS_Handle *h)
60 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "IDENTITY_DIR",
63 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
64 _("Configuration fails to specify `%s' in section `%s'\n"),
65 "IDENTITY_DIR", "fs");
73 * Return the name of the directory in which we store
74 * the update information graph for the given local namespace.
76 * @param ns namespace handle
77 * @return NULL on error, otherwise the name of the directory
80 get_update_information_directory (struct GNUNET_FS_Namespace *ns)
86 GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg, "FS", "UPDATE_DIR",
89 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
90 _("Configuration fails to specify `%s' in section `%s'\n"),
94 GNUNET_asprintf (&ret, "%s%s%s", dn, DIR_SEPARATOR_STR, ns->name);
101 * Write the namespace update node graph to a file.
103 * @param ns namespace to dump
106 write_update_information_graph (struct GNUNET_FS_Namespace *ns)
109 struct GNUNET_BIO_WriteHandle *wh;
111 struct NamespaceUpdateNode *n;
114 fn = get_update_information_directory (ns);
115 wh = GNUNET_BIO_write_open (fn);
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
123 if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, ns->update_node_count))
125 for (i = 0; i < ns->update_node_count; i++)
127 n = ns->update_nodes[i];
128 uris = GNUNET_FS_uri_to_string (n->uri);
129 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) ||
130 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) ||
131 (GNUNET_OK != GNUNET_BIO_write_string (wh, n->update)) ||
132 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
140 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"),
148 * Read the namespace update node graph from a file.
150 * @param ns namespace to read
153 read_update_information_graph (struct GNUNET_FS_Namespace *ns)
156 struct GNUNET_BIO_ReadHandle *rh;
158 struct NamespaceUpdateNode *n;
163 fn = get_update_information_directory (ns);
164 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
169 rh = GNUNET_BIO_read_open (fn);
175 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
180 if (count > 1024 * 1024)
187 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
192 GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
194 for (i = 0; i < count; i++)
196 n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
197 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024))
198 || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
200 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
201 (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
204 GNUNET_free_non_null (n->id);
205 GNUNET_free_non_null (n->update);
207 GNUNET_CONTAINER_meta_data_destroy (n->md);
211 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
218 GNUNET_free_non_null (n->update);
219 GNUNET_CONTAINER_meta_data_destroy (n->md);
223 ns->update_nodes[i] = n;
225 ns->update_node_count = i;
227 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"), emsg);
237 * Context for advertising a namespace.
239 struct AdvertisementContext
242 * Function to call with the result.
244 GNUNET_FS_PublishContinuation cont;
254 struct GNUNET_DATASTORE_Handle *dsh;
259 struct GNUNET_FS_Uri *ksk_uri;
267 * NBlock to sign and store.
274 struct GNUNET_FS_Namespace *ns;
279 struct GNUNET_FS_BlockOptions bo;
282 * Number of bytes of plaintext.
287 * Current keyword offset.
294 * Disconnect from the datastore.
296 * @param cls datastore handle
297 * @param tc scheduler context
300 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
302 struct GNUNET_DATASTORE_Handle *dsh = cls;
304 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
309 * Continuation called to notify client about result of the
312 * @param cls closure (our struct AdvertismentContext)
313 * @param success GNUNET_SYSERR on failure
314 * @param min_expiration minimum expiration time required for content to be stored
315 * @param msg NULL on success, otherwise an error message
318 advertisement_cont (void *cls, int success,
319 struct GNUNET_TIME_Absolute min_expiration,
322 struct AdvertisementContext *ac = cls;
325 GNUNET_HashCode query;
326 struct GNUNET_CRYPTO_AesSessionKey skey;
327 struct GNUNET_CRYPTO_AesInitializationVector iv;
328 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
330 if (GNUNET_OK != success)
333 GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
334 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
338 msg = _("Unknown error");
340 if (ac->cont != NULL)
341 ac->cont (ac->cont_cls, NULL, msg);
342 GNUNET_FS_uri_destroy (ac->ksk_uri);
343 GNUNET_free (ac->pt);
344 GNUNET_free (ac->nb);
345 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
349 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
352 GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
353 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
354 if (ac->cont != NULL)
355 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
356 GNUNET_FS_uri_destroy (ac->ksk_uri);
357 GNUNET_free (ac->pt);
358 GNUNET_free (ac->nb);
359 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
363 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
364 /* first character of keyword indicates if it is
365 * mandatory or not -- ignore for hashing */
366 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
367 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
368 GNUNET_CRYPTO_aes_encrypt (ac->pt, ac->pt_size, &skey, &iv, &ac->nb[1]);
369 GNUNET_break (GNUNET_OK ==
370 GNUNET_CRYPTO_rsa_sign (ac->ns->key, &ac->nb->ns_purpose,
371 &ac->nb->ns_signature));
372 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
373 GNUNET_assert (pk != NULL);
374 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
375 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
376 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
378 GNUNET_break (GNUNET_OK ==
379 GNUNET_CRYPTO_rsa_sign (pk, &ac->nb->ksk_purpose,
380 &ac->nb->ksk_signature));
381 GNUNET_CRYPTO_rsa_key_free (pk);
382 GNUNET_DATASTORE_put (ac->dsh, 0 /* no reservation */ ,
383 &query, ac->pt_size + sizeof (struct NBlock), ac->nb,
384 GNUNET_BLOCK_TYPE_FS_NBLOCK, ac->bo.content_priority,
385 ac->bo.anonymity_level, ac->bo.replication_level,
386 ac->bo.expiration_time, -2, 1,
387 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &advertisement_cont,
393 * Publish an advertismement for a namespace.
395 * @param h handle to the file sharing subsystem
396 * @param ksk_uri keywords to use for advertisment
397 * @param namespace handle for the namespace that should be advertised
398 * @param meta meta-data for the namespace advertisement
399 * @param bo block options
400 * @param rootEntry name of the root of the namespace
401 * @param cont continuation
402 * @param cont_cls closure for cont
405 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
406 struct GNUNET_FS_Uri *ksk_uri,
407 struct GNUNET_FS_Namespace *namespace,
408 const struct GNUNET_CONTAINER_MetaData *meta,
409 const struct GNUNET_FS_BlockOptions *bo,
410 const char *rootEntry,
411 GNUNET_FS_PublishContinuation cont,
419 struct GNUNET_DATASTORE_Handle *dsh;
420 struct AdvertisementContext *ctx;
423 /* create advertisements */
424 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
427 cont (cont_cls, NULL, _("Failed to serialize meta data"));
430 reslen = strlen (rootEntry) + 1;
431 size = mdsize + sizeof (struct NBlock) + reslen;
432 if (size > MAX_NBLOCK_SIZE)
434 size = MAX_NBLOCK_SIZE;
435 mdsize = size - sizeof (struct NBlock) - reslen;
438 pt = GNUNET_malloc (mdsize + reslen);
439 memcpy (pt, rootEntry, reslen);
442 GNUNET_CONTAINER_meta_data_serialize (meta, &mdst, mdsize,
443 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
448 cont (cont_cls, NULL, _("Failed to serialize meta data"));
451 size = mdsize + sizeof (struct NBlock) + reslen;
452 nb = GNUNET_malloc (size);
453 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &nb->subspace);
454 nb->ns_purpose.size =
455 htonl (mdsize + reslen +
456 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
457 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
458 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
459 nb->ksk_purpose.size =
460 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
461 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
462 dsh = GNUNET_DATASTORE_connect (h->cfg);
467 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
470 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
472 ctx->cont_cls = cont_cls;
474 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
477 ctx->pt_size = mdsize + reslen;
481 advertisement_cont (ctx, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
486 * Create a namespace with the given name; if one already
487 * exists, return a handle to the existing namespace.
489 * @param h handle to the file sharing subsystem
490 * @param name name to use for the namespace
491 * @return handle to the namespace, NULL on error
493 struct GNUNET_FS_Namespace *
494 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
498 struct GNUNET_FS_Namespace *ret;
500 dn = get_namespace_directory (h);
501 GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
503 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
506 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
507 if (ret->key == NULL)
509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
510 _("Failed to create or read private key for namespace `%s'\n"),
516 ret->name = GNUNET_strdup (name);
523 * Delete a namespace handle. Can be used for a clean shutdown (free
524 * memory) or also to freeze the namespace to prevent further
525 * insertions by anyone.
527 * @param namespace handle to the namespace that should be deleted / freed
528 * @param freeze prevents future insertions; creating a namespace
529 * with the same name again will create a fresh namespace instead
531 * @return GNUNET_OK on success, GNUNET_SYSERR on error
534 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace, int freeze)
537 struct NamespaceUpdateNode *nsn;
542 if (0 != UNLINK (namespace->filename))
543 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
544 namespace->filename);
546 if (0 == namespace->rc)
548 GNUNET_CRYPTO_rsa_key_free (namespace->key);
549 GNUNET_free (namespace->filename);
550 GNUNET_free (namespace->name);
551 for (i = 0; i < namespace->update_node_count; i++)
553 nsn = namespace->update_nodes[i];
554 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
555 GNUNET_FS_uri_destroy (nsn->uri);
556 GNUNET_free (nsn->id);
557 GNUNET_free (nsn->update);
560 GNUNET_array_grow (namespace->update_nodes, namespace->update_node_count,
562 if (namespace->update_map != NULL)
563 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
564 GNUNET_free (namespace);
571 * Context for the 'process_namespace' callback.
572 * Specifies a function to call on each namespace.
574 struct ProcessNamespaceContext
579 GNUNET_FS_NamespaceInfoProcessor cb;
589 * Function called with a filename of a namespace. Reads the key and
590 * calls the callback.
592 * @param cls closure (struct ProcessNamespaceContext)
593 * @param filename complete filename (absolute path)
594 * @return GNUNET_OK to continue to iterate,
595 * GNUNET_SYSERR to abort iteration with error!
598 process_namespace (void *cls, const char *filename)
600 struct ProcessNamespaceContext *pnc = cls;
601 struct GNUNET_CRYPTO_RsaPrivateKey *key;
602 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
607 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
612 ("Failed to read namespace private key file `%s', deleting it!\n"),
614 if (0 != UNLINK (filename))
615 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
618 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
619 GNUNET_CRYPTO_rsa_key_free (key);
620 GNUNET_CRYPTO_hash (&pk, sizeof (pk), &id);
622 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
624 pnc->cb (pnc->cb_cls, name, &id);
630 * Build a list of all available local (!) namespaces The returned
631 * names are only the nicknames since we only iterate over the local
634 * @param h handle to the file sharing subsystem
635 * @param cb function to call on each known namespace
636 * @param cb_cls closure for cb
639 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
640 GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls)
643 struct ProcessNamespaceContext ctx;
645 dn = get_namespace_directory (h);
650 GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx);
658 * Context for the SKS publication.
660 struct PublishSksContext
664 * URI of the new entry in the namespace.
666 struct GNUNET_FS_Uri *uri;
669 * Namespace update node to add to namespace on success (or to be
670 * deleted if publishing failed).
672 struct NamespaceUpdateNode *nsn;
675 * Namespace we're publishing to.
677 struct GNUNET_FS_Namespace *namespace;
680 * Handle to the datastore.
682 struct GNUNET_DATASTORE_Handle *dsh;
685 * Function to call once we're done.
687 GNUNET_FS_PublishContinuation cont;
698 * Function called by the datastore API with
699 * the result from the PUT (SBlock) request.
701 * @param cls closure of type "struct PublishSksContext*"
702 * @param success GNUNET_OK on success
703 * @param min_expiration minimum expiration time required for content to be stored
704 * @param msg error message (or NULL)
707 sb_put_cont (void *cls, int success,
708 struct GNUNET_TIME_Absolute min_expiration,
711 struct PublishSksContext *psc = cls;
714 if (NULL != psc->dsh)
716 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
719 if (GNUNET_OK != success)
721 if (psc->cont != NULL)
722 psc->cont (psc->cont_cls, NULL, msg);
726 if (psc->nsn != NULL)
728 /* FIXME: this can be done much more
729 * efficiently by simply appending to the
730 * file and overwriting the 4-byte header */
731 if (psc->namespace->update_nodes == NULL)
732 read_update_information_graph (psc->namespace);
733 GNUNET_array_append (psc->namespace->update_nodes,
734 psc->namespace->update_node_count, psc->nsn);
735 if (psc->namespace->update_map != NULL)
737 GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
738 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map, &hc,
740 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
743 write_update_information_graph (psc->namespace);
745 if (psc->cont != NULL)
746 psc->cont (psc->cont_cls, psc->uri, NULL);
748 GNUNET_FS_namespace_delete (psc->namespace, GNUNET_NO);
749 GNUNET_FS_uri_destroy (psc->uri);
750 if (psc->nsn != NULL)
752 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
753 GNUNET_FS_uri_destroy (psc->nsn->uri);
754 GNUNET_free (psc->nsn->id);
755 GNUNET_free (psc->nsn->update);
756 GNUNET_free (psc->nsn);
763 * Publish an SBlock on GNUnet.
765 * @param h handle to the file sharing subsystem
766 * @param namespace namespace to publish in
767 * @param identifier identifier to use
768 * @param update update identifier to use
769 * @param meta metadata to use
770 * @param uri URI to refer to in the SBlock
771 * @param bo block options
772 * @param options publication options
773 * @param cont continuation
774 * @param cont_cls closure for cont
777 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
778 struct GNUNET_FS_Namespace *namespace,
779 const char *identifier, const char *update,
780 const struct GNUNET_CONTAINER_MetaData *meta,
781 const struct GNUNET_FS_Uri *uri,
782 const struct GNUNET_FS_BlockOptions *bo,
783 enum GNUNET_FS_PublishOptions options,
784 GNUNET_FS_PublishContinuation cont, void *cont_cls)
786 struct PublishSksContext *psc;
787 struct GNUNET_CRYPTO_AesSessionKey sk;
788 struct GNUNET_CRYPTO_AesInitializationVector iv;
789 struct GNUNET_FS_Uri *sks_uri;
797 struct SBlock *sb_enc;
799 struct GNUNET_CONTAINER_MetaData *mmeta;
800 GNUNET_HashCode key; /* hash of thisId = key */
801 GNUNET_HashCode id; /* hash of hc = identifier */
802 GNUNET_HashCode query; /* id ^ nsid = DB query */
805 mmeta = GNUNET_CONTAINER_meta_data_create ();
807 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
808 uris = GNUNET_FS_uri_to_string (uri);
809 slen = strlen (uris) + 1;
810 idlen = strlen (identifier);
812 nidlen = strlen (update) + 1;
815 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
816 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
817 if (size > MAX_SBLOCK_SIZE)
819 size = MAX_SBLOCK_SIZE;
820 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
822 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
823 dest = (char *) &sb[1];
825 memcpy (dest, update, nidlen);
829 memcpy (dest, uris, slen);
833 GNUNET_CONTAINER_meta_data_serialize (mmeta, &dest, mdsize,
834 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
835 GNUNET_CONTAINER_meta_data_destroy (mmeta);
840 cont (cont_cls, NULL, _("Internal error."));
843 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
844 sb_enc = GNUNET_malloc (size);
845 GNUNET_CRYPTO_hash (identifier, idlen, &key);
846 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
847 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
849 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
850 GNUNET_CRYPTO_hash (&sb_enc->subspace,
851 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
852 &sks_uri->data.sks.namespace);
853 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
854 GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.namespace,
855 &sb_enc->identifier);
856 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
857 GNUNET_CRYPTO_aes_encrypt (&sb[1], size - sizeof (struct SBlock), &sk, &iv,
859 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
860 sb_enc->purpose.size =
861 htonl (slen + mdsize + nidlen + sizeof (struct SBlock) -
862 sizeof (struct GNUNET_CRYPTO_RsaSignature));
863 GNUNET_assert (GNUNET_OK ==
864 GNUNET_CRYPTO_rsa_sign (namespace->key, &sb_enc->purpose,
865 &sb_enc->signature));
866 psc = GNUNET_malloc (sizeof (struct PublishSksContext));
869 psc->namespace = namespace;
871 psc->cont_cls = cont_cls;
872 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
874 GNUNET_free (sb_enc);
876 sb_put_cont (psc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
879 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
880 if (NULL == psc->dsh)
882 GNUNET_free (sb_enc);
884 sb_put_cont (psc, GNUNET_NO, GNUNET_TIME_UNIT_ZERO_ABS, _("Failed to connect to datastore."));
887 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace, &id, &query);
890 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
891 psc->nsn->id = GNUNET_strdup (identifier);
892 psc->nsn->update = GNUNET_strdup (update);
893 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
894 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
896 GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc,
897 GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority,
898 bo->anonymity_level, bo->replication_level,
899 bo->expiration_time, -2, 1,
900 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
902 GNUNET_free (sb_enc);
907 * Closure for 'process_update_node'.
909 struct ProcessUpdateClosure
912 * Function to call for each node.
914 GNUNET_FS_IdentifierProcessor ip;
924 * Call the iterator in the closure for each node.
926 * @param cls closure (of type 'struct ProcessUpdateClosure *')
927 * @param key current key code
928 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
929 * @return GNUNET_YES if we should continue to
934 process_update_node (void *cls, const GNUNET_HashCode * key, void *value)
936 struct ProcessUpdateClosure *pc = cls;
937 struct NamespaceUpdateNode *nsn = value;
939 pc->ip (pc->ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
945 * Closure for 'find_trees'.
947 struct FindTreeClosure
950 * Namespace we are operating on.
952 struct GNUNET_FS_Namespace *namespace;
955 * Array with 'head's of TREEs.
957 struct NamespaceUpdateNode **tree_array;
960 * Size of 'tree_array'
962 unsigned int tree_array_size;
965 * Current generational ID used.
970 * Identifier for the current TREE, or UINT_MAX for none yet.
977 * Find all nodes reachable from the current node (including the
978 * current node itself). If they are in no tree, add them to the
979 * current one. If they are the head of another tree, merge the
980 * trees. If they are in the middle of another tree, let them be.
981 * We can tell that a node is already in an tree by checking if
982 * its 'nug' field is set to the current 'nug' value. It is the
983 * head of an tree if it is in the 'tree_array' under its respective
986 * In short, we're trying to find the smallest number of tree to
987 * cover a directed graph.
989 * @param cls closure (of type 'struct FindTreeClosure')
990 * @param key current key code
991 * @param value value in the hash map
992 * @return GNUNET_YES if we should continue to
997 find_trees (void *cls, const GNUNET_HashCode * key, void *value)
999 struct FindTreeClosure *fc = cls;
1000 struct NamespaceUpdateNode *nsn = value;
1003 if (nsn->nug == fc->nug)
1005 if (nsn->tree_id == UINT_MAX)
1006 return GNUNET_YES; /* circular */
1007 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
1008 if (fc->tree_array[nsn->tree_id] != nsn)
1009 return GNUNET_YES; /* part of "another" (directed) TREE,
1010 * and not root of it, end trace */
1011 if (nsn->tree_id == fc->id)
1012 return GNUNET_YES; /* that's our own root (can this be?) */
1013 /* merge existing TREE, we have a root for both */
1014 fc->tree_array[nsn->tree_id] = NULL;
1015 if (fc->id == UINT_MAX)
1016 fc->id = nsn->tree_id; /* take over ID */
1021 nsn->tree_id = UINT_MAX; /* mark as undef */
1023 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1024 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map, &hc,
1032 * List all of the identifiers in the namespace for which we could
1033 * produce an update. Namespace updates form a graph where each node
1034 * has a name. Each node can have any number of URI/meta-data entries
1035 * which can each be linked to other nodes. Cycles are possible.
1037 * Calling this function with "next_id" NULL will cause the library to
1038 * call "ip" with a root for each strongly connected component of the
1039 * graph (a root being a node from which all other nodes in the Tree
1042 * Calling this function with "next_id" being the name of a node will
1043 * cause the library to call "ip" with all children of the node. Note
1044 * that cycles within the final tree are possible (including self-loops).
1045 * I know, odd definition of a tree, but the GUI will display an actual
1046 * tree (GtkTreeView), so that's what counts for the term here.
1048 * @param namespace namespace to inspect for updateable content
1049 * @param next_id ID to look for; use NULL to look for tree roots
1050 * @param ip function to call on each updateable identifier
1051 * @param ip_cls closure for ip
1054 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1055 const char *next_id,
1056 GNUNET_FS_IdentifierProcessor ip,
1062 struct NamespaceUpdateNode *nsn;
1063 struct ProcessUpdateClosure pc;
1064 struct FindTreeClosure fc;
1066 if (namespace->update_nodes == NULL)
1067 read_update_information_graph (namespace);
1068 if (namespace->update_nodes == NULL)
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "No updateable nodes found for ID `%s'\n", next_id);
1074 return; /* no nodes */
1076 if (namespace->update_map == NULL)
1078 /* need to construct */
1079 namespace->update_map =
1080 GNUNET_CONTAINER_multihashmap_create (2 +
1081 3 * namespace->update_node_count /
1083 for (i = 0; i < namespace->update_node_count; i++)
1085 nsn = namespace->update_nodes[i];
1086 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1087 GNUNET_CONTAINER_multihashmap_put (namespace->update_map, &hc, nsn,
1088 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1091 if (next_id != NULL)
1093 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
1096 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1097 &process_update_node, &pc);
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Calculating TREEs to find roots of update trees\n");
1104 /* Find heads of TREEs in update graph */
1105 nug = ++namespace->nug_gen;
1106 fc.tree_array = NULL;
1107 fc.tree_array_size = 0;
1109 for (i = 0; i < namespace->update_node_count; i++)
1111 nsn = namespace->update_nodes[i];
1112 if (nsn->nug == nug)
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1118 continue; /* already placed in TREE */
1120 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1122 nsn->tree_id = UINT_MAX;
1125 fc.namespace = namespace;
1126 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1128 if (fc.id == UINT_MAX)
1130 /* start new TREE */
1131 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
1133 if (fc.tree_array[fc.id] == NULL)
1135 fc.tree_array[fc.id] = nsn;
1136 nsn->tree_id = fc.id;
1140 if (fc.id == fc.tree_array_size)
1142 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
1143 nsn->tree_id = fc.id;
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Starting new TREE %u with node `%s'\n", nsn->tree_id,
1150 /* put all nodes with same identifier into this TREE */
1151 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1152 fc.id = nsn->tree_id;
1154 fc.namespace = namespace;
1155 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1160 /* make head of TREE "id" */
1161 fc.tree_array[fc.id] = nsn;
1162 nsn->tree_id = fc.id;
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1169 for (i = 0; i < fc.tree_array_size; i++)
1171 nsn = fc.tree_array[i];
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
1179 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
1182 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
1189 /* end of fs_namespace.c */