Allowed to destroy NULL paths
[oweals/gnunet.git] / src / mesh / mesh_tunnel_tree.c
index cea02867011dc19461ee08260107f1f18dab0d3b..a80cbb0c07d5763d11cbee32a7ad1194352b3a12 100644 (file)
@@ -27,6 +27,8 @@
 #include "mesh.h"
 #include "mesh_tunnel_tree.h"
 
+#define MESH_TREE_DEBUG GNUNET_YES
+
 
 /**
  * Create a new path
@@ -102,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;
 }
 
 
@@ -154,6 +159,8 @@ path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
 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);
@@ -260,6 +267,7 @@ tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
   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;
 
@@ -313,7 +321,7 @@ tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
   {
     tree_mark_peers_disconnected (tree, n, cb);
   }
-  if (MESH_PEER_READY == parent->status)
+  if (MESH_PEER_READY == parent->status && NULL != cb)
   {
     cb (parent);
   }
@@ -336,7 +344,7 @@ tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
  * @param hop If known, ID of the first hop.
  *            If not known, NULL to find out and pass on children.
  */
-static void
+void
 tree_update_first_hops (struct MeshTunnelTree *tree,
                         struct MeshTunnelTreeNode *parent,
                         struct GNUNET_PeerIdentity *hop)
@@ -346,36 +354,47 @@ tree_update_first_hops (struct MeshTunnelTree *tree,
   struct GNUNET_PeerIdentity id;
   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 = π
     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 (n = parent->children_head; NULL != n; n = n->next)
   {
@@ -404,8 +423,13 @@ tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
   struct MeshTunnelTreeNode *node;
   struct MeshTunnelTreeNode *n;
 
+#if MESH_TREE_DEBUG
+  struct GNUNET_PeerIdentity id;
+  GNUNET_PEER_resolve(peer_id, &id);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Deleting path to %u.\n", peer_id);
+          "tree:   Deleting path to %s.\n",
+          GNUNET_i2s (&id));
+#endif
   if (peer_id == t->root->peer)
     return NULL;
 
@@ -420,7 +444,7 @@ tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
       return n;
     }
   }
-  n = tree_find_peer (t->me, peer_id);
+  n = tree_find_peer (t->root, peer_id);
   if (NULL == n)
     return NULL;
   node = n;
@@ -429,19 +453,24 @@ tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
   GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
   n->parent = NULL;
 
-  while (t->root != parent && MESH_PEER_RELAY == parent->status &&
-         NULL == parent->children_head)
+  while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
   {
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(parent->peer, &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-               "tree:   Deleting node %u.\n",
-               parent->peer);
+              "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 %u.\n",
-             parent->peer);
+             "tree:   Not deleted peer %s.\n",
+             GNUNET_i2s (&id));
+#endif
 
   tree_mark_peers_disconnected (t, node, cb);
 
@@ -486,6 +515,7 @@ 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
@@ -516,18 +546,23 @@ tree_add_path (struct MeshTunnelTree *t,
   struct MeshTunnelTreeNode *n;
   struct MeshTunnelTreeNode *c;
   struct GNUNET_PeerIdentity id;
-  struct GNUNET_PeerIdentity *hop;
   GNUNET_PEER_Id myid;
   int me;
+//   int oldnode_is_me;
   unsigned int i;
 
+#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
 
-  myid = t->me->peer;
+  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])
@@ -547,9 +582,12 @@ tree_add_path (struct MeshTunnelTree *t,
   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;
@@ -557,9 +595,12 @@ tree_add_path (struct MeshTunnelTree *t,
     {
       if (c->peer == p->peers[i])
       {
+#if MESH_TREE_DEBUG
+        GNUNET_PEER_resolve(parent->peer, &id);
         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Found in children of %u.\n",
-             parent->peer);
+                   "tree:   Found in children of %s.\n",
+                   GNUNET_i2s (&id));
+#endif
         n = c;
         break;
       }
@@ -569,25 +610,30 @@ tree_add_path (struct MeshTunnelTree *t,
     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);
+               "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");
+#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,
@@ -597,7 +643,9 @@ tree_add_path (struct MeshTunnelTree *t,
     }
     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;
@@ -608,22 +656,21 @@ tree_add_path (struct MeshTunnelTree *t,
   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_CONTAINER_multihashmap_remove_all(t->first_hops, &id.hashPubKey);
-      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;
 }
 
@@ -681,7 +728,10 @@ tree_notify_connection_broken (struct MeshTunnelTree *t,
 
 
 /**
- * Deletes a peer from a tunnel, marking its children as disconnected.
+ * 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.
@@ -695,24 +745,17 @@ tree_del_peer (struct MeshTunnelTree *t,
                MeshNodeDisconnectCB cb)
 {
   struct MeshTunnelTreeNode *n;
-  struct MeshTunnelTreeNode *c;
-  struct MeshTunnelTreeNode *aux;
 
   n = tree_del_path(t, peer, cb);
-  c = n->children_head;
-  while (NULL != c)
+  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)
   {
-    aux = c->next;
-    GNUNET_CONTAINER_DLL_remove(n->children_head,
-                                n->children_tail,
-                                c);
-    GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
-                                t->disconnected_tail,
-                                c);
-    cb (c);
-    c = aux;
+    tree_node_destroy (t->root);
+    t->root = NULL;
   }
-  tree_node_destroy(n);
   return GNUNET_OK;
 }