Allowed to destroy NULL paths
[oweals/gnunet.git] / src / mesh / mesh_tunnel_tree.c
index 74778909693fab0981119ab06247c5de126cec21..a80cbb0c07d5763d11cbee32a7ad1194352b3a12 100644 (file)
 #include "mesh.h"
 #include "mesh_tunnel_tree.h"
 
+#define MESH_TREE_DEBUG GNUNET_YES
 
-static void
-debug_node(struct MeshTunnelTreeNode *n, uint16_t level)
-{
-  uint16_t i;
-
-  for (i = 0; i < level; i++)
-    fprintf(stderr, " ");
-  fprintf(stderr, "%u\n", n->peer);
-  for (i = 0; i < n->nchildren; i++)
-    debug_node(&n->children[i], level + 1);
-}
 
-
-
-void
-tree_debug(struct MeshTunnelTree *t)
+/**
+ * Create a new path
+ *
+ * @param lenght How many hops will the path have.
+ *
+ * @return A newly allocated path with a peer array of the specified length.
+ */
+struct MeshPeerPath *
+path_new (unsigned int length)
 {
-  debug_node(t->root, 0);
-}
+  struct MeshPeerPath *p;
 
+  p = GNUNET_malloc (sizeof(struct MeshPeerPath));
+  if (length > 0)
+  {
+    p->length = length;
+    p->peers = GNUNET_malloc (length * sizeof(GNUNET_PEER_Id));
+  }
+  return p;
+}
 
 
 /**
@@ -71,19 +73,21 @@ path_invert (struct MeshPeerPath *path)
 
 
 /**
- * Destroy the path and free any allocated resources linked to it
+ * Duplicate a path, incrementing short peer's rc.
  *
- * @param p the path to destroy
- *
- * @return GNUNET_OK on success
+ * @param p The path to duplicate.
  */
-int
-path_destroy (struct MeshPeerPath *p)
+struct MeshPeerPath *
+path_duplicate (struct MeshPeerPath *path)
 {
-  GNUNET_PEER_decrement_rcs (p->peers, p->length);
-  GNUNET_free (p->peers);
-  GNUNET_free (p);
-  return GNUNET_OK;
+  struct MeshPeerPath *aux;
+  unsigned int i;
+
+  aux = path_new(path->length);
+  memcpy (aux->peers, path->peers, path->length * sizeof(GNUNET_PEER_Id));
+  for (i = 0; i < path->length; i++)
+    GNUNET_PEER_change_rc(path->peers[i], 1);
+  return aux;
 }
 
 
@@ -100,10 +104,13 @@ struct GNUNET_PeerIdentity *
 path_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
 {
   struct GNUNET_PeerIdentity id;
+  struct GNUNET_PeerIdentity *r;
 
   GNUNET_PEER_resolve (peer, &id);
-  return GNUNET_CONTAINER_multihashmap_get (t->first_hops,
-                                            &id.hashPubKey);
+  r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
+  GNUNET_break (NULL != r);
+
+  return r;
 }
 
 
@@ -142,6 +149,132 @@ path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
 }
 
 
