+/**
+ * Recusively update the info about what is the first hop to reach the node
+ *
+ * @param tree Tree this nodes belongs to.
+ * @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.
+ */
+static void
+tree_node_update_first_hops (struct MeshTunnelTree *tree,
+ struct MeshTunnelTreeNode *parent,
+ struct GNUNET_PeerIdentity *hop)
+{
+ struct GNUNET_PeerIdentity pi;
+ struct GNUNET_PeerIdentity *copy;
+ 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 %s.\n",
+ GNUNET_i2s (&id));
+#endif
+ if (NULL == hop)
+ {
+ struct MeshTunnelTreeNode *aux;
+ struct MeshTunnelTreeNode *old;
+
+ aux = old = parent;
+ while (aux != tree->me)
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (aux->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: ... checking %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 %s!\n",
+ GNUNET_i2s (&id));
+#endif
+ hop = π
+ GNUNET_PEER_resolve (old->peer, hop);
+ }
+ GNUNET_PEER_resolve (parent->peer, &id);
+ copy = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
+ if (NULL == copy)
+ copy = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
+ *copy = *hop;
+
+ (void) GNUNET_CONTAINER_multihashmap_put (tree->first_hops, &id.hashPubKey,
+ copy,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+
+ for (n = parent->children_head; NULL != n; n = n->next)
+ {
+ tree_node_update_first_hops (tree, n, hop);
+ }
+}
+
+
+static void
+tree_node_debug (struct MeshTunnelTreeNode *n, uint16_t level)
+{
+ struct MeshTunnelTreeNode *c;
+ struct GNUNET_PeerIdentity id;;
+ 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, "*");
+
+ GNUNET_PEER_resolve (n->peer, &id);
+ fprintf (stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
+ if (NULL != n->parent)
+ {
+ GNUNET_PEER_resolve (n->parent->peer, &id);
+ fprintf (stderr, "(-> %s [%u])\n", GNUNET_i2s (&id), 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 parent Parent node to be destroyed
+ */
+static void
+tree_node_destroy (struct MeshTunnelTreeNode *parent)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *next;
+
+#if MESH_TREE_DEBUG
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying node %u\n",
+ parent->peer);
+ GNUNET_PEER_resolve (parent->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: (%s)\n", GNUNET_i2s (&id));
+#endif
+ 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 tree.
+ *
+ * @param peer A short peer id of the root of the tree.
+ *
+ * @return A newly allocated and initialized tunnel tree.
+ */
+struct MeshTunnelTree *
+tree_new (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;
+
+ if (1 == peer)
+ {
+ tree->me = tree->root;
+ }
+
+ return tree;
+}
+
+
+/**
+ * Set the status of a node.
+ *
+ * @param tree Tree.
+ * @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,
+ enum MeshPeerState status)
+{
+ struct MeshTunnelTreeNode *n;
+
+ n = tree_find_peer (tree, peer);
+ if (NULL == n)
+ return;
+ n->status = status;
+}
+
+
+/**
+ * Get the status of a node.
+ *
+ * @param tree Tree whose node's status we want to now.
+ * @param peer A short peer id of the node.
+ *
+ * @return Status of the peer.
+ */
+enum MeshPeerState
+tree_get_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer)
+{
+ struct MeshTunnelTreeNode *n;
+
+ n = tree_find_peer (tree, peer);
+ if (NULL == n)
+ return MESH_PEER_INVALID;
+ return n->status;
+}
+
+
+/**
+ * Get the id of the predecessor of the local node.
+ *
+ * @param tree Tree whose local id we want to now.
+ *
+ * @return Short peer id of local peer.
+ */
+GNUNET_PEER_Id
+tree_get_predecessor (struct MeshTunnelTree *tree)
+{
+ if (NULL != tree->me && NULL != tree->me->parent)
+ return tree->me->parent->peer;
+ else
+ return (GNUNET_PEER_Id) 0;
+}
+
+
+/**
+ * Find the first peer whom to send a packet to go down this path
+ *
+ * @param t The tunnel tree to use
+ * @param peer The peerinfo of the peer we are trying to reach
+ *
+ * @return peerinfo of the peer who is the first hop in the tunnel
+ * NULL on error
+ */
+struct GNUNET_PeerIdentity *
+tree_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
+{
+ struct GNUNET_PeerIdentity id;
+ struct GNUNET_PeerIdentity *r;
+
+ GNUNET_PEER_resolve (peer, &id);
+ r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
+ if (NULL == r)
+ {
+ struct MeshTunnelTreeNode *n;
+
+ n = tree_find_peer (t, peer);
+ if (NULL != t->me && NULL != n)
+ {
+ tree_node_update_first_hops (t, n, NULL);
+ r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
+ GNUNET_assert (NULL != r);
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Tree structure inconsistent! me: %p, n: %p", t->me, n);
+ GNUNET_break (0);
+ }
+ }
+
+ return r;
+}
+
+
+/**
+ * Find the given peer in the tree.
+ *
+ * @param tree Tree where to look for the peer.
+ * @param peer_id Short ID of the peer to find.
+ *
+ * @return Pointer to the node of the peer. NULL if not found.
+ */
+struct MeshTunnelTreeNode *
+tree_find_peer (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer_id)
+{
+ return tree_node_find_peer (tree->root, peer_id);
+}
+
+