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"
30 #define MESH_TREE_DEBUG GNUNET_YES
36 * @param lenght How many hops will the path have.
38 * @return A newly allocated path with a peer array of the specified length.
41 path_new (unsigned int length)
43 struct MeshPeerPath *p;
45 p = GNUNET_malloc (sizeof(struct MeshPeerPath));
49 p->peers = GNUNET_malloc (length * sizeof(GNUNET_PEER_Id));
58 * @param p the path to invert
61 path_invert (struct MeshPeerPath *path)
66 for (i = 0; i < path->length / 2; i++)
69 path->peers[i] = path->peers[path->length - i - 1];
70 path->peers[path->length - i - 1] = aux;
76 * Duplicate a path, incrementing short peer's rc.
78 * @param p The path to duplicate.
81 path_duplicate (struct MeshPeerPath *path)
83 struct MeshPeerPath *aux;
86 aux = path_new(path->length);
87 memcpy (aux->peers, path->peers, path->length * sizeof(GNUNET_PEER_Id));
88 for (i = 0; i < path->length; i++)
89 GNUNET_PEER_change_rc(path->peers[i], 1);
95 * Find the first peer whom to send a packet to go down this path
97 * @param t The tunnel tree to use
98 * @param peer The peerinfo of the peer we are trying to reach
100 * @return peerinfo of the peer who is the first hop in the tunnel
103 struct GNUNET_PeerIdentity *
104 path_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
106 struct GNUNET_PeerIdentity id;
107 struct GNUNET_PeerIdentity *r;
109 GNUNET_PEER_resolve (peer, &id);
110 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
113 struct MeshTunnelTreeNode *n;
115 n = tree_find_peer(t->root, peer);
116 if (NULL != t->me && NULL != n)
118 tree_update_first_hops(t, n, NULL);
119 r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
120 GNUNET_assert (NULL != r);
124 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
125 "Tree structure inconsistent! me: %p, n: %p",
136 * Get the length of a path
138 * @param path The path to measure, with the local peer at any point of it
140 * @return Number of hops to reach destination
141 * UINT_MAX in case the peer is not in the path
144 path_get_length (struct MeshPeerPath *path)
153 * Get the cost of the path relative to the already built tunnel tree
155 * @param t The tunnel tree to which compare
156 * @param path The individual path to reach a peer
158 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
161 * TODO: remove dummy implementation, look into the tunnel tree
164 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
166 return path_get_length (path);
171 * Destroy the path and free any allocated resources linked to it
173 * @param p the path to destroy
175 * @return GNUNET_OK on success
178 path_destroy (struct MeshPeerPath *p)
182 GNUNET_PEER_decrement_rcs (p->peers, p->length);
183 GNUNET_free (p->peers);
191 * Allocates and initializes a new node.
192 * Sets ID and parent of the new node and inserts it in the DLL of the parent
194 * @param parent Node that will be the parent from the new node, NULL for root
195 * @param peer Short Id of the new node
197 * @return Newly allocated node
199 static struct MeshTunnelTreeNode *
200 tree_node_new(struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
202 struct MeshTunnelTreeNode *node;
204 node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
206 GNUNET_PEER_change_rc(peer, 1);
207 node->parent = parent;
209 GNUNET_CONTAINER_DLL_insert(parent->children_head,
210 parent->children_tail,
218 tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
220 struct MeshTunnelTreeNode *c;
221 struct GNUNET_PeerIdentity id;;
224 for (i = 0; i < level; i++)
225 fprintf(stderr, " ");
226 if (n->status == MESH_PEER_READY)
227 fprintf(stderr, "#");
228 if (n->status == MESH_PEER_SEARCHING)
229 fprintf(stderr, "+");
230 if (n->status == MESH_PEER_RELAY)
231 fprintf(stderr, "-");
232 if (n->status == MESH_PEER_RECONNECTING)
233 fprintf(stderr, "*");
235 GNUNET_PEER_resolve(n->peer, &id);
236 fprintf(stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
237 if (NULL != n->parent)
239 GNUNET_PEER_resolve(n->parent->peer, &id);
240 fprintf(stderr, "(-> %s [%u])\n", GNUNET_i2s(&id), n->parent->peer);
243 fprintf(stderr, "(root)\n");
244 for (c = n->children_head; NULL != c; c = c->next)
245 tree_node_debug(c, level + 1);
250 * Destroys and frees the node and all children
252 * @param n Parent node to be destroyed
255 tree_node_destroy (struct MeshTunnelTreeNode *parent)
257 struct MeshTunnelTreeNode *n;
258 struct MeshTunnelTreeNode *next;
260 struct GNUNET_PeerIdentity id;
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "tree: Destroying node %u\n",
265 GNUNET_PEER_resolve (parent->peer, &id);
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 n = parent->children_head;
274 tree_node_destroy(n);
277 GNUNET_PEER_change_rc(parent->peer, -1);
278 if (NULL != parent->parent)
279 GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
280 parent->parent->children_tail,
288 * Create a new tunnel tree associated to a tunnel
290 * @param t Tunnel this tree will represent
291 * @param peer A short peer id of the root of the tree
293 * @return A newly allocated and initialized tunnel tree
295 struct MeshTunnelTree *
296 tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
298 struct MeshTunnelTree *tree;
300 tree = GNUNET_malloc(sizeof (struct MeshTunnelTree));
301 tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
302 tree->root = tree_node_new(NULL, peer);
303 tree->root->status = MESH_PEER_ROOT;
312 * Recursively find the given peer in the tree.
314 * @param t Tunnel where to look for the peer.
315 * @param peer Peer to find
317 * @return Pointer to the node of the peer. NULL if not found.
319 struct MeshTunnelTreeNode *
320 tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
322 struct MeshTunnelTreeNode *n;
323 struct MeshTunnelTreeNode *r;
325 if (parent->peer == peer_id)
327 for (n = parent->children_head; NULL != n; n = n->next)
329 r = tree_find_peer (n, peer_id);
338 * Recusively mark peer and children as disconnected, notify client
340 * @param tree Tree this node belongs to
341 * @param parent Node to be clean, potentially with children
342 * @param cb Callback to use to notify about disconnected peers.
345 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
346 struct MeshTunnelTreeNode *parent,
347 MeshNodeDisconnectCB cb)
349 struct GNUNET_PeerIdentity *pi;
350 struct GNUNET_PeerIdentity id;
351 struct MeshTunnelTreeNode *n;
353 for (n = parent->children_head; NULL != n; n = n->next)
355 tree_mark_peers_disconnected (tree, n, cb);
357 if (MESH_PEER_READY == parent->status)
361 parent->status = MESH_PEER_RECONNECTING;
364 /* Remove and free info about first hop */
365 GNUNET_PEER_resolve(parent->peer, &id);
366 pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
367 GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
374 * Recusively update the info about what is the first hop to reach the node
376 * @param tree Tree this nodes belongs to
377 * @param parent Node to be start updating
378 * @param hop If known, ID of the first hop.
379 * If not known, NULL to find out and pass on children.
382 tree_update_first_hops (struct MeshTunnelTree *tree,
383 struct MeshTunnelTreeNode *parent,
384 struct GNUNET_PeerIdentity *hop)
386 struct GNUNET_PeerIdentity pi;
387 struct GNUNET_PeerIdentity *copy;
388 struct GNUNET_PeerIdentity id;
389 struct MeshTunnelTreeNode *n;
392 GNUNET_PEER_resolve(parent->peer, &id);
393 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
394 "tree: Finding first hop for %s.\n",
399 struct MeshTunnelTreeNode *aux;
400 struct MeshTunnelTreeNode *old;
403 while (aux != tree->me)
406 GNUNET_PEER_resolve(old->peer, &id);
407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
408 "tree: ... its not %s.\n",
413 GNUNET_assert(NULL != aux);
416 GNUNET_PEER_resolve(old->peer, &id);
417 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
422 GNUNET_PEER_resolve(old->peer, hop);
424 GNUNET_PEER_resolve(parent->peer, &id);
425 copy = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
427 copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
430 (void) GNUNET_CONTAINER_multihashmap_put(
434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
436 for (n = parent->children_head; NULL != n; n = n->next)
438 tree_update_first_hops (tree, n, hop);
444 * Delete the current path to the peer, including all now unused relays.
445 * The destination peer is NOT destroyed, it is returned in order to either set
446 * a new path to it or destroy it explicitly, taking care of it's child nodes.
448 * @param t Tunnel tree where to delete the path from.
449 * @param peer Destination peer whose path we want to remove.
450 * @param cb Callback to use to notify about disconnected peers.
452 * @return pointer to the pathless node.
453 * NULL when not found
455 struct MeshTunnelTreeNode *
456 tree_del_path (struct MeshTunnelTree *t,
457 GNUNET_PEER_Id peer_id,
458 MeshNodeDisconnectCB cb)
460 struct MeshTunnelTreeNode *parent;
461 struct MeshTunnelTreeNode *node;
462 struct MeshTunnelTreeNode *n;
465 struct GNUNET_PeerIdentity id;
466 GNUNET_PEER_resolve(peer_id, &id);
467 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
468 "tree: Deleting path to %s.\n",
471 if (peer_id == t->root->peer)
474 for (n = t->disconnected_head; NULL != n; n = n->next)
476 if (n->peer == peer_id)
478 /* Was already pathless, waiting for reconnection */
479 GNUNET_CONTAINER_DLL_remove (t->disconnected_head,
480 t->disconnected_tail,
485 n = tree_find_peer (t->root, peer_id);
491 GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
494 while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
497 GNUNET_PEER_resolve(parent->peer, &id);
498 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
499 "tree: Deleting node %s.\n",
503 tree_node_destroy(parent);
507 GNUNET_PEER_resolve(parent->peer, &id);
508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
509 "tree: Not deleted peer %s.\n",
513 tree_mark_peers_disconnected (t, node, cb);
520 * Return a newly allocated individual path to reach a peer from the local peer,
521 * according to the path tree of some tunnel.
523 * @param t Tunnel from which to read the path tree
524 * @param peer_info Destination peer to whom we want a path
526 * @return A newly allocated individual path to reach the destination peer.
527 * Path must be destroyed afterwards.
529 struct MeshPeerPath *
530 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
532 struct MeshTunnelTreeNode *n;
533 struct MeshPeerPath *p;
534 GNUNET_PEER_Id myid = t->me->peer;
536 n = tree_find_peer(t->me, peer);
541 /* Building the path (inverted!) */
542 while (n->peer != myid)
544 GNUNET_array_append(p->peers, p->length, n->peer);
545 GNUNET_PEER_change_rc(n->peer, 1);
547 GNUNET_assert(NULL != n);
549 GNUNET_array_append(p->peers, p->length, myid);
550 GNUNET_PEER_change_rc(myid, 1);
560 * Integrate a stand alone path into the tunnel tree.
561 * If the peer toward which the new path is already in the tree, the peer
562 * and its children will be maked as disconnected and the callback
563 * will be called on each one of them. They will be maked as online only after
564 * receiving a PATH ACK for the new path for each one of them, so the caller
565 * should take care of sending a new CREATE PATH message for each disconnected
568 * @param t Tunnel where to add the new path.
569 * @param p Path to be integrated.
570 * @param cb Callback to use to notify about peers temporarily disconnecting
572 * @return GNUNET_OK in case of success.
573 * GNUNET_SYSERR in case of error.
576 * - go backwards on path looking for each peer in the present tree
577 * - do not disconnect peers until new path is created & connected
580 tree_add_path (struct MeshTunnelTree *t,
581 const struct MeshPeerPath *p,
582 MeshNodeDisconnectCB cb)
584 struct MeshTunnelTreeNode *parent;
585 struct MeshTunnelTreeNode *oldnode;
586 struct MeshTunnelTreeNode *n;
587 struct MeshTunnelTreeNode *c;
588 struct GNUNET_PeerIdentity id;
594 GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
595 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
596 "tree: Adding path [%u] towards peer %s.\n",
605 GNUNET_assert(0 != p->length);
606 parent = n = t->root;
607 if (n->peer != p->peers[0])
610 return GNUNET_SYSERR;
614 oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
615 /* Look for the first node that is not already present in the tree
617 * Assuming that the tree is somewhat balanced, O(log n * log N).
618 * - Length of the path is expected to be log N (size of whole network).
619 * - Each level of the tree is expected to have log n children (size of tree).
621 me = t->root->peer == myid ? 0 : -1;
622 for (i = 1; i < p->length; i++)
625 GNUNET_PEER_resolve(p->peers[i], &id);
626 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
627 "tree: Looking for peer %s.\n",
631 if (p->peers[i] == myid)
633 for (c = n->children_head; NULL != c; c = c->next)
635 if (c->peer == p->peers[i])
638 GNUNET_PEER_resolve(parent->peer, &id);
639 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
640 "tree: Found in children of %s.\n",
647 /* If we couldn't find a child equal to path[i], we have reached the end
648 * of the common path. */
653 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
654 "tree: All childen visited.\n");
656 /* Add the rest of the path as a branch from parent. */
657 while (i < p->length)
660 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
661 "tree: Adding peer %u to %u.\n",
662 p->peers[i], parent->peer);
663 GNUNET_PEER_resolve(p->peers[i], &id);
664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
665 "tree: Adding peer %s.\n",
667 GNUNET_PEER_resolve(parent->peer, &id);
668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
673 if (i == p->length - 1 && NULL != oldnode)
676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
677 "tree: Putting old node into place.\n");
679 oldnode->parent = parent;
680 GNUNET_CONTAINER_DLL_insert(parent->children_head,
681 parent->children_tail,
683 tree_update_first_hops (t, oldnode, NULL);
689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
691 n = tree_node_new(parent, p->peers[i]);
693 n->status = MESH_PEER_RELAY;
700 n->status = MESH_PEER_SEARCHING;
702 /* Add info about first hop into hashmap. */
703 if (-1 != me && me < p->length - 1)
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "MESH: finding first hop (own pos %d/%u)\n",
710 GNUNET_PEER_resolve (p->peers[me + 1], &id);
711 tree_update_first_hops(t,
712 tree_find_peer(t->root, p->peers[p->length - 1]),
716 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
723 * Notifies a tree that a connection it might be using is broken.
724 * Marks all peers down the paths as disconnected and notifies the client.
726 * @param t Tree to use.
727 * @param p1 Short id of one of the peers (order unimportant)
728 * @param p2 Short id of one of the peers (order unimportant)
729 * @param cb Function to call for every peer that is marked as disconnected.
731 * @return Short ID of the first disconnected peer in the tree.
734 tree_notify_connection_broken (struct MeshTunnelTree *t,
737 MeshNodeDisconnectCB cb)
739 struct MeshTunnelTreeNode *n;
740 struct MeshTunnelTreeNode *c;
742 n = tree_find_peer(t->me, p1);
745 if (NULL != n->parent && n->parent->peer == p2)
747 tree_mark_peers_disconnected(t, n, cb);
748 GNUNET_CONTAINER_DLL_remove(n->parent->children_head,
749 n->parent->children_tail,
751 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
752 t->disconnected_tail,
756 for (c = n->children_head; NULL != c; c = c->next)
760 tree_mark_peers_disconnected(t, c, cb);
761 GNUNET_CONTAINER_DLL_remove(n->children_head,
764 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
765 t->disconnected_tail,
775 * Deletes a peer from a tunnel, liberating all unused resources on the path to
776 * it. It shouldn't have children, if it has they will be destroyed as well.
777 * If the tree is not local and no longer has any paths, the root node will be
778 * destroyed and marked as NULL.
780 * @param t Tunnel tree to use.
781 * @param peer Short ID of the peer to remove from the tunnel tree.
782 * @param cb Callback to notify client of disconnected peers.
784 * @return GNUNET_OK or GNUNET_SYSERR
787 tree_del_peer (struct MeshTunnelTree *t,
789 MeshNodeDisconnectCB cb)
791 struct MeshTunnelTreeNode *n;
793 n = tree_del_path(t, peer, cb);
795 return GNUNET_SYSERR;
796 GNUNET_break_op (NULL == n->children_head);
797 tree_node_destroy(n);
798 if (NULL == t->root->children_head && t->me != t->root)
800 tree_node_destroy (t->root);
808 * Print the tree on stderr
813 tree_debug(struct MeshTunnelTree *t)
815 tree_node_debug(t->root, 0);
820 * Iterator over hash map peer entries and frees all data in it.
821 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
824 * @param key current key code (will no longer contain valid data!!)
825 * @param value value in the hash map (treated as void *)
826 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
829 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
837 * Destroy the whole tree and free all used memory and Peer_Ids
839 * @param t Tree to be destroyed
842 tree_destroy (struct MeshTunnelTree *t)
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
847 tree_node_destroy(t->root);
848 GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
849 GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);