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 ns namespace handle
124 * @return NULL on error, otherwise the name of the directory
127 get_update_information_directory (struct GNUNET_FS_Handle *h,
128 const struct GNUNET_CRYPTO_EccPrivateKey *ns)
132 struct GNUNET_CRYPTO_EccPublicKey pub;
133 struct GNUNET_CRYPTO_ShortHashCode hc;
134 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
137 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "UPDATE_DIR",
140 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
144 GNUNET_CRYPTO_ecc_key_get_public (ns, &pub);
145 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &hc);
146 GNUNET_CRYPTO_short_hash_to_enc (&hc,
148 GNUNET_asprintf (&ret, "%s%s%s",
151 (const char *) enc.short_encoding);
158 * Release memory occupied by UIG datastructure.
160 * @param uig data structure to free
163 free_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig)
166 struct NamespaceUpdateNode *nsn;
168 for (i = 0; i < uig->update_node_count; i++)
170 nsn = uig->update_nodes[i];
171 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
172 GNUNET_FS_uri_destroy (nsn->uri);
173 GNUNET_free (nsn->id);
174 GNUNET_free (nsn->update);
177 GNUNET_array_grow (uig->update_nodes, uig->update_node_count,
179 if (NULL != uig->update_map)
180 GNUNET_CONTAINER_multihashmap_destroy (uig->update_map);
186 * Write the namespace update node graph to a file.
188 * @param ns namespace to dump
191 write_update_information_graph (struct GNUNET_FS_UpdateInformationGraph *uig)
194 struct GNUNET_BIO_WriteHandle *wh;
196 struct NamespaceUpdateNode *n;
199 fn = get_update_information_directory (uig->h,
201 wh = GNUNET_BIO_write_open (fn);
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
205 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
209 if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, uig->update_node_count))
211 for (i = 0; i < uig->update_node_count; i++)
213 n = uig->update_nodes[i];
214 uris = GNUNET_FS_uri_to_string (n->uri);
215 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) ||
216 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) ||
217 (GNUNET_OK != GNUNET_BIO_write_string (wh, n->update)) ||
218 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
226 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"),
234 * Read the namespace update node graph from a file.
236 * @param h FS handle to use
237 * @param ns namespace to read
238 * @return update graph, never NULL
240 static struct GNUNET_FS_UpdateInformationGraph *
241 read_update_information_graph (struct GNUNET_FS_Handle *h,
242 const struct GNUNET_CRYPTO_EccPrivateKey *ns)
244 struct GNUNET_FS_UpdateInformationGraph *uig;
246 struct GNUNET_BIO_ReadHandle *rh;
248 struct NamespaceUpdateNode *n;
253 uig = GNUNET_new (struct GNUNET_FS_UpdateInformationGraph);
256 fn = get_update_information_directory (h, ns);
257 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
262 rh = GNUNET_BIO_read_open (fn);
268 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
273 if (count > 1024 * 1024)
281 GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
283 for (i = 0; i < count; i++)
285 n = GNUNET_new (struct NamespaceUpdateNode);
286 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024))
287 || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
289 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
290 (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
293 GNUNET_free_non_null (n->id);
294 GNUNET_free_non_null (n->update);
296 GNUNET_CONTAINER_meta_data_destroy (n->md);
300 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
307 GNUNET_free_non_null (n->update);
308 GNUNET_CONTAINER_meta_data_destroy (n->md);
312 uig->update_nodes[i] = n;
314 uig->update_node_count = i;
316 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read `%s': %s\n"),
328 * Context for the SKS publication.
330 struct GNUNET_FS_PublishSksContext
334 * URI of the new entry in the namespace.
336 struct GNUNET_FS_Uri *uri;
339 * Namespace update node to add to namespace on success (or to be
340 * deleted if publishing failed).
342 struct NamespaceUpdateNode *nsn;
345 * Namespace we're publishing to.
347 struct GNUNET_CRYPTO_EccPrivateKey ns;
350 * Handle to the datastore.
352 struct GNUNET_DATASTORE_Handle *dsh;
357 struct GNUNET_FS_Handle *h;
360 * Function to call once we're done.
362 GNUNET_FS_PublishContinuation cont;
370 * Handle for our UBlock operation request.
372 struct GNUNET_FS_PublishUblockContext *uc;
377 * Function called by the UBlock construction with
378 * the result from the PUT (UBlock) request.
380 * @param cls closure of type "struct GNUNET_FS_PublishSksContext*"
381 * @param msg error message (or NULL)
384 sks_publish_cont (void *cls,
387 struct GNUNET_FS_PublishSksContext *psc = cls;
388 struct GNUNET_FS_UpdateInformationGraph *uig;
393 if (NULL != psc->cont)
394 psc->cont (psc->cont_cls, NULL, msg);
395 GNUNET_FS_publish_sks_cancel (psc);
398 if (NULL != psc->nsn)
400 /* FIXME: this can be done much more
401 * efficiently by simply appending to the
402 * file and overwriting the 4-byte header */
403 uig = read_update_information_graph (psc->h,
405 GNUNET_array_append (uig->update_nodes,
406 uig->update_node_count,
409 write_update_information_graph (uig);
410 free_update_information_graph (uig);
412 if (NULL != psc->cont)
413 psc->cont (psc->cont_cls, psc->uri, NULL);
414 GNUNET_FS_publish_sks_cancel (psc);
419 * Publish an SBlock on GNUnet.
421 * @param h handle to the file sharing subsystem
422 * @param ns namespace to publish in
423 * @param identifier identifier to use
424 * @param update update identifier to use
425 * @param meta metadata to use
426 * @param uri URI to refer to in the SBlock
427 * @param bo block options
428 * @param options publication options
429 * @param cont continuation
430 * @param cont_cls closure for cont
431 * @return NULL on error ('cont' will still be called)
433 struct GNUNET_FS_PublishSksContext *
434 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
435 const struct GNUNET_CRYPTO_EccPrivateKey *ns,
436 const char *identifier, const char *update,
437 const struct GNUNET_CONTAINER_MetaData *meta,
438 const struct GNUNET_FS_Uri *uri,
439 const struct GNUNET_FS_BlockOptions *bo,
440 enum GNUNET_FS_PublishOptions options,
441 GNUNET_FS_PublishContinuation cont, void *cont_cls)
443 struct GNUNET_FS_PublishSksContext *psc;
444 struct GNUNET_FS_Uri *sks_uri;
446 sks_uri = GNUNET_new (struct GNUNET_FS_Uri);
447 sks_uri->type = GNUNET_FS_URI_SKS;
448 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
449 GNUNET_CRYPTO_ecc_key_get_public (ns,
450 &sks_uri->data.sks.ns);
452 psc = GNUNET_new (struct GNUNET_FS_PublishSksContext);
456 psc->cont_cls = cont_cls;
458 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
460 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
461 if (NULL == psc->dsh)
463 sks_publish_cont (psc,
464 _("Failed to connect to datastore."));
470 psc->nsn = GNUNET_new (struct NamespaceUpdateNode);
471 psc->nsn->id = GNUNET_strdup (identifier);
472 psc->nsn->update = GNUNET_strdup (update);
473 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
474 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
476 psc->uc = GNUNET_FS_publish_ublock_ (h,
492 * Abort the SKS publishing operation.
494 * @param psc context of the operation to abort.
497 GNUNET_FS_publish_sks_cancel (struct GNUNET_FS_PublishSksContext *psc)
501 GNUNET_FS_publish_ublock_cancel_ (psc->uc);
504 if (NULL != psc->dsh)
506 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
509 GNUNET_FS_uri_destroy (psc->uri);
510 if (NULL != psc->nsn)
512 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
513 GNUNET_FS_uri_destroy (psc->nsn->uri);
514 GNUNET_free (psc->nsn->id);
515 GNUNET_free (psc->nsn->update);
516 GNUNET_free (psc->nsn);
523 * Closure for 'process_update_node'.
525 struct ProcessUpdateClosure
528 * Function to call for each node.
530 GNUNET_FS_IdentifierProcessor ip;
540 * Call the iterator in the closure for each node.
542 * @param cls closure (of type 'struct ProcessUpdateClosure *')
543 * @param key current key code
544 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
545 * @return GNUNET_YES if we should continue to
550 process_update_node (void *cls,
551 const struct GNUNET_HashCode *key,
554 struct ProcessUpdateClosure *pc = cls;
555 struct NamespaceUpdateNode *nsn = value;
567 * Closure for 'find_trees'.
569 struct FindTreeClosure
572 * UIG we are operating on.
574 struct GNUNET_FS_UpdateInformationGraph *uig;
577 * Array with 'head's of TREEs.
579 struct NamespaceUpdateNode **tree_array;
582 * Size of 'tree_array'
584 unsigned int tree_array_size;
587 * Current generational ID used.
592 * Identifier for the current TREE, or UINT_MAX for none yet.
599 * Find all nodes reachable from the current node (including the
600 * current node itself). If they are in no tree, add them to the
601 * current one. If they are the head of another tree, merge the
602 * trees. If they are in the middle of another tree, let them be.
603 * We can tell that a node is already in an tree by checking if
604 * its 'nug' field is set to the current 'nug' value. It is the
605 * head of an tree if it is in the 'tree_array' under its respective
608 * In short, we're trying to find the smallest number of tree to
609 * cover a directed graph.
611 * @param cls closure (of type 'struct FindTreeClosure')
612 * @param key current key code
613 * @param value value in the hash map
614 * @return GNUNET_YES if we should continue to
619 find_trees (void *cls,
620 const struct GNUNET_HashCode *key,
623 struct FindTreeClosure *fc = cls;
624 struct NamespaceUpdateNode *nsn = value;
625 struct GNUNET_HashCode hc;
627 if (nsn->nug == fc->nug)
629 if (UINT_MAX == nsn->tree_id)
630 return GNUNET_YES; /* circular */
631 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
632 if (fc->tree_array[nsn->tree_id] != nsn)
633 return GNUNET_YES; /* part of "another" (directed) TREE,
634 * and not root of it, end trace */
635 if (nsn->tree_id == fc->id)
636 return GNUNET_YES; /* that's our own root (can this be?) */
637 /* merge existing TREE, we have a root for both */
638 fc->tree_array[nsn->tree_id] = NULL;
639 if (UINT_MAX == fc->id)
640 fc->id = nsn->tree_id; /* take over ID */
645 nsn->tree_id = UINT_MAX; /* mark as undef */
647 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
648 GNUNET_CONTAINER_multihashmap_get_multiple (fc->uig->update_map, &hc,
656 * List all of the identifiers in the namespace for which we could
657 * produce an update. Namespace updates form a graph where each node
658 * has a name. Each node can have any number of URI/meta-data entries
659 * which can each be linked to other nodes. Cycles are possible.
661 * Calling this function with "next_id" NULL will cause the library to
662 * call "ip" with a root for each strongly connected component of the
663 * graph (a root being a node from which all other nodes in the Tree
666 * Calling this function with "next_id" being the name of a node will
667 * cause the library to call "ip" with all children of the node. Note
668 * that cycles within the final tree are possible (including self-loops).
669 * I know, odd definition of a tree, but the GUI will display an actual
670 * tree (GtkTreeView), so that's what counts for the term here.
672 * @param h fs handle to use
673 * @param ns namespace to inspect for updateable content
674 * @param next_id ID to look for; use NULL to look for tree roots
675 * @param ip function to call on each updateable identifier
676 * @param ip_cls closure for ip
679 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Handle *h,
680 const struct GNUNET_CRYPTO_EccPrivateKey *ns,
682 GNUNET_FS_IdentifierProcessor ip,
687 struct GNUNET_HashCode hc;
688 struct NamespaceUpdateNode *nsn;
689 struct ProcessUpdateClosure pc;
690 struct FindTreeClosure fc;
691 struct GNUNET_FS_UpdateInformationGraph *uig;
693 uig = read_update_information_graph (h, ns);
694 if (NULL == uig->update_nodes)
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
697 "No updateable nodes found for ID `%s'\n", next_id);
698 free_update_information_graph (uig);
699 return; /* no nodes */
702 GNUNET_CONTAINER_multihashmap_create (2 +
703 3 * uig->update_node_count /
706 for (i = 0; i < uig->update_node_count; i++)
708 nsn = uig->update_nodes[i];
709 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
710 GNUNET_CONTAINER_multihashmap_put (uig->update_map, &hc, nsn,
711 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
715 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
718 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
719 &process_update_node, &pc);
720 free_update_information_graph (uig);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Calculating TREEs to find roots of update trees\n");
725 /* Find heads of TREEs in update graph */
726 nug = ++uig->nug_gen;
727 fc.tree_array = NULL;
728 fc.tree_array_size = 0;
730 for (i = 0; i < uig->update_node_count; i++)
732 nsn = uig->update_nodes[i];
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
737 continue; /* already placed in TREE */
739 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
741 nsn->tree_id = UINT_MAX;
745 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
747 if (UINT_MAX == fc.id)
750 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
752 if (NULL == fc.tree_array[fc.id])
754 fc.tree_array[fc.id] = nsn;
755 nsn->tree_id = fc.id;
759 if (fc.id == fc.tree_array_size)
761 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
762 nsn->tree_id = fc.id;
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Starting new TREE %u with node `%s'\n", nsn->tree_id,
767 /* put all nodes with same identifier into this TREE */
768 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
769 fc.id = nsn->tree_id;
772 GNUNET_CONTAINER_multihashmap_get_multiple (uig->update_map, &hc,
777 /* make head of TREE "id" */
778 fc.tree_array[fc.id] = nsn;
779 nsn->tree_id = fc.id;
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "TREE of node `%s' is %u\n", nsn->id,
785 for (i = 0; i < fc.tree_array_size; i++)
787 nsn = fc.tree_array[i];
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
792 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
795 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
797 free_update_information_graph (uig);
801 /* end of fs_namespace.c */