2 This file is part of GNUnet.
3 (C) 2001 - 2011 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 mesh/mesh_tunnel_tree.c
23 * @brief Tunnel tree handling functions
24 * @author Bartlomiej Polot
28 #include "mesh_tunnel_tree.h"
30 #define MESH_TREE_DEBUG GNUNET_YES
34 * Node of path tree for a tunnel
36 struct MeshTunnelTreeNode
39 * Peer this node describes
44 * Parent node in the tree
46 struct MeshTunnelTreeNode *parent;
51 struct MeshTunnelTreeNode *next;
56 struct MeshTunnelTreeNode *prev;
61 struct MeshTunnelTreeNode *children_head;
66 struct MeshTunnelTreeNode *children_tail;
69 * Status of the peer in the tunnel
71 enum MeshPeerState status;
76 * Tree to reach all peers in the tunnel
81 * Root node of peer tree
83 struct MeshTunnelTreeNode *root;
86 * Node that represents our position in the tree (for non local tunnels)
88 struct MeshTunnelTreeNode *me;
91 * DLL of disconneted nodes
93 struct MeshTunnelTreeNode *disconnected_head;
96 * DLL of disconneted nodes
98 struct MeshTunnelTreeNode *disconnected_tail;
101 * Cache of all peers and the first hop to them.
102 * Indexed by PeerIdentity, contains a pointer to the PeerIdentity
105 struct GNUNET_CONTAINER_MultiHashMap *first_hops;
113 * @param lenght How many hops will the path have.
115 * @return A newly allocated path with a peer array of the specified length.
117 struct MeshPeerPath *
118 path_new (unsigned int length)
120 struct MeshPeerPath *p;
122 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
126 p->peers = GNUNET_malloc (length * sizeof (GNUNET_PEER_Id));
135 * @param p the path to invert
138 path_invert (struct MeshPeerPath *path)
143 for (i = 0; i < path->length / 2; i++)
145 aux = path->peers[i];
146 path->peers[i] = path->peers[path->length - i - 1];
147 path->peers[path->length - i - 1] = aux;
153 * Duplicate a path, incrementing short peer's rc.
155 * @param p The path to duplicate.
157 struct MeshPeerPath *
158 path_duplicate (struct MeshPeerPath *path)
160 struct MeshPeerPath *aux;
163 aux = path_new (path->length);
164 memcpy (aux->peers, path->peers, path->length * sizeof (GNUNET_PEER_Id));
165 for (i = 0; i < path->length; i++)
166 GNUNET_PEER_change_rc (path->peers[i], 1);
172 * Recusively update the info about what is the first hop to reach the node
174 * @param tree Tree this nodes belongs to.
175 * @param parent_id Short ID from node form which to start updating.
176 * @param hop If known, ID of the first hop.
177 * If not known, NULL to find out and pass on children.
180 tree_node_update_first_hops (struct MeshTunnelTree *tree,
181 struct MeshTunnelTreeNode *parent,
182 struct GNUNET_PeerIdentity *hop);
186 * Get the length of a path
188 * @param path The path to measure, with the local peer at any point of it
190 * @return Number of hops to reach destination
191 * UINT_MAX in case the peer is not in the path
194 path_get_length (struct MeshPeerPath *path)
203 * Get the cost of the path relative to the already built tunnel tree
205 * @param t The tunnel tree to which compare
206 * @param path The individual path to reach a peer
208 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
211 * TODO: remove dummy implementation, look into the tunnel tree
214 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
216 return path_get_length (path);
221 * Destroy the path and free any allocated resources linked to it
223 * @param p the path to destroy
225 * @return GNUNET_OK on success
228 path_destroy (struct MeshPeerPath *p)
232 GNUNET_PEER_decrement_rcs (p->peers, p->length);
233 GNUNET_free_non_null (p->peers);
241 * Allocates and initializes a new node.
242 * Sets ID and parent of the new node and inserts it in the DLL of the parent
244 * @param parent Node that will be the parent from the new node, NULL for root
245 * @param peer Short Id of the new node
247 * @return Newly allocated node
249 static struct MeshTunnelTreeNode *
250 tree_node_new (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
252 struct MeshTunnelTreeNode *node;
254 node = GNUNET_malloc (sizeof (struct MeshTunnelTreeNode));
256 GNUNET_PEER_change_rc (peer, 1);
257 node->parent = parent;
259 GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
267 * Recursively find the given peer.
269 * @param parent Node where to start looking.
270 * @param peer Peer to find.
272 * @return Pointer to the node of the peer. NULL if not found.
274 static struct MeshTunnelTreeNode *
275 tree_node_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
277 struct MeshTunnelTreeNode *n;
278 struct MeshTunnelTreeNode *r;
280 if (parent->peer == peer_id)
282 for (n = parent->children_head; NULL != n; n = n->next)
284 r = tree_node_find_peer (n, peer_id);
293 * Recusively update the info about what is the first hop to reach the node
295 * @param tree Tree this nodes belongs to.
296 * @param parent_id Short ID from node form which to start updating.
297 * @param hop If known, ID of the first hop.
298 * If not known, NULL to find out and pass on children.
301 tree_node_update_first_hops (struct MeshTunnelTree *tree,
302 struct MeshTunnelTreeNode *parent,
303 struct GNUNET_PeerIdentity *hop)
305 struct GNUNET_PeerIdentity pi;
306 struct GNUNET_PeerIdentity *copy;
307 struct GNUNET_PeerIdentity id;
308 struct MeshTunnelTreeNode *n;
311 GNUNET_PEER_resolve (parent->peer, &id);
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Finding first hop for %s.\n",
317 struct MeshTunnelTreeNode *aux;
318 struct MeshTunnelTreeNode *old;
321 while (aux != tree->me)
324 GNUNET_PEER_resolve (aux->peer, &id);
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: ... checking %s.\n",
330 GNUNET_assert (NULL != aux);
333 GNUNET_PEER_resolve (old->peer, &id);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: It's %s!\n",
338 GNUNET_PEER_resolve (old->peer, hop);
340 GNUNET_PEER_resolve (parent->peer, &id);
341 copy = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
343 copy = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
346 (void) GNUNET_CONTAINER_multihashmap_put (tree->first_hops, &id.hashPubKey,
348 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
350 for (n = parent->children_head; NULL != n; n = n->next)
352 tree_node_update_first_hops (tree, n, hop);
358 tree_node_debug (struct MeshTunnelTreeNode *n, uint16_t level)
360 struct MeshTunnelTreeNode *c;
361 struct GNUNET_PeerIdentity id;;
364 for (i = 0; i < level; i++)
365 fprintf (stderr, " ");
366 if (n->status == MESH_PEER_READY)
367 fprintf (stderr, "#");
368 if (n->status == MESH_PEER_SEARCHING)
369 fprintf (stderr, "+");
370 if (n->status == MESH_PEER_RELAY)
371 fprintf (stderr, "-");
372 if (n->status == MESH_PEER_RECONNECTING)
373 fprintf (stderr, "*");
375 GNUNET_PEER_resolve (n->peer, &id);
376 fprintf (stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
377 if (NULL != n->parent)
379 GNUNET_PEER_resolve (n->parent->peer, &id);
380 fprintf (stderr, "(-> %s [%u])\n", GNUNET_i2s (&id), n->parent->peer);
383 fprintf (stderr, "(root)\n");
384 for (c = n->children_head; NULL != c; c = c->next)
385 tree_node_debug (c, level + 1);
390 * Destroys and frees the node and all children
392 * @param n Parent node to be destroyed
395 tree_node_destroy (struct MeshTunnelTreeNode *parent)
397 struct MeshTunnelTreeNode *n;
398 struct MeshTunnelTreeNode *next;
401 struct GNUNET_PeerIdentity id;
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying node %u\n",
405 GNUNET_PEER_resolve (parent->peer, &id);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: (%s)\n", GNUNET_i2s (&id));
408 n = parent->children_head;
412 tree_node_destroy (n);
415 GNUNET_PEER_change_rc (parent->peer, -1);
416 if (NULL != parent->parent)
417 GNUNET_CONTAINER_DLL_remove (parent->parent->children_head,
418 parent->parent->children_tail, parent);
419 GNUNET_free (parent);
425 * Create a new tunnel tree associated to a tunnel
427 * @param t Tunnel this tree will represent
428 * @param peer A short peer id of the root of the tree
430 * @return A newly allocated and initialized tunnel tree
432 struct MeshTunnelTree *
433 tree_new (GNUNET_PEER_Id peer)
435 struct MeshTunnelTree *tree;
437 tree = GNUNET_malloc (sizeof (struct MeshTunnelTree));
438 tree->first_hops = GNUNET_CONTAINER_multihashmap_create (32);
439 tree->root = tree_node_new (NULL, peer);
440 tree->root->status = MESH_PEER_ROOT;
447 * Set the status of a node.
450 * @param peer A short peer id of local peer.
453 tree_set_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer,
454 enum MeshPeerState status)
456 struct MeshTunnelTreeNode *n;
458 n = tree_find_peer (tree, peer);
466 * Get the status of a node.
468 * @param tree Tree whose local id we want to now.
470 * @return Short peer id of local peer.
473 tree_get_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer)
475 struct MeshTunnelTreeNode *n;
477 n = tree_find_peer (tree, peer);
479 return MESH_PEER_INVALID;
485 * Get the id of the predecessor of the local node.
487 * @param tree Tree whose local id we want to now.
489 * @return Short peer id of local peer.
492 tree_get_predecessor (struct MeshTunnelTree *tree)
494 if (NULL != tree->me && NULL != tree->me->parent)
495 return tree->me->parent->peer;
497 return (GNUNET_PEER_Id) 0;
502 * Find the first peer whom to send a packet to go down this path
504 * @param t The tunnel tree to use
505 * @param peer The peerinfo of the peer we are trying to reach
507 * @return peerinfo of the peer who is the first hop in the tunnel
510 struct GNUNET_PeerIdentity *
511 tree_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
513 struct GNUNET_PeerIdentity id;
514 struct GNUNET_PeerIdentity *r;
516 GNUNET_PEER_resolve (peer, &id);
517 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
520 struct MeshTunnelTreeNode *n;
522 n = tree_find_peer (t, peer);
523 if (NULL != t->me && NULL != n)
525 tree_node_update_first_hops (t, n, NULL);
526 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
527 GNUNET_assert (NULL != r);
531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
532 "Tree structure inconsistent! me: %p, n: %p", t->me, n);
542 * Find the given peer in the tree.
544 * @param tree Tree where to look for the peer.
545 * @param peer Peer to find.
547 * @return Pointer to the node of the peer. NULL if not found.
549 struct MeshTunnelTreeNode *
550 tree_find_peer (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer_id)
552 return tree_node_find_peer (tree->root, peer_id);
557 * Recusively mark peer and children as disconnected, notify client
559 * @param tree Tree this node belongs to
560 * @param parent Node to be clean, potentially with children
561 * @param cb Callback to use to notify about disconnected peers.
564 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
565 struct MeshTunnelTreeNode *parent,
566 MeshTreeCallback cb, void *cbcls)
568 struct GNUNET_PeerIdentity *pi;
569 struct GNUNET_PeerIdentity id;
570 struct MeshTunnelTreeNode *n;
572 for (n = parent->children_head; NULL != n; n = n->next)
574 tree_mark_peers_disconnected (tree, n, cb, cbcls);
576 if (MESH_PEER_READY == parent->status)
579 cb (cbcls, parent->peer);
580 parent->status = MESH_PEER_RECONNECTING;
583 /* Remove and free info about first hop */
584 GNUNET_PEER_resolve (parent->peer, &id);
585 pi = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
586 GNUNET_CONTAINER_multihashmap_remove_all (tree->first_hops, &id.hashPubKey);
593 * Iterate over all children of the local node.
595 * @param tree Tree to use. Must have "me" set.
596 * @param cb Callback to call over each child.
597 * @param cls Closure.
600 tree_iterate_children (struct MeshTunnelTree *tree, MeshTreeCallback cb,
603 struct MeshTunnelTreeNode *n;
605 if (NULL == tree->me)
610 for (n = tree->me->children_head; NULL != n; n = n->next)
618 * Recusively update the info about what is the first hop to reach the node
620 * @param tree Tree this nodes belongs to.
621 * @param parent_id Short ID from node form which to start updating.
622 * @param hop If known, ID of the first hop.
623 * If not known, NULL to find out and pass on children.
626 tree_update_first_hops (struct MeshTunnelTree *tree, GNUNET_PEER_Id parent_id,
627 struct GNUNET_PeerIdentity *hop)
629 tree_node_update_first_hops (tree, tree_find_peer (tree, parent_id), hop);
634 * Delete the current path to the peer, including all now unused relays.
635 * The destination peer is NOT destroyed, it is returned in order to either set
636 * a new path to it or destroy it explicitly, taking care of it's child nodes.
638 * @param t Tunnel tree where to delete the path from.
639 * @param peer Destination peer whose path we want to remove.
640 * @param cb Callback to use to notify about disconnected peers.
641 * @param cbcls Closure for cb.
643 * @return pointer to the pathless node.
644 * NULL when not found
646 struct MeshTunnelTreeNode *
647 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
648 MeshTreeCallback cb, void *cbcls)
650 struct MeshTunnelTreeNode *parent;
651 struct MeshTunnelTreeNode *node;
652 struct MeshTunnelTreeNode *n;
655 struct GNUNET_PeerIdentity id;
657 GNUNET_PEER_resolve (peer_id, &id);
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting path to %s.\n",
661 if (peer_id == t->root->peer)
664 for (n = t->disconnected_head; NULL != n; n = n->next)
666 if (n->peer == peer_id)
668 /* Was already pathless, waiting for reconnection */
669 GNUNET_CONTAINER_DLL_remove (t->disconnected_head, t->disconnected_tail,
674 n = tree_find_peer (t, peer_id);
680 GNUNET_CONTAINER_DLL_remove (parent->children_head, parent->children_tail, n);
683 while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
686 GNUNET_PEER_resolve (parent->peer, &id);
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting node %s.\n",
691 tree_node_destroy (parent);
695 GNUNET_PEER_resolve (parent->peer, &id);
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Not deleted peer %s.\n",
700 tree_mark_peers_disconnected (t, node, cb, cbcls);
707 * Return a newly allocated individual path to reach a peer from the local peer,
708 * according to the path tree of some tunnel.
710 * @param t Tunnel from which to read the path tree
711 * @param peer_info Destination peer to whom we want a path
713 * @return A newly allocated individual path to reach the destination peer.
714 * Path must be destroyed afterwards.
716 struct MeshPeerPath *
717 tree_get_path_to_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
719 struct MeshTunnelTreeNode *n;
720 struct MeshPeerPath *p;
721 GNUNET_PEER_Id myid = t->me->peer;
723 n = tree_find_peer (t, peer);
728 /* Building the path (inverted!) */
729 while (n->peer != myid)
731 GNUNET_array_append (p->peers, p->length, n->peer);
732 GNUNET_PEER_change_rc (n->peer, 1);
740 GNUNET_array_append (p->peers, p->length, myid);
741 GNUNET_PEER_change_rc (myid, 1);
751 * Integrate a stand alone path into the tunnel tree.
752 * If the peer toward which the new path is already in the tree, the peer
753 * and its children will be maked as disconnected and the callback
754 * will be called on each one of them. They will be maked as online only after
755 * receiving a PATH ACK for the new path for each one of them, so the caller
756 * should take care of sending a new CREATE PATH message for each disconnected
759 * @param t Tunnel where to add the new path.
760 * @param p Path to be integrated.
761 * @param cb Callback to use to notify about peers temporarily disconnecting.
762 * @param cbcls Closure for cb.
764 * @return GNUNET_OK in case of success.
765 * GNUNET_SYSERR in case of error.
768 * - go backwards on path looking for each peer in the present tree
769 * - do not disconnect peers until new path is created & connected
772 tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
773 MeshTreeCallback cb, void *cbcls)
775 struct MeshTunnelTreeNode *parent;
776 struct MeshTunnelTreeNode *oldnode;
777 struct MeshTunnelTreeNode *n;
778 struct MeshTunnelTreeNode *c;
779 struct GNUNET_PeerIdentity id;
784 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "tree: Adding path [%u] towards peer %s.\n", p->length,
790 GNUNET_assert (0 != p->length);
791 parent = n = t->root;
792 if (n->peer != p->peers[0])
795 return GNUNET_SYSERR;
799 oldnode = tree_del_path (t, p->peers[p->length - 1], cb, cbcls);
800 /* Look for the first node that is not already present in the tree
802 * Assuming that the tree is somewhat balanced, O(log n * log N).
803 * - Length of the path is expected to be log N (size of whole network).
804 * - Each level of the tree is expected to have log n children (size of tree).
806 me = t->root->peer == 1 ? 0 : -1;
807 for (i = 1; i < p->length; i++)
810 GNUNET_PEER_resolve (p->peers[i], &id);
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Looking for peer %s.\n",
815 if (p->peers[i] == 1)
817 for (c = n->children_head; NULL != c; c = c->next)
819 if (c->peer == p->peers[i])
822 GNUNET_PEER_resolve (parent->peer, &id);
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "tree: Found in children of %s.\n", GNUNET_i2s (&id));
830 /* If we couldn't find a child equal to path[i], we have reached the end
831 * of the common path. */
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: All childen visited.\n");
838 /* Add the rest of the path as a branch from parent. */
839 while (i < p->length)
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %u to %u.\n",
843 p->peers[i], parent->peer);
844 GNUNET_PEER_resolve (p->peers[i], &id);
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %s.\n",
847 GNUNET_PEER_resolve (parent->peer, &id);
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: to %s.\n",
852 if (i == p->length - 1 && NULL != oldnode)
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "tree: Putting old node into place.\n");
858 oldnode->parent = parent;
859 GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
861 tree_node_update_first_hops (t, oldnode, NULL);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
869 n = tree_node_new (parent, p->peers[i]);
870 n->status = MESH_PEER_RELAY;
880 n->status = MESH_PEER_SEARCHING;
882 GNUNET_break (-1 != me);
884 /* Add info about first hop into hashmap. */
885 if (-1 != me && me < p->length - 1)
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "MESH: finding first hop (own pos %d/%u)\n", me,
892 GNUNET_PEER_resolve (p->peers[me + 1], &id);
893 tree_update_first_hops (t, p->peers[me + 1], &id);
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "MESH: was last in path, not updating first hops (%d/%u)\n",
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
909 * Notifies a tree that a connection it might be using is broken.
910 * Marks all peers down the paths as disconnected and notifies the client.
912 * @param t Tree to use.
913 * @param p1 Short id of one of the peers (order unimportant)
914 * @param p2 Short id of one of the peers (order unimportant)
915 * @param cb Function to call for every peer that is marked as disconnected.
916 * @param cbcls Closure for cb.
918 * @return Short ID of the first disconnected peer in the tree.
921 tree_notify_connection_broken (struct MeshTunnelTree *t, GNUNET_PEER_Id p1,
922 GNUNET_PEER_Id p2, MeshTreeCallback cb,
925 struct MeshTunnelTreeNode *n;
926 struct MeshTunnelTreeNode *c;
928 n = tree_find_peer (t, p1);
931 if (NULL != n->parent && n->parent->peer == p2)
933 tree_mark_peers_disconnected (t, n, cb, cbcls);
934 GNUNET_CONTAINER_DLL_remove (n->parent->children_head,
935 n->parent->children_tail, n);
936 GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail, n);
939 for (c = n->children_head; NULL != c; c = c->next)
943 tree_mark_peers_disconnected (t, c, cb, cbcls);
944 GNUNET_CONTAINER_DLL_remove (n->children_head, n->children_tail, c);
945 GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail,
955 * Deletes a peer from a tunnel, liberating all unused resources on the path to
956 * it. It shouldn't have children, if it has they will be destroyed as well.
957 * If the tree is not local and no longer has any paths, the root node will be
958 * destroyed and marked as NULL.
960 * @param t Tunnel tree to use.
961 * @param peer Short ID of the peer to remove from the tunnel tree.
962 * @param cb Callback to notify client of disconnected peers.
963 * @param cbcls Closure for cb.
965 * @return GNUNET_OK or GNUNET_SYSERR
968 tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer,
969 MeshTreeCallback cb, void *cbcls)
971 struct MeshTunnelTreeNode *n;
973 n = tree_del_path (t, peer, cb, cbcls);
979 GNUNET_break_op (NULL == n->children_head);
980 tree_node_destroy (n);
981 if (NULL == t->root->children_head && t->me != t->root)
983 tree_node_destroy (t->root);
992 * Print the tree on stderr
997 tree_debug (struct MeshTunnelTree *t)
999 tree_node_debug (t->root, 0);
1004 * Iterator over hash map peer entries and frees all data in it.
1005 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
1007 * @param cls closure
1008 * @param key current key code (will no longer contain valid data!!)
1009 * @param value value in the hash map (treated as void *)
1010 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1013 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
1015 GNUNET_free (value);
1021 * Destroy the whole tree and free all used memory and Peer_Ids
1023 * @param t Tree to be destroyed
1026 tree_destroy (struct MeshTunnelTree *t)
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
1031 tree_node_destroy (t->root);
1032 GNUNET_CONTAINER_multihashmap_iterate (t->first_hops, &iterate_free, NULL);
1033 GNUNET_CONTAINER_multihashmap_destroy (t->first_hops);