fixing 2012: network structure alignment now forced to be correct even on W32 using...
[oweals/gnunet.git] / src / mesh / mesh_tunnel_tree.c
index e39558c0b829b8b3fb08c8038650c49a9cad0771..a7577e36f8b14b810aed41975cf8b7a470234594 100644 (file)
@@ -110,7 +110,7 @@ struct MeshTunnelTree
 /**
  * Create a new path
  *
- * @param lenght How many hops will the path have.
+ * @param length How many hops will the path have.
  *
  * @return A newly allocated path with a peer array of the specified length.
  */
@@ -132,7 +132,7 @@ path_new (unsigned int length)
 /**
  * Invert the path
  *
- * @param p the path to invert
+ * @param path the path to invert
  */
 void
 path_invert (struct MeshPeerPath *path)
@@ -152,7 +152,7 @@ path_invert (struct MeshPeerPath *path)
 /**
  * Duplicate a path, incrementing short peer's rc.
  *
- * @param p The path to duplicate.
+ * @param path The path to duplicate.
  */
 struct MeshPeerPath *
 path_duplicate (struct MeshPeerPath *path)
@@ -172,7 +172,7 @@ path_duplicate (struct MeshPeerPath *path)
  * Recusively update the info about what is the first hop to reach the node
  *
  * @param tree Tree this nodes belongs to.
- * @param parent_id Short ID from node form which to start updating.
+ * @param parent The node form which to start updating.
  * @param hop If known, ID of the first hop.
  *            If not known, NULL to find out and pass on children.
  */
@@ -183,12 +183,12 @@ tree_node_update_first_hops (struct MeshTunnelTree *tree,
 
 
 /**
- * Get the length of a path
+ * Get the length of a path.
  *
- * @param path The path to measure, with the local peer at any point of it
+ * @param path The path to measure, with the local peer at any point of it.
  *
- * @return Number of hops to reach destination
- *         UINT_MAX in case the peer is not in the path
+ * @return Number of hops to reach destination.
+ *         UINT_MAX in case the peer is not in the path.
  */
 unsigned int
 path_get_length (struct MeshPeerPath *path)
@@ -199,24 +199,6 @@ path_get_length (struct MeshPeerPath *path)
 }
 
 
-/**
- * Get the cost of the path relative to the already built tunnel tree
- *
- * @param t The tunnel tree to which compare
- * @param path The individual path to reach a peer
- *
- * @return Number of hops to reach destination, UINT_MAX in case the peer is not
- * in the path
- *
- * TODO: remove dummy implementation, look into the tunnel tree
- */
-unsigned int
-path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
-{
-  return path_get_length (path);
-}
-
-
 /**
  * Destroy the path and free any allocated resources linked to it
  *
@@ -267,7 +249,7 @@ tree_node_new (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
  * Recursively find the given peer.
  *
  * @param parent Node where to start looking.
- * @param peer Peer to find.
+ * @param peer_id Short ID of the peer to find.
  *
  * @return Pointer to the node of the peer. NULL if not found.
  */
@@ -293,7 +275,7 @@ tree_node_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
  * Recusively update the info about what is the first hop to reach the node
  *
  * @param tree Tree this nodes belongs to.
- * @param parent_id Short ID from node form which to start updating.
+ * @param parent ID from node form which to start updating.
  * @param hop If known, ID of the first hop.
  *            If not known, NULL to find out and pass on children.
  */
@@ -389,7 +371,7 @@ tree_node_debug (struct MeshTunnelTreeNode *n, uint16_t level)
 /**
  * Destroys and frees the node and all children
  *
- * @param n Parent node to be destroyed
+ * @param parent Parent node to be destroyed
  */
 static void
 tree_node_destroy (struct MeshTunnelTreeNode *parent)
@@ -422,12 +404,11 @@ tree_node_destroy (struct MeshTunnelTreeNode *parent)
 
 
 /**
- * Create a new tunnel tree associated to a tunnel
+ * Create a new tree.
  *
- * @param t Tunnel this tree will represent
- * @param peer A short peer id of the root of the tree
+ * @param peer A short peer id of the root of the tree.
  *
- * @return A newly allocated and initialized tunnel tree
+ * @return A newly allocated and initialized tunnel tree.
  */
 struct MeshTunnelTree *
 tree_new (GNUNET_PEER_Id peer)
@@ -439,6 +420,11 @@ tree_new (GNUNET_PEER_Id peer)
   tree->root = tree_node_new (NULL, peer);
   tree->root->status = MESH_PEER_ROOT;
 
