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_non_null (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_FS_Uri *ksk_uri;
278 * NBlock to sign and store.
285 struct GNUNET_FS_Namespace *ns;
290 struct GNUNET_TIME_Absolute expiration;
293 * Number of bytes of plaintext.
308 * Current keyword offset.
315 * Disconnect from the datastore.
317 * @param cls datastore handle
318 * @param tc scheduler context
321 do_disconnect (void *cls,
322 const struct GNUNET_SCHEDULER_TaskContext *tc)
324 struct GNUNET_DATASTORE_Handle *dsh = cls;
326 GNUNET_DATASTORE_disconnect (dsh,
332 * Continuation called to notify client about result of the
335 * @param cls closure (our struct AdvertismentContext)
336 * @param success GNUNET_SYSERR on failure
337 * @param msg NULL on success, otherwise an error message
340 advertisement_cont (void *cls,
344 struct AdvertisementContext *ac = cls;
347 GNUNET_HashCode query;
348 struct GNUNET_CRYPTO_AesSessionKey skey;
349 struct GNUNET_CRYPTO_AesInitializationVector iv;
350 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
352 if (GNUNET_OK != success)
355 GNUNET_SCHEDULER_add_continuation (&do_disconnect,
357 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
358 if (ac->cont != NULL)
359 ac->cont (ac->cont_cls, NULL, msg);
360 GNUNET_FS_uri_destroy (ac->ksk_uri);
361 GNUNET_free (ac->pt);
362 GNUNET_free (ac->nb);
363 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
367 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
370 GNUNET_SCHEDULER_add_continuation (&do_disconnect,
372 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
373 if (ac->cont != NULL)
374 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
375 GNUNET_FS_uri_destroy (ac->ksk_uri);
376 GNUNET_free (ac->pt);
377 GNUNET_free (ac->nb);
378 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
382 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
383 /* first character of keyword indicates if it is
384 mandatory or not -- ignore for hashing */
385 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
386 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
387 GNUNET_CRYPTO_aes_encrypt (ac->pt,
392 GNUNET_break (GNUNET_OK ==
393 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
395 &ac->nb->ns_signature));
396 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
397 GNUNET_assert (pk != NULL);
398 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
399 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
400 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
402 GNUNET_break (GNUNET_OK ==
403 GNUNET_CRYPTO_rsa_sign (pk,
404 &ac->nb->ksk_purpose,
405 &ac->nb->ksk_signature));
406 GNUNET_CRYPTO_rsa_key_free (pk);
407 GNUNET_DATASTORE_put (ac->dsh,
408 0 /* no reservation */,
410 ac->pt_size + sizeof (struct NBlock),
412 GNUNET_BLOCK_TYPE_FS_NBLOCK,
417 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
424 * Publish an advertismement for a namespace.
426 * @param h handle to the file sharing subsystem
427 * @param ksk_uri keywords to use for advertisment
428 * @param namespace handle for the namespace that should be advertised
429 * @param meta meta-data for the namespace advertisement
430 * @param anonymity for the namespace advertismement
431 * @param priority for the namespace advertisement
432 * @param expiration for the namespace advertisement
433 * @param rootEntry name of the root of the namespace
434 * @param cont continuation
435 * @param cont_cls closure for cont
438 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
439 struct GNUNET_FS_Uri *ksk_uri,
440 struct GNUNET_FS_Namespace *namespace,
441 const struct GNUNET_CONTAINER_MetaData *meta,
444 struct GNUNET_TIME_Absolute expiration,
445 const char *rootEntry,
446 GNUNET_FS_PublishContinuation cont,
454 struct GNUNET_DATASTORE_Handle *dsh;
455 struct AdvertisementContext *ctx;
458 /* create advertisements */
459 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
462 cont (cont_cls, NULL, _("Failed to serialize meta data"));
465 reslen = strlen (rootEntry) + 1;
466 size = mdsize + sizeof (struct NBlock) + reslen;
467 if (size > MAX_NBLOCK_SIZE)
469 size = MAX_NBLOCK_SIZE;
470 mdsize = size - sizeof (struct NBlock) - reslen;
473 pt = GNUNET_malloc (mdsize + reslen);
474 memcpy (pt, rootEntry, reslen);
476 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
479 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
484 cont (cont_cls, NULL, _("Failed to serialize meta data"));
487 size = mdsize + sizeof (struct NBlock) + reslen;
488 nb = GNUNET_malloc (size);
489 GNUNET_CRYPTO_rsa_key_get_public (namespace->key,
491 nb->ns_purpose.size = htonl (mdsize + reslen +
492 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
493 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
494 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
495 nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
496 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
497 dsh = GNUNET_DATASTORE_connect (h->cfg);
502 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
505 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
507 ctx->cont_cls = cont_cls;
509 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
512 ctx->pt_size = mdsize + reslen;
515 ctx->anonymity = anonymity;
516 ctx->priority = priority;
517 ctx->expiration = expiration;
518 advertisement_cont (ctx, GNUNET_OK, NULL);
523 * Create a namespace with the given name; if one already
524 * exists, return a handle to the existing namespace.
526 * @param h handle to the file sharing subsystem
527 * @param name name to use for the namespace
528 * @return handle to the namespace, NULL on error
530 struct GNUNET_FS_Namespace *
531 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
536 struct GNUNET_FS_Namespace *ret;
538 dn = get_namespace_directory (h);
539 GNUNET_asprintf (&fn,
545 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
548 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
549 if (ret->key == NULL)
551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
552 _("Failed to create or read private key for namespace `%s'\n"),
558 ret->name = GNUNET_strdup (name);
565 * Delete a namespace handle. Can be used for a clean shutdown (free
566 * memory) or also to freeze the namespace to prevent further
567 * insertions by anyone.
569 * @param namespace handle to the namespace that should be deleted / freed
570 * @param freeze prevents future insertions; creating a namespace
571 * with the same name again will create a fresh namespace instead
573 * @return GNUNET_OK on success, GNUNET_SYSERR on error
576 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
580 struct NamespaceUpdateNode *nsn;
585 if (0 != UNLINK (namespace->filename))
586 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
588 namespace->filename);
590 if (0 == namespace->rc)
592 GNUNET_CRYPTO_rsa_key_free (namespace->key);
593 GNUNET_free (namespace->filename);
594 GNUNET_free (namespace->name);
595 for (i=0;i<namespace->update_node_count;i++)
597 nsn = namespace->update_nodes[i];
598 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
599 GNUNET_FS_uri_destroy (nsn->uri);
600 GNUNET_free (nsn->id);
601 GNUNET_free (nsn->update);
604 GNUNET_array_grow (namespace->update_nodes,
605 namespace->update_node_count,
607 if (namespace->update_map != NULL)
608 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
609 GNUNET_free (namespace);
616 * Context for the 'process_namespace' callback.
617 * Specifies a function to call on each namespace.
619 struct ProcessNamespaceContext
624 GNUNET_FS_NamespaceInfoProcessor cb;
634 * Function called with a filename of a namespace. Reads the key and
635 * calls the callback.
637 * @param cls closure (struct ProcessNamespaceContext)
638 * @param filename complete filename (absolute path)
639 * @return GNUNET_OK to continue to iterate,
640 * GNUNET_SYSERR to abort iteration with error!
643 process_namespace (void *cls,
644 const char *filename)
646 struct ProcessNamespaceContext *pnc = cls;
647 struct GNUNET_CRYPTO_RsaPrivateKey *key;
648 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
653 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
657 _("Failed to read namespace private key file `%s', deleting it!\n"),
659 if (0 != UNLINK (filename))
660 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
665 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
666 GNUNET_CRYPTO_rsa_key_free (key);
667 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
669 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
671 pnc->cb (pnc->cb_cls,
679 * Build a list of all available local (!) namespaces The returned
680 * names are only the nicknames since we only iterate over the local
683 * @param h handle to the file sharing subsystem
684 * @param cb function to call on each known namespace
685 * @param cb_cls closure for cb
688 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
689 GNUNET_FS_NamespaceInfoProcessor cb,
693 struct ProcessNamespaceContext ctx;
695 dn = get_namespace_directory (h);
700 GNUNET_DISK_directory_scan (dn,
710 * Context for the SKS publication.
712 struct PublishSksContext
716 * URI of the new entry in the namespace.
718 struct GNUNET_FS_Uri *uri;
721 * Namespace update node to add to namespace on success (or to be
722 * deleted if publishing failed).
724 struct NamespaceUpdateNode *nsn;
727 * Namespace we're publishing to.
729 struct GNUNET_FS_Namespace *namespace;
732 * Handle to the datastore.
734 struct GNUNET_DATASTORE_Handle *dsh;
737 * Function to call once we're done.
739 GNUNET_FS_PublishContinuation cont;
750 * Function called by the datastore API with
751 * the result from the PUT (SBlock) request.
753 * @param cls closure of type "struct PublishSksContext*"
754 * @param success GNUNET_OK on success
755 * @param msg error message (or NULL)
758 sb_put_cont (void *cls,
762 struct PublishSksContext *psc = cls;
765 if (NULL != psc->dsh)
767 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
770 if (GNUNET_OK != success)
772 if (psc->cont != NULL)
773 psc->cont (psc->cont_cls,
779 if (psc->nsn != NULL)
781 /* FIXME: this can be done much more
782 efficiently by simply appending to the
783 file and overwriting the 4-byte header */
784 if (psc->namespace->update_nodes == NULL)
785 read_update_information_graph (psc->namespace);
786 GNUNET_array_append (psc->namespace->update_nodes,
787 psc->namespace->update_node_count,
789 if (psc->namespace->update_map != NULL)
791 GNUNET_CRYPTO_hash (psc->nsn->id,
792 strlen (psc->nsn->id),
794 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map,
797 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
800 write_update_information_graph (psc->namespace);
802 if (psc->cont != NULL)
803 psc->cont (psc->cont_cls,
807 GNUNET_FS_namespace_delete (psc->namespace,
809 GNUNET_FS_uri_destroy (psc->uri);
810 if (psc->nsn != NULL)
812 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
813 GNUNET_FS_uri_destroy (psc->nsn->uri);
814 GNUNET_free (psc->nsn->id);
815 GNUNET_free (psc->nsn->update);
816 GNUNET_free (psc->nsn);
823 * Publish an SBlock on GNUnet.
825 * @param h handle to the file sharing subsystem
826 * @param namespace namespace to publish in
827 * @param identifier identifier to use
828 * @param update update identifier to use
829 * @param meta metadata to use
830 * @param uri URI to refer to in the SBlock
831 * @param expirationTime when the SBlock expires
832 * @param anonymity anonymity level for the SBlock
833 * @param priority priority for the SBlock
834 * @param options publication options
835 * @param cont continuation
836 * @param cont_cls closure for cont
839 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
840 struct GNUNET_FS_Namespace *namespace,
841 const char *identifier,
843 const struct GNUNET_CONTAINER_MetaData *meta,
844 const struct GNUNET_FS_Uri *uri,
845 struct GNUNET_TIME_Absolute expirationTime,
848 enum GNUNET_FS_PublishOptions options,
849 GNUNET_FS_PublishContinuation cont,
852 struct PublishSksContext *psc;
853 struct GNUNET_CRYPTO_AesSessionKey sk;
854 struct GNUNET_CRYPTO_AesInitializationVector iv;
855 struct GNUNET_FS_Uri *sks_uri;
863 struct SBlock *sb_enc;
865 struct GNUNET_CONTAINER_MetaData *mmeta;
866 GNUNET_HashCode key; /* hash of thisId = key */
867 GNUNET_HashCode id; /* hash of hc = identifier */
868 GNUNET_HashCode query; /* id ^ nsid = DB query */
871 mmeta = GNUNET_CONTAINER_meta_data_create ();
873 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
874 uris = GNUNET_FS_uri_to_string (uri);
875 slen = strlen (uris) + 1;
876 idlen = strlen (identifier);
879 nidlen = strlen (update) + 1;
880 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
881 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
882 if (size > MAX_SBLOCK_SIZE)
884 size = MAX_SBLOCK_SIZE;
885 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
887 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
888 dest = (char *) &sb[1];
889 memcpy (dest, update, nidlen);
891 memcpy (dest, uris, slen);
894 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
897 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
898 GNUNET_CONTAINER_meta_data_destroy (mmeta);
905 _("Internal error."));
908 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
909 sb_enc = GNUNET_malloc (size);
910 GNUNET_CRYPTO_hash (identifier, idlen, &key);
911 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
912 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
914 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
915 GNUNET_CRYPTO_hash (&sb_enc->subspace,
916 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
917 &sks_uri->data.sks.namespace);
918 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
919 GNUNET_CRYPTO_hash_xor (&id,
920 &sks_uri->data.sks.namespace,
921 &sb_enc->identifier);
922 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
923 GNUNET_CRYPTO_aes_encrypt (&sb[1],
924 size - sizeof (struct SBlock),
928 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
929 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
930 + sizeof(struct SBlock)
931 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
932 GNUNET_assert (GNUNET_OK ==
933 GNUNET_CRYPTO_rsa_sign (namespace->key,
935 &sb_enc->signature));
936 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
939 psc->namespace = namespace;
941 psc->cont_cls = cont_cls;
942 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
944 GNUNET_free (sb_enc);
951 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
952 if (NULL == psc->dsh)
954 GNUNET_free (sb_enc);
958 _("Failed to connect to datastore."));
961 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
966 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
967 psc->nsn->id = GNUNET_strdup (identifier);
968 psc->nsn->update = GNUNET_strdup (update);
969 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
970 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
972 GNUNET_DATASTORE_put (psc->dsh,
977 GNUNET_BLOCK_TYPE_FS_SBLOCK,
982 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
986 GNUNET_free (sb_enc);
991 * Closure for 'process_update_node'.
993 struct ProcessUpdateClosure
996 * Function to call for each node.
998 GNUNET_FS_IdentifierProcessor ip;
1008 * Call the iterator in the closure for each node.
1010 * @param cls closure (of type 'struct ProcessUpdateClosure *')
1011 * @param key current key code
1012 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
1013 * @return GNUNET_YES if we should continue to
1018 process_update_node (void *cls,
1019 const GNUNET_HashCode * key,
1022 struct ProcessUpdateClosure *pc = cls;
1023 struct NamespaceUpdateNode *nsn = value;
1035 * Closure for 'find_sccs'.
1037 struct FindSccClosure
1040 * Namespace we are operating on.
1042 struct GNUNET_FS_Namespace *namespace;
1045 * Array with 'head's of SCCs.
1047 struct NamespaceUpdateNode **scc_array;
1050 * Size of 'scc_array'
1052 unsigned int scc_array_size;
1055 * Current generational ID used.
1060 * Identifier for the current SCC, or UINT_MAX for none yet.
1067 * Find all nodes reachable from the current node (including the
1068 * current node itself). If they are in no SCC, add them to the
1069 * current one. If they are the head of another SCC, merge the
1070 * SCCs. If they are in the middle of another SCC, let them be.
1071 * We can tell that a node is already in an SCC by checking if
1072 * its 'nug' field is set to the current 'nug' value. It is the
1073 * head of an SCC if it is in the 'scc_array' under its respective
1076 * @param cls closure (of type 'struct FindSccClosure')
1077 * @param key current key code
1078 * @param value value in the hash map
1079 * @return GNUNET_YES if we should continue to
1084 find_sccs (void *cls,
1085 const GNUNET_HashCode * key,
1088 struct FindSccClosure *fc = cls;
1089 struct NamespaceUpdateNode *nsn = value;
1092 if (nsn->nug == fc->nug)
1094 if (fc->scc_array[nsn->scc_id] != nsn)
1095 return GNUNET_YES; /* part of another SCC, end trace */
1096 if (nsn->scc_id == fc->id)
1097 return GNUNET_YES; /* that's us */
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)
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "No updateable nodes found for ID `%s'\n",
1160 return; /* no nodes */
1162 if (namespace->update_map == NULL)
1164 /* need to construct */
1165 namespace->update_map = GNUNET_CONTAINER_multihashmap_create (2 + 3 * namespace->update_node_count / 4);
1166 for (i=0;i<namespace->update_node_count;i++)
1168 nsn = namespace->update_nodes[i];
1169 GNUNET_CRYPTO_hash (nsn->id,
1172 GNUNET_CONTAINER_multihashmap_put (namespace->update_map,
1175 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1178 if (next_id != NULL)
1180 GNUNET_CRYPTO_hash (next_id,
1185 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1187 &process_update_node,
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "Calculating SCCs to find roots of update trees\n");
1195 /* Find heads of SCCs in update graph */
1196 nug = ++namespace->nug_gen;
1197 fc.scc_array = NULL;
1198 fc.scc_array_size = 0;
1200 for (i=0;i<namespace->update_node_count;i++)
1202 nsn = namespace->update_nodes[i];
1203 if (nsn->nug == nug)
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "SCC of node `%s' is %u\n",
1211 continue; /* already placed in SCC */
1213 GNUNET_CRYPTO_hash (nsn->update,
1214 strlen (nsn->update),
1219 fc.namespace = namespace;
1220 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1224 if (fc.id == UINT_MAX)
1227 for (fc.id=0;fc.id<fc.scc_array_size;fc.id++)
1229 if (fc.scc_array[fc.id] == NULL)
1231 fc.scc_array[fc.id] = nsn;
1232 nsn->scc_id = fc.id;
1236 if (fc.id == fc.scc_array_size)
1238 GNUNET_array_append (fc.scc_array,
1241 nsn->scc_id = fc.id;
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Starting new SCC %u with node `%s'\n",
1249 /* put all nodes with same identifier into this SCC */
1250 GNUNET_CRYPTO_hash (nsn->id,
1253 fc.id = nsn->scc_id;
1255 fc.namespace = namespace;
1256 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1263 /* make head of SCC "id" */
1264 fc.scc_array[fc.id] = nsn;
1265 nsn->scc_id = fc.id;
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "SCC of node `%s' is %u\n",
1274 for (i=0;i<fc.scc_array_size;i++)
1276 nsn = fc.scc_array[i];
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "Root of SCC %u is node `%s'\n",
1293 GNUNET_array_grow (fc.scc_array,
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Done processing SCCs\n");
1303 /* end of fs_namespace.c */