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"
34 * Return the name of the directory in which we store
35 * our local namespaces (or rather, their public keys).
37 * @param h global fs handle
38 * @return NULL on error, otherwise the name of the directory
41 get_namespace_directory (struct GNUNET_FS_Handle *h)
46 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
51 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
52 _("Configuration fails to specify `%s' in section `%s'\n"),
62 * Return the name of the directory in which we store
63 * the update information graph for the given local namespace.
65 * @param ns namespace handle
66 * @return NULL on error, otherwise the name of the directory
69 get_update_information_directory (struct GNUNET_FS_Namespace *ns)
75 GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg,
80 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
81 _("Configuration fails to specify `%s' in section `%s'\n"),
86 GNUNET_asprintf (&ret,
97 * Write the namespace update node graph to a file.
99 * @param ns namespace to dump
102 write_update_information_graph (struct GNUNET_FS_Namespace *ns)
105 struct GNUNET_BIO_WriteHandle *wh;
107 struct NamespaceUpdateNode *n;
110 fn = get_update_information_directory (ns);
111 wh = GNUNET_BIO_write_open (fn);
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 _("Failed to open `%s' for writing: %s\n"),
121 GNUNET_BIO_write_int32 (wh, ns->update_node_count))
123 for (i=0;i<ns->update_node_count;i++)
125 n = ns->update_nodes[i];
126 uris = GNUNET_FS_uri_to_string (n->uri);
128 GNUNET_BIO_write_string (wh, n->id)) ||
130 GNUNET_BIO_write_meta_data (wh, n->md)) ||
132 GNUNET_BIO_write_string (wh, n->update)) ||
134 GNUNET_BIO_write_string (wh, uris)) )
142 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 _("Failed to write `%s': %s\n"),
151 * Read the namespace update node graph from a file.
153 * @param ns namespace to read
156 read_update_information_graph (struct GNUNET_FS_Namespace *ns)
159 struct GNUNET_BIO_ReadHandle *rh;
161 struct NamespaceUpdateNode *n;
166 fn = get_update_information_directory (ns);
168 GNUNET_DISK_file_test (fn))
173 rh = GNUNET_BIO_read_open (fn);
180 GNUNET_BIO_read_int32 (rh, &count))
185 if (count > 1024 * 1024)
192 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
196 ns->update_nodes = GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode*));
198 for (i=0;i<count;i++)
200 n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
202 GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024)) ||
204 GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
206 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
208 GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)) )
211 GNUNET_free_non_null (n->id);
212 GNUNET_free_non_null (n->update);
214 GNUNET_CONTAINER_meta_data_destroy (n->md);
218 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
225 GNUNET_free (n->update);
226 GNUNET_CONTAINER_meta_data_destroy (n->md);
230 ns->update_nodes[i] = n;
232 ns->update_node_count = i;
234 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 _("Failed to write `%s': %s\n"),
246 * Context for advertising a namespace.
248 struct AdvertisementContext
251 * Function to call with the result.
253 GNUNET_FS_PublishContinuation cont;
263 struct GNUNET_DATASTORE_Handle *dsh;
268 struct GNUNET_SCHEDULER_Handle *sched;
273 struct GNUNET_FS_Uri *ksk_uri;
281 * NBlock to sign and store.
288 struct GNUNET_FS_Namespace *ns;
293 struct GNUNET_TIME_Absolute expiration;
296 * Number of bytes of plaintext.
311 * Current keyword offset.
318 * Disconnect from the datastore.
320 * @param cls datastore handle
321 * @param tc scheduler context
324 do_disconnect (void *cls,
325 const struct GNUNET_SCHEDULER_TaskContext *tc)
327 struct GNUNET_DATASTORE_Handle *dsh = cls;
329 GNUNET_DATASTORE_disconnect (dsh,
335 * Continuation called to notify client about result of the
338 * @param cls closure (our struct AdvertismentContext)
339 * @param success GNUNET_SYSERR on failure
340 * @param msg NULL on success, otherwise an error message
343 advertisement_cont (void *cls,
347 struct AdvertisementContext *ac = cls;
350 GNUNET_HashCode query;
351 struct GNUNET_CRYPTO_AesSessionKey skey;
352 struct GNUNET_CRYPTO_AesInitializationVector iv;
353 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
355 if (GNUNET_OK != success)
358 GNUNET_SCHEDULER_add_continuation (ac->sched,
361 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
362 ac->cont (ac->cont_cls, NULL, msg);
363 GNUNET_FS_uri_destroy (ac->ksk_uri);
364 GNUNET_free (ac->pt);
365 GNUNET_free (ac->nb);
366 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
370 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
373 GNUNET_SCHEDULER_add_continuation (ac->sched,
376 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
377 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
378 GNUNET_FS_uri_destroy (ac->ksk_uri);
379 GNUNET_free (ac->pt);
380 GNUNET_free (ac->nb);
381 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
385 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
386 /* first character of keyword indicates if it is
387 mandatory or not -- ignore for hashing */
388 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
389 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
390 GNUNET_CRYPTO_aes_encrypt (ac->pt,
395 GNUNET_break (GNUNET_OK ==
396 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
398 &ac->nb->ns_signature));
399 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
400 GNUNET_assert (pk != NULL);
401 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
402 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
403 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
405 GNUNET_break (GNUNET_OK ==
406 GNUNET_CRYPTO_rsa_sign (pk,
407 &ac->nb->ksk_purpose,
408 &ac->nb->ksk_signature));
409 GNUNET_CRYPTO_rsa_key_free (pk);
410 GNUNET_DATASTORE_put (ac->dsh,
411 0 /* no reservation */,
413 ac->pt_size + sizeof (struct NBlock),
415 GNUNET_BLOCK_TYPE_NBLOCK,
420 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
427 * Publish an advertismement for a namespace.
429 * @param h handle to the file sharing subsystem
430 * @param ksk_uri keywords to use for advertisment
431 * @param namespace handle for the namespace that should be advertised
432 * @param meta meta-data for the namespace advertisement
433 * @param anonymity for the namespace advertismement
434 * @param priority for the namespace advertisement
435 * @param expiration for the namespace advertisement
436 * @param rootEntry name of the root of the namespace
437 * @param cont continuation
438 * @param cont_cls closure for cont
441 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
442 struct GNUNET_FS_Uri *ksk_uri,
443 struct GNUNET_FS_Namespace *namespace,
444 const struct GNUNET_CONTAINER_MetaData *meta,
447 struct GNUNET_TIME_Absolute expiration,
448 const char *rootEntry,
449 GNUNET_FS_PublishContinuation cont,
457 struct GNUNET_DATASTORE_Handle *dsh;
458 struct AdvertisementContext *ctx;
461 /* create advertisements */
462 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
465 cont (cont_cls, NULL, _("Failed to serialize meta data"));
468 reslen = strlen (rootEntry) + 1;
469 size = mdsize + sizeof (struct NBlock) + reslen;
470 if (size > MAX_NBLOCK_SIZE)
472 size = MAX_NBLOCK_SIZE;
473 mdsize = size - sizeof (struct NBlock) - reslen;
476 pt = GNUNET_malloc (mdsize + reslen);
477 memcpy (pt, rootEntry, reslen);
479 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
482 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
487 cont (cont_cls, NULL, _("Failed to serialize meta data"));
490 size = mdsize + sizeof (struct NBlock) + reslen;
491 nb = GNUNET_malloc (size);
492 GNUNET_CRYPTO_rsa_key_get_public (namespace->key,
494 nb->ns_purpose.size = htonl (mdsize + reslen +
495 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
496 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
497 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
498 nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
499 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
500 dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
505 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
508 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
510 ctx->cont_cls = cont_cls;
512 ctx->sched = h->sched;
513 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
516 ctx->pt_size = mdsize + reslen;
519 ctx->anonymity = anonymity;
520 ctx->priority = priority;
521 ctx->expiration = expiration;
522 advertisement_cont (ctx, GNUNET_OK, NULL);
527 * Create a namespace with the given name; if one already
528 * exists, return a handle to the existing namespace.
530 * @param h handle to the file sharing subsystem
531 * @param name name to use for the namespace
532 * @return handle to the namespace, NULL on error
534 struct GNUNET_FS_Namespace *
535 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
540 struct GNUNET_FS_Namespace *ret;
542 dn = get_namespace_directory (h);
543 GNUNET_asprintf (&fn,
549 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
552 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
553 if (ret->key == NULL)
555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
556 _("Failed to create or read private key for namespace `%s'\n"),
562 ret->name = GNUNET_strdup (name);
569 * Delete a namespace handle. Can be used for a clean shutdown (free
570 * memory) or also to freeze the namespace to prevent further
571 * insertions by anyone.
573 * @param namespace handle to the namespace that should be deleted / freed
574 * @param freeze prevents future insertions; creating a namespace
575 * with the same name again will create a fresh namespace instead
577 * @return GNUNET_OK on success, GNUNET_SYSERR on error
580 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
584 struct NamespaceUpdateNode *nsn;
589 if (0 != UNLINK (namespace->filename))
590 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
592 namespace->filename);
594 if (0 == namespace->rc)
596 GNUNET_CRYPTO_rsa_key_free (namespace->key);
597 GNUNET_free (namespace->filename);
598 GNUNET_free (namespace->name);
599 for (i=0;i<namespace->update_node_count;i++)
601 nsn = namespace->update_nodes[i];
602 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
603 GNUNET_FS_uri_destroy (nsn->uri);
604 GNUNET_free (nsn->id);
605 GNUNET_free (nsn->update);
608 GNUNET_array_grow (namespace->update_nodes,
609 namespace->update_node_count,
611 if (namespace->update_map != NULL)
612 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
613 GNUNET_free (namespace);
620 * Context for the 'process_namespace' callback.
621 * Specifies a function to call on each namespace.
623 struct ProcessNamespaceContext
628 GNUNET_FS_NamespaceInfoProcessor cb;
638 * Function called with a filename of a namespace. Reads the key and
639 * calls the callback.
641 * @param cls closure (struct ProcessNamespaceContext)
642 * @param filename complete filename (absolute path)
643 * @return GNUNET_OK to continue to iterate,
644 * GNUNET_SYSERR to abort iteration with error!
647 process_namespace (void *cls,
648 const char *filename)
650 struct ProcessNamespaceContext *pnc = cls;
651 struct GNUNET_CRYPTO_RsaPrivateKey *key;
652 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
657 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
661 _("Failed to read namespace private key file `%s', deleting it!\n"),
663 if (0 != UNLINK (filename))
664 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
669 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
670 GNUNET_CRYPTO_rsa_key_free (key);
671 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
673 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
675 pnc->cb (pnc->cb_cls,
683 * Build a list of all available local (!) namespaces The returned
684 * names are only the nicknames since we only iterate over the local
687 * @param h handle to the file sharing subsystem
688 * @param cb function to call on each known namespace
689 * @param cb_cls closure for cb
692 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
693 GNUNET_FS_NamespaceInfoProcessor cb,
697 struct ProcessNamespaceContext ctx;
699 dn = get_namespace_directory (h);
704 GNUNET_DISK_directory_scan (dn,
714 * Context for the SKS publication.
716 struct PublishSksContext
720 * URI of the new entry in the namespace.
722 struct GNUNET_FS_Uri *uri;
725 * Namespace update node to add to namespace on success (or to be
726 * deleted if publishing failed).
728 struct NamespaceUpdateNode *nsn;
731 * Namespace we're publishing to.
733 struct GNUNET_FS_Namespace *namespace;
736 * Handle to the datastore.
738 struct GNUNET_DATASTORE_Handle *dsh;
741 * Function to call once we're done.
743 GNUNET_FS_PublishContinuation cont;
754 * Function called by the datastore API with
755 * the result from the PUT (SBlock) request.
757 * @param cls closure of type "struct PublishSksContext*"
758 * @param success GNUNET_OK on success
759 * @param msg error message (or NULL)
762 sb_put_cont (void *cls,
766 struct PublishSksContext *psc = cls;
769 if (NULL != psc->dsh)
771 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
774 if (GNUNET_OK != success)
776 psc->cont (psc->cont_cls,
782 if (psc->nsn != NULL)
784 /* FIXME: this can be done much more
785 efficiently by simply appending to the
786 file and overwriting the 4-byte header */
787 if (psc->namespace->update_nodes == NULL)
788 read_update_information_graph (psc->namespace);
789 GNUNET_array_append (psc->namespace->update_nodes,
790 psc->namespace->update_node_count,
792 if (psc->namespace->update_map != NULL)
794 GNUNET_CRYPTO_hash (psc->nsn->id,
795 strlen (psc->nsn->id),
797 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map,
800 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
803 write_update_information_graph (psc->namespace);
805 psc->cont (psc->cont_cls,
809 GNUNET_FS_namespace_delete (psc->namespace,
811 GNUNET_FS_uri_destroy (psc->uri);
812 if (psc->nsn != NULL)
814 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
815 GNUNET_FS_uri_destroy (psc->nsn->uri);
816 GNUNET_free (psc->nsn->id);
817 GNUNET_free (psc->nsn->update);
818 GNUNET_free (psc->nsn);
825 * Publish an SBlock on GNUnet.
827 * @param h handle to the file sharing subsystem
828 * @param namespace namespace to publish in
829 * @param identifier identifier to use
830 * @param update update identifier to use
831 * @param meta metadata to use
832 * @param uri URI to refer to in the SBlock
833 * @param expirationTime when the SBlock expires
834 * @param anonymity anonymity level for the SBlock
835 * @param priority priority for the SBlock
836 * @param options publication options
837 * @param cont continuation
838 * @param cont_cls closure for cont
841 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
842 struct GNUNET_FS_Namespace *namespace,
843 const char *identifier,
845 const struct GNUNET_CONTAINER_MetaData *meta,
846 const struct GNUNET_FS_Uri *uri,
847 struct GNUNET_TIME_Absolute expirationTime,
850 enum GNUNET_FS_PublishOptions options,
851 GNUNET_FS_PublishContinuation cont,
854 struct PublishSksContext *psc;
855 struct GNUNET_CRYPTO_AesSessionKey sk;
856 struct GNUNET_CRYPTO_AesInitializationVector iv;
857 struct GNUNET_FS_Uri *sks_uri;
865 struct SBlock *sb_enc;
867 struct GNUNET_CONTAINER_MetaData *mmeta;
868 GNUNET_HashCode key; /* hash of thisId = key */
869 GNUNET_HashCode id; /* hash of hc = identifier */
870 GNUNET_HashCode query; /* id ^ nsid = DB query */
873 mmeta = GNUNET_CONTAINER_meta_data_create ();
875 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
876 uris = GNUNET_FS_uri_to_string (uri);
877 slen = strlen (uris) + 1;
878 idlen = strlen (identifier);
881 nidlen = strlen (update) + 1;
882 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
883 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
884 if (size > MAX_SBLOCK_SIZE)
886 size = MAX_SBLOCK_SIZE;
887 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
889 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
890 dest = (char *) &sb[1];
891 memcpy (dest, update, nidlen);
893 memcpy (dest, uris, slen);
896 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
899 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
900 GNUNET_CONTAINER_meta_data_destroy (mmeta);
907 _("Internal error."));
910 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
911 sb_enc = GNUNET_malloc (size);
912 GNUNET_CRYPTO_hash (identifier, idlen, &key);
913 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
914 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
916 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
917 GNUNET_CRYPTO_hash (&sb_enc->subspace,
918 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
919 &sks_uri->data.sks.namespace);
920 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
921 GNUNET_CRYPTO_hash_xor (&id,
922 &sks_uri->data.sks.namespace,
923 &sb_enc->identifier);
924 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
925 GNUNET_CRYPTO_aes_encrypt (&sb[1],
926 size - sizeof (struct SBlock),
930 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
931 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
932 + sizeof(struct SBlock)
933 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
934 GNUNET_assert (GNUNET_OK ==
935 GNUNET_CRYPTO_rsa_sign (namespace->key,
937 &sb_enc->signature));
938 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
941 psc->namespace = namespace;
943 psc->cont_cls = cont_cls;
944 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
946 GNUNET_free (sb_enc);
953 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
954 if (NULL == psc->dsh)
956 GNUNET_free (sb_enc);
960 _("Failed to connect to datastore."));
963 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
968 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
969 psc->nsn->id = GNUNET_strdup (identifier);
970 psc->nsn->update = GNUNET_strdup (update);
971 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
972 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
974 GNUNET_DATASTORE_put (psc->dsh,
979 GNUNET_BLOCK_TYPE_SBLOCK,
984 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
988 GNUNET_free (sb_enc);
993 * Closure for 'process_update_node'.
995 struct ProcessUpdateClosure
998 * Function to call for each node.
1000 GNUNET_FS_IdentifierProcessor ip;
1010 * Call the iterator in the closure for each node.
1012 * @param cls closure (of type 'struct ProcessUpdateClosure *')
1013 * @param key current key code
1014 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
1015 * @return GNUNET_YES if we should continue to
1020 process_update_node (void *cls,
1021 const GNUNET_HashCode * key,
1024 struct ProcessUpdateClosure *pc = cls;
1025 struct NamespaceUpdateNode *nsn = value;
1037 * Closure for 'find_sccs'.
1039 struct FindSccClosure
1042 * Namespace we are operating on.
1044 struct GNUNET_FS_Namespace *namespace;
1047 * Array with 'head's of SCCs.
1049 struct NamespaceUpdateNode **scc_array;
1052 * Size of 'scc_array'
1054 unsigned int scc_array_size;
1057 * Current generational ID used.
1062 * Identifier for the current SCC, or UINT_MAX for none yet.
1069 * Find all nodes reachable from the current node (including the
1070 * current node itself). If they are in no SCC, add them to the
1071 * current one. If they are the head of another SCC, merge the
1072 * SCCs. If they are in the middle of another SCC, let them be.
1073 * We can tell that a node is already in an SCC by checking if
1074 * its 'nug' field is set to the current 'nug' value. It is the
1075 * head of an SCC if it is in the 'scc_array' under its respective
1078 * @param cls closure (of type 'struct FindSccClosure')
1079 * @param key current key code
1080 * @param value value in the hash map
1081 * @return GNUNET_YES if we should continue to
1086 find_sccs (void *cls,
1087 const GNUNET_HashCode * key,
1090 struct FindSccClosure *fc = cls;
1091 struct NamespaceUpdateNode *nsn = value;
1094 if (nsn->nug == fc->nug)
1096 if (fc->scc_array[nsn->scc_id] != nsn)
1097 return GNUNET_YES; /* part of another SCC, end trace */
1098 fc->scc_array[nsn->scc_id] = NULL;
1099 if (fc->id == UINT_MAX)
1100 fc->id = nsn->scc_id; /* take over ID */
1106 GNUNET_CRYPTO_hash (nsn->update,
1107 strlen (nsn->update),
1109 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map,
1119 * List all of the identifiers in the namespace for which we could
1120 * produce an update. Namespace updates form a graph where each node
1121 * has a name. Each node can have any number of URI/meta-data entries
1122 * which can each be linked to other nodes. Cycles are possible.
1124 * Calling this function with "next_id" NULL will cause the library to
1125 * call "ip" with a root for each strongly connected component of the
1126 * graph (a root being a node from which all other nodes in the Scc
1129 * Calling this function with "next_id" being the name of a node will
1130 * cause the library to call "ip" with all children of the node. Note
1131 * that cycles within an SCC are possible (including self-loops).
1133 * @param namespace namespace to inspect for updateable content
1134 * @param next_id ID to look for; use NULL to look for SCC roots
1135 * @param ip function to call on each updateable identifier
1136 * @param ip_cls closure for ip
1139 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1140 const char *next_id,
1141 GNUNET_FS_IdentifierProcessor ip,
1147 struct NamespaceUpdateNode *nsn;
1148 struct ProcessUpdateClosure pc;
1149 struct FindSccClosure fc;
1151 if (namespace->update_nodes == NULL)
1152 read_update_information_graph (namespace);
1153 if (namespace->update_nodes == NULL)
1154 return; /* no nodes */
1155 if (namespace->update_map == NULL)
1157 /* need to construct */
1158 namespace->update_map = GNUNET_CONTAINER_multihashmap_create (2 + 3 * namespace->update_node_count / 4);
1159 for (i=0;i<namespace->update_node_count;i++)
1161 nsn = namespace->update_nodes[i];
1162 GNUNET_CRYPTO_hash (nsn->id,
1165 GNUNET_CONTAINER_multihashmap_put (namespace->update_map,
1168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1171 if (next_id != NULL)
1173 GNUNET_CRYPTO_hash (next_id,
1178 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1180 &process_update_node,
1184 /* Find heads of SCCs in update graph */
1185 nug = ++namespace->nug_gen;
1186 fc.scc_array = NULL;
1187 fc.scc_array_size = 0;
1189 for (i=0;i<namespace->update_node_count;i++)
1191 nsn = namespace->update_nodes[i];
1192 if (nsn->nug == nug)
1193 continue; /* already placed in SCC */
1194 GNUNET_CRYPTO_hash (nsn->update,
1195 strlen (nsn->update),
1200 fc.namespace = namespace;
1201 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1205 if (fc.id == UINT_MAX)
1208 for (fc.id=0;fc.id<fc.scc_array_size;fc.id++)
1210 if (fc.scc_array[fc.id] == NULL)
1212 fc.scc_array[fc.id] = nsn;
1213 nsn->scc_id = fc.id;
1217 if (fc.id == fc.scc_array_size)
1219 GNUNET_array_append (fc.scc_array,
1222 nsn->scc_id = fc.id;
1224 /* put all nodes with same identifier into this SCC */
1225 GNUNET_CRYPTO_hash (nsn->id,
1228 fc.id = nsn->scc_id;
1230 fc.namespace = namespace;
1231 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1238 /* make head of SCC "id" */
1239 fc.scc_array[fc.id] = nsn;
1240 nsn->scc_id = fc.id;
1243 for (i=0;i<fc.scc_array_size;i++)
1245 nsn = fc.scc_array[i];
1252 GNUNET_array_grow (fc.scc_array,
1258 /* end of fs_namespace.c */