+  if (1 == peer)
+  {
+    tree->me = tree->root;
+  }
+
   return tree;
 }
 
@@ -447,7 +433,8 @@ tree_new (GNUNET_PEER_Id peer)
  * Set the status of a node.
  *
  * @param tree Tree.
- * @param peer A short peer id of local peer.
+ * @param peer A short peer id of the node.
+ * @param status New status to set.
  */
 void
 tree_set_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer,
@@ -465,9 +452,10 @@ tree_set_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer,
 /**
  * Get the status of a node.
  *
- * @param tree Tree whose local id we want to now.
+ * @param tree Tree whose node's status we want to now.
+ * @param peer A short peer id of the node.
  *
- * @return Short peer id of local peer.
+ * @return Status of the peer.
  */
 enum MeshPeerState
 tree_get_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer)
@@ -542,7 +530,7 @@ tree_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
  * Find the given peer in the tree.
  *
  * @param tree Tree where to look for the peer.
- * @param peer Peer to find.
+ * @param peer_id Short ID of the peer to find.
  *
  * @return Pointer to the node of the peer. NULL if not found.
  */
@@ -559,6 +547,7 @@ tree_find_peer (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer_id)
  * @param tree Tree this node belongs to
  * @param parent Node to be clean, potentially with children
  * @param cb Callback to use to notify about disconnected peers.
+ * @param cbcls Closure for cb.
  */
 static void
 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
@@ -636,7 +625,7 @@ tree_update_first_hops (struct MeshTunnelTree *tree, GNUNET_PEER_Id parent_id,
  * a new path to it or destroy it explicitly, taking care of it's child nodes.
  *
  * @param t Tunnel tree where to delete the path from.
- * @param peer Destination peer whose path we want to remove.
+ * @param peer_id Short ID of the destination peer whose path we want to remove.
  * @param cb Callback to use to notify about disconnected peers.
  * @param cbcls Closure for cb.
  *
@@ -707,8 +696,8 @@ tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
  * Return a newly allocated individual path to reach a peer from the local peer,
  * according to the path tree of some tunnel.
  *
- * @param t Tunnel from which to read the path tree
- * @param peer_info Destination peer to whom we want a path
+ * @param t Tunnel from which to read the path tree.
+ * @param peer Short ID of the destination peer to whom we want a path.
  *
  * @return A newly allocated individual path to reach the destination peer.
  *         Path must be destroyed afterwards.
@@ -721,7 +710,10 @@ tree_get_path_to_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
 
   n = tree_find_peer (t, peer);
   if (NULL == n)
+  {
+    GNUNET_break (0);
     return NULL;
+  }
   p = path_new (0);
 
   /* Building the path (inverted!) */
@@ -732,6 +724,7 @@ tree_get_path_to_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
     n = n->parent;
     if (NULL == n)
     {
+      GNUNET_break (0);
       path_destroy (p);
       return NULL;
     }
@@ -900,6 +893,8 @@ tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree:   New node added.\n");
 #endif
+  if (NULL == t->me)
+    t->me = tree_find_peer (t, 1);
   return GNUNET_OK;
 }
 
@@ -987,6 +982,52 @@ tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer,
 }
 
 
+
+/**
+ * Get the cost of the path relative to the already built tunnel tree.
+ *
+ * @param t The tunnel tree to which compare.
+ * @param path The individual path to reach a peer. It has to start at the
+ *             root of the tree to be comparable.
+ *
+ * @return Number of hops to reach destination, UINT_MAX in case the peer is not
+ *         in the path.
+ *
+ * TODO: adapt to allow any start / root combination
+ * TODO: take in account state of the nodes
+ */
+unsigned int
+tree_get_path_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
+{
+  struct MeshTunnelTreeNode *n;
+  struct MeshTunnelTreeNode *p;
+  unsigned int i;
+  unsigned int l;
+
+  l = path_get_length (path);
+  p = t->root;
+  if (t->root->peer != path->peers[0])
+  {
+    GNUNET_break (0);
+    return UINT_MAX;
+  }
+  for (i = 1; i < l; i++)
+  {
+    for (n = p->children_head; NULL != n; n = n->next)
+    {
+      if (path->peers[i] == n->peer)
+      {
+        break;
+      }
+    }
+    if (NULL == n)
+      return l - i;
+    p = n;
+  }
+  return l - i;
+}
+
+
 /**
  * Print the tree on stderr
  *