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_NO
36 * Return the name of the directory in which we store
37 * our local namespaces (or rather, their public keys).
39 * @param h global fs handle
40 * @return NULL on error, otherwise the name of the directory
43 get_namespace_directory (struct GNUNET_FS_Handle *h)
48 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
49 "FS", "IDENTITY_DIR", &dn))
51 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52 _("Configuration fails to specify `%s' in section `%s'\n"),
53 "IDENTITY_DIR", "fs");
61 * Return the name of the directory in which we store
62 * the update information graph for the given local namespace.
64 * @param ns namespace handle
65 * @return NULL on error, otherwise the name of the directory
68 get_update_information_directory (struct GNUNET_FS_Namespace *ns)
74 GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg,
75 "FS", "UPDATE_DIR", &dn))
77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
78 _("Configuration fails to specify `%s' in section `%s'\n"),
82 GNUNET_asprintf (&ret, "%s%s%s", dn, DIR_SEPARATOR_STR, ns->name);
89 * Write the namespace update node graph to a file.
91 * @param ns namespace to dump
94 write_update_information_graph (struct GNUNET_FS_Namespace *ns)
97 struct GNUNET_BIO_WriteHandle *wh;
99 struct NamespaceUpdateNode *n;
102 fn = get_update_information_directory (ns);
103 wh = GNUNET_BIO_write_open (fn);
106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
111 if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, ns->update_node_count))
113 for (i = 0; i < ns->update_node_count; i++)
115 n = ns->update_nodes[i];
116 uris = GNUNET_FS_uri_to_string (n->uri);
118 GNUNET_BIO_write_string (wh, n->id)) ||
120 GNUNET_BIO_write_meta_data (wh, n->md)) ||
122 GNUNET_BIO_write_string (wh, n->update)) ||
123 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
131 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 _("Failed to write `%s': %s\n"), STRERROR (errno));
139 * Read the namespace update node graph from a file.
141 * @param ns namespace to read
144 read_update_information_graph (struct GNUNET_FS_Namespace *ns)
147 struct GNUNET_BIO_ReadHandle *rh;
149 struct NamespaceUpdateNode *n;
154 fn = get_update_information_directory (ns);
155 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
160 rh = GNUNET_BIO_read_open (fn);
166 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
171 if (count > 1024 * 1024)
178 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
183 GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
185 for (i = 0; i < count; i++)
187 n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
189 GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024)) ||
191 GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
193 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
194 (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
197 GNUNET_free_non_null (n->id);
198 GNUNET_free_non_null (n->update);
200 GNUNET_CONTAINER_meta_data_destroy (n->md);
204 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
211 GNUNET_free_non_null (n->update);
212 GNUNET_CONTAINER_meta_data_destroy (n->md);
216 ns->update_nodes[i] = n;
218 ns->update_node_count = i;
220 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"), emsg);
230 * Context for advertising a namespace.
232 struct AdvertisementContext
235 * Function to call with the result.
237 GNUNET_FS_PublishContinuation cont;
247 struct GNUNET_DATASTORE_Handle *dsh;
252 struct GNUNET_FS_Uri *ksk_uri;
260 * NBlock to sign and store.
267 struct GNUNET_FS_Namespace *ns;
272 struct GNUNET_FS_BlockOptions bo;
275 * Number of bytes of plaintext.
280 * Current keyword offset.
287 * Disconnect from the datastore.
289 * @param cls datastore handle
290 * @param tc scheduler context
293 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
295 struct GNUNET_DATASTORE_Handle *dsh = cls;
297 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
302 * Continuation called to notify client about result of the
305 * @param cls closure (our struct AdvertismentContext)
306 * @param success GNUNET_SYSERR on failure
307 * @param msg NULL on success, otherwise an error message
310 advertisement_cont (void *cls, int success, const char *msg)
312 struct AdvertisementContext *ac = cls;
315 GNUNET_HashCode query;
316 struct GNUNET_CRYPTO_AesSessionKey skey;
317 struct GNUNET_CRYPTO_AesInitializationVector iv;
318 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
320 if (GNUNET_OK != success)
323 GNUNET_SCHEDULER_add_continuation (&do_disconnect,
325 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
329 msg = _("Unknown error");
331 if (ac->cont != NULL)
332 ac->cont (ac->cont_cls, NULL, msg);
333 GNUNET_FS_uri_destroy (ac->ksk_uri);
334 GNUNET_free (ac->pt);
335 GNUNET_free (ac->nb);
336 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
340 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
343 GNUNET_SCHEDULER_add_continuation (&do_disconnect,
345 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
346 if (ac->cont != NULL)
347 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
348 GNUNET_FS_uri_destroy (ac->ksk_uri);
349 GNUNET_free (ac->pt);
350 GNUNET_free (ac->nb);
351 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
355 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
356 /* first character of keyword indicates if it is
357 * mandatory or not -- ignore for hashing */
358 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
359 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
360 GNUNET_CRYPTO_aes_encrypt (ac->pt, ac->pt_size, &skey, &iv, &ac->nb[1]);
361 GNUNET_break (GNUNET_OK ==
362 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
364 &ac->nb->ns_signature));
365 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
366 GNUNET_assert (pk != NULL);
367 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
368 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
369 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
371 GNUNET_break (GNUNET_OK ==
372 GNUNET_CRYPTO_rsa_sign (pk,
373 &ac->nb->ksk_purpose,
374 &ac->nb->ksk_signature));
375 GNUNET_CRYPTO_rsa_key_free (pk);
376 GNUNET_DATASTORE_put (ac->dsh, 0 /* no reservation */ ,
378 ac->pt_size + sizeof (struct NBlock),
380 GNUNET_BLOCK_TYPE_FS_NBLOCK,
381 ac->bo.content_priority,
382 ac->bo.anonymity_level,
383 ac->bo.replication_level,
384 ac->bo.expiration_time,
386 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
387 &advertisement_cont, ac);
392 * Publish an advertismement for a namespace.
394 * @param h handle to the file sharing subsystem
395 * @param ksk_uri keywords to use for advertisment
396 * @param namespace handle for the namespace that should be advertised
397 * @param meta meta-data for the namespace advertisement
398 * @param bo block options
399 * @param rootEntry name of the root of the namespace
400 * @param cont continuation
401 * @param cont_cls closure for cont
404 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
405 struct GNUNET_FS_Uri *ksk_uri,
406 struct GNUNET_FS_Namespace *namespace,
407 const struct GNUNET_CONTAINER_MetaData *meta,
408 const struct GNUNET_FS_BlockOptions *bo,
409 const char *rootEntry,
410 GNUNET_FS_PublishContinuation cont,
418 struct GNUNET_DATASTORE_Handle *dsh;
419 struct AdvertisementContext *ctx;
422 /* create advertisements */
423 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
426 cont (cont_cls, NULL, _("Failed to serialize meta data"));
429 reslen = strlen (rootEntry) + 1;
430 size = mdsize + sizeof (struct NBlock) + reslen;
431 if (size > MAX_NBLOCK_SIZE)
433 size = MAX_NBLOCK_SIZE;
434 mdsize = size - sizeof (struct NBlock) - reslen;
437 pt = GNUNET_malloc (mdsize + reslen);
438 memcpy (pt, rootEntry, reslen);
440 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
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 = htonl (mdsize + reslen +
455 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
458 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
459 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
460 nb->ksk_purpose.size =
461 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
462 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
463 dsh = GNUNET_DATASTORE_connect (h->cfg);
468 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
471 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
473 ctx->cont_cls = cont_cls;
475 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
478 ctx->pt_size = mdsize + reslen;
482 advertisement_cont (ctx, GNUNET_OK, NULL);
487 * Create a namespace with the given name; if one already
488 * exists, return a handle to the existing namespace.
490 * @param h handle to the file sharing subsystem
491 * @param name name to use for the namespace
492 * @return handle to the namespace, NULL on error
494 struct GNUNET_FS_Namespace *
495 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
499 struct GNUNET_FS_Namespace *ret;
501 dn = get_namespace_directory (h);
502 GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
504 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
507 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
508 if (ret->key == NULL)
510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
511 _("Failed to create or read private key for namespace `%s'\n"),
517 ret->name = GNUNET_strdup (name);
524 * Delete a namespace handle. Can be used for a clean shutdown (free
525 * memory) or also to freeze the namespace to prevent further
526 * insertions by anyone.
528 * @param namespace handle to the namespace that should be deleted / freed
529 * @param freeze prevents future insertions; creating a namespace
530 * with the same name again will create a fresh namespace instead
532 * @return GNUNET_OK on success, GNUNET_SYSERR on error
535 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace, int freeze)
538 struct NamespaceUpdateNode *nsn;
543 if (0 != UNLINK (namespace->filename))
544 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
545 "unlink", namespace->filename);
547 if (0 == namespace->rc)
549 GNUNET_CRYPTO_rsa_key_free (namespace->key);
550 GNUNET_free (namespace->filename);
551 GNUNET_free (namespace->name);
552 for (i = 0; i < namespace->update_node_count; i++)
554 nsn = namespace->update_nodes[i];
555 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
556 GNUNET_FS_uri_destroy (nsn->uri);
557 GNUNET_free (nsn->id);
558 GNUNET_free (nsn->update);
561 GNUNET_array_grow (namespace->update_nodes,
562 namespace->update_node_count, 0);
563 if (namespace->update_map != NULL)
564 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
565 GNUNET_free (namespace);
572 * Context for the 'process_namespace' callback.
573 * Specifies a function to call on each namespace.
575 struct ProcessNamespaceContext
580 GNUNET_FS_NamespaceInfoProcessor cb;
590 * Function called with a filename of a namespace. Reads the key and
591 * calls the callback.
593 * @param cls closure (struct ProcessNamespaceContext)
594 * @param filename complete filename (absolute path)
595 * @return GNUNET_OK to continue to iterate,
596 * GNUNET_SYSERR to abort iteration with error!
599 process_namespace (void *cls, const char *filename)
601 struct ProcessNamespaceContext *pnc = cls;
602 struct GNUNET_CRYPTO_RsaPrivateKey *key;
603 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
608 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
613 ("Failed to read namespace private key file `%s', deleting it!\n"),
615 if (0 != UNLINK (filename))
616 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
619 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
620 GNUNET_CRYPTO_rsa_key_free (key);
621 GNUNET_CRYPTO_hash (&pk, sizeof (pk), &id);
623 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
625 pnc->cb (pnc->cb_cls, name, &id);
631 * Build a list of all available local (!) namespaces The returned
632 * names are only the nicknames since we only iterate over the local
635 * @param h handle to the file sharing subsystem
636 * @param cb function to call on each known namespace
637 * @param cb_cls closure for cb
640 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
641 GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls)
644 struct ProcessNamespaceContext ctx;
646 dn = get_namespace_directory (h);
651 GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx);
659 * Context for the SKS publication.
661 struct PublishSksContext
665 * URI of the new entry in the namespace.
667 struct GNUNET_FS_Uri *uri;
670 * Namespace update node to add to namespace on success (or to be
671 * deleted if publishing failed).
673 struct NamespaceUpdateNode *nsn;
676 * Namespace we're publishing to.
678 struct GNUNET_FS_Namespace *namespace;
681 * Handle to the datastore.
683 struct GNUNET_DATASTORE_Handle *dsh;
686 * Function to call once we're done.
688 GNUNET_FS_PublishContinuation cont;
699 * Function called by the datastore API with
700 * the result from the PUT (SBlock) request.
702 * @param cls closure of type "struct PublishSksContext*"
703 * @param success GNUNET_OK on success
704 * @param msg error message (or NULL)
707 sb_put_cont (void *cls, int success, const char *msg)
709 struct PublishSksContext *psc = cls;
712 if (NULL != psc->dsh)
714 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
717 if (GNUNET_OK != success)
719 if (psc->cont != NULL)
720 psc->cont (psc->cont_cls, NULL, msg);
724 if (psc->nsn != NULL)
726 /* FIXME: this can be done much more
727 * efficiently by simply appending to the
728 * file and overwriting the 4-byte header */
729 if (psc->namespace->update_nodes == NULL)
730 read_update_information_graph (psc->namespace);
731 GNUNET_array_append (psc->namespace->update_nodes,
732 psc->namespace->update_node_count, psc->nsn);
733 if (psc->namespace->update_map != NULL)
735 GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
736 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map,
739 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
742 write_update_information_graph (psc->namespace);
744 if (psc->cont != NULL)
745 psc->cont (psc->cont_cls, psc->uri, NULL);
747 GNUNET_FS_namespace_delete (psc->namespace, GNUNET_NO);
748 GNUNET_FS_uri_destroy (psc->uri);
749 if (psc->nsn != NULL)
751 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
752 GNUNET_FS_uri_destroy (psc->nsn->uri);
753 GNUNET_free (psc->nsn->id);
754 GNUNET_free (psc->nsn->update);
755 GNUNET_free (psc->nsn);
762 * Publish an SBlock on GNUnet.
764 * @param h handle to the file sharing subsystem
765 * @param namespace namespace to publish in
766 * @param identifier identifier to use
767 * @param update update identifier to use
768 * @param meta metadata to use
769 * @param uri URI to refer to in the SBlock
770 * @param bo block options
771 * @param options publication options
772 * @param cont continuation
773 * @param cont_cls closure for cont
776 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
777 struct GNUNET_FS_Namespace *namespace,
778 const char *identifier,
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);
832 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
835 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
836 GNUNET_CONTAINER_meta_data_destroy (mmeta);
841 cont (cont_cls, NULL, _("Internal error."));
844 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
845 sb_enc = GNUNET_malloc (size);
846 GNUNET_CRYPTO_hash (identifier, idlen, &key);
847 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
848 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
850 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
851 GNUNET_CRYPTO_hash (&sb_enc->subspace,
852 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
853 &sks_uri->data.sks.namespace);
854 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
855 GNUNET_CRYPTO_hash_xor (&id,
856 &sks_uri->data.sks.namespace, &sb_enc->identifier);
857 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
858 GNUNET_CRYPTO_aes_encrypt (&sb[1],
859 size - sizeof (struct SBlock),
860 &sk, &iv, &sb_enc[1]);
861 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
862 sb_enc->purpose.size = htonl (slen + mdsize + nidlen
863 + sizeof (struct SBlock)
864 - sizeof (struct GNUNET_CRYPTO_RsaSignature));
865 GNUNET_assert (GNUNET_OK ==
866 GNUNET_CRYPTO_rsa_sign (namespace->key,
867 &sb_enc->purpose, &sb_enc->signature));
868 psc = GNUNET_malloc (sizeof (struct PublishSksContext));
871 psc->namespace = namespace;
873 psc->cont_cls = cont_cls;
874 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
876 GNUNET_free (sb_enc);
878 sb_put_cont (psc, GNUNET_OK, NULL);
881 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
882 if (NULL == psc->dsh)
884 GNUNET_free (sb_enc);
886 sb_put_cont (psc, GNUNET_NO, _("Failed to connect to datastore."));
889 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace, &id, &query);
892 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
893 psc->nsn->id = GNUNET_strdup (identifier);
894 psc->nsn->update = GNUNET_strdup (update);
895 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
896 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
898 GNUNET_DATASTORE_put (psc->dsh,
903 GNUNET_BLOCK_TYPE_FS_SBLOCK,
904 bo->content_priority,
906 bo->replication_level,
909 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
911 GNUNET_free (sb_enc);
916 * Closure for 'process_update_node'.
918 struct ProcessUpdateClosure
921 * Function to call for each node.
923 GNUNET_FS_IdentifierProcessor ip;
933 * Call the iterator in the closure for each node.
935 * @param cls closure (of type 'struct ProcessUpdateClosure *')
936 * @param key current key code
937 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
938 * @return GNUNET_YES if we should continue to
943 process_update_node (void *cls, const GNUNET_HashCode * key, void *value)
945 struct ProcessUpdateClosure *pc = cls;
946 struct NamespaceUpdateNode *nsn = value;
948 pc->ip (pc->ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
954 * Closure for 'find_trees'.
956 struct FindTreeClosure
959 * Namespace we are operating on.
961 struct GNUNET_FS_Namespace *namespace;
964 * Array with 'head's of TREEs.
966 struct NamespaceUpdateNode **tree_array;
969 * Size of 'tree_array'
971 unsigned int tree_array_size;
974 * Current generational ID used.
979 * Identifier for the current TREE, or UINT_MAX for none yet.
986 * Find all nodes reachable from the current node (including the
987 * current node itself). If they are in no tree, add them to the
988 * current one. If they are the head of another tree, merge the
989 * trees. If they are in the middle of another tree, let them be.
990 * We can tell that a node is already in an tree by checking if
991 * its 'nug' field is set to the current 'nug' value. It is the
992 * head of an tree if it is in the 'tree_array' under its respective
995 * In short, we're trying to find the smallest number of tree to
996 * cover a directed graph.
998 * @param cls closure (of type 'struct FindTreeClosure')
999 * @param key current key code
1000 * @param value value in the hash map
1001 * @return GNUNET_YES if we should continue to
1006 find_trees (void *cls, const GNUNET_HashCode * key, void *value)
1008 struct FindTreeClosure *fc = cls;
1009 struct NamespaceUpdateNode *nsn = value;
1012 if (nsn->nug == fc->nug)
1014 if (nsn->tree_id == UINT_MAX)
1015 return GNUNET_YES; /* circular */
1016 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
1017 if (fc->tree_array[nsn->tree_id] != nsn)
1018 return GNUNET_YES; /* part of "another" (directed) TREE,
1019 * and not root of it, end trace */
1020 if (nsn->tree_id == fc->id)
1021 return GNUNET_YES; /* that's our own root (can this be?) */
1022 /* merge existing TREE, we have a root for both */
1023 fc->tree_array[nsn->tree_id] = NULL;
1024 if (fc->id == UINT_MAX)
1025 fc->id = nsn->tree_id; /* take over ID */
1030 nsn->tree_id = UINT_MAX; /* mark as undef */
1032 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1033 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map,
1034 &hc, &find_trees, fc);
1041 * List all of the identifiers in the namespace for which we could
1042 * produce an update. Namespace updates form a graph where each node
1043 * has a name. Each node can have any number of URI/meta-data entries
1044 * which can each be linked to other nodes. Cycles are possible.
1046 * Calling this function with "next_id" NULL will cause the library to
1047 * call "ip" with a root for each strongly connected component of the
1048 * graph (a root being a node from which all other nodes in the Tree
1051 * Calling this function with "next_id" being the name of a node will
1052 * cause the library to call "ip" with all children of the node. Note
1053 * that cycles within the final tree are possible (including self-loops).
1054 * I know, odd definition of a tree, but the GUI will display an actual
1055 * tree (GtkTreeView), so that's what counts for the term here.
1057 * @param namespace namespace to inspect for updateable content
1058 * @param next_id ID to look for; use NULL to look for tree roots
1059 * @param ip function to call on each updateable identifier
1060 * @param ip_cls closure for ip
1063 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1064 const char *next_id,
1065 GNUNET_FS_IdentifierProcessor ip,
1071 struct NamespaceUpdateNode *nsn;
1072 struct ProcessUpdateClosure pc;
1073 struct FindTreeClosure fc;
1075 if (namespace->update_nodes == NULL)
1076 read_update_information_graph (namespace);
1077 if (namespace->update_nodes == NULL)
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "No updateable nodes found for ID `%s'\n", next_id);
1083 return; /* no nodes */
1085 if (namespace->update_map == NULL)
1087 /* need to construct */
1088 namespace->update_map =
1089 GNUNET_CONTAINER_multihashmap_create (2 +
1090 3 * namespace->update_node_count /
1092 for (i = 0; i < namespace->update_node_count; i++)
1094 nsn = namespace->update_nodes[i];
1095 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1096 GNUNET_CONTAINER_multihashmap_put (namespace->update_map,
1099 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1102 if (next_id != NULL)
1104 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
1107 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1108 &hc, &process_update_node, &pc);
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Calculating TREEs to find roots of update trees\n");
1115 /* Find heads of TREEs in update graph */
1116 nug = ++namespace->nug_gen;
1117 fc.tree_array = NULL;
1118 fc.tree_array_size = 0;
1120 for (i = 0; i < namespace->update_node_count; i++)
1122 nsn = namespace->update_nodes[i];
1123 if (nsn->nug == nug)
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "TREE of node `%s' is %u\n", nsn->id, nsn->nug);
1129 continue; /* already placed in TREE */
1131 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1135 fc.namespace = namespace;
1136 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1137 &hc, &find_trees, &fc);
1138 if (fc.id == UINT_MAX)
1140 /* start new TREE */
1141 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
1143 if (fc.tree_array[fc.id] == NULL)
1145 fc.tree_array[fc.id] = nsn;
1146 nsn->tree_id = fc.id;
1150 if (fc.id == fc.tree_array_size)
1152 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
1153 nsn->tree_id = fc.id;
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Starting new TREE %u with node `%s'\n",
1158 nsn->tree_id, nsn->id);
1160 /* put all nodes with same identifier into this TREE */
1161 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1162 fc.id = nsn->tree_id;
1164 fc.namespace = namespace;
1165 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1166 &hc, &find_trees, &fc);
1170 /* make head of TREE "id" */
1171 fc.tree_array[fc.id] = nsn;
1172 nsn->tree_id = fc.id;
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "TREE of node `%s' is %u\n", nsn->id, fc.id);
1179 for (i = 0; i < fc.tree_array_size; i++)
1181 nsn = fc.tree_array[i];
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Root of TREE %u is node `%s'\n", i, nsn->id);
1189 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
1192 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
1199 /* end of fs_namespace.c */