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
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, "FS", "IDENTITY_DIR",
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, "FS", "UPDATE_DIR",
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);
117 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) ||
118 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) ||
119 (GNUNET_OK != GNUNET_BIO_write_string (wh, n->update)) ||
120 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
128 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"),
136 * Read the namespace update node graph from a file.
138 * @param ns namespace to read
141 read_update_information_graph (struct GNUNET_FS_Namespace *ns)
144 struct GNUNET_BIO_ReadHandle *rh;
146 struct NamespaceUpdateNode *n;
151 fn = get_update_information_directory (ns);
152 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
157 rh = GNUNET_BIO_read_open (fn);
163 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
168 if (count > 1024 * 1024)
175 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
180 GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
182 for (i = 0; i < count; i++)
184 n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
185 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024))
186 || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
188 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
189 (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
192 GNUNET_free_non_null (n->id);
193 GNUNET_free_non_null (n->update);
195 GNUNET_CONTAINER_meta_data_destroy (n->md);
199 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
206 GNUNET_free_non_null (n->update);
207 GNUNET_CONTAINER_meta_data_destroy (n->md);
211 ns->update_nodes[i] = n;
213 ns->update_node_count = i;
215 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"), emsg);
225 * Context for advertising a namespace.
227 struct AdvertisementContext
230 * Function to call with the result.
232 GNUNET_FS_PublishContinuation cont;
242 struct GNUNET_DATASTORE_Handle *dsh;
247 struct GNUNET_FS_Uri *ksk_uri;
255 * NBlock to sign and store.
262 struct GNUNET_FS_Namespace *ns;
267 struct GNUNET_FS_BlockOptions bo;
270 * Number of bytes of plaintext.
275 * Current keyword offset.
282 * Disconnect from the datastore.
284 * @param cls datastore handle
285 * @param tc scheduler context
288 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct GNUNET_DATASTORE_Handle *dsh = cls;
292 GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
297 * Continuation called to notify client about result of the
300 * @param cls closure (our struct AdvertismentContext)
301 * @param success GNUNET_SYSERR on failure
302 * @param msg NULL on success, otherwise an error message
305 advertisement_cont (void *cls, int success, const char *msg)
307 struct AdvertisementContext *ac = cls;
310 GNUNET_HashCode query;
311 struct GNUNET_CRYPTO_AesSessionKey skey;
312 struct GNUNET_CRYPTO_AesInitializationVector iv;
313 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
315 if (GNUNET_OK != success)
318 GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
319 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
323 msg = _("Unknown error");
325 if (ac->cont != NULL)
326 ac->cont (ac->cont_cls, NULL, msg);
327 GNUNET_FS_uri_destroy (ac->ksk_uri);
328 GNUNET_free (ac->pt);
329 GNUNET_free (ac->nb);
330 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
334 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
337 GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
338 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
339 if (ac->cont != NULL)
340 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
341 GNUNET_FS_uri_destroy (ac->ksk_uri);
342 GNUNET_free (ac->pt);
343 GNUNET_free (ac->nb);
344 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
348 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
349 /* first character of keyword indicates if it is
350 * mandatory or not -- ignore for hashing */
351 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
352 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
353 GNUNET_CRYPTO_aes_encrypt (ac->pt, ac->pt_size, &skey, &iv, &ac->nb[1]);
354 GNUNET_break (GNUNET_OK ==
355 GNUNET_CRYPTO_rsa_sign (ac->ns->key, &ac->nb->ns_purpose,
356 &ac->nb->ns_signature));
357 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
358 GNUNET_assert (pk != NULL);
359 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
360 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
361 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
363 GNUNET_break (GNUNET_OK ==
364 GNUNET_CRYPTO_rsa_sign (pk, &ac->nb->ksk_purpose,
365 &ac->nb->ksk_signature));
366 GNUNET_CRYPTO_rsa_key_free (pk);
367 GNUNET_DATASTORE_put (ac->dsh, 0 /* no reservation */ ,
368 &query, ac->pt_size + sizeof (struct NBlock), ac->nb,
369 GNUNET_BLOCK_TYPE_FS_NBLOCK, ac->bo.content_priority,
370 ac->bo.anonymity_level, ac->bo.replication_level,
371 ac->bo.expiration_time, -2, 1,
372 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &advertisement_cont,
378 * Publish an advertismement for a namespace.
380 * @param h handle to the file sharing subsystem
381 * @param ksk_uri keywords to use for advertisment
382 * @param namespace handle for the namespace that should be advertised
383 * @param meta meta-data for the namespace advertisement
384 * @param bo block options
385 * @param rootEntry name of the root of the namespace
386 * @param cont continuation
387 * @param cont_cls closure for cont
390 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
391 struct GNUNET_FS_Uri *ksk_uri,
392 struct GNUNET_FS_Namespace *namespace,
393 const struct GNUNET_CONTAINER_MetaData *meta,
394 const struct GNUNET_FS_BlockOptions *bo,
395 const char *rootEntry,
396 GNUNET_FS_PublishContinuation cont,
404 struct GNUNET_DATASTORE_Handle *dsh;
405 struct AdvertisementContext *ctx;
408 /* create advertisements */
409 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
412 cont (cont_cls, NULL, _("Failed to serialize meta data"));
415 reslen = strlen (rootEntry) + 1;
416 size = mdsize + sizeof (struct NBlock) + reslen;
417 if (size > MAX_NBLOCK_SIZE)
419 size = MAX_NBLOCK_SIZE;
420 mdsize = size - sizeof (struct NBlock) - reslen;
423 pt = GNUNET_malloc (mdsize + reslen);
424 memcpy (pt, rootEntry, reslen);
427 GNUNET_CONTAINER_meta_data_serialize (meta, &mdst, mdsize,
428 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
433 cont (cont_cls, NULL, _("Failed to serialize meta data"));
436 size = mdsize + sizeof (struct NBlock) + reslen;
437 nb = GNUNET_malloc (size);
438 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &nb->subspace);
439 nb->ns_purpose.size =
440 htonl (mdsize + reslen +
441 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
442 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
443 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
444 nb->ksk_purpose.size =
445 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
446 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
447 dsh = GNUNET_DATASTORE_connect (h->cfg);
452 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
455 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
457 ctx->cont_cls = cont_cls;
459 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
462 ctx->pt_size = mdsize + reslen;
466 advertisement_cont (ctx, GNUNET_OK, NULL);
471 * Create a namespace with the given name; if one already
472 * exists, return a handle to the existing namespace.
474 * @param h handle to the file sharing subsystem
475 * @param name name to use for the namespace
476 * @return handle to the namespace, NULL on error
478 struct GNUNET_FS_Namespace *
479 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
483 struct GNUNET_FS_Namespace *ret;
485 dn = get_namespace_directory (h);
486 GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
488 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
491 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
492 if (ret->key == NULL)
494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
495 _("Failed to create or read private key for namespace `%s'\n"),
501 ret->name = GNUNET_strdup (name);
508 * Delete a namespace handle. Can be used for a clean shutdown (free
509 * memory) or also to freeze the namespace to prevent further
510 * insertions by anyone.
512 * @param namespace handle to the namespace that should be deleted / freed
513 * @param freeze prevents future insertions; creating a namespace
514 * with the same name again will create a fresh namespace instead
516 * @return GNUNET_OK on success, GNUNET_SYSERR on error
519 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace, int freeze)
522 struct NamespaceUpdateNode *nsn;
527 if (0 != UNLINK (namespace->filename))
528 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
529 namespace->filename);
531 if (0 == namespace->rc)
533 GNUNET_CRYPTO_rsa_key_free (namespace->key);
534 GNUNET_free (namespace->filename);
535 GNUNET_free (namespace->name);
536 for (i = 0; i < namespace->update_node_count; i++)
538 nsn = namespace->update_nodes[i];
539 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
540 GNUNET_FS_uri_destroy (nsn->uri);
541 GNUNET_free (nsn->id);
542 GNUNET_free (nsn->update);
545 GNUNET_array_grow (namespace->update_nodes, namespace->update_node_count,
547 if (namespace->update_map != NULL)
548 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
549 GNUNET_free (namespace);
556 * Context for the 'process_namespace' callback.
557 * Specifies a function to call on each namespace.
559 struct ProcessNamespaceContext
564 GNUNET_FS_NamespaceInfoProcessor cb;
574 * Function called with a filename of a namespace. Reads the key and
575 * calls the callback.
577 * @param cls closure (struct ProcessNamespaceContext)
578 * @param filename complete filename (absolute path)
579 * @return GNUNET_OK to continue to iterate,
580 * GNUNET_SYSERR to abort iteration with error!
583 process_namespace (void *cls, const char *filename)
585 struct ProcessNamespaceContext *pnc = cls;
586 struct GNUNET_CRYPTO_RsaPrivateKey *key;
587 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
592 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
597 ("Failed to read namespace private key file `%s', deleting it!\n"),
599 if (0 != UNLINK (filename))
600 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
603 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
604 GNUNET_CRYPTO_rsa_key_free (key);
605 GNUNET_CRYPTO_hash (&pk, sizeof (pk), &id);
607 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
609 pnc->cb (pnc->cb_cls, name, &id);
615 * Build a list of all available local (!) namespaces The returned
616 * names are only the nicknames since we only iterate over the local
619 * @param h handle to the file sharing subsystem
620 * @param cb function to call on each known namespace
621 * @param cb_cls closure for cb
624 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
625 GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls)
628 struct ProcessNamespaceContext ctx;
630 dn = get_namespace_directory (h);
635 GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx);
643 * Context for the SKS publication.
645 struct PublishSksContext
649 * URI of the new entry in the namespace.
651 struct GNUNET_FS_Uri *uri;
654 * Namespace update node to add to namespace on success (or to be
655 * deleted if publishing failed).
657 struct NamespaceUpdateNode *nsn;
660 * Namespace we're publishing to.
662 struct GNUNET_FS_Namespace *namespace;
665 * Handle to the datastore.
667 struct GNUNET_DATASTORE_Handle *dsh;
670 * Function to call once we're done.
672 GNUNET_FS_PublishContinuation cont;
683 * Function called by the datastore API with
684 * the result from the PUT (SBlock) request.
686 * @param cls closure of type "struct PublishSksContext*"
687 * @param success GNUNET_OK on success
688 * @param msg error message (or NULL)
691 sb_put_cont (void *cls, int success, const char *msg)
693 struct PublishSksContext *psc = cls;
696 if (NULL != psc->dsh)
698 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
701 if (GNUNET_OK != success)
703 if (psc->cont != NULL)
704 psc->cont (psc->cont_cls, NULL, msg);
708 if (psc->nsn != NULL)
710 /* FIXME: this can be done much more
711 * efficiently by simply appending to the
712 * file and overwriting the 4-byte header */
713 if (psc->namespace->update_nodes == NULL)
714 read_update_information_graph (psc->namespace);
715 GNUNET_array_append (psc->namespace->update_nodes,
716 psc->namespace->update_node_count, psc->nsn);
717 if (psc->namespace->update_map != NULL)
719 GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
720 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map, &hc,
722 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
725 write_update_information_graph (psc->namespace);
727 if (psc->cont != NULL)
728 psc->cont (psc->cont_cls, psc->uri, NULL);
730 GNUNET_FS_namespace_delete (psc->namespace, GNUNET_NO);
731 GNUNET_FS_uri_destroy (psc->uri);
732 if (psc->nsn != NULL)
734 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
735 GNUNET_FS_uri_destroy (psc->nsn->uri);
736 GNUNET_free (psc->nsn->id);
737 GNUNET_free (psc->nsn->update);
738 GNUNET_free (psc->nsn);
745 * Publish an SBlock on GNUnet.
747 * @param h handle to the file sharing subsystem
748 * @param namespace namespace to publish in
749 * @param identifier identifier to use
750 * @param update update identifier to use
751 * @param meta metadata to use
752 * @param uri URI to refer to in the SBlock
753 * @param bo block options
754 * @param options publication options
755 * @param cont continuation
756 * @param cont_cls closure for cont
759 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
760 struct GNUNET_FS_Namespace *namespace,
761 const char *identifier, const char *update,
762 const struct GNUNET_CONTAINER_MetaData *meta,
763 const struct GNUNET_FS_Uri *uri,
764 const struct GNUNET_FS_BlockOptions *bo,
765 enum GNUNET_FS_PublishOptions options,
766 GNUNET_FS_PublishContinuation cont, void *cont_cls)
768 struct PublishSksContext *psc;
769 struct GNUNET_CRYPTO_AesSessionKey sk;
770 struct GNUNET_CRYPTO_AesInitializationVector iv;
771 struct GNUNET_FS_Uri *sks_uri;
779 struct SBlock *sb_enc;
781 struct GNUNET_CONTAINER_MetaData *mmeta;
782 GNUNET_HashCode key; /* hash of thisId = key */
783 GNUNET_HashCode id; /* hash of hc = identifier */
784 GNUNET_HashCode query; /* id ^ nsid = DB query */
787 mmeta = GNUNET_CONTAINER_meta_data_create ();
789 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
790 uris = GNUNET_FS_uri_to_string (uri);
791 slen = strlen (uris) + 1;
792 idlen = strlen (identifier);
794 nidlen = strlen (update) + 1;
797 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
798 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
799 if (size > MAX_SBLOCK_SIZE)
801 size = MAX_SBLOCK_SIZE;
802 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
804 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
805 dest = (char *) &sb[1];
807 memcpy (dest, update, nidlen);
811 memcpy (dest, uris, slen);
815 GNUNET_CONTAINER_meta_data_serialize (mmeta, &dest, mdsize,
816 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
817 GNUNET_CONTAINER_meta_data_destroy (mmeta);
822 cont (cont_cls, NULL, _("Internal error."));
825 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
826 sb_enc = GNUNET_malloc (size);
827 GNUNET_CRYPTO_hash (identifier, idlen, &key);
828 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
829 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
831 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
832 GNUNET_CRYPTO_hash (&sb_enc->subspace,
833 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
834 &sks_uri->data.sks.namespace);
835 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
836 GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.namespace,
837 &sb_enc->identifier);
838 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
839 GNUNET_CRYPTO_aes_encrypt (&sb[1], size - sizeof (struct SBlock), &sk, &iv,
841 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
842 sb_enc->purpose.size =
843 htonl (slen + mdsize + nidlen + sizeof (struct SBlock) -
844 sizeof (struct GNUNET_CRYPTO_RsaSignature));
845 GNUNET_assert (GNUNET_OK ==
846 GNUNET_CRYPTO_rsa_sign (namespace->key, &sb_enc->purpose,
847 &sb_enc->signature));
848 psc = GNUNET_malloc (sizeof (struct PublishSksContext));
851 psc->namespace = namespace;
853 psc->cont_cls = cont_cls;
854 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
856 GNUNET_free (sb_enc);
858 sb_put_cont (psc, GNUNET_OK, NULL);
861 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
862 if (NULL == psc->dsh)
864 GNUNET_free (sb_enc);
866 sb_put_cont (psc, GNUNET_NO, _("Failed to connect to datastore."));
869 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace, &id, &query);
872 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
873 psc->nsn->id = GNUNET_strdup (identifier);
874 psc->nsn->update = GNUNET_strdup (update);
875 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
876 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
878 GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc,
879 GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority,
880 bo->anonymity_level, bo->replication_level,
881 bo->expiration_time, -2, 1,
882 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
884 GNUNET_free (sb_enc);
889 * Closure for 'process_update_node'.
891 struct ProcessUpdateClosure
894 * Function to call for each node.
896 GNUNET_FS_IdentifierProcessor ip;
906 * Call the iterator in the closure for each node.
908 * @param cls closure (of type 'struct ProcessUpdateClosure *')
909 * @param key current key code
910 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
911 * @return GNUNET_YES if we should continue to
916 process_update_node (void *cls, const GNUNET_HashCode * key, void *value)
918 struct ProcessUpdateClosure *pc = cls;
919 struct NamespaceUpdateNode *nsn = value;
921 pc->ip (pc->ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
927 * Closure for 'find_trees'.
929 struct FindTreeClosure
932 * Namespace we are operating on.
934 struct GNUNET_FS_Namespace *namespace;
937 * Array with 'head's of TREEs.
939 struct NamespaceUpdateNode **tree_array;
942 * Size of 'tree_array'
944 unsigned int tree_array_size;
947 * Current generational ID used.
952 * Identifier for the current TREE, or UINT_MAX for none yet.
959 * Find all nodes reachable from the current node (including the
960 * current node itself). If they are in no tree, add them to the
961 * current one. If they are the head of another tree, merge the
962 * trees. If they are in the middle of another tree, let them be.
963 * We can tell that a node is already in an tree by checking if
964 * its 'nug' field is set to the current 'nug' value. It is the
965 * head of an tree if it is in the 'tree_array' under its respective
968 * In short, we're trying to find the smallest number of tree to
969 * cover a directed graph.
971 * @param cls closure (of type 'struct FindTreeClosure')
972 * @param key current key code
973 * @param value value in the hash map
974 * @return GNUNET_YES if we should continue to
979 find_trees (void *cls, const GNUNET_HashCode * key, void *value)
981 struct FindTreeClosure *fc = cls;
982 struct NamespaceUpdateNode *nsn = value;
985 if (nsn->nug == fc->nug)
987 if (nsn->tree_id == UINT_MAX)
988 return GNUNET_YES; /* circular */
989 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
990 if (fc->tree_array[nsn->tree_id] != nsn)
991 return GNUNET_YES; /* part of "another" (directed) TREE,
992 * and not root of it, end trace */
993 if (nsn->tree_id == fc->id)
994 return GNUNET_YES; /* that's our own root (can this be?) */
995 /* merge existing TREE, we have a root for both */
996 fc->tree_array[nsn->tree_id] = NULL;
997 if (fc->id == UINT_MAX)
998 fc->id = nsn->tree_id; /* take over ID */
1003 nsn->tree_id = UINT_MAX; /* mark as undef */
1005 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1006 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map, &hc,
1014 * List all of the identifiers in the namespace for which we could
1015 * produce an update. Namespace updates form a graph where each node
1016 * has a name. Each node can have any number of URI/meta-data entries
1017 * which can each be linked to other nodes. Cycles are possible.
1019 * Calling this function with "next_id" NULL will cause the library to
1020 * call "ip" with a root for each strongly connected component of the
1021 * graph (a root being a node from which all other nodes in the Tree
1024 * Calling this function with "next_id" being the name of a node will
1025 * cause the library to call "ip" with all children of the node. Note
1026 * that cycles within the final tree are possible (including self-loops).
1027 * I know, odd definition of a tree, but the GUI will display an actual
1028 * tree (GtkTreeView), so that's what counts for the term here.
1030 * @param namespace namespace to inspect for updateable content
1031 * @param next_id ID to look for; use NULL to look for tree roots
1032 * @param ip function to call on each updateable identifier
1033 * @param ip_cls closure for ip
1036 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1037 const char *next_id,
1038 GNUNET_FS_IdentifierProcessor ip,
1044 struct NamespaceUpdateNode *nsn;
1045 struct ProcessUpdateClosure pc;
1046 struct FindTreeClosure fc;
1048 if (namespace->update_nodes == NULL)
1049 read_update_information_graph (namespace);
1050 if (namespace->update_nodes == NULL)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "No updateable nodes found for ID `%s'\n", next_id);
1056 return; /* no nodes */
1058 if (namespace->update_map == NULL)
1060 /* need to construct */
1061 namespace->update_map =
1062 GNUNET_CONTAINER_multihashmap_create (2 +
1063 3 * namespace->update_node_count /
1065 for (i = 0; i < namespace->update_node_count; i++)
1067 nsn = namespace->update_nodes[i];
1068 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1069 GNUNET_CONTAINER_multihashmap_put (namespace->update_map, &hc, nsn,
1070 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1073 if (next_id != NULL)
1075 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
1078 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1079 &process_update_node, &pc);
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Calculating TREEs to find roots of update trees\n");
1086 /* Find heads of TREEs in update graph */
1087 nug = ++namespace->nug_gen;
1088 fc.tree_array = NULL;
1089 fc.tree_array_size = 0;
1091 for (i = 0; i < namespace->update_node_count; i++)
1093 nsn = namespace->update_nodes[i];
1094 if (nsn->nug == nug)
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1100 continue; /* already placed in TREE */
1102 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1106 fc.namespace = namespace;
1107 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1109 if (fc.id == UINT_MAX)
1111 /* start new TREE */
1112 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
1114 if (fc.tree_array[fc.id] == NULL)
1116 fc.tree_array[fc.id] = nsn;
1117 nsn->tree_id = fc.id;
1121 if (fc.id == fc.tree_array_size)
1123 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
1124 nsn->tree_id = fc.id;
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Starting new TREE %u with node `%s'\n", nsn->tree_id,
1131 /* put all nodes with same identifier into this TREE */
1132 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1133 fc.id = nsn->tree_id;
1135 fc.namespace = namespace;
1136 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
1141 /* make head of TREE "id" */
1142 fc.tree_array[fc.id] = nsn;
1143 nsn->tree_id = fc.id;
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1150 for (i = 0; i < fc.tree_array_size; i++)
1152 nsn = fc.tree_array[i];
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
1160 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
1163 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
1170 /* end of fs_namespace.c */