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;
106 GNUNET_PEER_resolve (peer, &id);
107 return GNUNET_CONTAINER_multihashmap_get (t->first_hops,
113 * Get the length of a path
115 * @param path The path to measure, with the local peer at any point of it
117 * @return Number of hops to reach destination
118 * UINT_MAX in case the peer is not in the path
121 path_get_length (struct MeshPeerPath *path)
130 * Get the cost of the path relative to the already built tunnel tree
132 * @param t The tunnel tree to which compare
133 * @param path The individual path to reach a peer
135 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
138 * TODO: remove dummy implementation, look into the tunnel tree
141 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
143 return path_get_length (path);
148 * Destroy the path and free any allocated resources linked to it
150 * @param p the path to destroy
152 * @return GNUNET_OK on success
155 path_destroy (struct MeshPeerPath *p)
157 GNUNET_PEER_decrement_rcs (p->peers, p->length);
158 GNUNET_free (p->peers);
166 * Allocates and initializes a new node.
167 * Sets ID and parent of the new node and inserts it in the DLL of the parent
169 * @param parent Node that will be the parent from the new node, NULL for root
170 * @param peer Short Id of the new node
172 * @return Newly allocated node
174 static struct MeshTunnelTreeNode *
175 tree_node_new(struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
177 struct MeshTunnelTreeNode *node;
179 node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
181 GNUNET_PEER_change_rc(peer, 1);
182 node->parent = parent;
184 GNUNET_CONTAINER_DLL_insert(parent->children_head,
185 parent->children_tail,
193 tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
195 struct MeshTunnelTreeNode *c;
198 for (i = 0; i < level; i++)
199 fprintf(stderr, " ");
200 if (n->status == MESH_PEER_READY)
201 fprintf(stderr, "#");
202 if (n->status == MESH_PEER_SEARCHING)
203 fprintf(stderr, "+");
204 if (n->status == MESH_PEER_RELAY)
205 fprintf(stderr, "-");
206 if (n->status == MESH_PEER_RECONNECTING)
207 fprintf(stderr, "*");
209 fprintf(stderr, "%u [%p] ", n->peer, n);
210 if (NULL != n->parent)
211 fprintf(stderr, "(-> %u)\n", n->parent->peer);
213 fprintf(stderr, "(root)\n");
214 for (c = n->children_head; NULL != c; c = c->next)
215 tree_node_debug(c, level + 1);
220 * Destroys and frees the node and all children
222 * @param n Parent node to be destroyed
225 tree_node_destroy (struct MeshTunnelTreeNode *parent)
227 struct MeshTunnelTreeNode *n;
228 struct MeshTunnelTreeNode *next;
230 n = parent->children_head;
234 tree_node_destroy(n);
237 GNUNET_PEER_change_rc(parent->peer, -1);
238 if (NULL != parent->parent)
239 GNUNET_CONTAINER_DLL_remove(parent->parent->children_head,
240 parent->parent->children_tail,
248 * Create a new tunnel tree associated to a tunnel
250 * @param t Tunnel this tree will represent
251 * @param peer A short peer id of the root of the tree
253 * @return A newly allocated and initialized tunnel tree
255 struct MeshTunnelTree *
256 tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer)
258 struct MeshTunnelTree *tree;
260 tree = GNUNET_malloc(sizeof (struct MeshTunnelTree));
261 tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
262 tree->root = tree_node_new(NULL, peer);
271 * Recursively find the given peer in the tree.
273 * @param t Tunnel where to look for the peer.
274 * @param peer Peer to find
276 * @return Pointer to the node of the peer. NULL if not found.
278 struct MeshTunnelTreeNode *
279 tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
281 struct MeshTunnelTreeNode *n;
282 struct MeshTunnelTreeNode *r;
284 if (parent->peer == peer_id)
286 for (n = parent->children_head; NULL != n; n = n->next)
288 r = tree_find_peer (n, peer_id);
297 * Recusively mark peer and children as disconnected, notify client
299 * @param tree Tree this node belongs to
300 * @param parent Node to be clean, potentially with children
301 * @param cb Callback to use to notify about disconnected peers.
304 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
305 struct MeshTunnelTreeNode *parent,
306 MeshNodeDisconnectCB cb)
308 struct GNUNET_PeerIdentity *pi;
309 struct GNUNET_PeerIdentity id;
310 struct MeshTunnelTreeNode *n;
312 for (n = parent->children_head; NULL != n; n = n->next)
314 tree_mark_peers_disconnected (tree, n, cb);
316 if (MESH_PEER_READY == parent->status)
320 parent->status = MESH_PEER_RECONNECTING;
322 /* Remove and free info about first hop */
323 GNUNET_PEER_resolve(parent->peer, &id);
324 pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
325 GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
332 * Recusively update the info about what is the first hop to reach the node
334 * @param tree Tree this nodes belongs to
335 * @param parent Node to be start updating
336 * @param hop If known, ID of the first hop.
337 * If not known, NULL to find out and pass on children.
340 tree_update_first_hops (struct MeshTunnelTree *tree,
341 struct MeshTunnelTreeNode *parent,
342 struct GNUNET_PeerIdentity *hop)
344 struct GNUNET_PeerIdentity pi;
345 struct GNUNET_PeerIdentity *copy;
346 struct GNUNET_PeerIdentity id;
347 struct MeshTunnelTreeNode *n;
349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
350 "tree: Finding first hop for %u.\n",
354 struct MeshTunnelTreeNode *aux;
355 struct MeshTunnelTreeNode *old;
359 while (aux != tree->me)
361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
362 "tree: ... its not %u.\n",
366 GNUNET_assert(NULL != aux);
368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
372 GNUNET_PEER_resolve(old->peer, hop);
374 copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
376 GNUNET_PEER_resolve(parent->peer, &id);
377 GNUNET_CONTAINER_multihashmap_put(tree->first_hops, &id.hashPubKey, copy,
378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
380 for (n = parent->children_head; NULL != n; n = n->next)
382 tree_update_first_hops (tree, n, hop);
388 * Delete the current path to the peer, including all now unused relays.
389 * The destination peer is NOT destroyed, it is returned in order to either set
390 * a new path to it or destroy it explicitly, taking care of it's child nodes.
392 * @param t Tunnel tree where to delete the path from.
393 * @param peer Destination peer whose path we want to remove.
394 * @param cb Callback to use to notify about disconnected peers.
396 * @return pointer to the pathless node.
397 * NULL when not found
399 struct MeshTunnelTreeNode *
400 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
401 MeshNodeDisconnectCB cb)
403 struct MeshTunnelTreeNode *parent;
404 struct MeshTunnelTreeNode *node;
405 struct MeshTunnelTreeNode *n;
407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
408 "tree: Deleting path to %u.\n", peer_id);
409 if (peer_id == t->root->peer)
412 for (n = t->disconnected_head; NULL != n; n = n->next)
414 if (n->peer == peer_id)
416 /* Was already pathless, waiting for reconnection */
417 GNUNET_CONTAINER_DLL_remove (t->disconnected_head,
418 t->disconnected_tail,
423 n = tree_find_peer (t->me, peer_id);
429 GNUNET_CONTAINER_DLL_remove(parent->children_head, parent->children_tail, n);
432 while (t->root != parent && MESH_PEER_RELAY == parent->status &&
433 NULL == parent->children_head)
435 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
436 "tree: Deleting node %u.\n",
439 tree_node_destroy(parent);
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
443 "tree: Not deleted peer %u.\n",
446 tree_mark_peers_disconnected (t, node, cb);
453 * Return a newly allocated individual path to reach a peer from the local peer,
454 * according to the path tree of some tunnel.
456 * @param t Tunnel from which to read the path tree
457 * @param peer_info Destination peer to whom we want a path
459 * @return A newly allocated individual path to reach the destination peer.
460 * Path must be destroyed afterwards.
462 struct MeshPeerPath *
463 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
465 struct MeshTunnelTreeNode *n;
466 struct MeshPeerPath *p;
467 GNUNET_PEER_Id myid = t->me->peer;
469 n = tree_find_peer(t->me, peer);
472 /* Building the path (inverted!) */
473 while (n->peer != myid)
475 GNUNET_array_append(p->peers, p->length, n->peer);
476 GNUNET_PEER_change_rc(n->peer, 1);
478 GNUNET_assert(NULL != n);
480 GNUNET_array_append(p->peers, p->length, myid);
481 GNUNET_PEER_change_rc(myid, 1);
490 * Integrate a stand alone path into the tunnel tree.
491 * If the peer toward which the new path is already in the tree, the peer
492 * and its children will be maked as disconnected and the callback
493 * will be called on each one of them. They will be maked as online only after
494 * receiving a PATH ACK for the new path for each one of them, so the caller
495 * should take care of sending a new CREATE PATH message for each disconnected
498 * @param t Tunnel where to add the new path.
499 * @param p Path to be integrated.
500 * @param cb Callback to use to notify about peers temporarily disconnecting
502 * @return GNUNET_OK in case of success.
503 * GNUNET_SYSERR in case of error.
506 * - go backwards on path looking for each peer in the present tree
507 * - do not disconnect peers until new path is created & connected
510 tree_add_path (struct MeshTunnelTree *t,
511 const struct MeshPeerPath *p,
512 MeshNodeDisconnectCB cb)
514 struct MeshTunnelTreeNode *parent;
515 struct MeshTunnelTreeNode *oldnode;
516 struct MeshTunnelTreeNode *n;
517 struct MeshTunnelTreeNode *c;
518 struct GNUNET_PeerIdentity id;
519 struct GNUNET_PeerIdentity *hop;
524 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
525 "tree: Adding path [%u] towards peer %u to peer %u.\n",
527 p->peers[p->length - 1],
531 GNUNET_assert(0 != p->length);
532 parent = n = t->root;
533 if (n->peer != p->peers[0])
536 return GNUNET_SYSERR;
540 oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
541 /* Look for the first node that is not already present in the tree
543 * Assuming that the tree is somewhat balanced, O(log n * log N).
544 * - Length of the path is expected to be log N (size of whole network).
545 * - Each level of the tree is expected to have log n children (size of tree).
547 me = t->root->peer == myid ? 0 : -1;
548 for (i = 1; i < p->length; i++)
550 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
551 "tree: Looking for peer %u.\n",
554 if (p->peers[i] == myid)
556 for (c = n->children_head; NULL != c; c = c->next)
558 if (c->peer == p->peers[i])
560 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
561 "tree: Found in children of %u.\n",
567 /* If we couldn't find a child equal to path[i], we have reached the end
568 * of the common path. */
572 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
573 "tree: All childen visited.\n");
576 /* New path deviates from tree before reaching us. What happened? */
578 return GNUNET_SYSERR;
580 /* Add the rest of the path as a branch from parent. */
581 while (i < p->length)
583 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
584 "tree: Adding peer %u, to %u.\n",
588 if (i == p->length - 1 && NULL != oldnode)
590 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "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 (me < p->length - 1)
613 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
614 hop = GNUNET_CONTAINER_multihashmap_get(t->first_hops, &id.hashPubKey);
617 GNUNET_CONTAINER_multihashmap_remove_all(t->first_hops, &id.hashPubKey);
620 hop = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
621 GNUNET_PEER_resolve (p->peers[me + 1], hop);
622 GNUNET_CONTAINER_multihashmap_put (t->first_hops, &id.hashPubKey,
624 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
626 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
632 * Notifies a tree that a connection it might be using is broken.
633 * Marks all peers down the paths as disconnected and notifies the client.
635 * @param t Tree to use.
636 * @param p1 Short id of one of the peers (order unimportant)
637 * @param p2 Short id of one of the peers (order unimportant)
638 * @param cb Function to call for every peer that is marked as disconnected.
640 * @return Short ID of the first disconnected peer in the tree.
643 tree_notify_connection_broken (struct MeshTunnelTree *t,
646 MeshNodeDisconnectCB cb)
648 struct MeshTunnelTreeNode *n;
649 struct MeshTunnelTreeNode *c;
651 n = tree_find_peer(t->me, p1);
654 if (NULL != n->parent && n->parent->peer == p2)
656 tree_mark_peers_disconnected(t, n, cb);
657 GNUNET_CONTAINER_DLL_remove(n->parent->children_head,
658 n->parent->children_tail,
660 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
661 t->disconnected_tail,
665 for (c = n->children_head; NULL != c; c = c->next)
669 tree_mark_peers_disconnected(t, c, cb);
670 GNUNET_CONTAINER_DLL_remove(n->children_head,
673 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
674 t->disconnected_tail,
684 * Deletes a peer from a tunnel, marking its children as disconnected.
686 * @param t Tunnel tree to use.
687 * @param peer Short ID of the peer to remove from the tunnel tree.
688 * @param cb Callback to notify client of disconnected peers.
690 * @return GNUNET_OK or GNUNET_SYSERR
693 tree_del_peer (struct MeshTunnelTree *t,
695 MeshNodeDisconnectCB cb)
697 struct MeshTunnelTreeNode *n;
698 struct MeshTunnelTreeNode *c;
699 struct MeshTunnelTreeNode *aux;
701 n = tree_del_path(t, peer, cb);
702 c = n->children_head;
706 GNUNET_CONTAINER_DLL_remove(n->children_head,
709 GNUNET_CONTAINER_DLL_insert(t->disconnected_head,
710 t->disconnected_tail,
715 tree_node_destroy(n);
721 * Print the tree on stderr
726 tree_debug(struct MeshTunnelTree *t)
728 tree_node_debug(t->root, 0);
733 * Iterator over hash map peer entries and frees all data in it.
734 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
737 * @param key current key code (will no longer contain valid data!!)
738 * @param value value in the hash map (treated as void *)
739 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
742 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
750 * Destroy the whole tree and free all used memory and Peer_Ids
752 * @param t Tree to be destroyed
755 tree_destroy (struct MeshTunnelTree *t)
757 tree_node_destroy(t->root);
758 GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
759 GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);