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 * Destroy the path and free any allocated resources linked to it
205 * @param p the path to destroy
207 * @return GNUNET_OK on success
210 path_destroy (struct MeshPeerPath *p)
214 GNUNET_PEER_decrement_rcs (p->peers, p->length);
215 GNUNET_free_non_null (p->peers);
223 * Allocates and initializes a new node.
224 * Sets ID and parent of the new node and inserts it in the DLL of the parent
226 * @param parent Node that will be the parent from the new node, NULL for root
227 * @param peer Short Id of the new node
229 * @return Newly allocated node
231 static struct MeshTunnelTreeNode *
232 tree_node_new (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
234 struct MeshTunnelTreeNode *node;
236 node = GNUNET_malloc (sizeof (struct MeshTunnelTreeNode));
238 GNUNET_PEER_change_rc (peer, 1);
239 node->parent = parent;
241 GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
249 * Recursively find the given peer.
251 * @param parent Node where to start looking.
252 * @param peer Peer to find.
254 * @return Pointer to the node of the peer. NULL if not found.
256 static struct MeshTunnelTreeNode *
257 tree_node_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
259 struct MeshTunnelTreeNode *n;
260 struct MeshTunnelTreeNode *r;
262 if (parent->peer == peer_id)
264 for (n = parent->children_head; NULL != n; n = n->next)
266 r = tree_node_find_peer (n, peer_id);
275 * Recusively update the info about what is the first hop to reach the node
277 * @param tree Tree this nodes belongs to.
278 * @param parent_id Short ID from node form which to start updating.
279 * @param hop If known, ID of the first hop.
280 * If not known, NULL to find out and pass on children.
283 tree_node_update_first_hops (struct MeshTunnelTree *tree,
284 struct MeshTunnelTreeNode *parent,
285 struct GNUNET_PeerIdentity *hop)
287 struct GNUNET_PeerIdentity pi;
288 struct GNUNET_PeerIdentity *copy;
289 struct GNUNET_PeerIdentity id;
290 struct MeshTunnelTreeNode *n;
293 GNUNET_PEER_resolve (parent->peer, &id);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Finding first hop for %s.\n",
299 struct MeshTunnelTreeNode *aux;
300 struct MeshTunnelTreeNode *old;
303 while (aux != tree->me)
306 GNUNET_PEER_resolve (aux->peer, &id);
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: ... checking %s.\n",
312 GNUNET_assert (NULL != aux);
315 GNUNET_PEER_resolve (old->peer, &id);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: It's %s!\n",
320 GNUNET_PEER_resolve (old->peer, hop);
322 GNUNET_PEER_resolve (parent->peer, &id);
323 copy = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
325 copy = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
328 (void) GNUNET_CONTAINER_multihashmap_put (tree->first_hops, &id.hashPubKey,
330 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
332 for (n = parent->children_head; NULL != n; n = n->next)
334 tree_node_update_first_hops (tree, n, hop);
340 tree_node_debug (struct MeshTunnelTreeNode *n, uint16_t level)
342 struct MeshTunnelTreeNode *c;
343 struct GNUNET_PeerIdentity id;;
346 for (i = 0; i < level; i++)
347 fprintf (stderr, " ");
348 if (n->status == MESH_PEER_READY)
349 fprintf (stderr, "#");
350 if (n->status == MESH_PEER_SEARCHING)
351 fprintf (stderr, "+");
352 if (n->status == MESH_PEER_RELAY)
353 fprintf (stderr, "-");
354 if (n->status == MESH_PEER_RECONNECTING)
355 fprintf (stderr, "*");
357 GNUNET_PEER_resolve (n->peer, &id);
358 fprintf (stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
359 if (NULL != n->parent)
361 GNUNET_PEER_resolve (n->parent->peer, &id);
362 fprintf (stderr, "(-> %s [%u])\n", GNUNET_i2s (&id), n->parent->peer);
365 fprintf (stderr, "(root)\n");
366 for (c = n->children_head; NULL != c; c = c->next)
367 tree_node_debug (c, level + 1);
372 * Destroys and frees the node and all children
374 * @param n Parent node to be destroyed
377 tree_node_destroy (struct MeshTunnelTreeNode *parent)
379 struct MeshTunnelTreeNode *n;
380 struct MeshTunnelTreeNode *next;
383 struct GNUNET_PeerIdentity id;
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying node %u\n",
387 GNUNET_PEER_resolve (parent->peer, &id);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: (%s)\n", GNUNET_i2s (&id));
390 n = parent->children_head;
394 tree_node_destroy (n);
397 GNUNET_PEER_change_rc (parent->peer, -1);
398 if (NULL != parent->parent)
399 GNUNET_CONTAINER_DLL_remove (parent->parent->children_head,
400 parent->parent->children_tail, parent);
401 GNUNET_free (parent);
407 * Create a new tunnel tree associated to a tunnel
409 * @param t Tunnel this tree will represent
410 * @param peer A short peer id of the root of the tree
412 * @return A newly allocated and initialized tunnel tree
414 struct MeshTunnelTree *
415 tree_new (GNUNET_PEER_Id peer)
417 struct MeshTunnelTree *tree;
419 tree = GNUNET_malloc (sizeof (struct MeshTunnelTree));
420 tree->first_hops = GNUNET_CONTAINER_multihashmap_create (32);
421 tree->root = tree_node_new (NULL, peer);
422 tree->root->status = MESH_PEER_ROOT;
426 tree->me = tree->root;
434 * Set the status of a node.
437 * @param peer A short peer id of local peer.
440 tree_set_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer,
441 enum MeshPeerState status)
443 struct MeshTunnelTreeNode *n;
445 n = tree_find_peer (tree, peer);
453 * Get the status of a node.
455 * @param tree Tree whose local id we want to now.
457 * @return Short peer id of local peer.
460 tree_get_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer)
462 struct MeshTunnelTreeNode *n;
464 n = tree_find_peer (tree, peer);
466 return MESH_PEER_INVALID;
472 * Get the id of the predecessor of the local node.
474 * @param tree Tree whose local id we want to now.
476 * @return Short peer id of local peer.
479 tree_get_predecessor (struct MeshTunnelTree *tree)
481 if (NULL != tree->me && NULL != tree->me->parent)
482 return tree->me->parent->peer;
484 return (GNUNET_PEER_Id) 0;
489 * Find the first peer whom to send a packet to go down this path
491 * @param t The tunnel tree to use
492 * @param peer The peerinfo of the peer we are trying to reach
494 * @return peerinfo of the peer who is the first hop in the tunnel
497 struct GNUNET_PeerIdentity *
498 tree_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
500 struct GNUNET_PeerIdentity id;
501 struct GNUNET_PeerIdentity *r;
503 GNUNET_PEER_resolve (peer, &id);
504 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
507 struct MeshTunnelTreeNode *n;
509 n = tree_find_peer (t, peer);
510 if (NULL != t->me && NULL != n)
512 tree_node_update_first_hops (t, n, NULL);
513 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
514 GNUNET_assert (NULL != r);
518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
519 "Tree structure inconsistent! me: %p, n: %p", t->me, n);
529 * Find the given peer in the tree.
531 * @param tree Tree where to look for the peer.
532 * @param peer Peer to find.
534 * @return Pointer to the node of the peer. NULL if not found.
536 struct MeshTunnelTreeNode *
537 tree_find_peer (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer_id)
539 return tree_node_find_peer (tree->root, peer_id);
544 * Recusively mark peer and children as disconnected, notify client
546 * @param tree Tree this node belongs to
547 * @param parent Node to be clean, potentially with children
548 * @param cb Callback to use to notify about disconnected peers.
551 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
552 struct MeshTunnelTreeNode *parent,
553 MeshTreeCallback cb, void *cbcls)
555 struct GNUNET_PeerIdentity *pi;
556 struct GNUNET_PeerIdentity id;
557 struct MeshTunnelTreeNode *n;
559 for (n = parent->children_head; NULL != n; n = n->next)
561 tree_mark_peers_disconnected (tree, n, cb, cbcls);
563 if (MESH_PEER_READY == parent->status)
566 cb (cbcls, parent->peer);
567 parent->status = MESH_PEER_RECONNECTING;
570 /* Remove and free info about first hop */
571 GNUNET_PEER_resolve (parent->peer, &id);
572 pi = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
573 GNUNET_CONTAINER_multihashmap_remove_all (tree->first_hops, &id.hashPubKey);
580 * Iterate over all children of the local node.
582 * @param tree Tree to use. Must have "me" set.
583 * @param cb Callback to call over each child.
584 * @param cls Closure.
587 tree_iterate_children (struct MeshTunnelTree *tree, MeshTreeCallback cb,
590 struct MeshTunnelTreeNode *n;
592 if (NULL == tree->me)
597 for (n = tree->me->children_head; NULL != n; n = n->next)
605 * Recusively update the info about what is the first hop to reach the node
607 * @param tree Tree this nodes belongs to.
608 * @param parent_id Short ID from node form which to start updating.
609 * @param hop If known, ID of the first hop.
610 * If not known, NULL to find out and pass on children.
613 tree_update_first_hops (struct MeshTunnelTree *tree, GNUNET_PEER_Id parent_id,
614 struct GNUNET_PeerIdentity *hop)
616 tree_node_update_first_hops (tree, tree_find_peer (tree, parent_id), hop);
621 * Delete the current path to the peer, including all now unused relays.
622 * The destination peer is NOT destroyed, it is returned in order to either set
623 * a new path to it or destroy it explicitly, taking care of it's child nodes.
625 * @param t Tunnel tree where to delete the path from.
626 * @param peer Destination peer whose path we want to remove.
627 * @param cb Callback to use to notify about disconnected peers.
628 * @param cbcls Closure for cb.
630 * @return pointer to the pathless node.
631 * NULL when not found
633 struct MeshTunnelTreeNode *
634 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
635 MeshTreeCallback cb, void *cbcls)
637 struct MeshTunnelTreeNode *parent;
638 struct MeshTunnelTreeNode *node;
639 struct MeshTunnelTreeNode *n;
642 struct GNUNET_PeerIdentity id;
644 GNUNET_PEER_resolve (peer_id, &id);
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting path to %s.\n",
648 if (peer_id == t->root->peer)
651 for (n = t->disconnected_head; NULL != n; n = n->next)
653 if (n->peer == peer_id)
655 /* Was already pathless, waiting for reconnection */
656 GNUNET_CONTAINER_DLL_remove (t->disconnected_head, t->disconnected_tail,
661 n = tree_find_peer (t, peer_id);
667 GNUNET_CONTAINER_DLL_remove (parent->children_head, parent->children_tail, n);
670 while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
673 GNUNET_PEER_resolve (parent->peer, &id);
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting node %s.\n",
678 tree_node_destroy (parent);
682 GNUNET_PEER_resolve (parent->peer, &id);
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Not deleted peer %s.\n",
687 tree_mark_peers_disconnected (t, node, cb, cbcls);
694 * Return a newly allocated individual path to reach a peer from the local peer,
695 * according to the path tree of some tunnel.
697 * @param t Tunnel from which to read the path tree
698 * @param peer_info Destination peer to whom we want a path
700 * @return A newly allocated individual path to reach the destination peer.
701 * Path must be destroyed afterwards.
703 struct MeshPeerPath *
704 tree_get_path_to_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
706 struct MeshTunnelTreeNode *n;
707 struct MeshPeerPath *p;
709 n = tree_find_peer (t, peer);
717 /* Building the path (inverted!) */
720 GNUNET_array_append (p->peers, p->length, n->peer);
721 GNUNET_PEER_change_rc (n->peer, 1);
730 GNUNET_array_append (p->peers, p->length, 1);
731 GNUNET_PEER_change_rc (1, 1);
741 * Integrate a stand alone path into the tunnel tree.
742 * If the peer toward which the new path is already in the tree, the peer
743 * and its children will be maked as disconnected and the callback
744 * will be called on each one of them. They will be maked as online only after
745 * receiving a PATH ACK for the new path for each one of them, so the caller
746 * should take care of sending a new CREATE PATH message for each disconnected
749 * @param t Tunnel where to add the new path.
750 * @param p Path to be integrated.
751 * @param cb Callback to use to notify about peers temporarily disconnecting.
752 * @param cbcls Closure for cb.
754 * @return GNUNET_OK in case of success.
755 * GNUNET_SYSERR in case of error.
758 * - go backwards on path looking for each peer in the present tree
759 * - do not disconnect peers until new path is created & connected
762 tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
763 MeshTreeCallback cb, void *cbcls)
765 struct MeshTunnelTreeNode *parent;
766 struct MeshTunnelTreeNode *oldnode;
767 struct MeshTunnelTreeNode *n;
768 struct MeshTunnelTreeNode *c;
769 struct GNUNET_PeerIdentity id;
774 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "tree: Adding path [%u] towards peer %s.\n", p->length,
780 GNUNET_assert (0 != p->length);
781 parent = n = t->root;
782 if (n->peer != p->peers[0])
785 return GNUNET_SYSERR;
789 oldnode = tree_del_path (t, p->peers[p->length - 1], cb, cbcls);
790 /* Look for the first node that is not already present in the tree
792 * Assuming that the tree is somewhat balanced, O(log n * log N).
793 * - Length of the path is expected to be log N (size of whole network).
794 * - Each level of the tree is expected to have log n children (size of tree).
796 me = t->root->peer == 1 ? 0 : -1;
797 for (i = 1; i < p->length; i++)
800 GNUNET_PEER_resolve (p->peers[i], &id);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Looking for peer %s.\n",
805 if (p->peers[i] == 1)
807 for (c = n->children_head; NULL != c; c = c->next)
809 if (c->peer == p->peers[i])
812 GNUNET_PEER_resolve (parent->peer, &id);
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "tree: Found in children of %s.\n", GNUNET_i2s (&id));
820 /* If we couldn't find a child equal to path[i], we have reached the end
821 * of the common path. */
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: All childen visited.\n");
828 /* Add the rest of the path as a branch from parent. */
829 while (i < p->length)
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %u to %u.\n",
833 p->peers[i], parent->peer);
834 GNUNET_PEER_resolve (p->peers[i], &id);
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %s.\n",
837 GNUNET_PEER_resolve (parent->peer, &id);
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: to %s.\n",
842 if (i == p->length - 1 && NULL != oldnode)
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "tree: Putting old node into place.\n");
848 oldnode->parent = parent;
849 GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
851 tree_node_update_first_hops (t, oldnode, NULL);
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
859 n = tree_node_new (parent, p->peers[i]);
860 n->status = MESH_PEER_RELAY;
870 n->status = MESH_PEER_SEARCHING;
872 GNUNET_break (-1 != me);
874 /* Add info about first hop into hashmap. */
875 if (-1 != me && me < p->length - 1)
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "MESH: finding first hop (own pos %d/%u)\n", me,
882 GNUNET_PEER_resolve (p->peers[me + 1], &id);
883 tree_update_first_hops (t, p->peers[me + 1], &id);
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "MESH: was last in path, not updating first hops (%d/%u)\n",
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
895 t->me = tree_find_peer (t, 1);
901 * Notifies a tree that a connection it might be using is broken.
902 * Marks all peers down the paths as disconnected and notifies the client.
904 * @param t Tree to use.
905 * @param p1 Short id of one of the peers (order unimportant)
906 * @param p2 Short id of one of the peers (order unimportant)
907 * @param cb Function to call for every peer that is marked as disconnected.
908 * @param cbcls Closure for cb.
910 * @return Short ID of the first disconnected peer in the tree.
913 tree_notify_connection_broken (struct MeshTunnelTree *t, GNUNET_PEER_Id p1,
914 GNUNET_PEER_Id p2, MeshTreeCallback cb,
917 struct MeshTunnelTreeNode *n;
918 struct MeshTunnelTreeNode *c;
920 n = tree_find_peer (t, p1);
923 if (NULL != n->parent && n->parent->peer == p2)
925 tree_mark_peers_disconnected (t, n, cb, cbcls);
926 GNUNET_CONTAINER_DLL_remove (n->parent->children_head,
927 n->parent->children_tail, n);
928 GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail, n);
931 for (c = n->children_head; NULL != c; c = c->next)
935 tree_mark_peers_disconnected (t, c, cb, cbcls);
936 GNUNET_CONTAINER_DLL_remove (n->children_head, n->children_tail, c);
937 GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail,
947 * Deletes a peer from a tunnel, liberating all unused resources on the path to
948 * it. It shouldn't have children, if it has they will be destroyed as well.
949 * If the tree is not local and no longer has any paths, the root node will be
950 * destroyed and marked as NULL.
952 * @param t Tunnel tree to use.
953 * @param peer Short ID of the peer to remove from the tunnel tree.
954 * @param cb Callback to notify client of disconnected peers.
955 * @param cbcls Closure for cb.
957 * @return GNUNET_OK or GNUNET_SYSERR
960 tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer,
961 MeshTreeCallback cb, void *cbcls)
963 struct MeshTunnelTreeNode *n;
965 n = tree_del_path (t, peer, cb, cbcls);
971 GNUNET_break_op (NULL == n->children_head);
972 tree_node_destroy (n);
973 if (NULL == t->root->children_head && t->me != t->root)
975 tree_node_destroy (t->root);
985 * Get the cost of the path relative to the already built tunnel tree.
987 * @param t The tunnel tree to which compare.
988 * @param path The individual path to reach a peer. It has to start at the
989 * root of the tree to be comparable.
991 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
994 * TODO: adapt to allow any start / root combination
995 * TODO: take in account state of the nodes
998 tree_get_path_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
1000 struct MeshTunnelTreeNode *n;
1001 struct MeshTunnelTreeNode *p;
1005 l = path_get_length (path);
1007 if (t->root->peer != path->peers[0])
1012 for (i = 1; i < l; i++)
1014 for (n = p->children_head; NULL != n; n = n->next)
1016 if (path->peers[i] == n->peer)
1030 * Print the tree on stderr
1035 tree_debug (struct MeshTunnelTree *t)
1037 tree_node_debug (t->root, 0);
1042 * Iterator over hash map peer entries and frees all data in it.
1043 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
1045 * @param cls closure
1046 * @param key current key code (will no longer contain valid data!!)
1047 * @param value value in the hash map (treated as void *)
1048 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1051 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
1053 GNUNET_free (value);
1059 * Destroy the whole tree and free all used memory and Peer_Ids
1061 * @param t Tree to be destroyed
1064 tree_destroy (struct MeshTunnelTree *t)
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
1069 tree_node_destroy (t->root);
1070 GNUNET_CONTAINER_multihashmap_iterate (t->first_hops, &iterate_free, NULL);
1071 GNUNET_CONTAINER_multihashmap_destroy (t->first_hops);