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"
34 * @param lenght How many hops will the path have.
36 * @return A newly allocated path with a peer array of the specified length.
39 path_new (unsigned int length)
41 struct MeshPeerPath *p;
43 p = GNUNET_malloc (sizeof(struct MeshPeerPath));
47 p->peers = GNUNET_malloc (length * sizeof(GNUNET_PEER_Id));
56 * @param p the path to invert
59 path_invert (struct MeshPeerPath *path)
64 for (i = 0; i < path->length / 2; i++)
67 path->peers[i] = path->peers[path->length - i - 1];
68 path->peers[path->length - i - 1] = aux;
74 * Duplicate a path, incrementing short peer's rc.
76 * @param p The path to duplicate.
79 path_duplicate (struct MeshPeerPath *path)
81 struct MeshPeerPath *aux;
84 aux = path_new(path->length);
85 memcpy (aux->peers, path->peers, path->length * sizeof(GNUNET_PEER_Id));
86 for (i = 0; i < path->length; i++)
87 GNUNET_PEER_change_rc(path->peers[i], 1);
93 * Find the first peer whom to send a packet to go down this path
95 * @param t The tunnel tree to use
96 * @param peer The peerinfo of the peer we are trying to reach
98 * @return peerinfo of the peer who is the first hop in the tunnel
101 struct GNUNET_PeerIdentity *
102 path_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
104 struct GNUNET_PeerIdentity id;
105 struct GNUNET_PeerIdentity *r;
107 GNUNET_PEER_resolve (peer, &id);
108 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
109 GNUNET_break (NULL != r);
116 * Get the length of a path
118 * @param path The path to measure, with the local peer at any point of it
120 * @return Number of hops to reach destination
121 * UINT_MAX in case the peer is not in the path
124 path_get_length (struct MeshPeerPath *path)
133 * Get the cost of the path relative to the already built tunnel tree
135 * @param t The tunnel tree to which compare
136 * @param path The individual path to reach a peer
138 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
141 * TODO: remove dummy implementation, look into the tunnel tree
144 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
146 return path_get_length (path);
151 * Destroy the path and free any allocated resources linked to it
153 * @param p the path to destroy
155 * @return GNUNET_OK on success
158 path_destroy (struct MeshPeerPath *p)
160 GNUNET_PEER_decrement_rcs (p->peers, p->length);
161 GNUNET_free (p->peers);
169 * Allocates and initializes a new node.
170 * Sets ID and parent of the new node and inserts it in the DLL of the parent
172 * @param parent Node that will be the parent from the new node, NULL for root
173 * @param peer Short Id of the new node
175 * @return Newly allocated node
177 static struct MeshTunnelTreeNode *
178 tree_node_new(struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
180 struct MeshTunnelTreeNode *node;
182 node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
184 GNUNET_PEER_change_rc(peer, 1);
185 node->parent = parent;
187 GNUNET_CONTAINER_DLL_insert(parent->children_head,
188 parent->children_tail,
196 tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
198 struct MeshTunnelTreeNode *c;
201 for (i = 0; i < level; i++)
202 fprintf(stderr, " ");
203 if (n->status == MESH_PEER_READY)
204 fprintf(stderr, "#");
205 if (n->status == MESH_PEER_SEARCHING)
206 fprintf(stderr, "+");
207 if (n->status == MESH_PEER_RELAY)
208 fprintf(stderr, "-");
209 if (n->status == MESH_PEER_RECONNECTING)
210 fprintf(stderr, "*");
212 fprintf(stderr, "%u [%p] ", n->peer, n);
213 if (NULL != n->parent)
214 fprintf(stderr, "(-> %u)\n", n->parent->peer);
216 fprintf(stderr, "(root)\n");
217 for (c = n->children_head; NULL != c; c = c->next)
218 tree_node_debug(c, level + 1);
223 * Destroys and frees the node and all children
225 * @param n Parent node to be destroyed
228 tree_node_destroy (struct MeshTunnelTreeNode *parent)
230 struct MeshTunnelTreeNode *n;
231 struct MeshTunnelTreeNode *next;
233 n = parent->children_head;
237 tree_node_destroy(n);
240 GNUNET_PEER_change_rc(parent->peer, -1);
241 if (NULL != parent->parent)
242 GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
243 parent->parent->children_tail,
251 * Create a new tunnel tree associated to a tunnel
253 * @param t Tunnel this tree will represent
254 * @param peer A short peer id of the root of the tree
256 * @return A newly allocated and initialized tunnel tree
258 struct MeshTunnelTree *
259 tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
261 struct MeshTunnelTree *tree;
263 tree = GNUNET_malloc(sizeof (struct MeshTunnelTree));
264 tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
265 tree->root = tree_node_new(NULL, peer);
266 tree->root->status = MESH_PEER_ROOT;
275 * Recursively find the given peer in the tree.
277 * @param t Tunnel where to look for the peer.
278 * @param peer Peer to find
280 * @return Pointer to the node of the peer. NULL if not found.
282 struct MeshTunnelTreeNode *
283 tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
285 struct MeshTunnelTreeNode *n;
286 struct MeshTunnelTreeNode *r;
288 if (parent->peer == peer_id)
290 for (n = parent->children_head; NULL != n; n = n->next)
292 r = tree_find_peer (n, peer_id);
301 * Recusively mark peer and children as disconnected, notify client
303 * @param tree Tree this node belongs to
304 * @param parent Node to be clean, potentially with children
305 * @param cb Callback to use to notify about disconnected peers.
308 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
309 struct MeshTunnelTreeNode *parent,
310 MeshNodeDisconnectCB cb)
312 struct GNUNET_PeerIdentity *pi;
313 struct GNUNET_PeerIdentity id;
314 struct MeshTunnelTreeNode *n;
316 for (n = parent->children_head; NULL != n; n = n->next)
318 tree_mark_peers_disconnected (tree, n, cb);
320 if (MESH_PEER_READY == parent->status && NULL != cb)
324 parent->status = MESH_PEER_RECONNECTING;
326 /* Remove and free info about first hop */
327 GNUNET_PEER_resolve(parent->peer, &id);
328 pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
329 GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
336 * Recusively update the info about what is the first hop to reach the node
338 * @param tree Tree this nodes belongs to
339 * @param parent Node to be start updating
340 * @param hop If known, ID of the first hop.
341 * If not known, NULL to find out and pass on children.
344 tree_update_first_hops (struct MeshTunnelTree *tree,
345 struct MeshTunnelTreeNode *parent,
346 struct GNUNET_PeerIdentity *hop)
348 struct GNUNET_PeerIdentity pi;
349 struct GNUNET_PeerIdentity *copy;
350 struct GNUNET_PeerIdentity id;
351 struct MeshTunnelTreeNode *n;
353 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
354 "tree: Finding first hop for %u.\n",
358 struct MeshTunnelTreeNode *aux;
359 struct MeshTunnelTreeNode *old;
363 while (aux != tree->me)
365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
366 "tree: ... its not %u.\n",
370 GNUNET_assert(NULL != aux);
372 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
376 GNUNET_PEER_resolve(old->peer, hop);
378 copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
380 GNUNET_PEER_resolve(parent->peer, &id);
381 GNUNET_CONTAINER_multihashmap_put(tree->first_hops, &id.hashPubKey, copy,
382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
384 for (n = parent->children_head; NULL != n; n = n->next)
386 tree_update_first_hops (tree, n, hop);
392 * Delete the current path to the peer, including all now unused relays.
393 * The destination peer is NOT destroyed, it is returned in order to either set
394 * a new path to it or destroy it explicitly, taking care of it's child nodes.
396 * @param t Tunnel tree where to delete the path from.
397 * @param peer Destination peer whose path we want to remove.
398 * @param cb Callback to use to notify about disconnected peers.
400 * @return pointer to the pathless node.
401 * NULL when not found
403 struct MeshTunnelTreeNode *
404 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
405 MeshNodeDisconnectCB cb)
407 struct MeshTunnelTreeNode *parent;
408 struct MeshTunnelTreeNode *node;
409 struct MeshTunnelTreeNode *n;
411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
412 "tree: Deleting path to %u.\n", peer_id);
413 if (peer_id == t->root->peer)
416 for (n = t->disconnected_head; NULL != n; n = n->next)
418 if (n->peer == peer_id)
420 /* Was already pathless, waiting for reconnection */
421 GNUNET_CONTAINER_DLL_remove (t->disconnected_head,
422 t->disconnected_tail,
427 n = tree_find_peer (t->root, peer_id);
433 GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
436 while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
439 "tree: Deleting node %u.\n",
442 tree_node_destroy(parent);
445 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
446 "tree: Not deleted peer %u.\n",
449 tree_mark_peers_disconnected (t, node, cb);
456 * Return a newly allocated individual path to reach a peer from the local peer,
457 * according to the path tree of some tunnel.
459 * @param t Tunnel from which to read the path tree
460 * @param peer_info Destination peer to whom we want a path
462 * @return A newly allocated individual path to reach the destination peer.
463 * Path must be destroyed afterwards.
465 struct MeshPeerPath *
466 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
468 struct MeshTunnelTreeNode *n;
469 struct MeshPeerPath *p;
470 GNUNET_PEER_Id myid = t->me->peer;
472 n = tree_find_peer(t->me, peer);
475 /* Building the path (inverted!) */
476 while (n->peer != myid)
478 GNUNET_array_append(p->peers, p->length, n->peer);
479 GNUNET_PEER_change_rc(n->peer, 1);
481 GNUNET_assert(NULL != n);
483 GNUNET_array_append(p->peers, p->length, myid);
484 GNUNET_PEER_change_rc(myid, 1);
494 * Integrate a stand alone path into the tunnel tree.
495 * If the peer toward which the new path is already in the tree, the peer
496 * and its children will be maked as disconnected and the callback
497 * will be called on each one of them. They will be maked as online only after
498 * receiving a PATH ACK for the new path for each one of them, so the caller
499 * should take care of sending a new CREATE PATH message for each disconnected
502 * @param t Tunnel where to add the new path.
503 * @param p Path to be integrated.
504 * @param cb Callback to use to notify about peers temporarily disconnecting
506 * @return GNUNET_OK in case of success.
507 * GNUNET_SYSERR in case of error.
510 * - go backwards on path looking for each peer in the present tree
511 * - do not disconnect peers until new path is created & connected
514 tree_add_path (struct MeshTunnelTree *t,
515 const struct MeshPeerPath *p,
516 MeshNodeDisconnectCB cb)
518 struct MeshTunnelTreeNode *parent;
519 struct MeshTunnelTreeNode *oldnode;
520 struct MeshTunnelTreeNode *n;
521 struct MeshTunnelTreeNode *c;
522 struct GNUNET_PeerIdentity id;
527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
528 "tree: Adding path [%u] towards peer %u.\n",
530 p->peers[p->length - 1]);
536 GNUNET_assert(0 != p->length);
537 parent = n = t->root;
538 if (n->peer != p->peers[0])
541 return GNUNET_SYSERR;
545 oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
546 /* Look for the first node that is not already present in the tree
548 * Assuming that the tree is somewhat balanced, O(log n * log N).
549 * - Length of the path is expected to be log N (size of whole network).
550 * - Each level of the tree is expected to have log n children (size of tree).
552 me = t->root->peer == myid ? 0 : -1;
553 for (i = 1; i < p->length; i++)
555 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
556 "tree: Looking for peer %u.\n",
559 if (p->peers[i] == myid)
561 for (c = n->children_head; NULL != c; c = c->next)
563 if (c->peer == p->peers[i])
565 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
566 "tree: Found in children of %u.\n",
572 /* If we couldn't find a child equal to path[i], we have reached the end
573 * of the common path. */
577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
578 "tree: All childen visited.\n");
579 /* Add the rest of the path as a branch from parent. */
580 while (i < p->length)
582 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
583 "tree: Adding peer %u, to %u.\n",
587 if (i == p->length - 1 && NULL != oldnode)
589 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
590 "tree: Putting old node into place.\n");
591 oldnode->parent = parent;
592 GNUNET_CONTAINER_DLL_insert(parent->children_head,
593 parent->children_tail,
595 tree_update_first_hops (t, oldnode, NULL);
600 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
601 n = tree_node_new(parent, p->peers[i]);
603 n->status = MESH_PEER_RELAY;
608 n->status = MESH_PEER_SEARCHING;
610 /* Add info about first hop into hashmap. */
611 if (-1 != me && me < p->length - 1)
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "MESH: finding first hop (own pos %d/%u)\n",
616 GNUNET_PEER_resolve (p->peers[me + 1], &id);
617 tree_update_first_hops(t,
618 tree_find_peer(t->root, p->peers[p->length - 1]),
621 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
627 * Notifies a tree that a connection it might be using is broken.
628 * Marks all peers down the paths as disconnected and notifies the client.
630 * @param t Tree to use.
631 * @param p1 Short id of one of the peers (order unimportant)
632 * @param p2 Short id of one of the peers (order unimportant)
633 * @param cb Function to call for every peer that is marked as disconnected.
635 * @return Short ID of the first disconnected peer in the tree.
638 tree_notify_connection_broken (struct MeshTunnelTree *t,
641 MeshNodeDisconnectCB cb)
643 struct MeshTunnelTreeNode *n;
644 struct MeshTunnelTreeNode *c;
646 n = tree_find_peer(t->me, p1);
649 if (NULL != n->parent && n->parent->peer == p2)
651 tree_mark_peers_disconnected(t, n, cb);
652 GNUNET_CONTAINER_DLL_remove(n->parent->children_head,
653 n->parent->children_tail,
655 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
656 t->disconnected_tail,
660 for (c = n->children_head; NULL != c; c = c->next)
664 tree_mark_peers_disconnected(t, c, cb);
665 GNUNET_CONTAINER_DLL_remove(n->children_head,
668 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
669 t->disconnected_tail,
679 * Deletes a peer from a tunnel, liberating all unused resources on the path to
680 * it. It shouldn't have children, if it has they will be destroyed as well.
681 * If the tree is not local and no longer has any paths, the root node will be
682 * destroyed and marked as NULL.
684 * @param t Tunnel tree to use.
685 * @param peer Short ID of the peer to remove from the tunnel tree.
686 * @param cb Callback to notify client of disconnected peers.
688 * @return GNUNET_OK or GNUNET_SYSERR
691 tree_del_peer (struct MeshTunnelTree *t,
693 MeshNodeDisconnectCB cb)
695 struct MeshTunnelTreeNode *n;
697 n = tree_del_path(t, peer, cb);
699 return GNUNET_SYSERR;
700 GNUNET_break_op (NULL == n->children_head);
701 tree_node_destroy(n);
702 if (NULL == t->root->children_head && t->me != t->root)
704 tree_node_destroy (t->root);
712 * Print the tree on stderr
717 tree_debug(struct MeshTunnelTree *t)
719 tree_node_debug(t->root, 0);
724 * Iterator over hash map peer entries and frees all data in it.
725 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
728 * @param key current key code (will no longer contain valid data!!)
729 * @param value value in the hash map (treated as void *)
730 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
733 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
741 * Destroy the whole tree and free all used memory and Peer_Ids
743 * @param t Tree to be destroyed
746 tree_destroy (struct MeshTunnelTree *t)
748 tree_node_destroy(t->root);
749 GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
750 GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);