+/**
+ * Destroy the path and free any allocated resources linked to it
+ *
+ * @param p the path to destroy
+ *
+ * @return GNUNET_OK on success
+ */
+int
+path_destroy (struct MeshPeerPath *p)
+{
+  if (NULL == p)
+    return GNUNET_OK;
+  GNUNET_PEER_decrement_rcs (p->peers, p->length);
+  GNUNET_free (p->peers);
+  GNUNET_free (p);
+  return GNUNET_OK;
+}
+
+
+
+/**
+ * Allocates and initializes a new node.
+ * Sets ID and parent of the new node and inserts it in the DLL of the parent
+ *
+ * @param parent Node that will be the parent from the new node, NULL for root
+ * @param peer Short Id of the new node
+ *
+ * @return Newly allocated node
+ */
+static struct MeshTunnelTreeNode *
+tree_node_new(struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
+{
+  struct MeshTunnelTreeNode *node;
+
+  node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
+  node->peer = peer;
+  GNUNET_PEER_change_rc(peer, 1);
+  node->parent = parent;
+  if (NULL != parent)
+    GNUNET_CONTAINER_DLL_insert(parent->children_head,
+                                parent->children_tail,
+                                node);
+
+  return node;
+}
+
+
+static void
+tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
+{
+  struct MeshTunnelTreeNode *c;
+  uint16_t i;
+
+  for (i = 0; i < level; i++)
+    fprintf(stderr, "  ");
+  if (n->status == MESH_PEER_READY)
+    fprintf(stderr, "#");
+  if (n->status == MESH_PEER_SEARCHING)
+    fprintf(stderr, "+");
+  if (n->status == MESH_PEER_RELAY)
+    fprintf(stderr, "-");
+  if (n->status == MESH_PEER_RECONNECTING)
+    fprintf(stderr, "*");
+
+  fprintf(stderr, "%u [%p] ", n->peer, n);
+  if (NULL != n->parent)
+    fprintf(stderr, "(-> %u)\n", n->parent->peer);
+  else
+    fprintf(stderr, "(root)\n");
+  for (c = n->children_head; NULL != c; c = c->next)
+    tree_node_debug(c, level + 1);
+}
+
+
+/**
+ * Destroys and frees the node and all children
+ *
+ * @param n Parent node to be destroyed
+ */
+static void
+tree_node_destroy (struct MeshTunnelTreeNode *parent)
+{
+  struct MeshTunnelTreeNode *n;
+  struct MeshTunnelTreeNode *next;
+
+  n = parent->children_head;
+  while (NULL != n)
+  {
+    next = n->next;
+    tree_node_destroy(n);
+    n = next;
+  }
+  GNUNET_PEER_change_rc(parent->peer, -1);
+  if (NULL != parent->parent)
+    GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
+                                parent->parent->children_tail,
+                                parent);
+  GNUNET_free(parent);
+}
+
+
+
+/**
+ * Create a new tunnel tree associated to a tunnel
+ *
+ * @param t Tunnel this tree will represent
+ * @param peer A short peer id of the root of the tree
+ *
+ * @return A newly allocated and initialized tunnel tree
+ */
+struct MeshTunnelTree *
+tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
+{
+  struct MeshTunnelTree *tree;
+
+  tree = GNUNET_malloc(sizeof (struct MeshTunnelTree));
+  tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
+  tree->root = tree_node_new(NULL, peer);
+  tree->root->status = MESH_PEER_ROOT;
+  tree->t = t;
+  tree->root->t = t;
+
+  return tree;
+}
+
+
 /**
  * Recursively find the given peer in the tree.
  *
@@ -151,18 +284,18 @@ path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
  * @return Pointer to the node of the peer. NULL if not found.
  */
 struct MeshTunnelTreeNode *
-tree_find_peer (struct MeshTunnelTreeNode *root, GNUNET_PEER_Id peer_id)
+tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
 {
   struct MeshTunnelTreeNode *n;
-  unsigned int i;
+  struct MeshTunnelTreeNode *r;
 
-  if (root->peer == peer_id)
-    return root;
-  for (i = 0; i < root->nchildren; i++)
+  if (parent->peer == peer_id)
+    return parent;
+  for (n = parent->children_head; NULL != n; n = n->next)
   {
-    n = tree_find_peer (&root->children[i], peer_id);
-    if (NULL != n)
-      return n;
+    r = tree_find_peer (n, peer_id);
+    if (NULL != r)
+      return r;
   }
   return NULL;
 }
@@ -175,43 +308,31 @@ tree_find_peer (struct MeshTunnelTreeNode *root, GNUNET_PEER_Id peer_id)
  * @param parent Node to be clean, potentially with children
  * @param cb Callback to use to notify about disconnected peers.
  */
