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,
53 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
54 _("Configuration fails to specify `%s' in section `%s'\n"),
64 * Return the name of the directory in which we store
65 * the update information graph for the given local namespace.
67 * @param ns namespace handle
68 * @return NULL on error, otherwise the name of the directory
71 get_update_information_directory (struct GNUNET_FS_Namespace *ns)
77 GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg,
82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83 _("Configuration fails to specify `%s' in section `%s'\n"),
88 GNUNET_asprintf (&ret,
99 * Write the namespace update node graph to a file.
101 * @param ns namespace to dump
104 write_update_information_graph (struct GNUNET_FS_Namespace *ns)
107 struct GNUNET_BIO_WriteHandle *wh;
109 struct NamespaceUpdateNode *n;
112 fn = get_update_information_directory (ns);
113 wh = GNUNET_BIO_write_open (fn);
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 _("Failed to open `%s' for writing: %s\n"),
123 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);
130 GNUNET_BIO_write_string (wh, n->id)) ||
132 GNUNET_BIO_write_meta_data (wh, n->md)) ||
134 GNUNET_BIO_write_string (wh, n->update)) ||
136 GNUNET_BIO_write_string (wh, uris)) )
144 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 _("Failed to write `%s': %s\n"),
153 * Read the namespace update node graph from a file.
155 * @param ns namespace to read
158 read_update_information_graph (struct GNUNET_FS_Namespace *ns)
161 struct GNUNET_BIO_ReadHandle *rh;
163 struct NamespaceUpdateNode *n;
168 fn = get_update_information_directory (ns);
170 GNUNET_DISK_file_test (fn))
175 rh = GNUNET_BIO_read_open (fn);
182 GNUNET_BIO_read_int32 (rh, &count))
187 if (count > 1024 * 1024)
194 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
198 ns->update_nodes = GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode*));
200 for (i=0;i<count;i++)
202 n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
204 GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024)) ||
206 GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
208 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
210 GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)) )
213 GNUNET_free_non_null (n->id);
214 GNUNET_free_non_null (n->update);
216 GNUNET_CONTAINER_meta_data_destroy (n->md);
220 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
227 GNUNET_free (n->update);
228 GNUNET_CONTAINER_meta_data_destroy (n->md);
232 ns->update_nodes[i] = n;
234 ns->update_node_count = i;
236 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 _("Failed to write `%s': %s\n"),
248 * Context for advertising a namespace.
250 struct AdvertisementContext
253 * Function to call with the result.
255 GNUNET_FS_PublishContinuation cont;
265 struct GNUNET_DATASTORE_Handle *dsh;
270 struct GNUNET_SCHEDULER_Handle *sched;
275 struct GNUNET_FS_Uri *ksk_uri;
283 * NBlock to sign and store.
290 struct GNUNET_FS_Namespace *ns;
295 struct GNUNET_TIME_Absolute expiration;
298 * Number of bytes of plaintext.
313 * Current keyword offset.
320 * Disconnect from the datastore.
322 * @param cls datastore handle
323 * @param tc scheduler context
326 do_disconnect (void *cls,
327 const struct GNUNET_SCHEDULER_TaskContext *tc)
329 struct GNUNET_DATASTORE_Handle *dsh = cls;
331 GNUNET_DATASTORE_disconnect (dsh,
337 * Continuation called to notify client about result of the
340 * @param cls closure (our struct AdvertismentContext)
341 * @param success GNUNET_SYSERR on failure
342 * @param msg NULL on success, otherwise an error message
345 advertisement_cont (void *cls,
349 struct AdvertisementContext *ac = cls;
352 GNUNET_HashCode query;
353 struct GNUNET_CRYPTO_AesSessionKey skey;
354 struct GNUNET_CRYPTO_AesInitializationVector iv;
355 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
357 if (GNUNET_OK != success)
360 GNUNET_SCHEDULER_add_continuation (ac->sched,
363 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
364 ac->cont (ac->cont_cls, NULL, msg);
365 GNUNET_FS_uri_destroy (ac->ksk_uri);
366 GNUNET_free (ac->pt);
367 GNUNET_free (ac->nb);
368 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
372 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
375 GNUNET_SCHEDULER_add_continuation (ac->sched,
378 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
379 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
380 GNUNET_FS_uri_destroy (ac->ksk_uri);
381 GNUNET_free (ac->pt);
382 GNUNET_free (ac->nb);
383 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
387 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
388 /* first character of keyword indicates if it is
389 mandatory or not -- ignore for hashing */
390 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
391 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
392 GNUNET_CRYPTO_aes_encrypt (ac->pt,
397 GNUNET_break (GNUNET_OK ==
398 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
400 &ac->nb->ns_signature));
401 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
402 GNUNET_assert (pk != NULL);
403 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
404 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
405 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
407 GNUNET_break (GNUNET_OK ==
408 GNUNET_CRYPTO_rsa_sign (pk,
409 &ac->nb->ksk_purpose,
410 &ac->nb->ksk_signature));
411 GNUNET_CRYPTO_rsa_key_free (pk);
412 GNUNET_DATASTORE_put (ac->dsh,
413 0 /* no reservation */,
415 ac->pt_size + sizeof (struct NBlock),
417 GNUNET_BLOCK_TYPE_NBLOCK,
422 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
429 * Publish an advertismement for a namespace.
431 * @param h handle to the file sharing subsystem
432 * @param ksk_uri keywords to use for advertisment
433 * @param namespace handle for the namespace that should be advertised
434 * @param meta meta-data for the namespace advertisement
435 * @param anonymity for the namespace advertismement
436 * @param priority for the namespace advertisement
437 * @param expiration for the namespace advertisement
438 * @param rootEntry name of the root of the namespace
439 * @param cont continuation
440 * @param cont_cls closure for cont
443 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
444 struct GNUNET_FS_Uri *ksk_uri,
445 struct GNUNET_FS_Namespace *namespace,
446 const struct GNUNET_CONTAINER_MetaData *meta,
449 struct GNUNET_TIME_Absolute expiration,
450 const char *rootEntry,
451 GNUNET_FS_PublishContinuation cont,
459 struct GNUNET_DATASTORE_Handle *dsh;
460 struct AdvertisementContext *ctx;
463 /* create advertisements */
464 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
467 cont (cont_cls, NULL, _("Failed to serialize meta data"));
470 reslen = strlen (rootEntry) + 1;
471 size = mdsize + sizeof (struct NBlock) + reslen;
472 if (size > MAX_NBLOCK_SIZE)
474 size = MAX_NBLOCK_SIZE;
475 mdsize = size - sizeof (struct NBlock) - reslen;
478 pt = GNUNET_malloc (mdsize + reslen);
479 memcpy (pt, rootEntry, reslen);
481 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
484 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
489 cont (cont_cls, NULL, _("Failed to serialize meta data"));
492 size = mdsize + sizeof (struct NBlock) + reslen;
493 nb = GNUNET_malloc (size);
494 GNUNET_CRYPTO_rsa_key_get_public (namespace->key,
496 nb->ns_purpose.size = htonl (mdsize + reslen +
497 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
498 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
499 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
500 nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
501 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
502 dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
507 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
510 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
512 ctx->cont_cls = cont_cls;
514 ctx->sched = h->sched;
515 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
518 ctx->pt_size = mdsize + reslen;
521 ctx->anonymity = anonymity;
522 ctx->priority = priority;
523 ctx->expiration = expiration;
524 advertisement_cont (ctx, GNUNET_OK, NULL);
529 * Create a namespace with the given name; if one already
530 * exists, return a handle to the existing namespace.
532 * @param h handle to the file sharing subsystem
533 * @param name name to use for the namespace
534 * @return handle to the namespace, NULL on error
536 struct GNUNET_FS_Namespace *
537 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
542 struct GNUNET_FS_Namespace *ret;
544 dn = get_namespace_directory (h);
545 GNUNET_asprintf (&fn,
551 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
554 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
555 if (ret->key == NULL)
557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558 _("Failed to create or read private key for namespace `%s'\n"),
564 ret->name = GNUNET_strdup (name);
571 * Delete a namespace handle. Can be used for a clean shutdown (free
572 * memory) or also to freeze the namespace to prevent further
573 * insertions by anyone.
575 * @param namespace handle to the namespace that should be deleted / freed
576 * @param freeze prevents future insertions; creating a namespace
577 * with the same name again will create a fresh namespace instead
579 * @return GNUNET_OK on success, GNUNET_SYSERR on error
582 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
586 struct NamespaceUpdateNode *nsn;
591 if (0 != UNLINK (namespace->filename))
592 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
594 namespace->filename);
596 if (0 == namespace->rc)
598 GNUNET_CRYPTO_rsa_key_free (namespace->key);
599 GNUNET_free (namespace->filename);
600 GNUNET_free (namespace->name);
601 for (i=0;i<namespace->update_node_count;i++)
603 nsn = namespace->update_nodes[i];
604 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
605 GNUNET_FS_uri_destroy (nsn->uri);
606 GNUNET_free (nsn->id);
607 GNUNET_free (nsn->update);
610 GNUNET_array_grow (namespace->update_nodes,
611 namespace->update_node_count,
613 if (namespace->update_map != NULL)
614 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
615 GNUNET_free (namespace);
622 * Context for the 'process_namespace' callback.
623 * Specifies a function to call on each namespace.
625 struct ProcessNamespaceContext
630 GNUNET_FS_NamespaceInfoProcessor cb;
640 * Function called with a filename of a namespace. Reads the key and
641 * calls the callback.
643 * @param cls closure (struct ProcessNamespaceContext)
644 * @param filename complete filename (absolute path)
645 * @return GNUNET_OK to continue to iterate,
646 * GNUNET_SYSERR to abort iteration with error!
649 process_namespace (void *cls,
650 const char *filename)
652 struct ProcessNamespaceContext *pnc = cls;
653 struct GNUNET_CRYPTO_RsaPrivateKey *key;
654 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
659 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
663 _("Failed to read namespace private key file `%s', deleting it!\n"),
665 if (0 != UNLINK (filename))
666 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
671 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
672 GNUNET_CRYPTO_rsa_key_free (key);
673 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
675 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
677 pnc->cb (pnc->cb_cls,
685 * Build a list of all available local (!) namespaces The returned
686 * names are only the nicknames since we only iterate over the local
689 * @param h handle to the file sharing subsystem
690 * @param cb function to call on each known namespace
691 * @param cb_cls closure for cb
694 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
695 GNUNET_FS_NamespaceInfoProcessor cb,
699 struct ProcessNamespaceContext ctx;
701 dn = get_namespace_directory (h);
706 GNUNET_DISK_directory_scan (dn,
716 * Context for the SKS publication.
718 struct PublishSksContext
722 * URI of the new entry in the namespace.
724 struct GNUNET_FS_Uri *uri;
727 * Namespace update node to add to namespace on success (or to be
728 * deleted if publishing failed).
730 struct NamespaceUpdateNode *nsn;
733 * Namespace we're publishing to.
735 struct GNUNET_FS_Namespace *namespace;
738 * Handle to the datastore.
740 struct GNUNET_DATASTORE_Handle *dsh;
743 * Function to call once we're done.
745 GNUNET_FS_PublishContinuation cont;
756 * Function called by the datastore API with
757 * the result from the PUT (SBlock) request.
759 * @param cls closure of type "struct PublishSksContext*"
760 * @param success GNUNET_OK on success
761 * @param msg error message (or NULL)
764 sb_put_cont (void *cls,
768 struct PublishSksContext *psc = cls;
771 if (NULL != psc->dsh)
773 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
776 if (GNUNET_OK != success)
778 psc->cont (psc->cont_cls,
784 if (psc->nsn != NULL)
786 /* FIXME: this can be done much more
787 efficiently by simply appending to the
788 file and overwriting the 4-byte header */
789 if (psc->namespace->update_nodes == NULL)
790 read_update_information_graph (psc->namespace);
791 GNUNET_array_append (psc->namespace->update_nodes,
792 psc->namespace->update_node_count,
794 if (psc->namespace->update_map != NULL)
796 GNUNET_CRYPTO_hash (psc->nsn->id,
797 strlen (psc->nsn->id),
799 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map,
802 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
805 write_update_information_graph (psc->namespace);
807 psc->cont (psc->cont_cls,
811 GNUNET_FS_namespace_delete (psc->namespace,
813 GNUNET_FS_uri_destroy (psc->uri);
814 if (psc->nsn != NULL)
816 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
817 GNUNET_FS_uri_destroy (psc->nsn->uri);
818 GNUNET_free (psc->nsn->id);
819 GNUNET_free (psc->nsn->update);
820 GNUNET_free (psc->nsn);
827 * Publish an SBlock on GNUnet.
829 * @param h handle to the file sharing subsystem
830 * @param namespace namespace to publish in
831 * @param identifier identifier to use
832 * @param update update identifier to use
833 * @param meta metadata to use
834 * @param uri URI to refer to in the SBlock
835 * @param expirationTime when the SBlock expires
836 * @param anonymity anonymity level for the SBlock
837 * @param priority priority for the SBlock
838 * @param options publication options
839 * @param cont continuation
840 * @param cont_cls closure for cont
843 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
844 struct GNUNET_FS_Namespace *namespace,
845 const char *identifier,
847 const struct GNUNET_CONTAINER_MetaData *meta,
848 const struct GNUNET_FS_Uri *uri,
849 struct GNUNET_TIME_Absolute expirationTime,
852 enum GNUNET_FS_PublishOptions options,
853 GNUNET_FS_PublishContinuation cont,
856 struct PublishSksContext *psc;
857 struct GNUNET_CRYPTO_AesSessionKey sk;
858 struct GNUNET_CRYPTO_AesInitializationVector iv;
859 struct GNUNET_FS_Uri *sks_uri;
867 struct SBlock *sb_enc;
869 struct GNUNET_CONTAINER_MetaData *mmeta;
870 GNUNET_HashCode key; /* hash of thisId = key */
871 GNUNET_HashCode id; /* hash of hc = identifier */
872 GNUNET_HashCode query; /* id ^ nsid = DB query */
875 mmeta = GNUNET_CONTAINER_meta_data_create ();
877 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
878 uris = GNUNET_FS_uri_to_string (uri);
879 slen = strlen (uris) + 1;
880 idlen = strlen (identifier);
883 nidlen = strlen (update) + 1;
884 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
885 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
886 if (size > MAX_SBLOCK_SIZE)
888 size = MAX_SBLOCK_SIZE;
889 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
891 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
892 dest = (char *) &sb[1];
893 memcpy (dest, update, nidlen);
895 memcpy (dest, uris, slen);
898 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
901 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
902 GNUNET_CONTAINER_meta_data_destroy (mmeta);
909 _("Internal error."));
912 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
913 sb_enc = GNUNET_malloc (size);
914 GNUNET_CRYPTO_hash (identifier, idlen, &key);
915 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
916 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
918 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
919 GNUNET_CRYPTO_hash (&sb_enc->subspace,
920 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
921 &sks_uri->data.sks.namespace);
922 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
923 GNUNET_CRYPTO_hash_xor (&id,
924 &sks_uri->data.sks.namespace,
925 &sb_enc->identifier);
926 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
927 GNUNET_CRYPTO_aes_encrypt (&sb[1],
928 size - sizeof (struct SBlock),
932 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
933 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
934 + sizeof(struct SBlock)
935 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
936 GNUNET_assert (GNUNET_OK ==
937 GNUNET_CRYPTO_rsa_sign (namespace->key,
939 &sb_enc->signature));
940 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
943 psc->namespace = namespace;
945 psc->cont_cls = cont_cls;
946 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
948 GNUNET_free (sb_enc);
955 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
956 if (NULL == psc->dsh)
958 GNUNET_free (sb_enc);
962 _("Failed to connect to datastore."));
965 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
970 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
971 psc->nsn->id = GNUNET_strdup (identifier);
972 psc->nsn->update = GNUNET_strdup (update);
973 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
974 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
976 GNUNET_DATASTORE_put (psc->dsh,
981 GNUNET_BLOCK_TYPE_SBLOCK,
986 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
990 GNUNET_free (sb_enc);
995 * Closure for 'process_update_node'.
997 struct ProcessUpdateClosure
1000 * Function to call for each node.
1002 GNUNET_FS_IdentifierProcessor ip;
1012 * Call the iterator in the closure for each node.
1014 * @param cls closure (of type 'struct ProcessUpdateClosure *')
1015 * @param key current key code
1016 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
1017 * @return GNUNET_YES if we should continue to
1022 process_update_node (void *cls,
1023 const GNUNET_HashCode * key,
1026 struct ProcessUpdateClosure *pc = cls;
1027 struct NamespaceUpdateNode *nsn = value;
1039 * Closure for 'find_sccs'.
1041 struct FindSccClosure
1044 * Namespace we are operating on.
1046 struct GNUNET_FS_Namespace *namespace;
1049 * Array with 'head's of SCCs.
1051 struct NamespaceUpdateNode **scc_array;
1054 * Size of 'scc_array'
1056 unsigned int scc_array_size;
1059 * Current generational ID used.
1064 * Identifier for the current SCC, or UINT_MAX for none yet.
1071 * Find all nodes reachable from the current node (including the
1072 * current node itself). If they are in no SCC, add them to the
1073 * current one. If they are the head of another SCC, merge the
1074 * SCCs. If they are in the middle of another SCC, let them be.
1075 * We can tell that a node is already in an SCC by checking if
1076 * its 'nug' field is set to the current 'nug' value. It is the
1077 * head of an SCC if it is in the 'scc_array' under its respective
1080 * @param cls closure (of type 'struct FindSccClosure')
1081 * @param key current key code
1082 * @param value value in the hash map
1083 * @return GNUNET_YES if we should continue to
1088 find_sccs (void *cls,
1089 const GNUNET_HashCode * key,
1092 struct FindSccClosure *fc = cls;
1093 struct NamespaceUpdateNode *nsn = value;
1096 if (nsn->nug == fc->nug)
1098 if (fc->scc_array[nsn->scc_id] != nsn)
1099 return GNUNET_YES; /* part of another SCC, end trace */
1100 if (nsn->scc_id == fc->id)
1101 return GNUNET_YES; /* that's us */
1102 fc->scc_array[nsn->scc_id] = NULL;
1103 if (fc->id == UINT_MAX)
1104 fc->id = nsn->scc_id; /* take over ID */
1110 GNUNET_CRYPTO_hash (nsn->update,
1111 strlen (nsn->update),
1113 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map,
1123 * List all of the identifiers in the namespace for which we could
1124 * produce an update. Namespace updates form a graph where each node
1125 * has a name. Each node can have any number of URI/meta-data entries
1126 * which can each be linked to other nodes. Cycles are possible.
1128 * Calling this function with "next_id" NULL will cause the library to
1129 * call "ip" with a root for each strongly connected component of the
1130 * graph (a root being a node from which all other nodes in the Scc
1133 * Calling this function with "next_id" being the name of a node will
1134 * cause the library to call "ip" with all children of the node. Note
1135 * that cycles within an SCC are possible (including self-loops).
1137 * @param namespace namespace to inspect for updateable content
1138 * @param next_id ID to look for; use NULL to look for SCC roots
1139 * @param ip function to call on each updateable identifier
1140 * @param ip_cls closure for ip
1143 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1144 const char *next_id,
1145 GNUNET_FS_IdentifierProcessor ip,
1151 struct NamespaceUpdateNode *nsn;
1152 struct ProcessUpdateClosure pc;
1153 struct FindSccClosure fc;
1155 if (namespace->update_nodes == NULL)
1156 read_update_information_graph (namespace);
1157 if (namespace->update_nodes == NULL)
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "No updateable nodes found for ID `%s'\n",
1164 return; /* no nodes */
1166 if (namespace->update_map == NULL)
1168 /* need to construct */
1169 namespace->update_map = GNUNET_CONTAINER_multihashmap_create (2 + 3 * namespace->update_node_count / 4);
1170 for (i=0;i<namespace->update_node_count;i++)
1172 nsn = namespace->update_nodes[i];
1173 GNUNET_CRYPTO_hash (nsn->id,
1176 GNUNET_CONTAINER_multihashmap_put (namespace->update_map,
1179 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1182 if (next_id != NULL)
1184 GNUNET_CRYPTO_hash (next_id,
1189 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1191 &process_update_node,
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "Calculating SCCs to find roots of update trees\n");
1199 /* Find heads of SCCs in update graph */
1200 nug = ++namespace->nug_gen;
1201 fc.scc_array = NULL;
1202 fc.scc_array_size = 0;
1204 for (i=0;i<namespace->update_node_count;i++)
1206 nsn = namespace->update_nodes[i];
1207 if (nsn->nug == nug)
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "SCC of node `%s' is %u\n",
1215 continue; /* already placed in SCC */
1217 GNUNET_CRYPTO_hash (nsn->update,
1218 strlen (nsn->update),
1223 fc.namespace = namespace;
1224 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1228 if (fc.id == UINT_MAX)
1231 for (fc.id=0;fc.id<fc.scc_array_size;fc.id++)
1233 if (fc.scc_array[fc.id] == NULL)
1235 fc.scc_array[fc.id] = nsn;
1236 nsn->scc_id = fc.id;
1240 if (fc.id == fc.scc_array_size)
1242 GNUNET_array_append (fc.scc_array,
1245 nsn->scc_id = fc.id;
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Starting new SCC %u with node `%s'\n",
1253 /* put all nodes with same identifier into this SCC */
1254 GNUNET_CRYPTO_hash (nsn->id,
1257 fc.id = nsn->scc_id;
1259 fc.namespace = namespace;
1260 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1267 /* make head of SCC "id" */
1268 fc.scc_array[fc.id] = nsn;
1269 nsn->scc_id = fc.id;
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "SCC of node `%s' is %u\n",
1278 for (i=0;i<fc.scc_array_size;i++)
1280 nsn = fc.scc_array[i];
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Root of SCC %u is node `%s'\n",
1297 GNUNET_array_grow (fc.scc_array,
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Done processing SCCs\n");
1307 /* end of fs_namespace.c */