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);
167 rh = GNUNET_BIO_read_open (fn);
174 GNUNET_BIO_read_int32 (rh, &count))
179 if (count > 1024 * 1024)
186 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
190 ns->update_nodes = GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode*));
192 for (i=0;i<count;i++)
194 n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
196 GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024)) ||
198 GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
200 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
202 GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)) )
205 GNUNET_free_non_null (n->id);
206 GNUNET_free_non_null (n->update);
208 GNUNET_CONTAINER_meta_data_destroy (n->md);
212 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
219 GNUNET_free (n->update);
220 GNUNET_CONTAINER_meta_data_destroy (n->md);
224 ns->update_nodes[i] = n;
226 ns->update_node_count = i;
228 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 _("Failed to write `%s': %s\n"),
240 * Context for advertising a namespace.
242 struct AdvertisementContext
245 * Function to call with the result.
247 GNUNET_FS_PublishContinuation cont;
257 struct GNUNET_DATASTORE_Handle *dsh;
262 struct GNUNET_SCHEDULER_Handle *sched;
267 struct GNUNET_FS_Uri *ksk_uri;
275 * NBlock to sign and store.
282 struct GNUNET_FS_Namespace *ns;
287 struct GNUNET_TIME_Absolute expiration;
290 * Number of bytes of plaintext.
305 * Current keyword offset.
312 * Disconnect from the datastore.
314 * @param cls datastore handle
315 * @param tc scheduler context
318 do_disconnect (void *cls,
319 const struct GNUNET_SCHEDULER_TaskContext *tc)
321 struct GNUNET_DATASTORE_Handle *dsh = cls;
323 GNUNET_DATASTORE_disconnect (dsh,
329 * Continuation called to notify client about result of the
332 * @param cls closure (our struct AdvertismentContext)
333 * @param success GNUNET_SYSERR on failure
334 * @param msg NULL on success, otherwise an error message
337 advertisement_cont (void *cls,
341 struct AdvertisementContext *ac = cls;
344 GNUNET_HashCode query;
345 struct GNUNET_CRYPTO_AesSessionKey skey;
346 struct GNUNET_CRYPTO_AesInitializationVector iv;
347 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
349 if (GNUNET_OK != success)
352 GNUNET_SCHEDULER_add_continuation (ac->sched,
355 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
356 ac->cont (ac->cont_cls, NULL, msg);
357 GNUNET_FS_uri_destroy (ac->ksk_uri);
358 GNUNET_free (ac->pt);
359 GNUNET_free (ac->nb);
360 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
364 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
367 GNUNET_SCHEDULER_add_continuation (ac->sched,
370 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
371 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
372 GNUNET_FS_uri_destroy (ac->ksk_uri);
373 GNUNET_free (ac->pt);
374 GNUNET_free (ac->nb);
375 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
379 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
380 /* first character of keyword indicates if it is
381 mandatory or not -- ignore for hashing */
382 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
383 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
384 GNUNET_CRYPTO_aes_encrypt (ac->pt,
389 GNUNET_break (GNUNET_OK ==
390 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
392 &ac->nb->ns_signature));
393 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
394 GNUNET_assert (pk != NULL);
395 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
396 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
397 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
399 GNUNET_break (GNUNET_OK ==
400 GNUNET_CRYPTO_rsa_sign (pk,
401 &ac->nb->ksk_purpose,
402 &ac->nb->ksk_signature));
403 GNUNET_CRYPTO_rsa_key_free (pk);
404 GNUNET_DATASTORE_put (ac->dsh,
405 0 /* no reservation */,
407 ac->pt_size + sizeof (struct NBlock),
409 GNUNET_BLOCK_TYPE_NBLOCK,
414 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
421 * Publish an advertismement for a namespace.
423 * @param h handle to the file sharing subsystem
424 * @param ksk_uri keywords to use for advertisment
425 * @param namespace handle for the namespace that should be advertised
426 * @param meta meta-data for the namespace advertisement
427 * @param anonymity for the namespace advertismement
428 * @param priority for the namespace advertisement
429 * @param expiration for the namespace advertisement
430 * @param rootEntry name of the root of the namespace
431 * @param cont continuation
432 * @param cont_cls closure for cont
435 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
436 struct GNUNET_FS_Uri *ksk_uri,
437 struct GNUNET_FS_Namespace *namespace,
438 const struct GNUNET_CONTAINER_MetaData *meta,
441 struct GNUNET_TIME_Absolute expiration,
442 const char *rootEntry,
443 GNUNET_FS_PublishContinuation cont,
451 struct GNUNET_DATASTORE_Handle *dsh;
452 struct AdvertisementContext *ctx;
455 /* create advertisements */
456 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
459 cont (cont_cls, NULL, _("Failed to serialize meta data"));
462 reslen = strlen (rootEntry) + 1;
463 size = mdsize + sizeof (struct NBlock) + reslen;
464 if (size > MAX_NBLOCK_SIZE)
466 size = MAX_NBLOCK_SIZE;
467 mdsize = size - sizeof (struct NBlock) - reslen;
470 pt = GNUNET_malloc (mdsize + reslen);
471 memcpy (pt, rootEntry, reslen);
473 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
476 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
481 cont (cont_cls, NULL, _("Failed to serialize meta data"));
484 size = mdsize + sizeof (struct NBlock) + reslen;
485 nb = GNUNET_malloc (size);
486 GNUNET_CRYPTO_rsa_key_get_public (namespace->key,
488 nb->ns_purpose.size = htonl (mdsize + reslen +
489 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
490 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
491 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
492 nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
493 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
494 dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
499 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
502 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
504 ctx->cont_cls = cont_cls;
506 ctx->sched = h->sched;
507 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
510 ctx->pt_size = mdsize + reslen;
513 ctx->anonymity = anonymity;
514 ctx->priority = priority;
515 ctx->expiration = expiration;
516 advertisement_cont (ctx, GNUNET_OK, NULL);
521 * Create a namespace with the given name; if one already
522 * exists, return a handle to the existing namespace.
524 * @param h handle to the file sharing subsystem
525 * @param name name to use for the namespace
526 * @return handle to the namespace, NULL on error
528 struct GNUNET_FS_Namespace *
529 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
534 struct GNUNET_FS_Namespace *ret;
536 dn = get_namespace_directory (h);
537 GNUNET_asprintf (&fn,
543 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
546 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
547 if (ret->key == NULL)
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 _("Failed to create or read private key for namespace `%s'\n"),
556 ret->name = GNUNET_strdup (name);
563 * Delete a namespace handle. Can be used for a clean shutdown (free
564 * memory) or also to freeze the namespace to prevent further
565 * insertions by anyone.
567 * @param namespace handle to the namespace that should be deleted / freed
568 * @param freeze prevents future insertions; creating a namespace
569 * with the same name again will create a fresh namespace instead
571 * @return GNUNET_OK on success, GNUNET_SYSERR on error
574 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
578 struct NamespaceUpdateNode *nsn;
583 if (0 != UNLINK (namespace->filename))
584 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
586 namespace->filename);
588 if (0 == namespace->rc)
590 GNUNET_CRYPTO_rsa_key_free (namespace->key);
591 GNUNET_free (namespace->filename);
592 GNUNET_free (namespace->name);
593 for (i=0;i<namespace->update_node_count;i++)
595 nsn = namespace->update_nodes[i];
596 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
597 GNUNET_FS_uri_destroy (nsn->uri);
598 GNUNET_free (nsn->id);
599 GNUNET_free (nsn->update);
602 GNUNET_array_grow (namespace->update_nodes,
603 namespace->update_node_count,
605 if (namespace->update_map != NULL)
606 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
607 GNUNET_free (namespace);
614 * Context for the 'process_namespace' callback.
615 * Specifies a function to call on each namespace.
617 struct ProcessNamespaceContext
622 GNUNET_FS_NamespaceInfoProcessor cb;
632 * Function called with a filename of a namespace. Reads the key and
633 * calls the callback.
635 * @param cls closure (struct ProcessNamespaceContext)
636 * @param filename complete filename (absolute path)
637 * @return GNUNET_OK to continue to iterate,
638 * GNUNET_SYSERR to abort iteration with error!
641 process_namespace (void *cls,
642 const char *filename)
644 struct ProcessNamespaceContext *pnc = cls;
645 struct GNUNET_CRYPTO_RsaPrivateKey *key;
646 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
651 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
655 _("Failed to read namespace private key file `%s', deleting it!\n"),
657 if (0 != UNLINK (filename))
658 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
663 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
664 GNUNET_CRYPTO_rsa_key_free (key);
665 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
667 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
669 pnc->cb (pnc->cb_cls,
677 * Build a list of all available local (!) namespaces The returned
678 * names are only the nicknames since we only iterate over the local
681 * @param h handle to the file sharing subsystem
682 * @param cb function to call on each known namespace
683 * @param cb_cls closure for cb
686 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
687 GNUNET_FS_NamespaceInfoProcessor cb,
691 struct ProcessNamespaceContext ctx;
693 dn = get_namespace_directory (h);
698 GNUNET_DISK_directory_scan (dn,
708 * Context for the SKS publication.
710 struct PublishSksContext
714 * URI of the new entry in the namespace.
716 struct GNUNET_FS_Uri *uri;
719 * Namespace update node to add to namespace on success (or to be
720 * deleted if publishing failed).
722 struct NamespaceUpdateNode *nsn;
725 * Namespace we're publishing to.
727 struct GNUNET_FS_Namespace *namespace;
730 * Handle to the datastore.
732 struct GNUNET_DATASTORE_Handle *dsh;
735 * Function to call once we're done.
737 GNUNET_FS_PublishContinuation cont;
748 * Function called by the datastore API with
749 * the result from the PUT (SBlock) request.
751 * @param cls closure of type "struct PublishSksContext*"
752 * @param success GNUNET_OK on success
753 * @param msg error message (or NULL)
756 sb_put_cont (void *cls,
760 struct PublishSksContext *psc = cls;
763 if (NULL != psc->dsh)
765 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
768 if (GNUNET_OK != success)
770 psc->cont (psc->cont_cls,
776 if (psc->nsn != NULL)
778 /* FIXME: this can be done much more
779 efficiently by simply appending to the
780 file and overwriting the 4-byte header */
781 if (psc->namespace->update_nodes == NULL)
782 read_update_information_graph (psc->namespace);
783 GNUNET_array_append (psc->namespace->update_nodes,
784 psc->namespace->update_node_count,
786 if (psc->namespace->update_map != NULL)
788 GNUNET_CRYPTO_hash (psc->nsn->id,
789 strlen (psc->nsn->id),
791 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map,
794 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
797 write_update_information_graph (psc->namespace);
799 psc->cont (psc->cont_cls,
803 GNUNET_FS_namespace_delete (psc->namespace,
805 GNUNET_FS_uri_destroy (psc->uri);
806 if (psc->nsn != NULL)
808 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
809 GNUNET_FS_uri_destroy (psc->nsn->uri);
810 GNUNET_free (psc->nsn->id);
811 GNUNET_free (psc->nsn->update);
812 GNUNET_free (psc->nsn);
819 * Publish an SBlock on GNUnet.
821 * @param h handle to the file sharing subsystem
822 * @param namespace namespace to publish in
823 * @param identifier identifier to use
824 * @param update update identifier to use
825 * @param meta metadata to use
826 * @param uri URI to refer to in the SBlock
827 * @param expirationTime when the SBlock expires
828 * @param anonymity anonymity level for the SBlock
829 * @param priority priority for the SBlock
830 * @param options publication options
831 * @param cont continuation
832 * @param cont_cls closure for cont
835 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
836 struct GNUNET_FS_Namespace *namespace,
837 const char *identifier,
839 const struct GNUNET_CONTAINER_MetaData *meta,
840 const struct GNUNET_FS_Uri *uri,
841 struct GNUNET_TIME_Absolute expirationTime,
844 enum GNUNET_FS_PublishOptions options,
845 GNUNET_FS_PublishContinuation cont,
848 struct PublishSksContext *psc;
849 struct GNUNET_CRYPTO_AesSessionKey sk;
850 struct GNUNET_CRYPTO_AesInitializationVector iv;
851 struct GNUNET_FS_Uri *sks_uri;
859 struct SBlock *sb_enc;
861 struct GNUNET_CONTAINER_MetaData *mmeta;
862 GNUNET_HashCode key; /* hash of thisId = key */
863 GNUNET_HashCode id; /* hash of hc = identifier */
864 GNUNET_HashCode query; /* id ^ nsid = DB query */
867 mmeta = GNUNET_CONTAINER_meta_data_create ();
869 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
870 uris = GNUNET_FS_uri_to_string (uri);
871 slen = strlen (uris) + 1;
872 idlen = strlen (identifier);
875 nidlen = strlen (update) + 1;
876 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
877 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
878 if (size > MAX_SBLOCK_SIZE)
880 size = MAX_SBLOCK_SIZE;
881 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
883 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
884 dest = (char *) &sb[1];
885 memcpy (dest, update, nidlen);
887 memcpy (dest, uris, slen);
890 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
893 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
894 GNUNET_CONTAINER_meta_data_destroy (mmeta);
901 _("Internal error."));
904 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
905 sb_enc = GNUNET_malloc (size);
906 GNUNET_CRYPTO_hash (identifier, idlen, &key);
907 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
908 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
910 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
911 GNUNET_CRYPTO_hash (&sb_enc->subspace,
912 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
913 &sks_uri->data.sks.namespace);
914 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
915 GNUNET_CRYPTO_hash_xor (&id,
916 &sks_uri->data.sks.namespace,
917 &sb_enc->identifier);
918 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
919 GNUNET_CRYPTO_aes_encrypt (&sb[1],
920 size - sizeof (struct SBlock),
924 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
925 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
926 + sizeof(struct SBlock)
927 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
928 GNUNET_assert (GNUNET_OK ==
929 GNUNET_CRYPTO_rsa_sign (namespace->key,
931 &sb_enc->signature));
932 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
935 psc->namespace = namespace;
937 psc->cont_cls = cont_cls;
938 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
940 GNUNET_free (sb_enc);
947 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
948 if (NULL == psc->dsh)
950 GNUNET_free (sb_enc);
954 _("Failed to connect to datastore."));
957 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
962 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
963 psc->nsn->id = GNUNET_strdup (identifier);
964 psc->nsn->update = GNUNET_strdup (update);
965 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
966 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
968 GNUNET_DATASTORE_put (psc->dsh,
973 GNUNET_BLOCK_TYPE_SBLOCK,
978 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
982 GNUNET_free (sb_enc);
987 * Closure for 'process_update_node'.
989 struct ProcessUpdateClosure
992 * Function to call for each node.
994 GNUNET_FS_IdentifierProcessor ip;
1004 * Call the iterator in the closure for each node.
1006 * @param cls closure (of type 'struct ProcessUpdateClosure *')
1007 * @param key current key code
1008 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
1009 * @return GNUNET_YES if we should continue to
1014 process_update_node (void *cls,
1015 const GNUNET_HashCode * key,
1018 struct ProcessUpdateClosure *pc = cls;
1019 struct NamespaceUpdateNode *nsn = value;
1031 * Closure for 'find_sccs'.
1033 struct FindSccClosure
1036 * Namespace we are operating on.
1038 struct GNUNET_FS_Namespace *namespace;
1041 * Array with 'head's of SCCs.
1043 struct NamespaceUpdateNode **scc_array;
1046 * Size of 'scc_array'
1048 unsigned int scc_array_size;
1051 * Current generational ID used.
1056 * Identifier for the current SCC, or UINT_MAX for none yet.
1063 * Find all nodes reachable from the current node (including the
1064 * current node itself). If they are in no SCC, add them to the
1065 * current one. If they are the head of another SCC, merge the
1066 * SCCs. If they are in the middle of another SCC, let them be.
1067 * We can tell that a node is already in an SCC by checking if
1068 * its 'nug' field is set to the current 'nug' value. It is the
1069 * head of an SCC if it is in the 'scc_array' under its respective
1072 * @param cls closure (of type 'struct FindSccClosure')
1073 * @param key current key code
1074 * @param value value in the hash map
1075 * @return GNUNET_YES if we should continue to
1080 find_sccs (void *cls,
1081 const GNUNET_HashCode * key,
1084 struct FindSccClosure *fc = cls;
1085 struct NamespaceUpdateNode *nsn = value;
1088 if (nsn->nug == fc->nug)
1090 if (fc->scc_array[nsn->scc_id] != nsn)
1091 return GNUNET_YES; /* part of another SCC, end trace */
1092 fc->scc_array[nsn->scc_id] = NULL;
1093 if (fc->id == UINT_MAX)
1094 fc->id = nsn->scc_id; /* take over ID */
1100 GNUNET_CRYPTO_hash (nsn->update,
1101 strlen (nsn->update),
1103 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map,
1113 * List all of the identifiers in the namespace for which we could
1114 * produce an update. Namespace updates form a graph where each node
1115 * has a name. Each node can have any number of URI/meta-data entries
1116 * which can each be linked to other nodes. Cycles are possible.
1118 * Calling this function with "next_id" NULL will cause the library to
1119 * call "ip" with a root for each strongly connected component of the
1120 * graph (a root being a node from which all other nodes in the Scc
1123 * Calling this function with "next_id" being the name of a node will
1124 * cause the library to call "ip" with all children of the node. Note
1125 * that cycles within an SCC are possible (including self-loops).
1127 * @param namespace namespace to inspect for updateable content
1128 * @param next_id ID to look for; use NULL to look for SCC roots
1129 * @param ip function to call on each updateable identifier
1130 * @param ip_cls closure for ip
1133 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1134 const char *next_id,
1135 GNUNET_FS_IdentifierProcessor ip,
1141 struct NamespaceUpdateNode *nsn;
1142 struct ProcessUpdateClosure pc;
1143 struct FindSccClosure fc;
1145 if (namespace->update_nodes == NULL)
1146 read_update_information_graph (namespace);
1147 if (namespace->update_nodes == NULL)
1148 return; /* no nodes */
1149 if (namespace->update_map == NULL)
1151 /* need to construct */
1152 namespace->update_map = GNUNET_CONTAINER_multihashmap_create (2 + 3 * namespace->update_node_count / 4);
1153 for (i=0;i<namespace->update_node_count;i++)
1155 nsn = namespace->update_nodes[i];
1156 GNUNET_CRYPTO_hash (nsn->id,
1159 GNUNET_CONTAINER_multihashmap_put (namespace->update_map,
1162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1165 if (next_id != NULL)
1167 GNUNET_CRYPTO_hash (next_id,
1172 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1174 &process_update_node,
1178 /* Find heads of SCCs in update graph */
1179 nug = ++namespace->nug_gen;
1180 fc.scc_array = NULL;
1181 fc.scc_array_size = 0;
1183 for (i=0;i<namespace->update_node_count;i++)
1185 nsn = namespace->update_nodes[i];
1186 if (nsn->nug == nug)
1187 continue; /* already placed in SCC */
1188 GNUNET_CRYPTO_hash (nsn->update,
1189 strlen (nsn->update),
1194 fc.namespace = namespace;
1195 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1199 if (fc.id == UINT_MAX)
1202 for (fc.id=0;fc.id<fc.scc_array_size;fc.id++)
1204 if (fc.scc_array[fc.id] == NULL)
1206 fc.scc_array[fc.id] = nsn;
1207 nsn->scc_id = fc.id;
1211 if (fc.id == fc.scc_array_size)
1213 GNUNET_array_append (fc.scc_array,
1216 nsn->scc_id = fc.id;
1218 /* put all nodes with same identifier into this SCC */
1219 GNUNET_CRYPTO_hash (nsn->id,
1222 fc.id = nsn->scc_id;
1224 fc.namespace = namespace;
1225 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1232 /* make head of SCC "id" */
1233 fc.scc_array[fc.id] = nsn;
1234 nsn->scc_id = fc.id;
1237 for (i=0;i<fc.scc_array_size;i++)
1239 nsn = fc.scc_array[i];
1246 GNUNET_array_grow (fc.scc_array,
1252 /* end of fs_namespace.c */