-void
+static void
 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
                               struct MeshTunnelTreeNode *parent,
                               MeshNodeDisconnectCB cb)
 {
   struct GNUNET_PeerIdentity *pi;
   struct GNUNET_PeerIdentity id;
-  unsigned int i;
+  struct MeshTunnelTreeNode *n;
 
-  for (i = 0; i < parent->nchildren; i++)
+  for (n = parent->children_head; NULL != n; n = n->next)
   {
-    tree_mark_peers_disconnected (tree, &parent->children[i], cb);
+    tree_mark_peers_disconnected (tree, n, cb);
   }
-  if (MESH_PEER_READY == parent->status)
+  if (MESH_PEER_READY == parent->status && NULL != cb)
   {
     cb (parent);
   }
   parent->status = MESH_PEER_RECONNECTING;
-  
+
   /* Remove and free info about first hop */
   GNUNET_PEER_resolve(parent->peer, &id);
   pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
   GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
   if (NULL != pi)
     GNUNET_free(pi);
-//   FIXME: add to service code on callback
-//   struct GNUNET_MESH_PeerControl msg;
-//   if (NULL == parent->t->client)
-//     return;
-//   msg.header.size = htons (sizeof (msg));
-//   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
-//   msg.tunnel_id = htonl (parent->t->local_tid);
-//   GNUNET_PEER_resolve (parent->peer, &msg.peer);
-//   if (NULL == nc)
-//     return;
-//   GNUNET_SERVER_notification_context_unicast (nc, parent->t->client->handle,
-//                                               &msg.header, GNUNET_NO);
 }
 
 
@@ -231,42 +352,53 @@ tree_update_first_hops (struct MeshTunnelTree *tree,
   struct GNUNET_PeerIdentity pi;
   struct GNUNET_PeerIdentity *copy;
   struct GNUNET_PeerIdentity id;
-  unsigned int i;
+  struct MeshTunnelTreeNode *n;
 
+#if MESH_TREE_DEBUG
+  GNUNET_PEER_resolve(parent->peer, &id);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Finding first hop for %u.\n",
-             parent->peer);
+          "tree:   Finding first hop for %s.\n",
+          GNUNET_i2s (&id));
+#endif
   if (NULL == hop)
   {
     struct MeshTunnelTreeNode *aux;
     struct MeshTunnelTreeNode *old;
 
-    old = parent;
-    aux = old->parent;
+    aux = old = parent;
     while (aux != tree->me)
     {
+#if MESH_TREE_DEBUG
+      GNUNET_PEER_resolve(old->peer, &id);
       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   ... its not %u.\n",
-             old->peer);
+             "tree:   ... its not %s.\n",
+             GNUNET_i2s (&id));
+#endif
       old = aux;
       aux = aux->parent;
       GNUNET_assert(NULL != aux);
     }
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(old->peer, &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   It's %u!!\n",
-             old->peer);
+               "tree:   It's %s!\n",
+               GNUNET_i2s (&id));
+#endif
     hop = &pi;
     GNUNET_PEER_resolve(old->peer, hop);
   }
   copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
   *copy = *hop;
   GNUNET_PEER_resolve(parent->peer, &id);
-  GNUNET_CONTAINER_multihashmap_put(tree->first_hops, &id.hashPubKey, copy,
-                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+  GNUNET_CONTAINER_multihashmap_put(
+    tree->first_hops,
+    &id.hashPubKey,
+    copy,
+    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
 
-  for (i = 0; i < parent->nchildren; i++)
+  for (n = parent->children_head; NULL != n; n = n->next)
   {
-    tree_update_first_hops (tree, &parent->children[i], hop);
+    tree_update_first_hops (tree, n, hop);
   }
 }
 
@@ -280,7 +412,8 @@ tree_update_first_hops (struct MeshTunnelTree *tree,
  * @param peer Destination peer whose path we want to remove.
  * @param cb Callback to use to notify about disconnected peers.
  *
- * @return pointer to the pathless node, NULL on error
+ * @return pointer to the pathless node.
+ *         NULL when not found
  */
 struct MeshTunnelTreeNode *
 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
@@ -290,29 +423,54 @@ tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
   struct MeshTunnelTreeNode *node;
   struct MeshTunnelTreeNode *n;
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Deleting path to %u.\n", peer_id);
+#if MESH_TREE_DEBUG
+  struct GNUNET_PeerIdentity id;
+  GNUNET_PEER_resolve(peer_id, &id);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+          "tree:   Deleting path to %s.\n",
+          GNUNET_i2s (&id));
+#endif
   if (peer_id == t->root->peer)
     return NULL;
-  n = tree_find_peer (t->me, peer_id);
+
+  for (n = t->disconnected_head; NULL != n; n = n->next)
+  {
+    if (n->peer == peer_id)
+    {
+      /* Was already pathless, waiting for reconnection */
+      GNUNET_CONTAINER_DLL_remove (t->disconnected_head,
+                                   t->disconnected_tail,
+                                   n);
+      return n;
+    }
+  }
+  n = tree_find_peer (t->root, peer_id);
   if (NULL == n)
     return NULL;
-  node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
-  *node = *n;
+  node = n;
+
   parent = n->parent;
-  parent->nchildren--;
+  GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
   n->parent = NULL;
-  *n = parent->children[parent->nchildren];
-  parent->children = GNUNET_realloc(parent->children,
-                                    parent->nchildren
-                                    * sizeof(struct MeshTunnelTreeNode));
-  while (t->root != parent && MESH_PEER_RELAY == parent->status &&
-         0 == parent->nchildren)
+
+  while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Deleting node %u.\n", parent->peer);
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(parent->peer, &id);
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+              "tree:   Deleting node %s.\n",
+              GNUNET_i2s (&id));
+#endif
     n = parent->parent;
     tree_node_destroy(parent);
     parent = n;
   }
