2 This file is part of GNUnet
3 (C) 2003-2013 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 publishing to namespaces, and tracking updateable entries
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"
32 #include "fs_publish_ublock.h"
36 * Information about an (updateable) node in the
39 struct NamespaceUpdateNode
42 * Identifier for this node.
47 * Identifier of children of this node.
52 * Metadata for this entry.
54 struct GNUNET_CONTAINER_MetaData *md;
57 * URI of this entry in the namespace.
59 struct GNUNET_FS_Uri *uri;
62 * Namespace update generation ID. Used to ensure
63 * freshness of the tree_id.
68 * TREE this entry belongs to (if nug is current).
76 * Handle to update information for a namespace.
78 struct GNUNET_FS_UpdateInformationGraph
82 * Handle to the FS service context.
84 struct GNUNET_FS_Handle *h;
87 * Array with information about nodes in the namespace.
89 struct NamespaceUpdateNode **update_nodes;
92 * Private key for the namespace.
94 struct GNUNET_CRYPTO_EccPrivateKey ns;
97 * Hash map mapping identifiers of update nodes
98 * to the update nodes (initialized on-demand).
100 struct GNUNET_CONTAINER_MultiHashMap *update_map;
103 * Size of the update nodes array.
105 unsigned int update_node_count;
113 * Generator for unique nug numbers.
115 unsigned int nug_gen;
120 * Return the name of the directory in which we store
121 * the update information graph for the given local namespace.
123 * @param h file-sharing handle
124 * @param ns namespace handle
125 * @return NULL on error, otherwise the name of the directory
128 get_update_information_directory (struct GNUNET_FS_Handle *h,
129 const struct GNUNET_CRYPTO_EccPrivateKey *ns)
133 struct GNUNET_CRYPTO_EccPublicKey pub;
134 struct GNUNET_CRYPTO_ShortHashCode hc;
135 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
138 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "UPDATE_DIR",
141 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
145 GNUNET_CRYPTO_ecc_key_get_public (ns, &pub);
146 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &hc);
147 GNUNET_CRYPTO_short_hash_to_enc (&hc,
149 GNUNET_asprintf (&ret, "%s%s%s",
152 (const char *) enc.short_encoding);
159 * Release memory occupied by UIG datastructure.
161 * @param uig data structure to free
164 free_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig)
167 struct NamespaceUpdateNode *nsn;
169 for (i = 0; i < uig->update_node_count; i++)
171 nsn = uig->update_nodes[i];
172 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
173 GNUNET_FS_uri_destroy (nsn->uri);
174 GNUNET_free (nsn->id);
175 GNUNET_free (nsn->update);
178 GNUNET_array_grow (uig->update_nodes, uig->update_node_count,
180 if (NULL != uig->update_map)
181 GNUNET_CONTAINER_multihashmap_destroy (uig->update_map);
187 * Write a namespace's update node graph to a file.
189 * @param uig update information graph to dump
192 write_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig)
195 struct GNUNET_BIO_WriteHandle *wh;
197 struct NamespaceUpdateNode *n;
200 fn = get_update_information_directory (uig->h,
202 wh = GNUNET_BIO_write_open (fn);
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
210 if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, uig->update_node_count))
212 for (i = 0; i < uig->update_node_count; i++)
214 n = uig->update_nodes[i];
215 uris = GNUNET_FS_uri_to_string (n->uri);
216 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) ||
217 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) ||
218 (GNUNET_OK != GNUNET_BIO_write_string (wh, n->update)) ||
219 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
227 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"),
235 * Read the namespace update node graph from a file.
237 * @param h FS handle to use
238 * @param ns namespace to read
239 * @return update graph, never NULL
241 static struct GNUNET_FS_UpdateInformationGraph *
242 read_update_information_graph (struct GNUNET_FS_Handle *h,
243 const struct GNUNET_CRYPTO_EccPrivateKey *ns)
245 struct GNUNET_FS_UpdateInformationGraph *uig;
247 struct GNUNET_BIO_ReadHandle *rh;
249 struct NamespaceUpdateNode *n;
254 uig = GNUNET_new (struct GNUNET_FS_UpdateInformationGraph);
257 fn = get_update_information_directory (h, ns);
258 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
263 rh = GNUNET_BIO_read_open (fn);
269 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
274 if (count > 1024 * 1024)
282 GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
284 for (i = 0; i < count; i++)
286 n = GNUNET_new (struct NamespaceUpdateNode);
287 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024))
288 || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
290 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
291 (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
294 GNUNET_free_non_null (n->id);
295 GNUNET_free_non_null (n->update);
297 GNUNET_CONTAINER_meta_data_destroy (n->md);
301 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
308 GNUNET_free_non_null (n->update);
309 GNUNET_CONTAINER_meta_data_destroy (n->md);
313 uig->update_nodes[i] = n;
315 uig->update_node_count = i;
317 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read `%s': %s\n"),
329 * Context for the SKS publication.
331 struct GNUNET_FS_PublishSksContext
335 * URI of the new entry in the namespace.
337 struct GNUNET_FS_Uri *uri;
340 * Namespace update node to add to namespace on success (or to be
341 * deleted if publishing failed).
343 struct NamespaceUpdateNode *nsn;
346 * Namespace we're publishing to.
348 struct GNUNET_CRYPTO_EccPrivateKey ns;
351 * Handle to the datastore.
353 struct GNUNET_DATASTORE_Handle *dsh;
358 struct GNUNET_FS_Handle *h;
361 * Function to call once we're done.
363 GNUNET_FS_PublishContinuation cont;
371 * Handle for our UBlock operation request.
373 struct GNUNET_FS_PublishUblockContext *uc;
378 * Function called by the UBlock construction with
379 * the result from the PUT (UBlock) request.
381 * @param cls closure of type "struct GNUNET_FS_PublishSksContext*"
382 * @param msg error message (or NULL)
385 sks_publish_cont (void *cls,
388 struct GNUNET_FS_PublishSksContext *psc = cls;
389 struct GNUNET_FS_UpdateInformationGraph *uig;
394 if (NULL != psc->cont)
395 psc->cont (psc->cont_cls, NULL, msg);
396 GNUNET_FS_publish_sks_cancel (psc);
399 if (NULL != psc->nsn)
401 /* FIXME: this can be done much more
402 * efficiently by simply appending to the
403 * file and overwriting the 4-byte header */
404 uig = read_update_information_graph (psc->h,
406 GNUNET_array_append (uig->update_nodes,
407 uig->update_node_count,
410 write_update_information_graph (uig);
411 free_update_information_graph (uig);
413 if (NULL != psc->cont)
414 psc->cont (psc->cont_cls, psc->uri, NULL);
415 GNUNET_FS_publish_sks_cancel (psc);
420 * Publish an SBlock on GNUnet.
422 * @param h handle to the file sharing subsystem
423 * @param ns namespace to publish in
424 * @param identifier identifier to use
425 * @param update update identifier to use
426 * @param meta metadata to use
427 * @param uri URI to refer to in the SBlock
428 * @param bo block options
429 * @param options publication options
430 * @param cont continuation
431 * @param cont_cls closure for cont
432 * @return NULL on error ('cont' will still be called)
434 struct GNUNET_FS_PublishSksContext *
435 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
436 const struct GNUNET_CRYPTO_EccPrivateKey *ns,
437 const char *identifier, const char *update,
438 const struct GNUNET_CONTAINER_MetaData *meta,
439 const struct GNUNET_FS_Uri *uri,
440 const struct GNUNET_FS_BlockOptions *bo,
441 enum GNUNET_FS_PublishOptions options,
442 GNUNET_FS_PublishContinuation cont, void *cont_cls)
444 struct GNUNET_FS_PublishSksContext *psc;
445 struct GNUNET_FS_Uri *sks_uri;
447 sks_uri = GNUNET_new (struct GNUNET_FS_Uri);
448 sks_uri->type = GNUNET_FS_URI_SKS;
449 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
450 GNUNET_CRYPTO_ecc_key_get_public (ns,
451 &sks_uri->data.sks.ns);
453 psc = GNUNET_new (struct GNUNET_FS_PublishSksContext);
457 psc->cont_cls = cont_cls;
459 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
461 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
462 if (NULL == psc->dsh)
464 sks_publish_cont (psc,
465 _("Failed to connect to datastore."));
471 psc->nsn = GNUNET_new (struct NamespaceUpdateNode);
472 psc->nsn->id = GNUNET_strdup (identifier);
473 psc->nsn->update = GNUNET_strdup (update);
474 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
475 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
477 psc->uc = GNUNET_FS_publish_ublock_ (h,
493 * Abort the SKS publishing operation.
495 * @param psc context of the operation to abort.
498 GNUNET_FS_publish_sks_cancel (struct GNUNET_FS_PublishSksContext *psc)
502 GNUNET_FS_publish_ublock_cancel_ (psc->uc);
505 if (NULL != psc->dsh)
507 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
510 GNUNET_FS_uri_destroy (psc->uri);
511 if (NULL != psc->nsn)
513 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
514 GNUNET_FS_uri_destroy (psc->nsn->uri);
515 GNUNET_free (psc->nsn->id);
516 GNUNET_free (psc->nsn->update);
517 GNUNET_free (psc->nsn);
524 * Closure for 'process_update_node'.
526 struct ProcessUpdateClosure
529 * Function to call for each node.
531 GNUNET_FS_IdentifierProcessor ip;
541 * Call the iterator in the closure for each node.
543 * @param cls closure (of type 'struct ProcessUpdateClosure *')
544 * @param key current key code
545 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
546 * @return GNUNET_YES if we should continue to
551 process_update_node (void *cls,
552 const struct GNUNET_HashCode *key,
555 struct ProcessUpdateClosure *pc = cls;
556 struct NamespaceUpdateNode *nsn = value;
568 * Closure for 'find_trees'.
570 struct FindTreeClosure
573 * UIG we are operating on.
575 struct GNUNET_FS_UpdateInformationGraph *uig;
578 * Array with 'head's of TREEs.
580 struct NamespaceUpdateNode **tree_array;
583 * Size of 'tree_array'
585 unsigned int tree_array_size;
588 * Current generational ID used.
593 * Identifier for the current TREE, or UINT_MAX for none yet.
600 * Find all nodes reachable from the current node (including the
601 * current node itself). If they are in no tree, add them to the
602 * current one. If they are the head of another tree, merge the
603 * trees. If they are in the middle of another tree, let them be.
604 * We can tell that a node is already in an tree by checking if
605 * its 'nug' field is set to the current 'nug' value. It is the
606 * head of an tree if it is in the 'tree_array' under its respective
609 * In short, we're trying to find the smallest number of tree to
610 * cover a directed graph.
612 * @param cls closure (of type 'struct FindTreeClosure')
613 * @param key current key code
614 * @param value value in the hash map
615 * @return GNUNET_YES if we should continue to
620 find_trees (void *cls,
621 const struct GNUNET_HashCode *key,
624 struct FindTreeClosure *fc = cls;
625 struct NamespaceUpdateNode *nsn = value;
626 struct GNUNET_HashCode hc;
628 if (nsn->nug == fc->nug)
630 if (UINT_MAX == nsn->tree_id)
631 return GNUNET_YES; /* circular */
632 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
633 if (fc->tree_array[nsn->tree_id] != nsn)
634 return GNUNET_YES; /* part of "another" (directed) TREE,
635 * and not root of it, end trace */
636 if (nsn->tree_id == fc->id)
637 return GNUNET_YES; /* that's our own root (can this be?) */
638 /* merge existing TREE, we have a root for both */
639 fc->tree_array[nsn->tree_id] = NULL;
640 if (UINT_MAX == fc->id)
641 fc->id = nsn->tree_id; /* take over ID */
646 nsn->tree_id = UINT_MAX; /* mark as undef */
648 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
649 GNUNET_CONTAINER_multihashmap_get_multiple (fc->uig->update_map, &hc,
657 * List all of the identifiers in the namespace for which we could
658 * produce an update. Namespace updates form a graph where each node
659 * has a name. Each node can have any number of URI/meta-data entries
660 * which can each be linked to other nodes. Cycles are possible.
662 * Calling this function with "next_id" NULL will cause the library to
663 * call "ip" with a root for each strongly connected component of the
664 * graph (a root being a node from which all other nodes in the Tree
667 * Calling this function with "next_id" being the name of a node will
668 * cause the library to call "ip" with all children of the node. Note
669 * that cycles within the final tree are possible (including self-loops).
670 * I know, odd definition of a tree, but the GUI will display an actual
671 * tree (GtkTreeView), so that's what counts for the term here.
673 * @param h fs handle to use
674 * @param ns namespace to inspect for updateable content
675 * @param next_id ID to look for; use NULL to look for tree roots
676 * @param ip function to call on each updateable identifier
677 * @param ip_cls closure for ip
680 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Handle *h,
681 const struct GNUNET_CRYPTO_EccPrivateKey *ns,
683 GNUNET_FS_IdentifierProcessor ip,
688 struct GNUNET_HashCode hc;
689 struct NamespaceUpdateNode *nsn;
690 struct ProcessUpdateClosure pc;
691 struct FindTreeClosure fc;
692 struct GNUNET_FS_UpdateInformationGraph *uig;
694 uig = read_update_information_graph (h, ns);
695 if (NULL == uig->update_nodes)
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "No updateable nodes found for ID `%s'\n", next_id);
699 free_update_information_graph (uig);
700 return; /* no nodes */
703 GNUNET_CONTAINER_multihashmap_create (2 +
704 3 * uig->update_node_count /
707 for (i = 0; i < uig->update_node_count; i++)
709 nsn = uig->update_nodes[i];
710 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
711 GNUNET_CONTAINER_multihashmap_put (uig->update_map, &hc, nsn,
712 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
716 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
719 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
720 &process_update_node, &pc);
721 free_update_information_graph (uig);
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Calculating TREEs to find roots of update trees\n");
726 /* Find heads of TREEs in update graph */
727 nug = ++uig->nug_gen;
728 fc.tree_array = NULL;
729 fc.tree_array_size = 0;
731 for (i = 0; i < uig->update_node_count; i++)
733 nsn = uig->update_nodes[i];
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
738 continue; /* already placed in TREE */
740 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
742 nsn->tree_id = UINT_MAX;
746 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
748 if (UINT_MAX == fc.id)
751 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
753 if (NULL == fc.tree_array[fc.id])
755 fc.tree_array[fc.id] = nsn;
756 nsn->tree_id = fc.id;
760 if (fc.id == fc.tree_array_size)
762 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
763 nsn->tree_id = fc.id;
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Starting new TREE %u with node `%s'\n", nsn->tree_id,
768 /* put all nodes with same identifier into this TREE */
769 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
770 fc.id = nsn->tree_id;
773 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
778 /* make head of TREE "id" */
779 fc.tree_array[fc.id] = nsn;
780 nsn->tree_id = fc.id;
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "TREE of node `%s' is %u\n", nsn->id,
786 for (i = 0; i < fc.tree_array_size; i++)
788 nsn = fc.tree_array[i];
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
793 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
796 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
798 free_update_information_graph (uig);
802 /* end of fs_namespace.c */