+/**
+ * Recursively find the given peer.
+ *
+ * @param parent Node where to start looking.
+ * @param peer_id Short ID of the peer to find.
+ *
+ * @return Pointer to the node of the peer. NULL if not found.
+ */
+static struct MeshTunnelTreeNode *
+tree_node_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *r;
+
+ if (parent->peer == peer_id)
+ return parent;
+ for (n = parent->children_head; NULL != n; n = n->next)
+ {
+ r = tree_node_find_peer (n, peer_id);
+ if (NULL != r)
+ return r;
+ }
+ return NULL;
+}
+
+
+/**
+ * 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);
+ }
+}
+
+