+#if MESH_TREE_DEBUG
+  GNUNET_PEER_resolve(parent->peer, &id);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "tree:   Not deleted peer %s.\n",
+             GNUNET_i2s (&id));
+#endif
 
   tree_mark_peers_disconnected (t, node, cb);
 
@@ -338,7 +496,7 @@ tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
   GNUNET_PEER_Id myid = t->me->peer;
 
   n = tree_find_peer(t->me, peer);
-  p = GNUNET_malloc(sizeof(struct MeshPeerPath));
+  p = path_new(0);
 
   /* Building the path (inverted!) */
   while (n->peer != myid)
@@ -357,8 +515,15 @@ tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
 }
 
 
+
 /**
  * Integrate a stand alone path into the tunnel tree.
+ * If the peer toward which the new path is already in the tree, the peer
+ * and its children will be maked as disconnected and the callback
+ * will be called on each one of them. They will be maked as online only after
+ * receiving a PATH ACK for the new path for each one of them, so the caller
+ * should take care of sending a new CREATE PATH message for each disconnected
+ * peer.
  *
  * @param t Tunnel where to add the new path.
  * @param p Path to be integrated.
@@ -372,24 +537,32 @@ tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
  * - do not disconnect peers until new path is created & connected
  */
 int
-tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
-                 MeshNodeDisconnectCB cb)
+tree_add_path (struct MeshTunnelTree *t,
+               const struct MeshPeerPath *p,
+               MeshNodeDisconnectCB cb)
 {
   struct MeshTunnelTreeNode *parent;
   struct MeshTunnelTreeNode *oldnode;
   struct MeshTunnelTreeNode *n;
+  struct MeshTunnelTreeNode *c;
   struct GNUNET_PeerIdentity id;
-  struct GNUNET_PeerIdentity *hop;
-  GNUNET_PEER_Id myid = t->me->peer;
+  GNUNET_PEER_Id myid;
   int me;
+//   int oldnode_is_me;
   unsigned int i;
-  unsigned int j;
 
+#if MESH_TREE_DEBUG
+  GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Adding path [%u] towards peer %u to peer %u.\n",
-             p->length,
-             p->peers[p->length - 1],
-             t->me->peer);
+             "tree:   Adding path [%u] towards peer %s.\n",
+            p->length,
+            GNUNET_i2s (&id));
+#endif
+
+  if (NULL != t->me)
+    myid = t->me->peer;
+  else
+    myid = 0;
   GNUNET_assert(0 != p->length);
   parent = n = t->root;
   if (n->peer != p->peers[0])
@@ -409,17 +582,26 @@ tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
   me = t->root->peer == myid ? 0 : -1;
   for (i = 1; i < p->length; i++)
   {
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(p->peers[i], &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Looking for peer %u.\n",
-             p->peers[i]);
+               "tree:   Looking for peer %s.\n",
+               GNUNET_i2s (&id));
+#endif
     parent = n;
     if (p->peers[i] == myid)
       me = i;
