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);
111 GNUNET_break (NULL != r);
118 * Get the length of a path
120 * @param path The path to measure, with the local peer at any point of it
122 * @return Number of hops to reach destination
123 * UINT_MAX in case the peer is not in the path
126 path_get_length (struct MeshPeerPath *path)
135 * Get the cost of the path relative to the already built tunnel tree
137 * @param t The tunnel tree to which compare
138 * @param path The individual path to reach a peer
140 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
143 * TODO: remove dummy implementation, look into the tunnel tree
146 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
148 return path_get_length (path);
153 * Destroy the path and free any allocated resources linked to it
155 * @param p the path to destroy
157 * @return GNUNET_OK on success
160 path_destroy (struct MeshPeerPath *p)
164 GNUNET_PEER_decrement_rcs (p->peers, p->length);
165 GNUNET_free (p->peers);
173 * Allocates and initializes a new node.
174 * Sets ID and parent of the new node and inserts it in the DLL of the parent
176 * @param parent Node that will be the parent from the new node, NULL for root
177 * @param peer Short Id of the new node
179 * @return Newly allocated node
181 static struct MeshTunnelTreeNode *
182 tree_node_new(struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
184 struct MeshTunnelTreeNode *node;
186 node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
188 GNUNET_PEER_change_rc(peer, 1);
189 node->parent = parent;
191 GNUNET_CONTAINER_DLL_insert(parent->children_head,
192 parent->children_tail,
200 tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
202 struct MeshTunnelTreeNode *c;
203 struct GNUNET_PeerIdentity id;;
206 for (i = 0; i < level; i++)
207 fprintf(stderr, " ");
208 if (n->status == MESH_PEER_READY)
209 fprintf(stderr, "#");
210 if (n->status == MESH_PEER_SEARCHING)
211 fprintf(stderr, "+");
212 if (n->status == MESH_PEER_RELAY)
213 fprintf(stderr, "-");
214 if (n->status == MESH_PEER_RECONNECTING)
215 fprintf(stderr, "*");
217 GNUNET_PEER_resolve(n->peer, &id);
218 fprintf(stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
219 if (NULL != n->parent)
221 GNUNET_PEER_resolve(n->parent->peer, &id);
222 fprintf(stderr, "(-> %s [%u])\n", GNUNET_i2s(&id), n->parent->peer);
225 fprintf(stderr, "(root)\n");
226 for (c = n->children_head; NULL != c; c = c->next)
227 tree_node_debug(c, level + 1);
232 * Destroys and frees the node and all children
234 * @param n Parent node to be destroyed
237 tree_node_destroy (struct MeshTunnelTreeNode *parent)
239 struct MeshTunnelTreeNode *n;
240 struct MeshTunnelTreeNode *next;
242 struct GNUNET_PeerIdentity id;
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "tree: Destroying node %u\n",
247 GNUNET_PEER_resolve (parent->peer, &id);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 n = parent->children_head;
256 tree_node_destroy(n);
259 GNUNET_PEER_change_rc(parent->peer, -1);
260 if (NULL != parent->parent)
261 GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
262 parent->parent->children_tail,
270 * Create a new tunnel tree associated to a tunnel
272 * @param t Tunnel this tree will represent
273 * @param peer A short peer id of the root of the tree
275 * @return A newly allocated and initialized tunnel tree
277 struct MeshTunnelTree *
278 tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
280 struct MeshTunnelTree *tree;
282 tree = GNUNET_malloc(sizeof (struct MeshTunnelTree));
283 tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
284 tree->root = tree_node_new(NULL, peer);
285 tree->root->status = MESH_PEER_ROOT;
294 * Recursively find the given peer in the tree.
296 * @param t Tunnel where to look for the peer.
297 * @param peer Peer to find
299 * @return Pointer to the node of the peer. NULL if not found.
301 struct MeshTunnelTreeNode *
302 tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
304 struct MeshTunnelTreeNode *n;
305 struct MeshTunnelTreeNode *r;
307 if (parent->peer == peer_id)
309 for (n = parent->children_head; NULL != n; n = n->next)
311 r = tree_find_peer (n, peer_id);
320 * Recusively mark peer and children as disconnected, notify client
322 * @param tree Tree this node belongs to
323 * @param parent Node to be clean, potentially with children
324 * @param cb Callback to use to notify about disconnected peers.
327 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
328 struct MeshTunnelTreeNode *parent,
329 MeshNodeDisconnectCB cb)
331 struct GNUNET_PeerIdentity *pi;
332 struct GNUNET_PeerIdentity id;
333 struct MeshTunnelTreeNode *n;
335 for (n = parent->children_head; NULL != n; n = n->next)
337 tree_mark_peers_disconnected (tree, n, cb);
339 if (MESH_PEER_READY == parent->status)
343 parent->status = MESH_PEER_RECONNECTING;
346 /* Remove and free info about first hop */
347 GNUNET_PEER_resolve(parent->peer, &id);
348 pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
349 GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
356 * Recusively update the info about what is the first hop to reach the node
358 * @param tree Tree this nodes belongs to
359 * @param parent Node to be start updating
360 * @param hop If known, ID of the first hop.
361 * If not known, NULL to find out and pass on children.
364 tree_update_first_hops (struct MeshTunnelTree *tree,
365 struct MeshTunnelTreeNode *parent,
366 struct GNUNET_PeerIdentity *hop)
368 struct GNUNET_PeerIdentity pi;
369 struct GNUNET_PeerIdentity *copy;
370 struct GNUNET_PeerIdentity id;
371 struct MeshTunnelTreeNode *n;
374 GNUNET_PEER_resolve(parent->peer, &id);
375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
376 "tree: Finding first hop for %s.\n",
381 struct MeshTunnelTreeNode *aux;
382 struct MeshTunnelTreeNode *old;
385 while (aux != tree->me)
388 GNUNET_PEER_resolve(old->peer, &id);
389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
390 "tree: ... its not %s.\n",
395 GNUNET_assert(NULL != aux);
398 GNUNET_PEER_resolve(old->peer, &id);
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
404 GNUNET_PEER_resolve(old->peer, hop);
406 GNUNET_PEER_resolve(parent->peer, &id);
407 copy = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
409 copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
412 (void) GNUNET_CONTAINER_multihashmap_put(
416 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
418 for (n = parent->children_head; NULL != n; n = n->next)
420 tree_update_first_hops (tree, n, hop);
426 * Delete the current path to the peer, including all now unused relays.
427 * The destination peer is NOT destroyed, it is returned in order to either set
428 * a new path to it or destroy it explicitly, taking care of it's child nodes.
430 * @param t Tunnel tree where to delete the path from.
431 * @param peer Destination peer whose path we want to remove.
432 * @param cb Callback to use to notify about disconnected peers.
434 * @return pointer to the pathless node.
435 * NULL when not found
437 struct MeshTunnelTreeNode *
438 tree_del_path (struct MeshTunnelTree *t,
439 GNUNET_PEER_Id peer_id,
440 MeshNodeDisconnectCB cb)
442 struct MeshTunnelTreeNode *parent;
443 struct MeshTunnelTreeNode *node;
444 struct MeshTunnelTreeNode *n;
447 struct GNUNET_PeerIdentity id;
448 GNUNET_PEER_resolve(peer_id, &id);
449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
450 "tree: Deleting path to %s.\n",
453 if (peer_id == t->root->peer)
456 for (n = t->disconnected_head; NULL != n; n = n->next)
458 if (n->peer == peer_id)
460 /* Was already pathless, waiting for reconnection */
461 GNUNET_CONTAINER_DLL_remove (t->disconnected_head,
462 t->disconnected_tail,
467 n = tree_find_peer (t->root, peer_id);
473 GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
476 while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
479 GNUNET_PEER_resolve(parent->peer, &id);
480 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
481 "tree: Deleting node %s.\n",
485 tree_node_destroy(parent);
489 GNUNET_PEER_resolve(parent->peer, &id);
490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
491 "tree: Not deleted peer %s.\n",
495 tree_mark_peers_disconnected (t, node, cb);
502 * Return a newly allocated individual path to reach a peer from the local peer,
503 * according to the path tree of some tunnel.
505 * @param t Tunnel from which to read the path tree
506 * @param peer_info Destination peer to whom we want a path
508 * @return A newly allocated individual path to reach the destination peer.
509 * Path must be destroyed afterwards.
511 struct MeshPeerPath *
512 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
514 struct MeshTunnelTreeNode *n;
515 struct MeshPeerPath *p;
516 GNUNET_PEER_Id myid = t->me->peer;
518 n = tree_find_peer(t->me, peer);
523 /* Building the path (inverted!) */
524 while (n->peer != myid)
526 GNUNET_array_append(p->peers, p->length, n->peer);
527 GNUNET_PEER_change_rc(n->peer, 1);
529 GNUNET_assert(NULL != n);
531 GNUNET_array_append(p->peers, p->length, myid);
532 GNUNET_PEER_change_rc(myid, 1);
542 * Integrate a stand alone path into the tunnel tree.
543 * If the peer toward which the new path is already in the tree, the peer
544 * and its children will be maked as disconnected and the callback
545 * will be called on each one of them. They will be maked as online only after
546 * receiving a PATH ACK for the new path for each one of them, so the caller
547 * should take care of sending a new CREATE PATH message for each disconnected
550 * @param t Tunnel where to add the new path.
551 * @param p Path to be integrated.
552 * @param cb Callback to use to notify about peers temporarily disconnecting
554 * @return GNUNET_OK in case of success.
555 * GNUNET_SYSERR in case of error.
558 * - go backwards on path looking for each peer in the present tree
559 * - do not disconnect peers until new path is created & connected
562 tree_add_path (struct MeshTunnelTree *t,
563 const struct MeshPeerPath *p,
564 MeshNodeDisconnectCB cb)
566 struct MeshTunnelTreeNode *parent;
567 struct MeshTunnelTreeNode *oldnode;
568 struct MeshTunnelTreeNode *n;
569 struct MeshTunnelTreeNode *c;
570 struct GNUNET_PeerIdentity id;
573 // int oldnode_is_me;
577 GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
578 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
579 "tree: Adding path [%u] towards peer %s.\n",
588 GNUNET_assert(0 != p->length);
589 parent = n = t->root;
590 if (n->peer != p->peers[0])
593 return GNUNET_SYSERR;
597 oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
598 /* Look for the first node that is not already present in the tree
600 * Assuming that the tree is somewhat balanced, O(log n * log N).
601 * - Length of the path is expected to be log N (size of whole network).
602 * - Each level of the tree is expected to have log n children (size of tree).
604 me = t->root->peer == myid ? 0 : -1;
605 for (i = 1; i < p->length; i++)
608 GNUNET_PEER_resolve(p->peers[i], &id);
609 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
610 "tree: Looking for peer %s.\n",
614 if (p->peers[i] == myid)
616 for (c = n->children_head; NULL != c; c = c->next)
618 if (c->peer == p->peers[i])
621 GNUNET_PEER_resolve(parent->peer, &id);
622 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
623 "tree: Found in children of %s.\n",
630 /* If we couldn't find a child equal to path[i], we have reached the end
631 * of the common path. */
636 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
637 "tree: All childen visited.\n");
639 /* Add the rest of the path as a branch from parent. */
640 while (i < p->length)
643 GNUNET_PEER_resolve(p->peers[i], &id);
644 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
645 "tree: Adding peer %s.\n",
647 GNUNET_PEER_resolve(parent->peer, &id);
648 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
653 if (i == p->length - 1 && NULL != oldnode)
656 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
657 "tree: Putting old node into place.\n");
659 oldnode->parent = parent;
660 GNUNET_CONTAINER_DLL_insert(parent->children_head,
661 parent->children_tail,
663 tree_update_first_hops (t, oldnode, NULL);
669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
671 n = tree_node_new(parent, p->peers[i]);
673 n->status = MESH_PEER_RELAY;
678 n->status = MESH_PEER_SEARCHING;
680 /* Add info about first hop into hashmap. */
681 if (-1 != me && me < p->length - 1)
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "MESH: finding first hop (own pos %d/%u)\n",
688 GNUNET_PEER_resolve (p->peers[me + 1], &id);
689 tree_update_first_hops(t,
690 tree_find_peer(t->root, p->peers[p->length - 1]),
694 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
701 * Notifies a tree that a connection it might be using is broken.
702 * Marks all peers down the paths as disconnected and notifies the client.
704 * @param t Tree to use.
705 * @param p1 Short id of one of the peers (order unimportant)
706 * @param p2 Short id of one of the peers (order unimportant)
707 * @param cb Function to call for every peer that is marked as disconnected.
709 * @return Short ID of the first disconnected peer in the tree.
712 tree_notify_connection_broken (struct MeshTunnelTree *t,
715 MeshNodeDisconnectCB cb)
717 struct MeshTunnelTreeNode *n;
718 struct MeshTunnelTreeNode *c;
720 n = tree_find_peer(t->me, p1);
723 if (NULL != n->parent && n->parent->peer == p2)
725 tree_mark_peers_disconnected(t, n, cb);
726 GNUNET_CONTAINER_DLL_remove(n->parent->children_head,
727 n->parent->children_tail,
729 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
730 t->disconnected_tail,
734 for (c = n->children_head; NULL != c; c = c->next)
738 tree_mark_peers_disconnected(t, c, cb);
739 GNUNET_CONTAINER_DLL_remove(n->children_head,
742 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
743 t->disconnected_tail,
753 * Deletes a peer from a tunnel, liberating all unused resources on the path to
754 * it. It shouldn't have children, if it has they will be destroyed as well.
755 * If the tree is not local and no longer has any paths, the root node will be
756 * destroyed and marked as NULL.
758 * @param t Tunnel tree to use.
759 * @param peer Short ID of the peer to remove from the tunnel tree.
760 * @param cb Callback to notify client of disconnected peers.
762 * @return GNUNET_OK or GNUNET_SYSERR
765 tree_del_peer (struct MeshTunnelTree *t,
767 MeshNodeDisconnectCB cb)
769 struct MeshTunnelTreeNode *n;
771 n = tree_del_path(t, peer, cb);
773 return GNUNET_SYSERR;
774 GNUNET_break_op (NULL == n->children_head);
775 tree_node_destroy(n);
776 if (NULL == t->root->children_head && t->me != t->root)
778 tree_node_destroy (t->root);
786 * Print the tree on stderr
791 tree_debug(struct MeshTunnelTree *t)
793 tree_node_debug(t->root, 0);
798 * Iterator over hash map peer entries and frees all data in it.
799 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
802 * @param key current key code (will no longer contain valid data!!)
803 * @param value value in the hash map (treated as void *)
804 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
807 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
815 * Destroy the whole tree and free all used memory and Peer_Ids
817 * @param t Tree to be destroyed
820 tree_destroy (struct MeshTunnelTree *t)
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
825 tree_node_destroy(t->root);
826 GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
827 GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);