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
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fs_service.h"
33 #include "fs_publish_ublock.h"
37 * Information about an (updateable) node in the
40 struct NamespaceUpdateNode
43 * Identifier for this node.
48 * Identifier of children of this node.
53 * Metadata for this entry.
55 struct GNUNET_CONTAINER_MetaData *md;
58 * URI of this entry in the namespace.
60 struct GNUNET_FS_Uri *uri;
63 * Namespace update generation ID. Used to ensure
64 * freshness of the tree_id.
69 * TREE this entry belongs to (if nug is current).
77 * Handle to update information for a namespace.
79 struct GNUNET_FS_UpdateInformationGraph
83 * Handle to the FS service context.
85 struct GNUNET_FS_Handle *h;
88 * Array with information about nodes in the namespace.
90 struct NamespaceUpdateNode **update_nodes;
93 * Private key for the namespace.
95 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
98 * Hash map mapping identifiers of update nodes
99 * to the update nodes (initialized on-demand).
101 struct GNUNET_CONTAINER_MultiHashMap *update_map;
104 * Size of the update nodes array.
106 unsigned int update_node_count;
114 * Generator for unique nug numbers.
116 unsigned int nug_gen;
121 * Return the name of the directory in which we store
122 * the update information graph for the given local namespace.
124 * @param h file-sharing handle
125 * @param ns namespace handle
126 * @return NULL on error, otherwise the name of the directory
129 get_update_information_directory (struct GNUNET_FS_Handle *h,
130 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns)
134 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
135 struct GNUNET_HashCode hc;
136 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
139 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "UPDATE_DIR",
142 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
146 GNUNET_CRYPTO_ecdsa_key_get_public (ns, &pub);
147 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &hc);
148 GNUNET_CRYPTO_hash_to_enc (&hc,
150 GNUNET_asprintf (&ret, "%s%s%s",
153 (const char *) enc.encoding);
160 * Release memory occupied by UIG datastructure.
162 * @param uig data structure to free
165 free_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig)
168 struct NamespaceUpdateNode *nsn;
170 for (i = 0; i < uig->update_node_count; i++)
172 nsn = uig->update_nodes[i];
173 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
174 GNUNET_FS_uri_destroy (nsn->uri);
175 GNUNET_free (nsn->id);
176 GNUNET_free (nsn->update);
179 GNUNET_array_grow (uig->update_nodes, uig->update_node_count,
181 if (NULL != uig->update_map)
182 GNUNET_CONTAINER_multihashmap_destroy (uig->update_map);
188 * Write a namespace's update node graph to a file.
190 * @param uig update information graph to dump
193 write_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig)
196 struct GNUNET_BIO_WriteHandle *wh;
198 struct NamespaceUpdateNode *n;
201 fn = get_update_information_directory (uig->h,
203 wh = GNUNET_BIO_write_open (fn);
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
211 if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, uig->update_node_count))
213 for (i = 0; i < uig->update_node_count; i++)
215 n = uig->update_nodes[i];
216 uris = GNUNET_FS_uri_to_string (n->uri);
217 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) ||
218 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) ||
219 (GNUNET_OK != GNUNET_BIO_write_string (wh, n->update)) ||
220 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
228 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"),
236 * Read the namespace update node graph from a file.
238 * @param h FS handle to use
239 * @param ns namespace to read
240 * @return update graph, never NULL
242 static struct GNUNET_FS_UpdateInformationGraph *
243 read_update_information_graph (struct GNUNET_FS_Handle *h,
244 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns)
246 struct GNUNET_FS_UpdateInformationGraph *uig;
248 struct GNUNET_BIO_ReadHandle *rh;
250 struct NamespaceUpdateNode *n;
255 uig = GNUNET_new (struct GNUNET_FS_UpdateInformationGraph);
258 fn = get_update_information_directory (h, ns);
259 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
264 rh = GNUNET_BIO_read_open (fn);
270 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
275 if (count > 1024 * 1024)
283 GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
285 for (i = 0; i < count; i++)
287 n = GNUNET_new (struct NamespaceUpdateNode);
288 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024))
289 || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
291 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
292 (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
295 GNUNET_free_non_null (n->id);
296 GNUNET_free_non_null (n->update);
298 GNUNET_CONTAINER_meta_data_destroy (n->md);
302 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
309 GNUNET_free_non_null (n->update);
310 GNUNET_CONTAINER_meta_data_destroy (n->md);
314 uig->update_nodes[i] = n;
316 uig->update_node_count = i;
318 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read `%s': %s\n"),
330 * Context for the SKS publication.
332 struct GNUNET_FS_PublishSksContext
336 * URI of the new entry in the namespace.
338 struct GNUNET_FS_Uri *uri;
341 * Namespace update node to add to namespace on success (or to be
342 * deleted if publishing failed).
344 struct NamespaceUpdateNode *nsn;
347 * Namespace we're publishing to.
349 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
352 * Handle to the datastore.
354 struct GNUNET_DATASTORE_Handle *dsh;
359 struct GNUNET_FS_Handle *h;
362 * Function to call once we're done.
364 GNUNET_FS_PublishContinuation cont;
372 * Handle for our UBlock operation request.
374 struct GNUNET_FS_PublishUblockContext *uc;
379 * Function called by the UBlock construction with
380 * the result from the PUT (UBlock) request.
382 * @param cls closure of type "struct GNUNET_FS_PublishSksContext*"
383 * @param msg error message (or NULL)
386 sks_publish_cont (void *cls,
389 struct GNUNET_FS_PublishSksContext *psc = cls;
390 struct GNUNET_FS_UpdateInformationGraph *uig;
395 if (NULL != psc->cont)
396 psc->cont (psc->cont_cls, NULL, msg);
397 GNUNET_FS_publish_sks_cancel (psc);
400 if (NULL != psc->nsn)
402 /* FIXME: this can be done much more
403 * efficiently by simply appending to the
404 * file and overwriting the 4-byte header */
405 uig = read_update_information_graph (psc->h,
407 GNUNET_array_append (uig->update_nodes,
408 uig->update_node_count,
411 write_update_information_graph (uig);
412 free_update_information_graph (uig);
414 if (NULL != psc->cont)
415 psc->cont (psc->cont_cls, psc->uri, NULL);
416 GNUNET_FS_publish_sks_cancel (psc);
421 * Publish an SBlock on GNUnet.
423 * @param h handle to the file sharing subsystem
424 * @param ns namespace to publish in
425 * @param identifier identifier to use
426 * @param update update identifier to use
427 * @param meta metadata to use
428 * @param uri URI to refer to in the SBlock
429 * @param bo block options
430 * @param options publication options
431 * @param cont continuation
432 * @param cont_cls closure for cont
433 * @return NULL on error ('cont' will still be called)
435 struct GNUNET_FS_PublishSksContext *
436 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
437 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
438 const char *identifier, const char *update,
439 const struct GNUNET_CONTAINER_MetaData *meta,
440 const struct GNUNET_FS_Uri *uri,
441 const struct GNUNET_FS_BlockOptions *bo,
442 enum GNUNET_FS_PublishOptions options,
443 GNUNET_FS_PublishContinuation cont, void *cont_cls)
445 struct GNUNET_FS_PublishSksContext *psc;
446 struct GNUNET_FS_Uri *sks_uri;
448 sks_uri = GNUNET_new (struct GNUNET_FS_Uri);
449 sks_uri->type = GNUNET_FS_URI_SKS;
450 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
451 GNUNET_CRYPTO_ecdsa_key_get_public (ns,
452 &sks_uri->data.sks.ns);
454 psc = GNUNET_new (struct GNUNET_FS_PublishSksContext);
458 psc->cont_cls = cont_cls;
460 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
462 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
463 if (NULL == psc->dsh)
465 sks_publish_cont (psc,
466 _("Failed to connect to datastore."));
472 psc->nsn = GNUNET_new (struct NamespaceUpdateNode);
473 psc->nsn->id = GNUNET_strdup (identifier);
474 psc->nsn->update = GNUNET_strdup (update);
475 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
476 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
478 psc->uc = GNUNET_FS_publish_ublock_ (h,
494 * Abort the SKS publishing operation.
496 * @param psc context of the operation to abort.
499 GNUNET_FS_publish_sks_cancel (struct GNUNET_FS_PublishSksContext *psc)
503 GNUNET_FS_publish_ublock_cancel_ (psc->uc);
506 if (NULL != psc->dsh)
508 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
511 GNUNET_FS_uri_destroy (psc->uri);
512 if (NULL != psc->nsn)
514 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
515 GNUNET_FS_uri_destroy (psc->nsn->uri);
516 GNUNET_free (psc->nsn->id);
517 GNUNET_free (psc->nsn->update);
518 GNUNET_free (psc->nsn);
525 * Closure for 'process_update_node'.
527 struct ProcessUpdateClosure
530 * Function to call for each node.
532 GNUNET_FS_IdentifierProcessor ip;
542 * Call the iterator in the closure for each node.
544 * @param cls closure (of type 'struct ProcessUpdateClosure *')
545 * @param key current key code
546 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
547 * @return GNUNET_YES if we should continue to
552 process_update_node (void *cls,
553 const struct GNUNET_HashCode *key,
556 struct ProcessUpdateClosure *pc = cls;
557 struct NamespaceUpdateNode *nsn = value;
569 * Closure for 'find_trees'.
571 struct FindTreeClosure
574 * UIG we are operating on.
576 struct GNUNET_FS_UpdateInformationGraph *uig;
579 * Array with 'head's of TREEs.
581 struct NamespaceUpdateNode **tree_array;
584 * Size of 'tree_array'
586 unsigned int tree_array_size;
589 * Current generational ID used.
594 * Identifier for the current TREE, or UINT_MAX for none yet.
601 * Find all nodes reachable from the current node (including the
602 * current node itself). If they are in no tree, add them to the
603 * current one. If they are the head of another tree, merge the
604 * trees. If they are in the middle of another tree, let them be.
605 * We can tell that a node is already in an tree by checking if
606 * its 'nug' field is set to the current 'nug' value. It is the
607 * head of an tree if it is in the 'tree_array' under its respective
610 * In short, we're trying to find the smallest number of tree to
611 * cover a directed graph.
613 * @param cls closure (of type 'struct FindTreeClosure')
614 * @param key current key code
615 * @param value value in the hash map
616 * @return GNUNET_YES if we should continue to
621 find_trees (void *cls,
622 const struct GNUNET_HashCode *key,
625 struct FindTreeClosure *fc = cls;
626 struct NamespaceUpdateNode *nsn = value;
627 struct GNUNET_HashCode hc;
629 if (nsn->nug == fc->nug)
631 if (UINT_MAX == nsn->tree_id)
632 return GNUNET_YES; /* circular */
633 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
634 if (fc->tree_array[nsn->tree_id] != nsn)
635 return GNUNET_YES; /* part of "another" (directed) TREE,
636 * and not root of it, end trace */
637 if (nsn->tree_id == fc->id)
638 return GNUNET_YES; /* that's our own root (can this be?) */
639 /* merge existing TREE, we have a root for both */
640 fc->tree_array[nsn->tree_id] = NULL;
641 if (UINT_MAX == fc->id)
642 fc->id = nsn->tree_id; /* take over ID */
647 nsn->tree_id = UINT_MAX; /* mark as undef */
649 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
650 GNUNET_CONTAINER_multihashmap_get_multiple (fc->uig->update_map, &hc,
658 * List all of the identifiers in the namespace for which we could
659 * produce an update. Namespace updates form a graph where each node
660 * has a name. Each node can have any number of URI/meta-data entries
661 * which can each be linked to other nodes. Cycles are possible.
663 * Calling this function with "next_id" NULL will cause the library to
664 * call "ip" with a root for each strongly connected component of the
665 * graph (a root being a node from which all other nodes in the Tree
668 * Calling this function with "next_id" being the name of a node will
669 * cause the library to call "ip" with all children of the node. Note
670 * that cycles within the final tree are possible (including self-loops).
671 * I know, odd definition of a tree, but the GUI will display an actual
672 * tree (GtkTreeView), so that's what counts for the term here.
674 * @param h fs handle to use
675 * @param ns namespace to inspect for updateable content
676 * @param next_id ID to look for; use NULL to look for tree roots
677 * @param ip function to call on each updateable identifier
678 * @param ip_cls closure for ip
681 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Handle *h,
682 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
684 GNUNET_FS_IdentifierProcessor ip,
689 struct GNUNET_HashCode hc;
690 struct NamespaceUpdateNode *nsn;
691 struct ProcessUpdateClosure pc;
692 struct FindTreeClosure fc;
693 struct GNUNET_FS_UpdateInformationGraph *uig;
695 uig = read_update_information_graph (h, ns);
696 if (NULL == uig->update_nodes)
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "No updateable nodes found for ID `%s'\n", next_id);
700 free_update_information_graph (uig);
701 return; /* no nodes */
704 GNUNET_CONTAINER_multihashmap_create (2 +
705 3 * uig->update_node_count /
708 for (i = 0; i < uig->update_node_count; i++)
710 nsn = uig->update_nodes[i];
711 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
712 GNUNET_CONTAINER_multihashmap_put (uig->update_map, &hc, nsn,
713 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
717 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
720 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
721 &process_update_node, &pc);
722 free_update_information_graph (uig);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Calculating TREEs to find roots of update trees\n");
727 /* Find heads of TREEs in update graph */
728 nug = ++uig->nug_gen;
729 fc.tree_array = NULL;
730 fc.tree_array_size = 0;
732 for (i = 0; i < uig->update_node_count; i++)
734 nsn = uig->update_nodes[i];
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
739 continue; /* already placed in TREE */
741 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
743 nsn->tree_id = UINT_MAX;
747 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
749 if (UINT_MAX == fc.id)
752 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
754 if (NULL == fc.tree_array[fc.id])
756 fc.tree_array[fc.id] = nsn;
757 nsn->tree_id = fc.id;
761 if (fc.id == fc.tree_array_size)
763 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
764 nsn->tree_id = fc.id;
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Starting new TREE %u with node `%s'\n", nsn->tree_id,
769 /* put all nodes with same identifier into this TREE */
770 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
771 fc.id = nsn->tree_id;
774 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
779 /* make head of TREE "id" */
780 fc.tree_array[fc.id] = nsn;
781 nsn->tree_id = fc.id;
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "TREE of node `%s' is %u\n", nsn->id,
787 for (i = 0; i < fc.tree_array_size; i++)
789 nsn = fc.tree_array[i];
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
794 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
797 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
799 free_update_information_graph (uig);
803 /* end of fs_namespace.c */