-    for (j = 0; j < n->nchildren; j++)
+    for (c = n->children_head; NULL != c; c = c->next)
     {
-      if (n->children[j].peer == p->peers[i])
+      if (c->peer == p->peers[i])
       {
-        n = &n->children[j];
+#if MESH_TREE_DEBUG
+        GNUNET_PEER_resolve(parent->peer, &id);
+        GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                   "tree:   Found in children of %s.\n",
+                   GNUNET_i2s (&id));
+#endif
+        n = c;
         break;
       }
     }
@@ -428,48 +610,45 @@ tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
     if (parent == n)
       break;
   }
+#if MESH_TREE_DEBUG
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
              "tree:   All childen visited.\n");
-  if (-1 == me)
-  {
-    /* New path deviates from tree before reaching us. What happened? */
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
+#endif
   /* Add the rest of the path as a branch from parent. */
   while (i < p->length)
   {
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(p->peers[i], &id);
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+               "tree:   Adding  peer %s.\n",
+               GNUNET_i2s (&id));
+    GNUNET_PEER_resolve(parent->peer, &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-               "tree:   Adding  peer %u, to %u.\n",
-               p->peers[i],
-               parent->peer);
-    parent->nchildren++;
-    parent->children = GNUNET_realloc (parent->children,
-                                       parent->nchildren *
-                                       sizeof(struct MeshTunnelTreeNode));
-    n = &parent->children[parent->nchildren - 1];
-    n->parent = parent;
+               "tree:     to %s.\n",
+               GNUNET_i2s (&id));
+#endif
+
     if (i == p->length - 1 && NULL != oldnode)
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Putting old node into place.\n");
-      /* Assignation and free can be misleading, using explicit mempcy */
-      memcpy (n, oldnode, sizeof (struct MeshTunnelTreeNode));
-      n->parent = parent;
-      GNUNET_free (oldnode);
-      for (j = 0; j < n->nchildren; j++)
-      {
-        n->children[j].parent = n;
-        tree_update_first_hops (t, &n->children[j], NULL);
-      }
+#if MESH_TREE_DEBUG
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                 "tree:   Putting old node into place.\n");
+#endif
+      oldnode->parent = parent;
+      GNUNET_CONTAINER_DLL_insert(parent->children_head,
+                                  parent->children_tail,
+                                  oldnode);
+      tree_update_first_hops (t, oldnode, NULL);
+      n = oldnode;
     }
     else
     {
+#if MESH_TREE_DEBUG
       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Creating new node.\n");
+#endif
+      n = tree_node_new(parent, p->peers[i]);
       n->t = t->t;
       n->status = MESH_PEER_RELAY;
-      n->peer = p->peers[i];
-      n->nchildren = 0;
-      n->children = NULL;
     }
     i++;
     parent = n;
@@ -477,54 +656,119 @@ tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
   n->status = MESH_PEER_SEARCHING;
 
   /* Add info about first hop into hashmap. */
-  if (me < p->length - 1)
+  if (-1 != me && me < p->length - 1)
   {
-    GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
-    hop = GNUNET_CONTAINER_multihashmap_get(t->first_hops, &id.hashPubKey);
-    if (NULL != hop)
-      GNUNET_free(hop);
-    hop = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
-    GNUNET_PEER_resolve (p->peers[me + 1], hop);
-    GNUNET_CONTAINER_multihashmap_put (t->first_hops, &id.hashPubKey,
-                                       hop,
-                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+#if MESH_TREE_DEBUG
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "MESH:   finding first hop (own pos %d/%u)\n",
+                me, p->length - 1);
+#endif
+    GNUNET_PEER_resolve (p->peers[me + 1], &id);
+    tree_update_first_hops(t,
+                           tree_find_peer(t->root, p->peers[p->length - 1]),
+                           &id);
   }
