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);
185 * Find the first peer whom to send a packet to go down this path
187 * @param t The tunnel tree to use
188 * @param peer The peerinfo of the peer we are trying to reach
190 * @return peerinfo of the peer who is the first hop in the tunnel
193 struct GNUNET_PeerIdentity *
194 path_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
196 struct GNUNET_PeerIdentity id;
197 struct GNUNET_PeerIdentity *r;
199 GNUNET_PEER_resolve (peer, &id);
200 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
203 struct MeshTunnelTreeNode *n;
205 n = tree_find_peer (t, peer);
206 if (NULL != t->me && NULL != n)
208 tree_node_update_first_hops (t, n, NULL);
209 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
210 GNUNET_assert (NULL != r);
214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
215 "Tree structure inconsistent! me: %p, n: %p", t->me, n);
225 * Get the length of a path
227 * @param path The path to measure, with the local peer at any point of it
229 * @return Number of hops to reach destination
230 * UINT_MAX in case the peer is not in the path
233 path_get_length (struct MeshPeerPath *path)
242 * Get the cost of the path relative to the already built tunnel tree
244 * @param t The tunnel tree to which compare
245 * @param path The individual path to reach a peer
247 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
250 * TODO: remove dummy implementation, look into the tunnel tree
253 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
255 return path_get_length (path);
260 * Destroy the path and free any allocated resources linked to it
262 * @param p the path to destroy
264 * @return GNUNET_OK on success
267 path_destroy (struct MeshPeerPath *p)
271 GNUNET_PEER_decrement_rcs (p->peers, p->length);
272 GNUNET_free (p->peers);
280 * Allocates and initializes a new node.
281 * Sets ID and parent of the new node and inserts it in the DLL of the parent
283 * @param parent Node that will be the parent from the new node, NULL for root
284 * @param peer Short Id of the new node
286 * @return Newly allocated node
288 static struct MeshTunnelTreeNode *
289 tree_node_new (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
291 struct MeshTunnelTreeNode *node;
293 node = GNUNET_malloc (sizeof (struct MeshTunnelTreeNode));
295 GNUNET_PEER_change_rc (peer, 1);
296 node->parent = parent;
298 GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
306 * Recursively find the given peer.
308 * @param parent Node where to start looking.
309 * @param peer Peer to find.
311 * @return Pointer to the node of the peer. NULL if not found.
313 static struct MeshTunnelTreeNode *
314 tree_node_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
316 struct MeshTunnelTreeNode *n;
317 struct MeshTunnelTreeNode *r;
319 if (parent->peer == peer_id)
321 for (n = parent->children_head; NULL != n; n = n->next)
323 r = tree_node_find_peer (n, peer_id);
332 * Recusively update the info about what is the first hop to reach the node
334 * @param tree Tree this nodes belongs to.
335 * @param parent_id Short ID from node form which to start updating.
336 * @param hop If known, ID of the first hop.
337 * If not known, NULL to find out and pass on children.
340 tree_node_update_first_hops (struct MeshTunnelTree *tree,
341 struct MeshTunnelTreeNode *parent,
342 struct GNUNET_PeerIdentity *hop)
344 struct GNUNET_PeerIdentity pi;
345 struct GNUNET_PeerIdentity *copy;
346 struct GNUNET_PeerIdentity id;
347 struct MeshTunnelTreeNode *n;
350 GNUNET_PEER_resolve (parent->peer, &id);
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Finding first hop for %s.\n",
356 struct MeshTunnelTreeNode *aux;
357 struct MeshTunnelTreeNode *old;
360 while (aux != tree->me)
363 GNUNET_PEER_resolve (old->peer, &id);
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: ... its not %s.\n",
369 GNUNET_assert (NULL != aux);
372 GNUNET_PEER_resolve (old->peer, &id);
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: It's %s!\n",
377 GNUNET_PEER_resolve (old->peer, hop);
379 GNUNET_PEER_resolve (parent->peer, &id);
380 copy = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
382 copy = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
385 (void) GNUNET_CONTAINER_multihashmap_put (tree->first_hops, &id.hashPubKey,
387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
389 for (n = parent->children_head; NULL != n; n = n->next)
391 tree_node_update_first_hops (tree, n, hop);
397 tree_node_debug (struct MeshTunnelTreeNode *n, uint16_t level)
399 struct MeshTunnelTreeNode *c;
400 struct GNUNET_PeerIdentity id;;
403 for (i = 0; i < level; i++)
404 fprintf (stderr, " ");
405 if (n->status == MESH_PEER_READY)
406 fprintf (stderr, "#");
407 if (n->status == MESH_PEER_SEARCHING)
408 fprintf (stderr, "+");
409 if (n->status == MESH_PEER_RELAY)
410 fprintf (stderr, "-");
411 if (n->status == MESH_PEER_RECONNECTING)
412 fprintf (stderr, "*");
414 GNUNET_PEER_resolve (n->peer, &id);
415 fprintf (stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
416 if (NULL != n->parent)
418 GNUNET_PEER_resolve (n->parent->peer, &id);
419 fprintf (stderr, "(-> %s [%u])\n", GNUNET_i2s (&id), n->parent->peer);
422 fprintf (stderr, "(root)\n");
423 for (c = n->children_head; NULL != c; c = c->next)
424 tree_node_debug (c, level + 1);
429 * Destroys and frees the node and all children
431 * @param n Parent node to be destroyed
434 tree_node_destroy (struct MeshTunnelTreeNode *parent)
436 struct MeshTunnelTreeNode *n;
437 struct MeshTunnelTreeNode *next;
440 struct GNUNET_PeerIdentity id;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying node %u\n",
444 GNUNET_PEER_resolve (parent->peer, &id);
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: (%s)\n", GNUNET_i2s (&id));
447 n = parent->children_head;
451 tree_node_destroy (n);
454 GNUNET_PEER_change_rc (parent->peer, -1);
455 if (NULL != parent->parent)
456 GNUNET_CONTAINER_DLL_remove (parent->parent->children_head,
457 parent->parent->children_tail, parent);
458 GNUNET_free (parent);
464 * Create a new tunnel tree associated to a tunnel
466 * @param t Tunnel this tree will represent
467 * @param peer A short peer id of the root of the tree
469 * @return A newly allocated and initialized tunnel tree
471 struct MeshTunnelTree *
472 tree_new (GNUNET_PEER_Id peer)
474 struct MeshTunnelTree *tree;
476 tree = GNUNET_malloc (sizeof (struct MeshTunnelTree));
477 tree->first_hops = GNUNET_CONTAINER_multihashmap_create (32);
478 tree->root = tree_node_new (NULL, peer);
479 tree->root->status = MESH_PEER_ROOT;
486 * Set own identity in the tree
489 * @param peer A short peer id of local peer.
492 tree_set_me (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer)
494 tree->me = tree_find_peer (tree, peer);
499 * Get the id of the local node of the tree.
501 * @param tree Tree whose local id we want to now.
503 * @return Short peer id of local peer.
506 tree_get_me (struct MeshTunnelTree *tree)
508 if (NULL != tree->me)
509 return tree->me->peer;
511 return (GNUNET_PEER_Id) 0;
515 * Set the status of a node.
518 * @param peer A short peer id of local peer.
521 tree_set_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer,
522 enum MeshPeerState status)
524 struct MeshTunnelTreeNode *n;
526 n = tree_find_peer (tree, peer);
534 * Get the status of a node.
536 * @param tree Tree whose local id we want to now.
538 * @return Short peer id of local peer.
541 tree_get_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer)
543 struct MeshTunnelTreeNode *n;
545 n = tree_find_peer (tree, peer);
547 return MESH_PEER_INVALID;
553 * Get the id of the predecessor of the local node.
555 * @param tree Tree whose local id we want to now.
557 * @return Short peer id of local peer.
560 tree_get_predecessor (struct MeshTunnelTree *tree)
562 if (NULL != tree->me && NULL != tree->me->parent)
563 return tree->me->parent->peer;
565 return (GNUNET_PEER_Id) 0;
570 * Find the given peer in the tree.
572 * @param tree Tree where to look for the peer.
573 * @param peer Peer to find.
575 * @return Pointer to the node of the peer. NULL if not found.
577 struct MeshTunnelTreeNode *
578 tree_find_peer (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer_id)
580 return tree_node_find_peer (tree->root, peer_id);
585 * Recusively mark peer and children as disconnected, notify client
587 * @param tree Tree this node belongs to
588 * @param parent Node to be clean, potentially with children
589 * @param cb Callback to use to notify about disconnected peers.
592 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
593 struct MeshTunnelTreeNode *parent,
594 MeshTreeCallback cb, void *cbcls)
596 struct GNUNET_PeerIdentity *pi;
597 struct GNUNET_PeerIdentity id;
598 struct MeshTunnelTreeNode *n;
600 for (n = parent->children_head; NULL != n; n = n->next)
602 tree_mark_peers_disconnected (tree, n, cb, cbcls);
604 if (MESH_PEER_READY == parent->status)
607 cb (cbcls, parent->peer);
608 parent->status = MESH_PEER_RECONNECTING;
611 /* Remove and free info about first hop */
612 GNUNET_PEER_resolve (parent->peer, &id);
613 pi = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
614 GNUNET_CONTAINER_multihashmap_remove_all (tree->first_hops, &id.hashPubKey);
621 * Iterate over all children of the local node.
623 * @param tree Tree to use. Must have "me" set.
624 * @param cb Callback to call over each child.
625 * @param cls Closure.
628 tree_iterate_children (struct MeshTunnelTree *tree, MeshTreeCallback cb,
631 struct MeshTunnelTreeNode *n;
633 if (NULL == tree->me)
638 for (n = tree->me->children_head; NULL != n; n = n->next)
646 * Recusively update the info about what is the first hop to reach the node
648 * @param tree Tree this nodes belongs to.
649 * @param parent_id Short ID from node form which to start updating.
650 * @param hop If known, ID of the first hop.
651 * If not known, NULL to find out and pass on children.
654 tree_update_first_hops (struct MeshTunnelTree *tree, GNUNET_PEER_Id parent_id,
655 struct GNUNET_PeerIdentity *hop)
657 tree_node_update_first_hops (tree, tree_find_peer (tree, parent_id), hop);
662 * Delete the current path to the peer, including all now unused relays.
663 * The destination peer is NOT destroyed, it is returned in order to either set
664 * a new path to it or destroy it explicitly, taking care of it's child nodes.
666 * @param t Tunnel tree where to delete the path from.
667 * @param peer Destination peer whose path we want to remove.
668 * @param cb Callback to use to notify about disconnected peers.
669 * @param cbcls Closure for cb.
671 * @return pointer to the pathless node.
672 * NULL when not found
674 struct MeshTunnelTreeNode *
675 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
676 MeshTreeCallback cb, void *cbcls)
678 struct MeshTunnelTreeNode *parent;
679 struct MeshTunnelTreeNode *node;
680 struct MeshTunnelTreeNode *n;
683 struct GNUNET_PeerIdentity id;
685 GNUNET_PEER_resolve (peer_id, &id);
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting path to %s.\n",
689 if (peer_id == t->root->peer)
692 for (n = t->disconnected_head; NULL != n; n = n->next)
694 if (n->peer == peer_id)
696 /* Was already pathless, waiting for reconnection */
697 GNUNET_CONTAINER_DLL_remove (t->disconnected_head, t->disconnected_tail,
702 n = tree_find_peer (t, peer_id);
708 GNUNET_CONTAINER_DLL_remove (parent->children_head, parent->children_tail, n);
711 while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
714 GNUNET_PEER_resolve (parent->peer, &id);
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting node %s.\n",
719 tree_node_destroy (parent);
723 GNUNET_PEER_resolve (parent->peer, &id);
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Not deleted peer %s.\n",
728 tree_mark_peers_disconnected (t, node, cb, cbcls);
735 * Return a newly allocated individual path to reach a peer from the local peer,
736 * according to the path tree of some tunnel.
738 * @param t Tunnel from which to read the path tree
739 * @param peer_info Destination peer to whom we want a path
741 * @return A newly allocated individual path to reach the destination peer.
742 * Path must be destroyed afterwards.
744 struct MeshPeerPath *
745 tree_get_path_to_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
747 struct MeshTunnelTreeNode *n;
748 struct MeshPeerPath *p;
749 GNUNET_PEER_Id myid = t->me->peer;
751 n = tree_find_peer (t, peer);
756 /* Building the path (inverted!) */
757 while (n->peer != myid)
759 GNUNET_array_append (p->peers, p->length, n->peer);
760 GNUNET_PEER_change_rc (n->peer, 1);
765 GNUNET_array_append (p->peers, p->length, myid);
766 GNUNET_PEER_change_rc (myid, 1);
776 * Integrate a stand alone path into the tunnel tree.
777 * If the peer toward which the new path is already in the tree, the peer
778 * and its children will be maked as disconnected and the callback
779 * will be called on each one of them. They will be maked as online only after
780 * receiving a PATH ACK for the new path for each one of them, so the caller
781 * should take care of sending a new CREATE PATH message for each disconnected
784 * @param t Tunnel where to add the new path.
785 * @param p Path to be integrated.
786 * @param cb Callback to use to notify about peers temporarily disconnecting.
787 * @param cbcls Closure for cb.
789 * @return GNUNET_OK in case of success.
790 * GNUNET_SYSERR in case of error.
793 * - go backwards on path looking for each peer in the present tree
794 * - do not disconnect peers until new path is created & connected
797 tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
798 MeshTreeCallback cb, void *cbcls)
800 struct MeshTunnelTreeNode *parent;
801 struct MeshTunnelTreeNode *oldnode;
802 struct MeshTunnelTreeNode *n;
803 struct MeshTunnelTreeNode *c;
804 struct GNUNET_PeerIdentity id;
810 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "tree: Adding path [%u] towards peer %s.\n", p->length,
820 GNUNET_assert (0 != p->length);
821 parent = n = t->root;
822 if (n->peer != p->peers[0])
825 return GNUNET_SYSERR;
829 oldnode = tree_del_path (t, p->peers[p->length - 1], cb, cbcls);
830 /* Look for the first node that is not already present in the tree
832 * Assuming that the tree is somewhat balanced, O(log n * log N).
833 * - Length of the path is expected to be log N (size of whole network).
834 * - Each level of the tree is expected to have log n children (size of tree).
836 me = t->root->peer == myid ? 0 : -1;
837 for (i = 1; i < p->length; i++)
840 GNUNET_PEER_resolve (p->peers[i], &id);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Looking for peer %s.\n",
845 if (p->peers[i] == myid)
847 for (c = n->children_head; NULL != c; c = c->next)
849 if (c->peer == p->peers[i])
852 GNUNET_PEER_resolve (parent->peer, &id);
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "tree: Found in children of %s.\n", GNUNET_i2s (&id));
860 /* If we couldn't find a child equal to path[i], we have reached the end
861 * of the common path. */
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: All childen visited.\n");
868 /* Add the rest of the path as a branch from parent. */
869 while (i < p->length)
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %u to %u.\n",
873 p->peers[i], parent->peer);
874 GNUNET_PEER_resolve (p->peers[i], &id);
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %s.\n",
877 GNUNET_PEER_resolve (parent->peer, &id);
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: to %s.\n",
882 if (i == p->length - 1 && NULL != oldnode)
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "tree: Putting old node into place.\n");
888 oldnode->parent = parent;
889 GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
891 tree_node_update_first_hops (t, oldnode, NULL);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
899 n = tree_node_new (parent, p->peers[i]);
900 n->status = MESH_PEER_RELAY;
907 n->status = MESH_PEER_SEARCHING;
909 /* Add info about first hop into hashmap. */
910 if (-1 != me && me < p->length - 1)
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "MESH: finding first hop (own pos %d/%u)\n", me,
917 GNUNET_PEER_resolve (p->peers[me + 1], &id);
918 tree_update_first_hops (t, p->peers[p->length - 1], &id);
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
928 * Notifies a tree that a connection it might be using is broken.
929 * Marks all peers down the paths as disconnected and notifies the client.
931 * @param t Tree to use.
932 * @param p1 Short id of one of the peers (order unimportant)
933 * @param p2 Short id of one of the peers (order unimportant)
934 * @param cb Function to call for every peer that is marked as disconnected.
935 * @param cbcls Closure for cb.
937 * @return Short ID of the first disconnected peer in the tree.
940 tree_notify_connection_broken (struct MeshTunnelTree *t, GNUNET_PEER_Id p1,
941 GNUNET_PEER_Id p2, MeshTreeCallback cb,
944 struct MeshTunnelTreeNode *n;
945 struct MeshTunnelTreeNode *c;
947 n = tree_find_peer (t, p1);
950 if (NULL != n->parent && n->parent->peer == p2)
952 tree_mark_peers_disconnected (t, n, cb, cbcls);
953 GNUNET_CONTAINER_DLL_remove (n->parent->children_head,
954 n->parent->children_tail, n);
955 GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail, n);
958 for (c = n->children_head; NULL != c; c = c->next)
962 tree_mark_peers_disconnected (t, c, cb, cbcls);
963 GNUNET_CONTAINER_DLL_remove (n->children_head, n->children_tail, c);
964 GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail,
974 * Deletes a peer from a tunnel, liberating all unused resources on the path to
975 * it. It shouldn't have children, if it has they will be destroyed as well.
976 * If the tree is not local and no longer has any paths, the root node will be
977 * destroyed and marked as NULL.
979 * @param t Tunnel tree to use.
980 * @param peer Short ID of the peer to remove from the tunnel tree.
981 * @param cb Callback to notify client of disconnected peers.
982 * @param cbcls Closure for cb.
984 * @return GNUNET_OK or GNUNET_SYSERR
987 tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer,
988 MeshTreeCallback cb, void *cbcls)
990 struct MeshTunnelTreeNode *n;
992 n = tree_del_path (t, peer, cb, cbcls);
998 GNUNET_break_op (NULL == n->children_head);
999 tree_node_destroy (n);
1000 if (NULL == t->root->children_head && t->me != t->root)
1002 tree_node_destroy (t->root);
1011 * Print the tree on stderr
1016 tree_debug (struct MeshTunnelTree *t)
1018 tree_node_debug (t->root, 0);
1023 * Iterator over hash map peer entries and frees all data in it.
1024 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
1026 * @param cls closure
1027 * @param key current key code (will no longer contain valid data!!)
1028 * @param value value in the hash map (treated as void *)
1029 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1032 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
1034 GNUNET_free (value);
1040 * Destroy the whole tree and free all used memory and Peer_Ids
1042 * @param t Tree to be destroyed
1045 tree_destroy (struct MeshTunnelTree *t)
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
1050 tree_node_destroy (t->root);
1051 GNUNET_CONTAINER_multihashmap_iterate (t->first_hops, &iterate_free, NULL);
1052 GNUNET_CONTAINER_multihashmap_destroy (t->first_hops);