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_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 if (ac->cont != NULL)
365 ac->cont (ac->cont_cls, NULL, msg);
366 GNUNET_FS_uri_destroy (ac->ksk_uri);
367 GNUNET_free (ac->pt);
368 GNUNET_free (ac->nb);
369 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
373 if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
376 GNUNET_SCHEDULER_add_continuation (ac->sched,
379 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
380 if (ac->cont != NULL)
381 ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
382 GNUNET_FS_uri_destroy (ac->ksk_uri);
383 GNUNET_free (ac->pt);
384 GNUNET_free (ac->nb);
385 GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
389 keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
390 /* first character of keyword indicates if it is
391 mandatory or not -- ignore for hashing */
392 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
393 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
394 GNUNET_CRYPTO_aes_encrypt (ac->pt,
399 GNUNET_break (GNUNET_OK ==
400 GNUNET_CRYPTO_rsa_sign (ac->ns->key,
402 &ac->nb->ns_signature));
403 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
404 GNUNET_assert (pk != NULL);
405 GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
406 GNUNET_CRYPTO_hash (&ac->nb->keyspace,
407 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
409 GNUNET_break (GNUNET_OK ==
410 GNUNET_CRYPTO_rsa_sign (pk,
411 &ac->nb->ksk_purpose,
412 &ac->nb->ksk_signature));
413 GNUNET_CRYPTO_rsa_key_free (pk);
414 GNUNET_DATASTORE_put (ac->dsh,
415 0 /* no reservation */,
417 ac->pt_size + sizeof (struct NBlock),
419 GNUNET_BLOCK_TYPE_FS_NBLOCK,
424 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
431 * Publish an advertismement for a namespace.
433 * @param h handle to the file sharing subsystem
434 * @param ksk_uri keywords to use for advertisment
435 * @param namespace handle for the namespace that should be advertised
436 * @param meta meta-data for the namespace advertisement
437 * @param anonymity for the namespace advertismement
438 * @param priority for the namespace advertisement
439 * @param expiration for the namespace advertisement
440 * @param rootEntry name of the root of the namespace
441 * @param cont continuation
442 * @param cont_cls closure for cont
445 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
446 struct GNUNET_FS_Uri *ksk_uri,
447 struct GNUNET_FS_Namespace *namespace,
448 const struct GNUNET_CONTAINER_MetaData *meta,
451 struct GNUNET_TIME_Absolute expiration,
452 const char *rootEntry,
453 GNUNET_FS_PublishContinuation cont,
461 struct GNUNET_DATASTORE_Handle *dsh;
462 struct AdvertisementContext *ctx;
465 /* create advertisements */
466 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
469 cont (cont_cls, NULL, _("Failed to serialize meta data"));
472 reslen = strlen (rootEntry) + 1;
473 size = mdsize + sizeof (struct NBlock) + reslen;
474 if (size > MAX_NBLOCK_SIZE)
476 size = MAX_NBLOCK_SIZE;
477 mdsize = size - sizeof (struct NBlock) - reslen;
480 pt = GNUNET_malloc (mdsize + reslen);
481 memcpy (pt, rootEntry, reslen);
483 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
486 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
491 cont (cont_cls, NULL, _("Failed to serialize meta data"));
494 size = mdsize + sizeof (struct NBlock) + reslen;
495 nb = GNUNET_malloc (size);
496 GNUNET_CRYPTO_rsa_key_get_public (namespace->key,
498 nb->ns_purpose.size = htonl (mdsize + reslen +
499 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
500 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
501 nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
502 nb->ksk_purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
503 nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
504 dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
509 cont (cont_cls, NULL, _("Failed to connect to datastore service"));
512 ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
514 ctx->cont_cls = cont_cls;
516 ctx->sched = h->sched;
517 ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
520 ctx->pt_size = mdsize + reslen;
523 ctx->anonymity = anonymity;
524 ctx->priority = priority;
525 ctx->expiration = expiration;
526 advertisement_cont (ctx, GNUNET_OK, NULL);
531 * Create a namespace with the given name; if one already
532 * exists, return a handle to the existing namespace.
534 * @param h handle to the file sharing subsystem
535 * @param name name to use for the namespace
536 * @return handle to the namespace, NULL on error
538 struct GNUNET_FS_Namespace *
539 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
544 struct GNUNET_FS_Namespace *ret;
546 dn = get_namespace_directory (h);
547 GNUNET_asprintf (&fn,
553 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
556 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
557 if (ret->key == NULL)
559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
560 _("Failed to create or read private key for namespace `%s'\n"),
566 ret->name = GNUNET_strdup (name);
573 * Delete a namespace handle. Can be used for a clean shutdown (free
574 * memory) or also to freeze the namespace to prevent further
575 * insertions by anyone.
577 * @param namespace handle to the namespace that should be deleted / freed
578 * @param freeze prevents future insertions; creating a namespace
579 * with the same name again will create a fresh namespace instead
581 * @return GNUNET_OK on success, GNUNET_SYSERR on error
584 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
588 struct NamespaceUpdateNode *nsn;
593 if (0 != UNLINK (namespace->filename))
594 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
596 namespace->filename);
598 if (0 == namespace->rc)
600 GNUNET_CRYPTO_rsa_key_free (namespace->key);
601 GNUNET_free (namespace->filename);
602 GNUNET_free (namespace->name);
603 for (i=0;i<namespace->update_node_count;i++)
605 nsn = namespace->update_nodes[i];
606 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
607 GNUNET_FS_uri_destroy (nsn->uri);
608 GNUNET_free (nsn->id);
609 GNUNET_free (nsn->update);
612 GNUNET_array_grow (namespace->update_nodes,
613 namespace->update_node_count,
615 if (namespace->update_map != NULL)
616 GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
617 GNUNET_free (namespace);
624 * Context for the 'process_namespace' callback.
625 * Specifies a function to call on each namespace.
627 struct ProcessNamespaceContext
632 GNUNET_FS_NamespaceInfoProcessor cb;
642 * Function called with a filename of a namespace. Reads the key and
643 * calls the callback.
645 * @param cls closure (struct ProcessNamespaceContext)
646 * @param filename complete filename (absolute path)
647 * @return GNUNET_OK to continue to iterate,
648 * GNUNET_SYSERR to abort iteration with error!
651 process_namespace (void *cls,
652 const char *filename)
654 struct ProcessNamespaceContext *pnc = cls;
655 struct GNUNET_CRYPTO_RsaPrivateKey *key;
656 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
661 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
665 _("Failed to read namespace private key file `%s', deleting it!\n"),
667 if (0 != UNLINK (filename))
668 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
673 GNUNET_CRYPTO_rsa_key_get_public (key, &pk);
674 GNUNET_CRYPTO_rsa_key_free (key);
675 GNUNET_CRYPTO_hash (&pk, sizeof(pk), &id);
677 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
679 pnc->cb (pnc->cb_cls,
687 * Build a list of all available local (!) namespaces The returned
688 * names are only the nicknames since we only iterate over the local
691 * @param h handle to the file sharing subsystem
692 * @param cb function to call on each known namespace
693 * @param cb_cls closure for cb
696 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
697 GNUNET_FS_NamespaceInfoProcessor cb,
701 struct ProcessNamespaceContext ctx;
703 dn = get_namespace_directory (h);
708 GNUNET_DISK_directory_scan (dn,
718 * Context for the SKS publication.
720 struct PublishSksContext
724 * URI of the new entry in the namespace.
726 struct GNUNET_FS_Uri *uri;
729 * Namespace update node to add to namespace on success (or to be
730 * deleted if publishing failed).
732 struct NamespaceUpdateNode *nsn;
735 * Namespace we're publishing to.
737 struct GNUNET_FS_Namespace *namespace;
740 * Handle to the datastore.
742 struct GNUNET_DATASTORE_Handle *dsh;
745 * Function to call once we're done.
747 GNUNET_FS_PublishContinuation cont;
758 * Function called by the datastore API with
759 * the result from the PUT (SBlock) request.
761 * @param cls closure of type "struct PublishSksContext*"
762 * @param success GNUNET_OK on success
763 * @param msg error message (or NULL)
766 sb_put_cont (void *cls,
770 struct PublishSksContext *psc = cls;
773 if (NULL != psc->dsh)
775 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
778 if (GNUNET_OK != success)
780 if (psc->cont != NULL)
781 psc->cont (psc->cont_cls,
787 if (psc->nsn != NULL)
789 /* FIXME: this can be done much more
790 efficiently by simply appending to the
791 file and overwriting the 4-byte header */
792 if (psc->namespace->update_nodes == NULL)
793 read_update_information_graph (psc->namespace);
794 GNUNET_array_append (psc->namespace->update_nodes,
795 psc->namespace->update_node_count,
797 if (psc->namespace->update_map != NULL)
799 GNUNET_CRYPTO_hash (psc->nsn->id,
800 strlen (psc->nsn->id),
802 GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map,
805 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
808 write_update_information_graph (psc->namespace);
810 if (psc->cont != NULL)
811 psc->cont (psc->cont_cls,
815 GNUNET_FS_namespace_delete (psc->namespace,
817 GNUNET_FS_uri_destroy (psc->uri);
818 if (psc->nsn != NULL)
820 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
821 GNUNET_FS_uri_destroy (psc->nsn->uri);
822 GNUNET_free (psc->nsn->id);
823 GNUNET_free (psc->nsn->update);
824 GNUNET_free (psc->nsn);
831 * Publish an SBlock on GNUnet.
833 * @param h handle to the file sharing subsystem
834 * @param namespace namespace to publish in
835 * @param identifier identifier to use
836 * @param update update identifier to use
837 * @param meta metadata to use
838 * @param uri URI to refer to in the SBlock
839 * @param expirationTime when the SBlock expires
840 * @param anonymity anonymity level for the SBlock
841 * @param priority priority for the SBlock
842 * @param options publication options
843 * @param cont continuation
844 * @param cont_cls closure for cont
847 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
848 struct GNUNET_FS_Namespace *namespace,
849 const char *identifier,
851 const struct GNUNET_CONTAINER_MetaData *meta,
852 const struct GNUNET_FS_Uri *uri,
853 struct GNUNET_TIME_Absolute expirationTime,
856 enum GNUNET_FS_PublishOptions options,
857 GNUNET_FS_PublishContinuation cont,
860 struct PublishSksContext *psc;
861 struct GNUNET_CRYPTO_AesSessionKey sk;
862 struct GNUNET_CRYPTO_AesInitializationVector iv;
863 struct GNUNET_FS_Uri *sks_uri;
871 struct SBlock *sb_enc;
873 struct GNUNET_CONTAINER_MetaData *mmeta;
874 GNUNET_HashCode key; /* hash of thisId = key */
875 GNUNET_HashCode id; /* hash of hc = identifier */
876 GNUNET_HashCode query; /* id ^ nsid = DB query */
879 mmeta = GNUNET_CONTAINER_meta_data_create ();
881 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
882 uris = GNUNET_FS_uri_to_string (uri);
883 slen = strlen (uris) + 1;
884 idlen = strlen (identifier);
887 nidlen = strlen (update) + 1;
888 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
889 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
890 if (size > MAX_SBLOCK_SIZE)
892 size = MAX_SBLOCK_SIZE;
893 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
895 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
896 dest = (char *) &sb[1];
897 memcpy (dest, update, nidlen);
899 memcpy (dest, uris, slen);
902 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
905 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
906 GNUNET_CONTAINER_meta_data_destroy (mmeta);
913 _("Internal error."));
916 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
917 sb_enc = GNUNET_malloc (size);
918 GNUNET_CRYPTO_hash (identifier, idlen, &key);
919 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
920 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
922 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
923 GNUNET_CRYPTO_hash (&sb_enc->subspace,
924 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
925 &sks_uri->data.sks.namespace);
926 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
927 GNUNET_CRYPTO_hash_xor (&id,
928 &sks_uri->data.sks.namespace,
929 &sb_enc->identifier);
930 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
931 GNUNET_CRYPTO_aes_encrypt (&sb[1],
932 size - sizeof (struct SBlock),
936 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
937 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
938 + sizeof(struct SBlock)
939 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
940 GNUNET_assert (GNUNET_OK ==
941 GNUNET_CRYPTO_rsa_sign (namespace->key,
943 &sb_enc->signature));
944 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
947 psc->namespace = namespace;
949 psc->cont_cls = cont_cls;
950 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
952 GNUNET_free (sb_enc);
959 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
960 if (NULL == psc->dsh)
962 GNUNET_free (sb_enc);
966 _("Failed to connect to datastore."));
969 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
974 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
975 psc->nsn->id = GNUNET_strdup (identifier);
976 psc->nsn->update = GNUNET_strdup (update);
977 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
978 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
980 GNUNET_DATASTORE_put (psc->dsh,
985 GNUNET_BLOCK_TYPE_FS_SBLOCK,
990 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
994 GNUNET_free (sb_enc);
999 * Closure for 'process_update_node'.
1001 struct ProcessUpdateClosure
1004 * Function to call for each node.
1006 GNUNET_FS_IdentifierProcessor ip;
1016 * Call the iterator in the closure for each node.
1018 * @param cls closure (of type 'struct ProcessUpdateClosure *')
1019 * @param key current key code
1020 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
1021 * @return GNUNET_YES if we should continue to
1026 process_update_node (void *cls,
1027 const GNUNET_HashCode * key,
1030 struct ProcessUpdateClosure *pc = cls;
1031 struct NamespaceUpdateNode *nsn = value;
1043 * Closure for 'find_sccs'.
1045 struct FindSccClosure
1048 * Namespace we are operating on.
1050 struct GNUNET_FS_Namespace *namespace;
1053 * Array with 'head's of SCCs.
1055 struct NamespaceUpdateNode **scc_array;
1058 * Size of 'scc_array'
1060 unsigned int scc_array_size;
1063 * Current generational ID used.
1068 * Identifier for the current SCC, or UINT_MAX for none yet.
1075 * Find all nodes reachable from the current node (including the
1076 * current node itself). If they are in no SCC, add them to the
1077 * current one. If they are the head of another SCC, merge the
1078 * SCCs. If they are in the middle of another SCC, let them be.
1079 * We can tell that a node is already in an SCC by checking if
1080 * its 'nug' field is set to the current 'nug' value. It is the
1081 * head of an SCC if it is in the 'scc_array' under its respective
1084 * @param cls closure (of type 'struct FindSccClosure')
1085 * @param key current key code
1086 * @param value value in the hash map
1087 * @return GNUNET_YES if we should continue to
1092 find_sccs (void *cls,
1093 const GNUNET_HashCode * key,
1096 struct FindSccClosure *fc = cls;
1097 struct NamespaceUpdateNode *nsn = value;
1100 if (nsn->nug == fc->nug)
1102 if (fc->scc_array[nsn->scc_id] != nsn)
1103 return GNUNET_YES; /* part of another SCC, end trace */
1104 if (nsn->scc_id == fc->id)
1105 return GNUNET_YES; /* that's us */
1106 fc->scc_array[nsn->scc_id] = NULL;
1107 if (fc->id == UINT_MAX)
1108 fc->id = nsn->scc_id; /* take over ID */
1114 GNUNET_CRYPTO_hash (nsn->update,
1115 strlen (nsn->update),
1117 GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map,
1127 * List all of the identifiers in the namespace for which we could
1128 * produce an update. Namespace updates form a graph where each node
1129 * has a name. Each node can have any number of URI/meta-data entries
1130 * which can each be linked to other nodes. Cycles are possible.
1132 * Calling this function with "next_id" NULL will cause the library to
1133 * call "ip" with a root for each strongly connected component of the
1134 * graph (a root being a node from which all other nodes in the Scc
1137 * Calling this function with "next_id" being the name of a node will
1138 * cause the library to call "ip" with all children of the node. Note
1139 * that cycles within an SCC are possible (including self-loops).
1141 * @param namespace namespace to inspect for updateable content
1142 * @param next_id ID to look for; use NULL to look for SCC roots
1143 * @param ip function to call on each updateable identifier
1144 * @param ip_cls closure for ip
1147 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1148 const char *next_id,
1149 GNUNET_FS_IdentifierProcessor ip,
1155 struct NamespaceUpdateNode *nsn;
1156 struct ProcessUpdateClosure pc;
1157 struct FindSccClosure fc;
1159 if (namespace->update_nodes == NULL)
1160 read_update_information_graph (namespace);
1161 if (namespace->update_nodes == NULL)
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "No updateable nodes found for ID `%s'\n",
1168 return; /* no nodes */
1170 if (namespace->update_map == NULL)
1172 /* need to construct */
1173 namespace->update_map = GNUNET_CONTAINER_multihashmap_create (2 + 3 * namespace->update_node_count / 4);
1174 for (i=0;i<namespace->update_node_count;i++)
1176 nsn = namespace->update_nodes[i];
1177 GNUNET_CRYPTO_hash (nsn->id,
1180 GNUNET_CONTAINER_multihashmap_put (namespace->update_map,
1183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1186 if (next_id != NULL)
1188 GNUNET_CRYPTO_hash (next_id,
1193 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1195 &process_update_node,
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Calculating SCCs to find roots of update trees\n");
1203 /* Find heads of SCCs in update graph */
1204 nug = ++namespace->nug_gen;
1205 fc.scc_array = NULL;
1206 fc.scc_array_size = 0;
1208 for (i=0;i<namespace->update_node_count;i++)
1210 nsn = namespace->update_nodes[i];
1211 if (nsn->nug == nug)
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "SCC of node `%s' is %u\n",
1219 continue; /* already placed in SCC */
1221 GNUNET_CRYPTO_hash (nsn->update,
1222 strlen (nsn->update),
1227 fc.namespace = namespace;
1228 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1232 if (fc.id == UINT_MAX)
1235 for (fc.id=0;fc.id<fc.scc_array_size;fc.id++)
1237 if (fc.scc_array[fc.id] == NULL)
1239 fc.scc_array[fc.id] = nsn;
1240 nsn->scc_id = fc.id;
1244 if (fc.id == fc.scc_array_size)
1246 GNUNET_array_append (fc.scc_array,
1249 nsn->scc_id = fc.id;
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Starting new SCC %u with node `%s'\n",
1257 /* put all nodes with same identifier into this SCC */
1258 GNUNET_CRYPTO_hash (nsn->id,
1261 fc.id = nsn->scc_id;
1263 fc.namespace = namespace;
1264 GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map,
1271 /* make head of SCC "id" */
1272 fc.scc_array[fc.id] = nsn;
1273 nsn->scc_id = fc.id;
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277 "SCC of node `%s' is %u\n",
1282 for (i=0;i<fc.scc_array_size;i++)
1284 nsn = fc.scc_array[i];
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289 "Root of SCC %u is node `%s'\n",
1301 GNUNET_array_grow (fc.scc_array,
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "Done processing SCCs\n");
1311 /* end of fs_namespace.c */