Fix testcase
[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   struct GNUNET_PeerIdentity id;;
204   uint16_t i;
205
206   for (i = 0; i < level; i++)
207     fprintf(stderr, "  ");
208   if (n->status == MESH_PEER_READY)
209     fprintf(stderr, "#");
210   if (n->status == MESH_PEER_SEARCHING)
211     fprintf(stderr, "+");
212   if (n->status == MESH_PEER_RELAY)
213     fprintf(stderr, "-");
214   if (n->status == MESH_PEER_RECONNECTING)
215     fprintf(stderr, "*");
216
217   GNUNET_PEER_resolve(n->peer, &id);
218   fprintf(stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
219   if (NULL != n->parent)
220   {
221     GNUNET_PEER_resolve(n->parent->peer, &id);
222     fprintf(stderr, "(-> %s [%u])\n", GNUNET_i2s(&id), n->parent->peer);
223   }
224   else
225     fprintf(stderr, "(root)\n");
226   for (c = n->children_head; NULL != c; c = c->next)
227     tree_node_debug(c, level + 1);
228 }
229
230
231 /**
232  * Destroys and frees the node and all children
233  *
234  * @param n Parent node to be destroyed
235  */
236 static void
237 tree_node_destroy (struct MeshTunnelTreeNode *parent)
238 {
239   struct MeshTunnelTreeNode *n;
240   struct MeshTunnelTreeNode *next;
241 #if MESH_TREE_DEBUG
242   struct GNUNET_PeerIdentity id;
243
244   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245               "tree: Destroying node %u\n",
246               parent->peer);
247   GNUNET_PEER_resolve (parent->peer, &id);
248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249               "tree:   (%s)\n",
250               GNUNET_i2s (&id));
251 #endif
252   n = parent->children_head;
253   while (NULL != n)
254   {
255     next = n->next;
256     tree_node_destroy(n);
257     n = next;
258   }
259   GNUNET_PEER_change_rc(parent->peer, -1);
260   if (NULL != parent->parent)
261     GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
262                                 parent->parent->children_tail,
263                                 parent);
264   GNUNET_free(parent);
265 }
266
267
268
269 /**
270  * Create a new tunnel tree associated to a tunnel
271  *
272  * @param t Tunnel this tree will represent
273  * @param peer A short peer id of the root of the tree
274  *
275  * @return A newly allocated and initialized tunnel tree
276  */
277 struct MeshTunnelTree *
278 tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
279 {
280   struct MeshTunnelTree *tree;
281
282   tree = GNUNET_malloc(sizeof (struct MeshTunnelTree));
283   tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
284   tree->root = tree_node_new(NULL, peer);
285   tree->root->status = MESH_PEER_ROOT;
286   tree->t = t;
287   tree->root->t = t;
288
289   return tree;
290 }
291
292
293 /**
294  * Recursively find the given peer in the tree.
295  *
296  * @param t Tunnel where to look for the peer.
297  * @param peer Peer to find
298  *
299  * @return Pointer to the node of the peer. NULL if not found.
300  */
301 struct MeshTunnelTreeNode *
302 tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
303 {
304   struct MeshTunnelTreeNode *n;
305   struct MeshTunnelTreeNode *r;
306
307   if (parent->peer == peer_id)
308     return parent;
309   for (n = parent->children_head; NULL != n; n = n->next)
310   {
311     r = tree_find_peer (n, peer_id);
312     if (NULL != r)
313       return r;
314   }
315   return NULL;
316 }
317
318
319 /**
320  * Recusively mark peer and children as disconnected, notify client
321  *
322  * @param tree Tree this node belongs to
323  * @param parent Node to be clean, potentially with children
324  * @param cb Callback to use to notify about disconnected peers.
325  */
326 static void
327 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
328                               struct MeshTunnelTreeNode *parent,
329                               MeshNodeDisconnectCB cb)
330 {
331   struct GNUNET_PeerIdentity *pi;
332   struct GNUNET_PeerIdentity id;
333   struct MeshTunnelTreeNode *n;
334
335   for (n = parent->children_head; NULL != n; n = n->next)
336   {
337     tree_mark_peers_disconnected (tree, n, cb);
338   }
339   if (MESH_PEER_READY == parent->status)
340   {
341     if (NULL != cb)
342       cb (parent);
343     parent->status = MESH_PEER_RECONNECTING;
344   }
345
346   /* Remove and free info about first hop */
347   GNUNET_PEER_resolve(parent->peer, &id);
348   pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
349   GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
350   if (NULL != pi)
351     GNUNET_free(pi);
352 }
353
354
355 /**
356  * Recusively update the info about what is the first hop to reach the node
357  *
358  * @param tree Tree this nodes belongs to
359  * @param parent Node to be start updating
360  * @param hop If known, ID of the first hop.
361  *            If not known, NULL to find out and pass on children.
362  */
363 void
364 tree_update_first_hops (struct MeshTunnelTree *tree,
365                         struct MeshTunnelTreeNode *parent,
366                         struct GNUNET_PeerIdentity *hop)
367 {
368   struct GNUNET_PeerIdentity pi;
369   struct GNUNET_PeerIdentity *copy;
370   struct GNUNET_PeerIdentity id;
371   struct MeshTunnelTreeNode *n;
372
373 #if MESH_TREE_DEBUG
374   GNUNET_PEER_resolve(parent->peer, &id);
375   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
376           "tree:   Finding first hop for %s.\n",
377           GNUNET_i2s (&id));
378 #endif
379   if (NULL == hop)
380   {
381     struct MeshTunnelTreeNode *aux;
382     struct MeshTunnelTreeNode *old;
383
384     aux = old = parent;
385     while (aux != tree->me)
386     {
387 #if MESH_TREE_DEBUG
388       GNUNET_PEER_resolve(old->peer, &id);
389       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
390              "tree:   ... its not %s.\n",
391              GNUNET_i2s (&id));
392 #endif
393       old = aux;
394       aux = aux->parent;
395       GNUNET_assert(NULL != aux);
396     }
397 #if MESH_TREE_DEBUG
398     GNUNET_PEER_resolve(old->peer, &id);
399     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
400                "tree:   It's %s!\n",
401                GNUNET_i2s (&id));
402 #endif
403     hop = &pi;
404     GNUNET_PEER_resolve(old->peer, hop);
405   }
406   copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
407   *copy = *hop;
408   GNUNET_PEER_resolve(parent->peer, &id);
409   GNUNET_CONTAINER_multihashmap_put(
410     tree->first_hops,
411     &id.hashPubKey,
412     copy,
413     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
414
415   for (n = parent->children_head; NULL != n; n = n->next)
416   {
417     tree_update_first_hops (tree, n, hop);
418   }
419 }
420
421
422 /**
423  * Delete the current path to the peer, including all now unused relays.
424  * The destination peer is NOT destroyed, it is returned in order to either set
425  * a new path to it or destroy it explicitly, taking care of it's child nodes.
426  *
427  * @param t Tunnel tree where to delete the path from.
428  * @param peer Destination peer whose path we want to remove.
429  * @param cb Callback to use to notify about disconnected peers.
430  *
431  * @return pointer to the pathless node.
432  *         NULL when not found
433  */
434 struct MeshTunnelTreeNode *
435 tree_del_path (struct MeshTunnelTree *t,
436                GNUNET_PEER_Id peer_id,
437                MeshNodeDisconnectCB cb)
438 {
439   struct MeshTunnelTreeNode *parent;
440   struct MeshTunnelTreeNode *node;
441   struct MeshTunnelTreeNode *n;
442
443 #if MESH_TREE_DEBUG
444   struct GNUNET_PeerIdentity id;
445   GNUNET_PEER_resolve(peer_id, &id);
446   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
447           "tree:   Deleting path to %s.\n",
448           GNUNET_i2s (&id));
449 #endif
450   if (peer_id == t->root->peer)
451     return NULL;
452
453   for (n = t->disconnected_head; NULL != n; n = n->next)
454   {
455     if (n->peer == peer_id)
456     {
457       /* Was already pathless, waiting for reconnection */
458       GNUNET_CONTAINER_DLL_remove (t->disconnected_head,
459                                    t->disconnected_tail,
460                                    n);
461       return n;
462     }
463   }
464   n = tree_find_peer (t->root, peer_id);
465   if (NULL == n)
466     return NULL;
467   node = n;
468
469   parent = n->parent;
470   GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
471   n->parent = NULL;
472
473   while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
474   {
475 #if MESH_TREE_DEBUG
476     GNUNET_PEER_resolve(parent->peer, &id);
477     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
478               "tree:   Deleting node %s.\n",
479               GNUNET_i2s (&id));
480 #endif
481     n = parent->parent;
482     tree_node_destroy(parent);
483     parent = n;
484   }
485 #if MESH_TREE_DEBUG
486   GNUNET_PEER_resolve(parent->peer, &id);
487   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
488              "tree:   Not deleted peer %s.\n",
489              GNUNET_i2s (&id));
490 #endif
491
492   tree_mark_peers_disconnected (t, node, cb);
493
494   return node;
495 }
496
497
498 /**
499  * Return a newly allocated individual path to reach a peer from the local peer,
500  * according to the path tree of some tunnel.
501  *
502  * @param t Tunnel from which to read the path tree
503  * @param peer_info Destination peer to whom we want a path
504  *
505  * @return A newly allocated individual path to reach the destination peer.
506  *         Path must be destroyed afterwards.
507  */
508 struct MeshPeerPath *
509 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
510 {
511   struct MeshTunnelTreeNode *n;
512   struct MeshPeerPath *p;
513   GNUNET_PEER_Id myid = t->me->peer;
514
515   n = tree_find_peer(t->me, peer);
516   p = path_new(0);
517
518   /* Building the path (inverted!) */
519   while (n->peer != myid)
520   {
521     GNUNET_array_append(p->peers, p->length, n->peer);
522     GNUNET_PEER_change_rc(n->peer, 1);
523     n = n->parent;
524     GNUNET_assert(NULL != n);
525   }
526   GNUNET_array_append(p->peers, p->length, myid);
527   GNUNET_PEER_change_rc(myid, 1);
528
529   path_invert(p);
530
531   return p;
532 }
533
534
535
536 /**
537  * Integrate a stand alone path into the tunnel tree.
538  * If the peer toward which the new path is already in the tree, the peer
539  * and its children will be maked as disconnected and the callback
540  * will be called on each one of them. They will be maked as online only after
541  * receiving a PATH ACK for the new path for each one of them, so the caller
542  * should take care of sending a new CREATE PATH message for each disconnected
543  * peer.
544  *
545  * @param t Tunnel where to add the new path.
546  * @param p Path to be integrated.
547  * @param cb Callback to use to notify about peers temporarily disconnecting
548  *
549  * @return GNUNET_OK in case of success.
550  *         GNUNET_SYSERR in case of error.
551  *
552  * TODO: optimize
553  * - go backwards on path looking for each peer in the present tree
554  * - do not disconnect peers until new path is created & connected
555  */
556 int
557 tree_add_path (struct MeshTunnelTree *t,
558                const struct MeshPeerPath *p,
559                MeshNodeDisconnectCB cb)
560 {
561   struct MeshTunnelTreeNode *parent;
562   struct MeshTunnelTreeNode *oldnode;
563   struct MeshTunnelTreeNode *n;
564   struct MeshTunnelTreeNode *c;
565   struct GNUNET_PeerIdentity id;
566   GNUNET_PEER_Id myid;
567   int me;
568 //   int oldnode_is_me;
569   unsigned int i;
570
571 #if MESH_TREE_DEBUG
572   GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
573   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
574              "tree:   Adding path [%u] towards peer %s.\n",
575             p->length,
576             GNUNET_i2s (&id));
577 #endif
578
579   if (NULL != t->me)
580     myid = t->me->peer;
581   else
582     myid = 0;
583   GNUNET_assert(0 != p->length);
584   parent = n = t->root;
585   if (n->peer != p->peers[0])
586   {
587     GNUNET_break (0);
588     return GNUNET_SYSERR;
589   }
590   if (1 == p->length)
591     return GNUNET_OK;
592   oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
593   /* Look for the first node that is not already present in the tree
594    *
595    * Assuming that the tree is somewhat balanced, O(log n * log N).
596    * - Length of the path is expected to be log N (size of whole network).
597    * - Each level of the tree is expected to have log n children (size of tree).
598    */
599   me = t->root->peer == myid ? 0 : -1;
600   for (i = 1; i < p->length; i++)
601   {
602 #if MESH_TREE_DEBUG
603     GNUNET_PEER_resolve(p->peers[i], &id);
604     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
605                "tree:   Looking for peer %s.\n",
606                GNUNET_i2s (&id));
607 #endif
608     parent = n;
609     if (p->peers[i] == myid)
610       me = i;
611     for (c = n->children_head; NULL != c; c = c->next)
612     {
613       if (c->peer == p->peers[i])
614       {
615 #if MESH_TREE_DEBUG
616         GNUNET_PEER_resolve(parent->peer, &id);
617         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
618                    "tree:   Found in children of %s.\n",
619                    GNUNET_i2s (&id));
620 #endif
621         n = c;
622         break;
623       }
624     }
625     /*  If we couldn't find a child equal to path[i], we have reached the end
626      * of the common path. */
627     if (parent == n)
628       break;
629   }
630 #if MESH_TREE_DEBUG
631   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
632              "tree:   All childen visited.\n");
633 #endif
634   /* Add the rest of the path as a branch from parent. */
635   while (i < p->length)
636   {
637 #if MESH_TREE_DEBUG
638     GNUNET_PEER_resolve(p->peers[i], &id);
639     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
640                "tree:   Adding  peer %s.\n",
641                GNUNET_i2s (&id));
642     GNUNET_PEER_resolve(parent->peer, &id);
643     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
644                "tree:     to %s.\n",
645                GNUNET_i2s (&id));
646 #endif
647
648     if (i == p->length - 1 && NULL != oldnode)
649     {
650 #if MESH_TREE_DEBUG
651       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
652                  "tree:   Putting old node into place.\n");
653 #endif
654       oldnode->parent = parent;
655       GNUNET_CONTAINER_DLL_insert(parent->children_head,
656                                   parent->children_tail,
657                                   oldnode);
658       tree_update_first_hops (t, oldnode, NULL);
659       n = oldnode;
660     }
661     else
662     {
663 #if MESH_TREE_DEBUG
664       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Creating new node.\n");
665 #endif
666       n = tree_node_new(parent, p->peers[i]);
667       n->t = t->t;
668       n->status = MESH_PEER_RELAY;
669     }
670     i++;
671     parent = n;
672   }
673   n->status = MESH_PEER_SEARCHING;
674
675   /* Add info about first hop into hashmap. */
676   if (-1 != me && me < p->length - 1)
677   {
678 #if MESH_TREE_DEBUG
679     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680                 "MESH:   finding first hop (own pos %d/%u)\n",
681                 me, p->length - 1);
682 #endif
683     GNUNET_PEER_resolve (p->peers[me + 1], &id);
684     tree_update_first_hops(t,
685                            tree_find_peer(t->root, p->peers[p->length - 1]),
686                            &id);
687   }
688 #if MESH_TREE_DEBUG
689   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   New node added.\n");
690 #endif
691   return GNUNET_OK;
692 }
693
694
695 /**
696  * Notifies a tree that a connection it might be using is broken.
697  * Marks all peers down the paths as disconnected and notifies the client.
698  *
699  * @param t Tree to use.
700  * @param p1 Short id of one of the peers (order unimportant)
701  * @param p2 Short id of one of the peers (order unimportant)
702  * @param cb Function to call for every peer that is marked as disconnected.
703  *
704  * @return Short ID of the first disconnected peer in the tree.
705  */
706 GNUNET_PEER_Id
707 tree_notify_connection_broken (struct MeshTunnelTree *t,
708                                GNUNET_PEER_Id p1,
709                                GNUNET_PEER_Id p2,
710                                MeshNodeDisconnectCB cb)
711 {
712   struct MeshTunnelTreeNode *n;
713   struct MeshTunnelTreeNode *c;
714
715   n = tree_find_peer(t->me, p1);
716   if (NULL == n)
717     return 0;
718   if (NULL != n->parent && n->parent->peer == p2)
719   {
720     tree_mark_peers_disconnected(t, n, cb);
721     GNUNET_CONTAINER_DLL_remove(n->parent->children_head,
722                                 n->parent->children_tail,
723                                 n);
724     GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
725                                 t->disconnected_tail,
726                                 n);
727     return p1;
728   }
729   for (c = n->children_head; NULL != c; c = c->next)
730   {
731     if (c->peer == p2)
732     {
733       tree_mark_peers_disconnected(t, c, cb);
734       GNUNET_CONTAINER_DLL_remove(n->children_head,
735                                   n->children_tail,
736                                   c);
737       GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
738                                   t->disconnected_tail,
739                                   c);
740       return p2;
741     }
742   }
743   return 0;
744 }
745
746
747 /**
748  * Deletes a peer from a tunnel, liberating all unused resources on the path to
749  * it. It shouldn't have children, if it has they will be destroyed as well.
750  * If the tree is not local and no longer has any paths, the root node will be
751  * destroyed and marked as NULL.
752  *
753  * @param t Tunnel tree to use.
754  * @param peer Short ID of the peer to remove from the tunnel tree.
755  * @param cb Callback to notify client of disconnected peers.
756  *
757  * @return GNUNET_OK or GNUNET_SYSERR
758  */
759 int
760 tree_del_peer (struct MeshTunnelTree *t,
761                GNUNET_PEER_Id peer,
762                MeshNodeDisconnectCB cb)
763 {
764   struct MeshTunnelTreeNode *n;
765
766   n = tree_del_path(t, peer, cb);
767   if (NULL == n)
768     return GNUNET_SYSERR;
769   GNUNET_break_op (NULL == n->children_head);
770   tree_node_destroy(n);
771   if (NULL == t->root->children_head && t->me != t->root)
772   {
773     tree_node_destroy (t->root);
774     t->root = NULL;
775   }
776   return GNUNET_OK;
777 }
778
779
780 /**
781  * Print the tree on stderr
782  *
783  * @param t The tree
784  */
785 void
786 tree_debug(struct MeshTunnelTree *t)
787 {
788   tree_node_debug(t->root, 0);
789 }
790
791
792 /**
793  * Iterator over hash map peer entries and frees all data in it.
794  * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
795  *
796  * @param cls closure
797  * @param key current key code (will no longer contain valid data!!)
798  * @param value value in the hash map (treated as void *)
799  * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
800  */
801 static int
802 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
803 {
804   GNUNET_free(value);
805   return GNUNET_YES;
806 }
807
808
809 /**
810  * Destroy the whole tree and free all used memory and Peer_Ids
811  * 
812  * @param t Tree to be destroyed
813  */
814 void
815 tree_destroy (struct MeshTunnelTree *t)
816 {
817 #if MESH_TREE_DEBUG
818   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
819 #endif
820   tree_node_destroy(t->root);
821   GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
822   GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);
823   GNUNET_free(t);
824 }