+#if MESH_TREE_DEBUG
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   New node added.\n");
+#endif
   return GNUNET_OK;
 }
 
 
 /**
- * Destroy the node and all children
- * 
- * @param n Parent node to be destroyed
+ * Notifies a tree that a connection it might be using is broken.
+ * Marks all peers down the paths as disconnected and notifies the client.
+ *
+ * @param t Tree to use.
+ * @param p1 Short id of one of the peers (order unimportant)
+ * @param p2 Short id of one of the peers (order unimportant)
+ * @param cb Function to call for every peer that is marked as disconnected.
+ *
+ * @return Short ID of the first disconnected peer in the tree.
  */
-void
-tree_node_destroy (struct MeshTunnelTreeNode *n)
+GNUNET_PEER_Id
+tree_notify_connection_broken (struct MeshTunnelTree *t,
+                               GNUNET_PEER_Id p1,
+                               GNUNET_PEER_Id p2,
+                               MeshNodeDisconnectCB cb)
 {
-  struct MeshTunnelTreeNode *parent;
-  unsigned int i;
+  struct MeshTunnelTreeNode *n;
+  struct MeshTunnelTreeNode *c;
 
-  if (n->nchildren != 0)
+  n = tree_find_peer(t->me, p1);
+  if (NULL == n)
+    return 0;
+  if (NULL != n->parent && n->parent->peer == p2)
+  {
+    tree_mark_peers_disconnected(t, n, cb);
+    GNUNET_CONTAINER_DLL_remove(n->parent->children_head,
+                                n->parent->children_tail,
+                                n);
+    GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
+                                t->disconnected_tail,
+                                n);
+    return p1;
+  }
+  for (c = n->children_head; NULL != c; c = c->next)
   {
-    for (i = 0; i < n->nchildren; i++)
+    if (c->peer == p2)
     {
-      tree_node_destroy(&n->children[i]);
+      tree_mark_peers_disconnected(t, c, cb);
+      GNUNET_CONTAINER_DLL_remove(n->children_head,
+                                  n->children_tail,
+                                  c);
+      GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
+                                  t->disconnected_tail,
+                                  c);
+      return p2;
     }
-    if (n->children != NULL)
-      GNUNET_free(n->children);
   }
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Destroying node %u.\n", n->peer);
-  if (NULL == (parent = n->parent))
-    return;
-  i = (n - parent->children) / sizeof(struct MeshTunnelTreeNode);
-  parent->children[i] = parent->children[parent->nchildren - 1];
-  parent->nchildren--;
-  parent->children = realloc(parent->children,
-                             parent->nchildren
-                             * sizeof(struct MeshTunnelTreeNode));
-
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Destroyed.\n");
+  return 0;
+}
+
+
+/**
+ * Deletes a peer from a tunnel, liberating all unused resources on the path to
+ * it. It shouldn't have children, if it has they will be destroyed as well.
+ * If the tree is not local and no longer has any paths, the root node will be
+ * destroyed and marked as NULL.
+ *
+ * @param t Tunnel tree to use.
+ * @param peer Short ID of the peer to remove from the tunnel tree.
+ * @param cb Callback to notify client of disconnected peers.
+ *
+ * @return GNUNET_OK or GNUNET_SYSERR
+ */
+int
+tree_del_peer (struct MeshTunnelTree *t,
+               GNUNET_PEER_Id peer,
+               MeshNodeDisconnectCB cb)
+{
+  struct MeshTunnelTreeNode *n;
+
+  n = tree_del_path(t, peer, cb);
+  if (NULL == n)
+    return GNUNET_SYSERR;
+  GNUNET_break_op (NULL == n->children_head);
+  tree_node_destroy(n);
+  if (NULL == t->root->children_head && t->me != t->root)
+  {
+    tree_node_destroy (t->root);
+    t->root = NULL;
+  }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Print the tree on stderr
+ *
+ * @param t The tree
+ */
+void
+tree_debug(struct MeshTunnelTree *t)
+{
+  tree_node_debug(t->root, 0);
 }
 
 
@@ -554,7 +798,7 @@ void
 tree_destroy (struct MeshTunnelTree *t)
 {
   tree_node_destroy(t->root);
-  GNUNET_free(t->root);
   GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
   GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);
-}
\ No newline at end of file
+  GNUNET_free(t);
+}