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 msg NULL on success, otherwise an error message
317 advertisement_cont (void *cls, int success, const char *msg)
319 struct AdvertisementContext *ac = cls;
322 GNUNET_HashCode query;
323 struct GNUNET_CRYPTO_AesSessionKey skey;
324 struct GNUNET_CRYPTO_AesInitializationVector iv;
325 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
327 if (GNUNET_OK != success)
330 GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
331 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
335 msg = _("Unknown error");
337 if (ac->cont != NULL)
338 ac->cont (ac->cont_cls, NULL, msg);
339 GNUNET_FS_uri_destroy (ac->ksk_uri);
340 GNUNET_free (ac->pt);
341 GNUNET_free (ac->nb);
342 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
346 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
349 GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
350 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
351 if (ac->cont != NULL)
352 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
353 GNUNET_FS_uri_destroy (ac->ksk_uri);
354 GNUNET_free (ac->pt);
355 GNUNET_free (ac->nb);
356 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
360 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
361 /* first character of keyword indicates if it is
362 * mandatory or not -- ignore for hashing */
363 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
364 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
365 GNUNET_CRYPTO_aes_encrypt (ac->pt, ac->pt_size, &skey, &iv, &ac->nb[1]);
366 GNUNET_break (GNUNET_OK ==
367 GNUNET_CRYPTO_rsa_sign (ac->ns->key, &ac->nb->ns_purpose,
368 &ac->nb->ns_signature));
369 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
370 GNUNET_assert (pk != NULL);
371 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
372 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
373 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
375 GNUNET_break (GNUNET_OK ==
376 GNUNET_CRYPTO_rsa_sign (pk, &ac->nb->ksk_purpose,
377 &ac->nb->ksk_signature));
378 GNUNET_CRYPTO_rsa_key_free (pk);
379 GNUNET_DATASTORE_put (ac->dsh, 0 /* no reservation */ ,
380 &query, ac->pt_size + sizeof (struct NBlock), ac->nb,
381 GNUNET_BLOCK_TYPE_FS_NBLOCK, ac->bo.content_priority,
382 ac->bo.anonymity_level, ac->bo.replication_level,
383 ac->bo.expiration_time, -2, 1,
384 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &advertisement_cont,
390 * Publish an advertismement for a namespace.
392 * @param h handle to the file sharing subsystem
393 * @param ksk_uri keywords to use for advertisment
394 * @param namespace handle for the namespace that should be advertised
395 * @param meta meta-data for the namespace advertisement
396 * @param bo block options
397 * @param rootEntry name of the root of the namespace
398 * @param cont continuation
399 * @param cont_cls closure for cont
402 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
403 struct GNUNET_FS_Uri *ksk_uri,
404 struct GNUNET_FS_Namespace *namespace,
405 const struct GNUNET_CONTAINER_MetaData *meta,
406 const struct GNUNET_FS_BlockOptions *bo,
407 const char *rootEntry,
408 GNUNET_FS_PublishContinuation cont,
416 struct GNUNET_DATASTORE_Handle *dsh;
417 struct AdvertisementContext *ctx;
420 /* create advertisements */
421 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
424 cont (cont_cls, NULL, _("Failed to serialize meta data"));
427 reslen = strlen (rootEntry) + 1;
428 size = mdsize + sizeof (struct NBlock) + reslen;
429 if (size > MAX_NBLOCK_SIZE)
431 size = MAX_NBLOCK_SIZE;
432 mdsize = size - sizeof (struct NBlock) - reslen;
435 pt = GNUNET_malloc (mdsize + reslen);
436 memcpy (pt, rootEntry, reslen);
439 GNUNET_CONTAINER_meta_data_serialize (meta, &mdst, mdsize,
440 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
445 cont (cont_cls, NULL, _("Failed to serialize meta data"));
448 size = mdsize + sizeof (struct NBlock) + reslen;
449 nb = GNUNET_malloc (size);
450 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &nb->subspace);
451 nb->ns_purpose.size =
452 htonl (mdsize + reslen +
453 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
454 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
455 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
456 nb->ksk_purpose.size =
457 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
458 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
459 dsh = GNUNET_DATASTORE_connect (h->cfg);
464 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
467 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
469 ctx->cont_cls = cont_cls;
471 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
474 ctx->pt_size = mdsize + reslen;
478 advertisement_cont (ctx, GNUNET_OK, NULL);
483 * Create a namespace with the given name; if one already
484 * exists, return a handle to the existing namespace.
486 * @param h handle to the file sharing subsystem
487 * @param name name to use for the namespace
488 * @return handle to the namespace, NULL on error
490 struct GNUNET_FS_Namespace *
491 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
495 struct GNUNET_FS_Namespace *ret;
497 dn = get_namespace_directory (h);
498 GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
500 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
503 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
504 if (ret->key == NULL)
506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
507 _("Failed to create or read private key for namespace `%s'\n"),
513 ret->name = GNUNET_strdup (name);
520 * Delete a namespace handle. Can be used for a clean shutdown (free
521 * memory) or also to freeze the namespace to prevent further
522 * insertions by anyone.
524 * @param namespace handle to the namespace that should be deleted / freed
525 * @param freeze prevents future insertions; creating a namespace
526 * with the same name again will create a fresh namespace instead
528 * @return GNUNET_OK on success, GNUNET_SYSERR on error
531 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace, int freeze)
534 struct NamespaceUpdateNode *nsn;
539 if (0 != UNLINK (namespace->filename))
540 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
541 namespace->filename);
543 if (0 == namespace->rc)
545 GNUNET_CRYPTO_rsa_key_free (namespace->key);
546 GNUNET_free (namespace->filename);
547 GNUNET_free (namespace->name);
548 for (i = 0; i < namespace->update_node_count; i++)
550 nsn = namespace->update_nodes[i];
551 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
552 GNUNET_FS_uri_destroy (nsn->uri);
553 GNUNET_free (nsn->id);
554 GNUNET_free (nsn->update);
557 GNUNET_array_grow (namespace->update_nodes, namespace->update_node_count,
559 if (namespace->update_map != NULL)
560 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
561 GNUNET_free (namespace);
568 * Context for the 'process_namespace' callback.
569 * Specifies a function to call on each namespace.
571 struct ProcessNamespaceContext
576 GNUNET_FS_NamespaceInfoProcessor cb;
586 * Function called with a filename of a namespace. Reads the key and
587 * calls the callback.
589 * @param cls closure (struct ProcessNamespaceContext)
590 * @param filename complete filename (absolute path)
591 * @return GNUNET_OK to continue to iterate,
592 * GNUNET_SYSERR to abort iteration with error!
595 process_namespace (void *cls, const char *filename)
597 struct ProcessNamespaceContext *pnc = cls;
598 struct GNUNET_CRYPTO_RsaPrivateKey *key;
599 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
604 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 ("Failed to read namespace private key file `%s', deleting it!\n"),
611 if (0 != UNLINK (filename))
612 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
615 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
616 GNUNET_CRYPTO_rsa_key_free (key);
617 GNUNET_CRYPTO_hash (&pk, sizeof (pk), &id);
619 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
621 pnc->cb (pnc->cb_cls, name, &id);
627 * Build a list of all available local (!) namespaces The returned
628 * names are only the nicknames since we only iterate over the local
631 * @param h handle to the file sharing subsystem
632 * @param cb function to call on each known namespace
633 * @param cb_cls closure for cb
636 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
637 GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls)
640 struct ProcessNamespaceContext ctx;
642 dn = get_namespace_directory (h);
647 GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx);
655 * Context for the SKS publication.
657 struct PublishSksContext
661 * URI of the new entry in the namespace.
663 struct GNUNET_FS_Uri *uri;
666 * Namespace update node to add to namespace on success (or to be
667 * deleted if publishing failed).
669 struct NamespaceUpdateNode *nsn;
672 * Namespace we're publishing to.
674 struct GNUNET_FS_Namespace *namespace;
677 * Handle to the datastore.
679 struct GNUNET_DATASTORE_Handle *dsh;
682 * Function to call once we're done.
684 GNUNET_FS_PublishContinuation cont;
695 * Function called by the datastore API with
696 * the result from the PUT (SBlock) request.
698 * @param cls closure of type "struct PublishSksContext*"
699 * @param success GNUNET_OK on success
700 * @param msg error message (or NULL)
703 sb_put_cont (void *cls, int success, const char *msg)
705 struct PublishSksContext *psc = cls;
708 if (NULL != psc->dsh)
710 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
713 if (GNUNET_OK != success)
715 if (psc->cont != NULL)
716 psc->cont (psc->cont_cls, NULL, msg);
720 if (psc->nsn != NULL)
722 /* FIXME: this can be done much more
723 * efficiently by simply appending to the
724 * file and overwriting the 4-byte header */
725 if (psc->namespace->update_nodes == NULL)
726 read_update_information_graph (psc->namespace);
727 GNUNET_array_append (psc->namespace->update_nodes,
728 psc->namespace->update_node_count, psc->nsn);
729 if (psc->namespace->update_map != NULL)
731 GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
732 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map, &hc,
734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
737 write_update_information_graph (psc->namespace);
739 if (psc->cont != NULL)
740 psc->cont (psc->cont_cls, psc->uri, NULL);
742 GNUNET_FS_namespace_delete (psc->namespace, GNUNET_NO);
743 GNUNET_FS_uri_destroy (psc->uri);
744 if (psc->nsn != NULL)
746 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
747 GNUNET_FS_uri_destroy (psc->nsn->uri);
748 GNUNET_free (psc->nsn->id);
749 GNUNET_free (psc->nsn->update);
750 GNUNET_free (psc->nsn);
757 * Publish an SBlock on GNUnet.
759 * @param h handle to the file sharing subsystem
760 * @param namespace namespace to publish in
761 * @param identifier identifier to use
762 * @param update update identifier to use
763 * @param meta metadata to use
764 * @param uri URI to refer to in the SBlock
765 * @param bo block options
766 * @param options publication options
767 * @param cont continuation
768 * @param cont_cls closure for cont
771 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
772 struct GNUNET_FS_Namespace *namespace,
773 const char *identifier, const char *update,
774 const struct GNUNET_CONTAINER_MetaData *meta,
775 const struct GNUNET_FS_Uri *uri,
776 const struct GNUNET_FS_BlockOptions *bo,
777 enum GNUNET_FS_PublishOptions options,
778 GNUNET_FS_PublishContinuation cont, void *cont_cls)
780 struct PublishSksContext *psc;
781 struct GNUNET_CRYPTO_AesSessionKey sk;
782 struct GNUNET_CRYPTO_AesInitializationVector iv;
783 struct GNUNET_FS_Uri *sks_uri;
791 struct SBlock *sb_enc;
793 struct GNUNET_CONTAINER_MetaData *mmeta;
794 GNUNET_HashCode key; /* hash of thisId = key */
795 GNUNET_HashCode id; /* hash of hc = identifier */
796 GNUNET_HashCode query; /* id ^ nsid = DB query */
799 mmeta = GNUNET_CONTAINER_meta_data_create ();
801 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
802 uris = GNUNET_FS_uri_to_string (uri);
803 slen = strlen (uris) + 1;
804 idlen = strlen (identifier);
806 nidlen = strlen (update) + 1;
809 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
810 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
811 if (size > MAX_SBLOCK_SIZE)
813 size = MAX_SBLOCK_SIZE;
814 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
816 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
817 dest = (char *) &sb[1];
819 memcpy (dest, update, nidlen);
823 memcpy (dest, uris, slen);
827 GNUNET_CONTAINER_meta_data_serialize (mmeta, &dest, mdsize,
828 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
829 GNUNET_CONTAINER_meta_data_destroy (mmeta);
834 cont (cont_cls, NULL, _("Internal error."));
837 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
838 sb_enc = GNUNET_malloc (size);
839 GNUNET_CRYPTO_hash (identifier, idlen, &key);
840 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
841 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
843 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
844 GNUNET_CRYPTO_hash (&sb_enc->subspace,
845 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
846 &sks_uri->data.sks.namespace);
847 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
848 GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.namespace,
849 &sb_enc->identifier);
850 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
851 GNUNET_CRYPTO_aes_encrypt (&sb[1], size - sizeof (struct SBlock), &sk, &iv,
853 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
854 sb_enc->purpose.size =
855 htonl (slen + mdsize + nidlen + sizeof (struct SBlock) -
856 sizeof (struct GNUNET_CRYPTO_RsaSignature));
857 GNUNET_assert (GNUNET_OK ==
858 GNUNET_CRYPTO_rsa_sign (namespace->key, &sb_enc->purpose,
859 &sb_enc->signature));
860 psc = GNUNET_malloc (sizeof (struct PublishSksContext));
863 psc->namespace = namespace;
865 psc->cont_cls = cont_cls;
866 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
868 GNUNET_free (sb_enc);
870 sb_put_cont (psc, GNUNET_OK, NULL);
873 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
874 if (NULL == psc->dsh)
876 GNUNET_free (sb_enc);
878 sb_put_cont (psc, GNUNET_NO, _("Failed to connect to datastore."));
881 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace, &id, &query);
884 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
885 psc->nsn->id = GNUNET_strdup (identifier);
886 psc->nsn->update = GNUNET_strdup (update);
887 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
888 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
890 GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc,
891 GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority,
892 bo->anonymity_level, bo->replication_level,
893 bo->expiration_time, -2, 1,
894 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
896 GNUNET_free (sb_enc);
901 * Closure for 'process_update_node'.
903 struct ProcessUpdateClosure
906 * Function to call for each node.
908 GNUNET_FS_IdentifierProcessor ip;
918 * Call the iterator in the closure for each node.
920 * @param cls closure (of type 'struct ProcessUpdateClosure *')
921 * @param key current key code
922 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
923 * @return GNUNET_YES if we should continue to
928 process_update_node (void *cls, const GNUNET_HashCode * key, void *value)
930 struct ProcessUpdateClosure *pc = cls;
931 struct NamespaceUpdateNode *nsn = value;
933 pc->ip (pc->ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
939 * Closure for 'find_trees'.
941 struct FindTreeClosure
944 * Namespace we are operating on.
946 struct GNUNET_FS_Namespace *namespace;
949 * Array with 'head's of TREEs.
951 struct NamespaceUpdateNode **tree_array;
954 * Size of 'tree_array'
956 unsigned int tree_array_size;
959 * Current generational ID used.
964 * Identifier for the current TREE, or UINT_MAX for none yet.
971 * Find all nodes reachable from the current node (including the
972 * current node itself). If they are in no tree, add them to the
973 * current one. If they are the head of another tree, merge the
974 * trees. If they are in the middle of another tree, let them be.
975 * We can tell that a node is already in an tree by checking if
976 * its 'nug' field is set to the current 'nug' value. It is the
977 * head of an tree if it is in the 'tree_array' under its respective
980 * In short, we're trying to find the smallest number of tree to
981 * cover a directed graph.
983 * @param cls closure (of type 'struct FindTreeClosure')
984 * @param key current key code
985 * @param value value in the hash map
986 * @return GNUNET_YES if we should continue to
991 find_trees (void *cls, const GNUNET_HashCode * key, void *value)
993 struct FindTreeClosure *fc = cls;
994 struct NamespaceUpdateNode *nsn = value;
997 if (nsn->nug == fc->nug)
999 if (nsn->tree_id == UINT_MAX)
1000 return GNUNET_YES; /* circular */
1001 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
1002 if (fc->tree_array[nsn->tree_id] != nsn)
1003 return GNUNET_YES; /* part of "another" (directed) TREE,
1004 * and not root of it, end trace */
1005 if (nsn->tree_id == fc->id)
1006 return GNUNET_YES; /* that's our own root (can this be?) */
1007 /* merge existing TREE, we have a root for both */
1008 fc->tree_array[nsn->tree_id] = NULL;
1009 if (fc->id == UINT_MAX)
1010 fc->id = nsn->tree_id; /* take over ID */
1015 nsn->tree_id = UINT_MAX; /* mark as undef */
1017 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1018 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map, &hc,
1026 * List all of the identifiers in the namespace for which we could
1027 * produce an update. Namespace updates form a graph where each node
1028 * has a name. Each node can have any number of URI/meta-data entries
1029 * which can each be linked to other nodes. Cycles are possible.
1031 * Calling this function with "next_id" NULL will cause the library to
1032 * call "ip" with a root for each strongly connected component of the
1033 * graph (a root being a node from which all other nodes in the Tree
1036 * Calling this function with "next_id" being the name of a node will
1037 * cause the library to call "ip" with all children of the node. Note
1038 * that cycles within the final tree are possible (including self-loops).
1039 * I know, odd definition of a tree, but the GUI will display an actual
1040 * tree (GtkTreeView), so that's what counts for the term here.
1042 * @param namespace namespace to inspect for updateable content
1043 * @param next_id ID to look for; use NULL to look for tree roots
1044 * @param ip function to call on each updateable identifier
1045 * @param ip_cls closure for ip
1048 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1049 const char *next_id,
1050 GNUNET_FS_IdentifierProcessor ip,
1056 struct NamespaceUpdateNode *nsn;
1057 struct ProcessUpdateClosure pc;
1058 struct FindTreeClosure fc;
1060 if (namespace->update_nodes == NULL)
1061 read_update_information_graph (namespace);
1062 if (namespace->update_nodes == NULL)
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "No updateable nodes found for ID `%s'\n", next_id);
1068 return; /* no nodes */
1070 if (namespace->update_map == NULL)
1072 /* need to construct */
1073 namespace->update_map =
1074 GNUNET_CONTAINER_multihashmap_create (2 +
1075 3 * namespace->update_node_count /
1077 for (i = 0; i < namespace->update_node_count; i++)
1079 nsn = namespace->update_nodes[i];
1080 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1081 GNUNET_CONTAINER_multihashmap_put (namespace->update_map, &hc, nsn,
1082 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1085 if (next_id != NULL)
1087 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
1090 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1091 &process_update_node, &pc);
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 "Calculating TREEs to find roots of update trees\n");
1098 /* Find heads of TREEs in update graph */
1099 nug = ++namespace->nug_gen;
1100 fc.tree_array = NULL;
1101 fc.tree_array_size = 0;
1103 for (i = 0; i < namespace->update_node_count; i++)
1105 nsn = namespace->update_nodes[i];
1106 if (nsn->nug == nug)
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1112 continue; /* already placed in TREE */
1114 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1116 nsn->tree_id = UINT_MAX;
1119 fc.namespace = namespace;
1120 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1122 if (fc.id == UINT_MAX)
1124 /* start new TREE */
1125 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
1127 if (fc.tree_array[fc.id] == NULL)
1129 fc.tree_array[fc.id] = nsn;
1130 nsn->tree_id = fc.id;
1134 if (fc.id == fc.tree_array_size)
1136 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
1137 nsn->tree_id = fc.id;
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "Starting new TREE %u with node `%s'\n", nsn->tree_id,
1144 /* put all nodes with same identifier into this TREE */
1145 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1146 fc.id = nsn->tree_id;
1148 fc.namespace = namespace;
1149 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1154 /* make head of TREE "id" */
1155 fc.tree_array[fc.id] = nsn;
1156 nsn->tree_id = fc.id;
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1163 for (i = 0; i < fc.tree_array_size; i++)
1165 nsn = fc.tree_array[i];
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
1173 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
1176 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
1183 /* end of fs_namespace.c */