Allowed to destroy NULL paths
[oweals/gnunet.git] / src / mesh / mesh_tunnel_tree.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file mesh/mesh_tunnel_tree.c
23  * @brief Tunnel tree handling functions
24  * @author Bartlomiej Polot
25  */
26
27 #include "mesh.h"
28 #include "mesh_tunnel_tree.h"
29
30 #define MESH_TREE_DEBUG GNUNET_YES
31
32
33 /**
34  * Create a new path
35  *
36  * @param lenght How many hops will the path have.
37  *
38  * @return A newly allocated path with a peer array of the specified length.
39  */
40 struct MeshPeerPath *
41 path_new (unsigned int length)
42 {
43   struct MeshPeerPath *p;
44
45   p = GNUNET_malloc (sizeof(struct MeshPeerPath));
46   if (length > 0)
47   {
48     p->length = length;
49     p->peers = GNUNET_malloc (length * sizeof(GNUNET_PEER_Id));
50   }
51   return p;
52 }
53
54
55 /**
56  * Invert the path
57  *
58  * @param p the path to invert
59  */
60 void
61 path_invert (struct MeshPeerPath *path)
62 {
63   GNUNET_PEER_Id aux;
64   unsigned int i;
65
66   for (i = 0; i < path->length / 2; i++)
67   {
68     aux = path->peers[i];
69     path->peers[i] = path->peers[path->length - i - 1];
70     path->peers[path->length - i - 1] = aux;
71   }
72 }
73
74
75 /**
76  * Duplicate a path, incrementing short peer's rc.
77  *
78  * @param p The path to duplicate.
79  */
80 struct MeshPeerPath *
81 path_duplicate (struct MeshPeerPath *path)
82 {
83   struct MeshPeerPath *aux;
84   unsigned int i;
85
86   aux = path_new(path->length);
87   memcpy (aux->peers, path->peers, path->length * sizeof(GNUNET_PEER_Id));
88   for (i = 0; i < path->length; i++)
89     GNUNET_PEER_change_rc(path->peers[i], 1);
90   return aux;
91 }
92
93
94 /**
95  * Find the first peer whom to send a packet to go down this path
96  *
97  * @param t The tunnel tree to use
98  * @param peer The peerinfo of the peer we are trying to reach
99  *
100  * @return peerinfo of the peer who is the first hop in the tunnel
101  *         NULL on error
102  */
103 struct GNUNET_PeerIdentity *
104 path_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
105 {
106   struct GNUNET_PeerIdentity id;
107   struct GNUNET_PeerIdentity *r;
108
109   GNUNET_PEER_resolve (peer, &id);
110   r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
111   GNUNET_break (NULL != r);
112
113   return r;
114 }
115
116
117 /**
118  * Get the length of a path
119  *
120  * @param path The path to measure, with the local peer at any point of it
121  *
122  * @return Number of hops to reach destination
123  *         UINT_MAX in case the peer is not in the path
124  */
125 unsigned int
126 path_get_length (struct MeshPeerPath *path)
127 {
128   if (NULL == path)
129     return UINT_MAX;
130   return path->length;
131 }
132
133
134 /**
135  * Get the cost of the path relative to the already built tunnel tree
136  *
137  * @param t The tunnel tree to which compare
138  * @param path The individual path to reach a peer
139  *
140  * @return Number of hops to reach destination, UINT_MAX in case the peer is not
141  * in the path
142  *
143  * TODO: remove dummy implementation, look into the tunnel tree
144  */
145 unsigned int
146 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
147 {
148   return path_get_length (path);
149 }
150
151
152 /**
153  * Destroy the path and free any allocated resources linked to it
154  *
155  * @param p the path to destroy
156  *
157  * @return GNUNET_OK on success
158  */
159 int
160 path_destroy (struct MeshPeerPath *p)
161 {
162   if (NULL == p)
163     return GNUNET_OK;
164   GNUNET_PEER_decrement_rcs (p->peers, p->length);
165   GNUNET_free (p->peers);
166   GNUNET_free (p);
167   return GNUNET_OK;
168 }
169
170
171
172 /**
173  * Allocates and initializes a new node.
174  * Sets ID and parent of the new node and inserts it in the DLL of the parent
175  *
176  * @param parent Node that will be the parent from the new node, NULL for root
177  * @param peer Short Id of the new node
178  *
179  * @return Newly allocated node
180  */
181 static struct MeshTunnelTreeNode *
182 tree_node_new(struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
183 {
184   struct MeshTunnelTreeNode *node;
185
186   node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
187   node->peer = peer;
188   GNUNET_PEER_change_rc(peer, 1);
189   node->parent = parent;
190   if (NULL != parent)
191     GNUNET_CONTAINER_DLL_insert(parent->children_head,
192                                 parent->children_tail,
193                                 node);
194
195   return node;
196 }
197
198
199 static void
200 tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
201 {
202   struct MeshTunnelTreeNode *c;
203   uint16_t i;
204
205   for (i = 0; i < level; i++)
206     fprintf(stderr, "  ");
207   if (n->status == MESH_PEER_READY)
208     fprintf(stderr, "#");
209   if (n->status == MESH_PEER_SEARCHING)
210     fprintf(stderr, "+");
211   if (n->status == MESH_PEER_RELAY)
212     fprintf(stderr, "-");
213   if (n->status == MESH_PEER_RECONNECTING)
214     fprintf(stderr, "*");
215
216   fprintf(stderr, "%u [%p] ", n->peer, n);
217   if (NULL != n->parent)
218     fprintf(stderr, "(-> %u)\n", n->parent->peer);
219   else
220     fprintf(stderr, "(root)\n");
221   for (c = n->children_head; NULL != c; c = c->next)
222     tree_node_debug(c, level + 1);
223 }
224
225
226 /**
227  * Destroys and frees the node and all children
228  *
229  * @param n Parent node to be destroyed
230  */
231 static void
232 tree_node_destroy (struct MeshTunnelTreeNode *parent)
233 {
234   struct MeshTunnelTreeNode *n;
235   struct MeshTunnelTreeNode *next;
236
237   n = parent->children_head;
238   while (NULL != n)
239   {
240     next = n->next;
241     tree_node_destroy(n);
242     n = next;
243   }
244   GNUNET_PEER_change_rc(parent->peer, -1);
245   if (NULL != parent->parent)
246     GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
247                                 parent->parent->children_tail,
248                                 parent);
249   GNUNET_free(parent);
250 }
251
252
253
254 /**
255  * Create a new tunnel tree associated to a tunnel
256  *
257  * @param t Tunnel this tree will represent
258  * @param peer A short peer id of the root of the tree
259  *
260  * @return A newly allocated and initialized tunnel tree
261  */
262 struct MeshTunnelTree *
263 tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
264 {
265   struct MeshTunnelTree *tree;
266
267   tree = GNUNET_malloc(sizeof (struct MeshTunnelTree));
268   tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
269   tree->root = tree_node_new(NULL, peer);
270   tree->root->status = MESH_PEER_ROOT;
271   tree->t = t;
272   tree->root->t = t;
273
274   return tree;
275 }
276
277
278 /**
279  * Recursively find the given peer in the tree.
280  *
281  * @param t Tunnel where to look for the peer.
282  * @param peer Peer to find
283  *
284  * @return Pointer to the node of the peer. NULL if not found.
285  */
286 struct MeshTunnelTreeNode *
287 tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
288 {
289   struct MeshTunnelTreeNode *n;
290   struct MeshTunnelTreeNode *r;
291
292   if (parent->peer == peer_id)
293     return parent;
294   for (n = parent->children_head; NULL != n; n = n->next)
295   {
296     r = tree_find_peer (n, peer_id);
297     if (NULL != r)
298       return r;
299   }
300   return NULL;
301 }
302
303
304 /**
305  * Recusively mark peer and children as disconnected, notify client
306  *
307  * @param tree Tree this node belongs to
308  * @param parent Node to be clean, potentially with children
309  * @param cb Callback to use to notify about disconnected peers.
310  */
311 static void
312 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
313                               struct MeshTunnelTreeNode *parent,
314                               MeshNodeDisconnectCB cb)
315 {
316   struct GNUNET_PeerIdentity *pi;
317   struct GNUNET_PeerIdentity id;
318   struct MeshTunnelTreeNode *n;
319
320   for (n = parent->children_head; NULL != n; n = n->next)
321   {
322     tree_mark_peers_disconnected (tree, n, cb);
323   }
324   if (MESH_PEER_READY == parent->status && NULL != cb)
325   {
326     cb (parent);
327   }
328   parent->status = MESH_PEER_RECONNECTING;
329
330   /* Remove and free info about first hop */
331   GNUNET_PEER_resolve(parent->peer, &id);
332   pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
333   GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
334   if (NULL != pi)
335     GNUNET_free(pi);
336 }
337
338
339 /**
340  * Recusively update the info about what is the first hop to reach the node
341  *
342  * @param tree Tree this nodes belongs to
343  * @param parent Node to be start updating
344  * @param hop If known, ID of the first hop.
345  *            If not known, NULL to find out and pass on children.
346  */
347 void
348 tree_update_first_hops (struct MeshTunnelTree *tree,
349                         struct MeshTunnelTreeNode *parent,
350                         struct GNUNET_PeerIdentity *hop)
351 {
352   struct GNUNET_PeerIdentity pi;
353   struct GNUNET_PeerIdentity *copy;
354   struct GNUNET_PeerIdentity id;
355   struct MeshTunnelTreeNode *n;
356
357 #if MESH_TREE_DEBUG
358   GNUNET_PEER_resolve(parent->peer, &id);
359   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
360           "tree:   Finding first hop for %s.\n",
361           GNUNET_i2s (&id));
362 #endif
363   if (NULL == hop)
364   {
365     struct MeshTunnelTreeNode *aux;
366     struct MeshTunnelTreeNode *old;
367
368     aux = old = parent;
369     while (aux != tree->me)
370     {
371 #if MESH_TREE_DEBUG
372       GNUNET_PEER_resolve(old->peer, &id);
373       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
374              "tree:   ... its not %s.\n",
375              GNUNET_i2s (&id));
376 #endif
377       old = aux;
378       aux = aux->parent;
379       GNUNET_assert(NULL != aux);
380     }
381 #if MESH_TREE_DEBUG
382     GNUNET_PEER_resolve(old->peer, &id);
383     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
384                "tree:   It's %s!\n",
385                GNUNET_i2s (&id));
386 #endif
387     hop = &pi;
388     GNUNET_PEER_resolve(old->peer, hop);
389   }
390   copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
391   *copy = *hop;
392   GNUNET_PEER_resolve(parent->peer, &id);
393   GNUNET_CONTAINER_multihashmap_put(
394     tree->first_hops,
395     &id.hashPubKey,
396     copy,
397     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
398
399   for (n = parent->children_head; NULL != n; n = n->next)
400   {
401     tree_update_first_hops (tree, n, hop);
402   }
403 }
404
405
406 /**
407  * Delete the current path to the peer, including all now unused relays.
408  * The destination peer is NOT destroyed, it is returned in order to either set
409  * a new path to it or destroy it explicitly, taking care of it's child nodes.
410  *
411  * @param t Tunnel tree where to delete the path from.
412  * @param peer Destination peer whose path we want to remove.
413  * @param cb Callback to use to notify about disconnected peers.
414  *
415  * @return pointer to the pathless node.
416  *         NULL when not found
417  */
418 struct MeshTunnelTreeNode *
419 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
420                  MeshNodeDisconnectCB cb)
421 {
422   struct MeshTunnelTreeNode *parent;
423   struct MeshTunnelTreeNode *node;
424   struct MeshTunnelTreeNode *n;
425
426 #if MESH_TREE_DEBUG
427   struct GNUNET_PeerIdentity id;
428   GNUNET_PEER_resolve(peer_id, &id);
429   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
430           "tree:   Deleting path to %s.\n",
431           GNUNET_i2s (&id));
432 #endif
433   if (peer_id == t->root->peer)
434     return NULL;
435
436   for (n = t->disconnected_head; NULL != n; n = n->next)
437   {
438     if (n->peer == peer_id)
439     {
440       /* Was already pathless, waiting for reconnection */
441       GNUNET_CONTAINER_DLL_remove (t->disconnected_head,
442                                    t->disconnected_tail,
443                                    n);
444       return n;
445     }
446   }
447   n = tree_find_peer (t->root, peer_id);
448   if (NULL == n)
449     return NULL;
450   node = n;
451
452   parent = n->parent;
453   GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
454   n->parent = NULL;
455
456   while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
457   {
458 #if MESH_TREE_DEBUG
459     GNUNET_PEER_resolve(parent->peer, &id);
460     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
461               "tree:   Deleting node %s.\n",
462               GNUNET_i2s (&id));
463 #endif
464     n = parent->parent;
465     tree_node_destroy(parent);
466     parent = n;
467   }
468 #if MESH_TREE_DEBUG
469   GNUNET_PEER_resolve(parent->peer, &id);
470   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
471              "tree:   Not deleted peer %s.\n",
472              GNUNET_i2s (&id));
473 #endif
474
475   tree_mark_peers_disconnected (t, node, cb);
476
477   return node;
478 }
479
480
481 /**
482  * Return a newly allocated individual path to reach a peer from the local peer,
483  * according to the path tree of some tunnel.
484  *
485  * @param t Tunnel from which to read the path tree
486  * @param peer_info Destination peer to whom we want a path
487  *
488  * @return A newly allocated individual path to reach the destination peer.
489  *         Path must be destroyed afterwards.
490  */
491 struct MeshPeerPath *
492 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
493 {
494   struct MeshTunnelTreeNode *n;
495   struct MeshPeerPath *p;
496   GNUNET_PEER_Id myid = t->me->peer;
497
498   n = tree_find_peer(t->me, peer);
499   p = path_new(0);
500
501   /* Building the path (inverted!) */
502   while (n->peer != myid)
503   {
504     GNUNET_array_append(p->peers, p->length, n->peer);
505     GNUNET_PEER_change_rc(n->peer, 1);
506     n = n->parent;
507     GNUNET_assert(NULL != n);
508   }
509   GNUNET_array_append(p->peers, p->length, myid);
510   GNUNET_PEER_change_rc(myid, 1);
511
512   path_invert(p);
513
514   return p;
515 }
516
517
518
519 /**
520  * Integrate a stand alone path into the tunnel tree.
521  * If the peer toward which the new path is already in the tree, the peer
522  * and its children will be maked as disconnected and the callback
523  * will be called on each one of them. They will be maked as online only after
524  * receiving a PATH ACK for the new path for each one of them, so the caller
525  * should take care of sending a new CREATE PATH message for each disconnected
526  * peer.
527  *
528  * @param t Tunnel where to add the new path.
529  * @param p Path to be integrated.
530  * @param cb Callback to use to notify about peers temporarily disconnecting
531  *
532  * @return GNUNET_OK in case of success.
533  *         GNUNET_SYSERR in case of error.
534  *
535  * TODO: optimize
536  * - go backwards on path looking for each peer in the present tree
537  * - do not disconnect peers until new path is created & connected
538  */
539 int
540 tree_add_path (struct MeshTunnelTree *t,
541                const struct MeshPeerPath *p,
542                MeshNodeDisconnectCB cb)
543 {
544   struct MeshTunnelTreeNode *parent;
545   struct MeshTunnelTreeNode *oldnode;
546   struct MeshTunnelTreeNode *n;
547   struct MeshTunnelTreeNode *c;
548   struct GNUNET_PeerIdentity id;
549   GNUNET_PEER_Id myid;
550   int me;
551 //   int oldnode_is_me;
552   unsigned int i;
553
554 #if MESH_TREE_DEBUG
555   GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
556   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
557              "tree:   Adding path [%u] towards peer %s.\n",
558             p->length,
559             GNUNET_i2s (&id));
560 #endif
561
562   if (NULL != t->me)
563     myid = t->me->peer;
564   else
565     myid = 0;
566   GNUNET_assert(0 != p->length);
567   parent = n = t->root;
568   if (n->peer != p->peers[0])
569   {
570     GNUNET_break (0);
571     return GNUNET_SYSERR;
572   }
573   if (1 == p->length)
574     return GNUNET_OK;
575   oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
576   /* Look for the first node that is not already present in the tree
577    *
578    * Assuming that the tree is somewhat balanced, O(log n * log N).
579    * - Length of the path is expected to be log N (size of whole network).
580    * - Each level of the tree is expected to have log n children (size of tree).
581    */
582   me = t->root->peer == myid ? 0 : -1;
583   for (i = 1; i < p->length; i++)
584   {
585 #if MESH_TREE_DEBUG
586     GNUNET_PEER_resolve(p->peers[i], &id);
587     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
588                "tree:   Looking for peer %s.\n",
589                GNUNET_i2s (&id));
590 #endif
591     parent = n;
592     if (p->peers[i] == myid)
593       me = i;
594     for (c = n->children_head; NULL != c; c = c->next)
595     {
596       if (c->peer == p->peers[i])
597       {
598 #if MESH_TREE_DEBUG
599         GNUNET_PEER_resolve(parent->peer, &id);
600         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
601                    "tree:   Found in children of %s.\n",
602                    GNUNET_i2s (&id));
603 #endif
604         n = c;
605         break;
606       }
607     }
608     /*  If we couldn't find a child equal to path[i], we have reached the end
609      * of the common path. */
610     if (parent == n)
611       break;
612   }
613 #if MESH_TREE_DEBUG
614   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
615              "tree:   All childen visited.\n");
616 #endif
617   /* Add the rest of the path as a branch from parent. */
618   while (i < p->length)
619   {
620 #if MESH_TREE_DEBUG
621     GNUNET_PEER_resolve(p->peers[i], &id);
622     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
623                "tree:   Adding  peer %s.\n",
624                GNUNET_i2s (&id));
625     GNUNET_PEER_resolve(parent->peer, &id);
626     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
627                "tree:     to %s.\n",
628                GNUNET_i2s (&id));
629 #endif
630
631     if (i == p->length - 1 && NULL != oldnode)
632     {
633 #if MESH_TREE_DEBUG
634       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
635                  "tree:   Putting old node into place.\n");
636 #endif
637       oldnode->parent = parent;
638       GNUNET_CONTAINER_DLL_insert(parent->children_head,
639                                   parent->children_tail,
640                                   oldnode);
641       tree_update_first_hops (t, oldnode, NULL);
642       n = oldnode;
643     }
644     else
645     {
646 #if MESH_TREE_DEBUG
647       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Creating new node.\n");
648 #endif
649       n = tree_node_new(parent, p->peers[i]);
650       n->t = t->t;
651       n->status = MESH_PEER_RELAY;
652     }
653     i++;
654     parent = n;
655   }
656   n->status = MESH_PEER_SEARCHING;
657
658   /* Add info about first hop into hashmap. */
659   if (-1 != me && me < p->length - 1)
660   {
661 #if MESH_TREE_DEBUG
662     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663                 "MESH:   finding first hop (own pos %d/%u)\n",
664                 me, p->length - 1);
665 #endif
666     GNUNET_PEER_resolve (p->peers[me + 1], &id);
667     tree_update_first_hops(t,
668                            tree_find_peer(t->root, p->peers[p->length - 1]),
669                            &id);
670   }
671 #if MESH_TREE_DEBUG
672   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   New node added.\n");
673 #endif
674   return GNUNET_OK;
675 }
676
677
678 /**
679  * Notifies a tree that a connection it might be using is broken.
680  * Marks all peers down the paths as disconnected and notifies the client.
681  *
682  * @param t Tree to use.
683  * @param p1 Short id of one of the peers (order unimportant)
684  * @param p2 Short id of one of the peers (order unimportant)
685  * @param cb Function to call for every peer that is marked as disconnected.
686  *
687  * @return Short ID of the first disconnected peer in the tree.
688  */
689 GNUNET_PEER_Id
690 tree_notify_connection_broken (struct MeshTunnelTree *t,
691                                GNUNET_PEER_Id p1,
692                                GNUNET_PEER_Id p2,
693                                MeshNodeDisconnectCB cb)
694 {
695   struct MeshTunnelTreeNode *n;
696   struct MeshTunnelTreeNode *c;
697
698   n = tree_find_peer(t->me, p1);
699   if (NULL == n)
700     return 0;
701   if (NULL != n->parent && n->parent->peer == p2)
702   {
703     tree_mark_peers_disconnected(t, n, cb);
704     GNUNET_CONTAINER_DLL_remove(n->parent->children_head,
705                                 n->parent->children_tail,
706                                 n);
707     GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
708                                 t->disconnected_tail,
709                                 n);
710     return p1;
711   }
712   for (c = n->children_head; NULL != c; c = c->next)
713   {
714     if (c->peer == p2)
715     {
716       tree_mark_peers_disconnected(t, c, cb);
717       GNUNET_CONTAINER_DLL_remove(n->children_head,
718                                   n->children_tail,
719                                   c);
720       GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
721                                   t->disconnected_tail,
722                                   c);
723       return p2;
724     }
725   }
726   return 0;
727 }
728
729
730 /**
731  * Deletes a peer from a tunnel, liberating all unused resources on the path to
732  * it. It shouldn't have children, if it has they will be destroyed as well.
733  * If the tree is not local and no longer has any paths, the root node will be
734  * destroyed and marked as NULL.
735  *
736  * @param t Tunnel tree to use.
737  * @param peer Short ID of the peer to remove from the tunnel tree.
738  * @param cb Callback to notify client of disconnected peers.
739  *
740  * @return GNUNET_OK or GNUNET_SYSERR
741  */
742 int
743 tree_del_peer (struct MeshTunnelTree *t,
744                GNUNET_PEER_Id peer,
745                MeshNodeDisconnectCB cb)
746 {
747   struct MeshTunnelTreeNode *n;
748
749   n = tree_del_path(t, peer, cb);
750   if (NULL == n)
751     return GNUNET_SYSERR;
752   GNUNET_break_op (NULL == n->children_head);
753   tree_node_destroy(n);
754   if (NULL == t->root->children_head && t->me != t->root)
755   {
756     tree_node_destroy (t->root);
757     t->root = NULL;
758   }
759   return GNUNET_OK;
760 }
761
762
763 /**
764  * Print the tree on stderr
765  *
766  * @param t The tree
767  */
768 void
769 tree_debug(struct MeshTunnelTree *t)
770 {
771   tree_node_debug(t->root, 0);
772 }
773
774
775 /**
776  * Iterator over hash map peer entries and frees all data in it.
777  * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
778  *
779  * @param cls closure
780  * @param key current key code (will no longer contain valid data!!)
781  * @param value value in the hash map (treated as void *)
782  * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
783  */
784 static int
785 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
786 {
787   GNUNET_free(value);
788   return GNUNET_YES;
789 }
790
791
792 /**
793  * Destroy the whole tree and free all used memory and Peer_Ids
794  * 
795  * @param t Tree to be destroyed
796  */
797 void
798 tree_destroy (struct MeshTunnelTree *t)
799 {
800   tree_node_destroy(t->root);
801   GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
802   GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);
803   GNUNET_free(t);
804 }