2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/mesh_tunnel_tree.c
23 * @brief Tunnel tree handling functions
24 * @author Bartlomiej Polot
28 #include "mesh_tunnel_tree.h"
32 debug_node(struct MeshTunnelTreeNode *n, uint16_t level)
34 struct MeshTunnelTreeNode *c;
37 for (i = 0; i < level; i++)
39 if (n->status == MESH_PEER_READY)
41 if (n->status == MESH_PEER_SEARCHING)
43 if (n->status == MESH_PEER_RELAY)
45 if (n->status == MESH_PEER_RECONNECTING)
48 fprintf(stderr, "%u [%p] ", n->peer, n);
49 if (NULL != n->parent)
50 fprintf(stderr, "(-> %u)\n", n->parent->peer);
52 fprintf(stderr, "(root)\n");
53 for (c = n->children_head; NULL != c; c = c->next)
54 debug_node(c, level + 1);
60 tree_debug(struct MeshTunnelTree *t)
62 debug_node(t->root, 0);
70 * @param p the path to invert
73 path_invert (struct MeshPeerPath *path)
78 for (i = 0; i < path->length / 2; i++)
81 path->peers[i] = path->peers[path->length - i - 1];
82 path->peers[path->length - i - 1] = aux;
88 * Destroy the path and free any allocated resources linked to it
90 * @param p the path to destroy
92 * @return GNUNET_OK on success
95 path_destroy (struct MeshPeerPath *p)
97 GNUNET_PEER_decrement_rcs (p->peers, p->length);
98 GNUNET_free (p->peers);
105 * Find the first peer whom to send a packet to go down this path
107 * @param t The tunnel tree to use
108 * @param peer The peerinfo of the peer we are trying to reach
110 * @return peerinfo of the peer who is the first hop in the tunnel
113 struct GNUNET_PeerIdentity *
114 path_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
116 struct GNUNET_PeerIdentity id;
118 GNUNET_PEER_resolve (peer, &id);
119 return GNUNET_CONTAINER_multihashmap_get (t->first_hops,
125 * Get the length of a path
127 * @param path The path to measure, with the local peer at any point of it
129 * @return Number of hops to reach destination
130 * UINT_MAX in case the peer is not in the path
133 path_get_length (struct MeshPeerPath *path)
142 * Get the cost of the path relative to the already built tunnel tree
144 * @param t The tunnel tree to which compare
145 * @param path The individual path to reach a peer
147 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
150 * TODO: remove dummy implementation, look into the tunnel tree
153 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
155 return path_get_length (path);
160 * Recursively find the given peer in the tree.
162 * @param t Tunnel where to look for the peer.
163 * @param peer Peer to find
165 * @return Pointer to the node of the peer. NULL if not found.
167 struct MeshTunnelTreeNode *
168 tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
170 struct MeshTunnelTreeNode *n;
171 struct MeshTunnelTreeNode *r;
173 if (parent->peer == peer_id)
175 for (n = parent->children_head; NULL != n; n = n->next)
177 r = tree_find_peer (n, peer_id);
186 * Recusively mark peer and children as disconnected, notify client
188 * @param tree Tree this node belongs to
189 * @param parent Node to be clean, potentially with children
190 * @param cb Callback to use to notify about disconnected peers.
193 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
194 struct MeshTunnelTreeNode *parent,
195 MeshNodeDisconnectCB cb)
197 struct GNUNET_PeerIdentity *pi;
198 struct GNUNET_PeerIdentity id;
199 struct MeshTunnelTreeNode *n;
201 for (n = parent->children_head; NULL != n; n = n->next)
203 tree_mark_peers_disconnected (tree, n, cb);
205 if (MESH_PEER_READY == parent->status)
209 parent->status = MESH_PEER_RECONNECTING;
211 /* Remove and free info about first hop */
212 GNUNET_PEER_resolve(parent->peer, &id);
213 pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
214 GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
221 * Recusively update the info about what is the first hop to reach the node
223 * @param tree Tree this nodes belongs to
224 * @param parent Node to be start updating
225 * @param hop If known, ID of the first hop.
226 * If not known, NULL to find out and pass on children.
229 tree_update_first_hops (struct MeshTunnelTree *tree,
230 struct MeshTunnelTreeNode *parent,
231 struct GNUNET_PeerIdentity *hop)
233 struct GNUNET_PeerIdentity pi;
234 struct GNUNET_PeerIdentity *copy;
235 struct GNUNET_PeerIdentity id;
236 struct MeshTunnelTreeNode *n;
238 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
239 "tree: Finding first hop for %u.\n",
243 struct MeshTunnelTreeNode *aux;
244 struct MeshTunnelTreeNode *old;
248 while (aux != tree->me)
250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
251 "tree: ... its not %u.\n",
255 GNUNET_assert(NULL != aux);
257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
261 GNUNET_PEER_resolve(old->peer, hop);
263 copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
265 GNUNET_PEER_resolve(parent->peer, &id);
266 GNUNET_CONTAINER_multihashmap_put(tree->first_hops, &id.hashPubKey, copy,
267 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
269 for (n = parent->children_head; NULL != n; n = n->next)
271 tree_update_first_hops (tree, n, hop);
277 * Delete the current path to the peer, including all now unused relays.
278 * The destination peer is NOT destroyed, it is returned in order to either set
279 * a new path to it or destroy it explicitly, taking care of it's child nodes.
281 * @param t Tunnel tree where to delete the path from.
282 * @param peer Destination peer whose path we want to remove.
283 * @param cb Callback to use to notify about disconnected peers.
285 * @return pointer to the pathless node.
286 * NULL when not found
288 struct MeshTunnelTreeNode *
289 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
290 MeshNodeDisconnectCB cb)
292 struct MeshTunnelTreeNode *parent;
293 struct MeshTunnelTreeNode *node;
294 struct MeshTunnelTreeNode *n;
296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting path to %u.\n", peer_id);
297 if (peer_id == t->root->peer)
299 n = tree_find_peer (t->me, peer_id);
305 GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
308 while (t->root != parent && MESH_PEER_RELAY == parent->status &&
309 NULL == parent->children_head)
311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
312 "tree: Deleting node %u.\n",
315 tree_node_destroy(parent);
318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
319 "tree: Not deleted peer %u.\n",
322 tree_mark_peers_disconnected (t, node, cb);
329 * Return a newly allocated individual path to reach a peer from the local peer,
330 * according to the path tree of some tunnel.
332 * @param t Tunnel from which to read the path tree
333 * @param peer_info Destination peer to whom we want a path
335 * @return A newly allocated individual path to reach the destination peer.
336 * Path must be destroyed afterwards.
338 struct MeshPeerPath *
339 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
341 struct MeshTunnelTreeNode *n;
342 struct MeshPeerPath *p;
343 GNUNET_PEER_Id myid = t->me->peer;
345 n = tree_find_peer(t->me, peer);
346 p = GNUNET_malloc(sizeof(struct MeshPeerPath));
348 /* Building the path (inverted!) */
349 while (n->peer != myid)
351 GNUNET_array_append(p->peers, p->length, n->peer);
352 GNUNET_PEER_change_rc(n->peer, 1);
354 GNUNET_assert(NULL != n);
356 GNUNET_array_append(p->peers, p->length, myid);
357 GNUNET_PEER_change_rc(myid, 1);
366 * Integrate a stand alone path into the tunnel tree.
368 * @param t Tunnel where to add the new path.
369 * @param p Path to be integrated.
370 * @param cb Callback to use to notify about peers temporarily disconnecting
372 * @return GNUNET_OK in case of success.
373 * GNUNET_SYSERR in case of error.
376 * - go backwards on path looking for each peer in the present tree
377 * - do not disconnect peers until new path is created & connected
380 tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
381 MeshNodeDisconnectCB cb)
383 struct MeshTunnelTreeNode *parent;
384 struct MeshTunnelTreeNode *oldnode;
385 struct MeshTunnelTreeNode *n;
386 struct MeshTunnelTreeNode *c;
387 struct GNUNET_PeerIdentity id;
388 struct GNUNET_PeerIdentity *hop;
389 GNUNET_PEER_Id myid = t->me->peer;
393 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
394 "tree: Adding path [%u] towards peer %u to peer %u.\n",
396 p->peers[p->length - 1],
398 GNUNET_assert(0 != p->length);
399 parent = n = t->root;
400 if (n->peer != p->peers[0])
403 return GNUNET_SYSERR;
407 oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
408 /* Look for the first node that is not already present in the tree
410 * Assuming that the tree is somewhat balanced, O(log n * log N).
411 * - Length of the path is expected to be log N (size of whole network).
412 * - Each level of the tree is expected to have log n children (size of tree).
414 me = t->root->peer == myid ? 0 : -1;
415 for (i = 1; i < p->length; i++)
417 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
418 "tree: Looking for peer %u.\n",
421 if (p->peers[i] == myid)
423 for (c = n->children_head; NULL != c; c = c->next)
425 if (c->peer == p->peers[i])
427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
428 "tree: Found in children of %u.\n",
434 /* If we couldn't find a child equal to path[i], we have reached the end
435 * of the common path. */
439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
440 "tree: All childen visited.\n");
443 /* New path deviates from tree before reaching us. What happened? */
445 return GNUNET_SYSERR;
447 /* Add the rest of the path as a branch from parent. */
448 while (i < p->length)
450 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
451 "tree: Adding peer %u, to %u.\n",
455 if (i == p->length - 1 && NULL != oldnode)
457 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Putting old node into place.\n");
458 oldnode->parent = parent;
459 GNUNET_CONTAINER_DLL_insert(parent->children_head,
460 parent->children_tail,
462 tree_update_first_hops (t, oldnode, NULL);
467 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
468 n = tree_node_new(parent, p->peers[i]);
470 n->status = MESH_PEER_RELAY;
475 n->status = MESH_PEER_SEARCHING;
477 /* Add info about first hop into hashmap. */
478 if (me < p->length - 1)
480 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
481 hop = GNUNET_CONTAINER_multihashmap_get(t->first_hops, &id.hashPubKey);
484 GNUNET_CONTAINER_multihashmap_remove_all(t->first_hops, &id.hashPubKey);
487 hop = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
488 GNUNET_PEER_resolve (p->peers[me + 1], hop);
489 GNUNET_CONTAINER_multihashmap_put (t->first_hops, &id.hashPubKey,
491 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
493 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
499 * Allocates and initializes a new node.
500 * Sets ID and parent of the new node and inserts it in the DLL of the parent
502 * @param parent Node that will be the parent from the new node, NULL for root
503 * @param id Short Id of the new node
505 * @return Newly allocated node
507 struct MeshTunnelTreeNode *
508 tree_node_new(struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id id)
510 struct MeshTunnelTreeNode *node;
512 node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
514 GNUNET_PEER_change_rc(id, 1);
515 node->parent = parent;
517 GNUNET_CONTAINER_DLL_insert(parent->children_head,
518 parent->children_tail,
526 * Destroys and frees the node and all children
528 * @param n Parent node to be destroyed
531 tree_node_destroy (struct MeshTunnelTreeNode *parent)
533 struct MeshTunnelTreeNode *n;
534 struct MeshTunnelTreeNode *next;
536 n = parent->children_head;
540 tree_node_destroy(n);
543 GNUNET_PEER_change_rc(parent->peer, -1);
544 if (NULL != parent->parent)
545 GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
546 parent->parent->children_tail,
553 * Iterator over hash map peer entries and frees all data in it.
554 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
557 * @param key current key code (will no longer contain valid data!!)
558 * @param value value in the hash map (treated as void *)
559 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
562 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
570 * Destroy the whole tree and free all used memory and Peer_Ids
572 * @param t Tree to be destroyed
575 tree_destroy (struct MeshTunnelTree *t)
577 tree_node_destroy(t->root);
578 GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
579 GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);